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
13 changes: 7 additions & 6 deletions src/lib/libnoderawfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ addToLibrary({
if (!ENVIRONMENT_IS_NODE) {
throw new Error("NODERAWFS is currently only supported on Node.js environment.")
}
var nodeTTY = require('node:tty');
function _wrapNodeError(func) {
return (...args) => {
try {
Expand Down Expand Up @@ -64,11 +65,10 @@ addToLibrary({
return { path, node: { id: st.ino, mode, node_ops: NODERAWFS, path }};
},
createStandardStreams() {
// FIXME: tty is set to true to appease isatty(), the underlying ioctl syscalls still need to be implemented, see issue #22264.
FS.createStream({ nfd: 0, position: 0, path: '/dev/stdin', flags: 0, tty: true, seekable: false }, 0);
FS.createStream({ nfd: 0, position: 0, path: '/dev/stdin', flags: 0, seekable: false }, 0);
var paths = [,'/dev/stdout', '/dev/stderr'];
for (var i = 1; i < 3; i++) {
FS.createStream({ nfd: i, position: 0, path: paths[i], flags: {{{ cDefs.O_TRUNC | cDefs.O_CREAT | cDefs.O_WRONLY }}}, tty: true, seekable: false }, i);
FS.createStream({ nfd: i, position: 0, path: paths[i], flags: {{{ cDefs.O_TRUNC | cDefs.O_CREAT | cDefs.O_WRONLY }}}, seekable: false }, i);
Copy link
Member

Choose a reason for hiding this comment

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

Why is it correct to remove "tty: true" 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.

Because FS.createStream automatically sets the tty property now, with the correct value.

}
},
// generic function for all node creation
Expand Down Expand Up @@ -179,10 +179,11 @@ addToLibrary({
createStream(stream, fd) {
// Call the original FS.createStream
var rtn = VFS.createStream(stream, fd);
if (typeof rtn.shared.refcnt == 'undefined') {
rtn.shared.refcnt = 1;
} else {
// Detect PIPEFS streams and skip the refcnt/tty initialization in that case.
if (!stream.stream_ops) {
Copy link
Member

Choose a reason for hiding this comment

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

what does this change do?

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 just simplified the code a little here.

The presence of stream.stream_ops here means that the stream is a memfs/pipefs stream, not one that is managed by noderawfs.

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 added a comment here.

rtn.shared.refcnt ??= 0;
rtn.shared.refcnt++;
rtn.tty = nodeTTY.isatty(rtn.nfd);
}
return rtn;
},
Expand Down
9 changes: 8 additions & 1 deletion test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,7 @@ def test_export_all_and_exported_functions(self):
self.do_runf('lib.c', 'libfunc\n', cflags=['-sEXPORTED_FUNCTIONS=_libfunc2', '-sEXPORT_ALL', '--pre-js', 'pre.js'])

@all_engines
@also_with_wasmfs
@with_all_fs
@crossplatform
@parameterized({
'': ([],),
Expand Down Expand Up @@ -13128,7 +13128,14 @@ def test_unistd_pathconf(self):
def test_unistd_swab(self):
self.do_run_in_out_file_test('unistd/swab.c')

@also_with_noderawfs
def test_unistd_isatty(self):
if '-DNODERAWFS' in self.cflags:
# Under NODERAWFS istty reports accurate information about the file descriptors
# of the node process. When we run tests we always capture stdout so we never expect
# stdout to be a tty.
stdin_isatty = os.isatty(0)
self.cflags += ['-DEXPECT_STDOUT=0', f'-DEXPECT_STDIN={int(stdin_isatty)}']
self.do_runf('unistd/isatty.c', 'success')

def test_unistd_login(self):
Expand Down
38 changes: 29 additions & 9 deletions test/unistd/isatty.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,41 @@
#include <string.h>
#include <unistd.h>

#ifndef EXPECT_STDIN
#define EXPECT_STDIN 1
#endif

#ifndef EXPECT_STDOUT
#define EXPECT_STDOUT 1
#endif

int main() {
int err;
printf("EXPECT_STDIN: %d\n", EXPECT_STDIN);
printf("EXPECT_STDOUT: %d\n", EXPECT_STDOUT);

assert(isatty(0) == EXPECT_STDIN);
assert(isatty(1) == EXPECT_STDOUT);

err = isatty(-1);
assert(!err);
int err, fd;

assert(isatty(-1) == 0);
assert(errno == EBADF);

err = isatty(open("/dev/stdin", O_RDONLY));
assert(err == 1);
fd = open("/dev/stdin", O_RDONLY);
assert(fd >= 0);
assert(isatty(fd) == EXPECT_STDIN);

fd = open("/dev/stdout", O_RDONLY);
assert(fd >= 0);
assert(isatty(fd) == EXPECT_STDOUT);

err = isatty(open("/dev/null", O_RDONLY));
assert(!err);
fd = open("/dev/null", O_RDONLY);
assert(fd >= 0);
assert(isatty(fd) == 0);

err = isatty(open("/dev", O_RDONLY));
assert(!err);
fd = open("/dev", O_RDONLY);
assert(fd >= 0);
assert(isatty(fd) == 0);

puts("success");

Expand Down
Loading