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