Set GIT_EOSERR when the OS errno should be consulted This error code indicates the OS error code has a better value describing the last error, as it is likely a network or local file IO problem identified by a C library function call. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
diff --git a/src/errors.c b/src/errors.c
index deb106b..f348997 100644
--- a/src/errors.c
+++ b/src/errors.c
@@ -41,6 +41,9 @@ static struct {
const char *git_strerror(int num)
{
int i;
+
+ if (num == GIT_EOSERR)
+ return strerror(errno);
for (i = 0; i < ARRAY_SIZE(error_codes); i++)
if (num == error_codes[i].num)
return error_codes[i].str;
diff --git a/src/errors.h b/src/errors.h
index caebc63..ab415d5 100644
--- a/src/errors.h
+++ b/src/errors.h
@@ -11,6 +11,11 @@ GIT_INLINE(int) git_int_error(int code)
return code;
}
+GIT_INLINE(int) git_os_error(void)
+{
+ return git_int_error(GIT_EOSERR);
+}
+
GIT_INLINE(void) *git_ptr_error(int code)
{
git_errno = code;
diff --git a/src/fileops.c b/src/fileops.c
index 6522d02..1dd35dc 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -1,6 +1,18 @@
#include "common.h"
#include "fileops.h"
+int gitfo_open(const char *path, int flags)
+{
+ int fd = open(path, flags);
+ return fd >= 0 ? fd : git_os_error();
+}
+
+int gitfo_creat(const char *path, int mode)
+{
+ int fd = creat(path, mode);
+ return fd >= 0 ? fd : git_os_error();
+}
+
int gitfo_read(git_file fd, void *buf, size_t cnt)
{
char *b = buf;
@@ -9,11 +21,11 @@ int gitfo_read(git_file fd, void *buf, size_t cnt)
if (r < 0) {
if (errno == EINTR || errno == EAGAIN)
continue;
- return -1;
+ return git_os_error();
}
if (!r) {
errno = EPIPE;
- return -1;
+ return git_os_error();
}
cnt -= r;
b += r;
@@ -29,11 +41,11 @@ int gitfo_write(git_file fd, void *buf, size_t cnt)
if (r < 0) {
if (errno == EINTR || errno == EAGAIN)
continue;
- return -1;
+ return git_os_error();
}
if (!r) {
errno = EPIPE;
- return -1;
+ return git_os_error();
}
cnt -= r;
b += r;
@@ -43,9 +55,9 @@ int gitfo_write(git_file fd, void *buf, size_t cnt)
off_t gitfo_size(git_file fd)
{
- gitfo_statbuf sb;
+ struct stat sb;
if (fstat(fd, &sb))
- return -1;
+ return git_os_error();
return sb.st_size;
}
@@ -166,7 +178,7 @@ int gitfo_close_cached(gitfo_cache *ioc)
git_file fd;
if (gitfo_flush_cached(ioc) < 0)
- return -1;
+ return GIT_ERROR;
fd = ioc->fd;
free(ioc->cache);
@@ -201,7 +213,7 @@ int git_foreach_dirent(const char *wd, int (*fn)(void *, const char *), void *ar
dir = opendir(wd);
if (!dir)
- return GIT_ERROR;
+ return git_os_error();
while ((de = readdir(dir))) {
size_t de_len;
diff --git a/src/fileops.h b/src/fileops.h
index 9de0a70..336ab27 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -24,7 +24,6 @@
#define GITFO_BUF_INIT {NULL, 0}
typedef int git_file;
-typedef struct stat gitfo_statbuf;
typedef struct gitfo_cache gitfo_cache;
typedef struct { /* file io buffer */
@@ -33,18 +32,13 @@ typedef struct { /* file io buffer */
} gitfo_buf;
-#define gitfo_open(path, flags) open(path, flags)
-#define gitfo_creat(path, mode) creat(path, mode)
+extern int gitfo_open(const char *path, int flags);
+extern int gitfo_creat(const char *path, int mode);
#define gitfo_close(fd) close(fd)
extern int gitfo_read(git_file fd, void *buf, size_t cnt);
extern int gitfo_write(git_file fd, void *buf, size_t cnt);
-
extern off_t gitfo_size(git_file fd);
-#define gitfo_lstat(path, buf) lstat(path, buf)
-#define gitfo_fstat(fd, buf) fstat(fd, buf)
-#define gitfo_stat(path, buf) stat(path, buf)
-#define gitfo_fsync(fd) fsync(fd)
extern int gitfo_read_file(gitfo_buf *obj, const char *path);
extern void gitfo_free_buf(gitfo_buf *obj);
diff --git a/src/git/common.h b/src/git/common.h
index 6397533..75e1e84 100644
--- a/src/git/common.h
+++ b/src/git/common.h
@@ -58,6 +58,9 @@
/** Not enough space available. */
#define GIT_ENOMEM (GIT_ERROR - 3)
+/** Consult the OS error information. */
+#define GIT_EOSERR (GIT_ERROR - 4)
+
GIT_BEGIN_DECL
/** A revision traversal pool. */