esp_spiffs.c 24 KB

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