esp_spiffs.c 24 KB

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