Spiffs
despite being called "Spiffs" this module actualy uses littleFS
https://github.com/littlefs-project/littlefs
Overview
this module provides helper functions to set up and tear down the littleFS filesystem, and files on said filesystem.
Configuration
VERBOSE_LITTLEFS_LOGGING
Verbose logging prints out all file system set ups and tear downs, as well as file opens and closes, does not print out each operation within a file.
Public Functions
set_up_spiffs
Sets up the filesystem for use and mounts it. logs to console if there is an error.
esp_err_t set_up_spiffs(const char* partition_label, char* error)
{
LITTLEFS_LOG_VERBOSE("Initializing LittleFS");
esp_vfs_littlefs_conf_t conf = {
.base_path = "/spiffs",
.partition_label = partition_label,
.format_if_mount_failed = true,
.dont_mount = false,
};
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 ret;
}
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));
}
else
{
LITTLEFS_LOG_VERBOSE("Partition size: total: %d, used: %d", total, used);
}
return ret;
}
tear_down_spiffs
Unmounts the filesystem and unregisters it.
void tear_down_spiffs(const char* partition_label)
{
esp_vfs_littlefs_unregister(partition_label);
}
set_up_file
removes the file if it exists, then creates the new file with the given name.
esp_err_t set_up_file(const char* filename, FILE** f, const char* partition_label, char* error)
{
LITTLEFS_LOG_VERBOSE("file open started\n");
remove(filename);
*f = fopen(filename, "wb");
LITTLEFS_LOG_VERBOSE("file open complete\n");
if (*f == NULL)
{
sprintf(error, "FILE ERROR!\n");
return ESP_FAIL;
}
return ESP_OK;
}
tear_down_file
closes the file.
void tear_down_file(FILE* f)
{
fclose(f);
}