M8 networkmanager: the folder's unknown is the module's

The folder the two parts share says unknown module, so the node-engine does nothing
inside it and the watcher reacts: a path it does not know is left as it is and written
to its journal once per path, at its start and at the file event that makes it; the
README says so.

Builds: design 08 sign-in-page §12, §24, §25, §27; lead answer L-1 (hq 3a3a18a9)
This commit is contained in:
jochen
2026-11-24 21:31:51 +01:00
parent dd35217275
commit e20d3c7548
7 changed files with 184 additions and 4 deletions
@@ -4,6 +4,7 @@ package main
// SessionNew, and the system bus's socket coming back, heard as a file event of the kernel on its folder.
import (
"bytes"
"errors"
"fmt"
"os"
@@ -338,3 +339,56 @@ func watchFiles(dirs []string, match func(string) bool, stop <-chan struct{}) <-
}()
return out
}
// pathsMade hands each path a file event of the kernel makes in one of dirs, until stop closes; a folder that is not
// there is not watched, and said.
func pathsMade(dirs []string, stop <-chan struct{}) <-chan string {
out := make(chan string, 16)
fd, err := unix.InotifyInit1(unix.IN_CLOEXEC | unix.IN_NONBLOCK)
if err != nil {
journal("watching %v failed: %v", dirs, err)
return out
}
wds := map[int32]string{}
for _, dir := range dirs {
wd, err := unix.InotifyAddWatch(fd, dir, unix.IN_CREATE|unix.IN_MOVED_TO)
if err != nil {
journal("watching %s failed: %v", dir, err)
continue
}
wds[int32(wd)] = dir
}
f := os.NewFile(uintptr(fd), "inotify")
go func() {
<-stop
f.Close()
}()
go func() {
defer f.Close()
buf := make([]byte, 64*(unix.SizeofInotifyEvent+unix.NAME_MAX+1))
for {
n, err := f.Read(buf)
if err != nil {
return
}
for off := 0; off+unix.SizeofInotifyEvent <= n; {
ev := (*unix.InotifyEvent)(unsafe.Pointer(&buf[off]))
nameAt := off + unix.SizeofInotifyEvent
end := nameAt + int(ev.Len)
if end > n {
break
}
name := string(bytes.TrimRight(buf[nameAt:end], "\x00"))
off = end
if dir, ok := wds[ev.Wd]; ok && name != "" {
select {
case out <- filepath.Join(dir, name):
case <-stop:
return
}
}
}
}
}()
return out
}
@@ -114,3 +114,52 @@ func TestTheWatchersSocketAnswersOneLine(t *testing.T) {
t.Fatal("a closed socket answered")
}
}
// A path in the folder the watcher does not know is left as it is and written to its journal once per path: what is
// there at its start, and what a file event makes after (§12; lead answer L-1).
func TestAPathTheWatcherDoesNotKnowIsLeftAndSaidOnce(t *testing.T) {
lines := journalLines(t)
sys := &fakeSystem{}
sys.set(guest(ratingFull))
w := aWatcher(t, sys)
f := w.Folder
if _, err := f.keep(eventEnded, "s", map[string]string{}); err != nil {
t.Fatal(err)
}
f.writeCheckAddress("http://check.example.net/")
for _, p := range []string{filepath.Join(f.Dir, "notes.txt"), filepath.Join(f.Dir, pendingName, "stray")} {
if err := os.WriteFile(p, []byte("x"), 0o600); err != nil {
t.Fatal(err)
}
}
made := make(chan string)
w.Made = func(dirs []string, stop <-chan struct{}) <-chan string {
if len(dirs) != 2 || dirs[0] != f.Dir || dirs[1] != filepath.Join(f.Dir, pendingName) {
t.Errorf("watched %v", dirs)
}
return made
}
stop, done := make(chan struct{}), make(chan struct{})
go func() { w.run(stop); close(done) }()
for _, p := range []string{filepath.Join(f.Dir, "other"), filepath.Join(f.Dir, "other"), filepath.Join(f.Dir, "sequence.new"),
filepath.Join(f.Dir, pendingName, "0000000002.json")} {
made <- p
}
w.askLoop(askState)
close(stop)
<-done
var said []string
for _, l := range lines() {
if strings.Contains(l, "is not the module's") {
said = append(said, strings.TrimPrefix(l, f.Dir+"/"))
}
}
want := []string{"notes.txt is not the module's: left as it is", "pending/stray is not the module's: left as it is",
"other is not the module's: left as it is"}
if strings.Join(said, "|") != strings.Join(want, "|") {
t.Fatalf("said %q", said)
}
if _, err := os.Stat(filepath.Join(f.Dir, "notes.txt")); err != nil {
t.Fatal("a path the watcher does not know was not left")
}
}
@@ -107,7 +107,7 @@ func TestTheManifestDeclaresTheSignInParts(t *testing.T) {
}
dir := byID["sign-in-state"]
if dir["type"] != "directory" || dir["path"] != "${machine:account-home}/"+stateHome ||
dir["owner"] != "${machine:account}" || dir["mode"] != "0700" {
dir["owner"] != "${machine:account}" || dir["mode"] != "0700" || dir["unknown"] != "module" {
t.Errorf("the folder is %v", dir)
}
p := byID["sign-in"]
@@ -0,0 +1,60 @@
package main
// A path in the module's folder the watcher does not know (design 08 sign-in-page §12; lead answer L-1): the folder's
// `unknown` is `module`, so the node-engine does nothing inside it and the watcher reacts. It leaves such a path as it
// is and writes it to its journal once per path: at its start for what is there, and at each file event of the kernel
// that makes one.
import (
"os"
"path/filepath"
"regexp"
"strings"
)
// keptFile is a kept event's name in pending/, or one being written beside it.
var keptFile = regexp.MustCompile(`^[0-9]{10}\.json(\.new)?$`)
// knownPath says whether path is one of the folder's own: watch.sock, check-address, pending/ and its files,
// names.json, sequence, or a file being written beside one of them.
func (f Folder) knownPath(path string) bool {
rel, err := filepath.Rel(f.Dir, path)
if err != nil || strings.HasPrefix(rel, "..") {
return false
}
dir, name := filepath.Split(rel)
switch dir {
case "":
name = strings.TrimSuffix(name, ".new")
return name == socketName || name == checkName || name == pendingName || name == namesName || name == sequenceName
case pendingName + "/":
return keptFile.MatchString(name)
}
return false
}
// unknownAtStart is every path in the folder and in pending/ the watcher does not know.
func (f Folder) unknownAtStart() []string {
var out []string
for _, dir := range []string{f.Dir, filepath.Join(f.Dir, pendingName)} {
entries, _ := os.ReadDir(dir)
for _, e := range entries {
if p := filepath.Join(dir, e.Name()); !f.knownPath(p) {
out = append(out, p)
}
}
}
return out
}
// strange writes a path the watcher does not know to its journal, once per path, and leaves it as it is.
func (w *Watcher) strange(path string) {
if w.Folder.knownPath(path) || w.told[path] {
return
}
if w.told == nil {
w.told = map[string]bool{}
}
w.told[path] = true
journal("%s is not the module's: left as it is", path)
}
@@ -11,6 +11,7 @@ import (
"fmt"
"net/url"
"os"
"path/filepath"
"time"
"git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/nodeuplink"
@@ -148,6 +149,10 @@ type Watcher struct {
// Beside runs an open beside the loop; the func it returns is run by the loop at its end.
Beside func(work func() func())
// Made fires with each path a file event of the kernel makes in one of dirs, until stop closes.
Made func(dirs []string, stop <-chan struct{}) <-chan string
told map[string]bool
starting *sessionStart
opening bool
later []func()
@@ -166,7 +171,7 @@ type Watcher struct {
func newWatcher(f Folder) *Watcher {
w := &Watcher{Folder: f, Dial: dialSystemBus, SocketThere: systemSocketThere, Now: time.Now, asks: make(chan asked),
UID: uint32(os.Getuid()), Environment: userEnvironment, FileEvents: fileEvents, After: time.After,
Desk: newSessionDesk(), ends: make(chan func())}
Desk: newSessionDesk(), ends: make(chan func()), Made: pathsMade}
w.Beside = w.besideLoop
return w
}
@@ -206,6 +211,13 @@ func (w *Watcher) run(stop <-chan struct{}) {
if w.asks == nil {
w.asks = make(chan asked)
}
var made <-chan string
if w.Made != nil {
made = w.Made([]string{w.Folder.Dir, filepath.Join(w.Folder.Dir, pendingName)}, stop)
}
for _, p := range w.Folder.unknownAtStart() {
w.strange(p)
}
w.subscribe()
for {
select {
@@ -227,6 +239,8 @@ func (w *Watcher) run(stop <-chan struct{}) {
case <-w.socketAt:
w.socketAt = nil
w.subscribe()
case p := <-made:
w.strange(p)
case end := <-w.ends:
end()
case a := <-w.asks: