common.h 4.22 KB
Newer Older
Vicent Marti committed
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
Vicent Marti committed
3 4 5 6
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
7 8 9
#ifndef INCLUDE_common_h__
#define INCLUDE_common_h__

10
#include "git2/common.h"
11
#include "cc-compat.h"
12

Russell Belfer committed
13 14 15 16 17 18 19
/** Declare a function as always inlined. */
#if defined(_MSC_VER)
# define GIT_INLINE(type) static __inline type
#else
# define GIT_INLINE(type) static inline type
#endif

20
#include <assert.h>
21
#include <errno.h>
22
#include <limits.h>
23
#include <stdlib.h>
24
#include <stdio.h>
25 26
#include <string.h>

27
#include <sys/types.h>
28
#include <sys/stat.h>
29 30 31 32

#ifdef GIT_WIN32

# include <io.h>
33
# include <direct.h>
34
# include <winsock2.h>
35
# include <windows.h>
36 37
# include "win32/msvc-compat.h"
# include "win32/mingw-compat.h"
38
# include "win32/error.h"
39
# include "win32/version.h"
Vicent Marti committed
40
# ifdef GIT_THREADS
Vicent Marti committed
41
#	include "win32/pthread.h"
42
# endif
43

44 45
#else

46
# include <unistd.h>
47
# include <strings.h>
Vicent Marti committed
48
# ifdef GIT_THREADS
Vicent Marti committed
49
#	include <pthread.h>
50
#	include <sched.h>
Vicent Marti committed
51
# endif
Russell Belfer committed
52 53
#define GIT_STDLIB_CALL

Edward Thomson committed
54 55
# include <arpa/inet.h>

56 57
#endif

58
#include "git2/types.h"
59
#include "git2/errors.h"
60
#include "thread-utils.h"
61
#include "bswap.h"
62

63 64
#include <regex.h>

65 66 67
/**
 * Check a pointer allocation result, returning -1 if it failed.
 */
68 69
#define GITERR_CHECK_ALLOC(ptr) if (ptr == NULL) { return -1; }

70
/**
71 72 73
 * Check a return value and propogate result if non-zero.
 */
#define GITERR_CHECK_ERROR(code) \
74
	do { int _err = (code); if (_err) return _err; } while (0)
75 76

/**
77 78
 * Set the error message for this thread, formatting as needed.
 */
79
void giterr_set(int error_class, const char *string, ...);
80 81 82

/**
 * Set the error message for a regex failure, using the internal regex
83
 * error code lookup and return a libgit error code.
84
 */
85
int giterr_set_regex(const regex_t *regex, int error_code);
86

Ben Straub committed
87
/**
88 89 90 91 92 93 94
 * Set error message for user callback if needed.
 *
 * If the error code in non-zero and no error message is set, this
 * sets a generic error message.
 *
 * @return This always returns the `error_code` parameter.
 */
95 96
GIT_INLINE(int) giterr_set_after_callback_function(
	int error_code, const char *action)
97 98 99 100 101 102 103 104 105 106
{
	if (error_code) {
		const git_error *e = giterr_last();
		if (!e || !e->message)
			giterr_set(e ? e->klass : GITERR_CALLBACK,
				"%s callback returned %d", action, error_code);
	}
	return error_code;
}

107
#ifdef GIT_WIN32
108 109
#define giterr_set_after_callback(code) \
	giterr_set_after_callback_function((code), __FUNCTION__)
110
#else
111 112
#define giterr_set_after_callback(code) \
	giterr_set_after_callback_function((code), __func__)
113
#endif
114 115

/**
116 117
 * Gets the system error code for this thread.
 */
118
int giterr_system_last(void);
119 120 121 122

/**
 * Sets the system error code for this thread.
 */
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
void giterr_system_set(int code);

/**
 * Structure to preserve libgit2 error state
 */
typedef struct {
	int       error_code;
	git_error error_msg;
} git_error_state;

/**
 * Capture current error state to restore later, returning error code.
 * If `error_code` is zero, this does nothing and returns zero.
 */
int giterr_capture(git_error_state *state, int error_code);

/**
 * Restore error state to a previous value, returning saved error code.
 */
int giterr_restore(git_error_state *state);

/**
Ben Straub committed
145 146
 * Check a versioned structure for validity
 */
147
GIT_INLINE(int) giterr__check_version(const void *structure, unsigned int expected_max, const char *name)
Ben Straub committed
148
{
Vicent Marti committed
149
	unsigned int actual;
Vicent Marti committed
150

Ben Straub committed
151
	if (!structure)
152
		return 0;
Ben Straub committed
153

Vicent Marti committed
154
	actual = *(const unsigned int*)structure;
Ben Straub committed
155
	if (actual > 0 && actual <= expected_max)
156
		return 0;
Ben Straub committed
157 158

	giterr_set(GITERR_INVALID, "Invalid version %d on %s", actual, name);
159
	return -1;
Ben Straub committed
160
}
161
#define GITERR_CHECK_VERSION(S,V,N) if (giterr__check_version(S,V,N) < 0) return -1
Ben Straub committed
162

Ben Straub committed
163 164 165 166 167 168 169 170 171 172
/**
 * Initialize a structure with a version.
 */
GIT_INLINE(void) git__init_structure(void *structure, size_t len, unsigned int version)
{
	memset(structure, 0, len);
	*((int*)structure) = version;
}
#define GIT_INIT_STRUCTURE(S,V) git__init_structure(S, sizeof(*S), V)

173 174 175 176 177
#define GIT_INIT_STRUCTURE_FROM_TEMPLATE(PTR,VERSION,TYPE,TPL) do { \
	TYPE _tmpl = TPL; \
	GITERR_CHECK_VERSION(&(VERSION), _tmpl.version, #TYPE);	\
	memcpy((PTR), &_tmpl, sizeof(_tmpl)); } while (0)

178 179
/* NOTE: other giterr functions are in the public errors.h header file */

180 181
#include "util.h"

182
#endif /* INCLUDE_common_h__ */