baudehlo / node-fs-ext

Extras missing from node's fs module
MIT License
101 stars 45 forks source link

Does this module still work? #28

Closed alessioalex closed 10 years ago

alessioalex commented 10 years ago

Even though I've installed the module successfully and put a lock on a file I could still write to that file from other processes.

baudehlo commented 10 years ago

Yes. That's not how file locking works. It is advisory.

On Feb 2, 2014, at 11:03 AM, Alexandru Vlăduţu notifications@github.com wrote:

Even though I've installed the module successfully and put a lock on a file I could still write to that file from other processes.

— Reply to this email directly or view it on GitHub.

alessioalex commented 10 years ago

@baudehlo thanks for the quick answer, however no error is thrown if I try to get the lock twice in a row in the same process even:

var fs = require('fs-ext');
var fd = fs.openSync(__dirname + '/foo.txt', 'a+');
fs.flock(fd, 'ex', function (err) {
  if (err) {
    return console.log("Couldn't lock file");
  }
  // trying to get the same lock 
  fs.flock(fd, 'ex', function (err) {
    if (err) {
      return console.log("Couldn't lock file");
    }
  });

Am I doing something wrong? Thanks!

baudehlo commented 10 years ago

From the flock man page:

NOTES

 Locks are on files, not file descriptors.  That is, file descriptors

duplicated through dup(2) or

 fork(2) do not result in multiple instances of a lock, but rather

multiple references to a single

 lock.  If a process holding a lock on a file forks and the child

explicitly unlocks the file, the

 parent will lose its lock.

So you're just getting another instance of the same lock. Try from another process, or a different file descriptor.

On Sun, Feb 2, 2014 at 12:51 PM, Alexandru Vlăduţu <notifications@github.com

wrote:

@baudehlo https://github.com/baudehlo thanks for the quick answer, however no error is thrown if I try to get the lock twice in a row in the same process even:

var fs = require('fs-ext');var fd = fs.openSync(__dirname + '/foo.txt', 'a+');fs.flock(fd, 'ex', function (err) { if (err) { return console.log("Couldn't lock file"); } // trying to get the same lock fs.flock(fd, 'ex', function (err) { if (err) { return console.log("Couldn't lock file"); } });

Am I doing something wrong? Thanks!

— Reply to this email directly or view it on GitHubhttps://github.com/baudehlo/node-fs-ext/issues/28#issuecomment-33906702 .

alessioalex commented 10 years ago

@baudehlo thanks a lot for the explanations, I haven't worked with locking too much. Really appreciate it!

baudehlo commented 10 years ago

No worries. If you wanted to expand the docs it might not be a bad idea :)

On Feb 2, 2014, at 1:03 PM, Alexandru Vlăduţu notifications@github.com wrote:

@baudehlo thanks a lot for the explanations, I haven't worked with locking too much. Really appreciate it!

— Reply to this email directly or view it on GitHub.

alessioalex commented 10 years ago

Great idea, I will submit a pull request by tomorrow!

rlidwka commented 10 years ago

fs.flock(fd, 'ex', function (err) {

This is bad idea by the way. If you lock several files this way, you'll eventually fill up node.js i/o pool and cut off yourself out of any i/o until these locks are resolved.

Use exnb instead.

alessioalex commented 10 years ago

@baudehlo I've added an example file and made the pull request now: https://github.com/baudehlo/node-fs-ext/pull/29