aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKeith Randall <keithr@alum.mit.edu>2019-04-05 13:01:58 -0700
committerKeith Randall <khr@golang.org>2019-04-05 20:40:01 +0000
commit06cff114cf786d5f901aa41ac873f9e8bb8e1eba (patch)
treedfe7f4758a98759736e75d1dc374af771839f861 /src
parent68d89bb8e05afc2aa050b4c5ad0df4b3af03c45d (diff)
downloadgo-06cff114cf786d5f901aa41ac873f9e8bb8e1eba.tar.xz
syscall: use openat instead of dup to make a really new file descriptor
Update #31269 Change-Id: I0e7184420055b8dfd23688dab9f9d8cba1fa2485 Reviewed-on: https://go-review.googlesource.com/c/go/+/170892 Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src')
-rw-r--r--src/syscall/getdirentries_test.go82
-rw-r--r--src/syscall/syscall_darwin.go8
2 files changed, 89 insertions, 1 deletions
diff --git a/src/syscall/getdirentries_test.go b/src/syscall/getdirentries_test.go
new file mode 100644
index 0000000000..b20ae1d1e3
--- /dev/null
+++ b/src/syscall/getdirentries_test.go
@@ -0,0 +1,82 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build darwin freebsd netbsd openbsd
+
+package syscall_test
+
+import (
+ "fmt"
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "sort"
+ "strings"
+ "syscall"
+ "testing"
+ "unsafe"
+)
+
+func TestGetdirentries(t *testing.T) {
+ for _, count := range []int{10, 1000} {
+ t.Run(fmt.Sprintf("n=%d", count), func(t *testing.T) {
+ testGetdirentries(t, count)
+ })
+ }
+}
+func testGetdirentries(t *testing.T, count int) {
+ d, err := ioutil.TempDir("", "getdirentries-test")
+ if err != nil {
+ t.Fatalf("Tempdir: %v", err)
+ }
+ defer os.RemoveAll(d)
+ var names []string
+ for i := 0; i < count; i++ {
+ names = append(names, fmt.Sprintf("file%03d", i))
+ }
+
+ // Make files in the temp directory
+ for _, name := range names {
+ err := ioutil.WriteFile(filepath.Join(d, name), []byte("data"), 0)
+ if err != nil {
+ t.Fatalf("WriteFile: %v", err)
+ }
+ }
+
+ // Read files using Getdirentries
+ var names2 []string
+ fd, err := syscall.Open(d, syscall.O_RDONLY, 0)
+ if err != nil {
+ t.Fatalf("Open: %v", err)
+ }
+ defer syscall.Close(fd)
+ var base uintptr
+ var buf [2048]byte
+ for {
+ n, err := syscall.Getdirentries(fd, buf[:], &base)
+ if err != nil {
+ t.Fatalf("Getdirentries: %v", err)
+ }
+ if n == 0 {
+ break
+ }
+ data := buf[:n]
+ for len(data) > 0 {
+ dirent := (*syscall.Dirent)(unsafe.Pointer(&data[0]))
+ data = data[dirent.Reclen:]
+ name := make([]byte, dirent.Namlen)
+ for i := 0; i < int(dirent.Namlen); i++ {
+ name[i] = byte(dirent.Name[i])
+ }
+ names2 = append(names2, string(name))
+ }
+ }
+
+ names = append(names, ".", "..") // Getdirentries returns these also
+ sort.Strings(names)
+ sort.Strings(names2)
+ if strings.Join(names, ":") != strings.Join(names2, ":") {
+ t.Errorf("names don't match\n names: %q\nnames2: %q", names, names2)
+ }
+}
diff --git a/src/syscall/syscall_darwin.go b/src/syscall/syscall_darwin.go
index 7ceceff2c1..422f3d4425 100644
--- a/src/syscall/syscall_darwin.go
+++ b/src/syscall/syscall_darwin.go
@@ -368,7 +368,13 @@ func writelen(fd int, buf *byte, nbuf int) (n int, err error) {
func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) {
// Simulate Getdirentries using fdopendir/readdir_r/closedir.
const ptrSize = unsafe.Sizeof(uintptr(0))
- fd2, err := Dup(fd)
+ // We need to duplicate the incoming file descriptor
+ // because the caller expects to retain control of it, but
+ // fdopendir expects to take control of its argument.
+ // Just Dup'ing the file descriptor is not enough, as the
+ // result shares underlying state. Use openat to make a really
+ // new file descriptor referring to the same directory.
+ fd2, err := openat(fd, ".", O_RDONLY, 0)
if err != nil {
return 0, err
}