libgit2/src/errors.c
Shawn O. Pearce 7dd8a9f710 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>
2008-12-30 23:29:23 -08:00

53 lines
1.0 KiB
C

#include "common.h"
#include "thread-utils.h" /* for GIT_TLS */
#if defined(GIT_TLS)
/* compile-time constant initialization required */
GIT_TLS int git_errno = 0;
#elif defined(GIT_HAS_PTHREAD)
static pthread_key_t errno_key;
static void init_errno(void) __attribute__((constructor));
static void init_errno(void)
{
pthread_key_create(&errno_key, free);
}
int *git__errno_storage(void)
{
int *e = pthread_getspecific(errno_key);
if (!e) {
#undef calloc
e = calloc(1, sizeof(*e));
#define calloc(a,b) GIT__FORBID_MALLOC
pthread_setspecific(errno_key, e);
}
return e;
}
#endif
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" },
{ GIT_ENOMEM, "Not enough space" },
};
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;
return "Unknown error";
}