-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstat.go
233 lines (208 loc) · 6.14 KB
/
stat.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
/*
Constants/functions for interpreting results of os.Stat() and os.Lstat().
Suggested usage: import github.com/goimp/stat
*/
package stat
// Indices for stat struct members in the tuple returned by os.Stat()
const (
ST_MODE = 0
ST_INO = 1
ST_DEV = 2
ST_NLINK = 3
ST_UID = 4
ST_GID = 5
ST_SIZE = 6
ST_ATIME = 7
ST_MTIME = 8
ST_CTIME = 9
)
// Extract bits from the mode
// Return the portion of the file's mode that can be set by os.chmod().
func S_IMODE(mode uint32) uint32 {
return mode & 0o7777
}
// Return the portion of the file's mode that describes the file type.
func S_IFMT(mode uint32) uint32 {
return mode & 0o170000
}
// Constants used as S_IFMT() for various file types
// (not all are implemented on all systems)
const (
S_IFDIR = 0o040000 // directory
S_IFCHR = 0o020000 // character device
S_IFBLK = 0o060000 // block device
S_IFREG = 0o100000 // regular file
S_IFIFO = 0o010000 // fifo (named pipe)
S_IFLNK = 0o120000 // symbolic link
S_IFSOCK = 0o140000 // socket file
// Fallbacks for uncommon platform-specific constants
S_IFDOOR = 0
S_IFPORT = 0
S_IFWHT = 0
)
// Functions to test for each file type
// Return True if mode is from a directory.
func S_ISDIR(mode uint32) bool {
return S_IFMT(mode) == S_IFDIR
}
// Return True if mode is from a character special device file.
func S_ISCHR(mode uint32) bool {
return S_IFMT(mode) == S_IFCHR
}
// Return True if mode is from a block special device file.
func S_ISBLK(mode uint32) bool {
return S_IFMT(mode) == S_IFBLK
}
// Return True if mode is from a regular file.
func S_ISREG(mode uint32) bool {
return S_IFMT(mode) == S_IFREG
}
// Return True if mode is from a FIFO (named pipe).
func S_ISFIFO(mode uint32) bool {
return S_IFMT(mode) == S_IFIFO
}
// Return True if mode is from a symbolic link.
func S_ISLNK(mode uint32) bool {
return S_IFMT(mode) == S_IFLNK
}
// Return True if mode is from a socket.
func S_ISSOCK(mode uint32) bool {
return S_IFMT(mode) == S_IFSOCK
}
// Return True if mode is from a door.
func S_ISDOOR(mode uint32) bool {
return false
}
// Return True if mode is from an event port.
func S_ISPORT(mode uint32) bool {
return false
}
// Return True if mode is from a whiteout.
func S_ISWHT(mode uint32) bool {
return false
}
// Names for permission bits
const (
S_ISUID = 0o4000 // set UID bit
S_ISGID = 0o2000 // set GID bit
S_ENFMT = S_ISGID // file locking enforcement
S_ISVTX = 0o1000 // sticky bit
S_IREAD = 0o0400 // Unix V7 synonym for S_IRUSR
S_IWRITE = 0o0200 // Unix V7 synonym for S_IWUSR
S_IEXEC = 0o0100 // Unix V7 synonym for S_IXUSR
S_IRWXU = 0o0700 // mask for owner permissions
S_IRUSR = 0o0400 // read by owner
S_IWUSR = 0o0200 // write by owner
S_IXUSR = 0o0100 // execute by owner
S_IRWXG = 0o0070 // mask for group permissions
S_IRGRP = 0o0040 // read by group
S_IWGRP = 0o0020 // write by group
S_IXGRP = 0o0010 // execute by group
S_IRWXO = 0o0007 // mask for others (not in group) permissions
S_IROTH = 0o0004 // read by others
S_IWOTH = 0o0002 // write by others
S_IXOTH = 0o0001 // execute by others
)
// Names for file flags
const (
UF_SETTABLE = 0x0000ffff // owner settable flags
UF_NODUMP = 0x00000001 // do not dump file
UF_IMMUTABLE = 0x00000002 // file may not be changed
UF_APPEND = 0x00000004 // file may only be appended to
UF_OPAQUE = 0x00000008 // directory is opaque when viewed through a union stack
UF_NOUNLINK = 0x00000010 // file may not be renamed or deleted
UF_COMPRESSED = 0x00000020 // macOS: file is compressed
UF_TRACKED = 0x00000040 // macOS: used for handling document IDs
UF_DATAVAULT = 0x00000080 // macOS: entitlement needed for I/O
UF_HIDDEN = 0x00008000 // macOS: file should not be displayed
SF_SETTABLE = 0xffff0000 // superuser settable flags
SF_ARCHIVED = 0x00010000 // file may be archived
SF_IMMUTABLE = 0x00020000 // file may not be changed
SF_APPEND = 0x00040000 // file may only be appended to
SF_RESTRICTED = 0x00080000 // macOS: entitlement needed for writing
SF_NOUNLINK = 0x00100000 // file may not be renamed or deleted
SF_SNAPSHOT = 0x00200000 // file is a snapshot file
SF_FIRMLINK = 0x00800000 // macOS: file is a firmlink
SF_DATALESS = 0x40000000 // macOS: file is a dataless object
)
// File type chars according to:
// http://en.wikibooks.org/wiki/C_Programming/POSIX_Reference/sys/stat.h
var fileModeTable = [][]struct {
Mask uint32
Symbol string
}{
{
{S_IFLNK, "l"},
{S_IFSOCK, "s"},
{S_IFREG, "-"},
{S_IFBLK, "b"},
{S_IFDIR, "d"},
{S_IFCHR, "c"},
{S_IFIFO, "p"},
},
{{S_IRUSR, "r"}},
{{S_IWUSR, "w"}},
{
{S_IXUSR | S_ISUID, "s"},
{S_ISUID, "S"},
{S_IXUSR, "x"},
},
{{S_IRGRP, "r"}},
{{S_IWGRP, "w"}},
{
{S_IXGRP | S_ISGID, "s"},
{S_ISGID, "S"},
{S_IXGRP, "x"},
},
{{S_IROTH, "r"}},
{{S_IWOTH, "w"}},
{
{S_IXOTH | S_ISVTX, "t"},
{S_ISVTX, "T"},
{S_IXOTH, "x"},
},
}
// Convert a file's mode to a string of the form '-rwxrwxrwx'.
func FileMode(mode uint32) string {
var perm []rune
for index, table := range fileModeTable {
found := false
for _, entry := range table {
if mode&entry.Mask == entry.Mask {
perm = append(perm, rune(entry.Symbol[0]))
found = true
break
}
}
if !found {
if index == 0 {
// Unknown file type
perm = append(perm, '?')
} else {
perm = append(perm, '-')
}
}
}
return string(perm)
}
// Windows FILE_ATTRIBUTE constants for interpreting os.Stat()'s
// "st_file_attributes" member
const (
FILE_ATTRIBUTE_ARCHIVE = 32
FILE_ATTRIBUTE_COMPRESSED = 2048
FILE_ATTRIBUTE_DEVICE = 64
FILE_ATTRIBUTE_DIRECTORY = 16
FILE_ATTRIBUTE_ENCRYPTED = 16384
FILE_ATTRIBUTE_HIDDEN = 2
FILE_ATTRIBUTE_INTEGRITY_STREAM = 32768
FILE_ATTRIBUTE_NORMAL = 128
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192
FILE_ATTRIBUTE_NO_SCRUB_DATA = 131072
FILE_ATTRIBUTE_OFFLINE = 4096
FILE_ATTRIBUTE_READONLY = 1
FILE_ATTRIBUTE_REPARSE_POINT = 1024
FILE_ATTRIBUTE_SPARSE_FILE = 512
FILE_ATTRIBUTE_SYSTEM = 4
FILE_ATTRIBUTE_TEMPORARY = 256
FILE_ATTRIBUTE_VIRTUAL = 65536
)