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/>.
39 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
41 #define swap(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \
44 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (char**)argv, "", \
63 static const char *path_basename(const char *path)
70 /* Trailing slashes. */
71 while ((p != path - 1) && *p == '/')
74 while ((p != path - 1) && *p != '/')
81 static const char *usage_strings[] = {
83 " imagex append (DIRECTORY | NTFS_VOLUME) WIMFILE [\"IMAGE_NAME\"]\n"
84 " [\"DESCRIPTION\"] [--boot] [--check] [--flags EDITIONID]\n"
85 " [--dereference] [--config=FILE]\n",
87 " imagex apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
88 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
89 " [--symlink] [--verbose]\n",
91 " imagex capture (DIRECTORY | NTFS_VOLUME) WIMFILE [\"IMAGE_NAME\"]\n"
92 " [\"DESCRIPTION\"] [--boot] [--check] [--compress[=TYPE]]\n"
93 " [--flags \"EditionID\"] [--verbose] [--dereference]\n"
96 " imagex delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n",
98 " imagex dir WIMFILE (IMAGE_NUM | IMAGE_NAME | \"all\")\n",
100 " imagex export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
101 " DEST_WIMFILE [\"DEST_IMAGE_NAME\"] [\"DEST_IMAGE_DESCRIPTION\"]\n"
102 " [--boot] [--check] [--compress[=TYPE]]\n",
104 " imagex info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
105 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
106 " [--xml] [--extract-xml FILE] [--metadata]\n",
108 " imagex join [--check] WIMFILE SPLIT_WIM...\n",
110 " imagex mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
111 " [--check] [--debug] [--stream-interface=INTERFACE]\n",
113 " imagex mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
114 " [--check] [--debug] [--stream-interface=INTERFACE]\n",
116 " imagex split WIMFILE SPLIT_WIMFILE PART_SIZE [--check]\n",
118 " imagex unmount DIRECTORY [--commit] [--check]\n",
121 static const struct option common_options[] = {
122 {"help", 0, NULL, 'h'},
123 {"version", 0, NULL, 'v'},
127 static const struct option append_options[] = {
128 {"boot", no_argument, NULL, 'b'},
129 {"check", no_argument, NULL, 'c'},
130 {"flags", required_argument, NULL, 'f'},
131 {"verbose", no_argument, NULL, 'v'},
132 {"dereference", no_argument, NULL, 'L'},
133 {"config", required_argument, NULL, 'C'},
136 static const struct option apply_options[] = {
137 {"check", no_argument, NULL, 'c'},
138 {"hardlink", no_argument, NULL, 'h'},
139 {"symlink", no_argument, NULL, 's'},
140 {"verbose", no_argument, NULL, 'v'},
143 static const struct option capture_options[] = {
144 {"boot", no_argument, NULL, 'b'},
145 {"check", no_argument, NULL, 'c'},
146 {"compress", optional_argument, NULL, 'x'},
147 {"flags", required_argument, NULL, 'f'},
148 {"verbose", no_argument, NULL, 'v'},
149 {"dereference", no_argument, NULL, 'L'},
150 {"config", required_argument, NULL, 'C'},
153 static const struct option delete_options[] = {
154 {"check", no_argument, NULL, 'c'},
158 static const struct option export_options[] = {
159 {"boot", no_argument, NULL, 'b'},
160 {"check", no_argument , NULL, 'c'},
161 {"compress", optional_argument, NULL, 'x'},
165 static const struct option info_options[] = {
166 {"boot", no_argument, NULL, 'b'},
167 {"check", no_argument, NULL, 'c'},
168 {"header", no_argument, NULL, 'h'},
169 {"lookup-table", no_argument, NULL, 'l'},
170 {"xml", no_argument, NULL, 'x'},
171 {"extract-xml", required_argument, NULL, 'X'},
172 {"metadata", no_argument, NULL, 'm'},
176 static const struct option join_options[] = {
177 {"check", no_argument, NULL, 'c'},
181 static const struct option mount_options[] = {
182 {"check", no_argument, NULL, 'c'},
183 {"debug", no_argument, NULL, 'd'},
184 {"stream-interface", required_argument, NULL, 's'},
188 static const struct option split_options[] = {
189 {"check", no_argument, NULL, 'c'},
193 static const struct option unmount_options[] = {
194 {"commit", no_argument, NULL, 'c'},
195 {"check", no_argument, NULL, 'C'},
200 /* Print formatted error message to stderr. */
201 static void imagex_error(const char *format, ...)
204 va_start(va, format);
205 fputs("ERROR: ", stderr);
206 vfprintf(stderr, format, va);
211 /* Print formatted error message to stderr. */
212 static void imagex_error_with_errno(const char *format, ...)
214 int errno_save = errno;
216 va_start(va, format);
217 fputs("ERROR: ", stderr);
218 vfprintf(stderr, format, va);
219 fprintf(stderr, ": %s\n", strerror(errno_save));
224 static inline void version()
226 static const char *s =
227 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
228 "Copyright (C) 2012 Eric Biggers\n"
229 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
230 "This is free software: you are free to change and redistribute it.\n"
231 "There is NO WARRANTY, to the extent permitted by law.\n"
233 "Report bugs to "PACKAGE_BUGREPORT".\n";
237 static inline void usage(int cmd)
239 puts("IMAGEX: Usage:");
240 fputs(usage_strings[cmd], stdout);
243 static void usage_all()
245 puts("IMAGEX: Usage:");
246 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
247 fputs(usage_strings[i], stdout);
248 static const char *extra =
250 " imagex --version\n"
252 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
254 fputs(extra, stdout);
257 static int verify_image_exists(int image)
259 if (image == WIM_NO_IMAGE) {
260 imagex_error("Not a valid image");
261 return WIMLIB_ERR_INVALID_IMAGE;
266 static int verify_image_is_single(int image)
268 if (image == WIM_ALL_IMAGES) {
269 imagex_error("Cannot specify all images for this action");
270 return WIMLIB_ERR_INVALID_IMAGE;
275 static int verify_image_exists_and_is_single(int image)
278 ret = verify_image_exists(image);
280 ret = verify_image_is_single(image);
284 static int get_compression_type(const char *optarg)
287 return WIM_COMPRESSION_TYPE_XPRESS;
288 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
289 return WIM_COMPRESSION_TYPE_LZX;
290 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
291 return WIM_COMPRESSION_TYPE_XPRESS;
292 else if (strcasecmp(optarg, "none") == 0)
293 return WIM_COMPRESSION_TYPE_NONE;
295 imagex_error("Invalid compression type `%s'! Must be "
296 "\"maximum\", \"fast\", or \"none\".", optarg);
297 return WIM_COMPRESSION_TYPE_INVALID;
301 static const char *file_get_contents(const char *filename, size_t *len_ret)
308 if (stat(filename, &stbuf) != 0) {
309 imagex_error_with_errno("Failed to stat the file `%s'", filename);
314 fp = fopen(filename, "rb");
316 imagex_error_with_errno("Failed to open the file `%s'", filename);
322 imagex_error("Failed to allocate buffer of %zu bytes to hold "
323 "contents of file `%s'", len, filename);
326 if (fread(buf, 1, len, fp) != len) {
327 imagex_error_with_errno("Failed to read %lu bytes from the "
328 "file `%s'", len, filename);
340 static int imagex_append(int argc, const char **argv)
343 const char *flags_element = NULL;
344 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
345 int add_image_flags = 0;
346 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
351 const char *config_file = NULL;
352 const char *config_str = NULL;
353 size_t config_len = 0;
358 for_opt(c, append_options) {
361 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
364 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
365 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
368 config_file = optarg;
371 flags_element = optarg;
374 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
377 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
386 if (argc < 2 || argc > 4) {
392 name = (argc >= 3) ? argv[2] : path_basename(dir);
393 desc = (argc >= 4) ? argv[3] : NULL;
396 config_str = file_get_contents(config_file, &config_len);
401 ret = wimlib_open_wim(wimfile, open_flags, &w);
408 ret = stat(dir, &stbuf);
410 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
411 const char *ntfs_device = dir;
412 printf("Capturing WIM image NTFS filesystem on `%s'\n",
414 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
422 if (errno != -ENOENT)
423 imagex_error_with_errno("Failed to stat `%s'", dir);
426 ret = wimlib_add_image(w, dir, name, config_str, config_len,
432 cur_image = wimlib_get_num_images(w);
434 ret = wimlib_set_image_descripton(w, cur_image, desc);
439 ret = wimlib_set_image_flags(w, cur_image, flags_element);
443 ret = wimlib_overwrite(w, write_flags);
449 /* Extract one image, or all images, from a WIM file into a directory. */
450 static int imagex_apply(int argc, const char **argv)
453 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
460 const char *image_num_or_name;
461 int extract_flags = 0;
463 for_opt(c, apply_options) {
466 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
469 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
472 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
475 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
484 if (argc != 2 && argc != 3) {
491 image_num_or_name = "1";
494 image_num_or_name = argv[1];
498 ret = wimlib_open_wim(wimfile, open_flags, &w);
502 image = wimlib_resolve_image(w, image_num_or_name);
503 ret = verify_image_exists(image);
507 num_images = wimlib_get_num_images(w);
508 if (argc == 2 && num_images != 1) {
509 imagex_error("`%s' contains %d images; Please select one "
510 "(or all)", wimfile, num_images);
519 ret = stat(dir, &stbuf);
521 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
522 const char *ntfs_device = dir;
523 printf("Applying image %d of `%s' to NTFS filesystem on `%s'\n",
524 image, wimfile, ntfs_device);
525 ret = wimlib_apply_image_to_ntfs_volume(w, image,
531 if (errno != -ENOENT)
532 imagex_error_with_errno("Failed to stat `%s'", dir);
536 ret = wimlib_extract_image(w, image, dir, extract_flags);
543 /* Create a WIM file from a directory. */
544 static int imagex_capture(int argc, const char **argv)
547 int add_image_flags = 0;
548 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
549 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
550 const char *flags_element = NULL;
555 const char *config_file = NULL;
556 const char *config_str = NULL;
557 size_t config_len = 0;
562 for_opt(c, capture_options) {
565 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
568 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
571 config_file = optarg;
574 compression_type = get_compression_type(optarg);
575 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
579 flags_element = optarg;
582 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
583 write_flags |= WIMLIB_WRITE_FLAG_VERBOSE;
586 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
596 if (argc < 2 || argc > 4) {
602 name = (argc >= 3) ? argv[2] : dir;
603 desc = (argc >= 4) ? argv[3] : NULL;
606 config_str = file_get_contents(config_file, &config_len);
611 ret = wimlib_create_new_wim(compression_type, &w);
618 ret = stat(dir, &stbuf);
620 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
621 const char *ntfs_device = dir;
622 printf("Capturing WIM image NTFS filesystem on `%s'\n",
624 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
632 if (errno != -ENOENT)
633 imagex_error_with_errno("Failed to stat `%s'", dir);
636 ret = wimlib_add_image(w, dir, name, config_str,
637 config_len, add_image_flags);
641 imagex_error("Failed to add the image `%s'", dir);
644 cur_image = wimlib_get_num_images(w);
646 ret = wimlib_set_image_descripton(w, cur_image, desc);
651 ret = wimlib_set_image_flags(w, cur_image, flags_element);
656 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
658 imagex_error("Failed to write the WIM file `%s'", wimfile);
664 /* Remove image(s) from a WIM. */
665 static int imagex_delete(int argc, const char **argv)
668 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
669 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
671 const char *image_num_or_name;
676 for_opt(c, delete_options) {
679 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
680 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
692 imagex_error("Must specify a WIM file");
694 imagex_error("Must specify an image");
699 image_num_or_name = argv[1];
701 ret = wimlib_open_wim(wimfile, open_flags, &w);
705 image = wimlib_resolve_image(w, image_num_or_name);
707 ret = verify_image_exists(image);
711 ret = wimlib_delete_image(w, image);
713 imagex_error("Failed to delete image from `%s'", wimfile);
717 ret = wimlib_overwrite(w, write_flags);
719 imagex_error("Failed to write the file `%s' with image "
727 /* Print the files contained in an image(s) in a WIM file. */
728 static int imagex_dir(int argc, const char **argv)
738 imagex_error("Must specify a WIM file");
743 imagex_error("Too many arguments");
749 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
753 part_number = wimlib_get_part_number(w, NULL);
754 if (part_number != 1) {
755 imagex_error("`%s' is part %d of a split WIM! Specify the "
756 "first part to see the files",
757 wimfile, part_number);
758 ret = WIMLIB_ERR_SPLIT_UNSUPPORTED;
763 image = wimlib_resolve_image(w, argv[2]);
764 ret = verify_image_exists(image);
768 /* Image was not specified. If the WIM only contains one image,
769 * choose that one; otherwise, print an error. */
770 num_images = wimlib_get_num_images(w);
771 if (num_images != 1) {
772 imagex_error("The file `%s' contains %d images; Please "
773 "select one.", wimfile, num_images);
781 ret = wimlib_print_files(w, image);
787 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
789 static int imagex_export(int argc, const char **argv)
792 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
793 int export_flags = 0;
794 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
795 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
796 bool compression_type_specified = false;
797 const char *src_wimfile;
798 const char *src_image_num_or_name;
799 const char *dest_wimfile;
800 const char *dest_name;
801 const char *dest_desc;
802 WIMStruct *src_w = NULL;
803 WIMStruct *dest_w = NULL;
809 for_opt(c, export_options) {
812 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
815 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
816 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
819 compression_type = get_compression_type(optarg);
820 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
822 compression_type_specified = true;
831 if (argc < 3 || argc > 5) {
835 src_wimfile = argv[0];
836 src_image_num_or_name = argv[1];
837 dest_wimfile = argv[2];
838 dest_name = (argc >= 4) ? argv[3] : NULL;
839 dest_desc = (argc >= 5) ? argv[4] : NULL;
840 ret = wimlib_open_wim(src_wimfile, open_flags, &src_w);
844 /* Determine if the destination is an existing file or not.
845 * If so, we try to append the exported image(s) to it; otherwise, we
846 * create a new WIM containing the exported image(s). */
847 if (stat(dest_wimfile, &stbuf) == 0) {
849 /* Destination file exists. */
850 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
851 imagex_error("`%s' is not a regular file",
855 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
859 if (compression_type_specified && compression_type !=
860 wimlib_get_compression_type(dest_w)) {
861 imagex_error("Cannot specify a compression type that is "
862 "not the same as that used in the "
867 compression_type = wimlib_get_compression_type(dest_w);
870 /* dest_wimfile is not an existing file, so create a new WIM. */
871 if (errno == ENOENT) {
872 ret = wimlib_create_new_wim(compression_type, &dest_w);
876 imagex_error_with_errno("Cannot stat file `%s'",
882 image = wimlib_resolve_image(src_w, src_image_num_or_name);
883 ret = verify_image_exists(image);
887 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
894 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
897 ret = wimlib_overwrite(dest_w, write_flags);
904 /* Prints information about a WIM file; also can mark an image as bootable,
905 * change the name of an image, or change the description of an image. */
906 static int imagex_info(int argc, const char **argv)
912 bool lookup_table = false;
914 bool metadata = false;
915 bool short_header = true;
916 const char *xml_out_file = NULL;
918 const char *image_num_or_name = "all";
919 const char *new_name = NULL;
920 const char *new_desc = NULL;
925 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
926 WIMLIB_OPEN_FLAG_SPLIT_OK;
930 for_opt(c, info_options) {
940 short_header = false;
944 short_header = false;
948 short_header = false;
951 xml_out_file = optarg;
952 short_header = false;
956 short_header = false;
966 if (argc == 0 || argc > 4) {
972 image_num_or_name = argv[1];
982 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
984 ret = wimlib_open_wim(wimfile, open_flags, &w);
988 part_number = wimlib_get_part_number(w, &total_parts);
990 /*if (total_parts > 1 && part_number > 1) {*/
991 /*printf("Warning: this is part %d of a %d-part split WIM.\n"*/
992 /*" Select the first part if you want to see information\n"*/
993 /*" about images in the WIM.\n", */
994 /*part_number, total_parts);*/
997 image = wimlib_resolve_image(w, image_num_or_name);
998 if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
999 imagex_error("The image `%s' does not exist",
1002 imagex_error("If you would like to set the boot "
1003 "index to 0, specify image \"0\" with "
1004 "the --boot flag.");
1005 ret = WIMLIB_ERR_INVALID_IMAGE;
1009 if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
1011 imagex_error("Cannot specify the --boot flag "
1012 "without specifying a specific "
1013 "image in a multi-image WIM");
1014 ret = WIMLIB_ERR_INVALID_IMAGE;
1018 imagex_error("Cannot specify the NEW_NAME "
1019 "without specifying a specific "
1020 "image in a multi-image WIM");
1021 ret = WIMLIB_ERR_INVALID_IMAGE;
1026 /* Operations that print information are separated from operations that
1027 * recreate the WIM file. */
1028 if (!new_name && !boot) {
1030 if (image == WIM_NO_IMAGE) {
1031 imagex_error("`%s' is not a valid image",
1033 ret = WIMLIB_ERR_INVALID_IMAGE;
1037 if (image == WIM_ALL_IMAGES && short_header)
1038 wimlib_print_wim_information(w);
1041 wimlib_print_header(w);
1044 if (total_parts != 1) {
1045 printf("Warning: Only showing the lookup table "
1046 "for part %d of a %d-part WIM.\n",
1047 part_number, total_parts);
1049 wimlib_print_lookup_table(w);
1053 ret = wimlib_extract_xml_data(w, stdout);
1059 fp = fopen(xml_out_file, "wb");
1061 imagex_error_with_errno("Failed to open the "
1067 ret = wimlib_extract_xml_data(w, fp);
1068 if (fclose(fp) != 0) {
1069 imagex_error("Failed to close the file `%s'",
1079 wimlib_print_available_images(w, image);
1082 if (total_parts != 1 && part_number != 1) {
1083 imagex_error("Select part 1 of this %d-part WIM "
1084 "to see the image metadata",
1086 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
1088 ret = wimlib_print_metadata(w, image);
1093 if (total_parts != 1) {
1094 imagex_error("Modifying a split WIM is not supported.");
1097 if (image == WIM_ALL_IMAGES)
1100 if (image == WIM_NO_IMAGE && new_name) {
1101 imagex_error("Cannot specify new_name (`%s') when "
1102 "using image 0", new_name);
1107 if (image == wimlib_get_boot_idx(w)) {
1108 printf("Image %d is already marked as "
1109 "bootable.\n", image);
1112 printf("Marking image %d as bootable.\n",
1114 wimlib_set_boot_idx(w, image);
1118 if (strcmp(wimlib_get_image_name(w, image),
1120 printf("Image %d is already named \"%s\".\n",
1124 printf("Changing the name of image %d to "
1125 "\"%s\".\n", image, new_name);
1126 ret = wimlib_set_image_name(w, image, new_name);
1132 const char *old_desc;
1133 old_desc = wimlib_get_image_description(w, image);
1134 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1135 printf("The description of image %d is already "
1136 "\"%s\".\n", image, new_desc);
1139 printf("Changing the description of image %d "
1140 "to \"%s\".\n", image, new_desc);
1141 ret = wimlib_set_image_descripton(w, image,
1148 /* Only call wimlib_overwrite_xml_and_header() if something
1149 * actually needs to be changed. */
1150 if (boot || new_name || new_desc ||
1151 check != wimlib_has_integrity_table(w)) {
1153 ret = wimlib_overwrite_xml_and_header(w, check ?
1154 WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
1155 WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
1157 printf("The file `%s' was not modified because nothing "
1158 "needed to be done.\n", wimfile);
1168 /* Join split WIMs into one part WIM */
1169 static int imagex_join(int argc, const char **argv)
1172 int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1173 const char *output_path;
1175 for_opt(c, join_options) {
1178 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1188 imagex_error("Must specify at least one split WIM (.swm) parts "
1192 output_path = argv[0];
1193 return wimlib_join(++argv, --argc, output_path, flags);
1199 /* Mounts an image using a FUSE mount. */
1200 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1203 int mount_flags = 0;
1204 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1205 const char *wimfile;
1212 if (strcmp(argv[0], "mountrw") == 0)
1213 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1214 for_opt(c, mount_options) {
1217 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1220 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1223 if (strcasecmp(optarg, "none") == 0)
1224 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1225 else if (strcasecmp(optarg, "xattr") == 0)
1226 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1227 else if (strcasecmp(optarg, "windows") == 0)
1228 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1230 imagex_error("Unknown stream interface \"%s\"", optarg);
1240 if (argc != 2 && argc != 3)
1245 ret = wimlib_open_wim(wimfile, open_flags, &w);
1251 num_images = wimlib_get_num_images(w);
1252 if (num_images != 1) {
1253 imagex_error("The file `%s' contains %d images; Please "
1254 "select one", wimfile, num_images);
1255 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1257 ret = WIMLIB_ERR_INVALID_IMAGE;
1262 image = wimlib_resolve_image(w, argv[1]);
1266 ret = verify_image_exists_and_is_single(image);
1270 ret = wimlib_mount(w, image, dir, mount_flags);
1272 imagex_error("Failed to mount image %d from `%s' on `%s'",
1273 image, wimfile, dir);
1280 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1285 /* Split a WIM into a spanned set */
1286 static int imagex_split(int argc, const char **argv)
1289 int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1290 unsigned long part_size;
1292 for_opt(c, split_options) {
1295 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1309 part_size = strtoul(argv[2], NULL, 10) * (1 << 20);
1310 return wimlib_split(argv[0], argv[1], part_size, flags);
1313 /* Unmounts an image. */
1314 static int imagex_unmount(int argc, const char **argv)
1317 int unmount_flags = 0;
1320 for_opt(c, unmount_options) {
1323 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1326 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1340 ret = wimlib_unmount(argv[0], unmount_flags);
1342 imagex_error("Failed to unmount `%s'", argv[0]);
1346 struct imagex_command {
1348 int (*func)(int , const char **);
1352 static struct imagex_command imagex_commands[] = {
1353 {"append", imagex_append, APPEND},
1354 {"apply", imagex_apply, APPLY},
1355 {"capture", imagex_capture, CAPTURE},
1356 {"delete", imagex_delete, DELETE},
1357 {"dir", imagex_dir, DIR},
1358 {"export", imagex_export, EXPORT},
1359 {"info", imagex_info, INFO},
1360 {"join", imagex_join, JOIN},
1361 {"mount", imagex_mount_rw_or_ro, MOUNT},
1362 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1363 {"split", imagex_split, SPLIT},
1364 {"unmount", imagex_unmount, UNMOUNT},
1367 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1368 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1370 static void help_or_version(int argc, const char **argv)
1374 struct imagex_command *cmd;
1376 for (i = 1; i < argc; i++) {
1384 if (strcmp(p, "help") == 0 || (*p == '?' && *(p + 1) == '\0')) {
1385 for_imagex_command(cmd) {
1386 if (strcmp(cmd->name, argv[1]) == 0) {
1394 if (strcmp(p, "version") == 0) {
1402 int main(int argc, const char **argv)
1404 struct imagex_command *cmd;
1408 imagex_error("No command specified");
1413 help_or_version(argc, argv);
1417 wimlib_set_print_errors(true);
1419 for_imagex_command(cmd) {
1420 if (strcmp(cmd->name, *argv) == 0) {
1421 ret = cmd->func(argc, argv);
1423 imagex_error("Exiting with error code %d:\n"
1425 wimlib_get_error_string(ret));
1426 if (ret == WIMLIB_ERR_NTFS_3G)
1427 imagex_error_with_errno("errno");
1433 imagex_error("Unrecognized command: `%s'", argv[0]);
1437 /*#ifndef WITH_NTFS_3G*/
1438 /*ERROR("wimlib was not compiled with support for NTFS-3g, so we cannot extract");*/
1439 /*ERROR("a WIM to a NTFS filesystem while preserving NTFS-specific metadata.");*/
1440 /*ERROR("Please apply the WIM to a directory rather than a block device, ");*/
1441 /*ERROR("and without the NTFS flag; or compile in support for NTFS-3g.");*/
1442 /*return WIMLIB_ERR_UNSUPPORTED;*/