-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsinfo_unix.go
186 lines (164 loc) · 3.64 KB
/
fsinfo_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
//go:build darwin || linux
package fsinfo
import (
"fmt"
"io/fs"
"syscall"
"time"
)
func newFileInfo(name string, mode fs.FileMode, modTime time.Time, size int64, sys any) *fileInfo {
info := &fileInfo{name: name}
if s, _ := sys.(*syscall.Stat_t); s != nil {
info.stat = *s
}
if mode != 0 {
setMode(&info.stat, makeMode(mode))
}
if !modTime.IsZero() {
setMtime(&info.stat, modTime.Unix(), int64(modTime.Nanosecond()))
}
if size >= 0 {
setSize(&info.stat, size)
}
return info
}
type fileInfo struct {
name string
stat syscall.Stat_t
}
func (info *fileInfo) String() string {
mode := info.Mode()
size := info.Size()
mtime := info.ModTime().Format("Jan _2 15:04")
nlink := statNlink(&info.stat)
return fmt.Sprintf("%s %d %d %s %s", mode, nlink, size, mtime, info.name)
}
func (info *fileInfo) Name() string {
return info.name
}
func (info *fileInfo) Mode() fs.FileMode {
mode := fs.FileMode(info.stat.Mode).Perm()
switch info.stat.Mode & syscall.S_IFMT {
case syscall.S_IFBLK:
mode |= fs.ModeDevice
case syscall.S_IFCHR:
mode |= fs.ModeDevice | fs.ModeCharDevice
case syscall.S_IFDIR:
mode |= fs.ModeDir
case syscall.S_IFIFO:
mode |= fs.ModeNamedPipe
case syscall.S_IFLNK:
mode |= fs.ModeSymlink
case syscall.S_IFSOCK:
mode |= fs.ModeSocket
}
if (info.stat.Mode & syscall.S_ISGID) != 0 {
mode |= fs.ModeSetgid
}
if (info.stat.Mode & syscall.S_ISUID) != 0 {
mode |= fs.ModeSetuid
}
if (info.stat.Mode & syscall.S_ISVTX) != 0 {
mode |= fs.ModeSticky
}
return mode
}
func (info *fileInfo) ModTime() time.Time {
return time.Unix(statMtime(&info.stat))
}
func (info *fileInfo) IsDir() bool {
return (info.stat.Mode & syscall.S_IFDIR) != 0
}
func (info *fileInfo) Size() int64 {
return info.stat.Size
}
func (info *fileInfo) Sys() any {
return &info.stat
}
func makeMode(fileMode fs.FileMode) (mode uint32) {
mode = uint32(fileMode.Perm())
switch fileMode.Type() {
case fs.ModeDevice:
mode |= syscall.S_IFBLK
case fs.ModeDevice | fs.ModeCharDevice:
mode |= syscall.S_IFCHR
case fs.ModeDir:
mode |= syscall.S_IFDIR
case fs.ModeNamedPipe:
mode |= syscall.S_IFIFO
case fs.ModeSymlink:
mode |= syscall.S_IFLNK
case fs.ModeSocket:
mode |= syscall.S_IFSOCK
default:
mode |= syscall.S_IFREG
}
if (fileMode & fs.ModeSetgid) != 0 {
mode |= syscall.S_ISGID
}
if (fileMode & fs.ModeSetuid) != 0 {
mode |= syscall.S_ISUID
}
if (fileMode & fs.ModeSticky) != 0 {
mode |= syscall.S_ISVTX
}
return mode
}
func mode(info fs.FileInfo) uint32 {
if stat := stat(info); stat != nil {
return statMode(stat)
}
return makeMode(info.Mode())
}
func uid(info fs.FileInfo) uint32 {
if stat := stat(info); stat != nil {
return statUid(stat)
}
return 0
}
func gid(info fs.FileInfo) uint32 {
if stat := stat(info); stat != nil {
return statGid(stat)
}
return 0
}
func ino(info fs.FileInfo) uint64 {
if stat := stat(info); stat != nil {
return statIno(stat)
}
return 0
}
func nlink(info fs.FileInfo) uint64 {
if stat := stat(info); stat != nil {
return statNlink(stat)
}
return 1
}
func device(info fs.FileInfo) uint64 {
if stat := stat(info); stat != nil {
return statDev(stat)
}
return 0
}
func mtime(info fs.FileInfo) time.Time {
if stat := stat(info); stat != nil {
return time.Unix(statMtime(stat))
}
return time.Time{}
}
func atime(info fs.FileInfo) time.Time {
if stat := stat(info); stat != nil {
return time.Unix(statAtime(stat))
}
return time.Time{}
}
func ctime(info fs.FileInfo) time.Time {
if stat := stat(info); stat != nil {
return time.Unix(statCtime(stat))
}
return time.Time{}
}
func stat(info fs.FileInfo) *syscall.Stat_t {
stat, _ := info.Sys().(*syscall.Stat_t)
return stat
}