Skip to content

Commit 4589fbc

Browse files
committed
Fat FS: Added r+ file open mode (and allow w+ to read)
1 parent 9f82380 commit 4589fbc

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
Fix Math.sin on negative numbers on extremely constrained builds (Bangle.js 1/Microbit) where we have to use a very small sin implementation
1717
Bangle.js2: Calling Bangle.getPressure() while a conversion is in progress now returns the same promise (so it will complete as soon as pressure data is ready) rather than erroring
1818
Bangle.js2: Fix 2v22 regression where `E.showMenu({test:{}})` would display 'test:undefined'
19+
Fat FS: Added `r+` file open mode (and allow w+ to read)
1920

2021
2v26 : nRF5x: ensure TIMER1_IRQHandler doesn't always wake idle loop up (fix #1900)
2122
Puck.js: On v2.1 ensure Puck.mag behaves like other variants - just returning the last reading (avoids glitches when used with Puck.magOn)

libs/filesystem/jswrap_file.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ static bool allocateJsFile(JsFile* file,FileMode mode, FileType type) {
275275
"generate" : "jswrap_E_openFile",
276276
"params" : [
277277
["path","JsVar","the path to the file to open."],
278-
["mode","JsVar","The mode to use when opening the file. Valid values for mode are 'r' for read, 'w' for write new, 'w+' for write existing, and 'a' for append. If not specified, the default is 'r'."]
278+
["mode","JsVar","The mode to use when opening the file. Valid values for mode are 'r' for read, 'r+' for read+write [2v27+], 'w' for write new, 'w+' for write existing, and 'a' for append. If not specified, the default is 'r'."]
279279
],
280280
"return" : ["JsVar","A File object"],
281281
"return_object" : "File"
@@ -313,11 +313,16 @@ JsVar *jswrap_E_openFile(JsVar* path, JsVar* mode) {
313313
#ifndef LINUX
314314
ff_mode = FA_READ | FA_OPEN_EXISTING;
315315
#endif
316+
} else if(strcmp(modeStr,"r+") == 0) {
317+
fMode = FM_READ_WRITE;
318+
#ifndef LINUX
319+
ff_mode = FA_READ | FA_WRITE | FA_OPEN_EXISTING;
320+
#endif
316321
} else if(strcmp(modeStr,"a") == 0) {
317322
fMode = FM_WRITE;
318323
#ifndef LINUX
319324
ff_mode = FA_WRITE | FA_OPEN_ALWAYS;
320-
append = true;
325+
append = true; // FA_OPEN_APPEND only appeared in newer FatFS
321326
#endif
322327
} else if(strcmp(modeStr,"w") == 0) {
323328
fMode = FM_WRITE;
@@ -327,7 +332,7 @@ JsVar *jswrap_E_openFile(JsVar* path, JsVar* mode) {
327332
} else if(strcmp(modeStr,"w+") == 0) {
328333
fMode = FM_READ_WRITE;
329334
#ifndef LINUX
330-
ff_mode = FA_WRITE | FA_OPEN_ALWAYS;
335+
ff_mode = FA_READ | FA_WRITE | FA_OPEN_ALWAYS;
331336
#endif
332337
}
333338
if(fMode != FM_NONE && allocateJsFile(&file, fMode, FT_FILE)) {

0 commit comments

Comments
 (0)