add a conversion function from errno to got_error and use it
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
diff --git a/include/got_error.h b/include/got_error.h
index 8f4ae1a..59101dc 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -15,7 +15,7 @@
*/
/* Error codes */
-#define GOT_ERR_UNKNOWN 0
+#define GOT_ERR_ERRNO 0
#define GOT_ERR_NO_MEM 1
#define GOT_ERR_NOT_GIT_REPO 2
#define GOT_ERR_NOT_ABSPATH 3
@@ -38,7 +38,7 @@ static const struct got_error {
int code;
const char *msg;
} got_errors[] = {
- { GOT_ERR_UNKNOWN, "unknown error" },
+ { GOT_ERR_ERRNO, "see errno" },
{ GOT_ERR_NO_MEM, "out of memory" },
{ GOT_ERR_NOT_GIT_REPO, "no git repository found" },
{ GOT_ERR_NOT_ABSPATH, "absolute path expected" },
@@ -59,3 +59,4 @@ static const struct got_error {
};
const struct got_error * got_error(int code);
+const struct got_error *got_error_from_errno();
diff --git a/lib/error.c b/lib/error.c
index ab4b7f6..5ad5f52 100644
--- a/lib/error.c
+++ b/lib/error.c
@@ -14,6 +14,10 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+
#include "got_error.h"
#ifndef nitems
@@ -30,5 +34,15 @@ got_error(int code)
return &got_errors[i];
}
- return &got_errors[GOT_ERR_UNKNOWN];
+ abort();
+}
+
+const struct got_error *
+got_error_from_errno()
+{
+ static struct got_error err;
+
+ err.code = GOT_ERR_ERRNO;
+ err.msg = strerror(errno);
+ return &err;
}
diff --git a/lib/object.c b/lib/object.c
index 2beacfb..c0dae35 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -284,7 +284,7 @@ got_object_open(struct got_object **obj, struct got_repository *repo,
f = fopen(path, "rb");
if (f == NULL) {
if (errno != ENOENT) {
- err = got_error(GOT_ERR_BAD_PATH);
+ err = got_error_from_errno();
goto done;
}
err = got_packfile_extract_object(&f, id, repo);
diff --git a/lib/pack.c b/lib/pack.c
index 6f0e06c..9e1a9b8 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -346,7 +346,7 @@ dump_packed_object(FILE **f, FILE *packfile, off_t offset)
}
if (fseeko(packfile, offset, SEEK_SET) != 0) {
- err = got_error(errno == EIO ? GOT_ERR_IO : GOT_ERR_BAD_PATH);
+ err = got_error_from_errno();
goto done;
}
@@ -450,7 +450,7 @@ extract_object(FILE **f, const char *path_packdir,
packfile = fopen(path_packfile, "rb");
if (packfile == NULL) {
- err = got_error(errno == EIO ? GOT_ERR_IO : GOT_ERR_BAD_PATH);
+ err = got_error_from_errno();
goto done;
}
@@ -465,8 +465,8 @@ extract_object(FILE **f, const char *path_packdir,
done:
free(path_packfile);
- if (packfile && fclose(packfile) == -1 && errno == EIO && err == 0)
- err = got_error(GOT_ERR_IO);
+ if (packfile && fclose(packfile) == -1 && err == 0)
+ err = got_error_from_errno();
return err;
}
@@ -486,7 +486,7 @@ got_packfile_extract_object(FILE **f, struct got_object_id *id,
packdir = opendir(path_packdir);
if (packdir == NULL) {
- err = got_error(errno == EIO ? GOT_ERR_IO : GOT_ERR_BAD_PATH);
+ err = got_error_from_errno();
goto done;
}
@@ -517,7 +517,7 @@ got_packfile_extract_object(FILE **f, struct got_object_id *id,
done:
free(path_packdir);
- if (packdir && closedir(packdir) != 0 && errno == EIO && err == 0)
- err = got_error(GOT_ERR_IO);
+ if (packdir && closedir(packdir) != 0 && err == 0)
+ err = got_error_from_errno();
return err;
}