home *** CD-ROM | disk | FTP | other *** search
- /*
- cPDBM.c - Emerald checkPoint database manager
-
- Copyright 1987, 1988 Clinton Jeffery
- Last edit: 1/29/88
-
- This module manages database operations. The database is kept both
- in the standard Emerald Object Table and on disk in an NDBM(1) file.
- Database entries are exactly the object descriptors for all
- objects which have invoked a checkpoint operation. This
- violently simplifies the reconstruction of the object table.
-
- Operations are:
- Load a database entry from disk into an object table entry,
- Read the database (from the object table), and
- Write the database (write-through Object Table to disk).
- Initialize the database (open/create)
- Finalize the database
- */
-
- #include <stdio.h>
- #include <sys/file.h>
- #include <ndbm.h>
- #include "Kernel/h/kmdTypes.h"
- #include "Kernel/h/emTypes.h"
- #include "Kernel/h/cPDBM.h"
- #include "Kernel/h/cPLog.h"
-
- extern ODP OTLookup();
- extern char NODEDIR[];
-
- #define CPDBFILENAME "CheckPointDB"
- static char CP_DBFileName[200];
-
- #ifdef CPNEWLOG
- char newCP_DBFileName[200];
- #endif
-
- DBM *CP_DBM;
- #ifdef CPNEWLOG
- DBM *newCP_DBM;
- #endif
- static first_db_load = 1;
-
- #ifdef CPNEWLOG
- CP_SwitchDbase()
- {
- char junk[256];
-
- dbm_close(CP_DBM);
- dbm_close(newCP_DBM);
-
- sprintf(junk,"mv %s.dir %s.dir",newCP_DBFileName,CP_DBFileName);
- system(junk);
- sprintf(junk,"mv %s.pag %s.pag",newCP_DBFileName,CP_DBFileName);
- system(junk);
-
- CP_DBM = dbm_open(CP_DBFileName, O_RDWR, 0666 );
- return CP_DBM!=NULL;
- }
- #endif
-
- /* load_dbase - return object table entries for every
- * object upon whom a checkpoint has been invoked.
- * Call this iteratively until it returns a null.
- */
- GODP CP_LoadDbase()
- {
- datum temp;
- register int i;
- register char *result, *res, *src;
- char *emalloc();
-
- if (first_db_load) {
- temp = dbm_firstkey(CP_DBM);
- first_db_load = 0;
- } else {
- temp = dbm_nextkey(CP_DBM);
- }
- if (temp.dptr != NULL) {
- temp = dbm_fetch(CP_DBM,temp);
- if(temp.dsize!=sizeof(OD)) {
- KMDTrace("Checkpoint", 3,
- "CP_DBM error #1: DB entry size %u should be %u\n",
- temp.dsize,sizeof(OD));
- }
- /* copy DBM entry into memory we can use as an OTE */
- if((result = emalloc(sizeof(OD)))==NULL){
- KMDTrace("Checkpoint", 3, "CP_DBM emalloc fails!\n");
- return ((GODP)NULL);
- }
- for(i=0,res=result,src=temp.dptr;i<sizeof(OD);i++){
- *res++ = *src++;
- }
- return ((GODP)result);
- } else return ((GODP) NULL);
- }
-
- void CP_ReadDbase(obj,offset,size)
- OID obj;
- long *offset,*size; /* return values */
- {
- ODP theODP;
-
- /* actually just a quick lookup in object table */
- theODP = OTLookup(obj);
- *offset = theODP->G.cPOffset;
- *size = theODP->G.cPSize;
- }
-
- /*
- * update the object table and the dbase.
- */
- int CP_WriteDbase(obj,offset,size,db)
- ODP obj;
- long offset,size;
- DBM *db;
- {
- datum key, content;
- char *name;
-
- /* update the object table CP fields */
- obj->G.cPSize = size;
- obj->G.cPOffset = offset;
-
- if(obj->G.cPServerLoc == (unsigned)EMNIL) obj->G.cPServerLoc = obj->G.ownLoc;
-
- /* make the appropriate NDBM call */
- key.dptr = (char *) (&(obj->G.ownOID));
- key.dsize = sizeof(obj->G.ownOID);
- content.dptr = (char *)obj;
- content.dsize = sizeof(OD);
- if(dbm_store(db,key,content,DBM_REPLACE)){
- FAIL("CP_DBM error #2: DB store fails\n");
- }
-
- /* flush the dbase -- too lazy to find out if we could skip this */
- name = ((db==CP_DBM)?(CP_DBFileName):(newCP_DBFileName));
- dbm_close(db);
- db = dbm_open(name, O_RDWR, 0666 ); /* ugo+rw */
-
- return db!=NULL;
- }
-
- int CP_InitDbase()
- {
- sprintf(CP_DBFileName, "%s/%s", NODEDIR, CPDBFILENAME);
- #ifdef CPNEWLOG
- sprintf(newCP_DBFileName, "%s%s", CP_DBFileName, "cppnewlog");
- newCP_DBM = dbm_open(newCP_DBFileName,O_CREAT | O_RDWR, 0666 );
- #endif
- CP_DBM = dbm_open(CP_DBFileName,O_CREAT | O_RDWR, 0666 ); /* ugo+rw */
- return CP_DBM!=NULL;
- }
-
- void CP_FinalizeDbase()
- {
- dbm_close(CP_DBM);
- }
-