Skip to content

Commit 8a4747c

Browse files
authored
Inline doMknod/doReadlink/doAccess. NFC (#16752)
These previously had multiple call sites but no longer do.
1 parent 2e7b6fb commit 8a4747c

File tree

1 file changed

+39
-49
lines changed

1 file changed

+39
-49
lines changed

src/library_syscall.js

+39-49
Original file line numberDiff line numberDiff line change
@@ -77,52 +77,6 @@ var SyscallsLibrary = {
7777
var buffer = HEAPU8.slice(addr, addr + len);
7878
FS.msync(stream, buffer, offset, len, flags);
7979
},
80-
doMknod: function(path, mode, dev) {
81-
// we don't want this in the JS API as it uses mknod to create all nodes.
82-
switch (mode & {{{ cDefine('S_IFMT') }}}) {
83-
case {{{ cDefine('S_IFREG') }}}:
84-
case {{{ cDefine('S_IFCHR') }}}:
85-
case {{{ cDefine('S_IFBLK') }}}:
86-
case {{{ cDefine('S_IFIFO') }}}:
87-
case {{{ cDefine('S_IFSOCK') }}}:
88-
break;
89-
default: return -{{{ cDefine('EINVAL') }}};
90-
}
91-
FS.mknod(path, mode, dev);
92-
return 0;
93-
},
94-
doReadlink: function(path, buf, bufsize) {
95-
if (bufsize <= 0) return -{{{ cDefine('EINVAL') }}};
96-
var ret = FS.readlink(path);
97-
98-
var len = Math.min(bufsize, lengthBytesUTF8(ret));
99-
var endChar = HEAP8[buf+len];
100-
stringToUTF8(ret, buf, bufsize+1);
101-
// readlink is one of the rare functions that write out a C string, but does never append a null to the output buffer(!)
102-
// stringToUTF8() always appends a null byte, so restore the character under the null byte after the write.
103-
HEAP8[buf+len] = endChar;
104-
105-
return len;
106-
},
107-
doAccess: function(path, amode) {
108-
if (amode & ~{{{ cDefine('S_IRWXO') }}}) {
109-
// need a valid mode
110-
return -{{{ cDefine('EINVAL') }}};
111-
}
112-
var lookup = FS.lookupPath(path, { follow: true });
113-
var node = lookup.node;
114-
if (!node) {
115-
return -{{{ cDefine('ENOENT') }}};
116-
}
117-
var perms = '';
118-
if (amode & {{{ cDefine('R_OK') }}}) perms += 'r';
119-
if (amode & {{{ cDefine('W_OK') }}}) perms += 'w';
120-
if (amode & {{{ cDefine('X_OK') }}}) perms += 'x';
121-
if (perms /* otherwise, they've just passed F_OK */ && FS.nodePermissions(node, perms)) {
122-
return -{{{ cDefine('EACCES') }}};
123-
}
124-
return 0;
125-
},
12680
doReadv: function(stream, iov, iovcnt, offset) {
12781
var ret = 0;
12882
for (var i = 0; i < iovcnt; i++) {
@@ -886,7 +840,18 @@ var SyscallsLibrary = {
886840
#endif
887841
path = SYSCALLS.getStr(path);
888842
path = SYSCALLS.calculateAt(dirfd, path);
889-
return SYSCALLS.doMknod(path, mode, dev);
843+
// we don't want this in the JS API as it uses mknod to create all nodes.
844+
switch (mode & {{{ cDefine('S_IFMT') }}}) {
845+
case {{{ cDefine('S_IFREG') }}}:
846+
case {{{ cDefine('S_IFCHR') }}}:
847+
case {{{ cDefine('S_IFBLK') }}}:
848+
case {{{ cDefine('S_IFIFO') }}}:
849+
case {{{ cDefine('S_IFSOCK') }}}:
850+
break;
851+
default: return -{{{ cDefine('EINVAL') }}};
852+
}
853+
FS.mknod(path, mode, dev);
854+
return 0;
890855
},
891856
__syscall_fchownat: function(dirfd, path, owner, group, flags) {
892857
#if SYSCALL_DEBUG
@@ -949,7 +914,16 @@ var SyscallsLibrary = {
949914
__syscall_readlinkat: function(dirfd, path, buf, bufsize) {
950915
path = SYSCALLS.getStr(path);
951916
path = SYSCALLS.calculateAt(dirfd, path);
952-
return SYSCALLS.doReadlink(path, buf, bufsize);
917+
if (bufsize <= 0) return -{{{ cDefine('EINVAL') }}};
918+
var ret = FS.readlink(path);
919+
920+
var len = Math.min(bufsize, lengthBytesUTF8(ret));
921+
var endChar = HEAP8[buf+len];
922+
stringToUTF8(ret, buf, bufsize+1);
923+
// readlink is one of the rare functions that write out a C string, but does never append a null to the output buffer(!)
924+
// stringToUTF8() always appends a null byte, so restore the character under the null byte after the write.
925+
HEAP8[buf+len] = endChar;
926+
return len;
953927
},
954928
__syscall_fchmodat: function(dirfd, path, mode, varargs) {
955929
#if SYSCALL_DEBUG
@@ -969,7 +943,23 @@ var SyscallsLibrary = {
969943
assert(flags === 0);
970944
#endif
971945
path = SYSCALLS.calculateAt(dirfd, path);
972-
return SYSCALLS.doAccess(path, amode);
946+
if (amode & ~{{{ cDefine('S_IRWXO') }}}) {
947+
// need a valid mode
948+
return -{{{ cDefine('EINVAL') }}};
949+
}
950+
var lookup = FS.lookupPath(path, { follow: true });
951+
var node = lookup.node;
952+
if (!node) {
953+
return -{{{ cDefine('ENOENT') }}};
954+
}
955+
var perms = '';
956+
if (amode & {{{ cDefine('R_OK') }}}) perms += 'r';
957+
if (amode & {{{ cDefine('W_OK') }}}) perms += 'w';
958+
if (amode & {{{ cDefine('X_OK') }}}) perms += 'x';
959+
if (perms /* otherwise, they've just passed F_OK */ && FS.nodePermissions(node, perms)) {
960+
return -{{{ cDefine('EACCES') }}};
961+
}
962+
return 0;
973963
},
974964
__syscall_utimensat: function(dirfd, path, times, flags) {
975965
path = SYSCALLS.getStr(path);

0 commit comments

Comments
 (0)