Forcing DBM.open to be read-only?

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;
}
 
M

Mauricio Fernández

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?

maybe DBM.open(filename, 0666, DBM::READER) ?
(that sets the RUBY_DBM_RW_BIT... )
if (rb_scan_args(argc, argv, "12", &file, &vmode, &vflags) == 1) {
mode = 0666; /* default value */
} [...]
if (flags & RUBY_DBM_RW_BIT) {
flags &= ~RUBY_DBM_RW_BIT;
dbm = dbm_open(RSTRING(file)->ptr, flags, mode);
}
 
L

Lloyd Zusman

Mauricio Fernández said:
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?

maybe DBM.open(filename, 0666, DBM::READER) ?
(that sets the RUBY_DBM_RW_BIT... )

That logic is a little confusing to me: by _setting_ RUBY_DBM_RW_BIT in
the flags that are passed to DBM.open, that causes the bit to get
_cleared_ before the dbm_open call gets made. That seems
counter-intuitive, but as long as it works, I'm not going to
complain. :)

Thanks.

if (rb_scan_args(argc, argv, "12", &file, &vmode, &vflags) == 1) {
mode = 0666; /* default value */
} [...]
if (flags & RUBY_DBM_RW_BIT) {
flags &= ~RUBY_DBM_RW_BIT;
dbm = dbm_open(RSTRING(file)->ptr, flags, mode);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top