4 * Use wimlib to create, modify, extract, mount, unmount, or display information
9 * Copyright (C) 2012, 2013 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/>.
46 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
48 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (char**)argv, "", \
67 static void usage(int cmd_type);
68 static void usage_all();
70 static const char *usage_strings[] = {
72 "imagex append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
73 " [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
74 " [--verbose] [--dereference] [--config=FILE]\n"
75 " [--threads=NUM_THREADS] [--rebuild] [--unix-data]\n"
78 "imagex apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
79 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
80 " [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n",
82 "imagex capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
83 " [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
84 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
85 " [--config=FILE] [--threads=NUM_THREADS] [--unix-data]\n"
88 "imagex delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check] [--soft]\n",
90 "imagex dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n",
92 "imagex export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
93 " DEST_WIMFILE [DEST_IMAGE_NAME] [DEST_IMAGE_DESCRIPTION]\n"
94 " [--boot] [--check] [--compress=TYPE] [--ref=\"GLOB\"]\n"
95 " [--threads=NUM_THREADS] [--rebuild]\n",
97 "imagex info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
98 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
99 " [--xml] [--extract-xml FILE] [--metadata]\n",
101 "imagex join [--check] WIMFILE SPLIT_WIM...\n",
103 "imagex mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
104 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
105 " [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n",
107 "imagex mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
108 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
109 " [--staging-dir=DIR] [--unix-data] [--allow-other]\n",
111 "imagex optimize WIMFILE [--check] [--recompress] [--compress=TYPE]\n",
113 "imagex split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n",
115 "imagex unmount DIRECTORY [--commit] [--check] [--rebuild]\n",
118 static const struct option common_options[] = {
119 {"help", 0, NULL, 'h'},
120 {"version", 0, NULL, 'v'},
124 static const struct option apply_options[] = {
125 {"check", no_argument, NULL, 'c'},
126 {"hardlink", no_argument, NULL, 'h'},
127 {"symlink", no_argument, NULL, 's'},
128 {"verbose", no_argument, NULL, 'v'},
129 {"ref", required_argument, NULL, 'r'},
130 {"unix-data", no_argument, NULL, 'U'},
133 static const struct option capture_or_append_options[] = {
134 {"boot", no_argument, NULL, 'b'},
135 {"check", no_argument, NULL, 'c'},
136 {"compress", required_argument, NULL, 'x'},
137 {"config", required_argument, NULL, 'C'},
138 {"dereference", no_argument, NULL, 'L'},
139 {"flags", required_argument, NULL, 'f'},
140 {"verbose", no_argument, NULL, 'v'},
141 {"threads", required_argument, NULL, 't'},
142 {"rebuild", no_argument, NULL, 'R'},
143 {"unix-data", no_argument, NULL, 'U'},
144 {"source-list", no_argument, NULL, 'S'},
147 static const struct option delete_options[] = {
148 {"check", no_argument, NULL, 'c'},
149 {"soft", no_argument, NULL, 's'},
153 static const struct option export_options[] = {
154 {"boot", no_argument, NULL, 'b'},
155 {"check", no_argument, NULL, 'c'},
156 {"compress", required_argument, NULL, 'x'},
157 {"ref", required_argument, NULL, 'r'},
158 {"threads", required_argument, NULL, 't'},
159 {"rebuild", no_argument, NULL, 'R'},
163 static const struct option info_options[] = {
164 {"boot", no_argument, NULL, 'b'},
165 {"check", no_argument, NULL, 'c'},
166 {"extract-xml", required_argument, NULL, 'X'},
167 {"header", no_argument, NULL, 'h'},
168 {"lookup-table", no_argument, NULL, 'l'},
169 {"metadata", no_argument, NULL, 'm'},
170 {"xml", no_argument, NULL, 'x'},
174 static const struct option join_options[] = {
175 {"check", no_argument, NULL, 'c'},
179 static const struct option mount_options[] = {
180 {"check", no_argument, NULL, 'c'},
181 {"debug", no_argument, NULL, 'd'},
182 {"streams-interface", required_argument, NULL, 's'},
183 {"ref", required_argument, NULL, 'r'},
184 {"staging-dir", required_argument, NULL, 'D'},
185 {"unix-data", no_argument, NULL, 'U'},
186 {"allow-other", no_argument, NULL, 'A'},
190 static const struct option optimize_options[] = {
191 {"check", no_argument, NULL, 'c'},
192 {"recompress", no_argument, NULL, 'r'},
196 static const struct option split_options[] = {
197 {"check", no_argument, NULL, 'c'},
201 static const struct option unmount_options[] = {
202 {"commit", no_argument, NULL, 'c'},
203 {"check", no_argument, NULL, 'C'},
204 {"rebuild", no_argument, NULL, 'R'},
210 /* Print formatted error message to stderr. */
211 static void imagex_error(const char *format, ...)
214 va_start(va, format);
215 fputs("ERROR: ", stderr);
216 vfprintf(stderr, format, va);
221 /* Print formatted error message to stderr. */
222 static void imagex_error_with_errno(const char *format, ...)
224 int errno_save = errno;
226 va_start(va, format);
227 fputs("ERROR: ", stderr);
228 vfprintf(stderr, format, va);
229 fprintf(stderr, ": %s\n", strerror(errno_save));
233 static int verify_image_exists(int image, const char *image_name,
234 const char *wim_name)
236 if (image == WIMLIB_NO_IMAGE) {
237 imagex_error("\"%s\" is not a valid image in `%s'!\n"
238 " Please specify a 1-based imagex index or "
240 " You may use `imagex info' to list the images "
241 "contained in a WIM.",
242 image_name, wim_name);
248 static int verify_image_is_single(int image)
250 if (image == WIMLIB_ALL_IMAGES) {
251 imagex_error("Cannot specify all images for this action!");
257 static int verify_image_exists_and_is_single(int image, const char *image_name,
258 const char *wim_name)
261 ret = verify_image_exists(image, image_name, wim_name);
263 ret = verify_image_is_single(image);
267 static int get_compression_type(const char *optarg)
269 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
270 return WIMLIB_COMPRESSION_TYPE_LZX;
271 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
272 return WIMLIB_COMPRESSION_TYPE_XPRESS;
273 else if (strcasecmp(optarg, "none") == 0)
274 return WIMLIB_COMPRESSION_TYPE_NONE;
276 imagex_error("Invalid compression type `%s'! Must be "
277 "\"maximum\", \"fast\", or \"none\".", optarg);
278 return WIMLIB_COMPRESSION_TYPE_INVALID;
282 static off_t file_get_size(const char *filename)
285 if (stat(filename, &st) == 0)
291 static const char *default_capture_config =
296 "\\System Volume Information\n"
300 "[CompressionExclusionList]\n"
304 "\\WINDOWS\\inf\\*.pnf\n";
306 static char *stdin_get_contents(size_t *len_ret)
309 size_t newlen = 1024;
313 char *p = realloc(buf, newlen);
314 size_t bytes_read, bytes_to_read;
316 imagex_error("out of memory while reading stdin");
320 bytes_to_read = newlen - pos;
321 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
323 if (bytes_read != bytes_to_read) {
328 imagex_error_with_errno("error reading stdin");
341 PARSE_FILENAME_SUCCESS = 0,
342 PARSE_FILENAME_FAILURE = 1,
343 PARSE_FILENAME_NONE = 2,
346 static int parse_filename(char **fn_ret, char **line_p, size_t *len_p)
349 char *line = *line_p;
354 /* Skip leading whitespace */
357 return PARSE_FILENAME_NONE;
358 if (!isspace(*line) && *line != '\0')
364 if (quote_char == '"' || quote_char == '\'') {
365 /* Quoted filename */
369 line = memchr(line, quote_char, len);
373 ret = PARSE_FILENAME_SUCCESS;
375 imagex_error("Missing closing quote: %s", fn - 1);
376 return PARSE_FILENAME_FAILURE;
379 /* Unquoted filename. Go until whitespace. Line is terminated
380 * by '\0', so no need to check 'len'. */
385 } while (!isspace(*line) && *line != '\0');
387 ret = PARSE_FILENAME_SUCCESS;
396 parse_source_list_line(char *line, size_t len,
397 struct wimlib_capture_source *source)
400 ret = parse_filename(&source->fs_source_path, &line, &len);
401 if (ret != PARSE_FILENAME_SUCCESS)
403 ret = parse_filename(&source->wim_target_path, &line, &len);
404 if (ret == PARSE_FILENAME_NONE)
405 source->wim_target_path = source->fs_source_path;
406 return ret != PARSE_FILENAME_FAILURE;
409 static bool is_comment_line(const char *line, size_t len)
414 if (!isspace(*line) && *line != '\0')
423 static struct wimlib_capture_source *
424 parse_source_list(char *source_list_contents, size_t source_list_nbytes,
425 size_t *nsources_ret)
429 struct wimlib_capture_source *sources;
433 for (i = 0; i < source_list_nbytes; i++)
434 if (source_list_contents[i] == '\n')
436 sources = calloc(nlines, sizeof(*sources));
438 imagex_error("out of memory");
441 p = source_list_contents;
443 for (i = 0; i < nlines; i++) {
444 char *endp = strchr(p, '\n');
445 size_t len = endp - p + 1;
447 if (!is_comment_line(p, len)) {
448 if (!parse_source_list_line(p, len, &sources[j++])) {
460 static char *file_get_contents(const char *filename, size_t *len_ret)
467 if (stat(filename, &stbuf) != 0) {
468 imagex_error_with_errno("Failed to stat the file `%s'", filename);
473 fp = fopen(filename, "rb");
475 imagex_error_with_errno("Failed to open the file `%s'", filename);
481 imagex_error("Failed to allocate buffer of %zu bytes to hold "
482 "contents of file `%s'", len, filename);
485 if (fread(buf, 1, len, fp) != len) {
486 imagex_error_with_errno("Failed to read %lu bytes from the "
487 "file `%s'", len, filename);
501 static int file_writable(const char *path)
504 ret = access(path, W_OK);
506 imagex_error_with_errno("Can't modify `%s'", path);
510 #define TO_PERCENT(numerator, denominator) \
511 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
513 static const char *get_data_type(int ctype)
516 case WIMLIB_COMPRESSION_TYPE_NONE:
517 return "uncompressed";
518 case WIMLIB_COMPRESSION_TYPE_LZX:
519 return "LZX-compressed";
520 case WIMLIB_COMPRESSION_TYPE_XPRESS:
521 return "XPRESS-compressed";
526 static int imagex_progress_func(enum wimlib_progress_msg msg,
527 const union wimlib_progress_info *info)
529 unsigned percent_done;
531 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
532 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
533 info->write_streams.total_bytes);
534 if (info->write_streams.completed_streams == 0) {
535 const char *data_type;
537 data_type = get_data_type(info->write_streams.compression_type);
538 printf("Writing %s data using %u thread%s\n",
539 data_type, info->write_streams.num_threads,
540 (info->write_streams.num_threads == 1) ? "" : "s");
542 printf("\r%"PRIu64" MiB of %"PRIu64" MiB (uncompressed) "
543 "written (%u%% done)",
544 info->write_streams.completed_bytes >> 20,
545 info->write_streams.total_bytes >> 20,
547 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
550 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
551 printf("Scanning `%s'", info->scan.source);
552 if (*info->scan.wim_target_path) {
553 printf(" (loading as WIM path: `/%s')...\n",
554 info->scan.wim_target_path);
556 printf(" (loading as root of WIM image)...\n");
559 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
560 if (info->scan.excluded)
561 printf("Excluding `%s' from capture\n", info->scan.cur_path);
563 printf("Scanning `%s'\n", info->scan.cur_path);
565 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
567 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
568 percent_done = TO_PERCENT(info->integrity.completed_bytes,
569 info->integrity.total_bytes);
570 printf("\rVerifying integrity of `%s': %"PRIu64" MiB "
571 "of %"PRIu64" MiB (%u%%) done",
572 info->integrity.filename,
573 info->integrity.completed_bytes >> 20,
574 info->integrity.total_bytes >> 20,
576 if (info->integrity.completed_bytes == info->integrity.total_bytes)
579 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
580 percent_done = TO_PERCENT(info->integrity.completed_bytes,
581 info->integrity.total_bytes);
582 printf("\rCalculating integrity table for WIM: %"PRIu64" MiB "
583 "of %"PRIu64" MiB (%u%%) done",
584 info->integrity.completed_bytes >> 20,
585 info->integrity.total_bytes >> 20,
587 if (info->integrity.completed_bytes == info->integrity.total_bytes)
590 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
591 printf("Applying image %d (%s) from `%s' to %s `%s'\n",
593 info->extract.image_name,
594 info->extract.wimfile_name,
595 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
596 "NTFS volume" : "directory"),
597 info->extract.target);
599 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
600 /*printf("Applying directory structure to %s\n",*/
601 /*info->extract.target);*/
603 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
604 percent_done = TO_PERCENT(info->extract.completed_bytes,
605 info->extract.total_bytes);
606 printf("\rExtracting files: "
607 "%"PRIu64" MiB of %"PRIu64" MiB (%u%%) done",
608 info->extract.completed_bytes >> 20,
609 info->extract.total_bytes >> 20,
611 if (info->extract.completed_bytes >= info->extract.total_bytes)
614 case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
615 puts(info->extract.cur_path);
617 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
618 printf("Setting timestamps on all extracted files...\n");
620 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
621 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
622 printf("Unmounting NTFS volume `%s'...\n",
623 info->extract.target);
626 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
627 percent_done = TO_PERCENT(info->join.completed_bytes,
628 info->join.total_bytes);
629 printf("Writing resources from part %u of %u: "
630 "%"PRIu64 " MiB of %"PRIu64" MiB (%u%%) written\n",
631 (info->join.completed_parts == info->join.total_parts) ?
632 info->join.completed_parts : info->join.completed_parts + 1,
633 info->join.total_parts,
634 info->join.completed_bytes >> 20,
635 info->join.total_bytes >> 20,
638 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
639 percent_done = TO_PERCENT(info->split.completed_bytes,
640 info->split.total_bytes);
641 printf("Writing `%s': %"PRIu64" MiB of %"PRIu64" MiB (%u%%) written\n",
642 info->split.part_name,
643 info->split.completed_bytes >> 20,
644 info->split.total_bytes >> 20,
647 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
648 if (info->split.completed_bytes == info->split.total_bytes) {
649 printf("Finished writing %u split WIM parts\n",
650 info->split.cur_part_number);
661 static int open_swms_from_glob(const char *swm_glob,
662 const char *first_part,
664 WIMStruct ***additional_swms_ret,
665 unsigned *num_additional_swms_ret)
667 unsigned num_additional_swms = 0;
668 WIMStruct **additional_swms = NULL;
672 ret = glob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
674 if (ret == GLOB_NOMATCH) {
675 imagex_error("Found no files for glob \"%s\"",
678 imagex_error_with_errno("Failed to process glob "
684 num_additional_swms = globbuf.gl_pathc;
685 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
686 if (!additional_swms) {
687 imagex_error("Out of memory");
692 for (unsigned i = 0; i < num_additional_swms; i++) {
693 if (strcmp(globbuf.gl_pathv[i], first_part) == 0) {
697 ret = wimlib_open_wim(globbuf.gl_pathv[i],
698 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
699 &additional_swms[i - offset],
700 imagex_progress_func);
704 *additional_swms_ret = additional_swms;
705 *num_additional_swms_ret = num_additional_swms - offset;
709 for (unsigned i = 0; i < num_additional_swms; i++)
710 wimlib_free(additional_swms[i]);
711 free(additional_swms);
719 static unsigned parse_num_threads(const char *optarg)
722 unsigned nthreads = strtoul(optarg, &tmp, 10);
723 if (nthreads == UINT_MAX || *tmp || tmp == optarg) {
724 imagex_error("Number of threads must be a non-negative integer!");
732 /* Extract one image, or all images, from a WIM file into a directory. */
733 static int imagex_apply(int argc, char **argv)
736 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
743 const char *image_num_or_name;
744 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
746 const char *swm_glob = NULL;
747 WIMStruct **additional_swms = NULL;
748 unsigned num_additional_swms = 0;
750 for_opt(c, apply_options) {
753 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
756 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
759 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
762 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
768 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
777 if (argc != 2 && argc != 3) {
784 image_num_or_name = "1";
787 image_num_or_name = argv[1];
791 ret = wimlib_open_wim(wimfile, open_flags, &w, imagex_progress_func);
795 image = wimlib_resolve_image(w, image_num_or_name);
796 ret = verify_image_exists(image, image_num_or_name, wimfile);
800 num_images = wimlib_get_num_images(w);
801 if (argc == 2 && num_images != 1) {
802 imagex_error("`%s' contains %d images; Please select one "
803 "(or all)", wimfile, num_images);
810 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
812 &num_additional_swms);
819 ret = stat(target, &stbuf);
821 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
822 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
824 if (errno != ENOENT) {
825 imagex_error_with_errno("Failed to stat `%s'", target);
831 ret = wimlib_extract_image(w, image, target, extract_flags,
832 additional_swms, num_additional_swms,
833 imagex_progress_func);
835 printf("Done applying WIM image.\n");
838 if (additional_swms) {
839 for (unsigned i = 0; i < num_additional_swms; i++)
840 wimlib_free(additional_swms[i]);
841 free(additional_swms);
846 static int imagex_capture_or_append(int argc, char **argv)
850 int add_image_flags = 0;
852 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
856 const char *flags_element = NULL;
860 int cmd = strcmp(argv[0], "append") ? CAPTURE : APPEND;
861 unsigned num_threads = 0;
864 size_t source_name_len;
867 const char *config_file = NULL;
868 char *config_str = NULL;
869 size_t config_len = 0;
871 bool source_list = false;
872 size_t source_list_nbytes;
873 char *source_list_contents = NULL;
874 bool capture_sources_malloced = false;
875 struct wimlib_capture_source *capture_sources;
878 for_opt(c, capture_or_append_options) {
881 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
884 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
885 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
888 config_file = optarg;
891 compression_type = get_compression_type(optarg);
892 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
896 flags_element = optarg;
899 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
902 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
905 num_threads = parse_num_threads(optarg);
906 if (num_threads == UINT_MAX)
910 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
913 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
926 if (argc < 2 || argc > 4) {
937 source_name_len = strlen(source);
938 source_copy = alloca(source_name_len + 1);
939 name = basename(strcpy(source_copy, source));
941 desc = (argc >= 4) ? argv[3] : NULL;
944 if (source[0] == '-' && source[1] == '\0') {
945 source_list_contents = stdin_get_contents(&source_list_nbytes);
947 source_list_contents = file_get_contents(source,
948 &source_list_nbytes);
950 if (!source_list_contents)
953 capture_sources = parse_source_list(source_list_contents,
956 if (!capture_sources) {
960 capture_sources_malloced = true;
962 capture_sources = alloca(sizeof(struct wimlib_capture_source));
963 capture_sources[0].fs_source_path = source;
964 capture_sources[0].wim_target_path = NULL;
965 capture_sources[0].reserved = 0;
970 config_str = file_get_contents(config_file, &config_len);
978 ret = wimlib_open_wim(wimfile, open_flags, &w,
979 imagex_progress_func);
981 ret = wimlib_create_new_wim(compression_type, &w);
987 ret = stat(source, &stbuf);
989 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
990 printf("Capturing WIM image from NTFS filesystem on `%s'\n",
992 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
995 if (errno != ENOENT) {
996 imagex_error_with_errno("Failed to stat `%s'", source);
1003 ret = wimlib_add_image_multisource(w, capture_sources,
1005 (config_str ? config_str :
1006 default_capture_config),
1007 (config_str ? config_len :
1008 strlen(default_capture_config)),
1010 imagex_progress_func);
1013 cur_image = wimlib_get_num_images(w);
1015 ret = wimlib_set_image_descripton(w, cur_image, desc);
1019 if (flags_element) {
1020 ret = wimlib_set_image_flags(w, cur_image, flags_element);
1024 if (cmd == APPEND) {
1025 ret = wimlib_overwrite(w, write_flags, num_threads,
1026 imagex_progress_func);
1028 ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
1029 num_threads, imagex_progress_func);
1031 if (ret == WIMLIB_ERR_REOPEN)
1034 imagex_error("Failed to write the WIM file `%s'", wimfile);
1038 free(source_list_contents);
1039 if (capture_sources_malloced)
1040 free(capture_sources);
1044 /* Remove image(s) from a WIM. */
1045 static int imagex_delete(int argc, char **argv)
1049 int write_flags = 0;
1050 const char *wimfile;
1051 const char *image_num_or_name;
1056 for_opt(c, delete_options) {
1059 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1060 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1063 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
1075 imagex_error("Must specify a WIM file");
1077 imagex_error("Must specify an image");
1082 image_num_or_name = argv[1];
1084 ret = file_writable(wimfile);
1088 ret = wimlib_open_wim(wimfile, open_flags, &w,
1089 imagex_progress_func);
1093 image = wimlib_resolve_image(w, image_num_or_name);
1095 ret = verify_image_exists(image, image_num_or_name, wimfile);
1099 ret = wimlib_delete_image(w, image);
1101 imagex_error("Failed to delete image from `%s'", wimfile);
1105 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1106 if (ret == WIMLIB_ERR_REOPEN)
1109 imagex_error("Failed to write the file `%s' with image "
1110 "deleted", wimfile);
1117 /* Print the files contained in an image(s) in a WIM file. */
1118 static int imagex_dir(int argc, char **argv)
1120 const char *wimfile;
1127 imagex_error("Must specify a WIM file");
1132 imagex_error("Too many arguments");
1138 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w,
1139 imagex_progress_func);
1144 image = wimlib_resolve_image(w, argv[2]);
1145 ret = verify_image_exists(image, argv[2], wimfile);
1149 /* Image was not specified. If the WIM only contains one image,
1150 * choose that one; otherwise, print an error. */
1151 num_images = wimlib_get_num_images(w);
1152 if (num_images != 1) {
1153 imagex_error("The file `%s' contains %d images; Please "
1154 "select one.", wimfile, num_images);
1162 ret = wimlib_print_files(w, image);
1168 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
1170 static int imagex_export(int argc, char **argv)
1174 int export_flags = 0;
1175 int write_flags = 0;
1176 int compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
1177 bool compression_type_specified = false;
1178 const char *src_wimfile;
1179 const char *src_image_num_or_name;
1180 const char *dest_wimfile;
1181 const char *dest_name;
1182 const char *dest_desc;
1183 WIMStruct *src_w = NULL;
1184 WIMStruct *dest_w = NULL;
1189 const char *swm_glob = NULL;
1190 WIMStruct **additional_swms = NULL;
1191 unsigned num_additional_swms = 0;
1192 unsigned num_threads = 0;
1194 for_opt(c, export_options) {
1197 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
1200 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1201 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1204 compression_type = get_compression_type(optarg);
1205 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1207 compression_type_specified = true;
1213 num_threads = parse_num_threads(optarg);
1214 if (num_threads == UINT_MAX)
1218 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1227 if (argc < 3 || argc > 5) {
1231 src_wimfile = argv[0];
1232 src_image_num_or_name = argv[1];
1233 dest_wimfile = argv[2];
1234 dest_name = (argc >= 4) ? argv[3] : NULL;
1235 dest_desc = (argc >= 5) ? argv[4] : NULL;
1236 ret = wimlib_open_wim(src_wimfile,
1237 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w,
1238 imagex_progress_func);
1242 /* Determine if the destination is an existing file or not.
1243 * If so, we try to append the exported image(s) to it; otherwise, we
1244 * create a new WIM containing the exported image(s). */
1245 if (stat(dest_wimfile, &stbuf) == 0) {
1249 /* Destination file exists. */
1250 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
1251 imagex_error("`%s' is not a regular file",
1256 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w,
1257 imagex_progress_func);
1261 ret = file_writable(dest_wimfile);
1265 dest_ctype = wimlib_get_compression_type(dest_w);
1266 if (compression_type_specified
1267 && compression_type != dest_ctype)
1269 imagex_error("Cannot specify a compression type that is "
1270 "not the same as that used in the "
1277 /* dest_wimfile is not an existing file, so create a new WIM. */
1278 if (!compression_type_specified)
1279 compression_type = wimlib_get_compression_type(src_w);
1280 if (errno == ENOENT) {
1281 ret = wimlib_create_new_wim(compression_type, &dest_w);
1285 imagex_error_with_errno("Cannot stat file `%s'",
1292 image = wimlib_resolve_image(src_w, src_image_num_or_name);
1293 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
1298 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
1300 &num_additional_swms);
1305 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
1306 export_flags, additional_swms,
1307 num_additional_swms, imagex_progress_func);
1313 ret = wimlib_write(dest_w, dest_wimfile, WIMLIB_ALL_IMAGES,
1314 write_flags, num_threads,
1315 imagex_progress_func);
1317 ret = wimlib_overwrite(dest_w, write_flags, num_threads,
1318 imagex_progress_func);
1320 if (ret == WIMLIB_ERR_REOPEN)
1323 wimlib_free(dest_w);
1324 if (additional_swms) {
1325 for (unsigned i = 0; i < num_additional_swms; i++)
1326 wimlib_free(additional_swms[i]);
1327 free(additional_swms);
1332 /* Prints information about a WIM file; also can mark an image as bootable,
1333 * change the name of an image, or change the description of an image. */
1334 static int imagex_info(int argc, char **argv)
1339 bool header = false;
1340 bool lookup_table = false;
1342 bool metadata = false;
1343 bool short_header = true;
1344 const char *xml_out_file = NULL;
1345 const char *wimfile;
1346 const char *image_num_or_name = "all";
1347 const char *new_name = NULL;
1348 const char *new_desc = NULL;
1353 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1358 for_opt(c, info_options) {
1368 short_header = false;
1371 lookup_table = true;
1372 short_header = false;
1376 short_header = false;
1379 xml_out_file = optarg;
1380 short_header = false;
1384 short_header = false;
1394 if (argc == 0 || argc > 4) {
1400 image_num_or_name = argv[1];
1410 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1412 ret = wimlib_open_wim(wimfile, open_flags, &w,
1413 imagex_progress_func);
1417 part_number = wimlib_get_part_number(w, &total_parts);
1419 image = wimlib_resolve_image(w, image_num_or_name);
1420 if (image == WIMLIB_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
1421 imagex_error("The image `%s' does not exist",
1424 imagex_error("If you would like to set the boot "
1425 "index to 0, specify image \"0\" with "
1426 "the --boot flag.");
1427 ret = WIMLIB_ERR_INVALID_IMAGE;
1431 num_images = wimlib_get_num_images(w);
1433 if (num_images == 0) {
1435 imagex_error("--boot is meaningless on a WIM with no "
1437 ret = WIMLIB_ERR_INVALID_IMAGE;
1442 if (image == WIMLIB_ALL_IMAGES && num_images > 1) {
1444 imagex_error("Cannot specify the --boot flag "
1445 "without specifying a specific "
1446 "image in a multi-image WIM");
1447 ret = WIMLIB_ERR_INVALID_IMAGE;
1451 imagex_error("Cannot specify the NEW_NAME "
1452 "without specifying a specific "
1453 "image in a multi-image WIM");
1454 ret = WIMLIB_ERR_INVALID_IMAGE;
1459 /* Operations that print information are separated from operations that
1460 * recreate the WIM file. */
1461 if (!new_name && !boot) {
1463 /* Read-only operations */
1465 if (image == WIMLIB_NO_IMAGE) {
1466 imagex_error("`%s' is not a valid image",
1468 ret = WIMLIB_ERR_INVALID_IMAGE;
1472 if (image == WIMLIB_ALL_IMAGES && short_header)
1473 wimlib_print_wim_information(w);
1476 wimlib_print_header(w);
1479 if (total_parts != 1) {
1480 printf("Warning: Only showing the lookup table "
1481 "for part %d of a %d-part WIM.\n",
1482 part_number, total_parts);
1484 wimlib_print_lookup_table(w);
1488 ret = wimlib_extract_xml_data(w, stdout);
1494 fp = fopen(xml_out_file, "wb");
1496 imagex_error_with_errno("Failed to open the "
1503 ret = wimlib_extract_xml_data(w, fp);
1504 if (fclose(fp) != 0) {
1505 imagex_error("Failed to close the file `%s'",
1515 wimlib_print_available_images(w, image);
1518 ret = wimlib_print_metadata(w, image);
1524 /* Modification operations */
1525 if (total_parts != 1) {
1526 imagex_error("Modifying a split WIM is not supported.");
1530 if (image == WIMLIB_ALL_IMAGES)
1533 if (image == WIMLIB_NO_IMAGE && new_name) {
1534 imagex_error("Cannot specify new_name (`%s') when "
1535 "using image 0", new_name);
1541 if (image == wimlib_get_boot_idx(w)) {
1542 printf("Image %d is already marked as "
1543 "bootable.\n", image);
1546 printf("Marking image %d as bootable.\n",
1548 wimlib_set_boot_idx(w, image);
1552 if (strcmp(wimlib_get_image_name(w, image),
1554 printf("Image %d is already named \"%s\".\n",
1558 printf("Changing the name of image %d to "
1559 "\"%s\".\n", image, new_name);
1560 ret = wimlib_set_image_name(w, image, new_name);
1566 const char *old_desc;
1567 old_desc = wimlib_get_image_description(w, image);
1568 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1569 printf("The description of image %d is already "
1570 "\"%s\".\n", image, new_desc);
1573 printf("Changing the description of image %d "
1574 "to \"%s\".\n", image, new_desc);
1575 ret = wimlib_set_image_descripton(w, image,
1582 /* Only call wimlib_overwrite() if something actually needs to
1584 if (boot || new_name || new_desc ||
1585 (check && !wimlib_has_integrity_table(w)))
1589 ret = file_writable(wimfile);
1594 write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1598 ret = wimlib_overwrite(w, write_flags, 1,
1599 imagex_progress_func);
1600 if (ret == WIMLIB_ERR_REOPEN)
1603 printf("The file `%s' was not modified because nothing "
1604 "needed to be done.\n", wimfile);
1613 /* Join split WIMs into one part WIM */
1614 static int imagex_join(int argc, char **argv)
1617 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1618 int wim_write_flags = 0;
1619 const char *output_path;
1621 for_opt(c, join_options) {
1624 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1625 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1635 imagex_error("Must specify one or more split WIM (.swm) parts "
1639 output_path = argv[0];
1640 return wimlib_join((const char **)++argv, --argc, output_path,
1641 swm_open_flags, wim_write_flags,
1642 imagex_progress_func);
1648 /* Mounts an image using a FUSE mount. */
1649 static int imagex_mount_rw_or_ro(int argc, char **argv)
1652 int mount_flags = 0;
1653 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1654 const char *wimfile;
1660 const char *swm_glob = NULL;
1661 WIMStruct **additional_swms = NULL;
1662 unsigned num_additional_swms = 0;
1663 const char *staging_dir = NULL;
1665 if (strcmp(argv[0], "mountrw") == 0)
1666 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1668 for_opt(c, mount_options) {
1671 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
1674 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1677 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1680 if (strcasecmp(optarg, "none") == 0)
1681 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1682 else if (strcasecmp(optarg, "xattr") == 0)
1683 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1684 else if (strcasecmp(optarg, "windows") == 0)
1685 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1687 imagex_error("Unknown stream interface \"%s\"", optarg);
1695 staging_dir = optarg;
1698 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
1706 if (argc != 2 && argc != 3)
1711 ret = wimlib_open_wim(wimfile, open_flags, &w,
1712 imagex_progress_func);
1717 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1719 &num_additional_swms);
1726 num_images = wimlib_get_num_images(w);
1727 if (num_images != 1) {
1728 imagex_error("The file `%s' contains %d images; Please "
1729 "select one.", wimfile, num_images);
1730 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1737 image = wimlib_resolve_image(w, argv[1]);
1739 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
1744 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1745 ret = file_writable(wimfile);
1750 ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
1751 num_additional_swms, staging_dir);
1753 imagex_error("Failed to mount image %d from `%s' on `%s'",
1754 image, wimfile, dir);
1759 if (additional_swms) {
1760 for (unsigned i = 0; i < num_additional_swms; i++)
1761 wimlib_free(additional_swms[i]);
1762 free(additional_swms);
1766 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1771 static int imagex_optimize(int argc, char **argv)
1775 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
1778 const char *wimfile;
1782 for_opt(c, optimize_options) {
1785 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1786 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1789 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1806 ret = wimlib_open_wim(wimfile, open_flags, &w,
1807 imagex_progress_func);
1811 old_size = file_get_size(argv[0]);
1812 printf("`%s' original size: ", wimfile);
1816 printf("%"PRIu64" KiB\n", old_size >> 10);
1818 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1821 new_size = file_get_size(argv[0]);
1822 printf("`%s' optimized size: ", wimfile);
1826 printf("%"PRIu64" KiB\n", new_size >> 10);
1828 fputs("Space saved: ", stdout);
1829 if (new_size != -1 && old_size != -1) {
1830 printf("%lld KiB\n",
1831 ((long long)old_size - (long long)new_size) >> 10);
1841 /* Split a WIM into a spanned set */
1842 static int imagex_split(int argc, char **argv)
1845 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1846 int write_flags = 0;
1847 unsigned long part_size;
1852 for_opt(c, split_options) {
1855 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1856 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1870 part_size = strtod(argv[2], &tmp) * (1 << 20);
1871 if (tmp == argv[2] || *tmp) {
1872 imagex_error("Invalid part size \"%s\"", argv[2]);
1873 imagex_error("The part size must be an integer or floating-point number of megabytes.");
1876 ret = wimlib_open_wim(argv[0], open_flags, &w, imagex_progress_func);
1879 ret = wimlib_split(w, argv[1], part_size, write_flags, imagex_progress_func);
1884 /* Unmounts an image. */
1885 static int imagex_unmount(int argc, char **argv)
1888 int unmount_flags = 0;
1891 for_opt(c, unmount_options) {
1894 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1897 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1900 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
1914 ret = wimlib_unmount_image(argv[0], unmount_flags,
1915 imagex_progress_func);
1917 imagex_error("Failed to unmount `%s'", argv[0]);
1921 struct imagex_command {
1923 int (*func)(int , char **);
1928 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
1929 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
1931 static const struct imagex_command imagex_commands[] = {
1932 {"append", imagex_capture_or_append, APPEND},
1933 {"apply", imagex_apply, APPLY},
1934 {"capture", imagex_capture_or_append, CAPTURE},
1935 {"delete", imagex_delete, DELETE},
1936 {"dir", imagex_dir, DIR},
1937 {"export", imagex_export, EXPORT},
1938 {"info", imagex_info, INFO},
1939 {"join", imagex_join, JOIN},
1940 {"mount", imagex_mount_rw_or_ro, MOUNT},
1941 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
1942 {"optimize",imagex_optimize, OPTIMIZE},
1943 {"split", imagex_split, SPLIT},
1944 {"unmount", imagex_unmount, UNMOUNT},
1947 static void version()
1949 static const char *s =
1950 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
1951 "Copyright (C) 2012, 2013 Eric Biggers\n"
1952 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
1953 "This is free software: you are free to change and redistribute it.\n"
1954 "There is NO WARRANTY, to the extent permitted by law.\n"
1956 "Report bugs to "PACKAGE_BUGREPORT".\n";
1961 static void help_or_version(int argc, char **argv)
1965 const struct imagex_command *cmd;
1967 for (i = 1; i < argc; i++) {
1975 if (strcmp(p, "help") == 0) {
1976 for_imagex_command(cmd) {
1977 if (strcmp(cmd->name, argv[1]) == 0) {
1985 if (strcmp(p, "version") == 0) {
1993 static void usage(int cmd_type)
1995 const struct imagex_command *cmd;
1996 printf("Usage: %s", usage_strings[cmd_type]);
1997 for_imagex_command(cmd) {
1998 if (cmd->cmd == cmd_type)
1999 printf("\nTry `man imagex-%s' for more details.\n",
2004 static void usage_all()
2006 puts("IMAGEX: Usage:");
2007 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
2008 printf(" %s", usage_strings[i]);
2009 static const char *extra =
2011 " imagex --version\n"
2013 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
2015 " Try `man imagex' for more information.\n"
2017 fputs(extra, stdout);
2021 int main(int argc, char **argv)
2023 const struct imagex_command *cmd;
2027 imagex_error("No command specified");
2032 help_or_version(argc, argv);
2036 wimlib_set_print_errors(true);
2037 ret = wimlib_global_init();
2041 for_imagex_command(cmd) {
2042 if (strcmp(cmd->name, *argv) == 0) {
2043 ret = cmd->func(argc, argv);
2048 imagex_error("Unrecognized command: `%s'", argv[0]);
2053 imagex_error("Exiting with error code %d:\n"
2055 wimlib_get_error_string(ret));
2056 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
2057 imagex_error_with_errno("errno");
2059 wimlib_global_cleanup();