Skip to content

Commit

Permalink
Fix copyFile implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Hilst committed Jan 10, 2025
1 parent 8712198 commit c776c8f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
8 changes: 8 additions & 0 deletions include/cloysterhpc/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ std::string getCurrentTimestamp();
std::string findAndReplace(const std::string_view& source,
const std::string_view& find, const std::string_view& replace);

/**
* @brief Copies a file, ignore if it exists
*
* @param dir_entry
* @param string The string to add to the file.
*/
void copyFile(std::filesystem::path from, std::filesystem::path to);

} /* namespace cloyster */

#endif // CLOYSTERHPC_FUNCTIONS_H_
7 changes: 1 addition & 6 deletions include/cloysterhpc/services/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@
if (spdlog::get(productName) != nullptr) { \
spdlog::get(productName)->info(__VA_ARGS__); \
}
#define LOG_ABORT_IF(cnd, msg) \
if ((cnd) && spdlog::get(productName) != nullptr) { \
LOG_CRITICAL("ABORT - {}\n\t{}\n\tin file: {}\n\ton line: {}", #cnd, \
msg, __FILE__, __LINE__); \
LOG_BREAK; \
}

// Available only with DEBUG builds
#ifndef NDEBUG
#define LOG_DEBUG(...) \
Expand Down
20 changes: 20 additions & 0 deletions src/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,24 @@ std::string findAndReplace(const std::string_view& source,
return result;
}

/// Copy a file, ignore if file exists
void copyFile(std::filesystem::path from, std::filesystem::path to)
{
if (cloyster::dryRun) {
LOG_INFO("Would copy file {}, to {}", from.string(), to.string())
return;
}

try {
std::filesystem::copy(from, to);
} catch (std::filesystem::filesystem_error const& ex) {
if (ex.code().default_error_condition() == std::errc::file_exists) {
LOG_WARN("File {} exists, skiping copying", to.string());
return;
}

throw ex;
}
}

} // namespace cloyster
10 changes: 7 additions & 3 deletions src/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,13 @@ void OS::setVersion(const std::string& version)
setMajorVersion(static_cast<unsigned>(
std::stoul(version.substr(0, version.find('.')))));

LOG_ABORT_IF(version.find('.') == std::string::npos,
"system version (in the answerfile.yml) must follow the format M.N, "
"where M is the major version number and N is the minor version number.");
// We expect the system.version in the answerfile
// to be in the format M.N, and abort if it is not valid
if (version.find('.') == std::string::npos) {
LOG_CRITICAL(
"Unexpected value for system.version (in asnwerfile.ini). Expected M.N format, e.g., 9.5, value found instead: {}", version);
std::exit(-1);
}

setMinorVersion(
static_cast<unsigned>(stoul(version.substr(version.find('.') + 1))));
Expand Down
8 changes: 1 addition & 7 deletions src/repos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,7 @@ void RepoManager::commitStatus()

for (auto const& dir_entry :
std::filesystem::directory_iterator { tmpdir }) {
try {
std::filesystem::copy(dir_entry, "/etc/yum.repos.d");
} catch (std::filesystem::filesystem_error const& ex) {
if (ex.code().message().starts_with("File exists")) {
continue;
}
}
cloyster::copyFile(dir_entry, "/etc/yum.repos.d");
}

std::vector<std::string> to_enable;
Expand Down

0 comments on commit c776c8f

Please sign in to comment.