integrity checking

This commit is contained in:
ville rantanen
2016-09-09 12:13:47 +03:00
parent 640a2e1795
commit 90c4d73af8

View File

@@ -22,6 +22,8 @@ def setup_options():
help="Do not add new files [%(default)s]")
parser.add_argument("-c",action="store_true",dest="changed",default=False,
help="Modify changed files [%(default)s]")
parser.add_argument("--check",action="store_true",dest="check",default=False,
help="Check md5sums of files. Note that --full affects the comparison. Limit check with -s.")
parser.add_argument("-d",action="store_true",dest="delete",default=False,
help="Delete non-existing entries [%(default)s]")
parser.add_argument("--du",type=str,action='store',dest="diskused",default=False,
@@ -168,6 +170,8 @@ def add_recurse(options):
filename=os.path.realpath(os.path.join(path,file))
if file==options.sqlfile:
continue
if not os.path.isfile(filename):
continue
#if not is_listed(db,filename):
if file not in db_files:
if options.add:
@@ -304,6 +308,25 @@ def searchdb(sqlfile,needle):
for row in db:
print(row[0])
def checkdb(sqlfile,fullFile,needle):
if len(needle)==0:
needle.append('%')
needle=['%'+i+'%' for i in needle]
like_query=' OR '.join(['file LIKE ?' for i in needle])
conn=sqlite3.connect(sqlfile)
conn.text_factory=str
db=conn.cursor()
db.execute("SELECT file,hash FROM list WHERE "+like_query+" ORDER BY file",needle)
for row in db:
status='OK'
if os.path.exists(row[0]):
md5f=get_md5(row[0],fullFile)
if row[1]!=md5f:
status='Checksum-difference'
else:
status='Not-found'
print("%s %s"%(row[0],status))
def matchdb(sqlfile,needle,helper):
needle=needle.lower()
import difflib as dl
@@ -343,6 +366,9 @@ def main():
if options.haschanges or options.hasadditions or options.hasdeletions:
has_changes(options)
sys.exit(0)
if options.check:
checkdb(options.sqlfile,options.fullfile,options.search)
sys.exit(0)
if len(options.search)>0 and not options.match:
searchdb(options.sqlfile,options.search)
sys.exit(0)