]> git.proxmox.com Git - libgit2.git/blame - src/common.h
Improved error handling
[libgit2.git] / src / common.h
CommitLineData
bb742ede 1/*
359fc2d2 2 * Copyright (C) the libgit2 contributors. All rights reserved.
bb742ede
VM
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
76a8c447
AE
7#ifndef INCLUDE_common_h__
8#define INCLUDE_common_h__
9
0657e46d 10#include "git2/common.h"
8a086f87 11#include "cc-compat.h"
b3039bee 12
75d58430 13#include <assert.h>
76a8c447 14#include <errno.h>
028ef0de 15#include <limits.h>
76a8c447 16#include <stdlib.h>
840fb8b7 17#include <stdio.h>
76a8c447
AE
18#include <string.h>
19
840fb8b7 20#include <sys/types.h>
90d4d2f0 21#include <sys/stat.h>
840fb8b7
RJ
22
23#ifdef GIT_WIN32
24
25# include <io.h>
502acd16 26# include <direct.h>
ac8eac2f 27# include <winsock2.h>
79ca2edc 28# include <windows.h>
678e9e04
VM
29# include "win32/msvc-compat.h"
30# include "win32/mingw-compat.h"
bb3de0c4 31# ifdef GIT_THREADS
87d9869f 32# include "win32/pthread.h"
bb3de0c4 33#endif
79ca2edc 34
8a086f87 35# define snprintf _snprintf
840fb8b7 36
840fb8b7 37#else
840fb8b7 38# include <unistd.h>
840fb8b7 39
bb3de0c4 40# ifdef GIT_THREADS
87d9869f 41# include <pthread.h>
bb3de0c4 42# endif
840fb8b7
RJ
43#endif
44
e52e38d3 45#include "git2/types.h"
6810bf28 46#include "git2/errors.h"
028ef0de 47#include "thread-utils.h"
5dddf7c8 48#include "bswap.h"
5ee2fe77 49
dda708e7
VM
50#include <regex.h>
51
1a628100
RB
52/**
53 * Check a pointer allocation result, returning -1 if it failed.
54 */
dda708e7
VM
55#define GITERR_CHECK_ALLOC(ptr) if (ptr == NULL) { return -1; }
56
1a628100
RB
57/**
58 * Set the error message for this thread, formatting as needed.
59 */
dda708e7 60void giterr_set(int error_class, const char *string, ...);
1a628100
RB
61
62/**
63 * Set the error message for a regex failure, using the internal regex
cc146626 64 * error code lookup and return a libgit error code.
1a628100 65 */
cc146626 66int giterr_set_regex(const regex_t *regex, int error_code);
dda708e7 67
c7231c45
BS
68/**
69 * Check a versioned structure for validity
70 */
bf192cdb 71GIT_INLINE(int) giterr__check_version(const void *structure, unsigned int expected_max, const char *name)
c7231c45 72{
43efaabd 73 unsigned int actual;
8ff66112 74
c7231c45 75 if (!structure)
bf192cdb 76 return 0;
c7231c45 77
43efaabd 78 actual = *(const unsigned int*)structure;
c7231c45 79 if (actual > 0 && actual <= expected_max)
bf192cdb 80 return 0;
c7231c45
BS
81
82 giterr_set(GITERR_INVALID, "Invalid version %d on %s", actual, name);
bf192cdb 83 return -1;
c7231c45 84}
bf192cdb 85#define GITERR_CHECK_VERSION(S,V,N) if (giterr__check_version(S,V,N) < 0) return -1
c7231c45 86
0ab3a2ab
BS
87/**
88 * Initialize a structure with a version.
89 */
90GIT_INLINE(void) git__init_structure(void *structure, size_t len, unsigned int version)
91{
92 memset(structure, 0, len);
93 *((int*)structure) = version;
94}
95#define GIT_INIT_STRUCTURE(S,V) git__init_structure(S, sizeof(*S), V)
96
1a628100
RB
97/* NOTE: other giterr functions are in the public errors.h header file */
98
3f53c971
VM
99#include "util.h"
100
76a8c447 101#endif /* INCLUDE_common_h__ */