Skip to content

Commit 723eeaf

Browse files
authored
Add a checkOpExists helper for common code pattern. NFC (#23305)
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.
1 parent dde19fa commit 723eeaf

19 files changed

+36
-39
lines changed

src/library_fs.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,12 @@ FS.staticInit();
433433
}
434434
return FS.nodePermissions(node, FS.flagsToPermissionString(flags));
435435
},
436+
checkOpExists(op, err) {
437+
if (!op) {
438+
throw new FS.ErrnoError(err);
439+
}
440+
return op;
441+
},
436442

437443
//
438444
// streams
@@ -895,10 +901,8 @@ FS.staticInit();
895901
readdir(path) {
896902
var lookup = FS.lookupPath(path, { follow: true });
897903
var node = lookup.node;
898-
if (!node.node_ops.readdir) {
899-
throw new FS.ErrnoError({{{ cDefs.ENOTDIR }}});
900-
}
901-
return node.node_ops.readdir(node);
904+
var readdir = FS.checkOpExists(node.node_ops.readdir, {{{ cDefs.ENOTDIR }}});
905+
return readdir(node);
902906
},
903907
unlink(path) {
904908
var lookup = FS.lookupPath(path, { parent: true });
@@ -951,10 +955,8 @@ FS.staticInit();
951955
if (!node) {
952956
throw new FS.ErrnoError({{{ cDefs.ENOENT }}});
953957
}
954-
if (!node.node_ops.getattr) {
955-
throw new FS.ErrnoError({{{ cDefs.EPERM }}});
956-
}
957-
return node.node_ops.getattr(node);
958+
var getattr = FS.checkOpExists(node.node_ops.getattr, {{{ cDefs.EPERM }}});
959+
return getattr(node);
958960
},
959961
lstat(path) {
960962
return FS.stat(path, true);
@@ -967,10 +969,8 @@ FS.staticInit();
967969
} else {
968970
node = path;
969971
}
970-
if (!node.node_ops.setattr) {
971-
throw new FS.ErrnoError({{{ cDefs.EPERM }}});
972-
}
973-
node.node_ops.setattr(node, {
972+
var setattr = FS.checkOpExists(node.node_ops.setattr, {{{ cDefs.EPERM }}});
973+
setattr(node, {
974974
mode: (mode & {{{ cDefs.S_IALLUGO }}}) | (node.mode & ~{{{ cDefs.S_IALLUGO }}}),
975975
ctime: Date.now(),
976976
dontFollow
@@ -991,10 +991,8 @@ FS.staticInit();
991991
} else {
992992
node = path;
993993
}
994-
if (!node.node_ops.setattr) {
995-
throw new FS.ErrnoError({{{ cDefs.EPERM }}});
996-
}
997-
node.node_ops.setattr(node, {
994+
var setattr = FS.checkOpExists(node.node_ops.setattr, {{{ cDefs.EPERM }}});
995+
setattr(node, {
998996
timestamp: Date.now(),
999997
dontFollow
1000998
// we ignore the uid / gid for now
@@ -1018,9 +1016,6 @@ FS.staticInit();
10181016
} else {
10191017
node = path;
10201018
}
1021-
if (!node.node_ops.setattr) {
1022-
throw new FS.ErrnoError({{{ cDefs.EPERM }}});
1023-
}
10241019
if (FS.isDir(node.mode)) {
10251020
throw new FS.ErrnoError({{{ cDefs.EISDIR }}});
10261021
}
@@ -1031,7 +1026,8 @@ FS.staticInit();
10311026
if (errCode) {
10321027
throw new FS.ErrnoError(errCode);
10331028
}
1034-
node.node_ops.setattr(node, {
1029+
var setattr = FS.checkOpExists(node.node_ops.setattr, {{{ cDefs.EPERM }}});
1030+
setattr(node, {
10351031
size: len,
10361032
timestamp: Date.now()
10371033
});
@@ -1046,7 +1042,8 @@ FS.staticInit();
10461042
utime(path, atime, mtime) {
10471043
var lookup = FS.lookupPath(path, { follow: true });
10481044
var node = lookup.node;
1049-
node.node_ops.setattr(node, {
1045+
var setattr = FS.checkOpExists(node.node_ops.setattr, {{{ cDefs.EPERM }}});
1046+
setattr(node, {
10501047
atime: atime,
10511048
mtime: mtime
10521049
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8343
1+
8350
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20271
1+
20272
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8325
1+
8334
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20239
1+
20240
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9346
1+
9349
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24039
1+
24040
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8288
1+
8296
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20164
1+
20165
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8288
1+
8296

0 commit comments

Comments
 (0)