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 /* XXX: Could use rawmemchr() here instead, but it may not be
498 * available on all platforms. */
499 char *endp = memchr(p, '\n', source_list_nbytes);
500 size_t len = endp - p + 1;
502 if (!is_comment_line(p, len)) {
503 if (!parse_source_list_line(p, len, &sources[j++])) {
515 /* Reads the contents of a file into memory. */
516 static char *file_get_contents(const char *filename, size_t *len_ret)
523 if (stat(filename, &stbuf) != 0) {
524 imagex_error_with_errno("Failed to stat the file `%s'", filename);
529 fp = fopen(filename, "rb");
531 imagex_error_with_errno("Failed to open the file `%s'", filename);
537 imagex_error("Failed to allocate buffer of %zu bytes to hold "
538 "contents of file `%s'", len, filename);
541 if (fread(buf, 1, len, fp) != len) {
542 imagex_error_with_errno("Failed to read %lu bytes from the "
543 "file `%s'", len, filename);
557 /* Return 0 if a path names a file to which the current user has write access;
558 * -1 otherwise (and print an error message). */
559 static int file_writable(const char *path)
562 ret = access(path, W_OK);
564 imagex_error_with_errno("Can't modify `%s'", path);
568 #define TO_PERCENT(numerator, denominator) \
569 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
571 /* Given an enumerated value for WIM compression type, return a descriptive
573 static const char *get_data_type(int ctype)
576 case WIMLIB_COMPRESSION_TYPE_NONE:
577 return "uncompressed";
578 case WIMLIB_COMPRESSION_TYPE_LZX:
579 return "LZX-compressed";
580 case WIMLIB_COMPRESSION_TYPE_XPRESS:
581 return "XPRESS-compressed";
586 /* Progress callback function passed to various wimlib functions. */
587 static int imagex_progress_func(enum wimlib_progress_msg msg,
588 const union wimlib_progress_info *info)
590 unsigned percent_done;
592 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
593 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
594 info->write_streams.total_bytes);
595 if (info->write_streams.completed_streams == 0) {
596 const char *data_type;
598 data_type = get_data_type(info->write_streams.compression_type);
599 printf("Writing %s data using %u thread%s\n",
600 data_type, info->write_streams.num_threads,
601 (info->write_streams.num_threads == 1) ? "" : "s");
603 printf("\r%"PRIu64" MiB of %"PRIu64" MiB (uncompressed) "
604 "written (%u%% done)",
605 info->write_streams.completed_bytes >> 20,
606 info->write_streams.total_bytes >> 20,
608 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
611 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
612 printf("Scanning `%s'", info->scan.source);
613 if (*info->scan.wim_target_path) {
614 printf(" (loading as WIM path: `/%s')...\n",
615 info->scan.wim_target_path);
617 printf(" (loading as root of WIM image)...\n");
620 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
621 if (info->scan.excluded)
622 printf("Excluding `%s' from capture\n", info->scan.cur_path);
624 printf("Scanning `%s'\n", info->scan.cur_path);
626 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
628 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
629 percent_done = TO_PERCENT(info->integrity.completed_bytes,
630 info->integrity.total_bytes);
631 printf("\rVerifying integrity of `%s': %"PRIu64" MiB "
632 "of %"PRIu64" MiB (%u%%) done",
633 info->integrity.filename,
634 info->integrity.completed_bytes >> 20,
635 info->integrity.total_bytes >> 20,
637 if (info->integrity.completed_bytes == info->integrity.total_bytes)
640 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
641 percent_done = TO_PERCENT(info->integrity.completed_bytes,
642 info->integrity.total_bytes);
643 printf("\rCalculating integrity table for WIM: %"PRIu64" MiB "
644 "of %"PRIu64" MiB (%u%%) done",
645 info->integrity.completed_bytes >> 20,
646 info->integrity.total_bytes >> 20,
648 if (info->integrity.completed_bytes == info->integrity.total_bytes)
651 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
652 printf("Applying image %d (%s) from `%s' to %s `%s'\n",
654 info->extract.image_name,
655 info->extract.wimfile_name,
656 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
657 "NTFS volume" : "directory"),
658 info->extract.target);
660 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
661 /*printf("Applying directory structure to %s\n",*/
662 /*info->extract.target);*/
664 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
665 percent_done = TO_PERCENT(info->extract.completed_bytes,
666 info->extract.total_bytes);
667 printf("\rExtracting files: "
668 "%"PRIu64" MiB of %"PRIu64" MiB (%u%%) done",
669 info->extract.completed_bytes >> 20,
670 info->extract.total_bytes >> 20,
672 if (info->extract.completed_bytes >= info->extract.total_bytes)
675 case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
676 puts(info->extract.cur_path);
678 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
679 printf("Setting timestamps on all extracted files...\n");
681 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
682 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
683 printf("Unmounting NTFS volume `%s'...\n",
684 info->extract.target);
687 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
688 percent_done = TO_PERCENT(info->join.completed_bytes,
689 info->join.total_bytes);
690 printf("Writing resources from part %u of %u: "
691 "%"PRIu64 " MiB of %"PRIu64" MiB (%u%%) written\n",
692 (info->join.completed_parts == info->join.total_parts) ?
693 info->join.completed_parts : info->join.completed_parts + 1,
694 info->join.total_parts,
695 info->join.completed_bytes >> 20,
696 info->join.total_bytes >> 20,
699 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
700 percent_done = TO_PERCENT(info->split.completed_bytes,
701 info->split.total_bytes);
702 printf("Writing `%s': %"PRIu64" MiB of %"PRIu64" MiB (%u%%) written\n",
703 info->split.part_name,
704 info->split.completed_bytes >> 20,
705 info->split.total_bytes >> 20,
708 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
709 if (info->split.completed_bytes == info->split.total_bytes) {
710 printf("Finished writing %u split WIM parts\n",
711 info->split.cur_part_number);
721 /* Open all the split WIM parts that correspond to a file glob.
723 * @first_part specifies the first part of the split WIM and it may be either
724 * included or omitted from the glob. */
725 static int open_swms_from_glob(const char *swm_glob,
726 const char *first_part,
728 WIMStruct ***additional_swms_ret,
729 unsigned *num_additional_swms_ret)
731 unsigned num_additional_swms = 0;
732 WIMStruct **additional_swms = NULL;
736 ret = glob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
738 if (ret == GLOB_NOMATCH) {
739 imagex_error("Found no files for glob \"%s\"",
742 imagex_error_with_errno("Failed to process glob "
748 num_additional_swms = globbuf.gl_pathc;
749 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
750 if (!additional_swms) {
751 imagex_error("Out of memory");
756 for (unsigned i = 0; i < num_additional_swms; i++) {
757 if (strcmp(globbuf.gl_pathv[i], first_part) == 0) {
761 ret = wimlib_open_wim(globbuf.gl_pathv[i],
762 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
763 &additional_swms[i - offset],
764 imagex_progress_func);
768 *additional_swms_ret = additional_swms;
769 *num_additional_swms_ret = num_additional_swms - offset;
773 for (unsigned i = 0; i < num_additional_swms; i++)
774 wimlib_free(additional_swms[i]);
775 free(additional_swms);
783 static unsigned parse_num_threads(const char *optarg)
786 unsigned nthreads = strtoul(optarg, &tmp, 10);
787 if (nthreads == UINT_MAX || *tmp || tmp == optarg) {
788 imagex_error("Number of threads must be a non-negative integer!");
796 /* Apply one image, or all images, from a WIM file into a directory, OR apply
797 * one image from a WIM file to a NTFS volume. */
798 static int imagex_apply(int argc, char **argv)
801 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
808 const char *image_num_or_name;
809 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
811 const char *swm_glob = NULL;
812 WIMStruct **additional_swms = NULL;
813 unsigned num_additional_swms = 0;
815 for_opt(c, apply_options) {
818 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
821 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
824 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
827 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
833 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
842 if (argc != 2 && argc != 3) {
849 image_num_or_name = "1";
852 image_num_or_name = argv[1];
856 ret = wimlib_open_wim(wimfile, open_flags, &w, imagex_progress_func);
860 image = wimlib_resolve_image(w, image_num_or_name);
861 ret = verify_image_exists(image, image_num_or_name, wimfile);
865 num_images = wimlib_get_num_images(w);
866 if (argc == 2 && num_images != 1) {
867 imagex_error("`%s' contains %d images; Please select one "
868 "(or all)", wimfile, num_images);
875 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
877 &num_additional_swms);
884 ret = stat(target, &stbuf);
886 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
887 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
889 if (errno != ENOENT) {
890 imagex_error_with_errno("Failed to stat `%s'", target);
896 ret = wimlib_extract_image(w, image, target, extract_flags,
897 additional_swms, num_additional_swms,
898 imagex_progress_func);
900 printf("Done applying WIM image.\n");
903 if (additional_swms) {
904 for (unsigned i = 0; i < num_additional_swms; i++)
905 wimlib_free(additional_swms[i]);
906 free(additional_swms);
911 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
912 * directory trees. 'imagex capture': create a new WIM file containing the
913 * desired image. 'imagex append': add a new image to an existing WIM file. */
914 static int imagex_capture_or_append(int argc, char **argv)
918 int add_image_flags = 0;
920 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
924 const char *flags_element = NULL;
928 int cmd = strcmp(argv[0], "append") ? CAPTURE : APPEND;
929 unsigned num_threads = 0;
932 size_t source_name_len;
935 const char *config_file = NULL;
936 char *config_str = NULL;
939 bool source_list = false;
940 size_t source_list_nbytes;
941 char *source_list_contents = NULL;
942 bool capture_sources_malloced = false;
943 struct wimlib_capture_source *capture_sources;
946 for_opt(c, capture_or_append_options) {
949 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
952 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
953 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
956 config_file = optarg;
959 compression_type = get_compression_type(optarg);
960 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
964 flags_element = optarg;
967 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
970 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
973 num_threads = parse_num_threads(optarg);
974 if (num_threads == UINT_MAX)
978 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
981 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
994 if (argc < 2 || argc > 4) {
1005 /* Set default name to SOURCE argument, omitting any directory
1006 * prefixes and trailing slashes. This requires making a copy
1008 source_name_len = strlen(source);
1009 source_copy = alloca(source_name_len + 1);
1010 name = basename(strcpy(source_copy, source));
1012 /* Image description defaults to NULL if not given. */
1013 desc = (argc >= 4) ? argv[3] : NULL;
1016 /* Set up capture sources in source list mode */
1017 if (source[0] == '-' && source[1] == '\0') {
1018 source_list_contents = stdin_get_contents(&source_list_nbytes);
1020 source_list_contents = file_get_contents(source,
1021 &source_list_nbytes);
1023 if (!source_list_contents)
1026 capture_sources = parse_source_list(source_list_contents,
1029 if (!capture_sources) {
1033 capture_sources_malloced = true;
1035 /* Set up capture source in non-source-list mode (could be
1036 * either "normal" mode or "NTFS mode"--- see the man page). */
1037 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1038 capture_sources[0].fs_source_path = source;
1039 capture_sources[0].wim_target_path = NULL;
1040 capture_sources[0].reserved = 0;
1045 config_str = file_get_contents(config_file, &config_len);
1053 ret = wimlib_open_wim(wimfile, open_flags, &w,
1054 imagex_progress_func);
1056 ret = wimlib_create_new_wim(compression_type, &w);
1062 ret = stat(source, &stbuf);
1064 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1065 printf("Capturing WIM image from NTFS filesystem on `%s'\n",
1067 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1070 if (errno != ENOENT) {
1071 imagex_error_with_errno("Failed to stat `%s'", source);
1078 ret = wimlib_add_image_multisource(w, capture_sources,
1080 (config_str ? config_str :
1081 default_capture_config),
1082 (config_str ? config_len :
1083 strlen(default_capture_config)),
1085 imagex_progress_func);
1088 cur_image = wimlib_get_num_images(w);
1090 ret = wimlib_set_image_descripton(w, cur_image, desc);
1094 if (flags_element) {
1095 ret = wimlib_set_image_flags(w, cur_image, flags_element);
1099 if (cmd == APPEND) {
1100 ret = wimlib_overwrite(w, write_flags, num_threads,
1101 imagex_progress_func);
1103 ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
1104 num_threads, imagex_progress_func);
1106 if (ret == WIMLIB_ERR_REOPEN)
1109 imagex_error("Failed to write the WIM file `%s'", wimfile);
1113 free(source_list_contents);
1114 if (capture_sources_malloced)
1115 free(capture_sources);
1119 /* Remove image(s) from a WIM. */
1120 static int imagex_delete(int argc, char **argv)
1124 int write_flags = 0;
1125 const char *wimfile;
1126 const char *image_num_or_name;
1131 for_opt(c, delete_options) {
1134 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1135 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1138 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
1150 imagex_error("Must specify a WIM file");
1152 imagex_error("Must specify an image");
1157 image_num_or_name = argv[1];
1159 ret = file_writable(wimfile);
1163 ret = wimlib_open_wim(wimfile, open_flags, &w,
1164 imagex_progress_func);
1168 image = wimlib_resolve_image(w, image_num_or_name);
1170 ret = verify_image_exists(image, image_num_or_name, wimfile);
1174 ret = wimlib_delete_image(w, image);
1176 imagex_error("Failed to delete image from `%s'", wimfile);
1180 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1181 if (ret == WIMLIB_ERR_REOPEN)
1184 imagex_error("Failed to write the file `%s' with image "
1185 "deleted", wimfile);
1192 /* Print the files contained in an image(s) in a WIM file. */
1193 static int imagex_dir(int argc, char **argv)
1195 const char *wimfile;
1202 imagex_error("Must specify a WIM file");
1207 imagex_error("Too many arguments");
1213 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w,
1214 imagex_progress_func);
1219 image = wimlib_resolve_image(w, argv[2]);
1220 ret = verify_image_exists(image, argv[2], wimfile);
1224 /* Image was not specified. If the WIM only contains one image,
1225 * choose that one; otherwise, print an error. */
1226 num_images = wimlib_get_num_images(w);
1227 if (num_images != 1) {
1228 imagex_error("The file `%s' contains %d images; Please "
1229 "select one.", wimfile, num_images);
1237 ret = wimlib_print_files(w, image);
1243 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
1245 static int imagex_export(int argc, char **argv)
1249 int export_flags = 0;
1250 int write_flags = 0;
1251 int compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
1252 bool compression_type_specified = false;
1253 const char *src_wimfile;
1254 const char *src_image_num_or_name;
1255 const char *dest_wimfile;
1256 const char *dest_name;
1257 const char *dest_desc;
1258 WIMStruct *src_w = NULL;
1259 WIMStruct *dest_w = NULL;
1264 const char *swm_glob = NULL;
1265 WIMStruct **additional_swms = NULL;
1266 unsigned num_additional_swms = 0;
1267 unsigned num_threads = 0;
1269 for_opt(c, export_options) {
1272 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
1275 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1276 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1279 compression_type = get_compression_type(optarg);
1280 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1282 compression_type_specified = true;
1288 num_threads = parse_num_threads(optarg);
1289 if (num_threads == UINT_MAX)
1293 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1302 if (argc < 3 || argc > 5) {
1306 src_wimfile = argv[0];
1307 src_image_num_or_name = argv[1];
1308 dest_wimfile = argv[2];
1309 dest_name = (argc >= 4) ? argv[3] : NULL;
1310 dest_desc = (argc >= 5) ? argv[4] : NULL;
1311 ret = wimlib_open_wim(src_wimfile,
1312 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w,
1313 imagex_progress_func);
1317 /* Determine if the destination is an existing file or not.
1318 * If so, we try to append the exported image(s) to it; otherwise, we
1319 * create a new WIM containing the exported image(s). */
1320 if (stat(dest_wimfile, &stbuf) == 0) {
1324 /* Destination file exists. */
1325 if (!S_ISREG(stbuf.st_mode) && !S_ISLNK(stbuf.st_mode)) {
1326 imagex_error("`%s' is not a regular file",
1331 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w,
1332 imagex_progress_func);
1336 ret = file_writable(dest_wimfile);
1340 dest_ctype = wimlib_get_compression_type(dest_w);
1341 if (compression_type_specified
1342 && compression_type != dest_ctype)
1344 imagex_error("Cannot specify a compression type that is "
1345 "not the same as that used in the "
1352 /* dest_wimfile is not an existing file, so create a new WIM. */
1353 if (!compression_type_specified)
1354 compression_type = wimlib_get_compression_type(src_w);
1355 if (errno == ENOENT) {
1356 ret = wimlib_create_new_wim(compression_type, &dest_w);
1360 imagex_error_with_errno("Cannot stat file `%s'",
1367 image = wimlib_resolve_image(src_w, src_image_num_or_name);
1368 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
1373 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
1375 &num_additional_swms);
1380 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
1381 export_flags, additional_swms,
1382 num_additional_swms, imagex_progress_func);
1388 ret = wimlib_write(dest_w, dest_wimfile, WIMLIB_ALL_IMAGES,
1389 write_flags, num_threads,
1390 imagex_progress_func);
1392 ret = wimlib_overwrite(dest_w, write_flags, num_threads,
1393 imagex_progress_func);
1395 if (ret == WIMLIB_ERR_REOPEN)
1398 wimlib_free(dest_w);
1399 if (additional_swms) {
1400 for (unsigned i = 0; i < num_additional_swms; i++)
1401 wimlib_free(additional_swms[i]);
1402 free(additional_swms);
1407 /* Prints information about a WIM file; also can mark an image as bootable,
1408 * change the name of an image, or change the description of an image. */
1409 static int imagex_info(int argc, char **argv)
1414 bool header = false;
1415 bool lookup_table = false;
1417 bool metadata = false;
1418 bool short_header = true;
1419 const char *xml_out_file = NULL;
1420 const char *wimfile;
1421 const char *image_num_or_name = "all";
1422 const char *new_name = NULL;
1423 const char *new_desc = NULL;
1428 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1433 for_opt(c, info_options) {
1443 short_header = false;
1446 lookup_table = true;
1447 short_header = false;
1451 short_header = false;
1454 xml_out_file = optarg;
1455 short_header = false;
1459 short_header = false;
1469 if (argc == 0 || argc > 4) {
1475 image_num_or_name = argv[1];
1485 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1487 ret = wimlib_open_wim(wimfile, open_flags, &w,
1488 imagex_progress_func);
1492 part_number = wimlib_get_part_number(w, &total_parts);
1494 image = wimlib_resolve_image(w, image_num_or_name);
1495 if (image == WIMLIB_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
1496 imagex_error("The image `%s' does not exist",
1499 imagex_error("If you would like to set the boot "
1500 "index to 0, specify image \"0\" with "
1501 "the --boot flag.");
1502 ret = WIMLIB_ERR_INVALID_IMAGE;
1506 num_images = wimlib_get_num_images(w);
1508 if (num_images == 0) {
1510 imagex_error("--boot is meaningless on a WIM with no "
1512 ret = WIMLIB_ERR_INVALID_IMAGE;
1517 if (image == WIMLIB_ALL_IMAGES && num_images > 1) {
1519 imagex_error("Cannot specify the --boot flag "
1520 "without specifying a specific "
1521 "image in a multi-image WIM");
1522 ret = WIMLIB_ERR_INVALID_IMAGE;
1526 imagex_error("Cannot specify the NEW_NAME "
1527 "without specifying a specific "
1528 "image in a multi-image WIM");
1529 ret = WIMLIB_ERR_INVALID_IMAGE;
1534 /* Operations that print information are separated from operations that
1535 * recreate the WIM file. */
1536 if (!new_name && !boot) {
1538 /* Read-only operations */
1540 if (image == WIMLIB_NO_IMAGE) {
1541 imagex_error("`%s' is not a valid image",
1543 ret = WIMLIB_ERR_INVALID_IMAGE;
1547 if (image == WIMLIB_ALL_IMAGES && short_header)
1548 wimlib_print_wim_information(w);
1551 wimlib_print_header(w);
1554 if (total_parts != 1) {
1555 printf("Warning: Only showing the lookup table "
1556 "for part %d of a %d-part WIM.\n",
1557 part_number, total_parts);
1559 wimlib_print_lookup_table(w);
1563 ret = wimlib_extract_xml_data(w, stdout);
1569 fp = fopen(xml_out_file, "wb");
1571 imagex_error_with_errno("Failed to open the "
1578 ret = wimlib_extract_xml_data(w, fp);
1579 if (fclose(fp) != 0) {
1580 imagex_error("Failed to close the file `%s'",
1590 wimlib_print_available_images(w, image);
1593 ret = wimlib_print_metadata(w, image);
1599 /* Modification operations */
1600 if (total_parts != 1) {
1601 imagex_error("Modifying a split WIM is not supported.");
1605 if (image == WIMLIB_ALL_IMAGES)
1608 if (image == WIMLIB_NO_IMAGE && new_name) {
1609 imagex_error("Cannot specify new_name (`%s') when "
1610 "using image 0", new_name);
1616 if (image == wimlib_get_boot_idx(w)) {
1617 printf("Image %d is already marked as "
1618 "bootable.\n", image);
1621 printf("Marking image %d as bootable.\n",
1623 wimlib_set_boot_idx(w, image);
1627 if (strcmp(wimlib_get_image_name(w, image),
1629 printf("Image %d is already named \"%s\".\n",
1633 printf("Changing the name of image %d to "
1634 "\"%s\".\n", image, new_name);
1635 ret = wimlib_set_image_name(w, image, new_name);
1641 const char *old_desc;
1642 old_desc = wimlib_get_image_description(w, image);
1643 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1644 printf("The description of image %d is already "
1645 "\"%s\".\n", image, new_desc);
1648 printf("Changing the description of image %d "
1649 "to \"%s\".\n", image, new_desc);
1650 ret = wimlib_set_image_descripton(w, image,
1657 /* Only call wimlib_overwrite() if something actually needs to
1659 if (boot || new_name || new_desc ||
1660 (check && !wimlib_has_integrity_table(w)))
1664 ret = file_writable(wimfile);
1669 write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1673 ret = wimlib_overwrite(w, write_flags, 1,
1674 imagex_progress_func);
1675 if (ret == WIMLIB_ERR_REOPEN)
1678 printf("The file `%s' was not modified because nothing "
1679 "needed to be done.\n", wimfile);
1688 /* Join split WIMs into one part WIM */
1689 static int imagex_join(int argc, char **argv)
1692 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1693 int wim_write_flags = 0;
1694 const char *output_path;
1696 for_opt(c, join_options) {
1699 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1700 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1710 imagex_error("Must specify one or more split WIM (.swm) parts "
1714 output_path = argv[0];
1715 return wimlib_join((const char **)++argv, --argc, output_path,
1716 swm_open_flags, wim_write_flags,
1717 imagex_progress_func);
1723 /* Mounts an image using a FUSE mount. */
1724 static int imagex_mount_rw_or_ro(int argc, char **argv)
1727 int mount_flags = 0;
1728 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1729 const char *wimfile;
1735 const char *swm_glob = NULL;
1736 WIMStruct **additional_swms = NULL;
1737 unsigned num_additional_swms = 0;
1738 const char *staging_dir = NULL;
1740 if (strcmp(argv[0], "mountrw") == 0)
1741 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1743 for_opt(c, mount_options) {
1746 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
1749 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1752 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1755 if (strcasecmp(optarg, "none") == 0)
1756 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1757 else if (strcasecmp(optarg, "xattr") == 0)
1758 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1759 else if (strcasecmp(optarg, "windows") == 0)
1760 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1762 imagex_error("Unknown stream interface \"%s\"", optarg);
1770 staging_dir = optarg;
1773 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
1781 if (argc != 2 && argc != 3)
1786 ret = wimlib_open_wim(wimfile, open_flags, &w,
1787 imagex_progress_func);
1792 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1794 &num_additional_swms);
1801 num_images = wimlib_get_num_images(w);
1802 if (num_images != 1) {
1803 imagex_error("The file `%s' contains %d images; Please "
1804 "select one.", wimfile, num_images);
1805 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1812 image = wimlib_resolve_image(w, argv[1]);
1814 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
1819 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1820 ret = file_writable(wimfile);
1825 ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
1826 num_additional_swms, staging_dir);
1828 imagex_error("Failed to mount image %d from `%s' on `%s'",
1829 image, wimfile, dir);
1834 if (additional_swms) {
1835 for (unsigned i = 0; i < num_additional_swms; i++)
1836 wimlib_free(additional_swms[i]);
1837 free(additional_swms);
1841 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1846 /* Rebuild a WIM file */
1847 static int imagex_optimize(int argc, char **argv)
1851 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
1854 const char *wimfile;
1858 for_opt(c, optimize_options) {
1861 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1862 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1865 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1882 ret = wimlib_open_wim(wimfile, open_flags, &w,
1883 imagex_progress_func);
1887 old_size = file_get_size(argv[0]);
1888 printf("`%s' original size: ", wimfile);
1892 printf("%"PRIu64" KiB\n", old_size >> 10);
1894 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1897 new_size = file_get_size(argv[0]);
1898 printf("`%s' optimized size: ", wimfile);
1902 printf("%"PRIu64" KiB\n", new_size >> 10);
1904 fputs("Space saved: ", stdout);
1905 if (new_size != -1 && old_size != -1) {
1906 printf("%lld KiB\n",
1907 ((long long)old_size - (long long)new_size) >> 10);
1917 /* Split a WIM into a spanned set */
1918 static int imagex_split(int argc, char **argv)
1921 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1922 int write_flags = 0;
1923 unsigned long part_size;
1928 for_opt(c, split_options) {
1931 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1932 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1946 part_size = strtod(argv[2], &tmp) * (1 << 20);
1947 if (tmp == argv[2] || *tmp) {
1948 imagex_error("Invalid part size \"%s\"", argv[2]);
1949 imagex_error("The part size must be an integer or floating-point number of megabytes.");
1952 ret = wimlib_open_wim(argv[0], open_flags, &w, imagex_progress_func);
1955 ret = wimlib_split(w, argv[1], part_size, write_flags, imagex_progress_func);
1960 /* Unmounts a mounted WIM image. */
1961 static int imagex_unmount(int argc, char **argv)
1964 int unmount_flags = 0;
1967 for_opt(c, unmount_options) {
1970 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1973 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
1976 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
1990 ret = wimlib_unmount_image(argv[0], unmount_flags,
1991 imagex_progress_func);
1993 imagex_error("Failed to unmount `%s'", argv[0]);
1997 struct imagex_command {
1999 int (*func)(int , char **);
2004 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
2005 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
2007 static const struct imagex_command imagex_commands[] = {
2008 {"append", imagex_capture_or_append, APPEND},
2009 {"apply", imagex_apply, APPLY},
2010 {"capture", imagex_capture_or_append, CAPTURE},
2011 {"delete", imagex_delete, DELETE},
2012 {"dir", imagex_dir, DIR},
2013 {"export", imagex_export, EXPORT},
2014 {"info", imagex_info, INFO},
2015 {"join", imagex_join, JOIN},
2016 {"mount", imagex_mount_rw_or_ro, MOUNT},
2017 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
2018 {"optimize",imagex_optimize, OPTIMIZE},
2019 {"split", imagex_split, SPLIT},
2020 {"unmount", imagex_unmount, UNMOUNT},
2023 static void version()
2025 static const char *s =
2026 "imagex (" PACKAGE ") " PACKAGE_VERSION "\n"
2027 "Copyright (C) 2012, 2013 Eric Biggers\n"
2028 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
2029 "This is free software: you are free to change and redistribute it.\n"
2030 "There is NO WARRANTY, to the extent permitted by law.\n"
2032 "Report bugs to "PACKAGE_BUGREPORT".\n";
2037 static void help_or_version(int argc, char **argv)
2041 const struct imagex_command *cmd;
2043 for (i = 1; i < argc; i++) {
2051 if (strcmp(p, "help") == 0) {
2052 for_imagex_command(cmd) {
2053 if (strcmp(cmd->name, argv[1]) == 0) {
2061 if (strcmp(p, "version") == 0) {
2069 static void usage(int cmd_type)
2071 const struct imagex_command *cmd;
2072 printf("Usage: %s", usage_strings[cmd_type]);
2073 for_imagex_command(cmd) {
2074 if (cmd->cmd == cmd_type)
2075 printf("\nTry `man imagex-%s' for more details.\n",
2080 static void usage_all()
2082 puts("IMAGEX: Usage:");
2083 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
2084 printf(" %s", usage_strings[i]);
2085 static const char *extra =
2087 " imagex --version\n"
2089 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
2091 " Try `man imagex' for more information.\n"
2093 fputs(extra, stdout);
2096 /* Entry point for the 'imagex' program. */
2097 int main(int argc, char **argv)
2099 const struct imagex_command *cmd;
2103 imagex_error("No command specified");
2108 /* Handle --help and --version for all commands. Note that this will
2109 * not return if either of these arguments are present. */
2110 help_or_version(argc, argv);
2114 /* The user may like to see more informative error messages. */
2115 wimlib_set_print_errors(true);
2117 /* Calling wimlib_global_init() is not strictly necessary because
2118 * 'imagex' is single-threaded. */
2119 ret = wimlib_global_init();
2123 /* Search for the function to handle the 'imagex' subcommand. */
2124 for_imagex_command(cmd) {
2125 if (strcmp(cmd->name, *argv) == 0) {
2126 ret = cmd->func(argc, argv);
2127 goto out_check_write_error;
2131 imagex_error("Unrecognized command: `%s'", argv[0]);
2134 out_check_write_error:
2135 /* For 'imagex info' and 'imagex dir', data printed to standard output
2136 * is part of the program's actual behavior and not just for
2137 * informational purposes, so we should set a failure exit status if
2138 * there was a write error. */
2139 if (cmd == &imagex_commands[INFO] || cmd == &imagex_commands[DIR]) {
2140 if (ferror(stdout) || fclose(stdout)) {
2141 imagex_error_with_errno("output error");
2147 /* Exit status (ret): -1 indicates an error found by 'imagex' outside
2148 * of the wimlib library code. 0 indicates success. > 0 indicates a
2149 * wimlib error code from which an error message can be printed. */
2151 imagex_error("Exiting with error code %d:\n"
2153 wimlib_get_error_string(ret));
2154 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
2155 imagex_error_with_errno("errno");
2157 /* Calling wimlib_global_cleanup() is not strictly necessary because the
2158 * process is exiting anyway. */
2159 wimlib_global_cleanup();