common.h 3.81 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

13
#include <assert.h>
14
#include <errno.h>
15
#include <limits.h>
16
#include <stdlib.h>
17
#include <stdio.h>
18 19
#include <string.h>

20
#include <sys/types.h>
21
#include <sys/stat.h>
22 23 24 25

#ifdef GIT_WIN32

# include <io.h>
26
# include <direct.h>
27
# include <winsock2.h>
28
# include <windows.h>
29 30
# include "win32/msvc-compat.h"
# include "win32/mingw-compat.h"
31
# include "win32/error.h"
32
# include "win32/version.h"
Vicent Marti committed
33
# ifdef GIT_THREADS
Vicent Marti committed
34
#	include "win32/pthread.h"
35
# endif
36

37 38
#else

39
# include <unistd.h>
Vicent Marti committed
40
# ifdef GIT_THREADS
Vicent Marti committed
41
#	include <pthread.h>
Vicent Marti committed
42
# endif
Russell Belfer committed
43 44
#define GIT_STDLIB_CALL

Edward Thomson committed
45 46
# include <arpa/inet.h>

47 48
#endif

49
#include "git2/types.h"
50
#include "git2/errors.h"
51
#include "thread-utils.h"
52
#include "bswap.h"
53

54 55
#include <regex.h>

56 57 58
/**
 * Check a pointer allocation result, returning -1 if it failed.
 */
59 60
#define GITERR_CHECK_ALLOC(ptr) if (ptr == NULL) { return -1; }

61
/**
62 63 64
 * Check a return value and propogate result if non-zero.
 */
#define GITERR_CHECK_ERROR(code) \
65
	do { int _err = (code); if (_err) return _err; } while (0)
66 67

/**
68 69
 * Set the error message for this thread, formatting as needed.
 */
70
void giterr_set(int error_class, const char *string, ...);
71 72 73

/**
 * Set the error message for a regex failure, using the internal regex
74
 * error code lookup and return a libgit error code.
75
 */
76
int giterr_set_regex(const regex_t *regex, int error_code);
77

Ben Straub committed
78
/**
79 80 81 82 83 84 85
 * 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.
 */
86 87
GIT_INLINE(int) giterr_set_after_callback_function(
	int error_code, const char *action)
88 89 90 91 92 93 94 95 96 97
{
	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;
}

98
#ifdef GIT_WIN32
99 100
#define giterr_set_after_callback(code) \
	giterr_set_after_callback_function((code), __FUNCTION__)
101
#else
102 103
#define giterr_set_after_callback(code) \
	giterr_set_after_callback_function((code), __func__)
104
#endif
105 106

/**
107 108
 * Gets the system error code for this thread.
 */
109
int giterr_system_last(void);
110 111 112 113

/**
 * Sets the system error code for this thread.
 */
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
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
136 137
 * Check a versioned structure for validity
 */
138
GIT_INLINE(int) giterr__check_version(const void *structure, unsigned int expected_max, const char *name)
Ben Straub committed
139
{
Vicent Marti committed
140
	unsigned int actual;
Vicent Marti committed
141

Ben Straub committed
142
	if (!structure)
143
		return 0;
Ben Straub committed
144

Vicent Marti committed
145
	actual = *(const unsigned int*)structure;
Ben Straub committed
146
	if (actual > 0 && actual <= expected_max)
147
		return 0;
Ben Straub committed
148 149

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

Ben Straub committed
154 155 156 157 158 159 160 161 162 163
/**
 * 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)

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

166 167
#include "util.h"

168
#endif /* INCLUDE_common_h__ */