common.h 4.24 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
# include <ws2tcpip.h>
37 38
# include "win32/msvc-compat.h"
# include "win32/mingw-compat.h"
39
# include "win32/error.h"
40
# include "win32/version.h"
Vicent Marti committed
41
# ifdef GIT_THREADS
Vicent Marti committed
42
#	include "win32/pthread.h"
43
# endif
44

45 46
#else

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

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

57 58
#endif

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

64 65
#include <regex.h>

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

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

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

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

Ben Straub committed
88
/**
89 90 91 92 93 94 95
 * 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.
 */
96 97
GIT_INLINE(int) giterr_set_after_callback_function(
	int error_code, const char *action)
98 99 100 101 102 103 104 105 106 107
{
	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;
}

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

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

/**
 * Sets the system error code for this thread.
 */
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
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
146 147
 * Check a versioned structure for validity
 */
148
GIT_INLINE(int) giterr__check_version(const void *structure, unsigned int expected_max, const char *name)
Ben Straub committed
149
{
Vicent Marti committed
150
	unsigned int actual;
Vicent Marti committed
151

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

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

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

Ben Straub committed
164 165 166 167 168 169 170 171 172 173
/**
 * 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)

174 175 176 177 178
#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)

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

181 182
#include "util.h"

183
#endif /* INCLUDE_common_h__ */