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