Skip to content

Commit bc5aff3

Browse files
cfriedtkartben
authored andcommitted
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 <[email protected]>
1 parent fc860e1 commit bc5aff3

File tree

8 files changed

+77
-44
lines changed

8 files changed

+77
-44
lines changed

lib/posix/options/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ if (NOT CONFIG_TC_PROVIDES_POSIX_FILE_SYSTEM)
7171
zephyr_library_sources_ifdef(CONFIG_POSIX_FILE_SYSTEM fs.c)
7272
endif()
7373

74+
if (NOT CONFIG_TC_PROVIDES_POSIX_FILE_SYSTEM_R)
75+
zephyr_library_sources_ifdef(CONFIG_POSIX_FILE_SYSTEM_R file_system_r.c)
76+
endif()
77+
7478
zephyr_library_sources_ifdef(CONFIG_POSIX_FSYNC fsync.c)
7579
zephyr_library_sources_ifdef(CONFIG_POSIX_MEMLOCK mlockall.c)
7680
zephyr_library_sources_ifdef(CONFIG_POSIX_MEMLOCK_RANGE mlock.c)

lib/posix/options/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ rsource "Kconfig.c_lang_r"
1414
rsource "Kconfig.c_lib_ext"
1515
rsource "Kconfig.device_io"
1616
rsource "Kconfig.fd_mgmt"
17+
rsource "Kconfig.file_system_r"
1718
rsource "Kconfig.fs"
1819
rsource "Kconfig.mem"
1920
rsource "Kconfig.mqueue"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright (c) 2018 Intel Corporation
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
config POSIX_FILE_SYSTEM_R
6+
bool "Thread-Safe File System"
7+
select FILE_SYSTEM
8+
select FDTABLE
9+
help
10+
Select 'y' here and Zephyr will provide an implementation of the POSIX_FILE_SYSTEM_R
11+
Option Group, consisting of readdir_r().
12+
13+
For more informnation, please see
14+
https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_subprofiles.html

lib/posix/options/Kconfig.fs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,4 @@ config POSIX_FILE_SYSTEM_ALIAS_FSTAT
1717
help
1818
When selected via Kconfig, Zephyr will provide an alias for fstat() as _fstat().
1919

20-
config POSIX_FILE_SYSTEM_R
21-
bool "Thread-Safe File System"
22-
help
23-
Select 'y' here and Zephyr will provide an implementation of the POSIX_FILE_SYSTEM_R
24-
Option Group, consisting of readdir_r().
25-
26-
For more informnation, please see
27-
https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_subprofiles.html
28-
2920
endif # POSIX_FILE_SYSTEM

lib/posix/options/Kconfig.pthread

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ config POSIX_THREAD_PRIO_PROTECT
156156

157157
config POSIX_THREAD_SAFE_FUNCTIONS
158158
bool "POSIX thread-safe functions"
159-
select POSIX_FILE_SYSTEM_R if POSIX_FILE_SYSTEM
159+
select POSIX_FILE_SYSTEM_R
160160
select POSIX_C_LANG_SUPPORT_R
161161
help
162162
Select 'y' here to enable POSIX thread-safe functions including asctime_r(), ctime_r(),

lib/posix/options/file_system_r.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2024 Tenstorrent AI ULC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#undef _POSIX_C_SOURCE
8+
#define _POSIX_C_SOURCE 200809L
9+
10+
#include "fs_priv.h"
11+
12+
#include <errno.h>
13+
#include <limits.h>
14+
#include <string.h>
15+
16+
#include <zephyr/fs/fs.h>
17+
#include <zephyr/posix/posix_features.h>
18+
#include <zephyr/posix/dirent.h>
19+
20+
int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
21+
{
22+
int rc;
23+
struct fs_dirent de;
24+
struct posix_fs_desc *const ptr = dirp;
25+
26+
if (result == NULL) {
27+
return EINVAL;
28+
}
29+
30+
if (entry == NULL) {
31+
*result = NULL;
32+
return EINVAL;
33+
}
34+
35+
if (dirp == NULL) {
36+
*result = NULL;
37+
return EBADF;
38+
}
39+
40+
rc = fs_readdir(&ptr->dir, &de);
41+
if (rc < 0) {
42+
*result = NULL;
43+
return -rc;
44+
}
45+
46+
strncpy(entry->d_name, de.name, MIN(sizeof(entry->d_name), sizeof(de.name)));
47+
entry->d_name[sizeof(entry->d_name) - 1] = '\0';
48+
49+
if (entry->d_name[0] == '\0') {
50+
*result = NULL;
51+
return 0;
52+
}
53+
54+
*result = entry;
55+
return 0;
56+
}

lib/posix/options/fs.c

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -331,40 +331,6 @@ struct dirent *readdir(DIR *dirp)
331331
return &pdirent;
332332
}
333333

334-
#ifdef CONFIG_POSIX_FILE_SYSTEM_R
335-
int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
336-
{
337-
struct dirent *dir;
338-
339-
errno = 0;
340-
341-
dir = readdir(dirp);
342-
if (dir == NULL) {
343-
int error = errno;
344-
345-
if (error != 0) {
346-
if (result != NULL) {
347-
*result = NULL;
348-
}
349-
350-
return 0;
351-
} else {
352-
return error;
353-
}
354-
}
355-
356-
if (entry != NULL) {
357-
memcpy(entry, dir, sizeof(struct dirent));
358-
}
359-
360-
if (result != NULL) {
361-
*result = entry;
362-
}
363-
364-
return 0;
365-
}
366-
#endif /* CONFIG_POSIX_FILE_SYSTEM_R */
367-
368334
/**
369335
* @brief Rename a file.
370336
*

tests/posix/fs/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CONFIG_LOG=y
33
CONFIG_FAT_FILESYSTEM_ELM=y
44
CONFIG_POSIX_API=y
55
CONFIG_POSIX_FILE_SYSTEM=y
6+
CONFIG_POSIX_FILE_SYSTEM_R=y
67
CONFIG_ZTEST=y
78
CONFIG_MAIN_STACK_SIZE=4096
89
CONFIG_ZTEST_STACK_SIZE=2048

0 commit comments

Comments
 (0)