-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.go
More file actions
163 lines (137 loc) · 3.33 KB
/
start.go
File metadata and controls
163 lines (137 loc) · 3.33 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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"godedupe/compare"
"godedupe/report"
"godedupe/filter"
)
var (
countDirs int
countFiles int
excludePatterns []string
)
func update(f os.FileInfo) {
if f.IsDir() {
countDirs++
} else {
countFiles++
}
}
func loadExcludePatterns(fname string) error {
file, err := os.Open(fname)
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()
}
// readDir reads the files from the dir "s" recursively and checks if there are duplicated
func readDir(s string, depth int) {
depth++
files, err := ioutil.ReadDir(s)
if err != nil {
fmt.Printf("[-] Error reading %s: %s\n", s, err)
return
}
if len(files) == 0 {
return
}
for _, f := range files {
if f.Name() == ".godedupe_ignore" {
return
}
}
for _, f := range files {
path := filepath.Join(s, f.Name())
file := compare.File{
path,
f,
}
if opt.excludeHidden && strings.HasPrefix(f.Name(), ".") {
continue
}
p := filter.ParsePatterns(excludePatterns)
matched, err := filter.List(p, path)
if err != nil {
fmt.Printf("[-] Error %s\n", err)
return
}
if matched {
continue
}
update(file.Info)
if !file.Info.IsDir() {
// Only scan for files of a given extension
matched := true
if opt.pattern != "" {
matched, _ = filepath.Match(opt.pattern, file.Info.Name())
}
if !matched {
} else if opt.excludeEmptyFiles && file.Info.Size() == 0 {
} else if !opt.followSymlinks && file.Info.Mode()&os.ModeSymlink != 0 {
} else {
compare.AddFile(file)
}
} else if opt.enableRecursion {
if depth < opt.maxDepth || opt.maxDepth == -1 {
readDir(path, depth)
}
}
if !opt.quiet {
fmt.Printf("[+] Analyzed: %v directories and %v files\r", countDirs, countFiles)
}
}
}
// 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
}
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")
}
compare.ValidateDuplicatedFiles(!opt.quiet)
reportOpts := report.Opts{opt.JsonFile, opt.ShowSummary, opt.ShowNotification, opt.SameLine}
report.ObtainReportData(compare.DuplicatedFiles, reportOpts).DoReport()
}