From aab7a2bd8fae97ea0bd9a9fae2facef739b89d68 Mon Sep 17 00:00:00 2001 From: Christian Zangl Date: Fri, 22 Dec 2023 21:45:36 +0100 Subject: [PATCH] keep api at v3, see #10 --- chkbit/index.py | 29 ++++++++++++++++------------- chkbit/index_thread.py | 4 ++-- pyproject.toml | 2 +- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/chkbit/index.py b/chkbit/index.py index 1420bbf..4f9bb7e 100644 --- a/chkbit/index.py +++ b/chkbit/index.py @@ -9,7 +9,7 @@ class Index: - def __init__(self, context, path, files): + def __init__(self, context, path, files, *, readonly=False): self.context = context self.path = path self.files = files @@ -18,7 +18,8 @@ def __init__(self, context, path, files): self.ignore = [] self.load_ignore() self.updates = [] - self.modified = True + self.modified = None + self.readonly = readonly @property def ignore_filepath(self): @@ -34,14 +35,14 @@ def should_ignore(self, name): return True return False - def _setmod(self): - self.modified = True + def _setmod(self, value=True): + self.modified = value def _log(self, stat: Status, name: str): self.context.log(stat, os.path.join(self.path, name)) # calc new hashes for this index - def update(self, update): + def update(self): for name in self.files: if self.should_ignore(name): self._log(Status.SKIP, name) @@ -59,10 +60,10 @@ def update(self, update): a = old["a"] self.new[name] = self._calc_file(name, a) else: - if update: - self.new[name] = self._calc_file(name, a) - else: + if self.readonly: self.new[name] = self._list_file(name, a) + else: + self.new[name] = self._calc_file(name, a) # check/update the index (old vs new) def check_fix(self, force): @@ -101,8 +102,7 @@ def check_fix(self, force): self._setmod() def _list_file(self, name, a): - path = os.path.join(self.path, name) - self.context.hit(cfiles=1) + # produce a dummy entry for new files when the index is not updated return { "mod": None, "a": a, @@ -123,13 +123,16 @@ def _calc_file(self, name, a): def save(self): if self.modified: + if self.readonly: + raise Exception("Error trying to save a readonly index.") + data = {"v": VERSION, "idx": self.new} text = json.dumps(self.new, separators=(",", ":")) data["idx_hash"] = hashtext(text) with open(self.index_filepath, "w", encoding="utf-8") as f: json.dump(data, f, separators=(",", ":")) - self.modified = False + self._setmod(False) return True else: return False @@ -137,7 +140,7 @@ def save(self): def load(self): if not os.path.exists(self.index_filepath): return False - self.modified = False + self._setmod(False) with open(self.index_filepath, "r", encoding="utf-8") as f: data = json.load(f) if "data" in data: @@ -152,7 +155,7 @@ def load(self): self.old = data["idx"] text = json.dumps(self.old, separators=(",", ":")) if data.get("idx_hash") != hashtext(text): - self.modified = True + self._setmod() self._log(Status.ERR_IDX, self.index_filepath) return True diff --git a/chkbit/index_thread.py b/chkbit/index_thread.py index b153949..f22ce95 100644 --- a/chkbit/index_thread.py +++ b/chkbit/index_thread.py @@ -33,11 +33,11 @@ def _process_root(self, parent): files.append(name) # load index - index = Index(self.context, parent, files) + index = Index(self.context, parent, files, readonly=not self.update) index.load() # calc the new hashes - index.update(self.update) + index.update() # compare index.check_fix(self.context.force) diff --git a/pyproject.toml b/pyproject.toml index 867b185..6775ef3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "chkbit" -version = "3.0.1" +version = "3.0.2" description = "chkbit checks the data integrity of your files" authors = [ {name = "Christian Zangl", email = "laktak@cdak.net"},