-
Notifications
You must be signed in to change notification settings - Fork 70
/
modeTypeWithType.go
37 lines (34 loc) · 1.03 KB
/
modeTypeWithType.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
// +build darwin dragonfly freebsd linux netbsd openbsd
package godirwalk
import (
"os"
"path/filepath"
"syscall"
)
// modeTypeFromDirent converts a syscall defined constant, which is in purview
// of OS, to a constant defined by Go, assumed by this project to be stable.
//
// When the syscall constant is not recognized, this function falls back to a
// Stat on the file system.
func modeTypeFromDirent(de *syscall.Dirent, osDirname, osBasename string) (os.FileMode, error) {
switch de.Type {
case syscall.DT_REG:
return 0, nil
case syscall.DT_DIR:
return os.ModeDir, nil
case syscall.DT_LNK:
return os.ModeSymlink, nil
case syscall.DT_CHR:
return os.ModeDevice | os.ModeCharDevice, nil
case syscall.DT_BLK:
return os.ModeDevice, nil
case syscall.DT_FIFO:
return os.ModeNamedPipe, nil
case syscall.DT_SOCK:
return os.ModeSocket, nil
default:
// If syscall returned unknown type (e.g., DT_UNKNOWN, DT_WHT), then
// resolve actual mode by reading file information.
return modeType(filepath.Join(osDirname, osBasename))
}
}