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