| OLD | NEW |
| 1 {{+bindTo:partials.standard_nacl_article}} | 1 {{+bindTo:partials.standard_nacl_article}} |
| 2 | 2 |
| 3 <section id="the-nacl-io-library"> | 3 <section id="the-nacl-io-library"> |
| 4 <span id="nacl-io"></span><h1 id="the-nacl-io-library"><span id="nacl-io"></span
>The nacl_io Library</h1> | 4 <span id="nacl-io"></span><h1 id="the-nacl-io-library"><span id="nacl-io"></span
>The nacl_io Library</h1> |
| 5 <div class="contents local" id="contents" style="display: none"> | 5 <div class="contents local" id="contents" style="display: none"> |
| 6 <ul class="small-gap"> | 6 <ul class="small-gap"> |
| 7 <li><a class="reference internal" href="#introduction" id="id1">Introduction</a>
</li> | 7 <li><a class="reference internal" href="#introduction" id="id1">Introduction</a>
</li> |
| 8 <li><a class="reference internal" href="#using-nacl-io" id="id2">Using nacl_io</
a></li> | 8 <li><a class="reference internal" href="#using-nacl-io" id="id2">Using nacl_io</
a></li> |
| 9 <li><p class="first"><a class="reference internal" href="#the-nacl-io-demo" id="
id3">The nacl_io demo</a></p> | 9 <li><p class="first"><a class="reference internal" href="#the-nacl-io-demo" id="
id3">The nacl_io demo</a></p> |
| 10 <ul class="small-gap"> | 10 <ul class="small-gap"> |
| 11 <li><a class="reference internal" href="#building-and-running-the-demo" id="id4"
>Building and running the demo</a></li> | 11 <li><a class="reference internal" href="#building-and-running-the-demo" id="id4"
>Building and running the demo</a></li> |
| 12 <li><a class="reference internal" href="#a-look-at-the-code" id="id5">A look at
the code</a></li> | 12 <li><a class="reference internal" href="#a-look-at-the-code" id="id5">A look at
the code</a></li> |
| 13 </ul> | 13 </ul> |
| 14 </li> | 14 </li> |
| 15 <li><a class="reference internal" href="#reference-information" id="id6">Referen
ce information</a></li> | 15 <li><a class="reference internal" href="#reference-information" id="id6">Referen
ce information</a></li> |
| 16 </ul> | 16 </ul> |
| 17 | 17 |
| 18 </div><section id="introduction"> | 18 </div><h2 id="introduction">Introduction</h2> |
| 19 <h2 id="introduction">Introduction</h2> | |
| 20 <p><code>nacl_io</code> is a utility library that provides implementations of st
andard | 19 <p><code>nacl_io</code> is a utility library that provides implementations of st
andard |
| 21 C APIs such as POSIX I/O (<code>stdio.h</code>) and BSD sockets (<code>sys/socke
t.h</code>). | 20 C APIs such as POSIX I/O (<code>stdio.h</code>) and BSD sockets (<code>sys/socke
t.h</code>). |
| 22 Its primary function is to allow code that uses these standard APIs to be | 21 Its primary function is to allow code that uses these standard APIs to be |
| 23 compiled and used in a Native Client module. The library is included as part | 22 compiled and used in a Native Client module. The library is included as part |
| 24 of Native Client SDK and is implemented in on top of Pepper API.</p> | 23 of Native Client SDK and is implemented in on top of Pepper API.</p> |
| 25 <p>Since Native Client modules cannot access the host machine’s file syste
m | 24 <p>Since Native Client modules cannot access the host machine’s file syste
m |
| 26 directly, nacl_io provides several alternative filesystem types which | 25 directly, nacl_io provides several alternative filesystem types which |
| 27 can be used by the application. For example, the Chrome browser supports the | 26 can be used by the application. For example, the Chrome browser supports the |
| 28 <a class="reference external" href="http://www.html5rocks.com/en/tutorials/file/
filesystem/">HTML5 File System API</a> which provides | 27 <a class="reference external" href="http://www.html5rocks.com/en/tutorials/file/
filesystem/">HTML5 File System API</a> which provides |
| 29 access to a protected area of the local file system. This filesystem can | 28 access to a protected area of the local file system. This filesystem can |
| (...skipping 21 matching lines...) Expand all Loading... |
| 51 <td>An HTML5 local file system, which can be persistent or temporary</td> | 50 <td>An HTML5 local file system, which can be persistent or temporary</td> |
| 52 </tr> | 51 </tr> |
| 53 <tr class="row-even"><td>http</td> | 52 <tr class="row-even"><td>http</td> |
| 54 <td>Maps files on a remote webserver into the local filesystem.</td> | 53 <td>Maps files on a remote webserver into the local filesystem.</td> |
| 55 </tr> | 54 </tr> |
| 56 <tr class="row-odd"><td>dev</td> | 55 <tr class="row-odd"><td>dev</td> |
| 57 <td>A file system containing special files (e.g.: <code>/dev/null</code>)</td> | 56 <td>A file system containing special files (e.g.: <code>/dev/null</code>)</td> |
| 58 </tr> | 57 </tr> |
| 59 </tbody> | 58 </tbody> |
| 60 </table> | 59 </table> |
| 61 </section><section id="using-nacl-io"> | |
| 62 <h2 id="using-nacl-io">Using nacl_io</h2> | 60 <h2 id="using-nacl-io">Using nacl_io</h2> |
| 63 <p>Using nacl_io is mostly just a matter of using the standard POSIX C library | 61 <p>Using nacl_io is mostly just a matter of using the standard POSIX C library |
| 64 functions. However, there are some steps required to initialize the library | 62 functions. However, there are some steps required to initialize the library |
| 65 and setup the filesystem mounts. In general the following steps will be needed | 63 and setup the filesystem mounts. In general the following steps will be needed |
| 66 to use nacl_io in a NaCl application:</p> | 64 to use nacl_io in a NaCl application:</p> |
| 67 <ol class="arabic simple"> | 65 <ol class="arabic simple"> |
| 68 <li>Link the application with the nacl_io library (<code>-lnacl_io</code>)</li> | 66 <li>Link the application with the nacl_io library (<code>-lnacl_io</code>)</li> |
| 69 <li>Initialize nacl_io at startup using the <code>nacl_io_init_ppapi</code> or | 67 <li>Initialize nacl_io at startup using the <code>nacl_io_init_ppapi</code> or |
| 70 <code>nacl_io_init</code> functions.</li> | 68 <code>nacl_io_init</code> functions.</li> |
| 71 <li>Mount any desired filesystems using the <code>mount</code> function. The arg
uments | 69 <li>Mount any desired filesystems using the <code>mount</code> function. The arg
uments |
| 72 to <code>mount</code> for the different filesystem types are detailed in | 70 to <code>mount</code> for the different filesystem types are detailed in |
| 73 <code>include/nacl_io/nacl_io.h</code>.</li> | 71 <code>include/nacl_io/nacl_io.h</code>.</li> |
| 74 <li>If you are going to mount an HTML5 file system, be sure to allocate space | 72 <li>If you are going to mount an HTML5 file system, be sure to allocate space |
| 75 for it. You can either set the <code>unlimitedStorage</code> permission in the a
pp’s | 73 for it. You can either set the <code>unlimitedStorage</code> permission in the a
pp’s |
| 76 Web Store manifest file, or call the HTML5 QuotaManagement API. These | 74 Web Store manifest file, or call the HTML5 QuotaManagement API. These |
| 77 options are explained in the <a class="reference internal" href="/native-client/
devguide/coding/file-io.html#quota-management"><em>File IO documentation</em></a
>.</li> | 75 options are explained in the <a class="reference internal" href="/native-client/
devguide/coding/file-io.html#quota-management"><em>File IO documentation</em></a
>.</li> |
| 78 <li>Make sure that file and socket API calls are all made from the background | 76 <li>Make sure that file and socket API calls are all made from the background |
| 79 thread. This is because the main Pepper thread does not support the blocking | 77 thread. This is because the main Pepper thread does not support the blocking |
| 80 behavior needed by the POSIX I/O operations.</li> | 78 behavior needed by the POSIX I/O operations.</li> |
| 81 </ol> | 79 </ol> |
| 82 </section><section id="the-nacl-io-demo"> | |
| 83 <h2 id="the-nacl-io-demo">The nacl_io demo</h2> | 80 <h2 id="the-nacl-io-demo">The nacl_io demo</h2> |
| 84 <section id="building-and-running-the-demo"> | |
| 85 <h3 id="building-and-running-the-demo">Building and running the demo</h3> | 81 <h3 id="building-and-running-the-demo">Building and running the demo</h3> |
| 86 <p>The demo application launches a Native Client module that mounts three file | 82 <p>The demo application launches a Native Client module that mounts three file |
| 87 systems and displays a set of controls that let you work with them:</p> | 83 systems and displays a set of controls that let you work with them:</p> |
| 88 <img alt="/native-client/images/nacl_io1.png" src="/native-client/images/nacl_io
1.png" /> | 84 <img alt="/native-client/images/nacl_io1.png" src="/native-client/images/nacl_io
1.png" /> |
| 89 <p>Follow these steps to build and run the demo:</p> | 85 <p>Follow these steps to build and run the demo:</p> |
| 90 <ul class="small-gap"> | 86 <ul class="small-gap"> |
| 91 <li><p class="first">Open a terminal in the demo directory:</p> | 87 <li><p class="first">Open a terminal in the demo directory:</p> |
| 92 <pre class="prettyprint"> | 88 <pre class="prettyprint"> |
| 93 $ cd $NACL_SDK_ROOT/examples/demo/nacl_io_demo | 89 $ cd $NACL_SDK_ROOT/examples/demo/nacl_io_demo |
| 94 </pre> | 90 </pre> |
| (...skipping 14 matching lines...) Expand all Loading... |
| 109 menu that appears below on the left</li> | 105 menu that appears below on the left</li> |
| 110 <li>enter some data and press the fwrite button</li> | 106 <li>enter some data and press the fwrite button</li> |
| 111 <li>select the fclose command, be sure the file <code>/persistent/test</code> is
selected | 107 <li>select the fclose command, be sure the file <code>/persistent/test</code> is
selected |
| 112 in the menu, and press the fclose button</li> | 108 in the menu, and press the fclose button</li> |
| 113 <li>select the fopen command</li> | 109 <li>select the fopen command</li> |
| 114 <li>type in the filename <code>/persistent/test</code></li> | 110 <li>type in the filename <code>/persistent/test</code></li> |
| 115 <li>check the fread checkbox and press the fopen button</li> | 111 <li>check the fread checkbox and press the fopen button</li> |
| 116 <li>select the fread command, be sure the file /persistent/test is selected in | 112 <li>select the fread command, be sure the file /persistent/test is selected in |
| 117 the menu, enter a byte count, and press the fread button</li> | 113 the menu, enter a byte count, and press the fread button</li> |
| 118 </ol> | 114 </ol> |
| 119 </section><section id="a-look-at-the-code"> | |
| 120 <h3 id="a-look-at-the-code">A look at the code</h3> | 115 <h3 id="a-look-at-the-code">A look at the code</h3> |
| 121 <p>The demo is written C and comprises three files.</p> | 116 <p>The demo is written C and comprises three files.</p> |
| 122 <section id="nacl-io-demo-c"> | |
| 123 <h4 id="nacl-io-demo-c">nacl_io_demo.c</h4> | 117 <h4 id="nacl-io-demo-c">nacl_io_demo.c</h4> |
| 124 <p>This is the demo’s main file. The code here creates and initializes the
Native | 118 <p>This is the demo’s main file. The code here creates and initializes the
Native |
| 125 Client module instance. The Pepper function <code>Instance_DidCreate</code> init
ializes | 119 Client module instance. The Pepper function <code>Instance_DidCreate</code> init
ializes |
| 126 nacl_io and mounts an HTML5 filesystem at <code>/persistent</code>.</p> | 120 nacl_io and mounts an HTML5 filesystem at <code>/persistent</code>.</p> |
| 127 <pre class="prettyprint"> | 121 <pre class="prettyprint"> |
| 128 static PP_Bool Instance_DidCreate(PP_Instance instance, | 122 static PP_Bool Instance_DidCreate(PP_Instance instance, |
| 129 uint32_t argc, | 123 uint32_t argc, |
| 130 const char* argn[], | 124 const char* argn[], |
| 131 const char* argv[]) { | 125 const char* argv[]) { |
| 132 g_instance = instance; | 126 g_instance = instance; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 157 common.createNaClModule(name, tc, config, width, height); | 151 common.createNaClModule(name, tc, config, width, height); |
| 158 common.attachDefaultListeners(); | 152 common.attachDefaultListeners(); |
| 159 }, | 153 }, |
| 160 function(e) { alert('Failed to allocate space') }); | 154 function(e) { alert('Failed to allocate space') }); |
| 161 } | 155 } |
| 162 </pre> | 156 </pre> |
| 163 <p>The <code>Instance_DidCreate</code> function also creates a worker thread tha
t receives | 157 <p>The <code>Instance_DidCreate</code> function also creates a worker thread tha
t receives |
| 164 messages sent from the html page and performs the specified file system | 158 messages sent from the html page and performs the specified file system |
| 165 operations. The logic for the worker thread is encoded in the other two files, | 159 operations. The logic for the worker thread is encoded in the other two files, |
| 166 described below.</p> | 160 described below.</p> |
| 167 </section><section id="queue-c"> | |
| 168 <h4 id="queue-c">queue.c</h4> | 161 <h4 id="queue-c">queue.c</h4> |
| 169 <p>This file implements a circular queue that is used to receive messages from t
he | 162 <p>This file implements a circular queue that is used to receive messages from t
he |
| 170 browser UI to the Native Client module. The file system commands in the | 163 browser UI to the Native Client module. The file system commands in the |
| 171 enqueued messages are executed on the worker thread. This keeps blocking calls | 164 enqueued messages are executed on the worker thread. This keeps blocking calls |
| 172 (like fread) off the main Native Client thread, which is a good thing. The | 165 (like fread) off the main Native Client thread, which is a good thing. The |
| 173 queue is initialized in nacl_io_demo.c <code>Instance_DidCreate</code>.</p> | 166 queue is initialized in nacl_io_demo.c <code>Instance_DidCreate</code>.</p> |
| 174 </section><section id="handlers-c"> | |
| 175 <h4 id="handlers-c">handlers.c</h4> | 167 <h4 id="handlers-c">handlers.c</h4> |
| 176 <p>This file implements the stdio calls associated with the commands sent from t
he | 168 <p>This file implements the stdio calls associated with the commands sent from t
he |
| 177 browser. There is a separate <code>Handle*</code> function for each command: fop
en, | 169 browser. There is a separate <code>Handle*</code> function for each command: fop
en, |
| 178 fclose, fseek, fread, fwrite. The handlers are called from the | 170 fclose, fseek, fread, fwrite. The handlers are called from the |
| 179 <code>HandleMessage</code> function in nacl_io_demo.c, which runs in the worker | 171 <code>HandleMessage</code> function in nacl_io_demo.c, which runs in the worker |
| 180 thread managing the message queue. The code for the <code>fwrite</code> handler
appears | 172 thread managing the message queue. The code for the <code>fwrite</code> handler
appears |
| 181 below. Notice that it does not contain any PPAPI calls and looks like | 173 below. Notice that it does not contain any PPAPI calls and looks like |
| 182 “ordinary” C code.</p> | 174 “ordinary” C code.</p> |
| 183 <pre class="prettyprint"> | 175 <pre class="prettyprint"> |
| 184 int HandleFwrite(int num_params, char** params, char** output) { | 176 int HandleFwrite(int num_params, char** params, char** output) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 204 return 2; | 196 return 2; |
| 205 } | 197 } |
| 206 | 198 |
| 207 bytes_written = fwrite(data, 1, data_len, file); | 199 bytes_written = fwrite(data, 1, data_len, file); |
| 208 | 200 |
| 209 *output = PrintfToNewString("fwrite\1%s\1%d", file_index_string, | 201 *output = PrintfToNewString("fwrite\1%s\1%d", file_index_string, |
| 210 bytes_written); | 202 bytes_written); |
| 211 return 0; | 203 return 0; |
| 212 } | 204 } |
| 213 </pre> | 205 </pre> |
| 214 </section></section></section><section id="reference-information"> | |
| 215 <h2 id="reference-information">Reference information</h2> | 206 <h2 id="reference-information">Reference information</h2> |
| 216 <p>The example discussed here is included in the SDK in the directory | 207 <p>The example discussed here is included in the SDK in the directory |
| 217 <code>examples/demo/nacl_io_demo</code>.</p> | 208 <code>examples/demo/nacl_io_demo</code>.</p> |
| 218 <p>The nacl_io library is included in the SDK toolchain and is not a part of the | 209 <p>The nacl_io library is included in the SDK toolchain and is not a part of the |
| 219 Pepper API. For reference information related to the nacl_io interface see | 210 Pepper API. For reference information related to the nacl_io interface see |
| 220 its header file in the SDK directory, located at | 211 its header file in the SDK directory, located at |
| 221 <code>include/nacl_io/nacl_io.h</code>.</p> | 212 <code>include/nacl_io/nacl_io.h</code>.</p> |
| 222 <p>For more about the HTML5 file system read the <a class="reference external" h
ref="http://dev.w3.org/2009/dap/file-system/pub/FileSystem/">specification</a>.<
/p> | 213 <p>For more about the HTML5 file system read the <a class="reference external" h
ref="http://dev.w3.org/2009/dap/file-system/pub/FileSystem/">specification</a>.<
/p> |
| 223 </section></section> | 214 </section> |
| 224 | 215 |
| 225 {{/partials.standard_nacl_article}} | 216 {{/partials.standard_nacl_article}} |
| OLD | NEW |