Skip to content

Commit

Permalink
add a workaround for EPERM issue on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
rlidwka committed Nov 1, 2014
1 parent 7a7d794 commit 494dc0f
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion lib/local-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,31 @@ function write(dest, data, cb) {
var tmpname = dest + '.tmp' + String(Math.random()).substr(2)
fs.writeFile(tmpname, data, function(err) {
if (err) return cb(err)
return fs.rename(tmpname, dest, cb)
fs.rename(tmpname, dest, function(err) {
if (err) fs.unlink(tmpname)
if (err && err.code === 'EPERM' && process.platform.match(/^win/)) {
// windows isn't seem to be compatible with our locking
// algorithm, here is a simplified issue:
//
// fd = fs.openSync('file')
// fs.writeFileSync('file.tmp', 'blah')
// fs.renameSync('file.tmp', 'file')
// fs.closeSync(fd)
//
// fs.writeFileSync seem to work fine here, as long as
// you don't run multiple instances on windows
//
// fs.writeFile might still be prone to race conditions
// (not sure)
err = null
try {
fs.writeFileSync(dest, data)
} catch(_err) {
err = _err
}
}
cb(err)
})
})
}

Expand Down

0 comments on commit 494dc0f

Please sign in to comment.