From 5bc3f087dc8a344b182b62f26b39bbf8c95f9b90 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Mon, 25 Nov 2024 12:51:42 +0100 Subject: [PATCH 1/3] Make readlink system call not resolve the link Readlink in linux does not resolve the link, it returns the exact link contents. --- src/library_fs.js | 8 ++++++-- src/library_syscall.js | 2 +- test/unistd/links.c | 16 ---------------- 3 files changed, 7 insertions(+), 19 deletions(-) diff --git a/src/library_fs.js b/src/library_fs.js index 886efa61f4c45..54cccc95dcbce 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -895,7 +895,7 @@ FS.staticInit(); } #endif }, - readlink(path) { + readlink(path, opts = {}) { var lookup = FS.lookupPath(path); var link = lookup.node; if (!link) { @@ -904,7 +904,11 @@ FS.staticInit(); if (!link.node_ops.readlink) { throw new FS.ErrnoError({{{ cDefs.EINVAL }}}); } - return PATH_FS.resolve(FS.getPath(link.parent), link.node_ops.readlink(link)); + const target = link.node_ops.readlink(link); + if (opts.noResolve) { + return target; + } + return PATH_FS.resolve(FS.getPath(link.parent), target); }, stat(path, dontFollow) { var lookup = FS.lookupPath(path, { follow: !dontFollow }); diff --git a/src/library_syscall.js b/src/library_syscall.js index 73dd12fe09fbe..5678b9afe76ff 100644 --- a/src/library_syscall.js +++ b/src/library_syscall.js @@ -919,7 +919,7 @@ var SyscallsLibrary = { path = SYSCALLS.getStr(path); path = SYSCALLS.calculateAt(dirfd, path); if (bufsize <= 0) return -{{{ cDefs.EINVAL }}}; - var ret = FS.readlink(path); + var ret = FS.readlink(path, {noResolve: true}); var len = Math.min(bufsize, lengthBytesUTF8(ret)); var endChar = HEAP8[buf+len]; diff --git a/test/unistd/links.c b/test/unistd/links.c index d587160903346..81de30c60b2be 100644 --- a/test/unistd/links.c +++ b/test/unistd/links.c @@ -81,15 +81,7 @@ void test_reading_existing_symlinks() { continue; } - // WASMFS behaves the same as Linux (and as best as I can tell, the spec), - // seeing the symlink as a string. The old JS FS instead normalizes it and - // returns something modified. - // The same happens in the assertions below. -#if !defined(__EMSCRIPTEN__) || defined(WASMFS) assert(strcmp(buffer, "../test/../there!") == 0); -#else - assert(strcmp(buffer, "/there!") == 0); -#endif assert(strlen(buffer) == rtn); errno = 0; } @@ -111,11 +103,7 @@ void test_creating_symlink() { char buffer[256] = {0}; rtn = readlink("directory/link", buffer, 256); assert(errno == 0); -#if !defined(__EMSCRIPTEN__) || defined(WASMFS) assert(strcmp(buffer, "new-nonexistent-path") == 0); -#else - assert(strcmp(buffer, "/working/directory/new-nonexistent-path") == 0); -#endif assert(strlen(buffer) == rtn); errno = 0; } @@ -127,11 +115,7 @@ void test_reading_shortened_symlink() { int rtn = readlink("link", buffer, 4); assert(errno == 0); assert(rtn == 4); -#if !defined(__EMSCRIPTEN__) || defined(WASMFS) assert(strcmp(buffer, "../t**nexistent-path") == 0); -#else - assert(strcmp(buffer, "/the**ng/directory/new-nonexistent-path") == 0); -#endif errno = 0; } From 838444b6156d3d0ddbf6a56d48ccc4aa6c7c5e67 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Tue, 26 Nov 2024 22:06:37 +0100 Subject: [PATCH 2/3] Remove backwards compatibility --- src/library_fs.js | 7 ++----- src/library_syscall.js | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/library_fs.js b/src/library_fs.js index 54cccc95dcbce..c034b14c402ca 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -895,7 +895,7 @@ FS.staticInit(); } #endif }, - readlink(path, opts = {}) { + readlink(path) { var lookup = FS.lookupPath(path); var link = lookup.node; if (!link) { @@ -904,10 +904,7 @@ FS.staticInit(); if (!link.node_ops.readlink) { throw new FS.ErrnoError({{{ cDefs.EINVAL }}}); } - const target = link.node_ops.readlink(link); - if (opts.noResolve) { - return target; - } + var target = link.node_ops.readlink(link); return PATH_FS.resolve(FS.getPath(link.parent), target); }, stat(path, dontFollow) { diff --git a/src/library_syscall.js b/src/library_syscall.js index 5678b9afe76ff..73dd12fe09fbe 100644 --- a/src/library_syscall.js +++ b/src/library_syscall.js @@ -919,7 +919,7 @@ var SyscallsLibrary = { path = SYSCALLS.getStr(path); path = SYSCALLS.calculateAt(dirfd, path); if (bufsize <= 0) return -{{{ cDefs.EINVAL }}}; - var ret = FS.readlink(path, {noResolve: true}); + var ret = FS.readlink(path); var len = Math.min(bufsize, lengthBytesUTF8(ret)); var endChar = HEAP8[buf+len]; From 57013e3fc2d6902b73d28d9f81e637675a8221fb Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Tue, 26 Nov 2024 22:07:27 +0100 Subject: [PATCH 3/3] Fix --- src/library_fs.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/library_fs.js b/src/library_fs.js index c034b14c402ca..b50fb155a42e6 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -904,8 +904,7 @@ FS.staticInit(); if (!link.node_ops.readlink) { throw new FS.ErrnoError({{{ cDefs.EINVAL }}}); } - var target = link.node_ops.readlink(link); - return PATH_FS.resolve(FS.getPath(link.parent), target); + return link.node_ops.readlink(link); }, stat(path, dontFollow) { var lookup = FS.lookupPath(path, { follow: !dontFollow });