Review round 1: the contents are now given before the directory's own owner changes, so a walk that fails leaves the directory as it was and the next apply walks again; an entry gone meanwhile is nothing to give. An entry is matched by its user alone: one of another user is left with everything below it, and one given the new user gets the new group only where its group was the previous group. A test now applies a directory whose owner changed, with stand-ins for the chowns, and shows the contents given first and a placed child left. Builds: design 47 H2 H-A-550, H-A-554 (2)
118 lines
3.8 KiB
Go
118 lines
3.8 KiB
Go
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 (
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/declaration"
|
|
)
|
|
|
|
// giveContents gives every entry below dir whose user is from's (uid) to the new user to's, and the new group to's only
|
|
// where the entry's group was from's (H-A-554 (2)); but for the paths placed names (cleaned paths the declaration places)
|
|
// and what lies below them, an entry of another user and what lies below it, and what lies on another file system. An
|
|
// entry gone while the walk runs is nothing to give. Answers how many entries changed hands. Called before the directory's
|
|
// own owner changes (H-A-554 (2)), so a walk that failed is walked again at the next apply.
|
|
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 {
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
return 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 errors.Is(err, fs.ErrNotExist) {
|
|
return skip()
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
uid, gid, ok := statOwner(info)
|
|
if !ok || uid != from[0] {
|
|
return skip()
|
|
}
|
|
if e.IsDir() {
|
|
if d, derr := deviceOf(path); derr != nil || d != device {
|
|
return filepath.SkipDir
|
|
}
|
|
}
|
|
group := gid
|
|
if gid == from[1] {
|
|
group = to[1]
|
|
}
|
|
if err := lchownContent(path, to[0], group); err != nil {
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
return skip()
|
|
}
|
|
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), before the
|
|
// directory's own owner changes.
|
|
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
|
|
}
|
|
|
|
// ownDirectory gives a directory changed by path its owner (own); a variable so a test sees it without being root.
|
|
var ownDirectory = own
|