1
6
7
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
44
45
46
47
48
49
50
54
55
56
57
58
59
60
61
62
66
67
68
69
70
71
72
73
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
125
126
127
128
129
130
131
132
133
134
135
139
140
141
142
143
144
145
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
194
195
196
205
206
207
/* ... */
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include "esp_vfs.h"
#include "esp_vfs_fat.h"
#include "sdkconfig.h"10 includes
static const char *TAG = "example";
static const char *base_path = "/spiflash";
static const char *filename = "/spiflash/hello.txt";
static void list_dir(const char *path);
static void clean_dir(const char *path);
void app_main(void)
{
ESP_LOGI(TAG, "Mounting FAT filesystem");
const esp_vfs_fat_mount_config_t mount_config = {
.max_files = 4,
.format_if_mount_failed = true,
.allocation_unit_size = CONFIG_WL_SECTOR_SIZE,
.use_one_fat = false,
}{...};
wl_handle_t wl_handle = WL_INVALID_HANDLE;
esp_err_t err = ESP_OK;
err = esp_vfs_fat_spiflash_mount_rw_wl(base_path, "storage", &mount_config, &wl_handle);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
return;
}{...}
clean_dir(base_path);
ESP_LOGI(TAG, "Creating a file");
int fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0);
if (fd < 0) {
ESP_LOGE(TAG, "Failed to open file for writing");
return;
}{...}
ESP_LOGI(TAG, "Writing to the file");
const char *text = "Hello World!\n";
write(fd, text, strlen(text));
struct stat info;
if (stat(filename, &info) < 0) {
ESP_LOGE(TAG, "Failed to stat file: %s", strerror(errno));
close(fd);
return;
}{...}
ESP_LOGI(
TAG,
"File stats:\n"
"\tFile size: %ld bytes\n"
"\tFile modification time: %s",
info.st_size,
ctime(&info.st_mtime)
);
ESP_LOGI(TAG, "Wait for 3 seconds");
sleep(3);
ESP_LOGI(TAG, "Write more to the file");
write(fd, text, strlen(text));
ESP_LOGI(TAG, "Force cached data and metadata to the filesystem");
fsync(fd);
if (stat(filename, &info) < 0) {
ESP_LOGE(TAG, "Failed to stat file: %s", strerror(errno));
close(fd);
return;
}{...}
ESP_LOGI(
TAG,
"File stats:\n"
"\tFile size: %ld bytes\n"
"\tFile modification time: %s",
info.st_size,
ctime(&info.st_mtime)
);
ESP_LOGI(TAG, "Go to the beginning of the file");
lseek(fd, 0, SEEK_SET);
ESP_LOGI(TAG, "Reading from file:");
char buf[128] = {0};
ssize_t len = read(fd, buf, sizeof(buf) - 1);
if (len < 0) {
ESP_LOGE(TAG, "Failed to read file: %s", strerror(errno));
close(fd);
return;
}{...}
printf("%.*s\n", len, buf);
ESP_LOGI(TAG, "Closing file");
close(fd);
list_dir(base_path);
ESP_LOGI(TAG, "Creating a new directory");
if (mkdir("/spiflash/new_dir", 0777) < 0) {
ESP_LOGE(TAG, "Failed to create a new directory: %s", strerror(errno));
return;
}{...}
list_dir(base_path);
ESP_LOGI(TAG, "Rename a file");
if (rename(filename, "/spiflash/new_dir/hello_renamed.txt") < 0) {
ESP_LOGE(TAG, "Failed to rename file: %s", strerror(errno));
return;
}{...}
list_dir(base_path);
list_dir("/spiflash/new_dir");
ESP_LOGI(TAG, "Unmounting FAT filesystem");
ESP_ERROR_CHECK(esp_vfs_fat_spiflash_unmount_rw_wl(base_path, wl_handle));
ESP_LOGI(TAG, "Done");
}{ ... }
void list_dir(const char *path)
{
ESP_LOGI(TAG, "Listing files in %s:", path);
DIR *dir = opendir(path);
if (!dir) {
ESP_LOGE(TAG, "Failed to open directory: %s", strerror(errno));
return;
}{...}
printf("%s:\n", path);
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
printf(
" %s: %s\n",
(entry->d_type == DT_DIR)
? "directory"
: "file ",
entry->d_name
);
}{...}
closedir(dir);
}{ ... }
void clean_dir(const char *path)
{
ESP_LOGI(TAG, "Deleting everything in %s:", path);
DIR *dir = opendir(path);
if (!dir) {
ESP_LOGE(TAG, "Failed to open directory: %s", strerror(errno));
return;
}{...}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
char full_path[64] = {0};
snprintf(full_path, sizeof(full_path), "%.20s/%.40s", path, entry->d_name);
if (entry->d_type == DT_DIR)
clean_dir(full_path);
if (remove(full_path) != 0) {
ESP_LOGE(TAG, "Failed to remove %s: %s", full_path, strerror(errno));
}{...}
}{...}
closedir(dir);
}{ ... }