Debian Codegraph @

libbfio/libbfio_pool.c

raw symbols SCIP
1 /*
2 * The internal pool functions
3 *
4 * Copyright (C) 2009-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 #if defined( HAVE_UNISTD_H )
27 #include <unistd.h>
28 #endif
29
30 #include "libbfio_definitions.h"
31 #include "libbfio_handle.h"
32 #include "libbfio_libcdata.h"
33 #include "libbfio_libcerror.h"
34 #include "libbfio_pool.h"
35 #include "libbfio_types.h"
36
37 /* Creates a pool
38 * Make sure the value pool is referencing, is set to NULL
39 * Returns 1 if successful or -1 on error
40 */
41 int libbfio_pool_initialize(
42 libbfio_pool_t **pool,
43 int number_of_handles,
44 int maximum_number_of_open_handles,
45 libcerror_error_t **error )
46 {
47 libbfio_internal_pool_t *internal_pool = NULL;
48 static char *function = "libbfio_pool_initialize";
49
50 if( pool == NULL )
51 {
52 libcerror_error_set(
53 error,
54 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
55 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
56 "%s: invalid pool.",
57 function );
58
59 return( -1 );
60 }
61 if( *pool != NULL )
62 {
63 libcerror_error_set(
64 error,
65 LIBCERROR_ERROR_DOMAIN_RUNTIME,
66 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
67 "%s: invalid pool value already set.",
68 function );
69
70 return( -1 );
71 }
72 if( number_of_handles < 0 )
73 {
74 libcerror_error_set(
75 error,
76 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
77 LIBCERROR_ARGUMENT_ERROR_VALUE_LESS_THAN_ZERO,
78 "%s: invalid number of handles value less than zero.",
79 function );
80
81 return( -1 );
82 }
83 if( maximum_number_of_open_handles < 0 )
84 {
85 libcerror_error_set(
86 error,
87 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
88 LIBCERROR_ARGUMENT_ERROR_VALUE_LESS_THAN_ZERO,
89 "%s: invalid maximum number of open handles value less than zero.",
90 function );
91
92 return( -1 );
93 }
94 internal_pool = memory_allocate_structure(
95 libbfio_internal_pool_t );
96
97 if( internal_pool == NULL )
98 {
99 libcerror_error_set(
100 error,
101 LIBCERROR_ERROR_DOMAIN_MEMORY,
102 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
103 "%s: unable to create pool.",
104 function );
105
106 goto on_error;
107 }
108 if( memory_set(
109 internal_pool,
110 0,
111 sizeof( libbfio_internal_pool_t ) ) == NULL )
112 {
113 libcerror_error_set(
114 error,
115 LIBCERROR_ERROR_DOMAIN_MEMORY,
116 LIBCERROR_MEMORY_ERROR_SET_FAILED,
117 "%s: unable to clear pool.",
118 function );
119
120 memory_free(
121 internal_pool );
122
123 return( -1 );
124 }
125 if( libcdata_array_initialize(
126 &( internal_pool->handles_array ),
127 number_of_handles,
128 error ) != 1 )
129 {
130 libcerror_error_set(
131 error,
132 LIBCERROR_ERROR_DOMAIN_RUNTIME,
133 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
134 "%s: unable to create handles array.",
135 function );
136
137 goto on_error;
138 }
139 if( libcdata_list_initialize(
140 &( internal_pool->last_used_list ),
141 error ) != 1 )
142 {
143 libcerror_error_set(
144 error,
145 LIBCERROR_ERROR_DOMAIN_RUNTIME,
146 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
147 "%s: unable to create last used list.",
148 function );
149
150 goto on_error;
151 }
152 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
153 if( libcthreads_read_write_lock_initialize(
154 &( internal_pool->read_write_lock ),
155 error ) != 1 )
156 {
157 libcerror_error_set(
158 error,
159 LIBCERROR_ERROR_DOMAIN_RUNTIME,
160 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
161 "%s: unable to initialize read/write lock.",
162 function );
163
164 goto on_error;
165 }
166 #endif
167 internal_pool->maximum_number_of_open_handles = maximum_number_of_open_handles;
168 internal_pool->current_entry = -1;
169
170 *pool = (libbfio_pool_t *) internal_pool;
171
172 return( 1 );
173
174 on_error:
175 if( internal_pool != NULL )
176 {
177 if( internal_pool->last_used_list != NULL )
178 {
179 libcdata_list_free(
180 &( internal_pool->last_used_list ),
181 NULL,
182 NULL );
183 }
184 if( internal_pool->handles_array != NULL )
185 {
186 libcdata_array_free(
187 &( internal_pool->handles_array ),
188 (int (*)(intptr_t **, libcerror_error_t **)) &libbfio_handle_free,
189 NULL );
190 }
191 memory_free(
192 internal_pool );
193 }
194 return( -1 );
195 }
196
197 /* Frees a pool
198 * Returns 1 if successful or -1 on error
199 */
200 int libbfio_pool_free(
201 libbfio_pool_t **pool,
202 libcerror_error_t **error )
203 {
204 libbfio_internal_pool_t *internal_pool = NULL;
205 static char *function = "libbfio_pool_free";
206 int result = 1;
207
208 if( pool == NULL )
209 {
210 libcerror_error_set(
211 error,
212 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
213 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
214 "%s: invalid pool.",
215 function );
216
217 return( -1 );
218 }
219 if( *pool != NULL )
220 {
221 internal_pool = (libbfio_internal_pool_t *) *pool;
222 *pool = NULL;
223
224 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
225 if( libcthreads_read_write_lock_free(
226 &( internal_pool->read_write_lock ),
227 error ) != 1 )
228 {
229 libcerror_error_set(
230 error,
231 LIBCERROR_ERROR_DOMAIN_RUNTIME,
232 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
233 "%s: unable to free read/write lock.",
234 function );
235
236 result = -1;
237 }
238 #endif
239 if( libcdata_array_free(
240 &( internal_pool->handles_array ),
241 (int (*)(intptr_t **, libcerror_error_t **)) &libbfio_handle_free,
242 error ) != 1 )
243 {
244 libcerror_error_set(
245 error,
246 LIBCERROR_ERROR_DOMAIN_RUNTIME,
247 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
248 "%s: unable to free handles array.",
249 function );
250
251 result = -1;
252 }
253 if( libcdata_list_free(
254 &( internal_pool->last_used_list ),
255 NULL,
256 error ) != 1 )
257 {
258 libcerror_error_set(
259 error,
260 LIBCERROR_ERROR_DOMAIN_RUNTIME,
261 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
262 "%s: unable to free last used list.",
263 function );
264
265 result = -1;
266 }
267 memory_free(
268 internal_pool );
269 }
270 return( result );
271 }
272
273 /* Clones (duplicates) the pool
274 * The values in the offsets read list are not duplicated
275 * Returns 1 if successful or -1 on error
276 */
277 int libbfio_pool_clone(
278 libbfio_pool_t **destination_pool,
279 libbfio_pool_t *source_pool,
280 libcerror_error_t **error )
281 {
282 libbfio_internal_pool_t *internal_destination_pool = NULL;
283 libbfio_internal_pool_t *internal_source_pool = NULL;
284 static char *function = "libbfio_pool_clone";
285
286 if( destination_pool == NULL )
287 {
288 libcerror_error_set(
289 error,
290 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
291 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
292 "%s: invalid destination pool.",
293 function );
294
295 return( -1 );
296 }
297 if( *destination_pool != NULL )
298 {
299 libcerror_error_set(
300 error,
301 LIBCERROR_ERROR_DOMAIN_RUNTIME,
302 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
303 "%s: destination pool already set.",
304 function );
305
306 return( -1 );
307 }
308 if( source_pool == NULL )
309 {
310 *destination_pool = NULL;
311
312 return( 1 );
313 }
314 internal_source_pool = (libbfio_internal_pool_t *) source_pool;
315
316 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
317 if( libcthreads_read_write_lock_grab_for_read(
318 internal_source_pool->read_write_lock,
319 error ) != 1 )
320 {
321 libcerror_error_set(
322 error,
323 LIBCERROR_ERROR_DOMAIN_RUNTIME,
324 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
325 "%s: unable to grab read/write lock for reading.",
326 function );
327
328 return( -1 );
329 }
330 #endif
331 internal_destination_pool = memory_allocate_structure(
332 libbfio_internal_pool_t );
333
334 if( internal_destination_pool == NULL )
335 {
336 libcerror_error_set(
337 error,
338 LIBCERROR_ERROR_DOMAIN_MEMORY,
339 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
340 "%s: unable to create destination pool.",
341 function );
342
343 goto on_error;
344 }
345 if( memory_set(
346 internal_destination_pool,
347 0,
348 sizeof( libbfio_internal_pool_t ) ) == NULL )
349 {
350 libcerror_error_set(
351 error,
352 LIBCERROR_ERROR_DOMAIN_MEMORY,
353 LIBCERROR_MEMORY_ERROR_SET_FAILED,
354 "%s: unable to clear destination pool.",
355 function );
356
357 memory_free(
358 internal_destination_pool );
359
360 internal_destination_pool = NULL;
361
362 goto on_error;
363 }
364 internal_destination_pool->current_entry = -1;
365 internal_destination_pool->current_handle = NULL;
366
367 if( libcdata_array_clone(
368 &( internal_destination_pool->handles_array ),
369 internal_source_pool->handles_array,
370 (int (*)(intptr_t **, libcerror_error_t **)) &libbfio_handle_free,
371 (int (*)(intptr_t **, intptr_t *, libcerror_error_t **)) &libbfio_handle_clone,
372 error ) != 1 )
373 {
374 libcerror_error_set(
375 error,
376 LIBCERROR_ERROR_DOMAIN_RUNTIME,
377 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
378 "%s: unable to create handles array.",
379 function );
380
381 goto on_error;
382 }
383 if( libcdata_list_initialize(
384 &( internal_destination_pool->last_used_list ),
385 error ) != 1 )
386 {
387 libcerror_error_set(
388 error,
389 LIBCERROR_ERROR_DOMAIN_RUNTIME,
390 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
391 "%s: unable to create last used list.",
392 function );
393
394 goto on_error;
395 }
396 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
397 if( libcthreads_read_write_lock_initialize(
398 &( internal_destination_pool->read_write_lock ),
399 error ) != 1 )
400 {
401 libcerror_error_set(
402 error,
403 LIBCERROR_ERROR_DOMAIN_RUNTIME,
404 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
405 "%s: unable to initialize read/write lock.",
406 function );
407
408 goto on_error;
409 }
410 #endif
411 internal_destination_pool->maximum_number_of_open_handles = internal_source_pool->maximum_number_of_open_handles;
412
413 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
414 if( libcthreads_read_write_lock_release_for_read(
415 internal_source_pool->read_write_lock,
416 error ) != 1 )
417 {
418 libcerror_error_set(
419 error,
420 LIBCERROR_ERROR_DOMAIN_RUNTIME,
421 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
422 "%s: unable to release read/write lock for reading.",
423 function );
424
425 libbfio_pool_free(
426 (libbfio_pool_t **) &internal_destination_pool,
427 NULL );
428
429 return( -1 );
430 }
431 #endif
432 *destination_pool = (libbfio_pool_t *) internal_destination_pool;
433
434 return( 1 );
435
436 on_error:
437 if( internal_destination_pool != NULL )
438 {
439 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
440 if( internal_destination_pool->read_write_lock != NULL )
441 {
442 libcthreads_read_write_lock_free(
443 &( internal_destination_pool->read_write_lock ),
444 NULL );
445 }
446 #endif
447 if( internal_destination_pool->last_used_list != NULL )
448 {
449 libcdata_list_free(
450 &( internal_destination_pool->last_used_list ),
451 NULL,
452 NULL );
453 }
454 if( internal_destination_pool->handles_array != NULL )
455 {
456 libcdata_array_free(
457 &( internal_destination_pool->handles_array ),
458 (int (*)(intptr_t **, libcerror_error_t **)) &libbfio_handle_free,
459 NULL );
460 }
461 memory_free(
462 internal_destination_pool );
463 }
464 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
465 libcthreads_read_write_lock_release_for_read(
466 internal_source_pool->read_write_lock,
467 NULL );
468 #endif
469 return( -1 );
470 }
471
472 /* Resizes the pool
473 * Returns 1 if successful or -1 on error
474 */
475 int libbfio_pool_resize(
476 libbfio_pool_t *pool,
477 int number_of_handles,
478 libcerror_error_t **error )
479 {
480 libbfio_internal_pool_t *internal_pool = NULL;
481 static char *function = "libbfio_pool_resize";
482 int result = 1;
483
484 if( pool == NULL )
485 {
486 libcerror_error_set(
487 error,
488 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
489 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
490 "%s: invalid pool.",
491 function );
492
493 return( -1 );
494 }
495 internal_pool = (libbfio_internal_pool_t *) pool;
496
497 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
498 if( libcthreads_read_write_lock_grab_for_write(
499 internal_pool->read_write_lock,
500 error ) != 1 )
501 {
502 libcerror_error_set(
503 error,
504 LIBCERROR_ERROR_DOMAIN_RUNTIME,
505 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
506 "%s: unable to grab read/write lock for writing.",
507 function );
508
509 return( -1 );
510 }
511 #endif
512 if( libcdata_array_resize(
513 internal_pool->handles_array,
514 number_of_handles,
515 (int (*)(intptr_t **, libcerror_error_t **)) &libbfio_handle_free,
516 error ) != 1 )
517 {
518 libcerror_error_set(
519 error,
520 LIBCERROR_ERROR_DOMAIN_RUNTIME,
521 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
522 "%s: unable to resize handles array.",
523 function );
524
525 result = -1;
526 }
527 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
528 if( libcthreads_read_write_lock_release_for_write(
529 internal_pool->read_write_lock,
530 error ) != 1 )
531 {
532 libcerror_error_set(
533 error,
534 LIBCERROR_ERROR_DOMAIN_RUNTIME,
535 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
536 "%s: unable to release read/write lock for writing.",
537 function );
538
539 return( -1 );
540 }
541 #endif
542 return( result );
543 }
544
545 /* Opens the handle
546 * Returns 1 if successful or -1 on error
547 */
548 int libbfio_internal_pool_open_handle(
549 libbfio_internal_pool_t *internal_pool,
550 libbfio_handle_t *handle,
551 int access_flags,
552 libcerror_error_t **error )
553 {
554 libbfio_internal_handle_t *internal_handle = NULL;
555 static char *function = "libbfio_internal_pool_open_handle";
556 int is_open = 0;
557
558 if( internal_pool == NULL )
559 {
560 libcerror_error_set(
561 error,
562 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
563 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
564 "%s: invalid pool.",
565 function );
566
567 return( -1 );
568 }
569 if( internal_pool->last_used_list == NULL )
570 {
571 libcerror_error_set(
572 error,
573 LIBCERROR_ERROR_DOMAIN_RUNTIME,
574 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
575 "%s: invalid pool - missing last used list.",
576 function );
577
578 return( -1 );
579 }
580 if( handle == NULL )
581 {
582 libcerror_error_set(
583 error,
584 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
585 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
586 "%s: invalid handle.",
587 function );
588
589 return( -1 );
590 }
591 is_open = libbfio_handle_is_open(
592 handle,
593 error );
594
595 if( is_open == -1 )
596 {
597 libcerror_error_set(
598 error,
599 LIBCERROR_ERROR_DOMAIN_RUNTIME,
600 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
601 "%s: unable to determine if handle is open.",
602 function );
603
604 return( -1 );
605 }
606 else if( is_open == 1 )
607 {
608 return( 1 );
609 }
610 if( internal_pool->maximum_number_of_open_handles != LIBBFIO_POOL_UNLIMITED_NUMBER_OF_OPEN_HANDLES )
611 {
612 if( libbfio_internal_pool_append_handle_to_last_used_list(
613 internal_pool,
614 handle,
615 error ) != 1 )
616 {
617 libcerror_error_set(
618 error,
619 LIBCERROR_ERROR_DOMAIN_RUNTIME,
620 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
621 "%s: unable to append handle to last used list.",
622 function );
623
624 return( -1 );
625 }
626 }
627 if( libbfio_handle_open(
628 handle,
629 access_flags,
630 error ) != 1 )
631 {
632 libcerror_error_set(
633 error,
634 LIBCERROR_ERROR_DOMAIN_IO,
635 LIBCERROR_IO_ERROR_OPEN_FAILED,
636 "%s: unable to open handle.",
637 function );
638
639 return( -1 );
640 }
641 internal_handle = (libbfio_internal_handle_t *) handle;
642
643 if( libbfio_handle_seek_offset(
644 handle,
645 internal_handle->current_offset,
646 SEEK_SET,
647 error ) == -1 )
648 {
649 libcerror_error_set(
650 error,
651 LIBCERROR_ERROR_DOMAIN_IO,
652 LIBCERROR_IO_ERROR_SEEK_FAILED,
653 "%s: unable to seek offset.",
654 function );
655
656 return( -1 );
657 }
658 return( 1 );
659 }
660
661 /* Appends the handle to the last used list
662 * Closes the less frequently used handle if necessary
663 * Returns 1 if successful or -1 on error
664 */
665 int libbfio_internal_pool_append_handle_to_last_used_list(
666 libbfio_internal_pool_t *internal_pool,
667 const libbfio_handle_t *handle,
668 libcerror_error_t **error )
669 {
670 libbfio_internal_handle_t *internal_handle = NULL;
671 libcdata_list_element_t *last_used_list_element = NULL;
672 libcdata_list_element_t *safe_last_used_list_element = NULL;
673 static char *function = "libbfio_internal_pool_append_handle_to_last_used_list";
674
675 if( internal_pool == NULL )
676 {
677 libcerror_error_set(
678 error,
679 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
680 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
681 "%s: invalid pool.",
682 function );
683
684 return( -1 );
685 }
686 if( internal_pool->last_used_list == NULL )
687 {
688 libcerror_error_set(
689 error,
690 LIBCERROR_ERROR_DOMAIN_RUNTIME,
691 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
692 "%s: invalid pool - missing last used list.",
693 function );
694
695 return( -1 );
696 }
697 if( handle == NULL )
698 {
699 libcerror_error_set(
700 error,
701 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
702 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
703 "%s: invalid handle.",
704 function );
705
706 return( -1 );
707 }
708 /* Check if there is room in the pool for another open handle
709 */
710 if( ( internal_pool->maximum_number_of_open_handles != LIBBFIO_POOL_UNLIMITED_NUMBER_OF_OPEN_HANDLES )
711 && ( ( internal_pool->number_of_open_handles + 1 ) >= internal_pool->maximum_number_of_open_handles ) )
712 {
713 if( libcdata_list_get_last_element(
714 internal_pool->last_used_list,
715 &last_used_list_element,
716 error ) != 1 )
717 {
718 libcerror_error_set(
719 error,
720 LIBCERROR_ERROR_DOMAIN_RUNTIME,
721 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
722 "%s: unable to retrieve last list element from last used list.",
723 function );
724
725 goto on_error;
726 }
727 }
728 if( last_used_list_element == NULL )
729 {
730 if( libcdata_list_element_initialize(
731 &safe_last_used_list_element,
732 error ) != 1 )
733 {
734 libcerror_error_set(
735 error,
736 LIBCERROR_ERROR_DOMAIN_RUNTIME,
737 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
738 "%s: unable to create last used list element.",
739 function );
740
741 goto on_error;
742 }
743 internal_pool->number_of_open_handles++;
744
745 last_used_list_element = safe_last_used_list_element;
746 }
747 else
748 {
749 /* The last used list element is reused to contain the new last used entry
750 */
751 if( libcdata_list_element_get_value(
752 last_used_list_element,
753 (intptr_t **) &internal_handle,
754 error ) != 1 )
755 {
756 libcerror_error_set(
757 error,
758 LIBCERROR_ERROR_DOMAIN_RUNTIME,
759 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
760 "%s: unable to retrieve value from last used list element.",
761 function );
762
763 goto on_error;
764 }
765 if( libcdata_list_remove_element(
766 internal_pool->last_used_list,
767 last_used_list_element,
768 error ) != 1 )
769 {
770 libcerror_error_set(
771 error,
772 LIBCERROR_ERROR_DOMAIN_RUNTIME,
773 LIBCERROR_RUNTIME_ERROR_REMOVE_FAILED,
774 "%s: unable to remove last list element from last used list.",
775 function );
776
777 goto on_error;
778 }
779 safe_last_used_list_element = last_used_list_element;
780
781 internal_handle->pool_last_used_list_element = NULL;
782
783 if( internal_handle != NULL )
784 {
785 if( libbfio_handle_close(
786 (libbfio_handle_t *) internal_handle,
787 error ) != 0 )
788 {
789 libcerror_error_set(
790 error,
791 LIBCERROR_ERROR_DOMAIN_IO,
792 LIBCERROR_IO_ERROR_CLOSE_FAILED,
793 "%s: unable to close handle.",
794 function );
795
796 goto on_error;
797 }
798 /* Make sure the truncate flag is removed from the handle
799 */
800 internal_handle->access_flags &= ~( LIBBFIO_ACCESS_FLAG_TRUNCATE );
801 }
802 }
803 internal_handle = (libbfio_internal_handle_t *) handle;
804
805 if( libcdata_list_element_set_value(
806 last_used_list_element,
807 (intptr_t *) handle,
808 error ) != 1 )
809 {
810 libcerror_error_set(
811 error,
812 LIBCERROR_ERROR_DOMAIN_RUNTIME,
813 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
814 "%s: unable to set value in last used list element.",
815 function );
816
817 goto on_error;
818 }
819 if( libcdata_list_prepend_element(
820 internal_pool->last_used_list,
821 last_used_list_element,
822 error ) != 1 )
823 {
824 libcerror_error_set(
825 error,
826 LIBCERROR_ERROR_DOMAIN_RUNTIME,
827 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
828 "%s: unable to prepend last used list element to list.",
829 function );
830
831 goto on_error;
832 }
833 internal_handle->pool_last_used_list_element = last_used_list_element;
834
835 return( 1 );
836
837 on_error:
838 if( safe_last_used_list_element != NULL )
839 {
840 libcdata_list_element_free(
841 &safe_last_used_list_element,
842 NULL,
843 NULL );
844 }
845 return( -1 );
846 }
847
848 /* Moves the handle to the front of the last used list
849 * Returns 1 if successful or -1 on error
850 */
851 int libbfio_internal_pool_move_handle_to_front_of_last_used_list(
852 libbfio_internal_pool_t *internal_pool,
853 libbfio_handle_t *handle,
854 libcerror_error_t **error )
855 {
856 libbfio_internal_handle_t *internal_handle = NULL;
857 libcdata_list_element_t *first_list_element = NULL;
858 libcdata_list_element_t *last_used_list_element = NULL;
859 static char *function = "libbfio_internal_pool_move_handle_to_front_of_last_used_list";
860
861 if( internal_pool == NULL )
862 {
863 libcerror_error_set(
864 error,
865 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
866 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
867 "%s: invalid pool.",
868 function );
869
870 return( -1 );
871 }
872 if( internal_pool->last_used_list == NULL )
873 {
874 libcerror_error_set(
875 error,
876 LIBCERROR_ERROR_DOMAIN_RUNTIME,
877 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
878 "%s: invalid pool - missing last used list.",
879 function );
880
881 return( -1 );
882 }
883 if( handle == NULL )
884 {
885 libcerror_error_set(
886 error,
887 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
888 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
889 "%s: invalid handle.",
890 function );
891
892 return( -1 );
893 }
894 internal_handle = (libbfio_internal_handle_t *) handle;
895
896 if( libcdata_list_get_first_element(
897 internal_pool->last_used_list,
898 &first_list_element,
899 error ) != 1 )
900 {
901 libcerror_error_set(
902 error,
903 LIBCERROR_ERROR_DOMAIN_RUNTIME,
904 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
905 "%s: unable to retrieve first list element from last used list.",
906 function );
907
908 goto on_error;
909 }
910 if( internal_handle->pool_last_used_list_element == NULL )
911 {
912 libcerror_error_set(
913 error,
914 LIBCERROR_ERROR_DOMAIN_RUNTIME,
915 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
916 "%s: missing last used list element.",
917 function );
918
919 goto on_error;
920 }
921 if( internal_handle->pool_last_used_list_element != first_list_element )
922 {
923 if( libcdata_list_remove_element(
924 internal_pool->last_used_list,
925 last_used_list_element,
926 error ) != 1 )
927 {
928 libcerror_error_set(
929 error,
930 LIBCERROR_ERROR_DOMAIN_RUNTIME,
931 LIBCERROR_RUNTIME_ERROR_REMOVE_FAILED,
932 "%s: unable to remove last used list element from list.",
933 function );
934
935 goto on_error;
936 }
937 internal_handle->pool_last_used_list_element = NULL;
938
939 if( libcdata_list_prepend_element(
940 internal_pool->last_used_list,
941 last_used_list_element,
942 error ) != 1 )
943 {
944 libcerror_error_set(
945 error,
946 LIBCERROR_ERROR_DOMAIN_RUNTIME,
947 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
948 "%s: unable to prepend last used list element to list.",
949 function );
950
951 goto on_error;
952 }
953 internal_handle->pool_last_used_list_element = last_used_list_element;
954 }
955 return( 1 );
956
957 on_error:
958 if( last_used_list_element != NULL )
959 {
960 libcdata_list_element_free(
961 &last_used_list_element,
962 NULL,
963 NULL );
964 }
965 return( -1 );
966 }
967
968 /* Removes a handle from the last used list
969 * Returns 1 if successful or -1 on error
970 */
971 int libbfio_internal_pool_remove_handle_from_last_used_list(
972 libbfio_internal_pool_t *internal_pool,
973 const libbfio_handle_t *handle,
974 libcerror_error_t **error )
975 {
976 libbfio_handle_t *last_used_handle = NULL;
977 libcdata_list_element_t *last_used_list_element = NULL;
978 static char *function = "libbfio_internal_pool_remove_handle_from_last_used_list";
979
980 if( internal_pool == NULL )
981 {
982 libcerror_error_set(
983 error,
984 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
985 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
986 "%s: invalid pool.",
987 function );
988
989 return( -1 );
990 }
991 if( handle == NULL )
992 {
993 libcerror_error_set(
994 error,
995 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
996 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
997 "%s: invalid handle.",
998 function );
999
1000 return( -1 );
1001 }
1002 if( libcdata_list_get_first_element(
1003 internal_pool->last_used_list,
1004 &last_used_list_element,
1005 error ) != 1 )
1006 {
1007 libcerror_error_set(
1008 error,
1009 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1010 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1011 "%s: unable to retrieve first list element from last used list.",
1012 function );
1013
1014 return( -1 );
1015 }
1016 while( last_used_list_element != NULL )
1017 {
1018 if( libcdata_list_element_get_value(
1019 last_used_list_element,
1020 (intptr_t **) &last_used_handle,
1021 error ) != 1 )
1022 {
1023 libcerror_error_set(
1024 error,
1025 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1026 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1027 "%s: unable to retrieve last used handle.",
1028 function );
1029
1030 return( -1 );
1031 }
1032 if( last_used_handle == handle )
1033 {
1034 break;
1035 }
1036 if( libcdata_list_element_get_next_element(
1037 last_used_list_element,
1038 &last_used_list_element,
1039 error ) != 1 )
1040 {
1041 libcerror_error_set(
1042 error,
1043 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1044 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1045 "%s: unable to retrieve next last used list element.",
1046 function );
1047
1048 return( -1 );
1049 }
1050 }
1051 if( last_used_list_element != NULL )
1052 {
1053 if( libcdata_list_remove_element(
1054 internal_pool->last_used_list,
1055 last_used_list_element,
1056 error ) != 1 )
1057 {
1058 libcerror_error_set(
1059 error,
1060 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1061 LIBCERROR_RUNTIME_ERROR_REMOVE_FAILED,
1062 "%s: unable to remove last list element from last used list.",
1063 function );
1064
1065 return( -1 );
1066 }
1067 if( libcdata_list_element_free(
1068 &last_used_list_element,
1069 NULL,
1070 error ) != 1 )
1071 {
1072 libcerror_error_set(
1073 error,
1074 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1075 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
1076 "%s: unable to free last used list element.",
1077 function );
1078
1079 return( -1 );
1080 }
1081 }
1082 return( 1 );
1083 }
1084
1085 /* Retrieves the number of handles in the pool
1086 * Returns 1 if successful or -1 on error
1087 */
1088 int libbfio_pool_get_number_of_handles(
1089 libbfio_pool_t *pool,
1090 int *number_of_handles,
1091 libcerror_error_t **error )
1092 {
1093 libbfio_internal_pool_t *internal_pool = NULL;
1094 static char *function = "libbfio_pool_get_number_of_handles";
1095 int result = 1;
1096
1097 if( pool == NULL )
1098 {
1099 libcerror_error_set(
1100 error,
1101 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1102 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1103 "%s: invalid pool.",
1104 function );
1105
1106 return( -1 );
1107 }
1108 internal_pool = (libbfio_internal_pool_t *) pool;
1109
1110 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
1111 if( libcthreads_read_write_lock_grab_for_read(
1112 internal_pool->read_write_lock,
1113 error ) != 1 )
1114 {
1115 libcerror_error_set(
1116 error,
1117 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1118 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1119 "%s: unable to grab read/write lock for reading.",
1120 function );
1121
1122 return( -1 );
1123 }
1124 #endif
1125 if( libcdata_array_get_number_of_entries(
1126 internal_pool->handles_array,
1127 number_of_handles,
1128 error ) != 1 )
1129 {
1130 libcerror_error_set(
1131 error,
1132 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1133 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1134 "%s: unable to retrieve number of handles.",
1135 function );
1136
1137 result = -1;
1138 }
1139 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
1140 if( libcthreads_read_write_lock_release_for_read(
1141 internal_pool->read_write_lock,
1142 error ) != 1 )
1143 {
1144 libcerror_error_set(
1145 error,
1146 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1147 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1148 "%s: unable to release read/write lock for reading.",
1149 function );
1150
1151 return( -1 );
1152 }
1153 #endif
1154 return( result );
1155 }
1156
1157 /* Retrieves a specific handle from the pool
1158 * Returns 1 if successful or -1 on error
1159 */
1160 int libbfio_pool_get_handle(
1161 libbfio_pool_t *pool,
1162 int entry,
1163 libbfio_handle_t **handle,
1164 libcerror_error_t **error )
1165 {
1166 libbfio_internal_pool_t *internal_pool = NULL;
1167 static char *function = "libbfio_pool_get_handle";
1168 int result = 1;
1169
1170 if( pool == NULL )
1171 {
1172 libcerror_error_set(
1173 error,
1174 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1175 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1176 "%s: invalid pool.",
1177 function );
1178
1179 return( -1 );
1180 }
1181 internal_pool = (libbfio_internal_pool_t *) pool;
1182
1183 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
1184 if( libcthreads_read_write_lock_grab_for_read(
1185 internal_pool->read_write_lock,
1186 error ) != 1 )
1187 {
1188 libcerror_error_set(
1189 error,
1190 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1191 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1192 "%s: unable to grab read/write lock for reading.",
1193 function );
1194
1195 return( -1 );
1196 }
1197 #endif
1198 if( libcdata_array_get_entry_by_index(
1199 internal_pool->handles_array,
1200 entry,
1201 (intptr_t **) handle,
1202 error ) != 1 )
1203 {
1204 libcerror_error_set(
1205 error,
1206 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1207 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1208 "%s: unable to retrieve handle: %d.",
1209 function,
1210 entry );
1211
1212 result = -1;
1213 }
1214 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
1215 if( libcthreads_read_write_lock_release_for_read(
1216 internal_pool->read_write_lock,
1217 error ) != 1 )
1218 {
1219 libcerror_error_set(
1220 error,
1221 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1222 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1223 "%s: unable to release read/write lock for reading.",
1224 function );
1225
1226 return( -1 );
1227 }
1228 #endif
1229 return( result );
1230 }
1231
1232 /* Appends a handle to the pool
1233 * Sets the entry
1234 * Returns 1 if successful or -1 on error
1235 */
1236 int libbfio_pool_append_handle(
1237 libbfio_pool_t *pool,
1238 int *entry,
1239 libbfio_handle_t *handle,
1240 int access_flags,
1241 libcerror_error_t **error )
1242 {
1243 libbfio_internal_pool_t *internal_pool = NULL;
1244 static char *function = "libbfio_pool_append_handle";
1245 int is_open = 0;
1246 int number_of_handles = 0;
1247 int result = 1;
1248 int safe_entry = -1;
1249
1250 if( pool == NULL )
1251 {
1252 libcerror_error_set(
1253 error,
1254 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1255 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1256 "%s: invalid pool.",
1257 function );
1258
1259 return( -1 );
1260 }
1261 internal_pool = (libbfio_internal_pool_t *) pool;
1262
1263 if( internal_pool->last_used_list == NULL )
1264 {
1265 libcerror_error_set(
1266 error,
1267 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1268 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1269 "%s: invalid pool - missing last used list.",
1270 function );
1271
1272 return( -1 );
1273 }
1274 if( entry == NULL )
1275 {
1276 libcerror_error_set(
1277 error,
1278 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1279 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1280 "%s: invalid entry.",
1281 function );
1282
1283 return( -1 );
1284 }
1285 /* Check if the handle is open
1286 */
1287 is_open = libbfio_handle_is_open(
1288 handle,
1289 error );
1290
1291 if( is_open == -1 )
1292 {
1293 libcerror_error_set(
1294 error,
1295 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1296 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1297 "%s: unable to determine if handle is open.",
1298 function );
1299
1300 return( -1 );
1301 }
1302 else if( is_open == 0 )
1303 {
1304 /* Set the access flags is the handle is not open
1305 */
1306 if( libbfio_handle_set_access_flags(
1307 handle,
1308 access_flags,
1309 error ) != 1 )
1310 {
1311 libcerror_error_set(
1312 error,
1313 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1314 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1315 "%s: unable to set access flags.",
1316 function );
1317
1318 return( -1 );
1319 }
1320 }
1321 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
1322 if( libcthreads_read_write_lock_grab_for_write(
1323 internal_pool->read_write_lock,
1324 error ) != 1 )
1325 {
1326 libcerror_error_set(
1327 error,
1328 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1329 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1330 "%s: unable to grab read/write lock for writing.",
1331 function );
1332
1333 return( -1 );
1334 }
1335 #endif
1336 if( libcdata_array_get_number_of_entries(
1337 internal_pool->handles_array,
1338 &number_of_handles,
1339 error ) != 1 )
1340 {
1341 libcerror_error_set(
1342 error,
1343 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1344 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1345 "%s: unable to retrieve number of handles.",
1346 function );
1347
1348 result = -1;
1349 }
1350 else if( ( internal_pool->number_of_used_handles + 1 ) >= number_of_handles )
1351 {
1352 if( libcdata_array_append_entry(
1353 internal_pool->handles_array,
1354 &safe_entry,
1355 (intptr_t *) handle,
1356 error ) != 1 )
1357 {
1358 libcerror_error_set(
1359 error,
1360 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1361 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
1362 "%s: unable to append handle.",
1363 function );
1364
1365 result = -1;
1366 }
1367 }
1368 else
1369 {
1370 safe_entry = internal_pool->number_of_used_handles;
1371
1372 if( libcdata_array_set_entry_by_index(
1373 internal_pool->handles_array,
1374 safe_entry,
1375 (intptr_t *) handle,
1376 error ) != 1 )
1377 {
1378 libcerror_error_set(
1379 error,
1380 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1381 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1382 "%s: unable to set handle: %d.",
1383 function,
1384 safe_entry );
1385
1386 result = -1;
1387 }
1388 }
1389 if( result == 1 )
1390 {
1391 internal_pool->number_of_used_handles += 1;
1392
1393 if( is_open != 0 )
1394 {
1395 if( internal_pool->maximum_number_of_open_handles != LIBBFIO_POOL_UNLIMITED_NUMBER_OF_OPEN_HANDLES )
1396 {
1397 if( libbfio_internal_pool_append_handle_to_last_used_list(
1398 internal_pool,
1399 handle,
1400 error ) != 1 )
1401 {
1402 libcerror_error_set(
1403 error,
1404 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1405 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
1406 "%s: unable to append handle to last used list.",
1407 function );
1408
1409 result = -1;
1410 }
1411 }
1412 }
1413 }
1414 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
1415 if( libcthreads_read_write_lock_release_for_write(
1416 internal_pool->read_write_lock,
1417 error ) != 1 )
1418 {
1419 libcerror_error_set(
1420 error,
1421 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1422 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1423 "%s: unable to release read/write lock for writing.",
1424 function );
1425
1426 goto on_error;
1427 }
1428 #endif
1429 if( result != 1 )
1430 {
1431 goto on_error;
1432 }
1433 *entry = safe_entry;
1434
1435 return( 1 );
1436
1437 on_error:
1438 if( safe_entry >= 0 )
1439 {
1440 libcdata_array_set_entry_by_index(
1441 internal_pool->handles_array,
1442 safe_entry,
1443 NULL,
1444 NULL );
1445
1446 internal_pool->number_of_used_handles -= 1;
1447 }
1448 return( -1 );
1449 }
1450
1451 /* Sets a specific handle in the pool
1452 * Returns 1 if successful or -1 on error
1453 */
1454 int libbfio_pool_set_handle(
1455 libbfio_pool_t *pool,
1456 int entry,
1457 libbfio_handle_t *handle,
1458 int access_flags,
1459 libcerror_error_t **error )
1460 {
1461 libbfio_internal_handle_t *backup_handle = NULL;
1462 libbfio_internal_pool_t *internal_pool = NULL;
1463 static char *function = "libbfio_pool_set_handle";
1464 int is_open = 0;
1465 int result = 1;
1466
1467 if( pool == NULL )
1468 {
1469 libcerror_error_set(
1470 error,
1471 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1472 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1473 "%s: invalid pool.",
1474 function );
1475
1476 return( -1 );
1477 }
1478 internal_pool = (libbfio_internal_pool_t *) pool;
1479
1480 if( internal_pool->last_used_list == NULL )
1481 {
1482 libcerror_error_set(
1483 error,
1484 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1485 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1486 "%s: invalid pool - missing last used list.",
1487 function );
1488
1489 return( -1 );
1490 }
1491 /* Check if the handle is open
1492 */
1493 is_open = libbfio_handle_is_open(
1494 handle,
1495 error );
1496
1497 if( is_open == -1 )
1498 {
1499 libcerror_error_set(
1500 error,
1501 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1502 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1503 "%s: unable to determine if handle is open.",
1504 function );
1505
1506 return( -1 );
1507 }
1508 else if( is_open == 0 )
1509 {
1510 /* Set the access flags is the handle is not open
1511 */
1512 if( libbfio_handle_set_access_flags(
1513 handle,
1514 access_flags,
1515 error ) != 1 )
1516 {
1517 libcerror_error_set(
1518 error,
1519 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1520 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1521 "%s: unable to set access flags.",
1522 function );
1523
1524 return( -1 );
1525 }
1526 }
1527 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
1528 if( libcthreads_read_write_lock_grab_for_write(
1529 internal_pool->read_write_lock,
1530 error ) != 1 )
1531 {
1532 libcerror_error_set(
1533 error,
1534 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1535 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1536 "%s: unable to grab read/write lock for writing.",
1537 function );
1538
1539 return( -1 );
1540 }
1541 #endif
1542 if( libcdata_array_get_entry_by_index(
1543 internal_pool->handles_array,
1544 entry,
1545 (intptr_t **) &backup_handle,
1546 error ) != 1 )
1547 {
1548 libcerror_error_set(
1549 error,
1550 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1551 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1552 "%s: unable to retrieve handle: %d.",
1553 function,
1554 entry );
1555
1556 result = -1;
1557 }
1558 /* TODO allow to re set handles, make sure all pool references are removed */
1559 else if( backup_handle != NULL )
1560 {
1561 libcerror_error_set(
1562 error,
1563 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1564 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
1565 "%s: invalid handle entry: %d value already set.",
1566 function,
1567 entry );
1568
1569 result = -1;
1570 }
1571 else if( libcdata_array_set_entry_by_index(
1572 internal_pool->handles_array,
1573 entry,
1574 (intptr_t *) handle,
1575 error ) != 1 )
1576 {
1577 libcerror_error_set(
1578 error,
1579 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1580 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1581 "%s: unable to set handle: %d.",
1582 function,
1583 entry );
1584
1585 result = -1;
1586 }
1587 if( result == 1 )
1588 {
1589 if( is_open != 0 )
1590 {
1591 if( internal_pool->maximum_number_of_open_handles != LIBBFIO_POOL_UNLIMITED_NUMBER_OF_OPEN_HANDLES )
1592 {
1593 if( libbfio_internal_pool_append_handle_to_last_used_list(
1594 internal_pool,
1595 handle,
1596 error ) != 1 )
1597 {
1598 libcerror_error_set(
1599 error,
1600 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1601 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
1602 "%s: unable to append handle to last used list.",
1603 function );
1604
1605 result = -1;
1606 }
1607 }
1608 }
1609 if( entry == internal_pool->current_entry )
1610 {
1611 internal_pool->current_entry = -1;
1612 internal_pool->current_handle = NULL;
1613 }
1614 }
1615 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
1616 if( libcthreads_read_write_lock_release_for_write(
1617 internal_pool->read_write_lock,
1618 error ) != 1 )
1619 {
1620 libcerror_error_set(
1621 error,
1622 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1623 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1624 "%s: unable to release read/write lock for writing.",
1625 function );
1626
1627 goto on_error;
1628 }
1629 #endif
1630 if( result != 1 )
1631 {
1632 goto on_error;
1633 }
1634 return( result );
1635
1636 on_error:
1637 libcdata_array_set_entry_by_index(
1638 internal_pool->handles_array,
1639 entry,
1640 (intptr_t *) backup_handle,
1641 NULL );
1642
1643 return( -1 );
1644 }
1645
1646 /* Removes a specific handle from the pool
1647 * Returns 1 if successful or -1 on error
1648 */
1649 int libbfio_pool_remove_handle(
1650 libbfio_pool_t *pool,
1651 int entry,
1652 libbfio_handle_t **handle,
1653 libcerror_error_t **error )
1654 {
1655 libbfio_handle_t *backup_handle = NULL;
1656 libbfio_internal_pool_t *internal_pool = NULL;
1657 static char *function = "libbfio_pool_remove_handle";
1658 int result = 1;
1659
1660 if( pool == NULL )
1661 {
1662 libcerror_error_set(
1663 error,
1664 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1665 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1666 "%s: invalid pool.",
1667 function );
1668
1669 return( -1 );
1670 }
1671 internal_pool = (libbfio_internal_pool_t *) pool;
1672
1673 if( internal_pool->last_used_list == NULL )
1674 {
1675 libcerror_error_set(
1676 error,
1677 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1678 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1679 "%s: invalid pool - missing last used list.",
1680 function );
1681
1682 return( -1 );
1683 }
1684 if( handle == NULL )
1685 {
1686 libcerror_error_set(
1687 error,
1688 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1689 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1690 "%s: invalid handle.",
1691 function );
1692
1693 return( -1 );
1694 }
1695 *handle = NULL;
1696
1697 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
1698 if( libcthreads_read_write_lock_grab_for_write(
1699 internal_pool->read_write_lock,
1700 error ) != 1 )
1701 {
1702 libcerror_error_set(
1703 error,
1704 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1705 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1706 "%s: unable to grab read/write lock for writing.",
1707 function );
1708
1709 return( -1 );
1710 }
1711 #endif
1712 if( libcdata_array_get_entry_by_index(
1713 internal_pool->handles_array,
1714 entry,
1715 (intptr_t **) &backup_handle,
1716 error ) != 1 )
1717 {
1718 libcerror_error_set(
1719 error,
1720 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1721 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1722 "%s: unable to retrieve handle: %d.",
1723 function,
1724 entry );
1725
1726 result = -1;
1727 }
1728 if( result == 1 )
1729 {
1730 if( libbfio_internal_pool_remove_handle_from_last_used_list(
1731 internal_pool,
1732 backup_handle,
1733 error ) != 1 )
1734 {
1735 libcerror_error_set(
1736 error,
1737 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1738 LIBCERROR_RUNTIME_ERROR_REMOVE_FAILED,
1739 "%s: unable to remove handle: %d from last used list.",
1740 function,
1741 entry );
1742
1743 result = -1;
1744 }
1745 }
1746 if( result == 1 )
1747 {
1748 if( entry == internal_pool->current_entry )
1749 {
1750 internal_pool->current_entry = -1;
1751 internal_pool->current_handle = NULL;
1752 }
1753 if( libcdata_array_set_entry_by_index(
1754 internal_pool->handles_array,
1755 entry,
1756 NULL,
1757 error ) != 1 )
1758 {
1759 libcerror_error_set(
1760 error,
1761 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1762 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1763 "%s: unable to set handle: %d.",
1764 function,
1765 entry );
1766
1767 libbfio_internal_pool_append_handle_to_last_used_list(
1768 internal_pool,
1769 backup_handle,
1770 NULL );
1771
1772 result = -1;
1773 }
1774 }
1775 if( result == 1 )
1776 {
1777 internal_pool->number_of_used_handles -= 1;
1778 }
1779 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
1780 if( libcthreads_read_write_lock_release_for_write(
1781 internal_pool->read_write_lock,
1782 error ) != 1 )
1783 {
1784 libcerror_error_set(
1785 error,
1786 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1787 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1788 "%s: unable to release read/write lock for writing.",
1789 function );
1790
1791 goto on_error;
1792 }
1793 #endif
1794 if( result == 1 )
1795 {
1796 *handle = backup_handle;
1797 }
1798 return( result );
1799
1800 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
1801 on_error:
1802 if( result == 1 )
1803 {
1804 libcdata_array_set_entry_by_index(
1805 internal_pool->handles_array,
1806 entry,
1807 (intptr_t *) backup_handle,
1808 NULL );
1809
1810 libbfio_internal_pool_append_handle_to_last_used_list(
1811 internal_pool,
1812 backup_handle,
1813 NULL );
1814
1815 internal_pool->number_of_used_handles += 1;
1816 }
1817 return( -1 );
1818 #endif
1819 }
1820
1821 /* Retrieves the maximum number of open handles in the pool
1822 * Returns 1 if successful or -1 on error
1823 */
1824 int libbfio_pool_get_maximum_number_of_open_handles(
1825 libbfio_pool_t *pool,
1826 int *maximum_number_of_open_handles,
1827 libcerror_error_t **error )
1828 {
1829 libbfio_internal_pool_t *internal_pool = NULL;
1830 static char *function = "libbfio_pool_get_maximum_number_of_open_handles";
1831
1832 if( pool == NULL )
1833 {
1834 libcerror_error_set(
1835 error,
1836 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1837 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1838 "%s: invalid pool.",
1839 function );
1840
1841 return( -1 );
1842 }
1843 internal_pool = (libbfio_internal_pool_t *) pool;
1844
1845 if( maximum_number_of_open_handles == NULL )
1846 {
1847 libcerror_error_set(
1848 error,
1849 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1850 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1851 "%s: invalid maximum number of open handles.",
1852 function );
1853
1854 return( -1 );
1855 }
1856 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
1857 if( libcthreads_read_write_lock_grab_for_read(
1858 internal_pool->read_write_lock,
1859 error ) != 1 )
1860 {
1861 libcerror_error_set(
1862 error,
1863 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1864 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1865 "%s: unable to grab read/write lock for reading.",
1866 function );
1867
1868 return( -1 );
1869 }
1870 #endif
1871 *maximum_number_of_open_handles = internal_pool->maximum_number_of_open_handles;
1872
1873 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
1874 if( libcthreads_read_write_lock_release_for_read(
1875 internal_pool->read_write_lock,
1876 error ) != 1 )
1877 {
1878 libcerror_error_set(
1879 error,
1880 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1881 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1882 "%s: unable to release read/write lock for reading.",
1883 function );
1884
1885 return( -1 );
1886 }
1887 #endif
1888 return( 1 );
1889 }
1890
1891 /* Sets the maximum number of open handles in the pool
1892 * Returns 1 if successful or -1 on error
1893 */
1894 int libbfio_pool_set_maximum_number_of_open_handles(
1895 libbfio_pool_t *pool,
1896 int maximum_number_of_open_handles,
1897 libcerror_error_t **error )
1898 {
1899 libbfio_internal_handle_t *internal_handle = NULL;
1900 libbfio_internal_pool_t *internal_pool = NULL;
1901 libcdata_list_element_t *last_used_list_element = NULL;
1902 libcdata_list_element_t *safe_last_used_list_element = NULL;
1903 static char *function = "libbfio_pool_set_maximum_number_of_open_handles";
1904
1905 if( pool == NULL )
1906 {
1907 libcerror_error_set(
1908 error,
1909 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1910 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1911 "%s: invalid pool.",
1912 function );
1913
1914 return( -1 );
1915 }
1916 internal_pool = (libbfio_internal_pool_t *) pool;
1917
1918 if( maximum_number_of_open_handles < 0 )
1919 {
1920 libcerror_error_set(
1921 error,
1922 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1923 LIBCERROR_ARGUMENT_ERROR_VALUE_LESS_THAN_ZERO,
1924 "%s: invalid maximum number of open handles value less than zero.",
1925 function );
1926
1927 return( -1 );
1928 }
1929 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
1930 if( libcthreads_read_write_lock_grab_for_write(
1931 internal_pool->read_write_lock,
1932 error ) != 1 )
1933 {
1934 libcerror_error_set(
1935 error,
1936 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1937 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1938 "%s: unable to grab read/write lock for writing.",
1939 function );
1940
1941 return( -1 );
1942 }
1943 #endif
1944 internal_pool->maximum_number_of_open_handles = maximum_number_of_open_handles;
1945
1946 while( ( internal_pool->maximum_number_of_open_handles != LIBBFIO_POOL_UNLIMITED_NUMBER_OF_OPEN_HANDLES )
1947 && ( internal_pool->number_of_open_handles > internal_pool->maximum_number_of_open_handles ) )
1948 {
1949 if( libcdata_list_get_last_element(
1950 internal_pool->last_used_list,
1951 &last_used_list_element,
1952 error ) != 1 )
1953 {
1954 libcerror_error_set(
1955 error,
1956 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1957 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1958 "%s: unable to retrieve last list element from last used list.",
1959 function );
1960
1961 goto on_error;
1962 }
1963 if( libcdata_list_element_get_value(
1964 last_used_list_element,
1965 (intptr_t **) &internal_handle,
1966 error ) != 1 )
1967 {
1968 libcerror_error_set(
1969 error,
1970 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1971 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
1972 "%s: unable to retrieve value from last used list element.",
1973 function );
1974
1975 goto on_error;
1976 }
1977 if( internal_handle == NULL )
1978 {
1979 libcerror_error_set(
1980 error,
1981 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1982 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
1983 "%s: missing last used list element value.",
1984 function );
1985
1986 goto on_error;
1987 }
1988 if( libcdata_list_remove_element(
1989 internal_pool->last_used_list,
1990 last_used_list_element,
1991 error ) != 1 )
1992 {
1993 libcerror_error_set(
1994 error,
1995 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1996 LIBCERROR_RUNTIME_ERROR_REMOVE_FAILED,
1997 "%s: unable to remove last used list element from list.",
1998 function );
1999
2000 goto on_error;
2001 }
2002 safe_last_used_list_element = last_used_list_element;
2003
2004 internal_handle->pool_last_used_list_element = NULL;
2005
2006 if( libbfio_handle_close(
2007 (libbfio_handle_t *) internal_handle,
2008 error ) != 0 )
2009 {
2010 libcerror_error_set(
2011 error,
2012 LIBCERROR_ERROR_DOMAIN_IO,
2013 LIBCERROR_IO_ERROR_CLOSE_FAILED,
2014 "%s: unable to close handle.",
2015 function );
2016
2017 goto on_error;
2018 }
2019 internal_pool->number_of_open_handles--;
2020
2021 /* Make sure the truncate flag is removed from the handle
2022 */
2023 internal_handle->access_flags &= ~( LIBBFIO_ACCESS_FLAG_TRUNCATE );
2024
2025 if( libcdata_list_element_free(
2026 &safe_last_used_list_element,
2027 NULL,
2028 error ) != 1 )
2029 {
2030 libcerror_error_set(
2031 error,
2032 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2033 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
2034 "%s: unable to free last used list element.",
2035 function );
2036
2037 goto on_error;
2038 }
2039 }
2040 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
2041 if( libcthreads_read_write_lock_release_for_write(
2042 internal_pool->read_write_lock,
2043 error ) != 1 )
2044 {
2045 libcerror_error_set(
2046 error,
2047 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2048 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2049 "%s: unable to release read/write lock for writing.",
2050 function );
2051
2052 return( -1 );
2053 }
2054 #endif
2055 return( 1 );
2056
2057 on_error:
2058 if( safe_last_used_list_element != NULL )
2059 {
2060 libcdata_list_element_free(
2061 &safe_last_used_list_element,
2062 NULL,
2063 NULL );
2064 }
2065 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
2066 libcthreads_read_write_lock_release_for_write(
2067 internal_pool->read_write_lock,
2068 NULL );
2069 #endif
2070 return( -1 );
2071 }
2072
2073 /* Opens a handle in the pool
2074 * Returns 1 if successful or -1 on error
2075 */
2076 int libbfio_internal_pool_open(
2077 libbfio_internal_pool_t *internal_pool,
2078 int entry,
2079 int access_flags,
2080 libcerror_error_t **error )
2081 {
2082 libbfio_handle_t *handle = NULL;
2083 static char *function = "libbfio_internal_pool_open";
2084 int is_open = 0;
2085
2086 if( internal_pool == NULL )
2087 {
2088 libcerror_error_set(
2089 error,
2090 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2091 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2092 "%s: invalid pool.",
2093 function );
2094
2095 return( -1 );
2096 }
2097 if( libcdata_array_get_entry_by_index(
2098 internal_pool->handles_array,
2099 entry,
2100 (intptr_t **) &handle,
2101 error ) != 1 )
2102 {
2103 libcerror_error_set(
2104 error,
2105 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2106 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2107 "%s: unable to retrieve handle: %d.",
2108 function,
2109 entry );
2110
2111 return( -1 );
2112 }
2113 /* Make sure the handle is not already open
2114 */
2115 is_open = libbfio_handle_is_open(
2116 handle,
2117 error );
2118
2119 if( is_open == -1 )
2120 {
2121 libcerror_error_set(
2122 error,
2123 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2124 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2125 "%s: unable to determine if entry: %d is open.",
2126 function,
2127 entry );
2128
2129 return( -1 );
2130 }
2131 else if( is_open == 1 )
2132 {
2133 libcerror_error_set(
2134 error,
2135 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2136 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
2137 "%s: entry: %d is already open.",
2138 function,
2139 entry );
2140
2141 return( -1 );
2142 }
2143 if( libbfio_internal_pool_open_handle(
2144 internal_pool,
2145 handle,
2146 access_flags,
2147 error ) != 1 )
2148 {
2149 libcerror_error_set(
2150 error,
2151 LIBCERROR_ERROR_DOMAIN_IO,
2152 LIBCERROR_IO_ERROR_OPEN_FAILED,
2153 "%s: unable to open entry: %d.",
2154 function,
2155 entry );
2156
2157 return( -1 );
2158 }
2159 internal_pool->current_entry = entry;
2160 internal_pool->current_handle = handle;
2161
2162 return( 1 );
2163 }
2164
2165 /* Opens a handle in the pool
2166 * Returns 1 if successful or -1 on error
2167 */
2168 int libbfio_pool_open(
2169 libbfio_pool_t *pool,
2170 int entry,
2171 int access_flags,
2172 libcerror_error_t **error )
2173 {
2174 libbfio_internal_pool_t *internal_pool = NULL;
2175 static char *function = "libbfio_pool_open";
2176 int result = 1;
2177
2178 if( pool == NULL )
2179 {
2180 libcerror_error_set(
2181 error,
2182 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2183 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2184 "%s: invalid pool.",
2185 function );
2186
2187 return( -1 );
2188 }
2189 internal_pool = (libbfio_internal_pool_t *) pool;
2190
2191 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
2192 if( libcthreads_read_write_lock_grab_for_write(
2193 internal_pool->read_write_lock,
2194 error ) != 1 )
2195 {
2196 libcerror_error_set(
2197 error,
2198 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2199 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2200 "%s: unable to grab read/write lock for writing.",
2201 function );
2202
2203 return( -1 );
2204 }
2205 #endif
2206 if( libbfio_internal_pool_open(
2207 internal_pool,
2208 entry,
2209 access_flags,
2210 error ) != 1 )
2211 {
2212 libcerror_error_set(
2213 error,
2214 LIBCERROR_ERROR_DOMAIN_IO,
2215 LIBCERROR_IO_ERROR_OPEN_FAILED,
2216 "%s: unable to open entry: %d.",
2217 function,
2218 entry );
2219
2220 result = -1;
2221 }
2222 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
2223 if( libcthreads_read_write_lock_release_for_write(
2224 internal_pool->read_write_lock,
2225 error ) != 1 )
2226 {
2227 libcerror_error_set(
2228 error,
2229 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2230 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2231 "%s: unable to release read/write lock for writing.",
2232 function );
2233
2234 return( -1 );
2235 }
2236 #endif
2237 return( result );
2238 }
2239
2240 /* Reopens a handle in the pool
2241 * Returns 1 if successful or -1 on error
2242 */
2243 int libbfio_pool_reopen(
2244 libbfio_pool_t *pool,
2245 int entry,
2246 int access_flags,
2247 libcerror_error_t **error )
2248 {
2249 libbfio_handle_t *handle = NULL;
2250 libbfio_internal_pool_t *internal_pool = NULL;
2251 static char *function = "libbfio_pool_reopen";
2252
2253 if( pool == NULL )
2254 {
2255 libcerror_error_set(
2256 error,
2257 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2258 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2259 "%s: invalid pool.",
2260 function );
2261
2262 return( -1 );
2263 }
2264 internal_pool = (libbfio_internal_pool_t *) pool;
2265
2266 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
2267 if( libcthreads_read_write_lock_grab_for_write(
2268 internal_pool->read_write_lock,
2269 error ) != 1 )
2270 {
2271 libcerror_error_set(
2272 error,
2273 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2274 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2275 "%s: unable to grab read/write lock for writing.",
2276 function );
2277
2278 return( -1 );
2279 }
2280 #endif
2281 if( libcdata_array_get_entry_by_index(
2282 internal_pool->handles_array,
2283 entry,
2284 (intptr_t **) &handle,
2285 error ) != 1 )
2286 {
2287 libcerror_error_set(
2288 error,
2289 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2290 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2291 "%s: unable to retrieve handle: %d.",
2292 function,
2293 entry );
2294
2295 goto on_error;
2296 }
2297 if( libbfio_handle_reopen(
2298 handle,
2299 access_flags,
2300 error ) != 1 )
2301 {
2302 libcerror_error_set(
2303 error,
2304 LIBCERROR_ERROR_DOMAIN_IO,
2305 LIBCERROR_IO_ERROR_OPEN_FAILED,
2306 "%s: unable to reopen handle for entry: %d.",
2307 function,
2308 entry );
2309
2310 goto on_error;
2311 }
2312 internal_pool->current_entry = entry;
2313 internal_pool->current_handle = handle;
2314
2315 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
2316 if( libcthreads_read_write_lock_release_for_write(
2317 internal_pool->read_write_lock,
2318 error ) != 1 )
2319 {
2320 libcerror_error_set(
2321 error,
2322 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2323 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2324 "%s: unable to release read/write lock for writing.",
2325 function );
2326
2327 return( -1 );
2328 }
2329 #endif
2330 return( 1 );
2331
2332 on_error:
2333 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
2334 libcthreads_read_write_lock_release_for_write(
2335 internal_pool->read_write_lock,
2336 NULL );
2337 #endif
2338 return( -1 );
2339 }
2340
2341 /* Closes a handle in the pool
2342 * Returns 0 if successful or -1 on error
2343 */
2344 int libbfio_internal_pool_close(
2345 libbfio_internal_pool_t *internal_pool,
2346 int entry,
2347 libcerror_error_t **error )
2348 {
2349 libbfio_handle_t *handle = NULL;
2350 libbfio_internal_handle_t *internal_handle = NULL;
2351 libcdata_list_element_t *last_used_list_element = NULL;
2352 libcdata_list_element_t *safe_last_used_list_element = NULL;
2353 static char *function = "libbfio_internal_pool_close";
2354
2355 if( internal_pool == NULL )
2356 {
2357 libcerror_error_set(
2358 error,
2359 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2360 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2361 "%s: invalid pool.",
2362 function );
2363
2364 return( -1 );
2365 }
2366 if( libcdata_array_get_entry_by_index(
2367 internal_pool->handles_array,
2368 entry,
2369 (intptr_t **) &handle,
2370 error ) != 1 )
2371 {
2372 libcerror_error_set(
2373 error,
2374 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2375 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2376 "%s: unable to retrieve handle: %d.",
2377 function,
2378 entry );
2379
2380 goto on_error;
2381 }
2382 if( handle == NULL )
2383 {
2384 libcerror_error_set(
2385 error,
2386 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2387 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2388 "%s: invalid pool - missing handle for entry: %d.",
2389 function,
2390 entry );
2391
2392 goto on_error;
2393 }
2394 if( internal_pool->maximum_number_of_open_handles != LIBBFIO_POOL_UNLIMITED_NUMBER_OF_OPEN_HANDLES )
2395 {
2396 /* TODO move into get function */
2397 internal_handle = (libbfio_internal_handle_t *) handle;
2398
2399 last_used_list_element = internal_handle->pool_last_used_list_element;
2400
2401 if( libcdata_list_element_get_value(
2402 last_used_list_element,
2403 (intptr_t **) &internal_handle,
2404 error ) != 1 )
2405 {
2406 libcerror_error_set(
2407 error,
2408 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2409 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2410 "%s: unable to retrieve value from last used list element.",
2411 function );
2412
2413 goto on_error;
2414 }
2415 if( internal_handle == NULL )
2416 {
2417 libcerror_error_set(
2418 error,
2419 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2420 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
2421 "%s: missing last used list element value.",
2422 function );
2423
2424 goto on_error;
2425 }
2426 if( libcdata_list_remove_element(
2427 internal_pool->last_used_list,
2428 last_used_list_element,
2429 error ) != 1 )
2430 {
2431 libcerror_error_set(
2432 error,
2433 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2434 LIBCERROR_RUNTIME_ERROR_REMOVE_FAILED,
2435 "%s: unable to remove last used list element from list.",
2436 function );
2437
2438 goto on_error;
2439 }
2440 safe_last_used_list_element = last_used_list_element;
2441
2442 internal_handle->pool_last_used_list_element = NULL;
2443
2444 if( libcdata_list_element_free(
2445 &safe_last_used_list_element,
2446 NULL,
2447 error ) != 1 )
2448 {
2449 libcerror_error_set(
2450 error,
2451 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2452 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
2453 "%s: unable to free last used list element.",
2454 function );
2455
2456 goto on_error;
2457 }
2458 }
2459 if( libbfio_handle_close(
2460 handle,
2461 error ) != 0 )
2462 {
2463 libcerror_error_set(
2464 error,
2465 LIBCERROR_ERROR_DOMAIN_IO,
2466 LIBCERROR_IO_ERROR_CLOSE_FAILED,
2467 "%s: unable to close handle for entry: %d.",
2468 function,
2469 entry );
2470
2471 goto on_error;
2472 }
2473 if( entry == internal_pool->current_entry )
2474 {
2475 internal_pool->current_entry = -1;
2476 internal_pool->current_handle = NULL;
2477 }
2478 return( 0 );
2479
2480 on_error:
2481 if( safe_last_used_list_element != NULL )
2482 {
2483 libcdata_list_element_free(
2484 &safe_last_used_list_element,
2485 NULL,
2486 NULL );
2487 }
2488 return( -1 );
2489 }
2490
2491 /* Closes a handle in the pool
2492 * Returns 0 if successful or -1 on error
2493 */
2494 int libbfio_pool_close(
2495 libbfio_pool_t *pool,
2496 int entry,
2497 libcerror_error_t **error )
2498 {
2499 libbfio_internal_pool_t *internal_pool = NULL;
2500 static char *function = "libbfio_pool_close";
2501 int result = 0;
2502
2503 if( pool == NULL )
2504 {
2505 libcerror_error_set(
2506 error,
2507 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2508 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2509 "%s: invalid pool.",
2510 function );
2511
2512 return( -1 );
2513 }
2514 internal_pool = (libbfio_internal_pool_t *) pool;
2515
2516 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
2517 if( libcthreads_read_write_lock_grab_for_write(
2518 internal_pool->read_write_lock,
2519 error ) != 1 )
2520 {
2521 libcerror_error_set(
2522 error,
2523 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2524 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2525 "%s: unable to grab read/write lock for writing.",
2526 function );
2527
2528 return( -1 );
2529 }
2530 #endif
2531 if( libbfio_internal_pool_close(
2532 internal_pool,
2533 entry,
2534 error ) != 0 )
2535 {
2536 libcerror_error_set(
2537 error,
2538 LIBCERROR_ERROR_DOMAIN_IO,
2539 LIBCERROR_IO_ERROR_CLOSE_FAILED,
2540 "%s: unable to close handle: %d.",
2541 function,
2542 entry );
2543
2544 result = -1;
2545 }
2546 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
2547 if( libcthreads_read_write_lock_release_for_write(
2548 internal_pool->read_write_lock,
2549 error ) != 1 )
2550 {
2551 libcerror_error_set(
2552 error,
2553 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2554 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2555 "%s: unable to release read/write lock for writing.",
2556 function );
2557
2558 return( -1 );
2559 }
2560 #endif
2561 return( result );
2562 }
2563
2564 /* Closes all the files in the pool
2565 * Returns 0 if successful or -1 on error
2566 */
2567 int libbfio_pool_close_all(
2568 libbfio_pool_t *pool,
2569 libcerror_error_t **error )
2570 {
2571 libbfio_handle_t *handle = NULL;
2572 libbfio_internal_pool_t *internal_pool = NULL;
2573 static char *function = "libbfio_pool_close_all";
2574 int entry = 0;
2575 int is_open = 0;
2576 int number_of_handles = 0;
2577
2578 if( pool == NULL )
2579 {
2580 libcerror_error_set(
2581 error,
2582 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2583 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2584 "%s: invalid pool.",
2585 function );
2586
2587 return( -1 );
2588 }
2589 internal_pool = (libbfio_internal_pool_t *) pool;
2590
2591 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
2592 if( libcthreads_read_write_lock_grab_for_write(
2593 internal_pool->read_write_lock,
2594 error ) != 1 )
2595 {
2596 libcerror_error_set(
2597 error,
2598 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2599 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2600 "%s: unable to grab read/write lock for writing.",
2601 function );
2602
2603 return( -1 );
2604 }
2605 #endif
2606 if( libcdata_array_get_number_of_entries(
2607 internal_pool->handles_array,
2608 &number_of_handles,
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 number of handles.",
2616 function );
2617
2618 goto on_error;
2619 }
2620 for( entry = 0;
2621 entry < number_of_handles;
2622 entry++ )
2623 {
2624 if( libcdata_array_get_entry_by_index(
2625 internal_pool->handles_array,
2626 entry,
2627 (intptr_t **) &handle,
2628 error ) != 1 )
2629 {
2630 libcerror_error_set(
2631 error,
2632 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2633 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2634 "%s: unable to retrieve handle: %d.",
2635 function,
2636 entry );
2637
2638 goto on_error;
2639 }
2640 if( handle == NULL )
2641 {
2642 continue;
2643 }
2644 /* Make sure the handle is open
2645 */
2646 is_open = libbfio_handle_is_open(
2647 handle,
2648 error );
2649
2650 if( is_open == -1 )
2651 {
2652 libcerror_error_set(
2653 error,
2654 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2655 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2656 "%s: unable to determine if handle: %d is open.",
2657 function,
2658 entry );
2659
2660 goto on_error;
2661 }
2662 else if( is_open == 0 )
2663 {
2664 continue;
2665 }
2666 if( libbfio_internal_pool_close(
2667 internal_pool,
2668 entry,
2669 error ) != 0 )
2670 {
2671 libcerror_error_set(
2672 error,
2673 LIBCERROR_ERROR_DOMAIN_IO,
2674 LIBCERROR_IO_ERROR_CLOSE_FAILED,
2675 "%s: unable to close handle: %d.",
2676 function,
2677 entry );
2678
2679 goto on_error;
2680 }
2681 }
2682 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
2683 if( libcthreads_read_write_lock_release_for_write(
2684 internal_pool->read_write_lock,
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 release read/write lock for writing.",
2692 function );
2693
2694 return( -1 );
2695 }
2696 #endif
2697 return( 0 );
2698
2699 on_error:
2700 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
2701 libcthreads_read_write_lock_release_for_write(
2702 internal_pool->read_write_lock,
2703 NULL );
2704 #endif
2705 return( -1 );
2706 }
2707
2708 /* Retrieves a specific handle from the pool and opens it if needed
2709 * Returns 1 if successful or -1 on error
2710 */
2711 int libbfio_internal_pool_get_open_handle(
2712 libbfio_internal_pool_t *internal_pool,
2713 int entry,
2714 libbfio_handle_t **handle,
2715 libcerror_error_t **error )
2716 {
2717 libbfio_handle_t *safe_handle = NULL;
2718 static char *function = "libbfio_internal_pool_get_open_handle";
2719 int access_flags = 0;
2720 int is_open = 0;
2721
2722 if( internal_pool == NULL )
2723 {
2724 libcerror_error_set(
2725 error,
2726 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2727 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2728 "%s: invalid pool.",
2729 function );
2730
2731 return( -1 );
2732 }
2733 if( handle == NULL )
2734 {
2735 libcerror_error_set(
2736 error,
2737 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2738 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2739 "%s: invalid handle.",
2740 function );
2741
2742 return( -1 );
2743 }
2744 if( entry != internal_pool->current_entry )
2745 {
2746 if( libcdata_array_get_entry_by_index(
2747 internal_pool->handles_array,
2748 entry,
2749 (intptr_t **) &safe_handle,
2750 error ) != 1 )
2751 {
2752 libcerror_error_set(
2753 error,
2754 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2755 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2756 "%s: unable to retrieve handle: %d.",
2757 function,
2758 entry );
2759
2760 return( -1 );
2761 }
2762 /* Make sure the handle is open
2763 */
2764 is_open = libbfio_handle_is_open(
2765 safe_handle,
2766 error );
2767
2768 if( is_open == -1 )
2769 {
2770 libcerror_error_set(
2771 error,
2772 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2773 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2774 "%s: unable to determine if entry: %d is open.",
2775 function,
2776 entry );
2777
2778 return( -1 );
2779 }
2780 else if( is_open == 0 )
2781 {
2782 if( libbfio_handle_get_access_flags(
2783 safe_handle,
2784 &access_flags,
2785 error ) != 1 )
2786 {
2787 libcerror_error_set(
2788 error,
2789 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2790 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2791 "%s: unable to retrieve access flags.",
2792 function );
2793
2794 return( -1 );
2795 }
2796 if( libbfio_internal_pool_open_handle(
2797 internal_pool,
2798 safe_handle,
2799 access_flags,
2800 error ) != 1 )
2801 {
2802 libcerror_error_set(
2803 error,
2804 LIBCERROR_ERROR_DOMAIN_IO,
2805 LIBCERROR_IO_ERROR_OPEN_FAILED,
2806 "%s: unable to open entry: %d.",
2807 function,
2808 entry );
2809
2810 return( -1 );
2811 }
2812 }
2813 if( internal_pool->maximum_number_of_open_handles != LIBBFIO_POOL_UNLIMITED_NUMBER_OF_OPEN_HANDLES )
2814 {
2815 if( libbfio_internal_pool_move_handle_to_front_of_last_used_list(
2816 internal_pool,
2817 safe_handle,
2818 error ) != 1 )
2819 {
2820 libcerror_error_set(
2821 error,
2822 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2823 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2824 "%s: unable to move handle to front of last used list.",
2825 function );
2826
2827 return( -1 );
2828 }
2829 }
2830 internal_pool->current_entry = entry;
2831 internal_pool->current_handle = safe_handle;
2832 }
2833 *handle = internal_pool->current_handle;
2834
2835 return( 1 );
2836 }
2837
2838 /* Reads data at the current offset into the buffer
2839 * Returns the number of bytes read or -1 on error
2840 */
2841 ssize_t libbfio_pool_read_buffer(
2842 libbfio_pool_t *pool,
2843 int entry,
2844 uint8_t *buffer,
2845 size_t size,
2846 libcerror_error_t **error )
2847 {
2848 libbfio_handle_t *handle = NULL;
2849 libbfio_internal_pool_t *internal_pool = NULL;
2850 static char *function = "libbfio_pool_read_buffer";
2851 ssize_t read_count = 0;
2852
2853 if( pool == NULL )
2854 {
2855 libcerror_error_set(
2856 error,
2857 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2858 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2859 "%s: invalid pool.",
2860 function );
2861
2862 return( -1 );
2863 }
2864 internal_pool = (libbfio_internal_pool_t *) pool;
2865
2866 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
2867 if( libcthreads_read_write_lock_grab_for_write(
2868 internal_pool->read_write_lock,
2869 error ) != 1 )
2870 {
2871 libcerror_error_set(
2872 error,
2873 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2874 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2875 "%s: unable to grab read/write lock for writing.",
2876 function );
2877
2878 return( -1 );
2879 }
2880 #endif
2881 if( libbfio_internal_pool_get_open_handle(
2882 internal_pool,
2883 entry,
2884 &handle,
2885 error ) != 1 )
2886 {
2887 libcerror_error_set(
2888 error,
2889 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2890 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2891 "%s: unable to retrieve handle: %d.",
2892 function,
2893 entry );
2894
2895 read_count = -1;
2896 }
2897 else
2898 {
2899 read_count = libbfio_handle_read_buffer(
2900 handle,
2901 buffer,
2902 size,
2903 error );
2904
2905 if( read_count < 0 )
2906 {
2907 libcerror_error_set(
2908 error,
2909 LIBCERROR_ERROR_DOMAIN_IO,
2910 LIBCERROR_IO_ERROR_READ_FAILED,
2911 "%s: unable to read from entry: %d.",
2912 function,
2913 entry );
2914
2915 read_count = -1;
2916 }
2917 }
2918 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
2919 if( libcthreads_read_write_lock_release_for_write(
2920 internal_pool->read_write_lock,
2921 error ) != 1 )
2922 {
2923 libcerror_error_set(
2924 error,
2925 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2926 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2927 "%s: unable to release read/write lock for writing.",
2928 function );
2929
2930 return( -1 );
2931 }
2932 #endif
2933 return( read_count );
2934 }
2935
2936 /* Reads data at a specific offset into the buffer
2937 * Returns the number of bytes read or -1 on error
2938 */
2939 ssize_t libbfio_pool_read_buffer_at_offset(
2940 libbfio_pool_t *pool,
2941 int entry,
2942 uint8_t *buffer,
2943 size_t size,
2944 off64_t offset,
2945 libcerror_error_t **error )
2946 {
2947 libbfio_handle_t *handle = NULL;
2948 libbfio_internal_pool_t *internal_pool = NULL;
2949 static char *function = "libbfio_pool_read_buffer_at_offset";
2950 ssize_t read_count = 0;
2951
2952 if( pool == NULL )
2953 {
2954 libcerror_error_set(
2955 error,
2956 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2957 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2958 "%s: invalid pool.",
2959 function );
2960
2961 return( -1 );
2962 }
2963 internal_pool = (libbfio_internal_pool_t *) pool;
2964
2965 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
2966 if( libcthreads_read_write_lock_grab_for_write(
2967 internal_pool->read_write_lock,
2968 error ) != 1 )
2969 {
2970 libcerror_error_set(
2971 error,
2972 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2973 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
2974 "%s: unable to grab read/write lock for writing.",
2975 function );
2976
2977 return( -1 );
2978 }
2979 #endif
2980 if( libbfio_internal_pool_get_open_handle(
2981 internal_pool,
2982 entry,
2983 &handle,
2984 error ) != 1 )
2985 {
2986 libcerror_error_set(
2987 error,
2988 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2989 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
2990 "%s: unable to retrieve handle: %d.",
2991 function,
2992 entry );
2993
2994 read_count = -1;
2995 }
2996 else
2997 {
2998 read_count = libbfio_handle_read_buffer_at_offset(
2999 handle,
3000 buffer,
3001 size,
3002 offset,
3003 error );
3004
3005 if( read_count < 0 )
3006 {
3007 libcerror_error_set(
3008 error,
3009 LIBCERROR_ERROR_DOMAIN_IO,
3010 LIBCERROR_IO_ERROR_READ_FAILED,
3011 "%s: unable to read from entry: %d at offset: %" PRIi64 " (0x%08" PRIx64 ").",
3012 function,
3013 entry,
3014 offset,
3015 offset );
3016
3017 read_count = -1;
3018 }
3019 }
3020 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
3021 if( libcthreads_read_write_lock_release_for_write(
3022 internal_pool->read_write_lock,
3023 error ) != 1 )
3024 {
3025 libcerror_error_set(
3026 error,
3027 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3028 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3029 "%s: unable to release read/write lock for writing.",
3030 function );
3031
3032 return( -1 );
3033 }
3034 #endif
3035 return( read_count );
3036 }
3037
3038 /* Writes data at the current offset from the buffer
3039 * Returns the number of bytes written or -1 on error
3040 */
3041 ssize_t libbfio_pool_write_buffer(
3042 libbfio_pool_t *pool,
3043 int entry,
3044 const uint8_t *buffer,
3045 size_t size,
3046 libcerror_error_t **error )
3047 {
3048 libbfio_handle_t *handle = NULL;
3049 libbfio_internal_pool_t *internal_pool = NULL;
3050 static char *function = "libbfio_pool_write_buffer";
3051 ssize_t write_count = 0;
3052
3053 if( pool == NULL )
3054 {
3055 libcerror_error_set(
3056 error,
3057 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3058 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3059 "%s: invalid pool.",
3060 function );
3061
3062 return( -1 );
3063 }
3064 internal_pool = (libbfio_internal_pool_t *) pool;
3065
3066 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
3067 if( libcthreads_read_write_lock_grab_for_write(
3068 internal_pool->read_write_lock,
3069 error ) != 1 )
3070 {
3071 libcerror_error_set(
3072 error,
3073 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3074 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3075 "%s: unable to grab read/write lock for writing.",
3076 function );
3077
3078 return( -1 );
3079 }
3080 #endif
3081 if( libbfio_internal_pool_get_open_handle(
3082 internal_pool,
3083 entry,
3084 &handle,
3085 error ) != 1 )
3086 {
3087 libcerror_error_set(
3088 error,
3089 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3090 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3091 "%s: unable to retrieve handle: %d.",
3092 function,
3093 entry );
3094
3095 write_count = -1;
3096 }
3097 else
3098 {
3099 write_count = libbfio_handle_write_buffer(
3100 handle,
3101 buffer,
3102 size,
3103 error );
3104
3105 if( write_count < 0 )
3106 {
3107 libcerror_error_set(
3108 error,
3109 LIBCERROR_ERROR_DOMAIN_IO,
3110 LIBCERROR_IO_ERROR_WRITE_FAILED,
3111 "%s: unable to write to entry: %d.",
3112 function,
3113 entry );
3114
3115 write_count = -1;
3116 }
3117 }
3118 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
3119 if( libcthreads_read_write_lock_release_for_write(
3120 internal_pool->read_write_lock,
3121 error ) != 1 )
3122 {
3123 libcerror_error_set(
3124 error,
3125 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3126 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3127 "%s: unable to release read/write lock for writing.",
3128 function );
3129
3130 return( -1 );
3131 }
3132 #endif
3133 return( write_count );
3134 }
3135
3136 /* Writes data at a specific offset from the buffer
3137 * Returns the number of bytes written or -1 on error
3138 */
3139 ssize_t libbfio_pool_write_buffer_at_offset(
3140 libbfio_pool_t *pool,
3141 int entry,
3142 const uint8_t *buffer,
3143 size_t size,
3144 off64_t offset,
3145 libcerror_error_t **error )
3146 {
3147 libbfio_handle_t *handle = NULL;
3148 libbfio_internal_pool_t *internal_pool = NULL;
3149 static char *function = "libbfio_pool_write_buffer_at_offset";
3150 ssize_t write_count = 0;
3151
3152 if( pool == NULL )
3153 {
3154 libcerror_error_set(
3155 error,
3156 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3157 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3158 "%s: invalid pool.",
3159 function );
3160
3161 return( -1 );
3162 }
3163 internal_pool = (libbfio_internal_pool_t *) pool;
3164
3165 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
3166 if( libcthreads_read_write_lock_grab_for_write(
3167 internal_pool->read_write_lock,
3168 error ) != 1 )
3169 {
3170 libcerror_error_set(
3171 error,
3172 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3173 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3174 "%s: unable to grab read/write lock for writing.",
3175 function );
3176
3177 return( -1 );
3178 }
3179 #endif
3180 if( libbfio_internal_pool_get_open_handle(
3181 internal_pool,
3182 entry,
3183 &handle,
3184 error ) != 1 )
3185 {
3186 libcerror_error_set(
3187 error,
3188 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3189 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3190 "%s: unable to retrieve handle: %d.",
3191 function,
3192 entry );
3193
3194 write_count = -1;
3195 }
3196 else
3197 {
3198 write_count = libbfio_handle_write_buffer_at_offset(
3199 handle,
3200 buffer,
3201 size,
3202 offset,
3203 error );
3204
3205 if( write_count < 0 )
3206 {
3207 libcerror_error_set(
3208 error,
3209 LIBCERROR_ERROR_DOMAIN_IO,
3210 LIBCERROR_IO_ERROR_WRITE_FAILED,
3211 "%s: unable to write to entry: %d at offset: %" PRIi64 " (0x%08" PRIx64 ").",
3212 function,
3213 entry,
3214 offset,
3215 offset );
3216
3217 write_count = -1;
3218 }
3219 }
3220 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
3221 if( libcthreads_read_write_lock_release_for_write(
3222 internal_pool->read_write_lock,
3223 error ) != 1 )
3224 {
3225 libcerror_error_set(
3226 error,
3227 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3228 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3229 "%s: unable to release read/write lock for writing.",
3230 function );
3231
3232 return( -1 );
3233 }
3234 #endif
3235 return( write_count );
3236 }
3237
3238 /* Seeks an offset in a handle in the pool
3239 * Returns the offset if successful or -1 on error
3240 */
3241 off64_t libbfio_pool_seek_offset(
3242 libbfio_pool_t *pool,
3243 int entry,
3244 off64_t offset,
3245 int whence,
3246 libcerror_error_t **error )
3247 {
3248 libbfio_handle_t *handle = NULL;
3249 libbfio_internal_pool_t *internal_pool = NULL;
3250 static char *function = "libbfio_pool_seek_offset";
3251
3252 if( pool == NULL )
3253 {
3254 libcerror_error_set(
3255 error,
3256 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3257 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3258 "%s: invalid pool.",
3259 function );
3260
3261 return( -1 );
3262 }
3263 internal_pool = (libbfio_internal_pool_t *) pool;
3264
3265 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
3266 if( libcthreads_read_write_lock_grab_for_write(
3267 internal_pool->read_write_lock,
3268 error ) != 1 )
3269 {
3270 libcerror_error_set(
3271 error,
3272 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3273 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3274 "%s: unable to grab read/write lock for writing.",
3275 function );
3276
3277 return( -1 );
3278 }
3279 #endif
3280 if( libbfio_internal_pool_get_open_handle(
3281 internal_pool,
3282 entry,
3283 &handle,
3284 error ) != 1 )
3285 {
3286 libcerror_error_set(
3287 error,
3288 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3289 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3290 "%s: unable to retrieve handle: %d.",
3291 function,
3292 entry );
3293
3294 offset = -1;
3295 }
3296 else
3297 {
3298 offset = libbfio_handle_seek_offset(
3299 handle,
3300 offset,
3301 whence,
3302 error );
3303
3304 if( offset == -1 )
3305 {
3306 libcerror_error_set(
3307 error,
3308 LIBCERROR_ERROR_DOMAIN_IO,
3309 LIBCERROR_IO_ERROR_SEEK_FAILED,
3310 "%s: unable to seek offset in entry: %d.",
3311 function,
3312 entry );
3313
3314 offset = -1;
3315 }
3316 }
3317 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
3318 if( libcthreads_read_write_lock_release_for_write(
3319 internal_pool->read_write_lock,
3320 error ) != 1 )
3321 {
3322 libcerror_error_set(
3323 error,
3324 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3325 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3326 "%s: unable to release read/write lock for writing.",
3327 function );
3328
3329 return( -1 );
3330 }
3331 #endif
3332 return( offset );
3333 }
3334
3335 /* Retrieves the current offset in a handle in the pool
3336 * Returns 1 if successful or -1 on error
3337 */
3338 int libbfio_pool_get_offset(
3339 libbfio_pool_t *pool,
3340 int entry,
3341 off64_t *offset,
3342 libcerror_error_t **error )
3343 {
3344 libbfio_handle_t *handle = NULL;
3345 libbfio_internal_pool_t *internal_pool = NULL;
3346 static char *function = "libbfio_pool_get_offset";
3347 int result = 1;
3348
3349 if( pool == NULL )
3350 {
3351 libcerror_error_set(
3352 error,
3353 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3354 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3355 "%s: invalid pool.",
3356 function );
3357
3358 return( -1 );
3359 }
3360 internal_pool = (libbfio_internal_pool_t *) pool;
3361
3362 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
3363 if( libcthreads_read_write_lock_grab_for_write(
3364 internal_pool->read_write_lock,
3365 error ) != 1 )
3366 {
3367 libcerror_error_set(
3368 error,
3369 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3370 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3371 "%s: unable to grab read/write lock for writing.",
3372 function );
3373
3374 return( -1 );
3375 }
3376 #endif
3377 if( libbfio_internal_pool_get_open_handle(
3378 internal_pool,
3379 entry,
3380 &handle,
3381 error ) != 1 )
3382 {
3383 libcerror_error_set(
3384 error,
3385 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3386 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3387 "%s: unable to retrieve handle: %d.",
3388 function,
3389 entry );
3390
3391 result = -1;
3392 }
3393 else
3394 {
3395 if( libbfio_handle_get_offset(
3396 handle,
3397 offset,
3398 error ) != 1 )
3399 {
3400 libcerror_error_set(
3401 error,
3402 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3403 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3404 "%s: unable to retrieve offset.",
3405 function );
3406
3407 result = -1;
3408 }
3409 }
3410 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
3411 if( libcthreads_read_write_lock_release_for_write(
3412 internal_pool->read_write_lock,
3413 error ) != 1 )
3414 {
3415 libcerror_error_set(
3416 error,
3417 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3418 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3419 "%s: unable to release read/write lock for writing.",
3420 function );
3421
3422 return( -1 );
3423 }
3424 #endif
3425 return( result );
3426 }
3427
3428 /* Retrieves the size of a handle in the pool
3429 * Returns 1 if successful or -1 on error
3430 */
3431 int libbfio_pool_get_size(
3432 libbfio_pool_t *pool,
3433 int entry,
3434 size64_t *size,
3435 libcerror_error_t **error )
3436 {
3437 libbfio_handle_t *handle = NULL;
3438 libbfio_internal_pool_t *internal_pool = NULL;
3439 static char *function = "libbfio_pool_get_size";
3440 int result = 1;
3441
3442 if( pool == NULL )
3443 {
3444 libcerror_error_set(
3445 error,
3446 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3447 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3448 "%s: invalid pool.",
3449 function );
3450
3451 return( -1 );
3452 }
3453 internal_pool = (libbfio_internal_pool_t *) pool;
3454
3455 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
3456 if( libcthreads_read_write_lock_grab_for_write(
3457 internal_pool->read_write_lock,
3458 error ) != 1 )
3459 {
3460 libcerror_error_set(
3461 error,
3462 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3463 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3464 "%s: unable to grab read/write lock for writing.",
3465 function );
3466
3467 return( -1 );
3468 }
3469 #endif
3470 if( libbfio_internal_pool_get_open_handle(
3471 internal_pool,
3472 entry,
3473 &handle,
3474 error ) != 1 )
3475 {
3476 libcerror_error_set(
3477 error,
3478 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3479 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3480 "%s: unable to retrieve handle: %d.",
3481 function,
3482 entry );
3483
3484 result = -1;
3485 }
3486 else
3487 {
3488 if( libbfio_handle_get_size(
3489 handle,
3490 size,
3491 error ) != 1 )
3492 {
3493 libcerror_error_set(
3494 error,
3495 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3496 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3497 "%s: unable to retrieve size of entry: %d.",
3498 function,
3499 entry );
3500
3501 result = -1;
3502 }
3503 }
3504 #if defined( HAVE_MULTI_THREAD_SUPPORT ) && !defined( HAVE_LOCAL_LIBBFIO )
3505 if( libcthreads_read_write_lock_release_for_write(
3506 internal_pool->read_write_lock,
3507 error ) != 1 )
3508 {
3509 libcerror_error_set(
3510 error,
3511 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3512 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3513 "%s: unable to release read/write lock for writing.",
3514 function );
3515
3516 return( -1 );
3517 }
3518 #endif
3519 return( result );
3520 }
3521