Select one of the symbols to view example projects that use it.
 
Outline
#include "esp_spiffs.h"
#include "spiffs.h"
#include "spiffs_nucleus.h"
#include "esp_log.h"
#include "esp_partition.h"
#include "esp_image_format.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include <unistd.h>
#include <dirent.h>
#include <sys/errno.h>
#include <sys/fcntl.h>
#include <sys/lock.h>
#include "esp_vfs.h"
#include "esp_err.h"
#include "esp_rom_spiflash.h"
#include "spiffs_api.h"
TAG
spiffs_time_t
vfs_spiffs_dir_t
_efs
esp_spiffs_free(esp_spiffs_t **)
esp_spiffs_by_label(const char *, int *)
esp_spiffs_get_empty(int *)
esp_spiffs_init(const esp_vfs_spiffs_conf_t *)
esp_spiffs_mounted(const char *)
esp_spiffs_info(const char *, size_t *, size_t *)
esp_spiffs_check(const char *)
esp_spiffs_format(const char *)
esp_spiffs_gc(const char *, size_t)
s_vfs_spiffs_dir
s_vfs_spiffs
esp_vfs_spiffs_register(const esp_vfs_spiffs_conf_t *)
esp_vfs_spiffs_unregister(const char *)
spiffs_res_to_errno(s32_t)
spiffs_mode_conv(int)
vfs_spiffs_open(void *, const char *, int, int)
vfs_spiffs_write(void *, int, const void *, size_t)
vfs_spiffs_read(void *, int, void *, size_t)
vfs_spiffs_close(void *, int)
vfs_spiffs_lseek(void *, int, off_t, int)
vfs_spiffs_fstat(void *, int, struct stat *)
vfs_spiffs_fsync(void *, int)
vfs_spiffs_stat(void *, const char *, struct stat *)
vfs_spiffs_rename(void *, const char *, const char *)
vfs_spiffs_unlink(void *, const char *)
vfs_spiffs_opendir(void *, const char *)
vfs_spiffs_closedir(void *, DIR *)
vfs_spiffs_readdir(void *, DIR *)
vfs_spiffs_readdir_r(void *, DIR *, struct dirent *, struct dirent **)
vfs_spiffs_telldir(void *, DIR *)
vfs_spiffs_seekdir(void *, DIR *, long)
vfs_spiffs_mkdir(void *, const char *, mode_t)
vfs_spiffs_rmdir(void *, const char *)
vfs_spiffs_truncate(void *, const char *, off_t)
vfs_spiffs_ftruncate(void *, int, off_t)
vfs_spiffs_link(void *, const char *, const char *)
vfs_spiffs_update_mtime_value(spiffs *, const char *, spiffs_time_t)
vfs_spiffs_utime(void *, const char *, const struct utimbuf *)
vfs_spiffs_update_mtime(spiffs *, spiffs_file)
vfs_spiffs_get_mtime(const spiffs_stat *)
Files
ESP-IDF
components
app_trace
app_update
bootloader_support
bt
cmock
console
cxx
driver
efuse
esp_adc
esp_app_format
esp_bootloader_format
esp_coex
esp_common
esp_driver_ana_cmpr
esp_driver_cam
esp_driver_dac
esp_driver_gpio
esp_driver_gptimer
esp_driver_i2c
esp_driver_i2s
esp_driver_jpeg
esp_driver_ledc
esp_driver_mcpwm
esp_driver_parlio
esp_driver_pcnt
esp_driver_rmt
esp_driver_sdio
esp_driver_sdm
esp_driver_sdmmc
esp_driver_sdspi
esp_driver_spi
esp_driver_tsens
esp_driver_uart
esp_driver_usb_serial_jtag
esp_eth
esp_event
esp_gdbstub
esp_hid
esp_http_client
esp_http_server
esp_https_ota
esp_https_server
esp_hw_support
esp_lcd
esp_local_ctrl
esp_mm
esp_netif
esp_partition
esp_phy
esp_pm
esp_psram
esp_ringbuf
esp_rom
esp_security
esp_system
esp_timer
esp_vfs_console
esp_wifi
esp-tls
espcoredump
hal
heap
http_parser
ieee802154
log
mqtt
newlib
nvs_flash
nvs_sec_provider
openthread
perfmon
protobuf-c
protocomm
pthread
rt
sdmmc
soc
spi_flash
spiffs
include
spiffs
tcp_transport
ulp
unity
vfs
wear_levelling
wifi_provisioning
wpa_supplicant
xtensa
examples
lwIP
FreeRTOS
cJSON
mbedTLS
SourceVuESP-IDF Framework and ExamplesESP-IDFcomponents/spiffs/esp_spiffs.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 *//* ... */ #include "esp_spiffs.h" #include "spiffs.h" #include "spiffs_nucleus.h" #include "esp_log.h" #include "esp_partition.h" #include "esp_image_format.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/semphr.h" #include <unistd.h> #include <dirent.h> #include <sys/errno.h> #include <sys/fcntl.h> #include <sys/lock.h> #include "esp_vfs.h" #include "esp_err.h" #include "esp_rom_spiflash.h" #include "spiffs_api.h"18 includes static const char* TAG = "SPIFFS"; #ifdef CONFIG_SPIFFS_USE_MTIME #ifdef CONFIG_SPIFFS_MTIME_WIDE_64_BITS typedef time_t spiffs_time_t; #else typedef unsigned long spiffs_time_t; #endif _Static_assert(CONFIG_SPIFFS_META_LENGTH >= sizeof(spiffs_time_t), "SPIFFS_META_LENGTH size should be >= sizeof(spiffs_time_t)");/* ... */ #endif //CONFIG_SPIFFS_USE_MTIME _Static_assert(ESP_SPIFFS_PATH_MAX == ESP_VFS_PATH_MAX, "SPIFFS max path length has to be aligned with the VFS max path length"); /** * @brief SPIFFS DIR structure *//* ... */ typedef struct { DIR dir; /*!< VFS DIR struct */ spiffs_DIR d; /*!< SPIFFS DIR struct */ struct dirent e; /*!< Last open dirent */ long offset; /*!< Offset of the current dirent */ char path[SPIFFS_OBJ_NAME_LEN]; /*!< Requested directory name */ }{ ... } vfs_spiffs_dir_t; static int spiffs_res_to_errno(s32_t fr); static int vfs_spiffs_open(void* ctx, const char * path, int flags, int mode); static ssize_t vfs_spiffs_write(void* ctx, int fd, const void * data, size_t size); static ssize_t vfs_spiffs_read(void* ctx, int fd, void * dst, size_t size); static int vfs_spiffs_close(void* ctx, int fd); static off_t vfs_spiffs_lseek(void* ctx, int fd, off_t offset, int mode); static int vfs_spiffs_fstat(void* ctx, int fd, struct stat * st); static int vfs_spiffs_fsync(void* ctx, int fd); #ifdef CONFIG_VFS_SUPPORT_DIR static int vfs_spiffs_stat(void* ctx, const char * path, struct stat * st); static int vfs_spiffs_unlink(void* ctx, const char *path); static int vfs_spiffs_link(void* ctx, const char* n1, const char* n2); static int vfs_spiffs_rename(void* ctx, const char *src, const char *dst); static DIR* vfs_spiffs_opendir(void* ctx, const char* name); static int vfs_spiffs_closedir(void* ctx, DIR* pdir); static struct dirent* vfs_spiffs_readdir(void* ctx, DIR* pdir); static int vfs_spiffs_readdir_r(void* ctx, DIR* pdir, struct dirent* entry, struct dirent** out_dirent); static long vfs_spiffs_telldir(void* ctx, DIR* pdir); static void vfs_spiffs_seekdir(void* ctx, DIR* pdir, long offset); static int vfs_spiffs_mkdir(void* ctx, const char* name, mode_t mode); static int vfs_spiffs_rmdir(void* ctx, const char* name); static int vfs_spiffs_truncate(void* ctx, const char *path, off_t length); static int vfs_spiffs_ftruncate(void* ctx, int fd, off_t length); #ifdef CONFIG_SPIFFS_USE_MTIME static int vfs_spiffs_utime(void *ctx, const char *path, const struct utimbuf *times); #endif // CONFIG_SPIFFS_USE_MTIME/* ... */ #endif // CONFIG_VFS_SUPPORT_DIR static void vfs_spiffs_update_mtime(spiffs *fs, spiffs_file f); static time_t vfs_spiffs_get_mtime(const spiffs_stat* s); static esp_spiffs_t * _efs[CONFIG_SPIFFS_MAX_PARTITIONS]; static void esp_spiffs_free(esp_spiffs_t ** efs) { esp_spiffs_t * e = *efs; if (*efs == NULL) { return; }{...} *efs = NULL; if (e->fs) { SPIFFS_unmount(e->fs); free(e->fs); }{...} vSemaphoreDelete(e->lock); free(e->fds); free(e->cache); free(e->work); free(e); }{ ... } static esp_err_t esp_spiffs_by_label(const char* label, int * index){ int i; esp_spiffs_t * p; for (i = 0; i < CONFIG_SPIFFS_MAX_PARTITIONS; i++) { p = _efs[i]; if (p) { if (!label && !p->by_label) { *index = i; return ESP_OK; }{...} if (label && p->by_label && strncmp(label, p->partition->label, 17) == 0) { *index = i; return ESP_OK; }{...} }{...} }{...} return ESP_ERR_NOT_FOUND; }{ ... } static esp_err_t esp_spiffs_get_empty(int * index){ int i; for (i = 0; i < CONFIG_SPIFFS_MAX_PARTITIONS; i++) { if (_efs[i] == NULL) { *index = i; return ESP_OK; }{...} }{...} return ESP_ERR_NOT_FOUND; }{ ... } static esp_err_t esp_spiffs_init(const esp_vfs_spiffs_conf_t* conf) { int index; //find if such partition is already mounted if (esp_spiffs_by_label(conf->partition_label, &index) == ESP_OK) { return ESP_ERR_INVALID_STATE; }{...} if (esp_spiffs_get_empty(&index) != ESP_OK) { ESP_LOGE(TAG, "max mounted partitions reached"); return ESP_ERR_INVALID_STATE; }{...} uint32_t flash_page_size = g_rom_flashchip.page_size; uint32_t log_page_size = CONFIG_SPIFFS_PAGE_SIZE; if (log_page_size % flash_page_size != 0) { ESP_LOGE(TAG, "SPIFFS_PAGE_SIZE is not multiple of flash chip page size (%" PRIu32 ")", flash_page_size); return ESP_ERR_INVALID_ARG; }{...} esp_partition_subtype_t subtype = conf->partition_label ? ESP_PARTITION_SUBTYPE_ANY : ESP_PARTITION_SUBTYPE_DATA_SPIFFS; const esp_partition_t* partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, subtype, conf->partition_label); if (!partition) { ESP_LOGE(TAG, "spiffs partition could not be found"); return ESP_ERR_NOT_FOUND; }{...} if (partition->encrypted) { ESP_LOGE(TAG, "spiffs can not run on encrypted partition"); return ESP_ERR_INVALID_STATE; }{...} const size_t flash_erase_sector_size = g_rom_flashchip.sector_size; /* Older versions of IDF allowed creating misaligned data partitions. * This would result in hard-to-diagnose SPIFFS failures due to failing erase operations. *//* ... */ if (partition->address % flash_erase_sector_size != 0) { ESP_LOGE(TAG, "spiffs partition is not aligned to flash sector size, please check the partition table"); /* No return intentional to avoid accidentally breaking applications * which used misaligned read-only SPIFFS partitions. *//* ... */ }{...} /* Check if the SPIFFS internal data types are wide enough. * Casting -1 to the unsigned type produces the maximum value the type can hold. * All the checks here are based on comments for the said data types in spiffs_config.h. *//* ... */ if (partition->size / flash_erase_sector_size > (spiffs_block_ix) -1) { ESP_LOGE(TAG, "spiffs partition is too large for spiffs_block_ix type"); return ESP_ERR_INVALID_ARG; }{...} if (partition->size / log_page_size > (spiffs_page_ix) -1) { /* For 256 byte pages the largest partition is 16MB, but larger partitions can be supported * by increasing the page size (reducing the number of pages). *//* ... */ ESP_LOGE(TAG, "spiffs partition is too large for spiffs_page_ix type. Please increase CONFIG_SPIFFS_PAGE_SIZE."); return ESP_ERR_INVALID_ARG; }{...} if (2 + 2 * (partition->size / (2 * log_page_size)) > (spiffs_obj_id) -1) { ESP_LOGE(TAG, "spiffs partition is too large for spiffs_obj_id type. Please increase CONFIG_SPIFFS_PAGE_SIZE."); return ESP_ERR_INVALID_ARG; }{...} if (partition->size / log_page_size - 1 > (spiffs_span_ix) -1) { ESP_LOGE(TAG, "spiffs partition is too large for spiffs_span_ix type. Please increase CONFIG_SPIFFS_PAGE_SIZE."); return ESP_ERR_INVALID_ARG; }{...} esp_spiffs_t * efs = calloc(1, sizeof(esp_spiffs_t)); if (efs == NULL) { ESP_LOGE(TAG, "esp_spiffs could not be malloced"); return ESP_ERR_NO_MEM; }{...} efs->cfg.hal_erase_f = spiffs_api_erase; efs->cfg.hal_read_f = spiffs_api_read; efs->cfg.hal_write_f = spiffs_api_write; efs->cfg.log_block_size = flash_erase_sector_size; efs->cfg.log_page_size = log_page_size; efs->cfg.phys_addr = 0; efs->cfg.phys_erase_block = flash_erase_sector_size; efs->cfg.phys_size = partition->size; efs->by_label = conf->partition_label != NULL; efs->lock = xSemaphoreCreateMutex(); if (efs->lock == NULL) { ESP_LOGE(TAG, "mutex lock could not be created"); esp_spiffs_free(&efs); return ESP_ERR_NO_MEM; }{...} efs->fds_sz = conf->max_files * sizeof(spiffs_fd); efs->fds = calloc(1, efs->fds_sz); if (efs->fds == NULL) { ESP_LOGE(TAG, "fd buffer could not be allocated"); esp_spiffs_free(&efs); return ESP_ERR_NO_MEM; }{...} #if SPIFFS_CACHE efs->cache_sz = sizeof(spiffs_cache) + conf->max_files * (sizeof(spiffs_cache_page) + efs->cfg.log_page_size); efs->cache = calloc(1, efs->cache_sz); if (efs->cache == NULL) { ESP_LOGE(TAG, "cache buffer could not be allocated"); esp_spiffs_free(&efs); return ESP_ERR_NO_MEM; }{...} #endif/* ... */ const uint32_t work_sz = efs->cfg.log_page_size * 2; efs->work = calloc(1, work_sz); if (efs->work == NULL) { ESP_LOGE(TAG, "work buffer could not be allocated"); esp_spiffs_free(&efs); return ESP_ERR_NO_MEM; }{...} efs->fs = calloc(1, sizeof(spiffs)); if (efs->fs == NULL) { ESP_LOGE(TAG, "spiffs could not be allocated"); esp_spiffs_free(&efs); return ESP_ERR_NO_MEM; }{...} efs->fs->user_data = (void *)efs; efs->partition = partition; s32_t res = SPIFFS_mount(efs->fs, &efs->cfg, efs->work, efs->fds, efs->fds_sz, efs->cache, efs->cache_sz, spiffs_api_check); if (conf->format_if_mount_failed && res != SPIFFS_OK) { ESP_LOGW(TAG, "mount failed, %" PRId32 ". formatting...", SPIFFS_errno(efs->fs)); SPIFFS_clearerr(efs->fs); res = SPIFFS_format(efs->fs); if (res != SPIFFS_OK) { ESP_LOGE(TAG, "format failed, %" PRId32, SPIFFS_errno(efs->fs)); SPIFFS_clearerr(efs->fs); esp_spiffs_free(&efs); return ESP_FAIL; }{...} res = SPIFFS_mount(efs->fs, &efs->cfg, efs->work, efs->fds, efs->fds_sz, efs->cache, efs->cache_sz, spiffs_api_check); }{...} if (res != SPIFFS_OK) { ESP_LOGE(TAG, "mount failed, %" PRId32, SPIFFS_errno(efs->fs)); SPIFFS_clearerr(efs->fs); esp_spiffs_free(&efs); return ESP_FAIL; }{...} _efs[index] = efs; return ESP_OK; }{ ... } bool esp_spiffs_mounted(const char* partition_label) { int index; if (esp_spiffs_by_label(partition_label, &index) != ESP_OK) { return false; }{...} return (SPIFFS_mounted(_efs[index]->fs)); }{ ... } esp_err_t esp_spiffs_info(const char* partition_label, size_t *total_bytes, size_t *used_bytes) { int index; if (esp_spiffs_by_label(partition_label, &index) != ESP_OK) { return ESP_ERR_INVALID_STATE; }{...} SPIFFS_info(_efs[index]->fs, (uint32_t *)total_bytes, (uint32_t *)used_bytes); return ESP_OK; }{ ... } esp_err_t esp_spiffs_check(const char* partition_label) { int index; if (esp_spiffs_by_label(partition_label, &index) != ESP_OK) { return ESP_ERR_INVALID_STATE; }{...} if (SPIFFS_check(_efs[index]->fs) != SPIFFS_OK) { int spiffs_res = SPIFFS_errno(_efs[index]->fs); ESP_LOGE(TAG, "SPIFFS_check failed (%d)", spiffs_res); errno = spiffs_res_to_errno(SPIFFS_errno(_efs[index]->fs)); SPIFFS_clearerr(_efs[index]->fs); return ESP_FAIL; }{...} return ESP_OK; }{ ... } esp_err_t esp_spiffs_format(const char* partition_label) { bool partition_was_mounted = false; int index; /* If the partition is not mounted, need to create SPIFFS structures * and mount the partition, unmount, format, delete SPIFFS structures. * See SPIFFS wiki for the reason why. *//* ... */ esp_err_t err = esp_spiffs_by_label(partition_label, &index); if (err != ESP_OK) { esp_vfs_spiffs_conf_t conf = { .format_if_mount_failed = true, .partition_label = partition_label, .max_files = 1 }{...}; err = esp_spiffs_init(&conf); if (err != ESP_OK) { return err; }{...} err = esp_spiffs_by_label(partition_label, &index); assert(err == ESP_OK && "failed to get index of the partition just mounted"); }{...} else if (SPIFFS_mounted(_efs[index]->fs)) { partition_was_mounted = true; }{...} SPIFFS_unmount(_efs[index]->fs); s32_t res = SPIFFS_format(_efs[index]->fs); if (res != SPIFFS_OK) { ESP_LOGE(TAG, "format failed, %" PRId32, SPIFFS_errno(_efs[index]->fs)); SPIFFS_clearerr(_efs[index]->fs); /* If the partition was previously mounted, but format failed, don't * try to mount the partition back (it will probably fail). On the * other hand, if it was not mounted, need to clean up. *//* ... */ if (!partition_was_mounted) { esp_spiffs_free(&_efs[index]); }{...} return ESP_FAIL; }{...} if (partition_was_mounted) { res = SPIFFS_mount(_efs[index]->fs, &_efs[index]->cfg, _efs[index]->work, _efs[index]->fds, _efs[index]->fds_sz, _efs[index]->cache, _efs[index]->cache_sz, spiffs_api_check); if (res != SPIFFS_OK) { ESP_LOGE(TAG, "mount failed, %" PRId32, SPIFFS_errno(_efs[index]->fs)); SPIFFS_clearerr(_efs[index]->fs); return ESP_FAIL; }{...} }{...} else { esp_spiffs_free(&_efs[index]); }{...} return ESP_OK; }{ ... } esp_err_t esp_spiffs_gc(const char* partition_label, size_t size_to_gc) { int index; if (esp_spiffs_by_label(partition_label, &index) != ESP_OK) { return ESP_ERR_INVALID_STATE; }{...} int res = SPIFFS_gc(_efs[index]->fs, size_to_gc); if (res != SPIFFS_OK) { ESP_LOGE(TAG, "SPIFFS_gc failed, %d", res); SPIFFS_clearerr(_efs[index]->fs); if (res == SPIFFS_ERR_FULL) { return ESP_ERR_NOT_FINISHED; }{...} return ESP_FAIL; }{...} return ESP_OK; }{ ... } #ifdef CONFIG_VFS_SUPPORT_DIR static const esp_vfs_dir_ops_t s_vfs_spiffs_dir = { .stat_p = &vfs_spiffs_stat, .link_p = &vfs_spiffs_link, .unlink_p = &vfs_spiffs_unlink, .rename_p = &vfs_spiffs_rename, .opendir_p = &vfs_spiffs_opendir, .closedir_p = &vfs_spiffs_closedir, .readdir_p = &vfs_spiffs_readdir, .readdir_r_p = &vfs_spiffs_readdir_r, .seekdir_p = &vfs_spiffs_seekdir, .telldir_p = &vfs_spiffs_telldir, .mkdir_p = &vfs_spiffs_mkdir, .rmdir_p = &vfs_spiffs_rmdir, .truncate_p = &vfs_spiffs_truncate, .ftruncate_p = &vfs_spiffs_ftruncate, #ifdef CONFIG_SPIFFS_USE_MTIME .utime_p = &vfs_spiffs_utime, #else .utime_p = NULL, #endif // CONFIG_SPIFFS_USE_MTIME }{...}; /* ... */#endif // CONFIG_VFS_SUPPORT_DIR static const esp_vfs_fs_ops_t s_vfs_spiffs = { .write_p = &vfs_spiffs_write, .lseek_p = &vfs_spiffs_lseek, .read_p = &vfs_spiffs_read, .open_p = &vfs_spiffs_open, .close_p = &vfs_spiffs_close, .fstat_p = &vfs_spiffs_fstat, .fsync_p = &vfs_spiffs_fsync, #ifdef CONFIG_VFS_SUPPORT_DIR .dir = &s_vfs_spiffs_dir, #endif // CONFIG_VFS_SUPPORT_DIR }{...}; esp_err_t esp_vfs_spiffs_register(const esp_vfs_spiffs_conf_t * conf) { assert(conf->base_path); esp_err_t err = esp_spiffs_init(conf); if (err != ESP_OK) { return err; }{...} int index; if (esp_spiffs_by_label(conf->partition_label, &index) != ESP_OK) { return ESP_ERR_INVALID_STATE; }{...} int vfs_flags = ESP_VFS_FLAG_CONTEXT_PTR | ESP_VFS_FLAG_STATIC; if (_efs[index]->partition->readonly) { vfs_flags |= ESP_VFS_FLAG_READONLY_FS; }{...} strlcat(_efs[index]->base_path, conf->base_path, ESP_VFS_PATH_MAX + 1); err = esp_vfs_register_fs(conf->base_path, &s_vfs_spiffs, vfs_flags, _efs[index]); if (err != ESP_OK) { esp_spiffs_free(&_efs[index]); return err; }{...} return ESP_OK; }{ ... } esp_err_t esp_vfs_spiffs_unregister(const char* partition_label) { int index; if (esp_spiffs_by_label(partition_label, &index) != ESP_OK) { return ESP_ERR_INVALID_STATE; }{...} esp_err_t err = esp_vfs_unregister(_efs[index]->base_path); if (err != ESP_OK) { return err; }{...} esp_spiffs_free(&_efs[index]); return ESP_OK; }{ ... } static int spiffs_res_to_errno(s32_t fr) { switch(fr) { case SPIFFS_OK : return 0;... case SPIFFS_ERR_NOT_MOUNTED : return ENODEV;... case SPIFFS_ERR_NOT_A_FS : return ENODEV;... case SPIFFS_ERR_FULL : return ENOSPC;... case SPIFFS_ERR_BAD_DESCRIPTOR : return EBADF;... case SPIFFS_ERR_MOUNTED : return EEXIST;... case SPIFFS_ERR_FILE_EXISTS : return EEXIST;... case SPIFFS_ERR_NOT_FOUND : return ENOENT;... case SPIFFS_ERR_NOT_A_FILE : return ENOENT;... case SPIFFS_ERR_DELETED : return ENOENT;... case SPIFFS_ERR_FILE_DELETED : return ENOENT;... case SPIFFS_ERR_NAME_TOO_LONG : return ENAMETOOLONG;... case SPIFFS_ERR_RO_NOT_IMPL : return EROFS;... case SPIFFS_ERR_RO_ABORTED_OPERATION : return EROFS;... default : return EIO;... }{...} return ENOTSUP; }{ ... } static int spiffs_mode_conv(int m) { int res = 0; int acc_mode = m & O_ACCMODE; if (acc_mode == O_RDONLY) { res |= SPIFFS_O_RDONLY; }{...} else if (acc_mode == O_WRONLY) { res |= SPIFFS_O_WRONLY; }{...} else if (acc_mode == O_RDWR) { res |= SPIFFS_O_RDWR; }{...} if ((m & O_CREAT) && (m & O_EXCL)) { res |= SPIFFS_O_CREAT | SPIFFS_O_EXCL; }{...} else if ((m & O_CREAT) && (m & O_TRUNC)) { res |= SPIFFS_O_CREAT | SPIFFS_O_TRUNC; }{...} if (m & O_APPEND) { res |= SPIFFS_O_CREAT | SPIFFS_O_APPEND; }{...} return res; }{ ... } static int vfs_spiffs_open(void* ctx, const char * path, int flags, int mode) { assert(path); esp_spiffs_t * efs = (esp_spiffs_t *)ctx; int spiffs_flags = spiffs_mode_conv(flags); int fd = SPIFFS_open(efs->fs, path, spiffs_flags, mode); if (fd < 0) { errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs)); SPIFFS_clearerr(efs->fs); return -1; }{...} if (!(spiffs_flags & SPIFFS_RDONLY)) { vfs_spiffs_update_mtime(efs->fs, fd); }{...} return fd; }{ ... } static ssize_t vfs_spiffs_write(void* ctx, int fd, const void * data, size_t size) { esp_spiffs_t * efs = (esp_spiffs_t *)ctx; ssize_t res = SPIFFS_write(efs->fs, fd, (void *)data, size); if (res < 0) { errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs)); SPIFFS_clearerr(efs->fs); return -1; }{...} return res; }{ ... } static ssize_t vfs_spiffs_read(void* ctx, int fd, void * dst, size_t size) { esp_spiffs_t * efs = (esp_spiffs_t *)ctx; ssize_t res = SPIFFS_read(efs->fs, fd, dst, size); if (res < 0) { errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs)); SPIFFS_clearerr(efs->fs); return -1; }{...} return res; }{ ... } static int vfs_spiffs_close(void* ctx, int fd) { esp_spiffs_t * efs = (esp_spiffs_t *)ctx; int res = SPIFFS_close(efs->fs, fd); if (res < 0) { errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs)); SPIFFS_clearerr(efs->fs); return -1; }{...} return res; }{ ... } static off_t vfs_spiffs_lseek(void* ctx, int fd, off_t offset, int mode) { esp_spiffs_t * efs = (esp_spiffs_t *)ctx; off_t res = SPIFFS_lseek(efs->fs, fd, offset, mode); if (res < 0) { errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs)); SPIFFS_clearerr(efs->fs); return -1; }{...} return res; }{ ... } static int vfs_spiffs_fstat(void* ctx, int fd, struct stat * st) { assert(st); spiffs_stat s; esp_spiffs_t * efs = (esp_spiffs_t *)ctx; off_t res = SPIFFS_fstat(efs->fs, fd, &s); if (res < 0) { errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs)); SPIFFS_clearerr(efs->fs); return -1; }{...} memset(st, 0, sizeof(*st)); st->st_size = s.size; st->st_mode = S_IRWXU | S_IRWXG | S_IRWXO | S_IFREG; st->st_mtime = vfs_spiffs_get_mtime(&s); st->st_atime = 0; st->st_ctime = 0; return res; }{ ... } static int vfs_spiffs_fsync(void* ctx, int fd) { esp_spiffs_t * efs = (esp_spiffs_t *)ctx; int res = SPIFFS_fflush(efs->fs, fd); if (res < 0) { errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs)); SPIFFS_clearerr(efs->fs); return -1; }{...} return ESP_OK; }{ ... } #ifdef CONFIG_VFS_SUPPORT_DIR static int vfs_spiffs_stat(void* ctx, const char * path, struct stat * st) { assert(path); assert(st); spiffs_stat s; esp_spiffs_t * efs = (esp_spiffs_t *)ctx; off_t res = SPIFFS_stat(efs->fs, path, &s); if (res < 0) { errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs)); SPIFFS_clearerr(efs->fs); return -1; }{...} memset(st, 0, sizeof(*st)); st->st_size = s.size; st->st_mode = S_IRWXU | S_IRWXG | S_IRWXO; st->st_mode |= (s.type == SPIFFS_TYPE_DIR)?S_IFDIR:S_IFREG; st->st_mtime = vfs_spiffs_get_mtime(&s); st->st_atime = 0; st->st_ctime = 0; return res; }{ ... } static int vfs_spiffs_rename(void* ctx, const char *src, const char *dst) { assert(src); assert(dst); esp_spiffs_t * efs = (esp_spiffs_t *)ctx; int res = SPIFFS_rename(efs->fs, src, dst); if (res < 0) { errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs)); SPIFFS_clearerr(efs->fs); return -1; }{...} return res; }{ ... } static int vfs_spiffs_unlink(void* ctx, const char *path) { assert(path); esp_spiffs_t * efs = (esp_spiffs_t *)ctx; int res = SPIFFS_remove(efs->fs, path); if (res < 0) { errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs)); SPIFFS_clearerr(efs->fs); return -1; }{...} return res; }{ ... } static DIR* vfs_spiffs_opendir(void* ctx, const char* name) { assert(name); esp_spiffs_t * efs = (esp_spiffs_t *)ctx; vfs_spiffs_dir_t * dir = calloc(1, sizeof(vfs_spiffs_dir_t)); if (!dir) { errno = ENOMEM; return NULL; }{...} if (!SPIFFS_opendir(efs->fs, name, &dir->d)) { free(dir); errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs)); SPIFFS_clearerr(efs->fs); return NULL; }{...} dir->offset = 0; strlcpy(dir->path, name, SPIFFS_OBJ_NAME_LEN); return (DIR*) dir; }{ ... } static int vfs_spiffs_closedir(void* ctx, DIR* pdir) { assert(pdir); esp_spiffs_t * efs = (esp_spiffs_t *)ctx; vfs_spiffs_dir_t * dir = (vfs_spiffs_dir_t *)pdir; int res = SPIFFS_closedir(&dir->d); free(dir); if (res < 0) { errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs)); SPIFFS_clearerr(efs->fs); return -1; }{...} return res; }{ ... } static struct dirent* vfs_spiffs_readdir(void* ctx, DIR* pdir) { assert(pdir); vfs_spiffs_dir_t * dir = (vfs_spiffs_dir_t *)pdir; struct dirent* out_dirent; int err = vfs_spiffs_readdir_r(ctx, pdir, &dir->e, &out_dirent); if (err != 0) { errno = err; return NULL; }{...} return out_dirent; }{ ... } static int vfs_spiffs_readdir_r(void* ctx, DIR* pdir, struct dirent* entry, struct dirent** out_dirent) { assert(pdir); esp_spiffs_t * efs = (esp_spiffs_t *)ctx; vfs_spiffs_dir_t * dir = (vfs_spiffs_dir_t *)pdir; struct spiffs_dirent out; size_t plen; char * item_name; do { if (SPIFFS_readdir(&dir->d, &out) == 0) { errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs)); SPIFFS_clearerr(efs->fs); if (!errno) { *out_dirent = NULL; }{...} return errno; }{...} item_name = (char *)out.name; plen = strlen(dir->path); }{...} while ((plen > 1) && (strncasecmp(dir->path, (const char*)out.name, plen) || out.name[plen] != '/' || !out.name[plen + 1])); if (plen > 1) { item_name += plen + 1; }{...} else if (item_name[0] == '/') { item_name++; }{...} entry->d_ino = 0; entry->d_type = out.type; strncpy(entry->d_name, item_name, SPIFFS_OBJ_NAME_LEN); entry->d_name[SPIFFS_OBJ_NAME_LEN - 1] = '\0'; dir->offset++; *out_dirent = entry; return 0; }{ ... } static long vfs_spiffs_telldir(void* ctx, DIR* pdir) { assert(pdir); vfs_spiffs_dir_t * dir = (vfs_spiffs_dir_t *)pdir; return dir->offset; }{ ... } static void vfs_spiffs_seekdir(void* ctx, DIR* pdir, long offset) { assert(pdir); esp_spiffs_t * efs = (esp_spiffs_t *)ctx; vfs_spiffs_dir_t * dir = (vfs_spiffs_dir_t *)pdir; struct spiffs_dirent tmp; if (offset < dir->offset) { //rewind dir SPIFFS_closedir(&dir->d); if (!SPIFFS_opendir(efs->fs, NULL, &dir->d)) { errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs)); SPIFFS_clearerr(efs->fs); return; }{...} dir->offset = 0; }{...} while (dir->offset < offset) { if (SPIFFS_readdir(&dir->d, &tmp) == 0) { errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs)); SPIFFS_clearerr(efs->fs); return; }{...} size_t plen = strlen(dir->path); if (plen > 1) { if (strncasecmp(dir->path, (const char *)tmp.name, plen) || tmp.name[plen] != '/' || !tmp.name[plen+1]) { continue; }{...} }{...} dir->offset++; }{...} }{ ... } static int vfs_spiffs_mkdir(void* ctx, const char* name, mode_t mode) { errno = ENOTSUP; return -1; }{ ... } static int vfs_spiffs_rmdir(void* ctx, const char* name) { errno = ENOTSUP; return -1; }{ ... } static int vfs_spiffs_truncate(void* ctx, const char *path, off_t length) { assert(path); esp_spiffs_t * efs = (esp_spiffs_t *)ctx; int fd = SPIFFS_open(efs->fs, path, SPIFFS_WRONLY, 0); if (fd < 0) { goto err; }{...} int res = SPIFFS_ftruncate(efs->fs, fd, length); if (res < 0) { (void)SPIFFS_close(efs->fs, fd); goto err; }{...} res = SPIFFS_close(efs->fs, fd); if (res < 0) { goto err; }{...} return res; err: errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs)); SPIFFS_clearerr(efs->fs); return -1; }{ ... } static int vfs_spiffs_ftruncate(void* ctx, int fd, off_t length) { esp_spiffs_t * efs = (esp_spiffs_t *)ctx; int res = SPIFFS_ftruncate(efs->fs, fd, length); if (res < 0) { errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs)); SPIFFS_clearerr(efs->fs); return -1; }{...} return res; }{ ... } static int vfs_spiffs_link(void* ctx, const char* n1, const char* n2) { errno = ENOTSUP; return -1; }{ ... } #ifdef CONFIG_SPIFFS_USE_MTIME static int vfs_spiffs_update_mtime_value(spiffs *fs, const char *path, spiffs_time_t t) { int ret = SPIFFS_OK; spiffs_stat s; if (CONFIG_SPIFFS_META_LENGTH > sizeof(t)) { ret = SPIFFS_stat(fs, path, &s); }{...} if (ret == SPIFFS_OK) { memcpy(s.meta, &t, sizeof(t)); ret = SPIFFS_update_meta(fs, path, s.meta); }{...} if (ret != SPIFFS_OK) { ESP_LOGW(TAG, "Failed to update mtime (%d)", ret); }{...} return ret; }{ ... } /* ... */#endif //CONFIG_SPIFFS_USE_MTIME #ifdef CONFIG_SPIFFS_USE_MTIME static int vfs_spiffs_utime(void *ctx, const char *path, const struct utimbuf *times) { assert(path); esp_spiffs_t *efs = (esp_spiffs_t *) ctx; spiffs_time_t t; if (times) { t = (spiffs_time_t)times->modtime; }{...} else { // use current time t = (spiffs_time_t)time(NULL); }{...} int ret = vfs_spiffs_update_mtime_value(efs->fs, path, t); if (ret != SPIFFS_OK) { errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs)); SPIFFS_clearerr(efs->fs); return -1; }{...} return 0; }{ ... } /* ... */#endif //CONFIG_SPIFFS_USE_MTIME /* ... */ #endif // CONFIG_VFS_SUPPORT_DIR static void vfs_spiffs_update_mtime(spiffs *fs, spiffs_file fd) { #ifdef CONFIG_SPIFFS_USE_MTIME spiffs_time_t t = (spiffs_time_t)time(NULL); spiffs_stat s; int ret = SPIFFS_OK; if (CONFIG_SPIFFS_META_LENGTH > sizeof(t)) { ret = SPIFFS_fstat(fs, fd, &s); }{...} if (ret == SPIFFS_OK) { memcpy(s.meta, &t, sizeof(t)); ret = SPIFFS_fupdate_meta(fs, fd, s.meta); }{...} if (ret != SPIFFS_OK) { ESP_LOGW(TAG, "Failed to update mtime (%d)", ret); }{...} /* ... */#endif //CONFIG_SPIFFS_USE_MTIME }{ ... } static time_t vfs_spiffs_get_mtime(const spiffs_stat* s) { #ifdef CONFIG_SPIFFS_USE_MTIME spiffs_time_t t = 0; memcpy(&t, s->meta, sizeof(t));/* ... */ #else time_t t = 0; #endif return (time_t)t; }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.