esp_spiffs.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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 "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 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_vfs_spiffs_register(const esp_vfs_spiffs_conf_t * conf)
  344. {
  345. assert(conf->base_path);
  346. const esp_vfs_t vfs = {
  347. .flags = ESP_VFS_FLAG_CONTEXT_PTR,
  348. .write_p = &vfs_spiffs_write,
  349. .lseek_p = &vfs_spiffs_lseek,
  350. .read_p = &vfs_spiffs_read,
  351. .open_p = &vfs_spiffs_open,
  352. .close_p = &vfs_spiffs_close,
  353. .fstat_p = &vfs_spiffs_fstat,
  354. #ifdef CONFIG_VFS_SUPPORT_DIR
  355. .stat_p = &vfs_spiffs_stat,
  356. .link_p = &vfs_spiffs_link,
  357. .unlink_p = &vfs_spiffs_unlink,
  358. .rename_p = &vfs_spiffs_rename,
  359. .opendir_p = &vfs_spiffs_opendir,
  360. .closedir_p = &vfs_spiffs_closedir,
  361. .readdir_p = &vfs_spiffs_readdir,
  362. .readdir_r_p = &vfs_spiffs_readdir_r,
  363. .seekdir_p = &vfs_spiffs_seekdir,
  364. .telldir_p = &vfs_spiffs_telldir,
  365. .mkdir_p = &vfs_spiffs_mkdir,
  366. .rmdir_p = &vfs_spiffs_rmdir,
  367. .truncate_p = &vfs_spiffs_truncate,
  368. .ftruncate_p = &vfs_spiffs_ftruncate,
  369. #ifdef CONFIG_SPIFFS_USE_MTIME
  370. .utime_p = &vfs_spiffs_utime,
  371. #else
  372. .utime_p = NULL,
  373. #endif // CONFIG_SPIFFS_USE_MTIME
  374. #endif // CONFIG_VFS_SUPPORT_DIR
  375. };
  376. esp_err_t err = esp_spiffs_init(conf);
  377. if (err != ESP_OK) {
  378. return err;
  379. }
  380. int index;
  381. if (esp_spiffs_by_label(conf->partition_label, &index) != ESP_OK) {
  382. return ESP_ERR_INVALID_STATE;
  383. }
  384. strlcat(_efs[index]->base_path, conf->base_path, ESP_VFS_PATH_MAX + 1);
  385. err = esp_vfs_register(conf->base_path, &vfs, _efs[index]);
  386. if (err != ESP_OK) {
  387. esp_spiffs_free(&_efs[index]);
  388. return err;
  389. }
  390. return ESP_OK;
  391. }
  392. esp_err_t esp_vfs_spiffs_unregister(const char* partition_label)
  393. {
  394. int index;
  395. if (esp_spiffs_by_label(partition_label, &index) != ESP_OK) {
  396. return ESP_ERR_INVALID_STATE;
  397. }
  398. esp_err_t err = esp_vfs_unregister(_efs[index]->base_path);
  399. if (err != ESP_OK) {
  400. return err;
  401. }
  402. esp_spiffs_free(&_efs[index]);
  403. return ESP_OK;
  404. }
  405. static int spiffs_res_to_errno(s32_t fr)
  406. {
  407. switch(fr) {
  408. case SPIFFS_OK :
  409. return 0;
  410. case SPIFFS_ERR_NOT_MOUNTED :
  411. return ENODEV;
  412. case SPIFFS_ERR_NOT_A_FS :
  413. return ENODEV;
  414. case SPIFFS_ERR_FULL :
  415. return ENOSPC;
  416. case SPIFFS_ERR_BAD_DESCRIPTOR :
  417. return EBADF;
  418. case SPIFFS_ERR_MOUNTED :
  419. return EEXIST;
  420. case SPIFFS_ERR_FILE_EXISTS :
  421. return EEXIST;
  422. case SPIFFS_ERR_NOT_FOUND :
  423. return ENOENT;
  424. case SPIFFS_ERR_NOT_A_FILE :
  425. return ENOENT;
  426. case SPIFFS_ERR_DELETED :
  427. return ENOENT;
  428. case SPIFFS_ERR_FILE_DELETED :
  429. return ENOENT;
  430. case SPIFFS_ERR_NAME_TOO_LONG :
  431. return ENAMETOOLONG;
  432. case SPIFFS_ERR_RO_NOT_IMPL :
  433. return EROFS;
  434. case SPIFFS_ERR_RO_ABORTED_OPERATION :
  435. return EROFS;
  436. default :
  437. return EIO;
  438. }
  439. return ENOTSUP;
  440. }
  441. static int spiffs_mode_conv(int m)
  442. {
  443. int res = 0;
  444. int acc_mode = m & O_ACCMODE;
  445. if (acc_mode == O_RDONLY) {
  446. res |= SPIFFS_O_RDONLY;
  447. } else if (acc_mode == O_WRONLY) {
  448. res |= SPIFFS_O_WRONLY;
  449. } else if (acc_mode == O_RDWR) {
  450. res |= SPIFFS_O_RDWR;
  451. }
  452. if ((m & O_CREAT) && (m & O_EXCL)) {
  453. res |= SPIFFS_O_CREAT | SPIFFS_O_EXCL;
  454. } else if ((m & O_CREAT) && (m & O_TRUNC)) {
  455. res |= SPIFFS_O_CREAT | SPIFFS_O_TRUNC;
  456. }
  457. if (m & O_APPEND) {
  458. res |= SPIFFS_O_CREAT | SPIFFS_O_APPEND;
  459. }
  460. return res;
  461. }
  462. static int vfs_spiffs_open(void* ctx, const char * path, int flags, int mode)
  463. {
  464. assert(path);
  465. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  466. int spiffs_flags = spiffs_mode_conv(flags);
  467. int fd = SPIFFS_open(efs->fs, path, spiffs_flags, mode);
  468. if (fd < 0) {
  469. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  470. SPIFFS_clearerr(efs->fs);
  471. return -1;
  472. }
  473. if (!(spiffs_flags & SPIFFS_RDONLY)) {
  474. vfs_spiffs_update_mtime(efs->fs, fd);
  475. }
  476. return fd;
  477. }
  478. static ssize_t vfs_spiffs_write(void* ctx, int fd, const void * data, size_t size)
  479. {
  480. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  481. ssize_t res = SPIFFS_write(efs->fs, fd, (void *)data, size);
  482. if (res < 0) {
  483. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  484. SPIFFS_clearerr(efs->fs);
  485. return -1;
  486. }
  487. return res;
  488. }
  489. static ssize_t vfs_spiffs_read(void* ctx, int fd, void * dst, size_t size)
  490. {
  491. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  492. ssize_t res = SPIFFS_read(efs->fs, fd, dst, size);
  493. if (res < 0) {
  494. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  495. SPIFFS_clearerr(efs->fs);
  496. return -1;
  497. }
  498. return res;
  499. }
  500. static int vfs_spiffs_close(void* ctx, int fd)
  501. {
  502. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  503. int res = SPIFFS_close(efs->fs, fd);
  504. if (res < 0) {
  505. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  506. SPIFFS_clearerr(efs->fs);
  507. return -1;
  508. }
  509. return res;
  510. }
  511. static off_t vfs_spiffs_lseek(void* ctx, int fd, off_t offset, int mode)
  512. {
  513. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  514. off_t res = SPIFFS_lseek(efs->fs, fd, offset, mode);
  515. if (res < 0) {
  516. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  517. SPIFFS_clearerr(efs->fs);
  518. return -1;
  519. }
  520. return res;
  521. }
  522. static int vfs_spiffs_fstat(void* ctx, int fd, struct stat * st)
  523. {
  524. assert(st);
  525. spiffs_stat s;
  526. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  527. off_t res = SPIFFS_fstat(efs->fs, fd, &s);
  528. if (res < 0) {
  529. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  530. SPIFFS_clearerr(efs->fs);
  531. return -1;
  532. }
  533. memset(st, 0, sizeof(*st));
  534. st->st_size = s.size;
  535. st->st_mode = S_IRWXU | S_IRWXG | S_IRWXO | S_IFREG;
  536. st->st_mtime = vfs_spiffs_get_mtime(&s);
  537. st->st_atime = 0;
  538. st->st_ctime = 0;
  539. return res;
  540. }
  541. #ifdef CONFIG_VFS_SUPPORT_DIR
  542. static int vfs_spiffs_stat(void* ctx, const char * path, struct stat * st)
  543. {
  544. assert(path);
  545. assert(st);
  546. spiffs_stat s;
  547. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  548. off_t res = SPIFFS_stat(efs->fs, path, &s);
  549. if (res < 0) {
  550. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  551. SPIFFS_clearerr(efs->fs);
  552. return -1;
  553. }
  554. memset(st, 0, sizeof(*st));
  555. st->st_size = s.size;
  556. st->st_mode = S_IRWXU | S_IRWXG | S_IRWXO;
  557. st->st_mode |= (s.type == SPIFFS_TYPE_DIR)?S_IFDIR:S_IFREG;
  558. st->st_mtime = vfs_spiffs_get_mtime(&s);
  559. st->st_atime = 0;
  560. st->st_ctime = 0;
  561. return res;
  562. }
  563. static int vfs_spiffs_rename(void* ctx, const char *src, const char *dst)
  564. {
  565. assert(src);
  566. assert(dst);
  567. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  568. int res = SPIFFS_rename(efs->fs, src, dst);
  569. if (res < 0) {
  570. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  571. SPIFFS_clearerr(efs->fs);
  572. return -1;
  573. }
  574. return res;
  575. }
  576. static int vfs_spiffs_unlink(void* ctx, const char *path)
  577. {
  578. assert(path);
  579. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  580. int res = SPIFFS_remove(efs->fs, path);
  581. if (res < 0) {
  582. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  583. SPIFFS_clearerr(efs->fs);
  584. return -1;
  585. }
  586. return res;
  587. }
  588. static DIR* vfs_spiffs_opendir(void* ctx, const char* name)
  589. {
  590. assert(name);
  591. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  592. vfs_spiffs_dir_t * dir = calloc(1, sizeof(vfs_spiffs_dir_t));
  593. if (!dir) {
  594. errno = ENOMEM;
  595. return NULL;
  596. }
  597. if (!SPIFFS_opendir(efs->fs, name, &dir->d)) {
  598. free(dir);
  599. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  600. SPIFFS_clearerr(efs->fs);
  601. return NULL;
  602. }
  603. dir->offset = 0;
  604. strlcpy(dir->path, name, SPIFFS_OBJ_NAME_LEN);
  605. return (DIR*) dir;
  606. }
  607. static int vfs_spiffs_closedir(void* ctx, DIR* pdir)
  608. {
  609. assert(pdir);
  610. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  611. vfs_spiffs_dir_t * dir = (vfs_spiffs_dir_t *)pdir;
  612. int res = SPIFFS_closedir(&dir->d);
  613. free(dir);
  614. if (res < 0) {
  615. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  616. SPIFFS_clearerr(efs->fs);
  617. return -1;
  618. }
  619. return res;
  620. }
  621. static struct dirent* vfs_spiffs_readdir(void* ctx, DIR* pdir)
  622. {
  623. assert(pdir);
  624. vfs_spiffs_dir_t * dir = (vfs_spiffs_dir_t *)pdir;
  625. struct dirent* out_dirent;
  626. int err = vfs_spiffs_readdir_r(ctx, pdir, &dir->e, &out_dirent);
  627. if (err != 0) {
  628. errno = err;
  629. return NULL;
  630. }
  631. return out_dirent;
  632. }
  633. static int vfs_spiffs_readdir_r(void* ctx, DIR* pdir, struct dirent* entry,
  634. struct dirent** out_dirent)
  635. {
  636. assert(pdir);
  637. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  638. vfs_spiffs_dir_t * dir = (vfs_spiffs_dir_t *)pdir;
  639. struct spiffs_dirent out;
  640. size_t plen;
  641. char * item_name;
  642. do {
  643. if (SPIFFS_readdir(&dir->d, &out) == 0) {
  644. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  645. SPIFFS_clearerr(efs->fs);
  646. if (!errno) {
  647. *out_dirent = NULL;
  648. }
  649. return errno;
  650. }
  651. item_name = (char *)out.name;
  652. plen = strlen(dir->path);
  653. } while ((plen > 1) && (strncasecmp(dir->path, (const char*)out.name, plen) || out.name[plen] != '/' || !out.name[plen + 1]));
  654. if (plen > 1) {
  655. item_name += plen + 1;
  656. } else if (item_name[0] == '/') {
  657. item_name++;
  658. }
  659. entry->d_ino = 0;
  660. entry->d_type = out.type;
  661. strncpy(entry->d_name, item_name, SPIFFS_OBJ_NAME_LEN);
  662. entry->d_name[SPIFFS_OBJ_NAME_LEN - 1] = '\0';
  663. dir->offset++;
  664. *out_dirent = entry;
  665. return 0;
  666. }
  667. static long vfs_spiffs_telldir(void* ctx, DIR* pdir)
  668. {
  669. assert(pdir);
  670. vfs_spiffs_dir_t * dir = (vfs_spiffs_dir_t *)pdir;
  671. return dir->offset;
  672. }
  673. static void vfs_spiffs_seekdir(void* ctx, DIR* pdir, long offset)
  674. {
  675. assert(pdir);
  676. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  677. vfs_spiffs_dir_t * dir = (vfs_spiffs_dir_t *)pdir;
  678. struct spiffs_dirent tmp;
  679. if (offset < dir->offset) {
  680. //rewind dir
  681. SPIFFS_closedir(&dir->d);
  682. if (!SPIFFS_opendir(efs->fs, NULL, &dir->d)) {
  683. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  684. SPIFFS_clearerr(efs->fs);
  685. return;
  686. }
  687. dir->offset = 0;
  688. }
  689. while (dir->offset < offset) {
  690. if (SPIFFS_readdir(&dir->d, &tmp) == 0) {
  691. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  692. SPIFFS_clearerr(efs->fs);
  693. return;
  694. }
  695. size_t plen = strlen(dir->path);
  696. if (plen > 1) {
  697. if (strncasecmp(dir->path, (const char *)tmp.name, plen) || tmp.name[plen] != '/' || !tmp.name[plen+1]) {
  698. continue;
  699. }
  700. }
  701. dir->offset++;
  702. }
  703. }
  704. static int vfs_spiffs_mkdir(void* ctx, const char* name, mode_t mode)
  705. {
  706. errno = ENOTSUP;
  707. return -1;
  708. }
  709. static int vfs_spiffs_rmdir(void* ctx, const char* name)
  710. {
  711. errno = ENOTSUP;
  712. return -1;
  713. }
  714. static int vfs_spiffs_truncate(void* ctx, const char *path, off_t length)
  715. {
  716. assert(path);
  717. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  718. int fd = SPIFFS_open(efs->fs, path, SPIFFS_WRONLY, 0);
  719. if (fd < 0) {
  720. goto err;
  721. }
  722. int res = SPIFFS_ftruncate(efs->fs, fd, length);
  723. if (res < 0) {
  724. (void)SPIFFS_close(efs->fs, fd);
  725. goto err;
  726. }
  727. res = SPIFFS_close(efs->fs, fd);
  728. if (res < 0) {
  729. goto err;
  730. }
  731. return res;
  732. err:
  733. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  734. SPIFFS_clearerr(efs->fs);
  735. return -1;
  736. }
  737. static int vfs_spiffs_ftruncate(void* ctx, int fd, off_t length)
  738. {
  739. esp_spiffs_t * efs = (esp_spiffs_t *)ctx;
  740. int res = SPIFFS_ftruncate(efs->fs, fd, length);
  741. if (res < 0) {
  742. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  743. SPIFFS_clearerr(efs->fs);
  744. return -1;
  745. }
  746. return res;
  747. }
  748. static int vfs_spiffs_link(void* ctx, const char* n1, const char* n2)
  749. {
  750. errno = ENOTSUP;
  751. return -1;
  752. }
  753. #ifdef CONFIG_SPIFFS_USE_MTIME
  754. static int vfs_spiffs_update_mtime_value(spiffs *fs, const char *path, spiffs_time_t t)
  755. {
  756. int ret = SPIFFS_OK;
  757. spiffs_stat s;
  758. if (CONFIG_SPIFFS_META_LENGTH > sizeof(t)) {
  759. ret = SPIFFS_stat(fs, path, &s);
  760. }
  761. if (ret == SPIFFS_OK) {
  762. memcpy(s.meta, &t, sizeof(t));
  763. ret = SPIFFS_update_meta(fs, path, s.meta);
  764. }
  765. if (ret != SPIFFS_OK) {
  766. ESP_LOGW(TAG, "Failed to update mtime (%d)", ret);
  767. }
  768. return ret;
  769. }
  770. #endif //CONFIG_SPIFFS_USE_MTIME
  771. #ifdef CONFIG_SPIFFS_USE_MTIME
  772. static int vfs_spiffs_utime(void *ctx, const char *path, const struct utimbuf *times)
  773. {
  774. assert(path);
  775. esp_spiffs_t *efs = (esp_spiffs_t *) ctx;
  776. spiffs_time_t t;
  777. if (times) {
  778. t = (spiffs_time_t)times->modtime;
  779. } else {
  780. // use current time
  781. t = (spiffs_time_t)time(NULL);
  782. }
  783. int ret = vfs_spiffs_update_mtime_value(efs->fs, path, t);
  784. if (ret != SPIFFS_OK) {
  785. errno = spiffs_res_to_errno(SPIFFS_errno(efs->fs));
  786. SPIFFS_clearerr(efs->fs);
  787. return -1;
  788. }
  789. return 0;
  790. }
  791. #endif //CONFIG_SPIFFS_USE_MTIME
  792. #endif // CONFIG_VFS_SUPPORT_DIR
  793. static void vfs_spiffs_update_mtime(spiffs *fs, spiffs_file fd)
  794. {
  795. #ifdef CONFIG_SPIFFS_USE_MTIME
  796. spiffs_time_t t = (spiffs_time_t)time(NULL);
  797. spiffs_stat s;
  798. int ret = SPIFFS_OK;
  799. if (CONFIG_SPIFFS_META_LENGTH > sizeof(t)) {
  800. ret = SPIFFS_fstat(fs, fd, &s);
  801. }
  802. if (ret == SPIFFS_OK) {
  803. memcpy(s.meta, &t, sizeof(t));
  804. ret = SPIFFS_fupdate_meta(fs, fd, s.meta);
  805. }
  806. if (ret != SPIFFS_OK) {
  807. ESP_LOGW(TAG, "Failed to update mtime (%d)", ret);
  808. }
  809. #endif //CONFIG_SPIFFS_USE_MTIME
  810. }
  811. static time_t vfs_spiffs_get_mtime(const spiffs_stat* s)
  812. {
  813. #ifdef CONFIG_SPIFFS_USE_MTIME
  814. spiffs_time_t t = 0;
  815. memcpy(&t, s->meta, sizeof(t));
  816. #else
  817. time_t t = 0;
  818. #endif
  819. return (time_t)t;
  820. }