Coverage Report

Created: 2021-01-23 06:44

/Users/buildslave/jenkins/workspace/coverage/llvm-project/libcxx/src/filesystem/directory_iterator.cpp
Line
Count
Source (jump to first uncovered line)
1
//===------------------ directory_iterator.cpp ----------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "filesystem"
10
#include "__config"
11
#if defined(_LIBCPP_WIN32API)
12
#define WIN32_LEAN_AND_MEAN
13
#define NOMINMAX
14
#include <windows.h>
15
#else
16
#include <dirent.h>
17
#endif
18
#include <errno.h>
19
20
#include "filesystem_common.h"
21
22
_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
23
24
namespace detail {
25
namespace {
26
27
#if !defined(_LIBCPP_WIN32API)
28
29
#if defined(DT_BLK)
30
template <class DirEntT, class = decltype(DirEntT::d_type)>
31
0
static file_type get_file_type(DirEntT* ent, int) {
32
0
  switch (ent->d_type) {
33
0
  case DT_BLK:
34
0
    return file_type::block;
35
0
  case DT_CHR:
36
0
    return file_type::character;
37
0
  case DT_DIR:
38
0
    return file_type::directory;
39
0
  case DT_FIFO:
40
0
    return file_type::fifo;
41
0
  case DT_LNK:
42
0
    return file_type::symlink;
43
0
  case DT_REG:
44
0
    return file_type::regular;
45
0
  case DT_SOCK:
46
0
    return file_type::socket;
47
  // Unlike in lstat, hitting "unknown" here simply means that the underlying
48
  // filesystem doesn't support d_type. Report is as 'none' so we correctly
49
  // set the cache to empty.
50
0
  case DT_UNKNOWN:
51
0
    break;
52
0
  }
53
0
  return file_type::none;
54
0
}
55
#endif // defined(DT_BLK)
56
57
template <class DirEntT>
58
static file_type get_file_type(DirEntT* ent, long) {
59
  return file_type::none;
60
}
61
62
static pair<string_view, file_type> posix_readdir(DIR* dir_stream,
63
0
                                                  error_code& ec) {
64
0
  struct dirent* dir_entry_ptr = nullptr;
65
0
  errno = 0; // zero errno in order to detect errors
66
0
  ec.clear();
67
0
  if ((dir_entry_ptr = ::readdir(dir_stream)) == nullptr) {
68
0
    if (errno)
69
0
      ec = capture_errno();
70
0
    return {};
71
0
  } else {
72
0
    return {dir_entry_ptr->d_name, get_file_type(dir_entry_ptr, 0)};
73
0
  }
74
0
}
75
#else
76
// defined(_LIBCPP_WIN32API)
77
78
static file_type get_file_type(const WIN32_FIND_DATAW& data) {
79
  if (data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT &&
80
      data.dwReserved0 == IO_REPARSE_TAG_SYMLINK)
81
    return file_type::symlink;
82
  if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
83
    return file_type::directory;
84
  return file_type::regular;
85
}
86
static uintmax_t get_file_size(const WIN32_FIND_DATAW& data) {
87
  return (static_cast<uint64_t>(data.nFileSizeHigh) << 32) + data.nFileSizeLow;
88
}
89
static file_time_type get_write_time(const WIN32_FIND_DATAW& data) {
90
  ULARGE_INTEGER tmp;
91
  const FILETIME& time = data.ftLastWriteTime;
92
  tmp.u.LowPart = time.dwLowDateTime;
93
  tmp.u.HighPart = time.dwHighDateTime;
94
  return file_time_type(file_time_type::duration(tmp.QuadPart));
95
}
96
97
#endif
98
99
} // namespace
100
} // namespace detail
101
102
using detail::ErrorHandler;
103
104
#if defined(_LIBCPP_WIN32API)
105
class __dir_stream {
106
public:
107
  __dir_stream() = delete;
108
  __dir_stream& operator=(const __dir_stream&) = delete;
109
110
  __dir_stream(__dir_stream&& __ds) noexcept : __stream_(__ds.__stream_),
111
                                               __root_(move(__ds.__root_)),
112
                                               __entry_(move(__ds.__entry_)) {
113
    __ds.__stream_ = INVALID_HANDLE_VALUE;
114
  }
115
116
  __dir_stream(const path& root, directory_options opts, error_code& ec)
117
      : __stream_(INVALID_HANDLE_VALUE), __root_(root) {
118
    if (root.native().empty()) {
119
      ec = make_error_code(errc::no_such_file_or_directory);
120
      return;
121
    }
122
    __stream_ = ::FindFirstFileW((root / "*").c_str(), &__data_);
123
    if (__stream_ == INVALID_HANDLE_VALUE) {
124
      ec = detail::make_windows_error(GetLastError());
125
      const bool ignore_permission_denied =
126
          bool(opts & directory_options::skip_permission_denied);
127
      if (ignore_permission_denied && ec.value() == ERROR_ACCESS_DENIED)
128
        ec.clear();
129
      return;
130
    }
131
    if (!assign())
132
      advance(ec);
133
  }
134
135
  ~__dir_stream() noexcept {
136
    if (__stream_ == INVALID_HANDLE_VALUE)
137
      return;
138
    close();
139
  }
140
141
  bool good() const noexcept { return __stream_ != INVALID_HANDLE_VALUE; }
142
143
  bool advance(error_code& ec) {
144
    while (::FindNextFileW(__stream_, &__data_)) {
145
      if (assign())
146
        return true;
147
    }
148
    close();
149
    return false;
150
  }
151
152
  bool assign() {
153
    if (!wcscmp(__data_.cFileName, L".") || !wcscmp(__data_.cFileName, L".."))
154
      return false;
155
    // FIXME: Cache more of this
156
    //directory_entry::__cached_data cdata;
157
    //cdata.__type_ = get_file_type(__data_);
158
    //cdata.__size_ = get_file_size(__data_);
159
    //cdata.__write_time_ = get_write_time(__data_);
160
    __entry_.__assign_iter_entry(
161
        __root_ / __data_.cFileName,
162
        directory_entry::__create_iter_result(detail::get_file_type(__data_)));
163
    return true;
164
  }
165
166
private:
167
  error_code close() noexcept {
168
    error_code ec;
169
    if (!::FindClose(__stream_))
170
      ec = detail::make_windows_error(GetLastError());
171
    __stream_ = INVALID_HANDLE_VALUE;
172
    return ec;
173
  }
174
175
  HANDLE __stream_{INVALID_HANDLE_VALUE};
176
  WIN32_FIND_DATAW __data_;
177
178
public:
179
  path __root_;
180
  directory_entry __entry_;
181
};
182
#else
183
class __dir_stream {
184
public:
185
  __dir_stream() = delete;
186
  __dir_stream& operator=(const __dir_stream&) = delete;
187
188
  __dir_stream(__dir_stream&& other) noexcept : __stream_(other.__stream_),
189
                                                __root_(move(other.__root_)),
190
0
                                                __entry_(move(other.__entry_)) {
191
0
    other.__stream_ = nullptr;
192
0
  }
193
194
  __dir_stream(const path& root, directory_options opts, error_code& ec)
195
0
      : __stream_(nullptr), __root_(root) {
196
0
    if ((__stream_ = ::opendir(root.c_str())) == nullptr) {
197
0
      ec = detail::capture_errno();
198
0
      const bool allow_eacess =
199
0
          bool(opts & directory_options::skip_permission_denied);
200
0
      if (allow_eacess && ec.value() == EACCES)
201
0
        ec.clear();
202
0
      return;
203
0
    }
204
0
    advance(ec);
205
0
  }
206
207
0
  ~__dir_stream() noexcept {
208
0
    if (__stream_)
209
0
      close();
210
0
  }
211
212
0
  bool good() const noexcept { return __stream_ != nullptr; }
213
214
0
  bool advance(error_code& ec) {
215
0
    while (true) {
216
0
      auto str_type_pair = detail::posix_readdir(__stream_, ec);
217
0
      auto& str = str_type_pair.first;
218
0
      if (str == "." || str == "..") {
219
0
        continue;
220
0
      } else if (ec || str.empty()) {
221
0
        close();
222
0
        return false;
223
0
      } else {
224
0
        __entry_.__assign_iter_entry(
225
0
            __root_ / str,
226
0
            directory_entry::__create_iter_result(str_type_pair.second));
227
0
        return true;
228
0
      }
229
0
    }
230
0
  }
231
232
private:
233
0
  error_code close() noexcept {
234
0
    error_code m_ec;
235
0
    if (::closedir(__stream_) == -1)
236
0
      m_ec = detail::capture_errno();
237
0
    __stream_ = nullptr;
238
0
    return m_ec;
239
0
  }
240
241
  DIR* __stream_{nullptr};
242
243
public:
244
  path __root_;
245
  directory_entry __entry_;
246
};
247
#endif
248
249
// directory_iterator
250
251
directory_iterator::directory_iterator(const path& p, error_code* ec,
252
0
                                       directory_options opts) {
253
0
  ErrorHandler<void> err("directory_iterator::directory_iterator(...)", ec, &p);
254
255
0
  error_code m_ec;
256
0
  __imp_ = make_shared<__dir_stream>(p, opts, m_ec);
257
0
  if (ec)
258
0
    *ec = m_ec;
259
0
  if (!__imp_->good()) {
260
0
    __imp_.reset();
261
0
    if (m_ec)
262
0
      err.report(m_ec);
263
0
  }
264
0
}
265
266
0
directory_iterator& directory_iterator::__increment(error_code* ec) {
267
0
  _LIBCPP_ASSERT(__imp_, "Attempting to increment an invalid iterator");
268
0
  ErrorHandler<void> err("directory_iterator::operator++()", ec);
269
270
0
  error_code m_ec;
271
0
  if (!__imp_->advance(m_ec)) {
272
0
    path root = move(__imp_->__root_);
273
0
    __imp_.reset();
274
0
    if (m_ec)
275
0
      err.report(m_ec, "at root \"%s\"", root);
276
0
  }
277
0
  return *this;
278
0
}
279
280
0
directory_entry const& directory_iterator::__dereference() const {
281
0
  _LIBCPP_ASSERT(__imp_, "Attempting to dereference an invalid iterator");
282
0
  return __imp_->__entry_;
283
0
}
284
285
// recursive_directory_iterator
286
287
struct recursive_directory_iterator::__shared_imp {
288
  stack<__dir_stream> __stack_;
289
  directory_options __options_;
290
};
291
292
recursive_directory_iterator::recursive_directory_iterator(
293
    const path& p, directory_options opt, error_code* ec)
294
0
    : __imp_(nullptr), __rec_(true) {
295
0
  ErrorHandler<void> err("recursive_directory_iterator", ec, &p);
296
297
0
  error_code m_ec;
298
0
  __dir_stream new_s(p, opt, m_ec);
299
0
  if (m_ec)
300
0
    err.report(m_ec);
301
0
  if (m_ec || !new_s.good())
302
0
    return;
303
304
0
  __imp_ = make_shared<__shared_imp>();
305
0
  __imp_->__options_ = opt;
306
0
  __imp_->__stack_.push(move(new_s));
307
0
}
308
309
0
void recursive_directory_iterator::__pop(error_code* ec) {
310
0
  _LIBCPP_ASSERT(__imp_, "Popping the end iterator");
311
0
  if (ec)
312
0
    ec->clear();
313
0
  __imp_->__stack_.pop();
314
0
  if (__imp_->__stack_.size() == 0)
315
0
    __imp_.reset();
316
0
  else
317
0
    __advance(ec);
318
0
}
319
320
0
directory_options recursive_directory_iterator::options() const {
321
0
  return __imp_->__options_;
322
0
}
323
324
0
int recursive_directory_iterator::depth() const {
325
0
  return __imp_->__stack_.size() - 1;
326
0
}
327
328
0
const directory_entry& recursive_directory_iterator::__dereference() const {
329
0
  return __imp_->__stack_.top().__entry_;
330
0
}
331
332
recursive_directory_iterator&
333
0
recursive_directory_iterator::__increment(error_code* ec) {
334
0
  if (ec)
335
0
    ec->clear();
336
0
  if (recursion_pending()) {
337
0
    if (__try_recursion(ec) || (ec && *ec))
338
0
      return *this;
339
0
  }
340
0
  __rec_ = true;
341
0
  __advance(ec);
342
0
  return *this;
343
0
}
344
345
0
void recursive_directory_iterator::__advance(error_code* ec) {
346
0
  ErrorHandler<void> err("recursive_directory_iterator::operator++()", ec);
347
348
0
  const directory_iterator end_it;
349
0
  auto& stack = __imp_->__stack_;
350
0
  error_code m_ec;
351
0
  while (stack.size() > 0) {
352
0
    if (stack.top().advance(m_ec))
353
0
      return;
354
0
    if (m_ec)
355
0
      break;
356
0
    stack.pop();
357
0
  }
358
359
0
  if (m_ec) {
360
0
    path root = move(stack.top().__root_);
361
0
    __imp_.reset();
362
0
    err.report(m_ec, "at root \"%s\"", root);
363
0
  } else {
364
0
    __imp_.reset();
365
0
  }
366
0
}
367
368
0
bool recursive_directory_iterator::__try_recursion(error_code* ec) {
369
0
  ErrorHandler<void> err("recursive_directory_iterator::operator++()", ec);
370
371
0
  bool rec_sym = bool(options() & directory_options::follow_directory_symlink);
372
373
0
  auto& curr_it = __imp_->__stack_.top();
374
375
0
  bool skip_rec = false;
376
0
  error_code m_ec;
377
0
  if (!rec_sym) {
378
0
    file_status st(curr_it.__entry_.__get_sym_ft(&m_ec));
379
0
    if (m_ec && status_known(st))
380
0
      m_ec.clear();
381
0
    if (m_ec || is_symlink(st) || !is_directory(st))
382
0
      skip_rec = true;
383
0
  } else {
384
0
    file_status st(curr_it.__entry_.__get_ft(&m_ec));
385
0
    if (m_ec && status_known(st))
386
0
      m_ec.clear();
387
0
    if (m_ec || !is_directory(st))
388
0
      skip_rec = true;
389
0
  }
390
391
0
  if (!skip_rec) {
392
0
    __dir_stream new_it(curr_it.__entry_.path(), __imp_->__options_, m_ec);
393
0
    if (new_it.good()) {
394
0
      __imp_->__stack_.push(move(new_it));
395
0
      return true;
396
0
    }
397
0
  }
398
0
  if (m_ec) {
399
0
    const bool allow_eacess =
400
0
        bool(__imp_->__options_ & directory_options::skip_permission_denied);
401
0
    if (m_ec.value() == EACCES && allow_eacess) {
402
0
      if (ec)
403
0
        ec->clear();
404
0
    } else {
405
0
      path at_ent = move(curr_it.__entry_.__p_);
406
0
      __imp_.reset();
407
0
      err.report(m_ec, "attempting recursion into \"%s\"", at_ent);
408
0
    }
409
0
  }
410
0
  return false;
411
0
}
412
413
_LIBCPP_END_NAMESPACE_FILESYSTEM