Coverage Report

Created: 2020-02-15 09:57

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