Skip to content

Commit

Permalink
🧹 gucc: refactor file creation for overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
vnepogodin committed Jul 1, 2024
1 parent a527dc7 commit fc2c8ff
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 29 deletions.
8 changes: 6 additions & 2 deletions gucc/include/gucc/file_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 3 additions & 5 deletions gucc/src/crypttab.cpp
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -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 {

Check failure on line 105 in gucc/src/crypttab.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/gucc/src/crypttab.cpp:105:66 [bugprone-easily-swappable-parameters

2 adjacent parameters of 'generate_crypttab' of similar type ('std::string_view') are easily swapped by mistake
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;
}

Expand Down
13 changes: 11 additions & 2 deletions gucc/src/file_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Check failure on line 15 in gucc/src/file_utils.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/gucc/src/file_utils.cpp:15:5 [cppcoreguidelines-owning-memory

initializing non-owner 'FILE *' (aka '_IO_FILE *') with a newly created 'gsl::owner<>'
if (file == nullptr) {
Expand All @@ -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 {

Check failure on line 38 in gucc/src/file_utils.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/gucc/src/file_utils.cpp:38:20 [bugprone-easily-swappable-parameters

2 adjacent parameters of 'write_to_file' of similar type ('std::string_view') are easily swapped by mistake
std::ofstream file{filepath.data()};
if (!file.is_open()) {
spdlog::error("[WRITE_TO_FILE] '{}' open failed: {}", filepath, std::strerror(errno));
Expand All @@ -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 {

Check failure on line 48 in gucc/src/file_utils.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/gucc/src/file_utils.cpp:48:32 [bugprone-easily-swappable-parameters

2 adjacent parameters of 'create_file_for_overwrite' of similar type ('std::string_view') are easily swapped by mistake
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
8 changes: 3 additions & 5 deletions gucc/src/fstab.cpp
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -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;
}

Expand Down
7 changes: 2 additions & 5 deletions gucc/src/locale.cpp
Original file line number Diff line number Diff line change
@@ -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>

Expand Down Expand Up @@ -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
Expand Down
14 changes: 4 additions & 10 deletions gucc/src/user.cpp
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -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{};
Expand All @@ -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)) {
Expand All @@ -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;
}
Expand Down

0 comments on commit fc2c8ff

Please sign in to comment.