fstatfs_zos.go 4.1 KB

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