Search code examples
cgoogle-chromefile-iogoogle-nativeclient

How to open files from a NaCl Dev Environment application?


I'm trying to get a simple command line application to run in the NaCl Development Environment. But I don't understand why it doesn't want to open files:

#include <stdio.h>
#include <ppapi_simple/ps_main.h>
int my_main (int argc, char ** argv) {
  FILE * f = fopen ("out.txt","w");
  if (f) {
    fputs ("output to the file", f);
    fclose(f);
  } else {
    puts("could not open file");
  }
}
PPAPI_SIMPLE_REGISTER_MAIN(my_main)

Running:

bash.nmf-4.3$ gcc -I"$NACL_SDK_ROOT/include" test.c -lppapi_simple -lnacl_io -lppapi
bash.nmf-4.3$ ./a.out 
could not open file
bash.nmf-4.3$

It's clearly possible for an application to open files in arbitrary locations within the dev environment - I'm using nano to edit the test code! But the naclports version of nano doesn't look like it's been changed in ways that are immediately connected to file manipulation..?

Lua is another app that appears to have only been modified very slightly. It falls somewhere in between, in that it can run test files but only if they're placed in /mnt/html5, and won't load them from the home folder. My test program shows no difference in behaviour if I change it to look in /mnt/html5 though.

NB. my goal here is to build a terminal application I can use within the dev environment alongside Lua and nano and so on, not a browser-based app - I assume that makes some difference to the file handling rules.


Solution

  • Programs run in the NaCl Dev Environment currently need to linked with -lcli_main (which in turn depends on -lnacl_spawn) for an entry point which understands how to communicate with the javascript "kernel" in naclprocess.js. They need this to know what current working directory they were run from, as well as to heard about mounted file systems.

    Programs linked against just ppapi_simple can be run, but will not setup all the mount points the dev environment may expect.

    There is a linker script in the dev env that simplifies linking a command line program -lmingn. For example the test program from the question can be compiled with:

    gcc test.c -o test -lmingn

    NOTE: This linker script had a recently resolved issue, a new version with the fix was published to the store on 5/5/2015.

    In the near future, we have plans to simplify things further, by allowing main to be the entry point.

    Thanks for pointing out the lua port lacks the new entry point! I've filed an issue and will look into fixing it soon: https://code.google.com/p/naclports/issues/detail?id=215