tar
Version:
tar for JavaScript
121 lines • 4.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.delete = exports.deleteSync = void 0;
const codec_js_1 = require("./codec.js");
const create_js_1 = require("./create.js");
const dealias_js_1 = require("./dealias.js");
const entries_js_1 = require("./entries.js");
const list_js_1 = require("./list.js");
const read_file_js_1 = require("./read-file.js");
const noop = () => { };
// The codecs to look for, and the filter and the report of the paths
// to delete.
const plan = (opt, files) => {
if (!opt.file)
throw new TypeError('file is required');
if (!opt.fs)
throw new TypeError('the file option needs the fs option');
const codecs = [...(opt.codecs ?? []), codec_js_1.gzip];
if (files?.length)
return [codecs, ...(0, list_js_1.filesFilter)(files, opt.filter)];
if (opt.filter)
return [codecs, opt.filter];
throw new TypeError('no paths specified to delete');
};
// The path of the new archive, until it gets the path of the old one.
const tmpName = (file) => `${file}.${Math.random().toString(36).slice(2)}.tmp`;
/**
* Remove entries from an archive file. An entry is removed if
* `files` lists its path or one of its parent directories. With
* `opt.filter`, only if the filter also returns true. With no
* `files`, each entry that the filter accepts is removed. A listed
* path that no entry matches is a warning.
*
* The other entries are written to a new file next to the archive,
* with the compression and the mode of the archive. The new file
* then gets the path of the archive. The archive does not change if
* the command fails.
*/
const del = async (options, files) => {
const opt = (0, dealias_js_1.dealias)(options);
const [codecs, remove, report] = plan(opt, files);
const { fs, file, filter: _, maxReadSize, ...read } = opt;
const head = new Uint8Array((0, entries_js_1.headLength)(codecs));
let length = 0;
const handle = await fs.open(file, 'r');
try {
for (let n = 1; n > 0 && length < head.length; length += n) {
n = (await handle.read(head, length, head.length - length, length))
.bytesRead;
}
}
finally {
await handle.close();
}
const { mode } = await fs.stat(file);
const tmp = tmpName(file);
try {
await (0, create_js_1.create)({
...read,
fs,
file: tmp,
mode: mode & 0o777,
compress: (0, entries_js_1.findCodec)(codecs, head.subarray(0, length), file),
preservePaths: true,
filter: (path, entry) => !remove(path, entry),
}, [new entries_js_1.TarReader((0, read_file_js_1.readFile)(fs, file, maxReadSize), { ...read, file })]);
report?.(opt);
await fs.rename(tmp, file);
}
catch (er) {
await fs.unlink(tmp).catch(noop);
throw er;
}
};
exports.delete = del;
/** The sync form of `delete`. */
const deleteSync = (options, files) => {
const opt = (0, dealias_js_1.dealias)(options);
const [codecs, remove, report] = plan(opt, files);
const { fs, file, filter: _, maxReadSize, ...read } = opt;
const head = new Uint8Array((0, entries_js_1.headLength)(codecs));
let length = 0;
const fd = fs.openSync(file, 'r');
try {
for (let n = 1; n > 0 && length < head.length; length += n) {
n = fs.readSync(fd, head, length, head.length - length, length);
}
}
finally {
fs.closeSync(fd);
}
const { mode } = fs.statSync(file);
const tmp = tmpName(file);
try {
(0, create_js_1.createSync)({
...read,
fs,
file: tmp,
mode: mode & 0o777,
compress: (0, entries_js_1.findCodec)(codecs, head.subarray(0, length), file),
preservePaths: true,
filter: (path, entry) => !remove(path, entry),
}, [
new entries_js_1.TarReaderSync((0, read_file_js_1.readFileSync)(fs, file, maxReadSize), {
...read,
file,
}),
]);
report?.(opt);
fs.renameSync(tmp, file);
}
catch (er) {
try {
fs.unlinkSync(tmp);
}
catch { }
throw er;
}
};
exports.deleteSync = deleteSync;
//# sourceMappingURL=delete.js.map