diff options
| author | Vicent Martà <tanoku@gmail.com> | 2012-02-17 00:13:34 +0100 |
|---|---|---|
| committer | Vicent Martà <tanoku@gmail.com> | 2012-03-06 00:43:10 +0100 |
| commit | 1a48112342932e9fcd45a1ff5935f1c9c53b83d1 (patch) | |
| tree | fbb18cfe64e65025c6e1790972d1a106eea4cc54 /src/fileops.c | |
| parent | 45d387ac78bcf3167d69b736d0b322717bc492d4 (diff) | |
| download | libgit2-1a48112342932e9fcd45a1ff5935f1c9c53b83d1.tar.gz | |
error-handling: References
Yes, this is error handling solely for `refs.c`, but some of the
abstractions leak all ofer the code base.
Diffstat (limited to 'src/fileops.c')
| -rw-r--r-- | src/fileops.c | 73 |
1 files changed, 44 insertions, 29 deletions
diff --git a/src/fileops.c b/src/fileops.c index 856823afb..ffaf8319d 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -10,25 +10,19 @@ int git_futils_mkpath2file(const char *file_path, const mode_t mode) { - int error; + int result = 0; git_buf target_folder = GIT_BUF_INIT; - error = git_path_dirname_r(&target_folder, file_path); - if (error < GIT_SUCCESS) { - git_buf_free(&target_folder); - return git__throw(GIT_EINVALIDPATH, "Failed to recursively build `%s` tree structure. Unable to parse parent folder name", file_path); - } else { - /* reset error */ - error = GIT_SUCCESS; - } + if (git_path_dirname_r(&target_folder, file_path) < 0) + return -1; /* Does the containing folder exist? */ - if (git_path_isdir(target_folder.ptr) != GIT_SUCCESS) + if (git_path_isdir(target_folder.ptr) == false) /* Let's create the tree structure */ - error = git_futils_mkdir_r(target_folder.ptr, NULL, mode); + result = git_futils_mkdir_r(target_folder.ptr, NULL, mode); git_buf_free(&target_folder); - return error; + return result; } int git_futils_mktmp(git_buf *path_out, const char *filename) @@ -39,33 +33,50 @@ int git_futils_mktmp(git_buf *path_out, const char *filename) git_buf_puts(path_out, "_git2_XXXXXX"); if (git_buf_oom(path_out)) - return git__rethrow(git_buf_lasterror(path_out), - "Failed to create temporary file for %s", filename); + return -1; - if ((fd = p_mkstemp(path_out->ptr)) < 0) - return git__throw(GIT_EOSERR, "Failed to create temporary file %s", path_out->ptr); + if ((fd = p_mkstemp(path_out->ptr)) < 0) { + giterr_set(GITERR_OS, + "Failed to create temporary file '%s': %s", path_out->ptr, strerror(errno)); + return -1; + } return fd; } int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode) { - if (git_futils_mkpath2file(path, dirmode) < GIT_SUCCESS) - return git__throw(GIT_EOSERR, "Failed to create file %s", path); + int fd; - return p_creat(path, mode); + if (git_futils_mkpath2file(path, dirmode) < 0) + return -1; + + fd = p_creat(path, mode); + if (fd < 0) { + giterr_set(GITERR_OS, + "Failed to create file '%s': %s", path, strerror(errno)); + return -1; + } + + return fd; } int git_futils_creat_locked(const char *path, const mode_t mode) { int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_EXCL, mode); - return fd >= 0 ? fd : git__throw(GIT_EOSERR, "Failed to create locked file. Could not open %s", path); + if (fd < 0) { + giterr_set(GITERR_OS, + "Failed to create locked file '%s': %s", path, strerror(errno)); + return -1; + } + + return fd; } int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode) { - if (git_futils_mkpath2file(path, dirmode) < GIT_SUCCESS) - return git__throw(GIT_EOSERR, "Failed to create locked file %s", path); + if (git_futils_mkpath2file(path, dirmode) < 0) + return -1; return git_futils_creat_locked(path, mode); } @@ -105,12 +116,14 @@ int git_futils_readbuffer_updated(git_buf *buf, const char *path, time_t *mtime, *updated = 0; if ((fd = p_open(path, O_RDONLY)) < 0) { - return git__throw(GIT_ENOTFOUND, "Failed to read file '%s': %s", path, strerror(errno)); + giterr_set(GITERR_OS, "Failed to read file '%s': %s", path, strerror(errno)); + return (errno == ENOENT) ? GIT_ENOTFOUND : -1; } if (p_fstat(fd, &st) < 0 || S_ISDIR(st.st_mode) || !git__is_sizet(st.st_size+1)) { close(fd); - return git__throw(GIT_EOSERR, "Failed to stat file '%s'", path); + giterr_set(GITERR_OS, "Invalid regular file stat for '%s'", path); + return -1; } /* @@ -141,7 +154,9 @@ int git_futils_readbuffer_updated(git_buf *buf, const char *path, time_t *mtime, if (read_size < 0) { close(fd); - return git__throw(GIT_EOSERR, "Failed to read from FD"); + giterr_set(GITERR_OS, "Failed to read descriptor for %s: %s", + path, strerror(errno)); + return -1; } len -= read_size; @@ -218,7 +233,7 @@ int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode) pp += root_path_offset; /* On Windows, will skip the drive name (eg. C: or D:) */ while (error == GIT_SUCCESS && (sp = strchr(pp, '/')) != NULL) { - if (sp != pp && git_path_isdir(make_path.ptr) < GIT_SUCCESS) { + if (sp != pp && git_path_isdir(make_path.ptr) == false) { *sp = 0; error = p_mkdir(make_path.ptr, mode); @@ -251,7 +266,7 @@ static int _rmdir_recurs_foreach(void *opaque, git_buf *path) int error = GIT_SUCCESS; int force = *(int *)opaque; - if (git_path_isdir(path->ptr) == GIT_SUCCESS) { + if (git_path_isdir(path->ptr) == true) { error = git_path_direach(path, _rmdir_recurs_foreach, opaque); if (error < GIT_SUCCESS) return git__rethrow(error, "Failed to remove directory `%s`", path->ptr); @@ -293,7 +308,7 @@ int git_futils_find_global_file(git_buf *path, const char *filename) if ((error = git_buf_joinpath(path, home, filename)) < GIT_SUCCESS) return error; - if (git_path_exists(path->ptr) < GIT_SUCCESS) { + if (git_path_exists(path->ptr) == false) { git_buf_clear(path); return GIT_ENOTFOUND; } @@ -395,7 +410,7 @@ int git_futils_find_system_file(git_buf *path, const char *filename) if (git_buf_joinpath(path, "/etc", filename) < GIT_SUCCESS) return git_buf_lasterror(path); - if (git_path_exists(path->ptr) == GIT_SUCCESS) + if (git_path_exists(path->ptr) == true) return GIT_SUCCESS; git_buf_clear(path); |
