spiffs_example_main.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* SPIFFS filesystem example.
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <sys/unistd.h>
  10. #include <sys/stat.h>
  11. #include "esp_err.h"
  12. #include "esp_log.h"
  13. #include "esp_spiffs.h"
  14. static const char *TAG = "example";
  15. void app_main(void)
  16. {
  17. ESP_LOGI(TAG, "Initializing SPIFFS");
  18. esp_vfs_spiffs_conf_t conf = {
  19. .base_path = "/spiffs",
  20. .partition_label = NULL,
  21. .max_files = 5,
  22. .format_if_mount_failed = true
  23. };
  24. // Use settings defined above to initialize and mount SPIFFS filesystem.
  25. // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
  26. esp_err_t ret = esp_vfs_spiffs_register(&conf);
  27. if (ret != ESP_OK) {
  28. if (ret == ESP_FAIL) {
  29. ESP_LOGE(TAG, "Failed to mount or format filesystem");
  30. } else if (ret == ESP_ERR_NOT_FOUND) {
  31. ESP_LOGE(TAG, "Failed to find SPIFFS partition");
  32. } else {
  33. ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
  34. }
  35. return;
  36. }
  37. size_t total = 0, used = 0;
  38. ret = esp_spiffs_info(conf.partition_label, &total, &used);
  39. if (ret != ESP_OK) {
  40. ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
  41. } else {
  42. ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
  43. }
  44. // Use POSIX and C standard library functions to work with files.
  45. // First create a file.
  46. ESP_LOGI(TAG, "Opening file");
  47. FILE* f = fopen("/spiffs/hello.txt", "w");
  48. if (f == NULL) {
  49. ESP_LOGE(TAG, "Failed to open file for writing");
  50. return;
  51. }
  52. fprintf(f, "Hello World!\n");
  53. fclose(f);
  54. ESP_LOGI(TAG, "File written");
  55. // Check if destination file exists before renaming
  56. struct stat st;
  57. if (stat("/spiffs/foo.txt", &st) == 0) {
  58. // Delete it if it exists
  59. unlink("/spiffs/foo.txt");
  60. }
  61. // Rename original file
  62. ESP_LOGI(TAG, "Renaming file");
  63. if (rename("/spiffs/hello.txt", "/spiffs/foo.txt") != 0) {
  64. ESP_LOGE(TAG, "Rename failed");
  65. return;
  66. }
  67. // Open renamed file for reading
  68. ESP_LOGI(TAG, "Reading file");
  69. f = fopen("/spiffs/foo.txt", "r");
  70. if (f == NULL) {
  71. ESP_LOGE(TAG, "Failed to open file for reading");
  72. return;
  73. }
  74. char line[64];
  75. fgets(line, sizeof(line), f);
  76. fclose(f);
  77. // strip newline
  78. char* pos = strchr(line, '\n');
  79. if (pos) {
  80. *pos = '\0';
  81. }
  82. ESP_LOGI(TAG, "Read from file: '%s'", line);
  83. // All done, unmount partition and disable SPIFFS
  84. esp_vfs_spiffs_unregister(conf.partition_label);
  85. ESP_LOGI(TAG, "SPIFFS unmounted");
  86. }