-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
LittleFS.h
718 lines (622 loc) · 20.6 KB
/
LittleFS.h
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
/*
LittleFS.h - Filesystem wrapper for LittleFS on the ESP8266
Copyright (c) 2019 Earle F. Philhower, III. All rights reserved.
Based heavily off of the SPIFFS equivalent code in the ESP8266 core
"Copyright (c) 2015 Ivan Grokhotkov. All rights reserved."
This code was influenced by NodeMCU and Sming libraries, and first version of
Arduino wrapper written by Hristo Gochkov.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __LITTLEFS_H
#define __LITTLEFS_H
#include <limits>
#include <FS.h>
#include <FSImpl.h>
#include <debug.h>
#include <flash_utils.h>
#include <flash_hal.h>
#define LFS_NAME_MAX 32
#include "../lib/littlefs/lfs.h"
using namespace fs;
namespace littlefs_impl {
class LittleFSFileImpl;
class LittleFSDirImpl;
class LittleFSConfig : public FSConfig
{
public:
static constexpr uint32_t FSId = 0x4c495454;
LittleFSConfig(bool autoFormat = true) : FSConfig(FSId, autoFormat) { }
};
class LittleFSImpl : public FSImpl
{
public:
LittleFSImpl(uint32_t start, uint32_t size, uint32_t pageSize, uint32_t blockSize, uint32_t maxOpenFds)
: _start(start) , _size(size) , _pageSize(pageSize) , _blockSize(blockSize) , _maxOpenFds(maxOpenFds),
_mounted(false) {
memset(&_lfs, 0, sizeof(_lfs));
memset(&_lfs_cfg, 0, sizeof(_lfs_cfg));
if (_size && _blockSize) {
_lfs_cfg.context = (void*) this;
_lfs_cfg.read = lfs_flash_read;
_lfs_cfg.prog = lfs_flash_prog;
_lfs_cfg.erase = lfs_flash_erase;
_lfs_cfg.sync = lfs_flash_sync;
_lfs_cfg.read_size = 64;
_lfs_cfg.prog_size = 64;
_lfs_cfg.block_size = _blockSize;
_lfs_cfg.block_count = _size / _blockSize;
_lfs_cfg.block_cycles = 16; // TODO - need better explanation
_lfs_cfg.cache_size = 64;
_lfs_cfg.lookahead_size = 64;
_lfs_cfg.read_buffer = nullptr;
_lfs_cfg.prog_buffer = nullptr;
_lfs_cfg.lookahead_buffer = nullptr;
_lfs_cfg.name_max = 0;
_lfs_cfg.file_max = 0;
_lfs_cfg.attr_max = 0;
}
}
~LittleFSImpl() {
if (_mounted) {
lfs_unmount(&_lfs);
}
}
FileImplPtr open(const char* path, OpenMode openMode, AccessMode accessMode) override;
DirImplPtr openDir(const char *path) override;
bool exists(const char* path) override {
if (!_mounted || !path || !path[0]) {
return false;
}
lfs_info info;
int rc = lfs_stat(&_lfs, path, &info);
return rc == 0;
}
bool rename(const char* pathFrom, const char* pathTo) override {
if (!_mounted || !pathFrom || !pathFrom[0] || !pathTo || !pathTo[0]) {
return false;
}
int rc = lfs_rename(&_lfs, pathFrom, pathTo);
if (rc != 0) {
DEBUGV("lfs_rename: rc=%d, from=`%s`, to=`%s`\n", rc, pathFrom, pathTo);
return false;
}
return true;
}
bool info(FSInfo& info) override {
if (!_mounted) {
return false;
}
info.maxOpenFiles = _maxOpenFds;
info.blockSize = _blockSize;
info.pageSize = _pageSize;
info.maxOpenFiles = _maxOpenFds;
info.maxPathLength = LFS_NAME_MAX;
info.totalBytes = _size;
info.usedBytes = _getUsedBlocks() * _blockSize;
return true;
}
virtual bool info64(FSInfo64& info64) {
FSInfo i;
if (!info(i)) {
return false;
}
info64.blockSize = i.blockSize;
info64.pageSize = i.pageSize;
info64.maxOpenFiles = i.maxOpenFiles;
info64.maxPathLength = i.maxPathLength;
info64.totalBytes = i.totalBytes;
info64.usedBytes = i.usedBytes;
return true;
}
bool remove(const char* path) override {
if (!_mounted || !path || !path[0]) {
return false;
}
int rc = lfs_remove(&_lfs, path);
if (rc != 0) {
DEBUGV("lfs_remove: rc=%d path=`%s`\n", rc, path);
return false;
}
// Now try and remove any empty subdirs this makes, silently
char *pathStr = strdup(path);
if (pathStr) {
char *ptr = strrchr(pathStr, '/');
while (ptr) {
*ptr = 0;
lfs_remove(&_lfs, pathStr); // Don't care if fails if there are files left
ptr = strrchr(pathStr, '/');
}
free(pathStr);
}
return true;
}
bool mkdir(const char* path) override {
if (!_mounted || !path || !path[0]) {
return false;
}
int rc = lfs_mkdir(&_lfs, path);
return (rc==0);
}
bool rmdir(const char* path) override {
return remove(path); // Same call on LittleFS
}
bool setConfig(const FSConfig &cfg) override {
if ((cfg._type != LittleFSConfig::FSId) || _mounted) {
return false;
}
_cfg = *static_cast<const LittleFSConfig *>(&cfg);
return true;
}
bool begin() override {
if (_mounted) {
return true;
}
if ((_blockSize <= 0) || (_size <= 0)) {
DEBUGV("LittleFS size is <= zero");
return false;
}
if (_tryMount()) {
return true;
}
if (!_cfg._autoFormat || !format()) {
return false;
}
return _tryMount();
}
void end() override {
if (!_mounted) {
return;
}
lfs_unmount(&_lfs);
_mounted = false;
}
bool format() override {
if ((_blockSize <= 0) || (_size <= 0)) {
DEBUGV("lfs size is zero\n");
return false;
}
bool wasMounted = _mounted;
if (_mounted) {
lfs_unmount(&_lfs);
_mounted = false;
}
memset(&_lfs, 0, sizeof(_lfs));
int rc = lfs_format(&_lfs, &_lfs_cfg);
if (rc != 0) {
DEBUGV("lfs_format: rc=%d\n", rc);
return false;
}
if(_timeCallback && _tryMount()) {
// Mounting is required to set attributes
time_t t = _timeCallback();
rc = lfs_setattr(&_lfs, "/", 'c', &t, 8);
if (rc != 0) {
DEBUGV("lfs_format, lfs_setattr 'c': rc=%d\n", rc);
return false;
}
rc = lfs_setattr(&_lfs, "/", 't', &t, 8);
if (rc != 0) {
DEBUGV("lfs_format, lfs_setattr 't': rc=%d\n", rc);
return false;
}
lfs_unmount(&_lfs);
_mounted = false;
}
if (wasMounted) {
return _tryMount();
}
return true;
}
time_t getCreationTime() override {
time_t t;
uint32_t t32b;
if (lfs_getattr(&_lfs, "/", 'c', &t, 8) == 8) {
return t;
} else if (lfs_getattr(&_lfs, "/", 'c', &t32b, 4) == 4) {
return (time_t)t32b;
} else {
return 0;
}
}
protected:
friend class LittleFSFileImpl;
friend class LittleFSDirImpl;
lfs_t* getFS() {
return &_lfs;
}
bool _tryMount() {
if (_mounted) {
lfs_unmount(&_lfs);
_mounted = false;
}
memset(&_lfs, 0, sizeof(_lfs));
int rc = lfs_mount(&_lfs, &_lfs_cfg);
if (rc==0) {
_mounted = true;
}
return _mounted;
}
int _getUsedBlocks() {
if (!_mounted) {
return 0;
}
return lfs_fs_size(&_lfs);
}
static int _getFlags(OpenMode openMode, AccessMode accessMode) {
int mode = 0;
if (openMode & OM_CREATE) {
mode |= LFS_O_CREAT;
}
if (openMode & OM_APPEND) {
mode |= LFS_O_APPEND;
}
if (openMode & OM_TRUNCATE) {
mode |= LFS_O_TRUNC;
}
if (accessMode & AM_READ) {
mode |= LFS_O_RDONLY;
}
if (accessMode & AM_WRITE) {
mode |= LFS_O_WRONLY;
}
return mode;
}
// Check that no components of path beyond max len
static bool pathValid(const char *path) {
while (*path) {
const char *slash = strchr(path, '/');
if (!slash) {
if (strlen(path) >= LFS_NAME_MAX) {
// Terminal filename is too long
return false;
}
break;
}
if ((slash - path) >= LFS_NAME_MAX) {
// This subdir name too long
return false;
}
path = slash + 1;
}
return true;
}
// The actual flash accessing routines
static int lfs_flash_read(const struct lfs_config *c, lfs_block_t block,
lfs_off_t off, void *buffer, lfs_size_t size);
static int lfs_flash_prog(const struct lfs_config *c, lfs_block_t block,
lfs_off_t off, const void *buffer, lfs_size_t size);
static int lfs_flash_erase(const struct lfs_config *c, lfs_block_t block);
static int lfs_flash_sync(const struct lfs_config *c);
lfs_t _lfs;
lfs_config _lfs_cfg;
LittleFSConfig _cfg;
uint32_t _start;
uint32_t _size;
uint32_t _pageSize;
uint32_t _blockSize;
uint32_t _maxOpenFds;
bool _mounted;
};
class LittleFSFileImpl : public FileImpl
{
public:
LittleFSFileImpl(LittleFSImpl* fs, const char *name, std::shared_ptr<lfs_file_t> fd, int flags, time_t creation) : _fs(fs), _fd(fd), _opened(true), _flags(flags), _creation(creation) {
_name = std::shared_ptr<char>(new char[strlen(name) + 1], std::default_delete<char[]>());
strcpy(_name.get(), name);
}
~LittleFSFileImpl() override {
if (_opened) {
close();
}
}
int availableForWrite () override {
if (!_opened || !_fd) {
return 0;
}
const auto f = _getFD();
const auto fs = _fs->getFS();
// check for remaining size in current block
// ignore inline feature (per code in lfs_file_rawwrite())
auto afw = fs->cfg->block_size - f->off;
if (afw == 0) {
// current block is full
// check for filesystem full (per code in lfs_alloc())
if (!(fs->free.i == fs->free.size && fs->free.ack == 0)) {
// fs is not full, return a full sector as free space
afw = fs->cfg->block_size;
}
}
return afw;
}
size_t write(const uint8_t *buf, size_t size) override {
if (!_opened || !_fd || !buf) {
return 0;
}
int result = lfs_file_write(_fs->getFS(), _getFD(), (void*) buf, size);
if (result < 0) {
DEBUGV("lfs_write rc=%d\n", result);
return 0;
}
return result;
}
int read(uint8_t* buf, size_t size) override {
if (!_opened || !_fd | !buf) {
return 0;
}
int result = lfs_file_read(_fs->getFS(), _getFD(), (void*) buf, size);
if (result < 0) {
DEBUGV("lfs_read rc=%d\n", result);
return 0;
}
return result;
}
void flush() override {
if (!_opened || !_fd) {
return;
}
int rc = lfs_file_sync(_fs->getFS(), _getFD());
if (rc < 0) {
DEBUGV("lfs_file_sync rc=%d\n", rc);
}
}
bool seek(uint32_t pos, SeekMode mode) override {
if (!_opened || !_fd) {
return false;
}
int32_t offset = static_cast<int32_t>(pos);
if (mode == SeekEnd) {
offset = -offset; // TODO - this seems like its plain wrong vs. POSIX
}
auto lastPos = position();
int rc = lfs_file_seek(_fs->getFS(), _getFD(), offset, (int)mode); // NB. SeekMode === LFS_SEEK_TYPES
if (rc < 0) {
DEBUGV("lfs_file_seek rc=%d\n", rc);
return false;
}
if (position() > size()) {
seek(lastPos, SeekSet); // Pretend the seek() never happened
return false;
}
return true;
}
size_t position() const override {
if (!_opened || !_fd) {
return 0;
}
int result = lfs_file_tell(_fs->getFS(), _getFD());
if (result < 0) {
DEBUGV("lfs_file_tell rc=%d\n", result);
return 0;
}
return result;
}
size_t size() const override {
return (_opened && _fd)? lfs_file_size(_fs->getFS(), _getFD()) : 0;
}
bool truncate(uint32_t size) override {
if (!_opened || !_fd) {
return false;
}
int rc = lfs_file_truncate(_fs->getFS(), _getFD(), size);
if (rc < 0) {
DEBUGV("lfs_file_truncate rc=%d\n", rc);
return false;
}
return true;
}
void close() override {
if (_opened && _fd) {
lfs_file_close(_fs->getFS(), _getFD());
_opened = false;
DEBUGV("lfs_file_close: fd=%p\n", _getFD());
if (_timeCallback && (_flags & LFS_O_WRONLY)) {
// If the file opened with O_CREAT, write the creation time attribute
if (_creation) {
int rc = lfs_setattr(_fs->getFS(), _name.get(), 'c', (const void *)&_creation, sizeof(_creation));
if (rc < 0) {
DEBUGV("Unable to set creation time on '%s' to %ld\n", _name.get(), (long)_creation);
}
}
// Add metadata with last write time
time_t now = _timeCallback();
int rc = lfs_setattr(_fs->getFS(), _name.get(), 't', (const void *)&now, sizeof(now));
if (rc < 0) {
DEBUGV("Unable to set last write time on '%s' to %ld\n", _name.get(), (long)now);
}
}
}
}
time_t getLastWrite() override {
time_t ftime = 0;
if (_opened && _fd) {
int rc = lfs_getattr(_fs->getFS(), _name.get(), 't', (void *)&ftime, sizeof(ftime));
if (rc != sizeof(ftime))
ftime = 0; // Error, so clear read value
}
return ftime;
}
time_t getCreationTime() override {
time_t ftime = 0;
if (_opened && _fd) {
int rc = lfs_getattr(_fs->getFS(), _name.get(), 'c', (void *)&ftime, sizeof(ftime));
if (rc != sizeof(ftime))
ftime = 0; // Error, so clear read value
}
return ftime;
}
const char* name() const override {
if (!_opened) {
return nullptr;
} else {
const char *p = _name.get();
const char *slash = strrchr(p, '/');
return (slash && slash[1]) ? slash + 1 : p;
}
}
const char* fullName() const override {
return _opened ? _name.get() : nullptr;
}
bool isFile() const override {
if (!_opened || !_fd) {
return false;
}
lfs_info info;
int rc = lfs_stat(_fs->getFS(), fullName(), &info);
return (rc == 0) && (info.type == LFS_TYPE_REG);
}
bool isDirectory() const override {
if (!_opened) {
return false;
} else if (!_fd) {
return true;
}
lfs_info info;
int rc = lfs_stat(_fs->getFS(), fullName(), &info);
return (rc == 0) && (info.type == LFS_TYPE_DIR);
}
protected:
lfs_file_t *_getFD() const {
return _fd.get();
}
LittleFSImpl *_fs;
std::shared_ptr<lfs_file_t> _fd;
std::shared_ptr<char> _name;
bool _opened;
int _flags;
time_t _creation;
};
class LittleFSDirImpl : public DirImpl
{
public:
LittleFSDirImpl(const String& pattern, LittleFSImpl* fs, std::shared_ptr<lfs_dir_t> dir, const char *dirPath = nullptr)
: _pattern(pattern) , _fs(fs) , _dir(dir) , _dirPath(nullptr), _valid(false), _opened(true)
{
memset(&_dirent, 0, sizeof(_dirent));
if (dirPath) {
_dirPath = std::shared_ptr<char>(new char[strlen(dirPath) + 1], std::default_delete<char[]>());
strcpy(_dirPath.get(), dirPath);
}
}
~LittleFSDirImpl() override {
if (_opened) {
lfs_dir_close(_fs->getFS(), _getDir());
}
}
FileImplPtr openFile(OpenMode openMode, AccessMode accessMode) override {
if (!_valid) {
return FileImplPtr();
}
int nameLen = 3; // Slashes, terminator
nameLen += _dirPath.get() ? strlen(_dirPath.get()) : 0;
nameLen += strlen(_dirent.name);
char tmpName[nameLen];
snprintf(tmpName, nameLen, "%s%s%s", _dirPath.get() ? _dirPath.get() : "", _dirPath.get()&&_dirPath.get()[0]?"/":"", _dirent.name);
auto ret = _fs->open((const char *)tmpName, openMode, accessMode);
return ret;
}
const char* fileName() override {
if (!_valid) {
return nullptr;
}
return (const char*) _dirent.name;
}
size_t fileSize() override {
if (!_valid) {
return 0;
}
return _dirent.size;
}
time_t fileTime() override {
time_t t;
int32_t t32b;
// If the attribute is 8-bytes, we're all set
if (_getAttr('t', 8, &t)) {
return t;
} else if (_getAttr('t', 4, &t32b)) {
// If it's 4 bytes silently promote to 64b
return (time_t)t32b;
} else {
// OTW, none present
return 0;
}
}
time_t fileCreationTime() override {
time_t t;
int32_t t32b;
// If the attribute is 8-bytes, we're all set
if (_getAttr('c', 8, &t)) {
return t;
} else if (_getAttr('c', 4, &t32b)) {
// If it's 4 bytes silently promote to 64b
return (time_t)t32b;
} else {
// OTW, none present
return 0;
}
}
bool isFile() const override {
return _valid && (_dirent.type == LFS_TYPE_REG);
}
bool isDirectory() const override {
return _valid && (_dirent.type == LFS_TYPE_DIR);
}
bool rewind() override {
_valid = false;
int rc = lfs_dir_rewind(_fs->getFS(), _getDir());
// Skip the . and .. entries
lfs_info dirent;
lfs_dir_read(_fs->getFS(), _getDir(), &dirent);
lfs_dir_read(_fs->getFS(), _getDir(), &dirent);
return (rc == 0);
}
bool next() override {
const int n = _pattern.length();
bool match;
do {
_dirent.name[0] = 0;
int rc = lfs_dir_read(_fs->getFS(), _getDir(), &_dirent);
_valid = (rc == 1);
match = (!n || !strncmp((const char*) _dirent.name, _pattern.c_str(), n));
} while (_valid && !match);
return _valid;
}
protected:
lfs_dir_t *_getDir() const {
return _dir.get();
}
bool _getAttr(char attr, int len, void *dest) {
if (!_valid || !len || !dest) {
return false;
}
int nameLen = 3; // Slashes, terminator
nameLen += _dirPath.get() ? strlen(_dirPath.get()) : 0;
nameLen += strlen(_dirent.name);
char tmpName[nameLen];
snprintf(tmpName, nameLen, "%s%s%s", _dirPath.get() ? _dirPath.get() : "", _dirPath.get()&&_dirPath.get()[0]?"/":"", _dirent.name);
int rc = lfs_getattr(_fs->getFS(), tmpName, attr, dest, len);
return (rc == len);
}
String _pattern;
LittleFSImpl *_fs;
std::shared_ptr<lfs_dir_t> _dir;
std::shared_ptr<char> _dirPath;
lfs_info _dirent;
bool _valid;
bool _opened;
};
};
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_LITTLEFS)
extern FS LittleFS;
using littlefs_impl::LittleFSConfig;
#endif // ARDUINO
#endif // !defined(__LITTLEFS_H)