From ec7b6f47c69cca15059b8df1212a205a1b32c857 Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Sat, 6 Jul 2024 02:52:46 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20move=20hwclock=20into=20gucc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gucc/CMakeLists.txt | 1 + gucc/include/gucc/hwclock.hpp | 16 +++++++++++++ gucc/meson.build | 1 + gucc/src/hwclock.cpp | 42 +++++++++++++++++++++++++++++++++++ src/utils.cpp | 17 +++++++++++++- 5 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 gucc/include/gucc/hwclock.hpp create mode 100644 gucc/src/hwclock.cpp diff --git a/gucc/CMakeLists.txt b/gucc/CMakeLists.txt index b8b7fcc..a121ef9 100644 --- a/gucc/CMakeLists.txt +++ b/gucc/CMakeLists.txt @@ -29,6 +29,7 @@ add_library(${PROJECT_NAME} SHARED src/autologin.cpp include/gucc/autologin.hpp src/mtab.cpp include/gucc/mtab.hpp src/umount_partitions.cpp include/gucc/umount_partitions.hpp + src/hwclock.cpp include/gucc/hwclock.hpp #src/chwd_profiles.cpp src/chwd_profiles.hpp #src/disk.cpp src/disk.hpp ) diff --git a/gucc/include/gucc/hwclock.hpp b/gucc/include/gucc/hwclock.hpp new file mode 100644 index 0000000..f18179c --- /dev/null +++ b/gucc/include/gucc/hwclock.hpp @@ -0,0 +1,16 @@ +#ifndef HWCLOCK_HPP +#define HWCLOCK_HPP + +#include // for string_view + +namespace gucc::hwclock { + +// set utc hwclock +auto set_hwclock_utc(std::string_view root_mountpoint) noexcept -> bool; + +// set localtime hwclock +auto set_hwclock_localtime(std::string_view root_mountpoint) noexcept -> bool; + +} // namespace gucc::hwclock + +#endif // HWCLOCK_HPP diff --git a/gucc/meson.build b/gucc/meson.build index f90f545..08d7d37 100644 --- a/gucc/meson.build +++ b/gucc/meson.build @@ -19,6 +19,7 @@ gucc_lib = library('gucc', 'src/autologin.cpp', 'src/mtab.cpp', 'src/umount_partitions.cpp', + 'src/hwclock.cpp', ], include_directories : [include_directories('include')], dependencies: deps diff --git a/gucc/src/hwclock.cpp b/gucc/src/hwclock.cpp new file mode 100644 index 0000000..9bf0a6b --- /dev/null +++ b/gucc/src/hwclock.cpp @@ -0,0 +1,42 @@ +#include "gucc/hwclock.hpp" +#include "gucc/io_utils.hpp" + +#include +#include + +#include + +using namespace std::string_view_literals; + +namespace gucc::hwclock { + +auto set_hwclock_utc(std::string_view root_mountpoint) noexcept -> bool { + // try default + static constexpr auto hwclock_cmd = "hwclock --systohc --utc"sv; + if (utils::arch_chroot_checked(hwclock_cmd, root_mountpoint)) { + spdlog::info("hwclock UTC set on '{}'", root_mountpoint); + return true; + } + + // try fallback with direct ISA + static constexpr auto hwclock_isa_cmd = "hwclock --systohc --utc --directisa"sv; + if (utils::arch_chroot_checked(hwclock_isa_cmd, root_mountpoint)) { + spdlog::info("hwclock UTC set using direct ISA on '{}'", root_mountpoint); + return true; + } + + return false; +} + +auto set_hwclock_localtime(std::string_view root_mountpoint) noexcept -> bool { + // try default + static constexpr auto hwclock_cmd = "hwclock --systohc --localtime"sv; + if (utils::arch_chroot_checked(hwclock_cmd, root_mountpoint)) { + spdlog::info("hwclock localtime set on '{}'", root_mountpoint); + return true; + } + + return false; +} + +} // namespace gucc::hwclock diff --git a/src/utils.cpp b/src/utils.cpp index ce46291..2c9b587 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -11,6 +11,7 @@ #include "gucc/cpu.hpp" #include "gucc/file_utils.hpp" #include "gucc/fs_utils.hpp" +#include "gucc/hwclock.hpp" #include "gucc/initcpio.hpp" #include "gucc/io_utils.hpp" #include "gucc/locale.hpp" @@ -432,7 +433,21 @@ void set_timezone(const std::string_view& timezone) noexcept { void set_hw_clock(const std::string_view& clock_type) noexcept { spdlog::info("Clock type is: {}", clock_type); #ifdef NDEVENV - utils::arch_chroot(fmt::format(FMT_COMPILE("hwclock --systohc --{}"), clock_type), false); + auto* config_instance = Config::instance(); + auto& config_data = config_instance->data(); + const auto& mountpoint = std::get(config_data["MOUNTPOINT"]); + + if (clock_type == "utc"sv) { + if (!gucc::hwclock::set_hwclock_utc(mountpoint)) { + spdlog::error("Failed to set UTC hwclock"); + } + } else if (clock_type == "localtime"sv) { + if (!gucc::hwclock::set_hwclock_localtime(mountpoint)) { + spdlog::error("Failed to set localtime hwclock"); + } + } else { + spdlog::error("Unknown clock type {}", clock_type); + } #endif }