80 lines
2.2 KiB
C
80 lines
2.2 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;
|
|
}
|
|
|
|
void list_files_in_spiffs() {
|
|
DIR *dir = opendir("/spiffs"); // ´ò¿ª SPIFFS ¸ùĿ¼
|
|
if (dir == NULL) {
|
|
ESP_LOGE(TAG, "Failed to open directory");
|
|
return;
|
|
}
|
|
|
|
struct dirent *entry;
|
|
char filepath[300];
|
|
struct stat st;
|
|
|
|
while ((entry = readdir(dir)) != NULL) {
|
|
snprintf(filepath, sizeof(filepath), "/spiffs/%s", entry->d_name);
|
|
if (stat(filepath, &st) == 0) {
|
|
ESP_LOGI(TAG, "Found file: %s, size: %ld bytes", entry->d_name, st.st_size);
|
|
} 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);
|
|
|
|
// }
|
|
|