baudehlo / node-fs-ext

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

flock not working with fs.open ?! #97

Closed GeorgeFlorian closed 3 years ago

GeorgeFlorian commented 3 years ago

Hello !
I am trying this module in a simple express server, but flock doesn't seem to work with fs.open.

The following code doesn't work as it returns TypeError: Bad argument

  // open file
  const networkFile = fs.open(path.join(__dirname + '/test.txt'), "w+", (err, f) => {
    if(err) {
      console.log("Could not open file.");
    }
    console.log(f);
  });
  // lock file
  flock(networkFile, 'ex', (err) => {
    if(err) {
      return console.log("Could not lock file.");
    }
    // file is locked
    console.log("File is locked.");
  });

Chaning from fs.open to fs.openSync made flock work, so the following code works:

const networkFile = fs.openSync(path.join(__dirname + '/test.txt'), "w+");

Is this normal behavior ? Should fs.open not work with flock ? And if that's the case, can somebody explain as to why ?

Thank you.

baudehlo commented 3 years ago

You've misunderstood how callbacks work. Please do a node.js fundamentals course before continuing.

On Mon, Jul 20, 2020 at 1:07 PM Sloata George notifications@github.com wrote:

Hello ! I am trying this module in a simple express server, but flock doesn't seem to work with fs.open.

The following code doesn't work as it returns TypeError: Bad argument

// open file const networkFile = fs.open(path.join(__dirname + '/test.txt'), "w+", (err, f) => { if(err) { console.log("Could not open file."); } console.log(f); }); // lock file flock(networkFile, 'ex', (err) => { if(err) { return console.log("Could not lock file."); } // file is locked console.log("File is locked."); });

Chaning from fs.open to fs.openSync made flock work, so the following code works:

const networkFile = fs.openSync(path.join(__dirname + '/test.txt'), "w+");

Is this normal behavior ? Should fs.open not work with flock ? And if that's the case, can somebody explain as to why ?

Thank you.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/baudehlo/node-fs-ext/issues/97, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFBWYZLAM3Y4PNVDCFPYHDR4R2UJANCNFSM4PCRYPSQ .

GeorgeFlorian commented 3 years ago

I certainly do need to take that, but unfortunately I don't have the time.

I have found examples of fs.open here https://www.geeksforgeeks.org/node-js-fs-open-method/ If I am wrong, that means that whoever wrote that example is also in the wrong.