aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2023-12-21 21:07:41 +0100
committerPřemysl Eric Janouch <p@janouch.name>2023-12-21 21:07:41 +0100
commitb1bc65c9a17dad6336826e7b87b5877ca6d0bc6f (patch)
tree240029bd2aa27d6ab168b6e276b6a7a51030ab1f
parent19d6ff47f7026579e899520079b2e13a031a3ee3 (diff)
downloadgallery-b1bc65c9a17dad6336826e7b87b5877ca6d0bc6f.tar.gz
gallery-b1bc65c9a17dad6336826e7b87b5877ca6d0bc6f.tar.xz
gallery-b1bc65c9a17dad6336826e7b87b5877ca6d0bc6f.zip
WIP: FS to DB sync
-rw-r--r--main.go35
1 files changed, 23 insertions, 12 deletions
diff --git a/main.go b/main.go
index 96d527f..54cea82 100644
--- a/main.go
+++ b/main.go
@@ -1183,7 +1183,7 @@ func syncDirectory(c *syncContext, dbParent int64, fsPath string) error {
return nil
}
-func syncArgument(ctx context.Context, tx *sql.Tx, path string) error {
+func syncRoot(ctx context.Context, tx *sql.Tx, fsPath string) error {
c := syncContext{ctx: ctx, tx: tx}
c.pb = newProgressBar(-1)
@@ -1210,12 +1210,6 @@ func syncArgument(ctx context.Context, tx *sql.Tx, path string) error {
// i.e., it is for the result of the task that syncEnqueue() spawns.
c.info = make(chan syncFileInfo, cap(taskSemaphore)+1)
- // At least for now, turn all roots into absolute paths.
- fsPath, err := filepath.Abs(filepath.Clean(path))
- if err != nil {
- return err
- }
-
// Figure out a database root (not trying to convert F → D on conflict,
// also because we don't know yet if the argument is a directory).
//
@@ -1282,11 +1276,28 @@ func cmdSync(args []string) error {
ctxSignal, stop := signal.NotifyContext(ctx, os.Interrupt)
defer stop()
- // TODO: Check if one is not a prefix of another,
- // and either filter out these duplicates (easy to do, may warn),
- // or plainly refuse to work.
- for _, path := range args[1:] {
- if err := syncArgument(ctxSignal, tx, path); err != nil {
+ // Normalize arguments.
+ // At least for now, turn all roots into absolute paths.
+ roots := args[1:]
+ for i := range roots {
+ roots[i], err = filepath.Abs(filepath.Clean(roots[i]))
+ if err != nil {
+ return err
+ }
+ }
+
+ // Filter out duplicates.
+ sort.Strings(roots)
+ roots = slices.CompactFunc(roots, func(a, b string) bool {
+ if a != b && !strings.HasPrefix(b, a+"/") {
+ return false
+ }
+ log.Printf("asking to sync path twice: %s\n", b)
+ return true
+ })
+
+ for _, path := range roots {
+ if err := syncRoot(ctxSignal, tx, path); err != nil {
return err
}
}