Skip to content

Commit

Permalink
Errno fixes (emscripten-core#8402)
Browse files Browse the repository at this point in the history
 *   Use cDefine at compile time where possible, to avoid using ERRNO_CODES which closure errors on not being present (happens in later PRs; will be tested there).
 *   Translate node errnos and our syscall errnos directly (just need to be negated).
  • Loading branch information
kripken authored and VirtualTim committed May 21, 2019
1 parent 15f323a commit 586dd66
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 42 deletions.
18 changes: 9 additions & 9 deletions src/library_memfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mergeInto(LibraryManager.library, {
createNode: function(parent, name, mode, dev) {
if (FS.isBlkdev(mode) || FS.isFIFO(mode)) {
// no supported
throw new FS.ErrnoError(ERRNO_CODES.EPERM);
throw new FS.ErrnoError({{{ cDefine('EPERM') }}});
}
if (!MEMFS.ops_table) {
MEMFS.ops_table = {
Expand Down Expand Up @@ -192,7 +192,7 @@ mergeInto(LibraryManager.library, {
}
},
lookup: function(parent, name) {
throw FS.genericErrors[ERRNO_CODES.ENOENT];
throw FS.genericErrors[{{{ cDefine('ENOENT') }}}];
},
mknod: function(parent, name, mode, dev) {
return MEMFS.createNode(parent, name, mode, dev);
Expand All @@ -207,7 +207,7 @@ mergeInto(LibraryManager.library, {
}
if (new_node) {
for (var i in new_node.contents) {
throw new FS.ErrnoError(ERRNO_CODES.ENOTEMPTY);
throw new FS.ErrnoError({{{ cDefine('ENOTEMPTY') }}});
}
}
}
Expand All @@ -223,7 +223,7 @@ mergeInto(LibraryManager.library, {
rmdir: function(parent, name) {
var node = FS.lookupNode(parent, name);
for (var i in node.contents) {
throw new FS.ErrnoError(ERRNO_CODES.ENOTEMPTY);
throw new FS.ErrnoError({{{ cDefine('ENOTEMPTY') }}});
}
delete parent.contents[name];
},
Expand All @@ -244,7 +244,7 @@ mergeInto(LibraryManager.library, {
},
readlink: function(node) {
if (!FS.isLink(node.mode)) {
throw new FS.ErrnoError(ERRNO_CODES.EINVAL);
throw new FS.ErrnoError({{{ cDefine('EINVAL') }}});
}
return node.link;
},
Expand Down Expand Up @@ -332,7 +332,7 @@ mergeInto(LibraryManager.library, {
}
}
if (position < 0) {
throw new FS.ErrnoError(ERRNO_CODES.EINVAL);
throw new FS.ErrnoError({{{ cDefine('EINVAL') }}});
}
return position;
},
Expand All @@ -342,7 +342,7 @@ mergeInto(LibraryManager.library, {
},
mmap: function(stream, buffer, offset, length, position, prot, flags) {
if (!FS.isFile(stream.node.mode)) {
throw new FS.ErrnoError(ERRNO_CODES.ENODEV);
throw new FS.ErrnoError({{{ cDefine('ENODEV') }}});
}
var ptr;
var allocated;
Expand All @@ -366,15 +366,15 @@ mergeInto(LibraryManager.library, {
allocated = true;
ptr = _malloc(length);
if (!ptr) {
throw new FS.ErrnoError(ERRNO_CODES.ENOMEM);
throw new FS.ErrnoError({{{ cDefine('ENOMEM') }}});
}
buffer.set(contents, ptr);
}
return { ptr: ptr, allocated: allocated };
},
msync: function(stream, buffer, offset, length, mmapFlags) {
if (!FS.isFile(stream.node.mode)) {
throw new FS.ErrnoError(ERRNO_CODES.ENODEV);
throw new FS.ErrnoError({{{ cDefine('ENODEV') }}});
}
if (mmapFlags & {{{ cDefine('MAP_PRIVATE') }}}) {
// MAP_PRIVATE calls need not to be synced back to underlying fs
Expand Down
36 changes: 18 additions & 18 deletions src/library_nodefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mergeInto(LibraryManager.library, {
},
createNode: function (parent, name, mode, dev) {
if (!FS.isDir(mode) && !FS.isFile(mode) && !FS.isLink(mode)) {
throw new FS.ErrnoError(ERRNO_CODES.EINVAL);
throw new FS.ErrnoError({{{ cDefine('EINVAL') }}});
}
var node = FS.createNode(parent, name, mode);
node.node_ops = NODEFS.node_ops;
Expand All @@ -56,7 +56,7 @@ mergeInto(LibraryManager.library, {
}
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
throw new FS.ErrnoError(-e.errno); // syscall errnos are negated, node's are not
}
return stat.mode;
},
Expand Down Expand Up @@ -88,7 +88,7 @@ mergeInto(LibraryManager.library, {
if (!flags) {
return newFlags;
} else {
throw new FS.ErrnoError(ERRNO_CODES.EINVAL);
throw new FS.ErrnoError({{{ cDefine('EINVAL') }}});
}
},
node_ops: {
Expand All @@ -99,7 +99,7 @@ mergeInto(LibraryManager.library, {
stat = fs.lstatSync(path);
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
throw new FS.ErrnoError(-e.errno);
}
// 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
Expand Down Expand Up @@ -142,7 +142,7 @@ mergeInto(LibraryManager.library, {
}
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
throw new FS.ErrnoError(-e.errno);
}
},
lookup: function (parent, name) {
Expand All @@ -162,7 +162,7 @@ mergeInto(LibraryManager.library, {
}
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
throw new FS.ErrnoError(-e.errno);
}
return node;
},
Expand All @@ -173,7 +173,7 @@ mergeInto(LibraryManager.library, {
fs.renameSync(oldPath, newPath);
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
throw new FS.ErrnoError(-e.errno);
}
},
unlink: function(parent, name) {
Expand All @@ -182,7 +182,7 @@ mergeInto(LibraryManager.library, {
fs.unlinkSync(path);
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
throw new FS.ErrnoError(-e.errno);
}
},
rmdir: function(parent, name) {
Expand All @@ -191,7 +191,7 @@ mergeInto(LibraryManager.library, {
fs.rmdirSync(path);
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
throw new FS.ErrnoError(-e.errno);
}
},
readdir: function(node) {
Expand All @@ -200,7 +200,7 @@ mergeInto(LibraryManager.library, {
return fs.readdirSync(path);
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
throw new FS.ErrnoError(-e.errno);
}
},
symlink: function(parent, newName, oldPath) {
Expand All @@ -209,7 +209,7 @@ mergeInto(LibraryManager.library, {
fs.symlinkSync(oldPath, newPath);
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
throw new FS.ErrnoError(-e.errno);
}
},
readlink: function(node) {
Expand All @@ -220,7 +220,7 @@ mergeInto(LibraryManager.library, {
return path;
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
throw new FS.ErrnoError(-e.errno);
}
},
},
Expand All @@ -233,7 +233,7 @@ mergeInto(LibraryManager.library, {
}
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
throw new FS.ErrnoError(-e.errno);
}
},
close: function (stream) {
Expand All @@ -243,7 +243,7 @@ mergeInto(LibraryManager.library, {
}
} catch (e) {
if (!e.code) throw e;
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
throw new FS.ErrnoError(-e.errno);
}
},
read: function (stream, buffer, offset, length, position) {
Expand All @@ -252,14 +252,14 @@ mergeInto(LibraryManager.library, {
try {
return fs.readSync(stream.nfd, NODEFS.bufferFrom(buffer.buffer), offset, length, position);
} catch (e) {
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
throw new FS.ErrnoError(-e.errno);
}
},
write: function (stream, buffer, offset, length, position) {
try {
return fs.writeSync(stream.nfd, NODEFS.bufferFrom(buffer.buffer), offset, length, position);
} catch (e) {
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
throw new FS.ErrnoError(-e.errno);
}
},
llseek: function (stream, offset, whence) {
Expand All @@ -272,13 +272,13 @@ mergeInto(LibraryManager.library, {
var stat = fs.fstatSync(stream.nfd);
position += stat.size;
} catch (e) {
throw new FS.ErrnoError(ERRNO_CODES[e.code]);
throw new FS.ErrnoError(-e.errno);
}
}
}

if (position < 0) {
throw new FS.ErrnoError(ERRNO_CODES.EINVAL);
throw new FS.ErrnoError({{{ cDefine('EINVAL') }}});
}

return position;
Expand Down
12 changes: 6 additions & 6 deletions src/library_tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ mergeInto(LibraryManager.library, {
open: function(stream) {
var tty = TTY.ttys[stream.node.rdev];
if (!tty) {
throw new FS.ErrnoError(ERRNO_CODES.ENODEV);
throw new FS.ErrnoError({{{ cDefine('ENODEV') }}});
}
stream.tty = tty;
stream.seekable = false;
Expand All @@ -56,18 +56,18 @@ mergeInto(LibraryManager.library, {
},
read: function(stream, buffer, offset, length, pos /* ignored */) {
if (!stream.tty || !stream.tty.ops.get_char) {
throw new FS.ErrnoError(ERRNO_CODES.ENXIO);
throw new FS.ErrnoError({{{ cDefine('ENXIO') }}});
}
var bytesRead = 0;
for (var i = 0; i < length; i++) {
var result;
try {
result = stream.tty.ops.get_char(stream.tty);
} catch (e) {
throw new FS.ErrnoError(ERRNO_CODES.EIO);
throw new FS.ErrnoError({{{ cDefine('EIO') }}});
}
if (result === undefined && bytesRead === 0) {
throw new FS.ErrnoError(ERRNO_CODES.EAGAIN);
throw new FS.ErrnoError({{{ cDefine('EAGAIN') }}});
}
if (result === null || result === undefined) break;
bytesRead++;
Expand All @@ -80,14 +80,14 @@ mergeInto(LibraryManager.library, {
},
write: function(stream, buffer, offset, length, pos) {
if (!stream.tty || !stream.tty.ops.put_char) {
throw new FS.ErrnoError(ERRNO_CODES.ENXIO);
throw new FS.ErrnoError({{{ cDefine('ENXIO') }}});
}
try {
for (var i = 0; i < length; i++) {
stream.tty.ops.put_char(stream.tty, buffer[offset+i]);
}
} catch (e) {
throw new FS.ErrnoError(ERRNO_CODES.EIO);
throw new FS.ErrnoError({{{ cDefine('EIO') }}});
}
if (length) {
stream.node.timestamp = Date.now();
Expand Down
18 changes: 9 additions & 9 deletions src/library_workerfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,19 @@ mergeInto(LibraryManager.library, {
}
},
lookup: function(parent, name) {
throw new FS.ErrnoError(ERRNO_CODES.ENOENT);
throw new FS.ErrnoError({{{ cDefine('ENOENT') }}});
},
mknod: function (parent, name, mode, dev) {
throw new FS.ErrnoError(ERRNO_CODES.EPERM);
throw new FS.ErrnoError({{{ cDefine('EPERM') }}});
},
rename: function (oldNode, newDir, newName) {
throw new FS.ErrnoError(ERRNO_CODES.EPERM);
throw new FS.ErrnoError({{{ cDefine('EPERM') }}});
},
unlink: function(parent, name) {
throw new FS.ErrnoError(ERRNO_CODES.EPERM);
throw new FS.ErrnoError({{{ cDefine('EPERM') }}});
},
rmdir: function(parent, name) {
throw new FS.ErrnoError(ERRNO_CODES.EPERM);
throw new FS.ErrnoError({{{ cDefine('EPERM') }}});
},
readdir: function(node) {
var entries = ['.', '..'];
Expand All @@ -124,10 +124,10 @@ mergeInto(LibraryManager.library, {
return entries;
},
symlink: function(parent, newName, oldPath) {
throw new FS.ErrnoError(ERRNO_CODES.EPERM);
throw new FS.ErrnoError({{{ cDefine('EPERM') }}});
},
readlink: function(node) {
throw new FS.ErrnoError(ERRNO_CODES.EPERM);
throw new FS.ErrnoError({{{ cDefine('EPERM') }}});
},
},
stream_ops: {
Expand All @@ -139,7 +139,7 @@ mergeInto(LibraryManager.library, {
return chunk.size;
},
write: function (stream, buffer, offset, length, position) {
throw new FS.ErrnoError(ERRNO_CODES.EIO);
throw new FS.ErrnoError({{{ cDefine('EIO') }}});
},
llseek: function (stream, offset, whence) {
var position = offset;
Expand All @@ -151,7 +151,7 @@ mergeInto(LibraryManager.library, {
}
}
if (position < 0) {
throw new FS.ErrnoError(ERRNO_CODES.EINVAL);
throw new FS.ErrnoError({{{ cDefine('EINVAL') }}});
}
return position;
},
Expand Down

0 comments on commit 586dd66

Please sign in to comment.