esp_spiffs.c 24 KB

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