-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (42 loc) · 1.24 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
var fstream = require("fstream");
var tar = require("tar");
var fs = require("fs");
var path = require("path");
var toTar = path.join(__dirname, "example");
var bad = process.argv[2] === "bad";
var untar = function (tarball) {
console.log("Untarring", tarball);
var buf = fs.readFileSync(outFile);
var dest = path.join(__dirname, "extracted-" + Math.random());
fs.mkdirSync(dest);
var extractor = new tar.Extract({ path: dest });
extractor.on("error", function (err) {
console.log("Untar error:", err);
process.exit(1);
});
extractor.on("end", function () {
console.log("Untarred without errors to", dest);
process.exit(0);
});
extractor.write(buf);
extractor.end();
};
console.log("Making example/f not writeable");
fs.chmodSync(path.join(toTar, "f"), 0444);
var reader;
if (bad) {
reader = fstream.Reader({ path: toTar, type: 'Directory' });
} else {
reader = fstream.Reader(toTar);
}
var outFile = path.join(__dirname, "out.tgz");
console.log("Tarring to", outFile);
var out = fstream.Writer({ path: outFile });
out.on("close", function () {
untar(outFile);
});
out.on("error", function (err) {
console.log("Tar error:", err);
process.exit(1);
});
reader.pipe(tar.Pack({ noProprietary: true })).pipe(out);