errors.c 3.86 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6
 */
7
#include "common.h"
8
#include "global.h"
9
#include "posix.h"
10
#include "buffer.h"
11

12 13 14 15
/********************************************
 * New error handling
 ********************************************/

16 17 18 19 20
static git_error g_git_oom_error = {
	"Out of memory",
	GITERR_NOMEMORY
};

21
static void set_error_from_buffer(int error_class)
22 23
{
	git_error *error = &GIT_GLOBAL->error_t;
24
	git_buf *buf = &GIT_GLOBAL->error_buf;
25

26
	error->message = buf->ptr;
27 28 29 30 31
	error->klass = error_class;

	GIT_GLOBAL->last_error = error;
}

32 33 34 35 36 37 38 39 40 41 42 43 44
static void set_error(int error_class, char *string)
{
	git_buf *buf = &GIT_GLOBAL->error_buf;

	git_buf_clear(buf);
	if (string) {
		git_buf_puts(buf, string);
		git__free(string);
	}

	set_error_from_buffer(error_class);
}

45 46 47 48 49 50
void giterr_set_oom(void)
{
	GIT_GLOBAL->last_error = &g_git_oom_error;
}

void giterr_set(int error_class, const char *string, ...)
51 52
{
	va_list arglist;
53 54 55 56
#ifdef GIT_WIN32
	DWORD win32_error_code = (error_class == GITERR_OS) ? GetLastError() : 0;
#endif
	int error_code = (error_class == GITERR_OS) ? errno : 0;
57
	git_buf *buf = &GIT_GLOBAL->error_buf;
58

59
	git_buf_clear(buf);
60 61
	if (string) {
		va_start(arglist, string);
62
		git_buf_vprintf(buf, string, arglist);
63 64 65
		va_end(arglist);

		if (error_class == GITERR_OS)
66
			git_buf_PUTS(buf, ": ");
67
	}
68

69
	if (error_class == GITERR_OS) {
70
#ifdef GIT_WIN32
71 72
		char * win32_error = git_win32_get_error_message(win32_error_code);
		if (win32_error) {
73
			git_buf_puts(buf, win32_error);
74
			git__free(win32_error);
75 76

			SetLastError(0);
77
		}
78
		else
79
#endif
80
		if (error_code)
81
			git_buf_puts(buf, strerror(error_code));
82 83 84

		if (error_code)
			errno = 0;
85 86
	}

87 88
	if (!git_buf_oom(buf))
		set_error_from_buffer(error_class);
89 90 91 92
}

void giterr_set_str(int error_class, const char *string)
{
93
	git_buf *buf = &GIT_GLOBAL->error_buf;
94 95 96

	assert(string);

97 98
	if (!string)
		return;
99

100 101 102 103
	git_buf_clear(buf);
	git_buf_puts(buf, string);
	if (!git_buf_oom(buf))
		set_error_from_buffer(error_class);
104 105
}

106
int giterr_set_regex(const regex_t *regex, int error_code)
107 108
{
	char error_buf[1024];
109 110 111

	assert(error_code);

112 113
	regerror(error_code, regex, error_buf, sizeof(error_buf));
	giterr_set_str(GITERR_REGEX, error_buf);
114 115 116

	if (error_code == REG_NOMATCH)
		return GIT_ENOTFOUND;
117 118

	return GIT_EINVALIDSPEC;
119 120
}

121
void giterr_clear(void)
122
{
123 124 125 126
	if (GIT_GLOBAL->last_error != NULL) {
		set_error(0, NULL);
		GIT_GLOBAL->last_error = NULL;
	}
127 128 129 130 131

	errno = 0;
#ifdef GIT_WIN32
	SetLastError(0);
#endif
132
}
133

134 135 136 137 138 139
const git_error *giterr_last(void)
{
	return GIT_GLOBAL->last_error;
}

int giterr_state_capture(git_error_state *state, int error_code)
140 141
{
	git_error *error = GIT_GLOBAL->last_error;
142
	git_buf *error_buf = &GIT_GLOBAL->error_buf;
143

144
	memset(state, 0, sizeof(git_error_state));
145

146 147 148 149 150
	if (!error_code)
		return 0;

	state->error_code = error_code;
	state->oom = (error == &g_git_oom_error);
151

152 153
	if (error) {
		state->error_msg.klass = error->klass;
154

155 156 157 158
		if (state->oom)
			state->error_msg.message = g_git_oom_error.message;
		else
			state->error_msg.message = git_buf_detach(error_buf);
159
	}
160

161 162
	giterr_clear();
	return error_code;
163 164
}

165
int giterr_state_restore(git_error_state *state)
166
{
167
	int ret = 0;
168

169
	giterr_clear();
170

171 172
	if (state && state->error_msg.message) {
		if (state->oom)
173
			giterr_set_oom();
174
		else
175
			set_error(state->error_msg.klass, state->error_msg.message);
176 177 178

		ret = state->error_code;
		memset(state, 0, sizeof(git_error_state));
179
	}
180

181 182 183 184 185 186 187 188 189 190 191 192
	return ret;
}

void giterr_state_free(git_error_state *state)
{
	if (!state)
		return;

	if (!state->oom)
		git__free(state->error_msg.message);

	memset(state, 0, sizeof(git_error_state));
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
}

int giterr_system_last(void)
{
#ifdef GIT_WIN32
	return GetLastError();
#else
	return errno;
#endif
}

void giterr_system_set(int code)
{
#ifdef GIT_WIN32
	SetLastError(code);
#else
	errno = code;
#endif
}