Skip to content

Commit

Permalink
keep api at v3, see #10
Browse files Browse the repository at this point in the history
  • Loading branch information
laktak committed Dec 22, 2023
1 parent 360167c commit aab7a2b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
29 changes: 16 additions & 13 deletions chkbit/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -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):
Expand Down Expand Up @@ -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,
Expand All @@ -123,21 +123,24 @@ 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

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:
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions chkbit/index_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"},
Expand Down

0 comments on commit aab7a2b

Please sign in to comment.