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/>.
34 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
36 #define swap(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
39 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (char**)argv, "", \
58 static const char *path_basename(const char *path)
65 /* Trailing slashes. */
66 while ((p != path - 1) && *p == '/')
69 while ((p != path - 1) && *p != '/')
76 static const char *usage_strings[] = {
78 " imagex append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
79 " [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
80 " [--verbose] [--dereference] [--config=FILE]\n",
82 " imagex apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
83 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
84 " [--symlink] [--verbose]\n",
86 " imagex capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
87 " [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
88 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
91 " imagex delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n",
93 " imagex dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n",
95 " imagex export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
96 " DEST_WIMFILE [DEST_IMAGE_NAME]\n"
97 " [DEST_IMAGE_DESCRIPTION] [--boot] [--check]\n"
98 " [--compress=TYPE]\n",
100 " imagex info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
101 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
102 " [--xml] [--extract-xml FILE] [--metadata]\n",
104 " imagex join [--check] WIMFILE SPLIT_WIM...\n",
106 " imagex mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
107 " [--check] [--debug] [--streams-interface=INTERFACE]\n",
109 " imagex mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
110 " [--check] [--debug] [--streams-interface=INTERFACE]\n",
112 " imagex split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n",
114 " imagex unmount DIRECTORY [--commit] [--check]\n",
117 static const struct option common_options[] = {
118 {"help", 0, NULL, 'h'},
119 {"version", 0, NULL, 'v'},
123 static const struct option append_options[] = {
124 {"boot", no_argument, NULL, 'b'},
125 {"check", no_argument, NULL, 'c'},
126 {"config", required_argument, NULL, 'C'},
127 {"dereference", no_argument, NULL, 'L'},
128 {"flags", required_argument, NULL, 'f'},
129 {"verbose", no_argument, NULL, 'v'},
132 static const struct option apply_options[] = {
133 {"check", no_argument, NULL, 'c'},
134 {"hardlink", no_argument, NULL, 'h'},
135 {"symlink", no_argument, NULL, 's'},
136 {"verbose", no_argument, NULL, 'v'},
139 static const struct option capture_options[] = {
140 {"boot", no_argument, NULL, 'b'},
141 {"check", no_argument, NULL, 'c'},
142 {"compress", required_argument, NULL, 'x'},
143 {"config", required_argument, NULL, 'C'},
144 {"dereference", no_argument, NULL, 'L'},
145 {"flags", required_argument, NULL, 'f'},
146 {"verbose", no_argument, NULL, 'v'},
149 static const struct option delete_options[] = {
150 {"check", no_argument, NULL, 'c'},
154 static const struct option export_options[] = {
155 {"boot", no_argument, NULL, 'b'},
156 {"check", no_argument, NULL, 'c'},
157 {"compress", required_argument, NULL, 'x'},
161 static const struct option info_options[] = {
162 {"boot", no_argument, NULL, 'b'},
163 {"check", no_argument, NULL, 'c'},
164 {"extract-xml", required_argument, NULL, 'X'},
165 {"header", no_argument, NULL, 'h'},
166 {"lookup-table", no_argument, NULL, 'l'},
167 {"metadata", no_argument, NULL, 'm'},
168 {"xml", no_argument, NULL, 'x'},
172 static const struct option join_options[] = {
173 {"check", no_argument, NULL, 'c'},
177 static const struct option mount_options[] = {
178 {"check", no_argument, NULL, 'c'},
179 {"debug", no_argument, NULL, 'd'},
180 {"streams-interface", required_argument, NULL, 's'},
184 static const struct option split_options[] = {
185 {"check", no_argument, NULL, 'c'},
189 static const struct option unmount_options[] = {
190 {"commit", no_argument, NULL, 'c'},
191 {"check", no_argument, NULL, 'C'},
196 /* Print formatted error message to stderr. */
197 static void imagex_error(const char *format, ...)
200 va_start(va, format);
201 fputs("ERROR: ", stderr);
202 vfprintf(stderr, format, va);
207 /* Print formatted error message to stderr. */
208 static void imagex_error_with_errno(const char *format, ...)
210 int errno_save = errno;
212 va_start(va, format);
213 fputs("ERROR: ", stderr);
214 vfprintf(stderr, format, va);
215 fprintf(stderr, ": %s\n", strerror(errno_save));
220 static inline void version()
222 static const char *s =
223 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
224 "Copyright (C) 2012 Eric Biggers\n"
225 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
226 "This is free software: you are free to change and redistribute it.\n"
227 "There is NO WARRANTY, to the extent permitted by law.\n"
229 "Report bugs to "PACKAGE_BUGREPORT".\n";
233 static inline void usage(int cmd)
235 puts("IMAGEX: Usage:");
236 fputs(usage_strings[cmd], stdout);
239 static void usage_all()
241 puts("IMAGEX: Usage:");
242 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
243 fputs(usage_strings[i], stdout);
244 static const char *extra =
246 " imagex --version\n"
248 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
250 fputs(extra, stdout);
253 static int verify_image_exists(int image)
255 if (image == WIM_NO_IMAGE) {
256 imagex_error("Not a valid image");
257 return WIMLIB_ERR_INVALID_IMAGE;
262 static int verify_image_is_single(int image)
264 if (image == WIM_ALL_IMAGES) {
265 imagex_error("Cannot specify all images for this action");
266 return WIMLIB_ERR_INVALID_IMAGE;
271 static int verify_image_exists_and_is_single(int image)
274 ret = verify_image_exists(image);
276 ret = verify_image_is_single(image);
280 static int get_compression_type(const char *optarg)
282 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
283 return WIM_COMPRESSION_TYPE_LZX;
284 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
285 return WIM_COMPRESSION_TYPE_XPRESS;
286 else if (strcasecmp(optarg, "none") == 0)
287 return WIM_COMPRESSION_TYPE_NONE;
289 imagex_error("Invalid compression type `%s'! Must be "
290 "\"maximum\", \"fast\", or \"none\".", optarg);
291 return WIM_COMPRESSION_TYPE_INVALID;
295 static const char *file_get_contents(const char *filename, size_t *len_ret)
302 if (stat(filename, &stbuf) != 0) {
303 imagex_error_with_errno("Failed to stat the file `%s'", filename);
308 fp = fopen(filename, "rb");
310 imagex_error_with_errno("Failed to open the file `%s'", filename);
316 imagex_error("Failed to allocate buffer of %zu bytes to hold "
317 "contents of file `%s'", len, filename);
320 if (fread(buf, 1, len, fp) != len) {
321 imagex_error_with_errno("Failed to read %lu bytes from the "
322 "file `%s'", len, filename);
334 static int imagex_append(int argc, const char **argv)
337 const char *flags_element = NULL;
338 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
339 int add_image_flags = 0;
340 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
345 const char *config_file = NULL;
346 const char *config_str = NULL;
347 size_t config_len = 0;
352 for_opt(c, append_options) {
355 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
358 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
359 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
362 config_file = optarg;
365 flags_element = optarg;
368 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
371 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
380 if (argc < 2 || argc > 4) {
386 name = (argc >= 3) ? argv[2] : path_basename(dir);
387 desc = (argc >= 4) ? argv[3] : NULL;
390 config_str = file_get_contents(config_file, &config_len);
395 ret = wimlib_open_wim(wimfile, open_flags, &w);
402 ret = stat(dir, &stbuf);
404 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
405 const char *ntfs_device = dir;
406 printf("Capturing WIM image NTFS filesystem on `%s'\n",
408 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
416 if (errno != -ENOENT)
417 imagex_error_with_errno("Failed to stat `%s'", dir);
420 ret = wimlib_add_image(w, dir, name, config_str, config_len,
426 cur_image = wimlib_get_num_images(w);
428 ret = wimlib_set_image_descripton(w, cur_image, desc);
433 ret = wimlib_set_image_flags(w, cur_image, flags_element);
437 ret = wimlib_overwrite(w, write_flags);
443 /* Extract one image, or all images, from a WIM file into a directory. */
444 static int imagex_apply(int argc, const char **argv)
447 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
454 const char *image_num_or_name;
455 int extract_flags = 0;
457 for_opt(c, apply_options) {
460 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
463 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
466 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
469 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
478 if (argc != 2 && argc != 3) {
485 image_num_or_name = "1";
488 image_num_or_name = argv[1];
492 ret = wimlib_open_wim(wimfile, open_flags, &w);
496 image = wimlib_resolve_image(w, image_num_or_name);
497 ret = verify_image_exists(image);
501 num_images = wimlib_get_num_images(w);
502 if (argc == 2 && num_images != 1) {
503 imagex_error("`%s' contains %d images; Please select one "
504 "(or all)", wimfile, num_images);
513 ret = stat(dir, &stbuf);
515 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
516 const char *ntfs_device = dir;
517 printf("Applying image %d of `%s' to NTFS filesystem on `%s'\n",
518 image, wimfile, ntfs_device);
519 ret = wimlib_apply_image_to_ntfs_volume(w, image,
525 if (errno != -ENOENT)
526 imagex_error_with_errno("Failed to stat `%s'", dir);
530 ret = wimlib_extract_image(w, image, dir, extract_flags);
537 /* Create a WIM file from a directory. */
538 static int imagex_capture(int argc, const char **argv)
541 int add_image_flags = 0;
542 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
543 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
544 const char *flags_element = NULL;
549 const char *config_file = NULL;
550 const char *config_str = NULL;
551 size_t config_len = 0;
556 for_opt(c, capture_options) {
559 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
562 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
565 config_file = optarg;
568 compression_type = get_compression_type(optarg);
569 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
573 flags_element = optarg;
576 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
577 write_flags |= WIMLIB_WRITE_FLAG_VERBOSE;
580 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
590 if (argc < 2 || argc > 4) {
596 name = (argc >= 3) ? argv[2] : dir;
597 desc = (argc >= 4) ? argv[3] : NULL;
600 config_str = file_get_contents(config_file, &config_len);
605 ret = wimlib_create_new_wim(compression_type, &w);
612 ret = stat(dir, &stbuf);
614 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
615 const char *ntfs_device = dir;
616 printf("Capturing WIM image NTFS filesystem on `%s'\n",
618 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
626 if (errno != -ENOENT)
627 imagex_error_with_errno("Failed to stat `%s'", dir);
630 ret = wimlib_add_image(w, dir, name, config_str,
631 config_len, add_image_flags);
635 imagex_error("Failed to add the image `%s'", dir);
638 cur_image = wimlib_get_num_images(w);
640 ret = wimlib_set_image_descripton(w, cur_image, desc);
645 ret = wimlib_set_image_flags(w, cur_image, flags_element);
650 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
652 imagex_error("Failed to write the WIM file `%s'", wimfile);
658 /* Remove image(s) from a WIM. */
659 static int imagex_delete(int argc, const char **argv)
662 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
663 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
665 const char *image_num_or_name;
670 for_opt(c, delete_options) {
673 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
674 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
686 imagex_error("Must specify a WIM file");
688 imagex_error("Must specify an image");
693 image_num_or_name = argv[1];
695 ret = wimlib_open_wim(wimfile, open_flags, &w);
699 image = wimlib_resolve_image(w, image_num_or_name);
701 ret = verify_image_exists(image);
705 ret = wimlib_delete_image(w, image);
707 imagex_error("Failed to delete image from `%s'", wimfile);
711 ret = wimlib_overwrite(w, write_flags);
713 imagex_error("Failed to write the file `%s' with image "
721 /* Print the files contained in an image(s) in a WIM file. */
722 static int imagex_dir(int argc, const char **argv)
732 imagex_error("Must specify a WIM file");
737 imagex_error("Too many arguments");
743 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
747 part_number = wimlib_get_part_number(w, NULL);
748 if (part_number != 1) {
749 imagex_error("`%s' is part %d of a split WIM! Specify the "
750 "first part to see the files",
751 wimfile, part_number);
752 ret = WIMLIB_ERR_SPLIT_UNSUPPORTED;
757 image = wimlib_resolve_image(w, argv[2]);
758 ret = verify_image_exists(image);
762 /* Image was not specified. If the WIM only contains one image,
763 * choose that one; otherwise, print an error. */
764 num_images = wimlib_get_num_images(w);
765 if (num_images != 1) {
766 imagex_error("The file `%s' contains %d images; Please "
767 "select one.", wimfile, num_images);
775 ret = wimlib_print_files(w, image);
781 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
783 static int imagex_export(int argc, const char **argv)
786 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
787 int export_flags = 0;
788 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
789 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
790 bool compression_type_specified = false;
791 const char *src_wimfile;
792 const char *src_image_num_or_name;
793 const char *dest_wimfile;
794 const char *dest_name;
795 const char *dest_desc;
796 WIMStruct *src_w = NULL;
797 WIMStruct *dest_w = NULL;
803 for_opt(c, export_options) {
806 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
809 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
810 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
813 compression_type = get_compression_type(optarg);
814 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
816 compression_type_specified = true;
825 if (argc < 3 || argc > 5) {
829 src_wimfile = argv[0];
830 src_image_num_or_name = argv[1];
831 dest_wimfile = argv[2];
832 dest_name = (argc >= 4) ? argv[3] : NULL;
833 dest_desc = (argc >= 5) ? argv[4] : NULL;
834 ret = wimlib_open_wim(src_wimfile, open_flags, &src_w);
838 /* Determine if the destination is an existing file or not.
839 * If so, we try to append the exported image(s) to it; otherwise, we
840 * create a new WIM containing the exported image(s). */
841 if (stat(dest_wimfile, &stbuf) == 0) {
843 /* Destination file exists. */
844 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
845 imagex_error("`%s' is not a regular file",
849 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
853 if (compression_type_specified && compression_type !=
854 wimlib_get_compression_type(dest_w)) {
855 imagex_error("Cannot specify a compression type that is "
856 "not the same as that used in the "
861 compression_type = wimlib_get_compression_type(dest_w);
864 /* dest_wimfile is not an existing file, so create a new WIM. */
865 if (errno == ENOENT) {
866 ret = wimlib_create_new_wim(compression_type, &dest_w);
870 imagex_error_with_errno("Cannot stat file `%s'",
876 image = wimlib_resolve_image(src_w, src_image_num_or_name);
877 ret = verify_image_exists(image);
881 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
888 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
891 ret = wimlib_overwrite(dest_w, write_flags);
898 /* Prints information about a WIM file; also can mark an image as bootable,
899 * change the name of an image, or change the description of an image. */
900 static int imagex_info(int argc, const char **argv)
906 bool lookup_table = false;
908 bool metadata = false;
909 bool short_header = true;
910 const char *xml_out_file = NULL;
912 const char *image_num_or_name = "all";
913 const char *new_name = NULL;
914 const char *new_desc = NULL;
919 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
920 WIMLIB_OPEN_FLAG_SPLIT_OK;
924 for_opt(c, info_options) {
934 short_header = false;
938 short_header = false;
942 short_header = false;
945 xml_out_file = optarg;
946 short_header = false;
950 short_header = false;
960 if (argc == 0 || argc > 4) {
966 image_num_or_name = argv[1];
976 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
978 ret = wimlib_open_wim(wimfile, open_flags, &w);
982 part_number = wimlib_get_part_number(w, &total_parts);
984 /*if (total_parts > 1 && part_number > 1) {*/
985 /*printf("Warning: this is part %d of a %d-part split WIM.\n"*/
986 /*" Select the first part if you want to see information\n"*/
987 /*" about images in the WIM.\n", */
988 /*part_number, total_parts);*/
991 image = wimlib_resolve_image(w, image_num_or_name);
992 if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
993 imagex_error("The image `%s' does not exist",
996 imagex_error("If you would like to set the boot "
997 "index to 0, specify image \"0\" with "
999 ret = WIMLIB_ERR_INVALID_IMAGE;
1003 if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
1005 imagex_error("Cannot specify the --boot flag "
1006 "without specifying a specific "
1007 "image in a multi-image WIM");
1008 ret = WIMLIB_ERR_INVALID_IMAGE;
1012 imagex_error("Cannot specify the NEW_NAME "
1013 "without specifying a specific "
1014 "image in a multi-image WIM");
1015 ret = WIMLIB_ERR_INVALID_IMAGE;
1020 /* Operations that print information are separated from operations that
1021 * recreate the WIM file. */
1022 if (!new_name && !boot) {
1024 if (image == WIM_NO_IMAGE) {
1025 imagex_error("`%s' is not a valid image",
1027 ret = WIMLIB_ERR_INVALID_IMAGE;
1031 if (image == WIM_ALL_IMAGES && short_header)
1032 wimlib_print_wim_information(w);
1035 wimlib_print_header(w);
1038 if (total_parts != 1) {
1039 printf("Warning: Only showing the lookup table "
1040 "for part %d of a %d-part WIM.\n",
1041 part_number, total_parts);
1043 wimlib_print_lookup_table(w);
1047 ret = wimlib_extract_xml_data(w, stdout);
1053 fp = fopen(xml_out_file, "wb");
1055 imagex_error_with_errno("Failed to open the "
1061 ret = wimlib_extract_xml_data(w, fp);
1062 if (fclose(fp) != 0) {
1063 imagex_error("Failed to close the file `%s'",
1073 wimlib_print_available_images(w, image);
1076 if (total_parts != 1 && part_number != 1) {
1077 imagex_error("Select part 1 of this %d-part WIM "
1078 "to see the image metadata",
1080 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
1082 ret = wimlib_print_metadata(w, image);
1087 if (total_parts != 1) {
1088 imagex_error("Modifying a split WIM is not supported.");
1091 if (image == WIM_ALL_IMAGES)
1094 if (image == WIM_NO_IMAGE && new_name) {
1095 imagex_error("Cannot specify new_name (`%s') when "
1096 "using image 0", new_name);
1101 if (image == wimlib_get_boot_idx(w)) {
1102 printf("Image %d is already marked as "
1103 "bootable.\n", image);
1106 printf("Marking image %d as bootable.\n",
1108 wimlib_set_boot_idx(w, image);
1112 if (strcmp(wimlib_get_image_name(w, image),
1114 printf("Image %d is already named \"%s\".\n",
1118 printf("Changing the name of image %d to "
1119 "\"%s\".\n", image, new_name);
1120 ret = wimlib_set_image_name(w, image, new_name);
1126 const char *old_desc;
1127 old_desc = wimlib_get_image_description(w, image);
1128 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1129 printf("The description of image %d is already "
1130 "\"%s\".\n", image, new_desc);
1133 printf("Changing the description of image %d "
1134 "to \"%s\".\n", image, new_desc);
1135 ret = wimlib_set_image_descripton(w, image,
1142 /* Only call wimlib_overwrite_xml_and_header() if something
1143 * actually needs to be changed. */
1144 if (boot || new_name || new_desc ||
1145 check != wimlib_has_integrity_table(w)) {
1147 ret = wimlib_overwrite_xml_and_header(w, check ?
1148 WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
1149 WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
1151 printf("The file `%s' was not modified because nothing "
1152 "needed to be done.\n", wimfile);
1162 /* Join split WIMs into one part WIM */
1163 static int imagex_join(int argc, const char **argv)
1166 int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1167 const char *output_path;
1169 for_opt(c, join_options) {
1172 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1182 imagex_error("Must specify at least one split WIM (.swm) parts "
1186 output_path = argv[0];
1187 return wimlib_join(++argv, --argc, output_path, flags);
1193 /* Mounts an image using a FUSE mount. */
1194 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1197 int mount_flags = 0;
1198 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1199 const char *wimfile;
1206 if (strcmp(argv[0], "mountrw") == 0)
1207 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1208 for_opt(c, mount_options) {
1211 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1214 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1217 if (strcasecmp(optarg, "none") == 0)
1218 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1219 else if (strcasecmp(optarg, "xattr") == 0)
1220 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1221 else if (strcasecmp(optarg, "windows") == 0)
1222 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1224 imagex_error("Unknown stream interface \"%s\"", optarg);
1234 if (argc != 2 && argc != 3)
1239 ret = wimlib_open_wim(wimfile, open_flags, &w);
1245 num_images = wimlib_get_num_images(w);
1246 if (num_images != 1) {
1247 imagex_error("The file `%s' contains %d images; Please "
1248 "select one", wimfile, num_images);
1249 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1251 ret = WIMLIB_ERR_INVALID_IMAGE;
1256 image = wimlib_resolve_image(w, argv[1]);
1260 ret = verify_image_exists_and_is_single(image);
1264 ret = wimlib_mount(w, image, dir, mount_flags);
1266 imagex_error("Failed to mount image %d from `%s' on `%s'",
1267 image, wimfile, dir);
1274 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1279 /* Split a WIM into a spanned set */
1280 static int imagex_split(int argc, const char **argv)
1283 int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1284 unsigned long part_size;
1286 for_opt(c, split_options) {
1289 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1303 part_size = strtoul(argv[2], NULL, 10) * (1 << 20);
1304 return wimlib_split(argv[0], argv[1], part_size, flags);
1307 /* Unmounts an image. */
1308 static int imagex_unmount(int argc, const char **argv)
1311 int unmount_flags = 0;
1314 for_opt(c, unmount_options) {
1317 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1320 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1334 ret = wimlib_unmount(argv[0], unmount_flags);
1336 imagex_error("Failed to unmount `%s'", argv[0]);
1340 struct imagex_command {
1342 int (*func)(int , const char **);
1346 static struct imagex_command imagex_commands[] = {
1347 {"append", imagex_append, APPEND},
1348 {"apply", imagex_apply, APPLY},
1349 {"capture", imagex_capture, CAPTURE},
1350 {"delete", imagex_delete, DELETE},
1351 {"dir", imagex_dir, DIR},
1352 {"export", imagex_export, EXPORT},
1353 {"info", imagex_info, INFO},
1354 {"join", imagex_join, JOIN},
1355 {"mount", imagex_mount_rw_or_ro, MOUNT},
1356 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1357 {"split", imagex_split, SPLIT},
1358 {"unmount", imagex_unmount, UNMOUNT},
1361 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1362 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1364 static void help_or_version(int argc, const char **argv)
1368 struct imagex_command *cmd;
1370 for (i = 1; i < argc; i++) {
1378 if (strcmp(p, "help") == 0 || (*p == '?' && *(p + 1) == '\0')) {
1379 for_imagex_command(cmd) {
1380 if (strcmp(cmd->name, argv[1]) == 0) {
1388 if (strcmp(p, "version") == 0) {
1396 int main(int argc, const char **argv)
1398 struct imagex_command *cmd;
1402 imagex_error("No command specified");
1407 help_or_version(argc, argv);
1411 wimlib_set_print_errors(true);
1413 for_imagex_command(cmd) {
1414 if (strcmp(cmd->name, *argv) == 0) {
1415 ret = cmd->func(argc, argv);
1417 imagex_error("Exiting with error code %d:\n"
1419 wimlib_get_error_string(ret));
1420 if (ret == WIMLIB_ERR_NTFS_3G)
1421 imagex_error_with_errno("errno");
1427 imagex_error("Unrecognized command: `%s'", argv[0]);
1431 /*#ifndef WITH_NTFS_3G*/
1432 /*ERROR("wimlib was not compiled with support for NTFS-3g, so we cannot extract");*/
1433 /*ERROR("a WIM to a NTFS filesystem while preserving NTFS-specific metadata.");*/
1434 /*ERROR("Please apply the WIM to a directory rather than a block device, ");*/
1435 /*ERROR("and without the NTFS flag; or compile in support for NTFS-3g.");*/
1436 /*return WIMLIB_ERR_UNSUPPORTED;*/