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 7faeb48
Show file tree
Hide file tree
Showing 4 changed files with 23 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
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 7faeb48

Please sign in to comment.