libstdc++
bits/fs_dir.h
Go to the documentation of this file.
1 // Filesystem directory utilities -*- C++ -*-
2 
3 // Copyright (C) 2014-2020 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.
10 
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.
15 
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.
19 
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/>.
24 
25 /** @file include/bits/fs_dir.h
26  * This is an internal header file, included by other library headers.
27  * Do not attempt to use it directly. @headername{filesystem}
28  */
29 
30 #ifndef _GLIBCXX_FS_DIR_H
31 #define _GLIBCXX_FS_DIR_H 1
32 
33 #if __cplusplus >= 201703L
34 # include <typeinfo>
35 # include <ext/concurrence.h>
36 # include <bits/unique_ptr.h>
37 # include <bits/shared_ptr.h>
38 
39 namespace std _GLIBCXX_VISIBILITY(default)
40 {
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 
43 namespace filesystem
44 {
45  /** @addtogroup filesystem
46  * @{
47  */
48 
49  /// Information about a file's type and permissions.
50  class file_status
51  {
52  public:
53  // constructors and destructor
54  file_status() noexcept : file_status(file_type::none) {}
55 
56  explicit
57  file_status(file_type __ft, perms __prms = perms::unknown) noexcept
58  : _M_type(__ft), _M_perms(__prms) { }
59 
60  file_status(const file_status&) noexcept = default;
61  file_status(file_status&&) noexcept = default;
62  ~file_status() = default;
63 
64  file_status& operator=(const file_status&) noexcept = default;
65  file_status& operator=(file_status&&) noexcept = default;
66 
67  // observers
68  file_type type() const noexcept { return _M_type; }
69  perms permissions() const noexcept { return _M_perms; }
70 
71  // modifiers
72  void type(file_type __ft) noexcept { _M_type = __ft; }
73  void permissions(perms __prms) noexcept { _M_perms = __prms; }
74 
75  private:
76  file_type _M_type;
77  perms _M_perms;
78  };
79 
80 _GLIBCXX_BEGIN_NAMESPACE_CXX11
81 
82  struct _Dir;
83  class directory_iterator;
84  class recursive_directory_iterator;
85 
86  /// The value type used by directory iterators
87  class directory_entry
88  {
89  public:
90  // constructors and destructor
91  directory_entry() noexcept = default;
92  directory_entry(const directory_entry&) = default;
93  directory_entry(directory_entry&&) noexcept = default;
94 
95  explicit
96  directory_entry(const filesystem::path& __p)
97  : _M_path(__p)
98  { refresh(); }
99 
100  directory_entry(const filesystem::path& __p, error_code& __ec)
101  : _M_path(__p)
102  {
103  refresh(__ec);
104  if (__ec)
105  _M_path.clear();
106  }
107 
108  ~directory_entry() = default;
109 
110  // modifiers
111  directory_entry& operator=(const directory_entry&) = default;
112  directory_entry& operator=(directory_entry&&) noexcept = default;
113 
114  void
115  assign(const filesystem::path& __p)
116  {
117  _M_path = __p;
118  refresh();
119  }
120 
121  void
122  assign(const filesystem::path& __p, error_code& __ec)
123  {
124  _M_path = __p;
125  refresh(__ec);
126  }
127 
128  void
129  replace_filename(const filesystem::path& __p)
130  {
131  _M_path.replace_filename(__p);
132  refresh();
133  }
134 
135  void
136  replace_filename(const filesystem::path& __p, error_code& __ec)
137  {
138  _M_path.replace_filename(__p);
139  refresh(__ec);
140  }
141 
142  void
143  refresh()
144  { _M_type = symlink_status().type(); }
145 
146  void
147  refresh(error_code& __ec) noexcept
148  { _M_type = symlink_status(__ec).type(); }
149 
150  // observers
151  const filesystem::path& path() const noexcept { return _M_path; }
152  operator const filesystem::path& () const noexcept { return _M_path; }
153 
154  bool
155  exists() const
156  { return filesystem::exists(file_status{_M_file_type()}); }
157 
158  bool
159  exists(error_code& __ec) const noexcept
160  { return filesystem::exists(file_status{_M_file_type(__ec)}); }
161 
162  bool
163  is_block_file() const
164  { return _M_file_type() == file_type::block; }
165 
166  bool
167  is_block_file(error_code& __ec) const noexcept
168  { return _M_file_type(__ec) == file_type::block; }
169 
170  bool
171  is_character_file() const
172  { return _M_file_type() == file_type::character; }
173 
174  bool
175  is_character_file(error_code& __ec) const noexcept
176  { return _M_file_type(__ec) == file_type::character; }
177 
178  bool
179  is_directory() const
180  { return _M_file_type() == file_type::directory; }
181 
182  bool
183  is_directory(error_code& __ec) const noexcept
184  { return _M_file_type(__ec) == file_type::directory; }
185 
186  bool
187  is_fifo() const
188  { return _M_file_type() == file_type::fifo; }
189 
190  bool
191  is_fifo(error_code& __ec) const noexcept
192  { return _M_file_type(__ec) == file_type::fifo; }
193 
194  bool
195  is_other() const
196  { return filesystem::is_other(file_status{_M_file_type()}); }
197 
198  bool
199  is_other(error_code& __ec) const noexcept
200  { return filesystem::is_other(file_status{_M_file_type(__ec)}); }
201 
202  bool
203  is_regular_file() const
204  { return _M_file_type() == file_type::regular; }
205 
206  bool
207  is_regular_file(error_code& __ec) const noexcept
208  { return _M_file_type(__ec) == file_type::regular; }
209 
210  bool
211  is_socket() const
212  { return _M_file_type() == file_type::socket; }
213 
214  bool
215  is_socket(error_code& __ec) const noexcept
216  { return _M_file_type(__ec) == file_type::socket; }
217 
218  bool
219  is_symlink() const
220  {
221  if (_M_type != file_type::none)
222  return _M_type == file_type::symlink;
223  return symlink_status().type() == file_type::symlink;
224  }
225 
226  bool
227  is_symlink(error_code& __ec) const noexcept
228  {
229  if (_M_type != file_type::none)
230  return _M_type == file_type::symlink;
231  return symlink_status(__ec).type() == file_type::symlink;
232  }
233 
234  uintmax_t
235  file_size() const
236  { return filesystem::file_size(_M_path); }
237 
238  uintmax_t
239  file_size(error_code& __ec) const noexcept
240  { return filesystem::file_size(_M_path, __ec); }
241 
242  uintmax_t
243  hard_link_count() const
244  { return filesystem::hard_link_count(_M_path); }
245 
246  uintmax_t
247  hard_link_count(error_code& __ec) const noexcept
248  { return filesystem::hard_link_count(_M_path, __ec); }
249 
250  file_time_type
251  last_write_time() const
252  { return filesystem::last_write_time(_M_path); }
253 
254 
255  file_time_type
256  last_write_time(error_code& __ec) const noexcept
257  { return filesystem::last_write_time(_M_path, __ec); }
258 
259  file_status
260  status() const
261  { return filesystem::status(_M_path); }
262 
263  file_status
264  status(error_code& __ec) const noexcept
265  { return filesystem::status(_M_path, __ec); }
266 
267  file_status
268  symlink_status() const
269  { return filesystem::symlink_status(_M_path); }
270 
271  file_status
272  symlink_status(error_code& __ec) const noexcept
273  { return filesystem::symlink_status(_M_path, __ec); }
274 
275  bool
276  operator< (const directory_entry& __rhs) const noexcept
277  { return _M_path < __rhs._M_path; }
278 
279  bool
280  operator==(const directory_entry& __rhs) const noexcept
281  { return _M_path == __rhs._M_path; }
282 
283  bool
284  operator!=(const directory_entry& __rhs) const noexcept
285  { return _M_path != __rhs._M_path; }
286 
287  bool
288  operator<=(const directory_entry& __rhs) const noexcept
289  { return _M_path <= __rhs._M_path; }
290 
291  bool
292  operator> (const directory_entry& __rhs) const noexcept
293  { return _M_path > __rhs._M_path; }
294 
295  bool
296  operator>=(const directory_entry& __rhs) const noexcept
297  { return _M_path >= __rhs._M_path; }
298 
299  private:
300  friend class _Dir;
301  friend class directory_iterator;
302  friend class recursive_directory_iterator;
303 
304  // _GLIBCXX_RESOLVE_LIB_DEFECTS
305  // 3171. LWG 2989 breaks directory_entry stream insertion
306  template<typename _CharT, typename _Traits>
307  friend basic_ostream<_CharT, _Traits>&
308  operator<<(basic_ostream<_CharT, _Traits>& __os,
309  const directory_entry& __d)
310  { return __os << __d.path(); }
311 
312  directory_entry(const filesystem::path& __p, file_type __t)
313  : _M_path(__p), _M_type(__t)
314  { }
315 
316  // Equivalent to status().type() but uses cached value, if any.
317  file_type
318  _M_file_type() const
319  {
320  if (_M_type != file_type::none && _M_type != file_type::symlink)
321  return _M_type;
322  return status().type();
323  }
324 
325  // Equivalent to status(__ec).type() but uses cached value, if any.
326  file_type
327  _M_file_type(error_code& __ec) const noexcept
328  {
329  if (_M_type != file_type::none && _M_type != file_type::symlink)
330  {
331  __ec.clear();
332  return _M_type;
333  }
334  return status(__ec).type();
335  }
336 
337  filesystem::path _M_path;
338  file_type _M_type = file_type::none;
339  };
340 
341  /// Proxy returned by post-increment on directory iterators.
342  struct __directory_iterator_proxy
343  {
344  const directory_entry& operator*() const& noexcept { return _M_entry; }
345 
346  directory_entry operator*() && noexcept { return std::move(_M_entry); }
347 
348  private:
349  friend class directory_iterator;
350  friend class recursive_directory_iterator;
351 
352  explicit
353  __directory_iterator_proxy(const directory_entry& __e) : _M_entry(__e) { }
354 
355  directory_entry _M_entry;
356  };
357 
358  /// Iterator type for traversing the entries in a single directory.
359  class directory_iterator
360  {
361  public:
362  typedef directory_entry value_type;
363  typedef ptrdiff_t difference_type;
364  typedef const directory_entry* pointer;
365  typedef const directory_entry& reference;
366  typedef input_iterator_tag iterator_category;
367 
368  directory_iterator() = default;
369 
370  explicit
371  directory_iterator(const path& __p)
372  : directory_iterator(__p, directory_options::none, nullptr) { }
373 
374  directory_iterator(const path& __p, directory_options __options)
375  : directory_iterator(__p, __options, nullptr) { }
376 
377  directory_iterator(const path& __p, error_code& __ec)
378  : directory_iterator(__p, directory_options::none, __ec) { }
379 
380  directory_iterator(const path& __p, directory_options __options,
381  error_code& __ec)
382  : directory_iterator(__p, __options, &__ec) { }
383 
384  directory_iterator(const directory_iterator& __rhs) = default;
385 
386  directory_iterator(directory_iterator&& __rhs) noexcept = default;
387 
388  ~directory_iterator() = default;
389 
390  directory_iterator&
391  operator=(const directory_iterator& __rhs) = default;
392 
393  directory_iterator&
394  operator=(directory_iterator&& __rhs) noexcept = default;
395 
396  const directory_entry& operator*() const noexcept;
397  const directory_entry* operator->() const noexcept { return &**this; }
398  directory_iterator& operator++();
399  directory_iterator& increment(error_code& __ec);
400 
401  __directory_iterator_proxy operator++(int)
402  {
403  __directory_iterator_proxy __pr{**this};
404  ++*this;
405  return __pr;
406  }
407 
408  private:
409  directory_iterator(const path&, directory_options, error_code*);
410 
411  friend bool
412  operator==(const directory_iterator& __lhs,
413  const directory_iterator& __rhs) noexcept
414  {
415  return !__rhs._M_dir.owner_before(__lhs._M_dir)
416  && !__lhs._M_dir.owner_before(__rhs._M_dir);
417  }
418 
419  friend bool
420  operator!=(const directory_iterator& __lhs,
421  const directory_iterator& __rhs) noexcept
422  { return !(__lhs == __rhs); }
423 
424  friend class recursive_directory_iterator;
425 
426  std::__shared_ptr<_Dir> _M_dir;
427  };
428 
429  /// @relates std::filesystem::directory_iterator @{
430 
431  /** @brief Enable range-based `for` using directory_iterator.
432  *
433  * e.g. `for (auto& entry : std::filesystem::directory_iterator(".")) ...`
434  */
435  inline directory_iterator
436  begin(directory_iterator __iter) noexcept
437  { return __iter; }
438 
439  /// Return a past-the-end directory_iterator
440  inline directory_iterator
441  end(directory_iterator) noexcept
442  { return directory_iterator(); }
443  // @}
444 
445  /// Iterator type for recursively traversing a directory hierarchy.
446  class recursive_directory_iterator
447  {
448  public:
449  typedef directory_entry value_type;
450  typedef ptrdiff_t difference_type;
451  typedef const directory_entry* pointer;
452  typedef const directory_entry& reference;
453  typedef input_iterator_tag iterator_category;
454 
455  recursive_directory_iterator() = default;
456 
457  explicit
458  recursive_directory_iterator(const path& __p)
459  : recursive_directory_iterator(__p, directory_options::none, nullptr) { }
460 
461  recursive_directory_iterator(const path& __p, directory_options __options)
462  : recursive_directory_iterator(__p, __options, nullptr) { }
463 
464  recursive_directory_iterator(const path& __p, directory_options __options,
465  error_code& __ec)
466  : recursive_directory_iterator(__p, __options, &__ec) { }
467 
468  recursive_directory_iterator(const path& __p, error_code& __ec)
469  : recursive_directory_iterator(__p, directory_options::none, &__ec) { }
470 
471  recursive_directory_iterator(
472  const recursive_directory_iterator&) = default;
473 
474  recursive_directory_iterator(recursive_directory_iterator&&) = default;
475 
476  ~recursive_directory_iterator();
477 
478  // observers
479  directory_options options() const noexcept;
480  int depth() const noexcept;
481  bool recursion_pending() const noexcept;
482 
483  const directory_entry& operator*() const noexcept;
484  const directory_entry* operator->() const noexcept { return &**this; }
485 
486  // modifiers
487  recursive_directory_iterator&
488  operator=(const recursive_directory_iterator& __rhs) noexcept;
489  recursive_directory_iterator&
490  operator=(recursive_directory_iterator&& __rhs) noexcept;
491 
492  recursive_directory_iterator& operator++();
493  recursive_directory_iterator& increment(error_code& __ec);
494 
495  __directory_iterator_proxy operator++(int)
496  {
497  __directory_iterator_proxy __pr{**this};
498  ++*this;
499  return __pr;
500  }
501 
502  void pop();
503  void pop(error_code&);
504 
505  void disable_recursion_pending() noexcept;
506 
507  private:
508  recursive_directory_iterator(const path&, directory_options, error_code*);
509 
510  friend bool
511  operator==(const recursive_directory_iterator& __lhs,
512  const recursive_directory_iterator& __rhs) noexcept
513  {
514  return !__rhs._M_dirs.owner_before(__lhs._M_dirs)
515  && !__lhs._M_dirs.owner_before(__rhs._M_dirs);
516  }
517 
518  friend bool
519  operator!=(const recursive_directory_iterator& __lhs,
520  const recursive_directory_iterator& __rhs) noexcept
521  { return !(__lhs == __rhs); }
522 
523  struct _Dir_stack;
524  std::__shared_ptr<_Dir_stack> _M_dirs;
525  };
526 
527  /// @relates std::filesystem::recursive_directory_iterator @{
528 
529  /** @brief Enable range-based `for` using recursive_directory_iterator.
530  *
531  * e.g. `for (auto& entry : recursive_directory_iterator(".")) ...`
532  */
533  inline recursive_directory_iterator
534  begin(recursive_directory_iterator __iter) noexcept
535  { return __iter; }
536 
537  /// Return a past-the-end recursive_directory_iterator
538  inline recursive_directory_iterator
539  end(recursive_directory_iterator) noexcept
540  { return recursive_directory_iterator(); }
541  // @}
542 
543 _GLIBCXX_END_NAMESPACE_CXX11
544 
545  // @} group filesystem
546 } // namespace filesystem
547 
548  // Use explicit instantiations of these types. Any inconsistency in the
549  // value of __default_lock_policy between code including this header and
550  // the library will cause a linker error.
551  extern template class
552  __shared_ptr<filesystem::_Dir>;
553  extern template class
554  __shared_ptr<filesystem::recursive_directory_iterator::_Dir_stack>;
555 
556 _GLIBCXX_END_NAMESPACE_VERSION
557 } // namespace std
558 
559 #endif // C++17
560 
561 #endif // _GLIBCXX_FS_DIR_H
std
ISO C++ entities toplevel namespace is std.
typeinfo
std::end
_Tp * end(valarray< _Tp > &__va)
Return an iterator pointing to one past the last element of the valarray.
Definition: valarray:1234
std::begin
_Tp * begin(valarray< _Tp > &__va)
Return an iterator pointing to the first element of the valarray.
Definition: valarray:1214
unique_ptr.h
concurrence.h
std::experimental::filesystem::v1::perms
perms
Bitmask type.
Definition: experimental/bits/fs_fwd.h:141
std::operator*
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
Definition: complex:391
std::move
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:101