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_IMAGE_END:
411 printf("Done applying WIM image.\n");
412 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
413 printf("Unmounting NTFS volume `%s'...\n",
414 info->extract.target);
417 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
418 /*printf("Applying directory structure to %s\n",*/
419 /*info->extract.target);*/
421 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
422 percent_done = TO_PERCENT(info->extract.completed_bytes,
423 info->extract.total_bytes);
424 printf("\rExtracting files: "
425 "%"PRIu64" MiB of %"PRIu64" MiB (%u%%) done",
426 info->extract.completed_bytes >> 20,
427 info->extract.total_bytes >> 20,
429 if (info->extract.completed_bytes == info->extract.total_bytes)
432 case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
433 puts(info->extract.cur_path);
435 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
436 percent_done = TO_PERCENT(info->join.completed_bytes,
437 info->join.total_bytes);
438 printf("Writing resources from part %u of %u: "
439 "%"PRIu64 " MiB of %"PRIu64" MiB (%u%%) written\n",
440 (info->join.completed_parts == info->join.total_parts) ?
441 info->join.completed_parts : info->join.completed_parts + 1,
442 info->join.total_parts,
443 info->join.completed_bytes >> 20,
444 info->join.total_bytes >> 20,
447 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
448 percent_done = TO_PERCENT(info->split.completed_bytes,
449 info->split.total_bytes);
450 printf("Writing `%s': %"PRIu64" MiB of %"PRIu64" MiB (%u%%) written\n",
451 info->split.part_name,
452 info->split.completed_bytes >> 20,
453 info->split.total_bytes >> 20,
456 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
457 if (info->split.completed_bytes == info->split.total_bytes) {
458 printf("Finished writing %u split WIM parts\n",
459 info->split.cur_part_number);
470 static int open_swms_from_glob(const char *swm_glob,
471 const char *first_part,
473 WIMStruct ***additional_swms_ret,
474 unsigned *num_additional_swms_ret)
476 unsigned num_additional_swms = 0;
477 WIMStruct **additional_swms = NULL;
481 ret = glob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
483 if (ret == GLOB_NOMATCH) {
484 imagex_error("Found no files for glob \"%s\"",
487 imagex_error_with_errno("Failed to process glob "
493 num_additional_swms = globbuf.gl_pathc;
494 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
495 if (!additional_swms) {
496 imagex_error("Out of memory");
501 for (unsigned i = 0; i < num_additional_swms; i++) {
502 if (strcmp(globbuf.gl_pathv[i], first_part) == 0) {
506 ret = wimlib_open_wim(globbuf.gl_pathv[i],
507 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
508 &additional_swms[i - offset],
509 imagex_progress_func);
513 *additional_swms_ret = additional_swms;
514 *num_additional_swms_ret = num_additional_swms - offset;
518 for (unsigned i = 0; i < num_additional_swms; i++)
519 wimlib_free(additional_swms[i]);
520 free(additional_swms);
528 static unsigned parse_num_threads(const char *optarg)
531 unsigned nthreads = strtoul(optarg, &tmp, 10);
532 if (nthreads == UINT_MAX || *tmp || tmp == optarg) {
533 imagex_error("Number of threads must be a non-negative integer!");
541 /* Extract one image, or all images, from a WIM file into a directory. */
542 static int imagex_apply(int argc, const char **argv)
545 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
552 const char *image_num_or_name;
553 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
555 const char *swm_glob = NULL;
556 WIMStruct **additional_swms = NULL;
557 unsigned num_additional_swms = 0;
559 for_opt(c, apply_options) {
562 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
565 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
568 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
571 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
583 if (argc != 2 && argc != 3) {
590 image_num_or_name = "1";
593 image_num_or_name = argv[1];
597 ret = wimlib_open_wim(wimfile, open_flags, &w, imagex_progress_func);
601 image = wimlib_resolve_image(w, image_num_or_name);
602 ret = verify_image_exists(image, image_num_or_name, wimfile);
606 num_images = wimlib_get_num_images(w);
607 if (argc == 2 && num_images != 1) {
608 imagex_error("`%s' contains %d images; Please select one "
609 "(or all)", wimfile, num_images);
616 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
618 &num_additional_swms);
625 ret = stat(target, &stbuf);
627 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
628 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
630 if (errno != ENOENT) {
631 imagex_error_with_errno("Failed to stat `%s'", target);
637 ret = wimlib_extract_image(w, image, target, extract_flags,
638 additional_swms, num_additional_swms,
639 imagex_progress_func);
642 if (additional_swms) {
643 for (unsigned i = 0; i < num_additional_swms; i++)
644 wimlib_free(additional_swms[i]);
645 free(additional_swms);
650 static int imagex_capture_or_append(int argc, const char **argv)
654 int add_image_flags = 0;
656 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
661 const char *flags_element = NULL;
662 const char *config_file = NULL;
663 char *config_str = NULL;
664 size_t config_len = 0;
669 int cmd = strcmp(argv[0], "append") ? CAPTURE : APPEND;
670 unsigned num_threads = 0;
672 for_opt(c, capture_or_append_options) {
675 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
678 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
679 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
682 config_file = optarg;
685 compression_type = get_compression_type(optarg);
686 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
690 flags_element = optarg;
693 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
696 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
699 num_threads = parse_num_threads(optarg);
700 if (num_threads == UINT_MAX)
704 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
713 if (argc < 2 || argc > 4) {
720 char source_copy[strlen(source) + 1];
721 memcpy(source_copy, source, strlen(source) + 1);
722 default_name = basename(source_copy);
724 name = (argc >= 3) ? argv[2] : default_name;
725 desc = (argc >= 4) ? argv[3] : NULL;
728 config_str = file_get_contents(config_file, &config_len);
734 ret = wimlib_open_wim(wimfile, open_flags, &w,
735 imagex_progress_func);
737 ret = wimlib_create_new_wim(compression_type, &w);
743 ret = stat(source, &stbuf);
745 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
746 printf("Capturing WIM image from NTFS filesystem on `%s'\n",
748 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
751 if (errno != ENOENT) {
752 imagex_error_with_errno("Failed to stat `%s'", source);
758 ret = wimlib_add_image(w, source, name, config_str, config_len,
759 add_image_flags, imagex_progress_func);
763 cur_image = wimlib_get_num_images(w);
765 ret = wimlib_set_image_descripton(w, cur_image, desc);
770 ret = wimlib_set_image_flags(w, cur_image, flags_element);
775 ret = wimlib_overwrite(w, write_flags, num_threads,
776 imagex_progress_func);
778 ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
779 num_threads, imagex_progress_func);
781 if (ret == WIMLIB_ERR_REOPEN)
784 imagex_error("Failed to write the WIM file `%s'", wimfile);
791 /* Remove image(s) from a WIM. */
792 static int imagex_delete(int argc, const char **argv)
798 const char *image_num_or_name;
803 for_opt(c, delete_options) {
806 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
807 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
810 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
822 imagex_error("Must specify a WIM file");
824 imagex_error("Must specify an image");
829 image_num_or_name = argv[1];
831 ret = file_writable(wimfile);
835 ret = wimlib_open_wim(wimfile, open_flags, &w,
836 imagex_progress_func);
840 image = wimlib_resolve_image(w, image_num_or_name);
842 ret = verify_image_exists(image, image_num_or_name, wimfile);
846 ret = wimlib_delete_image(w, image);
848 imagex_error("Failed to delete image from `%s'", wimfile);
852 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
853 if (ret == WIMLIB_ERR_REOPEN)
856 imagex_error("Failed to write the file `%s' with image "
864 /* Print the files contained in an image(s) in a WIM file. */
865 static int imagex_dir(int argc, const char **argv)
874 imagex_error("Must specify a WIM file");
879 imagex_error("Too many arguments");
885 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w,
886 imagex_progress_func);
891 image = wimlib_resolve_image(w, argv[2]);
892 ret = verify_image_exists(image, argv[2], wimfile);
896 /* Image was not specified. If the WIM only contains one image,
897 * choose that one; otherwise, print an error. */
898 num_images = wimlib_get_num_images(w);
899 if (num_images != 1) {
900 imagex_error("The file `%s' contains %d images; Please "
901 "select one.", wimfile, num_images);
909 ret = wimlib_print_files(w, image);
915 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
917 static int imagex_export(int argc, const char **argv)
921 int export_flags = 0;
923 int compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
924 bool compression_type_specified = false;
925 const char *src_wimfile;
926 const char *src_image_num_or_name;
927 const char *dest_wimfile;
928 const char *dest_name;
929 const char *dest_desc;
930 WIMStruct *src_w = NULL;
931 WIMStruct *dest_w = NULL;
936 const char *swm_glob = NULL;
937 WIMStruct **additional_swms = NULL;
938 unsigned num_additional_swms = 0;
939 unsigned num_threads = 0;
941 for_opt(c, export_options) {
944 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
947 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
948 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
951 compression_type = get_compression_type(optarg);
952 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
954 compression_type_specified = true;
960 num_threads = parse_num_threads(optarg);
961 if (num_threads == UINT_MAX)
965 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
974 if (argc < 3 || argc > 5) {
978 src_wimfile = argv[0];
979 src_image_num_or_name = argv[1];
980 dest_wimfile = argv[2];
981 dest_name = (argc >= 4) ? argv[3] : NULL;
982 dest_desc = (argc >= 5) ? argv[4] : NULL;
983 ret = wimlib_open_wim(src_wimfile,
984 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w,
985 imagex_progress_func);
989 /* Determine if the destination is an existing file or not.
990 * If so, we try to append the exported image(s) to it; otherwise, we
991 * create a new WIM containing the exported image(s). */
992 if (stat(dest_wimfile, &stbuf) == 0) {
996 /* Destination file exists. */
997 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
998 imagex_error("`%s' is not a regular file",
1003 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w,
1004 imagex_progress_func);
1008 ret = file_writable(dest_wimfile);
1012 dest_ctype = wimlib_get_compression_type(dest_w);
1013 if (compression_type_specified
1014 && compression_type != dest_ctype)
1016 imagex_error("Cannot specify a compression type that is "
1017 "not the same as that used in the "
1022 compression_type = dest_ctype;
1025 /* dest_wimfile is not an existing file, so create a new WIM. */
1026 if (!compression_type_specified)
1027 compression_type = wimlib_get_compression_type(src_w);
1028 if (errno == ENOENT) {
1029 ret = wimlib_create_new_wim(compression_type, &dest_w);
1033 imagex_error_with_errno("Cannot stat file `%s'",
1040 image = wimlib_resolve_image(src_w, src_image_num_or_name);
1041 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
1046 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
1048 &num_additional_swms);
1053 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
1054 export_flags, additional_swms,
1055 num_additional_swms, imagex_progress_func);
1061 ret = wimlib_write(dest_w, dest_wimfile, WIMLIB_ALL_IMAGES,
1062 write_flags, num_threads,
1063 imagex_progress_func);
1065 ret = wimlib_overwrite(dest_w, write_flags, num_threads,
1066 imagex_progress_func);
1068 if (ret == WIMLIB_ERR_REOPEN)
1071 wimlib_free(dest_w);
1072 if (additional_swms) {
1073 for (unsigned i = 0; i < num_additional_swms; i++)
1074 wimlib_free(additional_swms[i]);
1075 free(additional_swms);
1080 /* Prints information about a WIM file; also can mark an image as bootable,
1081 * change the name of an image, or change the description of an image. */
1082 static int imagex_info(int argc, const char **argv)
1087 bool header = false;
1088 bool lookup_table = false;
1090 bool metadata = false;
1091 bool short_header = true;
1092 const char *xml_out_file = NULL;
1093 const char *wimfile;
1094 const char *image_num_or_name = "all";
1095 const char *new_name = NULL;
1096 const char *new_desc = NULL;
1101 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1106 for_opt(c, info_options) {
1116 short_header = false;
1119 lookup_table = true;
1120 short_header = false;
1124 short_header = false;
1127 xml_out_file = optarg;
1128 short_header = false;
1132 short_header = false;
1142 if (argc == 0 || argc > 4) {
1148 image_num_or_name = argv[1];
1158 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1160 ret = wimlib_open_wim(wimfile, open_flags, &w,
1161 imagex_progress_func);
1165 part_number = wimlib_get_part_number(w, &total_parts);
1167 image = wimlib_resolve_image(w, image_num_or_name);
1168 if (image == WIMLIB_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
1169 imagex_error("The image `%s' does not exist",
1172 imagex_error("If you would like to set the boot "
1173 "index to 0, specify image \"0\" with "
1174 "the --boot flag.");
1175 ret = WIMLIB_ERR_INVALID_IMAGE;
1179 num_images = wimlib_get_num_images(w);
1181 if (num_images == 0) {
1183 imagex_error("--boot is meaningless on a WIM with no "
1185 ret = WIMLIB_ERR_INVALID_IMAGE;
1190 if (image == WIMLIB_ALL_IMAGES && num_images > 1) {
1192 imagex_error("Cannot specify the --boot flag "
1193 "without specifying a specific "
1194 "image in a multi-image WIM");
1195 ret = WIMLIB_ERR_INVALID_IMAGE;
1199 imagex_error("Cannot specify the NEW_NAME "
1200 "without specifying a specific "
1201 "image in a multi-image WIM");
1202 ret = WIMLIB_ERR_INVALID_IMAGE;
1207 /* Operations that print information are separated from operations that
1208 * recreate the WIM file. */
1209 if (!new_name && !boot) {
1211 /* Read-only operations */
1213 if (image == WIMLIB_NO_IMAGE) {
1214 imagex_error("`%s' is not a valid image",
1216 ret = WIMLIB_ERR_INVALID_IMAGE;
1220 if (image == WIMLIB_ALL_IMAGES && short_header)
1221 wimlib_print_wim_information(w);
1224 wimlib_print_header(w);
1227 if (total_parts != 1) {
1228 printf("Warning: Only showing the lookup table "
1229 "for part %d of a %d-part WIM.\n",
1230 part_number, total_parts);
1232 wimlib_print_lookup_table(w);
1236 ret = wimlib_extract_xml_data(w, stdout);
1242 fp = fopen(xml_out_file, "wb");
1244 imagex_error_with_errno("Failed to open the "
1251 ret = wimlib_extract_xml_data(w, fp);
1252 if (fclose(fp) != 0) {
1253 imagex_error("Failed to close the file `%s'",
1263 wimlib_print_available_images(w, image);
1266 ret = wimlib_print_metadata(w, image);
1272 /* Modification operations */
1273 if (total_parts != 1) {
1274 imagex_error("Modifying a split WIM is not supported.");
1278 if (image == WIMLIB_ALL_IMAGES)
1281 if (image == WIMLIB_NO_IMAGE && new_name) {
1282 imagex_error("Cannot specify new_name (`%s') when "
1283 "using image 0", new_name);
1289 if (image == wimlib_get_boot_idx(w)) {
1290 printf("Image %d is already marked as "
1291 "bootable.\n", image);
1294 printf("Marking image %d as bootable.\n",
1296 wimlib_set_boot_idx(w, image);
1300 if (strcmp(wimlib_get_image_name(w, image),
1302 printf("Image %d is already named \"%s\".\n",
1306 printf("Changing the name of image %d to "
1307 "\"%s\".\n", image, new_name);
1308 ret = wimlib_set_image_name(w, image, new_name);
1314 const char *old_desc;
1315 old_desc = wimlib_get_image_description(w, image);
1316 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1317 printf("The description of image %d is already "
1318 "\"%s\".\n", image, new_desc);
1321 printf("Changing the description of image %d "
1322 "to \"%s\".\n", image, new_desc);
1323 ret = wimlib_set_image_descripton(w, image,
1330 /* Only call wimlib_overwrite() if something actually needs to
1332 if (boot || new_name || new_desc ||
1333 (check && !wimlib_has_integrity_table(w)))
1337 ret = file_writable(wimfile);
1342 write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1346 ret = wimlib_overwrite(w, write_flags, 1,
1347 imagex_progress_func);
1348 if (ret == WIMLIB_ERR_REOPEN)
1351 printf("The file `%s' was not modified because nothing "
1352 "needed to be done.\n", wimfile);
1361 /* Join split WIMs into one part WIM */
1362 static int imagex_join(int argc, const char **argv)
1365 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1366 int wim_write_flags = 0;
1367 const char *output_path;
1369 for_opt(c, join_options) {
1372 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1373 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1383 imagex_error("Must specify one or more split WIM (.swm) parts "
1387 output_path = argv[0];
1388 return wimlib_join(++argv, --argc, output_path, swm_open_flags,
1389 wim_write_flags, imagex_progress_func);
1395 /* Mounts an image using a FUSE mount. */
1396 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1399 int mount_flags = 0;
1400 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1401 const char *wimfile;
1407 const char *swm_glob = NULL;
1408 WIMStruct **additional_swms = NULL;
1409 unsigned num_additional_swms = 0;
1410 const char *staging_dir = NULL;
1412 if (strcmp(argv[0], "mountrw") == 0)
1413 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1415 for_opt(c, mount_options) {
1418 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1421 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1424 if (strcasecmp(optarg, "none") == 0)
1425 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1426 else if (strcasecmp(optarg, "xattr") == 0)
1427 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1428 else if (strcasecmp(optarg, "windows") == 0)
1429 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1431 imagex_error("Unknown stream interface \"%s\"", optarg);
1439 staging_dir = optarg;
1447 if (argc != 2 && argc != 3)
1452 ret = wimlib_open_wim(wimfile, open_flags, &w,
1453 imagex_progress_func);
1458 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1460 &num_additional_swms);
1467 num_images = wimlib_get_num_images(w);
1468 if (num_images != 1) {
1469 imagex_error("The file `%s' contains %d images; Please "
1470 "select one.", wimfile, num_images);
1471 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1478 image = wimlib_resolve_image(w, argv[1]);
1480 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
1485 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1486 ret = file_writable(wimfile);
1491 ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
1492 num_additional_swms, staging_dir);
1494 imagex_error("Failed to mount image %d from `%s' on `%s'",
1495 image, wimfile, dir);
1500 if (additional_swms) {
1501 for (unsigned i = 0; i < num_additional_swms; i++)
1502 wimlib_free(additional_swms[i]);
1503 free(additional_swms);
1507 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1512 static int imagex_optimize(int argc, const char **argv)
1516 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
1519 const char *wimfile;
1523 for_opt(c, optimize_options) {
1526 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1527 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1530 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1547 ret = wimlib_open_wim(wimfile, open_flags, &w,
1548 imagex_progress_func);
1552 old_size = file_get_size(argv[0]);
1553 printf("`%s' original size: ", wimfile);
1557 printf("%"PRIu64" KiB\n", old_size >> 10);
1559 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1562 new_size = file_get_size(argv[0]);
1563 printf("`%s' optimized size: ", wimfile);
1567 printf("%"PRIu64" KiB\n", new_size >> 10);
1569 fputs("Space saved: ", stdout);
1570 if (new_size != -1 && old_size != -1) {
1571 printf("%lld KiB\n",
1572 ((long long)old_size - (long long)new_size) >> 10);
1582 /* Split a WIM into a spanned set */
1583 static int imagex_split(int argc, const char **argv)
1586 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1587 int write_flags = 0;
1588 unsigned long part_size;
1593 for_opt(c, split_options) {
1596 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1597 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1611 part_size = strtod(argv[2], &tmp) * (1 << 20);
1612 if (tmp == argv[2] || *tmp) {
1613 imagex_error("Invalid part size \"%s\"", argv[2]);
1614 imagex_error("The part size must be an integer or floating-point number of megabytes.");
1617 ret = wimlib_open_wim(argv[0], open_flags, &w, imagex_progress_func);
1620 ret = wimlib_split(w, argv[1], part_size, write_flags, imagex_progress_func);
1625 /* Unmounts an image. */
1626 static int imagex_unmount(int argc, const char **argv)
1629 int unmount_flags = 0;
1632 for_opt(c, unmount_options) {
1635 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1638 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1641 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
1655 ret = wimlib_unmount_image(argv[0], unmount_flags,
1656 imagex_progress_func);
1658 imagex_error("Failed to unmount `%s'", argv[0]);
1662 struct imagex_command {
1664 int (*func)(int , const char **);
1669 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1670 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1672 static const struct imagex_command imagex_commands[] = {
1673 {"append", imagex_capture_or_append, APPEND},
1674 {"apply", imagex_apply, APPLY},
1675 {"capture", imagex_capture_or_append, CAPTURE},
1676 {"delete", imagex_delete, DELETE},
1677 {"dir", imagex_dir, DIR},
1678 {"export", imagex_export, EXPORT},
1679 {"info", imagex_info, INFO},
1680 {"join", imagex_join, JOIN},
1681 {"mount", imagex_mount_rw_or_ro, MOUNT},
1682 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1683 {"optimize",imagex_optimize, OPTIMIZE},
1684 {"split", imagex_split, SPLIT},
1685 {"unmount", imagex_unmount, UNMOUNT},
1688 static void version()
1690 static const char *s =
1691 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
1692 "Copyright (C) 2012 Eric Biggers\n"
1693 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
1694 "This is free software: you are free to change and redistribute it.\n"
1695 "There is NO WARRANTY, to the extent permitted by law.\n"
1697 "Report bugs to "PACKAGE_BUGREPORT".\n";
1702 static void help_or_version(int argc, const char **argv)
1706 const struct imagex_command *cmd;
1708 for (i = 1; i < argc; i++) {
1716 if (strcmp(p, "help") == 0) {
1717 for_imagex_command(cmd) {
1718 if (strcmp(cmd->name, argv[1]) == 0) {
1726 if (strcmp(p, "version") == 0) {
1734 static void usage(int cmd_type)
1736 const struct imagex_command *cmd;
1737 printf("Usage: %s", usage_strings[cmd_type]);
1738 for_imagex_command(cmd) {
1739 if (cmd->cmd == cmd_type)
1740 printf("\nTry `man imagex-%s' for more details.\n",
1745 static void usage_all()
1747 puts("IMAGEX: Usage:");
1748 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
1749 printf(" %s", usage_strings[i]);
1750 static const char *extra =
1752 " imagex --version\n"
1754 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
1756 " Try `man imagex' for more information.\n"
1758 fputs(extra, stdout);
1762 int main(int argc, const char **argv)
1764 const struct imagex_command *cmd;
1768 imagex_error("No command specified");
1773 help_or_version(argc, argv);
1777 wimlib_set_print_errors(true);
1779 for_imagex_command(cmd) {
1780 if (strcmp(cmd->name, *argv) == 0) {
1781 ret = cmd->func(argc, argv);
1783 imagex_error("Exiting with error code %d:\n"
1785 wimlib_get_error_string(ret));
1786 if (ret == WIMLIB_ERR_NTFS_3G)
1787 imagex_error_with_errno("errno");
1793 imagex_error("Unrecognized command: `%s'", argv[0]);