Skip to content

rhodey/fcntl.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fcntl.js

fcntl is the POSIX standard way to do file locks. Now available to node.

Tested on Linux, should work on Mac. Windows Subsystem for Linux => probably or easy to patch.

const fs = require('fs')
const fcntl = require('fcntl.js')

const C = fcntl.constants()

try {

  const path = '/tmp/test.txt'
  const fd = fs.openSync(path, 'w+')

  // fcntlNum
  const num1 = fcntl.fcntlNum(fd, C.F_GETFL)
  const num2 = fcntl.fcntlNum(fd, C.F_SETFL, C.O_APPEND)
  const num3 = fcntl.fcntlNum(fd, C.F_GETFL)
  console.log(num1, num2, num3)

  // fcntlObj
  const out = fcntl.fcntlObj(fd, C.F_SETLK, { type: C.F_WRLCK, whence: C.SEEK_SET, start: 0, len: 0 })
  console.log(out.rc, out)

  fs.closeSync(fd)

} catch (err) {
  // errno comes from c
  // err.message comes also from c
  console.log(err.errno, err)
}

Docs

The tests are pretty good and show how to set and check a lock

If you want docs google "fcntl linux" and you will find the info, these functions are just thin wrappers over C

npm install
npm run test

License

MIT