esp_spiffs.c 22 KB

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