spiffs_example_main.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #ifdef CONFIG_EXAMPLE_SPIFFS_CHECK_ON_START
  38. ESP_LOGI(TAG, "Performing SPIFFS_check().");
  39. ret = esp_spiffs_check(conf.partition_label);
  40. if (ret != ESP_OK) {
  41. ESP_LOGE(TAG, "SPIFFS_check() failed (%s)", esp_err_to_name(ret));
  42. return;
  43. } else {
  44. ESP_LOGI(TAG, "SPIFFS_check() successful");
  45. }
  46. #endif
  47. size_t total = 0, used = 0;
  48. ret = esp_spiffs_info(conf.partition_label, &total, &used);
  49. if (ret != ESP_OK) {
  50. ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s). Formatting...", esp_err_to_name(ret));
  51. esp_spiffs_format(conf.partition_label);
  52. return;
  53. } else {
  54. ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
  55. }
  56. #
  57. // Check consistency of reported partiton size info.
  58. if (used > total) {
  59. ESP_LOGW(TAG, "Number of used bytes cannot be larger than total. Performing SPIFFS_check().");
  60. ret = esp_spiffs_check(conf.partition_label);
  61. // Could be also used to mend broken files, to clean unreferenced pages, etc.
  62. // More info at https://github.com/pellepl/spiffs/wiki/FAQ#powerlosses-contd-when-should-i-run-spiffs_check
  63. if (ret != ESP_OK) {
  64. ESP_LOGE(TAG, "SPIFFS_check() failed (%s)", esp_err_to_name(ret));
  65. return;
  66. } else {
  67. ESP_LOGI(TAG, "SPIFFS_check() successful");
  68. }
  69. }
  70. // Use POSIX and C standard library functions to work with files.
  71. // First create a file.
  72. ESP_LOGI(TAG, "Opening file");
  73. FILE* f = fopen("/spiffs/hello.txt", "w");
  74. if (f == NULL) {
  75. ESP_LOGE(TAG, "Failed to open file for writing");
  76. return;
  77. }
  78. fprintf(f, "Hello World!\n");
  79. fclose(f);
  80. ESP_LOGI(TAG, "File written");
  81. // Check if destination file exists before renaming
  82. struct stat st;
  83. if (stat("/spiffs/foo.txt", &st) == 0) {
  84. // Delete it if it exists
  85. unlink("/spiffs/foo.txt");
  86. }
  87. // Rename original file
  88. ESP_LOGI(TAG, "Renaming file");
  89. if (rename("/spiffs/hello.txt", "/spiffs/foo.txt") != 0) {
  90. ESP_LOGE(TAG, "Rename failed");
  91. return;
  92. }
  93. // Open renamed file for reading
  94. ESP_LOGI(TAG, "Reading file");
  95. f = fopen("/spiffs/foo.txt", "r");
  96. if (f == NULL) {
  97. ESP_LOGE(TAG, "Failed to open file for reading");
  98. return;
  99. }
  100. char line[64];
  101. fgets(line, sizeof(line), f);
  102. fclose(f);
  103. // strip newline
  104. char* pos = strchr(line, '\n');
  105. if (pos) {
  106. *pos = '\0';
  107. }
  108. ESP_LOGI(TAG, "Read from file: '%s'", line);
  109. // All done, unmount partition and disable SPIFFS
  110. esp_vfs_spiffs_unregister(conf.partition_label);
  111. ESP_LOGI(TAG, "SPIFFS unmounted");
  112. }