esp_spiffs.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef _ESP_SPIFFS_H_
  7. #define _ESP_SPIFFS_H_
  8. #include <stdbool.h>
  9. #include "esp_err.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /**
  14. * @brief Configuration structure for esp_vfs_spiffs_register
  15. */
  16. typedef struct {
  17. const char* base_path; /*!< File path prefix associated with the filesystem. */
  18. const char* partition_label; /*!< Optional, label of SPIFFS partition to use. If set to NULL, first partition with subtype=spiffs will be used. */
  19. size_t max_files; /*!< Maximum files that could be open at the same time. */
  20. bool format_if_mount_failed; /*!< If true, it will format the file system if it fails to mount. */
  21. } esp_vfs_spiffs_conf_t;
  22. /**
  23. * Register and mount SPIFFS to VFS with given path prefix.
  24. *
  25. * @param conf Pointer to esp_vfs_spiffs_conf_t configuration structure
  26. *
  27. * @return
  28. * - ESP_OK if success
  29. * - ESP_ERR_NO_MEM if objects could not be allocated
  30. * - ESP_ERR_INVALID_STATE if already mounted or partition is encrypted
  31. * - ESP_ERR_NOT_FOUND if partition for SPIFFS was not found
  32. * - ESP_FAIL if mount or format fails
  33. */
  34. esp_err_t esp_vfs_spiffs_register(const esp_vfs_spiffs_conf_t * conf);
  35. /**
  36. * Unregister and unmount SPIFFS from VFS
  37. *
  38. * @param partition_label Same label as passed to esp_vfs_spiffs_register.
  39. *
  40. * @return
  41. * - ESP_OK if successful
  42. * - ESP_ERR_INVALID_STATE already unregistered
  43. */
  44. esp_err_t esp_vfs_spiffs_unregister(const char* partition_label);
  45. /**
  46. * Check if SPIFFS is mounted
  47. *
  48. * @param partition_label Optional, label of the partition to check.
  49. * If not specified, first partition with subtype=spiffs is used.
  50. *
  51. * @return
  52. * - true if mounted
  53. * - false if not mounted
  54. */
  55. bool esp_spiffs_mounted(const char* partition_label);
  56. /**
  57. * Format the SPIFFS partition
  58. *
  59. * @param partition_label Same label as passed to esp_vfs_spiffs_register.
  60. * @return
  61. * - ESP_OK if successful
  62. * - ESP_FAIL on error
  63. */
  64. esp_err_t esp_spiffs_format(const char* partition_label);
  65. /**
  66. * Get information for SPIFFS
  67. *
  68. * @param partition_label Same label as passed to esp_vfs_spiffs_register
  69. * @param[out] total_bytes Size of the file system
  70. * @param[out] used_bytes Current used bytes in the file system
  71. *
  72. * @return
  73. * - ESP_OK if success
  74. * - ESP_ERR_INVALID_STATE if not mounted
  75. */
  76. esp_err_t esp_spiffs_info(const char* partition_label, size_t *total_bytes, size_t *used_bytes);
  77. /**
  78. * Check integrity of SPIFFS
  79. *
  80. * @param partition_label Same label as passed to esp_vfs_spiffs_register
  81. * @return
  82. * - ESP_OK if successful
  83. * - ESP_ERR_INVALID_STATE if not mounted
  84. * - ESP_FAIL on error
  85. */
  86. esp_err_t esp_spiffs_check(const char* partition_label);
  87. /**
  88. * @brief Perform garbage collection in SPIFFS partition
  89. *
  90. * Call this function to run GC and ensure that at least the given amount of
  91. * space is available in the partition. This function will fail with ESP_ERR_NOT_FINISHED
  92. * if it is not possible to reclaim the requested space (that is, not enough free
  93. * or deleted pages in the filesystem). This function will also fail if it fails to
  94. * reclaim the requested space after CONFIG_SPIFFS_GC_MAX_RUNS number of GC iterations.
  95. * On one GC iteration, SPIFFS will erase one logical block (4kB). Therefore the value
  96. * of CONFIG_SPIFFS_GC_MAX_RUNS should be set at least to the maximum expected size_to_gc,
  97. * divided by 4096. For example, if the application expects to make room for a 1MB file and
  98. * calls esp_spiffs_gc(label, 1024 * 1024), CONFIG_SPIFFS_GC_MAX_RUNS should be set to
  99. * at least 256.
  100. * On the other hand, increasing CONFIG_SPIFFS_GC_MAX_RUNS value increases the maximum
  101. * amount of time for which any SPIFFS GC or write operation may potentially block.
  102. *
  103. * @param partition_label Label of the partition to be garbage-collected.
  104. * The partition must be already mounted.
  105. * @param size_to_gc The number of bytes that the GC process should attempt
  106. * to make available.
  107. * @return
  108. * - ESP_OK on success
  109. * - ESP_ERR_NOT_FINISHED if GC fails to reclaim the size given by size_to_gc
  110. * - ESP_ERR_INVALID_STATE if the partition is not mounted
  111. * - ESP_FAIL on all other errors
  112. */
  113. esp_err_t esp_spiffs_gc(const char* partition_label, size_t size_to_gc);
  114. #ifdef __cplusplus
  115. }
  116. #endif
  117. #endif /* _ESP_SPIFFS_H_ */