Skip to content

Commit df076df

Browse files
committed
SD: bugfixes for ls
1 parent b518d11 commit df076df

File tree

1 file changed

+24
-11
lines changed
  • ArduinoCore-Linux/libraries

1 file changed

+24
-11
lines changed

ArduinoCore-Linux/libraries/SD.h

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ class SdSpiConfig {
7070
class File : public Stream {
7171
public:
7272
bool isOpen() {
73+
if (filename == "") return false;
74+
if (is_dir) return true;
7375
bool result = file.is_open();
7476
return result;
7577
}
@@ -85,8 +87,12 @@ class File : public Stream {
8587
is_dir = true;
8688
size_bytes = 0;
8789
#ifdef USE_FILESYSTEM
88-
dir_path = std::filesystem::path(filename);
89-
iterator = std::filesystem::directory_iterator({dir_path});
90+
// prevent authorization exceptions
91+
try {
92+
dir_path = std::filesystem::path(filename);
93+
iterator = std::filesystem::directory_iterator({dir_path});
94+
} catch (...) {
95+
}
9096
#endif
9197
return true;
9298
} else {
@@ -131,10 +137,17 @@ class File : public Stream {
131137
return is_dir;
132138
}
133139
bool openNext(File &dir, int flags = O_RDONLY) {
140+
if (!*this) return false;
141+
142+
// if (pos == 0) {
143+
// dir_path = std::filesystem::path(filename);
144+
// iterator = std::filesystem::directory_iterator({dir_path});
145+
// }
146+
// pos++;
134147
#ifdef USE_FILESYSTEM
135-
if (dir.isDir() && dir.iterator != end(dir.iterator)) {
136-
std::filesystem::directory_entry entry = *dir.iterator++;
137-
return open(entry.path().c_str(), flags);
148+
if (iterator != end(iterator)) {
149+
std::filesystem::directory_entry entry = *iterator++;
150+
return dir.open(entry.path().c_str(), flags);
138151
}
139152
#endif
140153
return false;
@@ -169,9 +182,9 @@ class File : public Stream {
169182
protected:
170183
std::fstream file;
171184
size_t size_bytes = 0;
172-
bool is_dir;
185+
bool is_dir = false;
173186
int pos = 0;
174-
std::string filename;
187+
std::string filename = "";
175188
struct stat info;
176189

177190
#ifdef USE_FILESYSTEM
@@ -180,7 +193,7 @@ class File : public Stream {
180193
#endif
181194

182195
std::ios_base::openmode toMode(int flags) {
183-
return (std::ios_base::openmode) flags;
196+
return (std::ios_base::openmode)flags;
184197
}
185198
};
186199

@@ -191,7 +204,7 @@ class File : public Stream {
191204
class SdFat {
192205
public:
193206
bool begin(int cs = SS, int speed = 0) { return true; }
194-
207+
195208
bool begin(SdSpiConfig &cfg) { return true; }
196209

197210
void errorHalt(const char *msg) {
@@ -220,12 +233,12 @@ class SdFat {
220233
uint64_t totalBytes() {
221234
std::error_code ec;
222235
const std::filesystem::space_info si = std::filesystem::space("/home", ec);
223-
return si.capacity;
236+
return si.capacity;
224237
}
225238
uint64_t usedBytes() {
226239
std::error_code ec;
227240
const std::filesystem::space_info si = std::filesystem::space("/home", ec);
228-
return si.capacity - si.available;
241+
return si.capacity - si.available;
229242
}
230243
};
231244

0 commit comments

Comments
 (0)