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