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
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
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
|
||||
}
|
||||
@@ -47,21 +47,49 @@ func TestADirectoryInTheMeshsOwnTreeMadeByTheEngineIsTheMeshs(t *testing.T) {
|
||||
if info, _ := os.Stat(made); info.Mode().Perm() != 0o750 {
|
||||
t.Errorf("it was not given its declared mode: %04o", info.Mode().Perm())
|
||||
}
|
||||
// H-A-374, H-A-392: a record-held directory whose mode drifted on disk is set right at the engine's start, with one
|
||||
// line, though its composition is unchanged; as declared, it says nothing.
|
||||
if err := os.Chmod(made, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
postgres := declare(t, `{"id":"postgres.data","type":"directory","path":"`+made+`","mode":"0750"}`)
|
||||
if drifts := DirectoryDrifts(postgres, state); len(drifts) != 1 || drifts[0].Drift != "mode 0700, 0750 declared" {
|
||||
t.Errorf("the drift read is %+v", drifts)
|
||||
}
|
||||
if lines := ConvergeDirectories(postgres, state); len(lines) != 1 || !strings.Contains(lines[0], "was mode 0700, 0750 declared, and is set right") {
|
||||
t.Errorf("a drifted directory the mesh made was converged as %q", lines)
|
||||
}
|
||||
if info, _ := os.Stat(made); info.Mode().Perm() != 0o750 {
|
||||
t.Errorf("it was not set right at the start: %04o", info.Mode().Perm())
|
||||
}
|
||||
if lines := ConvergeDirectories(postgres, state); len(lines) != 0 {
|
||||
t.Errorf("a directory as declared was said at the start: %q", lines)
|
||||
}
|
||||
|
||||
// Outside the mesh's tree the rule of issue 339 stands: found, used as found.
|
||||
// Outside the mesh's tree the rule of issue 339 stands: found, used as found; and left so at a start, said.
|
||||
other := filepath.Join(root, "var-lib", "notes")
|
||||
if err := os.MkdirAll(other, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
said = nil
|
||||
decl = `{"id":"notes.data","type":"directory","path":"` + other + `","mode":"0750"}`
|
||||
if _, _, err := Apply(context.Background(), archHost(t), declare(t, decl), store.State{}, store.OriginDeclared,
|
||||
noServices, func(line string) { said = append(said, line) }, nil); err != nil {
|
||||
_, found, err := Apply(context.Background(), archHost(t), declare(t, decl), store.State{}, store.OriginDeclared,
|
||||
noServices, func(line string) { said = append(said, line) }, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(strings.Join(said, "\n"), "WARNING notes.data") {
|
||||
t.Errorf("a directory found outside the mesh's tree was not used as found: %q", said)
|
||||
}
|
||||
if lines := ConvergeDirectories(declare(t, decl), found); len(lines) != 1 || !strings.Contains(lines[0], "left as found") {
|
||||
t.Errorf("a directory the mesh never made was converged as %q", lines)
|
||||
}
|
||||
if drifts := DirectoryDrifts(declare(t, decl), found); len(drifts) != 0 {
|
||||
t.Errorf("a directory the mesh never made is named as drift: %+v", drifts)
|
||||
}
|
||||
if info, _ := os.Stat(other); info.Mode().Perm() != 0o700 {
|
||||
t.Errorf("a directory the mesh never made was changed at the start: %04o", info.Mode().Perm())
|
||||
}
|
||||
}
|
||||
|
||||
// Review of issue 1873's fix: the tree is judged by where the path leads, not by its text. A root-only link inside the
|
||||
|
||||
Reference in New Issue
Block a user