-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
242 lines (212 loc) · 4.63 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"os/signal"
"path/filepath"
"sort"
"strings"
"time"
"github.com/c2h5oh/datasize"
"github.com/cespare/xxhash/v2"
)
// var (
// version = "0.0.1"
// commit = "HEAD"
// date = "now"
// builtBy = "jondoveston"
// )
var size int64 = 0
var fs []File
type File struct {
Path string `json:"path"`
Size int64 `json:"size"`
Hash string `json:"hash"`
ModTime int64 `json:"mod_time"`
HashTime int64 `json:"hash_time"`
}
func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
j, _ := json.MarshalIndent(fs, "", " ")
fmt.Println(string(j))
os.Exit(0)
}
}()
currentDirectory, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
// require at least one argument
if len(os.Args) < 2 {
return
}
// parse stdin json
var fsin []File
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
bytes, err := io.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}
if len(bytes) > 0 {
err = json.Unmarshal(bytes, &fsin)
if err != nil {
log.Fatal(err)
}
}
}
// third argument is the optional minimum file size
if len(os.Args) == 3 {
var v datasize.ByteSize
err := v.UnmarshalText([]byte(os.Args[2]))
if err != nil {
log.Fatal(err)
}
size = int64(v.Bytes())
fmt.Fprintf(os.Stderr, "Min size %d\n", size)
}
// first argument is the command: hash or search
if os.Args[1] == "hash" {
hash(currentDirectory, fsin)
j, _ := json.MarshalIndent(fs, "", " ")
fmt.Println(string(j))
} else if os.Args[1] == "search" {
search(fsin)
}
}
// search for duplicate files
func search(fs []File) {
fmt.Fprintf(os.Stderr, "Searching %d files...\n", len(fs))
// groups files by hash
lookup := make(map[string][]File)
for _, f := range fs {
if f.Size <= size {
continue
}
fs, ok := lookup[f.Hash]
if ok {
fs = append(fs, f)
lookup[f.Hash] = fs
} else {
lookup[f.Hash] = []File{f}
}
}
// look for groups with more than one file
for _, fs := range lookup {
if len(fs) > 1 {
keep := selectFile(fs)
fmt.Fprintf(os.Stderr, "* %s\n", keep.Path)
for _, f := range fs {
if f.Path != keep.Path {
fmt.Fprintf(os.Stderr, "%s\n", f.Path)
fmt.Fprintf(os.Stdout, "%s\n", f.Path)
}
}
fmt.Fprintln(os.Stderr)
}
}
}
// recursively hash files
func hash(path string, fsin []File) {
// build lookup table for files already hashed
lookup := make(map[string]File)
for _, f := range fsin {
lookup[f.Path] = f
}
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Fatalf(err.Error())
}
if info.IsDir() {
return nil
}
if info.Size() <= size {
fmt.Fprintf(os.Stderr, "Too small: %s\n", info.Name())
return nil
}
fmt.Fprintf(os.Stderr, "File Name: %s\n", info.Name())
fmt.Fprintf(os.Stderr, "File Size: %d\n", info.Size())
var hashString string
var hashTime int64
if f, ok := lookup[path]; ok {
if f.HashTime > info.ModTime().Unix() && f.Size == info.Size() {
fmt.Fprintln(os.Stderr, "Already hashed")
hashString = f.Hash
hashTime = f.HashTime
}
}
if hashString == "" {
f, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer func() {
_ = f.Close()
}()
buf := make([]byte, 1024*1024)
hash := xxhash.New()
for {
n, err := f.Read(buf)
if err != nil {
if err != io.EOF {
log.Fatal(err)
}
break
}
_, err = hash.Write(buf[:n])
if err != nil {
log.Fatal(err)
}
}
hashTime = time.Now().Unix()
hashString = fmt.Sprintf("%016x", hash.Sum64())
}
if hashTime == 0 {
hashTime = time.Now().Unix()
}
fmt.Fprintf(os.Stderr, "File ModTime: %d\n", info.ModTime().Unix())
fmt.Fprintf(os.Stderr, "File Hash: %s\n", hashString)
fmt.Fprintf(os.Stderr, "File HashTime: %d\n", hashTime)
fs = append(fs, File{
Path: path,
Size: info.Size(),
ModTime: info.ModTime().Unix(),
Hash: hashString,
HashTime: hashTime,
})
fmt.Fprintln(os.Stderr)
return nil
})
if err != nil {
log.Fatalf(err.Error())
}
}
// select the file to keep
func selectFile(fs []File) File {
var keep []File
// do not keep files with "copy" in the name
for i, f := range fs {
if strings.Contains(strings.ToLower(f.Path), " copy") {
continue
}
keep = append(keep, fs[i])
}
// sort by ascending modification time
if len(keep) > 1 {
sort.Slice(keep, func(i, j int) bool {
return keep[i].ModTime < keep[j].ModTime
})
}
// return the oldest keep file
if len(keep) > 0 {
return keep[0]
} else {
return fs[0]
}
}