1 // Filesystem directory utilities -*- C++ -*-
2 
3 // Copyright (C) 2014-2018 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   /**
46    * @ingroup filesystem
47    * @{
48    */
49 
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   class directory_entry
87   {
88   public:
89     // constructors and destructor
90     directory_entry() noexcept = default;
91     directory_entry(const directory_entry&) = default;
92     directory_entry(directory_entry&&) noexcept = default;
93 
94     explicit
95     directory_entry(const filesystem::path& __p)
96     : _M_path(__p)
97     { refresh(); }
98 
99     directory_entry(const filesystem::path& __p, error_code& __ec)
100     : _M_path(__p)
101     {
102       refresh(__ec);
103       if (__ec)
104 	_M_path.clear();
105     }
106 
107     ~directory_entry() = default;
108 
109     // modifiers
110     directory_entry& operator=(const directory_entry&) = default;
111     directory_entry& operator=(directory_entry&&) noexcept = default;
112 
113     void
114     assign(const filesystem::path& __p)
115     {
116       _M_path = __p;
117       refresh();
118     }
119 
120     void
121     assign(const filesystem::path& __p, error_code& __ec)
122     {
123       _M_path = __p;
124       refresh(__ec);
125     }
126 
127     void
128     replace_filename(const filesystem::path& __p)
129     {
130       _M_path.replace_filename(__p);
131       refresh();
132     }
133 
134     void
135     replace_filename(const filesystem::path& __p, error_code& __ec)
136     {
137       _M_path.replace_filename(__p);
138       refresh(__ec);
139     }
140 
141     void refresh() { _M_type = symlink_status().type(); }
142     void refresh(error_code& __ec) { _M_type = symlink_status(__ec).type(); }
143 
144     // observers
145     const filesystem::path& path() const noexcept { return _M_path; }
146     operator const filesystem::path& () const noexcept { return _M_path; }
147 
148     bool
149     exists() const
150     { return filesystem::exists(file_status{_M_file_type()}); }
151 
152     bool
153     exists(error_code& __ec) const noexcept
154     { return filesystem::exists(file_status{_M_file_type(__ec)}); }
155 
156     bool
157     is_block_file() const
158     { return _M_file_type() == file_type::block; }
159 
160     bool
161     is_block_file(error_code& __ec) const noexcept
162     { return _M_file_type(__ec) == file_type::block; }
163 
164     bool
165     is_character_file() const
166     { return _M_file_type() == file_type::character; }
167 
168     bool
169     is_character_file(error_code& __ec) const noexcept
170     { return _M_file_type(__ec) == file_type::character; }
171 
172     bool
173     is_directory() const
174     { return _M_file_type() == file_type::directory; }
175 
176     bool
177     is_directory(error_code& __ec) const noexcept
178     { return _M_file_type(__ec) == file_type::directory; }
179 
180     bool
181     is_fifo() const
182     { return _M_file_type() == file_type::fifo; }
183 
184     bool
185     is_fifo(error_code& __ec) const noexcept
186     { return _M_file_type(__ec) == file_type::fifo; }
187 
188     bool
189     is_other() const
190     { return filesystem::is_other(file_status{_M_file_type()}); }
191 
192     bool
193     is_other(error_code& __ec) const noexcept
194     { return filesystem::is_other(file_status{_M_file_type(__ec)}); }
195 
196     bool
197     is_regular_file() const
198     { return _M_file_type() == file_type::regular; }
199 
200     bool
201     is_regular_file(error_code& __ec) const noexcept
202     { return _M_file_type(__ec) == file_type::regular; }
203 
204     bool
205     is_socket() const
206     { return _M_file_type() == file_type::socket; }
207 
208     bool
209     is_socket(error_code& __ec) const noexcept
210     { return _M_file_type(__ec) == file_type::socket; }
211 
212     bool
213     is_symlink() const
214     {
215       if (_M_type != file_type::none)
216 	return _M_type == file_type::symlink;
217       return symlink_status().type() == file_type::symlink;
218     }
219 
220     bool
221     is_symlink(error_code& __ec) const noexcept
222     {
223       if (_M_type != file_type::none)
224 	return _M_type == file_type::symlink;
225       return symlink_status(__ec).type() == file_type::symlink;
226     }
227 
228     uintmax_t
229     file_size() const
230     { return filesystem::file_size(_M_path); }
231 
232     uintmax_t
233     file_size(error_code& __ec) const noexcept
234     { return filesystem::file_size(_M_path, __ec); }
235 
236     uintmax_t
237     hard_link_count() const
238     { return filesystem::hard_link_count(_M_path); }
239 
240     uintmax_t
241     hard_link_count(error_code& __ec) const noexcept
242     { return filesystem::hard_link_count(_M_path, __ec); }
243 
244     file_time_type
245     last_write_time() const
246     { return filesystem::last_write_time(_M_path); }
247 
248 
249     file_time_type
250     last_write_time(error_code& __ec) const noexcept
251     { return filesystem::last_write_time(_M_path, __ec); }
252 
253     file_status
254     status() const
255     { return filesystem::status(_M_path); }
256 
257     file_status
258     status(error_code& __ec) const noexcept
259     { return filesystem::status(_M_path, __ec); }
260 
261     file_status
262     symlink_status() const
263     { return filesystem::symlink_status(_M_path); }
264 
265     file_status
266     symlink_status(error_code& __ec) const noexcept
267     { return filesystem::symlink_status(_M_path, __ec); }
268 
269     bool
270     operator< (const directory_entry& __rhs) const noexcept
271     { return _M_path < __rhs._M_path; }
272 
273     bool
274     operator==(const directory_entry& __rhs) const noexcept
275     { return _M_path == __rhs._M_path; }
276 
277     bool
278     operator!=(const directory_entry& __rhs) const noexcept
279     { return _M_path != __rhs._M_path; }
280 
281     bool
282     operator<=(const directory_entry& __rhs) const noexcept
283     { return _M_path <= __rhs._M_path; }
284 
285     bool
286     operator> (const directory_entry& __rhs) const noexcept
287     { return _M_path > __rhs._M_path; }
288 
289     bool
290     operator>=(const directory_entry& __rhs) const noexcept
291     { return _M_path >= __rhs._M_path; }
292 
293   private:
294     friend class _Dir;
295     friend class directory_iterator;
296     friend class recursive_directory_iterator;
297 
298     directory_entry(const filesystem::path& __p, file_type __t)
299     : _M_path(__p), _M_type(__t)
300     { }
301 
302     // Equivalent to status().type() but uses cached value, if any.
303     file_type
304     _M_file_type() const
305     {
306       if (_M_type != file_type::none && _M_type != file_type::symlink)
307 	return _M_type;
308       return status().type();
309     }
310 
311     // Equivalent to status(__ec).type() but uses cached value, if any.
312     file_type
313     _M_file_type(error_code& __ec) const noexcept
314     {
315       if (_M_type != file_type::none && _M_type != file_type::symlink)
316 	return _M_type;
317       return status(__ec).type();
318     }
319 
320     filesystem::path	_M_path;
321     file_type		_M_type = file_type::none;
322   };
323 
324   struct __directory_iterator_proxy
325   {
326     const directory_entry& operator*() const& noexcept { return _M_entry; }
327 
328     directory_entry operator*() && noexcept { return std::move(_M_entry); }
329 
330   private:
331     friend class directory_iterator;
332     friend class recursive_directory_iterator;
333 
334     explicit
335     __directory_iterator_proxy(const directory_entry& __e) : _M_entry(__e) { }
336 
337     directory_entry _M_entry;
338   };
339 
340   class directory_iterator
341   {
342   public:
343     typedef directory_entry        value_type;
344     typedef ptrdiff_t              difference_type;
345     typedef const directory_entry* pointer;
346     typedef const directory_entry& reference;
347     typedef input_iterator_tag     iterator_category;
348 
349     directory_iterator() = default;
350 
351     explicit
352     directory_iterator(const path& __p)
353     : directory_iterator(__p, directory_options::none, nullptr) { }
354 
355     directory_iterator(const path& __p, directory_options __options)
356     : directory_iterator(__p, __options, nullptr) { }
357 
358     directory_iterator(const path& __p, error_code& __ec)
359     : directory_iterator(__p, directory_options::none, __ec) { }
360 
361     directory_iterator(const path& __p, directory_options __options,
362 		       error_code& __ec)
363     : directory_iterator(__p, __options, &__ec) { }
364 
365     directory_iterator(const directory_iterator& __rhs) = default;
366 
367     directory_iterator(directory_iterator&& __rhs) noexcept = default;
368 
369     ~directory_iterator() = default;
370 
371     directory_iterator&
372     operator=(const directory_iterator& __rhs) = default;
373 
374     directory_iterator&
375     operator=(directory_iterator&& __rhs) noexcept = default;
376 
377     const directory_entry& operator*() const;
378     const directory_entry* operator->() const { return &**this; }
379     directory_iterator&    operator++();
380     directory_iterator&    increment(error_code& __ec);
381 
382     __directory_iterator_proxy operator++(int)
383     {
384       __directory_iterator_proxy __pr{**this};
385       ++*this;
386       return __pr;
387     }
388 
389   private:
390     directory_iterator(const path&, directory_options, error_code*);
391 
392     friend bool
393     operator==(const directory_iterator& __lhs,
394                const directory_iterator& __rhs);
395 
396     friend class recursive_directory_iterator;
397 
398     std::shared_ptr<_Dir> _M_dir;
399   };
400 
401   inline directory_iterator
402   begin(directory_iterator __iter) noexcept
403   { return __iter; }
404 
405   inline directory_iterator
406   end(directory_iterator) noexcept
407   { return directory_iterator(); }
408 
409   inline bool
410   operator==(const directory_iterator& __lhs, const directory_iterator& __rhs)
411   {
412     return !__rhs._M_dir.owner_before(__lhs._M_dir)
413       && !__lhs._M_dir.owner_before(__rhs._M_dir);
414   }
415 
416   inline bool
417   operator!=(const directory_iterator& __lhs, const directory_iterator& __rhs)
418   { return !(__lhs == __rhs); }
419 
420   class recursive_directory_iterator
421   {
422   public:
423     typedef directory_entry        value_type;
424     typedef ptrdiff_t              difference_type;
425     typedef const directory_entry* pointer;
426     typedef const directory_entry& reference;
427     typedef input_iterator_tag     iterator_category;
428 
429     recursive_directory_iterator() = default;
430 
431     explicit
432     recursive_directory_iterator(const path& __p)
433     : recursive_directory_iterator(__p, directory_options::none, nullptr) { }
434 
435     recursive_directory_iterator(const path& __p, directory_options __options)
436     : recursive_directory_iterator(__p, __options, nullptr) { }
437 
438     recursive_directory_iterator(const path& __p, directory_options __options,
439                                  error_code& __ec)
440     : recursive_directory_iterator(__p, __options, &__ec) { }
441 
442     recursive_directory_iterator(const path& __p, error_code& __ec)
443     : recursive_directory_iterator(__p, directory_options::none, &__ec) { }
444 
445     recursive_directory_iterator(
446         const recursive_directory_iterator&) = default;
447 
448     recursive_directory_iterator(recursive_directory_iterator&&) = default;
449 
450     ~recursive_directory_iterator();
451 
452     // observers
453     directory_options  options() const { return _M_options; }
454     int                depth() const;
455     bool               recursion_pending() const { return _M_pending; }
456 
457     const directory_entry& operator*() const;
458     const directory_entry* operator->() const { return &**this; }
459 
460     // modifiers
461     recursive_directory_iterator&
462     operator=(const recursive_directory_iterator& __rhs) noexcept;
463     recursive_directory_iterator&
464     operator=(recursive_directory_iterator&& __rhs) noexcept;
465 
466     recursive_directory_iterator& operator++();
467     recursive_directory_iterator& increment(error_code& __ec);
468 
469     __directory_iterator_proxy operator++(int)
470     {
471       __directory_iterator_proxy __pr{**this};
472       ++*this;
473       return __pr;
474     }
475 
476     void pop();
477     void pop(error_code&);
478 
479     void disable_recursion_pending() { _M_pending = false; }
480 
481   private:
482     recursive_directory_iterator(const path&, directory_options, error_code*);
483 
484     friend bool
485     operator==(const recursive_directory_iterator& __lhs,
486                const recursive_directory_iterator& __rhs);
487 
488     struct _Dir_stack;
489     std::shared_ptr<_Dir_stack> _M_dirs;
490     directory_options _M_options = {};
491     bool _M_pending = false;
492   };
493 
494   inline recursive_directory_iterator
495   begin(recursive_directory_iterator __iter) noexcept
496   { return __iter; }
497 
498   inline recursive_directory_iterator
499   end(recursive_directory_iterator) noexcept
500   { return recursive_directory_iterator(); }
501 
502   inline bool
503   operator==(const recursive_directory_iterator& __lhs,
504              const recursive_directory_iterator& __rhs)
505   {
506     return !__rhs._M_dirs.owner_before(__lhs._M_dirs)
507       && !__lhs._M_dirs.owner_before(__rhs._M_dirs);
508   }
509 
510   inline bool
511   operator!=(const recursive_directory_iterator& __lhs,
512              const recursive_directory_iterator& __rhs)
513   { return !(__lhs == __rhs); }
514 
515 _GLIBCXX_END_NAMESPACE_CXX11
516 
517   // @} group filesystem
518 } // namespace filesystem
519 
520 _GLIBCXX_END_NAMESPACE_VERSION
521 } // namespace std
522 
523 #endif // C++17
524 
525 #endif // _GLIBCXX_FS_DIR_H
526