-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
271 lines (243 loc) · 6.56 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package main
import (
"crypto/sha256"
"database/sql"
"flag"
"fmt"
_ "github.com/mattn/go-sqlite3"
"io"
"os"
"path/filepath"
"strings"
)
var version = "1.2.1"
func printOverwrite(message string) {
fmt.Printf("\r%s", message)
}
func initDB() (*sql.DB, error) {
dbPath := "./copycure.db"
err := os.Remove(dbPath)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
return nil, err
}
createTableSQL := `
CREATE TABLE IF NOT EXISTS files (
checksum TEXT PRIMARY KEY,
path TEXT NOT NULL
);
`
_, err = db.Exec(createTableSQL)
if err != nil {
db.Close()
return nil, err
}
return db, nil
}
func computeSHA256(db *sql.DB, filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()
hash := sha256.New()
_, err = io.Copy(hash, file)
if err != nil {
return "", err
}
checksum := fmt.Sprintf("%x", hash.Sum(nil))
return checksum, nil
}
func countFiles(directory string) (int, error) {
var totalFiles int
err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
totalFiles++
}
return nil
})
return totalFiles, err
}
func deletePath(path string, noconfirm bool) (bool, error) {
var response string
deleted := false
var err error
if !noconfirm {
fmt.Printf("\nDo you want to delete %s? (y/n): ", path)
fmt.Scanln(&response)
} else {
response = "y"
}
if strings.ToLower(response) == "y" {
err = os.Remove(path)
if err != nil {
fmt.Println()
fmt.Printf("Failed to remove duplicate file %s: %v\n", path, err)
} else {
deleted = true
}
} else {
fmt.Printf("Skipped %s\n", path)
}
return deleted, err
}
func removeDuplicates(db *sql.DB, seenChecksums map[string]struct{}, directory string, allFiles int, noConfirm bool, exclude []string, deleteEmpty bool, listMode bool) (int, int, error) {
cnt := 0
dbl := 0
pct := 0
fileCnt := 0
if !listMode {
printOverwrite(fmt.Sprintf("%d/%d (%d%%) - %d files deleted ...", fileCnt, allFiles, pct, cnt))
}
deleteSizeLimit := int64(10)
if deleteEmpty {
deleteSizeLimit = int64(-1)
}
err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && !containsSubstring(path, exclude) && info.Size() > deleteSizeLimit {
fileCnt++
newPct := int(float64(fileCnt*100) / float64(allFiles))
if newPct > pct && !listMode {
pct = newPct
printOverwrite(fmt.Sprintf("%d/%d (%d%%) - %d files deleted ...", fileCnt, allFiles, pct, cnt))
}
checksum, err := computeSHA256(db, path)
if err != nil {
fmt.Println()
fmt.Printf("Failed to compute checksum for file %s: %v\n", path, err)
return nil
}
if db != nil {
var existingPath string
err = db.QueryRow("SELECT path FROM files WHERE checksum = ?", checksum).Scan(&existingPath)
if err == sql.ErrNoRows {
// No duplicate found
_, err = db.Exec("INSERT INTO files (checksum, path) VALUES (?, ?)", checksum, path)
if err != nil {
fmt.Println("failed inserting checksum for file %s: %v\n", path, err)
}
} else if err != nil {
fmt.Println()
fmt.Printf("Failed to query database for file %s: %v\n", path, err)
return nil
} else {
if listMode {
fmt.Println(path)
} else {
deleted, _ := deletePath(path, noConfirm)
if deleted {
cnt++
}
}
dbl++
}
} else if seenChecksums != nil {
if _, exists := seenChecksums[checksum]; exists {
err := os.Remove(path)
if err != nil {
fmt.Println()
fmt.Printf("Failed to remove duplicate file %s: %v\n", path, err)
return nil
}
cnt++
} else {
seenChecksums[checksum] = struct{}{}
}
}
}
return nil
})
if err != nil {
return cnt, dbl, err
}
fmt.Println()
return cnt, dbl, nil
}
func containsSubstring(path string, exclude []string) bool {
for _, s := range exclude {
if s == "" {
continue
}
if strings.Contains(path, s) {
return true
}
}
return false
}
func isFlagPassed(name string) bool {
found := false
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}
func main() {
var directory string
var mode string
var excludeParam string
flag.StringVar(&directory, "i", "", "Directory to scan for duplicates")
flag.StringVar(&mode, "m", "sql", "Mode: 'sql' or 'mem'")
_ = flag.Bool("y", false, "Delete files without asking")
_ = flag.Bool("e", false, "Delete empty files too. Empty files all look the same to copycure. The Default is to ignore them")
_ = flag.Bool("l", false, "List the duplicates instead of deleting them. -y is automatically assumed to be set")
flag.StringVar(&excludeParam, "x", "", "Comma separated list of partial filenames to exclude (e.g. -e .venv/,.git/)")
flag.Parse()
exclude := strings.Split(excludeParam, ",")
noConfirm := isFlagPassed("y")
deleteEmpty := isFlagPassed("e")
listMode := isFlagPassed("l")
if directory == "" {
fmt.Printf("CopyCure %v (written by Sammy Fischer)\n", version)
fmt.Println("Usage: copycure -i /path/to/your/directory [-m sql|mem] [-y] [-x aaa,bbb] [-e] [-l]\n" +
" -m {sql|mem} : method to store known checksums. \n\tsql: use a temporary sqllite database mem: store in and array in RAM\n" +
" -y : remove files without asking\n" +
" -x {comma separated list} : exclude any file whose path contains one of the list values\n\t(e.g. -e .venv,.git ignores any path containing .venv or .git)}\n" +
" -e : remove duplicate empty files (size==0). Default is to ignore them.\n" +
" -l : only list the full path to the duplicates found without deleting them.\n")
os.Exit(1)
}
if !listMode {
fmt.Printf("CopyCure %v (written by Sammy Fischer)\n", version)
}
var db *sql.DB
var seenChecksums map[string]struct{}
switch mode {
case "sql":
var err error
db, err = initDB()
if err != nil {
fmt.Printf("Error initializing database: %v\n", err)
os.Exit(1)
}
defer db.Close()
case "mem":
seenChecksums = make(map[string]struct{})
default:
fmt.Println("Invalid mode. Use 'sql' or 'mem'.")
os.Exit(1)
}
totalFiles, err := countFiles(directory)
if err != nil {
fmt.Printf("Error counting files: %v\n", err)
os.Exit(1)
}
cnt, dbl, err := removeDuplicates(db, seenChecksums, directory, totalFiles, noConfirm, exclude, deleteEmpty, listMode)
if err != nil {
fmt.Printf("Error removing duplicates: %v\n", err)
os.Exit(1)
}
if !listMode {
fmt.Printf("%d duplicates found, %d files were removed.\n", dbl, cnt)
}
}