From 5c9a759573d3e12c4d2e9b4edc1f112b8a228ede Mon Sep 17 00:00:00 2001 From: David Howells Date: Wed, 12 Aug 2020 13:14:15 +0100 Subject: [PATCH] Add a class to represent times Signed-off-by: David Howells --- kafs/Makefile | 3 ++- kafs/kafs.H | 19 +++++++++++++++++++ kafs/misc.C | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 kafs/misc.C diff --git a/kafs/Makefile b/kafs/Makefile index aa1ff17..3dca362 100644 --- a/kafs/Makefile +++ b/kafs/Makefile @@ -7,7 +7,8 @@ CORE_SRCS := \ kafs.C \ arg_completion.C \ arg_parse.C \ - display_error.C + display_error.C \ + misc.C BOS_SRCS := \ bos.C \ diff --git a/kafs/kafs.H b/kafs/kafs.H index 0ae8b76..3011c18 100644 --- a/kafs/kafs.H +++ b/kafs/kafs.H @@ -23,6 +23,7 @@ extern "C" { namespace kafs { namespace afs { +class opr_time; constexpr unsigned int FS_PORT = 7000; /* AFS file server port */ constexpr unsigned int FS_SERVICE = 1; /* AFS File Service ID */ } @@ -34,6 +35,24 @@ enum Service { service_yfs, }; +class Time { + struct timespec ts; + bool has_nsecs; +public: + Time() { + ts.tv_sec = 0; + ts.tv_nsec = 0; + has_nsecs = false; + } + Time(time_t t) { + ts.tv_sec = t; + ts.tv_nsec = 0; + has_nsecs = false; + } + Time(const kafs::afs::opr_time &t); + inline time_t seconds() const { return ts.tv_sec; } +}; + class Context : public rxrpc::refcount { public: rxrpc::ref endpoint; diff --git a/kafs/misc.C b/kafs/misc.C new file mode 100644 index 0000000..fc3a184 --- /dev/null +++ b/kafs/misc.C @@ -0,0 +1,43 @@ +/* Miscellaneous routines. + * + * Copyright (C) 2020 Red Hat, Inc. All Rights Reserved. + * Written by David Howells (dhowells@redhat.com) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public Licence + * as published by the Free Software Foundation; either version + * 2 of the Licence, or (at your option) any later version. + */ + +#include "kafs.H" +#include "afs_xg.H" + +/* + * Convert a signed 100ns-resolution 64-bit time into a kafs time. + */ +kafs::Time::Time(const kafs::afs::opr_time &yt) +{ + unsigned long long abs_t, div, rem, t = yt.time; + + /* + * Unfortunately can not use normal 64 bit division on 32 bit arch, but + * the alternative, do_div, does not work with negative numbers so have + * to special case them + */ + if (t < 0) { + abs_t = -t; + div = abs_t / 10000000; + rem = abs_t % 10000000; + ts.tv_sec = -(div * 100); + ts.tv_nsec = rem; + } else { + abs_t = t; + div = abs_t / 10000000; + rem = abs_t % 10000000; + ts.tv_sec = div * 100; + ts.tv_nsec = rem; + } + + has_nsecs = true; +} + -- 2.49.0