Skip to content

Commit

Permalink
Make WASMFS FS.createPath match the FS version behavior on EEXIST
Browse files Browse the repository at this point in the history
If the file packager tries to add content to *an existing path* in the
filesystem (e.g., if the filesystem already has some directories, OPFS
content, loading multiple package, etc), the different behavior of
createPath causes an exception on WASMFS and no exception on JS FS.
  • Loading branch information
JoeOsborn committed Feb 5, 2025
1 parent 39ca935 commit af57bd6
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
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);
} catch (e) {
if (e.errno != {{{ cDefs.EEXIST }}}) throw e;
}
}
parent = current;
}
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):
def setup():
create_file('script1.js', '''
Module._set(456);
''')
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('script2.js', 'w'))
self.run_process([FILE_PACKAGER, 'test2.data', '--preload', 'sub/file2.txt@/target/file2.txt'], stdout=open('script3.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
66 changes: 66 additions & 0 deletions test/test_emscripten_overlapped_package.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2013 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>

int value = 0;

EMSCRIPTEN_KEEPALIVE void set(int x) {
printf("set! %d\n", x);
value = x;
}

void load3() {
printf("load3\n");
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 error3() {
printf("fail3\n");
}

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

void load2() {
printf("load2\n");
emscripten_async_load_script("script3.js", load3, error3);
}

void load1() {
printf("load1\n");
assert(value == 456);

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;
}

0 comments on commit af57bd6

Please sign in to comment.