2017-09-07 10:37:59 -04:00
|
|
|
/* SPIFFS filesystem example.
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_spiffs.h"
|
|
|
|
|
|
|
|
static const char *TAG = "example";
|
|
|
|
|
|
|
|
void app_main(void)
|
|
|
|
{
|
|
|
|
ESP_LOGI(TAG, "Initializing SPIFFS");
|
2020-11-10 02:40:01 -05:00
|
|
|
|
2017-09-07 10:37:59 -04:00
|
|
|
esp_vfs_spiffs_conf_t conf = {
|
|
|
|
.base_path = "/spiffs",
|
|
|
|
.partition_label = NULL,
|
|
|
|
.max_files = 5,
|
|
|
|
.format_if_mount_failed = true
|
|
|
|
};
|
2020-11-10 02:40:01 -05:00
|
|
|
|
2017-09-07 10:37:59 -04:00
|
|
|
// Use settings defined above to initialize and mount SPIFFS filesystem.
|
|
|
|
// Note: esp_vfs_spiffs_register is an all-in-one convenience function.
|
|
|
|
esp_err_t ret = esp_vfs_spiffs_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 SPIFFS partition");
|
|
|
|
} else {
|
2018-02-22 08:48:53 -05:00
|
|
|
ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
|
2017-09-07 10:37:59 -04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2020-11-10 02:40:01 -05:00
|
|
|
|
2021-10-19 10:24:53 -04:00
|
|
|
#ifdef CONFIG_EXAMPLE_SPIFFS_CHECK_ON_START
|
|
|
|
ESP_LOGI(TAG, "Performing SPIFFS_check().");
|
|
|
|
ret = esp_spiffs_check(conf.partition_label);
|
|
|
|
if (ret != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "SPIFFS_check() failed (%s)", esp_err_to_name(ret));
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
ESP_LOGI(TAG, "SPIFFS_check() successful");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-09-07 10:37:59 -04:00
|
|
|
size_t total = 0, used = 0;
|
2019-12-23 12:20:14 -05:00
|
|
|
ret = esp_spiffs_info(conf.partition_label, &total, &used);
|
2017-09-07 10:37:59 -04:00
|
|
|
if (ret != ESP_OK) {
|
2021-10-19 10:24:53 -04:00
|
|
|
ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s). Formatting...", esp_err_to_name(ret));
|
|
|
|
esp_spiffs_format(conf.partition_label);
|
|
|
|
return;
|
2017-09-07 10:37:59 -04:00
|
|
|
} else {
|
|
|
|
ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
|
|
|
|
}
|
|
|
|
|
2024-03-28 04:14:05 -04:00
|
|
|
// Check consistency of reported partition size info.
|
2021-10-19 10:24:53 -04:00
|
|
|
if (used > total) {
|
|
|
|
ESP_LOGW(TAG, "Number of used bytes cannot be larger than total. Performing SPIFFS_check().");
|
|
|
|
ret = esp_spiffs_check(conf.partition_label);
|
|
|
|
// Could be also used to mend broken files, to clean unreferenced pages, etc.
|
|
|
|
// More info at https://github.com/pellepl/spiffs/wiki/FAQ#powerlosses-contd-when-should-i-run-spiffs_check
|
|
|
|
if (ret != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "SPIFFS_check() failed (%s)", esp_err_to_name(ret));
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
ESP_LOGI(TAG, "SPIFFS_check() successful");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-07 10:37:59 -04:00
|
|
|
// Use POSIX and C standard library functions to work with files.
|
|
|
|
// First create a file.
|
|
|
|
ESP_LOGI(TAG, "Opening file");
|
|
|
|
FILE* f = fopen("/spiffs/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("/spiffs/foo.txt", &st) == 0) {
|
|
|
|
// Delete it if it exists
|
|
|
|
unlink("/spiffs/foo.txt");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename original file
|
|
|
|
ESP_LOGI(TAG, "Renaming file");
|
|
|
|
if (rename("/spiffs/hello.txt", "/spiffs/foo.txt") != 0) {
|
|
|
|
ESP_LOGE(TAG, "Rename failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open renamed file for reading
|
|
|
|
ESP_LOGI(TAG, "Reading file");
|
|
|
|
f = fopen("/spiffs/foo.txt", "r");
|
|
|
|
if (f == NULL) {
|
|
|
|
ESP_LOGE(TAG, "Failed to open file for reading");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char line[64];
|
|
|
|
fgets(line, sizeof(line), f);
|
|
|
|
fclose(f);
|
|
|
|
// strip newline
|
|
|
|
char* pos = strchr(line, '\n');
|
|
|
|
if (pos) {
|
|
|
|
*pos = '\0';
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Read from file: '%s'", line);
|
|
|
|
|
|
|
|
// All done, unmount partition and disable SPIFFS
|
2019-12-23 12:20:14 -05:00
|
|
|
esp_vfs_spiffs_unregister(conf.partition_label);
|
2017-09-07 10:37:59 -04:00
|
|
|
ESP_LOGI(TAG, "SPIFFS unmounted");
|
|
|
|
}
|