Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can stream_ops really be null here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was surprised by this, but I believe there was a CI failure when I didn't include this, I can double check which one. Also, it's really nice to be able to use ?..

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, test_unistd_dup_rawfs fails here without it.

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
45 changes: 45 additions & 0 deletions test/fs/test_nodefs_dup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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 <assert.h>
#include <emscripten.h>
#include <fcntl.h>
#include <stdio.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_WRONLY);
int fd2 = dup(fd1);
int fd3 = fcntl(fd1, F_DUPFD_CLOEXEC, 0);

assert(fd1 == 3);
assert(fd2 == 4);
assert(fd3 == 5);
assert(close(fd1) == 0);
assert(write(fd2, "abcdef", 6) == 6);
assert(close(fd2) == 0);
assert(write(fd3, "ghijkl", 6) == 6);
assert(close(fd3) == 0);
printf("success\n");
return 0;
}
8 changes: 8 additions & 0 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5749,6 +5749,14 @@ 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']
self.do_runf('fs/test_nodefs_dup.c', 'success')

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