1 // Filesystem directory iterator utilities -*- C++ -*-
3 // Copyright (C) 2014-2018 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
29 #if _GLIBCXX_FILESYSTEM_IS_WINDOWS
30 #include <wchar.h> // wcscmp
32 #ifdef _GLIBCXX_HAVE_DIRENT_H
33 # ifdef _GLIBCXX_HAVE_SYS_TYPES_H
34 # include <sys/types.h>
38 # error "the <dirent.h> header is needed to build the Filesystem TS"
41 namespace std
_GLIBCXX_VISIBILITY(default)
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
48 #if _GLIBCXX_FILESYSTEM_IS_WINDOWS
49 // Adapt the Windows _wxxx functions to look like POSIX xxx, but for wchar_t*.
50 using char_type
= wchar_t;
52 using dirent
= _wdirent
;
53 inline DIR* opendir(const wchar_t* path
) { return ::_wopendir(path
); }
54 inline dirent
* readdir(DIR* dir
) { return ::_wreaddir(dir
); }
55 inline int closedir(DIR* dir
) { return ::_wclosedir(dir
); }
57 using char_type
= char;
59 typedef struct ::dirent dirent
;
64 } // namespace __gnu_posix
66 namespace posix
= __gnu_posix
;
70 _Dir_base(posix::DIR* dirp
= nullptr) : dirp(dirp
) { }
72 // If no error occurs then dirp is non-null,
73 // otherwise null (whether error ignored or not).
74 _Dir_base(const posix::char_type
* pathname
, bool skip_permission_denied
,
75 error_code
& ec
) noexcept
76 : dirp(posix::opendir(pathname
))
82 const int err
= errno
;
83 if (err
== EACCES
&& skip_permission_denied
)
86 ec
.assign(err
, std::generic_category());
90 _Dir_base(_Dir_base
&& d
) : dirp(std::exchange(d
.dirp
, nullptr)) { }
92 _Dir_base
& operator=(_Dir_base
&&) = delete;
94 ~_Dir_base() { if (dirp
) posix::closedir(dirp
); }
97 advance(bool skip_permission_denied
, error_code
& ec
) noexcept
101 int err
= std::exchange(errno
, 0);
102 const posix::dirent
* entp
= posix::readdir(dirp
);
103 // std::swap cannot be used with Bionic's errno
104 err
= std::exchange(errno
, err
);
108 // skip past dot and dot-dot
109 if (is_dot_or_dotdot(entp
->d_name
))
110 return advance(skip_permission_denied
, ec
);
115 if (err
== EACCES
&& skip_permission_denied
)
117 ec
.assign(err
, std::generic_category());
127 static bool is_dot_or_dotdot(const char* s
) noexcept
128 { return !strcmp(s
, ".") || !strcmp(s
, ".."); }
130 #if _GLIBCXX_FILESYSTEM_IS_WINDOWS
131 static bool is_dot_or_dotdot(const wchar_t* s
) noexcept
132 { return !wcscmp(s
, L
".") || !wcscmp(s
, L
".."); }
138 } // namespace filesystem
140 // BEGIN/END macros must be defined before including this file.
141 _GLIBCXX_BEGIN_NAMESPACE_FILESYSTEM
144 get_file_type(const std::filesystem::__gnu_posix::dirent
& d
[[gnu::unused
]])
146 #ifdef _GLIBCXX_HAVE_STRUCT_DIRENT_D_TYPE
150 return file_type::block
;
152 return file_type::character
;
154 return file_type::directory
;
156 return file_type::fifo
;
158 return file_type::symlink
;
160 return file_type::regular
;
162 return file_type::socket
;
164 return file_type::unknown
;
166 return file_type::none
;
169 return file_type::none
;
172 _GLIBCXX_END_NAMESPACE_FILESYSTEM
174 _GLIBCXX_END_NAMESPACE_VERSION
177 #endif // _GLIBCXX_DIR_COMMON_H