From 80798acaf1a6ac359948b146df50732f9c55830e Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 6 May 2024 15:47:11 -0700 Subject: [PATCH] Cleanup symlink tests (#21890) - Add some more tests to `links.c` and make sure it runs on native linux. - Simplify symlink_on_nodefs.c by using assert. Also, fix `utils.delete_content` so that it also removes dangling symlinks. Without this the dangling symlinks in the test output directory are not removed. --- .circleci/config.yml | 2 +- src/library_fs.js | 2 +- test/test_core.py | 2 +- test/unistd/links.c | 34 ++++++++---------- test/unistd/symlink_on_nodefs.c | 58 ++++++++++++------------------- test/unistd/symlink_on_nodefs.out | 7 ++-- tools/utils.py | 5 ++- 7 files changed, 44 insertions(+), 66 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index d4debee57cdb..0f567d42ae0a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -609,7 +609,7 @@ jobs: wasmfs.test_stat wasmfs.test_fstatat wasmfs.test_futimens - wasmfs.test_unistd_links_memfs + wasmfs.test_unistd_links wasmfs.test_fcntl_open wasmfs.test_fs_js_api wasmfs.test_fs_llseek diff --git a/src/library_fs.js b/src/library_fs.js index 4435d234984d..cd9562d079a7 100644 --- a/src/library_fs.js +++ b/src/library_fs.js @@ -1014,8 +1014,8 @@ FS.staticInit();` + throw new FS.ErrnoError({{{ cDefs.ENOENT }}}); } flags = typeof flags == 'string' ? FS_modeStringToFlags(flags) : flags; - mode = typeof mode == 'undefined' ? 438 /* 0666 */ : mode; if ((flags & {{{ cDefs.O_CREAT }}})) { + mode = typeof mode == 'undefined' ? 438 /* 0666 */ : mode; mode = (mode & {{{ cDefs.S_IALLUGO }}}) | {{{ cDefs.S_IFREG }}}; } else { mode = 0; diff --git a/test/test_core.py b/test/test_core.py index 1f19d282f007..9dadd9221a3f 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -6068,7 +6068,7 @@ def test_unistd_unlink(self, fs): self.do_runf('unistd/unlink.c', 'success') @parameterized({ - 'memfs': ([], False), + '': ([], False), 'nodefs': (['-DNODEFS', '-lnodefs.js'], True) }) def test_unistd_links(self, args, nodefs): diff --git a/test/unistd/links.c b/test/unistd/links.c index 236f76701cf1..1acc557e5b69 100644 --- a/test/unistd/links.c +++ b/test/unistd/links.c @@ -7,38 +7,32 @@ #include #include +#include #include #include +#include #include #ifdef __EMSCRIPTEN__ #include -#else -#include -#include #endif void setup() { -#ifdef __EMSCRIPTEN__ - EM_ASM( - FS.mkdir('working'); -#if NODEFS - FS.mount(NODEFS, { root: '.' }, 'working'); + int rtn = mkdir("working", 0777); + assert(rtn == 0); +#if defined(__EMSCRIPTEN__) && defined(NODEFS) + EM_ASM(FS.mount(NODEFS, { root: '.' }, 'working')); #endif - FS.chdir('working'); - FS.symlink('../test/../there!', 'link'); - FS.writeFile('file', 'test'); - FS.mkdir('folder'); - ); -#else - mkdir("working", 0777); - chdir("working"); + rtn = chdir("working"); + assert(rtn == 0); symlink("../test/../there!", "link"); - int fd = open("file", O_RDWR); - write(fd, "test", 5); + int fd = open("file", O_RDWR | O_CREAT, 0777); + assert(fd >= 0); + rtn = write(fd, "test", 5); + assert(rtn == 5); close(fd); - mkdir("folder", 0777); -#endif + rtn = mkdir("folder", 0777); + assert(rtn == 0); } int main() { diff --git a/test/unistd/symlink_on_nodefs.c b/test/unistd/symlink_on_nodefs.c index 557b16f89aec..1d16eeaedad9 100644 --- a/test/unistd/symlink_on_nodefs.c +++ b/test/unistd/symlink_on_nodefs.c @@ -5,6 +5,7 @@ * found in the LICENSE file. */ +#include #include #include #include @@ -14,52 +15,39 @@ #include #include -int main() { +void setup() { EM_ASM( - fs.mkdirSync('./new-directory', '0777'); - fs.writeFileSync('./new-directory/test', 'Link it'); - fs.symlinkSync(fs.realpathSync('./new-directory'), './symlink'); + fs.mkdirSync('new-directory', '0777'); + fs.writeFileSync('new-directory/test', 'Link it'); + fs.symlinkSync(fs.realpathSync('new-directory'), 'symlink'); FS.mkdir('working'); FS.mount(NODEFS, { root: '.' }, 'working'); FS.mkdir('direct-link'); - FS.mount(NODEFS, { root: './symlink' }, 'direct-link'); + FS.mount(NODEFS, { root: 'symlink' }, 'direct-link'); ); +} - { - const char* path = "/working/symlink/test"; - printf("reading %s\n", path); +int main() { + setup(); - FILE* fd = fopen(path, "r"); - if (fd == NULL) { - printf("failed to open file %s\n", path); - } - else { - char buffer[8]; - fread(buffer, 1, 7, fd); - buffer[7] = 0; - printf("buffer is %s\n", buffer); - fclose(fd); - } - } + char buf[1024]; + readlink("/working/symlink", buf, 1024); + printf("readlink: %s\n", buf); - printf("\n"); + FILE* fd = fopen("/working/symlink/test", "r"); + assert(fd); + char buffer[8] = {0}; + int rtn = fread(buffer, 1, 7, fd); + assert(rtn == 7); + printf("buffer is '%s'\n", buffer); + fclose(fd); - { - const char* path = "/direct-link/test"; - printf("reading %s\n", path); + // This should fail, since it resolves to ../new-directory which is not + // mounted + fd = fopen("/direct-link/test", "r"); + assert(fd == NULL); - FILE* fd = fopen(path, "r"); - if (fd != NULL) { - // This should not happen, it resolves to ../new-directory which is not mounted - printf("opened file %s\n", path); - fclose(fd); - } - else { - printf("failed to open file %s\n", path); - } - } - return 0; } diff --git a/test/unistd/symlink_on_nodefs.out b/test/unistd/symlink_on_nodefs.out index e8903b372761..71696e626127 100644 --- a/test/unistd/symlink_on_nodefs.out +++ b/test/unistd/symlink_on_nodefs.out @@ -1,5 +1,2 @@ -reading /working/symlink/test -buffer is Link it - -reading /direct-link/test -failed to open file /direct-link/test +readlink: /working/new-directory +buffer is 'Link it' diff --git a/tools/utils.py b/tools/utils.py index 7e1e771902b8..539e519a5393 100644 --- a/tools/utils.py +++ b/tools/utils.py @@ -84,9 +84,8 @@ def write_binary(file_path, contents): def delete_file(filename): """Delete a file (if it exists).""" - if not os.path.exists(filename): - return - os.remove(filename) + if os.path.lexists(filename): + os.remove(filename) def delete_dir(dirname):