tar 7.5.24 → 8.0.0
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/README.md +1705 -1147
- package/dist/commonjs/bytes.d.ts +21 -0
- package/dist/commonjs/bytes.d.ts.map +1 -0
- package/dist/commonjs/bytes.js +83 -0
- package/dist/commonjs/bytes.js.map +1 -0
- package/dist/commonjs/codec.d.ts +53 -0
- package/dist/commonjs/codec.d.ts.map +1 -0
- package/dist/commonjs/codec.js +35 -0
- package/dist/commonjs/codec.js.map +1 -0
- package/dist/commonjs/concat.d.ts +3 -0
- package/dist/commonjs/concat.d.ts.map +1 -0
- package/dist/commonjs/concat.js +20 -0
- package/dist/commonjs/concat.js.map +1 -0
- package/dist/commonjs/create.d.ts +39 -2
- package/dist/commonjs/create.d.ts.map +1 -1
- package/dist/commonjs/create.js +150 -64
- package/dist/commonjs/create.js.map +1 -1
- package/dist/commonjs/dealias.d.ts +79 -0
- package/dist/commonjs/dealias.d.ts.map +1 -0
- package/dist/commonjs/dealias.js +50 -0
- package/dist/commonjs/dealias.js.map +1 -0
- package/dist/commonjs/delete.d.ts +19 -0
- package/dist/commonjs/delete.d.ts.map +1 -0
- package/dist/commonjs/delete.js +121 -0
- package/dist/commonjs/delete.js.map +1 -0
- package/dist/commonjs/entries.d.ts +82 -0
- package/dist/commonjs/entries.d.ts.map +1 -0
- package/dist/commonjs/entries.js +398 -0
- package/dist/commonjs/entries.js.map +1 -0
- package/dist/commonjs/extract.d.ts +14 -2
- package/dist/commonjs/extract.d.ts.map +1 -1
- package/dist/commonjs/extract.js +40 -81
- package/dist/commonjs/extract.js.map +1 -1
- package/dist/commonjs/fs.d.ts +201 -0
- package/dist/commonjs/fs.d.ts.map +1 -0
- package/dist/commonjs/fs.js +5 -0
- package/dist/commonjs/fs.js.map +1 -0
- package/dist/commonjs/header.d.ts +4 -4
- package/dist/commonjs/header.d.ts.map +1 -1
- package/dist/commonjs/header.js +38 -42
- package/dist/commonjs/header.js.map +1 -1
- package/dist/commonjs/index.d.ts +25 -18
- package/dist/commonjs/index.d.ts.map +1 -1
- package/dist/commonjs/index.js +46 -18
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/index.min.js +2 -3
- package/dist/commonjs/index.min.js.map +4 -4
- package/dist/commonjs/large-numbers.d.ts +2 -2
- package/dist/commonjs/large-numbers.d.ts.map +1 -1
- package/dist/commonjs/large-numbers.js +4 -4
- package/dist/commonjs/large-numbers.js.map +1 -1
- package/dist/commonjs/list.d.ts +23 -6
- package/dist/commonjs/list.d.ts.map +1 -1
- package/dist/commonjs/list.js +99 -138
- package/dist/commonjs/list.js.map +1 -1
- package/dist/commonjs/mkdir.d.ts +16 -11
- package/dist/commonjs/mkdir.d.ts.map +1 -1
- package/dist/commonjs/mkdir.js +121 -159
- package/dist/commonjs/mkdir.js.map +1 -1
- package/dist/commonjs/options.d.ts +284 -513
- package/dist/commonjs/options.d.ts.map +1 -1
- package/dist/commonjs/options.js +20 -61
- package/dist/commonjs/options.js.map +1 -1
- package/dist/commonjs/pack.d.ts +150 -96
- package/dist/commonjs/pack.d.ts.map +1 -1
- package/dist/commonjs/pack.js +752 -445
- package/dist/commonjs/pack.js.map +1 -1
- package/dist/commonjs/parser.d.ts +94 -0
- package/dist/commonjs/parser.d.ts.map +1 -0
- package/dist/commonjs/parser.js +381 -0
- package/dist/commonjs/parser.js.map +1 -0
- package/dist/commonjs/path-reservations.d.ts +17 -7
- package/dist/commonjs/path-reservations.d.ts.map +1 -1
- package/dist/commonjs/path-reservations.js +96 -119
- package/dist/commonjs/path-reservations.js.map +1 -1
- package/dist/commonjs/path.d.ts +19 -0
- package/dist/commonjs/path.d.ts.map +1 -0
- package/dist/commonjs/path.js +90 -0
- package/dist/commonjs/path.js.map +1 -0
- package/dist/commonjs/pax.d.ts +3 -1
- package/dist/commonjs/pax.d.ts.map +1 -1
- package/dist/commonjs/pax.js +53 -43
- package/dist/commonjs/pax.js.map +1 -1
- package/dist/commonjs/read-entry.d.ts +97 -11
- package/dist/commonjs/read-entry.d.ts.map +1 -1
- package/dist/commonjs/read-entry.js +248 -97
- package/dist/commonjs/read-entry.js.map +1 -1
- package/dist/commonjs/read-file.d.ts +22 -0
- package/dist/commonjs/read-file.d.ts.map +1 -0
- package/dist/commonjs/read-file.js +104 -0
- package/dist/commonjs/read-file.js.map +1 -0
- package/dist/commonjs/replace.d.ts +45 -1
- package/dist/commonjs/replace.d.ts.map +1 -1
- package/dist/commonjs/replace.js +165 -211
- package/dist/commonjs/replace.js.map +1 -1
- package/dist/commonjs/server/codecs.d.ts +38 -0
- package/dist/commonjs/server/codecs.d.ts.map +1 -0
- package/dist/commonjs/server/codecs.js +252 -0
- package/dist/commonjs/server/codecs.js.map +1 -0
- package/dist/commonjs/server/index.d.ts +115 -0
- package/dist/commonjs/server/index.d.ts.map +1 -0
- package/dist/commonjs/server/index.js +166 -0
- package/dist/commonjs/server/index.js.map +1 -0
- package/dist/commonjs/server/index.min.js +3 -0
- package/dist/commonjs/server/index.min.js.map +7 -0
- package/dist/commonjs/strip-absolute-path.d.ts +8 -0
- package/dist/commonjs/strip-absolute-path.d.ts.map +1 -1
- package/dist/commonjs/strip-absolute-path.js +17 -20
- package/dist/commonjs/strip-absolute-path.js.map +1 -1
- package/dist/commonjs/to-stream.d.ts +7 -0
- package/dist/commonjs/to-stream.d.ts.map +1 -0
- package/dist/commonjs/to-stream.js +22 -0
- package/dist/commonjs/to-stream.js.map +1 -0
- package/dist/commonjs/types.d.ts +4 -3
- package/dist/commonjs/types.d.ts.map +1 -1
- package/dist/commonjs/types.js +4 -3
- package/dist/commonjs/types.js.map +1 -1
- package/dist/commonjs/unpack.d.ts +135 -89
- package/dist/commonjs/unpack.d.ts.map +1 -1
- package/dist/commonjs/unpack.js +760 -727
- package/dist/commonjs/unpack.js.map +1 -1
- package/dist/commonjs/update.d.ts +11 -1
- package/dist/commonjs/update.d.ts.map +1 -1
- package/dist/commonjs/update.js +37 -28
- package/dist/commonjs/update.js.map +1 -1
- package/dist/commonjs/warn.d.ts +27 -0
- package/dist/commonjs/warn.d.ts.map +1 -0
- package/dist/commonjs/warn.js +36 -0
- package/dist/commonjs/warn.js.map +1 -0
- package/dist/commonjs/win32-path.d.ts +16 -0
- package/dist/commonjs/win32-path.d.ts.map +1 -0
- package/dist/commonjs/win32-path.js +67 -0
- package/dist/commonjs/win32-path.js.map +1 -0
- package/dist/commonjs/write-entry.d.ts +64 -111
- package/dist/commonjs/write-entry.d.ts.map +1 -1
- package/dist/commonjs/write-entry.js +108 -672
- package/dist/commonjs/write-entry.js.map +1 -1
- package/dist/commonjs/write-flag.d.ts +18 -0
- package/dist/commonjs/write-flag.d.ts.map +1 -0
- package/dist/commonjs/write-flag.js +29 -0
- package/dist/commonjs/write-flag.js.map +1 -0
- package/dist/esm/bytes.d.ts +21 -0
- package/dist/esm/bytes.d.ts.map +1 -0
- package/dist/esm/bytes.js +72 -0
- package/dist/esm/bytes.js.map +1 -0
- package/dist/esm/codec.d.ts +53 -0
- package/dist/esm/codec.d.ts.map +1 -0
- package/dist/esm/codec.js +32 -0
- package/dist/esm/codec.js.map +1 -0
- package/dist/esm/concat.d.ts +3 -0
- package/dist/esm/concat.d.ts.map +1 -0
- package/dist/esm/concat.js +16 -0
- package/dist/esm/concat.js.map +1 -0
- package/dist/esm/create.d.ts +39 -2
- package/dist/esm/create.d.ts.map +1 -1
- package/dist/esm/create.js +144 -61
- package/dist/esm/create.js.map +1 -1
- package/dist/esm/dealias.d.ts +79 -0
- package/dist/esm/dealias.d.ts.map +1 -0
- package/dist/esm/dealias.js +46 -0
- package/dist/esm/dealias.js.map +1 -0
- package/dist/esm/delete.d.ts +19 -0
- package/dist/esm/delete.d.ts.map +1 -0
- package/dist/esm/delete.js +117 -0
- package/dist/esm/delete.js.map +1 -0
- package/dist/esm/entries.d.ts +82 -0
- package/dist/esm/entries.d.ts.map +1 -0
- package/dist/esm/entries.js +388 -0
- package/dist/esm/entries.js.map +1 -0
- package/dist/esm/extract.d.ts +14 -2
- package/dist/esm/extract.d.ts.map +1 -1
- package/dist/esm/extract.js +37 -44
- package/dist/esm/extract.js.map +1 -1
- package/dist/esm/fs.d.ts +201 -0
- package/dist/esm/fs.d.ts.map +1 -0
- package/dist/esm/fs.js +4 -0
- package/dist/esm/fs.js.map +1 -0
- package/dist/esm/header.d.ts +4 -4
- package/dist/esm/header.d.ts.map +1 -1
- package/dist/esm/header.js +38 -42
- package/dist/esm/header.js.map +1 -1
- package/dist/esm/index.d.ts +25 -18
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +23 -18
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index.min.js +2 -3
- package/dist/esm/index.min.js.map +4 -4
- package/dist/esm/large-numbers.d.ts +2 -2
- package/dist/esm/large-numbers.d.ts.map +1 -1
- package/dist/esm/large-numbers.js +4 -4
- package/dist/esm/large-numbers.js.map +1 -1
- package/dist/esm/list.d.ts +23 -6
- package/dist/esm/list.d.ts.map +1 -1
- package/dist/esm/list.js +96 -101
- package/dist/esm/list.js.map +1 -1
- package/dist/esm/mkdir.d.ts +16 -11
- package/dist/esm/mkdir.d.ts.map +1 -1
- package/dist/esm/mkdir.js +119 -154
- package/dist/esm/mkdir.js.map +1 -1
- package/dist/esm/options.d.ts +284 -513
- package/dist/esm/options.d.ts.map +1 -1
- package/dist/esm/options.js +17 -51
- package/dist/esm/options.js.map +1 -1
- package/dist/esm/pack.d.ts +150 -96
- package/dist/esm/pack.d.ts.map +1 -1
- package/dist/esm/pack.js +750 -441
- package/dist/esm/pack.js.map +1 -1
- package/dist/esm/parser.d.ts +94 -0
- package/dist/esm/parser.d.ts.map +1 -0
- package/dist/esm/parser.js +377 -0
- package/dist/esm/parser.js.map +1 -0
- package/dist/esm/path-reservations.d.ts +17 -7
- package/dist/esm/path-reservations.d.ts.map +1 -1
- package/dist/esm/path-reservations.js +96 -119
- package/dist/esm/path-reservations.js.map +1 -1
- package/dist/esm/path.d.ts +19 -0
- package/dist/esm/path.d.ts.map +1 -0
- package/dist/esm/path.js +80 -0
- package/dist/esm/path.js.map +1 -0
- package/dist/esm/pax.d.ts +3 -1
- package/dist/esm/pax.d.ts.map +1 -1
- package/dist/esm/pax.js +52 -42
- package/dist/esm/pax.js.map +1 -1
- package/dist/esm/read-entry.d.ts +97 -11
- package/dist/esm/read-entry.d.ts.map +1 -1
- package/dist/esm/read-entry.js +245 -96
- package/dist/esm/read-entry.js.map +1 -1
- package/dist/esm/read-file.d.ts +22 -0
- package/dist/esm/read-file.d.ts.map +1 -0
- package/dist/esm/read-file.js +99 -0
- package/dist/esm/read-file.js.map +1 -0
- package/dist/esm/replace.d.ts +45 -1
- package/dist/esm/replace.d.ts.map +1 -1
- package/dist/esm/replace.js +161 -207
- package/dist/esm/replace.js.map +1 -1
- package/dist/esm/server/codecs.d.ts +38 -0
- package/dist/esm/server/codecs.d.ts.map +1 -0
- package/dist/esm/server/codecs.js +214 -0
- package/dist/esm/server/codecs.js.map +1 -0
- package/dist/esm/server/index.d.ts +115 -0
- package/dist/esm/server/index.d.ts.map +1 -0
- package/dist/esm/server/index.js +102 -0
- package/dist/esm/server/index.js.map +1 -0
- package/dist/esm/server/index.min.js +3 -0
- package/dist/esm/server/index.min.js.map +7 -0
- package/dist/esm/strip-absolute-path.d.ts +8 -0
- package/dist/esm/strip-absolute-path.d.ts.map +1 -1
- package/dist/esm/strip-absolute-path.js +17 -20
- package/dist/esm/strip-absolute-path.js.map +1 -1
- package/dist/esm/to-stream.d.ts +7 -0
- package/dist/esm/to-stream.d.ts.map +1 -0
- package/dist/esm/to-stream.js +18 -0
- package/dist/esm/to-stream.js.map +1 -0
- package/dist/esm/types.d.ts +4 -3
- package/dist/esm/types.d.ts.map +1 -1
- package/dist/esm/types.js +4 -3
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/unpack.d.ts +135 -89
- package/dist/esm/unpack.d.ts.map +1 -1
- package/dist/esm/unpack.js +756 -721
- package/dist/esm/unpack.js.map +1 -1
- package/dist/esm/update.d.ts +11 -1
- package/dist/esm/update.d.ts.map +1 -1
- package/dist/esm/update.js +35 -28
- package/dist/esm/update.js.map +1 -1
- package/dist/esm/warn.d.ts +27 -0
- package/dist/esm/warn.d.ts.map +1 -0
- package/dist/esm/warn.js +31 -0
- package/dist/esm/warn.js.map +1 -0
- package/dist/esm/win32-path.d.ts +16 -0
- package/dist/esm/win32-path.d.ts.map +1 -0
- package/dist/esm/win32-path.js +60 -0
- package/dist/esm/win32-path.js.map +1 -0
- package/dist/esm/write-entry.d.ts +64 -111
- package/dist/esm/write-entry.d.ts.map +1 -1
- package/dist/esm/write-entry.js +107 -633
- package/dist/esm/write-entry.js.map +1 -1
- package/dist/esm/write-flag.d.ts +18 -0
- package/dist/esm/write-flag.d.ts.map +1 -0
- package/dist/esm/write-flag.js +25 -0
- package/dist/esm/write-flag.js.map +1 -0
- package/package.json +68 -224
- package/dist/commonjs/get-write-flag.d.ts +0 -2
- package/dist/commonjs/get-write-flag.d.ts.map +0 -1
- package/dist/commonjs/get-write-flag.js +0 -32
- package/dist/commonjs/get-write-flag.js.map +0 -1
- package/dist/commonjs/make-command.d.ts +0 -49
- package/dist/commonjs/make-command.d.ts.map +0 -1
- package/dist/commonjs/make-command.js +0 -52
- package/dist/commonjs/make-command.js.map +0 -1
- package/dist/commonjs/normalize-windows-path.d.ts +0 -2
- package/dist/commonjs/normalize-windows-path.d.ts.map +0 -1
- package/dist/commonjs/normalize-windows-path.js +0 -12
- package/dist/commonjs/normalize-windows-path.js.map +0 -1
- package/dist/commonjs/parse.d.ts +0 -92
- package/dist/commonjs/parse.d.ts.map +0 -1
- package/dist/commonjs/parse.js +0 -655
- package/dist/commonjs/parse.js.map +0 -1
- package/dist/commonjs/process-umask.d.ts +0 -2
- package/dist/commonjs/process-umask.d.ts.map +0 -1
- package/dist/commonjs/process-umask.js +0 -7
- package/dist/commonjs/process-umask.js.map +0 -1
- package/dist/commonjs/warn-method.d.ts +0 -24
- package/dist/commonjs/warn-method.d.ts.map +0 -1
- package/dist/commonjs/warn-method.js +0 -31
- package/dist/commonjs/warn-method.js.map +0 -1
- package/dist/esm/get-write-flag.d.ts +0 -2
- package/dist/esm/get-write-flag.d.ts.map +0 -1
- package/dist/esm/get-write-flag.js +0 -26
- package/dist/esm/get-write-flag.js.map +0 -1
- package/dist/esm/make-command.d.ts +0 -49
- package/dist/esm/make-command.d.ts.map +0 -1
- package/dist/esm/make-command.js +0 -48
- package/dist/esm/make-command.js.map +0 -1
- package/dist/esm/normalize-windows-path.d.ts +0 -2
- package/dist/esm/normalize-windows-path.d.ts.map +0 -1
- package/dist/esm/normalize-windows-path.js +0 -9
- package/dist/esm/normalize-windows-path.js.map +0 -1
- package/dist/esm/parse.d.ts +0 -92
- package/dist/esm/parse.d.ts.map +0 -1
- package/dist/esm/parse.js +0 -651
- package/dist/esm/parse.js.map +0 -1
- package/dist/esm/process-umask.d.ts +0 -2
- package/dist/esm/process-umask.d.ts.map +0 -1
- package/dist/esm/process-umask.js +0 -3
- package/dist/esm/process-umask.js.map +0 -1
- package/dist/esm/warn-method.d.ts +0 -24
- package/dist/esm/warn-method.d.ts.map +0 -1
- package/dist/esm/warn-method.js +0 -27
- package/dist/esm/warn-method.js.map +0 -1
package/dist/esm/unpack.js
CHANGED
|
@@ -1,373 +1,333 @@
|
|
|
1
|
-
|
|
2
|
-
// but the path reservations are required to avoid race conditions where
|
|
3
|
-
// parallelized unpack ops may mess with one another, due to dependencies
|
|
4
|
-
// (like a Link depending on its target) or destructive operations (like
|
|
5
|
-
// clobbering an fs object to create one of a different type.)
|
|
6
|
-
import * as fsm from '@isaacs/fs-minipass';
|
|
7
|
-
import assert from 'node:assert';
|
|
8
|
-
import { randomBytes } from 'node:crypto';
|
|
9
|
-
import fs from 'node:fs';
|
|
10
|
-
import path from 'node:path';
|
|
11
|
-
import { getWriteFlag } from './get-write-flag.js';
|
|
12
|
-
import { mkdir, mkdirSync } from './mkdir.js';
|
|
13
|
-
import { normalizeWindowsPath } from './normalize-windows-path.js';
|
|
14
|
-
import { Parser } from './parse.js';
|
|
15
|
-
import { stripAbsolutePath } from './strip-absolute-path.js';
|
|
1
|
+
import { SymlinkError } from './symlink-error.js';
|
|
16
2
|
import * as wc from './winchars.js';
|
|
3
|
+
import { TarReader, TarReaderSync } from './entries.js';
|
|
4
|
+
import { mkdir, mkdirSync } from './mkdir.js';
|
|
5
|
+
import { countOption, ownOptions, } from './options.js';
|
|
17
6
|
import { PathReservations } from './path-reservations.js';
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const ISREUSABLE = Symbol('isReusable');
|
|
24
|
-
const MAKEFS = Symbol('makeFs');
|
|
25
|
-
const FILE = Symbol('file');
|
|
26
|
-
const DIRECTORY = Symbol('directory');
|
|
27
|
-
const LINK = Symbol('link');
|
|
28
|
-
const SYMLINK = Symbol('symlink');
|
|
29
|
-
const HARDLINK = Symbol('hardlink');
|
|
30
|
-
const ENSURE_NO_SYMLINK = Symbol('ensureNoSymlink');
|
|
31
|
-
const UNSUPPORTED = Symbol('unsupported');
|
|
32
|
-
const CHECKPATH = Symbol('checkPath');
|
|
33
|
-
const STRIPABSOLUTEPATH = Symbol('stripAbsolutePath');
|
|
34
|
-
const MKDIR = Symbol('mkdir');
|
|
35
|
-
const ONERROR = Symbol('onError');
|
|
36
|
-
const PENDING = Symbol('pending');
|
|
37
|
-
const PEND = Symbol('pend');
|
|
38
|
-
const UNPEND = Symbol('unpend');
|
|
39
|
-
const ENDED = Symbol('ended');
|
|
40
|
-
const MAYBECLOSE = Symbol('maybeClose');
|
|
41
|
-
const SKIP = Symbol('skip');
|
|
42
|
-
const DOCHOWN = Symbol('doChown');
|
|
43
|
-
const UID = Symbol('uid');
|
|
44
|
-
const GID = Symbol('gid');
|
|
45
|
-
const CHECKED_CWD = Symbol('checkedCwd');
|
|
46
|
-
const platform = process.env.TESTING_TAR_FAKE_PLATFORM || process.platform;
|
|
47
|
-
const isWindows = platform === 'win32';
|
|
7
|
+
import { dirname, isAbsolute, join, normalize, relative, resolve, } from './path.js';
|
|
8
|
+
import { stripAbsolutePath } from './strip-absolute-path.js';
|
|
9
|
+
import { warn } from './warn.js';
|
|
10
|
+
import { win32Dirname, win32IsAbsolute, win32Resolve, win32Root, } from './win32-path.js';
|
|
11
|
+
import { writeFlags } from './write-flag.js';
|
|
48
12
|
const DEFAULT_MAX_DEPTH = 1024;
|
|
49
|
-
//
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
//
|
|
60
|
-
//
|
|
61
|
-
|
|
62
|
-
//
|
|
63
|
-
// See: https://github.com/npm/node-tar/issues/183
|
|
64
|
-
/* c8 ignore start */
|
|
65
|
-
const unlinkFile = (path, cb) => {
|
|
66
|
-
if (!isWindows) {
|
|
67
|
-
return fs.unlink(path, cb);
|
|
68
|
-
}
|
|
69
|
-
const name = path + '.DELETE.' + randomBytes(16).toString('hex');
|
|
70
|
-
fs.rename(path, name, er => {
|
|
71
|
-
if (er) {
|
|
72
|
-
return cb(er);
|
|
73
|
-
}
|
|
74
|
-
fs.unlink(name, cb);
|
|
75
|
-
});
|
|
76
|
-
};
|
|
77
|
-
/* c8 ignore stop */
|
|
78
|
-
/* c8 ignore start */
|
|
79
|
-
const unlinkFileSync = (path) => {
|
|
80
|
-
if (!isWindows) {
|
|
81
|
-
return fs.unlinkSync(path);
|
|
82
|
-
}
|
|
83
|
-
const name = path + '.DELETE.' + randomBytes(16).toString('hex');
|
|
84
|
-
fs.renameSync(path, name);
|
|
85
|
-
fs.unlinkSync(name);
|
|
86
|
-
};
|
|
87
|
-
/* c8 ignore stop */
|
|
88
|
-
// this.gid, entry.gid, this.processUid
|
|
89
|
-
const uint32 = (a, b, c) => a !== undefined && a === a >>> 0 ? a
|
|
13
|
+
// Longer than any path that a supported platform can create.
|
|
14
|
+
const MAX_PATH_LENGTH = 32768;
|
|
15
|
+
const DEFAULT_JOBS = 16;
|
|
16
|
+
const fileTypes = new Set([
|
|
17
|
+
'File',
|
|
18
|
+
'OldFile',
|
|
19
|
+
'ContiguousFile',
|
|
20
|
+
]);
|
|
21
|
+
const dirTypes = new Set(['Directory', 'GNUDumpDir']);
|
|
22
|
+
const linkTypes = new Set(['Link', 'SymbolicLink']);
|
|
23
|
+
// The first of the values that is an unsigned 32-bit integer, or the
|
|
24
|
+
// last value.
|
|
25
|
+
const uint32 = (a, b, c) => a !== undefined ? a
|
|
90
26
|
: b !== undefined && b === b >>> 0 ? b
|
|
91
27
|
: c;
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
28
|
+
/**
|
|
29
|
+
* The parts of the extraction that do not use the file system: the
|
|
30
|
+
* options, the checks of each entry, and the warnings. `Unpack` and
|
|
31
|
+
* `UnpackSync` add the file system work.
|
|
32
|
+
*/
|
|
33
|
+
export class UnpackBase {
|
|
34
|
+
/** The options, with no prototype. */
|
|
35
|
+
opt;
|
|
36
|
+
/** The absolute path of the extraction directory. */
|
|
37
|
+
cwd;
|
|
38
|
+
win32;
|
|
39
|
+
strip;
|
|
40
|
+
maxDepth;
|
|
41
|
+
preservePaths;
|
|
42
|
+
unlink;
|
|
43
|
+
keep;
|
|
44
|
+
newer;
|
|
45
|
+
noMtime;
|
|
46
|
+
chmod;
|
|
100
47
|
uid;
|
|
101
48
|
gid;
|
|
102
|
-
setOwner;
|
|
103
49
|
preserveOwner;
|
|
104
|
-
processGid;
|
|
105
|
-
processUid;
|
|
106
|
-
maxDepth;
|
|
107
50
|
forceChown;
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
keep;
|
|
111
|
-
noMtime;
|
|
112
|
-
preservePaths;
|
|
113
|
-
unlink;
|
|
114
|
-
cwd;
|
|
115
|
-
strip;
|
|
51
|
+
processUid;
|
|
52
|
+
processGid;
|
|
116
53
|
processUmask;
|
|
117
54
|
umask;
|
|
118
55
|
dmode;
|
|
119
56
|
fmode;
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
57
|
+
/** Gives the `open` flag for a new file of a given size. */
|
|
58
|
+
writeFlag;
|
|
59
|
+
/** Resolve a path from an absolute path. */
|
|
60
|
+
resolve;
|
|
61
|
+
/** The parent directory of an absolute path. */
|
|
62
|
+
dirname;
|
|
63
|
+
/** Set when an error stops the extraction. */
|
|
64
|
+
fatal;
|
|
65
|
+
constructor(options) {
|
|
66
|
+
const opt = (this.opt = ownOptions(options));
|
|
67
|
+
const win32 = (this.win32 = opt.platform === 'win32');
|
|
68
|
+
this.resolve = win32 ? win32Resolve : resolve;
|
|
69
|
+
this.dirname = win32 ? win32Dirname : dirname;
|
|
70
|
+
if (typeof opt.cwd !== 'string') {
|
|
71
|
+
throw new TypeError('cwd is required');
|
|
72
|
+
}
|
|
73
|
+
const cwd = win32 ? opt.cwd.replaceAll('\\', '/') : opt.cwd;
|
|
74
|
+
if (!(win32 ? win32IsAbsolute : isAbsolute)(cwd)) {
|
|
75
|
+
throw new TypeError('cwd must be an absolute path');
|
|
76
|
+
}
|
|
77
|
+
this.cwd = this.resolve(cwd, '');
|
|
129
78
|
if (typeof opt.uid === 'number' || typeof opt.gid === 'number') {
|
|
130
79
|
// need both or neither
|
|
131
80
|
if (typeof opt.uid !== 'number' || typeof opt.gid !== 'number') {
|
|
132
81
|
throw new TypeError('cannot set owner without number uid and gid');
|
|
133
82
|
}
|
|
83
|
+
// A value that is not valid must not fall back to the owner
|
|
84
|
+
// in the archive.
|
|
85
|
+
if (opt.uid !== opt.uid >>> 0 || opt.gid !== opt.gid >>> 0) {
|
|
86
|
+
throw new TypeError('uid and gid must be unsigned 32-bit integers');
|
|
87
|
+
}
|
|
134
88
|
if (opt.preserveOwner) {
|
|
135
89
|
throw new TypeError('cannot preserve owner in archive and also set owner explicitly');
|
|
136
90
|
}
|
|
137
91
|
this.uid = opt.uid;
|
|
138
92
|
this.gid = opt.gid;
|
|
139
|
-
this.setOwner = true;
|
|
140
|
-
}
|
|
141
|
-
else {
|
|
142
|
-
this.uid = undefined;
|
|
143
|
-
this.gid = undefined;
|
|
144
|
-
this.setOwner = false;
|
|
145
93
|
}
|
|
146
|
-
|
|
94
|
+
this.processUid = opt.processUid;
|
|
95
|
+
this.processGid = opt.processGid;
|
|
147
96
|
this.preserveOwner =
|
|
148
97
|
opt.preserveOwner === undefined && typeof opt.uid !== 'number' ?
|
|
149
|
-
|
|
98
|
+
opt.processUid === 0
|
|
150
99
|
: !!opt.preserveOwner;
|
|
151
|
-
this.
|
|
152
|
-
(this.preserveOwner || this.setOwner) && process.getuid ?
|
|
153
|
-
process.getuid()
|
|
154
|
-
: undefined;
|
|
155
|
-
this.processGid =
|
|
156
|
-
(this.preserveOwner || this.setOwner) && process.getgid ?
|
|
157
|
-
process.getgid()
|
|
158
|
-
: undefined;
|
|
159
|
-
// prevent excessively deep nesting of subfolders
|
|
160
|
-
// set to `Infinity` to remove this restriction
|
|
100
|
+
this.forceChown = opt.forceChown === true;
|
|
161
101
|
this.maxDepth =
|
|
162
102
|
typeof opt.maxDepth === 'number' ? opt.maxDepth : DEFAULT_MAX_DEPTH;
|
|
163
|
-
|
|
164
|
-
// Forcibly trigger a chown on every entry, no matter what
|
|
165
|
-
this.forceChown = opt.forceChown === true;
|
|
166
|
-
// turn ><?| in filenames into 0xf000-higher encoded forms
|
|
167
|
-
this.win32 = !!opt.win32 || isWindows;
|
|
168
|
-
// do not unpack over files that are newer than what's in the archive
|
|
169
|
-
this.newer = !!opt.newer;
|
|
170
|
-
// do not unpack over ANY files
|
|
171
|
-
this.keep = !!opt.keep;
|
|
172
|
-
// do not set mtime/atime of extracted entries
|
|
173
|
-
this.noMtime = !!opt.noMtime;
|
|
174
|
-
// allow .., absolute path entries, and unpacking through symlinks
|
|
175
|
-
// without this, warn and skip .., relativize absolutes, and error
|
|
176
|
-
// on symlinks in extraction path
|
|
103
|
+
this.strip = Number(opt.strip) || 0;
|
|
177
104
|
this.preservePaths = !!opt.preservePaths;
|
|
178
|
-
// unlink files and links before writing. This breaks existing hard
|
|
179
|
-
// links, and removes symlink directories rather than erroring
|
|
180
105
|
this.unlink = !!opt.unlink;
|
|
181
|
-
this.
|
|
182
|
-
this.
|
|
183
|
-
|
|
106
|
+
this.keep = !!opt.keep;
|
|
107
|
+
this.newer = !!opt.newer;
|
|
108
|
+
this.noMtime = !!opt.noMtime;
|
|
109
|
+
this.chmod = !!opt.chmod;
|
|
110
|
+
// Without chmod, the umask of the process is not needed.
|
|
184
111
|
this.processUmask =
|
|
185
112
|
!this.chmod ? 0
|
|
186
113
|
: typeof opt.processUmask === 'number' ? opt.processUmask
|
|
187
|
-
:
|
|
114
|
+
: 0o22;
|
|
188
115
|
this.umask =
|
|
189
116
|
typeof opt.umask === 'number' ? opt.umask : this.processUmask;
|
|
190
|
-
|
|
191
|
-
this.
|
|
192
|
-
this.
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
117
|
+
this.dmode = opt.dmode || 0o777 & ~this.umask;
|
|
118
|
+
this.fmode = opt.fmode || 0o666 & ~this.umask;
|
|
119
|
+
this.writeFlag = writeFlags(opt.fs.constants, win32);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Report a problem to `onwarn`, or throw it if `strict` is set. A
|
|
123
|
+
* damaged archive is always an error here, because the extraction
|
|
124
|
+
* cannot be complete.
|
|
125
|
+
*/
|
|
126
|
+
warn(code, message, data = {}) {
|
|
199
127
|
if (code === 'TAR_BAD_ARCHIVE' || code === 'TAR_ABORT') {
|
|
200
128
|
data.recoverable = false;
|
|
201
129
|
}
|
|
202
|
-
|
|
130
|
+
data.cwd = this.cwd;
|
|
131
|
+
warn(this.opt, code, message, data);
|
|
203
132
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
133
|
+
/** The options for the reader. Its warnings go to `warn()`. */
|
|
134
|
+
readerOptions() {
|
|
135
|
+
return ownOptions(this.opt, {
|
|
136
|
+
strict: false,
|
|
137
|
+
onwarn: (code, message, data) => data instanceof Error ?
|
|
138
|
+
this.warn(code, data)
|
|
139
|
+
: this.warn(code, message, data),
|
|
140
|
+
});
|
|
210
141
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
142
|
+
/**
|
|
143
|
+
* Report the failure of one entry as a `TAR_ENTRY_ERROR` warning.
|
|
144
|
+
* Throws the error if it stops the extraction: the extraction
|
|
145
|
+
* directory is not usable, the archive cannot be read, or `strict`
|
|
146
|
+
* is set.
|
|
147
|
+
*/
|
|
148
|
+
entryError(er, entry) {
|
|
149
|
+
if (this.fatal?.error === er || er?.name === 'CwdError') {
|
|
150
|
+
throw er;
|
|
151
|
+
}
|
|
152
|
+
this.warn('TAR_ENTRY_ERROR', er, { entry });
|
|
153
|
+
}
|
|
154
|
+
/** Record the error that stops the extraction. Returns it. */
|
|
155
|
+
fail(error) {
|
|
156
|
+
this.fatal ??= { error };
|
|
157
|
+
return error;
|
|
158
|
+
}
|
|
159
|
+
// Remove the root of an absolute path, and refuse a path that can
|
|
160
|
+
// leave the extraction directory. False if the entry is refused.
|
|
161
|
+
#stripAbsolutePath(entry, field) {
|
|
214
162
|
const p = entry[field];
|
|
215
|
-
const { type } = entry;
|
|
216
163
|
if (!p || this.preservePaths)
|
|
217
164
|
return true;
|
|
218
|
-
// strip off the root
|
|
219
165
|
const [root, stripped] = stripAbsolutePath(p);
|
|
220
|
-
const parts = stripped.replaceAll(
|
|
221
|
-
if (parts.includes('..')
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
//
|
|
225
|
-
|
|
226
|
-
// '../sibling/file' are valid if they resolve within the cwd.
|
|
227
|
-
// For paths, they just simply may not ever use .. at all.
|
|
228
|
-
if (field === 'path' || type === 'Link') {
|
|
166
|
+
const parts = stripped.replaceAll('\\', '/').split('/');
|
|
167
|
+
if (parts.includes('..')) {
|
|
168
|
+
// A path, and the target of a hard link, cannot have '..' at
|
|
169
|
+
// all. The target of a symbolic link is relative to the
|
|
170
|
+
// directory of the link, so '../sibling/file' can be valid.
|
|
171
|
+
if (field === 'path' || entry.type === 'Link') {
|
|
229
172
|
this.warn('TAR_ENTRY_ERROR', `${field} contains '..'`, {
|
|
230
173
|
entry,
|
|
231
174
|
[field]: p,
|
|
232
175
|
});
|
|
233
|
-
// not ok!
|
|
234
176
|
return false;
|
|
235
177
|
}
|
|
236
|
-
//
|
|
237
|
-
//
|
|
238
|
-
//
|
|
239
|
-
const
|
|
240
|
-
|
|
241
|
-
|
|
178
|
+
// A '..' is only safe at the start of the target. Any named
|
|
179
|
+
// part before it can be a symlink, and then the '..' goes to
|
|
180
|
+
// the parent of the target of that symlink.
|
|
181
|
+
const firstName = parts.findIndex(part => part !== '..' && part !== '.' && part !== '');
|
|
182
|
+
if (firstName !== -1 && parts.includes('..', firstName)) {
|
|
183
|
+
this.warn('TAR_ENTRY_ERROR', `${field} contains '..' after a path portion`, { entry, [field]: p });
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
// Normalize the path, so that the directory is the one where
|
|
187
|
+
// the link is created. The path 'x/.' creates the link 'x'.
|
|
188
|
+
const entryDir = dirname(normalize(entry.path));
|
|
189
|
+
const resolved = normalize(join(entryDir, parts.join('/')));
|
|
242
190
|
if (resolved.startsWith('../') || resolved === '..') {
|
|
243
|
-
this.warn('TAR_ENTRY_ERROR', `${field} escapes extraction directory`, {
|
|
244
|
-
entry,
|
|
245
|
-
[field]: p,
|
|
246
|
-
});
|
|
191
|
+
this.warn('TAR_ENTRY_ERROR', `${field} escapes extraction directory`, { entry, [field]: p });
|
|
247
192
|
return false;
|
|
248
193
|
}
|
|
249
194
|
}
|
|
250
195
|
if (root) {
|
|
251
|
-
|
|
252
|
-
entry[field] =
|
|
253
|
-
this.warn('TAR_ENTRY_INFO', `stripping ${root} from absolute ${field}`, {
|
|
254
|
-
entry,
|
|
255
|
-
[field]: p,
|
|
256
|
-
});
|
|
196
|
+
;
|
|
197
|
+
entry[field] = stripped;
|
|
198
|
+
this.warn('TAR_ENTRY_INFO', `stripping ${root} from absolute ${field}`, { entry, [field]: p });
|
|
257
199
|
}
|
|
258
200
|
return true;
|
|
259
201
|
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
202
|
+
/**
|
|
203
|
+
* Check the entry, with no use of the file system. Set its
|
|
204
|
+
* `absolute` path, and change its `path`, `linkpath`, and `mode` to
|
|
205
|
+
* the values to extract. False if the entry must not be extracted.
|
|
206
|
+
*/
|
|
207
|
+
check(entry) {
|
|
208
|
+
const p = entry.path;
|
|
263
209
|
const parts = p.split('/');
|
|
264
210
|
if (this.strip) {
|
|
265
|
-
if (parts.length < this.strip)
|
|
211
|
+
if (parts.length < this.strip)
|
|
266
212
|
return false;
|
|
267
|
-
}
|
|
268
213
|
if (entry.type === 'Link') {
|
|
269
|
-
const linkparts =
|
|
270
|
-
if (linkparts.length
|
|
271
|
-
entry.linkpath = linkparts.slice(this.strip).join('/');
|
|
272
|
-
}
|
|
273
|
-
else {
|
|
214
|
+
const linkparts = String(entry.linkpath).split('/');
|
|
215
|
+
if (linkparts.length < this.strip)
|
|
274
216
|
return false;
|
|
275
|
-
|
|
217
|
+
entry.linkpath = linkparts.slice(this.strip).join('/');
|
|
276
218
|
}
|
|
277
219
|
parts.splice(0, this.strip);
|
|
220
|
+
// No part of the path is left, as for the top directory of a
|
|
221
|
+
// package. The entry must not change the extraction directory.
|
|
222
|
+
if (parts.every(part => part === ''))
|
|
223
|
+
return false;
|
|
278
224
|
entry.path = parts.join('/');
|
|
279
225
|
}
|
|
280
|
-
|
|
226
|
+
const { maxDepth } = this;
|
|
227
|
+
if (isFinite(maxDepth) && parts.length > maxDepth) {
|
|
281
228
|
this.warn('TAR_ENTRY_ERROR', 'path excessively deep', {
|
|
282
229
|
entry,
|
|
283
230
|
path: p,
|
|
284
231
|
depth: parts.length,
|
|
285
|
-
maxDepth
|
|
232
|
+
maxDepth,
|
|
233
|
+
});
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
// The path reservations do work for each parent directory of the
|
|
237
|
+
// path and of the linkpath.
|
|
238
|
+
const { linkpath } = entry;
|
|
239
|
+
if (linkpath &&
|
|
240
|
+
isFinite(maxDepth) &&
|
|
241
|
+
linkpath.split('/').length > maxDepth) {
|
|
242
|
+
this.warn('TAR_ENTRY_ERROR', 'linkpath excessively deep', {
|
|
243
|
+
entry,
|
|
244
|
+
path: p,
|
|
245
|
+
linkpath,
|
|
246
|
+
maxDepth,
|
|
286
247
|
});
|
|
287
248
|
return false;
|
|
288
249
|
}
|
|
289
|
-
|
|
290
|
-
|
|
250
|
+
for (const field of ['path', 'linkpath']) {
|
|
251
|
+
const { length } = entry[field] ?? '';
|
|
252
|
+
if (length > MAX_PATH_LENGTH) {
|
|
253
|
+
this.warn('TAR_ENTRY_ERROR', `${field} excessively long`, {
|
|
254
|
+
entry,
|
|
255
|
+
path: p,
|
|
256
|
+
length,
|
|
257
|
+
maxLength: MAX_PATH_LENGTH,
|
|
258
|
+
});
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
if (!this.#stripAbsolutePath(entry, 'path') ||
|
|
263
|
+
!this.#stripAbsolutePath(entry, 'linkpath')) {
|
|
291
264
|
return false;
|
|
292
265
|
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
// if we somehow ended up with a path that escapes the cwd, and we are
|
|
298
|
-
// not in preservePaths mode, then something is fishy! This should have
|
|
299
|
-
// been prevented above, so ignore this for coverage.
|
|
300
|
-
/* c8 ignore start - defense in depth */
|
|
266
|
+
const { cwd } = this;
|
|
267
|
+
let absolute = this.resolve(cwd, entry.path);
|
|
268
|
+
// The checks above refuse a path that leaves the extraction
|
|
269
|
+
// directory. This check is defense in depth.
|
|
301
270
|
if (!this.preservePaths &&
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
271
|
+
absolute !== cwd &&
|
|
272
|
+
// A root, such as `/` or `c:/`, ends with a slash.
|
|
273
|
+
!absolute.startsWith(cwd.endsWith('/') ? cwd : cwd + '/')) {
|
|
305
274
|
this.warn('TAR_ENTRY_ERROR', 'path escaped extraction target', {
|
|
306
275
|
entry,
|
|
307
|
-
path:
|
|
308
|
-
resolvedPath:
|
|
309
|
-
cwd
|
|
276
|
+
path: entry.path,
|
|
277
|
+
resolvedPath: absolute,
|
|
278
|
+
cwd,
|
|
310
279
|
});
|
|
311
280
|
return false;
|
|
312
281
|
}
|
|
313
|
-
|
|
314
|
-
//
|
|
315
|
-
|
|
316
|
-
if (entry.absolute === this.cwd &&
|
|
317
|
-
entry.type !== 'Directory' &&
|
|
318
|
-
entry.type !== 'GNUDumpDir') {
|
|
282
|
+
// An archive can set properties of the extraction directory, but
|
|
283
|
+
// it cannot replace it with a different kind of thing.
|
|
284
|
+
if (absolute === cwd && !dirTypes.has(entry.type))
|
|
319
285
|
return false;
|
|
320
|
-
|
|
321
|
-
// only encode : chars that aren't drive letter indicators
|
|
286
|
+
// Do not change the ':' of a drive letter.
|
|
322
287
|
if (this.win32) {
|
|
323
|
-
const
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
const { root: pRoot } = path.win32.parse(entry.path);
|
|
288
|
+
const aRoot = win32Root(absolute);
|
|
289
|
+
absolute = aRoot + wc.encode(absolute.slice(aRoot.length));
|
|
290
|
+
const pRoot = win32Root(entry.path);
|
|
327
291
|
entry.path = pRoot + wc.encode(entry.path.slice(pRoot.length));
|
|
328
292
|
}
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
// eslint-disable-next-line no-fallthrough
|
|
343
|
-
case 'File':
|
|
344
|
-
case 'OldFile':
|
|
345
|
-
case 'ContiguousFile':
|
|
346
|
-
case 'Link':
|
|
347
|
-
case 'SymbolicLink':
|
|
348
|
-
return this[CHECKFS](entry);
|
|
349
|
-
case 'CharacterDevice':
|
|
350
|
-
case 'BlockDevice':
|
|
351
|
-
case 'FIFO':
|
|
352
|
-
default:
|
|
353
|
-
return this[UNSUPPORTED](entry);
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
[ONERROR](er, entry) {
|
|
357
|
-
// Cwd has to exist, or else nothing works. That's serious.
|
|
358
|
-
// Other errors are warnings, which raise the error in strict
|
|
359
|
-
// mode, but otherwise continue on.
|
|
360
|
-
if (er.name === 'CwdError') {
|
|
361
|
-
this.emit('error', er);
|
|
362
|
-
}
|
|
363
|
-
else {
|
|
364
|
-
this.warn('TAR_ENTRY_ERROR', er, { entry });
|
|
365
|
-
this[UNPEND]();
|
|
366
|
-
entry.resume();
|
|
293
|
+
entry.absolute = absolute;
|
|
294
|
+
// Never set the setuid, setgid, or sticky bit from an archive.
|
|
295
|
+
if (typeof entry.mode === 'number')
|
|
296
|
+
entry.mode &= 0o777;
|
|
297
|
+
if (dirTypes.has(entry.type)) {
|
|
298
|
+
if (entry.mode)
|
|
299
|
+
entry.mode |= 0o700;
|
|
300
|
+
}
|
|
301
|
+
else if (!fileTypes.has(entry.type) && !linkTypes.has(entry.type)) {
|
|
302
|
+
// A device or a FIFO is never made.
|
|
303
|
+
entry.unsupported = true;
|
|
304
|
+
this.warn('TAR_ENTRY_UNSUPPORTED', `unsupported entry type: ${entry.type}`, { entry });
|
|
305
|
+
return false;
|
|
367
306
|
}
|
|
307
|
+
return true;
|
|
368
308
|
}
|
|
369
|
-
|
|
370
|
-
|
|
309
|
+
/**
|
|
310
|
+
* The paths that the entry must have for itself while it is made.
|
|
311
|
+
* A link also reserves its target, so that an entry that makes the
|
|
312
|
+
* target, and an entry that goes through the link, do not run at
|
|
313
|
+
* the same time as the link.
|
|
314
|
+
*/
|
|
315
|
+
reservedPaths(entry) {
|
|
316
|
+
const { path, linkpath } = entry;
|
|
317
|
+
if (!linkpath)
|
|
318
|
+
return [path];
|
|
319
|
+
// The target of a symlink is relative to the directory of the
|
|
320
|
+
// link. The target of a hard link is relative to the cwd.
|
|
321
|
+
return [
|
|
322
|
+
path,
|
|
323
|
+
entry.type === 'SymbolicLink' && !isAbsolute(linkpath) ?
|
|
324
|
+
join(dirname(path), linkpath)
|
|
325
|
+
: linkpath,
|
|
326
|
+
];
|
|
327
|
+
}
|
|
328
|
+
/** The options to make a directory with a mode. */
|
|
329
|
+
mkdirOptions(mode) {
|
|
330
|
+
return {
|
|
371
331
|
uid: this.uid,
|
|
372
332
|
gid: this.gid,
|
|
373
333
|
processUid: this.processUid,
|
|
@@ -376,516 +336,591 @@ export class Unpack extends Parser {
|
|
|
376
336
|
preserve: this.preservePaths,
|
|
377
337
|
unlink: this.unlink,
|
|
378
338
|
cwd: this.cwd,
|
|
379
|
-
mode
|
|
380
|
-
}
|
|
339
|
+
mode,
|
|
340
|
+
};
|
|
381
341
|
}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
342
|
+
/**
|
|
343
|
+
* The owner to set on the entry, if it needs one: with
|
|
344
|
+
* `preserveOwner`, when the owner in the archive is not the process
|
|
345
|
+
* owner, and with `uid` and `gid`, when they are not the process
|
|
346
|
+
* owner.
|
|
347
|
+
*/
|
|
348
|
+
owner(entry) {
|
|
349
|
+
const doChown = this.forceChown ||
|
|
386
350
|
(this.preserveOwner &&
|
|
387
351
|
((typeof entry.uid === 'number' &&
|
|
388
352
|
entry.uid !== this.processUid) ||
|
|
389
353
|
(typeof entry.gid === 'number' &&
|
|
390
354
|
entry.gid !== this.processGid))) ||
|
|
391
355
|
(typeof this.uid === 'number' && this.uid !== this.processUid) ||
|
|
392
|
-
(typeof this.gid === 'number' && this.gid !== this.processGid)
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
return
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
356
|
+
(typeof this.gid === 'number' && this.gid !== this.processGid);
|
|
357
|
+
const uid = uint32(this.uid, entry.uid, this.processUid);
|
|
358
|
+
const gid = uint32(this.gid, entry.gid, this.processGid);
|
|
359
|
+
return doChown && typeof uid === 'number' && typeof gid === 'number' ?
|
|
360
|
+
{ uid, gid }
|
|
361
|
+
: undefined;
|
|
362
|
+
}
|
|
363
|
+
/** The times to set on the entry, if it needs them. */
|
|
364
|
+
times(entry) {
|
|
365
|
+
return entry.mtime && !this.noMtime ?
|
|
366
|
+
{ atime: entry.atime || new Date(), mtime: entry.mtime }
|
|
367
|
+
: undefined;
|
|
368
|
+
}
|
|
369
|
+
/** True if the object on disk at the path of the entry must stay. */
|
|
370
|
+
skip(entry, st) {
|
|
371
|
+
return (this.keep || (this.newer && st.mtime > (entry.mtime ?? st.mtime)));
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* True if the entry can be written into the file that is on disk.
|
|
375
|
+
* A file with more than one link is not used, because the write
|
|
376
|
+
* would change the other links. Windows does not report a useful
|
|
377
|
+
* link count.
|
|
378
|
+
*/
|
|
379
|
+
isReusable(entry, st) {
|
|
380
|
+
return (entry.type === 'File' &&
|
|
381
|
+
!this.unlink &&
|
|
382
|
+
st.isFile() &&
|
|
383
|
+
st.nlink <= 1 &&
|
|
384
|
+
!this.win32);
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* The parts of the path from the extraction directory to the
|
|
388
|
+
* target of a link entry. None of them can be a symbolic link.
|
|
389
|
+
*/
|
|
390
|
+
linkParts(entry, absolute) {
|
|
391
|
+
const linkpath = String(entry.linkpath);
|
|
392
|
+
return entry.type === 'Link' ?
|
|
393
|
+
linkpath.split('/')
|
|
394
|
+
: relative(this.cwd, this.resolve(this.dirname(absolute), linkpath)).split('/');
|
|
395
|
+
}
|
|
396
|
+
/** The error for a link target that goes through a symbolic link. */
|
|
397
|
+
symlinkError(symlink, parts) {
|
|
398
|
+
return new SymlinkError(symlink, this.resolve(this.cwd, parts.join('/')));
|
|
399
399
|
}
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
400
|
+
}
|
|
401
|
+
// Unlinks on Windows are not atomic. A file that is made at the path
|
|
402
|
+
// of a file that was just removed can be removed with it. So rename
|
|
403
|
+
// the file first, and then remove it under the new name.
|
|
404
|
+
const deleteName = (path) => path +
|
|
405
|
+
'.DELETE.' +
|
|
406
|
+
Array.from(crypto.getRandomValues(new Uint8Array(16)), b => b.toString(16).padStart(2, '0')).join('');
|
|
407
|
+
/**
|
|
408
|
+
* Extracts the entries of an archive with async file system
|
|
409
|
+
* functions.
|
|
410
|
+
*
|
|
411
|
+
* Several entries are extracted at one time. Entries that use the
|
|
412
|
+
* same path, or a path and a directory above it, are extracted in
|
|
413
|
+
* the order of the archive.
|
|
414
|
+
*/
|
|
415
|
+
export class Unpack extends UnpackBase {
|
|
416
|
+
#fs;
|
|
417
|
+
#reservations;
|
|
418
|
+
#jobs;
|
|
419
|
+
// the number of entries that are in work
|
|
420
|
+
#active = 0;
|
|
421
|
+
#wake;
|
|
422
|
+
#checkedCwd = false;
|
|
423
|
+
#cwdCheck;
|
|
424
|
+
constructor(opt) {
|
|
425
|
+
super(opt);
|
|
426
|
+
this.#fs = this.opt.fs;
|
|
427
|
+
this.#reservations = new PathReservations(this.win32);
|
|
428
|
+
this.#jobs = countOption('jobs', this.opt.jobs, DEFAULT_JOBS);
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Extract the archive. The promise settles when all the file
|
|
432
|
+
* system work is done. It rejects with the first error that stops
|
|
433
|
+
* the extraction.
|
|
434
|
+
*/
|
|
435
|
+
async extract(input) {
|
|
436
|
+
const reader = new TarReader(input, this.readerOptions());
|
|
437
|
+
const { onEntry } = this.opt;
|
|
438
|
+
const stream = !!this.opt.transform;
|
|
439
|
+
try {
|
|
440
|
+
while (!this.fatal) {
|
|
441
|
+
const entry = reader.poll();
|
|
442
|
+
if (entry === null)
|
|
443
|
+
break;
|
|
444
|
+
if (entry === undefined) {
|
|
445
|
+
await reader.fill();
|
|
446
|
+
continue;
|
|
425
447
|
}
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
448
|
+
if (onEntry)
|
|
449
|
+
await onEntry(entry);
|
|
450
|
+
// The caller took the content, so there is nothing to write.
|
|
451
|
+
if (entry.bodyUsed)
|
|
452
|
+
continue;
|
|
453
|
+
if (!this.check(entry))
|
|
454
|
+
continue;
|
|
455
|
+
// A file whose content is all in memory is written while the
|
|
456
|
+
// next entries are read. Other files are written as their
|
|
457
|
+
// content arrives.
|
|
458
|
+
const body = [];
|
|
459
|
+
let inMemory = !stream;
|
|
460
|
+
if (inMemory && fileTypes.has(entry.type)) {
|
|
461
|
+
for (;;) {
|
|
462
|
+
const part = reader.readBody(entry);
|
|
463
|
+
if (part === null)
|
|
464
|
+
break;
|
|
465
|
+
if (part === undefined) {
|
|
466
|
+
inMemory = false;
|
|
467
|
+
break;
|
|
439
468
|
}
|
|
440
|
-
|
|
441
|
-
}
|
|
469
|
+
body.push(part);
|
|
470
|
+
}
|
|
442
471
|
}
|
|
472
|
+
const work = this.#start(entry, body, inMemory ? undefined : reader);
|
|
473
|
+
if (!inMemory)
|
|
474
|
+
await work;
|
|
475
|
+
while (this.#active >= this.#jobs)
|
|
476
|
+
await this.#idle();
|
|
443
477
|
}
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
fs.futimes(fd, atime, mtime, er => er ?
|
|
456
|
-
fs.utimes(abs, atime, mtime, er2 => done(er2 && er))
|
|
457
|
-
: done());
|
|
458
|
-
}
|
|
459
|
-
if (typeof fd === 'number' && this[DOCHOWN](entry)) {
|
|
460
|
-
actions++;
|
|
461
|
-
const uid = this[UID](entry);
|
|
462
|
-
const gid = this[GID](entry);
|
|
463
|
-
if (typeof uid === 'number' && typeof gid === 'number') {
|
|
464
|
-
fs.fchown(fd, uid, gid, er => er ? fs.chown(abs, uid, gid, er2 => done(er2 && er)) : done());
|
|
465
|
-
}
|
|
478
|
+
}
|
|
479
|
+
catch (er) {
|
|
480
|
+
this.fail(er);
|
|
481
|
+
}
|
|
482
|
+
await reader.return().catch(er => this.fail(er));
|
|
483
|
+
while (this.#active > 0)
|
|
484
|
+
await this.#idle();
|
|
485
|
+
if (this.fatal) {
|
|
486
|
+
const { error } = this.fatal;
|
|
487
|
+
if (error && typeof error === 'object' && 'tarCode' in error) {
|
|
488
|
+
Object.assign(error, { cwd: this.cwd });
|
|
466
489
|
}
|
|
467
|
-
|
|
468
|
-
});
|
|
469
|
-
const tx = this.transform ? this.transform(entry) || entry : entry;
|
|
470
|
-
if (tx !== entry) {
|
|
471
|
-
tx.on('error', er => {
|
|
472
|
-
this[ONERROR](er, entry);
|
|
473
|
-
fullyDone();
|
|
474
|
-
});
|
|
475
|
-
entry.pipe(tx);
|
|
490
|
+
throw error;
|
|
476
491
|
}
|
|
477
|
-
tx.pipe(stream);
|
|
478
492
|
}
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
493
|
+
// Resolves when an entry that is in work is done.
|
|
494
|
+
#idle() {
|
|
495
|
+
return new Promise(res => {
|
|
496
|
+
this.#wake = res;
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
// Extract the entry when its paths are free. The promise never
|
|
500
|
+
// rejects: a failure is a warning, or it sets `fatal`.
|
|
501
|
+
#start(entry, body, reader) {
|
|
502
|
+
this.#active++;
|
|
503
|
+
return this.#reservations.reserve(this.reservedPaths(entry), async () => {
|
|
504
|
+
try {
|
|
505
|
+
if (!this.fatal)
|
|
506
|
+
await this.#extract(entry, body, reader);
|
|
486
507
|
}
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
508
|
+
catch (er) {
|
|
509
|
+
try {
|
|
510
|
+
this.entryError(er, entry);
|
|
511
|
+
}
|
|
512
|
+
catch (fatal) {
|
|
513
|
+
this.fail(fatal);
|
|
493
514
|
}
|
|
494
|
-
};
|
|
495
|
-
if (entry.mtime && !this.noMtime) {
|
|
496
|
-
actions++;
|
|
497
|
-
fs.utimes(String(entry.absolute), entry.atime || new Date(), entry.mtime, done);
|
|
498
|
-
}
|
|
499
|
-
if (this[DOCHOWN](entry)) {
|
|
500
|
-
actions++;
|
|
501
|
-
fs.chown(String(entry.absolute), Number(this[UID](entry)), Number(this[GID](entry)), done);
|
|
502
515
|
}
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
}
|
|
506
|
-
[UNSUPPORTED](entry) {
|
|
507
|
-
entry.unsupported = true;
|
|
508
|
-
this.warn('TAR_ENTRY_UNSUPPORTED', `unsupported entry type: ${entry.type}`, { entry });
|
|
509
|
-
entry.resume();
|
|
510
|
-
}
|
|
511
|
-
[SYMLINK](entry, done) {
|
|
512
|
-
const parts = normalizeWindowsPath(path.relative(this.cwd, path.resolve(path.dirname(String(entry.absolute)), String(entry.linkpath)))).split('/');
|
|
513
|
-
this[ENSURE_NO_SYMLINK](entry, this.cwd, parts, () => this[LINK](entry, String(entry.linkpath), 'symlink', done), er => {
|
|
514
|
-
this[ONERROR](er, entry);
|
|
515
|
-
done();
|
|
516
|
-
});
|
|
517
|
-
}
|
|
518
|
-
[HARDLINK](entry, done) {
|
|
519
|
-
const linkpath = normalizeWindowsPath(path.resolve(this.cwd, String(entry.linkpath)));
|
|
520
|
-
const parts = normalizeWindowsPath(String(entry.linkpath)).split('/');
|
|
521
|
-
this[ENSURE_NO_SYMLINK](entry, this.cwd, parts, () => this[LINK](entry, linkpath, 'link', done), er => {
|
|
522
|
-
this[ONERROR](er, entry);
|
|
523
|
-
done();
|
|
516
|
+
this.#active--;
|
|
517
|
+
this.#wake?.();
|
|
524
518
|
});
|
|
525
519
|
}
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
520
|
+
#mkdir(dir, mode) {
|
|
521
|
+
return mkdir(this.#fs, dir, this.mkdirOptions(mode));
|
|
522
|
+
}
|
|
523
|
+
// The extraction directory must exist. Entries that start at the
|
|
524
|
+
// same time share one check.
|
|
525
|
+
#checkCwd() {
|
|
526
|
+
return (this.#cwdCheck ??= this.#mkdir(this.cwd, this.dmode).then(() => {
|
|
527
|
+
this.#checkedCwd = true;
|
|
528
|
+
}, er => {
|
|
529
|
+
this.#cwdCheck = undefined;
|
|
530
|
+
throw er;
|
|
531
|
+
}));
|
|
532
|
+
}
|
|
533
|
+
async #extract(entry, body, reader) {
|
|
534
|
+
const fs = this.#fs;
|
|
535
|
+
const absolute = entry.absolute;
|
|
536
|
+
const { cwd } = this;
|
|
537
|
+
if (!this.#checkedCwd)
|
|
538
|
+
await this.#checkCwd();
|
|
539
|
+
if (absolute !== cwd) {
|
|
540
|
+
const parent = this.dirname(absolute);
|
|
541
|
+
if (parent !== cwd)
|
|
542
|
+
await this.#mkdir(parent, this.dmode);
|
|
543
|
+
}
|
|
544
|
+
// Remove what is at the path, if the entry cannot use it.
|
|
545
|
+
const st = await fs.lstat(absolute).catch(() => undefined);
|
|
546
|
+
if (st && this.skip(entry, st))
|
|
547
|
+
return;
|
|
548
|
+
if (st && !this.isReusable(entry, st)) {
|
|
549
|
+
if (st.isDirectory() && dirTypes.has(entry.type)) {
|
|
550
|
+
if (this.chmod &&
|
|
551
|
+
entry.mode &&
|
|
552
|
+
(st.mode & 0o7777) !== entry.mode) {
|
|
553
|
+
await fs.chmod(absolute, entry.mode);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
else if (absolute !== cwd) {
|
|
557
|
+
if (st.isDirectory())
|
|
558
|
+
await fs.rmdir(absolute);
|
|
559
|
+
else
|
|
560
|
+
await this.#unlink(absolute);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
if (fileTypes.has(entry.type)) {
|
|
564
|
+
return this.#file(entry, absolute, body, reader);
|
|
565
|
+
}
|
|
566
|
+
if (dirTypes.has(entry.type))
|
|
567
|
+
return this.#directory(entry, absolute);
|
|
568
|
+
await this.#ensureNoSymlink(this.linkParts(entry, absolute));
|
|
569
|
+
const linkpath = String(entry.linkpath);
|
|
570
|
+
if (entry.type === 'Link') {
|
|
571
|
+
await fs.link(this.resolve(cwd, linkpath), absolute);
|
|
572
|
+
}
|
|
573
|
+
else {
|
|
574
|
+
await fs.symlink(linkpath, absolute);
|
|
575
|
+
}
|
|
546
576
|
}
|
|
547
|
-
|
|
548
|
-
this
|
|
549
|
-
|
|
577
|
+
async #unlink(path) {
|
|
578
|
+
const fs = this.#fs;
|
|
579
|
+
if (!this.win32)
|
|
580
|
+
return fs.unlink(path);
|
|
581
|
+
const name = deleteName(path);
|
|
582
|
+
await fs.rename(path, name);
|
|
583
|
+
await fs.unlink(name);
|
|
550
584
|
}
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
st.
|
|
558
|
-
st
|
|
559
|
-
|
|
585
|
+
async #ensureNoSymlink(parts) {
|
|
586
|
+
if (this.preservePaths)
|
|
587
|
+
return;
|
|
588
|
+
let t = this.cwd;
|
|
589
|
+
for (const p of parts) {
|
|
590
|
+
t = this.resolve(t, p);
|
|
591
|
+
const st = await this.#fs.lstat(t).catch(() => undefined);
|
|
592
|
+
if (!st)
|
|
593
|
+
return;
|
|
594
|
+
if (st.isSymbolicLink())
|
|
595
|
+
throw this.symlinkError(t, parts);
|
|
596
|
+
}
|
|
560
597
|
}
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
this
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
598
|
+
async #directory(entry, absolute) {
|
|
599
|
+
const fs = this.#fs;
|
|
600
|
+
await this.#mkdir(absolute, entry.mode ?? this.dmode);
|
|
601
|
+
// These do not follow a symbolic link that replaced the
|
|
602
|
+
// directory. A failure is not an error of the entry.
|
|
603
|
+
const times = this.times(entry);
|
|
604
|
+
if (times) {
|
|
605
|
+
await fs.lutimes(absolute, times.atime, times.mtime).catch(() => { });
|
|
606
|
+
}
|
|
607
|
+
const owner = this.owner(entry);
|
|
608
|
+
if (owner) {
|
|
609
|
+
await fs.lchown(absolute, owner.uid, owner.gid).catch(() => { });
|
|
567
610
|
}
|
|
568
|
-
this.reservations.reserve(paths, done => this[CHECKFS2](entry, done));
|
|
569
611
|
}
|
|
570
|
-
|
|
571
|
-
const
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
612
|
+
async #file(entry, absolute, body, reader) {
|
|
613
|
+
const fs = this.#fs;
|
|
614
|
+
const handle = await fs.open(absolute, this.writeFlag(entry.size), entry.mode ?? this.fmode);
|
|
615
|
+
try {
|
|
616
|
+
if (reader)
|
|
617
|
+
await this.#pipe(entry, body, reader, handle);
|
|
618
|
+
else
|
|
619
|
+
for (const part of body)
|
|
620
|
+
await handle.write(part);
|
|
621
|
+
// Use the open file, so that a symbolic link that replaced the
|
|
622
|
+
// path is not followed. If that fails, use the path.
|
|
623
|
+
const times = this.times(entry);
|
|
624
|
+
if (times) {
|
|
625
|
+
const { atime, mtime } = times;
|
|
626
|
+
await handle.utimes(atime, mtime).catch(async (er) => {
|
|
627
|
+
await fs.utimes(absolute, atime, mtime).catch(() => {
|
|
628
|
+
throw er;
|
|
629
|
+
});
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
const owner = this.owner(entry);
|
|
633
|
+
if (owner) {
|
|
634
|
+
const { uid, gid } = owner;
|
|
635
|
+
await handle.chown(uid, gid).catch(async (er) => {
|
|
636
|
+
await fs.chown(absolute, uid, gid).catch(() => {
|
|
637
|
+
throw er;
|
|
638
|
+
});
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
catch (er) {
|
|
643
|
+
await handle.close().catch(() => { });
|
|
644
|
+
throw er;
|
|
645
|
+
}
|
|
646
|
+
await handle.close();
|
|
647
|
+
}
|
|
648
|
+
// Write the content of the entry as it arrives, through the
|
|
649
|
+
// transform stream if there is one.
|
|
650
|
+
async #pipe(entry, body, reader, handle) {
|
|
651
|
+
let i = 0;
|
|
652
|
+
const next = async () => {
|
|
653
|
+
if (i < body.length)
|
|
654
|
+
return body[i++];
|
|
655
|
+
try {
|
|
656
|
+
for (;;) {
|
|
657
|
+
const part = reader.readBody(entry);
|
|
658
|
+
if (part !== undefined)
|
|
659
|
+
return part;
|
|
660
|
+
await reader.fill();
|
|
580
661
|
}
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
662
|
+
}
|
|
663
|
+
catch (er) {
|
|
664
|
+
// The archive cannot be read.
|
|
665
|
+
throw this.fail(er);
|
|
666
|
+
}
|
|
584
667
|
};
|
|
585
|
-
const
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
668
|
+
const tx = this.opt.transform?.(entry);
|
|
669
|
+
if (!tx) {
|
|
670
|
+
// Write one part while the next part is read.
|
|
671
|
+
let writing;
|
|
672
|
+
let failed;
|
|
673
|
+
try {
|
|
674
|
+
for (let part; (part = await next());) {
|
|
675
|
+
await writing;
|
|
676
|
+
if (failed)
|
|
677
|
+
break;
|
|
678
|
+
writing = handle.write(part).then(() => { }, error => {
|
|
679
|
+
failed = { error };
|
|
596
680
|
});
|
|
597
681
|
}
|
|
598
682
|
}
|
|
599
|
-
|
|
683
|
+
finally {
|
|
684
|
+
await writing;
|
|
685
|
+
}
|
|
686
|
+
if (failed)
|
|
687
|
+
throw failed.error;
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
690
|
+
const writer = tx.writable.getWriter();
|
|
691
|
+
const out = tx.readable.getReader();
|
|
692
|
+
let failed;
|
|
693
|
+
// Stop the two streams. A read that waits gets the end of the
|
|
694
|
+
// stream, and a write that waits fails.
|
|
695
|
+
const stop = async (error) => {
|
|
696
|
+
failed ??= { error };
|
|
697
|
+
await out.cancel(error).catch(() => { });
|
|
698
|
+
await writer.abort(error).catch(() => { });
|
|
600
699
|
};
|
|
601
|
-
const
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
(
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
}
|
|
614
|
-
if (
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
700
|
+
const feed = (async () => {
|
|
701
|
+
try {
|
|
702
|
+
for (let part; (part = await next());)
|
|
703
|
+
await writer.write(part);
|
|
704
|
+
await writer.close();
|
|
705
|
+
}
|
|
706
|
+
catch (er) {
|
|
707
|
+
await stop(er);
|
|
708
|
+
}
|
|
709
|
+
})();
|
|
710
|
+
try {
|
|
711
|
+
for (;;) {
|
|
712
|
+
const { done, value } = await out.read();
|
|
713
|
+
if (done)
|
|
714
|
+
break;
|
|
715
|
+
await handle.write(value);
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
catch (er) {
|
|
719
|
+
await stop(er);
|
|
720
|
+
}
|
|
721
|
+
// The feed reads the archive, so it must end before the next
|
|
722
|
+
// entry is read.
|
|
723
|
+
await feed;
|
|
724
|
+
if (failed)
|
|
725
|
+
throw failed.error;
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
/**
|
|
729
|
+
* Extracts the entries of an archive with sync file system
|
|
730
|
+
* functions, one entry at a time.
|
|
731
|
+
*/
|
|
732
|
+
export class UnpackSync extends UnpackBase {
|
|
733
|
+
#fs;
|
|
734
|
+
#checkedCwd = false;
|
|
735
|
+
constructor(opt) {
|
|
736
|
+
super(opt);
|
|
737
|
+
this.#fs = this.opt.fs;
|
|
738
|
+
}
|
|
739
|
+
/** Extract the archive. Throws the first error that stops it. */
|
|
740
|
+
extract(input) {
|
|
741
|
+
const reader = new TarReaderSync(input, this.readerOptions());
|
|
742
|
+
const { onEntry } = this.opt;
|
|
743
|
+
try {
|
|
744
|
+
for (const entry of reader) {
|
|
745
|
+
onEntry?.(entry);
|
|
746
|
+
if (entry.bodyUsed)
|
|
747
|
+
continue;
|
|
748
|
+
if (!this.check(entry))
|
|
749
|
+
continue;
|
|
750
|
+
try {
|
|
751
|
+
this.#extract(entry, reader);
|
|
633
752
|
}
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
if (entry.absolute === this.cwd) {
|
|
637
|
-
return this[MAKEFS](null, entry, done);
|
|
753
|
+
catch (er) {
|
|
754
|
+
this.entryError(er, entry);
|
|
638
755
|
}
|
|
639
|
-
|
|
640
|
-
});
|
|
641
|
-
};
|
|
642
|
-
if (this[CHECKED_CWD]) {
|
|
643
|
-
start();
|
|
756
|
+
}
|
|
644
757
|
}
|
|
645
|
-
|
|
646
|
-
|
|
758
|
+
catch (error) {
|
|
759
|
+
if (error && typeof error === 'object' && 'tarCode' in error) {
|
|
760
|
+
Object.assign(error, { cwd: this.cwd });
|
|
761
|
+
}
|
|
762
|
+
throw error;
|
|
647
763
|
}
|
|
648
764
|
}
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
this[ONERROR](er, entry);
|
|
652
|
-
done();
|
|
653
|
-
return;
|
|
654
|
-
}
|
|
655
|
-
switch (entry.type) {
|
|
656
|
-
case 'File':
|
|
657
|
-
case 'OldFile':
|
|
658
|
-
case 'ContiguousFile':
|
|
659
|
-
return this[FILE](entry, done);
|
|
660
|
-
case 'Link':
|
|
661
|
-
return this[HARDLINK](entry, done);
|
|
662
|
-
case 'SymbolicLink':
|
|
663
|
-
return this[SYMLINK](entry, done);
|
|
664
|
-
case 'Directory':
|
|
665
|
-
case 'GNUDumpDir':
|
|
666
|
-
return this[DIRECTORY](entry, done);
|
|
667
|
-
}
|
|
668
|
-
}
|
|
669
|
-
[LINK](entry, linkpath, link, done) {
|
|
670
|
-
fs[link](linkpath, String(entry.absolute), er => {
|
|
671
|
-
if (er) {
|
|
672
|
-
this[ONERROR](er, entry);
|
|
673
|
-
}
|
|
674
|
-
else {
|
|
675
|
-
this[UNPEND]();
|
|
676
|
-
entry.resume();
|
|
677
|
-
}
|
|
678
|
-
done();
|
|
679
|
-
});
|
|
765
|
+
#mkdir(dir, mode) {
|
|
766
|
+
mkdirSync(this.#fs, dir, this.mkdirOptions(mode));
|
|
680
767
|
}
|
|
681
|
-
|
|
682
|
-
const
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
const mkParent = this[MKDIR](parent, this.dmode);
|
|
709
|
-
if (mkParent) {
|
|
710
|
-
return this[ONERROR](mkParent, entry);
|
|
768
|
+
#extract(entry, reader) {
|
|
769
|
+
const fs = this.#fs;
|
|
770
|
+
const absolute = entry.absolute;
|
|
771
|
+
const { cwd } = this;
|
|
772
|
+
if (!this.#checkedCwd) {
|
|
773
|
+
this.#mkdir(cwd, this.dmode);
|
|
774
|
+
this.#checkedCwd = true;
|
|
775
|
+
}
|
|
776
|
+
if (absolute !== cwd) {
|
|
777
|
+
const parent = this.dirname(absolute);
|
|
778
|
+
if (parent !== cwd)
|
|
779
|
+
this.#mkdir(parent, this.dmode);
|
|
780
|
+
}
|
|
781
|
+
// Remove what is at the path, if the entry cannot use it.
|
|
782
|
+
let st;
|
|
783
|
+
try {
|
|
784
|
+
st = fs.lstatSync(absolute);
|
|
785
|
+
}
|
|
786
|
+
catch { }
|
|
787
|
+
if (st && this.skip(entry, st))
|
|
788
|
+
return;
|
|
789
|
+
if (st && !this.isReusable(entry, st)) {
|
|
790
|
+
if (st.isDirectory() && dirTypes.has(entry.type)) {
|
|
791
|
+
if (this.chmod &&
|
|
792
|
+
entry.mode &&
|
|
793
|
+
(st.mode & 0o7777) !== entry.mode) {
|
|
794
|
+
fs.chmodSync(absolute, entry.mode);
|
|
711
795
|
}
|
|
712
796
|
}
|
|
797
|
+
else if (absolute !== cwd) {
|
|
798
|
+
if (st.isDirectory())
|
|
799
|
+
fs.rmdirSync(absolute);
|
|
800
|
+
else
|
|
801
|
+
this.#unlink(absolute);
|
|
802
|
+
}
|
|
713
803
|
}
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
if (
|
|
722
|
-
|
|
723
|
-
}
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
const
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
this[MAKEFS](er, entry);
|
|
744
|
-
}
|
|
745
|
-
[FILE](entry, done) {
|
|
746
|
-
const mode = typeof entry.mode === 'number' ? entry.mode & 0o7777 : this.fmode;
|
|
747
|
-
const oner = (er) => {
|
|
748
|
-
let closeError;
|
|
804
|
+
if (fileTypes.has(entry.type)) {
|
|
805
|
+
return this.#file(entry, absolute, reader);
|
|
806
|
+
}
|
|
807
|
+
if (dirTypes.has(entry.type))
|
|
808
|
+
return this.#directory(entry, absolute);
|
|
809
|
+
this.#ensureNoSymlink(this.linkParts(entry, absolute));
|
|
810
|
+
const linkpath = String(entry.linkpath);
|
|
811
|
+
if (entry.type === 'Link') {
|
|
812
|
+
fs.linkSync(this.resolve(cwd, linkpath), absolute);
|
|
813
|
+
}
|
|
814
|
+
else {
|
|
815
|
+
fs.symlinkSync(linkpath, absolute);
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
#unlink(path) {
|
|
819
|
+
const fs = this.#fs;
|
|
820
|
+
if (!this.win32)
|
|
821
|
+
return fs.unlinkSync(path);
|
|
822
|
+
const name = deleteName(path);
|
|
823
|
+
fs.renameSync(path, name);
|
|
824
|
+
fs.unlinkSync(name);
|
|
825
|
+
}
|
|
826
|
+
#ensureNoSymlink(parts) {
|
|
827
|
+
if (this.preservePaths)
|
|
828
|
+
return;
|
|
829
|
+
let t = this.cwd;
|
|
830
|
+
for (const p of parts) {
|
|
831
|
+
t = this.resolve(t, p);
|
|
832
|
+
let st;
|
|
749
833
|
try {
|
|
750
|
-
fs.
|
|
834
|
+
st = this.#fs.lstatSync(t);
|
|
751
835
|
}
|
|
752
|
-
catch
|
|
753
|
-
|
|
754
|
-
}
|
|
755
|
-
if (er || closeError) {
|
|
756
|
-
this[ONERROR](er || closeError, entry);
|
|
836
|
+
catch {
|
|
837
|
+
return;
|
|
757
838
|
}
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
let fd;
|
|
761
|
-
try {
|
|
762
|
-
fd = fs.openSync(String(entry.absolute), getWriteFlag(entry.size), mode);
|
|
763
|
-
/* c8 ignore start - This is only a problem if the file was successfully
|
|
764
|
-
* statted, BUT failed to open. Testing this is annoying, and we
|
|
765
|
-
* already have ample testint for other uses of oner() methods.
|
|
766
|
-
*/
|
|
767
|
-
}
|
|
768
|
-
catch (er) {
|
|
769
|
-
return oner(er);
|
|
839
|
+
if (st.isSymbolicLink())
|
|
840
|
+
throw this.symlinkError(t, parts);
|
|
770
841
|
}
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
842
|
+
}
|
|
843
|
+
#directory(entry, absolute) {
|
|
844
|
+
const fs = this.#fs;
|
|
845
|
+
this.#mkdir(absolute, entry.mode ?? this.dmode);
|
|
846
|
+
// These do not follow a symbolic link that replaced the
|
|
847
|
+
// directory. A failure is not an error of the entry.
|
|
848
|
+
const times = this.times(entry);
|
|
849
|
+
if (times) {
|
|
850
|
+
try {
|
|
851
|
+
fs.lutimesSync(absolute, times.atime, times.mtime);
|
|
852
|
+
}
|
|
853
|
+
catch { }
|
|
776
854
|
}
|
|
777
|
-
|
|
855
|
+
const owner = this.owner(entry);
|
|
856
|
+
if (owner) {
|
|
778
857
|
try {
|
|
779
|
-
fs.
|
|
858
|
+
fs.lchownSync(absolute, owner.uid, owner.gid);
|
|
780
859
|
}
|
|
781
|
-
catch
|
|
782
|
-
|
|
860
|
+
catch { }
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
#file(entry, absolute, reader) {
|
|
864
|
+
const fs = this.#fs;
|
|
865
|
+
const fd = fs.openSync(absolute, this.writeFlag(entry.size), entry.mode ?? this.fmode);
|
|
866
|
+
try {
|
|
867
|
+
const tx = this.opt.transform?.(entry);
|
|
868
|
+
for (;;) {
|
|
869
|
+
let part;
|
|
870
|
+
try {
|
|
871
|
+
part = reader.readBody(entry);
|
|
872
|
+
}
|
|
873
|
+
catch (er) {
|
|
874
|
+
// The archive cannot be read.
|
|
875
|
+
throw this.fail(er);
|
|
876
|
+
}
|
|
877
|
+
if (!part)
|
|
878
|
+
break;
|
|
879
|
+
fs.writeSync(fd, tx ? tx(part) : part);
|
|
783
880
|
}
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
if (entry.mtime && !this.noMtime) {
|
|
790
|
-
const atime = entry.atime || new Date();
|
|
791
|
-
const mtime = entry.mtime;
|
|
881
|
+
// Use the open file, so that a symbolic link that replaced the
|
|
882
|
+
// path is not followed. If that fails, use the path.
|
|
883
|
+
const times = this.times(entry);
|
|
884
|
+
if (times) {
|
|
885
|
+
const { atime, mtime } = times;
|
|
792
886
|
try {
|
|
793
887
|
fs.futimesSync(fd, atime, mtime);
|
|
794
888
|
}
|
|
795
|
-
catch (
|
|
889
|
+
catch (er) {
|
|
796
890
|
try {
|
|
797
|
-
fs.utimesSync(
|
|
891
|
+
fs.utimesSync(absolute, atime, mtime);
|
|
798
892
|
}
|
|
799
893
|
catch {
|
|
800
|
-
er
|
|
894
|
+
throw er;
|
|
801
895
|
}
|
|
802
896
|
}
|
|
803
897
|
}
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
const gid =
|
|
898
|
+
const owner = this.owner(entry);
|
|
899
|
+
if (owner) {
|
|
900
|
+
const { uid, gid } = owner;
|
|
807
901
|
try {
|
|
808
|
-
fs.fchownSync(fd,
|
|
902
|
+
fs.fchownSync(fd, uid, gid);
|
|
809
903
|
}
|
|
810
|
-
catch (
|
|
904
|
+
catch (er) {
|
|
811
905
|
try {
|
|
812
|
-
fs.chownSync(
|
|
906
|
+
fs.chownSync(absolute, uid, gid);
|
|
813
907
|
}
|
|
814
908
|
catch {
|
|
815
|
-
|
|
909
|
+
throw er;
|
|
816
910
|
}
|
|
817
911
|
}
|
|
818
912
|
}
|
|
819
|
-
oner(er);
|
|
820
|
-
});
|
|
821
|
-
}
|
|
822
|
-
[DIRECTORY](entry, done) {
|
|
823
|
-
const mode = typeof entry.mode === 'number' ? entry.mode & 0o7777 : this.dmode;
|
|
824
|
-
const er = this[MKDIR](String(entry.absolute), mode);
|
|
825
|
-
if (er) {
|
|
826
|
-
this[ONERROR](er, entry);
|
|
827
|
-
done();
|
|
828
|
-
return;
|
|
829
913
|
}
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
/* c8 ignore next */
|
|
834
|
-
}
|
|
835
|
-
catch { }
|
|
836
|
-
}
|
|
837
|
-
if (this[DOCHOWN](entry)) {
|
|
914
|
+
catch (er) {
|
|
915
|
+
// Close the file one time, and do not use the descriptor again:
|
|
916
|
+
// the system can give its number to a different file.
|
|
838
917
|
try {
|
|
839
|
-
fs.
|
|
918
|
+
fs.closeSync(fd);
|
|
840
919
|
}
|
|
841
920
|
catch { }
|
|
921
|
+
throw er;
|
|
842
922
|
}
|
|
843
|
-
|
|
844
|
-
entry.resume();
|
|
845
|
-
}
|
|
846
|
-
[MKDIR](dir, mode) {
|
|
847
|
-
try {
|
|
848
|
-
return mkdirSync(normalizeWindowsPath(dir), {
|
|
849
|
-
uid: this.uid,
|
|
850
|
-
gid: this.gid,
|
|
851
|
-
processUid: this.processUid,
|
|
852
|
-
processGid: this.processGid,
|
|
853
|
-
umask: this.processUmask,
|
|
854
|
-
preserve: this.preservePaths,
|
|
855
|
-
unlink: this.unlink,
|
|
856
|
-
cwd: this.cwd,
|
|
857
|
-
mode: mode,
|
|
858
|
-
});
|
|
859
|
-
}
|
|
860
|
-
catch (er) {
|
|
861
|
-
return er;
|
|
862
|
-
}
|
|
863
|
-
}
|
|
864
|
-
[ENSURE_NO_SYMLINK](_entry, cwd, parts, done, onError) {
|
|
865
|
-
if (this.preservePaths || parts.length === 0)
|
|
866
|
-
return done();
|
|
867
|
-
let t = cwd;
|
|
868
|
-
for (const p of parts) {
|
|
869
|
-
t = path.resolve(t, p);
|
|
870
|
-
const [er, st] = callSync(() => fs.lstatSync(t));
|
|
871
|
-
if (er)
|
|
872
|
-
return done();
|
|
873
|
-
if (st.isSymbolicLink()) {
|
|
874
|
-
return onError(new SymlinkError(t, path.resolve(cwd, parts.join('/'))));
|
|
875
|
-
}
|
|
876
|
-
}
|
|
877
|
-
done();
|
|
878
|
-
}
|
|
879
|
-
[LINK](entry, linkpath, link, done) {
|
|
880
|
-
const linkSync = `${link}Sync`;
|
|
881
|
-
try {
|
|
882
|
-
fs[linkSync](linkpath, String(entry.absolute));
|
|
883
|
-
done();
|
|
884
|
-
entry.resume();
|
|
885
|
-
}
|
|
886
|
-
catch (er) {
|
|
887
|
-
return this[ONERROR](er, entry);
|
|
888
|
-
}
|
|
923
|
+
fs.closeSync(fd);
|
|
889
924
|
}
|
|
890
925
|
}
|
|
891
926
|
//# sourceMappingURL=unpack.js.map
|