-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdff.js
More file actions
282 lines (224 loc) · 6.12 KB
/
dff.js
File metadata and controls
282 lines (224 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/bin/node
/**
* Traverses a directory structure looking for duplicate files in order:
* 1. size
* 2. CRC
* 3. Hash
*/
/*
Modules
*/
var _ = require('underscore'),
async = require('async'),
crc = require('crc'),
fs = require('fs'),
util = require('util');
function DFFile(path, stat){
var slash_char = path.lastIndexOf('/');
// +1 to keep the slash
this.directory = path.slice(0, path.lastIndexOf('/')+1);
// +1 to remove the slash
this.filename = path.slice(path.lastIndexOf('/')+1);
this.fullpath = path;
this.stat = stat;
}
/**
* Parse the command line arguments
*
*
*/
function parse_arguments() {
var args = {
dirs: []
};
process.argv.forEach(function(arg, index, array) {
var stat = fs.statSync(arg);
if(stat.isDirectory()){
args.dirs.push(arg);
}
});
return args;
}
function print_duplicates(){
function prn(str){
console.log(str); //XXX
}
// console.time('print');
for (var i = duplicate_files.length - 1; i >= 0; i--) {
_.each(duplicate_files[i], prn);
}
// console.timeEnd('print');
// console.log('Total:', duplicate_files.length); //XXX
}
function get_hash_key(filename){
}
function get_crc_key(filename, fn){
var max_buffer_size = 1024,
fd = fs.openSync(filename, 'r'),
size = fs.fstatSync(fd).size,
bytes = (size > max_buffer_size) ? max_buffer_size : size,
buffer = new Buffer(bytes),
ret;
// some files dont register a byte so they need a special place otherwise
// fs.read gets angry
if(bytes === 0){
finish(crc.crc32(fs.readFileSync(filename, 'utf8')));
} else {
fs.read(fd, buffer, 0, bytes, 0, function(err, bytesRead, buffer){
finish(crc.crc32(buffer.toString('utf8')));
});
}
function finish(ret){
if(!!fn){
fs.closeSync(fd);
fn(null, ret);
}
}
}
function filter_duplicate_files(arr2d, fn){
var arr, crc, filename,
buff = {},
dupes,i;
// create the duplicate file array
for (i = arr2d.length - 1; i >= 0; i--) {
arr = arr2d[i];
filename = arr[0];
crc = arr[1];
// Try to add the crc to the object and if it already exists that means
// there is a duplicate file.
if(!!buff[crc]){
buff[crc].push(filename);
} else {
buff[crc] = [filename];
}
}
// clean out any crc's with only 1 file
dupes = _.reject(buff, function(arr, key){
return (arr.length == 1);
});
if(!!fn){
fn(null, dupes);
}
}
/**
* Readdir wrapper that adds full path and checks for trailing slash.
* @param {String} path The path to the dir to read.
* @param {Function} fn Callback
* @return {[type]} [description]
*/
function _readdir(path, fn){
var last_path_char = path.charAt(path.length-1);
path = (last_path_char === '/' || last_path_char === '\\') ? path : path+'/';
fn = fn || function(){};
fs.readdir(path, function(err, paths){
for (var i = paths.length - 1; i >= 0; i--) {
paths[i] = path + paths[i];
}
async.map(paths, readdir_fork, function(){
if(a_dirs.length > 0){
_readdir(a_dirs.pop(), fn);
} else {
fn();
}
});
});
}
function readdir_fork(path, fn){
fn = fn || function(){};
fs.stat(path, function(err, stat){
if(!stat){
fn();
return;
}
if(stat.isDirectory()){
a_dirs.push(path);
} else if(stat.isFile()){
var file = new DFFile(path, stat);
a_files.push(file);
} else {
console.log('Unknown type:', stat); //XXX
}
fn();
});
}
function get_files(path){
// console.log('get_files'); //XXX
var tk = new Tracker();
_readdir(path, tk.done);
return tk;
}
function Tracker(){
var count = 0,
call_count = 0,
self = this,
time = new Date().getTime(),
ondone = function(){
console.log(time,'ondone never set.'); //XXX
};
// console.log('tracker', time); //XXX
this.done = function(){
count++;
// console.log(count, call_count); //XXX
if(count >= call_count){
// console.log('complete', time); //XXX
process.nextTick(ondone);
return;
}
};
this.on = function(){
};
this.ondone = function(fn){
ondone = fn;
};
this.setCount = function(arg){
call_count = arg;
};
this.then = function(fn){
};
}
/*
* Main
*/
var args = parse_arguments(),
duplicate_files = [],
a_files = [],
a_dirs = [];
if(args.dirs.length >= 1){
async.series([
/*
Create an array of all the files in our search path.
*/
function(fn){
// todo scale to multiple dirs
get_files(args.dirs[0]).ondone(function(){
// dont do anything until there are no more directories to be parsed
if(a_dirs.length > 0){ return; }
// console.log('files:', a_files); //XXX
// console.log('dirs:', a_dirs); //XXX
// console.log(a_files.length); //XXX
fn();
});
},
/*
Calcualte the CRC of each of the files.
*/
function(fn){
async.map(a_files, get_crc_key, function(err, results){
var arr = _.zip(a_files,results);
a_files = arr;
// console.log(arr); //XXX
fn();
});
},
/*
Remove non-duplicates
*/
function(fn){
filter_duplicate_files(a_files, function(err, results){
duplicate_files = results;
fn();
});
}
],
print_duplicates);
}