Debian Codegraph @

vhditools/mount_handle.c

raw symbols SCIP
1 /*
2 * Mount handle
3 *
4 * Copyright (C) 2012-2025, 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 <narrow_string.h>
25 #include <system_string.h>
26 #include <types.h>
27 #include <wide_string.h>
28
29 #include "mount_file_entry.h"
30 #include "mount_file_system.h"
31 #include "mount_handle.h"
32 #include "vhditools_libcerror.h"
33 #include "vhditools_libcpath.h"
34 #include "vhditools_libvhdi.h"
35
36 /* Creates a mount handle
37 * Make sure the value mount_handle is referencing, is set to NULL
38 * Returns 1 if successful or -1 on error
39 */
40 int mount_handle_initialize(
41 mount_handle_t **mount_handle,
42 libcerror_error_t **error )
43 {
44 static char *function = "mount_handle_initialize";
45
46 if( mount_handle == NULL )
47 {
48 libcerror_error_set(
49 error,
50 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
51 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
52 "%s: invalid mount handle.",
53 function );
54
55 return( -1 );
56 }
57 if( *mount_handle != NULL )
58 {
59 libcerror_error_set(
60 error,
61 LIBCERROR_ERROR_DOMAIN_RUNTIME,
62 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
63 "%s: invalid mount handle value already set.",
64 function );
65
66 return( -1 );
67 }
68 *mount_handle = memory_allocate_structure(
69 mount_handle_t );
70
71 if( *mount_handle == NULL )
72 {
73 libcerror_error_set(
74 error,
75 LIBCERROR_ERROR_DOMAIN_MEMORY,
76 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
77 "%s: unable to create mount handle.",
78 function );
79
80 goto on_error;
81 }
82 if( memory_set(
83 *mount_handle,
84 0,
85 sizeof( mount_handle_t ) ) == NULL )
86 {
87 libcerror_error_set(
88 error,
89 LIBCERROR_ERROR_DOMAIN_MEMORY,
90 LIBCERROR_MEMORY_ERROR_SET_FAILED,
91 "%s: unable to clear mount handle.",
92 function );
93
94 goto on_error;
95 }
96 if( mount_file_system_initialize(
97 &( ( *mount_handle )->file_system ),
98 error ) != 1 )
99 {
100 libcerror_error_set(
101 error,
102 LIBCERROR_ERROR_DOMAIN_RUNTIME,
103 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
104 "%s: unable to initialize file system.",
105 function );
106
107 goto on_error;
108 }
109 return( 1 );
110
111 on_error:
112 if( *mount_handle != NULL )
113 {
114 memory_free(
115 *mount_handle );
116
117 *mount_handle = NULL;
118 }
119 return( -1 );
120 }
121
122 /* Frees a mount handle
123 * Returns 1 if successful or -1 on error
124 */
125 int mount_handle_free(
126 mount_handle_t **mount_handle,
127 libcerror_error_t **error )
128 {
129 static char *function = "mount_handle_free";
130 int result = 1;
131
132 if( mount_handle == NULL )
133 {
134 libcerror_error_set(
135 error,
136 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
137 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
138 "%s: invalid mount handle.",
139 function );
140
141 return( -1 );
142 }
143 if( *mount_handle != NULL )
144 {
145 if( ( *mount_handle )->basename != NULL )
146 {
147 memory_free(
148 ( *mount_handle )->basename );
149 }
150 if( mount_file_system_free(
151 &( ( *mount_handle )->file_system ),
152 error ) != 1 )
153 {
154 libcerror_error_set(
155 error,
156 LIBCERROR_ERROR_DOMAIN_RUNTIME,
157 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
158 "%s: unable to free file system.",
159 function );
160
161 result = -1;
162 }
163 memory_free(
164 *mount_handle );
165
166 *mount_handle = NULL;
167 }
168 return( result );
169 }
170
171 /* Signals the mount handle to abort
172 * Returns 1 if successful or -1 on error
173 */
174 int mount_handle_signal_abort(
175 mount_handle_t *mount_handle,
176 libcerror_error_t **error )
177 {
178 static char *function = "mount_handle_signal_abort";
179
180 if( mount_handle == NULL )
181 {
182 libcerror_error_set(
183 error,
184 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
185 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
186 "%s: invalid mount handle.",
187 function );
188
189 return( -1 );
190 }
191 if( mount_file_system_signal_abort(
192 mount_handle->file_system,
193 error ) != 1 )
194 {
195 libcerror_error_set(
196 error,
197 LIBCERROR_ERROR_DOMAIN_RUNTIME,
198 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
199 "%s: unable to signal file system to abort.",
200 function );
201
202 return( -1 );
203 }
204 return( 1 );
205 }
206
207 /* Sets the basename
208 * Returns 1 if successful or -1 on error
209 */
210 int mount_handle_set_basename(
211 mount_handle_t *mount_handle,
212 const system_character_t *basename,
213 size_t basename_size,
214 libcerror_error_t **error )
215 {
216 static char *function = "mount_handle_set_basename";
217
218 if( mount_handle == NULL )
219 {
220 libcerror_error_set(
221 error,
222 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
223 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
224 "%s: invalid mount handle.",
225 function );
226
227 return( -1 );
228 }
229 if( mount_handle->basename != NULL )
230 {
231 libcerror_error_set(
232 error,
233 LIBCERROR_ERROR_DOMAIN_RUNTIME,
234 LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
235 "%s: invalid mount handle - basename value already set.",
236 function );
237
238 return( -1 );
239 }
240 if( basename == NULL )
241 {
242 libcerror_error_set(
243 error,
244 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
245 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
246 "%s: invalid basename.",
247 function );
248
249 return( -1 );
250 }
251 if( basename_size == 0 )
252 {
253 libcerror_error_set(
254 error,
255 LIBCERROR_ERROR_DOMAIN_RUNTIME,
256 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
257 "%s: missing basename.",
258 function );
259
260 goto on_error;
261 }
262 if( basename_size > (size_t) ( MEMORY_MAXIMUM_ALLOCATION_SIZE / sizeof( system_character_t ) ) )
263 {
264 libcerror_error_set(
265 error,
266 LIBCERROR_ERROR_DOMAIN_RUNTIME,
267 LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM,
268 "%s: invalid basename size value exceeds maximum.",
269 function );
270
271 goto on_error;
272 }
273 mount_handle->basename = system_string_allocate(
274 basename_size );
275
276 if( mount_handle->basename == NULL )
277 {
278 libcerror_error_set(
279 error,
280 LIBCERROR_ERROR_DOMAIN_MEMORY,
281 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
282 "%s: unable to create basename string.",
283 function );
284
285 goto on_error;
286 }
287 if( system_string_copy(
288 mount_handle->basename,
289 basename,
290 basename_size ) == NULL )
291 {
292 libcerror_error_set(
293 error,
294 LIBCERROR_ERROR_DOMAIN_RUNTIME,
295 LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
296 "%s: unable to copy basename.",
297 function );
298
299 goto on_error;
300 }
301 mount_handle->basename[ basename_size - 1 ] = 0;
302
303 mount_handle->basename_size = basename_size;
304
305 return( 1 );
306
307 on_error:
308 if( mount_handle->basename != NULL )
309 {
310 memory_free(
311 mount_handle->basename );
312
313 mount_handle->basename = NULL;
314 }
315 mount_handle->basename_size = 0;
316
317 return( -1 );
318 }
319
320 /* Sets the path prefix
321 * Returns 1 if successful or -1 on error
322 */
323 int mount_handle_set_path_prefix(
324 mount_handle_t *mount_handle,
325 const system_character_t *path_prefix,
326 size_t path_prefix_size,
327 libcerror_error_t **error )
328 {
329 static char *function = "mount_handle_set_path_prefix";
330
331 if( mount_handle == NULL )
332 {
333 libcerror_error_set(
334 error,
335 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
336 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
337 "%s: invalid mount handle.",
338 function );
339
340 return( -1 );
341 }
342 if( mount_file_system_set_path_prefix(
343 mount_handle->file_system,
344 path_prefix,
345 path_prefix_size,
346 error ) != 1 )
347 {
348 libcerror_error_set(
349 error,
350 LIBCERROR_ERROR_DOMAIN_RUNTIME,
351 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
352 "%s: unable to set path prefix.",
353 function );
354
355 return( -1 );
356 }
357 return( 1 );
358 }
359
360 /* Opens the mount handle
361 * Returns 1 if successful, 0 if not or -1 on error
362 */
363 int mount_handle_open(
364 mount_handle_t *mount_handle,
365 const system_character_t *filename,
366 libcerror_error_t **error )
367 {
368 libvhdi_file_t *vhdi_file = NULL;
369 system_character_t *basename_end = NULL;
370 static char *function = "mount_handle_open";
371 size_t basename_length = 0;
372 size_t filename_length = 0;
373 int result = 0;
374
375 if( mount_handle == NULL )
376 {
377 libcerror_error_set(
378 error,
379 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
380 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
381 "%s: invalid mount handle.",
382 function );
383
384 return( -1 );
385 }
386 if( filename == NULL )
387 {
388 libcerror_error_set(
389 error,
390 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
391 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
392 "%s: invalid filename.",
393 function );
394
395 return( -1 );
396 }
397 filename_length = system_string_length(
398 filename );
399
400 basename_end = system_string_search_character_reverse(
401 filename,
402 (system_character_t) LIBCPATH_SEPARATOR,
403 filename_length + 1 );
404
405 if( basename_end != NULL )
406 {
407 basename_length = (size_t) ( basename_end - filename ) + 1;
408 }
409 if( basename_length > 0 )
410 {
411 if( mount_handle_set_basename(
412 mount_handle,
413 filename,
414 basename_length,
415 error ) != 1 )
416 {
417 libcerror_error_set(
418 error,
419 LIBCERROR_ERROR_DOMAIN_RUNTIME,
420 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
421 "%s: unable to set basename.",
422 function );
423
424 goto on_error;
425 }
426 }
427 if( libvhdi_file_initialize(
428 &vhdi_file,
429 error ) != 1 )
430 {
431 libcerror_error_set(
432 error,
433 LIBCERROR_ERROR_DOMAIN_RUNTIME,
434 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
435 "%s: unable to initialize file.",
436 function );
437
438 goto on_error;
439 }
440 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
441 result = libvhdi_file_open_wide(
442 vhdi_file,
443 filename,
444 LIBVHDI_OPEN_READ,
445 error );
446 #else
447 result = libvhdi_file_open(
448 vhdi_file,
449 filename,
450 LIBVHDI_OPEN_READ,
451 error );
452 #endif
453 if( result == -1 )
454 {
455 libcerror_error_set(
456 error,
457 LIBCERROR_ERROR_DOMAIN_IO,
458 LIBCERROR_IO_ERROR_OPEN_FAILED,
459 "%s: unable to open file.",
460 function );
461
462 goto on_error;
463 }
464 if( mount_handle_open_parent(
465 mount_handle,
466 vhdi_file,
467 error ) == -1 )
468 {
469 libcerror_error_set(
470 error,
471 LIBCERROR_ERROR_DOMAIN_IO,
472 LIBCERROR_IO_ERROR_OPEN_FAILED,
473 "%s: unable to open parent file.",
474 function );
475
476 goto on_error;
477 }
478 if( mount_file_system_append_file(
479 mount_handle->file_system,
480 vhdi_file,
481 error ) != 1 )
482 {
483 libcerror_error_set(
484 error,
485 LIBCERROR_ERROR_DOMAIN_RUNTIME,
486 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
487 "%s: unable to append file to file system.",
488 function );
489
490 goto on_error;
491 }
492 return( 1 );
493
494 on_error:
495 if( vhdi_file != NULL )
496 {
497 libvhdi_file_free(
498 &vhdi_file,
499 NULL );
500 }
501 return( -1 );
502 }
503
504 /* Opens a parent file
505 * Returns 1 if successful, 0 if no parent or -1 on error
506 */
507 int mount_handle_open_parent(
508 mount_handle_t *mount_handle,
509 libvhdi_file_t *vhdi_file,
510 libcerror_error_t **error )
511 {
512 uint8_t guid[ 16 ];
513
514 libvhdi_file_t *parent_vhdi_file = NULL;
515 system_character_t *parent_basename_end = NULL;
516 system_character_t *parent_filename = NULL;
517 system_character_t *parent_path = NULL;
518 static char *function = "mount_handle_open_parent";
519 size_t parent_basename_length = 0;
520 size_t parent_filename_size = 0;
521 size_t parent_path_size = 0;
522 int result = 0;
523
524 if( mount_handle == NULL )
525 {
526 libcerror_error_set(
527 error,
528 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
529 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
530 "%s: invalid mount handle.",
531 function );
532
533 return( -1 );
534 }
535 result = libvhdi_file_get_parent_identifier(
536 vhdi_file,
537 guid,
538 16,
539 error );
540
541 if( result == -1 )
542 {
543 libcerror_error_set(
544 error,
545 LIBCERROR_ERROR_DOMAIN_RUNTIME,
546 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
547 "%s: unable to retrieve parent content identifier.",
548 function );
549
550 goto on_error;
551 }
552 else if( result != 1 )
553 {
554 return( 0 );
555 }
556 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
557 result = libvhdi_file_get_utf16_parent_filename_size(
558 vhdi_file,
559 &parent_filename_size,
560 error );
561 #else
562 result = libvhdi_file_get_utf8_parent_filename_size(
563 vhdi_file,
564 &parent_filename_size,
565 error );
566 #endif
567 if( result != 1 )
568 {
569 libcerror_error_set(
570 error,
571 LIBCERROR_ERROR_DOMAIN_RUNTIME,
572 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
573 "%s: unable to retrieve parent filename size.",
574 function );
575
576 goto on_error;
577 }
578 if( parent_filename_size == 0 )
579 {
580 libcerror_error_set(
581 error,
582 LIBCERROR_ERROR_DOMAIN_RUNTIME,
583 LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
584 "%s: missing parent filename.",
585 function );
586
587 goto on_error;
588 }
589 if( parent_filename_size > (size_t) ( MEMORY_MAXIMUM_ALLOCATION_SIZE / sizeof( system_character_t ) ) )
590 {
591 libcerror_error_set(
592 error,
593 LIBCERROR_ERROR_DOMAIN_RUNTIME,
594 LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM,
595 "%s: invalid parent filename size value exceeds maximum.",
596 function );
597
598 goto on_error;
599 }
600 parent_filename = system_string_allocate(
601 parent_filename_size );
602
603 if( parent_filename == NULL )
604 {
605 libcerror_error_set(
606 error,
607 LIBCERROR_ERROR_DOMAIN_MEMORY,
608 LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
609 "%s: unable to create parent filename string.",
610 function );
611
612 goto on_error;
613 }
614 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
615 result = libvhdi_file_get_utf16_parent_filename(
616 vhdi_file,
617 (uint16_t *) parent_filename,
618 parent_filename_size,
619 error );
620 #else
621 result = libvhdi_file_get_utf8_parent_filename(
622 vhdi_file,
623 (uint8_t *) parent_filename,
624 parent_filename_size,
625 error );
626 #endif
627 if( result != 1 )
628 {
629 libcerror_error_set(
630 error,
631 LIBCERROR_ERROR_DOMAIN_RUNTIME,
632 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
633 "%s: unable to retrieve parent filename.",
634 function );
635
636 goto on_error;
637 }
638 parent_basename_end = system_string_search_character_reverse(
639 parent_filename,
640 (system_character_t) '\\',
641 parent_filename_size );
642
643 if( parent_basename_end != NULL )
644 {
645 parent_basename_length = (size_t) ( parent_basename_end - parent_filename ) + 1;
646 }
647 if( mount_handle->basename == NULL )
648 {
649 parent_path = &( parent_filename[ parent_basename_length ] );
650 parent_path_size = parent_filename_size - ( parent_basename_length + 1 );
651 }
652 else
653 {
654 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
655 if( libcpath_path_join_wide(
656 &parent_path,
657 &parent_path_size,
658 mount_handle->basename,
659 mount_handle->basename_size - 1,
660 &( parent_filename[ parent_basename_length ] ),
661 parent_filename_size - ( parent_basename_length + 1 ),
662 error ) != 1 )
663 #else
664 if( libcpath_path_join(
665 &parent_path,
666 &parent_path_size,
667 mount_handle->basename,
668 mount_handle->basename_size - 1,
669 &( parent_filename[ parent_basename_length ] ),
670 parent_filename_size - ( parent_basename_length + 1 ),
671 error ) != 1 )
672 #endif
673 {
674 libcerror_error_set(
675 error,
676 LIBCERROR_ERROR_DOMAIN_RUNTIME,
677 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
678 "%s: unable to create parent path.",
679 function );
680
681 goto on_error;
682 }
683 }
684 if( libvhdi_file_initialize(
685 &parent_vhdi_file,
686 error ) != 1 )
687 {
688 libcerror_error_set(
689 error,
690 LIBCERROR_ERROR_DOMAIN_RUNTIME,
691 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
692 "%s: unable to initialize parent file.",
693 function );
694
695 goto on_error;
696 }
697 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
698 if( libvhdi_file_open_wide(
699 parent_vhdi_file,
700 parent_path,
701 LIBVHDI_OPEN_READ,
702 error ) != 1 )
703 #else
704 if( libvhdi_file_open(
705 parent_vhdi_file,
706 parent_path,
707 LIBVHDI_OPEN_READ,
708 error ) != 1 )
709 #endif
710 {
711 libcerror_error_set(
712 error,
713 LIBCERROR_ERROR_DOMAIN_IO,
714 LIBCERROR_IO_ERROR_OPEN_FAILED,
715 "%s: unable to open parent file: %" PRIs_SYSTEM ".",
716 function,
717 parent_path );
718
719 goto on_error;
720 }
721 if( mount_handle_open_parent(
722 mount_handle,
723 parent_vhdi_file,
724 error ) == -1 )
725 {
726 libcerror_error_set(
727 error,
728 LIBCERROR_ERROR_DOMAIN_IO,
729 LIBCERROR_IO_ERROR_OPEN_FAILED,
730 "%s: unable to open parent file: %" PRIs_SYSTEM ".",
731 function,
732 parent_path );
733
734 return( -1 );
735 }
736 if( libvhdi_file_set_parent_file(
737 vhdi_file,
738 parent_vhdi_file,
739 error ) != 1 )
740 {
741 libcerror_error_set(
742 error,
743 LIBCERROR_ERROR_DOMAIN_RUNTIME,
744 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
745 "%s: unable to set parent file.",
746 function );
747
748 goto on_error;
749 }
750 if( mount_file_system_append_file(
751 mount_handle->file_system,
752 parent_vhdi_file,
753 error ) != 1 )
754 {
755 libcerror_error_set(
756 error,
757 LIBCERROR_ERROR_DOMAIN_RUNTIME,
758 LIBCERROR_RUNTIME_ERROR_APPEND_FAILED,
759 "%s: unable to append parent file to file system.",
760 function );
761
762 goto on_error;
763 }
764 if( parent_path != NULL )
765 {
766 if( mount_handle->basename != NULL )
767 {
768 memory_free(
769 parent_path );
770 }
771 parent_path = NULL;
772 }
773 if( parent_filename != NULL )
774 {
775 memory_free(
776 parent_filename );
777
778 parent_filename = NULL;
779 }
780 return( 1 );
781
782 on_error:
783 if( parent_vhdi_file != NULL )
784 {
785 libvhdi_file_free(
786 &parent_vhdi_file,
787 NULL );
788 }
789 if( ( parent_path != NULL )
790 && ( mount_handle->basename != NULL ) )
791 {
792 memory_free(
793 parent_path );
794 }
795 if( parent_filename != NULL )
796 {
797 memory_free(
798 parent_filename );
799 }
800 return( -1 );
801 }
802
803 /* Closes the mount handle
804 * Returns the 0 if succesful or -1 on error
805 */
806 int mount_handle_close(
807 mount_handle_t *mount_handle,
808 libcerror_error_t **error )
809 {
810 libvhdi_file_t *vhdi_file = NULL;
811 static char *function = "mount_handle_close";
812 int file_index = 0;
813 int number_of_files = 0;
814 int result = 0;
815
816 if( mount_handle == NULL )
817 {
818 libcerror_error_set(
819 error,
820 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
821 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
822 "%s: invalid mount handle.",
823 function );
824
825 return( -1 );
826 }
827 if( mount_file_system_get_number_of_files(
828 mount_handle->file_system,
829 &number_of_files,
830 error ) != 1 )
831 {
832 libcerror_error_set(
833 error,
834 LIBCERROR_ERROR_DOMAIN_RUNTIME,
835 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
836 "%s: unable to retrieve number of files.",
837 function );
838
839 result = -1;
840 }
841 for( file_index = number_of_files - 1;
842 file_index > 0;
843 file_index-- )
844 {
845 if( mount_file_system_get_file_by_index(
846 mount_handle->file_system,
847 file_index,
848 &vhdi_file,
849 error ) != 1 )
850 {
851 libcerror_error_set(
852 error,
853 LIBCERROR_ERROR_DOMAIN_RUNTIME,
854 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
855 "%s: unable to retrieve file: %d.",
856 function,
857 file_index );
858
859 result = -1;
860 }
861 /* TODO remove vhdi_file from file system */
862
863 if( libvhdi_file_close(
864 vhdi_file,
865 error ) != 0 )
866 {
867 libcerror_error_set(
868 error,
869 LIBCERROR_ERROR_DOMAIN_IO,
870 LIBCERROR_IO_ERROR_CLOSE_FAILED,
871 "%s: unable to close file: %d.",
872 function,
873 file_index );
874
875 result = -1;
876 }
877 if( libvhdi_file_free(
878 &vhdi_file,
879 error ) != 1 )
880 {
881 libcerror_error_set(
882 error,
883 LIBCERROR_ERROR_DOMAIN_RUNTIME,
884 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
885 "%s: unable to free file: %d.",
886 function,
887 file_index );
888
889 result = -1;
890 }
891 }
892 return( result );
893 }
894
895 /* Retrieves a file entry for a specific path
896 * Returns 1 if successful, 0 if no such file entry or -1 on error
897 */
898 int mount_handle_get_file_entry_by_path(
899 mount_handle_t *mount_handle,
900 const system_character_t *path,
901 mount_file_entry_t **file_entry,
902 libcerror_error_t **error )
903 {
904 libvhdi_file_t *vhdi_file = NULL;
905 const system_character_t *filename = NULL;
906 static char *function = "mount_handle_get_file_entry_by_path";
907 size_t filename_length = 0;
908 size_t path_index = 0;
909 size_t path_length = 0;
910 int result = 0;
911
912 if( mount_handle == NULL )
913 {
914 libcerror_error_set(
915 error,
916 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
917 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
918 "%s: invalid mount handle.",
919 function );
920
921 return( -1 );
922 }
923 if( path == NULL )
924 {
925 libcerror_error_set(
926 error,
927 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
928 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
929 "%s: invalid path.",
930 function );
931
932 return( -1 );
933 }
934 path_length = system_string_length(
935 path );
936
937 if( path_length == 0 )
938 {
939 libcerror_error_set(
940 error,
941 LIBCERROR_ERROR_DOMAIN_RUNTIME,
942 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
943 "%s: invalid path length value out of bounds.",
944 function );
945
946 goto on_error;
947 }
948 if( ( path_length >= 2 )
949 && ( path[ path_length - 1 ] == LIBCPATH_SEPARATOR ) )
950 {
951 path_length--;
952 }
953 path_index = path_length;
954
955 while( path_index > 0 )
956 {
957 if( path[ path_index ] == LIBCPATH_SEPARATOR )
958 {
959 break;
960 }
961 path_index--;
962 }
963 /* Ignore the name of the root item
964 */
965 if( path_length == 0 )
966 {
967 filename = _SYSTEM_STRING( "" );
968 filename_length = 0;
969 }
970 else
971 {
972 filename = &( path[ path_index + 1 ] );
973 filename_length = path_length - ( path_index + 1 );
974 }
975 result = mount_file_system_get_file_by_path(
976 mount_handle->file_system,
977 path,
978 path_length,
979 &vhdi_file,
980 error );
981
982 if( result == -1 )
983 {
984 libcerror_error_set(
985 error,
986 LIBCERROR_ERROR_DOMAIN_RUNTIME,
987 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
988 "%s: unable to retrieve file.",
989 function );
990
991 goto on_error;
992 }
993 else if( result != 0 )
994 {
995 if( mount_file_entry_initialize(
996 file_entry,
997 mount_handle->file_system,
998 filename,
999 filename_length,
1000 vhdi_file,
1001 error ) != 1 )
1002 {
1003 libcerror_error_set(
1004 error,
1005 LIBCERROR_ERROR_DOMAIN_RUNTIME,
1006 LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
1007 "%s: unable to initialize file entry.",
1008 function );
1009
1010 goto on_error;
1011 }
1012 }
1013 return( result );
1014
1015 on_error:
1016 return( -1 );
1017 }
1018