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 | NTFS_VOLUME) WIMFILE [\"IMAGE_NAME\"]\n"
84 " [\"DESCRIPTION\"] [--boot] [--check] [--flags EDITIONID]\n"
87 " imagex apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
88 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
89 " [--symlink] [--verbose]\n",
91 " imagex capture (DIRECTORY | NTFS_VOLUME) WIMFILE [\"IMAGE_NAME\"]\n"
92 " [\"DESCRIPTION\"] [--boot] [--check] [--compress[=TYPE]]\n"
93 " [--flags \"EditionID\"] [--verbose] [--dereference]\n",
95 " imagex delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n",
97 " imagex dir WIMFILE (IMAGE_NUM | IMAGE_NAME | \"all\")\n",
99 " imagex export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
100 " DEST_WIMFILE [\"DEST_IMAGE_NAME\"] [\"DEST_IMAGE_DESCRIPTION\"]\n"
101 " [--boot] [--check] [--compress[=TYPE]]\n",
103 " imagex info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
104 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
105 " [--xml] [--extract-xml FILE] [--metadata]\n",
107 " imagex join [--check] WIMFILE SPLIT_WIM...\n",
109 " imagex mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
110 " [--check] [--debug] [--stream-interface=INTERFACE]\n",
112 " imagex mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
113 " [--check] [--debug] [--stream-interface=INTERFACE]\n",
115 " imagex split WIMFILE SPLIT_WIMFILE PART_SIZE [--check]\n",
117 " imagex unmount DIRECTORY [--commit] [--check]\n",
120 static const struct option common_options[] = {
121 {"help", 0, NULL, 'h'},
122 {"version", 0, NULL, 'v'},
126 static const struct option append_options[] = {
127 {"boot", no_argument, NULL, 'b'},
128 {"check", no_argument, NULL, 'c'},
129 {"flags", required_argument, NULL, 'f'},
130 {"dereference", no_argument, NULL, 'L'},
133 static const struct option apply_options[] = {
134 {"check", no_argument, NULL, 'c'},
135 {"hardlink", no_argument, NULL, 'h'},
136 {"symlink", no_argument, NULL, 's'},
137 {"verbose", no_argument, NULL, 'v'},
140 static const struct option capture_options[] = {
141 {"boot", no_argument, NULL, 'b'},
142 {"check", no_argument, NULL, 'c'},
143 {"compress", optional_argument, NULL, 'x'},
144 {"flags", required_argument, NULL, 'f'},
145 {"verbose", no_argument, NULL,'v'},
146 {"dereference", no_argument, NULL, 'L'},
149 static const struct option delete_options[] = {
150 {"check", no_argument, NULL, 'c'},
154 static const struct option export_options[] = {
155 {"boot", no_argument, NULL, 'b'},
156 {"check", no_argument , NULL, 'c'},
157 {"compress", optional_argument, NULL, 'x'},
161 static const struct option info_options[] = {
162 {"boot", no_argument, NULL, 'b'},
163 {"check", no_argument, NULL, 'c'},
164 {"header", no_argument, NULL, 'h'},
165 {"lookup-table", no_argument, NULL, 'l'},
166 {"xml", no_argument, NULL, 'x'},
167 {"extract-xml", required_argument, NULL, 'X'},
168 {"metadata", no_argument, NULL, 'm'},
172 static const struct option join_options[] = {
173 {"check", no_argument, NULL, 'c'},
177 static const struct option mount_options[] = {
178 {"check", no_argument, NULL, 'c'},
179 {"debug", no_argument, NULL, 'd'},
180 {"stream-interface", required_argument, NULL, 's'},
184 static const struct option split_options[] = {
185 {"check", no_argument, NULL, 'c'},
189 static const struct option unmount_options[] = {
190 {"commit", no_argument, NULL, 'c'},
191 {"check", no_argument, NULL, 'C'},
196 /* Print formatted error message to stderr. */
197 static void imagex_error(const char *format, ...)
200 va_start(va, format);
201 fputs("ERROR: ", stderr);
202 vfprintf(stderr, format, va);
207 /* Print formatted error message to stderr. */
208 static void imagex_error_with_errno(const char *format, ...)
210 int errno_save = errno;
212 va_start(va, format);
213 fputs("ERROR: ", stderr);
214 vfprintf(stderr, format, va);
215 fprintf(stderr, ": %s\n", strerror(errno_save));
220 static inline void version()
222 static const char *s =
223 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
224 "Copyright (C) 2012 Eric Biggers\n"
225 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
226 "This is free software: you are free to change and redistribute it.\n"
227 "There is NO WARRANTY, to the extent permitted by law.\n"
229 "Report bugs to "PACKAGE_BUGREPORT".\n";
233 static inline void usage(int cmd)
235 puts("IMAGEX: Usage:");
236 fputs(usage_strings[cmd], stdout);
239 static void usage_all()
241 puts("IMAGEX: Usage:");
242 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
243 fputs(usage_strings[i], stdout);
244 static const char *extra =
246 " imagex --version\n"
248 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
250 fputs(extra, stdout);
253 static int verify_image_exists(int image)
255 if (image == WIM_NO_IMAGE) {
256 imagex_error("Not a valid image");
257 return WIMLIB_ERR_INVALID_IMAGE;
262 static int verify_image_is_single(int image)
264 if (image == WIM_ALL_IMAGES) {
265 imagex_error("Cannot specify all images for this action");
266 return WIMLIB_ERR_INVALID_IMAGE;
271 static int verify_image_exists_and_is_single(int image)
274 ret = verify_image_exists(image);
276 ret = verify_image_is_single(image);
280 static int get_compression_type(const char *optarg)
283 return WIM_COMPRESSION_TYPE_XPRESS;
284 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
285 return WIM_COMPRESSION_TYPE_LZX;
286 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
287 return WIM_COMPRESSION_TYPE_XPRESS;
288 else if (strcasecmp(optarg, "none") == 0)
289 return WIM_COMPRESSION_TYPE_NONE;
291 imagex_error("Invalid compression type `%s'! Must be "
292 "\"maximum\", \"fast\", or \"none\".", optarg);
293 return WIM_COMPRESSION_TYPE_INVALID;
297 static int imagex_append(int argc, const char **argv)
300 const char *flags_element = NULL;
301 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
302 int add_image_flags = 0;
303 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
311 for_opt(c, append_options) {
314 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
317 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
318 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
321 flags_element = optarg;
324 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
333 if (argc < 2 || argc > 4) {
339 name = (argc >= 3) ? argv[2] : path_basename(dir);
340 desc = (argc >= 4) ? argv[3] : NULL;
342 ret = wimlib_open_wim(wimfile, open_flags, &w);
349 ret = stat(dir, &stbuf);
351 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
352 const char *ntfs_device = dir;
353 printf("Capturing WIM image NTFS filesystem on `%s'\n",
355 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
362 if (errno != -ENOENT)
363 imagex_error_with_errno("Failed to stat `%s'", dir);
366 ret = wimlib_add_image(w, dir, name, desc,
367 flags_element, add_image_flags);
372 ret = wimlib_overwrite(w, write_flags);
378 /* Extract one image, or all images, from a WIM file into a directory. */
379 static int imagex_apply(int argc, const char **argv)
382 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
389 const char *image_num_or_name;
390 int extract_flags = 0;
392 for_opt(c, apply_options) {
395 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
398 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
401 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
404 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
413 if (argc != 2 && argc != 3) {
420 image_num_or_name = "1";
423 image_num_or_name = argv[1];
427 ret = wimlib_open_wim(wimfile, open_flags, &w);
431 image = wimlib_resolve_image(w, image_num_or_name);
432 ret = verify_image_exists(image);
436 num_images = wimlib_get_num_images(w);
437 if (argc == 2 && num_images != 1) {
438 imagex_error("`%s' contains %d images; Please select one "
439 "(or all)", wimfile, num_images);
448 ret = stat(dir, &stbuf);
450 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
451 const char *ntfs_device = dir;
452 printf("Applying image %d of `%s' to NTFS filesystem on `%s'\n",
453 image, wimfile, ntfs_device);
454 ret = wimlib_apply_image_to_ntfs_volume(w, image,
460 if (errno != -ENOENT)
461 imagex_error_with_errno("Failed to stat `%s'", dir);
465 ret = wimlib_extract_image(w, image, dir, extract_flags);
472 /* Create a WIM file from a directory. */
473 static int imagex_capture(int argc, const char **argv)
476 int add_image_flags = 0;
477 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
478 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
479 const char *flags_element = NULL;
487 for_opt(c, capture_options) {
490 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
493 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
496 compression_type = get_compression_type(optarg);
497 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
501 flags_element = optarg;
504 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
505 write_flags |= WIMLIB_WRITE_FLAG_VERBOSE;
508 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
518 if (argc < 2 || argc > 4) {
524 name = (argc >= 3) ? argv[2] : dir;
525 desc = (argc >= 4) ? argv[3] : NULL;
527 ret = wimlib_create_new_wim(compression_type, &w);
534 ret = stat(dir, &stbuf);
536 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
537 const char *ntfs_device = dir;
538 printf("Capturing WIM image NTFS filesystem on `%s'\n",
540 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
547 if (errno != -ENOENT)
548 imagex_error_with_errno("Failed to stat `%s'", dir);
551 ret = wimlib_add_image(w, dir, name, desc, flags_element,
556 imagex_error("Failed to add the image `%s'", dir);
560 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
562 imagex_error("Failed to write the WIM file `%s'", wimfile);
568 /* Remove image(s) from a WIM. */
569 static int imagex_delete(int argc, const char **argv)
572 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
573 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
575 const char *image_num_or_name;
580 for_opt(c, delete_options) {
583 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
584 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
596 imagex_error("Must specify a WIM file");
598 imagex_error("Must specify an image");
603 image_num_or_name = argv[1];
605 ret = wimlib_open_wim(wimfile, open_flags, &w);
609 image = wimlib_resolve_image(w, image_num_or_name);
611 ret = verify_image_exists(image);
615 ret = wimlib_delete_image(w, image);
617 imagex_error("Failed to delete image from `%s'", wimfile);
621 ret = wimlib_overwrite(w, write_flags);
623 imagex_error("Failed to write the file `%s' with image "
631 /* Print the files contained in an image(s) in a WIM file. */
632 static int imagex_dir(int argc, const char **argv)
642 imagex_error("Must specify a WIM file");
647 imagex_error("Too many arguments");
653 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
657 part_number = wimlib_get_part_number(w, NULL);
658 if (part_number != 1) {
659 imagex_error("`%s' is part %d of a split WIM! Specify the "
660 "first part to see the files",
661 wimfile, part_number);
662 ret = WIMLIB_ERR_SPLIT_UNSUPPORTED;
667 image = wimlib_resolve_image(w, argv[2]);
668 ret = verify_image_exists(image);
672 /* Image was not specified. If the WIM only contains one image,
673 * choose that one; otherwise, print an error. */
674 num_images = wimlib_get_num_images(w);
675 if (num_images != 1) {
676 imagex_error("The file `%s' contains %d images; Please "
677 "select one.", wimfile, num_images);
685 ret = wimlib_print_files(w, image);
691 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
693 static int imagex_export(int argc, const char **argv)
696 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
697 int export_flags = 0;
698 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
699 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
700 bool compression_type_specified = false;
701 const char *src_wimfile;
702 const char *src_image_num_or_name;
703 const char *dest_wimfile;
704 const char *dest_name;
705 const char *dest_desc;
706 WIMStruct *src_w = NULL;
707 WIMStruct *dest_w = NULL;
713 for_opt(c, export_options) {
716 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
719 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
720 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
723 compression_type = get_compression_type(optarg);
724 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
726 compression_type_specified = true;
735 if (argc < 3 || argc > 5) {
739 src_wimfile = argv[0];
740 src_image_num_or_name = argv[1];
741 dest_wimfile = argv[2];
742 dest_name = (argc >= 4) ? argv[3] : NULL;
743 dest_desc = (argc >= 5) ? argv[4] : NULL;
744 ret = wimlib_open_wim(src_wimfile, open_flags, &src_w);
748 /* Determine if the destination is an existing file or not.
749 * If so, we try to append the exported image(s) to it; otherwise, we
750 * create a new WIM containing the exported image(s). */
751 if (stat(dest_wimfile, &stbuf) == 0) {
753 /* Destination file exists. */
754 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
755 imagex_error("`%s' is not a regular file",
759 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
763 if (compression_type_specified && compression_type !=
764 wimlib_get_compression_type(dest_w)) {
765 imagex_error("Cannot specify a compression type that is "
766 "not the same as that used in the "
771 compression_type = wimlib_get_compression_type(dest_w);
774 /* dest_wimfile is not an existing file, so create a new WIM. */
775 if (errno == ENOENT) {
776 ret = wimlib_create_new_wim(compression_type, &dest_w);
780 imagex_error_with_errno("Cannot stat file `%s'",
786 image = wimlib_resolve_image(src_w, src_image_num_or_name);
787 ret = verify_image_exists(image);
791 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
798 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
801 ret = wimlib_overwrite(dest_w, write_flags);
808 /* Prints information about a WIM file; also can mark an image as bootable,
809 * change the name of an image, or change the description of an image. */
810 static int imagex_info(int argc, const char **argv)
816 bool lookup_table = false;
818 bool metadata = false;
819 bool short_header = true;
820 const char *xml_out_file = NULL;
822 const char *image_num_or_name = "all";
823 const char *new_name = NULL;
824 const char *new_desc = NULL;
829 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
830 WIMLIB_OPEN_FLAG_SPLIT_OK;
834 for_opt(c, info_options) {
844 short_header = false;
848 short_header = false;
852 short_header = false;
855 xml_out_file = optarg;
856 short_header = false;
860 short_header = false;
870 if (argc == 0 || argc > 4) {
876 image_num_or_name = argv[1];
886 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
888 ret = wimlib_open_wim(wimfile, open_flags, &w);
892 part_number = wimlib_get_part_number(w, &total_parts);
894 /*if (total_parts > 1 && part_number > 1) {*/
895 /*printf("Warning: this is part %d of a %d-part split WIM.\n"*/
896 /*" Select the first part if you want to see information\n"*/
897 /*" about images in the WIM.\n", */
898 /*part_number, total_parts);*/
901 image = wimlib_resolve_image(w, image_num_or_name);
902 if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
903 imagex_error("The image `%s' does not exist",
906 imagex_error("If you would like to set the boot "
907 "index to 0, specify image \"0\" with "
909 ret = WIMLIB_ERR_INVALID_IMAGE;
913 if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
915 imagex_error("Cannot specify the --boot flag "
916 "without specifying a specific "
917 "image in a multi-image WIM");
918 ret = WIMLIB_ERR_INVALID_IMAGE;
922 imagex_error("Cannot specify the NEW_NAME "
923 "without specifying a specific "
924 "image in a multi-image WIM");
925 ret = WIMLIB_ERR_INVALID_IMAGE;
930 /* Operations that print information are separated from operations that
931 * recreate the WIM file. */
932 if (!new_name && !boot) {
934 if (image == WIM_NO_IMAGE) {
935 imagex_error("`%s' is not a valid image",
937 ret = WIMLIB_ERR_INVALID_IMAGE;
941 if (image == WIM_ALL_IMAGES && short_header)
942 wimlib_print_wim_information(w);
945 wimlib_print_header(w);
948 if (total_parts != 1) {
949 printf("Warning: Only showing the lookup table "
950 "for part %d of a %d-part WIM.\n",
951 part_number, total_parts);
953 wimlib_print_lookup_table(w);
957 ret = wimlib_extract_xml_data(w, stdout);
963 fp = fopen(xml_out_file, "wb");
965 imagex_error_with_errno("Failed to open the "
971 ret = wimlib_extract_xml_data(w, fp);
972 if (fclose(fp) != 0) {
973 imagex_error("Failed to close the file `%s'",
983 wimlib_print_available_images(w, image);
986 if (total_parts != 1 && part_number != 1) {
987 imagex_error("Select part 1 of this %d-part WIM "
988 "to see the image metadata",
990 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
992 ret = wimlib_print_metadata(w, image);
997 if (total_parts != 1) {
998 imagex_error("Modifying a split WIM is not supported.");
1001 if (image == WIM_ALL_IMAGES)
1004 if (image == WIM_NO_IMAGE && new_name) {
1005 imagex_error("Cannot specify new_name (`%s') when "
1006 "using image 0", new_name);
1011 if (image == wimlib_get_boot_idx(w)) {
1012 printf("Image %d is already marked as "
1013 "bootable.\n", image);
1016 printf("Marking image %d as bootable.\n",
1018 wimlib_set_boot_idx(w, image);
1022 if (strcmp(wimlib_get_image_name(w, image),
1024 printf("Image %d is already named \"%s\".\n",
1028 printf("Changing the name of image %d to "
1029 "\"%s\".\n", image, new_name);
1030 ret = wimlib_set_image_name(w, image, new_name);
1036 const char *old_desc;
1037 old_desc = wimlib_get_image_description(w, image);
1038 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1039 printf("The description of image %d is already "
1040 "\"%s\".\n", image, new_desc);
1043 printf("Changing the description of image %d "
1044 "to \"%s\".\n", image, new_desc);
1045 ret = wimlib_set_image_descripton(w, image,
1052 /* Only call wimlib_overwrite_xml_and_header() if something
1053 * actually needs to be changed. */
1054 if (boot || new_name || new_desc ||
1055 check != wimlib_has_integrity_table(w)) {
1057 ret = wimlib_overwrite_xml_and_header(w, check ?
1058 WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
1059 WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
1061 printf("The file `%s' was not modified because nothing "
1062 "needed to be done.\n", wimfile);
1072 /* Join split WIMs into one part WIM */
1073 static int imagex_join(int argc, const char **argv)
1076 int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1077 const char *output_path;
1079 for_opt(c, join_options) {
1082 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1092 imagex_error("Must specify at least one split WIM (.swm) parts "
1096 output_path = argv[0];
1097 return wimlib_join(++argv, --argc, output_path, flags);
1103 /* Mounts an image using a FUSE mount. */
1104 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1107 int mount_flags = 0;
1108 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1109 const char *wimfile;
1116 if (strcmp(argv[0], "mountrw") == 0)
1117 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1118 for_opt(c, mount_options) {
1121 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1124 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1127 if (strcasecmp(optarg, "none") == 0)
1128 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1129 else if (strcasecmp(optarg, "xattr") == 0)
1130 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1131 else if (strcasecmp(optarg, "windows") == 0)
1132 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1134 imagex_error("Unknown stream interface \"%s\"", optarg);
1144 if (argc != 2 && argc != 3)
1149 ret = wimlib_open_wim(wimfile, open_flags, &w);
1155 num_images = wimlib_get_num_images(w);
1156 if (num_images != 1) {
1157 imagex_error("The file `%s' contains %d images; Please "
1158 "select one", wimfile, num_images);
1159 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1161 ret = WIMLIB_ERR_INVALID_IMAGE;
1166 image = wimlib_resolve_image(w, argv[1]);
1170 ret = verify_image_exists_and_is_single(image);
1174 ret = wimlib_mount(w, image, dir, mount_flags);
1176 imagex_error("Failed to mount image %d from `%s' on `%s'",
1177 image, wimfile, dir);
1184 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1189 /* Split a WIM into a spanned set */
1190 static int imagex_split(int argc, const char **argv)
1193 int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1194 unsigned long part_size;
1196 for_opt(c, split_options) {
1199 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1213 part_size = strtoul(argv[2], NULL, 10) * (1 << 20);
1214 return wimlib_split(argv[0], argv[1], part_size, flags);
1217 /* Unmounts an image. */
1218 static int imagex_unmount(int argc, const char **argv)
1221 int unmount_flags = 0;
1224 for_opt(c, unmount_options) {
1227 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1230 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1244 ret = wimlib_unmount(argv[0], unmount_flags);
1246 imagex_error("Failed to unmount `%s'", argv[0]);
1250 struct imagex_command {
1252 int (*func)(int , const char **);
1256 static struct imagex_command imagex_commands[] = {
1257 {"append", imagex_append, APPEND},
1258 {"apply", imagex_apply, APPLY},
1259 {"capture", imagex_capture, CAPTURE},
1260 {"delete", imagex_delete, DELETE},
1261 {"dir", imagex_dir, DIR},
1262 {"export", imagex_export, EXPORT},
1263 {"info", imagex_info, INFO},
1264 {"join", imagex_join, JOIN},
1265 {"mount", imagex_mount_rw_or_ro, MOUNT},
1266 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1267 {"split", imagex_split, SPLIT},
1268 {"unmount", imagex_unmount, UNMOUNT},
1271 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1272 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1274 static void help_or_version(int argc, const char **argv)
1278 struct imagex_command *cmd;
1280 for (i = 1; i < argc; i++) {
1288 if (strcmp(p, "help") == 0 || (*p == '?' && *(p + 1) == '\0')) {
1289 for_imagex_command(cmd) {
1290 if (strcmp(cmd->name, argv[1]) == 0) {
1298 if (strcmp(p, "version") == 0) {
1306 int main(int argc, const char **argv)
1308 struct imagex_command *cmd;
1312 imagex_error("No command specified");
1317 help_or_version(argc, argv);
1321 wimlib_set_print_errors(true);
1323 for_imagex_command(cmd) {
1324 if (strcmp(cmd->name, *argv) == 0) {
1325 ret = cmd->func(argc, argv);
1327 imagex_error("Exiting with error code %d:\n"
1329 wimlib_get_error_string(ret));
1330 if (ret == WIMLIB_ERR_NTFS_3G)
1331 imagex_error_with_errno("errno");
1337 imagex_error("Unrecognized command: `%s'", argv[0]);
1341 /*#ifndef WITH_NTFS_3G*/
1342 /*ERROR("wimlib was not compiled with support for NTFS-3g, so we cannot extract");*/
1343 /*ERROR("a WIM to a NTFS filesystem while preserving NTFS-specific metadata.");*/
1344 /*ERROR("Please apply the WIM to a directory rather than a block device, ");*/
1345 /*ERROR("and without the NTFS flag; or compile in support for NTFS-3g.");*/
1346 /*return WIMLIB_ERR_UNSUPPORTED;*/