author | Mikael Berthe <mikael@lilotux.net> |
Sat, 18 Mar 2017 10:21:54 +0100 | |
changeset 23 | 9ce0f2e2a33f |
parent 22 | 46681d21157a |
child 24 | 40074e33c579 |
permissions | -rw-r--r-- |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
1 |
/* |
21 | 2 |
* Copyright (C) 2014-2017 Mikael Berthe <mikael@lilotux.net> |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
3 |
* |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
4 |
* This program is free software; you can redistribute it and/or modify |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
5 |
* it under the terms of the GNU General Public License as published by |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
6 |
* the Free Software Foundation; either version 2 of the License, or (at |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
7 |
* your option) any later version. |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
8 |
* |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, but |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
10 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
12 |
* General Public License for more details. |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
13 |
* |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
15 |
* along with this program; if not, write to the Free Software |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
17 |
* USA |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
18 |
*/ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
19 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
20 |
// This program (Goduf) is a fast duplicate file finder. |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
21 |
// Use goduf --help to get the list of available options. |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
22 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
23 |
package main |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
24 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
25 |
import ( |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
26 |
"crypto/sha1" |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
27 |
"encoding/hex" |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
28 |
"errors" |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
29 |
"flag" |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
30 |
"fmt" |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
31 |
"io" |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
32 |
"log" |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
33 |
"os" |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
34 |
"path/filepath" |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
35 |
"sort" |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
36 |
) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
37 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
38 |
const medsumBytes = 128 |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
39 |
const minSizePartialChecksum = 49152 // Should be > 3*medsumBytes |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
40 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
41 |
type sumType int |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
42 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
43 |
const ( |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
44 |
noChecksum sumType = iota |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
45 |
fullChecksum |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
46 |
partialChecksum |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
47 |
) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
48 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
49 |
type fileObj struct { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
50 |
//Unique bool |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
51 |
FilePath string |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
52 |
os.FileInfo |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
53 |
PartialHash []byte |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
54 |
Hash []byte |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
55 |
needHash sumType |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
56 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
57 |
|
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
58 |
// FileObjList is only exported so that we can have a sort interface on inodes. |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
59 |
type FileObjList []*fileObj |
16 | 60 |
type foListList []FileObjList |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
61 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
62 |
type dataT struct { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
63 |
totalSize uint64 |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
64 |
cmpt uint |
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
65 |
sizeGroups map[int64]*FileObjList |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
66 |
emptyFiles FileObjList |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
67 |
ignoreCount int |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
68 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
69 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
70 |
var data dataT |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
71 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
72 |
type myLogT struct { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
73 |
verbosity int |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
74 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
75 |
|
20 | 76 |
// Implement my own logger |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
77 |
var myLog myLogT |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
78 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
79 |
func (l *myLogT) Printf(level int, format string, args ...interface{}) { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
80 |
if level > l.verbosity { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
81 |
return |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
82 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
83 |
if level >= 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
84 |
log.Printf(format, args...) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
85 |
return |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
86 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
87 |
// Error message without timestamp |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
88 |
fmt.Fprintf(os.Stderr, format, args...) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
89 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
90 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
91 |
func (l *myLogT) Println(level int, args ...interface{}) { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
92 |
if level > l.verbosity { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
93 |
return |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
94 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
95 |
if level >= 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
96 |
log.Println(args...) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
97 |
return |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
98 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
99 |
// Error message without timestamp |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
100 |
fmt.Fprintln(os.Stderr, args...) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
101 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
102 |
|
20 | 103 |
// visit is called for every file and directory. |
104 |
// We check the file object is correct (regular, readable...) and add |
|
105 |
// it to the data.sizeGroups hash. |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
106 |
func visit(path string, f os.FileInfo, err error) error { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
107 |
if err != nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
108 |
if f == nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
109 |
return err |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
110 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
111 |
if f.IsDir() { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
112 |
myLog.Println(-1, "Warning: cannot process directory:", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
113 |
path) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
114 |
return filepath.SkipDir |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
115 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
116 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
117 |
myLog.Println(-1, "Ignoring ", path, " - ", err) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
118 |
data.ignoreCount++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
119 |
return nil |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
120 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
121 |
if f.IsDir() { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
122 |
return nil |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
123 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
124 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
125 |
if mode := f.Mode(); mode&os.ModeType != 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
126 |
if mode&os.ModeSymlink != 0 { |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
127 |
myLog.Println(6, "Ignoring symbolic link", path) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
128 |
} else { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
129 |
myLog.Println(0, "Ignoring special file", path) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
130 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
131 |
data.ignoreCount++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
132 |
return nil |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
133 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
134 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
135 |
data.cmpt++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
136 |
data.totalSize += uint64(f.Size()) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
137 |
fo := &fileObj{FilePath: path, FileInfo: f} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
138 |
if _, ok := data.sizeGroups[f.Size()]; !ok { |
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
139 |
data.sizeGroups[f.Size()] = new(FileObjList) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
140 |
} |
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
141 |
*data.sizeGroups[f.Size()] = append(*data.sizeGroups[f.Size()], fo) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
142 |
return nil |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
143 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
144 |
|
20 | 145 |
// Checksum computes the file's complete SHA1 hash. |
146 |
func (fo *fileObj) Checksum() error { |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
147 |
file, err := os.Open(fo.FilePath) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
148 |
if err != nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
149 |
return err |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
150 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
151 |
defer file.Close() |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
152 |
hash := sha1.New() |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
153 |
if size, err := io.Copy(hash, file); size != fo.Size() || err != nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
154 |
if err == nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
155 |
return errors.New("failed to read the whole file: " + |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
156 |
fo.FilePath) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
157 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
158 |
return err |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
159 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
160 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
161 |
fo.Hash = hash.Sum(nil) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
162 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
163 |
return nil |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
164 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
165 |
|
20 | 166 |
// partialChecksum computes the file's partial SHA1 hash (first and last bytes). |
167 |
func (fo *fileObj) partialChecksum() error { |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
168 |
file, err := os.Open(fo.FilePath) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
169 |
if err != nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
170 |
return err |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
171 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
172 |
defer file.Close() |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
173 |
hash := sha1.New() |
19 | 174 |
|
175 |
// Read first bytes and last bytes from file |
|
176 |
for i := 0; i < 2; i++ { |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
177 |
if _, err := io.CopyN(hash, file, medsumBytes); err != nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
178 |
if err == nil { |
19 | 179 |
const errmsg = "failed to read bytes from file: " |
180 |
return errors.New(errmsg + fo.FilePath) |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
181 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
182 |
return err |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
183 |
} |
19 | 184 |
if i == 0 { // Seek to end of file |
185 |
file.Seek(0-medsumBytes, 2) |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
186 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
187 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
188 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
189 |
fo.PartialHash = hash.Sum(nil) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
190 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
191 |
return nil |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
192 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
193 |
|
20 | 194 |
// Sum computes the file's SHA1 hash, partial or full according to sType. |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
195 |
func (fo *fileObj) Sum(sType sumType) error { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
196 |
if sType == partialChecksum { |
20 | 197 |
return fo.partialChecksum() |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
198 |
} else if sType == fullChecksum { |
20 | 199 |
return fo.Checksum() |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
200 |
} else if sType == noChecksum { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
201 |
return nil |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
202 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
203 |
panic("Internal error: Invalid sType") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
204 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
205 |
|
20 | 206 |
// dispCount display statistics to the user. |
207 |
func (data *dataT) dispCount() { // It this still useful? |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
208 |
if myLog.verbosity < 4 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
209 |
return |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
210 |
} |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
211 |
var c1, c1b, c2 int |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
212 |
var s1 string |
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
213 |
for _, scListP := range data.sizeGroups { |
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
214 |
c1 += len(*scListP) |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
215 |
c2++ |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
216 |
} |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
217 |
c1b = len(data.emptyFiles) |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
218 |
if c1b > 0 { |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
219 |
s1 = fmt.Sprintf("+%d", c1b) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
220 |
} |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
221 |
myLog.Printf(4, " Current countdown: %d [%d%s/%d]\n", |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
222 |
c1+c1b, c1, s1, c2) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
223 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
224 |
|
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
225 |
// checksum returns the requested checksum as a string. |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
226 |
// If the checksum has not been pre-computed, it is calculated now. |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
227 |
func (fo fileObj) checksum(sType sumType) (string, error) { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
228 |
var hbytes []byte |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
229 |
if sType == partialChecksum { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
230 |
hbytes = fo.PartialHash |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
231 |
} else if sType == fullChecksum { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
232 |
hbytes = fo.Hash |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
233 |
} else { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
234 |
panic("Internal error: Invalid sType") |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
235 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
236 |
if hbytes == nil { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
237 |
if err := fo.Sum(sType); err != nil { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
238 |
return "", err |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
239 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
240 |
if sType == partialChecksum { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
241 |
hbytes = fo.PartialHash |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
242 |
} else if sType == fullChecksum { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
243 |
hbytes = fo.Hash |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
244 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
245 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
246 |
return hex.EncodeToString(hbytes), nil |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
247 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
248 |
|
20 | 249 |
// computeSheduledChecksums calculates the checksums for all the files |
250 |
// from the fileLists slice items (the kind of hash is taken from the |
|
251 |
// needHash field). |
|
16 | 252 |
func computeSheduledChecksums(fileLists ...foListList) { |
10
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
253 |
var bigFileList FileObjList |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
254 |
// Merge the lists of FileObjList lists and create a unique list |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
255 |
// of file objects. |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
256 |
for _, foll := range fileLists { |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
257 |
for _, fol := range foll { |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
258 |
bigFileList = append(bigFileList, fol...) |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
259 |
} |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
260 |
} |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
261 |
|
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
262 |
// Sort the list for better efficiency |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
263 |
sort.Sort(ByInode(bigFileList)) |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
264 |
|
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
265 |
// Compute checksums |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
266 |
for _, fo := range bigFileList { |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
267 |
if err := fo.Sum(fo.needHash); err != nil { |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
268 |
myLog.Println(0, "Error:", err) |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
269 |
} |
22
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
270 |
fo.needHash = noChecksum |
10
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
271 |
} |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
272 |
} |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
273 |
|
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
274 |
func (fileList FileObjList) scheduleChecksum(sType sumType) { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
275 |
for _, fo := range fileList { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
276 |
fo.needHash = sType |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
277 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
278 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
279 |
|
20 | 280 |
// findDupesChecksums splits the fileObj list into several lists with the |
281 |
// same sType hash. |
|
22
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
282 |
func (fileList FileObjList) findDupesChecksums(sType sumType, dryRun bool) foListList { |
16 | 283 |
var dupeList foListList |
284 |
var scheduleFull foListList |
|
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
285 |
hashes := make(map[string]FileObjList) |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
286 |
|
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
287 |
// Sort the list for better efficiency |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
288 |
sort.Sort(ByInode(fileList)) |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
289 |
|
22
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
290 |
if sType == fullChecksum && dryRun { |
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
291 |
fileList.scheduleChecksum(fullChecksum) |
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
292 |
return append(dupeList, fileList) |
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
293 |
} |
7
68375cc98f98
Refactor checksum functions to reduce code duplication
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
294 |
// Compute checksums |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
295 |
for _, fo := range fileList { |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
296 |
hash, err := fo.checksum(sType) |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
297 |
if err != nil { |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
298 |
myLog.Println(0, "Error:", err) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
299 |
continue |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
300 |
} |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
301 |
hashes[hash] = append(hashes[hash], fo) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
302 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
303 |
|
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
304 |
// Let's de-dupe now... |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
305 |
for _, l := range hashes { |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
306 |
if len(l) < 2 { |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
307 |
continue |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
308 |
} |
7
68375cc98f98
Refactor checksum functions to reduce code duplication
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
309 |
if sType == partialChecksum { |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
310 |
scheduleFull = append(scheduleFull, l) |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
311 |
} else { // full checksums -> we're done |
7
68375cc98f98
Refactor checksum functions to reduce code duplication
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
312 |
dupeList = append(dupeList, l) |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
313 |
myLog.Printf(5, " . found %d new duplicates\n", len(l)) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
314 |
} |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
315 |
} |
9 | 316 |
if sType == partialChecksum && len(scheduleFull) > 0 { |
22
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
317 |
//computeSheduledChecksums(scheduleFull) |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
318 |
for _, l := range scheduleFull { |
22
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
319 |
r := l.findDupesChecksums(fullChecksum, dryRun) |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
320 |
dupeList = append(dupeList, r...) |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
321 |
} |
22
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
322 |
if dryRun { |
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
323 |
return scheduleFull |
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
324 |
} |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
325 |
} |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
326 |
|
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
327 |
return dupeList |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
328 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
329 |
|
6 | 330 |
// findDupes() uses checksums to find file duplicates |
16 | 331 |
func (data *dataT) findDupes(skipPartial bool) foListList { |
332 |
var dupeList foListList |
|
333 |
var schedulePartial foListList |
|
22
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
334 |
var schedulePartial2 foListList |
16 | 335 |
var scheduleFull foListList |
6 | 336 |
|
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
337 |
for size, sgListP := range data.sizeGroups { |
6 | 338 |
// We skip partial checksums for small files or if requested |
339 |
if size > minSizePartialChecksum && !skipPartial { |
|
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
340 |
sgListP.scheduleChecksum(partialChecksum) |
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
341 |
schedulePartial = append(schedulePartial, *sgListP) |
6 | 342 |
} else { |
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
343 |
sgListP.scheduleChecksum(fullChecksum) |
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
344 |
scheduleFull = append(scheduleFull, *sgListP) |
6 | 345 |
} |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
346 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
347 |
|
10
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
348 |
computeSheduledChecksums(schedulePartial, scheduleFull) |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
349 |
|
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
350 |
for _, l := range schedulePartial { |
22
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
351 |
r := l.findDupesChecksums(partialChecksum, true) // dry-run |
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
352 |
schedulePartial2 = append(schedulePartial2, r...) |
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
353 |
} |
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
354 |
computeSheduledChecksums(schedulePartial2) |
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
355 |
for _, l := range schedulePartial { |
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
356 |
r := l.findDupesChecksums(partialChecksum, false) |
6 | 357 |
dupeList = append(dupeList, r...) |
358 |
} |
|
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
359 |
for _, l := range scheduleFull { |
22
46681d21157a
Experimental optimization
Mikael Berthe <mikael@lilotux.net>
parents:
21
diff
changeset
|
360 |
r := l.findDupesChecksums(fullChecksum, false) |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
361 |
dupeList = append(dupeList, r...) |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
362 |
} |
6 | 363 |
return dupeList |
364 |
} |
|
365 |
||
20 | 366 |
// dropEmptyFiles removes the empty files from the main map, since we don't |
367 |
// have to do any processing about them. |
|
368 |
// If ignoreEmpty is false, the empty file list is saved in data.emptyFiles. |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
369 |
func (data *dataT) dropEmptyFiles(ignoreEmpty bool) (emptyCount int) { |
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
370 |
sgListP, ok := data.sizeGroups[0] |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
371 |
if ok == false { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
372 |
return // no empty files |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
373 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
374 |
if !ignoreEmpty { |
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
375 |
if len(*sgListP) > 1 { |
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
376 |
data.emptyFiles = *sgListP |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
377 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
378 |
delete(data.sizeGroups, 0) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
379 |
return |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
380 |
} |
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
381 |
emptyCount = len(*sgListP) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
382 |
delete(data.sizeGroups, 0) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
383 |
return |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
384 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
385 |
|
2
55098d552ae2
s/createSizeHash/initialCleanup/
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
386 |
// initialCleanup() removes files with unique size as well as hard links |
55098d552ae2
s/createSizeHash/initialCleanup/
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
387 |
func (data *dataT) initialCleanup() (hardLinkCount, uniqueSizeCount int) { |
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
388 |
for s, sgListP := range data.sizeGroups { |
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
389 |
if len(*sgListP) < 2 { |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
390 |
delete(data.sizeGroups, s) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
391 |
uniqueSizeCount++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
392 |
continue |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
393 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
394 |
|
10
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
395 |
// We can't look for hard links if the O.S. does not support |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
396 |
// them... |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
397 |
if !OSHasInodes() { |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
398 |
continue |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
399 |
} |
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
400 |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
401 |
var hardlinksFound bool |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
402 |
|
10
1ee01b135e0e
Reduce code duplication related to computeSheduledChecksums()
Mikael Berthe <mikael@lilotux.net>
parents:
9
diff
changeset
|
403 |
// Check for hard links |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
404 |
// Remove unique dev/inodes |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
405 |
// Instead of this loop, another way would be to use the field |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
406 |
// "Unique" of the fileObj to mark them to be discarded |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
407 |
// and remove them all at the end. |
20 | 408 |
// TODO: Should we also check for duplicate paths? |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
409 |
for { |
15
4e3a67dc70a0
Cosmetics (go fmt and comments...)
Mikael Berthe <mikael@lilotux.net>
parents:
14
diff
changeset
|
410 |
type devinode struct{ dev, ino uint64 } |
12 | 411 |
devinodes := make(map[devinode]bool) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
412 |
var hardLinkIndex int |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
413 |
|
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
414 |
for i, fo := range *sgListP { |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
415 |
dev, ino := GetDevIno(fo) |
15
4e3a67dc70a0
Cosmetics (go fmt and comments...)
Mikael Berthe <mikael@lilotux.net>
parents:
14
diff
changeset
|
416 |
di := devinode{dev, ino} |
12 | 417 |
if _, hlink := devinodes[di]; hlink { |
418 |
hardLinkIndex = i |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
419 |
hardLinkCount++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
420 |
hardlinksFound = true |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
421 |
break |
12 | 422 |
} else { |
423 |
devinodes[di] = true |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
424 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
425 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
426 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
427 |
if hardLinkIndex == 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
428 |
break |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
429 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
430 |
i := hardLinkIndex |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
431 |
// Remove hardink |
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
432 |
copy((*sgListP)[i:], (*sgListP)[i+1:]) |
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
433 |
(*sgListP)[len(*sgListP)-1] = nil |
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
434 |
*sgListP = (*sgListP)[:len(*sgListP)-1] |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
435 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
436 |
// We have found hard links in this size group, |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
437 |
// maybe we can remove it |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
438 |
if hardlinksFound { |
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
439 |
if len(*sgListP) < 2 { |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
440 |
delete(data.sizeGroups, s) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
441 |
uniqueSizeCount++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
442 |
continue |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
443 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
444 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
445 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
446 |
return |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
447 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
448 |
|
20 | 449 |
// formatSize returns the size in a string with a human-readable format. |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
450 |
func formatSize(sizeBytes uint64, short bool) string { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
451 |
var units = map[int]string{ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
452 |
0: "B", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
453 |
1: "KiB", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
454 |
2: "MiB", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
455 |
3: "GiB", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
456 |
4: "TiB", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
457 |
5: "PiB", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
458 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
459 |
humanSize := sizeBytes |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
460 |
var n int |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
461 |
for n < len(units)-1 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
462 |
if humanSize < 10000 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
463 |
break |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
464 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
465 |
humanSize /= 1024 |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
466 |
n++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
467 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
468 |
if n < 1 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
469 |
return fmt.Sprintf("%d bytes", sizeBytes) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
470 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
471 |
if short { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
472 |
return fmt.Sprintf("%d %s", humanSize, units[n]) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
473 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
474 |
return fmt.Sprintf("%d bytes (%d %s)", sizeBytes, humanSize, units[n]) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
475 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
476 |
|
20 | 477 |
// It all starts here. |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
478 |
func main() { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
479 |
var verbose bool |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
480 |
var summary bool |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
481 |
var skipPartial bool |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
482 |
var ignoreEmpty bool |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
483 |
|
23
9ce0f2e2a33f
Add an assertion on constant variables
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
484 |
// Assertion on constant values |
9ce0f2e2a33f
Add an assertion on constant variables
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
485 |
if minSizePartialChecksum <= 2*medsumBytes { |
9ce0f2e2a33f
Add an assertion on constant variables
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
486 |
log.Fatal("Internal error: assert minSizePartialChecksum > 2*medsumBytes") |
9ce0f2e2a33f
Add an assertion on constant variables
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
487 |
} |
9ce0f2e2a33f
Add an assertion on constant variables
Mikael Berthe <mikael@lilotux.net>
parents:
22
diff
changeset
|
488 |
|
20 | 489 |
// Command line parameters parsingg |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
490 |
flag.BoolVar(&verbose, "verbose", false, "Be verbose (verbosity=1)") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
491 |
flag.BoolVar(&verbose, "v", false, "See --verbose") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
492 |
flag.BoolVar(&summary, "summary", false, "Do not display the duplicate list") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
493 |
flag.BoolVar(&summary, "s", false, "See --summary") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
494 |
flag.BoolVar(&skipPartial, "skip-partial", false, "Skip partial checksums") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
495 |
flag.IntVar(&myLog.verbosity, "verbosity", 0, |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
496 |
"Set verbosity level (1-6)") |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
497 |
flag.IntVar(&myLog.verbosity, "vl", 0, "See verbosity") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
498 |
timings := flag.Bool("timings", false, "Set detailed log timings") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
499 |
flag.BoolVar(&ignoreEmpty, "no-empty", false, "Ignore empty files") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
500 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
501 |
flag.Parse() |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
502 |
|
20 | 503 |
// Set verbosity: --verbose=true == --verbosity=1 |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
504 |
if myLog.verbosity > 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
505 |
verbose = true |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
506 |
} else if verbose == true { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
507 |
myLog.verbosity = 1 |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
508 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
509 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
510 |
if len(flag.Args()) == 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
511 |
// TODO: more helpful usage statement |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
512 |
myLog.Println(-1, "Usage:", os.Args[0], |
20 | 513 |
"[options] base_directory|file...") |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
514 |
os.Exit(0) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
515 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
516 |
|
20 | 517 |
// Change log format for benchmarking |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
518 |
if *timings { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
519 |
log.SetFlags(log.LstdFlags | log.Lmicroseconds) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
520 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
521 |
|
14
ea4286b6c4b1
Get rid of the now useless sizeClass structure
Mikael Berthe <mikael@lilotux.net>
parents:
13
diff
changeset
|
522 |
data.sizeGroups = make(map[int64]*FileObjList) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
523 |
myLog.Println(1, "* Reading file metadata") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
524 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
525 |
for _, root := range flag.Args() { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
526 |
if err := filepath.Walk(root, visit); err != nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
527 |
myLog.Printf(-1, "* Error: could not read file tree:\n") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
528 |
myLog.Printf(-1, "> %v\n", err) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
529 |
os.Exit(1) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
530 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
531 |
} |
20 | 532 |
|
533 |
// Count empty files and drop them if they should be ignored |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
534 |
emptyCount := data.dropEmptyFiles(ignoreEmpty) |
20 | 535 |
|
536 |
// Display a small report |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
537 |
if verbose { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
538 |
if data.ignoreCount > 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
539 |
myLog.Printf(1, " %d special files were ignored\n", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
540 |
data.ignoreCount) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
541 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
542 |
myLog.Println(2, " Initial counter:", data.cmpt, "files") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
543 |
myLog.Println(2, " Total size:", formatSize(data.totalSize, |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
544 |
false)) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
545 |
if emptyCount > 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
546 |
myLog.Printf(1, " %d empty files were ignored\n", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
547 |
emptyCount) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
548 |
} |
20 | 549 |
data.dispCount() |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
550 |
myLog.Println(3, "* Number of size groups:", len(data.sizeGroups)) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
551 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
552 |
|
20 | 553 |
// Remove unique sizes and hard links |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
554 |
myLog.Println(1, "* Removing files with unique size and hard links...") |
2
55098d552ae2
s/createSizeHash/initialCleanup/
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
555 |
hardLinkCount, uniqueSizeCount := data.initialCleanup() |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
556 |
if verbose { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
557 |
myLog.Printf(2, " Dropped %d files with unique size\n", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
558 |
uniqueSizeCount) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
559 |
myLog.Printf(2, " Dropped %d hard links\n", hardLinkCount) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
560 |
myLog.Println(3, "* Number of size groups:", len(data.sizeGroups)) |
20 | 561 |
data.dispCount() |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
562 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
563 |
|
20 | 564 |
// Get the final list of dupes, using checksums |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
565 |
myLog.Println(1, "* Computing checksums...") |
16 | 566 |
var result foListList |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
567 |
if len(data.emptyFiles) > 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
568 |
result = append(result, data.emptyFiles) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
569 |
} |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
570 |
result = append(result, data.findDupes(skipPartial)...) |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
571 |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
572 |
myLog.Println(3, "* Number of match groups:", len(result)) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
573 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
574 |
// Done! Dump dupes |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
575 |
if len(result) > 0 && !summary { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
576 |
myLog.Println(1, "* Dupes:") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
577 |
} |
21 | 578 |
|
579 |
// Sort files by path inside each group |
|
580 |
for _, l := range result { |
|
581 |
sort.Sort(byFilePathName(l)) |
|
582 |
} |
|
583 |
// Sort groups by increasing size (of the duplicated files) |
|
16 | 584 |
sort.Sort(byGroupFileSize(result)) |
585 |
||
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
586 |
var dupeSize uint64 |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
587 |
data.cmpt = 0 |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
588 |
for i, l := range result { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
589 |
size := uint64(l[0].Size()) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
590 |
// We do not count the size of the 1st item |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
591 |
// so we get only duplicate size. |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
592 |
dupeSize += size * uint64(len(l)-1) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
593 |
if !summary { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
594 |
fmt.Printf("\nGroup #%d (%d files * %v):\n", i+1, |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
595 |
len(l), formatSize(size, true)) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
596 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
597 |
for _, f := range l { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
598 |
if !summary { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
599 |
fmt.Println(f.FilePath) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
600 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
601 |
data.cmpt++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
602 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
603 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
604 |
summaryLevel := 1 // Default verbosity for the summary line |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
605 |
if summary == false { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
606 |
// Add a trailing newline |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
607 |
if len(result) > 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
608 |
fmt.Println("") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
609 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
610 |
} else { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
611 |
// The summary is requested so we lower the verbosity level |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
612 |
summaryLevel = 0 |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
613 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
614 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
615 |
myLog.Println(summaryLevel, "Final count:", data.cmpt, |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
616 |
"duplicate files in", len(result), "sets") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
617 |
myLog.Println(summaryLevel, "Redundant data size:", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
618 |
formatSize(dupeSize, false)) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
619 |
} |
16 | 620 |
|
621 |
// Implement a sort interface for the list of duplicate groups |
|
622 |
type byGroupFileSize foListList |
|
623 |
||
624 |
func (a byGroupFileSize) Len() int { return len(a) } |
|
625 |
func (a byGroupFileSize) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
|
626 |
func (a byGroupFileSize) Less(i, j int) bool { |
|
627 |
// Since this is supposed to be used for duplicate lists, |
|
628 |
// we use the size of the first file of the group. |
|
21 | 629 |
if a[i][0].Size() == a[j][0].Size() { |
630 |
return a[i][0].FilePath < a[j][0].FilePath |
|
631 |
} |
|
16 | 632 |
return a[i][0].Size() < a[j][0].Size() |
633 |
} |
|
634 |
||
635 |
// Implement a sort interface for a slice of files |
|
636 |
type byFilePathName FileObjList |
|
637 |
||
638 |
func (a byFilePathName) Len() int { return len(a) } |
|
639 |
func (a byFilePathName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
|
640 |
func (a byFilePathName) Less(i, j int) bool { |
|
21 | 641 |
return a[i].FilePath < a[j].FilePath |
16 | 642 |
} |