Skip to content

Commit

Permalink
Fix dup in nodefs
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane committed Feb 22, 2024
1 parent bf76ba4 commit 1e52bb6
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/library_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,11 @@ FS.staticInit();` +
closeStream(fd) {
FS.streams[fd] = null;
},
dupStream(origStream, fd = -1) {
var stream = FS.createStream(origStream, fd);
stream.stream_ops?.dup?.(stream);
return stream;
},

//
// devices
Expand Down
6 changes: 5 additions & 1 deletion src/library_nodefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ addToLibrary({
var path = NODEFS.realPath(stream.node);
try {
if (FS.isFile(stream.node.mode)) {
stream.shared.refcount = 1;
stream.nfd = fs.openSync(path, NODEFS.flagsForNode(stream.flags));
}
} catch (e) {
Expand All @@ -260,14 +261,17 @@ addToLibrary({
},
close(stream) {
try {
if (FS.isFile(stream.node.mode) && stream.nfd) {
if (FS.isFile(stream.node.mode) && stream.nfd && --stream.shared.refcount === 0) {
fs.closeSync(stream.nfd);
}
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
}
},
dup(stream) {
stream.shared.refcount ++;
},
read(stream, buffer, offset, length, position) {
// Node.js < 6 compatibility: node errors on 0 length reads
if (length === 0) return 0;
Expand Down
6 changes: 3 additions & 3 deletions src/library_syscall.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ var SyscallsLibrary = {
},
__syscall_dup: (fd) => {
var old = SYSCALLS.getStreamFromFD(fd);
return FS.createStream(old).fd;
return FS.dupStream(old).fd;
},
__syscall_pipe__deps: ['$PIPEFS'],
__syscall_pipe: (fdPtr) => {
Expand Down Expand Up @@ -760,7 +760,7 @@ var SyscallsLibrary = {
arg++;
}
var newStream;
newStream = FS.createStream(stream, arg);
newStream = FS.dupStream(stream, arg);
return newStream.fd;
}
case {{{ cDefs.F_GETFD }}}:
Expand Down Expand Up @@ -1007,7 +1007,7 @@ var SyscallsLibrary = {
if (old.fd === newfd) return -{{{ cDefs.EINVAL }}};
var existing = FS.getStream(newfd);
if (existing) FS.close(existing);
return FS.createStream(old, newfd).fd;
return FS.dupStream(old, newfd).fd;
},
};

Expand Down
39 changes: 39 additions & 0 deletions test/fs/test_nodefs_dup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2018 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/

#include <stdio.h>
#include <fcntl.h>
#include <emscripten.h>
#include <unistd.h>

#ifdef NODERAWFS
#define CWD ""
#else
#define CWD "/working/"
#endif

int main(void)
{
EM_ASM(
#ifdef NODERAWFS
FS.close(FS.open('test.txt', 'w'));
#else
FS.mkdir('/working');
FS.mount(NODEFS, {root: '.'}, '/working');
FS.close(FS.open('/working/test.txt', 'w'));
#endif
);
int fd1 = open(CWD "test.txt", O_RDONLY);
int fd2 = dup(fd1);
int fd3 = fcntl(fd1, F_DUPFD_CLOEXEC, 0);

printf("fd1: %d, fd2: %d\n", fd1, fd2);
printf("close(fd1): %d\n", close(fd1));
printf("close(fd2): %d\n", close(fd2));
printf("close(fd3): %d\n", close(fd3));
return 0;
}
10 changes: 10 additions & 0 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5749,6 +5749,16 @@ def test_fs_nodefs_cloexec(self):
self.emcc_args += ['-lnodefs.js']
self.do_runf('fs/test_nodefs_cloexec.c', 'success')

@also_with_noderawfs
@requires_node
def test_fs_nodefs_dup(self):
if self.get_setting('WASMFS'):
self.set_setting('FORCE_FILESYSTEM')
self.emcc_args += ['-lnodefs.js']
expected = "fd1: 3, fd2: 4\nclose(fd1): 0\nclose(fd2): 0\nclose(fd3): 0"

self.do_runf('fs/test_nodefs_dup.c', expected)

@requires_node
def test_fs_nodefs_home(self):
self.set_setting('FORCE_FILESYSTEM')
Expand Down

0 comments on commit 1e52bb6

Please sign in to comment.