/
jswrap_storage.c
998 lines (905 loc) · 33.8 KB
/
jswrap_storage.c
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2018 Gordon Williams <gw@pur3.co.uk>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* This file is designed to be parsed during the build process
*
* JavaScript Filesystem-style Flash IO functions
* ----------------------------------------------------------------------------
*/
#include "jswrap_storage.h"
#include "jswrap_flash.h"
#include "jshardware.h"
#include "jsflash.h"
#include "jsvar.h"
#include "jsvariterator.h"
#include "jsparse.h"
#include "jsinteractive.h"
#include "jswrap_json.h"
#ifdef DEBUG
#define DBG(...) jsiConsolePrintf("[Storage] "__VA_ARGS__)
#else
#define DBG(...)
#endif
const int STORAGEFILE_CHUNKSIZE =
#if FLASH_SAVED_CODE_LENGTH < 1000000
// for normal devices
(((FLASH_PAGE_SIZE<4096)?FLASH_PAGE_SIZE:4096) - sizeof(JsfFileHeader)); // use 32 for testing
#else
// where we have large external flash (eg Bangle.js), choose a chunk size much bigger than the
// page size. This allows us to have less chunks around, and to store much bigger files.
(FLASH_PAGE_SIZE*10) - sizeof(JsfFileHeader);
#endif
/*JSON{
"type" : "library",
"class" : "Storage"
}
This module allows you to read and write part of the nonvolatile flash memory of
your device using a filesystem-like API.
Also see the `Flash` library, which provides a low level, more dangerous way to
access all parts of your flash memory.
The `Storage` library provides two distinct types of file:
* `require("Storage").write(...)`/`require("Storage").read(...)`/etc create
simple contiguous files of fixed length. This is the recommended file type.
* `require("Storage").open(...)` creates a `StorageFile`, which stores the file
in numbered chunks (`"filename\1"`/`"filename\2"`/etc). It allows data to be
appended and for the file to be read line by line.
You must read a file using the same method you used to write it - e.g. you can't
create a file with `require("Storage").open(...)` and then read it with
`require("Storage").read(...)`.
**Note:** In firmware 2v05 and later, the maximum length for filenames is 28
characters. However in 2v04 and earlier the max length is 8.
*/
/*JSON{
"type" : "staticmethod",
"class" : "Storage",
"name" : "eraseAll",
"generate" : "jswrap_storage_eraseAll"
}
Erase the flash storage area. This will remove all files created with
`require("Storage").write(...)` as well as any code saved with `save()` or
`E.setBootCode()`.
*/
void jswrap_storage_eraseAll() {
jsfEraseAll();
}
/*JSON{
"type" : "staticmethod",
"class" : "Storage",
"name" : "erase",
"generate" : "jswrap_storage_erase",
"params" : [
["name","JsVar","The filename - max 28 characters (case sensitive)"]
],
"typescript" : "erase(name: string): void;"
}
Erase a single file from the flash storage area.
**Note:** This function should be used with normal files, and not `StorageFile`s
created with `require("Storage").open(filename, ...)`. To erase those, use
`require("Storage").open(..., "r").erase()`.
*/
void jswrap_storage_erase(JsVar *name) {
jsfEraseFile(jsfNameFromVar(name));
}
/*JSON{
"type" : "staticmethod",
"class" : "Storage",
"name" : "read",
"generate" : "jswrap_storage_read",
"params" : [
["name","JsVar","The filename - max 28 characters (case sensitive)"],
["offset","int","[optional] The offset in bytes to start from"],
["length","int","[optional] The length to read in bytes (if <=0, the entire file is read)"]
],
"return" : ["JsVar","A string of data, or `undefined` if the file is not found"],
"typescript" : "read(name: string, offset?: number, length?: number): string | undefined;"
}
Read a file from the flash storage area that has been written with
`require("Storage").write(...)`.
This function returns a memory-mapped String that points to the actual memory
area in read-only memory, so it won't use up RAM.
As such you can check if a file exists efficiently using
`require("Storage").read(filename)!==undefined`.
If you evaluate this string with `eval`, any functions contained in the String
will keep their code stored in flash memory.
**Note:** This function should be used with normal files, and not `StorageFile`s
created with `require("Storage").open(filename, ...)`
*/
JsVar *jswrap_storage_read(JsVar *name, int offset, int length) {
return jsfReadFile(jsfNameFromVar(name), offset, length);
}
/*JSON{
"type" : "staticmethod",
"ifndef" : "SAVE_ON_FLASH",
"class" : "Storage",
"name" : "readJSON",
"generate" : "jswrap_storage_readJSON",
"params" : [
["name","JsVar","The filename - max 28 characters (case sensitive)"],
["noExceptions","bool","If true and the JSON is not valid, just return `undefined` - otherwise an `Exception` is thrown"]
],
"return" : ["JsVar","An object containing parsed JSON from the file, or undefined"],
"typescript" : [
"function readJSON(name: string, noExceptions?: false | 0): unknown;",
"function readJSON(name: string, noExceptions?: ShortBoolean): unknown | undefined;"
]
}
Read a file from the flash storage area that has been written with
`require("Storage").write(...)`, and parse JSON in it into a JavaScript object.
This is identical to `JSON.parse(require("Storage").read(...))`. It will throw
an exception if the data in the file is not valid JSON.
**Note:** This function should be used with normal files, and not `StorageFile`s
created with `require("Storage").open(filename, ...)`
*/
JsVar *jswrap_storage_readJSON(JsVar *name, bool noExceptions) {
JsVar *v = jsfReadFile(jsfNameFromVar(name),0,0);
if (!v) return 0;
JsVar *r = jswrap_json_parse_ext(v, JSON_DROP_QUOTES);
jsvUnLock(v);
if (noExceptions) {
jsvUnLock(jspGetException());
execInfo.execute &= (JsExecFlags)~EXEC_EXCEPTION;
}
return r;
}
/*JSON{
"type" : "staticmethod",
"ifndef" : "SAVE_ON_FLASH",
"class" : "Storage",
"name" : "readArrayBuffer",
"generate" : "jswrap_storage_readArrayBuffer",
"params" : [
["name","JsVar","The filename - max 28 characters (case sensitive)"]
],
"return" : ["JsVar","An ArrayBuffer containing data from the file, or undefined"],
"typescript" : "readArrayBuffer(name: string): ArrayBuffer | undefined;"
}
Read a file from the flash storage area that has been written with
`require("Storage").write(...)`, and return the raw binary data as an
ArrayBuffer.
This can be used:
* In a `DataView` with `new DataView(require("Storage").readArrayBuffer("x"))`
* In a `Uint8Array/Float32Array/etc` with `new
Uint8Array(require("Storage").readArrayBuffer("x"))`
**Note:** This function should be used with normal files, and not `StorageFile`s
created with `require("Storage").open(filename, ...)`
*/
JsVar *jswrap_storage_readArrayBuffer(JsVar *name) {
JsVar *v = jsfReadFile(jsfNameFromVar(name),0,0);
if (!v) return 0;
JsVar *r = jsvNewArrayBufferFromString(v, 0);
jsvUnLock(v);
return r;
}
/*JSON{
"type" : "staticmethod",
"class" : "Storage",
"name" : "write",
"generate" : "jswrap_storage_write",
"params" : [
["name","JsVar","The filename - max 28 characters (case sensitive)"],
["data","JsVar","The data to write"],
["offset","int","[optional] The offset within the file to write (if `0`/`undefined` a new file is created, otherwise Espruino attempts to write within an existing file if one exists)"],
["size","int","[optional] The size of the file (if a file is to be created that is bigger than the data)"]
],
"return" : ["bool","True on success, false on failure"],
"typescript" : "write(name: string | ArrayBuffer | ArrayBufferView | number[] | object, data: any, offset?: number, size?: number): boolean;"
}
Write/create a file in the flash storage area. This is nonvolatile and will not
disappear when the device resets or power is lost.
Simply write `require("Storage").write("MyFile", "Some data")` to write a new
file, and `require("Storage").read("MyFile")` to read it.
If you supply:
* A String, it will be written as-is
* An array, will be written as a byte array (but read back as a String)
* An object, it will automatically be converted to a JSON string before being
written.
**Note:** If an array is supplied it will not be converted to JSON. To be
explicit about the conversion you can use `Storage.writeJSON`
You may also create a file and then populate data later **as long as you don't
try and overwrite data that already exists**. For instance:
```
var f = require("Storage");
f.write("a","Hello",0,14); // Creates a new file, 14 chars long
print(JSON.stringify(f.read("a"))); // read the file
// any nonwritten chars will be char code 255:
"Hello\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF\u00FF"
f.write("a"," ",5); // write within the file
f.write("a","World!!!",6); // write again within the file
print(f.read("a")); // "Hello World!!!"
f.write("a"," ",0); // Writing to location 0 again will cause the file to be re-written
print(f.read("a")); // " "
```
This can be useful if you've got more data to write than you have RAM
available - for instance the Web IDE uses this method to write large files into
onboard storage.
**Note:** This function should be used with normal files, and not `StorageFile`s
created with `require("Storage").open(filename, ...)`
*/
bool jswrap_storage_write(JsVar *name, JsVar *data, JsVarInt offset, JsVarInt _size) {
JsVar *d;
if (jsvIsObject(data)) {
d = jswrap_json_stringify(data,0,0);
offset = 0;
_size = 0;
} else
d = jsvLockAgainSafe(data);
bool success = jsfWriteFile(jsfNameFromVar(name), d, JSFF_NONE, offset, _size);
jsvUnLock(d);
return success;
}
/*JSON{
"type" : "staticmethod",
"ifndef" : "SAVE_ON_FLASH",
"class" : "Storage",
"name" : "writeJSON",
"generate" : "jswrap_storage_writeJSON",
"params" : [
["name","JsVar","The filename - max 28 characters (case sensitive)"],
["data","JsVar","The JSON data to write"]
],
"return" : ["bool","True on success, false on failure"],
"typescript" : "writeJSON(name: string, data: any): boolean;"
}
Write/create a file in the flash storage area. This is nonvolatile and will not
disappear when the device resets or power is lost.
Simply write `require("Storage").writeJSON("MyFile", [1,2,3])` to write a new
file, and `require("Storage").readJSON("MyFile")` to read it.
This is (almost) equivalent to `require("Storage").write(name, JSON.stringify(data))` (see the notes below)
**Note:** This function should be used with normal files, and not `StorageFile`s
created with `require("Storage").open(filename, ...)`
**Note:** Normally `JSON.stringify` converts any non-standard character to an escape code with `\uXXXX`, but
as of Espruino 2v20, when writing to a file we use the most compact form, like `\xXX` or `\X`, as well as
skipping quotes on fields. This saves space and is faster, but also means that if a String wasn't a UTF8
string but contained characters in the UTF8 codepoint range, when saved it won't end up getting reloaded as a UTF8 string.
It does mean that you cannot parse the file with just `JSON.parse` as it's no longer standard JSON but is JS,
so you must use `Storage.readJSON`
*/
bool jswrap_storage_writeJSON(JsVar *name, JsVar *data) {
JsVar *d = jsvNewFromEmptyString();
if (!d) return false;
/* Don't call jswrap_json_stringify directly because we want to ensure we don't use JSON_JSON_COMPATIBILE, so
String escapes like `\xFC` stay as `\xFC` and not `\u00FC` to save space and help with unicode compatibility
*/
jsfGetJSON(data, d, (JSON_DROP_QUOTES|JSON_IGNORE_FUNCTIONS|JSON_NO_UNDEFINED|JSON_ARRAYBUFFER_AS_ARRAY|JSON_JSON_COMPATIBILE|JSON_ALLOW_TOJSON) &~JSON_ALL_UNICODE_ESCAPE);
bool r = jsfWriteFile(jsfNameFromVar(name), d, JSFF_NONE, 0, 0);
jsvUnLock(d);
return r;
}
/*JSON{
"type" : "staticmethod",
"class" : "Storage",
"name" : "list",
"generate" : "jswrap_storage_list",
"params" : [
["regex","JsVar","[optional] If supplied, filenames are checked against this regular expression (with `String.match(regexp)`) to see if they match before being returned"],
["filter","JsVar","[optional] If supplied, File Types are filtered based on this: `{sf:true}` or `{sf:false}` for whether to show StorageFile"]
],
"return" : ["JsVar","An array of filenames"],
"typescript" : "list(regex?: RegExp, filter?: { sf: boolean }): string[];"
}
List all files in the flash storage area. An array of Strings is returned.
By default this lists files created by `StorageFile` (`require("Storage").open`)
which have a file number (`"\1"`/`"\2"`/etc) appended to them.
```
// All files
require("Storage").list()
// Files ending in '.js'
require("Storage").list(/\.js$/)
// All Storage Files
require("Storage").list(undefined, {sf:true})
// All normal files (e.g. created with Storage.write)
require("Storage").list(undefined, {sf:false})
```
**Note:** This will output system files (e.g. saved code) as well as files that
you may have written.
*/
JsVar *jswrap_storage_list(JsVar *regex, JsVar *filter) {
JsfFileFlags containing = 0;
JsfFileFlags notContaining = 0;
if (jsvIsObject(filter)) {
JsVar *v = jsvObjectGetChildIfExists(filter, "sf");
if (v) {
if (jsvGetBoolAndUnLock(v))
containing |= JSFF_STORAGEFILE;
else
notContaining |= JSFF_STORAGEFILE;
}
}
return jsfListFiles(regex, containing, notContaining);
}
/*JSON{
"type" : "staticmethod",
"ifndef" : "SAVE_ON_FLASH",
"class" : "Storage",
"name" : "hash",
"generate" : "jswrap_storage_hash",
"params" : [
["regex","JsVar","[optional] If supplied, filenames are checked against this regular expression (with `String.match(regexp)`) to see if they match before being hashed"]
],
"return" : ["int","A hash of the files matching"],
"typescript" : "hash(regex: RegExp): number;"
}
List all files in the flash storage area matching the specified regex (ignores
StorageFiles), and then hash their filenames *and* file locations.
Identical files may have different hashes (e.g. if Storage is compacted and the
file moves) but the chances of different files having the same hash are
extremely small.
```
// Hash files
require("Storage").hash()
// Files ending in '.boot.js'
require("Storage").hash(/\.boot\.js$/)
```
**Note:** This function is used by Bangle.js as a way to cache files. For
instance the bootloader will add all `.boot.js` files together into a single
`.boot0` file, but it needs to know quickly whether anything has changed.
*/
JsVarInt jswrap_storage_hash(JsVar *regex) {
return (JsVarInt)jsfHashFiles(regex, 0, JSFF_STORAGEFILE);
}
/*JSON{
"type" : "staticmethod",
"ifndef" : "SAVE_ON_FLASH",
"class" : "Storage",
"name" : "compact",
"params" : [
["showMessage","bool","[optional] If true, an overlay message will be displayed on the screen while compaction is happening. Default is false."]
],
"generate" : "jswrap_storage_compact"
}
The Flash Storage system is journaling. To make the most of the limited write
cycles of Flash memory, Espruino marks deleted/replaced files as garbage/trash files and
moves on to a fresh part of flash memory. Espruino only fully erases those files
when it is running low on flash, or when `compact` is called.
`compact` may fail if there isn't enough RAM free on the stack to use as swap
space, however in this case it will not lose data.
**Note:** `compact` rearranges the contents of memory. If code is referencing
that memory (e.g. functions that have their code stored in flash) then they may
become garbled when compaction happens. To avoid this, call `eraseFiles` before
uploading data that you intend to reference to ensure that uploaded files are
right at the start of flash and cannot be compacted further.
*/
void jswrap_storage_compact(bool showMessage) {
jsfCompact(showMessage);
}
/*JSON{
"type" : "staticmethod",
"ifdef" : "DEBUG",
"class" : "Storage",
"name" : "debug",
"generate" : "jswrap_storage_debug"
}
This writes information about all blocks in flash memory to the console - and is
only useful for debugging flash storage.
*/
void jswrap_storage_debug() {
jsfDebugFiles();
}
/*JSON{
"type" : "staticmethod",
"ifndef" : "SAVE_ON_FLASH",
"class" : "Storage",
"name" : "getFree",
"params" : [
["checkInternalFlash","bool","Check the internal flash (rather than external SPI flash). Default false, so will check external storage"]
],
"generate" : "jswrap_storage_getFree",
"return" : ["int","The amount of free bytes"]
}
Return the amount of free bytes available in Storage. Due to fragmentation there
may be more bytes available, but this represents the maximum size of file that
can be written.
**NOTE:** `checkInternalFlash` is only useful on DICKENS devices - other devices don't use two different flash banks
*/
int jswrap_storage_getFree(bool checkInternalFlash) {
uint32_t addr = 0;
#ifdef FLASH_SAVED_CODE2_START
addr = checkInternalFlash ? FLASH_SAVED_CODE_START : FLASH_SAVED_CODE2_START;
#endif
return (int)jsfGetStorageStats(addr,true).free;
}
/*JSON{
"type" : "staticmethod",
"ifndef" : "SAVE_ON_FLASH",
"class" : "Storage",
"name" : "getStats",
"params" : [
["checkInternalFlash","bool","Check the internal flash (rather than external SPI flash). Default false, so will check external storage"]
],
"generate" : "jswrap_storage_getStats",
"return" : ["JsVar","An object containing info about the current Storage system"]
}
Returns:
```
{
totalBytes // Amount of bytes in filesystem
freeBytes // How many bytes are left at the end of storage?
fileBytes // How many bytes of allocated files do we have?
fileCount // How many allocated files do we have?
trashBytes // How many bytes of trash files do we have?
trashCount // How many trash files do we have? (can be cleared with .compact)
}
```
**NOTE:** `checkInternalFlash` is only useful on DICKENS devices - other devices don't use two different flash banks
*/
JsVar *jswrap_storage_getStats(bool checkInternalFlash) {
JsVar *o = jsvNewObject();
if (!o) return NULL;
uint32_t addr = 0;
#ifdef FLASH_SAVED_CODE2_START
addr = checkInternalFlash ? FLASH_SAVED_CODE_START : FLASH_SAVED_CODE2_START;
#endif
JsfStorageStats stats = jsfGetStorageStats(addr, true);
jsvObjectSetChildAndUnLock(o, "totalBytes", jsvNewFromInteger((JsVarInt)stats.total));
jsvObjectSetChildAndUnLock(o, "freeBytes", jsvNewFromInteger((JsVarInt)stats.free));
jsvObjectSetChildAndUnLock(o, "fileBytes", jsvNewFromInteger((JsVarInt)stats.fileBytes));
jsvObjectSetChildAndUnLock(o, "fileCount", jsvNewFromInteger((JsVarInt)stats.fileCount));
jsvObjectSetChildAndUnLock(o, "trashBytes", jsvNewFromInteger((JsVarInt)stats.trashBytes));
jsvObjectSetChildAndUnLock(o, "trashCount", jsvNewFromInteger((JsVarInt)stats.trashCount));
return o;
}
/*JSON{
"type" : "staticmethod",
"ifndef" : "SAVE_ON_FLASH",
"class" : "Storage",
"name" : "optimise",
"generate" : "jswrap_storage_optimise"
}
Writes a lookup table for files into Bangle.js's storage. This allows any file
stored up to that point to be accessed quickly.
*/
void jswrap_storage_optimise() {
#ifdef ESPR_STORAGE_FILENAME_TABLE
jsfCreateFileTable();
#endif
}
/*JSON{
"type" : "staticmethod",
"ifndef" : "SAVE_ON_FLASH",
"class" : "Storage",
"name" : "open",
"generate" : "jswrap_storage_open",
"params" : [
["name","JsVar","The filename - max **27** characters (case sensitive)"],
["mode","JsVar","The open mode - must be either `'r'` for read,`'w'` for write , or `'a'` for append"]
],
"return" : ["JsVar","An object containing {read,write,erase}"],
"return_object" : "StorageFile",
"typescript" : "open(name: string, mode: \"r\" | \"w\" | \"a\"): StorageFile;"
}
Open a file in the Storage area. This can be used for appending data
(normal read/write operations only write the entire file).
Please see `StorageFile` for more information (and examples).
**Note:** These files write through immediately - they do not need closing.
*/
JsVar *jswrap_storage_open(JsVar *name, JsVar *modeVar) {
char mode = 0;
if (jsvIsStringEqual(modeVar,"r")) mode='r';
else if (jsvIsStringEqual(modeVar,"w")) mode='w';
else if (jsvIsStringEqual(modeVar,"a")) mode='a';
else {
jsExceptionHere(JSET_ERROR, "Invalid mode %j", modeVar);
return 0;
}
JsVar *f = jspNewObject(0, "StorageFile");
if (!f) return 0;
int chunk = 1;
JsVar *n = jsvNewFromStringVar(name,0,sizeof(JsfFileName));
JsfFileName fname = jsfNameFromVar(n);
int fnamei = sizeof(fname)-1;
while (fnamei && fname.c[fnamei-1]==0) fnamei--;
fname.c[fnamei]=(char)chunk;
jsvObjectSetChildAndUnLock(f,"name",n);
int offset = 0; // offset in file
JsfFileHeader header;
uint32_t addr = jsfFindFile(fname, &header);
if (mode=='w') { // write,
if (addr) { // we had a file - erase it
jswrap_storagefile_erase(f);
addr = 0;
}
}
int fileLen = addr ? (int)jsfGetFileSize(&header) : 0;
if (mode=='a') { // append
// Find the last free page (eg it has 0xFF at the end)
unsigned char lastCh = 255;
if (addr) jshFlashRead(&lastCh, addr+jsfGetFileSize(&header)-1, 1);
while (addr && lastCh!=255 && chunk<255) {
chunk++;
fname.c[fnamei]=(char)chunk;
addr = jsfFindFile(fname, &header);
if (addr) {
fileLen = (int)jsfGetFileSize(&header);
jshFlashRead(&lastCh, addr+fileLen-1, 1);
} else {
fileLen = 0;
}
}
if (addr) {
// if we have a page, try and find the end of it
char buf[64];
bool foundEnd = false;
while (!foundEnd) {
int l = (int)fileLen - offset;
if (l<=0) {
foundEnd = true;
break;
}
if (l>(int)sizeof(buf)) l=(int)sizeof(buf);
jshFlashRead(buf, addr+(uint32_t)offset, (uint32_t)l);
for (int i=0;i<l;i++) {
if (buf[i]==(char)255) {
l = i;
foundEnd = true;
break;
}
}
offset += l;
}
}
// Now 'chunk' and offset points to the last (or a free) page
}
if (mode=='r') {
// read - do nothing, we're good.
}
DBG("Open %j Chunk %d Offset %d addr 0x%08x len %d\n",name,chunk,offset,addr,fileLen);
jsvObjectSetChildAndUnLock(f,"chunk",jsvNewFromInteger(chunk));
jsvObjectSetChildAndUnLock(f,"offset",jsvNewFromInteger(offset));
jsvObjectSetChildAndUnLock(f,"mode",jsvNewFromInteger(mode));
return f;
}
/*JSON{
"type" : "class",
"class" : "StorageFile",
"ifndef" : "SAVE_ON_FLASH"
}
These objects are created from `require("Storage").open` and allow Storage items
to be read/written.
The `Storage` library writes into Flash memory (which can only be erased in
chunks), and unlike a normal filesystem it allocates files in one long
contiguous area to allow them to be accessed easily from Espruino.
This presents a challenge for `StorageFile` which allows you to append to a
file, so instead `StorageFile` stores files in chunks. It uses the last
character of the filename to denote the chunk number (e.g. `"foobar\1"`,
`"foobar\2"`, etc).
This means that while `StorageFile` files exist in the same area as those from
`Storage`, they should be read using `Storage.open` (and not `Storage.read`).
```
f = require("Storage").open("foobar","w");
f.write("Hell");
f.write("o World\n");
f.write("Hello\n");
f.write("World 2\n");
f.write("Hello World 3\n");
// there's no need to call 'close'
// then
f = require("Storage").open("foobar","r");
f.read(13) // "Hello World\nH"
f.read(13) // "ello\nWorld 2\n"
f.read(13) // "Hello World 3"
f.read(13) // "\n"
f.read(13) // undefined
// or
f = require("Storage").open("foobar","r");
f.readLine() // "Hello World\n"
f.readLine() // "Hello\n"
f.readLine() // "World 2\n"
f.readLine() // "Hello World 3\n"
f.readLine() // undefined
// now get rid of file
f.erase();
```
**Note:** `StorageFile` uses the fact that all bits of erased flash memory are 1
to detect the end of a file. As such you should not write character code 255
(`"\xFF"`) to these files.
*/
JsVar *jswrap_storagefile_read_internal(JsVar *f, int len) {
bool isReadLine = len<0;
char mode = (char)jsvObjectGetIntegerChild(f,"mode");
if (mode!='r') {
jsExceptionHere(JSET_ERROR, "Can't read in this mode");
return 0;
}
int chunk = jsvObjectGetIntegerChild(f,"chunk");
JsfFileName fname = jsfNameFromVarAndUnLock(jsvObjectGetChildIfExists(f,"name"));
int fnamei = sizeof(fname)-1;
while (fnamei && fname.c[fnamei-1]==0) fnamei--;
fname.c[fnamei]=(char)chunk;
JsfFileHeader header;
uint32_t addr = jsfFindFile(fname, &header);
if (!addr) return 0; // end of file/no file chunk found
int fileLen = (int)jsfGetFileSize(&header);
int offset = jsvObjectGetIntegerChild(f,"offset");
JsVar *result = 0;
char buf[32];
if (isReadLine) len = sizeof(buf);
while (len) {
int remaining = fileLen - offset;
if (remaining<=0) { // next page
offset = 0;
if (chunk==255) {
addr=0; // end of file!
} else {
chunk++;
fname.c[fnamei]=(char)chunk;
addr = jsfFindFile(fname, &header);
fileLen = (int)jsfGetFileSize(&header);
}
jsvObjectSetChildAndUnLock(f,"offset",jsvNewFromInteger(offset));
jsvObjectSetChildAndUnLock(f,"chunk",jsvNewFromInteger(chunk));
remaining = fileLen;
if (!addr) {
// end of file!
return result;
}
}
int l = len;
if (l>(int)sizeof(buf)) l=(int)sizeof(buf);
if (l>remaining) l=remaining;
jshFlashRead(buf, addr+(uint32_t)offset, (uint32_t)l);
for (int i=0;i<l;i++) {
if (buf[i]==(char)255) {
// end of file!
l = i;
len = l;
break;
}
if (isReadLine && buf[i]=='\n') {
l = i+1;
len = l;
isReadLine = false; // done
break;
}
}
if (!l) break;
if (!result)
result = jsvNewFromEmptyString();
if (result)
jsvAppendStringBuf(result,buf,(size_t)l);
len -= l;
offset += l;
// if we're still reading lines, set the length to buffer size
if (isReadLine)
len = sizeof(buf);
}
jsvObjectSetChildAndUnLock(f,"offset",jsvNewFromInteger(offset));
return result;
}
/*JSON{
"type" : "method",
"ifndef" : "SAVE_ON_FLASH",
"class" : "StorageFile",
"name" : "read",
"generate" : "jswrap_storagefile_read",
"params" : [
["len","int","How many bytes to read"]
],
"return" : ["JsVar","A String, or undefined "],
"return_object" : "String"
}
Read 'len' bytes of data from the file, and return a String containing those
bytes.
If the end of the file is reached, the String may be smaller than the amount of
bytes requested, or if the file is already at the end, `undefined` is returned.
*/
JsVar *jswrap_storagefile_read(JsVar *f, int len) {
if (len<0) len=0;
return jswrap_storagefile_read_internal(f,len);
}
/*JSON{
"type" : "method",
"ifndef" : "SAVE_ON_FLASH",
"class" : "StorageFile",
"name" : "readLine",
"generate" : "jswrap_storagefile_readLine",
"return" : ["JsVar","A line of data"],
"return_object" : "String"
}
Read a line of data from the file (up to and including `"\n"`)
*/
JsVar *jswrap_storagefile_readLine(JsVar *f) {
return jswrap_storagefile_read_internal(f,-1);
}
/*JSON{
"type" : "method",
"ifndef" : "SAVE_ON_FLASH",
"class" : "StorageFile",
"name" : "getLength",
"generate" : "jswrap_storagefile_getLength",
"return" : ["int","The current length in bytes of the file"]
}
Return the length of the current file.
This requires Espruino to read the file from scratch, which is not a fast
operation.
*/
int jswrap_storagefile_getLength(JsVar *f) {
// Get name and position of name digit
JsVar *n = jsvObjectGetChildIfExists(f,"name");
JsfFileName fname = jsfNameFromVar(n);
jsvUnLock(n);
int fnamei = sizeof(fname)-1;
while (fnamei && fname.c[fnamei-1]==0) fnamei--;
int chunk = 1;
fname.c[fnamei]=(char)chunk;
int length = 0; // actual length
int offset = 0; // offset in file
JsfFileHeader header;
uint32_t addr = jsfFindFile(fname, &header);
// Find the last free page
unsigned char lastCh = 255;
if (addr) jshFlashRead(&lastCh, addr+jsfGetFileSize(&header)-1, 1);
while (addr && lastCh!=255 && chunk<255) {
length += (int)jsfGetFileSize(&header);
chunk++;
fname.c[fnamei]=(char)chunk;
addr = jsfFindFile(fname, &header);
if (addr) jshFlashRead(&lastCh, addr+jsfGetFileSize(&header)-1, 1);
}
if (addr) {
// if we have a page, try and find the end of it
char buf[64];
bool foundEnd = false;
while (!foundEnd) {
int l = (int)jsfGetFileSize(&header) - offset;
if (l<=0) {
foundEnd = true;
break;
}
if (l>(int)sizeof(buf)) l=(int)sizeof(buf);
jshFlashRead(buf, addr+(uint32_t)offset, (uint32_t)l);
for (int i=0;i<l;i++) {
if (buf[i]==(char)255) {
l = i;
foundEnd = true;
break;
}
}
offset += l;
}
}
length += offset;
return length;
}
/*JSON{
"type" : "method",
"ifndef" : "SAVE_ON_FLASH",
"class" : "StorageFile",
"name" : "write",
"generate" : "jswrap_storagefile_write",
"params" : [
["data","JsVar","The data to write. This should not include `'\\xFF'` (character code 255)"]
],
"typescript" : "write(data: string): void;"
}
Append the given data to a file. You should not attempt to append `"\xFF"`
(character code 255).
*/
void jswrap_storagefile_write(JsVar *f, JsVar *_data) {
char mode = (char)jsvObjectGetIntegerChild(f,"mode");
if (mode!='w' && mode!='a') {
jsExceptionHere(JSET_ERROR, "Can't write in this mode");
return;
}
JsVar *data = jsvAsString(_data);
if (!data) return;
size_t len = jsvGetStringLength(data);
if (len==0) {
jsvUnLock(data);
return;
}
int offset = jsvObjectGetIntegerChild(f,"offset");
int chunk = jsvObjectGetIntegerChild(f,"chunk");
JsfFileName fname = jsfNameFromVarAndUnLock(jsvObjectGetChildIfExists(f,"name"));
int fnamei = sizeof(fname)-1;
while (fnamei && fname.c[fnamei-1]==0) fnamei--;
//DBG("Filename[%d]=%d\n",fnamei,chunk);
fname.c[fnamei]=(char)chunk;
JsfFileHeader header;
uint32_t addr = jsfFindFile(fname, &header);
int fileLen = addr ? (int)jsfGetFileSize(&header) : 0;
DBG("Write Chunk %d Offset %d addr 0x%08x\n",chunk,offset,addr);
if (!addr) {
DBG("Write Create Chunk\n");
// What if data length > the chunk size? jsfWriteFile fails with error currently
if (jsfWriteFile(fname, data, JSFF_STORAGEFILE, 0, STORAGEFILE_CHUNKSIZE)) {
addr = jsfFindFile(fname, &header);
fileLen = (int)jsfGetFileSize(&header);
offset = (int)len;
jsvObjectSetChildAndUnLock(f,"offset",jsvNewFromInteger(offset));
} else {
DBG("Write Create Chunk FAILED\n");
// there would already have been an exception
jsvObjectSetChildAndUnLock(f,"mode",jsvNewFromInteger(0)); // set mode to 0 so no more writing
}
jsvUnLock(data);
return;
}
int remaining = fileLen - offset;
if ((int)len<remaining) {
DBG("Write Append Chunk\n");
// Great, it all fits in
jswrap_flash_write(data, (int)addr+offset);
offset += (int)len;
jsvObjectSetChildAndUnLock(f,"offset",jsvNewFromInteger(offset));
} else {
DBG("Write Append Chunk and create new file\n");
// Fill up this page, do part of old page
// End of this page
JsVar *part = jsvNewFromStringVar(data,0,(size_t)remaining);
jswrap_flash_write(part, (int)addr+offset);
offset += remaining; // update offset now we've written
jsvObjectSetChildAndUnLock(f,"offset",jsvNewFromInteger(offset));
jsvUnLock(part);
// Next page
if (chunk==255) {
jsExceptionHere(JSET_ERROR, "File too big!");
jsvUnLock(data);
return;
} else {
chunk++;
fname.c[fnamei]=(char)chunk;
}
// Write Next page
part = jsvNewFromStringVar(data,(size_t)remaining,JSVAPPENDSTRINGVAR_MAXLENGTH);
if (jsfWriteFile(fname, part, JSFF_STORAGEFILE, 0, STORAGEFILE_CHUNKSIZE)) {
offset = (int)jsvGetStringLength(part);
jsvObjectSetChildAndUnLock(f,"chunk",jsvNewFromInteger(chunk));
jsvObjectSetChildAndUnLock(f,"offset",jsvNewFromInteger(offset));
} else {
// there would already have been an exception - no need to return
// we can free data up below
jsvObjectSetChildAndUnLock(f,"mode",jsvNewFromInteger(0)); // set mode to 0 so no more writing
}
jsvUnLock(part);
}
jsvUnLock(data);
}
/*JSON{
"type" : "method",
"ifndef" : "SAVE_ON_FLASH",
"class" : "StorageFile",
"name" : "erase",
"generate" : "jswrap_storagefile_erase"
}
Erase this `StorageFile` - after being called this file can no longer be written to.
**Note:** You shouldn't call `require("Storage").erase(...)` on a `StorageFile`, but should
instead open the StorageFile and call `.erase` on the returned file: `require("Storage").open(..., "r").erase()`
*/
void jswrap_storagefile_erase(JsVar *f) {
JsfFileName fname = jsfNameFromVarAndUnLock(jsvObjectGetChildIfExists(f,"name"));
int fnamei = sizeof(fname)-1;
while (fnamei && fname.c[fnamei-1]==0) fnamei--;
// erase all numbered files
int chunk = 1;
bool ok = true;
while (ok) {
fname.c[fnamei]=(char)chunk;
ok = jsfEraseFile(fname);
chunk++;
}
// reset everything
jsvObjectSetChildAndUnLock(f,"chunk",jsvNewFromInteger(1));
jsvObjectSetChildAndUnLock(f,"offset",jsvNewFromInteger(0));
jsvObjectSetChildAndUnLock(f,"mode",jsvNewFromInteger(0));
}
/*JSON{
"type" : "method",
"class" : "StorageFile",
"name" : "pipe",
"ifndef" : "SAVE_ON_FLASH",
"generate" : "jswrap_pipe",
"params" : [
["destination","JsVar","The destination file/stream that will receive content from the source."],
["options","JsVar",["[optional] An object `{ chunkSize : int=32, end : bool=true, complete : function }`","chunkSize : The amount of data to pipe from source to destination at a time","complete : a function to call when the pipe activity is complete","end : call the 'end' function on the destination when the source is finished"]]
],
"typescript": "pipe(destination: any, options?: PipeOptions): void"
}
Pipe this file to a stream (an object with a 'write' method)
*/