🧹 gucc: refactor file creation for overwrite

This commit is contained in:
Vladislav Nepogodin 2024-07-01 16:17:01 +04:00
parent a527dc7edd
commit fc2c8ff3ce
No known key found for this signature in database
GPG Key ID: B62C3D10C54D5DA9
6 changed files with 29 additions and 29 deletions

View File

@ -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

View File

@ -1,10 +1,10 @@
#include "gucc/crypttab.hpp"
#include "gucc/file_utils.hpp"
#include "gucc/partition.hpp"
#include <algorithm> // for any_of, sort, unique_copy
#include <filesystem>
#include <fstream>
#include <fmt/compile.h>
#include <fmt/format.h>
@ -104,13 +104,11 @@ auto generate_crypttab_content(const std::vector<Partition>& partitions, std::st
auto generate_crypttab(const std::vector<Partition>& 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;
}

View File

@ -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

View File

@ -1,11 +1,11 @@
#include "gucc/fstab.hpp"
#include "gucc/file_utils.hpp"
#include "gucc/io_utils.hpp"
#include <cctype> // for tolower
#include <algorithm> // for transform
#include <filesystem>
#include <fstream>
#include <fmt/compile.h>
#include <fmt/format.h>
@ -127,13 +127,11 @@ auto generate_fstab_content(const std::vector<Partition>& partitions) noexcept -
auto generate_fstab(const std::vector<Partition>& 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;
}

View File

@ -1,9 +1,8 @@
#include "gucc/locale.hpp"
#include "gucc/file_utils.hpp"
#include "gucc/io_utils.hpp"
#include "gucc/string_utils.hpp"
#include <fstream> // for ofstream
#include <fmt/compile.h>
#include <fmt/format.h>
@ -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

View File

@ -1,10 +1,10 @@
#include "gucc/user.hpp"
#include "gucc/file_utils.hpp"
#include "gucc/io_utils.hpp"
#include "gucc/string_utils.hpp"
#include <algorithm> // for find
#include <filesystem>
#include <fstream> // for ofstream
#include <fmt/compile.h>
#include <fmt/format.h>
@ -115,12 +115,10 @@ auto create_new_user(const user::UserInfo& user_info, const std::vector<std::str
const auto& sudoers_filepath = fmt::format(FMT_COMPILE("{}/etc/sudoers.d/10-installer"), mountpoint);
{
const auto& sudoers_line = fmt::format(FMT_COMPILE("%{} ALL=(ALL) ALL\n"), user_info.sudoers_group);
std::ofstream sudoers_file{sudoers_filepath, std::ios::out | std::ios::trunc};
if (!sudoers_file.is_open()) {
if (!file_utils::create_file_for_overwrite(sudoers_filepath, sudoers_line)) {
spdlog::error("Failed to open sudoers for writing {}", sudoers_filepath);
return false;
}
sudoers_file << sudoers_line;
}
std::error_code err{};
@ -138,12 +136,10 @@ auto set_hostname(std::string_view hostname, std::string_view mountpoint) noexce
{
const auto& hostname_filepath = fmt::format(FMT_COMPILE("{}/etc/hostname"), mountpoint);
const auto& hostname_line = fmt::format(FMT_COMPILE("{}\n"), hostname);
std::ofstream hostname_file{hostname_filepath, std::ios::out | std::ios::trunc};
if (!hostname_file.is_open()) {
if (!file_utils::create_file_for_overwrite(hostname_filepath, hostname_line)) {
spdlog::error("Failed to open hostname for writing {}", hostname_filepath);
return false;
}
hostname_file << hostname_line;
}
if (!user::set_hosts(hostname, mountpoint)) {
@ -167,12 +163,10 @@ ff02::2 ip6-allrouters
{
const auto& hosts_filepath = fmt::format(FMT_COMPILE("{}/etc/hosts"), mountpoint);
const auto& hosts_text = fmt::format(FMT_COMPILE("{}{}"), STANDARD_HOSTS, hostname.empty() ? std::string{} : fmt::format(REQUESTED_HOST, hostname));
std::ofstream hosts_file{hosts_filepath, std::ios::out | std::ios::trunc};
if (!hosts_file.is_open()) {
if (!file_utils::create_file_for_overwrite(hosts_filepath, hosts_text)) {
spdlog::error("Failed to open hosts for writing {}", hosts_filepath);
return false;
}
hosts_file << hosts_text;
}
return true;
}