@@ -217,7 +217,7 @@ function extractUpdated (entryStream, label, opts) {
217
217
// be output from it.
218
218
module . exports . _createCacheWriteStream = createCacheWriteStream
219
219
function createCacheWriteStream ( cacheFile , latest , opts ) {
220
- return _ensureCacheDirExists ( cacheFile , opts ) . then ( ( ) => {
220
+ return _ensureCacheDirExists ( cacheFile , opts ) . then ( ( { uid , gid } ) => {
221
221
log . silly ( 'all-package-metadata' , 'creating output stream' )
222
222
const outStream = _createCacheOutStream ( )
223
223
const cacheFileStream = writeStreamAtomic ( cacheFile )
@@ -231,19 +231,31 @@ function createCacheWriteStream (cacheFile, latest, opts) {
231
231
let errEmitted = false
232
232
linkStreams ( inputStream , outStream , ( ) => { errEmitted = true } )
233
233
234
- cacheFileStream . on ( 'close' , ( ) => ! errEmitted && outStream . end ( ) )
234
+ cacheFileStream . on ( 'close' , ( ) => {
235
+ if ( ! errEmitted ) {
236
+ if ( typeof uid === 'number' &&
237
+ typeof gid === 'number' &&
238
+ process . getuid &&
239
+ process . getgid &&
240
+ ( process . getuid ( ) !== uid || process . getgid ( ) !== gid ) ) {
241
+ chownr . sync ( cacheFile , uid , gid )
242
+ }
243
+ outStream . end ( )
244
+ }
245
+ } )
235
246
236
247
return ms . duplex . obj ( inputStream , outStream )
237
248
} )
238
249
}
239
250
251
+ // return the {uid,gid} that the cache should have
240
252
function _ensureCacheDirExists ( cacheFile , opts ) {
241
253
var cacheBase = path . dirname ( cacheFile )
242
254
log . silly ( 'all-package-metadata' , 'making sure cache dir exists at' , cacheBase )
243
255
return correctMkdir ( opts . cache ) . then ( st => {
244
256
return mkdir ( cacheBase ) . then ( made => {
245
257
return chownr ( made || cacheBase , st . uid , st . gid )
246
- } )
258
+ } ) . then ( ( ) => ( { uid : st . uid , gid : st . gid } ) )
247
259
} )
248
260
}
249
261
0 commit comments