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 char *file_get_contents(const char *filename, size_t *len_ret)
290 if (stat(filename, &stbuf) != 0) {
291 imagex_error_with_errno("Failed to stat the file `%s'", filename);
296 fp = fopen(filename, "rb");
298 imagex_error_with_errno("Failed to open the file `%s'", filename);
304 imagex_error("Failed to allocate buffer of %zu bytes to hold "
305 "contents of file `%s'", len, filename);
308 if (fread(buf, 1, len, fp) != len) {
309 imagex_error_with_errno("Failed to read %lu bytes from the "
310 "file `%s'", len, filename);
322 static int file_writable(const char *path)
325 ret = access(path, W_OK);
327 imagex_error_with_errno("Can't modify `%s'", path);
331 #define TO_PERCENT(numerator, denominator) \
332 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
334 static const char *get_data_type(int ctype)
337 case WIMLIB_COMPRESSION_TYPE_NONE:
338 return "uncompressed";
339 case WIMLIB_COMPRESSION_TYPE_LZX:
340 return "LZX-compressed";
341 case WIMLIB_COMPRESSION_TYPE_XPRESS:
342 return "XPRESS-compressed";
347 static int imagex_progress_func(enum wimlib_progress_msg msg,
348 const union wimlib_progress_info *info)
350 unsigned percent_done;
352 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
353 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
354 info->write_streams.total_bytes);
355 if (info->write_streams.completed_streams == 0) {
356 const char *data_type;
358 data_type = get_data_type(info->write_streams.compression_type);
359 printf("Writing %s data using %u thread%s\n",
360 data_type, info->write_streams.num_threads,
361 (info->write_streams.num_threads == 1) ? "" : "s");
363 printf("\r%"PRIu64" MiB of %"PRIu64" MiB (uncompressed) "
364 "written (%u%% done)",
365 info->write_streams.completed_bytes >> 20,
366 info->write_streams.total_bytes >> 20,
368 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
371 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
372 printf("Scanning `%s'...\n", info->scan.source);
374 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
375 if (info->scan.excluded)
376 printf("Excluding `%s' from capture\n", info->scan.cur_path);
378 printf("Scanning `%s'\n", info->scan.cur_path);
380 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
382 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
383 percent_done = TO_PERCENT(info->integrity.completed_bytes,
384 info->integrity.total_bytes);
385 printf("\rVerifying integrity of `%s': %"PRIu64" MiB "
386 "of %"PRIu64" MiB (%u%%) done",
387 info->integrity.filename,
388 info->integrity.completed_bytes >> 20,
389 info->integrity.total_bytes >> 20,
391 if (info->integrity.completed_bytes == info->integrity.total_bytes)
394 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
395 percent_done = TO_PERCENT(info->integrity.completed_bytes,
396 info->integrity.total_bytes);
397 printf("\rCalculating integrity table for WIM: %"PRIu64" MiB "
398 "of %"PRIu64" MiB (%u%%) done",
399 info->integrity.completed_bytes >> 20,
400 info->integrity.total_bytes >> 20,
402 if (info->integrity.completed_bytes == info->integrity.total_bytes)
405 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
406 printf("Applying image %d (%s) from `%s' to %s `%s'\n",
408 info->extract.image_name,
409 info->extract.wimfile_name,
410 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
411 "NTFS volume" : "directory"),
412 info->extract.target);
414 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
415 /*printf("Applying directory structure to %s\n",*/
416 /*info->extract.target);*/
418 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
419 percent_done = TO_PERCENT(info->extract.completed_bytes,
420 info->extract.total_bytes);
421 printf("\rExtracting files: "
422 "%"PRIu64" MiB of %"PRIu64" MiB (%u%%) done",
423 info->extract.completed_bytes >> 20,
424 info->extract.total_bytes >> 20,
426 if (info->extract.completed_bytes >= info->extract.total_bytes)
429 case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
430 puts(info->extract.cur_path);
432 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
433 printf("Setting timestamps on all extracted files...\n");
435 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
436 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
437 printf("Unmounting NTFS volume `%s'...\n",
438 info->extract.target);
441 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
442 percent_done = TO_PERCENT(info->join.completed_bytes,
443 info->join.total_bytes);
444 printf("Writing resources from part %u of %u: "
445 "%"PRIu64 " MiB of %"PRIu64" MiB (%u%%) written\n",
446 (info->join.completed_parts == info->join.total_parts) ?
447 info->join.completed_parts : info->join.completed_parts + 1,
448 info->join.total_parts,
449 info->join.completed_bytes >> 20,
450 info->join.total_bytes >> 20,
453 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
454 percent_done = TO_PERCENT(info->split.completed_bytes,
455 info->split.total_bytes);
456 printf("Writing `%s': %"PRIu64" MiB of %"PRIu64" MiB (%u%%) written\n",
457 info->split.part_name,
458 info->split.completed_bytes >> 20,
459 info->split.total_bytes >> 20,
462 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
463 if (info->split.completed_bytes == info->split.total_bytes) {
464 printf("Finished writing %u split WIM parts\n",
465 info->split.cur_part_number);
476 static int open_swms_from_glob(const char *swm_glob,
477 const char *first_part,
479 WIMStruct ***additional_swms_ret,
480 unsigned *num_additional_swms_ret)
482 unsigned num_additional_swms = 0;
483 WIMStruct **additional_swms = NULL;
487 ret = glob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
489 if (ret == GLOB_NOMATCH) {
490 imagex_error("Found no files for glob \"%s\"",
493 imagex_error_with_errno("Failed to process glob "
499 num_additional_swms = globbuf.gl_pathc;
500 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
501 if (!additional_swms) {
502 imagex_error("Out of memory");
507 for (unsigned i = 0; i < num_additional_swms; i++) {
508 if (strcmp(globbuf.gl_pathv[i], first_part) == 0) {
512 ret = wimlib_open_wim(globbuf.gl_pathv[i],
513 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
514 &additional_swms[i - offset],
515 imagex_progress_func);
519 *additional_swms_ret = additional_swms;
520 *num_additional_swms_ret = num_additional_swms - offset;
524 for (unsigned i = 0; i < num_additional_swms; i++)
525 wimlib_free(additional_swms[i]);
526 free(additional_swms);
534 static unsigned parse_num_threads(const char *optarg)
537 unsigned nthreads = strtoul(optarg, &tmp, 10);
538 if (nthreads == UINT_MAX || *tmp || tmp == optarg) {
539 imagex_error("Number of threads must be a non-negative integer!");
547 /* Extract one image, or all images, from a WIM file into a directory. */
548 static int imagex_apply(int argc, const char **argv)
551 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
558 const char *image_num_or_name;
559 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
561 const char *swm_glob = NULL;
562 WIMStruct **additional_swms = NULL;
563 unsigned num_additional_swms = 0;
565 for_opt(c, apply_options) {
568 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
571 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
574 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
577 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
583 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
592 if (argc != 2 && argc != 3) {
599 image_num_or_name = "1";
602 image_num_or_name = argv[1];
606 ret = wimlib_open_wim(wimfile, open_flags, &w, imagex_progress_func);
610 image = wimlib_resolve_image(w, image_num_or_name);
611 ret = verify_image_exists(image, image_num_or_name, wimfile);
615 num_images = wimlib_get_num_images(w);
616 if (argc == 2 && num_images != 1) {
617 imagex_error("`%s' contains %d images; Please select one "
618 "(or all)", wimfile, num_images);
625 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
627 &num_additional_swms);
634 ret = stat(target, &stbuf);
636 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
637 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
639 if (errno != ENOENT) {
640 imagex_error_with_errno("Failed to stat `%s'", target);
646 ret = wimlib_extract_image(w, image, target, extract_flags,
647 additional_swms, num_additional_swms,
648 imagex_progress_func);
650 printf("Done applying WIM image.\n");
653 if (additional_swms) {
654 for (unsigned i = 0; i < num_additional_swms; i++)
655 wimlib_free(additional_swms[i]);
656 free(additional_swms);
661 static int imagex_capture_or_append(int argc, const char **argv)
665 int add_image_flags = 0;
667 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
672 const char *flags_element = NULL;
673 const char *config_file = NULL;
674 char *config_str = NULL;
675 size_t config_len = 0;
680 int cmd = strcmp(argv[0], "append") ? CAPTURE : APPEND;
681 unsigned num_threads = 0;
683 for_opt(c, capture_or_append_options) {
686 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
689 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
690 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
693 config_file = optarg;
696 compression_type = get_compression_type(optarg);
697 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
701 flags_element = optarg;
704 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
707 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
710 num_threads = parse_num_threads(optarg);
711 if (num_threads == UINT_MAX)
715 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
718 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
727 if (argc < 2 || argc > 4) {
734 char source_copy[strlen(source) + 1];
735 memcpy(source_copy, source, strlen(source) + 1);
736 default_name = basename(source_copy);
738 name = (argc >= 3) ? argv[2] : default_name;
739 desc = (argc >= 4) ? argv[3] : NULL;
742 config_str = file_get_contents(config_file, &config_len);
748 ret = wimlib_open_wim(wimfile, open_flags, &w,
749 imagex_progress_func);
751 ret = wimlib_create_new_wim(compression_type, &w);
757 ret = stat(source, &stbuf);
759 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
760 printf("Capturing WIM image from NTFS filesystem on `%s'\n",
762 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
765 if (errno != ENOENT) {
766 imagex_error_with_errno("Failed to stat `%s'", source);
772 ret = wimlib_add_image(w, source, name, config_str, config_len,
773 add_image_flags, imagex_progress_func);
777 cur_image = wimlib_get_num_images(w);
779 ret = wimlib_set_image_descripton(w, cur_image, desc);
784 ret = wimlib_set_image_flags(w, cur_image, flags_element);
789 ret = wimlib_overwrite(w, write_flags, num_threads,
790 imagex_progress_func);
792 ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
793 num_threads, imagex_progress_func);
795 if (ret == WIMLIB_ERR_REOPEN)
798 imagex_error("Failed to write the WIM file `%s'", wimfile);
805 /* Remove image(s) from a WIM. */
806 static int imagex_delete(int argc, const char **argv)
812 const char *image_num_or_name;
817 for_opt(c, delete_options) {
820 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
821 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
824 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
836 imagex_error("Must specify a WIM file");
838 imagex_error("Must specify an image");
843 image_num_or_name = argv[1];
845 ret = file_writable(wimfile);
849 ret = wimlib_open_wim(wimfile, open_flags, &w,
850 imagex_progress_func);
854 image = wimlib_resolve_image(w, image_num_or_name);
856 ret = verify_image_exists(image, image_num_or_name, wimfile);
860 ret = wimlib_delete_image(w, image);
862 imagex_error("Failed to delete image from `%s'", wimfile);
866 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
867 if (ret == WIMLIB_ERR_REOPEN)
870 imagex_error("Failed to write the file `%s' with image "
878 /* Print the files contained in an image(s) in a WIM file. */
879 static int imagex_dir(int argc, const char **argv)
888 imagex_error("Must specify a WIM file");
893 imagex_error("Too many arguments");
899 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w,
900 imagex_progress_func);
905 image = wimlib_resolve_image(w, argv[2]);
906 ret = verify_image_exists(image, argv[2], wimfile);
910 /* Image was not specified. If the WIM only contains one image,
911 * choose that one; otherwise, print an error. */
912 num_images = wimlib_get_num_images(w);
913 if (num_images != 1) {
914 imagex_error("The file `%s' contains %d images; Please "
915 "select one.", wimfile, num_images);
923 ret = wimlib_print_files(w, image);
929 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
931 static int imagex_export(int argc, const char **argv)
935 int export_flags = 0;
937 int compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
938 bool compression_type_specified = false;
939 const char *src_wimfile;
940 const char *src_image_num_or_name;
941 const char *dest_wimfile;
942 const char *dest_name;
943 const char *dest_desc;
944 WIMStruct *src_w = NULL;
945 WIMStruct *dest_w = NULL;
950 const char *swm_glob = NULL;
951 WIMStruct **additional_swms = NULL;
952 unsigned num_additional_swms = 0;
953 unsigned num_threads = 0;
955 for_opt(c, export_options) {
958 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
961 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
962 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
965 compression_type = get_compression_type(optarg);
966 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
968 compression_type_specified = true;
974 num_threads = parse_num_threads(optarg);
975 if (num_threads == UINT_MAX)
979 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
988 if (argc < 3 || argc > 5) {
992 src_wimfile = argv[0];
993 src_image_num_or_name = argv[1];
994 dest_wimfile = argv[2];
995 dest_name = (argc >= 4) ? argv[3] : NULL;
996 dest_desc = (argc >= 5) ? argv[4] : NULL;
997 ret = wimlib_open_wim(src_wimfile,
998 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w,
999 imagex_progress_func);
1003 /* Determine if the destination is an existing file or not.
1004 * If so, we try to append the exported image(s) to it; otherwise, we
1005 * create a new WIM containing the exported image(s). */
1006 if (stat(dest_wimfile, &stbuf) == 0) {
1010 /* Destination file exists. */
1011 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
1012 imagex_error("`%s' is not a regular file",
1017 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w,
1018 imagex_progress_func);
1022 ret = file_writable(dest_wimfile);
1026 dest_ctype = wimlib_get_compression_type(dest_w);
1027 if (compression_type_specified
1028 && compression_type != dest_ctype)
1030 imagex_error("Cannot specify a compression type that is "
1031 "not the same as that used in the "
1036 compression_type = dest_ctype;
1039 /* dest_wimfile is not an existing file, so create a new WIM. */
1040 if (!compression_type_specified)
1041 compression_type = wimlib_get_compression_type(src_w);
1042 if (errno == ENOENT) {
1043 ret = wimlib_create_new_wim(compression_type, &dest_w);
1047 imagex_error_with_errno("Cannot stat file `%s'",
1054 image = wimlib_resolve_image(src_w, src_image_num_or_name);
1055 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
1060 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
1062 &num_additional_swms);
1067 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
1068 export_flags, additional_swms,
1069 num_additional_swms, imagex_progress_func);
1075 ret = wimlib_write(dest_w, dest_wimfile, WIMLIB_ALL_IMAGES,
1076 write_flags, num_threads,
1077 imagex_progress_func);
1079 ret = wimlib_overwrite(dest_w, write_flags, num_threads,
1080 imagex_progress_func);
1082 if (ret == WIMLIB_ERR_REOPEN)
1085 wimlib_free(dest_w);
1086 if (additional_swms) {
1087 for (unsigned i = 0; i < num_additional_swms; i++)
1088 wimlib_free(additional_swms[i]);
1089 free(additional_swms);
1094 /* Prints information about a WIM file; also can mark an image as bootable,
1095 * change the name of an image, or change the description of an image. */
1096 static int imagex_info(int argc, const char **argv)
1101 bool header = false;
1102 bool lookup_table = false;
1104 bool metadata = false;
1105 bool short_header = true;
1106 const char *xml_out_file = NULL;
1107 const char *wimfile;
1108 const char *image_num_or_name = "all";
1109 const char *new_name = NULL;
1110 const char *new_desc = NULL;
1115 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1120 for_opt(c, info_options) {
1130 short_header = false;
1133 lookup_table = true;
1134 short_header = false;
1138 short_header = false;
1141 xml_out_file = optarg;
1142 short_header = false;
1146 short_header = false;
1156 if (argc == 0 || argc > 4) {
1162 image_num_or_name = argv[1];
1172 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1174 ret = wimlib_open_wim(wimfile, open_flags, &w,
1175 imagex_progress_func);
1179 part_number = wimlib_get_part_number(w, &total_parts);
1181 image = wimlib_resolve_image(w, image_num_or_name);
1182 if (image == WIMLIB_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
1183 imagex_error("The image `%s' does not exist",
1186 imagex_error("If you would like to set the boot "
1187 "index to 0, specify image \"0\" with "
1188 "the --boot flag.");
1189 ret = WIMLIB_ERR_INVALID_IMAGE;
1193 num_images = wimlib_get_num_images(w);
1195 if (num_images == 0) {
1197 imagex_error("--boot is meaningless on a WIM with no "
1199 ret = WIMLIB_ERR_INVALID_IMAGE;
1204 if (image == WIMLIB_ALL_IMAGES && num_images > 1) {
1206 imagex_error("Cannot specify the --boot flag "
1207 "without specifying a specific "
1208 "image in a multi-image WIM");
1209 ret = WIMLIB_ERR_INVALID_IMAGE;
1213 imagex_error("Cannot specify the NEW_NAME "
1214 "without specifying a specific "
1215 "image in a multi-image WIM");
1216 ret = WIMLIB_ERR_INVALID_IMAGE;
1221 /* Operations that print information are separated from operations that
1222 * recreate the WIM file. */
1223 if (!new_name && !boot) {
1225 /* Read-only operations */
1227 if (image == WIMLIB_NO_IMAGE) {
1228 imagex_error("`%s' is not a valid image",
1230 ret = WIMLIB_ERR_INVALID_IMAGE;
1234 if (image == WIMLIB_ALL_IMAGES && short_header)
1235 wimlib_print_wim_information(w);
1238 wimlib_print_header(w);
1241 if (total_parts != 1) {
1242 printf("Warning: Only showing the lookup table "
1243 "for part %d of a %d-part WIM.\n",
1244 part_number, total_parts);
1246 wimlib_print_lookup_table(w);
1250 ret = wimlib_extract_xml_data(w, stdout);
1256 fp = fopen(xml_out_file, "wb");
1258 imagex_error_with_errno("Failed to open the "
1265 ret = wimlib_extract_xml_data(w, fp);
1266 if (fclose(fp) != 0) {
1267 imagex_error("Failed to close the file `%s'",
1277 wimlib_print_available_images(w, image);
1280 ret = wimlib_print_metadata(w, image);
1286 /* Modification operations */
1287 if (total_parts != 1) {
1288 imagex_error("Modifying a split WIM is not supported.");
1292 if (image == WIMLIB_ALL_IMAGES)
1295 if (image == WIMLIB_NO_IMAGE && new_name) {
1296 imagex_error("Cannot specify new_name (`%s') when "
1297 "using image 0", new_name);
1303 if (image == wimlib_get_boot_idx(w)) {
1304 printf("Image %d is already marked as "
1305 "bootable.\n", image);
1308 printf("Marking image %d as bootable.\n",
1310 wimlib_set_boot_idx(w, image);
1314 if (strcmp(wimlib_get_image_name(w, image),
1316 printf("Image %d is already named \"%s\".\n",
1320 printf("Changing the name of image %d to "
1321 "\"%s\".\n", image, new_name);
1322 ret = wimlib_set_image_name(w, image, new_name);
1328 const char *old_desc;
1329 old_desc = wimlib_get_image_description(w, image);
1330 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1331 printf("The description of image %d is already "
1332 "\"%s\".\n", image, new_desc);
1335 printf("Changing the description of image %d "
1336 "to \"%s\".\n", image, new_desc);
1337 ret = wimlib_set_image_descripton(w, image,
1344 /* Only call wimlib_overwrite() if something actually needs to
1346 if (boot || new_name || new_desc ||
1347 (check && !wimlib_has_integrity_table(w)))
1351 ret = file_writable(wimfile);
1356 write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1360 ret = wimlib_overwrite(w, write_flags, 1,
1361 imagex_progress_func);
1362 if (ret == WIMLIB_ERR_REOPEN)
1365 printf("The file `%s' was not modified because nothing "
1366 "needed to be done.\n", wimfile);
1375 /* Join split WIMs into one part WIM */
1376 static int imagex_join(int argc, const char **argv)
1379 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1380 int wim_write_flags = 0;
1381 const char *output_path;
1383 for_opt(c, join_options) {
1386 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1387 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1397 imagex_error("Must specify one or more split WIM (.swm) parts "
1401 output_path = argv[0];
1402 return wimlib_join(++argv, --argc, output_path, swm_open_flags,
1403 wim_write_flags, imagex_progress_func);
1409 /* Mounts an image using a FUSE mount. */
1410 static int imagex_mount_rw_or_ro(int argc, const char **argv)
1413 int mount_flags = 0;
1414 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1415 const char *wimfile;
1421 const char *swm_glob = NULL;
1422 WIMStruct **additional_swms = NULL;
1423 unsigned num_additional_swms = 0;
1424 const char *staging_dir = NULL;
1426 if (strcmp(argv[0], "mountrw") == 0)
1427 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1429 for_opt(c, mount_options) {
1432 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
1435 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1438 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1441 if (strcasecmp(optarg, "none") == 0)
1442 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1443 else if (strcasecmp(optarg, "xattr") == 0)
1444 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1445 else if (strcasecmp(optarg, "windows") == 0)
1446 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1448 imagex_error("Unknown stream interface \"%s\"", optarg);
1456 staging_dir = optarg;
1459 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
1467 if (argc != 2 && argc != 3)
1472 ret = wimlib_open_wim(wimfile, open_flags, &w,
1473 imagex_progress_func);
1478 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1480 &num_additional_swms);
1487 num_images = wimlib_get_num_images(w);
1488 if (num_images != 1) {
1489 imagex_error("The file `%s' contains %d images; Please "
1490 "select one.", wimfile, num_images);
1491 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1498 image = wimlib_resolve_image(w, argv[1]);
1500 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
1505 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1506 ret = file_writable(wimfile);
1511 ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
1512 num_additional_swms, staging_dir);
1514 imagex_error("Failed to mount image %d from `%s' on `%s'",
1515 image, wimfile, dir);
1520 if (additional_swms) {
1521 for (unsigned i = 0; i < num_additional_swms; i++)
1522 wimlib_free(additional_swms[i]);
1523 free(additional_swms);
1527 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1532 static int imagex_optimize(int argc, const char **argv)
1536 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
1539 const char *wimfile;
1543 for_opt(c, optimize_options) {
1546 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1547 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1550 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1567 ret = wimlib_open_wim(wimfile, open_flags, &w,
1568 imagex_progress_func);
1572 old_size = file_get_size(argv[0]);
1573 printf("`%s' original size: ", wimfile);
1577 printf("%"PRIu64" KiB\n", old_size >> 10);
1579 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1582 new_size = file_get_size(argv[0]);
1583 printf("`%s' optimized size: ", wimfile);
1587 printf("%"PRIu64" KiB\n", new_size >> 10);
1589 fputs("Space saved: ", stdout);
1590 if (new_size != -1 && old_size != -1) {
1591 printf("%lld KiB\n",
1592 ((long long)old_size - (long long)new_size) >> 10);
1602 /* Split a WIM into a spanned set */
1603 static int imagex_split(int argc, const char **argv)
1606 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1607 int write_flags = 0;
1608 unsigned long part_size;
1613 for_opt(c, split_options) {
1616 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1617 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1631 part_size = strtod(argv[2], &tmp) * (1 << 20);
1632 if (tmp == argv[2] || *tmp) {
1633 imagex_error("Invalid part size \"%s\"", argv[2]);
1634 imagex_error("The part size must be an integer or floating-point number of megabytes.");
1637 ret = wimlib_open_wim(argv[0], open_flags, &w, imagex_progress_func);
1640 ret = wimlib_split(w, argv[1], part_size, write_flags, imagex_progress_func);
1645 /* Unmounts an image. */
1646 static int imagex_unmount(int argc, const char **argv)
1649 int unmount_flags = 0;
1652 for_opt(c, unmount_options) {
1655 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1658 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1661 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
1675 ret = wimlib_unmount_image(argv[0], unmount_flags,
1676 imagex_progress_func);
1678 imagex_error("Failed to unmount `%s'", argv[0]);
1682 struct imagex_command {
1684 int (*func)(int , const char **);
1689 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1690 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1692 static const struct imagex_command imagex_commands[] = {
1693 {"append", imagex_capture_or_append, APPEND},
1694 {"apply", imagex_apply, APPLY},
1695 {"capture", imagex_capture_or_append, CAPTURE},
1696 {"delete", imagex_delete, DELETE},
1697 {"dir", imagex_dir, DIR},
1698 {"export", imagex_export, EXPORT},
1699 {"info", imagex_info, INFO},
1700 {"join", imagex_join, JOIN},
1701 {"mount", imagex_mount_rw_or_ro, MOUNT},
1702 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1703 {"optimize",imagex_optimize, OPTIMIZE},
1704 {"split", imagex_split, SPLIT},
1705 {"unmount", imagex_unmount, UNMOUNT},
1708 static void version()
1710 static const char *s =
1711 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
1712 "Copyright (C) 2012 Eric Biggers\n"
1713 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
1714 "This is free software: you are free to change and redistribute it.\n"
1715 "There is NO WARRANTY, to the extent permitted by law.\n"
1717 "Report bugs to "PACKAGE_BUGREPORT".\n";
1722 static void help_or_version(int argc, const char **argv)
1726 const struct imagex_command *cmd;
1728 for (i = 1; i < argc; i++) {
1736 if (strcmp(p, "help") == 0) {
1737 for_imagex_command(cmd) {
1738 if (strcmp(cmd->name, argv[1]) == 0) {
1746 if (strcmp(p, "version") == 0) {
1754 static void usage(int cmd_type)
1756 const struct imagex_command *cmd;
1757 printf("Usage: %s", usage_strings[cmd_type]);
1758 for_imagex_command(cmd) {
1759 if (cmd->cmd == cmd_type)
1760 printf("\nTry `man imagex-%s' for more details.\n",
1765 static void usage_all()
1767 puts("IMAGEX: Usage:");
1768 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
1769 printf(" %s", usage_strings[i]);
1770 static const char *extra =
1772 " imagex --version\n"
1774 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
1776 " Try `man imagex' for more information.\n"
1778 fputs(extra, stdout);
1782 int main(int argc, const char **argv)
1784 const struct imagex_command *cmd;
1788 imagex_error("No command specified");
1793 help_or_version(argc, argv);
1797 wimlib_set_print_errors(true);
1798 ret = wimlib_global_init();
1802 for_imagex_command(cmd) {
1803 if (strcmp(cmd->name, *argv) == 0) {
1804 ret = cmd->func(argc, argv);
1809 imagex_error("Unrecognized command: `%s'", argv[0]);
1814 imagex_error("Exiting with error code %d:\n"
1816 wimlib_get_error_string(ret));
1817 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
1818 imagex_error_with_errno("errno");
1820 wimlib_global_cleanup();