1 /* SPIFFS filesystem example.
2    This example code is in the Public Domain (or CC0 licensed, at your option.)
3 
4    Unless required by applicable law or agreed to in writing, this
5    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
6    CONDITIONS OF ANY KIND, either express or implied.
7 */
8 
9 #include <stdio.h>
10 #include <string.h>
11 #include <sys/unistd.h>
12 #include <sys/stat.h>
13 #include "esp_err.h"
14 #include "esp_log.h"
15 #include "esp_spiffs.h"
16 
17 static const char *TAG = "example";
18 
app_main(void)19 void app_main(void)
20 {
21     ESP_LOGI(TAG, "Initializing SPIFFS");
22 
23     esp_vfs_spiffs_conf_t conf = {
24       .base_path = "/spiffs",
25       .partition_label = NULL,
26       .max_files = 5,
27       .format_if_mount_failed = true
28     };
29 
30     // Use settings defined above to initialize and mount SPIFFS filesystem.
31     // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
32     esp_err_t ret = esp_vfs_spiffs_register(&conf);
33 
34     if (ret != ESP_OK) {
35         if (ret == ESP_FAIL) {
36             ESP_LOGE(TAG, "Failed to mount or format filesystem");
37         } else if (ret == ESP_ERR_NOT_FOUND) {
38             ESP_LOGE(TAG, "Failed to find SPIFFS partition");
39         } else {
40             ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
41         }
42         return;
43     }
44 
45     size_t total = 0, used = 0;
46     ret = esp_spiffs_info(conf.partition_label, &total, &used);
47     if (ret != ESP_OK) {
48         ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
49     } else {
50         ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
51     }
52 
53     // Use POSIX and C standard library functions to work with files.
54     // First create a file.
55     ESP_LOGI(TAG, "Opening file");
56     FILE* f = fopen("/spiffs/hello.txt", "w");
57     if (f == NULL) {
58         ESP_LOGE(TAG, "Failed to open file for writing");
59         return;
60     }
61     fprintf(f, "Hello World!\n");
62     fclose(f);
63     ESP_LOGI(TAG, "File written");
64 
65     // Check if destination file exists before renaming
66     struct stat st;
67     if (stat("/spiffs/foo.txt", &st) == 0) {
68         // Delete it if it exists
69         unlink("/spiffs/foo.txt");
70     }
71 
72     // Rename original file
73     ESP_LOGI(TAG, "Renaming file");
74     if (rename("/spiffs/hello.txt", "/spiffs/foo.txt") != 0) {
75         ESP_LOGE(TAG, "Rename failed");
76         return;
77     }
78 
79     // Open renamed file for reading
80     ESP_LOGI(TAG, "Reading file");
81     f = fopen("/spiffs/foo.txt", "r");
82     if (f == NULL) {
83         ESP_LOGE(TAG, "Failed to open file for reading");
84         return;
85     }
86     char line[64];
87     fgets(line, sizeof(line), f);
88     fclose(f);
89     // strip newline
90     char* pos = strchr(line, '\n');
91     if (pos) {
92         *pos = '\0';
93     }
94     ESP_LOGI(TAG, "Read from file: '%s'", line);
95 
96     // All done, unmount partition and disable SPIFFS
97     esp_vfs_spiffs_unregister(conf.partition_label);
98     ESP_LOGI(TAG, "SPIFFS unmounted");
99 }
100