/
os-readdir_unix.go
249 lines (220 loc) · 6.38 KB
/
os-readdir_unix.go
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// +build linux,!appengine darwin freebsd netbsd openbsd
/*
* MinIO Cloud Storage, (C) 2016-2020 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cmd
import (
"bytes"
"fmt"
"os"
"sync"
"syscall"
"unsafe"
)
// The buffer must be at least a block long.
// refer https://github.com/golang/go/issues/24015
const blockSize = 8 << 10 // 8192
// By default atleast 128 entries in single getdents call (1MiB buffer)
var direntPool = sync.Pool{
New: func() interface{} {
buf := make([]byte, blockSize*128)
return &buf
},
}
// unexpectedFileMode is a sentinel (and bogus) os.FileMode
// value used to represent a syscall.DT_UNKNOWN Dirent.Type.
const unexpectedFileMode os.FileMode = os.ModeNamedPipe | os.ModeSocket | os.ModeDevice
func parseDirEnt(buf []byte) (consumed int, name []byte, typ os.FileMode, err error) {
// golang.org/issue/15653
dirent := (*syscall.Dirent)(unsafe.Pointer(&buf[0]))
if v := unsafe.Offsetof(dirent.Reclen) + unsafe.Sizeof(dirent.Reclen); uintptr(len(buf)) < v {
return consumed, nil, typ, fmt.Errorf("buf size of %d smaller than dirent header size %d", len(buf), v)
}
if len(buf) < int(dirent.Reclen) {
return consumed, nil, typ, fmt.Errorf("buf size %d < record length %d", len(buf), dirent.Reclen)
}
consumed = int(dirent.Reclen)
if direntInode(dirent) == 0 { // File absent in directory.
return
}
switch dirent.Type {
case syscall.DT_REG:
typ = 0
case syscall.DT_DIR:
typ = os.ModeDir
case syscall.DT_LNK:
typ = os.ModeSymlink
default:
// Skip all other file types. Revisit if/when this code needs
// to handle such files, MinIO is only interested in
// files and directories.
typ = unexpectedFileMode
}
nameBuf := (*[unsafe.Sizeof(dirent.Name)]byte)(unsafe.Pointer(&dirent.Name[0]))
nameLen, err := direntNamlen(dirent)
if err != nil {
return consumed, nil, typ, err
}
return consumed, nameBuf[:nameLen], typ, nil
}
// Return all the entries at the directory dirPath.
func readDir(dirPath string) (entries []string, err error) {
return readDirN(dirPath, -1)
}
// readDirFn applies the fn() function on each entries at dirPath, doesn't recurse into
// the directory itself, if the dirPath doesn't exist this function doesn't return
// an error.
func readDirFn(dirPath string, fn func(name string, typ os.FileMode) error) error {
f, err := os.Open(dirPath)
if err != nil {
if osErrToFileErr(err) == errFileNotFound {
return nil
}
return osErrToFileErr(err)
}
defer f.Close()
buf := make([]byte, blockSize)
boff := 0 // starting read position in buf
nbuf := 0 // end valid data in buf
for {
if boff >= nbuf {
boff = 0
nbuf, err = syscall.ReadDirent(int(f.Fd()), buf)
if err != nil {
if isSysErrNotDir(err) {
return nil
}
err = osErrToFileErr(err)
if err == errFileNotFound {
return nil
}
return err
}
if nbuf <= 0 {
break // EOF
}
}
consumed, name, typ, err := parseDirEnt(buf[boff:nbuf])
if err != nil {
return err
}
boff += consumed
if len(name) == 0 || bytes.Equal(name, []byte{'.'}) || bytes.Equal(name, []byte{'.', '.'}) {
continue
}
// Fallback for filesystems (like old XFS) that don't
// support Dirent.Type and have DT_UNKNOWN (0) there
// instead.
if typ == unexpectedFileMode || typ&os.ModeSymlink == os.ModeSymlink {
fi, err := os.Stat(pathJoin(dirPath, string(name)))
if err != nil {
// It got deleted in the meantime, not found
// or returns too many symlinks ignore this
// file/directory.
if osIsNotExist(err) || isSysErrPathNotFound(err) ||
isSysErrTooManySymlinks(err) {
continue
}
return err
}
// Ignore symlinked directories.
if typ&os.ModeSymlink == os.ModeSymlink && fi.IsDir() {
continue
}
typ = fi.Mode() & os.ModeType
}
if err = fn(string(name), typ); err == errDoneForNow {
// fn() requested to return by caller.
return nil
}
}
return err
}
// Return count entries at the directory dirPath and all entries
// if count is set to -1
func readDirN(dirPath string, count int) (entries []string, err error) {
f, err := os.Open(dirPath)
if err != nil {
return nil, osErrToFileErr(err)
}
defer f.Close()
bufp := direntPool.Get().(*[]byte)
defer direntPool.Put(bufp)
nameTmp := direntPool.Get().(*[]byte)
defer direntPool.Put(nameTmp)
tmp := *nameTmp
boff := 0 // starting read position in buf
nbuf := 0 // end valid data in buf
for count != 0 {
if boff >= nbuf {
boff = 0
nbuf, err = syscall.ReadDirent(int(f.Fd()), *bufp)
if err != nil {
if isSysErrNotDir(err) {
return nil, errFileNotFound
}
return nil, osErrToFileErr(err)
}
if nbuf <= 0 {
break
}
}
consumed, name, typ, err := parseDirEnt((*bufp)[boff:nbuf])
if err != nil {
return nil, err
}
boff += consumed
if len(name) == 0 || bytes.Equal(name, []byte{'.'}) || bytes.Equal(name, []byte{'.', '.'}) {
continue
}
// Fallback for filesystems (like old XFS) that don't
// support Dirent.Type and have DT_UNKNOWN (0) there
// instead.
if typ == unexpectedFileMode || typ&os.ModeSymlink == os.ModeSymlink {
fi, err := os.Stat(pathJoin(dirPath, string(name)))
if err != nil {
// It got deleted in the meantime, not found
// or returns too many symlinks ignore this
// file/directory.
if osIsNotExist(err) || isSysErrPathNotFound(err) ||
isSysErrTooManySymlinks(err) {
continue
}
return nil, err
}
// Ignore symlinked directories.
if typ&os.ModeSymlink == os.ModeSymlink && fi.IsDir() {
continue
}
typ = fi.Mode() & os.ModeType
}
var nameStr string
if typ.IsRegular() {
nameStr = string(name)
} else if typ.IsDir() {
// Use temp buffer to append a slash to avoid string concat.
tmp = tmp[:len(name)+1]
copy(tmp, name)
tmp[len(tmp)-1] = '/' // SlashSeparator
nameStr = string(tmp)
}
count--
entries = append(entries, nameStr)
}
return
}
func globalSync() {
syscall.Sync()
}