home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / emerald / emrldsys.lha / Kernel / Checkpoint / cPDBM.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-17  |  3.8 KB  |  160 lines

  1. /*
  2.   cPDBM.c - Emerald checkPoint database manager
  3.  
  4.   Copyright 1987, 1988 Clinton Jeffery
  5.   Last edit: 1/29/88
  6.  
  7.   This module manages database operations.  The database is kept both
  8.   in the standard Emerald Object Table and on disk in an NDBM(1) file.
  9.   Database entries are exactly the object descriptors for all
  10.   objects which have invoked a checkpoint operation.  This
  11.   violently simplifies the reconstruction of the object table.
  12.  
  13.   Operations are:
  14.   Load a database entry from disk into an object table entry,
  15.   Read the database (from the object table), and
  16.   Write the database (write-through Object Table to disk).
  17.   Initialize the database (open/create)
  18.   Finalize the database
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <sys/file.h>
  23. #include <ndbm.h>
  24. #include "Kernel/h/kmdTypes.h"
  25. #include "Kernel/h/emTypes.h"
  26. #include "Kernel/h/cPDBM.h"
  27. #include "Kernel/h/cPLog.h"
  28.  
  29. extern ODP OTLookup();
  30. extern char NODEDIR[];
  31.  
  32. #define     CPDBFILENAME  "CheckPointDB"
  33. static char CP_DBFileName[200];
  34.  
  35. #ifdef CPNEWLOG
  36. char newCP_DBFileName[200];
  37. #endif
  38.  
  39. DBM *CP_DBM;
  40. #ifdef CPNEWLOG
  41. DBM *newCP_DBM;
  42. #endif
  43. static first_db_load = 1;
  44.  
  45. #ifdef CPNEWLOG
  46. CP_SwitchDbase()
  47. {
  48.   char junk[256];
  49.  
  50.   dbm_close(CP_DBM);
  51.   dbm_close(newCP_DBM);
  52.  
  53.   sprintf(junk,"mv %s.dir %s.dir",newCP_DBFileName,CP_DBFileName);
  54.   system(junk);
  55.   sprintf(junk,"mv %s.pag %s.pag",newCP_DBFileName,CP_DBFileName);
  56.   system(junk);
  57.  
  58.   CP_DBM = dbm_open(CP_DBFileName, O_RDWR, 0666 );
  59.   return CP_DBM!=NULL;
  60. }
  61. #endif
  62.  
  63. /* load_dbase - return object table entries for every
  64.  * object upon whom a checkpoint has been invoked.
  65.  * Call this iteratively until it returns a null.
  66.  */
  67. GODP CP_LoadDbase()
  68. {
  69.   datum temp;
  70.   register int i;
  71.   register char *result, *res, *src;
  72.   char *emalloc();
  73.  
  74.   if (first_db_load) {
  75.     temp = dbm_firstkey(CP_DBM);
  76.     first_db_load = 0;
  77.   } else {
  78.     temp = dbm_nextkey(CP_DBM);
  79.   }
  80.   if (temp.dptr != NULL) {
  81.     temp = dbm_fetch(CP_DBM,temp);
  82.     if(temp.dsize!=sizeof(OD)) {
  83.       KMDTrace("Checkpoint", 3,
  84.            "CP_DBM error #1: DB entry size %u should be %u\n",
  85.            temp.dsize,sizeof(OD));
  86.     }
  87.     /* copy DBM entry into memory we can use as an OTE */
  88.     if((result = emalloc(sizeof(OD)))==NULL){
  89.       KMDTrace("Checkpoint", 3, "CP_DBM emalloc fails!\n");
  90.       return ((GODP)NULL);
  91.     }
  92.     for(i=0,res=result,src=temp.dptr;i<sizeof(OD);i++){
  93.       *res++ = *src++;
  94.     }
  95.     return ((GODP)result);
  96.   } else return ((GODP) NULL);
  97. }
  98.  
  99. void CP_ReadDbase(obj,offset,size)
  100. OID obj;
  101. long *offset,*size; /* return values */
  102. {
  103.   ODP theODP;
  104.  
  105.   /* actually just a quick lookup in object table */
  106.   theODP  = OTLookup(obj);
  107.   *offset = theODP->G.cPOffset;
  108.   *size   = theODP->G.cPSize;
  109. }
  110.  
  111. /*
  112.  * update the object table and the dbase.
  113.  */
  114. int CP_WriteDbase(obj,offset,size,db)
  115. ODP obj;
  116. long offset,size;
  117. DBM *db;
  118. {
  119.   datum key, content;
  120.   char *name;
  121.  
  122.   /* update the object table CP fields */
  123.   obj->G.cPSize = size;
  124.   obj->G.cPOffset = offset;
  125.  
  126.   if(obj->G.cPServerLoc == (unsigned)EMNIL) obj->G.cPServerLoc = obj->G.ownLoc;
  127.  
  128.   /* make the appropriate NDBM call */
  129.   key.dptr      = (char *) (&(obj->G.ownOID));
  130.   key.dsize     = sizeof(obj->G.ownOID);
  131.   content.dptr  = (char *)obj;
  132.   content.dsize = sizeof(OD);
  133.   if(dbm_store(db,key,content,DBM_REPLACE)){
  134.     FAIL("CP_DBM error #2: DB store fails\n");
  135.   }
  136.  
  137.   /* flush the dbase -- too lazy to find out if we could skip this */
  138.   name = ((db==CP_DBM)?(CP_DBFileName):(newCP_DBFileName));
  139.   dbm_close(db);
  140.   db = dbm_open(name, O_RDWR, 0666 ); /* ugo+rw */
  141.  
  142.   return db!=NULL;
  143. }
  144.  
  145. int CP_InitDbase()
  146. {
  147.   sprintf(CP_DBFileName, "%s/%s", NODEDIR, CPDBFILENAME);
  148. #ifdef CPNEWLOG
  149.   sprintf(newCP_DBFileName, "%s%s", CP_DBFileName, "cppnewlog");
  150.   newCP_DBM = dbm_open(newCP_DBFileName,O_CREAT | O_RDWR, 0666 );
  151. #endif
  152.   CP_DBM = dbm_open(CP_DBFileName,O_CREAT | O_RDWR, 0666 ); /* ugo+rw */
  153.   return CP_DBM!=NULL;
  154. }
  155.  
  156. void CP_FinalizeDbase()
  157. {
  158.   dbm_close(CP_DBM);
  159. }
  160.