blob: 08a6b2b86e26a46f46c5df4ad0e19091999c0b7a [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"
12#include "fstream"
Eric Fiselier02cea5e2018-07-27 03:07:09 +000013#include "random" /* for unique_path */
Eric Fiselier91a182b2018-04-02 23:03:41 +000014#include "string_view"
15#include "type_traits"
16#include "vector"
Eric Fiselier435db152016-06-17 19:46:40 +000017#include "cstdlib"
18#include "climits"
19
Eric Fiselier70474082018-07-20 01:22:32 +000020#include "filesystem_common.h"
Eric Fiselier42d6d2c2017-07-08 04:18:41 +000021
Eric Fiselier435db152016-06-17 19:46:40 +000022#include <unistd.h>
23#include <sys/stat.h>
24#include <sys/statvfs.h>
Eric Fiselier7eba47e2018-07-25 20:51:49 +000025#include <time.h>
Eric Fiselier02cea5e2018-07-27 03:07:09 +000026#include <fcntl.h> /* values for fchmodat */
Eric Fiselierabfdbdf2018-07-22 02:00:53 +000027
28#if defined(__linux__)
Eric Fiselier02cea5e2018-07-27 03:07:09 +000029#include <linux/version.h>
30#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33)
31#include <sys/sendfile.h>
32#define _LIBCPP_USE_SENDFILE
33#endif
Eric Fiselierabfdbdf2018-07-22 02:00:53 +000034#elif defined(__APPLE__) || __has_include(<copyfile.h>)
35#include <copyfile.h>
Eric Fiselier02cea5e2018-07-27 03:07:09 +000036#define _LIBCPP_USE_COPYFILE
Eric Fiselierabfdbdf2018-07-22 02:00:53 +000037#endif
Nico Weber4f1d63a2018-02-06 19:17:41 +000038
Eric Fiselier7eba47e2018-07-25 20:51:49 +000039#if !defined(__APPLE__)
40#define _LIBCPP_USE_CLOCK_GETTIME
41#endif
42
43#if !defined(CLOCK_REALTIME) || !defined(_LIBCPP_USE_CLOCK_GETTIME)
44#include <sys/time.h> // for gettimeofday and timeval
45#endif // !defined(CLOCK_REALTIME)
46
Yi Kong24e58c02019-07-22 20:41:03 +000047#if defined(__unix__) && defined(__ELF__) && defined(_LIBCPP_HAS_COMMENT_LIB_PRAGMA)
Petr Hosek99575aa2019-05-30 01:34:41 +000048#pragma comment(lib, "rt")
49#endif
50
Eric Fiselierd8b25e32018-07-23 03:06:57 +000051#if defined(_LIBCPP_COMPILER_GCC)
52#if _GNUC_VER < 500
53#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
54#endif
55#endif
56
Eric Fiselier02cea5e2018-07-27 03:07:09 +000057_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
Eric Fiselier435db152016-06-17 19:46:40 +000058
Eric Fiselier02cea5e2018-07-27 03:07:09 +000059namespace {
60namespace parser {
Eric Fiselier91a182b2018-04-02 23:03:41 +000061
62using string_view_t = path::__string_view;
63using string_view_pair = pair<string_view_t, string_view_t>;
64using PosPtr = path::value_type const*;
65
66struct PathParser {
67 enum ParserState : unsigned char {
68 // Zero is a special sentinel value used by default constructed iterators.
Eric Fiselier23a120c2018-07-25 03:31:48 +000069 PS_BeforeBegin = path::iterator::_BeforeBegin,
70 PS_InRootName = path::iterator::_InRootName,
71 PS_InRootDir = path::iterator::_InRootDir,
72 PS_InFilenames = path::iterator::_InFilenames,
73 PS_InTrailingSep = path::iterator::_InTrailingSep,
74 PS_AtEnd = path::iterator::_AtEnd
Eric Fiselier91a182b2018-04-02 23:03:41 +000075 };
76
77 const string_view_t Path;
78 string_view_t RawEntry;
79 ParserState State;
80
81private:
Eric Fiselier02cea5e2018-07-27 03:07:09 +000082 PathParser(string_view_t P, ParserState State) noexcept : Path(P),
83 State(State) {}
Eric Fiselier91a182b2018-04-02 23:03:41 +000084
85public:
86 PathParser(string_view_t P, string_view_t E, unsigned char S)
87 : Path(P), RawEntry(E), State(static_cast<ParserState>(S)) {
88 // S cannot be '0' or PS_BeforeBegin.
89 }
90
91 static PathParser CreateBegin(string_view_t P) noexcept {
92 PathParser PP(P, PS_BeforeBegin);
93 PP.increment();
94 return PP;
95 }
96
97 static PathParser CreateEnd(string_view_t P) noexcept {
98 PathParser PP(P, PS_AtEnd);
99 return PP;
100 }
101
102 PosPtr peek() const noexcept {
103 auto TkEnd = getNextTokenStartPos();
104 auto End = getAfterBack();
105 return TkEnd == End ? nullptr : TkEnd;
106 }
107
108 void increment() noexcept {
109 const PosPtr End = getAfterBack();
110 const PosPtr Start = getNextTokenStartPos();
111 if (Start == End)
112 return makeState(PS_AtEnd);
113
114 switch (State) {
115 case PS_BeforeBegin: {
116 PosPtr TkEnd = consumeSeparator(Start, End);
117 if (TkEnd)
118 return makeState(PS_InRootDir, Start, TkEnd);
119 else
120 return makeState(PS_InFilenames, Start, consumeName(Start, End));
121 }
122 case PS_InRootDir:
123 return makeState(PS_InFilenames, Start, consumeName(Start, End));
124
125 case PS_InFilenames: {
126 PosPtr SepEnd = consumeSeparator(Start, End);
127 if (SepEnd != End) {
128 PosPtr TkEnd = consumeName(SepEnd, End);
129 if (TkEnd)
130 return makeState(PS_InFilenames, SepEnd, TkEnd);
131 }
132 return makeState(PS_InTrailingSep, Start, SepEnd);
133 }
134
135 case PS_InTrailingSep:
136 return makeState(PS_AtEnd);
137
138 case PS_InRootName:
139 case PS_AtEnd:
140 _LIBCPP_UNREACHABLE();
141 }
142 }
143
144 void decrement() noexcept {
145 const PosPtr REnd = getBeforeFront();
146 const PosPtr RStart = getCurrentTokenStartPos() - 1;
147 if (RStart == REnd) // we're decrementing the begin
148 return makeState(PS_BeforeBegin);
149
150 switch (State) {
151 case PS_AtEnd: {
152 // Try to consume a trailing separator or root directory first.
153 if (PosPtr SepEnd = consumeSeparator(RStart, REnd)) {
154 if (SepEnd == REnd)
155 return makeState(PS_InRootDir, Path.data(), RStart + 1);
156 return makeState(PS_InTrailingSep, SepEnd + 1, RStart + 1);
157 } else {
158 PosPtr TkStart = consumeName(RStart, REnd);
159 return makeState(PS_InFilenames, TkStart + 1, RStart + 1);
160 }
161 }
162 case PS_InTrailingSep:
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000163 return makeState(PS_InFilenames, consumeName(RStart, REnd) + 1,
164 RStart + 1);
Eric Fiselier91a182b2018-04-02 23:03:41 +0000165 case PS_InFilenames: {
166 PosPtr SepEnd = consumeSeparator(RStart, REnd);
167 if (SepEnd == REnd)
168 return makeState(PS_InRootDir, Path.data(), RStart + 1);
169 PosPtr TkEnd = consumeName(SepEnd, REnd);
170 return makeState(PS_InFilenames, TkEnd + 1, SepEnd + 1);
171 }
172 case PS_InRootDir:
173 // return makeState(PS_InRootName, Path.data(), RStart + 1);
174 case PS_InRootName:
175 case PS_BeforeBegin:
176 _LIBCPP_UNREACHABLE();
177 }
178 }
179
180 /// \brief Return a view with the "preferred representation" of the current
181 /// element. For example trailing separators are represented as a '.'
182 string_view_t operator*() const noexcept {
183 switch (State) {
184 case PS_BeforeBegin:
185 case PS_AtEnd:
186 return "";
187 case PS_InRootDir:
188 return "/";
189 case PS_InTrailingSep:
190 return "";
191 case PS_InRootName:
192 case PS_InFilenames:
193 return RawEntry;
194 }
195 _LIBCPP_UNREACHABLE();
196 }
197
198 explicit operator bool() const noexcept {
199 return State != PS_BeforeBegin && State != PS_AtEnd;
200 }
201
202 PathParser& operator++() noexcept {
203 increment();
204 return *this;
205 }
206
207 PathParser& operator--() noexcept {
208 decrement();
209 return *this;
210 }
211
Eric Fiselierc9a770e2018-12-21 03:16:30 +0000212 bool atEnd() const noexcept {
213 return State == PS_AtEnd;
214 }
215
216 bool inRootDir() const noexcept {
217 return State == PS_InRootDir;
218 }
219
220 bool inRootName() const noexcept {
221 return State == PS_InRootName;
222 }
223
Eric Fiselier91a182b2018-04-02 23:03:41 +0000224 bool inRootPath() const noexcept {
Eric Fiselierc9a770e2018-12-21 03:16:30 +0000225 return inRootName() || inRootDir();
Eric Fiselier91a182b2018-04-02 23:03:41 +0000226 }
227
228private:
229 void makeState(ParserState NewState, PosPtr Start, PosPtr End) noexcept {
230 State = NewState;
231 RawEntry = string_view_t(Start, End - Start);
232 }
233 void makeState(ParserState NewState) noexcept {
234 State = NewState;
235 RawEntry = {};
236 }
237
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000238 PosPtr getAfterBack() const noexcept { return Path.data() + Path.size(); }
Eric Fiselier91a182b2018-04-02 23:03:41 +0000239
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000240 PosPtr getBeforeFront() const noexcept { return Path.data() - 1; }
Eric Fiselier91a182b2018-04-02 23:03:41 +0000241
242 /// \brief Return a pointer to the first character after the currently
243 /// lexed element.
244 PosPtr getNextTokenStartPos() const noexcept {
245 switch (State) {
246 case PS_BeforeBegin:
247 return Path.data();
248 case PS_InRootName:
249 case PS_InRootDir:
250 case PS_InFilenames:
251 return &RawEntry.back() + 1;
252 case PS_InTrailingSep:
253 case PS_AtEnd:
254 return getAfterBack();
255 }
256 _LIBCPP_UNREACHABLE();
257 }
258
259 /// \brief Return a pointer to the first character in the currently lexed
260 /// element.
261 PosPtr getCurrentTokenStartPos() const noexcept {
262 switch (State) {
263 case PS_BeforeBegin:
264 case PS_InRootName:
265 return &Path.front();
266 case PS_InRootDir:
267 case PS_InFilenames:
268 case PS_InTrailingSep:
269 return &RawEntry.front();
270 case PS_AtEnd:
271 return &Path.back() + 1;
272 }
273 _LIBCPP_UNREACHABLE();
274 }
275
276 PosPtr consumeSeparator(PosPtr P, PosPtr End) const noexcept {
277 if (P == End || *P != '/')
278 return nullptr;
279 const int Inc = P < End ? 1 : -1;
280 P += Inc;
281 while (P != End && *P == '/')
282 P += Inc;
283 return P;
284 }
285
286 PosPtr consumeName(PosPtr P, PosPtr End) const noexcept {
287 if (P == End || *P == '/')
288 return nullptr;
289 const int Inc = P < End ? 1 : -1;
290 P += Inc;
291 while (P != End && *P != '/')
292 P += Inc;
293 return P;
294 }
295};
296
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000297string_view_pair separate_filename(string_view_t const& s) {
298 if (s == "." || s == ".." || s.empty())
299 return string_view_pair{s, ""};
300 auto pos = s.find_last_of('.');
301 if (pos == string_view_t::npos || pos == 0)
302 return string_view_pair{s, string_view_t{}};
303 return string_view_pair{s.substr(0, pos), s.substr(pos)};
Eric Fiselier91a182b2018-04-02 23:03:41 +0000304}
305
306string_view_t createView(PosPtr S, PosPtr E) noexcept {
307 return {S, static_cast<size_t>(E - S) + 1};
308}
309
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000310} // namespace parser
311} // namespace
Eric Fiselier91a182b2018-04-02 23:03:41 +0000312
Eric Fiselier435db152016-06-17 19:46:40 +0000313// POSIX HELPERS
314
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000315namespace detail {
316namespace {
Eric Fiselier435db152016-06-17 19:46:40 +0000317
318using value_type = path::value_type;
319using string_type = path::string_type;
320
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000321struct FileDescriptor {
322 const path& name;
323 int fd = -1;
324 StatT m_stat;
325 file_status m_status;
326
327 template <class... Args>
328 static FileDescriptor create(const path* p, error_code& ec, Args... args) {
329 ec.clear();
330 int fd;
331 if ((fd = ::open(p->c_str(), args...)) == -1) {
332 ec = capture_errno();
333 return FileDescriptor{p};
334 }
335 return FileDescriptor(p, fd);
336 }
337
338 template <class... Args>
339 static FileDescriptor create_with_status(const path* p, error_code& ec,
340 Args... args) {
341 FileDescriptor fd = create(p, ec, args...);
342 if (!ec)
343 fd.refresh_status(ec);
344
345 return fd;
346 }
347
348 file_status get_status() const { return m_status; }
349 StatT const& get_stat() const { return m_stat; }
350
351 bool status_known() const { return _VSTD_FS::status_known(m_status); }
352
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000353 file_status refresh_status(error_code& ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000354
355 void close() noexcept {
356 if (fd != -1)
357 ::close(fd);
358 fd = -1;
359 }
360
361 FileDescriptor(FileDescriptor&& other)
362 : name(other.name), fd(other.fd), m_stat(other.m_stat),
363 m_status(other.m_status) {
364 other.fd = -1;
365 other.m_status = file_status{};
366 }
367
368 ~FileDescriptor() { close(); }
369
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000370 FileDescriptor(FileDescriptor const&) = delete;
371 FileDescriptor& operator=(FileDescriptor const&) = delete;
372
373private:
374 explicit FileDescriptor(const path* p, int fd = -1) : name(*p), fd(fd) {}
375};
376
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000377perms posix_get_perms(const StatT& st) noexcept {
Eric Fiselier70474082018-07-20 01:22:32 +0000378 return static_cast<perms>(st.st_mode) & perms::mask;
Eric Fiselier435db152016-06-17 19:46:40 +0000379}
380
381::mode_t posix_convert_perms(perms prms) {
Eric Fiselier70474082018-07-20 01:22:32 +0000382 return static_cast< ::mode_t>(prms & perms::mask);
Eric Fiselier435db152016-06-17 19:46:40 +0000383}
384
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000385file_status create_file_status(error_code& m_ec, path const& p,
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000386 const StatT& path_stat, error_code* ec) {
Eric Fiselier70474082018-07-20 01:22:32 +0000387 if (ec)
388 *ec = m_ec;
Eric Fiselier70474082018-07-20 01:22:32 +0000389 if (m_ec && (m_ec.value() == ENOENT || m_ec.value() == ENOTDIR)) {
390 return file_status(file_type::not_found);
391 } else if (m_ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000392 ErrorHandler<void> err("posix_stat", ec, &p);
393 err.report(m_ec, "failed to determine attributes for the specified path");
Eric Fiselier70474082018-07-20 01:22:32 +0000394 return file_status(file_type::none);
395 }
396 // else
Eric Fiselier435db152016-06-17 19:46:40 +0000397
Eric Fiselier70474082018-07-20 01:22:32 +0000398 file_status fs_tmp;
399 auto const mode = path_stat.st_mode;
400 if (S_ISLNK(mode))
401 fs_tmp.type(file_type::symlink);
402 else if (S_ISREG(mode))
403 fs_tmp.type(file_type::regular);
404 else if (S_ISDIR(mode))
405 fs_tmp.type(file_type::directory);
406 else if (S_ISBLK(mode))
407 fs_tmp.type(file_type::block);
408 else if (S_ISCHR(mode))
409 fs_tmp.type(file_type::character);
410 else if (S_ISFIFO(mode))
411 fs_tmp.type(file_type::fifo);
412 else if (S_ISSOCK(mode))
413 fs_tmp.type(file_type::socket);
414 else
415 fs_tmp.type(file_type::unknown);
Eric Fiselier435db152016-06-17 19:46:40 +0000416
Eric Fiselier70474082018-07-20 01:22:32 +0000417 fs_tmp.permissions(detail::posix_get_perms(path_stat));
418 return fs_tmp;
Eric Fiselier435db152016-06-17 19:46:40 +0000419}
420
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000421file_status posix_stat(path const& p, StatT& path_stat, error_code* ec) {
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000422 error_code m_ec;
Eric Fiselier70474082018-07-20 01:22:32 +0000423 if (::stat(p.c_str(), &path_stat) == -1)
424 m_ec = detail::capture_errno();
425 return create_file_status(m_ec, p, path_stat, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000426}
427
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000428file_status posix_stat(path const& p, error_code* ec) {
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000429 StatT path_stat;
Eric Fiselier70474082018-07-20 01:22:32 +0000430 return posix_stat(p, path_stat, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000431}
432
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000433file_status posix_lstat(path const& p, StatT& path_stat, error_code* ec) {
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000434 error_code m_ec;
Eric Fiselier70474082018-07-20 01:22:32 +0000435 if (::lstat(p.c_str(), &path_stat) == -1)
436 m_ec = detail::capture_errno();
437 return create_file_status(m_ec, p, path_stat, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000438}
439
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000440file_status posix_lstat(path const& p, error_code* ec) {
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000441 StatT path_stat;
Eric Fiselier70474082018-07-20 01:22:32 +0000442 return posix_lstat(p, path_stat, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000443}
444
Dan Albert39b981d2019-01-15 19:16:25 +0000445// http://pubs.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html
446bool posix_ftruncate(const FileDescriptor& fd, off_t to_size, error_code& ec) {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000447 if (::ftruncate(fd.fd, to_size) == -1) {
448 ec = capture_errno();
Eric Fiselierf1aba0d2018-07-26 04:02:06 +0000449 return true;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000450 }
451 ec.clear();
Eric Fiselierf1aba0d2018-07-26 04:02:06 +0000452 return false;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000453}
454
455bool posix_fchmod(const FileDescriptor& fd, const StatT& st, error_code& ec) {
456 if (::fchmod(fd.fd, st.st_mode) == -1) {
457 ec = capture_errno();
Eric Fiselierf1aba0d2018-07-26 04:02:06 +0000458 return true;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000459 }
460 ec.clear();
Eric Fiselierf1aba0d2018-07-26 04:02:06 +0000461 return false;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000462}
463
464bool stat_equivalent(const StatT& st1, const StatT& st2) {
Eric Fiselier70474082018-07-20 01:22:32 +0000465 return (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino);
Eric Fiselier435db152016-06-17 19:46:40 +0000466}
467
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000468file_status FileDescriptor::refresh_status(error_code& ec) {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000469 // FD must be open and good.
470 m_status = file_status{};
Eric Fiselierd8b25e32018-07-23 03:06:57 +0000471 m_stat = {};
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000472 error_code m_ec;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000473 if (::fstat(fd, &m_stat) == -1)
474 m_ec = capture_errno();
475 m_status = create_file_status(m_ec, name, m_stat, &ec);
476 return m_status;
Eric Fiselier435db152016-06-17 19:46:40 +0000477}
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000478} // namespace
479} // end namespace detail
Eric Fiselier435db152016-06-17 19:46:40 +0000480
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000481using detail::capture_errno;
482using detail::ErrorHandler;
483using detail::StatT;
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000484using detail::TimeSpec;
Eric Fiselier91a182b2018-04-02 23:03:41 +0000485using parser::createView;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000486using parser::PathParser;
487using parser::string_view_t;
488
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000489const bool _FilesystemClock::is_steady;
490
491_FilesystemClock::time_point _FilesystemClock::now() noexcept {
492 typedef chrono::duration<rep> __secs;
493#if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
494 typedef chrono::duration<rep, nano> __nsecs;
495 struct timespec tp;
496 if (0 != clock_gettime(CLOCK_REALTIME, &tp))
497 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
498 return time_point(__secs(tp.tv_sec) +
499 chrono::duration_cast<duration>(__nsecs(tp.tv_nsec)));
500#else
501 typedef chrono::duration<rep, micro> __microsecs;
502 timeval tv;
503 gettimeofday(&tv, 0);
504 return time_point(__secs(tv.tv_sec) + __microsecs(tv.tv_usec));
505#endif // _LIBCPP_USE_CLOCK_GETTIME && CLOCK_REALTIME
506}
507
508filesystem_error::~filesystem_error() {}
509
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000510void filesystem_error::__create_what(int __num_paths) {
511 const char* derived_what = system_error::what();
512 __storage_->__what_ = [&]() -> string {
513 const char* p1 = path1().native().empty() ? "\"\"" : path1().c_str();
514 const char* p2 = path2().native().empty() ? "\"\"" : path2().c_str();
515 switch (__num_paths) {
516 default:
517 return detail::format_string("filesystem error: %s", derived_what);
518 case 1:
519 return detail::format_string("filesystem error: %s [%s]", derived_what,
520 p1);
521 case 2:
522 return detail::format_string("filesystem error: %s [%s] [%s]",
523 derived_what, p1, p2);
524 }
525 }();
526}
Eric Fiselier435db152016-06-17 19:46:40 +0000527
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000528static path __do_absolute(const path& p, path* cwd, error_code* ec) {
529 if (ec)
530 ec->clear();
531 if (p.is_absolute())
532 return p;
533 *cwd = __current_path(ec);
534 if (ec && *ec)
535 return {};
536 return (*cwd) / p;
Eric Fiselier91a182b2018-04-02 23:03:41 +0000537}
538
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000539path __absolute(const path& p, error_code* ec) {
540 path cwd;
541 return __do_absolute(p, &cwd, ec);
Eric Fiselier91a182b2018-04-02 23:03:41 +0000542}
543
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000544path __canonical(path const& orig_p, error_code* ec) {
545 path cwd;
546 ErrorHandler<path> err("canonical", ec, &orig_p, &cwd);
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000547
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000548 path p = __do_absolute(orig_p, &cwd, ec);
Eric Fiselierb5215302019-01-17 02:59:28 +0000549#if _POSIX_VERSION >= 200112
550 std::unique_ptr<char, decltype(&::free)>
551 hold(::realpath(p.c_str(), nullptr), &::free);
552 if (hold.get() == nullptr)
553 return err.report(capture_errno());
554 return {hold.get()};
555#else
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000556 char buff[PATH_MAX + 1];
557 char* ret;
558 if ((ret = ::realpath(p.c_str(), buff)) == nullptr)
559 return err.report(capture_errno());
560 return {ret};
Eric Fiselierb5215302019-01-17 02:59:28 +0000561#endif
Eric Fiselier435db152016-06-17 19:46:40 +0000562}
563
564void __copy(const path& from, const path& to, copy_options options,
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000565 error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000566 ErrorHandler<void> err("copy", ec, &from, &to);
Eric Fiselier435db152016-06-17 19:46:40 +0000567
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000568 const bool sym_status = bool(
569 options & (copy_options::create_symlinks | copy_options::skip_symlinks));
Eric Fiselier435db152016-06-17 19:46:40 +0000570
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000571 const bool sym_status2 = bool(options & copy_options::copy_symlinks);
Eric Fiselier435db152016-06-17 19:46:40 +0000572
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000573 error_code m_ec1;
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000574 StatT f_st = {};
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000575 const file_status f = sym_status || sym_status2
576 ? detail::posix_lstat(from, f_st, &m_ec1)
577 : detail::posix_stat(from, f_st, &m_ec1);
578 if (m_ec1)
579 return err.report(m_ec1);
Eric Fiselier435db152016-06-17 19:46:40 +0000580
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000581 StatT t_st = {};
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000582 const file_status t = sym_status ? detail::posix_lstat(to, t_st, &m_ec1)
583 : detail::posix_stat(to, t_st, &m_ec1);
Eric Fiselier435db152016-06-17 19:46:40 +0000584
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000585 if (not status_known(t))
586 return err.report(m_ec1);
587
588 if (!exists(f) || is_other(f) || is_other(t) ||
589 (is_directory(f) && is_regular_file(t)) ||
590 detail::stat_equivalent(f_st, t_st)) {
591 return err.report(errc::function_not_supported);
592 }
Eric Fiselier435db152016-06-17 19:46:40 +0000593
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000594 if (ec)
595 ec->clear();
Eric Fiselier435db152016-06-17 19:46:40 +0000596
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000597 if (is_symlink(f)) {
598 if (bool(copy_options::skip_symlinks & options)) {
599 // do nothing
600 } else if (not exists(t)) {
601 __copy_symlink(from, to, ec);
602 } else {
603 return err.report(errc::file_exists);
Eric Fiselier435db152016-06-17 19:46:40 +0000604 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000605 return;
606 } else if (is_regular_file(f)) {
607 if (bool(copy_options::directories_only & options)) {
608 // do nothing
609 } else if (bool(copy_options::create_symlinks & options)) {
610 __create_symlink(from, to, ec);
611 } else if (bool(copy_options::create_hard_links & options)) {
612 __create_hard_link(from, to, ec);
613 } else if (is_directory(t)) {
614 __copy_file(from, to / from.filename(), options, ec);
615 } else {
616 __copy_file(from, to, options, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000617 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000618 return;
619 } else if (is_directory(f) && bool(copy_options::create_symlinks & options)) {
620 return err.report(errc::is_a_directory);
621 } else if (is_directory(f) && (bool(copy_options::recursive & options) ||
622 copy_options::none == options)) {
Eric Fiselier435db152016-06-17 19:46:40 +0000623
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000624 if (!exists(t)) {
625 // create directory to with attributes from 'from'.
626 __create_directory(to, from, ec);
627 if (ec && *ec) {
628 return;
629 }
Eric Fiselier435db152016-06-17 19:46:40 +0000630 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000631 directory_iterator it =
632 ec ? directory_iterator(from, *ec) : directory_iterator(from);
633 if (ec && *ec) {
634 return;
635 }
636 error_code m_ec2;
637 for (; it != directory_iterator(); it.increment(m_ec2)) {
638 if (m_ec2) {
639 return err.report(m_ec2);
640 }
641 __copy(it->path(), to / it->path().filename(),
642 options | copy_options::__in_recursive_copy, ec);
643 if (ec && *ec) {
644 return;
645 }
646 }
647 }
Eric Fiselier435db152016-06-17 19:46:40 +0000648}
649
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000650namespace detail {
651namespace {
652
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000653#ifdef _LIBCPP_USE_SENDFILE
654bool copy_file_impl_sendfile(FileDescriptor& read_fd, FileDescriptor& write_fd,
655 error_code& ec) {
656
657 size_t count = read_fd.get_stat().st_size;
658 do {
659 ssize_t res;
660 if ((res = ::sendfile(write_fd.fd, read_fd.fd, nullptr, count)) == -1) {
661 ec = capture_errno();
662 return false;
663 }
664 count -= res;
665 } while (count > 0);
666
667 ec.clear();
668
669 return true;
670}
671#elif defined(_LIBCPP_USE_COPYFILE)
672bool copy_file_impl_copyfile(FileDescriptor& read_fd, FileDescriptor& write_fd,
673 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) {
686 ec = capture_errno();
687 return false;
688 }
689
690 ec.clear();
691 return true;
692}
693#endif
694
695// Note: This function isn't guarded by ifdef's even though it may be unused
696// in order to assure it still compiles.
697__attribute__((unused)) bool copy_file_impl_default(FileDescriptor& read_fd,
698 FileDescriptor& write_fd,
699 error_code& ec) {
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000700 ifstream in;
701 in.__open(read_fd.fd, ios::binary);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000702 if (!in.is_open()) {
703 // This assumes that __open didn't reset the error code.
704 ec = capture_errno();
705 return false;
706 }
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000707 ofstream out;
708 out.__open(write_fd.fd, ios::binary);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000709 if (!out.is_open()) {
710 ec = capture_errno();
711 return false;
712 }
713
714 if (in.good() && out.good()) {
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000715 using InIt = istreambuf_iterator<char>;
716 using OutIt = ostreambuf_iterator<char>;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000717 InIt bin(in);
718 InIt ein;
719 OutIt bout(out);
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000720 copy(bin, ein, bout);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000721 }
722 if (out.fail() || in.fail()) {
723 ec = make_error_code(errc::io_error);
724 return false;
725 }
726
727 ec.clear();
728 return true;
729}
730
731bool copy_file_impl(FileDescriptor& from, FileDescriptor& to, error_code& ec) {
732#if defined(_LIBCPP_USE_SENDFILE)
733 return copy_file_impl_sendfile(from, to, ec);
734#elif defined(_LIBCPP_USE_COPYFILE)
735 return copy_file_impl_copyfile(from, to, ec);
736#else
737 return copy_file_impl_default(from, to, ec);
738#endif
739}
740
741} // namespace
742} // namespace detail
Eric Fiselier435db152016-06-17 19:46:40 +0000743
744bool __copy_file(const path& from, const path& to, copy_options options,
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000745 error_code* ec) {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000746 using detail::FileDescriptor;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000747 ErrorHandler<bool> err("copy_file", ec, &to, &from);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000748
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000749 error_code m_ec;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000750 FileDescriptor from_fd =
751 FileDescriptor::create_with_status(&from, m_ec, O_RDONLY | O_NONBLOCK);
752 if (m_ec)
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000753 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000754
755 auto from_st = from_fd.get_status();
756 StatT const& from_stat = from_fd.get_stat();
757 if (!is_regular_file(from_st)) {
758 if (not m_ec)
759 m_ec = make_error_code(errc::not_supported);
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000760 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000761 }
762
763 const bool skip_existing = bool(copy_options::skip_existing & options);
764 const bool update_existing = bool(copy_options::update_existing & options);
765 const bool overwrite_existing =
766 bool(copy_options::overwrite_existing & options);
767
768 StatT to_stat_path;
769 file_status to_st = detail::posix_stat(to, to_stat_path, &m_ec);
770 if (!status_known(to_st))
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000771 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000772
773 const bool to_exists = exists(to_st);
774 if (to_exists && !is_regular_file(to_st))
Eric Fiselier268fa832018-07-23 11:55:13 +0000775 return err.report(errc::not_supported);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000776
777 if (to_exists && detail::stat_equivalent(from_stat, to_stat_path))
Eric Fiselier268fa832018-07-23 11:55:13 +0000778 return err.report(errc::file_exists);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000779
780 if (to_exists && skip_existing)
781 return false;
782
Eric Fiselier455ac4b2018-07-22 21:15:15 +0000783 bool ShouldCopy = [&]() {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000784 if (to_exists && update_existing) {
785 auto from_time = detail::extract_mtime(from_stat);
786 auto to_time = detail::extract_mtime(to_stat_path);
787 if (from_time.tv_sec < to_time.tv_sec)
Eric Fiselier435db152016-06-17 19:46:40 +0000788 return false;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000789 if (from_time.tv_sec == to_time.tv_sec &&
790 from_time.tv_nsec <= to_time.tv_nsec)
Eric Fiseliere7359252016-10-16 00:47:59 +0000791 return false;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000792 return true;
Eric Fiseliere7359252016-10-16 00:47:59 +0000793 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000794 if (!to_exists || overwrite_existing)
795 return true;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000796 return err.report(errc::file_exists);
Eric Fiselier455ac4b2018-07-22 21:15:15 +0000797 }();
798 if (!ShouldCopy)
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000799 return false;
Eric Fiseliere7359252016-10-16 00:47:59 +0000800
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000801 // Don't truncate right away. We may not be opening the file we originally
802 // looked at; we'll check this later.
Eric Fiselier455ac4b2018-07-22 21:15:15 +0000803 int to_open_flags = O_WRONLY;
804 if (!to_exists)
805 to_open_flags |= O_CREAT;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000806 FileDescriptor to_fd = FileDescriptor::create_with_status(
807 &to, m_ec, to_open_flags, from_stat.st_mode);
808 if (m_ec)
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000809 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000810
811 if (to_exists) {
812 // Check that the file we initially stat'ed is equivalent to the one
813 // we opened.
Eric Fiselier455ac4b2018-07-22 21:15:15 +0000814 // FIXME: report this better.
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000815 if (!detail::stat_equivalent(to_stat_path, to_fd.get_stat()))
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000816 return err.report(errc::bad_file_descriptor);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000817
818 // Set the permissions and truncate the file we opened.
Eric Fiselierf1aba0d2018-07-26 04:02:06 +0000819 if (detail::posix_fchmod(to_fd, from_stat, m_ec))
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000820 return err.report(m_ec);
Eric Fiselierf1aba0d2018-07-26 04:02:06 +0000821 if (detail::posix_ftruncate(to_fd, 0, m_ec))
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000822 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000823 }
824
825 if (!copy_file_impl(from_fd, to_fd, m_ec)) {
826 // FIXME: Remove the dest file if we failed, and it didn't exist previously.
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000827 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000828 }
829
830 return true;
Eric Fiselier435db152016-06-17 19:46:40 +0000831}
832
833void __copy_symlink(const path& existing_symlink, const path& new_symlink,
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000834 error_code* ec) {
835 const path real_path(__read_symlink(existing_symlink, ec));
836 if (ec && *ec) {
837 return;
838 }
839 // NOTE: proposal says you should detect if you should call
840 // create_symlink or create_directory_symlink. I don't think this
841 // is needed with POSIX
842 __create_symlink(real_path, new_symlink, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000843}
844
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000845bool __create_directories(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000846 ErrorHandler<bool> err("create_directories", ec, &p);
Eric Fiselier435db152016-06-17 19:46:40 +0000847
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000848 error_code m_ec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000849 auto const st = detail::posix_stat(p, &m_ec);
850 if (!status_known(st))
851 return err.report(m_ec);
852 else if (is_directory(st))
853 return false;
854 else if (exists(st))
855 return err.report(errc::file_exists);
856
857 const path parent = p.parent_path();
858 if (!parent.empty()) {
859 const file_status parent_st = status(parent, m_ec);
860 if (not status_known(parent_st))
861 return err.report(m_ec);
862 if (not exists(parent_st)) {
863 __create_directories(parent, ec);
864 if (ec && *ec) {
865 return false;
866 }
Eric Fiselier435db152016-06-17 19:46:40 +0000867 }
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000868 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000869 return __create_directory(p, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000870}
871
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000872bool __create_directory(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000873 ErrorHandler<bool> err("create_directory", ec, &p);
874
875 if (::mkdir(p.c_str(), static_cast<int>(perms::all)) == 0)
876 return true;
Eric Fiselier7ca3db82018-07-25 04:46:32 +0000877 if (errno != EEXIST)
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000878 err.report(capture_errno());
879 return false;
Eric Fiselier435db152016-06-17 19:46:40 +0000880}
881
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000882bool __create_directory(path const& p, path const& attributes, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000883 ErrorHandler<bool> err("create_directory", ec, &p, &attributes);
884
885 StatT attr_stat;
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000886 error_code mec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000887 auto st = detail::posix_stat(attributes, attr_stat, &mec);
888 if (!status_known(st))
889 return err.report(mec);
Eric Fiselier7ca3db82018-07-25 04:46:32 +0000890 if (!is_directory(st))
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000891 return err.report(errc::not_a_directory,
892 "the specified attribute path is invalid");
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000893
894 if (::mkdir(p.c_str(), attr_stat.st_mode) == 0)
895 return true;
Eric Fiselier7ca3db82018-07-25 04:46:32 +0000896 if (errno != EEXIST)
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000897 err.report(capture_errno());
898 return false;
Eric Fiselier435db152016-06-17 19:46:40 +0000899}
900
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000901void __create_directory_symlink(path const& from, path const& to,
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000902 error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000903 ErrorHandler<void> err("create_directory_symlink", ec, &from, &to);
904 if (::symlink(from.c_str(), to.c_str()) != 0)
905 return err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +0000906}
907
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000908void __create_hard_link(const path& from, const path& to, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000909 ErrorHandler<void> err("create_hard_link", ec, &from, &to);
910 if (::link(from.c_str(), to.c_str()) == -1)
911 return err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +0000912}
913
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000914void __create_symlink(path const& from, path const& to, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000915 ErrorHandler<void> err("create_symlink", ec, &from, &to);
916 if (::symlink(from.c_str(), to.c_str()) == -1)
917 return err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +0000918}
919
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000920path __current_path(error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000921 ErrorHandler<path> err("current_path", ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000922
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000923 auto size = ::pathconf(".", _PC_PATH_MAX);
924 _LIBCPP_ASSERT(size >= 0, "pathconf returned a 0 as max size");
925
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000926 auto buff = unique_ptr<char[]>(new char[size + 1]);
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000927 char* ret;
928 if ((ret = ::getcwd(buff.get(), static_cast<size_t>(size))) == nullptr)
929 return err.report(capture_errno(), "call to getcwd failed");
930
931 return {buff.get()};
Eric Fiselier435db152016-06-17 19:46:40 +0000932}
933
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000934void __current_path(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000935 ErrorHandler<void> err("current_path", ec, &p);
936 if (::chdir(p.c_str()) == -1)
937 err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +0000938}
939
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000940bool __equivalent(const path& p1, const path& p2, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000941 ErrorHandler<bool> err("equivalent", ec, &p1, &p2);
942
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000943 error_code ec1, ec2;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000944 StatT st1 = {}, st2 = {};
945 auto s1 = detail::posix_stat(p1.native(), st1, &ec1);
946 if (!exists(s1))
947 return err.report(errc::not_supported);
948 auto s2 = detail::posix_stat(p2.native(), st2, &ec2);
949 if (!exists(s2))
950 return err.report(errc::not_supported);
951
952 return detail::stat_equivalent(st1, st2);
Eric Fiselier435db152016-06-17 19:46:40 +0000953}
954
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000955uintmax_t __file_size(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000956 ErrorHandler<uintmax_t> err("file_size", ec, &p);
957
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000958 error_code m_ec;
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000959 StatT st;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000960 file_status fst = detail::posix_stat(p, st, &m_ec);
961 if (!exists(fst) || !is_regular_file(fst)) {
962 errc error_kind =
963 is_directory(fst) ? errc::is_a_directory : errc::not_supported;
964 if (!m_ec)
965 m_ec = make_error_code(error_kind);
966 return err.report(m_ec);
967 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000968 // is_regular_file(p) == true
969 return static_cast<uintmax_t>(st.st_size);
Eric Fiselier435db152016-06-17 19:46:40 +0000970}
971
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000972uintmax_t __hard_link_count(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000973 ErrorHandler<uintmax_t> err("hard_link_count", ec, &p);
974
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000975 error_code m_ec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000976 StatT st;
977 detail::posix_stat(p, st, &m_ec);
978 if (m_ec)
979 return err.report(m_ec);
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000980 return static_cast<uintmax_t>(st.st_nlink);
Eric Fiselier435db152016-06-17 19:46:40 +0000981}
982
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000983bool __fs_is_empty(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000984 ErrorHandler<bool> err("is_empty", ec, &p);
Eric Fiselieraa8c61f2016-10-15 23:05:04 +0000985
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000986 error_code m_ec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000987 StatT pst;
988 auto st = detail::posix_stat(p, pst, &m_ec);
989 if (m_ec)
990 return err.report(m_ec);
991 else if (!is_directory(st) && !is_regular_file(st))
992 return err.report(errc::not_supported);
993 else if (is_directory(st)) {
994 auto it = ec ? directory_iterator(p, *ec) : directory_iterator(p);
995 if (ec && *ec)
996 return false;
997 return it == directory_iterator{};
998 } else if (is_regular_file(st))
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000999 return static_cast<uintmax_t>(pst.st_size) == 0;
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001000
1001 _LIBCPP_UNREACHABLE();
Eric Fiselier435db152016-06-17 19:46:40 +00001002}
1003
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001004static file_time_type __extract_last_write_time(const path& p, const StatT& st,
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001005 error_code* ec) {
Eric Fiselier7eba47e2018-07-25 20:51:49 +00001006 using detail::fs_time;
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001007 ErrorHandler<file_time_type> err("last_write_time", ec, &p);
1008
Eric Fiselier70474082018-07-20 01:22:32 +00001009 auto ts = detail::extract_mtime(st);
Eric Fiselier7eba47e2018-07-25 20:51:49 +00001010 if (!fs_time::is_representable(ts))
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001011 return err.report(errc::value_too_large);
1012
Eric Fiselier7eba47e2018-07-25 20:51:49 +00001013 return fs_time::convert_from_timespec(ts);
Eric Fiselier70474082018-07-20 01:22:32 +00001014}
Eric Fiselier42d6d2c2017-07-08 04:18:41 +00001015
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001016file_time_type __last_write_time(const path& p, error_code* ec) {
1017 using namespace chrono;
1018 ErrorHandler<file_time_type> err("last_write_time", ec, &p);
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001019
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001020 error_code m_ec;
1021 StatT st;
1022 detail::posix_stat(p, st, &m_ec);
1023 if (m_ec)
1024 return err.report(m_ec);
1025 return __extract_last_write_time(p, st, ec);
Eric Fiselier435db152016-06-17 19:46:40 +00001026}
1027
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001028void __last_write_time(const path& p, file_time_type new_time, error_code* ec) {
1029 using detail::fs_time;
1030 ErrorHandler<void> err("last_write_time", ec, &p);
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001031
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001032 error_code m_ec;
1033 array<TimeSpec, 2> tbuf;
Eric Fiselier7eba47e2018-07-25 20:51:49 +00001034#if !defined(_LIBCPP_USE_UTIMENSAT)
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001035 // This implementation has a race condition between determining the
1036 // last access time and attempting to set it to the same value using
1037 // ::utimes
1038 StatT st;
1039 file_status fst = detail::posix_stat(p, st, &m_ec);
1040 if (m_ec)
1041 return err.report(m_ec);
1042 tbuf[0] = detail::extract_atime(st);
Eric Fiselier435db152016-06-17 19:46:40 +00001043#else
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001044 tbuf[0].tv_sec = 0;
1045 tbuf[0].tv_nsec = UTIME_OMIT;
Eric Fiselier435db152016-06-17 19:46:40 +00001046#endif
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001047 if (!fs_time::convert_to_timespec(tbuf[1], new_time))
1048 return err.report(errc::value_too_large);
Eric Fiselier70474082018-07-20 01:22:32 +00001049
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001050 detail::set_file_times(p, tbuf, m_ec);
1051 if (m_ec)
1052 return err.report(m_ec);
Eric Fiselier435db152016-06-17 19:46:40 +00001053}
1054
Eric Fiselier4f3dc0e2018-03-26 06:23:55 +00001055void __permissions(const path& p, perms prms, perm_options opts,
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001056 error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001057 ErrorHandler<void> err("permissions", ec, &p);
Eric Fiselier435db152016-06-17 19:46:40 +00001058
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001059 auto has_opt = [&](perm_options o) { return bool(o & opts); };
1060 const bool resolve_symlinks = !has_opt(perm_options::nofollow);
1061 const bool add_perms = has_opt(perm_options::add);
1062 const bool remove_perms = has_opt(perm_options::remove);
1063 _LIBCPP_ASSERT(
1064 (add_perms + remove_perms + has_opt(perm_options::replace)) == 1,
1065 "One and only one of the perm_options constants replace, add, or remove "
1066 "is present in opts");
1067
1068 bool set_sym_perms = false;
1069 prms &= perms::mask;
1070 if (!resolve_symlinks || (add_perms || remove_perms)) {
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001071 error_code m_ec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001072 file_status st = resolve_symlinks ? detail::posix_stat(p, &m_ec)
1073 : detail::posix_lstat(p, &m_ec);
1074 set_sym_perms = is_symlink(st);
1075 if (m_ec)
1076 return err.report(m_ec);
1077 _LIBCPP_ASSERT(st.permissions() != perms::unknown,
1078 "Permissions unexpectedly unknown");
1079 if (add_perms)
1080 prms |= st.permissions();
1081 else if (remove_perms)
1082 prms = st.permissions() & ~prms;
1083 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001084 const auto real_perms = detail::posix_convert_perms(prms);
Eric Fiselier435db152016-06-17 19:46:40 +00001085
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001086#if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_FDCWD)
1087 const int flags = set_sym_perms ? AT_SYMLINK_NOFOLLOW : 0;
1088 if (::fchmodat(AT_FDCWD, p.c_str(), real_perms, flags) == -1) {
1089 return err.report(capture_errno());
1090 }
1091#else
1092 if (set_sym_perms)
1093 return err.report(errc::operation_not_supported);
1094 if (::chmod(p.c_str(), real_perms) == -1) {
1095 return err.report(capture_errno());
1096 }
1097#endif
Eric Fiselier435db152016-06-17 19:46:40 +00001098}
1099
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001100path __read_symlink(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001101 ErrorHandler<path> err("read_symlink", ec, &p);
1102
Eric Fiselierb5215302019-01-17 02:59:28 +00001103#ifdef PATH_MAX
1104 struct NullDeleter { void operator()(void*) const {} };
1105 const size_t size = PATH_MAX + 1;
1106 char stack_buff[size];
1107 auto buff = std::unique_ptr<char[], NullDeleter>(stack_buff);
1108#else
1109 StatT sb;
1110 if (::lstat(p.c_str(), &sb) == -1) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001111 return err.report(capture_errno());
1112 }
Eric Fiselierb5215302019-01-17 02:59:28 +00001113 const size_t size = sb.st_size + 1;
1114 auto buff = unique_ptr<char[]>(new char[size]);
1115#endif
1116 ::ssize_t ret;
1117 if ((ret = ::readlink(p.c_str(), buff.get(), size)) == -1)
1118 return err.report(capture_errno());
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001119 _LIBCPP_ASSERT(ret > 0, "TODO");
Eric Fiselierb5215302019-01-17 02:59:28 +00001120 if (static_cast<size_t>(ret) >= size)
1121 return err.report(errc::value_too_large);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001122 buff[ret] = 0;
Eric Fiselierb5215302019-01-17 02:59:28 +00001123 return {buff.get()};
Eric Fiselier435db152016-06-17 19:46:40 +00001124}
1125
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001126bool __remove(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001127 ErrorHandler<bool> err("remove", ec, &p);
1128 if (::remove(p.c_str()) == -1) {
1129 if (errno != ENOENT)
1130 err.report(capture_errno());
1131 return false;
1132 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001133 return true;
Eric Fiselier435db152016-06-17 19:46:40 +00001134}
1135
1136namespace {
1137
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001138uintmax_t remove_all_impl(path const& p, error_code& ec) {
1139 const auto npos = static_cast<uintmax_t>(-1);
1140 const file_status st = __symlink_status(p, &ec);
1141 if (ec)
1142 return npos;
1143 uintmax_t count = 1;
1144 if (is_directory(st)) {
1145 for (directory_iterator it(p, ec); !ec && it != directory_iterator();
1146 it.increment(ec)) {
1147 auto other_count = remove_all_impl(it->path(), ec);
1148 if (ec)
1149 return npos;
1150 count += other_count;
Eric Fiselier435db152016-06-17 19:46:40 +00001151 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001152 if (ec)
1153 return npos;
1154 }
1155 if (!__remove(p, &ec))
1156 return npos;
1157 return count;
Eric Fiselier435db152016-06-17 19:46:40 +00001158}
1159
1160} // end namespace
1161
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001162uintmax_t __remove_all(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001163 ErrorHandler<uintmax_t> err("remove_all", ec, &p);
Ekaterina Vaartis52668f72018-01-11 17:04:29 +00001164
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001165 error_code mec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001166 auto count = remove_all_impl(p, mec);
1167 if (mec) {
1168 if (mec == errc::no_such_file_or_directory)
1169 return 0;
1170 return err.report(mec);
1171 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001172 return count;
Eric Fiselier435db152016-06-17 19:46:40 +00001173}
1174
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001175void __rename(const path& from, const path& to, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001176 ErrorHandler<void> err("rename", ec, &from, &to);
1177 if (::rename(from.c_str(), to.c_str()) == -1)
1178 err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +00001179}
1180
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001181void __resize_file(const path& p, uintmax_t size, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001182 ErrorHandler<void> err("resize_file", ec, &p);
1183 if (::truncate(p.c_str(), static_cast< ::off_t>(size)) == -1)
1184 return err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +00001185}
1186
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001187space_info __space(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001188 ErrorHandler<void> err("space", ec, &p);
1189 space_info si;
1190 struct statvfs m_svfs = {};
1191 if (::statvfs(p.c_str(), &m_svfs) == -1) {
1192 err.report(capture_errno());
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001193 si.capacity = si.free = si.available = static_cast<uintmax_t>(-1);
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001194 return si;
1195 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001196 // Multiply with overflow checking.
1197 auto do_mult = [&](uintmax_t& out, uintmax_t other) {
1198 out = other * m_svfs.f_frsize;
1199 if (other == 0 || out / other != m_svfs.f_frsize)
1200 out = static_cast<uintmax_t>(-1);
1201 };
1202 do_mult(si.capacity, m_svfs.f_blocks);
1203 do_mult(si.free, m_svfs.f_bfree);
1204 do_mult(si.available, m_svfs.f_bavail);
1205 return si;
Eric Fiselier435db152016-06-17 19:46:40 +00001206}
1207
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001208file_status __status(const path& p, error_code* ec) {
1209 return detail::posix_stat(p, ec);
Eric Fiselier435db152016-06-17 19:46:40 +00001210}
1211
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001212file_status __symlink_status(const path& p, error_code* ec) {
1213 return detail::posix_lstat(p, ec);
Eric Fiselier435db152016-06-17 19:46:40 +00001214}
1215
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001216path __temp_directory_path(error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001217 ErrorHandler<path> err("temp_directory_path", ec);
1218
Saleem Abdulrasoolcf279a52017-02-05 17:21:52 +00001219 const char* env_paths[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"};
1220 const char* ret = nullptr;
1221
1222 for (auto& ep : env_paths)
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001223 if ((ret = getenv(ep)))
Saleem Abdulrasoolcf279a52017-02-05 17:21:52 +00001224 break;
1225 if (ret == nullptr)
1226 ret = "/tmp";
1227
1228 path p(ret);
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001229 error_code m_ec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001230 file_status st = detail::posix_stat(p, &m_ec);
1231 if (!status_known(st))
1232 return err.report(m_ec, "cannot access path \"%s\"", p);
Saleem Abdulrasoolcf279a52017-02-05 17:21:52 +00001233
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001234 if (!exists(st) || !is_directory(st))
1235 return err.report(errc::not_a_directory, "path \"%s\" is not a directory",
1236 p);
1237
Saleem Abdulrasoolcf279a52017-02-05 17:21:52 +00001238 return p;
Eric Fiselier435db152016-06-17 19:46:40 +00001239}
1240
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001241path __weakly_canonical(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001242 ErrorHandler<path> err("weakly_canonical", ec, &p);
1243
Eric Fiselier91a182b2018-04-02 23:03:41 +00001244 if (p.empty())
1245 return __canonical("", ec);
Eric Fiselier435db152016-06-17 19:46:40 +00001246
Eric Fiselier91a182b2018-04-02 23:03:41 +00001247 path result;
1248 path tmp;
1249 tmp.__reserve(p.native().size());
1250 auto PP = PathParser::CreateEnd(p.native());
1251 --PP;
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001252 vector<string_view_t> DNEParts;
Eric Fiselier435db152016-06-17 19:46:40 +00001253
Eric Fiselier91a182b2018-04-02 23:03:41 +00001254 while (PP.State != PathParser::PS_BeforeBegin) {
1255 tmp.assign(createView(p.native().data(), &PP.RawEntry.back()));
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001256 error_code m_ec;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001257 file_status st = __status(tmp, &m_ec);
1258 if (!status_known(st)) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001259 return err.report(m_ec);
Eric Fiselier91a182b2018-04-02 23:03:41 +00001260 } else if (exists(st)) {
1261 result = __canonical(tmp, ec);
1262 break;
Eric Fiselier435db152016-06-17 19:46:40 +00001263 }
Eric Fiselier91a182b2018-04-02 23:03:41 +00001264 DNEParts.push_back(*PP);
1265 --PP;
1266 }
1267 if (PP.State == PathParser::PS_BeforeBegin)
1268 result = __canonical("", ec);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001269 if (ec)
1270 ec->clear();
Eric Fiselier91a182b2018-04-02 23:03:41 +00001271 if (DNEParts.empty())
1272 return result;
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001273 for (auto It = DNEParts.rbegin(); It != DNEParts.rend(); ++It)
Eric Fiselier91a182b2018-04-02 23:03:41 +00001274 result /= *It;
1275 return result.lexically_normal();
Eric Fiselier435db152016-06-17 19:46:40 +00001276}
1277
Eric Fiselier91a182b2018-04-02 23:03:41 +00001278///////////////////////////////////////////////////////////////////////////////
1279// path definitions
1280///////////////////////////////////////////////////////////////////////////////
1281
1282constexpr path::value_type path::preferred_separator;
1283
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001284path& path::replace_extension(path const& replacement) {
1285 path p = extension();
1286 if (not p.empty()) {
1287 __pn_.erase(__pn_.size() - p.native().size());
1288 }
1289 if (!replacement.empty()) {
1290 if (replacement.native()[0] != '.') {
1291 __pn_ += ".";
Eric Fiselier91a182b2018-04-02 23:03:41 +00001292 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001293 __pn_.append(replacement.__pn_);
1294 }
1295 return *this;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001296}
1297
1298///////////////////////////////////////////////////////////////////////////////
1299// path.decompose
1300
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001301string_view_t path::__root_name() const {
1302 auto PP = PathParser::CreateBegin(__pn_);
1303 if (PP.State == PathParser::PS_InRootName)
1304 return *PP;
1305 return {};
Eric Fiselier91a182b2018-04-02 23:03:41 +00001306}
1307
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001308string_view_t path::__root_directory() const {
1309 auto PP = PathParser::CreateBegin(__pn_);
1310 if (PP.State == PathParser::PS_InRootName)
1311 ++PP;
1312 if (PP.State == PathParser::PS_InRootDir)
1313 return *PP;
1314 return {};
1315}
1316
1317string_view_t path::__root_path_raw() const {
1318 auto PP = PathParser::CreateBegin(__pn_);
1319 if (PP.State == PathParser::PS_InRootName) {
1320 auto NextCh = PP.peek();
1321 if (NextCh && *NextCh == '/') {
Eric Fiselier91a182b2018-04-02 23:03:41 +00001322 ++PP;
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001323 return createView(__pn_.data(), &PP.RawEntry.back());
Eric Fiselier91a182b2018-04-02 23:03:41 +00001324 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001325 return PP.RawEntry;
1326 }
1327 if (PP.State == PathParser::PS_InRootDir)
1328 return *PP;
1329 return {};
Eric Fiselier91a182b2018-04-02 23:03:41 +00001330}
1331
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001332static bool ConsumeRootName(PathParser *PP) {
1333 static_assert(PathParser::PS_BeforeBegin == 1 &&
1334 PathParser::PS_InRootName == 2,
1335 "Values for enums are incorrect");
1336 while (PP->State <= PathParser::PS_InRootName)
1337 ++(*PP);
1338 return PP->State == PathParser::PS_AtEnd;
1339}
1340
Eric Fiselier91a182b2018-04-02 23:03:41 +00001341static bool ConsumeRootDir(PathParser* PP) {
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001342 static_assert(PathParser::PS_BeforeBegin == 1 &&
1343 PathParser::PS_InRootName == 2 &&
1344 PathParser::PS_InRootDir == 3, "Values for enums are incorrect");
Eric Fiselier91a182b2018-04-02 23:03:41 +00001345 while (PP->State <= PathParser::PS_InRootDir)
1346 ++(*PP);
1347 return PP->State == PathParser::PS_AtEnd;
1348}
1349
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001350string_view_t path::__relative_path() const {
1351 auto PP = PathParser::CreateBegin(__pn_);
1352 if (ConsumeRootDir(&PP))
1353 return {};
1354 return createView(PP.RawEntry.data(), &__pn_.back());
1355}
1356
1357string_view_t path::__parent_path() const {
1358 if (empty())
1359 return {};
1360 // Determine if we have a root path but not a relative path. In that case
1361 // return *this.
1362 {
Eric Fiselier91a182b2018-04-02 23:03:41 +00001363 auto PP = PathParser::CreateBegin(__pn_);
1364 if (ConsumeRootDir(&PP))
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001365 return __pn_;
1366 }
1367 // Otherwise remove a single element from the end of the path, and return
1368 // a string representing that path
1369 {
1370 auto PP = PathParser::CreateEnd(__pn_);
1371 --PP;
1372 if (PP.RawEntry.data() == __pn_.data())
Eric Fiselier91a182b2018-04-02 23:03:41 +00001373 return {};
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001374 --PP;
1375 return createView(__pn_.data(), &PP.RawEntry.back());
1376 }
Eric Fiselier91a182b2018-04-02 23:03:41 +00001377}
1378
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001379string_view_t path::__filename() const {
1380 if (empty())
1381 return {};
1382 {
1383 PathParser PP = PathParser::CreateBegin(__pn_);
1384 if (ConsumeRootDir(&PP))
Eric Fiselier91a182b2018-04-02 23:03:41 +00001385 return {};
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001386 }
1387 return *(--PathParser::CreateEnd(__pn_));
Eric Fiselier91a182b2018-04-02 23:03:41 +00001388}
1389
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001390string_view_t path::__stem() const {
1391 return parser::separate_filename(__filename()).first;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001392}
1393
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001394string_view_t path::__extension() const {
1395 return parser::separate_filename(__filename()).second;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001396}
1397
1398////////////////////////////////////////////////////////////////////////////
1399// path.gen
1400
Eric Fiselier91a182b2018-04-02 23:03:41 +00001401enum PathPartKind : unsigned char {
1402 PK_None,
1403 PK_RootSep,
1404 PK_Filename,
1405 PK_Dot,
1406 PK_DotDot,
1407 PK_TrailingSep
1408};
1409
1410static PathPartKind ClassifyPathPart(string_view_t Part) {
1411 if (Part.empty())
1412 return PK_TrailingSep;
1413 if (Part == ".")
1414 return PK_Dot;
1415 if (Part == "..")
1416 return PK_DotDot;
1417 if (Part == "/")
1418 return PK_RootSep;
1419 return PK_Filename;
1420}
1421
1422path path::lexically_normal() const {
1423 if (__pn_.empty())
1424 return *this;
1425
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001426 using PartKindPair = pair<string_view_t, PathPartKind>;
1427 vector<PartKindPair> Parts;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001428 // Guess as to how many elements the path has to avoid reallocating.
1429 Parts.reserve(32);
1430
1431 // Track the total size of the parts as we collect them. This allows the
1432 // resulting path to reserve the correct amount of memory.
1433 size_t NewPathSize = 0;
1434 auto AddPart = [&](PathPartKind K, string_view_t P) {
1435 NewPathSize += P.size();
1436 Parts.emplace_back(P, K);
1437 };
1438 auto LastPartKind = [&]() {
1439 if (Parts.empty())
1440 return PK_None;
1441 return Parts.back().second;
1442 };
1443
1444 bool MaybeNeedTrailingSep = false;
1445 // Build a stack containing the remaining elements of the path, popping off
1446 // elements which occur before a '..' entry.
1447 for (auto PP = PathParser::CreateBegin(__pn_); PP; ++PP) {
1448 auto Part = *PP;
1449 PathPartKind Kind = ClassifyPathPart(Part);
1450 switch (Kind) {
1451 case PK_Filename:
1452 case PK_RootSep: {
1453 // Add all non-dot and non-dot-dot elements to the stack of elements.
1454 AddPart(Kind, Part);
1455 MaybeNeedTrailingSep = false;
1456 break;
1457 }
1458 case PK_DotDot: {
1459 // Only push a ".." element if there are no elements preceding the "..",
1460 // or if the preceding element is itself "..".
1461 auto LastKind = LastPartKind();
1462 if (LastKind == PK_Filename) {
1463 NewPathSize -= Parts.back().first.size();
1464 Parts.pop_back();
1465 } else if (LastKind != PK_RootSep)
1466 AddPart(PK_DotDot, "..");
1467 MaybeNeedTrailingSep = LastKind == PK_Filename;
1468 break;
1469 }
1470 case PK_Dot:
1471 case PK_TrailingSep: {
1472 MaybeNeedTrailingSep = true;
1473 break;
1474 }
1475 case PK_None:
1476 _LIBCPP_UNREACHABLE();
1477 }
1478 }
1479 // [fs.path.generic]p6.8: If the path is empty, add a dot.
1480 if (Parts.empty())
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001481 return ".";
Eric Fiselier91a182b2018-04-02 23:03:41 +00001482
1483 // [fs.path.generic]p6.7: If the last filename is dot-dot, remove any
1484 // trailing directory-separator.
1485 bool NeedTrailingSep = MaybeNeedTrailingSep && LastPartKind() == PK_Filename;
1486
1487 path Result;
1488 Result.__pn_.reserve(Parts.size() + NewPathSize + NeedTrailingSep);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001489 for (auto& PK : Parts)
Eric Fiselier91a182b2018-04-02 23:03:41 +00001490 Result /= PK.first;
1491
1492 if (NeedTrailingSep)
1493 Result /= "";
1494
1495 return Result;
1496}
1497
1498static int DetermineLexicalElementCount(PathParser PP) {
1499 int Count = 0;
1500 for (; PP; ++PP) {
1501 auto Elem = *PP;
1502 if (Elem == "..")
1503 --Count;
Eric Fiselier9c4949a2018-12-21 04:25:40 +00001504 else if (Elem != "." && Elem != "")
Eric Fiselier91a182b2018-04-02 23:03:41 +00001505 ++Count;
1506 }
1507 return Count;
1508}
1509
1510path path::lexically_relative(const path& base) const {
1511 { // perform root-name/root-directory mismatch checks
1512 auto PP = PathParser::CreateBegin(__pn_);
1513 auto PPBase = PathParser::CreateBegin(base.__pn_);
1514 auto CheckIterMismatchAtBase = [&]() {
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001515 return PP.State != PPBase.State &&
1516 (PP.inRootPath() || PPBase.inRootPath());
Eric Fiselier91a182b2018-04-02 23:03:41 +00001517 };
Eric Fiselier9c4949a2018-12-21 04:25:40 +00001518 if (PP.inRootName() && PPBase.inRootName()) {
Eric Fiselier91a182b2018-04-02 23:03:41 +00001519 if (*PP != *PPBase)
1520 return {};
1521 } else if (CheckIterMismatchAtBase())
1522 return {};
1523
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001524 if (PP.inRootPath())
1525 ++PP;
1526 if (PPBase.inRootPath())
1527 ++PPBase;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001528 if (CheckIterMismatchAtBase())
1529 return {};
1530 }
1531
1532 // Find the first mismatching element
1533 auto PP = PathParser::CreateBegin(__pn_);
1534 auto PPBase = PathParser::CreateBegin(base.__pn_);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001535 while (PP && PPBase && PP.State == PPBase.State && *PP == *PPBase) {
Eric Fiselier91a182b2018-04-02 23:03:41 +00001536 ++PP;
1537 ++PPBase;
1538 }
1539
1540 // If there is no mismatch, return ".".
1541 if (!PP && !PPBase)
1542 return ".";
1543
1544 // Otherwise, determine the number of elements, 'n', which are not dot or
1545 // dot-dot minus the number of dot-dot elements.
1546 int ElemCount = DetermineLexicalElementCount(PPBase);
1547 if (ElemCount < 0)
1548 return {};
1549
Eric Fiselier9c4949a2018-12-21 04:25:40 +00001550 // if n == 0 and (a == end() || a->empty()), returns path("."); otherwise
1551 if (ElemCount == 0 && (PP.atEnd() || *PP == ""))
1552 return ".";
1553
Eric Fiselier91a182b2018-04-02 23:03:41 +00001554 // return a path constructed with 'n' dot-dot elements, followed by the the
1555 // elements of '*this' after the mismatch.
1556 path Result;
1557 // FIXME: Reserve enough room in Result that it won't have to re-allocate.
1558 while (ElemCount--)
1559 Result /= "..";
1560 for (; PP; ++PP)
1561 Result /= *PP;
1562 return Result;
1563}
1564
1565////////////////////////////////////////////////////////////////////////////
1566// path.comparisons
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001567static int CompareRootName(PathParser *LHS, PathParser *RHS) {
1568 if (!LHS->inRootName() && !RHS->inRootName())
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001569 return 0;
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001570
1571 auto GetRootName = [](PathParser *Parser) -> string_view_t {
1572 return Parser->inRootName() ? **Parser : "";
1573 };
1574 int res = GetRootName(LHS).compare(GetRootName(RHS));
1575 ConsumeRootName(LHS);
1576 ConsumeRootName(RHS);
1577 return res;
1578}
1579
1580static int CompareRootDir(PathParser *LHS, PathParser *RHS) {
1581 if (!LHS->inRootDir() && RHS->inRootDir())
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001582 return -1;
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001583 else if (LHS->inRootDir() && !RHS->inRootDir())
1584 return 1;
1585 else {
1586 ConsumeRootDir(LHS);
1587 ConsumeRootDir(RHS);
1588 return 0;
1589 }
1590}
1591
1592static int CompareRelative(PathParser *LHSPtr, PathParser *RHSPtr) {
1593 auto &LHS = *LHSPtr;
1594 auto &RHS = *RHSPtr;
Stephan T. Lavavejfb39ad72019-10-23 11:45:36 -07001595
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001596 int res;
1597 while (LHS && RHS) {
1598 if ((res = (*LHS).compare(*RHS)) != 0)
1599 return res;
1600 ++LHS;
1601 ++RHS;
1602 }
1603 return 0;
1604}
1605
1606static int CompareEndState(PathParser *LHS, PathParser *RHS) {
1607 if (LHS->atEnd() && !RHS->atEnd())
1608 return -1;
1609 else if (!LHS->atEnd() && RHS->atEnd())
1610 return 1;
1611 return 0;
1612}
1613
1614int path::__compare(string_view_t __s) const {
1615 auto LHS = PathParser::CreateBegin(__pn_);
1616 auto RHS = PathParser::CreateBegin(__s);
1617 int res;
1618
1619 if ((res = CompareRootName(&LHS, &RHS)) != 0)
1620 return res;
1621
1622 if ((res = CompareRootDir(&LHS, &RHS)) != 0)
1623 return res;
1624
1625 if ((res = CompareRelative(&LHS, &RHS)) != 0)
1626 return res;
1627
1628 return CompareEndState(&LHS, &RHS);
Eric Fiselier91a182b2018-04-02 23:03:41 +00001629}
1630
1631////////////////////////////////////////////////////////////////////////////
1632// path.nonmembers
1633size_t hash_value(const path& __p) noexcept {
1634 auto PP = PathParser::CreateBegin(__p.native());
1635 size_t hash_value = 0;
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001636 hash<string_view_t> hasher;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001637 while (PP) {
1638 hash_value = __hash_combine(hash_value, hasher(*PP));
1639 ++PP;
1640 }
1641 return hash_value;
1642}
1643
1644////////////////////////////////////////////////////////////////////////////
1645// path.itr
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001646path::iterator path::begin() const {
1647 auto PP = PathParser::CreateBegin(__pn_);
1648 iterator it;
1649 it.__path_ptr_ = this;
1650 it.__state_ = static_cast<path::iterator::_ParserState>(PP.State);
1651 it.__entry_ = PP.RawEntry;
1652 it.__stashed_elem_.__assign_view(*PP);
1653 return it;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001654}
1655
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001656path::iterator path::end() const {
1657 iterator it{};
1658 it.__state_ = path::iterator::_AtEnd;
1659 it.__path_ptr_ = this;
1660 return it;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001661}
1662
1663path::iterator& path::iterator::__increment() {
Eric Fiselier91a182b2018-04-02 23:03:41 +00001664 PathParser PP(__path_ptr_->native(), __entry_, __state_);
1665 ++PP;
Eric Fiselier23a120c2018-07-25 03:31:48 +00001666 __state_ = static_cast<_ParserState>(PP.State);
Eric Fiselier91a182b2018-04-02 23:03:41 +00001667 __entry_ = PP.RawEntry;
1668 __stashed_elem_.__assign_view(*PP);
1669 return *this;
1670}
1671
1672path::iterator& path::iterator::__decrement() {
1673 PathParser PP(__path_ptr_->native(), __entry_, __state_);
1674 --PP;
Eric Fiselier23a120c2018-07-25 03:31:48 +00001675 __state_ = static_cast<_ParserState>(PP.State);
Eric Fiselier91a182b2018-04-02 23:03:41 +00001676 __entry_ = PP.RawEntry;
1677 __stashed_elem_.__assign_view(*PP);
1678 return *this;
1679}
1680
Eric Fiselier70474082018-07-20 01:22:32 +00001681///////////////////////////////////////////////////////////////////////////////
1682// directory entry definitions
1683///////////////////////////////////////////////////////////////////////////////
1684
1685#ifndef _LIBCPP_WIN32API
1686error_code directory_entry::__do_refresh() noexcept {
1687 __data_.__reset();
1688 error_code failure_ec;
1689
Eric Fiselier7eba47e2018-07-25 20:51:49 +00001690 StatT full_st;
Eric Fiselier70474082018-07-20 01:22:32 +00001691 file_status st = detail::posix_lstat(__p_, full_st, &failure_ec);
1692 if (!status_known(st)) {
1693 __data_.__reset();
1694 return failure_ec;
1695 }
1696
1697 if (!_VSTD_FS::exists(st) || !_VSTD_FS::is_symlink(st)) {
1698 __data_.__cache_type_ = directory_entry::_RefreshNonSymlink;
1699 __data_.__type_ = st.type();
1700 __data_.__non_sym_perms_ = st.permissions();
1701 } else { // we have a symlink
1702 __data_.__sym_perms_ = st.permissions();
1703 // Get the information about the linked entity.
1704 // Ignore errors from stat, since we don't want errors regarding symlink
1705 // resolution to be reported to the user.
1706 error_code ignored_ec;
1707 st = detail::posix_stat(__p_, full_st, &ignored_ec);
1708
1709 __data_.__type_ = st.type();
1710 __data_.__non_sym_perms_ = st.permissions();
1711
1712 // If we failed to resolve the link, then only partially populate the
1713 // cache.
1714 if (!status_known(st)) {
1715 __data_.__cache_type_ = directory_entry::_RefreshSymlinkUnresolved;
1716 return error_code{};
1717 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001718 // Otherwise, we resolved the link, potentially as not existing.
Eric Fiseliere39cea92018-07-20 08:36:45 +00001719 // That's OK.
Eric Fiselier70474082018-07-20 01:22:32 +00001720 __data_.__cache_type_ = directory_entry::_RefreshSymlink;
1721 }
1722
1723 if (_VSTD_FS::is_regular_file(st))
1724 __data_.__size_ = static_cast<uintmax_t>(full_st.st_size);
1725
1726 if (_VSTD_FS::exists(st)) {
1727 __data_.__nlink_ = static_cast<uintmax_t>(full_st.st_nlink);
1728
1729 // Attempt to extract the mtime, and fail if it's not representable using
1730 // file_time_type. For now we ignore the error, as we'll report it when
1731 // the value is actually used.
1732 error_code ignored_ec;
1733 __data_.__write_time_ =
1734 __extract_last_write_time(__p_, full_st, &ignored_ec);
1735 }
1736
1737 return failure_ec;
1738}
1739#else
1740error_code directory_entry::__do_refresh() noexcept {
1741 __data_.__reset();
1742 error_code failure_ec;
1743
1744 file_status st = _VSTD_FS::symlink_status(__p_, failure_ec);
1745 if (!status_known(st)) {
1746 __data_.__reset();
1747 return failure_ec;
1748 }
1749
1750 if (!_VSTD_FS::exists(st) || !_VSTD_FS::is_symlink(st)) {
1751 __data_.__cache_type_ = directory_entry::_RefreshNonSymlink;
1752 __data_.__type_ = st.type();
1753 __data_.__non_sym_perms_ = st.permissions();
1754 } else { // we have a symlink
1755 __data_.__sym_perms_ = st.permissions();
1756 // Get the information about the linked entity.
1757 // Ignore errors from stat, since we don't want errors regarding symlink
1758 // resolution to be reported to the user.
1759 error_code ignored_ec;
1760 st = _VSTD_FS::status(__p_, ignored_ec);
1761
1762 __data_.__type_ = st.type();
1763 __data_.__non_sym_perms_ = st.permissions();
1764
1765 // If we failed to resolve the link, then only partially populate the
1766 // cache.
1767 if (!status_known(st)) {
1768 __data_.__cache_type_ = directory_entry::_RefreshSymlinkUnresolved;
1769 return error_code{};
1770 }
Eric Fiselier70474082018-07-20 01:22:32 +00001771 __data_.__cache_type_ = directory_entry::_RefreshSymlink;
1772 }
1773
1774 // FIXME: This is currently broken, and the implementation only a placeholder.
1775 // We need to cache last_write_time, file_size, and hard_link_count here before
1776 // the implementation actually works.
1777
1778 return failure_ec;
1779}
1780#endif
Eric Fiselier91a182b2018-04-02 23:03:41 +00001781
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001782_LIBCPP_END_NAMESPACE_FILESYSTEM