-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.go
More file actions
238 lines (198 loc) · 4.88 KB
/
Copy pathstart.go
File metadata and controls
238 lines (198 loc) · 4.88 KB
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
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"godedupe/compare"
"godedupe/filter"
"godedupe/report"
)
var (
countDirs int
countFiles int
excludePatterns []string
parsedExcludePatterns []filter.Pattern
)
func updateDir() {
countDirs++
}
func updateFile() {
countFiles++
}
func expandHomeDir(fname string) (string, error) {
if fname == "~" {
return os.UserHomeDir()
}
if !strings.HasPrefix(fname, "~/") {
return fname, nil
}
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, fname[2:]), nil
}
func loadExcludePatterns(fname string) error {
expandedName, err := expandHomeDir(fname)
if err != nil {
return err
}
file, err := os.Open(expandedName)
if err != nil {
if fname == defaultExcludeFile {
// only shown a warning, probably the user doesn't want to use it
fmt.Printf("[?] Exclude patterns will not be used: %s\n", err)
return nil
}
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
excludePatterns = append(excludePatterns, scanner.Text())
}
return scanner.Err()
}
func isComparableFile(file compare.File) bool {
matched := true
if opt.pattern != "" {
matched, _ = filepath.Match(opt.pattern, file.Info.Name())
}
if !matched {
return false
}
if opt.excludeEmptyFiles && file.Info.Size() == 0 {
return false
}
if !opt.followSymlinks && file.Info.Mode()&os.ModeSymlink != 0 {
return false
}
return true
}
// readDir reads the files from the dir "s" recursively and checks if there are duplicated
func readDir(s string, depth int) *compare.Directory {
depth++
dir := &compare.Directory{Path: s}
files, err := os.ReadDir(s)
if err != nil {
fmt.Printf("[-] Error reading %s: %s\n", s, err)
return nil
}
if len(files) == 0 {
compare.AddDirectory(dir)
return dir
}
for _, f := range files {
if f.Name() == ".godedupe_ignore" {
return nil
}
}
for _, f := range files {
path := filepath.Join(s, f.Name())
if opt.excludeHidden && strings.HasPrefix(f.Name(), ".") {
continue
}
matched, err := filter.List(parsedExcludePatterns, path)
if err != nil {
fmt.Printf("[-] Error %s\n", err)
return nil
}
if matched {
continue
}
if !f.IsDir() {
info, err := f.Info()
if err != nil {
fmt.Printf("[-] Error reading %s: %s\n", path, err)
return nil
}
updateFile()
file := compare.File{
Path: path,
Info: info,
}
if isComparableFile(file) {
compare.AddFile(file)
dir.Files = append(dir.Files, file)
}
} else if opt.enableRecursion {
updateDir()
if depth < opt.maxDepth || opt.maxDepth == -1 {
childDir := readDir(path, depth)
if childDir != nil {
dir.Children = append(dir.Children, childDir)
}
}
} else {
updateDir()
}
if !opt.quiet {
fmt.Printf("[+] Analyzed: %v directories and %v files\r", countDirs, countFiles)
}
}
compare.AddDirectory(dir)
return dir
}
// Start the program with the targetDirs options. Options param is read only
func start() {
// Set the global variable so readDir function can access to the options
if len(opt.targetDirs) == 0 {
fmt.Println("error: directory must be specified. See help.")
return
}
err := loadExcludePatterns(opt.excludeFrom)
if err != nil {
fmt.Printf("[-] Error reading %s: %s\n", opt.excludeFrom, err)
return
}
parsedExcludePatterns = filter.ParsePatterns(excludePatterns)
for _, dir := range opt.targetDirs {
if info, err := os.Stat(dir); err == nil && !info.IsDir() && !opt.quiet {
// This should return an error to avoid hiding potential configuration errors
fmt.Printf("[-] %s is not a valid directory", info.Name())
return
}
}
for _, dir := range opt.targetDirs {
if !opt.quiet {
fmt.Println("[+] Reading directory:", dir)
}
if opt.pattern != "" {
_, err := filepath.Match(opt.pattern, "")
if err != nil {
fmt.Println("[-] Bad Ppattern: ", err)
return
}
}
readDir(dir, 0)
}
if !opt.quiet {
fmt.Printf("\n[+] Stage 1 / 3 completed\n")
}
reportOpts := report.Opts{
JsonFile: opt.JsonFile,
ShowSummary: opt.ShowSummary,
ShowNotification: opt.ShowNotification,
SameLine: opt.SameLine,
}
compare.ValidateDuplicatedDirectories(!opt.quiet)
compare.ValidateDuplicatedFiles(!opt.quiet)
dupFiles := compare.FilterFilesCoveredByDirectories(compare.DuplicatedFiles, compare.DuplicatedDirectoriesFound)
displayOpts := reportOpts
displayOpts.ShowSummary = false
dirReport := report.ObtainDirectoryReportData(compare.DuplicatedDirectoriesFound, displayOpts)
fileReport := report.ObtainReportData(dupFiles, displayOpts)
reported := false
if len(compare.DuplicatedDirectoriesFound) > 0 {
dirReport.DoReport()
reported = true
}
if len(dupFiles) > 0 || !reported {
fileReport.DoReport()
}
if reportOpts.ShowSummary {
fmt.Print(report.CombinedSummary(dirReport, fileReport))
}
}