-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathpathconf_linux.go
More file actions
178 lines (158 loc) · 3.82 KB
/
Copy pathpathconf_linux.go
File metadata and controls
178 lines (158 loc) · 3.82 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2026 Tobias Klauser. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sysconf
import (
"errors"
"golang.org/x/sys/unix"
)
const (
linuxLinkMax = 127
linuxLongLinkMax = 65000
linuxSymlinks = 1
linuxFileSizeBits = 64
linuxRecXferAlign = 4096
linuxRecMinXfer = 4096
linuxAllocSizeMin = 4096
)
func pathconfConst(name int) (int64, error) {
switch name {
case PC_PATH_MAX:
return _PATH_MAX, nil
case PC_MAX_CANON:
return _MAX_CANON, nil
case PC_MAX_INPUT:
return _MAX_INPUT, nil
case PC_PIPE_BUF:
return _PIPE_BUF, nil
case PC_CHOWN_RESTRICTED:
// XXX: why is _POSIX_CHOWN_RESTRICTED not 1?
return 1, nil
case PC_NO_TRUNC:
return _POSIX_NO_TRUNC, nil
case PC_VDISABLE:
return _POSIX_VDISABLE, nil
case PC_SYNC_IO,
PC_PRIO_IO,
PC_REC_INCR_XFER_SIZE,
PC_REC_MAX_XFER_SIZE,
PC_SOCK_MAXBUF,
PC_SYMLINK_MAX:
return -1, nil
case PC_FILESIZEBITS:
return linuxFileSizeBits, nil
case PC_2_SYMLINKS:
return linuxSymlinks, nil
}
return -1, unix.EINVAL
}
func isReg(mode uint32) bool { return mode&unix.S_IFMT == unix.S_IFREG }
func isBlk(mode uint32) bool { return mode&unix.S_IFMT == unix.S_IFBLK }
func pathconf(path string, name int) (int64, error) {
if val, err := pathconfConst(name); err == nil {
return val, nil
}
switch name {
case PC_ASYNC_IO:
var st unix.Stat_t
if err := unix.Stat(path, &st); err != nil {
return -1, err
}
if isReg(st.Mode) || isBlk(st.Mode) {
return 1, nil
}
return -1, nil
}
statfs := func() (*unix.Statfs_t, error) {
var st unix.Statfs_t
if err := unix.Statfs(path, &st); err != nil {
return nil, err
}
return &st, nil
}
return pathconfStatfs(name, statfs)
}
func fpathconf(fd int, name int) (int64, error) {
if val, err := pathconfConst(name); err == nil {
return val, nil
}
switch name {
case PC_ASYNC_IO:
var st unix.Stat_t
if err := unix.Fstat(fd, &st); err != nil {
return -1, err
}
if isReg(st.Mode) || isBlk(st.Mode) {
return 1, nil
}
return -1, nil
}
statfs := func() (*unix.Statfs_t, error) {
var st unix.Statfs_t
if err := unix.Fstatfs(fd, &st); err != nil {
return nil, err
}
return &st, nil
}
return pathconfStatfs(name, statfs)
}
type statfsFn func() (*unix.Statfs_t, error)
func (statfs statfsFn) ValueWithFallback(value valueFn, fallback int64) (int64, error) {
st, err := statfs()
if err != nil {
if errors.Is(err, unix.ENOSYS) {
return fallback, nil
}
return -1, err
}
return value(st)
}
func (statfs statfsFn) Value(value valueFn) (int64, error) {
st, err := statfs()
if err != nil {
return -1, err
}
return value(st)
}
type valueFn func(*unix.Statfs_t) (int64, error)
func pathconfStatfs(name int, statfs statfsFn) (int64, error) {
switch name {
case PC_LINK_MAX:
return statfs.ValueWithFallback(func(st *unix.Statfs_t) (int64, error) {
switch uint32(st.Type) {
case unix.BTRFS_SUPER_MAGIC,
unix.EXT4_SUPER_MAGIC,
unix.OVERLAYFS_SUPER_MAGIC,
unix.XFS_SUPER_MAGIC:
return linuxLongLinkMax, nil
}
return linuxLinkMax, nil
}, linuxLinkMax)
case PC_NAME_MAX:
return statfs.ValueWithFallback(func(st *unix.Statfs_t) (int64, error) {
return int64(st.Namelen), nil
}, _NAME_MAX)
case PC_REC_MIN_XFER_SIZE:
return statfs.Value(func(st *unix.Statfs_t) (int64, error) {
if st.Bsize > 0 {
return int64(st.Bsize), nil
}
return linuxRecMinXfer, nil
})
case PC_REC_XFER_ALIGN:
return statfs.Value(func(st *unix.Statfs_t) (int64, error) {
if st.Frsize > 0 {
return int64(st.Frsize), nil
}
return linuxRecXferAlign, nil
})
case PC_ALLOC_SIZE_MIN:
return statfs.Value(func(st *unix.Statfs_t) (int64, error) {
if st.Frsize > 0 {
return int64(st.Frsize), nil
}
return linuxAllocSizeMin, nil
})
}
return -1, unix.EINVAL
}