|
| 1 | +ZJS API for File System |
| 2 | +================== |
| 3 | + |
| 4 | +* [Introduction](#introduction) |
| 5 | +* [API Documentation](#api-documentation) |
| 6 | +* [Sample Apps](#sample-apps) |
| 7 | + |
| 8 | +Introduction |
| 9 | +------------ |
| 10 | +ZJS provides File System API's which match Node.js' FS module. We describe them |
| 11 | +here as there could potentially be minor differences. It should be noted that by |
| 12 | +default the FS module only contains the synchronous Node.js API's. The |
| 13 | +asynchronous API's can be compiled in by enabling the pre-processor define |
| 14 | +`ZJS_FS_ASYNC_APIS`. They are compiled out because all of Zephyr's File |
| 15 | +System API's are synchronous, so making the JavaScript API's asynchronous was |
| 16 | +only adding ROM space. |
| 17 | + |
| 18 | +On the Arduino 101 the flash file system uses SPI and the pins are shared with |
| 19 | +IO10-13. For this reason you will not be able to use these GPIO pins at the |
| 20 | +same time as the file system. |
| 21 | + |
| 22 | +Available file modes: |
| 23 | + |
| 24 | +`'r'` - Open file for only reading. An error will be thrown if the file does |
| 25 | +not exist. |
| 26 | + |
| 27 | +`'r+'` - Open a file for reading and writing. An error will be thrown if the |
| 28 | +file does not exist. |
| 29 | + |
| 30 | +`'w'` - Opens a file for writing. The file will be overwritten if it already |
| 31 | +exists. |
| 32 | + |
| 33 | +`'w+'` - Opens a file for writing and reading. The file will be overwritten if |
| 34 | +it already exists. |
| 35 | + |
| 36 | +`'a'` - Opens a file for appending. The write pointer will always seek |
| 37 | +to the end of the file during a write. |
| 38 | + |
| 39 | +`'a+'` - Opens a file for appending and reading. As with `'a'` the write |
| 40 | +pointer will seek to the end for writes, but reads can be done from the |
| 41 | +start of the file (read pointer saved across different read calls). |
| 42 | + |
| 43 | +Web IDL |
| 44 | +------- |
| 45 | +This IDL provides an overview of the interface; see below for documentation of |
| 46 | +specific API functions. |
| 47 | + |
| 48 | +```javascript |
| 49 | +// require returns a FS object |
| 50 | +// var fs = require('fs'); |
| 51 | + |
| 52 | +interface Stat { |
| 53 | + boolean isFile(); |
| 54 | + boolean isDirectory(); |
| 55 | +}; |
| 56 | +``` |
| 57 | + |
| 58 | +API Documentation |
| 59 | +----------------- |
| 60 | + |
| 61 | +### FS.openSync |
| 62 | +`object openSync(string path, string mode);` |
| 63 | + |
| 64 | +Opens a file. |
| 65 | + |
| 66 | +`path` is the name/path of the file to open. |
| 67 | + |
| 68 | +`mode` is the mode to open the file in (r/w/a/r+/w+/a+). |
| 69 | + |
| 70 | +Returns an object representing the file descriptor. |
| 71 | + |
| 72 | +### FS.closeSync |
| 73 | +`void closeSync(object fd);` |
| 74 | + |
| 75 | +Closes a file. |
| 76 | + |
| 77 | +`fd` is the descriptor returned from `openSync()`. |
| 78 | + |
| 79 | +### FS.unlinkSync |
| 80 | +`void unlinkSync(string path);` |
| 81 | + |
| 82 | +Unlink (remove) a file from the file system. |
| 83 | + |
| 84 | +`path` is the file to remove. |
| 85 | + |
| 86 | +### FS.rmdirSync |
| 87 | +`void rmdirSync(string path);` |
| 88 | + |
| 89 | +Remove a directory from the file system. |
| 90 | + |
| 91 | +`path` is the name of the directory. |
| 92 | + |
| 93 | +### FS.writeSync |
| 94 | +`number writeSync(object fd, [String|Buffer] data, number offset, number length, optional number position);` |
| 95 | + |
| 96 | +Write bytes to an opened file. |
| 97 | + |
| 98 | +`fd` is the file descriptor returned from `openSync()`. |
| 99 | + |
| 100 | +`data` is either a string or buffer to write. |
| 101 | + |
| 102 | +`offset` is the position in `data` to start writing from. |
| 103 | + |
| 104 | +`length` is the number of bytes to write from `data`. |
| 105 | + |
| 106 | +`position` is the offset from the beginning of the file where `data` should be |
| 107 | +written. Default is 0. |
| 108 | + |
| 109 | +Returns the number of bytes actually written (this may be different from `length`). |
| 110 | + |
| 111 | +### FS.readSync |
| 112 | +`number readSync(object fd, buffer data, number offset, number length, number position);` |
| 113 | + |
| 114 | +Read bytes from a file. |
| 115 | + |
| 116 | +`fd` is the file descriptor returned from `openSync()`. |
| 117 | + |
| 118 | +`data` is a buffer where the data will be read into. |
| 119 | + |
| 120 | +`offset` is the offset in `data` to start writing at. |
| 121 | + |
| 122 | +`length` is the number of bytes to read. |
| 123 | + |
| 124 | +`position` is the position in the file to start reading from. |
| 125 | + |
| 126 | +Returns the number of bytes actually read. This may be different from `length` |
| 127 | +if there was a read error or if the file had no more data left to read. |
| 128 | + |
| 129 | +### FS.truncateSync |
| 130 | +`void truncateSync(string path, number length);` |
| 131 | + |
| 132 | +Truncate a file. If the length passed in is shorter than the existing file |
| 133 | +length then the trailing file data will be lost. |
| 134 | + |
| 135 | +`path` is the name of the file. |
| 136 | + |
| 137 | +`length` is the new length of the file. |
| 138 | + |
| 139 | +### FS.mkdirSync |
| 140 | +`void mkdirSync(string path)` |
| 141 | + |
| 142 | +Create a directory. If the directory already exists there is no effect. |
| 143 | + |
| 144 | +`path` is the name of the directory. |
| 145 | + |
| 146 | +### FS.readdirSync |
| 147 | +`string[] readdirSync(string path);` |
| 148 | + |
| 149 | +Read the contents of a directory. |
| 150 | + |
| 151 | +`path` directory path to read. |
| 152 | + |
| 153 | +Returns an array of filenames and directories found in `path`. |
| 154 | + |
| 155 | +### FS.statSync |
| 156 | +`Stat statSync(string path);` |
| 157 | + |
| 158 | +Get stats about a file or directory. |
| 159 | + |
| 160 | +`path` name of file or directory. |
| 161 | + |
| 162 | +Returns a `Stat` object for that file or directory. |
| 163 | + |
| 164 | +### FS.writeFileSync |
| 165 | +`void writeFileSync(string file, [string|buffer] data);` |
| 166 | + |
| 167 | +Open and write data to a file. This will replace the file if it already exists. |
| 168 | + |
| 169 | +`file` is the name of the file to write to. |
| 170 | + |
| 171 | +`data` is the bytes to write into the file. |
| 172 | + |
| 173 | +Sample Apps |
| 174 | +----------- |
| 175 | +* [FS test](../tests/test-fs.js) |
0 commit comments