Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===//// |
| 2 | // |
Chandler Carruth | 57b08b0 | 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 | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===//// |
| 8 | |
| 9 | #ifndef FILESYSTEM_COMMON_H |
| 10 | #define FILESYSTEM_COMMON_H |
| 11 | |
Eric Fiselier | 998a5c8 | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 12 | #include "__config" |
| 13 | #include "filesystem" |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 14 | #include "array" |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 15 | #include "chrono" |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 16 | #include "climits" |
Louis Dionne | c479e0c | 2020-10-30 11:19:07 -0400 | [diff] [blame] | 17 | #include "cstdlib" |
| 18 | #include "ctime" |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 19 | |
| 20 | #include <unistd.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <sys/statvfs.h> |
Eric Fiselier | a0a7c1f | 2018-07-26 03:57:26 +0000 | [diff] [blame] | 23 | #include <sys/time.h> // for ::utimes as used in __last_write_time |
Eric Fiselier | 998a5c8 | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 24 | #include <fcntl.h> /* values for fchmodat */ |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 25 | |
Eric Fiselier | 998a5c8 | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 26 | #include "../include/apple_availability.h" |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 27 | |
| 28 | #if !defined(__APPLE__) |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 29 | // We can use the presence of UTIME_OMIT to detect platforms that provide |
| 30 | // utimensat. |
| 31 | #if defined(UTIME_OMIT) |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 32 | #define _LIBCPP_USE_UTIMENSAT |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 33 | #endif |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 34 | #endif |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 35 | |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 36 | #if defined(__GNUC__) |
| 37 | #pragma GCC diagnostic push |
| 38 | #pragma GCC diagnostic ignored "-Wunused-function" |
| 39 | #endif |
| 40 | |
Eric Fiselier | 998a5c8 | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 41 | _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 42 | |
| 43 | namespace detail { |
| 44 | namespace { |
| 45 | |
Eric Fiselier | c48dba4 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 46 | static string format_string_imp(const char* msg, ...) { |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 47 | // we might need a second shot at this, so pre-emptivly make a copy |
| 48 | struct GuardVAList { |
| 49 | va_list& target; |
| 50 | bool active = true; |
Louis Dionne | c479e0c | 2020-10-30 11:19:07 -0400 | [diff] [blame] | 51 | GuardVAList(va_list& tgt) : target(tgt), active(true) {} |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 52 | void clear() { |
| 53 | if (active) |
| 54 | va_end(target); |
| 55 | active = false; |
| 56 | } |
| 57 | ~GuardVAList() { |
| 58 | if (active) |
| 59 | va_end(target); |
| 60 | } |
| 61 | }; |
| 62 | va_list args; |
| 63 | va_start(args, msg); |
Eric Fiselier | e3081d5 | 2018-07-23 03:06:57 +0000 | [diff] [blame] | 64 | GuardVAList args_guard(args); |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 65 | |
| 66 | va_list args_cp; |
| 67 | va_copy(args_cp, args); |
Eric Fiselier | e3081d5 | 2018-07-23 03:06:57 +0000 | [diff] [blame] | 68 | GuardVAList args_copy_guard(args_cp); |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 69 | |
Eric Fiselier | 01a87ef | 2018-11-26 20:15:38 +0000 | [diff] [blame] | 70 | std::string result; |
| 71 | |
Eric Fiselier | c48dba4 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 72 | array<char, 256> local_buff; |
Eric Fiselier | 01a87ef | 2018-11-26 20:15:38 +0000 | [diff] [blame] | 73 | size_t size_with_null = local_buff.size(); |
| 74 | auto ret = ::vsnprintf(local_buff.data(), size_with_null, msg, args_cp); |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 75 | |
| 76 | args_copy_guard.clear(); |
| 77 | |
| 78 | // handle empty expansion |
| 79 | if (ret == 0) |
Eric Fiselier | 01a87ef | 2018-11-26 20:15:38 +0000 | [diff] [blame] | 80 | return result; |
| 81 | if (static_cast<size_t>(ret) < size_with_null) { |
| 82 | result.assign(local_buff.data(), static_cast<size_t>(ret)); |
| 83 | return result; |
| 84 | } |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 85 | |
Eric Fiselier | 01a87ef | 2018-11-26 20:15:38 +0000 | [diff] [blame] | 86 | // we did not provide a long enough buffer on our first attempt. The |
| 87 | // return value is the number of bytes (excluding the null byte) that are |
| 88 | // needed for formatting. |
| 89 | size_with_null = static_cast<size_t>(ret) + 1; |
| 90 | result.__resize_default_init(size_with_null - 1); |
| 91 | ret = ::vsnprintf(&result[0], size_with_null, msg, args); |
| 92 | _LIBCPP_ASSERT(static_cast<size_t>(ret) == (size_with_null - 1), "TODO"); |
| 93 | |
| 94 | return result; |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | const char* unwrap(string const& s) { return s.c_str(); } |
| 98 | const char* unwrap(path const& p) { return p.native().c_str(); } |
| 99 | template <class Arg> |
| 100 | Arg const& unwrap(Arg const& a) { |
| 101 | static_assert(!is_class<Arg>::value, "cannot pass class here"); |
| 102 | return a; |
| 103 | } |
| 104 | |
| 105 | template <class... Args> |
Eric Fiselier | c48dba4 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 106 | string format_string(const char* fmt, Args const&... args) { |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 107 | return format_string_imp(fmt, unwrap(args)...); |
| 108 | } |
| 109 | |
Eric Fiselier | c48dba4 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 110 | error_code capture_errno() { |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 111 | _LIBCPP_ASSERT(errno, "Expected errno to be non-zero"); |
Eric Fiselier | c48dba4 | 2018-07-23 11:46:47 +0000 | [diff] [blame] | 112 | return error_code(errno, generic_category()); |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 115 | template <class T> |
| 116 | T error_value(); |
| 117 | template <> |
Eric Fiselier | e3081d5 | 2018-07-23 03:06:57 +0000 | [diff] [blame] | 118 | _LIBCPP_CONSTEXPR_AFTER_CXX11 void error_value<void>() {} |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 119 | template <> |
Eric Fiselier | d77f3ef | 2018-07-25 21:01:45 +0000 | [diff] [blame] | 120 | bool error_value<bool>() { |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 121 | return false; |
| 122 | } |
| 123 | template <> |
Eric Fiselier | d77f3ef | 2018-07-25 21:01:45 +0000 | [diff] [blame] | 124 | uintmax_t error_value<uintmax_t>() { |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 125 | return uintmax_t(-1); |
| 126 | } |
| 127 | template <> |
Eric Fiselier | e3081d5 | 2018-07-23 03:06:57 +0000 | [diff] [blame] | 128 | _LIBCPP_CONSTEXPR_AFTER_CXX11 file_time_type error_value<file_time_type>() { |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 129 | return file_time_type::min(); |
| 130 | } |
| 131 | template <> |
| 132 | path error_value<path>() { |
| 133 | return {}; |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 136 | template <class T> |
| 137 | struct ErrorHandler { |
Louis Dionne | c479e0c | 2020-10-30 11:19:07 -0400 | [diff] [blame] | 138 | const char* func_name_; |
| 139 | error_code* ec_ = nullptr; |
| 140 | const path* p1_ = nullptr; |
| 141 | const path* p2_ = nullptr; |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 142 | |
| 143 | ErrorHandler(const char* fname, error_code* ec, const path* p1 = nullptr, |
| 144 | const path* p2 = nullptr) |
Louis Dionne | c479e0c | 2020-10-30 11:19:07 -0400 | [diff] [blame] | 145 | : func_name_(fname), ec_(ec), p1_(p1), p2_(p2) { |
| 146 | if (ec_) |
| 147 | ec_->clear(); |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Louis Dionne | c479e0c | 2020-10-30 11:19:07 -0400 | [diff] [blame] | 150 | T report(const error_code& ec) const { |
| 151 | if (ec_) { |
| 152 | *ec_ = ec; |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 153 | return error_value<T>(); |
| 154 | } |
Louis Dionne | c479e0c | 2020-10-30 11:19:07 -0400 | [diff] [blame] | 155 | string what = string("in ") + func_name_; |
| 156 | switch (bool(p1_) + bool(p2_)) { |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 157 | case 0: |
Louis Dionne | c479e0c | 2020-10-30 11:19:07 -0400 | [diff] [blame] | 158 | __throw_filesystem_error(what, ec); |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 159 | case 1: |
Louis Dionne | c479e0c | 2020-10-30 11:19:07 -0400 | [diff] [blame] | 160 | __throw_filesystem_error(what, *p1_, ec); |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 161 | case 2: |
Louis Dionne | c479e0c | 2020-10-30 11:19:07 -0400 | [diff] [blame] | 162 | __throw_filesystem_error(what, *p1_, *p2_, ec); |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 163 | } |
| 164 | _LIBCPP_UNREACHABLE(); |
| 165 | } |
| 166 | |
| 167 | template <class... Args> |
Louis Dionne | c479e0c | 2020-10-30 11:19:07 -0400 | [diff] [blame] | 168 | T report(const error_code& ec, const char* msg, Args const&... args) const { |
| 169 | if (ec_) { |
| 170 | *ec_ = ec; |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 171 | return error_value<T>(); |
| 172 | } |
| 173 | string what = |
Louis Dionne | c479e0c | 2020-10-30 11:19:07 -0400 | [diff] [blame] | 174 | string("in ") + func_name_ + ": " + format_string(msg, args...); |
| 175 | switch (bool(p1_) + bool(p2_)) { |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 176 | case 0: |
Louis Dionne | c479e0c | 2020-10-30 11:19:07 -0400 | [diff] [blame] | 177 | __throw_filesystem_error(what, ec); |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 178 | case 1: |
Louis Dionne | c479e0c | 2020-10-30 11:19:07 -0400 | [diff] [blame] | 179 | __throw_filesystem_error(what, *p1_, ec); |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 180 | case 2: |
Louis Dionne | c479e0c | 2020-10-30 11:19:07 -0400 | [diff] [blame] | 181 | __throw_filesystem_error(what, *p1_, *p2_, ec); |
Eric Fiselier | 9158bfd | 2018-07-23 02:00:52 +0000 | [diff] [blame] | 182 | } |
| 183 | _LIBCPP_UNREACHABLE(); |
| 184 | } |
| 185 | |
| 186 | T report(errc const& err) const { return report(make_error_code(err)); } |
| 187 | |
| 188 | template <class... Args> |
| 189 | T report(errc const& err, const char* msg, Args const&... args) const { |
| 190 | return report(make_error_code(err), msg, args...); |
| 191 | } |
| 192 | |
| 193 | private: |
| 194 | ErrorHandler(ErrorHandler const&) = delete; |
| 195 | ErrorHandler& operator=(ErrorHandler const&) = delete; |
| 196 | }; |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 197 | |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 198 | using chrono::duration; |
| 199 | using chrono::duration_cast; |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 200 | |
Louis Dionne | 81b6aa0 | 2020-10-30 14:55:37 -0400 | [diff] [blame] | 201 | using TimeSpec = timespec; |
Louis Dionne | c479e0c | 2020-10-30 11:19:07 -0400 | [diff] [blame] | 202 | using StatT = struct stat; |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 203 | |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 204 | template <class FileTimeT, class TimeT, |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 205 | bool IsFloat = is_floating_point<typename FileTimeT::rep>::value> |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 206 | struct time_util_base { |
| 207 | using rep = typename FileTimeT::rep; |
| 208 | using fs_duration = typename FileTimeT::duration; |
| 209 | using fs_seconds = duration<rep>; |
| 210 | using fs_nanoseconds = duration<rep, nano>; |
| 211 | using fs_microseconds = duration<rep, micro>; |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 212 | |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 213 | static constexpr rep max_seconds = |
| 214 | duration_cast<fs_seconds>(FileTimeT::duration::max()).count(); |
| 215 | |
| 216 | static constexpr rep max_nsec = |
| 217 | duration_cast<fs_nanoseconds>(FileTimeT::duration::max() - |
| 218 | fs_seconds(max_seconds)) |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 219 | .count(); |
| 220 | |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 221 | static constexpr rep min_seconds = |
| 222 | duration_cast<fs_seconds>(FileTimeT::duration::min()).count(); |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 223 | |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 224 | static constexpr rep min_nsec_timespec = |
| 225 | duration_cast<fs_nanoseconds>( |
| 226 | (FileTimeT::duration::min() - fs_seconds(min_seconds)) + |
| 227 | fs_seconds(1)) |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 228 | .count(); |
| 229 | |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 230 | private: |
Eric Fiselier | b7e6c1d | 2018-07-25 22:07:36 +0000 | [diff] [blame] | 231 | #if _LIBCPP_STD_VER > 11 && !defined(_LIBCPP_HAS_NO_CXX14_CONSTEXPR) |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 232 | static constexpr fs_duration get_min_nsecs() { |
| 233 | return duration_cast<fs_duration>( |
| 234 | fs_nanoseconds(min_nsec_timespec) - |
| 235 | duration_cast<fs_nanoseconds>(fs_seconds(1))); |
| 236 | } |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 237 | // Static assert that these values properly round trip. |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 238 | static_assert(fs_seconds(min_seconds) + get_min_nsecs() == |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 239 | FileTimeT::duration::min(), |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 240 | "value doesn't roundtrip"); |
| 241 | |
| 242 | static constexpr bool check_range() { |
| 243 | // This kinda sucks, but it's what happens when we don't have __int128_t. |
| 244 | if (sizeof(TimeT) == sizeof(rep)) { |
| 245 | typedef duration<long long, ratio<3600 * 24 * 365> > Years; |
| 246 | return duration_cast<Years>(fs_seconds(max_seconds)) > Years(250) && |
| 247 | duration_cast<Years>(fs_seconds(min_seconds)) < Years(-250); |
| 248 | } |
| 249 | return max_seconds >= numeric_limits<TimeT>::max() && |
| 250 | min_seconds <= numeric_limits<TimeT>::min(); |
| 251 | } |
| 252 | static_assert(check_range(), "the representable range is unacceptable small"); |
| 253 | #endif |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 254 | }; |
| 255 | |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 256 | template <class FileTimeT, class TimeT> |
| 257 | struct time_util_base<FileTimeT, TimeT, true> { |
| 258 | using rep = typename FileTimeT::rep; |
| 259 | using fs_duration = typename FileTimeT::duration; |
| 260 | using fs_seconds = duration<rep>; |
| 261 | using fs_nanoseconds = duration<rep, nano>; |
| 262 | using fs_microseconds = duration<rep, micro>; |
| 263 | |
| 264 | static const rep max_seconds; |
| 265 | static const rep max_nsec; |
| 266 | static const rep min_seconds; |
| 267 | static const rep min_nsec_timespec; |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 268 | }; |
| 269 | |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 270 | template <class FileTimeT, class TimeT> |
| 271 | const typename FileTimeT::rep |
| 272 | time_util_base<FileTimeT, TimeT, true>::max_seconds = |
| 273 | duration_cast<fs_seconds>(FileTimeT::duration::max()).count(); |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 274 | |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 275 | template <class FileTimeT, class TimeT> |
| 276 | const typename FileTimeT::rep time_util_base<FileTimeT, TimeT, true>::max_nsec = |
| 277 | duration_cast<fs_nanoseconds>(FileTimeT::duration::max() - |
| 278 | fs_seconds(max_seconds)) |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 279 | .count(); |
| 280 | |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 281 | template <class FileTimeT, class TimeT> |
| 282 | const typename FileTimeT::rep |
| 283 | time_util_base<FileTimeT, TimeT, true>::min_seconds = |
| 284 | duration_cast<fs_seconds>(FileTimeT::duration::min()).count(); |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 285 | |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 286 | template <class FileTimeT, class TimeT> |
| 287 | const typename FileTimeT::rep |
| 288 | time_util_base<FileTimeT, TimeT, true>::min_nsec_timespec = |
| 289 | duration_cast<fs_nanoseconds>((FileTimeT::duration::min() - |
| 290 | fs_seconds(min_seconds)) + |
| 291 | fs_seconds(1)) |
| 292 | .count(); |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 293 | |
| 294 | template <class FileTimeT, class TimeT, class TimeSpecT> |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 295 | struct time_util : time_util_base<FileTimeT, TimeT> { |
| 296 | using Base = time_util_base<FileTimeT, TimeT>; |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 297 | using Base::max_nsec; |
| 298 | using Base::max_seconds; |
| 299 | using Base::min_nsec_timespec; |
| 300 | using Base::min_seconds; |
| 301 | |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 302 | using typename Base::fs_duration; |
| 303 | using typename Base::fs_microseconds; |
| 304 | using typename Base::fs_nanoseconds; |
| 305 | using typename Base::fs_seconds; |
| 306 | |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 307 | public: |
| 308 | template <class CType, class ChronoType> |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 309 | static _LIBCPP_CONSTEXPR_AFTER_CXX11 bool checked_set(CType* out, |
| 310 | ChronoType time) { |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 311 | using Lim = numeric_limits<CType>; |
| 312 | if (time > Lim::max() || time < Lim::min()) |
| 313 | return false; |
| 314 | *out = static_cast<CType>(time); |
| 315 | return true; |
| 316 | } |
| 317 | |
| 318 | static _LIBCPP_CONSTEXPR_AFTER_CXX11 bool is_representable(TimeSpecT tm) { |
| 319 | if (tm.tv_sec >= 0) { |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 320 | return tm.tv_sec < max_seconds || |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 321 | (tm.tv_sec == max_seconds && tm.tv_nsec <= max_nsec); |
| 322 | } else if (tm.tv_sec == (min_seconds - 1)) { |
| 323 | return tm.tv_nsec >= min_nsec_timespec; |
| 324 | } else { |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 325 | return tm.tv_sec >= min_seconds; |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| 329 | static _LIBCPP_CONSTEXPR_AFTER_CXX11 bool is_representable(FileTimeT tm) { |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 330 | auto secs = duration_cast<fs_seconds>(tm.time_since_epoch()); |
| 331 | auto nsecs = duration_cast<fs_nanoseconds>(tm.time_since_epoch() - secs); |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 332 | if (nsecs.count() < 0) { |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 333 | secs = secs + fs_seconds(1); |
| 334 | nsecs = nsecs + fs_seconds(1); |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 335 | } |
| 336 | using TLim = numeric_limits<TimeT>; |
| 337 | if (secs.count() >= 0) |
| 338 | return secs.count() <= TLim::max(); |
| 339 | return secs.count() >= TLim::min(); |
| 340 | } |
| 341 | |
| 342 | static _LIBCPP_CONSTEXPR_AFTER_CXX11 FileTimeT |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 343 | convert_from_timespec(TimeSpecT tm) { |
| 344 | if (tm.tv_sec >= 0 || tm.tv_nsec == 0) { |
| 345 | return FileTimeT(fs_seconds(tm.tv_sec) + |
| 346 | duration_cast<fs_duration>(fs_nanoseconds(tm.tv_nsec))); |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 347 | } else { // tm.tv_sec < 0 |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 348 | auto adj_subsec = duration_cast<fs_duration>(fs_seconds(1) - |
| 349 | fs_nanoseconds(tm.tv_nsec)); |
| 350 | auto Dur = fs_seconds(tm.tv_sec + 1) - adj_subsec; |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 351 | return FileTimeT(Dur); |
| 352 | } |
| 353 | } |
| 354 | |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 355 | template <class SubSecT> |
| 356 | static _LIBCPP_CONSTEXPR_AFTER_CXX11 bool |
| 357 | set_times_checked(TimeT* sec_out, SubSecT* subsec_out, FileTimeT tp) { |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 358 | auto dur = tp.time_since_epoch(); |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 359 | auto sec_dur = duration_cast<fs_seconds>(dur); |
| 360 | auto subsec_dur = duration_cast<fs_nanoseconds>(dur - sec_dur); |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 361 | // The tv_nsec and tv_usec fields must not be negative so adjust accordingly |
| 362 | if (subsec_dur.count() < 0) { |
| 363 | if (sec_dur.count() > min_seconds) { |
Eric Fiselier | ce34437 | 2018-07-25 21:53:43 +0000 | [diff] [blame] | 364 | sec_dur = sec_dur - fs_seconds(1); |
| 365 | subsec_dur = subsec_dur + fs_seconds(1); |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 366 | } else { |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 367 | subsec_dur = fs_nanoseconds::zero(); |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | return checked_set(sec_out, sec_dur.count()) && |
| 371 | checked_set(subsec_out, subsec_dur.count()); |
| 372 | } |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 373 | static _LIBCPP_CONSTEXPR_AFTER_CXX11 bool convert_to_timespec(TimeSpecT& dest, |
| 374 | FileTimeT tp) { |
| 375 | if (!is_representable(tp)) |
| 376 | return false; |
| 377 | return set_times_checked(&dest.tv_sec, &dest.tv_nsec, tp); |
| 378 | } |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 379 | }; |
| 380 | |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 381 | using fs_time = time_util<file_time_type, time_t, TimeSpec>; |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 382 | |
| 383 | #if defined(__APPLE__) |
| 384 | TimeSpec extract_mtime(StatT const& st) { return st.st_mtimespec; } |
| 385 | TimeSpec extract_atime(StatT const& st) { return st.st_atimespec; } |
| 386 | #else |
| 387 | TimeSpec extract_mtime(StatT const& st) { return st.st_mtim; } |
| 388 | TimeSpec extract_atime(StatT const& st) { return st.st_atim; } |
| 389 | #endif |
| 390 | |
Eric Fiselier | a0a7c1f | 2018-07-26 03:57:26 +0000 | [diff] [blame] | 391 | // allow the utimes implementation to compile even it we're not going |
| 392 | // to use it. |
| 393 | |
| 394 | bool posix_utimes(const path& p, std::array<TimeSpec, 2> const& TS, |
Eric Fiselier | 998a5c8 | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 395 | error_code& ec) { |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 396 | using namespace chrono; |
Alex Lorenz | 70cf5c4 | 2018-07-25 23:59:54 +0000 | [diff] [blame] | 397 | auto Convert = [](long nsec) { |
Eric Fiselier | 998a5c8 | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 398 | using int_type = decltype(std::declval< ::timeval>().tv_usec); |
Alex Lorenz | 70cf5c4 | 2018-07-25 23:59:54 +0000 | [diff] [blame] | 399 | auto dur = duration_cast<microseconds>(nanoseconds(nsec)).count(); |
| 400 | return static_cast<int_type>(dur); |
Eric Fiselier | c55ac10 | 2018-07-25 20:51:49 +0000 | [diff] [blame] | 401 | }; |
| 402 | struct ::timeval ConvertedTS[2] = {{TS[0].tv_sec, Convert(TS[0].tv_nsec)}, |
| 403 | {TS[1].tv_sec, Convert(TS[1].tv_nsec)}}; |
Eric Fiselier | a0a7c1f | 2018-07-26 03:57:26 +0000 | [diff] [blame] | 404 | if (::utimes(p.c_str(), ConvertedTS) == -1) { |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 405 | ec = capture_errno(); |
| 406 | return true; |
| 407 | } |
| 408 | return false; |
| 409 | } |
| 410 | |
Eric Fiselier | a0a7c1f | 2018-07-26 03:57:26 +0000 | [diff] [blame] | 411 | #if defined(_LIBCPP_USE_UTIMENSAT) |
| 412 | bool posix_utimensat(const path& p, std::array<TimeSpec, 2> const& TS, |
Eric Fiselier | 998a5c8 | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 413 | error_code& ec) { |
| 414 | if (::utimensat(AT_FDCWD, p.c_str(), TS.data(), 0) == -1) { |
Eric Fiselier | a0a7c1f | 2018-07-26 03:57:26 +0000 | [diff] [blame] | 415 | ec = capture_errno(); |
| 416 | return true; |
| 417 | } |
| 418 | return false; |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 419 | } |
Eric Fiselier | a0a7c1f | 2018-07-26 03:57:26 +0000 | [diff] [blame] | 420 | #endif |
| 421 | |
| 422 | bool set_file_times(const path& p, std::array<TimeSpec, 2> const& TS, |
| 423 | error_code& ec) { |
| 424 | #if !defined(_LIBCPP_USE_UTIMENSAT) |
| 425 | return posix_utimes(p, TS, ec); |
| 426 | #else |
| 427 | return posix_utimensat(p, TS, ec); |
| 428 | #endif |
| 429 | } |
| 430 | |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 431 | } // namespace |
| 432 | } // end namespace detail |
| 433 | |
Eric Fiselier | 998a5c8 | 2018-07-27 03:07:09 +0000 | [diff] [blame] | 434 | _LIBCPP_END_NAMESPACE_FILESYSTEM |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 435 | |
Eric Fiselier | c169986 | 2018-07-20 01:22:32 +0000 | [diff] [blame] | 436 | #endif // FILESYSTEM_COMMON_H |