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
45
46
47
48
49
50
51
52
53
54
55
56
57
65
66
72
73
74
75
76
77
78
79
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
230
231
232
233
234
235
236
237
238
239
240
241
242
250
251
252
260
261
262
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
299
300
301
302
303
304
305
306
307
308
309
310
314
315
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
347
348
349
350
351
352
353
354
355
356
357
358
359
365
366
367
368
369
370
371
372
373
374
375
376
382
383
384
385
388
389
390
391
392
393
394
395
396
397
398
399
400
404
405
415
416
417
420
421
424
425
428
429
430
433
434
435
440
441
442
443
444
445
446
449
450
451
452
453
454
455
459
460
463
464
465
468
469
470
474
475
476
477
478
479
480
484
485
489
490
491
492
495
496
497
501
502
503
504
505
509
510
511
512
516
517
518
519
520
521
522
523
524
525
526
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
556
557
566
567
568
569
572
573
574
575
576
577
578
583
584
585
586
587
588
589
590
591
606
607
608
609
613
614
615
616
621
622
623
624
625
626
627
628
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
654
655
656
657
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
710
726
727
735
736
740
741
750
751
752
753
754
755
758
759
760
761
762
771
772
773
774
775
776
777
778
779
782
783
784
787
788
792
793
794
795
798
799
800
801
802
803
804
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
829
834
835
846
857
868
879
890
896
897
898
899
903
904
905
906
910
911
912
913
914
915
916
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
944
945
946
947
948
949
950
951
952
953
957
958
959
960
961
962
963
964
965
966
970
971
972
973
974
975
976
977
978
979
980
984
985
986
987
988
989
990
991
992
993
994
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1011
1012
1013
1014
1015
1022
1023
1024
1025
1026
1027
1028
1029
1030
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1107
1108
1109
1110
1111
1112
1113
1114
1115
1119
1120
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1157
1158
1159
1160
1161
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1183
1184
1185
1186
1189
1190
1191
1192
1193
1194
1195
1196
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1239
1240
1241
1242
1246
1247
1248
1249
1250
1251
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1361
1362
1363
1364
1368
1369
1370
1371
1372
1373
1398
1399
1400
1401
1402
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1425
1426
1427
1428
1429
1434
1435
1436
1437
1438
1439
1440
1445
1446
1450
1451
1452
1453
1454
1455
1456
1457
1460
1461
1462
1465
1466
1479
1480
1481
1487
1493
1499
1500
1501
1502
1503
1504
1505
1516
1517
1518
1519
1525
1526
1527
1528
1529
1530
1534
1535
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1552
1553
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1579
1582
1585
1586
1587
1598
1599
1600
1601
1602
1603
1604
1607
1619
1620
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1655
1656
1657
1658
1659
1660
1661
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1780
1781
1782
1783
1784
1785
1786
1787
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
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
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
/* ... */
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/reent.h>
#include <sys/unistd.h>
#include <sys/lock.h>
#include <sys/param.h>
#include <dirent.h>
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "esp_vfs.h"
#include "esp_vfs_private.h"
#include "include/esp_vfs.h"
#include "sdkconfig.h"17 includes
#ifdef CONFIG_LWIP_USE_ONLY_LWIP_SELECT
#warning CONFIG_LWIP_USE_ONLY_LWIP_SELECT is deprecated: Please use CONFIG_VFS_SUPPORT_SELECT instead
#endif
#ifdef CONFIG_VFS_SUPPRESS_SELECT_DEBUG_OUTPUT
#define LOG_LOCAL_LEVEL ESP_LOG_NONE
#endif
#include "esp_log.h"
static const char *TAG = "vfs";
#ifdef CONFIG_VFS_MAX_COUNT
#define VFS_MAX_COUNT CONFIG_VFS_MAX_COUNT
#else
/* ... */
#define VFS_MAX_COUNT 1/* ... */
#endif
#define LEN_PATH_PREFIX_IGNORED SIZE_MAX
#define FD_TABLE_ENTRY_UNUSED (fd_table_t) { .permanent = false, .has_pending_close = false, .has_pending_select = false, .vfs_index = -1, .local_fd = -1 }
typedef uint8_t local_fd_t;
_Static_assert((1 << (sizeof(local_fd_t)*8)) >= MAX_FDS, "file descriptor type too small");
typedef int8_t vfs_index_t;
_Static_assert((1 << (sizeof(vfs_index_t)*8)) >= VFS_MAX_COUNT, "VFS index type too small");
_Static_assert(((vfs_index_t) -1) < 0, "vfs_index_t must be a signed type");
typedef struct {
bool permanent :1;
bool has_pending_close :1;
bool has_pending_select :1;
uint8_t _reserved :5;
vfs_index_t vfs_index;
local_fd_t local_fd;
}{...} fd_table_t;
typedef struct {
bool isset;
fd_set readfds;
fd_set writefds;
fd_set errorfds;
}{...} fds_triple_t;
static vfs_entry_t* s_vfs[VFS_MAX_COUNT] = { 0 };
static size_t s_vfs_count = 0;
static fd_table_t s_fd_table[MAX_FDS] = { [0 ... MAX_FDS-1] = FD_TABLE_ENTRY_UNUSED };
static _lock_t s_fd_table_lock;
static ssize_t esp_get_free_index(void) {
for (ssize_t i = 0; i < VFS_MAX_COUNT; i++) {
if (s_vfs[i] == NULL) {
return i;
}{...}
}{...}
return -1;
}{...}
static void esp_vfs_free_fs_ops(esp_vfs_fs_ops_t *vfs) {
#ifdef CONFIG_VFS_SUPPORT_TERMIOS
free((void*)vfs->termios);
#endif
#ifdef CONFIG_VFS_SUPPORT_DIR
free((void*)vfs->dir);
#endif
#ifdef CONFIG_VFS_SUPPORT_SELECT
free((void*)vfs->select);
#endif
free(vfs);
}{...}
static void esp_vfs_free_entry(vfs_entry_t *entry) {
if (entry == NULL) {
return;
}{...}
if (!(entry->flags & ESP_VFS_FLAG_STATIC)) {
esp_vfs_free_fs_ops((esp_vfs_fs_ops_t*)entry->vfs);
}{...}
free(entry);
}{...}
typedef struct {
#ifdef CONFIG_VFS_SUPPORT_DIR
esp_vfs_dir_ops_t *dir;
#endif
#ifdef CONFIG_VFS_SUPPORT_TERMIOS
esp_vfs_termios_ops_t *termios;
#endif
#ifdef CONFIG_VFS_SUPPORT_SELECT
esp_vfs_select_ops_t *select;
#endif
}{...} vfs_component_proxy_t;
static void free_proxy_members(vfs_component_proxy_t *proxy) {
#ifdef CONFIG_VFS_SUPPORT_DIR
free(proxy->dir);
#endif
#ifdef CONFIG_VFS_SUPPORT_TERMIOS
free(proxy->termios);
#endif
#ifdef CONFIG_VFS_SUPPORT_SELECT
free(proxy->select);
#endif
}{...}
static esp_vfs_fs_ops_t *esp_minify_vfs(const esp_vfs_t * const vfs, vfs_component_proxy_t proxy) {
assert(vfs != NULL);
#ifdef CONFIG_VFS_SUPPORT_DIR
if (proxy.dir != NULL) {
esp_vfs_dir_ops_t tmp = {
.stat = vfs->stat,
.link = vfs->link,
.unlink = vfs->unlink,
.rename = vfs->rename,
.opendir = vfs->opendir,
.readdir = vfs->readdir,
.readdir_r = vfs->readdir_r,
.telldir = vfs->telldir,
.seekdir = vfs->seekdir,
.closedir = vfs->closedir,
.mkdir = vfs->mkdir,
.rmdir = vfs->rmdir,
.access = vfs->access,
.truncate = vfs->truncate,
.ftruncate = vfs->ftruncate,
.utime = vfs->utime,
}{...};
memcpy(proxy.dir, &tmp, sizeof(esp_vfs_dir_ops_t));
}{...}
/* ... */#endif
#ifdef CONFIG_VFS_SUPPORT_TERMIOS
if (proxy.termios != NULL) {
esp_vfs_termios_ops_t tmp = {
.tcsetattr = vfs->tcsetattr,
.tcgetattr = vfs->tcgetattr,
.tcdrain = vfs->tcdrain,
.tcflush = vfs->tcflush,
.tcflow = vfs->tcflow,
.tcgetsid = vfs->tcgetsid,
.tcsendbreak = vfs->tcsendbreak,
}{...};
memcpy(proxy.termios, &tmp, sizeof(esp_vfs_termios_ops_t));
}{...}
/* ... */#endif
#ifdef CONFIG_VFS_SUPPORT_SELECT
if (proxy.select != NULL) {
esp_vfs_select_ops_t tmp = {
.start_select = vfs->start_select,
.socket_select = vfs->socket_select,
.stop_socket_select = vfs->stop_socket_select,
.stop_socket_select_isr = vfs->stop_socket_select_isr,
.get_socket_select_semaphore = vfs->get_socket_select_semaphore,
.end_select = vfs->end_select,
}{...};
memcpy(proxy.select, &tmp, sizeof(esp_vfs_select_ops_t));
}{...}
/* ... */#endif
esp_vfs_fs_ops_t tmp = {
.write = vfs->write,
.lseek = vfs->lseek,
.read = vfs->read,
.pread = vfs->pread,
.pwrite = vfs->pwrite,
.open = vfs->open,
.close = vfs->close,
.fstat = vfs->fstat,
.fcntl = vfs->fcntl,
.ioctl = vfs->ioctl,
.fsync = vfs->fsync,
#ifdef CONFIG_VFS_SUPPORT_DIR
.dir = proxy.dir,
#endif
#ifdef CONFIG_VFS_SUPPORT_TERMIOS
.termios = proxy.termios,
#endif
#ifdef CONFIG_VFS_SUPPORT_SELECT
.select = proxy.select,
#endif
}{...};
esp_vfs_fs_ops_t *out = heap_caps_malloc(sizeof(esp_vfs_fs_ops_t), VFS_MALLOC_FLAGS);
if (out == NULL) {
return NULL;
}{...}
memcpy(out, &tmp, sizeof(esp_vfs_fs_ops_t));
return out;
}{...}
static esp_vfs_fs_ops_t* esp_vfs_duplicate_fs_ops(const esp_vfs_fs_ops_t *orig) {
vfs_component_proxy_t proxy = {};
#ifdef CONFIG_VFS_SUPPORT_DIR
if (orig->dir != NULL) {
proxy.dir = (esp_vfs_dir_ops_t*) heap_caps_malloc(sizeof(esp_vfs_dir_ops_t), VFS_MALLOC_FLAGS);
if (proxy.dir == NULL) {
goto fail;
}{...}
memcpy(proxy.dir, orig->dir, sizeof(esp_vfs_dir_ops_t));
}{...}
/* ... */#endif
#ifdef CONFIG_VFS_SUPPORT_TERMIOS
if (orig->termios != NULL) {
proxy.termios = (esp_vfs_termios_ops_t*) heap_caps_malloc(sizeof(esp_vfs_termios_ops_t), VFS_MALLOC_FLAGS);
if (proxy.termios == NULL) {
goto fail;
}{...}
memcpy(proxy.termios, orig->termios, sizeof(esp_vfs_termios_ops_t));
}{...}
/* ... */#endif
#ifdef CONFIG_VFS_SUPPORT_SELECT
if (orig->select != NULL) {
proxy.select = (esp_vfs_select_ops_t*) heap_caps_malloc(sizeof(esp_vfs_select_ops_t), VFS_MALLOC_FLAGS);
if (proxy.select == NULL) {
goto fail;
}{...}
memcpy(proxy.select, orig->select, sizeof(esp_vfs_select_ops_t));
}{...}
/* ... */#endif
esp_vfs_fs_ops_t tmp = {
.write = orig->write,
.lseek = orig->lseek,
.read = orig->read,
.pread = orig->pread,
.pwrite = orig->pwrite,
.open = orig->open,
.close = orig->close,
.fstat = orig->fstat,
.fcntl = orig->fcntl,
.ioctl = orig->ioctl,
.fsync = orig->fsync,
#ifdef CONFIG_VFS_SUPPORT_DIR
.dir = proxy.dir,
#endif
#ifdef CONFIG_VFS_SUPPORT_TERMIOS
.termios = proxy.termios,
#endif
#ifdef CONFIG_VFS_SUPPORT_SELECT
.select = proxy.select,
#endif
}{...};
esp_vfs_fs_ops_t *out = heap_caps_malloc(sizeof(esp_vfs_fs_ops_t), VFS_MALLOC_FLAGS);
if (out == NULL) {
goto fail;
}{...}
memcpy(out, &tmp, sizeof(esp_vfs_fs_ops_t));
return out;
fail:
free_proxy_members(&proxy);
return NULL;
}{...}
static esp_err_t esp_vfs_make_fs_ops(const esp_vfs_t *vfs, esp_vfs_fs_ops_t **min) {
if (vfs == NULL) {
ESP_LOGE(TAG, "Cannot minify NULL VFS");
return ESP_ERR_INVALID_ARG;
}{...}
if (min == NULL) {
ESP_LOGE(TAG, "Cannot minify VFS to NULL");
return ESP_ERR_INVALID_ARG;
}{...}
vfs_component_proxy_t proxy = {};
#ifdef CONFIG_VFS_SUPPORT_DIR
const bool skip_dir =
vfs->stat == NULL &&
vfs->link == NULL &&
vfs->unlink == NULL &&
vfs->rename == NULL &&
vfs->opendir == NULL &&
vfs->readdir == NULL &&
vfs->readdir_r == NULL &&
vfs->telldir == NULL &&
vfs->seekdir == NULL &&
vfs->closedir == NULL &&
vfs->mkdir == NULL &&
vfs->rmdir == NULL &&
vfs->access == NULL &&
vfs->truncate == NULL &&
vfs->ftruncate == NULL &&
vfs->utime == NULL;
if (!skip_dir) {
proxy.dir = (esp_vfs_dir_ops_t*) heap_caps_malloc(sizeof(esp_vfs_dir_ops_t), VFS_MALLOC_FLAGS);
if (proxy.dir == NULL) {
goto fail;
}{...}
}{...}
/* ... */#endif
#ifdef CONFIG_VFS_SUPPORT_TERMIOS
const bool skip_termios =
vfs->tcsetattr == NULL &&
vfs->tcgetattr == NULL &&
vfs->tcdrain == NULL &&
vfs->tcflush == NULL &&
vfs->tcflow == NULL &&
vfs->tcgetsid == NULL &&
vfs->tcsendbreak == NULL;
if (!skip_termios) {
proxy.termios = (esp_vfs_termios_ops_t*) heap_caps_malloc(sizeof(esp_vfs_termios_ops_t), VFS_MALLOC_FLAGS);
if (proxy.termios == NULL) {
goto fail;
}{...}
}{...}
/* ... */#endif
#ifdef CONFIG_VFS_SUPPORT_SELECT
const bool skip_select =
vfs->start_select == NULL &&
vfs->socket_select == NULL &&
vfs->stop_socket_select == NULL &&
vfs->stop_socket_select_isr == NULL &&
vfs->get_socket_select_semaphore == NULL &&
vfs->end_select == NULL;
if (!skip_select) {
proxy.select = (esp_vfs_select_ops_t*) heap_caps_malloc(sizeof(esp_vfs_select_ops_t), VFS_MALLOC_FLAGS);
if (proxy.select == NULL) {
goto fail;
}{...}
}{...}
/* ... */#endif
esp_vfs_fs_ops_t *main = esp_minify_vfs(vfs, proxy);
if (main == NULL) {
goto fail;
}{...}
*min = main;
return ESP_OK;
fail:
free_proxy_members(&proxy);
return ESP_ERR_NO_MEM;
}{...}
static esp_err_t esp_vfs_register_fs_common(const char* base_path, size_t len, const esp_vfs_fs_ops_t* vfs, int flags, void* ctx, int *vfs_index)
{
if (vfs == NULL) {
ESP_LOGE(TAG, "VFS is NULL");
return ESP_ERR_INVALID_ARG;
}{...}
if (len != LEN_PATH_PREFIX_IGNORED) {
if ((len == 1) || (len > ESP_VFS_PATH_MAX)) {
return ESP_ERR_INVALID_ARG;
}{...}
if (len >= 2 && ((base_path[0] != '/') || (base_path[len - 1] == '/'))) {
return ESP_ERR_INVALID_ARG;
}{...}
}{...}
ssize_t index = esp_get_free_index();
if (index < 0) {
return ESP_ERR_NO_MEM;
}{...}
if (s_vfs[index] != NULL) {
return ESP_ERR_INVALID_STATE;
}{...}
if (index == s_vfs_count) {
s_vfs_count++;
}{...}
vfs_entry_t *entry = (vfs_entry_t*) heap_caps_malloc(sizeof(vfs_entry_t), VFS_MALLOC_FLAGS);
if (entry == NULL) {
return ESP_ERR_NO_MEM;
}{...}
s_vfs[index] = entry;
if (len != LEN_PATH_PREFIX_IGNORED) {
strcpy(entry->path_prefix, base_path);
}{...} else {
bzero(entry->path_prefix, sizeof(entry->path_prefix));
}{...}
entry->path_prefix_len = len;
entry->vfs = vfs;
entry->ctx = ctx;
entry->offset = index;
entry->flags = flags;
if (vfs_index) {
*vfs_index = index;
}{...}
return ESP_OK;
}{...}
esp_err_t esp_vfs_register_fs(const char* base_path, const esp_vfs_fs_ops_t* vfs, int flags, void* ctx)
{
if (vfs == NULL) {
ESP_LOGE(TAG, "VFS is NULL");
return ESP_ERR_INVALID_ARG;
}{...}
if ((flags & ESP_VFS_FLAG_STATIC)) {
return esp_vfs_register_fs_common(base_path, strlen(base_path), vfs, flags, ctx, NULL);
}{...}
esp_vfs_fs_ops_t *_vfs = esp_vfs_duplicate_fs_ops(vfs);
if (_vfs == NULL) {
return ESP_ERR_NO_MEM;
}{...}
esp_err_t ret = esp_vfs_register_fs_common(base_path, strlen(base_path), _vfs, flags, ctx, NULL);
if (ret != ESP_OK) {
esp_vfs_free_fs_ops(_vfs);
return ret;
}{...}
return ESP_OK;
}{...}
esp_err_t esp_vfs_register_common(const char* base_path, size_t len, const esp_vfs_t* vfs, void* ctx, int *vfs_index)
{
if (vfs == NULL) {
ESP_LOGE(TAG, "VFS is NULL");
return ESP_ERR_INVALID_ARG;
}{...}
if (vfs->flags & ESP_VFS_FLAG_STATIC) {
ESP_LOGE(TAG, "ESP_VFS_FLAG_STATIC is not supported for esp_vfs_t, use esp_vfs_register_fs instead");
return ESP_ERR_INVALID_ARG;
}{...}
esp_vfs_fs_ops_t *_vfs = NULL;
esp_err_t ret = esp_vfs_make_fs_ops(vfs, &_vfs);
if (ret != ESP_OK) {
return ret;
}{...}
ret = esp_vfs_register_fs_common(base_path, len, _vfs, vfs->flags, ctx, vfs_index);
if (ret != ESP_OK) {
esp_vfs_free_fs_ops(_vfs);
return ret;
}{...}
return ESP_OK;
}{...}
esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ctx)
{
return esp_vfs_register_common(base_path, strlen(base_path), vfs, ctx, NULL);
}{...}
esp_err_t esp_vfs_register_fd_range(const esp_vfs_t *vfs, void *ctx, int min_fd, int max_fd)
{
if (min_fd < 0 || max_fd < 0 || min_fd > MAX_FDS || max_fd > MAX_FDS || min_fd > max_fd) {
ESP_LOGD(TAG, "Invalid arguments: esp_vfs_register_fd_range(0x%x, 0x%x, %d, %d)", (int) vfs, (int) ctx, min_fd, max_fd);
return ESP_ERR_INVALID_ARG;
}{...}
int index = 0;
esp_err_t ret = esp_vfs_register_common("", LEN_PATH_PREFIX_IGNORED, vfs, ctx, &index);
if (ret == ESP_OK) {
_lock_acquire(&s_fd_table_lock);
for (int i = min_fd; i < max_fd; ++i) {
if (s_fd_table[i].vfs_index != -1) {
free(s_vfs[index]);
s_vfs[index] = NULL;
for (int j = min_fd; j < i; ++j) {
if (s_fd_table[j].vfs_index == index) {
s_fd_table[j] = FD_TABLE_ENTRY_UNUSED;
}{...}
}{...}
_lock_release(&s_fd_table_lock);
ESP_LOGD(TAG, "esp_vfs_register_fd_range cannot set fd %d (used by other VFS)", i);
return ESP_ERR_INVALID_ARG;
}{...}
s_fd_table[i].permanent = true;
s_fd_table[i].vfs_index = index;
s_fd_table[i].local_fd = i;
}{...}
_lock_release(&s_fd_table_lock);
ESP_LOGW(TAG, "esp_vfs_register_fd_range is successful for range <%d; %d) and VFS ID %d", min_fd, max_fd, index);
}{...}
return ret;
}{...}
esp_err_t esp_vfs_register_fs_with_id(const esp_vfs_fs_ops_t *vfs, int flags, void *ctx, esp_vfs_id_t *vfs_id)
{
if (vfs_id == NULL) {
return ESP_ERR_INVALID_ARG;
}{...}
*vfs_id = -1;
return esp_vfs_register_fs_common("", LEN_PATH_PREFIX_IGNORED, vfs, flags, ctx, vfs_id);
}{...}
esp_err_t esp_vfs_register_with_id(const esp_vfs_t *vfs, void *ctx, esp_vfs_id_t *vfs_id)
{
if (vfs_id == NULL) {
return ESP_ERR_INVALID_ARG;
}{...}
*vfs_id = -1;
return esp_vfs_register_common("", LEN_PATH_PREFIX_IGNORED, vfs, ctx, vfs_id);
}{...}
esp_err_t esp_vfs_unregister_with_id(esp_vfs_id_t vfs_id)
{
if (vfs_id < 0 || vfs_id >= VFS_MAX_COUNT || s_vfs[vfs_id] == NULL) {
return ESP_ERR_INVALID_ARG;
}{...}
vfs_entry_t* vfs = s_vfs[vfs_id];
esp_vfs_free_entry(vfs);
s_vfs[vfs_id] = NULL;
_lock_acquire(&s_fd_table_lock);
for (int j = 0; j < VFS_MAX_COUNT; ++j) {
if (s_fd_table[j].vfs_index == vfs_id) {
s_fd_table[j] = FD_TABLE_ENTRY_UNUSED;
}{...}
}{...}
_lock_release(&s_fd_table_lock);
return ESP_OK;
}{...}
esp_err_t esp_vfs_unregister_fs_with_id(esp_vfs_id_t vfs_id) __attribute__((alias("esp_vfs_unregister_with_id")));
esp_err_t esp_vfs_unregister(const char* base_path)
{
const size_t base_path_len = strlen(base_path);
for (size_t i = 0; i < s_vfs_count; ++i) {
vfs_entry_t* vfs = s_vfs[i];
if (vfs == NULL) {
continue;
}{...}
if (base_path_len == vfs->path_prefix_len &&
memcmp(base_path, vfs->path_prefix, vfs->path_prefix_len) == 0) {
return esp_vfs_unregister_with_id(i);
}{...}
}{...}
return ESP_ERR_INVALID_STATE;
}{...}
esp_err_t esp_vfs_unregister_fs(const char* base_path) __attribute__((alias("esp_vfs_unregister")));
esp_err_t esp_vfs_register_fd(esp_vfs_id_t vfs_id, int *fd)
{
return esp_vfs_register_fd_with_local_fd(vfs_id, -1, true, fd);
}{...}
esp_err_t esp_vfs_register_fd_with_local_fd(esp_vfs_id_t vfs_id, int local_fd, bool permanent, int *fd)
{
if (vfs_id < 0 || vfs_id >= s_vfs_count || fd == NULL) {
ESP_LOGD(TAG, "Invalid arguments for esp_vfs_register_fd_with_local_fd(%d, %d, %d, 0x%p)",
vfs_id, local_fd, permanent, fd);
return ESP_ERR_INVALID_ARG;
}{...}
esp_err_t ret = ESP_ERR_NO_MEM;
_lock_acquire(&s_fd_table_lock);
for (int i = 0; i < MAX_FDS; ++i) {
if (s_fd_table[i].vfs_index == -1) {
s_fd_table[i].permanent = permanent;
s_fd_table[i].vfs_index = vfs_id;
if (local_fd >= 0) {
s_fd_table[i].local_fd = local_fd;
}{...} else {
s_fd_table[i].local_fd = i;
}{...}
*fd = i;
ret = ESP_OK;
break;
}{...}
}{...}
_lock_release(&s_fd_table_lock);
ESP_LOGD(TAG, "esp_vfs_register_fd_with_local_fd(%d, %d, %d, 0x%p) finished with %s",
vfs_id, local_fd, permanent, fd, esp_err_to_name(ret));
return ret;
}{...}
esp_err_t esp_vfs_unregister_fd(esp_vfs_id_t vfs_id, int fd)
{
esp_err_t ret = ESP_ERR_INVALID_ARG;
if (vfs_id < 0 || vfs_id >= s_vfs_count || fd < 0 || fd >= MAX_FDS) {
ESP_LOGD(TAG, "Invalid arguments for esp_vfs_unregister_fd(%d, %d)", vfs_id, fd);
return ret;
}{...}
_lock_acquire(&s_fd_table_lock);
fd_table_t *item = s_fd_table + fd;
if (item->permanent == true && item->vfs_index == vfs_id && item->local_fd == fd) {
*item = FD_TABLE_ENTRY_UNUSED;
ret = ESP_OK;
}{...}
_lock_release(&s_fd_table_lock);
ESP_LOGD(TAG, "esp_vfs_unregister_fd(%d, %d) finished with %s", vfs_id, fd, esp_err_to_name(ret));
return ret;
}{...}
void esp_vfs_dump_fds(FILE *fp)
{
const vfs_entry_t* vfs;
fprintf(fp, "------------------------------------------------------\n");
fprintf(fp, "<VFS Path Prefix>-<FD seen by App>-<FD seen by driver>\n");
fprintf(fp, "------------------------------------------------------\n");
_lock_acquire(&s_fd_table_lock);
for (int index = 0; index < MAX_FDS; index++) {
if (s_fd_table[index].vfs_index != -1) {
vfs = s_vfs[s_fd_table[index].vfs_index];
if (strcmp(vfs->path_prefix, "")) {
fprintf(fp, "(%s) - 0x%x - 0x%x\n", vfs->path_prefix, index, s_fd_table[index].local_fd);
}{...} else {
fprintf(fp, "(socket) - 0x%x - 0x%x\n", index, s_fd_table[index].local_fd);
}{...}
}{...}
}{...}
_lock_release(&s_fd_table_lock);
}{...}
void esp_vfs_dump_registered_paths(FILE *fp)
{
fprintf(fp, "------------------------------------------------------\n");
fprintf(fp, "<index>:<VFS Path Prefix> -> <VFS entry ptr>\n");
fprintf(fp, "------------------------------------------------------\n");
for (size_t i = 0; i < VFS_MAX_COUNT; ++i) {
fprintf(
fp,
"%d:%s -> %p\n",
i,
s_vfs[i] ? s_vfs[i]->path_prefix : "NULL",
s_vfs[i] ? s_vfs[i]->vfs : NULL
);
}{...}
}{...}
/* ... */
esp_err_t esp_vfs_set_readonly_flag(const char* base_path)
{
const size_t base_path_len = strlen(base_path);
for (size_t i = 0; i < s_vfs_count; ++i) {
vfs_entry_t* vfs = s_vfs[i];
if (vfs == NULL) {
continue;
}{...}
if (base_path_len == vfs->path_prefix_len &&
memcmp(base_path, vfs->path_prefix, vfs->path_prefix_len) == 0) {
vfs->flags |= ESP_VFS_FLAG_READONLY_FS;
return ESP_OK;
}{...}
}{...}
return ESP_ERR_INVALID_STATE;
}{...}
const vfs_entry_t *get_vfs_for_index(int index)
{
if (index < 0 || index >= s_vfs_count) {
return NULL;
}{...} else {
return s_vfs[index];
}{...}
}{...}
static inline bool fd_valid(int fd)
{
return (fd < MAX_FDS) && (fd >= 0);
}{...}
static const vfs_entry_t *get_vfs_for_fd(int fd)
{
const vfs_entry_t *vfs = NULL;
if (fd_valid(fd)) {
const int index = s_fd_table[fd].vfs_index;
vfs = get_vfs_for_index(index);
}{...}
return vfs;
}{...}
static inline int get_local_fd(const vfs_entry_t *vfs, int fd)
{
int local_fd = -1;
if (vfs && fd_valid(fd)) {
local_fd = s_fd_table[fd].local_fd;
}{...}
return local_fd;
}{...}
static const char* translate_path(const vfs_entry_t* vfs, const char* src_path)
{
assert(strncmp(src_path, vfs->path_prefix, vfs->path_prefix_len) == 0);
if (strlen(src_path) == vfs->path_prefix_len) {
return "/";
}{...}
return src_path + vfs->path_prefix_len;
}{...}
const vfs_entry_t* get_vfs_for_path(const char* path)
{
const vfs_entry_t* best_match = NULL;
ssize_t best_match_prefix_len = -1;
size_t len = strlen(path);
for (size_t i = 0; i < s_vfs_count; ++i) {
const vfs_entry_t* vfs = s_vfs[i];
if (vfs == NULL || vfs->path_prefix_len == LEN_PATH_PREFIX_IGNORED) {
continue;
}{...}
if (len < vfs->path_prefix_len ||
memcmp(path, vfs->path_prefix, vfs->path_prefix_len) != 0) {
continue;
}{...}
if (vfs->path_prefix_len == 0 && !best_match) {
best_match = vfs;
continue;
}{...}
if (len > vfs->path_prefix_len &&
path[vfs->path_prefix_len] != '/') {
continue;
}{...}
if (best_match_prefix_len < (ssize_t) vfs->path_prefix_len) {
best_match_prefix_len = (ssize_t) vfs->path_prefix_len;
best_match = vfs;
}{...}
}{...}
return best_match;
}{...}
/* ... */
#define CHECK_AND_CALL(ret, r, pvfs, func, ...) \
if (pvfs->vfs->func == NULL) { \
__errno_r(r) = ENOSYS; \
return -1; \
}{...} \
if (pvfs->flags & ESP_VFS_FLAG_CONTEXT_PTR) { \
ret = (*pvfs->vfs->func ## _p)(pvfs->ctx, __VA_ARGS__); \
}{...} else { \
ret = (*pvfs->vfs->func)(__VA_ARGS__);\
}{...}
...
#define CHECK_AND_CALL_SUBCOMPONENT(ret, r, pvfs, component, func, ...) \
if (pvfs->vfs->component == NULL || pvfs->vfs->component->func == NULL) { \
__errno_r(r) = ENOSYS; \
return -1; \
}{...} \
if (pvfs->flags & ESP_VFS_FLAG_CONTEXT_PTR) { \
ret = (*pvfs->vfs->component->func ## _p)(pvfs->ctx, __VA_ARGS__); \
}{...} else { \
ret = (*pvfs->vfs->component->func)(__VA_ARGS__);\
}{...}
...
#define CHECK_AND_CALLV(r, pvfs, func, ...) \
if (pvfs->vfs->func == NULL) { \
__errno_r(r) = ENOSYS; \
return; \
}{...} \
if (pvfs->flags & ESP_VFS_FLAG_CONTEXT_PTR) { \
(*pvfs->vfs->func ## _p)(pvfs->ctx, __VA_ARGS__); \
}{...} else { \
(*pvfs->vfs->func)(__VA_ARGS__);\
}{...}
...
#define CHECK_AND_CALL_SUBCOMPONENTV(r, pvfs, component, func, ...) \
if (pvfs->vfs->component == NULL || pvfs->vfs->component->func == NULL) { \
__errno_r(r) = ENOSYS; \
return; \
}{...} \
if (pvfs->flags & ESP_VFS_FLAG_CONTEXT_PTR) { \
(*pvfs->vfs->component->func ## _p)(pvfs->ctx, __VA_ARGS__); \
}{...} else { \
(*pvfs->vfs->component->func)(__VA_ARGS__);\
}{...}
...
#define CHECK_AND_CALLP(ret, r, pvfs, func, ...) \
if (pvfs->vfs->func == NULL) { \
__errno_r(r) = ENOSYS; \
return NULL; \
}{...} \
if (pvfs->flags & ESP_VFS_FLAG_CONTEXT_PTR) { \
ret = (*pvfs->vfs->func ## _p)(pvfs->ctx, __VA_ARGS__); \
}{...} else { \
ret = (*pvfs->vfs->func)(__VA_ARGS__);\
}{...}
...
#define CHECK_AND_CALL_SUBCOMPONENTP(ret, r, pvfs, component, func, ...) \
if (pvfs->vfs->component == NULL || pvfs->vfs->component->func == NULL) { \
__errno_r(r) = ENOSYS; \
return NULL; \
}{...} \
if (pvfs->flags & ESP_VFS_FLAG_CONTEXT_PTR) { \
ret = (*pvfs->vfs->component->func ## _p)(pvfs->ctx, __VA_ARGS__); \
}{...} else { \
ret = (*pvfs->vfs->component->func)(__VA_ARGS__);\
}{...}
...
#define CHECK_VFS_READONLY_FLAG(flags) \
if (flags & ESP_VFS_FLAG_READONLY_FS) { \
__errno_r(r) = EROFS; \
return -1; \
}{...}
...
7 definesint esp_vfs_open(struct _reent *r, const char * path, int flags, int mode)
{
const vfs_entry_t *vfs = get_vfs_for_path(path);
if (vfs == NULL) {
__errno_r(r) = ENOENT;
return -1;
}{...}
int acc_mode = flags & O_ACCMODE;
int ro_filesystem = vfs->flags & ESP_VFS_FLAG_READONLY_FS;
if (acc_mode != O_RDONLY && ro_filesystem) {
__errno_r(r) = EROFS;
return -1;
}{...}
const char *path_within_vfs = translate_path(vfs, path);
int fd_within_vfs;
CHECK_AND_CALL(fd_within_vfs, r, vfs, open, path_within_vfs, flags, mode);
if (fd_within_vfs >= 0) {
_lock_acquire(&s_fd_table_lock);
for (int i = 0; i < MAX_FDS; ++i) {
if (s_fd_table[i].vfs_index == -1) {
s_fd_table[i].permanent = false;
s_fd_table[i].vfs_index = vfs->offset;
s_fd_table[i].local_fd = fd_within_vfs;
_lock_release(&s_fd_table_lock);
return i;
}{...}
}{...}
_lock_release(&s_fd_table_lock);
int ret;
CHECK_AND_CALL(ret, r, vfs, close, fd_within_vfs);
(void) ret;
__errno_r(r) = ENOMEM;
return -1;
}{...}
__errno_r(r) = errno;
return -1;
}{...}
ssize_t esp_vfs_write(struct _reent *r, int fd, const void * data, size_t size)
{
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
const int local_fd = get_local_fd(vfs, fd);
if (vfs == NULL || local_fd < 0) {
__errno_r(r) = EBADF;
return -1;
}{...}
ssize_t ret;
CHECK_AND_CALL(ret, r, vfs, write, local_fd, data, size);
return ret;
}{...}
off_t esp_vfs_lseek(struct _reent *r, int fd, off_t size, int mode)
{
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
const int local_fd = get_local_fd(vfs, fd);
if (vfs == NULL || local_fd < 0) {
__errno_r(r) = EBADF;
return -1;
}{...}
off_t ret;
CHECK_AND_CALL(ret, r, vfs, lseek, local_fd, size, mode);
return ret;
}{...}
ssize_t esp_vfs_read(struct _reent *r, int fd, void * dst, size_t size)
{
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
const int local_fd = get_local_fd(vfs, fd);
if (vfs == NULL || local_fd < 0) {
__errno_r(r) = EBADF;
return -1;
}{...}
ssize_t ret;
CHECK_AND_CALL(ret, r, vfs, read, local_fd, dst, size);
return ret;
}{...}
ssize_t esp_vfs_pread(int fd, void *dst, size_t size, off_t offset)
{
struct _reent *r = __getreent();
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
const int local_fd = get_local_fd(vfs, fd);
if (vfs == NULL || local_fd < 0) {
__errno_r(r) = EBADF;
return -1;
}{...}
ssize_t ret;
CHECK_AND_CALL(ret, r, vfs, pread, local_fd, dst, size, offset);
return ret;
}{...}
ssize_t esp_vfs_pwrite(int fd, const void *src, size_t size, off_t offset)
{
struct _reent *r = __getreent();
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
const int local_fd = get_local_fd(vfs, fd);
if (vfs == NULL || local_fd < 0) {
__errno_r(r) = EBADF;
return -1;
}{...}
ssize_t ret;
CHECK_AND_CALL(ret, r, vfs, pwrite, local_fd, src, size, offset);
return ret;
}{...}
int esp_vfs_close(struct _reent *r, int fd)
{
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
const int local_fd = get_local_fd(vfs, fd);
if (vfs == NULL || local_fd < 0) {
__errno_r(r) = EBADF;
return -1;
}{...}
int ret;
CHECK_AND_CALL(ret, r, vfs, close, local_fd);
_lock_acquire(&s_fd_table_lock);
if (!s_fd_table[fd].permanent) {
if (s_fd_table[fd].has_pending_select) {
s_fd_table[fd].has_pending_close = true;
}{...} else {
s_fd_table[fd] = FD_TABLE_ENTRY_UNUSED;
}{...}
}{...}
_lock_release(&s_fd_table_lock);
return ret;
}{...}
int esp_vfs_fstat(struct _reent *r, int fd, struct stat * st)
{
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
const int local_fd = get_local_fd(vfs, fd);
if (vfs == NULL || local_fd < 0) {
__errno_r(r) = EBADF;
return -1;
}{...}
int ret;
CHECK_AND_CALL(ret, r, vfs, fstat, local_fd, st);
return ret;
}{...}
int esp_vfs_fcntl_r(struct _reent *r, int fd, int cmd, int arg)
{
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
const int local_fd = get_local_fd(vfs, fd);
if (vfs == NULL || local_fd < 0) {
__errno_r(r) = EBADF;
return -1;
}{...}
int ret;
CHECK_AND_CALL(ret, r, vfs, fcntl, local_fd, cmd, arg);
return ret;
}{...}
int esp_vfs_ioctl(int fd, int cmd, ...)
{
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
const int local_fd = get_local_fd(vfs, fd);
struct _reent* r = __getreent();
if (vfs == NULL || local_fd < 0) {
__errno_r(r) = EBADF;
return -1;
}{...}
int ret;
va_list args;
va_start(args, cmd);
CHECK_AND_CALL(ret, r, vfs, ioctl, local_fd, cmd, args);
va_end(args);
return ret;
}{...}
int esp_vfs_fsync(int fd)
{
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
const int local_fd = get_local_fd(vfs, fd);
struct _reent* r = __getreent();
if (vfs == NULL || local_fd < 0) {
__errno_r(r) = EBADF;
return -1;
}{...}
int ret;
CHECK_AND_CALL(ret, r, vfs, fsync, local_fd);
return ret;
}{...}
#ifdef CONFIG_VFS_SUPPORT_DIR
int esp_vfs_stat(struct _reent *r, const char * path, struct stat * st)
{
const vfs_entry_t* vfs = get_vfs_for_path(path);
if (vfs == NULL) {
__errno_r(r) = ENOENT;
return -1;
}{...}
const char* path_within_vfs = translate_path(vfs, path);
int ret;
CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, stat, path_within_vfs, st);
return ret;
}{...}
int esp_vfs_utime(const char *path, const struct utimbuf *times)
{
int ret;
const vfs_entry_t* vfs = get_vfs_for_path(path);
struct _reent* r = __getreent();
if (vfs == NULL) {
__errno_r(r) = ENOENT;
return -1;
}{...}
const char* path_within_vfs = translate_path(vfs, path);
CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, utime, path_within_vfs, times);
return ret;
}{...}
int esp_vfs_link(struct _reent *r, const char* n1, const char* n2)
{
const vfs_entry_t* vfs = get_vfs_for_path(n1);
if (vfs == NULL) {
__errno_r(r) = ENOENT;
return -1;
}{...}
const vfs_entry_t* vfs2 = get_vfs_for_path(n2);
if (vfs != vfs2) {
__errno_r(r) = EXDEV;
return -1;
}{...}
CHECK_VFS_READONLY_FLAG(vfs2->flags);
const char* path1_within_vfs = translate_path(vfs, n1);
const char* path2_within_vfs = translate_path(vfs, n2);
int ret;
CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, link, path1_within_vfs, path2_within_vfs);
return ret;
}{...}
int esp_vfs_unlink(struct _reent *r, const char *path)
{
const vfs_entry_t* vfs = get_vfs_for_path(path);
if (vfs == NULL) {
__errno_r(r) = ENOENT;
return -1;
}{...}
CHECK_VFS_READONLY_FLAG(vfs->flags);
const char* path_within_vfs = translate_path(vfs, path);
int ret;
CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, unlink, path_within_vfs);
return ret;
}{...}
int esp_vfs_rename(struct _reent *r, const char *src, const char *dst)
{
const vfs_entry_t* vfs = get_vfs_for_path(src);
if (vfs == NULL) {
__errno_r(r) = ENOENT;
return -1;
}{...}
CHECK_VFS_READONLY_FLAG(vfs->flags);
const vfs_entry_t* vfs_dst = get_vfs_for_path(dst);
if (vfs != vfs_dst) {
__errno_r(r) = EXDEV;
return -1;
}{...}
CHECK_VFS_READONLY_FLAG(vfs_dst->flags);
const char* src_within_vfs = translate_path(vfs, src);
const char* dst_within_vfs = translate_path(vfs, dst);
int ret;
CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, rename, src_within_vfs, dst_within_vfs);
return ret;
}{...}
DIR* esp_vfs_opendir(const char* name)
{
const vfs_entry_t* vfs = get_vfs_for_path(name);
struct _reent* r = __getreent();
if (vfs == NULL) {
__errno_r(r) = ENOENT;
return NULL;
}{...}
const char* path_within_vfs = translate_path(vfs, name);
DIR* ret;
CHECK_AND_CALL_SUBCOMPONENTP(ret, r, vfs, dir, opendir, path_within_vfs);
if (ret != NULL) {
ret->dd_vfs_idx = vfs->offset;
}{...}
return ret;
}{...}
struct dirent* esp_vfs_readdir(DIR* pdir)
{
const vfs_entry_t* vfs = get_vfs_for_index(pdir->dd_vfs_idx);
struct _reent* r = __getreent();
if (vfs == NULL) {
__errno_r(r) = EBADF;
return NULL;
}{...}
struct dirent* ret;
CHECK_AND_CALL_SUBCOMPONENTP(ret, r, vfs, dir, readdir, pdir);
return ret;
}{...}
int esp_vfs_readdir_r(DIR* pdir, struct dirent* entry, struct dirent** out_dirent)
{
const vfs_entry_t* vfs = get_vfs_for_index(pdir->dd_vfs_idx);
struct _reent* r = __getreent();
if (vfs == NULL) {
errno = EBADF;
return -1;
}{...}
int ret;
CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, readdir_r, pdir, entry, out_dirent);
return ret;
}{...}
long esp_vfs_telldir(DIR* pdir)
{
const vfs_entry_t* vfs = get_vfs_for_index(pdir->dd_vfs_idx);
struct _reent* r = __getreent();
if (vfs == NULL) {
errno = EBADF;
return -1;
}{...}
long ret;
CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, telldir, pdir);
return ret;
}{...}
void esp_vfs_seekdir(DIR* pdir, long loc)
{
const vfs_entry_t* vfs = get_vfs_for_index(pdir->dd_vfs_idx);
struct _reent* r = __getreent();
if (vfs == NULL) {
errno = EBADF;
return;
}{...}
CHECK_AND_CALL_SUBCOMPONENTV(r, vfs, dir, seekdir, pdir, loc);
}{...}
void esp_vfs_rewinddir(DIR* pdir)
{
seekdir(pdir, 0);
}{...}
int esp_vfs_closedir(DIR* pdir)
{
const vfs_entry_t* vfs = get_vfs_for_index(pdir->dd_vfs_idx);
struct _reent* r = __getreent();
if (vfs == NULL) {
errno = EBADF;
return -1;
}{...}
int ret;
CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, closedir, pdir);
return ret;
}{...}
int esp_vfs_mkdir(const char* name, mode_t mode)
{
const vfs_entry_t* vfs = get_vfs_for_path(name);
struct _reent* r = __getreent();
if (vfs == NULL) {
__errno_r(r) = ENOENT;
return -1;
}{...}
CHECK_VFS_READONLY_FLAG(vfs->flags);
const char* path_within_vfs = translate_path(vfs, name);
int ret;
CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, mkdir, path_within_vfs, mode);
return ret;
}{...}
int esp_vfs_rmdir(const char* name)
{
const vfs_entry_t* vfs = get_vfs_for_path(name);
struct _reent* r = __getreent();
if (vfs == NULL) {
__errno_r(r) = ENOENT;
return -1;
}{...}
CHECK_VFS_READONLY_FLAG(vfs->flags);
const char* path_within_vfs = translate_path(vfs, name);
int ret;
CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, rmdir, path_within_vfs);
return ret;
}{...}
int esp_vfs_access(const char *path, int amode)
{
int ret;
const vfs_entry_t* vfs = get_vfs_for_path(path);
struct _reent* r = __getreent();
if (vfs == NULL) {
__errno_r(r) = ENOENT;
return -1;
}{...}
const char* path_within_vfs = translate_path(vfs, path);
CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, access, path_within_vfs, amode);
return ret;
}{...}
int esp_vfs_truncate(const char *path, off_t length)
{
int ret;
const vfs_entry_t* vfs = get_vfs_for_path(path);
struct _reent* r = __getreent();
if (vfs == NULL) {
__errno_r(r) = ENOENT;
return -1;
}{...}
CHECK_VFS_READONLY_FLAG(vfs->flags);
const char* path_within_vfs = translate_path(vfs, path);
CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, truncate, path_within_vfs, length);
return ret;
}{...}
int esp_vfs_ftruncate(int fd, off_t length)
{
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
int local_fd = get_local_fd(vfs, fd);
struct _reent* r = __getreent();
if (vfs == NULL || local_fd < 0) {
__errno_r(r) = EBADF;
return -1;
}{...}
CHECK_VFS_READONLY_FLAG(vfs->flags);
int ret;
CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, ftruncate, local_fd, length);
return ret;
}{...}
/* ... */
#endif
#ifdef CONFIG_VFS_SUPPORT_SELECT
static void call_end_selects(int end_index, const fds_triple_t *vfs_fds_triple, void **driver_args)
{
for (int i = 0; i < end_index; ++i) {
const vfs_entry_t *vfs = get_vfs_for_index(i);
const fds_triple_t *item = &vfs_fds_triple[i];
if (vfs != NULL
&& vfs->vfs->select != NULL
&& vfs->vfs->select->end_select != NULL
&& item->isset
) {
esp_err_t err = vfs->vfs->select->end_select(driver_args[i]);
if (err != ESP_OK) {
ESP_LOGD(TAG, "end_select failed: %s", esp_err_to_name(err));
}{...}
}{...}
}{...}
}{...}
static inline bool esp_vfs_safe_fd_isset(int fd, const fd_set *fds)
{
return fds && FD_ISSET(fd, fds);
}{...}
static int set_global_fd_sets(const fds_triple_t *vfs_fds_triple, int size, fd_set *readfds, fd_set *writefds, fd_set *errorfds)
{
int ret = 0;
for (int i = 0; i < size; ++i) {
const fds_triple_t *item = &vfs_fds_triple[i];
if (item->isset) {
for (int fd = 0; fd < MAX_FDS; ++fd) {
if (s_fd_table[fd].vfs_index == i) {
const int local_fd = s_fd_table[fd].local_fd;
if (readfds && esp_vfs_safe_fd_isset(local_fd, &item->readfds)) {
ESP_LOGD(TAG, "FD %d in readfds was set from VFS ID %d", fd, i);
FD_SET(fd, readfds);
++ret;
}{...}
if (writefds && esp_vfs_safe_fd_isset(local_fd, &item->writefds)) {
ESP_LOGD(TAG, "FD %d in writefds was set from VFS ID %d", fd, i);
FD_SET(fd, writefds);
++ret;
}{...}
if (errorfds && esp_vfs_safe_fd_isset(local_fd, &item->errorfds)) {
ESP_LOGD(TAG, "FD %d in errorfds was set from VFS ID %d", fd, i);
FD_SET(fd, errorfds);
++ret;
}{...}
}{...}
}{...}
}{...}
}{...}
return ret;
}{...}
static void esp_vfs_log_fd_set(const char *fds_name, const fd_set *fds)
{
if (fds_name && fds) {
ESP_LOGD(TAG, "FDs in %s =", fds_name);
for (int i = 0; i < MAX_FDS; ++i) {
if (esp_vfs_safe_fd_isset(i, fds)) {
ESP_LOGD(TAG, "%d", i);
}{...}
}{...}
}{...}
}{...}
int esp_vfs_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout)
{
int ret = 0;
struct _reent* r = __getreent();
ESP_LOGD(TAG, "esp_vfs_select starts with nfds = %d", nfds);
if (timeout) {
ESP_LOGD(TAG, "timeout is %lds + %ldus", (long)timeout->tv_sec, timeout->tv_usec);
}{...}
esp_vfs_log_fd_set("readfds", readfds);
esp_vfs_log_fd_set("writefds", writefds);
esp_vfs_log_fd_set("errorfds", errorfds);
if (nfds > MAX_FDS || nfds < 0) {
ESP_LOGD(TAG, "incorrect nfds");
__errno_r(r) = EINVAL;
return -1;
}{...}
const size_t vfs_count = s_vfs_count;
fds_triple_t *vfs_fds_triple;
if ((vfs_fds_triple = heap_caps_calloc(vfs_count, sizeof(fds_triple_t), VFS_MALLOC_FLAGS)) == NULL) {
__errno_r(r) = ENOMEM;
ESP_LOGD(TAG, "calloc is unsuccessful");
return -1;
}{...}
esp_vfs_select_sem_t sel_sem = {
.is_sem_local = false,
.sem = NULL,
}{...};
int (*socket_select)(int, fd_set *, fd_set *, fd_set *, struct timeval *) = NULL;
for (int fd = 0; fd < nfds; ++fd) {
_lock_acquire(&s_fd_table_lock);
const bool is_socket_fd = s_fd_table[fd].permanent;
const int vfs_index = s_fd_table[fd].vfs_index;
const int local_fd = s_fd_table[fd].local_fd;
if (esp_vfs_safe_fd_isset(fd, errorfds)) {
s_fd_table[fd].has_pending_select = true;
}{...}
_lock_release(&s_fd_table_lock);
if (vfs_index < 0) {
continue;
}{...}
if (is_socket_fd) {
if (!socket_select) {
if (esp_vfs_safe_fd_isset(fd, readfds) ||
esp_vfs_safe_fd_isset(fd, writefds) ||
esp_vfs_safe_fd_isset(fd, errorfds)) {
const vfs_entry_t *vfs = s_vfs[vfs_index];
socket_select = vfs->vfs->select->socket_select;
sel_sem.sem = vfs->vfs->select->get_socket_select_semaphore();
}{...}
}{...}
continue;
}{...}
fds_triple_t *item = &vfs_fds_triple[vfs_index];
if (esp_vfs_safe_fd_isset(fd, readfds)) {
item->isset = true;
FD_SET(local_fd, &item->readfds);
FD_CLR(fd, readfds);
ESP_LOGD(TAG, "removing %d from readfds and adding as local FD %d to fd_set of VFS ID %d", fd, local_fd, vfs_index);
}{...}
if (esp_vfs_safe_fd_isset(fd, writefds)) {
item->isset = true;
FD_SET(local_fd, &item->writefds);
FD_CLR(fd, writefds);
ESP_LOGD(TAG, "removing %d from writefds and adding as local FD %d to fd_set of VFS ID %d", fd, local_fd, vfs_index);
}{...}
if (esp_vfs_safe_fd_isset(fd, errorfds)) {
item->isset = true;
FD_SET(local_fd, &item->errorfds);
FD_CLR(fd, errorfds);
ESP_LOGD(TAG, "removing %d from errorfds and adding as local FD %d to fd_set of VFS ID %d", fd, local_fd, vfs_index);
}{...}
}{...}
if (!socket_select) {
sel_sem.is_sem_local = true;
if ((sel_sem.sem = xSemaphoreCreateBinary()) == NULL) {
free(vfs_fds_triple);
__errno_r(r) = ENOMEM;
ESP_LOGD(TAG, "cannot create select semaphore");
return -1;
}{...}
}{...}
void **driver_args = heap_caps_calloc(vfs_count, sizeof(void *), VFS_MALLOC_FLAGS);
if (driver_args == NULL) {
free(vfs_fds_triple);
__errno_r(r) = ENOMEM;
ESP_LOGD(TAG, "calloc is unsuccessful for driver args");
return -1;
}{...}
for (size_t i = 0; i < vfs_count; ++i) {
const vfs_entry_t *vfs = get_vfs_for_index(i);
fds_triple_t *item = &vfs_fds_triple[i];
if (vfs == NULL || vfs->vfs->select == NULL || vfs->vfs->select->start_select == NULL) {
ESP_LOGD(TAG, "start_select function callback for this vfs (s_vfs[%d]) is not defined", vfs->offset);
continue;
}{...}
if (!item->isset) {
continue;
}{...}
ESP_LOGD(TAG, "calling start_select for VFS ID %d with the following local FDs", i);
esp_vfs_log_fd_set("readfds", &item->readfds);
esp_vfs_log_fd_set("writefds", &item->writefds);
esp_vfs_log_fd_set("errorfds", &item->errorfds);
esp_err_t err = vfs->vfs->select->start_select(nfds, &item->readfds, &item->writefds, &item->errorfds, sel_sem,
driver_args + i);
if (err != ESP_OK) {
if (err != ESP_ERR_NOT_SUPPORTED) {
call_end_selects(i, vfs_fds_triple, driver_args);
}{...}
(void) set_global_fd_sets(vfs_fds_triple, vfs_count, readfds, writefds, errorfds);
if (sel_sem.is_sem_local && sel_sem.sem) {
vSemaphoreDelete(sel_sem.sem);
sel_sem.sem = NULL;
}{...}
free(vfs_fds_triple);
free(driver_args);
__errno_r(r) = EINTR;
ESP_LOGD(TAG, "start_select failed: %s", esp_err_to_name(err));
return -1;
}{...}
}{...}
if (socket_select) {
ESP_LOGD(TAG, "calling socket_select with the following FDs");
esp_vfs_log_fd_set("readfds", readfds);
esp_vfs_log_fd_set("writefds", writefds);
esp_vfs_log_fd_set("errorfds", errorfds);
ret = socket_select(nfds, readfds, writefds, errorfds, timeout);
ESP_LOGD(TAG, "socket_select returned %d and the FDs are the following", ret);
esp_vfs_log_fd_set("readfds", readfds);
esp_vfs_log_fd_set("writefds", writefds);
esp_vfs_log_fd_set("errorfds", errorfds);
}{...} else {
if (readfds) {
FD_ZERO(readfds);
}{...}
if (writefds) {
FD_ZERO(writefds);
}{...}
if (errorfds) {
FD_ZERO(errorfds);
}{...}
TickType_t ticks_to_wait = portMAX_DELAY;
if (timeout) {
uint32_t timeout_ms = (timeout->tv_sec * 1000) + (timeout->tv_usec / 1000);
/* ... */
ticks_to_wait = ((timeout_ms + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS) + 1;
ESP_LOGD(TAG, "timeout is %" PRIu32 "ms", timeout_ms);
}{...}
ESP_LOGD(TAG, "waiting without calling socket_select");
xSemaphoreTake(sel_sem.sem, ticks_to_wait);
}{...}
call_end_selects(vfs_count, vfs_fds_triple, driver_args);
if (ret >= 0) {
ret += set_global_fd_sets(vfs_fds_triple, vfs_count, readfds, writefds, errorfds);
}{...}
if (sel_sem.sem) {
if (sel_sem.is_sem_local) {
vSemaphoreDelete(sel_sem.sem);
}{...} else if (socket_select) {
SemaphoreHandle_t *s = sel_sem.sem;
/* ... */
xSemaphoreTake(*s, 0);
}{...}
sel_sem.sem = NULL;
}{...}
_lock_acquire(&s_fd_table_lock);
for (int fd = 0; fd < nfds; ++fd) {
if (s_fd_table[fd].has_pending_close) {
s_fd_table[fd] = FD_TABLE_ENTRY_UNUSED;
}{...}
}{...}
_lock_release(&s_fd_table_lock);
free(vfs_fds_triple);
free(driver_args);
ESP_LOGD(TAG, "esp_vfs_select returns %d", ret);
esp_vfs_log_fd_set("readfds", readfds);
esp_vfs_log_fd_set("writefds", writefds);
esp_vfs_log_fd_set("errorfds", errorfds);
return ret;
}{...}
void esp_vfs_select_triggered(esp_vfs_select_sem_t sem)
{
if (sem.is_sem_local) {
xSemaphoreGive(sem.sem);
}{...} else {
for (int i = 0; i < s_vfs_count; ++i) {
const vfs_entry_t *vfs = s_vfs[i];
if (vfs != NULL
&& vfs->vfs->select != NULL
&& vfs->vfs->select->stop_socket_select != NULL
) {
vfs->vfs->select->stop_socket_select(sem.sem);
break;
}{...}
}{...}
}{...}
}{...}
void esp_vfs_select_triggered_isr(esp_vfs_select_sem_t sem, BaseType_t *woken)
{
if (sem.is_sem_local) {
xSemaphoreGiveFromISR(sem.sem, woken);
}{...} else {
for (int i = 0; i < s_vfs_count; ++i) {
const vfs_entry_t *vfs = s_vfs[i];
if (vfs != NULL
&& vfs->vfs->select != NULL
&& vfs->vfs->select->stop_socket_select_isr != NULL
) {
vfs->vfs->select->stop_socket_select_isr(sem.sem, woken);
break;
}{...}
}{...}
}{...}
}{...}
/* ... */
#endif
#ifdef CONFIG_VFS_SUPPORT_TERMIOS
int tcgetattr(int fd, struct termios *p)
{
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
const int local_fd = get_local_fd(vfs, fd);
struct _reent* r = __getreent();
if (vfs == NULL || local_fd < 0) {
__errno_r(r) = EBADF;
return -1;
}{...}
int ret;
CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, termios, tcgetattr, local_fd, p);
return ret;
}{...}
int tcsetattr(int fd, int optional_actions, const struct termios *p)
{
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
const int local_fd = get_local_fd(vfs, fd);
struct _reent* r = __getreent();
if (vfs == NULL || local_fd < 0) {
__errno_r(r) = EBADF;
return -1;
}{...}
int ret;
CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, termios, tcsetattr, local_fd, optional_actions, p);
return ret;
}{...}
int tcdrain(int fd)
{
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
const int local_fd = get_local_fd(vfs, fd);
struct _reent* r = __getreent();
if (vfs == NULL || local_fd < 0) {
__errno_r(r) = EBADF;
return -1;
}{...}
int ret;
CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, termios, tcdrain, local_fd);
return ret;
}{...}
int tcflush(int fd, int select)
{
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
const int local_fd = get_local_fd(vfs, fd);
struct _reent* r = __getreent();
if (vfs == NULL || local_fd < 0) {
__errno_r(r) = EBADF;
return -1;
}{...}
int ret;
CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, termios, tcflush, local_fd, select);
return ret;
}{...}
int tcflow(int fd, int action)
{
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
const int local_fd = get_local_fd(vfs, fd);
struct _reent* r = __getreent();
if (vfs == NULL || local_fd < 0) {
__errno_r(r) = EBADF;
return -1;
}{...}
int ret;
CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, termios, tcflow, local_fd, action);
return ret;
}{...}
pid_t tcgetsid(int fd)
{
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
const int local_fd = get_local_fd(vfs, fd);
struct _reent* r = __getreent();
if (vfs == NULL || local_fd < 0) {
__errno_r(r) = EBADF;
return -1;
}{...}
int ret;
CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, termios, tcgetsid, local_fd);
return ret;
}{...}
int tcsendbreak(int fd, int duration)
{
const vfs_entry_t* vfs = get_vfs_for_fd(fd);
const int local_fd = get_local_fd(vfs, fd);
struct _reent* r = __getreent();
if (vfs == NULL || local_fd < 0) {
__errno_r(r) = EBADF;
return -1;
}{...}
int ret;
CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, termios, tcsendbreak, local_fd, duration);
return ret;
}{...}
/* ... */#endif
/* ... */
#ifdef CONFIG_VFS_SUPPORT_IO
int _open_r(struct _reent *r, const char * path, int flags, int mode)
__attribute__((alias("esp_vfs_open")));
int _close_r(struct _reent *r, int fd)
__attribute__((alias("esp_vfs_close")));
ssize_t _read_r(struct _reent *r, int fd, void * dst, size_t size)
__attribute__((alias("esp_vfs_read")));
ssize_t _write_r(struct _reent *r, int fd, const void * data, size_t size)
__attribute__((alias("esp_vfs_write")));
ssize_t pread(int fd, void *dst, size_t size, off_t offset)
__attribute__((alias("esp_vfs_pread")));
ssize_t pwrite(int fd, const void *src, size_t size, off_t offset)
__attribute__((alias("esp_vfs_pwrite")));
off_t _lseek_r(struct _reent *r, int fd, off_t size, int mode)
__attribute__((alias("esp_vfs_lseek")));
int _fcntl_r(struct _reent *r, int fd, int cmd, int arg)
__attribute__((alias("esp_vfs_fcntl_r")));
int _fstat_r(struct _reent *r, int fd, struct stat * st)
__attribute__((alias("esp_vfs_fstat")));
int fsync(int fd)
__attribute__((alias("esp_vfs_fsync")));
int ioctl(int fd, int cmd, ...)
__attribute__((alias("esp_vfs_ioctl")));/* ... */
#endif
#ifdef CONFIG_VFS_SUPPORT_SELECT
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout)
__attribute__((alias("esp_vfs_select")));/* ... */
#endif
#ifdef CONFIG_VFS_SUPPORT_DIR
int _stat_r(struct _reent *r, const char * path, struct stat * st)
__attribute__((alias("esp_vfs_stat")));
int _link_r(struct _reent *r, const char* n1, const char* n2)
__attribute__((alias("esp_vfs_link")));
int _unlink_r(struct _reent *r, const char *path)
__attribute__((alias("esp_vfs_unlink")));
int _rename_r(struct _reent *r, const char *src, const char *dst)
__attribute__((alias("esp_vfs_rename")));
int truncate(const char *path, off_t length)
__attribute__((alias("esp_vfs_truncate")));
int ftruncate(int fd, off_t length)
__attribute__((alias("esp_vfs_ftruncate")));
int access(const char *path, int amode)
__attribute__((alias("esp_vfs_access")));
int utime(const char *path, const struct utimbuf *times)
__attribute__((alias("esp_vfs_utime")));
int rmdir(const char* name)
__attribute__((alias("esp_vfs_rmdir")));
int mkdir(const char* name, mode_t mode)
__attribute__((alias("esp_vfs_mkdir")));
DIR* opendir(const char* name)
__attribute__((alias("esp_vfs_opendir")));
int closedir(DIR* pdir)
__attribute__((alias("esp_vfs_closedir")));
int readdir_r(DIR* pdir, struct dirent* entry, struct dirent** out_dirent)
__attribute__((alias("esp_vfs_readdir_r")));
struct dirent* readdir(DIR* pdir)
__attribute__((alias("esp_vfs_readdir")));
long telldir(DIR* pdir)
__attribute__((alias("esp_vfs_telldir")));
void seekdir(DIR* pdir, long loc)
__attribute__((alias("esp_vfs_seekdir")));
void rewinddir(DIR* pdir)
__attribute__((alias("esp_vfs_rewinddir")));/* ... */
#endif
void vfs_include_syscalls_impl(void)
{
}{...}