Skip to content

Commit 6dfebec

Browse files
authored
SdFat -> FS HAL mode fixes & test (#8833)
* re-use SdFat access mode through static const, no need to hard-code our own value w/ cast in the macro * separate read-modes from flags; read, write and rw are distinct numbers * simple compile-time tests in .cpp resolve #8831
1 parent d7da591 commit 6dfebec

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

libraries/SD/src/SD.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
#include "SD.h"
22

3+
static_assert(__builtin_strcmp(SDClassFileMode(FILE_READ), "r") == 0, "");
4+
static_assert(__builtin_strcmp(SDClassFileMode(FILE_WRITE), "a+") == 0, "");
5+
6+
static_assert(__builtin_strcmp(SDClassFileMode(O_RDONLY), "r") == 0, "");
7+
static_assert(__builtin_strcmp(SDClassFileMode(O_WRONLY), "w+") == 0, "");
8+
static_assert(__builtin_strcmp(SDClassFileMode(O_RDWR), "w+") == 0, "");
9+
static_assert(__builtin_strcmp(SDClassFileMode(O_WRONLY | O_APPEND), "a") == 0, "");
10+
static_assert(__builtin_strcmp(SDClassFileMode(O_RDWR | O_APPEND), "a+") == 0, "");
11+
312
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SD)
413
SDClass SD;
514
#endif

libraries/SD/src/SD.h

+37-16
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,44 @@
2424
#include <FS.h>
2525
#include <SDFS.h>
2626

27+
// Avoid type ambiguity, force u8 instead of untyped literal
28+
// ref. #6106 as to why we add APPEND to WRITE
29+
30+
inline constexpr uint8_t SDClassFileRead { FILE_READ };
2731
#undef FILE_READ
28-
#define FILE_READ ((uint8_t)O_READ)
29-
#undef FILE_WRITE
30-
#define FILE_WRITE ((uint8_t)(O_READ | O_WRITE | O_CREAT | O_APPEND))
32+
#define FILE_READ SDClassFileRead
3133

34+
inline constexpr uint8_t SDClassFileWrite { FILE_WRITE | O_APPEND };
35+
#undef FILE_WRITE
36+
#define FILE_WRITE SDClassFileWrite
37+
38+
static inline constexpr const char* SDClassFileMode(uint8_t mode) {
39+
bool read = false;
40+
bool write = false;
41+
42+
switch (mode & O_ACCMODE) {
43+
case O_RDONLY:
44+
read = true;
45+
break;
46+
case O_WRONLY:
47+
write = true;
48+
break;
49+
case O_RDWR:
50+
read = true;
51+
write = true;
52+
break;
53+
}
54+
55+
const bool append = (mode & O_APPEND) > 0;
56+
57+
if ( read && !write ) { return "r"; }
58+
else if ( !read && write && !append ) { return "w+"; }
59+
else if ( !read && write && append ) { return "a"; }
60+
else if ( read && write && !append ) { return "w+"; } // may be a bug in FS::mode interpretation, "r+" seems proper
61+
else if ( read && write && append ) { return "a+"; }
62+
63+
return "r";
64+
}
3265

3366
class SDClass {
3467
public:
@@ -45,7 +78,7 @@ class SDClass {
4578
}
4679

4780
fs::File open(const char *filename, uint8_t mode = FILE_READ) {
48-
return SDFS.open(filename, getMode(mode));
81+
return SDFS.open(filename, SDClassFileMode(mode));
4982
}
5083

5184
fs::File open(const char *filename, const char *mode) {
@@ -158,18 +191,6 @@ class SDClass {
158191
}
159192

160193
private:
161-
const char *getMode(uint8_t mode) {
162-
bool read = (mode & O_READ) ? true : false;
163-
bool write = (mode & O_WRITE) ? true : false;
164-
bool append = (mode & O_APPEND) ? true : false;
165-
if ( read & !write ) { return "r"; }
166-
else if ( !read & write & !append ) { return "w+"; }
167-
else if ( !read & write & append ) { return "a"; }
168-
else if ( read & write & !append ) { return "w+"; } // may be a bug in FS::mode interpretation, "r+" seems proper
169-
else if ( read & write & append ) { return "a+"; }
170-
else { return "r"; }
171-
}
172-
173194
static time_t wrapperTimeCB(void) {
174195
extern void (*__SD__userDateTimeCB)(uint16_t*, uint16_t*);
175196
if (__SD__userDateTimeCB) {

0 commit comments

Comments
 (0)