re PR c++/84661 (internal compiler error: Segmentation fault (strip_array_types()))
[official-gcc.git] / libstdc++-v3 / src / c++17 / fs_dir.cc
blob8e6755ead855dddbf9e98776165c2989e49c0fd2
1 // Class filesystem::directory_entry etc. -*- C++ -*-
3 // Copyright (C) 2014-2019 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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_USE_CXX11_ABI
26 # define _GLIBCXX_USE_CXX11_ABI 1
27 #endif
29 #include <filesystem>
30 #include <utility>
31 #include <stack>
32 #include <string.h>
33 #include <errno.h>
34 #define _GLIBCXX_BEGIN_NAMESPACE_FILESYSTEM namespace filesystem {
35 #define _GLIBCXX_END_NAMESPACE_FILESYSTEM }
36 #include "../filesystem/dir-common.h"
38 namespace fs = std::filesystem;
39 namespace posix = std::filesystem::__gnu_posix;
41 template class std::__shared_ptr<fs::_Dir>;
42 template class std::__shared_ptr<fs::recursive_directory_iterator::_Dir_stack>;
44 struct fs::_Dir : _Dir_base
46 _Dir(const fs::path& p, bool skip_permission_denied, error_code& ec)
47 : _Dir_base(p.c_str(), skip_permission_denied, ec)
49 if (!ec)
50 path = p;
53 _Dir(posix::DIR* dirp, const path& p) : _Dir_base(dirp), path(p) { }
55 _Dir(_Dir&&) = default;
57 // Returns false when the end of the directory entries is reached.
58 // Reports errors by setting ec.
59 bool advance(bool skip_permission_denied, error_code& ec) noexcept
61 if (const auto entp = _Dir_base::advance(skip_permission_denied, ec))
63 auto name = path;
64 name /= entp->d_name;
65 entry = fs::directory_entry{std::move(name), get_file_type(*entp)};
66 return true;
68 else if (!ec)
70 // reached the end
71 entry = {};
73 return false;
76 bool advance(error_code& ec) noexcept { return advance(false, ec); }
78 // Returns false when the end of the directory entries is reached.
79 // Reports errors by throwing.
80 bool advance(bool skip_permission_denied = false)
82 error_code ec;
83 const bool ok = advance(skip_permission_denied, ec);
84 if (ec)
85 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
86 "directory iterator cannot advance", ec));
87 return ok;
90 bool should_recurse(bool follow_symlink, error_code& ec) const
92 file_type type = entry._M_type;
93 if (type == file_type::none || type == file_type::unknown)
95 type = entry.symlink_status(ec).type();
96 if (ec)
97 return false;
100 if (type == file_type::directory)
101 return true;
102 if (type == file_type::symlink)
103 return follow_symlink && is_directory(entry.status(ec));
104 return false;
107 fs::path path;
108 directory_entry entry;
111 namespace
113 template<typename Bitmask>
114 inline bool
115 is_set(Bitmask obj, Bitmask bits)
117 return (obj & bits) != Bitmask::none;
121 fs::directory_iterator::
122 directory_iterator(const path& p, directory_options options, error_code* ecptr)
124 const bool skip_permission_denied
125 = is_set(options, directory_options::skip_permission_denied);
127 error_code ec;
128 _Dir dir(p, skip_permission_denied, ec);
130 if (dir.dirp)
132 auto sp = std::__make_shared<fs::_Dir>(std::move(dir));
133 if (sp->advance(skip_permission_denied, ec))
134 _M_dir.swap(sp);
136 if (ecptr)
137 *ecptr = ec;
138 else if (ec)
139 _GLIBCXX_THROW_OR_ABORT(fs::filesystem_error(
140 "directory iterator cannot open directory", p, ec));
143 const fs::directory_entry&
144 fs::directory_iterator::operator*() const
146 if (!_M_dir)
147 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
148 "non-dereferenceable directory iterator",
149 std::make_error_code(errc::invalid_argument)));
150 return _M_dir->entry;
153 fs::directory_iterator&
154 fs::directory_iterator::operator++()
156 if (!_M_dir)
157 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
158 "cannot advance non-dereferenceable directory iterator",
159 std::make_error_code(errc::invalid_argument)));
160 if (!_M_dir->advance())
161 _M_dir.reset();
162 return *this;
165 fs::directory_iterator&
166 fs::directory_iterator::increment(error_code& ec)
168 if (!_M_dir)
170 ec = std::make_error_code(errc::invalid_argument);
171 return *this;
173 if (!_M_dir->advance(ec))
174 _M_dir.reset();
175 return *this;
178 struct fs::recursive_directory_iterator::_Dir_stack : std::stack<_Dir>
180 void clear() { c.clear(); }
183 fs::recursive_directory_iterator::
184 recursive_directory_iterator(const path& p, directory_options options,
185 error_code* ecptr)
186 : _M_options(options), _M_pending(true)
188 if (posix::DIR* dirp = posix::opendir(p.c_str()))
190 if (ecptr)
191 ecptr->clear();
192 auto sp = std::__make_shared<_Dir_stack>();
193 sp->push(_Dir{ dirp, p });
194 if (ecptr ? sp->top().advance(*ecptr) : sp->top().advance())
195 _M_dirs.swap(sp);
197 else
199 const int err = errno;
200 if (err == EACCES
201 && is_set(options, fs::directory_options::skip_permission_denied))
203 if (ecptr)
204 ecptr->clear();
205 return;
208 if (!ecptr)
209 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
210 "recursive directory iterator cannot open directory", p,
211 std::error_code(err, std::generic_category())));
213 ecptr->assign(err, std::generic_category());
217 fs::recursive_directory_iterator::~recursive_directory_iterator() = default;
220 fs::recursive_directory_iterator::depth() const
222 return int(_M_dirs->size()) - 1;
225 const fs::directory_entry&
226 fs::recursive_directory_iterator::operator*() const
228 return _M_dirs->top().entry;
231 fs::recursive_directory_iterator&
232 fs::recursive_directory_iterator::
233 operator=(const recursive_directory_iterator& other) noexcept = default;
235 fs::recursive_directory_iterator&
236 fs::recursive_directory_iterator::
237 operator=(recursive_directory_iterator&& other) noexcept = default;
239 fs::recursive_directory_iterator&
240 fs::recursive_directory_iterator::operator++()
242 error_code ec;
243 increment(ec);
244 if (ec)
245 _GLIBCXX_THROW_OR_ABORT(filesystem_error(
246 "cannot increment recursive directory iterator", ec));
247 return *this;
250 fs::recursive_directory_iterator&
251 fs::recursive_directory_iterator::increment(error_code& ec)
253 if (!_M_dirs)
255 ec = std::make_error_code(errc::invalid_argument);
256 return *this;
259 const bool follow
260 = is_set(_M_options, directory_options::follow_directory_symlink);
261 const bool skip_permission_denied
262 = is_set(_M_options, directory_options::skip_permission_denied);
264 auto& top = _M_dirs->top();
266 if (std::exchange(_M_pending, true) && top.should_recurse(follow, ec))
268 _Dir dir(top.entry.path(), skip_permission_denied, ec);
269 if (ec)
271 _M_dirs.reset();
272 return *this;
274 if (dir.dirp)
275 _M_dirs->push(std::move(dir));
278 while (!_M_dirs->top().advance(skip_permission_denied, ec) && !ec)
280 _M_dirs->pop();
281 if (_M_dirs->empty())
283 _M_dirs.reset();
284 return *this;
287 return *this;
290 void
291 fs::recursive_directory_iterator::pop(error_code& ec)
293 if (!_M_dirs)
295 ec = std::make_error_code(errc::invalid_argument);
296 return;
299 const bool skip_permission_denied
300 = is_set(_M_options, directory_options::skip_permission_denied);
302 do {
303 _M_dirs->pop();
304 if (_M_dirs->empty())
306 _M_dirs.reset();
307 ec.clear();
308 return;
310 } while (!_M_dirs->top().advance(skip_permission_denied, ec));
313 void
314 fs::recursive_directory_iterator::pop()
316 error_code ec;
317 pop(ec);
318 if (ec)
319 _GLIBCXX_THROW_OR_ABORT(filesystem_error(_M_dirs
320 ? "recursive directory iterator cannot pop"
321 : "non-dereferenceable recursive directory iterator cannot pop",
322 ec));