-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhash-files.js
92 lines (78 loc) · 1.98 KB
/
hash-files.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
"use strict";
let crypto = require("crypto");
let fs = require("fs");
let path = require("path");
module.exports = hashFiles;
class Hash {
constructor() {
this.hash = crypto.createHash("sha512");
}
updateBuffer(buf) {
this.hash.update(buf);
}
updateString(s) {
this.hash.update(new Buffer(s));
}
updateNumber(n) {
// We would prefer to use Buffer.allocUnsafe here and below, but it isn't
// available in Node 4.0.0.
let buf = new Buffer(8);
buf.writeDoubleLE(n, 0);
this.hash.update(buf);
}
updateUInt48(n) {
let buf = new Buffer(6);
buf.writeUIntLE(n, 0, 6, true);
this.hash.update(buf);
}
updateUInt32(n) {
let buf = new Buffer(4);
buf.writeUInt32LE(n, 0, true);
this.hash.update(buf);
}
updateUInt16(n) {
let buf = new Buffer(2);
buf.writeUInt16LE(n, 0, true);
this.hash.update(buf);
}
updateUInt8(n) {
let buf = new Buffer(1);
buf.writeUInt8(n, 0, true);
this.hash.update(buf);
}
digest() {
return this.hash.digest("hex");
}
}
function hashFiles(fullPaths) {
let hash = new Hash();
for (let i = 0; i < fullPaths.length; i++) {
updateHash(hash, fullPaths[i]);
}
return hash.digest();
}
const FILE_TAG = 0;
const DIR_TAG = 1;
// We do not currently support depending on non-existent files
// const NOT_FOUND_TAG = 2;
function updateHash(hash, fullPath) {
let stats = fs.statSync(fullPath);
if (stats.isDirectory()) {
hash.updateUInt8(DIR_TAG);
let entries;
entries = fs.readdirSync(fullPath).sort();
hash.updateNumber(entries.length);
for (let i = 0; i < entries.length; i++) {
hash.updateString(entries[i]);
hash.updateUInt8(0);
updateHash(hash, fullPath + path.sep + entries[i]);
}
} else if (stats.isFile()) {
hash.updateUInt8(FILE_TAG);
hash.updateUInt16(stats.mode);
hash.updateUInt48(stats.mtime.getTime());
hash.updateNumber(stats.size);
} else {
throw new Error("Unexpected file type: " + fullPath);
}
}