Skip to content

Commit

Permalink
[libc] Enable linux directory entries syscalls in riscv64
Browse files Browse the repository at this point in the history
This patch updates the struct dirent to be on par with glibc (by adding
a missing d_type member) and update the readdir call to use SYS_getdents64
instead of SYS_getdents.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D147738
  • Loading branch information
mikhailramalho committed May 4, 2023
1 parent 8052c1e commit 4c9c1a4
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 6 deletions.
6 changes: 6 additions & 0 deletions libc/config/linux/riscv64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,12 @@ if(LLVM_LIBC_FULL_BUILD)
# assert.h entrypoints
libc.src.assert.__assert_fail

# dirent.h entrypoints
libc.src.dirent.closedir
libc.src.dirent.dirfd
libc.src.dirent.opendir
libc.src.dirent.readdir

# network.h entrypoints
libc.src.network.htonl
libc.src.network.htons
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/riscv64/headers.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
set(TARGET_PUBLIC_HEADERS
libc.include.assert
libc.include.ctype
libc.include.dirent
libc.include.errno
libc.include.fcntl
libc.include.fenv
Expand Down
1 change: 1 addition & 0 deletions libc/include/llvm-libc-types/struct_dirent.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct dirent {
off_t d_off;
unsigned short d_reclen;
#endif
unsigned char d_type;
// The user code should use strlen to determine actual the size of d_name.
// Likewise, it is incorrect and prohibited by the POSIX standard to detemine
// the size of struct dirent type using sizeof. The size should be got using
Expand Down
2 changes: 0 additions & 2 deletions libc/src/__support/File/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ add_object_library(
HDRS
file.h
DEPENDS

libc.src.__support.CPP.new
libc.src.__support.CPP.span
libc.src.__support.threads.mutex
Expand All @@ -37,7 +36,6 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS}_file.cpp)
${LIBC_TARGET_OS}_file.cpp
DEPENDS
.file

libc.include.fcntl
libc.include.stdio
libc.include.sys_syscall
Expand Down
2 changes: 0 additions & 2 deletions libc/src/__support/File/dir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include "src/__support/error_or.h"
#include "src/errno/libc_errno.h" // For error macros

#include <stdlib.h>

namespace __llvm_libc {

ErrorOr<Dir *> Dir::open(const char *path) {
Expand Down
9 changes: 7 additions & 2 deletions libc/src/__support/File/linux_dir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ ErrorOr<int> platform_opendir(const char *name) {
}

ErrorOr<size_t> platform_fetch_dirents(int fd, cpp::span<uint8_t> buffer) {
long size =
__llvm_libc::syscall_impl(SYS_getdents, fd, buffer.data(), buffer.size());
#ifdef SYS_getdents64
long size = __llvm_libc::syscall_impl(SYS_getdents64, fd, buffer.data(),
buffer.size());
#else
#error "getdents64 syscalls not available to perform a fetch dirents operation."
#endif

if (size < 0) {
return __llvm_libc::Error(static_cast<int>(-size));
}
Expand Down

0 comments on commit 4c9c1a4

Please sign in to comment.