UNPKG

tar

Version:
181 lines • 6.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.replaceSync = exports.replace = exports.AppendScan = void 0; const codec_js_1 = require("./codec.js"); const dealias_js_1 = require("./dealias.js"); const create_js_1 = require("./create.js"); const parser_js_1 = require("./parser.js"); // The most bytes to read from the archive at one time. const CHUNK = 64 * 1024; // Thrown in the parser to stop at the first block that is not valid. const STOP = {}; const noop = () => { }; const compressed = () => new Error('cannot append to compressed archives'); const check = (opt, files) => { const { file } = opt; if (!file) throw new TypeError('file is required'); if (opt.gzip || opt.brotli || opt.zstd || file.endsWith('.br') || file.endsWith('.tbr')) { throw new TypeError(compressed().message); } if (!files?.length) { throw new TypeError('no paths specified to add/replace'); } }; /** * Finds the place in an archive file where new entries go: the end * of the last complete entry before the first block that is not a * valid header. A PAX or GNU header with no entry after it is not a * complete entry. * * Read the file into `buf` at `pos`, and call `push()`, until it * returns false. The bodies of the entries are not read. */ class AppendScan { buf = new Uint8Array(CHUNK); /** The place in the file of the next read. */ pos = 0; #size; #parser; #codecs; #mtimes; /** * @param size the size of the file * @param mtimes gets the path and the `mtime` of each complete entry */ constructor(size, codecs = [], mtimes) { this.#size = size; this.#codecs = [...codecs, codec_js_1.gzip]; this.#mtimes = mtimes; this.#parser = new parser_js_1.Parser({ // The parser must not give the bodies. filter: () => false, onwarn: (_code, _message, data) => { // An error of the header code goes to the caller. throw data instanceof Error ? data : STOP; }, }); } /** The place in the file where new entries go. */ get position() { return this.#parser.position; } /** The number of bytes to read into `buf` at `pos`. */ get want() { return Math.min(CHUNK, this.#size - this.pos); } /** * Parse the first `length` bytes of `buf`. False if the place is * found. Throws if the file is compressed. */ push(length) { const chunk = this.buf.subarray(0, length); if (this.pos === 0 && this.#codecs.some(c => c.detect(chunk))) { throw compressed(); } const parser = this.#parser; this.pos += length; // the end of the body of the last entry in the chunk let end = 0; parser.push(chunk); try { for (let ev; (ev = parser.next());) { if (ev.type !== 'entry') return false; const { entry } = ev; end = parser.offset + 512 * Math.ceil(entry.size / 512); if (end > this.#size) return false; if (entry.mtime) this.#mtimes?.set(entry.path, entry.mtime); } // The rest of the body of the last entry is not read. if (this.pos < end) { parser.skipBody(); this.pos = end; } } catch (er) { if (er !== STOP) throw er; return false; } return length > 0 && this.pos < this.#size; } } exports.AppendScan = AppendScan; /** * Add the files to the end of an archive file that is not * compressed. The files are the same as for `create`. If the archive * does not exist, it is made. * * The new entries go after the last complete entry of the archive. * A reader of the archive gets the old entry and then the new entry * of a path that is in it two times. */ const replace = async (options, files) => { const opt = (0, dealias_js_1.dealias)(options); check(opt, files); const { fs, file } = opt; const pack = (0, create_js_1.packFiles)(opt, files); if (pack.codec) throw new TypeError(compressed().message); const handle = await fs.open(file, 'r+').catch(er => { if (er?.code !== 'ENOENT') throw er; return fs.open(file, 'w+', opt.mode || 0o666); }); let ok = false; try { const { size } = await handle.stat(); const scan = new AppendScan(size, opt.codecs, opt.mtimeCache); while (scan.push((await handle.read(scan.buf, 0, scan.want, scan.pos)).bytesRead)) ; await (0, create_js_1.writeChunks)(handle, pack, scan.position); ok = true; } finally { const closed = handle.close(); await (ok ? closed : closed.catch(noop)); } }; exports.replace = replace; /** The sync form of `replace`. */ const replaceSync = (options, files) => { const opt = (0, dealias_js_1.dealias)(options); check(opt, files); const { fs, file } = opt; const pack = (0, create_js_1.packFilesSync)(opt, files); if (pack.codec) throw new TypeError(compressed().message); let fd; try { fd = fs.openSync(file, 'r+'); } catch (er) { if (er?.code !== 'ENOENT') throw er; fd = fs.openSync(file, 'w+', opt.mode || 0o666); } try { const { size } = fs.fstatSync(fd); const scan = new AppendScan(size, opt.codecs, opt.mtimeCache); while (scan.push(fs.readSync(fd, scan.buf, 0, scan.want, scan.pos))) ; (0, create_js_1.writeChunksSync)(fs, fd, pack, scan.position); } catch (er) { try { fs.closeSync(fd); } catch { } throw er; } fs.closeSync(fd); }; exports.replaceSync = replaceSync; //# sourceMappingURL=replace.js.map