@@ -5,6 +5,13 @@ const Header = require('./header.js')
5
5
const fs = require ( 'fs' )
6
6
const path = require ( 'path' )
7
7
8
+ const prefixPath = ( path , prefix ) => {
9
+ if ( ! prefix )
10
+ return path
11
+ path = path . replace ( / ^ \. ( [ / \\ ] | $ ) / , '' )
12
+ return prefix + '/' + path
13
+ }
14
+
8
15
const maxReadSize = 16 * 1024 * 1024
9
16
const PROCESS = Symbol ( 'process' )
10
17
const FILE = Symbol ( 'file' )
@@ -23,6 +30,7 @@ const CLOSE = Symbol('close')
23
30
const MODE = Symbol ( 'mode' )
24
31
const AWAITDRAIN = Symbol ( 'awaitDrain' )
25
32
const ONDRAIN = Symbol ( 'ondrain' )
33
+ const PREFIX = Symbol ( 'prefix' )
26
34
const warner = require ( './warn-mixin.js' )
27
35
const winchars = require ( './winchars.js' )
28
36
const stripAbsolutePath = require ( './strip-absolute-path.js' )
@@ -50,6 +58,7 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
50
58
this . noPax = ! ! opt . noPax
51
59
this . noMtime = ! ! opt . noMtime
52
60
this . mtime = opt . mtime || null
61
+ this . prefix = opt . prefix || null
53
62
54
63
this . fd = null
55
64
this . blockLen = null
@@ -128,13 +137,19 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
128
137
return modeFix ( mode , this . type === 'Directory' , this . portable )
129
138
}
130
139
140
+ [ PREFIX ] ( path ) {
141
+ return prefixPath ( path , this . prefix )
142
+ }
143
+
131
144
[ HEADER ] ( ) {
132
145
if ( this . type === 'Directory' && this . portable )
133
146
this . noMtime = true
134
147
135
148
this . header = new Header ( {
136
- path : this . path ,
137
- linkpath : this . linkpath ,
149
+ path : this [ PREFIX ] ( this . path ) ,
150
+ // only apply the prefix to hard links.
151
+ linkpath : this . type === 'Link' ? this [ PREFIX ] ( this . linkpath )
152
+ : this . linkpath ,
138
153
// only the permissions and setuid/setgid/sticky bitflags
139
154
// not the higher-order bits that specify file type
140
155
mode : this [ MODE ] ( this . stat . mode ) ,
@@ -155,8 +170,9 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
155
170
ctime : this . portable ? null : this . header . ctime ,
156
171
gid : this . portable ? null : this . header . gid ,
157
172
mtime : this . noMtime ? null : this . mtime || this . header . mtime ,
158
- path : this . path ,
159
- linkpath : this . linkpath ,
173
+ path : this [ PREFIX ] ( this . path ) ,
174
+ linkpath : this . type === 'Link' ? this [ PREFIX ] ( this . linkpath )
175
+ : this . linkpath ,
160
176
size : this . header . size ,
161
177
uid : this . portable ? null : this . header . uid ,
162
178
uname : this . portable ? null : this . header . uname ,
@@ -385,6 +401,8 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
385
401
if ( this . type === 'Directory' && this . portable )
386
402
this . noMtime = true
387
403
404
+ this . prefix = opt . prefix || null
405
+
388
406
this . path = readEntry . path
389
407
this . mode = this [ MODE ] ( readEntry . mode )
390
408
this . uid = this . portable ? null : readEntry . uid
@@ -413,8 +431,9 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
413
431
this . blockRemain = readEntry . startBlockSize
414
432
415
433
this . header = new Header ( {
416
- path : this . path ,
417
- linkpath : this . linkpath ,
434
+ path : this [ PREFIX ] ( this . path ) ,
435
+ linkpath : this . type === 'Link' ? this [ PREFIX ] ( this . linkpath )
436
+ : this . linkpath ,
418
437
// only the permissions and setuid/setgid/sticky bitflags
419
438
// not the higher-order bits that specify file type
420
439
mode : this . mode ,
@@ -441,8 +460,9 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
441
460
ctime : this . portable ? null : this . ctime ,
442
461
gid : this . portable ? null : this . gid ,
443
462
mtime : this . noMtime ? null : this . mtime ,
444
- path : this . path ,
445
- linkpath : this . linkpath ,
463
+ path : this [ PREFIX ] ( this . path ) ,
464
+ linkpath : this . type === 'Link' ? this [ PREFIX ] ( this . linkpath )
465
+ : this . linkpath ,
446
466
size : this . size ,
447
467
uid : this . portable ? null : this . uid ,
448
468
uname : this . portable ? null : this . uname ,
@@ -456,6 +476,10 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
456
476
readEntry . pipe ( this )
457
477
}
458
478
479
+ [ PREFIX ] ( path ) {
480
+ return prefixPath ( path , this . prefix )
481
+ }
482
+
459
483
[ MODE ] ( mode ) {
460
484
return modeFix ( mode , this . type === 'Directory' , this . portable )
461
485
}
0 commit comments