1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // +build darwin,go1.13
12 "golang.org/x/sys/internal/unsafeheader"
15 //sys closedir(dir uintptr) (err error)
16 //sys readdir_r(dir uintptr, entry *Dirent, result **Dirent) (res Errno)
18 func fdopendir(fd int) (dir uintptr, err error) {
19 r0, _, e1 := syscall_syscallPtr(funcPC(libc_fdopendir_trampoline), uintptr(fd), 0, 0)
27 func libc_fdopendir_trampoline()
29 //go:cgo_import_dynamic libc_fdopendir fdopendir "/usr/lib/libSystem.B.dylib"
31 func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) {
32 // Simulate Getdirentries using fdopendir/readdir_r/closedir.
33 // We store the number of entries to skip in the seek
34 // offset of fd. See issue #31368.
35 // It's not the full required semantics, but should handle the case
36 // of calling Getdirentries or ReadDirent repeatedly.
37 // It won't handle assigning the results of lseek to *basep, or handle
38 // the directory being edited underfoot.
39 skip, err := Seek(fd, 0, 1 /* SEEK_CUR */)
44 // We need to duplicate the incoming file descriptor
45 // because the caller expects to retain control of it, but
46 // fdopendir expects to take control of its argument.
47 // Just Dup'ing the file descriptor is not enough, as the
48 // result shares underlying state. Use Openat to make a really
49 // new file descriptor referring to the same directory.
50 fd2, err := Openat(fd, ".", O_RDONLY, 0)
54 d, err := fdopendir(fd2)
65 e := readdir_r(d, &entry, &entryp)
78 reclen := int(entry.Reclen)
79 if reclen > len(buf) {
80 // Not enough room. Return for now.
81 // The counter will let us know where we should start up again.
82 // Note: this strategy for suspending in the middle and
83 // restarting is O(n^2) in the length of the directory. Oh well.
87 // Copy entry into return buffer.
89 hdr := (*unsafeheader.Slice)(unsafe.Pointer(&s))
90 hdr.Data = unsafe.Pointer(&entry)
99 // Set the seek offset of the input fd to record
100 // how many files we've already returned.
101 _, err = Seek(fd, cnt, 0 /* SEEK_SET */)