Debian Codegraph @

vhditools/vhdimount.c

raw symbols SCIP
1 /*
2 * Mounts a Virtual Hard Disk (VHD) image file.
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 <file_stream.h>
24 #include <memory.h>
25 #include <system_string.h>
26 #include <types.h>
27
28 #include <stdio.h>
29
30 #if defined( HAVE_IO_H ) || defined( WINAPI )
31 #include <io.h>
32 #endif
33
34 #if defined( HAVE_STDLIB_H ) || defined( WINAPI )
35 #include <stdlib.h>
36 #endif
37
38 #if defined( HAVE_UNISTD_H )
39 #include <unistd.h>
40 #endif
41
42 #include "mount_dokan.h"
43 #include "mount_fuse.h"
44 #include "mount_handle.h"
45 #include "vhditools_getopt.h"
46 #include "vhditools_i18n.h"
47 #include "vhditools_libcerror.h"
48 #include "vhditools_libclocale.h"
49 #include "vhditools_libcnotify.h"
50 #include "vhditools_libvhdi.h"
51 #include "vhditools_output.h"
52 #include "vhditools_signal.h"
53 #include "vhditools_unused.h"
54
55 mount_handle_t *vhdimount_mount_handle = NULL;
56 int vhdimount_abort = 0;
57
58 /* Prints usage information
59 */
60 void usage_fprint(
61 FILE *stream )
62 {
63 if( stream == NULL )
64 {
65 return;
66 }
67 fprintf( stream, "Use vhdimount to mount a Virtual Hard Disk (VHD) image file\n\n" );
68
69 fprintf( stream, "Usage: vhdimount [ -X extended_options ] [ -hvV ] image mount_point\n\n" );
70
71 fprintf( stream, "\timage: a Virtual Hard Disk (VHD) image file\n\n" );
72 fprintf( stream, "\tmount_point: the directory to serve as mount point\n\n" );
73
74 fprintf( stream, "\t-h: shows this help\n" );
75 fprintf( stream, "\t-v: verbose output to stderr, while vhdimount will remain running in the\n"
76 "\t foreground\n" );
77 fprintf( stream, "\t-V: print version\n" );
78 fprintf( stream, "\t-X: extended options to pass to sub system\n" );
79 }
80
81 /* Signal handler for vhdimount
82 */
83 void vhdimount_signal_handler(
84 vhditools_signal_t signal VHDITOOLS_ATTRIBUTE_UNUSED )
85 {
86 libcerror_error_t *error = NULL;
87 static char *function = "vhdimount_signal_handler";
88
89 VHDITOOLS_UNREFERENCED_PARAMETER( signal )
90
91 vhdimount_abort = 1;
92
93 if( vhdimount_mount_handle != NULL )
94 {
95 if( mount_handle_signal_abort(
96 vhdimount_mount_handle,
97 &error ) != 1 )
98 {
99 libcnotify_printf(
100 "%s: unable to signal mount handle to abort.\n",
101 function );
102
103 libcnotify_print_error_backtrace(
104 error );
105 libcerror_error_free(
106 &error );
107 }
108 }
109 /* Force stdin to close otherwise any function reading it will remain blocked
110 */
111 #if defined( WINAPI ) && !defined( __CYGWIN__ )
112 if( _close(
113 0 ) != 0 )
114 #else
115 if( close(
116 0 ) != 0 )
117 #endif
118 {
119 libcnotify_printf(
120 "%s: unable to close stdin.\n",
121 function );
122 }
123 }
124
125 /* The main program
126 */
127 #if defined( HAVE_WIDE_SYSTEM_CHARACTER )
128 int wmain( int argc, wchar_t * const argv[] )
129 #else
130 int main( int argc, char * const argv[] )
131 #endif
132 {
133 libvhdi_error_t *error = NULL;
134 system_character_t *mount_point = NULL;
135 system_character_t *option_extended_options = NULL;
136 const system_character_t *path_prefix = NULL;
137 system_character_t *source = NULL;
138 char *program = "vhdimount";
139 system_integer_t option = 0;
140 size_t path_prefix_size = 0;
141 int result = 0;
142 int verbose = 0;
143
144 #if defined( HAVE_LIBFUSE ) || defined( HAVE_LIBFUSE3 ) || defined( HAVE_LIBOSXFUSE )
145 struct fuse_operations vhdimount_fuse_operations;
146
147 #if defined( HAVE_LIBFUSE3 )
148 /* Need to set this to 1 even if there no arguments, otherwise this causes
149 * fuse: empty argv passed to fuse_session_new()
150 */
151 char *fuse_argv[ 2 ] = { program, NULL };
152 struct fuse_args vhdimount_fuse_arguments = FUSE_ARGS_INIT(1, fuse_argv);
153 #else
154 struct fuse_args vhdimount_fuse_arguments = FUSE_ARGS_INIT(0, NULL);
155 struct fuse_chan *vhdimount_fuse_channel = NULL;
156 #endif
157 struct fuse *vhdimount_fuse_handle = NULL;
158
159 #elif defined( HAVE_LIBDOKAN )
160 DOKAN_OPERATIONS vhdimount_dokan_operations;
161 DOKAN_OPTIONS vhdimount_dokan_options;
162 #endif
163
164 libcnotify_stream_set(
165 stderr,
166 NULL );
167 libcnotify_verbose_set(
168 1 );
169
170 if( libclocale_initialize(
171 "vhditools",
172 &error ) != 1 )
173 {
174 fprintf(
175 stderr,
176 "Unable to initialize locale values.\n" );
177
178 goto on_error;
179 }
180 if( vhditools_output_initialize(
181 _IONBF,
182 &error ) != 1 )
183 {
184 fprintf(
185 stderr,
186 "Unable to initialize output settings.\n" );
187
188 goto on_error;
189 }
190 vhditools_output_version_fprint(
191 stdout,
192 program );
193
194 while( ( option = vhditools_getopt(
195 argc,
196 argv,
197 _SYSTEM_STRING( "hvVX:" ) ) ) != (system_integer_t) -1 )
198 {
199 switch( option )
200 {
201 case (system_integer_t) '?':
202 default:
203 fprintf(
204 stderr,
205 "Invalid argument: %" PRIs_SYSTEM "\n",
206 argv[ optind - 1 ] );
207
208 usage_fprint(
209 stdout );
210
211 return( EXIT_FAILURE );
212
213 case (system_integer_t) 'h':
214 usage_fprint(
215 stdout );
216
217 return( EXIT_SUCCESS );
218
219 case (system_integer_t) 'v':
220 verbose = 1;
221
222 break;
223
224 case (system_integer_t) 'V':
225 vhditools_output_copyright_fprint(
226 stdout );
227
228 return( EXIT_SUCCESS );
229
230 case (system_integer_t) 'X':
231 option_extended_options = optarg;
232
233 break;
234 }
235 }
236 if( optind == argc )
237 {
238 fprintf(
239 stderr,
240 "Missing source image.\n" );
241
242 usage_fprint(
243 stdout );
244
245 return( EXIT_FAILURE );
246 }
247 source = argv[ optind++ ];
248
249 if( optind == argc )
250 {
251 fprintf(
252 stderr,
253 "Missing mount point.\n" );
254
255 usage_fprint(
256 stdout );
257
258 return( EXIT_FAILURE );
259 }
260 mount_point = argv[ optind ];
261
262 libcnotify_verbose_set(
263 verbose );
264 libvhdi_notify_set_stream(
265 stderr,
266 NULL );
267 libvhdi_notify_set_verbose(
268 verbose );
269
270 if( mount_handle_initialize(
271 &vhdimount_mount_handle,
272 &error ) != 1 )
273 {
274 fprintf(
275 stderr,
276 "Unable to initialize mount handle.\n" );
277
278 goto on_error;
279 }
280 #if defined( WINAPI )
281 path_prefix = _SYSTEM_STRING( "\\VHDI" );
282 #else
283 path_prefix = _SYSTEM_STRING( "/vhdi" );
284 #endif
285 path_prefix_size = 1 + system_string_length(
286 path_prefix );
287
288 if( mount_handle_set_path_prefix(
289 vhdimount_mount_handle,
290 path_prefix,
291 path_prefix_size,
292 &error ) != 1 )
293 {
294 fprintf(
295 stderr,
296 "Unable to set path prefix.\n" );
297
298 goto on_error;
299 }
300 if( mount_handle_open(
301 vhdimount_mount_handle,
302 source,
303 &error ) != 1 )
304 {
305 fprintf(
306 stderr,
307 "Unable to open source image\n" );
308
309 goto on_error;
310 }
311 #if defined( HAVE_LIBFUSE ) || defined( HAVE_LIBFUSE3 ) || defined( HAVE_LIBOSXFUSE )
312 if( option_extended_options != NULL )
313 {
314 #if defined( HAVE_LIBFUSE3 )
315 // fuse_opt_add_arg: Assertion `!args->argv || args->allocated' failed.
316 vhdimount_fuse_arguments.argc = 0;
317 vhdimount_fuse_arguments.argv = NULL;
318 #endif
319 /* This argument is required but ignored
320 */
321 if( fuse_opt_add_arg(
322 &vhdimount_fuse_arguments,
323 "" ) != 0 )
324 {
325 fprintf(
326 stderr,
327 "Unable add fuse arguments.\n" );
328
329 goto on_error;
330 }
331 if( fuse_opt_add_arg(
332 &vhdimount_fuse_arguments,
333 "-o" ) != 0 )
334 {
335 fprintf(
336 stderr,
337 "Unable add fuse arguments.\n" );
338
339 goto on_error;
340 }
341 if( fuse_opt_add_arg(
342 &vhdimount_fuse_arguments,
343 option_extended_options ) != 0 )
344 {
345 fprintf(
346 stderr,
347 "Unable add fuse arguments.\n" );
348
349 goto on_error;
350 }
351 }
352 if( memory_set(
353 &vhdimount_fuse_operations,
354 0,
355 sizeof( struct fuse_operations ) ) == NULL )
356 {
357 fprintf(
358 stderr,
359 "Unable to clear fuse operations.\n" );
360
361 goto on_error;
362 }
363 vhdimount_fuse_operations.open = &mount_fuse_open;
364 vhdimount_fuse_operations.read = &mount_fuse_read;
365 vhdimount_fuse_operations.release = &mount_fuse_release;
366 vhdimount_fuse_operations.opendir = &mount_fuse_opendir;
367 vhdimount_fuse_operations.readdir = &mount_fuse_readdir;
368 vhdimount_fuse_operations.releasedir = &mount_fuse_releasedir;
369 vhdimount_fuse_operations.getattr = &mount_fuse_getattr;
370 vhdimount_fuse_operations.destroy = &mount_fuse_destroy;
371
372 #if defined( HAVE_LIBFUSE3 )
373 vhdimount_fuse_handle = fuse_new(
374 &vhdimount_fuse_arguments,
375 &vhdimount_fuse_operations,
376 sizeof( struct fuse_operations ),
377 vhdimount_mount_handle );
378
379 if( vhdimount_fuse_handle == NULL )
380 {
381 fprintf(
382 stderr,
383 "Unable to create fuse handle.\n" );
384
385 goto on_error;
386 }
387 result = fuse_mount(
388 vhdimount_fuse_handle,
389 mount_point );
390
391 if( result != 0 )
392 {
393 fprintf(
394 stderr,
395 "Unable to fuse mount file system.\n" );
396
397 goto on_error;
398 }
399 #else
400 vhdimount_fuse_channel = fuse_mount(
401 mount_point,
402 &vhdimount_fuse_arguments );
403
404 if( vhdimount_fuse_channel == NULL )
405 {
406 fprintf(
407 stderr,
408 "Unable to create fuse channel.\n" );
409
410 goto on_error;
411 }
412 vhdimount_fuse_handle = fuse_new(
413 vhdimount_fuse_channel,
414 &vhdimount_fuse_arguments,
415 &vhdimount_fuse_operations,
416 sizeof( struct fuse_operations ),
417 vhdimount_mount_handle );
418
419 if( vhdimount_fuse_handle == NULL )
420 {
421 fprintf(
422 stderr,
423 "Unable to create fuse handle.\n" );
424
425 goto on_error;
426 }
427 #endif /* defined( HAVE_LIBFUSE3 ) */
428
429 if( verbose == 0 )
430 {
431 if( fuse_daemonize(
432 0 ) != 0 )
433 {
434 fprintf(
435 stderr,
436 "Unable to daemonize fuse.\n" );
437
438 goto on_error;
439 }
440 }
441 result = fuse_loop(
442 vhdimount_fuse_handle );
443
444 if( result != 0 )
445 {
446 fprintf(
447 stderr,
448 "Unable to run fuse loop.\n" );
449
450 goto on_error;
451 }
452 fuse_destroy(
453 vhdimount_fuse_handle );
454
455 fuse_opt_free_args(
456 &vhdimount_fuse_arguments );
457
458 return( EXIT_SUCCESS );
459
460 #elif defined( HAVE_LIBDOKAN )
461 if( memory_set(
462 &vhdimount_dokan_operations,
463 0,
464 sizeof( DOKAN_OPERATIONS ) ) == NULL )
465 {
466 fprintf(
467 stderr,
468 "Unable to clear dokan operations.\n" );
469
470 goto on_error;
471 }
472 if( memory_set(
473 &vhdimount_dokan_options,
474 0,
475 sizeof( DOKAN_OPTIONS ) ) == NULL )
476 {
477 fprintf(
478 stderr,
479 "Unable to clear dokan options.\n" );
480
481 goto on_error;
482 }
483 vhdimount_dokan_options.Version = DOKAN_VERSION;
484 vhdimount_dokan_options.MountPoint = mount_point;
485
486 #if DOKAN_MINIMUM_COMPATIBLE_VERSION >= 200
487 vhdimount_dokan_options.SingleThread = TRUE;
488 #else
489 vhdimount_dokan_options.ThreadCount = 0;
490 #endif
491 if( verbose != 0 )
492 {
493 vhdimount_dokan_options.Options |= DOKAN_OPTION_STDERR;
494 #if defined( HAVE_DEBUG_OUTPUT )
495 vhdimount_dokan_options.Options |= DOKAN_OPTION_DEBUG;
496 #endif
497 }
498 /* This will only affect the drive properties
499 vhdimount_dokan_options.Options |= DOKAN_OPTION_REMOVABLE;
500 */
501
502 #if ( DOKAN_VERSION >= 600 ) && ( DOKAN_VERSION < 800 )
503 vhdimount_dokan_options.Options |= DOKAN_OPTION_KEEP_ALIVE;
504
505 vhdimount_dokan_operations.CreateFile = &mount_dokan_CreateFile;
506 vhdimount_dokan_operations.OpenDirectory = &mount_dokan_OpenDirectory;
507 vhdimount_dokan_operations.CreateDirectory = NULL;
508 vhdimount_dokan_operations.Cleanup = NULL;
509 vhdimount_dokan_operations.CloseFile = &mount_dokan_CloseFile;
510 vhdimount_dokan_operations.ReadFile = &mount_dokan_ReadFile;
511 vhdimount_dokan_operations.WriteFile = NULL;
512 vhdimount_dokan_operations.FlushFileBuffers = NULL;
513 vhdimount_dokan_operations.GetFileInformation = &mount_dokan_GetFileInformation;
514 vhdimount_dokan_operations.FindFiles = &mount_dokan_FindFiles;
515 vhdimount_dokan_operations.FindFilesWithPattern = NULL;
516 vhdimount_dokan_operations.SetFileAttributes = NULL;
517 vhdimount_dokan_operations.SetFileTime = NULL;
518 vhdimount_dokan_operations.DeleteFile = NULL;
519 vhdimount_dokan_operations.DeleteDirectory = NULL;
520 vhdimount_dokan_operations.MoveFile = NULL;
521 vhdimount_dokan_operations.SetEndOfFile = NULL;
522 vhdimount_dokan_operations.SetAllocationSize = NULL;
523 vhdimount_dokan_operations.LockFile = NULL;
524 vhdimount_dokan_operations.UnlockFile = NULL;
525 vhdimount_dokan_operations.GetFileSecurity = NULL;
526 vhdimount_dokan_operations.SetFileSecurity = NULL;
527 vhdimount_dokan_operations.GetDiskFreeSpace = NULL;
528 vhdimount_dokan_operations.GetVolumeInformation = &mount_dokan_GetVolumeInformation;
529 vhdimount_dokan_operations.Unmount = &mount_dokan_Unmount;
530
531 #else
532 vhdimount_dokan_operations.ZwCreateFile = &mount_dokan_ZwCreateFile;
533 vhdimount_dokan_operations.Cleanup = NULL;
534 vhdimount_dokan_operations.CloseFile = &mount_dokan_CloseFile;
535 vhdimount_dokan_operations.ReadFile = &mount_dokan_ReadFile;
536 vhdimount_dokan_operations.WriteFile = NULL;
537 vhdimount_dokan_operations.FlushFileBuffers = NULL;
538 vhdimount_dokan_operations.GetFileInformation = &mount_dokan_GetFileInformation;
539 vhdimount_dokan_operations.FindFiles = &mount_dokan_FindFiles;
540 vhdimount_dokan_operations.FindFilesWithPattern = NULL;
541 vhdimount_dokan_operations.SetFileAttributes = NULL;
542 vhdimount_dokan_operations.SetFileTime = NULL;
543 vhdimount_dokan_operations.DeleteFile = NULL;
544 vhdimount_dokan_operations.DeleteDirectory = NULL;
545 vhdimount_dokan_operations.MoveFile = NULL;
546 vhdimount_dokan_operations.SetEndOfFile = NULL;
547 vhdimount_dokan_operations.SetAllocationSize = NULL;
548 vhdimount_dokan_operations.LockFile = NULL;
549 vhdimount_dokan_operations.UnlockFile = NULL;
550 vhdimount_dokan_operations.GetFileSecurity = NULL;
551 vhdimount_dokan_operations.SetFileSecurity = NULL;
552 vhdimount_dokan_operations.GetDiskFreeSpace = NULL;
553 vhdimount_dokan_operations.GetVolumeInformation = &mount_dokan_GetVolumeInformation;
554 vhdimount_dokan_operations.Unmounted = NULL;
555 vhdimount_dokan_operations.FindStreams = NULL;
556 vhdimount_dokan_operations.Mounted = NULL;
557
558 #endif /* ( DOKAN_VERSION >= 600 ) && ( DOKAN_VERSION < 800 ) */
559
560 #if DOKAN_MINIMUM_COMPATIBLE_VERSION >= 200
561 DokanInit();
562 #endif
563 result = DokanMain(
564 &vhdimount_dokan_options,
565 &vhdimount_dokan_operations );
566
567 #if DOKAN_MINIMUM_COMPATIBLE_VERSION >= 200
568 DokanShutdown();
569 #endif
570 switch( result )
571 {
572 case DOKAN_SUCCESS:
573 break;
574
575 case DOKAN_ERROR:
576 fprintf(
577 stderr,
578 "Unable to run dokan main: generic error\n" );
579 break;
580
581 case DOKAN_DRIVE_LETTER_ERROR:
582 fprintf(
583 stderr,
584 "Unable to run dokan main: bad drive letter\n" );
585 break;
586
587 case DOKAN_DRIVER_INSTALL_ERROR:
588 fprintf(
589 stderr,
590 "Unable to run dokan main: unable to load driver\n" );
591 break;
592
593 case DOKAN_START_ERROR:
594 fprintf(
595 stderr,
596 "Unable to run dokan main: driver error\n" );
597 break;
598
599 case DOKAN_MOUNT_ERROR:
600 fprintf(
601 stderr,
602 "Unable to run dokan main: unable to assign drive letter\n" );
603 break;
604
605 case DOKAN_MOUNT_POINT_ERROR:
606 fprintf(
607 stderr,
608 "Unable to run dokan main: mount point error\n" );
609 break;
610
611 default:
612 fprintf(
613 stderr,
614 "Unable to run dokan main: unknown error: %d\n",
615 result );
616 break;
617 }
618 return( EXIT_SUCCESS );
619
620 #else
621 fprintf(
622 stderr,
623 "No sub system to mount Virtual Hard Disk (VHD) image format.\n" );
624
625 return( EXIT_FAILURE );
626
627 #endif /* defined( HAVE_LIBFUSE ) || defined( HAVE_LIBFUSE3 ) || defined( HAVE_LIBOSXFUSE ) */
628
629 on_error:
630 if( error != NULL )
631 {
632 libcnotify_print_error_backtrace(
633 error );
634 libcerror_error_free(
635 &error );
636 }
637 #if defined( HAVE_LIBFUSE ) || defined( HAVE_LIBFUSE3 ) || defined( HAVE_LIBOSXFUSE )
638 if( vhdimount_fuse_handle != NULL )
639 {
640 fuse_destroy(
641 vhdimount_fuse_handle );
642 }
643 fuse_opt_free_args(
644 &vhdimount_fuse_arguments );
645 #endif
646 if( vhdimount_mount_handle != NULL )
647 {
648 mount_handle_free(
649 &vhdimount_mount_handle,
650 NULL );
651 }
652 return( EXIT_FAILURE );
653 }
654