1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License, Version 1.0 only
   6  * (the "License").  You may not use this file except in compliance
   7  * with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  *      db_dictionary_c.x
  24  *
  25  * Copyright 2015 Gary Mills
  26  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
  27  * Use is subject to license terms.
  28  */
  29  
  30 #if RPC_XDR
  31 %#include "xdr_nullptr.h"
  32 #endif /* RPC_XDR */
  33  
  34 #if RPC_HDR
  35 %#ifndef _DB_DICTIONARY_H
  36 %#define _DB_DICTIONARY_H
  37 
  38 #ifdef USINGC
  39 %#include "nisdb_rw.h"
  40 %#include "nisdb_ldap.h"
  41 %#include "db_entry_c.h"
  42 %#include "db_scheme_c.h"
  43 %#include "db_vers_c.h"
  44 %#include "ldap_xdr.h"
  45 %typedef void *nullptr;
  46 %typedef u_int db_dict_version;
  47 #else
  48 %#include "nisdb_rw.h"
  49 %#include "nisdb_ldap.h"
  50 %#include "db_entry.h"
  51 %#include "db_scheme.h"
  52 %#include "db.h"
  53 %#include "db_vers.h"
  54 %#include "db_dictlog.h"
  55 %#include "ldap_xdr.h"
  56 #endif /* USINGC */
  57 #endif /* RPC_HDR */
  58 
  59 struct db_table_desc {
  60   string table_name<NIS_MAXNAMELEN>;
  61   u_long hashval;
  62   db_scheme * scheme;
  63 #ifdef USINGC
  64   nullptr database;   /* for XDR, keep database from descriptor */
  65 #else
  66   db *database;        /* for program use in c++ code */
  67 #endif /* USINGC */
  68   db_table_desc *next;
  69 };
  70 typedef struct db_table_desc * db_table_desc_p;
  71 
  72 /* Defining own version of xdr_db_dict_version */
  73 #if RPC_HDR
  74 #ifndef USINGC
  75 typedef u_int db_dict_version;
  76 %bool_t xdr_db_dict_version();
  77 #endif /* USINGC */
  78 
  79 typedef char * db_table_namep;
  80 typedef db_table_namep db_table_names<>;
  81 
  82 /* Defining own version of xdr_db_dict_desc */
  83 #ifndef USINGC
  84 struct db_dict_desc {
  85         db_dict_version impl_vers;
  86         db_table_desc_p tables<>;
  87         int count;
  88 };
  89 #else
  90 %struct db_dict_desc {
  91 %       db_dict_version impl_vers;
  92 %       struct {
  93 %               u_int tables_len;
  94 %               db_table_desc_p *tables_val;
  95 %       } tables;
  96 %       int count;
  97 %};
  98 %typedef struct db_dict_desc db_dict_desc;
  99 %bool_t xdr_db_dict_desc();
 100 #endif /* USINGC */
 101 
 102 #endif
 103 
 104 typedef struct db_dict_desc * db_dict_desc_p;
 105 
 106 #if     RPC_HDR
 107 struct __nisdb_dictionary_defer_struct {
 108         db_dict_desc            *dictionary;    /* Old incarnation */
 109 };
 110 typedef struct __nisdb_dictionary_defer_struct  __nisdb_dictionary_defer_t;
 111 #ifdef  USINGC
 112 %bool_t xdr___nisdb_dictionary_defer_t();
 113 #endif  /* USINGC */
 114 #endif
 115 
 116 #ifndef USINGC
 117 #ifdef RPC_HDR
 118 %class db_dictionary {
 119 %  db_dict_desc_p dictionary;
 120 %  bool_t initialized;
 121 %  char* filename;
 122 %  char* tmpfilename;
 123 %  char* logfilename;
 124 %  db_dictlog *logfile;
 125 %  bool_t logfile_opened;
 126 %  bool_t changed;
 127 %  __nisdb_dictionary_defer_t deferred;
 128 %  __nisdb_flag_t noWriteThrough;
 129 %  STRUCTRWLOCK(dict);
 130 %
 131 %/* Dump contents of this dictionary (minus the database representation)
 132 %     to its file. Returns 0 if operation succeeds, -1 otherwise. */
 133 %  int dump();
 134 %
 135 %/* Delete old log file and descriptor */
 136 %  int reset_log();
 137 %
 138 %/* Open log file (and creates descriptor) if it has not been opened */
 139 %  int open_log();
 140 %
 141 %/* Incorporate updates in log to dictionary already loaded.
 142 %   Does not affect "logfile" */
 143 %  int incorporate_log( char * );
 144 %
 145 %  /* closes log file if opened */
 146 %  int close_log();
 147 %
 148 %/*  Log the given action and execute it.
 149 %    The minor version of the dictionary is updated after the action has
 150 %    been executed and the dictionary is flagged as being changed.
 151 %    Return the structure db_result, or NULL if the loggin failed or the
 152 %    action is unknown. */
 153 %  db_status log_action(int, char* table, table_obj* tobj =0);
 154 %
 155 %  db_status create_table_desc(char* table_name, table_obj* table_desc,
 156 %                              db_table_desc**);
 157 %
 158 %  db_dict_desc_p db_copy_dictionary(void);
 159 %
 160 % public:
 161 %/* Constructor:  creates an empty, uninitialized dictionary. */
 162 %  db_dictionary();
 163 %
 164 %/* Destructor: noop. Use db_shutdown if you really want to clean up. */
 165 %  ~db_dictionary() {}
 166 %
 167 %  db_status merge_dict (db_dictionary&, char *, char *);
 168 %
 169 %  db_status massage_dict (char *, char *, char *);
 170 %  int       db_clone_bucket (db_table_desc *, db_table_desc_p *);
 171 %  int       change_table_name (db_table_desc *, char *, char *);
 172 %  bool_t    extract_entries (db_dictionary&, char **, int );
 173 %
 174 %/* Real destructor: deletes filename and table descriptors */
 175 %  db_status db_shutdown();
 176 %
 177 %/*  Initialize dictionary from contents in 'file'.
 178 %    If there is already information in this dictionary, it is removed.
 179 %    Therefore, regardless of whether the load from the file succeeds,
 180 %    the contents of this dictionary will be altered.  Returns
 181 %    whether table has been initialized successfully. */
 182 %  bool_t init( char* fname );
 183 %  bool_t inittemp( char* fname, db_dictionary&);
 184 %
 185 %/* closes any open log files for all tables in dictionary or 'tab'.
 186 %   "tab" is an optional argument.
 187 % */
 188 %   db_status db_standby( char* tab = 0 );
 189 %
 190 %/* Write out in-memory copy of dictionary to file.
 191 %   1.  Update major version.
 192 %   2.  Dump contents to temporary file.
 193 %   3.  Rename temporary file to real dictionary file.
 194 %   4.  Remove log file. 
 195 %   A checkpoint is done only if it has changed since the previous checkpoint.
 196 %   Returns TRUE if checkpoint was successful; FALSE otherwise. */
 197 %  db_status checkpoint();
 198 %
 199 %/*  Checkpoints table specified by 'tab', or all tables if 'tab' is 0. */
 200 %   db_status db_checkpoint( char* tab = 0 );
 201 
 202 %/* Add table with given name 'tab' and description 'zdesc' to dictionary.
 203 %   Returns error code if table already exists, or if no memory can be found
 204 %   to store the descriptor, or if dictionary has not been intialized.  
 205 %   Dictionary is updated to stable store before addition.
 206 %   Fatal error occurs if dictionary cannot be saved.
 207 %   Returns DB_SUCCESS if dictionary has been updated successfully. */
 208 %  db_status add_table_aux(char* table_name, table_obj* table_desc, int mode);
 209 %
 210 %/* Delete table with given name 'tab' from dictionary.
 211 %   Returns error code if table does not exist or if dictionary has not been 
 212 %   initialized.   Dictionary is updated to stable store if deletion is 
 213 %   successful.  Fatal error occurs if dictionary cannot be saved.   
 214 %   Returns DB_SUCCESS if dictionary has been updated successfully.
 215 %   Note that the files associated with the table are also removed.  */
 216 %  db_status delete_table_aux( char* table_name, int mode );
 217 %
 218 %  db_status add_table( char* table_name, table_obj* table_desc );
 219 %  int copyfile( char* infile, char *outfile);
 220 %
 221 %  db_status delete_table( char* table_name );
 222 %
 223 %/* Return database structure of table named by 'table_name'.
 224 %   If 'where' is set, set it to the table_desc of 'table_name.'
 225 %   The database is loaded in from stable store if it has not been loaded.
 226 %   If it cannot be loaded, it is initialized using the scheme stored in
 227 %   the table_desc.  NULL is returned if the initialization fails.   */
 228 %  db* find_table( char* table_name, db_table_desc ** where = NULL );
 229 %
 230 %  db *find_table(char *table_name, db_table_desc **where,
 231 %                       bool_t searchDeferred);
 232 %  db *find_table(char *table_name, db_table_desc **where,
 233 %                       bool_t searchDeferred, bool_t doLDAP,
 234 %                       bool_t doLoad);
 235 %
 236 %  db *find_table_noLDAP(char *table_name, db_table_desc **where,
 237 %                       bool_t searchDeferred, bool_t doLoad);
 238 %
 239 %/* Returns db_table_desc of table name 'tab'.
 240 %   Use this if you do not want table to be loaded. */
 241 %  db_table_desc * find_table_desc( char* table_name );
 242 %
 243 %  db_table_desc * find_table_desc(char *table_name, bool_t searchDeferred);
 244 %
 245 %/* Translate given nis attribute list to a db_query structure. 
 246 %   Return FALSE if dictionary has not been initialized, or 
 247 %   table does not have a scheme (which should be a fatal error?). */
 248 %  db_query * translate_to_query( db_table_desc*, int, nis_attr * );
 249 %
 250 %/* Return an array of strings of table names of all tables in dictionary. */
 251 %   db_table_names * get_table_names(); 
 252 %
 253 %/* Set/clear no-write-through flag */
 254 %  void setNoWriteThrough(void);
 255 %  void clearNoWriteThrough(void);
 256 %
 257 %/* Locking */
 258 %  int acqexcl(void) {
 259 %       return(WLOCK(dict));
 260 %  }
 261 %
 262 %  int relexcl(void) {
 263 %       return (WULOCK(dict));
 264 %  }
 265 %
 266 %  int acqnonexcl(void) {
 267 %       return (RLOCK(dict));
 268 %  }
 269 %
 270 %  int relnonexcl(void) {
 271 %       return (RULOCK(dict));
 272 %  }
 273 %
 274 %/* Set deferred commit mode; intended for replica resync */
 275 %  db_status defer(char *table);
 276 %
 277 %/* Commit deferred changes; intended for replica resync */
 278 %  db_status commit(char *table);
 279 %
 280 %/* Roll back deferred changes; intended for replica resync */
 281 %  db_status rollback(char *table);
 282 %};
 283 %#ifdef __STDC__
 284 %extern "C" bool_t xdr_db_table_desc_p(XDR *, db_table_desc_p *); 
 285 %extern "C" bool_t xdr_db_table_desc(XDR *, db_table_desc *); 
 286 %extern "C" bool_t xdr_db_dict_desc_p(XDR *, db_dict_desc_p *); 
 287 %extern "C" bool_t xdr_db_table_namep(XDR *, db_table_namep *); 
 288 %extern "C" bool_t xdr_db_table_names(XDR *, db_table_names *); 
 289 %#endif
 290 
 291 #endif /* RPC_HDR */
 292 #endif /* USINGC */
 293 
 294 #if RPC_HDR
 295 %#endif /* _DB_DICTIONARY_H */
 296 #endif /* RPC_HDR */
 297