1 // Filesystem directory iterator utilities -*- C++ -*-
3 // Copyright (C) 2014-2022 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 #ifndef _GLIBCXX_DIR_COMMON_H
26 #define _GLIBCXX_DIR_COMMON_H 1
28 #include <string.h> // strcmp
30 #if _GLIBCXX_FILESYSTEM_IS_WINDOWS
31 #include <wchar.h> // wcscmp
33 #ifdef _GLIBCXX_HAVE_DIRENT_H
34 # ifdef _GLIBCXX_HAVE_SYS_TYPES_H
35 # include <sys/types.h>
40 namespace std
_GLIBCXX_VISIBILITY(default)
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47 #if _GLIBCXX_FILESYSTEM_IS_WINDOWS
48 // Adapt the Windows _wxxx functions to look like POSIX xxx, but for wchar_t*.
49 using char_type
= wchar_t;
51 using dirent
= _wdirent
;
52 inline DIR* opendir(const wchar_t* path
) { return ::_wopendir(path
); }
53 inline dirent
* readdir(DIR* dir
) { return ::_wreaddir(dir
); }
54 inline int closedir(DIR* dir
) { return ::_wclosedir(dir
); }
55 #elif defined _GLIBCXX_HAVE_DIRENT_H
56 using char_type
= char;
58 typedef struct ::dirent dirent
;
63 using char_type
= char;
64 struct dirent
{ const char* d_name
; };
66 inline DIR* opendir(const char*) { return nullptr; }
67 inline dirent
* readdir(DIR*) { return nullptr; }
68 inline int closedir(DIR*) { return -1; }
70 } // namespace __gnu_posix
72 namespace posix
= __gnu_posix
;
76 _Dir_base(posix::DIR* dirp
= nullptr) : dirp(dirp
) { }
78 // If no error occurs then dirp is non-null,
79 // otherwise null (whether error ignored or not).
80 _Dir_base(const posix::char_type
* pathname
, bool skip_permission_denied
,
81 error_code
& ec
) noexcept
82 : dirp(posix::opendir(pathname
))
88 const int err
= errno
;
89 if (err
== EACCES
&& skip_permission_denied
)
92 ec
.assign(err
, std::generic_category());
96 _Dir_base(_Dir_base
&& d
) : dirp(std::exchange(d
.dirp
, nullptr)) { }
98 _Dir_base
& operator=(_Dir_base
&&) = delete;
100 ~_Dir_base() { if (dirp
) posix::closedir(dirp
); }
103 advance(bool skip_permission_denied
, error_code
& ec
) noexcept
107 int err
= std::exchange(errno
, 0);
108 const posix::dirent
* entp
= posix::readdir(dirp
);
109 // std::swap cannot be used with Bionic's errno
110 err
= std::exchange(errno
, err
);
114 // skip past dot and dot-dot
115 if (is_dot_or_dotdot(entp
->d_name
))
116 return advance(skip_permission_denied
, ec
);
121 if (err
== EACCES
&& skip_permission_denied
)
123 ec
.assign(err
, std::generic_category());
133 static bool is_dot_or_dotdot(const char* s
) noexcept
134 { return !strcmp(s
, ".") || !strcmp(s
, ".."); }
136 #if _GLIBCXX_FILESYSTEM_IS_WINDOWS
137 static bool is_dot_or_dotdot(const wchar_t* s
) noexcept
138 { return !wcscmp(s
, L
".") || !wcscmp(s
, L
".."); }
145 is_permission_denied_error(int e
)
150 if (e
== EPERM
) // See PR 99533
156 } // namespace filesystem
158 // BEGIN/END macros must be defined before including this file.
159 _GLIBCXX_BEGIN_NAMESPACE_FILESYSTEM
162 get_file_type(const std::filesystem::__gnu_posix::dirent
& d
[[gnu::unused
]])
164 #ifdef _GLIBCXX_HAVE_STRUCT_DIRENT_D_TYPE
168 return file_type::block
;
170 return file_type::character
;
172 return file_type::directory
;
174 return file_type::fifo
;
176 return file_type::symlink
;
178 return file_type::regular
;
180 return file_type::socket
;
182 return file_type::unknown
;
184 return file_type::none
;
187 return file_type::none
;
190 _GLIBCXX_END_NAMESPACE_FILESYSTEM
192 _GLIBCXX_END_NAMESPACE_VERSION
195 #endif // _GLIBCXX_DIR_COMMON_H