esp_spiffs.c 22 KB

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