diff --git a/internal/syncer/push_delete_test.go b/internal/syncer/push_delete_test.go new file mode 100644 index 0000000..9da08c1 --- /dev/null +++ b/internal/syncer/push_delete_test.go @@ -0,0 +1,63 @@ +package syncer + +import ( + "net/http/httptest" + "os" + "path/filepath" + "testing" + + "github.com/shoemoney/devbox/internal/hub" + "github.com/shoemoney/devbox/internal/hub/blobstore" + "github.com/shoemoney/devbox/internal/hub/meta" + "github.com/shoemoney/devbox/internal/secret" +) + +// Companion to the locally-excluded-file preserve case: a path that's genuinely +// gone from disk (no .devignore rule hides it — the user just deleted it) must +// still delete on push. The carry-forward fix for excluded paths must not make +// real deletes un-deletable. +func TestPushRealDeletePropagates(t *testing.T) { + db, _ := meta.Open(":memory:") + defer db.Close() + store, _ := blobstore.NewDisk(t.TempDir()) + srv := httptest.NewServer(hub.NewServer(db, store).Handler()) + defer srv.Close() + guard, _ := secret.New(nil) + igEmpty, _ := LoadIgnore(t.TempDir()) + + A := joinDevice(t, db, srv.URL, "alice") + B := joinDevice(t, db, srv.URL, "bob") + if err := A.Publish("s"); err != nil { + t.Fatal(err) + } + + rootA := t.TempDir() + writeFile(t, rootA, "data.bin", "precious\n") + writeFile(t, rootA, "app.txt", "x\n") + snapA, _, err := Sync(A, rootA, "s", "", "", "alice", 1, igEmpty, guard, nil) + if err != nil { + t.Fatalf("A sync: %v", err) + } + + // B mounts the whole share (no exclude), then genuinely deletes data.bin. + rootB := t.TempDir() + baseB, _, err := Sync(B, rootB, "s", "", "", "bob", 2, igEmpty, guard, nil) + if err != nil { + t.Fatalf("B initial sync: %v", err) + } + if err := os.Remove(filepath.Join(rootB, "data.bin")); err != nil { + t.Fatalf("B delete: %v", err) + } + if _, _, err := Sync(B, rootB, "s", "", baseB, "bob", 3, igEmpty, guard, nil); err != nil { + t.Fatalf("B sync after delete: %v", err) + } + + // A pulls: the real delete must land, unlike the excluded-file case. + pr, err := Pull(A, rootA, "s", "", snapA, "alice", 4, igEmpty, guard, nil) + if err != nil { + t.Fatalf("A pull: %v", err) + } + if _, err := os.Stat(filepath.Join(rootA, "data.bin")); !os.IsNotExist(err) { + t.Fatalf("B's genuine delete did not propagate to A: deleted=%v", pr.Deleted) + } +} diff --git a/internal/syncer/repro_push_ignored_test.go b/internal/syncer/repro_push_ignored_test.go new file mode 100644 index 0000000..56e8e40 --- /dev/null +++ b/internal/syncer/repro_push_ignored_test.go @@ -0,0 +1,60 @@ +package syncer + +import ( + "net/http/httptest" + "os" + "path/filepath" + "testing" + + "github.com/shoemoney/devbox/internal/hub" + "github.com/shoemoney/devbox/internal/hub/blobstore" + "github.com/shoemoney/devbox/internal/hub/meta" + "github.com/shoemoney/devbox/internal/secret" +) + +// Repro for audit finding: device B's device-local exclude drops a shared file +// from the pushed head, and device A's next pull deletes it from disk. +func TestReproPushDropsLocallyExcludedFile(t *testing.T) { + db, _ := meta.Open(":memory:") + defer db.Close() + store, _ := blobstore.NewDisk(t.TempDir()) + srv := httptest.NewServer(hub.NewServer(db, store).Handler()) + defer srv.Close() + guard, _ := secret.New(nil) + igEmpty, _ := LoadIgnore(t.TempDir()) + + A := joinDevice(t, db, srv.URL, "alice") + B := joinDevice(t, db, srv.URL, "bob") + if err := A.Publish("s"); err != nil { + t.Fatal(err) + } + rootA := t.TempDir() + + // A publishes data.bin + app.txt. + writeFile(t, rootA, "data.bin", "precious\n") + writeFile(t, rootA, "app.txt", "x\n") + snapA, _, err := Sync(A, rootA, "s", "", "", "alice", 1, igEmpty, guard, nil) + if err != nil { + t.Fatalf("A sync: %v", err) + } + + // B mounts the share with a device-local exclude for data.bin and syncs. + rootB := t.TempDir() + igB, err := LoadIgnoreWith(rootB, []string{"data.bin"}) + if err != nil { + t.Fatal(err) + } + if _, _, err := Sync(B, rootB, "s", "", "", "bob", 2, igB, guard, nil); err != nil { + t.Fatalf("B sync: %v", err) + } + + // A pulls the new head. If push dropped data.bin from the head, A deletes it. + pr, err := Pull(A, rootA, "s", "", snapA, "alice", 3, igEmpty, guard, nil) + if err != nil { + t.Fatalf("A pull: %v", err) + } + t.Logf("A pull result: written=%v deleted=%v conflicts=%v", pr.Written, pr.Deleted, pr.Conflicts) + if _, err := os.Stat(filepath.Join(rootA, "data.bin")); os.IsNotExist(err) { + t.Fatalf("CONFIRMED: A's data.bin was deleted by B's device-local exclude (deleted=%v)", pr.Deleted) + } +} diff --git a/internal/syncer/syncer.go b/internal/syncer/syncer.go index f5db79f..de1fdc9 100644 --- a/internal/syncer/syncer.go +++ b/internal/syncer/syncer.go @@ -70,8 +70,10 @@ func Push(c *transport.Client, root, share, subpath string, ig *ignore.Matcher, } } - // Splice the local subtree into the full share manifest (no-op when subpath == ""). - fullM, err := splice(c, localM, subpath, parent) + // Splice the local subtree into the full share manifest, carrying forward any + // parent-head entry this device can't see because of its own ignore rules (a + // device-local exclude must never delete a path from the shared head). + fullM, err := splice(c, localM, subpath, parent, ig, blocked) if err != nil { return Result{}, err } @@ -133,23 +135,53 @@ func Push(c *transport.Client, root, share, subpath string, ig *ignore.Matcher, }, nil } -// splice builds the full share manifest from a sub-path mount's local manifest: -// it re-prefixes local entries with subpath and keeps the parent snapshot's -// entries outside subpath. For a whole-share mount it returns localM unchanged. -func splice(c *transport.Client, localM manifest.Manifest, subpath, parent string) (manifest.Manifest, error) { - if subpath == "" { - return localM, nil - } - entries := make([]manifest.Entry, 0, len(localM.Entries)) - if parent != "" { - base, err := fetchManifest(c, parent) - if err != nil { - return manifest.Manifest{}, err +// splice builds the full share manifest from a mount's local manifest: it +// re-prefixes local entries with subpath, keeps the parent snapshot's entries +// outside subpath untouched, and — within subpath — carries forward any parent +// entry the local device didn't produce because ig excludes it (a path +// "excluded from view" is not a "path deleted by me"). An entry the guard +// blocks, or one that's genuinely gone from disk (no ignore rule hides it), is +// NOT carried forward: the guard's whole job is to keep that content out of the +// head, and a real local delete must still delete. For a whole-share mount +// (subpath == "") this same pass runs over the full path space: under and +// strip both treat "" as "everything", so it's the same logic, not a special +// case. +func splice(c *transport.Client, localM manifest.Manifest, subpath, parent string, ig *ignore.Matcher, blocked []string) (manifest.Manifest, error) { + if parent == "" { + entries := make([]manifest.Entry, 0, len(localM.Entries)) + for _, e := range localM.Entries { + e.Path = prefixPath(e.Path, subpath) + entries = append(entries, e) } - for _, e := range base.Entries { - if !under(e.Path, subpath) { - entries = append(entries, e) - } + sort.Slice(entries, func(i, j int) bool { return entries[i].Path < entries[j].Path }) + return manifest.Manifest{Entries: entries}, nil + } + + base, err := fetchManifest(c, parent) + if err != nil { + return manifest.Manifest{}, err + } + blockedSet := make(map[string]bool, len(blocked)) + for _, p := range blocked { + blockedSet[p] = true + } + localSet := make(map[string]bool, len(localM.Entries)) + for _, e := range localM.Entries { + localSet[e.Path] = true + } + + entries := make([]manifest.Entry, 0, len(base.Entries)+len(localM.Entries)) + for _, e := range base.Entries { + if !under(e.Path, subpath) { + entries = append(entries, e) // outside the mount: untouched + continue + } + local := strip(e.Path, subpath) + if localSet[local] { + continue // present locally; localM's copy (appended below) wins + } + if !blockedSet[local] && ig != nil && ig.Match(local, false) { + entries = append(entries, e) // excluded from view, not deleted: preserve } } for _, e := range localM.Entries {