How to use ruby read/write Berkeley DB file?

Hi, all!

I create a Berkeley DB file for Vsftpd, I want user Ruby add new Vsftpd
virtual users via edit the Berkeley DB file.

I create BDB file as follow:

$ cat > test.txt << EOF
bar
Hello,
foo
World!
EOF

$ db48_load -T -t hash -f ./test.txt ./test.db

$ file test.db

test.db: Berkeley DB (Hash, version 9, native byte-order)

$ db48_dump -p test.db

VERSION=3
format=print
type=hash
h_nelem=2
db_pagesize=4096
HEADER=END
foo
World!
bar
Hello,
DATA=END

$ irb

2.0.0-p353 :001 > Dir.entries(Dir.pwd())
=> [".", "..", "test.txt", "test.db"]
2.0.0-p353 :002 > require 'dbm'
=> true
2.0.0-p353 :003 > DBM.open("./test.db") do |db|
2.0.0-p353 :004 > db.each_pair {|key, value| print "#{key}: #{value}\n"}
2.0.0-p353 :005?> end
=> #<DBM:0x00000002185590>
2.0.0-p353 :006 > Dir.entries(Dir.pwd())
=> [".", "..", "test.txt", "test.db", "test.db.pag", "test.db.dir"]
2.0.0-p353 :007 >

$ file test.db.dir

test.db.dir: GNU dbm 2.x database

I do not understand, why DBM cannot read Berkeley DB file, but can create a
GNU dmb file?

Thanks & Regards

Did you try not providing the file extension? In other words:

DBM.open("test") do |db|
   db.each_pair {|key, value| print "#{key}: #{value}\n"}
end

According to the documentation, it will figure out the extension automatically: http://rdoc.info/stdlib/dbm/DBM:initialize

As you can see, it thought you wanted to create a new DB called "test.db" and created the "test.db.pag" and "test.db.dir" files itself.

-Justin

ยทยทยท

On 01/02/2014 01:21 AM, Terry Zheng wrote:

Hi, all!

I create a Berkeley DB file for Vsftpd, I want user Ruby add new Vsftpd
virtual users via edit the Berkeley DB file.

I create BDB file as follow:
> $ cat > test.txt << EOF
>bar
>Hello,
>foo
>World!
>EOF

> $ db48_load -T -t hash -f ./test.txt ./test.db

> $ file test.db
test.db: Berkeley DB (Hash, version 9, native byte-order)

> $ db48_dump -p test.db
VERSION=3
format=print
type=hash
h_nelem=2
db_pagesize=4096
HEADER=END
  foo
  World!
  bar
  Hello,
DATA=END

> $ irb
2.0.0-p353 :001 > Dir.entries(Dir.pwd())
  => [".", "..", "test.txt", "test.db"]
2.0.0-p353 :002 > require 'dbm'
  => true
2.0.0-p353 :003 > DBM.open("./test.db") do |db|
2.0.0-p353 :004 > db.each_pair {|key, value| print "#{key}: #{value}\n"}
2.0.0-p353 :005?> end
  => #<DBM:0x00000002185590>
2.0.0-p353 :006 > Dir.entries(Dir.pwd())
  => [".", "..", "test.txt", "test.db", "test.db.pag", "test.db.dir"]
2.0.0-p353 :007 >

> $ file test.db.dir
test.db.dir: GNU dbm 2.x database

I do not understand, why DBM cannot read Berkeley DB file, but can
create a GNU dmb file?

Thanks & Regards