Keep the original of any file the host writes over without a record of it, on every node, and name where (hq ADR 0100)

This commit is contained in:
2026-09-22 18:35:03 +02:00
parent 272e1a65ea
commit bde5e3461c
2 changed files with 61 additions and 6 deletions
+27 -4
View File
@@ -322,7 +322,14 @@ func ApplyKeeping(
if o, isOpening := resource.(*declaration.Opening); isOpening {
outcome, err = applyOpening(ctx, o, run, fw)
} else {
outcome, err = applyOne(ctx, sys, resource, run, changed, declares, was, unseal)
// A file this host has no record of, under any id, is the machine's until the mesh
// writes over it — on any node, adopted or not: its original is kept first.
var keepFound Keep
if f, isFile := resource.(*declaration.File); isFile && was.ID == "" &&
!known.Recorded(string(declaration.TypeFile), f.Path) {
keepFound = keep
}
outcome, err = applyOne(ctx, sys, resource, run, changed, declares, was, unseal, keepFound)
}
if err != nil {
failed := &Error{Resource: resource.Identity(), Err: err, Done: report}
@@ -423,12 +430,12 @@ type Unseal func(sealed string) ([]byte, error)
func applyOne(ctx context.Context, sys system.System, r declaration.Resource, run Runner,
changed map[string]bool, declares map[string]string, previous store.Applied,
unseal Unseal) (Outcome, error) {
unseal Unseal, keepFound Keep) (Outcome, error) {
switch res := r.(type) {
case *declaration.Directory:
return applyDirectory(res)
case *declaration.File:
return applyFile(res, previous, unseal)
return applyFile(res, previous, unseal, keepFound)
case *declaration.Service:
return applyService(ctx, sys, res, run, changed)
case *declaration.Package:
@@ -573,7 +580,9 @@ func applyAccess(r *declaration.Access) (Outcome, error) {
return out, nil
}
func applyFile(r *declaration.File, previous store.Applied, unseal Unseal) (Outcome, error) {
// keepFound, when not nil, is where the original of a file this host has no record of is kept
// before it is written over (novox/hq ADR 0100): once, never overwritten, and named in the outcome.
func applyFile(r *declaration.File, previous store.Applied, unseal Unseal, keepFound Keep) (Outcome, error) {
if r.Into != "" {
return applyInto(r, previous)
}
@@ -670,7 +679,15 @@ func applyFile(r *declaration.File, previous store.Applied, unseal Unseal) (Outc
drifted := existed && previous.Wrote != "" && digestOf(string(existing)) != previous.Wrote
modeSame := existed && beforeMode == mode.Perm()
kept := ""
if !contentSame {
if existed && keepFound != nil {
// Before anything is written: a keep that fails stops the write, since the
// original could not be had back otherwise.
if kept, err = keepFound(r.Path, existing, beforeMode); err != nil {
return out, fmt.Errorf("keeping the original of %s before writing over it: %w", r.Path, err)
}
}
if err := os.MkdirAll(filepath.Dir(r.Path), 0o755); err != nil {
return out, err
}
@@ -734,6 +751,12 @@ func applyFile(r *declaration.File, previous store.Applied, unseal Unseal) (Outc
default:
out.Action = "unchanged"
}
if kept != "" {
if out.Detail != "" {
out.Detail += "; "
}
out.Detail += "the file found here, which the mesh had no record of, was kept at " + kept
}
return out, nil
}