Added a result system to keep better track of errors
This commit is contained in:
parent
85ad0ab103
commit
044f15dc3c
29
src/entry.c
29
src/entry.c
@ -20,4 +20,31 @@ bool is_entry_id_valid(const char* entry_id) {
|
||||
return false;
|
||||
|
||||
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;
|
||||
}
|
||||
|
15
src/entry.h
15
src/entry.h
@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
@ -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);
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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";
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user