blob: 70b531e8ea3b0744014ff8c48357442976e2cbc6 [file] [log] [blame]
Eric Fiselier435db152016-06-17 19:46:40 +00001//===--------------------- filesystem/ops.cpp -----------------------------===//
2//
Chandler Carruthd2012102019-01-19 10:56:40 +00003// 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
Eric Fiselier435db152016-06-17 19:46:40 +00006//
7//===----------------------------------------------------------------------===//
8
Eric Fiselier02cea5e2018-07-27 03:07:09 +00009#include "filesystem"
Eric Fiseliera75bbde2018-07-23 02:00:52 +000010#include "array"
Eric Fiselier435db152016-06-17 19:46:40 +000011#include "iterator"
Eric Fiselier91a182b2018-04-02 23:03:41 +000012#include "string_view"
13#include "type_traits"
14#include "vector"
Eric Fiselier435db152016-06-17 19:46:40 +000015#include "cstdlib"
16#include "climits"
17
Eric Fiselier70474082018-07-20 01:22:32 +000018#include "filesystem_common.h"
Eric Fiselier42d6d2c2017-07-08 04:18:41 +000019
Martin Storsjöfc25e3a2020-10-27 13:30:34 +020020#if defined(_LIBCPP_WIN32API)
21# define WIN32_LEAN_AND_MEAN
22# define NOMINMAX
23# include <windows.h>
24#else
25# include <unistd.h>
26# include <sys/stat.h>
27# include <sys/statvfs.h>
28#endif
Eric Fiselier7eba47e2018-07-25 20:51:49 +000029#include <time.h>
Eric Fiselier02cea5e2018-07-27 03:07:09 +000030#include <fcntl.h> /* values for fchmodat */
Eric Fiselierabfdbdf2018-07-22 02:00:53 +000031
Louis Dionne27bf9862020-10-15 13:14:22 -040032#if __has_include(<sys/sendfile.h>)
33# include <sys/sendfile.h>
34# define _LIBCPP_FILESYSTEM_USE_SENDFILE
Eric Fiselierabfdbdf2018-07-22 02:00:53 +000035#elif defined(__APPLE__) || __has_include(<copyfile.h>)
Louis Dionne27bf9862020-10-15 13:14:22 -040036# include <copyfile.h>
37# define _LIBCPP_FILESYSTEM_USE_COPYFILE
38#else
39# include "fstream"
40# define _LIBCPP_FILESYSTEM_USE_FSTREAM
Eric Fiselierabfdbdf2018-07-22 02:00:53 +000041#endif
Nico Weber4f1d63a2018-02-06 19:17:41 +000042
Louis Dionne678dc852020-02-12 17:01:19 +010043#if !defined(CLOCK_REALTIME)
Louis Dionne27bf9862020-10-15 13:14:22 -040044# include <sys/time.h> // for gettimeofday and timeval
45#endif
Eric Fiselier7eba47e2018-07-25 20:51:49 +000046
Michał Górny8d676fb2019-12-02 11:49:20 +010047#if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB)
Louis Dionne27bf9862020-10-15 13:14:22 -040048# pragma comment(lib, "rt")
Eric Fiselierd8b25e32018-07-23 03:06:57 +000049#endif
50
Eric Fiselier02cea5e2018-07-27 03:07:09 +000051_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
Eric Fiselier435db152016-06-17 19:46:40 +000052
Eric Fiselier02cea5e2018-07-27 03:07:09 +000053namespace {
54namespace parser {
Eric Fiselier91a182b2018-04-02 23:03:41 +000055
56using string_view_t = path::__string_view;
57using string_view_pair = pair<string_view_t, string_view_t>;
58using PosPtr = path::value_type const*;
59
60struct PathParser {
61 enum ParserState : unsigned char {
62 // Zero is a special sentinel value used by default constructed iterators.
Eric Fiselier23a120c2018-07-25 03:31:48 +000063 PS_BeforeBegin = path::iterator::_BeforeBegin,
64 PS_InRootName = path::iterator::_InRootName,
65 PS_InRootDir = path::iterator::_InRootDir,
66 PS_InFilenames = path::iterator::_InFilenames,
67 PS_InTrailingSep = path::iterator::_InTrailingSep,
68 PS_AtEnd = path::iterator::_AtEnd
Eric Fiselier91a182b2018-04-02 23:03:41 +000069 };
70
71 const string_view_t Path;
72 string_view_t RawEntry;
73 ParserState State;
74
75private:
Eric Fiselier02cea5e2018-07-27 03:07:09 +000076 PathParser(string_view_t P, ParserState State) noexcept : Path(P),
77 State(State) {}
Eric Fiselier91a182b2018-04-02 23:03:41 +000078
79public:
80 PathParser(string_view_t P, string_view_t E, unsigned char S)
81 : Path(P), RawEntry(E), State(static_cast<ParserState>(S)) {
82 // S cannot be '0' or PS_BeforeBegin.
83 }
84
85 static PathParser CreateBegin(string_view_t P) noexcept {
86 PathParser PP(P, PS_BeforeBegin);
87 PP.increment();
88 return PP;
89 }
90
91 static PathParser CreateEnd(string_view_t P) noexcept {
92 PathParser PP(P, PS_AtEnd);
93 return PP;
94 }
95
96 PosPtr peek() const noexcept {
97 auto TkEnd = getNextTokenStartPos();
98 auto End = getAfterBack();
99 return TkEnd == End ? nullptr : TkEnd;
100 }
101
102 void increment() noexcept {
103 const PosPtr End = getAfterBack();
104 const PosPtr Start = getNextTokenStartPos();
105 if (Start == End)
106 return makeState(PS_AtEnd);
107
108 switch (State) {
109 case PS_BeforeBegin: {
110 PosPtr TkEnd = consumeSeparator(Start, End);
111 if (TkEnd)
112 return makeState(PS_InRootDir, Start, TkEnd);
113 else
114 return makeState(PS_InFilenames, Start, consumeName(Start, End));
115 }
116 case PS_InRootDir:
117 return makeState(PS_InFilenames, Start, consumeName(Start, End));
118
119 case PS_InFilenames: {
120 PosPtr SepEnd = consumeSeparator(Start, End);
121 if (SepEnd != End) {
122 PosPtr TkEnd = consumeName(SepEnd, End);
123 if (TkEnd)
124 return makeState(PS_InFilenames, SepEnd, TkEnd);
125 }
126 return makeState(PS_InTrailingSep, Start, SepEnd);
127 }
128
129 case PS_InTrailingSep:
130 return makeState(PS_AtEnd);
131
132 case PS_InRootName:
133 case PS_AtEnd:
134 _LIBCPP_UNREACHABLE();
135 }
136 }
137
138 void decrement() noexcept {
139 const PosPtr REnd = getBeforeFront();
140 const PosPtr RStart = getCurrentTokenStartPos() - 1;
141 if (RStart == REnd) // we're decrementing the begin
142 return makeState(PS_BeforeBegin);
143
144 switch (State) {
145 case PS_AtEnd: {
146 // Try to consume a trailing separator or root directory first.
147 if (PosPtr SepEnd = consumeSeparator(RStart, REnd)) {
148 if (SepEnd == REnd)
149 return makeState(PS_InRootDir, Path.data(), RStart + 1);
150 return makeState(PS_InTrailingSep, SepEnd + 1, RStart + 1);
151 } else {
152 PosPtr TkStart = consumeName(RStart, REnd);
153 return makeState(PS_InFilenames, TkStart + 1, RStart + 1);
154 }
155 }
156 case PS_InTrailingSep:
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000157 return makeState(PS_InFilenames, consumeName(RStart, REnd) + 1,
158 RStart + 1);
Eric Fiselier91a182b2018-04-02 23:03:41 +0000159 case PS_InFilenames: {
160 PosPtr SepEnd = consumeSeparator(RStart, REnd);
161 if (SepEnd == REnd)
162 return makeState(PS_InRootDir, Path.data(), RStart + 1);
163 PosPtr TkEnd = consumeName(SepEnd, REnd);
164 return makeState(PS_InFilenames, TkEnd + 1, SepEnd + 1);
165 }
166 case PS_InRootDir:
167 // return makeState(PS_InRootName, Path.data(), RStart + 1);
168 case PS_InRootName:
169 case PS_BeforeBegin:
170 _LIBCPP_UNREACHABLE();
171 }
172 }
173
174 /// \brief Return a view with the "preferred representation" of the current
175 /// element. For example trailing separators are represented as a '.'
176 string_view_t operator*() const noexcept {
177 switch (State) {
178 case PS_BeforeBegin:
179 case PS_AtEnd:
Martin Storsjöe482f4b2020-10-27 13:09:08 +0200180 return PS("");
Eric Fiselier91a182b2018-04-02 23:03:41 +0000181 case PS_InRootDir:
Martin Storsjöe482f4b2020-10-27 13:09:08 +0200182 if (RawEntry[0] == '\\')
183 return PS("\\");
184 else
185 return PS("/");
Eric Fiselier91a182b2018-04-02 23:03:41 +0000186 case PS_InTrailingSep:
Martin Storsjöe482f4b2020-10-27 13:09:08 +0200187 return PS("");
Eric Fiselier91a182b2018-04-02 23:03:41 +0000188 case PS_InRootName:
189 case PS_InFilenames:
190 return RawEntry;
191 }
192 _LIBCPP_UNREACHABLE();
193 }
194
195 explicit operator bool() const noexcept {
196 return State != PS_BeforeBegin && State != PS_AtEnd;
197 }
198
199 PathParser& operator++() noexcept {
200 increment();
201 return *this;
202 }
203
204 PathParser& operator--() noexcept {
205 decrement();
206 return *this;
207 }
208
Eric Fiselierc9a770e2018-12-21 03:16:30 +0000209 bool atEnd() const noexcept {
210 return State == PS_AtEnd;
211 }
212
213 bool inRootDir() const noexcept {
214 return State == PS_InRootDir;
215 }
216
217 bool inRootName() const noexcept {
218 return State == PS_InRootName;
219 }
220
Eric Fiselier91a182b2018-04-02 23:03:41 +0000221 bool inRootPath() const noexcept {
Eric Fiselierc9a770e2018-12-21 03:16:30 +0000222 return inRootName() || inRootDir();
Eric Fiselier91a182b2018-04-02 23:03:41 +0000223 }
224
225private:
226 void makeState(ParserState NewState, PosPtr Start, PosPtr End) noexcept {
227 State = NewState;
228 RawEntry = string_view_t(Start, End - Start);
229 }
230 void makeState(ParserState NewState) noexcept {
231 State = NewState;
232 RawEntry = {};
233 }
234
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000235 PosPtr getAfterBack() const noexcept { return Path.data() + Path.size(); }
Eric Fiselier91a182b2018-04-02 23:03:41 +0000236
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000237 PosPtr getBeforeFront() const noexcept { return Path.data() - 1; }
Eric Fiselier91a182b2018-04-02 23:03:41 +0000238
239 /// \brief Return a pointer to the first character after the currently
240 /// lexed element.
241 PosPtr getNextTokenStartPos() const noexcept {
242 switch (State) {
243 case PS_BeforeBegin:
244 return Path.data();
245 case PS_InRootName:
246 case PS_InRootDir:
247 case PS_InFilenames:
248 return &RawEntry.back() + 1;
249 case PS_InTrailingSep:
250 case PS_AtEnd:
251 return getAfterBack();
252 }
253 _LIBCPP_UNREACHABLE();
254 }
255
256 /// \brief Return a pointer to the first character in the currently lexed
257 /// element.
258 PosPtr getCurrentTokenStartPos() const noexcept {
259 switch (State) {
260 case PS_BeforeBegin:
261 case PS_InRootName:
262 return &Path.front();
263 case PS_InRootDir:
264 case PS_InFilenames:
265 case PS_InTrailingSep:
266 return &RawEntry.front();
267 case PS_AtEnd:
268 return &Path.back() + 1;
269 }
270 _LIBCPP_UNREACHABLE();
271 }
272
273 PosPtr consumeSeparator(PosPtr P, PosPtr End) const noexcept {
274 if (P == End || *P != '/')
275 return nullptr;
276 const int Inc = P < End ? 1 : -1;
277 P += Inc;
278 while (P != End && *P == '/')
279 P += Inc;
280 return P;
281 }
282
283 PosPtr consumeName(PosPtr P, PosPtr End) const noexcept {
284 if (P == End || *P == '/')
285 return nullptr;
286 const int Inc = P < End ? 1 : -1;
287 P += Inc;
288 while (P != End && *P != '/')
289 P += Inc;
290 return P;
291 }
292};
293
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000294string_view_pair separate_filename(string_view_t const& s) {
Martin Storsjöe482f4b2020-10-27 13:09:08 +0200295 if (s == PS(".") || s == PS("..") || s.empty())
296 return string_view_pair{s, PS("")};
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000297 auto pos = s.find_last_of('.');
298 if (pos == string_view_t::npos || pos == 0)
299 return string_view_pair{s, string_view_t{}};
300 return string_view_pair{s.substr(0, pos), s.substr(pos)};
Eric Fiselier91a182b2018-04-02 23:03:41 +0000301}
302
303string_view_t createView(PosPtr S, PosPtr E) noexcept {
304 return {S, static_cast<size_t>(E - S) + 1};
305}
306
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000307} // namespace parser
308} // namespace
Eric Fiselier91a182b2018-04-02 23:03:41 +0000309
Eric Fiselier435db152016-06-17 19:46:40 +0000310// POSIX HELPERS
311
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000312namespace detail {
313namespace {
Eric Fiselier435db152016-06-17 19:46:40 +0000314
315using value_type = path::value_type;
316using string_type = path::string_type;
317
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000318struct FileDescriptor {
319 const path& name;
320 int fd = -1;
321 StatT m_stat;
322 file_status m_status;
323
324 template <class... Args>
325 static FileDescriptor create(const path* p, error_code& ec, Args... args) {
326 ec.clear();
327 int fd;
328 if ((fd = ::open(p->c_str(), args...)) == -1) {
329 ec = capture_errno();
330 return FileDescriptor{p};
331 }
332 return FileDescriptor(p, fd);
333 }
334
335 template <class... Args>
336 static FileDescriptor create_with_status(const path* p, error_code& ec,
337 Args... args) {
338 FileDescriptor fd = create(p, ec, args...);
339 if (!ec)
340 fd.refresh_status(ec);
341
342 return fd;
343 }
344
345 file_status get_status() const { return m_status; }
346 StatT const& get_stat() const { return m_stat; }
347
348 bool status_known() const { return _VSTD_FS::status_known(m_status); }
349
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000350 file_status refresh_status(error_code& ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000351
352 void close() noexcept {
353 if (fd != -1)
354 ::close(fd);
355 fd = -1;
356 }
357
358 FileDescriptor(FileDescriptor&& other)
359 : name(other.name), fd(other.fd), m_stat(other.m_stat),
360 m_status(other.m_status) {
361 other.fd = -1;
362 other.m_status = file_status{};
363 }
364
365 ~FileDescriptor() { close(); }
366
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000367 FileDescriptor(FileDescriptor const&) = delete;
368 FileDescriptor& operator=(FileDescriptor const&) = delete;
369
370private:
371 explicit FileDescriptor(const path* p, int fd = -1) : name(*p), fd(fd) {}
372};
373
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000374perms posix_get_perms(const StatT& st) noexcept {
Eric Fiselier70474082018-07-20 01:22:32 +0000375 return static_cast<perms>(st.st_mode) & perms::mask;
Eric Fiselier435db152016-06-17 19:46:40 +0000376}
377
378::mode_t posix_convert_perms(perms prms) {
Eric Fiselier70474082018-07-20 01:22:32 +0000379 return static_cast< ::mode_t>(prms & perms::mask);
Eric Fiselier435db152016-06-17 19:46:40 +0000380}
381
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000382file_status create_file_status(error_code& m_ec, path const& p,
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000383 const StatT& path_stat, error_code* ec) {
Eric Fiselier70474082018-07-20 01:22:32 +0000384 if (ec)
385 *ec = m_ec;
Eric Fiselier70474082018-07-20 01:22:32 +0000386 if (m_ec && (m_ec.value() == ENOENT || m_ec.value() == ENOTDIR)) {
387 return file_status(file_type::not_found);
388 } else if (m_ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000389 ErrorHandler<void> err("posix_stat", ec, &p);
390 err.report(m_ec, "failed to determine attributes for the specified path");
Eric Fiselier70474082018-07-20 01:22:32 +0000391 return file_status(file_type::none);
392 }
393 // else
Eric Fiselier435db152016-06-17 19:46:40 +0000394
Eric Fiselier70474082018-07-20 01:22:32 +0000395 file_status fs_tmp;
396 auto const mode = path_stat.st_mode;
397 if (S_ISLNK(mode))
398 fs_tmp.type(file_type::symlink);
399 else if (S_ISREG(mode))
400 fs_tmp.type(file_type::regular);
401 else if (S_ISDIR(mode))
402 fs_tmp.type(file_type::directory);
403 else if (S_ISBLK(mode))
404 fs_tmp.type(file_type::block);
405 else if (S_ISCHR(mode))
406 fs_tmp.type(file_type::character);
407 else if (S_ISFIFO(mode))
408 fs_tmp.type(file_type::fifo);
409 else if (S_ISSOCK(mode))
410 fs_tmp.type(file_type::socket);
411 else
412 fs_tmp.type(file_type::unknown);
Eric Fiselier435db152016-06-17 19:46:40 +0000413
Eric Fiselier70474082018-07-20 01:22:32 +0000414 fs_tmp.permissions(detail::posix_get_perms(path_stat));
415 return fs_tmp;
Eric Fiselier435db152016-06-17 19:46:40 +0000416}
417
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000418file_status posix_stat(path const& p, StatT& path_stat, error_code* ec) {
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000419 error_code m_ec;
Eric Fiselier70474082018-07-20 01:22:32 +0000420 if (::stat(p.c_str(), &path_stat) == -1)
421 m_ec = detail::capture_errno();
422 return create_file_status(m_ec, p, path_stat, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000423}
424
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000425file_status posix_stat(path const& p, error_code* ec) {
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000426 StatT path_stat;
Eric Fiselier70474082018-07-20 01:22:32 +0000427 return posix_stat(p, path_stat, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000428}
429
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000430file_status posix_lstat(path const& p, StatT& path_stat, error_code* ec) {
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000431 error_code m_ec;
Eric Fiselier70474082018-07-20 01:22:32 +0000432 if (::lstat(p.c_str(), &path_stat) == -1)
433 m_ec = detail::capture_errno();
434 return create_file_status(m_ec, p, path_stat, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000435}
436
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000437file_status posix_lstat(path const& p, error_code* ec) {
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000438 StatT path_stat;
Eric Fiselier70474082018-07-20 01:22:32 +0000439 return posix_lstat(p, path_stat, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000440}
441
Dan Albert39b981d2019-01-15 19:16:25 +0000442// http://pubs.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html
443bool posix_ftruncate(const FileDescriptor& fd, off_t to_size, error_code& ec) {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000444 if (::ftruncate(fd.fd, to_size) == -1) {
445 ec = capture_errno();
Eric Fiselierf1aba0d2018-07-26 04:02:06 +0000446 return true;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000447 }
448 ec.clear();
Eric Fiselierf1aba0d2018-07-26 04:02:06 +0000449 return false;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000450}
451
452bool posix_fchmod(const FileDescriptor& fd, const StatT& st, error_code& ec) {
453 if (::fchmod(fd.fd, st.st_mode) == -1) {
454 ec = capture_errno();
Eric Fiselierf1aba0d2018-07-26 04:02:06 +0000455 return true;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000456 }
457 ec.clear();
Eric Fiselierf1aba0d2018-07-26 04:02:06 +0000458 return false;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000459}
460
461bool stat_equivalent(const StatT& st1, const StatT& st2) {
Eric Fiselier70474082018-07-20 01:22:32 +0000462 return (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino);
Eric Fiselier435db152016-06-17 19:46:40 +0000463}
464
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000465file_status FileDescriptor::refresh_status(error_code& ec) {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000466 // FD must be open and good.
467 m_status = file_status{};
Eric Fiselierd8b25e32018-07-23 03:06:57 +0000468 m_stat = {};
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000469 error_code m_ec;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000470 if (::fstat(fd, &m_stat) == -1)
471 m_ec = capture_errno();
472 m_status = create_file_status(m_ec, name, m_stat, &ec);
473 return m_status;
Eric Fiselier435db152016-06-17 19:46:40 +0000474}
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000475} // namespace
476} // end namespace detail
Eric Fiselier435db152016-06-17 19:46:40 +0000477
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000478using detail::capture_errno;
479using detail::ErrorHandler;
480using detail::StatT;
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000481using detail::TimeSpec;
Eric Fiselier91a182b2018-04-02 23:03:41 +0000482using parser::createView;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000483using parser::PathParser;
484using parser::string_view_t;
485
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000486const bool _FilesystemClock::is_steady;
487
488_FilesystemClock::time_point _FilesystemClock::now() noexcept {
489 typedef chrono::duration<rep> __secs;
Louis Dionne678dc852020-02-12 17:01:19 +0100490#if defined(CLOCK_REALTIME)
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000491 typedef chrono::duration<rep, nano> __nsecs;
492 struct timespec tp;
493 if (0 != clock_gettime(CLOCK_REALTIME, &tp))
494 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
495 return time_point(__secs(tp.tv_sec) +
496 chrono::duration_cast<duration>(__nsecs(tp.tv_nsec)));
497#else
498 typedef chrono::duration<rep, micro> __microsecs;
499 timeval tv;
500 gettimeofday(&tv, 0);
501 return time_point(__secs(tv.tv_sec) + __microsecs(tv.tv_usec));
Louis Dionne678dc852020-02-12 17:01:19 +0100502#endif // CLOCK_REALTIME
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000503}
504
505filesystem_error::~filesystem_error() {}
506
Martin Storsjöe482f4b2020-10-27 13:09:08 +0200507#if defined(_LIBCPP_WIN32API)
508#define PS_FMT "%ls"
509#else
510#define PS_FMT "%s"
511#endif
512
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000513void filesystem_error::__create_what(int __num_paths) {
514 const char* derived_what = system_error::what();
515 __storage_->__what_ = [&]() -> string {
Martin Storsjöe482f4b2020-10-27 13:09:08 +0200516 const path::value_type* p1 = path1().native().empty() ? PS("\"\"") : path1().c_str();
517 const path::value_type* p2 = path2().native().empty() ? PS("\"\"") : path2().c_str();
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000518 switch (__num_paths) {
519 default:
520 return detail::format_string("filesystem error: %s", derived_what);
521 case 1:
Martin Storsjöe482f4b2020-10-27 13:09:08 +0200522 return detail::format_string("filesystem error: %s [" PS_FMT "]", derived_what,
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000523 p1);
524 case 2:
Martin Storsjöe482f4b2020-10-27 13:09:08 +0200525 return detail::format_string("filesystem error: %s [" PS_FMT "] [" PS_FMT "]",
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000526 derived_what, p1, p2);
527 }
528 }();
529}
Eric Fiselier435db152016-06-17 19:46:40 +0000530
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000531static path __do_absolute(const path& p, path* cwd, error_code* ec) {
532 if (ec)
533 ec->clear();
534 if (p.is_absolute())
535 return p;
536 *cwd = __current_path(ec);
537 if (ec && *ec)
538 return {};
539 return (*cwd) / p;
Eric Fiselier91a182b2018-04-02 23:03:41 +0000540}
541
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000542path __absolute(const path& p, error_code* ec) {
543 path cwd;
544 return __do_absolute(p, &cwd, ec);
Eric Fiselier91a182b2018-04-02 23:03:41 +0000545}
546
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000547path __canonical(path const& orig_p, error_code* ec) {
548 path cwd;
549 ErrorHandler<path> err("canonical", ec, &orig_p, &cwd);
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000550
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000551 path p = __do_absolute(orig_p, &cwd, ec);
YAMAMOTO Takashi43f19082020-10-28 15:40:16 -0400552#if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112
Eric Fiselierb5215302019-01-17 02:59:28 +0000553 std::unique_ptr<char, decltype(&::free)>
554 hold(::realpath(p.c_str(), nullptr), &::free);
555 if (hold.get() == nullptr)
556 return err.report(capture_errno());
557 return {hold.get()};
558#else
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000559 char buff[PATH_MAX + 1];
560 char* ret;
561 if ((ret = ::realpath(p.c_str(), buff)) == nullptr)
562 return err.report(capture_errno());
563 return {ret};
Eric Fiselierb5215302019-01-17 02:59:28 +0000564#endif
Eric Fiselier435db152016-06-17 19:46:40 +0000565}
566
567void __copy(const path& from, const path& to, copy_options options,
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000568 error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000569 ErrorHandler<void> err("copy", ec, &from, &to);
Eric Fiselier435db152016-06-17 19:46:40 +0000570
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000571 const bool sym_status = bool(
572 options & (copy_options::create_symlinks | copy_options::skip_symlinks));
Eric Fiselier435db152016-06-17 19:46:40 +0000573
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000574 const bool sym_status2 = bool(options & copy_options::copy_symlinks);
Eric Fiselier435db152016-06-17 19:46:40 +0000575
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000576 error_code m_ec1;
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000577 StatT f_st = {};
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000578 const file_status f = sym_status || sym_status2
579 ? detail::posix_lstat(from, f_st, &m_ec1)
580 : detail::posix_stat(from, f_st, &m_ec1);
581 if (m_ec1)
582 return err.report(m_ec1);
Eric Fiselier435db152016-06-17 19:46:40 +0000583
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000584 StatT t_st = {};
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000585 const file_status t = sym_status ? detail::posix_lstat(to, t_st, &m_ec1)
586 : detail::posix_stat(to, t_st, &m_ec1);
Eric Fiselier435db152016-06-17 19:46:40 +0000587
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000588 if (not status_known(t))
589 return err.report(m_ec1);
590
591 if (!exists(f) || is_other(f) || is_other(t) ||
592 (is_directory(f) && is_regular_file(t)) ||
593 detail::stat_equivalent(f_st, t_st)) {
594 return err.report(errc::function_not_supported);
595 }
Eric Fiselier435db152016-06-17 19:46:40 +0000596
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000597 if (ec)
598 ec->clear();
Eric Fiselier435db152016-06-17 19:46:40 +0000599
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000600 if (is_symlink(f)) {
601 if (bool(copy_options::skip_symlinks & options)) {
602 // do nothing
603 } else if (not exists(t)) {
604 __copy_symlink(from, to, ec);
605 } else {
606 return err.report(errc::file_exists);
Eric Fiselier435db152016-06-17 19:46:40 +0000607 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000608 return;
609 } else if (is_regular_file(f)) {
610 if (bool(copy_options::directories_only & options)) {
611 // do nothing
612 } else if (bool(copy_options::create_symlinks & options)) {
613 __create_symlink(from, to, ec);
614 } else if (bool(copy_options::create_hard_links & options)) {
615 __create_hard_link(from, to, ec);
616 } else if (is_directory(t)) {
617 __copy_file(from, to / from.filename(), options, ec);
618 } else {
619 __copy_file(from, to, options, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000620 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000621 return;
622 } else if (is_directory(f) && bool(copy_options::create_symlinks & options)) {
623 return err.report(errc::is_a_directory);
624 } else if (is_directory(f) && (bool(copy_options::recursive & options) ||
625 copy_options::none == options)) {
Eric Fiselier435db152016-06-17 19:46:40 +0000626
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000627 if (!exists(t)) {
628 // create directory to with attributes from 'from'.
629 __create_directory(to, from, ec);
630 if (ec && *ec) {
631 return;
632 }
Eric Fiselier435db152016-06-17 19:46:40 +0000633 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000634 directory_iterator it =
635 ec ? directory_iterator(from, *ec) : directory_iterator(from);
636 if (ec && *ec) {
637 return;
638 }
639 error_code m_ec2;
640 for (; it != directory_iterator(); it.increment(m_ec2)) {
641 if (m_ec2) {
642 return err.report(m_ec2);
643 }
644 __copy(it->path(), to / it->path().filename(),
645 options | copy_options::__in_recursive_copy, ec);
646 if (ec && *ec) {
647 return;
648 }
649 }
650 }
Eric Fiselier435db152016-06-17 19:46:40 +0000651}
652
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000653namespace detail {
654namespace {
655
Louis Dionne27bf9862020-10-15 13:14:22 -0400656#if defined(_LIBCPP_FILESYSTEM_USE_SENDFILE)
657 bool copy_file_impl(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) {
658 size_t count = read_fd.get_stat().st_size;
659 do {
660 ssize_t res;
661 if ((res = ::sendfile(write_fd.fd, read_fd.fd, nullptr, count)) == -1) {
662 ec = capture_errno();
663 return false;
664 }
665 count -= res;
666 } while (count > 0);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000667
Louis Dionne27bf9862020-10-15 13:14:22 -0400668 ec.clear();
669
670 return true;
671 }
672#elif defined(_LIBCPP_FILESYSTEM_USE_COPYFILE)
673 bool copy_file_impl(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) {
674 struct CopyFileState {
675 copyfile_state_t state;
676 CopyFileState() { state = copyfile_state_alloc(); }
677 ~CopyFileState() { copyfile_state_free(state); }
678
679 private:
680 CopyFileState(CopyFileState const&) = delete;
681 CopyFileState& operator=(CopyFileState const&) = delete;
682 };
683
684 CopyFileState cfs;
685 if (fcopyfile(read_fd.fd, write_fd.fd, cfs.state, COPYFILE_DATA) < 0) {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000686 ec = capture_errno();
687 return false;
688 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000689
Louis Dionne27bf9862020-10-15 13:14:22 -0400690 ec.clear();
691 return true;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000692 }
Louis Dionne27bf9862020-10-15 13:14:22 -0400693#elif defined(_LIBCPP_FILESYSTEM_USE_FSTREAM)
694 bool copy_file_impl(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) {
695 ifstream in;
696 in.__open(read_fd.fd, ios::binary);
697 if (!in.is_open()) {
698 // This assumes that __open didn't reset the error code.
699 ec = capture_errno();
700 return false;
701 }
Martin Storsjö64104352020-11-02 10:19:42 +0200702 read_fd.fd = -1;
Louis Dionne27bf9862020-10-15 13:14:22 -0400703 ofstream out;
704 out.__open(write_fd.fd, ios::binary);
705 if (!out.is_open()) {
706 ec = capture_errno();
707 return false;
708 }
Martin Storsjö64104352020-11-02 10:19:42 +0200709 write_fd.fd = -1;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000710
Louis Dionne27bf9862020-10-15 13:14:22 -0400711 if (in.good() && out.good()) {
712 using InIt = istreambuf_iterator<char>;
713 using OutIt = ostreambuf_iterator<char>;
714 InIt bin(in);
715 InIt ein;
716 OutIt bout(out);
717 copy(bin, ein, bout);
718 }
719 if (out.fail() || in.fail()) {
720 ec = make_error_code(errc::io_error);
721 return false;
722 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000723
Louis Dionne27bf9862020-10-15 13:14:22 -0400724 ec.clear();
725 return true;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000726 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000727#else
Louis Dionne27bf9862020-10-15 13:14:22 -0400728# error "Unknown implementation for copy_file_impl"
729#endif // copy_file_impl implementation
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000730
Louis Dionne27bf9862020-10-15 13:14:22 -0400731} // end anonymous namespace
732} // end namespace detail
Eric Fiselier435db152016-06-17 19:46:40 +0000733
734bool __copy_file(const path& from, const path& to, copy_options options,
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000735 error_code* ec) {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000736 using detail::FileDescriptor;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000737 ErrorHandler<bool> err("copy_file", ec, &to, &from);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000738
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000739 error_code m_ec;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000740 FileDescriptor from_fd =
741 FileDescriptor::create_with_status(&from, m_ec, O_RDONLY | O_NONBLOCK);
742 if (m_ec)
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000743 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000744
745 auto from_st = from_fd.get_status();
746 StatT const& from_stat = from_fd.get_stat();
747 if (!is_regular_file(from_st)) {
748 if (not m_ec)
749 m_ec = make_error_code(errc::not_supported);
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000750 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000751 }
752
753 const bool skip_existing = bool(copy_options::skip_existing & options);
754 const bool update_existing = bool(copy_options::update_existing & options);
755 const bool overwrite_existing =
756 bool(copy_options::overwrite_existing & options);
757
758 StatT to_stat_path;
759 file_status to_st = detail::posix_stat(to, to_stat_path, &m_ec);
760 if (!status_known(to_st))
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000761 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000762
763 const bool to_exists = exists(to_st);
764 if (to_exists && !is_regular_file(to_st))
Eric Fiselier268fa832018-07-23 11:55:13 +0000765 return err.report(errc::not_supported);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000766
767 if (to_exists && detail::stat_equivalent(from_stat, to_stat_path))
Eric Fiselier268fa832018-07-23 11:55:13 +0000768 return err.report(errc::file_exists);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000769
770 if (to_exists && skip_existing)
771 return false;
772
Eric Fiselier455ac4b2018-07-22 21:15:15 +0000773 bool ShouldCopy = [&]() {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000774 if (to_exists && update_existing) {
775 auto from_time = detail::extract_mtime(from_stat);
776 auto to_time = detail::extract_mtime(to_stat_path);
777 if (from_time.tv_sec < to_time.tv_sec)
Eric Fiselier435db152016-06-17 19:46:40 +0000778 return false;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000779 if (from_time.tv_sec == to_time.tv_sec &&
780 from_time.tv_nsec <= to_time.tv_nsec)
Eric Fiseliere7359252016-10-16 00:47:59 +0000781 return false;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000782 return true;
Eric Fiseliere7359252016-10-16 00:47:59 +0000783 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000784 if (!to_exists || overwrite_existing)
785 return true;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000786 return err.report(errc::file_exists);
Eric Fiselier455ac4b2018-07-22 21:15:15 +0000787 }();
788 if (!ShouldCopy)
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000789 return false;
Eric Fiseliere7359252016-10-16 00:47:59 +0000790
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000791 // Don't truncate right away. We may not be opening the file we originally
792 // looked at; we'll check this later.
Eric Fiselier455ac4b2018-07-22 21:15:15 +0000793 int to_open_flags = O_WRONLY;
794 if (!to_exists)
795 to_open_flags |= O_CREAT;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000796 FileDescriptor to_fd = FileDescriptor::create_with_status(
797 &to, m_ec, to_open_flags, from_stat.st_mode);
798 if (m_ec)
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000799 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000800
801 if (to_exists) {
802 // Check that the file we initially stat'ed is equivalent to the one
803 // we opened.
Eric Fiselier455ac4b2018-07-22 21:15:15 +0000804 // FIXME: report this better.
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000805 if (!detail::stat_equivalent(to_stat_path, to_fd.get_stat()))
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000806 return err.report(errc::bad_file_descriptor);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000807
808 // Set the permissions and truncate the file we opened.
Eric Fiselierf1aba0d2018-07-26 04:02:06 +0000809 if (detail::posix_fchmod(to_fd, from_stat, m_ec))
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000810 return err.report(m_ec);
Eric Fiselierf1aba0d2018-07-26 04:02:06 +0000811 if (detail::posix_ftruncate(to_fd, 0, m_ec))
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000812 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000813 }
814
815 if (!copy_file_impl(from_fd, to_fd, m_ec)) {
816 // FIXME: Remove the dest file if we failed, and it didn't exist previously.
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000817 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000818 }
819
820 return true;
Eric Fiselier435db152016-06-17 19:46:40 +0000821}
822
823void __copy_symlink(const path& existing_symlink, const path& new_symlink,
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000824 error_code* ec) {
825 const path real_path(__read_symlink(existing_symlink, ec));
826 if (ec && *ec) {
827 return;
828 }
829 // NOTE: proposal says you should detect if you should call
830 // create_symlink or create_directory_symlink. I don't think this
831 // is needed with POSIX
832 __create_symlink(real_path, new_symlink, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000833}
834
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000835bool __create_directories(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000836 ErrorHandler<bool> err("create_directories", ec, &p);
Eric Fiselier435db152016-06-17 19:46:40 +0000837
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000838 error_code m_ec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000839 auto const st = detail::posix_stat(p, &m_ec);
840 if (!status_known(st))
841 return err.report(m_ec);
842 else if (is_directory(st))
843 return false;
844 else if (exists(st))
845 return err.report(errc::file_exists);
846
847 const path parent = p.parent_path();
848 if (!parent.empty()) {
849 const file_status parent_st = status(parent, m_ec);
850 if (not status_known(parent_st))
851 return err.report(m_ec);
852 if (not exists(parent_st)) {
853 __create_directories(parent, ec);
854 if (ec && *ec) {
855 return false;
856 }
Eric Fiselier435db152016-06-17 19:46:40 +0000857 }
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000858 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000859 return __create_directory(p, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000860}
861
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000862bool __create_directory(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000863 ErrorHandler<bool> err("create_directory", ec, &p);
864
865 if (::mkdir(p.c_str(), static_cast<int>(perms::all)) == 0)
866 return true;
Marek Kurdej9c129772020-12-10 08:38:41 +0100867
868 if (errno == EEXIST) {
869 error_code mec = capture_errno();
870 error_code ignored_ec;
871 const file_status st = status(p, ignored_ec);
872 if (!is_directory(st)) {
873 err.report(mec);
874 }
875 } else {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000876 err.report(capture_errno());
Marek Kurdej9c129772020-12-10 08:38:41 +0100877 }
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000878 return false;
Eric Fiselier435db152016-06-17 19:46:40 +0000879}
880
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000881bool __create_directory(path const& p, path const& attributes, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000882 ErrorHandler<bool> err("create_directory", ec, &p, &attributes);
883
884 StatT attr_stat;
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000885 error_code mec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000886 auto st = detail::posix_stat(attributes, attr_stat, &mec);
887 if (!status_known(st))
888 return err.report(mec);
Eric Fiselier7ca3db82018-07-25 04:46:32 +0000889 if (!is_directory(st))
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000890 return err.report(errc::not_a_directory,
891 "the specified attribute path is invalid");
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000892
893 if (::mkdir(p.c_str(), attr_stat.st_mode) == 0)
894 return true;
Marek Kurdej9c129772020-12-10 08:38:41 +0100895
896 if (errno == EEXIST) {
897 error_code mec = capture_errno();
898 error_code ignored_ec;
899 const file_status st = status(p, ignored_ec);
900 if (!is_directory(st)) {
901 err.report(mec);
902 }
903 } else {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000904 err.report(capture_errno());
Marek Kurdej9c129772020-12-10 08:38:41 +0100905 }
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000906 return false;
Eric Fiselier435db152016-06-17 19:46:40 +0000907}
908
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000909void __create_directory_symlink(path const& from, path const& to,
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000910 error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000911 ErrorHandler<void> err("create_directory_symlink", ec, &from, &to);
912 if (::symlink(from.c_str(), to.c_str()) != 0)
913 return err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +0000914}
915
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000916void __create_hard_link(const path& from, const path& to, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000917 ErrorHandler<void> err("create_hard_link", ec, &from, &to);
918 if (::link(from.c_str(), to.c_str()) == -1)
919 return err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +0000920}
921
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000922void __create_symlink(path const& from, path const& to, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000923 ErrorHandler<void> err("create_symlink", ec, &from, &to);
924 if (::symlink(from.c_str(), to.c_str()) == -1)
925 return err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +0000926}
927
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000928path __current_path(error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000929 ErrorHandler<path> err("current_path", ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000930
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000931 auto size = ::pathconf(".", _PC_PATH_MAX);
932 _LIBCPP_ASSERT(size >= 0, "pathconf returned a 0 as max size");
933
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000934 auto buff = unique_ptr<char[]>(new char[size + 1]);
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000935 char* ret;
936 if ((ret = ::getcwd(buff.get(), static_cast<size_t>(size))) == nullptr)
937 return err.report(capture_errno(), "call to getcwd failed");
938
939 return {buff.get()};
Eric Fiselier435db152016-06-17 19:46:40 +0000940}
941
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000942void __current_path(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000943 ErrorHandler<void> err("current_path", ec, &p);
944 if (::chdir(p.c_str()) == -1)
945 err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +0000946}
947
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000948bool __equivalent(const path& p1, const path& p2, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000949 ErrorHandler<bool> err("equivalent", ec, &p1, &p2);
950
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000951 error_code ec1, ec2;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000952 StatT st1 = {}, st2 = {};
953 auto s1 = detail::posix_stat(p1.native(), st1, &ec1);
954 if (!exists(s1))
955 return err.report(errc::not_supported);
956 auto s2 = detail::posix_stat(p2.native(), st2, &ec2);
957 if (!exists(s2))
958 return err.report(errc::not_supported);
959
960 return detail::stat_equivalent(st1, st2);
Eric Fiselier435db152016-06-17 19:46:40 +0000961}
962
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000963uintmax_t __file_size(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000964 ErrorHandler<uintmax_t> err("file_size", ec, &p);
965
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000966 error_code m_ec;
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000967 StatT st;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000968 file_status fst = detail::posix_stat(p, st, &m_ec);
969 if (!exists(fst) || !is_regular_file(fst)) {
970 errc error_kind =
971 is_directory(fst) ? errc::is_a_directory : errc::not_supported;
972 if (!m_ec)
973 m_ec = make_error_code(error_kind);
974 return err.report(m_ec);
975 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000976 // is_regular_file(p) == true
977 return static_cast<uintmax_t>(st.st_size);
Eric Fiselier435db152016-06-17 19:46:40 +0000978}
979
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000980uintmax_t __hard_link_count(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000981 ErrorHandler<uintmax_t> err("hard_link_count", ec, &p);
982
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000983 error_code m_ec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000984 StatT st;
985 detail::posix_stat(p, st, &m_ec);
986 if (m_ec)
987 return err.report(m_ec);
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000988 return static_cast<uintmax_t>(st.st_nlink);
Eric Fiselier435db152016-06-17 19:46:40 +0000989}
990
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000991bool __fs_is_empty(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000992 ErrorHandler<bool> err("is_empty", ec, &p);
Eric Fiselieraa8c61f2016-10-15 23:05:04 +0000993
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000994 error_code m_ec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000995 StatT pst;
996 auto st = detail::posix_stat(p, pst, &m_ec);
997 if (m_ec)
998 return err.report(m_ec);
999 else if (!is_directory(st) && !is_regular_file(st))
1000 return err.report(errc::not_supported);
1001 else if (is_directory(st)) {
1002 auto it = ec ? directory_iterator(p, *ec) : directory_iterator(p);
1003 if (ec && *ec)
1004 return false;
1005 return it == directory_iterator{};
1006 } else if (is_regular_file(st))
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001007 return static_cast<uintmax_t>(pst.st_size) == 0;
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001008
1009 _LIBCPP_UNREACHABLE();
Eric Fiselier435db152016-06-17 19:46:40 +00001010}
1011
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001012static file_time_type __extract_last_write_time(const path& p, const StatT& st,
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001013 error_code* ec) {
Eric Fiselier7eba47e2018-07-25 20:51:49 +00001014 using detail::fs_time;
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001015 ErrorHandler<file_time_type> err("last_write_time", ec, &p);
1016
Eric Fiselier70474082018-07-20 01:22:32 +00001017 auto ts = detail::extract_mtime(st);
Eric Fiselier7eba47e2018-07-25 20:51:49 +00001018 if (!fs_time::is_representable(ts))
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001019 return err.report(errc::value_too_large);
1020
Eric Fiselier7eba47e2018-07-25 20:51:49 +00001021 return fs_time::convert_from_timespec(ts);
Eric Fiselier70474082018-07-20 01:22:32 +00001022}
Eric Fiselier42d6d2c2017-07-08 04:18:41 +00001023
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001024file_time_type __last_write_time(const path& p, error_code* ec) {
1025 using namespace chrono;
1026 ErrorHandler<file_time_type> err("last_write_time", ec, &p);
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001027
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001028 error_code m_ec;
1029 StatT st;
1030 detail::posix_stat(p, st, &m_ec);
1031 if (m_ec)
1032 return err.report(m_ec);
1033 return __extract_last_write_time(p, st, ec);
Eric Fiselier435db152016-06-17 19:46:40 +00001034}
1035
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001036void __last_write_time(const path& p, file_time_type new_time, error_code* ec) {
1037 using detail::fs_time;
1038 ErrorHandler<void> err("last_write_time", ec, &p);
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001039
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001040 error_code m_ec;
1041 array<TimeSpec, 2> tbuf;
Eric Fiselier7eba47e2018-07-25 20:51:49 +00001042#if !defined(_LIBCPP_USE_UTIMENSAT)
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001043 // This implementation has a race condition between determining the
1044 // last access time and attempting to set it to the same value using
1045 // ::utimes
1046 StatT st;
1047 file_status fst = detail::posix_stat(p, st, &m_ec);
1048 if (m_ec)
1049 return err.report(m_ec);
1050 tbuf[0] = detail::extract_atime(st);
Eric Fiselier435db152016-06-17 19:46:40 +00001051#else
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001052 tbuf[0].tv_sec = 0;
1053 tbuf[0].tv_nsec = UTIME_OMIT;
Eric Fiselier435db152016-06-17 19:46:40 +00001054#endif
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001055 if (!fs_time::convert_to_timespec(tbuf[1], new_time))
1056 return err.report(errc::value_too_large);
Eric Fiselier70474082018-07-20 01:22:32 +00001057
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001058 detail::set_file_times(p, tbuf, m_ec);
1059 if (m_ec)
1060 return err.report(m_ec);
Eric Fiselier435db152016-06-17 19:46:40 +00001061}
1062
Eric Fiselier4f3dc0e2018-03-26 06:23:55 +00001063void __permissions(const path& p, perms prms, perm_options opts,
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001064 error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001065 ErrorHandler<void> err("permissions", ec, &p);
Eric Fiselier435db152016-06-17 19:46:40 +00001066
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001067 auto has_opt = [&](perm_options o) { return bool(o & opts); };
1068 const bool resolve_symlinks = !has_opt(perm_options::nofollow);
1069 const bool add_perms = has_opt(perm_options::add);
1070 const bool remove_perms = has_opt(perm_options::remove);
1071 _LIBCPP_ASSERT(
1072 (add_perms + remove_perms + has_opt(perm_options::replace)) == 1,
1073 "One and only one of the perm_options constants replace, add, or remove "
1074 "is present in opts");
1075
1076 bool set_sym_perms = false;
1077 prms &= perms::mask;
1078 if (!resolve_symlinks || (add_perms || remove_perms)) {
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001079 error_code m_ec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001080 file_status st = resolve_symlinks ? detail::posix_stat(p, &m_ec)
1081 : detail::posix_lstat(p, &m_ec);
1082 set_sym_perms = is_symlink(st);
1083 if (m_ec)
1084 return err.report(m_ec);
1085 _LIBCPP_ASSERT(st.permissions() != perms::unknown,
1086 "Permissions unexpectedly unknown");
1087 if (add_perms)
1088 prms |= st.permissions();
1089 else if (remove_perms)
1090 prms = st.permissions() & ~prms;
1091 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001092 const auto real_perms = detail::posix_convert_perms(prms);
Eric Fiselier435db152016-06-17 19:46:40 +00001093
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001094#if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_FDCWD)
1095 const int flags = set_sym_perms ? AT_SYMLINK_NOFOLLOW : 0;
1096 if (::fchmodat(AT_FDCWD, p.c_str(), real_perms, flags) == -1) {
1097 return err.report(capture_errno());
1098 }
1099#else
1100 if (set_sym_perms)
1101 return err.report(errc::operation_not_supported);
1102 if (::chmod(p.c_str(), real_perms) == -1) {
1103 return err.report(capture_errno());
1104 }
1105#endif
Eric Fiselier435db152016-06-17 19:46:40 +00001106}
1107
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001108path __read_symlink(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001109 ErrorHandler<path> err("read_symlink", ec, &p);
1110
Eric Fiselierb5215302019-01-17 02:59:28 +00001111#ifdef PATH_MAX
1112 struct NullDeleter { void operator()(void*) const {} };
1113 const size_t size = PATH_MAX + 1;
1114 char stack_buff[size];
1115 auto buff = std::unique_ptr<char[], NullDeleter>(stack_buff);
1116#else
1117 StatT sb;
1118 if (::lstat(p.c_str(), &sb) == -1) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001119 return err.report(capture_errno());
1120 }
Eric Fiselierb5215302019-01-17 02:59:28 +00001121 const size_t size = sb.st_size + 1;
1122 auto buff = unique_ptr<char[]>(new char[size]);
1123#endif
1124 ::ssize_t ret;
1125 if ((ret = ::readlink(p.c_str(), buff.get(), size)) == -1)
1126 return err.report(capture_errno());
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001127 _LIBCPP_ASSERT(ret > 0, "TODO");
Eric Fiselierb5215302019-01-17 02:59:28 +00001128 if (static_cast<size_t>(ret) >= size)
1129 return err.report(errc::value_too_large);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001130 buff[ret] = 0;
Eric Fiselierb5215302019-01-17 02:59:28 +00001131 return {buff.get()};
Eric Fiselier435db152016-06-17 19:46:40 +00001132}
1133
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001134bool __remove(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001135 ErrorHandler<bool> err("remove", ec, &p);
1136 if (::remove(p.c_str()) == -1) {
1137 if (errno != ENOENT)
1138 err.report(capture_errno());
1139 return false;
1140 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001141 return true;
Eric Fiselier435db152016-06-17 19:46:40 +00001142}
1143
1144namespace {
1145
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001146uintmax_t remove_all_impl(path const& p, error_code& ec) {
1147 const auto npos = static_cast<uintmax_t>(-1);
1148 const file_status st = __symlink_status(p, &ec);
1149 if (ec)
1150 return npos;
1151 uintmax_t count = 1;
1152 if (is_directory(st)) {
1153 for (directory_iterator it(p, ec); !ec && it != directory_iterator();
1154 it.increment(ec)) {
1155 auto other_count = remove_all_impl(it->path(), ec);
1156 if (ec)
1157 return npos;
1158 count += other_count;
Eric Fiselier435db152016-06-17 19:46:40 +00001159 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001160 if (ec)
1161 return npos;
1162 }
1163 if (!__remove(p, &ec))
1164 return npos;
1165 return count;
Eric Fiselier435db152016-06-17 19:46:40 +00001166}
1167
1168} // end namespace
1169
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001170uintmax_t __remove_all(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001171 ErrorHandler<uintmax_t> err("remove_all", ec, &p);
Ekaterina Vaartis52668f72018-01-11 17:04:29 +00001172
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001173 error_code mec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001174 auto count = remove_all_impl(p, mec);
1175 if (mec) {
1176 if (mec == errc::no_such_file_or_directory)
1177 return 0;
1178 return err.report(mec);
1179 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001180 return count;
Eric Fiselier435db152016-06-17 19:46:40 +00001181}
1182
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001183void __rename(const path& from, const path& to, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001184 ErrorHandler<void> err("rename", ec, &from, &to);
1185 if (::rename(from.c_str(), to.c_str()) == -1)
1186 err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +00001187}
1188
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001189void __resize_file(const path& p, uintmax_t size, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001190 ErrorHandler<void> err("resize_file", ec, &p);
1191 if (::truncate(p.c_str(), static_cast< ::off_t>(size)) == -1)
1192 return err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +00001193}
1194
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001195space_info __space(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001196 ErrorHandler<void> err("space", ec, &p);
1197 space_info si;
1198 struct statvfs m_svfs = {};
1199 if (::statvfs(p.c_str(), &m_svfs) == -1) {
1200 err.report(capture_errno());
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001201 si.capacity = si.free = si.available = static_cast<uintmax_t>(-1);
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001202 return si;
1203 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001204 // Multiply with overflow checking.
1205 auto do_mult = [&](uintmax_t& out, uintmax_t other) {
1206 out = other * m_svfs.f_frsize;
1207 if (other == 0 || out / other != m_svfs.f_frsize)
1208 out = static_cast<uintmax_t>(-1);
1209 };
1210 do_mult(si.capacity, m_svfs.f_blocks);
1211 do_mult(si.free, m_svfs.f_bfree);
1212 do_mult(si.available, m_svfs.f_bavail);
1213 return si;
Eric Fiselier435db152016-06-17 19:46:40 +00001214}
1215
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001216file_status __status(const path& p, error_code* ec) {
1217 return detail::posix_stat(p, ec);
Eric Fiselier435db152016-06-17 19:46:40 +00001218}
1219
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001220file_status __symlink_status(const path& p, error_code* ec) {
1221 return detail::posix_lstat(p, ec);
Eric Fiselier435db152016-06-17 19:46:40 +00001222}
1223
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001224path __temp_directory_path(error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001225 ErrorHandler<path> err("temp_directory_path", ec);
1226
Saleem Abdulrasoolcf279a52017-02-05 17:21:52 +00001227 const char* env_paths[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"};
1228 const char* ret = nullptr;
1229
1230 for (auto& ep : env_paths)
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001231 if ((ret = getenv(ep)))
Saleem Abdulrasoolcf279a52017-02-05 17:21:52 +00001232 break;
1233 if (ret == nullptr)
1234 ret = "/tmp";
1235
1236 path p(ret);
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001237 error_code m_ec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001238 file_status st = detail::posix_stat(p, &m_ec);
1239 if (!status_known(st))
Martin Storsjöe482f4b2020-10-27 13:09:08 +02001240 return err.report(m_ec, "cannot access path \"" PS_FMT "\"", p);
Saleem Abdulrasoolcf279a52017-02-05 17:21:52 +00001241
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001242 if (!exists(st) || !is_directory(st))
Martin Storsjöe482f4b2020-10-27 13:09:08 +02001243 return err.report(errc::not_a_directory, "path \"" PS_FMT "\" is not a directory",
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001244 p);
1245
Saleem Abdulrasoolcf279a52017-02-05 17:21:52 +00001246 return p;
Eric Fiselier435db152016-06-17 19:46:40 +00001247}
1248
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001249path __weakly_canonical(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001250 ErrorHandler<path> err("weakly_canonical", ec, &p);
1251
Eric Fiselier91a182b2018-04-02 23:03:41 +00001252 if (p.empty())
1253 return __canonical("", ec);
Eric Fiselier435db152016-06-17 19:46:40 +00001254
Eric Fiselier91a182b2018-04-02 23:03:41 +00001255 path result;
1256 path tmp;
1257 tmp.__reserve(p.native().size());
1258 auto PP = PathParser::CreateEnd(p.native());
1259 --PP;
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001260 vector<string_view_t> DNEParts;
Eric Fiselier435db152016-06-17 19:46:40 +00001261
Eric Fiselier91a182b2018-04-02 23:03:41 +00001262 while (PP.State != PathParser::PS_BeforeBegin) {
1263 tmp.assign(createView(p.native().data(), &PP.RawEntry.back()));
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001264 error_code m_ec;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001265 file_status st = __status(tmp, &m_ec);
1266 if (!status_known(st)) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001267 return err.report(m_ec);
Eric Fiselier91a182b2018-04-02 23:03:41 +00001268 } else if (exists(st)) {
1269 result = __canonical(tmp, ec);
1270 break;
Eric Fiselier435db152016-06-17 19:46:40 +00001271 }
Eric Fiselier91a182b2018-04-02 23:03:41 +00001272 DNEParts.push_back(*PP);
1273 --PP;
1274 }
1275 if (PP.State == PathParser::PS_BeforeBegin)
1276 result = __canonical("", ec);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001277 if (ec)
1278 ec->clear();
Eric Fiselier91a182b2018-04-02 23:03:41 +00001279 if (DNEParts.empty())
1280 return result;
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001281 for (auto It = DNEParts.rbegin(); It != DNEParts.rend(); ++It)
Eric Fiselier91a182b2018-04-02 23:03:41 +00001282 result /= *It;
1283 return result.lexically_normal();
Eric Fiselier435db152016-06-17 19:46:40 +00001284}
1285
Eric Fiselier91a182b2018-04-02 23:03:41 +00001286///////////////////////////////////////////////////////////////////////////////
1287// path definitions
1288///////////////////////////////////////////////////////////////////////////////
1289
1290constexpr path::value_type path::preferred_separator;
1291
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001292path& path::replace_extension(path const& replacement) {
1293 path p = extension();
1294 if (not p.empty()) {
1295 __pn_.erase(__pn_.size() - p.native().size());
1296 }
1297 if (!replacement.empty()) {
1298 if (replacement.native()[0] != '.') {
Martin Storsjöe482f4b2020-10-27 13:09:08 +02001299 __pn_ += PS(".");
Eric Fiselier91a182b2018-04-02 23:03:41 +00001300 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001301 __pn_.append(replacement.__pn_);
1302 }
1303 return *this;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001304}
1305
1306///////////////////////////////////////////////////////////////////////////////
1307// path.decompose
1308
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001309string_view_t path::__root_name() const {
1310 auto PP = PathParser::CreateBegin(__pn_);
1311 if (PP.State == PathParser::PS_InRootName)
1312 return *PP;
1313 return {};
Eric Fiselier91a182b2018-04-02 23:03:41 +00001314}
1315
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001316string_view_t path::__root_directory() const {
1317 auto PP = PathParser::CreateBegin(__pn_);
1318 if (PP.State == PathParser::PS_InRootName)
1319 ++PP;
1320 if (PP.State == PathParser::PS_InRootDir)
1321 return *PP;
1322 return {};
1323}
1324
1325string_view_t path::__root_path_raw() const {
1326 auto PP = PathParser::CreateBegin(__pn_);
1327 if (PP.State == PathParser::PS_InRootName) {
1328 auto NextCh = PP.peek();
1329 if (NextCh && *NextCh == '/') {
Eric Fiselier91a182b2018-04-02 23:03:41 +00001330 ++PP;
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001331 return createView(__pn_.data(), &PP.RawEntry.back());
Eric Fiselier91a182b2018-04-02 23:03:41 +00001332 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001333 return PP.RawEntry;
1334 }
1335 if (PP.State == PathParser::PS_InRootDir)
1336 return *PP;
1337 return {};
Eric Fiselier91a182b2018-04-02 23:03:41 +00001338}
1339
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001340static bool ConsumeRootName(PathParser *PP) {
1341 static_assert(PathParser::PS_BeforeBegin == 1 &&
1342 PathParser::PS_InRootName == 2,
1343 "Values for enums are incorrect");
1344 while (PP->State <= PathParser::PS_InRootName)
1345 ++(*PP);
1346 return PP->State == PathParser::PS_AtEnd;
1347}
1348
Eric Fiselier91a182b2018-04-02 23:03:41 +00001349static bool ConsumeRootDir(PathParser* PP) {
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001350 static_assert(PathParser::PS_BeforeBegin == 1 &&
1351 PathParser::PS_InRootName == 2 &&
1352 PathParser::PS_InRootDir == 3, "Values for enums are incorrect");
Eric Fiselier91a182b2018-04-02 23:03:41 +00001353 while (PP->State <= PathParser::PS_InRootDir)
1354 ++(*PP);
1355 return PP->State == PathParser::PS_AtEnd;
1356}
1357
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001358string_view_t path::__relative_path() const {
1359 auto PP = PathParser::CreateBegin(__pn_);
1360 if (ConsumeRootDir(&PP))
1361 return {};
1362 return createView(PP.RawEntry.data(), &__pn_.back());
1363}
1364
1365string_view_t path::__parent_path() const {
1366 if (empty())
1367 return {};
1368 // Determine if we have a root path but not a relative path. In that case
1369 // return *this.
1370 {
Eric Fiselier91a182b2018-04-02 23:03:41 +00001371 auto PP = PathParser::CreateBegin(__pn_);
1372 if (ConsumeRootDir(&PP))
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001373 return __pn_;
1374 }
1375 // Otherwise remove a single element from the end of the path, and return
1376 // a string representing that path
1377 {
1378 auto PP = PathParser::CreateEnd(__pn_);
1379 --PP;
1380 if (PP.RawEntry.data() == __pn_.data())
Eric Fiselier91a182b2018-04-02 23:03:41 +00001381 return {};
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001382 --PP;
1383 return createView(__pn_.data(), &PP.RawEntry.back());
1384 }
Eric Fiselier91a182b2018-04-02 23:03:41 +00001385}
1386
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001387string_view_t path::__filename() const {
1388 if (empty())
1389 return {};
1390 {
1391 PathParser PP = PathParser::CreateBegin(__pn_);
1392 if (ConsumeRootDir(&PP))
Eric Fiselier91a182b2018-04-02 23:03:41 +00001393 return {};
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001394 }
1395 return *(--PathParser::CreateEnd(__pn_));
Eric Fiselier91a182b2018-04-02 23:03:41 +00001396}
1397
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001398string_view_t path::__stem() const {
1399 return parser::separate_filename(__filename()).first;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001400}
1401
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001402string_view_t path::__extension() const {
1403 return parser::separate_filename(__filename()).second;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001404}
1405
1406////////////////////////////////////////////////////////////////////////////
1407// path.gen
1408
Eric Fiselier91a182b2018-04-02 23:03:41 +00001409enum PathPartKind : unsigned char {
1410 PK_None,
1411 PK_RootSep,
1412 PK_Filename,
1413 PK_Dot,
1414 PK_DotDot,
1415 PK_TrailingSep
1416};
1417
1418static PathPartKind ClassifyPathPart(string_view_t Part) {
1419 if (Part.empty())
1420 return PK_TrailingSep;
Martin Storsjöe482f4b2020-10-27 13:09:08 +02001421 if (Part == PS("."))
Eric Fiselier91a182b2018-04-02 23:03:41 +00001422 return PK_Dot;
Martin Storsjöe482f4b2020-10-27 13:09:08 +02001423 if (Part == PS(".."))
Eric Fiselier91a182b2018-04-02 23:03:41 +00001424 return PK_DotDot;
Martin Storsjöe482f4b2020-10-27 13:09:08 +02001425 if (Part == PS("/"))
Eric Fiselier91a182b2018-04-02 23:03:41 +00001426 return PK_RootSep;
1427 return PK_Filename;
1428}
1429
1430path path::lexically_normal() const {
1431 if (__pn_.empty())
1432 return *this;
1433
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001434 using PartKindPair = pair<string_view_t, PathPartKind>;
1435 vector<PartKindPair> Parts;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001436 // Guess as to how many elements the path has to avoid reallocating.
1437 Parts.reserve(32);
1438
1439 // Track the total size of the parts as we collect them. This allows the
1440 // resulting path to reserve the correct amount of memory.
1441 size_t NewPathSize = 0;
1442 auto AddPart = [&](PathPartKind K, string_view_t P) {
1443 NewPathSize += P.size();
1444 Parts.emplace_back(P, K);
1445 };
1446 auto LastPartKind = [&]() {
1447 if (Parts.empty())
1448 return PK_None;
1449 return Parts.back().second;
1450 };
1451
1452 bool MaybeNeedTrailingSep = false;
1453 // Build a stack containing the remaining elements of the path, popping off
1454 // elements which occur before a '..' entry.
1455 for (auto PP = PathParser::CreateBegin(__pn_); PP; ++PP) {
1456 auto Part = *PP;
1457 PathPartKind Kind = ClassifyPathPart(Part);
1458 switch (Kind) {
1459 case PK_Filename:
1460 case PK_RootSep: {
1461 // Add all non-dot and non-dot-dot elements to the stack of elements.
1462 AddPart(Kind, Part);
1463 MaybeNeedTrailingSep = false;
1464 break;
1465 }
1466 case PK_DotDot: {
1467 // Only push a ".." element if there are no elements preceding the "..",
1468 // or if the preceding element is itself "..".
1469 auto LastKind = LastPartKind();
1470 if (LastKind == PK_Filename) {
1471 NewPathSize -= Parts.back().first.size();
1472 Parts.pop_back();
1473 } else if (LastKind != PK_RootSep)
Martin Storsjöe482f4b2020-10-27 13:09:08 +02001474 AddPart(PK_DotDot, PS(".."));
Eric Fiselier91a182b2018-04-02 23:03:41 +00001475 MaybeNeedTrailingSep = LastKind == PK_Filename;
1476 break;
1477 }
1478 case PK_Dot:
1479 case PK_TrailingSep: {
1480 MaybeNeedTrailingSep = true;
1481 break;
1482 }
1483 case PK_None:
1484 _LIBCPP_UNREACHABLE();
1485 }
1486 }
1487 // [fs.path.generic]p6.8: If the path is empty, add a dot.
1488 if (Parts.empty())
Martin Storsjöe482f4b2020-10-27 13:09:08 +02001489 return PS(".");
Eric Fiselier91a182b2018-04-02 23:03:41 +00001490
1491 // [fs.path.generic]p6.7: If the last filename is dot-dot, remove any
1492 // trailing directory-separator.
1493 bool NeedTrailingSep = MaybeNeedTrailingSep && LastPartKind() == PK_Filename;
1494
1495 path Result;
1496 Result.__pn_.reserve(Parts.size() + NewPathSize + NeedTrailingSep);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001497 for (auto& PK : Parts)
Eric Fiselier91a182b2018-04-02 23:03:41 +00001498 Result /= PK.first;
1499
1500 if (NeedTrailingSep)
Martin Storsjöe482f4b2020-10-27 13:09:08 +02001501 Result /= PS("");
Eric Fiselier91a182b2018-04-02 23:03:41 +00001502
1503 return Result;
1504}
1505
1506static int DetermineLexicalElementCount(PathParser PP) {
1507 int Count = 0;
1508 for (; PP; ++PP) {
1509 auto Elem = *PP;
Martin Storsjöe482f4b2020-10-27 13:09:08 +02001510 if (Elem == PS(".."))
Eric Fiselier91a182b2018-04-02 23:03:41 +00001511 --Count;
Martin Storsjöe482f4b2020-10-27 13:09:08 +02001512 else if (Elem != PS(".") && Elem != PS(""))
Eric Fiselier91a182b2018-04-02 23:03:41 +00001513 ++Count;
1514 }
1515 return Count;
1516}
1517
1518path path::lexically_relative(const path& base) const {
1519 { // perform root-name/root-directory mismatch checks
1520 auto PP = PathParser::CreateBegin(__pn_);
1521 auto PPBase = PathParser::CreateBegin(base.__pn_);
1522 auto CheckIterMismatchAtBase = [&]() {
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001523 return PP.State != PPBase.State &&
1524 (PP.inRootPath() || PPBase.inRootPath());
Eric Fiselier91a182b2018-04-02 23:03:41 +00001525 };
Eric Fiselier9c4949a2018-12-21 04:25:40 +00001526 if (PP.inRootName() && PPBase.inRootName()) {
Eric Fiselier91a182b2018-04-02 23:03:41 +00001527 if (*PP != *PPBase)
1528 return {};
1529 } else if (CheckIterMismatchAtBase())
1530 return {};
1531
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001532 if (PP.inRootPath())
1533 ++PP;
1534 if (PPBase.inRootPath())
1535 ++PPBase;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001536 if (CheckIterMismatchAtBase())
1537 return {};
1538 }
1539
1540 // Find the first mismatching element
1541 auto PP = PathParser::CreateBegin(__pn_);
1542 auto PPBase = PathParser::CreateBegin(base.__pn_);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001543 while (PP && PPBase && PP.State == PPBase.State && *PP == *PPBase) {
Eric Fiselier91a182b2018-04-02 23:03:41 +00001544 ++PP;
1545 ++PPBase;
1546 }
1547
1548 // If there is no mismatch, return ".".
1549 if (!PP && !PPBase)
1550 return ".";
1551
1552 // Otherwise, determine the number of elements, 'n', which are not dot or
1553 // dot-dot minus the number of dot-dot elements.
1554 int ElemCount = DetermineLexicalElementCount(PPBase);
1555 if (ElemCount < 0)
1556 return {};
1557
Eric Fiselier9c4949a2018-12-21 04:25:40 +00001558 // if n == 0 and (a == end() || a->empty()), returns path("."); otherwise
Martin Storsjöe482f4b2020-10-27 13:09:08 +02001559 if (ElemCount == 0 && (PP.atEnd() || *PP == PS("")))
1560 return PS(".");
Eric Fiselier9c4949a2018-12-21 04:25:40 +00001561
Eric Fiselier91a182b2018-04-02 23:03:41 +00001562 // return a path constructed with 'n' dot-dot elements, followed by the the
1563 // elements of '*this' after the mismatch.
1564 path Result;
1565 // FIXME: Reserve enough room in Result that it won't have to re-allocate.
1566 while (ElemCount--)
Martin Storsjöe482f4b2020-10-27 13:09:08 +02001567 Result /= PS("..");
Eric Fiselier91a182b2018-04-02 23:03:41 +00001568 for (; PP; ++PP)
1569 Result /= *PP;
1570 return Result;
1571}
1572
1573////////////////////////////////////////////////////////////////////////////
1574// path.comparisons
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001575static int CompareRootName(PathParser *LHS, PathParser *RHS) {
1576 if (!LHS->inRootName() && !RHS->inRootName())
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001577 return 0;
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001578
1579 auto GetRootName = [](PathParser *Parser) -> string_view_t {
Martin Storsjöe482f4b2020-10-27 13:09:08 +02001580 return Parser->inRootName() ? **Parser : PS("");
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001581 };
1582 int res = GetRootName(LHS).compare(GetRootName(RHS));
1583 ConsumeRootName(LHS);
1584 ConsumeRootName(RHS);
1585 return res;
1586}
1587
1588static int CompareRootDir(PathParser *LHS, PathParser *RHS) {
1589 if (!LHS->inRootDir() && RHS->inRootDir())
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001590 return -1;
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001591 else if (LHS->inRootDir() && !RHS->inRootDir())
1592 return 1;
1593 else {
1594 ConsumeRootDir(LHS);
1595 ConsumeRootDir(RHS);
1596 return 0;
1597 }
1598}
1599
1600static int CompareRelative(PathParser *LHSPtr, PathParser *RHSPtr) {
1601 auto &LHS = *LHSPtr;
1602 auto &RHS = *RHSPtr;
Stephan T. Lavavejfb39ad72019-10-23 11:45:36 -07001603
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001604 int res;
1605 while (LHS && RHS) {
1606 if ((res = (*LHS).compare(*RHS)) != 0)
1607 return res;
1608 ++LHS;
1609 ++RHS;
1610 }
1611 return 0;
1612}
1613
1614static int CompareEndState(PathParser *LHS, PathParser *RHS) {
1615 if (LHS->atEnd() && !RHS->atEnd())
1616 return -1;
1617 else if (!LHS->atEnd() && RHS->atEnd())
1618 return 1;
1619 return 0;
1620}
1621
1622int path::__compare(string_view_t __s) const {
1623 auto LHS = PathParser::CreateBegin(__pn_);
1624 auto RHS = PathParser::CreateBegin(__s);
1625 int res;
1626
1627 if ((res = CompareRootName(&LHS, &RHS)) != 0)
1628 return res;
1629
1630 if ((res = CompareRootDir(&LHS, &RHS)) != 0)
1631 return res;
1632
1633 if ((res = CompareRelative(&LHS, &RHS)) != 0)
1634 return res;
1635
1636 return CompareEndState(&LHS, &RHS);
Eric Fiselier91a182b2018-04-02 23:03:41 +00001637}
1638
1639////////////////////////////////////////////////////////////////////////////
1640// path.nonmembers
1641size_t hash_value(const path& __p) noexcept {
1642 auto PP = PathParser::CreateBegin(__p.native());
1643 size_t hash_value = 0;
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001644 hash<string_view_t> hasher;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001645 while (PP) {
1646 hash_value = __hash_combine(hash_value, hasher(*PP));
1647 ++PP;
1648 }
1649 return hash_value;
1650}
1651
1652////////////////////////////////////////////////////////////////////////////
1653// path.itr
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001654path::iterator path::begin() const {
1655 auto PP = PathParser::CreateBegin(__pn_);
1656 iterator it;
1657 it.__path_ptr_ = this;
1658 it.__state_ = static_cast<path::iterator::_ParserState>(PP.State);
1659 it.__entry_ = PP.RawEntry;
1660 it.__stashed_elem_.__assign_view(*PP);
1661 return it;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001662}
1663
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001664path::iterator path::end() const {
1665 iterator it{};
1666 it.__state_ = path::iterator::_AtEnd;
1667 it.__path_ptr_ = this;
1668 return it;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001669}
1670
1671path::iterator& path::iterator::__increment() {
Eric Fiselier91a182b2018-04-02 23:03:41 +00001672 PathParser PP(__path_ptr_->native(), __entry_, __state_);
1673 ++PP;
Eric Fiselier23a120c2018-07-25 03:31:48 +00001674 __state_ = static_cast<_ParserState>(PP.State);
Eric Fiselier91a182b2018-04-02 23:03:41 +00001675 __entry_ = PP.RawEntry;
1676 __stashed_elem_.__assign_view(*PP);
1677 return *this;
1678}
1679
1680path::iterator& path::iterator::__decrement() {
1681 PathParser PP(__path_ptr_->native(), __entry_, __state_);
1682 --PP;
Eric Fiselier23a120c2018-07-25 03:31:48 +00001683 __state_ = static_cast<_ParserState>(PP.State);
Eric Fiselier91a182b2018-04-02 23:03:41 +00001684 __entry_ = PP.RawEntry;
1685 __stashed_elem_.__assign_view(*PP);
1686 return *this;
1687}
1688
Martin Storsjöfc25e3a2020-10-27 13:30:34 +02001689#if defined(_LIBCPP_WIN32API)
1690////////////////////////////////////////////////////////////////////////////
1691// Windows path conversions
1692size_t __wide_to_char(const wstring &str, char *out, size_t outlen) {
1693 if (str.empty())
1694 return 0;
1695 ErrorHandler<size_t> err("__wide_to_char", nullptr);
1696 UINT codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
1697 BOOL used_default = FALSE;
1698 int ret = WideCharToMultiByte(codepage, 0, str.data(), str.size(), out,
1699 outlen, nullptr, &used_default);
1700 if (ret <= 0 || used_default)
1701 return err.report(errc::illegal_byte_sequence);
1702 return ret;
1703}
1704
1705size_t __char_to_wide(const string &str, wchar_t *out, size_t outlen) {
1706 if (str.empty())
1707 return 0;
1708 ErrorHandler<size_t> err("__char_to_wide", nullptr);
1709 UINT codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
1710 int ret = MultiByteToWideChar(codepage, MB_ERR_INVALID_CHARS, str.data(),
1711 str.size(), out, outlen);
1712 if (ret <= 0)
1713 return err.report(errc::illegal_byte_sequence);
1714 return ret;
1715}
1716#endif
1717
1718
Eric Fiselier70474082018-07-20 01:22:32 +00001719///////////////////////////////////////////////////////////////////////////////
1720// directory entry definitions
1721///////////////////////////////////////////////////////////////////////////////
1722
1723#ifndef _LIBCPP_WIN32API
1724error_code directory_entry::__do_refresh() noexcept {
1725 __data_.__reset();
1726 error_code failure_ec;
1727
Eric Fiselier7eba47e2018-07-25 20:51:49 +00001728 StatT full_st;
Eric Fiselier70474082018-07-20 01:22:32 +00001729 file_status st = detail::posix_lstat(__p_, full_st, &failure_ec);
1730 if (!status_known(st)) {
1731 __data_.__reset();
1732 return failure_ec;
1733 }
1734
1735 if (!_VSTD_FS::exists(st) || !_VSTD_FS::is_symlink(st)) {
1736 __data_.__cache_type_ = directory_entry::_RefreshNonSymlink;
1737 __data_.__type_ = st.type();
1738 __data_.__non_sym_perms_ = st.permissions();
1739 } else { // we have a symlink
1740 __data_.__sym_perms_ = st.permissions();
1741 // Get the information about the linked entity.
1742 // Ignore errors from stat, since we don't want errors regarding symlink
1743 // resolution to be reported to the user.
1744 error_code ignored_ec;
1745 st = detail::posix_stat(__p_, full_st, &ignored_ec);
1746
1747 __data_.__type_ = st.type();
1748 __data_.__non_sym_perms_ = st.permissions();
1749
1750 // If we failed to resolve the link, then only partially populate the
1751 // cache.
1752 if (!status_known(st)) {
1753 __data_.__cache_type_ = directory_entry::_RefreshSymlinkUnresolved;
1754 return error_code{};
1755 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001756 // Otherwise, we resolved the link, potentially as not existing.
Eric Fiseliere39cea92018-07-20 08:36:45 +00001757 // That's OK.
Eric Fiselier70474082018-07-20 01:22:32 +00001758 __data_.__cache_type_ = directory_entry::_RefreshSymlink;
1759 }
1760
1761 if (_VSTD_FS::is_regular_file(st))
1762 __data_.__size_ = static_cast<uintmax_t>(full_st.st_size);
1763
1764 if (_VSTD_FS::exists(st)) {
1765 __data_.__nlink_ = static_cast<uintmax_t>(full_st.st_nlink);
1766
1767 // Attempt to extract the mtime, and fail if it's not representable using
1768 // file_time_type. For now we ignore the error, as we'll report it when
1769 // the value is actually used.
1770 error_code ignored_ec;
1771 __data_.__write_time_ =
1772 __extract_last_write_time(__p_, full_st, &ignored_ec);
1773 }
1774
1775 return failure_ec;
1776}
1777#else
1778error_code directory_entry::__do_refresh() noexcept {
1779 __data_.__reset();
1780 error_code failure_ec;
1781
1782 file_status st = _VSTD_FS::symlink_status(__p_, failure_ec);
1783 if (!status_known(st)) {
1784 __data_.__reset();
1785 return failure_ec;
1786 }
1787
1788 if (!_VSTD_FS::exists(st) || !_VSTD_FS::is_symlink(st)) {
1789 __data_.__cache_type_ = directory_entry::_RefreshNonSymlink;
1790 __data_.__type_ = st.type();
1791 __data_.__non_sym_perms_ = st.permissions();
1792 } else { // we have a symlink
1793 __data_.__sym_perms_ = st.permissions();
1794 // Get the information about the linked entity.
1795 // Ignore errors from stat, since we don't want errors regarding symlink
1796 // resolution to be reported to the user.
1797 error_code ignored_ec;
1798 st = _VSTD_FS::status(__p_, ignored_ec);
1799
1800 __data_.__type_ = st.type();
1801 __data_.__non_sym_perms_ = st.permissions();
1802
1803 // If we failed to resolve the link, then only partially populate the
1804 // cache.
1805 if (!status_known(st)) {
1806 __data_.__cache_type_ = directory_entry::_RefreshSymlinkUnresolved;
1807 return error_code{};
1808 }
Eric Fiselier70474082018-07-20 01:22:32 +00001809 __data_.__cache_type_ = directory_entry::_RefreshSymlink;
1810 }
1811
1812 // FIXME: This is currently broken, and the implementation only a placeholder.
1813 // We need to cache last_write_time, file_size, and hard_link_count here before
1814 // the implementation actually works.
1815
1816 return failure_ec;
1817}
1818#endif
Eric Fiselier91a182b2018-04-02 23:03:41 +00001819
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001820_LIBCPP_END_NAMESPACE_FILESYSTEM