fstatfs_zos.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright 2020 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 zos && s390x
  5. package unix
  6. import (
  7. "unsafe"
  8. )
  9. // This file simulates fstatfs on z/OS using fstatvfs and w_getmntent.
  10. func Fstatfs(fd int, stat *Statfs_t) (err error) {
  11. var stat_v Statvfs_t
  12. err = Fstatvfs(fd, &stat_v)
  13. if err == nil {
  14. // populate stat
  15. stat.Type = 0
  16. stat.Bsize = stat_v.Bsize
  17. stat.Blocks = stat_v.Blocks
  18. stat.Bfree = stat_v.Bfree
  19. stat.Bavail = stat_v.Bavail
  20. stat.Files = stat_v.Files
  21. stat.Ffree = stat_v.Ffree
  22. stat.Fsid = stat_v.Fsid
  23. stat.Namelen = stat_v.Namemax
  24. stat.Frsize = stat_v.Frsize
  25. stat.Flags = stat_v.Flag
  26. for passn := 0; passn < 5; passn++ {
  27. switch passn {
  28. case 0:
  29. err = tryGetmntent64(stat)
  30. break
  31. case 1:
  32. err = tryGetmntent128(stat)
  33. break
  34. case 2:
  35. err = tryGetmntent256(stat)
  36. break
  37. case 3:
  38. err = tryGetmntent512(stat)
  39. break
  40. case 4:
  41. err = tryGetmntent1024(stat)
  42. break
  43. default:
  44. break
  45. }
  46. //proceed to return if: err is nil (found), err is nonnil but not ERANGE (another error occurred)
  47. if err == nil || err != nil && err != ERANGE {
  48. break
  49. }
  50. }
  51. }
  52. return err
  53. }
  54. func tryGetmntent64(stat *Statfs_t) (err error) {
  55. var mnt_ent_buffer struct {
  56. header W_Mnth
  57. filesys_info [64]W_Mntent
  58. }
  59. var buffer_size int = int(unsafe.Sizeof(mnt_ent_buffer))
  60. fs_count, err := W_Getmntent((*byte)(unsafe.Pointer(&mnt_ent_buffer)), buffer_size)
  61. if err != nil {
  62. return err
  63. }
  64. err = ERANGE //return ERANGE if no match is found in this batch
  65. for i := 0; i < fs_count; i++ {
  66. if stat.Fsid == uint64(mnt_ent_buffer.filesys_info[i].Dev) {
  67. stat.Type = uint32(mnt_ent_buffer.filesys_info[i].Fstname[0])
  68. err = nil
  69. break
  70. }
  71. }
  72. return err
  73. }
  74. func tryGetmntent128(stat *Statfs_t) (err error) {
  75. var mnt_ent_buffer struct {
  76. header W_Mnth
  77. filesys_info [128]W_Mntent
  78. }
  79. var buffer_size int = int(unsafe.Sizeof(mnt_ent_buffer))
  80. fs_count, err := W_Getmntent((*byte)(unsafe.Pointer(&mnt_ent_buffer)), buffer_size)
  81. if err != nil {
  82. return err
  83. }
  84. err = ERANGE //return ERANGE if no match is found in this batch
  85. for i := 0; i < fs_count; i++ {
  86. if stat.Fsid == uint64(mnt_ent_buffer.filesys_info[i].Dev) {
  87. stat.Type = uint32(mnt_ent_buffer.filesys_info[i].Fstname[0])
  88. err = nil
  89. break
  90. }
  91. }
  92. return err
  93. }
  94. func tryGetmntent256(stat *Statfs_t) (err error) {
  95. var mnt_ent_buffer struct {
  96. header W_Mnth
  97. filesys_info [256]W_Mntent
  98. }
  99. var buffer_size int = int(unsafe.Sizeof(mnt_ent_buffer))
  100. fs_count, err := W_Getmntent((*byte)(unsafe.Pointer(&mnt_ent_buffer)), buffer_size)
  101. if err != nil {
  102. return err
  103. }
  104. err = ERANGE //return ERANGE if no match is found in this batch
  105. for i := 0; i < fs_count; i++ {
  106. if stat.Fsid == uint64(mnt_ent_buffer.filesys_info[i].Dev) {
  107. stat.Type = uint32(mnt_ent_buffer.filesys_info[i].Fstname[0])
  108. err = nil
  109. break
  110. }
  111. }
  112. return err
  113. }
  114. func tryGetmntent512(stat *Statfs_t) (err error) {
  115. var mnt_ent_buffer struct {
  116. header W_Mnth
  117. filesys_info [512]W_Mntent
  118. }
  119. var buffer_size int = int(unsafe.Sizeof(mnt_ent_buffer))
  120. fs_count, err := W_Getmntent((*byte)(unsafe.Pointer(&mnt_ent_buffer)), buffer_size)
  121. if err != nil {
  122. return err
  123. }
  124. err = ERANGE //return ERANGE if no match is found in this batch
  125. for i := 0; i < fs_count; i++ {
  126. if stat.Fsid == uint64(mnt_ent_buffer.filesys_info[i].Dev) {
  127. stat.Type = uint32(mnt_ent_buffer.filesys_info[i].Fstname[0])
  128. err = nil
  129. break
  130. }
  131. }
  132. return err
  133. }
  134. func tryGetmntent1024(stat *Statfs_t) (err error) {
  135. var mnt_ent_buffer struct {
  136. header W_Mnth
  137. filesys_info [1024]W_Mntent
  138. }
  139. var buffer_size int = int(unsafe.Sizeof(mnt_ent_buffer))
  140. fs_count, err := W_Getmntent((*byte)(unsafe.Pointer(&mnt_ent_buffer)), buffer_size)
  141. if err != nil {
  142. return err
  143. }
  144. err = ERANGE //return ERANGE if no match is found in this batch
  145. for i := 0; i < fs_count; i++ {
  146. if stat.Fsid == uint64(mnt_ent_buffer.filesys_info[i].Dev) {
  147. stat.Type = uint32(mnt_ent_buffer.filesys_info[i].Fstname[0])
  148. err = nil
  149. break
  150. }
  151. }
  152. return err
  153. }