tar 5.0.7 → 5.0.11

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.
@@ -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.11",
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
  },