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] [--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'},
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'},
192 {"rebuild", no_argument, NULL, 'R'},
198 /* Print formatted error message to stderr. */
199 static void imagex_error(const char *format, ...)
202 va_start(va, format);
203 fputs("ERROR: ", stderr);
204 vfprintf(stderr, format, va);
209 /* Print formatted error message to stderr. */
210 static void imagex_error_with_errno(const char *format, ...)
212 int errno_save = errno;
214 va_start(va, format);
215 fputs("ERROR: ", stderr);
216 vfprintf(stderr, format, va);
217 fprintf(stderr, ": %s\n", strerror(errno_save));
221 static int verify_image_exists(int image, const char *image_name,
222 const char *wim_name)
224 if (image == WIMLIB_NO_IMAGE) {
225 imagex_error("\"%s\" is not a valid image in `%s'!\n"
226 " Please specify a 1-based imagex index or "
228 " You may use `imagex info' to list the images "
229 "contained in a WIM.",
230 image_name, wim_name);
236 static int verify_image_is_single(int image)
238 if (image == WIMLIB_ALL_IMAGES) {
239 imagex_error("Cannot specify all images for this action!");
245 static int verify_image_exists_and_is_single(int image, const char *image_name,
246 const char *wim_name)
249 ret = verify_image_exists(image, image_name, wim_name);
251 ret = verify_image_is_single(image);
255 static int get_compression_type(const char *optarg)
257 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
258 return WIMLIB_COMPRESSION_TYPE_LZX;
259 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
260 return WIMLIB_COMPRESSION_TYPE_XPRESS;
261 else if (strcasecmp(optarg, "none") == 0)
262 return WIMLIB_COMPRESSION_TYPE_NONE;
264 imagex_error("Invalid compression type `%s'! Must be "
265 "\"maximum\", \"fast\", or \"none\".", optarg);
266 return WIMLIB_COMPRESSION_TYPE_INVALID;
270 static off_t file_get_size(const char *filename)
273 if (stat(filename, &st) == 0)
279 static char *file_get_contents(const char *filename, size_t *len_ret)
286 if (stat(filename, &stbuf) != 0) {
287 imagex_error_with_errno("Failed to stat the file `%s'", filename);
292 fp = fopen(filename, "rb");
294 imagex_error_with_errno("Failed to open the file `%s'", filename);
300 imagex_error("Failed to allocate buffer of %zu bytes to hold "
301 "contents of file `%s'", len, filename);
304 if (fread(buf, 1, len, fp) != len) {
305 imagex_error_with_errno("Failed to read %lu bytes from the "
306 "file `%s'", len, filename);
318 static int file_writable(const char *path)
321 ret = access(path, F_OK | W_OK);
323 imagex_error_with_errno("Can't modify `%s'", path);
327 #define TO_PERCENT(numerator, denominator) \
328 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
330 static const char *get_data_type(int ctype)
333 case WIMLIB_COMPRESSION_TYPE_NONE:
334 return "uncompressed";
335 case WIMLIB_COMPRESSION_TYPE_LZX:
336 return "LZX-compressed";
337 case WIMLIB_COMPRESSION_TYPE_XPRESS:
338 return "XPRESS-compressed";
343 static int imagex_progress_func(enum wimlib_progress_msg msg,
344 const union wimlib_progress_info *info)
346 unsigned percent_done;
348 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
349 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
350 info->write_streams.total_bytes);
351 if (info->write_streams.completed_streams == 0) {
352 const char *data_type;
354 data_type = get_data_type(info->write_streams.compression_type);
355 printf("Writing %s data using %u thread%s\n",
356 data_type, info->write_streams.num_threads,
357 (info->write_streams.num_threads == 1) ? "" : "s");
359 printf("\r%"PRIu64" MiB of %"PRIu64" MiB (uncompressed) "
360 "written (%u%% done)",
361 info->write_streams.completed_bytes >> 20,
362 info->write_streams.total_bytes >> 20,
364 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
367 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
368 printf("Scanning `%s'...\n", info->scan.source);
370 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
371 if (info->scan.excluded)
372 printf("Excluding `%s' from capture\n", info->scan.cur_path);
374 printf("Scanning `%s'\n", info->scan.cur_path);
376 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
378 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
379 percent_done = TO_PERCENT(info->integrity.completed_bytes,
380 info->integrity.total_bytes);
381 printf("\rVerifying integrity of `%s': %"PRIu64" MiB "
382 "of %"PRIu64" MiB (%u%%) done",
383 info->integrity.filename,
384 info->integrity.completed_bytes >> 20,
385 info->integrity.total_bytes >> 20,
387 if (info->integrity.completed_bytes == info->integrity.total_bytes)
390 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
391 percent_done = TO_PERCENT(info->integrity.completed_bytes,
392 info->integrity.total_bytes);
393 printf("\rCalculating integrity table for WIM: %"PRIu64" MiB "
394 "of %"PRIu64" MiB (%u%%) done",
395 info->integrity.completed_bytes >> 20,
396 info->integrity.total_bytes >> 20,
398 if (info->integrity.completed_bytes == info->integrity.total_bytes)
401 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
402 printf("Applying image %d (%s) from `%s' to %s `%s'\n",
404 info->extract.image_name,
405 info->extract.wimfile_name,
406 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
407 "NTFS volume" : "directory"),
408 info->extract.target);
410 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
411 /*printf("Applying directory structure to %s\n",*/
412 /*info->extract.target);*/
414 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
415 percent_done = TO_PERCENT(info->extract.completed_bytes,
416 info->extract.total_bytes);
417 printf("\rExtracting files: "
418 "%"PRIu64" MiB of %"PRIu64" MiB (%u%%) done",
419 info->extract.completed_bytes >> 20,
420 info->extract.total_bytes >> 20,
422 if (info->extract.completed_bytes >= info->extract.total_bytes)
425 case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
426 puts(info->extract.cur_path);
428 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
429 printf("Setting timestamps on all extracted files...\n");
431 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
432 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
433 printf("Unmounting NTFS volume `%s'...\n",
434 info->extract.target);
437 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
438 percent_done = TO_PERCENT(info->join.completed_bytes,
439 info->join.total_bytes);
440 printf("Writing resources from part %u of %u: "
441 "%"PRIu64 " MiB of %"PRIu64" MiB (%u%%) written\n",
442 (info->join.completed_parts == info->join.total_parts) ?
443 info->join.completed_parts : info->join.completed_parts + 1,
444 info->join.total_parts,
445 info->join.completed_bytes >> 20,
446 info->join.total_bytes >> 20,
449 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
450 percent_done = TO_PERCENT(info->split.completed_bytes,
451 info->split.total_bytes);
452 printf("Writing `%s': %"PRIu64" MiB of %"PRIu64" MiB (%u%%) written\n",
453 info->split.part_name,
454 info->split.completed_bytes >> 20,
455 info->split.total_bytes >> 20,
458 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
459 if (info->split.completed_bytes == info->split.total_bytes) {
460 printf("Finished writing %u split WIM parts\n",
461 info->split.cur_part_number);
472 static int open_swms_from_glob(const char *swm_glob,
473 const char *first_part,
475 WIMStruct ***additional_swms_ret,
476 unsigned *num_additional_swms_ret)
478 unsigned num_additional_swms = 0;
479 WIMStruct **additional_swms = NULL;
483 ret = glob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
485 if (ret == GLOB_NOMATCH) {
486 imagex_error("Found no files for glob \"%s\"",
489 imagex_error_with_errno("Failed to process glob "
495 num_additional_swms = globbuf.gl_pathc;
496 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
497 if (!additional_swms) {
498 imagex_error("Out of memory");
503 for (unsigned i = 0; i < num_additional_swms; i++) {
504 if (strcmp(globbuf.gl_pathv[i], first_part) == 0) {
508 ret = wimlib_open_wim(globbuf.gl_pathv[i],
509 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
510 &additional_swms[i - offset],
511 imagex_progress_func);
515 *additional_swms_ret = additional_swms;
516 *num_additional_swms_ret = num_additional_swms - offset;
520 for (unsigned i = 0; i < num_additional_swms; i++)
521 wimlib_free(additional_swms[i]);
522 free(additional_swms);
530 static unsigned parse_num_threads(const char *optarg)
533 unsigned nthreads = strtoul(optarg, &tmp, 10);
534 if (nthreads == UINT_MAX || *tmp || tmp == optarg) {
535 imagex_error("Number of threads must be a non-negative integer!");
543 /* Extract one image, or all images, from a WIM file into a directory. */
544 static int imagex_apply(int argc, const char **argv)
547 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
554 const char *image_num_or_name;
555 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
557 const char *swm_glob = NULL;
558 WIMStruct **additional_swms = NULL;
559 unsigned num_additional_swms = 0;
561 for_opt(c, apply_options) {
564 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
567 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
570 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
573 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
585 if (argc != 2 && argc != 3) {
592 image_num_or_name = "1";
595 image_num_or_name = argv[1];
599 ret = wimlib_open_wim(wimfile, open_flags, &w, imagex_progress_func);
603 image = wimlib_resolve_image(w, image_num_or_name);
604 ret = verify_image_exists(image, image_num_or_name, wimfile);
608 num_images = wimlib_get_num_images(w);
609 if (argc == 2 && num_images != 1) {
610 imagex_error("`%s' contains %d images; Please select one "
611 "(or all)", wimfile, num_images);
618 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
620 &num_additional_swms);
627 ret = stat(target, &stbuf);
629 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
630 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
632 if (errno != ENOENT) {
633 imagex_error_with_errno("Failed to stat `%s'", target);
639 ret = wimlib_extract_image(w, image, target, extract_flags,
640 additional_swms, num_additional_swms,
641 imagex_progress_func);
643 printf("Done applying WIM image.\n");
646 if (additional_swms) {
647 for (unsigned i = 0; i < num_additional_swms; i++)
648 wimlib_free(additional_swms[i]);
649 free(additional_swms);
654 static int imagex_capture_or_append(int argc, const char **argv)
658 int add_image_flags = 0;
660 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
665 const char *flags_element = NULL;
666 const char *config_file = NULL;
667 char *config_str = NULL;
668 size_t config_len = 0;
673 int cmd = strcmp(argv[0], "append") ? CAPTURE : APPEND;
674 unsigned num_threads = 0;
676 for_opt(c, capture_or_append_options) {
679 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
682 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
683 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
686 config_file = optarg;
689 compression_type = get_compression_type(optarg);
690 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
694 flags_element = optarg;
697 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
700 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
703 num_threads = parse_num_threads(optarg);
704 if (num_threads == UINT_MAX)
708 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
717 if (argc < 2 || argc > 4) {
724 char source_copy[strlen(source) + 1];
725 memcpy(source_copy, source, strlen(source) + 1);
726 default_name = basename(source_copy);
728 name = (argc >= 3) ? argv[2] : default_name;
729 desc = (argc >= 4) ? argv[3] : NULL;
732 config_str = file_get_contents(config_file, &config_len);
738 ret = wimlib_open_wim(wimfile, open_flags, &w,
739 imagex_progress_func);
741 ret = wimlib_create_new_wim(compression_type, &w);
747 ret = stat(source, &stbuf);
749 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
750 printf("Capturing WIM image from NTFS filesystem on `%s'\n",
752 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
755 if (errno != ENOENT) {
756 imagex_error_with_errno("Failed to stat `%s'", source);
762 ret = wimlib_add_image(w, source, name, config_str, config_len,
763 add_image_flags, imagex_progress_func);
767 cur_image = wimlib_get_num_images(w);
769 ret = wimlib_set_image_descripton(w, cur_image, desc);
774 ret = wimlib_set_image_flags(w, cur_image, flags_element);
779 ret = wimlib_overwrite(w, write_flags, num_threads,
780 imagex_progress_func);
782 ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
783 num_threads, imagex_progress_func);
785 if (ret == WIMLIB_ERR_REOPEN)
788 imagex_error("Failed to write the WIM file `%s'", wimfile);
795 /* Remove image(s) from a WIM. */
796 static int imagex_delete(int argc, const char **argv)
802 const char *image_num_or_name;
807 for_opt(c, delete_options) {
810 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
811 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
814 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
826 imagex_error("Must specify a WIM file");
828 imagex_error("Must specify an image");
833 image_num_or_name = argv[1];
835 ret = file_writable(wimfile);
839 ret = wimlib_open_wim(wimfile, open_flags, &w,
840 imagex_progress_func);
844 image = wimlib_resolve_image(w, image_num_or_name);
846 ret = verify_image_exists(image, image_num_or_name, wimfile);
850 ret = wimlib_delete_image(w, image);
852 imagex_error("Failed to delete image from `%s'", wimfile);
856 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
857 if (ret == WIMLIB_ERR_REOPEN)
860 imagex_error("Failed to write the file `%s' with image "
868 /* Print the files contained in an image(s) in a WIM file. */
869 static int imagex_dir(int argc, const char **argv)
878 imagex_error("Must specify a WIM file");
883 imagex_error("Too many arguments");
889 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w,
890 imagex_progress_func);
895 image = wimlib_resolve_image(w, argv[2]);
896 ret = verify_image_exists(image, argv[2], wimfile);
900 /* Image was not specified. If the WIM only contains one image,
901 * choose that one; otherwise, print an error. */
902 num_images = wimlib_get_num_images(w);
903 if (num_images != 1) {
904 imagex_error("The file `%s' contains %d images; Please "
905 "select one.", wimfile, num_images);
913 ret = wimlib_print_files(w, image);
919 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
921 static int imagex_export(int argc, const char **argv)
925 int export_flags = 0;
927 int compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
928 bool compression_type_specified = false;
929 const char *src_wimfile;
930 const char *src_image_num_or_name;
931 const char *dest_wimfile;
932 const char *dest_name;
933 const char *dest_desc;
934 WIMStruct *src_w = NULL;
935 WIMStruct *dest_w = NULL;
940 const char *swm_glob = NULL;
941 WIMStruct **additional_swms = NULL;
942 unsigned num_additional_swms = 0;
943 unsigned num_threads = 0;
945 for_opt(c, export_options) {
948 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
951 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
952 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
955 compression_type = get_compression_type(optarg);
956 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
958 compression_type_specified = true;
964 num_threads = parse_num_threads(optarg);
965 if (num_threads == UINT_MAX)
969 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
978 if (argc < 3 || argc > 5) {
982 src_wimfile = argv[0];
983 src_image_num_or_name = argv[1];
984 dest_wimfile = argv[2];
985 dest_name = (argc >= 4) ? argv[3] : NULL;
986 dest_desc = (argc >= 5) ? argv[4] : NULL;
987 ret = wimlib_open_wim(src_wimfile,
988 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w,
989 imagex_progress_func);
993 /* Determine if the destination is an existing file or not.
994 * If so, we try to append the exported image(s) to it; otherwise, we
995 * create a new WIM containing the exported image(s). */
996 if (stat(dest_wimfile, &stbuf) == 0) {
1000 /* Destination file exists. */
1001 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
1002 imagex_error("`%s' is not a regular file",
1007 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w,
1008 imagex_progress_func);
1012 ret = file_writable(dest_wimfile);
1016 dest_ctype = wimlib_get_compression_type(dest_w);
1017 if (compression_type_specified
1018 && compression_type != dest_ctype)
1020 imagex_error("Cannot specify a compression type that is "
1021 "not the same as that used in the "
1026 compression_type = dest_ctype;
1029 /* dest_wimfile is not an existing file, so create a new WIM. */
1030 if (!compression_type_specified)
1031 compression_type = wimlib_get_compression_type(src_w);
1032 if (errno == ENOENT) {
1033 ret = wimlib_create_new_wim(compression_type, &dest_w);
1037 imagex_error_with_errno("Cannot stat file `%s'",
1044 image = wimlib_resolve_image(src_w, src_image_num_or_name);
1045 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
1050 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
1052 &num_additional_swms);
1057 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
1058 export_flags, additional_swms,
1059 num_additional_swms, imagex_progress_func);
1065 ret = wimlib_write(dest_w, dest_wimfile, WIMLIB_ALL_IMAGES,
1066 write_flags, num_threads,
1067 imagex_progress_func);
1069 ret = wimlib_overwrite(dest_w, write_flags, num_threads,
1070 imagex_progress_func);
1072 if (ret == WIMLIB_ERR_REOPEN)
1075 wimlib_free(dest_w);
1076 if (additional_swms) {
1077 for (unsigned i = 0; i < num_additional_swms; i++)
1078 wimlib_free(additional_swms[i]);
1079 free(additional_swms);
1084 /* Prints information about a WIM file; also can mark an image as bootable,
1085 * change the name of an image, or change the description of an image. */
1086 static int imagex_info(int argc, const char **argv)
1091 bool header = false;
1092 bool lookup_table = false;
1094 bool metadata = false;
1095 bool short_header = true;
1096 const char *xml_out_file = NULL;
1097 const char *wimfile;
1098 const char *image_num_or_name = "all";
1099 const char *new_name = NULL;
1100 const char *new_desc = NULL;
1105 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1110 for_opt(c, info_options) {
1120 short_header = false;
1123 lookup_table = true;
1124 short_header = false;
1128 short_header = false;
1131 xml_out_file = optarg;
1132 short_header = false;
1136 short_header = false;
1146 if (argc == 0 || argc > 4) {
1152 image_num_or_name = argv[1];
1162 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1164 ret = wimlib_open_wim(wimfile, open_flags, &w,
1165 imagex_progress_func);
1169 part_number = wimlib_get_part_number(w, &total_parts);
1171 image = wimlib_resolve_image(w, image_num_or_name);
1172 if (image == WIMLIB_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
1173 imagex_error("The image `%s' does not exist",
1176 imagex_error("If you would like to set the boot "
1177 "index to 0, specify image \"0\" with "
1178 "the --boot flag.");
1179 ret = WIMLIB_ERR_INVALID_IMAGE;
1183 num_images = wimlib_get_num_images(w);
1185 if (num_images == 0) {
1187 imagex_error("--boot is meaningless on a WIM with no "
1189 ret = WIMLIB_ERR_INVALID_IMAGE;
1194 if (image == WIMLIB_ALL_IMAGES && num_images > 1) {
1196 imagex_error("Cannot specify the --boot flag "
1197 "without specifying a specific "
1198 "image in a multi-image WIM");
1199 ret = WIMLIB_ERR_INVALID_IMAGE;
1203 imagex_error("Cannot specify the NEW_NAME "
1204 "without specifying a specific "
1205 "image in a multi-image WIM");
1206 ret = WIMLIB_ERR_INVALID_IMAGE;
1211 /* Operations that print information are separated from operations that
1212 * recreate the WIM file. */
1213 if (!new_name && !boot) {
1215 /* Read-only operations */
1217 if (image == WIMLIB_NO_IMAGE) {
1218 imagex_error("`%s' is not a valid image",
1220 ret = WIMLIB_ERR_INVALID_IMAGE;
1224 if (image == WIMLIB_ALL_IMAGES && short_header)
1225 wimlib_print_wim_information(w);
1228 wimlib_print_header(w);
1231 if (total_parts != 1) {
1232 printf("Warning: Only showing the lookup table "
1233 "for part %d of a %d-part WIM.\n",
1234 part_number, total_parts);
1236 wimlib_print_lookup_table(w);
1240 ret = wimlib_extract_xml_data(w, stdout);
1246 fp = fopen(xml_out_file, "wb");
1248 imagex_error_with_errno("Failed to open the "
1255 ret = wimlib_extract_xml_data(w, fp);
1256 if (fclose(fp) != 0) {
1257 imagex_error("Failed to close the file `%s'",
1267 wimlib_print_available_images(w, image);
1270 ret = wimlib_print_metadata(w, image);
1276 /* Modification operations */
1277 if (total_parts != 1) {
1278 imagex_error("Modifying a split WIM is not supported.");
1282 if (image == WIMLIB_ALL_IMAGES)
1285 if (image == WIMLIB_NO_IMAGE && new_name) {
1286 imagex_error("Cannot specify new_name (`%s') when "
1287 "using image 0", new_name);
1293 if (image == wimlib_get_boot_idx(w)) {
1294 printf("Image %d is already marked as "
1295 "bootable.\n", image);
1298 printf("Marking image %d as bootable.\n",
1300 wimlib_set_boot_idx(w, image);
1304 if (strcmp(wimlib_get_image_name(w, image),
1306 printf("Image %d is already named \"%s\".\n",
1310 printf("Changing the name of image %d to "
1311 "\"%s\".\n", image, new_name);
1312 ret = wimlib_set_image_name(w, image, new_name);
1318 const char *old_desc;
1319 old_desc = wimlib_get_image_description(w, image);
1320 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1321 printf("The description of image %d is already "
1322 "\"%s\".\n", image, new_desc);
1325 printf("Changing the description of image %d "
1326 "to \"%s\".\n", image, new_desc);
1327 ret = wimlib_set_image_descripton(w, image,
1334 /* Only call wimlib_overwrite() if something actually needs to
1336 if (boot || new_name || new_desc ||
1337 (check && !wimlib_has_integrity_table(w)))
1341 ret = file_writable(wimfile);
1346 write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1350 ret = wimlib_overwrite(w, write_flags, 1,
1351 imagex_progress_func);
1352 if (ret == WIMLIB_ERR_REOPEN)
1355 printf("The file `%s' was not modified because nothing "
1356 "needed to be done.\n", wimfile);
1365 /* Join split WIMs into one part WIM */
1366 static int imagex_join(int argc, const char **argv)
1369 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1370 int wim_write_flags = 0;
1371 const char *output_path;
1373 for_opt(c, join_options) {
1376 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1377 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1387 imagex_error("Must specify one or more split WIM (.swm) parts "
1391 output_path = argv[0];
1392 return wimlib_join(++argv, --argc, output_path, swm_open_flags,
1393 wim_write_flags, imagex_progress_func);
1399 /* Mounts an image using a FUSE mount. */
1400 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1403 int mount_flags = 0;
1404 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1405 const char *wimfile;
1411 const char *swm_glob = NULL;
1412 WIMStruct **additional_swms = NULL;
1413 unsigned num_additional_swms = 0;
1414 const char *staging_dir = NULL;
1416 if (strcmp(argv[0], "mountrw") == 0)
1417 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1419 for_opt(c, mount_options) {
1422 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1425 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1428 if (strcasecmp(optarg, "none") == 0)
1429 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1430 else if (strcasecmp(optarg, "xattr") == 0)
1431 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1432 else if (strcasecmp(optarg, "windows") == 0)
1433 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1435 imagex_error("Unknown stream interface \"%s\"", optarg);
1443 staging_dir = optarg;
1451 if (argc != 2 && argc != 3)
1456 ret = wimlib_open_wim(wimfile, open_flags, &w,
1457 imagex_progress_func);
1462 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1464 &num_additional_swms);
1471 num_images = wimlib_get_num_images(w);
1472 if (num_images != 1) {
1473 imagex_error("The file `%s' contains %d images; Please "
1474 "select one.", wimfile, num_images);
1475 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1482 image = wimlib_resolve_image(w, argv[1]);
1484 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
1489 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1490 ret = file_writable(wimfile);
1495 ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
1496 num_additional_swms, staging_dir);
1498 imagex_error("Failed to mount image %d from `%s' on `%s'",
1499 image, wimfile, dir);
1504 if (additional_swms) {
1505 for (unsigned i = 0; i < num_additional_swms; i++)
1506 wimlib_free(additional_swms[i]);
1507 free(additional_swms);
1511 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1516 static int imagex_optimize(int argc, const char **argv)
1520 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
1523 const char *wimfile;
1527 for_opt(c, optimize_options) {
1530 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1531 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1534 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1551 ret = wimlib_open_wim(wimfile, open_flags, &w,
1552 imagex_progress_func);
1556 old_size = file_get_size(argv[0]);
1557 printf("`%s' original size: ", wimfile);
1561 printf("%"PRIu64" KiB\n", old_size >> 10);
1563 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1566 new_size = file_get_size(argv[0]);
1567 printf("`%s' optimized size: ", wimfile);
1571 printf("%"PRIu64" KiB\n", new_size >> 10);
1573 fputs("Space saved: ", stdout);
1574 if (new_size != -1 && old_size != -1) {
1575 printf("%lld KiB\n",
1576 ((long long)old_size - (long long)new_size) >> 10);
1586 /* Split a WIM into a spanned set */
1587 static int imagex_split(int argc, const char **argv)
1590 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1591 int write_flags = 0;
1592 unsigned long part_size;
1597 for_opt(c, split_options) {
1600 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1601 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1615 part_size = strtod(argv[2], &tmp) * (1 << 20);
1616 if (tmp == argv[2] || *tmp) {
1617 imagex_error("Invalid part size \"%s\"", argv[2]);
1618 imagex_error("The part size must be an integer or floating-point number of megabytes.");
1621 ret = wimlib_open_wim(argv[0], open_flags, &w, imagex_progress_func);
1624 ret = wimlib_split(w, argv[1], part_size, write_flags, imagex_progress_func);
1629 /* Unmounts an image. */
1630 static int imagex_unmount(int argc, const char **argv)
1633 int unmount_flags = 0;
1636 for_opt(c, unmount_options) {
1639 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1642 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1645 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
1659 ret = wimlib_unmount_image(argv[0], unmount_flags,
1660 imagex_progress_func);
1662 imagex_error("Failed to unmount `%s'", argv[0]);
1666 struct imagex_command {
1668 int (*func)(int , const char **);
1673 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1674 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1676 static const struct imagex_command imagex_commands[] = {
1677 {"append", imagex_capture_or_append, APPEND},
1678 {"apply", imagex_apply, APPLY},
1679 {"capture", imagex_capture_or_append, CAPTURE},
1680 {"delete", imagex_delete, DELETE},
1681 {"dir", imagex_dir, DIR},
1682 {"export", imagex_export, EXPORT},
1683 {"info", imagex_info, INFO},
1684 {"join", imagex_join, JOIN},
1685 {"mount", imagex_mount_rw_or_ro, MOUNT},
1686 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1687 {"optimize",imagex_optimize, OPTIMIZE},
1688 {"split", imagex_split, SPLIT},
1689 {"unmount", imagex_unmount, UNMOUNT},
1692 static void version()
1694 static const char *s =
1695 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
1696 "Copyright (C) 2012 Eric Biggers\n"
1697 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
1698 "This is free software: you are free to change and redistribute it.\n"
1699 "There is NO WARRANTY, to the extent permitted by law.\n"
1701 "Report bugs to "PACKAGE_BUGREPORT".\n";
1706 static void help_or_version(int argc, const char **argv)
1710 const struct imagex_command *cmd;
1712 for (i = 1; i < argc; i++) {
1720 if (strcmp(p, "help") == 0) {
1721 for_imagex_command(cmd) {
1722 if (strcmp(cmd->name, argv[1]) == 0) {
1730 if (strcmp(p, "version") == 0) {
1738 static void usage(int cmd_type)
1740 const struct imagex_command *cmd;
1741 printf("Usage: %s", usage_strings[cmd_type]);
1742 for_imagex_command(cmd) {
1743 if (cmd->cmd == cmd_type)
1744 printf("\nTry `man imagex-%s' for more details.\n",
1749 static void usage_all()
1751 puts("IMAGEX: Usage:");
1752 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
1753 printf(" %s", usage_strings[i]);
1754 static const char *extra =
1756 " imagex --version\n"
1758 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
1760 " Try `man imagex' for more information.\n"
1762 fputs(extra, stdout);
1766 int main(int argc, const char **argv)
1768 const struct imagex_command *cmd;
1772 imagex_error("No command specified");
1777 help_or_version(argc, argv);
1781 wimlib_set_print_errors(true);
1783 for_imagex_command(cmd) {
1784 if (strcmp(cmd->name, *argv) == 0) {
1785 ret = cmd->func(argc, argv);
1787 imagex_error("Exiting with error code %d:\n"
1789 wimlib_get_error_string(ret));
1790 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
1791 imagex_error_with_errno("errno");
1797 imagex_error("Unrecognized command: `%s'", argv[0]);