From bc5aff3582248e03a2861919a68a5137a9c501e5 Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Thu, 26 Dec 2024 05:17:08 -0500 Subject: [PATCH] posix: options: fs: separate file_system_r to its own file Move the functionality of POSIX_FILE_SYSTEM_R to its own compilation unit and remove the unnecessary dependency on POSIX_FILE_SYSTEM. Signed-off-by: Chris Friedt --- lib/posix/options/CMakeLists.txt | 4 ++ lib/posix/options/Kconfig | 1 + lib/posix/options/Kconfig.file_system_r | 14 +++++++ lib/posix/options/Kconfig.fs | 9 ---- lib/posix/options/Kconfig.pthread | 2 +- lib/posix/options/file_system_r.c | 56 +++++++++++++++++++++++++ lib/posix/options/fs.c | 34 --------------- tests/posix/fs/prj.conf | 1 + 8 files changed, 77 insertions(+), 44 deletions(-) create mode 100644 lib/posix/options/Kconfig.file_system_r create mode 100644 lib/posix/options/file_system_r.c diff --git a/lib/posix/options/CMakeLists.txt b/lib/posix/options/CMakeLists.txt index 4be0e8b058c9..e7b10af8ce7c 100644 --- a/lib/posix/options/CMakeLists.txt +++ b/lib/posix/options/CMakeLists.txt @@ -71,6 +71,10 @@ if (NOT CONFIG_TC_PROVIDES_POSIX_FILE_SYSTEM) zephyr_library_sources_ifdef(CONFIG_POSIX_FILE_SYSTEM fs.c) endif() +if (NOT CONFIG_TC_PROVIDES_POSIX_FILE_SYSTEM_R) + zephyr_library_sources_ifdef(CONFIG_POSIX_FILE_SYSTEM_R file_system_r.c) +endif() + zephyr_library_sources_ifdef(CONFIG_POSIX_FSYNC fsync.c) zephyr_library_sources_ifdef(CONFIG_POSIX_MEMLOCK mlockall.c) zephyr_library_sources_ifdef(CONFIG_POSIX_MEMLOCK_RANGE mlock.c) diff --git a/lib/posix/options/Kconfig b/lib/posix/options/Kconfig index f453f5295d02..c7e60d9e3238 100644 --- a/lib/posix/options/Kconfig +++ b/lib/posix/options/Kconfig @@ -14,6 +14,7 @@ rsource "Kconfig.c_lang_r" rsource "Kconfig.c_lib_ext" rsource "Kconfig.device_io" rsource "Kconfig.fd_mgmt" +rsource "Kconfig.file_system_r" rsource "Kconfig.fs" rsource "Kconfig.mem" rsource "Kconfig.mqueue" diff --git a/lib/posix/options/Kconfig.file_system_r b/lib/posix/options/Kconfig.file_system_r new file mode 100644 index 000000000000..f6f66f4f7f23 --- /dev/null +++ b/lib/posix/options/Kconfig.file_system_r @@ -0,0 +1,14 @@ +# Copyright (c) 2018 Intel Corporation +# +# SPDX-License-Identifier: Apache-2.0 + +config POSIX_FILE_SYSTEM_R + bool "Thread-Safe File System" + select FILE_SYSTEM + select FDTABLE + help + Select 'y' here and Zephyr will provide an implementation of the POSIX_FILE_SYSTEM_R + Option Group, consisting of readdir_r(). + + For more informnation, please see + https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_subprofiles.html diff --git a/lib/posix/options/Kconfig.fs b/lib/posix/options/Kconfig.fs index ce1acb118e0f..bc492e5dc7a0 100644 --- a/lib/posix/options/Kconfig.fs +++ b/lib/posix/options/Kconfig.fs @@ -17,13 +17,4 @@ config POSIX_FILE_SYSTEM_ALIAS_FSTAT help When selected via Kconfig, Zephyr will provide an alias for fstat() as _fstat(). -config POSIX_FILE_SYSTEM_R - bool "Thread-Safe File System" - help - Select 'y' here and Zephyr will provide an implementation of the POSIX_FILE_SYSTEM_R - Option Group, consisting of readdir_r(). - - For more informnation, please see - https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_subprofiles.html - endif # POSIX_FILE_SYSTEM diff --git a/lib/posix/options/Kconfig.pthread b/lib/posix/options/Kconfig.pthread index f821d9defebb..4eef794039a3 100644 --- a/lib/posix/options/Kconfig.pthread +++ b/lib/posix/options/Kconfig.pthread @@ -156,7 +156,7 @@ config POSIX_THREAD_PRIO_PROTECT config POSIX_THREAD_SAFE_FUNCTIONS bool "POSIX thread-safe functions" - select POSIX_FILE_SYSTEM_R if POSIX_FILE_SYSTEM + select POSIX_FILE_SYSTEM_R select POSIX_C_LANG_SUPPORT_R help Select 'y' here to enable POSIX thread-safe functions including asctime_r(), ctime_r(), diff --git a/lib/posix/options/file_system_r.c b/lib/posix/options/file_system_r.c new file mode 100644 index 000000000000..1e967764ef97 --- /dev/null +++ b/lib/posix/options/file_system_r.c @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2024 Tenstorrent AI ULC + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#undef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200809L + +#include "fs_priv.h" + +#include +#include +#include + +#include +#include +#include + +int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) +{ + int rc; + struct fs_dirent de; + struct posix_fs_desc *const ptr = dirp; + + if (result == NULL) { + return EINVAL; + } + + if (entry == NULL) { + *result = NULL; + return EINVAL; + } + + if (dirp == NULL) { + *result = NULL; + return EBADF; + } + + rc = fs_readdir(&ptr->dir, &de); + if (rc < 0) { + *result = NULL; + return -rc; + } + + strncpy(entry->d_name, de.name, MIN(sizeof(entry->d_name), sizeof(de.name))); + entry->d_name[sizeof(entry->d_name) - 1] = '\0'; + + if (entry->d_name[0] == '\0') { + *result = NULL; + return 0; + } + + *result = entry; + return 0; +} diff --git a/lib/posix/options/fs.c b/lib/posix/options/fs.c index f58ec0cb97c9..45028248608f 100644 --- a/lib/posix/options/fs.c +++ b/lib/posix/options/fs.c @@ -331,40 +331,6 @@ struct dirent *readdir(DIR *dirp) return &pdirent; } -#ifdef CONFIG_POSIX_FILE_SYSTEM_R -int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) -{ - struct dirent *dir; - - errno = 0; - - dir = readdir(dirp); - if (dir == NULL) { - int error = errno; - - if (error != 0) { - if (result != NULL) { - *result = NULL; - } - - return 0; - } else { - return error; - } - } - - if (entry != NULL) { - memcpy(entry, dir, sizeof(struct dirent)); - } - - if (result != NULL) { - *result = entry; - } - - return 0; -} -#endif /* CONFIG_POSIX_FILE_SYSTEM_R */ - /** * @brief Rename a file. * diff --git a/tests/posix/fs/prj.conf b/tests/posix/fs/prj.conf index 7fb9ec9aed06..e1501c697ae1 100644 --- a/tests/posix/fs/prj.conf +++ b/tests/posix/fs/prj.conf @@ -3,6 +3,7 @@ CONFIG_LOG=y CONFIG_FAT_FILESYSTEM_ELM=y CONFIG_POSIX_API=y CONFIG_POSIX_FILE_SYSTEM=y +CONFIG_POSIX_FILE_SYSTEM_R=y CONFIG_ZTEST=y CONFIG_MAIN_STACK_SIZE=4096 CONFIG_ZTEST_STACK_SIZE=2048