Skip to content
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

Make WASMFS FS.createPath match the FS version behavior on EEXIST #23603

Merged
merged 4 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/libfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1626,7 +1626,7 @@ FS.staticInit();
try {
FS.mkdir(current);
} catch (e) {
// ignore EEXIST
if (e.errno != {{{ cDefs.EEXIST }}}) throw e;
}
parent = current;
}
Expand Down
6 changes: 5 additions & 1 deletion src/lib/libwasmfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ FS.init();
if (!wasmFSPreloadingFlushed) {
wasmFSPreloadedDirs.push({parentPath: parent, childName: part});
} else {
FS.mkdir(current);
try {
FS.mkdir(current);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually little confused by this fix. I tried running wasmfs.test_fs_js_api and it passed both with or without this change.

Looking into it it looks the repeated calls to FS.mkdir here always seem to succeed. I modified the code here to add some extra debugging and I see:


mkdir /home/nested1
0
mkdir /home/nested2
0

I'm a little confused how you were hitting an error here? I must be missing something.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm even stranger, browser.test_emscripten_overlapped_package seem to pass both before and after this test.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I wasn't running browser.test_emscripten_overlapped_package_wasmfs.. that one does fail.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the issue with test_fs_js_api: #23645

} catch (e) {
if (e.errno != {{{ cDefs.EEXIST }}}) throw e;
JoeOsborn marked this conversation as resolved.
Show resolved Hide resolved
}
JoeOsborn marked this conversation as resolved.
Show resolved Hide resolved
}
parent = current;
}
Expand Down
16 changes: 16 additions & 0 deletions test/fs/test_fs_js_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ EM_JS(void, test_fs_open, (), {
assert(createFileNotHere && createFileNotHere.fd >= 0);
});

// createPath should succeed when called on existing paths ( https://github.com/emscripten-core/emscripten/issues/23602 )
EM_JS(void, test_fs_createPath, (), {
FS.createPath('/', 'home', true, true);
FS.createPath('/home', 'nested1', true, true);
FS.createPath('/home', 'nested2', true, true);
FS.writeFile('/home/nested1/test.txt', 'a=1\nb=2\n');
FS.writeFile('/home/nested2/test.txt', 'a=2\nb=4\n');
var read1 = FS.readFile('/home/nested1/test.txt',{encoding:'utf8'});
var read2 = FS.readFile('/home/nested2/test.txt',{encoding:'utf8'});
console.log("r1",read1);
console.log("r2",read2);
assert(read1 == 'a=1\nb=2\n');
assert(read2 == 'a=2\nb=4\n');
});

EM_JS(void, test_fs_rename, (), {
FS.mkdir('renamedir');
FS.writeFile('renamedir/renametestfile', "");
Expand Down Expand Up @@ -456,6 +471,7 @@ void cleanup() {

int main() {
test_fs_open();
test_fs_createPath();
test_fs_rename();
test_fs_readlink();
test_fs_read();
Expand Down
16 changes: 16 additions & 0 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1881,6 +1881,22 @@ def setup():
shutil.copy(Path('sub/test.data'), '.')
self.btest_exit('test_emscripten_async_load_script.c', args=['-sFORCE_FILESYSTEM'])

@also_with_wasmfs
def test_emscripten_overlapped_package(self):
JoeOsborn marked this conversation as resolved.
Show resolved Hide resolved
# test that a program that loads multiple file_packager.py packages has a correctly initialized filesystem.
# this exercises https://github.com/emscripten-core/emscripten/issues/23602 whose root cause was a difference
# between JS FS and WASMFS behavior.
def setup():
JoeOsborn marked this conversation as resolved.
Show resolved Hide resolved
ensure_dir('sub')
create_file('sub/file1.txt', 'first')
create_file('sub/file2.txt', 'second')

setup()
self.run_process([FILE_PACKAGER, 'test.data', '--preload', 'sub/file1.txt@/target/file1.txt'], stdout=open('script1.js', 'w'))
self.run_process([FILE_PACKAGER, 'test2.data', '--preload', 'sub/file2.txt@/target/file2.txt'], stdout=open('script2.js', 'w'))
self.btest_exit('test_emscripten_overlapped_package.c', args=['-sFORCE_FILESYSTEM'])
self.clear()

def test_emscripten_api_infloop(self):
self.btest_exit('emscripten_api_browser_infloop.cpp', assert_returncode=7)

Expand Down
46 changes: 46 additions & 0 deletions test/test_emscripten_overlapped_package.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2025 The Emscripten Authors. All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include <emscripten.h>

void error2() {
printf("fail2\n");
}

void load2() {
char buffer[10];
memset(buffer, 0, 10);
FILE *f = fopen("/target/file1.txt", "r");
assert(f);
fread(buffer, 1, 5, f);
fclose(f);
assert(strcmp(buffer, "first") == 0);

memset(buffer, 0, 10);
f = fopen("/target/file2.txt", "r");
assert(f);
fread(buffer, 1, 6, f);
fclose(f);
assert(strcmp(buffer, "second") == 0);
exit(0);
}

void load1() {
emscripten_async_load_script("script2.js", load2, error2);
}

void error1() {
printf("fail1\n");
}

int main() {
emscripten_async_load_script("script1.js", load1, error1);
return 99;
}