-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessDir.go
89 lines (71 loc) · 1.64 KB
/
processDir.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
package main
import (
"io/fs"
"log"
"os"
"path/filepath"
"github.com/vitali-fedulov/images4"
)
func processDir(path string) {
if dirThreaded {
defer limit.Remove()
}
ds, err := os.ReadDir(path)
if err != nil {
log.Fatalf("error occured walking specified director%s, error:\n%s", func() string {
if options.Walk {
return "ies"
}
return "y"
}(), err.Error())
}
var (
dirProcessor func(d fs.DirEntry)
deferfunc = func() {}
)
// empty directory
if entries := len(ds); entries == 0 {
totalReads.Add(1)
if options.EmptyDir {
logToFile.Printf("[empty directory] at %s", path)
deleteDirEntry(path)
}
return
} else
// without caring for duplicates
if !options.Duplicates || entries < 2 {
dirProcessor = func(d fs.DirEntry) { dirEntry(path, d) }
} else
// with duplicates check
{
var results []dirEntryResponse
dirProcessor = func(d fs.DirEntry) {
results = append(results, dirEntryResponse{filename: fileName(path, d), icon: dirEntryWithResult(path, d)})
}
deferfunc = func() { matchDuplicates(results) }
}
defer deferfunc()
for _, d := range ds {
totalReads.Add(1)
dirProcessor(d)
}
}
func dirEntry(path string, d fs.DirEntry) {
if err := processDirEntry(fileName(path, d), d, nil); err != nil {
log.Fatal(err)
}
}
func dirEntryWithResult(path string, d fs.DirEntry) images4.IconT {
var result images4.IconT
if err := processDirEntry(fileName(path, d), d, &result); err != nil {
log.Fatal(err)
}
return result
}
func fileName(path string, d fs.DirEntry) string {
return filepath.Join(path, d.Name())
}
type dirEntryResponse struct {
filename string
icon images4.IconT
}