-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathverify-transfer
executable file
·148 lines (122 loc) · 3.65 KB
/
verify-transfer
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
#!/usr/bin/env node
if (process.argv.includes('-h') || process.argv.includes('--help')) {
usage(0);
}
const [ server1, dir1 ] = splitLocation(process.argv[2]);
const [ server2, dir2 ] = splitLocation(process.argv[3]);
if (!dir1 || !dir2) {
usage(1);
}
const childProcess = require('child_process');
const ProgressBar = require('progress');
main();
// Function definitions
function usage(exitCode = 0) {
console.log(`Compare all files within two directories.
This will display the filenames of any files that do not have the same hash.
A location can be local or remote (e.g. [email protected]:~/my/remote/file ~/my/local/file)
SSH will be used for remote files
Usage: ${process.argv[1]} <location1> <location2>
`);
process.exit(exitCode);
}
function splitLocation(location = '') {
location = location.replace(/ /g, '\\ ');
if (!~location.indexOf(':')) {
return [ '', location ] ;
}
return location.split(/:(.+)/);
}
function spawn(server, dir, cmd) {
let args, opts = {};
if (server) {
args = [ server, `cd ${dir} && ${cmd.replace(';', '\\;')}` ];
cmd = 'ssh';
} else {
args = cmd.split(' ');
cmd = args.shift();
opts.cwd = dir;
}
return childProcess.spawn(cmd, args, opts);
}
function fetchNumFiles(server, dir) {
return new Promise((resolve, reject) => {
let child = spawn(server, dir, 'find . -type f');
let numFiles = 0;
child.on('error', error => reject(error));
child.stderr.on('data', error => reject(`Cannot find directory ${server}:${dir}\n${error}`));
child.stdout.on('data', data => {
numFiles += `${data}`.split(/\n/).filter(line => line).length;
});
child.on('close', () => resolve(numFiles));
});
}
function fetchHashes(server, dir, progress) {
return new Promise((resolve, reject) => {
let hashes = {};
let child = spawn(server, dir, 'find . -type f -exec md5sum {} ;');
child.on('error', error => reject(error));
child.stdout.on('data', data => {
let lines = `${data}`.split('\n');
for (let line of lines) {
let [ hash, filename ] = line.split(/ +(.+)/);
if (filename) {
progress.tick(1);
hashes[filename.substr(2)] = hash;
}
}
});
child.on('close', () => resolve(hashes));
});
}
function hashDiff([ left, right ]) {
let diffs = { '<': [], '~': [], '>': [] };
for (let filename of Object.keys(left).sort()) {
if (left[filename] !== right[filename]) {
diffs[right[filename] ? '~' : '<'].push(filename);
}
delete right[filename];
}
diffs['>'] = Object.keys(right).sort();
return [ '<', '~', '>' ].reduce(
(all, type) => all.concat(diffs[type].map(filename => `${type} ${filename}`)),
[]
);
}
function forBoth(fn, ...extraArgs) {
return Promise.all([
fn(server1, dir1, ...extraArgs),
fn(server2, dir2, ...extraArgs),
]);
}
function main() {
console.log('Scanning for files...');
forBoth(fetchNumFiles)
.then(numFiles => {
return new ProgressBar('Calculating hashes (:bar) :percent :etas', {
complete: '.',
incomplete: ' ',
width: 20,
total: numFiles.reduce((sum, num) => sum + num, 0)
});
})
.then(progress => forBoth(fetchHashes, progress))
.then(hashDiff)
.then(differences => {
console.log(); // Ensure progress bar has a newline
if (differences.length) {
console.log(`Found ${differences.length} differences:`);
for (let difference of differences) {
console.log(difference);
}
process.exit(1);
} else {
console.log('Found no differences!');
process.exit(0);
}
})
.catch(error => {
console.log(error.stack || error);
process.exit(1);
});
}