Skip to content

Commit

Permalink
🧹 move hwclock into gucc
Browse files Browse the repository at this point in the history
  • Loading branch information
vnepogodin committed Jul 5, 2024
1 parent 742022e commit ec7b6f4
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions gucc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
16 changes: 16 additions & 0 deletions gucc/include/gucc/hwclock.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef HWCLOCK_HPP
#define HWCLOCK_HPP

#include <string_view> // 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
1 change: 1 addition & 0 deletions gucc/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions gucc/src/hwclock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "gucc/hwclock.hpp"
#include "gucc/io_utils.hpp"

#include <fmt/compile.h>
#include <fmt/format.h>

#include <spdlog/spdlog.h>

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
17 changes: 16 additions & 1 deletion src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<std::string>(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
}

Expand Down

0 comments on commit ec7b6f4

Please sign in to comment.