From 044f15dc3c068e48eff4fae49e69c2c5589f2874 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexe=C3=AF=20KADIR?= Date: Sat, 17 Feb 2024 00:45:44 +0100 Subject: [PATCH] Added a result system to keep better track of errors --- src/entry.c | 29 ++++++++++++++++++++++++++++- src/entry.h | 15 +++++++++++++++ src/sandbox.c | 7 ++++++- src/utils.c | 4 ---- src/utils.h | 3 +-- 5 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/entry.c b/src/entry.c index 83a15ab..24b001a 100644 --- a/src/entry.c +++ b/src/entry.c @@ -20,4 +20,31 @@ bool is_entry_id_valid(const char* entry_id) { return false; return true; -} \ No newline at end of file +} + +Result get_entry_path(const char* entry_id, char** out_path) { + *out_path = NULL; + + if (!is_entry_id_valid(entry_id)) + return FAILURE; + + return format(out_path, "%s/%s", ENTRY_POOL_DIR, entry_id); +} + +Result entry_exists(const char* entry_id, bool* out_exists) { + *out_exists = false; + + char* path; + Result result = get_entry_path(entry_id, &path); + if (result != SUCCESS) + return result; + + struct stat st; + bool exists = stat(path, &st) == 0 && S_ISDIR(st.st_mode); + + free(path); + + *out_exists = exists; + + return SUCCESS; +} diff --git a/src/entry.h b/src/entry.h index f6690da..b9a87ef 100644 --- a/src/entry.h +++ b/src/entry.h @@ -1,5 +1,7 @@ #pragma once +#include "utils.h" + #include #include @@ -10,3 +12,16 @@ /// @param entry_id The entry id to check. /// @return True if the entry id is valid, false otherwise. bool is_entry_id_valid(const char* entry_id); + +/// @brief Gets the path of the given entry. +/// @param entry_id The entry id. +/// @param out_path The pointer to the output path string. +/// @return The result of the operation. +Result get_entry_path(const char* entry_id, char** out_path); + +/// @brief Checks whether the given entry exists. +/// @param entry_id The entry id. +/// @param out_exists The pointer to the output boolean. +/// @return The result of the operation. +Result entry_exists(const char* entry_id, bool* out_exists); + diff --git a/src/sandbox.c b/src/sandbox.c index 5337409..67ee4d7 100644 --- a/src/sandbox.c +++ b/src/sandbox.c @@ -120,9 +120,14 @@ int command_add_entry(int argc, char* argv[]) { // Check if the entry already exists bool exists; - if (!entry_exists(entry_id, &exists)) + if (entry_exists(entry_id, &exists) != SUCCESS) return EXIT_FAILURE; + if (exists) { + log_message(LOG_LEVEL_ERROR, "Entry '%s' already exists.", entry_id); + return EXIT_FAILURE; + } + // Create the entry // TODO diff --git a/src/utils.c b/src/utils.c index ee328cb..06fc68b 100644 --- a/src/utils.c +++ b/src/utils.c @@ -29,10 +29,6 @@ void log_message(LogLevel level, const char* format, ...) { color = "\033[0;90m"; level_str = "DEBUG"; break; - case LOG_LEVEL_INFO: - color = "\033[0;32m"; - level_str = "INFO"; - break; case LOG_LEVEL_WARNING: color = "\033[0;33m"; level_str = "WARNING"; diff --git a/src/utils.h b/src/utils.h index 3f51dcb..b69d8f4 100644 --- a/src/utils.h +++ b/src/utils.h @@ -10,7 +10,6 @@ typedef enum { typedef enum { LOG_LEVEL_DEBUG, - LOG_LEVEL_INFO, LOG_LEVEL_WARNING, LOG_LEVEL_ERROR, } LogLevel; @@ -28,7 +27,7 @@ void set_log_level(LogLevel level); void log_message(LogLevel level, const char* format, ...); /// @brief Formats a string. -/// @param out_string The output string pointer. +/// @param out_string The pointer to the output string. /// @param fmt The format string. /// @param ... The format arguments. /// @return The result of the operation.