question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support fs.open() flags being a number vs. string

See original GitHub issue

Hi everyone!

I’m trying to do this and I’m getting flags is not valid:

fs.open("/file.txt", fs.constants.O_RDWR | fs.constants.O_CREAT, function(err, fd) {
  if(err) throw err;
  fs.fstat(fd, function(err, stats) {
    if(err) throw err;
    fs.close(fd);
  });
});

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
humphdcommented, Mar 8, 2019

@rscotchmer great, here are some more thoughts.

We currently use strings for flags, and we should switch to using numbers. If a string is passed to us, we should convert it into a number. See the code I linked to above from node, which does what we want.

Next, we do a bunch of checks all over like this: https://github.com/filerjs/filer/blob/master/src/filesystem/implementation.js#L622. Instead of doing this, we’d want to convert that to use the flags number, and bitwise & (and) it with the constant we want to check against. The constants we want are defined in https://github.com/filerjs/filer/blob/master/src/constants.js#L88.

We could start by converting the code in the first comment above into a test we add to the fs.open.spec.js test file, and add more as we go. I have no doubt you’ll find other bugs while fixing this, since we probably don’t do the right thing in all cases with flags vs. what node does.

Let me know what questions you have, or further help I can give. I’m happy to guide you as you deem necessary.

1reaction
humphdcommented, Mar 7, 2019

Hey @tinchoz49, this is a bug, thanks for filing. We currently assume flags are in string form ('w+' is closest to what you’d want), and don’t yet support passing flags as a bitwise number. We should.

Fixing this would involve making our code do what node does:

A simple fix would be to alter our O_FLAGS object to use numeric keys vs. strings, and convert strings to numbers like node does.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js - File System - Tutorialspoint
Parameters · path − This is the string having file name including path. · flags − Flags indicate the behavior of the file...
Read more >
Using open() | Working with Files in Node.js - InformIT
The open() method opens a file by passing a file name to the method (or path string), along with how to open the...
Read more >
In fs.writeFile([option]), how an "options parameter" generally ...
For anyone ending up here off a search looking for a flags reference, here it is: Flag, Description. r, Open file for reading....
Read more >
File system | Node.js v19.3.0 Documentation
writeSync(fd, string[, position[, encoding]]); fs. ... Instances of the <FileHandle> object are created by the fsPromises.open() method.
Read more >
How To Use the Flag Package in Go - DigitalOcean
The final parameter is a string of documentation that can be printed as a usage message. The value returned from this function is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found