Added a result system to keep better track of errors

This commit is contained in:
Alexei KADIR 2024-02-17 00:45:44 +01:00
parent 85ad0ab103
commit 044f15dc3c
5 changed files with 50 additions and 8 deletions

View File

@ -21,3 +21,30 @@ bool is_entry_id_valid(const char* entry_id) {
return true; return true;
} }
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;
}

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include "utils.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
@ -10,3 +12,16 @@
/// @param entry_id The entry id to check. /// @param entry_id The entry id to check.
/// @return True if the entry id is valid, false otherwise. /// @return True if the entry id is valid, false otherwise.
bool is_entry_id_valid(const char* entry_id); 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);

View File

@ -120,9 +120,14 @@ int command_add_entry(int argc, char* argv[]) {
// Check if the entry already exists // Check if the entry already exists
bool exists; bool exists;
if (!entry_exists(entry_id, &exists)) if (entry_exists(entry_id, &exists) != SUCCESS)
return EXIT_FAILURE; return EXIT_FAILURE;
if (exists) {
log_message(LOG_LEVEL_ERROR, "Entry '%s' already exists.", entry_id);
return EXIT_FAILURE;
}
// Create the entry // Create the entry
// TODO // TODO

View File

@ -29,10 +29,6 @@ void log_message(LogLevel level, const char* format, ...) {
color = "\033[0;90m"; color = "\033[0;90m";
level_str = "DEBUG"; level_str = "DEBUG";
break; break;
case LOG_LEVEL_INFO:
color = "\033[0;32m";
level_str = "INFO";
break;
case LOG_LEVEL_WARNING: case LOG_LEVEL_WARNING:
color = "\033[0;33m"; color = "\033[0;33m";
level_str = "WARNING"; level_str = "WARNING";

View File

@ -10,7 +10,6 @@ typedef enum {
typedef enum { typedef enum {
LOG_LEVEL_DEBUG, LOG_LEVEL_DEBUG,
LOG_LEVEL_INFO,
LOG_LEVEL_WARNING, LOG_LEVEL_WARNING,
LOG_LEVEL_ERROR, LOG_LEVEL_ERROR,
} LogLevel; } LogLevel;
@ -28,7 +27,7 @@ void set_log_level(LogLevel level);
void log_message(LogLevel level, const char* format, ...); void log_message(LogLevel level, const char* format, ...);
/// @brief Formats a string. /// @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 fmt The format string.
/// @param ... The format arguments. /// @param ... The format arguments.
/// @return The result of the operation. /// @return The result of the operation.