tar 4.4.15 → 4.4.16

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.
@@ -6,8 +6,16 @@ const Header = require('./header.js')
6
6
  const ReadEntry = require('./read-entry.js')
7
7
  const fs = require('fs')
8
8
  const path = require('path')
9
+ const normPath = require('./normalize-windows-path.js')
10
+ const stripSlash = require('./strip-trailing-slashes.js')
11
+
12
+ const prefixPath = (path, prefix) => {
13
+ if (!prefix)
14
+ return path
15
+ path = normPath(path).replace(/^\.(\/|$)/, '')
16
+ return stripSlash(prefix) + '/' + path
17
+ }
9
18
 
10
- const types = require('./types.js')
11
19
  const maxReadSize = 16 * 1024 * 1024
12
20
  const PROCESS = Symbol('process')
13
21
  const FILE = Symbol('file')
@@ -24,6 +32,10 @@ const OPENFILE = Symbol('openfile')
24
32
  const ONOPENFILE = Symbol('onopenfile')
25
33
  const CLOSE = Symbol('close')
26
34
  const MODE = Symbol('mode')
35
+ const AWAITDRAIN = Symbol('awaitDrain')
36
+ const ONDRAIN = Symbol('ondrain')
37
+ const PREFIX = Symbol('prefix')
38
+ const HAD_ERROR = Symbol('hadError')
27
39
  const warner = require('./warn-mixin.js')
28
40
  const winchars = require('./winchars.js')
29
41
  const stripAbsolutePath = require('./strip-absolute-path.js')
@@ -36,21 +48,31 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
36
48
  super(opt)
37
49
  if (typeof p !== 'string')
38
50
  throw new TypeError('path is required')
39
- this.path = p
51
+ this.path = normPath(p)
40
52
  // suppress atime, ctime, uid, gid, uname, gname
41
53
  this.portable = !!opt.portable
42
54
  // until node has builtin pwnam functions, this'll have to do
43
- this.myuid = process.getuid && process.getuid()
55
+ this.myuid = process.getuid && process.getuid() || 0
44
56
  this.myuser = process.env.USER || ''
45
57
  this.maxReadSize = opt.maxReadSize || maxReadSize
46
58
  this.linkCache = opt.linkCache || new Map()
47
59
  this.statCache = opt.statCache || new Map()
48
60
  this.preservePaths = !!opt.preservePaths
49
- this.cwd = opt.cwd || process.cwd()
61
+ this.cwd = normPath(opt.cwd || process.cwd())
50
62
  this.strict = !!opt.strict
51
63
  this.noPax = !!opt.noPax
52
64
  this.noMtime = !!opt.noMtime
53
65
  this.mtime = opt.mtime || null
66
+ this.prefix = opt.prefix ? normPath(opt.prefix) : null
67
+
68
+ this.fd = null
69
+ this.blockLen = null
70
+ this.blockRemain = null
71
+ this.buf = null
72
+ this.offset = null
73
+ this.length = null
74
+ this.pos = null
75
+ this.remain = null
54
76
 
55
77
  if (typeof opt.onwarn === 'function')
56
78
  this.on('warn', opt.onwarn)
@@ -58,18 +80,20 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
58
80
  if (!this.preservePaths) {
59
81
  const s = stripAbsolutePath(this.path)
60
82
  if (s[0]) {
83
+ this.warn('stripping ' + s[0] + ' from absolute path', this.path)
61
84
  this.path = s[1]
62
- this.warn('stripping ' + s[0] + ' from absolute path', p)
63
85
  }
64
86
  }
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 = './'
@@ -80,6 +104,12 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
80
104
  this[LSTAT]()
81
105
  }
82
106
 
107
+ emit (ev, ...data) {
108
+ if (ev === 'error')
109
+ this[HAD_ERROR] = true
110
+ return super.emit(ev, ...data)
111
+ }
112
+
83
113
  [LSTAT] () {
84
114
  fs.lstat(this.absolute, (er, stat) => {
85
115
  if (er)
@@ -112,13 +142,19 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
112
142
  return modeFix(mode, this.type === 'Directory')
113
143
  }
114
144
 
145
+ [PREFIX] (path) {
146
+ return prefixPath(path, this.prefix)
147
+ }
148
+
115
149
  [HEADER] () {
116
150
  if (this.type === 'Directory' && this.portable)
117
151
  this.noMtime = true
118
152
 
119
153
  this.header = new Header({
120
- path: this.path,
121
- linkpath: this.linkpath,
154
+ path: this[PREFIX](this.path),
155
+ // only apply the prefix to hard links.
156
+ linkpath: this.type === 'Link' ? this[PREFIX](this.linkpath)
157
+ : this.linkpath,
122
158
  // only the permissions and setuid/setgid/sticky bitflags
123
159
  // not the higher-order bits that specify file type
124
160
  mode: this[MODE](this.stat.mode),
@@ -133,14 +169,15 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
133
169
  ctime: this.portable ? null : this.stat.ctime
134
170
  })
135
171
 
136
- if (this.header.encode() && !this.noPax)
137
- this.write(new Pax({
172
+ if (this.header.encode() && !this.noPax) {
173
+ super.write(new Pax({
138
174
  atime: this.portable ? null : this.header.atime,
139
175
  ctime: this.portable ? null : this.header.ctime,
140
176
  gid: this.portable ? null : this.header.gid,
141
177
  mtime: this.noMtime ? null : this.mtime || this.header.mtime,
142
- path: this.path,
143
- linkpath: this.linkpath,
178
+ path: this[PREFIX](this.path),
179
+ linkpath: this.type === 'Link' ? this[PREFIX](this.linkpath)
180
+ : this.linkpath,
144
181
  size: this.header.size,
145
182
  uid: this.portable ? null : this.header.uid,
146
183
  uname: this.portable ? null : this.header.uname,
@@ -148,7 +185,8 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
148
185
  ino: this.portable ? null : this.stat.ino,
149
186
  nlink: this.portable ? null : this.stat.nlink
150
187
  }).encode())
151
- this.write(this.header.block)
188
+ }
189
+ super.write(this.header.block)
152
190
  }
153
191
 
154
192
  [DIRECTORY] () {
@@ -168,14 +206,14 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
168
206
  }
169
207
 
170
208
  [ONREADLINK] (linkpath) {
171
- this.linkpath = linkpath
209
+ this.linkpath = normPath(linkpath)
172
210
  this[HEADER]()
173
211
  this.end()
174
212
  }
175
213
 
176
214
  [HARDLINK] (linkpath) {
177
215
  this.type = 'Link'
178
- this.linkpath = path.relative(this.cwd, linkpath)
216
+ this.linkpath = normPath(path.relative(this.cwd, linkpath))
179
217
  this.stat.size = 0
180
218
  this[HEADER]()
181
219
  this.end()
@@ -208,75 +246,111 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
208
246
  }
209
247
 
210
248
  [ONOPENFILE] (fd) {
211
- const blockLen = 512 * Math.ceil(this.stat.size / 512)
212
- const bufLen = Math.min(blockLen, this.maxReadSize)
213
- const buf = Buffer.allocUnsafe(bufLen)
214
- this[READ](fd, buf, 0, buf.length, 0, this.stat.size, blockLen)
249
+ this.fd = fd
250
+ if (this[HAD_ERROR])
251
+ return this[CLOSE]()
252
+
253
+ this.blockLen = 512 * Math.ceil(this.stat.size / 512)
254
+ this.blockRemain = this.blockLen
255
+ const bufLen = Math.min(this.blockLen, this.maxReadSize)
256
+ this.buf = Buffer.allocUnsafe(bufLen)
257
+ this.offset = 0
258
+ this.pos = 0
259
+ this.remain = this.stat.size
260
+ this.length = this.buf.length
261
+ this[READ]()
215
262
  }
216
263
 
217
- [READ] (fd, buf, offset, length, pos, remain, blockRemain) {
264
+ [READ] () {
265
+ const { fd, buf, offset, length, pos } = this
218
266
  fs.read(fd, buf, offset, length, pos, (er, bytesRead) => {
219
- if (er)
220
- return this[CLOSE](fd, _ => this.emit('error', er))
221
- this[ONREAD](fd, buf, offset, length, pos, remain, blockRemain, bytesRead)
267
+ if (er) {
268
+ // ignoring the error from close(2) is a bad practice, but at
269
+ // this point we already have an error, don't need another one
270
+ return this[CLOSE](() => this.emit('error', er))
271
+ }
272
+ this[ONREAD](bytesRead)
222
273
  })
223
274
  }
224
275
 
225
- [CLOSE] (fd, cb) {
226
- fs.close(fd, cb)
276
+ [CLOSE] (cb) {
277
+ fs.close(this.fd, cb)
227
278
  }
228
279
 
229
- [ONREAD] (fd, buf, offset, length, pos, remain, blockRemain, bytesRead) {
230
- if (bytesRead <= 0 && remain > 0) {
280
+ [ONREAD] (bytesRead) {
281
+ if (bytesRead <= 0 && this.remain > 0) {
231
282
  const er = new Error('encountered unexpected EOF')
232
283
  er.path = this.absolute
233
284
  er.syscall = 'read'
234
285
  er.code = 'EOF'
235
- this[CLOSE](fd, _ => _)
236
- return this.emit('error', er)
286
+ return this[CLOSE](() => this.emit('error', er))
237
287
  }
238
288
 
239
- if (bytesRead > remain) {
289
+ if (bytesRead > this.remain) {
240
290
  const er = new Error('did not encounter expected EOF')
241
291
  er.path = this.absolute
242
292
  er.syscall = 'read'
243
293
  er.code = 'EOF'
244
- this[CLOSE](fd, _ => _)
245
- return this.emit('error', er)
294
+ return this[CLOSE](() => this.emit('error', er))
246
295
  }
247
296
 
248
297
  // null out the rest of the buffer, if we could fit the block padding
249
- if (bytesRead === remain) {
250
- for (let i = bytesRead; i < length && bytesRead < blockRemain; i++) {
251
- buf[i + offset] = 0
252
- bytesRead ++
253
- remain ++
298
+ // at the end of this loop, we've incremented bytesRead and this.remain
299
+ // to be incremented up to the blockRemain level, as if we had expected
300
+ // to get a null-padded file, and read it until the end. then we will
301
+ // decrement both remain and blockRemain by bytesRead, and know that we
302
+ // reached the expected EOF, without any null buffer to append.
303
+ if (bytesRead === this.remain) {
304
+ for (let i = bytesRead; i < this.length && bytesRead < this.blockRemain; i++) {
305
+ this.buf[i + this.offset] = 0
306
+ bytesRead++
307
+ this.remain++
254
308
  }
255
309
  }
256
310
 
257
- const writeBuf = offset === 0 && bytesRead === buf.length ?
258
- buf : buf.slice(offset, offset + bytesRead)
259
- remain -= bytesRead
260
- blockRemain -= bytesRead
261
- pos += bytesRead
262
- offset += bytesRead
263
-
264
- this.write(writeBuf)
265
-
266
- if (!remain) {
267
- if (blockRemain)
268
- this.write(Buffer.alloc(blockRemain))
269
- this.end()
270
- this[CLOSE](fd, _ => _)
271
- return
311
+ const writeBuf = this.offset === 0 && bytesRead === this.buf.length ?
312
+ this.buf : this.buf.slice(this.offset, this.offset + bytesRead)
313
+
314
+ const flushed = this.write(writeBuf)
315
+ if (!flushed)
316
+ this[AWAITDRAIN](() => this[ONDRAIN]())
317
+ else
318
+ this[ONDRAIN]()
319
+ }
320
+
321
+ [AWAITDRAIN] (cb) {
322
+ this.once('drain', cb)
323
+ }
324
+
325
+ write (writeBuf) {
326
+ if (this.blockRemain < writeBuf.length) {
327
+ const er = new Error('writing more data than expected')
328
+ er.path = this.absolute
329
+ return this.emit('error', er)
272
330
  }
331
+ this.remain -= writeBuf.length
332
+ this.blockRemain -= writeBuf.length
333
+ this.pos += writeBuf.length
334
+ this.offset += writeBuf.length
335
+ return super.write(writeBuf)
336
+ }
273
337
 
274
- if (offset >= length) {
275
- buf = Buffer.allocUnsafe(length)
276
- offset = 0
338
+ [ONDRAIN] () {
339
+ if (!this.remain) {
340
+ if (this.blockRemain)
341
+ super.write(Buffer.alloc(this.blockRemain))
342
+ return this[CLOSE](/* istanbul ignore next - legacy */
343
+ er => er ? this.emit('error', er) : this.end())
277
344
  }
278
- length = buf.length - offset
279
- this[READ](fd, buf, offset, length, pos, remain, blockRemain)
345
+
346
+ if (this.offset >= this.length) {
347
+ // if we only have a smaller bit left to read, alloc a smaller buffer
348
+ // otherwise, keep it the same length it was before.
349
+ this.buf = Buffer.allocUnsafe(Math.min(this.blockRemain, this.buf.length))
350
+ this.offset = 0
351
+ }
352
+ this.length = this.buf.length - this.offset
353
+ this[READ]()
280
354
  }
281
355
  })
282
356
 
@@ -297,20 +371,31 @@ class WriteEntrySync extends WriteEntry {
297
371
  this[ONOPENFILE](fs.openSync(this.absolute, 'r'))
298
372
  }
299
373
 
300
- [READ] (fd, buf, offset, length, pos, remain, blockRemain) {
374
+ [READ] () {
301
375
  let threw = true
302
376
  try {
377
+ const { fd, buf, offset, length, pos } = this
303
378
  const bytesRead = fs.readSync(fd, buf, offset, length, pos)
304
- this[ONREAD](fd, buf, offset, length, pos, remain, blockRemain, bytesRead)
379
+ this[ONREAD](bytesRead)
305
380
  threw = false
306
381
  } finally {
307
- if (threw)
308
- try { this[CLOSE](fd) } catch (er) {}
382
+ // ignoring the error from close(2) is a bad practice, but at
383
+ // this point we already have an error, don't need another one
384
+ if (threw) {
385
+ try {
386
+ this[CLOSE](() => {})
387
+ } catch (er) {}
388
+ }
309
389
  }
310
390
  }
311
391
 
312
- [CLOSE] (fd) {
313
- fs.closeSync(fd)
392
+ [AWAITDRAIN] (cb) {
393
+ cb()
394
+ }
395
+
396
+ [CLOSE] (cb) {
397
+ fs.closeSync(this.fd)
398
+ cb()
314
399
  }
315
400
  }
316
401
 
@@ -329,7 +414,9 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
329
414
  if (this.type === 'Directory' && this.portable)
330
415
  this.noMtime = true
331
416
 
332
- this.path = readEntry.path
417
+ this.prefix = opt.prefix || null
418
+
419
+ this.path = normPath(readEntry.path)
333
420
  this.mode = this[MODE](readEntry.mode)
334
421
  this.uid = this.portable ? null : readEntry.uid
335
422
  this.gid = this.portable ? null : readEntry.gid
@@ -339,7 +426,7 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
339
426
  this.mtime = this.noMtime ? null : opt.mtime || readEntry.mtime
340
427
  this.atime = this.portable ? null : readEntry.atime
341
428
  this.ctime = this.portable ? null : readEntry.ctime
342
- this.linkpath = readEntry.linkpath
429
+ this.linkpath = normPath(readEntry.linkpath)
343
430
 
344
431
  if (typeof opt.onwarn === 'function')
345
432
  this.on('warn', opt.onwarn)
@@ -359,8 +446,9 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
359
446
  this.blockRemain = readEntry.startBlockSize
360
447
 
361
448
  this.header = new Header({
362
- path: this.path,
363
- linkpath: this.linkpath,
449
+ path: this[PREFIX](this.path),
450
+ linkpath: this.type === 'Link' ? this[PREFIX](this.linkpath)
451
+ : this.linkpath,
364
452
  // only the permissions and setuid/setgid/sticky bitflags
365
453
  // not the higher-order bits that specify file type
366
454
  mode: this.mode,
@@ -380,8 +468,9 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
380
468
  ctime: this.portable ? null : this.ctime,
381
469
  gid: this.portable ? null : this.gid,
382
470
  mtime: this.noMtime ? null : this.mtime,
383
- path: this.path,
384
- linkpath: this.linkpath,
471
+ path: this[PREFIX](this.path),
472
+ linkpath: this.type === 'Link' ? this[PREFIX](this.linkpath)
473
+ : this.linkpath,
385
474
  size: this.size,
386
475
  uid: this.portable ? null : this.uid,
387
476
  uname: this.portable ? null : this.uname,
@@ -394,6 +483,10 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
394
483
  readEntry.pipe(this)
395
484
  }
396
485
 
486
+ [PREFIX] (path) {
487
+ return prefixPath(path, this.prefix)
488
+ }
489
+
397
490
  [MODE] (mode) {
398
491
  return modeFix(mode, this.type === 'Directory')
399
492
  }
@@ -408,7 +501,7 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
408
501
 
409
502
  end () {
410
503
  if (this.blockRemain)
411
- this.write(Buffer.alloc(this.blockRemain))
504
+ super.write(Buffer.alloc(this.blockRemain))
412
505
  return super.end()
413
506
  }
414
507
  })
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": "4.4.15",
5
+ "version": "4.4.16",
6
6
  "publishConfig": {
7
7
  "tag": "v4-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,21 +21,22 @@
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.1",
23
- "fs-minipass": "^1.2.5",
24
- "minipass": "^2.8.6",
25
- "minizlib": "^1.2.1",
26
- "mkdirp": "^0.5.0",
27
- "safe-buffer": "^5.1.2",
28
- "yallist": "^3.0.3"
24
+ "chownr": "^1.1.4",
25
+ "fs-minipass": "^1.2.7",
26
+ "minipass": "^2.9.0",
27
+ "minizlib": "^1.3.3",
28
+ "mkdirp": "^0.5.5",
29
+ "safe-buffer": "^5.2.1",
30
+ "yallist": "^3.1.1"
29
31
  },
30
32
  "devDependencies": {
31
33
  "chmodr": "^1.2.0",
32
- "end-of-stream": "^1.4.1",
34
+ "end-of-stream": "^1.4.4",
33
35
  "events-to-array": "^1.1.2",
34
36
  "mutate-fs": "^2.1.1",
35
- "rimraf": "^2.6.3",
36
- "tap": "^14.6.5",
37
+ "require-inject": "^1.4.4",
38
+ "rimraf": "^2.7.1",
39
+ "tap": "^14.11.0",
37
40
  "tar-fs": "^1.16.3",
38
41
  "tar-stream": "^1.6.2"
39
42
  },