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 placed := placedPaths(d.Resources) 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, placed) 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 }