esp_spiffs.c 24 KB

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