esp_spiffs.c 23 KB

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