Blame src/errors.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "global.h"
Packit Service 20376f
#include "posix.h"
Packit Service 20376f
#include "buffer.h"
Packit Service 20376f
Packit Service 20376f
/********************************************
Packit Service 20376f
 * New error handling
Packit Service 20376f
 ********************************************/
Packit Service 20376f
Packit Service 20376f
static git_error g_git_oom_error = {
Packit Service 20376f
	"Out of memory",
Packit Service 20376f
	GITERR_NOMEMORY
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
static void set_error_from_buffer(int error_class)
Packit Service 20376f
{
Packit Service 20376f
	git_error *error = &GIT_GLOBAL->error_t;
Packit Service 20376f
	git_buf *buf = &GIT_GLOBAL->error_buf;
Packit Service 20376f
Packit Service 20376f
	error->message = buf->ptr;
Packit Service 20376f
	error->klass = error_class;
Packit Service 20376f
Packit Service 20376f
	GIT_GLOBAL->last_error = error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void set_error(int error_class, char *string)
Packit Service 20376f
{
Packit Service 20376f
	git_buf *buf = &GIT_GLOBAL->error_buf;
Packit Service 20376f
Packit Service 20376f
	git_buf_clear(buf);
Packit Service 20376f
	if (string) {
Packit Service 20376f
		git_buf_puts(buf, string);
Packit Service 20376f
		git__free(string);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	set_error_from_buffer(error_class);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void giterr_set_oom(void)
Packit Service 20376f
{
Packit Service 20376f
	GIT_GLOBAL->last_error = &g_git_oom_error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void giterr_set(int error_class, const char *string, ...)
Packit Service 20376f
{
Packit Service 20376f
	va_list arglist;
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	DWORD win32_error_code = (error_class == GITERR_OS) ? GetLastError() : 0;
Packit Service 20376f
#endif
Packit Service 20376f
	int error_code = (error_class == GITERR_OS) ? errno : 0;
Packit Service 20376f
	git_buf *buf = &GIT_GLOBAL->error_buf;
Packit Service 20376f
Packit Service 20376f
	git_buf_clear(buf);
Packit Service 20376f
	if (string) {
Packit Service 20376f
		va_start(arglist, string);
Packit Service 20376f
		git_buf_vprintf(buf, string, arglist);
Packit Service 20376f
		va_end(arglist);
Packit Service 20376f
Packit Service 20376f
		if (error_class == GITERR_OS)
Packit Service 20376f
			git_buf_PUTS(buf, ": ");
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (error_class == GITERR_OS) {
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
		char * win32_error = git_win32_get_error_message(win32_error_code);
Packit Service 20376f
		if (win32_error) {
Packit Service 20376f
			git_buf_puts(buf, win32_error);
Packit Service 20376f
			git__free(win32_error);
Packit Service 20376f
Packit Service 20376f
			SetLastError(0);
Packit Service 20376f
		}
Packit Service 20376f
		else
Packit Service 20376f
#endif
Packit Service 20376f
		if (error_code)
Packit Service 20376f
			git_buf_puts(buf, strerror(error_code));
Packit Service 20376f
Packit Service 20376f
		if (error_code)
Packit Service 20376f
			errno = 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!git_buf_oom(buf))
Packit Service 20376f
		set_error_from_buffer(error_class);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void giterr_set_str(int error_class, const char *string)
Packit Service 20376f
{
Packit Service 20376f
	git_buf *buf = &GIT_GLOBAL->error_buf;
Packit Service 20376f
Packit Service 20376f
	assert(string);
Packit Service 20376f
Packit Service 20376f
	if (!string)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	git_buf_clear(buf);
Packit Service 20376f
	git_buf_puts(buf, string);
Packit Service 20376f
	if (!git_buf_oom(buf))
Packit Service 20376f
		set_error_from_buffer(error_class);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int giterr_set_regex(const regex_t *regex, int error_code)
Packit Service 20376f
{
Packit Service 20376f
	char error_buf[1024];
Packit Service 20376f
Packit Service 20376f
	assert(error_code);
Packit Service 20376f
Packit Service 20376f
	regerror(error_code, regex, error_buf, sizeof(error_buf));
Packit Service 20376f
	giterr_set_str(GITERR_REGEX, error_buf);
Packit Service 20376f
Packit Service 20376f
	if (error_code == REG_NOMATCH)
Packit Service 20376f
		return GIT_ENOTFOUND;
Packit Service 20376f
Packit Service 20376f
	return GIT_EINVALIDSPEC;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void giterr_clear(void)
Packit Service 20376f
{
Packit Service 20376f
	if (GIT_GLOBAL->last_error != NULL) {
Packit Service 20376f
		set_error(0, NULL);
Packit Service 20376f
		GIT_GLOBAL->last_error = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	errno = 0;
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	SetLastError(0);
Packit Service 20376f
#endif
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
const git_error *giterr_last(void)
Packit Service 20376f
{
Packit Service 20376f
	return GIT_GLOBAL->last_error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int giterr_state_capture(git_error_state *state, int error_code)
Packit Service 20376f
{
Packit Service 20376f
	git_error *error = GIT_GLOBAL->last_error;
Packit Service 20376f
	git_buf *error_buf = &GIT_GLOBAL->error_buf;
Packit Service 20376f
Packit Service 20376f
	memset(state, 0, sizeof(git_error_state));
Packit Service 20376f
Packit Service 20376f
	if (!error_code)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	state->error_code = error_code;
Packit Service 20376f
	state->oom = (error == &g_git_oom_error);
Packit Service 20376f
Packit Service 20376f
	if (error) {
Packit Service 20376f
		state->error_msg.klass = error->klass;
Packit Service 20376f
Packit Service 20376f
		if (state->oom)
Packit Service 20376f
			state->error_msg.message = g_git_oom_error.message;
Packit Service 20376f
		else
Packit Service 20376f
			state->error_msg.message = git_buf_detach(error_buf);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	giterr_clear();
Packit Service 20376f
	return error_code;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int giterr_state_restore(git_error_state *state)
Packit Service 20376f
{
Packit Service 20376f
	int ret = 0;
Packit Service 20376f
Packit Service 20376f
	giterr_clear();
Packit Service 20376f
Packit Service 20376f
	if (state && state->error_msg.message) {
Packit Service 20376f
		if (state->oom)
Packit Service 20376f
			giterr_set_oom();
Packit Service 20376f
		else
Packit Service 20376f
			set_error(state->error_msg.klass, state->error_msg.message);
Packit Service 20376f
Packit Service 20376f
		ret = state->error_code;
Packit Service 20376f
		memset(state, 0, sizeof(git_error_state));
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return ret;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void giterr_state_free(git_error_state *state)
Packit Service 20376f
{
Packit Service 20376f
	if (!state)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	if (!state->oom)
Packit Service 20376f
		git__free(state->error_msg.message);
Packit Service 20376f
Packit Service 20376f
	memset(state, 0, sizeof(git_error_state));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int giterr_system_last(void)
Packit Service 20376f
{
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	return GetLastError();
Packit Service 20376f
#else
Packit Service 20376f
	return errno;
Packit Service 20376f
#endif
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void giterr_system_set(int code)
Packit Service 20376f
{
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	SetLastError(code);
Packit Service 20376f
#else
Packit Service 20376f
	errno = code;
Packit Service 20376f
#endif
Packit Service 20376f
}