Support fs.open() flags being a number vs. string
See original GitHub issueHi 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:
- Created 5 years ago
- Comments:5 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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.
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:
flags
being aString
orNumber
stringToFlags()
in nodeA simple fix would be to alter our
O_FLAGS
object to use numeric keys vs. strings, and convert strings to numbers like node does.