tar 6.1.6 → 6.1.7

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/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
 
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
+
36
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 @@ 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,22 +79,17 @@ 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
- 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
- })
77
- }
85
+ if (dir === cwd)
86
+ return checkCwd(dir, done)
78
87
 
79
88
  if (preserve)
80
89
  return mkdirp(dir, {mode}).then(made => done(null, made), done)
81
90
 
82
- const sub = path.relative(cwd, dir)
83
- const parts = sub.split(/\/|\\/)
91
+ const sub = normPath(path.relative(cwd, dir))
92
+ const parts = sub.split('/')
84
93
  mkdir_(cwd, parts, mode, cache, unlink, cwd, null, done)
85
94
  }
86
95
 
@@ -88,22 +97,19 @@ const mkdir_ = (base, parts, mode, cache, unlink, cwd, created, cb) => {
88
97
  if (!parts.length)
89
98
  return cb(null, created)
90
99
  const p = parts.shift()
91
- const part = base + '/' + p
92
- if (cache.get(part))
100
+ const part = normPath(path.resolve(base + '/' + p))
101
+ if (cGet(cache, part))
93
102
  return mkdir_(part, parts, mode, cache, unlink, cwd, created, cb)
94
103
  fs.mkdir(part, mode, onmkdir(part, parts, mode, cache, unlink, cwd, created, cb))
95
104
  }
96
105
 
97
106
  const onmkdir = (part, parts, mode, cache, unlink, cwd, created, cb) => er => {
98
107
  if (er) {
99
- if (er.path && path.dirname(er.path) === cwd &&
100
- (er.code === 'ENOTDIR' || er.code === 'ENOENT'))
101
- return cb(new CwdError(cwd, er.code))
102
-
103
108
  fs.lstat(part, (statEr, st) => {
104
- if (statEr)
109
+ if (statEr) {
110
+ statEr.path = statEr.path && normPath(statEr.path)
105
111
  cb(statEr)
106
- else if (st.isDirectory())
112
+ } else if (st.isDirectory())
107
113
  mkdir_(part, parts, mode, cache, unlink, cwd, created, cb)
108
114
  else if (unlink) {
109
115
  fs.unlink(part, er => {
@@ -122,7 +128,21 @@ const onmkdir = (part, parts, mode, cache, unlink, cwd, created, cb) => er => {
122
128
  }
123
129
  }
124
130
 
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
+
125
144
  module.exports.sync = (dir, opt) => {
145
+ dir = normPath(dir)
126
146
  // if there's any overlap between mask and mode,
127
147
  // then we'll need an explicit chmod
128
148
  const umask = opt.umask
@@ -138,64 +158,51 @@ module.exports.sync = (dir, opt) => {
138
158
  const preserve = opt.preserve
139
159
  const unlink = opt.unlink
140
160
  const cache = opt.cache
141
- const cwd = opt.cwd
161
+ const cwd = normPath(opt.cwd)
142
162
 
143
163
  const done = (created) => {
144
- cache.set(dir, true)
164
+ cSet(cache, dir, true)
145
165
  if (created && doChown)
146
166
  chownr.sync(created, uid, gid)
147
167
  if (needChmod)
148
168
  fs.chmodSync(dir, mode)
149
169
  }
150
170
 
151
- if (cache && cache.get(dir) === true)
171
+ if (cache && cGet(cache, dir) === true)
152
172
  return done()
153
173
 
154
174
  if (dir === cwd) {
155
- let ok = false
156
- let code = 'ENOTDIR'
157
- try {
158
- ok = fs.statSync(dir).isDirectory()
159
- } catch (er) {
160
- code = er.code
161
- } finally {
162
- if (!ok)
163
- throw new CwdError(dir, code)
164
- }
165
- done()
166
- return
175
+ checkCwdSync(cwd)
176
+ return done()
167
177
  }
168
178
 
169
179
  if (preserve)
170
180
  return done(mkdirp.sync(dir, mode))
171
181
 
172
- const sub = path.relative(cwd, dir)
173
- const parts = sub.split(/\/|\\/)
182
+ const sub = normPath(path.relative(cwd, dir))
183
+ const parts = sub.split('/')
174
184
  let created = null
175
185
  for (let p = parts.shift(), part = cwd;
176
186
  p && (part += '/' + p);
177
187
  p = parts.shift()) {
178
- if (cache.get(part))
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()
@@ -133,7 +134,7 @@ const Pack = warner(class Pack extends MiniPass {
133
134
  }
134
135
 
135
136
  [ADDTARENTRY] (p) {
136
- const absolute = path.resolve(this.cwd, p.path)
137
+ const absolute = normPath(path.resolve(this.cwd, p.path))
137
138
  // in this case, we don't have to wait for the stat
138
139
  if (!this.filter(p.path, p))
139
140
  p.resume()
@@ -149,7 +150,7 @@ const Pack = warner(class Pack extends MiniPass {
149
150
  }
150
151
 
151
152
  [ADDFSENTRY] (p) {
152
- const absolute = path.resolve(this.cwd, p)
153
+ const absolute = normPath(path.resolve(this.cwd, p))
153
154
  this[QUEUE].push(new PackJob(p, absolute))
154
155
  this[PROCESS]()
155
156
  }
@@ -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,5 +1,6 @@
1
1
  'use strict'
2
2
  const MiniPass = require('minipass')
3
+ const normPath = require('./normalize-windows-path.js')
3
4
 
4
5
  const SLURP = Symbol('slurp')
5
6
  module.exports = class ReadEntry extends MiniPass {
@@ -46,7 +47,7 @@ module.exports = class ReadEntry extends MiniPass {
46
47
  this.ignore = true
47
48
  }
48
49
 
49
- this.path = header.path
50
+ this.path = normPath(header.path)
50
51
  this.mode = header.mode
51
52
  if (this.mode)
52
53
  this.mode = this.mode & 0o7777
@@ -58,7 +59,7 @@ module.exports = class ReadEntry extends MiniPass {
58
59
  this.mtime = header.mtime
59
60
  this.atime = header.atime
60
61
  this.ctime = header.ctime
61
- this.linkpath = header.linkpath
62
+ this.linkpath = normPath(header.linkpath)
62
63
  this.uname = header.uname
63
64
  this.gname = header.gname
64
65
 
@@ -93,7 +94,7 @@ module.exports = class ReadEntry extends MiniPass {
93
94
  // a global extended header, because that's weird.
94
95
  if (ex[k] !== null && ex[k] !== undefined &&
95
96
  !(global && k === 'path'))
96
- this[k] = ex[k]
97
+ this[k] = k === 'path' || k === 'linkpath' ? normPath(ex[k]) : ex[k]
97
98
  }
98
99
  }
99
100
  }
package/lib/replace.js CHANGED
@@ -170,7 +170,8 @@ const replace = (opt, files, cb) => {
170
170
 
171
171
  fs.fstat(fd, (er, st) => {
172
172
  if (er)
173
- return reject(er)
173
+ return fs.close(fd, () => reject(er))
174
+
174
175
  getPos(fd, st.size, (er, position) => {
175
176
  if (er)
176
177
  return reject(er)
package/lib/unpack.js CHANGED
@@ -15,6 +15,7 @@ const mkdir = require('./mkdir.js')
15
15
  const wc = require('./winchars.js')
16
16
  const pathReservations = require('./path-reservations.js')
17
17
  const stripAbsolutePath = require('./strip-absolute-path.js')
18
+ const normPath = require('./normalize-windows-path.js')
18
19
 
19
20
  const ONENTRY = Symbol('onEntry')
20
21
  const CHECKFS = Symbol('checkFs')
@@ -39,14 +40,10 @@ const SKIP = Symbol('skip')
39
40
  const DOCHOWN = Symbol('doChown')
40
41
  const UID = Symbol('uid')
41
42
  const GID = Symbol('gid')
43
+ const CHECKED_CWD = Symbol('checkedCwd')
42
44
  const crypto = require('crypto')
43
45
  const getFlag = require('./get-write-flag.js')
44
46
 
45
- /* istanbul ignore next */
46
- const neverCalled = () => {
47
- throw new Error('sync function called cb somehow?!?')
48
- }
49
-
50
47
  // Unlinks on Windows are not atomic.
51
48
  //
52
49
  // This means that if you have a file entry, followed by another
@@ -91,6 +88,17 @@ const uint32 = (a, b, c) =>
91
88
  : b === b >>> 0 ? b
92
89
  : c
93
90
 
91
+ const pruneCache = (cache, abs) => {
92
+ // clear the cache if it's a case-insensitive match, since we can't
93
+ // know if the current file system is case-sensitive or not.
94
+ abs = normPath(abs).toLowerCase()
95
+ for (const path of cache.keys()) {
96
+ const plower = path.toLowerCase()
97
+ if (plower === abs || plower.toLowerCase().indexOf(abs + '/') === 0)
98
+ cache.delete(path)
99
+ }
100
+ }
101
+
94
102
  class Unpack extends Parser {
95
103
  constructor (opt) {
96
104
  if (!opt)
@@ -103,6 +111,8 @@ class Unpack extends Parser {
103
111
 
104
112
  super(opt)
105
113
 
114
+ this[CHECKED_CWD] = false
115
+
106
116
  this.reservations = pathReservations()
107
117
 
108
118
  this.transform = typeof opt.transform === 'function' ? opt.transform : null
@@ -168,7 +178,7 @@ class Unpack extends Parser {
168
178
  // links, and removes symlink directories rather than erroring
169
179
  this.unlink = !!opt.unlink
170
180
 
171
- this.cwd = path.resolve(opt.cwd || process.cwd())
181
+ this.cwd = normPath(path.resolve(opt.cwd || process.cwd()))
172
182
  this.strip = +opt.strip || 0
173
183
  // if we're not chmodding, then we don't need the process umask
174
184
  this.processUmask = opt.noChmod ? 0 : process.umask()
@@ -201,23 +211,21 @@ class Unpack extends Parser {
201
211
 
202
212
  [CHECKPATH] (entry) {
203
213
  if (this.strip) {
204
- const parts = entry.path.split(/\/|\\/)
214
+ const parts = normPath(entry.path).split('/')
205
215
  if (parts.length < this.strip)
206
216
  return false
207
217
  entry.path = parts.slice(this.strip).join('/')
208
- if (entry.path === '' && entry.type !== 'Directory' && entry.type !== 'GNUDumpDir')
209
- return false
210
218
 
211
219
  if (entry.type === 'Link') {
212
- const linkparts = entry.linkpath.split(/\/|\\/)
220
+ const linkparts = normPath(entry.linkpath).split('/')
213
221
  if (linkparts.length >= this.strip)
214
222
  entry.linkpath = linkparts.slice(this.strip).join('/')
215
223
  }
216
224
  }
217
225
 
218
226
  if (!this.preservePaths) {
219
- const p = entry.path
220
- if (p.match(/(^|\/|\\)\.\.(\\|\/|$)/)) {
227
+ const p = normPath(entry.path)
228
+ if (p.split('/').includes('..')) {
221
229
  this.warn('TAR_ENTRY_ERROR', `path contains '..'`, {
222
230
  entry,
223
231
  path: p,
@@ -237,18 +245,26 @@ class Unpack extends Parser {
237
245
  }
238
246
  }
239
247
 
248
+ if (path.isAbsolute(entry.path))
249
+ entry.absolute = normPath(path.resolve(entry.path))
250
+ else
251
+ entry.absolute = normPath(path.resolve(this.cwd, entry.path))
252
+
253
+ // an archive can set properties on the extraction directory, but it
254
+ // may not replace the cwd with a different kind of thing entirely.
255
+ if (entry.absolute === this.cwd &&
256
+ entry.type !== 'Directory' &&
257
+ entry.type !== 'GNUDumpDir')
258
+ return false
259
+
240
260
  // only encode : chars that aren't drive letter indicators
241
261
  if (this.win32) {
242
- const parsed = path.win32.parse(entry.path)
243
- entry.path = parsed.root === '' ? wc.encode(entry.path)
244
- : parsed.root + wc.encode(entry.path.substr(parsed.root.length))
262
+ const { root: aRoot } = path.win32.parse(entry.absolute)
263
+ entry.absolute = aRoot + wc.encode(entry.absolute.substr(aRoot.length))
264
+ const { root: pRoot } = path.win32.parse(entry.path)
265
+ entry.path = pRoot + wc.encode(entry.path.substr(pRoot.length))
245
266
  }
246
267
 
247
- if (path.isAbsolute(entry.path))
248
- entry.absolute = entry.path
249
- else
250
- entry.absolute = path.resolve(this.cwd, entry.path)
251
-
252
268
  return true
253
269
  }
254
270
 
@@ -293,7 +309,7 @@ class Unpack extends Parser {
293
309
  }
294
310
 
295
311
  [MKDIR] (dir, mode, cb) {
296
- mkdir(dir, {
312
+ mkdir(normPath(dir), {
297
313
  uid: this.uid,
298
314
  gid: this.gid,
299
315
  processUid: this.processUid,
@@ -338,6 +354,7 @@ class Unpack extends Parser {
338
354
  stream.on('error', er => {
339
355
  if (stream.fd)
340
356
  fs.close(stream.fd, () => {})
357
+
341
358
  // flush all the data out so that we aren't left hanging
342
359
  // if the error wasn't actually fatal. otherwise the parse
343
360
  // is blocked, and we never proceed.
@@ -352,6 +369,7 @@ class Unpack extends Parser {
352
369
  /* istanbul ignore else - we should always have a fd by now */
353
370
  if (stream.fd)
354
371
  fs.close(stream.fd, () => {})
372
+
355
373
  this[ONERROR](er, entry)
356
374
  fullyDone()
357
375
  return
@@ -451,7 +469,8 @@ class Unpack extends Parser {
451
469
  }
452
470
 
453
471
  [HARDLINK] (entry, done) {
454
- this[LINK](entry, path.resolve(this.cwd, entry.linkpath), 'link', done)
472
+ const linkpath = normPath(path.resolve(this.cwd, entry.linkpath))
473
+ this[LINK](entry, linkpath, 'link', done)
455
474
  }
456
475
 
457
476
  [PEND] () {
@@ -493,41 +512,79 @@ class Unpack extends Parser {
493
512
  // then that means we are about to delete the directory we created
494
513
  // previously, and it is no longer going to be a directory, and neither
495
514
  // is any of its children.
496
- if (entry.type !== 'Directory') {
497
- for (const path of this.dirCache.keys()) {
498
- if (path === entry.absolute ||
499
- path.indexOf(entry.absolute + '/') === 0 ||
500
- path.indexOf(entry.absolute + '\\') === 0)
501
- this.dirCache.delete(path)
502
- }
515
+ if (entry.type !== 'Directory')
516
+ pruneCache(this.dirCache, entry.absolute)
517
+
518
+ const checkCwd = () => {
519
+ this[MKDIR](this.cwd, this.dmode, er => {
520
+ if (er) {
521
+ this[ONERROR](er, entry)
522
+ done()
523
+ return
524
+ }
525
+ this[CHECKED_CWD] = true
526
+ start()
527
+ })
503
528
  }
504
529
 
505
- this[MKDIR](path.dirname(entry.absolute), this.dmode, er => {
506
- if (er) {
507
- this[ONERROR](er, entry)
508
- done()
509
- return
530
+ const start = () => {
531
+ if (entry.absolute !== this.cwd) {
532
+ const parent = normPath(path.dirname(entry.absolute))
533
+ if (parent !== this.cwd) {
534
+ return this[MKDIR](parent, this.dmode, er => {
535
+ if (er) {
536
+ this[ONERROR](er, entry)
537
+ done()
538
+ return
539
+ }
540
+ afterMakeParent()
541
+ })
542
+ }
510
543
  }
511
- fs.lstat(entry.absolute, (er, st) => {
544
+ afterMakeParent()
545
+ }
546
+
547
+ const afterMakeParent = () => {
548
+ fs.lstat(entry.absolute, (lstatEr, st) => {
512
549
  if (st && (this.keep || this.newer && st.mtime > entry.mtime)) {
513
550
  this[SKIP](entry)
514
551
  done()
515
- } else if (er || this[ISREUSABLE](entry, st))
516
- this[MAKEFS](null, entry, done)
517
- else if (st.isDirectory()) {
552
+ return
553
+ }
554
+ if (lstatEr || this[ISREUSABLE](entry, st))
555
+ return this[MAKEFS](null, entry, done)
556
+
557
+ if (st.isDirectory()) {
518
558
  if (entry.type === 'Directory') {
519
- if (!this.noChmod && (!entry.mode || (st.mode & 0o7777) === entry.mode))
520
- this[MAKEFS](null, entry, done)
521
- else {
522
- fs.chmod(entry.absolute, entry.mode,
523
- er => this[MAKEFS](er, entry, done))
524
- }
525
- } else
526
- fs.rmdir(entry.absolute, er => this[MAKEFS](er, entry, done))
527
- } else
528
- unlinkFile(entry.absolute, er => this[MAKEFS](er, entry, done))
559
+ const needChmod = !this.noChmod &&
560
+ entry.mode &&
561
+ (st.mode & 0o7777) !== entry.mode
562
+ const afterChmod = er => this[MAKEFS](er, entry, done)
563
+ if (!needChmod)
564
+ return afterChmod()
565
+ return fs.chmod(entry.absolute, entry.mode, afterChmod)
566
+ }
567
+ // not a dir entry, have to remove it.
568
+ if (entry.absolute !== this.cwd) {
569
+ return fs.rmdir(entry.absolute, er =>
570
+ this[MAKEFS](er, entry, done))
571
+ }
572
+ }
573
+
574
+ // not a dir, and not reusable
575
+ // don't remove if the cwd, we want that error
576
+ if (entry.absolute === this.cwd)
577
+ return this[MAKEFS](null, entry, done)
578
+
579
+ unlinkFile(entry.absolute, er =>
580
+ this[MAKEFS](er, entry, done))
529
581
  })
530
- })
582
+ }
583
+
584
+ if (this[CHECKED_CWD])
585
+ start()
586
+ else
587
+ checkCwd()
531
588
  }
532
589
 
533
590
  [MAKEFS] (er, entry, done) {
@@ -556,7 +613,7 @@ class Unpack extends Parser {
556
613
  }
557
614
 
558
615
  [LINK] (entry, linkpath, link, done) {
559
- // XXX: get the type ('file' or 'dir') for windows
616
+ // XXX: get the type ('symlink' or 'junction') for windows
560
617
  fs[link](linkpath, entry.absolute, er => {
561
618
  if (er)
562
619
  this[ONERROR](er, entry)
@@ -569,44 +626,67 @@ class Unpack extends Parser {
569
626
  }
570
627
  }
571
628
 
629
+ const callSync = fn => {
630
+ try {
631
+ return [null, fn()]
632
+ } catch (er) {
633
+ return [er, null]
634
+ }
635
+ }
572
636
  class UnpackSync extends Unpack {
637
+ [MAKEFS] (er, entry) {
638
+ return super[MAKEFS](er, entry, () => {})
639
+ }
640
+
573
641
  [CHECKFS] (entry) {
574
- if (entry.type !== 'Directory') {
575
- for (const path of this.dirCache.keys()) {
576
- if (path === entry.absolute ||
577
- path.indexOf(entry.absolute + '/') === 0 ||
578
- path.indexOf(entry.absolute + '\\') === 0)
579
- this.dirCache.delete(path)
642
+ if (entry.type !== 'Directory')
643
+ pruneCache(this.dirCache, entry.absolute)
644
+
645
+ if (!this[CHECKED_CWD]) {
646
+ const er = this[MKDIR](this.cwd, this.dmode)
647
+ if (er)
648
+ return this[ONERROR](er, entry)
649
+ this[CHECKED_CWD] = true
650
+ }
651
+
652
+ // don't bother to make the parent if the current entry is the cwd,
653
+ // we've already checked it.
654
+ if (entry.absolute !== this.cwd) {
655
+ const parent = normPath(path.dirname(entry.absolute))
656
+ if (parent !== this.cwd) {
657
+ const mkParent = this[MKDIR](parent, this.dmode)
658
+ if (mkParent)
659
+ return this[ONERROR](mkParent, entry)
580
660
  }
581
661
  }
582
662
 
583
- const er = this[MKDIR](path.dirname(entry.absolute), this.dmode, neverCalled)
584
- if (er)
585
- return this[ONERROR](er, entry)
586
- try {
587
- const st = fs.lstatSync(entry.absolute)
588
- if (this.keep || this.newer && st.mtime > entry.mtime)
589
- return this[SKIP](entry)
590
- else if (this[ISREUSABLE](entry, st))
591
- return this[MAKEFS](null, entry, neverCalled)
592
- else {
593
- try {
594
- if (st.isDirectory()) {
595
- if (entry.type === 'Directory') {
596
- if (!this.noChmod && entry.mode && (st.mode & 0o7777) !== entry.mode)
597
- fs.chmodSync(entry.absolute, entry.mode)
598
- } else
599
- fs.rmdirSync(entry.absolute)
600
- } else
601
- unlinkFileSync(entry.absolute)
602
- return this[MAKEFS](null, entry, neverCalled)
603
- } catch (er) {
604
- return this[ONERROR](er, entry)
605
- }
663
+ const [lstatEr, st] = callSync(() => fs.lstatSync(entry.absolute))
664
+ if (st && (this.keep || this.newer && st.mtime > entry.mtime))
665
+ return this[SKIP](entry)
666
+
667
+ if (lstatEr || this[ISREUSABLE](entry, st))
668
+ return this[MAKEFS](null, entry)
669
+
670
+ if (st.isDirectory()) {
671
+ if (entry.type === 'Directory') {
672
+ const needChmod = !this.noChmod &&
673
+ entry.mode &&
674
+ (st.mode & 0o7777) !== entry.mode
675
+ const [er] = needChmod ? callSync(() => {
676
+ fs.chmodSync(entry.absolute, entry.mode)
677
+ }) : []
678
+ return this[MAKEFS](er, entry)
606
679
  }
607
- } catch (er) {
608
- return this[MAKEFS](null, entry, neverCalled)
680
+ // not a dir entry, have to remove it
681
+ const [er] = callSync(() => fs.rmdirSync(entry.absolute))
682
+ this[MAKEFS](er, entry)
609
683
  }
684
+
685
+ // not a dir, and not reusable.
686
+ // don't remove if it's the cwd, since we want that error.
687
+ const [er] = entry.absolute === this.cwd ? []
688
+ : callSync(() => unlinkFileSync(entry.absolute))
689
+ this[MAKEFS](er, entry)
610
690
  }
611
691
 
612
692
  [FILE] (entry, _) {
@@ -700,7 +780,7 @@ class UnpackSync extends Unpack {
700
780
 
701
781
  [MKDIR] (dir, mode) {
702
782
  try {
703
- return mkdir.sync(dir, {
783
+ return mkdir.sync(normPath(dir), {
704
784
  uid: this.uid,
705
785
  gid: this.gid,
706
786
  processUid: this.processUid,
@@ -4,12 +4,14 @@ const Pax = require('./pax.js')
4
4
  const Header = require('./header.js')
5
5
  const fs = require('fs')
6
6
  const path = require('path')
7
+ const normPath = require('./normalize-windows-path.js')
8
+ const stripSlash = require('./strip-trailing-slashes.js')
7
9
 
8
10
  const prefixPath = (path, prefix) => {
9
11
  if (!prefix)
10
- return path
11
- path = path.replace(/^\.([/\\]|$)/, '')
12
- return prefix + '/' + path
12
+ return normPath(path)
13
+ path = normPath(path).replace(/^\.(\/|$)/, '')
14
+ return stripSlash(prefix) + '/' + path
13
15
  }
14
16
 
15
17
  const maxReadSize = 16 * 1024 * 1024
@@ -31,6 +33,7 @@ const MODE = Symbol('mode')
31
33
  const AWAITDRAIN = Symbol('awaitDrain')
32
34
  const ONDRAIN = Symbol('ondrain')
33
35
  const PREFIX = Symbol('prefix')
36
+ const HAD_ERROR = Symbol('hadError')
34
37
  const warner = require('./warn-mixin.js')
35
38
  const winchars = require('./winchars.js')
36
39
  const stripAbsolutePath = require('./strip-absolute-path.js')
@@ -43,22 +46,22 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
43
46
  super(opt)
44
47
  if (typeof p !== 'string')
45
48
  throw new TypeError('path is required')
46
- this.path = p
49
+ this.path = normPath(p)
47
50
  // suppress atime, ctime, uid, gid, uname, gname
48
51
  this.portable = !!opt.portable
49
52
  // until node has builtin pwnam functions, this'll have to do
50
- this.myuid = process.getuid && process.getuid()
53
+ this.myuid = process.getuid && process.getuid() || 0
51
54
  this.myuser = process.env.USER || ''
52
55
  this.maxReadSize = opt.maxReadSize || maxReadSize
53
56
  this.linkCache = opt.linkCache || new Map()
54
57
  this.statCache = opt.statCache || new Map()
55
58
  this.preservePaths = !!opt.preservePaths
56
- this.cwd = opt.cwd || process.cwd()
59
+ this.cwd = normPath(opt.cwd || process.cwd())
57
60
  this.strict = !!opt.strict
58
61
  this.noPax = !!opt.noPax
59
62
  this.noMtime = !!opt.noMtime
60
63
  this.mtime = opt.mtime || null
61
- this.prefix = opt.prefix || null
64
+ this.prefix = opt.prefix ? normPath(opt.prefix) : null
62
65
 
63
66
  this.fd = null
64
67
  this.blockLen = null
@@ -83,11 +86,13 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
83
86
 
84
87
  this.win32 = !!opt.win32 || process.platform === 'win32'
85
88
  if (this.win32) {
89
+ // force the \ to / normalization, since we might not *actually*
90
+ // be on windows, but want \ to be considered a path separator.
86
91
  this.path = winchars.decode(this.path.replace(/\\/g, '/'))
87
92
  p = p.replace(/\\/g, '/')
88
93
  }
89
94
 
90
- this.absolute = opt.absolute || path.resolve(this.cwd, p)
95
+ this.absolute = normPath(opt.absolute || path.resolve(this.cwd, p))
91
96
 
92
97
  if (this.path === '')
93
98
  this.path = './'
@@ -105,6 +110,12 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
105
110
  this[LSTAT]()
106
111
  }
107
112
 
113
+ emit (ev, ...data) {
114
+ if (ev === 'error')
115
+ this[HAD_ERROR] = true
116
+ return super.emit(ev, ...data)
117
+ }
118
+
108
119
  [LSTAT] () {
109
120
  fs.lstat(this.absolute, (er, stat) => {
110
121
  if (er)
@@ -201,14 +212,14 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
201
212
  }
202
213
 
203
214
  [ONREADLINK] (linkpath) {
204
- this.linkpath = linkpath.replace(/\\/g, '/')
215
+ this.linkpath = normPath(linkpath)
205
216
  this[HEADER]()
206
217
  this.end()
207
218
  }
208
219
 
209
220
  [HARDLINK] (linkpath) {
210
221
  this.type = 'Link'
211
- this.linkpath = path.relative(this.cwd, linkpath).replace(/\\/g, '/')
222
+ this.linkpath = normPath(path.relative(this.cwd, linkpath))
212
223
  this.stat.size = 0
213
224
  this[HEADER]()
214
225
  this.end()
@@ -242,6 +253,9 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
242
253
 
243
254
  [ONOPENFILE] (fd) {
244
255
  this.fd = fd
256
+ if (this[HAD_ERROR])
257
+ return this[CLOSE]()
258
+
245
259
  this.blockLen = 512 * Math.ceil(this.stat.size / 512)
246
260
  this.blockRemain = this.blockLen
247
261
  const bufLen = Math.min(this.blockLen, this.maxReadSize)
@@ -403,7 +417,7 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
403
417
 
404
418
  this.prefix = opt.prefix || null
405
419
 
406
- this.path = readEntry.path
420
+ this.path = normPath(readEntry.path)
407
421
  this.mode = this[MODE](readEntry.mode)
408
422
  this.uid = this.portable ? null : readEntry.uid
409
423
  this.gid = this.portable ? null : readEntry.gid
@@ -413,7 +427,7 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
413
427
  this.mtime = this.noMtime ? null : opt.mtime || readEntry.mtime
414
428
  this.atime = this.portable ? null : readEntry.atime
415
429
  this.ctime = this.portable ? null : readEntry.ctime
416
- this.linkpath = readEntry.linkpath
430
+ this.linkpath = normPath(readEntry.linkpath)
417
431
 
418
432
  if (typeof opt.onwarn === 'function')
419
433
  this.on('warn', opt.onwarn)
package/package.json CHANGED
@@ -2,13 +2,15 @@
2
2
  "author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me/)",
3
3
  "name": "tar",
4
4
  "description": "tar for node",
5
- "version": "6.1.6",
5
+ "version": "6.1.7",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/npm/node-tar.git"
9
9
  },
10
10
  "scripts": {
11
- "test": "tap",
11
+ "test:posix": "tap",
12
+ "test:win32": "tap --lines=98 --branches=98 --statements=98 --functions=98",
13
+ "test": "node test/fixtures/test.js",
12
14
  "posttest": "npm run lint",
13
15
  "eslint": "eslint",
14
16
  "lint": "npm run eslint -- test lib",