title: Can't get NaCl C++ module to load file from within packaged app
body: <p>I have a chrome packaged app that also includes a PNaCl/NaCl C++ module, as well as some data files which the NaCl module needs to read in. However, I am not able to get it to read in the files.</p>
  
  <p>I set it up according to all the documentation and official examples that I could find, as well as the answer to: <a href="https://stackoverflow.com/questions/23416939/how-to-include-a-data-file-in-a-chrome-app-for-a-native-client-module-to-read">How to include a data file in a chrome app for a native client module to read</a></p>
  
  <p>The nacl_io demo that comes with the SDK is able to do this, but it is in C, not C++.</p>
  
  <p>I came up with a simple example, which I'll post below. When you press the button on the page, the NaCl module should load the first character of test.txt and show it. As of now, it always just responds with "-100" (the error value I put in), meaning that it could not open the file, rather than with the first character of the file.</p>
  
  <p>Can anyone suggest some changes that would allow it to work correctly and load the file?</p>
  
  <p>In order to run it, on the Mac at least, I use this command, with all the files in the ./file-test dir:
  /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --load-and-launch-app=./file-test</p>
  
  <p>Note that if you try to use this, you will most likely need to change the NACL_SDK_ROOT path in the makefile.</p>
  
  <h2>file_test.cc</h2>
  
  <pre><code>#include "ppapi/cpp/instance.h"
  #include "ppapi/cpp/module.h"
  #include "ppapi/cpp/var.h"
  
  #include "nacl_io/nacl_io.h"
  #include "sys/mount.h"
  
  class FileTestInstance : public pp::Instance {
   public:
    explicit FileTestInstance(PP_Instance instance) : pp::Instance(instance)
    {
      // initialize nacl file system
      nacl_io_init_ppapi(instance, pp::Module::Get()-&gt;get_browser_interface());
  
      // mount the http root at /http
      mount("", "/http", "httpfs", 0, "");
    }
    virtual ~FileTestInstance() {}
  
    // Receive message from javascript
    virtual void HandleMessage(const pp::Var&amp; var_message) {
      // Open and load from the file  
      int c;
      FILE *file;
      file = fopen("/http/test.txt", "r");
      if (file) {
          c = getc(file);
          fclose(file);
      } else {
          c = -100;
      }
  
      // Send message to JavaScript
      pp::Var var_reply(c);
      PostMessage(var_reply);
    }
  };
  
  class FileTestModule : public pp::Module {
   public:
    FileTestModule() : pp::Module() {}
    virtual ~FileTestModule() {}
  
    virtual pp::Instance* CreateInstance(PP_Instance instance) {
      return new FileTestInstance(instance);
    }
  };
  
  namespace pp {
  Module* CreateModule() {
    return new FileTestModule();
  }
  }  // namespace pp
  </code></pre>
  
  <h2>index.html</h2>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;File Test&lt;/title&gt;
   &lt;script type="text/javascript" src="script.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
    &lt;h1&gt;File Test&lt;/h1&gt;
  
    &lt;input type="button" id="test" name="test" value="Test" /&gt;
  
    &lt;p&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;p&gt;
    &lt;div id="output"&gt;
    &lt;/div&gt;
  
    &lt;p&gt;
      &lt;div id="listener"&gt;
        &lt;embed id="file_test" width=0 height=0 src="file_test.nmf" type="application/x-pnacl" /&gt;
      &lt;/div&gt;
    &lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <h2>script.js</h2>
  
  <pre><code>// outgoing messages
  function postMessage(message) {
   var nacl_module = document.getElementById('file_test')
   nacl_module.postMessage(message);
  }
  
  // incoming messages
  function handleMessage(message_event) {
    var outputDiv = document.getElementById('output');
    outputDiv.textContent = message_event.data;
  }
  
  // button action
  function buttonClicked() {
      postMessage("file");
  }
  
  // set up
  function init() {
      // add listener to nacl module
      var listener = document.getElementById('listener');
      listener.addEventListener('message', handleMessage, true);
  
      // add action to button
      document.getElementById("test").onclick = buttonClicked;
  }
  
  window.onload = init;
  </code></pre>
  
  <h2>main.js</h2>
  
  <pre><code>/**
   * Listens for the app launching then creates the window
   */
  chrome.app.runtime.onLaunched.addListener(function() {
    // Center window on screen.
    var screenWidth = screen.availWidth;
    var screenHeight = screen.availHeight;
    var width = 600;
    var height = 600;
  
    chrome.app.window.create('index.html', {
      id: "File-TestID",
      bounds: {
        width: width,
        height: height,
        left: Math.round((screenWidth-width)/2),
        top: Math.round((screenHeight-height)/2)
      }
    });
  });
  </code></pre>
  
  <h2>file_test.nmf</h2>
  
  <pre><code>{
    "program": {
      "portable": {
        "pnacl-translate": {
          "url": "file_test.pexe"
        }
      }
    }
  }
  </code></pre>
  
  <h2>Makefile</h2>
  
  <pre><code>#
  # Get pepper directory for toolchain and includes.
  #
  # If NACL_SDK_ROOT is not set, then assume where it can be found.
  #
  THIS_MAKEFILE := $(abspath $(lastword $(MAKEFILE_LIST)))
  NACL_SDK_ROOT ?= $(abspath $(dir $(THIS_MAKEFILE))../../nacl_sdk/pepper_33)
  
  # Project Build flags
  WARNINGS := -Wno-long-long -Wall -Wswitch-enum -pedantic -Werror
  CXXFLAGS := -pthread -std=gnu++98 $(WARNINGS)
  
  #
  # Compute tool paths
  #
  GETOS := python $(NACL_SDK_ROOT)/tools/getos.py
  OSHELPERS = python $(NACL_SDK_ROOT)/tools/oshelpers.py
  OSNAME := $(shell $(GETOS))
  RM := $(OSHELPERS) rm
  
  PNACL_TC_PATH := $(abspath $(NACL_SDK_ROOT)/toolchain/$(OSNAME)_pnacl)
  PNACL_CXX := $(PNACL_TC_PATH)/bin/pnacl-clang++
  PNACL_FINALIZE := $(PNACL_TC_PATH)/bin/pnacl-finalize
  CXXFLAGS := -I$(NACL_SDK_ROOT)/include -I$(NACL_SDK_ROOT)/include/pnacl
  LDFLAGS := -L$(NACL_SDK_ROOT)/lib/pnacl/Release -lppapi_cpp -lppapi -lnacl_io
  
  #
  # Disable DOS PATH warning when using Cygwin based tools Windows
  #
  CYGWIN ?= nodosfilewarning
  export CYGWIN
  
  
  # Declare the ALL target first, to make the 'all' target the default build
  all: file_test.pexe
  
  clean:
      $(RM) file_test.pexe file_test.bc
  
  file_test.bc: file_test.cc
      $(PNACL_CXX) -o $@ $&lt; -O2 $(CXXFLAGS) $(LDFLAGS)
  
  file_test.pexe: file_test.bc
      $(PNACL_FINALIZE) -o $@ $&lt;
  </code></pre>
  
  <h2>test.txt</h2>
  
  <pre><code>AAAA
  </code></pre>