fileops.c: Move to new error handling mechanism
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
diff --git a/src/fileops.c b/src/fileops.c
index 45dd8f4..3402df4 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -36,32 +36,32 @@ int gitfo_mktemp(char *path_out, const char *filename)
/* FIXME: there may be race conditions when multi-threading
* with the library */
if (_mktemp_s(path_out, GIT_PATH_MAX) != 0)
- return GIT_EOSERR;
+ return git__throw(GIT_EOSERR, "Failed to make temporary file %s", path_out);
fd = gitfo_creat(path_out, 0744);
#else
fd = mkstemp(path_out);
#endif
- return fd >= 0 ? fd : GIT_EOSERR;
+ return fd >= 0 ? fd : git__throw(GIT_EOSERR, "Failed to create temporary file %s", path_out);
}
int gitfo_open(const char *path, int flags)
{
int fd = open(path, flags | O_BINARY);
- return fd >= 0 ? fd : GIT_EOSERR;
+ return fd >= 0 ? fd : git__throw(GIT_EOSERR, "Failed to open %s", path);
}
int gitfo_creat(const char *path, int mode)
{
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode);
- return fd >= 0 ? fd : GIT_EOSERR;
+ return fd >= 0 ? fd : git__throw(GIT_EOSERR, "Failed to create file. Could not open %s", path);
}
int gitfo_creat_force(const char *path, int mode)
{
if (gitfo_mkdir_2file(path) < GIT_SUCCESS)
- return GIT_EOSERR;
+ return git__throw(GIT_EOSERR, "Failed to create file %s", path);
return gitfo_creat(path, mode);
}
@@ -74,11 +74,11 @@ int gitfo_read(git_file fd, void *buf, size_t cnt)
if (r < 0) {
if (errno == EINTR || errno == EAGAIN)
continue;
- return GIT_EOSERR;
+ return git__throw(GIT_EOSERR, "Failed to read from file");
}
if (!r) {
errno = EPIPE;
- return GIT_EOSERR;
+ return git__throw(GIT_EOSERR, "Failed to read from file");
}
cnt -= r;
b += r;
@@ -94,11 +94,11 @@ int gitfo_write(git_file fd, void *buf, size_t cnt)
if (r < 0) {
if (errno == EINTR || errno == EAGAIN)
continue;
- return GIT_EOSERR;
+ return git__throw(GIT_EOSERR, "Failed to write to file");
}
if (!r) {
errno = EPIPE;
- return GIT_EOSERR;
+ return git__throw(GIT_EOSERR, "Failed to write to file");
}
cnt -= r;
b += r;
@@ -112,7 +112,7 @@ int gitfo_isdir(const char *path)
int len, stat_error;
if (!path)
- return GIT_ENOTFOUND;
+ return git__throw(GIT_ENOTFOUND, "No path given to gitfo_isdir");
len = strlen(path);
@@ -128,10 +128,10 @@ int gitfo_isdir(const char *path)
}
if (stat_error < GIT_SUCCESS)
- return GIT_ENOTFOUND;
+ return git__throw(GIT_ENOTFOUND, "%s does not exist", path);
if (!S_ISDIR(st.st_mode))
- return GIT_ENOTFOUND;
+ return git__throw(GIT_ENOTFOUND, "%s is not a file", path);
return GIT_SUCCESS;
}
@@ -146,7 +146,7 @@ git_off_t gitfo_size(git_file fd)
{
struct stat sb;
if (gitfo_fstat(fd, &sb))
- return GIT_EOSERR;
+ return git__throw(GIT_EOSERR, "Failed to get size of file. File missing or corrupted");
return sb.st_size;
}
@@ -160,7 +160,7 @@ int gitfo_read_file(gitfo_buf *obj, const char *path)
assert(obj && path && *path);
if ((fd = gitfo_open(path, O_RDONLY)) < 0)
- return GIT_ERROR;
+ return git__throw(GIT_ERROR, "Failed to open %s for reading", path);
if (((size = gitfo_size(fd)) < 0) || !git__is_sizet(size+1)) {
gitfo_close(fd);
@@ -287,6 +287,8 @@ int gitfo_flush_cached(gitfo_cache *ioc)
ioc->pos = 0;
}
+ if (result < GIT_SUCCESS)
+ return git__rethrow(result, "Failed to flush cache");
return result;
}
@@ -325,7 +327,7 @@ int gitfo_close_cached(gitfo_cache *ioc)
git_file fd;
if (gitfo_flush_cached(ioc) < GIT_SUCCESS)
- return GIT_ERROR;
+ return git__throw(GIT_ERROR, "Failed to close cache. Could not flush cache");
fd = ioc->fd;
free(ioc->cache);
@@ -472,7 +474,7 @@ static int retrieve_previous_path_component_start(const char *path)
offset--;
if (offset < root_offset)
- return GIT_ERROR;
+ return git__throw(GIT_ERROR, "Failed to retrieve path component. Wrong offset");
while (offset > start && path[offset-1] != '/') {
offset--;