L
Lloyd Zusman
I'd like to use the DBM routines in ruby, and I want to force DBM.open
to be performed in read-only mode. However, looking at the source code
(in ext/dbm/dbm.c), it looks like ruby always tries to open the database
in read-write mode before trying it in read-only mode [see the
fdbm_initialize() routine, below].
Am I misinterpreting something?
In any case, how can I do a DBM.open with the guarantee that it will
never even try to open the file in read-write mode?
Thanks in advance.
Here's the code I that mentioned:
static VALUE
fdbm_initialize(argc, argv, obj)
int argc;
VALUE *argv;
VALUE obj;
{
VALUE file, vmode, vflags;
DBM *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 not exist */
}
else {
mode = NUM2INT(vmode);
}
if (!NIL_P(vflags))
flags = NUM2INT(vflags);
SafeStringValue(file);
if (flags & RUBY_DBM_RW_BIT) {
flags &= ~RUBY_DBM_RW_BIT;
dbm = dbm_open(RSTRING(file)->ptr, flags, mode);
}
else {
dbm = 0;
if (mode >= 0) {
dbm = dbm_open(RSTRING(file)->ptr, O_RDWR|O_CREAT, mode);
}
if (!dbm) {
dbm = dbm_open(RSTRING(file)->ptr, O_RDWR, 0);
}
if (!dbm) {
dbm = dbm_open(RSTRING(file)->ptr, O_RDONLY, 0);
}
}
if (!dbm) {
if (mode == -1) return Qnil;
rb_sys_fail(RSTRING(file)->ptr);
}
dbmp = ALLOC(struct dbmdata);
DATA_PTR(obj) = dbmp;
dbmp->di_dbm = dbm;
dbmp->di_size = -1;
return obj;
}
to be performed in read-only mode. However, looking at the source code
(in ext/dbm/dbm.c), it looks like ruby always tries to open the database
in read-write mode before trying it in read-only mode [see the
fdbm_initialize() routine, below].
Am I misinterpreting something?
In any case, how can I do a DBM.open with the guarantee that it will
never even try to open the file in read-write mode?
Thanks in advance.
Here's the code I that mentioned:
static VALUE
fdbm_initialize(argc, argv, obj)
int argc;
VALUE *argv;
VALUE obj;
{
VALUE file, vmode, vflags;
DBM *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 not exist */
}
else {
mode = NUM2INT(vmode);
}
if (!NIL_P(vflags))
flags = NUM2INT(vflags);
SafeStringValue(file);
if (flags & RUBY_DBM_RW_BIT) {
flags &= ~RUBY_DBM_RW_BIT;
dbm = dbm_open(RSTRING(file)->ptr, flags, mode);
}
else {
dbm = 0;
if (mode >= 0) {
dbm = dbm_open(RSTRING(file)->ptr, O_RDWR|O_CREAT, mode);
}
if (!dbm) {
dbm = dbm_open(RSTRING(file)->ptr, O_RDWR, 0);
}
if (!dbm) {
dbm = dbm_open(RSTRING(file)->ptr, O_RDONLY, 0);
}
}
if (!dbm) {
if (mode == -1) return Qnil;
rb_sys_fail(RSTRING(file)->ptr);
}
dbmp = ALLOC(struct dbmdata);
DATA_PTR(obj) = dbmp;
dbmp->di_dbm = dbm;
dbmp->di_size = -1;
return obj;
}