blob: 0b79ef1cdac681048daab314f8df231840f7e3c5 [file] [log] [blame]
Eric Fiselier435db152016-06-17 19:46:40 +00001//===--------------------- filesystem/ops.cpp -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eric Fiselier02cea5e2018-07-27 03:07:09 +000010#include "filesystem"
Eric Fiseliera75bbde2018-07-23 02:00:52 +000011#include "array"
Eric Fiselier435db152016-06-17 19:46:40 +000012#include "iterator"
13#include "fstream"
Eric Fiselier02cea5e2018-07-27 03:07:09 +000014#include "random" /* for unique_path */
Eric Fiselier91a182b2018-04-02 23:03:41 +000015#include "string_view"
16#include "type_traits"
17#include "vector"
Eric Fiselier435db152016-06-17 19:46:40 +000018#include "cstdlib"
19#include "climits"
20
Eric Fiselier70474082018-07-20 01:22:32 +000021#include "filesystem_common.h"
Eric Fiselier42d6d2c2017-07-08 04:18:41 +000022
Eric Fiselier435db152016-06-17 19:46:40 +000023#include <unistd.h>
24#include <sys/stat.h>
25#include <sys/statvfs.h>
Eric Fiselier7eba47e2018-07-25 20:51:49 +000026#include <time.h>
Eric Fiselier02cea5e2018-07-27 03:07:09 +000027#include <fcntl.h> /* values for fchmodat */
Eric Fiselierabfdbdf2018-07-22 02:00:53 +000028
29#if defined(__linux__)
Eric Fiselier02cea5e2018-07-27 03:07:09 +000030#include <linux/version.h>
31#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33)
32#include <sys/sendfile.h>
33#define _LIBCPP_USE_SENDFILE
34#endif
Eric Fiselierabfdbdf2018-07-22 02:00:53 +000035#elif defined(__APPLE__) || __has_include(<copyfile.h>)
36#include <copyfile.h>
Eric Fiselier02cea5e2018-07-27 03:07:09 +000037#define _LIBCPP_USE_COPYFILE
Eric Fiselierabfdbdf2018-07-22 02:00:53 +000038#endif
Nico Weber4f1d63a2018-02-06 19:17:41 +000039
Eric Fiselier7eba47e2018-07-25 20:51:49 +000040#if !defined(__APPLE__)
41#define _LIBCPP_USE_CLOCK_GETTIME
42#endif
43
44#if !defined(CLOCK_REALTIME) || !defined(_LIBCPP_USE_CLOCK_GETTIME)
45#include <sys/time.h> // for gettimeofday and timeval
46#endif // !defined(CLOCK_REALTIME)
47
Eric Fiselierd8b25e32018-07-23 03:06:57 +000048#if defined(_LIBCPP_COMPILER_GCC)
49#if _GNUC_VER < 500
50#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
51#endif
52#endif
53
Eric Fiselier02cea5e2018-07-27 03:07:09 +000054_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
Eric Fiselier435db152016-06-17 19:46:40 +000055
Eric Fiselier02cea5e2018-07-27 03:07:09 +000056namespace {
57namespace parser {
Eric Fiselier91a182b2018-04-02 23:03:41 +000058
59using string_view_t = path::__string_view;
60using string_view_pair = pair<string_view_t, string_view_t>;
61using PosPtr = path::value_type const*;
62
63struct PathParser {
64 enum ParserState : unsigned char {
65 // Zero is a special sentinel value used by default constructed iterators.
Eric Fiselier23a120c2018-07-25 03:31:48 +000066 PS_BeforeBegin = path::iterator::_BeforeBegin,
67 PS_InRootName = path::iterator::_InRootName,
68 PS_InRootDir = path::iterator::_InRootDir,
69 PS_InFilenames = path::iterator::_InFilenames,
70 PS_InTrailingSep = path::iterator::_InTrailingSep,
71 PS_AtEnd = path::iterator::_AtEnd
Eric Fiselier91a182b2018-04-02 23:03:41 +000072 };
73
74 const string_view_t Path;
75 string_view_t RawEntry;
76 ParserState State;
77
78private:
Eric Fiselier02cea5e2018-07-27 03:07:09 +000079 PathParser(string_view_t P, ParserState State) noexcept : Path(P),
80 State(State) {}
Eric Fiselier91a182b2018-04-02 23:03:41 +000081
82public:
83 PathParser(string_view_t P, string_view_t E, unsigned char S)
84 : Path(P), RawEntry(E), State(static_cast<ParserState>(S)) {
85 // S cannot be '0' or PS_BeforeBegin.
86 }
87
88 static PathParser CreateBegin(string_view_t P) noexcept {
89 PathParser PP(P, PS_BeforeBegin);
90 PP.increment();
91 return PP;
92 }
93
94 static PathParser CreateEnd(string_view_t P) noexcept {
95 PathParser PP(P, PS_AtEnd);
96 return PP;
97 }
98
99 PosPtr peek() const noexcept {
100 auto TkEnd = getNextTokenStartPos();
101 auto End = getAfterBack();
102 return TkEnd == End ? nullptr : TkEnd;
103 }
104
105 void increment() noexcept {
106 const PosPtr End = getAfterBack();
107 const PosPtr Start = getNextTokenStartPos();
108 if (Start == End)
109 return makeState(PS_AtEnd);
110
111 switch (State) {
112 case PS_BeforeBegin: {
113 PosPtr TkEnd = consumeSeparator(Start, End);
114 if (TkEnd)
115 return makeState(PS_InRootDir, Start, TkEnd);
116 else
117 return makeState(PS_InFilenames, Start, consumeName(Start, End));
118 }
119 case PS_InRootDir:
120 return makeState(PS_InFilenames, Start, consumeName(Start, End));
121
122 case PS_InFilenames: {
123 PosPtr SepEnd = consumeSeparator(Start, End);
124 if (SepEnd != End) {
125 PosPtr TkEnd = consumeName(SepEnd, End);
126 if (TkEnd)
127 return makeState(PS_InFilenames, SepEnd, TkEnd);
128 }
129 return makeState(PS_InTrailingSep, Start, SepEnd);
130 }
131
132 case PS_InTrailingSep:
133 return makeState(PS_AtEnd);
134
135 case PS_InRootName:
136 case PS_AtEnd:
137 _LIBCPP_UNREACHABLE();
138 }
139 }
140
141 void decrement() noexcept {
142 const PosPtr REnd = getBeforeFront();
143 const PosPtr RStart = getCurrentTokenStartPos() - 1;
144 if (RStart == REnd) // we're decrementing the begin
145 return makeState(PS_BeforeBegin);
146
147 switch (State) {
148 case PS_AtEnd: {
149 // Try to consume a trailing separator or root directory first.
150 if (PosPtr SepEnd = consumeSeparator(RStart, REnd)) {
151 if (SepEnd == REnd)
152 return makeState(PS_InRootDir, Path.data(), RStart + 1);
153 return makeState(PS_InTrailingSep, SepEnd + 1, RStart + 1);
154 } else {
155 PosPtr TkStart = consumeName(RStart, REnd);
156 return makeState(PS_InFilenames, TkStart + 1, RStart + 1);
157 }
158 }
159 case PS_InTrailingSep:
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000160 return makeState(PS_InFilenames, consumeName(RStart, REnd) + 1,
161 RStart + 1);
Eric Fiselier91a182b2018-04-02 23:03:41 +0000162 case PS_InFilenames: {
163 PosPtr SepEnd = consumeSeparator(RStart, REnd);
164 if (SepEnd == REnd)
165 return makeState(PS_InRootDir, Path.data(), RStart + 1);
166 PosPtr TkEnd = consumeName(SepEnd, REnd);
167 return makeState(PS_InFilenames, TkEnd + 1, SepEnd + 1);
168 }
169 case PS_InRootDir:
170 // return makeState(PS_InRootName, Path.data(), RStart + 1);
171 case PS_InRootName:
172 case PS_BeforeBegin:
173 _LIBCPP_UNREACHABLE();
174 }
175 }
176
177 /// \brief Return a view with the "preferred representation" of the current
178 /// element. For example trailing separators are represented as a '.'
179 string_view_t operator*() const noexcept {
180 switch (State) {
181 case PS_BeforeBegin:
182 case PS_AtEnd:
183 return "";
184 case PS_InRootDir:
185 return "/";
186 case PS_InTrailingSep:
187 return "";
188 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) {
295 if (s == "." || s == ".." || s.empty())
296 return string_view_pair{s, ""};
297 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
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000442bool posix_ftruncate(const FileDescriptor& fd, size_t to_size, error_code& ec) {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000443 if (::ftruncate(fd.fd, to_size) == -1) {
444 ec = capture_errno();
Eric Fiselierf1aba0d2018-07-26 04:02:06 +0000445 return true;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000446 }
447 ec.clear();
Eric Fiselierf1aba0d2018-07-26 04:02:06 +0000448 return false;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000449}
450
451bool posix_fchmod(const FileDescriptor& fd, const StatT& st, error_code& ec) {
452 if (::fchmod(fd.fd, st.st_mode) == -1) {
453 ec = capture_errno();
Eric Fiselierf1aba0d2018-07-26 04:02:06 +0000454 return true;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000455 }
456 ec.clear();
Eric Fiselierf1aba0d2018-07-26 04:02:06 +0000457 return false;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000458}
459
460bool stat_equivalent(const StatT& st1, const StatT& st2) {
Eric Fiselier70474082018-07-20 01:22:32 +0000461 return (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino);
Eric Fiselier435db152016-06-17 19:46:40 +0000462}
463
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000464file_status FileDescriptor::refresh_status(error_code& ec) {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000465 // FD must be open and good.
466 m_status = file_status{};
Eric Fiselierd8b25e32018-07-23 03:06:57 +0000467 m_stat = {};
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000468 error_code m_ec;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000469 if (::fstat(fd, &m_stat) == -1)
470 m_ec = capture_errno();
471 m_status = create_file_status(m_ec, name, m_stat, &ec);
472 return m_status;
Eric Fiselier435db152016-06-17 19:46:40 +0000473}
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000474} // namespace
475} // end namespace detail
Eric Fiselier435db152016-06-17 19:46:40 +0000476
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000477using detail::capture_errno;
478using detail::ErrorHandler;
479using detail::StatT;
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000480using detail::TimeSpec;
Eric Fiselier91a182b2018-04-02 23:03:41 +0000481using parser::createView;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000482using parser::PathParser;
483using parser::string_view_t;
484
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000485const bool _FilesystemClock::is_steady;
486
487_FilesystemClock::time_point _FilesystemClock::now() noexcept {
488 typedef chrono::duration<rep> __secs;
489#if defined(_LIBCPP_USE_CLOCK_GETTIME) && defined(CLOCK_REALTIME)
490 typedef chrono::duration<rep, nano> __nsecs;
491 struct timespec tp;
492 if (0 != clock_gettime(CLOCK_REALTIME, &tp))
493 __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed");
494 return time_point(__secs(tp.tv_sec) +
495 chrono::duration_cast<duration>(__nsecs(tp.tv_nsec)));
496#else
497 typedef chrono::duration<rep, micro> __microsecs;
498 timeval tv;
499 gettimeofday(&tv, 0);
500 return time_point(__secs(tv.tv_sec) + __microsecs(tv.tv_usec));
501#endif // _LIBCPP_USE_CLOCK_GETTIME && CLOCK_REALTIME
502}
503
504filesystem_error::~filesystem_error() {}
505
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000506void filesystem_error::__create_what(int __num_paths) {
507 const char* derived_what = system_error::what();
508 __storage_->__what_ = [&]() -> string {
509 const char* p1 = path1().native().empty() ? "\"\"" : path1().c_str();
510 const char* p2 = path2().native().empty() ? "\"\"" : path2().c_str();
511 switch (__num_paths) {
512 default:
513 return detail::format_string("filesystem error: %s", derived_what);
514 case 1:
515 return detail::format_string("filesystem error: %s [%s]", derived_what,
516 p1);
517 case 2:
518 return detail::format_string("filesystem error: %s [%s] [%s]",
519 derived_what, p1, p2);
520 }
521 }();
522}
Eric Fiselier435db152016-06-17 19:46:40 +0000523
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000524static path __do_absolute(const path& p, path* cwd, error_code* ec) {
525 if (ec)
526 ec->clear();
527 if (p.is_absolute())
528 return p;
529 *cwd = __current_path(ec);
530 if (ec && *ec)
531 return {};
532 return (*cwd) / p;
Eric Fiselier91a182b2018-04-02 23:03:41 +0000533}
534
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000535path __absolute(const path& p, error_code* ec) {
536 path cwd;
537 return __do_absolute(p, &cwd, ec);
Eric Fiselier91a182b2018-04-02 23:03:41 +0000538}
539
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000540path __canonical(path const& orig_p, error_code* ec) {
541 path cwd;
542 ErrorHandler<path> err("canonical", ec, &orig_p, &cwd);
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000543
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000544 path p = __do_absolute(orig_p, &cwd, ec);
545 char buff[PATH_MAX + 1];
546 char* ret;
547 if ((ret = ::realpath(p.c_str(), buff)) == nullptr)
548 return err.report(capture_errno());
549 return {ret};
Eric Fiselier435db152016-06-17 19:46:40 +0000550}
551
552void __copy(const path& from, const path& to, copy_options options,
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000553 error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000554 ErrorHandler<void> err("copy", ec, &from, &to);
Eric Fiselier435db152016-06-17 19:46:40 +0000555
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000556 const bool sym_status = bool(
557 options & (copy_options::create_symlinks | copy_options::skip_symlinks));
Eric Fiselier435db152016-06-17 19:46:40 +0000558
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000559 const bool sym_status2 = bool(options & copy_options::copy_symlinks);
Eric Fiselier435db152016-06-17 19:46:40 +0000560
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000561 error_code m_ec1;
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000562 StatT f_st = {};
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000563 const file_status f = sym_status || sym_status2
564 ? detail::posix_lstat(from, f_st, &m_ec1)
565 : detail::posix_stat(from, f_st, &m_ec1);
566 if (m_ec1)
567 return err.report(m_ec1);
Eric Fiselier435db152016-06-17 19:46:40 +0000568
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000569 StatT t_st = {};
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000570 const file_status t = sym_status ? detail::posix_lstat(to, t_st, &m_ec1)
571 : detail::posix_stat(to, t_st, &m_ec1);
Eric Fiselier435db152016-06-17 19:46:40 +0000572
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000573 if (not status_known(t))
574 return err.report(m_ec1);
575
576 if (!exists(f) || is_other(f) || is_other(t) ||
577 (is_directory(f) && is_regular_file(t)) ||
578 detail::stat_equivalent(f_st, t_st)) {
579 return err.report(errc::function_not_supported);
580 }
Eric Fiselier435db152016-06-17 19:46:40 +0000581
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000582 if (ec)
583 ec->clear();
Eric Fiselier435db152016-06-17 19:46:40 +0000584
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000585 if (is_symlink(f)) {
586 if (bool(copy_options::skip_symlinks & options)) {
587 // do nothing
588 } else if (not exists(t)) {
589 __copy_symlink(from, to, ec);
590 } else {
591 return err.report(errc::file_exists);
Eric Fiselier435db152016-06-17 19:46:40 +0000592 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000593 return;
594 } else if (is_regular_file(f)) {
595 if (bool(copy_options::directories_only & options)) {
596 // do nothing
597 } else if (bool(copy_options::create_symlinks & options)) {
598 __create_symlink(from, to, ec);
599 } else if (bool(copy_options::create_hard_links & options)) {
600 __create_hard_link(from, to, ec);
601 } else if (is_directory(t)) {
602 __copy_file(from, to / from.filename(), options, ec);
603 } else {
604 __copy_file(from, to, options, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000605 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000606 return;
607 } else if (is_directory(f) && bool(copy_options::create_symlinks & options)) {
608 return err.report(errc::is_a_directory);
609 } else if (is_directory(f) && (bool(copy_options::recursive & options) ||
610 copy_options::none == options)) {
Eric Fiselier435db152016-06-17 19:46:40 +0000611
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000612 if (!exists(t)) {
613 // create directory to with attributes from 'from'.
614 __create_directory(to, from, ec);
615 if (ec && *ec) {
616 return;
617 }
Eric Fiselier435db152016-06-17 19:46:40 +0000618 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000619 directory_iterator it =
620 ec ? directory_iterator(from, *ec) : directory_iterator(from);
621 if (ec && *ec) {
622 return;
623 }
624 error_code m_ec2;
625 for (; it != directory_iterator(); it.increment(m_ec2)) {
626 if (m_ec2) {
627 return err.report(m_ec2);
628 }
629 __copy(it->path(), to / it->path().filename(),
630 options | copy_options::__in_recursive_copy, ec);
631 if (ec && *ec) {
632 return;
633 }
634 }
635 }
Eric Fiselier435db152016-06-17 19:46:40 +0000636}
637
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000638namespace detail {
639namespace {
640
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000641#ifdef _LIBCPP_USE_SENDFILE
642bool copy_file_impl_sendfile(FileDescriptor& read_fd, FileDescriptor& write_fd,
643 error_code& ec) {
644
645 size_t count = read_fd.get_stat().st_size;
646 do {
647 ssize_t res;
648 if ((res = ::sendfile(write_fd.fd, read_fd.fd, nullptr, count)) == -1) {
649 ec = capture_errno();
650 return false;
651 }
652 count -= res;
653 } while (count > 0);
654
655 ec.clear();
656
657 return true;
658}
659#elif defined(_LIBCPP_USE_COPYFILE)
660bool copy_file_impl_copyfile(FileDescriptor& read_fd, FileDescriptor& write_fd,
661 error_code& ec) {
662 struct CopyFileState {
663 copyfile_state_t state;
664 CopyFileState() { state = copyfile_state_alloc(); }
665 ~CopyFileState() { copyfile_state_free(state); }
666
667 private:
668 CopyFileState(CopyFileState const&) = delete;
669 CopyFileState& operator=(CopyFileState const&) = delete;
670 };
671
672 CopyFileState cfs;
673 if (fcopyfile(read_fd.fd, write_fd.fd, cfs.state, COPYFILE_DATA) < 0) {
674 ec = capture_errno();
675 return false;
676 }
677
678 ec.clear();
679 return true;
680}
681#endif
682
683// Note: This function isn't guarded by ifdef's even though it may be unused
684// in order to assure it still compiles.
685__attribute__((unused)) bool copy_file_impl_default(FileDescriptor& read_fd,
686 FileDescriptor& write_fd,
687 error_code& ec) {
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000688 ifstream in;
689 in.__open(read_fd.fd, ios::binary);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000690 if (!in.is_open()) {
691 // This assumes that __open didn't reset the error code.
692 ec = capture_errno();
693 return false;
694 }
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000695 ofstream out;
696 out.__open(write_fd.fd, ios::binary);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000697 if (!out.is_open()) {
698 ec = capture_errno();
699 return false;
700 }
701
702 if (in.good() && out.good()) {
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000703 using InIt = istreambuf_iterator<char>;
704 using OutIt = ostreambuf_iterator<char>;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000705 InIt bin(in);
706 InIt ein;
707 OutIt bout(out);
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000708 copy(bin, ein, bout);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000709 }
710 if (out.fail() || in.fail()) {
711 ec = make_error_code(errc::io_error);
712 return false;
713 }
714
715 ec.clear();
716 return true;
717}
718
719bool copy_file_impl(FileDescriptor& from, FileDescriptor& to, error_code& ec) {
720#if defined(_LIBCPP_USE_SENDFILE)
721 return copy_file_impl_sendfile(from, to, ec);
722#elif defined(_LIBCPP_USE_COPYFILE)
723 return copy_file_impl_copyfile(from, to, ec);
724#else
725 return copy_file_impl_default(from, to, ec);
726#endif
727}
728
729} // namespace
730} // namespace detail
Eric Fiselier435db152016-06-17 19:46:40 +0000731
732bool __copy_file(const path& from, const path& to, copy_options options,
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000733 error_code* ec) {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000734 using detail::FileDescriptor;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000735 ErrorHandler<bool> err("copy_file", ec, &to, &from);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000736
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000737 error_code m_ec;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000738 FileDescriptor from_fd =
739 FileDescriptor::create_with_status(&from, m_ec, O_RDONLY | O_NONBLOCK);
740 if (m_ec)
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000741 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000742
743 auto from_st = from_fd.get_status();
744 StatT const& from_stat = from_fd.get_stat();
745 if (!is_regular_file(from_st)) {
746 if (not m_ec)
747 m_ec = make_error_code(errc::not_supported);
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000748 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000749 }
750
751 const bool skip_existing = bool(copy_options::skip_existing & options);
752 const bool update_existing = bool(copy_options::update_existing & options);
753 const bool overwrite_existing =
754 bool(copy_options::overwrite_existing & options);
755
756 StatT to_stat_path;
757 file_status to_st = detail::posix_stat(to, to_stat_path, &m_ec);
758 if (!status_known(to_st))
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000759 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000760
761 const bool to_exists = exists(to_st);
762 if (to_exists && !is_regular_file(to_st))
Eric Fiselier268fa832018-07-23 11:55:13 +0000763 return err.report(errc::not_supported);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000764
765 if (to_exists && detail::stat_equivalent(from_stat, to_stat_path))
Eric Fiselier268fa832018-07-23 11:55:13 +0000766 return err.report(errc::file_exists);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000767
768 if (to_exists && skip_existing)
769 return false;
770
Eric Fiselier455ac4b2018-07-22 21:15:15 +0000771 bool ShouldCopy = [&]() {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000772 if (to_exists && update_existing) {
773 auto from_time = detail::extract_mtime(from_stat);
774 auto to_time = detail::extract_mtime(to_stat_path);
775 if (from_time.tv_sec < to_time.tv_sec)
Eric Fiselier435db152016-06-17 19:46:40 +0000776 return false;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000777 if (from_time.tv_sec == to_time.tv_sec &&
778 from_time.tv_nsec <= to_time.tv_nsec)
Eric Fiseliere7359252016-10-16 00:47:59 +0000779 return false;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000780 return true;
Eric Fiseliere7359252016-10-16 00:47:59 +0000781 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000782 if (!to_exists || overwrite_existing)
783 return true;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000784 return err.report(errc::file_exists);
Eric Fiselier455ac4b2018-07-22 21:15:15 +0000785 }();
786 if (!ShouldCopy)
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000787 return false;
Eric Fiseliere7359252016-10-16 00:47:59 +0000788
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000789 // Don't truncate right away. We may not be opening the file we originally
790 // looked at; we'll check this later.
Eric Fiselier455ac4b2018-07-22 21:15:15 +0000791 int to_open_flags = O_WRONLY;
792 if (!to_exists)
793 to_open_flags |= O_CREAT;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000794 FileDescriptor to_fd = FileDescriptor::create_with_status(
795 &to, m_ec, to_open_flags, from_stat.st_mode);
796 if (m_ec)
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000797 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000798
799 if (to_exists) {
800 // Check that the file we initially stat'ed is equivalent to the one
801 // we opened.
Eric Fiselier455ac4b2018-07-22 21:15:15 +0000802 // FIXME: report this better.
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000803 if (!detail::stat_equivalent(to_stat_path, to_fd.get_stat()))
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000804 return err.report(errc::bad_file_descriptor);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000805
806 // Set the permissions and truncate the file we opened.
Eric Fiselierf1aba0d2018-07-26 04:02:06 +0000807 if (detail::posix_fchmod(to_fd, from_stat, m_ec))
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000808 return err.report(m_ec);
Eric Fiselierf1aba0d2018-07-26 04:02:06 +0000809 if (detail::posix_ftruncate(to_fd, 0, m_ec))
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000810 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000811 }
812
813 if (!copy_file_impl(from_fd, to_fd, m_ec)) {
814 // FIXME: Remove the dest file if we failed, and it didn't exist previously.
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000815 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000816 }
817
818 return true;
Eric Fiselier435db152016-06-17 19:46:40 +0000819}
820
821void __copy_symlink(const path& existing_symlink, const path& new_symlink,
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000822 error_code* ec) {
823 const path real_path(__read_symlink(existing_symlink, ec));
824 if (ec && *ec) {
825 return;
826 }
827 // NOTE: proposal says you should detect if you should call
828 // create_symlink or create_directory_symlink. I don't think this
829 // is needed with POSIX
830 __create_symlink(real_path, new_symlink, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000831}
832
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000833bool __create_directories(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000834 ErrorHandler<bool> err("create_directories", ec, &p);
Eric Fiselier435db152016-06-17 19:46:40 +0000835
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000836 error_code m_ec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000837 auto const st = detail::posix_stat(p, &m_ec);
838 if (!status_known(st))
839 return err.report(m_ec);
840 else if (is_directory(st))
841 return false;
842 else if (exists(st))
843 return err.report(errc::file_exists);
844
845 const path parent = p.parent_path();
846 if (!parent.empty()) {
847 const file_status parent_st = status(parent, m_ec);
848 if (not status_known(parent_st))
849 return err.report(m_ec);
850 if (not exists(parent_st)) {
851 __create_directories(parent, ec);
852 if (ec && *ec) {
853 return false;
854 }
Eric Fiselier435db152016-06-17 19:46:40 +0000855 }
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000856 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000857 return __create_directory(p, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000858}
859
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000860bool __create_directory(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000861 ErrorHandler<bool> err("create_directory", ec, &p);
862
863 if (::mkdir(p.c_str(), static_cast<int>(perms::all)) == 0)
864 return true;
Eric Fiselier7ca3db82018-07-25 04:46:32 +0000865 if (errno != EEXIST)
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000866 err.report(capture_errno());
867 return false;
Eric Fiselier435db152016-06-17 19:46:40 +0000868}
869
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000870bool __create_directory(path const& p, path const& attributes, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000871 ErrorHandler<bool> err("create_directory", ec, &p, &attributes);
872
873 StatT attr_stat;
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000874 error_code mec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000875 auto st = detail::posix_stat(attributes, attr_stat, &mec);
876 if (!status_known(st))
877 return err.report(mec);
Eric Fiselier7ca3db82018-07-25 04:46:32 +0000878 if (!is_directory(st))
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000879 return err.report(errc::not_a_directory,
880 "the specified attribute path is invalid");
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000881
882 if (::mkdir(p.c_str(), attr_stat.st_mode) == 0)
883 return true;
Eric Fiselier7ca3db82018-07-25 04:46:32 +0000884 if (errno != EEXIST)
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000885 err.report(capture_errno());
886 return false;
Eric Fiselier435db152016-06-17 19:46:40 +0000887}
888
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000889void __create_directory_symlink(path const& from, path const& to,
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000890 error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000891 ErrorHandler<void> err("create_directory_symlink", ec, &from, &to);
892 if (::symlink(from.c_str(), to.c_str()) != 0)
893 return err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +0000894}
895
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000896void __create_hard_link(const path& from, const path& to, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000897 ErrorHandler<void> err("create_hard_link", ec, &from, &to);
898 if (::link(from.c_str(), to.c_str()) == -1)
899 return err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +0000900}
901
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000902void __create_symlink(path const& from, path const& to, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000903 ErrorHandler<void> err("create_symlink", ec, &from, &to);
904 if (::symlink(from.c_str(), to.c_str()) == -1)
905 return err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +0000906}
907
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000908path __current_path(error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000909 ErrorHandler<path> err("current_path", ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000910
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000911 auto size = ::pathconf(".", _PC_PATH_MAX);
912 _LIBCPP_ASSERT(size >= 0, "pathconf returned a 0 as max size");
913
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000914 auto buff = unique_ptr<char[]>(new char[size + 1]);
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000915 char* ret;
916 if ((ret = ::getcwd(buff.get(), static_cast<size_t>(size))) == nullptr)
917 return err.report(capture_errno(), "call to getcwd failed");
918
919 return {buff.get()};
Eric Fiselier435db152016-06-17 19:46:40 +0000920}
921
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000922void __current_path(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000923 ErrorHandler<void> err("current_path", ec, &p);
924 if (::chdir(p.c_str()) == -1)
925 err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +0000926}
927
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000928bool __equivalent(const path& p1, const path& p2, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000929 ErrorHandler<bool> err("equivalent", ec, &p1, &p2);
930
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000931 error_code ec1, ec2;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000932 StatT st1 = {}, st2 = {};
933 auto s1 = detail::posix_stat(p1.native(), st1, &ec1);
934 if (!exists(s1))
935 return err.report(errc::not_supported);
936 auto s2 = detail::posix_stat(p2.native(), st2, &ec2);
937 if (!exists(s2))
938 return err.report(errc::not_supported);
939
940 return detail::stat_equivalent(st1, st2);
Eric Fiselier435db152016-06-17 19:46:40 +0000941}
942
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000943uintmax_t __file_size(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000944 ErrorHandler<uintmax_t> err("file_size", ec, &p);
945
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000946 error_code m_ec;
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000947 StatT st;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000948 file_status fst = detail::posix_stat(p, st, &m_ec);
949 if (!exists(fst) || !is_regular_file(fst)) {
950 errc error_kind =
951 is_directory(fst) ? errc::is_a_directory : errc::not_supported;
952 if (!m_ec)
953 m_ec = make_error_code(error_kind);
954 return err.report(m_ec);
955 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000956 // is_regular_file(p) == true
957 return static_cast<uintmax_t>(st.st_size);
Eric Fiselier435db152016-06-17 19:46:40 +0000958}
959
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000960uintmax_t __hard_link_count(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000961 ErrorHandler<uintmax_t> err("hard_link_count", ec, &p);
962
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000963 error_code m_ec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000964 StatT st;
965 detail::posix_stat(p, st, &m_ec);
966 if (m_ec)
967 return err.report(m_ec);
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000968 return static_cast<uintmax_t>(st.st_nlink);
Eric Fiselier435db152016-06-17 19:46:40 +0000969}
970
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000971bool __fs_is_empty(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000972 ErrorHandler<bool> err("is_empty", ec, &p);
Eric Fiselieraa8c61f2016-10-15 23:05:04 +0000973
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000974 error_code m_ec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000975 StatT pst;
976 auto st = detail::posix_stat(p, pst, &m_ec);
977 if (m_ec)
978 return err.report(m_ec);
979 else if (!is_directory(st) && !is_regular_file(st))
980 return err.report(errc::not_supported);
981 else if (is_directory(st)) {
982 auto it = ec ? directory_iterator(p, *ec) : directory_iterator(p);
983 if (ec && *ec)
984 return false;
985 return it == directory_iterator{};
986 } else if (is_regular_file(st))
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000987 return static_cast<uintmax_t>(pst.st_size) == 0;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000988
989 _LIBCPP_UNREACHABLE();
Eric Fiselier435db152016-06-17 19:46:40 +0000990}
991
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000992static file_time_type __extract_last_write_time(const path& p, const StatT& st,
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000993 error_code* ec) {
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000994 using detail::fs_time;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000995 ErrorHandler<file_time_type> err("last_write_time", ec, &p);
996
Eric Fiselier70474082018-07-20 01:22:32 +0000997 auto ts = detail::extract_mtime(st);
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000998 if (!fs_time::is_representable(ts))
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000999 return err.report(errc::value_too_large);
1000
Eric Fiselier7eba47e2018-07-25 20:51:49 +00001001 return fs_time::convert_from_timespec(ts);
Eric Fiselier70474082018-07-20 01:22:32 +00001002}
Eric Fiselier42d6d2c2017-07-08 04:18:41 +00001003
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001004file_time_type __last_write_time(const path& p, error_code* ec) {
1005 using namespace chrono;
1006 ErrorHandler<file_time_type> err("last_write_time", ec, &p);
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001007
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001008 error_code m_ec;
1009 StatT st;
1010 detail::posix_stat(p, st, &m_ec);
1011 if (m_ec)
1012 return err.report(m_ec);
1013 return __extract_last_write_time(p, st, ec);
Eric Fiselier435db152016-06-17 19:46:40 +00001014}
1015
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001016void __last_write_time(const path& p, file_time_type new_time, error_code* ec) {
1017 using detail::fs_time;
1018 ErrorHandler<void> 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 array<TimeSpec, 2> tbuf;
Eric Fiselier7eba47e2018-07-25 20:51:49 +00001022#if !defined(_LIBCPP_USE_UTIMENSAT)
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001023 // This implementation has a race condition between determining the
1024 // last access time and attempting to set it to the same value using
1025 // ::utimes
1026 StatT st;
1027 file_status fst = detail::posix_stat(p, st, &m_ec);
1028 if (m_ec)
1029 return err.report(m_ec);
1030 tbuf[0] = detail::extract_atime(st);
Eric Fiselier435db152016-06-17 19:46:40 +00001031#else
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001032 tbuf[0].tv_sec = 0;
1033 tbuf[0].tv_nsec = UTIME_OMIT;
Eric Fiselier435db152016-06-17 19:46:40 +00001034#endif
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001035 if (!fs_time::convert_to_timespec(tbuf[1], new_time))
1036 return err.report(errc::value_too_large);
Eric Fiselier70474082018-07-20 01:22:32 +00001037
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001038 detail::set_file_times(p, tbuf, m_ec);
1039 if (m_ec)
1040 return err.report(m_ec);
Eric Fiselier435db152016-06-17 19:46:40 +00001041}
1042
Eric Fiselier4f3dc0e2018-03-26 06:23:55 +00001043void __permissions(const path& p, perms prms, perm_options opts,
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001044 error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001045 ErrorHandler<void> err("permissions", ec, &p);
Eric Fiselier435db152016-06-17 19:46:40 +00001046
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001047 auto has_opt = [&](perm_options o) { return bool(o & opts); };
1048 const bool resolve_symlinks = !has_opt(perm_options::nofollow);
1049 const bool add_perms = has_opt(perm_options::add);
1050 const bool remove_perms = has_opt(perm_options::remove);
1051 _LIBCPP_ASSERT(
1052 (add_perms + remove_perms + has_opt(perm_options::replace)) == 1,
1053 "One and only one of the perm_options constants replace, add, or remove "
1054 "is present in opts");
1055
1056 bool set_sym_perms = false;
1057 prms &= perms::mask;
1058 if (!resolve_symlinks || (add_perms || remove_perms)) {
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001059 error_code m_ec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001060 file_status st = resolve_symlinks ? detail::posix_stat(p, &m_ec)
1061 : detail::posix_lstat(p, &m_ec);
1062 set_sym_perms = is_symlink(st);
1063 if (m_ec)
1064 return err.report(m_ec);
1065 _LIBCPP_ASSERT(st.permissions() != perms::unknown,
1066 "Permissions unexpectedly unknown");
1067 if (add_perms)
1068 prms |= st.permissions();
1069 else if (remove_perms)
1070 prms = st.permissions() & ~prms;
1071 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001072 const auto real_perms = detail::posix_convert_perms(prms);
Eric Fiselier435db152016-06-17 19:46:40 +00001073
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001074#if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_FDCWD)
1075 const int flags = set_sym_perms ? AT_SYMLINK_NOFOLLOW : 0;
1076 if (::fchmodat(AT_FDCWD, p.c_str(), real_perms, flags) == -1) {
1077 return err.report(capture_errno());
1078 }
1079#else
1080 if (set_sym_perms)
1081 return err.report(errc::operation_not_supported);
1082 if (::chmod(p.c_str(), real_perms) == -1) {
1083 return err.report(capture_errno());
1084 }
1085#endif
Eric Fiselier435db152016-06-17 19:46:40 +00001086}
1087
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001088path __read_symlink(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001089 ErrorHandler<path> err("read_symlink", ec, &p);
1090
1091 char buff[PATH_MAX + 1];
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001092 error_code m_ec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001093 ::ssize_t ret;
1094 if ((ret = ::readlink(p.c_str(), buff, PATH_MAX)) == -1) {
1095 return err.report(capture_errno());
1096 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001097 _LIBCPP_ASSERT(ret <= PATH_MAX, "TODO");
1098 _LIBCPP_ASSERT(ret > 0, "TODO");
1099 buff[ret] = 0;
1100 return {buff};
Eric Fiselier435db152016-06-17 19:46:40 +00001101}
1102
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001103bool __remove(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001104 ErrorHandler<bool> err("remove", ec, &p);
1105 if (::remove(p.c_str()) == -1) {
1106 if (errno != ENOENT)
1107 err.report(capture_errno());
1108 return false;
1109 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001110 return true;
Eric Fiselier435db152016-06-17 19:46:40 +00001111}
1112
1113namespace {
1114
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001115uintmax_t remove_all_impl(path const& p, error_code& ec) {
1116 const auto npos = static_cast<uintmax_t>(-1);
1117 const file_status st = __symlink_status(p, &ec);
1118 if (ec)
1119 return npos;
1120 uintmax_t count = 1;
1121 if (is_directory(st)) {
1122 for (directory_iterator it(p, ec); !ec && it != directory_iterator();
1123 it.increment(ec)) {
1124 auto other_count = remove_all_impl(it->path(), ec);
1125 if (ec)
1126 return npos;
1127 count += other_count;
Eric Fiselier435db152016-06-17 19:46:40 +00001128 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001129 if (ec)
1130 return npos;
1131 }
1132 if (!__remove(p, &ec))
1133 return npos;
1134 return count;
Eric Fiselier435db152016-06-17 19:46:40 +00001135}
1136
1137} // end namespace
1138
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001139uintmax_t __remove_all(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001140 ErrorHandler<uintmax_t> err("remove_all", ec, &p);
Ekaterina Vaartis52668f72018-01-11 17:04:29 +00001141
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001142 error_code mec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001143 auto count = remove_all_impl(p, mec);
1144 if (mec) {
1145 if (mec == errc::no_such_file_or_directory)
1146 return 0;
1147 return err.report(mec);
1148 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001149 return count;
Eric Fiselier435db152016-06-17 19:46:40 +00001150}
1151
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001152void __rename(const path& from, const path& to, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001153 ErrorHandler<void> err("rename", ec, &from, &to);
1154 if (::rename(from.c_str(), to.c_str()) == -1)
1155 err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +00001156}
1157
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001158void __resize_file(const path& p, uintmax_t size, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001159 ErrorHandler<void> err("resize_file", ec, &p);
1160 if (::truncate(p.c_str(), static_cast< ::off_t>(size)) == -1)
1161 return err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +00001162}
1163
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001164space_info __space(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001165 ErrorHandler<void> err("space", ec, &p);
1166 space_info si;
1167 struct statvfs m_svfs = {};
1168 if (::statvfs(p.c_str(), &m_svfs) == -1) {
1169 err.report(capture_errno());
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001170 si.capacity = si.free = si.available = static_cast<uintmax_t>(-1);
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001171 return si;
1172 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001173 // Multiply with overflow checking.
1174 auto do_mult = [&](uintmax_t& out, uintmax_t other) {
1175 out = other * m_svfs.f_frsize;
1176 if (other == 0 || out / other != m_svfs.f_frsize)
1177 out = static_cast<uintmax_t>(-1);
1178 };
1179 do_mult(si.capacity, m_svfs.f_blocks);
1180 do_mult(si.free, m_svfs.f_bfree);
1181 do_mult(si.available, m_svfs.f_bavail);
1182 return si;
Eric Fiselier435db152016-06-17 19:46:40 +00001183}
1184
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001185file_status __status(const path& p, error_code* ec) {
1186 return detail::posix_stat(p, ec);
Eric Fiselier435db152016-06-17 19:46:40 +00001187}
1188
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001189file_status __symlink_status(const path& p, error_code* ec) {
1190 return detail::posix_lstat(p, ec);
Eric Fiselier435db152016-06-17 19:46:40 +00001191}
1192
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001193path __temp_directory_path(error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001194 ErrorHandler<path> err("temp_directory_path", ec);
1195
Saleem Abdulrasoolcf279a52017-02-05 17:21:52 +00001196 const char* env_paths[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"};
1197 const char* ret = nullptr;
1198
1199 for (auto& ep : env_paths)
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001200 if ((ret = getenv(ep)))
Saleem Abdulrasoolcf279a52017-02-05 17:21:52 +00001201 break;
1202 if (ret == nullptr)
1203 ret = "/tmp";
1204
1205 path p(ret);
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001206 error_code m_ec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001207 file_status st = detail::posix_stat(p, &m_ec);
1208 if (!status_known(st))
1209 return err.report(m_ec, "cannot access path \"%s\"", p);
Saleem Abdulrasoolcf279a52017-02-05 17:21:52 +00001210
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001211 if (!exists(st) || !is_directory(st))
1212 return err.report(errc::not_a_directory, "path \"%s\" is not a directory",
1213 p);
1214
Saleem Abdulrasoolcf279a52017-02-05 17:21:52 +00001215 return p;
Eric Fiselier435db152016-06-17 19:46:40 +00001216}
1217
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001218path __weakly_canonical(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001219 ErrorHandler<path> err("weakly_canonical", ec, &p);
1220
Eric Fiselier91a182b2018-04-02 23:03:41 +00001221 if (p.empty())
1222 return __canonical("", ec);
Eric Fiselier435db152016-06-17 19:46:40 +00001223
Eric Fiselier91a182b2018-04-02 23:03:41 +00001224 path result;
1225 path tmp;
1226 tmp.__reserve(p.native().size());
1227 auto PP = PathParser::CreateEnd(p.native());
1228 --PP;
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001229 vector<string_view_t> DNEParts;
Eric Fiselier435db152016-06-17 19:46:40 +00001230
Eric Fiselier91a182b2018-04-02 23:03:41 +00001231 while (PP.State != PathParser::PS_BeforeBegin) {
1232 tmp.assign(createView(p.native().data(), &PP.RawEntry.back()));
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001233 error_code m_ec;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001234 file_status st = __status(tmp, &m_ec);
1235 if (!status_known(st)) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001236 return err.report(m_ec);
Eric Fiselier91a182b2018-04-02 23:03:41 +00001237 } else if (exists(st)) {
1238 result = __canonical(tmp, ec);
1239 break;
Eric Fiselier435db152016-06-17 19:46:40 +00001240 }
Eric Fiselier91a182b2018-04-02 23:03:41 +00001241 DNEParts.push_back(*PP);
1242 --PP;
1243 }
1244 if (PP.State == PathParser::PS_BeforeBegin)
1245 result = __canonical("", ec);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001246 if (ec)
1247 ec->clear();
Eric Fiselier91a182b2018-04-02 23:03:41 +00001248 if (DNEParts.empty())
1249 return result;
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001250 for (auto It = DNEParts.rbegin(); It != DNEParts.rend(); ++It)
Eric Fiselier91a182b2018-04-02 23:03:41 +00001251 result /= *It;
1252 return result.lexically_normal();
Eric Fiselier435db152016-06-17 19:46:40 +00001253}
1254
Eric Fiselier91a182b2018-04-02 23:03:41 +00001255///////////////////////////////////////////////////////////////////////////////
1256// path definitions
1257///////////////////////////////////////////////////////////////////////////////
1258
1259constexpr path::value_type path::preferred_separator;
1260
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001261path& path::replace_extension(path const& replacement) {
1262 path p = extension();
1263 if (not p.empty()) {
1264 __pn_.erase(__pn_.size() - p.native().size());
1265 }
1266 if (!replacement.empty()) {
1267 if (replacement.native()[0] != '.') {
1268 __pn_ += ".";
Eric Fiselier91a182b2018-04-02 23:03:41 +00001269 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001270 __pn_.append(replacement.__pn_);
1271 }
1272 return *this;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001273}
1274
1275///////////////////////////////////////////////////////////////////////////////
1276// path.decompose
1277
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001278string_view_t path::__root_name() const {
1279 auto PP = PathParser::CreateBegin(__pn_);
1280 if (PP.State == PathParser::PS_InRootName)
1281 return *PP;
1282 return {};
Eric Fiselier91a182b2018-04-02 23:03:41 +00001283}
1284
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001285string_view_t path::__root_directory() const {
1286 auto PP = PathParser::CreateBegin(__pn_);
1287 if (PP.State == PathParser::PS_InRootName)
1288 ++PP;
1289 if (PP.State == PathParser::PS_InRootDir)
1290 return *PP;
1291 return {};
1292}
1293
1294string_view_t path::__root_path_raw() const {
1295 auto PP = PathParser::CreateBegin(__pn_);
1296 if (PP.State == PathParser::PS_InRootName) {
1297 auto NextCh = PP.peek();
1298 if (NextCh && *NextCh == '/') {
Eric Fiselier91a182b2018-04-02 23:03:41 +00001299 ++PP;
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001300 return createView(__pn_.data(), &PP.RawEntry.back());
Eric Fiselier91a182b2018-04-02 23:03:41 +00001301 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001302 return PP.RawEntry;
1303 }
1304 if (PP.State == PathParser::PS_InRootDir)
1305 return *PP;
1306 return {};
Eric Fiselier91a182b2018-04-02 23:03:41 +00001307}
1308
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001309static bool ConsumeRootName(PathParser *PP) {
1310 static_assert(PathParser::PS_BeforeBegin == 1 &&
1311 PathParser::PS_InRootName == 2,
1312 "Values for enums are incorrect");
1313 while (PP->State <= PathParser::PS_InRootName)
1314 ++(*PP);
1315 return PP->State == PathParser::PS_AtEnd;
1316}
1317
Eric Fiselier91a182b2018-04-02 23:03:41 +00001318static bool ConsumeRootDir(PathParser* PP) {
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001319 static_assert(PathParser::PS_BeforeBegin == 1 &&
1320 PathParser::PS_InRootName == 2 &&
1321 PathParser::PS_InRootDir == 3, "Values for enums are incorrect");
Eric Fiselier91a182b2018-04-02 23:03:41 +00001322 while (PP->State <= PathParser::PS_InRootDir)
1323 ++(*PP);
1324 return PP->State == PathParser::PS_AtEnd;
1325}
1326
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001327string_view_t path::__relative_path() const {
1328 auto PP = PathParser::CreateBegin(__pn_);
1329 if (ConsumeRootDir(&PP))
1330 return {};
1331 return createView(PP.RawEntry.data(), &__pn_.back());
1332}
1333
1334string_view_t path::__parent_path() const {
1335 if (empty())
1336 return {};
1337 // Determine if we have a root path but not a relative path. In that case
1338 // return *this.
1339 {
Eric Fiselier91a182b2018-04-02 23:03:41 +00001340 auto PP = PathParser::CreateBegin(__pn_);
1341 if (ConsumeRootDir(&PP))
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001342 return __pn_;
1343 }
1344 // Otherwise remove a single element from the end of the path, and return
1345 // a string representing that path
1346 {
1347 auto PP = PathParser::CreateEnd(__pn_);
1348 --PP;
1349 if (PP.RawEntry.data() == __pn_.data())
Eric Fiselier91a182b2018-04-02 23:03:41 +00001350 return {};
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001351 --PP;
1352 return createView(__pn_.data(), &PP.RawEntry.back());
1353 }
Eric Fiselier91a182b2018-04-02 23:03:41 +00001354}
1355
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001356string_view_t path::__filename() const {
1357 if (empty())
1358 return {};
1359 {
1360 PathParser PP = PathParser::CreateBegin(__pn_);
1361 if (ConsumeRootDir(&PP))
Eric Fiselier91a182b2018-04-02 23:03:41 +00001362 return {};
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001363 }
1364 return *(--PathParser::CreateEnd(__pn_));
Eric Fiselier91a182b2018-04-02 23:03:41 +00001365}
1366
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001367string_view_t path::__stem() const {
1368 return parser::separate_filename(__filename()).first;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001369}
1370
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001371string_view_t path::__extension() const {
1372 return parser::separate_filename(__filename()).second;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001373}
1374
1375////////////////////////////////////////////////////////////////////////////
1376// path.gen
1377
Eric Fiselier91a182b2018-04-02 23:03:41 +00001378enum PathPartKind : unsigned char {
1379 PK_None,
1380 PK_RootSep,
1381 PK_Filename,
1382 PK_Dot,
1383 PK_DotDot,
1384 PK_TrailingSep
1385};
1386
1387static PathPartKind ClassifyPathPart(string_view_t Part) {
1388 if (Part.empty())
1389 return PK_TrailingSep;
1390 if (Part == ".")
1391 return PK_Dot;
1392 if (Part == "..")
1393 return PK_DotDot;
1394 if (Part == "/")
1395 return PK_RootSep;
1396 return PK_Filename;
1397}
1398
1399path path::lexically_normal() const {
1400 if (__pn_.empty())
1401 return *this;
1402
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001403 using PartKindPair = pair<string_view_t, PathPartKind>;
1404 vector<PartKindPair> Parts;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001405 // Guess as to how many elements the path has to avoid reallocating.
1406 Parts.reserve(32);
1407
1408 // Track the total size of the parts as we collect them. This allows the
1409 // resulting path to reserve the correct amount of memory.
1410 size_t NewPathSize = 0;
1411 auto AddPart = [&](PathPartKind K, string_view_t P) {
1412 NewPathSize += P.size();
1413 Parts.emplace_back(P, K);
1414 };
1415 auto LastPartKind = [&]() {
1416 if (Parts.empty())
1417 return PK_None;
1418 return Parts.back().second;
1419 };
1420
1421 bool MaybeNeedTrailingSep = false;
1422 // Build a stack containing the remaining elements of the path, popping off
1423 // elements which occur before a '..' entry.
1424 for (auto PP = PathParser::CreateBegin(__pn_); PP; ++PP) {
1425 auto Part = *PP;
1426 PathPartKind Kind = ClassifyPathPart(Part);
1427 switch (Kind) {
1428 case PK_Filename:
1429 case PK_RootSep: {
1430 // Add all non-dot and non-dot-dot elements to the stack of elements.
1431 AddPart(Kind, Part);
1432 MaybeNeedTrailingSep = false;
1433 break;
1434 }
1435 case PK_DotDot: {
1436 // Only push a ".." element if there are no elements preceding the "..",
1437 // or if the preceding element is itself "..".
1438 auto LastKind = LastPartKind();
1439 if (LastKind == PK_Filename) {
1440 NewPathSize -= Parts.back().first.size();
1441 Parts.pop_back();
1442 } else if (LastKind != PK_RootSep)
1443 AddPart(PK_DotDot, "..");
1444 MaybeNeedTrailingSep = LastKind == PK_Filename;
1445 break;
1446 }
1447 case PK_Dot:
1448 case PK_TrailingSep: {
1449 MaybeNeedTrailingSep = true;
1450 break;
1451 }
1452 case PK_None:
1453 _LIBCPP_UNREACHABLE();
1454 }
1455 }
1456 // [fs.path.generic]p6.8: If the path is empty, add a dot.
1457 if (Parts.empty())
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001458 return ".";
Eric Fiselier91a182b2018-04-02 23:03:41 +00001459
1460 // [fs.path.generic]p6.7: If the last filename is dot-dot, remove any
1461 // trailing directory-separator.
1462 bool NeedTrailingSep = MaybeNeedTrailingSep && LastPartKind() == PK_Filename;
1463
1464 path Result;
1465 Result.__pn_.reserve(Parts.size() + NewPathSize + NeedTrailingSep);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001466 for (auto& PK : Parts)
Eric Fiselier91a182b2018-04-02 23:03:41 +00001467 Result /= PK.first;
1468
1469 if (NeedTrailingSep)
1470 Result /= "";
1471
1472 return Result;
1473}
1474
1475static int DetermineLexicalElementCount(PathParser PP) {
1476 int Count = 0;
1477 for (; PP; ++PP) {
1478 auto Elem = *PP;
1479 if (Elem == "..")
1480 --Count;
1481 else if (Elem != ".")
1482 ++Count;
1483 }
1484 return Count;
1485}
1486
1487path path::lexically_relative(const path& base) const {
1488 { // perform root-name/root-directory mismatch checks
1489 auto PP = PathParser::CreateBegin(__pn_);
1490 auto PPBase = PathParser::CreateBegin(base.__pn_);
1491 auto CheckIterMismatchAtBase = [&]() {
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001492 return PP.State != PPBase.State &&
1493 (PP.inRootPath() || PPBase.inRootPath());
Eric Fiselier91a182b2018-04-02 23:03:41 +00001494 };
1495 if (PP.State == PathParser::PS_InRootName &&
1496 PPBase.State == PathParser::PS_InRootName) {
1497 if (*PP != *PPBase)
1498 return {};
1499 } else if (CheckIterMismatchAtBase())
1500 return {};
1501
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001502 if (PP.inRootPath())
1503 ++PP;
1504 if (PPBase.inRootPath())
1505 ++PPBase;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001506 if (CheckIterMismatchAtBase())
1507 return {};
1508 }
1509
1510 // Find the first mismatching element
1511 auto PP = PathParser::CreateBegin(__pn_);
1512 auto PPBase = PathParser::CreateBegin(base.__pn_);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001513 while (PP && PPBase && PP.State == PPBase.State && *PP == *PPBase) {
Eric Fiselier91a182b2018-04-02 23:03:41 +00001514 ++PP;
1515 ++PPBase;
1516 }
1517
1518 // If there is no mismatch, return ".".
1519 if (!PP && !PPBase)
1520 return ".";
1521
1522 // Otherwise, determine the number of elements, 'n', which are not dot or
1523 // dot-dot minus the number of dot-dot elements.
1524 int ElemCount = DetermineLexicalElementCount(PPBase);
1525 if (ElemCount < 0)
1526 return {};
1527
1528 // return a path constructed with 'n' dot-dot elements, followed by the the
1529 // elements of '*this' after the mismatch.
1530 path Result;
1531 // FIXME: Reserve enough room in Result that it won't have to re-allocate.
1532 while (ElemCount--)
1533 Result /= "..";
1534 for (; PP; ++PP)
1535 Result /= *PP;
1536 return Result;
1537}
1538
1539////////////////////////////////////////////////////////////////////////////
1540// path.comparisons
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001541static int CompareRootName(PathParser *LHS, PathParser *RHS) {
1542 if (!LHS->inRootName() && !RHS->inRootName())
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001543 return 0;
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001544
1545 auto GetRootName = [](PathParser *Parser) -> string_view_t {
1546 return Parser->inRootName() ? **Parser : "";
1547 };
1548 int res = GetRootName(LHS).compare(GetRootName(RHS));
1549 ConsumeRootName(LHS);
1550 ConsumeRootName(RHS);
1551 return res;
1552}
1553
1554static int CompareRootDir(PathParser *LHS, PathParser *RHS) {
1555 if (!LHS->inRootDir() && RHS->inRootDir())
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001556 return -1;
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001557 else if (LHS->inRootDir() && !RHS->inRootDir())
1558 return 1;
1559 else {
1560 ConsumeRootDir(LHS);
1561 ConsumeRootDir(RHS);
1562 return 0;
1563 }
1564}
1565
1566static int CompareRelative(PathParser *LHSPtr, PathParser *RHSPtr) {
1567 auto &LHS = *LHSPtr;
1568 auto &RHS = *RHSPtr;
1569
1570 int res;
1571 while (LHS && RHS) {
1572 if ((res = (*LHS).compare(*RHS)) != 0)
1573 return res;
1574 ++LHS;
1575 ++RHS;
1576 }
1577 return 0;
1578}
1579
1580static int CompareEndState(PathParser *LHS, PathParser *RHS) {
1581 if (LHS->atEnd() && !RHS->atEnd())
1582 return -1;
1583 else if (!LHS->atEnd() && RHS->atEnd())
1584 return 1;
1585 return 0;
1586}
1587
1588int path::__compare(string_view_t __s) const {
1589 auto LHS = PathParser::CreateBegin(__pn_);
1590 auto RHS = PathParser::CreateBegin(__s);
1591 int res;
1592
1593 if ((res = CompareRootName(&LHS, &RHS)) != 0)
1594 return res;
1595
1596 if ((res = CompareRootDir(&LHS, &RHS)) != 0)
1597 return res;
1598
1599 if ((res = CompareRelative(&LHS, &RHS)) != 0)
1600 return res;
1601
1602 return CompareEndState(&LHS, &RHS);
Eric Fiselier91a182b2018-04-02 23:03:41 +00001603}
1604
1605////////////////////////////////////////////////////////////////////////////
1606// path.nonmembers
1607size_t hash_value(const path& __p) noexcept {
1608 auto PP = PathParser::CreateBegin(__p.native());
1609 size_t hash_value = 0;
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001610 hash<string_view_t> hasher;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001611 while (PP) {
1612 hash_value = __hash_combine(hash_value, hasher(*PP));
1613 ++PP;
1614 }
1615 return hash_value;
1616}
1617
1618////////////////////////////////////////////////////////////////////////////
1619// path.itr
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001620path::iterator path::begin() const {
1621 auto PP = PathParser::CreateBegin(__pn_);
1622 iterator it;
1623 it.__path_ptr_ = this;
1624 it.__state_ = static_cast<path::iterator::_ParserState>(PP.State);
1625 it.__entry_ = PP.RawEntry;
1626 it.__stashed_elem_.__assign_view(*PP);
1627 return it;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001628}
1629
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001630path::iterator path::end() const {
1631 iterator it{};
1632 it.__state_ = path::iterator::_AtEnd;
1633 it.__path_ptr_ = this;
1634 return it;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001635}
1636
1637path::iterator& path::iterator::__increment() {
Eric Fiselier91a182b2018-04-02 23:03:41 +00001638 PathParser PP(__path_ptr_->native(), __entry_, __state_);
1639 ++PP;
Eric Fiselier23a120c2018-07-25 03:31:48 +00001640 __state_ = static_cast<_ParserState>(PP.State);
Eric Fiselier91a182b2018-04-02 23:03:41 +00001641 __entry_ = PP.RawEntry;
1642 __stashed_elem_.__assign_view(*PP);
1643 return *this;
1644}
1645
1646path::iterator& path::iterator::__decrement() {
1647 PathParser PP(__path_ptr_->native(), __entry_, __state_);
1648 --PP;
Eric Fiselier23a120c2018-07-25 03:31:48 +00001649 __state_ = static_cast<_ParserState>(PP.State);
Eric Fiselier91a182b2018-04-02 23:03:41 +00001650 __entry_ = PP.RawEntry;
1651 __stashed_elem_.__assign_view(*PP);
1652 return *this;
1653}
1654
Eric Fiselier70474082018-07-20 01:22:32 +00001655///////////////////////////////////////////////////////////////////////////////
1656// directory entry definitions
1657///////////////////////////////////////////////////////////////////////////////
1658
1659#ifndef _LIBCPP_WIN32API
1660error_code directory_entry::__do_refresh() noexcept {
1661 __data_.__reset();
1662 error_code failure_ec;
1663
Eric Fiselier7eba47e2018-07-25 20:51:49 +00001664 StatT full_st;
Eric Fiselier70474082018-07-20 01:22:32 +00001665 file_status st = detail::posix_lstat(__p_, full_st, &failure_ec);
1666 if (!status_known(st)) {
1667 __data_.__reset();
1668 return failure_ec;
1669 }
1670
1671 if (!_VSTD_FS::exists(st) || !_VSTD_FS::is_symlink(st)) {
1672 __data_.__cache_type_ = directory_entry::_RefreshNonSymlink;
1673 __data_.__type_ = st.type();
1674 __data_.__non_sym_perms_ = st.permissions();
1675 } else { // we have a symlink
1676 __data_.__sym_perms_ = st.permissions();
1677 // Get the information about the linked entity.
1678 // Ignore errors from stat, since we don't want errors regarding symlink
1679 // resolution to be reported to the user.
1680 error_code ignored_ec;
1681 st = detail::posix_stat(__p_, full_st, &ignored_ec);
1682
1683 __data_.__type_ = st.type();
1684 __data_.__non_sym_perms_ = st.permissions();
1685
1686 // If we failed to resolve the link, then only partially populate the
1687 // cache.
1688 if (!status_known(st)) {
1689 __data_.__cache_type_ = directory_entry::_RefreshSymlinkUnresolved;
1690 return error_code{};
1691 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001692 // Otherwise, we resolved the link, potentially as not existing.
Eric Fiseliere39cea92018-07-20 08:36:45 +00001693 // That's OK.
Eric Fiselier70474082018-07-20 01:22:32 +00001694 __data_.__cache_type_ = directory_entry::_RefreshSymlink;
1695 }
1696
1697 if (_VSTD_FS::is_regular_file(st))
1698 __data_.__size_ = static_cast<uintmax_t>(full_st.st_size);
1699
1700 if (_VSTD_FS::exists(st)) {
1701 __data_.__nlink_ = static_cast<uintmax_t>(full_st.st_nlink);
1702
1703 // Attempt to extract the mtime, and fail if it's not representable using
1704 // file_time_type. For now we ignore the error, as we'll report it when
1705 // the value is actually used.
1706 error_code ignored_ec;
1707 __data_.__write_time_ =
1708 __extract_last_write_time(__p_, full_st, &ignored_ec);
1709 }
1710
1711 return failure_ec;
1712}
1713#else
1714error_code directory_entry::__do_refresh() noexcept {
1715 __data_.__reset();
1716 error_code failure_ec;
1717
1718 file_status st = _VSTD_FS::symlink_status(__p_, failure_ec);
1719 if (!status_known(st)) {
1720 __data_.__reset();
1721 return failure_ec;
1722 }
1723
1724 if (!_VSTD_FS::exists(st) || !_VSTD_FS::is_symlink(st)) {
1725 __data_.__cache_type_ = directory_entry::_RefreshNonSymlink;
1726 __data_.__type_ = st.type();
1727 __data_.__non_sym_perms_ = st.permissions();
1728 } else { // we have a symlink
1729 __data_.__sym_perms_ = st.permissions();
1730 // Get the information about the linked entity.
1731 // Ignore errors from stat, since we don't want errors regarding symlink
1732 // resolution to be reported to the user.
1733 error_code ignored_ec;
1734 st = _VSTD_FS::status(__p_, ignored_ec);
1735
1736 __data_.__type_ = st.type();
1737 __data_.__non_sym_perms_ = st.permissions();
1738
1739 // If we failed to resolve the link, then only partially populate the
1740 // cache.
1741 if (!status_known(st)) {
1742 __data_.__cache_type_ = directory_entry::_RefreshSymlinkUnresolved;
1743 return error_code{};
1744 }
Eric Fiselier70474082018-07-20 01:22:32 +00001745 __data_.__cache_type_ = directory_entry::_RefreshSymlink;
1746 }
1747
1748 // FIXME: This is currently broken, and the implementation only a placeholder.
1749 // We need to cache last_write_time, file_size, and hard_link_count here before
1750 // the implementation actually works.
1751
1752 return failure_ec;
1753}
1754#endif
Eric Fiselier91a182b2018-04-02 23:03:41 +00001755
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001756_LIBCPP_END_NAMESPACE_FILESYSTEM