feat(vfs): Add function to dump VFS file descriptors

This commit is contained in:
sonika.rathi 2024-02-21 09:49:50 +01:00 committed by BOT
parent 131dfbef9b
commit 0e5793b270
3 changed files with 86 additions and 2 deletions

View File

@ -469,6 +469,25 @@ ssize_t esp_vfs_pread(int fd, void *dst, size_t size, off_t offset);
*/
ssize_t esp_vfs_pwrite(int fd, const void *src, size_t size, off_t offset);
/**
*
* @brief Dump the existing VFS FDs data to FILE* fp
*
* Dump the FDs in the format:
@verbatim
<VFS Path Prefix>-<FD seen by App>-<FD seen by driver>
where:
VFS Path Prefix : file prefix used in the esp_vfs_register call
FD seen by App : file descriptor returned by the vfs to the application for the path prefix
FD seen by driver : file descriptor used by the driver for the same file prefix.
@endverbatim
*
* @param fp File descriptor where data will be dumped
*/
void esp_vfs_dump_fds(FILE *fp);
#ifdef __cplusplus
} // extern "C"
#endif

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -18,6 +18,10 @@
#include "esp_log.h"
#include "test_utils.h"
#include "ccomp_timer.h"
#include "driver/uart.h"
#include "driver/uart_vfs.h"
#include "lwip/sockets.h"
#include "lwip/netdb.h"
#define VFS_PREF1 "/vfs1"
#define VFS_PREF2 "/vfs2"
@ -289,3 +293,44 @@ TEST_CASE("esp_vfs_register_fd_range checks for overlap", "[vfs]")
TEST_ESP_OK(esp_vfs_unregister("/test"));
TEST_ESP_ERR(ESP_ERR_INVALID_ARG, err);
}
static void socket_init(int *socket_fd)
{
const struct addrinfo hints = {
.ai_family = AF_INET,
.ai_socktype = SOCK_DGRAM,
};
struct addrinfo *res;
int err;
err = getaddrinfo("localhost", "80", &hints, &res);
TEST_ASSERT_EQUAL(err, 0);
TEST_ASSERT_NOT_NULL(res);
*socket_fd = socket(res->ai_family, res->ai_socktype, 0);
TEST_ASSERT(*socket_fd >= 0);
freeaddrinfo(res);
}
TEST_CASE("esp_vfs_dump_fds displays all registered fds in vfs", "[vfs]")
{
int uart_fd0, uart_fd1, uart_fd2;
int socket_fd=-1;
uart_fd0 = open("/dev/uart/0", O_RDWR);
TEST_ASSERT_NOT_EQUAL_MESSAGE(uart_fd0, -1, "Cannot open UART");
uart_fd1 = open("/dev/uart/1", O_RDWR);
TEST_ASSERT_NOT_EQUAL_MESSAGE(uart_fd1, -1, "Cannot open UART");
uart_fd2 = open("/dev/uart/1", O_RDWR);
TEST_ASSERT_NOT_EQUAL_MESSAGE(uart_fd2, -1, "Cannot open UART");
test_case_uses_tcpip();
socket_init(&socket_fd);
esp_vfs_dump_fds(stdout);
close(uart_fd0);
close(uart_fd1);
close(uart_fd2);
close(socket_fd);
}

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -270,6 +270,26 @@ esp_err_t esp_vfs_unregister_fd(esp_vfs_id_t vfs_id, int fd)
return ret;
}
void esp_vfs_dump_fds(FILE *fp)
{
const vfs_entry_t* vfs;
fprintf(fp, "------------------------------------------------------\n");
fprintf(fp, "<VFS Path Prefix>-<FD seen by App>-<FD seen by driver>\n");
fprintf(fp, "------------------------------------------------------\n");
_lock_acquire(&s_fd_table_lock);
for (int index = 0; index < MAX_FDS; index++) {
if (s_fd_table[index].vfs_index != -1) {
vfs = s_vfs[s_fd_table[index].vfs_index];
if (strcmp(vfs->path_prefix, "")) {
fprintf(fp, "(%s) - 0x%x - 0x%x\n", vfs->path_prefix, index, s_fd_table[index].local_fd);
} else {
fprintf(fp, "(socket) - 0x%x - 0x%x\n", index, s_fd_table[index].local_fd);
}
}
}
_lock_release(&s_fd_table_lock);
}
/*
* Set ESP_VFS_FLAG_READONLY_FS read-only flag for a registered virtual filesystem
* for given path prefix. Should be only called from the esp_vfs_*filesystem* register