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/>.
35 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
37 #define swap(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
40 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (char**)argv, "", \
59 static const char *path_basename(const char *path)
66 /* Trailing slashes. */
67 while ((p != path - 1) && *p == '/')
70 while ((p != path - 1) && *p != '/')
77 static const char *usage_strings[] = {
79 " imagex append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
80 " [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
81 " [--verbose] [--dereference] [--config=FILE]\n",
83 " imagex apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
84 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
85 " [--symlink] [--verbose]\n",
87 " imagex capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
88 " [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
89 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
92 " imagex delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n",
94 " imagex dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n",
96 " imagex export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
97 " DEST_WIMFILE [DEST_IMAGE_NAME]\n"
98 " [DEST_IMAGE_DESCRIPTION] [--boot] [--check]\n"
99 " [--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] [--streams-interface=INTERFACE]\n",
110 " imagex mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
111 " [--check] [--debug] [--streams-interface=INTERFACE]\n",
113 " imagex split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--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 {"config", required_argument, NULL, 'C'},
128 {"dereference", no_argument, NULL, 'L'},
129 {"flags", required_argument, NULL, 'f'},
130 {"verbose", no_argument, NULL, 'v'},
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'},
138 {"ref", required_argument, NULL, 'r'},
141 static const struct option capture_options[] = {
142 {"boot", no_argument, NULL, 'b'},
143 {"check", no_argument, NULL, 'c'},
144 {"compress", required_argument, NULL, 'x'},
145 {"config", required_argument, NULL, 'C'},
146 {"dereference", no_argument, NULL, 'L'},
147 {"flags", required_argument, NULL, 'f'},
148 {"verbose", no_argument, NULL, 'v'},
151 static const struct option delete_options[] = {
152 {"check", no_argument, NULL, 'c'},
156 static const struct option export_options[] = {
157 {"boot", no_argument, NULL, 'b'},
158 {"check", no_argument, NULL, 'c'},
159 {"compress", required_argument, NULL, 'x'},
163 static const struct option info_options[] = {
164 {"boot", no_argument, NULL, 'b'},
165 {"check", no_argument, NULL, 'c'},
166 {"extract-xml", required_argument, NULL, 'X'},
167 {"header", no_argument, NULL, 'h'},
168 {"lookup-table", no_argument, NULL, 'l'},
169 {"metadata", no_argument, NULL, 'm'},
170 {"xml", no_argument, NULL, 'x'},
174 static const struct option join_options[] = {
175 {"check", no_argument, NULL, 'c'},
179 static const struct option mount_options[] = {
180 {"check", no_argument, NULL, 'c'},
181 {"debug", no_argument, NULL, 'd'},
182 {"streams-interface", required_argument, NULL, 's'},
186 static const struct option split_options[] = {
187 {"check", no_argument, NULL, 'c'},
191 static const struct option unmount_options[] = {
192 {"commit", no_argument, NULL, 'c'},
193 {"check", no_argument, NULL, 'C'},
198 /* Print formatted error message to stderr. */
199 static void imagex_error(const char *format, ...)
202 va_start(va, format);
203 fputs("ERROR: ", stderr);
204 vfprintf(stderr, format, va);
209 /* Print formatted error message to stderr. */
210 static void imagex_error_with_errno(const char *format, ...)
212 int errno_save = errno;
214 va_start(va, format);
215 fputs("ERROR: ", stderr);
216 vfprintf(stderr, format, va);
217 fprintf(stderr, ": %s\n", strerror(errno_save));
222 static inline void version()
224 static const char *s =
225 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
226 "Copyright (C) 2012 Eric Biggers\n"
227 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
228 "This is free software: you are free to change and redistribute it.\n"
229 "There is NO WARRANTY, to the extent permitted by law.\n"
231 "Report bugs to "PACKAGE_BUGREPORT".\n";
235 static inline void usage(int cmd)
237 puts("IMAGEX: Usage:");
238 fputs(usage_strings[cmd], stdout);
241 static void usage_all()
243 puts("IMAGEX: Usage:");
244 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
245 fputs(usage_strings[i], stdout);
246 static const char *extra =
248 " imagex --version\n"
250 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
252 fputs(extra, stdout);
255 static int verify_image_exists(int image)
257 if (image == WIM_NO_IMAGE) {
258 imagex_error("Not a valid image");
259 return WIMLIB_ERR_INVALID_IMAGE;
264 static int verify_image_is_single(int image)
266 if (image == WIM_ALL_IMAGES) {
267 imagex_error("Cannot specify all images for this action");
268 return WIMLIB_ERR_INVALID_IMAGE;
273 static int verify_image_exists_and_is_single(int image)
276 ret = verify_image_exists(image);
278 ret = verify_image_is_single(image);
282 static int get_compression_type(const char *optarg)
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 const char *file_get_contents(const char *filename, size_t *len_ret)
304 if (stat(filename, &stbuf) != 0) {
305 imagex_error_with_errno("Failed to stat the file `%s'", filename);
310 fp = fopen(filename, "rb");
312 imagex_error_with_errno("Failed to open the file `%s'", filename);
318 imagex_error("Failed to allocate buffer of %zu bytes to hold "
319 "contents of file `%s'", len, filename);
322 if (fread(buf, 1, len, fp) != len) {
323 imagex_error_with_errno("Failed to read %lu bytes from the "
324 "file `%s'", len, filename);
336 static int imagex_append(int argc, const char **argv)
339 const char *flags_element = NULL;
340 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
341 int add_image_flags = 0;
342 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
347 const char *config_file = NULL;
348 const char *config_str = NULL;
349 size_t config_len = 0;
354 for_opt(c, append_options) {
357 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
360 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
361 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
364 config_file = optarg;
367 flags_element = optarg;
370 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
373 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
382 if (argc < 2 || argc > 4) {
388 name = (argc >= 3) ? argv[2] : path_basename(dir);
389 desc = (argc >= 4) ? argv[3] : NULL;
392 config_str = file_get_contents(config_file, &config_len);
397 ret = wimlib_open_wim(wimfile, open_flags, &w);
404 ret = stat(dir, &stbuf);
406 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
407 const char *ntfs_device = dir;
408 printf("Capturing WIM image NTFS filesystem on `%s'\n",
410 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
419 imagex_error_with_errno("Failed to stat `%s'", dir);
422 ret = wimlib_add_image(w, dir, name, config_str, config_len,
428 cur_image = wimlib_get_num_images(w);
430 ret = wimlib_set_image_descripton(w, cur_image, desc);
435 ret = wimlib_set_image_flags(w, cur_image, flags_element);
439 ret = wimlib_overwrite(w, write_flags);
445 /* Extract one image, or all images, from a WIM file into a directory. */
446 static int imagex_apply(int argc, const char **argv)
449 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
450 WIMLIB_OPEN_FLAG_SPLIT_OK;
457 const char *image_num_or_name;
458 int extract_flags = 0;
460 const char *swm_glob = NULL;
461 WIMStruct **additional_swms = NULL;
462 size_t num_additional_swms = 0;
465 for_opt(c, apply_options) {
468 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
471 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
474 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
477 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
489 if (argc != 2 && argc != 3) {
496 image_num_or_name = "1";
499 image_num_or_name = argv[1];
503 ret = wimlib_open_wim(wimfile, open_flags, &w);
507 image = wimlib_resolve_image(w, image_num_or_name);
508 ret = verify_image_exists(image);
512 num_images = wimlib_get_num_images(w);
513 if (argc == 2 && num_images != 1) {
514 imagex_error("`%s' contains %d images; Please select one "
515 "(or all)", wimfile, num_images);
522 ret = glob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
524 imagex_error_with_errno("Failed to process glob "
529 num_additional_swms = globbuf.gl_pathc;
530 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
531 if (!additional_swms) {
532 imagex_error("Out of memory");
537 for (size_t i = 0; i < num_additional_swms; i++) {
538 if (strcmp(globbuf.gl_pathv[i], wimfile) == 0) {
542 ret = wimlib_open_wim(globbuf.gl_pathv[i],
543 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
544 &additional_swms[i - offset]);
548 num_additional_swms -= offset;
554 ret = stat(dir, &stbuf);
556 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
557 const char *ntfs_device = dir;
558 printf("Applying image %d of `%s' to NTFS filesystem on `%s'\n",
559 image, wimfile, ntfs_device);
560 ret = wimlib_apply_image_to_ntfs_volume(w, image,
564 num_additional_swms);
569 imagex_error_with_errno("Failed to stat `%s'", dir);
573 ret = wimlib_extract_image(w, image, dir, extract_flags,
574 additional_swms, num_additional_swms);
578 for (size_t i = 0; i < num_additional_swms; i++)
579 wimlib_free(additional_swms[i]);
584 /* Create a WIM file from a directory. */
585 static int imagex_capture(int argc, const char **argv)
588 int add_image_flags = 0;
589 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
590 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
591 const char *flags_element = NULL;
596 const char *config_file = NULL;
597 const char *config_str = NULL;
598 size_t config_len = 0;
603 for_opt(c, capture_options) {
606 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
609 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
612 config_file = optarg;
615 compression_type = get_compression_type(optarg);
616 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
620 flags_element = optarg;
623 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
624 write_flags |= WIMLIB_WRITE_FLAG_VERBOSE;
627 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
637 if (argc < 2 || argc > 4) {
643 name = (argc >= 3) ? argv[2] : dir;
644 desc = (argc >= 4) ? argv[3] : NULL;
647 config_str = file_get_contents(config_file, &config_len);
652 ret = wimlib_create_new_wim(compression_type, &w);
659 ret = stat(dir, &stbuf);
661 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
662 const char *ntfs_device = dir;
663 printf("Capturing WIM image NTFS filesystem on `%s'\n",
665 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
674 imagex_error_with_errno("Failed to stat `%s'", dir);
677 ret = wimlib_add_image(w, dir, name, config_str,
678 config_len, add_image_flags);
682 imagex_error("Failed to add the image `%s'", dir);
685 cur_image = wimlib_get_num_images(w);
687 ret = wimlib_set_image_descripton(w, cur_image, desc);
692 ret = wimlib_set_image_flags(w, cur_image, flags_element);
697 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
699 imagex_error("Failed to write the WIM file `%s'", wimfile);
705 /* Remove image(s) from a WIM. */
706 static int imagex_delete(int argc, const char **argv)
709 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
710 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
712 const char *image_num_or_name;
717 for_opt(c, delete_options) {
720 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
721 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
733 imagex_error("Must specify a WIM file");
735 imagex_error("Must specify an image");
740 image_num_or_name = argv[1];
742 ret = wimlib_open_wim(wimfile, open_flags, &w);
746 image = wimlib_resolve_image(w, image_num_or_name);
748 ret = verify_image_exists(image);
752 ret = wimlib_delete_image(w, image);
754 imagex_error("Failed to delete image from `%s'", wimfile);
758 ret = wimlib_overwrite(w, write_flags);
760 imagex_error("Failed to write the file `%s' with image "
768 /* Print the files contained in an image(s) in a WIM file. */
769 static int imagex_dir(int argc, const char **argv)
779 imagex_error("Must specify a WIM file");
784 imagex_error("Too many arguments");
790 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
794 part_number = wimlib_get_part_number(w, NULL);
795 if (part_number != 1) {
796 imagex_error("`%s' is part %d of a split WIM! Specify the "
797 "first part to see the files",
798 wimfile, part_number);
799 ret = WIMLIB_ERR_SPLIT_UNSUPPORTED;
804 image = wimlib_resolve_image(w, argv[2]);
805 ret = verify_image_exists(image);
809 /* Image was not specified. If the WIM only contains one image,
810 * choose that one; otherwise, print an error. */
811 num_images = wimlib_get_num_images(w);
812 if (num_images != 1) {
813 imagex_error("The file `%s' contains %d images; Please "
814 "select one.", wimfile, num_images);
822 ret = wimlib_print_files(w, image);
828 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
830 static int imagex_export(int argc, const char **argv)
833 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
834 int export_flags = 0;
835 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
836 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
837 bool compression_type_specified = false;
838 const char *src_wimfile;
839 const char *src_image_num_or_name;
840 const char *dest_wimfile;
841 const char *dest_name;
842 const char *dest_desc;
843 WIMStruct *src_w = NULL;
844 WIMStruct *dest_w = NULL;
850 for_opt(c, export_options) {
853 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
856 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
857 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
860 compression_type = get_compression_type(optarg);
861 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
863 compression_type_specified = true;
872 if (argc < 3 || argc > 5) {
876 src_wimfile = argv[0];
877 src_image_num_or_name = argv[1];
878 dest_wimfile = argv[2];
879 dest_name = (argc >= 4) ? argv[3] : NULL;
880 dest_desc = (argc >= 5) ? argv[4] : NULL;
881 ret = wimlib_open_wim(src_wimfile, open_flags, &src_w);
885 /* Determine if the destination is an existing file or not.
886 * If so, we try to append the exported image(s) to it; otherwise, we
887 * create a new WIM containing the exported image(s). */
888 if (stat(dest_wimfile, &stbuf) == 0) {
890 /* Destination file exists. */
891 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
892 imagex_error("`%s' is not a regular file",
896 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
900 if (compression_type_specified && compression_type !=
901 wimlib_get_compression_type(dest_w)) {
902 imagex_error("Cannot specify a compression type that is "
903 "not the same as that used in the "
908 compression_type = wimlib_get_compression_type(dest_w);
911 /* dest_wimfile is not an existing file, so create a new WIM. */
912 if (errno == ENOENT) {
913 ret = wimlib_create_new_wim(compression_type, &dest_w);
917 imagex_error_with_errno("Cannot stat file `%s'",
923 image = wimlib_resolve_image(src_w, src_image_num_or_name);
924 ret = verify_image_exists(image);
928 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
935 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
938 ret = wimlib_overwrite(dest_w, write_flags);
945 /* Prints information about a WIM file; also can mark an image as bootable,
946 * change the name of an image, or change the description of an image. */
947 static int imagex_info(int argc, const char **argv)
953 bool lookup_table = false;
955 bool metadata = false;
956 bool short_header = true;
957 const char *xml_out_file = NULL;
959 const char *image_num_or_name = "all";
960 const char *new_name = NULL;
961 const char *new_desc = NULL;
966 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
967 WIMLIB_OPEN_FLAG_SPLIT_OK;
971 for_opt(c, info_options) {
981 short_header = false;
985 short_header = false;
989 short_header = false;
992 xml_out_file = optarg;
993 short_header = false;
997 short_header = false;
1007 if (argc == 0 || argc > 4) {
1013 image_num_or_name = argv[1];
1023 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1025 ret = wimlib_open_wim(wimfile, open_flags, &w);
1029 part_number = wimlib_get_part_number(w, &total_parts);
1031 /*if (total_parts > 1 && part_number > 1) {*/
1032 /*printf("Warning: this is part %d of a %d-part split WIM.\n"*/
1033 /*" Select the first part if you want to see information\n"*/
1034 /*" about images in the WIM.\n", */
1035 /*part_number, total_parts);*/
1038 image = wimlib_resolve_image(w, image_num_or_name);
1039 if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
1040 imagex_error("The image `%s' does not exist",
1043 imagex_error("If you would like to set the boot "
1044 "index to 0, specify image \"0\" with "
1045 "the --boot flag.");
1046 ret = WIMLIB_ERR_INVALID_IMAGE;
1050 if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
1052 imagex_error("Cannot specify the --boot flag "
1053 "without specifying a specific "
1054 "image in a multi-image WIM");
1055 ret = WIMLIB_ERR_INVALID_IMAGE;
1059 imagex_error("Cannot specify the NEW_NAME "
1060 "without specifying a specific "
1061 "image in a multi-image WIM");
1062 ret = WIMLIB_ERR_INVALID_IMAGE;
1067 /* Operations that print information are separated from operations that
1068 * recreate the WIM file. */
1069 if (!new_name && !boot) {
1071 if (image == WIM_NO_IMAGE) {
1072 imagex_error("`%s' is not a valid image",
1074 ret = WIMLIB_ERR_INVALID_IMAGE;
1078 if (image == WIM_ALL_IMAGES && short_header)
1079 wimlib_print_wim_information(w);
1082 wimlib_print_header(w);
1085 if (total_parts != 1) {
1086 printf("Warning: Only showing the lookup table "
1087 "for part %d of a %d-part WIM.\n",
1088 part_number, total_parts);
1090 wimlib_print_lookup_table(w);
1094 ret = wimlib_extract_xml_data(w, stdout);
1100 fp = fopen(xml_out_file, "wb");
1102 imagex_error_with_errno("Failed to open the "
1108 ret = wimlib_extract_xml_data(w, fp);
1109 if (fclose(fp) != 0) {
1110 imagex_error("Failed to close the file `%s'",
1120 wimlib_print_available_images(w, image);
1123 if (total_parts != 1 && part_number != 1) {
1124 imagex_error("Select part 1 of this %d-part WIM "
1125 "to see the image metadata",
1127 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
1129 ret = wimlib_print_metadata(w, image);
1134 if (total_parts != 1) {
1135 imagex_error("Modifying a split WIM is not supported.");
1138 if (image == WIM_ALL_IMAGES)
1141 if (image == WIM_NO_IMAGE && new_name) {
1142 imagex_error("Cannot specify new_name (`%s') when "
1143 "using image 0", new_name);
1148 if (image == wimlib_get_boot_idx(w)) {
1149 printf("Image %d is already marked as "
1150 "bootable.\n", image);
1153 printf("Marking image %d as bootable.\n",
1155 wimlib_set_boot_idx(w, image);
1159 if (strcmp(wimlib_get_image_name(w, image),
1161 printf("Image %d is already named \"%s\".\n",
1165 printf("Changing the name of image %d to "
1166 "\"%s\".\n", image, new_name);
1167 ret = wimlib_set_image_name(w, image, new_name);
1173 const char *old_desc;
1174 old_desc = wimlib_get_image_description(w, image);
1175 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1176 printf("The description of image %d is already "
1177 "\"%s\".\n", image, new_desc);
1180 printf("Changing the description of image %d "
1181 "to \"%s\".\n", image, new_desc);
1182 ret = wimlib_set_image_descripton(w, image,
1189 /* Only call wimlib_overwrite_xml_and_header() if something
1190 * actually needs to be changed. */
1191 if (boot || new_name || new_desc ||
1192 check != wimlib_has_integrity_table(w)) {
1194 ret = wimlib_overwrite_xml_and_header(w, check ?
1195 WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
1196 WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
1198 printf("The file `%s' was not modified because nothing "
1199 "needed to be done.\n", wimfile);
1209 /* Join split WIMs into one part WIM */
1210 static int imagex_join(int argc, const char **argv)
1213 int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1214 const char *output_path;
1216 for_opt(c, join_options) {
1219 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1229 imagex_error("Must specify at least one split WIM (.swm) parts "
1233 output_path = argv[0];
1234 return wimlib_join(++argv, --argc, output_path, flags);
1240 /* Mounts an image using a FUSE mount. */
1241 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1244 int mount_flags = 0;
1245 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1246 const char *wimfile;
1253 if (strcmp(argv[0], "mountrw") == 0)
1254 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1255 for_opt(c, mount_options) {
1258 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1261 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1264 if (strcasecmp(optarg, "none") == 0)
1265 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1266 else if (strcasecmp(optarg, "xattr") == 0)
1267 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1268 else if (strcasecmp(optarg, "windows") == 0)
1269 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1271 imagex_error("Unknown stream interface \"%s\"", optarg);
1281 if (argc != 2 && argc != 3)
1286 ret = wimlib_open_wim(wimfile, open_flags, &w);
1292 num_images = wimlib_get_num_images(w);
1293 if (num_images != 1) {
1294 imagex_error("The file `%s' contains %d images; Please "
1295 "select one", wimfile, num_images);
1296 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1298 ret = WIMLIB_ERR_INVALID_IMAGE;
1303 image = wimlib_resolve_image(w, argv[1]);
1307 ret = verify_image_exists_and_is_single(image);
1311 ret = wimlib_mount(w, image, dir, mount_flags);
1313 imagex_error("Failed to mount image %d from `%s' on `%s'",
1314 image, wimfile, dir);
1321 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1326 /* Split a WIM into a spanned set */
1327 static int imagex_split(int argc, const char **argv)
1330 int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1331 unsigned long part_size;
1333 for_opt(c, split_options) {
1336 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1350 part_size = strtoul(argv[2], NULL, 10) * (1 << 20);
1351 return wimlib_split(argv[0], argv[1], part_size, flags);
1354 /* Unmounts an image. */
1355 static int imagex_unmount(int argc, const char **argv)
1358 int unmount_flags = 0;
1361 for_opt(c, unmount_options) {
1364 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1367 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1381 ret = wimlib_unmount(argv[0], unmount_flags);
1383 imagex_error("Failed to unmount `%s'", argv[0]);
1387 struct imagex_command {
1389 int (*func)(int , const char **);
1393 static struct imagex_command imagex_commands[] = {
1394 {"append", imagex_append, APPEND},
1395 {"apply", imagex_apply, APPLY},
1396 {"capture", imagex_capture, CAPTURE},
1397 {"delete", imagex_delete, DELETE},
1398 {"dir", imagex_dir, DIR},
1399 {"export", imagex_export, EXPORT},
1400 {"info", imagex_info, INFO},
1401 {"join", imagex_join, JOIN},
1402 {"mount", imagex_mount_rw_or_ro, MOUNT},
1403 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1404 {"split", imagex_split, SPLIT},
1405 {"unmount", imagex_unmount, UNMOUNT},
1408 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1409 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1411 static void help_or_version(int argc, const char **argv)
1415 struct imagex_command *cmd;
1417 for (i = 1; i < argc; i++) {
1425 if (strcmp(p, "help") == 0 || (*p == '?' && *(p + 1) == '\0')) {
1426 for_imagex_command(cmd) {
1427 if (strcmp(cmd->name, argv[1]) == 0) {
1435 if (strcmp(p, "version") == 0) {
1443 int main(int argc, const char **argv)
1445 struct imagex_command *cmd;
1449 imagex_error("No command specified");
1454 help_or_version(argc, argv);
1458 wimlib_set_print_errors(true);
1460 for_imagex_command(cmd) {
1461 if (strcmp(cmd->name, *argv) == 0) {
1462 ret = cmd->func(argc, argv);
1464 imagex_error("Exiting with error code %d:\n"
1466 wimlib_get_error_string(ret));
1467 if (ret == WIMLIB_ERR_NTFS_3G)
1468 imagex_error_with_errno("errno");
1474 imagex_error("Unrecognized command: `%s'", argv[0]);
1478 /*#ifndef WITH_NTFS_3G*/
1479 /*ERROR("wimlib was not compiled with support for NTFS-3g, so we cannot extract");*/
1480 /*ERROR("a WIM to a NTFS filesystem while preserving NTFS-specific metadata.");*/
1481 /*ERROR("Please apply the WIM to a directory rather than a block device, ");*/
1482 /*ERROR("and without the NTFS flag; or compile in support for NTFS-3g.");*/
1483 /*return WIMLIB_ERR_UNSUPPORTED;*/