feat(storage/vfs): Remove old API usage

This commit is contained in:
Tomáš Rohlínek
2025-12-03 11:46:55 +01:00
committed by Zhang Shuxian
parent 4814f06283
commit 8c9d62de98
21 changed files with 154 additions and 123 deletions
@@ -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;
}
@@ -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;
}
@@ -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);
@@ -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);
@@ -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;
@@ -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;
@@ -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);
+1 -1
View File
@@ -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)
{
+30 -17
View File
@@ -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));
}
+1 -1
View File
@@ -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
+12 -16
View File
@@ -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, &param) );
TEST_ESP_OK( esp_vfs_register(VFS_PREF2, &desc, &param) );
TEST_ESP_OK( esp_vfs_register_fs(VFS_PREF1, &desc, ESP_VFS_FLAG_CONTEXT_PTR, &param) );
TEST_ESP_OK( esp_vfs_register_fs(VFS_PREF2, &desc, ESP_VFS_FLAG_CONTEXT_PTR, &param) );
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"));
@@ -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 <fcntl.h>
#include <errno.h>
#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;
+23 -26
View File
@@ -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 <sys/fcntl.h>
#include <dirent.h>
#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) );
+1 -1
View File
@@ -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);
+18 -12
View File
@@ -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)
+1 -5
View File
@@ -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
+1 -5
View File
@@ -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,
// ... 初始化其他成员
@@ -1,4 +1,4 @@
## IDF Component Manager Manifest File
dependencies:
espressif/esp_tinyusb:
version: "^2.0.0"
version: "^2.0.1~1"
@@ -1,4 +1,4 @@
## IDF Component Manager Manifest File
dependencies:
espressif/esp_tinyusb:
version: "^2.0.0"
version: "^2.0.1~1"
@@ -1,4 +1,4 @@
## IDF Component Manager Manifest File
dependencies:
espressif/esp_tinyusb:
version: "^2.0.0"
version: "^2.0.1~1"
@@ -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";