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/>.
47 # include "imagex-win32.h"
52 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
54 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (char**)argv, "", \
73 static void usage(int cmd_type);
74 static void usage_all();
76 static const char *usage_strings[] = {
78 "imagex append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
79 " [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
80 " [--verbose] [--dereference] [--config=FILE]\n"
81 " [--threads=NUM_THREADS] [--rebuild] [--unix-data]\n"
84 "imagex apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
85 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
86 " [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n",
88 "imagex capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
89 " [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
90 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
91 " [--config=FILE] [--threads=NUM_THREADS] [--unix-data]\n"
94 "imagex delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check] [--soft]\n",
96 "imagex dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n",
98 "imagex export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
99 " DEST_WIMFILE [DEST_IMAGE_NAME] [DEST_IMAGE_DESCRIPTION]\n"
100 " [--boot] [--check] [--compress=TYPE] [--ref=\"GLOB\"]\n"
101 " [--threads=NUM_THREADS] [--rebuild]\n",
103 "imagex info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
104 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
105 " [--xml] [--extract-xml FILE] [--metadata]\n",
107 "imagex join [--check] WIMFILE SPLIT_WIM...\n",
109 "imagex mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
110 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
111 " [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n",
113 "imagex mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
114 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
115 " [--staging-dir=DIR] [--unix-data] [--allow-other]\n",
117 "imagex optimize WIMFILE [--check] [--recompress] [--compress=TYPE]\n",
119 "imagex split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n",
121 "imagex unmount DIRECTORY [--commit] [--check] [--rebuild]\n",
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 /* Parse the argument to --compress */
268 static int get_compression_type(const char *optarg)
270 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
271 return WIMLIB_COMPRESSION_TYPE_LZX;
272 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
273 return WIMLIB_COMPRESSION_TYPE_XPRESS;
274 else if (strcasecmp(optarg, "none") == 0)
275 return WIMLIB_COMPRESSION_TYPE_NONE;
277 imagex_error("Invalid compression type `%s'! Must be "
278 "\"maximum\", \"fast\", or \"none\".", optarg);
279 return WIMLIB_COMPRESSION_TYPE_INVALID;
283 /* Returns the size of a file given its name, or -1 if the file does not exist
284 * or its size cannot be determined. */
285 static off_t file_get_size(const char *filename)
288 if (stat(filename, &st) == 0)
294 static const char *default_capture_config =
299 "\\System Volume Information\n"
303 "[CompressionExclusionList]\n"
307 "\\WINDOWS\\inf\\*.pnf\n";
309 /* Read standard input until EOF and return the full contents in a malloc()ed
310 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
312 static char *stdin_get_contents(size_t *len_ret)
314 /* stdin can, of course, be a pipe or other non-seekable file, so the
315 * total length of the data cannot be pre-determined */
317 size_t newlen = 1024;
321 char *p = realloc(buf, newlen);
322 size_t bytes_read, bytes_to_read;
324 imagex_error("out of memory while reading stdin");
328 bytes_to_read = newlen - pos;
329 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
331 if (bytes_read != bytes_to_read) {
336 imagex_error_with_errno("error reading stdin");
349 PARSE_FILENAME_SUCCESS = 0,
350 PARSE_FILENAME_FAILURE = 1,
351 PARSE_FILENAME_NONE = 2,
355 * Parses a filename in the source list file format. (See the man page for
356 * 'imagex capture' for details on this format and the meaning.) Accepted
357 * formats for filenames are an unquoted string (whitespace-delimited), or a
358 * double or single-quoted string.
360 * @line_p: Pointer to the pointer to the line of data. Will be updated
361 * to point past the filename iff the return value is
362 * PARSE_FILENAME_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
365 * @len_p: @len_p initially stores the length of the line of data, which may
366 * be 0, and it will be updated to the number of bytes remaining in
367 * the line iff the return value is PARSE_FILENAME_SUCCESS.
369 * @fn_ret: Iff the return value is PARSE_FILENAME_SUCCESS, a pointer to the
370 * parsed filename will be returned here.
372 * Returns: PARSE_FILENAME_SUCCESS if a filename was successfully parsed; or
373 * PARSE_FILENAME_FAILURE if the data was invalid due to a missing
374 * closing quote; or PARSE_FILENAME_NONE if the line ended before the
375 * beginning of a filename was found.
377 static int parse_filename(char **line_p, size_t *len_p, char **fn_ret)
380 char *line = *line_p;
384 /* Skip leading whitespace */
387 return PARSE_FILENAME_NONE;
388 if (!isspace(*line) && *line != '\0')
394 if (quote_char == '"' || quote_char == '\'') {
395 /* Quoted filename */
399 line = memchr(line, quote_char, len);
401 imagex_error("Missing closing quote: %s", fn - 1);
402 return PARSE_FILENAME_FAILURE;
405 /* Unquoted filename. Go until whitespace. Line is terminated
406 * by '\0', so no need to check 'len'. */
410 } while (!isspace(*line) && *line != '\0');
417 return PARSE_FILENAME_SUCCESS;
420 /* Parses a line of data (not an empty line or comment) in the source list file
421 * format. (See the man page for 'imagex capture' for details on this format
424 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
425 * len == 0. The data in @line will be modified by this function call.
427 * @len: Length of the line of data.
429 * @source: On success, the capture source and target described by the line is
430 * written into this destination. Note that it will contain pointers
431 * to data in the @line array.
433 * Returns true if the line was valid; false otherwise. */
435 parse_source_list_line(char *line, size_t len,
436 struct wimlib_capture_source *source)
440 ret = parse_filename(&line, &len, &source->fs_source_path);
441 if (ret != PARSE_FILENAME_SUCCESS)
443 ret = parse_filename(&line, &len, &source->wim_target_path);
444 if (ret == PARSE_FILENAME_NONE)
445 source->wim_target_path = source->fs_source_path;
446 return ret != PARSE_FILENAME_FAILURE;
449 /* Returns %true if the given line of length @len > 0 is a comment or empty line
450 * in the source list file format. */
451 static bool is_comment_line(const char *line, size_t len)
456 if (!isspace(*line) && *line != '\0')
465 /* Parses a file in the source list format. (See the man page for 'imagex
466 * capture' for details on this format and the meaning.)
468 * @source_list_contents: Contents of the source list file. Note that this
469 * buffer will be modified to save memory allocations,
470 * and cannot be freed until the returned array of
471 * wimlib_capture_source's has also been freed.
473 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
476 * @nsources_ret: On success, the length of the returned array is
479 * Returns: An array of `struct wimlib_capture_source's that can be passed to
480 * the wimlib_add_image_multisource() function to specify how a WIM image is to
482 static struct wimlib_capture_source *
483 parse_source_list(char *source_list_contents, size_t source_list_nbytes,
484 size_t *nsources_ret)
488 struct wimlib_capture_source *sources;
492 for (i = 0; i < source_list_nbytes; i++)
493 if (source_list_contents[i] == '\n')
495 sources = calloc(nlines, sizeof(*sources));
497 imagex_error("out of memory");
500 p = source_list_contents;
502 for (i = 0; i < nlines; i++) {
503 /* XXX: Could use rawmemchr() here instead, but it may not be
504 * available on all platforms. */
505 char *endp = memchr(p, '\n', source_list_nbytes);
506 size_t len = endp - p + 1;
508 if (!is_comment_line(p, len)) {
509 if (!parse_source_list_line(p, len, &sources[j++])) {
521 /* Reads the contents of a file into memory. */
522 static char *file_get_contents(const char *filename, size_t *len_ret)
529 if (stat(filename, &stbuf) != 0) {
530 imagex_error_with_errno("Failed to stat the file `%s'", filename);
535 fp = fopen(filename, "rb");
537 imagex_error_with_errno("Failed to open the file `%s'", filename);
543 imagex_error("Failed to allocate buffer of %zu bytes to hold "
544 "contents of file `%s'", len, filename);
547 if (fread(buf, 1, len, fp) != len) {
548 imagex_error_with_errno("Failed to read %lu bytes from the "
549 "file `%s'", len, filename);
563 /* Return 0 if a path names a file to which the current user has write access;
564 * -1 otherwise (and print an error message). */
565 static int file_writable(const char *path)
568 ret = access(path, W_OK);
570 imagex_error_with_errno("Can't modify `%s'", path);
574 #define TO_PERCENT(numerator, denominator) \
575 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
577 /* Given an enumerated value for WIM compression type, return a descriptive
579 static const char *get_data_type(int ctype)
582 case WIMLIB_COMPRESSION_TYPE_NONE:
583 return "uncompressed";
584 case WIMLIB_COMPRESSION_TYPE_LZX:
585 return "LZX-compressed";
586 case WIMLIB_COMPRESSION_TYPE_XPRESS:
587 return "XPRESS-compressed";
592 /* Progress callback function passed to various wimlib functions. */
593 static int imagex_progress_func(enum wimlib_progress_msg msg,
594 const union wimlib_progress_info *info)
596 unsigned percent_done;
598 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
599 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
600 info->write_streams.total_bytes);
601 if (info->write_streams.completed_streams == 0) {
602 const char *data_type;
604 data_type = get_data_type(info->write_streams.compression_type);
605 printf("Writing %s data using %u thread%s\n",
606 data_type, info->write_streams.num_threads,
607 (info->write_streams.num_threads == 1) ? "" : "s");
609 printf("\r%"PRIu64" MiB of %"PRIu64" MiB (uncompressed) "
610 "written (%u%% done)",
611 info->write_streams.completed_bytes >> 20,
612 info->write_streams.total_bytes >> 20,
614 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
617 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
618 printf("Scanning `%s'", info->scan.source);
619 if (*info->scan.wim_target_path) {
620 printf(" (loading as WIM path: `/%s')...\n",
621 info->scan.wim_target_path);
623 printf(" (loading as root of WIM image)...\n");
626 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
627 if (info->scan.excluded)
628 printf("Excluding `%s' from capture\n", info->scan.cur_path);
630 printf("Scanning `%s'\n", info->scan.cur_path);
632 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
634 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
635 percent_done = TO_PERCENT(info->integrity.completed_bytes,
636 info->integrity.total_bytes);
637 printf("\rVerifying integrity of `%s': %"PRIu64" MiB "
638 "of %"PRIu64" MiB (%u%%) done",
639 info->integrity.filename,
640 info->integrity.completed_bytes >> 20,
641 info->integrity.total_bytes >> 20,
643 if (info->integrity.completed_bytes == info->integrity.total_bytes)
646 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
647 percent_done = TO_PERCENT(info->integrity.completed_bytes,
648 info->integrity.total_bytes);
649 printf("\rCalculating integrity table for WIM: %"PRIu64" MiB "
650 "of %"PRIu64" MiB (%u%%) done",
651 info->integrity.completed_bytes >> 20,
652 info->integrity.total_bytes >> 20,
654 if (info->integrity.completed_bytes == info->integrity.total_bytes)
657 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
658 printf("Applying image %d (%s) from `%s' to %s `%s'\n",
660 info->extract.image_name,
661 info->extract.wimfile_name,
662 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
663 "NTFS volume" : "directory"),
664 info->extract.target);
666 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
667 /*printf("Applying directory structure to %s\n",*/
668 /*info->extract.target);*/
670 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
671 percent_done = TO_PERCENT(info->extract.completed_bytes,
672 info->extract.total_bytes);
673 printf("\rExtracting files: "
674 "%"PRIu64" MiB of %"PRIu64" MiB (%u%%) done",
675 info->extract.completed_bytes >> 20,
676 info->extract.total_bytes >> 20,
678 if (info->extract.completed_bytes >= info->extract.total_bytes)
681 case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
682 puts(info->extract.cur_path);
684 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
685 printf("Setting timestamps on all extracted files...\n");
687 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
688 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
689 printf("Unmounting NTFS volume `%s'...\n",
690 info->extract.target);
693 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
694 percent_done = TO_PERCENT(info->join.completed_bytes,
695 info->join.total_bytes);
696 printf("Writing resources from part %u of %u: "
697 "%"PRIu64 " MiB of %"PRIu64" MiB (%u%%) written\n",
698 (info->join.completed_parts == info->join.total_parts) ?
699 info->join.completed_parts : info->join.completed_parts + 1,
700 info->join.total_parts,
701 info->join.completed_bytes >> 20,
702 info->join.total_bytes >> 20,
705 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
706 percent_done = TO_PERCENT(info->split.completed_bytes,
707 info->split.total_bytes);
708 printf("Writing `%s': %"PRIu64" MiB of %"PRIu64" MiB (%u%%) written\n",
709 info->split.part_name,
710 info->split.completed_bytes >> 20,
711 info->split.total_bytes >> 20,
714 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
715 if (info->split.completed_bytes == info->split.total_bytes) {
716 printf("Finished writing %u split WIM parts\n",
717 info->split.cur_part_number);
727 /* Open all the split WIM parts that correspond to a file glob.
729 * @first_part specifies the first part of the split WIM and it may be either
730 * included or omitted from the glob. */
731 static int open_swms_from_glob(const char *swm_glob,
732 const char *first_part,
734 WIMStruct ***additional_swms_ret,
735 unsigned *num_additional_swms_ret)
737 unsigned num_additional_swms = 0;
738 WIMStruct **additional_swms = NULL;
742 /* Warning: glob() is replaced in Windows native builds */
743 ret = glob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
745 if (ret == GLOB_NOMATCH) {
746 imagex_error("Found no files for glob \"%s\"",
749 imagex_error_with_errno("Failed to process glob "
755 num_additional_swms = globbuf.gl_pathc;
756 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
757 if (!additional_swms) {
758 imagex_error("Out of memory");
763 for (unsigned i = 0; i < num_additional_swms; i++) {
764 if (strcmp(globbuf.gl_pathv[i], first_part) == 0) {
768 ret = wimlib_open_wim(globbuf.gl_pathv[i],
769 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
770 &additional_swms[i - offset],
771 imagex_progress_func);
775 *additional_swms_ret = additional_swms;
776 *num_additional_swms_ret = num_additional_swms - offset;
780 for (unsigned i = 0; i < num_additional_swms; i++)
781 wimlib_free(additional_swms[i]);
782 free(additional_swms);
790 static unsigned parse_num_threads(const char *optarg)
793 unsigned nthreads = strtoul(optarg, &tmp, 10);
794 if (nthreads == UINT_MAX || *tmp || tmp == optarg) {
795 imagex_error("Number of threads must be a non-negative integer!");
803 /* Apply one image, or all images, from a WIM file into a directory, OR apply
804 * one image from a WIM file to a NTFS volume. */
805 static int imagex_apply(int argc, char **argv)
808 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
815 const char *image_num_or_name;
816 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
818 const char *swm_glob = NULL;
819 WIMStruct **additional_swms = NULL;
820 unsigned num_additional_swms = 0;
822 for_opt(c, apply_options) {
825 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
828 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
831 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
834 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
840 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
849 if (argc != 2 && argc != 3) {
856 image_num_or_name = "1";
859 image_num_or_name = argv[1];
863 ret = wimlib_open_wim(wimfile, open_flags, &w, imagex_progress_func);
867 image = wimlib_resolve_image(w, image_num_or_name);
868 ret = verify_image_exists(image, image_num_or_name, wimfile);
872 num_images = wimlib_get_num_images(w);
873 if (argc == 2 && num_images != 1) {
874 imagex_error("`%s' contains %d images; Please select one "
875 "(or all)", wimfile, num_images);
882 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
884 &num_additional_swms);
891 ret = stat(target, &stbuf);
893 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
894 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
896 if (errno != ENOENT) {
897 imagex_error_with_errno("Failed to stat `%s'", target);
903 ret = wimlib_extract_image(w, image, target, extract_flags,
904 additional_swms, num_additional_swms,
905 imagex_progress_func);
907 printf("Done applying WIM image.\n");
910 if (additional_swms) {
911 for (unsigned i = 0; i < num_additional_swms; i++)
912 wimlib_free(additional_swms[i]);
913 free(additional_swms);
918 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
919 * directory trees. 'imagex capture': create a new WIM file containing the
920 * desired image. 'imagex append': add a new image to an existing WIM file. */
921 static int imagex_capture_or_append(int argc, char **argv)
925 int add_image_flags = 0;
927 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
931 const char *flags_element = NULL;
935 int cmd = strcmp(argv[0], "append") ? CAPTURE : APPEND;
936 unsigned num_threads = 0;
939 size_t source_name_len;
942 const char *config_file = NULL;
943 char *config_str = NULL;
946 bool source_list = false;
947 size_t source_list_nbytes;
948 char *source_list_contents = NULL;
949 bool capture_sources_malloced = false;
950 struct wimlib_capture_source *capture_sources;
953 for_opt(c, capture_or_append_options) {
956 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
959 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
960 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
963 config_file = optarg;
966 compression_type = get_compression_type(optarg);
967 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
971 flags_element = optarg;
974 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
977 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
980 num_threads = parse_num_threads(optarg);
981 if (num_threads == UINT_MAX)
985 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
988 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1001 if (argc < 2 || argc > 4) {
1012 /* Set default name to SOURCE argument, omitting any directory
1013 * prefixes and trailing slashes. This requires making a copy
1015 source_name_len = strlen(source);
1016 source_copy = alloca(source_name_len + 1);
1017 name = basename(strcpy(source_copy, source));
1019 /* Image description defaults to NULL if not given. */
1020 desc = (argc >= 4) ? argv[3] : NULL;
1023 /* Set up capture sources in source list mode */
1024 if (source[0] == '-' && source[1] == '\0') {
1025 source_list_contents = stdin_get_contents(&source_list_nbytes);
1027 source_list_contents = file_get_contents(source,
1028 &source_list_nbytes);
1030 if (!source_list_contents)
1033 capture_sources = parse_source_list(source_list_contents,
1036 if (!capture_sources) {
1040 capture_sources_malloced = true;
1042 /* Set up capture source in non-source-list mode (could be
1043 * either "normal" mode or "NTFS mode"--- see the man page). */
1044 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1045 capture_sources[0].fs_source_path = source;
1046 capture_sources[0].wim_target_path = NULL;
1047 capture_sources[0].reserved = 0;
1052 config_str = file_get_contents(config_file, &config_len);
1060 ret = wimlib_open_wim(wimfile, open_flags, &w,
1061 imagex_progress_func);
1063 ret = wimlib_create_new_wim(compression_type, &w);
1069 ret = stat(source, &stbuf);
1071 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1072 printf("Capturing WIM image from NTFS filesystem on `%s'\n",
1074 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1077 if (errno != ENOENT) {
1078 imagex_error_with_errno("Failed to stat `%s'", source);
1085 ret = wimlib_add_image_multisource(w, capture_sources,
1087 (config_str ? config_str :
1088 default_capture_config),
1089 (config_str ? config_len :
1090 strlen(default_capture_config)),
1092 imagex_progress_func);
1095 cur_image = wimlib_get_num_images(w);
1097 ret = wimlib_set_image_descripton(w, cur_image, desc);
1101 if (flags_element) {
1102 ret = wimlib_set_image_flags(w, cur_image, flags_element);
1106 if (cmd == APPEND) {
1107 ret = wimlib_overwrite(w, write_flags, num_threads,
1108 imagex_progress_func);
1110 ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
1111 num_threads, imagex_progress_func);
1113 if (ret == WIMLIB_ERR_REOPEN)
1116 imagex_error("Failed to write the WIM file `%s'", wimfile);
1120 free(source_list_contents);
1121 if (capture_sources_malloced)
1122 free(capture_sources);
1126 /* Remove image(s) from a WIM. */
1127 static int imagex_delete(int argc, char **argv)
1131 int write_flags = 0;
1132 const char *wimfile;
1133 const char *image_num_or_name;
1138 for_opt(c, delete_options) {
1141 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1142 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1145 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
1157 imagex_error("Must specify a WIM file");
1159 imagex_error("Must specify an image");
1164 image_num_or_name = argv[1];
1166 ret = file_writable(wimfile);
1170 ret = wimlib_open_wim(wimfile, open_flags, &w,
1171 imagex_progress_func);
1175 image = wimlib_resolve_image(w, image_num_or_name);
1177 ret = verify_image_exists(image, image_num_or_name, wimfile);
1181 ret = wimlib_delete_image(w, image);
1183 imagex_error("Failed to delete image from `%s'", wimfile);
1187 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1188 if (ret == WIMLIB_ERR_REOPEN)
1191 imagex_error("Failed to write the file `%s' with image "
1192 "deleted", wimfile);
1199 /* Print the files contained in an image(s) in a WIM file. */
1200 static int imagex_dir(int argc, char **argv)
1202 const char *wimfile;
1209 imagex_error("Must specify a WIM file");
1214 imagex_error("Too many arguments");
1220 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w,
1221 imagex_progress_func);
1226 image = wimlib_resolve_image(w, argv[2]);
1227 ret = verify_image_exists(image, argv[2], wimfile);
1231 /* Image was not specified. If the WIM only contains one image,
1232 * choose that one; otherwise, print an error. */
1233 num_images = wimlib_get_num_images(w);
1234 if (num_images != 1) {
1235 imagex_error("The file `%s' contains %d images; Please "
1236 "select one.", wimfile, num_images);
1244 ret = wimlib_print_files(w, image);
1250 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
1252 static int imagex_export(int argc, char **argv)
1256 int export_flags = 0;
1257 int write_flags = 0;
1258 int compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
1259 bool compression_type_specified = false;
1260 const char *src_wimfile;
1261 const char *src_image_num_or_name;
1262 const char *dest_wimfile;
1263 const char *dest_name;
1264 const char *dest_desc;
1265 WIMStruct *src_w = NULL;
1266 WIMStruct *dest_w = NULL;
1271 const char *swm_glob = NULL;
1272 WIMStruct **additional_swms = NULL;
1273 unsigned num_additional_swms = 0;
1274 unsigned num_threads = 0;
1276 for_opt(c, export_options) {
1279 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
1282 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1283 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1286 compression_type = get_compression_type(optarg);
1287 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1289 compression_type_specified = true;
1295 num_threads = parse_num_threads(optarg);
1296 if (num_threads == UINT_MAX)
1300 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1309 if (argc < 3 || argc > 5) {
1313 src_wimfile = argv[0];
1314 src_image_num_or_name = argv[1];
1315 dest_wimfile = argv[2];
1316 dest_name = (argc >= 4) ? argv[3] : NULL;
1317 dest_desc = (argc >= 5) ? argv[4] : NULL;
1318 ret = wimlib_open_wim(src_wimfile,
1319 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w,
1320 imagex_progress_func);
1324 /* Determine if the destination is an existing file or not.
1325 * If so, we try to append the exported image(s) to it; otherwise, we
1326 * create a new WIM containing the exported image(s). */
1327 if (stat(dest_wimfile, &stbuf) == 0) {
1331 /* Destination file exists. */
1333 if (!S_ISREG(stbuf.st_mode)) {
1334 imagex_error("`%s' is not a regular file",
1339 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w,
1340 imagex_progress_func);
1344 ret = file_writable(dest_wimfile);
1348 dest_ctype = wimlib_get_compression_type(dest_w);
1349 if (compression_type_specified
1350 && compression_type != dest_ctype)
1352 imagex_error("Cannot specify a compression type that is "
1353 "not the same as that used in the "
1360 /* dest_wimfile is not an existing file, so create a new WIM. */
1361 if (!compression_type_specified)
1362 compression_type = wimlib_get_compression_type(src_w);
1363 if (errno == ENOENT) {
1364 ret = wimlib_create_new_wim(compression_type, &dest_w);
1368 imagex_error_with_errno("Cannot stat file `%s'",
1375 image = wimlib_resolve_image(src_w, src_image_num_or_name);
1376 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
1381 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
1383 &num_additional_swms);
1388 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
1389 export_flags, additional_swms,
1390 num_additional_swms, imagex_progress_func);
1396 ret = wimlib_write(dest_w, dest_wimfile, WIMLIB_ALL_IMAGES,
1397 write_flags, num_threads,
1398 imagex_progress_func);
1400 ret = wimlib_overwrite(dest_w, write_flags, num_threads,
1401 imagex_progress_func);
1403 if (ret == WIMLIB_ERR_REOPEN)
1406 wimlib_free(dest_w);
1407 if (additional_swms) {
1408 for (unsigned i = 0; i < num_additional_swms; i++)
1409 wimlib_free(additional_swms[i]);
1410 free(additional_swms);
1415 /* Prints information about a WIM file; also can mark an image as bootable,
1416 * change the name of an image, or change the description of an image. */
1417 static int imagex_info(int argc, char **argv)
1422 bool header = false;
1423 bool lookup_table = false;
1425 bool metadata = false;
1426 bool short_header = true;
1427 const char *xml_out_file = NULL;
1428 const char *wimfile;
1429 const char *image_num_or_name = "all";
1430 const char *new_name = NULL;
1431 const char *new_desc = NULL;
1436 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1441 for_opt(c, info_options) {
1451 short_header = false;
1454 lookup_table = true;
1455 short_header = false;
1459 short_header = false;
1462 xml_out_file = optarg;
1463 short_header = false;
1467 short_header = false;
1477 if (argc == 0 || argc > 4) {
1483 image_num_or_name = argv[1];
1493 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1495 ret = wimlib_open_wim(wimfile, open_flags, &w,
1496 imagex_progress_func);
1500 part_number = wimlib_get_part_number(w, &total_parts);
1502 image = wimlib_resolve_image(w, image_num_or_name);
1503 if (image == WIMLIB_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
1504 imagex_error("The image `%s' does not exist",
1507 imagex_error("If you would like to set the boot "
1508 "index to 0, specify image \"0\" with "
1509 "the --boot flag.");
1510 ret = WIMLIB_ERR_INVALID_IMAGE;
1514 num_images = wimlib_get_num_images(w);
1516 if (num_images == 0) {
1518 imagex_error("--boot is meaningless on a WIM with no "
1520 ret = WIMLIB_ERR_INVALID_IMAGE;
1525 if (image == WIMLIB_ALL_IMAGES && num_images > 1) {
1527 imagex_error("Cannot specify the --boot flag "
1528 "without specifying a specific "
1529 "image in a multi-image WIM");
1530 ret = WIMLIB_ERR_INVALID_IMAGE;
1534 imagex_error("Cannot specify the NEW_NAME "
1535 "without specifying a specific "
1536 "image in a multi-image WIM");
1537 ret = WIMLIB_ERR_INVALID_IMAGE;
1542 /* Operations that print information are separated from operations that
1543 * recreate the WIM file. */
1544 if (!new_name && !boot) {
1546 /* Read-only operations */
1548 if (image == WIMLIB_NO_IMAGE) {
1549 imagex_error("`%s' is not a valid image",
1551 ret = WIMLIB_ERR_INVALID_IMAGE;
1555 if (image == WIMLIB_ALL_IMAGES && short_header)
1556 wimlib_print_wim_information(w);
1559 wimlib_print_header(w);
1562 if (total_parts != 1) {
1563 printf("Warning: Only showing the lookup table "
1564 "for part %d of a %d-part WIM.\n",
1565 part_number, total_parts);
1567 wimlib_print_lookup_table(w);
1571 ret = wimlib_extract_xml_data(w, stdout);
1577 fp = fopen(xml_out_file, "wb");
1579 imagex_error_with_errno("Failed to open the "
1586 ret = wimlib_extract_xml_data(w, fp);
1587 if (fclose(fp) != 0) {
1588 imagex_error("Failed to close the file `%s'",
1598 wimlib_print_available_images(w, image);
1601 ret = wimlib_print_metadata(w, image);
1607 /* Modification operations */
1608 if (total_parts != 1) {
1609 imagex_error("Modifying a split WIM is not supported.");
1613 if (image == WIMLIB_ALL_IMAGES)
1616 if (image == WIMLIB_NO_IMAGE && new_name) {
1617 imagex_error("Cannot specify new_name (`%s') when "
1618 "using image 0", new_name);
1624 if (image == wimlib_get_boot_idx(w)) {
1625 printf("Image %d is already marked as "
1626 "bootable.\n", image);
1629 printf("Marking image %d as bootable.\n",
1631 wimlib_set_boot_idx(w, image);
1635 if (strcmp(wimlib_get_image_name(w, image),
1637 printf("Image %d is already named \"%s\".\n",
1641 printf("Changing the name of image %d to "
1642 "\"%s\".\n", image, new_name);
1643 ret = wimlib_set_image_name(w, image, new_name);
1649 const char *old_desc;
1650 old_desc = wimlib_get_image_description(w, image);
1651 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1652 printf("The description of image %d is already "
1653 "\"%s\".\n", image, new_desc);
1656 printf("Changing the description of image %d "
1657 "to \"%s\".\n", image, new_desc);
1658 ret = wimlib_set_image_descripton(w, image,
1665 /* Only call wimlib_overwrite() if something actually needs to
1667 if (boot || new_name || new_desc ||
1668 (check && !wimlib_has_integrity_table(w)))
1672 ret = file_writable(wimfile);
1677 write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1681 ret = wimlib_overwrite(w, write_flags, 1,
1682 imagex_progress_func);
1683 if (ret == WIMLIB_ERR_REOPEN)
1686 printf("The file `%s' was not modified because nothing "
1687 "needed to be done.\n", wimfile);
1696 /* Join split WIMs into one part WIM */
1697 static int imagex_join(int argc, char **argv)
1700 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1701 int wim_write_flags = 0;
1702 const char *output_path;
1704 for_opt(c, join_options) {
1707 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1708 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1718 imagex_error("Must specify one or more split WIM (.swm) parts "
1722 output_path = argv[0];
1723 return wimlib_join((const char **)++argv, --argc, output_path,
1724 swm_open_flags, wim_write_flags,
1725 imagex_progress_func);
1731 /* Mounts an image using a FUSE mount. */
1732 static int imagex_mount_rw_or_ro(int argc, char **argv)
1735 int mount_flags = 0;
1736 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1737 const char *wimfile;
1743 const char *swm_glob = NULL;
1744 WIMStruct **additional_swms = NULL;
1745 unsigned num_additional_swms = 0;
1746 const char *staging_dir = NULL;
1748 if (strcmp(argv[0], "mountrw") == 0)
1749 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1751 for_opt(c, mount_options) {
1754 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
1757 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1760 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1763 if (strcasecmp(optarg, "none") == 0)
1764 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1765 else if (strcasecmp(optarg, "xattr") == 0)
1766 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1767 else if (strcasecmp(optarg, "windows") == 0)
1768 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1770 imagex_error("Unknown stream interface \"%s\"", optarg);
1778 staging_dir = optarg;
1781 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
1789 if (argc != 2 && argc != 3)
1794 ret = wimlib_open_wim(wimfile, open_flags, &w,
1795 imagex_progress_func);
1800 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1802 &num_additional_swms);
1809 num_images = wimlib_get_num_images(w);
1810 if (num_images != 1) {
1811 imagex_error("The file `%s' contains %d images; Please "
1812 "select one.", wimfile, num_images);
1813 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1820 image = wimlib_resolve_image(w, argv[1]);
1822 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
1827 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1828 ret = file_writable(wimfile);
1833 ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
1834 num_additional_swms, staging_dir);
1836 imagex_error("Failed to mount image %d from `%s' on `%s'",
1837 image, wimfile, dir);
1842 if (additional_swms) {
1843 for (unsigned i = 0; i < num_additional_swms; i++)
1844 wimlib_free(additional_swms[i]);
1845 free(additional_swms);
1849 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1854 /* Rebuild a WIM file */
1855 static int imagex_optimize(int argc, char **argv)
1859 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
1862 const char *wimfile;
1866 for_opt(c, optimize_options) {
1869 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1870 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1873 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1890 ret = wimlib_open_wim(wimfile, open_flags, &w,
1891 imagex_progress_func);
1895 old_size = file_get_size(argv[0]);
1896 printf("`%s' original size: ", wimfile);
1900 printf("%"PRIu64" KiB\n", old_size >> 10);
1902 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1905 new_size = file_get_size(argv[0]);
1906 printf("`%s' optimized size: ", wimfile);
1910 printf("%"PRIu64" KiB\n", new_size >> 10);
1912 fputs("Space saved: ", stdout);
1913 if (new_size != -1 && old_size != -1) {
1914 printf("%lld KiB\n",
1915 ((long long)old_size - (long long)new_size) >> 10);
1925 /* Split a WIM into a spanned set */
1926 static int imagex_split(int argc, char **argv)
1929 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1930 int write_flags = 0;
1931 unsigned long part_size;
1936 for_opt(c, split_options) {
1939 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1940 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1954 part_size = strtod(argv[2], &tmp) * (1 << 20);
1955 if (tmp == argv[2] || *tmp) {
1956 imagex_error("Invalid part size \"%s\"", argv[2]);
1957 imagex_error("The part size must be an integer or floating-point number of megabytes.");
1960 ret = wimlib_open_wim(argv[0], open_flags, &w, imagex_progress_func);
1963 ret = wimlib_split(w, argv[1], part_size, write_flags, imagex_progress_func);
1968 /* Unmounts a mounted WIM image. */
1969 static int imagex_unmount(int argc, char **argv)
1972 int unmount_flags = 0;
1975 for_opt(c, unmount_options) {
1978 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1981 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1984 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
1998 ret = wimlib_unmount_image(argv[0], unmount_flags,
1999 imagex_progress_func);
2001 imagex_error("Failed to unmount `%s'", argv[0]);
2005 struct imagex_command {
2007 int (*func)(int , char **);
2012 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
2013 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
2015 static const struct imagex_command imagex_commands[] = {
2016 {"append", imagex_capture_or_append, APPEND},
2017 {"apply", imagex_apply, APPLY},
2018 {"capture", imagex_capture_or_append, CAPTURE},
2019 {"delete", imagex_delete, DELETE},
2020 {"dir", imagex_dir, DIR},
2021 {"export", imagex_export, EXPORT},
2022 {"info", imagex_info, INFO},
2023 {"join", imagex_join, JOIN},
2024 {"mount", imagex_mount_rw_or_ro, MOUNT},
2025 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
2026 {"optimize",imagex_optimize, OPTIMIZE},
2027 {"split", imagex_split, SPLIT},
2028 {"unmount", imagex_unmount, UNMOUNT},
2031 static void version()
2033 static const char *s =
2034 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
2035 "Copyright (C) 2012, 2013 Eric Biggers\n"
2036 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
2037 "This is free software: you are free to change and redistribute it.\n"
2038 "There is NO WARRANTY, to the extent permitted by law.\n"
2040 "Report bugs to "PACKAGE_BUGREPORT".\n";
2045 static void help_or_version(int argc, char **argv)
2049 const struct imagex_command *cmd;
2051 for (i = 1; i < argc; i++) {
2059 if (strcmp(p, "help") == 0) {
2060 for_imagex_command(cmd) {
2061 if (strcmp(cmd->name, argv[1]) == 0) {
2069 if (strcmp(p, "version") == 0) {
2077 static void usage(int cmd_type)
2079 const struct imagex_command *cmd;
2080 printf("Usage: %s", usage_strings[cmd_type]);
2081 for_imagex_command(cmd) {
2082 if (cmd->cmd == cmd_type)
2083 printf("\nTry `man imagex-%s' for more details.\n",
2088 static void usage_all()
2090 puts("IMAGEX: Usage:");
2091 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
2092 printf(" %s", usage_strings[i]);
2093 static const char *extra =
2095 " imagex --version\n"
2097 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
2099 " Try `man imagex' for more information.\n"
2101 fputs(extra, stdout);
2104 /* Entry point for the 'imagex' program. */
2105 int main(int argc, char **argv)
2107 const struct imagex_command *cmd;
2111 imagex_error("No command specified");
2116 /* Handle --help and --version for all commands. Note that this will
2117 * not return if either of these arguments are present. */
2118 help_or_version(argc, argv);
2122 /* The user may like to see more informative error messages. */
2123 wimlib_set_print_errors(true);
2125 /* Calling wimlib_global_init() is not strictly necessary because
2126 * 'imagex' is single-threaded. */
2127 ret = wimlib_global_init();
2131 /* Search for the function to handle the 'imagex' subcommand. */
2132 for_imagex_command(cmd) {
2133 if (strcmp(cmd->name, *argv) == 0) {
2134 ret = cmd->func(argc, argv);
2135 goto out_check_write_error;
2139 imagex_error("Unrecognized command: `%s'", argv[0]);
2142 out_check_write_error:
2143 /* For 'imagex info' and 'imagex dir', data printed to standard output
2144 * is part of the program's actual behavior and not just for
2145 * informational purposes, so we should set a failure exit status if
2146 * there was a write error. */
2147 if (cmd == &imagex_commands[INFO] || cmd == &imagex_commands[DIR]) {
2148 if (ferror(stdout) || fclose(stdout)) {
2149 imagex_error_with_errno("output error");
2155 /* Exit status (ret): -1 indicates an error found by 'imagex' outside
2156 * of the wimlib library code. 0 indicates success. > 0 indicates a
2157 * wimlib error code from which an error message can be printed. */
2159 imagex_error("Exiting with error code %d:\n"
2161 wimlib_get_error_string(ret));
2162 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
2163 imagex_error_with_errno("errno");
2165 /* Calling wimlib_global_cleanup() is not strictly necessary because the
2166 * process is exiting anyway. */
2167 wimlib_global_cleanup();