Home | History | Annotate | Download | only in dbm
      1 """Generic interface to all dbm clones.
      2 
      3 Use
      4 
      5         import dbm
      6         d = dbm.open(file, 'w', 0o666)
      7 
      8 The returned object is a dbm.gnu, dbm.ndbm or dbm.dumb object, dependent on the
      9 type of database being opened (determined by the whichdb function) in the case
     10 of an existing dbm. If the dbm does not exist and the create or new flag ('c'
     11 or 'n') was specified, the dbm type will be determined by the availability of
     12 the modules (tested in the above order).
     13 
     14 It has the following interface (key and data are strings):
     15 
     16         d[key] = data   # store data at key (may override data at
     17                         # existing key)
     18         data = d[key]   # retrieve data at key (raise KeyError if no
     19                         # such key)
     20         del d[key]      # delete data stored at key (raises KeyError
     21                         # if no such key)
     22         flag = key in d # true if the key exists
     23         list = d.keys() # return a list of all existing keys (slow!)
     24 
     25 Future versions may change the order in which implementations are
     26 tested for existence, and add interfaces to other dbm-like
     27 implementations.
     28 """
     29 
     30 __all__ = ['open', 'whichdb', 'error']
     31 
     32 import io
     33 import os
     34 import struct
     35 import sys
     36 
     37 
     38 class error(Exception):
     39     pass
     40 
     41 _names = ['dbm.gnu', 'dbm.ndbm', 'dbm.dumb']
     42 _defaultmod = None
     43 _modules = {}
     44 
     45 error = (error, OSError)
     46 
     47 try:
     48     from dbm import ndbm
     49 except ImportError:
     50     ndbm = None
     51 
     52 
     53 def open(file, flag='r', mode=0o666):
     54     """Open or create database at path given by *file*.
     55 
     56     Optional argument *flag* can be 'r' (default) for read-only access, 'w'
     57     for read-write access of an existing database, 'c' for read-write access
     58     to a new or existing database, and 'n' for read-write access to a new
     59     database.
     60 
     61     Note: 'r' and 'w' fail if the database doesn't exist; 'c' creates it
     62     only if it doesn't exist; and 'n' always creates a new database.
     63     """
     64     global _defaultmod
     65     if _defaultmod is None:
     66         for name in _names:
     67             try:
     68                 mod = __import__(name, fromlist=['open'])
     69             except ImportError:
     70                 continue
     71             if not _defaultmod:
     72                 _defaultmod = mod
     73             _modules[name] = mod
     74         if not _defaultmod:
     75             raise ImportError("no dbm clone found; tried %s" % _names)
     76 
     77     # guess the type of an existing database, if not creating a new one
     78     result = whichdb(file) if 'n' not in flag else None
     79     if result is None:
     80         # db doesn't exist or 'n' flag was specified to create a new db
     81         if 'c' in flag or 'n' in flag:
     82             # file doesn't exist and the new flag was used so use default type
     83             mod = _defaultmod
     84         else:
     85             raise error[0]("need 'c' or 'n' flag to open new db")
     86     elif result == "":
     87         # db type cannot be determined
     88         raise error[0]("db type could not be determined")
     89     elif result not in _modules:
     90         raise error[0]("db type is {0}, but the module is not "
     91                        "available".format(result))
     92     else:
     93         mod = _modules[result]
     94     return mod.open(file, flag, mode)
     95 
     96 
     97 def whichdb(filename):
     98     """Guess which db package to use to open a db file.
     99 
    100     Return values:
    101 
    102     - None if the database file can't be read;
    103     - empty string if the file can be read but can't be recognized
    104     - the name of the dbm submodule (e.g. "ndbm" or "gnu") if recognized.
    105 
    106     Importing the given module may still fail, and opening the
    107     database using that module may still fail.
    108     """
    109 
    110     # Check for ndbm first -- this has a .pag and a .dir file
    111     try:
    112         f = io.open(filename + ".pag", "rb")
    113         f.close()
    114         f = io.open(filename + ".dir", "rb")
    115         f.close()
    116         return "dbm.ndbm"
    117     except OSError:
    118         # some dbm emulations based on Berkeley DB generate a .db file
    119         # some do not, but they should be caught by the bsd checks
    120         try:
    121             f = io.open(filename + ".db", "rb")
    122             f.close()
    123             # guarantee we can actually open the file using dbm
    124             # kind of overkill, but since we are dealing with emulations
    125             # it seems like a prudent step
    126             if ndbm is not None:
    127                 d = ndbm.open(filename)
    128                 d.close()
    129                 return "dbm.ndbm"
    130         except OSError:
    131             pass
    132 
    133     # Check for dumbdbm next -- this has a .dir and a .dat file
    134     try:
    135         # First check for presence of files
    136         os.stat(filename + ".dat")
    137         size = os.stat(filename + ".dir").st_size
    138         # dumbdbm files with no keys are empty
    139         if size == 0:
    140             return "dbm.dumb"
    141         f = io.open(filename + ".dir", "rb")
    142         try:
    143             if f.read(1) in (b"'", b'"'):
    144                 return "dbm.dumb"
    145         finally:
    146             f.close()
    147     except OSError:
    148         pass
    149 
    150     # See if the file exists, return None if not
    151     try:
    152         f = io.open(filename, "rb")
    153     except OSError:
    154         return None
    155 
    156     with f:
    157         # Read the start of the file -- the magic number
    158         s16 = f.read(16)
    159     s = s16[0:4]
    160 
    161     # Return "" if not at least 4 bytes
    162     if len(s) != 4:
    163         return ""
    164 
    165     # Convert to 4-byte int in native byte order -- return "" if impossible
    166     try:
    167         (magic,) = struct.unpack("=l", s)
    168     except struct.error:
    169         return ""
    170 
    171     # Check for GNU dbm
    172     if magic in (0x13579ace, 0x13579acd, 0x13579acf):
    173         return "dbm.gnu"
    174 
    175     # Later versions of Berkeley db hash file have a 12-byte pad in
    176     # front of the file type
    177     try:
    178         (magic,) = struct.unpack("=l", s16[-4:])
    179     except struct.error:
    180         return ""
    181 
    182     # Unknown
    183     return ""
    184 
    185 
    186 if __name__ == "__main__":
    187     for filename in sys.argv[1:]:
    188         print(whichdb(filename) or "UNKNOWN", filename)
    189