package apply // A directory whose owner changes gives its contents the new owner too (design 47 H2, H-A-545 (2), H-A-550): a module // moved to an account of its own (runs-as) keeps reading what it wrote as the account it ran as before, and nobody // changes an owner by hand. // // Only what the directory's previous owner held, and only what no resource of the declaration places: a path the // declaration places (a directory or a file of its own, gitea's data below its state) keeps its declared owner, and // nothing below it is touched; an entry another account owns is left, and nothing below it is walked. No link is // followed (a link is owned as a link, Lchown), and the walk stays on the directory's file system. import ( "fmt" "io/fs" "os" "path/filepath" "git.novox.be/novox/mesh-sdk/go/declaration" ) // giveContents gives every entry below dir that from (uid, gid) owns to the new owner to (uid, gid), but for the paths // placed names (cleaned paths the declaration places) and what lies below them, an entry from does not own and what lies // below it, and what lies on another file system. Answers how many entries changed hands. func giveContents(dir string, from, to [2]int, placed map[string]bool) (int, error) { root := filepath.Clean(dir) device, err := deviceOf(root) if err != nil { return 0, fmt.Errorf("the file system of %s cannot be read: %w", root, err) } given := 0 err = filepath.WalkDir(root, func(path string, e fs.DirEntry, err error) error { if err != nil { return err } if path == root { return nil } skip := func() error { if e.IsDir() { return filepath.SkipDir } return nil } if placed[filepath.Clean(path)] { return skip() } info, err := os.Lstat(path) if err != nil { return err } uid, gid, ok := statOwner(info) if !ok || uid != from[0] || gid != from[1] { return skip() } if e.IsDir() { if d, derr := deviceOf(path); derr != nil || d != device { return filepath.SkipDir } } if err := lchownContent(path, to[0], to[1]); err != nil { return fmt.Errorf("cannot give %s to the directory's new owner: %w", path, err) } given++ return nil }) return given, err } // lchownContent owns one entry as itself, a link as a link; a variable so a test sees what was given without being root. var lchownContent = os.Lchown // placedPaths is every path the resources place, cleaned: what giveContents leaves to its own resource. func placedPaths(resources ...[]declaration.Resource) map[string]bool { out := map[string]bool{} for _, list := range resources { for _, r := range list { if t := r.Target(); t != "" { out[filepath.Clean(t)] = true } } } return out } // giveContentsTo gives the contents of the directory r, found as before, to r's declared owner (giveContents). func giveContentsTo(r *declaration.Directory, before os.FileInfo, placed map[string]bool) error { fromUID, fromGID, ok := statOwner(before) if !ok || r.Owner == "" { return nil } uid, gid, err := idsOf(r.Owner) if err != nil { return fmt.Errorf("%s should belong to %q: %w", r.Path, r.Owner, err) } _, err = giveContents(r.Path, [2]int{fromUID, fromGID}, [2]int{uid, gid}, placed) return err }