@preventive/archive 1.3.0 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/compression.d.ts +50 -30
- package/compression.js +0 -5
- package/package.json +2 -1
- package/src/bytes.js +0 -2
- package/src/compression.js +36 -93
- package/src/entry.js +3 -9
- package/src/error.d.ts +10 -0
- package/src/error.js +4 -8
- package/src/names.js +30 -120
- package/src/tar/header.js +17 -43
- package/src/tar/pack.js +33 -48
- package/src/tar/pax.js +6 -11
- package/src/tar/unpack.js +75 -132
- package/src/text.js +6 -32
- package/src/zip/format.js +10 -29
- package/src/zip/unzip.js +47 -93
- package/src/zip/zip.js +24 -32
- package/tar.d.ts +166 -137
- package/tar.js +2 -18
- package/zip.d.ts +106 -82
- package/zip.js +1 -19
package/src/names.js
CHANGED
|
@@ -1,95 +1,51 @@
|
|
|
1
|
-
// Name safety, applied before writing an entry and before handing one out
|
|
2
|
-
// of an archive. A name is a relative path: `.` segments and a directory's
|
|
3
|
-
// trailing slash are dropped, and what is left has no empty or `..`
|
|
4
|
-
// segment, no control character and no backslash -- a separator on
|
|
5
|
-
// Windows, where `..\` would get past the check on `..` -- and does not
|
|
6
|
-
// start with a drive letter and colon, which Windows resolves from that
|
|
7
|
-
// drive rather than from the archive. A symlink target may use `..`, but
|
|
8
|
-
// is followed from where the link sits and refused if it climbs above the
|
|
9
|
-
// archive or passes through anything but a directory, which is how a
|
|
10
|
-
// chain of links would climb. A caller may let a relative target climb up
|
|
11
|
-
// to a number of levels above the archive's root, as a directory archived
|
|
12
|
-
// on its own links to the files of the tree it was cut from: past the root
|
|
13
|
-
// the archive cannot tell where a target leads -- not even back into
|
|
14
|
-
// itself, by the name it is extracted under -- so what it passes through
|
|
15
|
-
// there is the caller's to know. An absolute target is refused regardless.
|
|
16
|
-
// A hard link to a symlink, directly or down a chain of hard links, is a
|
|
17
|
-
// second name for it, and is held to the same walk from where that name
|
|
18
|
-
// sits, since a target safe under `a/b/` need not be safe at the root.
|
|
19
|
-
// Lengths are bounded by what a filesystem takes at all: PATH_MAX for the
|
|
20
|
-
// whole as the archive stores it, a directory's slash counted, and NAME_MAX
|
|
21
|
-
// for a segment, in bytes.
|
|
22
|
-
//
|
|
23
|
-
// A name may repeat only as the same entry again, field for field and byte
|
|
24
|
-
// for byte (some npm packagers write `d/f` and `d/./f` both): two different
|
|
25
|
-
// entries under one name would leave the winner to extraction order.
|
|
26
|
-
// Collisions a filesystem might add -- case, Unicode normalisation -- are
|
|
27
|
-
// the filesystem's, not the archive's, and are not looked for.
|
|
28
|
-
|
|
29
1
|
import { sameBytes } from './bytes.js'
|
|
30
2
|
import { ArchiveError } from './error.js'
|
|
31
3
|
import { checkString, hasUnsafe, quote, tooLong } from './text.js'
|
|
32
4
|
|
|
33
|
-
const PATH_MAX = 4096
|
|
34
|
-
const NAME_MAX = 255
|
|
5
|
+
const PATH_MAX = 4096 // bytes of the name as stored, a directory's slash counted
|
|
6
|
+
const NAME_MAX = 255 // bytes of a segment
|
|
35
7
|
|
|
36
|
-
// The rules
|
|
37
|
-
// segments, split the once.
|
|
8
|
+
// The rules for any path, a symlink target's too
|
|
38
9
|
function checkText(path, what) {
|
|
39
10
|
checkString(path, what)
|
|
40
11
|
if (path === '') throw new ArchiveError(`${what} is empty`)
|
|
12
|
+
// A backslash is a separator on Windows, where `..\` would pass the `..` check
|
|
41
13
|
if (hasUnsafe(path, true)) throw new ArchiveError(`${what} ${quote(path)} holds a control or formatting character, or a backslash`)
|
|
42
14
|
if (path.startsWith('/')) throw new ArchiveError(`${what} ${quote(path)} is absolute`)
|
|
43
15
|
const segments = path.split('/')
|
|
44
|
-
//
|
|
45
|
-
// that is not one is what a drive letter is looked for on.
|
|
16
|
+
// Windows resolves `C:x` from that drive, and `./C:x` is `C:x` once `.` is dropped
|
|
46
17
|
if (/^[a-zA-Z]:/u.test(segments.find((segment) => segment !== '.') ?? '')) throw new ArchiveError(`${what} ${quote(path)} starts with a drive letter`)
|
|
47
18
|
if (tooLong(path, PATH_MAX)) throw new ArchiveError(`${what} ${quote(path)} is longer than ${PATH_MAX} bytes`)
|
|
48
|
-
|
|
49
|
-
if (tooLong(segment, NAME_MAX)) throw new ArchiveError(`${what} ${quote(path)} has a segment longer than ${NAME_MAX} bytes`)
|
|
50
|
-
}
|
|
19
|
+
if (segments.some((segment) => tooLong(segment, NAME_MAX))) throw new ArchiveError(`${what} ${quote(path)} has a segment longer than ${NAME_MAX} bytes`)
|
|
51
20
|
return segments
|
|
52
21
|
}
|
|
53
22
|
|
|
54
|
-
//
|
|
55
|
-
// may name; it comes back as `.`. A path with nothing to drop comes back as
|
|
56
|
-
// the very string it came in as, so the name an archive stores and the one
|
|
57
|
-
// cleaned out of it are one string where they are one path.
|
|
23
|
+
// `.` segments and a directory's trailing slash dropped; only a directory may name the root, as `.`
|
|
58
24
|
export function cleanPath(path, what, directory = false) {
|
|
59
25
|
const segments = checkText(path, what)
|
|
60
|
-
|
|
61
|
-
if (slash) {
|
|
26
|
+
if (segments.at(-1) === '') {
|
|
62
27
|
if (!directory) throw new ArchiveError(`${what} ${quote(path)} ends in a slash but is not a directory`)
|
|
63
28
|
segments.pop()
|
|
64
29
|
}
|
|
65
30
|
const kept = segments.filter((segment) => segment !== '.')
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (segment === '..') throw new ArchiveError(`${what} ${quote(path)} has a .. segment`)
|
|
69
|
-
}
|
|
31
|
+
const bad = kept.find((segment) => segment === '' || segment === '..')
|
|
32
|
+
if (bad !== undefined) throw new ArchiveError(`${what} ${quote(path)} has ${bad ? 'a .. segment' : 'an empty segment'}`)
|
|
70
33
|
if (kept.length === 0) {
|
|
71
34
|
if (!directory) throw new ArchiveError(`${what} ${quote(path)} names the archive root but is not a directory`)
|
|
72
35
|
return '.'
|
|
73
36
|
}
|
|
74
|
-
const name =
|
|
75
|
-
// As the archive stores it, a directory's name carries its slash: what
|
|
76
|
-
// pack takes, unpack then reads.
|
|
37
|
+
const name = kept.join('/')
|
|
77
38
|
if (tooLong(name, PATH_MAX - (directory ? 1 : 0))) throw new ArchiveError(`${what} ${quote(path)} is longer than ${PATH_MAX} bytes`)
|
|
78
39
|
return name
|
|
79
40
|
}
|
|
80
41
|
|
|
81
|
-
// The levels above the archive's root a caller lets a symlink target climb
|
|
82
|
-
// (`symlinksAbove`): none unless given.
|
|
83
42
|
export function levelsAbove(above = 0) {
|
|
84
43
|
if (!(Number.isSafeInteger(above) && above >= 0)) throw new ArchiveError(`symlinksAbove ${String(above)} is not a whole number of levels`)
|
|
85
44
|
return above
|
|
86
45
|
}
|
|
87
46
|
|
|
88
|
-
//
|
|
89
|
-
// the
|
|
90
|
-
// steps: each segment it goes into, or `..` to come back out of one. The
|
|
91
|
-
// walk is counted rather than spelled out, since spelling out the path at
|
|
92
|
-
// every step of a deep one costs its depth squared.
|
|
47
|
+
// The target's steps from the link's directory, each a segment or `..`, climbing at most `above` levels
|
|
48
|
+
// above the root. Counted rather than spelled out, which would cost a deep walk its depth squared.
|
|
93
49
|
export function checkSymlinkTarget(name, target, above = 0) {
|
|
94
50
|
const steps = checkText(target, `symlink target of ${quote(name)}`).filter((segment) => segment !== '' && segment !== '.')
|
|
95
51
|
let depth = name.split('/').length - 1
|
|
@@ -103,86 +59,54 @@ export function checkSymlinkTarget(name, target, above = 0) {
|
|
|
103
59
|
return steps
|
|
104
60
|
}
|
|
105
61
|
|
|
106
|
-
//
|
|
62
|
+
// A symlink's target is checked, never rewritten; a hard link's is a name, cleaned as one
|
|
107
63
|
export function cleanNames(path, type, target, above = 0) {
|
|
108
64
|
const name = cleanPath(path, 'entry name', type === 'directory')
|
|
109
|
-
let linkname = target
|
|
110
65
|
if (type === 'symlink') checkSymlinkTarget(name, target, above)
|
|
111
|
-
|
|
112
|
-
return { name, linkname }
|
|
66
|
+
return { name, linkname: type === 'hardlink' ? cleanPath(target, `hard link target of ${quote(name)}`) : target }
|
|
113
67
|
}
|
|
114
68
|
|
|
115
69
|
const node = () => ({ kind: undefined, entry: undefined, symlink: undefined, first: undefined, firstNode: undefined, rest: undefined })
|
|
116
70
|
|
|
117
|
-
// A node's first child sits on the node
|
|
118
|
-
//
|
|
119
|
-
// one thing each, and a map at every link of it would cost more than the
|
|
120
|
-
// link. With `make` false, a missing child stays missing.
|
|
71
|
+
// A node's first child sits on the node and only a second brings a Map: a deep path is a chain of
|
|
72
|
+
// directories holding one thing each, where a Map at every link would cost more than the link.
|
|
121
73
|
function child(parent, segment, make = true) {
|
|
122
74
|
if (parent.first === segment) return parent.firstNode
|
|
123
75
|
let next = parent.rest?.get(segment)
|
|
124
76
|
if (next !== undefined || !make) return next
|
|
125
77
|
next = node()
|
|
126
|
-
if (parent.first === undefined) {
|
|
127
|
-
|
|
128
|
-
parent.firstNode = next
|
|
129
|
-
} else {
|
|
130
|
-
parent.rest ??= new Map()
|
|
131
|
-
parent.rest.set(segment, next)
|
|
132
|
-
}
|
|
78
|
+
if (parent.first === undefined) Object.assign(parent, { first: segment, firstNode: next })
|
|
79
|
+
else (parent.rest ??= new Map()).set(segment, next)
|
|
133
80
|
return next
|
|
134
81
|
}
|
|
135
82
|
|
|
136
83
|
const FIELDS = ['type', 'mode', 'uid', 'gid', 'mtime', 'uname', 'gname', 'linkname', 'devmajor', 'devminor']
|
|
137
84
|
|
|
138
|
-
// Each
|
|
139
|
-
//
|
|
140
|
-
//
|
|
141
|
-
// refused -- that is the shape of a path through a symlink -- and so is a
|
|
142
|
-
// symlink target that walks through one, in either order, so no chain of
|
|
143
|
-
// links leads out of the archive; where `above` lets a target climb out, a
|
|
144
|
-
// chain can only end at the link that does, so it climbs no further. Whether
|
|
145
|
-
// entries are kept with their data decides what a repeat can be compared
|
|
146
|
-
// with: the in-memory calls keep them, and a stream keeps the fields alone
|
|
147
|
-
// and refuses what it cannot compare.
|
|
148
|
-
//
|
|
149
|
-
// The names are a tree walked a segment at a time, so a lookup hashes one
|
|
150
|
-
// segment rather than a whole path, and a walk down a deep one costs its
|
|
151
|
-
// length rather than its depth squared; a path is spelled out only for a
|
|
152
|
-
// refusal. A node's `kind` is unset until its path is seen, which makes the
|
|
153
|
-
// tree hold exactly what a map from each path seen to its kind would.
|
|
85
|
+
// Each path seen is an 'entry', a 'directory', or a directory 'implied' by a path or symlink target through it.
|
|
86
|
+
// Nothing may sit or be walked through inside a non-directory, so no chain of links leads out of the archive.
|
|
87
|
+
// A name repeats only as the same entry; `keep` keeps data to compare a repeat with, else it is refused.
|
|
154
88
|
export class Names {
|
|
155
89
|
#root = node()
|
|
156
90
|
#keep
|
|
157
91
|
#above
|
|
158
92
|
|
|
159
|
-
|
|
160
|
-
constructor(keep = false, above = 0) {
|
|
93
|
+
constructor(keep = false, symlinksAbove = 0) {
|
|
161
94
|
this.#keep = keep
|
|
162
|
-
this.#above =
|
|
95
|
+
this.#above = levelsAbove(symlinksAbove)
|
|
163
96
|
}
|
|
164
97
|
|
|
165
|
-
// The levels a symlink target may climb above the root, for cleanNames.
|
|
166
98
|
get above() {
|
|
167
99
|
return this.#above
|
|
168
100
|
}
|
|
169
101
|
|
|
170
|
-
// `entry` is cleaned
|
|
102
|
+
// `entry` is cleaned
|
|
171
103
|
add(entry) {
|
|
172
104
|
const { name, type } = entry
|
|
173
|
-
// The target of the symlink this entry is, if it is one: a symlink's
|
|
174
|
-
// own, or, for a hard link, that of the entry it links to.
|
|
175
105
|
let symlink = type === 'symlink' ? entry.linkname : undefined
|
|
176
106
|
if (type === 'hardlink') {
|
|
177
107
|
const target = this.#find(entry.linkname)
|
|
178
108
|
if (target?.kind !== 'entry') throw new ArchiveError(`hard link ${quote(name)} targets ${quote(entry.linkname)}, which is not an earlier non-directory entry`)
|
|
179
|
-
// A hard link to a symlink is
|
|
180
|
-
// relative target is followed from wherever it is reached: `../x` under
|
|
181
|
-
// `a/b/` stays inside, the same link at the root does not. So the
|
|
182
|
-
// target is walked again from here, as if the symlink sat at this name.
|
|
183
|
-
// A hard link to such a hard link is the same symlink again, so the
|
|
184
|
-
// target travels down a chain of them, rather than the chain being
|
|
185
|
-
// followed back, which would cost each new link the chain's length.
|
|
109
|
+
// A hard link to a symlink is that symlink again at this name, where its target is walked again
|
|
186
110
|
symlink = target.symlink
|
|
187
111
|
if (symlink !== undefined) this.#symlink(name, symlink, `hard link ${quote(name)} to symlink ${quote(entry.linkname)}`)
|
|
188
112
|
}
|
|
@@ -198,8 +122,6 @@ export class Names {
|
|
|
198
122
|
}
|
|
199
123
|
const kind = type === 'directory' ? 'directory' : 'entry'
|
|
200
124
|
if (seen.kind !== undefined && kind !== 'directory') throw new ArchiveError(`${quote(name)} is already a directory, so it cannot be a ${type}`)
|
|
201
|
-
// Every parent, the outermost first; nodes[0] is the root, which no
|
|
202
|
-
// name has as a parent.
|
|
203
125
|
for (let i = 1; i < segments.length; i++) {
|
|
204
126
|
this.#directory(nodes[i], () => `${quote(name)} is inside ${quote(segments.slice(0, i).join('/'))}, which is not a directory`)
|
|
205
127
|
}
|
|
@@ -209,11 +131,8 @@ export class Names {
|
|
|
209
131
|
if (type === 'symlink') this.#symlink(name, entry.linkname, `symlink ${quote(name)}`)
|
|
210
132
|
}
|
|
211
133
|
|
|
212
|
-
//
|
|
213
|
-
//
|
|
214
|
-
// inside it has to be a directory -- each one it stands on short of the
|
|
215
|
-
// last step, the archive root aside. Out of it, nothing it passes is the
|
|
216
|
-
// archive's. `what` names it in a refusal.
|
|
134
|
+
// Every path the target stands on inside the archive short of its last step has to be a directory.
|
|
135
|
+
// Past the root nothing it passes is the archive's.
|
|
217
136
|
#symlink(at, target, what) {
|
|
218
137
|
const steps = checkSymlinkTarget(at, target, this.#above)
|
|
219
138
|
const path = at.split('/').slice(0, -1)
|
|
@@ -231,28 +150,19 @@ export class Names {
|
|
|
231
150
|
}
|
|
232
151
|
}
|
|
233
152
|
|
|
234
|
-
//
|
|
235
|
-
// `refusal` spells the path out, and only if it has to.
|
|
153
|
+
// `refusal` spells the path out only when it has to
|
|
236
154
|
#directory(at, refusal) {
|
|
237
155
|
if (at.kind === 'entry') throw new ArchiveError(refusal())
|
|
238
156
|
at.kind ??= 'implied'
|
|
239
157
|
}
|
|
240
158
|
|
|
241
|
-
// The root and the node of each segment in turn, made where missing: a
|
|
242
|
-
// node made here is unseen until something sets its kind.
|
|
243
159
|
#walk(segments) {
|
|
244
160
|
const nodes = [this.#root]
|
|
245
161
|
for (const segment of segments) nodes.push(child(nodes.at(-1), segment))
|
|
246
162
|
return nodes
|
|
247
163
|
}
|
|
248
164
|
|
|
249
|
-
// A path's node where it has one, without making any.
|
|
250
165
|
#find(path) {
|
|
251
|
-
|
|
252
|
-
for (const segment of path.split('/')) {
|
|
253
|
-
at = child(at, segment, false)
|
|
254
|
-
if (at === undefined) return undefined
|
|
255
|
-
}
|
|
256
|
-
return at
|
|
166
|
+
return path.split('/').reduce((at, segment) => at && child(at, segment, false), this.#root)
|
|
257
167
|
}
|
|
258
168
|
}
|
package/src/tar/header.js
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
// The 512-byte ustar header block, as GNU tar 1.35 writes and reads it.
|
|
2
|
-
// String fields come back as bytes cut at the first NUL, not text: GNU cuts
|
|
3
|
-
// a long name at 100 bytes wherever the real name went, and that is not
|
|
4
|
-
// always a whole character, so only the field that counts gets decoded.
|
|
5
|
-
|
|
6
1
|
import { EMPTY } from '../bytes.js'
|
|
7
2
|
import { ArchiveError } from '../error.js'
|
|
8
3
|
|
|
@@ -10,8 +5,6 @@ export const BLOCK = 512
|
|
|
10
5
|
|
|
11
6
|
export const isDevice = (type) => type === 'character-device' || type === 'block-device'
|
|
12
7
|
|
|
13
|
-
// Each entry type's type flag, and each extended header's: those GNU
|
|
14
|
-
// writes, under the names unpack reads them by.
|
|
15
8
|
export const TYPEFLAGS = {
|
|
16
9
|
file: 0x30,
|
|
17
10
|
hardlink: 0x31,
|
|
@@ -26,11 +19,9 @@ export const EXTENDED_TYPEFLAGS = { pax: 0x78, global: 0x67, longname: 0x4c, lon
|
|
|
26
19
|
|
|
27
20
|
export const NAME_SIZE = 100
|
|
28
21
|
export const PREFIX_SIZE = 155
|
|
29
|
-
|
|
30
|
-
export const OWNER_SIZE = 32
|
|
22
|
+
export const OWNER_SIZE = 32 // GNU always ends one with a NUL, so 31 bytes at most
|
|
31
23
|
|
|
32
24
|
const NAME = 0
|
|
33
|
-
// The numbers from mode to mtime, [key, offset, size], in the block's order.
|
|
34
25
|
const NUMBERS = [['mode', 100, 8], ['uid', 108, 8], ['gid', 116, 8], ['size', 124, 12], ['mtime', 136, 12]]
|
|
35
26
|
const CHKSUM = 148
|
|
36
27
|
const TYPEFLAG = 156
|
|
@@ -41,28 +32,22 @@ const GNAME = 297
|
|
|
41
32
|
const DEVMAJOR = 329
|
|
42
33
|
const DEVMINOR = 337
|
|
43
34
|
const PREFIX = 345
|
|
44
|
-
// Under the gnu magic
|
|
45
|
-
// file's real size. libarchive reads them whatever the type flag says --
|
|
46
|
-
// taking the real size as the file's, and the map and any extension blocks
|
|
47
|
-
// after the header as its layout -- where GNU tar and the rest read them for
|
|
48
|
-
// a sparse entry alone.
|
|
35
|
+
// Under the gnu magic, the old sparse map and real size: libarchive reads them whatever the type flag
|
|
49
36
|
const OLD_SPARSE = 386
|
|
50
37
|
const OLD_SPARSE_END = 495
|
|
51
38
|
|
|
52
|
-
const USTAR_MAGIC = 'ustar\u000000' //
|
|
39
|
+
const USTAR_MAGIC = 'ustar\u000000' // ustar and pax
|
|
53
40
|
const GNU_MAGIC = 'ustar \0'
|
|
54
41
|
|
|
55
42
|
const latin1 = (text) => Uint8Array.from(text, (c) => c.codePointAt(0))
|
|
56
43
|
const ascii = (raw) => String.fromCodePoint(...raw)
|
|
57
|
-
|
|
58
|
-
const
|
|
44
|
+
const put = (block, offset, size, bytes) => block.subarray(offset, offset + size).set(bytes) // throws rather than overflow
|
|
45
|
+
const field = (block, offset, size) => untilNul(block.subarray(offset, offset + size))
|
|
59
46
|
|
|
60
47
|
export const octalMax = (size) => 8 ** (size - 1) - 1
|
|
61
48
|
export const fitsOctal = (value, size) => value >= 0 && value <= octalMax(size)
|
|
62
49
|
|
|
63
|
-
//
|
|
64
|
-
// 0x80 (0xff when negative) and the value in base 256, big-endian, two's
|
|
65
|
-
// complement, over the rest of the field.
|
|
50
|
+
// Octal digits and a NUL, or for gnu, 0x80 (0xff if negative) and two's complement base 256 after it
|
|
66
51
|
export function writeNumber(block, offset, size, value, gnu) {
|
|
67
52
|
if (fitsOctal(value, size)) {
|
|
68
53
|
put(block, offset, size, latin1(value.toString(8).padStart(size - 1, '0')))
|
|
@@ -77,12 +62,8 @@ export function writeNumber(block, offset, size, value, gnu) {
|
|
|
77
62
|
}
|
|
78
63
|
}
|
|
79
64
|
|
|
80
|
-
//
|
|
81
|
-
//
|
|
82
|
-
// libarchive and the rest read it. A NUL ends the number wherever it sits, so
|
|
83
|
-
// a field that opens with one is 0 however it goes on -- GNU stops there, and
|
|
84
|
-
// reading the digits past it instead would give a size, and so an archive,
|
|
85
|
-
// that only this package sees. Only a time may be negative.
|
|
65
|
+
// Spaces around the digits and a blank field (0) are read as GNU tar and libarchive do. A NUL ends
|
|
66
|
+
// the number wherever it sits, as in GNU tar, which reading past it would disagree with.
|
|
86
67
|
export function readNumber(block, offset, size, what, at, signed = false) {
|
|
87
68
|
const first = block[offset]
|
|
88
69
|
if (first === 0x80 || first === 0xff) {
|
|
@@ -90,10 +71,10 @@ export function readNumber(block, offset, size, what, at, signed = false) {
|
|
|
90
71
|
for (let i = offset + 1; i < offset + size; i++) v = (v << 8n) | BigInt(block[i])
|
|
91
72
|
if (first === 0xff) v = BigInt.asIntN((size - 1) * 8, v)
|
|
92
73
|
if (v < 0n && !signed) throw new ArchiveError(`the ${what} field is negative`, at)
|
|
93
|
-
if (
|
|
74
|
+
if (!Number.isSafeInteger(Number(v))) throw new ArchiveError(`the ${what} field is too large`, at)
|
|
94
75
|
return Number(v)
|
|
95
76
|
}
|
|
96
|
-
const match = /^ *([0-7]*) *$/u.exec(ascii(
|
|
77
|
+
const match = /^ *([0-7]*) *$/u.exec(ascii(field(block, offset, size)))
|
|
97
78
|
if (!match) throw new ArchiveError(`the ${what} field is not an octal number`, at)
|
|
98
79
|
return match[1] === '' ? 0 : Number.parseInt(match[1], 8)
|
|
99
80
|
}
|
|
@@ -103,9 +84,7 @@ export function untilNul(raw) {
|
|
|
103
84
|
return end === -1 ? raw : raw.subarray(0, end)
|
|
104
85
|
}
|
|
105
86
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
// Every byte, with the checksum field itself counted as spaces.
|
|
87
|
+
// The checksum field itself counts as spaces
|
|
109
88
|
function checksum(block) {
|
|
110
89
|
let sum = 8 * 0x20
|
|
111
90
|
for (let i = 0; i < CHKSUM; i++) sum += block[i]
|
|
@@ -115,9 +94,7 @@ function checksum(block) {
|
|
|
115
94
|
|
|
116
95
|
export const isZeroBlock = (block) => block.every((byte) => byte === 0)
|
|
117
96
|
|
|
118
|
-
//
|
|
119
|
-
// format. `devmajor` and `devminor` are null except for a device, since
|
|
120
|
-
// GNU leaves those fields NUL for everything else.
|
|
97
|
+
// Strings are bytes already cut to size, numbers already fit; device numbers are null but for a device
|
|
121
98
|
export function encodeHeader(f) {
|
|
122
99
|
const block = new Uint8Array(BLOCK)
|
|
123
100
|
put(block, NAME, NAME_SIZE, f.name)
|
|
@@ -130,15 +107,13 @@ export function encodeHeader(f) {
|
|
|
130
107
|
if (f.devmajor !== null) writeNumber(block, DEVMAJOR, 8, f.devmajor, f.gnu)
|
|
131
108
|
if (f.devminor !== null) writeNumber(block, DEVMINOR, 8, f.devminor, f.gnu)
|
|
132
109
|
put(block, PREFIX, PREFIX_SIZE, f.prefix)
|
|
133
|
-
// The one field written as digits, NUL, space.
|
|
134
110
|
put(block, CHKSUM, 8, latin1(`${checksum(block).toString(8).padStart(6, '0')}\0 `))
|
|
135
111
|
return block
|
|
136
112
|
}
|
|
137
113
|
|
|
138
|
-
|
|
139
|
-
// than nothing or GNU's base 256, both of which readNumber reads.
|
|
140
|
-
const isOctal = (block, offset, size) => /^ *[0-7]+ *$/u.test(ascii(untilNul(block.subarray(offset, offset + size))))
|
|
114
|
+
const isOctal = (block, offset, size) => /^ *[0-7]+ *$/u.test(ascii(field(block, offset, size)))
|
|
141
115
|
|
|
116
|
+
// Strings come back as bytes: GNU cuts a long name at 100 bytes, not always on a whole character
|
|
142
117
|
export function decodeHeader(block, at) {
|
|
143
118
|
if (checksum(block) !== readNumber(block, CHKSUM, 8, 'checksum', at)) throw new ArchiveError('header checksum does not match', at)
|
|
144
119
|
const magic = ascii(block.subarray(MAGIC, MAGIC + 8))
|
|
@@ -147,19 +122,18 @@ export function decodeHeader(block, at) {
|
|
|
147
122
|
if (gnu && !isZeroBlock(block.subarray(OLD_SPARSE, OLD_SPARSE_END))) throw new ArchiveError('header carries an old GNU sparse map or real size', at)
|
|
148
123
|
const typeflag = block[TYPEFLAG]
|
|
149
124
|
const device = typeflag === TYPEFLAGS['character-device'] || typeflag === TYPEFLAGS['block-device']
|
|
125
|
+
const numbers = [['checksum', CHKSUM, 8], ...NUMBERS, ...(device ? [['devmajor', DEVMAJOR, 8], ['devminor', DEVMINOR, 8]] : [])]
|
|
150
126
|
return {
|
|
151
127
|
gnu,
|
|
152
128
|
typeflag,
|
|
153
129
|
name: field(block, NAME, NAME_SIZE),
|
|
154
|
-
|
|
155
|
-
prefix: gnu ? EMPTY : field(block, PREFIX, PREFIX_SIZE),
|
|
130
|
+
prefix: gnu ? EMPTY : field(block, PREFIX, PREFIX_SIZE), // gnu has its own fields there
|
|
156
131
|
linkname: field(block, LINKNAME, NAME_SIZE),
|
|
157
132
|
uname: field(block, UNAME, OWNER_SIZE),
|
|
158
133
|
gname: field(block, GNAME, OWNER_SIZE),
|
|
159
134
|
...Object.fromEntries(NUMBERS.map(([key, offset, size]) => [key, readNumber(block, offset, size, key, at, key === 'mtime')])),
|
|
160
135
|
devmajor: device ? readNumber(block, DEVMAJOR, 8, 'devmajor', at) : 0,
|
|
161
136
|
devminor: device ? readNumber(block, DEVMINOR, 8, 'devminor', at) : 0,
|
|
162
|
-
octal: [
|
|
163
|
-
.every(([, offset, size]) => isOctal(block, offset, size)),
|
|
137
|
+
octal: numbers.every(([, offset, size]) => isOctal(block, offset, size)),
|
|
164
138
|
}
|
|
165
139
|
}
|
package/src/tar/pack.js
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
// --group=0 --numeric-owner` and, for pax, `--pax-option=delete=atime,
|
|
3
|
-
// delete=ctime`. Where GNU would cut or substitute -- a name too long for
|
|
4
|
-
// ustar, a number out of range -- this refuses instead.
|
|
5
|
-
|
|
1
|
+
import { utf8fromString } from '@exodus/bytes/utf8.js'
|
|
6
2
|
import { EMPTY, concat, isAscii } from '../bytes.js'
|
|
7
3
|
import { DEFAULT_MODE, checkEntry, wireName } from '../entry.js'
|
|
8
4
|
import { ArchiveError } from '../error.js'
|
|
5
|
+
import { Names, cleanNames } from '../names.js'
|
|
6
|
+
import { checkString, hasUnsafe, quote } from '../text.js'
|
|
9
7
|
import { BLOCK, EXTENDED_TYPEFLAGS, NAME_SIZE, OWNER_SIZE, PREFIX_SIZE, TYPEFLAGS, encodeHeader, fitsOctal, isDevice, octalMax } from './header.js'
|
|
10
|
-
import { Names, cleanNames, levelsAbove } from '../names.js'
|
|
11
8
|
import { encodePax } from './pax.js'
|
|
12
|
-
|
|
9
|
+
|
|
10
|
+
// What GNU tar 1.35 writes with `--owner=0 --group=0 --numeric-owner` (and for pax,
|
|
11
|
+
// `--pax-option=delete=atime,delete=ctime`), but refusing what GNU would cut or substitute
|
|
13
12
|
|
|
14
13
|
const FORMATS = new Set(['gnu', 'ustar', 'pax'])
|
|
15
14
|
const SLASH = 0x2f
|
|
@@ -25,9 +24,9 @@ function ownerName(value, what) {
|
|
|
25
24
|
return value
|
|
26
25
|
}
|
|
27
26
|
|
|
28
|
-
function normalize(entry) {
|
|
27
|
+
function normalize(entry, above) {
|
|
29
28
|
const checked = checkEntry(entry, TYPEFLAGS)
|
|
30
|
-
const { name, type } = checked
|
|
29
|
+
const { name, type, linkname } = checked
|
|
31
30
|
const devmajor = integer(entry.devmajor ?? 0, 'devmajor')
|
|
32
31
|
const devminor = integer(entry.devminor ?? 0, 'devminor')
|
|
33
32
|
if ((devmajor !== 0 || devminor !== 0) && !isDevice(type)) throw new ArchiveError(`a ${type} cannot have device numbers (${quote(name)})`)
|
|
@@ -43,27 +42,26 @@ function normalize(entry) {
|
|
|
43
42
|
gname: ownerName(entry.gname ?? '', 'gname'),
|
|
44
43
|
devmajor,
|
|
45
44
|
devminor,
|
|
45
|
+
...cleanNames(name, type, linkname, above),
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
// A copy in whole blocks; `length` may reach past `raw` to count a NUL
|
|
49
|
+
// A copy in whole blocks; `length` may reach past `raw` to count a NUL
|
|
50
50
|
function padded(raw, length = raw.length) {
|
|
51
51
|
const out = new Uint8Array(Math.ceil(length / BLOCK) * BLOCK)
|
|
52
52
|
out.set(raw)
|
|
53
53
|
return out
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
// GNU's start_private_header
|
|
56
|
+
// GNU's start_private_header
|
|
57
57
|
const privateHeader = (name, size, mtime, typeflag, gnu) => encodeHeader({
|
|
58
|
-
gnu, name:
|
|
58
|
+
gnu, name: utf8fromString(name).subarray(0, NAME_SIZE), prefix: EMPTY, linkname: EMPTY, uname: EMPTY, gname: EMPTY,
|
|
59
59
|
typeflag, mode: 0o644, uid: 0, gid: 0, size, mtime, devmajor: null, devminor: null,
|
|
60
60
|
})
|
|
61
61
|
|
|
62
|
-
// The size counts the NUL after the name.
|
|
63
62
|
const longLink = (raw, typeflag) => [privateHeader('././@LongLink', raw.length + 1, 0, typeflag, true), padded(raw, raw.length + 1)]
|
|
64
63
|
|
|
65
|
-
// Named `%d/PaxHeaders/%f` after the entry,
|
|
66
|
-
// clamped into the field.
|
|
64
|
+
// Named `%d/PaxHeaders/%f` after the entry, its mtime clamped into the field
|
|
67
65
|
function paxHeader(e, records) {
|
|
68
66
|
const body = encodePax(records)
|
|
69
67
|
const slash = e.name.lastIndexOf('/')
|
|
@@ -71,32 +69,27 @@ function paxHeader(e, records) {
|
|
|
71
69
|
return [privateHeader(label, body.length, Math.max(0, Math.min(e.mtime, octalMax(12))), EXTENDED_TYPEFLAGS.pax, false), padded(body)]
|
|
72
70
|
}
|
|
73
71
|
|
|
74
|
-
// GNU's split_long_name: the last slash within 155 bytes of
|
|
75
|
-
// trailing slash does not count), leaving 1 to 100 bytes of name.
|
|
72
|
+
// GNU's split_long_name: at the last slash within 155 bytes, a trailing one aside, leaving 1 to 100 bytes of name
|
|
76
73
|
function splitName(raw, display) {
|
|
77
74
|
const { length } = raw
|
|
78
75
|
if (length > PREFIX_SIZE + NAME_SIZE + 1) throw new ArchiveError(`name ${quote(display)} is longer than 256 bytes, which ustar cannot hold`)
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
else if (raw[limit - 1] === SLASH) limit--
|
|
82
|
-
let i = limit - 1
|
|
83
|
-
while (i > 0 && raw[i] !== SLASH) i--
|
|
76
|
+
const limit = length > PREFIX_SIZE + 1 ? PREFIX_SIZE + 1 : raw[length - 1] === SLASH ? length - 1 : length
|
|
77
|
+
const i = raw.lastIndexOf(SLASH, limit - 1)
|
|
84
78
|
const rest = length - i - 1
|
|
85
|
-
if (i
|
|
79
|
+
if (i <= 0 || rest > NAME_SIZE || rest === 0) throw new ArchiveError(`name ${quote(display)} cannot be split into ustar's prefix and name fields`)
|
|
86
80
|
return { prefix: raw.subarray(0, i), name: raw.subarray(i + 1) }
|
|
87
81
|
}
|
|
88
82
|
|
|
89
|
-
//
|
|
90
|
-
//
|
|
91
|
-
// linkpath, path, uid, gid, size, mtime, devmajor, devminor, uname, gname.
|
|
83
|
+
// Long-name blocks or a pax header, the header, the data and its padding.
|
|
84
|
+
// Pax records go in GNU's order: linkpath, path, uid, gid, size, mtime, devmajor, devminor, uname, gname.
|
|
92
85
|
function encodeEntry(e, format) {
|
|
93
86
|
const gnu = format === 'gnu'
|
|
94
87
|
const pax = []
|
|
95
88
|
const chunks = []
|
|
96
89
|
const wire = wireName(e)
|
|
97
|
-
let name =
|
|
90
|
+
let name = utf8fromString(wire)
|
|
98
91
|
let prefix = EMPTY
|
|
99
|
-
let link =
|
|
92
|
+
let link = utf8fromString(e.linkname)
|
|
100
93
|
if (link.length > NAME_SIZE) {
|
|
101
94
|
if (gnu) chunks.push(...longLink(link, EXTENDED_TYPEFLAGS.longlink))
|
|
102
95
|
else if (format === 'pax') pax.push(['linkpath', e.linkname])
|
|
@@ -111,16 +104,13 @@ function encodeEntry(e, format) {
|
|
|
111
104
|
}
|
|
112
105
|
const number = (what, value, size) => {
|
|
113
106
|
if (gnu || fitsOctal(value, size)) return value
|
|
114
|
-
if (format
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
throw new ArchiveError(`${what} ${value} of ${quote(e.name)} does not fit the ustar format`)
|
|
107
|
+
if (format !== 'pax') throw new ArchiveError(`${what} ${value} of ${quote(e.name)} does not fit the ustar format`)
|
|
108
|
+
pax.push([what, String(value)])
|
|
109
|
+
return 0
|
|
119
110
|
}
|
|
120
|
-
// GNU
|
|
121
|
-
// 31 without one; that loss is the one place this does not follow it.
|
|
111
|
+
// GNU cuts a 32-byte owner name to 31 with no pax record; this refuses instead
|
|
122
112
|
const owner = (what, text) => {
|
|
123
|
-
const raw =
|
|
113
|
+
const raw = utf8fromString(text)
|
|
124
114
|
if (raw.length < OWNER_SIZE && (format !== 'pax' || isAscii(raw))) return raw
|
|
125
115
|
if (format !== 'pax') throw new ArchiveError(`${what} of ${quote(e.name)} is longer than 31 bytes, which the ${format} format cannot hold`)
|
|
126
116
|
pax.push([what, text])
|
|
@@ -140,33 +130,28 @@ function encodeEntry(e, format) {
|
|
|
140
130
|
}
|
|
141
131
|
if (pax.length) chunks.push(...paxHeader(e, pax))
|
|
142
132
|
chunks.push(encodeHeader(fields))
|
|
143
|
-
const rest = e.data.length % BLOCK
|
|
144
133
|
if (e.data.length) chunks.push(e.data)
|
|
145
|
-
if (
|
|
134
|
+
if (e.data.length % BLOCK) chunks.push(new Uint8Array(BLOCK - (e.data.length % BLOCK)))
|
|
146
135
|
return chunks
|
|
147
136
|
}
|
|
148
137
|
|
|
149
|
-
// `keep` holds entries for a repeat to be compared with, which only the
|
|
150
|
-
// in-memory call can afford.
|
|
151
138
|
function packer({ format = 'gnu', blocking = 20, symlinksAbove } = {}, keep = false) {
|
|
152
139
|
if (!FORMATS.has(format)) throw new ArchiveError(`format ${quote(String(format))} is not gnu, ustar or pax`)
|
|
153
140
|
if (!Number.isSafeInteger(blocking) || blocking < 1) throw new ArchiveError('blocking is not a positive integer')
|
|
154
|
-
const names = new Names(keep,
|
|
141
|
+
const names = new Names(keep, symlinksAbove)
|
|
155
142
|
let total = 0
|
|
156
143
|
return {
|
|
157
144
|
add(entry) {
|
|
158
|
-
const e = normalize(entry)
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
const chunks = encodeEntry(cleaned, format)
|
|
145
|
+
const e = normalize(entry, names.above)
|
|
146
|
+
names.add(e)
|
|
147
|
+
const chunks = encodeEntry(e, format)
|
|
162
148
|
for (const chunk of chunks) total += chunk.length
|
|
163
149
|
return chunks
|
|
164
150
|
},
|
|
165
|
-
// Two zero blocks, then zeros to a
|
|
151
|
+
// Two zero blocks, then zeros to a whole record
|
|
166
152
|
end() {
|
|
167
153
|
const record = blocking * BLOCK
|
|
168
|
-
|
|
169
|
-
return [new Uint8Array(2 * BLOCK + (rest ? record - rest : 0))]
|
|
154
|
+
return [new Uint8Array(Math.ceil((total + 2 * BLOCK) / record) * record - total)]
|
|
170
155
|
},
|
|
171
156
|
}
|
|
172
157
|
}
|
package/src/tar/pax.js
CHANGED
|
@@ -1,23 +1,19 @@
|
|
|
1
|
-
// pax extended header records: `<length> <keyword>=<value>\n`, the length
|
|
2
|
-
// in decimal counting the whole line, its own digits included.
|
|
3
|
-
|
|
4
1
|
import { utf8fromString } from '@exodus/bytes/utf8.js'
|
|
5
2
|
import { concat } from '../bytes.js'
|
|
6
3
|
import { ArchiveError } from '../error.js'
|
|
7
4
|
import { decodeUtf8, hasUnsafe, quote } from '../text.js'
|
|
8
5
|
|
|
6
|
+
// `<length> <keyword>=<value>\n`, the length counting the whole record, its own digits included
|
|
9
7
|
function record(keyword, value) {
|
|
10
8
|
const body = utf8fromString(` ${keyword}=${value}\n`)
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return concat([utf8fromString(String(total)), body])
|
|
9
|
+
let length = body.length + 1
|
|
10
|
+
while (body.length + String(length).length !== length) length++
|
|
11
|
+
return concat([utf8fromString(String(length)), body])
|
|
15
12
|
}
|
|
16
13
|
|
|
17
14
|
export const encodePax = (records) => concat(records.map(([keyword, value]) => record(keyword, value)))
|
|
18
15
|
|
|
19
|
-
//
|
|
20
|
-
// every entry under the same global header shares that header's.
|
|
16
|
+
// Read-only, as entries under one global header share its records
|
|
21
17
|
const READ_ONLY = 'pax records cannot be changed'
|
|
22
18
|
export class Records extends Map {
|
|
23
19
|
set() {
|
|
@@ -32,7 +28,6 @@ export class Records extends Map {
|
|
|
32
28
|
throw new TypeError(READ_ONLY)
|
|
33
29
|
}
|
|
34
30
|
}
|
|
35
|
-
const put = (records, keyword, value) => Map.prototype.set.call(records, keyword, value)
|
|
36
31
|
|
|
37
32
|
export function decodePax(bytes, at) {
|
|
38
33
|
const records = new Records()
|
|
@@ -48,7 +43,7 @@ export function decodePax(bytes, at) {
|
|
|
48
43
|
const keyword = decodeUtf8(bytes.subarray(i + 1, equals), 'a pax record', at)
|
|
49
44
|
if (keyword === '' || hasUnsafe(keyword, false) || keyword.includes(' ')) throw new ArchiveError(`pax keyword ${quote(keyword)} is malformed`, at)
|
|
50
45
|
if (records.has(keyword)) throw new ArchiveError(`pax keyword ${keyword} repeats`, at)
|
|
51
|
-
|
|
46
|
+
Map.prototype.set.call(records, keyword, decodeUtf8(bytes.subarray(equals + 1, end - 1), 'a pax record', at))
|
|
52
47
|
pos = end
|
|
53
48
|
}
|
|
54
49
|
return records
|