github.com/bir3/gocompiler@v0.9.2202/src/xvendor/golang.org/x/sys/unix/fstatfs_zos.go (about)

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