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 //===----------------------------------------------------------------------===//
9 #include "src/__support/File/dir.h"
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 LIBC_NAMESPACE
{
19 ErrorOr
<int> platform_opendir(const char *name
) {
20 int open_flags
= O_RDONLY
| O_DIRECTORY
| O_CLOEXEC
;
22 int fd
= LIBC_NAMESPACE::syscall_impl
<int>(SYS_open
, name
, open_flags
);
23 #elif defined(SYS_openat)
25 LIBC_NAMESPACE::syscall_impl
<int>(SYS_openat
, AT_FDCWD
, name
, open_flags
);
28 "SYS_open and SYS_openat syscalls not available to perform an open operation."
32 return LIBC_NAMESPACE::Error(-fd
);
37 ErrorOr
<size_t> platform_fetch_dirents(int fd
, cpp::span
<uint8_t> buffer
) {
39 long size
= LIBC_NAMESPACE::syscall_impl
<long>(SYS_getdents64
, fd
,
40 buffer
.data(), buffer
.size());
42 #error "getdents64 syscalls not available to perform a fetch dirents operation."
46 return LIBC_NAMESPACE::Error(static_cast<int>(-size
));
51 int platform_closedir(int fd
) {
52 int ret
= LIBC_NAMESPACE::syscall_impl
<int>(SYS_close
, fd
);
54 return static_cast<int>(-ret
);
59 } // namespace LIBC_NAMESPACE