package syncer import ( "fmt" "os" "path/filepath" "strings" "sync" "github.com/shoemoney/devbox/internal/hooks" "github.com/shoemoney/devbox/internal/ignore" "github.com/shoemoney/devbox/internal/manifest" "github.com/shoemoney/devbox/internal/secret" "github.com/shoemoney/devbox/internal/transport" ) // PullResult summarizes applying a remote head to the local tree. type PullResult struct { Base string // the snapshot now applied locally (the head we merged to) Written []string // files written/updated from the hub Deleted []string // files deleted to match the hub Conflicts []string // conflict copies created (local edits preserved beside canonical) Skipped []string // paths a filesystem error skipped (e.g. file<->dir clash) Vetoed bool // a pre-pull hook aborted applying inbound changes Warnings []string // non-fatal hook errors (e.g. post-pull timed out) } // Pull fetches the hub head manifest for share, three-way merges it against the // local tree (base = last-applied snapshot, "" if none) and applies the result: // - a file changed only on the hub is written/deleted locally; // - a file changed only locally is left alone (it will be pushed); // - a file changed on BOTH sides keeps the hub version canonical, and the local // edit is preserved beside it as path.conflict--.ext; // - delete-vs-edit never loses the edit (the edit always survives). // // Never destroys a byte: every losing local edit becomes a conflict copy. func Pull(c *transport.Client, root, share, subpath, base, host string, now int64, ig *ignore.Matcher, guard *secret.Guard, hk *hooks.Runner) (PullResult, error) { head, err := c.Head(share) if err != nil { return PullResult{}, err } res := PullResult{Base: base} if head == "" || head == base { return res, nil // nothing on the hub, or already up to date } theirsFull, err := fetchManifest(c, head) if err != nil { return PullResult{}, err } theirs := filterStrip(theirsFull, subpath) var baseM manifest.Manifest if base != "" { baseFull, err := fetchManifest(c, base) if err != nil { return PullResult{}, err } baseM = filterStrip(baseFull, subpath) } ours, _, err := manifest.Build(root, ig, guard) if err != nil { return PullResult{}, err } // pre-pull may veto (e.g. stop a running container before files change). incoming := manifest.Diff(baseM, theirs) pending := append(append(append([]string{}, incoming.Added...), incoming.Modified...), incoming.Deleted...) if err := hk.Run(hooks.PrePull, pending, head); err != nil { return PullResult{Base: base, Vetoed: true}, nil } bi, oi, ti := indexEntries(baseM), indexEntries(ours), indexEntries(theirs) for p := range unionKeys(bi, oi, ti) { be, bOK := bi[p] oe, oOK := oi[p] te, tOK := ti[p] oursCh := oOK != bOK || (oOK && bOK && !manifest.SameContent(oe, be)) theirsCh := tOK != bOK || (tOK && bOK && !manifest.SameContent(te, be)) if !theirsCh { continue // hub didn't touch p; local state (changed or not) stands } // `ours` was snapshotted once by manifest.Build before this loop started // (pull.go:60); a local edit landing after that snapshot but before we // get here would otherwise look unchanged and get silently clobbered // below. Re-check the on-disk file against its snapshotted entry right // before we might overwrite it — only when hub is about to write (tOK) // and ours already had an entry to compare against (oOK); a pure hub-only // add (!oOK) has nothing on disk yet to race with. Folding a detected // edit into oursCh routes it through the existing conflict-preserving // branch below instead of adding a second write path. This narrows the // clobber window; it doesn't close it (a later edit can still land // between this check and the writeEntry rename at pull.go:424-451). if !oursCh && tOK && oOK { if cur, ok, serr := manifest.BuildFile(root, p); serr == nil && ok && !manifest.SameContent(cur, oe) { oursCh = true } } switch { case !oursCh: // Hub-only change: apply it verbatim. if tOK { if fsErr, err := writeEntry(c, root, te); err != nil { if !fsErr { return PullResult{}, err // network error: abort, retry whole sync } res.Skipped = append(res.Skipped, p) // e.g. path is now a directory } else { res.Written = append(res.Written, p) } } else if err := deleteFile(root, p); err != nil { res.Skipped = append(res.Skipped, p) } else { res.Deleted = append(res.Deleted, p) } case tOK && oOK && manifest.SameContent(te, oe): // Both made the identical change — already in agreement. case tOK && oOK: // Both edited differently: preserve ours as a conflict copy, hub canonical. cp, cerr := preserveAsConflict(root, p, host, now) if cerr != nil { res.Skipped = append(res.Skipped, p) // couldn't preserve ours; never clobber continue } if fsErr, err := writeEntry(c, root, te); err != nil { if !fsErr { return PullResult{}, err } res.Skipped = append(res.Skipped, p) } else { res.Conflicts = append(res.Conflicts, cp) res.Written = append(res.Written, p) } case tOK && !oOK: // p is absent from `ours` — but `ours` is .devignore/secret-guard // FILTERED, so this is NOT necessarily a local delete. The file may // still be on disk with valuable content (an ignored config, or the // very .env/key the guard protects). Overwriting it blindly is data // loss — exactly the bytes we promised never to lose. So if the path // really exists on disk, preserve it as a conflict copy first; only a // genuinely-absent path is a true delete where the hub wins. if dst, jerr := safeJoin(root, p); jerr == nil { if _, statErr := os.Stat(dst); statErr == nil { cp, cerr := preserveAsConflict(root, p, host, now) if cerr != nil { res.Skipped = append(res.Skipped, p) // couldn't preserve; never clobber continue } res.Conflicts = append(res.Conflicts, cp) } } if fsErr, err := writeEntry(c, root, te); err != nil { if !fsErr { return PullResult{}, err } res.Skipped = append(res.Skipped, p) } else { res.Written = append(res.Written, p) } default: // Hub deleted, ours edited (!tOK && oOK): the edit wins — keep ours, // it will be re-pushed. Or both deleted — nothing to do. } } if len(res.Conflicts) > 0 { if err := hk.Run(hooks.OnConflict, res.Conflicts, head); err != nil { res.Warnings = append(res.Warnings, err.Error()) } } if err := hk.Run(hooks.PostPull, append(append([]string{}, res.Written...), res.Deleted...), head); err != nil { res.Warnings = append(res.Warnings, err.Error()) } res.Base = head return res, nil } // Restore rewrites the local tree to match an older snapshot, then pushes it as a // new head — so a restore is itself a reversible snapshot, never a history rewrite. // // With onlyPath set, just that one file is brought back to its snapshot content // (the rest of the tree is untouched). Otherwise the whole mounted subtree is made // to match the snapshot: every snapshot entry is written and any local path the // snapshot doesn't contain is deleted. A bad onlyPath (not in the snapshot) errors // without touching the tree. func Restore(c *transport.Client, root, share, subpath, snapshot, onlyPath, host string, now int64, ig *ignore.Matcher, guard *secret.Guard, hk *hooks.Runner) (Result, error) { // Never lose a byte, even on a deliberate revert: capture any uncommitted local // edit this restore would overwrite or delete, then drop it back as a .conflict // copy once the snapshot is applied (a clean restore — no uncommitted edits — // captures nothing and reproduces the snapshot exactly). endangered, err := captureUncommitted(c, root, share, subpath, snapshot, onlyPath, ig, guard) if err != nil { return Result{}, err } if _, err := applySnapshot(c, root, subpath, snapshot, onlyPath, ig, guard); err != nil { return Result{}, err } for path, data := range endangered { if _, err := writeConflictCopy(root, path, data, host, now); err != nil { return Result{}, err } } head, err := c.Head(share) if err != nil { return Result{}, err } return Push(c, root, share, subpath, ig, guard, head, hk) } // DeployResult summarizes pinning a mount to a snapshot. type DeployResult struct { Snapshot string // the snapshot now applied (== its manifest blob hash) Written int // files written from the snapshot } // Deploy rewrites the mounted subtree to match a snapshot WITHOUT pushing — it // pins a (typically read-only) mount to a specific version, e.g. blue/green // deploys of /var/www. Unlike Restore it creates no new head: the snapshot stays // exactly as-is on the hub and history is untouched. The caller pins the mount // (config.Mount.Pinned) so the daemon won't live-advance it back to head. func Deploy(c *transport.Client, root, subpath, snapshot string, ig *ignore.Matcher, guard *secret.Guard) (DeployResult, error) { n, err := applySnapshot(c, root, subpath, snapshot, "", ig, guard) if err != nil { return DeployResult{}, err } return DeployResult{Snapshot: snapshot, Written: n}, nil } // applySnapshot rewrites root to match a snapshot's manifest (filtered+stripped // to subpath): writes every entry and, when onlyPath is empty, deletes any local // path the snapshot doesn't contain. With onlyPath set, only that one entry is // written (no deletes). Returns the number of entries written. A bad onlyPath // (not in the snapshot) errors without touching the tree. func applySnapshot(c *transport.Client, root, subpath, snapshot, onlyPath string, ig *ignore.Matcher, guard *secret.Guard) (int, error) { full, err := fetchManifest(c, snapshot) if err != nil { return 0, err } target := filterStrip(full, subpath) idx := indexEntries(target) if onlyPath != "" { e, ok := idx[onlyPath] if !ok { return 0, fmt.Errorf("path %q not in snapshot %s", onlyPath, snapshot) } if _, err := writeEntry(c, root, e); err != nil { return 0, err } return 1, nil } for _, e := range target.Entries { if _, err := writeEntry(c, root, e); err != nil { return 0, err } } // Delete tracked files present locally but absent from the snapshot. We diff // against manifest.Build (ignore- + secret-guard-filtered) ON PURPOSE: a // restore/deploy must NOT delete a user's .devignore'd runtime files (caches, // logs) or local secrets — those were never in any snapshot, so "match the // snapshot" leaves them untouched. Tracked extras (e.g. a file added in a newer // snapshot) are still removed. ponytail: never lose a byte we didn't sync. local, _, err := manifest.Build(root, ig, guard) if err != nil { return 0, err } for _, e := range local.Entries { if _, keep := idx[e.Path]; !keep { if err := deleteFile(root, e.Path); err != nil { return 0, err } } } return len(target.Entries), nil } // Sync brings a mount into agreement with the hub: pull+merge to head, then push // the merged local state; retries if the hub advanced underneath. Returns the new // base snapshot and the pull result. func Sync(c *transport.Client, root, share, subpath, base, host string, now int64, ig *ignore.Matcher, guard *secret.Guard, hk *hooks.Runner) (string, PullResult, error) { for attempt := 0; attempt < 5; attempt++ { pr, err := Pull(c, root, share, subpath, base, host, now, ig, guard, hk) if err != nil { return base, pr, err } if pr.Vetoed { return base, pr, nil // pre-pull vetoed; wait for the next nudge } base = pr.Base push, err := Push(c, root, share, subpath, ig, guard, base, hk) if err != nil { return base, pr, err } if push.Vetoed { return base, pr, nil // pre-push vetoed } if push.Conflict { // The remote head has not been applied locally. Keep the last // merged base so the next Pull three-way merges the advanced head. continue } return push.Snapshot, pr, nil } return base, PullResult{}, fmt.Errorf("sync: too many conflict retries") } // --- sub-path helpers: a mount's local root maps to share/subpath/ --- // under reports whether a share-relative path lives within subpath ("" = whole share). func under(path, subpath string) bool { if subpath == "" { return true } return path == subpath || strings.HasPrefix(path, subpath+"/") } // strip removes the subpath prefix, yielding a local-relative path. func strip(path, subpath string) string { if subpath == "" { return path } return strings.TrimPrefix(path, subpath+"/") } // prefixPath adds the subpath prefix to a local-relative path. func prefixPath(path, subpath string) string { if subpath == "" { return path } return subpath + "/" + path } // filterStrip keeps only the entries of a full share manifest that live under // subpath, with the prefix stripped (so they're local-relative). func filterStrip(m manifest.Manifest, subpath string) manifest.Manifest { if subpath == "" { return m } var out []manifest.Entry for _, e := range m.Entries { if under(e.Path, subpath) { e.Path = strip(e.Path, subpath) out = append(out, e) } } return manifest.Manifest{Entries: out} } func fetchManifest(c *transport.Client, id string) (manifest.Manifest, error) { b, err := c.GetBlob(id) if err != nil { return manifest.Manifest{}, err } return manifest.Unmarshal(b) } func indexEntries(m manifest.Manifest) map[string]manifest.Entry { idx := make(map[string]manifest.Entry, len(m.Entries)) for _, e := range m.Entries { idx[e.Path] = e } return idx } func unionKeys(maps ...map[string]manifest.Entry) map[string]struct{} { u := map[string]struct{}{} for _, m := range maps { for k := range m { u[k] = struct{}{} } } return u } // writeEntry downloads e's content and writes it atomically. A network/download // error returns fsErr=false (the caller should abort and retry the whole sync); // a filesystem error — e.g. the path is currently a directory — returns // fsErr=true so the caller can skip just that path instead of wedging the mount. // downloadConcurrency bounds parallel chunk fetches when reassembling one file. const downloadConcurrency = 8 // fetchChunks downloads a file's chunks (up to downloadConcurrency in flight) and // concatenates them IN ORDER. Memory is bounded to this one file's content — the // same as the old sequential append. ponytail: parallelism is within-file; a // many-tiny-files clone is still one file at a time — a cross-file prefetch // window is the upgrade path if that ever dominates. func fetchChunks(c *transport.Client, hashes []string) ([]byte, error) { if len(hashes) == 0 { return nil, nil } if len(hashes) == 1 { return c.GetBlob(hashes[0]) } parts := make([][]byte, len(hashes)) sem := make(chan struct{}, downloadConcurrency) var wg sync.WaitGroup var mu sync.Mutex var firstErr error for i, h := range hashes { mu.Lock() stop := firstErr != nil mu.Unlock() if stop { break } sem <- struct{}{} wg.Add(1) go func(i int, h string) { defer wg.Done() defer func() { <-sem }() b, err := c.GetBlob(h) if err != nil { mu.Lock() if firstErr == nil { firstErr = err } mu.Unlock() return } parts[i] = b }(i, h) } wg.Wait() if firstErr != nil { return nil, firstErr } var buf []byte for _, p := range parts { buf = append(buf, p...) } return buf, nil } func writeEntry(c *transport.Client, root string, e manifest.Entry) (fsErr bool, err error) { buf, gerr := fetchChunks(c, e.Chunks) if gerr != nil { return false, gerr // network } dst, serr := safeJoin(root, e.Path) if serr != nil { return true, serr // unsafe manifest path: skip it, don't apply outside root } if merr := os.MkdirAll(filepath.Dir(dst), 0o755); merr != nil { return true, merr } mode := os.FileMode(e.Mode) if mode == 0 { mode = 0o644 } if werr := atomicWrite(dst, buf, mode); werr != nil { return true, werr } return false, nil } func deleteFile(root, relPath string) error { p, serr := safeJoin(root, relPath) if serr != nil { return serr } err := os.Remove(p) if os.IsNotExist(err) { return nil } return err } // safeJoin joins a hub-supplied, slash-separated relative path under root, // refusing anything that would escape the mount (absolute path, "..", empty, or // a Windows reserved name). Manifest paths come from the hub/peers and are // applied to the local filesystem, so an unchecked "../../.ssh/authorized_keys" // would let a malicious or buggy hub write or delete files outside the mount. func safeJoin(root, relPath string) (string, error) { clean := filepath.FromSlash(relPath) if !filepath.IsLocal(clean) { return "", fmt.Errorf("refusing unsafe manifest path %q", relPath) } return filepath.Join(root, clean), nil } // freeConflictName probes for an unused path.conflict--.ext sibling of // relPath and returns its relative path (ts is nanoseconds, so collisions are // already rare; the counter is belt-and-suspenders against overwriting a prior copy). func freeConflictName(root, relPath, host string, ts int64) (string, error) { ext := filepath.Ext(relPath) stem := strings.TrimSuffix(relPath, ext) for n := 0; ; n++ { cp := fmt.Sprintf("%s.conflict-%s-%d%s", stem, host, ts, ext) if n > 0 { cp = fmt.Sprintf("%s.conflict-%s-%d-%d%s", stem, host, ts, n, ext) } if _, err := os.Stat(filepath.Join(root, filepath.FromSlash(cp))); err == nil { continue // taken } else if !os.IsNotExist(err) { return "", err } return cp, nil } } // preserveAsConflict renames the current local file at relPath to a sibling named // path.conflict--.ext, returning the conflict copy's relative path. func preserveAsConflict(root, relPath, host string, ts int64) (string, error) { cp, err := freeConflictName(root, relPath, host, ts) if err != nil { return "", err } src := filepath.Join(root, filepath.FromSlash(relPath)) if err := os.Rename(src, filepath.Join(root, filepath.FromSlash(cp))); err != nil { return "", err } return cp, nil } // writeConflictCopy writes captured bytes (a file's pre-restore local content) to // a path.conflict-- sibling. Restore uses this to drop an uncommitted // local edit beside the file AFTER the restore has overwritten/deleted the // original — so the never-lose-a-byte guarantee holds even for a deliberate revert. func writeConflictCopy(root, relPath string, data []byte, host string, ts int64) (string, error) { cp, err := freeConflictName(root, relPath, host, ts) if err != nil { return "", err } dst := filepath.Join(root, filepath.FromSlash(cp)) if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil { return "", err } if err := atomicWrite(dst, data, 0o644); err != nil { return "", err } return cp, nil } // captureUncommitted reads the on-disk bytes of any local file an explicit restore // is about to LOSE: one whose content differs from BOTH the snapshot being restored // AND the current hub head. A file equal to the snapshot isn't changing; a file // equal to head is safe in history. Everything else is an uncommitted local edit // (or a file new since head) that restore would silently overwrite or delete — so // grab its bytes now and (after restore applies) drop them as a .conflict copy. // Honors onlyPath. Same ignore/secret filtering as the apply, so we never touch a // .devignore'd or guarded file. func captureUncommitted(c *transport.Client, root, share, subpath, snapshot, onlyPath string, ig *ignore.Matcher, guard *secret.Guard) (map[string][]byte, error) { tgt, err := fetchManifest(c, snapshot) if err != nil { return nil, err } target := indexEntries(filterStrip(tgt, subpath)) headIdx := map[string]manifest.Entry{} head, err := c.Head(share) if err != nil { return nil, err } if head != "" { hm, err := fetchManifest(c, head) if err != nil { return nil, err } headIdx = indexEntries(filterStrip(hm, subpath)) } local, _, err := manifest.Build(root, ig, guard) if err != nil { return nil, err } out := map[string][]byte{} for _, oe := range local.Entries { if onlyPath != "" && oe.Path != onlyPath { continue } if te, ok := target[oe.Path]; ok && manifest.SameContent(oe, te) { continue // unchanged by the restore } if he, ok := headIdx[oe.Path]; ok && manifest.SameContent(oe, he) { continue // committed content, recoverable from history } dst, err := safeJoin(root, oe.Path) if err != nil { continue } data, err := os.ReadFile(dst) if err != nil { continue // already gone / unreadable; nothing to preserve } out[oe.Path] = data } return out, nil } // atomicWrite writes data to path via a temp file + rename (atomic on POSIX and // Windows; never leaves a half-written file in place). func atomicWrite(path string, data []byte, perm os.FileMode) error { f, err := os.CreateTemp(filepath.Dir(path), ".devbox-tmp-*") if err != nil { return err } tmp := f.Name() defer os.Remove(tmp) if _, err := f.Write(data); err != nil { f.Close() return err } // Flush the data to the platter before the rename publishes it: a power loss // after the rename but before the bytes land would leave a truncated/empty file. if err := f.Sync(); err != nil { f.Close() return err } if err := f.Close(); err != nil { return err } if err := os.Chmod(tmp, perm); err != nil { return err } if err := os.Rename(tmp, path); err != nil { return err } // fsync the parent dir so the rename entry itself survives a crash. if dir, derr := os.Open(filepath.Dir(path)); derr == nil { _ = dir.Sync() // ponytail: dir fsync is best-effort; unsupported on Windows dir.Close() } return nil }