esp_spiffs.c 23 KB

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