esp_spiffs.c 24 KB

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