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 | #include "gb_key.h" |
---|
12 | #include "gb_comm.h" |
---|
13 | #include "gb_compress.h" |
---|
14 | #include "gb_localdata.h" |
---|
15 | #include "gb_ta.h" |
---|
16 | #include "gb_ts.h" |
---|
17 | #include "gb_index.h" |
---|
18 | |
---|
19 | #include <arb_misc.h> |
---|
20 | |
---|
21 | #include <rpc/types.h> |
---|
22 | #include <rpc/xdr.h> |
---|
23 | |
---|
24 | #include <set> |
---|
25 | |
---|
26 | gb_local_data *gb_local = NULp; |
---|
27 | |
---|
28 | #define INIT_TYPE_NAME(t) GB_TYPES_name[t] = #t |
---|
29 | |
---|
30 | static const char *GB_TYPES_2_name(GB_TYPES type) { |
---|
31 | static const char *GB_TYPES_name[GB_TYPE_MAX]; |
---|
32 | static bool initialized = false; |
---|
33 | |
---|
34 | if (!initialized) { |
---|
35 | memset(GB_TYPES_name, 0, sizeof(GB_TYPES_name)); |
---|
36 | INIT_TYPE_NAME(GB_NONE); |
---|
37 | INIT_TYPE_NAME(GB_BIT); |
---|
38 | INIT_TYPE_NAME(GB_BYTE); |
---|
39 | INIT_TYPE_NAME(GB_INT); |
---|
40 | INIT_TYPE_NAME(GB_FLOAT); |
---|
41 | INIT_TYPE_NAME(GB_POINTER); |
---|
42 | INIT_TYPE_NAME(GB_BITS); |
---|
43 | INIT_TYPE_NAME(GB_BYTES); |
---|
44 | INIT_TYPE_NAME(GB_INTS); |
---|
45 | INIT_TYPE_NAME(GB_FLOATS); |
---|
46 | INIT_TYPE_NAME(GB_STRING); |
---|
47 | INIT_TYPE_NAME(GB_STRING_SHRT); |
---|
48 | INIT_TYPE_NAME(GB_DB); |
---|
49 | |
---|
50 | GB_TYPES_name[GB_OBSOLETE] = "GB_LINK (obsolete)"; |
---|
51 | |
---|
52 | initialized = true; |
---|
53 | } |
---|
54 | |
---|
55 | const char *name = NULp; |
---|
56 | if (type >= 0 && type<GB_TYPE_MAX) name = GB_TYPES_name[type]; |
---|
57 | if (!name) { |
---|
58 | static char *unknownType = NULp; |
---|
59 | freeset(unknownType, GBS_global_string_copy("<invalid-type=%i>", type)); |
---|
60 | name = unknownType; |
---|
61 | } |
---|
62 | return name; |
---|
63 | } |
---|
64 | |
---|
65 | const char *GB_get_type_name(GBDATA *gbd) { |
---|
66 | return GB_TYPES_2_name(gbd->type()); |
---|
67 | } |
---|
68 | |
---|
69 | inline GB_ERROR gb_transactable_type(GB_TYPES type, GBDATA *gbd) { |
---|
70 | GB_ERROR error = NULp; |
---|
71 | if (GB_MAIN(gbd)->get_transaction_level() == 0) { |
---|
72 | error = "No transaction running"; |
---|
73 | } |
---|
74 | else if (GB_ARRAY_FLAGS(gbd).changed == GB_DELETED) { |
---|
75 | error = "Entry has been deleted"; |
---|
76 | } |
---|
77 | else { |
---|
78 | GB_TYPES gb_type = gbd->type(); |
---|
79 | if (gb_type != type && (type != GB_STRING || gb_type != GB_OBSOLETE)) { |
---|
80 | char *rtype = ARB_strdup(GB_TYPES_2_name(type)); |
---|
81 | char *rgb_type = ARB_strdup(GB_TYPES_2_name(gb_type)); |
---|
82 | |
---|
83 | error = GBS_global_string("type mismatch (want='%s', got='%s') in '%s'", rtype, rgb_type, GB_get_db_path(gbd)); |
---|
84 | |
---|
85 | free(rgb_type); |
---|
86 | free(rtype); |
---|
87 | } |
---|
88 | } |
---|
89 | if (error) { |
---|
90 | GBK_dump_backtrace(stderr, error); // it's a bug: none of the above errors should ever happen |
---|
91 | gb_assert(0); |
---|
92 | } |
---|
93 | return error; |
---|
94 | } |
---|
95 | |
---|
96 | __ATTR__USERESULT static GB_ERROR gb_security_error(GBDATA *gbd) { |
---|
97 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
98 | const char *error = GBS_global_string("Protection: Attempt to change a level-%i-'%s'-entry,\n" |
---|
99 | "but your current security level is only %i", |
---|
100 | GB_GET_SECURITY_WRITE(gbd), |
---|
101 | GB_read_key_pntr(gbd), |
---|
102 | Main->security_level); |
---|
103 | #if defined(DEBUG) |
---|
104 | fprintf(stderr, "%s\n", error); |
---|
105 | #endif // DEBUG |
---|
106 | return error; |
---|
107 | } |
---|
108 | |
---|
109 | inline GB_ERROR gb_type_writeable_to(GB_TYPES type, GBDATA *gbd) { |
---|
110 | GB_ERROR error = gb_transactable_type(type, gbd); |
---|
111 | if (!error) { |
---|
112 | if (GB_GET_SECURITY_WRITE(gbd) > GB_MAIN(gbd)->security_level) { |
---|
113 | error = gb_security_error(gbd); |
---|
114 | } |
---|
115 | } |
---|
116 | return error; |
---|
117 | } |
---|
118 | inline GB_ERROR gb_type_readable_from(GB_TYPES type, GBDATA *gbd) { |
---|
119 | return gb_transactable_type(type, gbd); |
---|
120 | } |
---|
121 | |
---|
122 | inline GB_ERROR error_with_dbentry(const char *action, GBDATA *gbd, GB_ERROR error) { |
---|
123 | if (error) { |
---|
124 | char *error_copy = ARB_strdup(error); |
---|
125 | const char *path = GB_get_db_path(gbd); |
---|
126 | error = GBS_global_string("Can't %s '%s':\n%s", action, path, error_copy); |
---|
127 | free(error_copy); |
---|
128 | } |
---|
129 | return error; |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | #define RETURN_ERROR_IF_NOT_WRITEABLE_AS_TYPE(gbd,type) \ |
---|
134 | do { \ |
---|
135 | GB_ERROR error = gb_type_writeable_to(type, gbd); \ |
---|
136 | if (error) { \ |
---|
137 | return error_with_dbentry("write", gbd, error); \ |
---|
138 | } \ |
---|
139 | } while(0) |
---|
140 | |
---|
141 | #define EXPORT_ERROR_AND_RETURN_RES_IF_NOT_READABLE_AS_TYPE(gbd,res,type) \ |
---|
142 | do { \ |
---|
143 | GB_ERROR error = gb_type_readable_from(type, gbd); \ |
---|
144 | if (error) { \ |
---|
145 | error = error_with_dbentry("read", gbd, error); \ |
---|
146 | GB_export_error(error); \ |
---|
147 | return res; \ |
---|
148 | } \ |
---|
149 | } while(0) \ |
---|
150 | |
---|
151 | |
---|
152 | // @@@ replace GB_TEST_READ_NUM, GB_TEST_READ_PTR and GB_TEST_READ by new names |
---|
153 | |
---|
154 | #define GB_TEST_READ_NUM(gbd,type,ignored) EXPORT_ERROR_AND_RETURN_RES_IF_NOT_READABLE_AS_TYPE(gbd,0,type) |
---|
155 | #define GB_TEST_READ_PTR(gbd,type,ignored) EXPORT_ERROR_AND_RETURN_RES_IF_NOT_READABLE_AS_TYPE(gbd,NULp,type) |
---|
156 | #define GB_TEST_WRITE(gbd,type,ignored) RETURN_ERROR_IF_NOT_WRITEABLE_AS_TYPE(gbd, type) |
---|
157 | |
---|
158 | #define GB_TEST_NON_BUFFER(x, gerror) \ |
---|
159 | do { \ |
---|
160 | if (GB_is_in_buffer(x)) { \ |
---|
161 | GBK_terminatef("%s: you are not allowed to write any data, which you get by pntr", gerror); \ |
---|
162 | } \ |
---|
163 | } while (0) |
---|
164 | |
---|
165 | |
---|
166 | static GB_ERROR GB_safe_atof(const char *str, float *res) { |
---|
167 | GB_ERROR error = NULp; |
---|
168 | |
---|
169 | char *end; |
---|
170 | *res = strtof(str, &end); |
---|
171 | |
---|
172 | if (end == str || end[0] != 0) { |
---|
173 | if (!str[0]) { |
---|
174 | *res = 0.0; |
---|
175 | } |
---|
176 | else { |
---|
177 | error = GBS_global_string("cannot convert '%s' to float", str); |
---|
178 | } |
---|
179 | } |
---|
180 | return error; |
---|
181 | } |
---|
182 | |
---|
183 | float GB_atof(const char *str) { |
---|
184 | // convert ASCII to float |
---|
185 | float res = 0; |
---|
186 | GB_ERROR err = GB_safe_atof(str, &res); |
---|
187 | if (err) { |
---|
188 | // expected float in 'str'- better use GB_safe_atof() |
---|
189 | GBK_terminatef("GB_safe_atof(\"%s\", ..) returns error: %s", str, err); |
---|
190 | } |
---|
191 | return res; |
---|
192 | } |
---|
193 | |
---|
194 | // --------------------------- |
---|
195 | // compression tables |
---|
196 | |
---|
197 | const int gb_convert_type_2_compression_flags[] = { |
---|
198 | GB_COMPRESSION_NONE, // GB_NONE 0 |
---|
199 | GB_COMPRESSION_NONE, // GB_BIT 1 |
---|
200 | GB_COMPRESSION_NONE, // GB_BYTE 2 |
---|
201 | GB_COMPRESSION_NONE, // GB_INT 3 |
---|
202 | GB_COMPRESSION_NONE, // GB_FLOAT 4 |
---|
203 | GB_COMPRESSION_NONE, // GB_?? 5 |
---|
204 | GB_COMPRESSION_BITS, // GB_BITS 6 |
---|
205 | GB_COMPRESSION_NONE, // GB_?? 7 |
---|
206 | GB_COMPRESSION_RUNLENGTH | GB_COMPRESSION_HUFFMANN, // GB_BYTES 8 |
---|
207 | GB_COMPRESSION_RUNLENGTH | GB_COMPRESSION_HUFFMANN | GB_COMPRESSION_SORTBYTES, // GB_INTS 9 |
---|
208 | GB_COMPRESSION_RUNLENGTH | GB_COMPRESSION_HUFFMANN | GB_COMPRESSION_SORTBYTES, // GB_FLTS 10 |
---|
209 | GB_COMPRESSION_NONE, // GB_LINK 11 |
---|
210 | GB_COMPRESSION_RUNLENGTH | GB_COMPRESSION_HUFFMANN | GB_COMPRESSION_DICTIONARY, // GB_STR 12 |
---|
211 | GB_COMPRESSION_NONE, // GB_STRS 13 |
---|
212 | GB_COMPRESSION_NONE, // GB?? 14 |
---|
213 | GB_COMPRESSION_NONE // GB_DB 15 |
---|
214 | }; |
---|
215 | |
---|
216 | int gb_convert_type_2_sizeof[] = { /* contains the unit-size of data stored in DB, |
---|
217 | * i.e. realsize = unit_size * size() |
---|
218 | */ |
---|
219 | 0, // GB_NONE 0 |
---|
220 | 0, // GB_BIT 1 |
---|
221 | sizeof(char), // GB_BYTE 2 |
---|
222 | sizeof(int), // GB_INT 3 |
---|
223 | sizeof(float), // GB_FLOAT 4 |
---|
224 | 0, // GB_?? 5 |
---|
225 | 0, // GB_BITS 6 |
---|
226 | 0, // GB_?? 7 |
---|
227 | sizeof(char), // GB_BYTES 8 |
---|
228 | sizeof(int), // GB_INTS 9 |
---|
229 | sizeof(float), // GB_FLTS 10 |
---|
230 | sizeof(char), // GB_LINK 11 |
---|
231 | sizeof(char), // GB_STR 12 |
---|
232 | sizeof(char), // GB_STRS 13 |
---|
233 | 0, // GB_?? 14 |
---|
234 | 0, // GB_DB 15 |
---|
235 | }; |
---|
236 | |
---|
237 | int gb_convert_type_2_appendix_size[] = { /* contains the size of the suffix (aka terminator element) |
---|
238 | * size is in bytes |
---|
239 | */ |
---|
240 | |
---|
241 | 0, // GB_NONE 0 |
---|
242 | 0, // GB_BIT 1 |
---|
243 | 0, // GB_BYTE 2 |
---|
244 | 0, // GB_INT 3 |
---|
245 | 0, // GB_FLOAT 4 |
---|
246 | 0, // GB_?? 5 |
---|
247 | 0, // GB_BITS 6 |
---|
248 | 0, // GB_?? 7 |
---|
249 | 0, // GB_BYTES 8 |
---|
250 | 0, // GB_INTS 9 |
---|
251 | 0, // GB_FLTS 10 |
---|
252 | 1, // GB_LINK 11 (zero terminated) |
---|
253 | 1, // GB_STR 12 (zero terminated) |
---|
254 | 1, // GB_STRS 13 (zero terminated) |
---|
255 | 0, // GB_?? 14 |
---|
256 | 0, // GB_DB 15 |
---|
257 | }; |
---|
258 | |
---|
259 | |
---|
260 | // --------------------------------- |
---|
261 | // local buffer management |
---|
262 | |
---|
263 | static void init_buffer(gb_buffer *buf, size_t initial_size) { |
---|
264 | buf->size = initial_size; |
---|
265 | buf->mem = buf->size ? ARB_alloc<char>(buf->size) : NULp; |
---|
266 | } |
---|
267 | |
---|
268 | static char *check_out_buffer(gb_buffer *buf) { |
---|
269 | char *checkOut = buf->mem; |
---|
270 | |
---|
271 | buf->mem = NULp; |
---|
272 | buf->size = 0; |
---|
273 | |
---|
274 | return checkOut; |
---|
275 | } |
---|
276 | |
---|
277 | static void alloc_buffer(gb_buffer *buf, size_t size) { |
---|
278 | free(buf->mem); |
---|
279 | buf->size = size; |
---|
280 | #if (MEMORY_TEST==1) |
---|
281 | ARB_alloc(buf->mem, buf->size); |
---|
282 | #else |
---|
283 | ARB_calloc(buf->mem, buf->size); |
---|
284 | #endif |
---|
285 | } |
---|
286 | |
---|
287 | static GB_BUFFER give_buffer(gb_buffer *buf, size_t size) { |
---|
288 | #if (MEMORY_TEST==1) |
---|
289 | alloc_buffer(buf, size); // do NOT reuse buffer if testing memory |
---|
290 | #else |
---|
291 | if (size >= buf->size) { |
---|
292 | alloc_buffer(buf, size); |
---|
293 | } |
---|
294 | #endif |
---|
295 | return buf->mem; |
---|
296 | } |
---|
297 | |
---|
298 | static int is_in_buffer(gb_buffer *buf, GB_CBUFFER ptr) { |
---|
299 | return ptr >= buf->mem && ptr < buf->mem+buf->size; |
---|
300 | } |
---|
301 | |
---|
302 | // ------------------------------ |
---|
303 | |
---|
304 | GB_BUFFER GB_give_buffer(size_t size) { |
---|
305 | // return a pointer to a static piece of memory at least size bytes long |
---|
306 | return give_buffer(&gb_local->buf1, size); |
---|
307 | } |
---|
308 | |
---|
309 | GB_BUFFER GB_increase_buffer(size_t size) { |
---|
310 | if (size < gb_local->buf1.size) { |
---|
311 | char *old_buffer = gb_local->buf1.mem; |
---|
312 | size_t old_size = gb_local->buf1.size; |
---|
313 | |
---|
314 | gb_local->buf1.mem = NULp; |
---|
315 | alloc_buffer(&gb_local->buf1, size); |
---|
316 | memcpy(gb_local->buf1.mem, old_buffer, old_size); |
---|
317 | |
---|
318 | free(old_buffer); |
---|
319 | } |
---|
320 | return gb_local->buf1.mem; |
---|
321 | } |
---|
322 | |
---|
323 | NOT4PERL int GB_give_buffer_size() { |
---|
324 | return gb_local->buf1.size; |
---|
325 | } |
---|
326 | |
---|
327 | GB_BUFFER GB_give_buffer2(long size) { |
---|
328 | return give_buffer(&gb_local->buf2, size); |
---|
329 | } |
---|
330 | |
---|
331 | static int GB_is_in_buffer(GB_CBUFFER ptr) { |
---|
332 | /* returns 1 or 2 if 'ptr' points to gb_local->buf1/buf2 |
---|
333 | * returns 0 otherwise |
---|
334 | */ |
---|
335 | int buffer = 0; |
---|
336 | |
---|
337 | if (is_in_buffer(&gb_local->buf1, ptr)) buffer = 1; |
---|
338 | else if (is_in_buffer(&gb_local->buf2, ptr)) buffer = 2; |
---|
339 | |
---|
340 | return buffer; |
---|
341 | } |
---|
342 | |
---|
343 | char *GB_check_out_buffer(GB_CBUFFER buffer) { |
---|
344 | /* Check a piece of memory out of the buffer management |
---|
345 | * after it is checked out, the user has the full control to use and free it |
---|
346 | * Returns a pointer to the start of the buffer (even if 'buffer' points inside the buffer!) |
---|
347 | */ |
---|
348 | char *old = NULp; |
---|
349 | |
---|
350 | if (is_in_buffer(&gb_local->buf1, buffer)) old = check_out_buffer(&gb_local->buf1); |
---|
351 | else if (is_in_buffer(&gb_local->buf2, buffer)) old = check_out_buffer(&gb_local->buf2); |
---|
352 | |
---|
353 | return old; |
---|
354 | } |
---|
355 | |
---|
356 | GB_BUFFER GB_give_other_buffer(GB_CBUFFER buffer, long size) { |
---|
357 | return is_in_buffer(&gb_local->buf1, buffer) |
---|
358 | ? GB_give_buffer2(size) |
---|
359 | : GB_give_buffer(size); |
---|
360 | } |
---|
361 | |
---|
362 | static unsigned char GB_BIT_compress_data[] = { |
---|
363 | 0x1d, GB_CS_OK, 0, 0, |
---|
364 | 0x04, GB_CS_OK, 0, 1, |
---|
365 | 0x0a, GB_CS_OK, 0, 2, |
---|
366 | 0x0b, GB_CS_OK, 0, 3, |
---|
367 | 0x0c, GB_CS_OK, 0, 4, |
---|
368 | 0x1a, GB_CS_OK, 0, 5, |
---|
369 | 0x1b, GB_CS_OK, 0, 6, |
---|
370 | 0x1c, GB_CS_OK, 0, 7, |
---|
371 | 0xf0, GB_CS_OK, 0, 8, |
---|
372 | 0xf1, GB_CS_OK, 0, 9, |
---|
373 | 0xf2, GB_CS_OK, 0, 10, |
---|
374 | 0xf3, GB_CS_OK, 0, 11, |
---|
375 | 0xf4, GB_CS_OK, 0, 12, |
---|
376 | 0xf5, GB_CS_OK, 0, 13, |
---|
377 | 0xf6, GB_CS_OK, 0, 14, |
---|
378 | 0xf7, GB_CS_OK, 0, 15, |
---|
379 | 0xf8, GB_CS_SUB, 0, 16, |
---|
380 | 0xf9, GB_CS_SUB, 0, 32, |
---|
381 | 0xfa, GB_CS_SUB, 0, 48, |
---|
382 | 0xfb, GB_CS_SUB, 0, 64, |
---|
383 | 0xfc, GB_CS_SUB, 0, 128, |
---|
384 | 0xfd, GB_CS_SUB, 1, 0, |
---|
385 | 0xfe, GB_CS_SUB, 2, 0, |
---|
386 | 0xff, GB_CS_SUB, 4, 0, |
---|
387 | 0 |
---|
388 | }; |
---|
389 | |
---|
390 | struct gb_exitfun { |
---|
391 | void (*exitfun)(); |
---|
392 | gb_exitfun *next; |
---|
393 | }; |
---|
394 | |
---|
395 | void GB_atexit(void (*exitfun)()) { |
---|
396 | // called when GB_shell is destroyed (use similar to atexit()) |
---|
397 | // |
---|
398 | // Since the program does not necessarily terminate, your code calling |
---|
399 | // GB_atexit() may run multiple times. Make sure everything is completely reset by your 'exitfun' |
---|
400 | |
---|
401 | gb_exitfun *fun = new gb_exitfun; |
---|
402 | fun->exitfun = exitfun; |
---|
403 | |
---|
404 | fun->next = gb_local->atgbexit; |
---|
405 | gb_local->atgbexit = fun; |
---|
406 | } |
---|
407 | |
---|
408 | static void run_and_destroy_exit_functions(gb_exitfun *fun) { |
---|
409 | if (fun) { |
---|
410 | fun->exitfun(); |
---|
411 | run_and_destroy_exit_functions(fun->next); |
---|
412 | delete fun; |
---|
413 | } |
---|
414 | } |
---|
415 | |
---|
416 | static void GB_exit_gb() { |
---|
417 | GB_shell::ensure_inside(); |
---|
418 | |
---|
419 | if (gb_local) { |
---|
420 | gb_local->~gb_local_data(); // inplace-dtor |
---|
421 | gbm_free_mem(gb_local, sizeof(*gb_local), 0); |
---|
422 | gb_local = NULp; |
---|
423 | gbm_flush_mem(); |
---|
424 | } |
---|
425 | } |
---|
426 | |
---|
427 | gb_local_data::~gb_local_data() { |
---|
428 | gb_assert(openedDBs == closedDBs); |
---|
429 | |
---|
430 | run_and_destroy_exit_functions(atgbexit); |
---|
431 | |
---|
432 | free(bitcompress); |
---|
433 | gb_free_compress_tree(bituncompress); |
---|
434 | free(write_buffer); |
---|
435 | |
---|
436 | free(check_out_buffer(&buf2)); |
---|
437 | free(check_out_buffer(&buf1)); |
---|
438 | free(open_gb_mains); |
---|
439 | } |
---|
440 | |
---|
441 | // ----------------- |
---|
442 | // GB_shell |
---|
443 | |
---|
444 | |
---|
445 | static GB_shell *inside_shell = NULp; |
---|
446 | |
---|
447 | GB_shell::GB_shell() { |
---|
448 | if (inside_shell) GBK_terminate("only one GB_shell allowed"); |
---|
449 | inside_shell = this; |
---|
450 | } |
---|
451 | GB_shell::~GB_shell() { |
---|
452 | gb_assert(inside_shell == this); |
---|
453 | GB_exit_gb(); |
---|
454 | inside_shell = NULp; |
---|
455 | } |
---|
456 | void GB_shell::ensure_inside() { if (!inside_shell) GBK_terminate("Not inside GB_shell"); } |
---|
457 | |
---|
458 | bool GB_shell::in_shell() { // used by code based on ARBDB (Kai IIRC) |
---|
459 | return inside_shell; |
---|
460 | } |
---|
461 | |
---|
462 | struct GB_test_shell_closed { |
---|
463 | ~GB_test_shell_closed() { |
---|
464 | if (GB_shell::in_shell()) { // leave that call |
---|
465 | inside_shell->~GB_shell(); // call dtor |
---|
466 | } |
---|
467 | } |
---|
468 | }; |
---|
469 | static GB_test_shell_closed test; |
---|
470 | |
---|
471 | #if defined(UNIT_TESTS) |
---|
472 | static bool closed_open_shell_for_unit_tests() { |
---|
473 | bool was_open = inside_shell; |
---|
474 | if (was_open) { |
---|
475 | if (gb_local) gb_local->fake_closed_DBs(); |
---|
476 | inside_shell->~GB_shell(); // just call dtor (not delete) |
---|
477 | } |
---|
478 | return was_open; |
---|
479 | } |
---|
480 | #endif |
---|
481 | |
---|
482 | void GB_init_gb() { |
---|
483 | GB_shell::ensure_inside(); |
---|
484 | if (!gb_local) { |
---|
485 | GBK_install_SIGSEGV_handler(true); // never uninstalled |
---|
486 | gbm_init_mem(); |
---|
487 | gb_local = (gb_local_data *)gbm_get_mem(sizeof(gb_local_data), 0); |
---|
488 | ::new(gb_local) gb_local_data(); // inplace-ctor |
---|
489 | } |
---|
490 | } |
---|
491 | |
---|
492 | int GB_open_DBs() { return gb_local ? gb_local->open_dbs() : 0; } |
---|
493 | |
---|
494 | gb_local_data::gb_local_data() { |
---|
495 | init_buffer(&buf1, 4000); |
---|
496 | init_buffer(&buf2, 4000); |
---|
497 | |
---|
498 | write_bufsize = GBCM_BUFFER; |
---|
499 | ARB_alloc(write_buffer, write_bufsize); |
---|
500 | |
---|
501 | write_ptr = write_buffer; |
---|
502 | write_free = write_bufsize; |
---|
503 | |
---|
504 | bituncompress = gb_build_uncompress_tree(GB_BIT_compress_data, 1, NULp); |
---|
505 | bitcompress = gb_build_compress_list(GB_BIT_compress_data, 1, &(bc_size)); |
---|
506 | |
---|
507 | openedDBs = 0; |
---|
508 | closedDBs = 0; |
---|
509 | |
---|
510 | open_gb_mains = NULp; |
---|
511 | open_gb_alloc = 0; |
---|
512 | |
---|
513 | atgbexit = NULp; |
---|
514 | |
---|
515 | iamclient = false; |
---|
516 | search_system_folder = false; |
---|
517 | running_client_transaction = ARB_NO_TRANS; |
---|
518 | } |
---|
519 | |
---|
520 | void gb_local_data::announce_db_open(GB_MAIN_TYPE *Main) { |
---|
521 | gb_assert(Main); |
---|
522 | int idx = open_dbs(); |
---|
523 | if (idx >= open_gb_alloc) { |
---|
524 | int new_alloc = open_gb_alloc + 10; |
---|
525 | ARB_recalloc(open_gb_mains, open_gb_alloc, new_alloc); |
---|
526 | open_gb_alloc = new_alloc; |
---|
527 | } |
---|
528 | open_gb_mains[idx] = Main; |
---|
529 | openedDBs++; |
---|
530 | } |
---|
531 | |
---|
532 | void gb_local_data::announce_db_close(GB_MAIN_TYPE *Main) { |
---|
533 | gb_assert(Main); |
---|
534 | int open = open_dbs(); |
---|
535 | int idx; |
---|
536 | for (idx = 0; idx<open; ++idx) if (open_gb_mains[idx] == Main) break; |
---|
537 | |
---|
538 | gb_assert(idx<open); // passed gb_main is unknown |
---|
539 | if (idx<open) { |
---|
540 | if (idx<(open-1)) { // not last |
---|
541 | open_gb_mains[idx] = open_gb_mains[open-1]; |
---|
542 | } |
---|
543 | closedDBs++; |
---|
544 | } |
---|
545 | if (closedDBs == openedDBs) { |
---|
546 | GB_exit_gb(); // free most memory allocated by ARBDB library |
---|
547 | // Caution: calling GB_exit_gb() frees 'this'! |
---|
548 | } |
---|
549 | } |
---|
550 | |
---|
551 | static GBDATA *gb_remembered_db() { |
---|
552 | GB_MAIN_TYPE *Main = gb_local ? gb_local->get_any_open_db() : NULp; |
---|
553 | return Main ? Main->gb_main() : NULp; |
---|
554 | } |
---|
555 | |
---|
556 | GB_ERROR gb_unfold(GBCONTAINER *gbc, long deep, int index_pos) { |
---|
557 | /*! get data from server. |
---|
558 | * |
---|
559 | * @param gbc container to unfold |
---|
560 | * @param deep if != 0, then get subitems too. |
---|
561 | * @param index_pos |
---|
562 | * - >= 0, get indexed item from server |
---|
563 | * - <0, get all items |
---|
564 | * |
---|
565 | * @return error on failure |
---|
566 | */ |
---|
567 | |
---|
568 | GB_ERROR error; |
---|
569 | gb_header_list *header = GB_DATA_LIST_HEADER(gbc->d); |
---|
570 | |
---|
571 | if (!gbc->flags2.folded_container) return NULp; |
---|
572 | if (index_pos> gbc->d.nheader) gb_create_header_array(gbc, index_pos + 1); |
---|
573 | if (index_pos >= 0 && GB_HEADER_LIST_GBD(header[index_pos])) return NULp; |
---|
574 | |
---|
575 | if (GBCONTAINER_MAIN(gbc)->is_server()) { |
---|
576 | GB_internal_error("Cannot unfold in server"); |
---|
577 | return NULp; |
---|
578 | } |
---|
579 | |
---|
580 | do { |
---|
581 | if (index_pos<0) break; |
---|
582 | if (index_pos >= gbc->d.nheader) break; |
---|
583 | if (header[index_pos].flags.changed >= GB_DELETED) { |
---|
584 | GB_internal_error("Tried to unfold a deleted item"); |
---|
585 | return NULp; |
---|
586 | } |
---|
587 | if (GB_HEADER_LIST_GBD(header[index_pos])) return NULp; // already unfolded |
---|
588 | } while (0); |
---|
589 | |
---|
590 | error = gbcm_unfold_client(gbc, deep, index_pos); |
---|
591 | if (error) { |
---|
592 | GB_print_error(); |
---|
593 | return error; |
---|
594 | } |
---|
595 | |
---|
596 | if (index_pos<0) { |
---|
597 | gb_untouch_children(gbc); |
---|
598 | gbc->flags2.folded_container = 0; |
---|
599 | } |
---|
600 | else { |
---|
601 | GBDATA *gb2 = GBCONTAINER_ELEM(gbc, index_pos); |
---|
602 | if (gb2) { |
---|
603 | if (gb2->is_container()) { |
---|
604 | gb_untouch_children_and_me(gb2->as_container()); |
---|
605 | } |
---|
606 | else { |
---|
607 | gb_untouch_me(gb2->as_entry()); |
---|
608 | } |
---|
609 | } |
---|
610 | } |
---|
611 | return NULp; |
---|
612 | } |
---|
613 | |
---|
614 | // ----------------------- |
---|
615 | // close database |
---|
616 | |
---|
617 | static void run_close_callbacks(GBDATA *gb_main) { |
---|
618 | GB_MAIN_TYPE *Main = GB_MAIN(gb_main); |
---|
619 | gb_assert(Main->gb_main() == gb_main); |
---|
620 | if (Main->close_callbacks) { |
---|
621 | Main->close_callbacks->call(gb_main, GB_CB_DELETE); |
---|
622 | } |
---|
623 | } |
---|
624 | |
---|
625 | void GB_close(GBDATA *gbd) { |
---|
626 | GB_ERROR error = NULp; |
---|
627 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
628 | |
---|
629 | gb_assert(Main->get_transaction_level() <= 0); // transaction running - you can't close DB yet! |
---|
630 | |
---|
631 | Main->forget_hierarchy_cbs(); |
---|
632 | |
---|
633 | gb_assert(Main->gb_main() == gbd); |
---|
634 | run_close_callbacks(gbd); |
---|
635 | Main->close_callbacks = NULp; |
---|
636 | |
---|
637 | bool quick_exit = Main->mapped; |
---|
638 | if (Main->is_client()) { |
---|
639 | GBCM_ServerResult result = gbcmc_close(Main->c_link); |
---|
640 | if (result != GBCM_SERVER_OK) error = GBS_global_string("close failed (with %i:%s)", result, GB_await_error()); |
---|
641 | |
---|
642 | gb_assert(!quick_exit); // client cannot be mapped |
---|
643 | } |
---|
644 | |
---|
645 | gbcm_logout(Main, NULp); // logout default user |
---|
646 | |
---|
647 | if (!error) { |
---|
648 | gb_assert(!Main->close_callbacks); |
---|
649 | |
---|
650 | #if defined(LEAKS_SANITIZED) |
---|
651 | quick_exit = false; |
---|
652 | #endif |
---|
653 | |
---|
654 | if (quick_exit) { |
---|
655 | // fake some data to allow quick-exit |
---|
656 | Main->dummy_father = NULp; |
---|
657 | Main->cache.entries = NULp; |
---|
658 | } |
---|
659 | else { |
---|
660 | // proper cleanup of DB (causes unwanted behavior described in #649) |
---|
661 | gb_delete_dummy_father(Main->dummy_father); |
---|
662 | } |
---|
663 | Main->root_container = NULp; |
---|
664 | |
---|
665 | /* ARBDB applications using awars easily crash in call_pending_callbacks(), |
---|
666 | * if AWARs are still bound to elements in the closed database. |
---|
667 | * |
---|
668 | * To unlink awars call AW_root::unlink_awars_from_DB(). |
---|
669 | * If that doesn't help, test Main->data (often aka as GLOBAL_gb_main) |
---|
670 | */ |
---|
671 | Main->call_pending_callbacks(); // do all callbacks |
---|
672 | delete Main; |
---|
673 | } |
---|
674 | |
---|
675 | if (error) { |
---|
676 | GB_warningf("Error in GB_close: %s", error); |
---|
677 | } |
---|
678 | } |
---|
679 | |
---|
680 | void gb_abort_and_close_all_DBs() { |
---|
681 | GBDATA *gb_main; |
---|
682 | while ((gb_main = gb_remembered_db())) { |
---|
683 | // abort any open transactions |
---|
684 | GB_MAIN_TYPE *Main = GB_MAIN(gb_main); |
---|
685 | while (Main->get_transaction_level()>0) { |
---|
686 | GB_ERROR error = Main->abort_transaction(); |
---|
687 | if (error) { |
---|
688 | fprintf(stderr, "Error in gb_abort_and_close_all_DBs: %s\n", error); |
---|
689 | } |
---|
690 | } |
---|
691 | // and close DB |
---|
692 | GB_close(gb_main); |
---|
693 | } |
---|
694 | } |
---|
695 | |
---|
696 | // ------------------ |
---|
697 | // read data |
---|
698 | |
---|
699 | long GB_read_int(GBDATA *gbd) { |
---|
700 | GB_TEST_READ_NUM(gbd, GB_INT, "GB_read_int"); |
---|
701 | return gbd->as_entry()->info.i; |
---|
702 | } |
---|
703 | |
---|
704 | int GB_read_byte(GBDATA *gbd) { |
---|
705 | GB_TEST_READ_NUM(gbd, GB_BYTE, "GB_read_byte"); |
---|
706 | return gbd->as_entry()->info.i; |
---|
707 | } |
---|
708 | |
---|
709 | GBDATA *GB_read_pointer(GBDATA *gbd) { |
---|
710 | GB_TEST_READ_PTR(gbd, GB_POINTER, "GB_read_pointer"); |
---|
711 | return gbd->as_entry()->info.ptr; |
---|
712 | } |
---|
713 | |
---|
714 | float GB_read_float(GBDATA *gbd) { |
---|
715 | XDR xdrs; |
---|
716 | float f; |
---|
717 | |
---|
718 | GB_TEST_READ_NUM(gbd, GB_FLOAT, "GB_read_float"); |
---|
719 | xdrmem_create(&xdrs, &gbd->as_entry()->info.in.data[0], SIZOFINTERN, XDR_DECODE); |
---|
720 | xdr_float(&xdrs, &f); |
---|
721 | xdr_destroy(&xdrs); |
---|
722 | |
---|
723 | gb_assert(f == f); // !nan |
---|
724 | |
---|
725 | return f; |
---|
726 | } |
---|
727 | |
---|
728 | long GB_read_count(GBDATA *gbd) { |
---|
729 | return gbd->as_entry()->size(); |
---|
730 | } |
---|
731 | |
---|
732 | long GB_read_memuse(GBDATA *gbd) { |
---|
733 | return gbd->as_entry()->memsize(); |
---|
734 | } |
---|
735 | |
---|
736 | #if defined(DEBUG) |
---|
737 | |
---|
738 | #define MIN_CBLISTNODE_SIZE 48 // minimum (found) callbacklist-elementsize |
---|
739 | |
---|
740 | #if defined(DARWIN) |
---|
741 | |
---|
742 | #define CBLISTNODE_SIZE MIN_CBLISTNODE_SIZE // assume known minimum (doesnt really matter; only used in db-browser) |
---|
743 | |
---|
744 | #else // linux: |
---|
745 | |
---|
746 | typedef std::_List_node<gb_callback_list::cbtype> CBLISTNODE_TYPE; |
---|
747 | const size_t CBLISTNODE_SIZE = sizeof(CBLISTNODE_TYPE); |
---|
748 | |
---|
749 | #if defined(ARB_64) |
---|
750 | // ignore smaller 32-bit implementations |
---|
751 | STATIC_ASSERT_ANNOTATED(MIN_CBLISTNODE_SIZE<=CBLISTNODE_SIZE, "MIN_CBLISTNODE_SIZE too big (smaller implementation detected)"); |
---|
752 | #endif |
---|
753 | |
---|
754 | #endif |
---|
755 | |
---|
756 | inline long calc_size(gb_callback_list *gbcbl) { |
---|
757 | return gbcbl |
---|
758 | ? sizeof(*gbcbl) + gbcbl->callbacks.size()* CBLISTNODE_SIZE |
---|
759 | : 0; |
---|
760 | } |
---|
761 | inline long calc_size(gb_transaction_save *gbts) { |
---|
762 | return gbts |
---|
763 | ? sizeof(*gbts) |
---|
764 | : 0; |
---|
765 | } |
---|
766 | inline long calc_size(gb_if_entries *gbie) { |
---|
767 | return gbie |
---|
768 | ? sizeof(*gbie) + calc_size(GB_IF_ENTRIES_NEXT(gbie)) |
---|
769 | : 0; |
---|
770 | } |
---|
771 | inline long calc_size(GB_REL_IFES *gbri, int table_size) { |
---|
772 | long size = 0; |
---|
773 | |
---|
774 | gb_if_entries *ifes; |
---|
775 | for (int idx = 0; idx<table_size; ++idx) { |
---|
776 | for (ifes = GB_ENTRIES_ENTRY(gbri, idx); |
---|
777 | ifes; |
---|
778 | ifes = GB_IF_ENTRIES_NEXT(ifes)) |
---|
779 | { |
---|
780 | size += calc_size(ifes); |
---|
781 | } |
---|
782 | } |
---|
783 | return size; |
---|
784 | } |
---|
785 | inline long calc_size(gb_index_files *gbif) { |
---|
786 | return gbif |
---|
787 | ? sizeof(*gbif) + calc_size(GB_INDEX_FILES_NEXT(gbif)) + calc_size(GB_INDEX_FILES_ENTRIES(gbif), gbif->hash_table_size) |
---|
788 | : 0; |
---|
789 | } |
---|
790 | inline long calc_size(gb_db_extended *gbe) { |
---|
791 | return gbe |
---|
792 | ? sizeof(*gbe) + calc_size(gbe->callback) + calc_size(gbe->old) |
---|
793 | : 0; |
---|
794 | } |
---|
795 | inline long calc_size(GBENTRY *gbe) { |
---|
796 | return gbe |
---|
797 | ? sizeof(*gbe) + calc_size(gbe->ext) |
---|
798 | : 0; |
---|
799 | } |
---|
800 | inline long calc_size(GBCONTAINER *gbc) { |
---|
801 | return gbc |
---|
802 | ? sizeof(*gbc) + calc_size(gbc->ext) + calc_size(GBCONTAINER_IFS(gbc)) |
---|
803 | : 0; |
---|
804 | } |
---|
805 | |
---|
806 | long GB_calc_structure_size(GBDATA *gbd) { |
---|
807 | long size = 0; |
---|
808 | if (gbd->is_container()) { |
---|
809 | size = calc_size(gbd->as_container()); |
---|
810 | } |
---|
811 | else { |
---|
812 | size = calc_size(gbd->as_entry()); |
---|
813 | } |
---|
814 | return size; |
---|
815 | } |
---|
816 | |
---|
817 | void GB_SizeInfo::collect(GBDATA *gbd) { |
---|
818 | if (gbd->is_container()) { |
---|
819 | ++containers; |
---|
820 | for (GBDATA *gb_child = GB_child(gbd); gb_child; gb_child = GB_nextChild(gb_child)) { |
---|
821 | collect(gb_child); |
---|
822 | } |
---|
823 | } |
---|
824 | else { |
---|
825 | ++terminals; |
---|
826 | mem += GB_read_memuse(gbd); |
---|
827 | |
---|
828 | long size; |
---|
829 | switch (gbd->type()) { |
---|
830 | case GB_INT: size = sizeof(int); break; |
---|
831 | case GB_FLOAT: size = sizeof(float); break; |
---|
832 | case GB_BYTE: size = sizeof(char); break; |
---|
833 | case GB_POINTER: size = sizeof(GBDATA*); break; |
---|
834 | case GB_STRING: size = GB_read_count(gbd); break; // accept 0 sized data for strings |
---|
835 | |
---|
836 | default: |
---|
837 | size = GB_read_count(gbd); |
---|
838 | gb_assert(size>0); // terminal w/o data - really? |
---|
839 | break; |
---|
840 | } |
---|
841 | data += size; |
---|
842 | } |
---|
843 | structure += GB_calc_structure_size(gbd); |
---|
844 | } |
---|
845 | #endif |
---|
846 | |
---|
847 | GB_CSTR GB_read_pntr(GBDATA *gbd) { |
---|
848 | GBENTRY *gbe = gbd->as_entry(); |
---|
849 | const char *data = gbe->data(); |
---|
850 | |
---|
851 | if (data) { |
---|
852 | if (gbe->flags.compressed_data) { // uncompressed data return pntr to database entry |
---|
853 | char *ca = gb_read_cache(gbe); |
---|
854 | |
---|
855 | if (!ca) { |
---|
856 | size_t size = gbe->uncompressed_size(); |
---|
857 | const char *da = gb_uncompress_data(gbe, data, size); |
---|
858 | |
---|
859 | if (da) { |
---|
860 | ca = gb_alloc_cache_index(gbe, size); |
---|
861 | memcpy(ca, da, size); |
---|
862 | } |
---|
863 | } |
---|
864 | data = ca; |
---|
865 | } |
---|
866 | } |
---|
867 | return data; |
---|
868 | } |
---|
869 | |
---|
870 | int gb_read_nr(GBDATA *gbd) { |
---|
871 | return gbd->index; |
---|
872 | } |
---|
873 | |
---|
874 | GB_CSTR GB_read_char_pntr(GBDATA *gbd) { |
---|
875 | GB_TEST_READ_PTR(gbd, GB_STRING, "GB_read_char_pntr"); |
---|
876 | return GB_read_pntr(gbd); |
---|
877 | } |
---|
878 | |
---|
879 | char *GB_read_string(GBDATA *gbd) { |
---|
880 | GB_TEST_READ_PTR(gbd, GB_STRING, "GB_read_string"); |
---|
881 | const char *d = GB_read_pntr(gbd); |
---|
882 | if (!d) return NULp; |
---|
883 | return GB_memdup(d, gbd->as_entry()->size()+1); |
---|
884 | } |
---|
885 | |
---|
886 | size_t GB_read_string_count(GBDATA *gbd) { |
---|
887 | GB_TEST_READ_NUM(gbd, GB_STRING, "GB_read_string_count"); |
---|
888 | return gbd->as_entry()->size(); |
---|
889 | } |
---|
890 | |
---|
891 | long GB_read_bits_count(GBDATA *gbd) { |
---|
892 | GB_TEST_READ_NUM(gbd, GB_BITS, "GB_read_bits_count"); |
---|
893 | return gbd->as_entry()->size(); |
---|
894 | } |
---|
895 | |
---|
896 | GB_CSTR GB_read_bits_pntr(GBDATA *gbd, char c_0, char c_1) { |
---|
897 | GB_TEST_READ_PTR(gbd, GB_BITS, "GB_read_bits_pntr"); |
---|
898 | GBENTRY *gbe = gbd->as_entry(); |
---|
899 | long size = gbe->size(); |
---|
900 | if (size) { |
---|
901 | // Note: the first 2 bytes of the cached entry contain the currently used encoding (c_0 and c_1) |
---|
902 | |
---|
903 | char *ca = gb_read_cache(gbe); |
---|
904 | if (ca) { |
---|
905 | // check if encoding was the same during last read |
---|
906 | if (ca[0] == c_0 && ca[1] == c_1) { // yes -> reuse |
---|
907 | return ca+2; |
---|
908 | } |
---|
909 | // no -> re-read from DB |
---|
910 | GB_flush_cache(gbe); |
---|
911 | ca = NULp; |
---|
912 | } |
---|
913 | |
---|
914 | const char *data = gbe->data(); |
---|
915 | char *da = gb_uncompress_bits(data, size, c_0, c_1); |
---|
916 | |
---|
917 | if (da) { |
---|
918 | ca = gb_alloc_cache_index(gbe, size+3); // 2 byte encoding + stored data + terminal '\0' |
---|
919 | ca[0] = c_0; |
---|
920 | ca[1] = c_1; |
---|
921 | memcpy(ca+2, da, size+1); |
---|
922 | return ca+2; |
---|
923 | } |
---|
924 | } |
---|
925 | return NULp; |
---|
926 | } |
---|
927 | |
---|
928 | char *GB_read_bits(GBDATA *gbd, char c_0, char c_1) { |
---|
929 | GB_CSTR d = GB_read_bits_pntr(gbd, c_0, c_1); |
---|
930 | return d ? GB_memdup(d, gbd->as_entry()->size()+1) : NULp; |
---|
931 | } |
---|
932 | |
---|
933 | |
---|
934 | GB_CSTR GB_read_bytes_pntr(GBDATA *gbd) { |
---|
935 | GB_TEST_READ_PTR(gbd, GB_BYTES, "GB_read_bytes_pntr"); |
---|
936 | return GB_read_pntr(gbd); |
---|
937 | } |
---|
938 | |
---|
939 | long GB_read_bytes_count(GBDATA *gbd) { |
---|
940 | GB_TEST_READ_NUM(gbd, GB_BYTES, "GB_read_bytes_count"); |
---|
941 | return gbd->as_entry()->size(); |
---|
942 | } |
---|
943 | |
---|
944 | char *GB_read_bytes(GBDATA *gbd) { |
---|
945 | GB_CSTR d = GB_read_bytes_pntr(gbd); |
---|
946 | return d ? GB_memdup(d, gbd->as_entry()->size()) : NULp; |
---|
947 | } |
---|
948 | |
---|
949 | GB_CUINT4 *GB_read_ints_pntr(GBDATA *gbd) { |
---|
950 | GB_TEST_READ_PTR(gbd, GB_INTS, "GB_read_ints_pntr"); |
---|
951 | GBENTRY *gbe = gbd->as_entry(); |
---|
952 | |
---|
953 | GB_UINT4 *res; |
---|
954 | if (gbe->flags.compressed_data) { |
---|
955 | res = (GB_UINT4 *)GB_read_pntr(gbe); |
---|
956 | } |
---|
957 | else { |
---|
958 | res = (GB_UINT4 *)gbe->data(); |
---|
959 | } |
---|
960 | if (!res) return NULp; |
---|
961 | |
---|
962 | if (0x01020304U == htonl(0x01020304U)) { |
---|
963 | return res; |
---|
964 | } |
---|
965 | else { |
---|
966 | int size = gbe->size(); |
---|
967 | char *buf2 = GB_give_other_buffer((char *)res, size<<2); |
---|
968 | GB_UINT4 *s = (GB_UINT4 *)res; |
---|
969 | GB_UINT4 *d = (GB_UINT4 *)buf2; |
---|
970 | |
---|
971 | for (long i=size; i; i--) { |
---|
972 | *(d++) = htonl(*(s++)); |
---|
973 | } |
---|
974 | return (GB_UINT4 *)buf2; |
---|
975 | } |
---|
976 | } |
---|
977 | |
---|
978 | long GB_read_ints_count(GBDATA *gbd) { // used by ../PERL_SCRIPTS/SAI/SAI.pm@read_ints_count |
---|
979 | GB_TEST_READ_NUM(gbd, GB_INTS, "GB_read_ints_count"); |
---|
980 | return gbd->as_entry()->size(); |
---|
981 | } |
---|
982 | |
---|
983 | GB_UINT4 *GB_read_ints(GBDATA *gbd) { |
---|
984 | GB_CUINT4 *i = GB_read_ints_pntr(gbd); |
---|
985 | if (!i) return NULp; |
---|
986 | return (GB_UINT4 *)GB_memdup((char *)i, gbd->as_entry()->size()*sizeof(GB_UINT4)); |
---|
987 | } |
---|
988 | |
---|
989 | GB_CFLOAT *GB_read_floats_pntr(GBDATA *gbd) { |
---|
990 | GB_TEST_READ_PTR(gbd, GB_FLOATS, "GB_read_floats_pntr"); |
---|
991 | GBENTRY *gbe = gbd->as_entry(); |
---|
992 | char *res; |
---|
993 | if (gbe->flags.compressed_data) { |
---|
994 | res = (char *)GB_read_pntr(gbe); |
---|
995 | } |
---|
996 | else { |
---|
997 | res = (char *)gbe->data(); |
---|
998 | } |
---|
999 | if (res) { |
---|
1000 | long size = gbe->size(); |
---|
1001 | long full_size = size*sizeof(float); |
---|
1002 | |
---|
1003 | XDR xdrs; |
---|
1004 | xdrmem_create(&xdrs, res, (int)(full_size), XDR_DECODE); |
---|
1005 | |
---|
1006 | char *buf2 = GB_give_other_buffer(res, full_size); |
---|
1007 | float *d = (float *)(void*)buf2; |
---|
1008 | for (long i=size; i; i--) { |
---|
1009 | xdr_float(&xdrs, d); |
---|
1010 | d++; |
---|
1011 | } |
---|
1012 | xdr_destroy(&xdrs); |
---|
1013 | return (float *)(void*)buf2; |
---|
1014 | } |
---|
1015 | return NULp; |
---|
1016 | } |
---|
1017 | |
---|
1018 | static long GB_read_floats_count(GBDATA *gbd) { |
---|
1019 | GB_TEST_READ_NUM(gbd, GB_FLOATS, "GB_read_floats_count"); |
---|
1020 | return gbd->as_entry()->size(); |
---|
1021 | } |
---|
1022 | |
---|
1023 | float *GB_read_floats(GBDATA *gbd) { // @@@ only used in unittest - check usage of floats |
---|
1024 | GB_CFLOAT *f; |
---|
1025 | f = GB_read_floats_pntr(gbd); |
---|
1026 | if (!f) return NULp; |
---|
1027 | return (float *)GB_memdup((char *)f, gbd->as_entry()->size()*sizeof(float)); |
---|
1028 | } |
---|
1029 | |
---|
1030 | char *GB_read_as_string(GBDATA *gbd) { |
---|
1031 | /*! reads basic db-field types and returns content as text. |
---|
1032 | * @see GB_write_autoconv_string |
---|
1033 | * @see GB_readable_as_string |
---|
1034 | */ |
---|
1035 | switch (gbd->type()) { |
---|
1036 | case GB_STRING: return GB_read_string(gbd); |
---|
1037 | case GB_BYTE: return GBS_global_string_copy("%i", GB_read_byte(gbd)); |
---|
1038 | case GB_INT: return GBS_global_string_copy("%li", GB_read_int(gbd)); |
---|
1039 | case GB_FLOAT: return ARB_strdup(ARB_float_2_ascii(GB_read_float(gbd))); |
---|
1040 | case GB_BITS: return GB_read_bits(gbd, '0', '1'); |
---|
1041 | /* Be careful : When adding new types here, you have to make sure that |
---|
1042 | * GB_write_autoconv_string is able to write them back and that this makes sense. |
---|
1043 | */ |
---|
1044 | default: |
---|
1045 | gb_assert(!GB_TYPE_readable_as_string(gbd->type())); |
---|
1046 | return NULp; |
---|
1047 | } |
---|
1048 | } |
---|
1049 | |
---|
1050 | bool GB_readable_as_string(GBDATA *gbd) { |
---|
1051 | //! @see GB_TYPE_readable_as_string() |
---|
1052 | return GB_TYPE_readable_as_string(gbd->type()); |
---|
1053 | } |
---|
1054 | |
---|
1055 | |
---|
1056 | inline GB_ERROR cannot_use_fun4entry(const char *fun, GBDATA *gb_entry) { |
---|
1057 | return GBS_global_string("Error: Cannot use %s() with a field of type %i (field=%s)", |
---|
1058 | fun, |
---|
1059 | GB_read_type(gb_entry), |
---|
1060 | GB_read_key_pntr(gb_entry)); |
---|
1061 | } |
---|
1062 | |
---|
1063 | NOT4PERL uint8_t GB_read_lossless_byte(GBDATA *gbd, GB_ERROR& error) { |
---|
1064 | /*! Reads an uint8_t previously written with GB_write_lossless_byte() |
---|
1065 | * @param gbd the DB field |
---|
1066 | * @param error result parameter (has to be NULp) |
---|
1067 | * @result is undefined if error got set; contains read value otherwise |
---|
1068 | */ |
---|
1069 | gb_assert(!error); |
---|
1070 | gb_assert(!GB_have_error()); |
---|
1071 | uint8_t result; |
---|
1072 | switch (gbd->type()) { |
---|
1073 | case GB_BYTE: |
---|
1074 | result = GB_read_byte(gbd); |
---|
1075 | break; |
---|
1076 | |
---|
1077 | case GB_INT: |
---|
1078 | result = GB_read_int(gbd); |
---|
1079 | break; |
---|
1080 | |
---|
1081 | case GB_FLOAT: |
---|
1082 | result = GB_read_float(gbd)+.5; |
---|
1083 | break; |
---|
1084 | |
---|
1085 | case GB_STRING: |
---|
1086 | result = atoi(GB_read_char_pntr(gbd)); |
---|
1087 | break; |
---|
1088 | |
---|
1089 | default: |
---|
1090 | error = cannot_use_fun4entry("GB_read_lossless_byte", gbd); |
---|
1091 | result = 0; |
---|
1092 | break; |
---|
1093 | } |
---|
1094 | |
---|
1095 | if (!error) error = GB_incur_error(); |
---|
1096 | return result; |
---|
1097 | } |
---|
1098 | NOT4PERL int32_t GB_read_lossless_int(GBDATA *gbd, GB_ERROR& error) { |
---|
1099 | /*! Reads an int32_t previously written with GB_write_lossless_int() |
---|
1100 | * @param gbd the DB field |
---|
1101 | * @param error result parameter (has to be NULp) |
---|
1102 | * @result is undefined if error got set; contains read value otherwise |
---|
1103 | */ |
---|
1104 | gb_assert(!error); |
---|
1105 | gb_assert(!GB_have_error()); |
---|
1106 | int32_t result; |
---|
1107 | switch (gbd->type()) { |
---|
1108 | case GB_INT: |
---|
1109 | result = GB_read_int(gbd); |
---|
1110 | break; |
---|
1111 | |
---|
1112 | case GB_STRING: |
---|
1113 | result = atoi(GB_read_char_pntr(gbd)); |
---|
1114 | break; |
---|
1115 | |
---|
1116 | default: |
---|
1117 | error = cannot_use_fun4entry("GB_read_lossless_int", gbd); |
---|
1118 | result = 0; |
---|
1119 | break; |
---|
1120 | } |
---|
1121 | |
---|
1122 | if (!error) error = GB_incur_error(); |
---|
1123 | return result; |
---|
1124 | } |
---|
1125 | NOT4PERL float GB_read_lossless_float(GBDATA *gbd, GB_ERROR& error) { |
---|
1126 | /*! Reads a float previously written with GB_write_lossless_float() |
---|
1127 | * @param gbd the DB field |
---|
1128 | * @param error result parameter (has to be NULp) |
---|
1129 | * @result is undefined if error got set; contains read value otherwise |
---|
1130 | */ |
---|
1131 | gb_assert(!error); |
---|
1132 | gb_assert(!GB_have_error()); |
---|
1133 | float result; |
---|
1134 | switch (gbd->type()) { |
---|
1135 | case GB_FLOAT: |
---|
1136 | result = GB_read_float(gbd); |
---|
1137 | break; |
---|
1138 | |
---|
1139 | case GB_STRING: |
---|
1140 | result = GB_atof(GB_read_char_pntr(gbd)); |
---|
1141 | break; |
---|
1142 | |
---|
1143 | default: |
---|
1144 | error = cannot_use_fun4entry("GB_read_lossless_float", gbd); |
---|
1145 | result = 0; |
---|
1146 | break; |
---|
1147 | } |
---|
1148 | |
---|
1149 | if (!error) error = GB_incur_error(); |
---|
1150 | return result; |
---|
1151 | } |
---|
1152 | |
---|
1153 | // ------------------------------------------------------------ |
---|
1154 | // array type access functions (intended for perl use) |
---|
1155 | |
---|
1156 | long GB_read_from_ints(GBDATA *gbd, long index) { // used by ../PERL_SCRIPTS/SAI/SAI.pm@read_from_ints |
---|
1157 | static GBDATA *last_gbd = NULp; |
---|
1158 | static long count = 0; |
---|
1159 | static GB_CUINT4 *i = NULp; |
---|
1160 | |
---|
1161 | if (gbd != last_gbd) { |
---|
1162 | count = GB_read_ints_count(gbd); |
---|
1163 | i = GB_read_ints_pntr(gbd); |
---|
1164 | last_gbd = gbd; |
---|
1165 | } |
---|
1166 | |
---|
1167 | if (index >= 0 && index < count) { |
---|
1168 | return i[index]; |
---|
1169 | } |
---|
1170 | return -1; |
---|
1171 | } |
---|
1172 | |
---|
1173 | double GB_read_from_floats(GBDATA *gbd, long index) { // @@@ unused |
---|
1174 | static GBDATA *last_gbd = NULp; |
---|
1175 | static long count = 0; |
---|
1176 | static GB_CFLOAT *f = NULp; |
---|
1177 | |
---|
1178 | if (gbd != last_gbd) { |
---|
1179 | count = GB_read_floats_count(gbd); |
---|
1180 | f = GB_read_floats_pntr(gbd); |
---|
1181 | last_gbd = gbd; |
---|
1182 | } |
---|
1183 | |
---|
1184 | if (index >= 0 && index < count) { |
---|
1185 | return f[index]; |
---|
1186 | } |
---|
1187 | return -1; |
---|
1188 | } |
---|
1189 | |
---|
1190 | // ------------------- |
---|
1191 | // write data |
---|
1192 | |
---|
1193 | static void gb_do_callbacks(GBDATA *gbd) { |
---|
1194 | gb_assert(GB_MAIN(gbd)->get_transaction_level() < 0); // only use in NO_TRANSACTION_MODE! |
---|
1195 | |
---|
1196 | while (gbd) { |
---|
1197 | GBDATA *gbdn = GB_get_father(gbd); |
---|
1198 | gb_callback_list *cbl = gbd->get_callbacks(); |
---|
1199 | if (cbl && cbl->call(gbd, GB_CB_CHANGED)) { |
---|
1200 | gb_remove_callbacks_marked_for_deletion(gbd); |
---|
1201 | } |
---|
1202 | gbd = gbdn; |
---|
1203 | } |
---|
1204 | } |
---|
1205 | |
---|
1206 | #define GB_DO_CALLBACKS(gbd) do { if (GB_MAIN(gbd)->get_transaction_level() < 0) gb_do_callbacks(gbd); } while (0) |
---|
1207 | |
---|
1208 | GB_ERROR GB_write_byte(GBDATA *gbd, int i) { |
---|
1209 | GB_TEST_WRITE(gbd, GB_BYTE, "GB_write_byte"); |
---|
1210 | GBENTRY *gbe = gbd->as_entry(); |
---|
1211 | if (gbe->info.i != i) { |
---|
1212 | gb_save_extern_data_in_ts(gbe); |
---|
1213 | gbe->info.i = i & 0xff; |
---|
1214 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
---|
1215 | GB_DO_CALLBACKS(gbe); |
---|
1216 | } |
---|
1217 | return NULp; |
---|
1218 | } |
---|
1219 | |
---|
1220 | GB_ERROR GB_write_int(GBDATA *gbd, long i) { |
---|
1221 | #if defined(ARB_64) |
---|
1222 | #if defined(WARN_TODO) |
---|
1223 | #warning GB_write_int should be GB_ERROR GB_write_int(GBDATA *gbd,int32_t i) |
---|
1224 | #endif |
---|
1225 | #endif |
---|
1226 | |
---|
1227 | GB_TEST_WRITE(gbd, GB_INT, "GB_write_int"); |
---|
1228 | if ((long)((int32_t)i) != i) { |
---|
1229 | gb_assert(0); |
---|
1230 | GB_warningf("Warning: 64bit incompatibility detected\nNo data written to '%s'\n", GB_get_db_path(gbd)); |
---|
1231 | return "GB_INT out of range (signed, 32bit)"; |
---|
1232 | } |
---|
1233 | GBENTRY *gbe = gbd->as_entry(); |
---|
1234 | if (gbe->info.i != (int32_t)i) { |
---|
1235 | gb_save_extern_data_in_ts(gbe); |
---|
1236 | gbe->info.i = i; |
---|
1237 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
---|
1238 | GB_DO_CALLBACKS(gbe); |
---|
1239 | } |
---|
1240 | return NULp; |
---|
1241 | } |
---|
1242 | |
---|
1243 | GB_ERROR GB_write_pointer(GBDATA *gbd, GBDATA *pointer) { |
---|
1244 | GB_TEST_WRITE(gbd, GB_POINTER, "GB_write_pointer"); |
---|
1245 | GBENTRY *gbe = gbd->as_entry(); |
---|
1246 | if (gbe->info.ptr != pointer) { |
---|
1247 | gb_save_extern_data_in_ts(gbe); |
---|
1248 | gbe->info.ptr = pointer; |
---|
1249 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
---|
1250 | GB_DO_CALLBACKS(gbe); |
---|
1251 | } |
---|
1252 | return NULp; |
---|
1253 | } |
---|
1254 | |
---|
1255 | GB_ERROR GB_write_float(GBDATA *gbd, float f) { |
---|
1256 | gb_assert(f == f); // !nan |
---|
1257 | GB_TEST_WRITE(gbd, GB_FLOAT, "GB_write_float"); |
---|
1258 | |
---|
1259 | if (GB_read_float(gbd) != f) { |
---|
1260 | GBENTRY *gbe = gbd->as_entry(); |
---|
1261 | gb_save_extern_data_in_ts(gbe); |
---|
1262 | |
---|
1263 | XDR xdrs; |
---|
1264 | xdrmem_create(&xdrs, &gbe->info.in.data[0], SIZOFINTERN, XDR_ENCODE); |
---|
1265 | xdr_float(&xdrs, &f); |
---|
1266 | xdr_destroy(&xdrs); |
---|
1267 | |
---|
1268 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
---|
1269 | GB_DO_CALLBACKS(gbe); |
---|
1270 | } |
---|
1271 | return NULp; |
---|
1272 | } |
---|
1273 | |
---|
1274 | inline void gb_save_extern_data_in_ts__and_uncache(GBENTRY *gbe) { |
---|
1275 | // order of the following commands is important: |
---|
1276 | // gb_save_extern_data_in_ts may recreate a cache-entry under the following conditions: |
---|
1277 | // - gbe is indexed AND |
---|
1278 | // - gbe is stored compressed (and compression really takes place) |
---|
1279 | // |
---|
1280 | // Calling these commands in reversed order (as done until [15622]) |
---|
1281 | // had the following effects: |
---|
1282 | // - writing modified data to an entry had no effect (cache still contained old value) |
---|
1283 | // - after saving and reloading the database, the modified value was effective |
---|
1284 | // |
---|
1285 | // Happened e.g. when copying an SAI (if dictionary compression occurred for SAI/name). See #742. |
---|
1286 | |
---|
1287 | gb_save_extern_data_in_ts(gbe); // Warning: might undo effect of gb_uncache if called afterwards |
---|
1288 | gb_uncache(gbe); |
---|
1289 | } |
---|
1290 | |
---|
1291 | GB_ERROR gb_write_compressed_pntr(GBENTRY *gbe, const char *s, long memsize, long stored_size) { |
---|
1292 | gb_save_extern_data_in_ts__and_uncache(gbe); |
---|
1293 | |
---|
1294 | gbe->flags.compressed_data = 1; |
---|
1295 | |
---|
1296 | gb_assert(!gbe->cache_index); // insert_data() will recreate the cache-entry (if entry is_indexed) |
---|
1297 | gbe->insert_data((char *)s, stored_size, (size_t)memsize); |
---|
1298 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
---|
1299 | |
---|
1300 | return NULp; |
---|
1301 | } |
---|
1302 | |
---|
1303 | int gb_get_compression_mask(GB_MAIN_TYPE *Main, GBQUARK key, int gb_type) { |
---|
1304 | gb_Key *ks = &Main->keys[key]; |
---|
1305 | int compression_mask; |
---|
1306 | |
---|
1307 | if (ks->gb_key_disabled) { |
---|
1308 | compression_mask = 0; |
---|
1309 | } |
---|
1310 | else { |
---|
1311 | if (!ks->gb_key) gb_load_single_key_data(Main->gb_main(), key); |
---|
1312 | compression_mask = gb_convert_type_2_compression_flags[gb_type] & ks->compression_mask; |
---|
1313 | } |
---|
1314 | |
---|
1315 | return compression_mask; |
---|
1316 | } |
---|
1317 | |
---|
1318 | GB_ERROR GB_write_pntr(GBDATA *gbd, const char *s, size_t bytes_size, size_t stored_size) { |
---|
1319 | // 'bytes_size' is the size of what 's' points to. |
---|
1320 | // 'stored_size' is the size-information written into the DB |
---|
1321 | // |
---|
1322 | // e.g. for strings : stored_size = bytes_size-1, cause stored_size is string len, |
---|
1323 | // but bytes_size includes zero byte. |
---|
1324 | |
---|
1325 | GBENTRY *gbe = gbd->as_entry(); |
---|
1326 | GB_MAIN_TYPE *Main = GB_MAIN(gbe); |
---|
1327 | GBQUARK key = GB_KEY_QUARK(gbe); |
---|
1328 | GB_TYPES type = gbe->type(); |
---|
1329 | |
---|
1330 | gb_assert(implicated(type == GB_STRING, stored_size == bytes_size-1)); // size constraint for strings not fulfilled! |
---|
1331 | |
---|
1332 | gb_save_extern_data_in_ts__and_uncache(gbe); |
---|
1333 | |
---|
1334 | int compression_mask = gb_get_compression_mask(Main, key, type); |
---|
1335 | |
---|
1336 | const char *d; |
---|
1337 | size_t memsize; |
---|
1338 | if (compression_mask) { |
---|
1339 | d = gb_compress_data(gbe, key, s, bytes_size, &memsize, compression_mask, false); |
---|
1340 | } |
---|
1341 | else { |
---|
1342 | d = NULp; |
---|
1343 | } |
---|
1344 | if (d) { |
---|
1345 | gbe->flags.compressed_data = 1; |
---|
1346 | } |
---|
1347 | else { |
---|
1348 | d = s; |
---|
1349 | gbe->flags.compressed_data = 0; |
---|
1350 | memsize = bytes_size; |
---|
1351 | } |
---|
1352 | |
---|
1353 | gb_assert(!gbe->cache_index); // insert_data() will recreate the cache-entry (if entry is_indexed) |
---|
1354 | gbe->insert_data(d, stored_size, memsize); |
---|
1355 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
---|
1356 | GB_DO_CALLBACKS(gbe); |
---|
1357 | |
---|
1358 | return NULp; |
---|
1359 | } |
---|
1360 | |
---|
1361 | GB_ERROR GB_write_string(GBDATA *gbd, const char *s) { |
---|
1362 | GBENTRY *gbe = gbd->as_entry(); |
---|
1363 | GB_TEST_WRITE(gbe, GB_STRING, "GB_write_string"); |
---|
1364 | GB_TEST_NON_BUFFER(s, "GB_write_string"); // compress would destroy the other buffer |
---|
1365 | |
---|
1366 | if (!s) s = ""; |
---|
1367 | size_t size = strlen(s); |
---|
1368 | |
---|
1369 | // no zero len strings allowed |
---|
1370 | if (gbe->memsize() && (size == gbe->size())) { |
---|
1371 | if (!strcmp(s, GB_read_pntr(gbe))) return NULp; |
---|
1372 | } |
---|
1373 | #if defined(DEBUG) && 0 |
---|
1374 | // check for error (in compression) |
---|
1375 | { |
---|
1376 | GB_ERROR error = GB_write_pntr(gbe, s, size+1, size); |
---|
1377 | if (!error) { |
---|
1378 | char *check = GB_read_string(gbe); |
---|
1379 | |
---|
1380 | gb_assert(check); |
---|
1381 | gb_assert(strcmp(check, s) == 0); |
---|
1382 | |
---|
1383 | free(check); |
---|
1384 | } |
---|
1385 | return error; |
---|
1386 | } |
---|
1387 | #else |
---|
1388 | return GB_write_pntr(gbe, s, size+1, size); |
---|
1389 | #endif // DEBUG |
---|
1390 | } |
---|
1391 | |
---|
1392 | GB_ERROR GB_write_bits(GBDATA *gbd, const char *bits, long size, const char *c_0) { |
---|
1393 | GBENTRY *gbe = gbd->as_entry(); |
---|
1394 | GB_TEST_WRITE(gbe, GB_BITS, "GB_write_bits"); |
---|
1395 | GB_TEST_NON_BUFFER(bits, "GB_write_bits"); // compress would destroy the other buffer |
---|
1396 | gb_save_extern_data_in_ts(gbe); |
---|
1397 | |
---|
1398 | long memsize; |
---|
1399 | char *d = gb_compress_bits(bits, size, (const unsigned char *)c_0, &memsize); |
---|
1400 | |
---|
1401 | gbe->flags.compressed_data = 1; |
---|
1402 | gbe->insert_data(d, size, memsize); |
---|
1403 | gb_touch_entry(gbe, GB_NORMAL_CHANGE); |
---|
1404 | GB_DO_CALLBACKS(gbe); |
---|
1405 | return NULp; |
---|
1406 | } |
---|
1407 | |
---|
1408 | GB_ERROR GB_write_bytes(GBDATA *gbd, const char *s, long size) { |
---|
1409 | GB_TEST_WRITE(gbd, GB_BYTES, "GB_write_bytes"); |
---|
1410 | return GB_write_pntr(gbd, s, size, size); |
---|
1411 | } |
---|
1412 | |
---|
1413 | GB_ERROR GB_write_ints(GBDATA *gbd, const GB_UINT4 *i, long size) { |
---|
1414 | GB_TEST_WRITE(gbd, GB_INTS, "GB_write_ints"); |
---|
1415 | GB_TEST_NON_BUFFER((char *)i, "GB_write_ints"); // compress would destroy the other buffer |
---|
1416 | |
---|
1417 | if (0x01020304 != htonl((GB_UINT4)0x01020304)) { |
---|
1418 | long j; |
---|
1419 | char *buf2 = GB_give_other_buffer((char *)i, size<<2); |
---|
1420 | GB_UINT4 *s = (GB_UINT4 *)i; |
---|
1421 | GB_UINT4 *d = (GB_UINT4 *)buf2; |
---|
1422 | |
---|
1423 | for (j=size; j; j--) { |
---|
1424 | *(d++) = htonl(*(s++)); |
---|
1425 | } |
---|
1426 | i = (GB_UINT4 *)buf2; |
---|
1427 | } |
---|
1428 | return GB_write_pntr(gbd, (char *)i, size* 4 /* sizeof(long4) */, size); |
---|
1429 | } |
---|
1430 | |
---|
1431 | GB_ERROR GB_write_floats(GBDATA *gbd, const float *f, long size) { |
---|
1432 | long fullsize = size * sizeof(float); |
---|
1433 | GB_TEST_WRITE(gbd, GB_FLOATS, "GB_write_floats"); |
---|
1434 | GB_TEST_NON_BUFFER((char *)f, "GB_write_floats"); // compress would destroy the other buffer |
---|
1435 | |
---|
1436 | { |
---|
1437 | XDR xdrs; |
---|
1438 | long i; |
---|
1439 | char *buf2 = GB_give_other_buffer((char *)f, fullsize); |
---|
1440 | float *s = (float *)f; |
---|
1441 | |
---|
1442 | xdrmem_create(&xdrs, buf2, (int)fullsize, XDR_ENCODE); |
---|
1443 | for (i=size; i; i--) { |
---|
1444 | xdr_float(&xdrs, s); |
---|
1445 | s++; |
---|
1446 | } |
---|
1447 | xdr_destroy (&xdrs); |
---|
1448 | f = (float*)(void*)buf2; |
---|
1449 | } |
---|
1450 | return GB_write_pntr(gbd, (char *)f, size*sizeof(float), size); |
---|
1451 | } |
---|
1452 | |
---|
1453 | GB_ERROR GB_write_autoconv_string(GBDATA *gbd, const char *val) { |
---|
1454 | /*! writes data to database field using automatic conversion. |
---|
1455 | * Warning: Conversion may cause silent data-loss! |
---|
1456 | * (e.g. writing "hello" to a numeric db-field results in zero content) |
---|
1457 | * |
---|
1458 | * Writing back the unmodified(!) result of GB_read_as_string will not cause data loss. |
---|
1459 | * |
---|
1460 | * Consider using the GB_write_lossless_...() functions below (and their counterparts GB_read_lossless_...()). |
---|
1461 | * |
---|
1462 | * @@@ refer to GBT_write_int_converted / GBT_write_float_converted |
---|
1463 | */ |
---|
1464 | switch (gbd->type()) { |
---|
1465 | case GB_STRING: return GB_write_string(gbd, val); |
---|
1466 | case GB_BYTE: return GB_write_byte(gbd, atoi(val)); |
---|
1467 | case GB_INT: return GB_write_int(gbd, atoi(val)); |
---|
1468 | case GB_FLOAT: { |
---|
1469 | float f; |
---|
1470 | GB_ERROR error = GB_safe_atof(val, &f); |
---|
1471 | return error ? error : GB_write_float(gbd, f); |
---|
1472 | } |
---|
1473 | case GB_BITS: return GB_write_bits(gbd, val, strlen(val), "0"); |
---|
1474 | default: return GBS_global_string("Error: You cannot use GB_write_autoconv_string on this type of entry (%s)", GB_read_key_pntr(gbd)); |
---|
1475 | } |
---|
1476 | } |
---|
1477 | |
---|
1478 | GB_ERROR GB_write_lossless_byte(GBDATA *gbd, uint8_t byte) { |
---|
1479 | /*! Writes an uint8_t to a database field capable to store any value w/o loss. |
---|
1480 | * @return error otherwise |
---|
1481 | * The corresponding field filter is FIELD_FILTER_BYTE_WRITEABLE. |
---|
1482 | */ |
---|
1483 | switch (gbd->type()) { |
---|
1484 | case GB_BYTE: return GB_write_byte(gbd, byte); |
---|
1485 | case GB_INT: return GB_write_int(gbd, byte); |
---|
1486 | case GB_FLOAT: return GB_write_float(gbd, byte); |
---|
1487 | case GB_STRING: { |
---|
1488 | char buffer[4]; |
---|
1489 | sprintf(buffer, "%u", unsigned(byte)); |
---|
1490 | return GB_write_string(gbd, buffer); |
---|
1491 | } |
---|
1492 | |
---|
1493 | default: return cannot_use_fun4entry("GB_write_lossless_byte", gbd); |
---|
1494 | } |
---|
1495 | } |
---|
1496 | |
---|
1497 | GB_ERROR GB_write_lossless_int(GBDATA *gbd, int32_t i) { |
---|
1498 | /*! Writes an int32_t to a database field capable to store any value w/o loss. |
---|
1499 | * @return error otherwise |
---|
1500 | * The corresponding field filter is FIELD_FILTER_INT_WRITEABLE. |
---|
1501 | */ |
---|
1502 | |
---|
1503 | switch (gbd->type()) { |
---|
1504 | case GB_INT: return GB_write_int(gbd, i); |
---|
1505 | case GB_STRING: { |
---|
1506 | const int BUFSIZE = 30; |
---|
1507 | char buffer[BUFSIZE]; |
---|
1508 | #if defined(ASSERTION_USED) |
---|
1509 | int printed = |
---|
1510 | #endif |
---|
1511 | sprintf(buffer, "%i", i); |
---|
1512 | gb_assert(printed<BUFSIZE); |
---|
1513 | return GB_write_string(gbd, buffer); |
---|
1514 | } |
---|
1515 | |
---|
1516 | default: return cannot_use_fun4entry("GB_write_lossless_int", gbd); |
---|
1517 | } |
---|
1518 | } |
---|
1519 | |
---|
1520 | GB_ERROR GB_write_lossless_float(GBDATA *gbd, float f) { |
---|
1521 | /*! Writes a float to a database field capable to store any value w/o loss. |
---|
1522 | * @return error otherwise |
---|
1523 | * The corresponding field filter is FIELD_FILTER_FLOAT_WRITEABLE. |
---|
1524 | */ |
---|
1525 | |
---|
1526 | switch (gbd->type()) { |
---|
1527 | case GB_FLOAT: return GB_write_float(gbd, f); |
---|
1528 | case GB_STRING: { |
---|
1529 | const int BUFSIZE = 30; |
---|
1530 | char buffer[BUFSIZE]; |
---|
1531 | #if defined(ASSERTION_USED) |
---|
1532 | int printed = |
---|
1533 | #endif |
---|
1534 | sprintf(buffer, "%e", f); |
---|
1535 | gb_assert(printed<BUFSIZE); |
---|
1536 | return GB_write_string(gbd, buffer); |
---|
1537 | } |
---|
1538 | |
---|
1539 | default: return cannot_use_fun4entry("GB_write_lossless_float", gbd); |
---|
1540 | } |
---|
1541 | } |
---|
1542 | |
---|
1543 | // --------------------------- |
---|
1544 | // security functions |
---|
1545 | |
---|
1546 | int GB_read_security_write(GBDATA *gbd) { |
---|
1547 | GB_test_transaction(gbd); |
---|
1548 | return GB_GET_SECURITY_WRITE(gbd); |
---|
1549 | } |
---|
1550 | int GB_read_security_read(GBDATA *gbd) { |
---|
1551 | GB_test_transaction(gbd); |
---|
1552 | return GB_GET_SECURITY_READ(gbd); |
---|
1553 | } |
---|
1554 | int GB_read_security_delete(GBDATA *gbd) { |
---|
1555 | GB_test_transaction(gbd); |
---|
1556 | return GB_GET_SECURITY_DELETE(gbd); |
---|
1557 | } |
---|
1558 | GB_ERROR GB_write_security_write(GBDATA *gbd, unsigned long level) { |
---|
1559 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
1560 | GB_test_transaction(Main); |
---|
1561 | |
---|
1562 | if (GB_GET_SECURITY_WRITE(gbd)>Main->security_level) return gb_security_error(gbd); |
---|
1563 | if (GB_GET_SECURITY_WRITE(gbd) == level) return NULp; |
---|
1564 | GB_PUT_SECURITY_WRITE(gbd, level); |
---|
1565 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
---|
1566 | GB_DO_CALLBACKS(gbd); |
---|
1567 | return NULp; |
---|
1568 | } |
---|
1569 | GB_ERROR GB_write_security_read(GBDATA *gbd, unsigned long level) { // @@@ unused |
---|
1570 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
1571 | GB_test_transaction(Main); |
---|
1572 | if (GB_GET_SECURITY_WRITE(gbd)>Main->security_level) return gb_security_error(gbd); |
---|
1573 | if (GB_GET_SECURITY_READ(gbd) == level) return NULp; |
---|
1574 | GB_PUT_SECURITY_READ(gbd, level); |
---|
1575 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
---|
1576 | GB_DO_CALLBACKS(gbd); |
---|
1577 | return NULp; |
---|
1578 | } |
---|
1579 | GB_ERROR GB_write_security_delete(GBDATA *gbd, unsigned long level) { |
---|
1580 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
1581 | GB_test_transaction(Main); |
---|
1582 | if (GB_GET_SECURITY_WRITE(gbd)>Main->security_level) return gb_security_error(gbd); |
---|
1583 | if (GB_GET_SECURITY_DELETE(gbd) == level) return NULp; |
---|
1584 | GB_PUT_SECURITY_DELETE(gbd, level); |
---|
1585 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
---|
1586 | GB_DO_CALLBACKS(gbd); |
---|
1587 | return NULp; |
---|
1588 | } |
---|
1589 | GB_ERROR GB_write_security_levels(GBDATA *gbd, unsigned long readlevel, unsigned long writelevel, unsigned long deletelevel) { // @@@ unused |
---|
1590 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
1591 | GB_test_transaction(Main); |
---|
1592 | if (GB_GET_SECURITY_WRITE(gbd)>Main->security_level) return gb_security_error(gbd); |
---|
1593 | GB_PUT_SECURITY_WRITE(gbd, writelevel); |
---|
1594 | GB_PUT_SECURITY_READ(gbd, readlevel); |
---|
1595 | GB_PUT_SECURITY_DELETE(gbd, deletelevel); |
---|
1596 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
---|
1597 | GB_DO_CALLBACKS(gbd); |
---|
1598 | return NULp; |
---|
1599 | } |
---|
1600 | |
---|
1601 | void GB_change_my_security(GBDATA *gbd, int level) { |
---|
1602 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
1603 | Main->security_level = level<0 ? 0 : (level>7 ? 7 : level); |
---|
1604 | } |
---|
1605 | |
---|
1606 | int GB_securityLevel::whats_my_security() const { |
---|
1607 | GB_MAIN_TYPE *Main = GB_MAIN(gbdata); |
---|
1608 | return Main->security_level; |
---|
1609 | } |
---|
1610 | void GB_securityLevel::change_my_security(int level) { |
---|
1611 | GB_change_my_security(gbdata, level); |
---|
1612 | } |
---|
1613 | |
---|
1614 | // ------------------------ |
---|
1615 | // Key information |
---|
1616 | |
---|
1617 | GB_TYPES GB_read_type(GBDATA *gbd) { |
---|
1618 | GB_test_transaction(gbd); |
---|
1619 | return gbd->type(); |
---|
1620 | } |
---|
1621 | |
---|
1622 | bool GB_is_container(GBDATA *gbd) { |
---|
1623 | return gbd && gbd->is_container(); |
---|
1624 | } |
---|
1625 | |
---|
1626 | char *GB_read_key(GBDATA *gbd) { |
---|
1627 | return ARB_strdup(GB_read_key_pntr(gbd)); |
---|
1628 | } |
---|
1629 | |
---|
1630 | GB_CSTR GB_read_key_pntr(GBDATA *gbd) { |
---|
1631 | GB_CSTR k; |
---|
1632 | GB_test_transaction(gbd); |
---|
1633 | k = GB_KEY(gbd); |
---|
1634 | if (!k) k = GBS_global_string("<invalid key (quark=%i)>", GB_KEY_QUARK(gbd)); |
---|
1635 | return k; |
---|
1636 | } |
---|
1637 | |
---|
1638 | GB_CSTR gb_read_key_pntr(GBDATA *gbd) { |
---|
1639 | return GB_KEY(gbd); |
---|
1640 | } |
---|
1641 | |
---|
1642 | GBQUARK gb_find_or_create_quark(GB_MAIN_TYPE *Main, const char *key) { |
---|
1643 | //! @return existing or newly created quark for 'key' |
---|
1644 | GBQUARK quark = key2quark(Main, key); |
---|
1645 | if (!quark) { |
---|
1646 | if (!key[0]) GBK_terminate("Attempt to create quark from empty key"); |
---|
1647 | quark = gb_create_key(Main, key, true); |
---|
1648 | } |
---|
1649 | return quark; |
---|
1650 | } |
---|
1651 | |
---|
1652 | GBQUARK gb_find_or_create_NULL_quark(GB_MAIN_TYPE *Main, const char *key) { |
---|
1653 | // similar to gb_find_or_create_quark, |
---|
1654 | // but if 'key' is NULp, quark 0 will be returned. |
---|
1655 | // |
---|
1656 | // Use this function with care. |
---|
1657 | // |
---|
1658 | // Known good use: |
---|
1659 | // - create main entry and its dummy father via gb_make_container() |
---|
1660 | |
---|
1661 | return key ? gb_find_or_create_quark(Main, key) : 0; |
---|
1662 | } |
---|
1663 | |
---|
1664 | GBQUARK GB_find_existing_quark(GBDATA *gbd, const char *key) { |
---|
1665 | //! @return existing quark for 'key' (-1 if key is NULp, 0 if key is unknown) |
---|
1666 | return key2quark(GB_MAIN(gbd), key); |
---|
1667 | } |
---|
1668 | |
---|
1669 | GBQUARK GB_find_or_create_quark(GBDATA *gbd, const char *key) { |
---|
1670 | //! @return existing or newly created quark for 'key' |
---|
1671 | return gb_find_or_create_quark(GB_MAIN(gbd), key); |
---|
1672 | } |
---|
1673 | |
---|
1674 | |
---|
1675 | // --------------------------------------------- |
---|
1676 | |
---|
1677 | GBQUARK GB_get_quark(GBDATA *gbd) { |
---|
1678 | return GB_KEY_QUARK(gbd); |
---|
1679 | } |
---|
1680 | |
---|
1681 | bool GB_has_key(GBDATA *gbd, const char *key) { |
---|
1682 | GBQUARK quark = GB_find_existing_quark(gbd, key); |
---|
1683 | return quark && (quark == GB_get_quark(gbd)); |
---|
1684 | } |
---|
1685 | |
---|
1686 | // --------------------------------------------- |
---|
1687 | |
---|
1688 | long GB_read_clock(GBDATA *gbd) { |
---|
1689 | if (GB_ARRAY_FLAGS(gbd).changed) return GB_MAIN(gbd)->clock; |
---|
1690 | return gbd->update_date(); |
---|
1691 | } |
---|
1692 | |
---|
1693 | // --------------------------------------------- |
---|
1694 | // Get and check the database hierarchy |
---|
1695 | |
---|
1696 | GBDATA *GB_get_father(GBDATA *gbd) { |
---|
1697 | // Get the father of an entry |
---|
1698 | GB_test_transaction(gbd); |
---|
1699 | return gbd->get_father(); |
---|
1700 | } |
---|
1701 | |
---|
1702 | GBDATA *GB_get_grandfather(GBDATA *gbd) { |
---|
1703 | GB_test_transaction(gbd); |
---|
1704 | |
---|
1705 | GBDATA *gb_grandpa = GB_FATHER(gbd); |
---|
1706 | if (gb_grandpa) { |
---|
1707 | gb_grandpa = GB_FATHER(gb_grandpa); |
---|
1708 | if (gb_grandpa && !GB_FATHER(gb_grandpa)) gb_grandpa = NULp; // never return dummy_father of root container |
---|
1709 | } |
---|
1710 | return gb_grandpa; |
---|
1711 | } |
---|
1712 | |
---|
1713 | // Get the root entry (gb_main) |
---|
1714 | GBDATA *GB_get_root(GBDATA *gbd) { return GB_MAIN(gbd)->gb_main(); } |
---|
1715 | GBCONTAINER *gb_get_root(GBENTRY *gbe) { return GB_MAIN(gbe)->root_container; } |
---|
1716 | GBCONTAINER *gb_get_root(GBCONTAINER *gbc) { return GB_MAIN(gbc)->root_container; } |
---|
1717 | |
---|
1718 | bool GB_is_ancestor_of(GBDATA *gb_ancestor, GBDATA *gb_descendant) { |
---|
1719 | // Test whether 'gb_descendant' is a subentry of 'gb_ancestor'. |
---|
1720 | // Note: returns false if gb_descendant == gb_ancestor! |
---|
1721 | |
---|
1722 | GB_test_transaction(gb_descendant); |
---|
1723 | if (gb_ancestor->is_container()) { // otherwise it contains nothing! |
---|
1724 | for (GBDATA *gb_up = gb_descendant->get_father(); |
---|
1725 | gb_up; |
---|
1726 | gb_up = gb_up->get_father()) |
---|
1727 | { |
---|
1728 | if (gb_up == gb_ancestor) return true; |
---|
1729 | } |
---|
1730 | } |
---|
1731 | return false; |
---|
1732 | } |
---|
1733 | |
---|
1734 | // -------------------------- |
---|
1735 | // create and rename |
---|
1736 | |
---|
1737 | GBENTRY *gb_create(GBCONTAINER *father, const char *key, GB_TYPES type) { |
---|
1738 | GBENTRY *gbe = gb_make_entry(father, key, -1, 0, type); |
---|
1739 | gb_touch_header(GB_FATHER(gbe)); |
---|
1740 | gb_touch_entry(gbe, GB_CREATED); |
---|
1741 | |
---|
1742 | gb_assert(GB_ARRAY_FLAGS(gbe).changed < GB_DELETED); // happens sometimes -> needs debugging |
---|
1743 | |
---|
1744 | return gbe; |
---|
1745 | } |
---|
1746 | |
---|
1747 | GBCONTAINER *gb_create_container(GBCONTAINER *father, const char *key) { |
---|
1748 | // Create a container, do not check anything |
---|
1749 | GBCONTAINER *gbc = gb_make_container(father, key, -1, 0); |
---|
1750 | gb_touch_header(GB_FATHER(gbc)); |
---|
1751 | gb_touch_entry(gbc, GB_CREATED); |
---|
1752 | return gbc; |
---|
1753 | } |
---|
1754 | |
---|
1755 | GBDATA *GB_create(GBDATA *father, const char *key, GB_TYPES type) { |
---|
1756 | /*! Create a DB entry |
---|
1757 | * |
---|
1758 | * @param father container to create DB field in |
---|
1759 | * @param key name of field |
---|
1760 | * @param type field type |
---|
1761 | * |
---|
1762 | * @return |
---|
1763 | * - created DB entry |
---|
1764 | * - NULp on failure (error is exported then) |
---|
1765 | * |
---|
1766 | * @see GB_create_container() |
---|
1767 | */ |
---|
1768 | |
---|
1769 | gb_assert(!GB_have_error()); // illegal to enter this function when an error is exported! |
---|
1770 | |
---|
1771 | if (GB_ERROR keyerr = GB_check_key(key)) { |
---|
1772 | GB_export_error(keyerr); |
---|
1773 | return NULp; |
---|
1774 | } |
---|
1775 | |
---|
1776 | if (type == GB_DB) { |
---|
1777 | gb_assert(type != GB_DB); // you like to use GB_create_container! |
---|
1778 | GB_export_error("GB_create: can't create containers"); |
---|
1779 | return NULp; |
---|
1780 | } |
---|
1781 | |
---|
1782 | if (!father) { |
---|
1783 | GB_internal_errorf("GB_create error in GB_create:\nno father (key = '%s')", key); |
---|
1784 | return NULp; |
---|
1785 | } |
---|
1786 | GB_test_transaction(father); |
---|
1787 | if (father->is_entry()) { |
---|
1788 | GB_export_errorf("while creating '%s': father (%s) is not of GB_DB type (%i)", |
---|
1789 | key, GB_read_key_pntr(father), father->type()); |
---|
1790 | return NULp; |
---|
1791 | } |
---|
1792 | |
---|
1793 | if (type == GB_POINTER) { |
---|
1794 | if (!GB_in_temporary_branch(father)) { |
---|
1795 | GB_export_error("GB_create: pointers only allowed in temporary branches"); |
---|
1796 | return NULp; |
---|
1797 | } |
---|
1798 | } |
---|
1799 | |
---|
1800 | return gb_create(father->expect_container(), key, type); |
---|
1801 | } |
---|
1802 | |
---|
1803 | GBDATA *GB_create_container(GBDATA *father, const char *key) { |
---|
1804 | /*! Create a new DB container |
---|
1805 | * |
---|
1806 | * @param father parent container |
---|
1807 | * @param key name of created container |
---|
1808 | * |
---|
1809 | * @return |
---|
1810 | * - created container |
---|
1811 | * - NULp on failure (error is exported then) |
---|
1812 | * |
---|
1813 | * @see GB_create() |
---|
1814 | */ |
---|
1815 | |
---|
1816 | gb_assert(!GB_have_error()); // illegal to enter this function when an error is exported! |
---|
1817 | |
---|
1818 | if (GB_ERROR keyerr = GB_check_key(key)) { |
---|
1819 | GB_export_error(keyerr); |
---|
1820 | return NULp; |
---|
1821 | } |
---|
1822 | |
---|
1823 | if ((*key == '\0')) { |
---|
1824 | GB_export_error("GB_create error: empty key"); |
---|
1825 | return NULp; |
---|
1826 | } |
---|
1827 | if (!father) { |
---|
1828 | GB_internal_errorf("GB_create error in GB_create:\nno father (key = '%s')", key); |
---|
1829 | return NULp; |
---|
1830 | } |
---|
1831 | |
---|
1832 | GB_test_transaction(father); |
---|
1833 | return gb_create_container(father->expect_container(), key); |
---|
1834 | } |
---|
1835 | |
---|
1836 | // ---------------------- |
---|
1837 | // recompression |
---|
1838 | |
---|
1839 | #if defined(WARN_TODO) |
---|
1840 | #warning rename gb_set_compression into gb_recompress (misleading name) |
---|
1841 | #endif |
---|
1842 | |
---|
1843 | static GB_ERROR gb_set_compression(GBDATA *source) { |
---|
1844 | GB_ERROR error = NULp; |
---|
1845 | GB_test_transaction(source); |
---|
1846 | |
---|
1847 | switch (source->type()) { |
---|
1848 | case GB_STRING: { |
---|
1849 | char *str = GB_read_string(source); |
---|
1850 | GB_write_string(source, ""); |
---|
1851 | GB_write_string(source, str); |
---|
1852 | free(str); |
---|
1853 | break; |
---|
1854 | } |
---|
1855 | case GB_BITS: |
---|
1856 | case GB_BYTES: |
---|
1857 | case GB_INTS: |
---|
1858 | case GB_FLOATS: |
---|
1859 | break; |
---|
1860 | case GB_DB: |
---|
1861 | for (GBDATA *gb_child = GB_child(source); gb_child && !error; gb_child = GB_nextChild(gb_child)) { |
---|
1862 | error = gb_set_compression(gb_child); |
---|
1863 | } |
---|
1864 | break; |
---|
1865 | default: |
---|
1866 | break; |
---|
1867 | } |
---|
1868 | return error; |
---|
1869 | } |
---|
1870 | |
---|
1871 | bool GB_allow_compression(GBDATA *gb_main, bool allow_compression) { |
---|
1872 | GB_MAIN_TYPE *Main = GB_MAIN(gb_main); |
---|
1873 | int prev_mask = Main->compression_mask; |
---|
1874 | Main->compression_mask = allow_compression ? -1 : 0; |
---|
1875 | |
---|
1876 | return prev_mask == 0 ? false : true; |
---|
1877 | } |
---|
1878 | |
---|
1879 | |
---|
1880 | GB_ERROR GB_delete(GBDATA*& source) { |
---|
1881 | /*! delete a database entry. |
---|
1882 | * MAY(!) set source to NULp. |
---|
1883 | */ |
---|
1884 | |
---|
1885 | GBDATA *gb_main; |
---|
1886 | |
---|
1887 | GB_test_transaction(source); |
---|
1888 | if (GB_GET_SECURITY_DELETE(source)>GB_MAIN(source)->security_level) { |
---|
1889 | return GBS_global_string("Security error: deleting entry '%s' not permitted", GB_read_key_pntr(source)); |
---|
1890 | } |
---|
1891 | |
---|
1892 | gb_main = GB_get_root(source); |
---|
1893 | |
---|
1894 | if (source->flags.compressed_data) { |
---|
1895 | bool was_allowed = GB_allow_compression(gb_main, false); |
---|
1896 | gb_set_compression(source); // write data w/o compression (otherwise GB_read_old_value... won't work) |
---|
1897 | GB_allow_compression(gb_main, was_allowed); |
---|
1898 | } |
---|
1899 | |
---|
1900 | { |
---|
1901 | GB_MAIN_TYPE *Main = GB_MAIN(source); |
---|
1902 | if (Main->get_transaction_level() < 0) { // no transaction mode |
---|
1903 | gb_delete_entry(source); |
---|
1904 | Main->call_pending_callbacks(); |
---|
1905 | } |
---|
1906 | else { |
---|
1907 | gb_touch_entry(source, GB_DELETED); |
---|
1908 | } |
---|
1909 | } |
---|
1910 | return NULp; |
---|
1911 | } |
---|
1912 | |
---|
1913 | GB_ERROR gb_delete_force(GBDATA *source) { |
---|
1914 | // delete always |
---|
1915 | gb_touch_entry(source, GB_DELETED); |
---|
1916 | return NULp; |
---|
1917 | } |
---|
1918 | |
---|
1919 | |
---|
1920 | // ------------------ |
---|
1921 | // Copy data |
---|
1922 | |
---|
1923 | enum CopyMode { |
---|
1924 | // bit values: |
---|
1925 | CM_DROP_PROTECTION = 1, // -> reset protection (use current default?). |
---|
1926 | CM_DROP_MARKS = 2, // -> clear marks of copied entries (other user flags are always dropped, e.g. query flag). this only affects containers! |
---|
1927 | CM_SKIP_TEMP = 4, // -> do not copy temporarily entries (and their subentries). |
---|
1928 | CM_DROP_TEMPSTATE = 8, // w/o effect if CM_SKIP_TEMP; otherwise clear temporary state of copied entries if set. |
---|
1929 | CM_OVERWRITE_EXISTING = 16, // -> overwrite existing sub-entries (otherwise keeps existing sub-entries + adds new entries) |
---|
1930 | |
---|
1931 | // convenience definitions: |
---|
1932 | CM_COPY_TRADITIONAL = CM_DROP_PROTECTION|CM_DROP_MARKS|CM_DROP_TEMPSTATE, // traditional behavior of GB_copy_dropProtectMarksAndTempstate |
---|
1933 | CM_COPY_WITHPROTECT = CM_COPY_TRADITIONAL & ~CM_DROP_PROTECTION, // like traditional, but dont drop protection (note: not dropping protection was the first attempt to fix copying) |
---|
1934 | CM_COPY_STANDARD = CM_SKIP_TEMP, |
---|
1935 | CM_COPY_FULL = 0, // copy everything |
---|
1936 | }; |
---|
1937 | |
---|
1938 | static GB_ERROR gb_copy_explicit(GBDATA *dest, GBDATA *source, CopyMode mode); |
---|
1939 | |
---|
1940 | static GBDATA *gb_clone_explicit(GBCONTAINER *gb_destCont, GBDATA *gb_source, CopyMode mode) { |
---|
1941 | /*! copy a clone of 'gb_source' into container 'gb_destCont' according to 'mode'. |
---|
1942 | * @return pointer to copy on success or NULp (in which case an error may be exported). |
---|
1943 | */ |
---|
1944 | GB_test_transaction(gb_source); |
---|
1945 | gb_assert(!GB_have_error()); // illegal to enter this function when an error is exported! |
---|
1946 | gb_assert(!gb_destCont->flags2.folded_container); // please unfold in caller! |
---|
1947 | |
---|
1948 | if (mode&CM_SKIP_TEMP && GB_is_temporary(gb_source)) return NULp; // skip temporaries (as GB_save does) |
---|
1949 | |
---|
1950 | const char *key = GB_read_key_pntr(gb_source); |
---|
1951 | GBDATA *gb_clone; |
---|
1952 | if (gb_source->is_container()) { |
---|
1953 | gb_assert(!GB_is_ancestor_of(gb_source, gb_destCont)); // (for performance reasons this has to be guaranteed by caller) |
---|
1954 | |
---|
1955 | if (strcmp(GB_SYSTEM_FOLDER, key) == 0) return NULp; // never ever clone system folder |
---|
1956 | |
---|
1957 | gb_clone = GB_create_container(gb_destCont, key); |
---|
1958 | if (gb_clone) gb_create_header_array(gb_clone->as_container(), gb_source->as_container()->d.size); |
---|
1959 | else GB_export_errorf("failed to clone container (Reason: %s)", GB_await_error()); |
---|
1960 | } |
---|
1961 | else gb_clone = GB_create(gb_destCont, key, gb_source->type()); |
---|
1962 | |
---|
1963 | if (gb_clone) { |
---|
1964 | // target is a basic field or an empty container -> behavior of normal copy and overlay-copy is identical |
---|
1965 | mode = CopyMode(mode&~CM_OVERWRITE_EXISTING); // -> drop overwrite mode |
---|
1966 | |
---|
1967 | GB_ERROR error = gb_copy_explicit(gb_clone, gb_source, mode); |
---|
1968 | if (error) { |
---|
1969 | IF_ASSERTION_USED(GB_ERROR derror =)GB_delete(gb_clone); |
---|
1970 | gb_assert(!derror); |
---|
1971 | gb_clone = NULp; |
---|
1972 | GB_export_error(error); |
---|
1973 | } |
---|
1974 | } |
---|
1975 | |
---|
1976 | gb_assert(contradicted(gb_clone, GB_have_error())); |
---|
1977 | return gb_clone; |
---|
1978 | } |
---|
1979 | |
---|
1980 | static GB_ERROR gb_copy_explicit(GBDATA *dest, GBDATA *source, CopyMode mode) { |
---|
1981 | GB_ERROR error = NULp; |
---|
1982 | GB_test_transaction(source); |
---|
1983 | |
---|
1984 | if (mode&CM_SKIP_TEMP) { |
---|
1985 | if (GB_is_temporary(source)) { |
---|
1986 | return "logic error: it's too late to skip copy of temporary entry"; // has to be done by caller! |
---|
1987 | } |
---|
1988 | #if defined(ASSERTION_USED) |
---|
1989 | bool overwriteTempTarget = GB_is_temporary(dest) && (mode&CM_OVERWRITE_EXISTING); |
---|
1990 | gb_assert(!overwriteTempTarget); // currently is permitted. may be problematic. |
---|
1991 | #endif |
---|
1992 | } |
---|
1993 | |
---|
1994 | GB_TYPES type = source->type(); |
---|
1995 | if (dest->type() != type) { |
---|
1996 | return GB_export_errorf("incompatible types in gb_copy_explicit (source %s:%u != %s:%u", |
---|
1997 | GB_read_key_pntr(source), type, GB_read_key_pntr(dest), dest->type()); |
---|
1998 | } |
---|
1999 | |
---|
2000 | switch (type) { |
---|
2001 | case GB_INT: |
---|
2002 | error = GB_write_int(dest, GB_read_int(source)); |
---|
2003 | break; |
---|
2004 | case GB_FLOAT: |
---|
2005 | error = GB_write_float(dest, GB_read_float(source)); |
---|
2006 | break; |
---|
2007 | case GB_BYTE: |
---|
2008 | error = GB_write_byte(dest, GB_read_byte(source)); |
---|
2009 | break; |
---|
2010 | case GB_STRING: // No local compression |
---|
2011 | error = GB_write_string(dest, GB_read_char_pntr(source)); |
---|
2012 | break; |
---|
2013 | case GB_OBSOLETE: |
---|
2014 | error = GB_set_temporary(dest); // exclude obsolete type from next save |
---|
2015 | break; |
---|
2016 | case GB_BITS: // only local compressions for the following types |
---|
2017 | case GB_BYTES: |
---|
2018 | case GB_INTS: |
---|
2019 | case GB_FLOATS: { |
---|
2020 | GBENTRY *source_entry = source->as_entry(); |
---|
2021 | GBENTRY *dest_entry = dest->as_entry(); |
---|
2022 | |
---|
2023 | gb_save_extern_data_in_ts(dest_entry); |
---|
2024 | dest_entry->insert_data(source_entry->data(), source_entry->size(), source_entry->memsize()); |
---|
2025 | |
---|
2026 | dest->flags.compressed_data = source->flags.compressed_data; |
---|
2027 | break; |
---|
2028 | } |
---|
2029 | case GB_DB: { |
---|
2030 | if (!dest->is_container()) { |
---|
2031 | GB_ERROR err = GB_export_errorf("GB_COPY Type conflict %s:%i != %s:%i", |
---|
2032 | GB_read_key_pntr(dest), dest->type(), GB_read_key_pntr(source), GB_DB); |
---|
2033 | GB_internal_error(err); |
---|
2034 | return err; |
---|
2035 | } |
---|
2036 | |
---|
2037 | GBCONTAINER *destc = dest->as_container(); |
---|
2038 | GBCONTAINER *sourcec = source->as_container(); |
---|
2039 | |
---|
2040 | if (!(mode&CM_DROP_MARKS)) GB_write_flag(destc, GB_read_flag(sourcec)); // preserve mark flags |
---|
2041 | |
---|
2042 | if (sourcec->flags2.folded_container) gb_unfold(sourcec, -1, -1); |
---|
2043 | if (destc->flags2.folded_container) gb_unfold(destc, 0, -1); |
---|
2044 | |
---|
2045 | gb_assert(!GB_is_ancestor_of(source, dest)); // (for performance reasons this has to be guaranteed by caller, use gb_copy_checked?) |
---|
2046 | |
---|
2047 | if (mode&CM_OVERWRITE_EXISTING) { // overlay childs of destination with childs of source |
---|
2048 | std::set<GBQUARK> keyHandled; |
---|
2049 | for (GBDATA *gb_child = GB_child(sourcec); gb_child && !error; gb_child = GB_nextChild(gb_child)) { |
---|
2050 | GBQUARK quark = GB_KEY_QUARK(gb_child); |
---|
2051 | if (keyHandled.find(quark) == keyHandled.end()) { // handle each quark once |
---|
2052 | GBDATA *gb_entry = gb_child; |
---|
2053 | const char *key = GB_KEY(gb_entry); |
---|
2054 | GBDATA *gb_exist = GB_entry(destc, key); |
---|
2055 | |
---|
2056 | while (gb_entry && gb_exist && !error) { |
---|
2057 | // @@@ skip over temporary entries here? |
---|
2058 | |
---|
2059 | // overlay subentries pairwise: |
---|
2060 | error = gb_copy_explicit(gb_exist, gb_entry, mode); |
---|
2061 | if (!error) { |
---|
2062 | gb_exist = GB_nextEntry(gb_exist); |
---|
2063 | gb_entry = GB_nextEntry(gb_entry); |
---|
2064 | } |
---|
2065 | } |
---|
2066 | |
---|
2067 | while (gb_entry && !error) { |
---|
2068 | error = GB_incur_error_if(!gb_clone_explicit(destc, gb_entry, mode)); |
---|
2069 | if (!error) gb_entry = GB_nextEntry(gb_entry); |
---|
2070 | } |
---|
2071 | |
---|
2072 | gb_assert(implicated(gb_entry, error)); |
---|
2073 | |
---|
2074 | keyHandled.insert(quark); |
---|
2075 | } |
---|
2076 | } |
---|
2077 | } |
---|
2078 | else { // copy all childs to destination |
---|
2079 | for (GBDATA *gb_child = GB_child(sourcec); gb_child && !error; gb_child = GB_nextChild(gb_child)) { |
---|
2080 | error = GB_incur_error_if(!gb_clone_explicit(destc, gb_child, mode)); |
---|
2081 | } |
---|
2082 | } |
---|
2083 | |
---|
2084 | destc->flags3 = sourcec->flags3; |
---|
2085 | break; |
---|
2086 | } |
---|
2087 | default: |
---|
2088 | error = GB_export_error("error in gb_copy_explicit: unhandled type"); |
---|
2089 | } |
---|
2090 | |
---|
2091 | if (!error) { |
---|
2092 | gb_touch_entry(dest, GB_NORMAL_CHANGE); |
---|
2093 | |
---|
2094 | dest->flags.security_read = source->flags.security_read; // (note: read security generally has no effect) |
---|
2095 | if (!(mode&CM_DROP_PROTECTION)) { // preserve protection |
---|
2096 | dest->flags.security_write = source->flags.security_write; |
---|
2097 | dest->flags.security_delete = source->flags.security_delete; |
---|
2098 | } |
---|
2099 | |
---|
2100 | if (!(mode&CM_DROP_TEMPSTATE)) { // preserve tempstate |
---|
2101 | bool source_is_temp = GB_is_temporary(source); |
---|
2102 | if (GB_is_temporary(dest) != source_is_temp) { |
---|
2103 | error = source_is_temp ? GB_set_temporary(dest) : GB_clear_temporary(dest); |
---|
2104 | } |
---|
2105 | } |
---|
2106 | } |
---|
2107 | return error; |
---|
2108 | } |
---|
2109 | |
---|
2110 | inline GB_ERROR gb_copy_checked(GBDATA *dest, GBDATA *source, CopyMode mode) { |
---|
2111 | /*! copies the content of 'source' into 'dest'. |
---|
2112 | * if 'dest' is a container, entries existing there will not be overwritten! |
---|
2113 | */ |
---|
2114 | if (GB_is_ancestor_of(source, dest)) { |
---|
2115 | return "infinite copy not permitted (destination may not be part of source)"; |
---|
2116 | } |
---|
2117 | return gb_copy_explicit(dest, source, mode); |
---|
2118 | } |
---|
2119 | |
---|
2120 | GB_ERROR GB_copy_dropProtectMarksAndTempstate(GBDATA *dest, GBDATA *source) { |
---|
2121 | /*! traditional but NOT RECOMMENDED copy flavour. |
---|
2122 | * |
---|
2123 | * resets protection (to current or to none?), |
---|
2124 | * clears all flags (including marks) and |
---|
2125 | * copies temporary entries, while clearing their temporary state (i.e. they may get saved to disk later) |
---|
2126 | * |
---|
2127 | * Will become obsolete! |
---|
2128 | */ |
---|
2129 | return gb_copy_checked(dest, source, CM_COPY_TRADITIONAL); |
---|
2130 | } |
---|
2131 | GB_ERROR GB_copy_dropMarksAndTempstate(GBDATA *dest, GBDATA *source) { |
---|
2132 | /*! first attempt to fix traditional GB_copy_dropProtectMarksAndTempstate. |
---|
2133 | * As well NOT RECOMMENDED! |
---|
2134 | * |
---|
2135 | * does correctly copy protection. |
---|
2136 | * clears marks + temporary state (see GB_copy_dropProtectMarksAndTempstate). |
---|
2137 | * |
---|
2138 | * Will become obsolete! |
---|
2139 | */ |
---|
2140 | return gb_copy_checked(dest, source, CM_COPY_WITHPROTECT); |
---|
2141 | } |
---|
2142 | GB_ERROR GB_copy_std(GBDATA *dest, GBDATA *source) { |
---|
2143 | /*! recommended copy flavour: skips temp, preserves protection and marks. |
---|
2144 | * deletes other flags (e.g. query flag). |
---|
2145 | * sub-entries existing in 'dest' will not get overwritten. |
---|
2146 | */ |
---|
2147 | return gb_copy_checked(dest, source, CM_COPY_STANDARD); |
---|
2148 | } |
---|
2149 | |
---|
2150 | GB_ERROR GB_copy_overlay(GBDATA *dest, GBDATA *source) { |
---|
2151 | /*! overlays 'dest' with 'source'. |
---|
2152 | * sub-entries existing in 'dest' will be overwritten! |
---|
2153 | * also applies to sub-containers. |
---|
2154 | */ |
---|
2155 | return gb_copy_checked(dest, source, CopyMode(CM_COPY_STANDARD|CM_OVERWRITE_EXISTING)); |
---|
2156 | } |
---|
2157 | |
---|
2158 | GB_ERROR GB_copy_full(GBDATA *dest, GBDATA *source) { |
---|
2159 | /*! complete copy (does also copy temporary entries) |
---|
2160 | */ |
---|
2161 | return gb_copy_checked(dest, source, CM_COPY_FULL); |
---|
2162 | } |
---|
2163 | |
---|
2164 | GBDATA *GB_clone(GBDATA *gb_destCont, GBDATA *gb_source) { |
---|
2165 | /*! copy a clone of 'gb_source' into container 'gb_destCont' (like GB_copy_std would do). |
---|
2166 | * @return pointer to clone (on success) or NULp (in which case an error MAY be exported). |
---|
2167 | */ |
---|
2168 | |
---|
2169 | gb_assert(!GB_have_error()); // illegal to enter this function when an error is exported! |
---|
2170 | |
---|
2171 | GB_ERROR error = NULp; |
---|
2172 | if (gb_destCont->is_container()) { |
---|
2173 | GBCONTAINER *destc = gb_destCont->as_container(); |
---|
2174 | if (destc->flags2.folded_container) gb_unfold(destc, 0, -1); |
---|
2175 | if (!GB_is_ancestor_of(gb_source, gb_destCont)) { |
---|
2176 | return gb_clone_explicit(destc, gb_source, CM_COPY_STANDARD); |
---|
2177 | } |
---|
2178 | error = "GB_clone destination cannot be part of source."; |
---|
2179 | } |
---|
2180 | else { |
---|
2181 | error = "GB_clone destination has to be a container."; |
---|
2182 | } |
---|
2183 | GB_export_error(error); |
---|
2184 | return NULp; |
---|
2185 | } |
---|
2186 | |
---|
2187 | |
---|
2188 | static char *gb_stpcpy(char *dest, const char *source) { |
---|
2189 | while ((*dest++=*source++)) ; |
---|
2190 | return dest-1; // return pointer to last copied character (which is \0) |
---|
2191 | } |
---|
2192 | |
---|
2193 | char* GB_get_subfields(GBDATA *gbd) { |
---|
2194 | /*! Get all subfield names |
---|
2195 | * |
---|
2196 | * @return all subfields of 'gbd' as ';'-separated heap-copy |
---|
2197 | * (first and last char of result is a ';') |
---|
2198 | */ |
---|
2199 | GB_test_transaction(gbd); |
---|
2200 | |
---|
2201 | char *result = NULp; |
---|
2202 | if (gbd->is_container()) { |
---|
2203 | GBCONTAINER *gbc = gbd->as_container(); |
---|
2204 | int result_length = 0; |
---|
2205 | |
---|
2206 | if (gbc->flags2.folded_container) { |
---|
2207 | gb_unfold(gbc, -1, -1); |
---|
2208 | } |
---|
2209 | |
---|
2210 | for (GBDATA *gbp = GB_child(gbd); gbp; gbp = GB_nextChild(gbp)) { |
---|
2211 | const char *key = GB_read_key_pntr(gbp); |
---|
2212 | int keylen = strlen(key); |
---|
2213 | |
---|
2214 | if (result) { |
---|
2215 | char *neu_result = ARB_alloc<char>(result_length+keylen+1+1); |
---|
2216 | |
---|
2217 | if (neu_result) { |
---|
2218 | char *p = gb_stpcpy(neu_result, result); |
---|
2219 | p = gb_stpcpy(p, key); |
---|
2220 | *p++ = ';'; |
---|
2221 | p[0] = 0; |
---|
2222 | |
---|
2223 | freeset(result, neu_result); |
---|
2224 | result_length += keylen+1; |
---|
2225 | } |
---|
2226 | else { |
---|
2227 | gb_assert(0); |
---|
2228 | } |
---|
2229 | } |
---|
2230 | else { |
---|
2231 | ARB_alloc(result, 1+keylen+1+1); |
---|
2232 | result[0] = ';'; |
---|
2233 | strcpy(result+1, key); |
---|
2234 | result[keylen+1] = ';'; |
---|
2235 | result[keylen+2] = 0; |
---|
2236 | result_length = keylen+2; |
---|
2237 | } |
---|
2238 | } |
---|
2239 | } |
---|
2240 | else { |
---|
2241 | result = ARB_strdup(";"); |
---|
2242 | } |
---|
2243 | |
---|
2244 | return result; |
---|
2245 | } |
---|
2246 | |
---|
2247 | // -------------------------- |
---|
2248 | // temporary entries |
---|
2249 | |
---|
2250 | GB_ERROR GB_set_temporary(GBDATA *gbd) { // goes to header: __ATTR__USERESULT |
---|
2251 | /*! if the temporary flag is set, then that entry (including all subentries) will not be saved |
---|
2252 | * @see GB_clear_temporary() and GB_is_temporary() |
---|
2253 | */ |
---|
2254 | |
---|
2255 | GB_ERROR error = NULp; |
---|
2256 | GB_test_transaction(gbd); |
---|
2257 | |
---|
2258 | if (GB_GET_SECURITY_DELETE(gbd)>GB_MAIN(gbd)->security_level) { |
---|
2259 | error = GBS_global_string("Security error in GB_set_temporary: %s", GB_read_key_pntr(gbd)); |
---|
2260 | } |
---|
2261 | else { |
---|
2262 | gbd->flags.temporary = 1; |
---|
2263 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
---|
2264 | } |
---|
2265 | RETURN_ERROR(error); |
---|
2266 | } |
---|
2267 | |
---|
2268 | GB_ERROR GB_clear_temporary(GBDATA *gbd) { // @@@ used in ptpan branch - do not remove |
---|
2269 | //! undo effect of GB_set_temporary() |
---|
2270 | |
---|
2271 | GB_test_transaction(gbd); |
---|
2272 | gbd->flags.temporary = 0; |
---|
2273 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
---|
2274 | return NULp; |
---|
2275 | } |
---|
2276 | |
---|
2277 | bool GB_is_temporary(GBDATA *gbd) { |
---|
2278 | //! @see GB_set_temporary() and GB_in_temporary_branch() |
---|
2279 | GB_test_transaction(gbd); |
---|
2280 | return (long)gbd->flags.temporary; |
---|
2281 | } |
---|
2282 | |
---|
2283 | bool GB_in_temporary_branch(GBDATA *gbd) { |
---|
2284 | /*! @return true, if 'gbd' is member of a temporary subtree, |
---|
2285 | * i.e. if GB_is_temporary(itself or any parent) |
---|
2286 | */ |
---|
2287 | |
---|
2288 | if (GB_is_temporary(gbd)) return true; |
---|
2289 | |
---|
2290 | GBDATA *gb_parent = GB_get_father(gbd); |
---|
2291 | if (!gb_parent) return false; |
---|
2292 | |
---|
2293 | return GB_in_temporary_branch(gb_parent); |
---|
2294 | } |
---|
2295 | |
---|
2296 | // --------------------- |
---|
2297 | // transactions |
---|
2298 | |
---|
2299 | GB_ERROR GB_MAIN_TYPE::initial_client_transaction() { |
---|
2300 | // the first client transaction ever |
---|
2301 | transaction_level = 1; |
---|
2302 | GB_ERROR error = gbcmc_init_transaction(root_container); |
---|
2303 | if (!error) ++clock; |
---|
2304 | return error; |
---|
2305 | } |
---|
2306 | |
---|
2307 | inline GB_ERROR GB_MAIN_TYPE::start_transaction() { |
---|
2308 | gb_assert(transaction_level == 0); |
---|
2309 | |
---|
2310 | transaction_level = 1; |
---|
2311 | aborted_transaction = 0; |
---|
2312 | |
---|
2313 | GB_ERROR error = NULp; |
---|
2314 | if (is_client()) { |
---|
2315 | error = gbcmc_begin_transaction(gb_main()); |
---|
2316 | if (!error) { |
---|
2317 | error = gb_commit_transaction_local_rek(gb_main_ref(), 0, NULp); // init structures |
---|
2318 | gb_untouch_children_and_me(root_container); |
---|
2319 | } |
---|
2320 | } |
---|
2321 | |
---|
2322 | if (!error) { |
---|
2323 | /* do all callbacks |
---|
2324 | * cb that change the db are no problem, because it's the beginning of a ta |
---|
2325 | */ |
---|
2326 | call_pending_callbacks(); |
---|
2327 | ++clock; |
---|
2328 | } |
---|
2329 | return error; |
---|
2330 | } |
---|
2331 | |
---|
2332 | inline GB_ERROR GB_MAIN_TYPE::begin_transaction() { |
---|
2333 | if (transaction_level>0) return GBS_global_string("attempt to start a NEW transaction (at transaction level %i)", transaction_level); |
---|
2334 | if (transaction_level == 0) return start_transaction(); |
---|
2335 | return NULp; // NO_TRANSACTION_MODE |
---|
2336 | } |
---|
2337 | |
---|
2338 | inline GB_ERROR GB_MAIN_TYPE::abort_transaction() { |
---|
2339 | if (transaction_level<=0) { |
---|
2340 | if (transaction_level<0) return "GB_abort_transaction: Attempt to abort transaction in no-transaction-mode"; |
---|
2341 | return "GB_abort_transaction: No transaction running"; |
---|
2342 | } |
---|
2343 | if (transaction_level>1) { |
---|
2344 | aborted_transaction = 1; |
---|
2345 | return pop_transaction(); |
---|
2346 | } |
---|
2347 | |
---|
2348 | gb_abort_transaction_local_rek(gb_main_ref()); |
---|
2349 | if (is_client()) { |
---|
2350 | GB_ERROR error = gbcmc_abort_transaction(gb_main()); |
---|
2351 | if (error) return error; |
---|
2352 | } |
---|
2353 | clock--; |
---|
2354 | call_pending_callbacks(); |
---|
2355 | transaction_level = 0; |
---|
2356 | gb_untouch_children_and_me(root_container); |
---|
2357 | return NULp; |
---|
2358 | } |
---|
2359 | |
---|
2360 | inline GB_ERROR GB_MAIN_TYPE::commit_transaction() { |
---|
2361 | GB_ERROR error = NULp; |
---|
2362 | GB_CHANGE flag; |
---|
2363 | |
---|
2364 | if (!transaction_level) { |
---|
2365 | return "commit_transaction: No transaction running"; |
---|
2366 | } |
---|
2367 | if (transaction_level>1) { |
---|
2368 | return GBS_global_string("attempt to commit at transaction level %i", transaction_level); |
---|
2369 | } |
---|
2370 | if (aborted_transaction) { |
---|
2371 | aborted_transaction = 0; |
---|
2372 | return abort_transaction(); |
---|
2373 | } |
---|
2374 | if (is_server()) { |
---|
2375 | char *error1 = gb_set_undo_sync(gb_main()); |
---|
2376 | while (1) { |
---|
2377 | flag = (GB_CHANGE)GB_ARRAY_FLAGS(gb_main()).changed; |
---|
2378 | if (!flag) break; // nothing to do |
---|
2379 | error = gb_commit_transaction_local_rek(gb_main_ref(), 0, NULp); |
---|
2380 | gb_untouch_children_and_me(root_container); |
---|
2381 | if (error) break; |
---|
2382 | call_pending_callbacks(); |
---|
2383 | } |
---|
2384 | gb_disable_undo(gb_main()); |
---|
2385 | if (error1) { |
---|
2386 | transaction_level = 0; |
---|
2387 | gb_assert(error); // maybe return error1? |
---|
2388 | return error; // @@@ huh? why not return error1 |
---|
2389 | } |
---|
2390 | } |
---|
2391 | else { |
---|
2392 | gb_disable_undo(gb_main()); |
---|
2393 | while (1) { |
---|
2394 | flag = (GB_CHANGE)GB_ARRAY_FLAGS(gb_main()).changed; |
---|
2395 | if (!flag) break; // nothing to do |
---|
2396 | |
---|
2397 | error = gbcmc_begin_sendupdate(gb_main()); if (error) break; |
---|
2398 | error = gb_commit_transaction_local_rek(gb_main_ref(), 1, NULp); if (error) break; |
---|
2399 | error = gbcmc_end_sendupdate(gb_main()); if (error) break; |
---|
2400 | |
---|
2401 | gb_untouch_children_and_me(root_container); |
---|
2402 | call_pending_callbacks(); |
---|
2403 | } |
---|
2404 | if (!error) error = gbcmc_commit_transaction(gb_main()); |
---|
2405 | |
---|
2406 | } |
---|
2407 | transaction_level = 0; |
---|
2408 | return error; |
---|
2409 | } |
---|
2410 | |
---|
2411 | inline GB_ERROR GB_MAIN_TYPE::push_transaction() { |
---|
2412 | if (transaction_level == 0) return start_transaction(); |
---|
2413 | if (transaction_level>0) ++transaction_level; |
---|
2414 | // transaction<0 is NO_TRANSACTION_MODE |
---|
2415 | return NULp; |
---|
2416 | } |
---|
2417 | |
---|
2418 | inline GB_ERROR GB_MAIN_TYPE::pop_transaction() { |
---|
2419 | if (transaction_level==0) return "attempt to pop nested transaction while none running"; |
---|
2420 | if (transaction_level<0) return NULp; // NO_TRANSACTION_MODE |
---|
2421 | if (transaction_level==1) return commit_transaction(); |
---|
2422 | transaction_level--; |
---|
2423 | return NULp; |
---|
2424 | } |
---|
2425 | |
---|
2426 | inline GB_ERROR GB_MAIN_TYPE::no_transaction() { |
---|
2427 | if (is_client()) return "Tried to disable transactions in a client"; |
---|
2428 | transaction_level = -1; |
---|
2429 | return NULp; |
---|
2430 | } |
---|
2431 | |
---|
2432 | GB_ERROR GB_MAIN_TYPE::send_update_to_server(GBDATA *gbd) { |
---|
2433 | GB_ERROR error = NULp; |
---|
2434 | |
---|
2435 | if (!transaction_level) error = "send_update_to_server: no transaction running"; |
---|
2436 | else if (is_server()) error = "send_update_to_server: only possible from clients (not from server itself)"; |
---|
2437 | else { |
---|
2438 | const gb_triggered_callback *chg_cbl_old = changeCBs.pending.get_tail(); |
---|
2439 | const gb_triggered_callback *del_cbl_old = deleteCBs.pending.get_tail(); |
---|
2440 | |
---|
2441 | error = gbcmc_begin_sendupdate(gb_main()); |
---|
2442 | if (!error) error = gb_commit_transaction_local_rek(gbd, 2, NULp); |
---|
2443 | if (!error) error = gbcmc_end_sendupdate(gb_main()); |
---|
2444 | |
---|
2445 | if (!error && |
---|
2446 | (chg_cbl_old != changeCBs.pending.get_tail() || |
---|
2447 | del_cbl_old != deleteCBs.pending.get_tail())) |
---|
2448 | { |
---|
2449 | error = "send_update_to_server triggered a callback (this is not allowed)"; |
---|
2450 | } |
---|
2451 | } |
---|
2452 | return error; |
---|
2453 | } |
---|
2454 | |
---|
2455 | // -------------------------------------- |
---|
2456 | // client transaction interface |
---|
2457 | |
---|
2458 | GB_ERROR GB_push_transaction(GBDATA *gbd) { |
---|
2459 | /*! start a transaction if no transaction is running. |
---|
2460 | * (otherwise only trace nested transactions) |
---|
2461 | * |
---|
2462 | * recommended transaction usage: |
---|
2463 | * |
---|
2464 | * \code |
---|
2465 | * GB_ERROR myFunc() { |
---|
2466 | * GB_ERROR error = GB_push_transaction(gbd); |
---|
2467 | * if (!error) { |
---|
2468 | * error = ...; |
---|
2469 | * } |
---|
2470 | * return GB_end_transaction(gbd, error); |
---|
2471 | * } |
---|
2472 | * |
---|
2473 | * void myFunc() { |
---|
2474 | * GB_ERROR error = GB_push_transaction(gbd); |
---|
2475 | * if (!error) { |
---|
2476 | * error = ...; |
---|
2477 | * } |
---|
2478 | * GB_end_transaction_show_error(gbd, error, aw_message); |
---|
2479 | * } |
---|
2480 | * \endcode |
---|
2481 | * |
---|
2482 | * @see GB_pop_transaction(), GB_end_transaction(), GB_begin_transaction() |
---|
2483 | */ |
---|
2484 | |
---|
2485 | return GB_MAIN(gbd)->push_transaction(); |
---|
2486 | } |
---|
2487 | |
---|
2488 | GB_ERROR GB_pop_transaction(GBDATA *gbd) { |
---|
2489 | //! commit a transaction started with GB_push_transaction() |
---|
2490 | return GB_MAIN(gbd)->pop_transaction(); |
---|
2491 | } |
---|
2492 | GB_ERROR GB_begin_transaction(GBDATA *gbd) { |
---|
2493 | /*! like GB_push_transaction(), |
---|
2494 | * but fails if there is already an transaction running. |
---|
2495 | * @see GB_commit_transaction() and GB_abort_transaction() |
---|
2496 | */ |
---|
2497 | return GB_MAIN(gbd)->begin_transaction(); |
---|
2498 | } |
---|
2499 | GB_ERROR GB_no_transaction(GBDATA *gbd) { // goes to header: __ATTR__USERESULT |
---|
2500 | return GB_MAIN(gbd)->no_transaction(); |
---|
2501 | } |
---|
2502 | |
---|
2503 | GB_ERROR GB_abort_transaction(GBDATA *gbd) { |
---|
2504 | /*! abort a running transaction, |
---|
2505 | * i.e. forget all changes made to DB inside the current transaction. |
---|
2506 | * |
---|
2507 | * May be called instead of GB_pop_transaction() or GB_commit_transaction() |
---|
2508 | * |
---|
2509 | * If a nested transactions got aborted, |
---|
2510 | * committing a surrounding transaction will silently abort it as well. |
---|
2511 | */ |
---|
2512 | return GB_MAIN(gbd)->abort_transaction(); |
---|
2513 | } |
---|
2514 | |
---|
2515 | GB_ERROR GB_commit_transaction(GBDATA *gbd) { |
---|
2516 | /*! commit a transaction started with GB_begin_transaction() |
---|
2517 | * |
---|
2518 | * commit changes made to DB. |
---|
2519 | * |
---|
2520 | * in case of nested transactions, this is equal to GB_pop_transaction() |
---|
2521 | */ |
---|
2522 | return GB_MAIN(gbd)->commit_transaction(); |
---|
2523 | } |
---|
2524 | |
---|
2525 | GB_ERROR GB_end_transaction(GBDATA *gbd, GB_ERROR error) { |
---|
2526 | /*! abort or commit transaction |
---|
2527 | * |
---|
2528 | * @ param error |
---|
2529 | * - if NULp commit transaction |
---|
2530 | * - else abort transaction |
---|
2531 | * |
---|
2532 | * always commits in no-transaction-mode |
---|
2533 | * |
---|
2534 | * @return error or transaction error |
---|
2535 | * @see GB_push_transaction() for example |
---|
2536 | */ |
---|
2537 | |
---|
2538 | if (GB_get_transaction_level(gbd)<0) { |
---|
2539 | ASSERT_RESULT(GB_ERROR, NULp, GB_pop_transaction(gbd)); |
---|
2540 | } |
---|
2541 | else { |
---|
2542 | if (error) GB_abort_transaction(gbd); |
---|
2543 | else error = GB_pop_transaction(gbd); |
---|
2544 | } |
---|
2545 | return error; |
---|
2546 | } |
---|
2547 | |
---|
2548 | void GB_end_transaction_show_error(GBDATA *gbd, GB_ERROR error, void (*error_handler)(GB_ERROR)) { |
---|
2549 | //! like GB_end_transaction(), but show error using 'error_handler' |
---|
2550 | error = GB_end_transaction(gbd, error); |
---|
2551 | if (error) error_handler(error); |
---|
2552 | } |
---|
2553 | |
---|
2554 | int GB_get_transaction_level(GBDATA *gbd) { |
---|
2555 | /*! @return transaction level |
---|
2556 | * <0 -> in no-transaction-mode (abort is impossible) |
---|
2557 | * 0 -> not in transaction |
---|
2558 | * 1 -> one single transaction |
---|
2559 | * 2, ... -> nested transactions |
---|
2560 | */ |
---|
2561 | return GB_MAIN(gbd)->get_transaction_level(); |
---|
2562 | } |
---|
2563 | |
---|
2564 | GB_ERROR GB_release(GBDATA *gbd) { |
---|
2565 | /*! free cached data in client. |
---|
2566 | * |
---|
2567 | * Warning: pointers into the freed region(s) will get invalid! |
---|
2568 | */ |
---|
2569 | GBCONTAINER *gbc; |
---|
2570 | GBDATA *gb; |
---|
2571 | int index; |
---|
2572 | GB_MAIN_TYPE *Main = GB_MAIN(gbd); |
---|
2573 | |
---|
2574 | GB_test_transaction(gbd); |
---|
2575 | if (Main->is_server()) return NULp; |
---|
2576 | if (GB_ARRAY_FLAGS(gbd).changed && !gbd->flags2.update_in_server) { |
---|
2577 | GB_ERROR error = Main->send_update_to_server(gbd); |
---|
2578 | if (error) return error; |
---|
2579 | } |
---|
2580 | if (gbd->type() != GB_DB) { |
---|
2581 | GB_ERROR error = GB_export_errorf("You cannot release non container (%s)", |
---|
2582 | GB_read_key_pntr(gbd)); |
---|
2583 | GB_internal_error(error); |
---|
2584 | return error; |
---|
2585 | } |
---|
2586 | if (gbd->flags2.folded_container) return NULp; |
---|
2587 | gbc = (GBCONTAINER *)gbd; |
---|
2588 | |
---|
2589 | for (index = 0; index < gbc->d.nheader; index++) { |
---|
2590 | if ((gb = GBCONTAINER_ELEM(gbc, index))) { |
---|
2591 | gb_delete_entry(gb); |
---|
2592 | } |
---|
2593 | } |
---|
2594 | |
---|
2595 | gbc->flags2.folded_container = 1; |
---|
2596 | Main->call_pending_callbacks(); |
---|
2597 | return NULp; |
---|
2598 | } |
---|
2599 | |
---|
2600 | int GB_nsons(GBDATA *gbd) { |
---|
2601 | /*! return number of child entries |
---|
2602 | * |
---|
2603 | * @@@ does this work in clients ? |
---|
2604 | */ |
---|
2605 | |
---|
2606 | return gbd->is_container() |
---|
2607 | ? gbd->as_container()->d.size |
---|
2608 | : 0; |
---|
2609 | } |
---|
2610 | |
---|
2611 | void GB_disable_quicksave(GBDATA *gbd, const char *reason) { |
---|
2612 | /*! Disable quicksaving database |
---|
2613 | * @param gbd any DB node |
---|
2614 | * @param reason why quicksaving is not allowed |
---|
2615 | */ |
---|
2616 | freedup(GB_MAIN(gbd)->qs.quick_save_disabled, reason); |
---|
2617 | } |
---|
2618 | |
---|
2619 | GB_ERROR GB_resort_data_base(GBDATA *gb_main, GBDATA **new_order_list, long listsize) { |
---|
2620 | { |
---|
2621 | long client_count = GB_read_clients(gb_main); |
---|
2622 | if (client_count<0) { |
---|
2623 | return "Sorry: this program is not the arbdb server, you cannot resort your data"; |
---|
2624 | } |
---|
2625 | if (client_count>0) { |
---|
2626 | // resort will do a big amount of client update callbacks => disallow clients here |
---|
2627 | bool called_from_macro = GB_inside_remote_action(gb_main); |
---|
2628 | if (!called_from_macro) { // accept macro clients |
---|
2629 | return GBS_global_string("There are %li clients (editors, tree programs) connected to this server.\n" |
---|
2630 | "You need to close these clients before you can run this operation.", |
---|
2631 | client_count); |
---|
2632 | } |
---|
2633 | } |
---|
2634 | } |
---|
2635 | |
---|
2636 | if (listsize <= 0) return NULp; |
---|
2637 | |
---|
2638 | GBCONTAINER *father = GB_FATHER(new_order_list[0]); |
---|
2639 | GB_disable_quicksave(gb_main, "some entries in the database got a new order"); |
---|
2640 | |
---|
2641 | gb_header_list *hl = GB_DATA_LIST_HEADER(father->d); |
---|
2642 | for (long new_index = 0; new_index< listsize; new_index++) { |
---|
2643 | long old_index = new_order_list[new_index]->index; |
---|
2644 | |
---|
2645 | if (old_index < new_index) { |
---|
2646 | GB_warningf("Warning at resort database: entry exists twice: %li and %li", |
---|
2647 | old_index, new_index); |
---|
2648 | } |
---|
2649 | else { |
---|
2650 | GBDATA *ogb = GB_HEADER_LIST_GBD(hl[old_index]); |
---|
2651 | GBDATA *ngb = GB_HEADER_LIST_GBD(hl[new_index]); |
---|
2652 | |
---|
2653 | gb_header_list h = hl[new_index]; |
---|
2654 | hl[new_index] = hl[old_index]; |
---|
2655 | hl[old_index] = h; // Warning: Relative Pointers are incorrect !!! |
---|
2656 | |
---|
2657 | SET_GB_HEADER_LIST_GBD(hl[old_index], ngb); |
---|
2658 | SET_GB_HEADER_LIST_GBD(hl[new_index], ogb); |
---|
2659 | |
---|
2660 | if (ngb) ngb->index = old_index; |
---|
2661 | if (ogb) ogb->index = new_index; |
---|
2662 | } |
---|
2663 | } |
---|
2664 | |
---|
2665 | gb_touch_entry(father, GB_NORMAL_CHANGE); |
---|
2666 | return NULp; |
---|
2667 | } |
---|
2668 | |
---|
2669 | GB_ERROR gb_resort_system_folder_to_top(GBCONTAINER *gb_main) { |
---|
2670 | if (GB_read_clients(gb_main)<0) { |
---|
2671 | return NULp; // we are not server |
---|
2672 | } |
---|
2673 | |
---|
2674 | GBDATA *gb_system = GB_entry(gb_main, GB_SYSTEM_FOLDER); |
---|
2675 | if (!gb_system) { |
---|
2676 | return GB_export_error("System databaseentry does not exist"); |
---|
2677 | } |
---|
2678 | |
---|
2679 | GBDATA *gb_first = GB_child(gb_main); |
---|
2680 | if (gb_first == gb_system) { |
---|
2681 | return NULp; |
---|
2682 | } |
---|
2683 | |
---|
2684 | int len = GB_number_of_subentries(gb_main); |
---|
2685 | GBDATA **new_order_list = ARB_calloc<GBDATA*>(len); |
---|
2686 | |
---|
2687 | new_order_list[0] = gb_system; |
---|
2688 | for (int i=1; i<len; i++) { |
---|
2689 | new_order_list[i] = gb_first; |
---|
2690 | do gb_first = GB_nextChild(gb_first); while (gb_first == gb_system); |
---|
2691 | } |
---|
2692 | |
---|
2693 | GB_ERROR error = GB_resort_data_base(gb_main, new_order_list, len); |
---|
2694 | free(new_order_list); |
---|
2695 | |
---|
2696 | return error; |
---|
2697 | } |
---|
2698 | |
---|
2699 | // ------------------------------ |
---|
2700 | // private(?) user flags |
---|
2701 | |
---|
2702 | STATIC_ASSERT_ANNOTATED(((GB_USERFLAG_ANY+1)&GB_USERFLAG_ANY) == 0, "not all bits set in GB_USERFLAG_ANY"); |
---|
2703 | |
---|
2704 | #if defined(ASSERTION_USED) |
---|
2705 | inline bool legal_user_bitmask(unsigned char bitmask) { |
---|
2706 | return bitmask>0 && bitmask<=GB_USERFLAG_ANY; |
---|
2707 | } |
---|
2708 | #endif |
---|
2709 | |
---|
2710 | inline gb_flag_types2& get_user_flags(GBDATA *gbd) { |
---|
2711 | return gbd->expect_container()->flags2; |
---|
2712 | } |
---|
2713 | |
---|
2714 | bool GB_user_flag(GBDATA *gbd, unsigned char user_bit) { |
---|
2715 | gb_assert(legal_user_bitmask(user_bit)); |
---|
2716 | return get_user_flags(gbd).user_bits & user_bit; |
---|
2717 | } |
---|
2718 | |
---|
2719 | void GB_raise_user_flag(GBDATA *gbd, unsigned char user_bit) { |
---|
2720 | gb_assert(legal_user_bitmask(user_bit)); |
---|
2721 | gb_flag_types2& flags = get_user_flags(gbd); |
---|
2722 | flags.user_bits |= user_bit; |
---|
2723 | } |
---|
2724 | void GB_clear_user_flag(GBDATA *gbd, unsigned char user_bit) { |
---|
2725 | gb_assert(legal_user_bitmask(user_bit)); |
---|
2726 | gb_flag_types2& flags = get_user_flags(gbd); |
---|
2727 | flags.user_bits &= (user_bit^GB_USERFLAG_ANY); |
---|
2728 | } |
---|
2729 | void GB_write_user_flag(GBDATA *gbd, unsigned char user_bit, bool state) { |
---|
2730 | (state ? GB_raise_user_flag : GB_clear_user_flag)(gbd, user_bit); |
---|
2731 | } |
---|
2732 | |
---|
2733 | |
---|
2734 | // ------------------------ |
---|
2735 | // mark DB entries |
---|
2736 | |
---|
2737 | void GB_write_flag(GBDATA *gbd, long flag) { |
---|
2738 | GBCONTAINER *gbc = gbd->expect_container(); |
---|
2739 | GB_MAIN_TYPE *Main = GB_MAIN(gbc); |
---|
2740 | |
---|
2741 | GB_test_transaction(Main); |
---|
2742 | |
---|
2743 | int ubit = Main->users[0]->userbit; |
---|
2744 | int prev = GB_ARRAY_FLAGS(gbc).flags; |
---|
2745 | gbc->flags.saved_flags = prev; |
---|
2746 | |
---|
2747 | if (flag) { |
---|
2748 | GB_ARRAY_FLAGS(gbc).flags |= ubit; |
---|
2749 | } |
---|
2750 | else { |
---|
2751 | GB_ARRAY_FLAGS(gbc).flags &= ~ubit; |
---|
2752 | } |
---|
2753 | if (prev != (int)GB_ARRAY_FLAGS(gbc).flags) { |
---|
2754 | gb_touch_entry(gbc, GB_NORMAL_CHANGE); |
---|
2755 | gb_touch_header(GB_FATHER(gbc)); |
---|
2756 | GB_DO_CALLBACKS(gbc); |
---|
2757 | } |
---|
2758 | } |
---|
2759 | |
---|
2760 | int GB_read_flag(GBDATA *gbd) { |
---|
2761 | GB_test_transaction(gbd); |
---|
2762 | if (GB_ARRAY_FLAGS(gbd).flags & GB_MAIN(gbd)->users[0]->userbit) return 1; |
---|
2763 | else return 0; |
---|
2764 | } |
---|
2765 | |
---|
2766 | void GB_touch(GBDATA *gbd) { |
---|
2767 | GB_test_transaction(gbd); |
---|
2768 | gb_touch_entry(gbd, GB_NORMAL_CHANGE); |
---|
2769 | GB_DO_CALLBACKS(gbd); |
---|
2770 | } |
---|
2771 | |
---|
2772 | |
---|
2773 | char GB_type_2_char(GB_TYPES type) { |
---|
2774 | const char *type2char = "-bcif-B-CIFlSS-%"; |
---|
2775 | return type2char[type]; |
---|
2776 | } |
---|
2777 | |
---|
2778 | void GB_print_debug_information(struct Unfixed_cb_parameter *, GBDATA *gb_main) { |
---|
2779 | GB_MAIN_TYPE *Main = GB_MAIN(gb_main); |
---|
2780 | GB_push_transaction(gb_main); |
---|
2781 | for (int i=0; i<Main->keycnt; i++) { |
---|
2782 | gb_Key& KEY = Main->keys[i]; |
---|
2783 | if (KEY.key) { |
---|
2784 | printf("%3i %20s nref %li\n", i, KEY.key, KEY.nref); |
---|
2785 | } |
---|
2786 | else { |
---|
2787 | printf(" %3i unused key, next free key = %li\n", i, KEY.next_free_key); |
---|
2788 | } |
---|
2789 | } |
---|
2790 | gbm_debug_mem(); |
---|
2791 | GB_pop_transaction(gb_main); |
---|
2792 | } |
---|
2793 | |
---|
2794 | static int GB_info_deep = 15; |
---|
2795 | |
---|
2796 | |
---|
2797 | static int gb_info(GBDATA *gbd, int deep) { |
---|
2798 | if (!gbd) { printf("NULp\n"); return -1; } |
---|
2799 | GB_push_transaction(gbd); |
---|
2800 | |
---|
2801 | GB_TYPES type = gbd->type(); |
---|
2802 | |
---|
2803 | if (deep) { |
---|
2804 | printf(" "); |
---|
2805 | } |
---|
2806 | |
---|
2807 | printf("(GBDATA*)0x%lx (GBCONTAINER*)0x%lx ", (long)gbd, (long)gbd); |
---|
2808 | |
---|
2809 | if (gbd->rel_father==0) { printf("father=NULp\n"); return -1; } |
---|
2810 | |
---|
2811 | GBCONTAINER *gbc; |
---|
2812 | GB_MAIN_TYPE *Main; |
---|
2813 | if (type==GB_DB) { gbc = gbd->as_container(); Main = GBCONTAINER_MAIN(gbc); } |
---|
2814 | else { gbc = NULp; Main = GB_MAIN(gbd); } |
---|
2815 | |
---|
2816 | if (!Main) { printf("Oops - I have no main entry!!!\n"); return -1; } |
---|
2817 | if (gbd==Main->dummy_father) { printf("dummy_father!\n"); return -1; } |
---|
2818 | |
---|
2819 | printf("%10s Type '%c' ", GB_read_key_pntr(gbd), GB_type_2_char(type)); |
---|
2820 | |
---|
2821 | switch (type) { |
---|
2822 | case GB_DB: { |
---|
2823 | int size = gbc->d.size; |
---|
2824 | printf("Size %i nheader %i hmemsize %i", gbc->d.size, gbc->d.nheader, gbc->d.headermemsize); |
---|
2825 | printf(" father=(GBDATA*)0x%lx\n", (long)GB_FATHER(gbd)); |
---|
2826 | if (size < GB_info_deep) { |
---|
2827 | int index; |
---|
2828 | gb_header_list *header; |
---|
2829 | |
---|
2830 | header = GB_DATA_LIST_HEADER(gbc->d); |
---|
2831 | for (index = 0; index < gbc->d.nheader; index++) { |
---|
2832 | GBDATA *gb_sub = GB_HEADER_LIST_GBD(header[index]); |
---|
2833 | GBQUARK quark = header[index].flags.key_quark; |
---|
2834 | printf("\t\t%10s (GBDATA*)0x%lx (GBCONTAINER*)0x%lx\n", quark2key(Main, quark), (long)gb_sub, (long)gb_sub); |
---|
2835 | } |
---|
2836 | } |
---|
2837 | break; |
---|
2838 | } |
---|
2839 | default: { |
---|
2840 | char *data = GB_read_as_string(gbd); |
---|
2841 | if (data) { printf("%s", data); free(data); } |
---|
2842 | printf(" father=(GBDATA*)0x%lx\n", (long)GB_FATHER(gbd)); |
---|
2843 | } |
---|
2844 | } |
---|
2845 | |
---|
2846 | |
---|
2847 | GB_pop_transaction(gbd); |
---|
2848 | |
---|
2849 | return 0; |
---|
2850 | } |
---|
2851 | |
---|
2852 | int GB_info(GBDATA *gbd) { // unused - intended to be used in debugger |
---|
2853 | return gb_info(gbd, 0); |
---|
2854 | } |
---|
2855 | |
---|
2856 | long GB_number_of_subentries(GBDATA *gbd) { |
---|
2857 | GBCONTAINER *gbc = gbd->expect_container(); |
---|
2858 | gb_header_list *header = GB_DATA_LIST_HEADER(gbc->d); |
---|
2859 | |
---|
2860 | long subentries = 0; |
---|
2861 | int end = gbc->d.nheader; |
---|
2862 | |
---|
2863 | for (int index = 0; index<end; index++) { |
---|
2864 | if (header[index].flags.changed < GB_DELETED) subentries++; |
---|
2865 | } |
---|
2866 | return subentries; |
---|
2867 | } |
---|
2868 | |
---|
2869 | // -------------------------------------------------------------------------------- |
---|
2870 | |
---|
2871 | #ifdef UNIT_TESTS |
---|
2872 | |
---|
2873 | #include <arb_diff.h> |
---|
2874 | #include <arb_file.h> |
---|
2875 | #include <test_unit.h> |
---|
2876 | #include <locale.h> |
---|
2877 | #include "arbdbt.h" |
---|
2878 | |
---|
2879 | void TEST_GB_atof() { |
---|
2880 | // arb depends on locale for floating-point conversion! |
---|
2881 | // see ../SOURCE_TOOLS/arb_main.h@setlocale |
---|
2882 | TEST_EXPECT_SIMILAR(GB_atof("0.031"), 0.031, 0.0001); // fails if LC_NUMERIC is set to "de_DE..." |
---|
2883 | } |
---|
2884 | |
---|
2885 | #if !defined(DARWIN) |
---|
2886 | // @@@ TEST_DISABLED_OSX: test fails to compile for OSX on build server |
---|
2887 | // @@@ re-activate test later; see missing libs http://bugs.arb-home.de/changeset/11664#file2 |
---|
2888 | void TEST_999_strtod_replacement() { |
---|
2889 | // caution: if it fails -> locale is not reset (therefore call with low priority 999) |
---|
2890 | const char *old = setlocale(LC_NUMERIC, "de_DE.UTF-8"); |
---|
2891 | { |
---|
2892 | // TEST_EXPECT_SIMILAR__BROKEN(strtod("0.031", NULp), 0.031, 0.0001); |
---|
2893 | TEST_EXPECT_SIMILAR(g_ascii_strtod("0.031", NULp), 0.031, 0.0001); |
---|
2894 | } |
---|
2895 | setlocale(LC_NUMERIC, old); |
---|
2896 | } |
---|
2897 | #endif |
---|
2898 | |
---|
2899 | #if defined(ENABLE_CRASH_TESTS) |
---|
2900 | static void test_another_shell() { delete new GB_shell; } |
---|
2901 | #endif |
---|
2902 | static void test_opendb() { GB_close(GB_open("no.arb", "c")); } |
---|
2903 | |
---|
2904 | void TEST_GB_shell__crashtest() { |
---|
2905 | { |
---|
2906 | GB_shell *shell = new GB_shell; |
---|
2907 | TEST_EXPECT_SEGFAULT(test_another_shell); |
---|
2908 | test_opendb(); // no SEGV here |
---|
2909 | delete shell; |
---|
2910 | } |
---|
2911 | |
---|
2912 | TEST_EXPECT_SEGFAULT(test_opendb); // should be impossible to open db w/o shell |
---|
2913 | } |
---|
2914 | |
---|
2915 | void TEST_GB_number_of_subentries() { |
---|
2916 | GB_shell shell; |
---|
2917 | GBDATA *gb_main = GB_open("no.arb", "c"); |
---|
2918 | |
---|
2919 | { |
---|
2920 | GB_transaction ta(gb_main); |
---|
2921 | |
---|
2922 | GBDATA *gb_cont = GB_create_container(gb_main, "container"); |
---|
2923 | TEST_EXPECT_EQUAL(GB_number_of_subentries(gb_cont), 0); |
---|
2924 | |
---|
2925 | TEST_EXPECT_RESULT__NOERROREXPORTED(GB_create(gb_cont, "entry", GB_STRING)); |
---|
2926 | TEST_EXPECT_EQUAL(GB_number_of_subentries(gb_cont), 1); |
---|
2927 | |
---|
2928 | { |
---|
2929 | GBDATA *gb_entry; |
---|
2930 | TEST_EXPECT_RESULT__NOERROREXPORTED(gb_entry = GB_create(gb_cont, "entry", GB_STRING)); |
---|
2931 | TEST_EXPECT_EQUAL(GB_number_of_subentries(gb_cont), 2); |
---|
2932 | |
---|
2933 | TEST_EXPECT_NO_ERROR(GB_delete(gb_entry)); |
---|
2934 | TEST_EXPECT_EQUAL(GB_number_of_subentries(gb_cont), 1); |
---|
2935 | } |
---|
2936 | |
---|
2937 | TEST_EXPECT_RESULT__NOERROREXPORTED(GB_create(gb_cont, "entry", GB_STRING)); |
---|
2938 | TEST_EXPECT_EQUAL(GB_number_of_subentries(gb_cont), 2); |
---|
2939 | } |
---|
2940 | |
---|
2941 | GB_close(gb_main); |
---|
2942 | } |
---|
2943 | |
---|
2944 | |
---|
2945 | void TEST_POSTCOND_arbdb() { |
---|
2946 | GB_ERROR error = GB_incur_error(); // clears the error (to make further tests succeed) |
---|
2947 | bool unclosed_GB_shell = closed_open_shell_for_unit_tests(); |
---|
2948 | |
---|
2949 | TEST_REJECT(error); // your test finished with an exported error |
---|
2950 | TEST_REJECT(unclosed_GB_shell); // your test finished w/o destroying GB_shell |
---|
2951 | } |
---|
2952 | |
---|
2953 | // #define TEST_AUTO_UPDATE // uncomment to auto-update expected results |
---|
2954 | |
---|
2955 | static void saveAndCompare(GBDATA *gb_main, const char *expectedname, bool |
---|
2956 | #if defined(TEST_AUTO_UPDATE) |
---|
2957 | allowAutoUpdate |
---|
2958 | #endif |
---|
2959 | ) { |
---|
2960 | const char *outputname = "copied.arb"; |
---|
2961 | |
---|
2962 | TEST_EXPECT_NO_ERROR(GB_save(gb_main, outputname, "a")); |
---|
2963 | |
---|
2964 | #if defined(TEST_AUTO_UPDATE) |
---|
2965 | if (allowAutoUpdate) { |
---|
2966 | TEST_COPY_FILE(outputname, expectedname); |
---|
2967 | } |
---|
2968 | #endif |
---|
2969 | TEST_EXPECT_TEXTFILE_DIFFLINES(outputname, expectedname, 0); |
---|
2970 | TEST_EXPECT_ZERO_OR_SHOW_ERRNO(GB_unlink(outputname)); |
---|
2971 | } |
---|
2972 | |
---|
2973 | void TEST_AFTER_SLOW_copy() { // run after TEST_SLOW_loadsave! |
---|
2974 | GB_shell shell; |
---|
2975 | GBDATA *gb_main = GB_open("TEST_loadsave_ascii.arb", "rw"); // ../UNIT_TESTER/run/TEST_loadsave_ascii.arb |
---|
2976 | |
---|
2977 | // --------------------------------------------------------- |
---|
2978 | // 1st step: copy root elements to container 'all' |
---|
2979 | GBDATA *gb_all; |
---|
2980 | { |
---|
2981 | GB_transaction ta(gb_main); |
---|
2982 | |
---|
2983 | TEST_EXPECT_RESULT__NOERROREXPORTED(gb_all = GB_create_container(gb_main, "all")); |
---|
2984 | |
---|
2985 | // move everything into new container: |
---|
2986 | GB_ERROR error = NULp; |
---|
2987 | for (GBDATA *gb_child = GB_child(gb_main); gb_child && !error; ) { |
---|
2988 | GBDATA *gb_next_child = GB_nextChild(gb_child); |
---|
2989 | if (gb_child != gb_all) { // skip target container |
---|
2990 | GBDATA *gb_clone = GB_clone(gb_all, gb_child); |
---|
2991 | error = GB_incur_error_if(!gb_clone); |
---|
2992 | if (gb_clone) { |
---|
2993 | gb_assert(!error); |
---|
2994 | error = GB_delete(gb_child); |
---|
2995 | } |
---|
2996 | } |
---|
2997 | gb_child = gb_next_child; |
---|
2998 | } |
---|
2999 | |
---|
3000 | TEST_EXPECT_NO_ERROR(error); |
---|
3001 | } |
---|
3002 | |
---|
3003 | saveAndCompare(gb_main, "TEST_copy.arb", true); |
---|
3004 | |
---|
3005 | // ----------------------------------------------------------------------------- |
---|
3006 | // 2nd step: copy container 'gb_all' with traditional GB_copy_dropProtectMarksAndTempstate into the fresh container 'gb_copy' (also named 'all') |
---|
3007 | GBDATA *gb_copy; |
---|
3008 | { |
---|
3009 | GB_transaction ta(gb_main); |
---|
3010 | |
---|
3011 | TEST_EXPECT_RESULT__NOERROREXPORTED(gb_copy = GB_create_container(gb_main, "all")); |
---|
3012 | TEST_EXPECT_NO_ERROR(GB_copy_dropProtectMarksAndTempstate(gb_copy, gb_all)); // w/o protection |
---|
3013 | |
---|
3014 | TEST_EXPECT_NO_ERROR(GB_set_temporary(gb_all)); // do not save 'all' |
---|
3015 | } |
---|
3016 | |
---|
3017 | saveAndCompare(gb_main, "TEST_copy_noProtect.arb", true); // ../UNIT_TESTER/run/TEST_copy_noProtect.arb |
---|
3018 | |
---|
3019 | // ----------------------------------------------------------------------------- |
---|
3020 | // 3rd step: copy container 'gb_all' with GB_copy_overlay to the fresh container 'gb_copy2' (also named 'all') |
---|
3021 | GBDATA *gb_copy2; |
---|
3022 | { |
---|
3023 | GB_transaction ta(gb_main); |
---|
3024 | |
---|
3025 | TEST_EXPECT_NO_ERROR(GB_clear_temporary(gb_all)); |
---|
3026 | |
---|
3027 | TEST_EXPECT_RESULT__NOERROREXPORTED(gb_copy2 = GB_create_container(gb_main, "all")); |
---|
3028 | TEST_EXPECT_NO_ERROR(GB_copy_overlay(gb_copy2, gb_all)); |
---|
3029 | |
---|
3030 | TEST_EXPECT_NO_ERROR(GB_set_temporary(gb_copy)); // skip save of 'copy' |
---|
3031 | TEST_EXPECT_NO_ERROR(GB_set_temporary(gb_all)); // skip save of 'all' |
---|
3032 | } |
---|
3033 | |
---|
3034 | saveAndCompare(gb_main, "TEST_copy.arb", false); |
---|
3035 | |
---|
3036 | // ----------------------------------------------------------------------------- |
---|
3037 | // 4th step: copy container 'gb_all' with GB_copy_overlay over container 'gb_copy' |
---|
3038 | // (Note: gb_copy lacks protection; it gets restored by overlay copy) |
---|
3039 | { |
---|
3040 | GB_transaction ta(gb_main); |
---|
3041 | |
---|
3042 | TEST_EXPECT_NO_ERROR(GB_clear_temporary(gb_all)); |
---|
3043 | TEST_EXPECT_NO_ERROR(GB_clear_temporary(gb_copy)); |
---|
3044 | |
---|
3045 | TEST_EXPECT_NO_ERROR(GB_copy_overlay(gb_copy, gb_all)); |
---|
3046 | |
---|
3047 | TEST_EXPECT_NO_ERROR(GB_set_temporary(gb_copy2)); // skip save of 'copy2' |
---|
3048 | TEST_EXPECT_NO_ERROR(GB_set_temporary(gb_all)); // skip save of 'all' |
---|
3049 | } |
---|
3050 | |
---|
3051 | saveAndCompare(gb_main, "TEST_copy.arb", false); |
---|
3052 | |
---|
3053 | // --------------------------------------------------------------------------------- |
---|
3054 | // 5th step: revert DB to original state by copying 'gb_all' back to DB-root |
---|
3055 | { |
---|
3056 | GB_transaction ta(gb_main); |
---|
3057 | |
---|
3058 | TEST_EXPECT_NO_ERROR(GB_set_temporary(gb_copy)); // skip save of 'copy' |
---|
3059 | TEST_EXPECT_NO_ERROR(GB_set_temporary(gb_copy2)); // skip save of 'copy2' |
---|
3060 | |
---|
3061 | // copy all entries back to root-container: |
---|
3062 | GBDATA *gb_tmp = GB_search(gb_all, "tmp", GB_FIND); |
---|
3063 | TEST_EXPECT_NULL(gb_tmp); // did not clone temp entries (in 1st step) |
---|
3064 | |
---|
3065 | TEST_EXPECT_NO_ERROR(GB_clear_temporary(gb_all)); // all = permanent (to permit copy) |
---|
3066 | TEST_EXPECT_NO_ERROR(GB_copy_std(gb_main, gb_all)); |
---|
3067 | TEST_EXPECT_NO_ERROR(GB_set_temporary(gb_all)); // all = temporary (to avoid save) |
---|
3068 | |
---|
3069 | TEST_EXPECT_ERROR_CONTAINS(gb_copy_checked(gb_main, gb_copy, CopyMode(CM_SKIP_TEMP & ~CM_DROP_TEMPSTATE)), |
---|
3070 | "logic error: it's too late to skip copy of temporary entry"); |
---|
3071 | |
---|
3072 | GBDATA *gb_description = GB_entry(gb_main, "description"); |
---|
3073 | TEST_EXPECT_NORESULT__ERROREXPORTED_CONTAINS(GB_clone(gb_description, gb_copy), |
---|
3074 | "GB_clone destination has to be a container"); |
---|
3075 | } |
---|
3076 | |
---|
3077 | saveAndCompare(gb_main, "TEST_loadsave_ascii.arb", false); |
---|
3078 | |
---|
3079 | // ---------------------------------------------- |
---|
3080 | // copying container into itself fails: |
---|
3081 | { |
---|
3082 | GB_transaction ta(gb_main); |
---|
3083 | |
---|
3084 | GBDATA *gb_key_data = GB_search(gb_copy, "presets/key_data", GB_FIND); |
---|
3085 | GBDATA *gb_key = GBT_find_item_rel_item_data(gb_key_data, "key_name", "full_name"); |
---|
3086 | |
---|
3087 | TEST_REJECT_NULL(gb_key); |
---|
3088 | TEST_EXPECT_NO_ERROR(GB_clear_temporary(gb_copy)); // allow to copy |
---|
3089 | TEST_EXPECT(GB_is_ancestor_of(gb_copy, gb_key)); // expect copy is ancestor of key |
---|
3090 | |
---|
3091 | TEST_EXPECT_ERROR_CONTAINS(GB_copy_std(gb_key, gb_copy), "infinite copy not permitted"); |
---|
3092 | TEST_EXPECT_NORESULT__ERROREXPORTED_CONTAINS(GB_clone(gb_key, gb_copy), "GB_clone destination cannot be part of source"); |
---|
3093 | } |
---|
3094 | |
---|
3095 | GB_close(gb_main); |
---|
3096 | } |
---|
3097 | |
---|
3098 | #endif // UNIT_TESTS |
---|