Comparing version 0.4.2 to 0.4.3
@@ -10,5 +10,5 @@ var fs = require('fs') | ||
var os = require('os') | ||
var filetime = 'ctime' | ||
exports.filetime = 'ctime' | ||
if (os.platform() == "win32") { | ||
filetime = 'mtime' | ||
exports.filetime = 'mtime' | ||
} | ||
@@ -96,3 +96,3 @@ | ||
fs.close(fd, function (er) { | ||
var age = Date.now() - st[filetime].getTime() | ||
var age = Date.now() - st[exports.filetime].getTime() | ||
return cb(er, age <= opts.stale) | ||
@@ -130,3 +130,3 @@ }) | ||
} | ||
var age = Date.now() - st[filetime].getTime() | ||
var age = Date.now() - st[exports.filetime].getTime() | ||
return (age <= opts.stale) | ||
@@ -138,3 +138,3 @@ } | ||
var req = 0 | ||
var req = 1 | ||
exports.lock = function (path, opts, cb) { | ||
@@ -174,26 +174,53 @@ if (typeof opts === 'function') cb = opts, opts = {} | ||
// someone's got this one. see if it's valid. | ||
if (opts.stale) fs.stat(path, function (statEr, st) { | ||
if (statEr) { | ||
if (statEr.code === 'ENOENT') { | ||
// expired already! | ||
var opts_ = Object.create(opts, { stale: { value: false }}) | ||
debug('lock stale enoent retry', path, opts_) | ||
exports.lock(path, opts_, cb) | ||
return | ||
} | ||
return cb(statEr) | ||
if (!opts.stale) return notStale(er, path, opts, cb) | ||
return maybeStale(er, path, opts, false, cb) | ||
}) | ||
} | ||
// Staleness checking algorithm | ||
// 1. acquire $lock, fail | ||
// 2. stat $lock, find that it is stale | ||
// 3. acquire $lock.STALE | ||
// 4. stat $lock, assert that it is still stale | ||
// 5. unlink $lock | ||
// 6. link $lock.STALE $lock | ||
// 7. unlink $lock.STALE | ||
// On any failure, clean up whatever we've done, and raise the error. | ||
function maybeStale (originalEr, path, opts, hasStaleLock, cb) { | ||
fs.stat(path, function (statEr, st) { | ||
if (statEr) { | ||
if (statEr.code === 'ENOENT') { | ||
// expired already! | ||
var opts_ = Object.create(opts, { stale: { value: false }}) | ||
debug('lock stale enoent retry', path, opts_) | ||
exports.lock(path, opts_, cb) | ||
return | ||
} | ||
return cb(statEr) | ||
} | ||
var age = Date.now() - st[filetime].getTime() | ||
if (age > opts.stale) { | ||
debug('lock stale', path, opts_) | ||
exports.unlock(path, function (er) { | ||
if (er) return cb(er) | ||
var opts_ = Object.create(opts, { stale: { value: false }}) | ||
debug('lock stale retry', path, opts_) | ||
exports.lock(path, opts_, cb) | ||
var age = Date.now() - st[exports.filetime].getTime() | ||
if (age <= opts.stale) return notStale(originalEr, path, opts, cb) | ||
debug('lock stale', path, opts_) | ||
if (hasStaleLock) { | ||
exports.unlock(path, function (er) { | ||
if (er) return cb(er) | ||
debug('lock stale retry', path, opts) | ||
fs.link(path + '.STALE', path, function (er) { | ||
fs.unlink(path + '.STALE', function () { | ||
// best effort. if the unlink fails, oh well. | ||
cb(er) | ||
}) | ||
}) | ||
} else notStale(er, path, opts, cb) | ||
}) | ||
else notStale(er, path, opts, cb) | ||
}) | ||
} else { | ||
debug('acquire .STALE file lock', opts) | ||
exports.lock(path + '.STALE', opts, function (er) { | ||
if (er) return cb(er) | ||
maybeStale(originalEr, path, opts, true, cb) | ||
}) | ||
} | ||
}) | ||
@@ -244,3 +271,3 @@ } | ||
var st = fs.statSync(path) | ||
var ct = st[filetime].getTime() | ||
var ct = st[exports.filetime].getTime() | ||
if (!(ct % 1000) && (opts.stale % 1000)) { | ||
@@ -247,0 +274,0 @@ // probably don't have subsecond resolution. |
{ | ||
"name": "lockfile", | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"main": "lockfile.js", | ||
@@ -5,0 +5,0 @@ "directories": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22584
10
603
4
3