/
list.go
416 lines (325 loc) · 9.7 KB
/
list.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//go:build !windows
// +build !windows
package fsutil
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
PATH "path"
"syscall"
)
// ////////////////////////////////////////////////////////////////////////////////// //
// ListingFilter is struct with properties for filtering listing output
type ListingFilter struct {
MatchPatterns []string // Slice with shell file name patterns
NotMatchPatterns []string // Slice with shell file name patterns
ATimeOlder int64 // Files with ATime less or equal to defined timestamp (BEFORE date)
ATimeYounger int64 // Files with ATime greater or equal to defined timestamp (AFTER date)
CTimeOlder int64 // Files with CTime less or equal to defined timestamp (BEFORE date)
CTimeYounger int64 // Files with CTime greater or equal to defined timestamp (AFTER date)
MTimeOlder int64 // Files with MTime less or equal to defined timestamp (BEFORE date)
MTimeYounger int64 // Files with MTime greater or equal to defined timestamp (AFTER date)
SizeLess int64 // Files with size less than defined
SizeGreater int64 // Files with size greater than defined
SizeEqual int64 // Files with size equals to defined
SizeZero bool // Empty files
Perms string // Permission (see fsutil.CheckPerms for more info)
NotPerms string // Permission (see fsutil.CheckPerms for more info)
}
// ////////////////////////////////////////////////////////////////////////////////// //
func (lf ListingFilter) hasMatchPatterns() bool {
return len(lf.MatchPatterns) != 0
}
func (lf ListingFilter) hasNotMatchPatterns() bool {
return len(lf.NotMatchPatterns) != 0
}
func (lf ListingFilter) hasTimes() bool {
switch {
case lf.ATimeOlder != 0,
lf.ATimeOlder != 0,
lf.ATimeYounger != 0,
lf.CTimeOlder != 0,
lf.CTimeYounger != 0,
lf.MTimeOlder != 0,
lf.MTimeYounger != 0:
return true
}
return false
}
func (lf ListingFilter) hasPerms() bool {
return lf.Perms != "" || lf.NotPerms != ""
}
func (lf ListingFilter) hasSize() bool {
return lf.SizeZero || lf.SizeGreater > 0 || lf.SizeLess > 0 || lf.SizeEqual > 0
}
// ////////////////////////////////////////////////////////////////////////////////// //
// List is lightweight method for listing directory
func List(dir string, ignoreHidden bool, filters ...ListingFilter) []string {
var names = readDir(dir)
if ignoreHidden {
names = filterHidden(names)
}
if len(filters) != 0 {
names = filterList(names, dir, filters[0])
}
return names
}
// ListAll is lightweight method for listing all files and directories
func ListAll(dir string, ignoreHidden bool, filters ...ListingFilter) []string {
if len(filters) == 0 {
return readDirRecAll(dir, "", ignoreHidden, ListingFilter{})
}
return readDirRecAll(dir, "", ignoreHidden, filters[0])
}
// ListAllDirs is lightweight method for listing all directories
func ListAllDirs(dir string, ignoreHidden bool, filters ...ListingFilter) []string {
if len(filters) == 0 {
return readDirRecDirs(dir, "", ignoreHidden, ListingFilter{})
}
return readDirRecDirs(dir, "", ignoreHidden, filters[0])
}
// ListAllFiles is lightweight method for listing all files
func ListAllFiles(dir string, ignoreHidden bool, filters ...ListingFilter) []string {
if len(filters) == 0 {
return readDirRecFiles(dir, "", ignoreHidden, ListingFilter{})
}
return readDirRecFiles(dir, "", ignoreHidden, filters[0])
}
// ListToAbsolute converts slice with relative paths to slice with absolute paths
func ListToAbsolute(path string, list []string) {
for i, t := range list {
list[i] = path + "/" + t
}
}
// ////////////////////////////////////////////////////////////////////////////////// //
func readDir(dir string) []string {
fd, err := syscall.Open(dir, syscall.O_CLOEXEC, 0644)
if err != nil {
return nil
}
defer syscall.Close(fd)
var size = 100
var n = -1
var nbuf int
var bufp int
var buf = make([]byte, 4096)
var names = make([]string, 0, size)
for n != 0 {
if bufp >= nbuf {
bufp = 0
var errno error
nbuf, errno = fixCount(syscall.ReadDirent(fd, buf))
if errno != nil {
return names
}
if nbuf <= 0 {
break
}
}
var nb, nc int
nb, nc, names = syscall.ParseDirent(buf[bufp:nbuf], n, names)
bufp += nb
n -= nc
}
return names
}
func readDirRecAll(path, base string, ignoreHidden bool, filter ListingFilter) []string {
var result []string
names := readDir(path)
for _, name := range names {
if name[0] == '.' && ignoreHidden {
continue
}
if !IsDir(path + "/" + name) {
if base == "" {
if isMatch(name, path+"/"+name, filter) {
result = append(result, name)
}
} else {
if isMatch(name, path+"/"+name, filter) {
result = append(result, base+"/"+name)
}
}
} else {
if base == "" {
if isMatch(name, path+"/"+name, filter) {
result = append(result, name)
result = append(result, readDirRecAll(path+"/"+name, name, ignoreHidden, filter)...)
}
} else {
if isMatch(name, path+"/"+name, filter) {
result = append(result, base+"/"+name)
result = append(result, readDirRecAll(path+"/"+name, base+"/"+name, ignoreHidden, filter)...)
}
}
}
}
return result
}
func readDirRecDirs(path, base string, ignoreHidden bool, filter ListingFilter) []string {
var result []string
names := readDir(path)
for _, name := range names {
if name[0] == '.' && ignoreHidden {
continue
}
if IsDir(path + "/" + name) {
if base == "" {
if isMatch(name, path+"/"+name, filter) {
result = append(result, name)
result = append(result, readDirRecDirs(path+"/"+name, name, ignoreHidden, filter)...)
}
} else {
if isMatch(name, path+"/"+name, filter) {
result = append(result, base+"/"+name)
result = append(result, readDirRecDirs(path+"/"+name, base+"/"+name, ignoreHidden, filter)...)
}
}
}
}
return result
}
func readDirRecFiles(path, base string, ignoreHidden bool, filter ListingFilter) []string {
var result []string
names := readDir(path)
for _, name := range names {
if name[0] == '.' && ignoreHidden {
continue
}
if IsDir(path + "/" + name) {
if base == "" {
result = append(result, readDirRecFiles(path+"/"+name, name, ignoreHidden, filter)...)
} else {
result = append(result, readDirRecFiles(path+"/"+name, base+"/"+name, ignoreHidden, filter)...)
}
} else {
if base == "" {
if isMatch(name, path+"/"+name, filter) {
result = append(result, name)
}
} else {
if isMatch(name, path+"/"+name, filter) {
result = append(result, base+"/"+name)
}
}
}
}
return result
}
// It's ok to have long function with many conditions to filter some entities
// codebeat:disable[LOC,ABC,CYCLO]
func isMatch(name, fullPath string, filter ListingFilter) bool {
var (
hasNotMatchPatterns = filter.hasNotMatchPatterns()
hasMatchPatterns = filter.hasMatchPatterns()
hasTimes = filter.hasTimes()
hasPerms = filter.hasPerms()
hasSize = filter.hasSize()
)
if !hasNotMatchPatterns && !hasMatchPatterns && !hasTimes && !hasPerms && !hasSize {
return true
}
var match = true
if hasNotMatchPatterns {
for _, pattern := range filter.NotMatchPatterns {
matched, _ := PATH.Match(pattern, name)
if matched {
match = false
break
}
}
}
if hasMatchPatterns {
for _, pattern := range filter.MatchPatterns {
matched, _ := PATH.Match(pattern, name)
if matched {
match = true
break
}
match = false
}
}
if !hasTimes && !hasPerms && !hasSize {
return match
}
if hasTimes {
atime, mtime, ctime, err := GetTimestamps(fullPath)
if err != nil {
return match
}
if filter.MTimeYounger != 0 {
match = match && mtime >= filter.MTimeYounger
}
if filter.MTimeOlder != 0 {
match = match && mtime <= filter.MTimeOlder
}
if filter.CTimeYounger != 0 {
match = match && ctime >= filter.CTimeYounger
}
if filter.CTimeOlder != 0 {
match = match && ctime <= filter.CTimeOlder
}
if filter.ATimeYounger != 0 {
match = match && atime >= filter.ATimeYounger
}
if filter.ATimeOlder != 0 {
match = match && atime <= filter.ATimeOlder
}
}
if !hasPerms && !hasSize {
return match
}
if hasPerms {
if filter.Perms != "" {
match = match && CheckPerms(filter.Perms, fullPath)
}
if filter.NotPerms != "" {
match = match && !CheckPerms(filter.NotPerms, fullPath)
}
}
if hasSize {
if filter.SizeZero {
match = match && GetSize(fullPath) == 0
} else {
if filter.SizeEqual > 0 {
match = match && GetSize(fullPath) == filter.SizeEqual
}
if filter.SizeGreater > 0 {
match = match && GetSize(fullPath) > filter.SizeGreater
}
if filter.SizeLess > 0 {
match = match && GetSize(fullPath) < filter.SizeLess
}
}
}
return match
}
// codebeat:enable[LOC,ABC,CYCLO]
func filterList(names []string, dir string, filter ListingFilter) []string {
var filteredNames []string
for _, name := range names {
if isMatch(name, dir+"/"+name, filter) {
filteredNames = append(filteredNames, name)
}
}
return filteredNames
}
func filterHidden(names []string) []string {
var filteredNames []string
for _, name := range names {
if name[0] == '.' {
continue
}
filteredNames = append(filteredNames, name)
}
return filteredNames
}
func fixCount(n int, err error) (int, error) {
if n < 0 {
n = 0
}
return n, err
}