blob: 6831a844073740d93db8c19ae1bc9a9b57625445 [file] [log] [blame]
Georg Brandl0a7ac7d2008-05-26 10:29:35 +00001"""Generic interface to all dbm clones.
2
3Use
4
5 import dbm
6 d = dbm.open(file, 'w', 0o666)
7
Victor Stinner1cec3e32010-07-29 16:26:56 +00008The returned object is a dbm.gnu, dbm.ndbm or dbm.dumb object, dependent on the
9type of database being opened (determined by the whichdb function) in the case
10of an existing dbm. If the dbm does not exist and the create or new flag ('c'
11or 'n') was specified, the dbm type will be determined by the availability of
12the modules (tested in the above order).
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000013
14It 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
25Future versions may change the order in which implementations are
Éric Araujof8e1b602011-04-20 18:52:55 +020026tested for existence, and add interfaces to other dbm-like
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000027implementations.
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000028"""
29
Antoine Pitroua3280292009-10-10 21:28:29 +000030__all__ = ['open', 'whichdb', 'error']
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000031
32import io
33import os
34import struct
35import sys
36
37
38class error(Exception):
39 pass
40
Victor Stinner1cec3e32010-07-29 16:26:56 +000041_names = ['dbm.gnu', 'dbm.ndbm', 'dbm.dumb']
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000042_defaultmod = None
43_modules = {}
44
Andrew Svetlovf7a17b42012-12-25 16:47:37 +020045error = (error, OSError)
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000046
Ezio Melottib08495b2013-07-07 13:15:08 +020047try:
48 from dbm import ndbm
49except ImportError:
50 ndbm = None
51
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000052
Éric Araujof8e1b602011-04-20 18:52:55 +020053def 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 """
Georg Brandlb17acad2008-05-28 08:43:17 +000064 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
briancurtin525c25d2011-03-14 16:03:54 -040077 # guess the type of an existing database, if not creating a new one
78 result = whichdb(file) if 'n' not in flag else None
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000079 if result is None:
briancurtin525c25d2011-03-14 16:03:54 -040080 # db doesn't exist or 'n' flag was specified to create a new db
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000081 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:
Amaury Forgeot d'Arcb5cf3012008-09-25 22:27:43 +000085 raise error[0]("need 'c' or 'n' flag to open new db")
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000086 elif result == "":
87 # db type cannot be determined
Amaury Forgeot d'Arcb5cf3012008-09-25 22:27:43 +000088 raise error[0]("db type could not be determined")
Georg Brandlb17acad2008-05-28 08:43:17 +000089 elif result not in _modules:
Amaury Forgeot d'Arcb5cf3012008-09-25 22:27:43 +000090 raise error[0]("db type is {0}, but the module is not "
91 "available".format(result))
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000092 else:
93 mod = _modules[result]
94 return mod.open(file, flag, mode)
95
96
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000097def 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()
Jesus Ceaf1af7052012-10-05 02:48:46 +0200114 f = io.open(filename + ".dir", "rb")
115 f.close()
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000116 return "dbm.ndbm"
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200117 except OSError:
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000118 # 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"
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200130 except OSError:
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000131 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()
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200147 except OSError:
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000148 pass
149
150 # See if the file exists, return None if not
151 try:
152 f = io.open(filename, "rb")
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200153 except OSError:
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000154 return None
155
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300156 with f:
157 # Read the start of the file -- the magic number
158 s16 = f.read(16)
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000159 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
Jesus Ceabc566b02011-09-19 17:08:18 +0200172 if magic in (0x13579ace, 0x13579acd, 0x13579acf):
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000173 return "dbm.gnu"
174
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000175 # 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
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000182 # Unknown
183 return ""
184
185
186if __name__ == "__main__":
187 for filename in sys.argv[1:]:
188 print(whichdb(filename) or "UNKNOWN", filename)