127 lines
3.6 KiB
C
127 lines
3.6 KiB
C
#include "spiffs.h"
|
|
#include "dirent.h"
|
|
#include <sys/stat.h>
|
|
|
|
static const char *TAG = "file System";
|
|
|
|
esp_err_t init_spiffs(void)
|
|
{
|
|
ESP_LOGI(TAG, "Initializing SPIFFS");
|
|
|
|
esp_vfs_spiffs_conf_t conf = {
|
|
.base_path = "/spiffs",
|
|
.partition_label = NULL,
|
|
.max_files = 10, // This decides the maximum number of files that can be created on the storage
|
|
.format_if_mount_failed = true
|
|
};
|
|
|
|
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 {
|
|
ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
|
|
}
|
|
return ESP_FAIL;
|
|
}
|
|
|
|
size_t total = 0, used = 0;
|
|
ret = esp_spiffs_info(NULL, &total, &used);
|
|
if (ret != ESP_OK) {
|
|
ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
|
|
return ESP_FAIL;
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
|
|
return ESP_OK;
|
|
}
|
|
|
|
uint8_t list_files_in_spiffs(const char *dir_path,spiffs_list_t *directory, size_t max_files) {
|
|
DIR *dir = opendir(dir_path);
|
|
if (dir == NULL) {
|
|
ESP_LOGE(TAG, "Failed to open directory");
|
|
return 0;
|
|
}
|
|
|
|
struct dirent *entry;
|
|
struct stat st;
|
|
char filepath[300];
|
|
uint8_t id = 0;
|
|
|
|
while ((entry = readdir(dir)) != NULL && id < max_files) {
|
|
snprintf(filepath, sizeof(filepath), "%s/%s", dir_path, entry->d_name);
|
|
if (stat(filepath, &st) == 0) {
|
|
directory[id].id = id;
|
|
directory[id].addr = 0; // SPIFFS 本身没有物理地址,可以留空或自定义
|
|
directory[id].size = st.st_size;
|
|
memset(directory[id].name, 0, sizeof(directory[id].name));
|
|
strncpy(directory[id].name, entry->d_name, sizeof(directory[id].name) - 1);
|
|
ESP_LOGI(TAG, "Found file: %s, size: %ld bytes", entry->d_name, st.st_size);
|
|
id++;
|
|
} else {
|
|
ESP_LOGE(TAG, "Failed to get size of file: %s", entry->d_name);
|
|
}
|
|
}
|
|
|
|
closedir(dir);
|
|
return id;
|
|
}
|
|
|
|
|
|
void delete_file(const char *file_path)
|
|
{
|
|
if (remove(file_path) == 0) {
|
|
ESP_LOGI(TAG, "File '%s' deleted successfully", file_path);
|
|
} else {
|
|
ESP_LOGE(TAG, "Failed to delete file '%s'", file_path);
|
|
}
|
|
}
|
|
|
|
void delete_all_file()
|
|
{
|
|
DIR *dir = opendir("/spiffs");
|
|
if (dir == NULL) {
|
|
ESP_LOGE(TAG, "Failed to open directory");
|
|
return;
|
|
}
|
|
|
|
struct dirent *entry;
|
|
struct stat st;
|
|
char filepath[300];
|
|
uint8_t id = 0;
|
|
|
|
while ((entry = readdir(dir)) != NULL && id < 10) {
|
|
snprintf(filepath, sizeof(filepath), "/spiffs/%s", entry->d_name);
|
|
if (stat(filepath, &st) == 0) {
|
|
ESP_LOGI(TAG, "remove file: %s, size: %ld bytes", entry->d_name, st.st_size);
|
|
remove(filepath);
|
|
id++;
|
|
} else {
|
|
ESP_LOGE(TAG, "Failed to get size of file: %s", entry->d_name);
|
|
}
|
|
}
|
|
|
|
closedir(dir);
|
|
}
|
|
|
|
|
|
|
|
|
|
// void Read_file(char* filename, Read_data data,uint16_t size)
|
|
// {
|
|
// FILE* f ;
|
|
|
|
// f = fopen(filename, "rb");
|
|
// if (f == NULL) {
|
|
// ESP_LOGE(FILE_TAG, "Failed to open file for writing");
|
|
// return;
|
|
// }
|
|
// data.size = fread(data.buf,1,size,f);
|
|
// data.type = SPIFFS;
|
|
// fclose(f);
|
|
|
|
// }
|
|
|