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/>.
48 # include "imagex-win32.h"
53 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
55 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (char**)argv, "", \
74 static void usage(int cmd_type);
75 static void usage_all();
78 static const char *usage_strings[] = {
80 IMAGEX_PROGNAME" append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
81 " [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
82 " [--verbose] [--dereference] [--config=FILE]\n"
83 " [--threads=NUM_THREADS] [--rebuild] [--unix-data]\n"
86 IMAGEX_PROGNAME" apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
87 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
88 " [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n",
90 IMAGEX_PROGNAME" capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
91 " [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
92 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
93 " [--config=FILE] [--threads=NUM_THREADS] [--unix-data]\n"
96 IMAGEX_PROGNAME" delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check] [--soft]\n",
98 IMAGEX_PROGNAME" dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n",
100 IMAGEX_PROGNAME" export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
101 " DEST_WIMFILE [DEST_IMAGE_NAME] [DEST_IMAGE_DESCRIPTION]\n"
102 " [--boot] [--check] [--compress=TYPE] [--ref=\"GLOB\"]\n"
103 " [--threads=NUM_THREADS] [--rebuild]\n",
105 IMAGEX_PROGNAME" info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
106 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
107 " [--xml] [--extract-xml FILE] [--metadata]\n",
109 IMAGEX_PROGNAME" join [--check] WIMFILE SPLIT_WIM...\n",
111 IMAGEX_PROGNAME" mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
112 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
113 " [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n",
115 IMAGEX_PROGNAME" mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
116 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
117 " [--staging-dir=DIR] [--unix-data] [--allow-other]\n",
119 IMAGEX_PROGNAME" optimize WIMFILE [--check] [--recompress] [--compress=TYPE]\n",
121 IMAGEX_PROGNAME" split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n",
123 IMAGEX_PROGNAME" unmount DIRECTORY [--commit] [--check] [--rebuild]\n",
126 static const struct option apply_options[] = {
127 {"check", no_argument, NULL, 'c'},
128 {"hardlink", no_argument, NULL, 'h'},
129 {"symlink", no_argument, NULL, 's'},
130 {"verbose", no_argument, NULL, 'v'},
131 {"ref", required_argument, NULL, 'r'},
132 {"unix-data", no_argument, NULL, 'U'},
135 static const struct option capture_or_append_options[] = {
136 {"boot", no_argument, NULL, 'b'},
137 {"check", no_argument, NULL, 'c'},
138 {"compress", required_argument, NULL, 'x'},
139 {"config", required_argument, NULL, 'C'},
140 {"dereference", no_argument, NULL, 'L'},
141 {"flags", required_argument, NULL, 'f'},
142 {"verbose", no_argument, NULL, 'v'},
143 {"threads", required_argument, NULL, 't'},
144 {"rebuild", no_argument, NULL, 'R'},
145 {"unix-data", no_argument, NULL, 'U'},
146 {"source-list", no_argument, NULL, 'S'},
149 static const struct option delete_options[] = {
150 {"check", no_argument, NULL, 'c'},
151 {"soft", no_argument, NULL, 's'},
155 static const struct option export_options[] = {
156 {"boot", no_argument, NULL, 'b'},
157 {"check", no_argument, NULL, 'c'},
158 {"compress", required_argument, NULL, 'x'},
159 {"ref", required_argument, NULL, 'r'},
160 {"threads", required_argument, NULL, 't'},
161 {"rebuild", no_argument, NULL, 'R'},
165 static const struct option info_options[] = {
166 {"boot", no_argument, NULL, 'b'},
167 {"check", no_argument, NULL, 'c'},
168 {"extract-xml", required_argument, NULL, 'X'},
169 {"header", no_argument, NULL, 'h'},
170 {"lookup-table", no_argument, NULL, 'l'},
171 {"metadata", no_argument, NULL, 'm'},
172 {"xml", no_argument, NULL, 'x'},
176 static const struct option join_options[] = {
177 {"check", no_argument, NULL, 'c'},
181 static const struct option mount_options[] = {
182 {"check", no_argument, NULL, 'c'},
183 {"debug", no_argument, NULL, 'd'},
184 {"streams-interface", required_argument, NULL, 's'},
185 {"ref", required_argument, NULL, 'r'},
186 {"staging-dir", required_argument, NULL, 'D'},
187 {"unix-data", no_argument, NULL, 'U'},
188 {"allow-other", no_argument, NULL, 'A'},
192 static const struct option optimize_options[] = {
193 {"check", no_argument, NULL, 'c'},
194 {"recompress", no_argument, NULL, 'r'},
198 static const struct option split_options[] = {
199 {"check", no_argument, NULL, 'c'},
203 static const struct option unmount_options[] = {
204 {"commit", no_argument, NULL, 'c'},
205 {"check", no_argument, NULL, 'C'},
206 {"rebuild", no_argument, NULL, 'R'},
212 /* Print formatted error message to stderr. */
213 static void imagex_error(const char *format, ...)
216 va_start(va, format);
217 fputs("ERROR: ", stderr);
218 vfprintf(stderr, format, va);
223 /* Print formatted error message to stderr. */
224 static void imagex_error_with_errno(const char *format, ...)
226 int errno_save = errno;
228 va_start(va, format);
229 fputs("ERROR: ", stderr);
230 vfprintf(stderr, format, va);
231 fprintf(stderr, ": %s\n", strerror(errno_save));
235 static int verify_image_exists(int image, const char *image_name,
236 const char *wim_name)
238 if (image == WIMLIB_NO_IMAGE) {
239 imagex_error("\"%s\" is not a valid image in `%s'!\n"
240 " Please specify a 1-based image index or "
242 " You may use `"IMAGEX_PROGNAME" info' to list the images "
243 "contained in a WIM.",
244 image_name, wim_name);
250 static int verify_image_is_single(int image)
252 if (image == WIMLIB_ALL_IMAGES) {
253 imagex_error("Cannot specify all images for this action!");
259 static int verify_image_exists_and_is_single(int image, const char *image_name,
260 const char *wim_name)
263 ret = verify_image_exists(image, image_name, wim_name);
265 ret = verify_image_is_single(image);
269 /* Parse the argument to --compress */
270 static int get_compression_type(const char *optarg)
272 if (strcasecmp(optarg, "maximum") == 0 || strcasecmp(optarg, "lzx") == 0)
273 return WIMLIB_COMPRESSION_TYPE_LZX;
274 else if (strcasecmp(optarg, "fast") == 0 || strcasecmp(optarg, "xpress") == 0)
275 return WIMLIB_COMPRESSION_TYPE_XPRESS;
276 else if (strcasecmp(optarg, "none") == 0)
277 return WIMLIB_COMPRESSION_TYPE_NONE;
279 imagex_error("Invalid compression type `%s'! Must be "
280 "\"maximum\", \"fast\", or \"none\".", optarg);
281 return WIMLIB_COMPRESSION_TYPE_INVALID;
285 /* Returns the size of a file given its name, or -1 if the file does not exist
286 * or its size cannot be determined. */
287 static off_t file_get_size(const char *filename)
290 if (stat(filename, &st) == 0)
296 static const char *default_capture_config =
301 "\\System Volume Information\n"
305 "[CompressionExclusionList]\n"
309 "\\WINDOWS\\inf\\*.pnf\n";
311 /* Read standard input until EOF and return the full contents in a malloc()ed
312 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
314 static char *stdin_get_contents(size_t *len_ret)
316 /* stdin can, of course, be a pipe or other non-seekable file, so the
317 * total length of the data cannot be pre-determined */
319 size_t newlen = 1024;
323 char *p = realloc(buf, newlen);
324 size_t bytes_read, bytes_to_read;
326 imagex_error("out of memory while reading stdin");
330 bytes_to_read = newlen - pos;
331 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
333 if (bytes_read != bytes_to_read) {
338 imagex_error_with_errno("error reading stdin");
351 PARSE_FILENAME_SUCCESS = 0,
352 PARSE_FILENAME_FAILURE = 1,
353 PARSE_FILENAME_NONE = 2,
357 * Parses a filename in the source list file format. (See the man page for
358 * 'wimlib-imagex capture' for details on this format and the meaning.)
359 * Accepted formats for filenames are an unquoted string (whitespace-delimited),
360 * or a double or single-quoted string.
362 * @line_p: Pointer to the pointer to the line of data. Will be updated
363 * to point past the filename iff the return value is
364 * PARSE_FILENAME_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
367 * @len_p: @len_p initially stores the length of the line of data, which may
368 * be 0, and it will be updated to the number of bytes remaining in
369 * the line iff the return value is PARSE_FILENAME_SUCCESS.
371 * @fn_ret: Iff the return value is PARSE_FILENAME_SUCCESS, a pointer to the
372 * parsed filename will be returned here.
374 * Returns: PARSE_FILENAME_SUCCESS if a filename was successfully parsed; or
375 * PARSE_FILENAME_FAILURE if the data was invalid due to a missing
376 * closing quote; or PARSE_FILENAME_NONE if the line ended before the
377 * beginning of a filename was found.
379 static int parse_filename(char **line_p, size_t *len_p, char **fn_ret)
382 char *line = *line_p;
386 /* Skip leading whitespace */
389 return PARSE_FILENAME_NONE;
390 if (!isspace(*line) && *line != '\0')
396 if (quote_char == '"' || quote_char == '\'') {
397 /* Quoted filename */
401 line = memchr(line, quote_char, len);
403 imagex_error("Missing closing quote: %s", fn - 1);
404 return PARSE_FILENAME_FAILURE;
407 /* Unquoted filename. Go until whitespace. Line is terminated
408 * by '\0', so no need to check 'len'. */
412 } while (!isspace(*line) && *line != '\0');
419 return PARSE_FILENAME_SUCCESS;
422 /* Parses a line of data (not an empty line or comment) in the source list file
423 * format. (See the man page for 'wimlib-imagex capture' for details on this
424 * format and the meaning.)
426 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
427 * len == 0. The data in @line will be modified by this function call.
429 * @len: Length of the line of data.
431 * @source: On success, the capture source and target described by the line is
432 * written into this destination. Note that it will contain pointers
433 * to data in the @line array.
435 * Returns true if the line was valid; false otherwise. */
437 parse_source_list_line(char *line, size_t len,
438 struct wimlib_capture_source *source)
442 ret = parse_filename(&line, &len, &source->fs_source_path);
443 if (ret != PARSE_FILENAME_SUCCESS)
445 ret = parse_filename(&line, &len, &source->wim_target_path);
446 if (ret == PARSE_FILENAME_NONE)
447 source->wim_target_path = source->fs_source_path;
448 return ret != PARSE_FILENAME_FAILURE;
451 /* Returns %true if the given line of length @len > 0 is a comment or empty line
452 * in the source list file format. */
453 static bool is_comment_line(const char *line, size_t len)
458 if (!isspace(*line) && *line != '\0')
467 /* Parses a file in the source list format. (See the man page for
468 * 'wimlib-imagex capture' for details on this format and the meaning.)
470 * @source_list_contents: Contents of the source list file. Note that this
471 * buffer will be modified to save memory allocations,
472 * and cannot be freed until the returned array of
473 * wimlib_capture_source's has also been freed.
475 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
478 * @nsources_ret: On success, the length of the returned array is
481 * Returns: An array of `struct wimlib_capture_source's that can be passed to
482 * the wimlib_add_image_multisource() function to specify how a WIM image is to
484 static struct wimlib_capture_source *
485 parse_source_list(char *source_list_contents, size_t source_list_nbytes,
486 size_t *nsources_ret)
490 struct wimlib_capture_source *sources;
494 for (i = 0; i < source_list_nbytes; i++)
495 if (source_list_contents[i] == '\n')
497 sources = calloc(nlines, sizeof(*sources));
499 imagex_error("out of memory");
502 p = source_list_contents;
504 for (i = 0; i < nlines; i++) {
505 /* XXX: Could use rawmemchr() here instead, but it may not be
506 * available on all platforms. */
507 char *endp = memchr(p, '\n', source_list_nbytes);
508 size_t len = endp - p + 1;
510 if (!is_comment_line(p, len)) {
511 if (!parse_source_list_line(p, len, &sources[j++])) {
523 /* Reads the contents of a file into memory. */
524 static char *file_get_contents(const char *filename, size_t *len_ret)
531 if (stat(filename, &stbuf) != 0) {
532 imagex_error_with_errno("Failed to stat the file `%s'", filename);
537 fp = fopen(filename, "rb");
539 imagex_error_with_errno("Failed to open the file `%s'", filename);
545 imagex_error("Failed to allocate buffer of %zu bytes to hold "
546 "contents of file `%s'", len, filename);
549 if (fread(buf, 1, len, fp) != len) {
550 imagex_error_with_errno("Failed to read %lu bytes from the "
551 "file `%s'", len, filename);
565 /* Return 0 if a path names a file to which the current user has write access;
566 * -1 otherwise (and print an error message). */
567 static int file_writable(const char *path)
570 ret = access(path, W_OK);
572 imagex_error_with_errno("Can't modify `%s'", path);
576 #define TO_PERCENT(numerator, denominator) \
577 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
579 /* Given an enumerated value for WIM compression type, return a descriptive
581 static const char *get_data_type(int ctype)
584 case WIMLIB_COMPRESSION_TYPE_NONE:
585 return "uncompressed";
586 case WIMLIB_COMPRESSION_TYPE_LZX:
587 return "LZX-compressed";
588 case WIMLIB_COMPRESSION_TYPE_XPRESS:
589 return "XPRESS-compressed";
594 /* Progress callback function passed to various wimlib functions. */
595 static int imagex_progress_func(enum wimlib_progress_msg msg,
596 const union wimlib_progress_info *info)
598 unsigned percent_done;
600 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
601 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
602 info->write_streams.total_bytes);
603 if (info->write_streams.completed_streams == 0) {
604 const char *data_type;
606 data_type = get_data_type(info->write_streams.compression_type);
607 printf("Writing %s data using %u thread%s\n",
608 data_type, info->write_streams.num_threads,
609 (info->write_streams.num_threads == 1) ? "" : "s");
611 printf("\r%"PRIu64" MiB of %"PRIu64" MiB (uncompressed) "
612 "written (%u%% done)",
613 info->write_streams.completed_bytes >> 20,
614 info->write_streams.total_bytes >> 20,
616 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
619 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
620 printf("Scanning `%s'", info->scan.source);
621 if (*info->scan.wim_target_path) {
622 printf(" (loading as WIM path: `/%s')...\n",
623 info->scan.wim_target_path);
625 printf(" (loading as root of WIM image)...\n");
628 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
629 if (info->scan.excluded)
630 printf("Excluding `%s' from capture\n", info->scan.cur_path);
632 printf("Scanning `%s'\n", info->scan.cur_path);
634 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
636 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
637 percent_done = TO_PERCENT(info->integrity.completed_bytes,
638 info->integrity.total_bytes);
639 printf("\rVerifying integrity of `%s': %"PRIu64" MiB "
640 "of %"PRIu64" MiB (%u%%) done",
641 info->integrity.filename,
642 info->integrity.completed_bytes >> 20,
643 info->integrity.total_bytes >> 20,
645 if (info->integrity.completed_bytes == info->integrity.total_bytes)
648 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
649 percent_done = TO_PERCENT(info->integrity.completed_bytes,
650 info->integrity.total_bytes);
651 printf("\rCalculating integrity table for WIM: %"PRIu64" MiB "
652 "of %"PRIu64" MiB (%u%%) done",
653 info->integrity.completed_bytes >> 20,
654 info->integrity.total_bytes >> 20,
656 if (info->integrity.completed_bytes == info->integrity.total_bytes)
659 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
660 printf("Applying image %d (%s) from `%s' to %s `%s'\n",
662 info->extract.image_name,
663 info->extract.wimfile_name,
664 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
665 "NTFS volume" : "directory"),
666 info->extract.target);
668 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
669 /*printf("Applying directory structure to %s\n",*/
670 /*info->extract.target);*/
672 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
673 percent_done = TO_PERCENT(info->extract.completed_bytes,
674 info->extract.total_bytes);
675 printf("\rExtracting files: "
676 "%"PRIu64" MiB of %"PRIu64" MiB (%u%%) done",
677 info->extract.completed_bytes >> 20,
678 info->extract.total_bytes >> 20,
680 if (info->extract.completed_bytes >= info->extract.total_bytes)
683 case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
684 puts(info->extract.cur_path);
686 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
687 printf("Setting timestamps on all extracted files...\n");
689 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
690 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
691 printf("Unmounting NTFS volume `%s'...\n",
692 info->extract.target);
695 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
696 percent_done = TO_PERCENT(info->join.completed_bytes,
697 info->join.total_bytes);
698 printf("Writing resources from part %u of %u: "
699 "%"PRIu64 " MiB of %"PRIu64" MiB (%u%%) written\n",
700 (info->join.completed_parts == info->join.total_parts) ?
701 info->join.completed_parts : info->join.completed_parts + 1,
702 info->join.total_parts,
703 info->join.completed_bytes >> 20,
704 info->join.total_bytes >> 20,
707 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
708 percent_done = TO_PERCENT(info->split.completed_bytes,
709 info->split.total_bytes);
710 printf("Writing `%s': %"PRIu64" MiB of %"PRIu64" MiB (%u%%) written\n",
711 info->split.part_name,
712 info->split.completed_bytes >> 20,
713 info->split.total_bytes >> 20,
716 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
717 if (info->split.completed_bytes == info->split.total_bytes) {
718 printf("Finished writing %u split WIM parts\n",
719 info->split.cur_part_number);
729 /* Open all the split WIM parts that correspond to a file glob.
731 * @first_part specifies the first part of the split WIM and it may be either
732 * included or omitted from the glob. */
733 static int open_swms_from_glob(const char *swm_glob,
734 const char *first_part,
736 WIMStruct ***additional_swms_ret,
737 unsigned *num_additional_swms_ret)
739 unsigned num_additional_swms = 0;
740 WIMStruct **additional_swms = NULL;
744 /* Warning: glob() is replaced in Windows native builds */
745 ret = glob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
747 if (ret == GLOB_NOMATCH) {
748 imagex_error("Found no files for glob \"%s\"",
751 imagex_error_with_errno("Failed to process glob "
757 num_additional_swms = globbuf.gl_pathc;
758 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
759 if (!additional_swms) {
760 imagex_error("Out of memory");
765 for (unsigned i = 0; i < num_additional_swms; i++) {
766 if (strcmp(globbuf.gl_pathv[i], first_part) == 0) {
770 ret = wimlib_open_wim(globbuf.gl_pathv[i],
771 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
772 &additional_swms[i - offset],
773 imagex_progress_func);
777 *additional_swms_ret = additional_swms;
778 *num_additional_swms_ret = num_additional_swms - offset;
782 for (unsigned i = 0; i < num_additional_swms; i++)
783 wimlib_free(additional_swms[i]);
784 free(additional_swms);
792 static unsigned parse_num_threads(const char *optarg)
795 unsigned nthreads = strtoul(optarg, &tmp, 10);
796 if (nthreads == UINT_MAX || *tmp || tmp == optarg) {
797 imagex_error("Number of threads must be a non-negative integer!");
805 /* Apply one image, or all images, from a WIM file into a directory, OR apply
806 * one image from a WIM file to a NTFS volume. */
807 static int imagex_apply(int argc, char **argv)
810 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
817 const char *image_num_or_name;
818 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
820 const char *swm_glob = NULL;
821 WIMStruct **additional_swms = NULL;
822 unsigned num_additional_swms = 0;
824 for_opt(c, apply_options) {
827 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
830 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
833 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
836 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
842 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
851 if (argc != 2 && argc != 3) {
858 image_num_or_name = "1";
861 image_num_or_name = argv[1];
865 ret = wimlib_open_wim(wimfile, open_flags, &w, imagex_progress_func);
869 image = wimlib_resolve_image(w, image_num_or_name);
870 ret = verify_image_exists(image, image_num_or_name, wimfile);
874 num_images = wimlib_get_num_images(w);
875 if (argc == 2 && num_images != 1) {
876 imagex_error("`%s' contains %d images; Please select one "
877 "(or all)", wimfile, num_images);
884 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
886 &num_additional_swms);
893 ret = stat(target, &stbuf);
895 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
896 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
898 if (errno != ENOENT) {
899 imagex_error_with_errno("Failed to stat `%s'", target);
906 win32_acquire_restore_privileges();
908 ret = wimlib_extract_image(w, image, target, extract_flags,
909 additional_swms, num_additional_swms,
910 imagex_progress_func);
912 printf("Done applying WIM image.\n");
914 win32_release_restore_privileges();
918 if (additional_swms) {
919 for (unsigned i = 0; i < num_additional_swms; i++)
920 wimlib_free(additional_swms[i]);
921 free(additional_swms);
926 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
927 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
928 * the desired image. 'wimlib-imagex append': add a new image to an existing
930 static int imagex_capture_or_append(int argc, char **argv)
934 int add_image_flags = 0;
936 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
940 const char *flags_element = NULL;
944 int cmd = strcmp(argv[0], "append") ? CAPTURE : APPEND;
945 unsigned num_threads = 0;
948 size_t source_name_len;
951 const char *config_file = NULL;
952 char *config_str = NULL;
955 bool source_list = false;
956 size_t source_list_nbytes;
957 char *source_list_contents = NULL;
958 bool capture_sources_malloced = false;
959 struct wimlib_capture_source *capture_sources;
962 for_opt(c, capture_or_append_options) {
965 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
968 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
969 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
972 config_file = optarg;
975 compression_type = get_compression_type(optarg);
976 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
980 flags_element = optarg;
983 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
986 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
989 num_threads = parse_num_threads(optarg);
990 if (num_threads == UINT_MAX)
994 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
997 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1010 if (argc < 2 || argc > 4) {
1021 /* Set default name to SOURCE argument, omitting any directory
1022 * prefixes and trailing slashes. This requires making a copy
1024 source_name_len = strlen(source);
1025 source_copy = alloca(source_name_len + 1);
1026 name = basename(strcpy(source_copy, source));
1028 /* Image description defaults to NULL if not given. */
1029 desc = (argc >= 4) ? argv[3] : NULL;
1032 /* Set up capture sources in source list mode */
1033 if (source[0] == '-' && source[1] == '\0') {
1034 source_list_contents = stdin_get_contents(&source_list_nbytes);
1036 source_list_contents = file_get_contents(source,
1037 &source_list_nbytes);
1039 if (!source_list_contents)
1042 capture_sources = parse_source_list(source_list_contents,
1045 if (!capture_sources) {
1049 capture_sources_malloced = true;
1051 /* Set up capture source in non-source-list mode (could be
1052 * either "normal" mode or "NTFS mode"--- see the man page). */
1053 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1054 capture_sources[0].fs_source_path = source;
1055 capture_sources[0].wim_target_path = NULL;
1056 capture_sources[0].reserved = 0;
1061 config_str = file_get_contents(config_file, &config_len);
1069 ret = wimlib_open_wim(wimfile, open_flags, &w,
1070 imagex_progress_func);
1072 ret = wimlib_create_new_wim(compression_type, &w);
1078 ret = stat(source, &stbuf);
1080 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1081 printf("Capturing WIM image from NTFS filesystem on `%s'\n",
1083 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1086 if (errno != ENOENT) {
1087 imagex_error_with_errno("Failed to stat `%s'", source);
1094 win32_acquire_capture_privileges();
1097 ret = wimlib_add_image_multisource(w, capture_sources,
1099 (config_str ? config_str :
1100 default_capture_config),
1101 (config_str ? config_len :
1102 strlen(default_capture_config)),
1104 imagex_progress_func);
1106 goto out_release_privs;
1107 cur_image = wimlib_get_num_images(w);
1109 ret = wimlib_set_image_descripton(w, cur_image, desc);
1111 goto out_release_privs;
1113 if (flags_element) {
1114 ret = wimlib_set_image_flags(w, cur_image, flags_element);
1116 goto out_release_privs;
1118 if (cmd == APPEND) {
1119 ret = wimlib_overwrite(w, write_flags, num_threads,
1120 imagex_progress_func);
1122 ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
1123 num_threads, imagex_progress_func);
1125 if (ret == WIMLIB_ERR_REOPEN)
1128 imagex_error("Failed to write the WIM file `%s'", wimfile);
1131 win32_release_capture_privileges();
1136 free(source_list_contents);
1137 if (capture_sources_malloced)
1138 free(capture_sources);
1142 /* Remove image(s) from a WIM. */
1143 static int imagex_delete(int argc, char **argv)
1147 int write_flags = 0;
1148 const char *wimfile;
1149 const char *image_num_or_name;
1154 for_opt(c, delete_options) {
1157 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1158 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1161 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
1173 imagex_error("Must specify a WIM file");
1175 imagex_error("Must specify an image");
1180 image_num_or_name = argv[1];
1182 ret = file_writable(wimfile);
1186 ret = wimlib_open_wim(wimfile, open_flags, &w,
1187 imagex_progress_func);
1191 image = wimlib_resolve_image(w, image_num_or_name);
1193 ret = verify_image_exists(image, image_num_or_name, wimfile);
1197 ret = wimlib_delete_image(w, image);
1199 imagex_error("Failed to delete image from `%s'", wimfile);
1203 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1204 if (ret == WIMLIB_ERR_REOPEN)
1207 imagex_error("Failed to write the file `%s' with image "
1208 "deleted", wimfile);
1215 /* Print the files contained in an image(s) in a WIM file. */
1216 static int imagex_dir(int argc, char **argv)
1218 const char *wimfile;
1225 imagex_error("Must specify a WIM file");
1230 imagex_error("Too many arguments");
1236 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w,
1237 imagex_progress_func);
1242 image = wimlib_resolve_image(w, argv[2]);
1243 ret = verify_image_exists(image, argv[2], wimfile);
1247 /* Image was not specified. If the WIM only contains one image,
1248 * choose that one; otherwise, print an error. */
1249 num_images = wimlib_get_num_images(w);
1250 if (num_images != 1) {
1251 imagex_error("The file `%s' contains %d images; Please "
1252 "select one.", wimfile, num_images);
1260 ret = wimlib_print_files(w, image);
1266 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
1268 static int imagex_export(int argc, char **argv)
1272 int export_flags = 0;
1273 int write_flags = 0;
1274 int compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
1275 bool compression_type_specified = false;
1276 const char *src_wimfile;
1277 const char *src_image_num_or_name;
1278 const char *dest_wimfile;
1279 const char *dest_name;
1280 const char *dest_desc;
1281 WIMStruct *src_w = NULL;
1282 WIMStruct *dest_w = NULL;
1287 const char *swm_glob = NULL;
1288 WIMStruct **additional_swms = NULL;
1289 unsigned num_additional_swms = 0;
1290 unsigned num_threads = 0;
1292 for_opt(c, export_options) {
1295 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
1298 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1299 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1302 compression_type = get_compression_type(optarg);
1303 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1305 compression_type_specified = true;
1311 num_threads = parse_num_threads(optarg);
1312 if (num_threads == UINT_MAX)
1316 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1325 if (argc < 3 || argc > 5) {
1329 src_wimfile = argv[0];
1330 src_image_num_or_name = argv[1];
1331 dest_wimfile = argv[2];
1332 dest_name = (argc >= 4) ? argv[3] : NULL;
1333 dest_desc = (argc >= 5) ? argv[4] : NULL;
1334 ret = wimlib_open_wim(src_wimfile,
1335 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w,
1336 imagex_progress_func);
1340 /* Determine if the destination is an existing file or not.
1341 * If so, we try to append the exported image(s) to it; otherwise, we
1342 * create a new WIM containing the exported image(s). */
1343 if (stat(dest_wimfile, &stbuf) == 0) {
1347 /* Destination file exists. */
1349 if (!S_ISREG(stbuf.st_mode)) {
1350 imagex_error("`%s' is not a regular file",
1355 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w,
1356 imagex_progress_func);
1360 ret = file_writable(dest_wimfile);
1364 dest_ctype = wimlib_get_compression_type(dest_w);
1365 if (compression_type_specified
1366 && compression_type != dest_ctype)
1368 imagex_error("Cannot specify a compression type that is "
1369 "not the same as that used in the "
1376 /* dest_wimfile is not an existing file, so create a new WIM. */
1377 if (!compression_type_specified)
1378 compression_type = wimlib_get_compression_type(src_w);
1379 if (errno == ENOENT) {
1380 ret = wimlib_create_new_wim(compression_type, &dest_w);
1384 imagex_error_with_errno("Cannot stat file `%s'",
1391 image = wimlib_resolve_image(src_w, src_image_num_or_name);
1392 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
1397 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
1399 &num_additional_swms);
1404 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
1405 export_flags, additional_swms,
1406 num_additional_swms, imagex_progress_func);
1412 ret = wimlib_write(dest_w, dest_wimfile, WIMLIB_ALL_IMAGES,
1413 write_flags, num_threads,
1414 imagex_progress_func);
1416 ret = wimlib_overwrite(dest_w, write_flags, num_threads,
1417 imagex_progress_func);
1419 if (ret == WIMLIB_ERR_REOPEN)
1422 wimlib_free(dest_w);
1423 if (additional_swms) {
1424 for (unsigned i = 0; i < num_additional_swms; i++)
1425 wimlib_free(additional_swms[i]);
1426 free(additional_swms);
1431 /* Prints information about a WIM file; also can mark an image as bootable,
1432 * change the name of an image, or change the description of an image. */
1433 static int imagex_info(int argc, char **argv)
1438 bool header = false;
1439 bool lookup_table = false;
1441 bool metadata = false;
1442 bool short_header = true;
1443 const char *xml_out_file = NULL;
1444 const char *wimfile;
1445 const char *image_num_or_name = "all";
1446 const char *new_name = NULL;
1447 const char *new_desc = NULL;
1452 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1457 for_opt(c, info_options) {
1467 short_header = false;
1470 lookup_table = true;
1471 short_header = false;
1475 short_header = false;
1478 xml_out_file = optarg;
1479 short_header = false;
1483 short_header = false;
1493 if (argc == 0 || argc > 4) {
1499 image_num_or_name = argv[1];
1509 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1511 ret = wimlib_open_wim(wimfile, open_flags, &w,
1512 imagex_progress_func);
1516 part_number = wimlib_get_part_number(w, &total_parts);
1518 image = wimlib_resolve_image(w, image_num_or_name);
1519 if (image == WIMLIB_NO_IMAGE && strcmp(image_num_or_name, "0") != 0) {
1520 imagex_error("The image `%s' does not exist",
1523 imagex_error("If you would like to set the boot "
1524 "index to 0, specify image \"0\" with "
1525 "the --boot flag.");
1526 ret = WIMLIB_ERR_INVALID_IMAGE;
1530 num_images = wimlib_get_num_images(w);
1532 if (num_images == 0) {
1534 imagex_error("--boot is meaningless on a WIM with no "
1536 ret = WIMLIB_ERR_INVALID_IMAGE;
1541 if (image == WIMLIB_ALL_IMAGES && num_images > 1) {
1543 imagex_error("Cannot specify the --boot flag "
1544 "without specifying a specific "
1545 "image in a multi-image WIM");
1546 ret = WIMLIB_ERR_INVALID_IMAGE;
1550 imagex_error("Cannot specify the NEW_NAME "
1551 "without specifying a specific "
1552 "image in a multi-image WIM");
1553 ret = WIMLIB_ERR_INVALID_IMAGE;
1558 /* Operations that print information are separated from operations that
1559 * recreate the WIM file. */
1560 if (!new_name && !boot) {
1562 /* Read-only operations */
1564 if (image == WIMLIB_NO_IMAGE) {
1565 imagex_error("`%s' is not a valid image",
1567 ret = WIMLIB_ERR_INVALID_IMAGE;
1571 if (image == WIMLIB_ALL_IMAGES && short_header)
1572 wimlib_print_wim_information(w);
1575 wimlib_print_header(w);
1578 if (total_parts != 1) {
1579 printf("Warning: Only showing the lookup table "
1580 "for part %d of a %d-part WIM.\n",
1581 part_number, total_parts);
1583 wimlib_print_lookup_table(w);
1587 ret = wimlib_extract_xml_data(w, stdout);
1593 fp = fopen(xml_out_file, "wb");
1595 imagex_error_with_errno("Failed to open the "
1602 ret = wimlib_extract_xml_data(w, fp);
1603 if (fclose(fp) != 0) {
1604 imagex_error("Failed to close the file `%s'",
1614 wimlib_print_available_images(w, image);
1617 ret = wimlib_print_metadata(w, image);
1623 /* Modification operations */
1624 if (total_parts != 1) {
1625 imagex_error("Modifying a split WIM is not supported.");
1629 if (image == WIMLIB_ALL_IMAGES)
1632 if (image == WIMLIB_NO_IMAGE && new_name) {
1633 imagex_error("Cannot specify new_name (`%s') when "
1634 "using image 0", new_name);
1640 if (image == wimlib_get_boot_idx(w)) {
1641 printf("Image %d is already marked as "
1642 "bootable.\n", image);
1645 printf("Marking image %d as bootable.\n",
1647 wimlib_set_boot_idx(w, image);
1651 if (strcmp(wimlib_get_image_name(w, image),
1653 printf("Image %d is already named \"%s\".\n",
1657 printf("Changing the name of image %d to "
1658 "\"%s\".\n", image, new_name);
1659 ret = wimlib_set_image_name(w, image, new_name);
1665 const char *old_desc;
1666 old_desc = wimlib_get_image_description(w, image);
1667 if (old_desc && strcmp(old_desc, new_desc) == 0) {
1668 printf("The description of image %d is already "
1669 "\"%s\".\n", image, new_desc);
1672 printf("Changing the description of image %d "
1673 "to \"%s\".\n", image, new_desc);
1674 ret = wimlib_set_image_descripton(w, image,
1681 /* Only call wimlib_overwrite() if something actually needs to
1683 if (boot || new_name || new_desc ||
1684 (check && !wimlib_has_integrity_table(w)))
1688 ret = file_writable(wimfile);
1693 write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1697 ret = wimlib_overwrite(w, write_flags, 1,
1698 imagex_progress_func);
1699 if (ret == WIMLIB_ERR_REOPEN)
1702 printf("The file `%s' was not modified because nothing "
1703 "needed to be done.\n", wimfile);
1712 /* Join split WIMs into one part WIM */
1713 static int imagex_join(int argc, char **argv)
1716 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1717 int wim_write_flags = 0;
1718 const char *output_path;
1720 for_opt(c, join_options) {
1723 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1724 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1734 imagex_error("Must specify one or more split WIM (.swm) parts "
1738 output_path = argv[0];
1739 return wimlib_join((const char **)++argv, --argc, output_path,
1740 swm_open_flags, wim_write_flags,
1741 imagex_progress_func);
1747 /* Mounts an image using a FUSE mount. */
1748 static int imagex_mount_rw_or_ro(int argc, char **argv)
1751 int mount_flags = 0;
1752 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1753 const char *wimfile;
1759 const char *swm_glob = NULL;
1760 WIMStruct **additional_swms = NULL;
1761 unsigned num_additional_swms = 0;
1762 const char *staging_dir = NULL;
1764 if (strcmp(argv[0], "mountrw") == 0)
1765 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1767 for_opt(c, mount_options) {
1770 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
1773 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1776 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1779 if (strcasecmp(optarg, "none") == 0)
1780 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1781 else if (strcasecmp(optarg, "xattr") == 0)
1782 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1783 else if (strcasecmp(optarg, "windows") == 0)
1784 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1786 imagex_error("Unknown stream interface \"%s\"", optarg);
1794 staging_dir = optarg;
1797 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
1805 if (argc != 2 && argc != 3)
1810 ret = wimlib_open_wim(wimfile, open_flags, &w,
1811 imagex_progress_func);
1816 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1818 &num_additional_swms);
1825 num_images = wimlib_get_num_images(w);
1826 if (num_images != 1) {
1827 imagex_error("The file `%s' contains %d images; Please "
1828 "select one.", wimfile, num_images);
1829 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1836 image = wimlib_resolve_image(w, argv[1]);
1838 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
1843 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1844 ret = file_writable(wimfile);
1849 ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
1850 num_additional_swms, staging_dir);
1852 imagex_error("Failed to mount image %d from `%s' on `%s'",
1853 image, wimfile, dir);
1858 if (additional_swms) {
1859 for (unsigned i = 0; i < num_additional_swms; i++)
1860 wimlib_free(additional_swms[i]);
1861 free(additional_swms);
1865 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1870 /* Rebuild a WIM file */
1871 static int imagex_optimize(int argc, char **argv)
1875 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
1878 const char *wimfile;
1882 for_opt(c, optimize_options) {
1885 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1886 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1889 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1906 ret = wimlib_open_wim(wimfile, open_flags, &w,
1907 imagex_progress_func);
1911 old_size = file_get_size(argv[0]);
1912 printf("`%s' original size: ", wimfile);
1916 printf("%"PRIu64" KiB\n", old_size >> 10);
1918 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1921 new_size = file_get_size(argv[0]);
1922 printf("`%s' optimized size: ", wimfile);
1926 printf("%"PRIu64" KiB\n", new_size >> 10);
1928 fputs("Space saved: ", stdout);
1929 if (new_size != -1 && old_size != -1) {
1930 printf("%lld KiB\n",
1931 ((long long)old_size - (long long)new_size) >> 10);
1941 /* Split a WIM into a spanned set */
1942 static int imagex_split(int argc, char **argv)
1945 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1946 int write_flags = 0;
1947 unsigned long part_size;
1952 for_opt(c, split_options) {
1955 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1956 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1970 part_size = strtod(argv[2], &tmp) * (1 << 20);
1971 if (tmp == argv[2] || *tmp) {
1972 imagex_error("Invalid part size \"%s\"", argv[2]);
1973 imagex_error("The part size must be an integer or floating-point number of megabytes.");
1976 ret = wimlib_open_wim(argv[0], open_flags, &w, imagex_progress_func);
1979 ret = wimlib_split(w, argv[1], part_size, write_flags, imagex_progress_func);
1984 /* Unmounts a mounted WIM image. */
1985 static int imagex_unmount(int argc, char **argv)
1988 int unmount_flags = 0;
1991 for_opt(c, unmount_options) {
1994 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
1997 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
2000 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
2014 ret = wimlib_unmount_image(argv[0], unmount_flags,
2015 imagex_progress_func);
2017 imagex_error("Failed to unmount `%s'", argv[0]);
2021 struct imagex_command {
2023 int (*func)(int , char **);
2028 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
2029 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
2031 static const struct imagex_command imagex_commands[] = {
2032 {"append", imagex_capture_or_append, APPEND},
2033 {"apply", imagex_apply, APPLY},
2034 {"capture", imagex_capture_or_append, CAPTURE},
2035 {"delete", imagex_delete, DELETE},
2036 {"dir", imagex_dir, DIR},
2037 {"export", imagex_export, EXPORT},
2038 {"info", imagex_info, INFO},
2039 {"join", imagex_join, JOIN},
2040 {"mount", imagex_mount_rw_or_ro, MOUNT},
2041 {"mountrw", imagex_mount_rw_or_ro, MOUNTRW},
2042 {"optimize",imagex_optimize, OPTIMIZE},
2043 {"split", imagex_split, SPLIT},
2044 {"unmount", imagex_unmount, UNMOUNT},
2047 static void version()
2049 static const char *s =
2050 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
2051 "Copyright (C) 2012, 2013 Eric Biggers\n"
2052 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
2053 "This is free software: you are free to change and redistribute it.\n"
2054 "There is NO WARRANTY, to the extent permitted by law.\n"
2056 "Report bugs to "PACKAGE_BUGREPORT".\n";
2061 static void help_or_version(int argc, char **argv)
2065 const struct imagex_command *cmd;
2067 for (i = 1; i < argc; i++) {
2075 if (strcmp(p, "help") == 0) {
2076 for_imagex_command(cmd) {
2077 if (strcmp(cmd->name, argv[1]) == 0) {
2085 if (strcmp(p, "version") == 0) {
2093 static void usage(int cmd_type)
2095 const struct imagex_command *cmd;
2096 printf("Usage: %s", usage_strings[cmd_type]);
2097 for_imagex_command(cmd) {
2098 if (cmd->cmd == cmd_type)
2099 printf("\nTry `man "IMAGEX_PROGNAME"-%s' for more details.\n",
2104 static void usage_all()
2106 puts("IMAGEX: Usage:");
2107 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
2108 printf(" %s", usage_strings[i]);
2109 static const char *extra =
2110 " "IMAGEX_PROGNAME" --help\n"
2111 " "IMAGEX_PROGNAME" --version\n"
2113 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
2115 " Try `man "IMAGEX_PROGNAME"' for more information.\n"
2117 fputs(extra, stdout);
2120 /* Entry point for wimlib's ImageX implementation */
2121 int main(int argc, char **argv)
2123 const struct imagex_command *cmd;
2126 setlocale(LC_ALL, "");
2129 imagex_error("No command specified");
2134 /* Handle --help and --version for all commands. Note that this will
2135 * not return if either of these arguments are present. */
2136 help_or_version(argc, argv);
2140 /* The user may like to see more informative error messages. */
2141 wimlib_set_print_errors(true);
2143 /* Do any initializations that the library needs */
2144 ret = wimlib_global_init();
2148 /* Search for the function to handle the ImageX subcommand. */
2149 for_imagex_command(cmd) {
2150 if (strcmp(cmd->name, *argv) == 0) {
2151 ret = cmd->func(argc, argv);
2152 goto out_check_write_error;
2156 imagex_error("Unrecognized command: `%s'", argv[0]);
2159 out_check_write_error:
2160 /* For 'wimlib-imagex info' and 'wimlib-imagex dir', data printed to
2161 * standard output is part of the program's actual behavior and not just
2162 * for informational purposes, so we should set a failure exit status if
2163 * there was a write error. */
2164 if (cmd == &imagex_commands[INFO] || cmd == &imagex_commands[DIR]) {
2165 if (ferror(stdout) || fclose(stdout)) {
2166 imagex_error_with_errno("output error");
2172 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
2173 * outside of the wimlib library code. 0 indicates success. > 0
2174 * indicates a wimlib error code from which an error message can be
2177 imagex_error("Exiting with error code %d:\n"
2179 wimlib_get_error_string(ret));
2180 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
2181 imagex_error_with_errno("errno");
2184 /* Make the library free any resources it's holding (not strictly
2185 * necessary because the process is ending anyway). */
2186 wimlib_global_cleanup();