1 //===--- Linux implementation of the Dir helpers --------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12 #include "src/__support/error_or.h"
14 #include <fcntl.h> // For open flags
15 #include <sys/syscall.h> // For syscall numbers
17 namespace __llvm_libc
{
19 ErrorOr
<int> platform_opendir(const char *name
) {
20 int open_flags
= O_RDONLY
| O_DIRECTORY
| O_CLOEXEC
;
22 int fd
= __llvm_libc::syscall_impl(SYS_open
, name
, open_flags
);
23 #elif defined(SYS_openat)
24 int fd
= __llvm_libc::syscall_impl(SYS_openat
, AT_FDCWD
, name
, open_flags
);
27 "SYS_open and SYS_openat syscalls not available to perform an open operation."
31 return __llvm_libc::Error(-fd
);
36 ErrorOr
<size_t> platform_fetch_dirents(int fd
, cpp::span
<uint8_t> buffer
) {
38 long size
= __llvm_libc::syscall_impl(SYS_getdents64
, fd
, buffer
.data(),
41 #error "getdents64 syscalls not available to perform a fetch dirents operation."
45 return __llvm_libc::Error(static_cast<int>(-size
));
50 int platform_closedir(int fd
) {
51 long ret
= __llvm_libc::syscall_impl(SYS_close
, fd
);
53 return static_cast<int>(-ret
);
58 } // namespace __llvm_libc