From 19d6ff47f7026579e899520079b2e13a031a3ee3 Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Thu, 21 Dec 2023 20:34:55 +0100 Subject: WIP: FS to DB sync --- main.go | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 7f15b5c..96d527f 100644 --- a/main.go +++ b/main.go @@ -41,6 +41,12 @@ var ( taskSemaphore semaphore ) +const ( + nameOfDB = "gallery.db" + nameOfImageRoot = "images" + nameOfThumbRoot = "thumbs" +) + func hammingDistance(a, b int64) int { return bits.OnesCount64(uint64(a) ^ uint64(b)) } @@ -56,17 +62,19 @@ func init() { func openDB(directory string) error { var err error db, err = sql.Open("sqlite3_custom", "file:"+filepath.Join(directory, - "gallery.db?_foreign_keys=1&_busy_timeout=1000")) + nameOfDB+"?_foreign_keys=1&_busy_timeout=1000")) galleryDirectory = directory return err } func imagePath(sha1 string) string { - return filepath.Join(galleryDirectory, "images", sha1[:2], sha1) + return filepath.Join(galleryDirectory, + nameOfImageRoot, sha1[:2], sha1) } func thumbPath(sha1 string) string { - return filepath.Join(galleryDirectory, "thumbs", sha1[:2], sha1+".webp") + return filepath.Join(galleryDirectory, + nameOfThumbRoot, sha1[:2], sha1+".webp") } func dbCollectStrings(query string) ([]string, error) { @@ -124,7 +132,7 @@ type progressBar struct { func newProgressBar(target int) *progressBar { pb := &progressBar{current: 0, target: target} - pb.update() + pb.Update() return pb } @@ -133,7 +141,7 @@ func (pb *progressBar) Stop() { os.Stdout.WriteString("\n") } -func (pb *progressBar) update() { +func (pb *progressBar) Update() { if pb.target < 0 { fmt.Printf("\r%d/?", pb.current) return @@ -153,7 +161,7 @@ func (pb *progressBar) Step() { defer pb.mutex.Unlock() pb.current++ - pb.update() + pb.Update() } // --- Initialization ---------------------------------------------------------- @@ -175,11 +183,11 @@ func cmdInit(args []string) error { // XXX: There's technically no reason to keep images as symlinks, // we might just keep absolute paths in the database as well. if err := os.MkdirAll( - filepath.Join(galleryDirectory, "images"), 0755); err != nil { + filepath.Join(galleryDirectory, nameOfImageRoot), 0755); err != nil { return err } if err := os.MkdirAll( - filepath.Join(galleryDirectory, "thumbs"), 0755); err != nil { + filepath.Join(galleryDirectory, nameOfThumbRoot), 0755); err != nil { return err } return nil @@ -814,6 +822,12 @@ type syncContext struct { pb *progressBar } +func syncPrintf(c *syncContext, format string, v ...any) { + c.pb.Stop() + log.Printf(format+"\n", v...) + c.pb.Update() +} + // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - type syncNode struct { @@ -1128,6 +1142,16 @@ func syncDirectory(c *syncContext, dbParent int64, fsPath string) error { return err } + // This would not be fatal, but it has annoying consequences. + if _, ok := slices.BinarySearchFunc(fs, syncFile{fsName: nameOfDB}, + func(a, b syncFile) int { + return strings.Compare(a.fsName, b.fsName) + }); ok { + syncPrintf(c, "%s may be a gallery directory, treating as empty", + fsPath) + fs = nil + } + // Convert differences to a more convenient form for processing. iDB, iFS, pairs := 0, 0, []syncPair{} for iDB < len(db) && iFS < len(fs) { @@ -1465,13 +1489,13 @@ func cmdCheck(args []string) error { // This somewhat duplicates {image,thumb}Path(). log.Println("checking SQL against filesystem") okImages, intersection, err := checkFiles( - filepath.Join(galleryDirectory, "images"), "", allSHA1) + filepath.Join(galleryDirectory, nameOfImageRoot), "", allSHA1) if err != nil { return err } okThumbs, _, err := checkFiles( - filepath.Join(galleryDirectory, "thumbs"), ".webp", thumbSHA1) + filepath.Join(galleryDirectory, nameOfThumbRoot), ".webp", thumbSHA1) if err != nil { return err } -- cgit v1.2.3-70-g09d2