source src/errors.c
Line | Flow | Count | Block(s) | Source |
---|---|---|---|---|
1 | - | /* | ||
2 | - | * Copyright (C) the libgit2 contributors. All rights reserved. | ||
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 | - | */ | ||
7 | - | |||
8 | - | #include "common.h" | ||
9 | - | |||
10 | - | #include "global.h" | ||
11 | - | #include "posix.h" | ||
12 | - | #include "buffer.h" | ||
13 | - | |||
14 | - | /******************************************** | ||
15 | - | * New error handling | ||
16 | - | ********************************************/ | ||
17 | - | |||
18 | - | static git_error g_git_oom_error = { | ||
19 | - | "Out of memory", | ||
20 | - | GIT_ERROR_NOMEMORY | ||
21 | - | }; | ||
22 | - | |||
23 | 599295 | 2 | static void set_error_from_buffer(int error_class) | |
24 | - | { | ||
25 | 599295 | 2 | git_error *error = &GIT_GLOBAL->error_t; | |
26 | 595734 | 3 | git_buf *buf = &GIT_GLOBAL->error_buf; | |
27 | - | |||
28 | 591310 | 4 | error->message = buf->ptr; | |
29 | 591310 | 4 | error->klass = error_class; | |
30 | - | |||
31 | 591310 | 4 | GIT_GLOBAL->last_error = error; | |
32 | 591098 | 5 | } | |
33 | - | |||
34 | 197321 | 2 | static void set_error(int error_class, char *string) | |
35 | - | { | ||
36 | 197321 | 2 | git_buf *buf = &GIT_GLOBAL->error_buf; | |
37 | - | |||
38 | 197180 | 3 | git_buf_clear(buf); | |
39 | 197089 | 4 | if (string) { | |
40 | 300 | 5 | git_buf_puts(buf, string); | |
41 | 300 | 6 | git__free(string); | |
42 | - | } | ||
43 | - | |||
44 | 197089 | 7 | set_error_from_buffer(error_class); | |
45 | 197305 | 8 | } | |
46 | - | |||
47 | 11 | 2 | void git_error_set_oom(void) | |
48 | - | { | ||
49 | 11 | 2 | GIT_GLOBAL->last_error = &g_git_oom_error; | |
50 | 11 | 3 | } | |
51 | - | |||
52 | 406604 | 2 | void git_error_set(int error_class, const char *fmt, ...) | |
53 | - | { | ||
54 | - | va_list ap; | ||
55 | - | |||
56 | 406604 | 2 | va_start(ap, fmt); | |
57 | 406604 | 2 | git_error_vset(error_class, fmt, ap); | |
58 | 398756 | 3 | va_end(ap); | |
59 | 398756 | 3 | } | |
60 | - | |||
61 | - | 2 | suppressed: function cannot be solved git_error_vset (automatic due to inconsistent arc counts in .gcda files)void git_error_vset(int error_class, const char *fmt, va_list ap) | |
62 | - | { | ||
63 | - | #ifdef GIT_WIN32 | ||
64 | - | DWORD win32_error_code = (error_class == GIT_ERROR_OS) ? GetLastError() : 0; | ||
65 | - | #endif | ||
66 | - | 2-5 | suppressed: function cannot be solved git_error_vset (automatic due to inconsistent arc counts in .gcda files) int error_code = (error_class == GIT_ERROR_OS) ? errno : 0; | |
67 | - | 6 | suppressed: function cannot be solved git_error_vset (automatic due to inconsistent arc counts in .gcda files) git_buf *buf = &GIT_GLOBAL->error_buf; | |
68 | - | |||
69 | - | 7 | suppressed: function cannot be solved git_error_vset (automatic due to inconsistent arc counts in .gcda files) git_buf_clear(buf); | |
70 | - | 8 | suppressed: function cannot be solved git_error_vset (automatic due to inconsistent arc counts in .gcda files) if (fmt) { | |
71 | - | 9 | suppressed: function cannot be solved git_error_vset (automatic due to inconsistent arc counts in .gcda files) git_buf_vprintf(buf, fmt, ap); | |
72 | - | 10 | suppressed: function cannot be solved git_error_vset (automatic due to inconsistent arc counts in .gcda files) if (error_class == GIT_ERROR_OS) | |
73 | - | 11 | suppressed: function cannot be solved git_error_vset (automatic due to inconsistent arc counts in .gcda files) git_buf_PUTS(buf, ": "); | |
74 | - | } | ||
75 | - | |||
76 | - | 12 | suppressed: function cannot be solved git_error_vset (automatic due to inconsistent arc counts in .gcda files) if (error_class == GIT_ERROR_OS) { | |
77 | - | #ifdef GIT_WIN32 | ||
78 | - | char * win32_error = git_win32_get_error_message(win32_error_code); | ||
79 | - | if (win32_error) { | ||
80 | - | git_buf_puts(buf, win32_error); | ||
81 | - | git__free(win32_error); | ||
82 | - | |||
83 | - | SetLastError(0); | ||
84 | - | } | ||
85 | - | else | ||
86 | - | #endif | ||
87 | - | 13 | suppressed: function cannot be solved git_error_vset (automatic due to inconsistent arc counts in .gcda files) if (error_code) | |
88 | - | 14,15 | suppressed: function cannot be solved git_error_vset (automatic due to inconsistent arc counts in .gcda files) git_buf_puts(buf, strerror(error_code)); | |
89 | - | |||
90 | - | 16 | suppressed: function cannot be solved git_error_vset (automatic due to inconsistent arc counts in .gcda files) if (error_code) | |
91 | - | 17,18 | suppressed: function cannot be solved git_error_vset (automatic due to inconsistent arc counts in .gcda files) errno = 0; | |
92 | - | } | ||
93 | - | |||
94 | - | 19,20 | suppressed: function cannot be solved git_error_vset (automatic due to inconsistent arc counts in .gcda files) if (!git_buf_oom(buf)) | |
95 | - | 21 | suppressed: function cannot be solved git_error_vset (automatic due to inconsistent arc counts in .gcda files) set_error_from_buffer(error_class); | |
96 | - | 22 | suppressed: function cannot be solved git_error_vset (automatic due to inconsistent arc counts in .gcda files)} | |
97 | - | |||
98 | 189 | 2 | int git_error_set_str(int error_class, const char *string) | |
99 | - | { | ||
100 | 189 | 2 | git_buf *buf = &GIT_GLOBAL->error_buf; | |
101 | - | |||
102 | 189 | 3,4 | assert(string); | |
103 | - | |||
104 | 189 | 5 | if (!string) { | |
105 | ##### | 6 | git_error_set(GIT_ERROR_INVALID, "unspecified caller error"); | |
106 | ##### | 7 | return -1; | |
107 | - | } | ||
108 | - | |||
109 | 189 | 8 | git_buf_clear(buf); | |
110 | 189 | 9 | git_buf_puts(buf, string); | |
111 | - | |||
112 | 189 | 10,11 | if (git_buf_oom(buf)) | |
113 | ##### | 12 | return -1; | |
114 | - | |||
115 | 189 | 13 | set_error_from_buffer(error_class); | |
116 | 189 | 14 | return 0; | |
117 | - | } | ||
118 | - | |||
119 | 615320 | 2 | void git_error_clear(void) | |
120 | - | { | ||
121 | 615320 | 2,3 | if (GIT_GLOBAL->last_error != NULL) { | |
122 | 197100 | 4 | set_error(0, NULL); | |
123 | 196998 | 5,6 | GIT_GLOBAL->last_error = NULL; | |
124 | - | } | ||
125 | - | |||
126 | 613250 | 7 | errno = 0; | |
127 | - | #ifdef GIT_WIN32 | ||
128 | - | SetLastError(0); | ||
129 | - | #endif | ||
130 | 613314 | 8 | } | |
131 | - | |||
132 | 1839 | 2 | const git_error *git_error_last(void) | |
133 | - | { | ||
134 | 1839 | 2 | return GIT_GLOBAL->last_error; | |
135 | - | } | ||
136 | - | |||
137 | 342 | 2 | int git_error_state_capture(git_error_state *state, int error_code) | |
138 | - | { | ||
139 | 342 | 2 | git_error *error = GIT_GLOBAL->last_error; | |
140 | 342 | 3 | git_buf *error_buf = &GIT_GLOBAL->error_buf; | |
141 | - | |||
142 | 342 | 4 | memset(state, 0, sizeof(git_error_state)); | |
143 | - | |||
144 | 342 | 4 | if (!error_code) | |
145 | 1 | 5 | return 0; | |
146 | - | |||
147 | 341 | 6 | state->error_code = error_code; | |
148 | 341 | 6 | state->oom = (error == &g_git_oom_error); | |
149 | - | |||
150 | 341 | 6 | if (error) { | |
151 | 307 | 7 | state->error_msg.klass = error->klass; | |
152 | - | |||
153 | 307 | 7 | if (state->oom) | |
154 | 1 | 8 | state->error_msg.message = g_git_oom_error.message; | |
155 | - | else | ||
156 | 306 | 9,10 | state->error_msg.message = git_buf_detach(error_buf); | |
157 | - | } | ||
158 | - | |||
159 | 341 | 11 | git_error_clear(); | |
160 | 341 | 12 | return error_code; | |
161 | - | } | ||
162 | - | |||
163 | 306 | 2 | int git_error_state_restore(git_error_state *state) | |
164 | - | { | ||
165 | 306 | 2 | int ret = 0; | |
166 | - | |||
167 | 306 | 2 | git_error_clear(); | |
168 | - | |||
169 | 306 | 3,4 | if (state && state->error_msg.message) { | |
170 | 301 | 5 | if (state->oom) | |
171 | 1 | 6 | git_error_set_oom(); | |
172 | - | else | ||
173 | 300 | 7 | set_error(state->error_msg.klass, state->error_msg.message); | |
174 | - | |||
175 | 301 | 8 | ret = state->error_code; | |
176 | 301 | 8 | memset(state, 0, sizeof(git_error_state)); | |
177 | - | } | ||
178 | - | |||
179 | 306 | 9 | return ret; | |
180 | - | } | ||
181 | - | |||
182 | 36 | 2 | void git_error_state_free(git_error_state *state) | |
183 | - | { | ||
184 | 36 | 2 | if (!state) | |
185 | 36 | 3,7 | return; | |
186 | - | |||
187 | 36 | 4 | if (!state->oom) | |
188 | 36 | 5 | git__free(state->error_msg.message); | |
189 | - | |||
190 | 36 | 6 | memset(state, 0, sizeof(git_error_state)); | |
191 | - | } | ||
192 | - | |||
193 | ##### | 2 | int git_error_system_last(void) | |
194 | - | { | ||
195 | - | #ifdef GIT_WIN32 | ||
196 | - | return GetLastError(); | ||
197 | - | #else | ||
198 | ##### | 2 | return errno; | |
199 | - | #endif | ||
200 | - | } | ||
201 | - | |||
202 | ##### | 2 | void git_error_system_set(int code) | |
203 | - | { | ||
204 | - | #ifdef GIT_WIN32 | ||
205 | - | SetLastError(code); | ||
206 | - | #else | ||
207 | ##### | 2 | errno = code; | |
208 | - | #endif | ||
209 | ##### | 3 | } | |
210 | - | |||
211 | - | /* Deprecated error values and functions */ | ||
212 | - | |||
213 | - | #ifndef GIT_DEPRECATE_HARD | ||
214 | ##### | 2 | const git_error *giterr_last(void) | |
215 | - | { | ||
216 | ##### | 2 | return git_error_last(); | |
217 | - | } | ||
218 | - | |||
219 | ##### | 2 | void giterr_clear(void) | |
220 | - | { | ||
221 | ##### | 2 | git_error_clear(); | |
222 | ##### | 3 | } | |
223 | - | |||
224 | ##### | 2 | void giterr_set_str(int error_class, const char *string) | |
225 | - | { | ||
226 | ##### | 2 | git_error_set_str(error_class, string); | |
227 | ##### | 3 | } | |
228 | - | |||
229 | ##### | 2 | void giterr_set_oom(void) | |
230 | - | { | ||
231 | ##### | 2 | git_error_set_oom(); | |
232 | ##### | 3 | } | |
233 | - | #endif |