Query the system status
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

file_darwin.go 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package system
  2. import (
  3. "syscall"
  4. "unsafe"
  5. )
  6. // sys/mount.h
  7. const (
  8. mountWait = 1
  9. mountNoWait = 2
  10. mountDWait = 4
  11. )
  12. // Not supported.
  13. func scanInodesWithPIDs(fn func(Inode, PID) bool) {}
  14. func getFileSystems() <-chan FileSystem {
  15. ch := make(chan FileSystem)
  16. go func() {
  17. defer close(ch)
  18. n, err := getfsstat(nil, mountNoWait)
  19. if n < 0 || err != nil {
  20. return
  21. }
  22. b := make([]syscall.Statfs_t, n)
  23. n, err = getfsstat(b, mountNoWait)
  24. if n < 0 || err != nil {
  25. return
  26. }
  27. for i := 0; i < n; i++ {
  28. ch <- FileSystem{
  29. Mount: int8string(&b[i].Mntonname[0]),
  30. Device: int8string(&b[i].Mntfromname[0]),
  31. Type: int8string(&b[i].Fstypename[0]),
  32. SysType: int8string(&b[i].Fstypename[0]),
  33. }
  34. }
  35. }()
  36. return ch
  37. }
  38. // syscall.Getfsstat() wrapper is broken, roll our own to workaround.
  39. func getfsstat(buf []syscall.Statfs_t, flags int) (n int, err error) {
  40. var ptr uintptr
  41. var size uintptr
  42. if len(buf) > 0 {
  43. ptr = uintptr(unsafe.Pointer(&buf[0]))
  44. size = unsafe.Sizeof(buf[0]) * uintptr(len(buf))
  45. } else {
  46. ptr = uintptr(0)
  47. size = uintptr(0)
  48. }
  49. trap := uintptr(syscall.SYS_GETFSSTAT64)
  50. ret, _, errno := syscall.Syscall(trap, ptr, size, uintptr(flags))
  51. n = int(ret)
  52. if errno != 0 {
  53. err = errno
  54. }
  55. return
  56. }
  57. func int8string(p *int8) string {
  58. var (
  59. b = (*[128]byte)(unsafe.Pointer(p))
  60. i int
  61. c byte
  62. )
  63. for i, c = range b {
  64. if c == 0 {
  65. break
  66. }
  67. }
  68. return string(b[:i])
  69. }