Debian Codegraph @

libcdata/libcdata_tree_node.c

raw symbols SCIP
1 /*
2 * Tree functions
3 *
4 * Copyright (C) 2006-2024, Joachim Metz <[email protected]>
5 *
6 * Refer to AUTHORS for acknowledgements.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <common.h>
23 #include <memory.h>
24 #include <types.h>
25
26 #include "libcdata_definitions.h"
27 #include "libcdata_libcerror.h"
28 #include "libcdata_libcthreads.h"
29 #include "libcdata_list.h"
30 #include "libcdata_tree_node.h"
31 #include "libcdata_types.h"
32
33 /* Creates a tree node
34 * Make sure the value node is referencing, is set to NULL
35 * Returns 1 if successful or -1 on error
36 */
37 int libcdata_tree_node_initialize(
38 libcdata_tree_node_t **node,
39 libcerror_error_t **error )
40 {
41 libcdata_internal_tree_node_t *internal_node = NULL;
42 static char *function = "libcdata_tree_node_initialize";
43
44 if( node == NULL )
45 {
46 libcerror_error_set(
47 error,
48 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
49 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
50 "%s: invalid node.",
51 function );
52
53 return( -1 );
54 }
55 if( *node != NULL )
56 {
57 libcerror_error_set(
58 error,
59 LIBCERROR_ERROR_DOMAIN_RUNTIME,
60 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
61 "%s: invalid node value already set.",
62 function );
63
64 return( -1 );
65 }
66 internal_node = memory_allocate_structure(
67 libcdata_internal_tree_node_t );
68
69 if( internal_node == NULL )
70 {
71 libcerror_error_set(
72 error,
73 LIBCERROR_ERROR_DOMAIN_MEMORY,
74 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
75 "%s: unable to create node.",
76 function );
77
78 goto on_error;
79 }
80 if( memory_set(
81 internal_node,
82 0,
83 sizeof( libcdata_internal_tree_node_t ) ) == NULL )
84 {
85 libcerror_error_set(
86 error,
87 LIBCERROR_ERROR_DOMAIN_MEMORY,
88 LIBCERROR_MEMORY_ERROR_SET_FAILED,
89 "%s: unable to clear node.",
90 function );
91
92 memory_free(
93 internal_node );
94
95 return( -1 );
96 }
97 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
98 if( libcthreads_read_write_lock_initialize(
99 &( internal_node->read_write_lock ),
100 error ) != 1 )
101 {
102 libcerror_error_set(
103 error,
104 LIBCERROR_ERROR_DOMAIN_RUNTIME,
105 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
106 "%s: unable to initialize read/write lock.",
107 function );
108
109 goto on_error;
110 }
111 #endif
112 *node = (libcdata_tree_node_t *) internal_node;
113
114 return( 1 );
115
116 on_error:
117 if( internal_node != NULL )
118 {
119 memory_free(
120 internal_node );
121 }
122 return( -1 );
123 }
124
125 /* Frees a tree node, its sub nodes
126 * Uses the value_free_function to free the value
127 * Returns 1 if successful or -1 on error
128 */
129 int libcdata_tree_node_free(
130 libcdata_tree_node_t **node,
131 int (*value_free_function)(
132 intptr_t **value,
133 libcerror_error_t **error ),
134 libcerror_error_t **error )
135 {
136 libcdata_internal_tree_node_t *internal_node = NULL;
137 static char *function = "libcdata_tree_node_free";
138 int result = 1;
139
140 if( node == NULL )
141 {
142 libcerror_error_set(
143 error,
144 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
145 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
146 "%s: invalid node.",
147 function );
148
149 return( -1 );
150 }
151 if( *node != NULL )
152 {
153 internal_node = (libcdata_internal_tree_node_t *) *node;
154
155 if( ( internal_node->parent_node != NULL )
156 || ( internal_node->previous_node != NULL )
157 || ( internal_node->next_node != NULL ) )
158 {
159 libcerror_error_set(
160 error,
161 LIBCERROR_ERROR_DOMAIN_RUNTIME,
162 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
163 "%s: invalid node - connected to other nodes.",
164 function );
165
166 return( -1 );
167 }
168 *node = NULL;
169
170 if( libcdata_tree_node_empty(
171 (libcdata_tree_node_t *) internal_node,
172 value_free_function,
173 error ) != 1 )
174 {
175 libcerror_error_set(
176 error,
177 LIBCERROR_ERROR_DOMAIN_RUNTIME,
178 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
179 "%s: unable to empty node.",
180 function );
181
182 result = -1;
183 }
184 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
185 if( libcthreads_read_write_lock_free(
186 &( internal_node->read_write_lock ),
187 error ) != 1 )
188 {
189 libcerror_error_set(
190 error,
191 LIBCERROR_ERROR_DOMAIN_RUNTIME,
192 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
193 "%s: unable to free read/write lock.",
194 function );
195
196 result = -1;
197 }
198 #endif
199 if( internal_node->value != NULL )
200 {
201 if( value_free_function != NULL )
202 {
203 if( value_free_function(
204 &( internal_node->value ),
205 error ) != 1 )
206 {
207 libcerror_error_set(
208 error,
209 LIBCERROR_ERROR_DOMAIN_RUNTIME,
210 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
211 "%s: unable to free value.",
212 function );
213
214 result = -1;
215 }
216 internal_node->value = NULL;
217 }
218 }
219 memory_free(
220 internal_node );
221 }
222 return( result );
223 }
224
225 /* Empties a tree node and frees its sub nodes
226 * Uses the value_free_function to free the value of the sub nodes
227 * Returns 1 if successful or -1 on error
228 */
229 int libcdata_tree_node_empty(
230 libcdata_tree_node_t *tree_node,
231 int (*value_free_function)(
232 intptr_t **value,
233 libcerror_error_t **error ),
234 libcerror_error_t **error )
235 {
236 libcdata_internal_tree_node_t *internal_node = NULL;
237 libcdata_tree_node_t *next_node = NULL;
238 libcdata_tree_node_t *parent_node = NULL;
239 libcdata_tree_node_t *previous_node = NULL;
240 libcdata_tree_node_t *sub_node = NULL;
241 static char *function = "libcdata_tree_node_empty";
242 int number_of_sub_nodes = 0;
243 int result = 1;
244 int sub_node_index = 0;
245
246 if( tree_node == NULL )
247 {
248 libcerror_error_set(
249 error,
250 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
251 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
252 "%s: invalid tree node.",
253 function );
254
255 return( -1 );
256 }
257 internal_node = (libcdata_internal_tree_node_t *) tree_node;
258
259 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
260 if( libcthreads_read_write_lock_grab_for_write(
261 internal_node->read_write_lock,
262 error ) != 1 )
263 {
264 libcerror_error_set(
265 error,
266 LIBCERROR_ERROR_DOMAIN_RUNTIME,
267 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
268 "%s: unable to grab read/write lock for writing.",
269 function );
270
271 return( -1 );
272 }
273 #endif
274 number_of_sub_nodes = internal_node->number_of_sub_nodes;
275
276 sub_node = internal_node->first_sub_node;
277
278 for( sub_node_index = 0;
279 sub_node_index < number_of_sub_nodes;
280 sub_node_index++ )
281 {
282 if( libcdata_tree_node_get_nodes(
283 sub_node,
284 &parent_node,
285 &previous_node,
286 &next_node,
287 error ) != 1 )
288 {
289 libcerror_error_set(
290 error,
291 LIBCERROR_ERROR_DOMAIN_RUNTIME,
292 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
293 "%s: unable to retrieve nodes of sub node: %d.",
294 function,
295 sub_node_index );
296
297 goto on_error;
298 }
299 if( previous_node != NULL )
300 {
301 libcerror_error_set(
302 error,
303 LIBCERROR_ERROR_DOMAIN_RUNTIME,
304 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
305 "%s: invalid sub node: %d - previous node is set.",
306 function,
307 sub_node_index );
308
309 goto on_error;
310 }
311 internal_node->first_sub_node = next_node;
312
313 if( internal_node->last_sub_node == sub_node )
314 {
315 internal_node->last_sub_node = next_node;
316 }
317 internal_node->number_of_sub_nodes -= 1;
318
319 if( next_node != NULL )
320 {
321 if( libcdata_tree_node_set_previous_node(
322 next_node,
323 NULL,
324 error ) != 1 )
325 {
326 libcerror_error_set(
327 error,
328 LIBCERROR_ERROR_DOMAIN_RUNTIME,
329 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
330 "%s: unable to set previous node of sub node: %d.",
331 function,
332 sub_node_index + 1 );
333
334 goto on_error;
335 }
336 }
337 if( libcdata_tree_node_set_nodes(
338 sub_node,
339 NULL,
340 NULL,
341 NULL,
342 error ) != 1 )
343 {
344 libcerror_error_set(
345 error,
346 LIBCERROR_ERROR_DOMAIN_RUNTIME,
347 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
348 "%s: unable to set nodes of sub node: %d.",
349 function,
350 sub_node_index );
351
352 goto on_error;
353 }
354 if( libcdata_tree_node_free(
355 &sub_node,
356 value_free_function,
357 error ) != 1 )
358 {
359 libcerror_error_set(
360 error,
361 LIBCERROR_ERROR_DOMAIN_RUNTIME,
362 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
363 "%s: unable to free sub node: %d.",
364 function,
365 sub_node_index );
366
367 result = -1;
368 }
369 sub_node = next_node;
370 }
371 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
372 if( libcthreads_read_write_lock_release_for_write(
373 internal_node->read_write_lock,
374 error ) != 1 )
375 {
376 libcerror_error_set(
377 error,
378 LIBCERROR_ERROR_DOMAIN_RUNTIME,
379 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
380 "%s: unable to release read/write lock for writing.",
381 function );
382
383 return( -1 );
384 }
385 #endif
386 return( result );
387
388 on_error:
389 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
390 libcthreads_read_write_lock_release_for_write(
391 internal_node->read_write_lock,
392 NULL );
393 #endif
394 return( -1 );
395 }
396
397 /* Clones the tree node and its sub nodes
398 *
399 * The values are cloned using the value_clone_function
400 * On error the values are freed using the value_free_function
401 *
402 * Returns 1 if successful or -1 on error
403 */
404 int libcdata_tree_node_clone(
405 libcdata_tree_node_t **destination_node,
406 libcdata_tree_node_t *source_node,
407 int (*value_free_function)(
408 intptr_t **value,
409 libcerror_error_t **error ),
410 int (*value_clone_function)(
411 intptr_t **destination_value,
412 intptr_t *source_value,
413 libcerror_error_t **error ),
414 libcerror_error_t **error )
415 {
416 libcdata_internal_tree_node_t *internal_destination_node = NULL;
417 libcdata_internal_tree_node_t *internal_source_node = NULL;
418 libcdata_tree_node_t *destination_sub_node = NULL;
419 libcdata_tree_node_t *sub_node = NULL;
420 static char *function = "libcdata_tree_node_clone";
421 int sub_node_index = 0;
422
423 if( destination_node == NULL )
424 {
425 libcerror_error_set(
426 error,
427 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
428 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
429 "%s: invalid destination tree node.",
430 function );
431
432 return( -1 );
433 }
434 if( *destination_node != NULL )
435 {
436 libcerror_error_set(
437 error,
438 LIBCERROR_ERROR_DOMAIN_RUNTIME,
439 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
440 "%s: invalid destination tree node already set.",
441 function );
442
443 return( -1 );
444 }
445 if( value_free_function == NULL )
446 {
447 libcerror_error_set(
448 error,
449 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
450 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
451 "%s: invalid value free function.",
452 function );
453
454 return( -1 );
455 }
456 if( value_clone_function == NULL )
457 {
458 libcerror_error_set(
459 error,
460 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
461 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
462 "%s: invalid value clone function.",
463 function );
464
465 return( -1 );
466 }
467 if( source_node == NULL )
468 {
469 *destination_node = NULL;
470
471 return( 1 );
472 }
473 internal_source_node = (libcdata_internal_tree_node_t *) source_node;
474
475 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
476 if( libcthreads_read_write_lock_grab_for_read(
477 internal_source_node->read_write_lock,
478 error ) != 1 )
479 {
480 libcerror_error_set(
481 error,
482 LIBCERROR_ERROR_DOMAIN_RUNTIME,
483 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
484 "%s: unable to grab read/write lock for reading.",
485 function );
486
487 return( -1 );
488 }
489 #endif
490 if( libcdata_tree_node_initialize(
491 (libcdata_tree_node_t **) &internal_destination_node,
492 error ) != 1 )
493 {
494 libcerror_error_set(
495 error,
496 LIBCERROR_ERROR_DOMAIN_RUNTIME,
497 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
498 "%s: unable to create destination tree node.",
499 function );
500
501 goto on_error;
502 }
503 if( internal_destination_node == NULL )
504 {
505 libcerror_error_set(
506 error,
507 LIBCERROR_ERROR_DOMAIN_RUNTIME,
508 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
509 "%s: missing destination tree node.",
510 function );
511
512 goto on_error;
513 }
514 if( value_clone_function(
515 &( internal_destination_node->value ),
516 internal_source_node->value,
517 error ) != 1 )
518 {
519 libcerror_error_set(
520 error,
521 LIBCERROR_ERROR_DOMAIN_RUNTIME,
522 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
523 "%s: unable to create destination tree node value.",
524 function );
525
526 goto on_error;
527 }
528 /* Clone the sub nodes
529 */
530 sub_node = internal_source_node->first_sub_node;
531
532 for( sub_node_index = 0;
533 sub_node_index < internal_source_node->number_of_sub_nodes;
534 sub_node_index++ )
535 {
536 if( sub_node == NULL )
537 {
538 libcerror_error_set(
539 error,
540 LIBCERROR_ERROR_DOMAIN_RUNTIME,
541 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
542 "%s: corruption detected in source sub node: %d.",
543 function,
544 sub_node_index );
545
546 goto on_error;
547 }
548 if( libcdata_tree_node_clone(
549 &destination_sub_node,
550 sub_node,
551 value_free_function,
552 value_clone_function,
553 error ) != 1 )
554 {
555 libcerror_error_set(
556 error,
557 LIBCERROR_ERROR_DOMAIN_RUNTIME,
558 LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
559 "%s: unable to create destination sub node: %d.",
560 function,
561 sub_node_index );
562
563 goto on_error;
564 }
565 if( libcdata_internal_tree_node_append_node(
566 internal_destination_node,
567 destination_sub_node,
568 error ) != 1 )
569 {
570 libcerror_error_set(
571 error,
572 LIBCERROR_ERROR_DOMAIN_RUNTIME,
573 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
574 "%s: unable to append sub node: %d to destination tree node.",
575 function,
576 sub_node_index );
577
578 goto on_error;
579 }
580 destination_sub_node = NULL;
581
582 if( libcdata_tree_node_get_next_node(
583 sub_node,
584 &sub_node,
585 error ) != 1 )
586 {
587 libcerror_error_set(
588 error,
589 LIBCERROR_ERROR_DOMAIN_RUNTIME,
590 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
591 "%s: unable to retrieve next node of sub node: %d.",
592 function,
593 sub_node_index );
594
595 goto on_error;
596 }
597 }
598 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
599 if( libcthreads_read_write_lock_release_for_read(
600 internal_source_node->read_write_lock,
601 error ) != 1 )
602 {
603 libcerror_error_set(
604 error,
605 LIBCERROR_ERROR_DOMAIN_RUNTIME,
606 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
607 "%s: unable to release read/write lock for reading.",
608 function );
609
610 libcdata_tree_node_free(
611 &destination_sub_node,
612 value_free_function,
613 NULL );
614
615 return( -1 );
616 }
617 #endif
618 *destination_node = (libcdata_tree_node_t *) internal_destination_node;
619
620 return( 1 );
621
622 on_error:
623 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
624 libcthreads_read_write_lock_release_for_read(
625 internal_source_node->read_write_lock,
626 NULL );
627 #endif
628 if( destination_sub_node != NULL )
629 {
630 libcdata_tree_node_free(
631 &destination_sub_node,
632 value_free_function,
633 NULL );
634 }
635 if( internal_destination_node != NULL )
636 {
637 libcdata_tree_node_free(
638 (libcdata_tree_node_t **) &internal_destination_node,
639 value_free_function,
640 NULL );
641 }
642 return( -1 );
643 }
644
645 /* Retrieves the value from the tree node
646 * Returns 1 if successful or -1 on error
647 */
648 int libcdata_tree_node_get_value(
649 libcdata_tree_node_t *node,
650 intptr_t **value,
651 libcerror_error_t **error )
652 {
653 libcdata_internal_tree_node_t *internal_node = NULL;
654 static char *function = "libcdata_tree_node_get_value";
655
656 if( node == NULL )
657 {
658 libcerror_error_set(
659 error,
660 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
661 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
662 "%s: invalid node.",
663 function );
664
665 return( -1 );
666 }
667 internal_node = (libcdata_internal_tree_node_t *) node;
668
669 if( value == NULL )
670 {
671 libcerror_error_set(
672 error,
673 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
674 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
675 "%s: invalid value.",
676 function );
677
678 return( -1 );
679 }
680 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
681 if( libcthreads_read_write_lock_grab_for_read(
682 internal_node->read_write_lock,
683 error ) != 1 )
684 {
685 libcerror_error_set(
686 error,
687 LIBCERROR_ERROR_DOMAIN_RUNTIME,
688 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
689 "%s: unable to grab read/write lock for reading.",
690 function );
691
692 return( -1 );
693 }
694 #endif
695 *value = internal_node->value;
696
697 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
698 if( libcthreads_read_write_lock_release_for_read(
699 internal_node->read_write_lock,
700 error ) != 1 )
701 {
702 libcerror_error_set(
703 error,
704 LIBCERROR_ERROR_DOMAIN_RUNTIME,
705 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
706 "%s: unable to release read/write lock for reading.",
707 function );
708
709 return( -1 );
710 }
711 #endif
712 return( 1 );
713 }
714
715 /* Sets the value in the tree node
716 * Returns 1 if successful or -1 on error
717 */
718 int libcdata_tree_node_set_value(
719 libcdata_tree_node_t *node,
720 intptr_t *value,
721 libcerror_error_t **error )
722 {
723 libcdata_internal_tree_node_t *internal_node = NULL;
724 static char *function = "libcdata_tree_node_set_value";
725
726 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
727 intptr_t *backup_value = NULL;
728 #endif
729
730 if( node == NULL )
731 {
732 libcerror_error_set(
733 error,
734 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
735 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
736 "%s: invalid node.",
737 function );
738
739 return( -1 );
740 }
741 internal_node = (libcdata_internal_tree_node_t *) node;
742
743 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
744 if( libcthreads_read_write_lock_grab_for_write(
745 internal_node->read_write_lock,
746 error ) != 1 )
747 {
748 libcerror_error_set(
749 error,
750 LIBCERROR_ERROR_DOMAIN_RUNTIME,
751 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
752 "%s: unable to grab read/write lock for writing.",
753 function );
754
755 return( -1 );
756 }
757 backup_value = internal_node->value;
758 #endif
759
760 internal_node->value = value;
761
762 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
763 if( libcthreads_read_write_lock_release_for_write(
764 internal_node->read_write_lock,
765 error ) != 1 )
766 {
767 libcerror_error_set(
768 error,
769 LIBCERROR_ERROR_DOMAIN_RUNTIME,
770 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
771 "%s: unable to release read/write lock for writing.",
772 function );
773
774 goto on_error;
775 }
776 #endif
777 return( 1 );
778
779 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
780 on_error:
781 internal_node->value = backup_value;
782
783 return( -1 );
784 #endif
785 }
786
787 /* Retrieves the parent node from the tree node
788 * Returns 1 if successful or -1 on error
789 */
790 int libcdata_tree_node_get_parent_node(
791 libcdata_tree_node_t *node,
792 libcdata_tree_node_t **parent_node,
793 libcerror_error_t **error )
794 {
795 libcdata_internal_tree_node_t *internal_node = NULL;
796 static char *function = "libcdata_tree_node_get_parent_node";
797
798 if( node == NULL )
799 {
800 libcerror_error_set(
801 error,
802 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
803 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
804 "%s: invalid node.",
805 function );
806
807 return( -1 );
808 }
809 internal_node = (libcdata_internal_tree_node_t *) node;
810
811 if( parent_node == NULL )
812 {
813 libcerror_error_set(
814 error,
815 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
816 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
817 "%s: invalid parent node.",
818 function );
819
820 return( -1 );
821 }
822 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
823 if( libcthreads_read_write_lock_grab_for_read(
824 internal_node->read_write_lock,
825 error ) != 1 )
826 {
827 libcerror_error_set(
828 error,
829 LIBCERROR_ERROR_DOMAIN_RUNTIME,
830 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
831 "%s: unable to grab read/write lock for reading.",
832 function );
833
834 return( -1 );
835 }
836 #endif
837 *parent_node = internal_node->parent_node;
838
839 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
840 if( libcthreads_read_write_lock_release_for_read(
841 internal_node->read_write_lock,
842 error ) != 1 )
843 {
844 libcerror_error_set(
845 error,
846 LIBCERROR_ERROR_DOMAIN_RUNTIME,
847 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
848 "%s: unable to release read/write lock for reading.",
849 function );
850
851 return( -1 );
852 }
853 #endif
854 return( 1 );
855 }
856
857 /* Sets the parent node in the tree node
858 * Returns 1 if successful or -1 on error
859 */
860 int libcdata_tree_node_set_parent_node(
861 libcdata_tree_node_t *node,
862 libcdata_tree_node_t *parent_node,
863 libcerror_error_t **error )
864 {
865 libcdata_internal_tree_node_t *internal_node = NULL;
866 static char *function = "libcdata_tree_node_set_parent_node";
867
868 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
869 libcdata_tree_node_t *backup_parent_node = NULL;
870 #endif
871
872 if( node == NULL )
873 {
874 libcerror_error_set(
875 error,
876 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
877 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
878 "%s: invalid node.",
879 function );
880
881 return( -1 );
882 }
883 internal_node = (libcdata_internal_tree_node_t *) node;
884
885 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
886 if( libcthreads_read_write_lock_grab_for_write(
887 internal_node->read_write_lock,
888 error ) != 1 )
889 {
890 libcerror_error_set(
891 error,
892 LIBCERROR_ERROR_DOMAIN_RUNTIME,
893 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
894 "%s: unable to grab read/write lock for writing.",
895 function );
896
897 return( -1 );
898 }
899 backup_parent_node = internal_node->parent_node;
900 #endif
901
902 internal_node->parent_node = parent_node;
903
904 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
905 if( libcthreads_read_write_lock_release_for_write(
906 internal_node->read_write_lock,
907 error ) != 1 )
908 {
909 libcerror_error_set(
910 error,
911 LIBCERROR_ERROR_DOMAIN_RUNTIME,
912 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
913 "%s: unable to release read/write lock for writing.",
914 function );
915
916 goto on_error;
917 }
918 #endif
919 return( 1 );
920
921 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
922 on_error:
923 internal_node->parent_node = backup_parent_node;
924
925 return( -1 );
926 #endif
927 }
928
929 /* Retrieves the previous node from the tree node
930 * Returns 1 if successful or -1 on error
931 */
932 int libcdata_tree_node_get_previous_node(
933 libcdata_tree_node_t *node,
934 libcdata_tree_node_t **previous_node,
935 libcerror_error_t **error )
936 {
937 libcdata_internal_tree_node_t *internal_node = NULL;
938 static char *function = "libcdata_tree_node_get_previous_node";
939
940 if( node == NULL )
941 {
942 libcerror_error_set(
943 error,
944 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
945 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
946 "%s: invalid node.",
947 function );
948
949 return( -1 );
950 }
951 internal_node = (libcdata_internal_tree_node_t *) node;
952
953 if( previous_node == NULL )
954 {
955 libcerror_error_set(
956 error,
957 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
958 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
959 "%s: invalid previous node.",
960 function );
961
962 return( -1 );
963 }
964 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
965 if( libcthreads_read_write_lock_grab_for_read(
966 internal_node->read_write_lock,
967 error ) != 1 )
968 {
969 libcerror_error_set(
970 error,
971 LIBCERROR_ERROR_DOMAIN_RUNTIME,
972 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
973 "%s: unable to grab read/write lock for reading.",
974 function );
975
976 return( -1 );
977 }
978 #endif
979 *previous_node = internal_node->previous_node;
980
981 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
982 if( libcthreads_read_write_lock_release_for_read(
983 internal_node->read_write_lock,
984 error ) != 1 )
985 {
986 libcerror_error_set(
987 error,
988 LIBCERROR_ERROR_DOMAIN_RUNTIME,
989 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
990 "%s: unable to release read/write lock for reading.",
991 function );
992
993 return( -1 );
994 }
995 #endif
996 return( 1 );
997 }
998
999 /* Sets the previous node in the tree node
1000 * Returns 1 if successful or -1 on error
1001 */
1002 int libcdata_tree_node_set_previous_node(
1003 libcdata_tree_node_t *node,
1004 libcdata_tree_node_t *previous_node,
1005 libcerror_error_t **error )
1006 {
1007 libcdata_internal_tree_node_t *internal_node = NULL;
1008 static char *function = "libcdata_tree_node_set_previous_node";
1009
1010 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1011 libcdata_tree_node_t *backup_previous_node = NULL;
1012 #endif
1013
1014 if( node == NULL )
1015 {
1016 libcerror_error_set(
1017 error,
1018 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1019 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1020 "%s: invalid node.",
1021 function );
1022
1023 return( -1 );
1024 }
1025 internal_node = (libcdata_internal_tree_node_t *) node;
1026
1027 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1028 if( libcthreads_read_write_lock_grab_for_write(
1029 internal_node->read_write_lock,
1030 error ) != 1 )
1031 {
1032 libcerror_error_set(
1033 error,
1034 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1035 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1036 "%s: unable to grab read/write lock for writing.",
1037 function );
1038
1039 return( -1 );
1040 }
1041 backup_previous_node = internal_node->previous_node;
1042 #endif
1043
1044 internal_node->previous_node = previous_node;
1045
1046 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1047 if( libcthreads_read_write_lock_release_for_write(
1048 internal_node->read_write_lock,
1049 error ) != 1 )
1050 {
1051 libcerror_error_set(
1052 error,
1053 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1054 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1055 "%s: unable to release read/write lock for writing.",
1056 function );
1057
1058 goto on_error;
1059 }
1060 #endif
1061 return( 1 );
1062
1063 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1064 on_error:
1065 internal_node->previous_node = backup_previous_node;
1066
1067 return( -1 );
1068 #endif
1069 }
1070
1071 /* Retrieves the next node from the tree node
1072 * Returns 1 if successful or -1 on error
1073 */
1074 int libcdata_tree_node_get_next_node(
1075 libcdata_tree_node_t *node,
1076 libcdata_tree_node_t **next_node,
1077 libcerror_error_t **error )
1078 {
1079 libcdata_internal_tree_node_t *internal_node = NULL;
1080 static char *function = "libcdata_tree_node_get_next_node";
1081
1082 if( node == NULL )
1083 {
1084 libcerror_error_set(
1085 error,
1086 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1087 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1088 "%s: invalid node.",
1089 function );
1090
1091 return( -1 );
1092 }
1093 internal_node = (libcdata_internal_tree_node_t *) node;
1094
1095 if( next_node == NULL )
1096 {
1097 libcerror_error_set(
1098 error,
1099 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1100 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1101 "%s: invalid next node.",
1102 function );
1103
1104 return( -1 );
1105 }
1106 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1107 if( libcthreads_read_write_lock_grab_for_read(
1108 internal_node->read_write_lock,
1109 error ) != 1 )
1110 {
1111 libcerror_error_set(
1112 error,
1113 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1114 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1115 "%s: unable to grab read/write lock for reading.",
1116 function );
1117
1118 return( -1 );
1119 }
1120 #endif
1121 *next_node = internal_node->next_node;
1122
1123 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1124 if( libcthreads_read_write_lock_release_for_read(
1125 internal_node->read_write_lock,
1126 error ) != 1 )
1127 {
1128 libcerror_error_set(
1129 error,
1130 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1131 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1132 "%s: unable to release read/write lock for reading.",
1133 function );
1134
1135 return( -1 );
1136 }
1137 #endif
1138 return( 1 );
1139 }
1140
1141 /* Sets the next node in the tree node
1142 * Returns 1 if successful or -1 on error
1143 */
1144 int libcdata_tree_node_set_next_node(
1145 libcdata_tree_node_t *node,
1146 libcdata_tree_node_t *next_node,
1147 libcerror_error_t **error )
1148 {
1149 libcdata_internal_tree_node_t *internal_node = NULL;
1150 static char *function = "libcdata_tree_node_set_next_node";
1151
1152 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1153 libcdata_tree_node_t *backup_next_node = NULL;
1154 #endif
1155
1156 if( node == NULL )
1157 {
1158 libcerror_error_set(
1159 error,
1160 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1161 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1162 "%s: invalid node.",
1163 function );
1164
1165 return( -1 );
1166 }
1167 internal_node = (libcdata_internal_tree_node_t *) node;
1168
1169 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1170 if( libcthreads_read_write_lock_grab_for_write(
1171 internal_node->read_write_lock,
1172 error ) != 1 )
1173 {
1174 libcerror_error_set(
1175 error,
1176 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1177 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1178 "%s: unable to grab read/write lock for writing.",
1179 function );
1180
1181 return( -1 );
1182 }
1183 backup_next_node = internal_node->next_node ;
1184 #endif
1185
1186 internal_node->next_node = next_node;
1187
1188 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1189 if( libcthreads_read_write_lock_release_for_write(
1190 internal_node->read_write_lock,
1191 error ) != 1 )
1192 {
1193 libcerror_error_set(
1194 error,
1195 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1196 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1197 "%s: unable to release read/write lock for writing.",
1198 function );
1199
1200 goto on_error;
1201 }
1202 #endif
1203 return( 1 );
1204
1205 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1206 on_error:
1207 internal_node->next_node = backup_next_node;
1208
1209 return( -1 );
1210 #endif
1211 }
1212
1213 /* Retrieves the nodes from the tree node
1214 * Returns 1 if successful or -1 on error
1215 */
1216 int libcdata_tree_node_get_nodes(
1217 libcdata_tree_node_t *node,
1218 libcdata_tree_node_t **parent_node,
1219 libcdata_tree_node_t **previous_node,
1220 libcdata_tree_node_t **next_node,
1221 libcerror_error_t **error )
1222 {
1223 libcdata_internal_tree_node_t *internal_node = NULL;
1224 static char *function = "libcdata_tree_node_get_nodes";
1225
1226 if( node == NULL )
1227 {
1228 libcerror_error_set(
1229 error,
1230 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1231 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1232 "%s: invalid node.",
1233 function );
1234
1235 return( -1 );
1236 }
1237 internal_node = (libcdata_internal_tree_node_t *) node;
1238
1239 if( parent_node == NULL )
1240 {
1241 libcerror_error_set(
1242 error,
1243 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1244 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1245 "%s: invalid parent node.",
1246 function );
1247
1248 return( -1 );
1249 }
1250 if( previous_node == NULL )
1251 {
1252 libcerror_error_set(
1253 error,
1254 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1255 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1256 "%s: invalid previous node.",
1257 function );
1258
1259 return( -1 );
1260 }
1261 if( next_node == NULL )
1262 {
1263 libcerror_error_set(
1264 error,
1265 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1266 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1267 "%s: invalid next node.",
1268 function );
1269
1270 return( -1 );
1271 }
1272 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1273 if( libcthreads_read_write_lock_grab_for_read(
1274 internal_node->read_write_lock,
1275 error ) != 1 )
1276 {
1277 libcerror_error_set(
1278 error,
1279 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1280 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1281 "%s: unable to grab read/write lock for reading.",
1282 function );
1283
1284 return( -1 );
1285 }
1286 #endif
1287 *parent_node = internal_node->parent_node;
1288 *previous_node = internal_node->previous_node;
1289 *next_node = internal_node->next_node;
1290
1291 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1292 if( libcthreads_read_write_lock_release_for_read(
1293 internal_node->read_write_lock,
1294 error ) != 1 )
1295 {
1296 libcerror_error_set(
1297 error,
1298 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1299 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1300 "%s: unable to release read/write lock for reading.",
1301 function );
1302
1303 return( -1 );
1304 }
1305 #endif
1306 return( 1 );
1307 }
1308
1309 /* Sets the nodes in the tree node
1310 * Returns 1 if successful or -1 on error
1311 */
1312 int libcdata_tree_node_set_nodes(
1313 libcdata_tree_node_t *node,
1314 libcdata_tree_node_t *parent_node,
1315 libcdata_tree_node_t *previous_node,
1316 libcdata_tree_node_t *next_node,
1317 libcerror_error_t **error )
1318 {
1319 libcdata_internal_tree_node_t *internal_node = NULL;
1320 static char *function = "libcdata_tree_node_set_nodes";
1321
1322 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1323 libcdata_tree_node_t *backup_next_node = NULL;
1324 libcdata_tree_node_t *backup_parent_node = NULL;
1325 libcdata_tree_node_t *backup_previous_node = NULL;
1326 #endif
1327
1328 if( node == NULL )
1329 {
1330 libcerror_error_set(
1331 error,
1332 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1333 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1334 "%s: invalid node.",
1335 function );
1336
1337 return( -1 );
1338 }
1339 internal_node = (libcdata_internal_tree_node_t *) node;
1340
1341 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1342 if( libcthreads_read_write_lock_grab_for_write(
1343 internal_node->read_write_lock,
1344 error ) != 1 )
1345 {
1346 libcerror_error_set(
1347 error,
1348 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1349 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1350 "%s: unable to grab read/write lock for writing.",
1351 function );
1352
1353 return( -1 );
1354 }
1355 backup_parent_node = internal_node->parent_node;
1356 backup_previous_node = internal_node->previous_node;
1357 backup_next_node = internal_node->next_node;
1358 #endif
1359
1360 internal_node->parent_node = parent_node;
1361 internal_node->previous_node = previous_node;
1362 internal_node->next_node = next_node;
1363
1364 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1365 if( libcthreads_read_write_lock_release_for_write(
1366 internal_node->read_write_lock,
1367 error ) != 1 )
1368 {
1369 libcerror_error_set(
1370 error,
1371 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1372 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1373 "%s: unable to release read/write lock for writing.",
1374 function );
1375
1376 goto on_error;
1377 }
1378 #endif
1379 return( 1 );
1380
1381 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1382 on_error:
1383 internal_node->parent_node = backup_parent_node;
1384 internal_node->previous_node = backup_previous_node;
1385 internal_node->next_node = backup_next_node;
1386
1387 return( -1 );
1388 #endif
1389 }
1390
1391 /* Retrieves the first sub node from the tree node
1392 * Returns 1 if successful or -1 on error
1393 */
1394 int libcdata_tree_node_get_first_sub_node(
1395 libcdata_tree_node_t *node,
1396 libcdata_tree_node_t **first_sub_node,
1397 libcerror_error_t **error )
1398 {
1399 libcdata_internal_tree_node_t *internal_node = NULL;
1400 static char *function = "libcdata_tree_node_get_first_sub_node";
1401
1402 if( node == NULL )
1403 {
1404 libcerror_error_set(
1405 error,
1406 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1407 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1408 "%s: invalid node.",
1409 function );
1410
1411 return( -1 );
1412 }
1413 internal_node = (libcdata_internal_tree_node_t *) node;
1414
1415 if( first_sub_node == NULL )
1416 {
1417 libcerror_error_set(
1418 error,
1419 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1420 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1421 "%s: invalid first sub node.",
1422 function );
1423
1424 return( -1 );
1425 }
1426 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1427 if( libcthreads_read_write_lock_grab_for_read(
1428 internal_node->read_write_lock,
1429 error ) != 1 )
1430 {
1431 libcerror_error_set(
1432 error,
1433 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1434 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1435 "%s: unable to grab read/write lock for reading.",
1436 function );
1437
1438 return( -1 );
1439 }
1440 #endif
1441 *first_sub_node = internal_node->first_sub_node;
1442
1443 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1444 if( libcthreads_read_write_lock_release_for_read(
1445 internal_node->read_write_lock,
1446 error ) != 1 )
1447 {
1448 libcerror_error_set(
1449 error,
1450 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1451 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1452 "%s: unable to release read/write lock for reading.",
1453 function );
1454
1455 return( -1 );
1456 }
1457 #endif
1458 return( 1 );
1459 }
1460
1461 /* Sets the first sub node in the tree node
1462 * Returns 1 if successful or -1 on error
1463 */
1464 int libcdata_internal_tree_node_set_first_sub_node(
1465 libcdata_internal_tree_node_t *internal_node,
1466 libcdata_tree_node_t *first_sub_node,
1467 libcerror_error_t **error )
1468 {
1469 libcdata_tree_node_t *backup_first_sub_node = NULL;
1470 libcdata_tree_node_t *backup_previous_node = NULL;
1471 static char *function = "libcdata_internal_tree_node_set_first_sub_node";
1472
1473 if( internal_node == NULL )
1474 {
1475 libcerror_error_set(
1476 error,
1477 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1478 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1479 "%s: invalid node.",
1480 function );
1481
1482 return( -1 );
1483 }
1484 if( first_sub_node != NULL )
1485 {
1486 if( libcdata_tree_node_get_previous_node(
1487 first_sub_node,
1488 &backup_previous_node,
1489 error ) != 1 )
1490 {
1491 libcerror_error_set(
1492 error,
1493 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1494 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1495 "%s: unable to retrieve previous node of sub node.",
1496 function );
1497
1498 return( -1 );
1499 }
1500 }
1501 backup_first_sub_node = internal_node->first_sub_node;
1502
1503 if( first_sub_node != NULL )
1504 {
1505 if( libcdata_tree_node_set_previous_node(
1506 first_sub_node,
1507 internal_node->first_sub_node,
1508 error ) != 1 )
1509 {
1510 libcerror_error_set(
1511 error,
1512 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1513 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1514 "%s: unable to set previous node of sub node.",
1515 function );
1516
1517 goto on_error;
1518 }
1519 }
1520 if( internal_node->first_sub_node != NULL )
1521 {
1522 if( libcdata_tree_node_set_next_node(
1523 internal_node->first_sub_node,
1524 first_sub_node,
1525 error ) != 1 )
1526 {
1527 libcerror_error_set(
1528 error,
1529 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1530 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1531 "%s: unable to set next node of first sub node.",
1532 function );
1533
1534 goto on_error;
1535 }
1536 }
1537 internal_node->first_sub_node = first_sub_node;
1538
1539 return( 1 );
1540
1541 on_error:
1542 if( first_sub_node != NULL )
1543 {
1544 libcdata_tree_node_set_previous_node(
1545 first_sub_node,
1546 backup_previous_node,
1547 NULL );
1548 }
1549 if( backup_first_sub_node != NULL )
1550 {
1551 libcdata_tree_node_set_next_node(
1552 backup_first_sub_node,
1553 NULL,
1554 NULL );
1555 }
1556 internal_node->first_sub_node = backup_first_sub_node;
1557
1558 return( -1 );
1559 }
1560
1561 /* Retrieves the last sub node from the tree node
1562 * Returns 1 if successful or -1 on error
1563 */
1564 int libcdata_tree_node_get_last_sub_node(
1565 libcdata_tree_node_t *node,
1566 libcdata_tree_node_t **last_sub_node,
1567 libcerror_error_t **error )
1568 {
1569 libcdata_internal_tree_node_t *internal_node = NULL;
1570 static char *function = "libcdata_tree_node_get_last_sub_node";
1571
1572 if( node == NULL )
1573 {
1574 libcerror_error_set(
1575 error,
1576 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1577 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1578 "%s: invalid node.",
1579 function );
1580
1581 return( -1 );
1582 }
1583 internal_node = (libcdata_internal_tree_node_t *) node;
1584
1585 if( last_sub_node == NULL )
1586 {
1587 libcerror_error_set(
1588 error,
1589 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1590 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1591 "%s: invalid last sub node.",
1592 function );
1593
1594 return( -1 );
1595 }
1596 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1597 if( libcthreads_read_write_lock_grab_for_read(
1598 internal_node->read_write_lock,
1599 error ) != 1 )
1600 {
1601 libcerror_error_set(
1602 error,
1603 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1604 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1605 "%s: unable to grab read/write lock for reading.",
1606 function );
1607
1608 return( -1 );
1609 }
1610 #endif
1611 *last_sub_node = internal_node->last_sub_node;
1612
1613 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1614 if( libcthreads_read_write_lock_release_for_read(
1615 internal_node->read_write_lock,
1616 error ) != 1 )
1617 {
1618 libcerror_error_set(
1619 error,
1620 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1621 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1622 "%s: unable to release read/write lock for reading.",
1623 function );
1624
1625 return( -1 );
1626 }
1627 #endif
1628 return( 1 );
1629 }
1630
1631 /* Sets the last sub node in the tree node
1632 * Returns 1 if successful or -1 on error
1633 */
1634 int libcdata_internal_tree_node_set_last_sub_node(
1635 libcdata_internal_tree_node_t *internal_node,
1636 libcdata_tree_node_t *last_sub_node,
1637 libcerror_error_t **error )
1638 {
1639 libcdata_tree_node_t *backup_last_sub_node = NULL;
1640 libcdata_tree_node_t *backup_previous_node = NULL;
1641 static char *function = "libcdata_internal_tree_node_set_last_sub_node";
1642
1643 if( internal_node == NULL )
1644 {
1645 libcerror_error_set(
1646 error,
1647 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1648 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1649 "%s: invalid node.",
1650 function );
1651
1652 return( -1 );
1653 }
1654 if( last_sub_node != NULL )
1655 {
1656 if( libcdata_tree_node_get_previous_node(
1657 last_sub_node,
1658 &backup_previous_node,
1659 error ) != 1 )
1660 {
1661 libcerror_error_set(
1662 error,
1663 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1664 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1665 "%s: unable to retrieve previous node of sub node.",
1666 function );
1667
1668 return( -1 );
1669 }
1670 }
1671 backup_last_sub_node = internal_node->last_sub_node;
1672
1673 if( last_sub_node != NULL )
1674 {
1675 if( libcdata_tree_node_set_previous_node(
1676 last_sub_node,
1677 internal_node->last_sub_node,
1678 error ) != 1 )
1679 {
1680 libcerror_error_set(
1681 error,
1682 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1683 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1684 "%s: unable to set previous node of sub node.",
1685 function );
1686
1687 goto on_error;
1688 }
1689 }
1690 if( internal_node->last_sub_node != NULL )
1691 {
1692 if( libcdata_tree_node_set_next_node(
1693 internal_node->last_sub_node,
1694 last_sub_node,
1695 error ) != 1 )
1696 {
1697 libcerror_error_set(
1698 error,
1699 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1700 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1701 "%s: unable to set next node of last sub node.",
1702 function );
1703
1704 goto on_error;
1705 }
1706 }
1707 internal_node->last_sub_node = last_sub_node;
1708
1709 return( 1 );
1710
1711 on_error:
1712 if( last_sub_node != NULL )
1713 {
1714 libcdata_tree_node_set_previous_node(
1715 last_sub_node,
1716 backup_previous_node,
1717 NULL );
1718 }
1719 if( backup_last_sub_node != NULL )
1720 {
1721 libcdata_tree_node_set_next_node(
1722 backup_last_sub_node,
1723 NULL,
1724 NULL );
1725 }
1726 internal_node->last_sub_node = backup_last_sub_node;
1727
1728 return( -1 );
1729 }
1730
1731 /* Retrieves the sub nodes from the tree node
1732 * Returns 1 if successful or -1 on error
1733 */
1734 int libcdata_tree_node_get_sub_nodes(
1735 libcdata_tree_node_t *node,
1736 libcdata_tree_node_t **first_sub_node,
1737 libcdata_tree_node_t **last_sub_node,
1738 libcerror_error_t **error )
1739 {
1740 libcdata_internal_tree_node_t *internal_node = NULL;
1741 static char *function = "libcdata_tree_node_sub_get_nodes";
1742
1743 if( node == NULL )
1744 {
1745 libcerror_error_set(
1746 error,
1747 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1748 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1749 "%s: invalid node.",
1750 function );
1751
1752 return( -1 );
1753 }
1754 internal_node = (libcdata_internal_tree_node_t *) node;
1755
1756 if( first_sub_node == NULL )
1757 {
1758 libcerror_error_set(
1759 error,
1760 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1761 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1762 "%s: invalid first sub node.",
1763 function );
1764
1765 return( -1 );
1766 }
1767 if( last_sub_node == NULL )
1768 {
1769 libcerror_error_set(
1770 error,
1771 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1772 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1773 "%s: invalid last sub node.",
1774 function );
1775
1776 return( -1 );
1777 }
1778 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1779 if( libcthreads_read_write_lock_grab_for_read(
1780 internal_node->read_write_lock,
1781 error ) != 1 )
1782 {
1783 libcerror_error_set(
1784 error,
1785 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1786 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1787 "%s: unable to grab read/write lock for reading.",
1788 function );
1789
1790 return( -1 );
1791 }
1792 #endif
1793 *first_sub_node = internal_node->first_sub_node;
1794 *last_sub_node = internal_node->last_sub_node;
1795
1796 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1797 if( libcthreads_read_write_lock_release_for_read(
1798 internal_node->read_write_lock,
1799 error ) != 1 )
1800 {
1801 libcerror_error_set(
1802 error,
1803 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1804 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1805 "%s: unable to release read/write lock for reading.",
1806 function );
1807
1808 return( -1 );
1809 }
1810 #endif
1811 return( 1 );
1812 }
1813
1814 /* Sets the sub nodes in the tree node
1815 * Returns 1 if successful or -1 on error
1816 */
1817 int libcdata_tree_node_set_sub_nodes(
1818 libcdata_tree_node_t *node,
1819 libcdata_tree_node_t *first_sub_node,
1820 libcdata_tree_node_t *last_sub_node,
1821 libcerror_error_t **error )
1822 {
1823 libcdata_internal_tree_node_t *internal_node = NULL;
1824 static char *function = "libcdata_tree_node_set_sub_nodes";
1825
1826 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1827 libcdata_tree_node_t *backup_first_sub_node = NULL;
1828 libcdata_tree_node_t *backup_last_sub_node = NULL;
1829 #endif
1830
1831 if( node == NULL )
1832 {
1833 libcerror_error_set(
1834 error,
1835 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1836 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1837 "%s: invalid node.",
1838 function );
1839
1840 return( -1 );
1841 }
1842 internal_node = (libcdata_internal_tree_node_t *) node;
1843
1844 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1845 if( libcthreads_read_write_lock_grab_for_write(
1846 internal_node->read_write_lock,
1847 error ) != 1 )
1848 {
1849 libcerror_error_set(
1850 error,
1851 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1852 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1853 "%s: unable to grab read/write lock for writing.",
1854 function );
1855
1856 return( -1 );
1857 }
1858 backup_first_sub_node = internal_node->first_sub_node;
1859 backup_last_sub_node = internal_node->last_sub_node;
1860 #endif
1861
1862 internal_node->first_sub_node = first_sub_node;
1863 internal_node->last_sub_node = last_sub_node;
1864
1865 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1866 if( libcthreads_read_write_lock_release_for_write(
1867 internal_node->read_write_lock,
1868 error ) != 1 )
1869 {
1870 libcerror_error_set(
1871 error,
1872 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1873 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1874 "%s: unable to release read/write lock for writing.",
1875 function );
1876
1877 goto on_error;
1878 }
1879 #endif
1880 return( 1 );
1881
1882 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
1883 on_error:
1884 internal_node->first_sub_node = backup_first_sub_node;
1885 internal_node->last_sub_node = backup_last_sub_node;
1886
1887 return( -1 );
1888 #endif
1889 }
1890
1891 /* Appends a sub tree node to the node
1892 * Returns 1 if successful or -1 on error
1893 */
1894 int libcdata_internal_tree_node_append_node(
1895 libcdata_internal_tree_node_t *internal_node,
1896 libcdata_tree_node_t *node_to_append,
1897 libcerror_error_t **error )
1898 {
1899 libcdata_tree_node_t *to_append_next_node = NULL;
1900 libcdata_tree_node_t *to_append_parent_node = NULL;
1901 libcdata_tree_node_t *to_append_previous_node = NULL;
1902 static char *function = "libcdata_internal_tree_node_append_node";
1903
1904 if( internal_node == NULL )
1905 {
1906 libcerror_error_set(
1907 error,
1908 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1909 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1910 "%s: invalid node.",
1911 function );
1912
1913 return( -1 );
1914 }
1915 if( node_to_append == NULL )
1916 {
1917 libcerror_error_set(
1918 error,
1919 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1920 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1921 "%s: invalid node to append.",
1922 function );
1923
1924 return( -1 );
1925 }
1926 if( libcdata_tree_node_get_nodes(
1927 node_to_append,
1928 &to_append_parent_node,
1929 &to_append_previous_node,
1930 &to_append_next_node,
1931 error ) != 1 )
1932 {
1933 libcerror_error_set(
1934 error,
1935 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1936 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1937 "%s: unable to retrieve nodes of node to append.",
1938 function );
1939
1940 return( -1 );
1941 }
1942 if( ( to_append_parent_node != NULL )
1943 || ( to_append_previous_node != NULL )
1944 || ( to_append_next_node != NULL ) )
1945 {
1946 libcerror_error_set(
1947 error,
1948 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1949 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
1950 "%s: invalid node to append - node is already part of a tree.",
1951 function );
1952
1953 return( -1 );
1954 }
1955 if( internal_node->number_of_sub_nodes == 0 )
1956 {
1957 if( internal_node->first_sub_node != NULL )
1958 {
1959 libcerror_error_set(
1960 error,
1961 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1962 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
1963 "%s: corruption detected - first sub node already set.",
1964 function );
1965
1966 return( -1 );
1967 }
1968 if( internal_node->last_sub_node != NULL )
1969 {
1970 libcerror_error_set(
1971 error,
1972 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1973 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
1974 "%s: corruption detected - last sub node already set.",
1975 function );
1976
1977 return( -1 );
1978 }
1979 internal_node->first_sub_node = node_to_append;
1980 internal_node->last_sub_node = node_to_append;
1981 }
1982 else
1983 {
1984 if( internal_node->first_sub_node == NULL )
1985 {
1986 libcerror_error_set(
1987 error,
1988 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1989 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1990 "%s: corruption detected - missing first sub node.",
1991 function );
1992
1993 return( -1 );
1994 }
1995 if( internal_node->last_sub_node == NULL )
1996 {
1997 libcerror_error_set(
1998 error,
1999 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2000 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2001 "%s: corruption detected - missing last sub node.",
2002 function );
2003
2004 return( -1 );
2005 }
2006 if( libcdata_tree_node_set_next_node(
2007 internal_node->last_sub_node,
2008 node_to_append,
2009 error ) != 1 )
2010 {
2011 libcerror_error_set(
2012 error,
2013 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2014 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2015 "%s: unable to set next node of last sub node.",
2016 function );
2017
2018 return( -1 );
2019 }
2020 if( libcdata_tree_node_set_previous_node(
2021 node_to_append,
2022 internal_node->last_sub_node,
2023 error ) != 1 )
2024 {
2025 libcerror_error_set(
2026 error,
2027 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2028 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2029 "%s: unable to set previous node of node to append.",
2030 function );
2031
2032 return( -1 );
2033 }
2034 internal_node->last_sub_node = node_to_append;
2035 }
2036 if( libcdata_tree_node_set_parent_node(
2037 node_to_append,
2038 (libcdata_tree_node_t *) internal_node,
2039 error ) != 1 )
2040 {
2041 libcerror_error_set(
2042 error,
2043 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2044 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2045 "%s: unable to set parent node of node to append.",
2046 function );
2047
2048 return( -1 );
2049 }
2050 internal_node->number_of_sub_nodes += 1;
2051
2052 return( 1 );
2053 }
2054
2055 /* Appends a tree node to the node
2056 * Returns 1 if successful or -1 on error
2057 */
2058 int libcdata_tree_node_append_node(
2059 libcdata_tree_node_t *node,
2060 libcdata_tree_node_t *node_to_append,
2061 libcerror_error_t **error )
2062 {
2063 libcdata_internal_tree_node_t *internal_node = NULL;
2064 libcdata_tree_node_t *to_append_next_node = NULL;
2065 libcdata_tree_node_t *to_append_parent_node = NULL;
2066 libcdata_tree_node_t *to_append_previous_node = NULL;
2067 static char *function = "libcdata_tree_node_append_node";
2068 int result = 1;
2069
2070 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
2071 libcdata_tree_node_t *backup_first_sub_node = NULL;
2072 libcdata_tree_node_t *backup_last_sub_node = NULL;
2073 #endif
2074
2075 if( node == NULL )
2076 {
2077 libcerror_error_set(
2078 error,
2079 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2080 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2081 "%s: invalid node.",
2082 function );
2083
2084 return( -1 );
2085 }
2086 internal_node = (libcdata_internal_tree_node_t *) node;
2087
2088 if( internal_node->number_of_sub_nodes == 0 )
2089 {
2090 if( internal_node->first_sub_node != NULL )
2091 {
2092 libcerror_error_set(
2093 error,
2094 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2095 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2096 "%s: corruption detected - first sub node already set.",
2097 function );
2098
2099 return( -1 );
2100 }
2101 if( internal_node->last_sub_node != NULL )
2102 {
2103 libcerror_error_set(
2104 error,
2105 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2106 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2107 "%s: corruption detected - last sub node already set.",
2108 function );
2109
2110 return( -1 );
2111 }
2112 }
2113 else
2114 {
2115 if( internal_node->first_sub_node == NULL )
2116 {
2117 libcerror_error_set(
2118 error,
2119 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2120 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2121 "%s: corruption detected - missing first sub node.",
2122 function );
2123
2124 return( -1 );
2125 }
2126 if( internal_node->last_sub_node == NULL )
2127 {
2128 libcerror_error_set(
2129 error,
2130 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2131 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2132 "%s: corruption detected - missing last sub node.",
2133 function );
2134
2135 return( -1 );
2136 }
2137 }
2138 if( libcdata_tree_node_get_nodes(
2139 node_to_append,
2140 &to_append_parent_node,
2141 &to_append_previous_node,
2142 &to_append_next_node,
2143 error ) != 1 )
2144 {
2145 libcerror_error_set(
2146 error,
2147 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2148 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2149 "%s: unable to retrieve nodes of node to append.",
2150 function );
2151
2152 return( -1 );
2153 }
2154 if( ( to_append_parent_node != NULL )
2155 || ( to_append_previous_node != NULL )
2156 || ( to_append_next_node != NULL ) )
2157 {
2158 libcerror_error_set(
2159 error,
2160 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2161 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
2162 "%s: invalid node to append - node is already part of a tree.",
2163 function );
2164
2165 return( -1 );
2166 }
2167 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
2168 if( libcthreads_read_write_lock_grab_for_write(
2169 internal_node->read_write_lock,
2170 error ) != 1 )
2171 {
2172 libcerror_error_set(
2173 error,
2174 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2175 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2176 "%s: unable to grab read/write lock for writing.",
2177 function );
2178
2179 return( -1 );
2180 }
2181 backup_first_sub_node = internal_node->first_sub_node;
2182 backup_last_sub_node = internal_node->last_sub_node;
2183 #endif
2184
2185 result = libcdata_tree_node_set_parent_node(
2186 node_to_append,
2187 node,
2188 error );
2189
2190 if( result != 1 )
2191 {
2192 libcerror_error_set(
2193 error,
2194 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2195 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2196 "%s: unable to set parent node of node to append.",
2197 function );
2198
2199 result = -1;
2200 }
2201 if( result == 1 )
2202 {
2203 result = libcdata_tree_node_set_previous_node(
2204 node_to_append,
2205 internal_node->last_sub_node,
2206 error );
2207
2208 if( result != 1 )
2209 {
2210 libcerror_error_set(
2211 error,
2212 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2213 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2214 "%s: unable to set previous node of node to append.",
2215 function );
2216
2217 libcdata_tree_node_set_parent_node(
2218 node_to_append,
2219 NULL,
2220 NULL );
2221
2222 result = -1;
2223 }
2224 }
2225 if( result == 1 )
2226 {
2227 if( internal_node->last_sub_node != NULL )
2228 {
2229 result = libcdata_tree_node_set_next_node(
2230 internal_node->last_sub_node,
2231 node_to_append,
2232 error );
2233
2234 if( result != 1 )
2235 {
2236 libcerror_error_set(
2237 error,
2238 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2239 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2240 "%s: unable to set next node of last sub node.",
2241 function );
2242
2243 libcdata_tree_node_set_parent_node(
2244 node_to_append,
2245 NULL,
2246 NULL );
2247
2248 libcdata_tree_node_set_previous_node(
2249 node_to_append,
2250 NULL,
2251 NULL );
2252
2253 result = -1;
2254 }
2255 }
2256 }
2257 if( result == 1 )
2258 {
2259 if( internal_node->first_sub_node == NULL )
2260 {
2261 internal_node->first_sub_node = node_to_append;
2262 }
2263 internal_node->last_sub_node = node_to_append;
2264
2265 internal_node->number_of_sub_nodes += 1;
2266 }
2267 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
2268 if( libcthreads_read_write_lock_release_for_write(
2269 internal_node->read_write_lock,
2270 error ) != 1 )
2271 {
2272 libcerror_error_set(
2273 error,
2274 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2275 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2276 "%s: unable to release read/write lock for writing.",
2277 function );
2278
2279 goto on_error;
2280 }
2281 #endif
2282 return( result );
2283
2284 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
2285 on_error:
2286 libcdata_tree_node_set_parent_node(
2287 node_to_append,
2288 NULL,
2289 NULL );
2290
2291 libcdata_tree_node_set_previous_node(
2292 node_to_append,
2293 NULL,
2294 NULL );
2295
2296 internal_node->first_sub_node = backup_first_sub_node;
2297 internal_node->last_sub_node = backup_last_sub_node;
2298
2299 internal_node->number_of_sub_nodes -= 1;
2300
2301 return( -1 );
2302 #endif
2303 }
2304
2305 /* Appends a value to the node
2306 * Creates a new sub tree node
2307 * Returns 1 if successful or -1 on error
2308 */
2309 int libcdata_tree_node_append_value(
2310 libcdata_tree_node_t *node,
2311 intptr_t *value,
2312 libcerror_error_t **error )
2313 {
2314 libcdata_tree_node_t *sub_node = NULL;
2315 static char *function = "libcdata_tree_node_append_value";
2316
2317 if( node == NULL )
2318 {
2319 libcerror_error_set(
2320 error,
2321 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2322 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2323 "%s: invalid node.",
2324 function );
2325
2326 return( -1 );
2327 }
2328 if( libcdata_tree_node_initialize(
2329 &sub_node,
2330 error ) != 1 )
2331 {
2332 libcerror_error_set(
2333 error,
2334 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2335 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
2336 "%s: unable to create sub node.",
2337 function );
2338
2339 goto on_error;
2340 }
2341 if( libcdata_tree_node_set_value(
2342 sub_node,
2343 value,
2344 error ) != 1 )
2345 {
2346 libcerror_error_set(
2347 error,
2348 LIBCERROR_ERROR_DOMAIN_MEMORY,
2349 LIBCERROR_MEMORY_ERROR_SET_FAILED,
2350 "%s: unable to set value in sub node.",
2351 function );
2352
2353 goto on_error;
2354 }
2355 if( libcdata_tree_node_append_node(
2356 node,
2357 sub_node,
2358 error ) != 1 )
2359 {
2360 libcerror_error_set(
2361 error,
2362 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2363 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
2364 "%s: unable to append sub node to node.",
2365 function );
2366
2367 goto on_error;
2368 }
2369 return( 1 );
2370
2371 on_error:
2372 if( sub_node != NULL )
2373 {
2374 libcdata_tree_node_free(
2375 &sub_node,
2376 NULL,
2377 NULL );
2378 }
2379 return( -1 );
2380 }
2381
2382
2383 /* Retrieves the node which the sub node should be inserted before
2384 *
2385 * Uses the value_compare_function to determine the order of the entries
2386 * The value_compare_function should return LIBCDATA_COMPARE_LESS,
2387 * LIBCDATA_COMPARE_EQUAL, LIBCDATA_COMPARE_GREATER if successful or -1 on error
2388 *
2389 * Duplicate entries are allowed by default and inserted after the last duplicate value.
2390 * Only allowing unique entries can be enforced by setting the flag LIBCDATA_INSERT_FLAG_UNIQUE_ENTRIES
2391 *
2392 * On return sub_node will be set to NULL if the sub node should be inserted at the end of the list.
2393 *
2394 * Returns 1 if successful, 0 if a sub node containing the value already exists or -1 on error
2395 */
2396 int libcdata_internal_tree_node_insert_node_find_sub_node(
2397 libcdata_internal_tree_node_t *internal_node,
2398 intptr_t *value_to_insert,
2399 int (*value_compare_function)(
2400 intptr_t *first_value,
2401 intptr_t *second_value,
2402 libcerror_error_t **error ),
2403 uint8_t insert_flags,
2404 int *sub_node_index,
2405 libcdata_tree_node_t **sub_node,
2406 libcerror_error_t **error )
2407 {
2408 libcdata_tree_node_t *sub_tree_node = NULL;
2409 intptr_t *sub_node_value = NULL;
2410 static char *function = "libcdata_internal_tree_node_insert_node_find_sub_node";
2411 int compare_result = 0;
2412 int result = 1;
2413 int safe_sub_node_index = 0;
2414
2415 if( internal_node == NULL )
2416 {
2417 libcerror_error_set(
2418 error,
2419 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2420 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2421 "%s: invalid node.",
2422 function );
2423
2424 return( -1 );
2425 }
2426 if( value_compare_function == NULL )
2427 {
2428 libcerror_error_set(
2429 error,
2430 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2431 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2432 "%s: invalid value compare function.",
2433 function );
2434
2435 return( -1 );
2436 }
2437 if( ( insert_flags & ~( LIBCDATA_INSERT_FLAG_UNIQUE_ENTRIES ) ) != 0 )
2438 {
2439 libcerror_error_set(
2440 error,
2441 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2442 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2443 "%s: unsupported insert flags: 0x%02" PRIx8 ".",
2444 function,
2445 insert_flags );
2446
2447 return( -1 );
2448 }
2449 if( sub_node_index == NULL )
2450 {
2451 libcerror_error_set(
2452 error,
2453 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2454 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2455 "%s: invalid sub node index.",
2456 function );
2457
2458 return( -1 );
2459 }
2460 if( sub_node == NULL )
2461 {
2462 libcerror_error_set(
2463 error,
2464 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2465 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2466 "%s: invalid sub node.",
2467 function );
2468
2469 return( -1 );
2470 }
2471 sub_tree_node = internal_node->first_sub_node;
2472
2473 for( safe_sub_node_index = 0;
2474 safe_sub_node_index < internal_node->number_of_sub_nodes;
2475 safe_sub_node_index++ )
2476 {
2477 if( libcdata_tree_node_get_value(
2478 sub_tree_node,
2479 &sub_node_value,
2480 error ) != 1 )
2481 {
2482 libcerror_error_set(
2483 error,
2484 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2485 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2486 "%s: unable to retrieve value of sub node: %d.",
2487 function,
2488 safe_sub_node_index );
2489
2490 return( -1 );
2491 }
2492 compare_result = value_compare_function(
2493 value_to_insert,
2494 sub_node_value,
2495 error );
2496
2497 if( compare_result == -1 )
2498 {
2499 libcerror_error_set(
2500 error,
2501 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2502 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2503 "%s: unable to compare sub node: %d.",
2504 function,
2505 safe_sub_node_index );
2506
2507 return( -1 );
2508 }
2509 else if( compare_result == LIBCDATA_COMPARE_EQUAL )
2510 {
2511 if( ( insert_flags & LIBCDATA_INSERT_FLAG_UNIQUE_ENTRIES ) != 0 )
2512 {
2513 result = 0;
2514
2515 break;
2516 }
2517 }
2518 else if( compare_result == LIBCDATA_COMPARE_LESS )
2519 {
2520 break;
2521 }
2522 else if( compare_result != LIBCDATA_COMPARE_GREATER )
2523 {
2524 libcerror_error_set(
2525 error,
2526 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2527 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2528 "%s: unsupported value compare function return value: %d.",
2529 function,
2530 result );
2531
2532 return( -1 );
2533 }
2534 if( libcdata_tree_node_get_next_node(
2535 sub_tree_node,
2536 &sub_tree_node,
2537 error ) != 1 )
2538 {
2539 libcerror_error_set(
2540 error,
2541 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2542 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2543 "%s: unable to retrieve next node of sub node: %d.",
2544 function,
2545 safe_sub_node_index );
2546
2547 return( -1 );
2548 }
2549 }
2550 if( ( compare_result == LIBCDATA_COMPARE_EQUAL )
2551 || ( compare_result == LIBCDATA_COMPARE_LESS ) )
2552 {
2553 *sub_node_index = safe_sub_node_index;
2554 *sub_node = sub_tree_node;
2555 }
2556 else
2557 {
2558 *sub_node_index = internal_node->number_of_sub_nodes;
2559 *sub_node = NULL;
2560 }
2561 return( result );
2562 }
2563
2564 /* Inserts the node before the sub node
2565 * If sub_node is NULL the node is inserted before or as the first sub node in the list
2566 * Returns 1 if successful, or -1 on error
2567 */
2568 int libcdata_internal_tree_node_insert_node_before_sub_node(
2569 libcdata_internal_tree_node_t *internal_node,
2570 libcdata_tree_node_t *sub_node,
2571 libcdata_tree_node_t *node_to_insert,
2572 libcerror_error_t **error )
2573 {
2574 libcdata_tree_node_t *backup_first_sub_node = NULL;
2575 libcdata_tree_node_t *backup_last_sub_node = NULL;
2576 libcdata_tree_node_t *previous_node = NULL;
2577 static char *function = "libcdata_internal_tree_node_insert_node_before_sub_node";
2578
2579 if( internal_node == NULL )
2580 {
2581 libcerror_error_set(
2582 error,
2583 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2584 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2585 "%s: invalid node.",
2586 function );
2587
2588 return( -1 );
2589 }
2590 if( node_to_insert == NULL )
2591 {
2592 libcerror_error_set(
2593 error,
2594 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2595 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2596 "%s: invalid node to insert.",
2597 function );
2598
2599 return( -1 );
2600 }
2601 backup_first_sub_node = internal_node->first_sub_node;
2602 backup_last_sub_node = internal_node->last_sub_node;
2603
2604 if( sub_node != NULL )
2605 {
2606 if( libcdata_tree_node_get_previous_node(
2607 sub_node,
2608 &previous_node,
2609 error ) != 1 )
2610 {
2611 libcerror_error_set(
2612 error,
2613 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2614 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2615 "%s: unable to retrieve previous node from sub node.",
2616 function );
2617
2618 return( -1 );
2619 }
2620 }
2621 if( internal_node->number_of_sub_nodes == 0 )
2622 {
2623 internal_node->first_sub_node = node_to_insert;
2624 internal_node->last_sub_node = node_to_insert;
2625 }
2626 else if( sub_node == NULL )
2627 {
2628 if( libcdata_internal_tree_node_set_last_sub_node(
2629 internal_node,
2630 node_to_insert,
2631 error ) != 1 )
2632 {
2633 libcerror_error_set(
2634 error,
2635 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2636 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2637 "%s: unable to set last sub node.",
2638 function );
2639
2640 goto on_error;
2641 }
2642 }
2643 else
2644 {
2645 if( libcdata_tree_node_set_nodes(
2646 node_to_insert,
2647 (libcdata_tree_node_t *) internal_node,
2648 previous_node,
2649 sub_node,
2650 error ) != 1 )
2651 {
2652 libcerror_error_set(
2653 error,
2654 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2655 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2656 "%s: unable to set parent, previous and next node of node to insert.",
2657 function );
2658
2659 goto on_error;
2660 }
2661 if( internal_node->first_sub_node == sub_node )
2662 {
2663 internal_node->first_sub_node = node_to_insert;
2664 }
2665 else
2666 {
2667 if( libcdata_tree_node_set_next_node(
2668 previous_node,
2669 node_to_insert,
2670 error ) != 1 )
2671 {
2672 libcerror_error_set(
2673 error,
2674 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2675 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2676 "%s: unable to set next node of previous node.",
2677 function );
2678
2679 goto on_error;
2680 }
2681 }
2682 if( libcdata_tree_node_set_previous_node(
2683 sub_node,
2684 node_to_insert,
2685 error ) != 1 )
2686 {
2687 libcerror_error_set(
2688 error,
2689 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2690 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2691 "%s: unable to set previous node of sub node.",
2692 function );
2693
2694 goto on_error;
2695 }
2696 }
2697 if( libcdata_tree_node_set_parent_node(
2698 node_to_insert,
2699 (libcdata_tree_node_t *) internal_node,
2700 error ) != 1 )
2701 {
2702 libcerror_error_set(
2703 error,
2704 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2705 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2706 "%s: unable to set parent node of node to insert.",
2707 function );
2708
2709 goto on_error;
2710 }
2711 internal_node->number_of_sub_nodes += 1;
2712
2713 return( 1 );
2714
2715 on_error:
2716 if( node_to_insert != NULL )
2717 {
2718 libcdata_tree_node_set_nodes(
2719 node_to_insert,
2720 NULL,
2721 NULL,
2722 NULL,
2723 NULL );
2724 }
2725 if( previous_node != NULL )
2726 {
2727 libcdata_tree_node_set_next_node(
2728 previous_node,
2729 sub_node,
2730 NULL );
2731 }
2732 if( sub_node != NULL )
2733 {
2734 libcdata_tree_node_set_previous_node(
2735 sub_node,
2736 previous_node,
2737 NULL );
2738 }
2739 internal_node->first_sub_node = backup_first_sub_node;
2740 internal_node->last_sub_node = backup_last_sub_node;
2741
2742 return( -1 );
2743 }
2744
2745 /* Inserts a sub node in the node
2746 *
2747 * Uses the value_compare_function to determine the order of the entries
2748 * The value_compare_function should return LIBCDATA_COMPARE_LESS,
2749 * LIBCDATA_COMPARE_EQUAL, LIBCDATA_COMPARE_GREATER if successful or -1 on error
2750 *
2751 * Duplicate entries are allowed by default and inserted after the last duplicate value.
2752 * Only allowing unique entries can be enforced by setting the flag LIBCDATA_INSERT_FLAG_UNIQUE_ENTRIES
2753 *
2754 * Returns 1 if successful, 0 if the node already exists or -1 on error
2755 */
2756 int libcdata_tree_node_insert_node(
2757 libcdata_tree_node_t *node,
2758 libcdata_tree_node_t *node_to_insert,
2759 int (*value_compare_function)(
2760 intptr_t *first_value,
2761 intptr_t *second_value,
2762 libcerror_error_t **error ),
2763 uint8_t insert_flags,
2764 libcerror_error_t **error )
2765 {
2766 libcdata_internal_tree_node_t *internal_node = NULL;
2767 libcdata_tree_node_t *next_node = NULL;
2768 libcdata_tree_node_t *parent_node = NULL;
2769 libcdata_tree_node_t *previous_node = NULL;
2770 libcdata_tree_node_t *sub_node = NULL;
2771 intptr_t *value_to_insert = NULL;
2772 static char *function = "libcdata_tree_node_insert_node";
2773 int result = 1;
2774 int sub_node_index = 0;
2775
2776 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
2777 libcdata_tree_node_t *backup_first_sub_node = NULL;
2778 libcdata_tree_node_t *backup_last_sub_node = NULL;
2779 #endif
2780
2781 if( node == NULL )
2782 {
2783 libcerror_error_set(
2784 error,
2785 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2786 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2787 "%s: invalid node.",
2788 function );
2789
2790 return( -1 );
2791 }
2792 internal_node = (libcdata_internal_tree_node_t *) node;
2793
2794 if( internal_node->number_of_sub_nodes == 0 )
2795 {
2796 if( internal_node->first_sub_node != NULL )
2797 {
2798 libcerror_error_set(
2799 error,
2800 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2801 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2802 "%s: corruption detected - first sub node already set.",
2803 function );
2804
2805 return( -1 );
2806 }
2807 if( internal_node->last_sub_node != NULL )
2808 {
2809 libcerror_error_set(
2810 error,
2811 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2812 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2813 "%s: corruption detected - last sub node already set.",
2814 function );
2815
2816 return( -1 );
2817 }
2818 }
2819 else
2820 {
2821 if( internal_node->first_sub_node == NULL )
2822 {
2823 libcerror_error_set(
2824 error,
2825 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2826 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2827 "%s: corruption detected - missing first sub node.",
2828 function );
2829
2830 return( -1 );
2831 }
2832 if( internal_node->last_sub_node == NULL )
2833 {
2834 libcerror_error_set(
2835 error,
2836 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2837 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2838 "%s: corruption detected - missing last sub node.",
2839 function );
2840
2841 return( -1 );
2842 }
2843 }
2844 if( node_to_insert == NULL )
2845 {
2846 libcerror_error_set(
2847 error,
2848 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2849 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2850 "%s: invalid node to insert.",
2851 function );
2852
2853 return( -1 );
2854 }
2855 if( value_compare_function == NULL )
2856 {
2857 libcerror_error_set(
2858 error,
2859 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2860 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2861 "%s: invalid value compare function.",
2862 function );
2863
2864 return( -1 );
2865 }
2866 if( ( insert_flags & ~( LIBCDATA_INSERT_FLAG_UNIQUE_ENTRIES ) ) != 0 )
2867 {
2868 libcerror_error_set(
2869 error,
2870 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2871 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2872 "%s: unsupported insert flags: 0x%02" PRIx8 ".",
2873 function,
2874 insert_flags );
2875
2876 return( -1 );
2877 }
2878 if( libcdata_tree_node_get_nodes(
2879 node_to_insert,
2880 &parent_node,
2881 &previous_node,
2882 &next_node,
2883 error ) != 1 )
2884 {
2885 libcerror_error_set(
2886 error,
2887 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2888 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2889 "%s: unable to retrieve nodes of node to insert.",
2890 function );
2891
2892 return( -1 );
2893 }
2894 if( ( parent_node != NULL )
2895 || ( previous_node != NULL )
2896 || ( next_node != NULL ) )
2897 {
2898 libcerror_error_set(
2899 error,
2900 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2901 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
2902 "%s: invalid node to insert - node is already part of a tree.",
2903 function );
2904
2905 return( -1 );
2906 }
2907 if( libcdata_tree_node_get_value(
2908 node_to_insert,
2909 &value_to_insert,
2910 error ) != 1 )
2911 {
2912 libcerror_error_set(
2913 error,
2914 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2915 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2916 "%s: unable to retrieve value from node to insert.",
2917 function );
2918
2919 return( -1 );
2920 }
2921 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
2922 if( libcthreads_read_write_lock_grab_for_write(
2923 internal_node->read_write_lock,
2924 error ) != 1 )
2925 {
2926 libcerror_error_set(
2927 error,
2928 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2929 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2930 "%s: unable to grab read/write lock for writing.",
2931 function );
2932
2933 return( -1 );
2934 }
2935 backup_first_sub_node = internal_node->first_sub_node;
2936 backup_last_sub_node = internal_node->last_sub_node;
2937 #endif
2938
2939 result = libcdata_internal_tree_node_insert_node_find_sub_node(
2940 internal_node,
2941 value_to_insert,
2942 value_compare_function,
2943 insert_flags,
2944 &sub_node_index,
2945 &sub_node,
2946 error );
2947
2948 if( result == -1 )
2949 {
2950 libcerror_error_set(
2951 error,
2952 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2953 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2954 "%s: unable to find sub node in tree node.",
2955 function );
2956
2957 result = -1;
2958 }
2959 else if( result == 1 )
2960 {
2961 if( sub_node != NULL )
2962 {
2963 if( libcdata_tree_node_get_nodes(
2964 sub_node,
2965 &parent_node,
2966 &previous_node,
2967 &next_node,
2968 error ) != 1 )
2969 {
2970 libcerror_error_set(
2971 error,
2972 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2973 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2974 "%s: unable to retrieve nodes of sub node: %d.",
2975 function,
2976 sub_node_index );
2977
2978 result = -1;
2979 }
2980 }
2981 if( result == 1 )
2982 {
2983 if( libcdata_internal_tree_node_insert_node_before_sub_node(
2984 internal_node,
2985 sub_node,
2986 node_to_insert,
2987 error ) != 1 )
2988 {
2989 libcerror_error_set(
2990 error,
2991 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2992 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
2993 "%s: unable to insert node before tree sub node.",
2994 function );
2995
2996 result = -1;
2997 }
2998 }
2999 }
3000 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
3001 if( libcthreads_read_write_lock_release_for_write(
3002 internal_node->read_write_lock,
3003 error ) != 1 )
3004 {
3005 libcerror_error_set(
3006 error,
3007 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3008 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3009 "%s: unable to release read/write lock for writing.",
3010 function );
3011
3012 goto on_error;
3013 }
3014 #endif
3015 return( result );
3016
3017 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
3018 on_error:
3019 if( result == 1 )
3020 {
3021 if( node_to_insert != NULL )
3022 {
3023 libcdata_tree_node_set_nodes(
3024 node_to_insert,
3025 NULL,
3026 NULL,
3027 NULL,
3028 NULL );
3029 }
3030 if( previous_node != NULL )
3031 {
3032 libcdata_tree_node_set_next_node(
3033 previous_node,
3034 sub_node,
3035 NULL );
3036 }
3037 if( sub_node != NULL )
3038 {
3039 libcdata_tree_node_set_previous_node(
3040 sub_node,
3041 previous_node,
3042 NULL );
3043 }
3044 internal_node->first_sub_node = backup_first_sub_node;
3045 internal_node->last_sub_node = backup_last_sub_node;
3046
3047 internal_node->number_of_sub_nodes -= 1;
3048 }
3049 return( -1 );
3050 #endif
3051 }
3052
3053 /* Inserts a value in the node
3054 *
3055 * Creates a new sub tree node
3056 *
3057 * Uses the value_compare_function to determine the order of the entries
3058 * The value_compare_function should return LIBCDATA_COMPARE_LESS,
3059 * LIBCDATA_COMPARE_EQUAL, LIBCDATA_COMPARE_GREATER if successful or -1 on error
3060 *
3061 * Duplicate entries are allowed by default and inserted after the last duplicate value.
3062 * Only allowing unique entries can be enforced by setting the flag LIBCDATA_INSERT_FLAG_UNIQUE_ENTRIES
3063 *
3064 * Returns 1 if successful, 0 if the node already exists or -1 on error
3065 */
3066 int libcdata_tree_node_insert_value(
3067 libcdata_tree_node_t *node,
3068 intptr_t *value,
3069 int (*value_compare_function)(
3070 intptr_t *first_value,
3071 intptr_t *second_value,
3072 libcerror_error_t **error ),
3073 uint8_t insert_flags,
3074 libcerror_error_t **error )
3075 {
3076 libcdata_tree_node_t *sub_node = NULL;
3077 static char *function = "libcdata_tree_node_insert_value";
3078 int result = 0;
3079
3080 if( node == NULL )
3081 {
3082 libcerror_error_set(
3083 error,
3084 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3085 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3086 "%s: invalid node.",
3087 function );
3088
3089 return( -1 );
3090 }
3091 if( libcdata_tree_node_initialize(
3092 &sub_node,
3093 error ) != 1 )
3094 {
3095 libcerror_error_set(
3096 error,
3097 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3098 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
3099 "%s: unable to create sub node.",
3100 function );
3101
3102 goto on_error;
3103 }
3104 if( libcdata_tree_node_set_value(
3105 sub_node,
3106 value,
3107 error ) != 1 )
3108 {
3109 libcerror_error_set(
3110 error,
3111 LIBCERROR_ERROR_DOMAIN_MEMORY,
3112 LIBCERROR_MEMORY_ERROR_SET_FAILED,
3113 "%s: unable to set value in sub node.",
3114 function );
3115
3116 goto on_error;
3117 }
3118 result = libcdata_tree_node_insert_node(
3119 node,
3120 sub_node,
3121 value_compare_function,
3122 insert_flags,
3123 error );
3124
3125 if( result == -1 )
3126 {
3127 libcerror_error_set(
3128 error,
3129 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3130 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
3131 "%s: unable to insert node.",
3132 function );
3133
3134 goto on_error;
3135 }
3136 else if( result == 0 )
3137 {
3138 if( libcdata_tree_node_free(
3139 &sub_node,
3140 NULL,
3141 error ) != 1 )
3142 {
3143 libcerror_error_set(
3144 error,
3145 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3146 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
3147 "%s: unable to free sub node.",
3148 function );
3149
3150 goto on_error;
3151 }
3152 }
3153 return( result );
3154
3155 on_error:
3156 if( sub_node != NULL )
3157 {
3158 libcdata_tree_node_free(
3159 &sub_node,
3160 NULL,
3161 NULL );
3162 }
3163 return( -1 );
3164 }
3165
3166 /* Replaces a tree node with another tree node
3167 * Returns 1 if successful or -1 on error
3168 */
3169 int libcdata_tree_node_replace_node(
3170 libcdata_tree_node_t *node,
3171 libcdata_tree_node_t *replacement_node,
3172 libcerror_error_t **error )
3173 {
3174 libcdata_internal_tree_node_t *internal_node = NULL;
3175 libcdata_tree_node_t *backup_parent_first_sub_node = NULL;
3176 libcdata_tree_node_t *backup_parent_last_sub_node = NULL;
3177 libcdata_tree_node_t *next_node = NULL;
3178 libcdata_tree_node_t *parent_first_sub_node = NULL;
3179 libcdata_tree_node_t *parent_last_sub_node = NULL;
3180 libcdata_tree_node_t *parent_node = NULL;
3181 libcdata_tree_node_t *previous_node = NULL;
3182 libcdata_tree_node_t *replacement_next_node = NULL;
3183 libcdata_tree_node_t *replacement_parent_node = NULL;
3184 libcdata_tree_node_t *replacement_previous_node = NULL;
3185 static char *function = "libcdata_tree_node_replace_node";
3186 int result = 1;
3187
3188 if( node == NULL )
3189 {
3190 libcerror_error_set(
3191 error,
3192 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3193 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3194 "%s: invalid node.",
3195 function );
3196
3197 return( -1 );
3198 }
3199 internal_node = (libcdata_internal_tree_node_t *) node;
3200
3201 if( replacement_node == NULL )
3202 {
3203 libcerror_error_set(
3204 error,
3205 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3206 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3207 "%s: invalid replacement node.",
3208 function );
3209
3210 return( -1 );
3211 }
3212 if( replacement_node == node )
3213 {
3214 libcerror_error_set(
3215 error,
3216 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3217 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
3218 "%s: cannot replace node with itself.",
3219 function );
3220
3221 return( -1 );
3222 }
3223 if( libcdata_tree_node_get_nodes(
3224 replacement_node,
3225 &replacement_parent_node,
3226 &replacement_previous_node,
3227 &replacement_next_node,
3228 error ) != 1 )
3229 {
3230 libcerror_error_set(
3231 error,
3232 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3233 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3234 "%s: unable to retrieve nodes of replacement node.",
3235 function );
3236
3237 return( -1 );
3238 }
3239 if( ( replacement_parent_node != NULL )
3240 || ( replacement_previous_node != NULL )
3241 || ( replacement_next_node != NULL ) )
3242 {
3243 libcerror_error_set(
3244 error,
3245 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3246 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
3247 "%s: invalid replacement node - already part of a tree.",
3248 function );
3249
3250 return( -1 );
3251 }
3252 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
3253 if( libcthreads_read_write_lock_grab_for_write(
3254 internal_node->read_write_lock,
3255 error ) != 1 )
3256 {
3257 libcerror_error_set(
3258 error,
3259 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3260 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3261 "%s: unable to grab read/write lock for writing.",
3262 function );
3263
3264 return( -1 );
3265 }
3266 #endif
3267 parent_node = internal_node->parent_node;
3268 previous_node = internal_node->previous_node;
3269 next_node = internal_node->next_node;
3270
3271 if( parent_node != NULL )
3272 {
3273 if( libcdata_tree_node_get_sub_nodes(
3274 parent_node,
3275 &parent_first_sub_node,
3276 &parent_last_sub_node,
3277 error ) != 1 )
3278 {
3279 libcerror_error_set(
3280 error,
3281 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3282 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3283 "%s: unable to retrieve sub nodes of parent node.",
3284 function );
3285
3286 result = -1;
3287 }
3288 }
3289 if( result == 1 )
3290 {
3291 if( libcdata_tree_node_set_nodes(
3292 replacement_node,
3293 parent_node,
3294 previous_node,
3295 next_node,
3296 error ) != 1 )
3297 {
3298 libcerror_error_set(
3299 error,
3300 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3301 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3302 "%s: unable to set nodes of replacement node.",
3303 function );
3304
3305 result = -1;
3306 }
3307 }
3308 if( result == 1 )
3309 {
3310 if( parent_node != NULL )
3311 {
3312 backup_parent_first_sub_node = parent_first_sub_node;
3313 backup_parent_last_sub_node = parent_last_sub_node;
3314
3315 if( parent_first_sub_node == node )
3316 {
3317 parent_first_sub_node = replacement_node;
3318 }
3319 if( parent_last_sub_node == node )
3320 {
3321 parent_last_sub_node = replacement_node;
3322 }
3323 if( libcdata_tree_node_set_sub_nodes(
3324 parent_node,
3325 parent_first_sub_node,
3326 parent_last_sub_node,
3327 error ) != 1 )
3328 {
3329 libcerror_error_set(
3330 error,
3331 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3332 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3333 "%s: unable to set sub nodes of parent node.",
3334 function );
3335
3336 libcdata_tree_node_set_nodes(
3337 replacement_node,
3338 NULL,
3339 NULL,
3340 NULL,
3341 NULL );
3342
3343 result = -1;
3344 }
3345 }
3346 }
3347 if( result == 1 )
3348 {
3349 if( previous_node != NULL )
3350 {
3351 if( libcdata_tree_node_set_next_node(
3352 previous_node,
3353 replacement_node,
3354 error ) != 1 )
3355 {
3356 libcerror_error_set(
3357 error,
3358 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3359 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3360 "%s: unable to set next node of previous node.",
3361 function );
3362
3363 libcdata_tree_node_set_nodes(
3364 replacement_node,
3365 NULL,
3366 NULL,
3367 NULL,
3368 NULL );
3369
3370 if( parent_node != NULL )
3371 {
3372 libcdata_tree_node_set_sub_nodes(
3373 parent_node,
3374 backup_parent_first_sub_node,
3375 backup_parent_last_sub_node,
3376 NULL );
3377 }
3378 result = -1;
3379 }
3380 }
3381 }
3382 if( result == 1 )
3383 {
3384 if( next_node != NULL )
3385 {
3386 if( libcdata_tree_node_set_previous_node(
3387 next_node,
3388 replacement_node,
3389 error ) != 1 )
3390 {
3391 libcerror_error_set(
3392 error,
3393 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3394 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3395 "%s: unable to set previous node of next node.",
3396 function );
3397
3398 libcdata_tree_node_set_nodes(
3399 replacement_node,
3400 NULL,
3401 NULL,
3402 NULL,
3403 NULL );
3404
3405 if( parent_node != NULL )
3406 {
3407 libcdata_tree_node_set_sub_nodes(
3408 parent_node,
3409 backup_parent_first_sub_node,
3410 backup_parent_last_sub_node,
3411 NULL );
3412 }
3413 if( previous_node != NULL )
3414 {
3415 libcdata_tree_node_set_next_node(
3416 previous_node,
3417 node,
3418 NULL );
3419 }
3420 result = -1;
3421 }
3422 }
3423 }
3424 if( result == 1 )
3425 {
3426 internal_node->parent_node = NULL;
3427 internal_node->previous_node = NULL;
3428 internal_node->next_node = NULL;
3429 }
3430 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
3431 if( libcthreads_read_write_lock_release_for_write(
3432 internal_node->read_write_lock,
3433 error ) != 1 )
3434 {
3435 libcerror_error_set(
3436 error,
3437 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3438 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3439 "%s: unable to release read/write lock for writing.",
3440 function );
3441
3442 goto on_error;
3443 }
3444 #endif
3445 return( result );
3446
3447 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
3448 on_error:
3449 if( result == 1 )
3450 {
3451 libcdata_tree_node_set_nodes(
3452 replacement_node,
3453 NULL,
3454 NULL,
3455 NULL,
3456 NULL );
3457
3458 if( parent_node != NULL )
3459 {
3460 libcdata_tree_node_set_sub_nodes(
3461 parent_node,
3462 backup_parent_first_sub_node,
3463 backup_parent_last_sub_node,
3464 NULL );
3465 }
3466 if( previous_node != NULL )
3467 {
3468 libcdata_tree_node_set_next_node(
3469 previous_node,
3470 node,
3471 NULL );
3472 }
3473 if( next_node != NULL )
3474 {
3475 libcdata_tree_node_set_previous_node(
3476 next_node,
3477 node,
3478 NULL );
3479 }
3480 internal_node->parent_node = parent_node;
3481 internal_node->previous_node = previous_node;
3482 internal_node->next_node = next_node;
3483 }
3484 return( -1 );
3485 #endif
3486 }
3487
3488 /* Removes a sub tree node from the tree node
3489 * Returns 1 if successful or -1 on error
3490 */
3491 int libcdata_tree_node_remove_node(
3492 libcdata_tree_node_t *node,
3493 libcdata_tree_node_t *sub_node_to_remove,
3494 libcerror_error_t **error )
3495 {
3496 libcdata_internal_tree_node_t *internal_node = NULL;
3497 libcdata_tree_node_t *next_node = NULL;
3498 libcdata_tree_node_t *parent_node = NULL;
3499 libcdata_tree_node_t *previous_node = NULL;
3500 static char *function = "libcdata_tree_node_remove_node";
3501 int result = 1;
3502
3503 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
3504 libcdata_tree_node_t *backup_first_sub_node = NULL;
3505 libcdata_tree_node_t *backup_last_sub_node = NULL;
3506 #endif
3507
3508 if( node == NULL )
3509 {
3510 libcerror_error_set(
3511 error,
3512 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3513 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3514 "%s: invalid node.",
3515 function );
3516
3517 return( -1 );
3518 }
3519 internal_node = (libcdata_internal_tree_node_t *) node;
3520
3521 if( internal_node->number_of_sub_nodes == 0 )
3522 {
3523 if( internal_node->first_sub_node != NULL )
3524 {
3525 libcerror_error_set(
3526 error,
3527 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3528 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
3529 "%s: corruption detected - first sub node already set.",
3530 function );
3531
3532 return( -1 );
3533 }
3534 if( internal_node->last_sub_node != NULL )
3535 {
3536 libcerror_error_set(
3537 error,
3538 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3539 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
3540 "%s: corruption detected - last sub node already set.",
3541 function );
3542
3543 return( -1 );
3544 }
3545 }
3546 else
3547 {
3548 if( internal_node->first_sub_node == NULL )
3549 {
3550 libcerror_error_set(
3551 error,
3552 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3553 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
3554 "%s: corruption detected - missing first sub node.",
3555 function );
3556
3557 return( -1 );
3558 }
3559 if( internal_node->last_sub_node == NULL )
3560 {
3561 libcerror_error_set(
3562 error,
3563 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3564 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
3565 "%s: corruption detected - missing last sub node.",
3566 function );
3567
3568 return( -1 );
3569 }
3570 }
3571 if( sub_node_to_remove == NULL )
3572 {
3573 libcerror_error_set(
3574 error,
3575 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3576 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3577 "%s: invalid sub node to remove.",
3578 function );
3579
3580 return( -1 );
3581 }
3582 if( internal_node->number_of_sub_nodes == 0 )
3583 {
3584 libcerror_error_set(
3585 error,
3586 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3587 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
3588 "%s: invalid node - missing number of sub nodes.",
3589 function );
3590
3591 return( -1 );
3592 }
3593 if( internal_node->first_sub_node == NULL )
3594 {
3595 libcerror_error_set(
3596 error,
3597 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3598 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
3599 "%s: invalid node - missing first sub node.",
3600 function );
3601
3602 return( -1 );
3603 }
3604 if( internal_node->last_sub_node == NULL )
3605 {
3606 libcerror_error_set(
3607 error,
3608 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3609 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
3610 "%s: invalid node - missing last sub node.",
3611 function );
3612
3613 return( -1 );
3614 }
3615 if( libcdata_tree_node_get_nodes(
3616 sub_node_to_remove,
3617 &parent_node,
3618 &previous_node,
3619 &next_node,
3620 error ) != 1 )
3621 {
3622 libcerror_error_set(
3623 error,
3624 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3625 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3626 "%s: unable to retrieve nodes of node to remove.",
3627 function );
3628
3629 return( -1 );
3630 }
3631 if( parent_node != node )
3632 {
3633 libcerror_error_set(
3634 error,
3635 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3636 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
3637 "%s: invalid node to remove - parent node mismatch.",
3638 function );
3639
3640 return( -1 );
3641 }
3642 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
3643 if( libcthreads_read_write_lock_grab_for_write(
3644 internal_node->read_write_lock,
3645 error ) != 1 )
3646 {
3647 libcerror_error_set(
3648 error,
3649 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3650 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3651 "%s: unable to grab read/write lock for writing.",
3652 function );
3653
3654 return( -1 );
3655 }
3656 backup_first_sub_node = internal_node->first_sub_node;
3657 backup_last_sub_node = internal_node->last_sub_node;
3658 #endif
3659 result = libcdata_tree_node_set_nodes(
3660 sub_node_to_remove,
3661 NULL,
3662 NULL,
3663 NULL,
3664 error );
3665
3666 if( result != 1 )
3667 {
3668 libcerror_error_set(
3669 error,
3670 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3671 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3672 "%s: unable to set nodes of node to remove.",
3673 function );
3674
3675 result = -1;
3676 }
3677 if( result == 1 )
3678 {
3679 if( next_node != NULL )
3680 {
3681 if( libcdata_tree_node_set_previous_node(
3682 next_node,
3683 previous_node,
3684 error ) != 1 )
3685 {
3686 libcerror_error_set(
3687 error,
3688 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3689 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3690 "%s: unable to set previous node of next node.",
3691 function );
3692
3693 libcdata_tree_node_set_nodes(
3694 sub_node_to_remove,
3695 parent_node,
3696 previous_node,
3697 next_node,
3698 NULL );
3699
3700 result = -1;
3701 }
3702 }
3703 }
3704 if( result == 1 )
3705 {
3706 if( previous_node != NULL )
3707 {
3708 if( libcdata_tree_node_set_next_node(
3709 previous_node,
3710 next_node,
3711 error ) != 1 )
3712 {
3713 libcerror_error_set(
3714 error,
3715 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3716 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3717 "%s: unable to set next node of previous node.",
3718 function );
3719
3720 libcdata_tree_node_set_nodes(
3721 sub_node_to_remove,
3722 parent_node,
3723 previous_node,
3724 next_node,
3725 NULL );
3726
3727 if( next_node != NULL )
3728 {
3729 libcdata_tree_node_set_previous_node(
3730 next_node,
3731 sub_node_to_remove,
3732 NULL );
3733 }
3734 result = -1;
3735 }
3736 }
3737 }
3738 if( result == 1 )
3739 {
3740 if( internal_node->first_sub_node == sub_node_to_remove )
3741 {
3742 internal_node->first_sub_node = next_node;
3743 }
3744 if( internal_node->last_sub_node == sub_node_to_remove )
3745 {
3746 internal_node->last_sub_node = previous_node;
3747 }
3748 internal_node->number_of_sub_nodes -= 1;
3749 }
3750 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
3751 if( libcthreads_read_write_lock_release_for_write(
3752 internal_node->read_write_lock,
3753 error ) != 1 )
3754 {
3755 libcerror_error_set(
3756 error,
3757 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3758 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3759 "%s: unable to release read/write lock for writing.",
3760 function );
3761
3762 goto on_error;
3763 }
3764 #endif
3765 return( 1 );
3766
3767 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
3768 on_error:
3769 if( result == 1 )
3770 {
3771 libcdata_tree_node_set_nodes(
3772 sub_node_to_remove,
3773 parent_node,
3774 previous_node,
3775 next_node,
3776 NULL );
3777
3778 if( next_node != NULL )
3779 {
3780 libcdata_tree_node_set_previous_node(
3781 next_node,
3782 sub_node_to_remove,
3783 NULL );
3784 }
3785 if( previous_node != NULL )
3786 {
3787 libcdata_tree_node_set_next_node(
3788 previous_node,
3789 sub_node_to_remove,
3790 NULL );
3791 }
3792 internal_node->first_sub_node = backup_first_sub_node;
3793 internal_node->last_sub_node = backup_last_sub_node;
3794
3795 internal_node->number_of_sub_nodes += 1;
3796 }
3797 return( -1 );
3798 #endif
3799 }
3800
3801 /* Retrieves the number of sub nodes in the tree node
3802 * Returns 1 if successful or -1 on error
3803 */
3804 int libcdata_tree_node_get_number_of_sub_nodes(
3805 libcdata_tree_node_t *node,
3806 int *number_of_sub_nodes,
3807 libcerror_error_t **error )
3808 {
3809 libcdata_internal_tree_node_t *internal_node = NULL;
3810 static char *function = "libcdata_tree_node_get_number_of_sub_nodes";
3811
3812 if( node == NULL )
3813 {
3814 libcerror_error_set(
3815 error,
3816 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3817 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3818 "%s: invalid node.",
3819 function );
3820
3821 return( -1 );
3822 }
3823 internal_node = (libcdata_internal_tree_node_t *) node;
3824
3825 if( number_of_sub_nodes == NULL )
3826 {
3827 libcerror_error_set(
3828 error,
3829 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3830 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3831 "%s: invalid number of sub nodes.",
3832 function );
3833
3834 return( -1 );
3835 }
3836 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
3837 if( libcthreads_read_write_lock_grab_for_read(
3838 internal_node->read_write_lock,
3839 error ) != 1 )
3840 {
3841 libcerror_error_set(
3842 error,
3843 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3844 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3845 "%s: unable to grab read/write lock for reading.",
3846 function );
3847
3848 return( -1 );
3849 }
3850 #endif
3851 *number_of_sub_nodes = internal_node->number_of_sub_nodes;
3852
3853 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
3854 if( libcthreads_read_write_lock_release_for_read(
3855 internal_node->read_write_lock,
3856 error ) != 1 )
3857 {
3858 libcerror_error_set(
3859 error,
3860 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3861 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3862 "%s: unable to release read/write lock for reading.",
3863 function );
3864
3865 return( -1 );
3866 }
3867 #endif
3868 return( 1 );
3869 }
3870
3871 /* Retrieves a specific sub node from the tree node
3872 * Returns 1 if successful or -1 on error
3873 */
3874 int libcdata_tree_node_get_sub_node_by_index(
3875 libcdata_tree_node_t *node,
3876 int sub_node_index,
3877 libcdata_tree_node_t **sub_node,
3878 libcerror_error_t **error )
3879 {
3880 libcdata_internal_tree_node_t *internal_node = NULL;
3881 libcdata_tree_node_t *safe_sub_node = NULL;
3882 static char *function = "libcdata_tree_node_get_sub_node_by_index";
3883 int result = -1;
3884 int sub_node_iterator = 0;
3885
3886 if( node == NULL )
3887 {
3888 libcerror_error_set(
3889 error,
3890 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3891 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3892 "%s: invalid node.",
3893 function );
3894
3895 return( -1 );
3896 }
3897 internal_node = (libcdata_internal_tree_node_t *) node;
3898
3899 if( ( sub_node_index < 0 )
3900 || ( sub_node_index >= internal_node->number_of_sub_nodes ) )
3901 {
3902 libcerror_error_set(
3903 error,
3904 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3905 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
3906 "%s: invalid sub node index value out of bounds.",
3907 function );
3908
3909 return( -1 );
3910 }
3911 if( sub_node == NULL )
3912 {
3913 libcerror_error_set(
3914 error,
3915 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3916 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3917 "%s: invalid sub node.",
3918 function );
3919
3920 return( -1 );
3921 }
3922 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
3923 if( libcthreads_read_write_lock_grab_for_read(
3924 internal_node->read_write_lock,
3925 error ) != 1 )
3926 {
3927 libcerror_error_set(
3928 error,
3929 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3930 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3931 "%s: unable to grab read/write lock for reading.",
3932 function );
3933
3934 return( -1 );
3935 }
3936 #endif
3937 /* Check if the sub nodes should be searched front to back
3938 * or back to front
3939 */
3940 if( sub_node_index < ( internal_node->number_of_sub_nodes / 2 ) )
3941 {
3942 safe_sub_node = internal_node->first_sub_node;
3943
3944 for( sub_node_iterator = 0;
3945 sub_node_iterator < internal_node->number_of_sub_nodes;
3946 sub_node_iterator++ )
3947 {
3948 if( sub_node_iterator == sub_node_index )
3949 {
3950 result = 1;
3951
3952 break;
3953 }
3954 if( libcdata_tree_node_get_next_node(
3955 safe_sub_node,
3956 &safe_sub_node,
3957 error ) != 1 )
3958 {
3959 libcerror_error_set(
3960 error,
3961 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3962 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3963 "%s: unable to retrieve next node of sub node: %d.",
3964 function,
3965 sub_node_iterator );
3966
3967 goto on_error;
3968 }
3969 }
3970 }
3971 else
3972 {
3973 safe_sub_node = internal_node->last_sub_node;
3974
3975 for( sub_node_iterator = ( internal_node->number_of_sub_nodes - 1 );
3976 sub_node_iterator >= 0;
3977 sub_node_iterator-- )
3978 {
3979 if( sub_node_iterator == sub_node_index )
3980 {
3981 result = 1;
3982
3983 break;
3984 }
3985 if( libcdata_tree_node_get_previous_node(
3986 safe_sub_node,
3987 &safe_sub_node,
3988 error ) != 1 )
3989 {
3990 libcerror_error_set(
3991 error,
3992 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3993 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3994 "%s: unable to retrieve previous node of sub node: %d.",
3995 function,
3996 sub_node_iterator );
3997
3998 goto on_error;
3999 }
4000 }
4001 }
4002 *sub_node = safe_sub_node;
4003
4004 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
4005 if( libcthreads_read_write_lock_release_for_read(
4006 internal_node->read_write_lock,
4007 error ) != 1 )
4008 {
4009 libcerror_error_set(
4010 error,
4011 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4012 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
4013 "%s: unable to release read/write lock for reading.",
4014 function );
4015
4016 return( -1 );
4017 }
4018 #endif
4019 return( result );
4020
4021 on_error:
4022 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
4023 libcthreads_read_write_lock_release_for_read(
4024 internal_node->read_write_lock,
4025 NULL );
4026 #endif
4027 return( -1 );
4028 }
4029
4030 /* Retrieves a list of all the leaf nodes
4031 * Returns 1 if successful or -1 on error
4032 */
4033 int libcdata_tree_node_get_leaf_node_list(
4034 libcdata_tree_node_t *node,
4035 libcdata_list_t **leaf_node_list,
4036 libcerror_error_t **error )
4037 {
4038 libcdata_internal_tree_node_t *internal_node = NULL;
4039 libcdata_tree_node_t *sub_node = NULL;
4040 static char *function = "libcdata_tree_node_get_leaf_node_list";
4041 int leaf_node_list_created_in_node = 0;
4042 int sub_node_index = 0;
4043
4044 if( node == NULL )
4045 {
4046 libcerror_error_set(
4047 error,
4048 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4049 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4050 "%s: invalid node.",
4051 function );
4052
4053 return( -1 );
4054 }
4055 internal_node = (libcdata_internal_tree_node_t *) node;
4056
4057 if( leaf_node_list == NULL )
4058 {
4059 libcerror_error_set(
4060 error,
4061 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4062 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4063 "%s: invalid leaf node list.",
4064 function );
4065
4066 return( -1 );
4067 }
4068 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
4069 if( libcthreads_read_write_lock_grab_for_read(
4070 internal_node->read_write_lock,
4071 error ) != 1 )
4072 {
4073 libcerror_error_set(
4074 error,
4075 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4076 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
4077 "%s: unable to grab read/write lock for reading.",
4078 function );
4079
4080 return( -1 );
4081 }
4082 #endif
4083 if( *leaf_node_list == NULL )
4084 {
4085 if( libcdata_list_initialize(
4086 leaf_node_list,
4087 error ) != 1 )
4088 {
4089 libcerror_error_set(
4090 error,
4091 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4092 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
4093 "%s: unable to create leaf node list.",
4094 function );
4095
4096 goto on_error;
4097 }
4098 leaf_node_list_created_in_node = 1;
4099 }
4100 /* Traverse the sub nodes
4101 */
4102 if( internal_node->number_of_sub_nodes == 0 )
4103 {
4104 if( internal_node->value == NULL )
4105 {
4106 libcerror_error_set(
4107 error,
4108 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4109 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
4110 "%s: invalid node - missing value.",
4111 function );
4112
4113 goto on_error;
4114 }
4115 if( libcdata_list_append_value(
4116 *leaf_node_list,
4117 internal_node->value,
4118 error ) != 1 )
4119 {
4120 libcerror_error_set(
4121 error,
4122 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4123 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
4124 "%s: unable to append tree node to leaf node list.",
4125 function );
4126
4127 goto on_error;
4128 }
4129 }
4130 else
4131 {
4132 sub_node = internal_node->first_sub_node;
4133
4134 for( sub_node_index = 0;
4135 sub_node_index < internal_node->number_of_sub_nodes;
4136 sub_node_index++ )
4137 {
4138 if( sub_node == NULL )
4139 {
4140 libcerror_error_set(
4141 error,
4142 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4143 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
4144 "%s: corruption detected for sub node: %d.",
4145 function,
4146 sub_node_index );
4147
4148 goto on_error;
4149 }
4150 if( libcdata_tree_node_get_leaf_node_list(
4151 sub_node,
4152 leaf_node_list,
4153 error ) != 1 )
4154 {
4155 libcerror_error_set(
4156 error,
4157 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4158 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
4159 "%s: unable to traverse sub node: %d.",
4160 function,
4161 sub_node_index );
4162
4163 goto on_error;
4164 }
4165 if( libcdata_tree_node_get_next_node(
4166 sub_node,
4167 &sub_node,
4168 error ) != 1 )
4169 {
4170 libcerror_error_set(
4171 error,
4172 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4173 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
4174 "%s: unable to retrieve next node of sub node: %d.",
4175 function,
4176 sub_node_index );
4177
4178 goto on_error;
4179 }
4180 }
4181 }
4182 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
4183 if( libcthreads_read_write_lock_release_for_read(
4184 internal_node->read_write_lock,
4185 error ) != 1 )
4186 {
4187 libcerror_error_set(
4188 error,
4189 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4190 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
4191 "%s: unable to release read/write lock for reading.",
4192 function );
4193
4194 return( -1 );
4195 }
4196 #endif
4197 return( 1 );
4198
4199 on_error:
4200 if( leaf_node_list_created_in_node != 0 )
4201 {
4202 if( *leaf_node_list == NULL )
4203 {
4204 libcdata_list_initialize(
4205 leaf_node_list,
4206 NULL );
4207 }
4208 }
4209 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBCDATA )
4210 libcthreads_read_write_lock_release_for_read(
4211 internal_node->read_write_lock,
4212 NULL );
4213 #endif
4214 return( -1 );
4215 }
4216