aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/golang.org/x/sys/unix/fstatfs_zos.go
blob: c8bde601e772336736ec494940f5fc326c41dd2d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build zos && s390x

package unix

import (
	"unsafe"
)

// This file simulates fstatfs on z/OS using fstatvfs and w_getmntent.

func Fstatfs(fd int, stat *Statfs_t) (err error) {
	var stat_v Statvfs_t
	err = Fstatvfs(fd, &stat_v)
	if err == nil {
		// populate stat
		stat.Type = 0
		stat.Bsize = stat_v.Bsize
		stat.Blocks = stat_v.Blocks
		stat.Bfree = stat_v.Bfree
		stat.Bavail = stat_v.Bavail
		stat.Files = stat_v.Files
		stat.Ffree = stat_v.Ffree
		stat.Fsid = stat_v.Fsid
		stat.Namelen = stat_v.Namemax
		stat.Frsize = stat_v.Frsize
		stat.Flags = stat_v.Flag
		for passn := 0; passn < 5; passn++ {
			switch passn {
			case 0:
				err = tryGetmntent64(stat)
				break
			case 1:
				err = tryGetmntent128(stat)
				break
			case 2:
				err = tryGetmntent256(stat)
				break
			case 3:
				err = tryGetmntent512(stat)
				break
			case 4:
				err = tryGetmntent1024(stat)
				break
			default:
				break
			}
			//proceed to return if: err is nil (found), err is nonnil but not ERANGE (another error occurred)
			if err == nil || err != nil && err != ERANGE {
				break
			}
		}
	}
	return err
}

func tryGetmntent64(stat *Statfs_t) (err error) {
	var mnt_ent_buffer struct {
		header       W_Mnth
		filesys_info [64]W_Mntent
	}
	var buffer_size int = int(unsafe.Sizeof(mnt_ent_buffer))
	fs_count, err := W_Getmntent((*byte)(unsafe.Pointer(&mnt_ent_buffer)), buffer_size)
	if err != nil {
		return err
	}
	err = ERANGE //return ERANGE if no match is found in this batch
	for i := 0; i < fs_count; i++ {
		if stat.Fsid == uint64(mnt_ent_buffer.filesys_info[i].Dev) {
			stat.Type = uint32(mnt_ent_buffer.filesys_info[i].Fstname[0])
			err = nil
			break
		}
	}
	return err
}

func tryGetmntent128(stat *Statfs_t) (err error) {
	var mnt_ent_buffer struct {
		header       W_Mnth
		filesys_info [128]W_Mntent
	}
	var buffer_size int = int(unsafe.Sizeof(mnt_ent_buffer))
	fs_count, err := W_Getmntent((*byte)(unsafe.Pointer(&mnt_ent_buffer)), buffer_size)
	if err != nil {
		return err
	}
	err = ERANGE //return ERANGE if no match is found in this batch
	for i := 0; i < fs_count; i++ {
		if stat.Fsid == uint64(mnt_ent_buffer.filesys_info[i].Dev) {
			stat.Type = uint32(mnt_ent_buffer.filesys_info[i].Fstname[0])
			err = nil
			break
		}
	}
	return err
}

func tryGetmntent256(stat *Statfs_t) (err error) {
	var mnt_ent_buffer struct {
		header       W_Mnth
		filesys_info [256]W_Mntent
	}
	var buffer_size int = int(unsafe.Sizeof(mnt_ent_buffer))
	fs_count, err := W_Getmntent((*byte)(unsafe.Pointer(&mnt_ent_buffer)), buffer_size)
	if err != nil {
		return err
	}
	err = ERANGE //return ERANGE if no match is found in this batch
	for i := 0; i < fs_count; i++ {
		if stat.Fsid == uint64(mnt_ent_buffer.filesys_info[i].Dev) {
			stat.Type = uint32(mnt_ent_buffer.filesys_info[i].Fstname[0])
			err = nil
			break
		}
	}
	return err
}

func tryGetmntent512(stat *Statfs_t) (err error) {
	var mnt_ent_buffer struct {
		header       W_Mnth
		filesys_info [512]W_Mntent
	}
	var buffer_size int = int(unsafe.Sizeof(mnt_ent_buffer))
	fs_count, err := W_Getmntent((*byte)(unsafe.Pointer(&mnt_ent_buffer)), buffer_size)
	if err != nil {
		return err
	}
	err = ERANGE //return ERANGE if no match is found in this batch
	for i := 0; i < fs_count; i++ {
		if stat.Fsid == uint64(mnt_ent_buffer.filesys_info[i].Dev) {
			stat.Type = uint32(mnt_ent_buffer.filesys_info[i].Fstname[0])
			err = nil
			break
		}
	}
	return err
}

func tryGetmntent1024(stat *Statfs_t) (err error) {
	var mnt_ent_buffer struct {
		header       W_Mnth
		filesys_info [1024]W_Mntent
	}
	var buffer_size int = int(unsafe.Sizeof(mnt_ent_buffer))
	fs_count, err := W_Getmntent((*byte)(unsafe.Pointer(&mnt_ent_buffer)), buffer_size)
	if err != nil {
		return err
	}
	err = ERANGE //return ERANGE if no match is found in this batch
	for i := 0; i < fs_count; i++ {
		if stat.Fsid == uint64(mnt_ent_buffer.filesys_info[i].Dev) {
			stat.Type = uint32(mnt_ent_buffer.filesys_info[i].Fstname[0])
			err = nil
			break
		}
	}
	return err
}