syscall_darwin.1_13.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.
  4. // +build darwin,go1.13
  5. package unix
  6. import "unsafe"
  7. //sys closedir(dir uintptr) (err error)
  8. //sys readdir_r(dir uintptr, entry *Dirent, result **Dirent) (res Errno)
  9. func fdopendir(fd int) (dir uintptr, err error) {
  10. r0, _, e1 := syscall_syscallPtr(funcPC(libc_fdopendir_trampoline), uintptr(fd), 0, 0)
  11. dir = uintptr(r0)
  12. if e1 != 0 {
  13. err = errnoErr(e1)
  14. }
  15. return
  16. }
  17. func libc_fdopendir_trampoline()
  18. //go:linkname libc_fdopendir libc_fdopendir
  19. //go:cgo_import_dynamic libc_fdopendir fdopendir "/usr/lib/libSystem.B.dylib"
  20. func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) {
  21. // Simulate Getdirentries using fdopendir/readdir_r/closedir.
  22. // We store the number of entries to skip in the seek
  23. // offset of fd. See issue #31368.
  24. // It's not the full required semantics, but should handle the case
  25. // of calling Getdirentries or ReadDirent repeatedly.
  26. // It won't handle assigning the results of lseek to *basep, or handle
  27. // the directory being edited underfoot.
  28. skip, err := Seek(fd, 0, 1 /* SEEK_CUR */)
  29. if err != nil {
  30. return 0, err
  31. }
  32. // We need to duplicate the incoming file descriptor
  33. // because the caller expects to retain control of it, but
  34. // fdopendir expects to take control of its argument.
  35. // Just Dup'ing the file descriptor is not enough, as the
  36. // result shares underlying state. Use Openat to make a really
  37. // new file descriptor referring to the same directory.
  38. fd2, err := Openat(fd, ".", O_RDONLY, 0)
  39. if err != nil {
  40. return 0, err
  41. }
  42. d, err := fdopendir(fd2)
  43. if err != nil {
  44. Close(fd2)
  45. return 0, err
  46. }
  47. defer closedir(d)
  48. var cnt int64
  49. for {
  50. var entry Dirent
  51. var entryp *Dirent
  52. e := readdir_r(d, &entry, &entryp)
  53. if e != 0 {
  54. return n, errnoErr(e)
  55. }
  56. if entryp == nil {
  57. break
  58. }
  59. if skip > 0 {
  60. skip--
  61. cnt++
  62. continue
  63. }
  64. reclen := int(entry.Reclen)
  65. if reclen > len(buf) {
  66. // Not enough room. Return for now.
  67. // The counter will let us know where we should start up again.
  68. // Note: this strategy for suspending in the middle and
  69. // restarting is O(n^2) in the length of the directory. Oh well.
  70. break
  71. }
  72. // Copy entry into return buffer.
  73. s := struct {
  74. ptr unsafe.Pointer
  75. siz int
  76. cap int
  77. }{ptr: unsafe.Pointer(&entry), siz: reclen, cap: reclen}
  78. copy(buf, *(*[]byte)(unsafe.Pointer(&s)))
  79. buf = buf[reclen:]
  80. n += reclen
  81. cnt++
  82. }
  83. // Set the seek offset of the input fd to record
  84. // how many files we've already returned.
  85. _, err = Seek(fd, cnt, 0 /* SEEK_SET */)
  86. if err != nil {
  87. return n, err
  88. }
  89. return n, nil
  90. }