Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorisaacs <i@izs.me>2012-06-28 01:19:21 +0400
committerisaacs <i@izs.me>2012-06-28 01:19:21 +0400
commit3d139b204b10c48ed13d30443a8cf6d092ea1a4c (patch)
treea2e5f9e7af61eef527763fa1c12f22d845d7cf34 /node_modules/lockfile
parent3f2a64705200a045d4b2956846bc9d90ea43f8e3 (diff)
s/lock-file/lockfile/
Diffstat (limited to 'node_modules/lockfile')
-rw-r--r--node_modules/lockfile/LICENSE25
-rw-r--r--node_modules/lockfile/README.md81
-rw-r--r--node_modules/lockfile/lockfile.js241
-rw-r--r--node_modules/lockfile/package.json39
-rw-r--r--node_modules/lockfile/test/basic.js226
-rw-r--r--node_modules/lockfile/test/fixtures/bad-child.js5
-rw-r--r--node_modules/lockfile/test/fixtures/child.js3
7 files changed, 620 insertions, 0 deletions
diff --git a/node_modules/lockfile/LICENSE b/node_modules/lockfile/LICENSE
new file mode 100644
index 000000000..74489e2e2
--- /dev/null
+++ b/node_modules/lockfile/LICENSE
@@ -0,0 +1,25 @@
+Copyright (c) Isaac Z. Schlueter
+All rights reserved.
+
+The BSD License
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
diff --git a/node_modules/lockfile/README.md b/node_modules/lockfile/README.md
new file mode 100644
index 000000000..18ffd5041
--- /dev/null
+++ b/node_modules/lockfile/README.md
@@ -0,0 +1,81 @@
+# lockfile
+
+A very polite lock file utility, which endeavors to not litter, and to
+wait patiently for others.
+
+## Usage
+
+```javascript
+var lockFile = require('lockfile')
+
+// 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.
+
+### opts.retries
+
+Used by lock and lockSync. Retry `n` number of times before giving up.
+
+### opts.retryWait
+
+Used by lock. Wait `n` milliseconds before retrying.
diff --git a/node_modules/lockfile/lockfile.js b/node_modules/lockfile/lockfile.js
new file mode 100644
index 000000000..bbac0b699
--- /dev/null
+++ b/node_modules/lockfile/lockfile.js
@@ -0,0 +1,241 @@
+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 () {
+ // 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('uncaughtException', 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 = {}
+
+ if (typeof opts.retries === 'number' && opts.retries > 0) {
+ cb = (function (orig) { return function (er, fd) {
+ if (!er) return orig(er, fd)
+ var newRT = opts.retries - 1
+ opts_ = Object.create(opts, { retries: { value: newRT }})
+ if (opts.retryWait) setTimeout(function() {
+ exports.lock(path, opts_, orig)
+ }, opts.retryWait)
+ else exports.lock(path, opts_, orig)
+ }})(cb)
+ }
+
+ // 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) fs.stat(path, function (er, st) {
+ if (er) {
+ if (er.code === 'ENOENT') {
+ // expired already!
+ var opts_ = Object.create(opts, { stale: { value: false }})
+ exports.lock(path, opts_, cb)
+ return
+ }
+ return cb(er)
+ }
+
+ 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 notStale(er, path, opts, cb)
+ })
+ else notStale(er, path, opts, cb)
+ })
+}
+
+function notStale (er, path, opts, cb) {
+ if (typeof opts.wait === 'number' && opts.wait > 0) {
+ // wait for some ms for the lock to clear
+ var start = Date.now()
+
+ var retried = false
+ function retry () {
+ if (retried) return
+ retried = true
+ // maybe already closed.
+ try { watcher.close() } catch (e) {}
+ clearTimeout(timer)
+ var newWait = Date.now() - start
+ var opts_ = Object.create(opts, { wait: { value: newWait }})
+ exports.lock(path, opts_, cb)
+ }
+
+ try {
+ var watcher = fs.watch(path, function (change) {
+ if (change === 'rename') {
+ // ok, try and get it now.
+ // if this fails, then continue waiting, maybe.
+ retry()
+ }
+ })
+ watcher.on('error', function (er) {
+ // usually means it expired before the watcher spotted it
+ retry()
+ })
+ } catch (er) {
+ retry()
+ }
+
+ 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 || opts.retryWait) {
+ 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') return retryThrow(path, opts, er)
+
+ if (opts.stale) {
+ var st = fs.statSync(path)
+ var age = Date.now() - st.ctime.getTime()
+ if (age > opts.stale) {
+ exports.unlockSync(path)
+ return exports.lockSync(path, opts)
+ }
+ }
+
+ // failed to lock!
+ return retryThrow(path, opts, er)
+ }
+}
+
+function retryThrow (path, opts, er) {
+ if (typeof opts.retries === 'number' && opts.retries > 0) {
+ var newRT = opts.retries - 1
+ var opts_ = Object.create(opts, { retries: { value: newRT }})
+ return exports.lockSync(path, opts_)
+ }
+ throw er
+}
+
diff --git a/node_modules/lockfile/package.json b/node_modules/lockfile/package.json
new file mode 100644
index 000000000..60d2e53c2
--- /dev/null
+++ b/node_modules/lockfile/package.json
@@ -0,0 +1,39 @@
+{
+ "name": "lockfile",
+ "version": "0.2.0",
+ "main": "lockfile.js",
+ "directories": {
+ "test": "test"
+ },
+ "dependencies": {},
+ "devDependencies": {
+ "tap": "~0.2.5"
+ },
+ "scripts": {
+ "test": "tap test/*.js"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/isaacs/lockfile"
+ },
+ "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": "# lockfile\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('lockfile')\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### opts.retries\n\nUsed by lock and lockSync. Retry `n` number of times before giving up.\n\n### opts.retryWait\n\nUsed by lock. Wait `n` milliseconds before retrying.\n",
+ "_id": "lockfile@0.2.0",
+ "dist": {
+ "shasum": "1c4090b1c45ac45ed6898025c9e5d1e53fa93813"
+ },
+ "_from": "../lockfile"
+}
diff --git a/node_modules/lockfile/test/basic.js b/node_modules/lockfile/test/basic.js
new file mode 100644
index 000000000..77308cdc9
--- /dev/null
+++ b/node_modules/lockfile/test/basic.js
@@ -0,0 +1,226 @@
+var test = require('tap').test
+var lockFile = require('../lockfile.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) {}
+ try { lockFile.unlockSync('retry-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('retries', function (t) {
+ // next 5 opens will fail.
+ var opens = 5
+ fs._open = fs.open
+ fs.open = function (path, mode, cb) {
+ if (--opens === 0) {
+ fs.open = fs._open
+ return fs.open(path, mode, cb)
+ }
+ var er = new Error('bogus')
+ // to be, or not to be, that is the question.
+ er.code = opens % 2 ? 'EEXIST' : 'ENOENT'
+ process.nextTick(cb.bind(null, er))
+ }
+
+ lockFile.lock('retry-lock', { retries: opens }, function (er, fd) {
+ if (er) throw er
+ t.equal(opens, 0)
+ t.ok(fd)
+ lockFile.unlockSync('retry-lock')
+ t.end()
+ })
+})
+
+test('retryWait', function (t) {
+ // next 5 opens will fail.
+ var opens = 5
+ fs._open = fs.open
+ fs.open = function (path, mode, cb) {
+ if (--opens === 0) {
+ fs.open = fs._open
+ return fs.open(path, mode, cb)
+ }
+ var er = new Error('bogus')
+ // to be, or not to be, that is the question.
+ er.code = opens % 2 ? 'EEXIST' : 'ENOENT'
+ process.nextTick(cb.bind(null, er))
+ }
+
+ var opts = { retries: opens, retryWait: 100 }
+ lockFile.lock('retry-lock', opts, function (er, fd) {
+ if (er) throw er
+ t.equal(opens, 0)
+ t.ok(fd)
+ lockFile.unlockSync('retry-lock')
+ t.end()
+ })
+})
+
+test('retry sync', function (t) {
+ // next 5 opens will fail.
+ var opens = 5
+ fs._openSync = fs.openSync
+ fs.openSync = function (path, mode) {
+ if (--opens === 0) {
+ fs.openSync = fs._openSync
+ return fs.openSync(path, mode)
+ }
+ var er = new Error('bogus')
+ // to be, or not to be, that is the question.
+ er.code = opens % 2 ? 'EEXIST' : 'ENOENT'
+ throw er
+ }
+
+ var opts = { retries: opens }
+ lockFile.lockSync('retry-lock', opts)
+ t.equal(opens, 0)
+ lockFile.unlockSync('retry-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) {}
+ try { lockFile.unlockSync('retry-lock') } catch (er) {}
+ t.end()
+})
+
diff --git a/node_modules/lockfile/test/fixtures/bad-child.js b/node_modules/lockfile/test/fixtures/bad-child.js
new file mode 100644
index 000000000..e65304542
--- /dev/null
+++ b/node_modules/lockfile/test/fixtures/bad-child.js
@@ -0,0 +1,5 @@
+var lockFile = require('../../lockfile.js')
+
+lockFile.lockSync('never-forget')
+
+throw new Error('waaaaaaaaa')
diff --git a/node_modules/lockfile/test/fixtures/child.js b/node_modules/lockfile/test/fixtures/child.js
new file mode 100644
index 000000000..5b61d6c91
--- /dev/null
+++ b/node_modules/lockfile/test/fixtures/child.js
@@ -0,0 +1,3 @@
+var lockFile = require('../../lockfile.js')
+
+lockFile.lock('never-forget', function () {})