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] [--unix-data]\n",
72 "imagex apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
73 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
74 " [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\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] [--unix-data]\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\"] [--unix-data] [--allow-other]\n",
100 "imagex mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
101 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
102 " [--staging-dir=DIR] [--unix-data] [--allow-other]\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] [--rebuild]\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'},
123 {"unix-data", no_argument, NULL, 'U'},
126 static const struct option capture_or_append_options[] = {
127 {"boot", no_argument, NULL, 'b'},
128 {"check", no_argument, NULL, 'c'},
129 {"compress", required_argument, NULL, 'x'},
130 {"config", required_argument, NULL, 'C'},
131 {"dereference", no_argument, NULL, 'L'},
132 {"flags", required_argument, NULL, 'f'},
133 {"verbose", no_argument, NULL, 'v'},
134 {"threads", required_argument, NULL, 't'},
135 {"rebuild", no_argument, NULL, 'R'},
136 {"unix-data", no_argument, NULL, 'U'},
139 static const struct option delete_options[] = {
140 {"check", no_argument, NULL, 'c'},
141 {"soft", no_argument, NULL, 's'},
145 static const struct option export_options[] = {
146 {"boot", no_argument, NULL, 'b'},
147 {"check", no_argument, NULL, 'c'},
148 {"compress", required_argument, NULL, 'x'},
149 {"ref", required_argument, NULL, 'r'},
150 {"threads", required_argument, NULL, 't'},
151 {"rebuild", no_argument, NULL, 'R'},
155 static const struct option info_options[] = {
156 {"boot", no_argument, NULL, 'b'},
157 {"check", no_argument, NULL, 'c'},
158 {"extract-xml", required_argument, NULL, 'X'},
159 {"header", no_argument, NULL, 'h'},
160 {"lookup-table", no_argument, NULL, 'l'},
161 {"metadata", no_argument, NULL, 'm'},
162 {"xml", no_argument, NULL, 'x'},
166 static const struct option join_options[] = {
167 {"check", no_argument, NULL, 'c'},
171 static const struct option mount_options[] = {
172 {"check", no_argument, NULL, 'c'},
173 {"debug", no_argument, NULL, 'd'},
174 {"streams-interface", required_argument, NULL, 's'},
175 {"ref", required_argument, NULL, 'r'},
176 {"staging-dir", required_argument, NULL, 'D'},
177 {"unix-data", no_argument, NULL, 'U'},
178 {"allow-other", no_argument, NULL, 'A'},
182 static const struct option optimize_options[] = {
183 {"check", no_argument, NULL, 'c'},
184 {"recompress", no_argument, NULL, 'r'},
188 static const struct option split_options[] = {
189 {"check", no_argument, NULL, 'c'},
193 static const struct option unmount_options[] = {
194 {"commit", no_argument, NULL, 'c'},
195 {"check", no_argument, NULL, 'C'},
196 {"rebuild", no_argument, NULL, 'R'},
202 /* Print formatted error message to stderr. */
203 static void imagex_error(const char *format, ...)
206 va_start(va, format);
207 fputs("ERROR: ", stderr);
208 vfprintf(stderr, format, va);
213 /* Print formatted error message to stderr. */
214 static void imagex_error_with_errno(const char *format, ...)
216 int errno_save = errno;
218 va_start(va, format);
219 fputs("ERROR: ", stderr);
220 vfprintf(stderr, format, va);
221 fprintf(stderr, ": %s\n", strerror(errno_save));
225 static int verify_image_exists(int image, const char *image_name,
226 const char *wim_name)
228 if (image == WIMLIB_NO_IMAGE) {
229 imagex_error("\"%s\" is not a valid image in `%s'!\n"
230 " Please specify a 1-based imagex index or "
232 " You may use `imagex info' to list the images "
233 "contained in a WIM.",
234 image_name, wim_name);
240 static int verify_image_is_single(int image)
242 if (image == WIMLIB_ALL_IMAGES) {
243 imagex_error("Cannot specify all images for this action!");
249 static int verify_image_exists_and_is_single(int image, const char *image_name,
250 const char *wim_name)
253 ret = verify_image_exists(image, image_name, wim_name);
255 ret = verify_image_is_single(image);
259 static int get_compression_type(const char *optarg)
261 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
262 return WIMLIB_COMPRESSION_TYPE_LZX;
263 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
264 return WIMLIB_COMPRESSION_TYPE_XPRESS;
265 else if (strcasecmp(optarg, "none") == 0)
266 return WIMLIB_COMPRESSION_TYPE_NONE;
268 imagex_error("Invalid compression type `%s'! Must be "
269 "\"maximum\", \"fast\", or \"none\".", optarg);
270 return WIMLIB_COMPRESSION_TYPE_INVALID;
274 static off_t file_get_size(const char *filename)
277 if (stat(filename, &st) == 0)
283 static const char *default_capture_config =
288 "\\System Volume Information\n"
292 "[CompressionExclusionList]\n"
296 "\\WINDOWS\\inf\\*.pnf\n";
298 static char *file_get_contents(const char *filename, size_t *len_ret)
305 if (stat(filename, &stbuf) != 0) {
306 imagex_error_with_errno("Failed to stat the file `%s'", filename);
311 fp = fopen(filename, "rb");
313 imagex_error_with_errno("Failed to open the file `%s'", filename);
319 imagex_error("Failed to allocate buffer of %zu bytes to hold "
320 "contents of file `%s'", len, filename);
323 if (fread(buf, 1, len, fp) != len) {
324 imagex_error_with_errno("Failed to read %lu bytes from the "
325 "file `%s'", len, filename);
337 static int file_writable(const char *path)
340 ret = access(path, W_OK);
342 imagex_error_with_errno("Can't modify `%s'", path);
346 #define TO_PERCENT(numerator, denominator) \
347 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
349 static const char *get_data_type(int ctype)
352 case WIMLIB_COMPRESSION_TYPE_NONE:
353 return "uncompressed";
354 case WIMLIB_COMPRESSION_TYPE_LZX:
355 return "LZX-compressed";
356 case WIMLIB_COMPRESSION_TYPE_XPRESS:
357 return "XPRESS-compressed";
362 static int imagex_progress_func(enum wimlib_progress_msg msg,
363 const union wimlib_progress_info *info)
365 unsigned percent_done;
367 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
368 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
369 info->write_streams.total_bytes);
370 if (info->write_streams.completed_streams == 0) {
371 const char *data_type;
373 data_type = get_data_type(info->write_streams.compression_type);
374 printf("Writing %s data using %u thread%s\n",
375 data_type, info->write_streams.num_threads,
376 (info->write_streams.num_threads == 1) ? "" : "s");
378 printf("\r%"PRIu64" MiB of %"PRIu64" MiB (uncompressed) "
379 "written (%u%% done)",
380 info->write_streams.completed_bytes >> 20,
381 info->write_streams.total_bytes >> 20,
383 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
386 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
387 printf("Scanning `%s'...\n", info->scan.source);
389 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
390 if (info->scan.excluded)
391 printf("Excluding `%s' from capture\n", info->scan.cur_path);
393 printf("Scanning `%s'\n", info->scan.cur_path);
395 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
397 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
398 percent_done = TO_PERCENT(info->integrity.completed_bytes,
399 info->integrity.total_bytes);
400 printf("\rVerifying integrity of `%s': %"PRIu64" MiB "
401 "of %"PRIu64" MiB (%u%%) done",
402 info->integrity.filename,
403 info->integrity.completed_bytes >> 20,
404 info->integrity.total_bytes >> 20,
406 if (info->integrity.completed_bytes == info->integrity.total_bytes)
409 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
410 percent_done = TO_PERCENT(info->integrity.completed_bytes,
411 info->integrity.total_bytes);
412 printf("\rCalculating integrity table for WIM: %"PRIu64" MiB "
413 "of %"PRIu64" MiB (%u%%) done",
414 info->integrity.completed_bytes >> 20,
415 info->integrity.total_bytes >> 20,
417 if (info->integrity.completed_bytes == info->integrity.total_bytes)
420 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
421 printf("Applying image %d (%s) from `%s' to %s `%s'\n",
423 info->extract.image_name,
424 info->extract.wimfile_name,
425 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
426 "NTFS volume" : "directory"),
427 info->extract.target);
429 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
430 /*printf("Applying directory structure to %s\n",*/
431 /*info->extract.target);*/
433 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
434 percent_done = TO_PERCENT(info->extract.completed_bytes,
435 info->extract.total_bytes);
436 printf("\rExtracting files: "
437 "%"PRIu64" MiB of %"PRIu64" MiB (%u%%) done",
438 info->extract.completed_bytes >> 20,
439 info->extract.total_bytes >> 20,
441 if (info->extract.completed_bytes >= info->extract.total_bytes)
444 case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
445 puts(info->extract.cur_path);
447 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
448 printf("Setting timestamps on all extracted files...\n");
450 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
451 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
452 printf("Unmounting NTFS volume `%s'...\n",
453 info->extract.target);
456 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
457 percent_done = TO_PERCENT(info->join.completed_bytes,
458 info->join.total_bytes);
459 printf("Writing resources from part %u of %u: "
460 "%"PRIu64 " MiB of %"PRIu64" MiB (%u%%) written\n",
461 (info->join.completed_parts == info->join.total_parts) ?
462 info->join.completed_parts : info->join.completed_parts + 1,
463 info->join.total_parts,
464 info->join.completed_bytes >> 20,
465 info->join.total_bytes >> 20,
468 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
469 percent_done = TO_PERCENT(info->split.completed_bytes,
470 info->split.total_bytes);
471 printf("Writing `%s': %"PRIu64" MiB of %"PRIu64" MiB (%u%%) written\n",
472 info->split.part_name,
473 info->split.completed_bytes >> 20,
474 info->split.total_bytes >> 20,
477 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
478 if (info->split.completed_bytes == info->split.total_bytes) {
479 printf("Finished writing %u split WIM parts\n",
480 info->split.cur_part_number);
491 static int open_swms_from_glob(const char *swm_glob,
492 const char *first_part,
494 WIMStruct ***additional_swms_ret,
495 unsigned *num_additional_swms_ret)
497 unsigned num_additional_swms = 0;
498 WIMStruct **additional_swms = NULL;
502 ret = glob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
504 if (ret == GLOB_NOMATCH) {
505 imagex_error("Found no files for glob \"%s\"",
508 imagex_error_with_errno("Failed to process glob "
514 num_additional_swms = globbuf.gl_pathc;
515 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
516 if (!additional_swms) {
517 imagex_error("Out of memory");
522 for (unsigned i = 0; i < num_additional_swms; i++) {
523 if (strcmp(globbuf.gl_pathv[i], first_part) == 0) {
527 ret = wimlib_open_wim(globbuf.gl_pathv[i],
528 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
529 &additional_swms[i - offset],
530 imagex_progress_func);
534 *additional_swms_ret = additional_swms;
535 *num_additional_swms_ret = num_additional_swms - offset;
539 for (unsigned i = 0; i < num_additional_swms; i++)
540 wimlib_free(additional_swms[i]);
541 free(additional_swms);
549 static unsigned parse_num_threads(const char *optarg)
552 unsigned nthreads = strtoul(optarg, &tmp, 10);
553 if (nthreads == UINT_MAX || *tmp || tmp == optarg) {
554 imagex_error("Number of threads must be a non-negative integer!");
562 /* Extract one image, or all images, from a WIM file into a directory. */
563 static int imagex_apply(int argc, const char **argv)
566 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
573 const char *image_num_or_name;
574 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
576 const char *swm_glob = NULL;
577 WIMStruct **additional_swms = NULL;
578 unsigned num_additional_swms = 0;
580 for_opt(c, apply_options) {
583 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
586 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
589 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
592 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
598 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
607 if (argc != 2 && argc != 3) {
614 image_num_or_name = "1";
617 image_num_or_name = argv[1];
621 ret = wimlib_open_wim(wimfile, open_flags, &w, imagex_progress_func);
625 image = wimlib_resolve_image(w, image_num_or_name);
626 ret = verify_image_exists(image, image_num_or_name, wimfile);
630 num_images = wimlib_get_num_images(w);
631 if (argc == 2 && num_images != 1) {
632 imagex_error("`%s' contains %d images; Please select one "
633 "(or all)", wimfile, num_images);
640 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
642 &num_additional_swms);
649 ret = stat(target, &stbuf);
651 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
652 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
654 if (errno != ENOENT) {
655 imagex_error_with_errno("Failed to stat `%s'", target);
661 ret = wimlib_extract_image(w, image, target, extract_flags,
662 additional_swms, num_additional_swms,
663 imagex_progress_func);
665 printf("Done applying WIM image.\n");
668 if (additional_swms) {
669 for (unsigned i = 0; i < num_additional_swms; i++)
670 wimlib_free(additional_swms[i]);
671 free(additional_swms);
676 static int imagex_capture_or_append(int argc, const char **argv)
680 int add_image_flags = 0;
682 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
687 const char *flags_element = NULL;
688 const char *config_file = NULL;
689 char *config_str = NULL;
690 size_t config_len = 0;
695 int cmd = strcmp(argv[0], "append") ? CAPTURE : APPEND;
696 unsigned num_threads = 0;
698 for_opt(c, capture_or_append_options) {
701 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
704 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
705 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
708 config_file = optarg;
711 compression_type = get_compression_type(optarg);
712 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
716 flags_element = optarg;
719 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
722 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
725 num_threads = parse_num_threads(optarg);
726 if (num_threads == UINT_MAX)
730 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
733 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
742 if (argc < 2 || argc > 4) {
749 char source_copy[strlen(source) + 1];
750 memcpy(source_copy, source, strlen(source) + 1);
751 default_name = basename(source_copy);
753 name = (argc >= 3) ? argv[2] : default_name;
754 desc = (argc >= 4) ? argv[3] : NULL;
757 config_str = file_get_contents(config_file, &config_len);
763 ret = wimlib_open_wim(wimfile, open_flags, &w,
764 imagex_progress_func);
766 ret = wimlib_create_new_wim(compression_type, &w);
772 ret = stat(source, &stbuf);
774 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
775 printf("Capturing WIM image from NTFS filesystem on `%s'\n",
777 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
780 if (errno != ENOENT) {
781 imagex_error_with_errno("Failed to stat `%s'", source);
787 ret = wimlib_add_image(w, source, name,
788 config_str ? config_str : default_capture_config,
789 config_len, add_image_flags,
790 imagex_progress_func);
794 cur_image = wimlib_get_num_images(w);
796 ret = wimlib_set_image_descripton(w, cur_image, desc);
801 ret = wimlib_set_image_flags(w, cur_image, flags_element);
806 ret = wimlib_overwrite(w, write_flags, num_threads,
807 imagex_progress_func);
809 ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
810 num_threads, imagex_progress_func);
812 if (ret == WIMLIB_ERR_REOPEN)
815 imagex_error("Failed to write the WIM file `%s'", wimfile);
822 /* Remove image(s) from a WIM. */
823 static int imagex_delete(int argc, const char **argv)
829 const char *image_num_or_name;
834 for_opt(c, delete_options) {
837 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
838 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
841 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
853 imagex_error("Must specify a WIM file");
855 imagex_error("Must specify an image");
860 image_num_or_name = argv[1];
862 ret = file_writable(wimfile);
866 ret = wimlib_open_wim(wimfile, open_flags, &w,
867 imagex_progress_func);
871 image = wimlib_resolve_image(w, image_num_or_name);
873 ret = verify_image_exists(image, image_num_or_name, wimfile);
877 ret = wimlib_delete_image(w, image);
879 imagex_error("Failed to delete image from `%s'", wimfile);
883 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
884 if (ret == WIMLIB_ERR_REOPEN)
887 imagex_error("Failed to write the file `%s' with image "
895 /* Print the files contained in an image(s) in a WIM file. */
896 static int imagex_dir(int argc, const char **argv)
905 imagex_error("Must specify a WIM file");
910 imagex_error("Too many arguments");
916 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w,
917 imagex_progress_func);
922 image = wimlib_resolve_image(w, argv[2]);
923 ret = verify_image_exists(image, argv[2], wimfile);
927 /* Image was not specified. If the WIM only contains one image,
928 * choose that one; otherwise, print an error. */
929 num_images = wimlib_get_num_images(w);
930 if (num_images != 1) {
931 imagex_error("The file `%s' contains %d images; Please "
932 "select one.", wimfile, num_images);
940 ret = wimlib_print_files(w, image);
946 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
948 static int imagex_export(int argc, const char **argv)
952 int export_flags = 0;
954 int compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
955 bool compression_type_specified = false;
956 const char *src_wimfile;
957 const char *src_image_num_or_name;
958 const char *dest_wimfile;
959 const char *dest_name;
960 const char *dest_desc;
961 WIMStruct *src_w = NULL;
962 WIMStruct *dest_w = NULL;
967 const char *swm_glob = NULL;
968 WIMStruct **additional_swms = NULL;
969 unsigned num_additional_swms = 0;
970 unsigned num_threads = 0;
972 for_opt(c, export_options) {
975 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
978 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
979 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
982 compression_type = get_compression_type(optarg);
983 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
985 compression_type_specified = true;
991 num_threads = parse_num_threads(optarg);
992 if (num_threads == UINT_MAX)
996 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1005 if (argc < 3 || argc > 5) {
1009 src_wimfile = argv[0];
1010 src_image_num_or_name = argv[1];
1011 dest_wimfile = argv[2];
1012 dest_name = (argc >= 4) ? argv[3] : NULL;
1013 dest_desc = (argc >= 5) ? argv[4] : NULL;
1014 ret = wimlib_open_wim(src_wimfile,
1015 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w,
1016 imagex_progress_func);
1020 /* Determine if the destination is an existing file or not.
1021 * If so, we try to append the exported image(s) to it; otherwise, we
1022 * create a new WIM containing the exported image(s). */
1023 if (stat(dest_wimfile, &stbuf) == 0) {
1027 /* Destination file exists. */
1028 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
1029 imagex_error("`%s' is not a regular file",
1034 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w,
1035 imagex_progress_func);
1039 ret = file_writable(dest_wimfile);
1043 dest_ctype = wimlib_get_compression_type(dest_w);
1044 if (compression_type_specified
1045 && compression_type != dest_ctype)
1047 imagex_error("Cannot specify a compression type that is "
1048 "not the same as that used in the "
1055 /* dest_wimfile is not an existing file, so create a new WIM. */
1056 if (!compression_type_specified)
1057 compression_type = wimlib_get_compression_type(src_w);
1058 if (errno == ENOENT) {
1059 ret = wimlib_create_new_wim(compression_type, &dest_w);
1063 imagex_error_with_errno("Cannot stat file `%s'",
1070 image = wimlib_resolve_image(src_w, src_image_num_or_name);
1071 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
1076 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
1078 &num_additional_swms);
1083 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
1084 export_flags, additional_swms,
1085 num_additional_swms, imagex_progress_func);
1091 ret = wimlib_write(dest_w, dest_wimfile, WIMLIB_ALL_IMAGES,
1092 write_flags, num_threads,
1093 imagex_progress_func);
1095 ret = wimlib_overwrite(dest_w, write_flags, num_threads,
1096 imagex_progress_func);
1098 if (ret == WIMLIB_ERR_REOPEN)
1101 wimlib_free(dest_w);
1102 if (additional_swms) {
1103 for (unsigned i = 0; i < num_additional_swms; i++)
1104 wimlib_free(additional_swms[i]);
1105 free(additional_swms);
1110 /* Prints information about a WIM file; also can mark an image as bootable,
1111 * change the name of an image, or change the description of an image. */
1112 static int imagex_info(int argc, const char **argv)
1117 bool header = false;
1118 bool lookup_table = false;
1120 bool metadata = false;
1121 bool short_header = true;
1122 const char *xml_out_file = NULL;
1123 const char *wimfile;
1124 const char *image_num_or_name = "all";
1125 const char *new_name = NULL;
1126 const char *new_desc = NULL;
1131 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1136 for_opt(c, info_options) {
1146 short_header = false;
1149 lookup_table = true;
1150 short_header = false;
1154 short_header = false;
1157 xml_out_file = optarg;
1158 short_header = false;
1162 short_header = false;
1172 if (argc == 0 || argc > 4) {
1178 image_num_or_name = argv[1];
1188 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1190 ret = wimlib_open_wim(wimfile, open_flags, &w,
1191 imagex_progress_func);
1195 part_number = wimlib_get_part_number(w, &total_parts);
1197 image = wimlib_resolve_image(w, image_num_or_name);
1198 if (image == WIMLIB_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
1199 imagex_error("The image `%s' does not exist",
1202 imagex_error("If you would like to set the boot "
1203 "index to 0, specify image \"0\" with "
1204 "the --boot flag.");
1205 ret = WIMLIB_ERR_INVALID_IMAGE;
1209 num_images = wimlib_get_num_images(w);
1211 if (num_images == 0) {
1213 imagex_error("--boot is meaningless on a WIM with no "
1215 ret = WIMLIB_ERR_INVALID_IMAGE;
1220 if (image == WIMLIB_ALL_IMAGES && num_images > 1) {
1222 imagex_error("Cannot specify the --boot flag "
1223 "without specifying a specific "
1224 "image in a multi-image WIM");
1225 ret = WIMLIB_ERR_INVALID_IMAGE;
1229 imagex_error("Cannot specify the NEW_NAME "
1230 "without specifying a specific "
1231 "image in a multi-image WIM");
1232 ret = WIMLIB_ERR_INVALID_IMAGE;
1237 /* Operations that print information are separated from operations that
1238 * recreate the WIM file. */
1239 if (!new_name && !boot) {
1241 /* Read-only operations */
1243 if (image == WIMLIB_NO_IMAGE) {
1244 imagex_error("`%s' is not a valid image",
1246 ret = WIMLIB_ERR_INVALID_IMAGE;
1250 if (image == WIMLIB_ALL_IMAGES && short_header)
1251 wimlib_print_wim_information(w);
1254 wimlib_print_header(w);
1257 if (total_parts != 1) {
1258 printf("Warning: Only showing the lookup table "
1259 "for part %d of a %d-part WIM.\n",
1260 part_number, total_parts);
1262 wimlib_print_lookup_table(w);
1266 ret = wimlib_extract_xml_data(w, stdout);
1272 fp = fopen(xml_out_file, "wb");
1274 imagex_error_with_errno("Failed to open the "
1281 ret = wimlib_extract_xml_data(w, fp);
1282 if (fclose(fp) != 0) {
1283 imagex_error("Failed to close the file `%s'",
1293 wimlib_print_available_images(w, image);
1296 ret = wimlib_print_metadata(w, image);
1302 /* Modification operations */
1303 if (total_parts != 1) {
1304 imagex_error("Modifying a split WIM is not supported.");
1308 if (image == WIMLIB_ALL_IMAGES)
1311 if (image == WIMLIB_NO_IMAGE && new_name) {
1312 imagex_error("Cannot specify new_name (`%s') when "
1313 "using image 0", new_name);
1319 if (image == wimlib_get_boot_idx(w)) {
1320 printf("Image %d is already marked as "
1321 "bootable.\n", image);
1324 printf("Marking image %d as bootable.\n",
1326 wimlib_set_boot_idx(w, image);
1330 if (strcmp(wimlib_get_image_name(w, image),
1332 printf("Image %d is already named \"%s\".\n",
1336 printf("Changing the name of image %d to "
1337 "\"%s\".\n", image, new_name);
1338 ret = wimlib_set_image_name(w, image, new_name);
1344 const char *old_desc;
1345 old_desc = wimlib_get_image_description(w, image);
1346 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1347 printf("The description of image %d is already "
1348 "\"%s\".\n", image, new_desc);
1351 printf("Changing the description of image %d "
1352 "to \"%s\".\n", image, new_desc);
1353 ret = wimlib_set_image_descripton(w, image,
1360 /* Only call wimlib_overwrite() if something actually needs to
1362 if (boot || new_name || new_desc ||
1363 (check && !wimlib_has_integrity_table(w)))
1367 ret = file_writable(wimfile);
1372 write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1376 ret = wimlib_overwrite(w, write_flags, 1,
1377 imagex_progress_func);
1378 if (ret == WIMLIB_ERR_REOPEN)
1381 printf("The file `%s' was not modified because nothing "
1382 "needed to be done.\n", wimfile);
1391 /* Join split WIMs into one part WIM */
1392 static int imagex_join(int argc, const char **argv)
1395 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1396 int wim_write_flags = 0;
1397 const char *output_path;
1399 for_opt(c, join_options) {
1402 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1403 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1413 imagex_error("Must specify one or more split WIM (.swm) parts "
1417 output_path = argv[0];
1418 return wimlib_join(++argv, --argc, output_path, swm_open_flags,
1419 wim_write_flags, imagex_progress_func);
1425 /* Mounts an image using a FUSE mount. */
1426 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1429 int mount_flags = 0;
1430 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1431 const char *wimfile;
1437 const char *swm_glob = NULL;
1438 WIMStruct **additional_swms = NULL;
1439 unsigned num_additional_swms = 0;
1440 const char *staging_dir = NULL;
1442 if (strcmp(argv[0], "mountrw") == 0)
1443 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1445 for_opt(c, mount_options) {
1448 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
1451 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1454 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1457 if (strcasecmp(optarg, "none") == 0)
1458 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1459 else if (strcasecmp(optarg, "xattr") == 0)
1460 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1461 else if (strcasecmp(optarg, "windows") == 0)
1462 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1464 imagex_error("Unknown stream interface \"%s\"", optarg);
1472 staging_dir = optarg;
1475 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
1483 if (argc != 2 && argc != 3)
1488 ret = wimlib_open_wim(wimfile, open_flags, &w,
1489 imagex_progress_func);
1494 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1496 &num_additional_swms);
1503 num_images = wimlib_get_num_images(w);
1504 if (num_images != 1) {
1505 imagex_error("The file `%s' contains %d images; Please "
1506 "select one.", wimfile, num_images);
1507 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1514 image = wimlib_resolve_image(w, argv[1]);
1516 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
1521 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1522 ret = file_writable(wimfile);
1527 ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
1528 num_additional_swms, staging_dir);
1530 imagex_error("Failed to mount image %d from `%s' on `%s'",
1531 image, wimfile, dir);
1536 if (additional_swms) {
1537 for (unsigned i = 0; i < num_additional_swms; i++)
1538 wimlib_free(additional_swms[i]);
1539 free(additional_swms);
1543 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1548 static int imagex_optimize(int argc, const char **argv)
1552 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
1555 const char *wimfile;
1559 for_opt(c, optimize_options) {
1562 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1563 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1566 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1583 ret = wimlib_open_wim(wimfile, open_flags, &w,
1584 imagex_progress_func);
1588 old_size = file_get_size(argv[0]);
1589 printf("`%s' original size: ", wimfile);
1593 printf("%"PRIu64" KiB\n", old_size >> 10);
1595 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1598 new_size = file_get_size(argv[0]);
1599 printf("`%s' optimized size: ", wimfile);
1603 printf("%"PRIu64" KiB\n", new_size >> 10);
1605 fputs("Space saved: ", stdout);
1606 if (new_size != -1 && old_size != -1) {
1607 printf("%lld KiB\n",
1608 ((long long)old_size - (long long)new_size) >> 10);
1618 /* Split a WIM into a spanned set */
1619 static int imagex_split(int argc, const char **argv)
1622 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1623 int write_flags = 0;
1624 unsigned long part_size;
1629 for_opt(c, split_options) {
1632 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1633 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1647 part_size = strtod(argv[2], &tmp) * (1 << 20);
1648 if (tmp == argv[2] || *tmp) {
1649 imagex_error("Invalid part size \"%s\"", argv[2]);
1650 imagex_error("The part size must be an integer or floating-point number of megabytes.");
1653 ret = wimlib_open_wim(argv[0], open_flags, &w, imagex_progress_func);
1656 ret = wimlib_split(w, argv[1], part_size, write_flags, imagex_progress_func);
1661 /* Unmounts an image. */
1662 static int imagex_unmount(int argc, const char **argv)
1665 int unmount_flags = 0;
1668 for_opt(c, unmount_options) {
1671 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1674 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1677 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
1691 ret = wimlib_unmount_image(argv[0], unmount_flags,
1692 imagex_progress_func);
1694 imagex_error("Failed to unmount `%s'", argv[0]);
1698 struct imagex_command {
1700 int (*func)(int , const char **);
1705 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1706 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1708 static const struct imagex_command imagex_commands[] = {
1709 {"append", imagex_capture_or_append, APPEND},
1710 {"apply", imagex_apply, APPLY},
1711 {"capture", imagex_capture_or_append, CAPTURE},
1712 {"delete", imagex_delete, DELETE},
1713 {"dir", imagex_dir, DIR},
1714 {"export", imagex_export, EXPORT},
1715 {"info", imagex_info, INFO},
1716 {"join", imagex_join, JOIN},
1717 {"mount", imagex_mount_rw_or_ro, MOUNT},
1718 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1719 {"optimize",imagex_optimize, OPTIMIZE},
1720 {"split", imagex_split, SPLIT},
1721 {"unmount", imagex_unmount, UNMOUNT},
1724 static void version()
1726 static const char *s =
1727 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
1728 "Copyright (C) 2012 Eric Biggers\n"
1729 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
1730 "This is free software: you are free to change and redistribute it.\n"
1731 "There is NO WARRANTY, to the extent permitted by law.\n"
1733 "Report bugs to "PACKAGE_BUGREPORT".\n";
1738 static void help_or_version(int argc, const char **argv)
1742 const struct imagex_command *cmd;
1744 for (i = 1; i < argc; i++) {
1752 if (strcmp(p, "help") == 0) {
1753 for_imagex_command(cmd) {
1754 if (strcmp(cmd->name, argv[1]) == 0) {
1762 if (strcmp(p, "version") == 0) {
1770 static void usage(int cmd_type)
1772 const struct imagex_command *cmd;
1773 printf("Usage: %s", usage_strings[cmd_type]);
1774 for_imagex_command(cmd) {
1775 if (cmd->cmd == cmd_type)
1776 printf("\nTry `man imagex-%s' for more details.\n",
1781 static void usage_all()
1783 puts("IMAGEX: Usage:");
1784 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
1785 printf(" %s", usage_strings[i]);
1786 static const char *extra =
1788 " imagex --version\n"
1790 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
1792 " Try `man imagex' for more information.\n"
1794 fputs(extra, stdout);
1798 int main(int argc, const char **argv)
1800 const struct imagex_command *cmd;
1804 imagex_error("No command specified");
1809 help_or_version(argc, argv);
1813 wimlib_set_print_errors(true);
1814 ret = wimlib_global_init();
1818 for_imagex_command(cmd) {
1819 if (strcmp(cmd->name, *argv) == 0) {
1820 ret = cmd->func(argc, argv);
1825 imagex_error("Unrecognized command: `%s'", argv[0]);
1830 imagex_error("Exiting with error code %d:\n"
1832 wimlib_get_error_string(ret));
1833 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
1834 imagex_error_with_errno("errno");
1836 wimlib_global_cleanup();