tar 3.1.14 → 3.2.2

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/README.md CHANGED
@@ -295,6 +295,16 @@ The following options are supported:
295
295
  `tar(1)`, but ACLs and other system-specific data is never unpacked
296
296
  in this implementation, and modes are set by default already.
297
297
  [Alias: `p`]
298
+ - `uid` Set to a number to force ownership of all extracted files and
299
+ folders, and all implicitly created directories, to be owned by the
300
+ specified user id, regardless of the `uid` field in the archive.
301
+ Cannot be used along with `preserveOwner`. Requires also setting a
302
+ `gid` option.
303
+ - `gid` Set to a number to force ownership of all extracted files and
304
+ folders, and all implicitly created directories, to be owned by the
305
+ specified group id, regardless of the `gid` field in the archive.
306
+ Cannot be used along with `preserveOwner`. Requires also setting a
307
+ `uid` option.
298
308
 
299
309
  The following options are mostly internal, but can be modified in some
300
310
  advanced use cases, such as re-using caches between runs.
@@ -554,6 +564,16 @@ mode.
554
564
  - `win32` True if on a windows platform. Causes behavior where
555
565
  filenames containing `<|>?` chars are converted to
556
566
  windows-compatible values while being unpacked.
567
+ - `uid` Set to a number to force ownership of all extracted files and
568
+ folders, and all implicitly created directories, to be owned by the
569
+ specified user id, regardless of the `uid` field in the archive.
570
+ Cannot be used along with `preserveOwner`. Requires also setting a
571
+ `gid` option.
572
+ - `gid` Set to a number to force ownership of all extracted files and
573
+ folders, and all implicitly created directories, to be owned by the
574
+ specified group id, regardless of the `gid` field in the archive.
575
+ Cannot be used along with `preserveOwner`. Requires also setting a
576
+ `uid` option.
557
577
 
558
578
  ### class tar.Unpack.Sync
559
579
 
package/lib/mkdir.js CHANGED
@@ -3,6 +3,7 @@
3
3
  const mkdirp = require('mkdirp')
4
4
  const fs = require('fs')
5
5
  const path = require('path')
6
+ const chownr = require('chownr')
6
7
 
7
8
  class SymlinkError extends Error {
8
9
  constructor (symlink, path) {
@@ -23,17 +24,25 @@ const mkdir = module.exports = (dir, opt, cb) => {
23
24
  const mode = opt.mode | 0o0700
24
25
  const needChmod = (mode & umask) !== 0
25
26
 
27
+ const uid = opt.uid
28
+ const gid = opt.gid
29
+ const doChown = typeof uid === 'number' &&
30
+ typeof gid === 'number' &&
31
+ ( uid !== opt.processUid || gid !== opt.processGid )
32
+
26
33
  const preserve = opt.preserve
27
34
  const unlink = opt.unlink
28
35
  const cache = opt.cache
29
36
  const cwd = opt.cwd
30
37
 
31
- const done = er => {
38
+ const done = (er, created) => {
32
39
  if (er)
33
40
  cb(er)
34
41
  else {
35
42
  cache.set(dir, true)
36
- if (needChmod)
43
+ if (created && doChown)
44
+ chownr(created, uid, gid, er => done(er))
45
+ else if (needChmod)
37
46
  fs.chmod(dir, mode, cb)
38
47
  else
39
48
  cb()
@@ -48,39 +57,41 @@ const mkdir = module.exports = (dir, opt, cb) => {
48
57
 
49
58
  const sub = path.relative(cwd, dir)
50
59
  const parts = sub.split(/\/|\\/)
51
- mkdir_(cwd, parts, mode, cache, unlink, done)
60
+ mkdir_(cwd, parts, mode, cache, unlink, null, done)
52
61
  }
53
62
 
54
- const mkdir_ = (base, parts, mode, cache, unlink, cb) => {
63
+ const mkdir_ = (base, parts, mode, cache, unlink, created, cb) => {
55
64
  if (!parts.length)
56
- return cb()
65
+ return cb(null, created)
57
66
  const p = parts.shift()
58
67
  const part = base + '/' + p
59
68
  if (cache.get(part))
60
- return mkdir_(part, parts, mode, cache, unlink, cb)
61
- fs.mkdir(part, mode, onmkdir(part, parts, mode, cache, unlink, cb))
69
+ return mkdir_(part, parts, mode, cache, unlink, created, cb)
70
+ fs.mkdir(part, mode, onmkdir(part, parts, mode, cache, unlink, created, cb))
62
71
  }
63
72
 
64
- const onmkdir = (part, parts, mode, cache, unlink, cb) => er => {
73
+ const onmkdir = (part, parts, mode, cache, unlink, created, cb) => er => {
65
74
  if (er) {
66
75
  fs.lstat(part, (statEr, st) => {
67
76
  if (statEr)
68
77
  cb(statEr)
69
78
  else if (st.isDirectory())
70
- mkdir_(part, parts, mode, cache, unlink, cb)
79
+ mkdir_(part, parts, mode, cache, unlink, created, cb)
71
80
  else if (unlink)
72
81
  fs.unlink(part, er => {
73
82
  if (er)
74
83
  return cb(er)
75
- fs.mkdir(part, mode, onmkdir(part, parts, mode, cache, unlink, cb))
84
+ fs.mkdir(part, mode, onmkdir(part, parts, mode, cache, unlink, created, cb))
76
85
  })
77
86
  else if (st.isSymbolicLink())
78
87
  return cb(new SymlinkError(part, part + '/' + parts.join('/')))
79
88
  else
80
89
  cb(er)
81
90
  })
82
- } else
83
- mkdir_(part, parts, mode, cache, unlink, cb)
91
+ } else {
92
+ created = created || part
93
+ mkdir_(part, parts, mode, cache, unlink, created, cb)
94
+ }
84
95
  }
85
96
 
86
97
  const mkdirSync = module.exports.sync = (dir, opt) => {
@@ -90,13 +101,21 @@ const mkdirSync = module.exports.sync = (dir, opt) => {
90
101
  const mode = opt.mode | 0o0700
91
102
  const needChmod = (mode & umask) !== 0
92
103
 
104
+ const uid = opt.uid
105
+ const gid = opt.gid
106
+ const doChown = typeof uid === 'number' &&
107
+ typeof gid === 'number' &&
108
+ ( uid !== opt.processUid || gid !== opt.processGid )
109
+
93
110
  const preserve = opt.preserve
94
111
  const unlink = opt.unlink
95
112
  const cache = opt.cache
96
113
  const cwd = opt.cwd
97
114
 
98
- const done = er => {
115
+ const done = (created) => {
99
116
  cache.set(dir, true)
117
+ if (created && doChown)
118
+ chownr.sync(created, uid, gid)
100
119
  if (needChmod)
101
120
  fs.chmodSync(dir, mode)
102
121
  cache.set(dir, true)
@@ -105,14 +124,12 @@ const mkdirSync = module.exports.sync = (dir, opt) => {
105
124
  if (cache && cache.get(dir) === true || dir === cwd)
106
125
  return done()
107
126
 
108
- if (preserve) {
109
- mkdirp.sync(dir, mode)
110
- cache.set(dir, true)
111
- return
112
- }
127
+ if (preserve)
128
+ return done(mkdirp.sync(dir, mode))
113
129
 
114
130
  const sub = path.relative(cwd, dir)
115
131
  const parts = sub.split(/\/|\\/)
132
+ let created = null
116
133
  for (let p = parts.shift(), part = cwd;
117
134
  p && (part += '/' + p);
118
135
  p = parts.shift()) {
@@ -122,6 +139,7 @@ const mkdirSync = module.exports.sync = (dir, opt) => {
122
139
 
123
140
  try {
124
141
  fs.mkdirSync(part, mode)
142
+ created = created || part
125
143
  cache.set(part, true)
126
144
  } catch (er) {
127
145
  const st = fs.lstatSync(part)
@@ -131,6 +149,7 @@ const mkdirSync = module.exports.sync = (dir, opt) => {
131
149
  } else if (unlink) {
132
150
  fs.unlinkSync(part)
133
151
  fs.mkdirSync(part, mode)
152
+ created = created || part
134
153
  cache.set(part, true)
135
154
  continue
136
155
  } else if (st.isSymbolicLink())
@@ -138,5 +157,5 @@ const mkdirSync = module.exports.sync = (dir, opt) => {
138
157
  }
139
158
  }
140
159
 
141
- return done()
160
+ return done(created)
142
161
  }
package/lib/pack.js CHANGED
@@ -129,7 +129,7 @@ const Pack = warner(class Pack extends MiniPass {
129
129
  [ADDTARENTRY] (p) {
130
130
  const absolute = path.resolve(this.cwd, p.path)
131
131
  if (this.prefix)
132
- p.path = this.prefix + '/' + p.path.replace(/^\.\/*/, '')
132
+ p.path = this.prefix + '/' + p.path.replace(/^\.(\/+|$)/, '')
133
133
 
134
134
  // in this case, we don't have to wait for the stat
135
135
  if (!this.filter(p.path, p))
@@ -148,7 +148,7 @@ const Pack = warner(class Pack extends MiniPass {
148
148
  [ADDFSENTRY] (p) {
149
149
  const absolute = path.resolve(this.cwd, p)
150
150
  if (this.prefix)
151
- p = this.prefix + '/' + p.replace(/^\.\/*/, '')
151
+ p = this.prefix + '/' + p.replace(/^\.(\/+|$)/, '')
152
152
 
153
153
  this[QUEUE].push(new PackJob(p, absolute))
154
154
  this[PROCESS]()
@@ -0,0 +1,14 @@
1
+ // unix absolute paths are also absolute on win32, so we use this for both
2
+ const { isAbsolute, parse } = require('path').win32
3
+
4
+ // returns [root, stripped]
5
+ module.exports = path => {
6
+ let r = ''
7
+ while (isAbsolute(path)) {
8
+ // windows will think that //x/y/z has a "root" of //x/y/
9
+ const root = path.charAt(0) === '/' ? '/' : parse(path).root
10
+ path = path.substr(root.length)
11
+ r += root
12
+ }
13
+ return [r, path]
14
+ }
package/lib/unpack.js CHANGED
@@ -8,6 +8,7 @@ const path = require('path')
8
8
  const mkdir = require('./mkdir.js')
9
9
  const mkdirSync = mkdir.sync
10
10
  const wc = require('./winchars.js')
11
+ const stripAbsolutePath = require('./strip-absolute-path.js')
11
12
 
12
13
  const ONENTRY = Symbol('onEntry')
13
14
  const CHECKFS = Symbol('checkFs')
@@ -29,6 +30,8 @@ const ENDED = Symbol('ended')
29
30
  const MAYBECLOSE = Symbol('maybeClose')
30
31
  const SKIP = Symbol('skip')
31
32
  const DOCHOWN = Symbol('doChown')
33
+ const UID = Symbol('uid')
34
+ const GID = Symbol('gid')
32
35
 
33
36
  class Unpack extends Parser {
34
37
  constructor (opt) {
@@ -50,18 +53,33 @@ class Unpack extends Parser {
50
53
 
51
54
  this.dirCache = opt.dirCache || new Map()
52
55
 
53
- if (opt.preserveOwner === undefined)
56
+ if (typeof opt.uid === 'number' || typeof opt.gid === 'number') {
57
+ // need both or neither
58
+ if (typeof opt.uid !== 'number' || typeof opt.gid !== 'number')
59
+ throw new TypeError('cannot set owner without number uid and gid')
60
+ if (opt.preserveOwner)
61
+ throw new TypeError(
62
+ 'cannot preserve owner in archive and also set owner explicitly')
63
+ this.uid = opt.uid
64
+ this.gid = opt.gid
65
+ this.setOwner = true
66
+ } else {
67
+ this.uid = null
68
+ this.gid = null
69
+ this.setOwner = false
70
+ }
71
+
72
+ // default true for root
73
+ if (opt.preserveOwner === undefined && typeof opt.uid !== 'number')
54
74
  this.preserveOwner = process.getuid && process.getuid() === 0
55
75
  else
56
76
  this.preserveOwner = !!opt.preserveOwner
57
77
 
58
- this.processUid = this.preserveOwner && process.getuid ?
78
+ this.processUid = (this.preserveOwner || this.setOwner) && process.getuid ?
59
79
  process.getuid() : null
60
- this.processGid = this.preserveOwner && process.getgid ?
80
+ this.processGid = (this.preserveOwner || this.setOwner) && process.getgid ?
61
81
  process.getgid() : null
62
82
 
63
- this.umask = process.umask()
64
-
65
83
  // turn ><?| in filenames into 0xf000-higher encoded forms
66
84
  this.win32 = !!opt.win32 || process.platform === 'win32'
67
85
 
@@ -85,7 +103,8 @@ class Unpack extends Parser {
85
103
 
86
104
  this.cwd = path.resolve(opt.cwd || process.cwd())
87
105
  this.strip = +opt.strip || 0
88
- this.umask = typeof opt.umask === 'number' ? opt.umask : process.umask()
106
+ this.processUmask = process.umask()
107
+ this.umask = typeof opt.umask === 'number' ? opt.umask : this.processUmask
89
108
  // default mode for dirs created as parents
90
109
  this.dmode = opt.dmode || (0o0777 & (~this.umask))
91
110
  this.fmode = opt.fmode || (0o0666 & (~this.umask))
@@ -118,10 +137,10 @@ class Unpack extends Parser {
118
137
 
119
138
  // absolutes on posix are also absolutes on win32
120
139
  // so we only need to test this one to get both
121
- if (path.win32.isAbsolute(p)) {
122
- const parsed = path.win32.parse(p)
123
- this.warn('stripping ' + parsed.root + ' from absolute path', p)
124
- entry.path = p.substr(parsed.root.length)
140
+ const s = stripAbsolutePath(p)
141
+ if (s[0]) {
142
+ entry.path = s[1]
143
+ this.warn(`stripping ${s[0]} from absolute path`, p)
125
144
  }
126
145
  }
127
146
 
@@ -174,7 +193,11 @@ class Unpack extends Parser {
174
193
 
175
194
  [MKDIR] (dir, mode, cb) {
176
195
  mkdir(dir, {
177
- umask: this.umask,
196
+ uid: this.uid,
197
+ gid: this.gid,
198
+ processUid: this.processUid,
199
+ processGid: this.processGid,
200
+ umask: this.processUmask,
178
201
  preserve: this.preservePaths,
179
202
  unlink: this.unlink,
180
203
  cache: this.dirCache,
@@ -184,9 +207,26 @@ class Unpack extends Parser {
184
207
  }
185
208
 
186
209
  [DOCHOWN] (entry) {
210
+ // in preserve owner mode, chown if the entry doesn't match process
211
+ // in set owner mode, chown if setting doesn't match process
187
212
  return this.preserveOwner &&
188
213
  ( typeof entry.uid === 'number' && entry.uid !== this.processUid ||
189
214
  typeof entry.gid === 'number' && entry.gid !== this.processGid )
215
+ ||
216
+ ( typeof this.uid === 'number' && this.uid !== this.processUid ||
217
+ typeof this.gid === 'number' && this.gid !== this.processGid )
218
+ }
219
+
220
+ [UID] (entry) {
221
+ return typeof this.uid === 'number' ? this.uid
222
+ : typeof entry.uid === 'number' ? entry.uid
223
+ : this.processUid
224
+ }
225
+
226
+ [GID] (entry) {
227
+ return typeof this.gid === 'number' ? this.gid
228
+ : typeof entry.gid === 'number' ? entry.gid
229
+ : this.processGid
190
230
  }
191
231
 
192
232
  [FILE] (entry) {
@@ -209,7 +249,7 @@ class Unpack extends Parser {
209
249
  fs.utimes(entry.absolute, entry.atime || new Date(), entry.mtime, cb))
210
250
  if (this[DOCHOWN](entry))
211
251
  queue.push(cb =>
212
- fs.chown(entry.absolute, entry.uid, entry.gid || this.processGid, cb))
252
+ fs.chown(entry.absolute, this[UID](entry), this[GID](entry), cb))
213
253
  processQueue()
214
254
  })
215
255
  entry.pipe(stream)
@@ -237,7 +277,7 @@ class Unpack extends Parser {
237
277
  fs.utimes(entry.absolute, entry.atime || new Date(), entry.mtime, cb))
238
278
  if (this[DOCHOWN](entry))
239
279
  queue.push(cb =>
240
- fs.chown(entry.absolute, entry.uid, entry.gid || this.processGid, cb))
280
+ fs.chown(entry.absolute, this[UID](entry), this[GID](entry), cb))
241
281
 
242
282
  processQueue()
243
283
  })
@@ -376,7 +416,7 @@ class UnpackSync extends Unpack {
376
416
  }
377
417
  if (this[DOCHOWN](entry)) {
378
418
  try {
379
- fs.fchownSync(fd, entry.uid, entry.gid || this.processGid)
419
+ fs.fchownSync(fd, this[UID](entry), this[GID](entry))
380
420
  } catch (er) {}
381
421
  }
382
422
  try { fs.closeSync(fd) } catch (er) { this[ONERROR](er, entry) }
@@ -396,7 +436,7 @@ class UnpackSync extends Unpack {
396
436
  }
397
437
  if (this[DOCHOWN](entry)) {
398
438
  try {
399
- fs.chownSync(entry.absolute, entry.uid, entry.gid || this.processGid)
439
+ fs.chownSync(entry.absolute, this[UID](entry), this[GID](entry))
400
440
  } catch (er) {}
401
441
  }
402
442
  entry.resume()
@@ -405,7 +445,11 @@ class UnpackSync extends Unpack {
405
445
  [MKDIR] (dir, mode) {
406
446
  try {
407
447
  return mkdir.sync(dir, {
408
- umask: this.umask,
448
+ uid: this.uid,
449
+ gid: this.gid,
450
+ processUid: this.processUid,
451
+ processGid: this.processGid,
452
+ umask: this.processUmask,
409
453
  preserve: this.preservePaths,
410
454
  unlink: this.unlink,
411
455
  cache: this.dirCache,
@@ -24,6 +24,7 @@ const ONOPENFILE = Symbol('onopenfile')
24
24
  const CLOSE = Symbol('close')
25
25
  const warner = require('./warn-mixin.js')
26
26
  const winchars = require('./winchars.js')
27
+ const stripAbsolutePath = require('./strip-absolute-path.js')
27
28
 
28
29
  const WriteEntry = warner(class WriteEntry extends MiniPass {
29
30
  constructor (p, opt) {
@@ -47,12 +48,12 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
47
48
  if (typeof opt.onwarn === 'function')
48
49
  this.on('warn', opt.onwarn)
49
50
 
50
- if (!this.preservePaths && path.win32.isAbsolute(p)) {
51
- // absolutes on posix are also absolutes on win32
52
- // so we only need to test this one to get both
53
- const parsed = path.win32.parse(p)
54
- this.warn('stripping ' + parsed.root + ' from absolute path', p)
55
- this.path = p.substr(parsed.root.length)
51
+ if (!this.preservePaths) {
52
+ const s = stripAbsolutePath(this.path)
53
+ if (s[0]) {
54
+ this.path = s[1]
55
+ this.warn('stripping ' + s[0] + ' from absolute path', p)
56
+ }
56
57
  }
57
58
 
58
59
  this.win32 = !!opt.win32 || process.platform === 'win32'
@@ -320,13 +321,15 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
320
321
  if (typeof opt.onwarn === 'function')
321
322
  this.on('warn', opt.onwarn)
322
323
 
323
- if (path.isAbsolute(this.path) && !this.preservePaths) {
324
- const parsed = path.parse(this.path)
325
- this.warn(
326
- 'stripping ' + parsed.root + ' from absolute path',
327
- this.path
328
- )
329
- this.path = this.path.substr(parsed.root.length)
324
+ if (!this.preservePaths) {
325
+ const s = stripAbsolutePath(this.path)
326
+ if (s[0]) {
327
+ this.warn(
328
+ 'stripping ' + s[0] + ' from absolute path',
329
+ this.path
330
+ )
331
+ this.path = s[1]
332
+ }
330
333
  }
331
334
 
332
335
  this.remain = readEntry.size
package/package.json CHANGED
@@ -2,7 +2,10 @@
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": "3.1.14",
5
+ "version": "3.2.2",
6
+ "publishConfig": {
7
+ "tag": "v3-legacy"
8
+ },
6
9
  "repository": {
7
10
  "type": "git",
8
11
  "url": "https://github.com/npm/node-tar.git"
@@ -16,6 +19,7 @@
16
19
  "bench": "for i in benchmarks/*/*.js; do echo $i; for j in {1..5}; do node $i || break; done; done"
17
20
  },
18
21
  "dependencies": {
22
+ "chownr": "^1.0.1",
19
23
  "minipass": "^2.0.2",
20
24
  "minizlib": "^1.0.3",
21
25
  "mkdirp": "^0.5.0",