Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
78 changes: 48 additions & 30 deletions src/lib/libfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,13 @@ FS.staticInit();
stream.stream_ops?.dup?.(stream);
return stream;
},
doSetAttr(stream, node, attr) {
var setattr = stream?.stream_ops.setattr;
var arg = setattr ? stream : node;
setattr ??= node.node_ops.setattr;
FS.checkOpExists(setattr, {{{ cDefs.EPERM }}})
setattr(arg, attr);
},

//
// devices
Expand Down Expand Up @@ -966,11 +973,23 @@ FS.staticInit();
return getattr(node);
},
fstat(fd) {
return FS.stat(FS.getStreamChecked(fd).path);
var stream = FS.getStreamChecked(fd);
var getattr = stream.stream_ops.getattr;
var arg = getattr ? stream : node;
getattr ??= node.node_ops.getattr;
FS.checkOpExists(getattr, {{{ cDefs.EPERM }}})
return getattr(arg);
},
lstat(path) {
return FS.stat(path, true);
},
doChmod(stream, node, mode, dontFollow) {
FS.doSetAttr(stream, node, {
mode: (mode & {{{ cDefs.S_IALLUGO }}}) | (node.mode & ~{{{ cDefs.S_IALLUGO }}}),
ctime: Date.now(),
dontFollow
});
},
chmod(path, mode, dontFollow) {
var node;
if (typeof path == 'string') {
Expand All @@ -979,19 +998,21 @@ FS.staticInit();
} else {
node = path;
}
var setattr = FS.checkOpExists(node.node_ops.setattr, {{{ cDefs.EPERM }}});
setattr(node, {
mode: (mode & {{{ cDefs.S_IALLUGO }}}) | (node.mode & ~{{{ cDefs.S_IALLUGO }}}),
ctime: Date.now(),
dontFollow
});
FS.doChmod(null, node, dontFollow);
},
lchmod(path, mode) {
FS.chmod(path, mode, true);
},
fchmod(fd, mode) {
var stream = FS.getStreamChecked(fd);
FS.chmod(stream.node, mode);
FS.doChmod(stream, stream.node, false);
},
doChown(stream, node, dontFollow) {
FS.doSetAttr(stream, node, {
timestamp: Date.now(),
dontFollow
// we ignore the uid / gid for now
});
},
chown(path, uid, gid, dontFollow) {
var node;
Expand All @@ -1001,31 +1022,16 @@ FS.staticInit();
} else {
node = path;
}
var setattr = FS.checkOpExists(node.node_ops.setattr, {{{ cDefs.EPERM }}});
setattr(node, {
timestamp: Date.now(),
dontFollow
// we ignore the uid / gid for now
});
FS.doChown(null, node, dontFollow);
},
lchown(path, uid, gid) {
FS.chown(path, uid, gid, true);
},
fchown(fd, uid, gid) {
var stream = FS.getStreamChecked(fd);
FS.chown(stream.node, uid, gid);
FS.doChown(stream, stream.node, dontFollow);
},
truncate(path, len) {
if (len < 0) {
throw new FS.ErrnoError({{{ cDefs.EINVAL }}});
}
var node;
if (typeof path == 'string') {
var lookup = FS.lookupPath(path, { follow: true });
node = lookup.node;
} else {
node = path;
}
doTruncate(stream, node, len) {
if (FS.isDir(node.mode)) {
throw new FS.ErrnoError({{{ cDefs.EISDIR }}});
}
Expand All @@ -1036,18 +1042,30 @@ FS.staticInit();
if (errCode) {
throw new FS.ErrnoError(errCode);
}
var setattr = FS.checkOpExists(node.node_ops.setattr, {{{ cDefs.EPERM }}});
setattr(node, {
FS.doSetAttr(stream, node, {
size: len,
timestamp: Date.now()
});
},
truncate(path, len) {
if (len < 0) {
throw new FS.ErrnoError({{{ cDefs.EINVAL }}});
}
var node;
if (typeof path == 'string') {
var lookup = FS.lookupPath(path, { follow: true });
node = lookup.node;
} else {
node = path;
}
FS.doTruncate(null, node, len);
},
ftruncate(fd, len) {
var stream = FS.getStreamChecked(fd);
if ((stream.flags & {{{ cDefs.O_ACCMODE }}}) === {{{ cDefs.O_RDONLY}}}) {
if (len < 0 || (stream.flags & {{{ cDefs.O_ACCMODE }}}) === {{{ cDefs.O_RDONLY}}}) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this a separate fix?

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

throw new FS.ErrnoError({{{ cDefs.EINVAL }}});
}
FS.truncate(stream.node, len);
FS.doTruncate(stream, stream.node, len);
},
utime(path, atime, mtime) {
var lookup = FS.lookupPath(path, { follow: true });
Expand Down
6 changes: 2 additions & 4 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5845,10 +5845,8 @@ def test_fs_64bit(self):
@crossplatform
@with_all_fs
def test_fs_stat_unnamed_file_descriptor(self):
noderawfs = '-DNODERAWFS' in self.emcc_args
wasmfs = self.get_setting('WASMFS')
if not (noderawfs or wasmfs):
self.skipTest('TODO: doesnt work in memfs or nodefs')
if '-DNODEFS' in self.emcc_args:
self.skipTest('TODO: doesnt work in nodefs')
self.do_runf('fs/test_stat_unnamed_file_descriptor.c', 'success')

@requires_node
Expand Down
Loading