Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nodefs): mmap and msync #10669

Merged
merged 1 commit into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/library_nodefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,29 @@ mergeInto(LibraryManager.library, {
}

return position;
},
mmap: function(stream, buffer, offset, length, position, prot, flags) {
if (!FS.isFile(stream.node.mode)) {
throw new FS.ErrnoError({{{ cDefine('ENODEV') }}});
}
var ptr = _malloc(length);

assert(offset === 0);
NODEFS.stream_ops.read(stream, buffer, ptr + offset, length, position);

Copy link
Member

Choose a reason for hiding this comment

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

comparing to MEMFS, offset is not used there, but you add it here - is that a bug there, or here? If we aren't sure, an assertion that the offset is 0 might be good enough for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not sure actually, so I've added the assertion as you suggest.

return { ptr: ptr, allocated: true };
},
msync: function(stream, buffer, offset, length, mmapFlags) {
if (!FS.isFile(stream.node.mode)) {
throw new FS.ErrnoError({{{ cDefine('ENODEV') }}});
}
if (mmapFlags & {{{ cDefine('MAP_PRIVATE') }}}) {
// MAP_PRIVATE calls need not to be synced back to underlying fs
return 0;
}

var bytesWritten = NODEFS.stream_ops.write(stream, buffer, 0, length, offset, false);
return 0;
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion tests/fs/test_mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
int main() {
EM_ASM(
FS.mkdir('yolo');
#if NODEFS
FS.mount(NODEFS, { root: '.' }, 'yolo');
#endif
FS.writeFile('/yolo/in.txt', 'mmap ftw!');
);

Expand Down Expand Up @@ -176,7 +179,7 @@ int main() {
printf("/yolo/sharedoffset.txt content=%s %d\n", buffer + offset, offset);
fclose(fd);
}

#if !defined(NODEFS)
kripken marked this conversation as resolved.
Show resolved Hide resolved
/**
* MMAP to an 'over-allocated' file
*
Expand Down Expand Up @@ -219,6 +222,7 @@ int main() {

close(fd);
}
#endif

return 0;
}
4 changes: 3 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5320,10 +5320,12 @@ def test_fs_append(self, js_engines=None):

def test_fs_mmap(self):
orig_compiler_opts = self.emcc_args[:]
for fs in ['MEMFS']:
for fs in ['MEMFS', 'NODEFS']:
src = path_from_root('tests', 'fs', 'test_mmap.c')
out = path_from_root('tests', 'fs', 'test_mmap.out')
self.emcc_args = orig_compiler_opts + ['-D' + fs]
if fs == 'NODEFS':
self.emcc_args += ['-lnodefs.js']
self.do_run_from_file(src, out)

@also_with_noderawfs
Expand Down