Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1 | //===--------------------- filesystem/ops.cpp -----------------------------===// |
| 2 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 9 | #include "filesystem" |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 10 | #include "array" |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 11 | #include "iterator" |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 12 | #include "string_view" |
| 13 | #include "type_traits" |
| 14 | #include "vector" |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 15 | #include "cstdlib" |
| 16 | #include "climits" |
| 17 | |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 18 | #include "filesystem_common.h" |
Eric Fiselier | 42d6d2c | 2017-07-08 04:18:41 +0000 | [diff] [blame] | 19 | |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 20 | #include <unistd.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/statvfs.h> |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 23 | #include <time.h> |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 24 | #include <fcntl.h> /* values for fchmodat */ |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 25 | |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 26 | #if __has_include(<sys/sendfile.h>) |
| 27 | # include <sys/sendfile.h> |
| 28 | # define _LIBCPP_FILESYSTEM_USE_SENDFILE |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 29 | #elif defined(__APPLE__) || __has_include(<copyfile.h>) |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 30 | # include <copyfile.h> |
| 31 | # define _LIBCPP_FILESYSTEM_USE_COPYFILE |
| 32 | #else |
| 33 | # include "fstream" |
| 34 | # define _LIBCPP_FILESYSTEM_USE_FSTREAM |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 35 | #endif |
Nico Weber | 4f1d63a | 2018-02-06 19:17:41 +0000 | [diff] [blame] | 36 | |
Louis Dionne | 678dc85 | 2020-02-12 17:01:19 +0100 | [diff] [blame] | 37 | #if !defined(CLOCK_REALTIME) |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 38 | # include <sys/time.h> // for gettimeofday and timeval |
| 39 | #endif |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 40 | |
Michał Górny | 8d676fb | 2019-12-02 11:49:20 +0100 | [diff] [blame] | 41 | #if defined(__ELF__) && defined(_LIBCPP_LINK_RT_LIB) |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 42 | # pragma comment(lib, "rt") |
Eric Fiselier | d8b25e3 | 2018-07-23 03:06:57 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 45 | _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 46 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 47 | namespace { |
| 48 | namespace parser { |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 49 | |
| 50 | using string_view_t = path::__string_view; |
| 51 | using string_view_pair = pair<string_view_t, string_view_t>; |
| 52 | using PosPtr = path::value_type const*; |
| 53 | |
| 54 | struct PathParser { |
| 55 | enum ParserState : unsigned char { |
| 56 | // Zero is a special sentinel value used by default constructed iterators. |
Eric Fiselier | 23a120c | 2018-07-25 03:31:48 +0000 | [diff] [blame] | 57 | PS_BeforeBegin = path::iterator::_BeforeBegin, |
| 58 | PS_InRootName = path::iterator::_InRootName, |
| 59 | PS_InRootDir = path::iterator::_InRootDir, |
| 60 | PS_InFilenames = path::iterator::_InFilenames, |
| 61 | PS_InTrailingSep = path::iterator::_InTrailingSep, |
| 62 | PS_AtEnd = path::iterator::_AtEnd |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | const string_view_t Path; |
| 66 | string_view_t RawEntry; |
| 67 | ParserState State; |
| 68 | |
| 69 | private: |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 70 | PathParser(string_view_t P, ParserState State) noexcept : Path(P), |
| 71 | State(State) {} |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 72 | |
| 73 | public: |
| 74 | PathParser(string_view_t P, string_view_t E, unsigned char S) |
| 75 | : Path(P), RawEntry(E), State(static_cast<ParserState>(S)) { |
| 76 | // S cannot be '0' or PS_BeforeBegin. |
| 77 | } |
| 78 | |
| 79 | static PathParser CreateBegin(string_view_t P) noexcept { |
| 80 | PathParser PP(P, PS_BeforeBegin); |
| 81 | PP.increment(); |
| 82 | return PP; |
| 83 | } |
| 84 | |
| 85 | static PathParser CreateEnd(string_view_t P) noexcept { |
| 86 | PathParser PP(P, PS_AtEnd); |
| 87 | return PP; |
| 88 | } |
| 89 | |
| 90 | PosPtr peek() const noexcept { |
| 91 | auto TkEnd = getNextTokenStartPos(); |
| 92 | auto End = getAfterBack(); |
| 93 | return TkEnd == End ? nullptr : TkEnd; |
| 94 | } |
| 95 | |
| 96 | void increment() noexcept { |
| 97 | const PosPtr End = getAfterBack(); |
| 98 | const PosPtr Start = getNextTokenStartPos(); |
| 99 | if (Start == End) |
| 100 | return makeState(PS_AtEnd); |
| 101 | |
| 102 | switch (State) { |
| 103 | case PS_BeforeBegin: { |
| 104 | PosPtr TkEnd = consumeSeparator(Start, End); |
| 105 | if (TkEnd) |
| 106 | return makeState(PS_InRootDir, Start, TkEnd); |
| 107 | else |
| 108 | return makeState(PS_InFilenames, Start, consumeName(Start, End)); |
| 109 | } |
| 110 | case PS_InRootDir: |
| 111 | return makeState(PS_InFilenames, Start, consumeName(Start, End)); |
| 112 | |
| 113 | case PS_InFilenames: { |
| 114 | PosPtr SepEnd = consumeSeparator(Start, End); |
| 115 | if (SepEnd != End) { |
| 116 | PosPtr TkEnd = consumeName(SepEnd, End); |
| 117 | if (TkEnd) |
| 118 | return makeState(PS_InFilenames, SepEnd, TkEnd); |
| 119 | } |
| 120 | return makeState(PS_InTrailingSep, Start, SepEnd); |
| 121 | } |
| 122 | |
| 123 | case PS_InTrailingSep: |
| 124 | return makeState(PS_AtEnd); |
| 125 | |
| 126 | case PS_InRootName: |
| 127 | case PS_AtEnd: |
| 128 | _LIBCPP_UNREACHABLE(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void decrement() noexcept { |
| 133 | const PosPtr REnd = getBeforeFront(); |
| 134 | const PosPtr RStart = getCurrentTokenStartPos() - 1; |
| 135 | if (RStart == REnd) // we're decrementing the begin |
| 136 | return makeState(PS_BeforeBegin); |
| 137 | |
| 138 | switch (State) { |
| 139 | case PS_AtEnd: { |
| 140 | // Try to consume a trailing separator or root directory first. |
| 141 | if (PosPtr SepEnd = consumeSeparator(RStart, REnd)) { |
| 142 | if (SepEnd == REnd) |
| 143 | return makeState(PS_InRootDir, Path.data(), RStart + 1); |
| 144 | return makeState(PS_InTrailingSep, SepEnd + 1, RStart + 1); |
| 145 | } else { |
| 146 | PosPtr TkStart = consumeName(RStart, REnd); |
| 147 | return makeState(PS_InFilenames, TkStart + 1, RStart + 1); |
| 148 | } |
| 149 | } |
| 150 | case PS_InTrailingSep: |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 151 | return makeState(PS_InFilenames, consumeName(RStart, REnd) + 1, |
| 152 | RStart + 1); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 153 | case PS_InFilenames: { |
| 154 | PosPtr SepEnd = consumeSeparator(RStart, REnd); |
| 155 | if (SepEnd == REnd) |
| 156 | return makeState(PS_InRootDir, Path.data(), RStart + 1); |
| 157 | PosPtr TkEnd = consumeName(SepEnd, REnd); |
| 158 | return makeState(PS_InFilenames, TkEnd + 1, SepEnd + 1); |
| 159 | } |
| 160 | case PS_InRootDir: |
| 161 | // return makeState(PS_InRootName, Path.data(), RStart + 1); |
| 162 | case PS_InRootName: |
| 163 | case PS_BeforeBegin: |
| 164 | _LIBCPP_UNREACHABLE(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /// \brief Return a view with the "preferred representation" of the current |
| 169 | /// element. For example trailing separators are represented as a '.' |
| 170 | string_view_t operator*() const noexcept { |
| 171 | switch (State) { |
| 172 | case PS_BeforeBegin: |
| 173 | case PS_AtEnd: |
| 174 | return ""; |
| 175 | case PS_InRootDir: |
| 176 | return "/"; |
| 177 | case PS_InTrailingSep: |
| 178 | return ""; |
| 179 | case PS_InRootName: |
| 180 | case PS_InFilenames: |
| 181 | return RawEntry; |
| 182 | } |
| 183 | _LIBCPP_UNREACHABLE(); |
| 184 | } |
| 185 | |
| 186 | explicit operator bool() const noexcept { |
| 187 | return State != PS_BeforeBegin && State != PS_AtEnd; |
| 188 | } |
| 189 | |
| 190 | PathParser& operator++() noexcept { |
| 191 | increment(); |
| 192 | return *this; |
| 193 | } |
| 194 | |
| 195 | PathParser& operator--() noexcept { |
| 196 | decrement(); |
| 197 | return *this; |
| 198 | } |
| 199 | |
Eric Fiselier | c9a770e | 2018-12-21 03:16:30 +0000 | [diff] [blame] | 200 | bool atEnd() const noexcept { |
| 201 | return State == PS_AtEnd; |
| 202 | } |
| 203 | |
| 204 | bool inRootDir() const noexcept { |
| 205 | return State == PS_InRootDir; |
| 206 | } |
| 207 | |
| 208 | bool inRootName() const noexcept { |
| 209 | return State == PS_InRootName; |
| 210 | } |
| 211 | |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 212 | bool inRootPath() const noexcept { |
Eric Fiselier | c9a770e | 2018-12-21 03:16:30 +0000 | [diff] [blame] | 213 | return inRootName() || inRootDir(); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | private: |
| 217 | void makeState(ParserState NewState, PosPtr Start, PosPtr End) noexcept { |
| 218 | State = NewState; |
| 219 | RawEntry = string_view_t(Start, End - Start); |
| 220 | } |
| 221 | void makeState(ParserState NewState) noexcept { |
| 222 | State = NewState; |
| 223 | RawEntry = {}; |
| 224 | } |
| 225 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 226 | PosPtr getAfterBack() const noexcept { return Path.data() + Path.size(); } |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 227 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 228 | PosPtr getBeforeFront() const noexcept { return Path.data() - 1; } |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 229 | |
| 230 | /// \brief Return a pointer to the first character after the currently |
| 231 | /// lexed element. |
| 232 | PosPtr getNextTokenStartPos() const noexcept { |
| 233 | switch (State) { |
| 234 | case PS_BeforeBegin: |
| 235 | return Path.data(); |
| 236 | case PS_InRootName: |
| 237 | case PS_InRootDir: |
| 238 | case PS_InFilenames: |
| 239 | return &RawEntry.back() + 1; |
| 240 | case PS_InTrailingSep: |
| 241 | case PS_AtEnd: |
| 242 | return getAfterBack(); |
| 243 | } |
| 244 | _LIBCPP_UNREACHABLE(); |
| 245 | } |
| 246 | |
| 247 | /// \brief Return a pointer to the first character in the currently lexed |
| 248 | /// element. |
| 249 | PosPtr getCurrentTokenStartPos() const noexcept { |
| 250 | switch (State) { |
| 251 | case PS_BeforeBegin: |
| 252 | case PS_InRootName: |
| 253 | return &Path.front(); |
| 254 | case PS_InRootDir: |
| 255 | case PS_InFilenames: |
| 256 | case PS_InTrailingSep: |
| 257 | return &RawEntry.front(); |
| 258 | case PS_AtEnd: |
| 259 | return &Path.back() + 1; |
| 260 | } |
| 261 | _LIBCPP_UNREACHABLE(); |
| 262 | } |
| 263 | |
| 264 | PosPtr consumeSeparator(PosPtr P, PosPtr End) const noexcept { |
| 265 | if (P == End || *P != '/') |
| 266 | return nullptr; |
| 267 | const int Inc = P < End ? 1 : -1; |
| 268 | P += Inc; |
| 269 | while (P != End && *P == '/') |
| 270 | P += Inc; |
| 271 | return P; |
| 272 | } |
| 273 | |
| 274 | PosPtr consumeName(PosPtr P, PosPtr End) const noexcept { |
| 275 | if (P == End || *P == '/') |
| 276 | return nullptr; |
| 277 | const int Inc = P < End ? 1 : -1; |
| 278 | P += Inc; |
| 279 | while (P != End && *P != '/') |
| 280 | P += Inc; |
| 281 | return P; |
| 282 | } |
| 283 | }; |
| 284 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 285 | string_view_pair separate_filename(string_view_t const& s) { |
| 286 | if (s == "." || s == ".." || s.empty()) |
| 287 | return string_view_pair{s, ""}; |
| 288 | auto pos = s.find_last_of('.'); |
| 289 | if (pos == string_view_t::npos || pos == 0) |
| 290 | return string_view_pair{s, string_view_t{}}; |
| 291 | return string_view_pair{s.substr(0, pos), s.substr(pos)}; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | string_view_t createView(PosPtr S, PosPtr E) noexcept { |
| 295 | return {S, static_cast<size_t>(E - S) + 1}; |
| 296 | } |
| 297 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 298 | } // namespace parser |
| 299 | } // namespace |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 300 | |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 301 | // POSIX HELPERS |
| 302 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 303 | namespace detail { |
| 304 | namespace { |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 305 | |
| 306 | using value_type = path::value_type; |
| 307 | using string_type = path::string_type; |
| 308 | |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 309 | struct FileDescriptor { |
| 310 | const path& name; |
| 311 | int fd = -1; |
| 312 | StatT m_stat; |
| 313 | file_status m_status; |
| 314 | |
| 315 | template <class... Args> |
| 316 | static FileDescriptor create(const path* p, error_code& ec, Args... args) { |
| 317 | ec.clear(); |
| 318 | int fd; |
| 319 | if ((fd = ::open(p->c_str(), args...)) == -1) { |
| 320 | ec = capture_errno(); |
| 321 | return FileDescriptor{p}; |
| 322 | } |
| 323 | return FileDescriptor(p, fd); |
| 324 | } |
| 325 | |
| 326 | template <class... Args> |
| 327 | static FileDescriptor create_with_status(const path* p, error_code& ec, |
| 328 | Args... args) { |
| 329 | FileDescriptor fd = create(p, ec, args...); |
| 330 | if (!ec) |
| 331 | fd.refresh_status(ec); |
| 332 | |
| 333 | return fd; |
| 334 | } |
| 335 | |
| 336 | file_status get_status() const { return m_status; } |
| 337 | StatT const& get_stat() const { return m_stat; } |
| 338 | |
| 339 | bool status_known() const { return _VSTD_FS::status_known(m_status); } |
| 340 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 341 | file_status refresh_status(error_code& ec); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 342 | |
| 343 | void close() noexcept { |
| 344 | if (fd != -1) |
| 345 | ::close(fd); |
| 346 | fd = -1; |
| 347 | } |
| 348 | |
| 349 | FileDescriptor(FileDescriptor&& other) |
| 350 | : name(other.name), fd(other.fd), m_stat(other.m_stat), |
| 351 | m_status(other.m_status) { |
| 352 | other.fd = -1; |
| 353 | other.m_status = file_status{}; |
| 354 | } |
| 355 | |
| 356 | ~FileDescriptor() { close(); } |
| 357 | |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 358 | FileDescriptor(FileDescriptor const&) = delete; |
| 359 | FileDescriptor& operator=(FileDescriptor const&) = delete; |
| 360 | |
| 361 | private: |
| 362 | explicit FileDescriptor(const path* p, int fd = -1) : name(*p), fd(fd) {} |
| 363 | }; |
| 364 | |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 365 | perms posix_get_perms(const StatT& st) noexcept { |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 366 | return static_cast<perms>(st.st_mode) & perms::mask; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | ::mode_t posix_convert_perms(perms prms) { |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 370 | return static_cast< ::mode_t>(prms & perms::mask); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 373 | file_status create_file_status(error_code& m_ec, path const& p, |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 374 | const StatT& path_stat, error_code* ec) { |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 375 | if (ec) |
| 376 | *ec = m_ec; |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 377 | if (m_ec && (m_ec.value() == ENOENT || m_ec.value() == ENOTDIR)) { |
| 378 | return file_status(file_type::not_found); |
| 379 | } else if (m_ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 380 | ErrorHandler<void> err("posix_stat", ec, &p); |
| 381 | err.report(m_ec, "failed to determine attributes for the specified path"); |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 382 | return file_status(file_type::none); |
| 383 | } |
| 384 | // else |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 385 | |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 386 | file_status fs_tmp; |
| 387 | auto const mode = path_stat.st_mode; |
| 388 | if (S_ISLNK(mode)) |
| 389 | fs_tmp.type(file_type::symlink); |
| 390 | else if (S_ISREG(mode)) |
| 391 | fs_tmp.type(file_type::regular); |
| 392 | else if (S_ISDIR(mode)) |
| 393 | fs_tmp.type(file_type::directory); |
| 394 | else if (S_ISBLK(mode)) |
| 395 | fs_tmp.type(file_type::block); |
| 396 | else if (S_ISCHR(mode)) |
| 397 | fs_tmp.type(file_type::character); |
| 398 | else if (S_ISFIFO(mode)) |
| 399 | fs_tmp.type(file_type::fifo); |
| 400 | else if (S_ISSOCK(mode)) |
| 401 | fs_tmp.type(file_type::socket); |
| 402 | else |
| 403 | fs_tmp.type(file_type::unknown); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 404 | |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 405 | fs_tmp.permissions(detail::posix_get_perms(path_stat)); |
| 406 | return fs_tmp; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 409 | file_status posix_stat(path const& p, StatT& path_stat, error_code* ec) { |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 410 | error_code m_ec; |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 411 | if (::stat(p.c_str(), &path_stat) == -1) |
| 412 | m_ec = detail::capture_errno(); |
| 413 | return create_file_status(m_ec, p, path_stat, ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 416 | file_status posix_stat(path const& p, error_code* ec) { |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 417 | StatT path_stat; |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 418 | return posix_stat(p, path_stat, ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 421 | file_status posix_lstat(path const& p, StatT& path_stat, error_code* ec) { |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 422 | error_code m_ec; |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 423 | if (::lstat(p.c_str(), &path_stat) == -1) |
| 424 | m_ec = detail::capture_errno(); |
| 425 | return create_file_status(m_ec, p, path_stat, ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 428 | file_status posix_lstat(path const& p, error_code* ec) { |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 429 | StatT path_stat; |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 430 | return posix_lstat(p, path_stat, ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Dan Albert | 39b981d | 2019-01-15 19:16:25 +0000 | [diff] [blame] | 433 | // http://pubs.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html |
| 434 | bool posix_ftruncate(const FileDescriptor& fd, off_t to_size, error_code& ec) { |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 435 | if (::ftruncate(fd.fd, to_size) == -1) { |
| 436 | ec = capture_errno(); |
Eric Fiselier | f1aba0d | 2018-07-26 04:02:06 +0000 | [diff] [blame] | 437 | return true; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 438 | } |
| 439 | ec.clear(); |
Eric Fiselier | f1aba0d | 2018-07-26 04:02:06 +0000 | [diff] [blame] | 440 | return false; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | bool posix_fchmod(const FileDescriptor& fd, const StatT& st, error_code& ec) { |
| 444 | if (::fchmod(fd.fd, st.st_mode) == -1) { |
| 445 | ec = capture_errno(); |
Eric Fiselier | f1aba0d | 2018-07-26 04:02:06 +0000 | [diff] [blame] | 446 | return true; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 447 | } |
| 448 | ec.clear(); |
Eric Fiselier | f1aba0d | 2018-07-26 04:02:06 +0000 | [diff] [blame] | 449 | return false; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | bool stat_equivalent(const StatT& st1, const StatT& st2) { |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 453 | return (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 456 | file_status FileDescriptor::refresh_status(error_code& ec) { |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 457 | // FD must be open and good. |
| 458 | m_status = file_status{}; |
Eric Fiselier | d8b25e3 | 2018-07-23 03:06:57 +0000 | [diff] [blame] | 459 | m_stat = {}; |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 460 | error_code m_ec; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 461 | if (::fstat(fd, &m_stat) == -1) |
| 462 | m_ec = capture_errno(); |
| 463 | m_status = create_file_status(m_ec, name, m_stat, &ec); |
| 464 | return m_status; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 465 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 466 | } // namespace |
| 467 | } // end namespace detail |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 468 | |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 469 | using detail::capture_errno; |
| 470 | using detail::ErrorHandler; |
| 471 | using detail::StatT; |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 472 | using detail::TimeSpec; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 473 | using parser::createView; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 474 | using parser::PathParser; |
| 475 | using parser::string_view_t; |
| 476 | |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 477 | const bool _FilesystemClock::is_steady; |
| 478 | |
| 479 | _FilesystemClock::time_point _FilesystemClock::now() noexcept { |
| 480 | typedef chrono::duration<rep> __secs; |
Louis Dionne | 678dc85 | 2020-02-12 17:01:19 +0100 | [diff] [blame] | 481 | #if defined(CLOCK_REALTIME) |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 482 | typedef chrono::duration<rep, nano> __nsecs; |
| 483 | struct timespec tp; |
| 484 | if (0 != clock_gettime(CLOCK_REALTIME, &tp)) |
| 485 | __throw_system_error(errno, "clock_gettime(CLOCK_REALTIME) failed"); |
| 486 | return time_point(__secs(tp.tv_sec) + |
| 487 | chrono::duration_cast<duration>(__nsecs(tp.tv_nsec))); |
| 488 | #else |
| 489 | typedef chrono::duration<rep, micro> __microsecs; |
| 490 | timeval tv; |
| 491 | gettimeofday(&tv, 0); |
| 492 | return time_point(__secs(tv.tv_sec) + __microsecs(tv.tv_usec)); |
Louis Dionne | 678dc85 | 2020-02-12 17:01:19 +0100 | [diff] [blame] | 493 | #endif // CLOCK_REALTIME |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | filesystem_error::~filesystem_error() {} |
| 497 | |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 498 | void filesystem_error::__create_what(int __num_paths) { |
| 499 | const char* derived_what = system_error::what(); |
| 500 | __storage_->__what_ = [&]() -> string { |
| 501 | const char* p1 = path1().native().empty() ? "\"\"" : path1().c_str(); |
| 502 | const char* p2 = path2().native().empty() ? "\"\"" : path2().c_str(); |
| 503 | switch (__num_paths) { |
| 504 | default: |
| 505 | return detail::format_string("filesystem error: %s", derived_what); |
| 506 | case 1: |
| 507 | return detail::format_string("filesystem error: %s [%s]", derived_what, |
| 508 | p1); |
| 509 | case 2: |
| 510 | return detail::format_string("filesystem error: %s [%s] [%s]", |
| 511 | derived_what, p1, p2); |
| 512 | } |
| 513 | }(); |
| 514 | } |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 515 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 516 | static path __do_absolute(const path& p, path* cwd, error_code* ec) { |
| 517 | if (ec) |
| 518 | ec->clear(); |
| 519 | if (p.is_absolute()) |
| 520 | return p; |
| 521 | *cwd = __current_path(ec); |
| 522 | if (ec && *ec) |
| 523 | return {}; |
| 524 | return (*cwd) / p; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 527 | path __absolute(const path& p, error_code* ec) { |
| 528 | path cwd; |
| 529 | return __do_absolute(p, &cwd, ec); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 532 | path __canonical(path const& orig_p, error_code* ec) { |
| 533 | path cwd; |
| 534 | ErrorHandler<path> err("canonical", ec, &orig_p, &cwd); |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 535 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 536 | path p = __do_absolute(orig_p, &cwd, ec); |
YAMAMOTO Takashi | 43f1908 | 2020-10-28 15:40:16 -0400 | [diff] [blame] | 537 | #if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112 |
Eric Fiselier | b521530 | 2019-01-17 02:59:28 +0000 | [diff] [blame] | 538 | std::unique_ptr<char, decltype(&::free)> |
| 539 | hold(::realpath(p.c_str(), nullptr), &::free); |
| 540 | if (hold.get() == nullptr) |
| 541 | return err.report(capture_errno()); |
| 542 | return {hold.get()}; |
| 543 | #else |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 544 | char buff[PATH_MAX + 1]; |
| 545 | char* ret; |
| 546 | if ((ret = ::realpath(p.c_str(), buff)) == nullptr) |
| 547 | return err.report(capture_errno()); |
| 548 | return {ret}; |
Eric Fiselier | b521530 | 2019-01-17 02:59:28 +0000 | [diff] [blame] | 549 | #endif |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | void __copy(const path& from, const path& to, copy_options options, |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 553 | error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 554 | ErrorHandler<void> err("copy", ec, &from, &to); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 555 | |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 556 | const bool sym_status = bool( |
| 557 | options & (copy_options::create_symlinks | copy_options::skip_symlinks)); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 558 | |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 559 | const bool sym_status2 = bool(options & copy_options::copy_symlinks); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 560 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 561 | error_code m_ec1; |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 562 | StatT f_st = {}; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 563 | 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 Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 568 | |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 569 | StatT t_st = {}; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 570 | const file_status t = sym_status ? detail::posix_lstat(to, t_st, &m_ec1) |
| 571 | : detail::posix_stat(to, t_st, &m_ec1); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 572 | |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 573 | 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 Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 581 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 582 | if (ec) |
| 583 | ec->clear(); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 584 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 585 | 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 Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 592 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 593 | 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 Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 605 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 606 | 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 Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 611 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 612 | 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 Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 618 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 619 | 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 Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 638 | namespace detail { |
| 639 | namespace { |
| 640 | |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 641 | #if defined(_LIBCPP_FILESYSTEM_USE_SENDFILE) |
| 642 | bool copy_file_impl(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) { |
| 643 | size_t count = read_fd.get_stat().st_size; |
| 644 | do { |
| 645 | ssize_t res; |
| 646 | if ((res = ::sendfile(write_fd.fd, read_fd.fd, nullptr, count)) == -1) { |
| 647 | ec = capture_errno(); |
| 648 | return false; |
| 649 | } |
| 650 | count -= res; |
| 651 | } while (count > 0); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 652 | |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 653 | ec.clear(); |
| 654 | |
| 655 | return true; |
| 656 | } |
| 657 | #elif defined(_LIBCPP_FILESYSTEM_USE_COPYFILE) |
| 658 | bool copy_file_impl(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) { |
| 659 | struct CopyFileState { |
| 660 | copyfile_state_t state; |
| 661 | CopyFileState() { state = copyfile_state_alloc(); } |
| 662 | ~CopyFileState() { copyfile_state_free(state); } |
| 663 | |
| 664 | private: |
| 665 | CopyFileState(CopyFileState const&) = delete; |
| 666 | CopyFileState& operator=(CopyFileState const&) = delete; |
| 667 | }; |
| 668 | |
| 669 | CopyFileState cfs; |
| 670 | if (fcopyfile(read_fd.fd, write_fd.fd, cfs.state, COPYFILE_DATA) < 0) { |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 671 | ec = capture_errno(); |
| 672 | return false; |
| 673 | } |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 674 | |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 675 | ec.clear(); |
| 676 | return true; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 677 | } |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 678 | #elif defined(_LIBCPP_FILESYSTEM_USE_FSTREAM) |
| 679 | bool copy_file_impl(FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) { |
| 680 | ifstream in; |
| 681 | in.__open(read_fd.fd, ios::binary); |
| 682 | if (!in.is_open()) { |
| 683 | // This assumes that __open didn't reset the error code. |
| 684 | ec = capture_errno(); |
| 685 | return false; |
| 686 | } |
| 687 | ofstream out; |
| 688 | out.__open(write_fd.fd, ios::binary); |
| 689 | if (!out.is_open()) { |
| 690 | ec = capture_errno(); |
| 691 | return false; |
| 692 | } |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 693 | |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 694 | if (in.good() && out.good()) { |
| 695 | using InIt = istreambuf_iterator<char>; |
| 696 | using OutIt = ostreambuf_iterator<char>; |
| 697 | InIt bin(in); |
| 698 | InIt ein; |
| 699 | OutIt bout(out); |
| 700 | copy(bin, ein, bout); |
| 701 | } |
| 702 | if (out.fail() || in.fail()) { |
| 703 | ec = make_error_code(errc::io_error); |
| 704 | return false; |
| 705 | } |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 706 | |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 707 | ec.clear(); |
| 708 | return true; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 709 | } |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 710 | #else |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 711 | # error "Unknown implementation for copy_file_impl" |
| 712 | #endif // copy_file_impl implementation |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 713 | |
Louis Dionne | 27bf986 | 2020-10-15 13:14:22 -0400 | [diff] [blame] | 714 | } // end anonymous namespace |
| 715 | } // end namespace detail |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 716 | |
| 717 | bool __copy_file(const path& from, const path& to, copy_options options, |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 718 | error_code* ec) { |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 719 | using detail::FileDescriptor; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 720 | ErrorHandler<bool> err("copy_file", ec, &to, &from); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 721 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 722 | error_code m_ec; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 723 | FileDescriptor from_fd = |
| 724 | FileDescriptor::create_with_status(&from, m_ec, O_RDONLY | O_NONBLOCK); |
| 725 | if (m_ec) |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 726 | return err.report(m_ec); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 727 | |
| 728 | auto from_st = from_fd.get_status(); |
| 729 | StatT const& from_stat = from_fd.get_stat(); |
| 730 | if (!is_regular_file(from_st)) { |
| 731 | if (not m_ec) |
| 732 | m_ec = make_error_code(errc::not_supported); |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 733 | return err.report(m_ec); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | const bool skip_existing = bool(copy_options::skip_existing & options); |
| 737 | const bool update_existing = bool(copy_options::update_existing & options); |
| 738 | const bool overwrite_existing = |
| 739 | bool(copy_options::overwrite_existing & options); |
| 740 | |
| 741 | StatT to_stat_path; |
| 742 | file_status to_st = detail::posix_stat(to, to_stat_path, &m_ec); |
| 743 | if (!status_known(to_st)) |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 744 | return err.report(m_ec); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 745 | |
| 746 | const bool to_exists = exists(to_st); |
| 747 | if (to_exists && !is_regular_file(to_st)) |
Eric Fiselier | 268fa83 | 2018-07-23 11:55:13 +0000 | [diff] [blame] | 748 | return err.report(errc::not_supported); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 749 | |
| 750 | if (to_exists && detail::stat_equivalent(from_stat, to_stat_path)) |
Eric Fiselier | 268fa83 | 2018-07-23 11:55:13 +0000 | [diff] [blame] | 751 | return err.report(errc::file_exists); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 752 | |
| 753 | if (to_exists && skip_existing) |
| 754 | return false; |
| 755 | |
Eric Fiselier | 455ac4b | 2018-07-22 21:15:15 +0000 | [diff] [blame] | 756 | bool ShouldCopy = [&]() { |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 757 | if (to_exists && update_existing) { |
| 758 | auto from_time = detail::extract_mtime(from_stat); |
| 759 | auto to_time = detail::extract_mtime(to_stat_path); |
| 760 | if (from_time.tv_sec < to_time.tv_sec) |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 761 | return false; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 762 | if (from_time.tv_sec == to_time.tv_sec && |
| 763 | from_time.tv_nsec <= to_time.tv_nsec) |
Eric Fiselier | e735925 | 2016-10-16 00:47:59 +0000 | [diff] [blame] | 764 | return false; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 765 | return true; |
Eric Fiselier | e735925 | 2016-10-16 00:47:59 +0000 | [diff] [blame] | 766 | } |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 767 | if (!to_exists || overwrite_existing) |
| 768 | return true; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 769 | return err.report(errc::file_exists); |
Eric Fiselier | 455ac4b | 2018-07-22 21:15:15 +0000 | [diff] [blame] | 770 | }(); |
| 771 | if (!ShouldCopy) |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 772 | return false; |
Eric Fiselier | e735925 | 2016-10-16 00:47:59 +0000 | [diff] [blame] | 773 | |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 774 | // Don't truncate right away. We may not be opening the file we originally |
| 775 | // looked at; we'll check this later. |
Eric Fiselier | 455ac4b | 2018-07-22 21:15:15 +0000 | [diff] [blame] | 776 | int to_open_flags = O_WRONLY; |
| 777 | if (!to_exists) |
| 778 | to_open_flags |= O_CREAT; |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 779 | FileDescriptor to_fd = FileDescriptor::create_with_status( |
| 780 | &to, m_ec, to_open_flags, from_stat.st_mode); |
| 781 | if (m_ec) |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 782 | return err.report(m_ec); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 783 | |
| 784 | if (to_exists) { |
| 785 | // Check that the file we initially stat'ed is equivalent to the one |
| 786 | // we opened. |
Eric Fiselier | 455ac4b | 2018-07-22 21:15:15 +0000 | [diff] [blame] | 787 | // FIXME: report this better. |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 788 | if (!detail::stat_equivalent(to_stat_path, to_fd.get_stat())) |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 789 | return err.report(errc::bad_file_descriptor); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 790 | |
| 791 | // Set the permissions and truncate the file we opened. |
Eric Fiselier | f1aba0d | 2018-07-26 04:02:06 +0000 | [diff] [blame] | 792 | if (detail::posix_fchmod(to_fd, from_stat, m_ec)) |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 793 | return err.report(m_ec); |
Eric Fiselier | f1aba0d | 2018-07-26 04:02:06 +0000 | [diff] [blame] | 794 | if (detail::posix_ftruncate(to_fd, 0, m_ec)) |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 795 | return err.report(m_ec); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | if (!copy_file_impl(from_fd, to_fd, m_ec)) { |
| 799 | // FIXME: Remove the dest file if we failed, and it didn't exist previously. |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 800 | return err.report(m_ec); |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | return true; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | void __copy_symlink(const path& existing_symlink, const path& new_symlink, |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 807 | error_code* ec) { |
| 808 | const path real_path(__read_symlink(existing_symlink, ec)); |
| 809 | if (ec && *ec) { |
| 810 | return; |
| 811 | } |
| 812 | // NOTE: proposal says you should detect if you should call |
| 813 | // create_symlink or create_directory_symlink. I don't think this |
| 814 | // is needed with POSIX |
| 815 | __create_symlink(real_path, new_symlink, ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 818 | bool __create_directories(const path& p, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 819 | ErrorHandler<bool> err("create_directories", ec, &p); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 820 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 821 | error_code m_ec; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 822 | auto const st = detail::posix_stat(p, &m_ec); |
| 823 | if (!status_known(st)) |
| 824 | return err.report(m_ec); |
| 825 | else if (is_directory(st)) |
| 826 | return false; |
| 827 | else if (exists(st)) |
| 828 | return err.report(errc::file_exists); |
| 829 | |
| 830 | const path parent = p.parent_path(); |
| 831 | if (!parent.empty()) { |
| 832 | const file_status parent_st = status(parent, m_ec); |
| 833 | if (not status_known(parent_st)) |
| 834 | return err.report(m_ec); |
| 835 | if (not exists(parent_st)) { |
| 836 | __create_directories(parent, ec); |
| 837 | if (ec && *ec) { |
| 838 | return false; |
| 839 | } |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 840 | } |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 841 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 842 | return __create_directory(p, ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 845 | bool __create_directory(const path& p, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 846 | ErrorHandler<bool> err("create_directory", ec, &p); |
| 847 | |
| 848 | if (::mkdir(p.c_str(), static_cast<int>(perms::all)) == 0) |
| 849 | return true; |
Eric Fiselier | 7ca3db8 | 2018-07-25 04:46:32 +0000 | [diff] [blame] | 850 | if (errno != EEXIST) |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 851 | err.report(capture_errno()); |
| 852 | return false; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 855 | bool __create_directory(path const& p, path const& attributes, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 856 | ErrorHandler<bool> err("create_directory", ec, &p, &attributes); |
| 857 | |
| 858 | StatT attr_stat; |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 859 | error_code mec; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 860 | auto st = detail::posix_stat(attributes, attr_stat, &mec); |
| 861 | if (!status_known(st)) |
| 862 | return err.report(mec); |
Eric Fiselier | 7ca3db8 | 2018-07-25 04:46:32 +0000 | [diff] [blame] | 863 | if (!is_directory(st)) |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 864 | return err.report(errc::not_a_directory, |
| 865 | "the specified attribute path is invalid"); |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 866 | |
| 867 | if (::mkdir(p.c_str(), attr_stat.st_mode) == 0) |
| 868 | return true; |
Eric Fiselier | 7ca3db8 | 2018-07-25 04:46:32 +0000 | [diff] [blame] | 869 | if (errno != EEXIST) |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 870 | err.report(capture_errno()); |
| 871 | return false; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 874 | void __create_directory_symlink(path const& from, path const& to, |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 875 | error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 876 | ErrorHandler<void> err("create_directory_symlink", ec, &from, &to); |
| 877 | if (::symlink(from.c_str(), to.c_str()) != 0) |
| 878 | return err.report(capture_errno()); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 881 | void __create_hard_link(const path& from, const path& to, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 882 | ErrorHandler<void> err("create_hard_link", ec, &from, &to); |
| 883 | if (::link(from.c_str(), to.c_str()) == -1) |
| 884 | return err.report(capture_errno()); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 887 | void __create_symlink(path const& from, path const& to, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 888 | ErrorHandler<void> err("create_symlink", ec, &from, &to); |
| 889 | if (::symlink(from.c_str(), to.c_str()) == -1) |
| 890 | return err.report(capture_errno()); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 893 | path __current_path(error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 894 | ErrorHandler<path> err("current_path", ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 895 | |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 896 | auto size = ::pathconf(".", _PC_PATH_MAX); |
| 897 | _LIBCPP_ASSERT(size >= 0, "pathconf returned a 0 as max size"); |
| 898 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 899 | auto buff = unique_ptr<char[]>(new char[size + 1]); |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 900 | char* ret; |
| 901 | if ((ret = ::getcwd(buff.get(), static_cast<size_t>(size))) == nullptr) |
| 902 | return err.report(capture_errno(), "call to getcwd failed"); |
| 903 | |
| 904 | return {buff.get()}; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 907 | void __current_path(const path& p, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 908 | ErrorHandler<void> err("current_path", ec, &p); |
| 909 | if (::chdir(p.c_str()) == -1) |
| 910 | err.report(capture_errno()); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 913 | bool __equivalent(const path& p1, const path& p2, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 914 | ErrorHandler<bool> err("equivalent", ec, &p1, &p2); |
| 915 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 916 | error_code ec1, ec2; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 917 | StatT st1 = {}, st2 = {}; |
| 918 | auto s1 = detail::posix_stat(p1.native(), st1, &ec1); |
| 919 | if (!exists(s1)) |
| 920 | return err.report(errc::not_supported); |
| 921 | auto s2 = detail::posix_stat(p2.native(), st2, &ec2); |
| 922 | if (!exists(s2)) |
| 923 | return err.report(errc::not_supported); |
| 924 | |
| 925 | return detail::stat_equivalent(st1, st2); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 928 | uintmax_t __file_size(const path& p, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 929 | ErrorHandler<uintmax_t> err("file_size", ec, &p); |
| 930 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 931 | error_code m_ec; |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 932 | StatT st; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 933 | file_status fst = detail::posix_stat(p, st, &m_ec); |
| 934 | if (!exists(fst) || !is_regular_file(fst)) { |
| 935 | errc error_kind = |
| 936 | is_directory(fst) ? errc::is_a_directory : errc::not_supported; |
| 937 | if (!m_ec) |
| 938 | m_ec = make_error_code(error_kind); |
| 939 | return err.report(m_ec); |
| 940 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 941 | // is_regular_file(p) == true |
| 942 | return static_cast<uintmax_t>(st.st_size); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 945 | uintmax_t __hard_link_count(const path& p, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 946 | ErrorHandler<uintmax_t> err("hard_link_count", ec, &p); |
| 947 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 948 | error_code m_ec; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 949 | StatT st; |
| 950 | detail::posix_stat(p, st, &m_ec); |
| 951 | if (m_ec) |
| 952 | return err.report(m_ec); |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 953 | return static_cast<uintmax_t>(st.st_nlink); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 956 | bool __fs_is_empty(const path& p, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 957 | ErrorHandler<bool> err("is_empty", ec, &p); |
Eric Fiselier | aa8c61f | 2016-10-15 23:05:04 +0000 | [diff] [blame] | 958 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 959 | error_code m_ec; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 960 | StatT pst; |
| 961 | auto st = detail::posix_stat(p, pst, &m_ec); |
| 962 | if (m_ec) |
| 963 | return err.report(m_ec); |
| 964 | else if (!is_directory(st) && !is_regular_file(st)) |
| 965 | return err.report(errc::not_supported); |
| 966 | else if (is_directory(st)) { |
| 967 | auto it = ec ? directory_iterator(p, *ec) : directory_iterator(p); |
| 968 | if (ec && *ec) |
| 969 | return false; |
| 970 | return it == directory_iterator{}; |
| 971 | } else if (is_regular_file(st)) |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 972 | return static_cast<uintmax_t>(pst.st_size) == 0; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 973 | |
| 974 | _LIBCPP_UNREACHABLE(); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 977 | static file_time_type __extract_last_write_time(const path& p, const StatT& st, |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 978 | error_code* ec) { |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 979 | using detail::fs_time; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 980 | ErrorHandler<file_time_type> err("last_write_time", ec, &p); |
| 981 | |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 982 | auto ts = detail::extract_mtime(st); |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 983 | if (!fs_time::is_representable(ts)) |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 984 | return err.report(errc::value_too_large); |
| 985 | |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 986 | return fs_time::convert_from_timespec(ts); |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 987 | } |
Eric Fiselier | 42d6d2c | 2017-07-08 04:18:41 +0000 | [diff] [blame] | 988 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 989 | file_time_type __last_write_time(const path& p, error_code* ec) { |
| 990 | using namespace chrono; |
| 991 | ErrorHandler<file_time_type> err("last_write_time", ec, &p); |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 992 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 993 | error_code m_ec; |
| 994 | StatT st; |
| 995 | detail::posix_stat(p, st, &m_ec); |
| 996 | if (m_ec) |
| 997 | return err.report(m_ec); |
| 998 | return __extract_last_write_time(p, st, ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1001 | void __last_write_time(const path& p, file_time_type new_time, error_code* ec) { |
| 1002 | using detail::fs_time; |
| 1003 | ErrorHandler<void> err("last_write_time", ec, &p); |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1004 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1005 | error_code m_ec; |
| 1006 | array<TimeSpec, 2> tbuf; |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 1007 | #if !defined(_LIBCPP_USE_UTIMENSAT) |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1008 | // This implementation has a race condition between determining the |
| 1009 | // last access time and attempting to set it to the same value using |
| 1010 | // ::utimes |
| 1011 | StatT st; |
| 1012 | file_status fst = detail::posix_stat(p, st, &m_ec); |
| 1013 | if (m_ec) |
| 1014 | return err.report(m_ec); |
| 1015 | tbuf[0] = detail::extract_atime(st); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1016 | #else |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1017 | tbuf[0].tv_sec = 0; |
| 1018 | tbuf[0].tv_nsec = UTIME_OMIT; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1019 | #endif |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1020 | if (!fs_time::convert_to_timespec(tbuf[1], new_time)) |
| 1021 | return err.report(errc::value_too_large); |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 1022 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1023 | detail::set_file_times(p, tbuf, m_ec); |
| 1024 | if (m_ec) |
| 1025 | return err.report(m_ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Eric Fiselier | 4f3dc0e | 2018-03-26 06:23:55 +0000 | [diff] [blame] | 1028 | void __permissions(const path& p, perms prms, perm_options opts, |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1029 | error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1030 | ErrorHandler<void> err("permissions", ec, &p); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1031 | |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1032 | auto has_opt = [&](perm_options o) { return bool(o & opts); }; |
| 1033 | const bool resolve_symlinks = !has_opt(perm_options::nofollow); |
| 1034 | const bool add_perms = has_opt(perm_options::add); |
| 1035 | const bool remove_perms = has_opt(perm_options::remove); |
| 1036 | _LIBCPP_ASSERT( |
| 1037 | (add_perms + remove_perms + has_opt(perm_options::replace)) == 1, |
| 1038 | "One and only one of the perm_options constants replace, add, or remove " |
| 1039 | "is present in opts"); |
| 1040 | |
| 1041 | bool set_sym_perms = false; |
| 1042 | prms &= perms::mask; |
| 1043 | if (!resolve_symlinks || (add_perms || remove_perms)) { |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 1044 | error_code m_ec; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1045 | file_status st = resolve_symlinks ? detail::posix_stat(p, &m_ec) |
| 1046 | : detail::posix_lstat(p, &m_ec); |
| 1047 | set_sym_perms = is_symlink(st); |
| 1048 | if (m_ec) |
| 1049 | return err.report(m_ec); |
| 1050 | _LIBCPP_ASSERT(st.permissions() != perms::unknown, |
| 1051 | "Permissions unexpectedly unknown"); |
| 1052 | if (add_perms) |
| 1053 | prms |= st.permissions(); |
| 1054 | else if (remove_perms) |
| 1055 | prms = st.permissions() & ~prms; |
| 1056 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1057 | const auto real_perms = detail::posix_convert_perms(prms); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1058 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1059 | #if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_FDCWD) |
| 1060 | const int flags = set_sym_perms ? AT_SYMLINK_NOFOLLOW : 0; |
| 1061 | if (::fchmodat(AT_FDCWD, p.c_str(), real_perms, flags) == -1) { |
| 1062 | return err.report(capture_errno()); |
| 1063 | } |
| 1064 | #else |
| 1065 | if (set_sym_perms) |
| 1066 | return err.report(errc::operation_not_supported); |
| 1067 | if (::chmod(p.c_str(), real_perms) == -1) { |
| 1068 | return err.report(capture_errno()); |
| 1069 | } |
| 1070 | #endif |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1073 | path __read_symlink(const path& p, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1074 | ErrorHandler<path> err("read_symlink", ec, &p); |
| 1075 | |
Eric Fiselier | b521530 | 2019-01-17 02:59:28 +0000 | [diff] [blame] | 1076 | #ifdef PATH_MAX |
| 1077 | struct NullDeleter { void operator()(void*) const {} }; |
| 1078 | const size_t size = PATH_MAX + 1; |
| 1079 | char stack_buff[size]; |
| 1080 | auto buff = std::unique_ptr<char[], NullDeleter>(stack_buff); |
| 1081 | #else |
| 1082 | StatT sb; |
| 1083 | if (::lstat(p.c_str(), &sb) == -1) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1084 | return err.report(capture_errno()); |
| 1085 | } |
Eric Fiselier | b521530 | 2019-01-17 02:59:28 +0000 | [diff] [blame] | 1086 | const size_t size = sb.st_size + 1; |
| 1087 | auto buff = unique_ptr<char[]>(new char[size]); |
| 1088 | #endif |
| 1089 | ::ssize_t ret; |
| 1090 | if ((ret = ::readlink(p.c_str(), buff.get(), size)) == -1) |
| 1091 | return err.report(capture_errno()); |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1092 | _LIBCPP_ASSERT(ret > 0, "TODO"); |
Eric Fiselier | b521530 | 2019-01-17 02:59:28 +0000 | [diff] [blame] | 1093 | if (static_cast<size_t>(ret) >= size) |
| 1094 | return err.report(errc::value_too_large); |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1095 | buff[ret] = 0; |
Eric Fiselier | b521530 | 2019-01-17 02:59:28 +0000 | [diff] [blame] | 1096 | return {buff.get()}; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1099 | bool __remove(const path& p, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1100 | ErrorHandler<bool> err("remove", ec, &p); |
| 1101 | if (::remove(p.c_str()) == -1) { |
| 1102 | if (errno != ENOENT) |
| 1103 | err.report(capture_errno()); |
| 1104 | return false; |
| 1105 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1106 | return true; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | namespace { |
| 1110 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1111 | uintmax_t remove_all_impl(path const& p, error_code& ec) { |
| 1112 | const auto npos = static_cast<uintmax_t>(-1); |
| 1113 | const file_status st = __symlink_status(p, &ec); |
| 1114 | if (ec) |
| 1115 | return npos; |
| 1116 | uintmax_t count = 1; |
| 1117 | if (is_directory(st)) { |
| 1118 | for (directory_iterator it(p, ec); !ec && it != directory_iterator(); |
| 1119 | it.increment(ec)) { |
| 1120 | auto other_count = remove_all_impl(it->path(), ec); |
| 1121 | if (ec) |
| 1122 | return npos; |
| 1123 | count += other_count; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1124 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1125 | if (ec) |
| 1126 | return npos; |
| 1127 | } |
| 1128 | if (!__remove(p, &ec)) |
| 1129 | return npos; |
| 1130 | return count; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | } // end namespace |
| 1134 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1135 | uintmax_t __remove_all(const path& p, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1136 | ErrorHandler<uintmax_t> err("remove_all", ec, &p); |
Ekaterina Vaartis | 52668f7 | 2018-01-11 17:04:29 +0000 | [diff] [blame] | 1137 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 1138 | error_code mec; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1139 | auto count = remove_all_impl(p, mec); |
| 1140 | if (mec) { |
| 1141 | if (mec == errc::no_such_file_or_directory) |
| 1142 | return 0; |
| 1143 | return err.report(mec); |
| 1144 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1145 | return count; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1148 | void __rename(const path& from, const path& to, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1149 | ErrorHandler<void> err("rename", ec, &from, &to); |
| 1150 | if (::rename(from.c_str(), to.c_str()) == -1) |
| 1151 | err.report(capture_errno()); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1154 | void __resize_file(const path& p, uintmax_t size, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1155 | ErrorHandler<void> err("resize_file", ec, &p); |
| 1156 | if (::truncate(p.c_str(), static_cast< ::off_t>(size)) == -1) |
| 1157 | return err.report(capture_errno()); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1160 | space_info __space(const path& p, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1161 | ErrorHandler<void> err("space", ec, &p); |
| 1162 | space_info si; |
| 1163 | struct statvfs m_svfs = {}; |
| 1164 | if (::statvfs(p.c_str(), &m_svfs) == -1) { |
| 1165 | err.report(capture_errno()); |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 1166 | si.capacity = si.free = si.available = static_cast<uintmax_t>(-1); |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1167 | return si; |
| 1168 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1169 | // Multiply with overflow checking. |
| 1170 | auto do_mult = [&](uintmax_t& out, uintmax_t other) { |
| 1171 | out = other * m_svfs.f_frsize; |
| 1172 | if (other == 0 || out / other != m_svfs.f_frsize) |
| 1173 | out = static_cast<uintmax_t>(-1); |
| 1174 | }; |
| 1175 | do_mult(si.capacity, m_svfs.f_blocks); |
| 1176 | do_mult(si.free, m_svfs.f_bfree); |
| 1177 | do_mult(si.available, m_svfs.f_bavail); |
| 1178 | return si; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1181 | file_status __status(const path& p, error_code* ec) { |
| 1182 | return detail::posix_stat(p, ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1185 | file_status __symlink_status(const path& p, error_code* ec) { |
| 1186 | return detail::posix_lstat(p, ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 1189 | path __temp_directory_path(error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1190 | ErrorHandler<path> err("temp_directory_path", ec); |
| 1191 | |
Saleem Abdulrasool | cf279a5 | 2017-02-05 17:21:52 +0000 | [diff] [blame] | 1192 | const char* env_paths[] = {"TMPDIR", "TMP", "TEMP", "TEMPDIR"}; |
| 1193 | const char* ret = nullptr; |
| 1194 | |
| 1195 | for (auto& ep : env_paths) |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 1196 | if ((ret = getenv(ep))) |
Saleem Abdulrasool | cf279a5 | 2017-02-05 17:21:52 +0000 | [diff] [blame] | 1197 | break; |
| 1198 | if (ret == nullptr) |
| 1199 | ret = "/tmp"; |
| 1200 | |
| 1201 | path p(ret); |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 1202 | error_code m_ec; |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1203 | file_status st = detail::posix_stat(p, &m_ec); |
| 1204 | if (!status_known(st)) |
| 1205 | return err.report(m_ec, "cannot access path \"%s\"", p); |
Saleem Abdulrasool | cf279a5 | 2017-02-05 17:21:52 +0000 | [diff] [blame] | 1206 | |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1207 | if (!exists(st) || !is_directory(st)) |
| 1208 | return err.report(errc::not_a_directory, "path \"%s\" is not a directory", |
| 1209 | p); |
| 1210 | |
Saleem Abdulrasool | cf279a5 | 2017-02-05 17:21:52 +0000 | [diff] [blame] | 1211 | return p; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1214 | path __weakly_canonical(const path& p, error_code* ec) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1215 | ErrorHandler<path> err("weakly_canonical", ec, &p); |
| 1216 | |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1217 | if (p.empty()) |
| 1218 | return __canonical("", ec); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1219 | |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1220 | path result; |
| 1221 | path tmp; |
| 1222 | tmp.__reserve(p.native().size()); |
| 1223 | auto PP = PathParser::CreateEnd(p.native()); |
| 1224 | --PP; |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 1225 | vector<string_view_t> DNEParts; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1226 | |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1227 | while (PP.State != PathParser::PS_BeforeBegin) { |
| 1228 | tmp.assign(createView(p.native().data(), &PP.RawEntry.back())); |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 1229 | error_code m_ec; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1230 | file_status st = __status(tmp, &m_ec); |
| 1231 | if (!status_known(st)) { |
Eric Fiselier | a75bbde | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 1232 | return err.report(m_ec); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1233 | } else if (exists(st)) { |
| 1234 | result = __canonical(tmp, ec); |
| 1235 | break; |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1236 | } |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1237 | DNEParts.push_back(*PP); |
| 1238 | --PP; |
| 1239 | } |
| 1240 | if (PP.State == PathParser::PS_BeforeBegin) |
| 1241 | result = __canonical("", ec); |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1242 | if (ec) |
| 1243 | ec->clear(); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1244 | if (DNEParts.empty()) |
| 1245 | return result; |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1246 | for (auto It = DNEParts.rbegin(); It != DNEParts.rend(); ++It) |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1247 | result /= *It; |
| 1248 | return result.lexically_normal(); |
Eric Fiselier | 435db15 | 2016-06-17 19:46:40 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1251 | /////////////////////////////////////////////////////////////////////////////// |
| 1252 | // path definitions |
| 1253 | /////////////////////////////////////////////////////////////////////////////// |
| 1254 | |
| 1255 | constexpr path::value_type path::preferred_separator; |
| 1256 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1257 | path& path::replace_extension(path const& replacement) { |
| 1258 | path p = extension(); |
| 1259 | if (not p.empty()) { |
| 1260 | __pn_.erase(__pn_.size() - p.native().size()); |
| 1261 | } |
| 1262 | if (!replacement.empty()) { |
| 1263 | if (replacement.native()[0] != '.') { |
| 1264 | __pn_ += "."; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1265 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1266 | __pn_.append(replacement.__pn_); |
| 1267 | } |
| 1268 | return *this; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | /////////////////////////////////////////////////////////////////////////////// |
| 1272 | // path.decompose |
| 1273 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1274 | string_view_t path::__root_name() const { |
| 1275 | auto PP = PathParser::CreateBegin(__pn_); |
| 1276 | if (PP.State == PathParser::PS_InRootName) |
| 1277 | return *PP; |
| 1278 | return {}; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1281 | string_view_t path::__root_directory() const { |
| 1282 | auto PP = PathParser::CreateBegin(__pn_); |
| 1283 | if (PP.State == PathParser::PS_InRootName) |
| 1284 | ++PP; |
| 1285 | if (PP.State == PathParser::PS_InRootDir) |
| 1286 | return *PP; |
| 1287 | return {}; |
| 1288 | } |
| 1289 | |
| 1290 | string_view_t path::__root_path_raw() const { |
| 1291 | auto PP = PathParser::CreateBegin(__pn_); |
| 1292 | if (PP.State == PathParser::PS_InRootName) { |
| 1293 | auto NextCh = PP.peek(); |
| 1294 | if (NextCh && *NextCh == '/') { |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1295 | ++PP; |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1296 | return createView(__pn_.data(), &PP.RawEntry.back()); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1297 | } |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1298 | return PP.RawEntry; |
| 1299 | } |
| 1300 | if (PP.State == PathParser::PS_InRootDir) |
| 1301 | return *PP; |
| 1302 | return {}; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
Eric Fiselier | c9a770e | 2018-12-21 03:16:30 +0000 | [diff] [blame] | 1305 | static bool ConsumeRootName(PathParser *PP) { |
| 1306 | static_assert(PathParser::PS_BeforeBegin == 1 && |
| 1307 | PathParser::PS_InRootName == 2, |
| 1308 | "Values for enums are incorrect"); |
| 1309 | while (PP->State <= PathParser::PS_InRootName) |
| 1310 | ++(*PP); |
| 1311 | return PP->State == PathParser::PS_AtEnd; |
| 1312 | } |
| 1313 | |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1314 | static bool ConsumeRootDir(PathParser* PP) { |
Eric Fiselier | c9a770e | 2018-12-21 03:16:30 +0000 | [diff] [blame] | 1315 | static_assert(PathParser::PS_BeforeBegin == 1 && |
| 1316 | PathParser::PS_InRootName == 2 && |
| 1317 | PathParser::PS_InRootDir == 3, "Values for enums are incorrect"); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1318 | while (PP->State <= PathParser::PS_InRootDir) |
| 1319 | ++(*PP); |
| 1320 | return PP->State == PathParser::PS_AtEnd; |
| 1321 | } |
| 1322 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1323 | string_view_t path::__relative_path() const { |
| 1324 | auto PP = PathParser::CreateBegin(__pn_); |
| 1325 | if (ConsumeRootDir(&PP)) |
| 1326 | return {}; |
| 1327 | return createView(PP.RawEntry.data(), &__pn_.back()); |
| 1328 | } |
| 1329 | |
| 1330 | string_view_t path::__parent_path() const { |
| 1331 | if (empty()) |
| 1332 | return {}; |
| 1333 | // Determine if we have a root path but not a relative path. In that case |
| 1334 | // return *this. |
| 1335 | { |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1336 | auto PP = PathParser::CreateBegin(__pn_); |
| 1337 | if (ConsumeRootDir(&PP)) |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1338 | return __pn_; |
| 1339 | } |
| 1340 | // Otherwise remove a single element from the end of the path, and return |
| 1341 | // a string representing that path |
| 1342 | { |
| 1343 | auto PP = PathParser::CreateEnd(__pn_); |
| 1344 | --PP; |
| 1345 | if (PP.RawEntry.data() == __pn_.data()) |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1346 | return {}; |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1347 | --PP; |
| 1348 | return createView(__pn_.data(), &PP.RawEntry.back()); |
| 1349 | } |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1352 | string_view_t path::__filename() const { |
| 1353 | if (empty()) |
| 1354 | return {}; |
| 1355 | { |
| 1356 | PathParser PP = PathParser::CreateBegin(__pn_); |
| 1357 | if (ConsumeRootDir(&PP)) |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1358 | return {}; |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1359 | } |
| 1360 | return *(--PathParser::CreateEnd(__pn_)); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1363 | string_view_t path::__stem() const { |
| 1364 | return parser::separate_filename(__filename()).first; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1367 | string_view_t path::__extension() const { |
| 1368 | return parser::separate_filename(__filename()).second; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | //////////////////////////////////////////////////////////////////////////// |
| 1372 | // path.gen |
| 1373 | |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1374 | enum PathPartKind : unsigned char { |
| 1375 | PK_None, |
| 1376 | PK_RootSep, |
| 1377 | PK_Filename, |
| 1378 | PK_Dot, |
| 1379 | PK_DotDot, |
| 1380 | PK_TrailingSep |
| 1381 | }; |
| 1382 | |
| 1383 | static PathPartKind ClassifyPathPart(string_view_t Part) { |
| 1384 | if (Part.empty()) |
| 1385 | return PK_TrailingSep; |
| 1386 | if (Part == ".") |
| 1387 | return PK_Dot; |
| 1388 | if (Part == "..") |
| 1389 | return PK_DotDot; |
| 1390 | if (Part == "/") |
| 1391 | return PK_RootSep; |
| 1392 | return PK_Filename; |
| 1393 | } |
| 1394 | |
| 1395 | path path::lexically_normal() const { |
| 1396 | if (__pn_.empty()) |
| 1397 | return *this; |
| 1398 | |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 1399 | using PartKindPair = pair<string_view_t, PathPartKind>; |
| 1400 | vector<PartKindPair> Parts; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1401 | // Guess as to how many elements the path has to avoid reallocating. |
| 1402 | Parts.reserve(32); |
| 1403 | |
| 1404 | // Track the total size of the parts as we collect them. This allows the |
| 1405 | // resulting path to reserve the correct amount of memory. |
| 1406 | size_t NewPathSize = 0; |
| 1407 | auto AddPart = [&](PathPartKind K, string_view_t P) { |
| 1408 | NewPathSize += P.size(); |
| 1409 | Parts.emplace_back(P, K); |
| 1410 | }; |
| 1411 | auto LastPartKind = [&]() { |
| 1412 | if (Parts.empty()) |
| 1413 | return PK_None; |
| 1414 | return Parts.back().second; |
| 1415 | }; |
| 1416 | |
| 1417 | bool MaybeNeedTrailingSep = false; |
| 1418 | // Build a stack containing the remaining elements of the path, popping off |
| 1419 | // elements which occur before a '..' entry. |
| 1420 | for (auto PP = PathParser::CreateBegin(__pn_); PP; ++PP) { |
| 1421 | auto Part = *PP; |
| 1422 | PathPartKind Kind = ClassifyPathPart(Part); |
| 1423 | switch (Kind) { |
| 1424 | case PK_Filename: |
| 1425 | case PK_RootSep: { |
| 1426 | // Add all non-dot and non-dot-dot elements to the stack of elements. |
| 1427 | AddPart(Kind, Part); |
| 1428 | MaybeNeedTrailingSep = false; |
| 1429 | break; |
| 1430 | } |
| 1431 | case PK_DotDot: { |
| 1432 | // Only push a ".." element if there are no elements preceding the "..", |
| 1433 | // or if the preceding element is itself "..". |
| 1434 | auto LastKind = LastPartKind(); |
| 1435 | if (LastKind == PK_Filename) { |
| 1436 | NewPathSize -= Parts.back().first.size(); |
| 1437 | Parts.pop_back(); |
| 1438 | } else if (LastKind != PK_RootSep) |
| 1439 | AddPart(PK_DotDot, ".."); |
| 1440 | MaybeNeedTrailingSep = LastKind == PK_Filename; |
| 1441 | break; |
| 1442 | } |
| 1443 | case PK_Dot: |
| 1444 | case PK_TrailingSep: { |
| 1445 | MaybeNeedTrailingSep = true; |
| 1446 | break; |
| 1447 | } |
| 1448 | case PK_None: |
| 1449 | _LIBCPP_UNREACHABLE(); |
| 1450 | } |
| 1451 | } |
| 1452 | // [fs.path.generic]p6.8: If the path is empty, add a dot. |
| 1453 | if (Parts.empty()) |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1454 | return "."; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1455 | |
| 1456 | // [fs.path.generic]p6.7: If the last filename is dot-dot, remove any |
| 1457 | // trailing directory-separator. |
| 1458 | bool NeedTrailingSep = MaybeNeedTrailingSep && LastPartKind() == PK_Filename; |
| 1459 | |
| 1460 | path Result; |
| 1461 | Result.__pn_.reserve(Parts.size() + NewPathSize + NeedTrailingSep); |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1462 | for (auto& PK : Parts) |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1463 | Result /= PK.first; |
| 1464 | |
| 1465 | if (NeedTrailingSep) |
| 1466 | Result /= ""; |
| 1467 | |
| 1468 | return Result; |
| 1469 | } |
| 1470 | |
| 1471 | static int DetermineLexicalElementCount(PathParser PP) { |
| 1472 | int Count = 0; |
| 1473 | for (; PP; ++PP) { |
| 1474 | auto Elem = *PP; |
| 1475 | if (Elem == "..") |
| 1476 | --Count; |
Eric Fiselier | 9c4949a | 2018-12-21 04:25:40 +0000 | [diff] [blame] | 1477 | else if (Elem != "." && Elem != "") |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1478 | ++Count; |
| 1479 | } |
| 1480 | return Count; |
| 1481 | } |
| 1482 | |
| 1483 | path path::lexically_relative(const path& base) const { |
| 1484 | { // perform root-name/root-directory mismatch checks |
| 1485 | auto PP = PathParser::CreateBegin(__pn_); |
| 1486 | auto PPBase = PathParser::CreateBegin(base.__pn_); |
| 1487 | auto CheckIterMismatchAtBase = [&]() { |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1488 | return PP.State != PPBase.State && |
| 1489 | (PP.inRootPath() || PPBase.inRootPath()); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1490 | }; |
Eric Fiselier | 9c4949a | 2018-12-21 04:25:40 +0000 | [diff] [blame] | 1491 | if (PP.inRootName() && PPBase.inRootName()) { |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1492 | if (*PP != *PPBase) |
| 1493 | return {}; |
| 1494 | } else if (CheckIterMismatchAtBase()) |
| 1495 | return {}; |
| 1496 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1497 | if (PP.inRootPath()) |
| 1498 | ++PP; |
| 1499 | if (PPBase.inRootPath()) |
| 1500 | ++PPBase; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1501 | if (CheckIterMismatchAtBase()) |
| 1502 | return {}; |
| 1503 | } |
| 1504 | |
| 1505 | // Find the first mismatching element |
| 1506 | auto PP = PathParser::CreateBegin(__pn_); |
| 1507 | auto PPBase = PathParser::CreateBegin(base.__pn_); |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1508 | while (PP && PPBase && PP.State == PPBase.State && *PP == *PPBase) { |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1509 | ++PP; |
| 1510 | ++PPBase; |
| 1511 | } |
| 1512 | |
| 1513 | // If there is no mismatch, return ".". |
| 1514 | if (!PP && !PPBase) |
| 1515 | return "."; |
| 1516 | |
| 1517 | // Otherwise, determine the number of elements, 'n', which are not dot or |
| 1518 | // dot-dot minus the number of dot-dot elements. |
| 1519 | int ElemCount = DetermineLexicalElementCount(PPBase); |
| 1520 | if (ElemCount < 0) |
| 1521 | return {}; |
| 1522 | |
Eric Fiselier | 9c4949a | 2018-12-21 04:25:40 +0000 | [diff] [blame] | 1523 | // if n == 0 and (a == end() || a->empty()), returns path("."); otherwise |
| 1524 | if (ElemCount == 0 && (PP.atEnd() || *PP == "")) |
| 1525 | return "."; |
| 1526 | |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1527 | // return a path constructed with 'n' dot-dot elements, followed by the the |
| 1528 | // elements of '*this' after the mismatch. |
| 1529 | path Result; |
| 1530 | // FIXME: Reserve enough room in Result that it won't have to re-allocate. |
| 1531 | while (ElemCount--) |
| 1532 | Result /= ".."; |
| 1533 | for (; PP; ++PP) |
| 1534 | Result /= *PP; |
| 1535 | return Result; |
| 1536 | } |
| 1537 | |
| 1538 | //////////////////////////////////////////////////////////////////////////// |
| 1539 | // path.comparisons |
Eric Fiselier | c9a770e | 2018-12-21 03:16:30 +0000 | [diff] [blame] | 1540 | static int CompareRootName(PathParser *LHS, PathParser *RHS) { |
| 1541 | if (!LHS->inRootName() && !RHS->inRootName()) |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1542 | return 0; |
Eric Fiselier | c9a770e | 2018-12-21 03:16:30 +0000 | [diff] [blame] | 1543 | |
| 1544 | auto GetRootName = [](PathParser *Parser) -> string_view_t { |
| 1545 | return Parser->inRootName() ? **Parser : ""; |
| 1546 | }; |
| 1547 | int res = GetRootName(LHS).compare(GetRootName(RHS)); |
| 1548 | ConsumeRootName(LHS); |
| 1549 | ConsumeRootName(RHS); |
| 1550 | return res; |
| 1551 | } |
| 1552 | |
| 1553 | static int CompareRootDir(PathParser *LHS, PathParser *RHS) { |
| 1554 | if (!LHS->inRootDir() && RHS->inRootDir()) |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1555 | return -1; |
Eric Fiselier | c9a770e | 2018-12-21 03:16:30 +0000 | [diff] [blame] | 1556 | else if (LHS->inRootDir() && !RHS->inRootDir()) |
| 1557 | return 1; |
| 1558 | else { |
| 1559 | ConsumeRootDir(LHS); |
| 1560 | ConsumeRootDir(RHS); |
| 1561 | return 0; |
| 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | static int CompareRelative(PathParser *LHSPtr, PathParser *RHSPtr) { |
| 1566 | auto &LHS = *LHSPtr; |
| 1567 | auto &RHS = *RHSPtr; |
Stephan T. Lavavej | fb39ad7 | 2019-10-23 11:45:36 -0700 | [diff] [blame] | 1568 | |
Eric Fiselier | c9a770e | 2018-12-21 03:16:30 +0000 | [diff] [blame] | 1569 | int res; |
| 1570 | while (LHS && RHS) { |
| 1571 | if ((res = (*LHS).compare(*RHS)) != 0) |
| 1572 | return res; |
| 1573 | ++LHS; |
| 1574 | ++RHS; |
| 1575 | } |
| 1576 | return 0; |
| 1577 | } |
| 1578 | |
| 1579 | static int CompareEndState(PathParser *LHS, PathParser *RHS) { |
| 1580 | if (LHS->atEnd() && !RHS->atEnd()) |
| 1581 | return -1; |
| 1582 | else if (!LHS->atEnd() && RHS->atEnd()) |
| 1583 | return 1; |
| 1584 | return 0; |
| 1585 | } |
| 1586 | |
| 1587 | int path::__compare(string_view_t __s) const { |
| 1588 | auto LHS = PathParser::CreateBegin(__pn_); |
| 1589 | auto RHS = PathParser::CreateBegin(__s); |
| 1590 | int res; |
| 1591 | |
| 1592 | if ((res = CompareRootName(&LHS, &RHS)) != 0) |
| 1593 | return res; |
| 1594 | |
| 1595 | if ((res = CompareRootDir(&LHS, &RHS)) != 0) |
| 1596 | return res; |
| 1597 | |
| 1598 | if ((res = CompareRelative(&LHS, &RHS)) != 0) |
| 1599 | return res; |
| 1600 | |
| 1601 | return CompareEndState(&LHS, &RHS); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | //////////////////////////////////////////////////////////////////////////// |
| 1605 | // path.nonmembers |
| 1606 | size_t hash_value(const path& __p) noexcept { |
| 1607 | auto PP = PathParser::CreateBegin(__p.native()); |
| 1608 | size_t hash_value = 0; |
Eric Fiselier | d6c49a3 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 1609 | hash<string_view_t> hasher; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1610 | while (PP) { |
| 1611 | hash_value = __hash_combine(hash_value, hasher(*PP)); |
| 1612 | ++PP; |
| 1613 | } |
| 1614 | return hash_value; |
| 1615 | } |
| 1616 | |
| 1617 | //////////////////////////////////////////////////////////////////////////// |
| 1618 | // path.itr |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1619 | path::iterator path::begin() const { |
| 1620 | auto PP = PathParser::CreateBegin(__pn_); |
| 1621 | iterator it; |
| 1622 | it.__path_ptr_ = this; |
| 1623 | it.__state_ = static_cast<path::iterator::_ParserState>(PP.State); |
| 1624 | it.__entry_ = PP.RawEntry; |
| 1625 | it.__stashed_elem_.__assign_view(*PP); |
| 1626 | return it; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1629 | path::iterator path::end() const { |
| 1630 | iterator it{}; |
| 1631 | it.__state_ = path::iterator::_AtEnd; |
| 1632 | it.__path_ptr_ = this; |
| 1633 | return it; |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | path::iterator& path::iterator::__increment() { |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1637 | PathParser PP(__path_ptr_->native(), __entry_, __state_); |
| 1638 | ++PP; |
Eric Fiselier | 23a120c | 2018-07-25 03:31:48 +0000 | [diff] [blame] | 1639 | __state_ = static_cast<_ParserState>(PP.State); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1640 | __entry_ = PP.RawEntry; |
| 1641 | __stashed_elem_.__assign_view(*PP); |
| 1642 | return *this; |
| 1643 | } |
| 1644 | |
| 1645 | path::iterator& path::iterator::__decrement() { |
| 1646 | PathParser PP(__path_ptr_->native(), __entry_, __state_); |
| 1647 | --PP; |
Eric Fiselier | 23a120c | 2018-07-25 03:31:48 +0000 | [diff] [blame] | 1648 | __state_ = static_cast<_ParserState>(PP.State); |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1649 | __entry_ = PP.RawEntry; |
| 1650 | __stashed_elem_.__assign_view(*PP); |
| 1651 | return *this; |
| 1652 | } |
| 1653 | |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 1654 | /////////////////////////////////////////////////////////////////////////////// |
| 1655 | // directory entry definitions |
| 1656 | /////////////////////////////////////////////////////////////////////////////// |
| 1657 | |
| 1658 | #ifndef _LIBCPP_WIN32API |
| 1659 | error_code directory_entry::__do_refresh() noexcept { |
| 1660 | __data_.__reset(); |
| 1661 | error_code failure_ec; |
| 1662 | |
Eric Fiselier | 7eba47e | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 1663 | StatT full_st; |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 1664 | file_status st = detail::posix_lstat(__p_, full_st, &failure_ec); |
| 1665 | if (!status_known(st)) { |
| 1666 | __data_.__reset(); |
| 1667 | return failure_ec; |
| 1668 | } |
| 1669 | |
| 1670 | if (!_VSTD_FS::exists(st) || !_VSTD_FS::is_symlink(st)) { |
| 1671 | __data_.__cache_type_ = directory_entry::_RefreshNonSymlink; |
| 1672 | __data_.__type_ = st.type(); |
| 1673 | __data_.__non_sym_perms_ = st.permissions(); |
| 1674 | } else { // we have a symlink |
| 1675 | __data_.__sym_perms_ = st.permissions(); |
| 1676 | // Get the information about the linked entity. |
| 1677 | // Ignore errors from stat, since we don't want errors regarding symlink |
| 1678 | // resolution to be reported to the user. |
| 1679 | error_code ignored_ec; |
| 1680 | st = detail::posix_stat(__p_, full_st, &ignored_ec); |
| 1681 | |
| 1682 | __data_.__type_ = st.type(); |
| 1683 | __data_.__non_sym_perms_ = st.permissions(); |
| 1684 | |
| 1685 | // If we failed to resolve the link, then only partially populate the |
| 1686 | // cache. |
| 1687 | if (!status_known(st)) { |
| 1688 | __data_.__cache_type_ = directory_entry::_RefreshSymlinkUnresolved; |
| 1689 | return error_code{}; |
| 1690 | } |
Eric Fiselier | abfdbdf | 2018-07-22 02:00:53 +0000 | [diff] [blame] | 1691 | // Otherwise, we resolved the link, potentially as not existing. |
Eric Fiselier | e39cea9 | 2018-07-20 08:36:45 +0000 | [diff] [blame] | 1692 | // That's OK. |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 1693 | __data_.__cache_type_ = directory_entry::_RefreshSymlink; |
| 1694 | } |
| 1695 | |
| 1696 | if (_VSTD_FS::is_regular_file(st)) |
| 1697 | __data_.__size_ = static_cast<uintmax_t>(full_st.st_size); |
| 1698 | |
| 1699 | if (_VSTD_FS::exists(st)) { |
| 1700 | __data_.__nlink_ = static_cast<uintmax_t>(full_st.st_nlink); |
| 1701 | |
| 1702 | // Attempt to extract the mtime, and fail if it's not representable using |
| 1703 | // file_time_type. For now we ignore the error, as we'll report it when |
| 1704 | // the value is actually used. |
| 1705 | error_code ignored_ec; |
| 1706 | __data_.__write_time_ = |
| 1707 | __extract_last_write_time(__p_, full_st, &ignored_ec); |
| 1708 | } |
| 1709 | |
| 1710 | return failure_ec; |
| 1711 | } |
| 1712 | #else |
| 1713 | error_code directory_entry::__do_refresh() noexcept { |
| 1714 | __data_.__reset(); |
| 1715 | error_code failure_ec; |
| 1716 | |
| 1717 | file_status st = _VSTD_FS::symlink_status(__p_, failure_ec); |
| 1718 | if (!status_known(st)) { |
| 1719 | __data_.__reset(); |
| 1720 | return failure_ec; |
| 1721 | } |
| 1722 | |
| 1723 | if (!_VSTD_FS::exists(st) || !_VSTD_FS::is_symlink(st)) { |
| 1724 | __data_.__cache_type_ = directory_entry::_RefreshNonSymlink; |
| 1725 | __data_.__type_ = st.type(); |
| 1726 | __data_.__non_sym_perms_ = st.permissions(); |
| 1727 | } else { // we have a symlink |
| 1728 | __data_.__sym_perms_ = st.permissions(); |
| 1729 | // Get the information about the linked entity. |
| 1730 | // Ignore errors from stat, since we don't want errors regarding symlink |
| 1731 | // resolution to be reported to the user. |
| 1732 | error_code ignored_ec; |
| 1733 | st = _VSTD_FS::status(__p_, ignored_ec); |
| 1734 | |
| 1735 | __data_.__type_ = st.type(); |
| 1736 | __data_.__non_sym_perms_ = st.permissions(); |
| 1737 | |
| 1738 | // If we failed to resolve the link, then only partially populate the |
| 1739 | // cache. |
| 1740 | if (!status_known(st)) { |
| 1741 | __data_.__cache_type_ = directory_entry::_RefreshSymlinkUnresolved; |
| 1742 | return error_code{}; |
| 1743 | } |
Eric Fiselier | 7047408 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 1744 | __data_.__cache_type_ = directory_entry::_RefreshSymlink; |
| 1745 | } |
| 1746 | |
| 1747 | // FIXME: This is currently broken, and the implementation only a placeholder. |
| 1748 | // We need to cache last_write_time, file_size, and hard_link_count here before |
| 1749 | // the implementation actually works. |
| 1750 | |
| 1751 | return failure_ec; |
| 1752 | } |
| 1753 | #endif |
Eric Fiselier | 91a182b | 2018-04-02 23:03:41 +0000 | [diff] [blame] | 1754 | |
Eric Fiselier | 02cea5e | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 1755 | _LIBCPP_END_NAMESPACE_FILESYSTEM |