whichdb module fails to recognize Berkeley DB v2 hash files

Skip Montanaro skip at mojam.com
Tue Jun 8 08:59:55 EDT 1999


I guess in 1.5.2 a new module, whichdb, was added that attempts to
divine the nature of a database file.  This module doesn't know anything
about Berkeley DB v2 files.  In v2, Sleepycat added a 12-byte null pad
in front of the old magic numbers (at least for hash and btree files). 
I've been using v2 for awhile and upgrading to 1.5.2 broke all my
anydbm.open calls. I believe the following patch corrects the problem.

*** whichdb.py.orig	Wed Jun  2 13:27:54 1999
--- whichdb.py	Tue Jun  8 08:59:59 1999
***************
*** 32,38 ****
          return None
  
!     # Read the first 4 bytes of the file -- the magic number
!     s = f.read(4)
      f.close()
  
      # Return "" if not at least 4 bytes
--- 32,39 ----
          return None
  
!     # Read the start of the file -- the magic number
!     s16 = f.read(16)
      f.close()
+     s = s16[0:4]
  
      # Return "" if not at least 4 bytes
***************
*** 49,52 ****
--- 50,63 ----
      if magic == 0x13579ace:
          return "gdbm"
+ 
+     # Check for BSD hash
+     if magic in (0x00061561, 0x61150600):
+         return "dbhash"
+ 
+     # BSD hash v2 has a 12-byte NULL pad in front of the file type
+     try:
+ 	(magic,) = struct.unpack("=l", s16[-4:])
+     except struct.error:
+         return ""
  
      # Check for BSD hash

-- 
Skip Montanaro	| Mojam: "Uniting the World of Music"
http://www.mojam.com/
skip at mojam.com  | Musi-Cal: http://www.musi-cal.com/
518-372-5583




More information about the Python-list mailing list