1'use strict'
2
3// tar -u
4
5const hlo = require('./high-level-opt.js')
6const r = require('./replace.js')
7// just call tar.r with the filter and mtimeCache
8
9module.exports = (opt_, files, cb) => {
10  const opt = hlo(opt_)
11
12  if (!opt.file) {
13    throw new TypeError('file is required')
14  }
15
16  if (opt.gzip || opt.brotli || opt.file.endsWith('.br') || opt.file.endsWith('.tbr')) {
17    throw new TypeError('cannot append to compressed archives')
18  }
19
20  if (!files || !Array.isArray(files) || !files.length) {
21    throw new TypeError('no files or directories specified')
22  }
23
24  files = Array.from(files)
25
26  mtimeFilter(opt)
27  return r(opt, files, cb)
28}
29
30const mtimeFilter = opt => {
31  const filter = opt.filter
32
33  if (!opt.mtimeCache) {
34    opt.mtimeCache = new Map()
35  }
36
37  opt.filter = filter ? (path, stat) =>
38    filter(path, stat) && !(opt.mtimeCache.get(path) > stat.mtime)
39    : (path, stat) => !(opt.mtimeCache.get(path) > stat.mtime)
40}
41