author | Mikael Berthe <mikael@lilotux.net> |
Sun, 29 Jun 2014 01:53:56 +0200 | |
changeset 9 | 5b58342459eb |
parent 8 | 25ad96511395 |
child 10 | 1ee01b135e0e |
permissions | -rw-r--r-- |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
1 |
/* |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
2 |
* Copyright (C) 2014 Mikael Berthe <mikael@lilotux.net> |
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 |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
60 |
|
7
68375cc98f98
Refactor checksum functions to reduce code duplication
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
61 |
type sizeClass struct { // XXX still useful? |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
62 |
files FileObjList |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
63 |
medsums map[string]FileObjList |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
64 |
fullsums map[string]FileObjList |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
65 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
66 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
67 |
type dataT struct { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
68 |
totalSize uint64 |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
69 |
cmpt uint |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
70 |
sizeGroups map[int64]*sizeClass |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
71 |
emptyFiles FileObjList |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
72 |
ignoreCount int |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
73 |
} |
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 |
var data dataT |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
76 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
77 |
type myLogT struct { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
78 |
verbosity int |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
79 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
80 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
81 |
var myLog myLogT |
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 |
func (l *myLogT) Printf(level int, format string, args ...interface{}) { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
84 |
if level > l.verbosity { |
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 |
if level >= 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
88 |
log.Printf(format, args...) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
89 |
return |
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 |
// Error message without timestamp |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
92 |
fmt.Fprintf(os.Stderr, format, args...) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
93 |
} |
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 |
func (l *myLogT) Println(level int, args ...interface{}) { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
96 |
if level > l.verbosity { |
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 |
if level >= 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
100 |
log.Println(args...) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
101 |
return |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
102 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
103 |
// Error message without timestamp |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
104 |
fmt.Fprintln(os.Stderr, args...) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
105 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
106 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
107 |
func visit(path string, f os.FileInfo, err error) error { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
108 |
if err != nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
109 |
if f == nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
110 |
return err |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
111 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
112 |
if f.IsDir() { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
113 |
myLog.Println(-1, "Warning: cannot process directory:", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
114 |
path) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
115 |
return filepath.SkipDir |
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 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
118 |
myLog.Println(-1, "Ignoring ", path, " - ", err) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
119 |
data.ignoreCount++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
120 |
return nil |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
121 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
122 |
if f.IsDir() { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
123 |
return nil |
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 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
126 |
if mode := f.Mode(); mode&os.ModeType != 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
127 |
if mode&os.ModeSymlink != 0 { |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
128 |
myLog.Println(6, "Ignoring symbolic link", path) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
129 |
} else { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
130 |
myLog.Println(0, "Ignoring special file", path) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
131 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
132 |
data.ignoreCount++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
133 |
return nil |
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 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
136 |
data.cmpt++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
137 |
data.totalSize += uint64(f.Size()) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
138 |
fo := &fileObj{FilePath: path, FileInfo: f} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
139 |
if _, ok := data.sizeGroups[f.Size()]; !ok { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
140 |
data.sizeGroups[f.Size()] = &sizeClass{} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
141 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
142 |
data.sizeGroups[f.Size()].files = |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
143 |
append(data.sizeGroups[f.Size()].files, fo) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
144 |
return nil |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
145 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
146 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
147 |
func (fo *fileObj) CheckSum() error { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
148 |
file, err := os.Open(fo.FilePath) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
149 |
if err != nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
150 |
return err |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
151 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
152 |
defer file.Close() |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
153 |
hash := sha1.New() |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
154 |
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
|
155 |
if err == nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
156 |
return errors.New("failed to read the whole file: " + |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
157 |
fo.FilePath) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
158 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
159 |
return err |
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 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
162 |
fo.Hash = hash.Sum(nil) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
163 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
164 |
return nil |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
165 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
166 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
167 |
func (fo *fileObj) MedSum() error { |
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() |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
174 |
// XXX: duplicated code! |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
175 |
// BOF |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
176 |
if _, err := io.CopyN(hash, file, medsumBytes); err != nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
177 |
if err == nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
178 |
return errors.New("failed to read bytes from file:" + |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
179 |
fo.FilePath) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
180 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
181 |
return err |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
182 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
183 |
/* |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
184 |
// MOF |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
185 |
file.Seek((fo.Size()-medsumBytes)/2, 0) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
186 |
if _, err := io.CopyN(hash, file, medsumBytes); err != nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
187 |
if err == nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
188 |
return errors.New("failed to read bytes from file:" + |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
189 |
fo.FilePath) |
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 err |
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 |
*/ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
194 |
// EOF |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
195 |
file.Seek(0-medsumBytes, 2) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
196 |
if _, err := io.CopyN(hash, file, medsumBytes); err != nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
197 |
if err == nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
198 |
return errors.New("failed to read bytes from file:" + |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
199 |
fo.FilePath) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
200 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
201 |
return err |
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 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
204 |
fo.PartialHash = hash.Sum(nil) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
205 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
206 |
return nil |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
207 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
208 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
209 |
func (fo *fileObj) Sum(sType sumType) error { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
210 |
if sType == partialChecksum { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
211 |
return fo.MedSum() |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
212 |
} else if sType == fullChecksum { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
213 |
return fo.CheckSum() |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
214 |
} else if sType == noChecksum { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
215 |
return nil |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
216 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
217 |
panic("Internal error: Invalid sType") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
218 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
219 |
|
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
220 |
func (data *dataT) dispCount() { // FIXME rather useless |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
221 |
if myLog.verbosity < 4 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
222 |
return |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
223 |
} |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
224 |
var c1, c1b, c2 int |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
225 |
var s1 string |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
226 |
for _, sc := range data.sizeGroups { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
227 |
c1 += len(sc.files) |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
228 |
c2++ |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
229 |
} |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
230 |
c1b = len(data.emptyFiles) |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
231 |
if c1b > 0 { |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
232 |
s1 = fmt.Sprintf("+%d", c1b) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
233 |
} |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
234 |
myLog.Printf(4, " Current countdown: %d [%d%s/%d]\n", |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
235 |
c1+c1b, c1, s1, c2) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
236 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
237 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
238 |
func (data *dataT) filesWithSameHash() (hgroups []FileObjList) { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
239 |
for _, sizeGroup := range data.sizeGroups { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
240 |
for _, l := range sizeGroup.fullsums { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
241 |
hgroups = append(hgroups, l) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
242 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
243 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
244 |
return |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
245 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
246 |
|
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
247 |
// 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
|
248 |
// 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
|
249 |
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
|
250 |
var hbytes []byte |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
251 |
if sType == partialChecksum { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
252 |
hbytes = fo.PartialHash |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
253 |
} else if sType == fullChecksum { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
254 |
hbytes = fo.Hash |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
255 |
} else { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
256 |
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
|
257 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
258 |
if hbytes == nil { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
259 |
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
|
260 |
return "", err |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
261 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
262 |
if sType == partialChecksum { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
263 |
hbytes = fo.PartialHash |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
264 |
} else if sType == fullChecksum { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
265 |
hbytes = fo.Hash |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
266 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
267 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
268 |
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
|
269 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
270 |
|
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
271 |
func (fileList FileObjList) computeSheduledChecksums() { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
272 |
// Sort the list for better efficiency |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
273 |
sort.Sort(ByInode(fileList)) |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
274 |
|
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
275 |
myLog.Printf(6, " . will compute %d checksums\n", len(fileList)) |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
276 |
|
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
277 |
// Compute checksums |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
278 |
for _, fo := range fileList { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
279 |
if err := fo.Sum(fo.needHash); err != nil { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
280 |
myLog.Println(0, "Error:", err) |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
281 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
282 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
283 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
284 |
|
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
285 |
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
|
286 |
for _, fo := range fileList { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
287 |
fo.needHash = sType |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
288 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
289 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
290 |
|
7
68375cc98f98
Refactor checksum functions to reduce code duplication
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
291 |
func (fileList FileObjList) findDupesChecksums(sType sumType) []FileObjList { |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
292 |
var dupeList []FileObjList |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
293 |
var scheduleFull []FileObjList |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
294 |
hashes := make(map[string]FileObjList) |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
295 |
|
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
296 |
// Sort the list for better efficiency |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
297 |
sort.Sort(ByInode(fileList)) |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
298 |
|
7
68375cc98f98
Refactor checksum functions to reduce code duplication
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
299 |
// Compute checksums |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
300 |
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
|
301 |
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
|
302 |
if err != nil { |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
303 |
myLog.Println(0, "Error:", err) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
304 |
continue |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
305 |
} |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
306 |
hashes[hash] = append(hashes[hash], fo) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
307 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
308 |
|
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
309 |
// Let's de-dupe now... |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
310 |
for _, l := range hashes { |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
311 |
if len(l) < 2 { |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
312 |
continue |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
313 |
} |
7
68375cc98f98
Refactor checksum functions to reduce code duplication
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
314 |
if sType == partialChecksum { |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
315 |
scheduleFull = append(scheduleFull, l) |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
316 |
} else { // full checksums -> we're done |
7
68375cc98f98
Refactor checksum functions to reduce code duplication
Mikael Berthe <mikael@lilotux.net>
parents:
6
diff
changeset
|
317 |
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
|
318 |
// TODO: sort by increasing size |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
319 |
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
|
320 |
} |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
321 |
} |
9 | 322 |
if sType == partialChecksum && len(scheduleFull) > 0 { |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
323 |
var csList FileObjList |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
324 |
for _, fol := range scheduleFull { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
325 |
csList = append(csList, fol...) |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
326 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
327 |
myLog.Printf(6, " .. findDupesChecksums: computing %d "+ |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
328 |
"full checksums\n", len(csList)) // DBG |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
329 |
csList.computeSheduledChecksums() |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
330 |
for _, l := range scheduleFull { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
331 |
r := l.findDupesChecksums(fullChecksum) |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
332 |
dupeList = append(dupeList, r...) |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
333 |
} |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
334 |
} |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
335 |
|
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
336 |
return dupeList |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
337 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
338 |
|
6 | 339 |
// findDupes() uses checksums to find file duplicates |
340 |
func (data *dataT) findDupes(skipPartial bool) []FileObjList { |
|
341 |
var dupeList []FileObjList |
|
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
342 |
var schedulePartial []FileObjList |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
343 |
var scheduleFull []FileObjList |
6 | 344 |
|
345 |
for size, sizeGroup := range data.sizeGroups { |
|
346 |
// We skip partial checksums for small files or if requested |
|
347 |
if size > minSizePartialChecksum && !skipPartial { |
|
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
348 |
sizeGroup.files.scheduleChecksum(partialChecksum) |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
349 |
schedulePartial = append(schedulePartial, sizeGroup.files) |
6 | 350 |
} else { |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
351 |
sizeGroup.files.scheduleChecksum(fullChecksum) |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
352 |
scheduleFull = append(scheduleFull, sizeGroup.files) |
6 | 353 |
} |
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
354 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
355 |
|
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
356 |
var csList FileObjList |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
357 |
for _, fol := range schedulePartial { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
358 |
csList = append(csList, fol...) |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
359 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
360 |
for _, fol := range scheduleFull { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
361 |
csList = append(csList, fol...) |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
362 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
363 |
myLog.Printf(6, " .. findDupes: computing %d misc checksums\n", |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
364 |
len(csList)) // DBG |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
365 |
csList.computeSheduledChecksums() |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
366 |
|
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
367 |
for _, l := range schedulePartial { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
368 |
r := l.findDupesChecksums(partialChecksum) |
6 | 369 |
dupeList = append(dupeList, r...) |
370 |
} |
|
8
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
371 |
for _, l := range scheduleFull { |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
372 |
r := l.findDupesChecksums(fullChecksum) |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
373 |
dupeList = append(dupeList, r...) |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
374 |
} |
25ad96511395
Schedule checksum computations so that we reduce hard drive seeks
Mikael Berthe <mikael@lilotux.net>
parents:
7
diff
changeset
|
375 |
// TODO: sort by increasing size |
6 | 376 |
return dupeList |
377 |
} |
|
378 |
||
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
379 |
func (data *dataT) dropEmptyFiles(ignoreEmpty bool) (emptyCount int) { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
380 |
sc, ok := data.sizeGroups[0] |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
381 |
if ok == false { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
382 |
return // no empty files |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
383 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
384 |
if !ignoreEmpty { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
385 |
if len(sc.files) > 1 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
386 |
data.emptyFiles = sc.files |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
387 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
388 |
delete(data.sizeGroups, 0) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
389 |
return |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
390 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
391 |
emptyCount = len(sc.files) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
392 |
delete(data.sizeGroups, 0) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
393 |
return |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
394 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
395 |
|
2
55098d552ae2
s/createSizeHash/initialCleanup/
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
396 |
// initialCleanup() removes files with unique size as well as hard links |
55098d552ae2
s/createSizeHash/initialCleanup/
Mikael Berthe <mikael@lilotux.net>
parents:
1
diff
changeset
|
397 |
func (data *dataT) initialCleanup() (hardLinkCount, uniqueSizeCount int) { |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
398 |
for s, sizeGroup := range data.sizeGroups { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
399 |
if len(sizeGroup.files) < 2 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
400 |
delete(data.sizeGroups, s) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
401 |
uniqueSizeCount++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
402 |
continue |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
403 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
404 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
405 |
var hardlinksFound bool |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
406 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
407 |
// Check for hardlinks |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
408 |
// Remove unique dev/inodes |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
409 |
// 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
|
410 |
// "Unique" of the fileObj to mark them to be discarded |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
411 |
// and remove them all at the end. |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
412 |
for { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
413 |
if !OSHasInodes() { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
414 |
break |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
415 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
416 |
var hardLinkIndex int |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
417 |
fo := sizeGroup.files[0] |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
418 |
prevDev, prevIno := GetDevIno(fo) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
419 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
420 |
for i, fo := range sizeGroup.files[1:] { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
421 |
dev, ino := GetDevIno(fo) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
422 |
if dev == prevDev && ino == prevIno { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
423 |
hardLinkIndex = i + 1 |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
424 |
hardLinkCount++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
425 |
hardlinksFound = true |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
426 |
break |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
427 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
428 |
prevDev = dev |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
429 |
prevIno = ino |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
430 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
431 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
432 |
if hardLinkIndex == 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
433 |
break |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
434 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
435 |
i := hardLinkIndex |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
436 |
// Remove hardink |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
437 |
copy(sizeGroup.files[i:], sizeGroup.files[i+1:]) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
438 |
sizeGroup.files[len(sizeGroup.files)-1] = nil |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
439 |
sizeGroup.files = sizeGroup.files[:len(sizeGroup.files)-1] |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
440 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
441 |
// We have found hard links in this size group, |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
442 |
// maybe we can remove it |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
443 |
if hardlinksFound { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
444 |
if len(sizeGroup.files) < 2 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
445 |
delete(data.sizeGroups, s) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
446 |
uniqueSizeCount++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
447 |
continue |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
448 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
449 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
450 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
451 |
return |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
452 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
453 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
454 |
func formatSize(sizeBytes uint64, short bool) string { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
455 |
var units = map[int]string{ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
456 |
0: "B", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
457 |
1: "KiB", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
458 |
2: "MiB", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
459 |
3: "GiB", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
460 |
4: "TiB", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
461 |
5: "PiB", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
462 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
463 |
humanSize := sizeBytes |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
464 |
var n int |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
465 |
for n < len(units)-1 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
466 |
if humanSize < 10000 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
467 |
break |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
468 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
469 |
humanSize /= 1024 |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
470 |
n++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
471 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
472 |
if n < 1 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
473 |
return fmt.Sprintf("%d bytes", sizeBytes) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
474 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
475 |
if short { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
476 |
return fmt.Sprintf("%d %s", humanSize, units[n]) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
477 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
478 |
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
|
479 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
480 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
481 |
func main() { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
482 |
var verbose bool |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
483 |
var summary bool |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
484 |
var skipPartial bool |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
485 |
var ignoreEmpty bool |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
486 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
487 |
flag.BoolVar(&verbose, "verbose", false, "Be verbose (verbosity=1)") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
488 |
flag.BoolVar(&verbose, "v", false, "See --verbose") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
489 |
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
|
490 |
flag.BoolVar(&summary, "s", false, "See --summary") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
491 |
flag.BoolVar(&skipPartial, "skip-partial", false, "Skip partial checksums") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
492 |
flag.IntVar(&myLog.verbosity, "verbosity", 0, |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
493 |
"Set verbosity level (1-6)") |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
494 |
flag.IntVar(&myLog.verbosity, "vl", 0, "See verbosity") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
495 |
timings := flag.Bool("timings", false, "Set detailed log timings") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
496 |
flag.BoolVar(&ignoreEmpty, "no-empty", false, "Ignore empty files") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
497 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
498 |
flag.Parse() |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
499 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
500 |
if myLog.verbosity > 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
501 |
verbose = true |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
502 |
} else if verbose == true { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
503 |
myLog.verbosity = 1 |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
504 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
505 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
506 |
if len(flag.Args()) == 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
507 |
// TODO: more helpful usage statement |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
508 |
myLog.Println(-1, "Usage:", os.Args[0], |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
509 |
"[options] base_directory") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
510 |
os.Exit(0) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
511 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
512 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
513 |
if *timings { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
514 |
log.SetFlags(log.LstdFlags | log.Lmicroseconds) |
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 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
517 |
data.sizeGroups = make(map[int64]*sizeClass) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
518 |
myLog.Println(1, "* Reading file metadata") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
519 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
520 |
for _, root := range flag.Args() { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
521 |
if err := filepath.Walk(root, visit); err != nil { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
522 |
myLog.Printf(-1, "* Error: could not read file tree:\n") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
523 |
myLog.Printf(-1, "> %v\n", err) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
524 |
os.Exit(1) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
525 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
526 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
527 |
emptyCount := data.dropEmptyFiles(ignoreEmpty) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
528 |
if verbose { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
529 |
if data.ignoreCount > 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
530 |
myLog.Printf(1, " %d special files were ignored\n", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
531 |
data.ignoreCount) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
532 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
533 |
myLog.Println(2, " Initial counter:", data.cmpt, "files") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
534 |
myLog.Println(2, " Total size:", formatSize(data.totalSize, |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
535 |
false)) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
536 |
if emptyCount > 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
537 |
myLog.Printf(1, " %d empty files were ignored\n", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
538 |
emptyCount) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
539 |
} |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
540 |
data.dispCount() // XXX |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
541 |
myLog.Println(3, "* Number of size groups:", len(data.sizeGroups)) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
542 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
543 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
544 |
// Remove unique sizes |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
545 |
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
|
546 |
hardLinkCount, uniqueSizeCount := data.initialCleanup() |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
547 |
if verbose { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
548 |
myLog.Printf(2, " Dropped %d files with unique size\n", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
549 |
uniqueSizeCount) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
550 |
myLog.Printf(2, " Dropped %d hard links\n", hardLinkCount) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
551 |
myLog.Println(3, "* Number of size groups:", len(data.sizeGroups)) |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
552 |
data.dispCount() // XXX |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
553 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
554 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
555 |
// Get list of dupes |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
556 |
myLog.Println(1, "* Computing checksums...") |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
557 |
var result []FileObjList |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
558 |
if len(data.emptyFiles) > 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
559 |
result = append(result, data.emptyFiles) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
560 |
} |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
561 |
result = append(result, data.findDupes(skipPartial)...) |
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
562 |
|
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
563 |
myLog.Println(3, "* Number of match groups:", len(result)) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
564 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
565 |
// Done! Dump dupes |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
566 |
if len(result) > 0 && !summary { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
567 |
myLog.Println(1, "* Dupes:") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
568 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
569 |
var dupeSize uint64 |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
570 |
data.cmpt = 0 |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
571 |
for i, l := range result { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
572 |
size := uint64(l[0].Size()) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
573 |
// We do not count the size of the 1st item |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
574 |
// so we get only duplicate size. |
5
887c21c26cc8
Refactor the checksum part
Mikael Berthe <mikael@lilotux.net>
parents:
2
diff
changeset
|
575 |
dupeSize += size * uint64(len(l)-1) |
0
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
576 |
if !summary { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
577 |
fmt.Printf("\nGroup #%d (%d files * %v):\n", i+1, |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
578 |
len(l), formatSize(size, true)) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
579 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
580 |
for _, f := range l { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
581 |
if !summary { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
582 |
fmt.Println(f.FilePath) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
583 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
584 |
data.cmpt++ |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
585 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
586 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
587 |
summaryLevel := 1 // Default verbosity for the summary line |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
588 |
if summary == false { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
589 |
// Add a trailing newline |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
590 |
if len(result) > 0 { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
591 |
fmt.Println("") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
592 |
} |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
593 |
} else { |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
594 |
// The summary is requested so we lower the verbosity level |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
595 |
summaryLevel = 0 |
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 |
|
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
598 |
myLog.Println(summaryLevel, "Final count:", data.cmpt, |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
599 |
"duplicate files in", len(result), "sets") |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
600 |
myLog.Println(summaryLevel, "Redundant data size:", |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
601 |
formatSize(dupeSize, false)) |
a5642cd03cef
Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
602 |
} |