1996-07-30 13:30:15 -03:00
|
|
|
"""Guess which db package to use to open a db file."""
|
|
|
|
|
2001-03-02 02:43:49 -04:00
|
|
|
import os
|
2002-08-02 14:12:15 -03:00
|
|
|
import struct
|
|
|
|
|
|
|
|
try:
|
|
|
|
import dbm
|
|
|
|
_dbmerror = dbm.error
|
|
|
|
except ImportError:
|
|
|
|
dbm = None
|
|
|
|
# just some sort of valid exception which might be raised in the
|
|
|
|
# dbm test
|
|
|
|
_dbmerror = IOError
|
2001-03-02 02:43:49 -04:00
|
|
|
|
1996-07-30 13:30:15 -03:00
|
|
|
def whichdb(filename):
|
|
|
|
"""Guess which db package to use to open a db file.
|
|
|
|
|
|
|
|
Return values:
|
|
|
|
|
|
|
|
- None if the database file can't be read;
|
|
|
|
- empty string if the file can be read but can't be recognized
|
|
|
|
- the module name (e.g. "dbm" or "gdbm") if recognized.
|
|
|
|
|
|
|
|
Importing the given module may still fail, and opening the
|
|
|
|
database using that module may still fail.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Check for dbm first -- this has a .pag and a .dir file
|
|
|
|
try:
|
2001-10-24 17:42:55 -03:00
|
|
|
f = open(filename + os.extsep + "pag", "rb")
|
1998-03-26 17:13:24 -04:00
|
|
|
f.close()
|
2001-10-24 17:42:55 -03:00
|
|
|
f = open(filename + os.extsep + "dir", "rb")
|
1998-03-26 17:13:24 -04:00
|
|
|
f.close()
|
|
|
|
return "dbm"
|
1996-07-30 13:30:15 -03:00
|
|
|
except IOError:
|
2002-08-02 14:12:15 -03:00
|
|
|
# some dbm emulations based on Berkeley DB generate a .db file
|
|
|
|
# some do not, but they should be caught by the dbhash checks
|
|
|
|
try:
|
|
|
|
f = open(filename + os.extsep + "db", "rb")
|
|
|
|
f.close()
|
|
|
|
# guarantee we can actually open the file using dbm
|
|
|
|
# kind of overkill, but since we are dealing with emulations
|
|
|
|
# it seems like a prudent step
|
|
|
|
if dbm is not None:
|
|
|
|
d = dbm.open(filename)
|
|
|
|
d.close()
|
|
|
|
return "dbm"
|
|
|
|
except (IOError, _dbmerror):
|
|
|
|
pass
|
1996-07-30 13:30:15 -03:00
|
|
|
|
2000-07-29 02:31:40 -03:00
|
|
|
# Check for dumbdbm next -- this has a .dir and and a .dat file
|
2000-08-04 05:46:59 -03:00
|
|
|
try:
|
2003-06-14 05:16:34 -03:00
|
|
|
# First check for presence of files
|
|
|
|
sizes = os.stat(filename + os.extsep + "dat").st_size, \
|
|
|
|
os.stat(filename + os.extsep + "dir").st_size
|
|
|
|
# dumbdbm files with no keys are empty
|
|
|
|
if sizes == (0, 0):
|
|
|
|
return "dumbdbm"
|
2001-10-24 17:42:55 -03:00
|
|
|
f = open(filename + os.extsep + "dir", "rb")
|
2000-07-29 02:31:40 -03:00
|
|
|
try:
|
|
|
|
if f.read(1) in ["'", '"']:
|
|
|
|
return "dumbdbm"
|
|
|
|
finally:
|
|
|
|
f.close()
|
2003-06-14 05:16:34 -03:00
|
|
|
except (OSError, IOError):
|
2000-07-29 02:31:40 -03:00
|
|
|
pass
|
|
|
|
|
1996-07-30 13:30:15 -03:00
|
|
|
# See if the file exists, return None if not
|
|
|
|
try:
|
1998-03-26 17:13:24 -04:00
|
|
|
f = open(filename, "rb")
|
1996-07-30 13:30:15 -03:00
|
|
|
except IOError:
|
1998-03-26 17:13:24 -04:00
|
|
|
return None
|
1996-07-30 13:30:15 -03:00
|
|
|
|
1999-06-08 10:13:16 -03:00
|
|
|
# Read the start of the file -- the magic number
|
|
|
|
s16 = f.read(16)
|
1996-07-30 13:30:15 -03:00
|
|
|
f.close()
|
1999-06-08 10:13:16 -03:00
|
|
|
s = s16[0:4]
|
1996-07-30 13:30:15 -03:00
|
|
|
|
|
|
|
# Return "" if not at least 4 bytes
|
|
|
|
if len(s) != 4:
|
1998-03-26 17:13:24 -04:00
|
|
|
return ""
|
1996-07-30 13:30:15 -03:00
|
|
|
|
|
|
|
# Convert to 4-byte int in native byte order -- return "" if impossible
|
|
|
|
try:
|
1998-03-26 17:13:24 -04:00
|
|
|
(magic,) = struct.unpack("=l", s)
|
1996-07-30 13:30:15 -03:00
|
|
|
except struct.error:
|
1998-03-26 17:13:24 -04:00
|
|
|
return ""
|
1996-07-30 13:30:15 -03:00
|
|
|
|
|
|
|
# Check for GNU dbm
|
|
|
|
if magic == 0x13579ace:
|
1998-03-26 17:13:24 -04:00
|
|
|
return "gdbm"
|
1996-07-30 13:30:15 -03:00
|
|
|
|
2003-05-06 17:42:10 -03:00
|
|
|
# Check for old Berkeley db hash file format v2
|
1998-04-28 12:41:03 -03:00
|
|
|
if magic in (0x00061561, 0x61150600):
|
2003-05-06 17:42:10 -03:00
|
|
|
return "bsddb185"
|
1996-07-30 13:30:15 -03:00
|
|
|
|
2003-05-06 17:42:10 -03:00
|
|
|
# Later versions of Berkeley db hash file have a 12-byte pad in
|
|
|
|
# front of the file type
|
1999-06-08 10:13:16 -03:00
|
|
|
try:
|
2000-02-10 13:17:14 -04:00
|
|
|
(magic,) = struct.unpack("=l", s16[-4:])
|
1999-06-08 10:13:16 -03:00
|
|
|
except struct.error:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
# Check for BSD hash
|
|
|
|
if magic in (0x00061561, 0x61150600):
|
|
|
|
return "dbhash"
|
|
|
|
|
1996-07-30 13:30:15 -03:00
|
|
|
# Unknown
|
|
|
|
return ""
|