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

View File

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

View File

@ -64,6 +64,13 @@ class Main:
"-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(
# "-d", "--delete", action="store_true", help="remove all .chkbit files from target"
# )