| 1 | n/a | # !/usr/bin/env python |
|---|
| 2 | 1 | """Guess which db package to use to open a db file.""" |
|---|
| 3 | n/a | |
|---|
| 4 | 1 | import os |
|---|
| 5 | 1 | import struct |
|---|
| 6 | 1 | import sys |
|---|
| 7 | n/a | |
|---|
| 8 | 1 | try: |
|---|
| 9 | 1 | import dbm |
|---|
| 10 | 1 | _dbmerror = dbm.error |
|---|
| 11 | 0 | except ImportError: |
|---|
| 12 | 0 | dbm = None |
|---|
| 13 | n/a | # just some sort of valid exception which might be raised in the |
|---|
| 14 | n/a | # dbm test |
|---|
| 15 | 0 | _dbmerror = IOError |
|---|
| 16 | n/a | |
|---|
| 17 | 1 | def whichdb(filename): |
|---|
| 18 | n/a | """Guess which db package to use to open a db file. |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | Return values: |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | - None if the database file can't be read; |
|---|
| 23 | n/a | - empty string if the file can be read but can't be recognized |
|---|
| 24 | n/a | - the module name (e.g. "dbm" or "gdbm") if recognized. |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | Importing the given module may still fail, and opening the |
|---|
| 27 | n/a | database using that module may still fail. |
|---|
| 28 | n/a | """ |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | # Check for dbm first -- this has a .pag and a .dir file |
|---|
| 31 | 96 | try: |
|---|
| 32 | 96 | f = open(filename + os.extsep + "pag", "rb") |
|---|
| 33 | 2 | f.close() |
|---|
| 34 | n/a | # dbm linked with gdbm on OS/2 doesn't have .dir file |
|---|
| 35 | 2 | if not (dbm.library == "GNU gdbm" and sys.platform == "os2emx"): |
|---|
| 36 | 2 | f = open(filename + os.extsep + "dir", "rb") |
|---|
| 37 | 2 | f.close() |
|---|
| 38 | 2 | return "dbm" |
|---|
| 39 | 94 | except IOError: |
|---|
| 40 | n/a | # some dbm emulations based on Berkeley DB generate a .db file |
|---|
| 41 | n/a | # some do not, but they should be caught by the dbhash checks |
|---|
| 42 | 94 | try: |
|---|
| 43 | 94 | f = open(filename + os.extsep + "db", "rb") |
|---|
| 44 | 0 | f.close() |
|---|
| 45 | n/a | # guarantee we can actually open the file using dbm |
|---|
| 46 | n/a | # kind of overkill, but since we are dealing with emulations |
|---|
| 47 | n/a | # it seems like a prudent step |
|---|
| 48 | 0 | if dbm is not None: |
|---|
| 49 | 0 | d = dbm.open(filename) |
|---|
| 50 | 0 | d.close() |
|---|
| 51 | 0 | return "dbm" |
|---|
| 52 | 94 | except (IOError, _dbmerror): |
|---|
| 53 | 94 | pass |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | # Check for dumbdbm next -- this has a .dir and a .dat file |
|---|
| 56 | 94 | try: |
|---|
| 57 | n/a | # First check for presence of files |
|---|
| 58 | 94 | os.stat(filename + os.extsep + "dat") |
|---|
| 59 | 2 | size = os.stat(filename + os.extsep + "dir").st_size |
|---|
| 60 | n/a | # dumbdbm files with no keys are empty |
|---|
| 61 | 2 | if size == 0: |
|---|
| 62 | 1 | return "dumbdbm" |
|---|
| 63 | 1 | f = open(filename + os.extsep + "dir", "rb") |
|---|
| 64 | 1 | try: |
|---|
| 65 | 1 | if f.read(1) in ("'", '"'): |
|---|
| 66 | 1 | return "dumbdbm" |
|---|
| 67 | n/a | finally: |
|---|
| 68 | 1 | f.close() |
|---|
| 69 | 92 | except (OSError, IOError): |
|---|
| 70 | 92 | pass |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | # See if the file exists, return None if not |
|---|
| 73 | 92 | try: |
|---|
| 74 | 92 | f = open(filename, "rb") |
|---|
| 75 | 85 | except IOError: |
|---|
| 76 | 85 | return None |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | # Read the start of the file -- the magic number |
|---|
| 79 | 7 | s16 = f.read(16) |
|---|
| 80 | 7 | f.close() |
|---|
| 81 | 7 | s = s16[0:4] |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | # Return "" if not at least 4 bytes |
|---|
| 84 | 7 | if len(s) != 4: |
|---|
| 85 | 0 | return "" |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | # Convert to 4-byte int in native byte order -- return "" if impossible |
|---|
| 88 | 7 | try: |
|---|
| 89 | 7 | (magic,) = struct.unpack("=l", s) |
|---|
| 90 | 0 | except struct.error: |
|---|
| 91 | 0 | return "" |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | # Check for GNU dbm |
|---|
| 94 | 7 | if magic == 0x13579ace: |
|---|
| 95 | 2 | return "gdbm" |
|---|
| 96 | n/a | |
|---|
| 97 | n/a | # Check for old Berkeley db hash file format v2 |
|---|
| 98 | 5 | if magic in (0x00061561, 0x61150600): |
|---|
| 99 | 0 | return "bsddb185" |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | # Later versions of Berkeley db hash file have a 12-byte pad in |
|---|
| 102 | n/a | # front of the file type |
|---|
| 103 | 5 | try: |
|---|
| 104 | 5 | (magic,) = struct.unpack("=l", s16[-4:]) |
|---|
| 105 | 0 | except struct.error: |
|---|
| 106 | 0 | return "" |
|---|
| 107 | n/a | |
|---|
| 108 | n/a | # Check for BSD hash |
|---|
| 109 | 5 | if magic in (0x00061561, 0x61150600): |
|---|
| 110 | 5 | return "dbhash" |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | # Unknown |
|---|
| 113 | 0 | return "" |
|---|
| 114 | n/a | |
|---|
| 115 | 1 | if __name__ == "__main__": |
|---|
| 116 | 0 | for filename in sys.argv[1:]: |
|---|
| 117 | 0 | print whichdb(filename) or "UNKNOWN", filename |
|---|