Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
src,lib: expose memory file mapping flag
Support for UV_FS_O_FILEMAP was added in libuv in version 1.31.0.
This exposes the flag in fs.constants and adds the prefix 'm' for
text flags.
  • Loading branch information
joaocgreis committed Aug 22, 2019
commit 2002dd16d89c3a60be24c213b2439955e10fadf5
9 changes: 9 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -4949,6 +4949,11 @@ The following constants are meant for use with `fs.open()`.
<td><code>O_NONBLOCK</code></td>
<td>Flag indicating to open the file in nonblocking mode when possible.</td>
</tr>
<tr>
<td><code>UV_FS_O_FILEMAP</code></td>
<td>When set, a memory file mapping is used to access the file. This flag
is available on Windows operating systems only.</td>
Comment thread
joaocgreis marked this conversation as resolved.
Outdated
</tr>
</table>

### File Type Constants
Expand Down Expand Up @@ -5141,6 +5146,10 @@ On Windows, opening an existing hidden file using the `'w'` flag (either
through `fs.open()` or `fs.writeFile()` or `fsPromises.open()`) will fail with
`EPERM`. Existing hidden files can be opened for writing with the `'r+'` flag.

All of the flags can be prepended with `'m'` to use a memory file mapping for
accessing the file (for example `'max+'`). This is only available on Windows,
Comment thread
joaocgreis marked this conversation as resolved.
Outdated
on other operative systems `'m'` is ignored.
Comment thread
joaocgreis marked this conversation as resolved.
Outdated

A call to `fs.ftruncate()` or `filehandle.truncate()` can be used to reset
the file contents.

Expand Down
5 changes: 5 additions & 0 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const {
O_SYNC,
O_TRUNC,
O_WRONLY,
UV_FS_O_FILEMAP,
S_IFBLK,
S_IFCHR,
S_IFDIR,
Expand Down Expand Up @@ -417,6 +418,10 @@ function stringToFlags(flags) {
case 'sa+': return O_APPEND | O_CREAT | O_RDWR | O_SYNC;
}

if (typeof flags === 'string' && flags.charAt(0) === 'm') {
return UV_FS_O_FILEMAP | stringToFlags(flags.substring(1));
}

throw new ERR_INVALID_OPT_VALUE('flags', flags);
}

Expand Down
4 changes: 4 additions & 0 deletions src/node_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,10 @@ void DefineSystemConstants(Local<Object> target) {
NODE_DEFINE_CONSTANT(target, O_EXCL);
#endif

#ifdef UV_FS_O_FILEMAP
NODE_DEFINE_CONSTANT(target, UV_FS_O_FILEMAP);
#endif
Comment thread
joaocgreis marked this conversation as resolved.
Outdated

#ifdef O_NOCTTY
NODE_DEFINE_CONSTANT(target, O_NOCTTY);
#endif
Expand Down
30 changes: 30 additions & 0 deletions test/parallel/test-fs-fmap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';
require('../common');
const assert = require('assert');
const fs = require('fs');
const join = require('path').join;

const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

// Run this test on all platforms. While the 'm' flag is only available on
// Windows, it should be silently ignored on other platforms.

const filename = join(tmpdir.path, 'fmap.txt');
const text = 'Memory File Mapping Test';

fs.writeFileSync(filename, text, { flag: 'mw' });
const r1 = fs.readFileSync(filename, { encoding: 'utf8', flag: 'mr' });
assert.strictEqual(r1, text);

fs.writeFileSync(filename, text, { flag: 'mr+' });
const r2 = fs.readFileSync(filename, { encoding: 'utf8', flag: 'ma+' });
assert.strictEqual(r2, text);

fs.writeFileSync(filename, text, { flag: 'ma' });
const numericFlag = fs.constants.UV_FS_O_FILEMAP | fs.constants.O_RDONLY;
const r3 = fs.readFileSync(filename, { encoding: 'utf8', flag: numericFlag });
assert.strictEqual(r3, text + text);

const r4 = fs.readFileSync(filename, { encoding: 'utf8', flag: 'mw+' });
assert.strictEqual(r4, '');
9 changes: 8 additions & 1 deletion test/parallel/test-fs-open-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ const { O_APPEND = 0,
O_SYNC = 0,
O_DSYNC = 0,
O_TRUNC = 0,
O_WRONLY = 0
O_WRONLY = 0,
UV_FS_O_FILEMAP = 0
} = fs.constants;

const { stringToFlags } = require('internal/fs/utils');
Expand All @@ -65,6 +66,12 @@ assert.strictEqual(stringToFlags('xa+'), O_APPEND | O_CREAT | O_RDWR | O_EXCL);
assert.strictEqual(stringToFlags('as+'), O_APPEND | O_CREAT | O_RDWR | O_SYNC);
assert.strictEqual(stringToFlags('sa+'), O_APPEND | O_CREAT | O_RDWR | O_SYNC);

assert.strictEqual(stringToFlags('mr'), UV_FS_O_FILEMAP | O_RDONLY);
assert.strictEqual(stringToFlags('mw'),
UV_FS_O_FILEMAP | O_TRUNC | O_CREAT | O_WRONLY);
assert.strictEqual(stringToFlags('max+'),
UV_FS_O_FILEMAP | O_APPEND | O_CREAT | O_RDWR | O_EXCL);

('+ +a +r +w rw wa war raw r++ a++ w++ x +x x+ rx rx+ wxx wax xwx xxx')
Comment thread
joaocgreis marked this conversation as resolved.
.split(' ')
.forEach(function(flags) {
Expand Down