Skip to content

Commit 1674b6e

Browse files
committed
fcntl.js
0 parents  commit 1674b6e

10 files changed

Lines changed: 2552 additions & 0 deletions

File tree

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.env*
2+
*\.swp
3+
.DS_Store
4+
node_modules/
5+
build/

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# fcntl.js
2+
fcntl is the POSIX standard way to do file locks. Now available to node.
3+
4+
Tested on Linux, should work on Mac. Windows Subsystem for Linux => probably or easy to patch.
5+
6+
```js
7+
const fs = require('fs')
8+
const fcntl = require('fcntl.js')
9+
10+
const C = fcntl.constants()
11+
12+
try {
13+
14+
const path = '/tmp/test.txt'
15+
const fd = fs.openSync(path, 'w+')
16+
17+
// fcntlNum
18+
const num1 = fcntl.fcntlNum(fd, C.F_GETFL)
19+
const num2 = fcntl.fcntlNum(fd, C.F_SETFL, C.O_APPEND)
20+
const num3 = fcntl.fcntlNum(fd, C.F_GETFL)
21+
console.log(num1, num2, num3)
22+
23+
// fcntlObj
24+
const out = fcntl.fcntlObj(fd, C.F_SETLK, { type: C.F_WRLCK, whence: C.SEEK_SET, start: 0, len: 0 })
25+
console.log(out.rc, out)
26+
27+
fs.closeSync(fd)
28+
29+
} catch (err) {
30+
// errno comes from c
31+
// err.message comes also from c
32+
console.log(err.errno, err)
33+
}
34+
```
35+
36+
## Docs
37+
The [tests](https://github.com/rhodey/fcntl.js/blob/master/test/test.js) are pretty good and show how to set and check a lock
38+
39+
If you want docs google "fcntl linux" and you will find the info, these functions are just thin wrappers over C
40+
```
41+
npm install
42+
npm run test
43+
```
44+
45+
## License
46+
MIT

binding.gyp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "fcntl",
5+
"sources": [ "src/addon.cc" ],
6+
"include_dirs": [
7+
"<!@(node -p \"require('node-addon-api').include\")"
8+
],
9+
"dependencies": [
10+
"<!(node -p \"require('node-addon-api').gyp\")"
11+
],
12+
"defines": [
13+
"NAPI_DISABLE_CPP_EXCEPTIONS=0"
14+
],
15+
"cflags!": [ "-fno-exceptions" ],
16+
"cxxflags!": [ "-fno-exceptions" ],
17+
"conditions": [
18+
[ "OS=='linux' or OS=='freebsd' or OS=='darwin'", {
19+
"libraries": []
20+
}]
21+
]
22+
}
23+
]
24+
}

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const load = require('node-gyp-build')
2+
module.exports = load(__dirname)

0 commit comments

Comments
 (0)