spiffsgen_example_main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* SPIFFS Image Generation on Build 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. #include "mbedtls/md5.h"
  15. static const char *TAG = "example";
  16. static void read_hello_txt(void)
  17. {
  18. ESP_LOGI(TAG, "Reading hello.txt");
  19. // Open for reading hello.txt
  20. FILE* f = fopen("/spiffs/hello.txt", "r");
  21. if (f == NULL) {
  22. ESP_LOGE(TAG, "Failed to open hello.txt");
  23. return;
  24. }
  25. char buf[64];
  26. memset(buf, 0, sizeof(buf));
  27. fread(buf, 1, sizeof(buf), f);
  28. fclose(f);
  29. // Display the read contents from the file
  30. ESP_LOGI(TAG, "Read from hello.txt: %s", buf);
  31. }
  32. static void compute_alice_txt_md5(void)
  33. {
  34. ESP_LOGI(TAG, "Computing alice.txt MD5 hash");
  35. // The file alice.txt lives under a subdirectory, though SPIFFS itself is flat
  36. FILE* f = fopen("/spiffs/sub/alice.txt", "r");
  37. if (f == NULL) {
  38. ESP_LOGE(TAG, "Failed to open alice.txt");
  39. return;
  40. }
  41. // Read file and compute the digest chunk by chunk
  42. #define MD5_MAX_LEN 16
  43. char buf[64];
  44. mbedtls_md5_context ctx;
  45. unsigned char digest[MD5_MAX_LEN];
  46. mbedtls_md5_init(&ctx);
  47. mbedtls_md5_starts_ret(&ctx);
  48. size_t read;
  49. do {
  50. read = fread((void*) buf, 1, sizeof(buf), f);
  51. mbedtls_md5_update_ret(&ctx, (unsigned const char*) buf, read);
  52. } while(read == sizeof(buf));
  53. mbedtls_md5_finish_ret(&ctx, digest);
  54. // Create a string of the digest
  55. char digest_str[MD5_MAX_LEN * 2];
  56. for (int i = 0; i < MD5_MAX_LEN; i++) {
  57. sprintf(&digest_str[i * 2], "%02x", (unsigned int)digest[i]);
  58. }
  59. // For reference, MD5 should be deeb71f585cbb3ae5f7976d5127faf2a
  60. ESP_LOGI(TAG, "Computed MD5 hash of alice.txt: %s", digest_str);
  61. fclose(f);
  62. }
  63. void app_main(void)
  64. {
  65. ESP_LOGI(TAG, "Initializing SPIFFS");
  66. esp_vfs_spiffs_conf_t conf = {
  67. .base_path = "/spiffs",
  68. .partition_label = NULL,
  69. .max_files = 5,
  70. .format_if_mount_failed = false
  71. };
  72. // Use settings defined above to initialize and mount SPIFFS filesystem.
  73. // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
  74. esp_err_t ret = esp_vfs_spiffs_register(&conf);
  75. if (ret != ESP_OK) {
  76. if (ret == ESP_FAIL) {
  77. ESP_LOGE(TAG, "Failed to mount or format filesystem");
  78. } else if (ret == ESP_ERR_NOT_FOUND) {
  79. ESP_LOGE(TAG, "Failed to find SPIFFS partition");
  80. } else {
  81. ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
  82. }
  83. return;
  84. }
  85. size_t total = 0, used = 0;
  86. ret = esp_spiffs_info(NULL, &total, &used);
  87. if (ret != ESP_OK) {
  88. ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
  89. } else {
  90. ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
  91. }
  92. /* The following calls demonstrate reading files from the generated SPIFFS
  93. * image. The images should contain the same files and contents as the spiffs_image directory.
  94. */
  95. // Read and display the contents of a small text file (hello.txt)
  96. read_hello_txt();
  97. // Compute and display the MD5 hash of a large text file (alice.txt)
  98. compute_alice_txt_md5();
  99. // All done, unmount partition and disable SPIFFS
  100. esp_vfs_spiffs_unregister(NULL);
  101. ESP_LOGI(TAG, "SPIFFS unmounted");
  102. }