From 868e7ec78a89558af55ac0aa4125ab5ec631f473 Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch Date: Thu, 21 Dec 2023 03:44:35 +0100 Subject: WIP: FS to DB sync --- main.go | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index 59ca682..8a3de6d 100644 --- a/main.go +++ b/main.go @@ -938,8 +938,6 @@ func syncDispose(c *syncContext, nodeID int64, keepNode bool) error { } func syncPostProcess(c *syncContext, info syncFileInfo) error { - // TODO: Actually dequeue, or something. - // TODO: // - When replacing a file, which needn't become orphaned, // we could offer copying all tags, but this needs another table @@ -965,7 +963,14 @@ func syncPostProcess(c *syncContext, info syncFileInfo) error { case info.dbID == 0: // 0 → F - // TODO: Add an image entry. Create a node. + // TODO: Add an image entry. + + if _, err := c.tx.Exec(`INSERT INTO node(parent, name, mtime, sha1) + VALUES (?, ?, ?, ?)`, + info.dbParent, info.dbName, info.fsMtime, info.sha1); err != nil { + return err + } + return nil default: // D → F, F → F (this statement is a no-op with the latter) @@ -977,9 +982,13 @@ func syncPostProcess(c *syncContext, info syncFileInfo) error { // But that will be a rare situation. // TODO: Add an image entry. - // TODO: Update the node. + + if _, err := c.tx.Exec(`UPDATE node SET mtime = ?, sha1 = ? + WHERE id = ?`, info.fsMtime, info.sha1, info.dbID); err != nil { + return err + } + return nil } - return nil } func syncDirectoryPair(c *syncContext, dbParent int64, fsPath string, @@ -1000,8 +1009,14 @@ func syncDirectoryPair(c *syncContext, dbParent int64, fsPath string, case db == nil && fs.fsIsDir: // 0 → D - // TODO: Create a directory node. - return syncDirectory(c, 42, filepath.Join(fsPath, fs.fsName)) + var id int64 + if result, err := c.tx.Exec(`INSERT INTO node(parent, name) + VALUES (?, ?)`, dbParent, fs.fsName); err != nil { + return err + } else if id, err = result.LastInsertId(); err != nil { + return err + } + return syncDirectory(c, id, filepath.Join(fsPath, fs.fsName)) case db == nil: // 0 → F (or 0 → 0) @@ -1024,7 +1039,10 @@ func syncDirectoryPair(c *syncContext, dbParent int64, fsPath string, if err := syncDispose(c, db.dbID, true /*keepNode*/); err != nil { return err } - // TODO: Change it in DB to a directory. + if _, err := c.tx.Exec(`UPDATE node + SET mtime = NULL, sha1 = NULL WHERE id = ?`, db.dbID); err != nil { + return err + } return syncDirectory(c, db.dbID, filepath.Join(fsPath, fs.fsName)) case db.dbMtime != fs.fsMtime: -- cgit v1.2.3-70-g09d2