Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a checkOpExists helper for common code pattern. NFC #23305

Merged
merged 10 commits into from
Jan 10, 2025
39 changes: 18 additions & 21 deletions src/library_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,12 @@ FS.staticInit();
}
return FS.nodePermissions(node, FS.flagsToPermissionString(flags));
},
checkOpExists(op, err) {
if (!op) {
throw new FS.ErrnoError(err);
}
return op;
},

//
// streams
Expand Down Expand Up @@ -895,10 +901,8 @@ FS.staticInit();
readdir(path) {
var lookup = FS.lookupPath(path, { follow: true });
var node = lookup.node;
if (!node.node_ops.readdir) {
throw new FS.ErrnoError({{{ cDefs.ENOTDIR }}});
}
return node.node_ops.readdir(node);
var readdir = FS.checkOpExists(node.node_ops.readdir, {{{ cDefs.ENOTDIR }}});
return readdir(node);
},
unlink(path) {
var lookup = FS.lookupPath(path, { parent: true });
Expand Down Expand Up @@ -951,10 +955,8 @@ FS.staticInit();
if (!node) {
throw new FS.ErrnoError({{{ cDefs.ENOENT }}});
}
if (!node.node_ops.getattr) {
throw new FS.ErrnoError({{{ cDefs.EPERM }}});
}
return node.node_ops.getattr(node);
var getattr = FS.checkOpExists(node.node_ops.getattr, {{{ cDefs.EPERM }}});
return getattr(node);
},
lstat(path) {
return FS.stat(path, true);
Expand All @@ -967,10 +969,8 @@ FS.staticInit();
} else {
node = path;
}
if (!node.node_ops.setattr) {
throw new FS.ErrnoError({{{ cDefs.EPERM }}});
}
node.node_ops.setattr(node, {
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
Expand All @@ -991,10 +991,8 @@ FS.staticInit();
} else {
node = path;
}
if (!node.node_ops.setattr) {
throw new FS.ErrnoError({{{ cDefs.EPERM }}});
}
node.node_ops.setattr(node, {
var setattr = FS.checkOpExists(node.node_ops.setattr, {{{ cDefs.EPERM }}});
setattr(node, {
timestamp: Date.now(),
dontFollow
// we ignore the uid / gid for now
Expand All @@ -1018,9 +1016,6 @@ FS.staticInit();
} else {
node = path;
}
if (!node.node_ops.setattr) {
throw new FS.ErrnoError({{{ cDefs.EPERM }}});
}
Comment on lines -1021 to -1023
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This changes which error we throw if there are multiple problems.

if (FS.isDir(node.mode)) {
throw new FS.ErrnoError({{{ cDefs.EISDIR }}});
}
Expand All @@ -1031,7 +1026,8 @@ FS.staticInit();
if (errCode) {
throw new FS.ErrnoError(errCode);
}
node.node_ops.setattr(node, {
var setattr = FS.checkOpExists(node.node_ops.setattr, {{{ cDefs.EPERM }}});
setattr(node, {
size: len,
timestamp: Date.now()
});
Expand All @@ -1046,7 +1042,8 @@ FS.staticInit();
utime(path, atime, mtime) {
var lookup = FS.lookupPath(path, { follow: true });
var node = lookup.node;
node.node_ops.setattr(node, {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Previously there was no error handling for setattr being missing, so if we called utime on a read only node it would crash.

var setattr = FS.checkOpExists(node.node_ops.setattr, {{{ cDefs.EPERM }}});
setattr(node, {
atime: atime,
mtime: mtime
});
Expand Down
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors1.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8343
8350
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors1.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20271
20272
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors2.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8325
8334
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors2.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20239
20240
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9346
9349
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24039
24040
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except_wasm.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8288
8296
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except_wasm.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20164
20165
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8288
8296
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20164
20165
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_lto.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8357
8364
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_lto.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20347
20348
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_mangle.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9351
9355
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_mangle.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24039
24040
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_noexcept.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8343
8350
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_noexcept.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20271
20272
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_js_fs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7650
7652
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_js_fs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
18817
18819
Loading