Skip to content

Commit

Permalink
posix: options: fs: separate file_system_r to its own file
Browse files Browse the repository at this point in the history
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 <cfriedt@tenstorrent.com>
  • Loading branch information
cfriedt committed Dec 26, 2024
1 parent b2d2b55 commit db11023
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/libc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ config PICOLIBC
select LIBC_ERRNO if THREAD_LOCAL_STORAGE
select NEED_LIBC_MEM_PARTITION
select TC_PROVIDES_POSIX_C_LANG_SUPPORT_R
select TC_PROVIDES_POSIX_FILE_LOCKING
imply COMMON_LIBC_MALLOC
depends on PICOLIBC_SUPPORTED
help
Expand Down
4 changes: 4 additions & 0 deletions lib/posix/options/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/posix/options/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions lib/posix/options/Kconfig.file_system_r
Original file line number Diff line number Diff line change
@@ -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
9 changes: 0 additions & 9 deletions lib/posix/options/Kconfig.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/posix/options/Kconfig.pthread
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
56 changes: 56 additions & 0 deletions lib/posix/options/file_system_r.c
Original file line number Diff line number Diff line change
@@ -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 <errno.h>
#include <limits.h>
#include <string.h>

#include <zephyr/fs/fs.h>
#include <zephyr/posix/posix_features.h>
#include <zephyr/posix/dirent.h>

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;
}

0 comments on commit db11023

Please sign in to comment.