xstd: add helper function for getting current time

Also, don't bother with failed clock_gettime() calls.
This commit is contained in:
CismonX 2025-03-19 21:26:27 +08:00
parent d7c7ec0174
commit 00f40beec7
No known key found for this signature in database
GPG key ID: 3094873E29A482FB
5 changed files with 26 additions and 30 deletions

View file

@ -808,10 +808,7 @@ build_ts (
return 0;
}
if (ts->tv_nsec == UTIME_NOW) {
if (unlikely(0 != clock_gettime(CLOCK_REALTIME, ts))) {
log_printf("clock_gettime(): %s", xstrerror(errno));
return -1;
}
xgetrealtime(ts);
}
// XXX: May overflow if system time is badly wrong,

View file

@ -2085,10 +2085,7 @@ usecs_now (
if (ts_buf == NULL) {
ts_buf = &ts_tmp;
}
if (unlikely(0 != clock_gettime(CLOCK_REALTIME, ts_buf))) {
log_printf("clock_gettime(): %s", xstrerror(errno));
return -1;
}
xgetrealtime(ts_buf);
return timespec_to_usecs(ts_buf);
}

View file

@ -235,7 +235,6 @@ static int bm_setattr (uint64_t, int, bool, struct fuse_file_info const *,
static int bm_setxattr (uint64_t, char const *, void const *, size_t, int);
static int bm_write (fuse_req_t, int, char const *, size_t, off_t,
struct fs_file_handle *);
static int current_time (struct timespec *);
static void do_delete (fuse_req_t, fuse_ino_t, char const *);
static void do_readdir (fuse_req_t, fuse_ino_t, size_t, off_t, uint32_t,
struct fuse_file_info const *);
@ -798,7 +797,7 @@ bm_open (
// We choose to silently ignore O_TRUNC.
if ((flags & O_ACCMODE) != O_RDONLY) {
fh->data_len = 0;
current_time(&fh->mtime);
xgetrealtime(&fh->mtime);
fh->flags |= FH_FLAG_DIRTY;
}
}
@ -1091,9 +1090,7 @@ bm_setattr (
memset(fh->buf + fh->data_len, 0, new_len - fh->data_len);
}
fh->data_len = new_len;
if (unlikely(0 != current_time(&fh->mtime))) {
return -EIO;
}
xgetrealtime(&fh->mtime);
fh->flags |= FH_FLAG_DIRTY;
}
@ -1104,9 +1101,7 @@ bm_setattr (
struct timespec ts = { .tv_nsec = UTIME_OMIT };
if (mask & TO_SET(ATIME_NOW, MTIME_NOW)) {
if (unlikely(0 != current_time(&ts))) {
return -EIO;
}
xgetrealtime(&ts);
}
struct timespec times[] = { ts, ts };
@ -1195,9 +1190,7 @@ bm_write (
fh->data_len = req_off_max;
}
if (unlikely(0 != current_time(&fh->mtime))) {
return -EIO;
}
xgetrealtime(&fh->mtime);
// We're tempted to free the cookie here, however,
// that would make a read following a writeback always realloc the buffer.
fh->flags |= FH_FLAG_DIRTY;
@ -1205,17 +1198,6 @@ bm_write (
return send_reply(write, req, req_buf_len);
}
static int
current_time (
struct timespec *ts
) {
if (unlikely(0 != clock_gettime(CLOCK_REALTIME, ts))) {
log_printf("clock_gettime(): %s", xstrerror(errno));
return -1;
}
return 0;
}
static void
do_delete (
fuse_req_t req,

View file

@ -86,6 +86,16 @@ xfsync (
return 0;
}
void
xgetrealtime (
struct timespec *ts
) {
if (0 != clock_gettime(CLOCK_REALTIME, ts)) {
log_printf("clock_gettime(): %s", xstrerror(errno));
abort();
}
}
void *
xmalloc (
size_t size

View file

@ -24,6 +24,7 @@
#define BOOKMARKFS_XSTD_H_
#include <stdio.h>
#include <time.h>
#include "defs.h"
@ -119,6 +120,15 @@ xfsync (
int fd
);
/**
* Like clock_gettime(CLOCK_REALTIME, ts), but aborts on error.
*/
BOOKMARKFS_INTERNAL
void
xgetrealtime (
struct timespec *ts
);
/**
* Like malloc(), but never returns NULL.
*