Commit f334529ed243c898cbf0cc8ee1058a769444f448

Stefan Sperling 2018-01-12T21:17:22

add a conversion function from errno to got_error and use it

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;
 }