Skip to content

Commit

Permalink
Cleanup symlink tests (emscripten-core#21890)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
sbc100 authored May 6, 2024
1 parent fa80cf2 commit 80798ac
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/library_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
34 changes: 14 additions & 20 deletions test/unistd/links.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,32 @@

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#else
#include <sys/stat.h>
#include <fcntl.h>
#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() {
Expand Down
58 changes: 23 additions & 35 deletions test/unistd/symlink_on_nodefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* found in the LICENSE file.
*/

#include <assert.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
Expand All @@ -14,52 +15,39 @@
#include <limits.h>
#include <stdlib.h>

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;
}
7 changes: 2 additions & 5 deletions test/unistd/symlink_on_nodefs.out
Original file line number Diff line number Diff line change
@@ -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'
5 changes: 2 additions & 3 deletions tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 80798ac

Please sign in to comment.