From 223406d4e45503318b197bd069d8f75335fee64f Mon Sep 17 00:00:00 2001 From: Timerix Date: Mon, 4 Aug 2025 19:15:30 +0300 Subject: [PATCH] added bool value return to dir_create --- include/tlibc/errors.h | 2 +- include/tlibc/filesystem.h | 5 +++-- src/filesystem/dir.c | 6 +++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/include/tlibc/errors.h b/include/tlibc/errors.h index 2b001a2..c796611 100755 --- a/include/tlibc/errors.h +++ b/include/tlibc/errors.h @@ -43,7 +43,7 @@ typedef struct Result_ { #define RESULT_ERROR(MSG, IS_MSG_ON_HEAP) (Result_){ .error = Error_create(MSG, IS_MSG_ON_HEAP, ErrorCallPos_here()) } #define RESULT_ERROR_FMT(FORMAT, ARGS...) RESULT_ERROR(sprintf_malloc(4096, FORMAT ,##ARGS), true) #define RESULT_ERROR_ERRNO() RESULT_ERROR(strerror(errno), false) -#define RESULT_VOID (Result_){ .error = NULL } +#define RESULT_VOID (Result_){ .error = NULL, .u = 0 } #define RESULT_VALUE(FIELD, V) (Result_){ .error = NULL, .FIELD = V } #define try(VAR, RSLT_CALL, DEFER_CODE) \ diff --git a/include/tlibc/filesystem.h b/include/tlibc/filesystem.h index 21eb88b..3a0c14c 100644 --- a/include/tlibc/filesystem.h +++ b/include/tlibc/filesystem.h @@ -123,5 +123,6 @@ static inline Result(void) file_readBytesArrayExactly(FILE* f, Array(u8) dst){ bool dir_exists(cstr path); -/// @brief creates directories specified in path recursively if they don't exist -Result(void) dir_create(cstr path); +/// @brief creates directories specified in path recursively +/// @return false if directory was present already, true if it has been created +Result(bool) dir_create(cstr path); diff --git a/src/filesystem/dir.c b/src/filesystem/dir.c index 8eb0b10..1bbe334 100644 --- a/src/filesystem/dir.c +++ b/src/filesystem/dir.c @@ -22,9 +22,9 @@ bool dir_exists(cstr path){ #endif } -Result(void) dir_create(cstr path){ +Result(bool) dir_create(cstr path){ if (dir_exists(path)) - return RESULT_VOID; + return RESULT_VALUE(i, false); char* parentDir= str_copy(path_dirname(str_from_cstr((void*)path))).data; try_void(dir_create(parentDir), free(parentDir)); @@ -38,5 +38,5 @@ Result(void) dir_create(cstr path){ return RESULT_ERROR_FMT("Can't create dicectory '%s': %s", path, strerror(errno)); } - return RESULT_VOID; + return RESULT_VALUE(i, true); }