Hi All,

I have got a test-app which is used for testing our application. When we 
run test-app we create output.xml for each of the test-cases which were 
executed.
Since in emscripten we can't create a new file on local file system i.e on 
local machine. On digging I come around IDBFS file system support in 
emscripten : by using this file system we can mock writing new file to 
local file system where the actual file are written to indexed db.

So for testing I have created POC attached is the cpp file : untitled.cpp

Command used :  

emcc untitled.cpp -o filetest.html -lidbfs.js -s FETCH=1 -s 
FORCE_FILESYSTEM=1 -s NO_EXIT_RUNTIME=1

Let me just brief you about the code :

1) I have mounted following directory in IDBFS : "working1"

 EM_ASM(

        FS.mkdir('/working1');

        FS.mount(IDBFS, {}, '/working1');

        

        FS.syncfs(true, function (err) {

          assert(!err);

        });

    );


2) call util_create func which in turn call test func. In test func I am 
creating new file "/working1/twrite.txt" *"/working1/**working2/*
*twrite1.txt"* and "/working1/twrite2.txt"

3) call sync_idbfs() which is used to persists changes in indexed db i.e. 
working1



Error : when call read_fs(func used for reading file) on file :*"/working1/*
*working2/**twrite1.txt" ->*  I am getting following error : stdio streams 
had content in them that was not flushed. you should set EXIT_RUNTIME to 1 
(see the FAQ), or make sure to emit a newline when you printf etc.

please note that in all my printf statement I have got \n and also the flag 
is passed while compiling.


And also, I am not able to see following file 
*"/working1/**working2/**twrite1.txt" 
in indexed db of browser but I am able to see following file twrite.txt and 
twrite2.txt.*

*So my question is that , has anyone created a sub directory using IDBFS 
file system. And pointer on the same i.e what all thing I can try.*

Please note that I have unsuccessfully tried following thing :
1) before mounting creating sub directory as well i.e. FS.mkdir('/working1'); 
and FS.mkdir('/working1/working2');

2) before creating new file in sub directory i,e 
FS.mkdir('/working1/working2'); 
and creating new file in sub dir.

Regards,

Anshul



 

-- 
You received this message because you are subscribed to the Google Groups 
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/emscripten-discuss/02ded535-e074-4592-a61e-90b29019b11d%40googlegroups.com.
#include <stdio.h>
#include <stdlib.h>
#include <emscripten.h>

void sync_idbfs() {
    EM_ASM(
        FS.syncfs(false,function (err) {});
    );
}

extern "C" void EMSCRIPTEN_KEEPALIVE test(){
    FILE* fp = fopen("/working1/twrite.txt", "rt");
    if(!fp){
      fp = fopen("/working1/twrite.txt", "a");
    }

    if (fp) {
        fprintf(fp, "This is twrite.txt.\n");
        fclose(fp);
    }

    FILE* fp1 = fopen("/working1/working2/twrite1.txt", "rt");
    if(!fp1){
      fp1 = fopen("/working1/working2/twrite1.txt", "a");
    }

    if (fp1) {
        fprintf(fp1, "This is twrite1.txt.\n");
        fclose(fp1);
    }

    FILE* fp2 = fopen("/working1/twrite2.txt", "rt");
    if(!fp2){
      fp2 = fopen("/working1/twrite2.txt", "a");
    }

    if (fp2) {
        fprintf(fp2, "This is twrite2.txt.\n");
        fclose(fp2);
    }

    
}

void util_create(){
    test();
}

void read_fs(const char* fname) {
    FILE* fp = fopen(fname, "rt");
    if(!fp){
      printf("cannot open file");
    }
    if (fp) {
        while (!feof(fp)) {
            char c = fgetc(fp);
            if (c != EOF) {
                putchar(c);
            }
        }
        fclose(fp);
    }
}

void write_fs() {

    FILE* fp = fopen("t3.txt", "a");
    if (fp) {
        fprintf(fp, "This is t3.txt.\n");
        fclose(fp);
    }
}

int main() {
    printf("testing started\n");
    write_fs();

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

    util_create();

    sync_idbfs();

    printf("Second time reading testing started after appending\n");
    read_fs("/working1/twrite.txt");

    printf("Third time reading testing started after appending\n");
    read_fs("/working1/twrite2.txt");
    read_fs("/working1/working2/twrite1.txt");
    return 0;
}

Reply via email to