1
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
71
72
75
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
135
136
137
138
141
142
143
144
146
147
148
149
150
151
152
153
154
155
156
157
158
161
162
163
164
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
230
231
232
233
234
245
246
247
248
249
250
251
252
253
256
262
263
267
268
269
270
271
272
273
274
275
279
280
281
282
283
284
285
286
287
288
289
290
291
292
295
296
297
298
299
302
303
304
305
306
307
308
309
310
311
312
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
377
378
379
380
386
387
394
400
401
402
403
408
409
410
411
412
413
414
415
416
417
421
422
423
427
428
429
430
431
432
433
434
435
436
437
441
442
443
444
448
449
450
451
452
453
454
455
456
457
458
462
463
464
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
494
495
499
500
501
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
536
537
541
542
543
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
581
582
583
587
588
589
593
594
595
596
597
598
599
600
601
602
603
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
695
696
697
698
699
700
701
705
706
707
708
709
710
711
720
721
727
728
729
730
731
732
733
734
735
736
746
747
748
749
750
751
752
753
754
755
756
757
758
759
769
770
771
772
773
774
775
776
777
778
779
780
781
791
792
793
794
795
796
797
802
813
814
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
850
851
852
853
854
855
856
857
861
862
863
864
865
866
867
868
869
870
871
872
873
874
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
909
910
911
915
916
917
918
919
920
921
922
923
924
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
966
967
968
972
973
974
975
976
981
982
983
984
985
986
987
988
989
990
995
996
997
998
999
1004
1005
1006
1007
1012
1013
1034
1035
1036
1042
1043
1044
1045
1046
1052
1053
1054
1055
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1073
1077
1082
1083
1084
1089
1090
1091
1092
1107
1108
1109
1110
1111
1112
1120
1121
1122
1123
1124
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1144
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1254
1255
1256
1257
1258
1259
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1297
1298
1299
1300
1301
1302
1303
1304
1308
1309
1310
1311
1312
1313
1314
1315
1316
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1386
1387
1388
1389
1390
1391
1404
1405
1406
1407
1408
1409
1410
1411
1417
1418
1419
1422
1423
1424
1425
1427
1428
1429
1430
1431
1432
1437
1438
1439
1440
1441
1442
1443
1445
1446
1448
1449
1450
1451
1452
1457
1458
1459
1460
1464
1465
1466
1467
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1507
1508
1509
1510
1514
1515
1516
1517
1518
1519
1521
1522
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1543
1544
1545
1546
1547
1548
1549
1550
1552
1553
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1596
1597
1601
1602
1603
1604
1605
1606
1607
1608
1610
1611
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1654
1655
1659
1660
1661
1662
1663
1664
1665
1666
1668
1669
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1704
1705
1707
1708
1709
1710
1711
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1736
1737
1738
1744
1745
1746
1747
1748
1749
1750
1751
1753
1754
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1777
1778
1779
1780
1781
1782
1783
1784
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1887
1888
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1927
1933
1934
1935
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1970
1971
1972
1977
1978
1979
1980
1981
1982
1983
1984
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2046
2047
2048
2049
2050
2052
2053
2054
2055
2056
2057
2058
2062
2065
2066
2071
2077
2078
2079
2080
2081
2082
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2105
2106
2107
2108
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2150
2151
2157
2158
2159
2160
2161
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2197
2198
2200
2201
2202
2203
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2225
2226
2227
2228
2229
2230
2231
2232
2233
2235
2236
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2268
2269
2270
2271
2272
2275
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2292
2293
2295
2296
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2398
2399
2400
2407
2408
2417
2418
2419
2420
2421
2422
2423
2429
2430
2431
2435
2436
2437
2438
2439
/* ... */
#ifndef LOG_LOCAL_LEVEL
#define LOG_LOCAL_LEVEL CONFIG_LOG_DEFAULT_LEVEL
#endif
#include "esp_littlefs.h"
#include "littlefs/lfs.h"
#include "sdkconfig.h"
#include "esp_log.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/task.h"
#include "littlefs_api.h"
#include <sys/dirent.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/lock.h>
#include <sys/param.h>
#include <unistd.h>15 includes
#ifdef CONFIG_LITTLEFS_SDMMC_SUPPORT
#include <sdmmc_cmd.h>
#endif
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
#include "spi_flash_mmap.h"
#else
#include "esp_spi_flash.h"
#endif
#if CONFIG_IDF_TARGET_ESP32
#include "esp32/rom/spi_flash.h"
#elif CONFIG_IDF_TARGET_ESP32S2
#include "esp32s2/rom/spi_flash.h"
#elif CONFIG_IDF_TARGET_ESP32S3
#include "esp32s3/rom/spi_flash.h"
#elif CONFIG_IDF_TARGET_ESP32C3
#include "esp32c3/rom/spi_flash.h"
#elif CONFIG_IDF_TARGET_ESP32H2
#include "esp32h2/rom/spi_flash.h"
#elif CONFIG_IDF_TARGET_ESP8684
#include "esp8684/rom/spi_flash.h"
#elif __has_include("esp32/rom/spi_flash.h")
#include "esp32/rom/spi_flash.h"
#else
#include "rom/spi_flash.h"
#endif
#define CONFIG_LITTLEFS_BLOCK_SIZE 4096
#define CONFIG_LITTLEFS_FD_CACHE_REALLOC_FACTOR 2
#define CONFIG_LITTLEFS_FD_CACHE_MIN_SIZE 4
#define CONFIG_LITTLEFS_FD_CACHE_HYST 4
/* ... */
#define LITTLEFS_ATTR_MTIME ((uint8_t) 't')5 defines
/* ... */
typedef struct {
DIR dir;
lfs_dir_t d;
struct dirent e;
long offset;
char *path;
}{ ... } vfs_littlefs_dir_t;
static int vfs_littlefs_open(void* ctx, const char * path, int flags, int mode);
static ssize_t vfs_littlefs_write(void* ctx, int fd, const void * data, size_t size);
static ssize_t vfs_littlefs_read(void* ctx, int fd, void * dst, size_t size);
static ssize_t vfs_littlefs_pwrite(void *ctx, int fd, const void *src, size_t size, off_t offset);
static ssize_t vfs_littlefs_pread(void *ctx, int fd, void *dst, size_t size, off_t offset);
static int vfs_littlefs_close(void* ctx, int fd);
static off_t vfs_littlefs_lseek(void* ctx, int fd, off_t offset, int mode);
static int vfs_littlefs_fsync(void* ctx, int fd);
static esp_vfs_t vfs_littlefs_create_struct(bool writeable);
#ifdef CONFIG_VFS_SUPPORT_DIR
static int vfs_littlefs_stat(void* ctx, const char * path, struct stat * st);
static int vfs_littlefs_unlink(void* ctx, const char *path);
static int vfs_littlefs_rename(void* ctx, const char *src, const char *dst);
static DIR* vfs_littlefs_opendir(void* ctx, const char* name);
static int vfs_littlefs_closedir(void* ctx, DIR* pdir);
static struct dirent* vfs_littlefs_readdir(void* ctx, DIR* pdir);
static int vfs_littlefs_readdir_r(void* ctx, DIR* pdir,
struct dirent* entry, struct dirent** out_dirent);
static long vfs_littlefs_telldir(void* ctx, DIR* pdir);
static void vfs_littlefs_seekdir(void* ctx, DIR* pdir, long offset);
static int vfs_littlefs_mkdir(void* ctx, const char* name, mode_t mode);
static int vfs_littlefs_rmdir(void* ctx, const char* name);
static ssize_t vfs_littlefs_truncate( void *ctx, const char *path, off_t size);
#ifdef ESP_LITTLEFS_ENABLE_FTRUNCATE
static int vfs_littlefs_ftruncate(void *ctx, int fd, off_t size);
#endif
static void esp_littlefs_dir_free(vfs_littlefs_dir_t *dir);/* ... */
#endif
static void esp_littlefs_take_efs_lock(void);
static esp_err_t esp_littlefs_init_efs(esp_littlefs_t** efs, const esp_partition_t* partition, bool read_only);
static esp_err_t esp_littlefs_init(const esp_vfs_littlefs_conf_t* conf);
static esp_err_t esp_littlefs_by_label(const char* label, int * index);
static esp_err_t esp_littlefs_by_partition(const esp_partition_t* part, int*index);
#ifdef CONFIG_LITTLEFS_SDMMC_SUPPORT
static esp_err_t esp_littlefs_by_sdmmc_handle(sdmmc_card_t *handle, int *index);
#endif
static esp_err_t esp_littlefs_get_empty(int *index);
static void esp_littlefs_free(esp_littlefs_t ** efs);
static int esp_littlefs_flags_conv(int m);
#if CONFIG_LITTLEFS_USE_MTIME
static int vfs_littlefs_utime(void *ctx, const char *path, const struct utimbuf *times);
static void vfs_littlefs_update_mtime(esp_littlefs_t *efs, const char *path);
static int vfs_littlefs_update_mtime_value(esp_littlefs_t *efs, const char *path, time_t t);
static time_t vfs_littlefs_get_mtime(esp_littlefs_t *efs, const char *path);/* ... */
#endif
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
/* ... */
static int vfs_littlefs_fstat(void* ctx, int fd, struct stat * st);/* ... */
#endif
#if CONFIG_LITTLEFS_SPIFFS_COMPAT
static void mkdirs(esp_littlefs_t * efs, const char *dir);
static void rmdirs(esp_littlefs_t * efs, const char *dir);/* ... */
#endif
static int vfs_littlefs_fcntl(void* ctx, int fd, int cmd, int arg);
static int sem_take(esp_littlefs_t *efs);
static int sem_give(esp_littlefs_t *efs);
static esp_err_t format_from_efs(esp_littlefs_t *efs);
static void get_total_and_used_bytes(esp_littlefs_t *efs, size_t *total_bytes, size_t *used_bytes);
static SemaphoreHandle_t _efs_lock = NULL;
static esp_littlefs_t * _efs[CONFIG_LITTLEFS_MAX_PARTITIONS] = { 0 };
/* ... */
#if CONFIG_LITTLEFS_HUMAN_READABLE
/* ... */
static const char * esp_littlefs_errno(enum lfs_error lfs_errno);/* ... */
#endif
static inline void * esp_littlefs_calloc(size_t __nmemb, size_t __size) {
#if defined(CONFIG_LITTLEFS_MALLOC_STRATEGY_INTERNAL)
return heap_caps_calloc(__nmemb, __size, MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
#elif defined(CONFIG_LITTLEFS_MALLOC_STRATEGY_SPIRAM)
return heap_caps_calloc(__nmemb, __size, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
#else
return calloc(__nmemb, __size);
#endif
}{ ... }
static void esp_littlefs_free_fds(esp_littlefs_t * efs) {
while (efs->file) {
vfs_littlefs_file_t * next = efs->file->next;
free(efs->file);
efs->file = next;
}{...}
free(efs->cache);
efs->cache = 0;
efs->cache_size = efs->fd_count = 0;
}{ ... }
static int lfs_errno_remap(enum lfs_error err) {
switch(err){
case LFS_ERR_OK: return 0;
case LFS_ERR_IO: return EIO;
case LFS_ERR_CORRUPT: return EBADMSG;
case LFS_ERR_NOENT: return ENOENT;
case LFS_ERR_EXIST: return EEXIST;
case LFS_ERR_NOTDIR: return ENOTDIR;
case LFS_ERR_ISDIR: return EISDIR;
case LFS_ERR_NOTEMPTY: return ENOTEMPTY;
case LFS_ERR_BADF: return EBADF;
case LFS_ERR_FBIG: return EFBIG;
case LFS_ERR_INVAL: return EINVAL;
case LFS_ERR_NOSPC: return ENOSPC;
case LFS_ERR_NOMEM: return ENOMEM;
case LFS_ERR_NOATTR: return ENODATA;
case LFS_ERR_NAMETOOLONG: return ENAMETOOLONG;
}{...}
return EINVAL;
}{ ... }
esp_err_t format_from_efs(esp_littlefs_t *efs)
{
assert( efs );
bool was_mounted = false;
if(efs->cache_size > 0){
int res;
ESP_LOGV(ESP_LITTLEFS_TAG, "Partition was mounted. Unmounting...");
was_mounted = true;
res = lfs_unmount(efs->fs);
if(res != LFS_ERR_OK){
ESP_LOGE(ESP_LITTLEFS_TAG, "Failed to unmount.");
return ESP_FAIL;
}{...}
esp_littlefs_free_fds(efs);
}{...}
#ifdef CONFIG_LITTLEFS_SDMMC_SUPPORT
if (efs->sdcard) {
esp_err_t ret = sdmmc_full_erase(efs->sdcard);
if (ret != ESP_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Failed to format SD card: 0x%x %s", ret, esp_err_to_name(ret));
return ret;
}{...}
ESP_LOGI(ESP_LITTLEFS_TAG, "SD card formatted!");
}{...}
#endif/* ... */
{
esp_err_t res = ESP_OK;
ESP_LOGV(ESP_LITTLEFS_TAG, "Formatting filesystem");
#ifdef CONFIG_LITTLEFS_SDMMC_SUPPORT
if (efs->sdcard) {
res = lfs_format(efs->fs, &efs->cfg);
}{...} else
#endif
{
efs->cfg.block_count = efs->partition->size / efs->cfg.block_size;
res = lfs_format(efs->fs, &efs->cfg);
efs->cfg.block_count = 0;
}{...}
if( res != LFS_ERR_OK ) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Failed to format filesystem");
return ESP_FAIL;
}{...}
}{...}
if( was_mounted ) {
int res;
ESP_LOGV(ESP_LITTLEFS_TAG, "Remounting formatted partition");
res = lfs_mount(efs->fs, &efs->cfg);
if( res != LFS_ERR_OK ) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Failed to re-mount filesystem");
return ESP_FAIL;
}{...}
efs->cache_size = CONFIG_LITTLEFS_FD_CACHE_MIN_SIZE;
efs->cache = esp_littlefs_calloc(sizeof(*efs->cache), efs->cache_size);
}{...}
ESP_LOGV(ESP_LITTLEFS_TAG, "Format Success!");
return ESP_OK;
}{ ... }
void get_total_and_used_bytes(esp_littlefs_t *efs, size_t *total_bytes, size_t *used_bytes) {
sem_take(efs);
size_t total_bytes_local = efs->cfg.block_size * efs->fs->block_count;
if(total_bytes) *total_bytes = total_bytes_local;
/* ... */
if(used_bytes) *used_bytes = MIN(total_bytes_local, efs->cfg.block_size * lfs_fs_size(efs->fs));
sem_give(efs);
}{ ... }
/* ... */
bool esp_littlefs_mounted(const char* partition_label) {
int index;
esp_err_t err;
err = esp_littlefs_by_label(partition_label, &index);
if(err != ESP_OK) return false;
return _efs[index]->cache_size > 0;
}{ ... }
bool esp_littlefs_partition_mounted(const esp_partition_t* partition) {
int index;
esp_err_t err = esp_littlefs_by_partition(partition, &index);
if(err != ESP_OK) return false;
return _efs[index]->cache_size > 0;
}{ ... }
#ifdef CONFIG_LITTLEFS_SDMMC_SUPPORT
bool esp_littlefs_sdmmc_mounted(sdmmc_card_t *sdcard)
{
int index;
esp_err_t err = esp_littlefs_by_sdmmc_handle(sdcard, &index);
if(err != ESP_OK) return false;
return _efs[index]->cache_size > 0;
}{...}
/* ... */#endif
esp_err_t esp_littlefs_info(const char* partition_label, size_t *total_bytes, size_t *used_bytes){
int index;
esp_err_t err;
err = esp_littlefs_by_label(partition_label, &index);
if(err != ESP_OK) return err;
get_total_and_used_bytes(_efs[index], total_bytes, used_bytes);
return ESP_OK;
}{ ... }
esp_err_t esp_littlefs_partition_info(const esp_partition_t* partition, size_t *total_bytes, size_t *used_bytes){
int index;
esp_err_t err;
err = esp_littlefs_by_partition(partition, &index);
if(err != ESP_OK) return err;
get_total_and_used_bytes(_efs[index], total_bytes, used_bytes);
return ESP_OK;
}{ ... }
#ifdef CONFIG_LITTLEFS_SDMMC_SUPPORT
esp_err_t esp_littlefs_sdmmc_info(sdmmc_card_t *sdcard, size_t *total_bytes, size_t *used_bytes)
{
int index;
esp_err_t err;
err = esp_littlefs_by_sdmmc_handle(sdcard, &index);
if(err != ESP_OK) return err;
get_total_and_used_bytes(_efs[index], total_bytes, used_bytes);
return ESP_OK;
}{...}
/* ... */#endif
esp_err_t esp_vfs_littlefs_register(const esp_vfs_littlefs_conf_t * conf)
{
assert(conf->base_path);
const esp_vfs_t vfs = vfs_littlefs_create_struct(!conf->read_only);
esp_err_t err = esp_littlefs_init(conf);
if (err != ESP_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Failed to initialize LittleFS");
return err;
}{...}
int index;
if(conf->partition_label) {
if (esp_littlefs_by_label(conf->partition_label, &index) != ESP_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Unable to find partition \"%s\"", conf->partition_label);
return ESP_ERR_NOT_FOUND;
}{...}
}{...}
#ifdef CONFIG_LITTLEFS_SDMMC_SUPPORT
else if (conf->sdcard) {
if (esp_littlefs_by_sdmmc_handle(conf->sdcard, &index) != ESP_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Unable to find SD card \"%p\"", conf->sdcard);
return ESP_ERR_NOT_FOUND;
}{...}
}{...}
#endif/* ... */
else {
if (esp_littlefs_by_partition(conf->partition, &index) != ESP_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Unable to find partition \"0x%08"PRIX32"\"", conf->partition->address);
return ESP_ERR_NOT_FOUND;
}{...}
}{...}
strlcat(_efs[index]->base_path, conf->base_path, ESP_VFS_PATH_MAX + 1);
err = esp_vfs_register(conf->base_path, &vfs, _efs[index]);
if (err != ESP_OK) {
esp_littlefs_free(&_efs[index]);
ESP_LOGE(ESP_LITTLEFS_TAG, "Failed to register Littlefs to \"%s\"", conf->base_path);
return err;
}{...}
ESP_LOGV(ESP_LITTLEFS_TAG, "Successfully registered LittleFS to \"%s\"", conf->base_path);
return ESP_OK;
}{ ... }
esp_err_t esp_vfs_littlefs_unregister(const char* partition_label)
{
assert(partition_label);
int index;
if (esp_littlefs_by_label(partition_label, &index) != ESP_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Partition was never registered.");
return ESP_ERR_INVALID_STATE;
}{...}
ESP_LOGV(ESP_LITTLEFS_TAG, "Unregistering \"%s\"", partition_label);
esp_err_t err = esp_vfs_unregister(_efs[index]->base_path);
if (err != ESP_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Failed to unregister \"%s\"", partition_label);
return err;
}{...}
esp_littlefs_free(&_efs[index]);
_efs[index] = NULL;
return ESP_OK;
}{ ... }
#ifdef CONFIG_LITTLEFS_SDMMC_SUPPORT
esp_err_t esp_vfs_littlefs_unregister_sdmmc(sdmmc_card_t *sdcard)
{
assert(sdcard);
int index;
if (esp_littlefs_by_sdmmc_handle(sdcard, &index) != ESP_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Partition was never registered.");
return ESP_ERR_INVALID_STATE;
}{...}
ESP_LOGV(ESP_LITTLEFS_TAG, "Unregistering SD card \"%p\"", sdcard);
esp_err_t err = esp_vfs_unregister(_efs[index]->base_path);
if (err != ESP_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Failed to unregister SD card \"%p\"", sdcard);
return err;
}{...}
esp_littlefs_free(&_efs[index]);
_efs[index] = NULL;
return ESP_OK;
}{...}
/* ... */#endif
esp_err_t esp_vfs_littlefs_unregister_partition(const esp_partition_t* partition) {
assert(partition);
int index;
if (esp_littlefs_by_partition(partition, &index) != ESP_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Partition was never registered.");
return ESP_ERR_INVALID_STATE;
}{...}
ESP_LOGV(ESP_LITTLEFS_TAG, "Unregistering \"0x%08"PRIX32"\"", partition->address);
esp_err_t err = esp_vfs_unregister(_efs[index]->base_path);
if (err != ESP_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Failed to unregister \"0x%08"PRIX32"\"", partition->address);
return err;
}{...}
esp_littlefs_free(&_efs[index]);
_efs[index] = NULL;
return ESP_OK;
}{ ... }
esp_err_t esp_littlefs_format(const char* partition_label) {
assert( partition_label );
bool efs_free = false;
int index = -1;
esp_err_t err;
ESP_LOGV(ESP_LITTLEFS_TAG, "Formatting \"%s\"", partition_label);
err = esp_littlefs_by_label(partition_label, &index);
if( err != ESP_OK ){
ESP_LOGV(ESP_LITTLEFS_TAG, "Temporarily creating EFS context.");
efs_free = true;
const esp_vfs_littlefs_conf_t conf = {
.dont_mount = true,
.partition_label = partition_label,
}{...};
err = esp_littlefs_init(&conf);
if( err != ESP_OK ) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Failed to initialize to format.");
goto exit;
}{...}
err = esp_littlefs_by_label(partition_label, &index);
if ( err != ESP_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Error obtaining context.");
goto exit;
}{...}
}{...}
err = format_from_efs(_efs[index]);
exit:
if(efs_free && index>=0) esp_littlefs_free(&_efs[index]);
return err;
}{ ... }
esp_err_t esp_littlefs_format_partition(const esp_partition_t* partition) {
assert( partition );
bool efs_free = false;
int index = -1;
esp_err_t err;
ESP_LOGV(ESP_LITTLEFS_TAG, "Formatting partition at \"0x%08"PRIX32"\"", partition->address);
err = esp_littlefs_by_partition(partition, &index);
if( err != ESP_OK ){
ESP_LOGV(ESP_LITTLEFS_TAG, "Temporarily creating EFS context.");
efs_free = true;
const esp_vfs_littlefs_conf_t conf = {
.dont_mount = true,
.partition_label = NULL,
.partition = partition,
}{...};
err = esp_littlefs_init(&conf);
if( err != ESP_OK ) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Failed to initialize to format.");
goto exit;
}{...}
err = esp_littlefs_by_partition(partition, &index);
if ( err != ESP_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Error obtaining context.");
goto exit;
}{...}
}{...}
err = format_from_efs(_efs[index]);
exit:
if(efs_free && index>=0) esp_littlefs_free(&_efs[index]);
return err;
}{ ... }
#ifdef CONFIG_LITTLEFS_SDMMC_SUPPORT
esp_err_t esp_littlefs_format_sdmmc(sdmmc_card_t *sdcard)
{
assert(sdcard);
bool efs_free = false;
int index = -1;
esp_err_t err;
ESP_LOGV(ESP_LITTLEFS_TAG, "Formatting sdcard %p", sdcard);
err = esp_littlefs_by_sdmmc_handle(sdcard, &index);
if( err != ESP_OK ){
ESP_LOGV(ESP_LITTLEFS_TAG, "Temporarily creating EFS context.");
efs_free = true;
const esp_vfs_littlefs_conf_t conf = {
.dont_mount = true,
.partition_label = NULL,
.partition = NULL,
.sdcard = sdcard,
}{...};
err = esp_littlefs_init(&conf);
if( err != ESP_OK ) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Failed to initialize to format.");
goto exit;
}{...}
err = esp_littlefs_by_sdmmc_handle(sdcard, &index);
if ( err != ESP_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Error obtaining context.");
goto exit;
}{...}
}{...}
err = format_from_efs(_efs[index]);
exit:
if(efs_free && index>=0) esp_littlefs_free(&_efs[index]);
return err;
}{...}
/* ... */#endif
/* ... */
#if CONFIG_LITTLEFS_HUMAN_READABLE
/* ... */
static const char * esp_littlefs_errno(enum lfs_error lfs_errno) {
switch(lfs_errno){
case LFS_ERR_OK: return "LFS_ERR_OK";
case LFS_ERR_IO: return "LFS_ERR_IO";
case LFS_ERR_CORRUPT: return "LFS_ERR_CORRUPT";
case LFS_ERR_NOENT: return "LFS_ERR_NOENT";
case LFS_ERR_EXIST: return "LFS_ERR_EXIST";
case LFS_ERR_NOTDIR: return "LFS_ERR_NOTDIR";
case LFS_ERR_ISDIR: return "LFS_ERR_ISDIR";
case LFS_ERR_NOTEMPTY: return "LFS_ERR_NOTEMPTY";
case LFS_ERR_BADF: return "LFS_ERR_BADF";
case LFS_ERR_FBIG: return "LFS_ERR_FBIG";
case LFS_ERR_INVAL: return "LFS_ERR_INVAL";
case LFS_ERR_NOSPC: return "LFS_ERR_NOSPC";
case LFS_ERR_NOMEM: return "LFS_ERR_NOMEM";
case LFS_ERR_NOATTR: return "LFS_ERR_NOATTR";
case LFS_ERR_NAMETOOLONG: return "LFS_ERR_NAMETOOLONG";
default: return "LFS_ERR_UNDEFINED";
}{...}
return "";
}{...}
/* ... */#else
#define esp_littlefs_errno(x) ""
#endif
static esp_vfs_t vfs_littlefs_create_struct(bool writeable) {
esp_vfs_t vfs = {
.flags = ESP_VFS_FLAG_CONTEXT_PTR,
.write_p = &vfs_littlefs_write,
.pwrite_p = &vfs_littlefs_pwrite,
.lseek_p = &vfs_littlefs_lseek,
.read_p = &vfs_littlefs_read,
.pread_p = &vfs_littlefs_pread,
.open_p = &vfs_littlefs_open,
.close_p = &vfs_littlefs_close,
.fsync_p = &vfs_littlefs_fsync,
.fcntl_p = &vfs_littlefs_fcntl,
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
.fstat_p = &vfs_littlefs_fstat,
#endif
#ifdef CONFIG_VFS_SUPPORT_DIR
.stat_p = &vfs_littlefs_stat,
.link_p = NULL,
.unlink_p = &vfs_littlefs_unlink,
.rename_p = &vfs_littlefs_rename,
.opendir_p = &vfs_littlefs_opendir,
.readdir_p = &vfs_littlefs_readdir,
.readdir_r_p = &vfs_littlefs_readdir_r,
.telldir_p = &vfs_littlefs_telldir,
.seekdir_p = &vfs_littlefs_seekdir,
.closedir_p = &vfs_littlefs_closedir,
.mkdir_p = &vfs_littlefs_mkdir,
.rmdir_p = &vfs_littlefs_rmdir,
.truncate_p = &vfs_littlefs_truncate,
#ifdef ESP_LITTLEFS_ENABLE_FTRUNCATE
.ftruncate_p = &vfs_littlefs_ftruncate,
#endif
#if CONFIG_LITTLEFS_USE_MTIME
.utime_p = &vfs_littlefs_utime,
#endif /* ... */
#endif
}{...};
if(!writeable) {
vfs.write_p = NULL;
vfs.pwrite_p = NULL;
vfs.fsync_p = NULL;
vfs.link_p = NULL;
vfs.unlink_p = NULL;
vfs.rename_p = NULL;
vfs.mkdir_p = NULL;
vfs.rmdir_p = NULL;
}{...}
return vfs;
}{ ... }
/* ... */
static void esp_littlefs_free(esp_littlefs_t ** efs)
{
esp_littlefs_t * e = *efs;
if (e == NULL) return;
*efs = NULL;
if (e->fs) {
if(e->cache_size > 0) lfs_unmount(e->fs);
free(e->fs);
}{...}
if(e->lock) vSemaphoreDelete(e->lock);
esp_littlefs_free_fds(e);
free(e);
}{ ... }
#ifdef CONFIG_VFS_SUPPORT_DIR
/* ... */
static void esp_littlefs_dir_free(vfs_littlefs_dir_t *dir){
if(dir == NULL) return;
if(dir->path) free(dir->path);
free(dir);
}{ ... }
#endif/* ... */
/* ... */
static esp_err_t esp_littlefs_by_partition(const esp_partition_t* part, int * index){
int i;
esp_littlefs_t * p;
if(!part || !index) return ESP_ERR_INVALID_ARG;
ESP_LOGV(ESP_LITTLEFS_TAG, "Searching for existing filesystem for partition \"0x%08"PRIX32"\"", part->address);
for (i = 0; i < CONFIG_LITTLEFS_MAX_PARTITIONS; i++) {
p = _efs[i];
if (!p) continue;
if (!p->partition) continue;
if (part->address == p->partition->address) {
*index = i;
ESP_LOGV(ESP_LITTLEFS_TAG, "Found existing filesystem \"0x%08"PRIX32"\" at index %d", part->address, *index);
return ESP_OK;
}{...}
}{...}
ESP_LOGV(ESP_LITTLEFS_TAG, "Existing filesystem \"0x%08"PRIX32"\" not found", part->address);
return ESP_ERR_NOT_FOUND;
}{ ... }
static esp_err_t esp_littlefs_by_label(const char* label, int * index){
int i;
esp_littlefs_t * p;
if(!label || !index) return ESP_ERR_INVALID_ARG;
ESP_LOGV(ESP_LITTLEFS_TAG, "Searching for existing filesystem for partition \"%s\"", label);
for (i = 0; i < CONFIG_LITTLEFS_MAX_PARTITIONS; i++) {
p = _efs[i];
if (!p) continue;
if (!p->partition) continue;
if (strncmp(label, p->partition->label, 17) == 0) {
*index = i;
ESP_LOGV(ESP_LITTLEFS_TAG, "Found existing filesystem \"%s\" at index %d", label, *index);
return ESP_OK;
}{...}
}{...}
ESP_LOGV(ESP_LITTLEFS_TAG, "Existing filesystem \"%s\" not found", label);
return ESP_ERR_NOT_FOUND;
}{ ... }
#ifdef CONFIG_LITTLEFS_SDMMC_SUPPORT
static esp_err_t esp_littlefs_by_sdmmc_handle(sdmmc_card_t *handle, int *index)
{
if(!handle || !index) return ESP_ERR_INVALID_ARG;
ESP_LOGV(ESP_LITTLEFS_TAG, "Searching for existing filesystem for SD handle %p", handle);
for (int i = 0; i < CONFIG_LITTLEFS_MAX_PARTITIONS; i++) {
esp_littlefs_t *p = _efs[i];
if (!p) continue;
if (!p->sdcard) continue;
if (p->sdcard == handle) {
*index = i;
ESP_LOGV(ESP_LITTLEFS_TAG, "Found existing filesystem %p at index %d", handle, *index);
return ESP_OK;
}{...}
}{...}
ESP_LOGV(ESP_LITTLEFS_TAG, "Existing filesystem %p not found", handle);
return ESP_ERR_NOT_FOUND;
}{...}
/* ... */#endif
/* ... */
static esp_err_t esp_littlefs_get_empty(int *index) {
assert(index);
for(uint8_t i=0; i < CONFIG_LITTLEFS_MAX_PARTITIONS; i++){
if( _efs[i] == NULL ){
*index = i;
return ESP_OK;
}{...}
}{...}
ESP_LOGE(ESP_LITTLEFS_TAG, "No more free partitions available.");
return ESP_FAIL;
}{ ... }
/* ... */
static int esp_littlefs_flags_conv(int m) {
int lfs_flags = 0;
m &= (O_APPEND | O_WRONLY | O_RDWR | O_EXCL | O_CREAT | O_TRUNC);
if (m == O_RDONLY) {ESP_LOGV(ESP_LITTLEFS_TAG, "O_RDONLY"); lfs_flags |= LFS_O_RDONLY;}
if (m & O_APPEND) {ESP_LOGV(ESP_LITTLEFS_TAG, "O_APPEND"); lfs_flags |= LFS_O_APPEND;}
if (m & O_WRONLY) {ESP_LOGV(ESP_LITTLEFS_TAG, "O_WRONLY"); lfs_flags |= LFS_O_WRONLY;}
if (m & O_RDWR) {ESP_LOGV(ESP_LITTLEFS_TAG, "O_RDWR"); lfs_flags |= LFS_O_RDWR;}
if (m & O_EXCL) {ESP_LOGV(ESP_LITTLEFS_TAG, "O_EXCL"); lfs_flags |= LFS_O_EXCL;}
if (m & O_CREAT) {ESP_LOGV(ESP_LITTLEFS_TAG, "O_CREAT"); lfs_flags |= LFS_O_CREAT;}
if (m & O_TRUNC) {ESP_LOGV(ESP_LITTLEFS_TAG, "O_TRUNC"); lfs_flags |= LFS_O_TRUNC;}
return lfs_flags;
}{ ... }
static void esp_littlefs_take_efs_lock(void) {
if( _efs_lock == NULL ){
static portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
portENTER_CRITICAL(&mux);
if( _efs_lock == NULL ){
_efs_lock = xSemaphoreCreateMutex();
assert(_efs_lock);
}{...}
portEXIT_CRITICAL(&mux);
}{...}
xSemaphoreTake(_efs_lock, portMAX_DELAY);
}{ ... }
#ifdef CONFIG_LITTLEFS_SDMMC_SUPPORT
static esp_err_t esp_littlefs_init_sdcard(esp_littlefs_t** efs, sdmmc_card_t* sdcard, bool read_only)
{
*efs = esp_littlefs_calloc(1, sizeof(esp_littlefs_t));
if (*efs == NULL) {
ESP_LOGE(ESP_LITTLEFS_TAG, "esp_littlefs could not be malloced");
return ESP_ERR_NO_MEM;
}{...}
(*efs)->sdcard = sdcard;
{
(*efs)->cfg.context = *efs;
(*efs)->read_only = read_only;
(*efs)->cfg.read = littlefs_sdmmc_read;
(*efs)->cfg.prog = littlefs_sdmmc_write;
(*efs)->cfg.erase = littlefs_sdmmc_erase;
(*efs)->cfg.sync = littlefs_sdmmc_sync;
size_t max_allowed_blk_cnt = 0;
if (((uint64_t)sdcard->csd.capacity * (uint64_t)sdcard->csd.sector_size) > SIZE_MAX) {
max_allowed_blk_cnt = SIZE_MAX / sdcard->csd.sector_size;
ESP_LOGW(ESP_LITTLEFS_TAG, "SD card is too big (sector=%d, count=%d; total=%llu bytes), throttling to maximum possible %u blocks",
sdcard->csd.sector_size, sdcard->csd.capacity,
(((uint64_t)sdcard->csd.capacity * (uint64_t)sdcard->csd.sector_size)), max_allowed_blk_cnt);
}{...} else {
max_allowed_blk_cnt = sdcard->csd.capacity;
}{...}
(*efs)->cfg.read_size = sdcard->csd.sector_size;
(*efs)->cfg.prog_size = sdcard->csd.sector_size;
(*efs)->cfg.block_size = sdcard->csd.sector_size;
(*efs)->cfg.block_count = max_allowed_blk_cnt;
(*efs)->cfg.cache_size = MAX(CONFIG_LITTLEFS_CACHE_SIZE, sdcard->csd.sector_size);
(*efs)->cfg.lookahead_size = CONFIG_LITTLEFS_LOOKAHEAD_SIZE;
(*efs)->cfg.block_cycles = CONFIG_LITTLEFS_BLOCK_CYCLES;
#if CONFIG_LITTLEFS_MULTIVERSION
#if CONFIG_LITTLEFS_DISK_VERSION_MOST_RECENT
(*efs)->cfg.disk_version = 0;
#elif CONFIG_LITTLEFS_DISK_VERSION_2_1
(*efs)->cfg.disk_version = 0x00020001;
#elif CONFIG_LITTLEFS_DISK_VERSION_2_0
(*efs)->cfg.disk_version = 0x00020000;
#else
#error "CONFIG_LITTLEFS_MULTIVERSION enabled but no or unknown disk version selected!"
#endif/* ... */
#endif
}{...}
(*efs)->lock = xSemaphoreCreateRecursiveMutex();
if ((*efs)->lock == NULL) {
ESP_LOGE(ESP_LITTLEFS_TAG, "mutex lock could not be created");
return ESP_ERR_NO_MEM;
}{...}
(*efs)->fs = esp_littlefs_calloc(1, sizeof(lfs_t));
if ((*efs)->fs == NULL) {
ESP_LOGE(ESP_LITTLEFS_TAG, "littlefs could not be malloced");
return ESP_ERR_NO_MEM;
}{...}
return ESP_OK;
}{...}
/* ... */#endif
static esp_err_t esp_littlefs_init_efs(esp_littlefs_t** efs, const esp_partition_t* partition, bool read_only)
{
*efs = esp_littlefs_calloc(1, sizeof(esp_littlefs_t));
if (*efs == NULL) {
ESP_LOGE(ESP_LITTLEFS_TAG, "esp_littlefs could not be malloced");
return ESP_ERR_NO_MEM;
}{...}
(*efs)->partition = partition;
{
(*efs)->cfg.context = *efs;
(*efs)->read_only = read_only;
(*efs)->cfg.read = littlefs_esp_part_read;
(*efs)->cfg.prog = littlefs_esp_part_write;
(*efs)->cfg.erase = littlefs_esp_part_erase;
(*efs)->cfg.sync = littlefs_esp_part_sync;
(*efs)->cfg.read_size = CONFIG_LITTLEFS_READ_SIZE;
(*efs)->cfg.prog_size = CONFIG_LITTLEFS_WRITE_SIZE;
(*efs)->cfg.block_size = CONFIG_LITTLEFS_BLOCK_SIZE;
(*efs)->cfg.block_count = 0;
(*efs)->cfg.cache_size = CONFIG_LITTLEFS_CACHE_SIZE;
(*efs)->cfg.lookahead_size = CONFIG_LITTLEFS_LOOKAHEAD_SIZE;
(*efs)->cfg.block_cycles = CONFIG_LITTLEFS_BLOCK_CYCLES;
#if CONFIG_LITTLEFS_MULTIVERSION
#if CONFIG_LITTLEFS_DISK_VERSION_MOST_RECENT
(*efs)->cfg.disk_version = 0;
#elif CONFIG_LITTLEFS_DISK_VERSION_2_1
(*efs)->cfg.disk_version = 0x00020001;
#elif CONFIG_LITTLEFS_DISK_VERSION_2_0
(*efs)->cfg.disk_version = 0x00020000;
#else
#error "CONFIG_LITTLEFS_MULTIVERSION enabled but no or unknown disk version selected!"
#endif/* ... */
#endif
}{...}
(*efs)->lock = xSemaphoreCreateRecursiveMutex();
if ((*efs)->lock == NULL) {
ESP_LOGE(ESP_LITTLEFS_TAG, "mutex lock could not be created");
return ESP_ERR_NO_MEM;
}{...}
(*efs)->fs = esp_littlefs_calloc(1, sizeof(lfs_t));
if ((*efs)->fs == NULL) {
ESP_LOGE(ESP_LITTLEFS_TAG, "littlefs could not be malloced");
return ESP_ERR_NO_MEM;
}{...}
return ESP_OK;
}{ ... }
/* ... */
static esp_err_t esp_littlefs_init(const esp_vfs_littlefs_conf_t* conf)
{
int index = -1;
esp_err_t err = ESP_FAIL;
const esp_partition_t* partition = NULL;
esp_littlefs_t * efs = NULL;
esp_littlefs_take_efs_lock();
if (esp_littlefs_get_empty(&index) != ESP_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "max mounted partitions reached");
err = ESP_ERR_INVALID_STATE;
goto exit;
}{...}
if(conf->partition_label)
{
if (esp_littlefs_by_label(conf->partition_label, &index) == ESP_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Partition already used");
err = ESP_ERR_INVALID_STATE;
goto exit;
}{...}
partition = esp_partition_find_first(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY,
conf->partition_label);
if (!partition) {
ESP_LOGE(ESP_LITTLEFS_TAG, "partition \"%s\" could not be found", conf->partition_label);
err = ESP_ERR_NOT_FOUND;
goto exit;
}{...}
}{...} else if(conf->partition) {
if (esp_littlefs_by_partition(conf->partition, &index) == ESP_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Partition already used");
err = ESP_ERR_INVALID_STATE;
goto exit;
}{...}
partition = conf->partition;
#ifdef CONFIG_LITTLEFS_SDMMC_SUPPORT
}{...} else if (conf->sdcard) {
ESP_LOGV(ESP_LITTLEFS_TAG, "Using SD card handle %p for LittleFS mount", conf->sdcard);
err = sdmmc_get_status(conf->sdcard);
if (err != ESP_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Failed when checking SD card status: 0x%x", err);
goto exit;
}{...}
#endif
}{...} else {
ESP_LOGE(ESP_LITTLEFS_TAG, "No partition specified in configuration");
err = ESP_ERR_INVALID_ARG;
goto exit;
}{...}
#ifdef CONFIG_LITTLEFS_SDMMC_SUPPORT
if (conf->sdcard) {
err = esp_littlefs_init_sdcard(&efs, conf->sdcard, conf->read_only);
if(err != ESP_OK) {
goto exit;
}{...}
}{...} else
#endif
{
uint32_t flash_page_size = g_rom_flashchip.page_size;
uint32_t log_page_size = CONFIG_LITTLEFS_PAGE_SIZE;
if (log_page_size % flash_page_size != 0) {
ESP_LOGE(ESP_LITTLEFS_TAG, "LITTLEFS_PAGE_SIZE is not multiple of flash chip page size (%u)",
(unsigned int) flash_page_size);
err = ESP_ERR_INVALID_ARG;
goto exit;
}{...}
err = esp_littlefs_init_efs(&efs, partition, conf->read_only);
if(err != ESP_OK) {
goto exit;
}{...}
}{...}
_efs[index] = efs;
if(!conf->dont_mount){
int res;
res = lfs_mount(efs->fs, &efs->cfg);
if (conf->format_if_mount_failed && res != LFS_ERR_OK) {
ESP_LOGW(ESP_LITTLEFS_TAG, "mount failed, %s (%i). formatting...", esp_littlefs_errno(res), res);
#ifdef CONFIG_LITTLEFS_SDMMC_SUPPORT
if (conf->sdcard) {
err = esp_littlefs_format_sdmmc(conf->sdcard);
}{...} else
#endif
{
err = esp_littlefs_format_partition(efs->partition);
}{...}
if(err != ESP_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "format failed");
err = ESP_FAIL;
goto exit;
}{...}
res = lfs_mount(efs->fs, &efs->cfg);
}{...}
if (res != LFS_ERR_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "mount failed, %s (%i)", esp_littlefs_errno(res), res);
err = ESP_FAIL;
goto exit;
}{...}
efs->cache_size = 4;
efs->cache = esp_littlefs_calloc(sizeof(*efs->cache), efs->cache_size);
if(conf->grow_on_mount){
#ifdef CONFIG_LITTLEFS_SDMMC_SUPPORT
if (efs->sdcard) {
res = lfs_fs_grow(efs->fs, efs->sdcard->csd.capacity);
}{...} else
#endif
{
res = lfs_fs_grow(efs->fs, efs->partition->size / efs->cfg.block_size);
}{...}
if (res != LFS_ERR_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "FS grow failed, %s (%i)", esp_littlefs_errno(res), res);
err = ESP_FAIL;
goto exit;
}{...}
}{...}
}{...}
err = ESP_OK;
exit:
if(err != ESP_OK){
if( index >= 0 ) {
esp_littlefs_free(&_efs[index]);
}{...}
else{
esp_littlefs_free(&efs);
}{...}
}{...}
xSemaphoreGive(_efs_lock);
return err;
}{ ... }
/* ... */
static inline int sem_take(esp_littlefs_t *efs) {
int res;
#if LOG_LOCAL_LEVEL >= 5
ESP_LOGV(ESP_LITTLEFS_TAG, "------------------------ Sem Taking [%s]", pcTaskGetName(NULL));
#endif
res = xSemaphoreTakeRecursive(efs->lock, portMAX_DELAY);
#if LOG_LOCAL_LEVEL >= 5
ESP_LOGV(ESP_LITTLEFS_TAG, "--------------------->>> Sem Taken [%s]", pcTaskGetName(NULL));
#endif
return res;
}{ ... }
/* ... */
static inline int sem_give(esp_littlefs_t *efs) {
#if LOG_LOCAL_LEVEL >= 5
ESP_LOGV(ESP_LITTLEFS_TAG, "---------------------<<< Sem Give [%s]", pcTaskGetName(NULL));
#endif
return xSemaphoreGiveRecursive(efs->lock);
}{ ... }
/* ... */
/* ... */
static int esp_littlefs_allocate_fd(esp_littlefs_t *efs, vfs_littlefs_file_t ** file
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
, const size_t path_len
#endif
)
{
int i = -1;
assert( efs->fd_count < UINT16_MAX );
assert( efs->cache_size < UINT16_MAX );
if (efs->fd_count + 1 > efs->cache_size) {
uint16_t new_size = (uint16_t)MIN(UINT16_MAX, CONFIG_LITTLEFS_FD_CACHE_REALLOC_FACTOR * efs->cache_size);
vfs_littlefs_file_t ** new_cache = realloc(efs->cache, new_size * sizeof(*efs->cache));
if (!new_cache) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Unable to allocate file cache");
return -1;
}{...}
memset(&new_cache[efs->cache_size], 0, (new_size - efs->cache_size) * sizeof(*efs->cache));
efs->cache = new_cache;
efs->cache_size = new_size;
}{...}
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
*file = esp_littlefs_calloc(1, sizeof(**file) + path_len);
#else
*file = esp_littlefs_calloc(1, sizeof(**file));
#endif
if (*file == NULL) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Unable to allocate FD");
return -1;
}{...}
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
/* ... */
(*file)->path = (char*)(*file) + sizeof(**file);/* ... */
#endif
for(i=0; i < efs->cache_size; i++) {
if (efs->cache[i] == NULL) {
efs->cache[i] = *file;
break;
}{...}
}{...}
(*file)->next = efs->file;
efs->file = *file;
efs->fd_count++;
return i;
}{ ... }
/* ... */
static int esp_littlefs_free_fd(esp_littlefs_t *efs, int fd){
vfs_littlefs_file_t * file, * head;
if((uint32_t)fd >= efs->cache_size) {
ESP_LOGE(ESP_LITTLEFS_TAG, "FD %d must be <%d.", fd, efs->cache_size);
return -1;
}{...}
file = efs->cache[fd];
head = efs->file;
if (file == head) {
efs->file = efs->file->next;
}{...} else {
while (head && head->next != file) {
head = head->next;
}{...}
if (!head) {
ESP_LOGE(ESP_LITTLEFS_TAG, "Inconsistent list");
return -1;
}{...}
head->next = file->next;
}{...}
efs->cache[fd] = NULL;
efs->fd_count--;
ESP_LOGV(ESP_LITTLEFS_TAG, "Clearing FD");
free(file);
#if 0
/* ... */
if(efs->cache_size > CONFIG_LITTLEFS_FD_CACHE_MIN_SIZE) {
uint16_t n_free;
uint16_t new_size = efs->cache_size / CONFIG_LITTLEFS_FD_CACHE_REALLOC_FACTOR;
if(new_size >= CONFIG_LITTLEFS_FD_CACHE_MIN_SIZE) {
for(n_free=0; n_free < efs->cache_size; n_free++) {
if(efs->cache[efs->cache_size - n_free - 1] != NULL) {
break;
}{...}
}{...}
if(n_free >= (efs->cache_size - new_size)){
new_size += CONFIG_LITTLEFS_FD_CACHE_HYST;
ESP_LOGV(ESP_LITTLEFS_TAG, "Reallocating cache %i -> %i", efs->cache_size, new_size);
vfs_littlefs_file_t ** new_cache;
new_cache = realloc(efs->cache, new_size * sizeof(*efs->cache));
if(new_cache) {
efs->cache = new_cache;
efs->cache_size = new_size;
}{...}
}{...}
}{...}
}{...}
#endif/* ... */
return 0;
}{ ... }
/* ... */
static uint32_t compute_hash(const char * path) {
uint32_t hash = 5381;
char c;
while ((c = *path++))
hash = ((hash << 5) + hash) + c;
return hash;
}{ ... }
#ifdef CONFIG_VFS_SUPPORT_DIR
/* ... */
static int esp_littlefs_get_fd_by_name(esp_littlefs_t *efs, const char *path){
uint32_t hash = compute_hash(path);
for(uint16_t i=0, j=0; i < efs->cache_size && j < efs->fd_count; i++){
if (efs->cache[i]) {
++j;
if (
efs->cache[i]->hash == hash
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
&& strcmp(path, efs->cache[i]->path) == 0
#endif
) {
ESP_LOGV(ESP_LITTLEFS_TAG, "Found \"%s\" at FD %d.", path, i);
return i;
}{...}
}{...}
}{...}
ESP_LOGV(ESP_LITTLEFS_TAG, "Unable to get a find FD for \"%s\"", path);
return -1;
}{ ... }
#endif/* ... */
static int vfs_littlefs_open(void* ctx, const char * path, int flags, int mode) {
int fd=-1, lfs_flags, res;
esp_littlefs_t *efs = (esp_littlefs_t *)ctx;
vfs_littlefs_file_t *file = NULL;
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
size_t path_len = strlen(path) + 1;
#endif
#if CONFIG_LITTLEFS_OPEN_DIR
struct lfs_info info;
#endif
assert(path);
ESP_LOGV(ESP_LITTLEFS_TAG, "Opening %s", path);
lfs_flags = esp_littlefs_flags_conv(flags);
if(efs->read_only && lfs_flags != LFS_O_RDONLY) {
return LFS_ERR_INVAL;
}{...}
sem_take(efs);
#if CONFIG_LITTLEFS_OPEN_DIR
if (flags & O_DIRECTORY) {
res = lfs_stat(efs->fs, path, &info);
if (res == LFS_ERR_OK) {
if (info.type == LFS_TYPE_REG) {
sem_give(efs);
ESP_LOGV(ESP_LITTLEFS_TAG, "Open directory but it is a file");
errno = ENOTDIR;
return LFS_ERR_INVAL;
}{...}
}{...}
}{...}
#endif/* ... */
fd = esp_littlefs_allocate_fd(efs, &file
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
, path_len
#endif
);
if(fd < 0) {
errno = lfs_errno_remap(fd);
sem_give(efs);
ESP_LOGV(ESP_LITTLEFS_TAG, "Error obtaining FD");
return LFS_ERR_INVAL;
}{...}
#if CONFIG_LITTLEFS_SPIFFS_COMPAT
ESP_LOGV(ESP_LITTLEFS_TAG, "LITTLEFS_SPIFFS_COMPAT attempting to create all directories for %s", path);
mkdirs(efs, path);/* ... */
#endif
#ifndef CONFIG_LITTLEFS_MALLOC_STRATEGY_DISABLE
res = lfs_file_open(efs->fs, &file->file, path, lfs_flags);/* ... */
#else
#error "The use of static buffers is not currently supported by this VFS wrapper"
#endif
#if CONFIG_LITTLEFS_OPEN_DIR
if ( flags & O_DIRECTORY && res == LFS_ERR_ISDIR) {
res = LFS_ERR_OK;
file->file.flags = flags;
}{...}
#endif/* ... */
if( res < 0 ) {
errno = lfs_errno_remap(res);
esp_littlefs_free_fd(efs, fd);
sem_give(efs);
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to open file %s. Error %s (%d)",
path, esp_littlefs_errno(res), res);/* ... */
#else
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to open file. Error %s (%d)",
esp_littlefs_errno(res), res);/* ... */
#endif
return LFS_ERR_INVAL;
}{...}
/* ... */
#if CONFIG_LITTLEFS_OPEN_DIR
if ( (flags & O_DIRECTORY) == 0 ) {
#endif
if(!efs->read_only)
{
res = lfs_file_sync(efs->fs, &file->file);
}{...}
if(res < 0){
errno = lfs_errno_remap(res);
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to sync at opening file \"%s\". Error %s (%d)",
file->path, esp_littlefs_errno(res), res);/* ... */
#else
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to sync at opening file %d. Error %d", fd, res);
#endif
}{...}
#if CONFIG_LITTLEFS_OPEN_DIR
}{...}
#endif
file->hash = compute_hash(path);
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
memcpy(file->path, path, path_len);
#endif
#if CONFIG_LITTLEFS_USE_MTIME
if (lfs_flags != LFS_O_RDONLY) {
vfs_littlefs_update_mtime(efs, path);
}{...}
#endif/* ... */
sem_give(efs);
ESP_LOGV(ESP_LITTLEFS_TAG, "Done opening %s", path);
return fd;
}{ ... }
static ssize_t vfs_littlefs_write(void* ctx, int fd, const void * data, size_t size) {
esp_littlefs_t * efs = (esp_littlefs_t *)ctx;
ssize_t res;
vfs_littlefs_file_t *file = NULL;
sem_take(efs);
if((uint32_t)fd > efs->cache_size) {
sem_give(efs);
ESP_LOGE(ESP_LITTLEFS_TAG, "FD %d must be <%d.", fd, efs->cache_size);
errno = EBADF;
return -1;
}{...}
file = efs->cache[fd];
res = lfs_file_write(efs->fs, &file->file, data, size);
#ifdef CONFIG_LITTLEFS_FLUSH_FILE_EVERY_WRITE
if(res > 0) {
vfs_littlefs_fsync(ctx, fd);
}{...}
#endif/* ... */
sem_give(efs);
if(res < 0){
errno = lfs_errno_remap(res);
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to write FD %d; path \"%s\". Error %s (%d)",
fd, file->path, esp_littlefs_errno(res), res);/* ... */
#else
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to write FD %d. Error %s (%d)",
fd, esp_littlefs_errno(res), res);/* ... */
#endif
return -1;
}{...}
return res;
}{ ... }
static ssize_t vfs_littlefs_read(void* ctx, int fd, void * dst, size_t size) {
esp_littlefs_t * efs = (esp_littlefs_t *)ctx;
ssize_t res;
vfs_littlefs_file_t *file = NULL;
sem_take(efs);
if((uint32_t)fd > efs->cache_size) {
sem_give(efs);
ESP_LOGE(ESP_LITTLEFS_TAG, "FD %d must be <%d.", fd, efs->cache_size);
errno = EBADF;
return -1;
}{...}
file = efs->cache[fd];
res = lfs_file_read(efs->fs, &file->file, dst, size);
sem_give(efs);
if(res < 0){
errno = lfs_errno_remap(res);
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to read file \"%s\". Error %s (%d)",
file->path, esp_littlefs_errno(res), res);/* ... */
#else
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to read FD %d. Error %s (%d)",
fd, esp_littlefs_errno(res), res);/* ... */
#endif
return -1;
}{...}
return res;
}{ ... }
static ssize_t vfs_littlefs_pwrite(void *ctx, int fd, const void *src, size_t size, off_t offset)
{
esp_littlefs_t *efs = (esp_littlefs_t *)ctx;
ssize_t res, save_res;
vfs_littlefs_file_t *file = NULL;
sem_take(efs);
if ((uint32_t)fd > efs->cache_size)
{
sem_give(efs);
ESP_LOGE(ESP_LITTLEFS_TAG, "FD %d must be <%d.", fd, efs->cache_size);
errno = EBADF;
return -1;
}{...}
file = efs->cache[fd];
off_t old_offset = lfs_file_seek(efs->fs, &file->file, 0, SEEK_CUR);
if (old_offset < (off_t)0)
{
res = old_offset;
goto exit;
}{...}
res = lfs_file_seek(efs->fs, &file->file, offset, SEEK_SET);
if (res < (off_t)0)
goto exit;
res = lfs_file_write(efs->fs, &file->file, src, size);
/* ... */
save_res = lfs_file_seek(efs->fs, &file->file, old_offset, SEEK_SET);
if (res >= (ssize_t)0 && save_res < (off_t)0)
{
res = save_res;
}{...}
sem_give(efs);
exit:
if (res < 0)
{
errno = lfs_errno_remap(res);
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to write FD %d; path \"%s\". Error %s (%d)",
fd, file->path, esp_littlefs_errno(res), res);/* ... */
#else
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to write FD %d. Error %s (%d)",
fd, esp_littlefs_errno(res), res);/* ... */
#endif
return -1;
}{...}
return res;
}{ ... }
static ssize_t vfs_littlefs_pread(void *ctx, int fd, void *dst, size_t size, off_t offset)
{
esp_littlefs_t *efs = (esp_littlefs_t *)ctx;
ssize_t res, save_res;
vfs_littlefs_file_t *file = NULL;
sem_take(efs);
if ((uint32_t)fd > efs->cache_size)
{
sem_give(efs);
ESP_LOGE(ESP_LITTLEFS_TAG, "FD %d must be <%d.", fd, efs->cache_size);
errno = EBADF;
return -1;
}{...}
file = efs->cache[fd];
off_t old_offset = lfs_file_seek(efs->fs, &file->file, 0, SEEK_CUR);
if (old_offset < (off_t)0)
{
res = old_offset;
goto exit;
}{...}
res = lfs_file_seek(efs->fs, &file->file, offset, SEEK_SET);
if (res < (off_t)0)
goto exit;
res = lfs_file_read(efs->fs, &file->file, dst, size);
/* ... */
save_res = lfs_file_seek(efs->fs, &file->file, old_offset, SEEK_SET);
if (res >= (ssize_t)0 && save_res < (off_t)0)
{
res = save_res;
}{...}
sem_give(efs);
exit:
if (res < 0)
{
errno = lfs_errno_remap(res);
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to read file \"%s\". Error %s (%d)",
file->path, esp_littlefs_errno(res), res);/* ... */
#else
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to read FD %d. Error %s (%d)",
fd, esp_littlefs_errno(res), res);/* ... */
#endif
return -1;
}{...}
return res;
}{ ... }
static int vfs_littlefs_close(void* ctx, int fd) {
esp_littlefs_t * efs = (esp_littlefs_t *)ctx;
int res;
vfs_littlefs_file_t *file = NULL;
sem_take(efs);
if((uint32_t)fd > efs->cache_size) {
sem_give(efs);
ESP_LOGE(ESP_LITTLEFS_TAG, "FD %d must be <%d.", fd, efs->cache_size);
errno = EBADF;
return -1;
}{...}
file = efs->cache[fd];
#if CONFIG_LITTLEFS_OPEN_DIR
if ((file->file.flags & O_DIRECTORY) == 0) {
#endif
res = lfs_file_close(efs->fs, &file->file);
if(res < 0){
errno = lfs_errno_remap(res);
sem_give(efs);
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to close file \"%s\". Error %s (%d)",
file->path, esp_littlefs_errno(res), res);/* ... */
#else
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to close Fd %d. Error %s (%d)",
fd, esp_littlefs_errno(res), res);/* ... */
#endif
return -1;
}{...}
#if CONFIG_LITTLEFS_OPEN_DIR
}{...} else {
res = 0;
}{...}
#endif
esp_littlefs_free_fd(efs, fd);
sem_give(efs);
return res;
}{ ... }
static off_t vfs_littlefs_lseek(void* ctx, int fd, off_t offset, int mode) {
esp_littlefs_t * efs = (esp_littlefs_t *)ctx;
lfs_soff_t res;
vfs_littlefs_file_t *file = NULL;
int whence;
switch (mode) {
case SEEK_SET: whence = LFS_SEEK_SET; break;
case SEEK_CUR: whence = LFS_SEEK_CUR; break;
case SEEK_END: whence = LFS_SEEK_END; break;
default:
ESP_LOGE(ESP_LITTLEFS_TAG, "Invalid mode");
errno = EINVAL;
return -1;...
}{...}
sem_take(efs);
if((uint32_t)fd > efs->cache_size) {
sem_give(efs);
ESP_LOGE(ESP_LITTLEFS_TAG, "FD %d must be <%d.", fd, efs->cache_size);
errno = EBADF;
return -1;
}{...}
file = efs->cache[fd];
res = lfs_file_seek(efs->fs, &file->file, offset, whence);
sem_give(efs);
if(res < 0){
errno = lfs_errno_remap(res);
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to seek file \"%s\" to offset %08x. Error %s (%d)",
file->path, (unsigned int)offset, esp_littlefs_errno(res), (int) res);/* ... */
#else
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to seek FD %d to offset %08x. Error (%d)",
fd, (unsigned int)offset, (int) res);/* ... */
#endif
return -1;
}{...}
return res;
}{ ... }
static int vfs_littlefs_fsync(void* ctx, int fd)
{
esp_littlefs_t * efs = (esp_littlefs_t *)ctx;
ssize_t res;
vfs_littlefs_file_t *file = NULL;
sem_take(efs);
if((uint32_t)fd > efs->cache_size) {
sem_give(efs);
ESP_LOGE(ESP_LITTLEFS_TAG, "FD %d must be <%d.", fd, efs->cache_size);
errno = EBADF;
return -1;
}{...}
file = efs->cache[fd];
res = lfs_file_sync(efs->fs, &file->file);
sem_give(efs);
if(res < 0){
errno = lfs_errno_remap(res);
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to sync file \"%s\". Error %s (%d)",
file->path, esp_littlefs_errno(res), res);/* ... */
#else
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to sync file %d. Error %d", fd, res);
#endif
return -1;
}{...}
return res;
}{ ... }
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
static int vfs_littlefs_fstat(void* ctx, int fd, struct stat * st) {
esp_littlefs_t * efs = (esp_littlefs_t *)ctx;
struct lfs_info info;
int res;
vfs_littlefs_file_t *file = NULL;
memset(st, 0, sizeof(struct stat));
st->st_blksize = efs->cfg.block_size;
sem_take(efs);
if((uint32_t)fd > efs->cache_size) {
sem_give(efs);
ESP_LOGE(ESP_LITTLEFS_TAG, "FD must be <%d.", efs->cache_size);
errno = EBADF;
return -1;
}{...}
file = efs->cache[fd];
res = lfs_stat(efs->fs, file->path, &info);
if (res < 0) {
errno = lfs_errno_remap(res);
sem_give(efs);
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to stat file \"%s\". Error %s (%d)",
file->path, esp_littlefs_errno(res), res);
return -1;
}{...}
#if CONFIG_LITTLEFS_USE_MTIME
st->st_mtime = vfs_littlefs_get_mtime(efs, file->path);
#endif
sem_give(efs);
st->st_size = info.size;
st->st_mode = ((info.type==LFS_TYPE_REG)?S_IFREG:S_IFDIR);
return 0;
}{ ... }
/* ... */#endif
#ifdef CONFIG_VFS_SUPPORT_DIR
static int vfs_littlefs_stat(void* ctx, const char * path, struct stat * st) {
assert(path);
esp_littlefs_t * efs = (esp_littlefs_t *)ctx;
struct lfs_info info;
int res;
memset(st, 0, sizeof(struct stat));
st->st_blksize = efs->cfg.block_size;
sem_take(efs);
res = lfs_stat(efs->fs, path, &info);
if (res < 0) {
errno = lfs_errno_remap(res);
sem_give(efs);
/* ... */
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to stat path \"%s\". Error %s (%d)",
path, esp_littlefs_errno(res), res);
return -1;
}{...}
#if CONFIG_LITTLEFS_USE_MTIME
st->st_mtime = vfs_littlefs_get_mtime(efs, path);
#endif
sem_give(efs);
st->st_size = info.size;
st->st_mode = ((info.type==LFS_TYPE_REG)?S_IFREG:S_IFDIR);
return 0;
}{ ... }
static int vfs_littlefs_unlink(void* ctx, const char *path) {
#define fail_str_1 "Failed to unlink path \"%s\"."
assert(path);
esp_littlefs_t * efs = (esp_littlefs_t *)ctx;
struct lfs_info info;
int res;
sem_take(efs);
res = lfs_stat(efs->fs, path, &info);
if (res < 0) {
errno = lfs_errno_remap(res);
sem_give(efs);
ESP_LOGV(ESP_LITTLEFS_TAG, fail_str_1 " Error %s (%d)",
path, esp_littlefs_errno(res), res);
return -1;
}{...}
if(esp_littlefs_get_fd_by_name(efs, path) >= 0) {
sem_give(efs);
ESP_LOGE(ESP_LITTLEFS_TAG, fail_str_1 " Has open FD.", path);
errno = EBUSY;
return -1;
}{...}
if (info.type == LFS_TYPE_DIR) {
sem_give(efs);
ESP_LOGV(ESP_LITTLEFS_TAG, "Cannot unlink a directory.");
errno = EISDIR;
return -1;
}{...}
res = lfs_remove(efs->fs, path);
if (res < 0) {
errno = lfs_errno_remap(res);
sem_give(efs);
ESP_LOGV(ESP_LITTLEFS_TAG, fail_str_1 " Error %s (%d)",
path, esp_littlefs_errno(res), res);
return -1;
}{...}
#if CONFIG_LITTLEFS_SPIFFS_COMPAT
rmdirs(efs, path);/* ... */
#endif
sem_give(efs);
return 0;
#undef fail_str_1
}{ ... }
static int vfs_littlefs_rename(void* ctx, const char *src, const char *dst) {
esp_littlefs_t * efs = (esp_littlefs_t *)ctx;
int res;
sem_take(efs);
if(esp_littlefs_get_fd_by_name(efs, src) >= 0){
sem_give(efs);
ESP_LOGE(ESP_LITTLEFS_TAG, "Cannot rename; src \"%s\" is open.", src);
errno = EBUSY;
return -1;
}{...}
else if(esp_littlefs_get_fd_by_name(efs, dst) >= 0){
sem_give(efs);
ESP_LOGE(ESP_LITTLEFS_TAG, "Cannot rename; dst \"%s\" is open.", dst);
errno = EBUSY;
return -1;
}{...}
#if CONFIG_LITTLEFS_SPIFFS_COMPAT
ESP_LOGV(ESP_LITTLEFS_TAG, "LITTLEFS_SPIFFS_COMPAT attempting to create all directories for %s", src);
mkdirs(efs, dst);/* ... */
#endif
res = lfs_rename(efs->fs, src, dst);
if (res < 0) {
errno = lfs_errno_remap(res);
sem_give(efs);
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to rename \"%s\" -> \"%s\". Error %s (%d)",
src, dst, esp_littlefs_errno(res), res);
return -1;
}{...}
#if CONFIG_LITTLEFS_SPIFFS_COMPAT
rmdirs(efs, src);/* ... */
#endif
sem_give(efs);
return 0;
}{ ... }
static DIR* vfs_littlefs_opendir(void* ctx, const char* name) {
esp_littlefs_t * efs = (esp_littlefs_t *)ctx;
int res;
vfs_littlefs_dir_t *dir = NULL;
dir = esp_littlefs_calloc(1, sizeof(vfs_littlefs_dir_t));
if( dir == NULL ) {
ESP_LOGE(ESP_LITTLEFS_TAG, "dir struct could not be malloced");
errno = ENOMEM;
goto exit;
}{...}
dir->path = strdup(name);
if(dir->path == NULL){
errno = ENOMEM;
ESP_LOGE(ESP_LITTLEFS_TAG, "dir path name could not be malloced");
goto exit;
}{...}
sem_take(efs);
res = lfs_dir_open(efs->fs, &dir->d, dir->path);
sem_give(efs);
if (res < 0) {
errno = lfs_errno_remap(res);
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to opendir \"%s\". Error %s (%d)",
dir->path, esp_littlefs_errno(res), res);/* ... */
#else
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to opendir \"%s\". Error %d", dir->path, res);
#endif
goto exit;
}{...}
return (DIR *)dir;
exit:
esp_littlefs_dir_free(dir);
return NULL;
}{ ... }
static int vfs_littlefs_closedir(void* ctx, DIR* pdir) {
assert(pdir);
esp_littlefs_t * efs = (esp_littlefs_t *)ctx;
vfs_littlefs_dir_t * dir = (vfs_littlefs_dir_t *) pdir;
int res;
sem_take(efs);
res = lfs_dir_close(efs->fs, &dir->d);
sem_give(efs);
if (res < 0) {
errno = lfs_errno_remap(res);
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to closedir \"%s\". Error %s (%d)",
dir->path, esp_littlefs_errno(res), res);/* ... */
#else
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to closedir \"%s\". Error %d", dir->path, res);
#endif
return res;
}{...}
esp_littlefs_dir_free(dir);
return 0;
}{ ... }
static struct dirent* vfs_littlefs_readdir(void* ctx, DIR* pdir) {
assert(pdir);
vfs_littlefs_dir_t * dir = (vfs_littlefs_dir_t *) pdir;
int res;
struct dirent* out_dirent;
res = vfs_littlefs_readdir_r(ctx, pdir, &dir->e, &out_dirent);
if (res != 0) return NULL;
return out_dirent;
}{ ... }
static int vfs_littlefs_readdir_r(void* ctx, DIR* pdir,
struct dirent* entry, struct dirent** out_dirent) {
assert(pdir);
esp_littlefs_t * efs = (esp_littlefs_t *)ctx;
vfs_littlefs_dir_t * dir = (vfs_littlefs_dir_t *) pdir;
int res;
struct lfs_info info = { 0 };
sem_take(efs);
do{
res = lfs_dir_read(efs->fs, &dir->d, &info);
}{...}while( res>0 && (strcmp(info.name, ".") == 0 || strcmp(info.name, "..") == 0));
sem_give(efs);
if (res < 0) {
errno = lfs_errno_remap(res);
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to readdir \"%s\". Error %s (%d)",
dir->path, esp_littlefs_errno(res), res);/* ... */
#else
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to readdir \"%s\". Error %d", dir->path, res);
#endif
return -1;
}{...}
if(info.type == LFS_TYPE_REG) {
ESP_LOGV(ESP_LITTLEFS_TAG, "readdir a file of size %u named \"%s\"",
(unsigned int) info.size, info.name);
}{...}
else {
ESP_LOGV(ESP_LITTLEFS_TAG, "readdir a dir named \"%s\"", info.name);
}{...}
if(res == 0) {
ESP_LOGV(ESP_LITTLEFS_TAG, "Reached the end of the directory.");
*out_dirent = NULL;
}{...}
else {
entry->d_ino = 0;
entry->d_type = info.type == LFS_TYPE_REG ? DT_REG : DT_DIR;
strncpy(entry->d_name, info.name, sizeof(entry->d_name));
*out_dirent = entry;
}{...}
dir->offset++;
return 0;
}{ ... }
static long vfs_littlefs_telldir(void* ctx, DIR* pdir) {
assert(pdir);
vfs_littlefs_dir_t * dir = (vfs_littlefs_dir_t *) pdir;
return dir->offset;
}{ ... }
static void vfs_littlefs_seekdir(void* ctx, DIR* pdir, long offset) {
assert(pdir);
esp_littlefs_t * efs = (esp_littlefs_t *)ctx;
vfs_littlefs_dir_t * dir = (vfs_littlefs_dir_t *) pdir;
int res;
if (offset < dir->offset) {
sem_take(efs);
res = lfs_dir_rewind(efs->fs, &dir->d);
sem_give(efs);
if (res < 0) {
errno = lfs_errno_remap(res);
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to rewind dir \"%s\". Error %s (%d)",
dir->path, esp_littlefs_errno(res), res);
return;
}{...}
dir->offset = 0;
}{...}
while(dir->offset < offset){
struct dirent *out_dirent;
res = vfs_littlefs_readdir_r(ctx, pdir, &dir->e, &out_dirent);
if( res != 0 ){
ESP_LOGE(ESP_LITTLEFS_TAG, "Error readdir_r");
return;
}{...}
}{...}
}{ ... }
static int vfs_littlefs_mkdir(void* ctx, const char* name, mode_t mode) {
esp_littlefs_t * efs = (esp_littlefs_t *)ctx;
int res;
ESP_LOGV(ESP_LITTLEFS_TAG, "mkdir \"%s\"", name);
sem_take(efs);
res = lfs_mkdir(efs->fs, name);
sem_give(efs);
if (res < 0) {
errno = lfs_errno_remap(res);
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to mkdir \"%s\". Error %s (%d)",
name, esp_littlefs_errno(res), res);
return -1;
}{...}
return 0;
}{ ... }
static int vfs_littlefs_rmdir(void* ctx, const char* name) {
esp_littlefs_t * efs = (esp_littlefs_t *)ctx;
struct lfs_info info;
int res;
sem_take(efs);
res = lfs_stat(efs->fs, name, &info);
if (res < 0) {
errno = lfs_errno_remap(res);
sem_give(efs);
ESP_LOGV(ESP_LITTLEFS_TAG, "\"%s\" doesn't exist.", name);
return -1;
}{...}
if (info.type != LFS_TYPE_DIR) {
sem_give(efs);
ESP_LOGV(ESP_LITTLEFS_TAG, "\"%s\" is not a directory.", name);
errno = ENOTDIR;
return -1;
}{...}
res = lfs_remove(efs->fs, name);
sem_give(efs);
if ( res < 0) {
errno = lfs_errno_remap(res);
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to unlink path \"%s\". Error %s (%d)",
name, esp_littlefs_errno(res), res);
return -1;
}{...}
return 0;
}{ ... }
static ssize_t vfs_littlefs_truncate( void *ctx, const char *path, off_t size )
{
esp_littlefs_t * efs = (esp_littlefs_t *)ctx;
ssize_t res = -1;
vfs_littlefs_file_t *file = NULL;
int fd = vfs_littlefs_open( ctx, path, LFS_O_RDWR, 438 );
sem_take(efs);
if((uint32_t)fd > efs->cache_size)
{
sem_give(efs);
ESP_LOGE(ESP_LITTLEFS_TAG, "FD %d must be <%d.", fd, efs->cache_size);
errno = EBADF;
return -1;
}{...}
file = efs->cache[fd];
res = lfs_file_truncate( efs->fs, &file->file, size );
sem_give(efs);
if(res < 0)
{
errno = lfs_errno_remap(res);
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to truncate file \"%s\". Error %s (%d)",
file->path, esp_littlefs_errno(res), res);/* ... */
#else
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to truncate FD %d. Error %s (%d)",
fd, esp_littlefs_errno(res), res);/* ... */
#endif
res = -1;
}{...}
else
{
ESP_LOGV( ESP_LITTLEFS_TAG, "Truncated file %s to %u bytes", path, (unsigned int) size );
}{...}
vfs_littlefs_close( ctx, fd );
return res;
}{ ... }
#ifdef ESP_LITTLEFS_ENABLE_FTRUNCATE
static int vfs_littlefs_ftruncate(void *ctx, int fd, off_t size)
{
esp_littlefs_t * efs = (esp_littlefs_t *)ctx;
ssize_t res;
vfs_littlefs_file_t *file = NULL;
sem_take(efs);
if((uint32_t)fd > efs->cache_size) {
sem_give(efs);
ESP_LOGE(ESP_LITTLEFS_TAG, "FD %d must be <%d.", fd, efs->cache_size);
errno = EBADF;
return -1;
}{...}
file = efs->cache[fd];
res = lfs_file_truncate( efs->fs, &file->file, size );
sem_give(efs);
if(res < 0)
{
errno = lfs_errno_remap(res);
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to truncate file \"%s\". Error %s (%d)",
file->path, esp_littlefs_errno(res), res);/* ... */
#else
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to truncate FD %d. Error %s (%d)",
fd, esp_littlefs_errno(res), res);/* ... */
#endif
res = -1;
}{...}
else
{
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV( ESP_LITTLEFS_TAG, "Truncated file %s to %u bytes", file->path, (unsigned int) size );
#else
ESP_LOGV(ESP_LITTLEFS_TAG, "Truncated FD %d to %u bytes", fd, (unsigned int) size );
#endif
}{...}
return res;
}{ ... }
/* ... */#endif /* ... */
#endif
#if CONFIG_LITTLEFS_USE_MTIME
/* ... */
static int vfs_littlefs_update_mtime_value(esp_littlefs_t *efs, const char *path, time_t t)
{
int res;
res = lfs_setattr(efs->fs, path, LITTLEFS_ATTR_MTIME,
&t, sizeof(t));
if( res < 0 ) {
errno = lfs_errno_remap(res);
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to update mtime (%d)", res);
return -1;
}{...}
return res;
}{ ... }
/* ... */
static void vfs_littlefs_update_mtime(esp_littlefs_t *efs, const char *path)
{
vfs_littlefs_utime(efs, path, NULL);
}{ ... }
static int vfs_littlefs_utime(void *ctx, const char *path, const struct utimbuf *times)
{
esp_littlefs_t * efs = (esp_littlefs_t *)ctx;
time_t t;
assert(path);
sem_take(efs);
if (times) {
t = times->modtime;
}{...} else {
#if CONFIG_LITTLEFS_MTIME_USE_SECONDS
t = time(NULL);/* ... */
#elif CONFIG_LITTLEFS_MTIME_USE_NONCE
assert( sizeof(time_t) == 4 );
t = vfs_littlefs_get_mtime(efs, path);
if( 0 == t ) t = esp_random();
else t += 1;
if( 0 == t ) t = 1;/* ... */
#else
#error "Invalid MTIME configuration"
#endif
}{...}
int ret = vfs_littlefs_update_mtime_value(efs, path, t);
sem_give(efs);
return ret;
}{ ... }
static time_t vfs_littlefs_get_mtime(esp_littlefs_t *efs, const char *path)
{
time_t t;
int size;
size = lfs_getattr(efs->fs, path, LITTLEFS_ATTR_MTIME,
&t, sizeof(t));
if( size < 0 ) {
errno = lfs_errno_remap(size);
#ifndef CONFIG_LITTLEFS_USE_ONLY_HASH
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to get mtime attribute %s (%d)",
esp_littlefs_errno(size), size);/* ... */
#else
ESP_LOGV(ESP_LITTLEFS_TAG, "Failed to get mtime attribute %d", size);
#endif
return -1;
}{...}
return t;
}{ ... }
#endif/* ... */
#if CONFIG_LITTLEFS_SPIFFS_COMPAT
/* ... */
static void mkdirs(esp_littlefs_t * efs, const char *dir) {
char tmp[CONFIG_LITTLEFS_OBJ_NAME_LEN];
char *p = NULL;
strlcpy(tmp, dir, sizeof(tmp));
for(p = tmp + 1; *p; p++) {
if(*p == '/') {
*p = '\0';
vfs_littlefs_mkdir((void*)efs, tmp, S_IRWXU);
*p = '/';
}{...}
}{...}
}{...}
/* ... */
static void rmdirs(esp_littlefs_t * efs, const char *dir) {
char tmp[CONFIG_LITTLEFS_OBJ_NAME_LEN];
char *p = NULL;
strlcpy(tmp, dir, sizeof(tmp));
for(p = tmp + strlen(tmp) - 1; p != tmp; p--) {
if(*p == '/') {
*p = '\0';
vfs_littlefs_rmdir((void*)efs, tmp);
*p = '/';
}{...}
}{...}
}{...}
/* ... */
#endif
static int vfs_littlefs_fcntl(void* ctx, int fd, int cmd, int arg)
{
int result = 0;
esp_littlefs_t *efs = (esp_littlefs_t *)ctx;
lfs_file_t *lfs_file = NULL;
vfs_littlefs_file_t *file = NULL;
const uint32_t flags_mask = LFS_O_WRONLY | LFS_O_RDONLY | LFS_O_RDWR;
sem_take(efs);
if((uint32_t)fd > efs->cache_size) {
sem_give(efs);
ESP_LOGE(ESP_LITTLEFS_TAG, "FD %d must be <%d.", fd, efs->cache_size);
errno = EBADF;
return -1;
}{...}
file = efs->cache[fd];
if (file) {
lfs_file = &efs->cache[fd]->file;
}{...} else {
sem_give(efs);
errno = EBADF;
return -1;
}{...}
if (cmd == F_GETFL) {
if ((lfs_file->flags & flags_mask) == LFS_O_WRONLY) {
result = O_WRONLY;
}{...} else if ((lfs_file->flags & flags_mask) == LFS_O_RDONLY) {
result = O_RDONLY;
}{...} else if ((lfs_file->flags & flags_mask) == LFS_O_RDWR) {
result = O_RDWR;
}{...}
}{...}
#ifdef CONFIG_LITTLEFS_FCNTL_GET_PATH
else if (cmd == F_GETPATH) {
char *buffer = (char *)(uintptr_t)arg;
assert(buffer);
if (snprintf(buffer, MAXPATHLEN, "%s%s", efs->base_path, file->path) > 0) {
result = 0;
}{...} else {
result = -1;
errno = EINVAL;
}{...}
}{...}
#endif/* ... */
else {
result = -1;
errno = ENOSYS;
}{...}
sem_give(efs);
return result;
}{ ... }