-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaf
executable file
·114 lines (101 loc) · 3.31 KB
/
saf
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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const SIGNATURE = Buffer.from([0xab, 0x53, 0x41, 0x46, 0x20, 0x31, 0x30, 0xbb, 0x0d, 0x0a, 0x1a, 0x0a]);
function processFile(file) {
const stat = fs.statSync(file);
if (stat.isFile()) {
return [{ path: file, size: stat.size }];
} else if (stat.isDirectory()) {
return fs.readdirSync(file)
.map(subfile => path.join(file, subfile))
.flatMap(subfile => processFile(subfile));
} else {
console.log(`Warning: skipping ${file}, because it is neither a regular file nor a directory`);
}
}
function readManifest(fd) {
const signature = Buffer.alloc(12);
fs.readSync(fd, signature);
if (!signature.equals(SIGNATURE)) {
throw new Error('Incorrect file signature');
}
const manifestSize = Buffer.alloc(4);
fs.readSync(fd, manifestSize);
const manifest = Buffer.alloc(manifestSize.readUInt32LE());
fs.readSync(fd, manifest);
return JSON.parse(manifest.toString());
}
function pack(output, files) {
const manifest = files.flatMap(file => processFile(path.basename(path.resolve(file))));
const manifestEncoded = Buffer.from(JSON.stringify(manifest));
const manifestSize = Buffer.alloc(4);
manifestSize.writeUInt32LE(manifestEncoded.length);
fs.writeFileSync(output, SIGNATURE);
fs.appendFileSync(output, manifestSize);
fs.appendFileSync(output, manifestEncoded);
for (const file of manifest) {
fs.appendFileSync(output, fs.readFileSync(file.path));
}
}
function unpack(files) {
for (const file of files) {
const fd = fs.openSync(file);
try {
const manifest = readManifest(fd);
console.log(`${file}:`);
for (const file of manifest) {
console.log(file.path);
fs.mkdirSync(path.dirname(file.path), { recursive: true });
const fileData = Buffer.alloc(file.size);
fs.readSync(fd, fileData);
fs.writeFileSync(file.path, fileData);
}
} catch (e) {
console.log(`Error: ${file} is corrupted or not a SAF archive`);
}
fs.closeSync(fd);
}
}
function list(files) {
for (const file of files) {
const fd = fs.openSync(file);
try {
const manifest = readManifest(fd);
console.log(`${file}:`);
for (const file of manifest) {
console.log(file.path);
}
} catch (e) {
console.log(`Error: ${file} is corrupted or not a SAF archive`);
}
fs.closeSync(fd);
}
}
function usage() {
console.log('Usage:');
console.log(' saf pack <outfile> [<infile>...]');
console.log(' saf unpack [<infile>...]');
console.log(' saf list [<infile>...]');
}
if (process.argv.length <= 2) {
usage();
process.exit(1);
}
const command = process.argv[2];
const files = process.argv.slice(3);
if (command === 'pack') {
if (files.length === 0) {
usage();
process.exit(1);
}
const output = files[0];
const infiles = files.slice(1);
pack(output, infiles);
} else if (command === 'unpack') {
unpack(files);
} else if (command === 'list') {
list(files);
} else {
console.log(`Error: ${command} is not a valid command`);
}