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]\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'},
149 static const struct option info_options[] = {
150 {"boot", no_argument, NULL, 'b'},
151 {"check", no_argument, NULL, 'c'},
152 {"extract-xml", required_argument, NULL, 'X'},
153 {"header", no_argument, NULL, 'h'},
154 {"lookup-table", no_argument, NULL, 'l'},
155 {"metadata", no_argument, NULL, 'm'},
156 {"xml", no_argument, NULL, 'x'},
160 static const struct option join_options[] = {
161 {"check", no_argument, NULL, 'c'},
165 static const struct option mount_options[] = {
166 {"check", no_argument, NULL, 'c'},
167 {"debug", no_argument, NULL, 'd'},
168 {"streams-interface", required_argument, NULL, 's'},
169 {"ref", required_argument, NULL, 'r'},
173 static const struct option split_options[] = {
174 {"check", no_argument, NULL, 'c'},
178 static const struct option unmount_options[] = {
179 {"commit", no_argument, NULL, 'c'},
180 {"check", no_argument, NULL, 'C'},
186 /* Print formatted error message to stderr. */
187 static void imagex_error(const char *format, ...)
190 va_start(va, format);
191 fputs("ERROR: ", stderr);
192 vfprintf(stderr, format, va);
197 /* Print formatted error message to stderr. */
198 static void imagex_error_with_errno(const char *format, ...)
200 int errno_save = errno;
202 va_start(va, format);
203 fputs("ERROR: ", stderr);
204 vfprintf(stderr, format, va);
205 fprintf(stderr, ": %s\n", strerror(errno_save));
209 static const char *path_basename(const char *path)
211 const char *p = path;
216 /* Trailing slashes. */
217 while ((p != path - 1) && *p == '/')
220 while ((p != path - 1) && *p != '/')
227 static int verify_image_exists(int image)
229 if (image == WIM_NO_IMAGE) {
230 imagex_error("Not a valid image");
231 return WIMLIB_ERR_INVALID_IMAGE;
236 static int verify_image_is_single(int image)
238 if (image == WIM_ALL_IMAGES) {
239 imagex_error("Cannot specify all images for this action");
240 return WIMLIB_ERR_INVALID_IMAGE;
245 static int verify_image_exists_and_is_single(int image)
248 ret = verify_image_exists(image);
250 ret = verify_image_is_single(image);
254 static int get_compression_type(const char *optarg)
256 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
257 return WIM_COMPRESSION_TYPE_LZX;
258 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
259 return WIM_COMPRESSION_TYPE_XPRESS;
260 else if (strcasecmp(optarg, "none") == 0)
261 return WIM_COMPRESSION_TYPE_NONE;
263 imagex_error("Invalid compression type `%s'! Must be "
264 "\"maximum\", \"fast\", or \"none\".", optarg);
265 return WIM_COMPRESSION_TYPE_INVALID;
269 static const char *file_get_contents(const char *filename, size_t *len_ret)
276 if (stat(filename, &stbuf) != 0) {
277 imagex_error_with_errno("Failed to stat the file `%s'", filename);
282 fp = fopen(filename, "rb");
284 imagex_error_with_errno("Failed to open the file `%s'", filename);
290 imagex_error("Failed to allocate buffer of %zu bytes to hold "
291 "contents of file `%s'", len, filename);
294 if (fread(buf, 1, len, fp) != len) {
295 imagex_error_with_errno("Failed to read %lu bytes from the "
296 "file `%s'", len, filename);
308 static int open_swms_from_glob(const char *swm_glob,
309 const char *first_part,
311 WIMStruct ***additional_swms_ret,
312 unsigned *num_additional_swms_ret)
314 unsigned num_additional_swms = 0;
315 WIMStruct **additional_swms = NULL;
319 ret = glob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
321 if (ret == GLOB_NOMATCH) {
322 imagex_error("Found no files for glob \"%s\"",
325 imagex_error_with_errno("Failed to process glob "
331 num_additional_swms = globbuf.gl_pathc;
332 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
333 if (!additional_swms) {
334 imagex_error("Out of memory");
339 for (size_t i = 0; i < num_additional_swms; i++) {
340 if (strcmp(globbuf.gl_pathv[i], first_part) == 0) {
344 ret = wimlib_open_wim(globbuf.gl_pathv[i],
345 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
346 &additional_swms[i - offset]);
350 *additional_swms_ret = additional_swms;
351 *num_additional_swms_ret = num_additional_swms - offset;
355 for (unsigned i = 0; i < num_additional_swms; i++)
356 wimlib_free(additional_swms[i]);
357 free(additional_swms);
364 static int imagex_append(int argc, const char **argv)
367 const char *flags_element = NULL;
368 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
369 int add_image_flags = 0;
370 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
375 const char *config_file = NULL;
376 const char *config_str = NULL;
377 size_t config_len = 0;
382 for_opt(c, append_options) {
385 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
388 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
389 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
392 config_file = optarg;
395 flags_element = optarg;
398 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
401 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
410 if (argc < 2 || argc > 4) {
416 name = (argc >= 3) ? argv[2] : path_basename(dir);
417 desc = (argc >= 4) ? argv[3] : NULL;
420 config_str = file_get_contents(config_file, &config_len);
425 ret = wimlib_open_wim(wimfile, open_flags, &w);
432 ret = stat(dir, &stbuf);
434 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
435 const char *ntfs_device = dir;
436 printf("Capturing WIM image NTFS filesystem on `%s'\n",
438 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
447 imagex_error_with_errno("Failed to stat `%s'", dir);
450 ret = wimlib_add_image(w, dir, name, config_str, config_len,
456 cur_image = wimlib_get_num_images(w);
458 ret = wimlib_set_image_descripton(w, cur_image, desc);
463 ret = wimlib_set_image_flags(w, cur_image, flags_element);
467 ret = wimlib_overwrite(w, write_flags);
473 /* Extract one image, or all images, from a WIM file into a directory. */
474 static int imagex_apply(int argc, const char **argv)
477 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
478 WIMLIB_OPEN_FLAG_SPLIT_OK;
485 const char *image_num_or_name;
486 int extract_flags = 0;
488 const char *swm_glob = NULL;
489 WIMStruct **additional_swms = NULL;
490 unsigned num_additional_swms = 0;
492 for_opt(c, apply_options) {
495 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
498 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
501 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
504 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
516 if (argc != 2 && argc != 3) {
523 image_num_or_name = "1";
526 image_num_or_name = argv[1];
530 ret = wimlib_open_wim(wimfile, open_flags, &w);
534 image = wimlib_resolve_image(w, image_num_or_name);
535 ret = verify_image_exists(image);
539 num_images = wimlib_get_num_images(w);
540 if (argc == 2 && num_images != 1) {
541 imagex_error("`%s' contains %d images; Please select one "
542 "(or all)", wimfile, num_images);
549 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
551 &num_additional_swms);
559 ret = stat(dir, &stbuf);
561 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
562 const char *ntfs_device = dir;
563 printf("Applying image %d of `%s' to NTFS filesystem on `%s'\n",
564 image, wimfile, ntfs_device);
565 ret = wimlib_apply_image_to_ntfs_volume(w, image,
569 num_additional_swms);
574 imagex_error_with_errno("Failed to stat `%s'", dir);
578 ret = wimlib_extract_image(w, image, dir, extract_flags,
579 additional_swms, num_additional_swms);
583 for (size_t i = 0; i < num_additional_swms; i++)
584 wimlib_free(additional_swms[i]);
589 /* Create a WIM file from a directory. */
590 static int imagex_capture(int argc, const char **argv)
593 int add_image_flags = 0;
594 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
595 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
596 const char *flags_element = NULL;
601 const char *config_file = NULL;
602 const char *config_str = NULL;
603 size_t config_len = 0;
608 for_opt(c, capture_options) {
611 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
614 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
617 config_file = optarg;
620 compression_type = get_compression_type(optarg);
621 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
625 flags_element = optarg;
628 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
629 write_flags |= WIMLIB_WRITE_FLAG_VERBOSE;
632 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
642 if (argc < 2 || argc > 4) {
648 name = (argc >= 3) ? argv[2] : dir;
649 desc = (argc >= 4) ? argv[3] : NULL;
652 config_str = file_get_contents(config_file, &config_len);
657 ret = wimlib_create_new_wim(compression_type, &w);
664 ret = stat(dir, &stbuf);
666 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
667 const char *ntfs_device = dir;
668 printf("Capturing WIM image NTFS filesystem on `%s'\n",
670 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
679 imagex_error_with_errno("Failed to stat `%s'", dir);
682 ret = wimlib_add_image(w, dir, name, config_str,
683 config_len, add_image_flags);
687 imagex_error("Failed to add the image `%s'", dir);
690 cur_image = wimlib_get_num_images(w);
692 ret = wimlib_set_image_descripton(w, cur_image, desc);
697 ret = wimlib_set_image_flags(w, cur_image, flags_element);
702 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
704 imagex_error("Failed to write the WIM file `%s'", wimfile);
710 /* Remove image(s) from a WIM. */
711 static int imagex_delete(int argc, const char **argv)
714 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
715 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
717 const char *image_num_or_name;
722 for_opt(c, delete_options) {
725 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
726 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
738 imagex_error("Must specify a WIM file");
740 imagex_error("Must specify an image");
745 image_num_or_name = argv[1];
747 ret = wimlib_open_wim(wimfile, open_flags, &w);
751 image = wimlib_resolve_image(w, image_num_or_name);
753 ret = verify_image_exists(image);
757 ret = wimlib_delete_image(w, image);
759 imagex_error("Failed to delete image from `%s'", wimfile);
763 ret = wimlib_overwrite(w, write_flags);
765 imagex_error("Failed to write the file `%s' with image "
773 /* Print the files contained in an image(s) in a WIM file. */
774 static int imagex_dir(int argc, const char **argv)
784 imagex_error("Must specify a WIM file");
789 imagex_error("Too many arguments");
795 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
799 part_number = wimlib_get_part_number(w, NULL);
800 if (part_number != 1) {
801 imagex_error("`%s' is part %d of a split WIM! Specify the "
802 "first part to see the files",
803 wimfile, part_number);
804 ret = WIMLIB_ERR_SPLIT_UNSUPPORTED;
809 image = wimlib_resolve_image(w, argv[2]);
810 ret = verify_image_exists(image);
814 /* Image was not specified. If the WIM only contains one image,
815 * choose that one; otherwise, print an error. */
816 num_images = wimlib_get_num_images(w);
817 if (num_images != 1) {
818 imagex_error("The file `%s' contains %d images; Please "
819 "select one.", wimfile, num_images);
827 ret = wimlib_print_files(w, image);
833 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
835 static int imagex_export(int argc, const char **argv)
838 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
839 int export_flags = 0;
840 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
841 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
842 bool compression_type_specified = false;
843 const char *src_wimfile;
844 const char *src_image_num_or_name;
845 const char *dest_wimfile;
846 const char *dest_name;
847 const char *dest_desc;
848 WIMStruct *src_w = NULL;
849 WIMStruct *dest_w = NULL;
855 for_opt(c, export_options) {
858 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
861 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
862 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
865 compression_type = get_compression_type(optarg);
866 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
868 compression_type_specified = true;
877 if (argc < 3 || argc > 5) {
881 src_wimfile = argv[0];
882 src_image_num_or_name = argv[1];
883 dest_wimfile = argv[2];
884 dest_name = (argc >= 4) ? argv[3] : NULL;
885 dest_desc = (argc >= 5) ? argv[4] : NULL;
886 ret = wimlib_open_wim(src_wimfile, open_flags, &src_w);
890 /* Determine if the destination is an existing file or not.
891 * If so, we try to append the exported image(s) to it; otherwise, we
892 * create a new WIM containing the exported image(s). */
893 if (stat(dest_wimfile, &stbuf) == 0) {
895 /* Destination file exists. */
896 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
897 imagex_error("`%s' is not a regular file",
901 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
905 if (compression_type_specified && compression_type !=
906 wimlib_get_compression_type(dest_w)) {
907 imagex_error("Cannot specify a compression type that is "
908 "not the same as that used in the "
913 compression_type = wimlib_get_compression_type(dest_w);
916 /* dest_wimfile is not an existing file, so create a new WIM. */
917 if (errno == ENOENT) {
918 ret = wimlib_create_new_wim(compression_type, &dest_w);
922 imagex_error_with_errno("Cannot stat file `%s'",
928 image = wimlib_resolve_image(src_w, src_image_num_or_name);
929 ret = verify_image_exists(image);
933 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
940 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
943 ret = wimlib_overwrite(dest_w, write_flags);
950 /* Prints information about a WIM file; also can mark an image as bootable,
951 * change the name of an image, or change the description of an image. */
952 static int imagex_info(int argc, const char **argv)
958 bool lookup_table = false;
960 bool metadata = false;
961 bool short_header = true;
962 const char *xml_out_file = NULL;
964 const char *image_num_or_name = "all";
965 const char *new_name = NULL;
966 const char *new_desc = NULL;
971 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
972 WIMLIB_OPEN_FLAG_SPLIT_OK;
976 for_opt(c, info_options) {
986 short_header = false;
990 short_header = false;
994 short_header = false;
997 xml_out_file = optarg;
998 short_header = false;
1002 short_header = false;
1012 if (argc == 0 || argc > 4) {
1018 image_num_or_name = argv[1];
1028 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1030 ret = wimlib_open_wim(wimfile, open_flags, &w);
1034 part_number = wimlib_get_part_number(w, &total_parts);
1036 /*if (total_parts > 1 && part_number > 1) {*/
1037 /*printf("Warning: this is part %d of a %d-part split WIM.\n"*/
1038 /*" Select the first part if you want to see information\n"*/
1039 /*" about images in the WIM.\n", */
1040 /*part_number, total_parts);*/
1043 image = wimlib_resolve_image(w, image_num_or_name);
1044 if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
1045 imagex_error("The image `%s' does not exist",
1048 imagex_error("If you would like to set the boot "
1049 "index to 0, specify image \"0\" with "
1050 "the --boot flag.");
1051 ret = WIMLIB_ERR_INVALID_IMAGE;
1055 if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
1057 imagex_error("Cannot specify the --boot flag "
1058 "without specifying a specific "
1059 "image in a multi-image WIM");
1060 ret = WIMLIB_ERR_INVALID_IMAGE;
1064 imagex_error("Cannot specify the NEW_NAME "
1065 "without specifying a specific "
1066 "image in a multi-image WIM");
1067 ret = WIMLIB_ERR_INVALID_IMAGE;
1072 /* Operations that print information are separated from operations that
1073 * recreate the WIM file. */
1074 if (!new_name && !boot) {
1076 if (image == WIM_NO_IMAGE) {
1077 imagex_error("`%s' is not a valid image",
1079 ret = WIMLIB_ERR_INVALID_IMAGE;
1083 if (image == WIM_ALL_IMAGES && short_header)
1084 wimlib_print_wim_information(w);
1087 wimlib_print_header(w);
1090 if (total_parts != 1) {
1091 printf("Warning: Only showing the lookup table "
1092 "for part %d of a %d-part WIM.\n",
1093 part_number, total_parts);
1095 wimlib_print_lookup_table(w);
1099 ret = wimlib_extract_xml_data(w, stdout);
1105 fp = fopen(xml_out_file, "wb");
1107 imagex_error_with_errno("Failed to open the "
1113 ret = wimlib_extract_xml_data(w, fp);
1114 if (fclose(fp) != 0) {
1115 imagex_error("Failed to close the file `%s'",
1125 wimlib_print_available_images(w, image);
1128 if (total_parts != 1 && part_number != 1) {
1129 imagex_error("Select part 1 of this %d-part WIM "
1130 "to see the image metadata",
1132 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
1134 ret = wimlib_print_metadata(w, image);
1139 if (total_parts != 1) {
1140 imagex_error("Modifying a split WIM is not supported.");
1143 if (image == WIM_ALL_IMAGES)
1146 if (image == WIM_NO_IMAGE && new_name) {
1147 imagex_error("Cannot specify new_name (`%s') when "
1148 "using image 0", new_name);
1153 if (image == wimlib_get_boot_idx(w)) {
1154 printf("Image %d is already marked as "
1155 "bootable.\n", image);
1158 printf("Marking image %d as bootable.\n",
1160 wimlib_set_boot_idx(w, image);
1164 if (strcmp(wimlib_get_image_name(w, image),
1166 printf("Image %d is already named \"%s\".\n",
1170 printf("Changing the name of image %d to "
1171 "\"%s\".\n", image, new_name);
1172 ret = wimlib_set_image_name(w, image, new_name);
1178 const char *old_desc;
1179 old_desc = wimlib_get_image_description(w, image);
1180 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1181 printf("The description of image %d is already "
1182 "\"%s\".\n", image, new_desc);
1185 printf("Changing the description of image %d "
1186 "to \"%s\".\n", image, new_desc);
1187 ret = wimlib_set_image_descripton(w, image,
1194 /* Only call wimlib_overwrite_xml_and_header() if something
1195 * actually needs to be changed. */
1196 if (boot || new_name || new_desc ||
1197 check != wimlib_has_integrity_table(w)) {
1199 ret = wimlib_overwrite_xml_and_header(w, check ?
1200 WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
1201 WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
1203 printf("The file `%s' was not modified because nothing "
1204 "needed to be done.\n", wimfile);
1214 /* Join split WIMs into one part WIM */
1215 static int imagex_join(int argc, const char **argv)
1218 int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1219 const char *output_path;
1221 for_opt(c, join_options) {
1224 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1234 imagex_error("Must specify at least one split WIM (.swm) parts "
1238 output_path = argv[0];
1239 return wimlib_join(++argv, --argc, output_path, flags);
1245 /* Mounts an image using a FUSE mount. */
1246 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1249 int mount_flags = 0;
1250 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
1251 WIMLIB_OPEN_FLAG_SPLIT_OK;
1252 const char *wimfile;
1258 const char *swm_glob = NULL;
1259 WIMStruct **additional_swms = NULL;
1260 unsigned num_additional_swms = 0;
1262 if (strcmp(argv[0], "mountrw") == 0)
1263 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1264 for_opt(c, mount_options) {
1267 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1270 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1273 if (strcasecmp(optarg, "none") == 0)
1274 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1275 else if (strcasecmp(optarg, "xattr") == 0)
1276 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1277 else if (strcasecmp(optarg, "windows") == 0)
1278 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1280 imagex_error("Unknown stream interface \"%s\"", optarg);
1293 if (argc != 2 && argc != 3)
1298 ret = wimlib_open_wim(wimfile, open_flags, &w);
1303 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1305 &num_additional_swms);
1312 num_images = wimlib_get_num_images(w);
1313 if (num_images != 1) {
1314 imagex_error("The file `%s' contains %d images; Please "
1315 "select one", wimfile, num_images);
1316 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1318 ret = WIMLIB_ERR_INVALID_IMAGE;
1323 image = wimlib_resolve_image(w, argv[1]);
1327 ret = verify_image_exists_and_is_single(image);
1331 ret = wimlib_mount(w, image, dir, mount_flags, additional_swms,
1332 num_additional_swms);
1334 imagex_error("Failed to mount image %d from `%s' on `%s'",
1335 image, wimfile, dir);
1340 if (additional_swms)
1341 for (unsigned i = 0; i < num_additional_swms; i++)
1342 wimlib_free(additional_swms[i]);
1345 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1350 /* Split a WIM into a spanned set */
1351 static int imagex_split(int argc, const char **argv)
1354 int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1355 unsigned long part_size;
1357 for_opt(c, split_options) {
1360 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1374 part_size = strtoul(argv[2], NULL, 10) * (1 << 20);
1375 return wimlib_split(argv[0], argv[1], part_size, flags);
1378 /* Unmounts an image. */
1379 static int imagex_unmount(int argc, const char **argv)
1382 int unmount_flags = 0;
1385 for_opt(c, unmount_options) {
1388 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1391 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1405 ret = wimlib_unmount(argv[0], unmount_flags);
1407 imagex_error("Failed to unmount `%s'", argv[0]);
1411 struct imagex_command {
1413 int (*func)(int , const char **);
1418 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1419 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1421 static struct imagex_command imagex_commands[] = {
1422 {"append", imagex_append, APPEND},
1423 {"apply", imagex_apply, APPLY},
1424 {"capture", imagex_capture, CAPTURE},
1425 {"delete", imagex_delete, DELETE},
1426 {"dir", imagex_dir, DIR},
1427 {"export", imagex_export, EXPORT},
1428 {"info", imagex_info, INFO},
1429 {"join", imagex_join, JOIN},
1430 {"mount", imagex_mount_rw_or_ro, MOUNT},
1431 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1432 {"split", imagex_split, SPLIT},
1433 {"unmount", imagex_unmount, UNMOUNT},
1436 static void version()
1438 static const char *s =
1439 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
1440 "Copyright (C) 2012 Eric Biggers\n"
1441 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
1442 "This is free software: you are free to change and redistribute it.\n"
1443 "There is NO WARRANTY, to the extent permitted by law.\n"
1445 "Report bugs to "PACKAGE_BUGREPORT".\n";
1450 static void help_or_version(int argc, const char **argv)
1454 struct imagex_command *cmd;
1456 for (i = 1; i < argc; i++) {
1464 if (strcmp(p, "help") == 0 || (*p == '?' && *(p + 1) == '\0')) {
1465 for_imagex_command(cmd) {
1466 if (strcmp(cmd->name, argv[1]) == 0) {
1474 if (strcmp(p, "version") == 0) {
1482 static void usage(int cmd_type)
1484 struct imagex_command *cmd;
1485 puts("IMAGEX: Usage:");
1486 fputs(usage_strings[cmd_type], stdout);
1487 for_imagex_command(cmd)
1488 if (cmd->cmd == cmd_type)
1489 printf("\nTry `man imagex-%s' for more details.\n",
1493 static void usage_all()
1495 puts("IMAGEX: Usage:");
1496 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
1497 fputs(usage_strings[i], stdout);
1498 static const char *extra =
1500 " imagex --version\n"
1502 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
1504 " Try `man imagex' for more information.\n"
1506 fputs(extra, stdout);
1510 int main(int argc, const char **argv)
1512 struct imagex_command *cmd;
1516 imagex_error("No command specified");
1521 help_or_version(argc, argv);
1525 wimlib_set_print_errors(true);
1527 for_imagex_command(cmd) {
1528 if (strcmp(cmd->name, *argv) == 0) {
1529 ret = cmd->func(argc, argv);
1531 imagex_error("Exiting with error code %d:\n"
1533 wimlib_get_error_string(ret));
1534 if (ret == WIMLIB_ERR_NTFS_3G)
1535 imagex_error_with_errno("errno");
1541 imagex_error("Unrecognized command: `%s'", argv[0]);
1545 /*#ifndef WITH_NTFS_3G*/
1546 /*ERROR("wimlib was not compiled with support for NTFS-3g, so we cannot extract");*/
1547 /*ERROR("a WIM to a NTFS filesystem while preserving NTFS-specific metadata.");*/
1548 /*ERROR("Please apply the WIM to a directory rather than a block device, ");*/
1549 /*ERROR("and without the NTFS flag; or compile in support for NTFS-3g.");*/
1550 /*return WIMLIB_ERR_UNSUPPORTED;*/