From 4814f062833a8fbb7e290f319e7410a9f6dd6be6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Rohl=C3=ADnek?= Date: Fri, 25 Oct 2024 12:23:32 +0200 Subject: [PATCH 1/6] feat(storage/vfs): refactor VFS calls to multiple files --- components/vfs/CMakeLists.txt | 3 +- components/vfs/linker.lf | 4 +- .../vfs/private_include/esp_vfs_private.h | 49 + .../vfs/private_include/esp_vfs_utils.h | 90 ++ components/vfs/vfs.c | 1134 +---------------- components/vfs/vfs_calls.c | 975 ++++++++++++++ 6 files changed, 1178 insertions(+), 1077 deletions(-) create mode 100644 components/vfs/private_include/esp_vfs_utils.h create mode 100644 components/vfs/vfs_calls.c diff --git a/components/vfs/CMakeLists.txt b/components/vfs/CMakeLists.txt index 10fb7ac53d..0611091dcb 100644 --- a/components/vfs/CMakeLists.txt +++ b/components/vfs/CMakeLists.txt @@ -7,7 +7,7 @@ if(${target} STREQUAL "linux") if(CONFIG_VFS_SUPPORT_IO) list(APPEND inc linux_include) list(APPEND priv_inc private_include) - list(APPEND srcs "vfs_linux.c" "vfs.c") + list(APPEND srcs "vfs_linux.c" "vfs.c" "vfs_calls.c") endif() if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux") @@ -36,6 +36,7 @@ if(IDF_BUILD_V2) endif() list(APPEND sources "vfs.c" + "vfs_calls.c" "vfs_eventfd.c" "vfs_semihost.c" "nullfs.c" diff --git a/components/vfs/linker.lf b/components/vfs/linker.lf index 90d1d43f20..16aaa57de3 100644 --- a/components/vfs/linker.lf +++ b/components/vfs/linker.lf @@ -2,4 +2,6 @@ archive: libvfs.a entries: if VFS_SELECT_IN_RAM = y: - vfs:esp_vfs_select_triggered_isr (noflash) + vfs_calls:esp_vfs_select_triggered_isr (noflash) + vfs:get_vfs_count (noflash) + vfs:start_select (noflash) diff --git a/components/vfs/private_include/esp_vfs_private.h b/components/vfs/private_include/esp_vfs_private.h index ffe67e5ad9..d599f3e90b 100644 --- a/components/vfs/private_include/esp_vfs_private.h +++ b/components/vfs/private_include/esp_vfs_private.h @@ -18,6 +18,14 @@ extern "C" { #define VFS_MALLOC_FLAGS MALLOC_CAP_DEFAULT #endif +#ifdef CONFIG_IDF_TARGET_LINUX +typedef uint16_t local_fd_t; +#else +typedef uint8_t local_fd_t; +#endif + +typedef int8_t vfs_index_t; + typedef struct vfs_entry_ { int flags; /*!< ESP_VFS_FLAG_CONTEXT_PTR and/or ESP_VFS_FLAG_READONLY_FS or ESP_VFS_FLAG_DEFAULT */ const esp_vfs_fs_ops_t *vfs; /*!< contains pointers to VFS functions */ @@ -27,6 +35,22 @@ typedef struct vfs_entry_ { const char path_prefix[]; /*!< path prefix mapped to this VFS */ } vfs_entry_t; +typedef struct { + bool permanent :1; + bool has_pending_close :1; + bool has_pending_select :1; + uint8_t _reserved :5; + vfs_index_t vfs_index; + local_fd_t local_fd; +} fd_table_t; + +typedef struct { + bool isset; // none or at least one bit is set in the following 3 fd sets + fd_set readfds; + fd_set writefds; + fd_set errorfds; +} fds_triple_t; + /** * Register a virtual filesystem. * @@ -73,6 +97,31 @@ const vfs_entry_t *get_vfs_for_path(const char *path); */ const vfs_entry_t *get_vfs_for_index(int index); +const char* translate_path(const vfs_entry_t* vfs, const char* src_path); + +const vfs_entry_t* get_vfs_for_path(const char* path); + +const vfs_entry_t *get_vfs_for_fd(int fd); + +int register_fd(int vfs_index, int local_fd, bool permanent); + +void unregister_fd(int fd); + +int get_local_fd(const vfs_entry_t *vfs, int fd); + +const fd_table_t *get_fd_entry(int fd); + +size_t get_vfs_count(void); + +void close_pending(int nfds); + +static inline bool esp_vfs_safe_fd_isset(int fd, const fd_set *fds) +{ + return fds && FD_ISSET(fd, fds); +} + +fd_table_t start_select(int fd, fd_set *errorfds); + #ifdef __cplusplus } #endif diff --git a/components/vfs/private_include/esp_vfs_utils.h b/components/vfs/private_include/esp_vfs_utils.h new file mode 100644 index 0000000000..58576d6fb5 --- /dev/null +++ b/components/vfs/private_include/esp_vfs_utils.h @@ -0,0 +1,90 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +/* + * Using huge multi-line macros is never nice, but in this case + * the only alternative is to repeat this chunk of code (with different function names) + * for each syscall being implemented. Given that this define is contained within a single + * file, this looks like a good tradeoff. + * + * First we check if syscall is implemented by VFS (corresponding member is not NULL), + * then call the right flavor of the method (e.g. open or open_p) depending on + * ESP_VFS_FLAG_CONTEXT_PTR flag. If ESP_VFS_FLAG_CONTEXT_PTR is set, context is passed + * in as first argument and _p variant is used for the call. + * It is enough to check just one of them for NULL, as both variants are part of a union. + */ +#define CHECK_AND_CALL(ret, r, pvfs, func, ...) \ + if (pvfs->vfs->func == NULL) { \ + __errno_r(r) = ENOSYS; \ + return -1; \ + } \ + if (pvfs->flags & ESP_VFS_FLAG_CONTEXT_PTR) { \ + ret = (*pvfs->vfs->func ## _p)(pvfs->ctx, __VA_ARGS__); \ + } else { \ + ret = (*pvfs->vfs->func)(__VA_ARGS__);\ + } + +#define CHECK_AND_CALL_SUBCOMPONENT(ret, r, pvfs, component, func, ...) \ + if (pvfs->vfs->component == NULL || pvfs->vfs->component->func == NULL) { \ + __errno_r(r) = ENOSYS; \ + return -1; \ + } \ + if (pvfs->flags & ESP_VFS_FLAG_CONTEXT_PTR) { \ + ret = (*pvfs->vfs->component->func ## _p)(pvfs->ctx, __VA_ARGS__); \ + } else { \ + ret = (*pvfs->vfs->component->func)(__VA_ARGS__);\ + } + +#define CHECK_AND_CALLV(r, pvfs, func, ...) \ + if (pvfs->vfs->func == NULL) { \ + __errno_r(r) = ENOSYS; \ + return; \ + } \ + if (pvfs->flags & ESP_VFS_FLAG_CONTEXT_PTR) { \ + (*pvfs->vfs->func ## _p)(pvfs->ctx, __VA_ARGS__); \ + } else { \ + (*pvfs->vfs->func)(__VA_ARGS__);\ + } + +#define CHECK_AND_CALL_SUBCOMPONENTV(r, pvfs, component, func, ...) \ + if (pvfs->vfs->component == NULL || pvfs->vfs->component->func == NULL) { \ + __errno_r(r) = ENOSYS; \ + return; \ + } \ + if (pvfs->flags & ESP_VFS_FLAG_CONTEXT_PTR) { \ + (*pvfs->vfs->component->func ## _p)(pvfs->ctx, __VA_ARGS__); \ + } else { \ + (*pvfs->vfs->component->func)(__VA_ARGS__);\ + } + +#define CHECK_AND_CALLP(ret, r, pvfs, func, ...) \ + if (pvfs->vfs->func == NULL) { \ + __errno_r(r) = ENOSYS; \ + return NULL; \ + } \ + if (pvfs->flags & ESP_VFS_FLAG_CONTEXT_PTR) { \ + ret = (*pvfs->vfs->func ## _p)(pvfs->ctx, __VA_ARGS__); \ + } else { \ + ret = (*pvfs->vfs->func)(__VA_ARGS__);\ + } + +#define CHECK_AND_CALL_SUBCOMPONENTP(ret, r, pvfs, component, func, ...) \ + if (pvfs->vfs->component == NULL || pvfs->vfs->component->func == NULL) { \ + __errno_r(r) = ENOSYS; \ + return NULL; \ + } \ + if (pvfs->flags & ESP_VFS_FLAG_CONTEXT_PTR) { \ + ret = (*pvfs->vfs->component->func ## _p)(pvfs->ctx, __VA_ARGS__); \ + } else { \ + ret = (*pvfs->vfs->component->func)(__VA_ARGS__);\ + } + +#define CHECK_VFS_READONLY_FLAG(flags) \ + if (flags & ESP_VFS_FLAG_READONLY_FS) { \ + __errno_r(r) = EROFS; \ + return -1; \ + } diff --git a/components/vfs/vfs.c b/components/vfs/vfs.c index 332bfeabc7..71b2ef4bf2 100644 --- a/components/vfs/vfs.c +++ b/components/vfs/vfs.c @@ -41,33 +41,11 @@ static const char *TAG = "vfs"; #define LEN_PATH_PREFIX_IGNORED SIZE_MAX /* special length value for VFS which is never recognised by open() */ #define FD_TABLE_ENTRY_UNUSED (fd_table_t) { .permanent = false, .has_pending_close = false, .has_pending_select = false, .vfs_index = -1, .local_fd = -1 } -#ifdef CONFIG_IDF_TARGET_LINUX -typedef uint16_t local_fd_t; -#else -typedef uint8_t local_fd_t; -#endif _Static_assert((1 << (sizeof(local_fd_t)*8)) >= MAX_FDS, "file descriptor type too small"); -typedef int8_t vfs_index_t; _Static_assert((1 << (sizeof(vfs_index_t)*8)) >= VFS_MAX_COUNT, "VFS index type too small"); _Static_assert(((vfs_index_t) -1) < 0, "vfs_index_t must be a signed type"); -typedef struct { - bool permanent :1; - bool has_pending_close :1; - bool has_pending_select :1; - uint8_t _reserved :5; - vfs_index_t vfs_index; - local_fd_t local_fd; -} fd_table_t; - -typedef struct { - bool isset; // none or at least one bit is set in the following 3 fd sets - fd_set readfds; - fd_set writefds; - fd_set errorfds; -} fds_triple_t; - static vfs_entry_t* s_vfs[VFS_MAX_COUNT] = { 0 }; static size_t s_vfs_count = 0; @@ -772,12 +750,52 @@ const vfs_entry_t *get_vfs_for_index(int index) } } +int register_fd(int vfs_index, int local_fd, bool permanent) +{ + _lock_acquire(&s_fd_table_lock); + for (int i = 0; i < MAX_FDS; ++i) { + if (s_fd_table[i].vfs_index == -1) { + s_fd_table[i].permanent = permanent; + s_fd_table[i].vfs_index = vfs_index; + s_fd_table[i].local_fd = local_fd; + _lock_release(&s_fd_table_lock); + return i; + } + } + _lock_release(&s_fd_table_lock); + return -1; +} + +void unregister_fd(int fd) +{ + _lock_acquire(&s_fd_table_lock); + + // Do not close permanent FDs + if (s_fd_table[fd].permanent) { + _lock_release(&s_fd_table_lock); + return; + } + + // If the FD is in use by select, mark it for closure + if (s_fd_table[fd].has_pending_select) { + s_fd_table[fd].has_pending_close = true; + } else { + s_fd_table[fd] = FD_TABLE_ENTRY_UNUSED; + } + _lock_release(&s_fd_table_lock); +} + static inline bool fd_valid(int fd) { return (fd < MAX_FDS) && (fd >= 0); } -static const vfs_entry_t *get_vfs_for_fd(int fd) +const fd_table_t *get_fd_entry(int fd) +{ + return fd_valid(fd) ? &s_fd_table[fd] : NULL; +} + +const vfs_entry_t *get_vfs_for_fd(int fd) { const vfs_entry_t *vfs = NULL; if (fd_valid(fd)) { @@ -787,7 +805,7 @@ static const vfs_entry_t *get_vfs_for_fd(int fd) return vfs; } -static inline int get_local_fd(const vfs_entry_t *vfs, int fd) +int get_local_fd(const vfs_entry_t *vfs, int fd) { int local_fd = -1; @@ -798,7 +816,7 @@ static inline int get_local_fd(const vfs_entry_t *vfs, int fd) return local_fd; } -static const char* translate_path(const vfs_entry_t* vfs, const char* src_path) +const char* translate_path(const vfs_entry_t* vfs, const char* src_path) { assert(strncmp(src_path, vfs->path_prefix, vfs->path_prefix_len) == 0); if (strlen(src_path) == vfs->path_prefix_len) { @@ -848,825 +866,13 @@ const vfs_entry_t* get_vfs_for_path(const char* path) return best_match; } -/* - * Using huge multi-line macros is never nice, but in this case - * the only alternative is to repeat this chunk of code (with different function names) - * for each syscall being implemented. Given that this define is contained within a single - * file, this looks like a good tradeoff. - * - * First we check if syscall is implemented by VFS (corresponding member is not NULL), - * then call the right flavor of the method (e.g. open or open_p) depending on - * ESP_VFS_FLAG_CONTEXT_PTR flag. If ESP_VFS_FLAG_CONTEXT_PTR is set, context is passed - * in as first argument and _p variant is used for the call. - * It is enough to check just one of them for NULL, as both variants are part of a union. - */ -#define CHECK_AND_CALL(ret, r, pvfs, func, ...) \ - if (pvfs->vfs->func == NULL) { \ - __errno_r(r) = ENOSYS; \ - return -1; \ - } \ - if (pvfs->flags & ESP_VFS_FLAG_CONTEXT_PTR) { \ - ret = (*pvfs->vfs->func ## _p)(pvfs->ctx, __VA_ARGS__); \ - } else { \ - ret = (*pvfs->vfs->func)(__VA_ARGS__);\ - } - -#define CHECK_AND_CALL_SUBCOMPONENT(ret, r, pvfs, component, func, ...) \ - if (pvfs->vfs->component == NULL || pvfs->vfs->component->func == NULL) { \ - __errno_r(r) = ENOSYS; \ - return -1; \ - } \ - if (pvfs->flags & ESP_VFS_FLAG_CONTEXT_PTR) { \ - ret = (*pvfs->vfs->component->func ## _p)(pvfs->ctx, __VA_ARGS__); \ - } else { \ - ret = (*pvfs->vfs->component->func)(__VA_ARGS__);\ - } - -#define CHECK_AND_CALLV(r, pvfs, func, ...) \ - if (pvfs->vfs->func == NULL) { \ - __errno_r(r) = ENOSYS; \ - return; \ - } \ - if (pvfs->flags & ESP_VFS_FLAG_CONTEXT_PTR) { \ - (*pvfs->vfs->func ## _p)(pvfs->ctx, __VA_ARGS__); \ - } else { \ - (*pvfs->vfs->func)(__VA_ARGS__);\ - } - -#define CHECK_AND_CALL_SUBCOMPONENTV(r, pvfs, component, func, ...) \ - if (pvfs->vfs->component == NULL || pvfs->vfs->component->func == NULL) { \ - __errno_r(r) = ENOSYS; \ - return; \ - } \ - if (pvfs->flags & ESP_VFS_FLAG_CONTEXT_PTR) { \ - (*pvfs->vfs->component->func ## _p)(pvfs->ctx, __VA_ARGS__); \ - } else { \ - (*pvfs->vfs->component->func)(__VA_ARGS__);\ - } - -#define CHECK_AND_CALLP(ret, r, pvfs, func, ...) \ - if (pvfs->vfs->func == NULL) { \ - __errno_r(r) = ENOSYS; \ - return NULL; \ - } \ - if (pvfs->flags & ESP_VFS_FLAG_CONTEXT_PTR) { \ - ret = (*pvfs->vfs->func ## _p)(pvfs->ctx, __VA_ARGS__); \ - } else { \ - ret = (*pvfs->vfs->func)(__VA_ARGS__);\ - } - -#define CHECK_AND_CALL_SUBCOMPONENTP(ret, r, pvfs, component, func, ...) \ - if (pvfs->vfs->component == NULL || pvfs->vfs->component->func == NULL) { \ - __errno_r(r) = ENOSYS; \ - return NULL; \ - } \ - if (pvfs->flags & ESP_VFS_FLAG_CONTEXT_PTR) { \ - ret = (*pvfs->vfs->component->func ## _p)(pvfs->ctx, __VA_ARGS__); \ - } else { \ - ret = (*pvfs->vfs->component->func)(__VA_ARGS__);\ - } - -#define CHECK_VFS_READONLY_FLAG(flags) \ - if (flags & ESP_VFS_FLAG_READONLY_FS) { \ - __errno_r(r) = EROFS; \ - return -1; \ - } - -int esp_vfs_open(struct _reent *r, const char * path, int flags, int mode) +size_t get_vfs_count(void) { - const vfs_entry_t *vfs = get_vfs_for_path(path); - if (vfs == NULL) { - __errno_r(r) = ENOENT; - return -1; - } - - int acc_mode = flags & O_ACCMODE; - int ro_filesystem = vfs->flags & ESP_VFS_FLAG_READONLY_FS; - if (acc_mode != O_RDONLY && ro_filesystem) { - __errno_r(r) = EROFS; - return -1; - } - - const char *path_within_vfs = translate_path(vfs, path); - int fd_within_vfs; - CHECK_AND_CALL(fd_within_vfs, r, vfs, open, path_within_vfs, flags, mode); - if (fd_within_vfs >= 0) { - _lock_acquire(&s_fd_table_lock); - for (int i = 0; i < MAX_FDS; ++i) { - if (s_fd_table[i].vfs_index == -1) { - s_fd_table[i].permanent = false; - s_fd_table[i].vfs_index = vfs->offset; - s_fd_table[i].local_fd = fd_within_vfs; - _lock_release(&s_fd_table_lock); - return i; - } - } - _lock_release(&s_fd_table_lock); - int ret; - CHECK_AND_CALL(ret, r, vfs, close, fd_within_vfs); - (void) ret; // remove "set but not used" warning - __errno_r(r) = ENOMEM; - return -1; - } - __errno_r(r) = errno; - return -1; + return s_vfs_count; } -ssize_t esp_vfs_write(struct _reent *r, int fd, const void * data, size_t size) +void close_pending(int nfds) { - const vfs_entry_t* vfs = get_vfs_for_fd(fd); - const int local_fd = get_local_fd(vfs, fd); - if (vfs == NULL || local_fd < 0) { - __errno_r(r) = EBADF; - return -1; - } - ssize_t ret; - CHECK_AND_CALL(ret, r, vfs, write, local_fd, data, size); - return ret; -} - -off_t esp_vfs_lseek(struct _reent *r, int fd, off_t size, int mode) -{ - const vfs_entry_t* vfs = get_vfs_for_fd(fd); - const int local_fd = get_local_fd(vfs, fd); - if (vfs == NULL || local_fd < 0) { - __errno_r(r) = EBADF; - return -1; - } - off_t ret; - CHECK_AND_CALL(ret, r, vfs, lseek, local_fd, size, mode); - return ret; -} - -ssize_t esp_vfs_read(struct _reent *r, int fd, void * dst, size_t size) -{ - const vfs_entry_t* vfs = get_vfs_for_fd(fd); - const int local_fd = get_local_fd(vfs, fd); - if (vfs == NULL || local_fd < 0) { - __errno_r(r) = EBADF; - return -1; - } - ssize_t ret; - CHECK_AND_CALL(ret, r, vfs, read, local_fd, dst, size); - return ret; -} - -ssize_t esp_vfs_pread(int fd, void *dst, size_t size, off_t offset) -{ - [[maybe_unused]] struct _reent *r = __getreent(); - const vfs_entry_t* vfs = get_vfs_for_fd(fd); - const int local_fd = get_local_fd(vfs, fd); - if (vfs == NULL || local_fd < 0) { - __errno_r(r) = EBADF; - return -1; - } - ssize_t ret; - CHECK_AND_CALL(ret, r, vfs, pread, local_fd, dst, size, offset); - return ret; -} - -ssize_t esp_vfs_pwrite(int fd, const void *src, size_t size, off_t offset) -{ - [[maybe_unused]] struct _reent *r = __getreent(); - const vfs_entry_t* vfs = get_vfs_for_fd(fd); - const int local_fd = get_local_fd(vfs, fd); - if (vfs == NULL || local_fd < 0) { - __errno_r(r) = EBADF; - return -1; - } - ssize_t ret; - CHECK_AND_CALL(ret, r, vfs, pwrite, local_fd, src, size, offset); - return ret; -} - -int esp_vfs_close(struct _reent *r, int fd) -{ - const vfs_entry_t* vfs = get_vfs_for_fd(fd); - const int local_fd = get_local_fd(vfs, fd); - if (vfs == NULL || local_fd < 0) { - __errno_r(r) = EBADF; - return -1; - } - int ret; - CHECK_AND_CALL(ret, r, vfs, close, local_fd); - - _lock_acquire(&s_fd_table_lock); - if (!s_fd_table[fd].permanent) { - if (s_fd_table[fd].has_pending_select) { - s_fd_table[fd].has_pending_close = true; - } else { - s_fd_table[fd] = FD_TABLE_ENTRY_UNUSED; - } - } - _lock_release(&s_fd_table_lock); - return ret; -} - -int esp_vfs_fstat(struct _reent *r, int fd, struct stat * st) -{ - const vfs_entry_t* vfs = get_vfs_for_fd(fd); - const int local_fd = get_local_fd(vfs, fd); - if (vfs == NULL || local_fd < 0) { - __errno_r(r) = EBADF; - return -1; - } - int ret; - CHECK_AND_CALL(ret, r, vfs, fstat, local_fd, st); - return ret; -} - -int esp_vfs_fcntl_r(struct _reent *r, int fd, int cmd, int arg) -{ - const vfs_entry_t* vfs = get_vfs_for_fd(fd); - const int local_fd = get_local_fd(vfs, fd); - if (vfs == NULL || local_fd < 0) { - __errno_r(r) = EBADF; - return -1; - } - int ret; - CHECK_AND_CALL(ret, r, vfs, fcntl, local_fd, cmd, arg); - return ret; -} - -int esp_vfs_ioctl(int fd, int cmd, ...) -{ - const vfs_entry_t* vfs = get_vfs_for_fd(fd); - const int local_fd = get_local_fd(vfs, fd); - [[maybe_unused]] struct _reent* r = __getreent(); - if (vfs == NULL || local_fd < 0) { - __errno_r(r) = EBADF; - return -1; - } - - va_list args; - va_start(args, cmd); - if (vfs->vfs->ioctl == NULL) { - __errno_r(r) = ENOSYS; - va_end(args); - return -1; - } - - int ret; - if (vfs->flags & ESP_VFS_FLAG_CONTEXT_PTR) { - ret = (*vfs->vfs->ioctl_p)(vfs->ctx, local_fd, cmd, args); - } else { - ret = (*vfs->vfs->ioctl)(local_fd, cmd, args); - } - - va_end(args); - return ret; -} - -int esp_vfs_fsync(int fd) -{ - const vfs_entry_t* vfs = get_vfs_for_fd(fd); - const int local_fd = get_local_fd(vfs, fd); - [[maybe_unused]] struct _reent* r = __getreent(); - if (vfs == NULL || local_fd < 0) { - __errno_r(r) = EBADF; - return -1; - } - int ret; - CHECK_AND_CALL(ret, r, vfs, fsync, local_fd); - return ret; -} - -#ifdef CONFIG_VFS_SUPPORT_DIR - -int esp_vfs_stat(struct _reent *r, const char * path, struct stat * st) -{ - const vfs_entry_t* vfs = get_vfs_for_path(path); - if (vfs == NULL) { - __errno_r(r) = ENOENT; - return -1; - } - const char* path_within_vfs = translate_path(vfs, path); - int ret; - CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, stat, path_within_vfs, st); - return ret; -} - -int esp_vfs_utime(const char *path, const struct utimbuf *times) -{ - int ret; - const vfs_entry_t* vfs = get_vfs_for_path(path); - [[maybe_unused]] struct _reent* r = __getreent(); - if (vfs == NULL) { - __errno_r(r) = ENOENT; - return -1; - } - const char* path_within_vfs = translate_path(vfs, path); - CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, utime, path_within_vfs, times); - return ret; -} - -int esp_vfs_link(struct _reent *r, const char* n1, const char* n2) -{ - const vfs_entry_t* vfs = get_vfs_for_path(n1); - if (vfs == NULL) { - __errno_r(r) = ENOENT; - return -1; - } - const vfs_entry_t* vfs2 = get_vfs_for_path(n2); - if (vfs != vfs2) { - __errno_r(r) = EXDEV; - return -1; - } - - CHECK_VFS_READONLY_FLAG(vfs2->flags); - - const char* path1_within_vfs = translate_path(vfs, n1); - const char* path2_within_vfs = translate_path(vfs, n2); - int ret; - CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, link, path1_within_vfs, path2_within_vfs); - return ret; -} - -int esp_vfs_unlink(struct _reent *r, const char *path) -{ - const vfs_entry_t* vfs = get_vfs_for_path(path); - if (vfs == NULL) { - __errno_r(r) = ENOENT; - return -1; - } - - CHECK_VFS_READONLY_FLAG(vfs->flags); - - const char* path_within_vfs = translate_path(vfs, path); - int ret; - CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, unlink, path_within_vfs); - return ret; -} - -int esp_vfs_rename(struct _reent *r, const char *src, const char *dst) -{ - const vfs_entry_t* vfs = get_vfs_for_path(src); - if (vfs == NULL) { - __errno_r(r) = ENOENT; - return -1; - } - - CHECK_VFS_READONLY_FLAG(vfs->flags); - - const vfs_entry_t* vfs_dst = get_vfs_for_path(dst); - if (vfs != vfs_dst) { - __errno_r(r) = EXDEV; - return -1; - } - - CHECK_VFS_READONLY_FLAG(vfs_dst->flags); - - const char* src_within_vfs = translate_path(vfs, src); - const char* dst_within_vfs = translate_path(vfs, dst); - int ret; - CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, rename, src_within_vfs, dst_within_vfs); - return ret; -} - -DIR* esp_vfs_opendir(const char* name) -{ - const vfs_entry_t* vfs = get_vfs_for_path(name); - [[maybe_unused]] struct _reent* r = __getreent(); - if (vfs == NULL) { - __errno_r(r) = ENOENT; - return NULL; - } - const char* path_within_vfs = translate_path(vfs, name); - DIR* ret; - CHECK_AND_CALL_SUBCOMPONENTP(ret, r, vfs, dir, opendir, path_within_vfs); - if (ret != NULL) { - ret->dd_vfs_idx = vfs->offset; - } - return ret; -} - -struct dirent* esp_vfs_readdir(DIR* pdir) -{ - const vfs_entry_t* vfs = get_vfs_for_index(pdir->dd_vfs_idx); - [[maybe_unused]] struct _reent* r = __getreent(); - if (vfs == NULL) { - __errno_r(r) = EBADF; - return NULL; - } - struct dirent* ret; - CHECK_AND_CALL_SUBCOMPONENTP(ret, r, vfs, dir, readdir, pdir); - return ret; -} - -int esp_vfs_readdir_r(DIR* pdir, struct dirent* entry, struct dirent** out_dirent) -{ - const vfs_entry_t* vfs = get_vfs_for_index(pdir->dd_vfs_idx); - [[maybe_unused]] struct _reent* r = __getreent(); - if (vfs == NULL) { - errno = EBADF; - return -1; - } - int ret; - CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, readdir_r, pdir, entry, out_dirent); - return ret; -} - -long esp_vfs_telldir(DIR* pdir) -{ - const vfs_entry_t* vfs = get_vfs_for_index(pdir->dd_vfs_idx); - [[maybe_unused]] struct _reent* r = __getreent(); - if (vfs == NULL) { - errno = EBADF; - return -1; - } - long ret; - CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, telldir, pdir); - return ret; -} - -void esp_vfs_seekdir(DIR* pdir, long loc) -{ - const vfs_entry_t* vfs = get_vfs_for_index(pdir->dd_vfs_idx); - [[maybe_unused]] struct _reent* r = __getreent(); - if (vfs == NULL) { - errno = EBADF; - return; - } - CHECK_AND_CALL_SUBCOMPONENTV(r, vfs, dir, seekdir, pdir, loc); -} - -void esp_vfs_rewinddir(DIR* pdir) -{ - seekdir(pdir, 0); -} - -int esp_vfs_closedir(DIR* pdir) -{ - const vfs_entry_t* vfs = get_vfs_for_index(pdir->dd_vfs_idx); - [[maybe_unused]] struct _reent* r = __getreent(); - if (vfs == NULL) { - errno = EBADF; - return -1; - } - int ret; - CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, closedir, pdir); - return ret; -} - -int esp_vfs_mkdir(const char* name, mode_t mode) -{ - const vfs_entry_t* vfs = get_vfs_for_path(name); - [[maybe_unused]] struct _reent* r = __getreent(); - if (vfs == NULL) { - __errno_r(r) = ENOENT; - return -1; - } - - CHECK_VFS_READONLY_FLAG(vfs->flags); - - const char* path_within_vfs = translate_path(vfs, name); - int ret; - CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, mkdir, path_within_vfs, mode); - return ret; -} - -int esp_vfs_rmdir(const char* name) -{ - const vfs_entry_t* vfs = get_vfs_for_path(name); - [[maybe_unused]] struct _reent* r = __getreent(); - if (vfs == NULL) { - __errno_r(r) = ENOENT; - return -1; - } - - CHECK_VFS_READONLY_FLAG(vfs->flags); - - const char* path_within_vfs = translate_path(vfs, name); - int ret; - CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, rmdir, path_within_vfs); - return ret; -} - -int esp_vfs_access(const char *path, int amode) -{ - int ret; - const vfs_entry_t* vfs = get_vfs_for_path(path); - [[maybe_unused]] struct _reent* r = __getreent(); - if (vfs == NULL) { - __errno_r(r) = ENOENT; - return -1; - } - const char* path_within_vfs = translate_path(vfs, path); - CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, access, path_within_vfs, amode); - return ret; -} - -int esp_vfs_truncate(const char *path, off_t length) -{ - int ret; - const vfs_entry_t* vfs = get_vfs_for_path(path); - [[maybe_unused]] struct _reent* r = __getreent(); - if (vfs == NULL) { - __errno_r(r) = ENOENT; - return -1; - } - - CHECK_VFS_READONLY_FLAG(vfs->flags); - - const char* path_within_vfs = translate_path(vfs, path); - CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, truncate, path_within_vfs, length); - return ret; -} - -int esp_vfs_ftruncate(int fd, off_t length) -{ - const vfs_entry_t* vfs = get_vfs_for_fd(fd); - int local_fd = get_local_fd(vfs, fd); - [[maybe_unused]] struct _reent* r = __getreent(); - if (vfs == NULL || local_fd < 0) { - __errno_r(r) = EBADF; - return -1; - } - - CHECK_VFS_READONLY_FLAG(vfs->flags); - - int ret; - CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, ftruncate, local_fd, length); - return ret; -} - -#endif // CONFIG_VFS_SUPPORT_DIR - -#ifdef CONFIG_VFS_SUPPORT_SELECT - -static void call_end_selects(int end_index, const fds_triple_t *vfs_fds_triple, void **driver_args) -{ - for (int i = 0; i < end_index; ++i) { - const vfs_entry_t *vfs = get_vfs_for_index(i); - const fds_triple_t *item = &vfs_fds_triple[i]; - if (vfs != NULL - && vfs->vfs->select != NULL - && vfs->vfs->select->end_select != NULL - && item->isset - ) { - esp_err_t err = vfs->vfs->select->end_select(driver_args[i]); - if (err != ESP_OK) { - ESP_LOGD(TAG, "end_select failed: %s", esp_err_to_name(err)); - } - } - } -} - -static inline bool esp_vfs_safe_fd_isset(int fd, const fd_set *fds) -{ - return fds && FD_ISSET(fd, fds); -} - -static int set_global_fd_sets(const fds_triple_t *vfs_fds_triple, int size, fd_set *readfds, fd_set *writefds, fd_set *errorfds) -{ - int ret = 0; - - for (int i = 0; i < size; ++i) { - const fds_triple_t *item = &vfs_fds_triple[i]; - if (item->isset) { - for (int fd = 0; fd < MAX_FDS; ++fd) { - if (s_fd_table[fd].vfs_index == i) { - const int local_fd = s_fd_table[fd].local_fd; // single read -> no locking is required - if (readfds && esp_vfs_safe_fd_isset(local_fd, &item->readfds)) { - ESP_LOGD(TAG, "FD %d in readfds was set from VFS ID %d", fd, i); - FD_SET(fd, readfds); - ++ret; - } - if (writefds && esp_vfs_safe_fd_isset(local_fd, &item->writefds)) { - ESP_LOGD(TAG, "FD %d in writefds was set from VFS ID %d", fd, i); - FD_SET(fd, writefds); - ++ret; - } - if (errorfds && esp_vfs_safe_fd_isset(local_fd, &item->errorfds)) { - ESP_LOGD(TAG, "FD %d in errorfds was set from VFS ID %d", fd, i); - FD_SET(fd, errorfds); - ++ret; - } - } - } - } - } - - return ret; -} - -static void esp_vfs_log_fd_set(const char *fds_name, const fd_set *fds) -{ - if (fds_name && fds) { - ESP_LOGD(TAG, "FDs in %s =", fds_name); - for (int i = 0; i < MAX_FDS; ++i) { - if (esp_vfs_safe_fd_isset(i, fds)) { - ESP_LOGD(TAG, "%d", i); - } - } - } -} - -int esp_vfs_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout) -{ - // NOTE: Please see the "Synchronous input/output multiplexing" section of the ESP-IDF Programming Guide - // (API Reference -> Storage -> Virtual Filesystem) for a general overview of the implementation of VFS select(). - int ret = 0; - [[maybe_unused]] struct _reent* r = __getreent(); - - ESP_LOGD(TAG, "esp_vfs_select starts with nfds = %d", nfds); - if (timeout) { - ESP_LOGD(TAG, "timeout is %lds + %ldus", (long)timeout->tv_sec, timeout->tv_usec); - } - esp_vfs_log_fd_set("readfds", readfds); - esp_vfs_log_fd_set("writefds", writefds); - esp_vfs_log_fd_set("errorfds", errorfds); - - if (nfds > MAX_FDS || nfds < 0) { - ESP_LOGD(TAG, "incorrect nfds"); - __errno_r(r) = EINVAL; - return -1; - } - - // Capture s_vfs_count to a local variable in case a new driver is registered or removed during this actual select() - // call. s_vfs_count cannot be protected with a mutex during a select() call (which can be one without a timeout) - // because that could block the registration of new driver. - const size_t vfs_count = s_vfs_count; - fds_triple_t *vfs_fds_triple; - if ((vfs_fds_triple = heap_caps_calloc(vfs_count, sizeof(fds_triple_t), VFS_MALLOC_FLAGS)) == NULL) { - __errno_r(r) = ENOMEM; - ESP_LOGD(TAG, "calloc is unsuccessful"); - return -1; - } - - esp_vfs_select_sem_t sel_sem = { - .is_sem_local = false, - .sem = NULL, - }; - - int (*socket_select)(int, fd_set *, fd_set *, fd_set *, struct timeval *) = NULL; - for (int fd = 0; fd < nfds; ++fd) { - _lock_acquire(&s_fd_table_lock); - const bool is_socket_fd = s_fd_table[fd].permanent; - const int vfs_index = s_fd_table[fd].vfs_index; - const int local_fd = s_fd_table[fd].local_fd; - if (esp_vfs_safe_fd_isset(fd, errorfds)) { - s_fd_table[fd].has_pending_select = true; - } - _lock_release(&s_fd_table_lock); - - if (vfs_index < 0) { - continue; - } - - if (is_socket_fd) { - if (!socket_select) { - // no socket_select found yet so take a look - if (esp_vfs_safe_fd_isset(fd, readfds) || - esp_vfs_safe_fd_isset(fd, writefds) || - esp_vfs_safe_fd_isset(fd, errorfds)) { - const vfs_entry_t *vfs = s_vfs[vfs_index]; - socket_select = vfs->vfs->select->socket_select; - sel_sem.sem = vfs->vfs->select->get_socket_select_semaphore(); - } - } - continue; - } - - fds_triple_t *item = &vfs_fds_triple[vfs_index]; // FD sets for VFS which belongs to fd - if (esp_vfs_safe_fd_isset(fd, readfds)) { - item->isset = true; - FD_SET(local_fd, &item->readfds); - FD_CLR(fd, readfds); - ESP_LOGD(TAG, "removing %d from readfds and adding as local FD %d to fd_set of VFS ID %d", fd, local_fd, vfs_index); - } - if (esp_vfs_safe_fd_isset(fd, writefds)) { - item->isset = true; - FD_SET(local_fd, &item->writefds); - FD_CLR(fd, writefds); - ESP_LOGD(TAG, "removing %d from writefds and adding as local FD %d to fd_set of VFS ID %d", fd, local_fd, vfs_index); - } - if (esp_vfs_safe_fd_isset(fd, errorfds)) { - item->isset = true; - FD_SET(local_fd, &item->errorfds); - FD_CLR(fd, errorfds); - ESP_LOGD(TAG, "removing %d from errorfds and adding as local FD %d to fd_set of VFS ID %d", fd, local_fd, vfs_index); - } - } - - // all non-socket VFSs have their FD sets in vfs_fds_triple - // the global readfds, writefds and errorfds contain only socket FDs (if - // there any) - - if (!socket_select) { - // There is no socket VFS registered or select() wasn't called for - // any socket. Therefore, we will use our own signalization. - sel_sem.is_sem_local = true; - if ((sel_sem.sem = xSemaphoreCreateBinary()) == NULL) { - free(vfs_fds_triple); - __errno_r(r) = ENOMEM; - ESP_LOGD(TAG, "cannot create select semaphore"); - return -1; - } - } - - void **driver_args = heap_caps_calloc(vfs_count, sizeof(void *), VFS_MALLOC_FLAGS); - - if (driver_args == NULL) { - free(vfs_fds_triple); - __errno_r(r) = ENOMEM; - ESP_LOGD(TAG, "calloc is unsuccessful for driver args"); - return -1; - } - - for (size_t i = 0; i < vfs_count; ++i) { - const vfs_entry_t *vfs = get_vfs_for_index(i); - fds_triple_t *item = &vfs_fds_triple[i]; - - if (vfs == NULL || vfs->vfs->select == NULL || vfs->vfs->select->start_select == NULL) { - ESP_LOGD(TAG, "start_select function callback for this vfs (s_vfs[%d]) is not defined", vfs->offset); - continue; - } - - if (!item->isset) { - continue; - } - - // call start_select for all non-socket VFSs with has at least one FD set in readfds, writefds, or errorfds - // note: it can point to socket VFS but item->isset will be false for that - ESP_LOGD(TAG, "calling start_select for VFS ID % " PRIuSIZE " with the following local FDs", i); - esp_vfs_log_fd_set("readfds", &item->readfds); - esp_vfs_log_fd_set("writefds", &item->writefds); - esp_vfs_log_fd_set("errorfds", &item->errorfds); - esp_err_t err = vfs->vfs->select->start_select(nfds, &item->readfds, &item->writefds, &item->errorfds, sel_sem, - driver_args + i); - - if (err != ESP_OK) { - if (err != ESP_ERR_NOT_SUPPORTED) { - call_end_selects(i, vfs_fds_triple, driver_args); - } - (void) set_global_fd_sets(vfs_fds_triple, vfs_count, readfds, writefds, errorfds); - if (sel_sem.is_sem_local && sel_sem.sem) { - vSemaphoreDelete(sel_sem.sem); - sel_sem.sem = NULL; - } - free(vfs_fds_triple); - free(driver_args); - __errno_r(r) = EINTR; - ESP_LOGD(TAG, "start_select failed: %s", esp_err_to_name(err)); - return -1; - } - } - - if (socket_select) { - ESP_LOGD(TAG, "calling socket_select with the following FDs"); - esp_vfs_log_fd_set("readfds", readfds); - esp_vfs_log_fd_set("writefds", writefds); - esp_vfs_log_fd_set("errorfds", errorfds); - ret = socket_select(nfds, readfds, writefds, errorfds, timeout); - ESP_LOGD(TAG, "socket_select returned %d and the FDs are the following", ret); - esp_vfs_log_fd_set("readfds", readfds); - esp_vfs_log_fd_set("writefds", writefds); - esp_vfs_log_fd_set("errorfds", errorfds); - } else { - if (readfds) { - FD_ZERO(readfds); - } - if (writefds) { - FD_ZERO(writefds); - } - if (errorfds) { - FD_ZERO(errorfds); - } - - TickType_t ticks_to_wait = portMAX_DELAY; - if (timeout) { - uint32_t timeout_ms = (timeout->tv_sec * 1000) + (timeout->tv_usec / 1000); - /* Round up the number of ticks. - * Not only we need to round up the number of ticks, but we also need to add 1. - * Indeed, `select` function shall wait for AT LEAST timeout, but on FreeRTOS, - * if we specify a timeout of 1 tick to `xSemaphoreTake`, it will take AT MOST - * 1 tick before triggering a timeout. Thus, we need to pass 2 ticks as a timeout - * to `xSemaphoreTake`. */ - ticks_to_wait = ((timeout_ms + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS) + 1; - ESP_LOGD(TAG, "timeout is %" PRIu32 "ms", timeout_ms); - } - ESP_LOGD(TAG, "waiting without calling socket_select"); - xSemaphoreTake(sel_sem.sem, ticks_to_wait); - } - - call_end_selects(vfs_count, vfs_fds_triple, driver_args); // for VFSs for start_select was called before - - if (ret >= 0) { - ret += set_global_fd_sets(vfs_fds_triple, vfs_count, readfds, writefds, errorfds); - } - if (sel_sem.sem) { // Cleanup the select semaphore - if (sel_sem.is_sem_local) { - vSemaphoreDelete(sel_sem.sem); - } else if (socket_select) { - SemaphoreHandle_t *s = sel_sem.sem; - /* Select might have been triggered from both lwip and vfs fds at the same time, and - * we have to make sure that the lwip semaphore is cleared when we exit select(). - * It is safe, as the semaphore belongs to the calling thread. */ - xSemaphoreTake(*s, 0); - } - sel_sem.sem = NULL; - } _lock_acquire(&s_fd_table_lock); for (int fd = 0; fd < nfds; ++fd) { if (s_fd_table[fd].has_pending_close) { @@ -1674,243 +880,21 @@ int esp_vfs_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds } } _lock_release(&s_fd_table_lock); - free(vfs_fds_triple); - free(driver_args); - - ESP_LOGD(TAG, "esp_vfs_select returns %d", ret); - esp_vfs_log_fd_set("readfds", readfds); - esp_vfs_log_fd_set("writefds", writefds); - esp_vfs_log_fd_set("errorfds", errorfds); - return ret; } -void esp_vfs_select_triggered(esp_vfs_select_sem_t sem) +fd_table_t start_select(int fd, fd_set *errorfds) { - if (sem.is_sem_local) { - xSemaphoreGive(sem.sem); - } else { - // Another way would be to go through s_fd_table and find the VFS - // which has a permanent FD. But in order to avoid to lock - // s_fd_table_lock we go through the VFS table. - for (int i = 0; i < s_vfs_count; ++i) { - // Note: s_vfs_count could have changed since the start of vfs_select() call. However, that change doesn't - // matter here stop_socket_select() will be called for only valid VFS drivers. - const vfs_entry_t *vfs = s_vfs[i]; - if (vfs != NULL - && vfs->vfs->select != NULL - && vfs->vfs->select->stop_socket_select != NULL - ) { - vfs->vfs->select->stop_socket_select(sem.sem); - break; - } - } + _lock_acquire(&s_fd_table_lock); + + // Set flag + if (esp_vfs_safe_fd_isset(fd, errorfds)) { + s_fd_table[fd].has_pending_select = true; } -} - -void esp_vfs_select_triggered_isr(esp_vfs_select_sem_t sem, BaseType_t *woken) -{ - if (sem.is_sem_local) { - xSemaphoreGiveFromISR(sem.sem, woken); - } else { - // Another way would be to go through s_fd_table and find the VFS - // which has a permanent FD. But in order to avoid to lock - // s_fd_table_lock we go through the VFS table. - for (int i = 0; i < s_vfs_count; ++i) { - // Note: s_vfs_count could have changed since the start of vfs_select() call. However, that change doesn't - // matter here stop_socket_select() will be called for only valid VFS drivers. - const vfs_entry_t *vfs = s_vfs[i]; - if (vfs != NULL - && vfs->vfs->select != NULL - && vfs->vfs->select->stop_socket_select_isr != NULL - ) { - // Note: If the UART ISR resides in IRAM, the function referenced by stop_socket_select_isr should also be placed in IRAM. - vfs->vfs->select->stop_socket_select_isr(sem.sem, woken); - break; - } - } - } -} - -#endif // CONFIG_VFS_SUPPORT_SELECT - -#ifdef CONFIG_VFS_SUPPORT_TERMIOS - -int tcgetattr(int fd, struct termios *p) -{ - const vfs_entry_t* vfs = get_vfs_for_fd(fd); - const int local_fd = get_local_fd(vfs, fd); - [[maybe_unused]] struct _reent* r = __getreent(); - if (vfs == NULL || local_fd < 0) { - __errno_r(r) = EBADF; - return -1; - } - int ret; - CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, termios, tcgetattr, local_fd, p); - return ret; -} - -int tcsetattr(int fd, int optional_actions, const struct termios *p) -{ - const vfs_entry_t* vfs = get_vfs_for_fd(fd); - const int local_fd = get_local_fd(vfs, fd); - [[maybe_unused]] struct _reent* r = __getreent(); - if (vfs == NULL || local_fd < 0) { - __errno_r(r) = EBADF; - return -1; - } - int ret; - CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, termios, tcsetattr, local_fd, optional_actions, p); - return ret; -} - -int tcdrain(int fd) -{ - const vfs_entry_t* vfs = get_vfs_for_fd(fd); - const int local_fd = get_local_fd(vfs, fd); - [[maybe_unused]] struct _reent* r = __getreent(); - if (vfs == NULL || local_fd < 0) { - __errno_r(r) = EBADF; - return -1; - } - int ret; - CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, termios, tcdrain, local_fd); - return ret; -} - -int tcflush(int fd, int select) -{ - const vfs_entry_t* vfs = get_vfs_for_fd(fd); - const int local_fd = get_local_fd(vfs, fd); - [[maybe_unused]] struct _reent* r = __getreent(); - if (vfs == NULL || local_fd < 0) { - __errno_r(r) = EBADF; - return -1; - } - int ret; - CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, termios, tcflush, local_fd, select); - return ret; -} - -int tcflow(int fd, int action) -{ - const vfs_entry_t* vfs = get_vfs_for_fd(fd); - const int local_fd = get_local_fd(vfs, fd); - [[maybe_unused]] struct _reent* r = __getreent(); - if (vfs == NULL || local_fd < 0) { - __errno_r(r) = EBADF; - return -1; - } - int ret; - CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, termios, tcflow, local_fd, action); - return ret; -} - -pid_t tcgetsid(int fd) -{ - const vfs_entry_t* vfs = get_vfs_for_fd(fd); - const int local_fd = get_local_fd(vfs, fd); - [[maybe_unused]] struct _reent* r = __getreent(); - if (vfs == NULL || local_fd < 0) { - __errno_r(r) = EBADF; - return -1; - } - int ret; - CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, termios, tcgetsid, local_fd); - return ret; -} - -int tcsendbreak(int fd, int duration) -{ - const vfs_entry_t* vfs = get_vfs_for_fd(fd); - const int local_fd = get_local_fd(vfs, fd); - [[maybe_unused]] struct _reent* r = __getreent(); - if (vfs == NULL || local_fd < 0) { - __errno_r(r) = EBADF; - return -1; - } - int ret; - CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, termios, tcsendbreak, local_fd, duration); - return ret; -} -#endif // CONFIG_VFS_SUPPORT_TERMIOS - - -#ifndef CONFIG_IDF_TARGET_LINUX -/* Create aliases for libc syscalls - - These functions are also available in ROM as stubs which use the syscall table, but linking them - directly here saves an additional function call when a software function is linked to one, and - makes linking with -stdlib easier. - */ -#ifdef CONFIG_VFS_SUPPORT_IO -int _open_r(struct _reent *r, const char * path, int flags, int mode) - __attribute__((alias("esp_vfs_open"))); -int _close_r(struct _reent *r, int fd) - __attribute__((alias("esp_vfs_close"))); -ssize_t _read_r(struct _reent *r, int fd, void * dst, size_t size) - __attribute__((alias("esp_vfs_read"))); -ssize_t _write_r(struct _reent *r, int fd, const void * data, size_t size) - __attribute__((alias("esp_vfs_write"))); -ssize_t pread(int fd, void *dst, size_t size, off_t offset) - __attribute__((alias("esp_vfs_pread"))); -ssize_t pwrite(int fd, const void *src, size_t size, off_t offset) - __attribute__((alias("esp_vfs_pwrite"))); -off_t _lseek_r(struct _reent *r, int fd, off_t size, int mode) - __attribute__((alias("esp_vfs_lseek"))); -int _fcntl_r(struct _reent *r, int fd, int cmd, int arg) - __attribute__((alias("esp_vfs_fcntl_r"))); -int _fstat_r(struct _reent *r, int fd, struct stat * st) - __attribute__((alias("esp_vfs_fstat"))); -int fsync(int fd) - __attribute__((alias("esp_vfs_fsync"))); -int ioctl(int fd, int cmd, ...) - __attribute__((alias("esp_vfs_ioctl"))); -#endif // CONFIG_VFS_SUPPORT_IO - -#ifdef CONFIG_VFS_SUPPORT_SELECT -int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout) - __attribute__((alias("esp_vfs_select"))); -#endif // CONFIG_VFS_SUPPORT_SELECT - -#ifdef CONFIG_VFS_SUPPORT_DIR -int _stat_r(struct _reent *r, const char * path, struct stat * st) - __attribute__((alias("esp_vfs_stat"))); -int _link_r(struct _reent *r, const char* n1, const char* n2) - __attribute__((alias("esp_vfs_link"))); -int _unlink_r(struct _reent *r, const char *path) - __attribute__((alias("esp_vfs_unlink"))); -int _rename_r(struct _reent *r, const char *src, const char *dst) - __attribute__((alias("esp_vfs_rename"))); -int truncate(const char *path, off_t length) - __attribute__((alias("esp_vfs_truncate"))); -int ftruncate(int fd, off_t length) - __attribute__((alias("esp_vfs_ftruncate"))); -int access(const char *path, int amode) - __attribute__((alias("esp_vfs_access"))); -int utime(const char *path, const struct utimbuf *times) - __attribute__((alias("esp_vfs_utime"))); -int rmdir(const char* name) - __attribute__((alias("esp_vfs_rmdir"))); -int mkdir(const char* name, mode_t mode) - __attribute__((alias("esp_vfs_mkdir"))); -DIR* opendir(const char* name) - __attribute__((alias("esp_vfs_opendir"))); -int closedir(DIR* pdir) - __attribute__((alias("esp_vfs_closedir"))); -int readdir_r(DIR* pdir, struct dirent* entry, struct dirent** out_dirent) - __attribute__((alias("esp_vfs_readdir_r"))); -struct dirent* readdir(DIR* pdir) - __attribute__((alias("esp_vfs_readdir"))); -long telldir(DIR* pdir) - __attribute__((alias("esp_vfs_telldir"))); -void seekdir(DIR* pdir, long loc) - __attribute__((alias("esp_vfs_seekdir"))); -void rewinddir(DIR* pdir) - __attribute__((alias("esp_vfs_rewinddir"))); -#endif // CONFIG_VFS_SUPPORT_DIR -#endif //CONFIG_IDF_TARGET_LINUX - -void vfs_include_syscalls_impl(void) -{ - // Linker hook function, exists to make the linker examine this file + + // Make a copy + fd_table_t out = s_fd_table[fd]; + + _lock_release(&s_fd_table_lock); + + return out; } diff --git a/components/vfs/vfs_calls.c b/components/vfs/vfs_calls.c new file mode 100644 index 0000000000..2c50e1eb50 --- /dev/null +++ b/components/vfs/vfs_calls.c @@ -0,0 +1,975 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "inttypes_ext.h" +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" +#include "esp_log.h" +#include "esp_vfs.h" +#include "esp_vfs_private.h" +#include "esp_vfs_utils.h" +#include "sdkconfig.h" + +static const char __attribute__((unused)) *TAG = "vfs_calls"; + +int esp_vfs_open(struct _reent *r, const char *path, int flags, int mode) +{ + const vfs_entry_t *vfs = get_vfs_for_path(path); + if (vfs == NULL) { + __errno_r(r) = ENOENT; + return -1; + } + + int acc_mode = flags & O_ACCMODE; + int ro_filesystem = vfs->flags & ESP_VFS_FLAG_READONLY_FS; + if (acc_mode != O_RDONLY && ro_filesystem) { + __errno_r(r) = EROFS; + return -1; + } + + const char *path_within_vfs = translate_path(vfs, path); + int fd_within_vfs; + CHECK_AND_CALL(fd_within_vfs, r, vfs, open, path_within_vfs, flags, mode); + + if (fd_within_vfs < 0) { + __errno_r(r) = errno; + return -1; + } + + int fd = register_fd(vfs->offset, fd_within_vfs, false); + if (fd >= 0) { + return fd; + } + + int ret; + CHECK_AND_CALL(ret, r, vfs, close, fd_within_vfs); + (void) ret; // remove "set but not used" warning + __errno_r(r) = ENFILE; + return -1; +} + +ssize_t esp_vfs_write(struct _reent *r, int fd, const void *data, size_t size) +{ + const vfs_entry_t *vfs = get_vfs_for_fd(fd); + const int local_fd = get_local_fd(vfs, fd); + if (vfs == NULL || local_fd < 0) { + __errno_r(r) = EBADF; + return -1; + } + ssize_t ret; + CHECK_AND_CALL(ret, r, vfs, write, local_fd, data, size); + return ret; +} + +off_t esp_vfs_lseek(struct _reent *r, int fd, off_t size, int mode) +{ + const vfs_entry_t *vfs = get_vfs_for_fd(fd); + const int local_fd = get_local_fd(vfs, fd); + if (vfs == NULL || local_fd < 0) { + __errno_r(r) = EBADF; + return -1; + } + off_t ret; + CHECK_AND_CALL(ret, r, vfs, lseek, local_fd, size, mode); + return ret; +} + +ssize_t esp_vfs_read(struct _reent *r, int fd, void *dst, size_t size) +{ + const vfs_entry_t *vfs = get_vfs_for_fd(fd); + const int local_fd = get_local_fd(vfs, fd); + if (vfs == NULL || local_fd < 0) { + __errno_r(r) = EBADF; + return -1; + } + ssize_t ret; + CHECK_AND_CALL(ret, r, vfs, read, local_fd, dst, size); + return ret; +} + +ssize_t esp_vfs_pread(int fd, void *dst, size_t size, off_t offset) +{ + struct _reent __attribute__((unused)) *r = __getreent(); + const vfs_entry_t *vfs = get_vfs_for_fd(fd); + const int local_fd = get_local_fd(vfs, fd); + if (vfs == NULL || local_fd < 0) { + __errno_r(r) = EBADF; + return -1; + } + ssize_t ret; + CHECK_AND_CALL(ret, r, vfs, pread, local_fd, dst, size, offset); + return ret; +} + +ssize_t esp_vfs_pwrite(int fd, const void *src, size_t size, off_t offset) +{ + struct _reent __attribute__((unused)) *r = __getreent(); + const vfs_entry_t *vfs = get_vfs_for_fd(fd); + const int local_fd = get_local_fd(vfs, fd); + if (vfs == NULL || local_fd < 0) { + __errno_r(r) = EBADF; + return -1; + } + ssize_t ret; + CHECK_AND_CALL(ret, r, vfs, pwrite, local_fd, src, size, offset); + return ret; +} + +int esp_vfs_close(struct _reent *r, int fd) +{ + const vfs_entry_t *vfs = get_vfs_for_fd(fd); + const int local_fd = get_local_fd(vfs, fd); + if (vfs == NULL || local_fd < 0) { + __errno_r(r) = EBADF; + return -1; + } + int ret; + CHECK_AND_CALL(ret, r, vfs, close, local_fd); + + unregister_fd(fd); + + return ret; +} + +int esp_vfs_fstat(struct _reent *r, int fd, struct stat *st) +{ + const vfs_entry_t *vfs = get_vfs_for_fd(fd); + const int local_fd = get_local_fd(vfs, fd); + if (vfs == NULL || local_fd < 0) { + __errno_r(r) = EBADF; + return -1; + } + int ret; + CHECK_AND_CALL(ret, r, vfs, fstat, local_fd, st); + return ret; +} + +int esp_vfs_fcntl_r(struct _reent *r, int fd, int cmd, int arg) +{ + const vfs_entry_t *vfs = get_vfs_for_fd(fd); + const int local_fd = get_local_fd(vfs, fd); + if (vfs == NULL || local_fd < 0) { + __errno_r(r) = EBADF; + return -1; + } + int ret; + CHECK_AND_CALL(ret, r, vfs, fcntl, local_fd, cmd, arg); + return ret; +} + +int esp_vfs_ioctl(int fd, int cmd, ...) +{ + const vfs_entry_t *vfs = get_vfs_for_fd(fd); + const int local_fd = get_local_fd(vfs, fd); + struct _reent __attribute__((unused)) *r = __getreent(); + if (vfs == NULL || local_fd < 0) { + __errno_r(r) = EBADF; + return -1; + } + int ret; + va_list args; + va_start(args, cmd); + CHECK_AND_CALL(ret, r, vfs, ioctl, local_fd, cmd, args); + va_end(args); + return ret; +} + +int esp_vfs_fsync(int fd) +{ + const vfs_entry_t *vfs = get_vfs_for_fd(fd); + const int local_fd = get_local_fd(vfs, fd); + struct _reent __attribute__((unused)) *r = __getreent(); + if (vfs == NULL || local_fd < 0) { + __errno_r(r) = EBADF; + return -1; + } + int ret; + CHECK_AND_CALL(ret, r, vfs, fsync, local_fd); + return ret; +} + +#ifdef CONFIG_VFS_SUPPORT_DIR + +int esp_vfs_stat(struct _reent *r, const char *path, struct stat *st) +{ + const vfs_entry_t *vfs = get_vfs_for_path(path); + if (vfs == NULL) { + __errno_r(r) = ENOENT; + return -1; + } + const char *path_within_vfs = translate_path(vfs, path); + int ret; + CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, stat, path_within_vfs, st); + return ret; +} + +int esp_vfs_utime(const char *path, const struct utimbuf *times) +{ + int ret; + const vfs_entry_t *vfs = get_vfs_for_path(path); + struct _reent __attribute__((unused)) *r = __getreent(); + if (vfs == NULL) { + __errno_r(r) = ENOENT; + return -1; + } + const char *path_within_vfs = translate_path(vfs, path); + CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, utime, path_within_vfs, times); + return ret; +} + +int esp_vfs_link(struct _reent *r, const char *n1, const char *n2) +{ + const vfs_entry_t *vfs = get_vfs_for_path(n1); + if (vfs == NULL) { + __errno_r(r) = ENOENT; + return -1; + } + const vfs_entry_t *vfs2 = get_vfs_for_path(n2); + if (vfs != vfs2) { + __errno_r(r) = EXDEV; + return -1; + } + + CHECK_VFS_READONLY_FLAG(vfs2->flags); + + const char *path1_within_vfs = translate_path(vfs, n1); + const char *path2_within_vfs = translate_path(vfs, n2); + int ret; + CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, link, path1_within_vfs, path2_within_vfs); + return ret; +} + +int esp_vfs_unlink(struct _reent *r, const char *path) +{ + const vfs_entry_t *vfs = get_vfs_for_path(path); + if (vfs == NULL) { + __errno_r(r) = ENOENT; + return -1; + } + + CHECK_VFS_READONLY_FLAG(vfs->flags); + + const char *path_within_vfs = translate_path(vfs, path); + int ret; + CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, unlink, path_within_vfs); + return ret; +} + +int esp_vfs_rename(struct _reent *r, const char *src, const char *dst) +{ + const vfs_entry_t *vfs = get_vfs_for_path(src); + if (vfs == NULL) { + __errno_r(r) = ENOENT; + return -1; + } + + CHECK_VFS_READONLY_FLAG(vfs->flags); + + const vfs_entry_t *vfs_dst = get_vfs_for_path(dst); + if (vfs != vfs_dst) { + __errno_r(r) = EXDEV; + return -1; + } + + CHECK_VFS_READONLY_FLAG(vfs_dst->flags); + + const char *src_within_vfs = translate_path(vfs, src); + const char *dst_within_vfs = translate_path(vfs, dst); + int ret; + CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, rename, src_within_vfs, dst_within_vfs); + return ret; +} + +DIR *esp_vfs_opendir(const char *name) +{ + const vfs_entry_t *vfs = get_vfs_for_path(name); + struct _reent __attribute__((unused)) *r = __getreent(); + if (vfs == NULL) { + __errno_r(r) = ENOENT; + return NULL; + } + const char *path_within_vfs = translate_path(vfs, name); + DIR *ret; + CHECK_AND_CALL_SUBCOMPONENTP(ret, r, vfs, dir, opendir, path_within_vfs); + if (ret != NULL) { + ret->dd_vfs_idx = vfs->offset; + } + return ret; +} + +struct dirent *esp_vfs_readdir(DIR *pdir) +{ + const vfs_entry_t *vfs = get_vfs_for_index(pdir->dd_vfs_idx); + struct _reent __attribute__((unused)) *r = __getreent(); + if (vfs == NULL) { + __errno_r(r) = EBADF; + return NULL; + } + struct dirent *ret; + CHECK_AND_CALL_SUBCOMPONENTP(ret, r, vfs, dir, readdir, pdir); + return ret; +} + +int esp_vfs_readdir_r(DIR *pdir, struct dirent *entry, struct dirent* *out_dirent) +{ + const vfs_entry_t *vfs = get_vfs_for_index(pdir->dd_vfs_idx); + struct _reent __attribute__((unused)) *r = __getreent(); + if (vfs == NULL) { + errno = EBADF; + return -1; + } + int ret; + CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, readdir_r, pdir, entry, out_dirent); + return ret; +} + +long esp_vfs_telldir(DIR *pdir) +{ + const vfs_entry_t *vfs = get_vfs_for_index(pdir->dd_vfs_idx); + struct _reent __attribute__((unused)) *r = __getreent(); + if (vfs == NULL) { + errno = EBADF; + return -1; + } + long ret; + CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, telldir, pdir); + return ret; +} + +void esp_vfs_seekdir(DIR *pdir, long loc) +{ + const vfs_entry_t *vfs = get_vfs_for_index(pdir->dd_vfs_idx); + struct _reent __attribute__((unused)) *r = __getreent(); + if (vfs == NULL) { + errno = EBADF; + return; + } + CHECK_AND_CALL_SUBCOMPONENTV(r, vfs, dir, seekdir, pdir, loc); +} + +void esp_vfs_rewinddir(DIR *pdir) +{ + seekdir(pdir, 0); +} + +int esp_vfs_closedir(DIR *pdir) +{ + const vfs_entry_t *vfs = get_vfs_for_index(pdir->dd_vfs_idx); + struct _reent __attribute__((unused)) *r = __getreent(); + if (vfs == NULL) { + errno = EBADF; + return -1; + } + int ret; + CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, closedir, pdir); + return ret; +} + +int esp_vfs_mkdir(const char *name, mode_t mode) +{ + const vfs_entry_t *vfs = get_vfs_for_path(name); + struct _reent __attribute__((unused)) *r = __getreent(); + if (vfs == NULL) { + __errno_r(r) = ENOENT; + return -1; + } + + CHECK_VFS_READONLY_FLAG(vfs->flags); + + const char *path_within_vfs = translate_path(vfs, name); + int ret; + CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, mkdir, path_within_vfs, mode); + return ret; +} + +int esp_vfs_rmdir(const char *name) +{ + const vfs_entry_t *vfs = get_vfs_for_path(name); + struct _reent __attribute__((unused)) *r = __getreent(); + if (vfs == NULL) { + __errno_r(r) = ENOENT; + return -1; + } + + CHECK_VFS_READONLY_FLAG(vfs->flags); + + const char *path_within_vfs = translate_path(vfs, name); + int ret; + CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, rmdir, path_within_vfs); + return ret; +} + +int esp_vfs_access(const char *path, int amode) +{ + int ret; + const vfs_entry_t *vfs = get_vfs_for_path(path); + struct _reent __attribute__((unused)) *r = __getreent(); + if (vfs == NULL) { + __errno_r(r) = ENOENT; + return -1; + } + const char *path_within_vfs = translate_path(vfs, path); + CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, access, path_within_vfs, amode); + return ret; +} + +int esp_vfs_truncate(const char *path, off_t length) +{ + int ret; + const vfs_entry_t *vfs = get_vfs_for_path(path); + struct _reent __attribute__((unused)) *r = __getreent(); + if (vfs == NULL) { + __errno_r(r) = ENOENT; + return -1; + } + + CHECK_VFS_READONLY_FLAG(vfs->flags); + + const char *path_within_vfs = translate_path(vfs, path); + CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, truncate, path_within_vfs, length); + return ret; +} + +int esp_vfs_ftruncate(int fd, off_t length) +{ + const vfs_entry_t *vfs = get_vfs_for_fd(fd); + int local_fd = get_local_fd(vfs, fd); + struct _reent __attribute__((unused)) *r = __getreent(); + if (vfs == NULL || local_fd < 0) { + __errno_r(r) = EBADF; + return -1; + } + + CHECK_VFS_READONLY_FLAG(vfs->flags); + + int ret; + CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, dir, ftruncate, local_fd, length); + return ret; +} + +#endif // CONFIG_VFS_SUPPORT_DIR + +#ifdef CONFIG_VFS_SUPPORT_SELECT + +static void call_end_selects(int end_index, const fds_triple_t *vfs_fds_triple, void **driver_args) +{ + for (int i = 0; i < end_index; ++i) { + const vfs_entry_t *vfs = get_vfs_for_index(i); + const fds_triple_t *item = &vfs_fds_triple[i]; + if (vfs != NULL + && vfs->vfs->select != NULL + && vfs->vfs->select->end_select != NULL + && item->isset + ) { + esp_err_t err = vfs->vfs->select->end_select(driver_args[i]); + if (err != ESP_OK) { + ESP_LOGD(TAG, "end_select failed: %s", esp_err_to_name(err)); + } + } + } +} + +static int set_global_fd_sets(const fds_triple_t *vfs_fds_triple, int size, fd_set *readfds, fd_set *writefds, fd_set *errorfds) +{ + int ret = 0; + + for (int i = 0; i < size; ++i) { + const fds_triple_t *item = &vfs_fds_triple[i]; + if (item->isset) { + for (int fd = 0; fd < MAX_FDS; ++fd) { + const fd_table_t *fd_entry = get_fd_entry(fd); + if (fd_entry->vfs_index == i) { + const int local_fd = fd_entry->local_fd; // single read -> no locking is required + if (readfds && esp_vfs_safe_fd_isset(local_fd, &item->readfds)) { + ESP_LOGD(TAG, "FD %d in readfds was set from VFS ID %d", fd, i); + FD_SET(fd, readfds); + ++ret; + } + if (writefds && esp_vfs_safe_fd_isset(local_fd, &item->writefds)) { + ESP_LOGD(TAG, "FD %d in writefds was set from VFS ID %d", fd, i); + FD_SET(fd, writefds); + ++ret; + } + if (errorfds && esp_vfs_safe_fd_isset(local_fd, &item->errorfds)) { + ESP_LOGD(TAG, "FD %d in errorfds was set from VFS ID %d", fd, i); + FD_SET(fd, errorfds); + ++ret; + } + } + } + } + } + + return ret; +} + +static void esp_vfs_log_fd_set(const char *fds_name, const fd_set *fds) +{ + if (fds_name && fds) { + ESP_LOGD(TAG, "FDs in %s =", fds_name); + for (int i = 0; i < MAX_FDS; ++i) { + if (esp_vfs_safe_fd_isset(i, fds)) { + ESP_LOGD(TAG, "%d", i); + } + } + } +} + +int esp_vfs_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout) +{ + // NOTE: Please see the "Synchronous input/output multiplexing" section of the ESP-IDF Programming Guide + // (API Reference -> Storage -> Virtual Filesystem) for a general overview of the implementation of VFS select(). + int ret = 0; + struct _reent __attribute__((unused)) *r = __getreent(); + + ESP_LOGD(TAG, "esp_vfs_select starts with nfds = %d", nfds); + if (timeout) { + ESP_LOGD(TAG, "timeout is %lds + %ldus", (long)timeout->tv_sec, timeout->tv_usec); + } + esp_vfs_log_fd_set("readfds", readfds); + esp_vfs_log_fd_set("writefds", writefds); + esp_vfs_log_fd_set("errorfds", errorfds); + + if (nfds > MAX_FDS || nfds < 0) { + ESP_LOGD(TAG, "incorrect nfds"); + __errno_r(r) = EINVAL; + return -1; + } + + // Capture s_vfs_count to a local variable in case a new driver is registered or removed during this actual select() + // call. s_vfs_count cannot be protected with a mutex during a select() call (which can be one without a timeout) + // because that could block the registration of new driver. + const size_t vfs_count = get_vfs_count(); + fds_triple_t *vfs_fds_triple; + if ((vfs_fds_triple = heap_caps_calloc(vfs_count, sizeof(fds_triple_t), VFS_MALLOC_FLAGS)) == NULL) { + __errno_r(r) = ENOMEM; + ESP_LOGD(TAG, "calloc is unsuccessful"); + return -1; + } + + esp_vfs_select_sem_t sel_sem = { + .is_sem_local = false, + .sem = NULL, + }; + + int (*socket_select)(int, fd_set *, fd_set *, fd_set *, struct timeval *) = NULL; + for (int fd = 0; fd < nfds; ++fd) { + const fd_table_t fd_entry = start_select(fd, errorfds); + const bool is_socket_fd = fd_entry.permanent; + const int vfs_index = fd_entry.vfs_index; + const int local_fd = fd_entry.local_fd; + + if (vfs_index < 0) { + continue; + } + + if (is_socket_fd) { + if (!socket_select) { + // no socket_select found yet so take a look + if (esp_vfs_safe_fd_isset(fd, readfds) || + esp_vfs_safe_fd_isset(fd, writefds) || + esp_vfs_safe_fd_isset(fd, errorfds)) { + const vfs_entry_t *vfs = get_vfs_for_index(vfs_index); + socket_select = vfs->vfs->select->socket_select; + sel_sem.sem = vfs->vfs->select->get_socket_select_semaphore(); + } + } + continue; + } + + fds_triple_t *item = &vfs_fds_triple[vfs_index]; // FD sets for VFS which belongs to fd + if (esp_vfs_safe_fd_isset(fd, readfds)) { + item->isset = true; + FD_SET(local_fd, &item->readfds); + FD_CLR(fd, readfds); + ESP_LOGD(TAG, "removing %d from readfds and adding as local FD %d to fd_set of VFS ID %d", fd, local_fd, vfs_index); + } + if (esp_vfs_safe_fd_isset(fd, writefds)) { + item->isset = true; + FD_SET(local_fd, &item->writefds); + FD_CLR(fd, writefds); + ESP_LOGD(TAG, "removing %d from writefds and adding as local FD %d to fd_set of VFS ID %d", fd, local_fd, vfs_index); + } + if (esp_vfs_safe_fd_isset(fd, errorfds)) { + item->isset = true; + FD_SET(local_fd, &item->errorfds); + FD_CLR(fd, errorfds); + ESP_LOGD(TAG, "removing %d from errorfds and adding as local FD %d to fd_set of VFS ID %d", fd, local_fd, vfs_index); + } + } + + // all non-socket VFSs have their FD sets in vfs_fds_triple + // the global readfds, writefds and errorfds contain only socket FDs (if + // there any) + + if (!socket_select) { + // There is no socket VFS registered or select() wasn't called for + // any socket. Therefore, we will use our own signalization. + sel_sem.is_sem_local = true; + if ((sel_sem.sem = xSemaphoreCreateBinary()) == NULL) { + free(vfs_fds_triple); + __errno_r(r) = ENOMEM; + ESP_LOGD(TAG, "cannot create select semaphore"); + return -1; + } + } + + void **driver_args = heap_caps_calloc(vfs_count, sizeof(void *), VFS_MALLOC_FLAGS); + + if (driver_args == NULL) { + free(vfs_fds_triple); + __errno_r(r) = ENOMEM; + ESP_LOGD(TAG, "calloc is unsuccessful for driver args"); + return -1; + } + + for (size_t i = 0; i < vfs_count; ++i) { + const vfs_entry_t *vfs = get_vfs_for_index(i); + fds_triple_t *item = &vfs_fds_triple[i]; + + if (vfs == NULL || vfs->vfs->select == NULL || vfs->vfs->select->start_select == NULL) { + ESP_LOGD(TAG, "start_select function callback for this vfs (s_vfs[%d]) is not defined", vfs->offset); + continue; + } + + if (!item->isset) { + continue; + } + + // call start_select for all non-socket VFSs with has at least one FD set in readfds, writefds, or errorfds + // note: it can point to socket VFS but item->isset will be false for that + ESP_LOGD(TAG, "calling start_select for VFS ID " PRIuSIZE " with the following local FDs", i); + esp_vfs_log_fd_set("readfds", &item->readfds); + esp_vfs_log_fd_set("writefds", &item->writefds); + esp_vfs_log_fd_set("errorfds", &item->errorfds); + esp_err_t err = vfs->vfs->select->start_select(nfds, &item->readfds, &item->writefds, &item->errorfds, sel_sem, + driver_args + i); + + if (err != ESP_OK) { + if (err != ESP_ERR_NOT_SUPPORTED) { + call_end_selects(i, vfs_fds_triple, driver_args); + } + (void) set_global_fd_sets(vfs_fds_triple, vfs_count, readfds, writefds, errorfds); + if (sel_sem.is_sem_local && sel_sem.sem) { + vSemaphoreDelete(sel_sem.sem); + sel_sem.sem = NULL; + } + free(vfs_fds_triple); + free(driver_args); + __errno_r(r) = EINTR; + ESP_LOGD(TAG, "start_select failed: %s", esp_err_to_name(err)); + return -1; + } + } + + if (socket_select) { + ESP_LOGD(TAG, "calling socket_select with the following FDs"); + esp_vfs_log_fd_set("readfds", readfds); + esp_vfs_log_fd_set("writefds", writefds); + esp_vfs_log_fd_set("errorfds", errorfds); + ret = socket_select(nfds, readfds, writefds, errorfds, timeout); + ESP_LOGD(TAG, "socket_select returned %d and the FDs are the following", ret); + esp_vfs_log_fd_set("readfds", readfds); + esp_vfs_log_fd_set("writefds", writefds); + esp_vfs_log_fd_set("errorfds", errorfds); + } else { + if (readfds) { + FD_ZERO(readfds); + } + if (writefds) { + FD_ZERO(writefds); + } + if (errorfds) { + FD_ZERO(errorfds); + } + + TickType_t ticks_to_wait = portMAX_DELAY; + if (timeout) { + uint32_t timeout_ms = (timeout->tv_sec *1000) + (timeout->tv_usec / 1000); + /* Round up the number of ticks. + * Not only we need to round up the number of ticks, but we also need to add 1. + * Indeed, `select` function shall wait for AT LEAST timeout, but on FreeRTOS, + * if we specify a timeout of 1 tick to `xSemaphoreTake`, it will take AT MOST + * 1 tick before triggering a timeout. Thus, we need to pass 2 ticks as a timeout + * to `xSemaphoreTake`. */ + ticks_to_wait = ((timeout_ms + portTICK_PERIOD_MS - 1) / portTICK_PERIOD_MS) + 1; + ESP_LOGD(TAG, "timeout is %" PRIu32 "ms", timeout_ms); + } + ESP_LOGD(TAG, "waiting without calling socket_select"); + xSemaphoreTake(sel_sem.sem, ticks_to_wait); + } + + call_end_selects(vfs_count, vfs_fds_triple, driver_args); // for VFSs for start_select was called before + + if (ret >= 0) { + ret += set_global_fd_sets(vfs_fds_triple, vfs_count, readfds, writefds, errorfds); + } + if (sel_sem.sem) { // Cleanup the select semaphore + if (sel_sem.is_sem_local) { + vSemaphoreDelete(sel_sem.sem); + } else if (socket_select) { + SemaphoreHandle_t *s = sel_sem.sem; + /* Select might have been triggered from both lwip and vfs fds at the same time, and + * we have to make sure that the lwip semaphore is cleared when we exit select(). + * It is safe, as the semaphore belongs to the calling thread. */ + xSemaphoreTake(*s, 0); + } + sel_sem.sem = NULL; + } + close_pending(nfds); + free(vfs_fds_triple); + free(driver_args); + + ESP_LOGD(TAG, "esp_vfs_select returns %d", ret); + esp_vfs_log_fd_set("readfds", readfds); + esp_vfs_log_fd_set("writefds", writefds); + esp_vfs_log_fd_set("errorfds", errorfds); + return ret; +} + +void esp_vfs_select_triggered(esp_vfs_select_sem_t sem) +{ + if (sem.is_sem_local) { + xSemaphoreGive(sem.sem); + } else { + // Another way would be to go through s_fd_table and find the VFS + // which has a permanent FD. But in order to avoid to lock + // s_fd_table_lock we go through the VFS table. + size_t vfs_count = get_vfs_count(); + for (int i = 0; i < vfs_count; ++i) { + // Note: vfs_count could have changed since the start of vfs_select() call. However, that change doesn't + // matter here stop_socket_select() will be called for only valid VFS drivers. + const vfs_entry_t *vfs = get_vfs_for_index(i); + if (vfs != NULL + && vfs->vfs->select != NULL + && vfs->vfs->select->stop_socket_select != NULL + ) { + vfs->vfs->select->stop_socket_select(sem.sem); + break; + } + } + } +} + +void esp_vfs_select_triggered_isr(esp_vfs_select_sem_t sem, BaseType_t *woken) +{ + if (sem.is_sem_local) { + xSemaphoreGiveFromISR(sem.sem, woken); + } else { + // Another way would be to go through s_fd_table and find the VFS + // which has a permanent FD. But in order to avoid to lock + // s_fd_table_lock we go through the VFS table. + size_t vfs_count = get_vfs_count(); + for (int i = 0; i < vfs_count; ++i) { + // Note: s_vfs_count could have changed since the start of vfs_select() call. However, that change doesn't + // matter here stop_socket_select() will be called for only valid VFS drivers. + const vfs_entry_t *vfs = get_vfs_for_index(i); + if (vfs != NULL + && vfs->vfs->select != NULL + && vfs->vfs->select->stop_socket_select_isr != NULL + ) { + // Note: If the UART ISR resides in IRAM, the function referenced by stop_socket_select_isr should also be placed in IRAM. + vfs->vfs->select->stop_socket_select_isr(sem.sem, woken); + break; + } + } + } +} + +#endif // CONFIG_VFS_SUPPORT_SELECT + +#ifdef CONFIG_VFS_SUPPORT_TERMIOS + +int tcgetattr(int fd, struct termios *p) +{ + const vfs_entry_t *vfs = get_vfs_for_fd(fd); + const int local_fd = get_local_fd(vfs, fd); + struct _reent __attribute__((unused)) *r = __getreent(); + if (vfs == NULL || local_fd < 0) { + __errno_r(r) = EBADF; + return -1; + } + int ret; + CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, termios, tcgetattr, local_fd, p); + return ret; +} + +int tcsetattr(int fd, int optional_actions, const struct termios *p) +{ + const vfs_entry_t *vfs = get_vfs_for_fd(fd); + const int local_fd = get_local_fd(vfs, fd); + struct _reent __attribute__((unused)) *r = __getreent(); + if (vfs == NULL || local_fd < 0) { + __errno_r(r) = EBADF; + return -1; + } + int ret; + CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, termios, tcsetattr, local_fd, optional_actions, p); + return ret; +} + +int tcdrain(int fd) +{ + const vfs_entry_t *vfs = get_vfs_for_fd(fd); + const int local_fd = get_local_fd(vfs, fd); + struct _reent __attribute__((unused)) *r = __getreent(); + if (vfs == NULL || local_fd < 0) { + __errno_r(r) = EBADF; + return -1; + } + int ret; + CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, termios, tcdrain, local_fd); + return ret; +} + +int tcflush(int fd, int select) +{ + const vfs_entry_t *vfs = get_vfs_for_fd(fd); + const int local_fd = get_local_fd(vfs, fd); + struct _reent __attribute__((unused)) *r = __getreent(); + if (vfs == NULL || local_fd < 0) { + __errno_r(r) = EBADF; + return -1; + } + int ret; + CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, termios, tcflush, local_fd, select); + return ret; +} + +int tcflow(int fd, int action) +{ + const vfs_entry_t *vfs = get_vfs_for_fd(fd); + const int local_fd = get_local_fd(vfs, fd); + struct _reent __attribute__((unused)) *r = __getreent(); + if (vfs == NULL || local_fd < 0) { + __errno_r(r) = EBADF; + return -1; + } + int ret; + CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, termios, tcflow, local_fd, action); + return ret; +} + +pid_t tcgetsid(int fd) +{ + const vfs_entry_t *vfs = get_vfs_for_fd(fd); + const int local_fd = get_local_fd(vfs, fd); + struct _reent __attribute__((unused)) *r = __getreent(); + if (vfs == NULL || local_fd < 0) { + __errno_r(r) = EBADF; + return -1; + } + int ret; + CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, termios, tcgetsid, local_fd); + return ret; +} + +int tcsendbreak(int fd, int duration) +{ + const vfs_entry_t *vfs = get_vfs_for_fd(fd); + const int local_fd = get_local_fd(vfs, fd); + struct _reent __attribute__((unused)) *r = __getreent(); + if (vfs == NULL || local_fd < 0) { + __errno_r(r) = EBADF; + return -1; + } + int ret; + CHECK_AND_CALL_SUBCOMPONENT(ret, r, vfs, termios, tcsendbreak, local_fd, duration); + return ret; +} +#endif // CONFIG_VFS_SUPPORT_TERMIOS + +#ifndef CONFIG_IDF_TARGET_LINUX + +/* Create aliases for libc syscalls + + These functions are also available in ROM as stubs which use the syscall table, but linking them + directly here saves an additional function call when a software function is linked to one, and + makes linking with -stdlib easier. + */ +#ifdef CONFIG_VFS_SUPPORT_IO +int _open_r(struct _reent *r, const char *path, int flags, int mode) + __attribute__((alias("esp_vfs_open"))); +int _close_r(struct _reent *r, int fd) + __attribute__((alias("esp_vfs_close"))); +ssize_t _read_r(struct _reent *r, int fd, void *dst, size_t size) + __attribute__((alias("esp_vfs_read"))); +ssize_t _write_r(struct _reent *r, int fd, const void *data, size_t size) + __attribute__((alias("esp_vfs_write"))); +ssize_t pread(int fd, void *dst, size_t size, off_t offset) + __attribute__((alias("esp_vfs_pread"))); +ssize_t pwrite(int fd, const void *src, size_t size, off_t offset) + __attribute__((alias("esp_vfs_pwrite"))); +off_t _lseek_r(struct _reent *r, int fd, off_t size, int mode) + __attribute__((alias("esp_vfs_lseek"))); +int _fcntl_r(struct _reent *r, int fd, int cmd, int arg) + __attribute__((alias("esp_vfs_fcntl_r"))); +int _fstat_r(struct _reent *r, int fd, struct stat *st) + __attribute__((alias("esp_vfs_fstat"))); +int fsync(int fd) + __attribute__((alias("esp_vfs_fsync"))); +int ioctl(int fd, int cmd, ...) + __attribute__((alias("esp_vfs_ioctl"))); +#endif // CONFIG_VFS_SUPPORT_IO + +#ifdef CONFIG_VFS_SUPPORT_SELECT +int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout) + __attribute__((alias("esp_vfs_select"))); +#endif // CONFIG_VFS_SUPPORT_SELECT + +#ifdef CONFIG_VFS_SUPPORT_DIR +int _stat_r(struct _reent *r, const char *path, struct stat *st) + __attribute__((alias("esp_vfs_stat"))); +int _link_r(struct _reent *r, const char *n1, const char *n2) + __attribute__((alias("esp_vfs_link"))); +int _unlink_r(struct _reent *r, const char *path) + __attribute__((alias("esp_vfs_unlink"))); +int _rename_r(struct _reent *r, const char *src, const char *dst) + __attribute__((alias("esp_vfs_rename"))); +int truncate(const char *path, off_t length) + __attribute__((alias("esp_vfs_truncate"))); +int ftruncate(int fd, off_t length) + __attribute__((alias("esp_vfs_ftruncate"))); +int access(const char *path, int amode) + __attribute__((alias("esp_vfs_access"))); +int utime(const char *path, const struct utimbuf *times) + __attribute__((alias("esp_vfs_utime"))); +int rmdir(const char *name) + __attribute__((alias("esp_vfs_rmdir"))); +int mkdir(const char *name, mode_t mode) + __attribute__((alias("esp_vfs_mkdir"))); +DIR *opendir(const char *name) + __attribute__((alias("esp_vfs_opendir"))); +int closedir(DIR *pdir) + __attribute__((alias("esp_vfs_closedir"))); +int readdir_r(DIR *pdir, struct dirent *entry, struct dirent* *out_dirent) + __attribute__((alias("esp_vfs_readdir_r"))); +struct dirent *readdir(DIR *pdir) + __attribute__((alias("esp_vfs_readdir"))); +long telldir(DIR *pdir) + __attribute__((alias("esp_vfs_telldir"))); +void seekdir(DIR *pdir, long loc) + __attribute__((alias("esp_vfs_seekdir"))); +void rewinddir(DIR *pdir) + __attribute__((alias("esp_vfs_rewinddir"))); +#endif // CONFIG_VFS_SUPPORT_DIR + +#endif //CONFIG_IDF_TARGET_LINUX + +void vfs_include_syscalls_impl(void) +{ + // Linker hook function, exists to make the linker examine this file +} From 8c9d62de9814dd0cde96fd47c18ee39f65b7b0dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Rohl=C3=ADnek?= Date: Wed, 3 Dec 2025 11:46:55 +0100 Subject: [PATCH 2/6] feat(storage/vfs): Remove old API usage --- .../btc/profile/std/l2cap/btc_l2cap.c | 8 +-- .../bluedroid/btc/profile/std/spp/btc_spp.c | 8 +-- .../include/driver/esp_private/uart_vfs.h | 2 +- .../driver/esp_private/usb_serial_jtag_vfs.h | 2 +- .../test_apps/newlib/main/test_file.c | 8 ++- .../test_apps/newlib/main/test_misc.c | 11 ++-- .../include/esp_private/esp_vfs_cdcacm.h | 2 +- components/fatfs/vfs/vfs_fat_spiflash.c | 2 +- components/lwip/port/esp32xx/vfs_lwip.c | 47 ++++++++++------- components/vfs/include/esp_vfs.h | 2 +- components/vfs/test_apps/main/test_vfs_fd.c | 28 +++++----- components/vfs/test_apps/main/test_vfs_open.c | 7 +-- .../vfs/test_apps/main/test_vfs_paths.c | 49 +++++++++--------- components/vfs/vfs.c | 2 +- components/vfs/vfs_eventfd.c | 30 ++++++----- docs/en/api-reference/storage/vfs.rst | 6 +-- docs/zh_CN/api-reference/storage/vfs.rst | 6 +-- .../main/idf_component.yml | 2 +- .../tusb_console/main/idf_component.yml | 2 +- .../tusb_serial_device/main/idf_component.yml | 2 +- .../std_filesystem/main/test_status.cpp | 51 +++++++++++++++---- 21 files changed, 154 insertions(+), 123 deletions(-) diff --git a/components/bt/host/bluedroid/btc/profile/std/l2cap/btc_l2cap.c b/components/bt/host/bluedroid/btc/profile/std/l2cap/btc_l2cap.c index 63477d6ce5..81b7ebf44e 100644 --- a/components/bt/host/bluedroid/btc/profile/std/l2cap/btc_l2cap.c +++ b/components/bt/host/bluedroid/btc/profile/std/l2cap/btc_l2cap.c @@ -1252,19 +1252,15 @@ static void btc_l2cap_vfs_register(void) break; } - esp_vfs_t vfs = { - .flags = ESP_VFS_FLAG_DEFAULT, + static const esp_vfs_fs_ops_t vfs = { .write = l2cap_vfs_write, - .open = NULL, - .fstat = NULL, .close = l2cap_vfs_close, .read = l2cap_vfs_read, - .fcntl = NULL }; // No FD range is registered here: l2cap_vfs_id is used to register/unregister // file descriptors - if (esp_vfs_register_with_id(&vfs, NULL, &l2cap_local_param.l2cap_vfs_id) != ESP_OK) { + if (esp_vfs_register_fs_with_id(&vfs, ESP_VFS_FLAG_STATIC, NULL, &l2cap_local_param.l2cap_vfs_id) != ESP_OK) { ret = ESP_BT_L2CAP_FAILURE; break; } diff --git a/components/bt/host/bluedroid/btc/profile/std/spp/btc_spp.c b/components/bt/host/bluedroid/btc/profile/std/spp/btc_spp.c index 8607e42191..33e5a59070 100644 --- a/components/bt/host/bluedroid/btc/profile/std/spp/btc_spp.c +++ b/components/bt/host/bluedroid/btc/profile/std/spp/btc_spp.c @@ -1659,19 +1659,15 @@ static void btc_spp_vfs_register(void) break; } - esp_vfs_t vfs = { - .flags = ESP_VFS_FLAG_DEFAULT, + static const esp_vfs_fs_ops_t vfs = { .write = spp_vfs_write, - .open = NULL, - .fstat = NULL, .close = spp_vfs_close, .read = spp_vfs_read, - .fcntl = NULL }; // No FD range is registered here: spp_vfs_id is used to register/unregister // file descriptors - if (esp_vfs_register_with_id(&vfs, NULL, &spp_local_param.spp_vfs_id) != ESP_OK) { + if (esp_vfs_register_fs_with_id(&vfs, ESP_VFS_FLAG_STATIC, NULL, &spp_local_param.spp_vfs_id) != ESP_OK) { ret = ESP_SPP_FAILURE; break; } diff --git a/components/esp_driver_uart/include/driver/esp_private/uart_vfs.h b/components/esp_driver_uart/include/driver/esp_private/uart_vfs.h index b74996ae6f..ced7196835 100644 --- a/components/esp_driver_uart/include/driver/esp_private/uart_vfs.h +++ b/components/esp_driver_uart/include/driver/esp_private/uart_vfs.h @@ -18,7 +18,7 @@ extern "C" { * This function is called in vfs_console in order to get the vfs implementation * of uart. * - * @return pointer to structure esp_vfs_t + * @return pointer to structure esp_vfs_fs_ops_t */ const esp_vfs_fs_ops_t *esp_vfs_uart_get_vfs(void); diff --git a/components/esp_driver_usb_serial_jtag/include/driver/esp_private/usb_serial_jtag_vfs.h b/components/esp_driver_usb_serial_jtag/include/driver/esp_private/usb_serial_jtag_vfs.h index 4415447613..3c4c0f9ecd 100644 --- a/components/esp_driver_usb_serial_jtag/include/driver/esp_private/usb_serial_jtag_vfs.h +++ b/components/esp_driver_usb_serial_jtag/include/driver/esp_private/usb_serial_jtag_vfs.h @@ -18,7 +18,7 @@ extern "C" { * This function is called in vfs_console in order to get the vfs implementation * of usb_serial_jtag. * - * @return pointer to structure esp_vfs_t + * @return pointer to structure esp_vfs_fs_ops_t */ const esp_vfs_fs_ops_t *esp_vfs_usb_serial_jtag_get_vfs(void); diff --git a/components/esp_libc/test_apps/newlib/main/test_file.c b/components/esp_libc/test_apps/newlib/main/test_file.c index a7f5cab625..7d2530564c 100644 --- a/components/esp_libc/test_apps/newlib/main/test_file.c +++ b/components/esp_libc/test_apps/newlib/main/test_file.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -47,14 +47,12 @@ TEST_CASE("file - blksize", "[newlib_file]") FILE* f; blksize_test_ctx_t ctx = {}; const char c = 42; - const esp_vfs_t desc = { - .flags = ESP_VFS_FLAG_CONTEXT_PTR, + static const esp_vfs_fs_ops_t desc = { .open_p = blksize_test_open, .fstat_p = blksize_test_fstat, .write_p = blksize_test_write, }; - - TEST_ESP_OK(esp_vfs_register("/test", &desc, &ctx)); + TEST_ESP_OK(esp_vfs_register_fs("/test", &desc, ESP_VFS_FLAG_CONTEXT_PTR | ESP_VFS_FLAG_STATIC, &ctx)); /* test with zero st_blksize (=not set) */ ctx.blksize = 0; diff --git a/components/esp_libc/test_apps/newlib/main/test_misc.c b/components/esp_libc/test_apps/newlib/main/test_misc.c index bc4e6656c8..dd825c775d 100644 --- a/components/esp_libc/test_apps/newlib/main/test_misc.c +++ b/components/esp_libc/test_apps/newlib/main/test_misc.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -153,14 +153,17 @@ TEST_CASE("file - scandir", "[newlib_misc]") scandir_test_dir_context_t scandir_test_dir_ctx = { .filenames = test_filenames, .num_files = num_test_files }; - const esp_vfs_t scandir_test_vfs = { - .flags = ESP_VFS_FLAG_CONTEXT_PTR, + static const esp_vfs_dir_ops_t dir_ops = { .opendir_p = scandir_test_opendir, .readdir_p = scandir_test_readdir, .closedir_p = scandir_test_closedir }; - TEST_ESP_OK(esp_vfs_register("/data", &scandir_test_vfs, &scandir_test_dir_ctx)); + static const esp_vfs_fs_ops_t fs_ops = { + .dir = &dir_ops, + }; + + TEST_ESP_OK(esp_vfs_register_fs("/data", &fs_ops, ESP_VFS_FLAG_CONTEXT_PTR | ESP_VFS_FLAG_STATIC, &scandir_test_dir_ctx)); struct dirent **namelist; s_select_calls = 0; diff --git a/components/esp_usb_cdc_rom_console/include/esp_private/esp_vfs_cdcacm.h b/components/esp_usb_cdc_rom_console/include/esp_private/esp_vfs_cdcacm.h index d03b06b3fc..8c1f911a50 100644 --- a/components/esp_usb_cdc_rom_console/include/esp_private/esp_vfs_cdcacm.h +++ b/components/esp_usb_cdc_rom_console/include/esp_private/esp_vfs_cdcacm.h @@ -18,7 +18,7 @@ extern "C" { * This function is called in vfs_console in order to get the vfs implementation * of cdcacm. * - * @return pointer to structure esp_vfs_t + * @return pointer to structure esp_vfs_fs_ops_t */ const esp_vfs_fs_ops_t *esp_vfs_cdcacm_get_vfs(void); diff --git a/components/fatfs/vfs/vfs_fat_spiflash.c b/components/fatfs/vfs/vfs_fat_spiflash.c index 0ee00e25d7..a5f88a2519 100644 --- a/components/fatfs/vfs/vfs_fat_spiflash.c +++ b/components/fatfs/vfs/vfs_fat_spiflash.c @@ -23,7 +23,7 @@ static const char* TAG = "vfs_fat_spiflash"; static vfs_fat_spiflash_ctx_t *s_ctx[FF_VOLUMES] = {}; -extern esp_err_t esp_vfs_set_readonly_flag(const char* base_path); // from vfs/vfs.c to set readonly flag in esp_vfs_t struct externally +extern esp_err_t esp_vfs_set_readonly_flag(const char* base_path); // from vfs/vfs.c to set readonly flag externally static bool s_get_context_id_by_label(const char *label, uint32_t *out_id) { diff --git a/components/lwip/port/esp32xx/vfs_lwip.c b/components/lwip/port/esp32xx/vfs_lwip.c index 8c42cddc52..d59922d594 100644 --- a/components/lwip/port/esp32xx/vfs_lwip.c +++ b/components/lwip/port/esp32xx/vfs_lwip.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2017-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -88,26 +88,39 @@ static int lwip_fstat(int fd, struct stat * st) void esp_vfs_lwip_sockets_register(void) { - esp_vfs_t vfs = { - .flags = ESP_VFS_FLAG_DEFAULT, - .write = &lwip_write, - .open = NULL, - .fstat = &lwip_fstat, - .close = &lwip_close, - .read = &lwip_read, - .fcntl = &lwip_fcntl_r_wrapper, - .ioctl = &lwip_ioctl_r_wrapper, + #ifdef CONFIG_VFS_SUPPORT_SELECT + + static const esp_vfs_select_ops_t s_lwip_select_ops = { .socket_select = &lwip_select, - .get_socket_select_semaphore = &lwip_get_socket_select_semaphore, .stop_socket_select = &lwip_stop_socket_select, .stop_socket_select_isr = &lwip_stop_socket_select_isr, -#endif // CONFIG_VFS_SUPPORT_SELECT + .get_socket_select_semaphore = &lwip_get_socket_select_semaphore, }; - /* Non-LWIP file descriptors are from 0 to (LWIP_SOCKET_OFFSET-1). LWIP - * file descriptors are registered from LWIP_SOCKET_OFFSET to - * MAX_FDS-1. - */ - ESP_ERROR_CHECK(esp_vfs_register_fd_range(&vfs, NULL, LWIP_SOCKET_OFFSET, MAX_FDS)); +#endif + + static const esp_vfs_fs_ops_t s_lwip_vfs = { + .write = &lwip_write, + .read = &lwip_read, + .close = &lwip_close, + .fstat = &lwip_fstat, + .fcntl = &lwip_fcntl_r_wrapper, + .ioctl = &lwip_ioctl_r_wrapper, +#ifdef CONFIG_VFS_SUPPORT_SELECT + .select = &s_lwip_select_ops, +#endif + }; + + /* Non-LWIP file descriptors are from 0 to (LWIP_SOCKET_OFFSET-1). + * LWIP file descriptors are registered from LWIP_SOCKET_OFFSET to MAX_FDS-1. + * + * Use ESP_VFS_FLAG_STATIC since s_lwip_vfs and subcomponents are static. + * No context pointer needed -> flags have no ESP_VFS_FLAG_CONTEXT_PTR. + */ + ESP_ERROR_CHECK(esp_vfs_register_fd_range(&s_lwip_vfs, + ESP_VFS_FLAG_STATIC, + NULL /* ctx */, + LWIP_SOCKET_OFFSET, + MAX_FDS)); } diff --git a/components/vfs/include/esp_vfs.h b/components/vfs/include/esp_vfs.h index 4f515eb488..c8f6d9ccdb 100644 --- a/components/vfs/include/esp_vfs.h +++ b/components/vfs/include/esp_vfs.h @@ -292,7 +292,7 @@ esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ct * registered, ESP_ERR_INVALID_ARG if the file descriptor boundaries * are incorrect. */ -esp_err_t esp_vfs_register_fd_range(const esp_vfs_t *vfs, void *ctx, int min_fd, int max_fd); +esp_err_t esp_vfs_register_fd_range(const esp_vfs_fs_ops_t *vfs, int flags, void *ctx, int min_fd, int max_fd); /** * Special case function for registering a VFS that uses a method other than diff --git a/components/vfs/test_apps/main/test_vfs_fd.c b/components/vfs/test_apps/main/test_vfs_fd.c index 353701cdca..904e864032 100644 --- a/components/vfs/test_apps/main/test_vfs_fd.c +++ b/components/vfs/test_apps/main/test_vfs_fd.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -60,13 +60,12 @@ TEST_CASE("FDs from different VFSs don't collide", "[vfs]") .fd = 1, }; - esp_vfs_t desc = { - .flags = ESP_VFS_FLAG_CONTEXT_PTR, + esp_vfs_fs_ops_t desc = { .open_p = collision_test_vfs_open, .close_p = collision_test_vfs_close, }; - TEST_ESP_OK( esp_vfs_register(VFS_PREF1, &desc, ¶m) ); - TEST_ESP_OK( esp_vfs_register(VFS_PREF2, &desc, ¶m) ); + TEST_ESP_OK( esp_vfs_register_fs(VFS_PREF1, &desc, ESP_VFS_FLAG_CONTEXT_PTR, ¶m) ); + TEST_ESP_OK( esp_vfs_register_fs(VFS_PREF2, &desc, ESP_VFS_FLAG_CONTEXT_PTR, ¶m) ); const int fd1 = open(VFS_PREF1 FILE1, 0, 0); const int fd2 = open(VFS_PREF2 FILE1, 0, 0); @@ -155,13 +154,12 @@ static void concurrent_task(void *param) TEST_CASE("VFS can handle concurrent open/close requests", "[vfs]") { - esp_vfs_t desc = { - .flags = ESP_VFS_FLAG_DEFAULT, + esp_vfs_fs_ops_t desc = { .open = concurrent_test_vfs_open, .close = concurrent_test_vfs_close, }; - TEST_ESP_OK( esp_vfs_register(VFS_PREF1, &desc, NULL) ); + TEST_ESP_OK( esp_vfs_register_fs(VFS_PREF1, &desc, ESP_VFS_FLAG_DEFAULT, NULL) ); concurrent_test_task_param_t param1 = { .path = VFS_PREF1 FILE1, .done = xSemaphoreCreateBinary() }; concurrent_test_task_param_t param2 = { .path = VFS_PREF1 FILE1, .done = xSemaphoreCreateBinary() }; @@ -233,14 +231,13 @@ static int time_test_vfs_write(int fd, const void *data, size_t size) TEST_CASE("Open & write & close through VFS passes performance test", "[vfs]") { - esp_vfs_t desc = { - .flags = ESP_VFS_FLAG_DEFAULT, + esp_vfs_fs_ops_t desc = { .open = time_test_vfs_open, .close = time_test_vfs_close, .write = time_test_vfs_write, }; - TEST_ESP_OK( esp_vfs_register(VFS_PREF1, &desc, NULL) ); + TEST_ESP_OK( esp_vfs_register_fs(VFS_PREF1, &desc, ESP_VFS_FLAG_DEFAULT, NULL) ); ccomp_timer_start(); const int iter_count = 5000; @@ -278,17 +275,16 @@ static int vfs_overlap_test_close(int fd) TEST_CASE("esp_vfs_register_fd_range checks for overlap", "[vfs]") { - esp_vfs_t vfs1 = { + esp_vfs_fs_ops_t vfs1 = { .open = vfs_overlap_test_open, .close = vfs_overlap_test_close }; - - TEST_ESP_OK(esp_vfs_register("/test", &vfs1, NULL)); + TEST_ESP_OK(esp_vfs_register_fs("/test", &vfs1, ESP_VFS_FLAG_DEFAULT, NULL)); int fd = open("/test/1", 0, 0); TEST_ASSERT_NOT_EQUAL(-1, fd); - esp_vfs_t vfs2 = { }; - esp_err_t err = esp_vfs_register_fd_range(&vfs2, NULL, fd, fd + 1); + esp_vfs_fs_ops_t vfs2 = { }; + esp_err_t err = esp_vfs_register_fd_range(&vfs2, ESP_VFS_FLAG_DEFAULT, NULL, fd, fd + 1); close(fd); TEST_ESP_OK(esp_vfs_unregister("/test")); diff --git a/components/vfs/test_apps/main/test_vfs_open.c b/components/vfs/test_apps/main/test_vfs_open.c index 7b888d83b4..fc9815fe96 100644 --- a/components/vfs/test_apps/main/test_vfs_open.c +++ b/components/vfs/test_apps/main/test_vfs_open.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -9,6 +9,7 @@ #include #include #include "esp_vfs.h" +#include "esp_vfs_ops.h" #include "unity.h" static int open_errno_test_open(const char * path, int flags, int mode) @@ -19,10 +20,10 @@ static int open_errno_test_open(const char * path, int flags, int mode) TEST_CASE("esp_vfs_open sets correct errno", "[vfs]") { - esp_vfs_t desc = { + const esp_vfs_fs_ops_t desc = { .open = open_errno_test_open }; - TEST_ESP_OK(esp_vfs_register("/test", &desc, NULL)); + TEST_ESP_OK(esp_vfs_register_fs("/test", &desc, ESP_VFS_FLAG_DEFAULT, NULL)); int fd = open("/test/path", 0, 0); int e = errno; diff --git a/components/vfs/test_apps/main/test_vfs_paths.c b/components/vfs/test_apps/main/test_vfs_paths.c index f7b9234bd8..65fc624fcd 100644 --- a/components/vfs/test_apps/main/test_vfs_paths.c +++ b/components/vfs/test_apps/main/test_vfs_paths.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -12,6 +12,7 @@ #include #include #include "esp_vfs.h" +#include "esp_vfs_ops.h" #include "unity.h" #include "esp_log.h" @@ -68,13 +69,16 @@ static int dummy_closedir(void* ctx, DIR* pdir) /* Initializer for this dummy VFS implementation */ -#define DUMMY_VFS() { \ - .flags = ESP_VFS_FLAG_CONTEXT_PTR, \ - .open_p = dummy_open, \ - .close_p = dummy_close, \ - .opendir_p = dummy_opendir, \ - .closedir_p = dummy_closedir \ - } +static const esp_vfs_dir_ops_t s_dummy_vfs_dir = { + .opendir_p = dummy_opendir, + .closedir_p = dummy_closedir, +}; + +static const esp_vfs_fs_ops_t s_dummy_vfs = { + .open_p = dummy_open, + .close_p = dummy_close, + .dir = &s_dummy_vfs_dir, +}; /* Helper functions to test VFS behavior */ @@ -136,15 +140,13 @@ TEST_CASE("vfs parses paths correctly", "[vfs]") .match_path = "", .called = false }; - esp_vfs_t desc_foo = DUMMY_VFS(); - TEST_ESP_OK( esp_vfs_register("/foo", &desc_foo, &inst_foo) ); + TEST_ESP_OK( esp_vfs_register_fs("/foo", &s_dummy_vfs, ESP_VFS_FLAG_CONTEXT_PTR, &inst_foo) ); dummy_vfs_t inst_foo1 = { .match_path = "", .called = false }; - esp_vfs_t desc_foo1 = DUMMY_VFS(); - TEST_ESP_OK( esp_vfs_register("/foo1", &desc_foo1, &inst_foo1) ); + TEST_ESP_OK( esp_vfs_register_fs("/foo1", &s_dummy_vfs, ESP_VFS_FLAG_CONTEXT_PTR, &inst_foo1) ); inst_foo.match_path = "/file"; test_opened(&inst_foo, "/foo/file"); @@ -171,15 +173,13 @@ TEST_CASE("vfs parses paths correctly", "[vfs]") .match_path = "", .called = false }; - esp_vfs_t desc_foobar = DUMMY_VFS(); - TEST_ESP_OK( esp_vfs_register("/foo/bar", &desc_foobar, &inst_foobar) ); + TEST_ESP_OK( esp_vfs_register_fs("/foo/bar", &s_dummy_vfs, ESP_VFS_FLAG_CONTEXT_PTR, &inst_foobar) ); dummy_vfs_t inst_toplevel = { .match_path = "", .called = false }; - esp_vfs_t desc_toplevel = DUMMY_VFS(); - TEST_ESP_OK( esp_vfs_register("", &desc_toplevel, &inst_toplevel) ); + TEST_ESP_OK( esp_vfs_register_fs("", &s_dummy_vfs, ESP_VFS_FLAG_CONTEXT_PTR, &inst_toplevel) ); inst_foo.match_path = "/bar/file"; inst_foobar.match_path = "/file"; @@ -204,15 +204,13 @@ TEST_CASE("vfs unregisters correct nested mount point", "[vfs]") .match_path = "/file", .called = false }; - esp_vfs_t desc_foobar = DUMMY_VFS(); - TEST_ESP_OK( esp_vfs_register("/foo/bar", &desc_foobar, &inst_foobar) ); + TEST_ESP_OK( esp_vfs_register_fs("/foo/bar", &s_dummy_vfs, ESP_VFS_FLAG_CONTEXT_PTR, &inst_foobar) ); dummy_vfs_t inst_foo = { .match_path = "/bar/file", .called = false }; - esp_vfs_t desc_foo = DUMMY_VFS(); - TEST_ESP_OK( esp_vfs_register("/foo", &desc_foo, &inst_foo) ); + TEST_ESP_OK( esp_vfs_register_fs("/foo", &s_dummy_vfs, ESP_VFS_FLAG_CONTEXT_PTR, &inst_foo) ); /* basic operation */ test_opened(&inst_foobar, "/foo/bar/file"); @@ -228,8 +226,8 @@ TEST_CASE("vfs unregisters correct nested mount point", "[vfs]") /* repeat the above, with the reverse order of registration */ TEST_ESP_OK( esp_vfs_unregister("/foo/bar") ); - TEST_ESP_OK( esp_vfs_register("/foo", &desc_foo, &inst_foo) ); - TEST_ESP_OK( esp_vfs_register("/foo/bar", &desc_foobar, &inst_foobar) ); + TEST_ESP_OK( esp_vfs_register_fs("/foo", &s_dummy_vfs, ESP_VFS_FLAG_CONTEXT_PTR, &inst_foo) ); + TEST_ESP_OK( esp_vfs_register_fs("/foo/bar", &s_dummy_vfs, ESP_VFS_FLAG_CONTEXT_PTR, &inst_foobar) ); test_opened(&inst_foobar, "/foo/bar/file"); test_not_called(&inst_foo, "/foo/bar/file"); TEST_ESP_OK( esp_vfs_unregister("/foo") ); @@ -242,13 +240,12 @@ TEST_CASE("vfs unregisters correct nested mount point", "[vfs]") void test_vfs_register(const char* prefix, bool expect_success, int line) { dummy_vfs_t inst; - esp_vfs_t desc = DUMMY_VFS(); - esp_err_t err = esp_vfs_register(prefix, &desc, &inst); + esp_err_t err = esp_vfs_register_fs(prefix, &s_dummy_vfs, ESP_VFS_FLAG_CONTEXT_PTR, &inst); if (expect_success) { - UNITY_TEST_ASSERT_EQUAL_INT(ESP_OK, err, line, "esp_vfs_register should succeed"); + UNITY_TEST_ASSERT_EQUAL_INT(ESP_OK, err, line, "esp_vfs_register_fs should succeed"); } else { UNITY_TEST_ASSERT_EQUAL_INT(ESP_ERR_INVALID_ARG, - err, line, "esp_vfs_register should fail"); + err, line, "esp_vfs_register_fs should fail"); } if (err == ESP_OK) { TEST_ESP_OK( esp_vfs_unregister(prefix) ); diff --git a/components/vfs/vfs.c b/components/vfs/vfs.c index 71b2ef4bf2..ebf41b80a9 100644 --- a/components/vfs/vfs.c +++ b/components/vfs/vfs.c @@ -520,7 +520,7 @@ esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ct return esp_vfs_register_common(base_path, strlen(base_path), vfs, ctx, NULL); } -esp_err_t esp_vfs_register_fd_range(const esp_vfs_t *vfs, void *ctx, int min_fd, int max_fd) +esp_err_t esp_vfs_register_fd_range(const esp_vfs_fs_ops_t *vfs, int flags, void *ctx, int min_fd, int max_fd) { if (min_fd < 0 || max_fd < 0 || min_fd > MAX_FDS || max_fd > MAX_FDS || min_fd > max_fd) { ESP_LOGD(TAG, "Invalid arguments: esp_vfs_register_fd_range(0x%p, 0x%p, %d, %d)", vfs, ctx, min_fd, max_fd); diff --git a/components/vfs/vfs_eventfd.c b/components/vfs/vfs_eventfd.c index a8ae79d1b9..bae6ecff9e 100644 --- a/components/vfs/vfs_eventfd.c +++ b/components/vfs/vfs_eventfd.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -351,6 +351,22 @@ static int event_close(int fd) return ret; } +#ifdef CONFIG_VFS_SUPPORT_SELECT +static const esp_vfs_select_ops_t s_vfs_eventfd_select = { + .start_select = &event_start_select, + .end_select = &event_end_select, +}; +#endif + +static const esp_vfs_fs_ops_t s_vfs_eventfd = { + .write = &event_write, + .read = &event_read, + .close = &event_close, +#ifdef CONFIG_VFS_SUPPORT_SELECT + .select = &s_vfs_eventfd_select, +#endif +}; + esp_err_t esp_vfs_eventfd_register(const esp_vfs_eventfd_config_t *config) { if (config == NULL || config->max_fds >= MAX_FDS) { @@ -367,17 +383,7 @@ esp_err_t esp_vfs_eventfd_register(const esp_vfs_eventfd_config_t *config) s_events[i].fd = FD_INVALID; } - esp_vfs_t vfs = { - .flags = ESP_VFS_FLAG_DEFAULT, - .write = &event_write, - .close = &event_close, - .read = &event_read, -#ifdef CONFIG_VFS_SUPPORT_SELECT - .start_select = &event_start_select, - .end_select = &event_end_select, -#endif - }; - return esp_vfs_register_with_id(&vfs, NULL, &s_eventfd_vfs_id); + return esp_vfs_register_fs_with_id(&s_vfs_eventfd, ESP_VFS_FLAG_STATIC | ESP_VFS_FLAG_STATIC, NULL, &s_eventfd_vfs_id); } esp_err_t esp_vfs_eventfd_unregister(void) diff --git a/docs/en/api-reference/storage/vfs.rst b/docs/en/api-reference/storage/vfs.rst index f9e0d87ddc..98cd97fd7c 100644 --- a/docs/en/api-reference/storage/vfs.rst +++ b/docs/en/api-reference/storage/vfs.rst @@ -17,10 +17,6 @@ For example, one can mount a FAT filesystem driver at the ``/fat`` prefix and ca FS Registration --------------- -.. note:: - - For previous version of the API (using :cpp:type:`esp_vfs_t`), see documentation for previous release. - To register an FS driver, an application needs to define an instance of the :cpp:type:`esp_vfs_fs_ops_t` structure and populate it with function pointers to FS APIs: .. highlight:: c @@ -79,7 +75,7 @@ In some cases, it might be beneficial or even necessary to pass some context to ssize_t myfs_write(myfs_t* fs, int fd, const void * data, size_t size); - // In definition of esp_vfs_t: + // In definition of esp_vfs_fs_ops_t: .write_p = &myfs_write, // ... other members initialized diff --git a/docs/zh_CN/api-reference/storage/vfs.rst b/docs/zh_CN/api-reference/storage/vfs.rst index 74e19ba49d..523ab264f2 100644 --- a/docs/zh_CN/api-reference/storage/vfs.rst +++ b/docs/zh_CN/api-reference/storage/vfs.rst @@ -17,10 +17,6 @@ VFS 组件支持 C 库函数(如 fopen 和 fprintf 等)与文件系统 (FS) 注册 FS 驱动程序 --------------------- -.. note:: - - 有关先前版本 API(使用 :cpp:type:`esp_vfs_t`)的内容,可以查阅之前发布的文档。 - 如需注册 FS 驱动程序,应用程序首先要定义一个 :cpp:type:`esp_vfs_fs_ops_t` 结构体实例,并用指向 FS API 的函数指针填充它。 .. highlight:: c @@ -79,7 +75,7 @@ VFS 组件支持 C 库函数(如 fopen 和 fprintf 等)与文件系统 (FS) ssize_t myfs_write(myfs_t* fs, int fd, const void * data, size_t size); - // 在 esp_vfs_t 的定义中: + // 在 esp_vfs_fs_ops_t 的定义中: .write_p = &myfs_write, // ... 初始化其他成员 diff --git a/examples/peripherals/usb/device/tusb_composite_msc_serialdevice/main/idf_component.yml b/examples/peripherals/usb/device/tusb_composite_msc_serialdevice/main/idf_component.yml index 03340b8fe0..e1a93a262e 100644 --- a/examples/peripherals/usb/device/tusb_composite_msc_serialdevice/main/idf_component.yml +++ b/examples/peripherals/usb/device/tusb_composite_msc_serialdevice/main/idf_component.yml @@ -1,4 +1,4 @@ ## IDF Component Manager Manifest File dependencies: espressif/esp_tinyusb: - version: "^2.0.0" + version: "^2.0.1~1" diff --git a/examples/peripherals/usb/device/tusb_console/main/idf_component.yml b/examples/peripherals/usb/device/tusb_console/main/idf_component.yml index 03340b8fe0..e1a93a262e 100644 --- a/examples/peripherals/usb/device/tusb_console/main/idf_component.yml +++ b/examples/peripherals/usb/device/tusb_console/main/idf_component.yml @@ -1,4 +1,4 @@ ## IDF Component Manager Manifest File dependencies: espressif/esp_tinyusb: - version: "^2.0.0" + version: "^2.0.1~1" diff --git a/examples/peripherals/usb/device/tusb_serial_device/main/idf_component.yml b/examples/peripherals/usb/device/tusb_serial_device/main/idf_component.yml index 03340b8fe0..e1a93a262e 100644 --- a/examples/peripherals/usb/device/tusb_serial_device/main/idf_component.yml +++ b/examples/peripherals/usb/device/tusb_serial_device/main/idf_component.yml @@ -1,4 +1,4 @@ ## IDF Component Manager Manifest File dependencies: espressif/esp_tinyusb: - version: "^2.0.0" + version: "^2.0.1~1" diff --git a/tools/test_apps/storage/std_filesystem/main/test_status.cpp b/tools/test_apps/storage/std_filesystem/main/test_status.cpp index 7eeb0ce8f7..74d3c45e73 100644 --- a/tools/test_apps/storage/std_filesystem/main/test_status.cpp +++ b/tools/test_apps/storage/std_filesystem/main/test_status.cpp @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -81,15 +81,48 @@ static int test_vfs_closedir(void* ctx, DIR* pdir) TEST_CASE("std::filesystem status functions") { test_vfs_ctx_t test_ctx = {}; - esp_vfs_t desc = {}; - desc.flags = ESP_VFS_FLAG_CONTEXT_PTR; - desc.open_p = test_vfs_open; - desc.stat_p = test_vfs_stat; - desc.opendir_p = test_vfs_opendir; - desc.readdir_p = test_vfs_readdir; - desc.closedir_p = test_vfs_closedir; + esp_vfs_dir_ops_t dir_desc = { + .stat_p = test_vfs_stat, + .link_p = nullptr, + .unlink_p = nullptr, + .rename_p = nullptr, + .opendir_p = test_vfs_opendir, + .readdir_p = test_vfs_readdir, + .readdir_r_p = nullptr, + .telldir_p = nullptr, + .seekdir_p = nullptr, + .closedir_p = test_vfs_closedir, + .mkdir_p = nullptr, + .rmdir_p = nullptr, + .access_p = nullptr, + .truncate_p = nullptr, + .ftruncate_p = nullptr, + .utime_p = nullptr, + }; - REQUIRE(esp_vfs_register("/test", &desc, &test_ctx) == ESP_OK); + esp_vfs_fs_ops_t desc = { + .write_p = nullptr, + .lseek_p = nullptr, + .read_p = nullptr, + .pread_p = nullptr, + .pwrite_p = nullptr, + .open_p = test_vfs_open, + .close_p = nullptr, + .fstat_p = nullptr, + .fcntl_p = nullptr, + .ioctl_p = nullptr, + .fsync_p = nullptr, + .dir = &dir_desc, +#ifdef CONFIG_VFS_SUPPORT_TERMIOS + .termios = nullptr, +#endif + +#if CONFIG_VFS_SUPPORT_SELECT || defined __DOXYGEN__ + .select = nullptr, +#endif + }; + + REQUIRE(esp_vfs_register_fs("/test", &desc, ESP_VFS_FLAG_CONTEXT_PTR, &test_ctx) == ESP_OK); SECTION("Test file exists") { test_ctx.cmp_path = "/file.txt"; From 515975d2bb26fa61f196a1698f70e333757c0c02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Rohl=C3=ADnek?= Date: Wed, 3 Dec 2025 14:09:25 +0100 Subject: [PATCH 3/6] feat(storage/vfs): Make lwip specific API private --- components/lwip/port/esp32xx/vfs_lwip.c | 1 + components/vfs/include/esp_private/socket.h | 34 +++++++++++++++++++++ components/vfs/include/esp_vfs.h | 17 ----------- components/vfs/test_apps/main/test_vfs_fd.c | 1 + components/vfs/vfs.c | 2 ++ components/vfs/vfs_calls.c | 1 + 6 files changed, 39 insertions(+), 17 deletions(-) create mode 100644 components/vfs/include/esp_private/socket.h diff --git a/components/lwip/port/esp32xx/vfs_lwip.c b/components/lwip/port/esp32xx/vfs_lwip.c index d59922d594..c4c42d8220 100644 --- a/components/lwip/port/esp32xx/vfs_lwip.c +++ b/components/lwip/port/esp32xx/vfs_lwip.c @@ -13,6 +13,7 @@ #include #include "esp_attr.h" #include "esp_vfs.h" +#include "esp_private/socket.h" #include "sdkconfig.h" #include "lwip/sockets.h" #include "lwip/sys.h" diff --git a/components/vfs/include/esp_private/socket.h b/components/vfs/include/esp_private/socket.h new file mode 100644 index 0000000000..fad5bf5114 --- /dev/null +++ b/components/vfs/include/esp_private/socket.h @@ -0,0 +1,34 @@ +/* + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include "esp_vfs.h" +#include "esp_err.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Special case function for registering a VFS that uses a method other than + * open() to open new file descriptors from the interval Date: Thu, 11 Dec 2025 08:31:32 +0100 Subject: [PATCH 4/6] feat(storage/vfs): Deprecate legacy API --- components/vfs/include/esp_vfs.h | 7 +++---- components/vfs/vfs.c | 22 +++++++++++----------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/components/vfs/include/esp_vfs.h b/components/vfs/include/esp_vfs.h index d38b091e62..941c4a4fe6 100644 --- a/components/vfs/include/esp_vfs.h +++ b/components/vfs/include/esp_vfs.h @@ -48,7 +48,7 @@ extern "C" { #define ESP_VFS_PATH_MAX 15 /** - * Default value of flags member in esp_vfs_t structure. + * Default value of flags member in esp_vfs_fs_ops_t structure. */ #define ESP_VFS_FLAG_DEFAULT (1 << 0) @@ -70,6 +70,7 @@ extern "C" { /** * @brief VFS definition structure + * @note Prefer using esp_vfs_fs_ops_t with esp_vfs_register_fs*() instead. * * This structure should be filled with pointers to corresponding * FS driver functions. @@ -250,8 +251,6 @@ typedef struct #endif // CONFIG_VFS_SUPPORT_SELECT || defined __DOXYGEN__ } esp_vfs_t; - - /** * Register a virtual filesystem for given path prefix. * @@ -293,7 +292,7 @@ esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ct * registered, ESP_ERR_INVALID_ARG if the file descriptor boundaries * are incorrect. */ -esp_err_t esp_vfs_register_with_id(const esp_vfs_t *vfs, void *ctx, esp_vfs_id_t *vfs_id); +esp_err_t esp_vfs_register_with_id(const esp_vfs_t *vfs, void *ctx, esp_vfs_id_t *vfs_id) __attribute__((deprecated("Use esp_vfs_register_fs_with_id() instead"))); /** * Unregister a virtual filesystem for given path prefix diff --git a/components/vfs/vfs.c b/components/vfs/vfs.c index 534839e325..b1e6bf202b 100644 --- a/components/vfs/vfs.c +++ b/components/vfs/vfs.c @@ -522,6 +522,16 @@ esp_err_t esp_vfs_register(const char* base_path, const esp_vfs_t* vfs, void* ct return esp_vfs_register_common(base_path, strlen(base_path), vfs, ctx, NULL); } +esp_err_t esp_vfs_register_with_id(const esp_vfs_t *vfs, void *ctx, esp_vfs_id_t *vfs_id) +{ + if (vfs_id == NULL) { + return ESP_ERR_INVALID_ARG; + } + + *vfs_id = -1; + return esp_vfs_register_common("", LEN_PATH_PREFIX_IGNORED, vfs, ctx, vfs_id); +} + esp_err_t esp_vfs_register_fd_range(const esp_vfs_fs_ops_t *vfs, int flags, void *ctx, int min_fd, int max_fd) { if (min_fd < 0 || max_fd < 0 || min_fd > MAX_FDS || max_fd > MAX_FDS || min_fd > max_fd) { @@ -530,7 +540,7 @@ esp_err_t esp_vfs_register_fd_range(const esp_vfs_fs_ops_t *vfs, int flags, void } int index = 0; - esp_err_t ret = esp_vfs_register_common("", LEN_PATH_PREFIX_IGNORED, vfs, ctx, &index); + esp_err_t ret = esp_vfs_register_fs_common(NULL, vfs, flags, ctx, &index); if (ret == ESP_OK) { _lock_acquire(&s_fd_table_lock); @@ -569,16 +579,6 @@ esp_err_t esp_vfs_register_fs_with_id(const esp_vfs_fs_ops_t *vfs, int flags, vo return esp_vfs_register_fs_common(NULL, vfs, flags, ctx, vfs_id); } -esp_err_t esp_vfs_register_with_id(const esp_vfs_t *vfs, void *ctx, esp_vfs_id_t *vfs_id) -{ - if (vfs_id == NULL) { - return ESP_ERR_INVALID_ARG; - } - - *vfs_id = -1; - return esp_vfs_register_common("", LEN_PATH_PREFIX_IGNORED, vfs, ctx, vfs_id); -} - esp_err_t esp_vfs_unregister_with_id(esp_vfs_id_t vfs_id) { if (vfs_id < 0 || vfs_id >= VFS_MAX_COUNT || s_vfs[vfs_id] == NULL) { From 18c5aa7b802529726bce03136075fa7ee8e33d86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Rohl=C3=ADnek?= Date: Wed, 3 Dec 2025 13:38:36 +0100 Subject: [PATCH 5/6] docs(storage/vfs): Add migration guide for breaking changes --- docs/en/migration-guides/release-6.x/6.0/storage.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/en/migration-guides/release-6.x/6.0/storage.rst b/docs/en/migration-guides/release-6.x/6.0/storage.rst index c62235749b..281e09efee 100644 --- a/docs/en/migration-guides/release-6.x/6.0/storage.rst +++ b/docs/en/migration-guides/release-6.x/6.0/storage.rst @@ -14,6 +14,8 @@ VFS - Deleted deprecated UART-VFS functions (``esp_vfs_dev_uart_*``) located in the ``vfs`` component. Please use API from UART driver instead: ``uart_vfs_dev_*``. - Deleted deprecated USB-Serial-JTAG-VFS functions (``esp_vfs_dev_usb_serial_jtag_*``) located in the ``vfs`` component. Please use API from USB-Serial-JTAG driver instead: ``usb_serial_jtag_vfs_*``. +- ``esp_vfs_register_fd_range`` is now considered private and its signature was changed to match the new VFS API style. Projects that still rely on this internal helper must include ``esp_private/socket.h`` and should be aware that the API may change without notice. +- Legacy VFS APIs (such as ``esp_vfs_register``) that operate on ``esp_vfs_t`` instead of ``esp_vfs_fs_ops_t`` are deprecated and will be removed in the next major release. Switch to the new ``esp_vfs_fs_ops_t``-based APIs. ``esp_vfs_console`` From cd8351a620d67b9c1ee3d5b87190664727271b4e Mon Sep 17 00:00:00 2001 From: Zhang Shuxian Date: Tue, 16 Dec 2025 15:18:27 +0800 Subject: [PATCH 6/6] docs: Update CN translation --- docs/zh_CN/migration-guides/release-6.x/6.0/storage.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/zh_CN/migration-guides/release-6.x/6.0/storage.rst b/docs/zh_CN/migration-guides/release-6.x/6.0/storage.rst index 89853f9718..a791b04dc5 100644 --- a/docs/zh_CN/migration-guides/release-6.x/6.0/storage.rst +++ b/docs/zh_CN/migration-guides/release-6.x/6.0/storage.rst @@ -14,6 +14,8 @@ VFS - 位于 ``vfs`` 组件中已弃用的 UART-VFS 函数 ``esp_vfs_dev_uart_*`` 现已删除,请改用 UART 驱动中的 API:``uart_vfs_dev_*``。 - 位于 ``vfs`` 组件中已弃用的 USB-Serial-JTAG-VFS 函数 ``esp_vfs_dev_usb_serial_jtag_*`` 现已删除,请改用 USB-Serial-JTAG 驱动中的 API:``usb_serial_jtag_vfs_*``。 +- ``esp_vfs_register_fd_range`` 现被视为私有接口,其函数签名已调整为匹配新的 VFS API 风格。仍依赖此内部辅助函数的项目需包含 ``esp_private/socket.h`` 头文件,并请注意该 API 可能在不另行通知的情况下发生变更。 +- 基于 ``esp_vfs_t`` 而非 ``esp_vfs_fs_ops_t`` 的传统 VFS API(如 ``esp_vfs_register``)已弃用,并将在下一个主版本中移除。请迁移至基于 ``esp_vfs_fs_ops_t`` 的新 API。 ``esp_vfs_console``