Skip to content

Commit

Permalink
Added filesystem error handling
Browse files Browse the repository at this point in the history
Signed-off-by: AssemblyJohn <[email protected]>
  • Loading branch information
AssemblyJohn committed May 6, 2024
1 parent b56760f commit ed2f85a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
2 changes: 2 additions & 0 deletions include/evse_security/utils/evse_filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 2 additions & 13 deletions lib/evse_security/certificate/x509_bundle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
56 changes: 43 additions & 13 deletions lib/evse_security/utils/evse_filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,69 @@ 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<char>(file)), std::istreambuf_iterator<char>());
return true;
if (file.is_open()) {
out_data = std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
return true;
}
}
} catch (const std::exception& e) {
EVLOG_error << "Error while reading from file: " << e.what();
}

EVLOG_error << "Error reading file: " << file_path;
return false;
}

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);
Expand Down

0 comments on commit ed2f85a

Please sign in to comment.