forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilesystemAdapter.cpp
More file actions
800 lines (647 loc) · 21.9 KB
/
FilesystemAdapter.cpp
File metadata and controls
800 lines (647 loc) · 21.9 KB
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
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2024
// MIT License
#include <MicroOcpp/Core/FilesystemAdapter.h>
#include <MicroOcpp/Core/ConfigurationOptions.h> //FilesystemOpt
#include <MicroOcpp/Core/Memory.h>
#include <MicroOcpp/Debug.h>
#include <cstring>
/*
* Platform specific implementations. Currently supported:
* - Arduino LittleFs
* - Arduino SPIFFS
* - ESP-IDF SPIFFS
* - POSIX-like API (tested on Ubuntu 20.04)
* Plus a filesystem index decorator working with any of the above
*
* You can add support for other file systems by passing a custom adapter to mocpp_initialize(...)
*/
#if MO_ENABLE_FILE_INDEX
#include <algorithm>
namespace MicroOcpp {
class FilesystemAdapterIndex;
class IndexedFileAdapter : public FileAdapter, public MemoryManaged {
private:
FilesystemAdapterIndex& index;
char fn [MO_MAX_PATH_SIZE];
std::unique_ptr<FileAdapter> file;
size_t written = 0;
public:
IndexedFileAdapter(FilesystemAdapterIndex& index, const char *fn, std::unique_ptr<FileAdapter> file)
: MemoryManaged("FilesystemIndex"), index(index), file(std::move(file)) {
snprintf(this->fn, sizeof(this->fn), "%s", fn);
}
~IndexedFileAdapter(); // destructor updates file index with written size
size_t read(char *buf, size_t len) override {
return file->read(buf, len);
}
size_t write(const char *buf, size_t len) override {
auto ret = file->write(buf, len);
written += ret;
return ret;
}
size_t seek(size_t offset) override {
auto ret = file->seek(offset);
written = ret;
return ret;
}
int read() override {
return file->read();
}
};
class FilesystemAdapterIndex : public FilesystemAdapter, public MemoryManaged {
private:
std::shared_ptr<FilesystemAdapter> filesystem;
struct IndexEntry {
String fname;
size_t size;
IndexEntry(const char *fname, size_t size) : fname(makeString("FilesystemIndex", fname)), size(size) { }
};
Vector<IndexEntry> index;
IndexEntry *getEntryByFname(const char *fn) {
auto entry = std::find_if(index.begin(), index.end(),
[fn] (const IndexEntry& el) -> bool {
return el.fname.compare(fn) == 0;
});
if (entry != index.end()) {
return &(*entry);
} else {
return nullptr;
}
}
IndexEntry *getEntryByPath(const char *path) {
if (strlen(path) < sizeof(MO_FILENAME_PREFIX) - 1) {
MO_DBG_ERR("invalid fn");
return nullptr;
}
const char *fn = path + sizeof(MO_FILENAME_PREFIX) - 1;
return getEntryByFname(fn);
}
void (*onDestruct)(void*) = nullptr;
public:
FilesystemAdapterIndex(std::shared_ptr<FilesystemAdapter> filesystem, void (*onDestruct)(void*) = nullptr) : MemoryManaged("FilesystemIndex"), filesystem(std::move(filesystem)), index(makeVector<IndexEntry>("FilesystemIndex")), onDestruct(onDestruct) { }
~FilesystemAdapterIndex() {
if (onDestruct) {
onDestruct(this);
}
}
int stat(const char *path, size_t *size) override {
if (auto file = getEntryByPath(path)) {
*size = file->size;
return 0;
} else {
return -1;
}
}
std::unique_ptr<FileAdapter> open(const char *path, const char *mode) {
if (!strcmp(mode, "r")) {
return filesystem->open(path, "r");
} else if (!strcmp(mode, "w")) {
if (strlen(path) < sizeof(MO_FILENAME_PREFIX) - 1) {
MO_DBG_ERR("invalid fn");
return nullptr;
}
const char *fn = path + sizeof(MO_FILENAME_PREFIX) - 1;
auto file = filesystem->open(path, "w");
if (!file) {
return nullptr;
}
IndexEntry *entry = nullptr;
if (!(entry = getEntryByFname(fn))) {
index.emplace_back(fn, 0);
entry = &index.back();
}
if (!entry) {
MO_DBG_ERR("internal error");
return nullptr;
}
entry->size = 0; //write always empties the file
return std::unique_ptr<IndexedFileAdapter>(new IndexedFileAdapter(*this, entry->fname.c_str(), std::move(file)));
} else {
MO_DBG_ERR("only support r or w");
return nullptr;
}
}
bool remove(const char *path) override {
if (strlen(path) >= sizeof(MO_FILENAME_PREFIX) - 1) {
//valid path
const char *fn = path + sizeof(MO_FILENAME_PREFIX) - 1;
index.erase(std::remove_if(index.begin(), index.end(),
[fn] (const IndexEntry& el) -> bool {
return el.fname.compare(fn) == 0;
}), index.end());
}
return filesystem->remove(path);
}
int ftw_root(std::function<int(const char *fpath)> fn) {
// allow fn to remove elements
for (size_t it = 0; it < index.size();) {
auto size_before = index.size();
auto err = fn(index[it].fname.c_str());
if (err) {
return err;
}
if (index.size() + 1 == size_before) {
// element removed
continue;
}
// normal execution
it++;
}
return 0;
}
bool createIndex() {
if (!index.empty()) {
return false;
}
auto ret = filesystem->ftw_root([this] (const char *fn) -> int {
int ret;
char path [MO_MAX_PATH_SIZE];
ret = snprintf(path, MO_MAX_PATH_SIZE, MO_FILENAME_PREFIX "%s", fn);
if (ret < 0 || ret >= MO_MAX_PATH_SIZE) {
MO_DBG_ERR("fn error: %i", ret);
return 0; //ignore this entry and continue ftw
}
size_t size;
ret = filesystem->stat(path, &size);
if (ret == 0) {
//add fn and size to index
MO_DBG_DEBUG("add file to index: %s (%zuB)", fn, size);
index.emplace_back(fn, size);
return 0; //successfully added filename to index
} else {
MO_DBG_ERR("unexpected entry: %s", fn);
return 0; //ignore this entry and continue ftw
}
});
MO_DBG_DEBUG("create fs index: %s, %zu entries", ret == 0 ? "success" : "failure", index.size());
return ret == 0;
}
void updateFilesize(const char *fn, size_t size) {
if (auto entry = getEntryByFname(fn)) {
entry->size = size;
MO_DBG_DEBUG("update index: %s (%zuB)", entry->fname.c_str(), entry->size);
}
}
};
IndexedFileAdapter::~IndexedFileAdapter() {
index.updateFilesize(fn, written);
}
std::shared_ptr<FilesystemAdapter> decorateIndex(std::shared_ptr<FilesystemAdapter> filesystem, void (*onDestruct)(void*) = nullptr) {
auto fsIndex = std::allocate_shared<FilesystemAdapterIndex>(makeAllocator<FilesystemAdapterIndex>("FilesystemIndex"), std::move(filesystem), onDestruct);
if (!fsIndex) {
MO_DBG_ERR("OOM");
return nullptr;
}
if (!fsIndex->createIndex()) {
MO_DBG_ERR("createIndex err");
return nullptr;
}
return fsIndex;
}
} // namespace MicroOcpp
#endif //MO_ENABLE_FILE_INDEX
#if MO_USE_FILEAPI == ARDUINO_LITTLEFS || MO_USE_FILEAPI == ARDUINO_SPIFFS
#if MO_USE_FILEAPI == ARDUINO_LITTLEFS
#include <LittleFS.h>
#include <vfs_api.h>
#define USE_FS LittleFS
#elif MO_USE_FILEAPI == ARDUINO_SPIFFS
#include <FS.h>
#define USE_FS SPIFFS
#endif
namespace MicroOcpp {
class ArduinoFileAdapter : public FileAdapter, public MemoryManaged {
File file;
public:
ArduinoFileAdapter(File&& file) : MemoryManaged("Filesystem"), file(file) {}
~ArduinoFileAdapter() {
if (file) {
file.close();
}
}
int read() override {
return file.read();
};
size_t read(char *buf, size_t len) override {
return file.readBytes(buf, len);
}
size_t write(const char *buf, size_t len) override {
return file.printf("%.*s", len, buf);
}
size_t seek(size_t offset) override {
return file.seek(offset);
}
};
class ArduinoFilesystemAdapter : public FilesystemAdapter, public MemoryManaged {
private:
bool valid = false;
FilesystemOpt config;
void (* onDestruct)(void*) = nullptr;
public:
ArduinoFilesystemAdapter(FilesystemOpt config, void (*onDestruct)(void*) = nullptr) : MemoryManaged("Filesystem"), config(config), onDestruct(onDestruct) {
valid = true;
if (config.mustMount()) {
#if MO_USE_FILEAPI == ARDUINO_LITTLEFS
if(!USE_FS.begin(config.formatOnFail())) {
MO_DBG_ERR("Error while mounting LITTLEFS");
valid = false;
} else {
MO_DBG_DEBUG("LittleFS mount success");
}
#elif MO_USE_FILEAPI == ARDUINO_SPIFFS
//ESP8266
SPIFFSConfig cfg;
cfg.setAutoFormat(config.formatOnFail());
SPIFFS.setConfig(cfg);
if (!SPIFFS.begin()) {
MO_DBG_ERR("Unable to initialize: unable to mount SPIFFS");
valid = false;
}
#else
#error
#endif
} //end if mustMount()
}
~ArduinoFilesystemAdapter() {
if (config.mustMount()) {
USE_FS.end();
}
if (onDestruct) {
onDestruct(this);
}
}
operator bool() {return valid;}
int stat(const char *path, size_t *size) override {
#if MO_USE_FILEAPI == ARDUINO_LITTLEFS
char partition_path [MO_MAX_PATH_SIZE];
auto ret = snprintf(partition_path, MO_MAX_PATH_SIZE, "/littlefs%s", path);
if (ret < 0 || ret >= MO_MAX_PATH_SIZE) {
MO_DBG_ERR("fn error: %i", ret);
return -1;
}
struct ::stat st;
auto status = ::stat(partition_path, &st);
if (status == 0) {
*size = st.st_size;
}
return status;
#elif MO_USE_FILEAPI == ARDUINO_SPIFFS
if (!USE_FS.exists(path)) {
return -1;
}
File f = USE_FS.open(path, "r");
if (!f) {
return -1;
}
int status = -1;
if (!f.isDirectory()) {
*size = f.size();
status = 0;
} else {
//fetch more information for directory when MicroOcpp also uses them
//status = 0;
}
f.close();
return status;
#else
#error
#endif
} //end stat
std::unique_ptr<FileAdapter> open(const char *fn, const char *mode) override {
File file = USE_FS.open(fn, mode);
if (file && !file.isDirectory()) {
MO_DBG_DEBUG("File open successful: %s", fn);
return std::unique_ptr<FileAdapter>(new ArduinoFileAdapter(std::move(file)));
} else {
return nullptr;
}
}
bool remove(const char *fn) override {
return USE_FS.remove(fn);
};
int ftw_root(std::function<int(const char *fpath)> fn) override {
#if MO_USE_FILEAPI == ARDUINO_LITTLEFS
auto dir = USE_FS.open(MO_FILENAME_PREFIX);
if (!dir) {
MO_DBG_ERR("cannot open root directory: " MO_FILENAME_PREFIX);
return -1;
}
int err = 0;
while (auto entry = dir.openNextFile()) {
char fname [MO_MAX_PATH_SIZE];
auto ret = snprintf(fname, MO_MAX_PATH_SIZE, "%s", entry.name());
entry.close();
if (ret < 0 || ret >= MO_MAX_PATH_SIZE) {
MO_DBG_ERR("fn error: %i", ret);
return -1;
}
err = fn(fname);
if (err) {
break;
}
}
return err;
#elif MO_USE_FILEAPI == ARDUINO_SPIFFS
auto dir = USE_FS.openDir(MO_FILENAME_PREFIX);
int err = 0;
while (dir.next()) {
auto fname = dir.fileName();
if (fname.c_str()) {
err = fn(fname.c_str() + strlen(MO_FILENAME_PREFIX));
} else {
MO_DBG_ERR("fs error");
err = -1;
}
if (err) {
break;
}
}
return err;
#else
#error
#endif
}
};
std::weak_ptr<FilesystemAdapter> filesystemCache;
void resetFilesystemCache(void*) {
filesystemCache.reset();
}
std::shared_ptr<FilesystemAdapter> makeDefaultFilesystemAdapter(FilesystemOpt config) {
if (auto cached = filesystemCache.lock()) {
return cached;
}
if (!config.accessAllowed()) {
MO_DBG_DEBUG("Access to Arduino FS not allowed by config");
return nullptr;
}
auto fs_concrete = new ArduinoFilesystemAdapter(config, resetFilesystemCache);
auto fs = std::shared_ptr<FilesystemAdapter>(fs_concrete, std::default_delete<FilesystemAdapter>(), makeAllocator<FilesystemAdapter>("Filesystem"));
#if MO_ENABLE_FILE_INDEX
fs = decorateIndex(fs, resetFilesystemCache);
#endif // MO_ENABLE_FILE_INDEX
filesystemCache = fs;
if (*fs_concrete) {
return fs;
} else {
return nullptr;
}
}
} //end namespace MicroOcpp
#elif MO_USE_FILEAPI == ESPIDF_SPIFFS
#include <sys/stat.h>
#include <dirent.h>
#include "esp_spiffs.h"
#ifndef MO_PARTITION_LABEL
#define MO_PARTITION_LABEL "mo"
#endif
namespace MicroOcpp {
class EspIdfFileAdapter : public FileAdapter, public MemoryManaged {
FILE *file {nullptr};
public:
EspIdfFileAdapter(FILE *file) : MemoryManaged("Filesystem"), file(file) {}
~EspIdfFileAdapter() {
fclose(file);
}
size_t read(char *buf, size_t len) override {
return fread(buf, 1, len, file);
}
size_t write(const char *buf, size_t len) override {
return fwrite(buf, 1, len, file);
}
size_t seek(size_t offset) override {
return fseek(file, offset, SEEK_SET);
}
int read() override {
return fgetc(file);
}
};
class EspIdfFilesystemAdapter : public FilesystemAdapter, public MemoryManaged {
public:
FilesystemOpt config;
void (* onDestruct)(void*) = nullptr;
public:
EspIdfFilesystemAdapter(FilesystemOpt config, void (* onDestruct)(void*) = nullptr) : MemoryManaged("Filesystem"), config(config), onDestruct(onDestruct) { }
~EspIdfFilesystemAdapter() {
if (config.mustMount()) {
esp_vfs_spiffs_unregister(MO_PARTITION_LABEL);
MO_DBG_DEBUG("SPIFFS unmounted");
}
if (onDestruct) {
onDestruct(this);
}
}
int stat(const char *path, size_t *size) override {
struct ::stat st;
auto ret = ::stat(path, &st);
if (ret == 0) {
*size = st.st_size;
}
return ret;
}
std::unique_ptr<FileAdapter> open(const char *fn, const char *mode) override {
auto file = fopen(fn, mode);
if (file) {
return std::unique_ptr<FileAdapter>(new EspIdfFileAdapter(std::move(file)));
} else {
MO_DBG_DEBUG("Failed to open file path %s", fn);
return nullptr;
}
}
bool remove(const char *fn) override {
return unlink(fn) == 0;
}
int ftw_root(std::function<int(const char *fpath)> fn) override {
//open MO root directory
char dname [MO_MAX_PATH_SIZE];
auto dlen = snprintf(dname, MO_MAX_PATH_SIZE, "%s", MO_FILENAME_PREFIX);
if (dlen < 0 || dlen >= MO_MAX_PATH_SIZE) {
MO_DBG_ERR("fn error: %i", dlen);
return -1;
}
// trim trailing '/' if not root directory
if (dlen >= 2 && dname[dlen - 1] == '/') {
dname[dlen - 1] = '\0';
}
auto dir = opendir(dname);
if (!dir) {
MO_DBG_ERR("cannot open root directory: %s", dname);
return -1;
}
int err = 0;
while (auto entry = readdir(dir)) {
err = fn(entry->d_name);
if (err) {
break;
}
}
closedir(dir);
return err;
}
};
std::weak_ptr<FilesystemAdapter> filesystemCache;
void resetFilesystemCache(void*) {
filesystemCache.reset();
}
std::shared_ptr<FilesystemAdapter> makeDefaultFilesystemAdapter(FilesystemOpt config) {
if (auto cached = filesystemCache.lock()) {
return cached;
}
if (!config.accessAllowed()) {
MO_DBG_DEBUG("Access to ESP-IDF SPIFFS not allowed by config");
return nullptr;
}
bool mounted = true;
if (config.mustMount()) {
mounted = false;
char fnpref [MO_MAX_PATH_SIZE];
auto fnpref_len = snprintf(fnpref, MO_MAX_PATH_SIZE, "%s", MO_FILENAME_PREFIX);
if (fnpref_len < 0 || fnpref_len >= MO_MAX_PATH_SIZE) {
MO_DBG_ERR("MO_FILENAME_PREFIX error %i", fnpref_len);
return nullptr;
} else if (fnpref_len <= 2) { //shortest possible prefix: "/p/", i.e. length = 3
MO_DBG_ERR("MO_FILENAME_PREFIX cannot be root on ESP-IDF (working example: \"/mo_store/\")");
return nullptr;
}
// trim trailing '/'
if (fnpref[fnpref_len - 1] == '/') {
fnpref[fnpref_len - 1] = '\0';
}
esp_vfs_spiffs_conf_t conf = {
.base_path = fnpref,
.partition_label = MO_PARTITION_LABEL,
.max_files = 5,
.format_if_mount_failed = config.formatOnFail()
};
esp_err_t ret = esp_vfs_spiffs_register(&conf);
if (ret == ESP_OK) {
mounted = true;
MO_DBG_DEBUG("SPIFFS mounted");
} else {
if (ret == ESP_FAIL) {
MO_DBG_ERR("Failed to mount or format filesystem");
} else if (ret == ESP_ERR_NOT_FOUND) {
MO_DBG_ERR("Failed to find SPIFFS partition");
} else {
MO_DBG_ERR("Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
}
}
}
if (mounted) {
auto fs = std::shared_ptr<FilesystemAdapter>(new EspIdfFilesystemAdapter(config, resetFilesystemCache), std::default_delete<FilesystemAdapter>(), makeAllocator<FilesystemAdapter>("Filesystem"));
#if MO_ENABLE_FILE_INDEX
fs = decorateIndex(fs, resetFilesystemCache);
#endif // MO_ENABLE_FILE_INDEX
filesystemCache = fs;
return fs;
} else {
return nullptr;
}
}
} //end namespace MicroOcpp
#elif MO_USE_FILEAPI == POSIX_FILEAPI
#include <cstdio>
#include <sys/stat.h>
#include <dirent.h>
namespace MicroOcpp {
class PosixFileAdapter : public FileAdapter, public MemoryManaged {
FILE *file {nullptr};
public:
PosixFileAdapter(FILE *file) : MemoryManaged("Filesystem"), file(file) {}
~PosixFileAdapter() {
fclose(file);
}
size_t read(char *buf, size_t len) override {
return fread(buf, 1, len, file);
}
size_t write(const char *buf, size_t len) override {
return fwrite(buf, 1, len, file);
}
size_t seek(size_t offset) override {
return fseek(file, offset, SEEK_SET);
}
int read() override {
return fgetc(file);
}
};
class PosixFilesystemAdapter : public FilesystemAdapter, public MemoryManaged {
public:
FilesystemOpt config;
void (* onDestruct)(void*) = nullptr;
public:
PosixFilesystemAdapter(FilesystemOpt config, void (* onDestruct)(void*) = nullptr) : MemoryManaged("Filesystem"), config(config), onDestruct(onDestruct) { }
~PosixFilesystemAdapter() {
if (onDestruct) {
onDestruct(this);
}
}
int stat(const char *path, size_t *size) override {
struct ::stat st;
auto ret = ::stat(path, &st);
if (ret == 0) {
*size = st.st_size;
}
return ret;
}
std::unique_ptr<FileAdapter> open(const char *fn, const char *mode) override {
auto file = fopen(fn, mode);
if (file) {
return std::unique_ptr<FileAdapter>(new PosixFileAdapter(std::move(file)));
} else {
MO_DBG_DEBUG("Failed to open file path %s", fn);
return nullptr;
}
}
bool remove(const char *fn) override {
return ::remove(fn) == 0;
}
int ftw_root(std::function<int(const char *fpath)> fn) override {
auto dir = opendir(MO_FILENAME_PREFIX); // use c_str() to convert the path string to a C-style string
if (!dir) {
MO_DBG_ERR("cannot open root directory: " MO_FILENAME_PREFIX);
return -1;
}
int err = 0;
while (auto entry = readdir(dir)) {
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
continue; //files . and .. are specific to desktop systems and rarely appear on microcontroller filesystems. Filter them
}
err = fn(entry->d_name);
if (err) {
break;
}
}
closedir(dir);
return err;
}
};
std::weak_ptr<FilesystemAdapter> filesystemCache;
void resetFilesystemCache(void*) {
filesystemCache.reset();
}
std::shared_ptr<FilesystemAdapter> makeDefaultFilesystemAdapter(FilesystemOpt config) {
if (auto cached = filesystemCache.lock()) {
return cached;
}
if (!config.accessAllowed()) {
MO_DBG_DEBUG("Access to FS not allowed by config");
return nullptr;
}
if (config.mustMount()) {
MO_DBG_DEBUG("Skip mounting on UNIX host");
}
auto fs = std::shared_ptr<FilesystemAdapter>(new PosixFilesystemAdapter(config, resetFilesystemCache), std::default_delete<FilesystemAdapter>(), makeAllocator<FilesystemAdapter>("Filesystem"));
#if MO_ENABLE_FILE_INDEX
fs = decorateIndex(fs, resetFilesystemCache);
#endif // MO_ENABLE_FILE_INDEX
filesystemCache = fs;
return fs;
}
} //end namespace MicroOcpp
#else //filesystem disabled
namespace MicroOcpp {
std::shared_ptr<FilesystemAdapter> makeDefaultFilesystemAdapter(FilesystemOpt config) {
return nullptr;
}
} //end namespace MicroOcpp
#endif //switch-case MO_USE_FILEAPI