tar 5.0.7 → 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 => {
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()
@@ -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
  }
@@ -297,7 +292,8 @@ const Pack = warner(class Pack extends MiniPass {
297
292
  linkCache: this.linkCache,
298
293
  statCache: this.statCache,
299
294
  noMtime: this.noMtime,
300
- mtime: this.mtime
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
  })
@@ -379,10 +372,7 @@ class PackSync extends Pack {
379
372
 
380
373
  if (job.readdir)
381
374
  job.readdir.forEach(entry => {
382
- const p = this.prefix ?
383
- job.path.slice(this.prefix.length + 1) || './'
384
- : job.path
385
-
375
+ const p = job.path
386
376
  const base = p === './' ? '' : p.replace(/\/*$/, '/')
387
377
  this[ADDFSENTRY](base + entry)
388
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,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
@@ -17,6 +17,7 @@ const mkdirSync = mkdir.sync
17
17
  const wc = require('./winchars.js')
18
18
  const pathReservations = require('./path-reservations.js')
19
19
  const stripAbsolutePath = require('./strip-absolute-path.js')
20
+ const normPath = require('./normalize-windows-path.js')
20
21
 
21
22
  const ONENTRY = Symbol('onEntry')
22
23
  const CHECKFS = Symbol('checkFs')
@@ -42,14 +43,10 @@ const SKIP = Symbol('skip')
42
43
  const DOCHOWN = Symbol('doChown')
43
44
  const UID = Symbol('uid')
44
45
  const GID = Symbol('gid')
46
+ const CHECKED_CWD = Symbol('checkedCwd')
45
47
  const crypto = require('crypto')
46
48
  const getFlag = require('./get-write-flag.js')
47
49
 
48
- /* istanbul ignore next */
49
- const neverCalled = () => {
50
- throw new Error('sync function called cb somehow?!?')
51
- }
52
-
53
50
  // Unlinks on Windows are not atomic.
54
51
  //
55
52
  // This means that if you have a file entry, followed by another
@@ -94,6 +91,17 @@ const uint32 = (a, b, c) =>
94
91
  : b === b >>> 0 ? b
95
92
  : c
96
93
 
94
+ const pruneCache = (cache, abs) => {
95
+ // clear the cache if it's a case-insensitive match, since we can't
96
+ // know if the current file system is case-sensitive or not.
97
+ abs = normPath(abs).toLowerCase()
98
+ for (const path of cache.keys()) {
99
+ const plower = path.toLowerCase()
100
+ if (plower === abs || plower.toLowerCase().indexOf(abs + '/') === 0)
101
+ cache.delete(path)
102
+ }
103
+ }
104
+
97
105
  class Unpack extends Parser {
98
106
  constructor (opt) {
99
107
  if (!opt)
@@ -106,6 +114,8 @@ class Unpack extends Parser {
106
114
 
107
115
  super(opt)
108
116
 
117
+ this[CHECKED_CWD] = false
118
+
109
119
  this.reservations = pathReservations()
110
120
 
111
121
  this.transform = typeof opt.transform === 'function' ? opt.transform : null
@@ -170,7 +180,7 @@ class Unpack extends Parser {
170
180
  // links, and removes symlink directories rather than erroring
171
181
  this.unlink = !!opt.unlink
172
182
 
173
- this.cwd = path.resolve(opt.cwd || process.cwd())
183
+ this.cwd = normPath(path.resolve(opt.cwd || process.cwd()))
174
184
  this.strip = +opt.strip || 0
175
185
  this.processUmask = process.umask()
176
186
  this.umask = typeof opt.umask === 'number' ? opt.umask : this.processUmask
@@ -200,21 +210,21 @@ class Unpack extends Parser {
200
210
 
201
211
  [CHECKPATH] (entry) {
202
212
  if (this.strip) {
203
- const parts = entry.path.split(/\/|\\/)
213
+ const parts = normPath(entry.path).split('/')
204
214
  if (parts.length < this.strip)
205
215
  return false
206
216
  entry.path = parts.slice(this.strip).join('/')
207
217
 
208
218
  if (entry.type === 'Link') {
209
- const linkparts = entry.linkpath.split(/\/|\\/)
219
+ const linkparts = normPath(entry.linkpath).split('/')
210
220
  if (linkparts.length >= this.strip)
211
221
  entry.linkpath = linkparts.slice(this.strip).join('/')
212
222
  }
213
223
  }
214
224
 
215
225
  if (!this.preservePaths) {
216
- const p = entry.path
217
- if (p.match(/(^|\/|\\)\.\.(\\|\/|$)/)) {
226
+ const p = normPath(entry.path)
227
+ if (p.split('/').includes('..')) {
218
228
  this.warn('TAR_ENTRY_ERROR', `path contains '..'`, {
219
229
  entry,
220
230
  path: p,
@@ -234,18 +244,26 @@ class Unpack extends Parser {
234
244
  }
235
245
  }
236
246
 
247
+ if (path.isAbsolute(entry.path))
248
+ entry.absolute = normPath(path.resolve(entry.path))
249
+ else
250
+ entry.absolute = normPath(path.resolve(this.cwd, entry.path))
251
+
252
+ // an archive can set properties on the extraction directory, but it
253
+ // may not replace the cwd with a different kind of thing entirely.
254
+ if (entry.absolute === this.cwd &&
255
+ entry.type !== 'Directory' &&
256
+ entry.type !== 'GNUDumpDir')
257
+ return false
258
+
237
259
  // only encode : chars that aren't drive letter indicators
238
260
  if (this.win32) {
239
- const parsed = path.win32.parse(entry.path)
240
- entry.path = parsed.root === '' ? wc.encode(entry.path)
241
- : parsed.root + wc.encode(entry.path.substr(parsed.root.length))
261
+ const { root: aRoot } = path.win32.parse(entry.absolute)
262
+ entry.absolute = aRoot + wc.encode(entry.absolute.substr(aRoot.length))
263
+ const { root: pRoot } = path.win32.parse(entry.path)
264
+ entry.path = pRoot + wc.encode(entry.path.substr(pRoot.length))
242
265
  }
243
266
 
244
- if (path.isAbsolute(entry.path))
245
- entry.absolute = entry.path
246
- else
247
- entry.absolute = path.resolve(this.cwd, entry.path)
248
-
249
267
  return true
250
268
  }
251
269
 
@@ -289,7 +307,7 @@ class Unpack extends Parser {
289
307
  }
290
308
 
291
309
  [MKDIR] (dir, mode, cb) {
292
- mkdir(dir, {
310
+ mkdir(normPath(dir), {
293
311
  uid: this.uid,
294
312
  gid: this.gid,
295
313
  processUid: this.processUid,
@@ -330,17 +348,37 @@ class Unpack extends Parser {
330
348
  mode: mode,
331
349
  autoClose: false
332
350
  })
333
- stream.on('error', er => this[ONERROR](er, entry))
351
+ stream.on('error', er => {
352
+ if (stream.fd)
353
+ fs.close(stream.fd, () => {})
354
+
355
+ // flush all the data out so that we aren't left hanging
356
+ // if the error wasn't actually fatal. otherwise the parse
357
+ // is blocked, and we never proceed.
358
+ stream.write = () => true
359
+ this[ONERROR](er, entry)
360
+ fullyDone()
361
+ })
334
362
 
335
363
  let actions = 1
336
364
  const done = er => {
337
- if (er)
338
- return this[ONERROR](er, entry)
365
+ if (er) {
366
+ /* istanbul ignore else - we should always have a fd by now */
367
+ if (stream.fd)
368
+ fs.close(stream.fd, () => {})
369
+
370
+ this[ONERROR](er, entry)
371
+ fullyDone()
372
+ return
373
+ }
339
374
 
340
375
  if (--actions === 0) {
341
376
  fs.close(stream.fd, er => {
377
+ if (er)
378
+ this[ONERROR](er, entry)
379
+ else
380
+ this[UNPEND]()
342
381
  fullyDone()
343
- er ? this[ONERROR](er, entry) : this[UNPEND]()
344
382
  })
345
383
  }
346
384
  }
@@ -375,7 +413,10 @@ class Unpack extends Parser {
375
413
 
376
414
  const tx = this.transform ? this.transform(entry) || entry : entry
377
415
  if (tx !== entry) {
378
- tx.on('error', er => this[ONERROR](er, entry))
416
+ tx.on('error', er => {
417
+ this[ONERROR](er, entry)
418
+ fullyDone()
419
+ })
379
420
  entry.pipe(tx)
380
421
  }
381
422
  tx.pipe(stream)
@@ -385,8 +426,9 @@ class Unpack extends Parser {
385
426
  const mode = entry.mode & 0o7777 || this.dmode
386
427
  this[MKDIR](entry.absolute, mode, er => {
387
428
  if (er) {
429
+ this[ONERROR](er, entry)
388
430
  fullyDone()
389
- return this[ONERROR](er, entry)
431
+ return
390
432
  }
391
433
 
392
434
  let actions = 1
@@ -424,7 +466,8 @@ class Unpack extends Parser {
424
466
  }
425
467
 
426
468
  [HARDLINK] (entry, done) {
427
- this[LINK](entry, path.resolve(this.cwd, entry.linkpath), 'link', done)
469
+ const linkpath = normPath(path.resolve(this.cwd, entry.linkpath))
470
+ this[LINK](entry, linkpath, 'link', done)
428
471
  }
429
472
 
430
473
  [PEND] () {
@@ -465,45 +508,87 @@ class Unpack extends Parser {
465
508
  // then that means we are about to delete the directory we created
466
509
  // previously, and it is no longer going to be a directory, and neither
467
510
  // is any of its children.
468
- if (entry.type !== 'Directory') {
469
- for (const path of this.dirCache.keys()) {
470
- if (path === entry.absolute ||
471
- path.indexOf(entry.absolute + '/') === 0 ||
472
- path.indexOf(entry.absolute + '\\') === 0)
473
- this.dirCache.delete(path)
474
- }
511
+ if (entry.type !== 'Directory')
512
+ pruneCache(this.dirCache, entry.absolute)
513
+
514
+ const checkCwd = () => {
515
+ this[MKDIR](this.cwd, this.dmode, er => {
516
+ if (er) {
517
+ this[ONERROR](er, entry)
518
+ done()
519
+ return
520
+ }
521
+ this[CHECKED_CWD] = true
522
+ start()
523
+ })
475
524
  }
476
525
 
477
- this[MKDIR](path.dirname(entry.absolute), this.dmode, er => {
478
- if (er) {
479
- done()
480
- return this[ONERROR](er, entry)
526
+ const start = () => {
527
+ if (entry.absolute !== this.cwd) {
528
+ const parent = normPath(path.dirname(entry.absolute))
529
+ if (parent !== this.cwd) {
530
+ return this[MKDIR](parent, this.dmode, er => {
531
+ if (er) {
532
+ this[ONERROR](er, entry)
533
+ done()
534
+ return
535
+ }
536
+ afterMakeParent()
537
+ })
538
+ }
481
539
  }
482
- fs.lstat(entry.absolute, (er, st) => {
540
+ afterMakeParent()
541
+ }
542
+
543
+ const afterMakeParent = () => {
544
+ fs.lstat(entry.absolute, (lstatEr, st) => {
483
545
  if (st && (this.keep || this.newer && st.mtime > entry.mtime)) {
484
546
  this[SKIP](entry)
485
547
  done()
486
- } else if (er || this[ISREUSABLE](entry, st)) {
487
- this[MAKEFS](null, entry, done)
548
+ return
488
549
  }
489
- else if (st.isDirectory()) {
550
+ if (lstatEr || this[ISREUSABLE](entry, st))
551
+ return this[MAKEFS](null, entry, done)
552
+
553
+ if (st.isDirectory()) {
490
554
  if (entry.type === 'Directory') {
491
- if (!entry.mode || (st.mode & 0o7777) === entry.mode)
492
- this[MAKEFS](null, entry, done)
493
- else
494
- fs.chmod(entry.absolute, entry.mode,
495
- er => this[MAKEFS](er, entry, done))
496
- } else
497
- fs.rmdir(entry.absolute, er => this[MAKEFS](er, entry, done))
498
- } else
499
- unlinkFile(entry.absolute, er => this[MAKEFS](er, entry, done))
555
+ const needChmod = !this.noChmod &&
556
+ entry.mode &&
557
+ (st.mode & 0o7777) !== entry.mode
558
+ const afterChmod = er => this[MAKEFS](er, entry, done)
559
+ if (!needChmod)
560
+ return afterChmod()
561
+ return fs.chmod(entry.absolute, entry.mode, afterChmod)
562
+ }
563
+ // not a dir entry, have to remove it.
564
+ if (entry.absolute !== this.cwd) {
565
+ return fs.rmdir(entry.absolute, er =>
566
+ this[MAKEFS](er, entry, done))
567
+ }
568
+ }
569
+
570
+ // not a dir, and not reusable
571
+ // don't remove if the cwd, we want that error
572
+ if (entry.absolute === this.cwd)
573
+ return this[MAKEFS](null, entry, done)
574
+
575
+ unlinkFile(entry.absolute, er =>
576
+ this[MAKEFS](er, entry, done))
500
577
  })
501
- })
578
+ }
579
+
580
+ if (this[CHECKED_CWD])
581
+ start()
582
+ else
583
+ checkCwd()
502
584
  }
503
585
 
504
586
  [MAKEFS] (er, entry, done) {
505
- if (er)
506
- return this[ONERROR](er, entry)
587
+ if (er) {
588
+ this[ONERROR](er, entry)
589
+ done()
590
+ return
591
+ }
507
592
 
508
593
  switch (entry.type) {
509
594
  case 'File':
@@ -524,59 +609,80 @@ class Unpack extends Parser {
524
609
  }
525
610
 
526
611
  [LINK] (entry, linkpath, link, done) {
527
- // XXX: get the type ('file' or 'dir') for windows
612
+ // XXX: get the type ('symlink' or 'junction') for windows
528
613
  fs[link](linkpath, entry.absolute, er => {
529
614
  if (er)
530
- return this[ONERROR](er, entry)
615
+ this[ONERROR](er, entry)
616
+ else {
617
+ this[UNPEND]()
618
+ entry.resume()
619
+ }
531
620
  done()
532
- this[UNPEND]()
533
- entry.resume()
534
621
  })
535
622
  }
536
623
  }
537
624
 
625
+ const callSync = fn => {
626
+ try {
627
+ return [null, fn()]
628
+ } catch (er) {
629
+ return [er, null]
630
+ }
631
+ }
538
632
  class UnpackSync extends Unpack {
539
- constructor (opt) {
540
- super(opt)
633
+ [MAKEFS] (er, entry) {
634
+ return super[MAKEFS](er, entry, () => {})
541
635
  }
542
636
 
543
637
  [CHECKFS] (entry) {
544
- if (entry.type !== 'Directory') {
545
- for (const path of this.dirCache.keys()) {
546
- if (path === entry.absolute ||
547
- path.indexOf(entry.absolute + '/') === 0 ||
548
- path.indexOf(entry.absolute + '\\') === 0)
549
- this.dirCache.delete(path)
638
+ if (entry.type !== 'Directory')
639
+ pruneCache(this.dirCache, entry.absolute)
640
+
641
+ if (!this[CHECKED_CWD]) {
642
+ const er = this[MKDIR](this.cwd, this.dmode)
643
+ if (er)
644
+ return this[ONERROR](er, entry)
645
+ this[CHECKED_CWD] = true
646
+ }
647
+
648
+ // don't bother to make the parent if the current entry is the cwd,
649
+ // we've already checked it.
650
+ if (entry.absolute !== this.cwd) {
651
+ const parent = normPath(path.dirname(entry.absolute))
652
+ if (parent !== this.cwd) {
653
+ const mkParent = this[MKDIR](parent, this.dmode)
654
+ if (mkParent)
655
+ return this[ONERROR](mkParent, entry)
550
656
  }
551
657
  }
552
658
 
553
- const er = this[MKDIR](path.dirname(entry.absolute), this.dmode, neverCalled)
554
- if (er)
555
- return this[ONERROR](er, entry)
556
- try {
557
- const st = fs.lstatSync(entry.absolute)
558
- if (this.keep || this.newer && st.mtime > entry.mtime)
559
- return this[SKIP](entry)
560
- else if (this[ISREUSABLE](entry, st))
561
- return this[MAKEFS](null, entry, neverCalled)
562
- else {
563
- try {
564
- if (st.isDirectory()) {
565
- if (entry.type === 'Directory') {
566
- if (entry.mode && (st.mode & 0o7777) !== entry.mode)
567
- fs.chmodSync(entry.absolute, entry.mode)
568
- } else
569
- fs.rmdirSync(entry.absolute)
570
- } else
571
- unlinkFileSync(entry.absolute)
572
- return this[MAKEFS](null, entry, neverCalled)
573
- } catch (er) {
574
- return this[ONERROR](er, entry)
575
- }
659
+ const [lstatEr, st] = callSync(() => fs.lstatSync(entry.absolute))
660
+ if (st && (this.keep || this.newer && st.mtime > entry.mtime))
661
+ return this[SKIP](entry)
662
+
663
+ if (lstatEr || this[ISREUSABLE](entry, st))
664
+ return this[MAKEFS](null, entry)
665
+
666
+ if (st.isDirectory()) {
667
+ if (entry.type === 'Directory') {
668
+ const needChmod = !this.noChmod &&
669
+ entry.mode &&
670
+ (st.mode & 0o7777) !== entry.mode
671
+ const [er] = needChmod ? callSync(() => {
672
+ fs.chmodSync(entry.absolute, entry.mode)
673
+ }) : []
674
+ return this[MAKEFS](er, entry)
576
675
  }
577
- } catch (er) {
578
- return this[MAKEFS](null, entry, neverCalled)
676
+ // not a dir entry, have to remove it
677
+ const [er] = callSync(() => fs.rmdirSync(entry.absolute))
678
+ this[MAKEFS](er, entry)
579
679
  }
680
+
681
+ // not a dir, and not reusable.
682
+ // don't remove if it's the cwd, since we want that error.
683
+ const [er] = entry.absolute === this.cwd ? []
684
+ : callSync(() => unlinkFileSync(entry.absolute))
685
+ this[MAKEFS](er, entry)
580
686
  }
581
687
 
582
688
  [FILE] (entry, _) {
@@ -671,7 +777,7 @@ class UnpackSync extends Unpack {
671
777
 
672
778
  [MKDIR] (dir, mode) {
673
779
  try {
674
- return mkdir.sync(dir, {
780
+ return mkdir.sync(normPath(dir), {
675
781
  uid: this.uid,
676
782
  gid: this.gid,
677
783
  processUid: this.processUid,
@@ -5,8 +5,16 @@ const Header = require('./header.js')
5
5
  const ReadEntry = require('./read-entry.js')
6
6
  const fs = require('fs')
7
7
  const path = require('path')
8
+ const normPath = require('./normalize-windows-path.js')
9
+ const stripSlash = require('./strip-trailing-slashes.js')
10
+
11
+ const prefixPath = (path, prefix) => {
12
+ if (!prefix)
13
+ return path
14
+ path = normPath(path).replace(/^\.(\/|$)/, '')
15
+ return stripSlash(prefix) + '/' + path
16
+ }
8
17
 
9
- const types = require('./types.js')
10
18
  const maxReadSize = 16 * 1024 * 1024
11
19
  const PROCESS = Symbol('process')
12
20
  const FILE = Symbol('file')
@@ -23,6 +31,10 @@ const OPENFILE = Symbol('openfile')
23
31
  const ONOPENFILE = Symbol('onopenfile')
24
32
  const CLOSE = Symbol('close')
25
33
  const MODE = Symbol('mode')
34
+ const AWAITDRAIN = Symbol('awaitDrain')
35
+ const ONDRAIN = Symbol('ondrain')
36
+ const PREFIX = Symbol('prefix')
37
+ const HAD_ERROR = Symbol('hadError')
26
38
  const warner = require('./warn-mixin.js')
27
39
  const winchars = require('./winchars.js')
28
40
  const stripAbsolutePath = require('./strip-absolute-path.js')
@@ -35,21 +47,31 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
35
47
  super(opt)
36
48
  if (typeof p !== 'string')
37
49
  throw new TypeError('path is required')
38
- this.path = p
50
+ this.path = normPath(p)
39
51
  // suppress atime, ctime, uid, gid, uname, gname
40
52
  this.portable = !!opt.portable
41
53
  // until node has builtin pwnam functions, this'll have to do
42
- this.myuid = process.getuid && process.getuid()
54
+ this.myuid = process.getuid && process.getuid() || 0
43
55
  this.myuser = process.env.USER || ''
44
56
  this.maxReadSize = opt.maxReadSize || maxReadSize
45
57
  this.linkCache = opt.linkCache || new Map()
46
58
  this.statCache = opt.statCache || new Map()
47
59
  this.preservePaths = !!opt.preservePaths
48
- this.cwd = opt.cwd || process.cwd()
60
+ this.cwd = normPath(opt.cwd || process.cwd())
49
61
  this.strict = !!opt.strict
50
62
  this.noPax = !!opt.noPax
51
63
  this.noMtime = !!opt.noMtime
52
64
  this.mtime = opt.mtime || null
65
+ this.prefix = opt.prefix ? normPath(opt.prefix) : null
66
+
67
+ this.fd = null
68
+ this.blockLen = null
69
+ this.blockRemain = null
70
+ this.buf = null
71
+ this.offset = null
72
+ this.length = null
73
+ this.pos = null
74
+ this.remain = null
53
75
 
54
76
  if (typeof opt.onwarn === 'function')
55
77
  this.on('warn', opt.onwarn)
@@ -65,11 +87,13 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
65
87
 
66
88
  this.win32 = !!opt.win32 || process.platform === 'win32'
67
89
  if (this.win32) {
90
+ // force the \ to / normalization, since we might not *actually*
91
+ // be on windows, but want \ to be considered a path separator.
68
92
  this.path = winchars.decode(this.path.replace(/\\/g, '/'))
69
93
  p = p.replace(/\\/g, '/')
70
94
  }
71
95
 
72
- this.absolute = opt.absolute || path.resolve(this.cwd, p)
96
+ this.absolute = normPath(opt.absolute || path.resolve(this.cwd, p))
73
97
 
74
98
  if (this.path === '')
75
99
  this.path = './'
@@ -87,6 +111,12 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
87
111
  this[LSTAT]()
88
112
  }
89
113
 
114
+ emit (ev, ...data) {
115
+ if (ev === 'error')
116
+ this[HAD_ERROR] = true
117
+ return super.emit(ev, ...data)
118
+ }
119
+
90
120
  [LSTAT] () {
91
121
  fs.lstat(this.absolute, (er, stat) => {
92
122
  if (er)
@@ -119,13 +149,19 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
119
149
  return modeFix(mode, this.type === 'Directory', this.portable)
120
150
  }
121
151
 
152
+ [PREFIX] (path) {
153
+ return prefixPath(path, this.prefix)
154
+ }
155
+
122
156
  [HEADER] () {
123
157
  if (this.type === 'Directory' && this.portable)
124
158
  this.noMtime = true
125
159
 
126
160
  this.header = new Header({
127
- path: this.path,
128
- linkpath: this.linkpath,
161
+ path: this[PREFIX](this.path),
162
+ // only apply the prefix to hard links.
163
+ linkpath: this.type === 'Link' ? this[PREFIX](this.linkpath)
164
+ : this.linkpath,
129
165
  // only the permissions and setuid/setgid/sticky bitflags
130
166
  // not the higher-order bits that specify file type
131
167
  mode: this[MODE](this.stat.mode),
@@ -140,14 +176,15 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
140
176
  ctime: this.portable ? null : this.stat.ctime
141
177
  })
142
178
 
143
- if (this.header.encode() && !this.noPax)
144
- this.write(new Pax({
179
+ if (this.header.encode() && !this.noPax) {
180
+ super.write(new Pax({
145
181
  atime: this.portable ? null : this.header.atime,
146
182
  ctime: this.portable ? null : this.header.ctime,
147
183
  gid: this.portable ? null : this.header.gid,
148
184
  mtime: this.noMtime ? null : this.mtime || this.header.mtime,
149
- path: this.path,
150
- linkpath: this.linkpath,
185
+ path: this[PREFIX](this.path),
186
+ linkpath: this.type === 'Link' ? this[PREFIX](this.linkpath)
187
+ : this.linkpath,
151
188
  size: this.header.size,
152
189
  uid: this.portable ? null : this.header.uid,
153
190
  uname: this.portable ? null : this.header.uname,
@@ -155,7 +192,8 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
155
192
  ino: this.portable ? null : this.stat.ino,
156
193
  nlink: this.portable ? null : this.stat.nlink
157
194
  }).encode())
158
- this.write(this.header.block)
195
+ }
196
+ super.write(this.header.block)
159
197
  }
160
198
 
161
199
  [DIRECTORY] () {
@@ -175,14 +213,14 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
175
213
  }
176
214
 
177
215
  [ONREADLINK] (linkpath) {
178
- this.linkpath = linkpath
216
+ this.linkpath = normPath(linkpath)
179
217
  this[HEADER]()
180
218
  this.end()
181
219
  }
182
220
 
183
221
  [HARDLINK] (linkpath) {
184
222
  this.type = 'Link'
185
- this.linkpath = path.relative(this.cwd, linkpath)
223
+ this.linkpath = normPath(path.relative(this.cwd, linkpath))
186
224
  this.stat.size = 0
187
225
  this[HEADER]()
188
226
  this.end()
@@ -215,74 +253,110 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
215
253
  }
216
254
 
217
255
  [ONOPENFILE] (fd) {
218
- const blockLen = 512 * Math.ceil(this.stat.size / 512)
219
- const bufLen = Math.min(blockLen, this.maxReadSize)
220
- const buf = Buffer.allocUnsafe(bufLen)
221
- this[READ](fd, buf, 0, buf.length, 0, this.stat.size, blockLen)
256
+ this.fd = fd
257
+ if (this[HAD_ERROR])
258
+ return this[CLOSE]()
259
+
260
+ this.blockLen = 512 * Math.ceil(this.stat.size / 512)
261
+ this.blockRemain = this.blockLen
262
+ const bufLen = Math.min(this.blockLen, this.maxReadSize)
263
+ this.buf = Buffer.allocUnsafe(bufLen)
264
+ this.offset = 0
265
+ this.pos = 0
266
+ this.remain = this.stat.size
267
+ this.length = this.buf.length
268
+ this[READ]()
222
269
  }
223
270
 
224
- [READ] (fd, buf, offset, length, pos, remain, blockRemain) {
271
+ [READ] () {
272
+ const { fd, buf, offset, length, pos } = this
225
273
  fs.read(fd, buf, offset, length, pos, (er, bytesRead) => {
226
274
  if (er) {
227
275
  // ignoring the error from close(2) is a bad practice, but at
228
276
  // this point we already have an error, don't need another one
229
- return this[CLOSE](fd, () => this.emit('error', er))
277
+ return this[CLOSE](() => this.emit('error', er))
230
278
  }
231
- this[ONREAD](fd, buf, offset, length, pos, remain, blockRemain, bytesRead)
279
+ this[ONREAD](bytesRead)
232
280
  })
233
281
  }
234
282
 
235
- [CLOSE] (fd, cb) {
236
- fs.close(fd, cb)
283
+ [CLOSE] (cb) {
284
+ fs.close(this.fd, cb)
237
285
  }
238
286
 
239
- [ONREAD] (fd, buf, offset, length, pos, remain, blockRemain, bytesRead) {
240
- if (bytesRead <= 0 && remain > 0) {
287
+ [ONREAD] (bytesRead) {
288
+ if (bytesRead <= 0 && this.remain > 0) {
241
289
  const er = new Error('encountered unexpected EOF')
242
290
  er.path = this.absolute
243
291
  er.syscall = 'read'
244
292
  er.code = 'EOF'
245
- return this[CLOSE](fd, () => this.emit('error', er))
293
+ return this[CLOSE](() => this.emit('error', er))
246
294
  }
247
295
 
248
- if (bytesRead > remain) {
296
+ if (bytesRead > this.remain) {
249
297
  const er = new Error('did not encounter expected EOF')
250
298
  er.path = this.absolute
251
299
  er.syscall = 'read'
252
300
  er.code = 'EOF'
253
- return this[CLOSE](fd, () => this.emit('error', er))
301
+ return this[CLOSE](() => this.emit('error', er))
254
302
  }
255
303
 
256
304
  // null out the rest of the buffer, if we could fit the block padding
257
- if (bytesRead === remain) {
258
- for (let i = bytesRead; i < length && bytesRead < blockRemain; i++) {
259
- buf[i + offset] = 0
260
- bytesRead ++
261
- remain ++
305
+ // at the end of this loop, we've incremented bytesRead and this.remain
306
+ // to be incremented up to the blockRemain level, as if we had expected
307
+ // to get a null-padded file, and read it until the end. then we will
308
+ // decrement both remain and blockRemain by bytesRead, and know that we
309
+ // reached the expected EOF, without any null buffer to append.
310
+ if (bytesRead === this.remain) {
311
+ for (let i = bytesRead; i < this.length && bytesRead < this.blockRemain; i++) {
312
+ this.buf[i + this.offset] = 0
313
+ bytesRead++
314
+ this.remain++
262
315
  }
263
316
  }
264
317
 
265
- const writeBuf = offset === 0 && bytesRead === buf.length ?
266
- buf : buf.slice(offset, offset + bytesRead)
267
- remain -= bytesRead
268
- blockRemain -= bytesRead
269
- pos += bytesRead
270
- offset += bytesRead
318
+ const writeBuf = this.offset === 0 && bytesRead === this.buf.length ?
319
+ this.buf : this.buf.slice(this.offset, this.offset + bytesRead)
271
320
 
272
- this.write(writeBuf)
321
+ const flushed = this.write(writeBuf)
322
+ if (!flushed)
323
+ this[AWAITDRAIN](() => this[ONDRAIN]())
324
+ else
325
+ this[ONDRAIN]()
326
+ }
273
327
 
274
- if (!remain) {
275
- if (blockRemain)
276
- this.write(Buffer.alloc(blockRemain))
277
- return this[CLOSE](fd, er => er ? this.emit('error', er) : this.end())
328
+ [AWAITDRAIN] (cb) {
329
+ this.once('drain', cb)
330
+ }
331
+
332
+ write (writeBuf) {
333
+ if (this.blockRemain < writeBuf.length) {
334
+ const er = new Error('writing more data than expected')
335
+ er.path = this.absolute
336
+ return this.emit('error', er)
337
+ }
338
+ this.remain -= writeBuf.length
339
+ this.blockRemain -= writeBuf.length
340
+ this.pos += writeBuf.length
341
+ this.offset += writeBuf.length
342
+ return super.write(writeBuf)
343
+ }
344
+
345
+ [ONDRAIN] () {
346
+ if (!this.remain) {
347
+ if (this.blockRemain)
348
+ super.write(Buffer.alloc(this.blockRemain))
349
+ return this[CLOSE](er => er ? this.emit('error', er) : this.end())
278
350
  }
279
351
 
280
- if (offset >= length) {
281
- buf = Buffer.allocUnsafe(length)
282
- offset = 0
352
+ if (this.offset >= this.length) {
353
+ // if we only have a smaller bit left to read, alloc a smaller buffer
354
+ // otherwise, keep it the same length it was before.
355
+ this.buf = Buffer.allocUnsafe(Math.min(this.blockRemain, this.buf.length))
356
+ this.offset = 0
283
357
  }
284
- length = buf.length - offset
285
- this[READ](fd, buf, offset, length, pos, remain, blockRemain)
358
+ this.length = this.buf.length - this.offset
359
+ this[READ]()
286
360
  }
287
361
  })
288
362
 
@@ -303,22 +377,30 @@ class WriteEntrySync extends WriteEntry {
303
377
  this[ONOPENFILE](fs.openSync(this.absolute, 'r'))
304
378
  }
305
379
 
306
- [READ] (fd, buf, offset, length, pos, remain, blockRemain) {
380
+ [READ] () {
307
381
  let threw = true
308
382
  try {
383
+ const { fd, buf, offset, length, pos } = this
309
384
  const bytesRead = fs.readSync(fd, buf, offset, length, pos)
310
- this[ONREAD](fd, buf, offset, length, pos, remain, blockRemain, bytesRead)
385
+ this[ONREAD](bytesRead)
311
386
  threw = false
312
387
  } finally {
313
388
  // ignoring the error from close(2) is a bad practice, but at
314
389
  // this point we already have an error, don't need another one
315
- if (threw)
316
- try { this[CLOSE](fd, () => {}) } catch (er) {}
390
+ if (threw) {
391
+ try {
392
+ this[CLOSE](() => {})
393
+ } catch (er) {}
394
+ }
317
395
  }
318
396
  }
319
397
 
320
- [CLOSE] (fd, cb) {
321
- fs.closeSync(fd)
398
+ [AWAITDRAIN] (cb) {
399
+ cb()
400
+ }
401
+
402
+ [CLOSE] (cb) {
403
+ fs.closeSync(this.fd)
322
404
  cb()
323
405
  }
324
406
  }
@@ -338,7 +420,9 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
338
420
  if (this.type === 'Directory' && this.portable)
339
421
  this.noMtime = true
340
422
 
341
- this.path = readEntry.path
423
+ this.prefix = opt.prefix || null
424
+
425
+ this.path = normPath(readEntry.path)
342
426
  this.mode = this[MODE](readEntry.mode)
343
427
  this.uid = this.portable ? null : readEntry.uid
344
428
  this.gid = this.portable ? null : readEntry.gid
@@ -348,7 +432,7 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
348
432
  this.mtime = this.noMtime ? null : opt.mtime || readEntry.mtime
349
433
  this.atime = this.portable ? null : readEntry.atime
350
434
  this.ctime = this.portable ? null : readEntry.ctime
351
- this.linkpath = readEntry.linkpath
435
+ this.linkpath = normPath(readEntry.linkpath)
352
436
 
353
437
  if (typeof opt.onwarn === 'function')
354
438
  this.on('warn', opt.onwarn)
@@ -366,8 +450,9 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
366
450
  this.blockRemain = readEntry.startBlockSize
367
451
 
368
452
  this.header = new Header({
369
- path: this.path,
370
- linkpath: this.linkpath,
453
+ path: this[PREFIX](this.path),
454
+ linkpath: this.type === 'Link' ? this[PREFIX](this.linkpath)
455
+ : this.linkpath,
371
456
  // only the permissions and setuid/setgid/sticky bitflags
372
457
  // not the higher-order bits that specify file type
373
458
  mode: this.mode,
@@ -394,8 +479,9 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
394
479
  ctime: this.portable ? null : this.ctime,
395
480
  gid: this.portable ? null : this.gid,
396
481
  mtime: this.noMtime ? null : this.mtime,
397
- path: this.path,
398
- linkpath: this.linkpath,
482
+ path: this[PREFIX](this.path),
483
+ linkpath: this.type === 'Link' ? this[PREFIX](this.linkpath)
484
+ : this.linkpath,
399
485
  size: this.size,
400
486
  uid: this.portable ? null : this.uid,
401
487
  uname: this.portable ? null : this.uname,
@@ -408,6 +494,10 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
408
494
  readEntry.pipe(this)
409
495
  }
410
496
 
497
+ [PREFIX] (path) {
498
+ return prefixPath(path, this.prefix)
499
+ }
500
+
411
501
  [MODE] (mode) {
412
502
  return modeFix(mode, this.type === 'Directory', this.portable)
413
503
  }
@@ -422,7 +512,7 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
422
512
 
423
513
  end () {
424
514
  if (this.blockRemain)
425
- this.write(Buffer.alloc(this.blockRemain))
515
+ super.write(Buffer.alloc(this.blockRemain))
426
516
  return super.end()
427
517
  }
428
518
  })
package/package.json CHANGED
@@ -2,7 +2,7 @@
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": "5.0.7",
5
+ "version": "5.0.8",
6
6
  "publishConfig": {
7
7
  "tag": "v5-legacy"
8
8
  },
@@ -11,7 +11,9 @@
11
11
  "url": "https://github.com/npm/node-tar.git"
12
12
  },
13
13
  "scripts": {
14
- "test": "tap",
14
+ "test:posix": "tap",
15
+ "test:win32": "tap --lines=98 --branches=98 --statements=98 --functions=98",
16
+ "test": "node test/fixtures/test.js",
15
17
  "preversion": "npm test",
16
18
  "postversion": "npm publish",
17
19
  "postpublish": "git push origin --follow-tags",
@@ -19,20 +21,21 @@
19
21
  "bench": "for i in benchmarks/*/*.js; do echo $i; for j in {1..5}; do node $i || break; done; done"
20
22
  },
21
23
  "dependencies": {
22
- "chownr": "^1.1.3",
23
- "fs-minipass": "^2.0.0",
24
- "minipass": "^3.0.0",
25
- "minizlib": "^2.1.0",
26
- "mkdirp": "^0.5.0",
24
+ "chownr": "^1.1.4",
25
+ "fs-minipass": "^2.1.0",
26
+ "minipass": "^3.1.3",
27
+ "minizlib": "^2.1.2",
28
+ "mkdirp": "^0.5.5",
27
29
  "yallist": "^4.0.0"
28
30
  },
29
31
  "devDependencies": {
30
32
  "chmodr": "^1.2.0",
31
- "end-of-stream": "^1.4.3",
33
+ "end-of-stream": "^1.4.4",
32
34
  "events-to-array": "^1.1.2",
33
35
  "mutate-fs": "^2.1.1",
36
+ "require-inject": "^1.4.4",
34
37
  "rimraf": "^2.7.1",
35
- "tap": "^14.6.9",
38
+ "tap": "^14.11.0",
36
39
  "tar-fs": "^1.16.3",
37
40
  "tar-stream": "^1.6.2"
38
41
  },