esp_spiffs.c 24 KB

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