1 //===--- Implementation of a platform independent Dir data structure ------===//
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/CPP/new.h"
12 #include "src/__support/error_or.h"
13 #include "src/errno/libc_errno.h" // For error macros
15 namespace __llvm_libc
{
17 ErrorOr
<Dir
*> Dir::open(const char *path
) {
18 auto fd
= platform_opendir(path
);
20 return __llvm_libc::Error(fd
.error());
22 __llvm_libc::AllocChecker ac
;
23 Dir
*dir
= new (ac
) Dir(fd
);
25 return __llvm_libc::Error(ENOMEM
);
29 ErrorOr
<struct ::dirent
*> Dir::read() {
30 MutexLock
lock(&mutex
);
31 if (readptr
>= fillsize
) {
32 auto readsize
= platform_fetch_dirents(fd
, buffer
);
34 return __llvm_libc::Error(readsize
.error());
41 struct ::dirent
*d
= reinterpret_cast<struct ::dirent
*>(buffer
+ readptr
);
43 // The d_reclen field is available on Linux but not required by POSIX.
44 readptr
+= d
->d_reclen
;
46 // Other platforms have to implement how the read pointer is to be updated.
47 #error "DIR read pointer update is missing."
54 MutexLock
lock(&mutex
);
55 int retval
= platform_closedir(fd
);
63 } // namespace __llvm_libc