Skip to content

Commit

Permalink
Factor out try/cartch duplication in nodefs code. NFC (#21922)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 committed May 9, 2024
1 parent a488fca commit 7c7ea15
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 168 deletions.
126 changes: 40 additions & 86 deletions src/library_nodefs.js
Expand Up @@ -49,6 +49,17 @@ addToLibrary({
#endif
return ERRNO_CODES[code];
},
tryFSOperation(f) {
try {
return f();
} catch (e) {
if (!e.code) throw e;
// node under windows can return code 'UNKNOWN' here:
// https://github.com/emscripten-core/emscripten/issues/15468
if (e.code === 'UNKNOWN') throw new FS.ErrnoError({{{ cDefs.EINVAL }}});
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
}
},
mount(mount) {
#if ASSERTIONS
assert(ENVIRONMENT_IS_NODE);
Expand All @@ -66,18 +77,15 @@ addToLibrary({
},
getMode(path) {
var stat;
try {
return NODEFS.tryFSOperation(() => {
stat = fs.lstatSync(path);
if (NODEFS.isWindows) {
// Node.js on Windows never represents permission bit 'x', so
// propagate read bits to execute bits
stat.mode |= (stat.mode & {{{ cDefs.S_IRUSR | cDefs.S_IRGRP | cDefs.S_IROTH }}}) >> 2;
}
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
}
return stat.mode;
return stat.mode;
});
},
realPath(node) {
var parts = [];
Expand Down Expand Up @@ -109,17 +117,14 @@ addToLibrary({
}
return newFlags;
},

node_ops: {
getattr(node) {
var path = NODEFS.realPath(node);
var stat;
try {
stat = fs.lstatSync(path);
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
}
// node.js v0.10.20 doesn't report blksize and blocks on Windows. Fake them with default blksize of 4096.
NODEFS.tryFSOperation(() => stat = fs.lstatSync(path));
// node.js v0.10.20 doesn't report blksize and blocks on Windows. Fake
// them with default blksize of 4096.
// See http://support.microsoft.com/kb/140365
if (NODEFS.isWindows && !stat.blksize) {
stat.blksize = 4096;
Expand All @@ -145,7 +150,7 @@ addToLibrary({
},
setattr(node, attr) {
var path = NODEFS.realPath(node);
try {
NODEFS.tryFSOperation(() => {
if (attr.mode !== undefined) {
fs.chmodSync(path, attr.mode);
// update the common node structure mode as well
Expand All @@ -158,10 +163,7 @@ addToLibrary({
if (attr.size !== undefined) {
fs.truncateSync(path, attr.size);
}
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
}
});
},
lookup(parent, name) {
var path = PATH.join2(NODEFS.realPath(parent), name);
Expand All @@ -172,132 +174,84 @@ addToLibrary({
var node = NODEFS.createNode(parent, name, mode, dev);
// create the backing node for this in the fs root as well
var path = NODEFS.realPath(node);
try {
NODEFS.tryFSOperation(() => {
if (FS.isDir(node.mode)) {
fs.mkdirSync(path, node.mode);
} else {
fs.writeFileSync(path, '', { mode: node.mode });
}
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
}
});
return node;
},
rename(oldNode, newDir, newName) {
var oldPath = NODEFS.realPath(oldNode);
var newPath = PATH.join2(NODEFS.realPath(newDir), newName);
try {
fs.renameSync(oldPath, newPath);
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
}
NODEFS.tryFSOperation(() => fs.renameSync(oldPath, newPath));
oldNode.name = newName;
},
unlink(parent, name) {
var path = PATH.join2(NODEFS.realPath(parent), name);
try {
fs.unlinkSync(path);
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
}
NODEFS.tryFSOperation(() => fs.unlinkSync(path));
},
rmdir(parent, name) {
var path = PATH.join2(NODEFS.realPath(parent), name);
try {
fs.rmdirSync(path);
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
}
NODEFS.tryFSOperation(() => fs.rmdirSync(path));
},
readdir(node) {
var path = NODEFS.realPath(node);
try {
return fs.readdirSync(path);
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
}
return NODEFS.tryFSOperation(() => fs.readdirSync(path));
},
symlink(parent, newName, oldPath) {
var newPath = PATH.join2(NODEFS.realPath(parent), newName);
try {
fs.symlinkSync(oldPath, newPath);
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
}
NODEFS.tryFSOperation(() => fs.symlinkSync(oldPath, newPath));
},
readlink(node) {
var path = NODEFS.realPath(node);
try {
return fs.readlinkSync(path);
} catch (e) {
if (!e.code) throw e;
// node under windows can return code 'UNKNOWN' here:
// https://github.com/emscripten-core/emscripten/issues/15468
if (e.code === 'UNKNOWN') throw new FS.ErrnoError({{{ cDefs.EINVAL }}});
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
}
return NODEFS.tryFSOperation(() => fs.readlinkSync(path));
},
},
stream_ops: {
open(stream) {
var path = NODEFS.realPath(stream.node);
try {
NODEFS.tryFSOperation(() => {
if (FS.isFile(stream.node.mode)) {
stream.shared.refcount = 1;
stream.nfd = fs.openSync(path, NODEFS.flagsForNode(stream.flags));
}
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
}
});
},
close(stream) {
try {
NODEFS.tryFSOperation(() => {
if (FS.isFile(stream.node.mode) && stream.nfd && --stream.shared.refcount === 0) {
fs.closeSync(stream.nfd);
}
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
}
});
},
dup(stream) {
stream.shared.refcount++;
},
read(stream, buffer, offset, length, position) {
// Node.js < 6 compatibility: node errors on 0 length reads
if (length === 0) return 0;
try {
return fs.readSync(stream.nfd, new Int8Array(buffer.buffer, offset, length), 0, length, position);
} catch (e) {
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
}
return NODEFS.tryFSOperation(() =>
fs.readSync(stream.nfd, new Int8Array(buffer.buffer, offset, length), 0, length, position)
);
},
write(stream, buffer, offset, length, position) {
try {
return fs.writeSync(stream.nfd, new Int8Array(buffer.buffer, offset, length), 0, length, position);
} catch (e) {
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
}
return NODEFS.tryFSOperation(() =>
fs.writeSync(stream.nfd, new Int8Array(buffer.buffer, offset, length), 0, length, position)
);
},
llseek(stream, offset, whence) {
var position = offset;
if (whence === {{{ cDefs.SEEK_CUR }}}) {
position += stream.position;
} else if (whence === {{{ cDefs.SEEK_END }}}) {
if (FS.isFile(stream.node.mode)) {
try {
NODEFS.tryFSOperation(() => {
var stat = fs.fstatSync(stream.nfd);
position += stat.size;
} catch (e) {
throw new FS.ErrnoError(NODEFS.convertNodeCode(e));
}
});
}
}

Expand Down

0 comments on commit 7c7ea15

Please sign in to comment.