tar 6.2.1 → 7.0.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.
Files changed (253) hide show
  1. package/README.md +30 -16
  2. package/dist/commonjs/create.d.ts +8 -0
  3. package/dist/commonjs/create.d.ts.map +1 -0
  4. package/dist/commonjs/create.js +106 -0
  5. package/dist/commonjs/create.js.map +1 -0
  6. package/dist/commonjs/cwd-error.d.ts +8 -0
  7. package/dist/commonjs/cwd-error.d.ts.map +1 -0
  8. package/dist/commonjs/cwd-error.js +18 -0
  9. package/dist/commonjs/cwd-error.js.map +1 -0
  10. package/dist/commonjs/extract.d.ts +8 -0
  11. package/dist/commonjs/extract.d.ts.map +1 -0
  12. package/dist/commonjs/extract.js +141 -0
  13. package/dist/commonjs/extract.js.map +1 -0
  14. package/dist/commonjs/get-write-flag.d.ts +2 -0
  15. package/dist/commonjs/get-write-flag.d.ts.map +1 -0
  16. package/dist/commonjs/get-write-flag.js +29 -0
  17. package/dist/commonjs/get-write-flag.js.map +1 -0
  18. package/dist/commonjs/header.d.ts +54 -0
  19. package/dist/commonjs/header.d.ts.map +1 -0
  20. package/dist/commonjs/header.js +311 -0
  21. package/dist/commonjs/header.js.map +1 -0
  22. package/dist/commonjs/index.d.ts +19 -0
  23. package/dist/commonjs/index.d.ts.map +1 -0
  24. package/dist/commonjs/index.js +54 -0
  25. package/dist/commonjs/index.js.map +1 -0
  26. package/dist/commonjs/large-numbers.d.ts +4 -0
  27. package/dist/commonjs/large-numbers.d.ts.map +1 -0
  28. package/dist/commonjs/large-numbers.js +99 -0
  29. package/dist/commonjs/large-numbers.js.map +1 -0
  30. package/dist/commonjs/list.d.ts +8 -0
  31. package/dist/commonjs/list.d.ts.map +1 -0
  32. package/dist/commonjs/list.js +167 -0
  33. package/dist/commonjs/list.js.map +1 -0
  34. package/dist/commonjs/mkdir.d.ts +27 -0
  35. package/dist/commonjs/mkdir.d.ts.map +1 -0
  36. package/dist/commonjs/mkdir.js +209 -0
  37. package/dist/commonjs/mkdir.js.map +1 -0
  38. package/dist/commonjs/mode-fix.d.ts +2 -0
  39. package/dist/commonjs/mode-fix.d.ts.map +1 -0
  40. package/dist/commonjs/mode-fix.js +29 -0
  41. package/dist/commonjs/mode-fix.js.map +1 -0
  42. package/dist/commonjs/normalize-unicode.d.ts +2 -0
  43. package/dist/commonjs/normalize-unicode.d.ts.map +1 -0
  44. package/dist/commonjs/normalize-unicode.js +17 -0
  45. package/dist/commonjs/normalize-unicode.js.map +1 -0
  46. package/dist/commonjs/normalize-windows-path.d.ts +2 -0
  47. package/dist/commonjs/normalize-windows-path.d.ts.map +1 -0
  48. package/dist/commonjs/normalize-windows-path.js +12 -0
  49. package/dist/commonjs/normalize-windows-path.js.map +1 -0
  50. package/dist/commonjs/options.d.ts +545 -0
  51. package/dist/commonjs/options.d.ts.map +1 -0
  52. package/dist/commonjs/options.js +55 -0
  53. package/dist/commonjs/options.js.map +1 -0
  54. package/dist/commonjs/pack.d.ts +98 -0
  55. package/dist/commonjs/pack.d.ts.map +1 -0
  56. package/dist/commonjs/pack.js +461 -0
  57. package/dist/commonjs/pack.js.map +1 -0
  58. package/dist/commonjs/package.json +3 -0
  59. package/dist/commonjs/parse.d.ts +84 -0
  60. package/dist/commonjs/parse.d.ts.map +1 -0
  61. package/dist/commonjs/parse.js +571 -0
  62. package/dist/commonjs/parse.js.map +1 -0
  63. package/dist/commonjs/path-reservations.d.ts +11 -0
  64. package/dist/commonjs/path-reservations.d.ts.map +1 -0
  65. package/dist/commonjs/path-reservations.js +169 -0
  66. package/dist/commonjs/path-reservations.js.map +1 -0
  67. package/dist/commonjs/pax.d.ts +27 -0
  68. package/dist/commonjs/pax.d.ts.map +1 -0
  69. package/dist/commonjs/pax.js +158 -0
  70. package/dist/commonjs/pax.js.map +1 -0
  71. package/dist/commonjs/read-entry.d.ts +37 -0
  72. package/dist/commonjs/read-entry.d.ts.map +1 -0
  73. package/dist/commonjs/read-entry.js +139 -0
  74. package/dist/commonjs/read-entry.js.map +1 -0
  75. package/dist/commonjs/replace.d.ts +5 -0
  76. package/dist/commonjs/replace.d.ts.map +1 -0
  77. package/dist/commonjs/replace.js +223 -0
  78. package/dist/commonjs/replace.js.map +1 -0
  79. package/dist/commonjs/strip-absolute-path.d.ts +2 -0
  80. package/dist/commonjs/strip-absolute-path.d.ts.map +1 -0
  81. package/dist/commonjs/strip-absolute-path.js +29 -0
  82. package/dist/commonjs/strip-absolute-path.js.map +1 -0
  83. package/dist/commonjs/strip-trailing-slashes.d.ts +2 -0
  84. package/dist/commonjs/strip-trailing-slashes.d.ts.map +1 -0
  85. package/dist/commonjs/strip-trailing-slashes.js +18 -0
  86. package/dist/commonjs/strip-trailing-slashes.js.map +1 -0
  87. package/dist/commonjs/symlink-error.d.ts +9 -0
  88. package/dist/commonjs/symlink-error.d.ts.map +1 -0
  89. package/dist/commonjs/symlink-error.js +19 -0
  90. package/dist/commonjs/symlink-error.js.map +1 -0
  91. package/dist/commonjs/types.d.ts +7 -0
  92. package/dist/commonjs/types.d.ts.map +1 -0
  93. package/dist/commonjs/types.js +50 -0
  94. package/dist/commonjs/types.js.map +1 -0
  95. package/dist/commonjs/unpack.d.ts +98 -0
  96. package/dist/commonjs/unpack.d.ts.map +1 -0
  97. package/dist/commonjs/unpack.js +921 -0
  98. package/dist/commonjs/unpack.js.map +1 -0
  99. package/dist/commonjs/update.d.ts +3 -0
  100. package/dist/commonjs/update.d.ts.map +1 -0
  101. package/dist/commonjs/update.js +47 -0
  102. package/dist/commonjs/update.js.map +1 -0
  103. package/dist/commonjs/warn-method.d.ts +20 -0
  104. package/dist/commonjs/warn-method.d.ts.map +1 -0
  105. package/dist/commonjs/warn-method.js +31 -0
  106. package/dist/commonjs/warn-method.js.map +1 -0
  107. package/dist/commonjs/winchars.d.ts +3 -0
  108. package/dist/commonjs/winchars.d.ts.map +1 -0
  109. package/dist/commonjs/winchars.js +14 -0
  110. package/dist/commonjs/winchars.js.map +1 -0
  111. package/dist/commonjs/write-entry.d.ts +125 -0
  112. package/dist/commonjs/write-entry.d.ts.map +1 -0
  113. package/dist/commonjs/write-entry.js +657 -0
  114. package/dist/commonjs/write-entry.js.map +1 -0
  115. package/dist/esm/create.d.ts +8 -0
  116. package/dist/esm/create.d.ts.map +1 -0
  117. package/dist/esm/create.js +99 -0
  118. package/dist/esm/create.js.map +1 -0
  119. package/dist/esm/cwd-error.d.ts +8 -0
  120. package/dist/esm/cwd-error.d.ts.map +1 -0
  121. package/dist/esm/cwd-error.js +14 -0
  122. package/dist/esm/cwd-error.js.map +1 -0
  123. package/dist/esm/extract.d.ts +8 -0
  124. package/dist/esm/extract.d.ts.map +1 -0
  125. package/dist/esm/extract.js +111 -0
  126. package/dist/esm/extract.js.map +1 -0
  127. package/dist/esm/get-write-flag.d.ts +2 -0
  128. package/dist/esm/get-write-flag.d.ts.map +1 -0
  129. package/dist/esm/get-write-flag.js +23 -0
  130. package/dist/esm/get-write-flag.js.map +1 -0
  131. package/dist/esm/header.d.ts +54 -0
  132. package/dist/esm/header.d.ts.map +1 -0
  133. package/dist/esm/header.js +284 -0
  134. package/dist/esm/header.js.map +1 -0
  135. package/dist/esm/index.d.ts +19 -0
  136. package/dist/esm/index.d.ts.map +1 -0
  137. package/dist/esm/index.js +20 -0
  138. package/dist/esm/index.js.map +1 -0
  139. package/dist/esm/large-numbers.d.ts +4 -0
  140. package/dist/esm/large-numbers.d.ts.map +1 -0
  141. package/dist/esm/large-numbers.js +94 -0
  142. package/dist/esm/large-numbers.js.map +1 -0
  143. package/dist/esm/list.d.ts +8 -0
  144. package/dist/esm/list.d.ts.map +1 -0
  145. package/dist/esm/list.js +137 -0
  146. package/dist/esm/list.js.map +1 -0
  147. package/dist/esm/mkdir.d.ts +27 -0
  148. package/dist/esm/mkdir.d.ts.map +1 -0
  149. package/dist/esm/mkdir.js +201 -0
  150. package/dist/esm/mkdir.js.map +1 -0
  151. package/dist/esm/mode-fix.d.ts +2 -0
  152. package/dist/esm/mode-fix.d.ts.map +1 -0
  153. package/dist/esm/mode-fix.js +25 -0
  154. package/dist/esm/mode-fix.js.map +1 -0
  155. package/dist/esm/normalize-unicode.d.ts +2 -0
  156. package/dist/esm/normalize-unicode.d.ts.map +1 -0
  157. package/dist/esm/normalize-unicode.js +13 -0
  158. package/dist/esm/normalize-unicode.js.map +1 -0
  159. package/dist/esm/normalize-windows-path.d.ts +2 -0
  160. package/dist/esm/normalize-windows-path.d.ts.map +1 -0
  161. package/{lib → dist/esm}/normalize-windows-path.js +5 -4
  162. package/dist/esm/normalize-windows-path.js.map +1 -0
  163. package/dist/esm/options.d.ts +545 -0
  164. package/dist/esm/options.d.ts.map +1 -0
  165. package/dist/esm/options.js +48 -0
  166. package/dist/esm/options.js.map +1 -0
  167. package/dist/esm/pack.d.ts +98 -0
  168. package/dist/esm/pack.d.ts.map +1 -0
  169. package/dist/esm/pack.js +429 -0
  170. package/dist/esm/pack.js.map +1 -0
  171. package/dist/esm/package.json +3 -0
  172. package/dist/esm/parse.d.ts +84 -0
  173. package/dist/esm/parse.d.ts.map +1 -0
  174. package/dist/esm/parse.js +567 -0
  175. package/dist/esm/parse.js.map +1 -0
  176. package/dist/esm/path-reservations.d.ts +11 -0
  177. package/dist/esm/path-reservations.d.ts.map +1 -0
  178. package/dist/esm/path-reservations.js +165 -0
  179. package/dist/esm/path-reservations.js.map +1 -0
  180. package/dist/esm/pax.d.ts +27 -0
  181. package/dist/esm/pax.d.ts.map +1 -0
  182. package/dist/esm/pax.js +154 -0
  183. package/dist/esm/pax.js.map +1 -0
  184. package/dist/esm/read-entry.d.ts +37 -0
  185. package/dist/esm/read-entry.d.ts.map +1 -0
  186. package/dist/esm/read-entry.js +135 -0
  187. package/dist/esm/read-entry.js.map +1 -0
  188. package/dist/esm/replace.d.ts +5 -0
  189. package/dist/esm/replace.d.ts.map +1 -0
  190. package/dist/esm/replace.js +216 -0
  191. package/dist/esm/replace.js.map +1 -0
  192. package/dist/esm/strip-absolute-path.d.ts +2 -0
  193. package/dist/esm/strip-absolute-path.d.ts.map +1 -0
  194. package/dist/esm/strip-absolute-path.js +25 -0
  195. package/dist/esm/strip-absolute-path.js.map +1 -0
  196. package/dist/esm/strip-trailing-slashes.d.ts +2 -0
  197. package/dist/esm/strip-trailing-slashes.d.ts.map +1 -0
  198. package/dist/esm/strip-trailing-slashes.js +14 -0
  199. package/dist/esm/strip-trailing-slashes.js.map +1 -0
  200. package/dist/esm/symlink-error.d.ts +9 -0
  201. package/dist/esm/symlink-error.d.ts.map +1 -0
  202. package/dist/esm/symlink-error.js +15 -0
  203. package/dist/esm/symlink-error.js.map +1 -0
  204. package/dist/esm/types.d.ts +7 -0
  205. package/dist/esm/types.d.ts.map +1 -0
  206. package/dist/esm/types.js +45 -0
  207. package/dist/esm/types.js.map +1 -0
  208. package/dist/esm/unpack.d.ts +98 -0
  209. package/dist/esm/unpack.d.ts.map +1 -0
  210. package/dist/esm/unpack.js +890 -0
  211. package/dist/esm/unpack.js.map +1 -0
  212. package/dist/esm/update.d.ts +3 -0
  213. package/dist/esm/update.d.ts.map +1 -0
  214. package/dist/esm/update.js +43 -0
  215. package/dist/esm/update.js.map +1 -0
  216. package/dist/esm/warn-method.d.ts +20 -0
  217. package/dist/esm/warn-method.d.ts.map +1 -0
  218. package/dist/esm/warn-method.js +27 -0
  219. package/dist/esm/warn-method.js.map +1 -0
  220. package/dist/esm/winchars.d.ts +3 -0
  221. package/dist/esm/winchars.d.ts.map +1 -0
  222. package/dist/esm/winchars.js +9 -0
  223. package/dist/esm/winchars.js.map +1 -0
  224. package/dist/esm/write-entry.d.ts +125 -0
  225. package/dist/esm/write-entry.d.ts.map +1 -0
  226. package/dist/esm/write-entry.js +625 -0
  227. package/dist/esm/write-entry.js.map +1 -0
  228. package/package.json +255 -40
  229. package/index.js +0 -18
  230. package/lib/create.js +0 -111
  231. package/lib/extract.js +0 -113
  232. package/lib/get-write-flag.js +0 -20
  233. package/lib/header.js +0 -304
  234. package/lib/high-level-opt.js +0 -29
  235. package/lib/large-numbers.js +0 -104
  236. package/lib/list.js +0 -139
  237. package/lib/mkdir.js +0 -229
  238. package/lib/mode-fix.js +0 -27
  239. package/lib/normalize-unicode.js +0 -12
  240. package/lib/pack.js +0 -432
  241. package/lib/parse.js +0 -552
  242. package/lib/path-reservations.js +0 -156
  243. package/lib/pax.js +0 -150
  244. package/lib/read-entry.js +0 -107
  245. package/lib/replace.js +0 -246
  246. package/lib/strip-absolute-path.js +0 -24
  247. package/lib/strip-trailing-slashes.js +0 -13
  248. package/lib/types.js +0 -44
  249. package/lib/unpack.js +0 -923
  250. package/lib/update.js +0 -40
  251. package/lib/warn-mixin.js +0 -24
  252. package/lib/winchars.js +0 -23
  253. package/lib/write-entry.js +0 -546
@@ -0,0 +1,921 @@
1
+ "use strict";
2
+ // the PEND/UNPEND stuff tracks whether we're ready to emit end/close yet.
3
+ // but the path reservations are required to avoid race conditions where
4
+ // parallelized unpack ops may mess with one another, due to dependencies
5
+ // (like a Link depending on its target) or destructive operations (like
6
+ // clobbering an fs object to create one of a different type.)
7
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
8
+ if (k2 === undefined) k2 = k;
9
+ var desc = Object.getOwnPropertyDescriptor(m, k);
10
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
11
+ desc = { enumerable: true, get: function() { return m[k]; } };
12
+ }
13
+ Object.defineProperty(o, k2, desc);
14
+ }) : (function(o, m, k, k2) {
15
+ if (k2 === undefined) k2 = k;
16
+ o[k2] = m[k];
17
+ }));
18
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
19
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
20
+ }) : function(o, v) {
21
+ o["default"] = v;
22
+ });
23
+ var __importStar = (this && this.__importStar) || function (mod) {
24
+ if (mod && mod.__esModule) return mod;
25
+ var result = {};
26
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
27
+ __setModuleDefault(result, mod);
28
+ return result;
29
+ };
30
+ var __importDefault = (this && this.__importDefault) || function (mod) {
31
+ return (mod && mod.__esModule) ? mod : { "default": mod };
32
+ };
33
+ Object.defineProperty(exports, "__esModule", { value: true });
34
+ exports.UnpackSync = exports.Unpack = void 0;
35
+ const fsm = __importStar(require("@isaacs/fs-minipass"));
36
+ const node_assert_1 = __importDefault(require("node:assert"));
37
+ const node_crypto_1 = require("node:crypto");
38
+ const node_fs_1 = __importDefault(require("node:fs"));
39
+ const node_path_1 = __importDefault(require("node:path"));
40
+ const get_write_flag_js_1 = require("./get-write-flag.js");
41
+ const mkdir_js_1 = require("./mkdir.js");
42
+ const normalize_unicode_js_1 = require("./normalize-unicode.js");
43
+ const normalize_windows_path_js_1 = require("./normalize-windows-path.js");
44
+ const parse_js_1 = require("./parse.js");
45
+ const strip_absolute_path_js_1 = require("./strip-absolute-path.js");
46
+ const strip_trailing_slashes_js_1 = require("./strip-trailing-slashes.js");
47
+ const wc = __importStar(require("./winchars.js"));
48
+ const path_reservations_js_1 = require("./path-reservations.js");
49
+ const ONENTRY = Symbol('onEntry');
50
+ const CHECKFS = Symbol('checkFs');
51
+ const CHECKFS2 = Symbol('checkFs2');
52
+ const PRUNECACHE = Symbol('pruneCache');
53
+ const ISREUSABLE = Symbol('isReusable');
54
+ const MAKEFS = Symbol('makeFs');
55
+ const FILE = Symbol('file');
56
+ const DIRECTORY = Symbol('directory');
57
+ const LINK = Symbol('link');
58
+ const SYMLINK = Symbol('symlink');
59
+ const HARDLINK = Symbol('hardlink');
60
+ const UNSUPPORTED = Symbol('unsupported');
61
+ const CHECKPATH = Symbol('checkPath');
62
+ const MKDIR = Symbol('mkdir');
63
+ const ONERROR = Symbol('onError');
64
+ const PENDING = Symbol('pending');
65
+ const PEND = Symbol('pend');
66
+ const UNPEND = Symbol('unpend');
67
+ const ENDED = Symbol('ended');
68
+ const MAYBECLOSE = Symbol('maybeClose');
69
+ const SKIP = Symbol('skip');
70
+ const DOCHOWN = Symbol('doChown');
71
+ const UID = Symbol('uid');
72
+ const GID = Symbol('gid');
73
+ const CHECKED_CWD = Symbol('checkedCwd');
74
+ const platform = process.env.TESTING_TAR_FAKE_PLATFORM || process.platform;
75
+ const isWindows = platform === 'win32';
76
+ const DEFAULT_MAX_DEPTH = 1024;
77
+ // Unlinks on Windows are not atomic.
78
+ //
79
+ // This means that if you have a file entry, followed by another
80
+ // file entry with an identical name, and you cannot re-use the file
81
+ // (because it's a hardlink, or because unlink:true is set, or it's
82
+ // Windows, which does not have useful nlink values), then the unlink
83
+ // will be committed to the disk AFTER the new file has been written
84
+ // over the old one, deleting the new file.
85
+ //
86
+ // To work around this, on Windows systems, we rename the file and then
87
+ // delete the renamed file. It's a sloppy kludge, but frankly, I do not
88
+ // know of a better way to do this, given windows' non-atomic unlink
89
+ // semantics.
90
+ //
91
+ // See: https://github.com/npm/node-tar/issues/183
92
+ /* c8 ignore start */
93
+ const unlinkFile = (path, cb) => {
94
+ if (!isWindows) {
95
+ return node_fs_1.default.unlink(path, cb);
96
+ }
97
+ const name = path + '.DELETE.' + (0, node_crypto_1.randomBytes)(16).toString('hex');
98
+ node_fs_1.default.rename(path, name, er => {
99
+ if (er) {
100
+ return cb(er);
101
+ }
102
+ node_fs_1.default.unlink(name, cb);
103
+ });
104
+ };
105
+ /* c8 ignore stop */
106
+ /* c8 ignore start */
107
+ const unlinkFileSync = (path) => {
108
+ if (!isWindows) {
109
+ return node_fs_1.default.unlinkSync(path);
110
+ }
111
+ const name = path + '.DELETE.' + (0, node_crypto_1.randomBytes)(16).toString('hex');
112
+ node_fs_1.default.renameSync(path, name);
113
+ node_fs_1.default.unlinkSync(name);
114
+ };
115
+ /* c8 ignore stop */
116
+ // this.gid, entry.gid, this.processUid
117
+ const uint32 = (a, b, c) => a !== undefined && a === a >>> 0
118
+ ? a
119
+ : b !== undefined && b === b >>> 0
120
+ ? b
121
+ : c;
122
+ // clear the cache if it's a case-insensitive unicode-squashing match.
123
+ // we can't know if the current file system is case-sensitive or supports
124
+ // unicode fully, so we check for similarity on the maximally compatible
125
+ // representation. Err on the side of pruning, since all it's doing is
126
+ // preventing lstats, and it's not the end of the world if we get a false
127
+ // positive.
128
+ // Note that on windows, we always drop the entire cache whenever a
129
+ // symbolic link is encountered, because 8.3 filenames are impossible
130
+ // to reason about, and collisions are hazards rather than just failures.
131
+ const cacheKeyNormalize = (path) => (0, strip_trailing_slashes_js_1.stripTrailingSlashes)((0, normalize_windows_path_js_1.normalizeWindowsPath)((0, normalize_unicode_js_1.normalizeUnicode)(path))).toLowerCase();
132
+ // remove all cache entries matching ${abs}/**
133
+ const pruneCache = (cache, abs) => {
134
+ abs = cacheKeyNormalize(abs);
135
+ for (const path of cache.keys()) {
136
+ const pnorm = cacheKeyNormalize(path);
137
+ if (pnorm === abs || pnorm.indexOf(abs + '/') === 0) {
138
+ cache.delete(path);
139
+ }
140
+ }
141
+ };
142
+ const dropCache = (cache) => {
143
+ for (const key of cache.keys()) {
144
+ cache.delete(key);
145
+ }
146
+ };
147
+ class Unpack extends parse_js_1.Parser {
148
+ [ENDED] = false;
149
+ [CHECKED_CWD] = false;
150
+ [PENDING] = 0;
151
+ reservations = new path_reservations_js_1.PathReservations();
152
+ transform;
153
+ writable = true;
154
+ readable = false;
155
+ dirCache;
156
+ uid;
157
+ gid;
158
+ setOwner;
159
+ preserveOwner;
160
+ processGid;
161
+ processUid;
162
+ maxDepth;
163
+ forceChown;
164
+ win32;
165
+ newer;
166
+ keep;
167
+ noMtime;
168
+ preservePaths;
169
+ unlink;
170
+ cwd;
171
+ strip;
172
+ processUmask;
173
+ umask;
174
+ dmode;
175
+ fmode;
176
+ chmod;
177
+ constructor(opt = {}) {
178
+ opt.ondone = () => {
179
+ this[ENDED] = true;
180
+ this[MAYBECLOSE]();
181
+ };
182
+ super(opt);
183
+ this.transform = opt.transform;
184
+ this.dirCache = opt.dirCache || new Map();
185
+ this.chmod = !!opt.chmod;
186
+ if (typeof opt.uid === 'number' || typeof opt.gid === 'number') {
187
+ // need both or neither
188
+ if (typeof opt.uid !== 'number' ||
189
+ typeof opt.gid !== 'number') {
190
+ throw new TypeError('cannot set owner without number uid and gid');
191
+ }
192
+ if (opt.preserveOwner) {
193
+ throw new TypeError('cannot preserve owner in archive and also set owner explicitly');
194
+ }
195
+ this.uid = opt.uid;
196
+ this.gid = opt.gid;
197
+ this.setOwner = true;
198
+ }
199
+ else {
200
+ this.uid = undefined;
201
+ this.gid = undefined;
202
+ this.setOwner = false;
203
+ }
204
+ // default true for root
205
+ if (opt.preserveOwner === undefined &&
206
+ typeof opt.uid !== 'number') {
207
+ this.preserveOwner = !!(process.getuid && process.getuid() === 0);
208
+ }
209
+ else {
210
+ this.preserveOwner = !!opt.preserveOwner;
211
+ }
212
+ this.processUid =
213
+ (this.preserveOwner || this.setOwner) && process.getuid
214
+ ? process.getuid()
215
+ : undefined;
216
+ this.processGid =
217
+ (this.preserveOwner || this.setOwner) && process.getgid
218
+ ? process.getgid()
219
+ : undefined;
220
+ // prevent excessively deep nesting of subfolders
221
+ // set to `Infinity` to remove this restriction
222
+ this.maxDepth =
223
+ typeof opt.maxDepth === 'number'
224
+ ? opt.maxDepth
225
+ : DEFAULT_MAX_DEPTH;
226
+ // mostly just for testing, but useful in some cases.
227
+ // Forcibly trigger a chown on every entry, no matter what
228
+ this.forceChown = opt.forceChown === true;
229
+ // turn ><?| in filenames into 0xf000-higher encoded forms
230
+ this.win32 = !!opt.win32 || isWindows;
231
+ // do not unpack over files that are newer than what's in the archive
232
+ this.newer = !!opt.newer;
233
+ // do not unpack over ANY files
234
+ this.keep = !!opt.keep;
235
+ // do not set mtime/atime of extracted entries
236
+ this.noMtime = !!opt.noMtime;
237
+ // allow .., absolute path entries, and unpacking through symlinks
238
+ // without this, warn and skip .., relativize absolutes, and error
239
+ // on symlinks in extraction path
240
+ this.preservePaths = !!opt.preservePaths;
241
+ // unlink files and links before writing. This breaks existing hard
242
+ // links, and removes symlink directories rather than erroring
243
+ this.unlink = !!opt.unlink;
244
+ this.cwd = (0, normalize_windows_path_js_1.normalizeWindowsPath)(node_path_1.default.resolve(opt.cwd || process.cwd()));
245
+ this.strip = Number(opt.strip) || 0;
246
+ // if we're not chmodding, then we don't need the process umask
247
+ this.processUmask = !this.chmod
248
+ ? 0
249
+ : typeof opt.processUmask === 'number'
250
+ ? opt.processUmask
251
+ : process.umask();
252
+ this.umask =
253
+ typeof opt.umask === 'number' ? opt.umask : this.processUmask;
254
+ // default mode for dirs created as parents
255
+ this.dmode = opt.dmode || 0o0777 & ~this.umask;
256
+ this.fmode = opt.fmode || 0o0666 & ~this.umask;
257
+ this.on('entry', entry => this[ONENTRY](entry));
258
+ }
259
+ // a bad or damaged archive is a warning for Parser, but an error
260
+ // when extracting. Mark those errors as unrecoverable, because
261
+ // the Unpack contract cannot be met.
262
+ warn(code, msg, data = {}) {
263
+ if (code === 'TAR_BAD_ARCHIVE' || code === 'TAR_ABORT') {
264
+ data.recoverable = false;
265
+ }
266
+ return super.warn(code, msg, data);
267
+ }
268
+ [MAYBECLOSE]() {
269
+ if (this[ENDED] && this[PENDING] === 0) {
270
+ this.emit('prefinish');
271
+ this.emit('finish');
272
+ this.emit('end');
273
+ }
274
+ }
275
+ [CHECKPATH](entry) {
276
+ const p = (0, normalize_windows_path_js_1.normalizeWindowsPath)(entry.path);
277
+ const parts = p.split('/');
278
+ if (this.strip) {
279
+ if (parts.length < this.strip) {
280
+ return false;
281
+ }
282
+ if (entry.type === 'Link') {
283
+ const linkparts = (0, normalize_windows_path_js_1.normalizeWindowsPath)(String(entry.linkpath)).split('/');
284
+ if (linkparts.length >= this.strip) {
285
+ entry.linkpath = linkparts.slice(this.strip).join('/');
286
+ }
287
+ else {
288
+ return false;
289
+ }
290
+ }
291
+ parts.splice(0, this.strip);
292
+ entry.path = parts.join('/');
293
+ }
294
+ if (isFinite(this.maxDepth) && parts.length > this.maxDepth) {
295
+ this.warn('TAR_ENTRY_ERROR', 'path excessively deep', {
296
+ entry,
297
+ path: p,
298
+ depth: parts.length,
299
+ maxDepth: this.maxDepth,
300
+ });
301
+ return false;
302
+ }
303
+ if (!this.preservePaths) {
304
+ if (parts.includes('..') ||
305
+ /* c8 ignore next */
306
+ (isWindows && /^[a-z]:\.\.$/i.test(parts[0] ?? ''))) {
307
+ this.warn('TAR_ENTRY_ERROR', `path contains '..'`, {
308
+ entry,
309
+ path: p,
310
+ });
311
+ return false;
312
+ }
313
+ // strip off the root
314
+ const [root, stripped] = (0, strip_absolute_path_js_1.stripAbsolutePath)(p);
315
+ if (root) {
316
+ entry.path = String(stripped);
317
+ this.warn('TAR_ENTRY_INFO', `stripping ${root} from absolute path`, {
318
+ entry,
319
+ path: p,
320
+ });
321
+ }
322
+ }
323
+ if (node_path_1.default.isAbsolute(entry.path)) {
324
+ entry.absolute = (0, normalize_windows_path_js_1.normalizeWindowsPath)(node_path_1.default.resolve(entry.path));
325
+ }
326
+ else {
327
+ entry.absolute = (0, normalize_windows_path_js_1.normalizeWindowsPath)(node_path_1.default.resolve(this.cwd, entry.path));
328
+ }
329
+ // if we somehow ended up with a path that escapes the cwd, and we are
330
+ // not in preservePaths mode, then something is fishy! This should have
331
+ // been prevented above, so ignore this for coverage.
332
+ /* c8 ignore start - defense in depth */
333
+ if (!this.preservePaths &&
334
+ typeof entry.absolute === 'string' &&
335
+ entry.absolute.indexOf(this.cwd + '/') !== 0 &&
336
+ entry.absolute !== this.cwd) {
337
+ this.warn('TAR_ENTRY_ERROR', 'path escaped extraction target', {
338
+ entry,
339
+ path: (0, normalize_windows_path_js_1.normalizeWindowsPath)(entry.path),
340
+ resolvedPath: entry.absolute,
341
+ cwd: this.cwd,
342
+ });
343
+ return false;
344
+ }
345
+ /* c8 ignore stop */
346
+ // an archive can set properties on the extraction directory, but it
347
+ // may not replace the cwd with a different kind of thing entirely.
348
+ if (entry.absolute === this.cwd &&
349
+ entry.type !== 'Directory' &&
350
+ entry.type !== 'GNUDumpDir') {
351
+ return false;
352
+ }
353
+ // only encode : chars that aren't drive letter indicators
354
+ if (this.win32) {
355
+ const { root: aRoot } = node_path_1.default.win32.parse(String(entry.absolute));
356
+ entry.absolute =
357
+ aRoot + wc.encode(String(entry.absolute).slice(aRoot.length));
358
+ const { root: pRoot } = node_path_1.default.win32.parse(entry.path);
359
+ entry.path = pRoot + wc.encode(entry.path.slice(pRoot.length));
360
+ }
361
+ return true;
362
+ }
363
+ [ONENTRY](entry) {
364
+ if (!this[CHECKPATH](entry)) {
365
+ return entry.resume();
366
+ }
367
+ node_assert_1.default.equal(typeof entry.absolute, 'string');
368
+ switch (entry.type) {
369
+ case 'Directory':
370
+ case 'GNUDumpDir':
371
+ if (entry.mode) {
372
+ entry.mode = entry.mode | 0o700;
373
+ }
374
+ // eslint-disable-next-line no-fallthrough
375
+ case 'File':
376
+ case 'OldFile':
377
+ case 'ContiguousFile':
378
+ case 'Link':
379
+ case 'SymbolicLink':
380
+ return this[CHECKFS](entry);
381
+ case 'CharacterDevice':
382
+ case 'BlockDevice':
383
+ case 'FIFO':
384
+ default:
385
+ return this[UNSUPPORTED](entry);
386
+ }
387
+ }
388
+ [ONERROR](er, entry) {
389
+ // Cwd has to exist, or else nothing works. That's serious.
390
+ // Other errors are warnings, which raise the error in strict
391
+ // mode, but otherwise continue on.
392
+ if (er.name === 'CwdError') {
393
+ this.emit('error', er);
394
+ }
395
+ else {
396
+ this.warn('TAR_ENTRY_ERROR', er, { entry });
397
+ this[UNPEND]();
398
+ entry.resume();
399
+ }
400
+ }
401
+ [MKDIR](dir, mode, cb) {
402
+ (0, mkdir_js_1.mkdir)((0, normalize_windows_path_js_1.normalizeWindowsPath)(dir), {
403
+ uid: this.uid,
404
+ gid: this.gid,
405
+ processUid: this.processUid,
406
+ processGid: this.processGid,
407
+ umask: this.processUmask,
408
+ preserve: this.preservePaths,
409
+ unlink: this.unlink,
410
+ cache: this.dirCache,
411
+ cwd: this.cwd,
412
+ mode: mode,
413
+ }, cb);
414
+ }
415
+ [DOCHOWN](entry) {
416
+ // in preserve owner mode, chown if the entry doesn't match process
417
+ // in set owner mode, chown if setting doesn't match process
418
+ return (this.forceChown ||
419
+ (this.preserveOwner &&
420
+ ((typeof entry.uid === 'number' &&
421
+ entry.uid !== this.processUid) ||
422
+ (typeof entry.gid === 'number' &&
423
+ entry.gid !== this.processGid))) ||
424
+ (typeof this.uid === 'number' &&
425
+ this.uid !== this.processUid) ||
426
+ (typeof this.gid === 'number' && this.gid !== this.processGid));
427
+ }
428
+ [UID](entry) {
429
+ return uint32(this.uid, entry.uid, this.processUid);
430
+ }
431
+ [GID](entry) {
432
+ return uint32(this.gid, entry.gid, this.processGid);
433
+ }
434
+ [FILE](entry, fullyDone) {
435
+ const mode = typeof entry.mode === 'number'
436
+ ? entry.mode & 0o7777
437
+ : this.fmode;
438
+ const stream = new fsm.WriteStream(String(entry.absolute), {
439
+ // slight lie, but it can be numeric flags
440
+ flags: (0, get_write_flag_js_1.getWriteFlag)(entry.size),
441
+ mode: mode,
442
+ autoClose: false,
443
+ });
444
+ stream.on('error', (er) => {
445
+ if (stream.fd) {
446
+ node_fs_1.default.close(stream.fd, () => { });
447
+ }
448
+ // flush all the data out so that we aren't left hanging
449
+ // if the error wasn't actually fatal. otherwise the parse
450
+ // is blocked, and we never proceed.
451
+ stream.write = () => true;
452
+ this[ONERROR](er, entry);
453
+ fullyDone();
454
+ });
455
+ let actions = 1;
456
+ const done = (er) => {
457
+ if (er) {
458
+ /* c8 ignore start - we should always have a fd by now */
459
+ if (stream.fd) {
460
+ node_fs_1.default.close(stream.fd, () => { });
461
+ }
462
+ /* c8 ignore stop */
463
+ this[ONERROR](er, entry);
464
+ fullyDone();
465
+ return;
466
+ }
467
+ if (--actions === 0) {
468
+ if (stream.fd !== undefined) {
469
+ node_fs_1.default.close(stream.fd, er => {
470
+ if (er) {
471
+ this[ONERROR](er, entry);
472
+ }
473
+ else {
474
+ this[UNPEND]();
475
+ }
476
+ fullyDone();
477
+ });
478
+ }
479
+ }
480
+ };
481
+ stream.on('finish', () => {
482
+ // if futimes fails, try utimes
483
+ // if utimes fails, fail with the original error
484
+ // same for fchown/chown
485
+ const abs = String(entry.absolute);
486
+ const fd = stream.fd;
487
+ if (typeof fd === 'number' && entry.mtime && !this.noMtime) {
488
+ actions++;
489
+ const atime = entry.atime || new Date();
490
+ const mtime = entry.mtime;
491
+ node_fs_1.default.futimes(fd, atime, mtime, er => er
492
+ ? node_fs_1.default.utimes(abs, atime, mtime, er2 => done(er2 && er))
493
+ : done());
494
+ }
495
+ if (typeof fd === 'number' && this[DOCHOWN](entry)) {
496
+ actions++;
497
+ const uid = this[UID](entry);
498
+ const gid = this[GID](entry);
499
+ if (typeof uid === 'number' && typeof gid === 'number') {
500
+ node_fs_1.default.fchown(fd, uid, gid, er => er
501
+ ? node_fs_1.default.chown(abs, uid, gid, er2 => done(er2 && er))
502
+ : done());
503
+ }
504
+ }
505
+ done();
506
+ });
507
+ const tx = this.transform ? this.transform(entry) || entry : entry;
508
+ if (tx !== entry) {
509
+ tx.on('error', (er) => {
510
+ this[ONERROR](er, entry);
511
+ fullyDone();
512
+ });
513
+ entry.pipe(tx);
514
+ }
515
+ tx.pipe(stream);
516
+ }
517
+ [DIRECTORY](entry, fullyDone) {
518
+ const mode = typeof entry.mode === 'number'
519
+ ? entry.mode & 0o7777
520
+ : this.dmode;
521
+ this[MKDIR](String(entry.absolute), mode, er => {
522
+ if (er) {
523
+ this[ONERROR](er, entry);
524
+ fullyDone();
525
+ return;
526
+ }
527
+ let actions = 1;
528
+ const done = () => {
529
+ if (--actions === 0) {
530
+ fullyDone();
531
+ this[UNPEND]();
532
+ entry.resume();
533
+ }
534
+ };
535
+ if (entry.mtime && !this.noMtime) {
536
+ actions++;
537
+ node_fs_1.default.utimes(String(entry.absolute), entry.atime || new Date(), entry.mtime, done);
538
+ }
539
+ if (this[DOCHOWN](entry)) {
540
+ actions++;
541
+ node_fs_1.default.chown(String(entry.absolute), Number(this[UID](entry)), Number(this[GID](entry)), done);
542
+ }
543
+ done();
544
+ });
545
+ }
546
+ [UNSUPPORTED](entry) {
547
+ entry.unsupported = true;
548
+ this.warn('TAR_ENTRY_UNSUPPORTED', `unsupported entry type: ${entry.type}`, { entry });
549
+ entry.resume();
550
+ }
551
+ [SYMLINK](entry, done) {
552
+ this[LINK](entry, String(entry.linkpath), 'symlink', done);
553
+ }
554
+ [HARDLINK](entry, done) {
555
+ const linkpath = (0, normalize_windows_path_js_1.normalizeWindowsPath)(node_path_1.default.resolve(this.cwd, String(entry.linkpath)));
556
+ this[LINK](entry, linkpath, 'link', done);
557
+ }
558
+ [PEND]() {
559
+ this[PENDING]++;
560
+ }
561
+ [UNPEND]() {
562
+ this[PENDING]--;
563
+ this[MAYBECLOSE]();
564
+ }
565
+ [SKIP](entry) {
566
+ this[UNPEND]();
567
+ entry.resume();
568
+ }
569
+ // Check if we can reuse an existing filesystem entry safely and
570
+ // overwrite it, rather than unlinking and recreating
571
+ // Windows doesn't report a useful nlink, so we just never reuse entries
572
+ [ISREUSABLE](entry, st) {
573
+ return (entry.type === 'File' &&
574
+ !this.unlink &&
575
+ st.isFile() &&
576
+ st.nlink <= 1 &&
577
+ !isWindows);
578
+ }
579
+ // check if a thing is there, and if so, try to clobber it
580
+ [CHECKFS](entry) {
581
+ this[PEND]();
582
+ const paths = [entry.path];
583
+ if (entry.linkpath) {
584
+ paths.push(entry.linkpath);
585
+ }
586
+ this.reservations.reserve(paths, done => this[CHECKFS2](entry, done));
587
+ }
588
+ [PRUNECACHE](entry) {
589
+ // if we are not creating a directory, and the path is in the dirCache,
590
+ // then that means we are about to delete the directory we created
591
+ // previously, and it is no longer going to be a directory, and neither
592
+ // is any of its children.
593
+ // If a symbolic link is encountered, all bets are off. There is no
594
+ // reasonable way to sanitize the cache in such a way we will be able to
595
+ // avoid having filesystem collisions. If this happens with a non-symlink
596
+ // entry, it'll just fail to unpack, but a symlink to a directory, using an
597
+ // 8.3 shortname or certain unicode attacks, can evade detection and lead
598
+ // to arbitrary writes to anywhere on the system.
599
+ if (entry.type === 'SymbolicLink') {
600
+ dropCache(this.dirCache);
601
+ }
602
+ else if (entry.type !== 'Directory') {
603
+ pruneCache(this.dirCache, String(entry.absolute));
604
+ }
605
+ }
606
+ [CHECKFS2](entry, fullyDone) {
607
+ this[PRUNECACHE](entry);
608
+ const done = (er) => {
609
+ this[PRUNECACHE](entry);
610
+ fullyDone(er);
611
+ };
612
+ const checkCwd = () => {
613
+ this[MKDIR](this.cwd, this.dmode, er => {
614
+ if (er) {
615
+ this[ONERROR](er, entry);
616
+ done();
617
+ return;
618
+ }
619
+ this[CHECKED_CWD] = true;
620
+ start();
621
+ });
622
+ };
623
+ const start = () => {
624
+ if (entry.absolute !== this.cwd) {
625
+ const parent = (0, normalize_windows_path_js_1.normalizeWindowsPath)(node_path_1.default.dirname(String(entry.absolute)));
626
+ if (parent !== this.cwd) {
627
+ return this[MKDIR](parent, this.dmode, er => {
628
+ if (er) {
629
+ this[ONERROR](er, entry);
630
+ done();
631
+ return;
632
+ }
633
+ afterMakeParent();
634
+ });
635
+ }
636
+ }
637
+ afterMakeParent();
638
+ };
639
+ const afterMakeParent = () => {
640
+ node_fs_1.default.lstat(String(entry.absolute), (lstatEr, st) => {
641
+ if (st &&
642
+ (this.keep ||
643
+ /* c8 ignore next */
644
+ (this.newer && st.mtime > (entry.mtime ?? st.mtime)))) {
645
+ this[SKIP](entry);
646
+ done();
647
+ return;
648
+ }
649
+ if (lstatEr || this[ISREUSABLE](entry, st)) {
650
+ return this[MAKEFS](null, entry, done);
651
+ }
652
+ if (st.isDirectory()) {
653
+ if (entry.type === 'Directory') {
654
+ const needChmod = this.chmod &&
655
+ entry.mode &&
656
+ (st.mode & 0o7777) !== entry.mode;
657
+ const afterChmod = (er) => this[MAKEFS](er ?? null, entry, done);
658
+ if (!needChmod) {
659
+ return afterChmod();
660
+ }
661
+ return node_fs_1.default.chmod(String(entry.absolute), Number(entry.mode), afterChmod);
662
+ }
663
+ // Not a dir entry, have to remove it.
664
+ // NB: the only way to end up with an entry that is the cwd
665
+ // itself, in such a way that == does not detect, is a
666
+ // tricky windows absolute path with UNC or 8.3 parts (and
667
+ // preservePaths:true, or else it will have been stripped).
668
+ // In that case, the user has opted out of path protections
669
+ // explicitly, so if they blow away the cwd, c'est la vie.
670
+ if (entry.absolute !== this.cwd) {
671
+ return node_fs_1.default.rmdir(String(entry.absolute), (er) => this[MAKEFS](er ?? null, entry, done));
672
+ }
673
+ }
674
+ // not a dir, and not reusable
675
+ // don't remove if the cwd, we want that error
676
+ if (entry.absolute === this.cwd) {
677
+ return this[MAKEFS](null, entry, done);
678
+ }
679
+ unlinkFile(String(entry.absolute), er => this[MAKEFS](er ?? null, entry, done));
680
+ });
681
+ };
682
+ if (this[CHECKED_CWD]) {
683
+ start();
684
+ }
685
+ else {
686
+ checkCwd();
687
+ }
688
+ }
689
+ [MAKEFS](er, entry, done) {
690
+ if (er) {
691
+ this[ONERROR](er, entry);
692
+ done();
693
+ return;
694
+ }
695
+ switch (entry.type) {
696
+ case 'File':
697
+ case 'OldFile':
698
+ case 'ContiguousFile':
699
+ return this[FILE](entry, done);
700
+ case 'Link':
701
+ return this[HARDLINK](entry, done);
702
+ case 'SymbolicLink':
703
+ return this[SYMLINK](entry, done);
704
+ case 'Directory':
705
+ case 'GNUDumpDir':
706
+ return this[DIRECTORY](entry, done);
707
+ }
708
+ }
709
+ [LINK](entry, linkpath, link, done) {
710
+ // XXX: get the type ('symlink' or 'junction') for windows
711
+ node_fs_1.default[link](linkpath, String(entry.absolute), er => {
712
+ if (er) {
713
+ this[ONERROR](er, entry);
714
+ }
715
+ else {
716
+ this[UNPEND]();
717
+ entry.resume();
718
+ }
719
+ done();
720
+ });
721
+ }
722
+ }
723
+ exports.Unpack = Unpack;
724
+ const callSync = (fn) => {
725
+ try {
726
+ return [null, fn()];
727
+ }
728
+ catch (er) {
729
+ return [er, null];
730
+ }
731
+ };
732
+ class UnpackSync extends Unpack {
733
+ [MAKEFS](er, entry) {
734
+ return super[MAKEFS](er, entry, () => { });
735
+ }
736
+ [CHECKFS](entry) {
737
+ this[PRUNECACHE](entry);
738
+ if (!this[CHECKED_CWD]) {
739
+ const er = this[MKDIR](this.cwd, this.dmode);
740
+ if (er) {
741
+ return this[ONERROR](er, entry);
742
+ }
743
+ this[CHECKED_CWD] = true;
744
+ }
745
+ // don't bother to make the parent if the current entry is the cwd,
746
+ // we've already checked it.
747
+ if (entry.absolute !== this.cwd) {
748
+ const parent = (0, normalize_windows_path_js_1.normalizeWindowsPath)(node_path_1.default.dirname(String(entry.absolute)));
749
+ if (parent !== this.cwd) {
750
+ const mkParent = this[MKDIR](parent, this.dmode);
751
+ if (mkParent) {
752
+ return this[ONERROR](mkParent, entry);
753
+ }
754
+ }
755
+ }
756
+ const [lstatEr, st] = callSync(() => node_fs_1.default.lstatSync(String(entry.absolute)));
757
+ if (st &&
758
+ (this.keep ||
759
+ /* c8 ignore next */
760
+ (this.newer && st.mtime > (entry.mtime ?? st.mtime)))) {
761
+ return this[SKIP](entry);
762
+ }
763
+ if (lstatEr || this[ISREUSABLE](entry, st)) {
764
+ return this[MAKEFS](null, entry);
765
+ }
766
+ if (st.isDirectory()) {
767
+ if (entry.type === 'Directory') {
768
+ const needChmod = this.chmod &&
769
+ entry.mode &&
770
+ (st.mode & 0o7777) !== entry.mode;
771
+ const [er] = needChmod
772
+ ? callSync(() => {
773
+ node_fs_1.default.chmodSync(String(entry.absolute), Number(entry.mode));
774
+ })
775
+ : [];
776
+ return this[MAKEFS](er, entry);
777
+ }
778
+ // not a dir entry, have to remove it
779
+ const [er] = callSync(() => node_fs_1.default.rmdirSync(String(entry.absolute)));
780
+ this[MAKEFS](er, entry);
781
+ }
782
+ // not a dir, and not reusable.
783
+ // don't remove if it's the cwd, since we want that error.
784
+ const [er] = entry.absolute === this.cwd
785
+ ? []
786
+ : callSync(() => unlinkFileSync(String(entry.absolute)));
787
+ this[MAKEFS](er, entry);
788
+ }
789
+ [FILE](entry, done) {
790
+ const mode = typeof entry.mode === 'number'
791
+ ? entry.mode & 0o7777
792
+ : this.fmode;
793
+ const oner = (er) => {
794
+ let closeError;
795
+ try {
796
+ node_fs_1.default.closeSync(fd);
797
+ }
798
+ catch (e) {
799
+ closeError = e;
800
+ }
801
+ if (er || closeError) {
802
+ this[ONERROR](er || closeError, entry);
803
+ }
804
+ done();
805
+ };
806
+ let fd;
807
+ try {
808
+ fd = node_fs_1.default.openSync(String(entry.absolute), (0, get_write_flag_js_1.getWriteFlag)(entry.size), mode);
809
+ }
810
+ catch (er) {
811
+ return oner(er);
812
+ }
813
+ const tx = this.transform ? this.transform(entry) || entry : entry;
814
+ if (tx !== entry) {
815
+ tx.on('error', (er) => this[ONERROR](er, entry));
816
+ entry.pipe(tx);
817
+ }
818
+ tx.on('data', (chunk) => {
819
+ try {
820
+ node_fs_1.default.writeSync(fd, chunk, 0, chunk.length);
821
+ }
822
+ catch (er) {
823
+ oner(er);
824
+ }
825
+ });
826
+ tx.on('end', () => {
827
+ let er = null;
828
+ // try both, falling futimes back to utimes
829
+ // if either fails, handle the first error
830
+ if (entry.mtime && !this.noMtime) {
831
+ const atime = entry.atime || new Date();
832
+ const mtime = entry.mtime;
833
+ try {
834
+ node_fs_1.default.futimesSync(fd, atime, mtime);
835
+ }
836
+ catch (futimeser) {
837
+ try {
838
+ node_fs_1.default.utimesSync(String(entry.absolute), atime, mtime);
839
+ }
840
+ catch (utimeser) {
841
+ er = futimeser;
842
+ }
843
+ }
844
+ }
845
+ if (this[DOCHOWN](entry)) {
846
+ const uid = this[UID](entry);
847
+ const gid = this[GID](entry);
848
+ try {
849
+ node_fs_1.default.fchownSync(fd, Number(uid), Number(gid));
850
+ }
851
+ catch (fchowner) {
852
+ try {
853
+ node_fs_1.default.chownSync(String(entry.absolute), Number(uid), Number(gid));
854
+ }
855
+ catch (chowner) {
856
+ er = er || fchowner;
857
+ }
858
+ }
859
+ }
860
+ oner(er);
861
+ });
862
+ }
863
+ [DIRECTORY](entry, done) {
864
+ const mode = typeof entry.mode === 'number'
865
+ ? entry.mode & 0o7777
866
+ : this.dmode;
867
+ const er = this[MKDIR](String(entry.absolute), mode);
868
+ if (er) {
869
+ this[ONERROR](er, entry);
870
+ done();
871
+ return;
872
+ }
873
+ if (entry.mtime && !this.noMtime) {
874
+ try {
875
+ node_fs_1.default.utimesSync(String(entry.absolute), entry.atime || new Date(), entry.mtime);
876
+ /* c8 ignore next */
877
+ }
878
+ catch (er) { }
879
+ }
880
+ if (this[DOCHOWN](entry)) {
881
+ try {
882
+ node_fs_1.default.chownSync(String(entry.absolute), Number(this[UID](entry)), Number(this[GID](entry)));
883
+ }
884
+ catch (er) { }
885
+ }
886
+ done();
887
+ entry.resume();
888
+ }
889
+ [MKDIR](dir, mode) {
890
+ try {
891
+ return (0, mkdir_js_1.mkdirSync)((0, normalize_windows_path_js_1.normalizeWindowsPath)(dir), {
892
+ uid: this.uid,
893
+ gid: this.gid,
894
+ processUid: this.processUid,
895
+ processGid: this.processGid,
896
+ umask: this.processUmask,
897
+ preserve: this.preservePaths,
898
+ unlink: this.unlink,
899
+ cache: this.dirCache,
900
+ cwd: this.cwd,
901
+ mode: mode,
902
+ });
903
+ }
904
+ catch (er) {
905
+ return er;
906
+ }
907
+ }
908
+ [LINK](entry, linkpath, link, done) {
909
+ const ls = `${link}Sync`;
910
+ try {
911
+ node_fs_1.default[ls](linkpath, String(entry.absolute));
912
+ done();
913
+ entry.resume();
914
+ }
915
+ catch (er) {
916
+ return this[ONERROR](er, entry);
917
+ }
918
+ }
919
+ }
920
+ exports.UnpackSync = UnpackSync;
921
+ //# sourceMappingURL=unpack.js.map