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 apply_options[] = {
119 {"check", no_argument, NULL, 'c'},
120 {"hardlink", no_argument, NULL, 'h'},
121 {"symlink", no_argument, NULL, 's'},
122 {"verbose", no_argument, NULL, 'v'},
123 {"ref", required_argument, NULL, 'r'},
124 {"unix-data", no_argument, NULL, 'U'},
127 static const struct option capture_or_append_options[] = {
128 {"boot", no_argument, NULL, 'b'},
129 {"check", no_argument, NULL, 'c'},
130 {"compress", required_argument, NULL, 'x'},
131 {"config", required_argument, NULL, 'C'},
132 {"dereference", no_argument, NULL, 'L'},
133 {"flags", required_argument, NULL, 'f'},
134 {"verbose", no_argument, NULL, 'v'},
135 {"threads", required_argument, NULL, 't'},
136 {"rebuild", no_argument, NULL, 'R'},
137 {"unix-data", no_argument, NULL, 'U'},
138 {"source-list", no_argument, NULL, 'S'},
141 static const struct option delete_options[] = {
142 {"check", no_argument, NULL, 'c'},
143 {"soft", no_argument, NULL, 's'},
147 static const struct option export_options[] = {
148 {"boot", no_argument, NULL, 'b'},
149 {"check", no_argument, NULL, 'c'},
150 {"compress", required_argument, NULL, 'x'},
151 {"ref", required_argument, NULL, 'r'},
152 {"threads", required_argument, NULL, 't'},
153 {"rebuild", no_argument, NULL, 'R'},
157 static const struct option info_options[] = {
158 {"boot", no_argument, NULL, 'b'},
159 {"check", no_argument, NULL, 'c'},
160 {"extract-xml", required_argument, NULL, 'X'},
161 {"header", no_argument, NULL, 'h'},
162 {"lookup-table", no_argument, NULL, 'l'},
163 {"metadata", no_argument, NULL, 'm'},
164 {"xml", no_argument, NULL, 'x'},
168 static const struct option join_options[] = {
169 {"check", no_argument, NULL, 'c'},
173 static const struct option mount_options[] = {
174 {"check", no_argument, NULL, 'c'},
175 {"debug", no_argument, NULL, 'd'},
176 {"streams-interface", required_argument, NULL, 's'},
177 {"ref", required_argument, NULL, 'r'},
178 {"staging-dir", required_argument, NULL, 'D'},
179 {"unix-data", no_argument, NULL, 'U'},
180 {"allow-other", no_argument, NULL, 'A'},
184 static const struct option optimize_options[] = {
185 {"check", no_argument, NULL, 'c'},
186 {"recompress", no_argument, NULL, 'r'},
190 static const struct option split_options[] = {
191 {"check", no_argument, NULL, 'c'},
195 static const struct option unmount_options[] = {
196 {"commit", no_argument, NULL, 'c'},
197 {"check", no_argument, NULL, 'C'},
198 {"rebuild", no_argument, NULL, 'R'},
204 /* Print formatted error message to stderr. */
205 static void imagex_error(const char *format, ...)
208 va_start(va, format);
209 fputs("ERROR: ", stderr);
210 vfprintf(stderr, format, va);
215 /* Print formatted error message to stderr. */
216 static void imagex_error_with_errno(const char *format, ...)
218 int errno_save = errno;
220 va_start(va, format);
221 fputs("ERROR: ", stderr);
222 vfprintf(stderr, format, va);
223 fprintf(stderr, ": %s\n", strerror(errno_save));
227 static int verify_image_exists(int image, const char *image_name,
228 const char *wim_name)
230 if (image == WIMLIB_NO_IMAGE) {
231 imagex_error("\"%s\" is not a valid image in `%s'!\n"
232 " Please specify a 1-based imagex index or "
234 " You may use `imagex info' to list the images "
235 "contained in a WIM.",
236 image_name, wim_name);
242 static int verify_image_is_single(int image)
244 if (image == WIMLIB_ALL_IMAGES) {
245 imagex_error("Cannot specify all images for this action!");
251 static int verify_image_exists_and_is_single(int image, const char *image_name,
252 const char *wim_name)
255 ret = verify_image_exists(image, image_name, wim_name);
257 ret = verify_image_is_single(image);
261 /* Parse the argument to --compress */
262 static int get_compression_type(const char *optarg)
264 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
265 return WIMLIB_COMPRESSION_TYPE_LZX;
266 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
267 return WIMLIB_COMPRESSION_TYPE_XPRESS;
268 else if (strcasecmp(optarg, "none") == 0)
269 return WIMLIB_COMPRESSION_TYPE_NONE;
271 imagex_error("Invalid compression type `%s'! Must be "
272 "\"maximum\", \"fast\", or \"none\".", optarg);
273 return WIMLIB_COMPRESSION_TYPE_INVALID;
277 /* Returns the size of a file given its name, or -1 if the file does not exist
278 * or its size cannot be determined. */
279 static off_t file_get_size(const char *filename)
282 if (stat(filename, &st) == 0)
288 static const char *default_capture_config =
293 "\\System Volume Information\n"
297 "[CompressionExclusionList]\n"
301 "\\WINDOWS\\inf\\*.pnf\n";
303 /* Read standard input until EOF and return the full contents in a malloc()ed
304 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
306 static char *stdin_get_contents(size_t *len_ret)
308 /* stdin can, of course, be a pipe or other non-seekable file, so the
309 * total length of the data cannot be pre-determined */
311 size_t newlen = 1024;
315 char *p = realloc(buf, newlen);
316 size_t bytes_read, bytes_to_read;
318 imagex_error("out of memory while reading stdin");
322 bytes_to_read = newlen - pos;
323 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
325 if (bytes_read != bytes_to_read) {
330 imagex_error_with_errno("error reading stdin");
343 PARSE_FILENAME_SUCCESS = 0,
344 PARSE_FILENAME_FAILURE = 1,
345 PARSE_FILENAME_NONE = 2,
349 * Parses a filename in the source list file format. (See the man page for
350 * 'imagex capture' for details on this format and the meaning.) Accepted
351 * formats for filenames are an unquoted string, whitespace-delimited, or a
352 * double or single-quoted string.
354 * @line_p: Pointer to the pointer to the line of data. Will be updated
355 * to point past the filename iff the return value is
356 * PARSE_FILENAME_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
359 * @len_p: @len_p initially stores the length of the line of data, which may
360 * be 0, and it will be updated to the number of bytes remaining in
361 * the line iff the return value is PARSE_FILENAME_SUCCESS.
363 * @fn_ret: Iff the return value is PARSE_FILENAME_SUCCESS, a pointer to the
364 * parsed filename will be returned here.
366 * Returns: PARSE_FILENAME_SUCCESS if a filename was successfully parsed; or
367 * PARSE_FILENAME_FAILURE if the data was invalid due to a missing
368 * closing quote; or PARSE_FILENAME_NONE if the line ended before the
369 * beginning of a filename was found.
371 static int parse_filename(char **line_p, size_t *len_p, char **fn_ret)
374 char *line = *line_p;
378 /* Skip leading whitespace */
381 return PARSE_FILENAME_NONE;
382 if (!isspace(*line) && *line != '\0')
388 if (quote_char == '"' || quote_char == '\'') {
389 /* Quoted filename */
393 line = memchr(line, quote_char, len);
395 imagex_error("Missing closing quote: %s", fn - 1);
396 return PARSE_FILENAME_FAILURE;
399 /* Unquoted filename. Go until whitespace. Line is terminated
400 * by '\0', so no need to check 'len'. */
404 } while (!isspace(*line) && *line != '\0');
411 return PARSE_FILENAME_SUCCESS;
414 /* Parses a line of data (not an empty line or comment) in the source list file
415 * format. (See the man page for 'imagex capture' for details on this format
418 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
419 * len == 0. The data in @line will be modified by this function call.
421 * @len: Length of the line of data.
423 * @source: On success, the capture source and target described by the line is
424 * written into this destination. Note that it will contain pointers
425 * to data in the @line array.
427 * Returns true if the line was valid; false otherwise. */
429 parse_source_list_line(char *line, size_t len,
430 struct wimlib_capture_source *source)
434 ret = parse_filename(&line, &len, &source->fs_source_path);
435 if (ret != PARSE_FILENAME_SUCCESS)
437 ret = parse_filename(&line, &len, &source->wim_target_path);
438 if (ret == PARSE_FILENAME_NONE)
439 source->wim_target_path = source->fs_source_path;
440 return ret != PARSE_FILENAME_FAILURE;
443 /* Returns %true if the given line of length @len > 0 is a comment or empty line
444 * in the source list file format. */
445 static bool is_comment_line(const char *line, size_t len)
450 if (!isspace(*line) && *line != '\0')
459 /* Parses a file in the source list format. (See the man page for 'imagex
460 * capture' for details on this format and the meaning.)
462 * @source_list_contents: Contents of the source list file. Note that this
463 * buffer will be modified to save memory allocations,
464 * and cannot be freed until the returned array of
465 * wimlib_capture_source's has also been freed.
467 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
470 * @nsources_ret: On success, the length of the returned array is
473 * Returns: An array of `struct wimlib_capture_source's that can be passed to
474 * the wimlib_add_image_multisource() function to specify how a WIM image is to
476 static struct wimlib_capture_source *
477 parse_source_list(char *source_list_contents, size_t source_list_nbytes,
478 size_t *nsources_ret)
482 struct wimlib_capture_source *sources;
486 for (i = 0; i < source_list_nbytes; i++)
487 if (source_list_contents[i] == '\n')
489 sources = calloc(nlines, sizeof(*sources));
491 imagex_error("out of memory");
494 p = source_list_contents;
496 for (i = 0; i < nlines; i++) {
497 char *endp = strchr(p, '\n');
498 size_t len = endp - p + 1;
500 if (!is_comment_line(p, len)) {
501 if (!parse_source_list_line(p, len, &sources[j++])) {
513 /* Reads the contents of a file into memory. */
514 static char *file_get_contents(const char *filename, size_t *len_ret)
521 if (stat(filename, &stbuf) != 0) {
522 imagex_error_with_errno("Failed to stat the file `%s'", filename);
527 fp = fopen(filename, "rb");
529 imagex_error_with_errno("Failed to open the file `%s'", filename);
535 imagex_error("Failed to allocate buffer of %zu bytes to hold "
536 "contents of file `%s'", len, filename);
539 if (fread(buf, 1, len, fp) != len) {
540 imagex_error_with_errno("Failed to read %lu bytes from the "
541 "file `%s'", len, filename);
555 /* Return 0 if a path names a file to which the current user has write access;
556 * -1 otherwise (and print an error message). */
557 static int file_writable(const char *path)
560 ret = access(path, W_OK);
562 imagex_error_with_errno("Can't modify `%s'", path);
566 #define TO_PERCENT(numerator, denominator) \
567 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
569 /* Given an enumerated value for WIM compression type, return a descriptive
571 static const char *get_data_type(int ctype)
574 case WIMLIB_COMPRESSION_TYPE_NONE:
575 return "uncompressed";
576 case WIMLIB_COMPRESSION_TYPE_LZX:
577 return "LZX-compressed";
578 case WIMLIB_COMPRESSION_TYPE_XPRESS:
579 return "XPRESS-compressed";
584 /* Progress callback function passed to various wimlib functions. */
585 static int imagex_progress_func(enum wimlib_progress_msg msg,
586 const union wimlib_progress_info *info)
588 unsigned percent_done;
590 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
591 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
592 info->write_streams.total_bytes);
593 if (info->write_streams.completed_streams == 0) {
594 const char *data_type;
596 data_type = get_data_type(info->write_streams.compression_type);
597 printf("Writing %s data using %u thread%s\n",
598 data_type, info->write_streams.num_threads,
599 (info->write_streams.num_threads == 1) ? "" : "s");
601 printf("\r%"PRIu64" MiB of %"PRIu64" MiB (uncompressed) "
602 "written (%u%% done)",
603 info->write_streams.completed_bytes >> 20,
604 info->write_streams.total_bytes >> 20,
606 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
609 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
610 printf("Scanning `%s'", info->scan.source);
611 if (*info->scan.wim_target_path) {
612 printf(" (loading as WIM path: `/%s')...\n",
613 info->scan.wim_target_path);
615 printf(" (loading as root of WIM image)...\n");
618 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
619 if (info->scan.excluded)
620 printf("Excluding `%s' from capture\n", info->scan.cur_path);
622 printf("Scanning `%s'\n", info->scan.cur_path);
624 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
626 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
627 percent_done = TO_PERCENT(info->integrity.completed_bytes,
628 info->integrity.total_bytes);
629 printf("\rVerifying integrity of `%s': %"PRIu64" MiB "
630 "of %"PRIu64" MiB (%u%%) done",
631 info->integrity.filename,
632 info->integrity.completed_bytes >> 20,
633 info->integrity.total_bytes >> 20,
635 if (info->integrity.completed_bytes == info->integrity.total_bytes)
638 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
639 percent_done = TO_PERCENT(info->integrity.completed_bytes,
640 info->integrity.total_bytes);
641 printf("\rCalculating integrity table for WIM: %"PRIu64" MiB "
642 "of %"PRIu64" MiB (%u%%) done",
643 info->integrity.completed_bytes >> 20,
644 info->integrity.total_bytes >> 20,
646 if (info->integrity.completed_bytes == info->integrity.total_bytes)
649 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
650 printf("Applying image %d (%s) from `%s' to %s `%s'\n",
652 info->extract.image_name,
653 info->extract.wimfile_name,
654 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
655 "NTFS volume" : "directory"),
656 info->extract.target);
658 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
659 /*printf("Applying directory structure to %s\n",*/
660 /*info->extract.target);*/
662 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
663 percent_done = TO_PERCENT(info->extract.completed_bytes,
664 info->extract.total_bytes);
665 printf("\rExtracting files: "
666 "%"PRIu64" MiB of %"PRIu64" MiB (%u%%) done",
667 info->extract.completed_bytes >> 20,
668 info->extract.total_bytes >> 20,
670 if (info->extract.completed_bytes >= info->extract.total_bytes)
673 case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
674 puts(info->extract.cur_path);
676 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
677 printf("Setting timestamps on all extracted files...\n");
679 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
680 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
681 printf("Unmounting NTFS volume `%s'...\n",
682 info->extract.target);
685 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
686 percent_done = TO_PERCENT(info->join.completed_bytes,
687 info->join.total_bytes);
688 printf("Writing resources from part %u of %u: "
689 "%"PRIu64 " MiB of %"PRIu64" MiB (%u%%) written\n",
690 (info->join.completed_parts == info->join.total_parts) ?
691 info->join.completed_parts : info->join.completed_parts + 1,
692 info->join.total_parts,
693 info->join.completed_bytes >> 20,
694 info->join.total_bytes >> 20,
697 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
698 percent_done = TO_PERCENT(info->split.completed_bytes,
699 info->split.total_bytes);
700 printf("Writing `%s': %"PRIu64" MiB of %"PRIu64" MiB (%u%%) written\n",
701 info->split.part_name,
702 info->split.completed_bytes >> 20,
703 info->split.total_bytes >> 20,
706 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
707 if (info->split.completed_bytes == info->split.total_bytes) {
708 printf("Finished writing %u split WIM parts\n",
709 info->split.cur_part_number);
719 /* Open all the split WIM parts that correspond to a file glob.
721 * @first_part specifies the first part of the split WIM and it may be either
722 * included or omitted from the glob. */
723 static int open_swms_from_glob(const char *swm_glob,
724 const char *first_part,
726 WIMStruct ***additional_swms_ret,
727 unsigned *num_additional_swms_ret)
729 unsigned num_additional_swms = 0;
730 WIMStruct **additional_swms = NULL;
734 ret = glob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
736 if (ret == GLOB_NOMATCH) {
737 imagex_error("Found no files for glob \"%s\"",
740 imagex_error_with_errno("Failed to process glob "
746 num_additional_swms = globbuf.gl_pathc;
747 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
748 if (!additional_swms) {
749 imagex_error("Out of memory");
754 for (unsigned i = 0; i < num_additional_swms; i++) {
755 if (strcmp(globbuf.gl_pathv[i], first_part) == 0) {
759 ret = wimlib_open_wim(globbuf.gl_pathv[i],
760 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
761 &additional_swms[i - offset],
762 imagex_progress_func);
766 *additional_swms_ret = additional_swms;
767 *num_additional_swms_ret = num_additional_swms - offset;
771 for (unsigned i = 0; i < num_additional_swms; i++)
772 wimlib_free(additional_swms[i]);
773 free(additional_swms);
781 static unsigned parse_num_threads(const char *optarg)
784 unsigned nthreads = strtoul(optarg, &tmp, 10);
785 if (nthreads == UINT_MAX || *tmp || tmp == optarg) {
786 imagex_error("Number of threads must be a non-negative integer!");
794 /* Apply one image, or all images, from a WIM file into a directory, OR apply
795 * one image from a WIM file to a NTFS volume. */
796 static int imagex_apply(int argc, char **argv)
799 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
806 const char *image_num_or_name;
807 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
809 const char *swm_glob = NULL;
810 WIMStruct **additional_swms = NULL;
811 unsigned num_additional_swms = 0;
813 for_opt(c, apply_options) {
816 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
819 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
822 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
825 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
831 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
840 if (argc != 2 && argc != 3) {
847 image_num_or_name = "1";
850 image_num_or_name = argv[1];
854 ret = wimlib_open_wim(wimfile, open_flags, &w, imagex_progress_func);
858 image = wimlib_resolve_image(w, image_num_or_name);
859 ret = verify_image_exists(image, image_num_or_name, wimfile);
863 num_images = wimlib_get_num_images(w);
864 if (argc == 2 && num_images != 1) {
865 imagex_error("`%s' contains %d images; Please select one "
866 "(or all)", wimfile, num_images);
873 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
875 &num_additional_swms);
882 ret = stat(target, &stbuf);
884 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
885 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
887 if (errno != ENOENT) {
888 imagex_error_with_errno("Failed to stat `%s'", target);
894 ret = wimlib_extract_image(w, image, target, extract_flags,
895 additional_swms, num_additional_swms,
896 imagex_progress_func);
898 printf("Done applying WIM image.\n");
901 if (additional_swms) {
902 for (unsigned i = 0; i < num_additional_swms; i++)
903 wimlib_free(additional_swms[i]);
904 free(additional_swms);
909 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
910 * directory trees. 'imagex capture': create a new WIM file containing the
911 * desired image. 'imagex append': add a new image to an existing WIM file. */
912 static int imagex_capture_or_append(int argc, char **argv)
916 int add_image_flags = 0;
918 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
922 const char *flags_element = NULL;
926 int cmd = strcmp(argv[0], "append") ? CAPTURE : APPEND;
927 unsigned num_threads = 0;
930 size_t source_name_len;
933 const char *config_file = NULL;
934 char *config_str = NULL;
937 bool source_list = false;
938 size_t source_list_nbytes;
939 char *source_list_contents = NULL;
940 bool capture_sources_malloced = false;
941 struct wimlib_capture_source *capture_sources;
944 for_opt(c, capture_or_append_options) {
947 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
950 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
951 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
954 config_file = optarg;
957 compression_type = get_compression_type(optarg);
958 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
962 flags_element = optarg;
965 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
968 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
971 num_threads = parse_num_threads(optarg);
972 if (num_threads == UINT_MAX)
976 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
979 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
992 if (argc < 2 || argc > 4) {
1003 /* Set default name to SOURCE argument, omitting any directory
1004 * prefixes and trailing slashes. This requires making a copy
1006 source_name_len = strlen(source);
1007 source_copy = alloca(source_name_len + 1);
1008 name = basename(strcpy(source_copy, source));
1010 /* Image description defaults to NULL if not given. */
1011 desc = (argc >= 4) ? argv[3] : NULL;
1014 /* Set up capture sources in source list mode */
1015 if (source[0] == '-' && source[1] == '\0') {
1016 source_list_contents = stdin_get_contents(&source_list_nbytes);
1018 source_list_contents = file_get_contents(source,
1019 &source_list_nbytes);
1021 if (!source_list_contents)
1024 capture_sources = parse_source_list(source_list_contents,
1027 if (!capture_sources) {
1031 capture_sources_malloced = true;
1033 /* Set up capture source in non-source-list mode (could be
1034 * either "normal" mode or "NTFS mode"--- see the man page). */
1035 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1036 capture_sources[0].fs_source_path = source;
1037 capture_sources[0].wim_target_path = NULL;
1038 capture_sources[0].reserved = 0;
1043 config_str = file_get_contents(config_file, &config_len);
1051 ret = wimlib_open_wim(wimfile, open_flags, &w,
1052 imagex_progress_func);
1054 ret = wimlib_create_new_wim(compression_type, &w);
1060 ret = stat(source, &stbuf);
1062 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1063 printf("Capturing WIM image from NTFS filesystem on `%s'\n",
1065 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1068 if (errno != ENOENT) {
1069 imagex_error_with_errno("Failed to stat `%s'", source);
1076 ret = wimlib_add_image_multisource(w, capture_sources,
1078 (config_str ? config_str :
1079 default_capture_config),
1080 (config_str ? config_len :
1081 strlen(default_capture_config)),
1083 imagex_progress_func);
1086 cur_image = wimlib_get_num_images(w);
1088 ret = wimlib_set_image_descripton(w, cur_image, desc);
1092 if (flags_element) {
1093 ret = wimlib_set_image_flags(w, cur_image, flags_element);
1097 if (cmd == APPEND) {
1098 ret = wimlib_overwrite(w, write_flags, num_threads,
1099 imagex_progress_func);
1101 ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
1102 num_threads, imagex_progress_func);
1104 if (ret == WIMLIB_ERR_REOPEN)
1107 imagex_error("Failed to write the WIM file `%s'", wimfile);
1111 free(source_list_contents);
1112 if (capture_sources_malloced)
1113 free(capture_sources);
1117 /* Remove image(s) from a WIM. */
1118 static int imagex_delete(int argc, char **argv)
1122 int write_flags = 0;
1123 const char *wimfile;
1124 const char *image_num_or_name;
1129 for_opt(c, delete_options) {
1132 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1133 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1136 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
1148 imagex_error("Must specify a WIM file");
1150 imagex_error("Must specify an image");
1155 image_num_or_name = argv[1];
1157 ret = file_writable(wimfile);
1161 ret = wimlib_open_wim(wimfile, open_flags, &w,
1162 imagex_progress_func);
1166 image = wimlib_resolve_image(w, image_num_or_name);
1168 ret = verify_image_exists(image, image_num_or_name, wimfile);
1172 ret = wimlib_delete_image(w, image);
1174 imagex_error("Failed to delete image from `%s'", wimfile);
1178 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1179 if (ret == WIMLIB_ERR_REOPEN)
1182 imagex_error("Failed to write the file `%s' with image "
1183 "deleted", wimfile);
1190 /* Print the files contained in an image(s) in a WIM file. */
1191 static int imagex_dir(int argc, char **argv)
1193 const char *wimfile;
1200 imagex_error("Must specify a WIM file");
1205 imagex_error("Too many arguments");
1211 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w,
1212 imagex_progress_func);
1217 image = wimlib_resolve_image(w, argv[2]);
1218 ret = verify_image_exists(image, argv[2], wimfile);
1222 /* Image was not specified. If the WIM only contains one image,
1223 * choose that one; otherwise, print an error. */
1224 num_images = wimlib_get_num_images(w);
1225 if (num_images != 1) {
1226 imagex_error("The file `%s' contains %d images; Please "
1227 "select one.", wimfile, num_images);
1235 ret = wimlib_print_files(w, image);
1241 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
1243 static int imagex_export(int argc, char **argv)
1247 int export_flags = 0;
1248 int write_flags = 0;
1249 int compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
1250 bool compression_type_specified = false;
1251 const char *src_wimfile;
1252 const char *src_image_num_or_name;
1253 const char *dest_wimfile;
1254 const char *dest_name;
1255 const char *dest_desc;
1256 WIMStruct *src_w = NULL;
1257 WIMStruct *dest_w = NULL;
1262 const char *swm_glob = NULL;
1263 WIMStruct **additional_swms = NULL;
1264 unsigned num_additional_swms = 0;
1265 unsigned num_threads = 0;
1267 for_opt(c, export_options) {
1270 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
1273 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1274 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1277 compression_type = get_compression_type(optarg);
1278 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1280 compression_type_specified = true;
1286 num_threads = parse_num_threads(optarg);
1287 if (num_threads == UINT_MAX)
1291 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1300 if (argc < 3 || argc > 5) {
1304 src_wimfile = argv[0];
1305 src_image_num_or_name = argv[1];
1306 dest_wimfile = argv[2];
1307 dest_name = (argc >= 4) ? argv[3] : NULL;
1308 dest_desc = (argc >= 5) ? argv[4] : NULL;
1309 ret = wimlib_open_wim(src_wimfile,
1310 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w,
1311 imagex_progress_func);
1315 /* Determine if the destination is an existing file or not.
1316 * If so, we try to append the exported image(s) to it; otherwise, we
1317 * create a new WIM containing the exported image(s). */
1318 if (stat(dest_wimfile, &stbuf) == 0) {
1322 /* Destination file exists. */
1323 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
1324 imagex_error("`%s' is not a regular file",
1329 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w,
1330 imagex_progress_func);
1334 ret = file_writable(dest_wimfile);
1338 dest_ctype = wimlib_get_compression_type(dest_w);
1339 if (compression_type_specified
1340 && compression_type != dest_ctype)
1342 imagex_error("Cannot specify a compression type that is "
1343 "not the same as that used in the "
1350 /* dest_wimfile is not an existing file, so create a new WIM. */
1351 if (!compression_type_specified)
1352 compression_type = wimlib_get_compression_type(src_w);
1353 if (errno == ENOENT) {
1354 ret = wimlib_create_new_wim(compression_type, &dest_w);
1358 imagex_error_with_errno("Cannot stat file `%s'",
1365 image = wimlib_resolve_image(src_w, src_image_num_or_name);
1366 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
1371 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
1373 &num_additional_swms);
1378 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
1379 export_flags, additional_swms,
1380 num_additional_swms, imagex_progress_func);
1386 ret = wimlib_write(dest_w, dest_wimfile, WIMLIB_ALL_IMAGES,
1387 write_flags, num_threads,
1388 imagex_progress_func);
1390 ret = wimlib_overwrite(dest_w, write_flags, num_threads,
1391 imagex_progress_func);
1393 if (ret == WIMLIB_ERR_REOPEN)
1396 wimlib_free(dest_w);
1397 if (additional_swms) {
1398 for (unsigned i = 0; i < num_additional_swms; i++)
1399 wimlib_free(additional_swms[i]);
1400 free(additional_swms);
1405 /* Prints information about a WIM file; also can mark an image as bootable,
1406 * change the name of an image, or change the description of an image. */
1407 static int imagex_info(int argc, char **argv)
1412 bool header = false;
1413 bool lookup_table = false;
1415 bool metadata = false;
1416 bool short_header = true;
1417 const char *xml_out_file = NULL;
1418 const char *wimfile;
1419 const char *image_num_or_name = "all";
1420 const char *new_name = NULL;
1421 const char *new_desc = NULL;
1426 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1431 for_opt(c, info_options) {
1441 short_header = false;
1444 lookup_table = true;
1445 short_header = false;
1449 short_header = false;
1452 xml_out_file = optarg;
1453 short_header = false;
1457 short_header = false;
1467 if (argc == 0 || argc > 4) {
1473 image_num_or_name = argv[1];
1483 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1485 ret = wimlib_open_wim(wimfile, open_flags, &w,
1486 imagex_progress_func);
1490 part_number = wimlib_get_part_number(w, &total_parts);
1492 image = wimlib_resolve_image(w, image_num_or_name);
1493 if (image == WIMLIB_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
1494 imagex_error("The image `%s' does not exist",
1497 imagex_error("If you would like to set the boot "
1498 "index to 0, specify image \"0\" with "
1499 "the --boot flag.");
1500 ret = WIMLIB_ERR_INVALID_IMAGE;
1504 num_images = wimlib_get_num_images(w);
1506 if (num_images == 0) {
1508 imagex_error("--boot is meaningless on a WIM with no "
1510 ret = WIMLIB_ERR_INVALID_IMAGE;
1515 if (image == WIMLIB_ALL_IMAGES && num_images > 1) {
1517 imagex_error("Cannot specify the --boot flag "
1518 "without specifying a specific "
1519 "image in a multi-image WIM");
1520 ret = WIMLIB_ERR_INVALID_IMAGE;
1524 imagex_error("Cannot specify the NEW_NAME "
1525 "without specifying a specific "
1526 "image in a multi-image WIM");
1527 ret = WIMLIB_ERR_INVALID_IMAGE;
1532 /* Operations that print information are separated from operations that
1533 * recreate the WIM file. */
1534 if (!new_name && !boot) {
1536 /* Read-only operations */
1538 if (image == WIMLIB_NO_IMAGE) {
1539 imagex_error("`%s' is not a valid image",
1541 ret = WIMLIB_ERR_INVALID_IMAGE;
1545 if (image == WIMLIB_ALL_IMAGES && short_header)
1546 wimlib_print_wim_information(w);
1549 wimlib_print_header(w);
1552 if (total_parts != 1) {
1553 printf("Warning: Only showing the lookup table "
1554 "for part %d of a %d-part WIM.\n",
1555 part_number, total_parts);
1557 wimlib_print_lookup_table(w);
1561 ret = wimlib_extract_xml_data(w, stdout);
1567 fp = fopen(xml_out_file, "wb");
1569 imagex_error_with_errno("Failed to open the "
1576 ret = wimlib_extract_xml_data(w, fp);
1577 if (fclose(fp) != 0) {
1578 imagex_error("Failed to close the file `%s'",
1588 wimlib_print_available_images(w, image);
1591 ret = wimlib_print_metadata(w, image);
1597 /* Modification operations */
1598 if (total_parts != 1) {
1599 imagex_error("Modifying a split WIM is not supported.");
1603 if (image == WIMLIB_ALL_IMAGES)
1606 if (image == WIMLIB_NO_IMAGE && new_name) {
1607 imagex_error("Cannot specify new_name (`%s') when "
1608 "using image 0", new_name);
1614 if (image == wimlib_get_boot_idx(w)) {
1615 printf("Image %d is already marked as "
1616 "bootable.\n", image);
1619 printf("Marking image %d as bootable.\n",
1621 wimlib_set_boot_idx(w, image);
1625 if (strcmp(wimlib_get_image_name(w, image),
1627 printf("Image %d is already named \"%s\".\n",
1631 printf("Changing the name of image %d to "
1632 "\"%s\".\n", image, new_name);
1633 ret = wimlib_set_image_name(w, image, new_name);
1639 const char *old_desc;
1640 old_desc = wimlib_get_image_description(w, image);
1641 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1642 printf("The description of image %d is already "
1643 "\"%s\".\n", image, new_desc);
1646 printf("Changing the description of image %d "
1647 "to \"%s\".\n", image, new_desc);
1648 ret = wimlib_set_image_descripton(w, image,
1655 /* Only call wimlib_overwrite() if something actually needs to
1657 if (boot || new_name || new_desc ||
1658 (check && !wimlib_has_integrity_table(w)))
1662 ret = file_writable(wimfile);
1667 write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1671 ret = wimlib_overwrite(w, write_flags, 1,
1672 imagex_progress_func);
1673 if (ret == WIMLIB_ERR_REOPEN)
1676 printf("The file `%s' was not modified because nothing "
1677 "needed to be done.\n", wimfile);
1686 /* Join split WIMs into one part WIM */
1687 static int imagex_join(int argc, char **argv)
1690 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1691 int wim_write_flags = 0;
1692 const char *output_path;
1694 for_opt(c, join_options) {
1697 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1698 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1708 imagex_error("Must specify one or more split WIM (.swm) parts "
1712 output_path = argv[0];
1713 return wimlib_join((const char **)++argv, --argc, output_path,
1714 swm_open_flags, wim_write_flags,
1715 imagex_progress_func);
1721 /* Mounts an image using a FUSE mount. */
1722 static int imagex_mount_rw_or_ro(int argc, char **argv)
1725 int mount_flags = 0;
1726 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1727 const char *wimfile;
1733 const char *swm_glob = NULL;
1734 WIMStruct **additional_swms = NULL;
1735 unsigned num_additional_swms = 0;
1736 const char *staging_dir = NULL;
1738 if (strcmp(argv[0], "mountrw") == 0)
1739 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1741 for_opt(c, mount_options) {
1744 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
1747 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1750 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1753 if (strcasecmp(optarg, "none") == 0)
1754 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1755 else if (strcasecmp(optarg, "xattr") == 0)
1756 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1757 else if (strcasecmp(optarg, "windows") == 0)
1758 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1760 imagex_error("Unknown stream interface \"%s\"", optarg);
1768 staging_dir = optarg;
1771 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
1779 if (argc != 2 && argc != 3)
1784 ret = wimlib_open_wim(wimfile, open_flags, &w,
1785 imagex_progress_func);
1790 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1792 &num_additional_swms);
1799 num_images = wimlib_get_num_images(w);
1800 if (num_images != 1) {
1801 imagex_error("The file `%s' contains %d images; Please "
1802 "select one.", wimfile, num_images);
1803 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1810 image = wimlib_resolve_image(w, argv[1]);
1812 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
1817 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1818 ret = file_writable(wimfile);
1823 ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
1824 num_additional_swms, staging_dir);
1826 imagex_error("Failed to mount image %d from `%s' on `%s'",
1827 image, wimfile, dir);
1832 if (additional_swms) {
1833 for (unsigned i = 0; i < num_additional_swms; i++)
1834 wimlib_free(additional_swms[i]);
1835 free(additional_swms);
1839 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1844 /* Rebuild a WIM file */
1845 static int imagex_optimize(int argc, char **argv)
1849 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
1852 const char *wimfile;
1856 for_opt(c, optimize_options) {
1859 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1860 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1863 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1880 ret = wimlib_open_wim(wimfile, open_flags, &w,
1881 imagex_progress_func);
1885 old_size = file_get_size(argv[0]);
1886 printf("`%s' original size: ", wimfile);
1890 printf("%"PRIu64" KiB\n", old_size >> 10);
1892 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1895 new_size = file_get_size(argv[0]);
1896 printf("`%s' optimized size: ", wimfile);
1900 printf("%"PRIu64" KiB\n", new_size >> 10);
1902 fputs("Space saved: ", stdout);
1903 if (new_size != -1 && old_size != -1) {
1904 printf("%lld KiB\n",
1905 ((long long)old_size - (long long)new_size) >> 10);
1915 /* Split a WIM into a spanned set */
1916 static int imagex_split(int argc, char **argv)
1919 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1920 int write_flags = 0;
1921 unsigned long part_size;
1926 for_opt(c, split_options) {
1929 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1930 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1944 part_size = strtod(argv[2], &tmp) * (1 << 20);
1945 if (tmp == argv[2] || *tmp) {
1946 imagex_error("Invalid part size \"%s\"", argv[2]);
1947 imagex_error("The part size must be an integer or floating-point number of megabytes.");
1950 ret = wimlib_open_wim(argv[0], open_flags, &w, imagex_progress_func);
1953 ret = wimlib_split(w, argv[1], part_size, write_flags, imagex_progress_func);
1958 /* Unmounts a mounted WIM image. */
1959 static int imagex_unmount(int argc, char **argv)
1962 int unmount_flags = 0;
1965 for_opt(c, unmount_options) {
1968 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1971 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1974 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
1988 ret = wimlib_unmount_image(argv[0], unmount_flags,
1989 imagex_progress_func);
1991 imagex_error("Failed to unmount `%s'", argv[0]);
1995 struct imagex_command {
1997 int (*func)(int , char **);
2002 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
2003 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
2005 static const struct imagex_command imagex_commands[] = {
2006 {"append", imagex_capture_or_append, APPEND},
2007 {"apply", imagex_apply, APPLY},
2008 {"capture", imagex_capture_or_append, CAPTURE},
2009 {"delete", imagex_delete, DELETE},
2010 {"dir", imagex_dir, DIR},
2011 {"export", imagex_export, EXPORT},
2012 {"info", imagex_info, INFO},
2013 {"join", imagex_join, JOIN},
2014 {"mount", imagex_mount_rw_or_ro, MOUNT},
2015 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
2016 {"optimize",imagex_optimize, OPTIMIZE},
2017 {"split", imagex_split, SPLIT},
2018 {"unmount", imagex_unmount, UNMOUNT},
2021 static void version()
2023 static const char *s =
2024 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
2025 "Copyright (C) 2012, 2013 Eric Biggers\n"
2026 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
2027 "This is free software: you are free to change and redistribute it.\n"
2028 "There is NO WARRANTY, to the extent permitted by law.\n"
2030 "Report bugs to "PACKAGE_BUGREPORT".\n";
2035 static void help_or_version(int argc, char **argv)
2039 const struct imagex_command *cmd;
2041 for (i = 1; i < argc; i++) {
2049 if (strcmp(p, "help") == 0) {
2050 for_imagex_command(cmd) {
2051 if (strcmp(cmd->name, argv[1]) == 0) {
2059 if (strcmp(p, "version") == 0) {
2067 static void usage(int cmd_type)
2069 const struct imagex_command *cmd;
2070 printf("Usage: %s", usage_strings[cmd_type]);
2071 for_imagex_command(cmd) {
2072 if (cmd->cmd == cmd_type)
2073 printf("\nTry `man imagex-%s' for more details.\n",
2078 static void usage_all()
2080 puts("IMAGEX: Usage:");
2081 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
2082 printf(" %s", usage_strings[i]);
2083 static const char *extra =
2085 " imagex --version\n"
2087 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
2089 " Try `man imagex' for more information.\n"
2091 fputs(extra, stdout);
2094 /* Entry point for the 'imagex' program. */
2095 int main(int argc, char **argv)
2097 const struct imagex_command *cmd;
2101 imagex_error("No command specified");
2106 /* Handle --help and --version for all commands. Note that this will
2107 * not return if either of these arguments are present. */
2108 help_or_version(argc, argv);
2112 /* The user may like to see more informative error messages. */
2113 wimlib_set_print_errors(true);
2115 /* Calling wimlib_global_init() is not strictly necessary because
2116 * 'imagex' is single-threaded. */
2117 ret = wimlib_global_init();
2121 /* Search for the function to handle the 'imagex' subcommand. */
2122 for_imagex_command(cmd) {
2123 if (strcmp(cmd->name, *argv) == 0) {
2124 ret = cmd->func(argc, argv);
2129 imagex_error("Unrecognized command: `%s'", argv[0]);
2133 /* For 'imagex info' and 'imagex dir', data printed to standard output
2134 * is part of the program's actual behavior and not just for
2135 * informational purposes, so we should set a failure exit status if
2136 * there was a write error. */
2137 if (cmd == &imagex_commands[INFO] || cmd == &imagex_commands[DIR]) {
2138 if (ferror(stdout) || fclose(stdout)) {
2139 imagex_error_with_errno("output error");
2145 /* Exit status (ret): -1 indicates an error found by 'imagex' outside
2146 * of the wimlib library code. 0 indicates success. > 0 indicates a
2147 * wimlib error code from which an error message can be printed. */
2149 imagex_error("Exiting with error code %d:\n"
2151 wimlib_get_error_string(ret));
2152 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
2153 imagex_error_with_errno("errno");
2155 /* Calling wimlib_global_cleanup() is not strictly necessary because the
2156 * process is exiting anyway. */
2157 wimlib_global_cleanup();