Select one of the symbols to view example projects that use it.
 
Outline
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "esp_err.h"
#include "esp_log.h"
#include "esp_system.h"
#include "esp_littlefs.h"
TAG
app_main()
Files
loading...
SourceVuESP-IDF Framework and Exampleslittlefs samplemain/esp_littlefs_example.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2023 Brian Pugh * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 *//* ... */ #include <stdio.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> #include "esp_err.h" #include "esp_log.h" #include "esp_system.h" #include "esp_littlefs.h"8 includes static const char *TAG = "esp_littlefs"; void app_main(void) { ESP_LOGI(TAG, "Initializing LittleFS"); esp_vfs_littlefs_conf_t conf = { .base_path = "/littlefs", .partition_label = "storage", .format_if_mount_failed = true, .dont_mount = false, }{...}; // Use settings defined above to initialize and mount LittleFS filesystem. // Note: esp_vfs_littlefs_register is an all-in-one convenience function. esp_err_t ret = esp_vfs_littlefs_register(&conf); if (ret != ESP_OK) { if (ret == ESP_FAIL) { ESP_LOGE(TAG, "Failed to mount or format filesystem"); }{...} else if (ret == ESP_ERR_NOT_FOUND) { ESP_LOGE(TAG, "Failed to find LittleFS partition"); }{...} else { ESP_LOGE(TAG, "Failed to initialize LittleFS (%s)", esp_err_to_name(ret)); }{...} return; }{...} size_t total = 0, used = 0; ret = esp_littlefs_info(conf.partition_label, &total, &used); if (ret != ESP_OK) { ESP_LOGE(TAG, "Failed to get LittleFS partition information (%s)", esp_err_to_name(ret)); esp_littlefs_format(conf.partition_label); }{...} else { ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used); }{...} // Use POSIX and C standard library functions to work with files. // First create a file. ESP_LOGI(TAG, "Opening file"); FILE *f = fopen("/littlefs/hello.txt", "w"); if (f == NULL) { ESP_LOGE(TAG, "Failed to open file for writing"); return; }{...} fprintf(f, "Hello World!\n"); fclose(f); ESP_LOGI(TAG, "File written"); // Check if destination file exists before renaming struct stat st; if (stat("/littlefs/foo.txt", &st) == 0) { // Delete it if it exists unlink("/littlefs/foo.txt"); }{...} // Rename original file ESP_LOGI(TAG, "Renaming file"); if (rename("/littlefs/hello.txt", "/littlefs/foo.txt") != 0) { ESP_LOGE(TAG, "Rename failed"); return; }{...} // Open renamed file for reading ESP_LOGI(TAG, "Reading file"); f = fopen("/littlefs/foo.txt", "r"); if (f == NULL) { ESP_LOGE(TAG, "Failed to open file for reading"); return; }{...} char line[128] = {0}; fgets(line, sizeof(line), f); fclose(f); // strip newline char* pos = strpbrk(line, "\r\n"); if (pos) { *pos = '\0'; }{...} ESP_LOGI(TAG, "Read from file: '%s'", line); ESP_LOGI(TAG, "Reading from flashed filesystem example.txt"); f = fopen("/littlefs/example.txt", "r"); if (f == NULL) { ESP_LOGE(TAG, "Failed to open file for reading"); return; }{...} fgets(line, sizeof(line), f); fclose(f); // strip newline pos = strpbrk(line, "\r\n"); if (pos) { *pos = '\0'; }{...} ESP_LOGI(TAG, "Read from file: '%s'", line); // All done, unmount partition and disable LittleFS esp_vfs_littlefs_unregister(conf.partition_label); ESP_LOGI(TAG, "LittleFS unmounted"); }{ ... }
Details