Creates a newGDBM instance by opening a gdbm file named
filename. If the file does not exist, a new file with file mode mode will
be created. flags may be one of the following:
WRCREAT - open as a writer; if
the database does not exist, create a new one
NEWDB - open as a writer;
overwrite any existing databases
The valuesWRITER,WRCREAT
and NEWDB may be combined with the following values by bitwise or:
SYNC - cause all database operations to be synchronized to the
disk
NOLOCK - do not lock the database file
If no flags are specified, the GDBM
object will try to open the database
file as a writer and will create it if it does not already exist (cf. flag
WRCREAT). If this fails (for instance, if another process has
already opened the database as a reader), it will try to open the database file as a reader (cf.
flag READER).
static VALUE
fgdbm_initialize(int argc, VALUE *argv, VALUE obj)
{
VALUE file, vmode, vflags;
GDBM_FILE dbm;
struct dbmdata *dbmp;
int mode, flags = 0;
if (rb_scan_args(argc, argv, "12", &file, &vmode, &vflags) == 1) {
mode = 0666; /* default value */
}
else if (NIL_P(vmode)) {
mode = -1; /* return nil if DB does not exist */
}
else {
mode = NUM2INT(vmode);
}
if (!NIL_P(vflags))
flags = NUM2INT(vflags);
SafeStringValue(file);
if (flags & RUBY_GDBM_RW_BIT) {
flags &= ~RUBY_GDBM_RW_BIT;
dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE,
flags, mode, MY_FATAL_FUNC);
}
else {
dbm = 0;
if (mode >= 0)
dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE,
GDBM_WRCREAT|flags, mode, MY_FATAL_FUNC);
if (!dbm)
dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE,
GDBM_WRITER|flags, 0, MY_FATAL_FUNC);
if (!dbm)
dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE,
GDBM_READER|flags, 0, MY_FATAL_FUNC);
}
if (!dbm) {
if (mode == -1) return Qnil;
if (gdbm_errno == GDBM_FILE_OPEN_ERROR ||
gdbm_errno == GDBM_CANT_BE_READER ||
gdbm_errno == GDBM_CANT_BE_WRITER)
rb_sys_fail(RSTRING_PTR(file));
else
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
}
dbmp = ALLOC(struct dbmdata);
free_dbm(DATA_PTR(obj));
DATA_PTR(obj) = dbmp;
dbmp->di_dbm = dbm;
dbmp->di_size = -1;
return obj;
}