33 #include <sys/unistd.h>
37 #include "esp_spiffs.h"
39 #include <sys/param.h>
40 #include <sys/unistd.h>
48 static const char *
TAG = CONFIG_SPIFFS_TAG;
55 if (stat(filename, &st) == 0)
57 ESP_LOGI(
TAG,
"file size: %ld", st.st_size);
60 ESP_LOGI(
TAG,
"Cannot determine size of %s\n",
67 ESP_LOGI(
TAG,
"Reading %s", pwd);
69 FILE *file = fopen(pwd,
"r");
72 ESP_LOGE(
TAG,
"Failed to open %s", pwd);
77 char buf[file_size < 2000 ? file_size : 2000];
79 off_t current_position = 2000;
80 while (current_position < file_size + 2000)
82 memset(buf, 0,
sizeof(buf));
83 fread(buf, 1,
sizeof(buf), file);
84 ESP_LOGI(
TAG,
"%s", buf);
85 current_position += 1088;
89 ESP_LOGI(
TAG,
"Read from %s", pwd);
96 const char *entrytype;
98 DIR *dir = opendir(
"/spiffs");
99 const size_t dirpath_len = strlen(
"/spiffs");
100 while ((entry = readdir(dir)) != NULL)
102 entrytype = (entry->d_type == DT_DIR ?
"directory" :
"file");
103 strlcpy(entrypath + dirpath_len, entry->d_name,
sizeof(entrypath) - dirpath_len);
104 ESP_LOGI(
TAG,
"%s : %s", entrytype, entry->d_name);
113 ESP_LOGI(
TAG,
"Initializing SPIFFS");
115 esp_vfs_spiffs_conf_t conf = {
116 .base_path =
"/spiffs",
117 .partition_label = NULL,
119 .format_if_mount_failed =
false};
123 esp_err_t ret = esp_vfs_spiffs_register(&conf);
129 ESP_LOGE(
TAG,
"Failed to mount or format filesystem");
131 else if (ret == ESP_ERR_NOT_FOUND)
133 ESP_LOGE(
TAG,
"Failed to find SPIFFS partition");
137 ESP_LOGE(
TAG,
"Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
142 size_t total = 0, used = 0;
143 ret = esp_spiffs_info(NULL, &total, &used);
146 ESP_LOGE(
TAG,
"Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
150 ESP_LOGI(
TAG,
"Partition size: total: %d, used: %d", total, used);
161 esp_vfs_spiffs_unregister(NULL);
162 ESP_LOGI(
TAG,
"SPIFFS unmounted");
off_t get_file_size(const char *filename)
void read_file(const char *pwd)
defininitions for simple utility functions