From 7a0fea9a807457ed6b6a9329213a8a34f56b72f2 Mon Sep 17 00:00:00 2001 From: Jacob Hinkle Date: Tue, 11 Oct 2022 12:49:11 -0400 Subject: [PATCH] Fix missing .hex() when inserting SHA256 --- pyproject.toml | 2 +- src/nancy/fs.py | 28 +++++++++++----------------- src/nancy/store.py | 25 ++++++++++++------------- 3 files changed, 24 insertions(+), 31 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index b562401..c573b1c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,7 +37,7 @@ deps = pytest-cov coverage commands = - pytest --cov {envsitepackagesdir}/nancy + pytest --cov {envsitepackagesdir}/nancy {posargs} [testenv:mypy] deps = diff --git a/src/nancy/fs.py b/src/nancy/fs.py index 83f8a38..91fbe60 100644 --- a/src/nancy/fs.py +++ b/src/nancy/fs.py @@ -89,9 +89,9 @@ class FSEntryVersion: "INSERT INTO filedir_version VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", ( self.uuid, - self.filedir.sha256, + self.filedir.sha256.hex(), datetime.now().timestamp(), - self.filetype, + str(self.filetype), False, self.perms, self.symlink_target, @@ -286,22 +286,16 @@ class FSEntry: ) -> None: # Find entries with this name and parent cur.execute( - "SELECT sha256 FROM filedir WHERE store = 1 AND name = ? AND parent = ? LIMIT 1", - (self.filename, None if self.parent is None else self.parent.sha256), + "INSERT OR IGNORE INTO filedir VALUES (?, ?, ?, ?)", + ( + self.sha256.hex(), + None if self.store is None else self.store.uuid, + self.filename, + parent_key, + ), ) - res = cur.fetchall() - if len(res) == 0: - # create filedir entry and get its id - cur.execute( - "INSERT INTO filedir VALUES (?, ?, ?, ?)", - ( - self.sha256, - None if self.store is None else self.store.uuid, - self.filename, - parent_key, - ), - ) - self.versions[-1].persist(cur=cur, source_task=source_task) + for v in self.versions: + v.persist(cur=cur, source_task=source_task) # descend into children and record all of them anew as well for c in self.children: diff --git a/src/nancy/store.py b/src/nancy/store.py index 715f993..f3deda1 100644 --- a/src/nancy/store.py +++ b/src/nancy/store.py @@ -30,6 +30,7 @@ class Store: self, directory: fs.PathStr, conn: Optional[sqlite3.Connection] = None, + uuid: Optional[str] = None, ): self.path = Path(directory) self.db_path = self.path / "nancy.db" @@ -39,7 +40,7 @@ class Store: else: self.conn = conn - self.uuid = self.find_store_uuid() + self.uuid = self.find_store_uuid() if uuid is None else uuid def copy(self: _StoreT, store_path: fs.PathStr) -> _StoreT: """Copy this store to a new store path""" @@ -94,21 +95,19 @@ class Store: conn.commit() cur.close() - new_store = cls(directory=directory, conn=conn) + new_uuid = str(uuid.uuid4()) + new_store = cls(directory=directory, conn=conn, uuid=new_uuid) + # generate a new UUID for this store + with new_store.committing() as cur: + cur.execute( + 'INSERT INTO local_metadata VALUES ("store_uuid", ?)', + (new_store.uuid,), + ) with new_store.new_program("INIT", message) as p: # set the timing to the actual times it took to initialize the db p.start_time = start_time - # generate a new UUID for this store - assert new_store.uuid is None - new_store.uuid = str(uuid.uuid4()) - with new_store.committing() as cur: - cur.execute( - 'INSERT INTO local_metadata VALUES ("store_uuid", ?)', - (new_store.uuid,), - ) - return new_store def filedir_root_key(self, cur: Optional[sqlite3.Cursor] = None) -> Optional[str]: @@ -116,7 +115,7 @@ class Store: if cur is None: assert self.conn is not None cur = self.conn.cursor() - cur.execute("SELECT uuid FROM filedir WHERE store=1 AND parent is NULL") + cur.execute("SELECT sha256 FROM filedir WHERE store=1 AND parent is NULL") row = cur.fetchone() if row is None: return None @@ -147,7 +146,7 @@ class Store: for p in Path(rel).parts: # Path.parts splits a path reliably # get child with that name cur.execute( - "SELECT uuid, filetype FROM filedir WHERE filename=? AND parent=? LIMIT 1", + "SELECT sha256, filetype FROM filedir WHERE filename=? AND parent=? LIMIT 1", (p, fd_key), ) row = cur.fetchone()