1 # node-lockfd
2
3 A trivial wrapper around `fcntl(F_SETLK)` (or `F_SETLKW`). Presently allows
4 a synchronous or asynchronous call to get a whole-file, exclusive, advisory
5 write lock on a file, or to block until one is possible.
6
7 This module has been crafted specifically to work on SmartOS, and may not work
8 anywhere else. Please see [fcntl(2)](http://illumos.org/man/2/fcntl) for more
9 details on the locking semantics. In general, the lock will be released when
10 either the file descriptor is closed, or the process exits. The manual page
11 contains information on exceptions to this behaviour.
12
13 ## Usage
14
15 ### lockfd(fd, callback)
16
17 Will attempt to lock the open file descriptor `fd` as described above. Once
18 the lock is acquired, or an error condition manifests, `callback(err)` will be
19 called.
20
21 ### lockfdSync(fd)
22
23 Synchronous version of `lockfd(fd)`.
24
25 ## Examples
26
27 ```javascript
28 var mod_fs = require('fs');
29 var mod_lockfd = require('lockfd');
30
31 var fd = mod_fs.openSync('/tmp/.lockfile', 'r+');
32 console.error('open fd %d', fd);
33
34 console.error('locking file...');
35 mod_lockfd.lockfdSync(fd);
36 console.log('locked.');
37
38 /*
39 * Do work...
40 */
41
42 mod_fs.closeSync(fd);
43 process.exit(0);
44 ```
45
46 ## License
47
48 MIT.