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 for_opt(c, opts) while ((c = getopt_long_only(argc, (char**)argv, "", \
61 static void usage(int cmd_type);
62 static void usage_all();
64 static const char *usage_strings[] = {
66 "imagex append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
67 " [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
68 " [--verbose] [--dereference] [--config=FILE]\n"
69 " [--threads=NUM_THREADS] [--rebuild]\n",
71 "imagex apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
72 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
73 " [--symlink] [--verbose] [--ref=\"GLOB\"]\n",
75 "imagex capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
76 " [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
77 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
78 " [--config=FILE] [--threads=NUM_THREADS]\n",
80 "imagex delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n",
82 "imagex dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n",
84 "imagex export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
85 " DEST_WIMFILE [DEST_IMAGE_NAME] [DEST_IMAGE_DESCRIPTION]\n"
86 " [--boot] [--check] [--compress=TYPE] [--ref=\"GLOB\"]\n"
87 " [--threads=NUM_THREADS] [--rebuild]\n",
89 "imagex info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
90 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
91 " [--xml] [--extract-xml FILE] [--metadata]\n",
93 "imagex join [--check] WIMFILE SPLIT_WIM...\n",
95 "imagex mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
96 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
97 " [--ref=\"GLOB\"]\n",
99 "imagex mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
100 " [--check] [--debug] [--streams-interface=INTERFACE]\n",
102 "imagex optimize WIMFILE [--check] [--recompress]\n",
104 "imagex split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n",
106 "imagex unmount DIRECTORY [--commit] [--check]\n",
109 static const struct option common_options[] = {
110 {"help", 0, NULL, 'h'},
111 {"version", 0, NULL, 'v'},
115 static const struct option apply_options[] = {
116 {"check", no_argument, NULL, 'c'},
117 {"hardlink", no_argument, NULL, 'h'},
118 {"symlink", no_argument, NULL, 's'},
119 {"verbose", no_argument, NULL, 'v'},
120 {"ref", required_argument, NULL, 'r'},
123 static const struct option capture_or_append_options[] = {
124 {"boot", no_argument, NULL, 'b'},
125 {"check", no_argument, NULL, 'c'},
126 {"compress", required_argument, NULL, 'x'},
127 {"config", required_argument, NULL, 'C'},
128 {"dereference", no_argument, NULL, 'L'},
129 {"flags", required_argument, NULL, 'f'},
130 {"verbose", no_argument, NULL, 'v'},
131 {"threads", required_argument, NULL, 't'},
132 {"rebuild", no_argument, NULL, 'R'},
135 static const struct option delete_options[] = {
136 {"check", no_argument, NULL, 'c'},
140 static const struct option export_options[] = {
141 {"boot", no_argument, NULL, 'b'},
142 {"check", no_argument, NULL, 'c'},
143 {"compress", required_argument, NULL, 'x'},
144 {"ref", required_argument, NULL, 'r'},
145 {"threads", required_argument, NULL, 't'},
146 {"rebuild", no_argument, NULL, 'R'},
150 static const struct option info_options[] = {
151 {"boot", no_argument, NULL, 'b'},
152 {"check", no_argument, NULL, 'c'},
153 {"extract-xml", required_argument, NULL, 'X'},
154 {"header", no_argument, NULL, 'h'},
155 {"lookup-table", no_argument, NULL, 'l'},
156 {"metadata", no_argument, NULL, 'm'},
157 {"xml", no_argument, NULL, 'x'},
161 static const struct option join_options[] = {
162 {"check", no_argument, NULL, 'c'},
166 static const struct option mount_options[] = {
167 {"check", no_argument, NULL, 'c'},
168 {"debug", no_argument, NULL, 'd'},
169 {"streams-interface", required_argument, NULL, 's'},
170 {"ref", required_argument, NULL, 'r'},
174 static const struct option optimize_options[] = {
175 {"check", no_argument, NULL, 'c'},
176 {"recompress", no_argument, NULL, 'r'},
180 static const struct option split_options[] = {
181 {"check", no_argument, NULL, 'c'},
185 static const struct option unmount_options[] = {
186 {"commit", no_argument, NULL, 'c'},
187 {"check", no_argument, NULL, 'C'},
193 /* Print formatted error message to stderr. */
194 static void imagex_error(const char *format, ...)
197 va_start(va, format);
198 fputs("ERROR: ", stderr);
199 vfprintf(stderr, format, va);
204 /* Print formatted error message to stderr. */
205 static void imagex_error_with_errno(const char *format, ...)
207 int errno_save = errno;
209 va_start(va, format);
210 fputs("ERROR: ", stderr);
211 vfprintf(stderr, format, va);
212 fprintf(stderr, ": %s\n", strerror(errno_save));
216 static int verify_image_exists(int image, const char *image_name,
217 const char *wim_name)
219 if (image == WIM_NO_IMAGE) {
220 imagex_error("\"%s\" is not a valid image in `%s'!\n"
221 " Please specify a 1-based imagex index or "
223 " You may use `imagex info' to list the images "
224 "contained in a WIM.",
225 image_name, wim_name);
231 static int verify_image_is_single(int image)
233 if (image == WIM_ALL_IMAGES) {
234 imagex_error("Cannot specify all images for this action!");
240 static int verify_image_exists_and_is_single(int image, const char *image_name,
241 const char *wim_name)
244 ret = verify_image_exists(image, image_name, wim_name);
246 ret = verify_image_is_single(image);
250 static int get_compression_type(const char *optarg)
252 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
253 return WIM_COMPRESSION_TYPE_LZX;
254 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
255 return WIM_COMPRESSION_TYPE_XPRESS;
256 else if (strcasecmp(optarg, "none") == 0)
257 return WIM_COMPRESSION_TYPE_NONE;
259 imagex_error("Invalid compression type `%s'! Must be "
260 "\"maximum\", \"fast\", or \"none\".", optarg);
261 return WIM_COMPRESSION_TYPE_INVALID;
265 static off_t file_get_size(const char *filename)
268 if (stat(filename, &st) == 0)
274 static char *file_get_contents(const char *filename, size_t *len_ret)
281 if (stat(filename, &stbuf) != 0) {
282 imagex_error_with_errno("Failed to stat the file `%s'", filename);
287 fp = fopen(filename, "rb");
289 imagex_error_with_errno("Failed to open the file `%s'", filename);
295 imagex_error("Failed to allocate buffer of %zu bytes to hold "
296 "contents of file `%s'", len, filename);
299 if (fread(buf, 1, len, fp) != len) {
300 imagex_error_with_errno("Failed to read %lu bytes from the "
301 "file `%s'", len, filename);
313 static int file_writable(const char *path)
316 ret = access(path, F_OK | W_OK);
318 imagex_error_with_errno("Can't modify `%s'", path);
322 static int open_swms_from_glob(const char *swm_glob,
323 const char *first_part,
325 WIMStruct ***additional_swms_ret,
326 unsigned *num_additional_swms_ret)
328 unsigned num_additional_swms = 0;
329 WIMStruct **additional_swms = NULL;
333 ret = glob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
335 if (ret == GLOB_NOMATCH) {
336 imagex_error("Found no files for glob \"%s\"",
339 imagex_error_with_errno("Failed to process glob "
345 num_additional_swms = globbuf.gl_pathc;
346 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
347 if (!additional_swms) {
348 imagex_error("Out of memory");
353 for (unsigned i = 0; i < num_additional_swms; i++) {
354 if (strcmp(globbuf.gl_pathv[i], first_part) == 0) {
358 ret = wimlib_open_wim(globbuf.gl_pathv[i],
359 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
360 &additional_swms[i - offset]);
364 *additional_swms_ret = additional_swms;
365 *num_additional_swms_ret = num_additional_swms - offset;
369 for (unsigned i = 0; i < num_additional_swms; i++)
370 wimlib_free(additional_swms[i]);
371 free(additional_swms);
379 static unsigned parse_num_threads(const char *optarg)
382 unsigned nthreads = strtoul(optarg, &tmp, 10);
383 if (nthreads == UINT_MAX || *tmp || tmp == optarg) {
384 imagex_error("Number of threads must be a non-negative integer!");
392 /* Extract one image, or all images, from a WIM file into a directory. */
393 static int imagex_apply(int argc, const char **argv)
396 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
397 WIMLIB_OPEN_FLAG_SPLIT_OK;
404 const char *image_num_or_name;
405 int extract_flags = 0;
407 const char *swm_glob = NULL;
408 WIMStruct **additional_swms = NULL;
409 unsigned num_additional_swms = 0;
411 for_opt(c, apply_options) {
414 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
417 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
420 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
423 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
435 if (argc != 2 && argc != 3) {
442 image_num_or_name = "1";
445 image_num_or_name = argv[1];
449 ret = wimlib_open_wim(wimfile, open_flags, &w);
453 image = wimlib_resolve_image(w, image_num_or_name);
454 ret = verify_image_exists(image, image_num_or_name, wimfile);
458 num_images = wimlib_get_num_images(w);
459 if (argc == 2 && num_images != 1) {
460 imagex_error("`%s' contains %d images; Please select one "
461 "(or all)", wimfile, num_images);
468 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
470 &num_additional_swms);
478 ret = stat(dir, &stbuf);
480 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
481 const char *ntfs_device = dir;
482 printf("Applying image %d of `%s' to NTFS filesystem on `%s'\n",
483 image, wimfile, ntfs_device);
484 ret = wimlib_apply_image_to_ntfs_volume(w, image,
488 num_additional_swms);
492 if (errno != ENOENT) {
493 imagex_error_with_errno("Failed to stat `%s'", dir);
500 ret = wimlib_extract_image(w, image, dir, extract_flags,
501 additional_swms, num_additional_swms);
504 if (additional_swms) {
505 for (unsigned i = 0; i < num_additional_swms; i++)
506 wimlib_free(additional_swms[i]);
507 free(additional_swms);
512 static int imagex_capture_or_append(int argc, const char **argv)
515 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
516 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_SHOW_PROGRESS;
517 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
518 int compression_type = WIM_COMPRESSION_TYPE_XPRESS;
523 const char *flags_element = NULL;
524 const char *config_file = NULL;
525 char *config_str = NULL;
526 size_t config_len = 0;
531 int cmd = strcmp(argv[0], "append") ? CAPTURE : APPEND;
532 unsigned num_threads = 0;
534 for_opt(c, capture_or_append_options) {
537 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
540 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
541 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
544 config_file = optarg;
547 compression_type = get_compression_type(optarg);
548 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
552 flags_element = optarg;
555 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
558 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
559 write_flags |= WIMLIB_WRITE_FLAG_VERBOSE;
562 num_threads = parse_num_threads(optarg);
563 if (num_threads == UINT_MAX)
567 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
576 if (argc < 2 || argc > 4) {
583 char dir_copy[strlen(dir) + 1];
584 memcpy(dir_copy, dir, strlen(dir) + 1);
585 default_name = basename(dir_copy);
587 name = (argc >= 3) ? argv[2] : default_name;
588 desc = (argc >= 4) ? argv[3] : NULL;
591 config_str = file_get_contents(config_file, &config_len);
597 ret = wimlib_open_wim(wimfile, open_flags, &w);
599 ret = wimlib_create_new_wim(compression_type, &w);
606 ret = stat(dir, &stbuf);
608 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
609 const char *ntfs_device = dir;
610 printf("Capturing WIM image NTFS filesystem on `%s'\n",
612 ret = wimlib_add_image_from_ntfs_volume(w, ntfs_device,
620 if (errno != ENOENT) {
621 imagex_error_with_errno("Failed to stat `%s'", dir);
627 ret = wimlib_add_image(w, dir, name, config_str, config_len,
633 cur_image = wimlib_get_num_images(w);
635 ret = wimlib_set_image_descripton(w, cur_image, desc);
640 ret = wimlib_set_image_flags(w, cur_image, flags_element);
645 ret = wimlib_overwrite(w, write_flags, num_threads);
647 ret = wimlib_write(w, wimfile, WIM_ALL_IMAGES, write_flags,
650 if (ret == WIMLIB_ERR_REOPEN)
653 imagex_error("Failed to write the WIM file `%s'", wimfile);
660 /* Remove image(s) from a WIM. */
661 static int imagex_delete(int argc, const char **argv)
664 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
665 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
667 const char *image_num_or_name;
672 for_opt(c, delete_options) {
675 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
676 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
688 imagex_error("Must specify a WIM file");
690 imagex_error("Must specify an image");
695 image_num_or_name = argv[1];
697 ret = file_writable(wimfile);
701 ret = wimlib_open_wim(wimfile, open_flags, &w);
705 image = wimlib_resolve_image(w, image_num_or_name);
707 ret = verify_image_exists(image, image_num_or_name, wimfile);
711 ret = wimlib_delete_image(w, image);
713 imagex_error("Failed to delete image from `%s'", wimfile);
717 ret = wimlib_overwrite(w, write_flags, 0);
718 if (ret == WIMLIB_ERR_REOPEN)
721 imagex_error("Failed to write the file `%s' with image "
729 /* Print the files contained in an image(s) in a WIM file. */
730 static int imagex_dir(int argc, const char **argv)
739 imagex_error("Must specify a WIM file");
744 imagex_error("Too many arguments");
750 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
755 image = wimlib_resolve_image(w, argv[2]);
756 ret = verify_image_exists(image, argv[2], wimfile);
760 /* Image was not specified. If the WIM only contains one image,
761 * choose that one; otherwise, print an error. */
762 num_images = wimlib_get_num_images(w);
763 if (num_images != 1) {
764 imagex_error("The file `%s' contains %d images; Please "
765 "select one.", wimfile, num_images);
773 ret = wimlib_print_files(w, image);
779 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
781 static int imagex_export(int argc, const char **argv)
784 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
785 int export_flags = 0;
786 int write_flags = WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
787 int compression_type;
788 bool compression_type_specified = false;
789 const char *src_wimfile;
790 const char *src_image_num_or_name;
791 const char *dest_wimfile;
792 const char *dest_name;
793 const char *dest_desc;
794 WIMStruct *src_w = NULL;
795 WIMStruct *dest_w = NULL;
800 const char *swm_glob = NULL;
801 WIMStruct **additional_swms = NULL;
802 unsigned num_additional_swms = 0;
803 unsigned num_threads = 0;
805 for_opt(c, export_options) {
808 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
811 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
812 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
815 compression_type = get_compression_type(optarg);
816 if (compression_type == WIM_COMPRESSION_TYPE_INVALID)
818 compression_type_specified = true;
824 num_threads = parse_num_threads(optarg);
825 if (num_threads == UINT_MAX)
829 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
838 if (argc < 3 || argc > 5) {
842 src_wimfile = argv[0];
843 src_image_num_or_name = argv[1];
844 dest_wimfile = argv[2];
845 dest_name = (argc >= 4) ? argv[3] : NULL;
846 dest_desc = (argc >= 5) ? argv[4] : NULL;
847 ret = wimlib_open_wim(src_wimfile,
848 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w);
852 /* Determine if the destination is an existing file or not.
853 * If so, we try to append the exported image(s) to it; otherwise, we
854 * create a new WIM containing the exported image(s). */
855 if (stat(dest_wimfile, &stbuf) == 0) {
859 /* Destination file exists. */
860 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
861 imagex_error("`%s' is not a regular file",
866 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w);
870 ret = file_writable(dest_wimfile);
874 dest_ctype = wimlib_get_compression_type(dest_w);
875 if (compression_type_specified
876 && compression_type != dest_ctype)
878 imagex_error("Cannot specify a compression type that is "
879 "not the same as that used in the "
884 compression_type = dest_ctype;
887 /* dest_wimfile is not an existing file, so create a new WIM. */
888 if (!compression_type_specified)
889 compression_type = wimlib_get_compression_type(src_w);
890 if (errno == ENOENT) {
891 ret = wimlib_create_new_wim(compression_type, &dest_w);
895 imagex_error_with_errno("Cannot stat file `%s'",
902 image = wimlib_resolve_image(src_w, src_image_num_or_name);
903 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
908 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
910 &num_additional_swms);
915 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
916 export_flags, additional_swms,
917 num_additional_swms);
923 ret = wimlib_write(dest_w, dest_wimfile, WIM_ALL_IMAGES,
924 write_flags, num_threads);
926 ret = wimlib_overwrite(dest_w, write_flags, num_threads);
928 if (ret == WIMLIB_ERR_REOPEN)
932 if (additional_swms) {
933 for (unsigned i = 0; i < num_additional_swms; i++)
934 wimlib_free(additional_swms[i]);
935 free(additional_swms);
940 /* Prints information about a WIM file; also can mark an image as bootable,
941 * change the name of an image, or change the description of an image. */
942 static int imagex_info(int argc, const char **argv)
948 bool lookup_table = false;
950 bool metadata = false;
951 bool short_header = true;
952 const char *xml_out_file = NULL;
954 const char *image_num_or_name = "all";
955 const char *new_name = NULL;
956 const char *new_desc = NULL;
961 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
962 WIMLIB_OPEN_FLAG_SPLIT_OK;
966 for_opt(c, info_options) {
976 short_header = false;
980 short_header = false;
984 short_header = false;
987 xml_out_file = optarg;
988 short_header = false;
992 short_header = false;
1002 if (argc == 0 || argc > 4) {
1008 image_num_or_name = argv[1];
1018 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1020 ret = wimlib_open_wim(wimfile, open_flags, &w);
1024 part_number = wimlib_get_part_number(w, &total_parts);
1026 image = wimlib_resolve_image(w, image_num_or_name);
1027 if (image == WIM_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
1028 imagex_error("The image `%s' does not exist",
1031 imagex_error("If you would like to set the boot "
1032 "index to 0, specify image \"0\" with "
1033 "the --boot flag.");
1034 ret = WIMLIB_ERR_INVALID_IMAGE;
1038 if (image == WIM_ALL_IMAGES && wimlib_get_num_images(w) > 1) {
1040 imagex_error("Cannot specify the --boot flag "
1041 "without specifying a specific "
1042 "image in a multi-image WIM");
1043 ret = WIMLIB_ERR_INVALID_IMAGE;
1047 imagex_error("Cannot specify the NEW_NAME "
1048 "without specifying a specific "
1049 "image in a multi-image WIM");
1050 ret = WIMLIB_ERR_INVALID_IMAGE;
1055 /* Operations that print information are separated from operations that
1056 * recreate the WIM file. */
1057 if (!new_name && !boot) {
1059 /* Read-only operations */
1061 if (image == WIM_NO_IMAGE) {
1062 imagex_error("`%s' is not a valid image",
1064 ret = WIMLIB_ERR_INVALID_IMAGE;
1068 if (image == WIM_ALL_IMAGES && short_header)
1069 wimlib_print_wim_information(w);
1072 wimlib_print_header(w);
1075 if (total_parts != 1) {
1076 printf("Warning: Only showing the lookup table "
1077 "for part %d of a %d-part WIM.\n",
1078 part_number, total_parts);
1080 wimlib_print_lookup_table(w);
1084 ret = wimlib_extract_xml_data(w, stdout);
1090 fp = fopen(xml_out_file, "wb");
1092 imagex_error_with_errno("Failed to open the "
1098 ret = wimlib_extract_xml_data(w, fp);
1099 if (fclose(fp) != 0) {
1100 imagex_error("Failed to close the file `%s'",
1110 wimlib_print_available_images(w, image);
1113 ret = wimlib_print_metadata(w, image);
1119 /* Modification operations */
1120 if (total_parts != 1) {
1121 imagex_error("Modifying a split WIM is not supported.");
1124 if (image == WIM_ALL_IMAGES)
1127 if (image == WIM_NO_IMAGE && new_name) {
1128 imagex_error("Cannot specify new_name (`%s') when "
1129 "using image 0", new_name);
1134 if (image == wimlib_get_boot_idx(w)) {
1135 printf("Image %d is already marked as "
1136 "bootable.\n", image);
1139 printf("Marking image %d as bootable.\n",
1141 wimlib_set_boot_idx(w, image);
1145 if (strcmp(wimlib_get_image_name(w, image),
1147 printf("Image %d is already named \"%s\".\n",
1151 printf("Changing the name of image %d to "
1152 "\"%s\".\n", image, new_name);
1153 ret = wimlib_set_image_name(w, image, new_name);
1159 const char *old_desc;
1160 old_desc = wimlib_get_image_description(w, image);
1161 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1162 printf("The description of image %d is already "
1163 "\"%s\".\n", image, new_desc);
1166 printf("Changing the description of image %d "
1167 "to \"%s\".\n", image, new_desc);
1168 ret = wimlib_set_image_descripton(w, image,
1175 /* Only call wimlib_overwrite() if something actually needs to
1177 if (boot || new_name || new_desc ||
1178 check != wimlib_has_integrity_table(w)) {
1180 ret = file_writable(wimfile);
1186 write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY |
1187 WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
1192 ret = wimlib_overwrite(w, write_flags, 1);
1193 if (ret == WIMLIB_ERR_REOPEN)
1196 printf("The file `%s' was not modified because nothing "
1197 "needed to be done.\n", wimfile);
1206 /* Join split WIMs into one part WIM */
1207 static int imagex_join(int argc, const char **argv)
1210 int flags = WIMLIB_OPEN_FLAG_SPLIT_OK | WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1211 const char *output_path;
1213 for_opt(c, join_options) {
1216 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1226 imagex_error("Must specify at least one split WIM (.swm) parts "
1230 output_path = argv[0];
1231 return wimlib_join(++argv, --argc, output_path, flags);
1237 /* Mounts an image using a FUSE mount. */
1238 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1241 int mount_flags = 0;
1242 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS |
1243 WIMLIB_OPEN_FLAG_SPLIT_OK;
1244 const char *wimfile;
1250 const char *swm_glob = NULL;
1251 WIMStruct **additional_swms = NULL;
1252 unsigned num_additional_swms = 0;
1254 if (strcmp(argv[0], "mountrw") == 0)
1255 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1257 for_opt(c, mount_options) {
1260 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1263 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1266 if (strcasecmp(optarg, "none") == 0)
1267 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1268 else if (strcasecmp(optarg, "xattr") == 0)
1269 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1270 else if (strcasecmp(optarg, "windows") == 0)
1271 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1273 imagex_error("Unknown stream interface \"%s\"", optarg);
1286 if (argc != 2 && argc != 3)
1291 ret = wimlib_open_wim(wimfile, open_flags, &w);
1296 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1298 &num_additional_swms);
1305 num_images = wimlib_get_num_images(w);
1306 if (num_images != 1) {
1307 imagex_error("The file `%s' contains %d images; Please "
1308 "select one.", wimfile, num_images);
1309 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1316 image = wimlib_resolve_image(w, argv[1]);
1318 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
1323 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1324 ret = file_writable(wimfile);
1329 ret = wimlib_mount(w, image, dir, mount_flags, additional_swms,
1330 num_additional_swms);
1332 imagex_error("Failed to mount image %d from `%s' on `%s'",
1333 image, wimfile, dir);
1338 if (additional_swms) {
1339 for (unsigned i = 0; i < num_additional_swms; i++)
1340 wimlib_free(additional_swms[i]);
1341 free(additional_swms);
1345 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1350 static int imagex_optimize(int argc, const char **argv)
1353 int open_flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1354 int write_flags = WIMLIB_WRITE_FLAG_REBUILD |
1355 WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
1358 const char *wimfile;
1362 for_opt(c, optimize_options) {
1365 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1366 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1369 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1386 ret = wimlib_open_wim(wimfile, open_flags, &w);
1390 old_size = file_get_size(argv[0]);
1391 printf("`%s' original size: ", wimfile);
1395 printf("%"PRIu64" KiB\n", old_size >> 10);
1397 ret = wimlib_overwrite(w, write_flags, 0);
1399 new_size = file_get_size(argv[0]);
1400 printf("`%s' optimized size: ", wimfile);
1404 printf("%"PRIu64" KiB\n", new_size >> 10);
1406 fputs("Space saved: ", stdout);
1407 if (new_size != -1 && old_size != -1) {
1408 printf("%lld KiB\n",
1409 ((long long)old_size - (long long)new_size) >> 10);
1418 /* Split a WIM into a spanned set */
1419 static int imagex_split(int argc, const char **argv)
1422 int flags = WIMLIB_OPEN_FLAG_SHOW_PROGRESS;
1423 unsigned long part_size;
1426 for_opt(c, split_options) {
1429 flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1443 part_size = strtod(argv[2], &tmp) * (1 << 20);
1444 if (tmp == argv[2] || *tmp) {
1445 imagex_error("Invalid part size \"%s\"", argv[2]);
1446 imagex_error("The part size must be an integer or floating-point number of megabytes.");
1449 return wimlib_split(argv[0], argv[1], part_size, flags);
1452 /* Unmounts an image. */
1453 static int imagex_unmount(int argc, const char **argv)
1456 int unmount_flags = 0;
1459 for_opt(c, unmount_options) {
1462 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1465 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1479 ret = wimlib_unmount(argv[0], unmount_flags);
1481 imagex_error("Failed to unmount `%s'", argv[0]);
1485 struct imagex_command {
1487 int (*func)(int , const char **);
1492 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1493 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1495 static const struct imagex_command imagex_commands[] = {
1496 {"append", imagex_capture_or_append, APPEND},
1497 {"apply", imagex_apply, APPLY},
1498 {"capture", imagex_capture_or_append, CAPTURE},
1499 {"delete", imagex_delete, DELETE},
1500 {"dir", imagex_dir, DIR},
1501 {"export", imagex_export, EXPORT},
1502 {"info", imagex_info, INFO},
1503 {"join", imagex_join, JOIN},
1504 {"mount", imagex_mount_rw_or_ro, MOUNT},
1505 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1506 {"optimize",imagex_optimize, OPTIMIZE},
1507 {"split", imagex_split, SPLIT},
1508 {"unmount", imagex_unmount, UNMOUNT},
1511 static void version()
1513 static const char *s =
1514 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
1515 "Copyright (C) 2012 Eric Biggers\n"
1516 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
1517 "This is free software: you are free to change and redistribute it.\n"
1518 "There is NO WARRANTY, to the extent permitted by law.\n"
1520 "Report bugs to "PACKAGE_BUGREPORT".\n";
1525 static void help_or_version(int argc, const char **argv)
1529 const struct imagex_command *cmd;
1531 for (i = 1; i < argc; i++) {
1539 if (strcmp(p, "help") == 0) {
1540 for_imagex_command(cmd) {
1541 if (strcmp(cmd->name, argv[1]) == 0) {
1549 if (strcmp(p, "version") == 0) {
1557 static void usage(int cmd_type)
1559 const struct imagex_command *cmd;
1560 printf("Usage: %s", usage_strings[cmd_type]);
1561 for_imagex_command(cmd) {
1562 if (cmd->cmd == cmd_type)
1563 printf("\nTry `man imagex-%s' for more details.\n",
1568 static void usage_all()
1570 puts("IMAGEX: Usage:");
1571 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
1572 printf(" %s", usage_strings[i]);
1573 static const char *extra =
1575 " imagex --version\n"
1577 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
1579 " Try `man imagex' for more information.\n"
1581 fputs(extra, stdout);
1585 int main(int argc, const char **argv)
1587 const struct imagex_command *cmd;
1591 imagex_error("No command specified");
1596 help_or_version(argc, argv);
1600 wimlib_set_print_errors(true);
1602 for_imagex_command(cmd) {
1603 if (strcmp(cmd->name, *argv) == 0) {
1604 ret = cmd->func(argc, argv);
1606 imagex_error("Exiting with error code %d:\n"
1608 wimlib_get_error_string(ret));
1609 if (ret == WIMLIB_ERR_NTFS_3G)
1610 imagex_error_with_errno("errno");
1616 imagex_error("Unrecognized command: `%s'", argv[0]);