This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dumplist.js
508 lines (395 loc) · 14 KB
/
dumplist.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#!/usr/bin/env node
/* eslint-disable class-methods-use-this, no-await-in-loop, no-continue, no-new, no-process-exit */
/**
* Usage: node dumplist.js [--check|--test|--generate|--update|--touchdir]
*
* Generates and works with a custom SHA256SUMS file
* for listing and hashing contents of a directory.
*
* @Author Jorge Oliveira (NewEraCracker)
* @Date Dec 8th 2021
* @License Public Domain
* @Version 0.3.6-node
*/
const [crypto, fs, { promisify }] = [require('crypto'), require('fs'), require('util')];
const [access, readdir, readFile, stat, utimes, writeFile] = [promisify(fs.access), promisify(fs.readdir), promisify(fs.readFile), promisify(fs.stat), promisify(fs.utimes), promisify(fs.writeFile)];
const die = (mssg) => {
console.error(mssg);
process.exit(1);
};
const file_exists = (path) => {
return access(path).then(() => true, () => false);
};
const is_writable = (path) => {
return access(path, fs.constants.W_OK).then(() => true, () => false);
};
const filemtime = async (path) => {
const stats = await stat(path);
return Math.floor(stats.mtimeMs / 1000);
};
const md5_file = (filename) => {
return new Promise((resolve, reject) => {
const [output, input] = [crypto.createHash('md5'), fs.createReadStream(filename)];
input.on('error', (err) => {
reject(err);
});
output.once('readable', () => {
resolve(output.read().toString('hex'));
});
input.pipe(output);
});
};
const sha1_file = (filename) => {
return new Promise((resolve, reject) => {
const [output, input] = [crypto.createHash('sha1'), fs.createReadStream(filename)];
input.on('error', (err) => {
reject(err);
});
output.once('readable', () => {
resolve(output.read().toString('hex'));
});
input.pipe(output);
});
};
const parity_file = (filename) => {
return Promise.all([
md5_file(filename),
sha1_file(filename)
]).then(
(values) => {
return Buffer.from((values[0] + values[1]), 'hex')
.toString('base64')
.replace(/=/g, '')
.replace(/\+/g, '-')
.replace(/\//g, '_');
},
(rejection) => {
throw rejection;
}
);
};
const sha256_file = (filename) => {
return new Promise((resolve, reject) => {
const [output, input] = [crypto.createHash('sha256'), fs.createReadStream(filename)];
input.on('error', (err) => {
reject(err);
});
output.once('readable', () => {
resolve(output.read().toString('hex'));
});
input.pipe(output);
});
};
const substr_count = (str, char) => {
let cnt = 0;
for (let i = 0; i < str.length; i++) {
if (str[i] === char) { ++cnt; }
}
return cnt;
};
const { basename, dirname } = require('path');
/** Utility static methods for dump listing */
class NewEra_DumpListUtil {
/**
* This will parse a listfile
*/
async parse_listfile(filename) {
const [fileproperties, comment, content] = [{}, { mtime: [], parity: [], name: [] }, { sha256: [], name: [] }];
if (!await file_exists(filename)) {
console.error('Error parsing listfile: File does not exist');
return false;
}
const filecontents = await readFile(filename, { encoding: 'utf8' });
filecontents.replace(/^; ([0-9]+) ([\w\d_-]{48}) ([*][^\r\n]+)/gm, (...m) => {
comment.mtime.push(m[1]);
comment.parity.push(m[2]);
comment.name.push(m[3]);
});
if (!comment.mtime.length) {
console.error('Error parsing listfile: Unable to parse comments');
return false;
}
filecontents.replace(/^([0-9a-f]{64}) ([*][^\r\n]+)/gm, (...m) => {
content.sha256.push(m[1]);
content.name.push(m[2]);
});
if (!content.sha256.length) {
console.error('Error parsing listfile: Unable to parse contents');
return false;
}
if (comment['name'].length === content['name'].length) {
for (let i = 0; i < comment['name'].length; i++) {
if (comment['name'][i][0] == '*' && comment['name'][i] === content['name'][i]) {
// Not an hack: We have to remove the asterisk in begining and restore ./ in path for Node.js to be able to work it out
const file = './' + comment['name'][i].substr(1);
fileproperties[`${file}`] = {
'mtime': comment['mtime'][i],
'parity': comment['parity'][i],
'sha256': content['sha256'][i]
};
} else {
console.error('Error parsing listfile: Invalid entry order');
return false;
}
}
} else {
console.error('Error parsing listfile: Invalid entry count');
return false;
}
return fileproperties;
}
/** This will generate a listfile */
generate_listfile(fileproperties) {
// Init contents of list file
let [comment, content] = ['', ''];
// Sort file properties array and walk
for (const file of Object.keys(fileproperties).sort(NewEra_Compare.prototype.sort_files_by_name)) {
const properties = fileproperties[`${file}`];
// Not an hack: We have to replace ./ in path by an asterisk for other applications (QuickSFV, TeraCopy...) to be able to work it out
const filename = '*' + file.substr(2);
comment += `; ${properties['mtime']} ${properties['parity']} ${filename}\n`;
content += properties['sha256'] + ' ' + filename + "\n";
}
return (comment + content);
}
/** Array with the paths a dir contains */
async readdir_recursive(dir = '.', show_dirs = false, ignored = []) {
// Set types for stack and return value
const [stack, result] = [[], []];
// Initialize stack
stack.push(dir);
// Pop the first element of stack and evaluate it (do this until stack is fully empty)
while (dir = stack.shift()) { // eslint-disable-line no-cond-assign
const files = await readdir(dir);
for (let path of files) {
// Prepend dir to current path
path = dir + '/' + path;
// Stat the path to determine attributes
const stats = await stat(path).catch(e => {
console.error(e);
return { isDirectory: () => false, isFile: () => false };
});
if (stats.isDirectory()) {
// Check ignored dirs
if (Array.isArray(ignored) && ignored.length && ignored.indexOf(path + '/') !== -1) { continue; }
// Add dir to stack for reading
stack.push(path);
// If show_dirs is true, add dir path to result
if (show_dirs) { result.push(path); }
} else if (stats.isFile()) {
// Check ignored files
if (Array.isArray(ignored) && ignored.length && ignored.indexOf(path) !== -1) { continue; }
// Add file path to result
result.push(path);
}
}
}
// Sort the array using simple ordering
result.sort();
// Now we can return it
return result;
}
}
/* Useful comparators */
class NewEra_Compare {
/* Ascending directory sorting by names */
sort_files_by_name(a, b) {
/* Equal */
if (a == b) { return 0; }
/* Let strcmp decide */
return ((a > b) ? 1 : -1);
}
/* Ascending directory sorting by levels and names */
sort_files_by_level_asc(a, b) {
/* Equal */
if (a == b) { return 0; }
/* Check dir levels */
const la = substr_count(a, '/');
const lb = substr_count(b, '/');
/* Prioritize levels, in case of equality let sorting by names decide */
return ((la < lb) ? -1 : ((la == lb) ? NewEra_Compare.prototype.sort_files_by_name(a, b) : 1));
}
/* Reverse directory sorting by levels and names */
sort_files_by_level_dsc(a, b) {
return NewEra_Compare.prototype.sort_files_by_level_asc(b, a);
}
}
/** Methods used in dump listing */
class NewEra_DumpList {
/** Construct the object and perform actions */
constructor(listfile = './SHA256SUMS', ignored = []) {
/** The file that holds the file list */
this.listfile = listfile;
/** Ignored paths */
this.ignored = [
listfile, /* List file */
...ignored /* Original ignored array */
];
/** Simple file list array */
this.filelist = [];
/** Detailed file list array */
this.fileproperties = [];
// Check arguments count
if (process.argv.length != 3) {
die('Usage: node ' + basename(__filename) + " [--check|--test|--generate|--update|--touchdir]");
}
// Fix argument
const argument = process.argv[2].replace(/^[-]{1,2}/g, '');
// Process arguments
switch (argument) {
case 'test':
this.dumplist_update(true, true, true);
break;
case 'check':
this.dumplist_update(false, false, true);
break;
case 'generate':
this.dumplist_generate();
break;
case 'update':
this.dumplist_update(false, false, false);
break;
case 'touchdir':
this.dumplist_touchdir();
break;
default:
die('Usage: node ' + basename(__filename) + " [--check|--test|--generate|--update|--touchdir]");
}
}
/** Update dump file listing / Run the check on each file */
async dumplist_update(testsha256 = false, testparity = false, dryrun = false) {
this.filelist = await NewEra_DumpListUtil.prototype.readdir_recursive('.', false, this.ignored);
this.fileproperties = await NewEra_DumpListUtil.prototype.parse_listfile(this.listfile);
if (!this.fileproperties) { return; }
for (const file of this.filelist) {
// Handle creation case
if (!this.fileproperties.hasOwnProperty(file)) {
console.log(`${file} is a new file.`);
try {
if (!dryrun) this.fileproperties[`${file}`] = {
'mtime': await filemtime(file),
'parity': await parity_file(file),
'sha256': await sha256_file(file)
};
} catch (e) { console.error(e); }
continue;
}
}
// Save the keys to remove in case there is file deletion
const keys_to_remove = [];
// Handle each file in the properties list
for (const file of Object.keys(this.fileproperties)) {
const properties = this.fileproperties[`${file}`];
// Handle deletion (Save it, will delete the keys later)
if (!await file_exists(file)) {
console.log(`${file} does not exist.`);
if (!dryrun) keys_to_remove.push(file);
continue;
}
// Handle file modification
if (dryrun && await filemtime(file) != properties['mtime']) {
console.log(`${file} was modified.`);
continue;
}
// Test file parity if required
if (testparity) {
const parity = await parity_file(file);
if (parity != properties['parity']) {
console.log(`${file} Expected parity: ${properties['parity']} Got: ${parity}.`);
if (!dryrun) console.error(new Error('Parity mismatch, skiping update').stack);
continue;
}
}
// Test file sha256 if required
if (testsha256) {
const sha256 = await sha256_file(file);
if (sha256 != properties['sha256']) {
console.log(`${file} Expected sha256: ${properties['sha256']} Got: ${sha256}.`);
if (!dryrun) console.error(new Error('SHA256 mismatch, skiping update').stack);
continue;
}
}
// Handle file modification
if (!dryrun && await filemtime(file) != properties['mtime']) {
console.log(`${file} was modified.`);
try {
this.fileproperties[`${file}`] = {
'mtime': await filemtime(file),
'parity': await parity_file(file),
'sha256': await sha256_file(file)
};
} catch (e) { console.error(e); }
continue;
}
}
if (!dryrun) {
// Handle deletion (Delete the keys now)
if (keys_to_remove.length > 0) {
for (const key of keys_to_remove) {
this.fileproperties[key] = null;
delete this.fileproperties[key];
}
}
const contents = NewEra_DumpListUtil.prototype.generate_listfile(this.fileproperties);
await writeFile(this.listfile, contents);
}
}
/** Generate dump file listing */
async dumplist_generate() {
this.filelist = await NewEra_DumpListUtil.prototype.readdir_recursive('.', false, this.ignored);
this.fileproperties = {};
for (const file of this.filelist) {
try {
this.fileproperties[`${file}`] = {
mtime: await filemtime(file),
parity: await parity_file(file),
sha256: await sha256_file(file)
};
} catch (e) { console.error(e); }
}
const contents = NewEra_DumpListUtil.prototype.generate_listfile(this.fileproperties);
await writeFile(this.listfile, contents);
}
async dumplist_touchdir() {
// Filelist including directories
const list = await NewEra_DumpListUtil.prototype.readdir_recursive('.', true, this.ignored);
// Easier with a bottom to top approach
list.sort(NewEra_Compare.prototype.sort_files_by_level_dsc);
// Handle list including directories. Then run
// another pass with list without directories
for (let i = 0; i < 2; i++) {
// Reset internal variables state
let [dir, time] = [null, null];
// Handle list
for (const file of list) {
// Ignore dir dates on pass two
if (i === 1 && (await stat(file)).isDirectory()) {
continue;
}
// Blacklist certain names
if (file.toLowerCase().indexOf('/desktop.ini') !== -1 || file.indexOf('/.') != -1) {
continue;
}
// Reset internal variables state when moving to another dir
if (dir !== dirname(file)) {
dir = dirname(file);
time = 0;
}
// Save current time
const mtime = await filemtime(file);
// Only update when mtime is correctly set and higher than time
// Also check for writability to prevent errors
if (mtime > 0 && mtime > time && is_writable(dir)) {
// Save new timestamp
time = mtime;
// Update timestamp
await utimes(dir, time, time);
}
}
}
// I think we should be OK
return true;
}
}
/** Run */
new NewEra_DumpList('./SHA256SUMS', ['./.htaccess', './.htpasswd']);