tar 6.1.3 → 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/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
  module.exports = (opt_, files, cb) => {
11
12
  if (typeof opt_ === 'function')
@@ -41,7 +42,7 @@ 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 => {
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
  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
 
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,10 +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
- if (this.prefix)
138
- p.path = this.prefix + '/' + p.path.replace(/^\.(\/+|$)/, '')
139
-
137
+ const absolute = normPath(path.resolve(this.cwd, p.path))
140
138
  // in this case, we don't have to wait for the stat
141
139
  if (!this.filter(p.path, p))
142
140
  p.resume()
@@ -152,10 +150,7 @@ const Pack = warner(class Pack extends MiniPass {
152
150
  }
153
151
 
154
152
  [ADDFSENTRY] (p) {
155
- const absolute = path.resolve(this.cwd, p)
156
- if (this.prefix)
157
- p = this.prefix + '/' + p.replace(/^\.(\/+|$)/, '')
158
-
153
+ const absolute = normPath(path.resolve(this.cwd, p))
159
154
  this[QUEUE].push(new PackJob(p, absolute))
160
155
  this[PROCESS]()
161
156
  }
@@ -298,6 +293,7 @@ const Pack = warner(class Pack extends MiniPass {
298
293
  statCache: this.statCache,
299
294
  noMtime: this.noMtime,
300
295
  mtime: this.mtime,
296
+ prefix: this.prefix,
301
297
  }
302
298
  }
303
299
 
@@ -323,10 +319,7 @@ const Pack = warner(class Pack extends MiniPass {
323
319
 
324
320
  if (job.readdir) {
325
321
  job.readdir.forEach(entry => {
326
- const p = this.prefix ?
327
- job.path.slice(this.prefix.length + 1) || './'
328
- : job.path
329
-
322
+ const p = job.path
330
323
  const base = p === './' ? '' : p.replace(/\/*$/, '/')
331
324
  this[ADDFSENTRY](base + entry)
332
325
  })
@@ -381,10 +374,7 @@ class PackSync extends Pack {
381
374
 
382
375
  if (job.readdir) {
383
376
  job.readdir.forEach(entry => {
384
- const p = this.prefix ?
385
- job.path.slice(this.prefix.length + 1) || './'
386
- : job.path
387
-
377
+ const p = job.path
388
378
  const base = p === './' ? '' : p.replace(/\/*$/, '/')
389
379
  this[ADDFSENTRY](base + entry)
390
380
  })
@@ -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)
@@ -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
+ }
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,
@@ -335,17 +351,37 @@ class Unpack extends Parser {
335
351
  mode: mode,
336
352
  autoClose: false,
337
353
  })
338
- stream.on('error', er => this[ONERROR](er, entry))
354
+ stream.on('error', er => {
355
+ if (stream.fd)
356
+ fs.close(stream.fd, () => {})
357
+
358
+ // flush all the data out so that we aren't left hanging
359
+ // if the error wasn't actually fatal. otherwise the parse
360
+ // is blocked, and we never proceed.
361
+ stream.write = () => true
362
+ this[ONERROR](er, entry)
363
+ fullyDone()
364
+ })
339
365
 
340
366
  let actions = 1
341
367
  const done = er => {
342
- if (er)
343
- return this[ONERROR](er, entry)
368
+ if (er) {
369
+ /* istanbul ignore else - we should always have a fd by now */
370
+ if (stream.fd)
371
+ fs.close(stream.fd, () => {})
372
+
373
+ this[ONERROR](er, entry)
374
+ fullyDone()
375
+ return
376
+ }
344
377
 
345
378
  if (--actions === 0) {
346
379
  fs.close(stream.fd, er => {
380
+ if (er)
381
+ this[ONERROR](er, entry)
382
+ else
383
+ this[UNPEND]()
347
384
  fullyDone()
348
- er ? this[ONERROR](er, entry) : this[UNPEND]()
349
385
  })
350
386
  }
351
387
  }
@@ -380,7 +416,10 @@ class Unpack extends Parser {
380
416
 
381
417
  const tx = this.transform ? this.transform(entry) || entry : entry
382
418
  if (tx !== entry) {
383
- tx.on('error', er => this[ONERROR](er, entry))
419
+ tx.on('error', er => {
420
+ this[ONERROR](er, entry)
421
+ fullyDone()
422
+ })
384
423
  entry.pipe(tx)
385
424
  }
386
425
  tx.pipe(stream)
@@ -390,8 +429,9 @@ class Unpack extends Parser {
390
429
  const mode = entry.mode & 0o7777 || this.dmode
391
430
  this[MKDIR](entry.absolute, mode, er => {
392
431
  if (er) {
432
+ this[ONERROR](er, entry)
393
433
  fullyDone()
394
- return this[ONERROR](er, entry)
434
+ return
395
435
  }
396
436
 
397
437
  let actions = 1
@@ -429,7 +469,8 @@ class Unpack extends Parser {
429
469
  }
430
470
 
431
471
  [HARDLINK] (entry, done) {
432
- 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)
433
474
  }
434
475
 
435
476
  [PEND] () {
@@ -471,46 +512,87 @@ class Unpack extends Parser {
471
512
  // then that means we are about to delete the directory we created
472
513
  // previously, and it is no longer going to be a directory, and neither
473
514
  // is any of its children.
474
- if (entry.type !== 'Directory') {
475
- for (const path of this.dirCache.keys()) {
476
- if (path === entry.absolute ||
477
- path.indexOf(entry.absolute + '/') === 0 ||
478
- path.indexOf(entry.absolute + '\\') === 0)
479
- this.dirCache.delete(path)
480
- }
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
+ })
481
528
  }
482
529
 
483
- this[MKDIR](path.dirname(entry.absolute), this.dmode, er => {
484
- if (er) {
485
- done()
486
- return this[ONERROR](er, entry)
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
+ }
487
543
  }
488
- fs.lstat(entry.absolute, (er, st) => {
544
+ afterMakeParent()
545
+ }
546
+
547
+ const afterMakeParent = () => {
548
+ fs.lstat(entry.absolute, (lstatEr, st) => {
489
549
  if (st && (this.keep || this.newer && st.mtime > entry.mtime)) {
490
550
  this[SKIP](entry)
491
551
  done()
492
- } else if (er || this[ISREUSABLE](entry, st))
493
- this[MAKEFS](null, entry, done)
552
+ return
553
+ }
554
+ if (lstatEr || this[ISREUSABLE](entry, st))
555
+ return this[MAKEFS](null, entry, done)
494
556
 
495
- else if (st.isDirectory()) {
557
+ if (st.isDirectory()) {
496
558
  if (entry.type === 'Directory') {
497
- if (!this.noChmod && (!entry.mode || (st.mode & 0o7777) === entry.mode))
498
- this[MAKEFS](null, entry, done)
499
- else {
500
- fs.chmod(entry.absolute, entry.mode,
501
- er => this[MAKEFS](er, entry, done))
502
- }
503
- } else
504
- fs.rmdir(entry.absolute, er => this[MAKEFS](er, entry, done))
505
- } else
506
- 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))
507
581
  })
508
- })
582
+ }
583
+
584
+ if (this[CHECKED_CWD])
585
+ start()
586
+ else
587
+ checkCwd()
509
588
  }
510
589
 
511
590
  [MAKEFS] (er, entry, done) {
512
- if (er)
513
- return this[ONERROR](er, entry)
591
+ if (er) {
592
+ this[ONERROR](er, entry)
593
+ done()
594
+ return
595
+ }
514
596
 
515
597
  switch (entry.type) {
516
598
  case 'File':
@@ -531,55 +613,80 @@ class Unpack extends Parser {
531
613
  }
532
614
 
533
615
  [LINK] (entry, linkpath, link, done) {
534
- // XXX: get the type ('file' or 'dir') for windows
616
+ // XXX: get the type ('symlink' or 'junction') for windows
535
617
  fs[link](linkpath, entry.absolute, er => {
536
618
  if (er)
537
- return this[ONERROR](er, entry)
619
+ this[ONERROR](er, entry)
620
+ else {
621
+ this[UNPEND]()
622
+ entry.resume()
623
+ }
538
624
  done()
539
- this[UNPEND]()
540
- entry.resume()
541
625
  })
542
626
  }
543
627
  }
544
628
 
629
+ const callSync = fn => {
630
+ try {
631
+ return [null, fn()]
632
+ } catch (er) {
633
+ return [er, null]
634
+ }
635
+ }
545
636
  class UnpackSync extends Unpack {
637
+ [MAKEFS] (er, entry) {
638
+ return super[MAKEFS](er, entry, () => {})
639
+ }
640
+
546
641
  [CHECKFS] (entry) {
547
- if (entry.type !== 'Directory') {
548
- for (const path of this.dirCache.keys()) {
549
- if (path === entry.absolute ||
550
- path.indexOf(entry.absolute + '/') === 0 ||
551
- path.indexOf(entry.absolute + '\\') === 0)
552
- 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)
553
660
  }
554
661
  }
555
662
 
556
- const er = this[MKDIR](path.dirname(entry.absolute), this.dmode, neverCalled)
557
- if (er)
558
- return this[ONERROR](er, entry)
559
- try {
560
- const st = fs.lstatSync(entry.absolute)
561
- if (this.keep || this.newer && st.mtime > entry.mtime)
562
- return this[SKIP](entry)
563
- else if (this[ISREUSABLE](entry, st))
564
- return this[MAKEFS](null, entry, neverCalled)
565
- else {
566
- try {
567
- if (st.isDirectory()) {
568
- if (entry.type === 'Directory') {
569
- if (!this.noChmod && entry.mode && (st.mode & 0o7777) !== entry.mode)
570
- fs.chmodSync(entry.absolute, entry.mode)
571
- } else
572
- fs.rmdirSync(entry.absolute)
573
- } else
574
- unlinkFileSync(entry.absolute)
575
- return this[MAKEFS](null, entry, neverCalled)
576
- } catch (er) {
577
- return this[ONERROR](er, entry)
578
- }
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)
579
679
  }
580
- } catch (er) {
581
- 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)
582
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)
583
690
  }
584
691
 
585
692
  [FILE] (entry, _) {
@@ -673,7 +780,7 @@ class UnpackSync extends Unpack {
673
780
 
674
781
  [MKDIR] (dir, mode) {
675
782
  try {
676
- return mkdir.sync(dir, {
783
+ return mkdir.sync(normPath(dir), {
677
784
  uid: this.uid,
678
785
  gid: this.gid,
679
786
  processUid: this.processUid,
@@ -4,6 +4,15 @@ 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')
9
+
10
+ const prefixPath = (path, prefix) => {
11
+ if (!prefix)
12
+ return normPath(path)
13
+ path = normPath(path).replace(/^\.(\/|$)/, '')
14
+ return stripSlash(prefix) + '/' + path
15
+ }
7
16
 
8
17
  const maxReadSize = 16 * 1024 * 1024
9
18
  const PROCESS = Symbol('process')
@@ -21,6 +30,10 @@ const OPENFILE = Symbol('openfile')
21
30
  const ONOPENFILE = Symbol('onopenfile')
22
31
  const CLOSE = Symbol('close')
23
32
  const MODE = Symbol('mode')
33
+ const AWAITDRAIN = Symbol('awaitDrain')
34
+ const ONDRAIN = Symbol('ondrain')
35
+ const PREFIX = Symbol('prefix')
36
+ const HAD_ERROR = Symbol('hadError')
24
37
  const warner = require('./warn-mixin.js')
25
38
  const winchars = require('./winchars.js')
26
39
  const stripAbsolutePath = require('./strip-absolute-path.js')
@@ -33,21 +46,31 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
33
46
  super(opt)
34
47
  if (typeof p !== 'string')
35
48
  throw new TypeError('path is required')
36
- this.path = p
49
+ this.path = normPath(p)
37
50
  // suppress atime, ctime, uid, gid, uname, gname
38
51
  this.portable = !!opt.portable
39
52
  // until node has builtin pwnam functions, this'll have to do
40
- this.myuid = process.getuid && process.getuid()
53
+ this.myuid = process.getuid && process.getuid() || 0
41
54
  this.myuser = process.env.USER || ''
42
55
  this.maxReadSize = opt.maxReadSize || maxReadSize
43
56
  this.linkCache = opt.linkCache || new Map()
44
57
  this.statCache = opt.statCache || new Map()
45
58
  this.preservePaths = !!opt.preservePaths
46
- this.cwd = opt.cwd || process.cwd()
59
+ this.cwd = normPath(opt.cwd || process.cwd())
47
60
  this.strict = !!opt.strict
48
61
  this.noPax = !!opt.noPax
49
62
  this.noMtime = !!opt.noMtime
50
63
  this.mtime = opt.mtime || null
64
+ this.prefix = opt.prefix ? normPath(opt.prefix) : null
65
+
66
+ this.fd = null
67
+ this.blockLen = null
68
+ this.blockRemain = null
69
+ this.buf = null
70
+ this.offset = null
71
+ this.length = null
72
+ this.pos = null
73
+ this.remain = null
51
74
 
52
75
  if (typeof opt.onwarn === 'function')
53
76
  this.on('warn', opt.onwarn)
@@ -63,11 +86,13 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
63
86
 
64
87
  this.win32 = !!opt.win32 || process.platform === 'win32'
65
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.
66
91
  this.path = winchars.decode(this.path.replace(/\\/g, '/'))
67
92
  p = p.replace(/\\/g, '/')
68
93
  }
69
94
 
70
- this.absolute = opt.absolute || path.resolve(this.cwd, p)
95
+ this.absolute = normPath(opt.absolute || path.resolve(this.cwd, p))
71
96
 
72
97
  if (this.path === '')
73
98
  this.path = './'
@@ -85,6 +110,12 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
85
110
  this[LSTAT]()
86
111
  }
87
112
 
113
+ emit (ev, ...data) {
114
+ if (ev === 'error')
115
+ this[HAD_ERROR] = true
116
+ return super.emit(ev, ...data)
117
+ }
118
+
88
119
  [LSTAT] () {
89
120
  fs.lstat(this.absolute, (er, stat) => {
90
121
  if (er)
@@ -117,13 +148,19 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
117
148
  return modeFix(mode, this.type === 'Directory', this.portable)
118
149
  }
119
150
 
151
+ [PREFIX] (path) {
152
+ return prefixPath(path, this.prefix)
153
+ }
154
+
120
155
  [HEADER] () {
121
156
  if (this.type === 'Directory' && this.portable)
122
157
  this.noMtime = true
123
158
 
124
159
  this.header = new Header({
125
- path: this.path,
126
- linkpath: this.linkpath,
160
+ path: this[PREFIX](this.path),
161
+ // only apply the prefix to hard links.
162
+ linkpath: this.type === 'Link' ? this[PREFIX](this.linkpath)
163
+ : this.linkpath,
127
164
  // only the permissions and setuid/setgid/sticky bitflags
128
165
  // not the higher-order bits that specify file type
129
166
  mode: this[MODE](this.stat.mode),
@@ -139,13 +176,14 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
139
176
  })
140
177
 
141
178
  if (this.header.encode() && !this.noPax) {
142
- this.write(new Pax({
179
+ super.write(new Pax({
143
180
  atime: this.portable ? null : this.header.atime,
144
181
  ctime: this.portable ? null : this.header.ctime,
145
182
  gid: this.portable ? null : this.header.gid,
146
183
  mtime: this.noMtime ? null : this.mtime || this.header.mtime,
147
- path: this.path,
148
- linkpath: this.linkpath,
184
+ path: this[PREFIX](this.path),
185
+ linkpath: this.type === 'Link' ? this[PREFIX](this.linkpath)
186
+ : this.linkpath,
149
187
  size: this.header.size,
150
188
  uid: this.portable ? null : this.header.uid,
151
189
  uname: this.portable ? null : this.header.uname,
@@ -154,7 +192,7 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
154
192
  nlink: this.portable ? null : this.stat.nlink,
155
193
  }).encode())
156
194
  }
157
- this.write(this.header.block)
195
+ super.write(this.header.block)
158
196
  }
159
197
 
160
198
  [DIRECTORY] () {
@@ -174,14 +212,14 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
174
212
  }
175
213
 
176
214
  [ONREADLINK] (linkpath) {
177
- this.linkpath = linkpath.replace(/\\/g, '/')
215
+ this.linkpath = normPath(linkpath)
178
216
  this[HEADER]()
179
217
  this.end()
180
218
  }
181
219
 
182
220
  [HARDLINK] (linkpath) {
183
221
  this.type = 'Link'
184
- this.linkpath = path.relative(this.cwd, linkpath).replace(/\\/g, '/')
222
+ this.linkpath = normPath(path.relative(this.cwd, linkpath))
185
223
  this.stat.size = 0
186
224
  this[HEADER]()
187
225
  this.end()
@@ -214,74 +252,110 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
214
252
  }
215
253
 
216
254
  [ONOPENFILE] (fd) {
217
- const blockLen = 512 * Math.ceil(this.stat.size / 512)
218
- const bufLen = Math.min(blockLen, this.maxReadSize)
219
- const buf = Buffer.allocUnsafe(bufLen)
220
- this[READ](fd, buf, 0, buf.length, 0, this.stat.size, blockLen)
255
+ this.fd = fd
256
+ if (this[HAD_ERROR])
257
+ return this[CLOSE]()
258
+
259
+ this.blockLen = 512 * Math.ceil(this.stat.size / 512)
260
+ this.blockRemain = this.blockLen
261
+ const bufLen = Math.min(this.blockLen, this.maxReadSize)
262
+ this.buf = Buffer.allocUnsafe(bufLen)
263
+ this.offset = 0
264
+ this.pos = 0
265
+ this.remain = this.stat.size
266
+ this.length = this.buf.length
267
+ this[READ]()
221
268
  }
222
269
 
223
- [READ] (fd, buf, offset, length, pos, remain, blockRemain) {
270
+ [READ] () {
271
+ const { fd, buf, offset, length, pos } = this
224
272
  fs.read(fd, buf, offset, length, pos, (er, bytesRead) => {
225
273
  if (er) {
226
274
  // ignoring the error from close(2) is a bad practice, but at
227
275
  // this point we already have an error, don't need another one
228
- return this[CLOSE](fd, () => this.emit('error', er))
276
+ return this[CLOSE](() => this.emit('error', er))
229
277
  }
230
- this[ONREAD](fd, buf, offset, length, pos, remain, blockRemain, bytesRead)
278
+ this[ONREAD](bytesRead)
231
279
  })
232
280
  }
233
281
 
234
- [CLOSE] (fd, cb) {
235
- fs.close(fd, cb)
282
+ [CLOSE] (cb) {
283
+ fs.close(this.fd, cb)
236
284
  }
237
285
 
238
- [ONREAD] (fd, buf, offset, length, pos, remain, blockRemain, bytesRead) {
239
- if (bytesRead <= 0 && remain > 0) {
286
+ [ONREAD] (bytesRead) {
287
+ if (bytesRead <= 0 && this.remain > 0) {
240
288
  const er = new Error('encountered unexpected EOF')
241
289
  er.path = this.absolute
242
290
  er.syscall = 'read'
243
291
  er.code = 'EOF'
244
- return this[CLOSE](fd, () => this.emit('error', er))
292
+ return this[CLOSE](() => this.emit('error', er))
245
293
  }
246
294
 
247
- if (bytesRead > remain) {
295
+ if (bytesRead > this.remain) {
248
296
  const er = new Error('did not encounter expected EOF')
249
297
  er.path = this.absolute
250
298
  er.syscall = 'read'
251
299
  er.code = 'EOF'
252
- return this[CLOSE](fd, () => this.emit('error', er))
300
+ return this[CLOSE](() => this.emit('error', er))
253
301
  }
254
302
 
255
303
  // null out the rest of the buffer, if we could fit the block padding
256
- if (bytesRead === remain) {
257
- for (let i = bytesRead; i < length && bytesRead < blockRemain; i++) {
258
- buf[i + offset] = 0
304
+ // at the end of this loop, we've incremented bytesRead and this.remain
305
+ // to be incremented up to the blockRemain level, as if we had expected
306
+ // to get a null-padded file, and read it until the end. then we will
307
+ // decrement both remain and blockRemain by bytesRead, and know that we
308
+ // reached the expected EOF, without any null buffer to append.
309
+ if (bytesRead === this.remain) {
310
+ for (let i = bytesRead; i < this.length && bytesRead < this.blockRemain; i++) {
311
+ this.buf[i + this.offset] = 0
259
312
  bytesRead++
260
- remain++
313
+ this.remain++
261
314
  }
262
315
  }
263
316
 
264
- const writeBuf = offset === 0 && bytesRead === buf.length ?
265
- buf : buf.slice(offset, offset + bytesRead)
266
- remain -= bytesRead
267
- blockRemain -= bytesRead
268
- pos += bytesRead
269
- offset += bytesRead
317
+ const writeBuf = this.offset === 0 && bytesRead === this.buf.length ?
318
+ this.buf : this.buf.slice(this.offset, this.offset + bytesRead)
319
+
320
+ const flushed = this.write(writeBuf)
321
+ if (!flushed)
322
+ this[AWAITDRAIN](() => this[ONDRAIN]())
323
+ else
324
+ this[ONDRAIN]()
325
+ }
326
+
327
+ [AWAITDRAIN] (cb) {
328
+ this.once('drain', cb)
329
+ }
270
330
 
271
- this.write(writeBuf)
331
+ write (writeBuf) {
332
+ if (this.blockRemain < writeBuf.length) {
333
+ const er = new Error('writing more data than expected')
334
+ er.path = this.absolute
335
+ return this.emit('error', er)
336
+ }
337
+ this.remain -= writeBuf.length
338
+ this.blockRemain -= writeBuf.length
339
+ this.pos += writeBuf.length
340
+ this.offset += writeBuf.length
341
+ return super.write(writeBuf)
342
+ }
272
343
 
273
- if (!remain) {
274
- if (blockRemain)
275
- this.write(Buffer.alloc(blockRemain))
276
- return this[CLOSE](fd, er => er ? this.emit('error', er) : this.end())
344
+ [ONDRAIN] () {
345
+ if (!this.remain) {
346
+ if (this.blockRemain)
347
+ super.write(Buffer.alloc(this.blockRemain))
348
+ return this[CLOSE](er => er ? this.emit('error', er) : this.end())
277
349
  }
278
350
 
279
- if (offset >= length) {
280
- buf = Buffer.allocUnsafe(length)
281
- offset = 0
351
+ if (this.offset >= this.length) {
352
+ // if we only have a smaller bit left to read, alloc a smaller buffer
353
+ // otherwise, keep it the same length it was before.
354
+ this.buf = Buffer.allocUnsafe(Math.min(this.blockRemain, this.buf.length))
355
+ this.offset = 0
282
356
  }
283
- length = buf.length - offset
284
- this[READ](fd, buf, offset, length, pos, remain, blockRemain)
357
+ this.length = this.buf.length - this.offset
358
+ this[READ]()
285
359
  }
286
360
  })
287
361
 
@@ -298,25 +372,30 @@ class WriteEntrySync extends WriteEntry {
298
372
  this[ONOPENFILE](fs.openSync(this.absolute, 'r'))
299
373
  }
300
374
 
301
- [READ] (fd, buf, offset, length, pos, remain, blockRemain) {
375
+ [READ] () {
302
376
  let threw = true
303
377
  try {
378
+ const { fd, buf, offset, length, pos } = this
304
379
  const bytesRead = fs.readSync(fd, buf, offset, length, pos)
305
- this[ONREAD](fd, buf, offset, length, pos, remain, blockRemain, bytesRead)
380
+ this[ONREAD](bytesRead)
306
381
  threw = false
307
382
  } finally {
308
383
  // ignoring the error from close(2) is a bad practice, but at
309
384
  // this point we already have an error, don't need another one
310
385
  if (threw) {
311
386
  try {
312
- this[CLOSE](fd, () => {})
387
+ this[CLOSE](() => {})
313
388
  } catch (er) {}
314
389
  }
315
390
  }
316
391
  }
317
392
 
318
- [CLOSE] (fd, cb) {
319
- fs.closeSync(fd)
393
+ [AWAITDRAIN] (cb) {
394
+ cb()
395
+ }
396
+
397
+ [CLOSE] (cb) {
398
+ fs.closeSync(this.fd)
320
399
  cb()
321
400
  }
322
401
  }
@@ -336,7 +415,9 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
336
415
  if (this.type === 'Directory' && this.portable)
337
416
  this.noMtime = true
338
417
 
339
- this.path = readEntry.path
418
+ this.prefix = opt.prefix || null
419
+
420
+ this.path = normPath(readEntry.path)
340
421
  this.mode = this[MODE](readEntry.mode)
341
422
  this.uid = this.portable ? null : readEntry.uid
342
423
  this.gid = this.portable ? null : readEntry.gid
@@ -346,7 +427,7 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
346
427
  this.mtime = this.noMtime ? null : opt.mtime || readEntry.mtime
347
428
  this.atime = this.portable ? null : readEntry.atime
348
429
  this.ctime = this.portable ? null : readEntry.ctime
349
- this.linkpath = readEntry.linkpath
430
+ this.linkpath = normPath(readEntry.linkpath)
350
431
 
351
432
  if (typeof opt.onwarn === 'function')
352
433
  this.on('warn', opt.onwarn)
@@ -364,8 +445,9 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
364
445
  this.blockRemain = readEntry.startBlockSize
365
446
 
366
447
  this.header = new Header({
367
- path: this.path,
368
- linkpath: this.linkpath,
448
+ path: this[PREFIX](this.path),
449
+ linkpath: this.type === 'Link' ? this[PREFIX](this.linkpath)
450
+ : this.linkpath,
369
451
  // only the permissions and setuid/setgid/sticky bitflags
370
452
  // not the higher-order bits that specify file type
371
453
  mode: this.mode,
@@ -392,8 +474,9 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
392
474
  ctime: this.portable ? null : this.ctime,
393
475
  gid: this.portable ? null : this.gid,
394
476
  mtime: this.noMtime ? null : this.mtime,
395
- path: this.path,
396
- linkpath: this.linkpath,
477
+ path: this[PREFIX](this.path),
478
+ linkpath: this.type === 'Link' ? this[PREFIX](this.linkpath)
479
+ : this.linkpath,
397
480
  size: this.size,
398
481
  uid: this.portable ? null : this.uid,
399
482
  uname: this.portable ? null : this.uname,
@@ -407,6 +490,10 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
407
490
  readEntry.pipe(this)
408
491
  }
409
492
 
493
+ [PREFIX] (path) {
494
+ return prefixPath(path, this.prefix)
495
+ }
496
+
410
497
  [MODE] (mode) {
411
498
  return modeFix(mode, this.type === 'Directory', this.portable)
412
499
  }
@@ -421,7 +508,7 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
421
508
 
422
509
  end () {
423
510
  if (this.blockRemain)
424
- this.write(Buffer.alloc(this.blockRemain))
511
+ super.write(Buffer.alloc(this.blockRemain))
425
512
  return super.end()
426
513
  }
427
514
  })
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.3",
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",