H3 hard-links-left: an entry with more than one hard link keeps its owner in the owner pass, said in the journal

Builds: design 47 H2 H-A-579 (2), H-A-583 (4), H-A-604 (4)
This commit is contained in:
2026-11-25 17:10:27 +01:00
parent 9c40b4a3fb
commit 894e2c89c2
3 changed files with 36 additions and 4 deletions
+9 -1
View File
@@ -26,7 +26,7 @@ import (
// 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
// entry with more than one hard link is left with its owner, said in the journal with its path and link count (H-A-579 (2)). 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)
@@ -69,6 +69,11 @@ func giveContents(dir string, from, to [2]int, placed map[string]bool) (int, err
if d, derr := deviceOf(path); derr != nil || d != device {
return filepath.SkipDir
}
} else if links := linkCount(info); links > 1 {
// An entry with more than one name is left with its owner (H-A-579 (2)): its other names may lie outside the
// directory, which the pass never crosses.
journal(fmt.Sprintf("%s has %d hard links, and is left with its owner when the directory %s is given its new one", path, links, root))
return nil
}
group := gid
if gid == from[1] {
@@ -86,6 +91,9 @@ func giveContents(dir string, from, to [2]int, placed map[string]bool) (int, err
return given, err
}
// journal says one line in the node-engine's journal (its standard error); a variable so a test reads what was said.
var journal = func(line string) { fmt.Fprintf(os.Stderr, "mesh-host: %s\n", line) }
// 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
+19 -3
View File
@@ -26,6 +26,7 @@ func TestADirectoryGivesItsContentsTheNewOwner(t *testing.T) {
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
linked bool // token has a second name, token.again (H-A-579 (2))
want []string
}{
{name: "what the previous owner wrote is given", from: [2]int{me, myGroup},
@@ -38,6 +39,8 @@ func TestADirectoryGivesItsContentsTheNewOwner(t *testing.T) {
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"}},
{name: "an entry with two hard links is left with its owner, and said", from: [2]int{me, myGroup}, linked: true,
want: []string{"data", "data/repo", "kept", "kept/webhook.secret", "link"}},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
@@ -60,9 +63,15 @@ func TestADirectoryGivesItsContentsTheNewOwner(t *testing.T) {
if err := os.Symlink(outside, filepath.Join(root, "link")); err != nil {
t.Fatal(err)
}
var given []string
wasChown, wasDevice := lchownContent, deviceOf
t.Cleanup(func() { lchownContent, deviceOf = wasChown, wasDevice })
if c.linked {
if err := os.Link(filepath.Join(root, "token"), filepath.Join(root, "token.again")); err != nil {
t.Fatal(err)
}
}
var given, said []string
wasChown, wasDevice, wasJournal := lchownContent, deviceOf, journal
t.Cleanup(func() { lchownContent, deviceOf, journal = wasChown, wasDevice, wasJournal })
journal = func(line string) { said = append(said, line) }
wantGroup := to[1]
if c.group != 0 {
wantGroup = c.group
@@ -95,6 +104,13 @@ func TestADirectoryGivesItsContentsTheNewOwner(t *testing.T) {
if n != len(c.want) || strings.Join(given, ",") != strings.Join(c.want, ",") {
t.Fatalf("gave %d: %v, want %v", n, given, c.want)
}
if c.linked {
if len(said) != 2 || !strings.Contains(said[0]+said[1], filepath.Join(root, "token")+" has 2 hard links") {
t.Errorf("said %q, want one line for each name of token naming its 2 hard links", said)
}
} else if len(said) != 0 {
t.Errorf("said %q, want nothing", said)
}
})
}
// The placed paths are every resource's target, cleaned.
+8
View File
@@ -16,3 +16,11 @@ func statOwner(info os.FileInfo) (uid, gid int, ok bool) {
}
return int(stat.Uid), int(stat.Gid), true
}
// linkCount is how many names a file has (its hard links); 1 where the file system does not say.
func linkCount(info os.FileInfo) uint64 {
if stat, ok := info.Sys().(*syscall.Stat_t); ok {
return uint64(stat.Nlink)
}
return 1
}