syscall_darwin.1_13.go 2.7 KB

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