tar 5.0.4 → 5.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/extract.js CHANGED
@@ -6,6 +6,7 @@ const Unpack = require('./unpack.js')
6
6
  const fs = require('fs')
7
7
  const fsm = require('fs-minipass')
8
8
  const path = require('path')
9
+ const stripSlash = require('./strip-trailing-slashes.js')
9
10
 
10
11
  const x = module.exports = (opt_, files, cb) => {
11
12
  if (typeof opt_ === 'function')
@@ -41,7 +42,7 @@ const x = module.exports = (opt_, files, cb) => {
41
42
  // construct a filter that limits the file entries listed
42
43
  // include child entries if a dir is included
43
44
  const filesFilter = (opt, files) => {
44
- const map = new Map(files.map(f => [f.replace(/\/+$/, ''), true]))
45
+ const map = new Map(files.map(f => [stripSlash(f), true]))
45
46
  const filter = opt.filter
46
47
 
47
48
  const mapHas = (file, r) => {
@@ -55,8 +56,8 @@ const filesFilter = (opt, files) => {
55
56
  }
56
57
 
57
58
  opt.filter = filter
58
- ? (file, entry) => filter(file, entry) && mapHas(file.replace(/\/+$/, ''))
59
- : file => mapHas(file.replace(/\/+$/, ''))
59
+ ? (file, entry) => filter(file, entry) && mapHas(stripSlash(file))
60
+ : file => mapHas(stripSlash(file))
60
61
  }
61
62
 
62
63
  const extractFileSync = opt => {
@@ -8,8 +8,11 @@
8
8
  // Only supported in Node v12.9.0 and above.
9
9
  const platform = process.env.__FAKE_PLATFORM__ || process.platform
10
10
  const isWindows = platform === 'win32'
11
- const fs = require('fs')
11
+ const fs = global.__FAKE_TESTING_FS__ || require('fs')
12
+
13
+ /* istanbul ignore next */
12
14
  const { O_CREAT, O_TRUNC, O_WRONLY, UV_FS_O_FILEMAP = 0 } = fs.constants
15
+
13
16
  const fMapEnabled = isWindows && !!UV_FS_O_FILEMAP
14
17
  const fMapLimit = 512 * 1024
15
18
  const fMapFlag = UV_FS_O_FILEMAP | O_TRUNC | O_CREAT | O_WRONLY
package/lib/list.js CHANGED
@@ -9,6 +9,7 @@ const Parser = require('./parse.js')
9
9
  const fs = require('fs')
10
10
  const fsm = require('fs-minipass')
11
11
  const path = require('path')
12
+ const stripSlash = require('./strip-trailing-slashes.js')
12
13
 
13
14
  const t = module.exports = (opt_, files, cb) => {
14
15
  if (typeof opt_ === 'function')
@@ -54,7 +55,7 @@ const onentryFunction = opt => {
54
55
  // construct a filter that limits the file entries listed
55
56
  // include child entries if a dir is included
56
57
  const filesFilter = (opt, files) => {
57
- const map = new Map(files.map(f => [f.replace(/\/+$/, ''), true]))
58
+ const map = new Map(files.map(f => [stripSlash(f), true]))
58
59
  const filter = opt.filter
59
60
 
60
61
  const mapHas = (file, r) => {
@@ -68,8 +69,8 @@ const filesFilter = (opt, files) => {
68
69
  }
69
70
 
70
71
  opt.filter = filter
71
- ? (file, entry) => filter(file, entry) && mapHas(file.replace(/\/+$/, ''))
72
- : file => mapHas(file.replace(/\/+$/, ''))
72
+ ? (file, entry) => filter(file, entry) && mapHas(stripSlash(file))
73
+ : file => mapHas(stripSlash(file))
73
74
  }
74
75
 
75
76
  const listFileSync = opt => {
package/lib/mkdir.js CHANGED
@@ -8,6 +8,7 @@ const mkdirp = require('mkdirp')
8
8
  const fs = require('fs')
9
9
  const path = require('path')
10
10
  const chownr = require('chownr')
11
+ const normPath = require('./normalize-windows-path.js')
11
12
 
12
13
  class SymlinkError extends Error {
13
14
  constructor (symlink, path) {
@@ -33,7 +34,20 @@ class CwdError extends Error {
33
34
  }
34
35
  }
35
36
 
36
- const mkdir = module.exports = (dir, opt, cb) => {
37
+ const cGet = (cache, key) => cache.get(normPath(key))
38
+ const cSet = (cache, key, val) => cache.set(normPath(key), val)
39
+
40
+ const checkCwd = (dir, cb) => {
41
+ fs.stat(dir, (er, st) => {
42
+ if (er || !st.isDirectory())
43
+ er = new CwdError(dir, er && er.code || 'ENOTDIR')
44
+ cb(er)
45
+ })
46
+ }
47
+
48
+ module.exports = (dir, opt, cb) => {
49
+ dir = normPath(dir)
50
+
37
51
  // if there's any overlap between mask and mode,
38
52
  // then we'll need an explicit chmod
39
53
  const umask = opt.umask
@@ -49,13 +63,13 @@ const mkdir = module.exports = (dir, opt, cb) => {
49
63
  const preserve = opt.preserve
50
64
  const unlink = opt.unlink
51
65
  const cache = opt.cache
52
- const cwd = opt.cwd
66
+ const cwd = normPath(opt.cwd)
53
67
 
54
68
  const done = (er, created) => {
55
69
  if (er)
56
70
  cb(er)
57
71
  else {
58
- cache.set(dir, true)
72
+ cSet(cache, dir, true)
59
73
  if (created && doChown)
60
74
  chownr(created, uid, gid, er => done(er))
61
75
  else if (needChmod)
@@ -65,21 +79,17 @@ const mkdir = module.exports = (dir, opt, cb) => {
65
79
  }
66
80
  }
67
81
 
68
- if (cache && cache.get(dir) === true)
82
+ if (cache && cGet(cache, dir) === true)
69
83
  return done()
70
84
 
71
85
  if (dir === cwd)
72
- return fs.stat(dir, (er, st) => {
73
- if (er || !st.isDirectory())
74
- er = new CwdError(dir, er && er.code || 'ENOTDIR')
75
- done(er)
76
- })
86
+ return checkCwd(dir, done)
77
87
 
78
88
  if (preserve)
79
89
  return mkdirp(dir, mode, done)
80
90
 
81
- const sub = path.relative(cwd, dir)
82
- const parts = sub.split(/\/|\\/)
91
+ const sub = normPath(path.relative(cwd, dir))
92
+ const parts = sub.split('/')
83
93
  mkdir_(cwd, parts, mode, cache, unlink, cwd, null, done)
84
94
  }
85
95
 
@@ -87,22 +97,19 @@ const mkdir_ = (base, parts, mode, cache, unlink, cwd, created, cb) => {
87
97
  if (!parts.length)
88
98
  return cb(null, created)
89
99
  const p = parts.shift()
90
- const part = base + '/' + p
91
- if (cache.get(part))
100
+ const part = normPath(path.resolve(base + '/' + p))
101
+ if (cGet(cache, part))
92
102
  return mkdir_(part, parts, mode, cache, unlink, cwd, created, cb)
93
103
  fs.mkdir(part, mode, onmkdir(part, parts, mode, cache, unlink, cwd, created, cb))
94
104
  }
95
105
 
96
106
  const onmkdir = (part, parts, mode, cache, unlink, cwd, created, cb) => er => {
97
107
  if (er) {
98
- if (er.path && path.dirname(er.path) === cwd &&
99
- (er.code === 'ENOTDIR' || er.code === 'ENOENT'))
100
- return cb(new CwdError(cwd, er.code))
101
-
102
108
  fs.lstat(part, (statEr, st) => {
103
- if (statEr)
109
+ if (statEr) {
110
+ statEr.path = statEr.path && normPath(statEr.path)
104
111
  cb(statEr)
105
- else if (st.isDirectory())
112
+ } else if (st.isDirectory())
106
113
  mkdir_(part, parts, mode, cache, unlink, cwd, created, cb)
107
114
  else if (unlink)
108
115
  fs.unlink(part, er => {
@@ -121,7 +128,21 @@ const onmkdir = (part, parts, mode, cache, unlink, cwd, created, cb) => er => {
121
128
  }
122
129
  }
123
130
 
124
- const mkdirSync = module.exports.sync = (dir, opt) => {
131
+ const checkCwdSync = dir => {
132
+ let ok = false
133
+ let code = 'ENOTDIR'
134
+ try {
135
+ ok = fs.statSync(dir).isDirectory()
136
+ } catch (er) {
137
+ code = er.code
138
+ } finally {
139
+ if (!ok)
140
+ throw new CwdError(dir, code)
141
+ }
142
+ }
143
+
144
+ module.exports.sync = (dir, opt) => {
145
+ dir = normPath(dir)
125
146
  // if there's any overlap between mask and mode,
126
147
  // then we'll need an explicit chmod
127
148
  const umask = opt.umask
@@ -137,65 +158,51 @@ const mkdirSync = module.exports.sync = (dir, opt) => {
137
158
  const preserve = opt.preserve
138
159
  const unlink = opt.unlink
139
160
  const cache = opt.cache
140
- const cwd = opt.cwd
161
+ const cwd = normPath(opt.cwd)
141
162
 
142
163
  const done = (created) => {
143
- cache.set(dir, true)
164
+ cSet(cache, dir, true)
144
165
  if (created && doChown)
145
166
  chownr.sync(created, uid, gid)
146
167
  if (needChmod)
147
168
  fs.chmodSync(dir, mode)
148
169
  }
149
170
 
150
- if (cache && cache.get(dir) === true)
171
+ if (cache && cGet(cache, dir) === true)
151
172
  return done()
152
173
 
153
174
  if (dir === cwd) {
154
- let ok = false
155
- let code = 'ENOTDIR'
156
- try {
157
- ok = fs.statSync(dir).isDirectory()
158
- } catch (er) {
159
- code = er.code
160
- } finally {
161
- if (!ok)
162
- throw new CwdError(dir, code)
163
- }
164
- done()
165
- return
175
+ checkCwdSync(cwd)
176
+ return done()
166
177
  }
167
178
 
168
179
  if (preserve)
169
180
  return done(mkdirp.sync(dir, mode))
170
181
 
171
- const sub = path.relative(cwd, dir)
172
- const parts = sub.split(/\/|\\/)
182
+ const sub = normPath(path.relative(cwd, dir))
183
+ const parts = sub.split('/')
173
184
  let created = null
174
185
  for (let p = parts.shift(), part = cwd;
175
- p && (part += '/' + p);
176
- p = parts.shift()) {
177
-
178
- if (cache.get(part))
186
+ p && (part += '/' + p);
187
+ p = parts.shift()) {
188
+ part = normPath(path.resolve(part))
189
+ if (cGet(cache, part))
179
190
  continue
180
191
 
181
192
  try {
182
193
  fs.mkdirSync(part, mode)
183
194
  created = created || part
184
- cache.set(part, true)
195
+ cSet(cache, part, true)
185
196
  } catch (er) {
186
- if (er.path && path.dirname(er.path) === cwd &&
187
- (er.code === 'ENOTDIR' || er.code === 'ENOENT'))
188
- return new CwdError(cwd, er.code)
189
-
190
197
  const st = fs.lstatSync(part)
191
198
  if (st.isDirectory()) {
192
- cache.set(part, true)
199
+ cSet(cache, part, true)
193
200
  continue
194
201
  } else if (unlink) {
195
202
  fs.unlinkSync(part)
196
203
  fs.mkdirSync(part, mode)
197
204
  created = created || part
198
- cache.set(part, true)
205
+ cSet(cache, part, true)
199
206
  continue
200
207
  } else if (st.isSymbolicLink())
201
208
  return new SymlinkError(part, part + '/' + parts.join('/'))
@@ -0,0 +1,8 @@
1
+ // on windows, either \ or / are valid directory separators.
2
+ // on unix, \ is a valid character in filenames.
3
+ // so, on windows, and only on windows, we replace all \ chars with /,
4
+ // so that we can use / as our one and only directory separator char.
5
+
6
+ const platform = process.env.TESTING_TAR_FAKE_PLATFORM || process.platform
7
+ module.exports = platform !== 'win32' ? p => p
8
+ : p => p && p.replace(/\\/g, '/')
package/lib/pack.js CHANGED
@@ -54,6 +54,7 @@ const ONDRAIN = Symbol('ondrain')
54
54
  const fs = require('fs')
55
55
  const path = require('path')
56
56
  const warner = require('./warn-mixin.js')
57
+ const normPath = require('./normalize-windows-path.js')
57
58
 
58
59
  const Pack = warner(class Pack extends MiniPass {
59
60
  constructor (opt) {
@@ -66,7 +67,7 @@ const Pack = warner(class Pack extends MiniPass {
66
67
  this.preservePaths = !!opt.preservePaths
67
68
  this.strict = !!opt.strict
68
69
  this.noPax = !!opt.noPax
69
- this.prefix = (opt.prefix || '').replace(/(\\|\/)+$/, '')
70
+ this.prefix = normPath(opt.prefix || '')
70
71
  this.linkCache = opt.linkCache || new Map()
71
72
  this.statCache = opt.statCache || new Map()
72
73
  this.readdirCache = opt.readdirCache || new Map()
@@ -75,10 +76,13 @@ const Pack = warner(class Pack extends MiniPass {
75
76
  if (typeof opt.onwarn === 'function')
76
77
  this.on('warn', opt.onwarn)
77
78
 
79
+ this.portable = !!opt.portable
78
80
  this.zip = null
79
81
  if (opt.gzip) {
80
82
  if (typeof opt.gzip !== 'object')
81
83
  opt.gzip = {}
84
+ if (this.portable)
85
+ opt.gzip.portable = true
82
86
  this.zip = new zlib.Gzip(opt.gzip)
83
87
  this.zip.on('data', chunk => super.write(chunk))
84
88
  this.zip.on('end', _ => super.end())
@@ -87,7 +91,6 @@ const Pack = warner(class Pack extends MiniPass {
87
91
  } else
88
92
  this.on('drain', this[ONDRAIN])
89
93
 
90
- this.portable = !!opt.portable
91
94
  this.noDirRecurse = !!opt.noDirRecurse
92
95
  this.follow = !!opt.follow
93
96
  this.noMtime = !!opt.noMtime
@@ -131,10 +134,7 @@ const Pack = warner(class Pack extends MiniPass {
131
134
  }
132
135
 
133
136
  [ADDTARENTRY] (p) {
134
- const absolute = path.resolve(this.cwd, p.path)
135
- if (this.prefix)
136
- p.path = this.prefix + '/' + p.path.replace(/^\.(\/+|$)/, '')
137
-
137
+ const absolute = normPath(path.resolve(this.cwd, p.path))
138
138
  // in this case, we don't have to wait for the stat
139
139
  if (!this.filter(p.path, p))
140
140
  p.resume()
@@ -150,10 +150,7 @@ const Pack = warner(class Pack extends MiniPass {
150
150
  }
151
151
 
152
152
  [ADDFSENTRY] (p) {
153
- const absolute = path.resolve(this.cwd, p)
154
- if (this.prefix)
155
- p = this.prefix + '/' + p.replace(/^\.(\/+|$)/, '')
156
-
153
+ const absolute = normPath(path.resolve(this.cwd, p))
157
154
  this[QUEUE].push(new PackJob(p, absolute))
158
155
  this[PROCESS]()
159
156
  }
@@ -295,7 +292,8 @@ const Pack = warner(class Pack extends MiniPass {
295
292
  linkCache: this.linkCache,
296
293
  statCache: this.statCache,
297
294
  noMtime: this.noMtime,
298
- mtime: this.mtime
295
+ mtime: this.mtime,
296
+ prefix: this.prefix,
299
297
  }
300
298
  }
301
299
 
@@ -321,10 +319,7 @@ const Pack = warner(class Pack extends MiniPass {
321
319
 
322
320
  if (job.readdir)
323
321
  job.readdir.forEach(entry => {
324
- const p = this.prefix ?
325
- job.path.slice(this.prefix.length + 1) || './'
326
- : job.path
327
-
322
+ const p = job.path
328
323
  const base = p === './' ? '' : p.replace(/\/*$/, '/')
329
324
  this[ADDFSENTRY](base + entry)
330
325
  })
@@ -377,10 +372,7 @@ class PackSync extends Pack {
377
372
 
378
373
  if (job.readdir)
379
374
  job.readdir.forEach(entry => {
380
- const p = this.prefix ?
381
- job.path.slice(this.prefix.length + 1) || './'
382
- : job.path
383
-
375
+ const p = job.path
384
376
  const base = p === './' ? '' : p.replace(/\/*$/, '/')
385
377
  this[ADDFSENTRY](base + entry)
386
378
  })
@@ -7,6 +7,7 @@
7
7
  // while still allowing maximal safe parallelization.
8
8
 
9
9
  const assert = require('assert')
10
+ const normPath = require('./normalize-windows-path.js')
10
11
 
11
12
  module.exports = () => {
12
13
  // path => [function or Set]
@@ -20,8 +21,9 @@ module.exports = () => {
20
21
  // return a set of parent dirs for a given path
21
22
  const { join } = require('path')
22
23
  const getDirs = path =>
23
- join(path).split(/[\\\/]/).slice(0, -1).reduce((set, path) =>
24
- set.length ? set.concat(join(set[set.length-1], path)) : [path], [])
24
+ normPath(join(path)).split('/').slice(0, -1).reduce((set, path) =>
25
+ set.length ? set.concat(normPath(join(set[set.length - 1], path)))
26
+ : [path], [])
25
27
 
26
28
  // functions currently running
27
29
  const running = new Set()
package/lib/read-entry.js CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict'
2
2
  const types = require('./types.js')
3
3
  const MiniPass = require('minipass')
4
+ const normPath = require('./normalize-windows-path.js')
4
5
 
5
6
  const SLURP = Symbol('slurp')
6
7
  module.exports = class ReadEntry extends MiniPass {
@@ -47,7 +48,7 @@ module.exports = class ReadEntry extends MiniPass {
47
48
  this.ignore = true
48
49
  }
49
50
 
50
- this.path = header.path
51
+ this.path = normPath(header.path)
51
52
  this.mode = header.mode
52
53
  if (this.mode)
53
54
  this.mode = this.mode & 0o7777
@@ -59,7 +60,7 @@ module.exports = class ReadEntry extends MiniPass {
59
60
  this.mtime = header.mtime
60
61
  this.atime = header.atime
61
62
  this.ctime = header.ctime
62
- this.linkpath = header.linkpath
63
+ this.linkpath = normPath(header.linkpath)
63
64
  this.uname = header.uname
64
65
  this.gname = header.gname
65
66
 
@@ -92,7 +93,7 @@ module.exports = class ReadEntry extends MiniPass {
92
93
  // a global extended header, because that's weird.
93
94
  if (ex[k] !== null && ex[k] !== undefined &&
94
95
  !(global && k === 'path'))
95
- this[k] = ex[k]
96
+ this[k] = k === 'path' || k === 'linkpath' ? normPath(ex[k]) : ex[k]
96
97
  }
97
98
  }
98
99
  }
package/lib/replace.js CHANGED
@@ -167,7 +167,8 @@ const replace = (opt, files, cb) => {
167
167
 
168
168
  fs.fstat(fd, (er, st) => {
169
169
  if (er)
170
- return reject(er)
170
+ return fs.close(fd, () => reject(er))
171
+
171
172
  getPos(fd, st.size, (er, position) => {
172
173
  if (er)
173
174
  return reject(er)
@@ -0,0 +1,14 @@
1
+ // unix absolute paths are also absolute on win32, so we use this for both
2
+ const { isAbsolute, parse } = require('path').win32
3
+
4
+ // returns [root, stripped]
5
+ module.exports = path => {
6
+ let r = ''
7
+ while (isAbsolute(path)) {
8
+ // windows will think that //x/y/z has a "root" of //x/y/
9
+ const root = path.charAt(0) === '/' ? '/' : parse(path).root
10
+ path = path.substr(root.length)
11
+ r += root
12
+ }
13
+ return [r, path]
14
+ }
@@ -0,0 +1,24 @@
1
+ // this is the only approach that was significantly faster than using
2
+ // str.replace(/\/+$/, '') for strings ending with a lot of / chars and
3
+ // containing multiple / chars.
4
+ const batchStrings = [
5
+ '/'.repeat(1024),
6
+ '/'.repeat(512),
7
+ '/'.repeat(256),
8
+ '/'.repeat(128),
9
+ '/'.repeat(64),
10
+ '/'.repeat(32),
11
+ '/'.repeat(16),
12
+ '/'.repeat(8),
13
+ '/'.repeat(4),
14
+ '/'.repeat(2),
15
+ '/',
16
+ ]
17
+
18
+ module.exports = str => {
19
+ for (const s of batchStrings) {
20
+ while (str.length >= s.length && str.slice(-1 * s.length) === s)
21
+ str = str.slice(0, -1 * s.length)
22
+ }
23
+ return str
24
+ }