errors.c 4.4 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

8
#include "common.h"
9

10
#include "threadstate.h"
11
#include "posix.h"
12
#include "buffer.h"
13
#include "libgit2.h"
14

15 16 17 18
/********************************************
 * New error handling
 ********************************************/

19 20
static git_error g_git_oom_error = {
	"Out of memory",
21
	GIT_ERROR_NOMEMORY
22 23
};

24 25 26 27 28
static git_error g_git_uninitialized_error = {
	"libgit2 has not been initialized; you must call git_libgit2_init",
	GIT_ERROR_INVALID
};

29
static void set_error_from_buffer(int error_class)
30
{
31 32
	git_error *error = &GIT_THREADSTATE->error_t;
	git_buf *buf = &GIT_THREADSTATE->error_buf;
33

34
	error->message = buf->ptr;
35 36
	error->klass = error_class;

37
	GIT_THREADSTATE->last_error = error;
38 39
}

40 41
static void set_error(int error_class, char *string)
{
42
	git_buf *buf = &GIT_THREADSTATE->error_buf;
43 44 45 46 47 48 49 50 51 52

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

	set_error_from_buffer(error_class);
}

53
void git_error_set_oom(void)
54
{
55
	GIT_THREADSTATE->last_error = &g_git_oom_error;
56 57
}

58 59 60 61 62 63 64 65 66 67
void git_error_set(int error_class, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	git_error_vset(error_class, fmt, ap);
	va_end(ap);
}

void git_error_vset(int error_class, const char *fmt, va_list ap)
68
{
69
#ifdef GIT_WIN32
70
	DWORD win32_error_code = (error_class == GIT_ERROR_OS) ? GetLastError() : 0;
71
#endif
72
	int error_code = (error_class == GIT_ERROR_OS) ? errno : 0;
73
	git_buf *buf = &GIT_THREADSTATE->error_buf;
74

75
	git_buf_clear(buf);
76 77
	if (fmt) {
		git_buf_vprintf(buf, fmt, ap);
78
		if (error_class == GIT_ERROR_OS)
79
			git_buf_PUTS(buf, ": ");
80
	}
81

82
	if (error_class == GIT_ERROR_OS) {
83
#ifdef GIT_WIN32
84 85
		char * win32_error = git_win32_get_error_message(win32_error_code);
		if (win32_error) {
86
			git_buf_puts(buf, win32_error);
87
			git__free(win32_error);
88 89

			SetLastError(0);
90
		}
91
		else
92
#endif
93
		if (error_code)
94
			git_buf_puts(buf, strerror(error_code));
95 96 97

		if (error_code)
			errno = 0;
98 99
	}

100 101
	if (!git_buf_oom(buf))
		set_error_from_buffer(error_class);
102 103
}

104
int git_error_set_str(int error_class, const char *string)
105
{
106
	git_buf *buf = &GIT_THREADSTATE->error_buf;
107

108
	GIT_ASSERT_ARG(string);
109

110 111
	git_buf_clear(buf);
	git_buf_puts(buf, string);
112 113 114 115 116 117

	if (git_buf_oom(buf))
		return -1;

	set_error_from_buffer(error_class);
	return 0;
118 119
}

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

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

133
const git_error *git_error_last(void)
134
{
135 136 137 138
	/* If the library is not initialized, return a static error. */
	if (!git_libgit2_init_count())
		return &g_git_uninitialized_error;

139
	return GIT_THREADSTATE->last_error;
140 141
}

142
int git_error_state_capture(git_error_state *state, int error_code)
143
{
144 145
	git_error *error = GIT_THREADSTATE->last_error;
	git_buf *error_buf = &GIT_THREADSTATE->error_buf;
146

147
	memset(state, 0, sizeof(git_error_state));
148

149 150 151 152 153
	if (!error_code)
		return 0;

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

155 156
	if (error) {
		state->error_msg.klass = error->klass;
157

158 159 160 161
		if (state->oom)
			state->error_msg.message = g_git_oom_error.message;
		else
			state->error_msg.message = git_buf_detach(error_buf);
162
	}
163

164
	git_error_clear();
165
	return error_code;
166 167
}

168
int git_error_state_restore(git_error_state *state)
169
{
170
	int ret = 0;
171

172
	git_error_clear();
173

174 175
	if (state && state->error_msg.message) {
		if (state->oom)
176
			git_error_set_oom();
177
		else
178
			set_error(state->error_msg.klass, state->error_msg.message);
179 180 181

		ret = state->error_code;
		memset(state, 0, sizeof(git_error_state));
182
	}
183

184 185 186
	return ret;
}

187
void git_error_state_free(git_error_state *state)
188 189 190 191 192 193 194 195
{
	if (!state)
		return;

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

	memset(state, 0, sizeof(git_error_state));
196 197
}

198
int git_error_system_last(void)
199 200 201 202 203 204 205 206
{
#ifdef GIT_WIN32
	return GetLastError();
#else
	return errno;
#endif
}

207
void git_error_system_set(int code)
208 209 210 211 212 213 214
{
#ifdef GIT_WIN32
	SetLastError(code);
#else
	errno = code;
#endif
}
215

216
/* Deprecated error values and functions */
217

218
#ifndef GIT_DEPRECATE_HARD
219 220 221 222 223 224 225 226 227 228 229 230
const git_error *giterr_last(void)
{
	return git_error_last();
}

void giterr_clear(void)
{
	git_error_clear();
}

void giterr_set_str(int error_class, const char *string)
{
231
	git_error_set_str(error_class, string);
232 233 234 235 236 237
}

void giterr_set_oom(void)
{
	git_error_set_oom();
}
238
#endif