Give a directory's contents before the directory, matched by user

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)
This commit is contained in:
2026-11-23 12:25:59 +01:00
parent d16c4cd003
commit 684931e099
4 changed files with 105 additions and 18 deletions
+5 -3
View File
@@ -1815,14 +1815,16 @@ func applyDirectory(r *declaration.Directory, previous store.Applied, wasBefore
return out, err
}
if !ownedAlready {
if err := own(r.Path, r.Owner); err != nil {
return out, err
}
// The contents first (H-A-554 (2)): a walk that fails leaves the directory's owner as it was, so the next apply
// walks again.
if existed {
if err := giveContentsTo(r, before, placed); err != nil {
return out, err
}
}
if err := ownDirectory(r.Path, r.Owner); err != nil {
return out, err
}
}
out.Action = "unchanged"
+26 -6
View File
@@ -10,6 +10,7 @@ package apply
// 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"
@@ -18,9 +19,11 @@ import (
"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.
// 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)
@@ -30,6 +33,9 @@ func giveContents(dir string, from, to [2]int, placed map[string]bool) (int, 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 {
@@ -45,11 +51,14 @@ func giveContents(dir string, from, to [2]int, placed map[string]bool) (int, err
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] || gid != from[1] {
if !ok || uid != from[0] {
return skip()
}
if e.IsDir() {
@@ -57,7 +66,14 @@ func giveContents(dir string, from, to [2]int, placed map[string]bool) (int, err
return filepath.SkipDir
}
}
if err := lchownContent(path, to[0], to[1]); err != nil {
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++
@@ -82,7 +98,8 @@ func placedPaths(resources ...[]declaration.Resource) map[string]bool {
return out
}
// giveContentsTo gives the contents of the directory r, found as before, to r's declared owner (giveContents).
// 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 == "" {
@@ -95,3 +112,6 @@ func giveContentsTo(r *declaration.Directory, before os.FileInfo, placed map[str
_, 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
+63 -3
View File
@@ -1,13 +1,16 @@
package apply
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"syscall"
"testing"
"git.novox.be/novox/mesh-sdk/go/declaration"
"github.com/novox/mesh-host/internal/store"
)
// A directory whose owner changes gives its contents the new owner (H-A-545 (2), H-A-550): what its previous owner
@@ -21,6 +24,7 @@ func TestADirectoryGivesItsContentsTheNewOwner(t *testing.T) {
from [2]int
placed func(root string) []string
other string // a directory the stand-in deviceOf says is on another file system
group int // the group an entry keeps, when not to's
want []string
}{
{name: "what the previous owner wrote is given", from: [2]int{me, myGroup},
@@ -28,7 +32,9 @@ func TestADirectoryGivesItsContentsTheNewOwner(t *testing.T) {
{name: "a declared child with the same previous owner keeps its owner, and what lies below it",
from: [2]int{me, myGroup}, placed: func(root string) []string { return []string{filepath.Join(root, "data")} },
want: []string{"kept", "kept/webhook.secret", "link", "token"}},
{name: "what another account owns is left", from: [2]int{me + 1, myGroup}},
{name: "what another user owns is left", from: [2]int{me + 1, myGroup}},
{name: "the new user is given, the group only where it was the previous group", from: [2]int{me, myGroup + 1},
group: myGroup, want: []string{"data", "data/repo", "kept", "kept/webhook.secret", "link", "token"}},
{name: "another file system is not crossed", from: [2]int{me, myGroup}, other: "kept",
want: []string{"data", "data/repo", "link", "token"}},
}
@@ -56,9 +62,13 @@ func TestADirectoryGivesItsContentsTheNewOwner(t *testing.T) {
var given []string
wasChown, wasDevice := lchownContent, deviceOf
t.Cleanup(func() { lchownContent, deviceOf = wasChown, wasDevice })
wantGroup := to[1]
if c.group != 0 {
wantGroup = c.group
}
lchownContent = func(path string, uid, gid int) error {
if uid != to[0] || gid != to[1] {
t.Errorf("%s given to %d:%d, want %d:%d", path, uid, gid, to[0], to[1])
if uid != to[0] || gid != wantGroup {
t.Errorf("%s given to %d:%d, want %d:%d", path, uid, gid, to[0], wantGroup)
}
rel, _ := filepath.Rel(root, path)
given = append(given, rel)
@@ -93,3 +103,53 @@ func TestADirectoryGivesItsContentsTheNewOwner(t *testing.T) {
t.Fatalf("placed %v", got)
}
}
// Through the apply (review r1 of fix/gitea-runs-as): a directory found with another owner than declared gives its
// contents the new owner first, then itself; a placed child with the same previous owner keeps its owner; and a walk that
// fails leaves the directory's owner as it was.
func TestADirectoryWhoseOwnerChangedGivesItsContentsFirst(t *testing.T) {
me, myGroup := os.Getuid(), os.Getgid()
root := t.TempDir()
dir := filepath.Join(root, "state")
for _, d := range []string{dir, filepath.Join(dir, "data")} {
if err := os.Mkdir(d, 0o700); err != nil {
t.Fatal(err)
}
}
for _, f := range []string{"webhook.secret", "data/repo"} {
if err := os.WriteFile(filepath.Join(dir, f), []byte("x"), 0o600); err != nil {
t.Fatal(err)
}
}
owner := fmt.Sprintf("%d:%d", me+4242, myGroup+4242)
r := &declaration.Directory{ID: "gitea.state", Path: dir, Mode: "0700", Owner: owner}
placed := map[string]bool{filepath.Join(dir, "data"): true}
for _, fails := range []bool{true, false} {
var order []string
wasContent, wasOwn, wasF := lchownContent, ownDirectory, fchownDirectory
lchownContent = func(path string, uid, gid int) error {
if fails {
return syscall.EPERM
}
rel, _ := filepath.Rel(dir, path)
order = append(order, rel)
return nil
}
ownDirectory = func(path, o string) error { order = append(order, "."); return nil }
fchownDirectory = func(fd, uid, gid int) error { order = append(order, "."); return nil }
_, err := applyDirectory(r, store.Applied{Target: dir}, true, placed)
lchownContent, ownDirectory, fchownDirectory = wasContent, wasOwn, wasF
if fails {
if err == nil || len(order) != 0 {
t.Fatalf("a failing walk answered %v and changed %v; want an error and the directory's owner left", err, order)
}
continue
}
if err != nil {
t.Fatal(err)
}
if strings.Join(order, ",") != "webhook.secret,." {
t.Fatalf("changed %v, want the contents but the placed data, then the directory", order)
}
}
}
+11 -6
View File
@@ -17,6 +17,9 @@ import (
"git.novox.be/novox/mesh-sdk/go/declaration"
)
// fchownDirectory gives a mesh directory its owner through its descriptor; a variable so a test sees it without being root.
var fchownDirectory = unix.Fchown
// meshDirsByDescriptor says this system can open a directory component by component without following a link.
const meshDirsByDescriptor = true
@@ -88,18 +91,20 @@ func applyMeshDirectory(r *declaration.Directory, out Outcome, mode os.FileMode,
}
ownedAlready := !chown || (beforeUID == uid && beforeGID == gid)
if !ownedAlready {
if err := unix.Fchown(fd, uid, gid); err != nil {
// The contents first (H-A-554 (2)): a walk that fails leaves the directory's owner as it was, so the next apply
// walks again.
if existed && !created {
if _, err := giveContents(walk.At, [2]int{beforeUID, beforeGID}, [2]int{uid, gid}, placed); err != nil {
return out, err
}
}
if err := fchownDirectory(fd, uid, gid); err != nil {
return out, fmt.Errorf("cannot give %s to %q: %w", r.Path, r.Owner, &os.PathError{Op: "chown", Path: r.Path, Err: err})
}
// A chown may take the setgid bit: the mode is set again after it.
if err := unix.Fchmod(fd, unixDirMode(mode)); err != nil {
return out, &os.PathError{Op: "chmod", Path: r.Path, Err: err}
}
if existed && !created {
if _, err := giveContents(walk.At, [2]int{beforeUID, beforeGID}, [2]int{uid, gid}, placed); err != nil {
return out, err
}
}
}
// Read back, from the descriptor that was changed.