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 "
704 compression_type = wimlib_get_compression_type(dest_w);
707 /* dest_wimfile is not an existing file, so create a new WIM. */
708 if (errno == ENOENT) {
709 ret = wimlib_create_new_wim(compression_type, &dest_w);
713 imagex_error_with_errno("Cannot stat file `%s'",
719 image = wimlib_resolve_image(src_w, src_image_num_or_name);
720 ret = verify_image_exists(image);
724 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
731 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
734 ret = wimlib_overwrite(dest_w, write_flags);
741 /* Prints information about a WIM file; also can mark an image as bootable,
742 * change the name of an image, or change the description of an image. */
743 static int imagex_info(int argc, const char **argv)
749 bool lookup_table = false;
751 bool metadata = false;
752 bool short_header = true;
753 const char *xml_out_file = NULL;
755 const char *image_num_or_name = "all";
756 const char *new_name = NULL;
757 const char *new_desc = NULL;
762 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
763 WIMLIB_OPEN_FLAG_SPLIT_OK;
767 for_opt(c, info_options) {
777 short_header = false;
781 short_header = false;
785 short_header = false;
788 xml_out_file = optarg;
789 short_header = false;
793 short_header = false;
803 if (argc == 0 || argc > 4) {
809 image_num_or_name = argv[1];
819 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
821 ret = wimlib_open_wim(wimfile, open_flags, &w);
825 part_number = wimlib_get_part_number(w, &total_parts);
827 /*if (total_parts > 1 && part_number > 1) {*/
828 /*printf("Warning: this is part %d of a %d-part split WIM.\n"*/
829 /*" Select the first part if you want to see information\n"*/
830 /*" about images in the WIM.\n", */
831 /*part_number, total_parts);*/
834 image = wimlib_resolve_image(w, image_num_or_name);
835 if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
836 imagex_error("The image `%s' does not exist",
839 imagex_error("If you would like to set the boot "
840 "index to 0, specify image \"0\" with "
842 ret = WIMLIB_ERR_INVALID_IMAGE;
846 if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
848 imagex_error("Cannot specify the --boot flag "
849 "without specifying a specific "
850 "image in a multi-image WIM");
851 ret = WIMLIB_ERR_INVALID_IMAGE;
855 imagex_error("Cannot specify the NEW_NAME "
856 "without specifying a specific "
857 "image in a multi-image WIM");
858 ret = WIMLIB_ERR_INVALID_IMAGE;
863 /* Operations that print information are separated from operations that
864 * recreate the WIM file. */
865 if (!new_name && !boot) {
867 if (image == WIM_NO_IMAGE) {
868 imagex_error("`%s' is not a valid image",
870 ret = WIMLIB_ERR_INVALID_IMAGE;
874 if (image == WIM_ALL_IMAGES && short_header)
875 wimlib_print_wim_information(w);
878 wimlib_print_header(w);
881 if (total_parts != 1) {
882 printf("Warning: Only showing the lookup table "
883 "for part %d of a %d-part WIM.\n",
884 part_number, total_parts);
886 wimlib_print_lookup_table(w);
890 ret = wimlib_extract_xml_data(w, stdout);
896 fp = fopen(xml_out_file, "wb");
898 imagex_error_with_errno("Failed to open the "
904 ret = wimlib_extract_xml_data(w, fp);
905 if (fclose(fp) != 0) {
906 imagex_error("Failed to close the file `%s'",
916 wimlib_print_available_images(w, image);
919 if (total_parts != 1 && part_number != 1) {
920 imagex_error("Select part 1 of this %d-part WIM "
921 "to see the image metadata",
923 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
925 ret = wimlib_print_metadata(w, image);
930 if (total_parts != 1) {
931 imagex_error("Modifying a split WIM is not supported.");
934 if (image == WIM_ALL_IMAGES)
937 if (image == WIM_NO_IMAGE && new_name) {
938 imagex_error("Cannot specify new_name (`%s') when "
939 "using image 0", new_name);
944 if (image == wimlib_get_boot_idx(w)) {
945 printf("Image %d is already marked as "
946 "bootable.\n", image);
949 printf("Marking image %d as bootable.\n",
951 wimlib_set_boot_idx(w, image);
955 if (strcmp(wimlib_get_image_name(w, image),
957 printf("Image %d is already named \"%s\".\n",
961 printf("Changing the name of image %d to "
962 "\"%s\".\n", image, new_name);
963 ret = wimlib_set_image_name(w, image, new_name);
969 const char *old_desc;
970 old_desc = wimlib_get_image_description(w, image);
971 if (old_desc && strcmp(old_desc, new_desc) == 0) {
972 printf("The description of image %d is already "
973 "\"%s\".\n", image, new_desc);
976 printf("Changing the description of image %d "
977 "to \"%s\".\n", image, new_desc);
978 ret = wimlib_set_image_descripton(w, image,
985 /* Only call wimlib_overwrite_xml_and_header() if something
986 * actually needs to be changed. */
987 if (boot || new_name || new_desc ||
988 check != wimlib_has_integrity_table(w)) {
990 ret = wimlib_overwrite_xml_and_header(w, check ?
991 WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
992 WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
994 printf("The file `%s' was not modified because nothing "
995 "needed to be done.\n", wimfile);
1005 /* Join split WIMs into one part WIM */
1006 static int imagex_join(int argc, const char **argv)
1009 int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1010 const char *output_path;
1012 for_opt(c, join_options) {
1015 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1025 imagex_error("Must specify at least one split WIM (.swm) parts "
1029 output_path = argv[0];
1030 return wimlib_join(++argv, --argc, output_path, flags);
1036 /* Mounts an image using a FUSE mount. */
1037 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1040 int mount_flags = 0;
1041 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1042 const char *wimfile;
1049 if (strcmp(argv[0], "mountrw") == 0)
1050 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1051 for_opt(c, mount_options) {
1054 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1057 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1060 if (strcasecmp(optarg, "none") == 0)
1061 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1062 else if (strcasecmp(optarg, "xattr") == 0)
1063 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1064 else if (strcasecmp(optarg, "windows") == 0)
1065 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1067 imagex_error("Unknown stream interface \"%s\"", optarg);
1077 if (argc != 2 && argc != 3)
1082 ret = wimlib_open_wim(wimfile, open_flags, &w);
1088 num_images = wimlib_get_num_images(w);
1089 if (num_images != 1) {
1090 imagex_error("The file `%s' contains %d images; Please "
1091 "select one", wimfile, num_images);
1092 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1094 ret = WIMLIB_ERR_INVALID_IMAGE;
1099 image = wimlib_resolve_image(w, argv[1]);
1103 ret = verify_image_exists_and_is_single(image);
1107 ret = wimlib_mount(w, image, dir, mount_flags);
1109 imagex_error("Failed to mount image %d from `%s' on `%s'",
1110 image, wimfile, dir);
1117 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1122 /* Split a WIM into a spanned set */
1123 static int imagex_split(int argc, const char **argv)
1126 int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1127 unsigned long part_size;
1129 for_opt(c, split_options) {
1132 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1146 part_size = strtoul(argv[2], NULL, 10) * (1 << 20);
1147 return wimlib_split(argv[0], argv[1], part_size, flags);
1150 /* Unmounts an image. */
1151 static int imagex_unmount(int argc, const char **argv)
1154 int unmount_flags = 0;
1157 for_opt(c, unmount_options) {
1160 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1163 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1177 ret = wimlib_unmount(argv[0], unmount_flags);
1179 imagex_error("Failed to unmount `%s'", argv[0]);
1183 struct imagex_command {
1185 int (*func)(int , const char **);
1189 static struct imagex_command imagex_commands[] = {
1190 {"append", imagex_append, APPEND},
1191 {"apply", imagex_apply, APPLY},
1192 {"capture", imagex_capture, CAPTURE},
1193 {"delete", imagex_delete, DELETE},
1194 {"dir", imagex_dir, DIR},
1195 {"export", imagex_export, EXPORT},
1196 {"info", imagex_info, INFO},
1197 {"join", imagex_join, JOIN},
1198 {"mount", imagex_mount_rw_or_ro, MOUNT},
1199 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1200 {"split", imagex_split, SPLIT},
1201 {"unmount", imagex_unmount, UNMOUNT},
1204 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1205 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1207 static void help_or_version(int argc, const char **argv)
1211 struct imagex_command *cmd;
1213 for (i = 1; i < argc; i++) {
1221 if (strcmp(p, "help") == 0 || (*p == '?' && *(p + 1) == '\0')) {
1222 for_imagex_command(cmd) {
1223 if (strcmp(cmd->name, argv[1]) == 0) {
1231 if (strcmp(p, "version") == 0) {
1239 int main(int argc, const char **argv)
1241 struct imagex_command *cmd;
1245 imagex_error("No command specified");
1250 help_or_version(argc, argv);
1254 wimlib_set_print_errors(true);
1256 for_imagex_command(cmd) {
1257 if (strcmp(cmd->name, *argv) == 0) {
1258 ret = cmd->func(argc, argv);
1260 imagex_error("Exiting with error code %d:\n"
1262 wimlib_get_error_string(ret));
1268 imagex_error("Unrecognized command: `%s'", argv[0]);
1272 /*#ifndef WITH_NTFS_3G*/
1273 /*ERROR("wimlib was not compiled with support for NTFS-3g, so we cannot extract");*/
1274 /*ERROR("a WIM to a NTFS filesystem while preserving NTFS-specific metadata.");*/
1275 /*ERROR("Please apply the WIM to a directory rather than a block device, ");*/
1276 /*ERROR("and without the NTFS flag; or compile in support for NTFS-3g.");*/
1277 /*return WIMLIB_ERR_UNSUPPORTED;*/