]> Cypherpunks repositories - gostls13.git/commitdiff
os: do not skip directory entries with zero inodes on wasip1
authorAchille Roussel <achille.roussel@gmail.com>
Wed, 5 Jul 2023 01:15:58 +0000 (18:15 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 6 Jul 2023 23:23:41 +0000 (23:23 +0000)
When building programs to GOOS=wasip1, the program does not have the
guarantees that the underlying directories will come from a file system
where a zero inode value indicates that the entry was deleted but not
yet removed from the directory. The host runtime may be running on
windows or may be exposing virtual user-space file systems that do not
have the concept of inodes. In those setup, we assume that the host
runtime is in charge of dealing with edge cases such as skipping
directory entries with zero inodes when needed, and the guest
application should trust the list of entries that it sees;
therefore, we disable skipping over zero inodes on wasip1.

Change-Id: I99aa562441cdb4182965f270af054cf3cf7f8f20
Reviewed-on: https://go-review.googlesource.com/c/go/+/507915
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
src/os/dir_darwin.go
src/os/dir_unix.go
src/syscall/dirent.go

index deba3eb37f10b920adcf9f6343da0432cb9ef6f9..e6d5bda24bdd490c2744c5cf5e4782fd72f1a709 100644 (file)
@@ -54,6 +54,15 @@ func (f *File) readdir(n int, mode readdirMode) (names []string, dirents []DirEn
                if entptr == nil { // EOF
                        break
                }
+               // Darwin may return a zero inode when a directory entry has been
+               // deleted but not yet removed from the directory. The man page for
+               // getdirentries(2) states that programs are responsible for skipping
+               // those entries:
+               //
+               //   Users of getdirentries() should skip entries with d_fileno = 0,
+               //   as such entries represent files which have been deleted but not
+               //   yet removed from the directory entry.
+               //
                if dirent.Ino == 0 {
                        continue
                }
index 004b9fbb2be6a776f8a8e6a0e404a6ebdd62aab2..266a78acafce5ec9d7ed06aa3f4e99f9f43ce62a 100644 (file)
@@ -89,7 +89,11 @@ func (f *File) readdir(n int, mode readdirMode) (names []string, dirents []DirEn
                if !ok {
                        break
                }
-               if ino == 0 {
+               // When building to wasip1, the host runtime might be running on Windows
+               // or might expose a remote file system which does not have the concept
+               // of inodes. Therefore, we cannot make the assumption that it is safe
+               // to skip entries with zero inodes.
+               if ino == 0 && runtime.GOOS != "wasip1" {
                        continue
                }
                const namoff = uint64(unsafe.Offsetof(syscall.Dirent{}.Name))
index eee94bf73c0c55670ca77066658abf5323419ab2..1a0f1eec11bf3dc6db993b552828c4a1f5c468e4 100644 (file)
@@ -6,7 +6,10 @@
 
 package syscall
 
-import "unsafe"
+import (
+       "runtime"
+       "unsafe"
+)
 
 // readInt returns the size-bytes unsigned integer in native byte order at offset off.
 func readInt(b []byte, off, size uintptr) (u uint64, ok bool) {
@@ -75,7 +78,9 @@ func ParseDirent(buf []byte, max int, names []string) (consumed int, count int,
                if !ok {
                        break
                }
-               if ino == 0 { // File absent in directory.
+               // See src/os/dir_unix.go for the reason why this condition is
+               // excluded on wasip1.
+               if ino == 0 && runtime.GOOS != "wasip1" { // File absent in directory.
                        continue
                }
                const namoff = uint64(unsafe.Offsetof(Dirent{}.Name))