blob: 5ba979ca905ac7cd9a75a0655bf232b813d0c098 [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
Eric Fiselierd8b25e32018-07-23 03:06:57 +000047#if defined(_LIBCPP_COMPILER_GCC)
48#if _GNUC_VER < 500
49#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
50#endif
51#endif
52
Eric Fiselier02cea5e2018-07-27 03:07:09 +000053_LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
Eric Fiselier435db152016-06-17 19:46:40 +000054
Eric Fiselier02cea5e2018-07-27 03:07:09 +000055namespace {
56namespace parser {
Eric Fiselier91a182b2018-04-02 23:03:41 +000057
58using string_view_t = path::__string_view;
59using string_view_pair = pair<string_view_t, string_view_t>;
60using PosPtr = path::value_type const*;
61
62struct PathParser {
63 enum ParserState : unsigned char {
64 // Zero is a special sentinel value used by default constructed iterators.
Eric Fiselier23a120c2018-07-25 03:31:48 +000065 PS_BeforeBegin = path::iterator::_BeforeBegin,
66 PS_InRootName = path::iterator::_InRootName,
67 PS_InRootDir = path::iterator::_InRootDir,
68 PS_InFilenames = path::iterator::_InFilenames,
69 PS_InTrailingSep = path::iterator::_InTrailingSep,
70 PS_AtEnd = path::iterator::_AtEnd
Eric Fiselier91a182b2018-04-02 23:03:41 +000071 };
72
73 const string_view_t Path;
74 string_view_t RawEntry;
75 ParserState State;
76
77private:
Eric Fiselier02cea5e2018-07-27 03:07:09 +000078 PathParser(string_view_t P, ParserState State) noexcept : Path(P),
79 State(State) {}
Eric Fiselier91a182b2018-04-02 23:03:41 +000080
81public:
82 PathParser(string_view_t P, string_view_t E, unsigned char S)
83 : Path(P), RawEntry(E), State(static_cast<ParserState>(S)) {
84 // S cannot be '0' or PS_BeforeBegin.
85 }
86
87 static PathParser CreateBegin(string_view_t P) noexcept {
88 PathParser PP(P, PS_BeforeBegin);
89 PP.increment();
90 return PP;
91 }
92
93 static PathParser CreateEnd(string_view_t P) noexcept {
94 PathParser PP(P, PS_AtEnd);
95 return PP;
96 }
97
98 PosPtr peek() const noexcept {
99 auto TkEnd = getNextTokenStartPos();
100 auto End = getAfterBack();
101 return TkEnd == End ? nullptr : TkEnd;
102 }
103
104 void increment() noexcept {
105 const PosPtr End = getAfterBack();
106 const PosPtr Start = getNextTokenStartPos();
107 if (Start == End)
108 return makeState(PS_AtEnd);
109
110 switch (State) {
111 case PS_BeforeBegin: {
112 PosPtr TkEnd = consumeSeparator(Start, End);
113 if (TkEnd)
114 return makeState(PS_InRootDir, Start, TkEnd);
115 else
116 return makeState(PS_InFilenames, Start, consumeName(Start, End));
117 }
118 case PS_InRootDir:
119 return makeState(PS_InFilenames, Start, consumeName(Start, End));
120
121 case PS_InFilenames: {
122 PosPtr SepEnd = consumeSeparator(Start, End);
123 if (SepEnd != End) {
124 PosPtr TkEnd = consumeName(SepEnd, End);
125 if (TkEnd)
126 return makeState(PS_InFilenames, SepEnd, TkEnd);
127 }
128 return makeState(PS_InTrailingSep, Start, SepEnd);
129 }
130
131 case PS_InTrailingSep:
132 return makeState(PS_AtEnd);
133
134 case PS_InRootName:
135 case PS_AtEnd:
136 _LIBCPP_UNREACHABLE();
137 }
138 }
139
140 void decrement() noexcept {
141 const PosPtr REnd = getBeforeFront();
142 const PosPtr RStart = getCurrentTokenStartPos() - 1;
143 if (RStart == REnd) // we're decrementing the begin
144 return makeState(PS_BeforeBegin);
145
146 switch (State) {
147 case PS_AtEnd: {
148 // Try to consume a trailing separator or root directory first.
149 if (PosPtr SepEnd = consumeSeparator(RStart, REnd)) {
150 if (SepEnd == REnd)
151 return makeState(PS_InRootDir, Path.data(), RStart + 1);
152 return makeState(PS_InTrailingSep, SepEnd + 1, RStart + 1);
153 } else {
154 PosPtr TkStart = consumeName(RStart, REnd);
155 return makeState(PS_InFilenames, TkStart + 1, RStart + 1);
156 }
157 }
158 case PS_InTrailingSep:
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000159 return makeState(PS_InFilenames, consumeName(RStart, REnd) + 1,
160 RStart + 1);
Eric Fiselier91a182b2018-04-02 23:03:41 +0000161 case PS_InFilenames: {
162 PosPtr SepEnd = consumeSeparator(RStart, REnd);
163 if (SepEnd == REnd)
164 return makeState(PS_InRootDir, Path.data(), RStart + 1);
165 PosPtr TkEnd = consumeName(SepEnd, REnd);
166 return makeState(PS_InFilenames, TkEnd + 1, SepEnd + 1);
167 }
168 case PS_InRootDir:
169 // return makeState(PS_InRootName, Path.data(), RStart + 1);
170 case PS_InRootName:
171 case PS_BeforeBegin:
172 _LIBCPP_UNREACHABLE();
173 }
174 }
175
176 /// \brief Return a view with the "preferred representation" of the current
177 /// element. For example trailing separators are represented as a '.'
178 string_view_t operator*() const noexcept {
179 switch (State) {
180 case PS_BeforeBegin:
181 case PS_AtEnd:
182 return "";
183 case PS_InRootDir:
184 return "/";
185 case PS_InTrailingSep:
186 return "";
187 case PS_InRootName:
188 case PS_InFilenames:
189 return RawEntry;
190 }
191 _LIBCPP_UNREACHABLE();
192 }
193
194 explicit operator bool() const noexcept {
195 return State != PS_BeforeBegin && State != PS_AtEnd;
196 }
197
198 PathParser& operator++() noexcept {
199 increment();
200 return *this;
201 }
202
203 PathParser& operator--() noexcept {
204 decrement();
205 return *this;
206 }
207
Eric Fiselierc9a770e2018-12-21 03:16:30 +0000208 bool atEnd() const noexcept {
209 return State == PS_AtEnd;
210 }
211
212 bool inRootDir() const noexcept {
213 return State == PS_InRootDir;
214 }
215
216 bool inRootName() const noexcept {
217 return State == PS_InRootName;
218 }
219
Eric Fiselier91a182b2018-04-02 23:03:41 +0000220 bool inRootPath() const noexcept {
Eric Fiselierc9a770e2018-12-21 03:16:30 +0000221 return inRootName() || inRootDir();
Eric Fiselier91a182b2018-04-02 23:03:41 +0000222 }
223
224private:
225 void makeState(ParserState NewState, PosPtr Start, PosPtr End) noexcept {
226 State = NewState;
227 RawEntry = string_view_t(Start, End - Start);
228 }
229 void makeState(ParserState NewState) noexcept {
230 State = NewState;
231 RawEntry = {};
232 }
233
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000234 PosPtr getAfterBack() const noexcept { return Path.data() + Path.size(); }
Eric Fiselier91a182b2018-04-02 23:03:41 +0000235
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000236 PosPtr getBeforeFront() const noexcept { return Path.data() - 1; }
Eric Fiselier91a182b2018-04-02 23:03:41 +0000237
238 /// \brief Return a pointer to the first character after the currently
239 /// lexed element.
240 PosPtr getNextTokenStartPos() const noexcept {
241 switch (State) {
242 case PS_BeforeBegin:
243 return Path.data();
244 case PS_InRootName:
245 case PS_InRootDir:
246 case PS_InFilenames:
247 return &RawEntry.back() + 1;
248 case PS_InTrailingSep:
249 case PS_AtEnd:
250 return getAfterBack();
251 }
252 _LIBCPP_UNREACHABLE();
253 }
254
255 /// \brief Return a pointer to the first character in the currently lexed
256 /// element.
257 PosPtr getCurrentTokenStartPos() const noexcept {
258 switch (State) {
259 case PS_BeforeBegin:
260 case PS_InRootName:
261 return &Path.front();
262 case PS_InRootDir:
263 case PS_InFilenames:
264 case PS_InTrailingSep:
265 return &RawEntry.front();
266 case PS_AtEnd:
267 return &Path.back() + 1;
268 }
269 _LIBCPP_UNREACHABLE();
270 }
271
272 PosPtr consumeSeparator(PosPtr P, PosPtr End) const noexcept {
273 if (P == End || *P != '/')
274 return nullptr;
275 const int Inc = P < End ? 1 : -1;
276 P += Inc;
277 while (P != End && *P == '/')
278 P += Inc;
279 return P;
280 }
281
282 PosPtr consumeName(PosPtr P, PosPtr End) const noexcept {
283 if (P == End || *P == '/')
284 return nullptr;
285 const int Inc = P < End ? 1 : -1;
286 P += Inc;
287 while (P != End && *P != '/')
288 P += Inc;
289 return P;
290 }
291};
292
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000293string_view_pair separate_filename(string_view_t const& s) {
294 if (s == "." || s == ".." || s.empty())
295 return string_view_pair{s, ""};
296 auto pos = s.find_last_of('.');
297 if (pos == string_view_t::npos || pos == 0)
298 return string_view_pair{s, string_view_t{}};
299 return string_view_pair{s.substr(0, pos), s.substr(pos)};
Eric Fiselier91a182b2018-04-02 23:03:41 +0000300}
301
302string_view_t createView(PosPtr S, PosPtr E) noexcept {
303 return {S, static_cast<size_t>(E - S) + 1};
304}
305
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000306} // namespace parser
307} // namespace
Eric Fiselier91a182b2018-04-02 23:03:41 +0000308
Eric Fiselier435db152016-06-17 19:46:40 +0000309// POSIX HELPERS
310
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000311namespace detail {
312namespace {
Eric Fiselier435db152016-06-17 19:46:40 +0000313
314using value_type = path::value_type;
315using string_type = path::string_type;
316
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000317struct FileDescriptor {
318 const path& name;
319 int fd = -1;
320 StatT m_stat;
321 file_status m_status;
322
323 template <class... Args>
324 static FileDescriptor create(const path* p, error_code& ec, Args... args) {
325 ec.clear();
326 int fd;
327 if ((fd = ::open(p->c_str(), args...)) == -1) {
328 ec = capture_errno();
329 return FileDescriptor{p};
330 }
331 return FileDescriptor(p, fd);
332 }
333
334 template <class... Args>
335 static FileDescriptor create_with_status(const path* p, error_code& ec,
336 Args... args) {
337 FileDescriptor fd = create(p, ec, args...);
338 if (!ec)
339 fd.refresh_status(ec);
340
341 return fd;
342 }
343
344 file_status get_status() const { return m_status; }
345 StatT const& get_stat() const { return m_stat; }
346
347 bool status_known() const { return _VSTD_FS::status_known(m_status); }
348
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000349 file_status refresh_status(error_code& ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000350
351 void close() noexcept {
352 if (fd != -1)
353 ::close(fd);
354 fd = -1;
355 }
356
357 FileDescriptor(FileDescriptor&& other)
358 : name(other.name), fd(other.fd), m_stat(other.m_stat),
359 m_status(other.m_status) {
360 other.fd = -1;
361 other.m_status = file_status{};
362 }
363
364 ~FileDescriptor() { close(); }
365
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000366 FileDescriptor(FileDescriptor const&) = delete;
367 FileDescriptor& operator=(FileDescriptor const&) = delete;
368
369private:
370 explicit FileDescriptor(const path* p, int fd = -1) : name(*p), fd(fd) {}
371};
372
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000373perms posix_get_perms(const StatT& st) noexcept {
Eric Fiselier70474082018-07-20 01:22:32 +0000374 return static_cast<perms>(st.st_mode) & perms::mask;
Eric Fiselier435db152016-06-17 19:46:40 +0000375}
376
377::mode_t posix_convert_perms(perms prms) {
Eric Fiselier70474082018-07-20 01:22:32 +0000378 return static_cast< ::mode_t>(prms & perms::mask);
Eric Fiselier435db152016-06-17 19:46:40 +0000379}
380
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000381file_status create_file_status(error_code& m_ec, path const& p,
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000382 const StatT& path_stat, error_code* ec) {
Eric Fiselier70474082018-07-20 01:22:32 +0000383 if (ec)
384 *ec = m_ec;
Eric Fiselier70474082018-07-20 01:22:32 +0000385 if (m_ec && (m_ec.value() == ENOENT || m_ec.value() == ENOTDIR)) {
386 return file_status(file_type::not_found);
387 } else if (m_ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000388 ErrorHandler<void> err("posix_stat", ec, &p);
389 err.report(m_ec, "failed to determine attributes for the specified path");
Eric Fiselier70474082018-07-20 01:22:32 +0000390 return file_status(file_type::none);
391 }
392 // else
Eric Fiselier435db152016-06-17 19:46:40 +0000393
Eric Fiselier70474082018-07-20 01:22:32 +0000394 file_status fs_tmp;
395 auto const mode = path_stat.st_mode;
396 if (S_ISLNK(mode))
397 fs_tmp.type(file_type::symlink);
398 else if (S_ISREG(mode))
399 fs_tmp.type(file_type::regular);
400 else if (S_ISDIR(mode))
401 fs_tmp.type(file_type::directory);
402 else if (S_ISBLK(mode))
403 fs_tmp.type(file_type::block);
404 else if (S_ISCHR(mode))
405 fs_tmp.type(file_type::character);
406 else if (S_ISFIFO(mode))
407 fs_tmp.type(file_type::fifo);
408 else if (S_ISSOCK(mode))
409 fs_tmp.type(file_type::socket);
410 else
411 fs_tmp.type(file_type::unknown);
Eric Fiselier435db152016-06-17 19:46:40 +0000412
Eric Fiselier70474082018-07-20 01:22:32 +0000413 fs_tmp.permissions(detail::posix_get_perms(path_stat));
414 return fs_tmp;
Eric Fiselier435db152016-06-17 19:46:40 +0000415}
416
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000417file_status posix_stat(path const& p, StatT& path_stat, error_code* ec) {
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000418 error_code m_ec;
Eric Fiselier70474082018-07-20 01:22:32 +0000419 if (::stat(p.c_str(), &path_stat) == -1)
420 m_ec = detail::capture_errno();
421 return create_file_status(m_ec, p, path_stat, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000422}
423
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000424file_status posix_stat(path const& p, error_code* ec) {
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000425 StatT path_stat;
Eric Fiselier70474082018-07-20 01:22:32 +0000426 return posix_stat(p, path_stat, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000427}
428
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000429file_status posix_lstat(path const& p, StatT& path_stat, error_code* ec) {
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000430 error_code m_ec;
Eric Fiselier70474082018-07-20 01:22:32 +0000431 if (::lstat(p.c_str(), &path_stat) == -1)
432 m_ec = detail::capture_errno();
433 return create_file_status(m_ec, p, path_stat, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000434}
435
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000436file_status posix_lstat(path const& p, error_code* ec) {
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000437 StatT path_stat;
Eric Fiselier70474082018-07-20 01:22:32 +0000438 return posix_lstat(p, path_stat, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000439}
440
Dan Albert39b981d2019-01-15 19:16:25 +0000441// http://pubs.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html
442bool posix_ftruncate(const FileDescriptor& fd, off_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);
Eric Fiselierb5215302019-01-17 02:59:28 +0000545#if _POSIX_VERSION >= 200112
546 std::unique_ptr<char, decltype(&::free)>
547 hold(::realpath(p.c_str(), nullptr), &::free);
548 if (hold.get() == nullptr)
549 return err.report(capture_errno());
550 return {hold.get()};
551#else
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000552 char buff[PATH_MAX + 1];
553 char* ret;
554 if ((ret = ::realpath(p.c_str(), buff)) == nullptr)
555 return err.report(capture_errno());
556 return {ret};
Eric Fiselierb5215302019-01-17 02:59:28 +0000557#endif
Eric Fiselier435db152016-06-17 19:46:40 +0000558}
559
560void __copy(const path& from, const path& to, copy_options options,
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000561 error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000562 ErrorHandler<void> err("copy", ec, &from, &to);
Eric Fiselier435db152016-06-17 19:46:40 +0000563
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000564 const bool sym_status = bool(
565 options & (copy_options::create_symlinks | copy_options::skip_symlinks));
Eric Fiselier435db152016-06-17 19:46:40 +0000566
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000567 const bool sym_status2 = bool(options & copy_options::copy_symlinks);
Eric Fiselier435db152016-06-17 19:46:40 +0000568
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000569 error_code m_ec1;
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000570 StatT f_st = {};
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000571 const file_status f = sym_status || sym_status2
572 ? detail::posix_lstat(from, f_st, &m_ec1)
573 : detail::posix_stat(from, f_st, &m_ec1);
574 if (m_ec1)
575 return err.report(m_ec1);
Eric Fiselier435db152016-06-17 19:46:40 +0000576
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000577 StatT t_st = {};
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000578 const file_status t = sym_status ? detail::posix_lstat(to, t_st, &m_ec1)
579 : detail::posix_stat(to, t_st, &m_ec1);
Eric Fiselier435db152016-06-17 19:46:40 +0000580
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000581 if (not status_known(t))
582 return err.report(m_ec1);
583
584 if (!exists(f) || is_other(f) || is_other(t) ||
585 (is_directory(f) && is_regular_file(t)) ||
586 detail::stat_equivalent(f_st, t_st)) {
587 return err.report(errc::function_not_supported);
588 }
Eric Fiselier435db152016-06-17 19:46:40 +0000589
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000590 if (ec)
591 ec->clear();
Eric Fiselier435db152016-06-17 19:46:40 +0000592
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000593 if (is_symlink(f)) {
594 if (bool(copy_options::skip_symlinks & options)) {
595 // do nothing
596 } else if (not exists(t)) {
597 __copy_symlink(from, to, ec);
598 } else {
599 return err.report(errc::file_exists);
Eric Fiselier435db152016-06-17 19:46:40 +0000600 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000601 return;
602 } else if (is_regular_file(f)) {
603 if (bool(copy_options::directories_only & options)) {
604 // do nothing
605 } else if (bool(copy_options::create_symlinks & options)) {
606 __create_symlink(from, to, ec);
607 } else if (bool(copy_options::create_hard_links & options)) {
608 __create_hard_link(from, to, ec);
609 } else if (is_directory(t)) {
610 __copy_file(from, to / from.filename(), options, ec);
611 } else {
612 __copy_file(from, to, options, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000613 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000614 return;
615 } else if (is_directory(f) && bool(copy_options::create_symlinks & options)) {
616 return err.report(errc::is_a_directory);
617 } else if (is_directory(f) && (bool(copy_options::recursive & options) ||
618 copy_options::none == options)) {
Eric Fiselier435db152016-06-17 19:46:40 +0000619
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000620 if (!exists(t)) {
621 // create directory to with attributes from 'from'.
622 __create_directory(to, from, ec);
623 if (ec && *ec) {
624 return;
625 }
Eric Fiselier435db152016-06-17 19:46:40 +0000626 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000627 directory_iterator it =
628 ec ? directory_iterator(from, *ec) : directory_iterator(from);
629 if (ec && *ec) {
630 return;
631 }
632 error_code m_ec2;
633 for (; it != directory_iterator(); it.increment(m_ec2)) {
634 if (m_ec2) {
635 return err.report(m_ec2);
636 }
637 __copy(it->path(), to / it->path().filename(),
638 options | copy_options::__in_recursive_copy, ec);
639 if (ec && *ec) {
640 return;
641 }
642 }
643 }
Eric Fiselier435db152016-06-17 19:46:40 +0000644}
645
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000646namespace detail {
647namespace {
648
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000649#ifdef _LIBCPP_USE_SENDFILE
650bool copy_file_impl_sendfile(FileDescriptor& read_fd, FileDescriptor& write_fd,
651 error_code& ec) {
652
653 size_t count = read_fd.get_stat().st_size;
654 do {
655 ssize_t res;
656 if ((res = ::sendfile(write_fd.fd, read_fd.fd, nullptr, count)) == -1) {
657 ec = capture_errno();
658 return false;
659 }
660 count -= res;
661 } while (count > 0);
662
663 ec.clear();
664
665 return true;
666}
667#elif defined(_LIBCPP_USE_COPYFILE)
668bool copy_file_impl_copyfile(FileDescriptor& read_fd, FileDescriptor& write_fd,
669 error_code& ec) {
670 struct CopyFileState {
671 copyfile_state_t state;
672 CopyFileState() { state = copyfile_state_alloc(); }
673 ~CopyFileState() { copyfile_state_free(state); }
674
675 private:
676 CopyFileState(CopyFileState const&) = delete;
677 CopyFileState& operator=(CopyFileState const&) = delete;
678 };
679
680 CopyFileState cfs;
681 if (fcopyfile(read_fd.fd, write_fd.fd, cfs.state, COPYFILE_DATA) < 0) {
682 ec = capture_errno();
683 return false;
684 }
685
686 ec.clear();
687 return true;
688}
689#endif
690
691// Note: This function isn't guarded by ifdef's even though it may be unused
692// in order to assure it still compiles.
693__attribute__((unused)) bool copy_file_impl_default(FileDescriptor& read_fd,
694 FileDescriptor& write_fd,
695 error_code& ec) {
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000696 ifstream in;
697 in.__open(read_fd.fd, ios::binary);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000698 if (!in.is_open()) {
699 // This assumes that __open didn't reset the error code.
700 ec = capture_errno();
701 return false;
702 }
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000703 ofstream out;
704 out.__open(write_fd.fd, ios::binary);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000705 if (!out.is_open()) {
706 ec = capture_errno();
707 return false;
708 }
709
710 if (in.good() && out.good()) {
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000711 using InIt = istreambuf_iterator<char>;
712 using OutIt = ostreambuf_iterator<char>;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000713 InIt bin(in);
714 InIt ein;
715 OutIt bout(out);
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000716 copy(bin, ein, bout);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000717 }
718 if (out.fail() || in.fail()) {
719 ec = make_error_code(errc::io_error);
720 return false;
721 }
722
723 ec.clear();
724 return true;
725}
726
727bool copy_file_impl(FileDescriptor& from, FileDescriptor& to, error_code& ec) {
728#if defined(_LIBCPP_USE_SENDFILE)
729 return copy_file_impl_sendfile(from, to, ec);
730#elif defined(_LIBCPP_USE_COPYFILE)
731 return copy_file_impl_copyfile(from, to, ec);
732#else
733 return copy_file_impl_default(from, to, ec);
734#endif
735}
736
737} // namespace
738} // namespace detail
Eric Fiselier435db152016-06-17 19:46:40 +0000739
740bool __copy_file(const path& from, const path& to, copy_options options,
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000741 error_code* ec) {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000742 using detail::FileDescriptor;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000743 ErrorHandler<bool> err("copy_file", ec, &to, &from);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000744
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000745 error_code m_ec;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000746 FileDescriptor from_fd =
747 FileDescriptor::create_with_status(&from, m_ec, O_RDONLY | O_NONBLOCK);
748 if (m_ec)
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000749 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000750
751 auto from_st = from_fd.get_status();
752 StatT const& from_stat = from_fd.get_stat();
753 if (!is_regular_file(from_st)) {
754 if (not m_ec)
755 m_ec = make_error_code(errc::not_supported);
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000756 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000757 }
758
759 const bool skip_existing = bool(copy_options::skip_existing & options);
760 const bool update_existing = bool(copy_options::update_existing & options);
761 const bool overwrite_existing =
762 bool(copy_options::overwrite_existing & options);
763
764 StatT to_stat_path;
765 file_status to_st = detail::posix_stat(to, to_stat_path, &m_ec);
766 if (!status_known(to_st))
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000767 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000768
769 const bool to_exists = exists(to_st);
770 if (to_exists && !is_regular_file(to_st))
Eric Fiselier268fa832018-07-23 11:55:13 +0000771 return err.report(errc::not_supported);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000772
773 if (to_exists && detail::stat_equivalent(from_stat, to_stat_path))
Eric Fiselier268fa832018-07-23 11:55:13 +0000774 return err.report(errc::file_exists);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000775
776 if (to_exists && skip_existing)
777 return false;
778
Eric Fiselier455ac4b2018-07-22 21:15:15 +0000779 bool ShouldCopy = [&]() {
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000780 if (to_exists && update_existing) {
781 auto from_time = detail::extract_mtime(from_stat);
782 auto to_time = detail::extract_mtime(to_stat_path);
783 if (from_time.tv_sec < to_time.tv_sec)
Eric Fiselier435db152016-06-17 19:46:40 +0000784 return false;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000785 if (from_time.tv_sec == to_time.tv_sec &&
786 from_time.tv_nsec <= to_time.tv_nsec)
Eric Fiseliere7359252016-10-16 00:47:59 +0000787 return false;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000788 return true;
Eric Fiseliere7359252016-10-16 00:47:59 +0000789 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000790 if (!to_exists || overwrite_existing)
791 return true;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000792 return err.report(errc::file_exists);
Eric Fiselier455ac4b2018-07-22 21:15:15 +0000793 }();
794 if (!ShouldCopy)
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000795 return false;
Eric Fiseliere7359252016-10-16 00:47:59 +0000796
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000797 // Don't truncate right away. We may not be opening the file we originally
798 // looked at; we'll check this later.
Eric Fiselier455ac4b2018-07-22 21:15:15 +0000799 int to_open_flags = O_WRONLY;
800 if (!to_exists)
801 to_open_flags |= O_CREAT;
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000802 FileDescriptor to_fd = FileDescriptor::create_with_status(
803 &to, m_ec, to_open_flags, from_stat.st_mode);
804 if (m_ec)
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000805 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000806
807 if (to_exists) {
808 // Check that the file we initially stat'ed is equivalent to the one
809 // we opened.
Eric Fiselier455ac4b2018-07-22 21:15:15 +0000810 // FIXME: report this better.
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000811 if (!detail::stat_equivalent(to_stat_path, to_fd.get_stat()))
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000812 return err.report(errc::bad_file_descriptor);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000813
814 // Set the permissions and truncate the file we opened.
Eric Fiselierf1aba0d2018-07-26 04:02:06 +0000815 if (detail::posix_fchmod(to_fd, from_stat, m_ec))
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000816 return err.report(m_ec);
Eric Fiselierf1aba0d2018-07-26 04:02:06 +0000817 if (detail::posix_ftruncate(to_fd, 0, m_ec))
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000818 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000819 }
820
821 if (!copy_file_impl(from_fd, to_fd, m_ec)) {
822 // FIXME: Remove the dest file if we failed, and it didn't exist previously.
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000823 return err.report(m_ec);
Eric Fiselierabfdbdf2018-07-22 02:00:53 +0000824 }
825
826 return true;
Eric Fiselier435db152016-06-17 19:46:40 +0000827}
828
829void __copy_symlink(const path& existing_symlink, const path& new_symlink,
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000830 error_code* ec) {
831 const path real_path(__read_symlink(existing_symlink, ec));
832 if (ec && *ec) {
833 return;
834 }
835 // NOTE: proposal says you should detect if you should call
836 // create_symlink or create_directory_symlink. I don't think this
837 // is needed with POSIX
838 __create_symlink(real_path, new_symlink, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000839}
840
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000841bool __create_directories(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000842 ErrorHandler<bool> err("create_directories", ec, &p);
Eric Fiselier435db152016-06-17 19:46:40 +0000843
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000844 error_code m_ec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000845 auto const st = detail::posix_stat(p, &m_ec);
846 if (!status_known(st))
847 return err.report(m_ec);
848 else if (is_directory(st))
849 return false;
850 else if (exists(st))
851 return err.report(errc::file_exists);
852
853 const path parent = p.parent_path();
854 if (!parent.empty()) {
855 const file_status parent_st = status(parent, m_ec);
856 if (not status_known(parent_st))
857 return err.report(m_ec);
858 if (not exists(parent_st)) {
859 __create_directories(parent, ec);
860 if (ec && *ec) {
861 return false;
862 }
Eric Fiselier435db152016-06-17 19:46:40 +0000863 }
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000864 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000865 return __create_directory(p, ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000866}
867
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000868bool __create_directory(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000869 ErrorHandler<bool> err("create_directory", ec, &p);
870
871 if (::mkdir(p.c_str(), static_cast<int>(perms::all)) == 0)
872 return true;
Eric Fiselier7ca3db82018-07-25 04:46:32 +0000873 if (errno != EEXIST)
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000874 err.report(capture_errno());
875 return false;
Eric Fiselier435db152016-06-17 19:46:40 +0000876}
877
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000878bool __create_directory(path const& p, path const& attributes, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000879 ErrorHandler<bool> err("create_directory", ec, &p, &attributes);
880
881 StatT attr_stat;
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000882 error_code mec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000883 auto st = detail::posix_stat(attributes, attr_stat, &mec);
884 if (!status_known(st))
885 return err.report(mec);
Eric Fiselier7ca3db82018-07-25 04:46:32 +0000886 if (!is_directory(st))
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000887 return err.report(errc::not_a_directory,
888 "the specified attribute path is invalid");
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000889
890 if (::mkdir(p.c_str(), attr_stat.st_mode) == 0)
891 return true;
Eric Fiselier7ca3db82018-07-25 04:46:32 +0000892 if (errno != EEXIST)
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000893 err.report(capture_errno());
894 return false;
Eric Fiselier435db152016-06-17 19:46:40 +0000895}
896
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000897void __create_directory_symlink(path const& from, path const& to,
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000898 error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000899 ErrorHandler<void> err("create_directory_symlink", ec, &from, &to);
900 if (::symlink(from.c_str(), to.c_str()) != 0)
901 return err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +0000902}
903
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000904void __create_hard_link(const path& from, const path& to, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000905 ErrorHandler<void> err("create_hard_link", ec, &from, &to);
906 if (::link(from.c_str(), to.c_str()) == -1)
907 return err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +0000908}
909
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000910void __create_symlink(path const& from, path const& to, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000911 ErrorHandler<void> err("create_symlink", ec, &from, &to);
912 if (::symlink(from.c_str(), to.c_str()) == -1)
913 return err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +0000914}
915
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000916path __current_path(error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000917 ErrorHandler<path> err("current_path", ec);
Eric Fiselier435db152016-06-17 19:46:40 +0000918
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000919 auto size = ::pathconf(".", _PC_PATH_MAX);
920 _LIBCPP_ASSERT(size >= 0, "pathconf returned a 0 as max size");
921
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000922 auto buff = unique_ptr<char[]>(new char[size + 1]);
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000923 char* ret;
924 if ((ret = ::getcwd(buff.get(), static_cast<size_t>(size))) == nullptr)
925 return err.report(capture_errno(), "call to getcwd failed");
926
927 return {buff.get()};
Eric Fiselier435db152016-06-17 19:46:40 +0000928}
929
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000930void __current_path(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000931 ErrorHandler<void> err("current_path", ec, &p);
932 if (::chdir(p.c_str()) == -1)
933 err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +0000934}
935
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000936bool __equivalent(const path& p1, const path& p2, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000937 ErrorHandler<bool> err("equivalent", ec, &p1, &p2);
938
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000939 error_code ec1, ec2;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000940 StatT st1 = {}, st2 = {};
941 auto s1 = detail::posix_stat(p1.native(), st1, &ec1);
942 if (!exists(s1))
943 return err.report(errc::not_supported);
944 auto s2 = detail::posix_stat(p2.native(), st2, &ec2);
945 if (!exists(s2))
946 return err.report(errc::not_supported);
947
948 return detail::stat_equivalent(st1, st2);
Eric Fiselier435db152016-06-17 19:46:40 +0000949}
950
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000951uintmax_t __file_size(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000952 ErrorHandler<uintmax_t> err("file_size", ec, &p);
953
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000954 error_code m_ec;
Eric Fiselier7eba47e2018-07-25 20:51:49 +0000955 StatT st;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000956 file_status fst = detail::posix_stat(p, st, &m_ec);
957 if (!exists(fst) || !is_regular_file(fst)) {
958 errc error_kind =
959 is_directory(fst) ? errc::is_a_directory : errc::not_supported;
960 if (!m_ec)
961 m_ec = make_error_code(error_kind);
962 return err.report(m_ec);
963 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000964 // is_regular_file(p) == true
965 return static_cast<uintmax_t>(st.st_size);
Eric Fiselier435db152016-06-17 19:46:40 +0000966}
967
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000968uintmax_t __hard_link_count(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000969 ErrorHandler<uintmax_t> err("hard_link_count", ec, &p);
970
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000971 error_code m_ec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000972 StatT st;
973 detail::posix_stat(p, st, &m_ec);
974 if (m_ec)
975 return err.report(m_ec);
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000976 return static_cast<uintmax_t>(st.st_nlink);
Eric Fiselier435db152016-06-17 19:46:40 +0000977}
978
Eric Fiselier02cea5e2018-07-27 03:07:09 +0000979bool __fs_is_empty(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000980 ErrorHandler<bool> err("is_empty", ec, &p);
Eric Fiselieraa8c61f2016-10-15 23:05:04 +0000981
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000982 error_code m_ec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000983 StatT pst;
984 auto st = detail::posix_stat(p, pst, &m_ec);
985 if (m_ec)
986 return err.report(m_ec);
987 else if (!is_directory(st) && !is_regular_file(st))
988 return err.report(errc::not_supported);
989 else if (is_directory(st)) {
990 auto it = ec ? directory_iterator(p, *ec) : directory_iterator(p);
991 if (ec && *ec)
992 return false;
993 return it == directory_iterator{};
994 } else if (is_regular_file(st))
Eric Fiselierd6c49a32018-07-23 11:46:47 +0000995 return static_cast<uintmax_t>(pst.st_size) == 0;
Eric Fiseliera75bbde2018-07-23 02:00:52 +0000996
997 _LIBCPP_UNREACHABLE();
Eric Fiselier435db152016-06-17 19:46:40 +0000998}
999
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001000static file_time_type __extract_last_write_time(const path& p, const StatT& st,
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001001 error_code* ec) {
Eric Fiselier7eba47e2018-07-25 20:51:49 +00001002 using detail::fs_time;
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001003 ErrorHandler<file_time_type> err("last_write_time", ec, &p);
1004
Eric Fiselier70474082018-07-20 01:22:32 +00001005 auto ts = detail::extract_mtime(st);
Eric Fiselier7eba47e2018-07-25 20:51:49 +00001006 if (!fs_time::is_representable(ts))
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001007 return err.report(errc::value_too_large);
1008
Eric Fiselier7eba47e2018-07-25 20:51:49 +00001009 return fs_time::convert_from_timespec(ts);
Eric Fiselier70474082018-07-20 01:22:32 +00001010}
Eric Fiselier42d6d2c2017-07-08 04:18:41 +00001011
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001012file_time_type __last_write_time(const path& p, error_code* ec) {
1013 using namespace chrono;
1014 ErrorHandler<file_time_type> err("last_write_time", ec, &p);
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001015
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001016 error_code m_ec;
1017 StatT st;
1018 detail::posix_stat(p, st, &m_ec);
1019 if (m_ec)
1020 return err.report(m_ec);
1021 return __extract_last_write_time(p, st, ec);
Eric Fiselier435db152016-06-17 19:46:40 +00001022}
1023
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001024void __last_write_time(const path& p, file_time_type new_time, error_code* ec) {
1025 using detail::fs_time;
1026 ErrorHandler<void> err("last_write_time", ec, &p);
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001027
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001028 error_code m_ec;
1029 array<TimeSpec, 2> tbuf;
Eric Fiselier7eba47e2018-07-25 20:51:49 +00001030#if !defined(_LIBCPP_USE_UTIMENSAT)
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001031 // This implementation has a race condition between determining the
1032 // last access time and attempting to set it to the same value using
1033 // ::utimes
1034 StatT st;
1035 file_status fst = detail::posix_stat(p, st, &m_ec);
1036 if (m_ec)
1037 return err.report(m_ec);
1038 tbuf[0] = detail::extract_atime(st);
Eric Fiselier435db152016-06-17 19:46:40 +00001039#else
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001040 tbuf[0].tv_sec = 0;
1041 tbuf[0].tv_nsec = UTIME_OMIT;
Eric Fiselier435db152016-06-17 19:46:40 +00001042#endif
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001043 if (!fs_time::convert_to_timespec(tbuf[1], new_time))
1044 return err.report(errc::value_too_large);
Eric Fiselier70474082018-07-20 01:22:32 +00001045
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001046 detail::set_file_times(p, tbuf, m_ec);
1047 if (m_ec)
1048 return err.report(m_ec);
Eric Fiselier435db152016-06-17 19:46:40 +00001049}
1050
Eric Fiselier4f3dc0e2018-03-26 06:23:55 +00001051void __permissions(const path& p, perms prms, perm_options opts,
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001052 error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001053 ErrorHandler<void> err("permissions", ec, &p);
Eric Fiselier435db152016-06-17 19:46:40 +00001054
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001055 auto has_opt = [&](perm_options o) { return bool(o & opts); };
1056 const bool resolve_symlinks = !has_opt(perm_options::nofollow);
1057 const bool add_perms = has_opt(perm_options::add);
1058 const bool remove_perms = has_opt(perm_options::remove);
1059 _LIBCPP_ASSERT(
1060 (add_perms + remove_perms + has_opt(perm_options::replace)) == 1,
1061 "One and only one of the perm_options constants replace, add, or remove "
1062 "is present in opts");
1063
1064 bool set_sym_perms = false;
1065 prms &= perms::mask;
1066 if (!resolve_symlinks || (add_perms || remove_perms)) {
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001067 error_code m_ec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001068 file_status st = resolve_symlinks ? detail::posix_stat(p, &m_ec)
1069 : detail::posix_lstat(p, &m_ec);
1070 set_sym_perms = is_symlink(st);
1071 if (m_ec)
1072 return err.report(m_ec);
1073 _LIBCPP_ASSERT(st.permissions() != perms::unknown,
1074 "Permissions unexpectedly unknown");
1075 if (add_perms)
1076 prms |= st.permissions();
1077 else if (remove_perms)
1078 prms = st.permissions() & ~prms;
1079 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001080 const auto real_perms = detail::posix_convert_perms(prms);
Eric Fiselier435db152016-06-17 19:46:40 +00001081
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001082#if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_FDCWD)
1083 const int flags = set_sym_perms ? AT_SYMLINK_NOFOLLOW : 0;
1084 if (::fchmodat(AT_FDCWD, p.c_str(), real_perms, flags) == -1) {
1085 return err.report(capture_errno());
1086 }
1087#else
1088 if (set_sym_perms)
1089 return err.report(errc::operation_not_supported);
1090 if (::chmod(p.c_str(), real_perms) == -1) {
1091 return err.report(capture_errno());
1092 }
1093#endif
Eric Fiselier435db152016-06-17 19:46:40 +00001094}
1095
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001096path __read_symlink(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001097 ErrorHandler<path> err("read_symlink", ec, &p);
1098
Eric Fiselierb5215302019-01-17 02:59:28 +00001099#ifdef PATH_MAX
1100 struct NullDeleter { void operator()(void*) const {} };
1101 const size_t size = PATH_MAX + 1;
1102 char stack_buff[size];
1103 auto buff = std::unique_ptr<char[], NullDeleter>(stack_buff);
1104#else
1105 StatT sb;
1106 if (::lstat(p.c_str(), &sb) == -1) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001107 return err.report(capture_errno());
1108 }
Eric Fiselierb5215302019-01-17 02:59:28 +00001109 const size_t size = sb.st_size + 1;
1110 auto buff = unique_ptr<char[]>(new char[size]);
1111#endif
1112 ::ssize_t ret;
1113 if ((ret = ::readlink(p.c_str(), buff.get(), size)) == -1)
1114 return err.report(capture_errno());
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001115 _LIBCPP_ASSERT(ret > 0, "TODO");
Eric Fiselierb5215302019-01-17 02:59:28 +00001116 if (static_cast<size_t>(ret) >= size)
1117 return err.report(errc::value_too_large);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001118 buff[ret] = 0;
Eric Fiselierb5215302019-01-17 02:59:28 +00001119 return {buff.get()};
Eric Fiselier435db152016-06-17 19:46:40 +00001120}
1121
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001122bool __remove(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001123 ErrorHandler<bool> err("remove", ec, &p);
1124 if (::remove(p.c_str()) == -1) {
1125 if (errno != ENOENT)
1126 err.report(capture_errno());
1127 return false;
1128 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001129 return true;
Eric Fiselier435db152016-06-17 19:46:40 +00001130}
1131
1132namespace {
1133
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001134uintmax_t remove_all_impl(path const& p, error_code& ec) {
1135 const auto npos = static_cast<uintmax_t>(-1);
1136 const file_status st = __symlink_status(p, &ec);
1137 if (ec)
1138 return npos;
1139 uintmax_t count = 1;
1140 if (is_directory(st)) {
1141 for (directory_iterator it(p, ec); !ec && it != directory_iterator();
1142 it.increment(ec)) {
1143 auto other_count = remove_all_impl(it->path(), ec);
1144 if (ec)
1145 return npos;
1146 count += other_count;
Eric Fiselier435db152016-06-17 19:46:40 +00001147 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001148 if (ec)
1149 return npos;
1150 }
1151 if (!__remove(p, &ec))
1152 return npos;
1153 return count;
Eric Fiselier435db152016-06-17 19:46:40 +00001154}
1155
1156} // end namespace
1157
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001158uintmax_t __remove_all(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001159 ErrorHandler<uintmax_t> err("remove_all", ec, &p);
Ekaterina Vaartis52668f72018-01-11 17:04:29 +00001160
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001161 error_code mec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001162 auto count = remove_all_impl(p, mec);
1163 if (mec) {
1164 if (mec == errc::no_such_file_or_directory)
1165 return 0;
1166 return err.report(mec);
1167 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001168 return count;
Eric Fiselier435db152016-06-17 19:46:40 +00001169}
1170
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001171void __rename(const path& from, const path& to, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001172 ErrorHandler<void> err("rename", ec, &from, &to);
1173 if (::rename(from.c_str(), to.c_str()) == -1)
1174 err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +00001175}
1176
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001177void __resize_file(const path& p, uintmax_t size, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001178 ErrorHandler<void> err("resize_file", ec, &p);
1179 if (::truncate(p.c_str(), static_cast< ::off_t>(size)) == -1)
1180 return err.report(capture_errno());
Eric Fiselier435db152016-06-17 19:46:40 +00001181}
1182
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001183space_info __space(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001184 ErrorHandler<void> err("space", ec, &p);
1185 space_info si;
1186 struct statvfs m_svfs = {};
1187 if (::statvfs(p.c_str(), &m_svfs) == -1) {
1188 err.report(capture_errno());
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001189 si.capacity = si.free = si.available = static_cast<uintmax_t>(-1);
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001190 return si;
1191 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001192 // Multiply with overflow checking.
1193 auto do_mult = [&](uintmax_t& out, uintmax_t other) {
1194 out = other * m_svfs.f_frsize;
1195 if (other == 0 || out / other != m_svfs.f_frsize)
1196 out = static_cast<uintmax_t>(-1);
1197 };
1198 do_mult(si.capacity, m_svfs.f_blocks);
1199 do_mult(si.free, m_svfs.f_bfree);
1200 do_mult(si.available, m_svfs.f_bavail);
1201 return si;
Eric Fiselier435db152016-06-17 19:46:40 +00001202}
1203
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001204file_status __status(const path& p, error_code* ec) {
1205 return detail::posix_stat(p, ec);
Eric Fiselier435db152016-06-17 19:46:40 +00001206}
1207
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001208file_status __symlink_status(const path& p, error_code* ec) {
1209 return detail::posix_lstat(p, ec);
Eric Fiselier435db152016-06-17 19:46:40 +00001210}
1211
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001212path __temp_directory_path(error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001213 ErrorHandler<path> err("temp_directory_path", ec);
1214
Saleem Abdulrasoolcf279a52017-02-05 17:21:52 +00001215 const char* env_paths[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"};
1216 const char* ret = nullptr;
1217
1218 for (auto& ep : env_paths)
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001219 if ((ret = getenv(ep)))
Saleem Abdulrasoolcf279a52017-02-05 17:21:52 +00001220 break;
1221 if (ret == nullptr)
1222 ret = "/tmp";
1223
1224 path p(ret);
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001225 error_code m_ec;
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001226 file_status st = detail::posix_stat(p, &m_ec);
1227 if (!status_known(st))
1228 return err.report(m_ec, "cannot access path \"%s\"", p);
Saleem Abdulrasoolcf279a52017-02-05 17:21:52 +00001229
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001230 if (!exists(st) || !is_directory(st))
1231 return err.report(errc::not_a_directory, "path \"%s\" is not a directory",
1232 p);
1233
Saleem Abdulrasoolcf279a52017-02-05 17:21:52 +00001234 return p;
Eric Fiselier435db152016-06-17 19:46:40 +00001235}
1236
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001237path __weakly_canonical(const path& p, error_code* ec) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001238 ErrorHandler<path> err("weakly_canonical", ec, &p);
1239
Eric Fiselier91a182b2018-04-02 23:03:41 +00001240 if (p.empty())
1241 return __canonical("", ec);
Eric Fiselier435db152016-06-17 19:46:40 +00001242
Eric Fiselier91a182b2018-04-02 23:03:41 +00001243 path result;
1244 path tmp;
1245 tmp.__reserve(p.native().size());
1246 auto PP = PathParser::CreateEnd(p.native());
1247 --PP;
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001248 vector<string_view_t> DNEParts;
Eric Fiselier435db152016-06-17 19:46:40 +00001249
Eric Fiselier91a182b2018-04-02 23:03:41 +00001250 while (PP.State != PathParser::PS_BeforeBegin) {
1251 tmp.assign(createView(p.native().data(), &PP.RawEntry.back()));
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001252 error_code m_ec;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001253 file_status st = __status(tmp, &m_ec);
1254 if (!status_known(st)) {
Eric Fiseliera75bbde2018-07-23 02:00:52 +00001255 return err.report(m_ec);
Eric Fiselier91a182b2018-04-02 23:03:41 +00001256 } else if (exists(st)) {
1257 result = __canonical(tmp, ec);
1258 break;
Eric Fiselier435db152016-06-17 19:46:40 +00001259 }
Eric Fiselier91a182b2018-04-02 23:03:41 +00001260 DNEParts.push_back(*PP);
1261 --PP;
1262 }
1263 if (PP.State == PathParser::PS_BeforeBegin)
1264 result = __canonical("", ec);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001265 if (ec)
1266 ec->clear();
Eric Fiselier91a182b2018-04-02 23:03:41 +00001267 if (DNEParts.empty())
1268 return result;
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001269 for (auto It = DNEParts.rbegin(); It != DNEParts.rend(); ++It)
Eric Fiselier91a182b2018-04-02 23:03:41 +00001270 result /= *It;
1271 return result.lexically_normal();
Eric Fiselier435db152016-06-17 19:46:40 +00001272}
1273
Eric Fiselier91a182b2018-04-02 23:03:41 +00001274///////////////////////////////////////////////////////////////////////////////
1275// path definitions
1276///////////////////////////////////////////////////////////////////////////////
1277
1278constexpr path::value_type path::preferred_separator;
1279
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001280path& path::replace_extension(path const& replacement) {
1281 path p = extension();
1282 if (not p.empty()) {
1283 __pn_.erase(__pn_.size() - p.native().size());
1284 }
1285 if (!replacement.empty()) {
1286 if (replacement.native()[0] != '.') {
1287 __pn_ += ".";
Eric Fiselier91a182b2018-04-02 23:03:41 +00001288 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001289 __pn_.append(replacement.__pn_);
1290 }
1291 return *this;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001292}
1293
1294///////////////////////////////////////////////////////////////////////////////
1295// path.decompose
1296
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001297string_view_t path::__root_name() const {
1298 auto PP = PathParser::CreateBegin(__pn_);
1299 if (PP.State == PathParser::PS_InRootName)
1300 return *PP;
1301 return {};
Eric Fiselier91a182b2018-04-02 23:03:41 +00001302}
1303
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001304string_view_t path::__root_directory() const {
1305 auto PP = PathParser::CreateBegin(__pn_);
1306 if (PP.State == PathParser::PS_InRootName)
1307 ++PP;
1308 if (PP.State == PathParser::PS_InRootDir)
1309 return *PP;
1310 return {};
1311}
1312
1313string_view_t path::__root_path_raw() const {
1314 auto PP = PathParser::CreateBegin(__pn_);
1315 if (PP.State == PathParser::PS_InRootName) {
1316 auto NextCh = PP.peek();
1317 if (NextCh && *NextCh == '/') {
Eric Fiselier91a182b2018-04-02 23:03:41 +00001318 ++PP;
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001319 return createView(__pn_.data(), &PP.RawEntry.back());
Eric Fiselier91a182b2018-04-02 23:03:41 +00001320 }
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001321 return PP.RawEntry;
1322 }
1323 if (PP.State == PathParser::PS_InRootDir)
1324 return *PP;
1325 return {};
Eric Fiselier91a182b2018-04-02 23:03:41 +00001326}
1327
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001328static bool ConsumeRootName(PathParser *PP) {
1329 static_assert(PathParser::PS_BeforeBegin == 1 &&
1330 PathParser::PS_InRootName == 2,
1331 "Values for enums are incorrect");
1332 while (PP->State <= PathParser::PS_InRootName)
1333 ++(*PP);
1334 return PP->State == PathParser::PS_AtEnd;
1335}
1336
Eric Fiselier91a182b2018-04-02 23:03:41 +00001337static bool ConsumeRootDir(PathParser* PP) {
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001338 static_assert(PathParser::PS_BeforeBegin == 1 &&
1339 PathParser::PS_InRootName == 2 &&
1340 PathParser::PS_InRootDir == 3, "Values for enums are incorrect");
Eric Fiselier91a182b2018-04-02 23:03:41 +00001341 while (PP->State <= PathParser::PS_InRootDir)
1342 ++(*PP);
1343 return PP->State == PathParser::PS_AtEnd;
1344}
1345
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001346string_view_t path::__relative_path() const {
1347 auto PP = PathParser::CreateBegin(__pn_);
1348 if (ConsumeRootDir(&PP))
1349 return {};
1350 return createView(PP.RawEntry.data(), &__pn_.back());
1351}
1352
1353string_view_t path::__parent_path() const {
1354 if (empty())
1355 return {};
1356 // Determine if we have a root path but not a relative path. In that case
1357 // return *this.
1358 {
Eric Fiselier91a182b2018-04-02 23:03:41 +00001359 auto PP = PathParser::CreateBegin(__pn_);
1360 if (ConsumeRootDir(&PP))
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001361 return __pn_;
1362 }
1363 // Otherwise remove a single element from the end of the path, and return
1364 // a string representing that path
1365 {
1366 auto PP = PathParser::CreateEnd(__pn_);
1367 --PP;
1368 if (PP.RawEntry.data() == __pn_.data())
Eric Fiselier91a182b2018-04-02 23:03:41 +00001369 return {};
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001370 --PP;
1371 return createView(__pn_.data(), &PP.RawEntry.back());
1372 }
Eric Fiselier91a182b2018-04-02 23:03:41 +00001373}
1374
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001375string_view_t path::__filename() const {
1376 if (empty())
1377 return {};
1378 {
1379 PathParser PP = PathParser::CreateBegin(__pn_);
1380 if (ConsumeRootDir(&PP))
Eric Fiselier91a182b2018-04-02 23:03:41 +00001381 return {};
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001382 }
1383 return *(--PathParser::CreateEnd(__pn_));
Eric Fiselier91a182b2018-04-02 23:03:41 +00001384}
1385
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001386string_view_t path::__stem() const {
1387 return parser::separate_filename(__filename()).first;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001388}
1389
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001390string_view_t path::__extension() const {
1391 return parser::separate_filename(__filename()).second;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001392}
1393
1394////////////////////////////////////////////////////////////////////////////
1395// path.gen
1396
Eric Fiselier91a182b2018-04-02 23:03:41 +00001397enum PathPartKind : unsigned char {
1398 PK_None,
1399 PK_RootSep,
1400 PK_Filename,
1401 PK_Dot,
1402 PK_DotDot,
1403 PK_TrailingSep
1404};
1405
1406static PathPartKind ClassifyPathPart(string_view_t Part) {
1407 if (Part.empty())
1408 return PK_TrailingSep;
1409 if (Part == ".")
1410 return PK_Dot;
1411 if (Part == "..")
1412 return PK_DotDot;
1413 if (Part == "/")
1414 return PK_RootSep;
1415 return PK_Filename;
1416}
1417
1418path path::lexically_normal() const {
1419 if (__pn_.empty())
1420 return *this;
1421
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001422 using PartKindPair = pair<string_view_t, PathPartKind>;
1423 vector<PartKindPair> Parts;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001424 // Guess as to how many elements the path has to avoid reallocating.
1425 Parts.reserve(32);
1426
1427 // Track the total size of the parts as we collect them. This allows the
1428 // resulting path to reserve the correct amount of memory.
1429 size_t NewPathSize = 0;
1430 auto AddPart = [&](PathPartKind K, string_view_t P) {
1431 NewPathSize += P.size();
1432 Parts.emplace_back(P, K);
1433 };
1434 auto LastPartKind = [&]() {
1435 if (Parts.empty())
1436 return PK_None;
1437 return Parts.back().second;
1438 };
1439
1440 bool MaybeNeedTrailingSep = false;
1441 // Build a stack containing the remaining elements of the path, popping off
1442 // elements which occur before a '..' entry.
1443 for (auto PP = PathParser::CreateBegin(__pn_); PP; ++PP) {
1444 auto Part = *PP;
1445 PathPartKind Kind = ClassifyPathPart(Part);
1446 switch (Kind) {
1447 case PK_Filename:
1448 case PK_RootSep: {
1449 // Add all non-dot and non-dot-dot elements to the stack of elements.
1450 AddPart(Kind, Part);
1451 MaybeNeedTrailingSep = false;
1452 break;
1453 }
1454 case PK_DotDot: {
1455 // Only push a ".." element if there are no elements preceding the "..",
1456 // or if the preceding element is itself "..".
1457 auto LastKind = LastPartKind();
1458 if (LastKind == PK_Filename) {
1459 NewPathSize -= Parts.back().first.size();
1460 Parts.pop_back();
1461 } else if (LastKind != PK_RootSep)
1462 AddPart(PK_DotDot, "..");
1463 MaybeNeedTrailingSep = LastKind == PK_Filename;
1464 break;
1465 }
1466 case PK_Dot:
1467 case PK_TrailingSep: {
1468 MaybeNeedTrailingSep = true;
1469 break;
1470 }
1471 case PK_None:
1472 _LIBCPP_UNREACHABLE();
1473 }
1474 }
1475 // [fs.path.generic]p6.8: If the path is empty, add a dot.
1476 if (Parts.empty())
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001477 return ".";
Eric Fiselier91a182b2018-04-02 23:03:41 +00001478
1479 // [fs.path.generic]p6.7: If the last filename is dot-dot, remove any
1480 // trailing directory-separator.
1481 bool NeedTrailingSep = MaybeNeedTrailingSep && LastPartKind() == PK_Filename;
1482
1483 path Result;
1484 Result.__pn_.reserve(Parts.size() + NewPathSize + NeedTrailingSep);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001485 for (auto& PK : Parts)
Eric Fiselier91a182b2018-04-02 23:03:41 +00001486 Result /= PK.first;
1487
1488 if (NeedTrailingSep)
1489 Result /= "";
1490
1491 return Result;
1492}
1493
1494static int DetermineLexicalElementCount(PathParser PP) {
1495 int Count = 0;
1496 for (; PP; ++PP) {
1497 auto Elem = *PP;
1498 if (Elem == "..")
1499 --Count;
Eric Fiselier9c4949a2018-12-21 04:25:40 +00001500 else if (Elem != "." && Elem != "")
Eric Fiselier91a182b2018-04-02 23:03:41 +00001501 ++Count;
1502 }
1503 return Count;
1504}
1505
1506path path::lexically_relative(const path& base) const {
1507 { // perform root-name/root-directory mismatch checks
1508 auto PP = PathParser::CreateBegin(__pn_);
1509 auto PPBase = PathParser::CreateBegin(base.__pn_);
1510 auto CheckIterMismatchAtBase = [&]() {
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001511 return PP.State != PPBase.State &&
1512 (PP.inRootPath() || PPBase.inRootPath());
Eric Fiselier91a182b2018-04-02 23:03:41 +00001513 };
Eric Fiselier9c4949a2018-12-21 04:25:40 +00001514 if (PP.inRootName() && PPBase.inRootName()) {
Eric Fiselier91a182b2018-04-02 23:03:41 +00001515 if (*PP != *PPBase)
1516 return {};
1517 } else if (CheckIterMismatchAtBase())
1518 return {};
1519
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001520 if (PP.inRootPath())
1521 ++PP;
1522 if (PPBase.inRootPath())
1523 ++PPBase;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001524 if (CheckIterMismatchAtBase())
1525 return {};
1526 }
1527
1528 // Find the first mismatching element
1529 auto PP = PathParser::CreateBegin(__pn_);
1530 auto PPBase = PathParser::CreateBegin(base.__pn_);
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001531 while (PP && PPBase && PP.State == PPBase.State && *PP == *PPBase) {
Eric Fiselier91a182b2018-04-02 23:03:41 +00001532 ++PP;
1533 ++PPBase;
1534 }
1535
1536 // If there is no mismatch, return ".".
1537 if (!PP && !PPBase)
1538 return ".";
1539
1540 // Otherwise, determine the number of elements, 'n', which are not dot or
1541 // dot-dot minus the number of dot-dot elements.
1542 int ElemCount = DetermineLexicalElementCount(PPBase);
1543 if (ElemCount < 0)
1544 return {};
1545
Eric Fiselier9c4949a2018-12-21 04:25:40 +00001546 // if n == 0 and (a == end() || a->empty()), returns path("."); otherwise
1547 if (ElemCount == 0 && (PP.atEnd() || *PP == ""))
1548 return ".";
1549
Eric Fiselier91a182b2018-04-02 23:03:41 +00001550 // return a path constructed with 'n' dot-dot elements, followed by the the
1551 // elements of '*this' after the mismatch.
1552 path Result;
1553 // FIXME: Reserve enough room in Result that it won't have to re-allocate.
1554 while (ElemCount--)
1555 Result /= "..";
1556 for (; PP; ++PP)
1557 Result /= *PP;
1558 return Result;
1559}
1560
1561////////////////////////////////////////////////////////////////////////////
1562// path.comparisons
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001563static int CompareRootName(PathParser *LHS, PathParser *RHS) {
1564 if (!LHS->inRootName() && !RHS->inRootName())
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001565 return 0;
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001566
1567 auto GetRootName = [](PathParser *Parser) -> string_view_t {
1568 return Parser->inRootName() ? **Parser : "";
1569 };
1570 int res = GetRootName(LHS).compare(GetRootName(RHS));
1571 ConsumeRootName(LHS);
1572 ConsumeRootName(RHS);
1573 return res;
1574}
1575
1576static int CompareRootDir(PathParser *LHS, PathParser *RHS) {
1577 if (!LHS->inRootDir() && RHS->inRootDir())
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001578 return -1;
Eric Fiselierc9a770e2018-12-21 03:16:30 +00001579 else if (LHS->inRootDir() && !RHS->inRootDir())
1580 return 1;
1581 else {
1582 ConsumeRootDir(LHS);
1583 ConsumeRootDir(RHS);
1584 return 0;
1585 }
1586}
1587
1588static int CompareRelative(PathParser *LHSPtr, PathParser *RHSPtr) {
1589 auto &LHS = *LHSPtr;
1590 auto &RHS = *RHSPtr;
1591
1592 int res;
1593 while (LHS && RHS) {
1594 if ((res = (*LHS).compare(*RHS)) != 0)
1595 return res;
1596 ++LHS;
1597 ++RHS;
1598 }
1599 return 0;
1600}
1601
1602static int CompareEndState(PathParser *LHS, PathParser *RHS) {
1603 if (LHS->atEnd() && !RHS->atEnd())
1604 return -1;
1605 else if (!LHS->atEnd() && RHS->atEnd())
1606 return 1;
1607 return 0;
1608}
1609
1610int path::__compare(string_view_t __s) const {
1611 auto LHS = PathParser::CreateBegin(__pn_);
1612 auto RHS = PathParser::CreateBegin(__s);
1613 int res;
1614
1615 if ((res = CompareRootName(&LHS, &RHS)) != 0)
1616 return res;
1617
1618 if ((res = CompareRootDir(&LHS, &RHS)) != 0)
1619 return res;
1620
1621 if ((res = CompareRelative(&LHS, &RHS)) != 0)
1622 return res;
1623
1624 return CompareEndState(&LHS, &RHS);
Eric Fiselier91a182b2018-04-02 23:03:41 +00001625}
1626
1627////////////////////////////////////////////////////////////////////////////
1628// path.nonmembers
1629size_t hash_value(const path& __p) noexcept {
1630 auto PP = PathParser::CreateBegin(__p.native());
1631 size_t hash_value = 0;
Eric Fiselierd6c49a32018-07-23 11:46:47 +00001632 hash<string_view_t> hasher;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001633 while (PP) {
1634 hash_value = __hash_combine(hash_value, hasher(*PP));
1635 ++PP;
1636 }
1637 return hash_value;
1638}
1639
1640////////////////////////////////////////////////////////////////////////////
1641// path.itr
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001642path::iterator path::begin() const {
1643 auto PP = PathParser::CreateBegin(__pn_);
1644 iterator it;
1645 it.__path_ptr_ = this;
1646 it.__state_ = static_cast<path::iterator::_ParserState>(PP.State);
1647 it.__entry_ = PP.RawEntry;
1648 it.__stashed_elem_.__assign_view(*PP);
1649 return it;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001650}
1651
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001652path::iterator path::end() const {
1653 iterator it{};
1654 it.__state_ = path::iterator::_AtEnd;
1655 it.__path_ptr_ = this;
1656 return it;
Eric Fiselier91a182b2018-04-02 23:03:41 +00001657}
1658
1659path::iterator& path::iterator::__increment() {
Eric Fiselier91a182b2018-04-02 23:03:41 +00001660 PathParser PP(__path_ptr_->native(), __entry_, __state_);
1661 ++PP;
Eric Fiselier23a120c2018-07-25 03:31:48 +00001662 __state_ = static_cast<_ParserState>(PP.State);
Eric Fiselier91a182b2018-04-02 23:03:41 +00001663 __entry_ = PP.RawEntry;
1664 __stashed_elem_.__assign_view(*PP);
1665 return *this;
1666}
1667
1668path::iterator& path::iterator::__decrement() {
1669 PathParser PP(__path_ptr_->native(), __entry_, __state_);
1670 --PP;
Eric Fiselier23a120c2018-07-25 03:31:48 +00001671 __state_ = static_cast<_ParserState>(PP.State);
Eric Fiselier91a182b2018-04-02 23:03:41 +00001672 __entry_ = PP.RawEntry;
1673 __stashed_elem_.__assign_view(*PP);
1674 return *this;
1675}
1676
Eric Fiselier70474082018-07-20 01:22:32 +00001677///////////////////////////////////////////////////////////////////////////////
1678// directory entry definitions
1679///////////////////////////////////////////////////////////////////////////////
1680
1681#ifndef _LIBCPP_WIN32API
1682error_code directory_entry::__do_refresh() noexcept {
1683 __data_.__reset();
1684 error_code failure_ec;
1685
Eric Fiselier7eba47e2018-07-25 20:51:49 +00001686 StatT full_st;
Eric Fiselier70474082018-07-20 01:22:32 +00001687 file_status st = detail::posix_lstat(__p_, full_st, &failure_ec);
1688 if (!status_known(st)) {
1689 __data_.__reset();
1690 return failure_ec;
1691 }
1692
1693 if (!_VSTD_FS::exists(st) || !_VSTD_FS::is_symlink(st)) {
1694 __data_.__cache_type_ = directory_entry::_RefreshNonSymlink;
1695 __data_.__type_ = st.type();
1696 __data_.__non_sym_perms_ = st.permissions();
1697 } else { // we have a symlink
1698 __data_.__sym_perms_ = st.permissions();
1699 // Get the information about the linked entity.
1700 // Ignore errors from stat, since we don't want errors regarding symlink
1701 // resolution to be reported to the user.
1702 error_code ignored_ec;
1703 st = detail::posix_stat(__p_, full_st, &ignored_ec);
1704
1705 __data_.__type_ = st.type();
1706 __data_.__non_sym_perms_ = st.permissions();
1707
1708 // If we failed to resolve the link, then only partially populate the
1709 // cache.
1710 if (!status_known(st)) {
1711 __data_.__cache_type_ = directory_entry::_RefreshSymlinkUnresolved;
1712 return error_code{};
1713 }
Eric Fiselierabfdbdf2018-07-22 02:00:53 +00001714 // Otherwise, we resolved the link, potentially as not existing.
Eric Fiseliere39cea92018-07-20 08:36:45 +00001715 // That's OK.
Eric Fiselier70474082018-07-20 01:22:32 +00001716 __data_.__cache_type_ = directory_entry::_RefreshSymlink;
1717 }
1718
1719 if (_VSTD_FS::is_regular_file(st))
1720 __data_.__size_ = static_cast<uintmax_t>(full_st.st_size);
1721
1722 if (_VSTD_FS::exists(st)) {
1723 __data_.__nlink_ = static_cast<uintmax_t>(full_st.st_nlink);
1724
1725 // Attempt to extract the mtime, and fail if it's not representable using
1726 // file_time_type. For now we ignore the error, as we'll report it when
1727 // the value is actually used.
1728 error_code ignored_ec;
1729 __data_.__write_time_ =
1730 __extract_last_write_time(__p_, full_st, &ignored_ec);
1731 }
1732
1733 return failure_ec;
1734}
1735#else
1736error_code directory_entry::__do_refresh() noexcept {
1737 __data_.__reset();
1738 error_code failure_ec;
1739
1740 file_status st = _VSTD_FS::symlink_status(__p_, failure_ec);
1741 if (!status_known(st)) {
1742 __data_.__reset();
1743 return failure_ec;
1744 }
1745
1746 if (!_VSTD_FS::exists(st) || !_VSTD_FS::is_symlink(st)) {
1747 __data_.__cache_type_ = directory_entry::_RefreshNonSymlink;
1748 __data_.__type_ = st.type();
1749 __data_.__non_sym_perms_ = st.permissions();
1750 } else { // we have a symlink
1751 __data_.__sym_perms_ = st.permissions();
1752 // Get the information about the linked entity.
1753 // Ignore errors from stat, since we don't want errors regarding symlink
1754 // resolution to be reported to the user.
1755 error_code ignored_ec;
1756 st = _VSTD_FS::status(__p_, ignored_ec);
1757
1758 __data_.__type_ = st.type();
1759 __data_.__non_sym_perms_ = st.permissions();
1760
1761 // If we failed to resolve the link, then only partially populate the
1762 // cache.
1763 if (!status_known(st)) {
1764 __data_.__cache_type_ = directory_entry::_RefreshSymlinkUnresolved;
1765 return error_code{};
1766 }
Eric Fiselier70474082018-07-20 01:22:32 +00001767 __data_.__cache_type_ = directory_entry::_RefreshSymlink;
1768 }
1769
1770 // FIXME: This is currently broken, and the implementation only a placeholder.
1771 // We need to cache last_write_time, file_size, and hard_link_count here before
1772 // the implementation actually works.
1773
1774 return failure_ec;
1775}
1776#endif
Eric Fiselier91a182b2018-04-02 23:03:41 +00001777
Eric Fiselier02cea5e2018-07-27 03:07:09 +00001778_LIBCPP_END_NAMESPACE_FILESYSTEM