Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit 6b7abbd

Browse files
James Prestwoodgrgustaf
authored andcommitted
[fs] File System module (#510)
Signed-off-by: James Prestwood <[email protected]>
1 parent 70a30cc commit 6b7abbd

File tree

10 files changed

+1582
-0
lines changed

10 files changed

+1582
-0
lines changed

docs/API.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ General
4545

4646
[Events](./events.md)
4747

48+
[File System](./fs.md)
49+
4850
[Performance](./performance.md)
4951

5052
[Timers](./timers.md)

docs/fs.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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)

samples/FsAsync.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var fs = require('fs');
2+
3+
console.log('File System sample');
4+
5+
fs.open('testfile.txt', 'w+', function(err, fd) {
6+
console.log('opened file error=' + err);
7+
fs.stat('testfile.txt', function(err, stats) {
8+
if (stats.isFile()) {
9+
console.log('fd is file');
10+
} else if (stats.isDirectory()) {
11+
console.log('fd is directory');
12+
} else {
13+
console.log('fd has unknown type');
14+
}
15+
});
16+
var wbuf = new Buffer('write some bytes');
17+
fs.write(fd, wbuf, 0, wbuf.length, 0, function(err, written, buffer) {
18+
console.log('wrote ' + written + ' bytes');
19+
var rbuf = new Buffer(16);
20+
fs.read(fd, rbuf, 0, 16, 0, function(err, num, buf) {
21+
console.log('read from file; error=' + err);
22+
console.log('buffer=' + buf.toString('ascii'));
23+
});
24+
});
25+
});
26+
27+
fs.open('file.txt', 'w', function(err, fd) {
28+
console.log('opened file error=' + err);
29+
});
30+
31+
fs.writeFile('string.txt', 'string data', {}, function(err) {
32+
console.log('wrote string data file error=' + err);
33+
});
34+
fs.writeFile('buffer.txt', new Buffer('buffer data'), function(err) {
35+
console.log('wrote buffer data file error=' + err);
36+
});
37+
38+
setTimeout(function() {
39+
fs.readdir('/', function(err, files) {
40+
for (var i = 0; i < files.length; ++i) {
41+
console.log('files[' + i + ']=' + files[i]);
42+
}
43+
});
44+
}, 1000);

samples/FsSync.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var fs = require('fs');
2+
3+
console.log('File System sample');
4+
5+
var fd = fs.openSync('testfile.txt', 'w+');
6+
7+
var stats = fs.statSync('testfile.txt');
8+
9+
if (stats.isFile()) {
10+
console.log('fd is file');
11+
} else if (stats.isDirectory()) {
12+
console.log('fd is directory');
13+
} else {
14+
console.log('fd has unknown type');
15+
}
16+
17+
var wbuf = new Buffer('write some bytes');
18+
19+
var written = fs.writeSync(fd, wbuf, 0, wbuf.length, 0);
20+
21+
console.log('wrote ' + written + ' bytes');
22+
23+
24+
var rbuf = new Buffer(16);
25+
var ret = fs.readSync(fd, rbuf, 0, 16, 0);
26+
console.log('read ' + ret + ' bytes from file');
27+
console.log('buffer=' + rbuf.toString('ascii'));
28+
29+
var fd1 = fs.openSync('file.txt', 'w');
30+
31+
fs.writeFileSync('string.txt', 'string data', {});
32+
fs.writeFileSync('buffer.txt', new Buffer('buffer data'));
33+
34+
var files = fs.readdirSync('/');
35+
for (var i = 0; i < files.length; ++i) {
36+
console.log('files[' + i + ']=' + files[i]);
37+
}

scripts/analyze.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,23 @@ if check_for_require aio || check_config_file ZJS_AIO; then
236236
echo "export ZJS_AIO=y" >> zjs.conf.tmp
237237
fi
238238

239+
if check_for_require fs || check_config_file ZJS_FS; then
240+
>&2 echo Using module: FS
241+
MODULES+=" -DBUILD_MODULE_FS -DBUILD_MODULE_BUFFER"
242+
if [ $BOARD = "arduino_101" ]; then
243+
echo "CONFIG_FS_FAT_FLASH_DISK_W25QXXDV=y" >> prj.conf.tmp
244+
fi
245+
echo "CONFIG_FILE_SYSTEM=y" >> prj.conf.tmp
246+
echo "CONFIG_FILE_SYSTEM_FAT=y" >> prj.conf.tmp
247+
echo "CONFIG_DISK_ACCESS_FLASH=y" >> prj.conf.tmp
248+
249+
echo "CONFIG_FLASH=y" >> prj.conf.tmp
250+
echo "CONFIG_SPI=y" >> prj.conf.tmp
251+
echo "CONFIG_GPIO=y" >> prj.conf.tmp
252+
echo "export ZJS_FS=y" >> zjs.conf.tmp
253+
echo "export ZJS_BUFFER=y" >> zjs.conf.tmp
254+
fi
255+
239256
if check_for_require i2c || check_config_file ZJS_I2C; then
240257
>&2 echo Using module: I2C
241258
MODULES+=" -DBUILD_MODULE_I2C"

src/Makefile.base

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ obj-$(ZJS_BUFFER) += zjs_buffer.o
4545
obj-$(ZJS_CONSOLE) += zjs_console.o
4646
obj-$(ZJS_DGRAM) += zjs_dgram.o
4747
obj-$(ZJS_EVENTS) += zjs_event.o
48+
obj-$(ZJS_FS) += zjs_fs.o
4849
obj-$(ZJS_GPIO) += zjs_gpio.o
4950
obj-$(ZJS_BLE) += zjs_ble.o
5051
obj-$(ZJS_PWM) += zjs_pwm.o

0 commit comments

Comments
 (0)