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