1757fe0cbeb7f5d5bd9a562ec91b5f153cba3612d84df18fc00883f3192665512563e80d1182c001389ee97ef1d876bbf101dd1aafe9c22e3618fb6e758794 34 KB

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