diff options
author | Russell Belfer <rb@github.com> | 2013-12-06 15:07:57 -0800 |
---|---|---|
committer | Russell Belfer <rb@github.com> | 2013-12-11 10:57:49 -0800 |
commit | 25e0b1576d5f9e5248603f81d3198a65bfccf0ed (patch) | |
tree | dac0da29acfbd6478bed93bcab3557e2877dabaa /src/fileops.c | |
parent | fcd324c625d8be3f368c924d787e945e5812d8dd (diff) | |
download | libgit2-25e0b1576d5f9e5248603f81d3198a65bfccf0ed.tar.gz |
Remove converting user error to GIT_EUSER
This changes the behavior of callbacks so that the callback error
code is not converted into GIT_EUSER and instead we propagate the
return value through to the caller. Instead of using the
giterr_capture and giterr_restore functions, we now rely on all
functions to pass back the return value from a callback.
To avoid having a return value with no error message, the user
can call the public giterr_set_str or some such function to set
an error message. There is a new helper 'giterr_set_callback'
that functions can invoke after making a callback which ensures
that some error message was set in case the callback did not set
one.
In places where the sign of the callback return value is
meaningful (e.g. positive to skip, negative to abort), only the
negative values are returned back to the caller, obviously, since
the other values allow for continuing the loop.
The hardest parts of this were in the checkout code where positive
return values were overloaded as meaningful values for checkout.
I fixed this by adding an output parameter to many of the internal
checkout functions and removing the overload. This added some
code, but it is probably a better implementation.
There is some funkiness in the network code where user provided
callbacks could be returning a positive or a negative value and
we want to rely on that to cancel the loop. There are still a
couple places where an user error might get turned into GIT_EUSER
there, I think, though none exercised by the tests.
Diffstat (limited to 'src/fileops.c')
-rw-r--r-- | src/fileops.c | 47 |
1 files changed, 17 insertions, 30 deletions
diff --git a/src/fileops.c b/src/fileops.c index 0418e9e52..98dcd3269 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -404,7 +404,6 @@ typedef struct { size_t baselen; uint32_t flags; int depth; - git_error_state error; } futils__rmdir_data; #define FUTILS_MAX_DEPTH 100 @@ -474,16 +473,14 @@ static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path) data->depth++; error = git_path_direach(path, 0, futils__rmdir_recurs_foreach, data); - if (error == GIT_EUSER) - return error; data->depth--; if (error < 0) - goto done; + return error; if (data->depth == 0 && (data->flags & GIT_RMDIR_SKIP_ROOT) != 0) - goto done; + return error; if ((error = p_rmdir(path->ptr)) < 0) { if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) != 0 && @@ -502,8 +499,7 @@ static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path) else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0) error = futils__error_cannot_rmdir(path->ptr, "still present"); -done: - return giterr_capture(&data->error, error); + return error; } static int futils__rmdir_empty_parent(void *opaque, git_buf *path) @@ -512,9 +508,9 @@ static int futils__rmdir_empty_parent(void *opaque, git_buf *path) int error = 0; if (git_buf_len(path) <= data->baselen) - return giterr_capture(&data->error, GIT_ITEROVER); + error = GIT_ITEROVER; - if (p_rmdir(git_buf_cstr(path)) < 0) { + else if (p_rmdir(git_buf_cstr(path)) < 0) { int en = errno; if (en == ENOENT || en == ENOTDIR) { @@ -526,7 +522,7 @@ static int futils__rmdir_empty_parent(void *opaque, git_buf *path) } } - return giterr_capture(&data->error, error); + return error; } int git_futils_rmdir_r( @@ -552,10 +548,10 @@ int git_futils_rmdir_r( error = git_path_walk_up( &fullpath, base, futils__rmdir_empty_parent, &data); - if (error == GIT_EUSER) - error = giterr_restore(&data.error); - if (error == GIT_ITEROVER) + if (error == GIT_ITEROVER) { + giterr_clear(); error = 0; + } git_buf_free(&fullpath); @@ -859,7 +855,6 @@ typedef struct { uint32_t flags; uint32_t mkdir_flags; mode_t dirmode; - git_error_state error; } cp_r_info; #define GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT (1u << 10) @@ -899,19 +894,19 @@ static int _cp_r_callback(void *ref, git_buf *from) if ((error = git_buf_joinpath( &info->to, info->to_root, from->ptr + info->from_prefix)) < 0) - goto done; + return error; if (!(error = git_path_lstat(info->to.ptr, &to_st))) exists = true; else if (error != GIT_ENOTFOUND) - goto done; + return error; else { giterr_clear(); error = 0; } if ((error = git_path_lstat(from->ptr, &from_st)) < 0) - goto done; + return error; if (S_ISDIR(from_st.st_mode)) { mode_t oldmode = info->dirmode; @@ -925,16 +920,13 @@ static int _cp_r_callback(void *ref, git_buf *from) error = _cp_r_mkdir(info, from); /* recurse onto target directory */ - if (!error && (!exists || S_ISDIR(to_st.st_mode))) { + if (!error && (!exists || S_ISDIR(to_st.st_mode))) error = git_path_direach(from, 0, _cp_r_callback, info); - if (error == GIT_EUSER) - return error; - } if (oldmode != 0) info->dirmode = oldmode; - goto done; + return error; } if (exists) { @@ -944,8 +936,7 @@ static int _cp_r_callback(void *ref, git_buf *from) if (p_unlink(info->to.ptr) < 0) { giterr_set(GITERR_OS, "Cannot overwrite existing file '%s'", info->to.ptr); - error = -1; - goto done; + return GIT_EEXISTS; } } @@ -958,7 +949,7 @@ static int _cp_r_callback(void *ref, git_buf *from) /* Make container directory on demand if needed */ if ((info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0 && (error = _cp_r_mkdir(info, from)) < 0) - goto done; + return error; /* make symlink or regular file */ if (S_ISLNK(from_st.st_mode)) @@ -972,8 +963,7 @@ static int _cp_r_callback(void *ref, git_buf *from) error = git_futils_cp(from->ptr, info->to.ptr, usemode); } -done: - return giterr_capture(&info->error, error); + return error; } int git_futils_cp_r( @@ -1015,9 +1005,6 @@ int git_futils_cp_r( git_buf_free(&path); git_buf_free(&info.to); - if (error == GIT_EUSER) - error = giterr_restore(&info.error); - return error; } |