syscall_darwin.1_13.go 2.7 KB

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