-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Fix IDBFS autoPersist on mkdir #24799
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
sbc100
merged 3 commits into
emscripten-core:main
from
handlerug:patch-fix-idbfs-mkdir-autopersist
Aug 6, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
/* | ||
* 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 <assert.h> | ||
#include <stdbool.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <emscripten.h> | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
#include <sys/stat.h> | ||
#include <errno.h> | ||
|
||
EM_JS_DEPS(deps, "$callUserCallback"); | ||
|
||
enum { | ||
TEST_CASE_OPEN, | ||
TEST_CASE_CLOSE, | ||
TEST_CASE_SYMLINK, | ||
TEST_CASE_UNLINK, | ||
TEST_CASE_RENAME, | ||
TEST_CASE_MKDIR, | ||
}; | ||
|
||
static void test_case_open(void) { | ||
switch (TEST_PHASE) { | ||
case 1: { | ||
int fd = open("/working1/file", O_RDWR | O_CREAT | O_EXCL, 0777); | ||
assert(fd != -1); | ||
break; | ||
} | ||
case 2: { | ||
struct stat st; | ||
int res = lstat("/working1/file", &st); | ||
assert(res == 0); | ||
assert(st.st_size == 0); | ||
break; | ||
} | ||
default: | ||
assert(false); | ||
} | ||
} | ||
|
||
static void test_case_close(void) { | ||
switch (TEST_PHASE) { | ||
case 1: { | ||
int fd = open("/working1/file", O_RDWR | O_CREAT | O_EXCL, 0777); | ||
assert(fd != -1); | ||
break; | ||
} | ||
case 2: { | ||
int fd = open("/working1/file", O_RDWR | O_CREAT, 0777); | ||
assert(fd != -1); | ||
ssize_t bytes_written = write(fd, "foo", 3); | ||
assert(bytes_written == 3); | ||
int res = close(fd); | ||
assert(res == 0); | ||
break; | ||
} | ||
case 3: { | ||
struct stat st; | ||
int res = lstat("/working1/file", &st); | ||
assert(res == 0); | ||
assert(st.st_size == 3); | ||
break; | ||
} | ||
default: | ||
assert(false); | ||
} | ||
} | ||
|
||
static void test_case_symlink(void) { | ||
switch (TEST_PHASE) { | ||
case 1: { | ||
int fd = open("/working1/file", O_RDWR | O_CREAT | O_EXCL, 0777); | ||
assert(fd != -1); | ||
break; | ||
} | ||
case 2: { | ||
int res = symlink("/working1/file", "/working1/symlink"); | ||
assert(res == 0); | ||
break; | ||
} | ||
case 3: { | ||
struct stat st; | ||
int res = lstat("/working1/symlink", &st); | ||
assert(res == 0); | ||
break; | ||
} | ||
default: | ||
assert(false); | ||
} | ||
} | ||
|
||
static void test_case_unlink(void) { | ||
switch (TEST_PHASE) { | ||
case 1: { | ||
int fd = open("/working1/file", O_RDWR | O_CREAT | O_EXCL, 0777); | ||
assert(fd != -1); | ||
break; | ||
} | ||
case 2: { | ||
int res = unlink("/working1/file"); | ||
assert(res == 0); | ||
break; | ||
} | ||
case 3: { | ||
struct stat st; | ||
int res = lstat("/working1/file", &st); | ||
assert(res == -1); | ||
assert(errno == ENOENT); | ||
break; | ||
} | ||
default: | ||
assert(false); | ||
} | ||
} | ||
|
||
static void test_case_rename(void) { | ||
switch (TEST_PHASE) { | ||
case 1: { | ||
int fd = open("/working1/file", O_RDWR | O_CREAT | O_EXCL, 0777); | ||
assert(fd != -1); | ||
break; | ||
} | ||
case 2: { | ||
int res = rename("/working1/file", "/working1/file_renamed"); | ||
assert(res == 0); | ||
break; | ||
} | ||
case 3: { | ||
struct stat st; | ||
int res = lstat("/working1/file_renamed", &st); | ||
assert(res == 0); | ||
res = lstat("/working1/file", &st); | ||
assert(res == -1); | ||
assert(errno == ENOENT); | ||
break; | ||
} | ||
default: | ||
assert(false); | ||
} | ||
} | ||
|
||
static void test_case_mkdir(void) { | ||
switch (TEST_PHASE) { | ||
case 1: { | ||
int res = mkdir("/working1/dir", 0777); | ||
assert(res == 0); | ||
break; | ||
} | ||
case 2: { | ||
struct stat st; | ||
int res = lstat("/working1/dir", &st); | ||
assert(res == 0); | ||
break; | ||
} | ||
default: | ||
assert(false); | ||
} | ||
} | ||
|
||
EMSCRIPTEN_KEEPALIVE | ||
void finish(void) { | ||
emscripten_force_exit(0); | ||
} | ||
|
||
EMSCRIPTEN_KEEPALIVE | ||
void test(void) { | ||
switch (TEST_CASE) { | ||
case TEST_CASE_OPEN: test_case_open(); break; | ||
case TEST_CASE_CLOSE: test_case_close(); break; | ||
case TEST_CASE_SYMLINK: test_case_symlink(); break; | ||
case TEST_CASE_UNLINK: test_case_unlink(); break; | ||
case TEST_CASE_RENAME: test_case_rename(); break; | ||
case TEST_CASE_MKDIR: test_case_mkdir(); break; | ||
default: assert(false); | ||
} | ||
|
||
EM_ASM({ | ||
// Wait until IDBFS has persisted before exiting | ||
runOnceIDBFSIdle(() => { | ||
callUserCallback(_finish); | ||
}); | ||
}); | ||
} | ||
|
||
int main(void) { | ||
EM_ASM({ | ||
globalThis.runOnceIDBFSIdle = (callback) => { | ||
const { mount } = FS.lookupPath('/working1').node; | ||
assert('idbPersistState' in mount, 'mount object must have idbPersistState'); | ||
if (mount.idbPersistState !== 0) { | ||
// IDBFS hasn't finished persisting. Check again after all pending tasks have executed | ||
setTimeout(() => runOnceIDBFSIdle(callback), 0); | ||
return; | ||
} | ||
callback(); | ||
}; | ||
|
||
FS.mkdir('/working1'); | ||
FS.mount(IDBFS, { | ||
autoPersist: true | ||
}, '/working1'); | ||
}); | ||
|
||
if (TEST_PHASE == 1) { | ||
EM_ASM({ | ||
// The first phase of a test case must start from an empty filesystem. | ||
// Erase persisted state by overwriting the contents of IndexedDB | ||
// with our empty in-memory filesystem. | ||
FS.syncfs(false, (err) => { | ||
assert(!err); | ||
callUserCallback(_test); | ||
}); | ||
}); | ||
} else if (TEST_PHASE > 1) { | ||
EM_ASM({ | ||
// All subsequent phases rely on the effects of phases before them. | ||
// Load the persisted filesystem from IndexedDB into memory. | ||
FS.syncfs(true, (err) => { | ||
assert(!err); | ||
|
||
// FS.syncfs() may run operations on the in-memory filesystem which | ||
// might trigger IDBFS.queuePersist() calls. These queued calls will | ||
// also persist modifications made by the test. We want to verify that | ||
// each operation we test calls IDBFS.queuePersist() on its own, so | ||
// the interference from FS.syncfs() is unwanted. | ||
// Wait until the IDBFS mount has been persisted. | ||
runOnceIDBFSIdle(() => { | ||
callUserCallback(_test); | ||
}); | ||
}); | ||
}); | ||
} else { | ||
assert(false); | ||
} | ||
|
||
emscripten_exit_with_live_runtime(); | ||
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
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.
Is this a separate bug? What was the effect of this bug?
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.
Yes. The effect wasn't observable to users because the code doesn't strictly check for equality with
0
andundefined
is considered falsy:emscripten/src/lib/libidbfs.js
Line 35 in 052cd1a
This fix is needed for the
runOnceIDBFSIdle
test helper.