-
Notifications
You must be signed in to change notification settings - Fork 0
/
tar.ts
337 lines (308 loc) · 7.85 KB
/
tar.ts
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
/**
* Tar implementation ported and modified from https://github.com/beatgammit/tar-js
* under The MIT License
*
* Copyright (c) 2011 T. Jameson Little
* Copyright (c) 2019 Jun Kato
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import { Uint8ArrayReader } from "./io.ts";
import { MultiReader } from "./deps.ts";
const recordSize = 512;
/*
struct posix_header { // byte offset
char name[100]; // 0
char mode[8]; // 100
char uid[8]; // 108
char gid[8]; // 116
char size[12]; // 124
char mtime[12]; // 136
char chksum[8]; // 148
char typeflag; // 156
char linkname[100]; // 157
char magic[6]; // 257
char version[2]; // 263
char uname[32]; // 265
char gname[32]; // 297
char devmajor[8]; // 329
char devminor[8]; // 337
char prefix[155]; // 345
// 500
};
*/
type TarStructure = { field: string; length: number }[];
const structure: TarStructure = [
{
field: "fileName",
length: 100
},
{
field: "fileMode",
length: 8
},
{
field: "uid",
length: 8
},
{
field: "gid",
length: 8
},
{
field: "fileSize",
length: 12
},
{
field: "mtime",
length: 12
},
{
field: "checksum",
length: 8
},
{
field: "type",
length: 1
},
{
field: "linkName",
length: 100
},
{
field: "ustar",
length: 8
},
{
field: "owner",
length: 32
},
{
field: "group",
length: 32
},
{
field: "majorNumber",
length: 8
},
{
field: "minorNumber",
length: 8
},
{
field: "filenamePrefix",
length: 155
},
{
field: "padding",
length: 12
}
];
export interface TarData {
fileName: string;
fileMode: string;
uid: string;
gid: string;
fileSize: string;
mtime: string;
checksum: string;
type: string;
ustar: string;
owner: string;
group: string;
/**
* file to read
*/
filePath: string;
/**
* buffer to read
*/
reader: Deno.Reader;
}
export interface TarOptions {
/**
* append file
*/
filePath?: string;
/**
* append any arbitrary content
*/
reader?: Deno.Reader;
/**
* size of the content to be appended
*/
contentSize?: number;
mode?: number;
mtime?: number;
uid?: number;
gid?: number;
owner?: string;
group?: string;
}
export class FileReader implements Deno.Reader {
private filePath: string;
private file: Deno.File;
constructor(filePath: string) {
this.filePath = filePath;
}
public async read(p: Uint8Array) {
if (!this.file) {
this.file = await Deno.open(this.filePath, "r");
}
const res = await Deno.read(this.file.rid, p);
if (res.eof) {
await Deno.close(this.file.rid);
this.file = null;
}
return res;
}
}
export class FileWriter implements Deno.Writer {
private filePath: string;
private file: Deno.File;
constructor(filePath: string) {
this.filePath = filePath;
}
public async write(p: Uint8Array) {
if (!this.file) {
this.file = await Deno.open(this.filePath, "w");
}
return Deno.write(this.file.rid, p);
}
public async dispose() {
if (!this.file) return;
Deno.close(this.file.rid);
this.file = null;
}
}
export class Tar {
data: TarData[];
written: number;
out: Uint8Array;
private blockSize: number;
constructor(recordsPerBlock?: number) {
this.data = [];
this.written = 0;
this.blockSize = (recordsPerBlock || 20) * recordSize;
this.out = clean(this.blockSize);
}
async append(fileName: string, opts: TarOptions) {
opts = opts || {};
const info = opts.filePath && (await Deno.stat(opts.filePath));
let mode = opts.mode || (info && info.mode) || parseInt("777", 8) & 0xfff,
mtime =
opts.mtime ||
(info && info.modified) ||
Math.floor(new Date().getTime() / 1000),
uid = opts.uid || 0,
gid = opts.gid || 0;
const tarData = {
fileName,
fileMode: pad(mode, 7),
uid: pad(uid, 7),
gid: pad(gid, 7),
fileSize: pad(info ? info.len : opts.contentSize, 11),
mtime: pad(mtime, 11),
checksum: " ",
type: "0", // just a file
ustar: "ustar\u000000\u0000",
owner: opts.owner || "",
group: opts.group || "",
filePath: opts.filePath,
reader: opts.reader
} as TarData;
// calculate the checksum
let checksum = 0;
const encoder = new TextEncoder();
Object.keys(tarData)
.filter(key => ["filePath", "reader"].indexOf(key) < 0)
.forEach(function(key) {
checksum += encoder.encode(tarData[key]).reduce((p, c) => p + c, 0);
});
tarData.checksum = pad(checksum, 6) + "\u0000 ";
this.data.push(tarData);
}
getReader() {
const readers: Deno.Reader[] = [];
this.data.forEach(tarData => {
let { filePath, reader } = tarData,
headerArr = format(tarData);
readers.push(new Uint8ArrayReader(headerArr));
if (!reader) {
reader = new FileReader(filePath);
}
readers.push(reader);
// to the nearest multiple of recordSize
readers.push(
new Uint8ArrayReader(
clean(
recordSize -
(parseInt(tarData.fileSize, 8) % recordSize || recordSize)
)
)
);
});
// append 2 empty records
readers.push(new Uint8ArrayReader(clean(recordSize * 2)));
return new MyMultiReader(...readers);
}
}
/** Reader utility for combining multiple readers */
export class MyMultiReader implements Deno.Reader {
private readonly readers: Deno.Reader[];
private currentIndex = 0;
constructor(...readers: Deno.Reader[]) {
this.readers = readers;
}
async read(p: Uint8Array): Promise<Deno.ReadResult> {
const r = this.readers[this.currentIndex];
if (!r) {
return { nread: 0, eof: true };
}
const { nread, eof } = await r.read(p);
if (eof) {
this.currentIndex++;
if (nread === 0) {
// don't return { nread: 0, eof: false } which is actually treated as { eof: true }
return this.read(p);
}
}
return { nread, eof: false };
}
}
function format(data: TarData) {
const encoder = new TextEncoder(),
buffer = clean(512);
let offset = 0;
structure.forEach(function(value) {
const entry = encoder.encode(data[value.field] || "");
buffer.set(entry, offset);
offset += value.length; // space it out with nulls
});
return buffer;
}
function clean(length: number) {
const buffer = new Uint8Array(length);
buffer.fill(0, 0, length - 1);
return buffer;
}
function pad(num: number, bytes: number, base?: number) {
var numString = num.toString(base || 8);
return "000000000000".substr(numString.length + 12 - bytes) + numString;
}