Permalink
Browse files

Add lock-file dependency

  • Loading branch information...
1 parent c174a63 commit c40946cb4d4ef79f715f9c10da5ee9bc6111e9ea @isaacs isaacs committed Jun 27, 2012
View
74 node_modules/lock-file/README.md
@@ -0,0 +1,74 @@
+# lock-file
+
+A very polite lock file utility, which endeavors to not litter, and to
+wait patiently for others.
+
+## Usage
+
+```javascript
+var lockFile = require('lock-file')
+
+// opts is optional, and defaults to {}
+lockFile.lock('some-file.lock', opts, function (er, fd) {
+ // if the er happens, then it failed to acquire a lock.
+ // if there was not an error, then the fd is opened in
+ // wx mode. If you want to write something to it, go ahead.
+
+ // do my stuff, free of interruptions
+ // then, some time later, do:
+ lockFile.unlock('some-file.lock', function (er) {
+ // er means that an error happened, and is probably bad.
+ })
+})
+```
+
+## Methods
+
+Sync methods return the value/throw the error, others don't. Standard
+node fs stuff.
+
+All known locks are removed when the process exits. Of course, it's
+possible for certain types of failures to cause this to fail, but a best
+effort is made to not be a litterbug.
+
+### lockFile.lock(path, [opts], cb)
+
+Acquire a file lock on the specified path. Returns the FD.
+
+### lockFile.lockSync(path, [opts])
+
+Acquire a file lock on the specified path
+
+### lockFile.unlock(path, cb)
+
+Close and unlink the lockfile.
+
+### lockFile.unlockSync(path)
+
+Close and unlink the lockfile.
+
+### lockFile.check(path, [opts], cb)
+
+Check if the lockfile is locked and not stale.
+
+Returns boolean.
+
+### lockFile.checkSync(path, [opts], cb)
+
+Check if the lockfile is locked and not stale.
+
+Callback is called with `cb(error, isLocked)`.
+
+## Options
+
+### opts.wait
+
+A number of milliseconds to wait for locks to expire before giving up.
+Only used by lockFile.lock. Relies on fs.watch. If the lock is not
+cleared by the time the wait expires, then it returns with the original
+error.
+
+### opts.stale
+
+A number of milliseconds before locks are considered to have expired.
+
View
186 node_modules/lock-file/lock-file.js
@@ -0,0 +1,186 @@
+var fs = require('fs')
+
+var wx = 'wx'
+if (process.version.match(/^v0.[456]/)) {
+ var c = require('constants')
+ wx = c.O_TRUNC | c.O_CREAT | c.O_WRONLY | c.O_EXCL
+}
+
+var locks = {}
+
+process.on('exit', function () {
+ console.error('lock exit')
+ // cleanup
+ Object.keys(locks).forEach(exports.unlockSync)
+})
+
+// XXX https://github.com/joyent/node/issues/3555
+// Remove when node 0.8 is deprecated.
+process.on('uncaughtException', function H (er) {
+ var l = process.listeners('uncaughtException').filter(function (h) {
+ return h !== H
+ })
+ if (!l.length) {
+ // cleanup
+ Object.keys(locks).forEach(exports.unlockSync)
+ process.removeListener(H)
+ throw er
+ }
+})
+
+exports.unlock = function (path, cb) {
+ // best-effort. unlocking an already-unlocked lock is a noop
+ fs.unlink(path, function (unlinkEr) {
+ if (!locks.hasOwnProperty(path)) return cb()
+ fs.close(locks[path], function (closeEr) {
+ delete locks[path]
+ cb()
+ })
+ })
+}
+
+exports.unlockSync = function (path) {
+ try { fs.unlinkSync(path) } catch (er) {}
+ if (!locks.hasOwnProperty(path)) return
+ // best-effort. unlocking an already-unlocked lock is a noop
+ try { fs.close(locks[path]) } catch (er) {}
+ delete locks[path]
+}
+
+
+// if the file can be opened in readonly mode, then it's there.
+// if the error is something other than ENOENT, then it's not.
+exports.check = function (path, opts, cb) {
+ if (typeof opts === 'function') cb = opts, opts = {}
+ fs.open(path, 'r', function (er, fd) {
+ if (er) {
+ if (er.code !== 'ENOENT') return cb(er)
+ return cb(null, false)
+ }
+
+ if (!opts.stale) {
+ return fs.close(fd, function (er) {
+ return cb(er, true)
+ })
+ }
+
+ fs.fstat(fd, function (er, st) {
+ if (er) return fs.close(fd, function (er2) {
+ return cb(er)
+ })
+
+ fs.close(fd, function (er) {
+ var age = Date.now() - st.ctime.getTime()
+ return cb(er, age <= opts.stale)
+ })
+ })
+ })
+}
+
+exports.checkSync = function (path, opts) {
+ opts = opts || {}
+ if (opts.wait) {
+ throw new Error('opts.wait not supported sync for obvious reasons')
+ }
+
+ try {
+ var fd = fs.openSync(path, 'r')
+ } catch (er) {
+ if (er.code !== 'ENOENT') throw er
+ return false
+ }
+
+ if (!opts.stale) {
+ fs.closeSync(fd)
+ return true
+ }
+
+ // file exists. however, might be stale
+ if (opts.stale) {
+ try {
+ var st = fs.fstatSync(fd)
+ } finally {
+ fs.closeSync(fd)
+ }
+ var age = Date.now() - st.ctime.getTime()
+ return (age <= opts.stale)
+ }
+}
+
+
+
+exports.lock = function (path, opts, cb) {
+ if (typeof opts === 'function') cb = opts, opts = {}
+ // try to engage the lock.
+ // if this succeeds, then we're in business.
+ fs.open(path, wx, function (er, fd) {
+ if (!er) {
+ locks[path] = fd
+ return cb(null, fd)
+ }
+
+ // something other than "currently locked"
+ // maybe eperm or something.
+ if (er.code !== 'EEXIST') return cb(er)
+
+ // someone's got this one. see if it's valid.
+ if (opts.stale) {
+ return fs.stat(path, function (er, st) {
+ var age = Date.now() - st.ctime.getTime()
+ if (age > opts.stale) {
+ exports.unlock(path, function (er) {
+ if (er) return cb(er)
+ var opts_ = Object.create(opts, { stale: { value: false }})
+ exports.lock(path, opts_, cb)
+ })
+ }
+ })
+ } else if (opts.wait) {
+ // wait for some ms for the lock to clear
+ var watcher = fs.watch(path, function (change) {
+ if (change === 'rename') {
+ // ok, try and get it now.
+ watcher.close()
+ clearTimeout(timer)
+ var opts_ = Object.create(opts, { wait: { value: false }})
+ exports.lock(path, opts_, cb)
+ }
+ })
+ var timer = setTimeout(function () {
+ watcher.close()
+ cb(er)
+ }, opts.wait)
+ } else {
+ // failed to lock!
+ return cb(er)
+ }
+ })
+}
+
+exports.lockSync = function (path, opts) {
+ opts = opts || {}
+ if (opts.wait) {
+ throw new Error('opts.wait not supported sync for obvious reasons')
+ }
+
+ try {
+ var fd = fs.openSync(path, wx)
+ locks[path] = fd
+ return fd
+ } catch (er) {
+ if (er.code !== 'EEXIST') throw er
+
+ if (opts.stale) {
+ var st = fs.statSync(path)
+ var age = Date.now() - st.ctime.getTime()
+ if (age > opts.stale) {
+ exports.unlockSync(path)
+ var opts_ = Object.create(opts, { stale: { value: false }})
+ return exports.lockSync(path, opts_)
+ }
+ }
+
+ // failed to lock!
+ throw er
+ }
+}
View
39 node_modules/lock-file/package.json
@@ -0,0 +1,39 @@
+{
+ "name": "lock-file",
+ "version": "0.0.1",
+ "main": "lock-file.js",
+ "directories": {
+ "test": "test"
+ },
+ "dependencies": {},
+ "devDependencies": {
+ "tap": "~0.2.5"
+ },
+ "scripts": {
+ "test": "tap test/*.js"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/isaacs/lock-file"
+ },
+ "keywords": [
+ "lockfile",
+ "lock",
+ "file",
+ "fs",
+ "O_EXCL"
+ ],
+ "author": {
+ "name": "Isaac Z. Schlueter",
+ "email": "i@izs.me",
+ "url": "http://blog.izs.me/"
+ },
+ "license": "BSD",
+ "description": "A very polite lock file utility, which endeavors to not litter, and to wait patiently for others.",
+ "readme": "# lock-file\n\nA very polite lock file utility, which endeavors to not litter, and to\nwait patiently for others.\n\n## Usage\n\n```javascript\nvar lockFile = require('lock-file')\n\n// opts is optional, and defaults to {}\nlockFile.lock('some-file.lock', opts, function (er, fd) {\n // if the er happens, then it failed to acquire a lock.\n // if there was not an error, then the fd is opened in\n // wx mode. If you want to write something to it, go ahead.\n\n // do my stuff, free of interruptions\n // then, some time later, do:\n lockFile.unlock('some-file.lock', function (er) {\n // er means that an error happened, and is probably bad.\n })\n})\n```\n\n## Methods\n\nSync methods return the value/throw the error, others don't. Standard\nnode fs stuff.\n\nAll known locks are removed when the process exits. Of course, it's\npossible for certain types of failures to cause this to fail, but a best\neffort is made to not be a litterbug.\n\n### lockFile.lock(path, [opts], cb)\n\nAcquire a file lock on the specified path. Returns the FD.\n\n### lockFile.lockSync(path, [opts])\n\nAcquire a file lock on the specified path\n\n### lockFile.unlock(path, cb)\n\nClose and unlink the lockfile.\n\n### lockFile.unlockSync(path)\n\nClose and unlink the lockfile.\n\n### lockFile.check(path, [opts], cb)\n\nCheck if the lockfile is locked and not stale.\n\nReturns boolean.\n\n### lockFile.checkSync(path, [opts], cb)\n\nCheck if the lockfile is locked and not stale.\n\nCallback is called with `cb(error, isLocked)`.\n\n## Options\n\n### opts.wait\n\nA number of milliseconds to wait for locks to expire before giving up.\nOnly used by lockFile.lock. Relies on fs.watch. If the lock is not\ncleared by the time the wait expires, then it returns with the original\nerror.\n\n### opts.stale\n\nA number of milliseconds before locks are considered to have expired.\n\n",
+ "_id": "lock-file@0.0.1",
+ "dist": {
+ "shasum": "c8d18edf9b0b509de20846aa0a83564f619b57d8"
+ },
+ "_from": "lock-file@0.0.1"
+}
View
154 node_modules/lock-file/test/basic.js
@@ -0,0 +1,154 @@
+var test = require('tap').test
+var lockFile = require('../lock-file.js')
+var path = require('path')
+var fs = require('fs')
+
+test('setup', function (t) {
+ try { lockFile.unlockSync('basic-lock') } catch (er) {}
+ try { lockFile.unlockSync('sync-lock') } catch (er) {}
+ try { lockFile.unlockSync('never-forget') } catch (er) {}
+ try { lockFile.unlockSync('stale-lock') } catch (er) {}
+ try { lockFile.unlockSync('watch-lock') } catch (er) {}
+ t.end()
+})
+
+test('basic test', function (t) {
+ lockFile.check('basic-lock', function (er, locked) {
+ if (er) throw er
+ t.notOk(locked)
+ lockFile.lock('basic-lock', function (er) {
+ if (er) throw er
+ lockFile.lock('basic-lock', function (er) {
+ t.ok(er)
+ lockFile.check('basic-lock', function (er, locked) {
+ if (er) throw er
+ t.ok(locked)
+ lockFile.unlock('basic-lock', function (er) {
+ if (er) throw er
+ lockFile.check('basic-lock', function (er, locked) {
+ if (er) throw er
+ t.notOk(locked)
+ t.end()
+ })
+ })
+ })
+ })
+ })
+ })
+})
+
+test('sync test', function (t) {
+ var locked
+ locked = lockFile.checkSync('sync-lock')
+ t.notOk(locked)
+ lockFile.lockSync('sync-lock')
+ locked = lockFile.checkSync('sync-lock')
+ t.ok(locked)
+ lockFile.unlockSync('sync-lock')
+ locked = lockFile.checkSync('sync-lock')
+ t.notOk(locked)
+ t.end()
+})
+
+test('exit cleanup test', function (t) {
+ var child = require.resolve('./fixtures/child.js')
+ var node = process.execPath
+ var spawn = require('child_process').spawn
+ spawn(node, [child]).on('exit', function () {
+ setTimeout(function () {
+ var locked = lockFile.checkSync('never-forget')
+ t.notOk(locked)
+ t.end()
+ }, 100)
+ })
+})
+
+test('error exit cleanup test', function (t) {
+ var child = require.resolve('./fixtures/bad-child.js')
+ var node = process.execPath
+ var spawn = require('child_process').spawn
+ spawn(node, [child]).on('exit', function () {
+ setTimeout(function () {
+ var locked = lockFile.checkSync('never-forget')
+ t.notOk(locked)
+ t.end()
+ }, 100)
+ })
+})
+
+
+test('staleness test', function (t) {
+ lockFile.lock('stale-lock', function (er) {
+ if (er) throw er
+
+ var opts = { stale: 1 }
+ setTimeout(next, 10)
+ function next () {
+ lockFile.check('stale-lock', opts, function (er, locked) {
+ if (er) throw er
+ t.notOk(locked)
+ lockFile.lock('stale-lock', opts, function (er) {
+ if (er) throw er
+ lockFile.unlock('stale-lock', function (er) {
+ if (er) throw er
+ t.end()
+ })
+ })
+ })
+ }
+ })
+})
+
+test('staleness sync test', function (t) {
+ var opts = { stale: 1 }
+ lockFile.lockSync('stale-lock')
+ setTimeout(next, 10)
+ function next () {
+ var locked
+ locked = lockFile.checkSync('stale-lock', opts)
+ t.notOk(locked)
+ lockFile.lockSync('stale-lock', opts)
+ lockFile.unlockSync('stale-lock')
+ t.end()
+ }
+})
+
+test('watch test', function (t) {
+ var opts = { wait: 100 }
+ var fdx
+ lockFile.lock('watch-lock', function (er, fd1) {
+ if (er) throw er
+ setTimeout(unlock, 10)
+ function unlock () {
+ console.error('unlocking it')
+ lockFile.unlockSync('watch-lock')
+ // open another file, so the fd gets reused
+ // so we can know that it actually re-opened it fresh,
+ // rather than just getting the same lock as before.
+ fdx = fs.openSync('x', 'w')
+ fdy = fs.openSync('x', 'w')
+ }
+
+ // should have gotten a new fd
+ lockFile.lock('watch-lock', opts, function (er, fd2) {
+ if (er) throw er
+ t.notEqual(fd1, fd2)
+ fs.closeSync(fdx)
+ fs.closeSync(fdy)
+ fs.unlinkSync('x')
+ lockFile.unlockSync('watch-lock')
+ t.end()
+ })
+ })
+})
+
+
+test('cleanup', function (t) {
+ try { lockFile.unlockSync('basic-lock') } catch (er) {}
+ try { lockFile.unlockSync('sync-lock') } catch (er) {}
+ try { lockFile.unlockSync('never-forget') } catch (er) {}
+ try { lockFile.unlockSync('stale-lock') } catch (er) {}
+ try { lockFile.unlockSync('watch-lock') } catch (er) {}
+ t.end()
+})
+
View
5 node_modules/lock-file/test/fixtures/bad-child.js
@@ -0,0 +1,5 @@
+var lockFile = require('../../lock-file.js')
+
+lockFile.lockSync('never-forget')
+
+throw new Error('waaaaaaaaa')
View
3 node_modules/lock-file/test/fixtures/child.js
@@ -0,0 +1,3 @@
+var lockFile = require('../../lock-file.js')
+
+lockFile.lock('never-forget', function () {})
View
6 package.json
@@ -65,7 +65,8 @@
"read-installed": "0",
"glob": "~3.1.9",
"init-package-json": "0",
- "osenv": "0"
+ "osenv": "0",
+ "lock-file": "0"
},
"bundleDependencies": [
"semver",
@@ -99,7 +100,8 @@
"read-installed",
"glob",
"init-package-json",
- "osenv"
+ "osenv",
+ "lock-file"
],
"devDependencies": {
"ronn": "https://github.com/isaacs/ronnjs/tarball/master",

0 comments on commit c40946c

Please sign in to comment.