Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 1 | """Guess which db package to use to open a db file.""" |
| 2 | |
Guido van Rossum | d74fb6b | 2001-03-02 06:43:49 +0000 | [diff] [blame] | 3 | import os |
Skip Montanaro | 404378f | 2002-08-02 17:12:15 +0000 | [diff] [blame] | 4 | import struct |
| 5 | |
| 6 | try: |
| 7 | import dbm |
| 8 | _dbmerror = dbm.error |
| 9 | except ImportError: |
| 10 | dbm = None |
| 11 | # just some sort of valid exception which might be raised in the |
| 12 | # dbm test |
| 13 | _dbmerror = IOError |
Guido van Rossum | d74fb6b | 2001-03-02 06:43:49 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 15 | def whichdb(filename): |
| 16 | """Guess which db package to use to open a db file. |
| 17 | |
| 18 | Return values: |
| 19 | |
| 20 | - None if the database file can't be read; |
| 21 | - empty string if the file can be read but can't be recognized |
| 22 | - the module name (e.g. "dbm" or "gdbm") if recognized. |
| 23 | |
| 24 | Importing the given module may still fail, and opening the |
| 25 | database using that module may still fail. |
| 26 | """ |
| 27 | |
| 28 | # Check for dbm first -- this has a .pag and a .dir file |
| 29 | try: |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 30 | f = open(filename + os.extsep + "pag", "rb") |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 31 | f.close() |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 32 | f = open(filename + os.extsep + "dir", "rb") |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 33 | f.close() |
| 34 | return "dbm" |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 35 | except IOError: |
Skip Montanaro | 404378f | 2002-08-02 17:12:15 +0000 | [diff] [blame] | 36 | # some dbm emulations based on Berkeley DB generate a .db file |
| 37 | # some do not, but they should be caught by the dbhash checks |
| 38 | try: |
| 39 | f = open(filename + os.extsep + "db", "rb") |
| 40 | f.close() |
| 41 | # guarantee we can actually open the file using dbm |
| 42 | # kind of overkill, but since we are dealing with emulations |
| 43 | # it seems like a prudent step |
| 44 | if dbm is not None: |
| 45 | d = dbm.open(filename) |
| 46 | d.close() |
| 47 | return "dbm" |
| 48 | except (IOError, _dbmerror): |
| 49 | pass |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 50 | |
Moshe Zadka | 7a4409c | 2000-07-29 05:31:40 +0000 | [diff] [blame] | 51 | # Check for dumbdbm next -- this has a .dir and and a .dat file |
Thomas Wouters | 3027b15 | 2000-08-04 08:46:59 +0000 | [diff] [blame] | 52 | try: |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 53 | f = open(filename + os.extsep + "dat", "rb") |
Moshe Zadka | 7a4409c | 2000-07-29 05:31:40 +0000 | [diff] [blame] | 54 | f.close() |
Guido van Rossum | e2ae77b | 2001-10-24 20:42:55 +0000 | [diff] [blame] | 55 | f = open(filename + os.extsep + "dir", "rb") |
Moshe Zadka | 7a4409c | 2000-07-29 05:31:40 +0000 | [diff] [blame] | 56 | try: |
| 57 | if f.read(1) in ["'", '"']: |
| 58 | return "dumbdbm" |
| 59 | finally: |
| 60 | f.close() |
| 61 | except IOError: |
| 62 | pass |
| 63 | |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 64 | # See if the file exists, return None if not |
| 65 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 66 | f = open(filename, "rb") |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 67 | except IOError: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 68 | return None |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 69 | |
Guido van Rossum | cf09a39 | 1999-06-08 13:13:16 +0000 | [diff] [blame] | 70 | # Read the start of the file -- the magic number |
| 71 | s16 = f.read(16) |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 72 | f.close() |
Guido van Rossum | cf09a39 | 1999-06-08 13:13:16 +0000 | [diff] [blame] | 73 | s = s16[0:4] |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 74 | |
| 75 | # Return "" if not at least 4 bytes |
| 76 | if len(s) != 4: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 77 | return "" |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 78 | |
| 79 | # Convert to 4-byte int in native byte order -- return "" if impossible |
| 80 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 81 | (magic,) = struct.unpack("=l", s) |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 82 | except struct.error: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 83 | return "" |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 84 | |
| 85 | # Check for GNU dbm |
| 86 | if magic == 0x13579ace: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 87 | return "gdbm" |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 88 | |
| 89 | # Check for BSD hash |
Guido van Rossum | b86ba12 | 1998-04-28 15:41:03 +0000 | [diff] [blame] | 90 | if magic in (0x00061561, 0x61150600): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 91 | return "dbhash" |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 92 | |
Guido van Rossum | cf09a39 | 1999-06-08 13:13:16 +0000 | [diff] [blame] | 93 | # BSD hash v2 has a 12-byte NULL pad in front of the file type |
| 94 | try: |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 95 | (magic,) = struct.unpack("=l", s16[-4:]) |
Guido van Rossum | cf09a39 | 1999-06-08 13:13:16 +0000 | [diff] [blame] | 96 | except struct.error: |
| 97 | return "" |
| 98 | |
| 99 | # Check for BSD hash |
| 100 | if magic in (0x00061561, 0x61150600): |
| 101 | return "dbhash" |
| 102 | |
Guido van Rossum | 8d12a1b | 1996-07-30 16:30:15 +0000 | [diff] [blame] | 103 | # Unknown |
| 104 | return "" |