diff --git a/include/evse_security/utils/evse_filesystem.hpp b/include/evse_security/utils/evse_filesystem.hpp index 67b9afb..997780b 100644 --- a/include/evse_security/utils/evse_filesystem.hpp +++ b/include/evse_security/utils/evse_filesystem.hpp @@ -14,6 +14,8 @@ bool is_subdirectory(const fs::path& base, const fs::path& subdir); /// @brief Should be used to ensure file exists, not for directories bool create_file_if_nonexistent(const fs::path& file_path); +/// @brief Ensure a file exists (if there's an extension), or a directory if no extension is found +bool create_file_or_dir_if_nonexistent(const fs::path& file_path); bool delete_file(const fs::path& file_path); bool read_from_file(const fs::path& file_path, std::string& out_data); diff --git a/lib/evse_security/certificate/x509_bundle.cpp b/lib/evse_security/certificate/x509_bundle.cpp index 1e8a43c..6cfea58 100644 --- a/lib/evse_security/certificate/x509_bundle.cpp +++ b/lib/evse_security/certificate/x509_bundle.cpp @@ -45,19 +45,8 @@ X509CertificateBundle::X509CertificateBundle(const fs::path& path, const Encodin hierarchy_invalidated(true) { this->path = path; - // In case the path is missing, create it - if (fs::exists(path) == false) { - if (path.has_extension()) { - if (path.extension() == PEM_EXTENSION) { - // Create file if we have an PEM extension - std::ofstream new_file(path.c_str()); - new_file.close(); - } - } else { - // Else create a directory - fs::create_directories(path); - } - } + // Attempt creation + filesystem_utils::create_file_or_dir_if_nonexistent(path); if (fs::is_directory(path)) { source = X509CertificateSource::DIRECTORY; diff --git a/lib/evse_security/utils/evse_filesystem.cpp b/lib/evse_security/utils/evse_filesystem.cpp index 65dc20c..7bce50d 100644 --- a/lib/evse_security/utils/evse_filesystem.cpp +++ b/lib/evse_security/utils/evse_filesystem.cpp @@ -18,21 +18,30 @@ bool is_subdirectory(const fs::path& base, const fs::path& subdir) { } bool delete_file(const fs::path& file_path) { - if (fs::is_regular_file(file_path)) - return fs::remove(file_path); + try { + if (fs::is_regular_file(file_path)) { + return fs::remove(file_path); + } + } catch (const std::exception& e) { + EVLOG_error << "Filesystem error: " << e.what(); + } EVLOG_error << "Error deleting file: " << file_path; return false; } bool read_from_file(const fs::path& file_path, std::string& out_data) { - if (fs::is_regular_file(file_path)) { - fsstd::ifstream file(file_path, std::ios::binary); + try { + if (fs::is_regular_file(file_path)) { + fsstd::ifstream file(file_path, std::ios::binary); - if (file.is_open()) { - out_data = std::string((std::istreambuf_iterator(file)), std::istreambuf_iterator()); - return true; + if (file.is_open()) { + out_data = std::string((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + return true; + } } + } catch (const std::exception& e) { + EVLOG_error << "Error while reading from file: " << e.what(); } EVLOG_error << "Error reading file: " << file_path; @@ -40,17 +49,38 @@ bool read_from_file(const fs::path& file_path, std::string& out_data) { } bool create_file_if_nonexistent(const fs::path& file_path) { - if (!fs::exists(file_path)) { - std::ofstream file(file_path); - return true; - } else if (fs::is_directory(file_path)) { - EVLOG_error << "Attempting to create file over existing directory: " << file_path; - return false; + try { + if (!fs::exists(file_path)) { + std::ofstream file(file_path); + return true; + } else if (fs::is_directory(file_path)) { + EVLOG_error << "Attempting to create file over existing directory: " << file_path; + return false; + } + } catch (const std::exception& e) { + EVLOG_error << "Error while creating file: " << e.what(); } return true; } +bool create_file_or_dir_if_nonexistent(const fs::path& path) { + try { + // In case the path is missing, create it + if (fs::exists(path) == false) { + if (path.has_extension()) { + std::ofstream new_file(path.c_str()); + new_file.close(); + } else { + // Else create a directory + fs::create_directories(path); + } + } + } catch (const std::exception& e) { + EVLOG_error << "Error while creating dir/file: " << e.what(); + } +} + bool write_to_file(const fs::path& file_path, const std::string& data, std::ios::openmode mode) { try { fsstd::ofstream fs(file_path, mode | std::ios::binary);