diff options
| author | Ophestra <cat@gensokyo.uk> | 2025-03-23 14:14:45 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2025-03-23 14:53:50 +0900 |
| commit | 75e0c5d4061d68dc4802285ec137e464f4e01b17 (patch) | |
| tree | b8307fa68188bac4c522afc36c27a5c1fb7c02da /test/sandbox/mount.go | |
| parent | 770b37ae166a18f495865e96ccafc6f9fb9f4e88 (diff) | |
test/sandbox: parse full test case
This makes declaring multiple tests much cleaner.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'test/sandbox/mount.go')
| -rw-r--r-- | test/sandbox/mount.go | 44 |
1 files changed, 31 insertions, 13 deletions
diff --git a/test/sandbox/mount.go b/test/sandbox/mount.go index 9a3b58c9..d65824f2 100644 --- a/test/sandbox/mount.go +++ b/test/sandbox/mount.go @@ -12,6 +12,7 @@ import "C" import ( "fmt" + "iter" "runtime" "sync" "unsafe" @@ -49,21 +50,38 @@ func (e *Mntent) Is(want *Mntent) bool { (e.Passno == want.Passno || want.Passno == -1) } -func IterMounts(name string, f func(e *Mntent)) error { - m := new(mounts) - m.p = name - if err := m.open(); err != nil { - return err - } +type MountsFile struct { + m *mounts + mu sync.Mutex + done bool +} - for m.scan() { - e := new(Mntent) - m.copy(e) - f(e) - } +func OpenMounts(name string) (*MountsFile, error) { + f := new(MountsFile) + f.m = new(mounts) + f.m.p = name + return f, f.m.open() +} + +func (f *MountsFile) Err() error { return f.m.Err() } +func (f *MountsFile) Entries() iter.Seq[*Mntent] { + return func(yield func(*Mntent) bool) { + f.mu.Lock() + defer f.mu.Unlock() + if f.done { + return + } - m.close() - return m.Err() + for f.m.scan() { + e := new(Mntent) + f.m.copy(e) + if !yield(e) { + return + } + } + f.done = true + f.m.close() + } } type mounts struct { |
