tar
Version:
tar for JavaScript
42 lines • 1.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateSync = exports.update = void 0;
const replace_js_1 = require("./replace.js");
// Add a filter that refuses a path that is not newer than the entry
// of the same path in the archive. The filter accepts each directory
// of the file system: a file in a directory can be newer than the
// directory, and the pack reads a directory only if it adds it.
// A ustar header has the time in whole seconds. A file is not newer
// than such an entry if only the part below one second differs.
const isNewer = (file, archived) => {
const a = archived.getTime();
const f = file.getTime();
return (a % 1000 === 0 ? f - (f % 1000) : f) > a;
};
const newer = (opt) => {
const { filter } = opt;
const mtimeCache = opt.mtimeCache ?? new Map();
return {
...opt,
mtimeCache,
filter: (path, entry) => {
if (filter && !filter(path, entry))
return false;
if ('isDirectory' in entry && entry.isDirectory())
return true;
const archived = mtimeCache.get(path);
return !archived || !entry.mtime || isNewer(entry.mtime, archived);
},
};
};
/**
* The same as `replace`, but a file is added only if it is newer than
* the last entry of the archive that has its path. A directory is
* always added, and each path in it is checked.
*/
const update = (opt, files) => (0, replace_js_1.replace)(newer(opt), files);
exports.update = update;
/** The sync form of `update`. */
const updateSync = (opt, files) => (0, replace_js_1.replaceSync)(newer(opt), files);
exports.updateSync = updateSync;
//# sourceMappingURL=update.js.map