diff --git a/lib/entry-index.js b/lib/entry-index.js index c24c539..087dbfd 100644 --- a/lib/entry-index.js +++ b/lib/entry-index.js @@ -30,16 +30,16 @@ module.exports.insert = insert function insert (cache, key, digest, opts) { opts = opts || {} const bucket = bucketPath(cache, key) + const entry = { + key: key, + digest: digest, + hashAlgorithm: opts.hashAlgorithm || 'sha512', + time: +(new Date()), + metadata: opts.metadata + } return fixOwner.mkdirfix( path.dirname(bucket), opts.uid, opts.gid ).then(() => { - const entry = { - key: key, - digest: digest, - hashAlgorithm: opts.hashAlgorithm || 'sha512', - time: +(new Date()), - metadata: opts.metadata - } const stringified = JSON.stringify(entry) // NOTE - Cleverness ahoy! // @@ -50,12 +50,18 @@ function insert (cache, key, digest, opts) { // Thanks to @isaacs for the whiteboarding session that ended up with this. return appendFileAsync( bucket, `\n${hashEntry(stringified)}\t${stringified}` - ).then(() => entry) - }).then(entry => ( - fixOwner.chownr(bucket, opts.uid, opts.gid).then(() => ( - formatEntry(cache, entry) - )) - )) + ) + }).then( + () => fixOwner.chownr(bucket, opts.uid, opts.gid) + ).catch({code: 'ENOENT'}, () => { + // There's a class of race conditions that happen when things get deleted + // during fixOwner, or between the two mkdirfix/chownr calls. + // + // It's perfectly fine to just not bother in those cases and lie + // that the index entry was written. Because it's a cache. + }).then(() => { + return formatEntry(cache, entry) + }) } module.exports.find = find