Skip to content
3 changes: 3 additions & 0 deletions src/library_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,9 @@ FS.staticInit();
var getattr = FS.checkOpExists(node.node_ops.getattr, {{{ cDefs.EPERM }}});
return getattr(node);
},
fstat(fd) {
return FS.stat(FS.getStreamChecked(fd).path);
},
lstat(path) {
return FS.stat(path, true);
},
Expand Down
4 changes: 4 additions & 0 deletions src/library_noderawfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ addToLibrary({
}
return stat;
},
fstat(fd) {
var stream = FS.getStreamChecked(fd);
return fs.fstatSync(stream.nfd);
},
statfsStream(stream) {
return fs.statfsSync(stream.path);
},
Expand Down
3 changes: 1 addition & 2 deletions src/library_syscall.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,8 +690,7 @@ var SyscallsLibrary = {
return SYSCALLS.writeStat(buf, FS.lstat(path));
},
__syscall_fstat64: (fd, buf) => {
var stream = SYSCALLS.getStreamFromFD(fd);
return SYSCALLS.writeStat(buf, FS.stat(stream.path));
return SYSCALLS.writeStat(buf, FS.fstat(fd));
},
__syscall_fchown32: (fd, owner, group) => {
FS.fchown(fd, owner, group);
Expand Down
20 changes: 20 additions & 0 deletions test/fs/test_stat_unnamed_file_descriptor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <assert.h>
#include "stdio.h"

int main() {
int fd = open("file.txt", O_RDWR | O_CREAT, 0666);
unlink("file.txt");
int res;
struct stat buf;
res = fstat(fd, &buf);
assert(res == 0);
assert(buf.st_atime > 1000000000);
res = fchmod(fd, 0777);
assert(res == 0);
res = ftruncate(fd, 10);
assert(res == 0);
printf("success\n");
}
8 changes: 8 additions & 0 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5847,6 +5847,14 @@ def test_fs_64bit(self):
self.set_setting('FORCE_FILESYSTEM')
self.do_runf('fs/test_64bit.c', 'success')

@crossplatform
@also_with_nodefs_both
def test_fs_stat_unnamed_file_descriptor(self):
noderawfs = '-DNODERAWFS' in self.emcc_args
if not noderawfs:
self.skipTest('TODO: only works in noderawfs for now')
self.do_runf('fs/test_stat_unnamed_file_descriptor.c', 'success')

@requires_node
@crossplatform
@with_all_fs
Expand Down
Loading