-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
223 lines (187 loc) · 5.72 KB
/
index.js
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
#!/usr/bin/env node
var fs = require('fs');
var tingo = require('tingodb')();
var archiver = require('archiver');
var md5 = require('md5-file');
var program = require('commander');
var dup_catalog = '.jagarchiver_dup_catalog';
var src_dir = '/home/arnaud/www/';
var min_meta_compress_size_treshold = 2048;
program
.command('c')
.description('Create an archive')
.action(function(archive, dir) {
console.log('compress ' + archive);
console.log(' dir ' + dir);
compress(archive, dir);
});
program
.command('x')
.description('Decompress an archive')
.action(function(archive) {
decompress(archive);
});
program
.command('m')
.description('Perform meta decompression')
.action(function(dir) {
dir = normalize_dir(dir);
meta_decompress(dir, dup_catalog);
});
program.parse(process.argv);
var archive_file = program.args.unshift();;
var files = program.args;
function compress(archive_file, dir) {
dir = normalize_dir(dir);
var output = fs.createWriteStream(archive_file);
var archive = archiver('tar', {
gzip: true,
gzipOptions: {
level: 1
}
});
output.on('close', function() {
console.log(archive.pointer() + ' total bytes');
console.log('archiver has been finalized and the output file descriptor has closed.');
});
archive.pipe(output);
var file_by_size = {};
var collisions = {};
var dirs = [];
var files = [];
// List all files in a directory in Node.js recursively in a synchronous fashion
function walkSync(dir) {
dirs.push(dir);
var dir_files = fs.readdirSync(dir);
dir_files.forEach(function(file) {
if (fs.statSync(dir + file).isDirectory()) {
walkSync(dir + file + '/');
} else {
var stats = fs.statSync(dir + file);
var size = stats.size;
files.push({ name: dir + file, size: size});
if (typeof file_by_size[ size ] == 'undefined') {
file_by_size[ size ] = [ dir + file ];
} else {
file_by_size[ size ].push(dir + file);
}
}
});
};
walkSync(dir);
for (var size in file_by_size) {
var list = file_by_size[ size ];
var file_by_hash = {};
// don't compute md5 if only one file of this side (no collision possible)
if (list.length == 1) {
file_by_size[ size ] = undefined;
}
list.forEach(function(name){
var md5sum = md5(name);
if (typeof file_by_hash[md5sum] != 'undefined') {
file_by_hash[md5sum].push(name);
} else {
file_by_hash[md5sum] = [name];
}
});
file_by_size[ size ] = file_by_hash;
}
var catalog_file = fs.createWriteStream(dup_catalog);
dirs.forEach(function(dir){
catalog_file.write('DIR' + '|' + dir + '|' + dir + '|' + 0 + "\n");
});
files.forEach(function(file_data){
var name = file_data.name;
var size = file_data.size;
var list = file_by_size[ size ];
if (typeof list == 'undefined') {
archive.file(name, {name: name})
}
var md5sum = md5(name);
if (typeof list[md5sum] != 'undefined' && list[md5sum].length > 1) {
console.log( 'list[md5sum[0]]=' + list[ md5sum ][0]);
if (list[ md5sum ][0] == name) {
console.log("Adding source of dulicates");
// First file with this md5 => add to archive
archive.file(name, {name: name})
} else {
// Collision => write to catalog
console.log("Duplicate detected");
catalog_file.write('DUP_FILE' + '|' + list[md5sum][0] + '|' + name + '|' + size + "\n");
// console.log('Collision of size ' + size + ' for ' + name + ' <=> ' + list[ md5sum ].join(', '));
}
} else {
archive.file(name, {name: name})
}
});
catalog_file.end();
archive.file(dup_catalog, {name: dup_catalog})
archive.finalize();
};
function decompress(archive_file) {
console.log('Uncompressing');
const extract = require('extract-archive');
extract(archive_file, './').then(function (dirPath) {
meta_decompress('./', dup_catalog);
// remove catalog_file
console.log('done');
});
}
function meta_decompress (dir, catalog_file) {
var array = fs.readFileSync( dir + catalog_file).toString().split("\n");
for(i in array) {
// array[i].slice(-1, '');
var file = array[i].split('|');
// Type of meta action to process
var type = file[0];
var src = normalize_path(dir + file[1]);
var dst = normalize_path(dir + file[2]);
if (type == 'DUP_FILE') {
try {
fs.accessSync(src, fs.F_OK);
copyFileSync(src, dst);
console.log('OK ' + src + ' => ' + dst);
} catch (e) {
console.log('!! ' + src + ' => ' + dst + '(' + e + ')');
// It isn't accessible
}
} else if (type == 'DIR') {
try {
fs.mkdirSync(src);
console.log('DIR ' + src + ' created');
} catch (e) {
if ( e.code != 'EEXIST' ) {
console.log('Dir error ' + e );
}
}
} else {
console.log('Unknown type of meta action (' + type + ')');
}
}
}
function normalize_dir (dir) {
var last = dir.slice(-1);
if (last != '/') {
return dir + '/';
}
return dir;
}
function normalize_path(path) {
var result = path.replace(/\/\//, '\/');
return result;
}
function copyFileSync (srcFile, destFile) {
const BUF_LENGTH = 64*1024;
var buff = new Buffer(BUF_LENGTH);
var fdr = fs.openSync(srcFile, 'r');
var fdw = fs.openSync(destFile, 'w');
bytesRead = 1;
pos = 0;
while (bytesRead > 0) {
bytesRead = fs.readSync(fdr, buff, 0, BUF_LENGTH, pos);
fs.writeSync(fdw,buff,0,bytesRead);
pos += bytesRead;
}
fs.closeSync(fdr);
fs.closeSync(fdw);
}