From 00f40beec72fe1e01e34121a8e6b8ff270bf7877 Mon Sep 17 00:00:00 2001 From: CismonX Date: Wed, 19 Mar 2025 21:26:27 +0800 Subject: [PATCH] xstd: add helper function for getting current time Also, don't bother with failed clock_gettime() calls. --- src/backend_chromium.c | 5 +---- src/backend_firefox.c | 5 +---- src/fs_ops.c | 26 ++++---------------------- src/xstd.c | 10 ++++++++++ src/xstd.h | 10 ++++++++++ 5 files changed, 26 insertions(+), 30 deletions(-) diff --git a/src/backend_chromium.c b/src/backend_chromium.c index 6331971..f8af84d 100644 --- a/src/backend_chromium.c +++ b/src/backend_chromium.c @@ -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, diff --git a/src/backend_firefox.c b/src/backend_firefox.c index 3909864..66d26c0 100644 --- a/src/backend_firefox.c +++ b/src/backend_firefox.c @@ -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); } diff --git a/src/fs_ops.c b/src/fs_ops.c index 676f7bb..3224414 100644 --- a/src/fs_ops.c +++ b/src/fs_ops.c @@ -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, diff --git a/src/xstd.c b/src/xstd.c index dc770d7..d815644 100644 --- a/src/xstd.c +++ b/src/xstd.c @@ -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 diff --git a/src/xstd.h b/src/xstd.h index 050d468..c267c75 100644 --- a/src/xstd.h +++ b/src/xstd.h @@ -24,6 +24,7 @@ #define BOOKMARKFS_XSTD_H_ #include +#include #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. *