From b1bc65c9a17dad6336826e7b87b5877ca6d0bc6f Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch Date: Thu, 21 Dec 2023 21:07:41 +0100 Subject: WIP: FS to DB sync --- main.go | 35 +++++++++++++++++++++++------------ 1 file 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 } } -- cgit v1.2.3-70-g09d2