Files
mesh-host/internal/apply/converge_dirs.go
T
jschoubben 1c32872fcc H-A-374: set a drifted directory the mesh made right at the engine's start
ace's Node toolchain cache directory stood at 0700 against its entry's 0711
while the engine said its composition was unchanged: an unchanged
composition is never applied, and the judgement reads the compositions, not
the disk. Each module taken as running at the engine's start now has every
directory entry its record says the mesh made given its declared owner and
mode where they drifted on disk, one journal line each; one the mesh never
made stays used as found and is said (issue 339). The verb composition's
diff names a drifted directory the mesh made. The agent test that marks
modules not composed now waits for every want it marks.

Builds: design 47 H2 §813; H-A-374, H-A-392
2026-11-22 00:26:18 +01:00

112 lines
3.7 KiB
Go

package apply
import (
"errors"
"fmt"
"os"
"git.novox.be/novox/mesh-sdk/go/declaration"
"github.com/novox/mesh-host/internal/store"
)
// ConvergeDirectories gives each directory entry of d whose record known holds, found on disk with another owner or mode
// than declared, its declared owner and mode (design 47 H2's E lines, H-A-374, H-A-392): at the engine's start, so a
// drift lasts at most until then, and with no apply of an unchanged composition. One the mesh never made (its record used
// as found, issue 339) is left as found and said. Answered as one line per directory set right or left; a directory
// declared that is missing, that has no record, or that is as declared, says nothing.
func ConvergeDirectories(d *declaration.Declaration, known store.State) []string {
if d == nil {
return nil
}
var lines []string
for _, r := range d.Resources {
dir, ok := r.(*declaration.Directory)
if !ok {
continue
}
rec, held := known.Find(dir.ID)
if !held || rec.Type != string(declaration.TypeDirectory) {
continue
}
drift, err := directoryDrift(dir)
switch {
case errors.Is(err, os.ErrNotExist) || (err == nil && drift == ""):
continue
case err != nil:
lines = append(lines, fmt.Sprintf("%s (%s) cannot be judged at this start: %v", dir.Path, dir.ID, err))
continue
}
out, err := applyDirectory(dir, rec, true)
switch {
case err != nil:
lines = append(lines, fmt.Sprintf("%s (%s) is %s and cannot be set right at this start: %v", dir.Path, dir.ID, drift, err))
case out.asFound:
lines = append(lines, fmt.Sprintf("%s (%s) is %s, and is left as found at this start: %s", dir.Path, dir.ID, drift, out.Detail))
default:
lines = append(lines, fmt.Sprintf("%s (%s) was %s, and is set right at this start", dir.Path, dir.ID, drift))
}
}
return lines
}
// DirectoryDrift is one directory entry the mesh made, as its record holds, whose owner or mode on disk differ from
// what is declared (H-A-392): its id, its path and how it differs, in words.
type DirectoryDrift struct {
ID string `json:"id"`
Path string `json:"path"`
Drift string `json:"drift"`
}
// DirectoryDrifts is each directory entry of d the mesh made, by its record in known (not used as found), whose owner or
// mode on disk differ from its entry's (H-A-392): read-only, what the verb composition's diff names as drift. A directory
// that is missing, has no record, or cannot be read says nothing here.
func DirectoryDrifts(d *declaration.Declaration, known store.State) []DirectoryDrift {
if d == nil {
return nil
}
var out []DirectoryDrift
for _, r := range d.Resources {
dir, ok := r.(*declaration.Directory)
if !ok {
continue
}
if rec, held := known.Find(dir.ID); !held || rec.Type != string(declaration.TypeDirectory) || rec.AsFound {
continue
}
if drift, err := directoryDrift(dir); err == nil && drift != "" {
out = append(out, DirectoryDrift{ID: dir.ID, Path: dir.Path, Drift: drift})
}
}
return out
}
// directoryDrift says how a directory on disk differs from its entry's owner and mode, in words; empty when it does not.
func directoryDrift(dir *declaration.Directory) (string, error) {
mode, err := dirModeOf(dir.Mode, 0o755)
if err != nil {
return "", err
}
info, err := os.Lstat(dir.Path)
if err != nil {
return "", err
}
if !info.IsDir() {
return "", fmt.Errorf("%s is not a directory", dir.Path)
}
owned, err := ownedBy(dir.Path, dir.Owner)
if err != nil {
return "", err
}
var drift string
if dirBits(info.Mode()) != dirBits(mode) {
drift = fmt.Sprintf("mode %s, %s declared", modeText(dirBits(info.Mode())), modeText(mode))
}
if !owned {
if drift != "" {
drift += ", and "
}
drift += fmt.Sprintf("owned by %s, %s declared", ownerText(info), dir.Owner)
}
return drift, nil
}