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/>.
26 #include "wimlib_tchar.h"
47 # include "imagex-win32.h"
48 # define tbasename win32_wbasename
49 # define tglob win32_wglob
53 # include <langinfo.h>
54 # define tbasename basename
59 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
61 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
80 static void usage(int cmd_type);
81 static void usage_all();
84 static const tchar *usage_strings[] = {
87 IMAGEX_PROGNAME" append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
88 " [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
89 " [--verbose] [--dereference] [--config=FILE]\n"
90 " [--threads=NUM_THREADS] [--rebuild] [--unix-data]\n"
91 " [--source-list] [--noacls]\n"
95 IMAGEX_PROGNAME" apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
96 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
97 " [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n"
102 IMAGEX_PROGNAME" capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
103 " [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
104 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
105 " [--config=FILE] [--threads=NUM_THREADS] [--unix-data]\n"
106 " [--source-list] [--noacls]\n"
110 IMAGEX_PROGNAME" delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check] [--soft]\n"
114 IMAGEX_PROGNAME" dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n"
118 IMAGEX_PROGNAME" export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
119 " DEST_WIMFILE [DEST_IMAGE_NAME] [DEST_IMAGE_DESCRIPTION]\n"
120 " [--boot] [--check] [--compress=TYPE] [--ref=\"GLOB\"]\n"
121 " [--threads=NUM_THREADS] [--rebuild]\n"
125 IMAGEX_PROGNAME" info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
126 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
127 " [--xml] [--extract-xml FILE] [--metadata]\n"
131 IMAGEX_PROGNAME" join [--check] WIMFILE SPLIT_WIM...\n"
135 IMAGEX_PROGNAME" mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
136 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
137 " [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n"
141 IMAGEX_PROGNAME" mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
142 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
143 " [--staging-dir=DIR] [--unix-data] [--allow-other]\n"
147 IMAGEX_PROGNAME" optimize WIMFILE [--check] [--recompress]\n"
151 IMAGEX_PROGNAME" split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n"
155 IMAGEX_PROGNAME" unmount DIRECTORY [--commit] [--check] [--rebuild]\n"
159 static const struct option apply_options[] = {
160 {T("check"), no_argument, NULL, 'c'},
161 {T("hardlink"), no_argument, NULL, 'h'},
162 {T("symlink"), no_argument, NULL, 's'},
163 {T("verbose"), no_argument, NULL, 'v'},
164 {T("ref"), required_argument, NULL, 'r'},
165 {T("unix-data"), no_argument, NULL, 'U'},
166 {T("noacls"), no_argument, NULL, 'N'},
169 static const struct option capture_or_append_options[] = {
170 {T("boot"), no_argument, NULL, 'b'},
171 {T("check"), no_argument, NULL, 'c'},
172 {T("compress"), required_argument, NULL, 'x'},
173 {T("config"), required_argument, NULL, 'C'},
174 {T("dereference"), no_argument, NULL, 'L'},
175 {T("flags"), required_argument, NULL, 'f'},
176 {T("verbose"), no_argument, NULL, 'v'},
177 {T("threads"), required_argument, NULL, 't'},
178 {T("rebuild"), no_argument, NULL, 'R'},
179 {T("unix-data"), no_argument, NULL, 'U'},
180 {T("source-list"), no_argument, NULL, 'S'},
181 {T("noacls"), no_argument, NULL, 'N'},
184 static const struct option delete_options[] = {
185 {T("check"), no_argument, NULL, 'c'},
186 {T("soft"), no_argument, NULL, 's'},
190 static const struct option export_options[] = {
191 {T("boot"), no_argument, NULL, 'b'},
192 {T("check"), no_argument, NULL, 'c'},
193 {T("compress"), required_argument, NULL, 'x'},
194 {T("ref"), required_argument, NULL, 'r'},
195 {T("threads"), required_argument, NULL, 't'},
196 {T("rebuild"), no_argument, NULL, 'R'},
200 static const struct option info_options[] = {
201 {T("boot"), no_argument, NULL, 'b'},
202 {T("check"), no_argument, NULL, 'c'},
203 {T("extract-xml"), required_argument, NULL, 'X'},
204 {T("header"), no_argument, NULL, 'h'},
205 {T("lookup-table"), no_argument, NULL, 'l'},
206 {T("metadata"), no_argument, NULL, 'm'},
207 {T("xml"), no_argument, NULL, 'x'},
211 static const struct option join_options[] = {
212 {T("check"), no_argument, NULL, 'c'},
216 static const struct option mount_options[] = {
217 {T("check"), no_argument, NULL, 'c'},
218 {T("debug"), no_argument, NULL, 'd'},
219 {T("streams-interface"), required_argument, NULL, 's'},
220 {T("ref"), required_argument, NULL, 'r'},
221 {T("staging-dir"), required_argument, NULL, 'D'},
222 {T("unix-data"), no_argument, NULL, 'U'},
223 {T("allow-other"), no_argument, NULL, 'A'},
227 static const struct option optimize_options[] = {
228 {T("check"), no_argument, NULL, 'c'},
229 {T("recompress"), no_argument, NULL, 'r'},
233 static const struct option split_options[] = {
234 {T("check"), no_argument, NULL, 'c'},
238 static const struct option unmount_options[] = {
239 {T("commit"), no_argument, NULL, 'c'},
240 {T("check"), no_argument, NULL, 'C'},
241 {T("rebuild"), no_argument, NULL, 'R'},
247 /* Print formatted error message to stderr. */
249 imagex_error(const tchar *format, ...)
252 va_start(va, format);
253 tfputs(T("ERROR: "), stderr);
254 tvfprintf(stderr, format, va);
255 tputc(T('\n'), stderr);
259 /* Print formatted error message to stderr. */
261 imagex_error_with_errno(const tchar *format, ...)
263 int errno_save = errno;
265 va_start(va, format);
266 tfputs(T("ERROR: "), stderr);
267 tvfprintf(stderr, format, va);
268 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
273 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
275 if (image == WIMLIB_NO_IMAGE) {
276 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
277 " Please specify a 1-based image index or "
279 " You may use `"IMAGEX_PROGNAME" info' to list the images "
280 "contained in a WIM."),
281 image_name, wim_name);
288 verify_image_is_single(int image)
290 if (image == WIMLIB_ALL_IMAGES) {
291 imagex_error(T("Cannot specify all images for this action!"));
298 verify_image_exists_and_is_single(int image, const tchar *image_name,
299 const tchar *wim_name)
302 ret = verify_image_exists(image, image_name, wim_name);
304 ret = verify_image_is_single(image);
308 /* Parse the argument to --compress */
310 get_compression_type(const tchar *optarg)
312 if (tstrcasecmp(optarg, T("maximum")) == 0 || tstrcasecmp(optarg, T("lzx")) == 0)
313 return WIMLIB_COMPRESSION_TYPE_LZX;
314 else if (tstrcasecmp(optarg, T("fast")) == 0 || tstrcasecmp(optarg, T("xpress")) == 0)
315 return WIMLIB_COMPRESSION_TYPE_XPRESS;
316 else if (tstrcasecmp(optarg, T("none")) == 0)
317 return WIMLIB_COMPRESSION_TYPE_NONE;
319 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
320 "\"maximum\", \"fast\", or \"none\"."), optarg);
321 return WIMLIB_COMPRESSION_TYPE_INVALID;
325 /* Returns the size of a file given its name, or -1 if the file does not exist
326 * or its size cannot be determined. */
328 file_get_size(const tchar *filename)
331 if (tstat(filename, &st) == 0)
337 static const tchar *default_capture_config =
343 "\\System Volume Information\n"
347 "[CompressionExclusionList]\n"
351 "\\WINDOWS\\inf\\*.pnf\n"
355 PARSE_FILENAME_SUCCESS = 0,
356 PARSE_FILENAME_FAILURE = 1,
357 PARSE_FILENAME_NONE = 2,
361 * Parses a filename in the source list file format. (See the man page for
362 * 'wimlib-imagex capture' for details on this format and the meaning.)
363 * Accepted formats for filenames are an unquoted string (whitespace-delimited),
364 * or a double or single-quoted string.
366 * @line_p: Pointer to the pointer to the line of data. Will be updated
367 * to point past the filename iff the return value is
368 * PARSE_FILENAME_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
371 * @len_p: @len_p initially stores the length of the line of data, which may
372 * be 0, and it will be updated to the number of bytes remaining in
373 * the line iff the return value is PARSE_FILENAME_SUCCESS.
375 * @fn_ret: Iff the return value is PARSE_FILENAME_SUCCESS, a pointer to the
376 * parsed filename will be returned here.
378 * Returns: PARSE_FILENAME_SUCCESS if a filename was successfully parsed; or
379 * PARSE_FILENAME_FAILURE if the data was invalid due to a missing
380 * closing quote; or PARSE_FILENAME_NONE if the line ended before the
381 * beginning of a filename was found.
384 parse_filename(tchar **line_p, size_t *len_p, tchar **fn_ret)
387 tchar *line = *line_p;
391 /* Skip leading whitespace */
394 return PARSE_FILENAME_NONE;
395 if (!istspace(*line) && *line != T('\0'))
401 if (quote_char == T('"') || quote_char == T('\'')) {
402 /* Quoted filename */
406 line = tmemchr(line, quote_char, len);
408 imagex_error(T("Missing closing quote: %"TS), fn - 1);
409 return PARSE_FILENAME_FAILURE;
412 /* Unquoted filename. Go until whitespace. Line is terminated
413 * by '\0', so no need to check 'len'. */
417 } while (!istspace(*line) && *line != T('\0'));
424 return PARSE_FILENAME_SUCCESS;
427 /* Parses a line of data (not an empty line or comment) in the source list file
428 * format. (See the man page for 'wimlib-imagex capture' for details on this
429 * format and the meaning.)
431 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
432 * len == 0. The data in @line will be modified by this function call.
434 * @len: Length of the line of data.
436 * @source: On success, the capture source and target described by the line is
437 * written into this destination. Note that it will contain pointers
438 * to data in the @line array.
440 * Returns true if the line was valid; false otherwise. */
442 parse_source_list_line(tchar *line, size_t len,
443 struct wimlib_capture_source *source)
447 ret = parse_filename(&line, &len, &source->fs_source_path);
448 if (ret != PARSE_FILENAME_SUCCESS)
450 ret = parse_filename(&line, &len, &source->wim_target_path);
451 if (ret == PARSE_FILENAME_NONE)
452 source->wim_target_path = source->fs_source_path;
453 return ret != PARSE_FILENAME_FAILURE;
456 /* Returns %true if the given line of length @len > 0 is a comment or empty line
457 * in the source list file format. */
459 is_comment_line(const tchar *line, size_t len)
464 if (!istspace(*line) && *line != T('\0'))
473 /* Parses a file in the source list format. (See the man page for
474 * 'wimlib-imagex capture' for details on this format and the meaning.)
476 * @source_list_contents: Contents of the source list file. Note that this
477 * buffer will be modified to save memory allocations,
478 * and cannot be freed until the returned array of
479 * wimlib_capture_source's has also been freed.
481 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
484 * @nsources_ret: On success, the length of the returned array is
487 * Returns: An array of `struct wimlib_capture_source's that can be passed to
488 * the wimlib_add_image_multisource() function to specify how a WIM image is to
490 static struct wimlib_capture_source *
491 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
492 size_t *nsources_ret)
496 struct wimlib_capture_source *sources;
498 tchar *source_list_contents = *source_list_contents_p;
501 for (i = 0; i < source_list_nchars; i++)
502 if (source_list_contents[i] == T('\n'))
505 /* Handle last line not terminated by a newline */
506 if (source_list_nchars != 0 &&
507 source_list_contents[source_list_nchars - 1] != T('\n'))
509 source_list_contents = realloc(source_list_contents,
510 (source_list_nchars + 1) * sizeof(tchar));
511 if (!source_list_contents)
513 source_list_contents[source_list_nchars] = T('\n');
514 *source_list_contents_p = source_list_contents;
515 source_list_nchars++;
519 sources = calloc(nlines, sizeof(*sources));
522 p = source_list_contents;
524 for (i = 0; i < nlines; i++) {
525 /* XXX: Could use rawmemchr() here instead, but it may not be
526 * available on all platforms. */
527 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
528 size_t len = endp - p + 1;
530 if (!is_comment_line(p, len)) {
531 if (!parse_source_list_line(p, len, &sources[j++])) {
542 imagex_error(T("out of memory"));
546 /* Reads the contents of a file into memory. */
548 file_get_contents(const tchar *filename, size_t *len_ret)
555 if (tstat(filename, &stbuf) != 0) {
556 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
561 fp = tfopen(filename, T("rb"));
563 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
569 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
570 "contents of file \"%"TS"\""), len, filename);
573 if (fread(buf, 1, len, fp) != len) {
574 imagex_error_with_errno(T("Failed to read %zu bytes from the "
575 "file \"%"TS"\""), len, filename);
589 /* Read standard input until EOF and return the full contents in a malloc()ed
590 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
593 stdin_get_contents(size_t *len_ret)
595 /* stdin can, of course, be a pipe or other non-seekable file, so the
596 * total length of the data cannot be pre-determined */
598 size_t newlen = 1024;
602 char *p = realloc(buf, newlen);
603 size_t bytes_read, bytes_to_read;
605 imagex_error(T("out of memory while reading stdin"));
609 bytes_to_read = newlen - pos;
610 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
612 if (bytes_read != bytes_to_read) {
617 imagex_error_with_errno(T("error reading stdin"));
631 translate_text_to_tstr(char **text_p, size_t num_bytes,
632 size_t *num_tchars_ret)
635 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
637 *num_tchars_ret = num_bytes;
639 #else /* !__WIN32__ */
640 /* On Windows, translate the text to UTF-16LE */
641 const char *text_bytestr = *text_p;
645 if (num_bytes >= 2 &&
646 ((text_bytestr[0] == 0xff && text_bytestr[1] == 0xfe) ||
647 (text_bytestr[0] <= 0x7f && text_bytestr[1] == 0x00)))
649 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
650 * with something that looks like an ASCII character encoded as
651 * a UTF-16LE code unit. Assume the file is encoded as
652 * UTF-16LE. This is not a 100% reliable check. */
653 num_wchars = num_bytes / 2;
654 text_wstr = (wchar_t*)text_bytestr;
656 /* File does not look like UTF-16LE. Assume it is encoded in
657 * the current Windows code page. I think these are always
658 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
659 * should work as expected. */
660 text_wstr = win32_mbs_to_wcs(text_bytestr,
664 *num_tchars_ret = num_wchars;
666 #endif /* __WIN32__ */
670 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
675 contents = file_get_contents(filename, &num_bytes);
678 return translate_text_to_tstr(&contents, num_bytes, num_tchars_ret);
682 stdin_get_text_contents(size_t *num_tchars_ret)
687 contents = stdin_get_contents(&num_bytes);
690 return translate_text_to_tstr(&contents, num_bytes, num_tchars_ret);
693 /* Return 0 if a path names a file to which the current user has write access;
694 * -1 otherwise (and print an error message). */
696 file_writable(const tchar *path)
699 ret = taccess(path, W_OK);
701 imagex_error_with_errno(T("Can't modify \"%"TS"\""), path);
705 #define TO_PERCENT(numerator, denominator) \
706 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
708 /* Given an enumerated value for WIM compression type, return a descriptive
711 get_data_type(int ctype)
714 case WIMLIB_COMPRESSION_TYPE_NONE:
715 return T("uncompressed");
716 case WIMLIB_COMPRESSION_TYPE_LZX:
717 return T("LZX-compressed");
718 case WIMLIB_COMPRESSION_TYPE_XPRESS:
719 return T("XPRESS-compressed");
724 /* Progress callback function passed to various wimlib functions. */
726 imagex_progress_func(enum wimlib_progress_msg msg,
727 const union wimlib_progress_info *info)
729 unsigned percent_done;
731 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
732 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
733 info->write_streams.total_bytes);
734 if (info->write_streams.completed_streams == 0) {
735 const tchar *data_type;
737 data_type = get_data_type(info->write_streams.compression_type);
738 tprintf(T("Writing %"TS" data using %u thread%"TS"\n"),
739 data_type, info->write_streams.num_threads,
740 (info->write_streams.num_threads == 1) ? T("") : T("s"));
742 tprintf(T("\r%"PRIu64" MiB of %"PRIu64" MiB (uncompressed) "
743 "written (%u%% done)"),
744 info->write_streams.completed_bytes >> 20,
745 info->write_streams.total_bytes >> 20,
747 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
750 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
751 tprintf(T("Scanning \"%"TS"\""), info->scan.source);
752 if (*info->scan.wim_target_path) {
753 tprintf(T(" (loading as WIM path: \"/%"TS"\")...\n"),
754 info->scan.wim_target_path);
756 tprintf(T(" (loading as root of WIM image)...\n"));
759 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
760 if (info->scan.excluded)
761 tprintf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
763 tprintf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
765 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
767 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
768 percent_done = TO_PERCENT(info->integrity.completed_bytes,
769 info->integrity.total_bytes);
770 tprintf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" MiB "
771 "of %"PRIu64" MiB (%u%%) done"),
772 info->integrity.filename,
773 info->integrity.completed_bytes >> 20,
774 info->integrity.total_bytes >> 20,
776 if (info->integrity.completed_bytes == info->integrity.total_bytes)
779 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
780 percent_done = TO_PERCENT(info->integrity.completed_bytes,
781 info->integrity.total_bytes);
782 tprintf(T("\rCalculating integrity table for WIM: %"PRIu64" MiB "
783 "of %"PRIu64" MiB (%u%%) done"),
784 info->integrity.completed_bytes >> 20,
785 info->integrity.total_bytes >> 20,
787 if (info->integrity.completed_bytes == info->integrity.total_bytes)
790 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
791 tprintf(T("Applying image %d (%"TS") from \"%"TS"\" "
792 "to %"TS" \"%"TS"\"\n"),
794 info->extract.image_name,
795 info->extract.wimfile_name,
796 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
797 T("NTFS volume") : T("directory")),
798 info->extract.target);
800 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
801 /*printf("Applying directory structure to %s\n",*/
802 /*info->extract.target);*/
804 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
805 percent_done = TO_PERCENT(info->extract.completed_bytes,
806 info->extract.total_bytes);
807 tprintf(T("\rExtracting files: "
808 "%"PRIu64" MiB of %"PRIu64" MiB (%u%%) done"),
809 info->extract.completed_bytes >> 20,
810 info->extract.total_bytes >> 20,
812 if (info->extract.completed_bytes >= info->extract.total_bytes)
815 case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
816 tprintf(T("%"TS"\n"), info->extract.cur_path);
818 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
819 tprintf(T("Setting timestamps on all extracted files...\n"));
821 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
822 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
823 tprintf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
824 info->extract.target);
827 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
828 percent_done = TO_PERCENT(info->join.completed_bytes,
829 info->join.total_bytes);
830 printf("Writing resources from part %u of %u: "
831 "%"PRIu64 " MiB of %"PRIu64" MiB (%u%%) written\n",
832 (info->join.completed_parts == info->join.total_parts) ?
833 info->join.completed_parts : info->join.completed_parts + 1,
834 info->join.total_parts,
835 info->join.completed_bytes >> 20,
836 info->join.total_bytes >> 20,
839 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
840 percent_done = TO_PERCENT(info->split.completed_bytes,
841 info->split.total_bytes);
842 tprintf(T("Writing \"%"TS"\": %"PRIu64" MiB of "
843 "%"PRIu64" MiB (%u%%) written\n"),
844 info->split.part_name,
845 info->split.completed_bytes >> 20,
846 info->split.total_bytes >> 20,
849 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
850 if (info->split.completed_bytes == info->split.total_bytes) {
851 tprintf(T("Finished writing %u split WIM parts\n"),
852 info->split.cur_part_number);
862 /* Open all the split WIM parts that correspond to a file glob.
864 * @first_part specifies the first part of the split WIM and it may be either
865 * included or omitted from the glob. */
867 open_swms_from_glob(const tchar *swm_glob,
868 const tchar *first_part,
870 WIMStruct ***additional_swms_ret,
871 unsigned *num_additional_swms_ret)
873 unsigned num_additional_swms = 0;
874 WIMStruct **additional_swms = NULL;
878 /* Warning: glob() is replaced in Windows native builds */
879 ret = tglob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
881 if (ret == GLOB_NOMATCH) {
882 imagex_error(T("Found no files for glob \"%"TS"\""),
885 imagex_error_with_errno(T("Failed to process glob \"%"TS"\""),
891 num_additional_swms = globbuf.gl_pathc;
892 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
893 if (!additional_swms) {
894 imagex_error(T("Out of memory"));
899 for (unsigned i = 0; i < num_additional_swms; i++) {
900 if (tstrcmp(globbuf.gl_pathv[i], first_part) == 0) {
904 ret = wimlib_open_wim(globbuf.gl_pathv[i],
905 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
906 &additional_swms[i - offset],
907 imagex_progress_func);
911 *additional_swms_ret = additional_swms;
912 *num_additional_swms_ret = num_additional_swms - offset;
916 for (unsigned i = 0; i < num_additional_swms; i++)
917 wimlib_free(additional_swms[i]);
918 free(additional_swms);
927 parse_num_threads(const tchar *optarg)
930 unsigned nthreads = tstrtoul(optarg, &tmp, 10);
931 if (nthreads == UINT_MAX || *tmp || tmp == optarg) {
932 imagex_error(T("Number of threads must be a non-negative integer!"));
940 /* Apply one image, or all images, from a WIM file into a directory, OR apply
941 * one image from a WIM file to a NTFS volume. */
943 imagex_apply(int argc, tchar **argv)
946 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
951 const tchar *wimfile;
953 const tchar *image_num_or_name;
954 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
956 const tchar *swm_glob = NULL;
957 WIMStruct **additional_swms = NULL;
958 unsigned num_additional_swms = 0;
960 for_opt(c, apply_options) {
963 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
966 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
969 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
972 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
978 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
981 extract_flags |= WIMLIB_EXTRACT_FLAG_NOACLS;
990 if (argc != 2 && argc != 3) {
997 image_num_or_name = T("1");
1000 image_num_or_name = argv[1];
1004 ret = wimlib_open_wim(wimfile, open_flags, &w, imagex_progress_func);
1008 image = wimlib_resolve_image(w, image_num_or_name);
1009 ret = verify_image_exists(image, image_num_or_name, wimfile);
1013 num_images = wimlib_get_num_images(w);
1014 if (argc == 2 && num_images != 1) {
1015 imagex_error(T("\"%"TS"\" contains %d images; Please select one "
1016 "(or all)"), wimfile, num_images);
1023 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1025 &num_additional_swms);
1032 ret = tstat(target, &stbuf);
1034 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1035 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1037 if (errno != ENOENT) {
1038 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1046 win32_acquire_restore_privileges();
1048 ret = wimlib_extract_image(w, image, target, extract_flags,
1049 additional_swms, num_additional_swms,
1050 imagex_progress_func);
1052 tprintf(T("Done applying WIM image.\n"));
1054 win32_release_restore_privileges();
1058 if (additional_swms) {
1059 for (unsigned i = 0; i < num_additional_swms; i++)
1060 wimlib_free(additional_swms[i]);
1061 free(additional_swms);
1066 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1067 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1068 * the desired image. 'wimlib-imagex append': add a new image to an existing
1071 imagex_capture_or_append(int argc, tchar **argv)
1075 int add_image_flags = 0;
1076 int write_flags = 0;
1077 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
1078 const tchar *wimfile;
1081 const tchar *flags_element = NULL;
1082 WIMStruct *w = NULL;
1085 int cmd = tstrcmp(argv[0], T("append")) ? CAPTURE : APPEND;
1086 unsigned num_threads = 0;
1089 size_t source_name_len;
1092 const tchar *config_file = NULL;
1093 tchar *config_str = NULL;
1096 bool source_list = false;
1097 size_t source_list_nchars;
1098 tchar *source_list_contents = NULL;
1099 bool capture_sources_malloced = false;
1100 struct wimlib_capture_source *capture_sources;
1103 for_opt(c, capture_or_append_options) {
1106 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1109 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1110 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1113 config_file = optarg;
1116 compression_type = get_compression_type(optarg);
1117 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1121 flags_element = optarg;
1124 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1127 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1130 num_threads = parse_num_threads(optarg);
1131 if (num_threads == UINT_MAX)
1135 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1138 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1144 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1154 if (argc < 2 || argc > 4) {
1165 /* Set default name to SOURCE argument, omitting any directory
1166 * prefixes and trailing slashes. This requires making a copy
1168 source_name_len = tstrlen(source);
1169 source_copy = alloca((source_name_len + 1) * sizeof(tchar));
1170 name = tbasename(tstrcpy(source_copy, source));
1172 /* Image description defaults to NULL if not given. */
1173 desc = (argc >= 4) ? argv[3] : NULL;
1176 /* Set up capture sources in source list mode */
1177 if (source[0] == T('-') && source[1] == T('\0')) {
1178 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1180 source_list_contents = file_get_text_contents(source,
1181 &source_list_nchars);
1183 if (!source_list_contents)
1186 capture_sources = parse_source_list(&source_list_contents,
1189 if (!capture_sources) {
1193 capture_sources_malloced = true;
1195 /* Set up capture source in non-source-list mode (could be
1196 * either "normal" mode or "NTFS mode"--- see the man page). */
1197 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1198 capture_sources[0].fs_source_path = source;
1199 capture_sources[0].wim_target_path = NULL;
1200 capture_sources[0].reserved = 0;
1205 config_str = file_get_text_contents(config_file, &config_len);
1213 ret = wimlib_open_wim(wimfile, open_flags, &w,
1214 imagex_progress_func);
1216 ret = wimlib_create_new_wim(compression_type, &w);
1222 ret = tstat(source, &stbuf);
1224 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1225 tprintf(T("Capturing WIM image from NTFS "
1226 "filesystem on \"%"TS"\"\n"), source);
1227 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1230 if (errno != ENOENT) {
1231 imagex_error_with_errno(T("Failed to stat "
1232 "\"%"TS"\""), source);
1239 win32_acquire_capture_privileges();
1242 ret = wimlib_add_image_multisource(w, capture_sources,
1244 (config_str ? config_str :
1245 default_capture_config),
1246 (config_str ? config_len :
1247 tstrlen(default_capture_config)),
1249 imagex_progress_func);
1251 goto out_release_privs;
1252 cur_image = wimlib_get_num_images(w);
1254 ret = wimlib_set_image_descripton(w, cur_image, desc);
1256 goto out_release_privs;
1258 if (flags_element) {
1259 ret = wimlib_set_image_flags(w, cur_image, flags_element);
1261 goto out_release_privs;
1263 if (cmd == APPEND) {
1264 ret = wimlib_overwrite(w, write_flags, num_threads,
1265 imagex_progress_func);
1267 ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
1268 num_threads, imagex_progress_func);
1270 if (ret == WIMLIB_ERR_REOPEN)
1273 imagex_error(T("Failed to write the WIM file \"%"TS"\""),
1277 win32_release_capture_privileges();
1282 free(source_list_contents);
1283 if (capture_sources_malloced)
1284 free(capture_sources);
1288 /* Remove image(s) from a WIM. */
1290 imagex_delete(int argc, tchar **argv)
1294 int write_flags = 0;
1295 const tchar *wimfile;
1296 const tchar *image_num_or_name;
1301 for_opt(c, delete_options) {
1304 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1305 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1308 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
1320 imagex_error(T("Must specify a WIM file"));
1322 imagex_error(T("Must specify an image"));
1327 image_num_or_name = argv[1];
1329 ret = file_writable(wimfile);
1333 ret = wimlib_open_wim(wimfile, open_flags, &w,
1334 imagex_progress_func);
1338 image = wimlib_resolve_image(w, image_num_or_name);
1340 ret = verify_image_exists(image, image_num_or_name, wimfile);
1344 ret = wimlib_delete_image(w, image);
1346 imagex_error(T("Failed to delete image from \"%"TS"\""), wimfile);
1350 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1351 if (ret == WIMLIB_ERR_REOPEN)
1354 imagex_error(T("Failed to write the file \"%"TS"\" with image "
1355 "deleted"), wimfile);
1362 /* Print the files contained in an image(s) in a WIM file. */
1364 imagex_dir(int argc, tchar **argv)
1366 const tchar *wimfile;
1373 imagex_error(T("Must specify a WIM file"));
1378 imagex_error(T("Too many arguments"));
1384 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w,
1385 imagex_progress_func);
1390 image = wimlib_resolve_image(w, argv[2]);
1391 ret = verify_image_exists(image, argv[2], wimfile);
1395 /* Image was not specified. If the WIM only contains one image,
1396 * choose that one; otherwise, print an error. */
1397 num_images = wimlib_get_num_images(w);
1398 if (num_images != 1) {
1399 imagex_error(T("The file \"%"TS"\" contains %d images; Please "
1400 "select one."), wimfile, num_images);
1408 ret = wimlib_print_files(w, image);
1414 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
1417 imagex_export(int argc, tchar **argv)
1421 int export_flags = 0;
1422 int write_flags = 0;
1423 int compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
1424 bool compression_type_specified = false;
1425 const tchar *src_wimfile;
1426 const tchar *src_image_num_or_name;
1427 const tchar *dest_wimfile;
1428 const tchar *dest_name;
1429 const tchar *dest_desc;
1430 WIMStruct *src_w = NULL;
1431 WIMStruct *dest_w = NULL;
1436 const tchar *swm_glob = NULL;
1437 WIMStruct **additional_swms = NULL;
1438 unsigned num_additional_swms = 0;
1439 unsigned num_threads = 0;
1441 for_opt(c, export_options) {
1444 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
1447 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1448 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1451 compression_type = get_compression_type(optarg);
1452 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1454 compression_type_specified = true;
1460 num_threads = parse_num_threads(optarg);
1461 if (num_threads == UINT_MAX)
1465 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1474 if (argc < 3 || argc > 5) {
1478 src_wimfile = argv[0];
1479 src_image_num_or_name = argv[1];
1480 dest_wimfile = argv[2];
1481 dest_name = (argc >= 4) ? argv[3] : NULL;
1482 dest_desc = (argc >= 5) ? argv[4] : NULL;
1483 ret = wimlib_open_wim(src_wimfile,
1484 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w,
1485 imagex_progress_func);
1489 /* Determine if the destination is an existing file or not.
1490 * If so, we try to append the exported image(s) to it; otherwise, we
1491 * create a new WIM containing the exported image(s). */
1492 if (tstat(dest_wimfile, &stbuf) == 0) {
1496 /* Destination file exists. */
1498 if (!S_ISREG(stbuf.st_mode)) {
1499 imagex_error(T("\"%"TS"\" is not a regular file"),
1504 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w,
1505 imagex_progress_func);
1509 ret = file_writable(dest_wimfile);
1513 dest_ctype = wimlib_get_compression_type(dest_w);
1514 if (compression_type_specified
1515 && compression_type != dest_ctype)
1517 imagex_error(T("Cannot specify a compression type that is "
1518 "not the same as that used in the "
1519 "destination WIM"));
1525 /* dest_wimfile is not an existing file, so create a new WIM. */
1526 if (!compression_type_specified)
1527 compression_type = wimlib_get_compression_type(src_w);
1528 if (errno == ENOENT) {
1529 ret = wimlib_create_new_wim(compression_type, &dest_w);
1533 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
1540 image = wimlib_resolve_image(src_w, src_image_num_or_name);
1541 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
1546 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
1548 &num_additional_swms);
1553 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
1554 export_flags, additional_swms,
1555 num_additional_swms, imagex_progress_func);
1561 ret = wimlib_write(dest_w, dest_wimfile, WIMLIB_ALL_IMAGES,
1562 write_flags, num_threads,
1563 imagex_progress_func);
1565 ret = wimlib_overwrite(dest_w, write_flags, num_threads,
1566 imagex_progress_func);
1568 if (ret == WIMLIB_ERR_REOPEN)
1571 wimlib_free(dest_w);
1572 if (additional_swms) {
1573 for (unsigned i = 0; i < num_additional_swms; i++)
1574 wimlib_free(additional_swms[i]);
1575 free(additional_swms);
1580 /* Prints information about a WIM file; also can mark an image as bootable,
1581 * change the name of an image, or change the description of an image. */
1583 imagex_info(int argc, tchar **argv)
1588 bool header = false;
1589 bool lookup_table = false;
1591 bool metadata = false;
1592 bool short_header = true;
1593 const tchar *xml_out_file = NULL;
1594 const tchar *wimfile;
1595 const tchar *image_num_or_name = T("all");
1596 const tchar *new_name = NULL;
1597 const tchar *new_desc = NULL;
1602 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1607 for_opt(c, info_options) {
1617 short_header = false;
1620 lookup_table = true;
1621 short_header = false;
1625 short_header = false;
1628 xml_out_file = optarg;
1629 short_header = false;
1633 short_header = false;
1643 if (argc == 0 || argc > 4) {
1649 image_num_or_name = argv[1];
1659 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1661 ret = wimlib_open_wim(wimfile, open_flags, &w,
1662 imagex_progress_func);
1666 part_number = wimlib_get_part_number(w, &total_parts);
1668 image = wimlib_resolve_image(w, image_num_or_name);
1669 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
1670 imagex_error(T("The image \"%"TS"\" does not exist"),
1673 imagex_error(T("If you would like to set the boot "
1674 "index to 0, specify image \"0\" with "
1675 "the --boot flag."));
1677 ret = WIMLIB_ERR_INVALID_IMAGE;
1681 num_images = wimlib_get_num_images(w);
1683 if (num_images == 0) {
1685 imagex_error(T("--boot is meaningless on a WIM with no "
1687 ret = WIMLIB_ERR_INVALID_IMAGE;
1692 if (image == WIMLIB_ALL_IMAGES && num_images > 1) {
1694 imagex_error(T("Cannot specify the --boot flag "
1695 "without specifying a specific "
1696 "image in a multi-image WIM"));
1697 ret = WIMLIB_ERR_INVALID_IMAGE;
1701 imagex_error(T("Cannot specify the NEW_NAME "
1702 "without specifying a specific "
1703 "image in a multi-image WIM"));
1704 ret = WIMLIB_ERR_INVALID_IMAGE;
1709 /* Operations that print information are separated from operations that
1710 * recreate the WIM file. */
1711 if (!new_name && !boot) {
1713 /* Read-only operations */
1715 if (image == WIMLIB_NO_IMAGE) {
1716 imagex_error(T("\"%"TS"\" is not a valid image"),
1718 ret = WIMLIB_ERR_INVALID_IMAGE;
1722 if (image == WIMLIB_ALL_IMAGES && short_header)
1723 wimlib_print_wim_information(w);
1726 wimlib_print_header(w);
1729 if (total_parts != 1) {
1730 tprintf(T("Warning: Only showing the lookup table "
1731 "for part %d of a %d-part WIM.\n"),
1732 part_number, total_parts);
1734 wimlib_print_lookup_table(w);
1738 ret = wimlib_extract_xml_data(w, stdout);
1744 fp = tfopen(xml_out_file, T("wb"));
1746 imagex_error_with_errno(T("Failed to open the "
1747 "file \"%"TS"\" for "
1753 ret = wimlib_extract_xml_data(w, fp);
1754 if (fclose(fp) != 0) {
1755 imagex_error(T("Failed to close the file "
1766 wimlib_print_available_images(w, image);
1769 ret = wimlib_print_metadata(w, image);
1775 /* Modification operations */
1776 if (total_parts != 1) {
1777 imagex_error(T("Modifying a split WIM is not supported."));
1781 if (image == WIMLIB_ALL_IMAGES)
1784 if (image == WIMLIB_NO_IMAGE && new_name) {
1785 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
1786 "when using image 0"), new_name);
1792 if (image == wimlib_get_boot_idx(w)) {
1793 tprintf(T("Image %d is already marked as "
1794 "bootable.\n"), image);
1797 tprintf(T("Marking image %d as bootable.\n"),
1799 wimlib_set_boot_idx(w, image);
1803 if (!tstrcmp(wimlib_get_image_name(w, image), new_name))
1805 tprintf(T("Image %d is already named \"%"TS"\".\n"),
1809 tprintf(T("Changing the name of image %d to "
1810 "\"%"TS"\".\n"), image, new_name);
1811 ret = wimlib_set_image_name(w, image, new_name);
1817 const tchar *old_desc;
1818 old_desc = wimlib_get_image_description(w, image);
1819 if (old_desc && !tstrcmp(old_desc, new_desc)) {
1820 tprintf(T("The description of image %d is already "
1821 "\"%"TS"\".\n"), image, new_desc);
1824 tprintf(T("Changing the description of image %d "
1825 "to \"%"TS"\".\n"), image, new_desc);
1826 ret = wimlib_set_image_descripton(w, image,
1833 /* Only call wimlib_overwrite() if something actually needs to
1835 if (boot || new_name || new_desc ||
1836 (check && !wimlib_has_integrity_table(w)))
1840 ret = file_writable(wimfile);
1845 write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1849 ret = wimlib_overwrite(w, write_flags, 1,
1850 imagex_progress_func);
1851 if (ret == WIMLIB_ERR_REOPEN)
1854 tprintf(T("The file \"%"TS"\" was not modified because nothing "
1855 "needed to be done.\n"), wimfile);
1864 /* Join split WIMs into one part WIM */
1866 imagex_join(int argc, tchar **argv)
1869 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1870 int wim_write_flags = 0;
1871 const tchar *output_path;
1873 for_opt(c, join_options) {
1876 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1877 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1887 imagex_error(T("Must specify one or more split WIM (.swm) "
1891 output_path = argv[0];
1892 return wimlib_join((const tchar * const *)++argv,
1897 imagex_progress_func);
1903 /* Mounts an image using a FUSE mount. */
1905 imagex_mount_rw_or_ro(int argc, tchar **argv)
1908 int mount_flags = 0;
1909 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1910 const tchar *wimfile;
1916 const tchar *swm_glob = NULL;
1917 WIMStruct **additional_swms = NULL;
1918 unsigned num_additional_swms = 0;
1919 const tchar *staging_dir = NULL;
1921 if (tstrcmp(argv[0], T("mountrw")) == 0)
1922 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1924 for_opt(c, mount_options) {
1927 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
1930 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1933 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1936 if (tstrcasecmp(optarg, T("none")) == 0)
1937 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1938 else if (tstrcasecmp(optarg, T("xattr")) == 0)
1939 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1940 else if (tstrcasecmp(optarg, T("windows")) == 0)
1941 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1943 imagex_error(T("Unknown stream interface \"%"TS"\""),
1952 staging_dir = optarg;
1955 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
1963 if (argc != 2 && argc != 3)
1968 ret = wimlib_open_wim(wimfile, open_flags, &w,
1969 imagex_progress_func);
1974 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1976 &num_additional_swms);
1983 num_images = wimlib_get_num_images(w);
1984 if (num_images != 1) {
1985 imagex_error(T("The file \"%"TS"\" contains %d images; Please "
1986 "select one."), wimfile, num_images);
1987 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
1994 image = wimlib_resolve_image(w, argv[1]);
1996 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
2001 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2002 ret = file_writable(wimfile);
2007 ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
2008 num_additional_swms, staging_dir);
2010 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
2012 image, wimfile, dir);
2017 if (additional_swms) {
2018 for (unsigned i = 0; i < num_additional_swms; i++)
2019 wimlib_free(additional_swms[i]);
2020 free(additional_swms);
2024 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2029 /* Rebuild a WIM file */
2031 imagex_optimize(int argc, tchar **argv)
2035 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
2038 const tchar *wimfile;
2042 for_opt(c, optimize_options) {
2045 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2046 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2049 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
2066 ret = wimlib_open_wim(wimfile, open_flags, &w,
2067 imagex_progress_func);
2071 old_size = file_get_size(argv[0]);
2072 tprintf(T("\"%"TS"\" original size: "), wimfile);
2074 tfputs(T("Unknown\n"), stdout);
2076 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
2078 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
2081 new_size = file_get_size(argv[0]);
2082 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
2084 tfputs(T("Unknown\n"), stdout);
2086 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
2088 tfputs(T("Space saved: "), stdout);
2089 if (new_size != -1 && old_size != -1) {
2090 tprintf(T("%lld KiB\n"),
2091 ((long long)old_size - (long long)new_size) >> 10);
2093 tfputs(T("Unknown\n"), stdout);
2101 /* Split a WIM into a spanned set */
2103 imagex_split(int argc, tchar **argv)
2106 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2107 int write_flags = 0;
2108 unsigned long part_size;
2113 for_opt(c, split_options) {
2116 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2117 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2131 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
2132 if (tmp == argv[2] || *tmp) {
2133 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
2134 imagex_error(T("The part size must be an integer or "
2135 "floating-point number of megabytes."));
2138 ret = wimlib_open_wim(argv[0], open_flags, &w, imagex_progress_func);
2141 ret = wimlib_split(w, argv[1], part_size, write_flags, imagex_progress_func);
2146 /* Unmounts a mounted WIM image. */
2148 imagex_unmount(int argc, tchar **argv)
2151 int unmount_flags = 0;
2154 for_opt(c, unmount_options) {
2157 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
2160 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
2163 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
2177 ret = wimlib_unmount_image(argv[0], unmount_flags,
2178 imagex_progress_func);
2180 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
2184 struct imagex_command {
2186 int (*func)(int , tchar **);
2191 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
2192 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
2194 static const struct imagex_command imagex_commands[] = {
2195 {T("append"), imagex_capture_or_append, APPEND},
2196 {T("apply"), imagex_apply, APPLY},
2197 {T("capture"), imagex_capture_or_append, CAPTURE},
2198 {T("delete"), imagex_delete, DELETE},
2199 {T("dir"), imagex_dir, DIR},
2200 {T("export"), imagex_export, EXPORT},
2201 {T("info"), imagex_info, INFO},
2202 {T("join"), imagex_join, JOIN},
2203 {T("mount"), imagex_mount_rw_or_ro, MOUNT},
2204 {T("mountrw"), imagex_mount_rw_or_ro, MOUNTRW},
2205 {T("optimize"),imagex_optimize, OPTIMIZE},
2206 {T("split"), imagex_split, SPLIT},
2207 {T("unmount"), imagex_unmount, UNMOUNT},
2213 static const tchar *s =
2215 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
2216 "Copyright (C) 2012, 2013 Eric Biggers\n"
2217 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
2218 "This is free software: you are free to change and redistribute it.\n"
2219 "There is NO WARRANTY, to the extent permitted by law.\n"
2221 "Report bugs to "PACKAGE_BUGREPORT".\n"
2228 help_or_version(int argc, tchar **argv)
2232 const struct imagex_command *cmd;
2234 for (i = 1; i < argc; i++) {
2242 if (!tstrcmp(p, T("help"))) {
2243 for_imagex_command(cmd) {
2244 if (!tstrcmp(cmd->name, argv[1])) {
2252 if (!tstrcmp(p, T("version"))) {
2263 const struct imagex_command *cmd;
2264 tprintf(T("Usage:\n%"TS), usage_strings[cmd_type]);
2265 for_imagex_command(cmd) {
2266 if (cmd->cmd == cmd_type) {
2267 tprintf(T("\nTry `man "IMAGEX_PROGNAME"-%"TS"' "
2268 "for more details.\n"), cmd->name);
2276 tfputs(T("Usage:\n"), stdout);
2277 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
2278 tprintf(T(" %"TS), usage_strings[i]);
2279 static const tchar *extra =
2281 " "IMAGEX_PROGNAME" --help\n"
2282 " "IMAGEX_PROGNAME" --version\n"
2284 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
2286 " Try `man "IMAGEX_PROGNAME"' for more information.\n"
2288 tfputs(extra, stdout);
2291 /* Entry point for wimlib's ImageX implementation. On UNIX the command
2292 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
2293 * something else), while an Windows the command arguments will be UTF-16LE
2294 * encoded 'wchar_t' strings. */
2297 wmain(int argc, wchar_t **argv, wchar_t **envp)
2299 main(int argc, char **argv)
2302 const struct imagex_command *cmd;
2306 setlocale(LC_ALL, "");
2308 char *codeset = nl_langinfo(CODESET);
2309 if (!strstr(codeset, "UTF-8") &&
2310 !strstr(codeset, "UTF8") &&
2311 !strstr(codeset, "utf-8") &&
2312 !strstr(codeset, "utf8"))
2315 "WARNING: Running "IMAGEX_PROGNAME" in a UTF-8 locale is recommended!\n"
2316 " (Maybe try: `export LANG=en_US.UTF-8'?\n", stderr);
2320 #endif /* !__WIN32__ */
2323 imagex_error(T("No command specified"));
2328 /* Handle --help and --version for all commands. Note that this will
2329 * not return if either of these arguments are present. */
2330 help_or_version(argc, argv);
2334 /* The user may like to see more informative error messages. */
2335 wimlib_set_print_errors(true);
2337 /* Do any initializations that the library needs */
2338 ret = wimlib_global_init();
2342 /* Search for the function to handle the ImageX subcommand. */
2343 for_imagex_command(cmd) {
2344 if (!tstrcmp(cmd->name, *argv)) {
2345 ret = cmd->func(argc, argv);
2346 goto out_check_write_error;
2350 imagex_error(T("Unrecognized command: `%"TS"'"), argv[0]);
2353 out_check_write_error:
2354 /* For 'wimlib-imagex info' and 'wimlib-imagex dir', data printed to
2355 * standard output is part of the program's actual behavior and not just
2356 * for informational purposes, so we should set a failure exit status if
2357 * there was a write error. */
2358 if (cmd == &imagex_commands[INFO] || cmd == &imagex_commands[DIR]) {
2359 if (ferror(stdout) || fclose(stdout)) {
2360 imagex_error_with_errno(T("output error"));
2366 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
2367 * outside of the wimlib library code. 0 indicates success. > 0
2368 * indicates a wimlib error code from which an error message can be
2371 imagex_error(T("Exiting with error code %d:\n"
2373 wimlib_get_error_string(ret));
2374 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
2375 imagex_error_with_errno(T("errno"));
2378 /* Make the library free any resources it's holding (not strictly
2379 * necessary because the process is ending anyway). */
2380 wimlib_global_cleanup();