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/>.
34 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
36 #define swap(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
39 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (char**)argv, "", \
57 static const char *path_basename(const char *path)
64 /* Trailing slashes. */
65 while ((p != path - 1) && *p == '/')
68 while ((p != path - 1) && *p != '/')
75 static const char *usage_strings[] = {
77 " imagex append DIRECTORY WIMFILE [\"IMAGE_NAME\"] [\"DESCRIPTION\"] [--boot]\n"
78 " [--check] [--flags EDITIONID] [--dereference]\n",
80 " imagex apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all] DIRECTORY [--check]\n"
81 " [--hardlink] [--symlink] [--verbose]\n",
83 " imagex capture DIRECTORY WIMFILE [\"IMAGE_NAME\"] [\"DESCRIPTION\"]\n"
84 " l [--boot] [--check] [--compress[=TYPE]]\n"
85 " [--flags \"EditionID\"] [--verbose] [--dereference]\n",
87 " imagex delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n",
89 " imagex dir WIMFILE (IMAGE_NUM | IMAGE_NAME | \"all\")\n",
91 " imagex export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
92 " DEST_WIMFILE [\"DEST_IMAGE_NAME\"] [\"DEST_IMAGE_DESCRIPTION\"]\n"
93 " [--boot] [--check] [--compress[=TYPE]]\n",
95 " imagex info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
96 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
97 " [--xml] [--extract-xml FILE] [--metadata]\n",
99 " imagex join [--check] WIMFILE SPLIT_WIM...\n",
101 " imagex mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
102 " [--check] [--debug] [--stream-interface=INTERFACE]\n",
104 " imagex mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
105 " [--check] [--debug] [--stream-interface=INTERFACE]\n",
107 " imagex split WIMFILE SPLIT_WIMFILE PART_SIZE [--check]\n",
109 " imagex unmount DIRECTORY [--commit] [--check]\n",
112 static const struct option common_options[] = {
113 {"help", 0, NULL, 'h'},
114 {"version", 0, NULL, 'v'},
118 static const struct option append_options[] = {
119 {"boot", no_argument, NULL, 'b'},
120 {"check", no_argument, NULL, 'c'},
121 {"flags", required_argument, NULL, 'f'},
122 {"dereference", no_argument, NULL, 'L'},
125 static const struct option apply_options[] = {
126 {"check", no_argument, NULL, 'c'},
127 {"hardlink", no_argument, NULL, 'h'},
128 {"symlink", no_argument, NULL, 's'},
129 {"verbose", no_argument, NULL, 'v'},
130 {"ntfs", no_argument, NULL, 'N'},
133 static const struct option capture_options[] = {
134 {"boot", no_argument, NULL, 'b'},
135 {"check", no_argument, NULL, 'c'},
136 {"compress", optional_argument, NULL, 'x'},
137 {"flags", required_argument, NULL, 'f'},
138 {"verbose", no_argument, NULL,'v'},
139 {"ntfs", no_argument, NULL, 'N'},
140 {"dereference", no_argument, NULL, 'L'},
143 static const struct option delete_options[] = {
144 {"check", no_argument, NULL, 'c'},
148 static const struct option export_options[] = {
149 {"boot", no_argument, NULL, 'b'},
150 {"check", no_argument , NULL, 'c'},
151 {"compress", optional_argument, NULL, 'x'},
155 static const struct option info_options[] = {
156 {"boot", no_argument, NULL, 'b'},
157 {"check", no_argument, NULL, 'c'},
158 {"header", no_argument, NULL, 'h'},
159 {"lookup-table", no_argument, NULL, 'l'},
160 {"xml", no_argument, NULL, 'x'},
161 {"extract-xml", required_argument, NULL, 'X'},
162 {"metadata", no_argument, NULL, 'm'},
166 static const struct option join_options[] = {
167 {"check", no_argument, NULL, 'c'},
171 static const struct option mount_options[] = {
172 {"check", no_argument, NULL, 'c'},
173 {"debug", no_argument, NULL, 'd'},
174 {"stream-interface", required_argument, NULL, 's'},
178 static const struct option split_options[] = {
179 {"check", no_argument, NULL, 'c'},
183 static const struct option unmount_options[] = {
184 {"commit", no_argument, NULL, 'c'},
185 {"check", no_argument, NULL, 'C'},
190 /* Print formatted error message to stderr. */
191 static void imagex_error(const char *format, ...)
194 va_start(va, format);
195 fputs("ERROR: ", stderr);
196 vfprintf(stderr, format, va);
201 /* Print formatted error message to stderr. */
202 static void imagex_error_with_errno(const char *format, ...)
204 int errno_save = errno;
206 va_start(va, format);
207 fputs("ERROR: ", stderr);
208 vfprintf(stderr, format, va);
209 fprintf(stderr, ": %s\n", strerror(errno_save));
214 static inline void version()
216 static const char *s =
217 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
218 "Copyright (C) 2012 Eric Biggers\n"
219 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
220 "This is free software: you are free to change and redistribute it.\n"
221 "There is NO WARRANTY, to the extent permitted by law.\n"
223 "Report bugs to "PACKAGE_BUGREPORT".\n";
227 static inline void usage(int cmd)
229 puts("IMAGEX: Usage:");
230 fputs(usage_strings[cmd], stdout);
233 static void usage_all()
235 puts("IMAGEX: Usage:");
236 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
237 fputs(usage_strings[i], stdout);
238 static const char *extra =
240 " imagex --version\n"
242 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
244 fputs(extra, stdout);
247 static int verify_image_exists(int image)
249 if (image == WIM_NO_IMAGE) {
250 imagex_error("Not a valid image");
251 return WIMLIB_ERR_INVALID_IMAGE;
256 static int verify_image_is_single(int image)
258 if (image == WIM_ALL_IMAGES) {
259 imagex_error("Cannot specify all images for this action");
260 return WIMLIB_ERR_INVALID_IMAGE;
265 static int verify_image_exists_and_is_single(int image)
268 ret = verify_image_exists(image);
270 ret = verify_image_is_single(image);
274 static int get_compression_type(const char *optarg)
277 return WIM_COMPRESSION_TYPE_LZX;
278 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
279 return WIM_COMPRESSION_TYPE_LZX;
280 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
281 return WIM_COMPRESSION_TYPE_XPRESS;
282 else if (strcasecmp(optarg, "none") == 0)
283 return WIM_COMPRESSION_TYPE_NONE;
285 imagex_error("Invalid compression type `%s'! Must be "
286 "\"maximum\", \"fast\", or \"none\".", optarg);
287 return WIM_COMPRESSION_TYPE_INVALID;
291 static int imagex_append(int argc, const char **argv)
294 const char *flags_element = NULL;
295 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
296 int add_image_flags = 0;
297 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
305 for_opt(c, append_options) {
308 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
311 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
312 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
315 flags_element = optarg;
318 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
327 if (argc < 2 || argc > 4) {
333 name = (argc >= 3) ? argv[2] : path_basename(dir);
334 desc = (argc >= 4) ? argv[3] : NULL;
336 ret = wimlib_open_wim(wimfile, open_flags, &w);
340 ret = wimlib_add_image(w, dir, name, desc,
341 flags_element, add_image_flags);
344 ret = wimlib_overwrite(w, write_flags);
350 /* Extract one image, or all images, from a WIM file into a directory. */
351 static int imagex_apply(int argc, const char **argv)
354 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
361 const char *image_num_or_name;
362 int extract_flags = 0;
364 for_opt(c, apply_options) {
367 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
370 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
373 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
376 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
379 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
388 if (argc != 2 && argc != 3) {
395 image_num_or_name = "1";
398 image_num_or_name = argv[1];
402 ret = wimlib_open_wim(wimfile, open_flags, &w);
406 image = wimlib_resolve_image(w, image_num_or_name);
407 ret = verify_image_exists(image);
411 num_images = wimlib_get_num_images(w);
412 if (argc == 2 && num_images != 1) {
413 imagex_error("`%s' contains %d images; Please select one "
414 "(or all)", wimfile, num_images);
419 ret = wimlib_extract_image(w, image, dir, extract_flags);
426 /* Create a WIM file from a directory. */
427 static int imagex_capture(int argc, const char **argv)
430 int add_image_flags = 0;
431 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
432 int compression_type = WIM_COMPRESSION_TYPE_NONE;
433 const char *flags_element = NULL;
441 for_opt(c, capture_options) {
444 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
447 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
450 compression_type = get_compression_type(optarg);
451 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
455 flags_element = optarg;
458 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
461 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
464 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
474 if (argc < 2 || argc > 4) {
480 name = (argc >= 3) ? argv[2] : dir;
481 desc = (argc >= 4) ? argv[3] : NULL;
483 ret = wimlib_create_new_wim(compression_type, &w);
487 ret = wimlib_add_image(w, dir, name, desc, flags_element,
490 imagex_error("Failed to add the image `%s'", dir);
494 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
496 imagex_error("Failed to write the WIM file `%s'", wimfile);
502 /* Remove image(s) from a WIM. */
503 static int imagex_delete(int argc, const char **argv)
506 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
507 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
509 const char *image_num_or_name;
514 for_opt(c, delete_options) {
517 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
518 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
530 imagex_error("Must specify a WIM file");
532 imagex_error("Must specify an image");
537 image_num_or_name = argv[1];
539 ret = wimlib_open_wim(wimfile, open_flags, &w);
543 image = wimlib_resolve_image(w, image_num_or_name);
545 ret = verify_image_exists(image);
549 ret = wimlib_delete_image(w, image);
551 imagex_error("Failed to delete image from `%s'", wimfile);
555 ret = wimlib_overwrite(w, write_flags);
557 imagex_error("Failed to write the file `%s' with image "
565 /* Print the files contained in an image(s) in a WIM file. */
566 static int imagex_dir(int argc, const char **argv)
576 imagex_error("Must specify a WIM file");
581 imagex_error("Too many arguments");
587 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
591 part_number = wimlib_get_part_number(w, NULL);
592 if (part_number != 1) {
593 imagex_error("`%s' is part %d of a split WIM! Specify the "
594 "first part to see the files",
595 wimfile, part_number);
596 ret = WIMLIB_ERR_SPLIT_UNSUPPORTED;
601 image = wimlib_resolve_image(w, argv[2]);
602 ret = verify_image_exists(image);
606 /* Image was not specified. If the WIM only contains one image,
607 * choose that one; otherwise, print an error. */
608 num_images = wimlib_get_num_images(w);
609 if (num_images != 1) {
610 imagex_error("The file `%s' contains %d images; Please "
611 "select one.", wimfile, num_images);
619 ret = wimlib_print_files(w, image);
625 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
627 static int imagex_export(int argc, const char **argv)
630 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
631 int export_flags = 0;
632 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
633 int compression_type = WIM_COMPRESSION_TYPE_NONE;
634 bool compression_type_specified = false;
635 const char *src_wimfile;
636 const char *src_image_num_or_name;
637 const char *dest_wimfile;
638 const char *dest_name;
639 const char *dest_desc;
640 WIMStruct *src_w = NULL;
641 WIMStruct *dest_w = NULL;
647 for_opt(c, export_options) {
650 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
653 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
654 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
657 compression_type = get_compression_type(optarg);
658 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
660 compression_type_specified = true;
669 if (argc < 3 || argc > 5) {
673 src_wimfile = argv[0];
674 src_image_num_or_name = argv[1];
675 dest_wimfile = argv[2];
676 dest_name = (argc >= 4) ? argv[3] : NULL;
677 dest_desc = (argc >= 5) ? argv[4] : NULL;
678 ret = wimlib_open_wim(src_wimfile, open_flags, &src_w);
682 /* Determine if the destination is an existing file or not.
683 * If so, we try to append the exported image(s) to it; otherwise, we
684 * create a new WIM containing the exported image(s). */
685 if (stat(dest_wimfile, &stbuf) == 0) {
687 /* Destination file exists. */
688 if (!S_ISREG(stbuf.st_mode)) {
689 imagex_error("`%s' is not a regular file",
693 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
697 if (compression_type_specified && compression_type !=
698 wimlib_get_compression_type(dest_w)) {
699 imagex_error("Cannot specify a compression type that is "
700 "not the same as that used in the "
705 compression_type = wimlib_get_compression_type(dest_w);
708 /* dest_wimfile is not an existing file, so create a new WIM. */
709 if (errno == ENOENT) {
710 ret = wimlib_create_new_wim(compression_type, &dest_w);
714 imagex_error_with_errno("Cannot stat file `%s'",
720 image = wimlib_resolve_image(src_w, src_image_num_or_name);
721 ret = verify_image_exists(image);
725 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
732 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
735 ret = wimlib_overwrite(dest_w, write_flags);
742 /* Prints information about a WIM file; also can mark an image as bootable,
743 * change the name of an image, or change the description of an image. */
744 static int imagex_info(int argc, const char **argv)
750 bool lookup_table = false;
752 bool metadata = false;
753 bool short_header = true;
754 const char *xml_out_file = NULL;
756 const char *image_num_or_name = "all";
757 const char *new_name = NULL;
758 const char *new_desc = NULL;
763 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
764 WIMLIB_OPEN_FLAG_SPLIT_OK;
768 for_opt(c, info_options) {
778 short_header = false;
782 short_header = false;
786 short_header = false;
789 xml_out_file = optarg;
790 short_header = false;
794 short_header = false;
804 if (argc == 0 || argc > 4) {
810 image_num_or_name = argv[1];
820 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
822 ret = wimlib_open_wim(wimfile, open_flags, &w);
826 part_number = wimlib_get_part_number(w, &total_parts);
828 /*if (total_parts > 1 && part_number > 1) {*/
829 /*printf("Warning: this is part %d of a %d-part split WIM.\n"*/
830 /*" Select the first part if you want to see information\n"*/
831 /*" about images in the WIM.\n", */
832 /*part_number, total_parts);*/
835 image = wimlib_resolve_image(w, image_num_or_name);
836 if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
837 imagex_error("The image `%s' does not exist",
840 imagex_error("If you would like to set the boot "
841 "index to 0, specify image \"0\" with "
843 ret = WIMLIB_ERR_INVALID_IMAGE;
847 if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
849 imagex_error("Cannot specify the --boot flag "
850 "without specifying a specific "
851 "image in a multi-image WIM");
852 ret = WIMLIB_ERR_INVALID_IMAGE;
856 imagex_error("Cannot specify the NEW_NAME "
857 "without specifying a specific "
858 "image in a multi-image WIM");
859 ret = WIMLIB_ERR_INVALID_IMAGE;
864 /* Operations that print information are separated from operations that
865 * recreate the WIM file. */
866 if (!new_name && !boot) {
868 if (image == WIM_NO_IMAGE) {
869 imagex_error("`%s' is not a valid image",
871 ret = WIMLIB_ERR_INVALID_IMAGE;
875 if (image == WIM_ALL_IMAGES && short_header)
876 wimlib_print_wim_information(w);
879 wimlib_print_header(w);
882 if (total_parts != 1) {
883 printf("Warning: Only showing the lookup table "
884 "for part %d of a %d-part WIM.\n",
885 part_number, total_parts);
887 wimlib_print_lookup_table(w);
891 ret = wimlib_extract_xml_data(w, stdout);
897 fp = fopen(xml_out_file, "wb");
899 imagex_error_with_errno("Failed to open the "
905 ret = wimlib_extract_xml_data(w, fp);
906 if (fclose(fp) != 0) {
907 imagex_error("Failed to close the file `%s'",
917 wimlib_print_available_images(w, image);
920 if (total_parts != 1 && part_number != 1) {
921 imagex_error("Select part 1 of this %d-part WIM "
922 "to see the image metadata",
924 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
926 ret = wimlib_print_metadata(w, image);
931 if (total_parts != 1) {
932 imagex_error("Modifying a split WIM is not supported.");
935 if (image == WIM_ALL_IMAGES)
938 if (image == WIM_NO_IMAGE && new_name) {
939 imagex_error("Cannot specify new_name (`%s') when "
940 "using image 0", new_name);
945 if (image == wimlib_get_boot_idx(w)) {
946 printf("Image %d is already marked as "
947 "bootable.\n", image);
950 printf("Marking image %d as bootable.\n",
952 wimlib_set_boot_idx(w, image);
956 if (strcmp(wimlib_get_image_name(w, image),
958 printf("Image %d is already named \"%s\".\n",
962 printf("Changing the name of image %d to "
963 "\"%s\".\n", image, new_name);
964 ret = wimlib_set_image_name(w, image, new_name);
970 const char *old_desc;
971 old_desc = wimlib_get_image_description(w, image);
972 if (old_desc && strcmp(old_desc, new_desc) == 0) {
973 printf("The description of image %d is already "
974 "\"%s\".\n", image, new_desc);
977 printf("Changing the description of image %d "
978 "to \"%s\".\n", image, new_desc);
979 ret = wimlib_set_image_descripton(w, image,
986 /* Only call wimlib_overwrite_xml_and_header() if something
987 * actually needs to be changed. */
988 if (boot || new_name || new_desc ||
989 check != wimlib_has_integrity_table(w)) {
991 ret = wimlib_overwrite_xml_and_header(w, check ?
992 WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
993 WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
995 printf("The file `%s' was not modified because nothing "
996 "needed to be done.\n", wimfile);
1006 /* Join split WIMs into one part WIM */
1007 static int imagex_join(int argc, const char **argv)
1010 int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1011 const char *output_path;
1013 for_opt(c, join_options) {
1016 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1026 imagex_error("Must specify at least one split WIM (.swm) parts "
1030 output_path = argv[0];
1031 return wimlib_join(++argv, --argc, output_path, flags);
1037 /* Mounts an image using a FUSE mount. */
1038 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1041 int mount_flags = 0;
1042 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1043 const char *wimfile;
1050 if (strcmp(argv[0], "mountrw") == 0)
1051 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1052 for_opt(c, mount_options) {
1055 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1058 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1061 if (strcasecmp(optarg, "none") == 0)
1062 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1063 else if (strcasecmp(optarg, "xattr") == 0)
1064 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1065 else if (strcasecmp(optarg, "windows") == 0)
1066 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1068 imagex_error("Unknown stream interface \"%s\"", optarg);
1078 if (argc != 2 && argc != 3)
1083 ret = wimlib_open_wim(wimfile, open_flags, &w);
1089 num_images = wimlib_get_num_images(w);
1090 if (num_images != 1) {
1091 imagex_error("The file `%s' contains %d images; Please "
1092 "select one", wimfile, num_images);
1093 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1095 ret = WIMLIB_ERR_INVALID_IMAGE;
1100 image = wimlib_resolve_image(w, argv[1]);
1104 ret = verify_image_exists_and_is_single(image);
1108 ret = wimlib_mount(w, image, dir, mount_flags);
1110 imagex_error("Failed to mount image %d from `%s' on `%s'",
1111 image, wimfile, dir);
1118 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1123 /* Split a WIM into a spanned set */
1124 static int imagex_split(int argc, const char **argv)
1127 int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1128 unsigned long part_size;
1130 for_opt(c, split_options) {
1133 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1147 part_size = strtoul(argv[2], NULL, 10) * (1 << 20);
1148 return wimlib_split(argv[0], argv[1], part_size, flags);
1151 /* Unmounts an image. */
1152 static int imagex_unmount(int argc, const char **argv)
1155 int unmount_flags = 0;
1158 for_opt(c, unmount_options) {
1161 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1164 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1178 ret = wimlib_unmount(argv[0], unmount_flags);
1180 imagex_error("Failed to unmount `%s'", argv[0]);
1184 struct imagex_command {
1186 int (*func)(int , const char **);
1190 static struct imagex_command imagex_commands[] = {
1191 {"append", imagex_append, APPEND},
1192 {"apply", imagex_apply, APPLY},
1193 {"capture", imagex_capture, CAPTURE},
1194 {"delete", imagex_delete, DELETE},
1195 {"dir", imagex_dir, DIR},
1196 {"export", imagex_export, EXPORT},
1197 {"info", imagex_info, INFO},
1198 {"join", imagex_join, JOIN},
1199 {"mount", imagex_mount_rw_or_ro, MOUNT},
1200 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1201 {"split", imagex_split, SPLIT},
1202 {"unmount", imagex_unmount, UNMOUNT},
1205 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1206 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1208 static void help_or_version(int argc, const char **argv)
1212 struct imagex_command *cmd;
1214 for (i = 1; i < argc; i++) {
1222 if (strcmp(p, "help") == 0 || (*p == '?' && *(p + 1) == '\0')) {
1223 for_imagex_command(cmd) {
1224 if (strcmp(cmd->name, argv[1]) == 0) {
1232 if (strcmp(p, "version") == 0) {
1240 int main(int argc, const char **argv)
1242 struct imagex_command *cmd;
1246 imagex_error("No command specified");
1251 help_or_version(argc, argv);
1255 wimlib_set_print_errors(true);
1257 for_imagex_command(cmd) {
1258 if (strcmp(cmd->name, *argv) == 0) {
1259 ret = cmd->func(argc, argv);
1261 imagex_error("Exiting with error code %d:\n"
1263 wimlib_get_error_string(ret));
1269 imagex_error("Unrecognized command: `%s'", argv[0]);
1273 /*#ifndef WITH_NTFS_3G*/
1274 /*ERROR("wimlib was not compiled with support for NTFS-3g, so we cannot extract");*/
1275 /*ERROR("a WIM to a NTFS filesystem while preserving NTFS-specific metadata.");*/
1276 /*ERROR("Please apply the WIM to a directory rather than a block device, ");*/
1277 /*ERROR("and without the NTFS flag; or compile in support for NTFS-3g.");*/
1278 /*return WIMLIB_ERR_UNSUPPORTED;*/