-
Notifications
You must be signed in to change notification settings - Fork 6
/
fastwalk_darwin.go
122 lines (115 loc) · 2.56 KB
/
fastwalk_darwin.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
//go:build darwin && go1.13
package fastwalk
import (
"os"
"syscall"
"unsafe"
)
func (w *walker) readDir(dirName string) (err error) {
var fd uintptr
for {
fd, err = opendir(dirName)
if err != syscall.EINTR {
break
}
}
if err != nil {
return &os.PathError{Op: "opendir", Path: dirName, Err: err}
}
defer closedir(fd) //nolint:errcheck
var p *[]*unixDirent
if w.sortMode != SortNone {
p = direntSlicePool.Get().(*[]*unixDirent)
}
defer putDirentSlice(p)
skipFiles := false
var dirent syscall.Dirent
var entptr *syscall.Dirent
for {
if errno := readdir_r(fd, &dirent, &entptr); errno != 0 {
if errno == syscall.EINTR {
continue
}
return &os.PathError{Op: "readdir", Path: dirName, Err: errno}
}
if entptr == nil { // EOF
break
}
// Darwin may return a zero inode when a directory entry has been
// deleted but not yet removed from the directory. The man page for
// getdirentries(2) states that programs are responsible for skipping
// those entries:
//
// Users of getdirentries() should skip entries with d_fileno = 0,
// as such entries represent files which have been deleted but not
// yet removed from the directory entry.
//
if dirent.Ino == 0 {
continue
}
typ := dtToType(dirent.Type)
if skipFiles && typ.IsRegular() {
continue
}
name := (*[len(syscall.Dirent{}.Name)]byte)(unsafe.Pointer(&dirent.Name))[:]
for i, c := range name {
if c == 0 {
name = name[:i]
break
}
}
// Check for useless names before allocating a string.
if string(name) == "." || string(name) == ".." {
continue
}
nm := string(name)
de := newUnixDirent(dirName, nm, typ)
if w.sortMode == SortNone {
if err := w.onDirEnt(dirName, nm, de); err != nil {
if err != ErrSkipFiles {
return err
}
skipFiles = true
}
} else {
*p = append(*p, de)
}
}
if w.sortMode == SortNone {
return nil
}
dents := *p
sortDirents(w.sortMode, dents)
for _, d := range dents {
d := d
if skipFiles && d.typ.IsRegular() {
continue
}
if err := w.onDirEnt(dirName, d.Name(), d); err != nil {
if err != ErrSkipFiles {
return err
}
skipFiles = true
}
}
return nil
}
func dtToType(typ uint8) os.FileMode {
switch typ {
case syscall.DT_BLK:
return os.ModeDevice
case syscall.DT_CHR:
return os.ModeDevice | os.ModeCharDevice
case syscall.DT_DIR:
return os.ModeDir
case syscall.DT_FIFO:
return os.ModeNamedPipe
case syscall.DT_LNK:
return os.ModeSymlink
case syscall.DT_REG:
return 0
case syscall.DT_SOCK:
return os.ModeSocket
}
return ^os.FileMode(0)
}