Skip to content

Commit

Permalink
Add a doNodeOp helper for common code pattern. NFC
Browse files Browse the repository at this point in the history
We frequently check if a node op is present, if not raise a specific error,
if it is present call it. This factors out this common pattern.
  • Loading branch information
hoodmane committed Jan 6, 2025
1 parent f604a15 commit d46d6db
Showing 1 changed file with 13 additions and 25 deletions.
38 changes: 13 additions & 25 deletions src/library_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -895,10 +895,7 @@ 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);
return FS.doNodeOp(node, "readdir", {{{ cDefs.ENOTDIR }}});
},
unlink(path) {
var lookup = FS.lookupPath(path, { parent: true });
Expand Down Expand Up @@ -945,16 +942,17 @@ FS.staticInit();
}
return link.node_ops.readlink(link);
},
doNodeOp(node, op, err, ...args) {
var fn = node.node_ops[op];
if (!fn) {
throw new FS.ErrnoError(err);
}
return fn(node, ...args);
},
stat(path, dontFollow) {
var lookup = FS.lookupPath(path, { follow: !dontFollow });
var node = lookup.node;
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);
FS.doNodeOp(node, "getattr", {{{ cDefs.EPERM }}});
},
lstat(path) {
return FS.stat(path, true);
Expand All @@ -967,10 +965,7 @@ FS.staticInit();
} else {
node = path;
}
if (!node.node_ops.setattr) {
throw new FS.ErrnoError({{{ cDefs.EPERM }}});
}
node.node_ops.setattr(node, {
FS.doNodeOp(node, "setattr", {{{ cDefs.EPERM }}}, {
mode: (mode & {{{ cDefs.S_IALLUGO }}}) | (node.mode & ~{{{ cDefs.S_IALLUGO }}}),
ctime: Date.now()
});
Expand All @@ -990,12 +985,8 @@ FS.staticInit();
} else {
node = path;
}
if (!node.node_ops.setattr) {
throw new FS.ErrnoError({{{ cDefs.EPERM }}});
}
node.node_ops.setattr(node, {
FS.doNodeOp(node, "setattr", {{{ cDefs.EPERM }}}, {
timestamp: Date.now()
// we ignore the uid / gid for now
});
},
lchown(path, uid, gid) {
Expand All @@ -1016,9 +1007,6 @@ FS.staticInit();
} else {
node = path;
}
if (!node.node_ops.setattr) {
throw new FS.ErrnoError({{{ cDefs.EPERM }}});
}
if (FS.isDir(node.mode)) {
throw new FS.ErrnoError({{{ cDefs.EISDIR }}});
}
Expand All @@ -1029,7 +1017,7 @@ FS.staticInit();
if (errCode) {
throw new FS.ErrnoError(errCode);
}
node.node_ops.setattr(node, {
FS.doNodeOp(node, "setattr", {{{ cDefs.EPERM }}}, {
size: len,
timestamp: Date.now()
});
Expand All @@ -1044,7 +1032,7 @@ FS.staticInit();
utime(path, atime, mtime) {
var lookup = FS.lookupPath(path, { follow: true });
var node = lookup.node;
node.node_ops.setattr(node, {
FS.doNodeOp(node, "setattr", {{{ cDefs.EPERM }}}, {
atime: atime,
mtime: mtime
});
Expand Down

0 comments on commit d46d6db

Please sign in to comment.