@@ -5,6 +5,13 @@ const Header = require('./header.js')
55const fs = require ( 'fs' )
66const path = require ( 'path' )
77
8+ const prefixPath = ( path , prefix ) => {
9+ if ( ! prefix )
10+ return path
11+ path = path . replace ( / ^ \. ( [ / \\ ] | $ ) / , '' )
12+ return prefix + '/' + path
13+ }
14+
815const maxReadSize = 16 * 1024 * 1024
916const PROCESS = Symbol ( 'process' )
1017const FILE = Symbol ( 'file' )
@@ -23,6 +30,7 @@ const CLOSE = Symbol('close')
2330const MODE = Symbol ( 'mode' )
2431const AWAITDRAIN = Symbol ( 'awaitDrain' )
2532const ONDRAIN = Symbol ( 'ondrain' )
33+ const PREFIX = Symbol ( 'prefix' )
2634const warner = require ( './warn-mixin.js' )
2735const winchars = require ( './winchars.js' )
2836const stripAbsolutePath = require ( './strip-absolute-path.js' )
@@ -50,6 +58,7 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
5058 this . noPax = ! ! opt . noPax
5159 this . noMtime = ! ! opt . noMtime
5260 this . mtime = opt . mtime || null
61+ this . prefix = opt . prefix || null
5362
5463 this . fd = null
5564 this . blockLen = null
@@ -128,13 +137,19 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
128137 return modeFix ( mode , this . type === 'Directory' , this . portable )
129138 }
130139
140+ [ PREFIX ] ( path ) {
141+ return prefixPath ( path , this . prefix )
142+ }
143+
131144 [ HEADER ] ( ) {
132145 if ( this . type === 'Directory' && this . portable )
133146 this . noMtime = true
134147
135148 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 ,
138153 // only the permissions and setuid/setgid/sticky bitflags
139154 // not the higher-order bits that specify file type
140155 mode : this [ MODE ] ( this . stat . mode ) ,
@@ -155,8 +170,9 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
155170 ctime : this . portable ? null : this . header . ctime ,
156171 gid : this . portable ? null : this . header . gid ,
157172 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 ,
160176 size : this . header . size ,
161177 uid : this . portable ? null : this . header . uid ,
162178 uname : this . portable ? null : this . header . uname ,
@@ -385,6 +401,8 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
385401 if ( this . type === 'Directory' && this . portable )
386402 this . noMtime = true
387403
404+ this . prefix = opt . prefix || null
405+
388406 this . path = readEntry . path
389407 this . mode = this [ MODE ] ( readEntry . mode )
390408 this . uid = this . portable ? null : readEntry . uid
@@ -413,8 +431,9 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
413431 this . blockRemain = readEntry . startBlockSize
414432
415433 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 ,
418437 // only the permissions and setuid/setgid/sticky bitflags
419438 // not the higher-order bits that specify file type
420439 mode : this . mode ,
@@ -441,8 +460,9 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
441460 ctime : this . portable ? null : this . ctime ,
442461 gid : this . portable ? null : this . gid ,
443462 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 ,
446466 size : this . size ,
447467 uid : this . portable ? null : this . uid ,
448468 uname : this . portable ? null : this . uname ,
@@ -456,6 +476,10 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
456476 readEntry . pipe ( this )
457477 }
458478
479+ [ PREFIX ] ( path ) {
480+ return prefixPath ( path , this . prefix )
481+ }
482+
459483 [ MODE ] ( mode ) {
460484 return modeFix ( mode , this . type === 'Directory' , this . portable )
461485 }
0 commit comments