esp_spiffs.c 23 KB

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