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