4 * Use wimlib to create, modify, extract, mount, unmount, or display information
9 * Copyright (C) 2012 Eric Biggers
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
41 #define swap(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
44 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (char**)argv, "", \
63 static const char *path_basename(const char *path)
70 /* Trailing slashes. */
71 while ((p != path - 1) && *p == '/')
74 while ((p != path - 1) && *p != '/')
81 static const char *usage_strings[] = {
83 " imagex append DIRECTORY WIMFILE [\"IMAGE_NAME\"] [\"DESCRIPTION\"] [--boot]\n"
84 " [--check] [--flags EDITIONID] [--dereference]\n",
86 " imagex apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all] DIRECTORY [--check]\n"
87 " [--hardlink] [--symlink] [--verbose]\n",
89 " imagex capture DIRECTORY WIMFILE [\"IMAGE_NAME\"] [\"DESCRIPTION\"]\n"
90 " l [--boot] [--check] [--compress[=TYPE]]\n"
91 " [--flags \"EditionID\"] [--verbose] [--dereference]\n",
93 " imagex delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n",
95 " imagex dir WIMFILE (IMAGE_NUM | IMAGE_NAME | \"all\")\n",
97 " imagex export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
98 " DEST_WIMFILE [\"DEST_IMAGE_NAME\"] [\"DEST_IMAGE_DESCRIPTION\"]\n"
99 " [--boot] [--check] [--compress[=TYPE]]\n",
101 " imagex info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
102 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
103 " [--xml] [--extract-xml FILE] [--metadata]\n",
105 " imagex join [--check] WIMFILE SPLIT_WIM...\n",
107 " imagex mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
108 " [--check] [--debug] [--stream-interface=INTERFACE]\n",
110 " imagex mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
111 " [--check] [--debug] [--stream-interface=INTERFACE]\n",
113 " imagex split WIMFILE SPLIT_WIMFILE PART_SIZE [--check]\n",
115 " imagex unmount DIRECTORY [--commit] [--check]\n",
118 static const struct option common_options[] = {
119 {"help", 0, NULL, 'h'},
120 {"version", 0, NULL, 'v'},
124 static const struct option append_options[] = {
125 {"boot", no_argument, NULL, 'b'},
126 {"check", no_argument, NULL, 'c'},
127 {"flags", required_argument, NULL, 'f'},
128 {"dereference", no_argument, NULL, 'L'},
131 static const struct option apply_options[] = {
132 {"check", no_argument, NULL, 'c'},
133 {"hardlink", no_argument, NULL, 'h'},
134 {"symlink", no_argument, NULL, 's'},
135 {"verbose", no_argument, NULL, 'v'},
138 static const struct option capture_options[] = {
139 {"boot", no_argument, NULL, 'b'},
140 {"check", no_argument, NULL, 'c'},
141 {"compress", optional_argument, NULL, 'x'},
142 {"flags", required_argument, NULL, 'f'},
143 {"verbose", no_argument, NULL,'v'},
144 {"dereference", no_argument, NULL, 'L'},
147 static const struct option delete_options[] = {
148 {"check", no_argument, NULL, 'c'},
152 static const struct option export_options[] = {
153 {"boot", no_argument, NULL, 'b'},
154 {"check", no_argument , NULL, 'c'},
155 {"compress", optional_argument, NULL, 'x'},
159 static const struct option info_options[] = {
160 {"boot", no_argument, NULL, 'b'},
161 {"check", no_argument, NULL, 'c'},
162 {"header", no_argument, NULL, 'h'},
163 {"lookup-table", no_argument, NULL, 'l'},
164 {"xml", no_argument, NULL, 'x'},
165 {"extract-xml", required_argument, NULL, 'X'},
166 {"metadata", no_argument, NULL, 'm'},
170 static const struct option join_options[] = {
171 {"check", no_argument, NULL, 'c'},
175 static const struct option mount_options[] = {
176 {"check", no_argument, NULL, 'c'},
177 {"debug", no_argument, NULL, 'd'},
178 {"stream-interface", required_argument, NULL, 's'},
182 static const struct option split_options[] = {
183 {"check", no_argument, NULL, 'c'},
187 static const struct option unmount_options[] = {
188 {"commit", no_argument, NULL, 'c'},
189 {"check", no_argument, NULL, 'C'},
194 /* Print formatted error message to stderr. */
195 static void imagex_error(const char *format, ...)
198 va_start(va, format);
199 fputs("ERROR: ", stderr);
200 vfprintf(stderr, format, va);
205 /* Print formatted error message to stderr. */
206 static void imagex_error_with_errno(const char *format, ...)
208 int errno_save = errno;
210 va_start(va, format);
211 fputs("ERROR: ", stderr);
212 vfprintf(stderr, format, va);
213 fprintf(stderr, ": %s\n", strerror(errno_save));
218 static inline void version()
220 static const char *s =
221 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
222 "Copyright (C) 2012 Eric Biggers\n"
223 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
224 "This is free software: you are free to change and redistribute it.\n"
225 "There is NO WARRANTY, to the extent permitted by law.\n"
227 "Report bugs to "PACKAGE_BUGREPORT".\n";
231 static inline void usage(int cmd)
233 puts("IMAGEX: Usage:");
234 fputs(usage_strings[cmd], stdout);
237 static void usage_all()
239 puts("IMAGEX: Usage:");
240 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
241 fputs(usage_strings[i], stdout);
242 static const char *extra =
244 " imagex --version\n"
246 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
248 fputs(extra, stdout);
251 static int verify_image_exists(int image)
253 if (image == WIM_NO_IMAGE) {
254 imagex_error("Not a valid image");
255 return WIMLIB_ERR_INVALID_IMAGE;
260 static int verify_image_is_single(int image)
262 if (image == WIM_ALL_IMAGES) {
263 imagex_error("Cannot specify all images for this action");
264 return WIMLIB_ERR_INVALID_IMAGE;
269 static int verify_image_exists_and_is_single(int image)
272 ret = verify_image_exists(image);
274 ret = verify_image_is_single(image);
278 static int get_compression_type(const char *optarg)
281 return WIM_COMPRESSION_TYPE_LZX;
282 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
283 return WIM_COMPRESSION_TYPE_LZX;
284 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
285 return WIM_COMPRESSION_TYPE_XPRESS;
286 else if (strcasecmp(optarg, "none") == 0)
287 return WIM_COMPRESSION_TYPE_NONE;
289 imagex_error("Invalid compression type `%s'! Must be "
290 "\"maximum\", \"fast\", or \"none\".", optarg);
291 return WIM_COMPRESSION_TYPE_INVALID;
295 static int imagex_append(int argc, const char **argv)
298 const char *flags_element = NULL;
299 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
300 int add_image_flags = 0;
301 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
309 for_opt(c, append_options) {
312 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
315 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
316 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
319 flags_element = optarg;
322 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
331 if (argc < 2 || argc > 4) {
337 name = (argc >= 3) ? argv[2] : path_basename(dir);
338 desc = (argc >= 4) ? argv[3] : NULL;
340 ret = wimlib_open_wim(wimfile, open_flags, &w);
344 ret = wimlib_add_image(w, dir, name, desc,
345 flags_element, add_image_flags);
348 ret = wimlib_overwrite(w, write_flags);
354 /* Extract one image, or all images, from a WIM file into a directory. */
355 static int imagex_apply(int argc, const char **argv)
358 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
365 const char *image_num_or_name;
366 int extract_flags = 0;
368 for_opt(c, apply_options) {
371 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
374 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
377 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
380 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
389 if (argc != 2 && argc != 3) {
396 image_num_or_name = "1";
399 image_num_or_name = argv[1];
403 ret = wimlib_open_wim(wimfile, open_flags, &w);
407 image = wimlib_resolve_image(w, image_num_or_name);
408 ret = verify_image_exists(image);
412 num_images = wimlib_get_num_images(w);
413 if (argc == 2 && num_images != 1) {
414 imagex_error("`%s' contains %d images; Please select one "
415 "(or all)", wimfile, num_images);
424 ret = stat(dir, &stbuf);
426 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
427 const char *ntfs_device = dir;
428 printf("Applying image %d of `%s' to NTFS filesystem on `%s'\n",
429 image, wimfile, ntfs_device);
430 ret = wimlib_apply_image_to_ntfs_volume(w, image,
436 if (errno != -ENOENT)
437 imagex_error_with_errno("Failed to stat `%s'", dir);
441 ret = wimlib_extract_image(w, image, dir, extract_flags);
448 /* Create a WIM file from a directory. */
449 static int imagex_capture(int argc, const char **argv)
452 int add_image_flags = 0;
453 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
454 int compression_type = WIM_COMPRESSION_TYPE_NONE;
455 const char *flags_element = NULL;
463 for_opt(c, capture_options) {
466 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
469 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
472 compression_type = get_compression_type(optarg);
473 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
477 flags_element = optarg;
480 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
483 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
486 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
496 if (argc < 2 || argc > 4) {
502 name = (argc >= 3) ? argv[2] : dir;
503 desc = (argc >= 4) ? argv[3] : NULL;
505 ret = wimlib_create_new_wim(compression_type, &w);
509 ret = wimlib_add_image(w, dir, name, desc, flags_element,
512 imagex_error("Failed to add the image `%s'", dir);
516 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
518 imagex_error("Failed to write the WIM file `%s'", wimfile);
524 /* Remove image(s) from a WIM. */
525 static int imagex_delete(int argc, const char **argv)
528 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
529 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
531 const char *image_num_or_name;
536 for_opt(c, delete_options) {
539 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
540 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
552 imagex_error("Must specify a WIM file");
554 imagex_error("Must specify an image");
559 image_num_or_name = argv[1];
561 ret = wimlib_open_wim(wimfile, open_flags, &w);
565 image = wimlib_resolve_image(w, image_num_or_name);
567 ret = verify_image_exists(image);
571 ret = wimlib_delete_image(w, image);
573 imagex_error("Failed to delete image from `%s'", wimfile);
577 ret = wimlib_overwrite(w, write_flags);
579 imagex_error("Failed to write the file `%s' with image "
587 /* Print the files contained in an image(s) in a WIM file. */
588 static int imagex_dir(int argc, const char **argv)
598 imagex_error("Must specify a WIM file");
603 imagex_error("Too many arguments");
609 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
613 part_number = wimlib_get_part_number(w, NULL);
614 if (part_number != 1) {
615 imagex_error("`%s' is part %d of a split WIM! Specify the "
616 "first part to see the files",
617 wimfile, part_number);
618 ret = WIMLIB_ERR_SPLIT_UNSUPPORTED;
623 image = wimlib_resolve_image(w, argv[2]);
624 ret = verify_image_exists(image);
628 /* Image was not specified. If the WIM only contains one image,
629 * choose that one; otherwise, print an error. */
630 num_images = wimlib_get_num_images(w);
631 if (num_images != 1) {
632 imagex_error("The file `%s' contains %d images; Please "
633 "select one.", wimfile, num_images);
641 ret = wimlib_print_files(w, image);
647 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
649 static int imagex_export(int argc, const char **argv)
652 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
653 int export_flags = 0;
654 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
655 int compression_type = WIM_COMPRESSION_TYPE_NONE;
656 bool compression_type_specified = false;
657 const char *src_wimfile;
658 const char *src_image_num_or_name;
659 const char *dest_wimfile;
660 const char *dest_name;
661 const char *dest_desc;
662 WIMStruct *src_w = NULL;
663 WIMStruct *dest_w = NULL;
669 for_opt(c, export_options) {
672 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
675 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
676 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
679 compression_type = get_compression_type(optarg);
680 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
682 compression_type_specified = true;
691 if (argc < 3 || argc > 5) {
695 src_wimfile = argv[0];
696 src_image_num_or_name = argv[1];
697 dest_wimfile = argv[2];
698 dest_name = (argc >= 4) ? argv[3] : NULL;
699 dest_desc = (argc >= 5) ? argv[4] : NULL;
700 ret = wimlib_open_wim(src_wimfile, open_flags, &src_w);
704 /* Determine if the destination is an existing file or not.
705 * If so, we try to append the exported image(s) to it; otherwise, we
706 * create a new WIM containing the exported image(s). */
707 if (stat(dest_wimfile, &stbuf) == 0) {
709 /* Destination file exists. */
710 if (!S_ISREG(stbuf.st_mode)) {
711 imagex_error("`%s' is not a regular file",
715 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
719 if (compression_type_specified && compression_type !=
720 wimlib_get_compression_type(dest_w)) {
721 imagex_error("Cannot specify a compression type that is "
722 "not the same as that used in the "
727 compression_type = wimlib_get_compression_type(dest_w);
730 /* dest_wimfile is not an existing file, so create a new WIM. */
731 if (errno == ENOENT) {
732 ret = wimlib_create_new_wim(compression_type, &dest_w);
736 imagex_error_with_errno("Cannot stat file `%s'",
742 image = wimlib_resolve_image(src_w, src_image_num_or_name);
743 ret = verify_image_exists(image);
747 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
754 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
757 ret = wimlib_overwrite(dest_w, write_flags);
764 /* Prints information about a WIM file; also can mark an image as bootable,
765 * change the name of an image, or change the description of an image. */
766 static int imagex_info(int argc, const char **argv)
772 bool lookup_table = false;
774 bool metadata = false;
775 bool short_header = true;
776 const char *xml_out_file = NULL;
778 const char *image_num_or_name = "all";
779 const char *new_name = NULL;
780 const char *new_desc = NULL;
785 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
786 WIMLIB_OPEN_FLAG_SPLIT_OK;
790 for_opt(c, info_options) {
800 short_header = false;
804 short_header = false;
808 short_header = false;
811 xml_out_file = optarg;
812 short_header = false;
816 short_header = false;
826 if (argc == 0 || argc > 4) {
832 image_num_or_name = argv[1];
842 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
844 ret = wimlib_open_wim(wimfile, open_flags, &w);
848 part_number = wimlib_get_part_number(w, &total_parts);
850 /*if (total_parts > 1 && part_number > 1) {*/
851 /*printf("Warning: this is part %d of a %d-part split WIM.\n"*/
852 /*" Select the first part if you want to see information\n"*/
853 /*" about images in the WIM.\n", */
854 /*part_number, total_parts);*/
857 image = wimlib_resolve_image(w, image_num_or_name);
858 if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
859 imagex_error("The image `%s' does not exist",
862 imagex_error("If you would like to set the boot "
863 "index to 0, specify image \"0\" with "
865 ret = WIMLIB_ERR_INVALID_IMAGE;
869 if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
871 imagex_error("Cannot specify the --boot flag "
872 "without specifying a specific "
873 "image in a multi-image WIM");
874 ret = WIMLIB_ERR_INVALID_IMAGE;
878 imagex_error("Cannot specify the NEW_NAME "
879 "without specifying a specific "
880 "image in a multi-image WIM");
881 ret = WIMLIB_ERR_INVALID_IMAGE;
886 /* Operations that print information are separated from operations that
887 * recreate the WIM file. */
888 if (!new_name && !boot) {
890 if (image == WIM_NO_IMAGE) {
891 imagex_error("`%s' is not a valid image",
893 ret = WIMLIB_ERR_INVALID_IMAGE;
897 if (image == WIM_ALL_IMAGES && short_header)
898 wimlib_print_wim_information(w);
901 wimlib_print_header(w);
904 if (total_parts != 1) {
905 printf("Warning: Only showing the lookup table "
906 "for part %d of a %d-part WIM.\n",
907 part_number, total_parts);
909 wimlib_print_lookup_table(w);
913 ret = wimlib_extract_xml_data(w, stdout);
919 fp = fopen(xml_out_file, "wb");
921 imagex_error_with_errno("Failed to open the "
927 ret = wimlib_extract_xml_data(w, fp);
928 if (fclose(fp) != 0) {
929 imagex_error("Failed to close the file `%s'",
939 wimlib_print_available_images(w, image);
942 if (total_parts != 1 && part_number != 1) {
943 imagex_error("Select part 1 of this %d-part WIM "
944 "to see the image metadata",
946 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
948 ret = wimlib_print_metadata(w, image);
953 if (total_parts != 1) {
954 imagex_error("Modifying a split WIM is not supported.");
957 if (image == WIM_ALL_IMAGES)
960 if (image == WIM_NO_IMAGE && new_name) {
961 imagex_error("Cannot specify new_name (`%s') when "
962 "using image 0", new_name);
967 if (image == wimlib_get_boot_idx(w)) {
968 printf("Image %d is already marked as "
969 "bootable.\n", image);
972 printf("Marking image %d as bootable.\n",
974 wimlib_set_boot_idx(w, image);
978 if (strcmp(wimlib_get_image_name(w, image),
980 printf("Image %d is already named \"%s\".\n",
984 printf("Changing the name of image %d to "
985 "\"%s\".\n", image, new_name);
986 ret = wimlib_set_image_name(w, image, new_name);
992 const char *old_desc;
993 old_desc = wimlib_get_image_description(w, image);
994 if (old_desc && strcmp(old_desc, new_desc) == 0) {
995 printf("The description of image %d is already "
996 "\"%s\".\n", image, new_desc);
999 printf("Changing the description of image %d "
1000 "to \"%s\".\n", image, new_desc);
1001 ret = wimlib_set_image_descripton(w, image,
1008 /* Only call wimlib_overwrite_xml_and_header() if something
1009 * actually needs to be changed. */
1010 if (boot || new_name || new_desc ||
1011 check != wimlib_has_integrity_table(w)) {
1013 ret = wimlib_overwrite_xml_and_header(w, check ?
1014 WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
1015 WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
1017 printf("The file `%s' was not modified because nothing "
1018 "needed to be done.\n", wimfile);
1028 /* Join split WIMs into one part WIM */
1029 static int imagex_join(int argc, const char **argv)
1032 int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1033 const char *output_path;
1035 for_opt(c, join_options) {
1038 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1048 imagex_error("Must specify at least one split WIM (.swm) parts "
1052 output_path = argv[0];
1053 return wimlib_join(++argv, --argc, output_path, flags);
1059 /* Mounts an image using a FUSE mount. */
1060 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1063 int mount_flags = 0;
1064 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1065 const char *wimfile;
1072 if (strcmp(argv[0], "mountrw") == 0)
1073 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1074 for_opt(c, mount_options) {
1077 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1080 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1083 if (strcasecmp(optarg, "none") == 0)
1084 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1085 else if (strcasecmp(optarg, "xattr") == 0)
1086 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1087 else if (strcasecmp(optarg, "windows") == 0)
1088 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1090 imagex_error("Unknown stream interface \"%s\"", optarg);
1100 if (argc != 2 && argc != 3)
1105 ret = wimlib_open_wim(wimfile, open_flags, &w);
1111 num_images = wimlib_get_num_images(w);
1112 if (num_images != 1) {
1113 imagex_error("The file `%s' contains %d images; Please "
1114 "select one", wimfile, num_images);
1115 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1117 ret = WIMLIB_ERR_INVALID_IMAGE;
1122 image = wimlib_resolve_image(w, argv[1]);
1126 ret = verify_image_exists_and_is_single(image);
1130 ret = wimlib_mount(w, image, dir, mount_flags);
1132 imagex_error("Failed to mount image %d from `%s' on `%s'",
1133 image, wimfile, dir);
1140 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1145 /* Split a WIM into a spanned set */
1146 static int imagex_split(int argc, const char **argv)
1149 int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1150 unsigned long part_size;
1152 for_opt(c, split_options) {
1155 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1169 part_size = strtoul(argv[2], NULL, 10) * (1 << 20);
1170 return wimlib_split(argv[0], argv[1], part_size, flags);
1173 /* Unmounts an image. */
1174 static int imagex_unmount(int argc, const char **argv)
1177 int unmount_flags = 0;
1180 for_opt(c, unmount_options) {
1183 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1186 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1200 ret = wimlib_unmount(argv[0], unmount_flags);
1202 imagex_error("Failed to unmount `%s'", argv[0]);
1206 struct imagex_command {
1208 int (*func)(int , const char **);
1212 static struct imagex_command imagex_commands[] = {
1213 {"append", imagex_append, APPEND},
1214 {"apply", imagex_apply, APPLY},
1215 {"capture", imagex_capture, CAPTURE},
1216 {"delete", imagex_delete, DELETE},
1217 {"dir", imagex_dir, DIR},
1218 {"export", imagex_export, EXPORT},
1219 {"info", imagex_info, INFO},
1220 {"join", imagex_join, JOIN},
1221 {"mount", imagex_mount_rw_or_ro, MOUNT},
1222 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1223 {"split", imagex_split, SPLIT},
1224 {"unmount", imagex_unmount, UNMOUNT},
1227 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1228 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1230 static void help_or_version(int argc, const char **argv)
1234 struct imagex_command *cmd;
1236 for (i = 1; i < argc; i++) {
1244 if (strcmp(p, "help") == 0 || (*p == '?' && *(p + 1) == '\0')) {
1245 for_imagex_command(cmd) {
1246 if (strcmp(cmd->name, argv[1]) == 0) {
1254 if (strcmp(p, "version") == 0) {
1262 int main(int argc, const char **argv)
1264 struct imagex_command *cmd;
1268 imagex_error("No command specified");
1273 help_or_version(argc, argv);
1277 wimlib_set_print_errors(true);
1279 for_imagex_command(cmd) {
1280 if (strcmp(cmd->name, *argv) == 0) {
1281 ret = cmd->func(argc, argv);
1283 imagex_error("Exiting with error code %d:\n"
1285 wimlib_get_error_string(ret));
1286 if (ret == WIMLIB_ERR_NTFS_3G)
1287 imagex_error_with_errno("errno");
1293 imagex_error("Unrecognized command: `%s'", argv[0]);
1297 /*#ifndef WITH_NTFS_3G*/
1298 /*ERROR("wimlib was not compiled with support for NTFS-3g, so we cannot extract");*/
1299 /*ERROR("a WIM to a NTFS filesystem while preserving NTFS-specific metadata.");*/
1300 /*ERROR("Please apply the WIM to a directory rather than a block device, ");*/
1301 /*ERROR("and without the NTFS flag; or compile in support for NTFS-3g.");*/
1302 /*return WIMLIB_ERR_UNSUPPORTED;*/