-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[nodefs] Don't try to resolve absolute symlinks outside of the VFS #21805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
a5fe8cd
readlink method returns grand parent when parent path equals current …
mho22 7d672ac
Merge branch 'emscripten-core:main' into patch-1
mho22 5941467
Improved readlink behavior to accept absolute paths
ca48b26
Improved readlink behavior to accept absolute paths
a39210b
Improved readlink behavior to understand absolute paths
521a95e
Merge branch 'main' into patch-1
mho22 122ff6a
Merge branch 'main' into patch-1
mho22 26a514a
Merge branch 'main' into patch-1
mho22 5300efc
Merge branch 'main' into patch-1
mho22 38c0631
Cleared readlink relative path manipulation
mho22 5abe4b7
Merge branch 'patch-1' of github.com:mho22/emscripten into patch-1
mho22 fdf4a4c
Improved readlink behavior to understand absolute paths
mho22 bd746f1
Improved readlink behavior to understand absolute paths
mho22 94b6d6d
Improved readlink behavior to understand absolute paths
mho22 8eefae8
Improved readlink behavior to understand absolute paths
mho22 d51fe58
Revert resolving outside VFS absolute symlinks
mho22 445b52a
Revert resolving outside VFS absolute symlinks
mho22 271c523
Revert resolving outside VFS absolute symlinks
mho22 82298ce
Revert resolving outside VFS absolute symlinks
mho22 a340985
Revert resolving outside VFS absolute symlinks
mho22 faaa6f7
Revert resolving outside VFS absolute symlinks
mho22 4a81b4b
Merge branch 'main' into patch-1
mho22 0ef6e4f
Revert resolving outside VFS absolute symlinks
mho22 bf23b63
Merge branch 'patch-1' of github.com:mho22/emscripten into patch-1
mho22 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,39 +17,70 @@ | |
| #include <emscripten.h> | ||
| #endif | ||
|
|
||
| void setup() { | ||
| int rtn = mkdir("working", 0777); | ||
| assert(rtn == 0); | ||
| #if defined(__EMSCRIPTEN__) && defined(NODEFS) | ||
| EM_ASM(FS.mount(NODEFS, { root: '.' }, 'working')); | ||
| #endif | ||
| rtn = chdir("working"); | ||
| void makedir(char *dir) { | ||
| int rtn = mkdir(dir, 0777); | ||
| assert(rtn == 0); | ||
| symlink("../test/../there!", "link"); | ||
| int fd = open("file", O_RDWR | O_CREAT, 0777); | ||
| } | ||
|
|
||
| void makefile(char *file, char *content) { | ||
|
||
| int fd = open(file, O_RDWR | O_CREAT, 0777); | ||
| assert(fd >= 0); | ||
| rtn = write(fd, "test", 5); | ||
| assert(rtn == 5); | ||
| int rtn = write(fd, content, strlen(content)); | ||
| assert(rtn == strlen(content)); | ||
| close(fd); | ||
| rtn = mkdir("folder", 0777); | ||
| } | ||
|
|
||
| void makelink(char *link, char *path) { | ||
| int rtn = symlink(link, path); | ||
| assert(rtn == 0); | ||
| } | ||
|
|
||
| int main() { | ||
| setup(); | ||
| void changedir(char *dir) { | ||
| int rtn = chdir(dir); | ||
| assert(rtn == 0); | ||
| } | ||
|
|
||
| char* files[] = {"link", "file", "folder"}; | ||
| char buffer[256] = {0}; | ||
| int ret; | ||
| void setup() { | ||
| makedir("working"); | ||
| #if defined(__EMSCRIPTEN__) && defined(NODEFS) | ||
| EM_ASM(FS.mount(NODEFS, { root: '.' }, 'working')); | ||
| #endif | ||
| changedir("working"); | ||
| makelink("../test/../there!", "link"); | ||
| makefile("file", "test"); | ||
| makedir("directory"); | ||
| makedir("directory/subdirectory"); | ||
| makefile("directory/subdirectory/file", "Subdirectory"); | ||
|
|
||
| makedir("relative"); | ||
| makedir("relative/subrelative"); | ||
| makefile("relative/file", "Relative"); | ||
| makefile("relative/subrelative/file", "Subrelative"); | ||
| makelink("../relative/file", "directory/relative"); | ||
| makelink("../../relative/subrelative/file", "directory/subdirectory/subrelative"); | ||
| makelink("directory/subdirectory/file", "subdirectoryrelative"); | ||
|
|
||
| makedir("absolute"); | ||
| makedir("absolute/subabsolute"); | ||
| makefile("absolute/file", "Absolute"); | ||
| makefile("absolute/subabsolute/file", "Subabsolute"); | ||
| makelink("/working/absolute/file", "/working/directory/absolute"); | ||
| makelink("/working/absolute/subabsolute/file", "/working/directory/subdirectory/subabsolute"); | ||
| makelink("/working/directory/subdirectory/file", "/working/subdirectoryabsolute"); | ||
| } | ||
|
|
||
| void test_reading_existing_symlinks() { | ||
| char* files[] = {"link", "file", "directory"}; | ||
|
|
||
| for (int i = 0; i < sizeof files / sizeof files[0]; i++) { | ||
| printf("readlink(%s)\n", files[i]); | ||
| ret = readlink(files[i], buffer, 256); | ||
| printf("errno: %s\n", strerror(errno)); | ||
| if (ret < 0) { | ||
| printf("not a link\n\n"); | ||
| char buffer[256] = {0}; | ||
| int rtn = readlink(files[i], buffer, 256); | ||
| printf("readlink: '%s'\n", files[i]); | ||
| printf("errno: %s\n\n", strerror(errno)); | ||
| if (rtn < 0) { | ||
| 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. | ||
|
|
@@ -59,56 +90,125 @@ int main() { | |
| #else | ||
| assert(strcmp(buffer, "/there!") == 0); | ||
| #endif | ||
| assert(strlen(buffer) == ret); | ||
| assert(strlen(buffer) == rtn); | ||
| errno = 0; | ||
| printf("\n"); | ||
| } | ||
| } | ||
|
|
||
| printf("symlink/overwrite\n"); | ||
| ret = symlink("new-nonexistent-path", "link"); | ||
| assert(ret == -1); | ||
| void test_overwriting_symlink() { | ||
| int rtn = symlink("new-nonexistent-path", "link"); | ||
| assert(rtn == -1); | ||
| assert(errno == EEXIST); | ||
| errno = 0; | ||
| } | ||
|
|
||
| printf("\nsymlink/normal\n"); | ||
| ret = symlink("new-nonexistent-path", "folder/link"); | ||
| assert(ret == 0); | ||
| void test_creating_symlink() { | ||
| int rtn = symlink("new-nonexistent-path", "directory/link"); | ||
| assert(rtn == 0); | ||
| assert(errno == 0); | ||
| errno = 0; | ||
|
|
||
| printf("\nreadlink(created link)\n"); | ||
| ret = readlink("folder/link", buffer, 256); | ||
| 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/folder/new-nonexistent-path") == 0); | ||
| assert(strcmp(buffer, "/working/directory/new-nonexistent-path") == 0); | ||
| #endif | ||
| assert(strlen(buffer) == ret); | ||
| assert(strlen(buffer) == rtn); | ||
| errno = 0; | ||
| } | ||
|
|
||
| void test_reading_shortened_symlink() { | ||
| char buffer[256] = {0}; | ||
| readlink("directory/link", buffer, 256); | ||
| buffer[0] = buffer[1] = buffer[2] = buffer[3] = buffer[4] = buffer[5] = '*'; | ||
| printf("\nreadlink(short buffer)\n"); | ||
| ret = readlink("link", buffer, 4); | ||
| int rtn = readlink("link", buffer, 4); | ||
| assert(errno == 0); | ||
| assert(ret == 4); | ||
| assert(rtn == 4); | ||
| #if !defined(__EMSCRIPTEN__) || defined(WASMFS) | ||
| assert(strcmp(buffer, "../t**nexistent-path") == 0); | ||
| #else | ||
| assert(strcmp(buffer, "/the**ng/folder/new-nonexistent-path") == 0); | ||
| assert(strcmp(buffer, "/the**ng/directory/new-nonexistent-path") == 0); | ||
| #endif | ||
| errno = 0; | ||
| } | ||
|
|
||
| void test_noticing_loop_in_symlink() { | ||
| // FS.lookupPath should notice the symlink loop and return ELOOP, not go into | ||
| // an infinite recurse. | ||
| // | ||
| // This test doesn't work in wasmfs -- probably because access sees the | ||
| // symlink and returns true without bothering to chase the symlink | ||
| symlink("./linkX/inside", "./linkX"); | ||
| ret = access("linkX", F_OK); | ||
| assert(ret == -1); | ||
| symlink("./loop-link/inside", "./loop-link"); | ||
| int rtn = access("loop-link", F_OK); | ||
| assert(rtn == -1); | ||
| assert(errno == ELOOP); | ||
| errno = 0; | ||
| } | ||
|
|
||
|
|
||
| void test_relative_path_symlinks() { | ||
| char* parents[] = { | ||
| "/working/directory/", | ||
| "/working/directory/subdirectory/", | ||
| "/working/" | ||
| }; | ||
|
|
||
| char* links[] = { | ||
| "relative", | ||
| "subrelative", | ||
| "subdirectoryrelative", | ||
| }; | ||
|
|
||
| for (int i = 0; i < sizeof links / sizeof links[0]; i++) { | ||
| int rtn = chdir(parents[i]); | ||
| assert(rtn == 0); | ||
| char symlink[256] = {0}; | ||
| strcat(strcpy(symlink, parents[i]), links[i]); | ||
| printf("symlink: '%s'\n", symlink); | ||
| char buf[256] = {0}; | ||
| rtn = readlink(links[i], buf, 256); | ||
| FILE *fd = fopen(buf, "r"); | ||
| assert(fd); | ||
| char buffer[13] = {0}; | ||
| rtn = fread(buffer, 1, 13, fd); | ||
| assert(rtn <= 13); | ||
| printf("buffer: '%s'\n\n", buffer); | ||
| fclose(fd); | ||
| } | ||
| } | ||
|
|
||
| void test_absolute_path_symlinks() { | ||
| char* links[] = { | ||
| "/working/directory/absolute", | ||
| "/working/directory/subdirectory/subabsolute", | ||
| "/working/subdirectoryabsolute" | ||
| }; | ||
|
|
||
| for (int i = 0; i < sizeof links / sizeof links[0]; i++) { | ||
| printf("symlink: '%s'\n", links[i]); | ||
| char buf[256] = {0}; | ||
| readlink(links[i], buf, 256); | ||
| FILE *fd = fopen(buf, "r"); | ||
| assert(fd); | ||
| char buffer[13] = {0}; | ||
| int rtn = fread(buffer, 1, 13, fd); | ||
| assert(rtn <= 13); | ||
| printf("buffer: '%s'\n\n", buffer); | ||
| fclose(fd); | ||
| } | ||
| } | ||
|
|
||
| int main() { | ||
| setup(); | ||
| test_reading_existing_symlinks(); | ||
| test_overwriting_symlink(); | ||
| test_creating_symlink(); | ||
| test_reading_shortened_symlink(); | ||
| test_noticing_loop_in_symlink(); | ||
| test_relative_path_symlinks(); | ||
| test_absolute_path_symlinks(); | ||
| return 0; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,26 @@ | ||
| readlink(link) | ||
| readlink: 'link' | ||
| errno: No error information | ||
|
|
||
| readlink(file) | ||
| readlink: 'file' | ||
| errno: Invalid argument | ||
| not a link | ||
|
|
||
| readlink(folder) | ||
| readlink: 'directory' | ||
| errno: Invalid argument | ||
| not a link | ||
|
|
||
| symlink/overwrite | ||
| symlink: '/working/directory/relative' | ||
| buffer: 'Relative' | ||
|
|
||
| symlink/normal | ||
| symlink: '/working/directory/subdirectory/subrelative' | ||
| buffer: 'Subrelative' | ||
|
|
||
| readlink(created link) | ||
| symlink: '/working/subdirectoryrelative' | ||
| buffer: 'Subdirectory' | ||
|
|
||
| readlink(short buffer) | ||
| symlink: '/working/directory/absolute' | ||
| buffer: 'Absolute' | ||
|
|
||
| symlink: '/working/directory/subdirectory/subabsolute' | ||
| buffer: 'Subabsolute' | ||
|
|
||
| symlink: '/working/subdirectoryabsolute' | ||
| buffer: 'Subdirectory' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you revert all these unralated changes to the ChangeLog where newlines are removed?
BTW, the way I setup my editor is to (1) show trailing whitespace and (2) have a shortcut to strip it all, but not to have it do this automatically.. otherwise you end up with changes like this.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the insights and sorry for the inconvenience.
I indeed had these three lines in my
vscode/settings.jsonfile enabled :I should be more careful.