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 void usage(int cmd_type);
60 static void usage_all();
62 static const char *usage_strings[] = {
64 " imagex append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
65 " [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
66 " [--verbose] [--dereference] [--config=FILE]\n",
68 " imagex apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
69 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
70 " [--symlink] [--verbose] [--ref=\"GLOB\"]\n",
72 " imagex capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
73 " [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
74 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
77 " imagex delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n",
79 " imagex dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n",
81 " imagex export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
82 " DEST_WIMFILE [DEST_IMAGE_NAME]\n"
83 " [DEST_IMAGE_DESCRIPTION] [--boot] [--check]\n"
84 " [--compress=TYPE] [--ref=\"GLOB\"]\n",
86 " imagex info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
87 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
88 " [--xml] [--extract-xml FILE] [--metadata]\n",
90 " imagex join [--check] WIMFILE SPLIT_WIM...\n",
92 " imagex mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
93 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
94 " [--ref=\"GLOB\"]\n",
96 " imagex mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
97 " [--check] [--debug] [--streams-interface=INTERFACE]\n",
99 " imagex split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n",
101 " imagex unmount DIRECTORY [--commit] [--check]\n",
104 static const struct option common_options[] = {
105 {"help", 0, NULL, 'h'},
106 {"version", 0, NULL, 'v'},
110 static const struct option append_options[] = {
111 {"boot", no_argument, NULL, 'b'},
112 {"check", no_argument, NULL, 'c'},
113 {"config", required_argument, NULL, 'C'},
114 {"dereference", no_argument, NULL, 'L'},
115 {"flags", required_argument, NULL, 'f'},
116 {"verbose", no_argument, NULL, 'v'},
119 static const struct option apply_options[] = {
120 {"check", no_argument, NULL, 'c'},
121 {"hardlink", no_argument, NULL, 'h'},
122 {"symlink", no_argument, NULL, 's'},
123 {"verbose", no_argument, NULL, 'v'},
124 {"ref", required_argument, NULL, 'r'},
127 static const struct option capture_options[] = {
128 {"boot", no_argument, NULL, 'b'},
129 {"check", no_argument, NULL, 'c'},
130 {"compress", required_argument, NULL, 'x'},
131 {"config", required_argument, NULL, 'C'},
132 {"dereference", no_argument, NULL, 'L'},
133 {"flags", required_argument, NULL, 'f'},
134 {"verbose", no_argument, NULL, 'v'},
137 static const struct option delete_options[] = {
138 {"check", no_argument, NULL, 'c'},
142 static const struct option export_options[] = {
143 {"boot", no_argument, NULL, 'b'},
144 {"check", no_argument, NULL, 'c'},
145 {"compress", required_argument, NULL, 'x'},
146 {"ref", required_argument, NULL, 'r'},
150 static const struct option info_options[] = {
151 {"boot", no_argument, NULL, 'b'},
152 {"check", no_argument, NULL, 'c'},
153 {"extract-xml", required_argument, NULL, 'X'},
154 {"header", no_argument, NULL, 'h'},
155 {"lookup-table", no_argument, NULL, 'l'},
156 {"metadata", no_argument, NULL, 'm'},
157 {"xml", no_argument, NULL, 'x'},
161 static const struct option join_options[] = {
162 {"check", no_argument, NULL, 'c'},
166 static const struct option mount_options[] = {
167 {"check", no_argument, NULL, 'c'},
168 {"debug", no_argument, NULL, 'd'},
169 {"streams-interface", required_argument, NULL, 's'},
170 {"ref", required_argument, NULL, 'r'},
174 static const struct option split_options[] = {
175 {"check", no_argument, NULL, 'c'},
179 static const struct option unmount_options[] = {
180 {"commit", no_argument, NULL, 'c'},
181 {"check", no_argument, NULL, 'C'},
187 /* Print formatted error message to stderr. */
188 static void imagex_error(const char *format, ...)
191 va_start(va, format);
192 fputs("ERROR: ", stderr);
193 vfprintf(stderr, format, va);
198 /* Print formatted error message to stderr. */
199 static void imagex_error_with_errno(const char *format, ...)
201 int errno_save = errno;
203 va_start(va, format);
204 fputs("ERROR: ", stderr);
205 vfprintf(stderr, format, va);
206 fprintf(stderr, ": %s\n", strerror(errno_save));
210 static const char *path_basename(const char *path)
212 const char *p = path;
217 /* Trailing slashes. */
218 while ((p != path - 1) && *p == '/')
221 while ((p != path - 1) && *p != '/')
228 static int verify_image_exists(int image)
230 if (image == WIM_NO_IMAGE) {
231 imagex_error("Not a valid image");
232 return WIMLIB_ERR_INVALID_IMAGE;
237 static int verify_image_is_single(int image)
239 if (image == WIM_ALL_IMAGES) {
240 imagex_error("Cannot specify all images for this action");
241 return WIMLIB_ERR_INVALID_IMAGE;
246 static int verify_image_exists_and_is_single(int image)
249 ret = verify_image_exists(image);
251 ret = verify_image_is_single(image);
255 static int get_compression_type(const char *optarg)
257 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
258 return WIM_COMPRESSION_TYPE_LZX;
259 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
260 return WIM_COMPRESSION_TYPE_XPRESS;
261 else if (strcasecmp(optarg, "none") == 0)
262 return WIM_COMPRESSION_TYPE_NONE;
264 imagex_error("Invalid compression type `%s'! Must be "
265 "\"maximum\", \"fast\", or \"none\".", optarg);
266 return WIM_COMPRESSION_TYPE_INVALID;
270 static const char *file_get_contents(const char *filename, size_t *len_ret)
277 if (stat(filename, &stbuf) != 0) {
278 imagex_error_with_errno("Failed to stat the file `%s'", filename);
283 fp = fopen(filename, "rb");
285 imagex_error_with_errno("Failed to open the file `%s'", filename);
291 imagex_error("Failed to allocate buffer of %zu bytes to hold "
292 "contents of file `%s'", len, filename);
295 if (fread(buf, 1, len, fp) != len) {
296 imagex_error_with_errno("Failed to read %lu bytes from the "
297 "file `%s'", len, filename);
309 static int open_swms_from_glob(const char *swm_glob,
310 const char *first_part,
312 WIMStruct ***additional_swms_ret,
313 unsigned *num_additional_swms_ret)
315 unsigned num_additional_swms = 0;
316 WIMStruct **additional_swms = NULL;
320 ret = glob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
322 if (ret == GLOB_NOMATCH) {
323 imagex_error("Found no files for glob \"%s\"",
326 imagex_error_with_errno("Failed to process glob "
332 num_additional_swms = globbuf.gl_pathc;
333 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
334 if (!additional_swms) {
335 imagex_error("Out of memory");
340 for (size_t i = 0; i < num_additional_swms; i++) {
341 if (strcmp(globbuf.gl_pathv[i], first_part) == 0) {
345 ret = wimlib_open_wim(globbuf.gl_pathv[i],
346 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
347 &additional_swms[i - offset]);
351 *additional_swms_ret = additional_swms;
352 *num_additional_swms_ret = num_additional_swms - offset;
356 for (unsigned i = 0; i < num_additional_swms; i++)
357 wimlib_free(additional_swms[i]);
358 free(additional_swms);
365 static int imagex_append(int argc, const char **argv)
368 const char *flags_element = NULL;
369 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
370 int add_image_flags = 0;
371 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
376 const char *config_file = NULL;
377 const char *config_str = NULL;
378 size_t config_len = 0;
383 for_opt(c, append_options) {
386 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
389 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
390 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
393 config_file = optarg;
396 flags_element = optarg;
399 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
402 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
411 if (argc < 2 || argc > 4) {
417 name = (argc >= 3) ? argv[2] : path_basename(dir);
418 desc = (argc >= 4) ? argv[3] : NULL;
421 config_str = file_get_contents(config_file, &config_len);
426 ret = wimlib_open_wim(wimfile, open_flags, &w);
433 ret = stat(dir, &stbuf);
435 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
436 const char *ntfs_device = dir;
437 printf("Capturing WIM image NTFS filesystem on `%s'\n",
439 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
448 imagex_error_with_errno("Failed to stat `%s'", dir);
451 ret = wimlib_add_image(w, dir, name, config_str, config_len,
457 cur_image = wimlib_get_num_images(w);
459 ret = wimlib_set_image_descripton(w, cur_image, desc);
464 ret = wimlib_set_image_flags(w, cur_image, flags_element);
468 ret = wimlib_overwrite(w, write_flags);
474 /* Extract one image, or all images, from a WIM file into a directory. */
475 static int imagex_apply(int argc, const char **argv)
478 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
479 WIMLIB_OPEN_FLAG_SPLIT_OK;
486 const char *image_num_or_name;
487 int extract_flags = 0;
489 const char *swm_glob = NULL;
490 WIMStruct **additional_swms = NULL;
491 unsigned num_additional_swms = 0;
493 for_opt(c, apply_options) {
496 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
499 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
502 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
505 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
517 if (argc != 2 && argc != 3) {
524 image_num_or_name = "1";
527 image_num_or_name = argv[1];
531 ret = wimlib_open_wim(wimfile, open_flags, &w);
535 image = wimlib_resolve_image(w, image_num_or_name);
536 ret = verify_image_exists(image);
540 num_images = wimlib_get_num_images(w);
541 if (argc == 2 && num_images != 1) {
542 imagex_error("`%s' contains %d images; Please select one "
543 "(or all)", wimfile, num_images);
550 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
552 &num_additional_swms);
560 ret = stat(dir, &stbuf);
562 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
563 const char *ntfs_device = dir;
564 printf("Applying image %d of `%s' to NTFS filesystem on `%s'\n",
565 image, wimfile, ntfs_device);
566 ret = wimlib_apply_image_to_ntfs_volume(w, image,
570 num_additional_swms);
575 imagex_error_with_errno("Failed to stat `%s'", dir);
579 ret = wimlib_extract_image(w, image, dir, extract_flags,
580 additional_swms, num_additional_swms);
584 for (size_t i = 0; i < num_additional_swms; i++)
585 wimlib_free(additional_swms[i]);
590 /* Create a WIM file from a directory. */
591 static int imagex_capture(int argc, const char **argv)
594 int add_image_flags = 0;
595 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
596 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
597 const char *flags_element = NULL;
602 const char *config_file = NULL;
603 const char *config_str = NULL;
604 size_t config_len = 0;
609 for_opt(c, capture_options) {
612 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
615 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
618 config_file = optarg;
621 compression_type = get_compression_type(optarg);
622 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
626 flags_element = optarg;
629 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
630 write_flags |= WIMLIB_WRITE_FLAG_VERBOSE;
633 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
643 if (argc < 2 || argc > 4) {
649 name = (argc >= 3) ? argv[2] : dir;
650 desc = (argc >= 4) ? argv[3] : NULL;
653 config_str = file_get_contents(config_file, &config_len);
658 ret = wimlib_create_new_wim(compression_type, &w);
665 ret = stat(dir, &stbuf);
667 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
668 const char *ntfs_device = dir;
669 printf("Capturing WIM image NTFS filesystem on `%s'\n",
671 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
680 imagex_error_with_errno("Failed to stat `%s'", dir);
683 ret = wimlib_add_image(w, dir, name, config_str,
684 config_len, add_image_flags);
688 imagex_error("Failed to add the image `%s'", dir);
691 cur_image = wimlib_get_num_images(w);
693 ret = wimlib_set_image_descripton(w, cur_image, desc);
698 ret = wimlib_set_image_flags(w, cur_image, flags_element);
703 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
705 imagex_error("Failed to write the WIM file `%s'", wimfile);
711 /* Remove image(s) from a WIM. */
712 static int imagex_delete(int argc, const char **argv)
715 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
716 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
718 const char *image_num_or_name;
723 for_opt(c, delete_options) {
726 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
727 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
739 imagex_error("Must specify a WIM file");
741 imagex_error("Must specify an image");
746 image_num_or_name = argv[1];
748 ret = wimlib_open_wim(wimfile, open_flags, &w);
752 image = wimlib_resolve_image(w, image_num_or_name);
754 ret = verify_image_exists(image);
758 ret = wimlib_delete_image(w, image);
760 imagex_error("Failed to delete image from `%s'", wimfile);
764 ret = wimlib_overwrite(w, write_flags);
766 imagex_error("Failed to write the file `%s' with image "
774 /* Print the files contained in an image(s) in a WIM file. */
775 static int imagex_dir(int argc, const char **argv)
785 imagex_error("Must specify a WIM file");
790 imagex_error("Too many arguments");
796 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
800 part_number = wimlib_get_part_number(w, NULL);
801 if (part_number != 1) {
802 imagex_error("`%s' is part %d of a split WIM! Specify the "
803 "first part to see the files",
804 wimfile, part_number);
805 ret = WIMLIB_ERR_SPLIT_UNSUPPORTED;
810 image = wimlib_resolve_image(w, argv[2]);
811 ret = verify_image_exists(image);
815 /* Image was not specified. If the WIM only contains one image,
816 * choose that one; otherwise, print an error. */
817 num_images = wimlib_get_num_images(w);
818 if (num_images != 1) {
819 imagex_error("The file `%s' contains %d images; Please "
820 "select one.", wimfile, num_images);
828 ret = wimlib_print_files(w, image);
834 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
836 static int imagex_export(int argc, const char **argv)
839 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
840 int export_flags = 0;
841 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
842 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
843 bool compression_type_specified = false;
844 const char *src_wimfile;
845 const char *src_image_num_or_name;
846 const char *dest_wimfile;
847 const char *dest_name;
848 const char *dest_desc;
849 WIMStruct *src_w = NULL;
850 WIMStruct *dest_w = NULL;
855 const char *swm_glob = NULL;
856 WIMStruct **additional_swms = NULL;
857 unsigned num_additional_swms = 0;
859 for_opt(c, export_options) {
862 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
865 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
866 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
869 compression_type = get_compression_type(optarg);
870 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
872 compression_type_specified = true;
884 if (argc < 3 || argc > 5) {
888 src_wimfile = argv[0];
889 src_image_num_or_name = argv[1];
890 dest_wimfile = argv[2];
891 dest_name = (argc >= 4) ? argv[3] : NULL;
892 dest_desc = (argc >= 5) ? argv[4] : NULL;
893 ret = wimlib_open_wim(src_wimfile,
894 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w);
898 /* Determine if the destination is an existing file or not.
899 * If so, we try to append the exported image(s) to it; otherwise, we
900 * create a new WIM containing the exported image(s). */
901 if (stat(dest_wimfile, &stbuf) == 0) {
903 /* Destination file exists. */
904 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
905 imagex_error("`%s' is not a regular file",
909 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
913 if (compression_type_specified && compression_type !=
914 wimlib_get_compression_type(dest_w)) {
915 imagex_error("Cannot specify a compression type that is "
916 "not the same as that used in the "
921 compression_type = wimlib_get_compression_type(dest_w);
924 /* dest_wimfile is not an existing file, so create a new WIM. */
925 if (errno == ENOENT) {
926 ret = wimlib_create_new_wim(compression_type, &dest_w);
930 imagex_error_with_errno("Cannot stat file `%s'",
936 image = wimlib_resolve_image(src_w, src_image_num_or_name);
937 ret = verify_image_exists(image);
942 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
944 &num_additional_swms);
949 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
950 export_flags, additional_swms,
951 num_additional_swms);
957 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
960 ret = wimlib_overwrite(dest_w, write_flags);
965 for (unsigned i = 0; i < num_additional_swms; i++)
966 wimlib_free(additional_swms[i]);
970 /* Prints information about a WIM file; also can mark an image as bootable,
971 * change the name of an image, or change the description of an image. */
972 static int imagex_info(int argc, const char **argv)
978 bool lookup_table = false;
980 bool metadata = false;
981 bool short_header = true;
982 const char *xml_out_file = NULL;
984 const char *image_num_or_name = "all";
985 const char *new_name = NULL;
986 const char *new_desc = NULL;
991 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
992 WIMLIB_OPEN_FLAG_SPLIT_OK;
996 for_opt(c, info_options) {
1006 short_header = false;
1009 lookup_table = true;
1010 short_header = false;
1014 short_header = false;
1017 xml_out_file = optarg;
1018 short_header = false;
1022 short_header = false;
1032 if (argc == 0 || argc > 4) {
1038 image_num_or_name = argv[1];
1048 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1050 ret = wimlib_open_wim(wimfile, open_flags, &w);
1054 part_number = wimlib_get_part_number(w, &total_parts);
1056 /*if (total_parts > 1 && part_number > 1) {*/
1057 /*printf("Warning: this is part %d of a %d-part split WIM.\n"*/
1058 /*" Select the first part if you want to see information\n"*/
1059 /*" about images in the WIM.\n", */
1060 /*part_number, total_parts);*/
1063 image = wimlib_resolve_image(w, image_num_or_name);
1064 if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
1065 imagex_error("The image `%s' does not exist",
1068 imagex_error("If you would like to set the boot "
1069 "index to 0, specify image \"0\" with "
1070 "the --boot flag.");
1071 ret = WIMLIB_ERR_INVALID_IMAGE;
1075 if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
1077 imagex_error("Cannot specify the --boot flag "
1078 "without specifying a specific "
1079 "image in a multi-image WIM");
1080 ret = WIMLIB_ERR_INVALID_IMAGE;
1084 imagex_error("Cannot specify the NEW_NAME "
1085 "without specifying a specific "
1086 "image in a multi-image WIM");
1087 ret = WIMLIB_ERR_INVALID_IMAGE;
1092 /* Operations that print information are separated from operations that
1093 * recreate the WIM file. */
1094 if (!new_name && !boot) {
1096 if (image == WIM_NO_IMAGE) {
1097 imagex_error("`%s' is not a valid image",
1099 ret = WIMLIB_ERR_INVALID_IMAGE;
1103 if (image == WIM_ALL_IMAGES && short_header)
1104 wimlib_print_wim_information(w);
1107 wimlib_print_header(w);
1110 if (total_parts != 1) {
1111 printf("Warning: Only showing the lookup table "
1112 "for part %d of a %d-part WIM.\n",
1113 part_number, total_parts);
1115 wimlib_print_lookup_table(w);
1119 ret = wimlib_extract_xml_data(w, stdout);
1125 fp = fopen(xml_out_file, "wb");
1127 imagex_error_with_errno("Failed to open the "
1133 ret = wimlib_extract_xml_data(w, fp);
1134 if (fclose(fp) != 0) {
1135 imagex_error("Failed to close the file `%s'",
1145 wimlib_print_available_images(w, image);
1148 if (total_parts != 1 && part_number != 1) {
1149 imagex_error("Select part 1 of this %d-part WIM "
1150 "to see the image metadata",
1152 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
1154 ret = wimlib_print_metadata(w, image);
1159 if (total_parts != 1) {
1160 imagex_error("Modifying a split WIM is not supported.");
1163 if (image == WIM_ALL_IMAGES)
1166 if (image == WIM_NO_IMAGE && new_name) {
1167 imagex_error("Cannot specify new_name (`%s') when "
1168 "using image 0", new_name);
1173 if (image == wimlib_get_boot_idx(w)) {
1174 printf("Image %d is already marked as "
1175 "bootable.\n", image);
1178 printf("Marking image %d as bootable.\n",
1180 wimlib_set_boot_idx(w, image);
1184 if (strcmp(wimlib_get_image_name(w, image),
1186 printf("Image %d is already named \"%s\".\n",
1190 printf("Changing the name of image %d to "
1191 "\"%s\".\n", image, new_name);
1192 ret = wimlib_set_image_name(w, image, new_name);
1198 const char *old_desc;
1199 old_desc = wimlib_get_image_description(w, image);
1200 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1201 printf("The description of image %d is already "
1202 "\"%s\".\n", image, new_desc);
1205 printf("Changing the description of image %d "
1206 "to \"%s\".\n", image, new_desc);
1207 ret = wimlib_set_image_descripton(w, image,
1214 /* Only call wimlib_overwrite_xml_and_header() if something
1215 * actually needs to be changed. */
1216 if (boot || new_name || new_desc ||
1217 check != wimlib_has_integrity_table(w)) {
1219 ret = wimlib_overwrite_xml_and_header(w, check ?
1220 WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
1221 WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
1223 printf("The file `%s' was not modified because nothing "
1224 "needed to be done.\n", wimfile);
1234 /* Join split WIMs into one part WIM */
1235 static int imagex_join(int argc, const char **argv)
1238 int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1239 const char *output_path;
1241 for_opt(c, join_options) {
1244 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1254 imagex_error("Must specify at least one split WIM (.swm) parts "
1258 output_path = argv[0];
1259 return wimlib_join(++argv, --argc, output_path, flags);
1265 /* Mounts an image using a FUSE mount. */
1266 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1269 int mount_flags = 0;
1270 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
1271 WIMLIB_OPEN_FLAG_SPLIT_OK;
1272 const char *wimfile;
1278 const char *swm_glob = NULL;
1279 WIMStruct **additional_swms = NULL;
1280 unsigned num_additional_swms = 0;
1282 if (strcmp(argv[0], "mountrw") == 0)
1283 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1284 for_opt(c, mount_options) {
1287 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1290 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1293 if (strcasecmp(optarg, "none") == 0)
1294 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1295 else if (strcasecmp(optarg, "xattr") == 0)
1296 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1297 else if (strcasecmp(optarg, "windows") == 0)
1298 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1300 imagex_error("Unknown stream interface \"%s\"", optarg);
1313 if (argc != 2 && argc != 3)
1318 ret = wimlib_open_wim(wimfile, open_flags, &w);
1323 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1325 &num_additional_swms);
1332 num_images = wimlib_get_num_images(w);
1333 if (num_images != 1) {
1334 imagex_error("The file `%s' contains %d images; Please "
1335 "select one", wimfile, num_images);
1336 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1338 ret = WIMLIB_ERR_INVALID_IMAGE;
1343 image = wimlib_resolve_image(w, argv[1]);
1347 ret = verify_image_exists_and_is_single(image);
1351 ret = wimlib_mount(w, image, dir, mount_flags, additional_swms,
1352 num_additional_swms);
1354 imagex_error("Failed to mount image %d from `%s' on `%s'",
1355 image, wimfile, dir);
1360 if (additional_swms)
1361 for (unsigned i = 0; i < num_additional_swms; i++)
1362 wimlib_free(additional_swms[i]);
1365 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1370 /* Split a WIM into a spanned set */
1371 static int imagex_split(int argc, const char **argv)
1374 int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1375 unsigned long part_size;
1377 for_opt(c, split_options) {
1380 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1394 part_size = strtoul(argv[2], NULL, 10) * (1 << 20);
1395 return wimlib_split(argv[0], argv[1], part_size, flags);
1398 /* Unmounts an image. */
1399 static int imagex_unmount(int argc, const char **argv)
1402 int unmount_flags = 0;
1405 for_opt(c, unmount_options) {
1408 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1411 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1425 ret = wimlib_unmount(argv[0], unmount_flags);
1427 imagex_error("Failed to unmount `%s'", argv[0]);
1431 struct imagex_command {
1433 int (*func)(int , const char **);
1438 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1439 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1441 static struct imagex_command imagex_commands[] = {
1442 {"append", imagex_append, APPEND},
1443 {"apply", imagex_apply, APPLY},
1444 {"capture", imagex_capture, CAPTURE},
1445 {"delete", imagex_delete, DELETE},
1446 {"dir", imagex_dir, DIR},
1447 {"export", imagex_export, EXPORT},
1448 {"info", imagex_info, INFO},
1449 {"join", imagex_join, JOIN},
1450 {"mount", imagex_mount_rw_or_ro, MOUNT},
1451 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1452 {"split", imagex_split, SPLIT},
1453 {"unmount", imagex_unmount, UNMOUNT},
1456 static void version()
1458 static const char *s =
1459 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
1460 "Copyright (C) 2012 Eric Biggers\n"
1461 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
1462 "This is free software: you are free to change and redistribute it.\n"
1463 "There is NO WARRANTY, to the extent permitted by law.\n"
1465 "Report bugs to "PACKAGE_BUGREPORT".\n";
1470 static void help_or_version(int argc, const char **argv)
1474 struct imagex_command *cmd;
1476 for (i = 1; i < argc; i++) {
1484 if (strcmp(p, "help") == 0 || (*p == '?' && *(p + 1) == '\0')) {
1485 for_imagex_command(cmd) {
1486 if (strcmp(cmd->name, argv[1]) == 0) {
1494 if (strcmp(p, "version") == 0) {
1502 static void usage(int cmd_type)
1504 struct imagex_command *cmd;
1505 puts("IMAGEX: Usage:");
1506 fputs(usage_strings[cmd_type], stdout);
1507 for_imagex_command(cmd)
1508 if (cmd->cmd == cmd_type)
1509 printf("\nTry `man imagex-%s' for more details.\n",
1513 static void usage_all()
1515 puts("IMAGEX: Usage:");
1516 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
1517 fputs(usage_strings[i], stdout);
1518 static const char *extra =
1520 " imagex --version\n"
1522 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
1524 " Try `man imagex' for more information.\n"
1526 fputs(extra, stdout);
1530 int main(int argc, const char **argv)
1532 struct imagex_command *cmd;
1536 imagex_error("No command specified");
1541 help_or_version(argc, argv);
1545 wimlib_set_print_errors(true);
1547 for_imagex_command(cmd) {
1548 if (strcmp(cmd->name, *argv) == 0) {
1549 ret = cmd->func(argc, argv);
1551 imagex_error("Exiting with error code %d:\n"
1553 wimlib_get_error_string(ret));
1554 if (ret == WIMLIB_ERR_NTFS_3G)
1555 imagex_error_with_errno("errno");
1561 imagex_error("Unrecognized command: `%s'", argv[0]);
1565 /*#ifndef WITH_NTFS_3G*/
1566 /*ERROR("wimlib was not compiled with support for NTFS-3g, so we cannot extract");*/
1567 /*ERROR("a WIM to a NTFS filesystem while preserving NTFS-specific metadata.");*/
1568 /*ERROR("Please apply the WIM to a directory rather than a block device, ");*/
1569 /*ERROR("and without the NTFS flag; or compile in support for NTFS-3g.");*/
1570 /*return WIMLIB_ERR_UNSUPPORTED;*/