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 " [--verbose] [--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\"]\n"
102 " [\"DEST_IMAGE_DESCRIPTION\"] [--boot] [--check]\n"
103 " [--compress=TYPE]\n",
105 " imagex info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
106 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
107 " [--xml] [--extract-xml FILE] [--metadata]\n",
109 " imagex join [--check] WIMFILE SPLIT_WIM...\n",
111 " imagex mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
112 " [--check] [--debug] [--streams-interface=INTERFACE]\n",
114 " imagex mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
115 " [--check] [--debug] [--streams-interface=INTERFACE]\n",
117 " imagex split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n",
119 " imagex unmount DIRECTORY [--commit] [--check]\n",
122 static const struct option common_options[] = {
123 {"help", 0, NULL, 'h'},
124 {"version", 0, NULL, 'v'},
128 static const struct option append_options[] = {
129 {"boot", no_argument, NULL, 'b'},
130 {"check", no_argument, NULL, 'c'},
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 apply_options[] = {
138 {"check", no_argument, NULL, 'c'},
139 {"hardlink", no_argument, NULL, 'h'},
140 {"symlink", no_argument, NULL, 's'},
141 {"verbose", no_argument, NULL, 'v'},
144 static const struct option capture_options[] = {
145 {"boot", no_argument, NULL, 'b'},
146 {"check", no_argument, NULL, 'c'},
147 {"compress", required_argument, NULL, 'x'},
148 {"config", required_argument, NULL, 'C'},
149 {"dereference", no_argument, NULL, 'L'},
150 {"flags", required_argument, NULL, 'f'},
151 {"verbose", no_argument, NULL, 'v'},
154 static const struct option delete_options[] = {
155 {"check", no_argument, NULL, 'c'},
159 static const struct option export_options[] = {
160 {"boot", no_argument, NULL, 'b'},
161 {"check", no_argument, NULL, 'c'},
162 {"compress", required_argument, NULL, 'x'},
166 static const struct option info_options[] = {
167 {"boot", no_argument, NULL, 'b'},
168 {"check", no_argument, NULL, 'c'},
169 {"extract-xml", required_argument, NULL, 'X'},
170 {"header", no_argument, NULL, 'h'},
171 {"lookup-table", no_argument, NULL, 'l'},
172 {"metadata", no_argument, NULL, 'm'},
173 {"xml", no_argument, NULL, 'x'},
177 static const struct option join_options[] = {
178 {"check", no_argument, NULL, 'c'},
182 static const struct option mount_options[] = {
183 {"check", no_argument, NULL, 'c'},
184 {"debug", no_argument, NULL, 'd'},
185 {"streams-interface", required_argument, NULL, 's'},
189 static const struct option split_options[] = {
190 {"check", no_argument, NULL, 'c'},
194 static const struct option unmount_options[] = {
195 {"commit", no_argument, NULL, 'c'},
196 {"check", no_argument, NULL, 'C'},
201 /* Print formatted error message to stderr. */
202 static void imagex_error(const char *format, ...)
205 va_start(va, format);
206 fputs("ERROR: ", stderr);
207 vfprintf(stderr, format, va);
212 /* Print formatted error message to stderr. */
213 static void imagex_error_with_errno(const char *format, ...)
215 int errno_save = errno;
217 va_start(va, format);
218 fputs("ERROR: ", stderr);
219 vfprintf(stderr, format, va);
220 fprintf(stderr, ": %s\n", strerror(errno_save));
225 static inline void version()
227 static const char *s =
228 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
229 "Copyright (C) 2012 Eric Biggers\n"
230 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
231 "This is free software: you are free to change and redistribute it.\n"
232 "There is NO WARRANTY, to the extent permitted by law.\n"
234 "Report bugs to "PACKAGE_BUGREPORT".\n";
238 static inline void usage(int cmd)
240 puts("IMAGEX: Usage:");
241 fputs(usage_strings[cmd], stdout);
244 static void usage_all()
246 puts("IMAGEX: Usage:");
247 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
248 fputs(usage_strings[i], stdout);
249 static const char *extra =
251 " imagex --version\n"
253 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
255 fputs(extra, stdout);
258 static int verify_image_exists(int image)
260 if (image == WIM_NO_IMAGE) {
261 imagex_error("Not a valid image");
262 return WIMLIB_ERR_INVALID_IMAGE;
267 static int verify_image_is_single(int image)
269 if (image == WIM_ALL_IMAGES) {
270 imagex_error("Cannot specify all images for this action");
271 return WIMLIB_ERR_INVALID_IMAGE;
276 static int verify_image_exists_and_is_single(int image)
279 ret = verify_image_exists(image);
281 ret = verify_image_is_single(image);
285 static int get_compression_type(const char *optarg)
287 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
288 return WIM_COMPRESSION_TYPE_LZX;
289 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
290 return WIM_COMPRESSION_TYPE_XPRESS;
291 else if (strcasecmp(optarg, "none") == 0)
292 return WIM_COMPRESSION_TYPE_NONE;
294 imagex_error("Invalid compression type `%s'! Must be "
295 "\"maximum\", \"fast\", or \"none\".", optarg);
296 return WIM_COMPRESSION_TYPE_INVALID;
300 static const char *file_get_contents(const char *filename, size_t *len_ret)
307 if (stat(filename, &stbuf) != 0) {
308 imagex_error_with_errno("Failed to stat the file `%s'", filename);
313 fp = fopen(filename, "rb");
315 imagex_error_with_errno("Failed to open the file `%s'", filename);
321 imagex_error("Failed to allocate buffer of %zu bytes to hold "
322 "contents of file `%s'", len, filename);
325 if (fread(buf, 1, len, fp) != len) {
326 imagex_error_with_errno("Failed to read %lu bytes from the "
327 "file `%s'", len, filename);
339 static int imagex_append(int argc, const char **argv)
342 const char *flags_element = NULL;
343 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
344 int add_image_flags = 0;
345 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
350 const char *config_file = NULL;
351 const char *config_str = NULL;
352 size_t config_len = 0;
357 for_opt(c, append_options) {
360 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
363 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
364 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
367 config_file = optarg;
370 flags_element = optarg;
373 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
376 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
385 if (argc < 2 || argc > 4) {
391 name = (argc >= 3) ? argv[2] : path_basename(dir);
392 desc = (argc >= 4) ? argv[3] : NULL;
395 config_str = file_get_contents(config_file, &config_len);
400 ret = wimlib_open_wim(wimfile, open_flags, &w);
407 ret = stat(dir, &stbuf);
409 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
410 const char *ntfs_device = dir;
411 printf("Capturing WIM image NTFS filesystem on `%s'\n",
413 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
421 if (errno != -ENOENT)
422 imagex_error_with_errno("Failed to stat `%s'", dir);
425 ret = wimlib_add_image(w, dir, name, config_str, config_len,
431 cur_image = wimlib_get_num_images(w);
433 ret = wimlib_set_image_descripton(w, cur_image, desc);
438 ret = wimlib_set_image_flags(w, cur_image, flags_element);
442 ret = wimlib_overwrite(w, write_flags);
448 /* Extract one image, or all images, from a WIM file into a directory. */
449 static int imagex_apply(int argc, const char **argv)
452 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
459 const char *image_num_or_name;
460 int extract_flags = 0;
462 for_opt(c, apply_options) {
465 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
468 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
471 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
474 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
483 if (argc != 2 && argc != 3) {
490 image_num_or_name = "1";
493 image_num_or_name = argv[1];
497 ret = wimlib_open_wim(wimfile, open_flags, &w);
501 image = wimlib_resolve_image(w, image_num_or_name);
502 ret = verify_image_exists(image);
506 num_images = wimlib_get_num_images(w);
507 if (argc == 2 && num_images != 1) {
508 imagex_error("`%s' contains %d images; Please select one "
509 "(or all)", wimfile, num_images);
518 ret = stat(dir, &stbuf);
520 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
521 const char *ntfs_device = dir;
522 printf("Applying image %d of `%s' to NTFS filesystem on `%s'\n",
523 image, wimfile, ntfs_device);
524 ret = wimlib_apply_image_to_ntfs_volume(w, image,
530 if (errno != -ENOENT)
531 imagex_error_with_errno("Failed to stat `%s'", dir);
535 ret = wimlib_extract_image(w, image, dir, extract_flags);
542 /* Create a WIM file from a directory. */
543 static int imagex_capture(int argc, const char **argv)
546 int add_image_flags = 0;
547 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
548 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
549 const char *flags_element = NULL;
554 const char *config_file = NULL;
555 const char *config_str = NULL;
556 size_t config_len = 0;
561 for_opt(c, capture_options) {
564 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
567 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
570 config_file = optarg;
573 compression_type = get_compression_type(optarg);
574 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
578 flags_element = optarg;
581 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
582 write_flags |= WIMLIB_WRITE_FLAG_VERBOSE;
585 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
595 if (argc < 2 || argc > 4) {
601 name = (argc >= 3) ? argv[2] : dir;
602 desc = (argc >= 4) ? argv[3] : NULL;
605 config_str = file_get_contents(config_file, &config_len);
610 ret = wimlib_create_new_wim(compression_type, &w);
617 ret = stat(dir, &stbuf);
619 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
620 const char *ntfs_device = dir;
621 printf("Capturing WIM image NTFS filesystem on `%s'\n",
623 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
631 if (errno != -ENOENT)
632 imagex_error_with_errno("Failed to stat `%s'", dir);
635 ret = wimlib_add_image(w, dir, name, config_str,
636 config_len, add_image_flags);
640 imagex_error("Failed to add the image `%s'", dir);
643 cur_image = wimlib_get_num_images(w);
645 ret = wimlib_set_image_descripton(w, cur_image, desc);
650 ret = wimlib_set_image_flags(w, cur_image, flags_element);
655 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
657 imagex_error("Failed to write the WIM file `%s'", wimfile);
663 /* Remove image(s) from a WIM. */
664 static int imagex_delete(int argc, const char **argv)
667 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
668 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
670 const char *image_num_or_name;
675 for_opt(c, delete_options) {
678 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
679 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
691 imagex_error("Must specify a WIM file");
693 imagex_error("Must specify an image");
698 image_num_or_name = argv[1];
700 ret = wimlib_open_wim(wimfile, open_flags, &w);
704 image = wimlib_resolve_image(w, image_num_or_name);
706 ret = verify_image_exists(image);
710 ret = wimlib_delete_image(w, image);
712 imagex_error("Failed to delete image from `%s'", wimfile);
716 ret = wimlib_overwrite(w, write_flags);
718 imagex_error("Failed to write the file `%s' with image "
726 /* Print the files contained in an image(s) in a WIM file. */
727 static int imagex_dir(int argc, const char **argv)
737 imagex_error("Must specify a WIM file");
742 imagex_error("Too many arguments");
748 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
752 part_number = wimlib_get_part_number(w, NULL);
753 if (part_number != 1) {
754 imagex_error("`%s' is part %d of a split WIM! Specify the "
755 "first part to see the files",
756 wimfile, part_number);
757 ret = WIMLIB_ERR_SPLIT_UNSUPPORTED;
762 image = wimlib_resolve_image(w, argv[2]);
763 ret = verify_image_exists(image);
767 /* Image was not specified. If the WIM only contains one image,
768 * choose that one; otherwise, print an error. */
769 num_images = wimlib_get_num_images(w);
770 if (num_images != 1) {
771 imagex_error("The file `%s' contains %d images; Please "
772 "select one.", wimfile, num_images);
780 ret = wimlib_print_files(w, image);
786 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
788 static int imagex_export(int argc, const char **argv)
791 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
792 int export_flags = 0;
793 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
794 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
795 bool compression_type_specified = false;
796 const char *src_wimfile;
797 const char *src_image_num_or_name;
798 const char *dest_wimfile;
799 const char *dest_name;
800 const char *dest_desc;
801 WIMStruct *src_w = NULL;
802 WIMStruct *dest_w = NULL;
808 for_opt(c, export_options) {
811 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
814 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
815 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
818 compression_type = get_compression_type(optarg);
819 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
821 compression_type_specified = true;
830 if (argc < 3 || argc > 5) {
834 src_wimfile = argv[0];
835 src_image_num_or_name = argv[1];
836 dest_wimfile = argv[2];
837 dest_name = (argc >= 4) ? argv[3] : NULL;
838 dest_desc = (argc >= 5) ? argv[4] : NULL;
839 ret = wimlib_open_wim(src_wimfile, open_flags, &src_w);
843 /* Determine if the destination is an existing file or not.
844 * If so, we try to append the exported image(s) to it; otherwise, we
845 * create a new WIM containing the exported image(s). */
846 if (stat(dest_wimfile, &stbuf) == 0) {
848 /* Destination file exists. */
849 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
850 imagex_error("`%s' is not a regular file",
854 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
858 if (compression_type_specified && compression_type !=
859 wimlib_get_compression_type(dest_w)) {
860 imagex_error("Cannot specify a compression type that is "
861 "not the same as that used in the "
866 compression_type = wimlib_get_compression_type(dest_w);
869 /* dest_wimfile is not an existing file, so create a new WIM. */
870 if (errno == ENOENT) {
871 ret = wimlib_create_new_wim(compression_type, &dest_w);
875 imagex_error_with_errno("Cannot stat file `%s'",
881 image = wimlib_resolve_image(src_w, src_image_num_or_name);
882 ret = verify_image_exists(image);
886 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
893 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
896 ret = wimlib_overwrite(dest_w, write_flags);
903 /* Prints information about a WIM file; also can mark an image as bootable,
904 * change the name of an image, or change the description of an image. */
905 static int imagex_info(int argc, const char **argv)
911 bool lookup_table = false;
913 bool metadata = false;
914 bool short_header = true;
915 const char *xml_out_file = NULL;
917 const char *image_num_or_name = "all";
918 const char *new_name = NULL;
919 const char *new_desc = NULL;
924 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
925 WIMLIB_OPEN_FLAG_SPLIT_OK;
929 for_opt(c, info_options) {
939 short_header = false;
943 short_header = false;
947 short_header = false;
950 xml_out_file = optarg;
951 short_header = false;
955 short_header = false;
965 if (argc == 0 || argc > 4) {
971 image_num_or_name = argv[1];
981 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
983 ret = wimlib_open_wim(wimfile, open_flags, &w);
987 part_number = wimlib_get_part_number(w, &total_parts);
989 /*if (total_parts > 1 && part_number > 1) {*/
990 /*printf("Warning: this is part %d of a %d-part split WIM.\n"*/
991 /*" Select the first part if you want to see information\n"*/
992 /*" about images in the WIM.\n", */
993 /*part_number, total_parts);*/
996 image = wimlib_resolve_image(w, image_num_or_name);
997 if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
998 imagex_error("The image `%s' does not exist",
1001 imagex_error("If you would like to set the boot "
1002 "index to 0, specify image \"0\" with "
1003 "the --boot flag.");
1004 ret = WIMLIB_ERR_INVALID_IMAGE;
1008 if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
1010 imagex_error("Cannot specify the --boot flag "
1011 "without specifying a specific "
1012 "image in a multi-image WIM");
1013 ret = WIMLIB_ERR_INVALID_IMAGE;
1017 imagex_error("Cannot specify the NEW_NAME "
1018 "without specifying a specific "
1019 "image in a multi-image WIM");
1020 ret = WIMLIB_ERR_INVALID_IMAGE;
1025 /* Operations that print information are separated from operations that
1026 * recreate the WIM file. */
1027 if (!new_name && !boot) {
1029 if (image == WIM_NO_IMAGE) {
1030 imagex_error("`%s' is not a valid image",
1032 ret = WIMLIB_ERR_INVALID_IMAGE;
1036 if (image == WIM_ALL_IMAGES && short_header)
1037 wimlib_print_wim_information(w);
1040 wimlib_print_header(w);
1043 if (total_parts != 1) {
1044 printf("Warning: Only showing the lookup table "
1045 "for part %d of a %d-part WIM.\n",
1046 part_number, total_parts);
1048 wimlib_print_lookup_table(w);
1052 ret = wimlib_extract_xml_data(w, stdout);
1058 fp = fopen(xml_out_file, "wb");
1060 imagex_error_with_errno("Failed to open the "
1066 ret = wimlib_extract_xml_data(w, fp);
1067 if (fclose(fp) != 0) {
1068 imagex_error("Failed to close the file `%s'",
1078 wimlib_print_available_images(w, image);
1081 if (total_parts != 1 && part_number != 1) {
1082 imagex_error("Select part 1 of this %d-part WIM "
1083 "to see the image metadata",
1085 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
1087 ret = wimlib_print_metadata(w, image);
1092 if (total_parts != 1) {
1093 imagex_error("Modifying a split WIM is not supported.");
1096 if (image == WIM_ALL_IMAGES)
1099 if (image == WIM_NO_IMAGE && new_name) {
1100 imagex_error("Cannot specify new_name (`%s') when "
1101 "using image 0", new_name);
1106 if (image == wimlib_get_boot_idx(w)) {
1107 printf("Image %d is already marked as "
1108 "bootable.\n", image);
1111 printf("Marking image %d as bootable.\n",
1113 wimlib_set_boot_idx(w, image);
1117 if (strcmp(wimlib_get_image_name(w, image),
1119 printf("Image %d is already named \"%s\".\n",
1123 printf("Changing the name of image %d to "
1124 "\"%s\".\n", image, new_name);
1125 ret = wimlib_set_image_name(w, image, new_name);
1131 const char *old_desc;
1132 old_desc = wimlib_get_image_description(w, image);
1133 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1134 printf("The description of image %d is already "
1135 "\"%s\".\n", image, new_desc);
1138 printf("Changing the description of image %d "
1139 "to \"%s\".\n", image, new_desc);
1140 ret = wimlib_set_image_descripton(w, image,
1147 /* Only call wimlib_overwrite_xml_and_header() if something
1148 * actually needs to be changed. */
1149 if (boot || new_name || new_desc ||
1150 check != wimlib_has_integrity_table(w)) {
1152 ret = wimlib_overwrite_xml_and_header(w, check ?
1153 WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
1154 WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
1156 printf("The file `%s' was not modified because nothing "
1157 "needed to be done.\n", wimfile);
1167 /* Join split WIMs into one part WIM */
1168 static int imagex_join(int argc, const char **argv)
1171 int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1172 const char *output_path;
1174 for_opt(c, join_options) {
1177 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1187 imagex_error("Must specify at least one split WIM (.swm) parts "
1191 output_path = argv[0];
1192 return wimlib_join(++argv, --argc, output_path, flags);
1198 /* Mounts an image using a FUSE mount. */
1199 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1202 int mount_flags = 0;
1203 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1204 const char *wimfile;
1211 if (strcmp(argv[0], "mountrw") == 0)
1212 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1213 for_opt(c, mount_options) {
1216 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1219 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1222 if (strcasecmp(optarg, "none") == 0)
1223 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1224 else if (strcasecmp(optarg, "xattr") == 0)
1225 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1226 else if (strcasecmp(optarg, "windows") == 0)
1227 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1229 imagex_error("Unknown stream interface \"%s\"", optarg);
1239 if (argc != 2 && argc != 3)
1244 ret = wimlib_open_wim(wimfile, open_flags, &w);
1250 num_images = wimlib_get_num_images(w);
1251 if (num_images != 1) {
1252 imagex_error("The file `%s' contains %d images; Please "
1253 "select one", wimfile, num_images);
1254 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1256 ret = WIMLIB_ERR_INVALID_IMAGE;
1261 image = wimlib_resolve_image(w, argv[1]);
1265 ret = verify_image_exists_and_is_single(image);
1269 ret = wimlib_mount(w, image, dir, mount_flags);
1271 imagex_error("Failed to mount image %d from `%s' on `%s'",
1272 image, wimfile, dir);
1279 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1284 /* Split a WIM into a spanned set */
1285 static int imagex_split(int argc, const char **argv)
1288 int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1289 unsigned long part_size;
1291 for_opt(c, split_options) {
1294 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1308 part_size = strtoul(argv[2], NULL, 10) * (1 << 20);
1309 return wimlib_split(argv[0], argv[1], part_size, flags);
1312 /* Unmounts an image. */
1313 static int imagex_unmount(int argc, const char **argv)
1316 int unmount_flags = 0;
1319 for_opt(c, unmount_options) {
1322 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1325 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1339 ret = wimlib_unmount(argv[0], unmount_flags);
1341 imagex_error("Failed to unmount `%s'", argv[0]);
1345 struct imagex_command {
1347 int (*func)(int , const char **);
1351 static struct imagex_command imagex_commands[] = {
1352 {"append", imagex_append, APPEND},
1353 {"apply", imagex_apply, APPLY},
1354 {"capture", imagex_capture, CAPTURE},
1355 {"delete", imagex_delete, DELETE},
1356 {"dir", imagex_dir, DIR},
1357 {"export", imagex_export, EXPORT},
1358 {"info", imagex_info, INFO},
1359 {"join", imagex_join, JOIN},
1360 {"mount", imagex_mount_rw_or_ro, MOUNT},
1361 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1362 {"split", imagex_split, SPLIT},
1363 {"unmount", imagex_unmount, UNMOUNT},
1366 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1367 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1369 static void help_or_version(int argc, const char **argv)
1373 struct imagex_command *cmd;
1375 for (i = 1; i < argc; i++) {
1383 if (strcmp(p, "help") == 0 || (*p == '?' && *(p + 1) == '\0')) {
1384 for_imagex_command(cmd) {
1385 if (strcmp(cmd->name, argv[1]) == 0) {
1393 if (strcmp(p, "version") == 0) {
1401 int main(int argc, const char **argv)
1403 struct imagex_command *cmd;
1407 imagex_error("No command specified");
1412 help_or_version(argc, argv);
1416 wimlib_set_print_errors(true);
1418 for_imagex_command(cmd) {
1419 if (strcmp(cmd->name, *argv) == 0) {
1420 ret = cmd->func(argc, argv);
1422 imagex_error("Exiting with error code %d:\n"
1424 wimlib_get_error_string(ret));
1425 if (ret == WIMLIB_ERR_NTFS_3G)
1426 imagex_error_with_errno("errno");
1432 imagex_error("Unrecognized command: `%s'", argv[0]);
1436 /*#ifndef WITH_NTFS_3G*/
1437 /*ERROR("wimlib was not compiled with support for NTFS-3g, so we cannot extract");*/
1438 /*ERROR("a WIM to a NTFS filesystem while preserving NTFS-specific metadata.");*/
1439 /*ERROR("Please apply the WIM to a directory rather than a block device, ");*/
1440 /*ERROR("and without the NTFS flag; or compile in support for NTFS-3g.");*/
1441 /*return WIMLIB_ERR_UNSUPPORTED;*/