devbox/internal/syncer/syncer.go
shoemoney 512e7f8b73 feat(M4): sub-path mounts 🎯
- mount share/subpath into any local dir; Pull filters+strips the share manifest
  to the subpath, Push splices the local subtree back into the full share head
  (entries outside the subpath preserved) so a partial mount never drops the rest
- declares all full-manifest chunks for correct hub refcounts (hub chunks size 0)
- cmd: 'devbox mount projects/p22/backend /var/www'; daemon threads Subpath
- state keyed by share+subpath+local
- test: sub-path mount sees only its subtree (stripped); editing+pushing from it
  preserves docs/ and root.txt elsewhere in the share
2026-06-23 00:36:11 -05:00

161 lines
4.8 KiB
Go

// Package syncer drives the device-side push: build a content-addressed manifest
// of a tree (filtered by .devignore and the secret guard), upload only the blobs
// the hub is missing, and commit a snapshot.
package syncer
import (
"os"
"path/filepath"
"sort"
"strings"
"git.shoemoney.ai/shoemoney/devbox/internal/chunk"
"git.shoemoney.ai/shoemoney/devbox/internal/ignore"
"git.shoemoney.ai/shoemoney/devbox/internal/manifest"
"git.shoemoney.ai/shoemoney/devbox/internal/secret"
"git.shoemoney.ai/shoemoney/devbox/internal/transport"
"git.shoemoney.ai/shoemoney/devbox/pkg/proto"
)
// Result summarizes a push.
type Result struct {
Snapshot string
Head string
Blocked []string // secret files refused upload
Uploaded int // blobs actually uploaded (the hub lacked)
Files int // files in the manifest
Conflict bool // hub rejected the push: device is behind head (pull + reconcile)
}
// Push builds a manifest of root (filtered by ig + guard), uploads any blobs the
// hub is missing (chunks + the manifest blob), then commits a snapshot on share.
// For a sub-path mount (subpath != "") root maps to share/subpath/: the local
// subtree is spliced into the share head (entries outside subpath are preserved)
// so a partial mount can push without dropping the rest of the share.
// parent is the device's last-known head ("" if none).
func Push(c *transport.Client, root, share, subpath string, ig *ignore.Matcher, guard *secret.Guard, parent string) (Result, error) {
localM, blocked, err := manifest.Build(root, ig, guard)
if err != nil {
return Result{}, err
}
// Gather the bytes of every distinct local chunk (with sizes).
blobs := map[string][]byte{}
refs := map[string]int64{} // local chunk hash -> size
for _, e := range localM.Entries {
data, err := os.ReadFile(filepath.Join(root, filepath.FromSlash(e.Path)))
if err != nil {
return Result{}, err
}
cs, err := chunk.Split(data)
if err != nil {
return Result{}, err
}
for _, ch := range cs {
if _, ok := blobs[ch.Hash]; !ok {
blobs[ch.Hash] = data[ch.Offset : ch.Offset+ch.Size]
refs[ch.Hash] = ch.Size
}
}
}
// Splice the local subtree into the full share manifest (no-op when subpath == "").
fullM, err := splice(c, localM, subpath, parent)
if err != nil {
return Result{}, err
}
manBytes, err := fullM.Marshal()
if err != nil {
return Result{}, err
}
manHash := fullM.ID()
blobs[manHash] = manBytes
// Upload only what the hub lacks.
all := make([]string, 0, len(blobs))
for h := range blobs {
all = append(all, h)
}
missing, err := c.Have(all)
if err != nil {
return Result{}, err
}
for _, h := range missing {
if err := c.PutBlob(h, blobs[h]); err != nil {
return Result{}, err
}
}
// Declare every distinct chunk the full manifest references so the hub's
// refcounts stay correct. Local chunks carry their size; chunks already on
// the hub (outside the subpath) carry 0 — the hub keeps the size it recorded
// on first upload and just bumps the refcount.
seen := map[string]bool{}
var chunks []proto.ChunkRef
for _, e := range fullM.Entries {
for _, h := range e.Chunks {
if seen[h] {
continue
}
seen[h] = true
chunks = append(chunks, proto.ChunkRef{Hash: h, Size: refs[h]})
}
}
resp, err := c.Push(proto.PushRequest{
Share: share,
Parent: parent,
ManifestHash: manHash,
Chunks: chunks,
})
if err != nil {
return Result{}, err
}
return Result{
Snapshot: resp.Snapshot,
Head: resp.Head,
Blocked: blocked,
Uploaded: len(missing),
Files: len(localM.Entries),
Conflict: resp.Conflict,
}, 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
}
for _, e := range base.Entries {
if !under(e.Path, subpath) {
entries = append(entries, e)
}
}
}
for _, e := range localM.Entries {
e.Path = prefixPath(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
}
// LoadIgnore compiles root/.devignore (an empty matcher if the file is absent).
func LoadIgnore(root string) (*ignore.Matcher, error) {
b, err := os.ReadFile(filepath.Join(root, ".devignore"))
if os.IsNotExist(err) {
return ignore.Compile(nil)
}
if err != nil {
return nil, err
}
return ignore.Compile(strings.Split(string(b), "\n"))
}