2018-12-17 05:29:52 +03:00
|
|
|
/*
|
2019-06-20 02:58:25 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2018-12-17 05:29:52 +03:00
|
|
|
*
|
2019-06-20 02:58:25 +03:00
|
|
|
* This software may be used and distributed according to the terms of the
|
|
|
|
* GNU General Public License version 2.
|
2018-12-17 05:29:52 +03:00
|
|
|
*/
|
2019-10-11 15:26:59 +03:00
|
|
|
|
2018-12-17 05:29:52 +03:00
|
|
|
#pragma once
|
2019-04-25 00:11:30 +03:00
|
|
|
|
|
|
|
#include <folly/chrono/Conv.h>
|
2018-12-17 05:29:52 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <time.h>
|
2019-04-25 00:11:30 +03:00
|
|
|
#include <chrono>
|
2018-12-17 05:29:52 +03:00
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace eden {
|
|
|
|
|
|
|
|
/** Helper for accessing the `atime` field of a `struct stat` as a timespec.
|
|
|
|
* Linux and macOS have different names for this field. */
|
2020-04-25 01:54:10 +03:00
|
|
|
inline struct timespec stAtime(const struct stat& st) {
|
2018-12-17 05:29:52 +03:00
|
|
|
#ifdef __APPLE__
|
|
|
|
return st.st_atimespec;
|
|
|
|
#elif defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || \
|
|
|
|
_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
|
|
|
|
return st.st_atim;
|
|
|
|
#else
|
2020-04-25 01:54:10 +03:00
|
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = st.st_atime;
|
|
|
|
ts.tv_nsec = 0;
|
|
|
|
return ts;
|
2018-12-17 05:29:52 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Helper for accessing the `mtime` field of a `struct stat` as a timespec.
|
|
|
|
* Linux and macOS have different names for this field. */
|
2020-04-25 01:54:10 +03:00
|
|
|
inline struct timespec stMtime(const struct stat& st) {
|
2018-12-17 05:29:52 +03:00
|
|
|
#ifdef __APPLE__
|
|
|
|
return st.st_mtimespec;
|
|
|
|
#elif defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || \
|
|
|
|
_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
|
|
|
|
return st.st_mtim;
|
|
|
|
#else
|
2020-04-25 01:54:10 +03:00
|
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = st.st_mtime;
|
|
|
|
ts.tv_nsec = 0;
|
|
|
|
return ts;
|
2018-12-17 05:29:52 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Helper for accessing the `ctime` field of a `struct stat` as a timespec.
|
|
|
|
* Linux and macOS have different names for this field. */
|
2020-04-25 01:54:10 +03:00
|
|
|
inline struct timespec stCtime(const struct stat& st) {
|
2018-12-17 05:29:52 +03:00
|
|
|
#ifdef __APPLE__
|
|
|
|
return st.st_ctimespec;
|
|
|
|
#elif defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || \
|
|
|
|
_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700
|
|
|
|
return st.st_ctim;
|
|
|
|
#else
|
2020-04-25 01:54:10 +03:00
|
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = st.st_ctime;
|
|
|
|
ts.tv_nsec = 0;
|
|
|
|
return ts;
|
2018-12-17 05:29:52 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-04-25 00:11:30 +03:00
|
|
|
/**
|
|
|
|
* Access stat atime as a system_clock::time_point.
|
|
|
|
*/
|
|
|
|
inline std::chrono::system_clock::time_point stAtimepoint(
|
|
|
|
const struct stat& st) {
|
|
|
|
return folly::to<std::chrono::system_clock::time_point>(stAtime(st));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Access stat mtime as a system_clock::time_point.
|
|
|
|
*/
|
|
|
|
inline std::chrono::system_clock::time_point stCtimepoint(
|
|
|
|
const struct stat& st) {
|
|
|
|
return folly::to<std::chrono::system_clock::time_point>(stCtime(st));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Access stat ctime as a system_clock::time_point.
|
|
|
|
*/
|
|
|
|
inline std::chrono::system_clock::time_point stMtimepoint(
|
|
|
|
const struct stat& st) {
|
|
|
|
return folly::to<std::chrono::system_clock::time_point>(stMtime(st));
|
|
|
|
}
|
|
|
|
|
2018-12-17 05:29:52 +03:00
|
|
|
} // namespace eden
|
|
|
|
} // namespace facebook
|