syscall_darwin.1_13.go 2.7 KB

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