-
Notifications
You must be signed in to change notification settings - Fork 376
/
error_payload.c
309 lines (285 loc) · 6.94 KB
/
error_payload.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright 2010-2021, Tarantool AUTHORS, please see AUTHORS file.
*/
#include "error_payload.h"
#include "mp_uuid.h"
#include "msgpuck.h"
void
error_payload_create(struct error_payload *p)
{
p->count = 0;
p->fields = NULL;
}
void
error_payload_destroy(struct error_payload *p)
{
for (int i = 0; i < p->count; ++i) {
TRASH(p->fields[i]);
free(p->fields[i]);
}
free(p->fields);
TRASH(p);
}
/**
* Prepare a payload field to get a new value. If the field didn't exist, it is
* added. If it existed then it is reallocated if was necessary and should be
* overwritten. Name is already filled in the field. Only need to fill the
* value.
* Extra parameter allows to allocate extra memory for arbitrary usage after the
* error field and its value.
*/
static struct error_field *
error_payload_prepare(struct error_payload *e, const char *name,
uint32_t value_size, uint32_t extra)
{
struct error_field *f;
uint32_t name_size = strlen(name) + 1;
uint32_t data_offset = offsetof(struct error_field, name[name_size]);
uint32_t total = data_offset + value_size + extra;
struct error_field **fields = e->fields;
int count = e->count;
int i;
for (i = 0; i < count; ++i) {
f = fields[i];
if (strcmp(name, f->name) != 0)
continue;
f = xrealloc(f, total);
goto set;
}
e->count = ++count;
fields = xrealloc(fields, sizeof(fields[0]) * count);
e->fields = fields;
f = xmalloc(total);
memcpy(f->name, name, name_size);
set:
f->data = (char *)f + data_offset;
f->size = value_size;
fields[i] = f;
return f;
}
const char *
error_payload_get_str(const struct error_payload *e, const char *name)
{
const struct error_field *f = error_payload_find(e, name);
if (f == NULL)
return NULL;
const char *data = f->data;
if (mp_typeof(*data) != MP_STR)
return NULL;
uint32_t len;
data = mp_decode_str(&data, &len);
assert(data[len] == 0);
return data;
}
void
error_payload_set_str(struct error_payload *e, const char *name,
const char *value)
{
uint32_t len = strlen(value);
char *data = error_payload_prepare(
e, name, mp_sizeof_str(len), 1)->data;
/*
* 1 extra byte in the end is reserved to append 0 after the encoded
* string. To be able to return it from get() without copying.
*/
*mp_encode_str(data, value, len) = 0;
}
bool
error_payload_get_uint(const struct error_payload *e, const char *name,
uint64_t *value)
{
const struct error_field *f = error_payload_find(e, name);
if (f == NULL)
goto not_found;
const char *data = f->data;
if (mp_typeof(*data) != MP_UINT)
goto not_found;
*value = mp_decode_uint(&data);
return true;
not_found:
*value = 0;
return false;
}
void
error_payload_set_uint(struct error_payload *e, const char *name,
uint64_t value)
{
char *data = error_payload_prepare(
e, name, mp_sizeof_uint(value), 0)->data;
mp_encode_uint(data, value);
}
bool
error_payload_get_int(const struct error_payload *e, const char *name,
int64_t *value)
{
const struct error_field *f = error_payload_find(e, name);
if (f == NULL)
goto not_found;
const char *data = f->data;
if (mp_typeof(*data) == MP_UINT) {
uint64_t u = mp_decode_uint(&data);
if (u > INT64_MAX)
goto not_found;
*value = u;
return true;
} else if (mp_typeof(*data) == MP_INT) {
*value = mp_decode_int(&data);
return true;
}
not_found:
*value = 0;
return false;
}
void
error_payload_set_int(struct error_payload *e, const char *name, int64_t value)
{
if (value >= 0)
return error_payload_set_uint(e, name, value);
char *data = error_payload_prepare(
e, name, mp_sizeof_int(value), 0)->data;
mp_encode_int(data, value);
}
bool
error_payload_get_double(const struct error_payload *e, const char *name,
double *value)
{
const struct error_field *f = error_payload_find(e, name);
if (f == NULL)
goto not_found;
const char *data = f->data;
if (mp_typeof(*data) == MP_DOUBLE) {
*value = mp_decode_double(&data);
return true;
} else if (mp_typeof(*data) == MP_FLOAT) {
*value = mp_decode_float(&data);
return true;
}
not_found:
*value = 0;
return false;
}
void
error_payload_set_double(struct error_payload *e, const char *name,
double value)
{
char *data = error_payload_prepare(
e, name, mp_sizeof_double(value), 0)->data;
mp_encode_double(data, value);
}
bool
error_payload_get_bool(const struct error_payload *e, const char *name,
bool *value)
{
const struct error_field *f = error_payload_find(e, name);
if (f == NULL)
goto not_found;
const char *data = f->data;
if (mp_typeof(*data) != MP_BOOL)
goto not_found;
*value = mp_decode_bool(&data);
return true;
not_found:
*value = false;
return false;
}
void
error_payload_set_bool(struct error_payload *e, const char *name, bool value)
{
char *data = error_payload_prepare(
e, name, mp_sizeof_bool(value), 0)->data;
mp_encode_bool(data, value);
}
bool
error_payload_get_uuid(const struct error_payload *e, const char *name,
struct tt_uuid *uuid)
{
const struct error_field *f = error_payload_find(e, name);
if (f == NULL)
goto not_found;
const char *data = f->data;
if (mp_decode_uuid(&data, uuid) == NULL)
goto not_found;
return true;
not_found:
*uuid = uuid_nil;
return false;
}
void
error_payload_set_uuid(struct error_payload *e, const char *name,
const struct tt_uuid *uuid)
{
char *data = error_payload_prepare(e, name, mp_sizeof_uuid(), 0)->data;
mp_encode_uuid(data, uuid);
}
const char *
error_payload_get_mp(const struct error_payload *e, const char *name,
uint32_t *size)
{
const struct error_field *f = error_payload_find(e, name);
if (f == NULL) {
*size = 0;
return NULL;
}
*size = f->size;
return f->data;
}
void
error_payload_set_mp(struct error_payload *e, const char *name,
const char *src, uint32_t size)
{
char *data;
if (mp_typeof(*src) == MP_STR) {
data = error_payload_prepare(e, name, size, 1)->data;
/* @sa error_payload_set_str(). */
data[size] = 0;
} else {
data = error_payload_prepare(e, name, size, 0)->data;
}
memcpy(data, src, size);
}
void
error_payload_clear(struct error_payload *e, const char *name)
{
struct error_field **fields = e->fields;
for (int i = 0; i < e->count; ++i) {
struct error_field *f = fields[i];
if (strcmp(name, f->name) != 0)
continue;
TRASH(f);
free(f);
int count = --e->count;
if (count == 0) {
free(fields);
e->fields = NULL;
return;
}
/* Cyclic deletion. Order does not matter in a dictionary. */
fields[i] = fields[count];
return;
}
}
void
error_payload_move(struct error_payload *dst, struct error_payload *src)
{
for (int i = 0; i < dst->count; ++i) {
TRASH(dst->fields[i]);
free(dst->fields[i]);
}
free(dst->fields);
dst->fields = src->fields;
dst->count = src->count;
src->fields = NULL;
src->count = 0;
}
const struct error_field *
error_payload_find(const struct error_payload *e, const char *name)
{
for (int i = 0; i < e->count; ++i) {
const struct error_field *f = e->fields[i];
if (strcmp(name, f->name) == 0)
return f;
}
return NULL;
}