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)
291 ret = access(path, F_OK | W_OK);
293 imagex_error_with_errno("Can't modify `%s'", path);
297 static int open_swms_from_glob(const char *swm_glob,
298 const char *first_part,
300 WIMStruct ***additional_swms_ret,
301 unsigned *num_additional_swms_ret)
303 unsigned num_additional_swms = 0;
304 WIMStruct **additional_swms = NULL;
308 ret = glob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
310 if (ret == GLOB_NOMATCH) {
311 imagex_error("Found no files for glob \"%s\"",
314 imagex_error_with_errno("Failed to process glob "
320 num_additional_swms = globbuf.gl_pathc;
321 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
322 if (!additional_swms) {
323 imagex_error("Out of memory");
328 for (unsigned i = 0; i < num_additional_swms; i++) {
329 if (strcmp(globbuf.gl_pathv[i], first_part) == 0) {
333 ret = wimlib_open_wim(globbuf.gl_pathv[i],
334 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
335 &additional_swms[i - offset]);
339 *additional_swms_ret = additional_swms;
340 *num_additional_swms_ret = num_additional_swms - offset;
344 for (unsigned i = 0; i < num_additional_swms; i++)
345 wimlib_free(additional_swms[i]);
346 free(additional_swms);
354 /* Extract one image, or all images, from a WIM file into a directory. */
355 static int imagex_apply(int argc, const char **argv)
358 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
359 WIMLIB_OPEN_FLAG_SPLIT_OK;
366 const char *image_num_or_name;
367 int extract_flags = 0;
369 const char *swm_glob = NULL;
370 WIMStruct **additional_swms = NULL;
371 unsigned num_additional_swms = 0;
373 for_opt(c, apply_options) {
376 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
379 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
382 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
385 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
397 if (argc != 2 && argc != 3) {
404 image_num_or_name = "1";
407 image_num_or_name = argv[1];
411 ret = wimlib_open_wim(wimfile, open_flags, &w);
415 image = wimlib_resolve_image(w, image_num_or_name);
416 ret = verify_image_exists(image, image_num_or_name, wimfile);
420 num_images = wimlib_get_num_images(w);
421 if (argc == 2 && num_images != 1) {
422 imagex_error("`%s' contains %d images; Please select one "
423 "(or all)", wimfile, num_images);
430 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
432 &num_additional_swms);
440 ret = stat(dir, &stbuf);
442 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
443 const char *ntfs_device = dir;
444 printf("Applying image %d of `%s' to NTFS filesystem on `%s'\n",
445 image, wimfile, ntfs_device);
446 ret = wimlib_apply_image_to_ntfs_volume(w, image,
450 num_additional_swms);
454 if (errno != ENOENT) {
455 imagex_error_with_errno("Failed to stat `%s'", dir);
462 ret = wimlib_extract_image(w, image, dir, extract_flags,
463 additional_swms, num_additional_swms);
466 if (additional_swms) {
467 for (unsigned i = 0; i < num_additional_swms; i++)
468 wimlib_free(additional_swms[i]);
469 free(additional_swms);
474 static int imagex_capture_or_append(int argc, const char **argv)
477 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
478 int add_image_flags = 0;
479 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
480 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
485 const char *flags_element = NULL;
486 const char *config_file = NULL;
487 char *config_str = NULL;
488 size_t config_len = 0;
493 int cmd = strcmp(argv[0], "append") ? CAPTURE : APPEND;
495 for_opt(c, capture_or_append_options) {
498 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
501 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
502 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
505 config_file = optarg;
508 compression_type = get_compression_type(optarg);
509 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
513 flags_element = optarg;
516 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
519 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
520 write_flags |= WIMLIB_WRITE_FLAG_VERBOSE;
529 if (argc < 2 || argc > 4) {
536 char dir_copy[strlen(dir) + 1];
537 memcpy(dir_copy, dir, strlen(dir) + 1);
538 default_name = basename(dir_copy);
540 name = (argc >= 3) ? argv[2] : default_name;
541 desc = (argc >= 4) ? argv[3] : NULL;
544 config_str = file_get_contents(config_file, &config_len);
550 ret = wimlib_open_wim(wimfile, open_flags, &w);
552 ret = wimlib_create_new_wim(compression_type, &w);
559 ret = stat(dir, &stbuf);
561 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
562 const char *ntfs_device = dir;
563 printf("Capturing WIM image NTFS filesystem on `%s'\n",
565 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
573 if (errno != ENOENT) {
574 imagex_error_with_errno("Failed to stat `%s'", dir);
580 ret = wimlib_add_image(w, dir, name, config_str, config_len,
586 cur_image = wimlib_get_num_images(w);
588 ret = wimlib_set_image_descripton(w, cur_image, desc);
593 ret = wimlib_set_image_flags(w, cur_image, flags_element);
598 ret = wimlib_overwrite(w, write_flags);
600 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags);
602 imagex_error("Failed to write the WIM file `%s'", wimfile);
609 /* Remove image(s) from a WIM. */
610 static int imagex_delete(int argc, const char **argv)
613 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
614 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
616 const char *image_num_or_name;
621 for_opt(c, delete_options) {
624 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
625 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
637 imagex_error("Must specify a WIM file");
639 imagex_error("Must specify an image");
644 image_num_or_name = argv[1];
646 ret = file_writable(wimfile);
650 ret = wimlib_open_wim(wimfile, open_flags, &w);
654 image = wimlib_resolve_image(w, image_num_or_name);
656 ret = verify_image_exists(image, image_num_or_name, wimfile);
660 ret = wimlib_delete_image(w, image);
662 imagex_error("Failed to delete image from `%s'", wimfile);
666 ret = wimlib_overwrite(w, write_flags);
668 imagex_error("Failed to write the file `%s' with image "
676 /* Print the files contained in an image(s) in a WIM file. */
677 static int imagex_dir(int argc, const char **argv)
686 imagex_error("Must specify a WIM file");
691 imagex_error("Too many arguments");
697 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
702 image = wimlib_resolve_image(w, argv[2]);
703 ret = verify_image_exists(image, argv[2], wimfile);
707 /* Image was not specified. If the WIM only contains one image,
708 * choose that one; otherwise, print an error. */
709 num_images = wimlib_get_num_images(w);
710 if (num_images != 1) {
711 imagex_error("The file `%s' contains %d images; Please "
712 "select one.", wimfile, num_images);
720 ret = wimlib_print_files(w, image);
726 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
728 static int imagex_export(int argc, const char **argv)
731 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
732 int export_flags = 0;
733 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
734 int compression_type;
735 bool compression_type_specified = false;
736 const char *src_wimfile;
737 const char *src_image_num_or_name;
738 const char *dest_wimfile;
739 const char *dest_name;
740 const char *dest_desc;
741 WIMStruct *src_w = NULL;
742 WIMStruct *dest_w = NULL;
747 const char *swm_glob = NULL;
748 WIMStruct **additional_swms = NULL;
749 unsigned num_additional_swms = 0;
751 for_opt(c, export_options) {
754 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
757 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
758 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
761 compression_type = get_compression_type(optarg);
762 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
764 compression_type_specified = true;
776 if (argc < 3 || argc > 5) {
780 src_wimfile = argv[0];
781 src_image_num_or_name = argv[1];
782 dest_wimfile = argv[2];
783 dest_name = (argc >= 4) ? argv[3] : NULL;
784 dest_desc = (argc >= 5) ? argv[4] : NULL;
785 ret = wimlib_open_wim(src_wimfile,
786 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w);
790 /* Determine if the destination is an existing file or not.
791 * If so, we try to append the exported image(s) to it; otherwise, we
792 * create a new WIM containing the exported image(s). */
793 if (stat(dest_wimfile, &stbuf) == 0) {
797 /* Destination file exists. */
798 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
799 imagex_error("`%s' is not a regular file",
804 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
808 ret = file_writable(dest_wimfile);
812 dest_ctype = wimlib_get_compression_type(dest_w);
813 if (compression_type_specified
814 && compression_type != dest_ctype)
816 imagex_error("Cannot specify a compression type that is "
817 "not the same as that used in the "
822 compression_type = dest_ctype;
825 /* dest_wimfile is not an existing file, so create a new WIM. */
826 if (!compression_type_specified)
827 compression_type = wimlib_get_compression_type(src_w);
828 if (errno == ENOENT) {
829 ret = wimlib_create_new_wim(compression_type, &dest_w);
833 imagex_error_with_errno("Cannot stat file `%s'",
840 image = wimlib_resolve_image(src_w, src_image_num_or_name);
841 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
846 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
848 &num_additional_swms);
853 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
854 export_flags, additional_swms,
855 num_additional_swms);
861 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
864 ret = wimlib_overwrite(dest_w, write_flags);
868 if (additional_swms) {
869 for (unsigned i = 0; i < num_additional_swms; i++)
870 wimlib_free(additional_swms[i]);
871 free(additional_swms);
876 /* Prints information about a WIM file; also can mark an image as bootable,
877 * change the name of an image, or change the description of an image. */
878 static int imagex_info(int argc, const char **argv)
884 bool lookup_table = false;
886 bool metadata = false;
887 bool short_header = true;
888 const char *xml_out_file = NULL;
890 const char *image_num_or_name = "all";
891 const char *new_name = NULL;
892 const char *new_desc = NULL;
897 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
898 WIMLIB_OPEN_FLAG_SPLIT_OK;
902 for_opt(c, info_options) {
912 short_header = false;
916 short_header = false;
920 short_header = false;
923 xml_out_file = optarg;
924 short_header = false;
928 short_header = false;
938 if (argc == 0 || argc > 4) {
944 image_num_or_name = argv[1];
954 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
956 ret = wimlib_open_wim(wimfile, open_flags, &w);
960 part_number = wimlib_get_part_number(w, &total_parts);
962 image = wimlib_resolve_image(w, image_num_or_name);
963 if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
964 imagex_error("The image `%s' does not exist",
967 imagex_error("If you would like to set the boot "
968 "index to 0, specify image \"0\" with "
970 ret = WIMLIB_ERR_INVALID_IMAGE;
974 if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
976 imagex_error("Cannot specify the --boot flag "
977 "without specifying a specific "
978 "image in a multi-image WIM");
979 ret = WIMLIB_ERR_INVALID_IMAGE;
983 imagex_error("Cannot specify the NEW_NAME "
984 "without specifying a specific "
985 "image in a multi-image WIM");
986 ret = WIMLIB_ERR_INVALID_IMAGE;
991 /* Operations that print information are separated from operations that
992 * recreate the WIM file. */
993 if (!new_name && !boot) {
995 /* Read-only operations */
997 if (image == WIM_NO_IMAGE) {
998 imagex_error("`%s' is not a valid image",
1000 ret = WIMLIB_ERR_INVALID_IMAGE;
1004 if (image == WIM_ALL_IMAGES && short_header)
1005 wimlib_print_wim_information(w);
1008 wimlib_print_header(w);
1011 if (total_parts != 1) {
1012 printf("Warning: Only showing the lookup table "
1013 "for part %d of a %d-part WIM.\n",
1014 part_number, total_parts);
1016 wimlib_print_lookup_table(w);
1020 ret = wimlib_extract_xml_data(w, stdout);
1026 fp = fopen(xml_out_file, "wb");
1028 imagex_error_with_errno("Failed to open the "
1034 ret = wimlib_extract_xml_data(w, fp);
1035 if (fclose(fp) != 0) {
1036 imagex_error("Failed to close the file `%s'",
1046 wimlib_print_available_images(w, image);
1049 ret = wimlib_print_metadata(w, image);
1055 /* Modification operations */
1056 if (total_parts != 1) {
1057 imagex_error("Modifying a split WIM is not supported.");
1060 if (image == WIM_ALL_IMAGES)
1063 if (image == WIM_NO_IMAGE && new_name) {
1064 imagex_error("Cannot specify new_name (`%s') when "
1065 "using image 0", new_name);
1070 if (image == wimlib_get_boot_idx(w)) {
1071 printf("Image %d is already marked as "
1072 "bootable.\n", image);
1075 printf("Marking image %d as bootable.\n",
1077 wimlib_set_boot_idx(w, image);
1081 if (strcmp(wimlib_get_image_name(w, image),
1083 printf("Image %d is already named \"%s\".\n",
1087 printf("Changing the name of image %d to "
1088 "\"%s\".\n", image, new_name);
1089 ret = wimlib_set_image_name(w, image, new_name);
1095 const char *old_desc;
1096 old_desc = wimlib_get_image_description(w, image);
1097 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1098 printf("The description of image %d is already "
1099 "\"%s\".\n", image, new_desc);
1102 printf("Changing the description of image %d "
1103 "to \"%s\".\n", image, new_desc);
1104 ret = wimlib_set_image_descripton(w, image,
1111 /* Only call wimlib_overwrite_xml_and_header() if something
1112 * actually needs to be changed. */
1113 if (boot || new_name || new_desc ||
1114 check != wimlib_has_integrity_table(w)) {
1116 ret = file_writable(wimfile);
1120 ret = wimlib_overwrite_xml_and_header(w, check ?
1121 WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
1122 WIMLIB_WRITE_FLAG_SHOW_PROGRESS : 0);
1124 printf("The file `%s' was not modified because nothing "
1125 "needed to be done.\n", wimfile);
1134 /* Join split WIMs into one part WIM */
1135 static int imagex_join(int argc, const char **argv)
1138 int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1139 const char *output_path;
1141 for_opt(c, join_options) {
1144 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1154 imagex_error("Must specify at least one split WIM (.swm) parts "
1158 output_path = argv[0];
1159 return wimlib_join(++argv, --argc, output_path, flags);
1165 /* Mounts an image using a FUSE mount. */
1166 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1169 int mount_flags = 0;
1170 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
1171 WIMLIB_OPEN_FLAG_SPLIT_OK;
1172 const char *wimfile;
1178 const char *swm_glob = NULL;
1179 WIMStruct **additional_swms = NULL;
1180 unsigned num_additional_swms = 0;
1182 if (strcmp(argv[0], "mountrw") == 0)
1183 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1185 for_opt(c, mount_options) {
1188 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1191 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1194 if (strcasecmp(optarg, "none") == 0)
1195 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1196 else if (strcasecmp(optarg, "xattr") == 0)
1197 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1198 else if (strcasecmp(optarg, "windows") == 0)
1199 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1201 imagex_error("Unknown stream interface \"%s\"", optarg);
1214 if (argc != 2 && argc != 3)
1219 ret = wimlib_open_wim(wimfile, open_flags, &w);
1224 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1226 &num_additional_swms);
1233 num_images = wimlib_get_num_images(w);
1234 if (num_images != 1) {
1235 imagex_error("The file `%s' contains %d images; Please "
1236 "select one.", wimfile, num_images);
1237 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1244 image = wimlib_resolve_image(w, argv[1]);
1246 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
1251 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1252 ret = file_writable(wimfile);
1257 ret = wimlib_mount(w, image, dir, mount_flags, additional_swms,
1258 num_additional_swms);
1260 imagex_error("Failed to mount image %d from `%s' on `%s'",
1261 image, wimfile, dir);
1266 if (additional_swms) {
1267 for (unsigned i = 0; i < num_additional_swms; i++)
1268 wimlib_free(additional_swms[i]);
1269 free(additional_swms);
1273 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1278 /* Split a WIM into a spanned set */
1279 static int imagex_split(int argc, const char **argv)
1282 int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1283 unsigned long part_size;
1286 for_opt(c, split_options) {
1289 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1303 part_size = strtod(argv[2], &tmp) * (1 << 20);
1304 if (tmp == argv[2] || *tmp) {
1305 imagex_error("Invalid part size \"%s\"", argv[2]);
1306 imagex_error("The part size must be an integer or floating-point number of megabytes.");
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 **);
1352 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1353 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1355 static const struct imagex_command imagex_commands[] = {
1356 {"append", imagex_capture_or_append, APPEND},
1357 {"apply", imagex_apply, APPLY},
1358 {"capture", imagex_capture_or_append, CAPTURE},
1359 {"delete", imagex_delete, DELETE},
1360 {"dir", imagex_dir, DIR},
1361 {"export", imagex_export, EXPORT},
1362 {"info", imagex_info, INFO},
1363 {"join", imagex_join, JOIN},
1364 {"mount", imagex_mount_rw_or_ro, MOUNT},
1365 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1366 {"split", imagex_split, SPLIT},
1367 {"unmount", imagex_unmount, UNMOUNT},
1370 static void version()
1372 static const char *s =
1373 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
1374 "Copyright (C) 2012 Eric Biggers\n"
1375 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
1376 "This is free software: you are free to change and redistribute it.\n"
1377 "There is NO WARRANTY, to the extent permitted by law.\n"
1379 "Report bugs to "PACKAGE_BUGREPORT".\n";
1384 static void help_or_version(int argc, const char **argv)
1388 const struct imagex_command *cmd;
1390 for (i = 1; i < argc; i++) {
1398 if (strcmp(p, "help") == 0) {
1399 for_imagex_command(cmd) {
1400 if (strcmp(cmd->name, argv[1]) == 0) {
1408 if (strcmp(p, "version") == 0) {
1416 static void usage(int cmd_type)
1418 const struct imagex_command *cmd;
1419 printf("Usage: %s", usage_strings[cmd_type]);
1420 for_imagex_command(cmd) {
1421 if (cmd->cmd == cmd_type)
1422 printf("\nTry `man imagex-%s' for more details.\n",
1427 static void usage_all()
1429 puts("IMAGEX: Usage:");
1430 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
1431 fputs(usage_strings[i], stdout);
1432 static const char *extra =
1434 " imagex --version\n"
1436 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
1438 " Try `man imagex' for more information.\n"
1440 fputs(extra, stdout);
1444 int main(int argc, const char **argv)
1446 const struct imagex_command *cmd;
1450 imagex_error("No command specified");
1455 help_or_version(argc, argv);
1459 wimlib_set_print_errors(true);
1461 for_imagex_command(cmd) {
1462 if (strcmp(cmd->name, *argv) == 0) {
1463 ret = cmd->func(argc, argv);
1465 imagex_error("Exiting with error code %d:\n"
1467 wimlib_get_error_string(ret));
1468 if (ret == WIMLIB_ERR_NTFS_3G)
1469 imagex_error_with_errno("errno");
1475 imagex_error("Unrecognized command: `%s'", argv[0]);