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_XPRESS;
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_XPRESS;
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;
481 write_flags |= WIMLIB_WRITE_FLAG_VERBOSE;
484 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
487 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
497 if (argc < 2 || argc > 4) {
503 name = (argc >= 3) ? argv[2] : dir;
504 desc = (argc >= 4) ? argv[3] : NULL;
506 ret = wimlib_create_new_wim(compression_type, &w);
510 ret = wimlib_add_image(w, dir, name, desc, flags_element,
513 imagex_error("Failed to add the image `%s'", dir);
517 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
519 imagex_error("Failed to write the WIM file `%s'", wimfile);
525 /* Remove image(s) from a WIM. */
526 static int imagex_delete(int argc, const char **argv)
529 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
530 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
532 const char *image_num_or_name;
537 for_opt(c, delete_options) {
540 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
541 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
553 imagex_error("Must specify a WIM file");
555 imagex_error("Must specify an image");
560 image_num_or_name = argv[1];
562 ret = wimlib_open_wim(wimfile, open_flags, &w);
566 image = wimlib_resolve_image(w, image_num_or_name);
568 ret = verify_image_exists(image);
572 ret = wimlib_delete_image(w, image);
574 imagex_error("Failed to delete image from `%s'", wimfile);
578 ret = wimlib_overwrite(w, write_flags);
580 imagex_error("Failed to write the file `%s' with image "
588 /* Print the files contained in an image(s) in a WIM file. */
589 static int imagex_dir(int argc, const char **argv)
599 imagex_error("Must specify a WIM file");
604 imagex_error("Too many arguments");
610 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
614 part_number = wimlib_get_part_number(w, NULL);
615 if (part_number != 1) {
616 imagex_error("`%s' is part %d of a split WIM! Specify the "
617 "first part to see the files",
618 wimfile, part_number);
619 ret = WIMLIB_ERR_SPLIT_UNSUPPORTED;
624 image = wimlib_resolve_image(w, argv[2]);
625 ret = verify_image_exists(image);
629 /* Image was not specified. If the WIM only contains one image,
630 * choose that one; otherwise, print an error. */
631 num_images = wimlib_get_num_images(w);
632 if (num_images != 1) {
633 imagex_error("The file `%s' contains %d images; Please "
634 "select one.", wimfile, num_images);
642 ret = wimlib_print_files(w, image);
648 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
650 static int imagex_export(int argc, const char **argv)
653 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
654 int export_flags = 0;
655 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
656 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
657 bool compression_type_specified = false;
658 const char *src_wimfile;
659 const char *src_image_num_or_name;
660 const char *dest_wimfile;
661 const char *dest_name;
662 const char *dest_desc;
663 WIMStruct *src_w = NULL;
664 WIMStruct *dest_w = NULL;
670 for_opt(c, export_options) {
673 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
676 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
677 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
680 compression_type = get_compression_type(optarg);
681 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
683 compression_type_specified = true;
692 if (argc < 3 || argc > 5) {
696 src_wimfile = argv[0];
697 src_image_num_or_name = argv[1];
698 dest_wimfile = argv[2];
699 dest_name = (argc >= 4) ? argv[3] : NULL;
700 dest_desc = (argc >= 5) ? argv[4] : NULL;
701 ret = wimlib_open_wim(src_wimfile, open_flags, &src_w);
705 /* Determine if the destination is an existing file or not.
706 * If so, we try to append the exported image(s) to it; otherwise, we
707 * create a new WIM containing the exported image(s). */
708 if (stat(dest_wimfile, &stbuf) == 0) {
710 /* Destination file exists. */
711 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
712 imagex_error("`%s' is not a regular file",
716 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
720 if (compression_type_specified && compression_type !=
721 wimlib_get_compression_type(dest_w)) {
722 imagex_error("Cannot specify a compression type that is "
723 "not the same as that used in the "
728 compression_type = wimlib_get_compression_type(dest_w);
731 /* dest_wimfile is not an existing file, so create a new WIM. */
732 if (errno == ENOENT) {
733 ret = wimlib_create_new_wim(compression_type, &dest_w);
737 imagex_error_with_errno("Cannot stat file `%s'",
743 image = wimlib_resolve_image(src_w, src_image_num_or_name);
744 ret = verify_image_exists(image);
748 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
755 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
758 ret = wimlib_overwrite(dest_w, write_flags);
765 /* Prints information about a WIM file; also can mark an image as bootable,
766 * change the name of an image, or change the description of an image. */
767 static int imagex_info(int argc, const char **argv)
773 bool lookup_table = false;
775 bool metadata = false;
776 bool short_header = true;
777 const char *xml_out_file = NULL;
779 const char *image_num_or_name = "all";
780 const char *new_name = NULL;
781 const char *new_desc = NULL;
786 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
787 WIMLIB_OPEN_FLAG_SPLIT_OK;
791 for_opt(c, info_options) {
801 short_header = false;
805 short_header = false;
809 short_header = false;
812 xml_out_file = optarg;
813 short_header = false;
817 short_header = false;
827 if (argc == 0 || argc > 4) {
833 image_num_or_name = argv[1];
843 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
845 ret = wimlib_open_wim(wimfile, open_flags, &w);
849 part_number = wimlib_get_part_number(w, &total_parts);
851 /*if (total_parts > 1 && part_number > 1) {*/
852 /*printf("Warning: this is part %d of a %d-part split WIM.\n"*/
853 /*" Select the first part if you want to see information\n"*/
854 /*" about images in the WIM.\n", */
855 /*part_number, total_parts);*/
858 image = wimlib_resolve_image(w, image_num_or_name);
859 if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
860 imagex_error("The image `%s' does not exist",
863 imagex_error("If you would like to set the boot "
864 "index to 0, specify image \"0\" with "
866 ret = WIMLIB_ERR_INVALID_IMAGE;
870 if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
872 imagex_error("Cannot specify the --boot flag "
873 "without specifying a specific "
874 "image in a multi-image WIM");
875 ret = WIMLIB_ERR_INVALID_IMAGE;
879 imagex_error("Cannot specify the NEW_NAME "
880 "without specifying a specific "
881 "image in a multi-image WIM");
882 ret = WIMLIB_ERR_INVALID_IMAGE;
887 /* Operations that print information are separated from operations that
888 * recreate the WIM file. */
889 if (!new_name && !boot) {
891 if (image == WIM_NO_IMAGE) {
892 imagex_error("`%s' is not a valid image",
894 ret = WIMLIB_ERR_INVALID_IMAGE;
898 if (image == WIM_ALL_IMAGES && short_header)
899 wimlib_print_wim_information(w);
902 wimlib_print_header(w);
905 if (total_parts != 1) {
906 printf("Warning: Only showing the lookup table "
907 "for part %d of a %d-part WIM.\n",
908 part_number, total_parts);
910 wimlib_print_lookup_table(w);
914 ret = wimlib_extract_xml_data(w, stdout);
920 fp = fopen(xml_out_file, "wb");
922 imagex_error_with_errno("Failed to open the "
928 ret = wimlib_extract_xml_data(w, fp);
929 if (fclose(fp) != 0) {
930 imagex_error("Failed to close the file `%s'",
940 wimlib_print_available_images(w, image);
943 if (total_parts != 1 && part_number != 1) {
944 imagex_error("Select part 1 of this %d-part WIM "
945 "to see the image metadata",
947 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
949 ret = wimlib_print_metadata(w, image);
954 if (total_parts != 1) {
955 imagex_error("Modifying a split WIM is not supported.");
958 if (image == WIM_ALL_IMAGES)
961 if (image == WIM_NO_IMAGE && new_name) {
962 imagex_error("Cannot specify new_name (`%s') when "
963 "using image 0", new_name);
968 if (image == wimlib_get_boot_idx(w)) {
969 printf("Image %d is already marked as "
970 "bootable.\n", image);
973 printf("Marking image %d as bootable.\n",
975 wimlib_set_boot_idx(w, image);
979 if (strcmp(wimlib_get_image_name(w, image),
981 printf("Image %d is already named \"%s\".\n",
985 printf("Changing the name of image %d to "
986 "\"%s\".\n", image, new_name);
987 ret = wimlib_set_image_name(w, image, new_name);
993 const char *old_desc;
994 old_desc = wimlib_get_image_description(w, image);
995 if (old_desc && strcmp(old_desc, new_desc) == 0) {
996 printf("The description of image %d is already "
997 "\"%s\".\n", image, new_desc);
1000 printf("Changing the description of image %d "
1001 "to \"%s\".\n", image, new_desc);
1002 ret = wimlib_set_image_descripton(w, image,
1009 /* Only call wimlib_overwrite_xml_and_header() if something
1010 * actually needs to be changed. */
1011 if (boot || new_name || new_desc ||
1012 check != wimlib_has_integrity_table(w)) {
1014 ret = wimlib_overwrite_xml_and_header(w, check ?
1015 WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
1016 WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
1018 printf("The file `%s' was not modified because nothing "
1019 "needed to be done.\n", wimfile);
1029 /* Join split WIMs into one part WIM */
1030 static int imagex_join(int argc, const char **argv)
1033 int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1034 const char *output_path;
1036 for_opt(c, join_options) {
1039 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1049 imagex_error("Must specify at least one split WIM (.swm) parts "
1053 output_path = argv[0];
1054 return wimlib_join(++argv, --argc, output_path, flags);
1060 /* Mounts an image using a FUSE mount. */
1061 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1064 int mount_flags = 0;
1065 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1066 const char *wimfile;
1073 if (strcmp(argv[0], "mountrw") == 0)
1074 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1075 for_opt(c, mount_options) {
1078 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1081 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1084 if (strcasecmp(optarg, "none") == 0)
1085 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1086 else if (strcasecmp(optarg, "xattr") == 0)
1087 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1088 else if (strcasecmp(optarg, "windows") == 0)
1089 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1091 imagex_error("Unknown stream interface \"%s\"", optarg);
1101 if (argc != 2 && argc != 3)
1106 ret = wimlib_open_wim(wimfile, open_flags, &w);
1112 num_images = wimlib_get_num_images(w);
1113 if (num_images != 1) {
1114 imagex_error("The file `%s' contains %d images; Please "
1115 "select one", wimfile, num_images);
1116 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1118 ret = WIMLIB_ERR_INVALID_IMAGE;
1123 image = wimlib_resolve_image(w, argv[1]);
1127 ret = verify_image_exists_and_is_single(image);
1131 ret = wimlib_mount(w, image, dir, mount_flags);
1133 imagex_error("Failed to mount image %d from `%s' on `%s'",
1134 image, wimfile, dir);
1141 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1146 /* Split a WIM into a spanned set */
1147 static int imagex_split(int argc, const char **argv)
1150 int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1151 unsigned long part_size;
1153 for_opt(c, split_options) {
1156 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1170 part_size = strtoul(argv[2], NULL, 10) * (1 << 20);
1171 return wimlib_split(argv[0], argv[1], part_size, flags);
1174 /* Unmounts an image. */
1175 static int imagex_unmount(int argc, const char **argv)
1178 int unmount_flags = 0;
1181 for_opt(c, unmount_options) {
1184 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1187 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1201 ret = wimlib_unmount(argv[0], unmount_flags);
1203 imagex_error("Failed to unmount `%s'", argv[0]);
1207 struct imagex_command {
1209 int (*func)(int , const char **);
1213 static struct imagex_command imagex_commands[] = {
1214 {"append", imagex_append, APPEND},
1215 {"apply", imagex_apply, APPLY},
1216 {"capture", imagex_capture, CAPTURE},
1217 {"delete", imagex_delete, DELETE},
1218 {"dir", imagex_dir, DIR},
1219 {"export", imagex_export, EXPORT},
1220 {"info", imagex_info, INFO},
1221 {"join", imagex_join, JOIN},
1222 {"mount", imagex_mount_rw_or_ro, MOUNT},
1223 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1224 {"split", imagex_split, SPLIT},
1225 {"unmount", imagex_unmount, UNMOUNT},
1228 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1229 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1231 static void help_or_version(int argc, const char **argv)
1235 struct imagex_command *cmd;
1237 for (i = 1; i < argc; i++) {
1245 if (strcmp(p, "help") == 0 || (*p == '?' && *(p + 1) == '\0')) {
1246 for_imagex_command(cmd) {
1247 if (strcmp(cmd->name, argv[1]) == 0) {
1255 if (strcmp(p, "version") == 0) {
1263 int main(int argc, const char **argv)
1265 struct imagex_command *cmd;
1269 imagex_error("No command specified");
1274 help_or_version(argc, argv);
1278 wimlib_set_print_errors(true);
1280 for_imagex_command(cmd) {
1281 if (strcmp(cmd->name, *argv) == 0) {
1282 ret = cmd->func(argc, argv);
1284 imagex_error("Exiting with error code %d:\n"
1286 wimlib_get_error_string(ret));
1287 if (ret == WIMLIB_ERR_NTFS_3G)
1288 imagex_error_with_errno("errno");
1294 imagex_error("Unrecognized command: `%s'", argv[0]);
1298 /*#ifndef WITH_NTFS_3G*/
1299 /*ERROR("wimlib was not compiled with support for NTFS-3g, so we cannot extract");*/
1300 /*ERROR("a WIM to a NTFS filesystem while preserving NTFS-specific metadata.");*/
1301 /*ERROR("Please apply the WIM to a directory rather than a block device, ");*/
1302 /*ERROR("and without the NTFS flag; or compile in support for NTFS-3g.");*/
1303 /*return WIMLIB_ERR_UNSUPPORTED;*/