aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2023-12-21 20:34:55 +0100
committerPřemysl Eric Janouch <p@janouch.name>2023-12-21 20:34:55 +0100
commit19d6ff47f7026579e899520079b2e13a031a3ee3 (patch)
treef89e65bb131d88252e148b7e77d7e8e5556b8ae3
parent068f6f82cfee723e6c6213ea58d05c9e9708e131 (diff)
downloadgallery-19d6ff47f7026579e899520079b2e13a031a3ee3.tar.gz
gallery-19d6ff47f7026579e899520079b2e13a031a3ee3.tar.xz
gallery-19d6ff47f7026579e899520079b2e13a031a3ee3.zip
WIP: FS to DB sync
-rw-r--r--main.go44
1 files changed, 34 insertions, 10 deletions
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
}