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/>.
37 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
39 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (char**)argv, "", \
58 static void usage(int cmd_type);
59 static void usage_all();
61 static const char *usage_strings[] = {
63 "imagex append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
64 " [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
65 " [--verbose] [--dereference] [--config=FILE]\n",
67 "imagex apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
68 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
69 " [--symlink] [--verbose] [--ref=\"GLOB\"]\n",
71 "imagex capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
72 " [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
73 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
76 "imagex delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n",
78 "imagex dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n",
80 "imagex export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
81 " DEST_WIMFILE [DEST_IMAGE_NAME]\n"
82 " [DEST_IMAGE_DESCRIPTION] [--boot] [--check]\n"
83 " [--compress=TYPE] [--ref=\"GLOB\"]\n",
85 "imagex info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
86 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
87 " [--xml] [--extract-xml FILE] [--metadata]\n",
89 "imagex join [--check] WIMFILE SPLIT_WIM...\n",
91 "imagex mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
92 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
93 " [--ref=\"GLOB\"]\n",
95 "imagex mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
96 " [--check] [--debug] [--streams-interface=INTERFACE]\n",
98 "imagex split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n",
100 "imagex unmount DIRECTORY [--commit] [--check]\n",
103 static const struct option common_options[] = {
104 {"help", 0, NULL, 'h'},
105 {"version", 0, NULL, 'v'},
109 static const struct option apply_options[] = {
110 {"check", no_argument, NULL, 'c'},
111 {"hardlink", no_argument, NULL, 'h'},
112 {"symlink", no_argument, NULL, 's'},
113 {"verbose", no_argument, NULL, 'v'},
114 {"ref", required_argument, NULL, 'r'},
117 static const struct option capture_or_append_options[] = {
118 {"boot", no_argument, NULL, 'b'},
119 {"check", no_argument, NULL, 'c'},
120 {"compress", required_argument, NULL, 'x'},
121 {"config", required_argument, NULL, 'C'},
122 {"dereference", no_argument, NULL, 'L'},
123 {"flags", required_argument, NULL, 'f'},
124 {"verbose", no_argument, NULL, 'v'},
127 static const struct option delete_options[] = {
128 {"check", no_argument, NULL, 'c'},
132 static const struct option export_options[] = {
133 {"boot", no_argument, NULL, 'b'},
134 {"check", no_argument, NULL, 'c'},
135 {"compress", required_argument, NULL, 'x'},
136 {"ref", required_argument, NULL, 'r'},
140 static const struct option info_options[] = {
141 {"boot", no_argument, NULL, 'b'},
142 {"check", no_argument, NULL, 'c'},
143 {"extract-xml", required_argument, NULL, 'X'},
144 {"header", no_argument, NULL, 'h'},
145 {"lookup-table", no_argument, NULL, 'l'},
146 {"metadata", no_argument, NULL, 'm'},
147 {"xml", no_argument, NULL, 'x'},
151 static const struct option join_options[] = {
152 {"check", no_argument, NULL, 'c'},
156 static const struct option mount_options[] = {
157 {"check", no_argument, NULL, 'c'},
158 {"debug", no_argument, NULL, 'd'},
159 {"streams-interface", required_argument, NULL, 's'},
160 {"ref", required_argument, NULL, 'r'},
164 static const struct option split_options[] = {
165 {"check", no_argument, NULL, 'c'},
169 static const struct option unmount_options[] = {
170 {"commit", no_argument, NULL, 'c'},
171 {"check", no_argument, NULL, 'C'},
177 /* Print formatted error message to stderr. */
178 static void imagex_error(const char *format, ...)
181 va_start(va, format);
182 fputs("ERROR: ", stderr);
183 vfprintf(stderr, format, va);
188 /* Print formatted error message to stderr. */
189 static void imagex_error_with_errno(const char *format, ...)
191 int errno_save = errno;
193 va_start(va, format);
194 fputs("ERROR: ", stderr);
195 vfprintf(stderr, format, va);
196 fprintf(stderr, ": %s\n", strerror(errno_save));
200 static int verify_image_exists(int image, const char *image_name,
201 const char *wim_name)
203 if (image == WIM_NO_IMAGE) {
204 imagex_error("\"%s\" is not a valid image in `%s'!\n"
205 " Please specify a 1-based imagex index or "
207 " You may use `imagex info' to list the images "
208 "contained in a WIM.",
209 image_name, wim_name);
215 static int verify_image_is_single(int image)
217 if (image == WIM_ALL_IMAGES) {
218 imagex_error("Cannot specify all images for this action!");
224 static int verify_image_exists_and_is_single(int image, const char *image_name,
225 const char *wim_name)
228 ret = verify_image_exists(image, image_name, wim_name);
230 ret = verify_image_is_single(image);
234 static int get_compression_type(const char *optarg)
236 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
237 return WIM_COMPRESSION_TYPE_LZX;
238 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
239 return WIM_COMPRESSION_TYPE_XPRESS;
240 else if (strcasecmp(optarg, "none") == 0)
241 return WIM_COMPRESSION_TYPE_NONE;
243 imagex_error("Invalid compression type `%s'! Must be "
244 "\"maximum\", \"fast\", or \"none\".", optarg);
245 return WIM_COMPRESSION_TYPE_INVALID;
249 static char *file_get_contents(const char *filename, size_t *len_ret)
256 if (stat(filename, &stbuf) != 0) {
257 imagex_error_with_errno("Failed to stat the file `%s'", filename);
262 fp = fopen(filename, "rb");
264 imagex_error_with_errno("Failed to open the file `%s'", filename);
270 imagex_error("Failed to allocate buffer of %zu bytes to hold "
271 "contents of file `%s'", len, filename);
274 if (fread(buf, 1, len, fp) != len) {
275 imagex_error_with_errno("Failed to read %lu bytes from the "
276 "file `%s'", len, filename);
288 static int file_writable(const char *path)
292 ret = access(path, F_OK | W_OK);
294 imagex_error_with_errno("Can't modify `%s'", path);
298 static int open_swms_from_glob(const char *swm_glob,
299 const char *first_part,
301 WIMStruct ***additional_swms_ret,
302 unsigned *num_additional_swms_ret)
304 unsigned num_additional_swms = 0;
305 WIMStruct **additional_swms = NULL;
309 ret = glob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
311 if (ret == GLOB_NOMATCH) {
312 imagex_error("Found no files for glob \"%s\"",
315 imagex_error_with_errno("Failed to process glob "
321 num_additional_swms = globbuf.gl_pathc;
322 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
323 if (!additional_swms) {
324 imagex_error("Out of memory");
329 for (unsigned i = 0; i < num_additional_swms; i++) {
330 if (strcmp(globbuf.gl_pathv[i], first_part) == 0) {
334 ret = wimlib_open_wim(globbuf.gl_pathv[i],
335 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
336 &additional_swms[i - offset]);
340 *additional_swms_ret = additional_swms;
341 *num_additional_swms_ret = num_additional_swms - offset;
345 for (unsigned i = 0; i < num_additional_swms; i++)
346 wimlib_free(additional_swms[i]);
347 free(additional_swms);
355 /* Extract one image, or all images, from a WIM file into a directory. */
356 static int imagex_apply(int argc, const char **argv)
359 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
360 WIMLIB_OPEN_FLAG_SPLIT_OK;
367 const char *image_num_or_name;
368 int extract_flags = 0;
370 const char *swm_glob = NULL;
371 WIMStruct **additional_swms = NULL;
372 unsigned num_additional_swms = 0;
374 for_opt(c, apply_options) {
377 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
380 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
383 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
386 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
398 if (argc != 2 && argc != 3) {
405 image_num_or_name = "1";
408 image_num_or_name = argv[1];
412 ret = wimlib_open_wim(wimfile, open_flags, &w);
416 image = wimlib_resolve_image(w, image_num_or_name);
417 ret = verify_image_exists(image, image_num_or_name, wimfile);
421 num_images = wimlib_get_num_images(w);
422 if (argc == 2 && num_images != 1) {
423 imagex_error("`%s' contains %d images; Please select one "
424 "(or all)", wimfile, num_images);
431 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
433 &num_additional_swms);
441 ret = stat(dir, &stbuf);
443 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
444 const char *ntfs_device = dir;
445 printf("Applying image %d of `%s' to NTFS filesystem on `%s'\n",
446 image, wimfile, ntfs_device);
447 ret = wimlib_apply_image_to_ntfs_volume(w, image,
451 num_additional_swms);
455 if (errno != ENOENT) {
456 imagex_error_with_errno("Failed to stat `%s'", dir);
463 ret = wimlib_extract_image(w, image, dir, extract_flags,
464 additional_swms, num_additional_swms);
467 if (additional_swms) {
468 for (unsigned i = 0; i < num_additional_swms; i++)
469 wimlib_free(additional_swms[i]);
470 free(additional_swms);
475 static int imagex_capture_or_append(int argc, const char **argv)
478 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
479 int add_image_flags = 0;
480 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
481 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
486 const char *flags_element = NULL;
487 const char *config_file = NULL;
488 char *config_str = NULL;
489 size_t config_len = 0;
494 int cmd = strcmp(argv[0], "append") ? CAPTURE : APPEND;
496 for_opt(c, capture_or_append_options) {
499 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
502 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
503 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
506 config_file = optarg;
509 compression_type = get_compression_type(optarg);
510 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
514 flags_element = optarg;
517 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
520 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
521 write_flags |= WIMLIB_WRITE_FLAG_VERBOSE;
530 if (argc < 2 || argc > 4) {
537 char dir_copy[strlen(dir) + 1];
538 memcpy(dir_copy, dir, strlen(dir) + 1);
539 default_name = basename(dir_copy);
541 name = (argc >= 3) ? argv[2] : default_name;
542 desc = (argc >= 4) ? argv[3] : NULL;
545 config_str = file_get_contents(config_file, &config_len);
551 ret = wimlib_open_wim(wimfile, open_flags, &w);
553 ret = wimlib_create_new_wim(compression_type, &w);
560 ret = stat(dir, &stbuf);
562 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
563 const char *ntfs_device = dir;
564 printf("Capturing WIM image NTFS filesystem on `%s'\n",
566 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
574 if (errno != ENOENT) {
575 imagex_error_with_errno("Failed to stat `%s'", dir);
581 ret = wimlib_add_image(w, dir, name, config_str, config_len,
587 cur_image = wimlib_get_num_images(w);
589 ret = wimlib_set_image_descripton(w, cur_image, desc);
594 ret = wimlib_set_image_flags(w, cur_image, flags_element);
599 ret = wimlib_overwrite(w, write_flags);
601 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
603 imagex_error("Failed to write the WIM file `%s'", wimfile);
610 /* Remove image(s) from a WIM. */
611 static int imagex_delete(int argc, const char **argv)
614 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
615 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
617 const char *image_num_or_name;
622 for_opt(c, delete_options) {
625 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
626 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
638 imagex_error("Must specify a WIM file");
640 imagex_error("Must specify an image");
645 image_num_or_name = argv[1];
647 ret = file_writable(wimfile);
651 ret = wimlib_open_wim(wimfile, open_flags, &w);
655 image = wimlib_resolve_image(w, image_num_or_name);
657 ret = verify_image_exists(image, image_num_or_name, wimfile);
661 ret = wimlib_delete_image(w, image);
663 imagex_error("Failed to delete image from `%s'", wimfile);
667 ret = wimlib_overwrite(w, write_flags);
669 imagex_error("Failed to write the file `%s' with image "
677 /* Print the files contained in an image(s) in a WIM file. */
678 static int imagex_dir(int argc, const char **argv)
687 imagex_error("Must specify a WIM file");
692 imagex_error("Too many arguments");
698 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
703 image = wimlib_resolve_image(w, argv[2]);
704 ret = verify_image_exists(image, argv[2], wimfile);
708 /* Image was not specified. If the WIM only contains one image,
709 * choose that one; otherwise, print an error. */
710 num_images = wimlib_get_num_images(w);
711 if (num_images != 1) {
712 imagex_error("The file `%s' contains %d images; Please "
713 "select one.", wimfile, num_images);
721 ret = wimlib_print_files(w, image);
727 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
729 static int imagex_export(int argc, const char **argv)
732 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
733 int export_flags = 0;
734 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
735 int compression_type;
736 bool compression_type_specified = false;
737 const char *src_wimfile;
738 const char *src_image_num_or_name;
739 const char *dest_wimfile;
740 const char *dest_name;
741 const char *dest_desc;
742 WIMStruct *src_w = NULL;
743 WIMStruct *dest_w = NULL;
748 const char *swm_glob = NULL;
749 WIMStruct **additional_swms = NULL;
750 unsigned num_additional_swms = 0;
752 for_opt(c, export_options) {
755 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
758 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
759 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
762 compression_type = get_compression_type(optarg);
763 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
765 compression_type_specified = true;
777 if (argc < 3 || argc > 5) {
781 src_wimfile = argv[0];
782 src_image_num_or_name = argv[1];
783 dest_wimfile = argv[2];
784 dest_name = (argc >= 4) ? argv[3] : NULL;
785 dest_desc = (argc >= 5) ? argv[4] : NULL;
786 ret = wimlib_open_wim(src_wimfile,
787 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w);
791 /* Determine if the destination is an existing file or not.
792 * If so, we try to append the exported image(s) to it; otherwise, we
793 * create a new WIM containing the exported image(s). */
794 if (stat(dest_wimfile, &stbuf) == 0) {
798 /* Destination file exists. */
799 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
800 imagex_error("`%s' is not a regular file",
805 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
809 ret = file_writable(dest_wimfile);
813 dest_ctype = wimlib_get_compression_type(dest_w);
814 if (compression_type_specified
815 && compression_type != dest_ctype)
817 imagex_error("Cannot specify a compression type that is "
818 "not the same as that used in the "
823 compression_type = dest_ctype;
826 /* dest_wimfile is not an existing file, so create a new WIM. */
827 if (!compression_type_specified)
828 compression_type = wimlib_get_compression_type(src_w);
829 if (errno == ENOENT) {
830 ret = wimlib_create_new_wim(compression_type, &dest_w);
834 imagex_error_with_errno("Cannot stat file `%s'",
841 image = wimlib_resolve_image(src_w, src_image_num_or_name);
842 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
847 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
849 &num_additional_swms);
854 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
855 export_flags, additional_swms,
856 num_additional_swms);
862 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
865 ret = wimlib_overwrite(dest_w, write_flags);
869 if (additional_swms) {
870 for (unsigned i = 0; i < num_additional_swms; i++)
871 wimlib_free(additional_swms[i]);
872 free(additional_swms);
877 /* Prints information about a WIM file; also can mark an image as bootable,
878 * change the name of an image, or change the description of an image. */
879 static int imagex_info(int argc, const char **argv)
885 bool lookup_table = false;
887 bool metadata = false;
888 bool short_header = true;
889 const char *xml_out_file = NULL;
891 const char *image_num_or_name = "all";
892 const char *new_name = NULL;
893 const char *new_desc = NULL;
898 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
899 WIMLIB_OPEN_FLAG_SPLIT_OK;
903 for_opt(c, info_options) {
913 short_header = false;
917 short_header = false;
921 short_header = false;
924 xml_out_file = optarg;
925 short_header = false;
929 short_header = false;
939 if (argc == 0 || argc > 4) {
945 image_num_or_name = argv[1];
955 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
957 ret = wimlib_open_wim(wimfile, open_flags, &w);
961 part_number = wimlib_get_part_number(w, &total_parts);
963 image = wimlib_resolve_image(w, image_num_or_name);
964 if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
965 imagex_error("The image `%s' does not exist",
968 imagex_error("If you would like to set the boot "
969 "index to 0, specify image \"0\" with "
971 ret = WIMLIB_ERR_INVALID_IMAGE;
975 if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
977 imagex_error("Cannot specify the --boot flag "
978 "without specifying a specific "
979 "image in a multi-image WIM");
980 ret = WIMLIB_ERR_INVALID_IMAGE;
984 imagex_error("Cannot specify the NEW_NAME "
985 "without specifying a specific "
986 "image in a multi-image WIM");
987 ret = WIMLIB_ERR_INVALID_IMAGE;
992 /* Operations that print information are separated from operations that
993 * recreate the WIM file. */
994 if (!new_name && !boot) {
996 /* Read-only operations */
998 if (image == WIM_NO_IMAGE) {
999 imagex_error("`%s' is not a valid image",
1001 ret = WIMLIB_ERR_INVALID_IMAGE;
1005 if (image == WIM_ALL_IMAGES && short_header)
1006 wimlib_print_wim_information(w);
1009 wimlib_print_header(w);
1012 if (total_parts != 1) {
1013 printf("Warning: Only showing the lookup table "
1014 "for part %d of a %d-part WIM.\n",
1015 part_number, total_parts);
1017 wimlib_print_lookup_table(w);
1021 ret = wimlib_extract_xml_data(w, stdout);
1027 fp = fopen(xml_out_file, "wb");
1029 imagex_error_with_errno("Failed to open the "
1035 ret = wimlib_extract_xml_data(w, fp);
1036 if (fclose(fp) != 0) {
1037 imagex_error("Failed to close the file `%s'",
1047 wimlib_print_available_images(w, image);
1050 ret = wimlib_print_metadata(w, image);
1056 /* Modification operations */
1057 if (total_parts != 1) {
1058 imagex_error("Modifying a split WIM is not supported.");
1061 if (image == WIM_ALL_IMAGES)
1064 if (image == WIM_NO_IMAGE && new_name) {
1065 imagex_error("Cannot specify new_name (`%s') when "
1066 "using image 0", new_name);
1071 if (image == wimlib_get_boot_idx(w)) {
1072 printf("Image %d is already marked as "
1073 "bootable.\n", image);
1076 printf("Marking image %d as bootable.\n",
1078 wimlib_set_boot_idx(w, image);
1082 if (strcmp(wimlib_get_image_name(w, image),
1084 printf("Image %d is already named \"%s\".\n",
1088 printf("Changing the name of image %d to "
1089 "\"%s\".\n", image, new_name);
1090 ret = wimlib_set_image_name(w, image, new_name);
1096 const char *old_desc;
1097 old_desc = wimlib_get_image_description(w, image);
1098 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1099 printf("The description of image %d is already "
1100 "\"%s\".\n", image, new_desc);
1103 printf("Changing the description of image %d "
1104 "to \"%s\".\n", image, new_desc);
1105 ret = wimlib_set_image_descripton(w, image,
1112 /* Only call wimlib_overwrite_xml_and_header() if something
1113 * actually needs to be changed. */
1114 if (boot || new_name || new_desc ||
1115 check != wimlib_has_integrity_table(w)) {
1117 ret = file_writable(wimfile);
1121 ret = wimlib_overwrite_xml_and_header(w, check ?
1122 WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
1123 WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
1125 printf("The file `%s' was not modified because nothing "
1126 "needed to be done.\n", wimfile);
1135 /* Join split WIMs into one part WIM */
1136 static int imagex_join(int argc, const char **argv)
1139 int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1140 const char *output_path;
1142 for_opt(c, join_options) {
1145 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1155 imagex_error("Must specify at least one split WIM (.swm) parts "
1159 output_path = argv[0];
1160 return wimlib_join(++argv, --argc, output_path, flags);
1166 /* Mounts an image using a FUSE mount. */
1167 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1170 int mount_flags = 0;
1171 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
1172 WIMLIB_OPEN_FLAG_SPLIT_OK;
1173 const char *wimfile;
1179 const char *swm_glob = NULL;
1180 WIMStruct **additional_swms = NULL;
1181 unsigned num_additional_swms = 0;
1183 if (strcmp(argv[0], "mountrw") == 0)
1184 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1186 for_opt(c, mount_options) {
1189 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1192 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1195 if (strcasecmp(optarg, "none") == 0)
1196 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1197 else if (strcasecmp(optarg, "xattr") == 0)
1198 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1199 else if (strcasecmp(optarg, "windows") == 0)
1200 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1202 imagex_error("Unknown stream interface \"%s\"", optarg);
1215 if (argc != 2 && argc != 3)
1220 ret = wimlib_open_wim(wimfile, open_flags, &w);
1225 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1227 &num_additional_swms);
1234 num_images = wimlib_get_num_images(w);
1235 if (num_images != 1) {
1236 imagex_error("The file `%s' contains %d images; Please "
1237 "select one.", wimfile, num_images);
1238 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1245 image = wimlib_resolve_image(w, argv[1]);
1247 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
1252 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1253 ret = file_writable(wimfile);
1258 ret = wimlib_mount(w, image, dir, mount_flags, additional_swms,
1259 num_additional_swms);
1261 imagex_error("Failed to mount image %d from `%s' on `%s'",
1262 image, wimfile, dir);
1267 if (additional_swms)
1268 for (unsigned i = 0; i < num_additional_swms; i++)
1269 wimlib_free(additional_swms[i]);
1272 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1277 /* Split a WIM into a spanned set */
1278 static int imagex_split(int argc, const char **argv)
1281 int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1282 unsigned long part_size;
1285 for_opt(c, split_options) {
1288 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1302 part_size = strtod(argv[2], &tmp) * (1 << 20);
1303 if (tmp == argv[2] || *tmp) {
1304 imagex_error("Invalid part size \"%s\"", argv[2]);
1305 imagex_error("The part size must be an integer or floating-point number of megabytes.");
1308 return wimlib_split(argv[0], argv[1], part_size, flags);
1311 /* Unmounts an image. */
1312 static int imagex_unmount(int argc, const char **argv)
1315 int unmount_flags = 0;
1318 for_opt(c, unmount_options) {
1321 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1324 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1338 ret = wimlib_unmount(argv[0], unmount_flags);
1340 imagex_error("Failed to unmount `%s'", argv[0]);
1344 struct imagex_command {
1346 int (*func)(int , const char **);
1351 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1352 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1354 static const struct imagex_command imagex_commands[] = {
1355 {"append", imagex_capture_or_append, APPEND},
1356 {"apply", imagex_apply, APPLY},
1357 {"capture", imagex_capture_or_append, CAPTURE},
1358 {"delete", imagex_delete, DELETE},
1359 {"dir", imagex_dir, DIR},
1360 {"export", imagex_export, EXPORT},
1361 {"info", imagex_info, INFO},
1362 {"join", imagex_join, JOIN},
1363 {"mount", imagex_mount_rw_or_ro, MOUNT},
1364 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1365 {"split", imagex_split, SPLIT},
1366 {"unmount", imagex_unmount, UNMOUNT},
1369 static void version()
1371 static const char *s =
1372 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
1373 "Copyright (C) 2012 Eric Biggers\n"
1374 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
1375 "This is free software: you are free to change and redistribute it.\n"
1376 "There is NO WARRANTY, to the extent permitted by law.\n"
1378 "Report bugs to "PACKAGE_BUGREPORT".\n";
1383 static void help_or_version(int argc, const char **argv)
1387 const struct imagex_command *cmd;
1389 for (i = 1; i < argc; i++) {
1397 if (strcmp(p, "help") == 0) {
1398 for_imagex_command(cmd) {
1399 if (strcmp(cmd->name, argv[1]) == 0) {
1407 if (strcmp(p, "version") == 0) {
1415 static void usage(int cmd_type)
1417 const struct imagex_command *cmd;
1418 printf("Usage: %s", usage_strings[cmd_type]);
1419 for_imagex_command(cmd) {
1420 if (cmd->cmd == cmd_type)
1421 printf("\nTry `man imagex-%s' for more details.\n",
1426 static void usage_all()
1428 puts("IMAGEX: Usage:");
1429 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
1430 fputs(usage_strings[i], stdout);
1431 static const char *extra =
1433 " imagex --version\n"
1435 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
1437 " Try `man imagex' for more information.\n"
1439 fputs(extra, stdout);
1443 int main(int argc, const char **argv)
1445 const struct imagex_command *cmd;
1449 imagex_error("No command specified");
1454 help_or_version(argc, argv);
1458 wimlib_set_print_errors(true);
1460 for_imagex_command(cmd) {
1461 if (strcmp(cmd->name, *argv) == 0) {
1462 ret = cmd->func(argc, argv);
1464 imagex_error("Exiting with error code %d:\n"
1466 wimlib_get_error_string(ret));
1467 if (ret == WIMLIB_ERR_NTFS_3G)
1468 imagex_error_with_errno("errno");
1474 imagex_error("Unrecognized command: `%s'", argv[0]);