[NFC][Py Reformat] Added more commits to .git-blame-ignore-revs
[llvm-project.git] / libc / src / __support / File / linux_dir.cpp
blobaae565ffb337a587bb24c469e6c225c3d83c6dbb
1 //===--- Linux implementation of the Dir helpers --------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "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 __llvm_libc {
19 ErrorOr<int> platform_opendir(const char *name) {
20 int open_flags = O_RDONLY | O_DIRECTORY | O_CLOEXEC;
21 #ifdef SYS_open
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);
25 #else
26 #error \
27 "SYS_open and SYS_openat syscalls not available to perform an open operation."
28 #endif
30 if (fd < 0) {
31 return __llvm_libc::Error(-fd);
33 return fd;
36 ErrorOr<size_t> platform_fetch_dirents(int fd, cpp::span<uint8_t> buffer) {
37 #ifdef SYS_getdents64
38 long size = __llvm_libc::syscall_impl(SYS_getdents64, fd, buffer.data(),
39 buffer.size());
40 #else
41 #error "getdents64 syscalls not available to perform a fetch dirents operation."
42 #endif
44 if (size < 0) {
45 return __llvm_libc::Error(static_cast<int>(-size));
47 return size;
50 int platform_closedir(int fd) {
51 long ret = __llvm_libc::syscall_impl(SYS_close, fd);
52 if (ret < 0) {
53 return static_cast<int>(-ret);
55 return 0;
58 } // namespace __llvm_libc