esp_spiffs.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "esp_spiffs.h"
  7. #include "spiffs.h"
  8. #include "spiffs_nucleus.h"
  9. #include "esp_log.h"
  10. #include "esp_partition.h"
  11. #include "spi_flash_mmap.h"
  12. #include "esp_image_format.h"
  13. #include "freertos/FreeRTOS.h"
  14. #include "freertos/task.h"
  15. #include "freertos/semphr.h"
  16. #include <unistd.h>
  17. #include <dirent.h>
  18. #include <sys/errno.h>
  19. #include <sys/fcntl.h>
  20. #include <sys/lock.h>
  21. #include "esp_vfs.h"
  22. #include "esp_err.h"
  23. #include "esp_rom_spiflash.h"
  24. #include "spiffs_api.h"
  25. static const char* TAG = "SPIFFS";
  26. #ifdef CONFIG_SPIFFS_USE_MTIME
  27. #ifdef CONFIG_SPIFFS_MTIME_WIDE_64_BITS
  28. typedef time_t spiffs_time_t;
  29. #else
  30. typedef unsigned long spiffs_time_t;
  31. #endif
  32. _Static_assert(CONFIG_SPIFFS_META_LENGTH >= sizeof(spiffs_time_t),
  33. "SPIFFS_META_LENGTH size should be >= sizeof(spiffs_time_t)");
  34. #endif //CONFIG_SPIFFS_USE_MTIME
  35. /**
  36. * @brief SPIFFS DIR structure
  37. */
  38. typedef struct {
  39. DIR dir; /*!< VFS DIR struct */
  40. spiffs_DIR d; /*!< SPIFFS DIR struct */
  41. struct dirent e; /*!< Last open dirent */
  42. long offset; /*!< Offset of the current dirent */
  43. char path[SPIFFS_OBJ_NAME_LEN]; /*!< Requested directory name */
  44. } vfs_spiffs_dir_t;
  45. static int spiffs_res_to_errno(s32_t fr);
  46. static int vfs_spiffs_open(void* ctx, const char * path, int flags, int mode);
  47. static ssize_t vfs_spiffs_write(void* ctx, int fd, const void * data, size_t size);
  48. static ssize_t vfs_spiffs_read(void* ctx, int fd, void * dst, size_t size);
  49. static int vfs_spiffs_close(void* ctx, int fd);
  50. static off_t vfs_spiffs_lseek(void* ctx, int fd, off_t offset, int mode);
  51. static int vfs_spiffs_fstat(void* ctx, int fd, struct stat * st);
  52. #ifdef CONFIG_VFS_SUPPORT_DIR
  53. static int vfs_spiffs_stat(void* ctx, const char * path, struct stat * st);
  54. static int vfs_spiffs_unlink(void* ctx, const char *path);
  55. static int vfs_spiffs_link(void* ctx, const char* n1, const char* n2);
  56. static int vfs_spiffs_rename(void* ctx, const char *src, const char *dst);
  57. static DIR* vfs_spiffs_opendir(void* ctx, const char* name);
  58. static int vfs_spiffs_closedir(void* ctx, DIR* pdir);
  59. static struct dirent* vfs_spiffs_readdir(void* ctx, DIR* pdir);
  60. static int vfs_spiffs_readdir_r(void* ctx, DIR* pdir,
  61. struct dirent* entry, struct dirent** out_dirent);
  62. static long vfs_spiffs_telldir(void* ctx, DIR* pdir);
  63. static void vfs_spiffs_seekdir(void* ctx, DIR* pdir, long offset);
  64. static int vfs_spiffs_mkdir(void* ctx, const char* name, mode_t mode);
  65. static int vfs_spiffs_rmdir(void* ctx, const char* name);
  66. static int vfs_spiffs_truncate(void* ctx, const char *path, off_t length);
  67. static int vfs_spiffs_ftruncate(void* ctx, int fd, off_t length);
  68. #ifdef CONFIG_SPIFFS_USE_MTIME
  69. static int vfs_spiffs_utime(void *ctx, const char *path, const struct utimbuf *times);
  70. #endif // CONFIG_SPIFFS_USE_MTIME
  71. #endif // CONFIG_VFS_SUPPORT_DIR
  72. static void vfs_spiffs_update_mtime(spiffs *fs, spiffs_file f);
  73. static time_t vfs_spiffs_get_mtime(const spiffs_stat* s);
  74. static esp_spiffs_t * _efs[CONFIG_SPIFFS_MAX_PARTITIONS];
  75. static void esp_spiffs_free(esp_spiffs_t ** efs)
  76. {
  77. esp_spiffs_t * e = *efs;
  78. if (*efs == NULL) {
  79. return;
  80. }
  81. *efs = NULL;
  82. if (e->fs) {
  83. SPIFFS_unmount(e->fs);
  84. free(e->fs);
  85. }
  86. vSemaphoreDelete(e->lock);
  87. free(e->fds);
  88. free(e->cache);
  89. free(e->work);
  90. free(e);
  91. }
  92. static esp_err_t esp_spiffs_by_label(const char* label, int * index){
  93. int i;
  94. esp_spiffs_t * p;
  95. for (i = 0; i < CONFIG_SPIFFS_MAX_PARTITIONS; i++) {
  96. p = _efs[i];
  97. if (p) {
  98. if (!label && !p->by_label) {
  99. *index = i;
  100. return ESP_OK;
  101. }
  102. if (label && p->by_label && strncmp(label, p->partition->label, 17) == 0) {
  103. *index = i;
  104. return ESP_OK;
  105. }
  106. }
  107. }
  108. return ESP_ERR_NOT_FOUND;
  109. }
  110. static esp_err_t esp_spiffs_get_empty(int * index){
  111. int i;
  112. for (i = 0; i < CONFIG_SPIFFS_MAX_PARTITIONS; i++) {
  113. if (_efs[i] == NULL) {
  114. *index = i;
  115. return ESP_OK;
  116. }
  117. }
  118. return ESP_ERR_NOT_FOUND;
  119. }
  120. static esp_err_t esp_spiffs_init(const esp_vfs_spiffs_conf_t* conf)
  121. {
  122. int index;
  123. //find if such partition is already mounted
  124. if (esp_spiffs_by_label(conf->partition_label, &index) == ESP_OK) {
  125. return ESP_ERR_INVALID_STATE;
  126. }
  127. if (esp_spiffs_get_empty(&index) != ESP_OK) {
  128. ESP_LOGE(TAG, "max mounted partitions reached");
  129. return ESP_ERR_INVALID_STATE;
  130. }
  131. uint32_t flash_page_size = g_rom_flashchip.page_size;
  132. uint32_t log_page_size = CONFIG_SPIFFS_PAGE_SIZE;
  133. if (log_page_size % flash_page_size != 0) {
  134. ESP_LOGE(TAG, "SPIFFS_PAGE_SIZE is not multiple of flash chip page size (%d)",
  135. flash_page_size);
  136. return ESP_ERR_INVALID_ARG;
  137. }
  138. esp_partition_subtype_t subtype = conf->partition_label ?
  139. ESP_PARTITION_SUBTYPE_ANY : ESP_PARTITION_SUBTYPE_DATA_SPIFFS;
  140. const esp_partition_t* partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
  141. subtype, conf->partition_label);
  142. if (!partition) {
  143. ESP_LOGE(TAG, "spiffs partition could not be found");
  144. return ESP_ERR_NOT_FOUND;
  145. }
  146. if (partition->encrypted) {
  147. ESP_LOGE(TAG, "spiffs can not run on encrypted partition");
  148. return ESP_ERR_INVALID_STATE;
  149. }
  150. const size_t flash_erase_sector_size = g_rom_flashchip.sector_size;
  151. /* Older versions of IDF allowed creating misaligned data partitions.
  152. * This would result in hard-to-diagnose SPIFFS failures due to failing erase operations.
  153. */
  154. if (partition->address % flash_erase_sector_size != 0) {
  155. ESP_LOGE(TAG, "spiffs partition is not aligned to flash sector size, please check the partition table");
  156. /* No return intentional to avoid accidentally breaking applications
  157. * which used misaligned read-only SPIFFS partitions.
  158. */
  159. }
  160. /* Check if the SPIFFS internal data types are wide enough.
  161. * Casting -1 to the unsigned type produces the maximum value the type can hold.
  162. * All the checks here are based on comments for the said data types in spiffs_config.h.
  163. */
  164. if (partition->size / flash_erase_sector_size > (spiffs_block_ix) -1) {
  165. ESP_LOGE(TAG, "spiffs partition is too large for spiffs_block_ix type");
  166. return ESP_ERR_INVALID_ARG;
  167. }
  168. if (partition->size / log_page_size > (spiffs_page_ix) -1) {
  169. /* For 256 byte pages the largest partition is 16MB, but larger partitions can be supported
  170. * by increasing the page size (reducing the number of pages).
  171. */
  172. ESP_LOGE(TAG, "spiffs partition is too large for spiffs_page_ix type. Please increase CONFIG_SPIFFS_PAGE_SIZE.");
  173. return ESP_ERR_INVALID_ARG;
  174. }
  175. if (2 + 2 * (partition->size / (2 * log_page_size)) > (spiffs_obj_id) -1) {
  176. ESP_LOGE(TAG, "spiffs partition is too large for spiffs_obj_id type. Please increase CONFIG_SPIFFS_PAGE_SIZE.");
  177. return ESP_ERR_INVALID_ARG;
  178. }
  179. if (partition->size / log_page_size - 1 > (spiffs_span_ix) -1) {
  180. ESP_LOGE(TAG, "spiffs partition is too large for spiffs_span_ix type. Please increase CONFIG_SPIFFS_PAGE_SIZE.");
  181. return ESP_ERR_INVALID_ARG;
  182. }
  183. esp_spiffs_t * efs = calloc(sizeof(esp_spiffs_t), 1);
  184. if (efs == NULL) {
  185. ESP_LOGE(TAG, "esp_spiffs could not be malloced");
  186. return ESP_ERR_NO_MEM;
  187. }
  188. efs->cfg.hal_erase_f = spiffs_api_erase;
  189. efs->cfg.hal_read_f = spiffs_api_read;
  190. efs->cfg.hal_write_f = spiffs_api_write;
  191. efs->cfg.log_block_size = flash_erase_sector_size;
  192. efs->cfg.log_page_size = log_page_size;
  193. efs->cfg.phys_addr = 0;
  194. efs->cfg.phys_erase_block = flash_erase_sector_size;
  195. efs->cfg.phys_size = partition->size;
  196. efs->by_label = conf->partition_label != NULL;
  197. efs->lock = xSemaphoreCreateMutex();
  198. if (efs->lock == NULL) {
  199. ESP_LOGE(TAG, "mutex lock could not be created");
  200. esp_spiffs_free(&efs);
  201. return ESP_ERR_NO_MEM;
  202. }
  203. efs->fds_sz = conf->max_files * sizeof(spiffs_fd);
  204. efs->fds = calloc(efs->fds_sz, 1);
  205. if (efs->fds == NULL) {
  206. ESP_LOGE(TAG, "fd buffer could not be allocated");
  207. esp_spiffs_free(&efs);
  208. return ESP_ERR_NO_MEM;
  209. }
  210. #if SPIFFS_CACHE
  211. efs->cache_sz = sizeof(spiffs_cache) + conf->max_files * (sizeof(spiffs_cache_page)
  212. + efs->cfg.log_page_size);
  213. efs->cache = calloc(efs->cache_sz, 1);
  214. if (efs->cache == NULL) {
  215. ESP_LOGE(TAG, "cache buffer could not be allocated");
  216. esp_spiffs_free(&efs);
  217. return ESP_ERR_NO_MEM;
  218. }
  219. #endif
  220. const uint32_t work_sz = efs->cfg.log_page_size * 2;
  221. efs->work = calloc(work_sz, 1);
  222. if (efs->work == NULL) {
  223. ESP_LOGE(TAG, "work buffer could not be allocated");
  224. esp_spiffs_free(&efs);
  225. return ESP_ERR_NO_MEM;
  226. }
  227. efs->fs = calloc(sizeof(spiffs), 1);
  228. if (efs->fs == NULL) {
  229. ESP_LOGE(TAG, "spiffs could not be allocated");
  230. esp_spiffs_free(&efs);
  231. return ESP_ERR_NO_MEM;
  232. }
  233. efs->fs->user_data = (void *)efs;
  234. efs->partition = partition;
  235. s32_t res = SPIFFS_mount(efs->fs, &efs->cfg, efs->work, efs->fds, efs->fds_sz,
  236. efs->cache, efs->cache_sz, spiffs_api_check);
  237. if (conf->format_if_mount_failed && res != SPIFFS_OK) {
  238. ESP_LOGW(TAG, "mount failed, %i. formatting...", SPIFFS_errno(efs->fs));
  239. SPIFFS_clearerr(efs->fs);
  240. res = SPIFFS_format(efs->fs);
  241. if (res != SPIFFS_OK) {
  242. ESP_LOGE(TAG, "format failed, %i", SPIFFS_errno(efs->fs));
  243. SPIFFS_clearerr(efs->fs);
  244. esp_spiffs_free(&efs);
  245. return ESP_FAIL;
  246. }
  247. res = SPIFFS_mount(efs->fs, &efs->cfg, efs->work, efs->fds, efs->fds_sz,
  248. efs->cache, efs->cache_sz, spiffs_api_check);
  249. }
  250. if (res != SPIFFS_OK) {
  251. ESP_LOGE(TAG, "mount failed, %i", SPIFFS_errno(efs->fs));
  252. SPIFFS_clearerr(efs->fs);
  253. esp_spiffs_free(&efs);
  254. return ESP_FAIL;
  255. }
  256. _efs[index] = efs;
  257. return ESP_OK;
  258. }
  259. bool esp_spiffs_mounted(const char* partition_label)
  260. {
  261. int index;
  262. if (esp_spiffs_by_label(partition_label, &index) != ESP_OK) {
  263. return false;
  264. }
  265. return (SPIFFS_mounted(_efs[index]->fs));
  266. }
  267. esp_err_t esp_spiffs_info(const char* partition_label, size_t *total_bytes, size_t *used_bytes)
  268. {
  269. int index;
  270. if (esp_spiffs_by_label(partition_label, &index) != ESP_OK) {
  271. return ESP_ERR_INVALID_STATE;
  272. }
  273. SPIFFS_info(_efs[index]->fs, (uint32_t *)total_bytes, (uint32_t *)used_bytes);
  274. return ESP_OK;
  275. }
  276. esp_err_t esp_spiffs_check(const char* partition_label)
  277. {
  278. int index;
  279. if (esp_spiffs_by_label(partition_label, &index) != ESP_OK) {
  280. return ESP_ERR_INVALID_STATE;
  281. }
  282. if (SPIFFS_check(_efs[index]->fs) != SPIFFS_OK) {
  283. int spiffs_res = SPIFFS_errno(_efs[index]->fs);
  284. ESP_LOGE(TAG, "SPIFFS_check failed (%d)", spiffs_res);
  285. errno = spiffs_res_to_errno(SPIFFS_errno(_efs[index]->fs));
  286. SPIFFS_clearerr(_efs[index]->fs);
  287. return ESP_FAIL;
  288. }
  289. return ESP_OK;
  290. }
  291. esp_err_t esp_spiffs_format(const char* partition_label)
  292. {
  293. bool partition_was_mounted = false;
  294. int index;
  295. /* If the partition is not mounted, need to create SPIFFS structures
  296. * and mount the partition, unmount, format, delete SPIFFS structures.
  297. * See SPIFFS wiki for the reason why.
  298. */
  299. esp_err_t err = esp_spiffs_by_label(partition_label, &index);
  300. if (err != ESP_OK) {
  301. esp_vfs_spiffs_conf_t conf = {
  302. .format_if_mount_failed = true,
  303. .partition_label = partition_label,
  304. .max_files = 1
  305. };
  306. err = esp_spiffs_init(&conf);
  307. if (err != ESP_OK) {
  308. return err;
  309. }
  310. err = esp_spiffs_by_label(partition_label, &index);
  311. assert(err == ESP_OK && "failed to get index of the partition just mounted");
  312. } else if (SPIFFS_mounted(_efs[index]->fs)) {
  313. partition_was_mounted = true;
  314. }
  315. SPIFFS_unmount(_efs[index]->fs);
  316. s32_t res = SPIFFS_format(_efs[index]->fs);
  317. if (res != SPIFFS_OK) {
  318. ESP_LOGE(TAG, "format failed, %i", SPIFFS_errno(_efs[index]->fs));
  319. SPIFFS_clearerr(_efs[index]->fs);
  320. /* If the partition was previously mounted, but format failed, don't
  321. * try to mount the partition back (it will probably fail). On the
  322. * other hand, if it was not mounted, need to clean up.
  323. */
  324. if (!partition_was_mounted) {
  325. esp_spiffs_free(&_efs[index]);
  326. }
  327. return ESP_FAIL;
  328. }
  329. if (partition_was_mounted) {
  330. res = SPIFFS_mount(_efs[index]->fs, &_efs[index]->cfg, _efs[index]->work,
  331. _efs[index]->fds, _efs[index]->fds_sz, _efs[index]->cache,
  332. _efs[index]->cache_sz, spiffs_api_check);
  333. if (res != SPIFFS_OK) {
  334. ESP_LOGE(TAG, "mount failed, %i", SPIFFS_errno(_efs[index]->fs));
  335. SPIFFS_clearerr(_efs[index]->fs);
  336. return ESP_FAIL;
  337. }
  338. } else {
  339. esp_spiffs_free(&_efs[index]);
  340. }
  341. return ESP_OK;
  342. }
  343. esp_err_t esp_spiffs_gc(const char* partition_label, size_t size_to_gc)
  344. {
  345. int index;
  346. if (esp_spiffs_by_label(partition_label, &index) != ESP_OK) {
  347. return ESP_ERR_INVALID_STATE;
  348. }
  349. int res = SPIFFS_gc(_efs[index]->fs, size_to_gc);
  350. if (res != SPIFFS_OK) {
  351. ESP_LOGE(TAG, "SPIFFS_gc failed, %d", res);
  352. SPIFFS_clearerr(_efs[index]->fs);
  353. if (res == SPIFFS_ERR_FULL) {
  354. return ESP_ERR_NOT_FINISHED;
  355. }
  356. return ESP_FAIL;
  357. }
  358. return ESP_OK;
  359. }
  360. esp_err_t esp_vfs_spiffs_register(const esp_vfs_spiffs_conf_t * conf)
  361. {
  362. assert(conf->base_path);
  363. const esp_vfs_t vfs = {
  364. .flags = ESP_VFS_FLAG_CONTEXT_PTR,
  365. .write_p = &vfs_spiffs_write,
  366. .lseek_p = &vfs_spiffs_lseek,
  367. .read_p = &vfs_spiffs_read,
  368. .open_p = &vfs_spiffs_open,
  369. .close_p = &vfs_spiffs_close,
  370. .fstat_p = &vfs_spiffs_fstat,
  371. #ifdef CONFIG_VFS_SUPPORT_DIR
  372. .stat_p = &vfs_spiffs_stat,
  373. .link_p = &vfs_spiffs_link,
  374. .unlink_p = &vfs_spiffs_unlink,
  375. .rename_p = &vfs_spiffs_rename,
  376. .opendir_p = &vfs_spiffs_opendir,
  377. .closedir_p = &vfs_spiffs_closedir,
  378. .readdir_p = &vfs_spiffs_readdir,
  379. .readdir_r_p = &vfs_spiffs_readdir_r,
  380. .seekdir_p = &vfs_spiffs_seekdir,
  381. .telldir_p = &vfs_spiffs_telldir,
  382. .mkdir_p = &vfs_spiffs_mkdir,
  383. .rmdir_p = &vfs_spiffs_rmdir,
  384. .truncate_p = &vfs_spiffs_truncate,
  385. .ftruncate_p = &vfs_spiffs_ftruncate,
  386. #ifdef CONFIG_SPIFFS_USE_MTIME
  387. .utime_p = &vfs_spiffs_utime,
  388. #else
  389. .utime_p = NULL,
  390. #endif // CONFIG_SPIFFS_USE_MTIME
  391. #endif // CONFIG_VFS_SUPPORT_DIR
  392. };
  393. esp_err_t err = esp_spiffs_init(conf);
  394. if (err != ESP_OK) {
  395. return err;
  396. }
  397. int index;
  398. if (esp_spiffs_by_label(conf->partition_label, &index) != ESP_OK) {
  399. return ESP_ERR_INVALID_STATE;
  400. }
  401. strlcat(_efs[index]->base_path, conf->base_path, ESP_VFS_PATH_MAX + 1);
  402. err = esp_vfs_register(conf->base_path, &vfs, _efs[index]);
  403. if (err != ESP_OK) {
  404. esp_spiffs_free(&_efs[index]);
  405. return err;
  406. }
  407. return ESP_OK;
  408. }
  409. esp_err_t esp_vfs_spiffs_unregister(const char* partition_label)
  410. {
  411. int index;
  412. if (esp_spiffs_by_label(partition_label, &index) != ESP_OK) {
  413. return ESP_ERR_INVALID_STATE;
  414. }
  415. esp_err_t err = esp_vfs_unregister(_efs[index]->base_path);
  416. if (err != ESP_OK) {
  417. return err;
  418. }
  419. esp_spiffs_free(&_efs[index]);
  420. return ESP_OK;
  421. }
  422. static int spiffs_res_to_errno(s32_t fr)
  423. {
  424. switch(fr) {
  425. case SPIFFS_OK :
  426. return 0;
  427. case SPIFFS_ERR_NOT_MOUNTED :
  428. return ENODEV;
  429. case SPIFFS_ERR_NOT_A_FS :
  430. return ENODEV;
  431. case SPIFFS_ERR_FULL :
  432. return ENOSPC;
  433. case SPIFFS_ERR_BAD_DESCRIPTOR :
  434. return EBADF;
  435. case SPIFFS_ERR_MOUNTED :
  436. return EEXIST;
  437. case SPIFFS_ERR_FILE_EXISTS :
  438. return EEXIST;
  439. case SPIFFS_ERR_NOT_FOUND :
  440. return ENOENT;
  441. case SPIFFS_ERR_NOT_A_FILE :
  442. return ENOENT;
  443. case SPIFFS_ERR_DELETED :
  444. return ENOENT;
  445. case SPIFFS_ERR_FILE_DELETED :
  446. return ENOENT;
  447. case SPIFFS_ERR_NAME_TOO_LONG :
  448. return ENAMETOOLONG;
  449. case SPIFFS_ERR_RO_NOT_IMPL :
  450. return EROFS;
  451. case SPIFFS_ERR_RO_ABORTED_OPERATION :
  452. return EROFS;
  453. default :
  454. return EIO;
  455. }
  456. return ENOTSUP;
  457. }
  458. static int spiffs_mode_conv(int m)
  459. {
  460. int res = 0;
  461. int acc_mode = m & O_ACCMODE;
  462. if (acc_mode == O_RDONLY) {
  463. res |= SPIFFS_O_RDONLY;
  464. } else if (acc_mode == O_WRONLY) {
  465. res |= SPIFFS_O_WRONLY;
  466. } else if (acc_mode == O_RDWR) {
  467. res |= SPIFFS_O_RDWR;
  468. }
  469. if ((m & O_CREAT) && (m & O_EXCL)) {
  470. res |= SPIFFS_O_CREAT | SPIFFS_O_EXCL;
  471. } else if ((m & O_CREAT) && (m & O_TRUNC)) {
  472. res |= SPIFFS_O_CREAT | SPIFFS_O_TRUNC;
  473. }
  474. if (m & O_APPEND) {
  475. res |= SPIFFS_O_CREAT | SPIFFS_O_APPEND;
  476. }
  477. return res;
  478. }
  479. static int vfs_spiffs_open(void* ctx, const char * path, int flags, int mode)
  480. {
  481. assert(path);
  482. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  483. int spiffs_flags = spiffs_mode_conv(flags);
  484. int fd = SPIFFS_open(efs->fs, path, spiffs_flags, mode);
  485. if (fd < 0) {
  486. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  487. SPIFFS_clearerr(efs->fs);
  488. return -1;
  489. }
  490. if (!(spiffs_flags & SPIFFS_RDONLY)) {
  491. vfs_spiffs_update_mtime(efs->fs, fd);
  492. }
  493. return fd;
  494. }
  495. static ssize_t vfs_spiffs_write(void* ctx, int fd, const void * data, size_t size)
  496. {
  497. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  498. ssize_t res = SPIFFS_write(efs->fs, fd, (void *)data, size);
  499. if (res < 0) {
  500. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  501. SPIFFS_clearerr(efs->fs);
  502. return -1;
  503. }
  504. return res;
  505. }
  506. static ssize_t vfs_spiffs_read(void* ctx, int fd, void * dst, size_t size)
  507. {
  508. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  509. ssize_t res = SPIFFS_read(efs->fs, fd, dst, size);
  510. if (res < 0) {
  511. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  512. SPIFFS_clearerr(efs->fs);
  513. return -1;
  514. }
  515. return res;
  516. }
  517. static int vfs_spiffs_close(void* ctx, int fd)
  518. {
  519. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  520. int res = SPIFFS_close(efs->fs, fd);
  521. if (res < 0) {
  522. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  523. SPIFFS_clearerr(efs->fs);
  524. return -1;
  525. }
  526. return res;
  527. }
  528. static off_t vfs_spiffs_lseek(void* ctx, int fd, off_t offset, int mode)
  529. {
  530. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  531. off_t res = SPIFFS_lseek(efs->fs, fd, offset, mode);
  532. if (res < 0) {
  533. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  534. SPIFFS_clearerr(efs->fs);
  535. return -1;
  536. }
  537. return res;
  538. }
  539. static int vfs_spiffs_fstat(void* ctx, int fd, struct stat * st)
  540. {
  541. assert(st);
  542. spiffs_stat s;
  543. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  544. off_t res = SPIFFS_fstat(efs->fs, fd, &s);
  545. if (res < 0) {
  546. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  547. SPIFFS_clearerr(efs->fs);
  548. return -1;
  549. }
  550. memset(st, 0, sizeof(*st));
  551. st->st_size = s.size;
  552. st->st_mode = S_IRWXU | S_IRWXG | S_IRWXO | S_IFREG;
  553. st->st_mtime = vfs_spiffs_get_mtime(&s);
  554. st->st_atime = 0;
  555. st->st_ctime = 0;
  556. return res;
  557. }
  558. #ifdef CONFIG_VFS_SUPPORT_DIR
  559. static int vfs_spiffs_stat(void* ctx, const char * path, struct stat * st)
  560. {
  561. assert(path);
  562. assert(st);
  563. spiffs_stat s;
  564. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  565. off_t res = SPIFFS_stat(efs->fs, path, &s);
  566. if (res < 0) {
  567. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  568. SPIFFS_clearerr(efs->fs);
  569. return -1;
  570. }
  571. memset(st, 0, sizeof(*st));
  572. st->st_size = s.size;
  573. st->st_mode = S_IRWXU | S_IRWXG | S_IRWXO;
  574. st->st_mode |= (s.type == SPIFFS_TYPE_DIR)?S_IFDIR:S_IFREG;
  575. st->st_mtime = vfs_spiffs_get_mtime(&s);
  576. st->st_atime = 0;
  577. st->st_ctime = 0;
  578. return res;
  579. }
  580. static int vfs_spiffs_rename(void* ctx, const char *src, const char *dst)
  581. {
  582. assert(src);
  583. assert(dst);
  584. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  585. int res = SPIFFS_rename(efs->fs, src, dst);
  586. if (res < 0) {
  587. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  588. SPIFFS_clearerr(efs->fs);
  589. return -1;
  590. }
  591. return res;
  592. }
  593. static int vfs_spiffs_unlink(void* ctx, const char *path)
  594. {
  595. assert(path);
  596. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  597. int res = SPIFFS_remove(efs->fs, path);
  598. if (res < 0) {
  599. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  600. SPIFFS_clearerr(efs->fs);
  601. return -1;
  602. }
  603. return res;
  604. }
  605. static DIR* vfs_spiffs_opendir(void* ctx, const char* name)
  606. {
  607. assert(name);
  608. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  609. vfs_spiffs_dir_t * dir = calloc(1, sizeof(vfs_spiffs_dir_t));
  610. if (!dir) {
  611. errno = ENOMEM;
  612. return NULL;
  613. }
  614. if (!SPIFFS_opendir(efs->fs, name, &dir->d)) {
  615. free(dir);
  616. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  617. SPIFFS_clearerr(efs->fs);
  618. return NULL;
  619. }
  620. dir->offset = 0;
  621. strlcpy(dir->path, name, SPIFFS_OBJ_NAME_LEN);
  622. return (DIR*) dir;
  623. }
  624. static int vfs_spiffs_closedir(void* ctx, DIR* pdir)
  625. {
  626. assert(pdir);
  627. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  628. vfs_spiffs_dir_t * dir = (vfs_spiffs_dir_t *)pdir;
  629. int res = SPIFFS_closedir(&dir->d);
  630. free(dir);
  631. if (res < 0) {
  632. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  633. SPIFFS_clearerr(efs->fs);
  634. return -1;
  635. }
  636. return res;
  637. }
  638. static struct dirent* vfs_spiffs_readdir(void* ctx, DIR* pdir)
  639. {
  640. assert(pdir);
  641. vfs_spiffs_dir_t * dir = (vfs_spiffs_dir_t *)pdir;
  642. struct dirent* out_dirent;
  643. int err = vfs_spiffs_readdir_r(ctx, pdir, &dir->e, &out_dirent);
  644. if (err != 0) {
  645. errno = err;
  646. return NULL;
  647. }
  648. return out_dirent;
  649. }
  650. static int vfs_spiffs_readdir_r(void* ctx, DIR* pdir, struct dirent* entry,
  651. struct dirent** out_dirent)
  652. {
  653. assert(pdir);
  654. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  655. vfs_spiffs_dir_t * dir = (vfs_spiffs_dir_t *)pdir;
  656. struct spiffs_dirent out;
  657. size_t plen;
  658. char * item_name;
  659. do {
  660. if (SPIFFS_readdir(&dir->d, &out) == 0) {
  661. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  662. SPIFFS_clearerr(efs->fs);
  663. if (!errno) {
  664. *out_dirent = NULL;
  665. }
  666. return errno;
  667. }
  668. item_name = (char *)out.name;
  669. plen = strlen(dir->path);
  670. } while ((plen > 1) && (strncasecmp(dir->path, (const char*)out.name, plen) || out.name[plen] != '/' || !out.name[plen + 1]));
  671. if (plen > 1) {
  672. item_name += plen + 1;
  673. } else if (item_name[0] == '/') {
  674. item_name++;
  675. }
  676. entry->d_ino = 0;
  677. entry->d_type = out.type;
  678. strncpy(entry->d_name, item_name, SPIFFS_OBJ_NAME_LEN);
  679. entry->d_name[SPIFFS_OBJ_NAME_LEN - 1] = '\0';
  680. dir->offset++;
  681. *out_dirent = entry;
  682. return 0;
  683. }
  684. static long vfs_spiffs_telldir(void* ctx, DIR* pdir)
  685. {
  686. assert(pdir);
  687. vfs_spiffs_dir_t * dir = (vfs_spiffs_dir_t *)pdir;
  688. return dir->offset;
  689. }
  690. static void vfs_spiffs_seekdir(void* ctx, DIR* pdir, long offset)
  691. {
  692. assert(pdir);
  693. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  694. vfs_spiffs_dir_t * dir = (vfs_spiffs_dir_t *)pdir;
  695. struct spiffs_dirent tmp;
  696. if (offset < dir->offset) {
  697. //rewind dir
  698. SPIFFS_closedir(&dir->d);
  699. if (!SPIFFS_opendir(efs->fs, NULL, &dir->d)) {
  700. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  701. SPIFFS_clearerr(efs->fs);
  702. return;
  703. }
  704. dir->offset = 0;
  705. }
  706. while (dir->offset < offset) {
  707. if (SPIFFS_readdir(&dir->d, &tmp) == 0) {
  708. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  709. SPIFFS_clearerr(efs->fs);
  710. return;
  711. }
  712. size_t plen = strlen(dir->path);
  713. if (plen > 1) {
  714. if (strncasecmp(dir->path, (const char *)tmp.name, plen) || tmp.name[plen] != '/' || !tmp.name[plen+1]) {
  715. continue;
  716. }
  717. }
  718. dir->offset++;
  719. }
  720. }
  721. static int vfs_spiffs_mkdir(void* ctx, const char* name, mode_t mode)
  722. {
  723. errno = ENOTSUP;
  724. return -1;
  725. }
  726. static int vfs_spiffs_rmdir(void* ctx, const char* name)
  727. {
  728. errno = ENOTSUP;
  729. return -1;
  730. }
  731. static int vfs_spiffs_truncate(void* ctx, const char *path, off_t length)
  732. {
  733. assert(path);
  734. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  735. int fd = SPIFFS_open(efs->fs, path, SPIFFS_WRONLY, 0);
  736. if (fd < 0) {
  737. goto err;
  738. }
  739. int res = SPIFFS_ftruncate(efs->fs, fd, length);
  740. if (res < 0) {
  741. (void)SPIFFS_close(efs->fs, fd);
  742. goto err;
  743. }
  744. res = SPIFFS_close(efs->fs, fd);
  745. if (res < 0) {
  746. goto err;
  747. }
  748. return res;
  749. err:
  750. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  751. SPIFFS_clearerr(efs->fs);
  752. return -1;
  753. }
  754. static int vfs_spiffs_ftruncate(void* ctx, int fd, off_t length)
  755. {
  756. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  757. int res = SPIFFS_ftruncate(efs->fs, fd, length);
  758. if (res < 0) {
  759. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  760. SPIFFS_clearerr(efs->fs);
  761. return -1;
  762. }
  763. return res;
  764. }
  765. static int vfs_spiffs_link(void* ctx, const char* n1, const char* n2)
  766. {
  767. errno = ENOTSUP;
  768. return -1;
  769. }
  770. #ifdef CONFIG_SPIFFS_USE_MTIME
  771. static int vfs_spiffs_update_mtime_value(spiffs *fs, const char *path, spiffs_time_t t)
  772. {
  773. int ret = SPIFFS_OK;
  774. spiffs_stat s;
  775. if (CONFIG_SPIFFS_META_LENGTH > sizeof(t)) {
  776. ret = SPIFFS_stat(fs, path, &s);
  777. }
  778. if (ret == SPIFFS_OK) {
  779. memcpy(s.meta, &t, sizeof(t));
  780. ret = SPIFFS_update_meta(fs, path, s.meta);
  781. }
  782. if (ret != SPIFFS_OK) {
  783. ESP_LOGW(TAG, "Failed to update mtime (%d)", ret);
  784. }
  785. return ret;
  786. }
  787. #endif //CONFIG_SPIFFS_USE_MTIME
  788. #ifdef CONFIG_SPIFFS_USE_MTIME
  789. static int vfs_spiffs_utime(void *ctx, const char *path, const struct utimbuf *times)
  790. {
  791. assert(path);
  792. esp_spiffs_t *efs = (esp_spiffs_t *) ctx;
  793. spiffs_time_t t;
  794. if (times) {
  795. t = (spiffs_time_t)times->modtime;
  796. } else {
  797. // use current time
  798. t = (spiffs_time_t)time(NULL);
  799. }
  800. int ret = vfs_spiffs_update_mtime_value(efs->fs, path, t);
  801. if (ret != SPIFFS_OK) {
  802. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  803. SPIFFS_clearerr(efs->fs);
  804. return -1;
  805. }
  806. return 0;
  807. }
  808. #endif //CONFIG_SPIFFS_USE_MTIME
  809. #endif // CONFIG_VFS_SUPPORT_DIR
  810. static void vfs_spiffs_update_mtime(spiffs *fs, spiffs_file fd)
  811. {
  812. #ifdef CONFIG_SPIFFS_USE_MTIME
  813. spiffs_time_t t = (spiffs_time_t)time(NULL);
  814. spiffs_stat s;
  815. int ret = SPIFFS_OK;
  816. if (CONFIG_SPIFFS_META_LENGTH > sizeof(t)) {
  817. ret = SPIFFS_fstat(fs, fd, &s);
  818. }
  819. if (ret == SPIFFS_OK) {
  820. memcpy(s.meta, &t, sizeof(t));
  821. ret = SPIFFS_fupdate_meta(fs, fd, s.meta);
  822. }
  823. if (ret != SPIFFS_OK) {
  824. ESP_LOGW(TAG, "Failed to update mtime (%d)", ret);
  825. }
  826. #endif //CONFIG_SPIFFS_USE_MTIME
  827. }
  828. static time_t vfs_spiffs_get_mtime(const spiffs_stat* s)
  829. {
  830. #ifdef CONFIG_SPIFFS_USE_MTIME
  831. spiffs_time_t t = 0;
  832. memcpy(&t, s->meta, sizeof(t));
  833. #else
  834. time_t t = 0;
  835. #endif
  836. return (time_t)t;
  837. }