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/>.
36 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
38 #define swap(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
41 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (char**)argv, "", \
60 static void usage(int cmd_type);
61 static void usage_all();
63 static const char *usage_strings[] = {
65 " imagex append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
66 " [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
67 " [--verbose] [--dereference] [--config=FILE]\n",
69 " imagex apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
70 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
71 " [--symlink] [--verbose] [--ref=\"GLOB\"]\n",
73 " imagex capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
74 " [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
75 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
78 " imagex delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n",
80 " imagex dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n",
82 " imagex export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
83 " DEST_WIMFILE [DEST_IMAGE_NAME]\n"
84 " [DEST_IMAGE_DESCRIPTION] [--boot] [--check]\n"
85 " [--compress=TYPE] [--ref=\"GLOB\"]\n",
87 " imagex info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
88 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
89 " [--xml] [--extract-xml FILE] [--metadata]\n",
91 " imagex join [--check] WIMFILE SPLIT_WIM...\n",
93 " imagex mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
94 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
95 " [--ref=\"GLOB\"]\n",
97 " imagex mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
98 " [--check] [--debug] [--streams-interface=INTERFACE]\n",
100 " imagex split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n",
102 " imagex unmount DIRECTORY [--commit] [--check]\n",
105 static const struct option common_options[] = {
106 {"help", 0, NULL, 'h'},
107 {"version", 0, NULL, 'v'},
111 static const struct option append_options[] = {
112 {"boot", no_argument, NULL, 'b'},
113 {"check", no_argument, NULL, 'c'},
114 {"config", required_argument, NULL, 'C'},
115 {"dereference", no_argument, NULL, 'L'},
116 {"flags", required_argument, NULL, 'f'},
117 {"verbose", no_argument, NULL, 'v'},
120 static const struct option apply_options[] = {
121 {"check", no_argument, NULL, 'c'},
122 {"hardlink", no_argument, NULL, 'h'},
123 {"symlink", no_argument, NULL, 's'},
124 {"verbose", no_argument, NULL, 'v'},
125 {"ref", required_argument, NULL, 'r'},
128 static const struct option capture_options[] = {
129 {"boot", no_argument, NULL, 'b'},
130 {"check", no_argument, NULL, 'c'},
131 {"compress", required_argument, NULL, 'x'},
132 {"config", required_argument, NULL, 'C'},
133 {"dereference", no_argument, NULL, 'L'},
134 {"flags", required_argument, NULL, 'f'},
135 {"verbose", no_argument, NULL, 'v'},
138 static const struct option delete_options[] = {
139 {"check", no_argument, NULL, 'c'},
143 static const struct option export_options[] = {
144 {"boot", no_argument, NULL, 'b'},
145 {"check", no_argument, NULL, 'c'},
146 {"compress", required_argument, NULL, 'x'},
147 {"ref", required_argument, NULL, 'r'},
151 static const struct option info_options[] = {
152 {"boot", no_argument, NULL, 'b'},
153 {"check", no_argument, NULL, 'c'},
154 {"extract-xml", required_argument, NULL, 'X'},
155 {"header", no_argument, NULL, 'h'},
156 {"lookup-table", no_argument, NULL, 'l'},
157 {"metadata", no_argument, NULL, 'm'},
158 {"xml", no_argument, NULL, 'x'},
162 static const struct option join_options[] = {
163 {"check", no_argument, NULL, 'c'},
167 static const struct option mount_options[] = {
168 {"check", no_argument, NULL, 'c'},
169 {"debug", no_argument, NULL, 'd'},
170 {"streams-interface", required_argument, NULL, 's'},
171 {"ref", required_argument, NULL, 'r'},
175 static const struct option split_options[] = {
176 {"check", no_argument, NULL, 'c'},
180 static const struct option unmount_options[] = {
181 {"commit", no_argument, NULL, 'c'},
182 {"check", no_argument, NULL, 'C'},
188 /* Print formatted error message to stderr. */
189 static void imagex_error(const char *format, ...)
192 va_start(va, format);
193 fputs("ERROR: ", stderr);
194 vfprintf(stderr, format, va);
199 /* Print formatted error message to stderr. */
200 static void imagex_error_with_errno(const char *format, ...)
202 int errno_save = errno;
204 va_start(va, format);
205 fputs("ERROR: ", stderr);
206 vfprintf(stderr, format, va);
207 fprintf(stderr, ": %s\n", strerror(errno_save));
211 static const char *path_basename(const char *path)
213 const char *p = path;
218 /* Trailing slashes. */
219 while ((p != path - 1) && *p == '/')
222 while ((p != path - 1) && *p != '/')
229 static int verify_image_exists(int image)
231 if (image == WIM_NO_IMAGE) {
232 imagex_error("Not a valid image");
233 return WIMLIB_ERR_INVALID_IMAGE;
238 static int verify_image_is_single(int image)
240 if (image == WIM_ALL_IMAGES) {
241 imagex_error("Cannot specify all images for this action");
242 return WIMLIB_ERR_INVALID_IMAGE;
247 static int verify_image_exists_and_is_single(int image)
250 ret = verify_image_exists(image);
252 ret = verify_image_is_single(image);
256 static int get_compression_type(const char *optarg)
258 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
259 return WIM_COMPRESSION_TYPE_LZX;
260 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
261 return WIM_COMPRESSION_TYPE_XPRESS;
262 else if (strcasecmp(optarg, "none") == 0)
263 return WIM_COMPRESSION_TYPE_NONE;
265 imagex_error("Invalid compression type `%s'! Must be "
266 "\"maximum\", \"fast\", or \"none\".", optarg);
267 return WIM_COMPRESSION_TYPE_INVALID;
271 static const char *file_get_contents(const char *filename, size_t *len_ret)
278 if (stat(filename, &stbuf) != 0) {
279 imagex_error_with_errno("Failed to stat the file `%s'", filename);
284 fp = fopen(filename, "rb");
286 imagex_error_with_errno("Failed to open the file `%s'", filename);
292 imagex_error("Failed to allocate buffer of %zu bytes to hold "
293 "contents of file `%s'", len, filename);
296 if (fread(buf, 1, len, fp) != len) {
297 imagex_error_with_errno("Failed to read %lu bytes from the "
298 "file `%s'", len, filename);
310 static int open_swms_from_glob(const char *swm_glob,
311 const char *first_part,
313 WIMStruct ***additional_swms_ret,
314 unsigned *num_additional_swms_ret)
316 unsigned num_additional_swms = 0;
317 WIMStruct **additional_swms = NULL;
321 ret = glob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
323 if (ret == GLOB_NOMATCH) {
324 imagex_error("Found no files for glob \"%s\"",
327 imagex_error_with_errno("Failed to process glob "
333 num_additional_swms = globbuf.gl_pathc;
334 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
335 if (!additional_swms) {
336 imagex_error("Out of memory");
341 for (size_t i = 0; i < num_additional_swms; i++) {
342 if (strcmp(globbuf.gl_pathv[i], first_part) == 0) {
346 ret = wimlib_open_wim(globbuf.gl_pathv[i],
347 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
348 &additional_swms[i - offset]);
352 *additional_swms_ret = additional_swms;
353 *num_additional_swms_ret = num_additional_swms - offset;
357 for (unsigned i = 0; i < num_additional_swms; i++)
358 wimlib_free(additional_swms[i]);
359 free(additional_swms);
366 static int imagex_append(int argc, const char **argv)
369 const char *flags_element = NULL;
370 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
371 int add_image_flags = 0;
372 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
377 const char *config_file = NULL;
378 const char *config_str = NULL;
379 size_t config_len = 0;
385 for_opt(c, append_options) {
388 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
391 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
392 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
395 config_file = optarg;
398 flags_element = optarg;
401 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
404 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
413 if (argc < 2 || argc > 4) {
420 char dir_copy[strlen(dir) + 1];
421 memcpy(dir_copy, dir, strlen(dir) + 1);
422 default_name = basename(dir_copy);
424 name = (argc >= 3) ? argv[2] : default_name;
425 desc = (argc >= 4) ? argv[3] : NULL;
428 config_str = file_get_contents(config_file, &config_len);
433 ret = wimlib_open_wim(wimfile, open_flags, &w);
440 ret = stat(dir, &stbuf);
442 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
443 const char *ntfs_device = dir;
444 printf("Capturing WIM image NTFS filesystem on `%s'\n",
446 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
455 imagex_error_with_errno("Failed to stat `%s'", dir);
458 ret = wimlib_add_image(w, dir, name, config_str, config_len,
464 cur_image = wimlib_get_num_images(w);
466 ret = wimlib_set_image_descripton(w, cur_image, desc);
471 ret = wimlib_set_image_flags(w, cur_image, flags_element);
475 ret = wimlib_overwrite(w, write_flags);
481 /* Extract one image, or all images, from a WIM file into a directory. */
482 static int imagex_apply(int argc, const char **argv)
485 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
486 WIMLIB_OPEN_FLAG_SPLIT_OK;
493 const char *image_num_or_name;
494 int extract_flags = 0;
496 const char *swm_glob = NULL;
497 WIMStruct **additional_swms = NULL;
498 unsigned num_additional_swms = 0;
500 for_opt(c, apply_options) {
503 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
506 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
509 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
512 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
524 if (argc != 2 && argc != 3) {
531 image_num_or_name = "1";
534 image_num_or_name = argv[1];
538 ret = wimlib_open_wim(wimfile, open_flags, &w);
542 image = wimlib_resolve_image(w, image_num_or_name);
543 ret = verify_image_exists(image);
547 num_images = wimlib_get_num_images(w);
548 if (argc == 2 && num_images != 1) {
549 imagex_error("`%s' contains %d images; Please select one "
550 "(or all)", wimfile, num_images);
557 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
559 &num_additional_swms);
567 ret = stat(dir, &stbuf);
569 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
570 const char *ntfs_device = dir;
571 printf("Applying image %d of `%s' to NTFS filesystem on `%s'\n",
572 image, wimfile, ntfs_device);
573 ret = wimlib_apply_image_to_ntfs_volume(w, image,
577 num_additional_swms);
582 imagex_error_with_errno("Failed to stat `%s'", dir);
586 ret = wimlib_extract_image(w, image, dir, extract_flags,
587 additional_swms, num_additional_swms);
591 for (size_t i = 0; i < num_additional_swms; i++)
592 wimlib_free(additional_swms[i]);
597 /* Create a WIM file from a directory. */
598 static int imagex_capture(int argc, const char **argv)
601 int add_image_flags = 0;
602 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
603 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
604 const char *flags_element = NULL;
609 const char *config_file = NULL;
610 const char *config_str = NULL;
611 size_t config_len = 0;
617 for_opt(c, capture_options) {
620 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
623 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
626 config_file = optarg;
629 compression_type = get_compression_type(optarg);
630 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
634 flags_element = optarg;
637 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
638 write_flags |= WIMLIB_WRITE_FLAG_VERBOSE;
641 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
651 if (argc < 2 || argc > 4) {
658 char dir_copy[strlen(dir) + 1];
659 memcpy(dir_copy, dir, strlen(dir) + 1);
660 default_name = basename(dir_copy);
662 name = (argc >= 3) ? argv[2] : default_name;
663 desc = (argc >= 4) ? argv[3] : NULL;
666 config_str = file_get_contents(config_file, &config_len);
671 ret = wimlib_create_new_wim(compression_type, &w);
678 ret = stat(dir, &stbuf);
680 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
681 const char *ntfs_device = dir;
682 printf("Capturing WIM image NTFS filesystem on `%s'\n",
684 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
693 imagex_error_with_errno("Failed to stat `%s'", dir);
696 ret = wimlib_add_image(w, dir, name, config_str,
697 config_len, add_image_flags);
701 imagex_error("Failed to add the image `%s'", dir);
704 cur_image = wimlib_get_num_images(w);
706 ret = wimlib_set_image_descripton(w, cur_image, desc);
711 ret = wimlib_set_image_flags(w, cur_image, flags_element);
716 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
718 imagex_error("Failed to write the WIM file `%s'", wimfile);
724 /* Remove image(s) from a WIM. */
725 static int imagex_delete(int argc, const char **argv)
728 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
729 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
731 const char *image_num_or_name;
736 for_opt(c, delete_options) {
739 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
740 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
752 imagex_error("Must specify a WIM file");
754 imagex_error("Must specify an image");
759 image_num_or_name = argv[1];
761 ret = wimlib_open_wim(wimfile, open_flags, &w);
765 image = wimlib_resolve_image(w, image_num_or_name);
767 ret = verify_image_exists(image);
771 ret = wimlib_delete_image(w, image);
773 imagex_error("Failed to delete image from `%s'", wimfile);
777 ret = wimlib_overwrite(w, write_flags);
779 imagex_error("Failed to write the file `%s' with image "
787 /* Print the files contained in an image(s) in a WIM file. */
788 static int imagex_dir(int argc, const char **argv)
798 imagex_error("Must specify a WIM file");
803 imagex_error("Too many arguments");
809 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
813 part_number = wimlib_get_part_number(w, NULL);
814 if (part_number != 1) {
815 imagex_error("`%s' is part %d of a split WIM! Specify the "
816 "first part to see the files",
817 wimfile, part_number);
818 ret = WIMLIB_ERR_SPLIT_UNSUPPORTED;
823 image = wimlib_resolve_image(w, argv[2]);
824 ret = verify_image_exists(image);
828 /* Image was not specified. If the WIM only contains one image,
829 * choose that one; otherwise, print an error. */
830 num_images = wimlib_get_num_images(w);
831 if (num_images != 1) {
832 imagex_error("The file `%s' contains %d images; Please "
833 "select one.", wimfile, num_images);
841 ret = wimlib_print_files(w, image);
847 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
849 static int imagex_export(int argc, const char **argv)
852 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
853 int export_flags = 0;
854 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
855 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
856 bool compression_type_specified = false;
857 const char *src_wimfile;
858 const char *src_image_num_or_name;
859 const char *dest_wimfile;
860 const char *dest_name;
861 const char *dest_desc;
862 WIMStruct *src_w = NULL;
863 WIMStruct *dest_w = NULL;
868 const char *swm_glob = NULL;
869 WIMStruct **additional_swms = NULL;
870 unsigned num_additional_swms = 0;
872 for_opt(c, export_options) {
875 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
878 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
879 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
882 compression_type = get_compression_type(optarg);
883 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
885 compression_type_specified = true;
897 if (argc < 3 || argc > 5) {
901 src_wimfile = argv[0];
902 src_image_num_or_name = argv[1];
903 dest_wimfile = argv[2];
904 dest_name = (argc >= 4) ? argv[3] : NULL;
905 dest_desc = (argc >= 5) ? argv[4] : NULL;
906 ret = wimlib_open_wim(src_wimfile,
907 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w);
911 /* Determine if the destination is an existing file or not.
912 * If so, we try to append the exported image(s) to it; otherwise, we
913 * create a new WIM containing the exported image(s). */
914 if (stat(dest_wimfile, &stbuf) == 0) {
916 /* Destination file exists. */
917 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
918 imagex_error("`%s' is not a regular file",
922 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
926 if (compression_type_specified && compression_type !=
927 wimlib_get_compression_type(dest_w)) {
928 imagex_error("Cannot specify a compression type that is "
929 "not the same as that used in the "
934 compression_type = wimlib_get_compression_type(dest_w);
937 /* dest_wimfile is not an existing file, so create a new WIM. */
938 if (errno == ENOENT) {
939 ret = wimlib_create_new_wim(compression_type, &dest_w);
943 imagex_error_with_errno("Cannot stat file `%s'",
949 image = wimlib_resolve_image(src_w, src_image_num_or_name);
950 ret = verify_image_exists(image);
955 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
957 &num_additional_swms);
962 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
963 export_flags, additional_swms,
964 num_additional_swms);
970 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
973 ret = wimlib_overwrite(dest_w, write_flags);
978 for (unsigned i = 0; i < num_additional_swms; i++)
979 wimlib_free(additional_swms[i]);
983 /* Prints information about a WIM file; also can mark an image as bootable,
984 * change the name of an image, or change the description of an image. */
985 static int imagex_info(int argc, const char **argv)
991 bool lookup_table = false;
993 bool metadata = false;
994 bool short_header = true;
995 const char *xml_out_file = NULL;
997 const char *image_num_or_name = "all";
998 const char *new_name = NULL;
999 const char *new_desc = NULL;
1004 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
1005 WIMLIB_OPEN_FLAG_SPLIT_OK;
1009 for_opt(c, info_options) {
1019 short_header = false;
1022 lookup_table = true;
1023 short_header = false;
1027 short_header = false;
1030 xml_out_file = optarg;
1031 short_header = false;
1035 short_header = false;
1045 if (argc == 0 || argc > 4) {
1051 image_num_or_name = argv[1];
1061 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1063 ret = wimlib_open_wim(wimfile, open_flags, &w);
1067 part_number = wimlib_get_part_number(w, &total_parts);
1069 /*if (total_parts > 1 && part_number > 1) {*/
1070 /*printf("Warning: this is part %d of a %d-part split WIM.\n"*/
1071 /*" Select the first part if you want to see information\n"*/
1072 /*" about images in the WIM.\n", */
1073 /*part_number, total_parts);*/
1076 image = wimlib_resolve_image(w, image_num_or_name);
1077 if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
1078 imagex_error("The image `%s' does not exist",
1081 imagex_error("If you would like to set the boot "
1082 "index to 0, specify image \"0\" with "
1083 "the --boot flag.");
1084 ret = WIMLIB_ERR_INVALID_IMAGE;
1088 if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
1090 imagex_error("Cannot specify the --boot flag "
1091 "without specifying a specific "
1092 "image in a multi-image WIM");
1093 ret = WIMLIB_ERR_INVALID_IMAGE;
1097 imagex_error("Cannot specify the NEW_NAME "
1098 "without specifying a specific "
1099 "image in a multi-image WIM");
1100 ret = WIMLIB_ERR_INVALID_IMAGE;
1105 /* Operations that print information are separated from operations that
1106 * recreate the WIM file. */
1107 if (!new_name && !boot) {
1109 if (image == WIM_NO_IMAGE) {
1110 imagex_error("`%s' is not a valid image",
1112 ret = WIMLIB_ERR_INVALID_IMAGE;
1116 if (image == WIM_ALL_IMAGES && short_header)
1117 wimlib_print_wim_information(w);
1120 wimlib_print_header(w);
1123 if (total_parts != 1) {
1124 printf("Warning: Only showing the lookup table "
1125 "for part %d of a %d-part WIM.\n",
1126 part_number, total_parts);
1128 wimlib_print_lookup_table(w);
1132 ret = wimlib_extract_xml_data(w, stdout);
1138 fp = fopen(xml_out_file, "wb");
1140 imagex_error_with_errno("Failed to open the "
1146 ret = wimlib_extract_xml_data(w, fp);
1147 if (fclose(fp) != 0) {
1148 imagex_error("Failed to close the file `%s'",
1158 wimlib_print_available_images(w, image);
1161 if (total_parts != 1 && part_number != 1) {
1162 imagex_error("Select part 1 of this %d-part WIM "
1163 "to see the image metadata",
1165 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
1167 ret = wimlib_print_metadata(w, image);
1172 if (total_parts != 1) {
1173 imagex_error("Modifying a split WIM is not supported.");
1176 if (image == WIM_ALL_IMAGES)
1179 if (image == WIM_NO_IMAGE && new_name) {
1180 imagex_error("Cannot specify new_name (`%s') when "
1181 "using image 0", new_name);
1186 if (image == wimlib_get_boot_idx(w)) {
1187 printf("Image %d is already marked as "
1188 "bootable.\n", image);
1191 printf("Marking image %d as bootable.\n",
1193 wimlib_set_boot_idx(w, image);
1197 if (strcmp(wimlib_get_image_name(w, image),
1199 printf("Image %d is already named \"%s\".\n",
1203 printf("Changing the name of image %d to "
1204 "\"%s\".\n", image, new_name);
1205 ret = wimlib_set_image_name(w, image, new_name);
1211 const char *old_desc;
1212 old_desc = wimlib_get_image_description(w, image);
1213 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1214 printf("The description of image %d is already "
1215 "\"%s\".\n", image, new_desc);
1218 printf("Changing the description of image %d "
1219 "to \"%s\".\n", image, new_desc);
1220 ret = wimlib_set_image_descripton(w, image,
1227 /* Only call wimlib_overwrite_xml_and_header() if something
1228 * actually needs to be changed. */
1229 if (boot || new_name || new_desc ||
1230 check != wimlib_has_integrity_table(w)) {
1232 ret = wimlib_overwrite_xml_and_header(w, check ?
1233 WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
1234 WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
1236 printf("The file `%s' was not modified because nothing "
1237 "needed to be done.\n", wimfile);
1247 /* Join split WIMs into one part WIM */
1248 static int imagex_join(int argc, const char **argv)
1251 int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1252 const char *output_path;
1254 for_opt(c, join_options) {
1257 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1267 imagex_error("Must specify at least one split WIM (.swm) parts "
1271 output_path = argv[0];
1272 return wimlib_join(++argv, --argc, output_path, flags);
1278 /* Mounts an image using a FUSE mount. */
1279 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1282 int mount_flags = 0;
1283 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
1284 WIMLIB_OPEN_FLAG_SPLIT_OK;
1285 const char *wimfile;
1291 const char *swm_glob = NULL;
1292 WIMStruct **additional_swms = NULL;
1293 unsigned num_additional_swms = 0;
1295 if (strcmp(argv[0], "mountrw") == 0)
1296 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1297 for_opt(c, mount_options) {
1300 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1303 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1306 if (strcasecmp(optarg, "none") == 0)
1307 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1308 else if (strcasecmp(optarg, "xattr") == 0)
1309 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1310 else if (strcasecmp(optarg, "windows") == 0)
1311 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1313 imagex_error("Unknown stream interface \"%s\"", optarg);
1326 if (argc != 2 && argc != 3)
1331 ret = wimlib_open_wim(wimfile, open_flags, &w);
1336 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1338 &num_additional_swms);
1345 num_images = wimlib_get_num_images(w);
1346 if (num_images != 1) {
1347 imagex_error("The file `%s' contains %d images; Please "
1348 "select one", wimfile, num_images);
1349 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1351 ret = WIMLIB_ERR_INVALID_IMAGE;
1356 image = wimlib_resolve_image(w, argv[1]);
1360 ret = verify_image_exists_and_is_single(image);
1364 ret = wimlib_mount(w, image, dir, mount_flags, additional_swms,
1365 num_additional_swms);
1367 imagex_error("Failed to mount image %d from `%s' on `%s'",
1368 image, wimfile, dir);
1373 if (additional_swms)
1374 for (unsigned i = 0; i < num_additional_swms; i++)
1375 wimlib_free(additional_swms[i]);
1378 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1383 /* Split a WIM into a spanned set */
1384 static int imagex_split(int argc, const char **argv)
1387 int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1388 unsigned long part_size;
1390 for_opt(c, split_options) {
1393 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1407 part_size = strtoul(argv[2], NULL, 10) * (1 << 20);
1408 return wimlib_split(argv[0], argv[1], part_size, flags);
1411 /* Unmounts an image. */
1412 static int imagex_unmount(int argc, const char **argv)
1415 int unmount_flags = 0;
1418 for_opt(c, unmount_options) {
1421 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1424 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1438 ret = wimlib_unmount(argv[0], unmount_flags);
1440 imagex_error("Failed to unmount `%s'", argv[0]);
1444 struct imagex_command {
1446 int (*func)(int , const char **);
1451 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1452 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1454 static struct imagex_command imagex_commands[] = {
1455 {"append", imagex_append, APPEND},
1456 {"apply", imagex_apply, APPLY},
1457 {"capture", imagex_capture, CAPTURE},
1458 {"delete", imagex_delete, DELETE},
1459 {"dir", imagex_dir, DIR},
1460 {"export", imagex_export, EXPORT},
1461 {"info", imagex_info, INFO},
1462 {"join", imagex_join, JOIN},
1463 {"mount", imagex_mount_rw_or_ro, MOUNT},
1464 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1465 {"split", imagex_split, SPLIT},
1466 {"unmount", imagex_unmount, UNMOUNT},
1469 static void version()
1471 static const char *s =
1472 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
1473 "Copyright (C) 2012 Eric Biggers\n"
1474 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
1475 "This is free software: you are free to change and redistribute it.\n"
1476 "There is NO WARRANTY, to the extent permitted by law.\n"
1478 "Report bugs to "PACKAGE_BUGREPORT".\n";
1483 static void help_or_version(int argc, const char **argv)
1487 struct imagex_command *cmd;
1489 for (i = 1; i < argc; i++) {
1497 if (strcmp(p, "help") == 0 || (*p == '?' && *(p + 1) == '\0')) {
1498 for_imagex_command(cmd) {
1499 if (strcmp(cmd->name, argv[1]) == 0) {
1507 if (strcmp(p, "version") == 0) {
1515 static void usage(int cmd_type)
1517 struct imagex_command *cmd;
1518 puts("IMAGEX: Usage:");
1519 fputs(usage_strings[cmd_type], stdout);
1520 for_imagex_command(cmd)
1521 if (cmd->cmd == cmd_type)
1522 printf("\nTry `man imagex-%s' for more details.\n",
1526 static void usage_all()
1528 puts("IMAGEX: Usage:");
1529 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
1530 fputs(usage_strings[i], stdout);
1531 static const char *extra =
1533 " imagex --version\n"
1535 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
1537 " Try `man imagex' for more information.\n"
1539 fputs(extra, stdout);
1543 int main(int argc, const char **argv)
1545 struct imagex_command *cmd;
1549 imagex_error("No command specified");
1554 help_or_version(argc, argv);
1558 wimlib_set_print_errors(true);
1560 for_imagex_command(cmd) {
1561 if (strcmp(cmd->name, *argv) == 0) {
1562 ret = cmd->func(argc, argv);
1564 imagex_error("Exiting with error code %d:\n"
1566 wimlib_get_error_string(ret));
1567 if (ret == WIMLIB_ERR_NTFS_3G)
1568 imagex_error_with_errno("errno");
1574 imagex_error("Unrecognized command: `%s'", argv[0]);
1578 /*#ifndef WITH_NTFS_3G*/
1579 /*ERROR("wimlib was not compiled with support for NTFS-3g, so we cannot extract");*/
1580 /*ERROR("a WIM to a NTFS filesystem while preserving NTFS-specific metadata.");*/
1581 /*ERROR("Please apply the WIM to a directory rather than a block device, ");*/
1582 /*ERROR("and without the NTFS flag; or compile in support for NTFS-3g.");*/
1583 /*return WIMLIB_ERR_UNSUPPORTED;*/