From 7fd42ec9119d4d5d8c21e455bdf3f913d2b69973 Mon Sep 17 00:00:00 2001 From: haoqixu Date: Fri, 3 Jan 2025 17:56:49 +0800 Subject: [PATCH] sysfs: always reopen directory before readdir Signed-off-by: haoqixu --- internal/sysfs/osfile.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/internal/sysfs/osfile.go b/internal/sysfs/osfile.go index a9b01eb6a9..3b87037171 100644 --- a/internal/sysfs/osfile.go +++ b/internal/sysfs/osfile.go @@ -4,7 +4,6 @@ import ( "io" "io/fs" "os" - "runtime" experimentalsys "github.com/tetratelabs/wazero/experimental/sys" "github.com/tetratelabs/wazero/internal/fsapi" @@ -15,7 +14,15 @@ func newOsFile(path string, flag experimentalsys.Oflag, perm fs.FileMode, f *os. // Windows cannot read files written to a directory after it was opened. // This was noticed in #1087 in zig tests. Use a flag instead of a // different type. - reopenDir := runtime.GOOS == "windows" + // + // As POSIX states, if a file is removed from or added to the directory + // after the most recent call to opendir() or rewinddir(), whether a + // subsequent call to readdir() returns an entry for that file is unspecified. + // + // So there is no guarantee that files added after opendir() will be visible + // in readdir(). We need to reopendir() to get the new state of the directory + // before readdir(). + reopenDir := true return &osFile{path: path, flag: flag, perm: perm, reopenDir: reopenDir, file: f, fd: f.Fd()} }