From fc2c8ff3cedb9b5f79d0036e36f55999df9741e7 Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Mon, 1 Jul 2024 16:17:01 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20gucc:=20refactor=20file=20creati?= =?UTF-8?q?on=20for=20overwrite?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gucc/include/gucc/file_utils.hpp | 8 ++++++-- gucc/src/crypttab.cpp | 8 +++----- gucc/src/file_utils.cpp | 13 +++++++++++-- gucc/src/fstab.cpp | 8 +++----- gucc/src/locale.cpp | 7 ++----- gucc/src/user.cpp | 14 ++++---------- 6 files changed, 29 insertions(+), 29 deletions(-) diff --git a/gucc/include/gucc/file_utils.hpp b/gucc/include/gucc/file_utils.hpp index f31a014..b36e4eb 100644 --- a/gucc/include/gucc/file_utils.hpp +++ b/gucc/include/gucc/file_utils.hpp @@ -6,8 +6,12 @@ namespace gucc::file_utils { -auto read_whole_file(const std::string_view& filepath) noexcept -> std::string; -bool write_to_file(const std::string_view& data, const std::string_view& filepath) noexcept; +auto read_whole_file(std::string_view filepath) noexcept -> std::string; +auto write_to_file(std::string_view data, std::string_view filepath) noexcept -> bool; + +// If the file doesn't exist, then it create one and write into it. +// If the file exists already, then it will overwrite file content with provided data. +auto create_file_for_overwrite(std::string_view filepath, std::string_view data) noexcept -> bool; } // namespace gucc::file_utils diff --git a/gucc/src/crypttab.cpp b/gucc/src/crypttab.cpp index c9c50e4..610fd02 100644 --- a/gucc/src/crypttab.cpp +++ b/gucc/src/crypttab.cpp @@ -1,10 +1,10 @@ #include "gucc/crypttab.hpp" +#include "gucc/file_utils.hpp" #include "gucc/partition.hpp" #include // for any_of, sort, unique_copy #include -#include #include #include @@ -104,13 +104,11 @@ auto generate_crypttab_content(const std::vector& partitions, std::st auto generate_crypttab(const std::vector& partitions, std::string_view root_mountpoint, std::string_view crypttab_opts) noexcept -> bool { const auto& crypttab_filepath = fmt::format(FMT_COMPILE("{}/etc/crypttab"), root_mountpoint); - - std::ofstream crypttab_file{crypttab_filepath, std::ios::out | std::ios::trunc}; - if (!crypttab_file.is_open()) { + const auto& crypttab_content = fs::generate_crypttab_content(partitions, crypttab_opts); + if (!file_utils::create_file_for_overwrite(crypttab_filepath, crypttab_content)) { spdlog::error("Failed to open crypttab for writing {}", crypttab_filepath); return false; } - crypttab_file << fs::generate_crypttab_content(partitions, crypttab_opts); return true; } diff --git a/gucc/src/file_utils.cpp b/gucc/src/file_utils.cpp index e8c2350..208b6cd 100644 --- a/gucc/src/file_utils.cpp +++ b/gucc/src/file_utils.cpp @@ -10,7 +10,7 @@ namespace gucc::file_utils { -auto read_whole_file(const std::string_view& filepath) noexcept -> std::string { +auto read_whole_file(std::string_view filepath) noexcept -> std::string { // Use std::fopen because it's faster than std::ifstream auto* file = std::fopen(filepath.data(), "rb"); if (file == nullptr) { @@ -35,7 +35,7 @@ auto read_whole_file(const std::string_view& filepath) noexcept -> std::string { return buf; } -bool write_to_file(const std::string_view& data, const std::string_view& filepath) noexcept { +auto write_to_file(std::string_view data, std::string_view filepath) noexcept -> bool { std::ofstream file{filepath.data()}; if (!file.is_open()) { spdlog::error("[WRITE_TO_FILE] '{}' open failed: {}", filepath, std::strerror(errno)); @@ -45,4 +45,13 @@ bool write_to_file(const std::string_view& data, const std::string_view& filepat return true; } +auto create_file_for_overwrite(std::string_view filepath, std::string_view data) noexcept -> bool { + std::ofstream file{filepath.data(), std::ios::out | std::ios::trunc}; + if (!file.is_open()) { + return false; + } + file << data; + return true; +} + } // namespace gucc::file_utils diff --git a/gucc/src/fstab.cpp b/gucc/src/fstab.cpp index 8c8ab31..d7090ee 100644 --- a/gucc/src/fstab.cpp +++ b/gucc/src/fstab.cpp @@ -1,11 +1,11 @@ #include "gucc/fstab.hpp" +#include "gucc/file_utils.hpp" #include "gucc/io_utils.hpp" #include // for tolower #include // for transform #include -#include #include #include @@ -127,13 +127,11 @@ auto generate_fstab_content(const std::vector& partitions) noexcept - auto generate_fstab(const std::vector& partitions, std::string_view root_mountpoint) noexcept -> bool { const auto& fstab_filepath = fmt::format(FMT_COMPILE("{}/etc/fstab"), root_mountpoint); - - std::ofstream fstab_file{fstab_filepath, std::ios::out | std::ios::trunc}; - if (!fstab_file.is_open()) { + const auto& fstab_content = fs::generate_fstab_content(partitions); + if (!file_utils::create_file_for_overwrite(fstab_filepath, fstab_content)) { spdlog::error("Failed to open fstab for writing {}", fstab_filepath); return false; } - fstab_file << fs::generate_fstab_content(partitions); return true; } diff --git a/gucc/src/locale.cpp b/gucc/src/locale.cpp index 718bf65..cc408d6 100644 --- a/gucc/src/locale.cpp +++ b/gucc/src/locale.cpp @@ -1,9 +1,8 @@ #include "gucc/locale.hpp" +#include "gucc/file_utils.hpp" #include "gucc/io_utils.hpp" #include "gucc/string_utils.hpp" -#include // for ofstream - #include #include @@ -32,12 +31,10 @@ LC_MESSAGES="{0}" { const auto& locale_config_text = fmt::format(LOCALE_CONFIG_PART, locale); - std::ofstream locale_config_file{locale_config_path, std::ios::out | std::ios::trunc}; - if (!locale_config_file.is_open()) { + if (!file_utils::create_file_for_overwrite(locale_config_path, locale_config_text)) { spdlog::error("Failed to open locale config for writing {}", locale_config_path); return false; } - locale_config_file << locale_config_text; } // TODO(vnepogodin): refactor and make backups of locale config and locale gen diff --git a/gucc/src/user.cpp b/gucc/src/user.cpp index ef5d321..05670c5 100644 --- a/gucc/src/user.cpp +++ b/gucc/src/user.cpp @@ -1,10 +1,10 @@ #include "gucc/user.hpp" +#include "gucc/file_utils.hpp" #include "gucc/io_utils.hpp" #include "gucc/string_utils.hpp" #include // for find #include -#include // for ofstream #include #include @@ -115,12 +115,10 @@ auto create_new_user(const user::UserInfo& user_info, const std::vector