aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/libcxx/include/__filesystem/directory_entry.h
blob: 5ba3ef96de481e2448428263a7525466a9cc789b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP___FILESYSTEM_DIRECTORY_ENTRY_H
#define _LIBCPP___FILESYSTEM_DIRECTORY_ENTRY_H

#include <__availability>
#include <__chrono/time_point.h>
#include <__config>
#include <__errc>
#include <__filesystem/file_status.h>
#include <__filesystem/file_time_type.h>
#include <__filesystem/file_type.h>
#include <__filesystem/filesystem_error.h>
#include <__filesystem/operations.h>
#include <__filesystem/path.h>
#include <__filesystem/perms.h>
#include <__utility/unreachable.h>
#include <cstdint>
#include <cstdlib>
#include <iosfwd>
#include <system_error>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#  pragma GCC system_header
#endif

_LIBCPP_PUSH_MACROS
#include <__undef_macros>

#ifndef _LIBCPP_CXX03_LANG

_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM

_LIBCPP_AVAILABILITY_FILESYSTEM_PUSH


class directory_entry {
  typedef _VSTD_FS::path _Path;

public:
  // constructors and destructors
  directory_entry() noexcept = default;
  directory_entry(directory_entry const&) = default;
  directory_entry(directory_entry&&) noexcept = default;

  _LIBCPP_INLINE_VISIBILITY
  explicit directory_entry(_Path const& __p) : __p_(__p) {
    error_code __ec;
    __refresh(&__ec);
  }

  _LIBCPP_INLINE_VISIBILITY
  directory_entry(_Path const& __p, error_code& __ec) : __p_(__p) {
    __refresh(&__ec);
  }

  ~directory_entry() {}

  directory_entry& operator=(directory_entry const&) = default;
  directory_entry& operator=(directory_entry&&) noexcept = default;

  _LIBCPP_INLINE_VISIBILITY
  void assign(_Path const& __p) {
    __p_ = __p;
    error_code __ec;
    __refresh(&__ec);
  }

  _LIBCPP_INLINE_VISIBILITY
  void assign(_Path const& __p, error_code& __ec) {
    __p_ = __p;
    __refresh(&__ec);
  }

  _LIBCPP_INLINE_VISIBILITY
  void replace_filename(_Path const& __p) {
    __p_.replace_filename(__p);
    error_code __ec;
    __refresh(&__ec);
  }

  _LIBCPP_INLINE_VISIBILITY
  void replace_filename(_Path const& __p, error_code& __ec) {
    __p_ = __p_.parent_path() / __p;
    __refresh(&__ec);
  }

  _LIBCPP_INLINE_VISIBILITY
  void refresh() { __refresh(); }

  _LIBCPP_INLINE_VISIBILITY
  void refresh(error_code& __ec) noexcept { __refresh(&__ec); }

  _LIBCPP_INLINE_VISIBILITY
  _Path const& path() const noexcept { return __p_; }

  _LIBCPP_INLINE_VISIBILITY
  operator const _Path&() const noexcept { return __p_; }

  _LIBCPP_INLINE_VISIBILITY
  bool exists() const { return _VSTD_FS::exists(file_status{__get_ft()}); }

  _LIBCPP_INLINE_VISIBILITY
  bool exists(error_code& __ec) const noexcept {
    return _VSTD_FS::exists(file_status{__get_ft(&__ec)});
  }

  _LIBCPP_INLINE_VISIBILITY
  bool is_block_file() const { return __get_ft() == file_type::block; }

  _LIBCPP_INLINE_VISIBILITY
  bool is_block_file(error_code& __ec) const noexcept {
    return __get_ft(&__ec) == file_type::block;
  }

  _LIBCPP_INLINE_VISIBILITY
  bool is_character_file() const { return __get_ft() == file_type::character; }

  _LIBCPP_INLINE_VISIBILITY
  bool is_character_file(error_code& __ec) const noexcept {
    return __get_ft(&__ec) == file_type::character;
  }

  _LIBCPP_INLINE_VISIBILITY
  bool is_directory() const { return __get_ft() == file_type::directory; }

  _LIBCPP_INLINE_VISIBILITY
  bool is_directory(error_code& __ec) const noexcept {
    return __get_ft(&__ec) == file_type::directory;
  }

  _LIBCPP_INLINE_VISIBILITY
  bool is_fifo() const { return __get_ft() == file_type::fifo; }

  _LIBCPP_INLINE_VISIBILITY
  bool is_fifo(error_code& __ec) const noexcept {
    return __get_ft(&__ec) == file_type::fifo;
  }

  _LIBCPP_INLINE_VISIBILITY
  bool is_other() const { return _VSTD_FS::is_other(file_status{__get_ft()}); }

  _LIBCPP_INLINE_VISIBILITY
  bool is_other(error_code& __ec) const noexcept {
    return _VSTD_FS::is_other(file_status{__get_ft(&__ec)});
  }

  _LIBCPP_INLINE_VISIBILITY
  bool is_regular_file() const { return __get_ft() == file_type::regular; }

  _LIBCPP_INLINE_VISIBILITY
  bool is_regular_file(error_code& __ec) const noexcept {
    return __get_ft(&__ec) == file_type::regular;
  }

  _LIBCPP_INLINE_VISIBILITY
  bool is_socket() const { return __get_ft() == file_type::socket; }

  _LIBCPP_INLINE_VISIBILITY
  bool is_socket(error_code& __ec) const noexcept {
    return __get_ft(&__ec) == file_type::socket;
  }

  _LIBCPP_INLINE_VISIBILITY
  bool is_symlink() const { return __get_sym_ft() == file_type::symlink; }

  _LIBCPP_INLINE_VISIBILITY
  bool is_symlink(error_code& __ec) const noexcept {
    return __get_sym_ft(&__ec) == file_type::symlink;
  }
  _LIBCPP_INLINE_VISIBILITY
  uintmax_t file_size() const { return __get_size(); }

  _LIBCPP_INLINE_VISIBILITY
  uintmax_t file_size(error_code& __ec) const noexcept {
    return __get_size(&__ec);
  }

  _LIBCPP_INLINE_VISIBILITY
  uintmax_t hard_link_count() const { return __get_nlink(); }

  _LIBCPP_INLINE_VISIBILITY
  uintmax_t hard_link_count(error_code& __ec) const noexcept {
    return __get_nlink(&__ec);
  }

  _LIBCPP_INLINE_VISIBILITY
  file_time_type last_write_time() const { return __get_write_time(); }

  _LIBCPP_INLINE_VISIBILITY
  file_time_type last_write_time(error_code& __ec) const noexcept {
    return __get_write_time(&__ec);
  }

  _LIBCPP_INLINE_VISIBILITY
  file_status status() const { return __get_status(); }

  _LIBCPP_INLINE_VISIBILITY
  file_status status(error_code& __ec) const noexcept {
    return __get_status(&__ec);
  }

  _LIBCPP_INLINE_VISIBILITY
  file_status symlink_status() const { return __get_symlink_status(); }

  _LIBCPP_INLINE_VISIBILITY
  file_status symlink_status(error_code& __ec) const noexcept {
    return __get_symlink_status(&__ec);
  }

  _LIBCPP_INLINE_VISIBILITY
  bool operator<(directory_entry const& __rhs) const noexcept {
    return __p_ < __rhs.__p_;
  }

  _LIBCPP_INLINE_VISIBILITY
  bool operator==(directory_entry const& __rhs) const noexcept {
    return __p_ == __rhs.__p_;
  }

  _LIBCPP_INLINE_VISIBILITY
  bool operator!=(directory_entry const& __rhs) const noexcept {
    return __p_ != __rhs.__p_;
  }

  _LIBCPP_INLINE_VISIBILITY
  bool operator<=(directory_entry const& __rhs) const noexcept {
    return __p_ <= __rhs.__p_;
  }

  _LIBCPP_INLINE_VISIBILITY
  bool operator>(directory_entry const& __rhs) const noexcept {
    return __p_ > __rhs.__p_;
  }

  _LIBCPP_INLINE_VISIBILITY
  bool operator>=(directory_entry const& __rhs) const noexcept {
    return __p_ >= __rhs.__p_;
  }

  template <class _CharT, class _Traits>
  _LIBCPP_INLINE_VISIBILITY
  friend basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const directory_entry& __d) {
    return __os << __d.path();
  }

private:
  friend class directory_iterator;
  friend class recursive_directory_iterator;
  friend class _LIBCPP_HIDDEN __dir_stream;

  enum _CacheType : unsigned char {
    _Empty,
    _IterSymlink,
    _IterNonSymlink,
    _RefreshSymlink,
    _RefreshSymlinkUnresolved,
    _RefreshNonSymlink
  };

  struct __cached_data {
    uintmax_t __size_;
    uintmax_t __nlink_;
    file_time_type __write_time_;
    perms __sym_perms_;
    perms __non_sym_perms_;
    file_type __type_;
    _CacheType __cache_type_;

    _LIBCPP_INLINE_VISIBILITY
    __cached_data() noexcept { __reset(); }

    _LIBCPP_INLINE_VISIBILITY
    void __reset() {
      __cache_type_ = _Empty;
      __type_ = file_type::none;
      __sym_perms_ = __non_sym_perms_ = perms::unknown;
      __size_ = __nlink_ = uintmax_t(-1);
      __write_time_ = file_time_type::min();
    }
  };

  _LIBCPP_INLINE_VISIBILITY
  static __cached_data __create_iter_result(file_type __ft) {
    __cached_data __data;
    __data.__type_ = __ft;
    __data.__cache_type_ = [&]() {
      switch (__ft) {
      case file_type::none:
        return _Empty;
      case file_type::symlink:
        return _IterSymlink;
      default:
        return _IterNonSymlink;
      }
    }();
    return __data;
  }

  _LIBCPP_INLINE_VISIBILITY
  void __assign_iter_entry(_Path&& __p, __cached_data __dt) {
    __p_ = _VSTD::move(__p);
    __data_ = __dt;
  }

  _LIBCPP_FUNC_VIS
  error_code __do_refresh() noexcept;

  _LIBCPP_INLINE_VISIBILITY
  static bool __is_dne_error(error_code const& __ec) {
    if (!__ec)
      return true;
    switch (static_cast<errc>(__ec.value())) {
    case errc::no_such_file_or_directory:
    case errc::not_a_directory:
      return true;
    default:
      return false;
    }
  }

  _LIBCPP_INLINE_VISIBILITY
  void __handle_error(const char* __msg, error_code* __dest_ec,
                      error_code const& __ec, bool __allow_dne = false) const {
    if (__dest_ec) {
      *__dest_ec = __ec;
      return;
    }
    if (__ec && (!__allow_dne || !__is_dne_error(__ec)))
      __throw_filesystem_error(__msg, __p_, __ec);
  }

  _LIBCPP_INLINE_VISIBILITY
  void __refresh(error_code* __ec = nullptr) {
    __handle_error("in directory_entry::refresh", __ec, __do_refresh(),
                   /*allow_dne*/ true);
  }

  _LIBCPP_INLINE_VISIBILITY
  file_type __get_sym_ft(error_code* __ec = nullptr) const {
    switch (__data_.__cache_type_) {
    case _Empty:
      return __symlink_status(__p_, __ec).type();
    case _IterSymlink:
    case _RefreshSymlink:
    case _RefreshSymlinkUnresolved:
      if (__ec)
        __ec->clear();
      return file_type::symlink;
    case _IterNonSymlink:
    case _RefreshNonSymlink:
      file_status __st(__data_.__type_);
      if (__ec && !_VSTD_FS::exists(__st))
        *__ec = make_error_code(errc::no_such_file_or_directory);
      else if (__ec)
        __ec->clear();
      return __data_.__type_;
    }
    __libcpp_unreachable();
  }

  _LIBCPP_INLINE_VISIBILITY
  file_type __get_ft(error_code* __ec = nullptr) const {
    switch (__data_.__cache_type_) {
    case _Empty:
    case _IterSymlink:
    case _RefreshSymlinkUnresolved:
      return __status(__p_, __ec).type();
    case _IterNonSymlink:
    case _RefreshNonSymlink:
    case _RefreshSymlink: {
      file_status __st(__data_.__type_);
      if (__ec && !_VSTD_FS::exists(__st))
        *__ec = make_error_code(errc::no_such_file_or_directory);
      else if (__ec)
        __ec->clear();
      return __data_.__type_;
    }
    }
    __libcpp_unreachable();
  }

  _LIBCPP_INLINE_VISIBILITY
  file_status __get_status(error_code* __ec = nullptr) const {
    switch (__data_.__cache_type_) {
    case _Empty:
    case _IterNonSymlink:
    case _IterSymlink:
    case _RefreshSymlinkUnresolved:
      return __status(__p_, __ec);
    case _RefreshNonSymlink:
    case _RefreshSymlink:
      return file_status(__get_ft(__ec), __data_.__non_sym_perms_);
    }
    __libcpp_unreachable();
  }

  _LIBCPP_INLINE_VISIBILITY
  file_status __get_symlink_status(error_code* __ec = nullptr) const {
    switch (__data_.__cache_type_) {
    case _Empty:
    case _IterNonSymlink:
    case _IterSymlink:
      return __symlink_status(__p_, __ec);
    case _RefreshNonSymlink:
      return file_status(__get_sym_ft(__ec), __data_.__non_sym_perms_);
    case _RefreshSymlink:
    case _RefreshSymlinkUnresolved:
      return file_status(__get_sym_ft(__ec), __data_.__sym_perms_);
    }
    __libcpp_unreachable();
  }

  _LIBCPP_INLINE_VISIBILITY
  uintmax_t __get_size(error_code* __ec = nullptr) const {
    switch (__data_.__cache_type_) {
    case _Empty:
    case _IterNonSymlink:
    case _IterSymlink:
    case _RefreshSymlinkUnresolved:
      return _VSTD_FS::__file_size(__p_, __ec);
    case _RefreshSymlink:
    case _RefreshNonSymlink: {
      error_code __m_ec;
      file_status __st(__get_ft(&__m_ec));
      __handle_error("in directory_entry::file_size", __ec, __m_ec);
      if (_VSTD_FS::exists(__st) && !_VSTD_FS::is_regular_file(__st)) {
        errc __err_kind = _VSTD_FS::is_directory(__st) ? errc::is_a_directory
                                                       : errc::not_supported;
        __handle_error("in directory_entry::file_size", __ec,
                       make_error_code(__err_kind));
      }
      return __data_.__size_;
    }
    }
    __libcpp_unreachable();
  }

  _LIBCPP_INLINE_VISIBILITY
  uintmax_t __get_nlink(error_code* __ec = nullptr) const {
    switch (__data_.__cache_type_) {
    case _Empty:
    case _IterNonSymlink:
    case _IterSymlink:
    case _RefreshSymlinkUnresolved:
      return _VSTD_FS::__hard_link_count(__p_, __ec);
    case _RefreshSymlink:
    case _RefreshNonSymlink: {
      error_code __m_ec;
      (void)__get_ft(&__m_ec);
      __handle_error("in directory_entry::hard_link_count", __ec, __m_ec);
      return __data_.__nlink_;
    }
    }
    __libcpp_unreachable();
  }

  _LIBCPP_INLINE_VISIBILITY
  file_time_type __get_write_time(error_code* __ec = nullptr) const {
    switch (__data_.__cache_type_) {
    case _Empty:
    case _IterNonSymlink:
    case _IterSymlink:
    case _RefreshSymlinkUnresolved:
      return _VSTD_FS::__last_write_time(__p_, __ec);
    case _RefreshSymlink:
    case _RefreshNonSymlink: {
      error_code __m_ec;
      file_status __st(__get_ft(&__m_ec));
      __handle_error("in directory_entry::last_write_time", __ec, __m_ec);
      if (_VSTD_FS::exists(__st) &&
          __data_.__write_time_ == file_time_type::min())
        __handle_error("in directory_entry::last_write_time", __ec,
                       make_error_code(errc::value_too_large));
      return __data_.__write_time_;
    }
    }
    __libcpp_unreachable();
  }

private:
  _Path __p_;
  __cached_data __data_;
};

class __dir_element_proxy {
public:
  inline _LIBCPP_INLINE_VISIBILITY directory_entry operator*() {
    return _VSTD::move(__elem_);
  }

private:
  friend class directory_iterator;
  friend class recursive_directory_iterator;
  explicit __dir_element_proxy(directory_entry const& __e) : __elem_(__e) {}
  __dir_element_proxy(__dir_element_proxy&& __o)
      : __elem_(_VSTD::move(__o.__elem_)) {}
  directory_entry __elem_;
};

_LIBCPP_AVAILABILITY_FILESYSTEM_POP

_LIBCPP_END_NAMESPACE_FILESYSTEM

#endif // _LIBCPP_CXX03_LANG

_LIBCPP_POP_MACROS

#endif // _LIBCPP___FILESYSTEM_DIRECTORY_ENTRY_H