Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Commit

Permalink
fix(index): ignore index removal races when inserting
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Apr 1, 2017
1 parent dc6482d commit b9d2fa2
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions lib/entry-index.js
Expand Up @@ -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!
//
Expand All @@ -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
Expand Down

0 comments on commit b9d2fa2

Please sign in to comment.