@@ -7,7 +7,13 @@ const ReadEntry = require('./read-entry.js')
77const fs = require ( 'fs' )
88const path = require ( 'path' )
99
10- const types = require ( './types.js' )
10+ const prefixPath = ( path , prefix ) => {
11+ if ( ! prefix )
12+ return path
13+ path = path . replace ( / ^ \. ( [ / \\ ] | $ ) / , '' )
14+ return prefix + '/' + path
15+ }
16+
1117const maxReadSize = 16 * 1024 * 1024
1218const PROCESS = Symbol ( 'process' )
1319const FILE = Symbol ( 'file' )
@@ -26,6 +32,7 @@ const CLOSE = Symbol('close')
2632const MODE = Symbol ( 'mode' )
2733const AWAITDRAIN = Symbol ( 'awaitDrain' )
2834const ONDRAIN = Symbol ( 'ondrain' )
35+ const PREFIX = Symbol ( 'prefix' )
2936const warner = require ( './warn-mixin.js' )
3037const winchars = require ( './winchars.js' )
3138const stripAbsolutePath = require ( './strip-absolute-path.js' )
@@ -53,6 +60,7 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
5360 this . noPax = ! ! opt . noPax
5461 this . noMtime = ! ! opt . noMtime
5562 this . mtime = opt . mtime || null
63+ this . prefix = opt . prefix || null
5664
5765 this . fd = null
5866 this . blockLen = null
@@ -123,13 +131,19 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
123131 return modeFix ( mode , this . type === 'Directory' )
124132 }
125133
134+ [ PREFIX ] ( path ) {
135+ return prefixPath ( path , this . prefix )
136+ }
137+
126138 [ HEADER ] ( ) {
127139 if ( this . type === 'Directory' && this . portable )
128140 this . noMtime = true
129141
130142 this . header = new Header ( {
131- path : this . path ,
132- linkpath : this . linkpath ,
143+ path : this [ PREFIX ] ( this . path ) ,
144+ // only apply the prefix to hard links.
145+ linkpath : this . type === 'Link' ? this [ PREFIX ] ( this . linkpath )
146+ : this . linkpath ,
133147 // only the permissions and setuid/setgid/sticky bitflags
134148 // not the higher-order bits that specify file type
135149 mode : this [ MODE ] ( this . stat . mode ) ,
@@ -150,8 +164,9 @@ const WriteEntry = warner(class WriteEntry extends MiniPass {
150164 ctime : this . portable ? null : this . header . ctime ,
151165 gid : this . portable ? null : this . header . gid ,
152166 mtime : this . noMtime ? null : this . mtime || this . header . mtime ,
153- path : this . path ,
154- linkpath : this . linkpath ,
167+ path : this [ PREFIX ] ( this . path ) ,
168+ linkpath : this . type === 'Link' ? this [ PREFIX ] ( this . linkpath )
169+ : this . linkpath ,
155170 size : this . header . size ,
156171 uid : this . portable ? null : this . header . uid ,
157172 uname : this . portable ? null : this . header . uname ,
@@ -385,6 +400,8 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
385400 if ( this . type === 'Directory' && this . portable )
386401 this . noMtime = true
387402
403+ this . prefix = opt . prefix || null
404+
388405 this . path = readEntry . path
389406 this . mode = this [ MODE ] ( readEntry . mode )
390407 this . uid = this . portable ? null : readEntry . uid
@@ -415,8 +432,9 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
415432 this . blockRemain = readEntry . startBlockSize
416433
417434 this . header = new Header ( {
418- path : this . path ,
419- linkpath : this . linkpath ,
435+ path : this [ PREFIX ] ( this . path ) ,
436+ linkpath : this . type === 'Link' ? this [ PREFIX ] ( this . linkpath )
437+ : this . linkpath ,
420438 // only the permissions and setuid/setgid/sticky bitflags
421439 // not the higher-order bits that specify file type
422440 mode : this . mode ,
@@ -436,8 +454,9 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
436454 ctime : this . portable ? null : this . ctime ,
437455 gid : this . portable ? null : this . gid ,
438456 mtime : this . noMtime ? null : this . mtime ,
439- path : this . path ,
440- linkpath : this . linkpath ,
457+ path : this [ PREFIX ] ( this . path ) ,
458+ linkpath : this . type === 'Link' ? this [ PREFIX ] ( this . linkpath )
459+ : this . linkpath ,
441460 size : this . size ,
442461 uid : this . portable ? null : this . uid ,
443462 uname : this . portable ? null : this . uname ,
@@ -450,6 +469,10 @@ const WriteEntryTar = warner(class WriteEntryTar extends MiniPass {
450469 readEntry . pipe ( this )
451470 }
452471
472+ [ PREFIX ] ( path ) {
473+ return prefixPath ( path , this . prefix )
474+ }
475+
453476 [ MODE ] ( mode ) {
454477 return modeFix ( mode , this . type === 'Directory' )
455478 }
0 commit comments