From 1054fd76982bcb5ea5facead7fda8cf6ba6c6544 Mon Sep 17 00:00:00 2001 From: q Date: Mon, 30 Jan 2017 20:22:00 +0200 Subject: [PATCH] added checksum checking --- files/image_list.py | 57 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/files/image_list.py b/files/image_list.py index 2cb8a70..00cb415 100755 --- a/files/image_list.py +++ b/files/image_list.py @@ -29,6 +29,8 @@ def setup_options(): help="Do not add new files [%(default)s]") db.add_argument("-c",action="store_true",dest="changed",default=False, 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, help="Delete non-existing entries [%(default)s]") db.add_argument("-D",action="store_true",dest="delete_data",default=False, @@ -98,6 +100,52 @@ def setup_options(): options.add=False 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): conn=sqlite3.connect(sqlfile) db=conn.cursor() @@ -824,6 +872,11 @@ def disk_used(options): humanize_size(entry[0]).rjust(8), entry[1]])) +def print_stderr(s): + sys.stderr.write(s) + sys.stderr.write("\n") + sys.stderr.flush() + def print_structure(files): for hash in files: #print(hash[0]) @@ -1020,7 +1073,7 @@ def main(): if options.delete_data: print('Deleting metadata...') delete_data(options.sqlfile) - if options.search: + if options.search and not options.check: print_structure(searchdb(options.sqlfile,options.search)) if options.measure: append_data(options) @@ -1075,6 +1128,8 @@ def main(): if options.export_descriptions: print("Export descriptions") export_descriptions(options) + if options.check: + checkdb(options) sys.exit(0) if __name__ == "__main__":