Skip to content

Commit 9c9893d

Browse files
committed
Make fstat work on file descriptors with no name in node rawfs
This fixes fstat on "anonymous" file descriptors in node rawfs. It is split off from emscripten-core#23058.
1 parent dde19fa commit 9c9893d

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

src/library_fs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,9 @@ FS.staticInit();
956956
}
957957
return node.node_ops.getattr(node);
958958
},
959+
fstat(fd) {
960+
return FS.stat(FS.getStreamChecked(fd).node);
961+
},
959962
lstat(path) {
960963
return FS.stat(path, true);
961964
},

src/library_noderawfs.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ addToLibrary({
7979
}
8080
return stat;
8181
},
82+
fstat(fd) {
83+
var stream = FS.getStreamChecked(fd);
84+
return fs.fstatSync(stream.nfd);
85+
},
8286
chmod(path, mode, dontFollow) {
8387
mode &= {{{ cDefs.S_IALLUGO }}};
8488
if (NODEFS.isWindows) {

src/library_syscall.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,8 +678,7 @@ var SyscallsLibrary = {
678678
return SYSCALLS.writeStat(buf, FS.lstat(path));
679679
},
680680
__syscall_fstat64: (fd, buf) => {
681-
var stream = SYSCALLS.getStreamFromFD(fd);
682-
return SYSCALLS.writeStat(buf, FS.stat(stream.path));
681+
return SYSCALLS.writeStat(buf, FS.fstat(fd));
683682
},
684683
__syscall_fchown32: (fd, owner, group) => {
685684
FS.fchown(fd, owner, group);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <fcntl.h>
2+
#include <unistd.h>
3+
#include <sys/stat.h>
4+
#include <assert.h>
5+
#include "stdio.h"
6+
7+
int main() {
8+
int fd = open("file.txt", O_RDWR | O_CREAT, 0666);
9+
unlink("file.txt");
10+
int res;
11+
struct stat buf;
12+
res = fstat(fd, &buf);
13+
assert(res == 0);
14+
assert(buf.st_atime > 1000000000);
15+
res = fchmod(fd, 0777);
16+
assert(res == 0);
17+
res = ftruncate(fd, 10);
18+
assert(res == 0);
19+
printf("success\n");
20+
}

test/test_core.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5847,6 +5847,16 @@ def test_fs_64bit(self):
58475847
self.set_setting('FORCE_FILESYSTEM')
58485848
self.do_runf('fs/test_64bit.c', 'success')
58495849

5850+
@crossplatform
5851+
@also_with_noderawfs
5852+
def test_fs_stat_unnamed_file_descriptor(self):
5853+
nodefs = '-DNODEFS' in self.emcc_args or '-DNODERAWFS' in self.emcc_args
5854+
if self.get_setting('WASMFS'):
5855+
if nodefs:
5856+
self.skipTest('NODEFS in WasmFS')
5857+
self.set_setting('FORCE_FILESYSTEM')
5858+
self.do_runf('fs/test_stat_unnamed_file_descriptor.c', 'success')
5859+
58505860
@requires_node
58515861
@crossplatform
58525862
@with_all_fs

0 commit comments

Comments
 (0)