add --verify-index flag to only verify files already in the index

This commit is contained in:
Christian Zangl 2020-09-14 16:43:16 +02:00
parent 6b5618d1df
commit 82f848f659
3 changed files with 24 additions and 12 deletions

View File

@ -56,6 +56,7 @@ class Index:
if self.log: if self.log:
self.log(stat, os.path.join(self.path, name)) self.log(stat, os.path.join(self.path, name))
# calc new hashes for this index
def update(self): def update(self):
for name in self.files: for name in self.files:
if self.should_ignore(name): if self.should_ignore(name):
@ -63,6 +64,7 @@ class Index:
continue continue
self.new[name] = self._calc_file(name) self.new[name] = self._calc_file(name)
# check/update the index (old vs new)
def check_fix(self, force): def check_fix(self, force):
for name in self.new.keys(): for name in self.new.keys():
if not name in self.old: if not name in self.old:
@ -119,7 +121,7 @@ class Index:
def load(self): def load(self):
if not os.path.exists(self.idx_file): if not os.path.exists(self.idx_file):
return return False
self.modified = False self.modified = False
with open(self.idx_file, "r", encoding="utf-8") as f: with open(self.idx_file, "r", encoding="utf-8") as f:
data = json.load(f) data = json.load(f)
@ -133,6 +135,7 @@ class Index:
if data.get("idx_hash") != hashtext(text): if data.get("idx_hash") != hashtext(text):
self.modified = True self.modified = True
self._log(Stat.ERR_IDX, self.idx_file) self._log(Stat.ERR_IDX, self.idx_file)
return True
def load_ignore(self): def load_ignore(self):
if not os.path.exists(self.ignore_file): if not os.path.exists(self.ignore_file):

View File

@ -8,7 +8,8 @@ from chkbit import Index, Stat
class IndexThread: class IndexThread:
def __init__(self, idx, args, res_queue, todo_queue): def __init__(self, idx, args, res_queue, todo_queue):
self.idx = idx self.idx = idx
self.update = args.update self.verify_index_only = args.verify_index
self.update = args.update and not self.verify_index_only
self.force = args.force self.force = args.force
self.todo_queue = todo_queue self.todo_queue = todo_queue
self.res_queue = res_queue self.res_queue = res_queue
@ -17,7 +18,8 @@ class IndexThread:
self.t.start() self.t.start()
def _log(self, stat, path): def _log(self, stat, path):
self.res_queue.put((self.idx, stat, path)) if not self.verify_index_only or stat != Stat.NEW:
self.res_queue.put((self.idx, stat, path))
def _process_root(self, parent): def _process_root(self, parent):
files = [] files = []
@ -35,18 +37,18 @@ class IndexThread:
# load index # load index
e = Index(parent, files, log=self._log) e = Index(parent, files, log=self._log)
e.load() if e.load() or not self.verify_index_only:
# update the index from current state # calc the new hashes
e.update() e.update()
# compare # compare
e.check_fix(self.force) e.check_fix(self.force)
# save if update is set # save if update is set
if self.update: if self.update:
if e.save(): if e.save():
self._log(Stat.FLAG_MOD, "") self._log(Stat.FLAG_MOD, "")
# process subdirs # process subdirs
for name in dirs: for name in dirs:

View File

@ -64,6 +64,13 @@ class Main:
"-f", "--force", action="store_true", help="force update of damaged items" "-f", "--force", action="store_true", help="force update of damaged items"
) )
parser.add_argument(
"-i",
"--verify-index",
action="store_true",
help="verify files in the index only (will not report new files)",
)
# parser.add_argument( # parser.add_argument(
# "-d", "--delete", action="store_true", help="remove all .chkbit files from target" # "-d", "--delete", action="store_true", help="remove all .chkbit files from target"
# ) # )