Add an embryo of a TLS-aware error handling system This adds the per-thread global variable git_errno to the system, which callers can examine to get information about an error. Two helper functions are added to reduce LoC-count for the library code itself. Also, some exceptions are made for running sparse on GIT_TLS definitions, since it doesn't grok thread-local variables at all. Signed-off-by: Andreas Ericsson <ae@op5.se> 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
diff --git a/Makefile b/Makefile
index bbbf64b..29c52c3 100644
--- a/Makefile
+++ b/Makefile
@@ -41,7 +41,7 @@ apidocs:
test: $(TEST_RUN)
sparse:
- @for i in $(SRC_C); do sparse $$i $(SPARSE_FLAGS) $(BASIC_CFLAGS) $(CFLAGS); done
+ @for i in $(SRC_C); do sparse $$i -DSPARSE_IS_RUNNING $(SPARSE_FLAGS) $(BASIC_CFLAGS) $(CFLAGS); done
install-headers: $(PUBLIC_HEADERS)
@mkdir -p /tmp/gitinc/git
diff --git a/src/common.h b/src/common.h
index 5a6fa6e..1a2e9c0 100644
--- a/src/common.h
+++ b/src/common.h
@@ -3,6 +3,7 @@
#include "cc-compat.h"
#include "util.h"
+#include "errors.h"
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
diff --git a/src/errors.c b/src/errors.c
new file mode 100644
index 0000000..b3e014d
--- /dev/null
+++ b/src/errors.c
@@ -0,0 +1,23 @@
+#include "common.h"
+#include "thread-utils.h" /* for GIT_TLS */
+
+/* compile-time constant initialization required */
+GIT_TLS int git_errno = 0;
+
+static struct {
+ int num;
+ const char *str;
+} error_codes[] = {
+ { GIT_ENOTOID, "Not a git oid" },
+ { GIT_ENOTFOUND, "Object does not exist in the scope searched" },
+};
+
+const char *git_strerror(int num)
+{
+ int i;
+ for (i = 0; i < ARRAY_SIZE(error_codes); i++)
+ if (num == error_codes[i].num)
+ return error_codes[i].str;
+
+ return "Unknown error";
+}
diff --git a/src/errors.h b/src/errors.h
new file mode 100644
index 0000000..e323fc5
--- /dev/null
+++ b/src/errors.h
@@ -0,0 +1,17 @@
+#ifndef INCLUDE_errors_h__
+#define INCLUDE_errors_h__
+#include "git/errors.h"
+
+/* convenience functions */
+static inline int git_int_error(int code)
+{
+ git_errno = code;
+ return code;
+}
+
+static inline void *git_ptr_error(int code)
+{
+ git_errno = code;
+ return NULL;
+}
+#endif
diff --git a/src/git/errors.h b/src/git/errors.h
new file mode 100644
index 0000000..d0a0d63
--- /dev/null
+++ b/src/git/errors.h
@@ -0,0 +1,25 @@
+#ifndef INCLUDE_git_errors_h__
+#define INCLUDE_git_errors_h__
+/**
+ * @file git/errors.h
+ * @brief Git error handling routines and variables
+ * @ingroup Git
+ * @{
+ */
+
+#include "common.h"
+#include "thread-utils.h"
+GIT_BEGIN_DECL
+
+/** The git errno. */
+GIT_EXTERN(int) GIT_TLS git_errno;
+
+/**
+ * strerror() for the Git library
+ * @param num The error code to explain
+ * @return a string explaining the error code
+ */
+GIT_EXTERN(const char *) git_strerror(int num);
+/** @} */
+GIT_END_DECL
+#endif
diff --git a/src/git/thread-utils.h b/src/git/thread-utils.h
index f2ddf1e..7ce66fd 100644
--- a/src/git/thread-utils.h
+++ b/src/git/thread-utils.h
@@ -22,4 +22,11 @@
# define GIT_TLS /* nothing: tls vars are thread-global */
#endif
+/* sparse doesn't grok thread-local variables */
+#ifdef SPARSE_IS_RUNNING
+# undef GIT_HAS_TLS
+# undef GIT_TLS
+# define GIT_TLS
+#endif
+
#endif /* INCLUDE_git_thread_utils_h__ */