Always call clock_gettime(2) (#1871)

e01ded9e27 introduced this compatibility
shim but macOS 10.12 (2016) added this:
https://stackoverflow.com/a/39801564 .  Also remove fallback to
time(3) which loses precision.
This commit is contained in:
Andrew Gaul 2022-01-25 08:36:27 +09:00 committed by GitHub
parent de0c87c801
commit 662882d2f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 25 deletions

View File

@ -68,7 +68,7 @@ Many systems provide pre-built packages:
sudo zypper install s3fs
```
* macOS via [Homebrew](https://brew.sh/):
* macOS 10.12 and newer via [Homebrew](https://brew.sh/):
```
brew install --cask osxfuse

View File

@ -18,11 +18,9 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#ifndef HAVE_CLOCK_GETTIME
#include <sys/time.h>
#endif
#include <algorithm>
@ -45,29 +43,11 @@
#define CLOCK_MONOTONIC_COARSE CLOCK_MONOTONIC
#endif
#ifdef HAVE_CLOCK_GETTIME
static int s3fs_clock_gettime(int clk_id, struct timespec* ts)
{
return clock_gettime(static_cast<clockid_t>(clk_id), ts);
}
#else
static int s3fs_clock_gettime(int clk_id, struct timespec* ts)
{
struct timeval now;
if(0 != gettimeofday(&now, NULL)){
return -1;
}
ts->tv_sec = now.tv_sec;
ts->tv_nsec = now.tv_usec * 1000;
return 0;
}
#endif
inline void SetStatCacheTime(struct timespec& ts)
{
if(-1 == s3fs_clock_gettime(CLOCK_MONOTONIC_COARSE, &ts)){
ts.tv_sec = time(NULL);
ts.tv_nsec = 0;
if(-1 == clock_gettime(static_cast<clockid_t>(CLOCK_MONOTONIC_COARSE), &ts)){
S3FS_PRN_CRIT("clock_gettime failed: %d", errno);
abort();
}
}