Skip to content

Commit

Permalink
feat(nodefs): mmap and msync
Browse files Browse the repository at this point in the history
add implementation of mmap and msync for NODEFS
  • Loading branch information
petersalomonsen committed Mar 11, 2020
1 parent 9fdc8af commit 3188415
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
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);

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)
/**
* 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

0 comments on commit 3188415

Please sign in to comment.