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/>.
41 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
43 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (char**)argv, "", \
62 static void usage(int cmd_type);
63 static void usage_all();
65 static const char *usage_strings[] = {
67 "imagex append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
68 " [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
69 " [--verbose] [--dereference] [--config=FILE]\n"
70 " [--threads=NUM_THREADS] [--rebuild]\n",
72 "imagex apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
73 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
74 " [--symlink] [--verbose] [--ref=\"GLOB\"]\n",
76 "imagex capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
77 " [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
78 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
79 " [--config=FILE] [--threads=NUM_THREADS]\n",
81 "imagex delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check] [--soft]\n",
83 "imagex dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n",
85 "imagex export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
86 " DEST_WIMFILE [DEST_IMAGE_NAME] [DEST_IMAGE_DESCRIPTION]\n"
87 " [--boot] [--check] [--compress=TYPE] [--ref=\"GLOB\"]\n"
88 " [--threads=NUM_THREADS] [--rebuild]\n",
90 "imagex info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
91 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
92 " [--xml] [--extract-xml FILE] [--metadata]\n",
94 "imagex join [--check] WIMFILE SPLIT_WIM...\n",
96 "imagex mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
97 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
98 " [--ref=\"GLOB\"]\n",
100 "imagex mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
101 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
102 " [--staging-dir=DIR]\n",
104 "imagex optimize WIMFILE [--check] [--recompress] [--compress=TYPE]\n",
106 "imagex split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n",
108 "imagex unmount DIRECTORY [--commit] [--check]\n",
111 static const struct option common_options[] = {
112 {"help", 0, NULL, 'h'},
113 {"version", 0, NULL, 'v'},
117 static const struct option apply_options[] = {
118 {"check", no_argument, NULL, 'c'},
119 {"hardlink", no_argument, NULL, 'h'},
120 {"symlink", no_argument, NULL, 's'},
121 {"verbose", no_argument, NULL, 'v'},
122 {"ref", required_argument, NULL, 'r'},
125 static const struct option capture_or_append_options[] = {
126 {"boot", no_argument, NULL, 'b'},
127 {"check", no_argument, NULL, 'c'},
128 {"compress", required_argument, NULL, 'x'},
129 {"config", required_argument, NULL, 'C'},
130 {"dereference", no_argument, NULL, 'L'},
131 {"flags", required_argument, NULL, 'f'},
132 {"verbose", no_argument, NULL, 'v'},
133 {"threads", required_argument, NULL, 't'},
134 {"rebuild", no_argument, NULL, 'R'},
137 static const struct option delete_options[] = {
138 {"check", no_argument, NULL, 'c'},
139 {"soft", no_argument, NULL, 's'},
143 static const struct option export_options[] = {
144 {"boot", no_argument, NULL, 'b'},
145 {"check", no_argument, NULL, 'c'},
146 {"compress", required_argument, NULL, 'x'},
147 {"ref", required_argument, NULL, 'r'},
148 {"threads", required_argument, NULL, 't'},
149 {"rebuild", no_argument, NULL, 'R'},
153 static const struct option info_options[] = {
154 {"boot", no_argument, NULL, 'b'},
155 {"check", no_argument, NULL, 'c'},
156 {"extract-xml", required_argument, NULL, 'X'},
157 {"header", no_argument, NULL, 'h'},
158 {"lookup-table", no_argument, NULL, 'l'},
159 {"metadata", no_argument, NULL, 'm'},
160 {"xml", no_argument, NULL, 'x'},
164 static const struct option join_options[] = {
165 {"check", no_argument, NULL, 'c'},
169 static const struct option mount_options[] = {
170 {"check", no_argument, NULL, 'c'},
171 {"debug", no_argument, NULL, 'd'},
172 {"streams-interface", required_argument, NULL, 's'},
173 {"ref", required_argument, NULL, 'r'},
174 {"staging-dir", required_argument, NULL, 'D'},
178 static const struct option optimize_options[] = {
179 {"check", no_argument, NULL, 'c'},
180 {"recompress", no_argument, NULL, 'r'},
184 static const struct option split_options[] = {
185 {"check", no_argument, NULL, 'c'},
189 static const struct option unmount_options[] = {
190 {"commit", no_argument, NULL, 'c'},
191 {"check", no_argument, NULL, 'C'},
197 /* Print formatted error message to stderr. */
198 static void imagex_error(const char *format, ...)
201 va_start(va, format);
202 fputs("ERROR: ", stderr);
203 vfprintf(stderr, format, va);
208 /* Print formatted error message to stderr. */
209 static void imagex_error_with_errno(const char *format, ...)
211 int errno_save = errno;
213 va_start(va, format);
214 fputs("ERROR: ", stderr);
215 vfprintf(stderr, format, va);
216 fprintf(stderr, ": %s\n", strerror(errno_save));
220 static int verify_image_exists(int image, const char *image_name,
221 const char *wim_name)
223 if (image == WIMLIB_NO_IMAGE) {
224 imagex_error("\"%s\" is not a valid image in `%s'!\n"
225 " Please specify a 1-based imagex index or "
227 " You may use `imagex info' to list the images "
228 "contained in a WIM.",
229 image_name, wim_name);
235 static int verify_image_is_single(int image)
237 if (image == WIMLIB_ALL_IMAGES) {
238 imagex_error("Cannot specify all images for this action!");
244 static int verify_image_exists_and_is_single(int image, const char *image_name,
245 const char *wim_name)
248 ret = verify_image_exists(image, image_name, wim_name);
250 ret = verify_image_is_single(image);
254 static int get_compression_type(const char *optarg)
256 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
257 return WIMLIB_COMPRESSION_TYPE_LZX;
258 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
259 return WIMLIB_COMPRESSION_TYPE_XPRESS;
260 else if (strcasecmp(optarg, "none") == 0)
261 return WIMLIB_COMPRESSION_TYPE_NONE;
263 imagex_error("Invalid compression type `%s'! Must be "
264 "\"maximum\", \"fast\", or \"none\".", optarg);
265 return WIMLIB_COMPRESSION_TYPE_INVALID;
269 static off_t file_get_size(const char *filename)
272 if (stat(filename, &st) == 0)
278 static char *file_get_contents(const char *filename, size_t *len_ret)
285 if (stat(filename, &stbuf) != 0) {
286 imagex_error_with_errno("Failed to stat the file `%s'", filename);
291 fp = fopen(filename, "rb");
293 imagex_error_with_errno("Failed to open the file `%s'", filename);
299 imagex_error("Failed to allocate buffer of %zu bytes to hold "
300 "contents of file `%s'", len, filename);
303 if (fread(buf, 1, len, fp) != len) {
304 imagex_error_with_errno("Failed to read %lu bytes from the "
305 "file `%s'", len, filename);
317 static int file_writable(const char *path)
320 ret = access(path, F_OK | W_OK);
322 imagex_error_with_errno("Can't modify `%s'", path);
326 #define TO_PERCENT(numerator, denominator) \
327 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
329 static const char *get_data_type(int ctype)
332 case WIMLIB_COMPRESSION_TYPE_NONE:
333 return "uncompressed";
334 case WIMLIB_COMPRESSION_TYPE_LZX:
335 return "LZX-compressed";
336 case WIMLIB_COMPRESSION_TYPE_XPRESS:
337 return "XPRESS-compressed";
342 static int imagex_progress_func(enum wimlib_progress_msg msg,
343 const union wimlib_progress_info *info)
345 unsigned percent_done;
347 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
348 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
349 info->write_streams.total_bytes);
350 if (info->write_streams.completed_streams == 0) {
351 const char *data_type;
353 data_type = get_data_type(info->write_streams.compression_type);
354 printf("Writing %s data using %u thread%s\n",
355 data_type, info->write_streams.num_threads,
356 (info->write_streams.num_threads == 1) ? "" : "s");
358 printf("\r%"PRIu64" MiB of %"PRIu64" MiB (uncompressed) "
359 "written (%u%% done)",
360 info->write_streams.completed_bytes >> 20,
361 info->write_streams.total_bytes >> 20,
363 if (info->write_streams.completed_bytes == info->write_streams.total_bytes)
366 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
367 printf("Scanning `%s'...\n", info->scan.source);
369 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
370 if (info->scan.excluded)
371 printf("Excluding `%s' from capture\n", info->scan.cur_path);
373 printf("Scanning `%s'\n", info->scan.cur_path);
375 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
377 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
378 percent_done = TO_PERCENT(info->integrity.completed_bytes,
379 info->integrity.total_bytes);
380 printf("\rVerifying integrity of `%s': %"PRIu64" MiB "
381 "of %"PRIu64" MiB (%u%%) done",
382 info->integrity.filename,
383 info->integrity.completed_bytes >> 20,
384 info->integrity.total_bytes >> 20,
386 if (info->integrity.completed_bytes == info->integrity.total_bytes)
389 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
390 percent_done = TO_PERCENT(info->integrity.completed_bytes,
391 info->integrity.total_bytes);
392 printf("\rCalculating integrity table for WIM: %"PRIu64" MiB "
393 "of %"PRIu64" MiB (%u%%) done",
394 info->integrity.completed_bytes >> 20,
395 info->integrity.total_bytes >> 20,
397 if (info->integrity.completed_bytes == info->integrity.total_bytes)
400 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
401 printf("Applying image %d (%s) from `%s' to %s `%s'\n",
403 info->extract.image_name,
404 info->extract.wimfile_name,
405 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
406 "NTFS volume" : "directory"),
407 info->extract.target);
409 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
410 printf("Done applying WIM image.\n");
411 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
412 printf("Unmounting NTFS volume `%s'...\n",
413 info->extract.target);
416 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
417 /*printf("Applying directory structure to %s\n",*/
418 /*info->extract.target);*/
420 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
421 percent_done = TO_PERCENT(info->extract.completed_bytes,
422 info->extract.total_bytes);
423 printf("\rExtracting files: "
424 "%"PRIu64" MiB of %"PRIu64" MiB (%u%%) done",
425 info->extract.completed_bytes >> 20,
426 info->extract.total_bytes >> 20,
428 if (info->extract.completed_bytes == info->extract.total_bytes)
431 case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
432 puts(info->extract.cur_path);
434 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
435 percent_done = TO_PERCENT(info->join.completed_bytes,
436 info->join.total_bytes);
437 printf("Writing resources from part %u of %u: "
438 "%"PRIu64 " MiB of %"PRIu64" MiB (%u%%) written\n",
439 (info->join.completed_parts == info->join.total_parts) ?
440 info->join.completed_parts : info->join.completed_parts + 1,
441 info->join.total_parts,
442 info->join.completed_bytes >> 20,
443 info->join.total_bytes >> 20,
446 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
447 percent_done = TO_PERCENT(info->split.completed_bytes,
448 info->split.total_bytes);
449 printf("Writing `%s': %"PRIu64" MiB of %"PRIu64" MiB (%u%%) written\n",
450 info->split.part_name,
451 info->split.completed_bytes >> 20,
452 info->split.total_bytes >> 20,
455 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
456 if (info->split.completed_bytes == info->split.total_bytes) {
457 printf("Finished writing %u split WIM parts\n",
458 info->split.cur_part_number);
469 static int open_swms_from_glob(const char *swm_glob,
470 const char *first_part,
472 WIMStruct ***additional_swms_ret,
473 unsigned *num_additional_swms_ret)
475 unsigned num_additional_swms = 0;
476 WIMStruct **additional_swms = NULL;
480 ret = glob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
482 if (ret == GLOB_NOMATCH) {
483 imagex_error("Found no files for glob \"%s\"",
486 imagex_error_with_errno("Failed to process glob "
492 num_additional_swms = globbuf.gl_pathc;
493 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
494 if (!additional_swms) {
495 imagex_error("Out of memory");
500 for (unsigned i = 0; i < num_additional_swms; i++) {
501 if (strcmp(globbuf.gl_pathv[i], first_part) == 0) {
505 ret = wimlib_open_wim(globbuf.gl_pathv[i],
506 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
507 &additional_swms[i - offset],
508 imagex_progress_func);
512 *additional_swms_ret = additional_swms;
513 *num_additional_swms_ret = num_additional_swms - offset;
517 for (unsigned i = 0; i < num_additional_swms; i++)
518 wimlib_free(additional_swms[i]);
519 free(additional_swms);
527 static unsigned parse_num_threads(const char *optarg)
530 unsigned nthreads = strtoul(optarg, &tmp, 10);
531 if (nthreads == UINT_MAX || *tmp || tmp == optarg) {
532 imagex_error("Number of threads must be a non-negative integer!");
540 /* Extract one image, or all images, from a WIM file into a directory. */
541 static int imagex_apply(int argc, const char **argv)
544 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
551 const char *image_num_or_name;
552 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
554 const char *swm_glob = NULL;
555 WIMStruct **additional_swms = NULL;
556 unsigned num_additional_swms = 0;
558 for_opt(c, apply_options) {
561 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
564 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
567 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
570 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
582 if (argc != 2 && argc != 3) {
589 image_num_or_name = "1";
592 image_num_or_name = argv[1];
596 ret = wimlib_open_wim(wimfile, open_flags, &w, imagex_progress_func);
600 image = wimlib_resolve_image(w, image_num_or_name);
601 ret = verify_image_exists(image, image_num_or_name, wimfile);
605 num_images = wimlib_get_num_images(w);
606 if (argc == 2 && num_images != 1) {
607 imagex_error("`%s' contains %d images; Please select one "
608 "(or all)", wimfile, num_images);
615 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
617 &num_additional_swms);
624 ret = stat(target, &stbuf);
626 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
627 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
629 if (errno != ENOENT) {
630 imagex_error_with_errno("Failed to stat `%s'", target);
636 ret = wimlib_extract_image(w, image, target, extract_flags,
637 additional_swms, num_additional_swms,
638 imagex_progress_func);
641 if (additional_swms) {
642 for (unsigned i = 0; i < num_additional_swms; i++)
643 wimlib_free(additional_swms[i]);
644 free(additional_swms);
649 static int imagex_capture_or_append(int argc, const char **argv)
653 int add_image_flags = 0;
655 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
660 const char *flags_element = NULL;
661 const char *config_file = NULL;
662 char *config_str = NULL;
663 size_t config_len = 0;
668 int cmd = strcmp(argv[0], "append") ? CAPTURE : APPEND;
669 unsigned num_threads = 0;
671 for_opt(c, capture_or_append_options) {
674 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
677 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
678 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
681 config_file = optarg;
684 compression_type = get_compression_type(optarg);
685 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
689 flags_element = optarg;
692 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
695 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
698 num_threads = parse_num_threads(optarg);
699 if (num_threads == UINT_MAX)
703 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
712 if (argc < 2 || argc > 4) {
719 char source_copy[strlen(source) + 1];
720 memcpy(source_copy, source, strlen(source) + 1);
721 default_name = basename(source_copy);
723 name = (argc >= 3) ? argv[2] : default_name;
724 desc = (argc >= 4) ? argv[3] : NULL;
727 config_str = file_get_contents(config_file, &config_len);
733 ret = wimlib_open_wim(wimfile, open_flags, &w,
734 imagex_progress_func);
736 ret = wimlib_create_new_wim(compression_type, &w);
742 ret = stat(source, &stbuf);
744 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
745 printf("Capturing WIM image from NTFS filesystem on `%s'\n",
747 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
750 if (errno != ENOENT) {
751 imagex_error_with_errno("Failed to stat `%s'", source);
757 ret = wimlib_add_image(w, source, name, config_str, config_len,
758 add_image_flags, imagex_progress_func);
762 cur_image = wimlib_get_num_images(w);
764 ret = wimlib_set_image_descripton(w, cur_image, desc);
769 ret = wimlib_set_image_flags(w, cur_image, flags_element);
774 ret = wimlib_overwrite(w, write_flags, num_threads,
775 imagex_progress_func);
777 ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
778 num_threads, imagex_progress_func);
780 if (ret == WIMLIB_ERR_REOPEN)
783 imagex_error("Failed to write the WIM file `%s'", wimfile);
790 /* Remove image(s) from a WIM. */
791 static int imagex_delete(int argc, const char **argv)
797 const char *image_num_or_name;
802 for_opt(c, delete_options) {
805 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
806 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
809 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
821 imagex_error("Must specify a WIM file");
823 imagex_error("Must specify an image");
828 image_num_or_name = argv[1];
830 ret = file_writable(wimfile);
834 ret = wimlib_open_wim(wimfile, open_flags, &w,
835 imagex_progress_func);
839 image = wimlib_resolve_image(w, image_num_or_name);
841 ret = verify_image_exists(image, image_num_or_name, wimfile);
845 ret = wimlib_delete_image(w, image);
847 imagex_error("Failed to delete image from `%s'", wimfile);
851 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
852 if (ret == WIMLIB_ERR_REOPEN)
855 imagex_error("Failed to write the file `%s' with image "
863 /* Print the files contained in an image(s) in a WIM file. */
864 static int imagex_dir(int argc, const char **argv)
873 imagex_error("Must specify a WIM file");
878 imagex_error("Too many arguments");
884 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w,
885 imagex_progress_func);
890 image = wimlib_resolve_image(w, argv[2]);
891 ret = verify_image_exists(image, argv[2], wimfile);
895 /* Image was not specified. If the WIM only contains one image,
896 * choose that one; otherwise, print an error. */
897 num_images = wimlib_get_num_images(w);
898 if (num_images != 1) {
899 imagex_error("The file `%s' contains %d images; Please "
900 "select one.", wimfile, num_images);
908 ret = wimlib_print_files(w, image);
914 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
916 static int imagex_export(int argc, const char **argv)
920 int export_flags = 0;
922 int compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
923 bool compression_type_specified = false;
924 const char *src_wimfile;
925 const char *src_image_num_or_name;
926 const char *dest_wimfile;
927 const char *dest_name;
928 const char *dest_desc;
929 WIMStruct *src_w = NULL;
930 WIMStruct *dest_w = NULL;
935 const char *swm_glob = NULL;
936 WIMStruct **additional_swms = NULL;
937 unsigned num_additional_swms = 0;
938 unsigned num_threads = 0;
940 for_opt(c, export_options) {
943 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
946 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
947 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
950 compression_type = get_compression_type(optarg);
951 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
953 compression_type_specified = true;
959 num_threads = parse_num_threads(optarg);
960 if (num_threads == UINT_MAX)
964 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
973 if (argc < 3 || argc > 5) {
977 src_wimfile = argv[0];
978 src_image_num_or_name = argv[1];
979 dest_wimfile = argv[2];
980 dest_name = (argc >= 4) ? argv[3] : NULL;
981 dest_desc = (argc >= 5) ? argv[4] : NULL;
982 ret = wimlib_open_wim(src_wimfile,
983 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w,
984 imagex_progress_func);
988 /* Determine if the destination is an existing file or not.
989 * If so, we try to append the exported image(s) to it; otherwise, we
990 * create a new WIM containing the exported image(s). */
991 if (stat(dest_wimfile, &stbuf) == 0) {
995 /* Destination file exists. */
996 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
997 imagex_error("`%s' is not a regular file",
1002 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w,
1003 imagex_progress_func);
1007 ret = file_writable(dest_wimfile);
1011 dest_ctype = wimlib_get_compression_type(dest_w);
1012 if (compression_type_specified
1013 && compression_type != dest_ctype)
1015 imagex_error("Cannot specify a compression type that is "
1016 "not the same as that used in the "
1021 compression_type = dest_ctype;
1024 /* dest_wimfile is not an existing file, so create a new WIM. */
1025 if (!compression_type_specified)
1026 compression_type = wimlib_get_compression_type(src_w);
1027 if (errno == ENOENT) {
1028 ret = wimlib_create_new_wim(compression_type, &dest_w);
1032 imagex_error_with_errno("Cannot stat file `%s'",
1039 image = wimlib_resolve_image(src_w, src_image_num_or_name);
1040 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
1045 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
1047 &num_additional_swms);
1052 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
1053 export_flags, additional_swms,
1054 num_additional_swms, imagex_progress_func);
1060 ret = wimlib_write(dest_w, dest_wimfile, WIMLIB_ALL_IMAGES,
1061 write_flags, num_threads,
1062 imagex_progress_func);
1064 ret = wimlib_overwrite(dest_w, write_flags, num_threads,
1065 imagex_progress_func);
1067 if (ret == WIMLIB_ERR_REOPEN)
1070 wimlib_free(dest_w);
1071 if (additional_swms) {
1072 for (unsigned i = 0; i < num_additional_swms; i++)
1073 wimlib_free(additional_swms[i]);
1074 free(additional_swms);
1079 /* Prints information about a WIM file; also can mark an image as bootable,
1080 * change the name of an image, or change the description of an image. */
1081 static int imagex_info(int argc, const char **argv)
1086 bool header = false;
1087 bool lookup_table = false;
1089 bool metadata = false;
1090 bool short_header = true;
1091 const char *xml_out_file = NULL;
1092 const char *wimfile;
1093 const char *image_num_or_name = "all";
1094 const char *new_name = NULL;
1095 const char *new_desc = NULL;
1100 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1105 for_opt(c, info_options) {
1115 short_header = false;
1118 lookup_table = true;
1119 short_header = false;
1123 short_header = false;
1126 xml_out_file = optarg;
1127 short_header = false;
1131 short_header = false;
1141 if (argc == 0 || argc > 4) {
1147 image_num_or_name = argv[1];
1157 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1159 ret = wimlib_open_wim(wimfile, open_flags, &w,
1160 imagex_progress_func);
1164 part_number = wimlib_get_part_number(w, &total_parts);
1166 image = wimlib_resolve_image(w, image_num_or_name);
1167 if (image == WIMLIB_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
1168 imagex_error("The image `%s' does not exist",
1171 imagex_error("If you would like to set the boot "
1172 "index to 0, specify image \"0\" with "
1173 "the --boot flag.");
1174 ret = WIMLIB_ERR_INVALID_IMAGE;
1178 num_images = wimlib_get_num_images(w);
1180 if (num_images == 0) {
1182 imagex_error("--boot is meaningless on a WIM with no "
1184 ret = WIMLIB_ERR_INVALID_IMAGE;
1189 if (image == WIMLIB_ALL_IMAGES && num_images > 1) {
1191 imagex_error("Cannot specify the --boot flag "
1192 "without specifying a specific "
1193 "image in a multi-image WIM");
1194 ret = WIMLIB_ERR_INVALID_IMAGE;
1198 imagex_error("Cannot specify the NEW_NAME "
1199 "without specifying a specific "
1200 "image in a multi-image WIM");
1201 ret = WIMLIB_ERR_INVALID_IMAGE;
1206 /* Operations that print information are separated from operations that
1207 * recreate the WIM file. */
1208 if (!new_name && !boot) {
1210 /* Read-only operations */
1212 if (image == WIMLIB_NO_IMAGE) {
1213 imagex_error("`%s' is not a valid image",
1215 ret = WIMLIB_ERR_INVALID_IMAGE;
1219 if (image == WIMLIB_ALL_IMAGES && short_header)
1220 wimlib_print_wim_information(w);
1223 wimlib_print_header(w);
1226 if (total_parts != 1) {
1227 printf("Warning: Only showing the lookup table "
1228 "for part %d of a %d-part WIM.\n",
1229 part_number, total_parts);
1231 wimlib_print_lookup_table(w);
1235 ret = wimlib_extract_xml_data(w, stdout);
1241 fp = fopen(xml_out_file, "wb");
1243 imagex_error_with_errno("Failed to open the "
1250 ret = wimlib_extract_xml_data(w, fp);
1251 if (fclose(fp) != 0) {
1252 imagex_error("Failed to close the file `%s'",
1262 wimlib_print_available_images(w, image);
1265 ret = wimlib_print_metadata(w, image);
1271 /* Modification operations */
1272 if (total_parts != 1) {
1273 imagex_error("Modifying a split WIM is not supported.");
1277 if (image == WIMLIB_ALL_IMAGES)
1280 if (image == WIMLIB_NO_IMAGE && new_name) {
1281 imagex_error("Cannot specify new_name (`%s') when "
1282 "using image 0", new_name);
1288 if (image == wimlib_get_boot_idx(w)) {
1289 printf("Image %d is already marked as "
1290 "bootable.\n", image);
1293 printf("Marking image %d as bootable.\n",
1295 wimlib_set_boot_idx(w, image);
1299 if (strcmp(wimlib_get_image_name(w, image),
1301 printf("Image %d is already named \"%s\".\n",
1305 printf("Changing the name of image %d to "
1306 "\"%s\".\n", image, new_name);
1307 ret = wimlib_set_image_name(w, image, new_name);
1313 const char *old_desc;
1314 old_desc = wimlib_get_image_description(w, image);
1315 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1316 printf("The description of image %d is already "
1317 "\"%s\".\n", image, new_desc);
1320 printf("Changing the description of image %d "
1321 "to \"%s\".\n", image, new_desc);
1322 ret = wimlib_set_image_descripton(w, image,
1329 /* Only call wimlib_overwrite() if something actually needs to
1331 if (boot || new_name || new_desc ||
1332 (check && !wimlib_has_integrity_table(w)))
1336 ret = file_writable(wimfile);
1341 write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1345 ret = wimlib_overwrite(w, write_flags, 1,
1346 imagex_progress_func);
1347 if (ret == WIMLIB_ERR_REOPEN)
1350 printf("The file `%s' was not modified because nothing "
1351 "needed to be done.\n", wimfile);
1360 /* Join split WIMs into one part WIM */
1361 static int imagex_join(int argc, const char **argv)
1364 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1365 int wim_write_flags = 0;
1366 const char *output_path;
1368 for_opt(c, join_options) {
1371 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1372 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1382 imagex_error("Must specify one or more split WIM (.swm) parts "
1386 output_path = argv[0];
1387 return wimlib_join(++argv, --argc, output_path, swm_open_flags,
1388 wim_write_flags, imagex_progress_func);
1394 /* Mounts an image using a FUSE mount. */
1395 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1398 int mount_flags = 0;
1399 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1400 const char *wimfile;
1406 const char *swm_glob = NULL;
1407 WIMStruct **additional_swms = NULL;
1408 unsigned num_additional_swms = 0;
1409 const char *staging_dir = NULL;
1411 if (strcmp(argv[0], "mountrw") == 0)
1412 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1414 for_opt(c, mount_options) {
1417 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1420 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1423 if (strcasecmp(optarg, "none") == 0)
1424 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1425 else if (strcasecmp(optarg, "xattr") == 0)
1426 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1427 else if (strcasecmp(optarg, "windows") == 0)
1428 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1430 imagex_error("Unknown stream interface \"%s\"", optarg);
1438 staging_dir = optarg;
1446 if (argc != 2 && argc != 3)
1451 ret = wimlib_open_wim(wimfile, open_flags, &w,
1452 imagex_progress_func);
1457 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1459 &num_additional_swms);
1466 num_images = wimlib_get_num_images(w);
1467 if (num_images != 1) {
1468 imagex_error("The file `%s' contains %d images; Please "
1469 "select one.", wimfile, num_images);
1470 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1477 image = wimlib_resolve_image(w, argv[1]);
1479 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
1484 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1485 ret = file_writable(wimfile);
1490 ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
1491 num_additional_swms, staging_dir);
1493 imagex_error("Failed to mount image %d from `%s' on `%s'",
1494 image, wimfile, dir);
1499 if (additional_swms) {
1500 for (unsigned i = 0; i < num_additional_swms; i++)
1501 wimlib_free(additional_swms[i]);
1502 free(additional_swms);
1506 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1511 static int imagex_optimize(int argc, const char **argv)
1515 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
1518 const char *wimfile;
1522 for_opt(c, optimize_options) {
1525 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1526 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1529 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1546 ret = wimlib_open_wim(wimfile, open_flags, &w,
1547 imagex_progress_func);
1551 old_size = file_get_size(argv[0]);
1552 printf("`%s' original size: ", wimfile);
1556 printf("%"PRIu64" KiB\n", old_size >> 10);
1558 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1561 new_size = file_get_size(argv[0]);
1562 printf("`%s' optimized size: ", wimfile);
1566 printf("%"PRIu64" KiB\n", new_size >> 10);
1568 fputs("Space saved: ", stdout);
1569 if (new_size != -1 && old_size != -1) {
1570 printf("%lld KiB\n",
1571 ((long long)old_size - (long long)new_size) >> 10);
1581 /* Split a WIM into a spanned set */
1582 static int imagex_split(int argc, const char **argv)
1585 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1586 int write_flags = 0;
1587 unsigned long part_size;
1592 for_opt(c, split_options) {
1595 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1596 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1610 part_size = strtod(argv[2], &tmp) * (1 << 20);
1611 if (tmp == argv[2] || *tmp) {
1612 imagex_error("Invalid part size \"%s\"", argv[2]);
1613 imagex_error("The part size must be an integer or floating-point number of megabytes.");
1616 ret = wimlib_open_wim(argv[0], open_flags, &w, imagex_progress_func);
1619 ret = wimlib_split(w, argv[1], part_size, write_flags, imagex_progress_func);
1624 /* Unmounts an image. */
1625 static int imagex_unmount(int argc, const char **argv)
1628 int unmount_flags = 0;
1631 for_opt(c, unmount_options) {
1634 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1637 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1651 ret = wimlib_unmount_image(argv[0], unmount_flags,
1652 imagex_progress_func);
1654 imagex_error("Failed to unmount `%s'", argv[0]);
1658 struct imagex_command {
1660 int (*func)(int , const char **);
1665 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1666 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1668 static const struct imagex_command imagex_commands[] = {
1669 {"append", imagex_capture_or_append, APPEND},
1670 {"apply", imagex_apply, APPLY},
1671 {"capture", imagex_capture_or_append, CAPTURE},
1672 {"delete", imagex_delete, DELETE},
1673 {"dir", imagex_dir, DIR},
1674 {"export", imagex_export, EXPORT},
1675 {"info", imagex_info, INFO},
1676 {"join", imagex_join, JOIN},
1677 {"mount", imagex_mount_rw_or_ro, MOUNT},
1678 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1679 {"optimize",imagex_optimize, OPTIMIZE},
1680 {"split", imagex_split, SPLIT},
1681 {"unmount", imagex_unmount, UNMOUNT},
1684 static void version()
1686 static const char *s =
1687 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
1688 "Copyright (C) 2012 Eric Biggers\n"
1689 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
1690 "This is free software: you are free to change and redistribute it.\n"
1691 "There is NO WARRANTY, to the extent permitted by law.\n"
1693 "Report bugs to "PACKAGE_BUGREPORT".\n";
1698 static void help_or_version(int argc, const char **argv)
1702 const struct imagex_command *cmd;
1704 for (i = 1; i < argc; i++) {
1712 if (strcmp(p, "help") == 0) {
1713 for_imagex_command(cmd) {
1714 if (strcmp(cmd->name, argv[1]) == 0) {
1722 if (strcmp(p, "version") == 0) {
1730 static void usage(int cmd_type)
1732 const struct imagex_command *cmd;
1733 printf("Usage: %s", usage_strings[cmd_type]);
1734 for_imagex_command(cmd) {
1735 if (cmd->cmd == cmd_type)
1736 printf("\nTry `man imagex-%s' for more details.\n",
1741 static void usage_all()
1743 puts("IMAGEX: Usage:");
1744 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
1745 printf(" %s", usage_strings[i]);
1746 static const char *extra =
1748 " imagex --version\n"
1750 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
1752 " Try `man imagex' for more information.\n"
1754 fputs(extra, stdout);
1758 int main(int argc, const char **argv)
1760 const struct imagex_command *cmd;
1764 imagex_error("No command specified");
1769 help_or_version(argc, argv);
1773 wimlib_set_print_errors(true);
1775 for_imagex_command(cmd) {
1776 if (strcmp(cmd->name, *argv) == 0) {
1777 ret = cmd->func(argc, argv);
1779 imagex_error("Exiting with error code %d:\n"
1781 wimlib_get_error_string(ret));
1782 if (ret == WIMLIB_ERR_NTFS_3G)
1783 imagex_error_with_errno("errno");
1789 imagex_error("Unrecognized command: `%s'", argv[0]);