/* * call-seq: * SDBM.new(filename, mode = 0666) * * Creates a new database handle by opening the given +filename+. SDBM actually * uses two physical files, with extensions '.dir' and '.pag'. These extensions * will automatically be appended to the +filename+. * * If the file does not exist, a new file will be created using the given * +mode+, unless +mode+ is explicitly set to nil. In the latter case, no * database will be created. * * If the file exists, it will be opened in read/write mode. If this fails, it * will be opened in read-only mode. */ static VALUE fsdbm_initialize(int argc, VALUE *argv, VALUE obj) { volatile VALUE file; VALUE vmode; DBM *dbm; struct dbmdata *dbmp; int mode; if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) { mode = 0666; /* default value */ } else if (NIL_P(vmode)) { mode = -1; /* return nil if DB not exist */ } else { mode = NUM2INT(vmode); } FilePathValue(file); dbm = 0; if (mode >= 0) dbm = sdbm_open(RSTRING_PTR(file), O_RDWR|O_CREAT, mode); if (!dbm) dbm = sdbm_open(RSTRING_PTR(file), O_RDWR, 0); if (!dbm) dbm = sdbm_open(RSTRING_PTR(file), O_RDONLY, 0); if (!dbm) { if (mode == -1) return Qnil; rb_sys_fail_str(file); } dbmp = ALLOC(struct dbmdata); DATA_PTR(obj) = dbmp; dbmp->di_dbm = dbm; dbmp->di_size = -1; return obj; }
static int open_dbm (const char *filename, int rdonly, int type) { SDBM *db; Dbm *dbm; int perm; if (rdonly) perm = O_RDONLY; else perm = O_RDWR|O_CREAT; if (!(db = sdbm_open(filename, perm, 0660))) { yell("open_dbm(%s) failed: %s", filename, strerror(errno)); return -1; } dbm = new_dbm(db, type); return dbm->refnum; }
int main(int argc, char *argv[]) { DBM *db; datum key, val; int count = 1; if (argc != 2) { printf("%s - dumps mod_dav lock database\n", argv[0]); printf("%s: no database specified (don't include extension)\n", argv[0]); return(-1); } if (CheckFile(argv, ".pag")) return(-1); if (CheckFile(argv, ".dir")) return(-1); db = sdbm_open((char *) argv[1], O_RDONLY | O_BINARY, DAV_FS_MODE_FILE); key = sdbm_firstkey(db); if (key.dsize == 0) { printf("%s: no outstanding locks.\n", argv[0]); } else { while(key.dsize) { printf("[Lock record #%3d]\n", count++); val = sdbm_fetch(db, key); if (display_info(key, val) == -1) return(-1); key = sdbm_nextkey(db); } } sdbm_close(db); exit(0); }