Question

I'm trying to get some kind of persistence working with emscripten...

What I'm trying to do (as a proof of concept) is every time the page is hit the time is added to the end of a file

currently it seems to recreate the file each time its run...

here's the pertinent bit of code

EM_ASM(
    FS.mkdir('/IDBFS');
    FS.mount(IDBFS, {}, '/IDBFS');
    FS.syncfs(true, function (err) {
        assert(!err);
    }); // sync FROM backing store
);

int c;
FILE *file;
file = fopen("/IDBFS/test.txt", "a");

if (file) {
    time_t rawtime;
    struct tm * timeinfo;

    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    fprintf ( file, "time:%s\n", asctime (timeinfo) );

    fclose(file);

    EM_ASM(     
        FS.syncfs(function (err) { // sync TO backing store
            assert(!err);
        });
    );

    file = fopen("/IDBFS/test.txt", "r");
    while ((c = getc(file)) != EOF)
        printf("%c",c);
    fclose(file);
}
Was it helpful?

Solution

The problem here seems to be that syncfs is an asynchronous operation, yet you're treating it as if its synchronous.

You'll have to wait until syncfs's callback has been fired (when syncing from the backing store) to open and read from your file.

As an example, see: https://github.com/kripken/emscripten/blob/master/tests/fs/test_idbfs_sync.c

It uses ccall() to call into a C function once the callback has completed. Also, notice the use of emscripten_exit_with_live_runtime to keep the program alive even when main has returned.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top