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