Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

posix: fs: refactor posix_file_system_r #83395

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
46 changes: 3 additions & 43 deletions lib/posix/options/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L

#include "fs_priv.h"

#include <errno.h>
#include <zephyr/kernel.h>
#include <limits.h>
Expand All @@ -21,15 +24,6 @@ int zvfs_fstat(int fd, struct stat *buf);

BUILD_ASSERT(PATH_MAX >= MAX_FILE_NAME, "PATH_MAX is less than MAX_FILE_NAME");

struct posix_fs_desc {
union {
struct fs_file_t file;
struct fs_dir_t dir;
};
bool is_dir;
bool used;
};

static struct posix_fs_desc desc_array[CONFIG_POSIX_OPEN_MAX];

static struct fs_dirent fdirent;
Expand Down Expand Up @@ -337,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.
*
Expand Down
23 changes: 23 additions & 0 deletions lib/posix/options/fs_priv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef ZEPHYR_LIB_POSIX_OPTIONS_FS_PRIV_H_
#define ZEPHYR_LIB_POSIX_OPTIONS_FS_PRIV_H_

#include <stdbool.h>

#include <zephyr/fs/fs.h>

struct posix_fs_desc {
union {
struct fs_file_t file;
struct fs_dir_t dir;
};
bool is_dir: 1;
bool used: 1;
};

#endif
2 changes: 2 additions & 0 deletions tests/posix/fs/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ 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
CONFIG_EVENTFD=n
4 changes: 2 additions & 2 deletions tests/posix/fs/src/test_fs_dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ static int test_mkdir(void)
static struct dirent *readdir_wrap(DIR *dirp, bool thread_safe)
{
if (thread_safe) {
struct dirent *entry = NULL;
struct dirent entry;
struct dirent *result = NULL;

zassert_ok(readdir_r(dirp, entry, &result));
zassert_ok(readdir_r(dirp, &entry, &result));

return result;
} else {
Expand Down
Loading