From 5551f2b7b467c66b79cbbb2d216d43d653971c12 Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Wed, 18 Jan 2023 03:07:00 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=20push=20v3=20repo=20as=20first=20?= =?UTF-8?q?repo=20natively?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 1 + src/pacmanconf_repo.cpp | 64 +++++++++++++++++++++++++++++++++++++++++ src/pacmanconf_repo.hpp | 10 +++++++ src/utils.cpp | 25 ++++++++++++---- 4 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 src/pacmanconf_repo.cpp create mode 100644 src/pacmanconf_repo.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f8dbba0..16993f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -96,6 +96,7 @@ add_executable(${PROJECT_NAME} src/config.cpp src/config.hpp src/utils.cpp src/utils.hpp src/cpu.cpp src/cpu.hpp + src/pacmanconf_repo.cpp src/pacmanconf_repo.hpp src/initcpio.cpp src/initcpio.hpp src/disk.cpp src/disk.hpp src/drivers.cpp src/drivers.hpp diff --git a/src/pacmanconf_repo.cpp b/src/pacmanconf_repo.cpp new file mode 100644 index 0000000..16196a6 --- /dev/null +++ b/src/pacmanconf_repo.cpp @@ -0,0 +1,64 @@ +#include "pacmanconf_repo.hpp" +#include "utils.hpp" + +#include // for ofstream +#include // for string +#include // for vector + +#include + +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wold-style-cast" +#elif defined(__GNUC__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wnull-dereference" +#pragma GCC diagnostic ignored "-Wuseless-cast" +#pragma GCC diagnostic ignored "-Wold-style-cast" +#endif + +#include +#include +#include + +#if defined(__clang__) +#pragma clang diagnostic pop +#elif defined(__GNUC__) +#pragma GCC diagnostic pop +#endif + +namespace detail::pacmanconf { + +bool push_repos_front(const std::string_view& file_path, const std::string_view& value) noexcept { + using StringVec = std::vector; + auto&& file_content = utils::read_whole_file(file_path); + if (file_content.empty()) { + spdlog::error("[PACMANCONFREPO] '{}' error occurred!", file_path); + return false; + } + auto&& content_lines = file_content | ranges::views::split('\n') | ranges::to>(); + for (std::size_t i = 1; i < content_lines.size(); ++i) { + const std::string_view current_line{content_lines[i - 1]}; + if (current_line.empty() || current_line.starts_with("#") || !current_line.starts_with("[") || current_line.starts_with("[options]")) { + continue; + } + + auto&& to_be_inserted = value | ranges::views::split('\n') | ranges::to>(); + to_be_inserted.emplace_back("\n"); + content_lines.insert(content_lines.begin() + static_cast(i - 1), + std::move_iterator(to_be_inserted.begin()), std::move_iterator(to_be_inserted.end())); + break; + } + + std::string&& result = content_lines | ranges::views::join('\n') | ranges::to(); + + std::ofstream pacmanconf_file{file_path.data()}; + if (!pacmanconf_file.is_open()) { + spdlog::error("[PACMANCONFREPO] '{}' open failed: {}", file_path, std::strerror(errno)); + return false; + } + pacmanconf_file << result; + return true; +} + +} // namespace detail::pacmanconf diff --git a/src/pacmanconf_repo.hpp b/src/pacmanconf_repo.hpp new file mode 100644 index 0000000..d0f5bb7 --- /dev/null +++ b/src/pacmanconf_repo.hpp @@ -0,0 +1,10 @@ +#ifndef PACMANCONF_REPO_HPP +#define PACMANCONF_REPO_HPP + +#include // for string_view + +namespace detail::pacmanconf { +bool push_repos_front(const std::string_view& file_path, const std::string_view& value) noexcept; +} // namespace detail::pacmanconf + +#endif // PACMANCONF_REPO_HPP diff --git a/src/utils.cpp b/src/utils.cpp index 1c232fc..b26983a 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -4,6 +4,7 @@ #include "definitions.hpp" #include "follow_process_log.hpp" #include "initcpio.hpp" +#include "pacmanconf_repo.hpp" #include "subprocess.h" #include "tui.hpp" #include "widgets.hpp" @@ -1512,7 +1513,7 @@ void id_system() noexcept { } void install_cachyos_repo() noexcept { - const auto& add_arch_specific_repo = [](auto&& isa_level, [[maybe_unused]] auto&& repo_name, const auto& isa_levels, [[maybe_unused]] const auto& functor) { + const auto& add_arch_specific_repo = [](auto&& isa_level, [[maybe_unused]] auto&& repo_name, const auto& isa_levels, [[maybe_unused]] auto&& repos_data) { if (ranges::contains(isa_levels, isa_level)) { spdlog::warn("{} is not supported", isa_level); return; @@ -1521,11 +1522,16 @@ void install_cachyos_repo() noexcept { #ifdef NDEVENV static constexpr auto pacman_conf = "/etc/pacman.conf"; - static constexpr auto pacman_conf_cachyos = "/etc/pacman-more.conf"; + static constexpr auto pacman_conf_cachyos = "./pacman.conf"; static constexpr auto pacman_conf_path_backup = "/etc/pacman.conf.bak"; std::error_code err{}; - functor(pacman_conf_cachyos); + if (!fs::copy_file(pacman_conf, pacman_conf_cachyos, err)) { + spdlog::error("Failed to copy pacman config [{}]", err.message()); + return; + } + + detail::pacmanconf::push_repos_front(pacman_conf_cachyos, repos_data); spdlog::info("backup old config"); fs::rename(pacman_conf, pacman_conf_path_backup, err); @@ -1550,9 +1556,16 @@ void install_cachyos_repo() noexcept { #endif const auto& isa_levels = utils::get_isa_levels(); - add_arch_specific_repo("x86-64-v3", "cachyos-v3", isa_levels, [](auto&& target_config) { - utils::exec(fmt::format(FMT_COMPILE("sed -i 's/#//g' {}"), target_config)); - }); + +static constexpr auto CACHYOS_V3_REPO_STR = R"( +[cachyos-v3] +Include = /etc/pacman.d/cachyos-v3-mirrorlist + +[cachyos] +Include = /etc/pacman.d/cachyos-mirrorlist +)"; + + add_arch_specific_repo("x86-64-v3", "cachyos-v3", isa_levels, CACHYOS_V3_REPO_STR); } bool handle_connection() noexcept {