aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2023-12-21 03:44:35 +0100
committerPřemysl Eric Janouch <p@janouch.name>2023-12-21 03:44:35 +0100
commit868e7ec78a89558af55ac0aa4125ab5ec631f473 (patch)
tree5b1d9e084c3c55f9b2db866d16e9c6749c614f86 /main.go
parent3910e351123dec0f1caa3625cbc6cb63c84ee369 (diff)
downloadgallery-868e7ec78a89558af55ac0aa4125ab5ec631f473.tar.gz
gallery-868e7ec78a89558af55ac0aa4125ab5ec631f473.tar.xz
gallery-868e7ec78a89558af55ac0aa4125ab5ec631f473.zip
WIP: FS to DB sync
Diffstat (limited to 'main.go')
-rw-r--r--main.go34
1 files 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: