Commit 45d387ac by Vicent Martí

refs: Error handling rework. WIP

parent 60bc2d20
...@@ -122,9 +122,12 @@ typedef struct { ...@@ -122,9 +122,12 @@ typedef struct {
typedef enum { typedef enum {
GITERR_NOMEMORY, GITERR_NOMEMORY,
GITERR_REFERENCE,
} git_error_class; } git_error_class;
#define GITERR_CHECK_ALLOC(ptr, error) if (ptr == NULL) { giterr_set_oom(error); return -1 }
GIT_EXTERN(void) giterr_set(git_error **error_out, int error_class, const char *string, ...); GIT_EXTERN(void) giterr_set(git_error **error_out, int error_class, const char *string, ...);
GIT_EXTERN(void) giterr_set_oom(git_error **error); GIT_EXTERN(void) giterr_set_oom(git_error **error);
GIT_EXTERN(void) giterr_free(git_error *error); GIT_EXTERN(void) giterr_free(git_error *error);
......
...@@ -486,20 +486,23 @@ GIT_INLINE(int) is_dot_or_dotdot(const char *name) ...@@ -486,20 +486,23 @@ GIT_INLINE(int) is_dot_or_dotdot(const char *name)
int git_path_direach( int git_path_direach(
git_buf *path, git_buf *path,
int (*fn)(void *, git_buf *), int (*fn)(void *, git_buf *, git_error **),
void *arg) void *arg,
git_error **error)
{ {
ssize_t wd_len; ssize_t wd_len;
DIR *dir; DIR *dir;
struct dirent de_buf, *de; struct dirent de_buf, *de;
if (git_path_to_dir(path) < GIT_SUCCESS) if (git_path_to_dir(path, error) < 0)
return git_buf_lasterror(path); return -1;
wd_len = path->size; wd_len = path->size;
dir = opendir(path->ptr); dir = opendir(path->ptr);
if (!dir) if (!dir) {
return git__throw(GIT_EOSERR, "Failed to process `%s` tree structure. An error occured while opening the directory", path->ptr); giterr_set(error, GITERR_OS, "Failed to `opendir` %s: %s", path->ptr, strerror(errno));
return -1;
}
while (p_readdir_r(dir, &de_buf, &de) == 0 && de != NULL) { while (p_readdir_r(dir, &de_buf, &de) == 0 && de != NULL) {
int result; int result;
...@@ -507,16 +510,18 @@ int git_path_direach( ...@@ -507,16 +510,18 @@ int git_path_direach(
if (is_dot_or_dotdot(de->d_name)) if (is_dot_or_dotdot(de->d_name))
continue; continue;
if (git_buf_puts(path, de->d_name) < GIT_SUCCESS) if (git_buf_puts(path, de->d_name) < 0) {
return git_buf_lasterror(path); giterr_set_oom(error);
return -1;
}
result = fn(arg, path); result = fn(arg, path, error);
git_buf_truncate(path, wd_len); /* restore path */ git_buf_truncate(path, wd_len); /* restore path */
if (result != GIT_SUCCESS) { if (result < 0) {
closedir(dir); closedir(dir);
return result; /* The callee is reponsible for setting the correct error message */ return -1;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment