esp_spiffs.c 25 KB

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