From 8d12ef22b939c1be366c1cadba4b74d3876cc661 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Fri, 23 Aug 2024 17:35:34 -0500 Subject: [PATCH] refactor --- .../src/drives/fuse/fuse_drive.cpp | 38 +++++++++---------- .../fuse/remotefuse/remote_fuse_drive.cpp | 1 + .../drives/fuse/remotefuse/remote_server.cpp | 29 ++++++-------- .../winfsp/remotewinfsp/remote_server.cpp | 36 ++++++++---------- 4 files changed, 47 insertions(+), 57 deletions(-) diff --git a/repertory/librepertory/src/drives/fuse/fuse_drive.cpp b/repertory/librepertory/src/drives/fuse/fuse_drive.cpp index ec7d3df5..5a6d1f81 100644 --- a/repertory/librepertory/src/drives/fuse/fuse_drive.cpp +++ b/repertory/librepertory/src/drives/fuse/fuse_drive.cpp @@ -1155,8 +1155,8 @@ auto fuse_drive::setbkuptime_impl( std::string api_path, const struct timespec *bkuptime) -> api_error { return check_and_perform( api_path, X_OK, [&](api_meta_map &meta) -> api_error { - const auto nanos = - bkuptime->tv_nsec + (bkuptime->tv_nsec * NANOS_PER_SECOND); + const auto nanos = bkuptime->tv_nsec + + (bkuptime->tv_nsec * utils::time::NANOS_PER_SECOND); return provider_.set_item_meta(api_path, META_BACKUP, std::to_string(nanos)); }); @@ -1166,8 +1166,8 @@ auto fuse_drive::setchgtime_impl(std::string api_path, const struct timespec *chgtime) -> api_error { return check_and_perform( api_path, X_OK, [&](api_meta_map &meta) -> api_error { - const auto nanos = - chgtime->tv_nsec + (chgtime->tv_nsec * NANOS_PER_SECOND); + const auto nanos = chgtime->tv_nsec + + (chgtime->tv_nsec * utils::time::NANOS_PER_SECOND); return provider_.set_item_meta(api_path, META_CHANGED, std::to_string(nanos)); }); @@ -1178,7 +1178,7 @@ auto fuse_drive::setcrtime_impl(std::string api_path, return check_and_perform( api_path, X_OK, [&](api_meta_map &meta) -> api_error { const auto nanos = - crtime->tv_nsec + (crtime->tv_nsec * NANOS_PER_SECOND); + crtime->tv_nsec + (crtime->tv_nsec * utils::time::NANOS_PER_SECOND); return provider_.set_item_meta(api_path, META_CREATION, std::to_string(nanos)); }); @@ -1310,19 +1310,20 @@ auto fuse_drive::utimens_impl(std::string api_path, } meta.clear(); - if ((tv == nullptr) || (tv[0U].tv_nsec == UTIME_NOW)) { - meta[META_ACCESSED] = std::to_string(utils::time::get_time_now()); - } else if (tv[0U].tv_nsec != UTIME_OMIT) { - const auto val = tv[0U].tv_nsec + (tv[0U].tv_sec * NANOS_PER_SECOND); - meta[META_ACCESSED] = std::to_string(val); - } + const auto process_timespec = [&meta](auto *tv, const auto &val, + std::string attr) { + if ((tv == nullptr) || (val.tv_nsec == UTIME_NOW)) { + meta[attr] = std::to_string(utils::time::get_time_now()); + } else if (val.tv_nsec != UTIME_OMIT) { + meta[attr] = std::to_string( + val.tv_nsec + + (val.tv_sec * static_cast>( + utils::time::NANOS_PER_SECOND))); + } + }; - if ((tv == nullptr) || (tv[1U].tv_nsec == UTIME_NOW)) { - meta[META_MODIFIED] = std::to_string(utils::time::get_time_now()); - } else if (tv[1U].tv_nsec != UTIME_OMIT) { - const auto val = tv[1U].tv_nsec + (tv[1U].tv_sec * NANOS_PER_SECOND); - meta[META_MODIFIED] = std::to_string(val); - } + process_timespec(tv, tv[0U], META_ACCESSED); + process_timespec(tv, tv[1U], META_MODIFIED); if (not meta.empty()) { return provider_.set_item_meta(api_path, meta); @@ -1369,8 +1370,7 @@ void fuse_drive::update_accessed_time(const std::string &api_path) { if (atime_enabled_) { auto res = provider_.set_item_meta( - api_path, META_ACCESSED, - std::to_string(utils::time::get_time_now())); + api_path, META_ACCESSED, std::to_string(utils::time::get_time_now())); if (res != api_error::success) { utils::error::raise_api_path_error(function_name, api_path, res, "failed to set accessed time"); diff --git a/repertory/librepertory/src/drives/fuse/remotefuse/remote_fuse_drive.cpp b/repertory/librepertory/src/drives/fuse/remotefuse/remote_fuse_drive.cpp index 466bc633..00f6e58a 100644 --- a/repertory/librepertory/src/drives/fuse/remotefuse/remote_fuse_drive.cpp +++ b/repertory/librepertory/src/drives/fuse/remotefuse/remote_fuse_drive.cpp @@ -570,6 +570,7 @@ auto remote_fuse_drive::utimens_impl(std::string api_path, if (tv != nullptr) { rtv[0U] = static_cast( tv[0U].tv_nsec + (tv[0U].tv_sec * utils::time::NANOS_PER_SECOND)); + rtv[1U] = static_cast( tv[1U].tv_nsec + (tv[1U].tv_sec * utils::time::NANOS_PER_SECOND)); } diff --git a/repertory/librepertory/src/drives/fuse/remotefuse/remote_server.cpp b/repertory/librepertory/src/drives/fuse/remotefuse/remote_server.cpp index 245482bd..4403f8f2 100644 --- a/repertory/librepertory/src/drives/fuse/remotefuse/remote_server.cpp +++ b/repertory/librepertory/src/drives/fuse/remotefuse/remote_server.cpp @@ -1006,25 +1006,18 @@ auto remote_server::fuse_utimens(const char *path, const remote::file_time *tv, const auto file_path = construct_path(path); struct timespec tv2[2] = {{0, 0}}; - if ((op0 == UTIME_NOW) || (op0 == UTIME_OMIT)) { - tv2[0U].tv_nsec = static_cast(op0); - tv2[0U].tv_sec = 0; - } else { - tv2[0U].tv_nsec = - static_cast(tv[0U] % utils::time::NANOS_PER_SECOND); - tv2[0U].tv_sec = - static_cast(tv[0U] / utils::time::NANOS_PER_SECOND); - } + const auto process_timespec = [](auto op, const auto &src, auto &dst) { + if ((op == UTIME_NOW) || (op == UTIME_OMIT)) { + dst.tv_nsec = static_cast(op); + dst.tv_sec = 0; + } else { + dst.tv_nsec = static_cast(src % utils::time::NANOS_PER_SECOND); + dst.tv_sec = static_cast(src / utils::time::NANOS_PER_SECOND); + } + }; - if ((op1 == UTIME_NOW) || (op1 == UTIME_OMIT)) { - tv2[1U].tv_nsec = static_cast(op1); - tv2[1U].tv_sec = 0; - } else { - tv2[1U].tv_nsec = - static_cast(tv[1U] % utils::time::NANOS_PER_SECOND); - tv2[1U].tv_sec = - static_cast(tv[1U] / utils::time::NANOS_PER_SECOND); - } + process_timespec(op0, tv[0U], tv2[0U]); + process_timespec(op1, tv[1U], tv2[1U]); const auto res = utimensat(0, file_path.c_str(), &tv2[0U], AT_SYMLINK_NOFOLLOW); diff --git a/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_server.cpp b/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_server.cpp index 8af50ad4..a7383afd 100644 --- a/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_server.cpp +++ b/repertory/librepertory/src/drives/winfsp/remotewinfsp/remote_server.cpp @@ -864,26 +864,21 @@ auto remote_server::fuse_utimens(const char *path, const remote::file_time *tv, FILETIME *access_time_ptr = nullptr; FILETIME *write_time_ptr = nullptr; - if ((tv[0U] == 0U) || (op0 == UTIME_NOW)) { - const auto now = utils::time::get_time_now(); - access_time.dwHighDateTime = - static_cast((now >> 32U) & 0xFFFFFFFF); - access_time.dwLowDateTime = now & 0xFFFFFFFF; - access_time_ptr = &access_time; - } else if (op0 != UTIME_OMIT) { - access_time = utils::time::unix_time_to_filetime(tv[0U]); - access_time_ptr = &access_time; - } - - if ((tv[1U] == 0U) || (op1 == UTIME_NOW)) { - const auto now = utils::time::get_time_now(); - write_time.dwHighDateTime = static_cast((now >> 32U) & 0xFFFFFFFF); - write_time.dwLowDateTime = now & 0xFFFFFFFF; - write_time_ptr = &write_time; - } else if (op1 != UTIME_OMIT) { - write_time = utils::time::unix_time_to_filetime(tv[1U]); - write_time_ptr = &write_time; - } + const auto proccess_timespec = [](auto op, const auto &src, auto &dst, + auto *&dst_ptr) { + if ((src == 0U) || (op == UTIME_NOW)) { + auto now = + utils::time::unix_error_to_windows(utils::time::get_time_now()); + dest.dwHighDateTime = static_cast((now >> 32U) & 0xFFFFFFFF); + dest.dwLowDateTime = now & 0xFFFFFFFF; + dst_ptr = &dest; + } else if (op != UTIME_OMIT) { + dest = utils::time::unix_time_to_filetime(src); + dst_ptr = &dest; + } + }; + proccess_timespec(op0, tv[0U], access_time, access_time_ptr); + proccess_timespec(op1, tv[1U], write_time, write_time_ptr); errno = EFAULT; if (::SetFileTime(os_handle, nullptr, access_time_ptr, write_time_ptr) != @@ -891,6 +886,7 @@ auto remote_server::fuse_utimens(const char *path, const remote::file_time *tv, res = 0; errno = 0; } + ::CloseHandle(os_handle); }