added checksum checking
This commit is contained in:
@@ -29,6 +29,8 @@ def setup_options():
|
|||||||
help="Do not add new files [%(default)s]")
|
help="Do not add new files [%(default)s]")
|
||||||
db.add_argument("-c",action="store_true",dest="changed",default=False,
|
db.add_argument("-c",action="store_true",dest="changed",default=False,
|
||||||
help="Modify changed files [%(default)s]")
|
help="Modify changed files [%(default)s]")
|
||||||
|
db.add_argument("--check",action="store_true",dest="check",default=False,
|
||||||
|
help="Check md5sums of files. Limit check with -s.")
|
||||||
db.add_argument("-d",action="store_true",dest="delete",default=False,
|
db.add_argument("-d",action="store_true",dest="delete",default=False,
|
||||||
help="Delete non-existing entries [%(default)s]")
|
help="Delete non-existing entries [%(default)s]")
|
||||||
db.add_argument("-D",action="store_true",dest="delete_data",default=False,
|
db.add_argument("-D",action="store_true",dest="delete_data",default=False,
|
||||||
@@ -98,6 +100,52 @@ def setup_options():
|
|||||||
options.add=False
|
options.add=False
|
||||||
return options
|
return options
|
||||||
|
|
||||||
|
def checkdb(options):
|
||||||
|
|
||||||
|
if options.search:
|
||||||
|
needle='%'+options.search+'%'
|
||||||
|
else:
|
||||||
|
needle='%'
|
||||||
|
conn=sqlite3.connect(options.sqlfile)
|
||||||
|
conn.text_factory=str
|
||||||
|
db=conn.cursor()
|
||||||
|
db.execute("SELECT file,hash,size,date FROM list WHERE file LIKE ? ORDER BY file",(needle,))
|
||||||
|
missing=[]
|
||||||
|
differing=[]
|
||||||
|
OK_count=0
|
||||||
|
for row in db:
|
||||||
|
status='OK'
|
||||||
|
sys.stdout.write("\r%s"%(row[0],))
|
||||||
|
if os.path.exists(row[0]):
|
||||||
|
md5f=get_md5(row[0])
|
||||||
|
if row[1]!=md5f:
|
||||||
|
status='Checksum-difference'
|
||||||
|
differing.append(row)
|
||||||
|
else:
|
||||||
|
status='Not-found'
|
||||||
|
missing.append(row)
|
||||||
|
sys.stdout.write("\r%s %s\n"%(row[0],status))
|
||||||
|
if status=='OK':
|
||||||
|
OK_count+=1
|
||||||
|
if len(differing)>0:
|
||||||
|
print_stderr("----\nDiffering files:")
|
||||||
|
pad=str(max([len(x[0]) for x in differing]))
|
||||||
|
for f in differing:
|
||||||
|
print_stderr(("%-"+pad+"s (%s %7s => %s %7s)")%(f[0],humanize_date(f[3]),humanize_size(f[2]),
|
||||||
|
humanize_date(os.path.getmtime(f[0])),
|
||||||
|
humanize_size(os.path.getsize(f[0]))))
|
||||||
|
if len(missing)>0:
|
||||||
|
print_stderr("----\nMissing files:")
|
||||||
|
pad=str(max([len(x[0]) for x in missing]))
|
||||||
|
for f in missing:
|
||||||
|
print_stderr(("%-"+pad+"s (%s %7s)")%(f[0],humanize_date(f[3]),humanize_size(f[2])))
|
||||||
|
|
||||||
|
print_stderr("----\nFile check summary:")
|
||||||
|
print_stderr("Database modified: %s"%(humanize_date(os.path.getmtime(options.sqlfile)),))
|
||||||
|
print_stderr("Checksum matches : %d"%(OK_count,))
|
||||||
|
print_stderr("Checksum mismatch: %d"%(len(differing),))
|
||||||
|
print_stderr("Files missing : %d"%(len(missing),))
|
||||||
|
|
||||||
def createdb(sqlfile):
|
def createdb(sqlfile):
|
||||||
conn=sqlite3.connect(sqlfile)
|
conn=sqlite3.connect(sqlfile)
|
||||||
db=conn.cursor()
|
db=conn.cursor()
|
||||||
@@ -824,6 +872,11 @@ def disk_used(options):
|
|||||||
humanize_size(entry[0]).rjust(8),
|
humanize_size(entry[0]).rjust(8),
|
||||||
entry[1]]))
|
entry[1]]))
|
||||||
|
|
||||||
|
def print_stderr(s):
|
||||||
|
sys.stderr.write(s)
|
||||||
|
sys.stderr.write("\n")
|
||||||
|
sys.stderr.flush()
|
||||||
|
|
||||||
def print_structure(files):
|
def print_structure(files):
|
||||||
for hash in files:
|
for hash in files:
|
||||||
#print(hash[0])
|
#print(hash[0])
|
||||||
@@ -1020,7 +1073,7 @@ def main():
|
|||||||
if options.delete_data:
|
if options.delete_data:
|
||||||
print('Deleting metadata...')
|
print('Deleting metadata...')
|
||||||
delete_data(options.sqlfile)
|
delete_data(options.sqlfile)
|
||||||
if options.search:
|
if options.search and not options.check:
|
||||||
print_structure(searchdb(options.sqlfile,options.search))
|
print_structure(searchdb(options.sqlfile,options.search))
|
||||||
if options.measure:
|
if options.measure:
|
||||||
append_data(options)
|
append_data(options)
|
||||||
@@ -1075,6 +1128,8 @@ def main():
|
|||||||
if options.export_descriptions:
|
if options.export_descriptions:
|
||||||
print("Export descriptions")
|
print("Export descriptions")
|
||||||
export_descriptions(options)
|
export_descriptions(options)
|
||||||
|
if options.check:
|
||||||
|
checkdb(options)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user