[prev] 33 [next]

Single-file Disk Manager (cont)

// start using DB 
DB openDatabase(char *name) { 
   DB db = new(DBdescriptor);
   db->dbname = strdup(name);
   db->fd = open(name,O_RDWR);
   db->map = readSpaceTable(db);
   db->names = readNameTable(db);
   return db;
}
// stop using DB and update all meta-data
void closeDatabase(DB db) {
   writeSpaceTable(db,db->map);
   writeNameTable(db,db->names);
   fsync(db->fd);  // ensure that changes reach disk
   close(db->fd);
   free(db);
}