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