Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
fs: add sync open flags 'rs' and 'rs+'
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmonkey authored and bnoordhuis committed May 15, 2012
1 parent 643f00d commit dfcdd5b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
7 changes: 7 additions & 0 deletions doc/api/fs.markdown
Expand Up @@ -270,6 +270,13 @@ An exception occurs if the file does not exist.
* `'r+'` - Open file for reading and writing.
An exception occurs if the file does not exist.

* `'rs'` - Open file for reading, telling the OS to open it synchronously
(ie using the O_SYNC flag). Whilst rarely useful, when used with caution by
those who know what they're doing it can be sometimes necessary. Note that
this doesn't turn `fs.open()` into a synchronous blocking call, if that's what
you want then you should be using `fs.openSync()`
An exception occurs if the file does not exist.

* `'w'` - Open file for writing.
The file is created (if it does not exist) or truncated (if it exists).

Expand Down
2 changes: 2 additions & 0 deletions lib/fs.js
Expand Up @@ -210,7 +210,9 @@ function stringToFlags(flag) {

switch (flag) {
case 'r' : return O_RDONLY;
case 'rs' : return O_RDONLY | O_SYNC;
case 'r+' : return O_RDWR;
case 'rs+' : return O_RDWR | O_SYNC;

case 'w' : return O_TRUNC | O_CREAT | O_WRONLY;
case 'wx' : // fall through
Expand Down
10 changes: 9 additions & 1 deletion test/simple/test-fs-open.js
Expand Up @@ -41,11 +41,19 @@ fs.open(__filename, 'r', function(err, fd) {
if (err) {
throw err;
}

openFd = fd;
});

var openFd2;
fs.open(__filename, 'rs', function(err, fd) {
if (err) {
throw err;
}
openFd2 = fd;
});

process.on('exit', function() {
assert.ok(openFd);
assert.ok(openFd2);
});

2 comments on commit dfcdd5b

@mscdex
Copy link

@mscdex mscdex commented on dfcdd5b May 15, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation for 'rs+' is missing?

@magicmonkey
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, oversight on my part - I've added docs in a new pull request (3274).

Please sign in to comment.