1 | // =============================================================== // |
---|
2 | // // |
---|
3 | // File : arbdb.cxx // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // =============================================================== // |
---|
10 | |
---|
11 | #if defined(DARWIN) |
---|
12 | #include <cstdio> |
---|
13 | #endif // DARWIN |
---|
14 | |
---|
15 | #include <rpc/types.h> |
---|
16 | #include <rpc/xdr.h> |
---|
17 | |
---|
18 | #include "ad_hcb.h" |
---|
19 | #include "gb_comm.h" |
---|
20 | #include "gb_compress.h" |
---|
21 | #include "gb_localdata.h" |
---|
22 | #include "gb_ta.h" |
---|
23 | #include "gb_ts.h" |
---|
24 | #include "gb_index.h" |
---|
25 | #include <arb_strarray.h> |
---|
26 | |
---|
27 | #include <glib.h> |
---|
28 | #include <glib/gprintf.h> |
---|
29 | |
---|
30 | gb_local_data *gb_local = 0; |
---|
31 | |
---|
32 | #define INIT_TYPE_NAME(t) GB_TYPES_name[t] = #t |
---|
33 | |
---|
34 | static const char *GB_TYPES_2_name(GB_TYPES type) { |
---|
35 | static const char *GB_TYPES_name[GB_TYPE_MAX]; |
---|
36 | static bool initialized = false; |
---|
37 | |
---|
38 | if (!initialized) { |
---|
39 | memset(GB_TYPES_name, 0, sizeof(GB_TYPES_name)); |
---|
40 | INIT_TYPE_NAME(GB_NONE); |
---|
41 | INIT_TYPE_NAME(GB_BIT); |
---|
42 | INIT_TYPE_NAME(GB_BYTE); |
---|
43 | INIT_TYPE_NAME(GB_INT); |
---|
44 | INIT_TYPE_NAME(GB_FLOAT); |
---|
45 | INIT_TYPE_NAME(GB_POINTER); |
---|
46 | INIT_TYPE_NAME(GB_BITS); |
---|
47 | INIT_TYPE_NAME(GB_BYTES); |
---|
48 | INIT_TYPE_NAME(GB_INTS); |
---|
49 | INIT_TYPE_NAME(GB_FLOATS); |
---|
50 | INIT_TYPE_NAME(GB_LINK); |
---|
51 | INIT_TYPE_NAME(GB_STRING); |
---|
52 | INIT_TYPE_NAME(GB_STRING_SHRT); |
---|
53 | INIT_TYPE_NAME(GB_DB); |
---|
54 | initialized = true; |
---|
55 | } |
---|
56 | |
---|
57 | const char *name = NULL; |
---|
58 | if (type >= 0 && type<GB_TYPE_MAX) name = GB_TYPES_name[type]; |
---|
59 | if (!name) { |
---|
60 | static char *unknownType = 0; |
---|
61 | freeset(unknownType, GBS_global_string_copy("<invalid-type=%i>", type)); |
---|
62 | name = unknownType; |
---|
63 | } |
---|
64 | return name; |
---|
65 | } |
---|
66 | |
---|
67 | const char *GB_get_type_name(GBDATA *gbd) { |
---|
68 | return GB_TYPES_2_name(gbd->type()); |
---|
69 | } |
---|
70 | |
---|
71 | inline GB_ERROR gb_transactable_type(GB_TYPES type, GBDATA *gbd) { |
---|
72 | GB_ERROR error = NULL; |
---|
73 | if (GB_MAIN(gbd)->get_transaction_level() == 0) { |
---|
74 | error = "No transaction running"; |
---|
75 | } |
---|
76 | else if (GB_ARRAY_FLAGS(gbd).changed == GB_DELETED) { |
---|
77 | error = "Entry has been deleted"; |
---|
78 | } |
---|
79 | else { |
---|
80 | GB_TYPES gb_type = gbd->type(); |
---|
81 | if (gb_type != type && (type != GB_STRING || gb_type != GB_LINK)) { |
---|
82 | char *rtype = strdup(GB_TYPES_2_name(type)); |
---|
83 | char *rgb_type = strdup(GB_TYPES_2_name(gb_type)); |
---|
84 | |
---|
85 | error = GBS_global_string("type mismatch (want='%s', got='%s') in '%s'", rtype, rgb_type, GB_get_db_path(gbd)); |
---|
86 | |
---|
87 | free(rgb_type); |
---|
88 | free(rtype); |
---|
89 | } |
---|
90 | } |
---|
91 | if (error) { |
---|
92 | GBK_dump_backtrace(stderr, error); // it's a bug: none of the above errors should ever happen |
---|
93 | gb_assert(0); |
---|
94 | } |
---|
95 | return error; |
---|
96 | } |
---|
97 | |
---|
98 | __ATTR__USERESULT static GB_ERROR gb_security_error(GBDATA *gbd) { |
---|
99 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
100 | const char *error = GBS_global_string("Protection: Attempt to change a level-%i-'%s'-entry,\n" |
---|
101 | "but your current security level is only %i", |
---|
102 | GB_GET_SECURITY_WRITE(gbd), |
---|
103 | GB_read_key_pntr(gbd), |
---|
104 | Main->security_level); |
---|
105 | #if defined(DEBUG) |
---|
106 | fprintf(stderr, "%s\n", error); |
---|
107 | #endif // DEBUG |
---|
108 | return error; |
---|
109 | } |
---|
110 | |
---|
111 | inline GB_ERROR gb_type_writeable_to(GB_TYPES type, GBDATA *gbd) { |
---|
112 | GB_ERROR error = gb_transactable_type(type, gbd); |
---|
113 | if (!error) { |
---|
114 | if (GB_GET_SECURITY_WRITE(gbd) > GB_MAIN(gbd)->security_level) { |
---|
115 | error = gb_security_error(gbd); |
---|
116 | } |
---|
117 | } |
---|
118 | return error; |
---|
119 | } |
---|
120 | inline GB_ERROR gb_type_readable_from(GB_TYPES type, GBDATA *gbd) { |
---|
121 | return gb_transactable_type(type, gbd); |
---|
122 | } |
---|
123 | |
---|
124 | inline GB_ERROR error_with_dbentry(const char *action, GBDATA *gbd, GB_ERROR error) { |
---|
125 | if (error) { |
---|
126 | char *error_copy = strdup(error); |
---|
127 | const char *path = GB_get_db_path(gbd); |
---|
128 | error = GBS_global_string("Can't %s '%s':\n%s", action, path, error_copy); |
---|
129 | free(error_copy); |
---|
130 | } |
---|
131 | return error; |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | #define RETURN_ERROR_IF_NOT_WRITEABLE_AS_TYPE(gbd, type) \ |
---|
136 | do { \ |
---|
137 | GB_ERROR error = gb_type_writeable_to(type, gbd); \ |
---|
138 | if (error) { \ |
---|
139 | return error_with_dbentry("write", gbd, error); \ |
---|
140 | } \ |
---|
141 | } while(0) |
---|
142 | |
---|
143 | #define EXPORT_ERROR_AND_RETURN_0_IF_NOT_READABLE_AS_TYPE(gbd, type) \ |
---|
144 | do { \ |
---|
145 | GB_ERROR error = gb_type_readable_from(type, gbd); \ |
---|
146 | if (error) { \ |
---|
147 | error = error_with_dbentry("read", gbd, error); \ |
---|
148 | GB_export_error(error); \ |
---|
149 | return 0; \ |
---|
150 | } \ |
---|
151 | } while(0) \ |
---|
152 | |
---|
153 | |
---|
154 | #if defined(WARN_TODO) |
---|
155 | #warning replace GB_TEST_READ / GB_TEST_READ by new names later |
---|
156 | #endif |
---|
157 | |
---|
158 | #define GB_TEST_READ(gbd, type, ignored) EXPORT_ERROR_AND_RETURN_0_IF_NOT_READABLE_AS_TYPE(gbd, type) |
---|
159 | #define GB_TEST_WRITE(gbd, type, ignored) RETURN_ERROR_IF_NOT_WRITEABLE_AS_TYPE(gbd, type) |
---|
160 | |
---|
161 | #define GB_TEST_NON_BUFFER(x, gerror) \ |
---|
162 | do { \ |
---|
163 | if (GB_is_in_buffer(x)) { \ |
---|
164 | GBK_terminatef("%s: you are not allowed to write any data, which you get by pntr", gerror); \ |
---|
165 | } \ |
---|
166 | } while (0) |
---|
167 | |
---|
168 | |
---|
169 | static GB_ERROR GB_safe_atof(const char *str, double *res) { |
---|
170 | GB_ERROR error = NULL; |
---|
171 | char *end; |
---|
172 | *res = strtod(str, &end); |
---|
173 | if (end == str || end[0] != 0) { |
---|
174 | if (!str[0]) { |
---|
175 | *res = 0.0; |
---|
176 | } |
---|
177 | else { |
---|
178 | error = GBS_global_string("cannot convert '%s' to double", str); |
---|
179 | } |
---|
180 | } |
---|
181 | return error; |
---|
182 | } |
---|
183 | |
---|
184 | double GB_atof(const char *str) { |
---|
185 | // convert ASCII to double |
---|
186 | double res = 0; |
---|
187 | GB_ERROR err = GB_safe_atof(str, &res); |
---|
188 | if (err) { |
---|
189 | // expected double in 'str'- better use GB_safe_atof() |
---|
190 | GBK_terminatef("GB_safe_atof(\"%s\", ..) returns error: %s", str, err); |
---|
191 | } |
---|
192 | return res; |
---|
193 | } |
---|
194 | |
---|
195 | // --------------------------- |
---|
196 | // compression tables |
---|
197 | |
---|
198 | const int gb_convert_type_2_compression_flags[] = { |
---|
199 | GB_COMPRESSION_NONE, // GB_NONE 0 |
---|
200 | GB_COMPRESSION_NONE, // GB_BIT 1 |
---|
201 | GB_COMPRESSION_NONE, // GB_BYTE 2 |
---|
202 | GB_COMPRESSION_NONE, // GB_INT 3 |
---|
203 | GB_COMPRESSION_NONE, // GB_FLOAT 4 |
---|
204 | GB_COMPRESSION_NONE, // GB_?? 5 |
---|
205 | GB_COMPRESSION_BITS, // GB_BITS 6 |
---|
206 | GB_COMPRESSION_NONE, // GB_?? 7 |
---|
207 | GB_COMPRESSION_RUNLENGTH | GB_COMPRESSION_HUFFMANN, // GB_BYTES 8 |
---|
208 | GB_COMPRESSION_RUNLENGTH | GB_COMPRESSION_HUFFMANN | GB_COMPRESSION_SORTBYTES, // GB_INTS 9 |
---|
209 | GB_COMPRESSION_RUNLENGTH | GB_COMPRESSION_HUFFMANN | GB_COMPRESSION_SORTBYTES, // GB_FLTS 10 |
---|
210 | GB_COMPRESSION_NONE, // GB_LINK 11 |
---|
211 | GB_COMPRESSION_RUNLENGTH | GB_COMPRESSION_HUFFMANN | GB_COMPRESSION_DICTIONARY, // GB_STR 12 |
---|
212 | GB_COMPRESSION_NONE, // GB_STRS 13 |
---|
213 | GB_COMPRESSION_NONE, // GB?? 14 |
---|
214 | GB_COMPRESSION_NONE // GB_DB 15 |
---|
215 | }; |
---|
216 | |
---|
217 | int gb_convert_type_2_sizeof[] = { /* contains the unit-size of data stored in DB, |
---|
218 | * i.e. realsize = unit_size * size() |
---|
219 | */ |
---|
220 | 0, // GB_NONE 0 |
---|
221 | 0, // GB_BIT 1 |
---|
222 | sizeof(char), // GB_BYTE 2 |
---|
223 | sizeof(int), // GB_INT 3 |
---|
224 | sizeof(float), // GB_FLOAT 4 |
---|
225 | 0, // GB_?? 5 |
---|
226 | 0, // GB_BITS 6 |
---|
227 | 0, // GB_?? 7 |
---|
228 | sizeof(char), // GB_BYTES 8 |
---|
229 | sizeof(int), // GB_INTS 9 |
---|
230 | sizeof(float), // GB_FLTS 10 |
---|
231 | sizeof(char), // GB_LINK 11 |
---|
232 | sizeof(char), // GB_STR 12 |
---|
233 | sizeof(char), // GB_STRS 13 |
---|
234 | 0, // GB_?? 14 |
---|
235 | 0, // GB_DB 15 |
---|
236 | }; |
---|
237 | |
---|
238 | int gb_convert_type_2_appendix_size[] = { /* contains the size of the suffix (aka terminator element) |
---|
239 | * size is in bytes |
---|
240 | */ |
---|
241 | |
---|
242 | 0, // GB_NONE 0 |
---|
243 | 0, // GB_BIT 1 |
---|
244 | 0, // GB_BYTE 2 |
---|
245 | 0, // GB_INT 3 |
---|
246 | 0, // GB_FLOAT 4 |
---|
247 | 0, // GB_?? 5 |
---|
248 | 0, // GB_BITS 6 |
---|
249 | 0, // GB_?? 7 |
---|
250 | 0, // GB_BYTES 8 |
---|
251 | 0, // GB_INTS 9 |
---|
252 | 0, // GB_FLTS 10 |
---|
253 | 1, // GB_LINK 11 (zero terminated) |
---|
254 | 1, // GB_STR 12 (zero terminated) |
---|
255 | 1, // GB_STRS 13 (zero terminated) |
---|
256 | 0, // GB_?? 14 |
---|
257 | 0, // GB_DB 15 |
---|
258 | }; |
---|
259 | |
---|
260 | |
---|
261 | // --------------------------------- |
---|
262 | // local buffer management |
---|
263 | |
---|
264 | static void init_buffer(gb_buffer *buf, size_t initial_size) { |
---|
265 | buf->size = initial_size; |
---|
266 | buf->mem = buf->size ? (char*)malloc(buf->size) : NULL; |
---|
267 | } |
---|
268 | |
---|
269 | static char *check_out_buffer(gb_buffer *buf) { |
---|
270 | char *checkOut = buf->mem; |
---|
271 | |
---|
272 | buf->mem = 0; |
---|
273 | buf->size = 0; |
---|
274 | |
---|
275 | return checkOut; |
---|
276 | } |
---|
277 | |
---|
278 | static void alloc_buffer(gb_buffer *buf, size_t size) { |
---|
279 | free(buf->mem); |
---|
280 | buf->size = size; |
---|
281 | #if (MEMORY_TEST==1) |
---|
282 | buf->mem = (char *)malloc(buf->size); |
---|
283 | #else |
---|
284 | buf->mem = (char *)GB_calloc(buf->size, 1); |
---|
285 | #endif |
---|
286 | } |
---|
287 | |
---|
288 | static GB_BUFFER give_buffer(gb_buffer *buf, size_t size) { |
---|
289 | #if (MEMORY_TEST==1) |
---|
290 | alloc_buffer(buf, size); // do NOT reuse buffer if testing memory |
---|
291 | #else |
---|
292 | if (size >= buf->size) { |
---|
293 | alloc_buffer(buf, size); |
---|
294 | } |
---|
295 | #endif |
---|
296 | return buf->mem; |
---|
297 | } |
---|
298 | |
---|
299 | static int is_in_buffer(gb_buffer *buf, GB_CBUFFER ptr) { |
---|
300 | return ptr >= buf->mem && ptr < buf->mem+buf->size; |
---|
301 | } |
---|
302 | |
---|
303 | // ------------------------------ |
---|
304 | |
---|
305 | GB_BUFFER GB_give_buffer(size_t size) { |
---|
306 | // return a pointer to a static piece of memory at least size bytes long |
---|
307 | return give_buffer(&gb_local->buf1, size); |
---|
308 | } |
---|
309 | |
---|
310 | GB_BUFFER GB_increase_buffer(size_t size) { |
---|
311 | if (size < gb_local->buf1.size) { |
---|
312 | char *old_buffer = gb_local->buf1.mem; |
---|
313 | size_t old_size = gb_local->buf1.size; |
---|
314 | |
---|
315 | gb_local->buf1.mem = NULL; |
---|
316 | alloc_buffer(&gb_local->buf1, size); |
---|
317 | memcpy(gb_local->buf1.mem, old_buffer, old_size); |
---|
318 | |
---|
319 | free(old_buffer); |
---|
320 | } |
---|
321 | return gb_local->buf1.mem; |
---|
322 | } |
---|
323 | |
---|
324 | NOT4PERL int GB_give_buffer_size() { |
---|
325 | return gb_local->buf1.size; |
---|
326 | } |
---|
327 | |
---|
328 | GB_BUFFER GB_give_buffer2(long size) { |
---|
329 | return give_buffer(&gb_local->buf2, size); |
---|
330 | } |
---|
331 | |
---|
332 | static int GB_is_in_buffer(GB_CBUFFER ptr) { |
---|
333 | /* returns 1 or 2 if 'ptr' points to gb_local->buf1/buf2 |
---|
334 | * returns 0 otherwise |
---|
335 | */ |
---|
336 | int buffer = 0; |
---|
337 | |
---|
338 | if (is_in_buffer(&gb_local->buf1, ptr)) buffer = 1; |
---|
339 | else if (is_in_buffer(&gb_local->buf2, ptr)) buffer = 2; |
---|
340 | |
---|
341 | return buffer; |
---|
342 | } |
---|
343 | |
---|
344 | char *GB_check_out_buffer(GB_CBUFFER buffer) { |
---|
345 | /* Check a piece of memory out of the buffer management |
---|
346 | * after it is checked out, the user has the full control to use and free it |
---|
347 | * Returns a pointer to the start of the buffer (even if 'buffer' points inside the buffer!) |
---|
348 | */ |
---|
349 | char *old = 0; |
---|
350 | |
---|
351 | if (is_in_buffer(&gb_local->buf1, buffer)) old = check_out_buffer(&gb_local->buf1); |
---|
352 | else if (is_in_buffer(&gb_local->buf2, buffer)) old = check_out_buffer(&gb_local->buf2); |
---|
353 | |
---|
354 | return old; |
---|
355 | } |
---|
356 | |
---|
357 | GB_BUFFER GB_give_other_buffer(GB_CBUFFER buffer, long size) { |
---|
358 | return is_in_buffer(&gb_local->buf1, buffer) |
---|
359 | ? GB_give_buffer2(size) |
---|
360 | : GB_give_buffer(size); |
---|
361 | } |
---|
362 | |
---|
363 | static unsigned char GB_BIT_compress_data[] = { |
---|
364 | 0x1d, GB_CS_OK, 0, 0, |
---|
365 | 0x04, GB_CS_OK, 0, 1, |
---|
366 | 0x0a, GB_CS_OK, 0, 2, |
---|
367 | 0x0b, GB_CS_OK, 0, 3, |
---|
368 | 0x0c, GB_CS_OK, 0, 4, |
---|
369 | 0x1a, GB_CS_OK, 0, 5, |
---|
370 | 0x1b, GB_CS_OK, 0, 6, |
---|
371 | 0x1c, GB_CS_OK, 0, 7, |
---|
372 | 0xf0, GB_CS_OK, 0, 8, |
---|
373 | 0xf1, GB_CS_OK, 0, 9, |
---|
374 | 0xf2, GB_CS_OK, 0, 10, |
---|
375 | 0xf3, GB_CS_OK, 0, 11, |
---|
376 | 0xf4, GB_CS_OK, 0, 12, |
---|
377 | 0xf5, GB_CS_OK, 0, 13, |
---|
378 | 0xf6, GB_CS_OK, 0, 14, |
---|
379 | 0xf7, GB_CS_OK, 0, 15, |
---|
380 | 0xf8, GB_CS_SUB, 0, 16, |
---|
381 | 0xf9, GB_CS_SUB, 0, 32, |
---|
382 | 0xfa, GB_CS_SUB, 0, 48, |
---|
383 | 0xfb, GB_CS_SUB, 0, 64, |
---|
384 | 0xfc, GB_CS_SUB, 0, 128, |
---|
385 | 0xfd, GB_CS_SUB, 1, 0, |
---|
386 | 0xfe, GB_CS_SUB, 2, 0, |
---|
387 | 0xff, GB_CS_SUB, 4, 0, |
---|
388 | 0 |
---|
389 | }; |
---|
390 | |
---|
391 | struct gb_exitfun { |
---|
392 | void (*exitfun)(); |
---|
393 | gb_exitfun *next; |
---|
394 | }; |
---|
395 | |
---|
396 | void GB_atexit(void (*exitfun)()) { |
---|
397 | // called when GB_shell is destroyed (use similar to atexit()) |
---|
398 | // |
---|
399 | // Since the program does not neccessarily terminate, your code calling |
---|
400 | // GB_atexit() may run multiple times. Make sure everything is completely reset by your 'exitfun' |
---|
401 | |
---|
402 | gb_exitfun *fun = new gb_exitfun; |
---|
403 | fun->exitfun = exitfun; |
---|
404 | |
---|
405 | fun->next = gb_local->atgbexit; |
---|
406 | gb_local->atgbexit = fun; |
---|
407 | } |
---|
408 | |
---|
409 | static void run_and_destroy_exit_functions(gb_exitfun *fun) { |
---|
410 | if (fun) { |
---|
411 | fun->exitfun(); |
---|
412 | run_and_destroy_exit_functions(fun->next); |
---|
413 | delete fun; |
---|
414 | } |
---|
415 | } |
---|
416 | |
---|
417 | static void GB_exit_gb() { |
---|
418 | GB_shell::ensure_inside(); |
---|
419 | |
---|
420 | if (gb_local) { |
---|
421 | gb_local->~gb_local_data(); // inplace-dtor |
---|
422 | gbm_free_mem(gb_local, sizeof(*gb_local), 0); |
---|
423 | gb_local = NULL; |
---|
424 | gbm_flush_mem(); |
---|
425 | } |
---|
426 | } |
---|
427 | |
---|
428 | gb_local_data::~gb_local_data() { |
---|
429 | gb_assert(openedDBs == closedDBs); |
---|
430 | |
---|
431 | run_and_destroy_exit_functions(atgbexit); |
---|
432 | |
---|
433 | free(bitcompress); |
---|
434 | gb_free_compress_tree(bituncompress); |
---|
435 | free(write_buffer); |
---|
436 | |
---|
437 | free(check_out_buffer(&buf2)); |
---|
438 | free(check_out_buffer(&buf1)); |
---|
439 | free(open_gb_mains); |
---|
440 | } |
---|
441 | |
---|
442 | // ----------------- |
---|
443 | // GB_shell |
---|
444 | |
---|
445 | |
---|
446 | static GB_shell *inside_shell = NULL; |
---|
447 | |
---|
448 | GB_shell::GB_shell() { |
---|
449 | if (inside_shell) GBK_terminate("only one GB_shell allowed"); |
---|
450 | inside_shell = this; |
---|
451 | } |
---|
452 | GB_shell::~GB_shell() { |
---|
453 | gb_assert(inside_shell == this); |
---|
454 | GB_exit_gb(); |
---|
455 | inside_shell = NULL; |
---|
456 | } |
---|
457 | void GB_shell::ensure_inside() { if (!inside_shell) GBK_terminate("Not inside GB_shell"); } |
---|
458 | |
---|
459 | bool GB_shell::in_shell() { // used by code based on ARBDB (Kai IIRC) |
---|
460 | return inside_shell; |
---|
461 | } |
---|
462 | |
---|
463 | struct GB_test_shell_closed { |
---|
464 | ~GB_test_shell_closed() { |
---|
465 | if (GB_shell::in_shell()) { // leave that call |
---|
466 | inside_shell->~GB_shell(); // call dtor |
---|
467 | } |
---|
468 | } |
---|
469 | }; |
---|
470 | static GB_test_shell_closed test; |
---|
471 | |
---|
472 | #if defined(UNIT_TESTS) |
---|
473 | static bool closed_open_shell_for_unit_tests() { |
---|
474 | bool was_open = inside_shell; |
---|
475 | if (was_open) { |
---|
476 | if (gb_local) gb_local->fake_closed_DBs(); |
---|
477 | inside_shell->~GB_shell(); // just call dtor (not delete) |
---|
478 | } |
---|
479 | return was_open; |
---|
480 | } |
---|
481 | #endif |
---|
482 | |
---|
483 | void GB_init_gb() { |
---|
484 | GB_shell::ensure_inside(); |
---|
485 | if (!gb_local) { |
---|
486 | GBK_install_SIGSEGV_handler(true); // never uninstalled |
---|
487 | gbm_init_mem(); |
---|
488 | gb_local = (gb_local_data *)gbm_get_mem(sizeof(gb_local_data), 0); |
---|
489 | ::new(gb_local) gb_local_data(); // inplace-ctor |
---|
490 | } |
---|
491 | } |
---|
492 | |
---|
493 | int GB_open_DBs() { return gb_local ? gb_local->open_dbs() : 0; } |
---|
494 | |
---|
495 | gb_local_data::gb_local_data() |
---|
496 | { |
---|
497 | init_buffer(&buf1, 4000); |
---|
498 | init_buffer(&buf2, 4000); |
---|
499 | |
---|
500 | write_bufsize = GBCM_BUFFER; |
---|
501 | write_buffer = (char *)malloc((size_t)write_bufsize); |
---|
502 | write_ptr = write_buffer; |
---|
503 | write_free = write_bufsize; |
---|
504 | |
---|
505 | bituncompress = gb_build_uncompress_tree(GB_BIT_compress_data, 1, 0); |
---|
506 | bitcompress = gb_build_compress_list(GB_BIT_compress_data, 1, &(bc_size)); |
---|
507 | |
---|
508 | openedDBs = 0; |
---|
509 | closedDBs = 0; |
---|
510 | |
---|
511 | open_gb_mains = NULL; |
---|
512 | open_gb_alloc = 0; |
---|
513 | |
---|
514 | atgbexit = NULL; |
---|
515 | |
---|
516 | iamclient = false; |
---|
517 | search_system_folder = false; |
---|
518 | running_client_transaction = ARB_NO_TRANS; |
---|
519 | } |
---|
520 | |
---|
521 | void gb_local_data::announce_db_open(GB_MAIN_TYPE *Main) { |
---|
522 | gb_assert(Main); |
---|
523 | int idx = open_dbs(); |
---|
524 | if (idx >= open_gb_alloc) { |
---|
525 | int new_alloc = open_gb_alloc+10; |
---|
526 | GB_MAIN_TYPE **new_mains = (GB_MAIN_TYPE**)realloc(open_gb_mains, new_alloc*sizeof(*new_mains)); |
---|
527 | memset(new_mains+open_gb_alloc, 0, 10*sizeof(*new_mains)); |
---|
528 | open_gb_alloc = new_alloc; |
---|
529 | open_gb_mains = new_mains; |
---|
530 | } |
---|
531 | open_gb_mains[idx] = Main; |
---|
532 | openedDBs++; |
---|
533 | } |
---|
534 | |
---|
535 | void gb_local_data::announce_db_close(GB_MAIN_TYPE *Main) { |
---|
536 | gb_assert(Main); |
---|
537 | int open = open_dbs(); |
---|
538 | int idx; |
---|
539 | for (idx = 0; idx<open; ++idx) if (open_gb_mains[idx] == Main) break; |
---|
540 | |
---|
541 | gb_assert(idx<open); // passed gb_main is unknown |
---|
542 | if (idx<open) { |
---|
543 | if (idx<(open-1)) { // not last |
---|
544 | open_gb_mains[idx] = open_gb_mains[open-1]; |
---|
545 | } |
---|
546 | closedDBs++; |
---|
547 | } |
---|
548 | if (closedDBs == openedDBs) { |
---|
549 | GB_exit_gb(); // free most memory allocated by ARBDB library |
---|
550 | // Caution: calling GB_exit_gb() frees 'this'! |
---|
551 | } |
---|
552 | } |
---|
553 | |
---|
554 | static GBDATA *gb_remembered_db() { |
---|
555 | GB_MAIN_TYPE *Main = gb_local ? gb_local->get_any_open_db() : NULL; |
---|
556 | return Main ? Main->gb_main() : NULL; |
---|
557 | } |
---|
558 | |
---|
559 | GB_ERROR gb_unfold(GBCONTAINER *gbc, long deep, int index_pos) { |
---|
560 | /*! get data from server. |
---|
561 | * |
---|
562 | * @param gbc container to unfold |
---|
563 | * @param deep if != 0, then get subitems too. |
---|
564 | * @param index_pos |
---|
565 | * - >= 0, get indexed item from server |
---|
566 | * - <0, get all items |
---|
567 | * |
---|
568 | * @return error on failure |
---|
569 | */ |
---|
570 | |
---|
571 | GB_ERROR error; |
---|
572 | gb_header_list *header = GB_DATA_LIST_HEADER(gbc->d); |
---|
573 | |
---|
574 | if (!gbc->flags2.folded_container) return 0; |
---|
575 | if (index_pos> gbc->d.nheader) gb_create_header_array(gbc, index_pos + 1); |
---|
576 | if (index_pos >= 0 && GB_HEADER_LIST_GBD(header[index_pos])) return 0; |
---|
577 | |
---|
578 | if (GBCONTAINER_MAIN(gbc)->is_server()) { |
---|
579 | GB_internal_error("Cannot unfold in server"); |
---|
580 | return 0; |
---|
581 | } |
---|
582 | |
---|
583 | do { |
---|
584 | if (index_pos<0) break; |
---|
585 | if (index_pos >= gbc->d.nheader) break; |
---|
586 | if (header[index_pos].flags.changed >= GB_DELETED) { |
---|
587 | GB_internal_error("Tried to unfold a deleted item"); |
---|
588 | return 0; |
---|
589 | } |
---|
590 | if (GB_HEADER_LIST_GBD(header[index_pos])) return 0; // already unfolded |
---|
591 | } while (0); |
---|
592 | |
---|
593 | error = gbcm_unfold_client(gbc, deep, index_pos); |
---|
594 | if (error) { |
---|
595 | GB_print_error(); |
---|
596 | return error; |
---|
597 | } |
---|
598 | |
---|
599 | if (index_pos<0) { |
---|
600 | gb_untouch_children(gbc); |
---|
601 | gbc->flags2.folded_container = 0; |
---|
602 | } |
---|
603 | else { |
---|
604 | GBDATA *gb2 = GBCONTAINER_ELEM(gbc, index_pos); |
---|
605 | if (gb2) { |
---|
606 | if (gb2->is_container()) { |
---|
607 | gb_untouch_children_and_me(gb2->as_container()); |
---|
608 | } |
---|
609 | else { |
---|
610 | gb_untouch_me(gb2->as_entry()); |
---|
611 | } |
---|
612 | } |
---|
613 | } |
---|
614 | return 0; |
---|
615 | } |
---|
616 | |
---|
617 | // ----------------------- |
---|
618 | // close database |
---|
619 | |
---|
620 | typedef void (*gb_close_callback)(GBDATA *gb_main, void *client_data); |
---|
621 | |
---|
622 | struct gb_close_callback_list { |
---|
623 | gb_close_callback_list *next; |
---|
624 | gb_close_callback cb; |
---|
625 | void *client_data; |
---|
626 | }; |
---|
627 | |
---|
628 | #if defined(ASSERTION_USED) |
---|
629 | static bool atclose_cb_exists(gb_close_callback_list *gccs, gb_close_callback cb) { |
---|
630 | return gccs && (gccs->cb == cb || atclose_cb_exists(gccs->next, cb)); |
---|
631 | } |
---|
632 | #endif // ASSERTION_USED |
---|
633 | |
---|
634 | void GB_atclose(GBDATA *gbd, void (*fun)(GBDATA *gb_main, void *client_data), void *client_data) { |
---|
635 | /*! Add a callback, which gets called directly before GB_close destroys all data. |
---|
636 | * This is the recommended way to remove all callbacks from DB elements. |
---|
637 | */ |
---|
638 | |
---|
639 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
640 | |
---|
641 | gb_assert(!atclose_cb_exists(Main->close_callbacks, fun)); // each close callback should only exist once |
---|
642 | |
---|
643 | gb_close_callback_list *gccs = (gb_close_callback_list *)malloc(sizeof(*gccs)); |
---|
644 | |
---|
645 | gccs->next = Main->close_callbacks; |
---|
646 | gccs->cb = fun; |
---|
647 | gccs->client_data = client_data; |
---|
648 | |
---|
649 | Main->close_callbacks = gccs; |
---|
650 | } |
---|
651 | |
---|
652 | static void run_close_callbacks(GBDATA *gb_main, gb_close_callback_list *gccs) { |
---|
653 | while (gccs) { |
---|
654 | gccs->cb(gb_main, gccs->client_data); |
---|
655 | gb_close_callback_list *next = gccs->next; |
---|
656 | free(gccs); |
---|
657 | gccs = next; |
---|
658 | } |
---|
659 | } |
---|
660 | |
---|
661 | static gb_triggered_callback *currently_called_back = NULL; // points to callback during callback; NULL otherwise |
---|
662 | static GB_MAIN_TYPE *inside_callback_main = NULL; // points to DB root during callback; NULL otherwise |
---|
663 | |
---|
664 | void gb_pending_callbacks::call_and_forget(GB_CB_TYPE allowedTypes) { |
---|
665 | #if defined(ASSERTION_USED) |
---|
666 | const gb_triggered_callback *tail = get_tail(); |
---|
667 | #endif |
---|
668 | |
---|
669 | for (itertype cb = callbacks.begin(); cb != callbacks.end(); ++cb) { |
---|
670 | currently_called_back = &*cb; |
---|
671 | gb_assert(currently_called_back); |
---|
672 | currently_called_back->spec(cb->gbd, allowedTypes); |
---|
673 | currently_called_back = NULL; |
---|
674 | } |
---|
675 | |
---|
676 | gb_assert(tail == get_tail()); |
---|
677 | |
---|
678 | callbacks.clear(); |
---|
679 | } |
---|
680 | |
---|
681 | void GB_MAIN_TYPE::call_pending_callbacks() { |
---|
682 | inside_callback_main = this; |
---|
683 | |
---|
684 | deleteCBs.pending.call_and_forget(GB_CB_DELETE); // first all delete callbacks: |
---|
685 | changeCBs.pending.call_and_forget(GB_CB_ALL_BUT_DELETE); // then all change callbacks: |
---|
686 | |
---|
687 | inside_callback_main = NULL; |
---|
688 | } |
---|
689 | |
---|
690 | inline void GB_MAIN_TYPE::callback_group::forget_hcbs() { |
---|
691 | delete hierarchy_cbs; |
---|
692 | hierarchy_cbs = NULL; |
---|
693 | } |
---|
694 | |
---|
695 | void GB_MAIN_TYPE::forget_hierarchy_cbs() { |
---|
696 | changeCBs.forget_hcbs(); |
---|
697 | deleteCBs.forget_hcbs(); |
---|
698 | } |
---|
699 | |
---|
700 | void GB_close(GBDATA *gbd) { |
---|
701 | GB_ERROR error = NULL; |
---|
702 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
703 | |
---|
704 | gb_assert(Main->get_transaction_level() <= 0); // transaction running - you can't close DB yet! |
---|
705 | |
---|
706 | Main->forget_hierarchy_cbs(); |
---|
707 | |
---|
708 | gb_assert(Main->gb_main() == gbd); |
---|
709 | run_close_callbacks(gbd, Main->close_callbacks); |
---|
710 | Main->close_callbacks = 0; |
---|
711 | |
---|
712 | if (Main->is_client()) { |
---|
713 | long result = gbcmc_close(Main->c_link); |
---|
714 | if (result != 0) error = GBS_global_string("gbcmc_close returns %li", result); |
---|
715 | } |
---|
716 | |
---|
717 | gbcm_logout(Main, NULL); // logout default user |
---|
718 | |
---|
719 | if (!error) { |
---|
720 | gb_assert(Main->close_callbacks == 0); |
---|
721 | |
---|
722 | gb_delete_dummy_father(Main->dummy_father); |
---|
723 | Main->root_container = NULL; |
---|
724 | |
---|
725 | /* ARBDB applications using awars easily crash in call_pending_callbacks(), |
---|
726 | * if AWARs are still bound to elements in the closed database. |
---|
727 | * |
---|
728 | * To unlink awars call AW_root::unlink_awars_from_DB(). |
---|
729 | * If that doesn't help, test Main->data (often aka as GLOBAL_gb_main) |
---|
730 | */ |
---|
731 | Main->call_pending_callbacks(); // do all callbacks |
---|
732 | delete Main; |
---|
733 | } |
---|
734 | |
---|
735 | if (error) { |
---|
736 | GB_warningf("Error in GB_close: %s", error); |
---|
737 | } |
---|
738 | } |
---|
739 | |
---|
740 | void gb_abort_and_close_all_DBs() { |
---|
741 | GBDATA *gb_main; |
---|
742 | while ((gb_main = gb_remembered_db())) { |
---|
743 | // abort any open transactions |
---|
744 | GB_MAIN_TYPE *Main = GB_MAIN(gb_main); |
---|
745 | while (Main->get_transaction_level()>0) { |
---|
746 | Main->abort_transaction(); |
---|
747 | } |
---|
748 | // and close DB |
---|
749 | GB_close(gb_main); |
---|
750 | } |
---|
751 | } |
---|
752 | |
---|
753 | // ------------------ |
---|
754 | // read data |
---|
755 | |
---|
756 | long GB_read_int(GBDATA *gbd) |
---|
757 | { |
---|
758 | GB_TEST_READ(gbd, GB_INT, "GB_read_int"); |
---|
759 | return gbd->as_entry()->info.i; |
---|
760 | } |
---|
761 | |
---|
762 | int GB_read_byte(GBDATA *gbd) |
---|
763 | { |
---|
764 | GB_TEST_READ(gbd, GB_BYTE, "GB_read_byte"); |
---|
765 | return gbd->as_entry()->info.i; |
---|
766 | } |
---|
767 | |
---|
768 | GBDATA *GB_read_pointer(GBDATA *gbd) { |
---|
769 | GB_TEST_READ(gbd, GB_POINTER, "GB_read_pointer"); |
---|
770 | return gbd->as_entry()->info.ptr; |
---|
771 | } |
---|
772 | |
---|
773 | double GB_read_float(GBDATA *gbd) // @@@ change return type to float (that's what's stored in DB) |
---|
774 | { |
---|
775 | XDR xdrs; |
---|
776 | static float f; // @@@ why static? |
---|
777 | |
---|
778 | GB_TEST_READ(gbd, GB_FLOAT, "GB_read_float"); |
---|
779 | xdrmem_create(&xdrs, &gbd->as_entry()->info.in.data[0], SIZOFINTERN, XDR_DECODE); |
---|
780 | xdr_float(&xdrs, &f); |
---|
781 | xdr_destroy(&xdrs); |
---|
782 | |
---|
783 | gb_assert(f == f); // !nan |
---|
784 | |
---|
785 | return (double)f; |
---|
786 | } |
---|
787 | |
---|
788 | long GB_read_count(GBDATA *gbd) { |
---|
789 | return gbd->as_entry()->size(); |
---|
790 | } |
---|
791 | |
---|
792 | long GB_read_memuse(GBDATA *gbd) { |
---|
793 | return gbd->as_entry()->memsize(); |
---|
794 | } |
---|
795 | |
---|
796 | #if defined(DEBUG) |
---|
797 | |
---|
798 | #define MIN_CBLISTNODE_SIZE 48 // minimum (found) callbacklist-elementsize |
---|
799 | |
---|
800 | #if defined(DARWIN) |
---|
801 | |
---|
802 | #define CBLISTNODE_SIZE MIN_CBLISTNODE_SIZE // assume known minimum (doesnt really matter; only used in db-browser) |
---|
803 | |
---|
804 | #else // linux: |
---|
805 | |
---|
806 | typedef std::_List_node<gb_callback_list::cbtype> CBLISTNODE_TYPE; |
---|
807 | const size_t CBLISTNODE_SIZE = sizeof(CBLISTNODE_TYPE); |
---|
808 | |
---|
809 | #if defined(ARB_64) |
---|
810 | // ignore smaller 32-bit implementations |
---|
811 | STATIC_ASSERT_ANNOTATED(MIN_CBLISTNODE_SIZE<=CBLISTNODE_SIZE, "MIN_CBLISTNODE_SIZE too big (smaller implementation detected)"); |
---|
812 | #endif |
---|
813 | |
---|
814 | #endif |
---|
815 | |
---|
816 | inline long calc_size(gb_callback_list *gbcbl) { |
---|
817 | return gbcbl |
---|
818 | ? sizeof(*gbcbl) + gbcbl->callbacks.size()* CBLISTNODE_SIZE |
---|
819 | : 0; |
---|
820 | } |
---|
821 | inline long calc_size(gb_transaction_save *gbts) { |
---|
822 | return gbts |
---|
823 | ? sizeof(*gbts) |
---|
824 | : 0; |
---|
825 | } |
---|
826 | inline long calc_size(gb_if_entries *gbie) { |
---|
827 | return gbie |
---|
828 | ? sizeof(*gbie) + calc_size(GB_IF_ENTRIES_NEXT(gbie)) |
---|
829 | : 0; |
---|
830 | } |
---|
831 | inline long calc_size(GB_REL_IFES *gbri, int table_size) { |
---|
832 | long size = 0; |
---|
833 | |
---|
834 | gb_if_entries *ifes; |
---|
835 | for (int idx = 0; idx<table_size; ++idx) { |
---|
836 | for (ifes = GB_ENTRIES_ENTRY(gbri, idx); |
---|
837 | ifes; |
---|
838 | ifes = GB_IF_ENTRIES_NEXT(ifes)) |
---|
839 | { |
---|
840 | size += calc_size(ifes); |
---|
841 | } |
---|
842 | } |
---|
843 | return size; |
---|
844 | } |
---|
845 | inline long calc_size(gb_index_files *gbif) { |
---|
846 | return gbif |
---|
847 | ? sizeof(*gbif) + calc_size(GB_INDEX_FILES_NEXT(gbif)) + calc_size(GB_INDEX_FILES_ENTRIES(gbif), gbif->hash_table_size) |
---|
848 | : 0; |
---|
849 | } |
---|
850 | inline long calc_size(gb_db_extended *gbe) { |
---|
851 | return gbe |
---|
852 | ? sizeof(*gbe) + calc_size(gbe->callback) + calc_size(gbe->old) |
---|
853 | : 0; |
---|
854 | } |
---|
855 | inline long calc_size(GBENTRY *gbe) { |
---|
856 | return gbe |
---|
857 | ? sizeof(*gbe) + calc_size(gbe->ext) |
---|
858 | : 0; |
---|
859 | } |
---|
860 | inline long calc_size(GBCONTAINER *gbc) { |
---|
861 | return gbc |
---|
862 | ? sizeof(*gbc) + calc_size(gbc->ext) + calc_size(GBCONTAINER_IFS(gbc)) |
---|
863 | : 0; |
---|
864 | } |
---|
865 | |
---|
866 | long GB_calc_structure_size(GBDATA *gbd) { |
---|
867 | long size = 0; |
---|
868 | if (gbd->is_container()) { |
---|
869 | size = calc_size(gbd->as_container()); |
---|
870 | } |
---|
871 | else { |
---|
872 | size = calc_size(gbd->as_entry()); |
---|
873 | } |
---|
874 | return size; |
---|
875 | } |
---|
876 | |
---|
877 | void GB_SizeInfo::collect(GBDATA *gbd) { |
---|
878 | if (gbd->is_container()) { |
---|
879 | ++containers; |
---|
880 | for (GBDATA *gb_child = GB_child(gbd); gb_child; gb_child = GB_nextChild(gb_child)) { |
---|
881 | collect(gb_child); |
---|
882 | } |
---|
883 | } |
---|
884 | else { |
---|
885 | ++terminals; |
---|
886 | mem += GB_read_memuse(gbd); |
---|
887 | |
---|
888 | long size; |
---|
889 | switch (gbd->type()) { |
---|
890 | case GB_INT: size = sizeof(int); break; |
---|
891 | case GB_FLOAT: size = sizeof(float); break; |
---|
892 | case GB_BYTE: size = sizeof(char); break; |
---|
893 | case GB_POINTER: size = sizeof(GBDATA*); break; |
---|
894 | case GB_STRING: size = GB_read_count(gbd); break; // accept 0 sized data for strings |
---|
895 | |
---|
896 | default: |
---|
897 | size = GB_read_count(gbd); |
---|
898 | gb_assert(size>0); // terminal w/o data - really? |
---|
899 | break; |
---|
900 | } |
---|
901 | data += size; |
---|
902 | } |
---|
903 | structure += GB_calc_structure_size(gbd); |
---|
904 | } |
---|
905 | #endif |
---|
906 | |
---|
907 | GB_CSTR GB_read_pntr(GBDATA *gbd) { |
---|
908 | GBENTRY *gbe = gbd->as_entry(); |
---|
909 | const char *data = gbe->data(); |
---|
910 | |
---|
911 | if (data) { |
---|
912 | if (gbe->flags.compressed_data) { // uncompressed data return pntr to database entry |
---|
913 | char *ca = gb_read_cache(gbe); |
---|
914 | |
---|
915 | if (!ca) { |
---|
916 | size_t size = gbe->uncompressed_size(); |
---|
917 | const char *da = gb_uncompress_data(gbe, data, size); |
---|
918 | |
---|
919 | if (da) { |
---|
920 | ca = gb_alloc_cache_index(gbe, size); |
---|
921 | memcpy(ca, da, size); |
---|
922 | } |
---|
923 | } |
---|
924 | data = ca; |
---|
925 | } |
---|
926 | } |
---|
927 | return data; |
---|
928 | } |
---|
929 | |
---|
930 | int gb_read_nr(GBDATA *gbd) { |
---|
931 | return gbd->index; |
---|
932 | } |
---|
933 | |
---|
934 | GB_CSTR GB_read_char_pntr(GBDATA *gbd) { |
---|
935 | GB_TEST_READ(gbd, GB_STRING, "GB_read_char_pntr"); |
---|
936 | return GB_read_pntr(gbd); |
---|
937 | } |
---|
938 | |
---|
939 | char *GB_read_string(GBDATA *gbd) { |
---|
940 | GB_TEST_READ(gbd, GB_STRING, "GB_read_string"); |
---|
941 | const char *d = GB_read_pntr(gbd); |
---|
942 | if (!d) return NULL; |
---|
943 | return GB_memdup(d, gbd->as_entry()->size()+1); |
---|
944 | } |
---|
945 | |
---|
946 | size_t GB_read_string_count(GBDATA *gbd) { |
---|
947 | GB_TEST_READ(gbd, GB_STRING, "GB_read_string_count"); |
---|
948 | return gbd->as_entry()->size(); |
---|
949 | } |
---|
950 | |
---|
951 | GB_CSTR GB_read_link_pntr(GBDATA *gbd) { |
---|
952 | GB_TEST_READ(gbd, GB_LINK, "GB_read_link_pntr"); |
---|
953 | return GB_read_pntr(gbd); |
---|
954 | } |
---|
955 | |
---|
956 | static char *GB_read_link(GBDATA *gbd) { |
---|
957 | const char *d; |
---|
958 | GB_TEST_READ(gbd, GB_LINK, "GB_read_link_pntr"); |
---|
959 | d = GB_read_pntr(gbd); |
---|
960 | if (!d) return NULL; |
---|
961 | return GB_memdup(d, gbd->as_entry()->size()+1); |
---|
962 | } |
---|
963 | |
---|
964 | long GB_read_bits_count(GBDATA *gbd) { |
---|
965 | GB_TEST_READ(gbd, GB_BITS, "GB_read_bits_count"); |
---|
966 | return gbd->as_entry()->size(); |
---|
967 | } |
---|
968 | |
---|
969 | GB_CSTR GB_read_bits_pntr(GBDATA *gbd, char c_0, char c_1) { |
---|
970 | GB_TEST_READ(gbd, GB_BITS, "GB_read_bits_pntr"); |
---|
971 | GBENTRY *gbe = gbd->as_entry(); |
---|
972 | long size = gbe->size(); |
---|
973 | if (size) { |
---|
974 | char *ca = gb_read_cache(gbe); |
---|
975 | if (ca) return ca; |
---|
976 | |
---|
977 | ca = gb_alloc_cache_index(gbe, size+1); |
---|
978 | const char *data = gbe->data(); |
---|
979 | char *da = gb_uncompress_bits(data, size, c_0, c_1); |
---|
980 | if (ca) { |
---|
981 | memcpy(ca, da, size+1); |
---|
982 | return ca; |
---|
983 | } |
---|
984 | return da; |
---|
985 | } |
---|
986 | return 0; |
---|
987 | } |
---|
988 | |
---|
989 | char *GB_read_bits(GBDATA *gbd, char c_0, char c_1) { |
---|
990 | GB_CSTR d = GB_read_bits_pntr(gbd, c_0, c_1); |
---|
991 | return d ? GB_memdup(d, gbd->as_entry()->size()+1) : 0; |
---|
992 | } |
---|
993 | |
---|
994 | |
---|
995 | GB_CSTR GB_read_bytes_pntr(GBDATA *gbd) |
---|
996 | { |
---|
997 | GB_TEST_READ(gbd, GB_BYTES, "GB_read_bytes_pntr"); |
---|
998 | return GB_read_pntr(gbd); |
---|
999 | } |
---|
1000 | |
---|
1001 | long GB_read_bytes_count(GBDATA *gbd) |
---|
1002 | { |
---|
1003 | GB_TEST_READ(gbd, GB_BYTES, "GB_read_bytes_count"); |
---|
1004 | return gbd->as_entry()->size(); |
---|
1005 | } |
---|
1006 | |
---|
1007 | char *GB_read_bytes(GBDATA *gbd) { |
---|
1008 | GB_CSTR d = GB_read_bytes_pntr(gbd); |
---|
1009 | return d ? GB_memdup(d, gbd->as_entry()->size()) : 0; |
---|
1010 | } |
---|
1011 | |
---|
1012 | GB_CUINT4 *GB_read_ints_pntr(GBDATA *gbd) |
---|
1013 | { |
---|
1014 | GB_TEST_READ(gbd, GB_INTS, "GB_read_ints_pntr"); |
---|
1015 | GBENTRY *gbe = gbd->as_entry(); |
---|
1016 | |
---|
1017 | GB_UINT4 *res; |
---|
1018 | if (gbe->flags.compressed_data) { |
---|
1019 | res = (GB_UINT4 *)GB_read_pntr(gbe); |
---|
1020 | } |
---|
1021 | else { |
---|
1022 | res = (GB_UINT4 *)gbe->data(); |
---|
1023 | } |
---|
1024 | if (!res) return NULL; |
---|
1025 | |
---|
1026 | if (0x01020304U == htonl(0x01020304U)) { |
---|
1027 | return res; |
---|
1028 | } |
---|
1029 | else { |
---|
1030 | int size = gbe->size(); |
---|
1031 | char *buf2 = GB_give_other_buffer((char *)res, size<<2); |
---|
1032 | GB_UINT4 *s = (GB_UINT4 *)res; |
---|
1033 | GB_UINT4 *d = (GB_UINT4 *)buf2; |
---|
1034 | |
---|
1035 | for (long i=size; i; i--) { |
---|
1036 | *(d++) = htonl(*(s++)); |
---|
1037 | } |
---|
1038 | return (GB_UINT4 *)buf2; |
---|
1039 | } |
---|
1040 | } |
---|
1041 | |
---|
1042 | long GB_read_ints_count(GBDATA *gbd) { // used by ../PERL_SCRIPTS/SAI/SAI.pm@read_ints_count |
---|
1043 | GB_TEST_READ(gbd, GB_INTS, "GB_read_ints_count"); |
---|
1044 | return gbd->as_entry()->size(); |
---|
1045 | } |
---|
1046 | |
---|
1047 | GB_UINT4 *GB_read_ints(GBDATA *gbd) |
---|
1048 | { |
---|
1049 | GB_CUINT4 *i = GB_read_ints_pntr(gbd); |
---|
1050 | if (!i) return NULL; |
---|
1051 | return (GB_UINT4 *)GB_memdup((char *)i, gbd->as_entry()->size()*sizeof(GB_UINT4)); |
---|
1052 | } |
---|
1053 | |
---|
1054 | GB_CFLOAT *GB_read_floats_pntr(GBDATA *gbd) |
---|
1055 | { |
---|
1056 | GB_TEST_READ(gbd, GB_FLOATS, "GB_read_floats_pntr"); |
---|
1057 | GBENTRY *gbe = gbd->as_entry(); |
---|
1058 | char *res; |
---|
1059 | if (gbe->flags.compressed_data) { |
---|
1060 | res = (char *)GB_read_pntr(gbe); |
---|
1061 | } |
---|
1062 | else { |
---|
1063 | res = (char *)gbe->data(); |
---|
1064 | } |
---|
1065 | if (res) { |
---|
1066 | long size = gbe->size(); |
---|
1067 | long full_size = size*sizeof(float); |
---|
1068 | |
---|
1069 | XDR xdrs; |
---|
1070 | xdrmem_create(&xdrs, res, (int)(full_size), XDR_DECODE); |
---|
1071 | |
---|
1072 | char *buf2 = GB_give_other_buffer(res, full_size); |
---|
1073 | float *d = (float *)(void*)buf2; |
---|
1074 | for (long i=size; i; i--) { |
---|
1075 | xdr_float(&xdrs, d); |
---|
1076 | d++; |
---|
1077 | } |
---|
1078 | xdr_destroy(&xdrs); |
---|
1079 | return (float *)(void*)buf2; |
---|
1080 | } |
---|
1081 | return NULL; |
---|
1082 | } |
---|
1083 | |
---|
1084 | static long GB_read_floats_count(GBDATA *gbd) |
---|
1085 | { |
---|
1086 | GB_TEST_READ(gbd, GB_FLOATS, "GB_read_floats_count"); |
---|
1087 | return gbd->as_entry()->size(); |
---|
1088 | } |
---|
1089 | |
---|
1090 | float *GB_read_floats(GBDATA *gbd) { // @@@ only used in unittest - check usage of floats |
---|
1091 | GB_CFLOAT *f; |
---|
1092 | f = GB_read_floats_pntr(gbd); |
---|
1093 | if (!f) return NULL; |
---|
1094 | return (float *)GB_memdup((char *)f, gbd->as_entry()->size()*sizeof(float)); |
---|
1095 | } |
---|
1096 | |
---|
1097 | char *GB_read_as_string(GBDATA *gbd) { |
---|
1098 | switch (gbd->type()) { |
---|
1099 | case GB_STRING: return GB_read_string(gbd); |
---|
1100 | case GB_LINK: return GB_read_link(gbd); |
---|
1101 | case GB_BYTE: return GBS_global_string_copy("%i", GB_read_byte(gbd)); |
---|
1102 | case GB_INT: return GBS_global_string_copy("%li", GB_read_int(gbd)); |
---|
1103 | case GB_FLOAT: return GBS_global_string_copy("%g", GB_read_float(gbd)); |
---|
1104 | case GB_BITS: return GB_read_bits(gbd, '0', '1'); |
---|
1105 | /* Be careful : When adding new types here, you have to make sure that |
---|
1106 | * GB_write_as_string is able to write them back and that this makes sense. |
---|
1107 | */ |
---|
1108 | default: return NULL; |
---|
1109 | } |
---|
1110 | } |
---|
1111 | |
---|
1112 | // ------------------------------------------------------------ |
---|
1113 | // array type access functions (intended for perl use) |
---|
1114 | |
---|
1115 | long GB_read_from_ints(GBDATA *gbd, long index) { // used by ../PERL_SCRIPTS/SAI/SAI.pm@read_from_ints |
---|
1116 | static GBDATA *last_gbd = 0; |
---|
1117 | static long count = 0; |
---|
1118 | static GB_CUINT4 *i = 0; |
---|
1119 | |
---|
1120 | if (gbd != last_gbd) { |
---|
1121 | count = GB_read_ints_count(gbd); |
---|
1122 | i = GB_read_ints_pntr(gbd); |
---|
1123 | last_gbd = gbd; |
---|
1124 | } |
---|
1125 | |
---|
1126 | if (index >= 0 && index < count) { |
---|
1127 | return i[index]; |
---|
1128 | } |
---|
1129 | return -1; |
---|
1130 | } |
---|
1131 | |
---|
1132 | double GB_read_from_floats(GBDATA *gbd, long index) { // @@@ unused |
---|
1133 | static GBDATA *last_gbd = 0; |
---|
1134 | static long count = 0; |
---|
1135 | static GB_CFLOAT *f = 0; |
---|
1136 | |
---|
1137 | if (gbd != last_gbd) { |
---|
1138 | count = GB_read_floats_count(gbd); |
---|
1139 | f = GB_read_floats_pntr(gbd); |
---|
1140 | last_gbd = gbd; |
---|
1141 | } |
---|
1142 | |
---|
1143 | if (index >= 0 && index < count) { |
---|
1144 | return f[index]; |
---|
1145 | } |
---|
1146 | return -1; |
---|
1147 | } |
---|
1148 | |
---|
1149 | // ------------------- |
---|
1150 | // write data |
---|
1151 | |
---|
1152 | static void gb_remove_callbacks_marked_for_deletion(GBDATA *gbd); |
---|
1153 | |
---|
1154 | static void gb_do_callbacks(GBDATA *gbd) { |
---|
1155 | gb_assert(GB_MAIN(gbd)->get_transaction_level() < 0); // only use in NO_TRANSACTION_MODE! |
---|
1156 | |
---|
1157 | while (gbd) { |
---|
1158 | GBDATA *gbdn = GB_get_father(gbd); |
---|
1159 | gb_callback_list *cbl = gbd->get_callbacks(); |
---|
1160 | if (cbl && cbl->call(gbd, GB_CB_CHANGED)) { |
---|
1161 | gb_remove_callbacks_marked_for_deletion(gbd); |
---|
1162 | } |
---|
1163 | gbd = gbdn; |
---|
1164 | } |
---|
1165 | } |
---|
1166 | |
---|
1167 | #define GB_DO_CALLBACKS(gbd) do { if (GB_MAIN(gbd)->get_transaction_level() < 0) gb_do_callbacks(gbd); } while (0) |
---|
1168 | |
---|
1169 | GB_ERROR GB_write_byte(GBDATA *gbd, int i) |
---|
1170 | { |
---|
1171 | GB_TEST_WRITE(gbd, GB_BYTE, "GB_write_byte"); |
---|
1172 | GBENTRY *gbe = gbd->as_entry(); |
---|
1173 | if (gbe->info.i != i) { |
---|
1174 | gb_save_extern_data_in_ts(gbe); |
---|
1175 | gbe->info.i = i & 0xff; |
---|
1176 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
---|
1177 | GB_DO_CALLBACKS(gbe); |
---|
1178 | } |
---|
1179 | return 0; |
---|
1180 | } |
---|
1181 | |
---|
1182 | GB_ERROR GB_write_int(GBDATA *gbd, long i) { |
---|
1183 | #if defined(ARB_64) |
---|
1184 | #if defined(WARN_TODO) |
---|
1185 | #warning GB_write_int should be GB_ERROR GB_write_int(GBDATA *gbd,int32_t i) |
---|
1186 | #endif |
---|
1187 | #endif |
---|
1188 | |
---|
1189 | GB_TEST_WRITE(gbd, GB_INT, "GB_write_int"); |
---|
1190 | if ((long)((int32_t)i) != i) { |
---|
1191 | gb_assert(0); |
---|
1192 | GB_warningf("Warning: 64bit incompatibility detected\nNo data written to '%s'\n", GB_get_db_path(gbd)); |
---|
1193 | return "GB_INT out of range (signed, 32bit)"; |
---|
1194 | } |
---|
1195 | GBENTRY *gbe = gbd->as_entry(); |
---|
1196 | if (gbe->info.i != (int32_t)i) { |
---|
1197 | gb_save_extern_data_in_ts(gbe); |
---|
1198 | gbe->info.i = i; |
---|
1199 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
---|
1200 | GB_DO_CALLBACKS(gbe); |
---|
1201 | } |
---|
1202 | return 0; |
---|
1203 | } |
---|
1204 | |
---|
1205 | GB_ERROR GB_write_pointer(GBDATA *gbd, GBDATA *pointer) { |
---|
1206 | GB_TEST_WRITE(gbd, GB_POINTER, "GB_write_pointer"); |
---|
1207 | GBENTRY *gbe = gbd->as_entry(); |
---|
1208 | if (gbe->info.ptr != pointer) { |
---|
1209 | gb_save_extern_data_in_ts(gbe); |
---|
1210 | gbe->info.ptr = pointer; |
---|
1211 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
---|
1212 | GB_DO_CALLBACKS(gbe); |
---|
1213 | } |
---|
1214 | return 0; |
---|
1215 | } |
---|
1216 | |
---|
1217 | GB_ERROR GB_write_float(GBDATA *gbd, double f) |
---|
1218 | { |
---|
1219 | gb_assert(f == f); // !nan |
---|
1220 | |
---|
1221 | XDR xdrs; |
---|
1222 | static float f2; |
---|
1223 | |
---|
1224 | GB_TEST_WRITE(gbd, GB_FLOAT, "GB_write_float"); |
---|
1225 | |
---|
1226 | #if defined(WARN_TODO) |
---|
1227 | #warning call GB_read_float here! |
---|
1228 | #endif |
---|
1229 | GB_TEST_READ(gbd, GB_FLOAT, "GB_read_float"); |
---|
1230 | |
---|
1231 | GBENTRY *gbe = gbd->as_entry(); |
---|
1232 | xdrmem_create(&xdrs, &gbe->info.in.data[0], SIZOFINTERN, XDR_DECODE); |
---|
1233 | xdr_float(&xdrs, &f2); |
---|
1234 | xdr_destroy(&xdrs); |
---|
1235 | |
---|
1236 | if (f2 != f) { |
---|
1237 | f2 = f; |
---|
1238 | gb_save_extern_data_in_ts(gbe); |
---|
1239 | xdrmem_create(&xdrs, &gbe->info.in.data[0], SIZOFINTERN, XDR_ENCODE); |
---|
1240 | xdr_float(&xdrs, &f2); |
---|
1241 | xdr_destroy(&xdrs); |
---|
1242 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
---|
1243 | GB_DO_CALLBACKS(gbe); |
---|
1244 | } |
---|
1245 | xdr_destroy(&xdrs); |
---|
1246 | return 0; |
---|
1247 | } |
---|
1248 | |
---|
1249 | GB_ERROR gb_write_compressed_pntr(GBENTRY *gbe, const char *s, long memsize, long stored_size) { |
---|
1250 | gb_uncache(gbe); |
---|
1251 | gb_save_extern_data_in_ts(gbe); |
---|
1252 | gbe->flags.compressed_data = 1; |
---|
1253 | gbe->insert_data((char *)s, stored_size, (size_t)memsize); |
---|
1254 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
---|
1255 | |
---|
1256 | return 0; |
---|
1257 | } |
---|
1258 | |
---|
1259 | int gb_get_compression_mask(GB_MAIN_TYPE *Main, GBQUARK key, int gb_type) { |
---|
1260 | gb_Key *ks = &Main->keys[key]; |
---|
1261 | int compression_mask; |
---|
1262 | |
---|
1263 | if (ks->gb_key_disabled) { |
---|
1264 | compression_mask = 0; |
---|
1265 | } |
---|
1266 | else { |
---|
1267 | if (!ks->gb_key) gb_load_single_key_data(Main->gb_main(), key); |
---|
1268 | compression_mask = gb_convert_type_2_compression_flags[gb_type] & ks->compression_mask; |
---|
1269 | } |
---|
1270 | |
---|
1271 | return compression_mask; |
---|
1272 | } |
---|
1273 | |
---|
1274 | GB_ERROR GB_write_pntr(GBDATA *gbd, const char *s, size_t bytes_size, size_t stored_size) |
---|
1275 | { |
---|
1276 | // 'bytes_size' is the size of what 's' points to. |
---|
1277 | // 'stored_size' is the size-information written into the DB |
---|
1278 | // |
---|
1279 | // e.g. for strings : stored_size = bytes_size-1, cause stored_size is string len, |
---|
1280 | // but bytes_size includes zero byte. |
---|
1281 | |
---|
1282 | GBENTRY *gbe = gbd->as_entry(); |
---|
1283 | GB_MAIN_TYPE *Main = GB_MAIN(gbe); |
---|
1284 | GBQUARK key = GB_KEY_QUARK(gbe); |
---|
1285 | GB_TYPES type = gbe->type(); |
---|
1286 | |
---|
1287 | gb_assert(implicated(type == GB_STRING, stored_size == bytes_size-1)); // size constraint for strings not fulfilled! |
---|
1288 | |
---|
1289 | gb_uncache(gbe); |
---|
1290 | gb_save_extern_data_in_ts(gbe); |
---|
1291 | |
---|
1292 | int compression_mask = gb_get_compression_mask(Main, key, type); |
---|
1293 | |
---|
1294 | const char *d; |
---|
1295 | size_t memsize; |
---|
1296 | if (compression_mask) { |
---|
1297 | d = gb_compress_data(gbe, key, s, bytes_size, &memsize, compression_mask, false); |
---|
1298 | } |
---|
1299 | else { |
---|
1300 | d = NULL; |
---|
1301 | } |
---|
1302 | if (d) { |
---|
1303 | gbe->flags.compressed_data = 1; |
---|
1304 | } |
---|
1305 | else { |
---|
1306 | d = s; |
---|
1307 | gbe->flags.compressed_data = 0; |
---|
1308 | memsize = bytes_size; |
---|
1309 | } |
---|
1310 | |
---|
1311 | gbe->insert_data(d, stored_size, memsize); |
---|
1312 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
---|
1313 | GB_DO_CALLBACKS(gbe); |
---|
1314 | |
---|
1315 | return 0; |
---|
1316 | } |
---|
1317 | |
---|
1318 | GB_ERROR GB_write_string(GBDATA *gbd, const char *s) { |
---|
1319 | GBENTRY *gbe = gbd->as_entry(); |
---|
1320 | GB_TEST_WRITE(gbe, GB_STRING, "GB_write_string"); |
---|
1321 | GB_TEST_NON_BUFFER(s, "GB_write_string"); // compress would destroy the other buffer |
---|
1322 | |
---|
1323 | if (!s) s = ""; |
---|
1324 | size_t size = strlen(s); |
---|
1325 | |
---|
1326 | // no zero len strings allowed |
---|
1327 | if (gbe->memsize() && (size == gbe->size())) |
---|
1328 | { |
---|
1329 | if (!strcmp(s, GB_read_pntr(gbe))) |
---|
1330 | return 0; |
---|
1331 | } |
---|
1332 | #if defined(DEBUG) && 0 |
---|
1333 | // check for error (in compression) |
---|
1334 | { |
---|
1335 | GB_ERROR error = GB_write_pntr(gbe, s, size+1, size); |
---|
1336 | if (!error) { |
---|
1337 | char *check = GB_read_string(gbe); |
---|
1338 | |
---|
1339 | gb_assert(check); |
---|
1340 | gb_assert(strcmp(check, s) == 0); |
---|
1341 | |
---|
1342 | free(check); |
---|
1343 | } |
---|
1344 | return error; |
---|
1345 | } |
---|
1346 | #else |
---|
1347 | return GB_write_pntr(gbe, s, size+1, size); |
---|
1348 | #endif // DEBUG |
---|
1349 | } |
---|
1350 | |
---|
1351 | GB_ERROR GB_write_link(GBDATA *gbd, const char *s) |
---|
1352 | { |
---|
1353 | GBENTRY *gbe = gbd->as_entry(); |
---|
1354 | GB_TEST_WRITE(gbe, GB_STRING, "GB_write_link"); |
---|
1355 | GB_TEST_NON_BUFFER(s, "GB_write_link"); // compress would destroy the other buffer |
---|
1356 | |
---|
1357 | if (!s) s = ""; |
---|
1358 | size_t size = strlen(s); |
---|
1359 | |
---|
1360 | // no zero len strings allowed |
---|
1361 | if (gbe->memsize() && (size == gbe->size())) |
---|
1362 | { |
---|
1363 | if (!strcmp(s, GB_read_pntr(gbe))) |
---|
1364 | return 0; |
---|
1365 | } |
---|
1366 | return GB_write_pntr(gbe, s, size+1, size); |
---|
1367 | } |
---|
1368 | |
---|
1369 | |
---|
1370 | GB_ERROR GB_write_bits(GBDATA *gbd, const char *bits, long size, const char *c_0) |
---|
1371 | { |
---|
1372 | GBENTRY *gbe = gbd->as_entry(); |
---|
1373 | GB_TEST_WRITE(gbe, GB_BITS, "GB_write_bits"); |
---|
1374 | GB_TEST_NON_BUFFER(bits, "GB_write_bits"); // compress would destroy the other buffer |
---|
1375 | gb_save_extern_data_in_ts(gbe); |
---|
1376 | |
---|
1377 | long memsize; |
---|
1378 | char *d = gb_compress_bits(bits, size, (const unsigned char *)c_0, &memsize); |
---|
1379 | |
---|
1380 | gbe->flags.compressed_data = 1; |
---|
1381 | gbe->insert_data(d, size, memsize); |
---|
1382 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
---|
1383 | GB_DO_CALLBACKS(gbe); |
---|
1384 | return 0; |
---|
1385 | } |
---|
1386 | |
---|
1387 | GB_ERROR GB_write_bytes(GBDATA *gbd, const char *s, long size) |
---|
1388 | { |
---|
1389 | GB_TEST_WRITE(gbd, GB_BYTES, "GB_write_bytes"); |
---|
1390 | return GB_write_pntr(gbd, s, size, size); |
---|
1391 | } |
---|
1392 | |
---|
1393 | GB_ERROR GB_write_ints(GBDATA *gbd, const GB_UINT4 *i, long size) |
---|
1394 | { |
---|
1395 | |
---|
1396 | GB_TEST_WRITE(gbd, GB_INTS, "GB_write_ints"); |
---|
1397 | GB_TEST_NON_BUFFER((char *)i, "GB_write_ints"); // compress would destroy the other buffer |
---|
1398 | |
---|
1399 | if (0x01020304 != htonl((GB_UINT4)0x01020304)) { |
---|
1400 | long j; |
---|
1401 | char *buf2 = GB_give_other_buffer((char *)i, size<<2); |
---|
1402 | GB_UINT4 *s = (GB_UINT4 *)i; |
---|
1403 | GB_UINT4 *d = (GB_UINT4 *)buf2; |
---|
1404 | |
---|
1405 | for (j=size; j; j--) { |
---|
1406 | *(d++) = htonl(*(s++)); |
---|
1407 | } |
---|
1408 | i = (GB_UINT4 *)buf2; |
---|
1409 | } |
---|
1410 | return GB_write_pntr(gbd, (char *)i, size* 4 /* sizeof(long4) */, size); |
---|
1411 | } |
---|
1412 | |
---|
1413 | GB_ERROR GB_write_floats(GBDATA *gbd, const float *f, long size) |
---|
1414 | { |
---|
1415 | long fullsize = size * sizeof(float); |
---|
1416 | GB_TEST_WRITE(gbd, GB_FLOATS, "GB_write_floats"); |
---|
1417 | GB_TEST_NON_BUFFER((char *)f, "GB_write_floats"); // compress would destroy the other buffer |
---|
1418 | |
---|
1419 | { |
---|
1420 | XDR xdrs; |
---|
1421 | long i; |
---|
1422 | char *buf2 = GB_give_other_buffer((char *)f, fullsize); |
---|
1423 | float *s = (float *)f; |
---|
1424 | |
---|
1425 | xdrmem_create(&xdrs, buf2, (int)fullsize, XDR_ENCODE); |
---|
1426 | for (i=size; i; i--) { |
---|
1427 | xdr_float(&xdrs, s); |
---|
1428 | s++; |
---|
1429 | } |
---|
1430 | xdr_destroy (&xdrs); |
---|
1431 | f = (float*)(void*)buf2; |
---|
1432 | } |
---|
1433 | return GB_write_pntr(gbd, (char *)f, size*sizeof(float), size); |
---|
1434 | } |
---|
1435 | |
---|
1436 | GB_ERROR GB_write_as_string(GBDATA *gbd, const char *val) { |
---|
1437 | switch (gbd->type()) { |
---|
1438 | case GB_STRING: return GB_write_string(gbd, val); |
---|
1439 | case GB_LINK: return GB_write_link(gbd, val); |
---|
1440 | case GB_BYTE: return GB_write_byte(gbd, atoi(val)); |
---|
1441 | case GB_INT: return GB_write_int(gbd, atoi(val)); |
---|
1442 | case GB_FLOAT: return GB_write_float(gbd, GB_atof(val)); |
---|
1443 | case GB_BITS: return GB_write_bits(gbd, val, strlen(val), "0"); |
---|
1444 | default: return GB_export_errorf("Error: You cannot use GB_write_as_string on this type of entry (%s)", GB_read_key_pntr(gbd)); |
---|
1445 | } |
---|
1446 | } |
---|
1447 | |
---|
1448 | // --------------------------- |
---|
1449 | // security functions |
---|
1450 | |
---|
1451 | int GB_read_security_write(GBDATA *gbd) { |
---|
1452 | GB_test_transaction(gbd); |
---|
1453 | return GB_GET_SECURITY_WRITE(gbd); |
---|
1454 | } |
---|
1455 | int GB_read_security_read(GBDATA *gbd) { |
---|
1456 | GB_test_transaction(gbd); |
---|
1457 | return GB_GET_SECURITY_READ(gbd); |
---|
1458 | } |
---|
1459 | int GB_read_security_delete(GBDATA *gbd) { |
---|
1460 | GB_test_transaction(gbd); |
---|
1461 | return GB_GET_SECURITY_DELETE(gbd); |
---|
1462 | } |
---|
1463 | GB_ERROR GB_write_security_write(GBDATA *gbd, unsigned long level) |
---|
1464 | { |
---|
1465 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
1466 | GB_test_transaction(Main); |
---|
1467 | |
---|
1468 | if (GB_GET_SECURITY_WRITE(gbd)>Main->security_level) |
---|
1469 | return gb_security_error(gbd); |
---|
1470 | if (GB_GET_SECURITY_WRITE(gbd) == level) return 0; |
---|
1471 | GB_PUT_SECURITY_WRITE(gbd, level); |
---|
1472 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
---|
1473 | GB_DO_CALLBACKS(gbd); |
---|
1474 | return 0; |
---|
1475 | } |
---|
1476 | GB_ERROR GB_write_security_read(GBDATA *gbd, unsigned long level) // @@@ unused |
---|
1477 | { |
---|
1478 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
1479 | GB_test_transaction(Main); |
---|
1480 | if (GB_GET_SECURITY_WRITE(gbd)>Main->security_level) |
---|
1481 | return gb_security_error(gbd); |
---|
1482 | if (GB_GET_SECURITY_READ(gbd) == level) return 0; |
---|
1483 | GB_PUT_SECURITY_READ(gbd, level); |
---|
1484 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
---|
1485 | GB_DO_CALLBACKS(gbd); |
---|
1486 | return 0; |
---|
1487 | } |
---|
1488 | GB_ERROR GB_write_security_delete(GBDATA *gbd, unsigned long level) |
---|
1489 | { |
---|
1490 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
1491 | GB_test_transaction(Main); |
---|
1492 | if (GB_GET_SECURITY_WRITE(gbd)>Main->security_level) |
---|
1493 | return gb_security_error(gbd); |
---|
1494 | if (GB_GET_SECURITY_DELETE(gbd) == level) return 0; |
---|
1495 | GB_PUT_SECURITY_DELETE(gbd, level); |
---|
1496 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
---|
1497 | GB_DO_CALLBACKS(gbd); |
---|
1498 | return 0; |
---|
1499 | } |
---|
1500 | GB_ERROR GB_write_security_levels(GBDATA *gbd, unsigned long readlevel, unsigned long writelevel, unsigned long deletelevel) |
---|
1501 | { |
---|
1502 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
1503 | GB_test_transaction(Main); |
---|
1504 | if (GB_GET_SECURITY_WRITE(gbd)>Main->security_level) |
---|
1505 | return gb_security_error(gbd); |
---|
1506 | GB_PUT_SECURITY_WRITE(gbd, writelevel); |
---|
1507 | GB_PUT_SECURITY_READ(gbd, readlevel); |
---|
1508 | GB_PUT_SECURITY_DELETE(gbd, deletelevel); |
---|
1509 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
---|
1510 | GB_DO_CALLBACKS(gbd); |
---|
1511 | return 0; |
---|
1512 | } |
---|
1513 | |
---|
1514 | void GB_change_my_security(GBDATA *gbd, int level) { |
---|
1515 | GB_MAIN(gbd)->security_level = level<0 ? 0 : (level>7 ? 7 : level); |
---|
1516 | } |
---|
1517 | |
---|
1518 | // For internal use only |
---|
1519 | void GB_push_my_security(GBDATA *gbd) |
---|
1520 | { |
---|
1521 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
1522 | Main->pushed_security_level++; |
---|
1523 | if (Main->pushed_security_level <= 1) { |
---|
1524 | Main->old_security_level = Main->security_level; |
---|
1525 | Main->security_level = 7; |
---|
1526 | } |
---|
1527 | } |
---|
1528 | |
---|
1529 | void GB_pop_my_security(GBDATA *gbd) { |
---|
1530 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
1531 | Main->pushed_security_level--; |
---|
1532 | if (Main->pushed_security_level <= 0) { |
---|
1533 | Main->security_level = Main->old_security_level; |
---|
1534 | } |
---|
1535 | } |
---|
1536 | |
---|
1537 | |
---|
1538 | // ------------------------ |
---|
1539 | // Key information |
---|
1540 | |
---|
1541 | GB_TYPES GB_read_type(GBDATA *gbd) { |
---|
1542 | GB_test_transaction(gbd); |
---|
1543 | return gbd->type(); |
---|
1544 | } |
---|
1545 | |
---|
1546 | bool GB_is_container(GBDATA *gbd) { |
---|
1547 | return gbd && gbd->is_container(); |
---|
1548 | } |
---|
1549 | |
---|
1550 | char *GB_read_key(GBDATA *gbd) { |
---|
1551 | return strdup(GB_read_key_pntr(gbd)); |
---|
1552 | } |
---|
1553 | |
---|
1554 | GB_CSTR GB_read_key_pntr(GBDATA *gbd) { |
---|
1555 | GB_CSTR k; |
---|
1556 | GB_test_transaction(gbd); |
---|
1557 | k = GB_KEY(gbd); |
---|
1558 | if (!k) k = GBS_global_string("<invalid key (quark=%i)>", GB_KEY_QUARK(gbd)); |
---|
1559 | return k; |
---|
1560 | } |
---|
1561 | |
---|
1562 | GB_CSTR gb_read_key_pntr(GBDATA *gbd) { |
---|
1563 | return GB_KEY(gbd); |
---|
1564 | } |
---|
1565 | |
---|
1566 | |
---|
1567 | GBQUARK gb_find_existing_quark(GB_MAIN_TYPE *Main, const char *key) { |
---|
1568 | //! @return existing quark for 'key' (-1 if key == NULL, 0 if key is unknown) |
---|
1569 | return key ? GBS_read_hash(Main->key_2_index_hash, key) : -1; |
---|
1570 | } |
---|
1571 | |
---|
1572 | GBQUARK gb_find_or_create_quark(GB_MAIN_TYPE *Main, const char *key) { |
---|
1573 | //! @return existing or newly created quark for 'key' |
---|
1574 | GBQUARK quark = gb_find_existing_quark(Main, key); |
---|
1575 | if (!quark) quark = gb_create_key(Main, key, true); |
---|
1576 | return quark; |
---|
1577 | } |
---|
1578 | |
---|
1579 | GBQUARK gb_find_or_create_NULL_quark(GB_MAIN_TYPE *Main, const char *key) { |
---|
1580 | // similar to gb_find_or_create_quark, |
---|
1581 | // but if 'key' is NULL, quark 0 will be returned. |
---|
1582 | // |
---|
1583 | // Use this function with care. |
---|
1584 | // |
---|
1585 | // Known good use: |
---|
1586 | // - create main entry and its dummy father via gb_make_container() |
---|
1587 | |
---|
1588 | return key ? gb_find_or_create_quark(Main, key) : 0; |
---|
1589 | } |
---|
1590 | |
---|
1591 | GBQUARK GB_find_existing_quark(GBDATA *gbd, const char *key) { |
---|
1592 | //! @return existing quark for 'key' (-1 if key == NULL, 0 if key is unknown) |
---|
1593 | return gb_find_existing_quark(GB_MAIN(gbd), key); |
---|
1594 | } |
---|
1595 | |
---|
1596 | GBQUARK GB_find_or_create_quark(GBDATA *gbd, const char *key) { |
---|
1597 | //! @return existing or newly created quark for 'key' |
---|
1598 | return gb_find_or_create_quark(GB_MAIN(gbd), key); |
---|
1599 | } |
---|
1600 | |
---|
1601 | |
---|
1602 | // --------------------------------------------- |
---|
1603 | |
---|
1604 | GBQUARK GB_get_quark(GBDATA *gbd) { |
---|
1605 | return GB_KEY_QUARK(gbd); |
---|
1606 | } |
---|
1607 | |
---|
1608 | bool GB_has_key(GBDATA *gbd, const char *key) { |
---|
1609 | GBQUARK quark = GB_find_existing_quark(gbd, key); |
---|
1610 | return quark && (quark == GB_get_quark(gbd)); |
---|
1611 | } |
---|
1612 | |
---|
1613 | // --------------------------------------------- |
---|
1614 | |
---|
1615 | long GB_read_clock(GBDATA *gbd) { |
---|
1616 | if (GB_ARRAY_FLAGS(gbd).changed) return GB_MAIN(gbd)->clock; |
---|
1617 | return gbd->update_date(); |
---|
1618 | } |
---|
1619 | |
---|
1620 | // --------------------------------------------- |
---|
1621 | // Get and check the database hierarchy |
---|
1622 | |
---|
1623 | GBDATA *GB_get_father(GBDATA *gbd) { |
---|
1624 | // Get the father of an entry |
---|
1625 | GB_test_transaction(gbd); |
---|
1626 | return gbd->get_father(); |
---|
1627 | } |
---|
1628 | |
---|
1629 | GBDATA *GB_get_grandfather(GBDATA *gbd) { |
---|
1630 | GB_test_transaction(gbd); |
---|
1631 | |
---|
1632 | GBDATA *gb_grandpa = GB_FATHER(gbd); |
---|
1633 | if (gb_grandpa) { |
---|
1634 | gb_grandpa = GB_FATHER(gb_grandpa); |
---|
1635 | if (gb_grandpa && !GB_FATHER(gb_grandpa)) gb_grandpa = NULL; // never return dummy_father of root container |
---|
1636 | } |
---|
1637 | return gb_grandpa; |
---|
1638 | } |
---|
1639 | |
---|
1640 | // Get the root entry (gb_main) |
---|
1641 | GBDATA *GB_get_root(GBDATA *gbd) { return GB_MAIN(gbd)->gb_main(); } |
---|
1642 | GBCONTAINER *gb_get_root(GBENTRY *gbe) { return GB_MAIN(gbe)->root_container; } |
---|
1643 | GBCONTAINER *gb_get_root(GBCONTAINER *gbc) { return GB_MAIN(gbc)->root_container; } |
---|
1644 | |
---|
1645 | bool GB_check_father(GBDATA *gbd, GBDATA *gb_maybefather) { |
---|
1646 | // Test whether an entry is a subentry of another |
---|
1647 | GBDATA *gbfather; |
---|
1648 | for (gbfather = GB_get_father(gbd); |
---|
1649 | gbfather; |
---|
1650 | gbfather = GB_get_father(gbfather)) |
---|
1651 | { |
---|
1652 | if (gbfather == gb_maybefather) return true; |
---|
1653 | } |
---|
1654 | return false; |
---|
1655 | } |
---|
1656 | |
---|
1657 | // -------------------------- |
---|
1658 | // create and rename |
---|
1659 | |
---|
1660 | GBENTRY *gb_create(GBCONTAINER *father, const char *key, GB_TYPES type) { |
---|
1661 | GBENTRY *gbe = gb_make_entry(father, key, -1, 0, type); |
---|
1662 | gb_touch_header(GB_FATHER(gbe)); |
---|
1663 | gb_touch_entry(gbe, GB_CREATED); |
---|
1664 | |
---|
1665 | gb_assert(GB_ARRAY_FLAGS(gbe).changed < GB_DELETED); // happens sometimes -> needs debugging |
---|
1666 | |
---|
1667 | return gbe; |
---|
1668 | } |
---|
1669 | |
---|
1670 | GBCONTAINER *gb_create_container(GBCONTAINER *father, const char *key) { |
---|
1671 | // Create a container, do not check anything |
---|
1672 | GBCONTAINER *gbc = gb_make_container(father, key, -1, 0); |
---|
1673 | gb_touch_header(GB_FATHER(gbc)); |
---|
1674 | gb_touch_entry(gbc, GB_CREATED); |
---|
1675 | return gbc; |
---|
1676 | } |
---|
1677 | |
---|
1678 | GBDATA *GB_create(GBDATA *father, const char *key, GB_TYPES type) { |
---|
1679 | /*! Create a DB entry |
---|
1680 | * |
---|
1681 | * @param father container to create DB field in |
---|
1682 | * @param key name of field |
---|
1683 | * @param type field type |
---|
1684 | * |
---|
1685 | * @return |
---|
1686 | * - created DB entry |
---|
1687 | * - NULL on failure (error is exported then) |
---|
1688 | * |
---|
1689 | * @see GB_create_container() |
---|
1690 | */ |
---|
1691 | |
---|
1692 | if (GB_check_key(key)) { |
---|
1693 | GB_print_error(); |
---|
1694 | return NULL; |
---|
1695 | } |
---|
1696 | |
---|
1697 | if (type == GB_DB) { |
---|
1698 | gb_assert(type != GB_DB); // you like to use GB_create_container! |
---|
1699 | GB_export_error("GB_create error: can't create containers"); |
---|
1700 | return NULL; |
---|
1701 | } |
---|
1702 | |
---|
1703 | if (!father) { |
---|
1704 | GB_internal_errorf("GB_create error in GB_create:\nno father (key = '%s')", key); |
---|
1705 | return NULL; |
---|
1706 | } |
---|
1707 | GB_test_transaction(father); |
---|
1708 | if (father->is_entry()) { |
---|
1709 | GB_export_errorf("GB_create: father (%s) is not of GB_DB type (%i) (creating '%s')", |
---|
1710 | GB_read_key_pntr(father), father->type(), key); |
---|
1711 | return NULL; |
---|
1712 | } |
---|
1713 | |
---|
1714 | if (type == GB_POINTER) { |
---|
1715 | if (!GB_in_temporary_branch(father)) { |
---|
1716 | GB_export_error("GB_create: pointers only allowed in temporary branches"); |
---|
1717 | return NULL; |
---|
1718 | } |
---|
1719 | } |
---|
1720 | |
---|
1721 | return gb_create(father->expect_container(), key, type); |
---|
1722 | } |
---|
1723 | |
---|
1724 | GBDATA *GB_create_container(GBDATA *father, const char *key) { |
---|
1725 | /*! Create a new DB container |
---|
1726 | * |
---|
1727 | * @param father parent container |
---|
1728 | * @param key name of created container |
---|
1729 | * |
---|
1730 | * @return |
---|
1731 | * - created container |
---|
1732 | * - NULL on failure (error is exported then) |
---|
1733 | * |
---|
1734 | * @see GB_create() |
---|
1735 | */ |
---|
1736 | |
---|
1737 | if (GB_check_key(key)) { |
---|
1738 | GB_print_error(); |
---|
1739 | return NULL; |
---|
1740 | } |
---|
1741 | |
---|
1742 | if ((*key == '\0')) { |
---|
1743 | GB_export_error("GB_create error: empty key"); |
---|
1744 | return NULL; |
---|
1745 | } |
---|
1746 | if (!father) { |
---|
1747 | GB_internal_errorf("GB_create error in GB_create:\nno father (key = '%s')", key); |
---|
1748 | return NULL; |
---|
1749 | } |
---|
1750 | |
---|
1751 | GB_test_transaction(father); |
---|
1752 | return gb_create_container(father->expect_container(), key); |
---|
1753 | } |
---|
1754 | |
---|
1755 | // ---------------------- |
---|
1756 | // recompression |
---|
1757 | |
---|
1758 | #if defined(WARN_TODO) |
---|
1759 | #warning rename gb_set_compression into gb_recompress (misleading name) |
---|
1760 | #endif |
---|
1761 | |
---|
1762 | static GB_ERROR gb_set_compression(GBDATA *source) { |
---|
1763 | GB_ERROR error = 0; |
---|
1764 | GB_test_transaction(source); |
---|
1765 | |
---|
1766 | switch (source->type()) { |
---|
1767 | case GB_STRING: { |
---|
1768 | char *str = GB_read_string(source); |
---|
1769 | GB_write_string(source, ""); |
---|
1770 | GB_write_string(source, str); |
---|
1771 | free(str); |
---|
1772 | break; |
---|
1773 | } |
---|
1774 | case GB_BITS: |
---|
1775 | case GB_BYTES: |
---|
1776 | case GB_INTS: |
---|
1777 | case GB_FLOATS: |
---|
1778 | break; |
---|
1779 | case GB_DB: |
---|
1780 | for (GBDATA *gb_p = GB_child(source); gb_p && !error; gb_p = GB_nextChild(gb_p)) { |
---|
1781 | error = gb_set_compression(gb_p); |
---|
1782 | } |
---|
1783 | break; |
---|
1784 | default: |
---|
1785 | break; |
---|
1786 | } |
---|
1787 | return error; |
---|
1788 | } |
---|
1789 | |
---|
1790 | bool GB_allow_compression(GBDATA *gb_main, bool allow_compression) { |
---|
1791 | GB_MAIN_TYPE *Main = GB_MAIN(gb_main); |
---|
1792 | int prev_mask = Main->compression_mask; |
---|
1793 | Main->compression_mask = allow_compression ? -1 : 0; |
---|
1794 | |
---|
1795 | return prev_mask == 0 ? false : true; |
---|
1796 | } |
---|
1797 | |
---|
1798 | |
---|
1799 | GB_ERROR GB_delete(GBDATA*& source) { |
---|
1800 | GBDATA *gb_main; |
---|
1801 | |
---|
1802 | GB_test_transaction(source); |
---|
1803 | if (GB_GET_SECURITY_DELETE(source)>GB_MAIN(source)->security_level) { |
---|
1804 | return GBS_global_string("Security error: deleting entry '%s' not permitted", GB_read_key_pntr(source)); |
---|
1805 | } |
---|
1806 | |
---|
1807 | gb_main = GB_get_root(source); |
---|
1808 | |
---|
1809 | if (source->flags.compressed_data) { |
---|
1810 | bool was_allowed = GB_allow_compression(gb_main, false); |
---|
1811 | gb_set_compression(source); // write data w/o compression (otherwise GB_read_old_value... won't work) |
---|
1812 | GB_allow_compression(gb_main, was_allowed); |
---|
1813 | } |
---|
1814 | |
---|
1815 | { |
---|
1816 | GB_MAIN_TYPE *Main = GB_MAIN(source); |
---|
1817 | if (Main->get_transaction_level() < 0) { // no transaction mode |
---|
1818 | gb_delete_entry(source); |
---|
1819 | Main->call_pending_callbacks(); |
---|
1820 | } |
---|
1821 | else { |
---|
1822 | gb_touch_entry(source, GB_DELETED); |
---|
1823 | } |
---|
1824 | } |
---|
1825 | return 0; |
---|
1826 | } |
---|
1827 | |
---|
1828 | GB_ERROR gb_delete_force(GBDATA *source) // delete always |
---|
1829 | { |
---|
1830 | gb_touch_entry(source, GB_DELETED); |
---|
1831 | return 0; |
---|
1832 | } |
---|
1833 | |
---|
1834 | |
---|
1835 | // ------------------ |
---|
1836 | // Copy data |
---|
1837 | |
---|
1838 | #if defined(WARN_TODO) |
---|
1839 | #warning replace GB_copy with GB_copy_with_protection after release |
---|
1840 | #endif |
---|
1841 | |
---|
1842 | GB_ERROR GB_copy(GBDATA *dest, GBDATA *source) { |
---|
1843 | return GB_copy_with_protection(dest, source, false); |
---|
1844 | } |
---|
1845 | |
---|
1846 | GB_ERROR GB_copy_with_protection(GBDATA *dest, GBDATA *source, bool copy_all_protections) { |
---|
1847 | GB_ERROR error = 0; |
---|
1848 | GB_test_transaction(source); |
---|
1849 | |
---|
1850 | GB_TYPES type = source->type(); |
---|
1851 | if (dest->type() != type) { |
---|
1852 | return GB_export_errorf("incompatible types in GB_copy (source %s:%u != %s:%u", |
---|
1853 | GB_read_key_pntr(source), type, GB_read_key_pntr(dest), dest->type()); |
---|
1854 | } |
---|
1855 | |
---|
1856 | switch (type) { |
---|
1857 | case GB_INT: |
---|
1858 | error = GB_write_int(dest, GB_read_int(source)); |
---|
1859 | break; |
---|
1860 | case GB_FLOAT: |
---|
1861 | error = GB_write_float(dest, GB_read_float(source)); |
---|
1862 | break; |
---|
1863 | case GB_BYTE: |
---|
1864 | error = GB_write_byte(dest, GB_read_byte(source)); |
---|
1865 | break; |
---|
1866 | case GB_STRING: // No local compression |
---|
1867 | error = GB_write_string(dest, GB_read_char_pntr(source)); |
---|
1868 | break; |
---|
1869 | case GB_LINK: // No local compression |
---|
1870 | error = GB_write_link(dest, GB_read_link_pntr(source)); |
---|
1871 | break; |
---|
1872 | case GB_BITS: // only local compressions for the following types |
---|
1873 | case GB_BYTES: |
---|
1874 | case GB_INTS: |
---|
1875 | case GB_FLOATS: { |
---|
1876 | GBENTRY *source_entry = source->as_entry(); |
---|
1877 | GBENTRY *dest_entry = dest->as_entry(); |
---|
1878 | |
---|
1879 | gb_save_extern_data_in_ts(dest_entry); |
---|
1880 | dest_entry->insert_data(source_entry->data(), source_entry->size(), source_entry->memsize()); |
---|
1881 | |
---|
1882 | dest->flags.compressed_data = source->flags.compressed_data; |
---|
1883 | break; |
---|
1884 | } |
---|
1885 | case GB_DB: { |
---|
1886 | if (!dest->is_container()) { |
---|
1887 | GB_ERROR err = GB_export_errorf("GB_COPY Type conflict %s:%i != %s:%i", |
---|
1888 | GB_read_key_pntr(dest), dest->type(), GB_read_key_pntr(source), GB_DB); |
---|
1889 | GB_internal_error(err); |
---|
1890 | return err; |
---|
1891 | } |
---|
1892 | |
---|
1893 | GBCONTAINER *destc = dest->as_container(); |
---|
1894 | GBCONTAINER *sourcec = source->as_container(); |
---|
1895 | |
---|
1896 | if (sourcec->flags2.folded_container) gb_unfold(sourcec, -1, -1); |
---|
1897 | if (destc->flags2.folded_container) gb_unfold(destc, 0, -1); |
---|
1898 | |
---|
1899 | for (GBDATA *gb_p = GB_child(sourcec); gb_p; gb_p = GB_nextChild(gb_p)) { |
---|
1900 | const char *key = GB_read_key_pntr(gb_p); |
---|
1901 | GBDATA *gb_d; |
---|
1902 | |
---|
1903 | if (gb_p->is_container()) { |
---|
1904 | gb_d = GB_create_container(destc, key); |
---|
1905 | gb_create_header_array(gb_d->as_container(), gb_p->as_container()->d.size); |
---|
1906 | } |
---|
1907 | else { |
---|
1908 | gb_d = GB_create(destc, key, gb_p->type()); |
---|
1909 | } |
---|
1910 | |
---|
1911 | if (!gb_d) error = GB_await_error(); |
---|
1912 | else error = GB_copy_with_protection(gb_d, gb_p, copy_all_protections); |
---|
1913 | |
---|
1914 | if (error) break; |
---|
1915 | } |
---|
1916 | |
---|
1917 | destc->flags3 = sourcec->flags3; |
---|
1918 | break; |
---|
1919 | } |
---|
1920 | default: |
---|
1921 | error = GB_export_error("GB_copy-error: unhandled type"); |
---|
1922 | } |
---|
1923 | if (error) return error; |
---|
1924 | |
---|
1925 | gb_touch_entry(dest, GB_NORMAL_CHANGE); |
---|
1926 | |
---|
1927 | dest->flags.security_read = source->flags.security_read; |
---|
1928 | if (copy_all_protections) { |
---|
1929 | dest->flags.security_write = source->flags.security_write; |
---|
1930 | dest->flags.security_delete = source->flags.security_delete; |
---|
1931 | } |
---|
1932 | |
---|
1933 | return 0; |
---|
1934 | } |
---|
1935 | |
---|
1936 | |
---|
1937 | static char *gb_stpcpy(char *dest, const char *source) |
---|
1938 | { |
---|
1939 | while ((*dest++=*source++)) ; |
---|
1940 | return dest-1; // return pointer to last copied character (which is \0) |
---|
1941 | } |
---|
1942 | |
---|
1943 | char* GB_get_subfields(GBDATA *gbd) { |
---|
1944 | /*! Get all subfield names |
---|
1945 | * |
---|
1946 | * @return all subfields of 'gbd' as ';'-separated heap-copy |
---|
1947 | * (first and last char of result is a ';') |
---|
1948 | */ |
---|
1949 | GB_test_transaction(gbd); |
---|
1950 | |
---|
1951 | char *result = 0; |
---|
1952 | if (gbd->is_container()) { |
---|
1953 | GBCONTAINER *gbc = gbd->as_container(); |
---|
1954 | int result_length = 0; |
---|
1955 | |
---|
1956 | if (gbc->flags2.folded_container) { |
---|
1957 | gb_unfold(gbc, -1, -1); |
---|
1958 | } |
---|
1959 | |
---|
1960 | for (GBDATA *gbp = GB_child(gbd); gbp; gbp = GB_nextChild(gbp)) { |
---|
1961 | const char *key = GB_read_key_pntr(gbp); |
---|
1962 | int keylen = strlen(key); |
---|
1963 | |
---|
1964 | if (result) { |
---|
1965 | char *neu_result = (char*)malloc(result_length+keylen+1+1); |
---|
1966 | |
---|
1967 | if (neu_result) { |
---|
1968 | char *p = gb_stpcpy(neu_result, result); |
---|
1969 | p = gb_stpcpy(p, key); |
---|
1970 | *p++ = ';'; |
---|
1971 | p[0] = 0; |
---|
1972 | |
---|
1973 | freeset(result, neu_result); |
---|
1974 | result_length += keylen+1; |
---|
1975 | } |
---|
1976 | else { |
---|
1977 | gb_assert(0); |
---|
1978 | } |
---|
1979 | } |
---|
1980 | else { |
---|
1981 | result = (char*)malloc(1+keylen+1+1); |
---|
1982 | result[0] = ';'; |
---|
1983 | strcpy(result+1, key); |
---|
1984 | result[keylen+1] = ';'; |
---|
1985 | result[keylen+2] = 0; |
---|
1986 | result_length = keylen+2; |
---|
1987 | } |
---|
1988 | } |
---|
1989 | } |
---|
1990 | else { |
---|
1991 | result = strdup(";"); |
---|
1992 | } |
---|
1993 | |
---|
1994 | return result; |
---|
1995 | } |
---|
1996 | |
---|
1997 | // -------------------------- |
---|
1998 | // temporary entries |
---|
1999 | |
---|
2000 | GB_ERROR GB_set_temporary(GBDATA *gbd) { // goes to header: __ATTR__USERESULT |
---|
2001 | /*! if the temporary flag is set, then that entry (including all subentries) will not be saved |
---|
2002 | * @see GB_clear_temporary() and GB_is_temporary() |
---|
2003 | */ |
---|
2004 | |
---|
2005 | GB_ERROR error = NULL; |
---|
2006 | GB_test_transaction(gbd); |
---|
2007 | |
---|
2008 | if (GB_GET_SECURITY_DELETE(gbd)>GB_MAIN(gbd)->security_level) { |
---|
2009 | error = GBS_global_string("Security error in GB_set_temporary: %s", GB_read_key_pntr(gbd)); |
---|
2010 | } |
---|
2011 | else { |
---|
2012 | gbd->flags.temporary = 1; |
---|
2013 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
---|
2014 | } |
---|
2015 | RETURN_ERROR(error); |
---|
2016 | } |
---|
2017 | |
---|
2018 | GB_ERROR GB_clear_temporary(GBDATA *gbd) { // @@@ used in ptpan branch - do not remove |
---|
2019 | //! undo effect of GB_set_temporary() |
---|
2020 | |
---|
2021 | GB_test_transaction(gbd); |
---|
2022 | gbd->flags.temporary = 0; |
---|
2023 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
---|
2024 | return 0; |
---|
2025 | } |
---|
2026 | |
---|
2027 | bool GB_is_temporary(GBDATA *gbd) { |
---|
2028 | //! @see GB_set_temporary() and GB_in_temporary_branch() |
---|
2029 | GB_test_transaction(gbd); |
---|
2030 | return (long)gbd->flags.temporary; |
---|
2031 | } |
---|
2032 | |
---|
2033 | bool GB_in_temporary_branch(GBDATA *gbd) { |
---|
2034 | /*! @return true, if 'gbd' is member of a temporary subtree, |
---|
2035 | * i.e. if GB_is_temporary(itself or any parent) |
---|
2036 | */ |
---|
2037 | |
---|
2038 | if (GB_is_temporary(gbd)) return true; |
---|
2039 | |
---|
2040 | GBDATA *gb_parent = GB_get_father(gbd); |
---|
2041 | if (!gb_parent) return false; |
---|
2042 | |
---|
2043 | return GB_in_temporary_branch(gb_parent); |
---|
2044 | } |
---|
2045 | |
---|
2046 | // --------------------- |
---|
2047 | // transactions |
---|
2048 | |
---|
2049 | GB_ERROR GB_MAIN_TYPE::initial_client_transaction() { |
---|
2050 | // the first client transaction ever |
---|
2051 | transaction_level = 1; |
---|
2052 | GB_ERROR error = gbcmc_init_transaction(root_container); |
---|
2053 | if (!error) ++clock; |
---|
2054 | return error; |
---|
2055 | } |
---|
2056 | |
---|
2057 | inline GB_ERROR GB_MAIN_TYPE::start_transaction() { |
---|
2058 | gb_assert(transaction_level == 0); |
---|
2059 | |
---|
2060 | transaction_level = 1; |
---|
2061 | aborted_transaction = 0; |
---|
2062 | |
---|
2063 | GB_ERROR error = NULL; |
---|
2064 | if (is_client()) { |
---|
2065 | error = gbcmc_begin_transaction(gb_main()); |
---|
2066 | if (!error) { |
---|
2067 | error = gb_commit_transaction_local_rek(gb_main_ref(), 0, 0); // init structures |
---|
2068 | gb_untouch_children_and_me(root_container); |
---|
2069 | } |
---|
2070 | } |
---|
2071 | |
---|
2072 | if (!error) { |
---|
2073 | /* do all callbacks |
---|
2074 | * cb that change the db are no problem, because it's the beginning of a ta |
---|
2075 | */ |
---|
2076 | call_pending_callbacks(); |
---|
2077 | ++clock; |
---|
2078 | } |
---|
2079 | return error; |
---|
2080 | } |
---|
2081 | |
---|
2082 | inline GB_ERROR GB_MAIN_TYPE::begin_transaction() { |
---|
2083 | if (transaction_level>0) return GBS_global_string("attempt to start a NEW transaction (at transaction level %i)", transaction_level); |
---|
2084 | if (transaction_level == 0) return start_transaction(); |
---|
2085 | return NULL; // NO_TRANSACTION_MODE |
---|
2086 | } |
---|
2087 | |
---|
2088 | inline GB_ERROR GB_MAIN_TYPE::abort_transaction() { |
---|
2089 | if (transaction_level<=0) { |
---|
2090 | if (transaction_level<0) return "GB_abort_transaction: Attempt to abort transaction in no-transaction-mode"; |
---|
2091 | return "GB_abort_transaction: No transaction running"; |
---|
2092 | } |
---|
2093 | if (transaction_level>1) { |
---|
2094 | aborted_transaction = 1; |
---|
2095 | return pop_transaction(); |
---|
2096 | } |
---|
2097 | |
---|
2098 | gb_abort_transaction_local_rek(gb_main_ref()); |
---|
2099 | if (is_client()) { |
---|
2100 | GB_ERROR error = gbcmc_abort_transaction(gb_main()); |
---|
2101 | if (error) return error; |
---|
2102 | } |
---|
2103 | clock--; |
---|
2104 | call_pending_callbacks(); |
---|
2105 | transaction_level = 0; |
---|
2106 | gb_untouch_children_and_me(root_container); |
---|
2107 | return 0; |
---|
2108 | } |
---|
2109 | |
---|
2110 | inline GB_ERROR GB_MAIN_TYPE::commit_transaction() { |
---|
2111 | GB_ERROR error = 0; |
---|
2112 | GB_CHANGE flag; |
---|
2113 | |
---|
2114 | if (!transaction_level) { |
---|
2115 | return "commit_transaction: No transaction running"; |
---|
2116 | } |
---|
2117 | if (transaction_level>1) { |
---|
2118 | return GBS_global_string("attempt to commit at transaction level %i", transaction_level); |
---|
2119 | } |
---|
2120 | if (aborted_transaction) { |
---|
2121 | aborted_transaction = 0; |
---|
2122 | return abort_transaction(); |
---|
2123 | } |
---|
2124 | if (is_server()) { |
---|
2125 | char *error1 = gb_set_undo_sync(gb_main()); |
---|
2126 | while (1) { |
---|
2127 | flag = (GB_CHANGE)GB_ARRAY_FLAGS(gb_main()).changed; |
---|
2128 | if (!flag) break; // nothing to do |
---|
2129 | error = gb_commit_transaction_local_rek(gb_main_ref(), 0, 0); |
---|
2130 | gb_untouch_children_and_me(root_container); |
---|
2131 | if (error) break; |
---|
2132 | call_pending_callbacks(); |
---|
2133 | } |
---|
2134 | gb_disable_undo(gb_main()); |
---|
2135 | if (error1) { |
---|
2136 | transaction_level = 0; |
---|
2137 | gb_assert(error); // maybe return error1? |
---|
2138 | return error; // @@@ huh? why not return error1 |
---|
2139 | } |
---|
2140 | } |
---|
2141 | else { |
---|
2142 | gb_disable_undo(gb_main()); |
---|
2143 | while (1) { |
---|
2144 | flag = (GB_CHANGE)GB_ARRAY_FLAGS(gb_main()).changed; |
---|
2145 | if (!flag) break; // nothing to do |
---|
2146 | |
---|
2147 | error = gbcmc_begin_sendupdate(gb_main()); if (error) break; |
---|
2148 | error = gb_commit_transaction_local_rek(gb_main_ref(), 1, 0); if (error) break; |
---|
2149 | error = gbcmc_end_sendupdate(gb_main()); if (error) break; |
---|
2150 | |
---|
2151 | gb_untouch_children_and_me(root_container); |
---|
2152 | call_pending_callbacks(); |
---|
2153 | } |
---|
2154 | if (!error) error = gbcmc_commit_transaction(gb_main()); |
---|
2155 | |
---|
2156 | } |
---|
2157 | transaction_level = 0; |
---|
2158 | return error; |
---|
2159 | } |
---|
2160 | |
---|
2161 | inline GB_ERROR GB_MAIN_TYPE::push_transaction() { |
---|
2162 | if (transaction_level == 0) return start_transaction(); |
---|
2163 | if (transaction_level>0) ++transaction_level; |
---|
2164 | // transaction<0 is NO_TRANSACTION_MODE |
---|
2165 | return NULL; |
---|
2166 | } |
---|
2167 | |
---|
2168 | inline GB_ERROR GB_MAIN_TYPE::pop_transaction() { |
---|
2169 | if (transaction_level==0) return "attempt to pop nested transaction while none running"; |
---|
2170 | if (transaction_level<0) return 0; // NO_TRANSACTION_MODE |
---|
2171 | if (transaction_level==1) return commit_transaction(); |
---|
2172 | transaction_level--; |
---|
2173 | return NULL; |
---|
2174 | } |
---|
2175 | |
---|
2176 | inline GB_ERROR GB_MAIN_TYPE::no_transaction() { |
---|
2177 | if (is_client()) return "Tried to disable transactions in a client"; |
---|
2178 | transaction_level = -1; |
---|
2179 | return NULL; |
---|
2180 | } |
---|
2181 | |
---|
2182 | GB_ERROR GB_MAIN_TYPE::send_update_to_server(GBDATA *gbd) { |
---|
2183 | GB_ERROR error = NULL; |
---|
2184 | |
---|
2185 | if (!transaction_level) error = "send_update_to_server: no transaction running"; |
---|
2186 | else if (is_server()) error = "send_update_to_server: only possible from clients (not from server itself)"; |
---|
2187 | else { |
---|
2188 | const gb_triggered_callback *chg_cbl_old = changeCBs.pending.get_tail(); |
---|
2189 | const gb_triggered_callback *del_cbl_old = deleteCBs.pending.get_tail(); |
---|
2190 | |
---|
2191 | error = gbcmc_begin_sendupdate(gb_main()); |
---|
2192 | if (!error) error = gb_commit_transaction_local_rek(gbd, 2, 0); |
---|
2193 | if (!error) error = gbcmc_end_sendupdate(gb_main()); |
---|
2194 | |
---|
2195 | if (!error && |
---|
2196 | (chg_cbl_old != changeCBs.pending.get_tail() || |
---|
2197 | del_cbl_old != deleteCBs.pending.get_tail())) |
---|
2198 | { |
---|
2199 | error = "send_update_to_server triggered a callback (this is not allowed)"; |
---|
2200 | } |
---|
2201 | } |
---|
2202 | return error; |
---|
2203 | } |
---|
2204 | |
---|
2205 | // -------------------------------------- |
---|
2206 | // client transaction interface |
---|
2207 | |
---|
2208 | GB_ERROR GB_push_transaction(GBDATA *gbd) { |
---|
2209 | /*! start a transaction if no transaction is running. |
---|
2210 | * (otherwise only trace nested transactions) |
---|
2211 | * |
---|
2212 | * recommended transaction usage: |
---|
2213 | * |
---|
2214 | * \code |
---|
2215 | * GB_ERROR myFunc() { |
---|
2216 | * GB_ERROR error = GB_push_transaction(gbd); |
---|
2217 | * if (!error) { |
---|
2218 | * error = ...; |
---|
2219 | * } |
---|
2220 | * return GB_end_transaction(gbd, error); |
---|
2221 | * } |
---|
2222 | * |
---|
2223 | * void myFunc() { |
---|
2224 | * GB_ERROR error = GB_push_transaction(gbd); |
---|
2225 | * if (!error) { |
---|
2226 | * error = ...; |
---|
2227 | * } |
---|
2228 | * GB_end_transaction_show_error(gbd, error, aw_message); |
---|
2229 | * } |
---|
2230 | * \endcode |
---|
2231 | * |
---|
2232 | * @see GB_pop_transaction(), GB_end_transaction(), GB_begin_transaction() |
---|
2233 | */ |
---|
2234 | |
---|
2235 | return GB_MAIN(gbd)->push_transaction(); |
---|
2236 | } |
---|
2237 | |
---|
2238 | GB_ERROR GB_pop_transaction(GBDATA *gbd) { |
---|
2239 | //! commit a transaction started with GB_push_transaction() |
---|
2240 | return GB_MAIN(gbd)->pop_transaction(); |
---|
2241 | } |
---|
2242 | GB_ERROR GB_begin_transaction(GBDATA *gbd) { |
---|
2243 | /*! like GB_push_transaction(), |
---|
2244 | * but fails if there is already an transaction running. |
---|
2245 | * @see GB_commit_transaction() and GB_abort_transaction() |
---|
2246 | */ |
---|
2247 | return GB_MAIN(gbd)->begin_transaction(); |
---|
2248 | } |
---|
2249 | GB_ERROR GB_no_transaction(GBDATA *gbd) { // goes to header: __ATTR__USERESULT |
---|
2250 | return GB_MAIN(gbd)->no_transaction(); |
---|
2251 | } |
---|
2252 | |
---|
2253 | GB_ERROR GB_abort_transaction(GBDATA *gbd) { |
---|
2254 | /*! abort a running transaction, |
---|
2255 | * i.e. forget all changes made to DB inside the current transaction. |
---|
2256 | * |
---|
2257 | * May be called instead of GB_pop_transaction() or GB_commit_transaction() |
---|
2258 | * |
---|
2259 | * If a nested transactions got aborted, |
---|
2260 | * committing a surrounding transaction will silently abort it as well. |
---|
2261 | */ |
---|
2262 | return GB_MAIN(gbd)->abort_transaction(); |
---|
2263 | } |
---|
2264 | |
---|
2265 | GB_ERROR GB_commit_transaction(GBDATA *gbd) { |
---|
2266 | /*! commit a transaction started with GB_begin_transaction() |
---|
2267 | * |
---|
2268 | * commit changes made to DB. |
---|
2269 | * |
---|
2270 | * in case of nested transactions, this is equal to GB_pop_transaction() |
---|
2271 | */ |
---|
2272 | return GB_MAIN(gbd)->commit_transaction(); |
---|
2273 | } |
---|
2274 | |
---|
2275 | GB_ERROR GB_end_transaction(GBDATA *gbd, GB_ERROR error) { |
---|
2276 | /*! abort or commit transaction |
---|
2277 | * |
---|
2278 | * @ param error |
---|
2279 | * - if NULL commit transaction |
---|
2280 | * - else abort transaction |
---|
2281 | * |
---|
2282 | * always commits in no-transaction-mode |
---|
2283 | * |
---|
2284 | * @return error or transaction error |
---|
2285 | * @see GB_push_transaction() for example |
---|
2286 | */ |
---|
2287 | |
---|
2288 | if (GB_get_transaction_level(gbd)<0) { |
---|
2289 | ASSERT_RESULT(GB_ERROR, NULL, GB_pop_transaction(gbd)); |
---|
2290 | } |
---|
2291 | else { |
---|
2292 | if (error) GB_abort_transaction(gbd); |
---|
2293 | else error = GB_pop_transaction(gbd); |
---|
2294 | } |
---|
2295 | return error; |
---|
2296 | } |
---|
2297 | |
---|
2298 | void GB_end_transaction_show_error(GBDATA *gbd, GB_ERROR error, void (*error_handler)(GB_ERROR)) { |
---|
2299 | //! like GB_end_transaction(), but show error using 'error_handler' |
---|
2300 | error = GB_end_transaction(gbd, error); |
---|
2301 | if (error) error_handler(error); |
---|
2302 | } |
---|
2303 | |
---|
2304 | int GB_get_transaction_level(GBDATA *gbd) { |
---|
2305 | /*! @return transaction level |
---|
2306 | * <0 -> in no-transaction-mode (abort is impossible) |
---|
2307 | * 0 -> not in transaction |
---|
2308 | * 1 -> one single transaction |
---|
2309 | * 2, ... -> nested transactions |
---|
2310 | */ |
---|
2311 | return GB_MAIN(gbd)->get_transaction_level(); |
---|
2312 | } |
---|
2313 | |
---|
2314 | // ------------------ |
---|
2315 | // callbacks |
---|
2316 | |
---|
2317 | static void dummy_db_cb(GBDATA*, GB_CB_TYPE) { gb_assert(0); } // used as marker for deleted callbacks |
---|
2318 | DatabaseCallback TypedDatabaseCallback::MARKED_DELETED = makeDatabaseCallback(dummy_db_cb); |
---|
2319 | |
---|
2320 | GB_MAIN_TYPE *gb_get_main_during_cb() { |
---|
2321 | /* if inside a callback, return the DB root of the DB element, the callback was called for. |
---|
2322 | * if not inside a callback, return NULL. |
---|
2323 | */ |
---|
2324 | return inside_callback_main; |
---|
2325 | } |
---|
2326 | |
---|
2327 | NOT4PERL bool GB_inside_callback(GBDATA *of_gbd, GB_CB_TYPE cbtype) { |
---|
2328 | GB_MAIN_TYPE *Main = gb_get_main_during_cb(); |
---|
2329 | bool inside = false; |
---|
2330 | |
---|
2331 | if (Main) { // inside a callback |
---|
2332 | gb_assert(currently_called_back); |
---|
2333 | if (currently_called_back->gbd == of_gbd) { |
---|
2334 | GB_CB_TYPE curr_cbtype; |
---|
2335 | if (Main->has_pending_delete_callback()) { // delete callbacks were not all performed yet |
---|
2336 | // => current callback is a delete callback |
---|
2337 | curr_cbtype = GB_CB_TYPE(currently_called_back->spec.get_type() & GB_CB_DELETE); |
---|
2338 | } |
---|
2339 | else { |
---|
2340 | gb_assert(Main->has_pending_change_callback()); |
---|
2341 | curr_cbtype = GB_CB_TYPE(currently_called_back->spec.get_type() & (GB_CB_ALL-GB_CB_DELETE)); |
---|
2342 | } |
---|
2343 | gb_assert(curr_cbtype != GB_CB_NONE); // wtf!? are we inside callback or not? |
---|
2344 | |
---|
2345 | if ((cbtype&curr_cbtype) != GB_CB_NONE) { |
---|
2346 | inside = true; |
---|
2347 | } |
---|
2348 | } |
---|
2349 | } |
---|
2350 | |
---|
2351 | return inside; |
---|
2352 | } |
---|
2353 | |
---|
2354 | GBDATA *GB_get_gb_main_during_cb() { |
---|
2355 | GBDATA *gb_main = NULL; |
---|
2356 | GB_MAIN_TYPE *Main = gb_get_main_during_cb(); |
---|
2357 | |
---|
2358 | if (Main) { // inside callback |
---|
2359 | if (!GB_inside_callback(Main->gb_main(), GB_CB_DELETE)) { // main is not deleted |
---|
2360 | gb_main = Main->gb_main(); |
---|
2361 | } |
---|
2362 | } |
---|
2363 | return gb_main; |
---|
2364 | } |
---|
2365 | |
---|
2366 | |
---|
2367 | |
---|
2368 | static GB_CSTR gb_read_pntr_ts(GBDATA *gbd, gb_transaction_save *ts) { |
---|
2369 | int type = GB_TYPE_TS(ts); |
---|
2370 | const char *data = GB_GETDATA_TS(ts); |
---|
2371 | if (data) { |
---|
2372 | if (ts->flags.compressed_data) { // uncompressed data return pntr to database entry |
---|
2373 | long size = GB_GETSIZE_TS(ts) * gb_convert_type_2_sizeof[type] + gb_convert_type_2_appendix_size[type]; |
---|
2374 | data = gb_uncompress_data(gbd, data, size); |
---|
2375 | } |
---|
2376 | } |
---|
2377 | return data; |
---|
2378 | } |
---|
2379 | |
---|
2380 | // get last array value in callbacks |
---|
2381 | NOT4PERL const void *GB_read_old_value() { |
---|
2382 | char *data; |
---|
2383 | |
---|
2384 | if (!currently_called_back) { |
---|
2385 | GB_export_error("You cannot call GB_read_old_value outside a ARBDB callback"); |
---|
2386 | return NULL; |
---|
2387 | } |
---|
2388 | if (!currently_called_back->old) { |
---|
2389 | GB_export_error("No old value available in GB_read_old_value"); |
---|
2390 | return NULL; |
---|
2391 | } |
---|
2392 | data = GB_GETDATA_TS(currently_called_back->old); |
---|
2393 | if (!data) return NULL; |
---|
2394 | |
---|
2395 | return gb_read_pntr_ts(currently_called_back->gbd, currently_called_back->old); |
---|
2396 | } |
---|
2397 | // same for size |
---|
2398 | long GB_read_old_size() { |
---|
2399 | if (!currently_called_back) { |
---|
2400 | GB_export_error("You cannot call GB_read_old_size outside a ARBDB callback"); |
---|
2401 | return -1; |
---|
2402 | } |
---|
2403 | if (!currently_called_back->old) { |
---|
2404 | GB_export_error("No old value available in GB_read_old_size"); |
---|
2405 | return -1; |
---|
2406 | } |
---|
2407 | return GB_GETSIZE_TS(currently_called_back->old); |
---|
2408 | } |
---|
2409 | |
---|
2410 | inline char *cbtype2readable(GB_CB_TYPE type) { |
---|
2411 | ConstStrArray septype; |
---|
2412 | |
---|
2413 | #define appendcbtype(cbt) do { \ |
---|
2414 | if (type&cbt) { \ |
---|
2415 | type = GB_CB_TYPE(type-cbt); \ |
---|
2416 | septype.put(#cbt); \ |
---|
2417 | } \ |
---|
2418 | } while(0) |
---|
2419 | |
---|
2420 | appendcbtype(GB_CB_DELETE); |
---|
2421 | appendcbtype(GB_CB_CHANGED); |
---|
2422 | appendcbtype(GB_CB_SON_CREATED); |
---|
2423 | |
---|
2424 | gb_assert(type == GB_CB_NONE); |
---|
2425 | |
---|
2426 | return GBT_join_names(septype, '|'); |
---|
2427 | } |
---|
2428 | |
---|
2429 | char *TypedDatabaseCallback::get_info() const { |
---|
2430 | const char *readable_fun = GBS_funptr2readable((void*)dbcb.callee(), true); |
---|
2431 | char *readable_cbtype = cbtype2readable((GB_CB_TYPE)dbcb.inspect_CD2()); |
---|
2432 | char *result = GBS_global_string_copy("func='%s' type=%s clientdata=%p", |
---|
2433 | readable_fun, readable_cbtype, (void*)dbcb.inspect_CD1()); |
---|
2434 | |
---|
2435 | free(readable_cbtype); |
---|
2436 | |
---|
2437 | return result; |
---|
2438 | } |
---|
2439 | |
---|
2440 | char *GB_get_callback_info(GBDATA *gbd) { |
---|
2441 | // returns human-readable information about callbacks of 'gbd' or 0 |
---|
2442 | char *result = 0; |
---|
2443 | if (gbd->ext) { |
---|
2444 | gb_callback_list *cbl = gbd->get_callbacks(); |
---|
2445 | if (cbl) { |
---|
2446 | for (gb_callback_list::itertype cb = cbl->callbacks.begin(); cb != cbl->callbacks.end(); ++cb) { |
---|
2447 | char *cb_info = cb->spec.get_info(); |
---|
2448 | if (result) { |
---|
2449 | char *new_result = GBS_global_string_copy("%s\n%s", result, cb_info); |
---|
2450 | free(result); |
---|
2451 | free(cb_info); |
---|
2452 | result = new_result; |
---|
2453 | } |
---|
2454 | else { |
---|
2455 | result = cb_info; |
---|
2456 | } |
---|
2457 | } |
---|
2458 | } |
---|
2459 | } |
---|
2460 | |
---|
2461 | return result; |
---|
2462 | } |
---|
2463 | |
---|
2464 | #if defined(ASSERTION_USED) |
---|
2465 | template<typename CB> |
---|
2466 | bool CallbackList<CB>::contains_unremoved_callback(const CB& like) const { |
---|
2467 | for (const_itertype cb = callbacks.begin(); cb != callbacks.end(); ++cb) { |
---|
2468 | if (cb->spec.is_equal_to(like.spec) && !cb->spec.is_marked_for_removal()) { |
---|
2469 | return true; |
---|
2470 | } |
---|
2471 | } |
---|
2472 | return false; |
---|
2473 | } |
---|
2474 | #endif |
---|
2475 | |
---|
2476 | inline void add_to_callback_chain(gb_callback_list*& head, const TypedDatabaseCallback& cbs) { |
---|
2477 | if (!head) head = new gb_callback_list; |
---|
2478 | head->add(gb_callback(cbs)); |
---|
2479 | } |
---|
2480 | inline void add_to_callback_chain(gb_hierarchy_callback_list*& head, const TypedDatabaseCallback& cbs, GBDATA *gb_representative) { |
---|
2481 | if (!head) head = new gb_hierarchy_callback_list; |
---|
2482 | head->add(gb_hierarchy_callback(cbs, gb_representative)); |
---|
2483 | } |
---|
2484 | |
---|
2485 | inline GB_ERROR gb_add_callback(GBDATA *gbd, const TypedDatabaseCallback& cbs) { |
---|
2486 | /* Adds a callback to a DB entry. |
---|
2487 | * |
---|
2488 | * Be careful when writing GB_CB_DELETE callbacks, there is a severe restriction: |
---|
2489 | * |
---|
2490 | * - the DB element may already be freed. The pointer is still pointing to the original |
---|
2491 | * location, so you can use it to identify the DB element, but you cannot dereference |
---|
2492 | * it under all circumstances. |
---|
2493 | * |
---|
2494 | * ARBDB internal delete-callbacks may use gb_get_main_during_cb() to access the DB root. |
---|
2495 | * See also: GB_get_gb_main_during_cb() |
---|
2496 | */ |
---|
2497 | |
---|
2498 | #if defined(DEBUG) |
---|
2499 | if (GB_inside_callback(gbd, GB_CB_DELETE)) { |
---|
2500 | printf("Warning: add_priority_callback called inside delete-callback of gbd (gbd may already be freed)\n"); |
---|
2501 | #if defined(DEVEL_RALF) |
---|
2502 | gb_assert(0); // fix callback-handling (never modify callbacks from inside delete callbacks) |
---|
2503 | #endif // DEVEL_RALF |
---|
2504 | } |
---|
2505 | #endif // DEBUG |
---|
2506 | |
---|
2507 | GB_test_transaction(gbd); // may return error |
---|
2508 | gbd->create_extended(); |
---|
2509 | add_to_callback_chain(gbd->ext->callback, cbs); |
---|
2510 | return 0; |
---|
2511 | } |
---|
2512 | |
---|
2513 | GB_ERROR GB_add_callback(GBDATA *gbd, GB_CB_TYPE type, const DatabaseCallback& dbcb) { |
---|
2514 | return gb_add_callback(gbd, TypedDatabaseCallback(dbcb, type)); |
---|
2515 | } |
---|
2516 | |
---|
2517 | inline void GB_MAIN_TYPE::callback_group::add_hcb(GBDATA *gb_representative, const TypedDatabaseCallback& dbcb) { |
---|
2518 | add_to_callback_chain(hierarchy_cbs, dbcb, gb_representative); |
---|
2519 | } |
---|
2520 | |
---|
2521 | GB_ERROR GB_MAIN_TYPE::add_hierarchy_cb(GBDATA *gb_representative, const TypedDatabaseCallback& dbcb) { |
---|
2522 | GB_CB_TYPE type = dbcb.get_type(); |
---|
2523 | if (type & GB_CB_DELETE) { |
---|
2524 | deleteCBs.add_hcb(gb_representative, dbcb.with_type_changed_to(GB_CB_DELETE)); |
---|
2525 | } |
---|
2526 | if (type & GB_CB_ALL_BUT_DELETE) { |
---|
2527 | changeCBs.add_hcb(gb_representative, dbcb.with_type_changed_to(GB_CB_TYPE(type&GB_CB_ALL_BUT_DELETE))); |
---|
2528 | } |
---|
2529 | return NULL; |
---|
2530 | } |
---|
2531 | |
---|
2532 | GB_ERROR GB_add_hierarchy_callback(GBDATA *gbd, GB_CB_TYPE type, const DatabaseCallback& dbcb) { // @@@ needed to impl #418 |
---|
2533 | /*! bind callback to ALL entries which are at the same DB-hierarchy as 'gbd'. |
---|
2534 | * |
---|
2535 | * Hierarchy callbacks are triggered before normal callbacks (added by GB_add_callback or GB_ensure_callback). |
---|
2536 | * Nevertheless delete callbacks take precedence over change callbacks |
---|
2537 | * (i.e. a normal delete callback is triggered before a hierarchical change callback). |
---|
2538 | * |
---|
2539 | * Hierarchy callbacks are cannot be installed and will NOT be triggered in NO_TRANSACTION_MODE |
---|
2540 | * (i.e. it will not work in ARBs property DBs) |
---|
2541 | */ |
---|
2542 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
2543 | gb_assert(Main->get_transaction_level()>=0); // hierarchical callbacks are not supported in NO_TRANSACTION_MODE |
---|
2544 | return Main->add_hierarchy_cb(gbd, TypedDatabaseCallback(dbcb, type)); |
---|
2545 | } |
---|
2546 | |
---|
2547 | template <typename PRED> |
---|
2548 | inline void gb_remove_callbacks_that(GBDATA *gbd, PRED shallRemove) { |
---|
2549 | #if defined(ASSERTION_USED) |
---|
2550 | if (GB_inside_callback(gbd, GB_CB_DELETE)) { |
---|
2551 | printf("Warning: gb_remove_callback called inside delete-callback of gbd (gbd may already be freed)\n"); |
---|
2552 | gb_assert(0); // fix callback-handling (never modify callbacks from inside delete callbacks) |
---|
2553 | return; |
---|
2554 | } |
---|
2555 | #endif // DEBUG |
---|
2556 | |
---|
2557 | if (gbd->ext) { |
---|
2558 | gb_callback_list *cbl = gbd->get_callbacks(); |
---|
2559 | if (cbl) { |
---|
2560 | bool prev_running = false; |
---|
2561 | |
---|
2562 | for (gb_callback_list::itertype cb = cbl->callbacks.begin(); cb != cbl->callbacks.end(); ) { |
---|
2563 | bool this_running = cb->running; |
---|
2564 | |
---|
2565 | if (shallRemove(*cb)) { |
---|
2566 | if (prev_running || this_running) { |
---|
2567 | cb->spec.mark_for_removal(); |
---|
2568 | ++cb; |
---|
2569 | } |
---|
2570 | else { |
---|
2571 | cb = cbl->callbacks.erase(cb); |
---|
2572 | } |
---|
2573 | } |
---|
2574 | else { |
---|
2575 | ++cb; |
---|
2576 | } |
---|
2577 | prev_running = this_running; |
---|
2578 | } |
---|
2579 | } |
---|
2580 | } |
---|
2581 | } |
---|
2582 | |
---|
2583 | struct ShallBeDeleted { |
---|
2584 | bool operator()(const gb_callback& cb) const { return cb.spec.is_marked_for_removal(); } |
---|
2585 | }; |
---|
2586 | static void gb_remove_callbacks_marked_for_deletion(GBDATA *gbd) { |
---|
2587 | gb_remove_callbacks_that(gbd, ShallBeDeleted()); |
---|
2588 | } |
---|
2589 | |
---|
2590 | struct IsCallback : private TypedDatabaseCallback { |
---|
2591 | IsCallback(GB_CB func_, GB_CB_TYPE type_) : TypedDatabaseCallback(makeDatabaseCallback((GB_CB)func_, (int*)NULL), type_) {} |
---|
2592 | bool operator()(const gb_callback& cb) const { return sig_is_equal_to(cb.spec); } |
---|
2593 | }; |
---|
2594 | struct IsSpecificCallback : private TypedDatabaseCallback { |
---|
2595 | IsSpecificCallback(const TypedDatabaseCallback& cb) : TypedDatabaseCallback(cb) {} |
---|
2596 | bool operator()(const gb_callback& cb) const { return is_equal_to(cb.spec); } |
---|
2597 | }; |
---|
2598 | |
---|
2599 | void GB_remove_callback(GBDATA *gbd, GB_CB_TYPE type, const DatabaseCallback& dbcb) { |
---|
2600 | // remove specific callback; 'type' and 'dbcb' have to match |
---|
2601 | gb_remove_callbacks_that(gbd, IsSpecificCallback(TypedDatabaseCallback(dbcb, type))); |
---|
2602 | } |
---|
2603 | void GB_remove_all_callbacks_to(GBDATA *gbd, GB_CB_TYPE type, GB_CB func) { |
---|
2604 | // removes all callbacks 'func' bound to 'gbd' with 'type' |
---|
2605 | gb_remove_callbacks_that(gbd, IsCallback(func, type)); |
---|
2606 | } |
---|
2607 | |
---|
2608 | GB_ERROR GB_ensure_callback(GBDATA *gbd, GB_CB_TYPE type, const DatabaseCallback& dbcb) { |
---|
2609 | TypedDatabaseCallback newcb(dbcb, type); |
---|
2610 | gb_callback_list *cbl = gbd->get_callbacks(); |
---|
2611 | if (cbl) { |
---|
2612 | for (gb_callback_list::itertype cb = cbl->callbacks.begin(); cb != cbl->callbacks.end(); ++cb) { |
---|
2613 | if (cb->spec.is_equal_to(newcb) && !cb->spec.is_marked_for_removal()) { |
---|
2614 | return NULL; // already in cb list |
---|
2615 | } |
---|
2616 | } |
---|
2617 | } |
---|
2618 | return gb_add_callback(gbd, newcb); |
---|
2619 | } |
---|
2620 | |
---|
2621 | int GB_nsons(GBDATA *gbd) { |
---|
2622 | /*! return number of child entries |
---|
2623 | * |
---|
2624 | * @@@ does this work in clients ? |
---|
2625 | */ |
---|
2626 | |
---|
2627 | return gbd->is_container() |
---|
2628 | ? gbd->as_container()->d.size |
---|
2629 | : 0; |
---|
2630 | } |
---|
2631 | |
---|
2632 | void GB_disable_quicksave(GBDATA *gbd, const char *reason) { |
---|
2633 | /*! Disable quicksaving database |
---|
2634 | * @param gbd any DB node |
---|
2635 | * @param reason why quicksaving is not allowed |
---|
2636 | */ |
---|
2637 | freedup(GB_MAIN(gbd)->qs.quick_save_disabled, reason); |
---|
2638 | } |
---|
2639 | |
---|
2640 | GB_ERROR GB_resort_data_base(GBDATA *gb_main, GBDATA **new_order_list, long listsize) { |
---|
2641 | { |
---|
2642 | long client_count = GB_read_clients(gb_main); |
---|
2643 | if (client_count<0) { |
---|
2644 | return "Sorry: this program is not the arbdb server, you cannot resort your data"; |
---|
2645 | } |
---|
2646 | if (client_count>0) { |
---|
2647 | // resort will do a big amount of client update callbacks => disallow clients here |
---|
2648 | bool called_from_macro = GB_inside_remote_action(gb_main); |
---|
2649 | if (!called_from_macro) { // accept macro clients |
---|
2650 | return GBS_global_string("There are %li clients (editors, tree programs) connected to this server.\n" |
---|
2651 | "You need to close these clients before you can run this operation.", |
---|
2652 | client_count); |
---|
2653 | } |
---|
2654 | } |
---|
2655 | } |
---|
2656 | |
---|
2657 | if (listsize <= 0) return 0; |
---|
2658 | |
---|
2659 | GBCONTAINER *father = GB_FATHER(new_order_list[0]); |
---|
2660 | GB_disable_quicksave(gb_main, "some entries in the database got a new order"); |
---|
2661 | |
---|
2662 | gb_header_list *hl = GB_DATA_LIST_HEADER(father->d); |
---|
2663 | for (long new_index = 0; new_index< listsize; new_index++) { |
---|
2664 | long old_index = new_order_list[new_index]->index; |
---|
2665 | |
---|
2666 | if (old_index < new_index) { |
---|
2667 | GB_warningf("Warning at resort database: entry exists twice: %li and %li", |
---|
2668 | old_index, new_index); |
---|
2669 | } |
---|
2670 | else { |
---|
2671 | GBDATA *ogb = GB_HEADER_LIST_GBD(hl[old_index]); |
---|
2672 | GBDATA *ngb = GB_HEADER_LIST_GBD(hl[new_index]); |
---|
2673 | |
---|
2674 | gb_header_list h = hl[new_index]; |
---|
2675 | hl[new_index] = hl[old_index]; |
---|
2676 | hl[old_index] = h; // Warning: Relative Pointers are incorrect !!! |
---|
2677 | |
---|
2678 | SET_GB_HEADER_LIST_GBD(hl[old_index], ngb); |
---|
2679 | SET_GB_HEADER_LIST_GBD(hl[new_index], ogb); |
---|
2680 | |
---|
2681 | if (ngb) ngb->index = old_index; |
---|
2682 | if (ogb) ogb->index = new_index; |
---|
2683 | } |
---|
2684 | } |
---|
2685 | |
---|
2686 | gb_touch_entry(father, GB_NORMAL_CHANGE); |
---|
2687 | return 0; |
---|
2688 | } |
---|
2689 | |
---|
2690 | GB_ERROR gb_resort_system_folder_to_top(GBCONTAINER *gb_main) { |
---|
2691 | GBDATA *gb_system = GB_entry(gb_main, GB_SYSTEM_FOLDER); |
---|
2692 | GBDATA *gb_first = GB_child(gb_main); |
---|
2693 | GBDATA **new_order_list; |
---|
2694 | GB_ERROR error = 0; |
---|
2695 | int i, len; |
---|
2696 | if (GB_read_clients(gb_main)<0) return 0; // we are not server |
---|
2697 | if (!gb_system) { |
---|
2698 | return GB_export_error("System databaseentry does not exist"); |
---|
2699 | } |
---|
2700 | if (gb_first == gb_system) return 0; |
---|
2701 | len = GB_number_of_subentries(gb_main); |
---|
2702 | new_order_list = (GBDATA **)GB_calloc(sizeof(GBDATA *), len); |
---|
2703 | new_order_list[0] = gb_system; |
---|
2704 | for (i=1; i<len; i++) { |
---|
2705 | new_order_list[i] = gb_first; |
---|
2706 | do { |
---|
2707 | gb_first = GB_nextChild(gb_first); |
---|
2708 | } while (gb_first == gb_system); |
---|
2709 | } |
---|
2710 | error = GB_resort_data_base(gb_main, new_order_list, len); |
---|
2711 | free(new_order_list); |
---|
2712 | return error; |
---|
2713 | } |
---|
2714 | |
---|
2715 | // ------------------------------ |
---|
2716 | // private(?) user flags |
---|
2717 | |
---|
2718 | STATIC_ASSERT_ANNOTATED(((GB_USERFLAG_ANY+1)&GB_USERFLAG_ANY) == 0, "not all bits set in GB_USERFLAG_ANY"); |
---|
2719 | |
---|
2720 | #if defined(ASSERTION_USED) |
---|
2721 | inline bool legal_user_bitmask(unsigned char bitmask) { |
---|
2722 | return bitmask>0 && bitmask<=GB_USERFLAG_ANY; |
---|
2723 | } |
---|
2724 | #endif |
---|
2725 | |
---|
2726 | inline gb_flag_types2& get_user_flags(GBDATA *gbd) { |
---|
2727 | return gbd->expect_container()->flags2; |
---|
2728 | } |
---|
2729 | |
---|
2730 | bool GB_user_flag(GBDATA *gbd, unsigned char user_bit) { |
---|
2731 | gb_assert(legal_user_bitmask(user_bit)); |
---|
2732 | return get_user_flags(gbd).user_bits & user_bit; |
---|
2733 | } |
---|
2734 | void GB_set_user_flag(GBDATA *gbd, unsigned char user_bit) { |
---|
2735 | gb_assert(legal_user_bitmask(user_bit)); |
---|
2736 | gb_flag_types2& flags = get_user_flags(gbd); |
---|
2737 | flags.user_bits |= user_bit; |
---|
2738 | } |
---|
2739 | |
---|
2740 | void GB_clear_user_flag(GBDATA *gbd, unsigned char user_bit) { |
---|
2741 | gb_assert(legal_user_bitmask(user_bit)); |
---|
2742 | gb_flag_types2& flags = get_user_flags(gbd); |
---|
2743 | flags.user_bits &= (user_bit^GB_USERFLAG_ANY); |
---|
2744 | } |
---|
2745 | |
---|
2746 | // ------------------------ |
---|
2747 | // mark DB entries |
---|
2748 | |
---|
2749 | void GB_write_flag(GBDATA *gbd, long flag) { |
---|
2750 | GBCONTAINER *gbc = gbd->expect_container(); |
---|
2751 | GB_MAIN_TYPE *Main = GB_MAIN(gbc); |
---|
2752 | |
---|
2753 | GB_test_transaction(Main); |
---|
2754 | |
---|
2755 | int ubit = Main->users[0]->userbit; |
---|
2756 | int prev = GB_ARRAY_FLAGS(gbc).flags; |
---|
2757 | gbc->flags.saved_flags = prev; |
---|
2758 | |
---|
2759 | if (flag) { |
---|
2760 | GB_ARRAY_FLAGS(gbc).flags |= ubit; |
---|
2761 | } |
---|
2762 | else { |
---|
2763 | GB_ARRAY_FLAGS(gbc).flags &= ~ubit; |
---|
2764 | } |
---|
2765 | if (prev != (int)GB_ARRAY_FLAGS(gbc).flags) { |
---|
2766 | gb_touch_entry(gbc, GB_NORMAL_CHANGE); |
---|
2767 | gb_touch_header(GB_FATHER(gbc)); |
---|
2768 | GB_DO_CALLBACKS(gbc); |
---|
2769 | } |
---|
2770 | } |
---|
2771 | |
---|
2772 | int GB_read_flag(GBDATA *gbd) { |
---|
2773 | GB_test_transaction(gbd); |
---|
2774 | if (GB_ARRAY_FLAGS(gbd).flags & GB_MAIN(gbd)->users[0]->userbit) return 1; |
---|
2775 | else return 0; |
---|
2776 | } |
---|
2777 | |
---|
2778 | void GB_touch(GBDATA *gbd) { |
---|
2779 | GB_test_transaction(gbd); |
---|
2780 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
---|
2781 | GB_DO_CALLBACKS(gbd); |
---|
2782 | } |
---|
2783 | |
---|
2784 | |
---|
2785 | char GB_type_2_char(GB_TYPES type) { |
---|
2786 | const char *type2char = "-bcif-B-CIFlSS-%"; |
---|
2787 | return type2char[type]; |
---|
2788 | } |
---|
2789 | |
---|
2790 | GB_ERROR GB_print_debug_information(void */*dummy_AW_root*/, GBDATA *gb_main) { |
---|
2791 | GB_MAIN_TYPE *Main = GB_MAIN(gb_main); |
---|
2792 | GB_push_transaction(gb_main); |
---|
2793 | for (int i=0; i<Main->keycnt; i++) { |
---|
2794 | if (Main->keys[i].key) { |
---|
2795 | printf("%3i %20s nref %i\n", i, Main->keys[i].key, (int)Main->keys[i].nref); |
---|
2796 | } |
---|
2797 | else { |
---|
2798 | printf(" %3i unused key, next free key = %li\n", i, Main->keys[i].next_free_key); |
---|
2799 | } |
---|
2800 | } |
---|
2801 | gbm_debug_mem(); |
---|
2802 | GB_pop_transaction(gb_main); |
---|
2803 | return 0; |
---|
2804 | } |
---|
2805 | |
---|
2806 | static int GB_info_deep = 15; |
---|
2807 | |
---|
2808 | |
---|
2809 | static int gb_info(GBDATA *gbd, int deep) { |
---|
2810 | if (gbd==NULL) { printf("NULL\n"); return -1; } |
---|
2811 | GB_push_transaction(gbd); |
---|
2812 | |
---|
2813 | GB_TYPES type = gbd->type(); |
---|
2814 | |
---|
2815 | if (deep) { |
---|
2816 | printf(" "); |
---|
2817 | } |
---|
2818 | |
---|
2819 | printf("(GBDATA*)0x%lx (GBCONTAINER*)0x%lx ", (long)gbd, (long)gbd); |
---|
2820 | |
---|
2821 | if (gbd->rel_father==0) { printf("father=NULL\n"); return -1; } |
---|
2822 | |
---|
2823 | GBCONTAINER *gbc; |
---|
2824 | GB_MAIN_TYPE *Main; |
---|
2825 | if (type==GB_DB) { gbc = gbd->as_container(); Main = GBCONTAINER_MAIN(gbc); } |
---|
2826 | else { gbc = NULL; Main = GB_MAIN(gbd); } |
---|
2827 | |
---|
2828 | if (!Main) { printf("Oops - I have no main entry!!!\n"); return -1; } |
---|
2829 | if (gbd==Main->dummy_father) { printf("dummy_father!\n"); return -1; } |
---|
2830 | |
---|
2831 | printf("%10s Type '%c' ", GB_read_key_pntr(gbd), GB_type_2_char(type)); |
---|
2832 | |
---|
2833 | switch (type) { |
---|
2834 | case GB_DB: { |
---|
2835 | int size = gbc->d.size; |
---|
2836 | printf("Size %i nheader %i hmemsize %i", gbc->d.size, gbc->d.nheader, gbc->d.headermemsize); |
---|
2837 | printf(" father=(GBDATA*)0x%lx\n", (long)GB_FATHER(gbd)); |
---|
2838 | if (size < GB_info_deep) { |
---|
2839 | int index; |
---|
2840 | gb_header_list *header; |
---|
2841 | |
---|
2842 | header = GB_DATA_LIST_HEADER(gbc->d); |
---|
2843 | for (index = 0; index < gbc->d.nheader; index++) { |
---|
2844 | GBDATA *gb_sub = GB_HEADER_LIST_GBD(header[index]); |
---|
2845 | printf("\t\t%10s (GBDATA*)0x%lx (GBCONTAINER*)0x%lx\n", Main->keys[header[index].flags.key_quark].key, (long)gb_sub, (long)gb_sub); |
---|
2846 | } |
---|
2847 | } |
---|
2848 | break; |
---|
2849 | } |
---|
2850 | default: { |
---|
2851 | char *data = GB_read_as_string(gbd); |
---|
2852 | if (data) { printf("%s", data); free(data); } |
---|
2853 | printf(" father=(GBDATA*)0x%lx\n", (long)GB_FATHER(gbd)); |
---|
2854 | } |
---|
2855 | } |
---|
2856 | |
---|
2857 | |
---|
2858 | GB_pop_transaction(gbd); |
---|
2859 | |
---|
2860 | return 0; |
---|
2861 | } |
---|
2862 | |
---|
2863 | int GB_info(GBDATA *gbd) { // unused - intended to be used in debugger |
---|
2864 | return gb_info(gbd, 0); |
---|
2865 | } |
---|
2866 | |
---|
2867 | long GB_number_of_subentries(GBDATA *gbd) { |
---|
2868 | GBCONTAINER *gbc = gbd->expect_container(); |
---|
2869 | gb_header_list *header = GB_DATA_LIST_HEADER(gbc->d); |
---|
2870 | |
---|
2871 | long subentries = 0; |
---|
2872 | int end = gbc->d.nheader; |
---|
2873 | |
---|
2874 | for (int index = 0; index<end; index++) { |
---|
2875 | if (header[index].flags.changed < GB_DELETED) subentries++; |
---|
2876 | } |
---|
2877 | return subentries; |
---|
2878 | } |
---|
2879 | |
---|
2880 | // -------------------------------------------------------------------------------- |
---|
2881 | |
---|
2882 | #ifdef UNIT_TESTS |
---|
2883 | |
---|
2884 | #include <test_unit.h> |
---|
2885 | #include <locale.h> |
---|
2886 | |
---|
2887 | void TEST_GB_atof() { |
---|
2888 | // startup of ARB (gtk_only@11651) is failing on ubuntu 13.10 (in GBT_read_tree) |
---|
2889 | // (failed with "Error: 'GB_safe_atof("0.0810811", ..) returns error: cannot convert '0.0810811' to double'") |
---|
2890 | // Reason: LANG[UAGE] or LC_NUMERIC set to "de_DE..." |
---|
2891 | // |
---|
2892 | // Notes: |
---|
2893 | // * gtk apparently calls 'setlocale(LC_ALL, "");', motif doesnt |
---|
2894 | |
---|
2895 | TEST_EXPECT_SIMILAR(GB_atof("0.031"), 0.031, 0.0001); // @@@ make this fail, then fix it |
---|
2896 | } |
---|
2897 | |
---|
2898 | void TEST_999_strtod_replacement() { |
---|
2899 | // caution: if it fails -> locale is not reset (therefore call with low priority 999) |
---|
2900 | const char *old = setlocale(LC_NUMERIC, "de_DE.UTF-8"); |
---|
2901 | { |
---|
2902 | // TEST_EXPECT_SIMILAR__BROKEN(strtod("0.031", NULL), 0.031, 0.0001); |
---|
2903 | TEST_EXPECT_SIMILAR(g_ascii_strtod("0.031", NULL), 0.031, 0.0001); |
---|
2904 | } |
---|
2905 | setlocale(LC_NUMERIC, old); |
---|
2906 | } |
---|
2907 | |
---|
2908 | static void test_another_shell() { delete new GB_shell; } |
---|
2909 | static void test_opendb() { GB_close(GB_open("no.arb", "c")); } |
---|
2910 | |
---|
2911 | void TEST_GB_shell() { |
---|
2912 | { |
---|
2913 | GB_shell *shell = new GB_shell; |
---|
2914 | TEST_EXPECT_SEGFAULT(test_another_shell); |
---|
2915 | test_opendb(); // no SEGV here |
---|
2916 | delete shell; |
---|
2917 | } |
---|
2918 | |
---|
2919 | TEST_EXPECT_SEGFAULT(test_opendb); // should be impossible to open db w/o shell |
---|
2920 | } |
---|
2921 | |
---|
2922 | void TEST_GB_number_of_subentries() { |
---|
2923 | GB_shell shell; |
---|
2924 | GBDATA *gb_main = GB_open("no.arb", "c"); |
---|
2925 | |
---|
2926 | { |
---|
2927 | GB_transaction ta(gb_main); |
---|
2928 | |
---|
2929 | GBDATA *gb_cont = GB_create_container(gb_main, "container"); |
---|
2930 | TEST_EXPECT_EQUAL(GB_number_of_subentries(gb_cont), 0); |
---|
2931 | |
---|
2932 | TEST_EXPECT_RESULT__NOERROREXPORTED(GB_create(gb_cont, "entry", GB_STRING)); |
---|
2933 | TEST_EXPECT_EQUAL(GB_number_of_subentries(gb_cont), 1); |
---|
2934 | |
---|
2935 | { |
---|
2936 | GBDATA *gb_entry; |
---|
2937 | TEST_EXPECT_RESULT__NOERROREXPORTED(gb_entry = GB_create(gb_cont, "entry", GB_STRING)); |
---|
2938 | TEST_EXPECT_EQUAL(GB_number_of_subentries(gb_cont), 2); |
---|
2939 | |
---|
2940 | TEST_EXPECT_NO_ERROR(GB_delete(gb_entry)); |
---|
2941 | TEST_EXPECT_EQUAL(GB_number_of_subentries(gb_cont), 1); |
---|
2942 | } |
---|
2943 | |
---|
2944 | TEST_EXPECT_RESULT__NOERROREXPORTED(GB_create(gb_cont, "entry", GB_STRING)); |
---|
2945 | TEST_EXPECT_EQUAL(GB_number_of_subentries(gb_cont), 2); |
---|
2946 | } |
---|
2947 | |
---|
2948 | // TEST_REJECT(true); // @@@ fail while GB_shell open |
---|
2949 | |
---|
2950 | GB_close(gb_main); |
---|
2951 | } |
---|
2952 | |
---|
2953 | static void test_count_cb(GBDATA *, int *counter) { |
---|
2954 | fprintf(stderr, "test_count_cb: var.add=%p old.val=%i ", counter, *counter); |
---|
2955 | (*counter)++; |
---|
2956 | fprintf(stderr, "new.val=%i\n", *counter); |
---|
2957 | fflush(stderr); |
---|
2958 | } |
---|
2959 | |
---|
2960 | static void remove_self_cb(GBDATA *gbe, GB_CB_TYPE cbtype) { |
---|
2961 | GB_remove_callback(gbe, cbtype, makeDatabaseCallback(remove_self_cb)); |
---|
2962 | } |
---|
2963 | |
---|
2964 | static void re_add_self_cb(GBDATA *gbe, int *calledCounter, GB_CB_TYPE cbtype) { |
---|
2965 | ++(*calledCounter); |
---|
2966 | |
---|
2967 | DatabaseCallback dbcb = makeDatabaseCallback(re_add_self_cb, calledCounter); |
---|
2968 | GB_remove_callback(gbe, cbtype, dbcb); |
---|
2969 | |
---|
2970 | GB_ERROR error = GB_add_callback(gbe, cbtype, dbcb); |
---|
2971 | gb_assert(!error); |
---|
2972 | } |
---|
2973 | |
---|
2974 | void TEST_db_callbacks_ta_nota() { |
---|
2975 | GB_shell shell; |
---|
2976 | |
---|
2977 | enum TAmode { |
---|
2978 | NO_TA = 1, // no transaction mode |
---|
2979 | WITH_TA = 2, // transaction mode |
---|
2980 | |
---|
2981 | BOTH_TA_MODES = (NO_TA|WITH_TA) |
---|
2982 | }; |
---|
2983 | |
---|
2984 | for (TAmode ta_mode = NO_TA; ta_mode <= WITH_TA; ta_mode = TAmode(ta_mode+1)) { |
---|
2985 | GBDATA *gb_main = GB_open("no.arb", "c"); |
---|
2986 | GB_ERROR error; |
---|
2987 | |
---|
2988 | TEST_ANNOTATE(ta_mode == NO_TA ? "NO_TA" : "WITH_TA"); |
---|
2989 | if (ta_mode == NO_TA) { |
---|
2990 | error = GB_no_transaction(gb_main); TEST_EXPECT_NO_ERROR(error); |
---|
2991 | } |
---|
2992 | |
---|
2993 | // create some DB entries |
---|
2994 | GBDATA *gbc; |
---|
2995 | GBDATA *gbe1; |
---|
2996 | GBDATA *gbe2; |
---|
2997 | GBDATA *gbe3; |
---|
2998 | { |
---|
2999 | GB_transaction ta(gb_main); |
---|
3000 | |
---|
3001 | gbc = GB_create_container(gb_main, "cont"); |
---|
3002 | gbe1 = GB_create(gbc, "entry", GB_STRING); |
---|
3003 | gbe2 = GB_create(gb_main, "entry", GB_INT); |
---|
3004 | } |
---|
3005 | |
---|
3006 | // counters to detect called callbacks |
---|
3007 | int e1_changed = 0; |
---|
3008 | int e2_changed = 0; |
---|
3009 | int c_changed = 0; |
---|
3010 | int c_son_created = 0; |
---|
3011 | |
---|
3012 | int e1_deleted = 0; |
---|
3013 | int e2_deleted = 0; |
---|
3014 | int e3_deleted = 0; |
---|
3015 | int c_deleted = 0; |
---|
3016 | |
---|
3017 | #define CHCB_COUNTERS_EXPECTATION(e1c,e2c,cc,csc) \ |
---|
3018 | that(e1_changed).is_equal_to(e1c), \ |
---|
3019 | that(e2_changed).is_equal_to(e2c), \ |
---|
3020 | that(c_changed).is_equal_to(cc), \ |
---|
3021 | that(c_son_created).is_equal_to(csc) |
---|
3022 | |
---|
3023 | #define DLCB_COUNTERS_EXPECTATION(e1d,e2d,e3d,cd) \ |
---|
3024 | that(e1_deleted).is_equal_to(e1d), \ |
---|
3025 | that(e2_deleted).is_equal_to(e2d), \ |
---|
3026 | that(e3_deleted).is_equal_to(e3d), \ |
---|
3027 | that(c_deleted).is_equal_to(cd) |
---|
3028 | |
---|
3029 | #define TEST_EXPECT_CHCB_COUNTERS(e1c,e2c,cc,csc,tam) do{ if (ta_mode & (tam)) TEST_EXPECTATION(all().of(CHCB_COUNTERS_EXPECTATION(e1c,e2c,cc,csc))); }while(0) |
---|
3030 | #define TEST_EXPECT_CHCB___WANTED(e1c,e2c,cc,csc,tam) do{ if (ta_mode & (tam)) TEST_EXPECTATION__WANTED(all().of(CHCB_COUNTERS_EXPECTATION(e1c,e2c,cc,csc))); }while(0) |
---|
3031 | |
---|
3032 | #define TEST_EXPECT_DLCB_COUNTERS(e1d,e2d,e3d,cd,tam) do{ if (ta_mode & (tam)) TEST_EXPECTATION(all().of(DLCB_COUNTERS_EXPECTATION(e1d,e2d,e3d,cd))); }while(0) |
---|
3033 | #define TEST_EXPECT_DLCB___WANTED(e1d,e2d,e3d,cd,tam) do{ if (ta_mode & (tam)) TEST_EXPECTATION__WANTED(all().of(DLCB_COUNTERS_EXPECTATION(e1d,e2d,e3d,cd))); }while(0) |
---|
3034 | |
---|
3035 | #define TEST_EXPECT_COUNTER(tam,cnt,expected) do{ if (ta_mode & (tam)) TEST_EXPECT_EQUAL(cnt, expected); }while(0) |
---|
3036 | #define TEST_EXPECT_COUNTER__BROKEN(tam,cnt,expected,got) do{ if (ta_mode & (tam)) TEST_EXPECT_EQUAL__BROKEN(cnt, expected, got); }while(0) |
---|
3037 | |
---|
3038 | #define RESET_CHCB_COUNTERS() do{ e1_changed = e2_changed = c_changed = c_son_created = 0; }while(0) |
---|
3039 | #define RESET_DLCB_COUNTERS() do{ e1_deleted = e2_deleted = e3_deleted = c_deleted = 0; }while(0) |
---|
3040 | #define RESET_ALL_CB_COUNTERS() do{ RESET_CHCB_COUNTERS(); RESET_DLCB_COUNTERS(); }while(0) |
---|
3041 | |
---|
3042 | // install some DB callbacks |
---|
3043 | { |
---|
3044 | GB_transaction ta(gb_main); |
---|
3045 | GB_add_callback(gbe1, GB_CB_CHANGED, makeDatabaseCallback(test_count_cb, &e1_changed)); |
---|
3046 | GB_add_callback(gbe2, GB_CB_CHANGED, makeDatabaseCallback(test_count_cb, &e2_changed)); |
---|
3047 | GB_add_callback(gbc, GB_CB_CHANGED, makeDatabaseCallback(test_count_cb, &c_changed)); |
---|
3048 | GB_add_callback(gbc, GB_CB_SON_CREATED, makeDatabaseCallback(test_count_cb, &c_son_created)); |
---|
3049 | } |
---|
3050 | |
---|
3051 | // check callbacks were not called yet |
---|
3052 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, BOTH_TA_MODES); |
---|
3053 | |
---|
3054 | // trigger callbacks |
---|
3055 | { |
---|
3056 | GB_transaction ta(gb_main); |
---|
3057 | |
---|
3058 | error = GB_write_string(gbe1, "hi"); TEST_EXPECT_NO_ERROR(error); |
---|
3059 | error = GB_write_int(gbe2, 666); TEST_EXPECT_NO_ERROR(error); |
---|
3060 | |
---|
3061 | TEST_EXPECT_CHCB_COUNTERS(1, 1, 1, 0, NO_TA); // callbacks triggered instantly in NO_TA mode |
---|
3062 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, WITH_TA); // callbacks delayed until transaction is committed |
---|
3063 | |
---|
3064 | } // [Note: callbacks happen here in ta_mode] |
---|
3065 | |
---|
3066 | // test that GB_CB_SON_CREATED is not triggered here: |
---|
3067 | TEST_EXPECT_CHCB_COUNTERS(1, 1, 1, 0, NO_TA); |
---|
3068 | TEST_EXPECT_CHCB_COUNTERS(1, 1, 1, 0, WITH_TA); |
---|
3069 | |
---|
3070 | // really create a son |
---|
3071 | RESET_CHCB_COUNTERS(); |
---|
3072 | { |
---|
3073 | GB_transaction ta(gb_main); |
---|
3074 | gbe3 = GB_create(gbc, "e3", GB_STRING); |
---|
3075 | } |
---|
3076 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, NO_TA); // broken |
---|
3077 | TEST_EXPECT_CHCB___WANTED(0, 0, 1, 1, NO_TA); |
---|
3078 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 1, 1, WITH_TA); |
---|
3079 | |
---|
3080 | // change that son |
---|
3081 | RESET_CHCB_COUNTERS(); |
---|
3082 | { |
---|
3083 | GB_transaction ta(gb_main); |
---|
3084 | error = GB_write_string(gbe3, "bla"); TEST_EXPECT_NO_ERROR(error); |
---|
3085 | } |
---|
3086 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 1, 0, BOTH_TA_MODES); |
---|
3087 | |
---|
3088 | |
---|
3089 | // test delete callbacks |
---|
3090 | RESET_CHCB_COUNTERS(); |
---|
3091 | { |
---|
3092 | GB_transaction ta(gb_main); |
---|
3093 | |
---|
3094 | GB_add_callback(gbe1, GB_CB_DELETE, makeDatabaseCallback(test_count_cb, &e1_deleted)); |
---|
3095 | GB_add_callback(gbe2, GB_CB_DELETE, makeDatabaseCallback(test_count_cb, &e2_deleted)); |
---|
3096 | GB_add_callback(gbe3, GB_CB_DELETE, makeDatabaseCallback(test_count_cb, &e3_deleted)); |
---|
3097 | GB_add_callback(gbc, GB_CB_DELETE, makeDatabaseCallback(test_count_cb, &c_deleted)); |
---|
3098 | } |
---|
3099 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, BOTH_TA_MODES); // adding callbacks does not trigger existing change-callbacks |
---|
3100 | { |
---|
3101 | GB_transaction ta(gb_main); |
---|
3102 | |
---|
3103 | error = GB_delete(gbe3); TEST_EXPECT_NO_ERROR(error); |
---|
3104 | error = GB_delete(gbe2); TEST_EXPECT_NO_ERROR(error); |
---|
3105 | |
---|
3106 | TEST_EXPECT_DLCB_COUNTERS(0, 1, 1, 0, NO_TA); |
---|
3107 | TEST_EXPECT_DLCB_COUNTERS(0, 0, 0, 0, WITH_TA); |
---|
3108 | } |
---|
3109 | |
---|
3110 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 1, 0, WITH_TA); // container changed by deleting a son (gbe3); no longer triggers via GB_SON_CHANGED |
---|
3111 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, NO_TA); // change is not triggered in NO_TA mode (error?) |
---|
3112 | TEST_EXPECT_CHCB___WANTED(0, 0, 1, 1, NO_TA); |
---|
3113 | |
---|
3114 | TEST_EXPECT_DLCB_COUNTERS(0, 1, 1, 0, BOTH_TA_MODES); |
---|
3115 | |
---|
3116 | RESET_ALL_CB_COUNTERS(); |
---|
3117 | { |
---|
3118 | GB_transaction ta(gb_main); |
---|
3119 | error = GB_delete(gbc); TEST_EXPECT_NO_ERROR(error); // delete the container containing gbe1 and gbe3 (gbe3 alreay deleted) |
---|
3120 | } |
---|
3121 | TEST_EXPECT_CHCB_COUNTERS(0, 0, 0, 0, BOTH_TA_MODES); // deleting the container does not trigger any change callbacks |
---|
3122 | TEST_EXPECT_DLCB_COUNTERS(1, 0, 0, 1, BOTH_TA_MODES); // deleting the container does also trigger the delete callback for gbe1 |
---|
3123 | |
---|
3124 | // -------------------------------------------------------------------------------- |
---|
3125 | // document that a callback now can be removed while it is running |
---|
3126 | // (in NO_TA mode; always worked in WITH_TA mode) |
---|
3127 | { |
---|
3128 | GBDATA *gbe; |
---|
3129 | { |
---|
3130 | GB_transaction ta(gb_main); |
---|
3131 | gbe = GB_create(gb_main, "new_e1", GB_INT); // recreate |
---|
3132 | GB_add_callback(gbe, GB_CB_CHANGED, makeDatabaseCallback(remove_self_cb)); |
---|
3133 | } |
---|
3134 | { GB_transaction ta(gb_main); GB_touch(gbe); } |
---|
3135 | } |
---|
3136 | |
---|
3137 | // test that a callback may remove and re-add itself |
---|
3138 | { |
---|
3139 | GBDATA *gbn1; |
---|
3140 | GBDATA *gbn2; |
---|
3141 | |
---|
3142 | int counter1 = 0; |
---|
3143 | int counter2 = 0; |
---|
3144 | |
---|
3145 | { |
---|
3146 | GB_transaction ta(gb_main); |
---|
3147 | gbn1 = GB_create(gb_main, "new_e1", GB_INT); |
---|
3148 | gbn2 = GB_create(gb_main, "new_e2", GB_INT); |
---|
3149 | GB_add_callback(gbn1, GB_CB_CHANGED, makeDatabaseCallback(re_add_self_cb, &counter1)); |
---|
3150 | } |
---|
3151 | |
---|
3152 | TEST_EXPECT_COUNTER(NO_TA, counter1, 0); // no callback triggered (trigger happens BEFORE call to GB_add_callback in NO_TA mode!) |
---|
3153 | TEST_EXPECT_COUNTER(WITH_TA, counter1, 1); // callback gets triggered by GB_create |
---|
3154 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter2, 0); |
---|
3155 | |
---|
3156 | counter1 = 0; counter2 = 0; |
---|
3157 | |
---|
3158 | // test no callback is triggered by just adding a callback |
---|
3159 | { |
---|
3160 | GB_transaction ta(gb_main); |
---|
3161 | GB_add_callback(gbn2, GB_CB_CHANGED, makeDatabaseCallback(re_add_self_cb, &counter2)); |
---|
3162 | } |
---|
3163 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter1, 0); |
---|
3164 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter2, 0); |
---|
3165 | |
---|
3166 | { GB_transaction ta(gb_main); GB_touch(gbn1); } |
---|
3167 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter1, 1); |
---|
3168 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter2, 0); |
---|
3169 | |
---|
3170 | { GB_transaction ta(gb_main); GB_touch(gbn2); } |
---|
3171 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter1, 1); |
---|
3172 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter2, 1); |
---|
3173 | |
---|
3174 | { GB_transaction ta(gb_main); } |
---|
3175 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter1, 1); |
---|
3176 | TEST_EXPECT_COUNTER(BOTH_TA_MODES, counter2, 1); |
---|
3177 | } |
---|
3178 | |
---|
3179 | GB_close(gb_main); |
---|
3180 | } |
---|
3181 | } |
---|
3182 | |
---|
3183 | // ----------------------- |
---|
3184 | // test callbacks |
---|
3185 | |
---|
3186 | struct calledWith { |
---|
3187 | GBDATA *gbd; |
---|
3188 | GB_CB_TYPE type; |
---|
3189 | int time_called; |
---|
3190 | |
---|
3191 | static int timer; |
---|
3192 | |
---|
3193 | calledWith(GBDATA *gbd_, GB_CB_TYPE type_) : gbd(gbd_), type(type_), time_called(++timer) {} |
---|
3194 | calledWith(const calledWith& other) : gbd(other.gbd), type(other.type), time_called(other.time_called) {} |
---|
3195 | DECLARE_ASSIGNMENT_OPERATOR(calledWith); |
---|
3196 | }; |
---|
3197 | |
---|
3198 | int calledWith::timer = 0; |
---|
3199 | |
---|
3200 | class callback_trace { |
---|
3201 | typedef std::list<calledWith> calledList; |
---|
3202 | typedef calledList::iterator calledIter; |
---|
3203 | |
---|
3204 | calledList called; |
---|
3205 | |
---|
3206 | calledIter find(GBDATA *gbd) { |
---|
3207 | calledIter c = called.begin(); |
---|
3208 | while (c != called.end()) { |
---|
3209 | if (c->gbd == gbd) break; |
---|
3210 | ++c; |
---|
3211 | } |
---|
3212 | return c; |
---|
3213 | } |
---|
3214 | calledIter find(GB_CB_TYPE exp_type) { |
---|
3215 | calledIter c = called.begin(); |
---|
3216 | while (c != called.end()) { |
---|
3217 | if (c->type&exp_type) break; |
---|
3218 | ++c; |
---|
3219 | } |
---|
3220 | return c; |
---|
3221 | } |
---|
3222 | calledIter find(GBDATA *gbd, GB_CB_TYPE exp_type) { |
---|
3223 | calledIter c = called.begin(); |
---|
3224 | while (c != called.end()) { |
---|
3225 | if (c->gbd == gbd && (c->type&exp_type)) break; |
---|
3226 | ++c; |
---|
3227 | } |
---|
3228 | return c; |
---|
3229 | } |
---|
3230 | |
---|
3231 | bool removed(calledIter c) { |
---|
3232 | if (c == called.end()) return false; |
---|
3233 | called.erase(c); |
---|
3234 | return true; |
---|
3235 | } |
---|
3236 | |
---|
3237 | public: |
---|
3238 | callback_trace() { called.clear(); } |
---|
3239 | |
---|
3240 | void set_called_by(GBDATA *gbd, GB_CB_TYPE type) { called.push_back(calledWith(gbd, type)); } |
---|
3241 | |
---|
3242 | bool was_called_by(GBDATA *gbd) { return removed(find(gbd)); } |
---|
3243 | bool was_called_by(GB_CB_TYPE exp_type) { return removed(find(exp_type)); } |
---|
3244 | bool was_called_by(GBDATA *gbd, GB_CB_TYPE exp_type) { return removed(find(gbd, exp_type)); } |
---|
3245 | |
---|
3246 | int call_time(GBDATA *gbd, GB_CB_TYPE exp_type) { |
---|
3247 | calledIter found = find(gbd, exp_type); |
---|
3248 | if (found == called.end()) return -1; |
---|
3249 | |
---|
3250 | int t = found->time_called; |
---|
3251 | removed(found); |
---|
3252 | return t; |
---|
3253 | } |
---|
3254 | |
---|
3255 | bool was_not_called() const { return called.empty(); } |
---|
3256 | bool was_called() const { return !was_not_called(); } |
---|
3257 | }; |
---|
3258 | |
---|
3259 | static void some_cb(GBDATA *gbd, callback_trace *trace, GB_CB_TYPE cbtype) { |
---|
3260 | trace->set_called_by(gbd, cbtype); |
---|
3261 | } |
---|
3262 | |
---|
3263 | #define TRACESTRUCT(ELEM,FLAVOR) trace_##ELEM##_##FLAVOR |
---|
3264 | #define HIERARCHY_TRACESTRUCT(ELEM,FLAVOR) traceHier_##ELEM##_##FLAVOR |
---|
3265 | |
---|
3266 | #define ADD_CHANGED_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_callback(elem, GB_CB_CHANGED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,changed)))) |
---|
3267 | #define ADD_DELETED_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_callback(elem, GB_CB_DELETE, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,deleted)))) |
---|
3268 | #define ADD_NWCHILD_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_callback(elem, GB_CB_SON_CREATED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,newchild)))) |
---|
3269 | |
---|
3270 | #define ADD_CHANGED_HIERARCHY_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_hierarchy_callback(elem, GB_CB_CHANGED, makeDatabaseCallback(some_cb, &HIERARCHY_TRACESTRUCT(elem,changed)))) |
---|
3271 | #define ADD_DELETED_HIERARCHY_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_hierarchy_callback(elem, GB_CB_DELETE, makeDatabaseCallback(some_cb, &HIERARCHY_TRACESTRUCT(elem,deleted)))) |
---|
3272 | #define ADD_NWCHILD_HIERARCHY_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_add_hierarchy_callback(elem, GB_CB_SON_CREATED, makeDatabaseCallback(some_cb, &HIERARCHY_TRACESTRUCT(elem,newchild)))) |
---|
3273 | |
---|
3274 | #define ENSURE_CHANGED_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_ensure_callback(elem, GB_CB_CHANGED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,changed)))) |
---|
3275 | #define ENSURE_DELETED_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_ensure_callback(elem, GB_CB_DELETE, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,deleted)))) |
---|
3276 | #define ENSURE_NWCHILD_CALLBACK(elem) TEST_EXPECT_NO_ERROR(GB_ensure_callback(elem, GB_CB_SON_CREATED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,newchild)))) |
---|
3277 | |
---|
3278 | #define REMOVE_CHANGED_CALLBACK(elem) GB_remove_callback(elem, GB_CB_CHANGED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,changed))) |
---|
3279 | #define REMOVE_DELETED_CALLBACK(elem) GB_remove_callback(elem, GB_CB_DELETE, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,deleted))) |
---|
3280 | #define REMOVE_NWCHILD_CALLBACK(elem) GB_remove_callback(elem, GB_CB_SON_CREATED, makeDatabaseCallback(some_cb, &TRACESTRUCT(elem,newchild))) |
---|
3281 | |
---|
3282 | #define INIT_CHANGED_CALLBACK(elem) callback_trace TRACESTRUCT(elem,changed); ADD_CHANGED_CALLBACK(elem) |
---|
3283 | #define INIT_DELETED_CALLBACK(elem) callback_trace TRACESTRUCT(elem,deleted); ADD_DELETED_CALLBACK(elem) |
---|
3284 | #define INIT_NWCHILD_CALLBACK(elem) callback_trace TRACESTRUCT(elem,newchild); ADD_NWCHILD_CALLBACK(elem) |
---|
3285 | |
---|
3286 | #define INIT_CHANGED_HIERARCHY_CALLBACK(elem) callback_trace HIERARCHY_TRACESTRUCT(elem,changed); ADD_CHANGED_HIERARCHY_CALLBACK(elem) |
---|
3287 | #define INIT_DELETED_HIERARCHY_CALLBACK(elem) callback_trace HIERARCHY_TRACESTRUCT(elem,deleted); ADD_DELETED_HIERARCHY_CALLBACK(elem) |
---|
3288 | #define INIT_NWCHILD_HIERARCHY_CALLBACK(elem) callback_trace HIERARCHY_TRACESTRUCT(elem,newchild); ADD_NWCHILD_HIERARCHY_CALLBACK(elem) |
---|
3289 | |
---|
3290 | #define ENSURE_ENTRY_CALLBACKS(entry) ENSURE_CHANGED_CALLBACK(entry); ENSURE_DELETED_CALLBACK(entry) |
---|
3291 | #define ENSURE_CONTAINER_CALLBACKS(cont) ENSURE_CHANGED_CALLBACK(cont); ENSURE_NWCHILD_CALLBACK(cont); ENSURE_DELETED_CALLBACK(cont) |
---|
3292 | |
---|
3293 | #define REMOVE_ENTRY_CALLBACKS(entry) REMOVE_CHANGED_CALLBACK(entry); REMOVE_DELETED_CALLBACK(entry) |
---|
3294 | #define REMOVE_CONTAINER_CALLBACKS(cont) REMOVE_CHANGED_CALLBACK(cont); REMOVE_NWCHILD_CALLBACK(cont); REMOVE_DELETED_CALLBACK(cont) |
---|
3295 | |
---|
3296 | #define INIT_ENTRY_CALLBACKS(entry) INIT_CHANGED_CALLBACK(entry); INIT_DELETED_CALLBACK(entry) |
---|
3297 | #define INIT_CONTAINER_CALLBACKS(cont) INIT_CHANGED_CALLBACK(cont); INIT_NWCHILD_CALLBACK(cont); INIT_DELETED_CALLBACK(cont) |
---|
3298 | |
---|
3299 | #define TRIGGER_CHANGE(gbd) do { \ |
---|
3300 | GB_initial_transaction ta(gb_main); \ |
---|
3301 | if (ta.ok()) GB_touch(gbd); \ |
---|
3302 | TEST_EXPECT_NO_ERROR(ta.close(NULL)); \ |
---|
3303 | } while(0) |
---|
3304 | |
---|
3305 | #define TRIGGER_2_CHANGES(gbd1, gbd2) do { \ |
---|
3306 | GB_initial_transaction ta(gb_main); \ |
---|
3307 | if (ta.ok()) { \ |
---|
3308 | GB_touch(gbd1); \ |
---|
3309 | GB_touch(gbd2); \ |
---|
3310 | } \ |
---|
3311 | TEST_EXPECT_NO_ERROR(ta.close(NULL)); \ |
---|
3312 | } while(0) |
---|
3313 | |
---|
3314 | #define TRIGGER_DELETE(gbd) do { \ |
---|
3315 | GB_initial_transaction ta(gb_main); \ |
---|
3316 | GB_ERROR error = NULL; \ |
---|
3317 | if (ta.ok()) error = GB_delete(gbd); \ |
---|
3318 | TEST_EXPECT_NO_ERROR(ta.close(error)); \ |
---|
3319 | } while(0) |
---|
3320 | |
---|
3321 | #define TEST_EXPECT_NO_CALLBACK_TRIGGERED() \ |
---|
3322 | TEST_EXPECT(trace_top_deleted.was_not_called()); \ |
---|
3323 | TEST_EXPECT(trace_son_deleted.was_not_called()); \ |
---|
3324 | TEST_EXPECT(trace_grandson_deleted.was_not_called()); \ |
---|
3325 | TEST_EXPECT(trace_ograndson_deleted.was_not_called()); \ |
---|
3326 | TEST_EXPECT(trace_cont_top_deleted.was_not_called()); \ |
---|
3327 | TEST_EXPECT(trace_cont_son_deleted.was_not_called()); \ |
---|
3328 | TEST_EXPECT(trace_top_changed.was_not_called()); \ |
---|
3329 | TEST_EXPECT(trace_son_changed.was_not_called()); \ |
---|
3330 | TEST_EXPECT(trace_grandson_changed.was_not_called()); \ |
---|
3331 | TEST_EXPECT(trace_ograndson_changed.was_not_called()); \ |
---|
3332 | TEST_EXPECT(trace_cont_top_changed.was_not_called()); \ |
---|
3333 | TEST_EXPECT(trace_cont_son_changed.was_not_called()); \ |
---|
3334 | TEST_EXPECT(trace_cont_top_newchild.was_not_called()); \ |
---|
3335 | TEST_EXPECT(trace_cont_son_newchild.was_not_called()); \ |
---|
3336 | |
---|
3337 | |
---|
3338 | #define TEST_EXPECT_CB_TRIGGERED(TRACE,GBD,TYPE) TEST_EXPECT(TRACE.was_called_by(GBD, TYPE)) |
---|
3339 | #define TEST_EXPECT_CB_TRIGGERED_AT(TRACE,GBD,TYPE,TIME) TEST_EXPECT_EQUAL(TRACE.call_time(GBD, TYPE), TIME) |
---|
3340 | |
---|
3341 | #define TEST_EXPECT_CHANGE_TRIGGERED(TRACE,GBD) TEST_EXPECT_CB_TRIGGERED(TRACE, GBD, GB_CB_CHANGED) |
---|
3342 | #define TEST_EXPECT_DELETE_TRIGGERED(TRACE,GBD) TEST_EXPECT_CB_TRIGGERED(TRACE, GBD, GB_CB_DELETE) |
---|
3343 | #define TEST_EXPECT_NCHILD_TRIGGERED(TRACE,GBD) TEST_EXPECT_CB_TRIGGERED(TRACE, GBD, GB_CB_SON_CREATED) |
---|
3344 | |
---|
3345 | #define TEST_EXPECT_CHANGE_TRIGGERED_AT(TRACE,GBD,TIME) TEST_EXPECT_CB_TRIGGERED_AT(TRACE, GBD, GB_CB_CHANGED, TIME) |
---|
3346 | #define TEST_EXPECT_DELETE_TRIGGERED_AT(TRACE,GBD,TIME) TEST_EXPECT_CB_TRIGGERED_AT(TRACE, GBD, GB_CB_DELETE, TIME) |
---|
3347 | |
---|
3348 | void TEST_db_callbacks() { |
---|
3349 | GB_shell shell; |
---|
3350 | GBDATA *gb_main = GB_open("new.arb", "c"); |
---|
3351 | |
---|
3352 | // create some data |
---|
3353 | GB_begin_transaction(gb_main); |
---|
3354 | |
---|
3355 | GBDATA *cont_top = GB_create_container(gb_main, "cont_top"); TEST_REJECT_NULL(cont_top); |
---|
3356 | GBDATA *cont_son = GB_create_container(cont_top, "cont_son"); TEST_REJECT_NULL(cont_son); |
---|
3357 | |
---|
3358 | GBDATA *top = GB_create(gb_main, "top", GB_STRING); TEST_REJECT_NULL(top); |
---|
3359 | GBDATA *son = GB_create(cont_top, "son", GB_INT); TEST_REJECT_NULL(son); |
---|
3360 | GBDATA *grandson = GB_create(cont_son, "grandson", GB_STRING); TEST_REJECT_NULL(grandson); |
---|
3361 | GBDATA *ograndson = GB_create(cont_son, "ograndson", GB_STRING); TEST_REJECT_NULL(ograndson); |
---|
3362 | |
---|
3363 | GB_commit_transaction(gb_main); |
---|
3364 | |
---|
3365 | // install callbacks |
---|
3366 | GB_begin_transaction(gb_main); |
---|
3367 | INIT_CONTAINER_CALLBACKS(cont_top); |
---|
3368 | INIT_CONTAINER_CALLBACKS(cont_son); |
---|
3369 | INIT_ENTRY_CALLBACKS(top); |
---|
3370 | INIT_ENTRY_CALLBACKS(son); |
---|
3371 | INIT_ENTRY_CALLBACKS(grandson); |
---|
3372 | INIT_ENTRY_CALLBACKS(ograndson); |
---|
3373 | GB_commit_transaction(gb_main); |
---|
3374 | |
---|
3375 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3376 | |
---|
3377 | // trigger change callbacks via change |
---|
3378 | GB_begin_transaction(gb_main); |
---|
3379 | GB_write_string(top, "hello world"); |
---|
3380 | GB_commit_transaction(gb_main); |
---|
3381 | TEST_EXPECT_CHANGE_TRIGGERED(trace_top_changed, top); |
---|
3382 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3383 | |
---|
3384 | GB_begin_transaction(gb_main); |
---|
3385 | GB_write_string(top, "hello world"); // no change |
---|
3386 | GB_commit_transaction(gb_main); |
---|
3387 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3388 | |
---|
3389 | #if 0 |
---|
3390 | // code is wrong (cannot set terminal entry to "marked") |
---|
3391 | GB_begin_transaction(gb_main); |
---|
3392 | GB_write_flag(son, 1); // only change "mark" |
---|
3393 | GB_commit_transaction(gb_main); |
---|
3394 | TEST_EXPECT_CHANGE_TRIGGERED(trace_son_changed, son); |
---|
3395 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3396 | TEST_EXPECT_TRIGGER__UNWANTED(trace_cont_top_newchild); // @@@ modifying son should not trigger newchild callback |
---|
3397 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3398 | #else |
---|
3399 | // @@@ add test code similar to wrong section above |
---|
3400 | #endif |
---|
3401 | |
---|
3402 | GB_begin_transaction(gb_main); |
---|
3403 | GB_touch(grandson); |
---|
3404 | GB_commit_transaction(gb_main); |
---|
3405 | TEST_EXPECT_CHANGE_TRIGGERED(trace_grandson_changed, grandson); |
---|
3406 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_son_changed, cont_son); |
---|
3407 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3408 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3409 | |
---|
3410 | // trigger change- and soncreate-callbacks via create |
---|
3411 | |
---|
3412 | GB_begin_transaction(gb_main); |
---|
3413 | GBDATA *son2 = GB_create(cont_top, "son2", GB_INT); TEST_REJECT_NULL(son2); |
---|
3414 | GB_commit_transaction(gb_main); |
---|
3415 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3416 | TEST_EXPECT_NCHILD_TRIGGERED(trace_cont_top_newchild, cont_top); |
---|
3417 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3418 | |
---|
3419 | GB_begin_transaction(gb_main); |
---|
3420 | GBDATA *grandson2 = GB_create(cont_son, "grandson2", GB_STRING); TEST_REJECT_NULL(grandson2); |
---|
3421 | GB_commit_transaction(gb_main); |
---|
3422 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_son_changed, cont_son); |
---|
3423 | TEST_EXPECT_NCHILD_TRIGGERED(trace_cont_son_newchild, cont_son); |
---|
3424 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3425 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3426 | |
---|
3427 | // trigger callbacks via delete |
---|
3428 | |
---|
3429 | TRIGGER_DELETE(son2); |
---|
3430 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3431 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3432 | |
---|
3433 | TRIGGER_DELETE(grandson2); |
---|
3434 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3435 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_son_changed, cont_son); |
---|
3436 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3437 | |
---|
3438 | TEST_EXPECT_NO_ERROR(GB_request_undo_type(gb_main, GB_UNDO_UNDO)); |
---|
3439 | |
---|
3440 | TRIGGER_DELETE(top); |
---|
3441 | TEST_EXPECT_DELETE_TRIGGERED(trace_top_deleted, top); |
---|
3442 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3443 | |
---|
3444 | TRIGGER_DELETE(grandson); |
---|
3445 | TEST_EXPECT_DELETE_TRIGGERED(trace_grandson_deleted, grandson); |
---|
3446 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3447 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_son_changed, cont_son); |
---|
3448 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3449 | |
---|
3450 | TRIGGER_DELETE(cont_son); |
---|
3451 | TEST_EXPECT_DELETE_TRIGGERED(trace_ograndson_deleted, ograndson); |
---|
3452 | TEST_EXPECT_DELETE_TRIGGERED(trace_cont_son_deleted, cont_son); |
---|
3453 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3454 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3455 | |
---|
3456 | // trigger callbacks by undoing last 3 delete transactions |
---|
3457 | |
---|
3458 | TEST_EXPECT_NO_ERROR(GB_undo(gb_main, GB_UNDO_UNDO)); // undo delete of cont_son |
---|
3459 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3460 | TEST_EXPECT_NCHILD_TRIGGERED(trace_cont_top_newchild, cont_top); |
---|
3461 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3462 | |
---|
3463 | TEST_EXPECT_NO_ERROR(GB_undo(gb_main, GB_UNDO_UNDO)); // undo delete of grandson |
---|
3464 | // cont_son callbacks are not triggered (they are not restored by undo) |
---|
3465 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3466 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3467 | |
---|
3468 | TEST_EXPECT_NO_ERROR(GB_undo(gb_main, GB_UNDO_UNDO)); // undo delete of top |
---|
3469 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3470 | |
---|
3471 | // reinstall callbacks that were removed by deletes |
---|
3472 | |
---|
3473 | GB_begin_transaction(gb_main); |
---|
3474 | ENSURE_CONTAINER_CALLBACKS(cont_top); |
---|
3475 | ENSURE_CONTAINER_CALLBACKS(cont_son); |
---|
3476 | ENSURE_ENTRY_CALLBACKS(top); |
---|
3477 | ENSURE_ENTRY_CALLBACKS(son); |
---|
3478 | ENSURE_ENTRY_CALLBACKS(grandson); |
---|
3479 | GB_commit_transaction(gb_main); |
---|
3480 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3481 | |
---|
3482 | // trigger callbacks which will be removed |
---|
3483 | |
---|
3484 | TRIGGER_CHANGE(son); |
---|
3485 | TEST_EXPECT_CHANGE_TRIGGERED(trace_son_changed, son); |
---|
3486 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3487 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3488 | |
---|
3489 | GB_begin_transaction(gb_main); |
---|
3490 | GBDATA *son3 = GB_create(cont_top, "son3", GB_INT); TEST_REJECT_NULL(son3); |
---|
3491 | GB_commit_transaction(gb_main); |
---|
3492 | TEST_EXPECT_CHANGE_TRIGGERED(trace_cont_top_changed, cont_top); |
---|
3493 | TEST_EXPECT_NCHILD_TRIGGERED(trace_cont_top_newchild, cont_top); |
---|
3494 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3495 | |
---|
3496 | // test remove callback |
---|
3497 | |
---|
3498 | GB_begin_transaction(gb_main); |
---|
3499 | REMOVE_ENTRY_CALLBACKS(son); |
---|
3500 | REMOVE_CONTAINER_CALLBACKS(cont_top); |
---|
3501 | GB_commit_transaction(gb_main); |
---|
3502 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3503 | |
---|
3504 | // "trigger" removed callbacks |
---|
3505 | |
---|
3506 | TRIGGER_CHANGE(son); |
---|
3507 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3508 | |
---|
3509 | GB_begin_transaction(gb_main); |
---|
3510 | GBDATA *son4 = GB_create(cont_top, "son4", GB_INT); TEST_REJECT_NULL(son4); |
---|
3511 | GB_commit_transaction(gb_main); |
---|
3512 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3513 | |
---|
3514 | GB_close(gb_main); |
---|
3515 | } |
---|
3516 | |
---|
3517 | void TEST_POSTCOND_arbdb() { |
---|
3518 | GB_ERROR error = NULL; |
---|
3519 | if (GB_have_error()) { |
---|
3520 | error = GB_await_error(); // clears the error (to make further tests succeed) |
---|
3521 | } |
---|
3522 | |
---|
3523 | bool unclosed_GB_shell = closed_open_shell_for_unit_tests(); |
---|
3524 | |
---|
3525 | TEST_REJECT(error); // your test finished with an exported error |
---|
3526 | TEST_REJECT(unclosed_GB_shell); // your test finished w/o destroying GB_shell |
---|
3527 | } |
---|
3528 | |
---|
3529 | #define TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED() \ |
---|
3530 | TEST_EXPECT(traceHier_anyGrandson_changed.was_not_called()); \ |
---|
3531 | TEST_EXPECT(traceHier_anyGrandson_deleted.was_not_called()); \ |
---|
3532 | TEST_EXPECT(traceHier_anySonContainer_newchild.was_not_called()) |
---|
3533 | |
---|
3534 | #undef TEST_EXPECT_NO_CALLBACK_TRIGGERED |
---|
3535 | |
---|
3536 | #define TEST_EXPECT_NO_CALLBACK_TRIGGERED() \ |
---|
3537 | TEST_EXPECT(trace_anotherGrandson_changed.was_not_called()); \ |
---|
3538 | TEST_EXPECT(trace_elimGrandson2_deleted.was_not_called()) |
---|
3539 | |
---|
3540 | void TEST_hierarchy_callbacks() { |
---|
3541 | GB_shell shell; |
---|
3542 | GBDATA *gb_main = GB_open("new.arb", "c"); |
---|
3543 | |
---|
3544 | // create some data |
---|
3545 | GB_begin_transaction(gb_main); |
---|
3546 | |
---|
3547 | GBDATA *cont_top1 = GB_create_container(gb_main, "cont_top"); TEST_REJECT_NULL(cont_top1); |
---|
3548 | GBDATA *cont_top2 = GB_create_container(gb_main, "cont_top"); TEST_REJECT_NULL(cont_top2); |
---|
3549 | |
---|
3550 | GBDATA *cont_son11 = GB_create_container(cont_top1, "cont_son"); TEST_REJECT_NULL(cont_son11); |
---|
3551 | GBDATA *cont_son21 = GB_create_container(cont_top2, "cont_son"); TEST_REJECT_NULL(cont_son21); |
---|
3552 | GBDATA *cont_son22 = GB_create_container(cont_top2, "cont_son"); TEST_REJECT_NULL(cont_son22); |
---|
3553 | |
---|
3554 | GBDATA *top1 = GB_create(gb_main, "top", GB_STRING); TEST_REJECT_NULL(top1); |
---|
3555 | GBDATA *top2 = GB_create(gb_main, "top", GB_STRING); TEST_REJECT_NULL(top2); |
---|
3556 | |
---|
3557 | GBDATA *son11 = GB_create(cont_top1, "son", GB_INT); TEST_REJECT_NULL(son11); |
---|
3558 | GBDATA *son12 = GB_create(cont_top1, "son", GB_INT); TEST_REJECT_NULL(son12); |
---|
3559 | GBDATA *son21 = GB_create(cont_top2, "son", GB_INT); TEST_REJECT_NULL(son21); |
---|
3560 | |
---|
3561 | GBDATA *grandson111 = GB_create(cont_son11, "grandson", GB_STRING); TEST_REJECT_NULL(grandson111); |
---|
3562 | GBDATA *grandson112 = GB_create(cont_son11, "grandson", GB_STRING); TEST_REJECT_NULL(grandson112); |
---|
3563 | GBDATA *grandson211 = GB_create(cont_son21, "grandson", GB_STRING); TEST_REJECT_NULL(grandson211); |
---|
3564 | GBDATA *grandson221 = GB_create(cont_son22, "grandson", GB_STRING); TEST_REJECT_NULL(grandson221); |
---|
3565 | GBDATA *grandson222 = GB_create(cont_son22, "grandson", GB_STRING); TEST_REJECT_NULL(grandson222); |
---|
3566 | |
---|
3567 | // create some entries at uncommon locations (compared to entries created above) |
---|
3568 | GBDATA *ctop_top = GB_create (cont_top2, "top", GB_STRING); TEST_REJECT_NULL(ctop_top); |
---|
3569 | GBDATA *top_son = GB_create (gb_main, "son", GB_INT); TEST_REJECT_NULL(top_son); |
---|
3570 | GBDATA *cson = GB_create_container(gb_main, "cont_son"); TEST_REJECT_NULL(cson); |
---|
3571 | GBDATA *cson_gs = GB_create (cson, "grandson", GB_STRING); TEST_REJECT_NULL(cson_gs); |
---|
3572 | |
---|
3573 | GB_commit_transaction(gb_main); |
---|
3574 | |
---|
3575 | // test gb_hierarchy_location |
---|
3576 | { |
---|
3577 | gb_hierarchy_location loc_top(top1); |
---|
3578 | gb_hierarchy_location loc_son(son11); |
---|
3579 | gb_hierarchy_location loc_grandson(grandson222); |
---|
3580 | |
---|
3581 | TEST_EXPECT(loc_top.matches(top1)); |
---|
3582 | TEST_EXPECT(loc_top.matches(top2)); |
---|
3583 | TEST_EXPECT(!loc_top.matches(cont_top1)); |
---|
3584 | TEST_EXPECT(!loc_top.matches(son12)); |
---|
3585 | TEST_EXPECT(!loc_top.matches(cont_son22)); |
---|
3586 | TEST_EXPECT(!loc_top.matches(ctop_top)); |
---|
3587 | |
---|
3588 | TEST_EXPECT(loc_son.matches(son11)); |
---|
3589 | TEST_EXPECT(loc_son.matches(son21)); |
---|
3590 | TEST_EXPECT(!loc_son.matches(top1)); |
---|
3591 | TEST_EXPECT(!loc_son.matches(grandson111)); |
---|
3592 | TEST_EXPECT(!loc_son.matches(cont_son22)); |
---|
3593 | TEST_EXPECT(!loc_son.matches(top_son)); |
---|
3594 | |
---|
3595 | TEST_EXPECT(loc_grandson.matches(grandson222)); |
---|
3596 | TEST_EXPECT(loc_grandson.matches(grandson111)); |
---|
3597 | TEST_EXPECT(!loc_grandson.matches(son11)); |
---|
3598 | TEST_EXPECT(!loc_grandson.matches(top1)); |
---|
3599 | TEST_EXPECT(!loc_grandson.matches(cont_son22)); |
---|
3600 | TEST_EXPECT(!loc_grandson.matches(cson_gs)); |
---|
3601 | |
---|
3602 | gb_hierarchy_location loc_ctop_top(ctop_top); |
---|
3603 | TEST_EXPECT(loc_ctop_top.matches(ctop_top)); |
---|
3604 | TEST_EXPECT(!loc_ctop_top.matches(top1)); |
---|
3605 | |
---|
3606 | gb_hierarchy_location loc_top_son(top_son); |
---|
3607 | TEST_EXPECT(loc_top_son.matches(top_son)); |
---|
3608 | TEST_EXPECT(!loc_top_son.matches(son11)); |
---|
3609 | |
---|
3610 | gb_hierarchy_location loc_gs(cson_gs); |
---|
3611 | TEST_EXPECT(loc_gs.matches(cson_gs)); |
---|
3612 | TEST_EXPECT(!loc_gs.matches(grandson211)); |
---|
3613 | |
---|
3614 | gb_hierarchy_location loc_root(gb_main); |
---|
3615 | TEST_EXPECT(loc_root.matches(gb_main)); |
---|
3616 | TEST_EXPECT(!loc_root.matches(cont_top1)); |
---|
3617 | TEST_EXPECT(!loc_root.matches(cont_son11)); |
---|
3618 | TEST_EXPECT(!loc_root.matches(top1)); |
---|
3619 | TEST_EXPECT(!loc_root.matches(son11)); |
---|
3620 | TEST_EXPECT(!loc_root.matches(grandson211)); |
---|
3621 | } |
---|
3622 | |
---|
3623 | |
---|
3624 | // instanciate callback_trace data and install hierarchy callbacks |
---|
3625 | GBDATA *anySon = son11; |
---|
3626 | |
---|
3627 | GBDATA *anySonContainer = cont_son11; |
---|
3628 | GBDATA *anotherSonContainer = cont_son22; |
---|
3629 | |
---|
3630 | GBDATA *anyGrandson = grandson221; |
---|
3631 | GBDATA *anotherGrandson = grandson112; |
---|
3632 | GBDATA *elimGrandson = grandson222; |
---|
3633 | GBDATA *elimGrandson2 = grandson111; |
---|
3634 | GBDATA *newGrandson = NULL; |
---|
3635 | |
---|
3636 | INIT_CHANGED_HIERARCHY_CALLBACK(anyGrandson); |
---|
3637 | INIT_DELETED_HIERARCHY_CALLBACK(anyGrandson); |
---|
3638 | INIT_NWCHILD_HIERARCHY_CALLBACK(anySonContainer); |
---|
3639 | |
---|
3640 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
---|
3641 | |
---|
3642 | // trigger change-callback using same DB entry |
---|
3643 | TRIGGER_CHANGE(anyGrandson); |
---|
3644 | TEST_EXPECT_CHANGE_TRIGGERED(traceHier_anyGrandson_changed, anyGrandson); |
---|
3645 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
---|
3646 | |
---|
3647 | // trigger change-callback using another DB entry (same hierarchy) |
---|
3648 | TRIGGER_CHANGE(anotherGrandson); |
---|
3649 | TEST_EXPECT_CHANGE_TRIGGERED(traceHier_anyGrandson_changed, anotherGrandson); |
---|
3650 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
---|
3651 | |
---|
3652 | // check nothing is triggered by an element at different hierarchy |
---|
3653 | TRIGGER_CHANGE(anySon); |
---|
3654 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
---|
3655 | |
---|
3656 | // trigger change-callback using both DB entries (in two TAs) |
---|
3657 | TRIGGER_CHANGE(anyGrandson); |
---|
3658 | TRIGGER_CHANGE(anotherGrandson); |
---|
3659 | TEST_EXPECT_CHANGE_TRIGGERED(traceHier_anyGrandson_changed, anyGrandson); |
---|
3660 | TEST_EXPECT_CHANGE_TRIGGERED(traceHier_anyGrandson_changed, anotherGrandson); |
---|
3661 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
---|
3662 | |
---|
3663 | // trigger change-callback using both DB entries (in one TA) |
---|
3664 | TRIGGER_2_CHANGES(anyGrandson, anotherGrandson); |
---|
3665 | TEST_EXPECT_CHANGE_TRIGGERED(traceHier_anyGrandson_changed, anyGrandson); |
---|
3666 | TEST_EXPECT_CHANGE_TRIGGERED(traceHier_anyGrandson_changed, anotherGrandson); |
---|
3667 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
---|
3668 | |
---|
3669 | // trigger son-created-callback |
---|
3670 | { |
---|
3671 | GB_initial_transaction ta(gb_main); |
---|
3672 | if (ta.ok()) { |
---|
3673 | GBDATA *someson = GB_create(anySonContainer, "someson", GB_STRING); TEST_REJECT_NULL(someson); |
---|
3674 | } |
---|
3675 | TEST_EXPECT_NO_ERROR(ta.close(NULL)); |
---|
3676 | } |
---|
3677 | TEST_EXPECT_NCHILD_TRIGGERED(traceHier_anySonContainer_newchild, anySonContainer); |
---|
3678 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
---|
3679 | |
---|
3680 | // trigger 2 son-created-callbacks (for 2 containers) and one change-callback (for a newly created son) |
---|
3681 | { |
---|
3682 | GB_initial_transaction ta(gb_main); |
---|
3683 | if (ta.ok()) { |
---|
3684 | newGrandson = GB_create(anotherSonContainer, "grandson", GB_STRING); TEST_REJECT_NULL(newGrandson); |
---|
3685 | GBDATA *someson = GB_create(anySonContainer, "someson", GB_STRING); TEST_REJECT_NULL(someson); |
---|
3686 | } |
---|
3687 | TEST_EXPECT_NO_ERROR(ta.close(NULL)); |
---|
3688 | } |
---|
3689 | TEST_EXPECT_CHANGE_TRIGGERED(traceHier_anyGrandson_changed, newGrandson); |
---|
3690 | TEST_EXPECT_NCHILD_TRIGGERED(traceHier_anySonContainer_newchild, anotherSonContainer); |
---|
3691 | TEST_EXPECT_NCHILD_TRIGGERED(traceHier_anySonContainer_newchild, anySonContainer); |
---|
3692 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
---|
3693 | |
---|
3694 | // trigger delete-callback |
---|
3695 | { |
---|
3696 | GB_initial_transaction ta(gb_main); |
---|
3697 | TEST_EXPECT_NO_ERROR(GB_delete(elimGrandson)); |
---|
3698 | TEST_EXPECT_NO_ERROR(ta.close(NULL)); |
---|
3699 | } |
---|
3700 | TEST_EXPECT_DELETE_TRIGGERED(traceHier_anyGrandson_deleted, elimGrandson); |
---|
3701 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
---|
3702 | |
---|
3703 | // bind normal (non-hierarchical) callbacks to entries which trigger hierarchical callbacks and .. |
---|
3704 | calledWith::timer = 0; |
---|
3705 | GB_begin_transaction(gb_main); |
---|
3706 | INIT_CHANGED_CALLBACK(anotherGrandson); |
---|
3707 | INIT_DELETED_CALLBACK(elimGrandson2); |
---|
3708 | GB_commit_transaction(gb_main); |
---|
3709 | |
---|
3710 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3711 | |
---|
3712 | { |
---|
3713 | GB_initial_transaction ta(gb_main); |
---|
3714 | if (ta.ok()) { |
---|
3715 | GB_touch(anotherGrandson); |
---|
3716 | GB_touch(elimGrandson2); |
---|
3717 | TEST_EXPECT_NO_ERROR(GB_delete(elimGrandson2)); |
---|
3718 | } |
---|
3719 | } |
---|
3720 | |
---|
3721 | // .. test call-order (delete before change, hierarchical before normal): |
---|
3722 | TEST_EXPECT_DELETE_TRIGGERED_AT(traceHier_anyGrandson_deleted, elimGrandson2, 1); |
---|
3723 | TEST_EXPECT_DELETE_TRIGGERED_AT(trace_elimGrandson2_deleted, elimGrandson2, 2); |
---|
3724 | TEST_EXPECT_CHANGE_TRIGGERED_AT(traceHier_anyGrandson_changed, anotherGrandson, 3); |
---|
3725 | TEST_EXPECT_CHANGE_TRIGGERED_AT(trace_anotherGrandson_changed, anotherGrandson, 4); |
---|
3726 | |
---|
3727 | TEST_EXPECT_NO_HIERARCHY_CALLBACK_TRIGGERED(); |
---|
3728 | TEST_EXPECT_NO_CALLBACK_TRIGGERED(); |
---|
3729 | |
---|
3730 | // cleanup |
---|
3731 | GB_close(gb_main); |
---|
3732 | } |
---|
3733 | |
---|
3734 | #endif // UNIT_TESTS |
---|
3735 | |
---|
3736 | |
---|