aboutsummaryrefslogtreecommitdiff
path: root/docs/fs.md
blob: 8c5f8babb0f05c11c9a6eea90d9fb6c3cc97ceeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
ZJS API for File System
==================

* [Introduction](#introduction)
* [API Documentation](#api-documentation)
* [Sample Apps](#sample-apps)

Introduction
------------
ZJS provides File System API's which match Node.js' FS module. We describe them
here as there could potentially be minor differences. It should be noted that by
default the FS module only contains the synchronous Node.js API's. The
asynchronous API's can be compiled in by enabling the pre-processor define
`ZJS_FS_ASYNC_APIS`. They are compiled out because all of Zephyr's File
System API's are synchronous, so making the JavaScript API's asynchronous was
only adding ROM space.

On the Arduino 101 the flash file system uses SPI and the pins are shared with
IO10-13. For this reason you will not be able to use these GPIO pins at the
same time as the file system.

Available file modes:

`'r'` - Open file for only reading. An error will be thrown if the file does
not exist.

`'r+'` - Open a file for reading and writing. An error will be thrown if the
file does not exist.

`'w'` - Opens a file for writing. The file will be overwritten if it already
exists.

`'w+'` - Opens a file for writing and reading. The file will be overwritten if
it already exists.

`'a'` - Opens a file for appending. The write pointer will always seek
to the end of the file during a write.

`'a+'` - Opens a file for appending and reading. As with `'a'` the write
pointer will seek to the end for writes, but reads can be done from the
start of the file (read pointer saved across different read calls).

Web IDL
-------
This IDL provides an overview of the interface; see below for documentation of
specific API functions.

```javascript
// require returns a FS object
// var fs = require('fs');

interface Stat {
    boolean isFile();
    boolean isDirectory();
};
```

API Documentation
-----------------

### FS.openSync
`object openSync(string path, string mode);`

Opens a file.

`path` is the name/path of the file to open.

`mode` is the mode to open the file in (r/w/a/r+/w+/a+).

Returns an object representing the file descriptor.

### FS.closeSync
`void closeSync(object fd);`

Closes a file.

`fd` is the descriptor returned from `openSync()`.

### FS.unlinkSync
`void unlinkSync(string path);`

Unlink (remove) a file from the file system.

`path` is the file to remove.

### FS.rmdirSync
`void rmdirSync(string path);`

Remove a directory from the file system.

`path` is the name of the directory.

### FS.writeSync
`number writeSync(object fd, Buffer data, number offset, number length, optional number position);`

Write bytes to an opened file.

`fd` is the file descriptor returned from `openSync()`.

`data` is either a string or buffer to write.

`offset` is the position in `data` to start writing from.

`length` is the number of bytes to write from `data`.

`position` is the offset from the beginning of the file where `data` should be
written. Default is 0.

Returns the number of bytes actually written (this may be different from `length`).

### FS.readSync
`number readSync(object fd, buffer data, number offset, number length, number position);`

Read bytes from a file.

`fd` is the file descriptor returned from `openSync()`.

`data` is a buffer where the data will be read into.

`offset` is the offset in `data` to start writing at.

`length` is the number of bytes to read.

`position` is the position in the file to start reading from.

Returns the number of bytes actually read. This may be different from `length`
if there was a read error or if the file had no more data left to read.

### FS.truncateSync
`void truncateSync(string path, number length);`

Truncate a file. If the length passed in is shorter than the existing file
length then the trailing file data will be lost.

`path` is the name of the file.

`length` is the new length of the file.

### FS.mkdirSync
`void mkdirSync(string path)`

Create a directory. If the directory already exists there is no effect.

`path` is the name of the directory.

### FS.readdirSync
`string[] readdirSync(string path);`

Read the contents of a directory.

`path` directory path to read.

Returns an array of filenames and directories found in `path`.

### FS.statSync
`Stat statSync(string path);`

Get stats about a file or directory.

`path` name of file or directory.

Returns a `Stat` object for that file or directory.

### FS.writeFileSync
`void writeFileSync(string file, [string|buffer] data);`

Open and write data to a file. This will replace the file if it already exists.

`file` is the name of the file to write to.

`data` is the bytes to write into the file.

Sample Apps
-----------
* [FS test](../tests/test-fs.js)