-
Notifications
You must be signed in to change notification settings - Fork 0
/
dupefinder.go
134 lines (115 loc) · 2.94 KB
/
dupefinder.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
package main
import (
"bitbucket.org/alexthekone/dupefinder/format"
"crypto/md5"
"encoding/hex"
"fmt"
"gopkg.in/alecthomas/kingpin.v2"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
)
var (
app = kingpin.New("dupefinder", "Find duplicate files with checksums.")
verbose = app.Flag("verbose", "Enable verbose mode.").Short('v').Bool()
debug = app.Flag("debug", "Enable debug mode.").Short('d').Bool()
rec = app.Flag("recursive", "Search recursively.").Short('r').Bool()
output = app.Flag("output", "File to which the report will be written.").Short('o').Default("./dupefinder.csv").String()
target = app.Arg("target", "Where to look for duplicate files.").Default(".").String()
abs_path = ""
file_map = make(map[string][]byte)
)
func main() {
app.Version("1.1.0")
kingpin.MustParse(app.Parse(os.Args[1:]))
abs_path, _ = filepath.Abs(*target)
report_dest, report_file := filepath.Split(*output)
report_dest, _ = filepath.Abs(report_dest)
format.Print_debug(*verbose, debug, rec, report_dest, report_file, abs_path)
find_dupes()
output_map := reduce_duplicates(file_map)
csv := format.Pp_csv(output_map)
if *verbose {
fmt.Println(csv)
}
data := []byte(csv)
if len(output_map) > 0 {
err := ioutil.WriteFile(report_dest+"/"+report_file, data, 0644)
check(err)
} else {
fmt.Println("No duplicates found.")
os.Exit(1)
}
}
func find_dupes() {
if *rec == true {
find_dupes_rec(abs_path)
} else {
find_dupes_flat(abs_path)
}
}
func isDirectory(path string) (bool, error) {
fileInfo, err := os.Stat(path)
return fileInfo.IsDir(), err
}
func visit(path string, f os.FileInfo, err error) error {
isDir, _ := isDirectory(path)
if !isDir {
md5_hash, _ := ComputeMd5(path)
file_map[path] = md5_hash
}
return nil
}
func ComputeMd5(filePath string) ([]byte, error) {
var result []byte
file, err := os.Open(filePath)
if err != nil {
return result, err
}
defer file.Close()
hash := md5.New()
if _, err := io.Copy(hash, file); err != nil {
return result, err
}
return hash.Sum(result), nil
}
func reduce_duplicates(input_map map[string][]byte) map[string][]string {
output_map := make(map[string][]string)
for file_key, md5_hash := range input_map {
md5s := hex.EncodeToString(md5_hash)
output_map[md5s] = append(output_map[md5s], file_key)
}
for md5_key, file_array := range output_map {
if len(file_array) == 1 {
delete(output_map, md5_key)
}
}
for hash_key, file_refs := range output_map {
sort.Strings(file_refs)
output_map[hash_key] = file_refs
}
return output_map
}
func find_dupes_rec(abs_path string) {
err := filepath.Walk(abs_path, visit)
check(err)
}
func find_dupes_flat(abs_path string) {
files, err := ioutil.ReadDir(abs_path)
check(err)
for _, file := range files {
if !file.IsDir() {
fullpath := abs_path + "/" + file.Name()
md5_hash, _ := ComputeMd5(fullpath)
file_map[fullpath] = md5_hash
}
}
}
func check(e error) {
if e != nil {
log.Fatal(e)
}
}