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/>.
36 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
38 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (char**)argv, "", \
57 static void usage(int cmd_type);
58 static void usage_all();
60 static const char *usage_strings[] = {
62 " imagex append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
63 " [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
64 " [--verbose] [--dereference] [--config=FILE]\n",
66 " imagex apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
67 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
68 " [--symlink] [--verbose] [--ref=\"GLOB\"]\n",
70 " imagex capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
71 " [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
72 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
75 " imagex delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n",
77 " imagex dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n",
79 " imagex export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
80 " DEST_WIMFILE [DEST_IMAGE_NAME]\n"
81 " [DEST_IMAGE_DESCRIPTION] [--boot] [--check]\n"
82 " [--compress=TYPE] [--ref=\"GLOB\"]\n",
84 " imagex info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
85 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
86 " [--xml] [--extract-xml FILE] [--metadata]\n",
88 " imagex join [--check] WIMFILE SPLIT_WIM...\n",
90 " imagex mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
91 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
92 " [--ref=\"GLOB\"]\n",
94 " imagex mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
95 " [--check] [--debug] [--streams-interface=INTERFACE]\n",
97 " imagex split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n",
99 " imagex unmount DIRECTORY [--commit] [--check]\n",
102 static const struct option common_options[] = {
103 {"help", 0, NULL, 'h'},
104 {"version", 0, NULL, 'v'},
108 static const struct option apply_options[] = {
109 {"check", no_argument, NULL, 'c'},
110 {"hardlink", no_argument, NULL, 'h'},
111 {"symlink", no_argument, NULL, 's'},
112 {"verbose", no_argument, NULL, 'v'},
113 {"ref", required_argument, NULL, 'r'},
116 static const struct option capture_or_append_options[] = {
117 {"boot", no_argument, NULL, 'b'},
118 {"check", no_argument, NULL, 'c'},
119 {"compress", required_argument, NULL, 'x'},
120 {"config", required_argument, NULL, 'C'},
121 {"dereference", no_argument, NULL, 'L'},
122 {"flags", required_argument, NULL, 'f'},
123 {"verbose", no_argument, NULL, 'v'},
126 static const struct option delete_options[] = {
127 {"check", no_argument, NULL, 'c'},
131 static const struct option export_options[] = {
132 {"boot", no_argument, NULL, 'b'},
133 {"check", no_argument, NULL, 'c'},
134 {"compress", required_argument, NULL, 'x'},
135 {"ref", required_argument, NULL, 'r'},
139 static const struct option info_options[] = {
140 {"boot", no_argument, NULL, 'b'},
141 {"check", no_argument, NULL, 'c'},
142 {"extract-xml", required_argument, NULL, 'X'},
143 {"header", no_argument, NULL, 'h'},
144 {"lookup-table", no_argument, NULL, 'l'},
145 {"metadata", no_argument, NULL, 'm'},
146 {"xml", no_argument, NULL, 'x'},
150 static const struct option join_options[] = {
151 {"check", no_argument, NULL, 'c'},
155 static const struct option mount_options[] = {
156 {"check", no_argument, NULL, 'c'},
157 {"debug", no_argument, NULL, 'd'},
158 {"streams-interface", required_argument, NULL, 's'},
159 {"ref", required_argument, NULL, 'r'},
163 static const struct option split_options[] = {
164 {"check", no_argument, NULL, 'c'},
168 static const struct option unmount_options[] = {
169 {"commit", no_argument, NULL, 'c'},
170 {"check", no_argument, NULL, 'C'},
176 /* Print formatted error message to stderr. */
177 static void imagex_error(const char *format, ...)
180 va_start(va, format);
181 fputs("ERROR: ", stderr);
182 vfprintf(stderr, format, va);
187 /* Print formatted error message to stderr. */
188 static void imagex_error_with_errno(const char *format, ...)
190 int errno_save = errno;
192 va_start(va, format);
193 fputs("ERROR: ", stderr);
194 vfprintf(stderr, format, va);
195 fprintf(stderr, ": %s\n", strerror(errno_save));
199 static int verify_image_exists(int image)
201 if (image == WIM_NO_IMAGE) {
202 imagex_error("Not a valid image");
203 return WIMLIB_ERR_INVALID_IMAGE;
208 static int verify_image_is_single(int image)
210 if (image == WIM_ALL_IMAGES) {
211 imagex_error("Cannot specify all images for this action");
212 return WIMLIB_ERR_INVALID_IMAGE;
217 static int verify_image_exists_and_is_single(int image)
220 ret = verify_image_exists(image);
222 ret = verify_image_is_single(image);
226 static int get_compression_type(const char *optarg)
228 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
229 return WIM_COMPRESSION_TYPE_LZX;
230 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
231 return WIM_COMPRESSION_TYPE_XPRESS;
232 else if (strcasecmp(optarg, "none") == 0)
233 return WIM_COMPRESSION_TYPE_NONE;
235 imagex_error("Invalid compression type `%s'! Must be "
236 "\"maximum\", \"fast\", or \"none\".", optarg);
237 return WIM_COMPRESSION_TYPE_INVALID;
241 static char *file_get_contents(const char *filename, size_t *len_ret)
248 if (stat(filename, &stbuf) != 0) {
249 imagex_error_with_errno("Failed to stat the file `%s'", filename);
254 fp = fopen(filename, "rb");
256 imagex_error_with_errno("Failed to open the file `%s'", filename);
262 imagex_error("Failed to allocate buffer of %zu bytes to hold "
263 "contents of file `%s'", len, filename);
266 if (fread(buf, 1, len, fp) != len) {
267 imagex_error_with_errno("Failed to read %lu bytes from the "
268 "file `%s'", len, filename);
280 static int open_swms_from_glob(const char *swm_glob,
281 const char *first_part,
283 WIMStruct ***additional_swms_ret,
284 unsigned *num_additional_swms_ret)
286 unsigned num_additional_swms = 0;
287 WIMStruct **additional_swms = NULL;
291 ret = glob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
293 if (ret == GLOB_NOMATCH) {
294 imagex_error("Found no files for glob \"%s\"",
297 imagex_error_with_errno("Failed to process glob "
303 num_additional_swms = globbuf.gl_pathc;
304 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
305 if (!additional_swms) {
306 imagex_error("Out of memory");
311 for (unsigned i = 0; i < num_additional_swms; i++) {
312 if (strcmp(globbuf.gl_pathv[i], first_part) == 0) {
316 ret = wimlib_open_wim(globbuf.gl_pathv[i],
317 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
318 &additional_swms[i - offset]);
322 *additional_swms_ret = additional_swms;
323 *num_additional_swms_ret = num_additional_swms - offset;
327 for (unsigned i = 0; i < num_additional_swms; i++)
328 wimlib_free(additional_swms[i]);
329 free(additional_swms);
337 /* Extract one image, or all images, from a WIM file into a directory. */
338 static int imagex_apply(int argc, const char **argv)
341 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
342 WIMLIB_OPEN_FLAG_SPLIT_OK;
349 const char *image_num_or_name;
350 int extract_flags = 0;
352 const char *swm_glob = NULL;
353 WIMStruct **additional_swms = NULL;
354 unsigned num_additional_swms = 0;
356 for_opt(c, apply_options) {
359 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
362 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
365 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
368 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
380 if (argc != 2 && argc != 3) {
387 image_num_or_name = "1";
390 image_num_or_name = argv[1];
394 ret = wimlib_open_wim(wimfile, open_flags, &w);
398 image = wimlib_resolve_image(w, image_num_or_name);
399 ret = verify_image_exists(image);
403 num_images = wimlib_get_num_images(w);
404 if (argc == 2 && num_images != 1) {
405 imagex_error("`%s' contains %d images; Please select one "
406 "(or all)", wimfile, num_images);
413 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
415 &num_additional_swms);
423 ret = stat(dir, &stbuf);
425 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
426 const char *ntfs_device = dir;
427 printf("Applying image %d of `%s' to NTFS filesystem on `%s'\n",
428 image, wimfile, ntfs_device);
429 ret = wimlib_apply_image_to_ntfs_volume(w, image,
433 num_additional_swms);
437 if (errno != ENOENT) {
438 imagex_error_with_errno("Failed to stat `%s'", dir);
445 ret = wimlib_extract_image(w, image, dir, extract_flags,
446 additional_swms, num_additional_swms);
449 if (additional_swms) {
450 for (unsigned i = 0; i < num_additional_swms; i++)
451 wimlib_free(additional_swms[i]);
452 free(additional_swms);
457 static int imagex_capture_or_append(int argc, const char **argv)
460 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
461 int add_image_flags = 0;
462 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
463 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
468 const char *flags_element = NULL;
469 const char *config_file = NULL;
470 char *config_str = NULL;
471 size_t config_len = 0;
476 int cmd = strcmp(argv[0], "append") ? CAPTURE : APPEND;
478 for_opt(c, capture_or_append_options) {
481 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
484 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
485 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
488 config_file = optarg;
491 compression_type = get_compression_type(optarg);
492 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
496 flags_element = optarg;
499 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
502 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
503 write_flags |= WIMLIB_WRITE_FLAG_VERBOSE;
512 if (argc < 2 || argc > 4) {
519 char dir_copy[strlen(dir) + 1];
520 memcpy(dir_copy, dir, strlen(dir) + 1);
521 default_name = basename(dir_copy);
523 name = (argc >= 3) ? argv[2] : default_name;
524 desc = (argc >= 4) ? argv[3] : NULL;
527 config_str = file_get_contents(config_file, &config_len);
533 ret = wimlib_open_wim(wimfile, open_flags, &w);
535 ret = wimlib_create_new_wim(compression_type, &w);
542 ret = stat(dir, &stbuf);
544 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
545 const char *ntfs_device = dir;
546 printf("Capturing WIM image NTFS filesystem on `%s'\n",
548 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
556 if (errno != ENOENT) {
557 imagex_error_with_errno("Failed to stat `%s'", dir);
563 ret = wimlib_add_image(w, dir, name, config_str, config_len,
569 cur_image = wimlib_get_num_images(w);
571 ret = wimlib_set_image_descripton(w, cur_image, desc);
576 ret = wimlib_set_image_flags(w, cur_image, flags_element);
581 ret = wimlib_overwrite(w, write_flags);
583 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
585 imagex_error("Failed to write the WIM file `%s'", wimfile);
592 /* Remove image(s) from a WIM. */
593 static int imagex_delete(int argc, const char **argv)
596 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
597 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
599 const char *image_num_or_name;
604 for_opt(c, delete_options) {
607 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
608 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
620 imagex_error("Must specify a WIM file");
622 imagex_error("Must specify an image");
627 image_num_or_name = argv[1];
629 ret = wimlib_open_wim(wimfile, open_flags, &w);
633 image = wimlib_resolve_image(w, image_num_or_name);
635 ret = verify_image_exists(image);
639 ret = wimlib_delete_image(w, image);
641 imagex_error("Failed to delete image from `%s'", wimfile);
645 ret = wimlib_overwrite(w, write_flags);
647 imagex_error("Failed to write the file `%s' with image "
655 /* Print the files contained in an image(s) in a WIM file. */
656 static int imagex_dir(int argc, const char **argv)
665 imagex_error("Must specify a WIM file");
670 imagex_error("Too many arguments");
676 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
681 image = wimlib_resolve_image(w, argv[2]);
682 ret = verify_image_exists(image);
686 /* Image was not specified. If the WIM only contains one image,
687 * choose that one; otherwise, print an error. */
688 num_images = wimlib_get_num_images(w);
689 if (num_images != 1) {
690 imagex_error("The file `%s' contains %d images; Please "
691 "select one.", wimfile, num_images);
699 ret = wimlib_print_files(w, image);
705 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
707 static int imagex_export(int argc, const char **argv)
710 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
711 int export_flags = 0;
712 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
713 int compression_type;
714 bool compression_type_specified = false;
715 const char *src_wimfile;
716 const char *src_image_num_or_name;
717 const char *dest_wimfile;
718 const char *dest_name;
719 const char *dest_desc;
720 WIMStruct *src_w = NULL;
721 WIMStruct *dest_w = NULL;
726 const char *swm_glob = NULL;
727 WIMStruct **additional_swms = NULL;
728 unsigned num_additional_swms = 0;
730 for_opt(c, export_options) {
733 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
736 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
737 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
740 compression_type = get_compression_type(optarg);
741 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
743 compression_type_specified = true;
755 if (argc < 3 || argc > 5) {
759 src_wimfile = argv[0];
760 src_image_num_or_name = argv[1];
761 dest_wimfile = argv[2];
762 dest_name = (argc >= 4) ? argv[3] : NULL;
763 dest_desc = (argc >= 5) ? argv[4] : NULL;
764 ret = wimlib_open_wim(src_wimfile,
765 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w);
769 /* Determine if the destination is an existing file or not.
770 * If so, we try to append the exported image(s) to it; otherwise, we
771 * create a new WIM containing the exported image(s). */
772 if (stat(dest_wimfile, &stbuf) == 0) {
776 /* Destination file exists. */
777 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
778 imagex_error("`%s' is not a regular file",
783 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
787 dest_ctype = wimlib_get_compression_type(dest_w);
788 if (compression_type_specified
789 && compression_type != dest_ctype)
791 imagex_error("Cannot specify a compression type that is "
792 "not the same as that used in the "
797 compression_type = dest_ctype;
800 /* dest_wimfile is not an existing file, so create a new WIM. */
801 if (!compression_type_specified)
802 compression_type = wimlib_get_compression_type(src_w);
803 if (errno == ENOENT) {
804 ret = wimlib_create_new_wim(compression_type, &dest_w);
808 imagex_error_with_errno("Cannot stat file `%s'",
815 image = wimlib_resolve_image(src_w, src_image_num_or_name);
816 ret = verify_image_exists(image);
821 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
823 &num_additional_swms);
828 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
829 export_flags, additional_swms,
830 num_additional_swms);
836 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
839 ret = wimlib_overwrite(dest_w, write_flags);
843 if (additional_swms) {
844 for (unsigned i = 0; i < num_additional_swms; i++)
845 wimlib_free(additional_swms[i]);
846 free(additional_swms);
851 /* Prints information about a WIM file; also can mark an image as bootable,
852 * change the name of an image, or change the description of an image. */
853 static int imagex_info(int argc, const char **argv)
859 bool lookup_table = false;
861 bool metadata = false;
862 bool short_header = true;
863 const char *xml_out_file = NULL;
865 const char *image_num_or_name = "all";
866 const char *new_name = NULL;
867 const char *new_desc = NULL;
872 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
873 WIMLIB_OPEN_FLAG_SPLIT_OK;
877 for_opt(c, info_options) {
887 short_header = false;
891 short_header = false;
895 short_header = false;
898 xml_out_file = optarg;
899 short_header = false;
903 short_header = false;
913 if (argc == 0 || argc > 4) {
919 image_num_or_name = argv[1];
929 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
931 ret = wimlib_open_wim(wimfile, open_flags, &w);
935 part_number = wimlib_get_part_number(w, &total_parts);
937 image = wimlib_resolve_image(w, image_num_or_name);
938 if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
939 imagex_error("The image `%s' does not exist",
942 imagex_error("If you would like to set the boot "
943 "index to 0, specify image \"0\" with "
945 ret = WIMLIB_ERR_INVALID_IMAGE;
949 if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
951 imagex_error("Cannot specify the --boot flag "
952 "without specifying a specific "
953 "image in a multi-image WIM");
954 ret = WIMLIB_ERR_INVALID_IMAGE;
958 imagex_error("Cannot specify the NEW_NAME "
959 "without specifying a specific "
960 "image in a multi-image WIM");
961 ret = WIMLIB_ERR_INVALID_IMAGE;
966 /* Operations that print information are separated from operations that
967 * recreate the WIM file. */
968 if (!new_name && !boot) {
970 /* Read-only operations */
972 if (image == WIM_NO_IMAGE) {
973 imagex_error("`%s' is not a valid image",
975 ret = WIMLIB_ERR_INVALID_IMAGE;
979 if (image == WIM_ALL_IMAGES && short_header)
980 wimlib_print_wim_information(w);
983 wimlib_print_header(w);
986 if (total_parts != 1) {
987 printf("Warning: Only showing the lookup table "
988 "for part %d of a %d-part WIM.\n",
989 part_number, total_parts);
991 wimlib_print_lookup_table(w);
995 ret = wimlib_extract_xml_data(w, stdout);
1001 fp = fopen(xml_out_file, "wb");
1003 imagex_error_with_errno("Failed to open the "
1009 ret = wimlib_extract_xml_data(w, fp);
1010 if (fclose(fp) != 0) {
1011 imagex_error("Failed to close the file `%s'",
1021 wimlib_print_available_images(w, image);
1024 ret = wimlib_print_metadata(w, image);
1030 /* Modification operations */
1032 if (total_parts != 1) {
1033 imagex_error("Modifying a split WIM is not supported.");
1036 if (image == WIM_ALL_IMAGES)
1039 if (image == WIM_NO_IMAGE && new_name) {
1040 imagex_error("Cannot specify new_name (`%s') when "
1041 "using image 0", new_name);
1046 if (image == wimlib_get_boot_idx(w)) {
1047 printf("Image %d is already marked as "
1048 "bootable.\n", image);
1051 printf("Marking image %d as bootable.\n",
1053 wimlib_set_boot_idx(w, image);
1057 if (strcmp(wimlib_get_image_name(w, image),
1059 printf("Image %d is already named \"%s\".\n",
1063 printf("Changing the name of image %d to "
1064 "\"%s\".\n", image, new_name);
1065 ret = wimlib_set_image_name(w, image, new_name);
1071 const char *old_desc;
1072 old_desc = wimlib_get_image_description(w, image);
1073 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1074 printf("The description of image %d is already "
1075 "\"%s\".\n", image, new_desc);
1078 printf("Changing the description of image %d "
1079 "to \"%s\".\n", image, new_desc);
1080 ret = wimlib_set_image_descripton(w, image,
1087 /* Only call wimlib_overwrite_xml_and_header() if something
1088 * actually needs to be changed. */
1089 if (boot || new_name || new_desc ||
1090 check != wimlib_has_integrity_table(w)) {
1092 ret = wimlib_overwrite_xml_and_header(w, check ?
1093 WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
1094 WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
1096 printf("The file `%s' was not modified because nothing "
1097 "needed to be done.\n", wimfile);
1106 /* Join split WIMs into one part WIM */
1107 static int imagex_join(int argc, const char **argv)
1110 int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1111 const char *output_path;
1113 for_opt(c, join_options) {
1116 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1126 imagex_error("Must specify at least one split WIM (.swm) parts "
1130 output_path = argv[0];
1131 return wimlib_join(++argv, --argc, output_path, flags);
1137 /* Mounts an image using a FUSE mount. */
1138 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1141 int mount_flags = 0;
1142 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
1143 WIMLIB_OPEN_FLAG_SPLIT_OK;
1144 const char *wimfile;
1150 const char *swm_glob = NULL;
1151 WIMStruct **additional_swms = NULL;
1152 unsigned num_additional_swms = 0;
1154 if (strcmp(argv[0], "mountrw") == 0)
1155 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1157 for_opt(c, mount_options) {
1160 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1163 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1166 if (strcasecmp(optarg, "none") == 0)
1167 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1168 else if (strcasecmp(optarg, "xattr") == 0)
1169 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1170 else if (strcasecmp(optarg, "windows") == 0)
1171 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1173 imagex_error("Unknown stream interface \"%s\"", optarg);
1186 if (argc != 2 && argc != 3)
1191 ret = wimlib_open_wim(wimfile, open_flags, &w);
1196 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1198 &num_additional_swms);
1205 num_images = wimlib_get_num_images(w);
1206 if (num_images != 1) {
1207 imagex_error("The file `%s' contains %d images; Please "
1208 "select one", wimfile, num_images);
1209 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1211 ret = WIMLIB_ERR_INVALID_IMAGE;
1216 image = wimlib_resolve_image(w, argv[1]);
1220 ret = verify_image_exists_and_is_single(image);
1224 ret = wimlib_mount(w, image, dir, mount_flags, additional_swms,
1225 num_additional_swms);
1227 imagex_error("Failed to mount image %d from `%s' on `%s'",
1228 image, wimfile, dir);
1233 if (additional_swms)
1234 for (unsigned i = 0; i < num_additional_swms; i++)
1235 wimlib_free(additional_swms[i]);
1238 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1243 /* Split a WIM into a spanned set */
1244 static int imagex_split(int argc, const char **argv)
1247 int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1248 unsigned long part_size;
1251 for_opt(c, split_options) {
1254 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1268 part_size = strtod(argv[2], &tmp) * (1 << 20);
1269 if (tmp == argv[2] || *tmp) {
1270 imagex_error("Invalid part size \"%s\"", argv[2]);
1271 imagex_error("The part size must be an integer or floating-point number of megabytes.");
1274 return wimlib_split(argv[0], argv[1], part_size, flags);
1277 /* Unmounts an image. */
1278 static int imagex_unmount(int argc, const char **argv)
1281 int unmount_flags = 0;
1284 for_opt(c, unmount_options) {
1287 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1290 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1304 ret = wimlib_unmount(argv[0], unmount_flags);
1306 imagex_error("Failed to unmount `%s'", argv[0]);
1310 struct imagex_command {
1312 int (*func)(int , const char **);
1317 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1318 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1320 static const struct imagex_command imagex_commands[] = {
1321 {"append", imagex_capture_or_append, APPEND},
1322 {"apply", imagex_apply, APPLY},
1323 {"capture", imagex_capture_or_append, CAPTURE},
1324 {"delete", imagex_delete, DELETE},
1325 {"dir", imagex_dir, DIR},
1326 {"export", imagex_export, EXPORT},
1327 {"info", imagex_info, INFO},
1328 {"join", imagex_join, JOIN},
1329 {"mount", imagex_mount_rw_or_ro, MOUNT},
1330 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1331 {"split", imagex_split, SPLIT},
1332 {"unmount", imagex_unmount, UNMOUNT},
1335 static void version()
1337 static const char *s =
1338 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
1339 "Copyright (C) 2012 Eric Biggers\n"
1340 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
1341 "This is free software: you are free to change and redistribute it.\n"
1342 "There is NO WARRANTY, to the extent permitted by law.\n"
1344 "Report bugs to "PACKAGE_BUGREPORT".\n";
1349 static void help_or_version(int argc, const char **argv)
1353 const struct imagex_command *cmd;
1355 for (i = 1; i < argc; i++) {
1363 if (strcmp(p, "help") == 0) {
1364 for_imagex_command(cmd) {
1365 if (strcmp(cmd->name, argv[1]) == 0) {
1373 if (strcmp(p, "version") == 0) {
1381 static void usage(int cmd_type)
1383 const struct imagex_command *cmd;
1384 puts("IMAGEX: Usage:");
1385 fputs(usage_strings[cmd_type], stdout);
1386 for_imagex_command(cmd)
1387 if (cmd->cmd == cmd_type)
1388 printf("\nTry `man imagex-%s' for more details.\n",
1392 static void usage_all()
1394 puts("IMAGEX: Usage:");
1395 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
1396 fputs(usage_strings[i], stdout);
1397 static const char *extra =
1399 " imagex --version\n"
1401 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
1403 " Try `man imagex' for more information.\n"
1405 fputs(extra, stdout);
1409 int main(int argc, const char **argv)
1411 const struct imagex_command *cmd;
1415 imagex_error("No command specified");
1420 help_or_version(argc, argv);
1424 wimlib_set_print_errors(true);
1426 for_imagex_command(cmd) {
1427 if (strcmp(cmd->name, *argv) == 0) {
1428 ret = cmd->func(argc, argv);
1430 imagex_error("Exiting with error code %d:\n"
1432 wimlib_get_error_string(ret));
1433 if (ret == WIMLIB_ERR_NTFS_3G)
1434 imagex_error_with_errno("errno");
1440 imagex_error("Unrecognized command: `%s'", argv[0]);