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 "config.h" /* Need for PACKAGE_VERSION, etc. */
30 #include "wimlib_tchar.h"
50 # include "imagex-win32.h"
51 # define tbasename win32_wbasename
52 # define tglob win32_wglob
53 # define OS_PREFERRED_PATH_SEPARATOR L'\\'
54 # define OS_PREFERRED_PATH_SEPARATOR_STRING L"\\"
58 # include <langinfo.h>
59 # define tbasename basename
61 # define OS_PREFERRED_PATH_SEPARATOR '/'
62 # define OS_PREFERRED_PATH_SEPARATOR_STRING "/"
66 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
68 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
89 static void usage(int cmd_type);
90 static void usage_all(void);
92 static bool imagex_be_quiet = false;
95 static const tchar *usage_strings[] = {
98 IMAGEX_PROGNAME" append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
99 " [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
100 " [--verbose] [--dereference] [--config=FILE]\n"
101 " [--threads=NUM_THREADS] [--rebuild] [--unix-data]\n"
102 " [--source-list] [--no-acls] [--strict-acls]\n"
103 " [--rpfix] [--norpfix]\n"
107 IMAGEX_PROGNAME" apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
108 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
109 " [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n"
110 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
111 " [--include-invalid-names]\n"
115 IMAGEX_PROGNAME" capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
116 " [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
117 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
118 " [--config=FILE] [--threads=NUM_THREADS] [--unix-data]\n"
119 " [--source-list] [--no-acls] [--strict-acls]\n"
120 " [--rpfix] [--norpfix]\n"
124 IMAGEX_PROGNAME" delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check] [--soft]\n"
128 IMAGEX_PROGNAME" dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH]\n"
132 IMAGEX_PROGNAME" export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
133 " DEST_WIMFILE [DEST_IMAGE_NAME] [DEST_IMAGE_DESCRIPTION]\n"
134 " [--boot] [--check] [--compress=TYPE] [--ref=\"GLOB\"]\n"
135 " [--threads=NUM_THREADS] [--rebuild]\n"
139 IMAGEX_PROGNAME" extract WIMFILE (IMAGE_NUM | IMAGE_NAME) [PATH...]\n"
140 " [--check] [--ref=\"GLOB\"] [--verbose] [--unix-data]\n"
141 " [--no-acls] [--strict-acls] [--to-stdout] [--dest-dir=DIR]\n"
142 " [--include-invalid-names]\n"
146 IMAGEX_PROGNAME" info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
147 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
148 " [--xml] [--extract-xml FILE] [--metadata]\n"
152 IMAGEX_PROGNAME" join [--check] WIMFILE SPLIT_WIM...\n"
156 IMAGEX_PROGNAME" mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
157 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
158 " [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n"
162 IMAGEX_PROGNAME" mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
163 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
164 " [--staging-dir=DIR] [--unix-data] [--allow-other]\n"
168 IMAGEX_PROGNAME" optimize WIMFILE [--check] [--recompress]\n"
169 " [--threads=NUM_THREADS]\n"
173 IMAGEX_PROGNAME" split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n"
177 IMAGEX_PROGNAME" unmount DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
181 IMAGEX_PROGNAME" update WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
182 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
183 " [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
189 recommend_man_page(const tchar *cmd_name)
191 const tchar *format_str;
193 format_str = T("See "IMAGEX_PROGNAME"%"TS"%"TS".pdf in the "
194 "doc directory for more details.\n");
196 format_str = T("Try `man "IMAGEX_PROGNAME"%"TS"%"TS"' "
197 "for more details.\n");
199 tprintf(format_str, *cmd_name ? T("-") : T(""), cmd_name);
203 IMAGEX_ALLOW_OTHER_OPTION,
206 IMAGEX_COMMAND_OPTION,
207 IMAGEX_COMMIT_OPTION,
208 IMAGEX_COMPRESS_OPTION,
209 IMAGEX_CONFIG_OPTION,
211 IMAGEX_DEREFERENCE_OPTION,
212 IMAGEX_DEST_DIR_OPTION,
213 IMAGEX_EXTRACT_XML_OPTION,
216 IMAGEX_HARDLINK_OPTION,
217 IMAGEX_HEADER_OPTION,
218 IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
220 IMAGEX_LOOKUP_TABLE_OPTION,
221 IMAGEX_METADATA_OPTION,
222 IMAGEX_NORPFIX_OPTION,
223 IMAGEX_NO_ACLS_OPTION,
225 IMAGEX_REBUILD_OPTION,
226 IMAGEX_RECOMPRESS_OPTION,
227 IMAGEX_RECURSIVE_OPTION,
231 IMAGEX_SOURCE_LIST_OPTION,
232 IMAGEX_STAGING_DIR_OPTION,
233 IMAGEX_STREAMS_INTERFACE_OPTION,
234 IMAGEX_STRICT_ACLS_OPTION,
235 IMAGEX_SYMLINK_OPTION,
236 IMAGEX_THREADS_OPTION,
237 IMAGEX_TO_STDOUT_OPTION,
238 IMAGEX_UNIX_DATA_OPTION,
239 IMAGEX_VERBOSE_OPTION,
243 static const struct option apply_options[] = {
244 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
245 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
246 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
247 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
248 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
249 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
250 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
251 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
252 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
253 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
254 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
255 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
258 static const struct option capture_or_append_options[] = {
259 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
260 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
261 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
262 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
263 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
264 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
265 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
266 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
267 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
268 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
269 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
270 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
271 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
272 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
273 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
274 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
277 static const struct option delete_options[] = {
278 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
279 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
283 static const struct option dir_options[] = {
284 {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
288 static const struct option export_options[] = {
289 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
290 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
291 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
292 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
293 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
294 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
298 static const struct option extract_options[] = {
299 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
300 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
301 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
302 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
303 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
304 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
305 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
306 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
307 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
308 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
312 static const struct option info_options[] = {
313 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
314 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
315 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
316 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
317 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
318 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
319 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
323 static const struct option join_options[] = {
324 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
328 static const struct option mount_options[] = {
329 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
330 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
331 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
332 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
333 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
334 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
335 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
339 static const struct option optimize_options[] = {
340 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
341 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
342 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
346 static const struct option split_options[] = {
347 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
351 static const struct option unmount_options[] = {
352 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
353 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
354 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
355 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
359 static const struct option update_options[] = {
360 /* Careful: some of the options here set the defaults for update
361 * commands, but the flags given to an actual update command (and not to
362 * `imagex update' itself are also handled in
363 * update_command_add_option(). */
364 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
365 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
366 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
367 {T("command"), required_argument, NULL, IMAGEX_COMMAND_OPTION},
369 /* Default delete options */
370 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
371 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
373 /* Global add option */
374 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
376 /* Default add options */
377 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
378 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
379 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
380 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
381 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
382 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
387 /* Print formatted error message to stderr. */
389 imagex_error(const tchar *format, ...)
392 va_start(va, format);
393 tfputs(T("ERROR: "), stderr);
394 tvfprintf(stderr, format, va);
395 tputc(T('\n'), stderr);
399 /* Print formatted error message to stderr. */
401 imagex_error_with_errno(const tchar *format, ...)
403 int errno_save = errno;
405 va_start(va, format);
406 tfputs(T("ERROR: "), stderr);
407 tvfprintf(stderr, format, va);
408 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
413 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
415 if (image == WIMLIB_NO_IMAGE) {
416 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
417 " Please specify a 1-based image index or "
419 " You may use `"IMAGEX_PROGNAME" info' to list the images "
420 "contained in a WIM."),
421 image_name, wim_name);
428 verify_image_is_single(int image)
430 if (image == WIMLIB_ALL_IMAGES) {
431 imagex_error(T("Cannot specify all images for this action!"));
438 verify_image_exists_and_is_single(int image, const tchar *image_name,
439 const tchar *wim_name)
442 ret = verify_image_exists(image, image_name, wim_name);
444 ret = verify_image_is_single(image);
448 /* Parse the argument to --compress */
450 get_compression_type(const tchar *optarg)
452 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
453 return WIMLIB_COMPRESSION_TYPE_LZX;
454 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
455 return WIMLIB_COMPRESSION_TYPE_XPRESS;
456 else if (!tstrcasecmp(optarg, T("none")))
457 return WIMLIB_COMPRESSION_TYPE_NONE;
459 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
460 "\"maximum\", \"fast\", or \"none\"."), optarg);
461 return WIMLIB_COMPRESSION_TYPE_INVALID;
465 /* Returns the size of a file given its name, or -1 if the file does not exist
466 * or its size cannot be determined. */
468 file_get_size(const tchar *filename)
471 if (tstat(filename, &st) == 0)
477 tchar pat_ntfs_log[] = T("/$ntfs.log");
478 tchar pat_hiberfil_sys[] = T("/hiberfil.sys");
479 tchar pat_pagefile_sys[] = T("/pagefile.sys");
480 tchar pat_system_volume_information[] = T("/System Volume Information");
481 tchar pat_recycler[] = T("/RECYCLER");
482 tchar pat_windows_csc[] = T("/Windows/CSC");
484 tchar *default_pats[] = {
488 pat_system_volume_information,
493 static struct wimlib_capture_config default_capture_config = {
495 .num_pats = sizeof(default_pats) / sizeof(*default_pats),
496 .pats = default_pats,
501 PARSE_STRING_SUCCESS = 0,
502 PARSE_STRING_FAILURE = 1,
503 PARSE_STRING_NONE = 2,
507 * Parses a string token from an array of characters.
509 * Tokens are either whitespace-delimited, or double or single-quoted.
511 * @line_p: Pointer to the pointer to the line of data. Will be updated
512 * to point past the string token iff the return value is
513 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
516 * @len_p: @len_p initially stores the length of the line of data, which may
517 * be 0, and it will be updated to the number of bytes remaining in
518 * the line iff the return value is PARSE_STRING_SUCCESS.
520 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
521 * parsed string token will be returned here.
523 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
524 * PARSE_STRING_FAILURE if the data was invalid due to a missing
525 * closing quote; or PARSE_STRING_NONE if the line ended before the
526 * beginning of a string token was found.
529 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
532 tchar *line = *line_p;
536 /* Skip leading whitespace */
539 return PARSE_STRING_NONE;
540 if (!istspace(*line) && *line != T('\0'))
546 if (quote_char == T('"') || quote_char == T('\'')) {
551 line = tmemchr(line, quote_char, len);
553 imagex_error(T("Missing closing quote: %"TS), fn - 1);
554 return PARSE_STRING_FAILURE;
557 /* Unquoted string. Go until whitespace. Line is terminated
558 * by '\0', so no need to check 'len'. */
562 } while (!istspace(*line) && *line != T('\0'));
569 return PARSE_STRING_SUCCESS;
572 /* Parses a line of data (not an empty line or comment) in the source list file
573 * format. (See the man page for 'wimlib-imagex capture' for details on this
574 * format and the meaning.)
576 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
577 * len == 0. The data in @line will be modified by this function call.
579 * @len: Length of the line of data.
581 * @source: On success, the capture source and target described by the line is
582 * written into this destination. Note that it will contain pointers
583 * to data in the @line array.
585 * Returns true if the line was valid; false otherwise. */
587 parse_source_list_line(tchar *line, size_t len,
588 struct wimlib_capture_source *source)
592 ret = parse_string(&line, &len, &source->fs_source_path);
593 if (ret != PARSE_STRING_SUCCESS)
595 ret = parse_string(&line, &len, &source->wim_target_path);
596 if (ret == PARSE_STRING_NONE)
597 source->wim_target_path = source->fs_source_path;
598 return ret != PARSE_STRING_FAILURE;
601 /* Returns %true if the given line of length @len > 0 is a comment or empty line
602 * in the source list file format. */
604 is_comment_line(const tchar *line, size_t len)
609 if (!istspace(*line) && *line != T('\0'))
619 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
622 tchar *contents = *contents_p;
623 size_t nchars = *nchars_p;
626 for (i = 0; i < nchars; i++)
627 if (contents[i] == T('\n'))
630 /* Handle last line not terminated by a newline */
631 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
632 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
634 imagex_error(T("Out of memory!"));
637 contents[nchars] = T('\n');
638 *contents_p = contents;
646 /* Parses a file in the source list format. (See the man page for
647 * 'wimlib-imagex capture' for details on this format and the meaning.)
649 * @source_list_contents: Contents of the source list file. Note that this
650 * buffer will be modified to save memory allocations,
651 * and cannot be freed until the returned array of
652 * wimlib_capture_source's has also been freed.
654 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
657 * @nsources_ret: On success, the length of the returned array is
660 * Returns: An array of `struct wimlib_capture_source's that can be passed to
661 * the wimlib_add_image_multisource() function to specify how a WIM image is to
663 static struct wimlib_capture_source *
664 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
665 size_t *nsources_ret)
669 struct wimlib_capture_source *sources;
672 nlines = text_file_count_lines(source_list_contents_p,
673 &source_list_nchars);
677 /* Always allocate at least 1 slot, just in case the implementation of
678 * calloc() returns NULL if 0 bytes are requested. */
679 sources = calloc(nlines ?: 1, sizeof(*sources));
681 imagex_error(T("out of memory"));
684 p = *source_list_contents_p;
686 for (i = 0; i < nlines; i++) {
687 /* XXX: Could use rawmemchr() here instead, but it may not be
688 * available on all platforms. */
689 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
690 size_t len = endp - p + 1;
692 if (!is_comment_line(p, len)) {
693 if (!parse_source_list_line(p, len, &sources[j++])) {
706 enum capture_config_section {
707 CAPTURE_CONFIG_NO_SECTION,
708 CAPTURE_CONFIG_EXCLUSION_SECTION,
709 CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
710 CAPTURE_CONFIG_IGNORE_SECTION,
714 CAPTURE_CONFIG_INVALID_SECTION,
715 CAPTURE_CONFIG_CHANGED_SECTION,
716 CAPTURE_CONFIG_SAME_SECTION,
720 check_config_section(tchar *line, size_t len,
721 enum capture_config_section *cur_section)
723 while (istspace(*line))
727 return CAPTURE_CONFIG_SAME_SECTION;
730 tchar *endbrace = tstrrchr(line, T(']'));
732 return CAPTURE_CONFIG_SAME_SECTION;
734 if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
735 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
736 } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
737 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
738 } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
739 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
740 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
741 "of capture config file\n"),
743 } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
744 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
745 tfputs(T("WARNING: Ignoring [AlignmentList] section "
746 "of capture config file\n"),
749 imagex_error(T("Invalid capture config file section \"%"TS"\""),
751 return CAPTURE_CONFIG_INVALID_SECTION;
753 return CAPTURE_CONFIG_CHANGED_SECTION;
758 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
761 if (pat_list->num_pats == pat_list->num_allocated_pats) {
763 size_t num_allocated_pats = pat_list->num_pats + 8;
765 pats = realloc(pat_list->pats,
766 num_allocated_pats * sizeof(pat_list->pats[0]));
768 imagex_error(T("Out of memory!"));
771 pat_list->pats = pats;
772 pat_list->num_allocated_pats = num_allocated_pats;
774 pat_list->pats[pat_list->num_pats++] = pat;
779 parse_capture_config_line(tchar *line, size_t len,
780 enum capture_config_section *cur_section,
781 struct wimlib_capture_config *config)
786 ret = check_config_section(line, len, cur_section);
787 if (ret == CAPTURE_CONFIG_INVALID_SECTION)
789 if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
792 switch (*cur_section) {
793 case CAPTURE_CONFIG_NO_SECTION:
794 imagex_error(T("Line \"%"TS"\" is not in a section "
795 "(such as [ExclusionList]"), line);
797 case CAPTURE_CONFIG_EXCLUSION_SECTION:
798 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
800 return pattern_list_add_pattern(&config->exclusion_pats,
802 case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
803 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
805 return pattern_list_add_pattern(&config->exclusion_exception_pats,
807 case CAPTURE_CONFIG_IGNORE_SECTION:
814 parse_capture_config(tchar **contents_p, size_t nchars,
815 struct wimlib_capture_config *config)
820 enum capture_config_section cur_section;
822 memset(config, 0, sizeof(*config));
824 nlines = text_file_count_lines(contents_p, &nchars);
828 cur_section = CAPTURE_CONFIG_NO_SECTION;
830 for (i = 0; i < nlines; i++) {
831 tchar *endp = tmemchr(p, T('\n'), nchars);
832 size_t len = endp - p + 1;
834 if (!is_comment_line(p, len))
835 if (!parse_capture_config_line(p, len, &cur_section, config))
843 /* Reads the contents of a file into memory. */
845 file_get_contents(const tchar *filename, size_t *len_ret)
852 if (tstat(filename, &stbuf) != 0) {
853 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
858 fp = tfopen(filename, T("rb"));
860 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
866 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
867 "contents of file \"%"TS"\""), len, filename);
870 if (fread(buf, 1, len, fp) != len) {
871 imagex_error_with_errno(T("Failed to read %zu bytes from the "
872 "file \"%"TS"\""), len, filename);
886 /* Read standard input until EOF and return the full contents in a malloc()ed
887 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
890 stdin_get_contents(size_t *len_ret)
892 /* stdin can, of course, be a pipe or other non-seekable file, so the
893 * total length of the data cannot be pre-determined */
895 size_t newlen = 1024;
899 char *p = realloc(buf, newlen);
900 size_t bytes_read, bytes_to_read;
902 imagex_error(T("out of memory while reading stdin"));
906 bytes_to_read = newlen - pos;
907 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
909 if (bytes_read != bytes_to_read) {
914 imagex_error_with_errno(T("error reading stdin"));
928 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
931 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
933 *num_tchars_ret = num_bytes;
935 #else /* !__WIN32__ */
936 /* On Windows, translate the text to UTF-16LE */
940 if (num_bytes >= 2 &&
941 ((text[0] == 0xff && text[1] == 0xfe) ||
942 (text[0] <= 0x7f && text[1] == 0x00)))
944 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
945 * with something that looks like an ASCII character encoded as
946 * a UTF-16LE code unit. Assume the file is encoded as
947 * UTF-16LE. This is not a 100% reliable check. */
948 num_wchars = num_bytes / 2;
949 text_wstr = (wchar_t*)text;
951 /* File does not look like UTF-16LE. Assume it is encoded in
952 * the current Windows code page. I think these are always
953 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
954 * should work as expected. */
955 text_wstr = win32_mbs_to_wcs(text,
960 *num_tchars_ret = num_wchars;
962 #endif /* __WIN32__ */
966 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
971 contents = file_get_contents(filename, &num_bytes);
974 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
978 stdin_get_text_contents(size_t *num_tchars_ret)
983 contents = stdin_get_contents(&num_bytes);
986 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
989 #define TO_PERCENT(numerator, denominator) \
990 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
992 /* Given an enumerated value for WIM compression type, return a descriptive
995 get_data_type(int ctype)
998 case WIMLIB_COMPRESSION_TYPE_NONE:
999 return T("uncompressed");
1000 case WIMLIB_COMPRESSION_TYPE_LZX:
1001 return T("LZX-compressed");
1002 case WIMLIB_COMPRESSION_TYPE_XPRESS:
1003 return T("XPRESS-compressed");
1008 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
1009 #define MEBIBYTE_MIN_NBYTES 10000000ULL
1010 #define KIBIBYTE_MIN_NBYTES 10000ULL
1013 get_unit(uint64_t total_bytes, const tchar **name_ret)
1015 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
1016 *name_ret = T("GiB");
1018 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
1019 *name_ret = T("MiB");
1021 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
1022 *name_ret = T("KiB");
1025 *name_ret = T("bytes");
1030 /* Progress callback function passed to various wimlib functions. */
1032 imagex_progress_func(enum wimlib_progress_msg msg,
1033 const union wimlib_progress_info *info)
1035 unsigned percent_done;
1036 unsigned unit_shift;
1037 const tchar *unit_name;
1038 if (imagex_be_quiet)
1041 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1042 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1043 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1044 info->write_streams.total_bytes);
1045 if (info->write_streams.completed_streams == 0) {
1046 const tchar *data_type;
1048 data_type = get_data_type(info->write_streams.compression_type);
1049 tprintf(T("Writing %"TS" data using %u thread%"TS"\n"),
1050 data_type, info->write_streams.num_threads,
1051 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1053 tprintf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1054 "written (%u%% done)"),
1055 info->write_streams.completed_bytes >> unit_shift,
1057 info->write_streams.total_bytes >> unit_shift,
1060 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1063 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1064 tprintf(T("Scanning \"%"TS"\""), info->scan.source);
1065 if (*info->scan.wim_target_path) {
1066 tprintf(T(" (loading as WIM path: "
1067 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
1068 info->scan.wim_target_path);
1070 tprintf(T(" (loading as root of WIM image)...\n"));
1073 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1074 if (info->scan.excluded)
1075 tprintf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1077 tprintf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
1079 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
1081 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1082 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1083 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1084 info->integrity.total_bytes);
1085 tprintf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1086 "of %"PRIu64" %"TS" (%u%%) done"),
1087 info->integrity.filename,
1088 info->integrity.completed_bytes >> unit_shift,
1090 info->integrity.total_bytes >> unit_shift,
1093 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1096 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1097 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1098 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1099 info->integrity.total_bytes);
1100 tprintf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1101 "of %"PRIu64" %"TS" (%u%%) done"),
1102 info->integrity.completed_bytes >> unit_shift,
1104 info->integrity.total_bytes >> unit_shift,
1107 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1110 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1111 tprintf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1112 "to %"TS" \"%"TS"\"\n"),
1113 info->extract.image,
1114 info->extract.image_name,
1115 info->extract.wimfile_name,
1116 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1117 T("NTFS volume") : T("directory")),
1118 info->extract.target);
1120 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1121 tprintf(T("Extracting "
1122 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\" from image %d (\"%"TS"\") "
1123 "in \"%"TS"\" to \"%"TS"\"\n"),
1124 info->extract.extract_root_wim_source_path,
1125 info->extract.image,
1126 info->extract.image_name,
1127 info->extract.wimfile_name,
1128 info->extract.target);
1130 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
1131 /*tprintf(T("Applying directory structure to %"TS"\n"),*/
1132 /*info->extract.target);*/
1134 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1135 percent_done = TO_PERCENT(info->extract.completed_bytes,
1136 info->extract.total_bytes);
1137 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1138 tprintf(T("\rExtracting files: "
1139 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1140 info->extract.completed_bytes >> unit_shift,
1142 info->extract.total_bytes >> unit_shift,
1145 if (info->extract.completed_bytes >= info->extract.total_bytes)
1148 case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
1149 tprintf(T("%"TS"\n"), info->extract.cur_path);
1151 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1152 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1153 tprintf(T("Setting timestamps on all extracted files...\n"));
1155 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1156 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1157 tprintf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1158 info->extract.target);
1161 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
1162 percent_done = TO_PERCENT(info->join.completed_bytes,
1163 info->join.total_bytes);
1164 unit_shift = get_unit(info->join.total_bytes, &unit_name);
1165 tprintf(T("Writing resources from part %u of %u: "
1166 "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written\n"),
1167 (info->join.completed_parts == info->join.total_parts) ?
1168 info->join.completed_parts : info->join.completed_parts + 1,
1169 info->join.total_parts,
1170 info->join.completed_bytes >> unit_shift,
1172 info->join.total_bytes >> unit_shift,
1176 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1177 percent_done = TO_PERCENT(info->split.completed_bytes,
1178 info->split.total_bytes);
1179 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1180 tprintf(T("Writing \"%"TS"\": %"PRIu64" %"TS" of "
1181 "%"PRIu64" %"TS" (%u%%) written\n"),
1182 info->split.part_name,
1183 info->split.completed_bytes >> unit_shift,
1185 info->split.total_bytes >> unit_shift,
1189 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1190 if (info->split.completed_bytes == info->split.total_bytes) {
1191 tprintf(T("Finished writing %u split WIM parts\n"),
1192 info->split.cur_part_number);
1195 case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1196 switch (info->update.command->op) {
1197 case WIMLIB_UPDATE_OP_DELETE:
1198 tprintf(T("Deleted WIM path "
1199 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1200 info->update.command->delete.wim_path);
1202 case WIMLIB_UPDATE_OP_RENAME:
1203 tprintf(T("Renamed WIM path "
1204 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
1205 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1206 info->update.command->rename.wim_source_path,
1207 info->update.command->rename.wim_target_path);
1209 case WIMLIB_UPDATE_OP_ADD:
1221 /* Open all the split WIM parts that correspond to a file glob.
1223 * @first_part specifies the first part of the split WIM and it may be either
1224 * included or omitted from the glob. */
1226 open_swms_from_glob(const tchar *swm_glob,
1227 const tchar *first_part,
1229 WIMStruct ***additional_swms_ret,
1230 unsigned *num_additional_swms_ret)
1232 unsigned num_additional_swms = 0;
1233 WIMStruct **additional_swms = NULL;
1237 /* Warning: glob() is replaced in Windows native builds */
1238 ret = tglob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
1240 if (ret == GLOB_NOMATCH) {
1241 imagex_error(T("Found no files for glob \"%"TS"\""),
1244 imagex_error_with_errno(T("Failed to process glob \"%"TS"\""),
1250 num_additional_swms = globbuf.gl_pathc;
1251 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
1252 if (!additional_swms) {
1253 imagex_error(T("Out of memory"));
1257 unsigned offset = 0;
1258 for (unsigned i = 0; i < num_additional_swms; i++) {
1259 if (tstrcmp(globbuf.gl_pathv[i], first_part) == 0) {
1263 ret = wimlib_open_wim(globbuf.gl_pathv[i],
1264 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
1265 &additional_swms[i - offset],
1266 imagex_progress_func);
1268 goto out_close_swms;
1270 *additional_swms_ret = additional_swms;
1271 *num_additional_swms_ret = num_additional_swms - offset;
1275 for (unsigned i = 0; i < num_additional_swms; i++)
1276 wimlib_free(additional_swms[i]);
1277 free(additional_swms);
1286 parse_num_threads(const tchar *optarg)
1289 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1290 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1291 imagex_error(T("Number of threads must be a non-negative integer!"));
1299 * Parse an option passed to an update command.
1301 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1304 * @option: Text string for the option (beginning with --)
1306 * @cmd: `struct wimlib_update_command' that is being constructed for
1309 * Returns true if the option was recognized; false if not.
1312 update_command_add_option(int op, const tchar *option,
1313 struct wimlib_update_command *cmd)
1315 bool recognized = true;
1317 case WIMLIB_UPDATE_OP_ADD:
1318 if (!tstrcmp(option, T("--verbose")))
1319 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1320 else if (!tstrcmp(option, T("--unix-data")))
1321 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1322 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1323 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1324 else if (!tstrcmp(option, T("--strict-acls")))
1325 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1326 else if (!tstrcmp(option, T("--dereference")))
1327 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1331 case WIMLIB_UPDATE_OP_DELETE:
1332 if (!tstrcmp(option, T("--force")))
1333 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1334 else if (!tstrcmp(option, T("--recursive")))
1335 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1346 /* How many nonoption arguments each `imagex update' command expects */
1347 static const unsigned update_command_num_nonoptions[] = {
1348 [WIMLIB_UPDATE_OP_ADD] = 2,
1349 [WIMLIB_UPDATE_OP_DELETE] = 1,
1350 [WIMLIB_UPDATE_OP_RENAME] = 2,
1354 update_command_add_nonoption(int op, const tchar *nonoption,
1355 struct wimlib_update_command *cmd,
1356 unsigned num_nonoptions)
1359 case WIMLIB_UPDATE_OP_ADD:
1360 if (num_nonoptions == 0)
1361 cmd->add.fs_source_path = (tchar*)nonoption;
1363 cmd->add.wim_target_path = (tchar*)nonoption;
1365 case WIMLIB_UPDATE_OP_DELETE:
1366 cmd->delete.wim_path = (tchar*)nonoption;
1368 case WIMLIB_UPDATE_OP_RENAME:
1369 if (num_nonoptions == 0)
1370 cmd->rename.wim_source_path = (tchar*)nonoption;
1372 cmd->rename.wim_target_path = (tchar*)nonoption;
1378 * Parse a command passed on stdin to `imagex update'.
1380 * @line: Text of the command.
1381 * @len: Length of the line, including a null terminator
1384 * @command: A `struct wimlib_update_command' to fill in from the parsed
1387 * @line_number: Line number of the command, for diagnostics.
1389 * Returns true on success; returns false on parse error.
1392 parse_update_command(tchar *line, size_t len,
1393 struct wimlib_update_command *command,
1397 tchar *command_name;
1399 size_t num_nonoptions;
1401 /* Get the command name ("add", "delete", "rename") */
1402 ret = parse_string(&line, &len, &command_name);
1403 if (ret != PARSE_STRING_SUCCESS)
1406 if (!tstrcasecmp(command_name, T("add"))) {
1407 op = WIMLIB_UPDATE_OP_ADD;
1408 } else if (!tstrcasecmp(command_name, T("delete"))) {
1409 op = WIMLIB_UPDATE_OP_DELETE;
1410 } else if (!tstrcasecmp(command_name, T("rename"))) {
1411 op = WIMLIB_UPDATE_OP_RENAME;
1413 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1414 command_name, line_number);
1419 /* Parse additional options and non-options as needed */
1424 ret = parse_string(&line, &len, &next_string);
1425 if (ret == PARSE_STRING_NONE) /* End of line */
1427 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1429 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1431 if (!update_command_add_option(op, next_string, command))
1433 imagex_error(T("Unrecognized option \"%"TS"\" to "
1434 "update command \"%"TS"\" on line %zu"),
1435 next_string, command_name, line_number);
1441 if (num_nonoptions == update_command_num_nonoptions[op])
1443 imagex_error(T("Unexpected argument \"%"TS"\" in "
1444 "update command on line %zu\n"
1445 " (The \"%"TS"\" command only "
1446 "takes %u nonoption arguments!)\n"),
1447 next_string, line_number,
1448 command_name, num_nonoptions);
1451 update_command_add_nonoption(op, next_string,
1452 command, num_nonoptions);
1457 if (num_nonoptions != update_command_num_nonoptions[op]) {
1458 imagex_error(T("Not enough arguments to update command "
1459 "\"%"TS"\" on line %zu"), command_name, line_number);
1465 static struct wimlib_update_command *
1466 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1467 size_t *num_cmds_ret)
1471 struct wimlib_update_command *cmds;
1474 nlines = text_file_count_lines(cmd_file_contents_p,
1479 /* Always allocate at least 1 slot, just in case the implementation of
1480 * calloc() returns NULL if 0 bytes are requested. */
1481 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1483 imagex_error(T("out of memory"));
1486 p = *cmd_file_contents_p;
1488 for (i = 0; i < nlines; i++) {
1489 /* XXX: Could use rawmemchr() here instead, but it may not be
1490 * available on all platforms. */
1491 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1492 size_t len = endp - p + 1;
1494 if (!is_comment_line(p, len)) {
1495 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1506 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1507 * one image from a WIM file to a NTFS volume. */
1509 imagex_apply(int argc, tchar **argv)
1512 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1517 const tchar *wimfile;
1518 const tchar *target;
1519 const tchar *image_num_or_name;
1520 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1522 const tchar *swm_glob = NULL;
1523 WIMStruct **additional_swms = NULL;
1524 unsigned num_additional_swms = 0;
1526 for_opt(c, apply_options) {
1528 case IMAGEX_CHECK_OPTION:
1529 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1531 case IMAGEX_HARDLINK_OPTION:
1532 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1534 case IMAGEX_SYMLINK_OPTION:
1535 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1537 case IMAGEX_VERBOSE_OPTION:
1538 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
1540 case IMAGEX_REF_OPTION:
1543 case IMAGEX_UNIX_DATA_OPTION:
1544 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1546 case IMAGEX_NO_ACLS_OPTION:
1547 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1549 case IMAGEX_STRICT_ACLS_OPTION:
1550 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1552 case IMAGEX_NORPFIX_OPTION:
1553 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1555 case IMAGEX_RPFIX_OPTION:
1556 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1558 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1559 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1560 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1569 if (argc != 2 && argc != 3) {
1576 image_num_or_name = T("1");
1579 image_num_or_name = argv[1];
1583 ret = wimlib_open_wim(wimfile, open_flags, &w, imagex_progress_func);
1587 image = wimlib_resolve_image(w, image_num_or_name);
1588 ret = verify_image_exists(image, image_num_or_name, wimfile);
1592 num_images = wimlib_get_num_images(w);
1593 if (argc == 2 && num_images != 1) {
1594 imagex_error(T("\"%"TS"\" contains %d images; Please select one "
1595 "(or all)."), wimfile, num_images);
1602 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1604 &num_additional_swms);
1611 ret = tstat(target, &stbuf);
1613 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1614 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1616 if (errno != ENOENT) {
1617 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1625 win32_acquire_restore_privileges();
1627 ret = wimlib_extract_image(w, image, target, extract_flags,
1628 additional_swms, num_additional_swms,
1629 imagex_progress_func);
1631 tprintf(T("Done applying WIM image.\n"));
1633 win32_release_restore_privileges();
1637 if (additional_swms) {
1638 for (unsigned i = 0; i < num_additional_swms; i++)
1639 wimlib_free(additional_swms[i]);
1640 free(additional_swms);
1645 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1646 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1647 * the desired image. 'wimlib-imagex append': add a new image to an existing
1650 imagex_capture_or_append(int argc, tchar **argv)
1653 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1654 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
1655 int write_flags = 0;
1656 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
1657 const tchar *wimfile;
1660 const tchar *flags_element = NULL;
1664 int cmd = tstrcmp(argv[0], T("append")) ? CAPTURE : APPEND;
1665 unsigned num_threads = 0;
1668 size_t source_name_len;
1671 const tchar *config_file = NULL;
1673 struct wimlib_capture_config *config = NULL;
1675 bool source_list = false;
1676 size_t source_list_nchars;
1677 tchar *source_list_contents;
1678 bool capture_sources_malloced;
1679 struct wimlib_capture_source *capture_sources;
1681 bool name_defaulted;
1683 for_opt(c, capture_or_append_options) {
1685 case IMAGEX_BOOT_OPTION:
1686 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1688 case IMAGEX_CHECK_OPTION:
1689 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1690 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1692 case IMAGEX_CONFIG_OPTION:
1693 config_file = optarg;
1695 case IMAGEX_COMPRESS_OPTION:
1696 compression_type = get_compression_type(optarg);
1697 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1700 case IMAGEX_FLAGS_OPTION:
1701 flags_element = optarg;
1703 case IMAGEX_DEREFERENCE_OPTION:
1704 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1706 case IMAGEX_VERBOSE_OPTION:
1707 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1709 case IMAGEX_THREADS_OPTION:
1710 num_threads = parse_num_threads(optarg);
1711 if (num_threads == UINT_MAX)
1714 case IMAGEX_REBUILD_OPTION:
1715 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1717 case IMAGEX_UNIX_DATA_OPTION:
1718 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1720 case IMAGEX_SOURCE_LIST_OPTION:
1723 case IMAGEX_NO_ACLS_OPTION:
1724 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1726 case IMAGEX_STRICT_ACLS_OPTION:
1727 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1729 case IMAGEX_RPFIX_OPTION:
1730 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1732 case IMAGEX_NORPFIX_OPTION:
1733 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1743 if (argc < 2 || argc > 4) {
1753 name_defaulted = false;
1755 /* Set default name to SOURCE argument, omitting any directory
1756 * prefixes and trailing slashes. This requires making a copy
1757 * of @source. Leave some free characters at the end in case we
1758 * append a number to keep the name unique. */
1759 source_name_len = tstrlen(source);
1760 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1761 name = tbasename(tstrcpy(source_copy, source));
1762 name_defaulted = true;
1764 /* Image description defaults to NULL if not given. */
1765 desc = (argc >= 4) ? argv[3] : NULL;
1768 /* Set up capture sources in source list mode */
1769 if (source[0] == T('-') && source[1] == T('\0')) {
1770 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1772 source_list_contents = file_get_text_contents(source,
1773 &source_list_nchars);
1775 if (!source_list_contents)
1778 capture_sources = parse_source_list(&source_list_contents,
1781 if (!capture_sources) {
1783 goto out_free_source_list_contents;
1785 capture_sources_malloced = true;
1787 /* Set up capture source in non-source-list mode (could be
1788 * either "normal" mode or "NTFS mode"--- see the man page). */
1789 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1790 capture_sources[0].fs_source_path = source;
1791 capture_sources[0].wim_target_path = NULL;
1792 capture_sources[0].reserved = 0;
1794 capture_sources_malloced = false;
1795 source_list_contents = NULL;
1801 config_str = file_get_text_contents(config_file, &config_len);
1804 goto out_free_capture_sources;
1807 config = alloca(sizeof(*config));
1808 ret = parse_capture_config(&config_str, config_len, config);
1810 goto out_free_config;
1812 config = &default_capture_config;
1816 ret = wimlib_open_wim(wimfile, open_flags, &w,
1817 imagex_progress_func);
1819 ret = wimlib_create_new_wim(compression_type, &w);
1821 goto out_free_config;
1825 ret = tstat(source, &stbuf);
1827 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1828 tprintf(T("Capturing WIM image from NTFS "
1829 "filesystem on \"%"TS"\"\n"), source);
1830 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1833 if (errno != ENOENT) {
1834 imagex_error_with_errno(T("Failed to stat "
1835 "\"%"TS"\""), source);
1837 goto out_wimlib_free;
1842 if (cmd == APPEND && name_defaulted) {
1843 /* If the user did not specify an image name, and the basename
1844 * of the source already exists as an image name in the WIM
1845 * file, append a suffix to make it unique. */
1846 unsigned long conflict_idx;
1847 tchar *name_end = tstrchr(name, T('\0'));
1848 for (conflict_idx = 1;
1849 wimlib_image_name_in_use(w, name);
1852 tsprintf(name_end, T(" (%lu)"), conflict_idx);
1856 win32_acquire_capture_privileges();
1859 ret = wimlib_add_image_multisource(w,
1865 imagex_progress_func);
1867 goto out_release_privs;
1868 cur_image = wimlib_get_num_images(w);
1870 ret = wimlib_set_image_descripton(w, cur_image, desc);
1872 goto out_release_privs;
1874 if (flags_element) {
1875 ret = wimlib_set_image_flags(w, cur_image, flags_element);
1877 goto out_release_privs;
1879 if (cmd == APPEND) {
1880 ret = wimlib_overwrite(w, write_flags, num_threads,
1881 imagex_progress_func);
1883 ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
1884 num_threads, imagex_progress_func);
1887 imagex_error(T("Failed to write the WIM file \"%"TS"\""),
1891 win32_release_capture_privileges();
1896 if (config != NULL && config != &default_capture_config) {
1897 free(config->exclusion_pats.pats);
1898 free(config->exclusion_exception_pats.pats);
1901 out_free_capture_sources:
1902 if (capture_sources_malloced)
1903 free(capture_sources);
1904 out_free_source_list_contents:
1905 free(source_list_contents);
1909 /* Remove image(s) from a WIM. */
1911 imagex_delete(int argc, tchar **argv)
1914 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1915 int write_flags = 0;
1916 const tchar *wimfile;
1917 const tchar *image_num_or_name;
1922 for_opt(c, delete_options) {
1924 case IMAGEX_CHECK_OPTION:
1925 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1926 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1928 case IMAGEX_SOFT_OPTION:
1929 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
1941 imagex_error(T("Must specify a WIM file"));
1943 imagex_error(T("Must specify an image"));
1948 image_num_or_name = argv[1];
1950 ret = wimlib_open_wim(wimfile, open_flags, &w,
1951 imagex_progress_func);
1955 image = wimlib_resolve_image(w, image_num_or_name);
1957 ret = verify_image_exists(image, image_num_or_name, wimfile);
1961 ret = wimlib_delete_image(w, image);
1963 imagex_error(T("Failed to delete image from \"%"TS"\""), wimfile);
1967 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1969 imagex_error(T("Failed to write the file \"%"TS"\" with image "
1970 "deleted"), wimfile);
1978 print_full_path(const struct wimlib_dir_entry *wdentry, void *_ignore)
1980 int ret = tprintf(T("%"TS"\n"), wdentry->full_path);
1981 return (ret >= 0) ? 0 : -1;
1984 /* Print the files contained in an image(s) in a WIM file. */
1986 imagex_dir(int argc, tchar **argv)
1988 const tchar *wimfile;
1989 WIMStruct *wim = NULL;
1992 const tchar *path = T("");
1995 for_opt(c, dir_options) {
1997 case IMAGEX_PATH_OPTION:
2008 imagex_error(T("Must specify a WIM file"));
2012 imagex_error(T("Too many arguments"));
2017 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &wim,
2018 imagex_progress_func);
2023 image = wimlib_resolve_image(wim, argv[1]);
2024 ret = verify_image_exists(image, argv[1], wimfile);
2026 goto out_wimlib_free;
2028 /* Image was not specified. If the WIM only contains one image,
2029 * choose that one; otherwise, print an error. */
2030 int num_images = wimlib_get_num_images(wim);
2031 if (num_images != 1) {
2032 imagex_error(T("\"%"TS"\" contains %d images; Please "
2033 "select one."), wimfile, num_images);
2040 ret = wimlib_iterate_dir_tree(wim, image, path,
2041 WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE,
2042 print_full_path, NULL);
2053 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2056 imagex_export(int argc, tchar **argv)
2060 int export_flags = 0;
2061 int write_flags = 0;
2062 int compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
2063 bool compression_type_specified = false;
2064 const tchar *src_wimfile;
2065 const tchar *src_image_num_or_name;
2066 const tchar *dest_wimfile;
2067 const tchar *dest_name;
2068 const tchar *dest_desc;
2069 WIMStruct *src_w = NULL;
2070 WIMStruct *dest_w = NULL;
2075 const tchar *swm_glob = NULL;
2076 WIMStruct **additional_swms = NULL;
2077 unsigned num_additional_swms = 0;
2078 unsigned num_threads = 0;
2080 for_opt(c, export_options) {
2082 case IMAGEX_BOOT_OPTION:
2083 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2085 case IMAGEX_CHECK_OPTION:
2086 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2087 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2089 case IMAGEX_COMPRESS_OPTION:
2090 compression_type = get_compression_type(optarg);
2091 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2093 compression_type_specified = true;
2095 case IMAGEX_REF_OPTION:
2098 case IMAGEX_THREADS_OPTION:
2099 num_threads = parse_num_threads(optarg);
2100 if (num_threads == UINT_MAX)
2103 case IMAGEX_REBUILD_OPTION:
2104 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2113 if (argc < 3 || argc > 5) {
2117 src_wimfile = argv[0];
2118 src_image_num_or_name = argv[1];
2119 dest_wimfile = argv[2];
2120 dest_name = (argc >= 4) ? argv[3] : NULL;
2121 dest_desc = (argc >= 5) ? argv[4] : NULL;
2122 ret = wimlib_open_wim(src_wimfile,
2123 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w,
2124 imagex_progress_func);
2128 /* Determine if the destination is an existing file or not.
2129 * If so, we try to append the exported image(s) to it; otherwise, we
2130 * create a new WIM containing the exported image(s). */
2131 if (tstat(dest_wimfile, &stbuf) == 0) {
2135 /* Destination file exists. */
2137 if (!S_ISREG(stbuf.st_mode)) {
2138 imagex_error(T("\"%"TS"\" is not a regular file"),
2143 ret = wimlib_open_wim(dest_wimfile, open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2144 &dest_w, imagex_progress_func);
2148 dest_ctype = wimlib_get_compression_type(dest_w);
2149 if (compression_type_specified
2150 && compression_type != dest_ctype)
2152 imagex_error(T("Cannot specify a compression type that is "
2153 "not the same as that used in the "
2154 "destination WIM"));
2160 /* dest_wimfile is not an existing file, so create a new WIM. */
2161 if (!compression_type_specified)
2162 compression_type = wimlib_get_compression_type(src_w);
2163 if (errno == ENOENT) {
2164 ret = wimlib_create_new_wim(compression_type, &dest_w);
2168 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2175 image = wimlib_resolve_image(src_w, src_image_num_or_name);
2176 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2181 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
2183 &num_additional_swms);
2188 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
2189 export_flags, additional_swms,
2190 num_additional_swms, imagex_progress_func);
2196 ret = wimlib_write(dest_w, dest_wimfile, WIMLIB_ALL_IMAGES,
2197 write_flags, num_threads,
2198 imagex_progress_func);
2200 ret = wimlib_overwrite(dest_w, write_flags, num_threads,
2201 imagex_progress_func);
2204 wimlib_free(dest_w);
2205 if (additional_swms) {
2206 for (unsigned i = 0; i < num_additional_swms; i++)
2207 wimlib_free(additional_swms[i]);
2208 free(additional_swms);
2214 is_root_wim_path(const tchar *path)
2217 for (p = path; *p; p++)
2218 if (*p != T('\\') && *p != T('/'))
2224 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2225 const tchar *dest_dir)
2227 for (size_t i = 0; i < num_cmds; i++)
2228 if (cmds[i].fs_dest_path != dest_dir)
2229 free(cmds[i].fs_dest_path);
2233 static struct wimlib_extract_command *
2234 prepare_extract_commands(tchar **paths, unsigned num_paths,
2235 int extract_flags, tchar *dest_dir,
2236 size_t *num_cmds_ret)
2238 struct wimlib_extract_command *cmds;
2240 tchar *emptystr = T("");
2242 if (num_paths == 0) {
2246 num_cmds = num_paths;
2247 cmds = calloc(num_cmds, sizeof(cmds[0]));
2249 imagex_error(T("Out of memory!"));
2253 for (size_t i = 0; i < num_cmds; i++) {
2254 cmds[i].extract_flags = extract_flags;
2255 cmds[i].wim_source_path = paths[i];
2256 if (is_root_wim_path(paths[i])) {
2257 cmds[i].fs_dest_path = dest_dir;
2259 size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2260 cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2261 if (!cmds[i].fs_dest_path) {
2262 free_extract_commands(cmds, num_cmds, dest_dir);
2265 tsprintf(cmds[i].fs_dest_path, T("%"TS""OS_PREFERRED_PATH_SEPARATOR_STRING"%"TS),
2266 dest_dir, tbasename(paths[i]));
2269 *num_cmds_ret = num_cmds;
2273 /* Extract files or directories from a WIM image */
2275 imagex_extract(int argc, tchar **argv)
2278 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2282 const tchar *wimfile;
2283 const tchar *image_num_or_name;
2284 tchar *dest_dir = T(".");
2285 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL | WIMLIB_EXTRACT_FLAG_NORPFIX;
2287 const tchar *swm_glob = NULL;
2288 WIMStruct **additional_swms = NULL;
2289 unsigned num_additional_swms = 0;
2291 struct wimlib_extract_command *cmds;
2294 for_opt(c, extract_options) {
2296 case IMAGEX_CHECK_OPTION:
2297 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2299 case IMAGEX_VERBOSE_OPTION:
2300 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
2302 case IMAGEX_REF_OPTION:
2305 case IMAGEX_UNIX_DATA_OPTION:
2306 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2308 case IMAGEX_NO_ACLS_OPTION:
2309 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2311 case IMAGEX_STRICT_ACLS_OPTION:
2312 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2314 case IMAGEX_DEST_DIR_OPTION:
2317 case IMAGEX_TO_STDOUT_OPTION:
2318 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2319 imagex_be_quiet = true;
2321 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2322 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2323 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2336 image_num_or_name = argv[1];
2341 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2348 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2352 image = wimlib_resolve_image(wim, image_num_or_name);
2353 ret = verify_image_exists_and_is_single(image,
2357 goto out_wimlib_free;
2360 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2362 &num_additional_swms);
2364 goto out_wimlib_free;
2368 win32_acquire_restore_privileges();
2371 ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
2372 additional_swms, num_additional_swms,
2373 imagex_progress_func);
2375 if (!imagex_be_quiet)
2376 tprintf(T("Done extracting files.\n"));
2377 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2378 tfprintf(stderr, T("Note: You can use `"IMAGEX_PROGNAME" dir' to see what "
2379 "files and directories\n"
2380 " are in the WIM image.\n"));
2383 win32_release_restore_privileges();
2385 if (additional_swms) {
2386 for (unsigned i = 0; i < num_additional_swms; i++)
2387 wimlib_free(additional_swms[i]);
2388 free(additional_swms);
2393 free_extract_commands(cmds, num_cmds, dest_dir);
2402 static void print_byte_field(const uint8_t field[], size_t len)
2405 tprintf(T("%02hhx"), *field++);
2409 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2411 tputs(T("WIM Information:"));
2412 tputs(T("----------------"));
2413 tprintf(T("Path: %"TS"\n"), wimfile);
2414 tprintf(T("GUID: 0x"));
2415 print_byte_field(info->guid, sizeof(info->guid));
2417 tprintf(T("Image Count: %d\n"), info->image_count);
2418 tprintf(T("Compression: %"TS"\n"),
2419 wimlib_get_compression_type_string(info->compression_type));
2420 tprintf(T("Part Number: %d/%d\n"), info->part_number, info->total_parts);
2421 tprintf(T("Boot Index: %d\n"), info->boot_index);
2422 tprintf(T("Size: %"PRIu64" bytes\n"), info->total_bytes);
2423 tprintf(T("Integrity Info: %"TS"\n"),
2424 info->has_integrity_table ? T("yes") : T("no"));
2425 tprintf(T("Relative path junction: %"TS"\n"),
2426 info->has_rpfix ? T("yes") : T("no"));
2431 print_resource(const struct wimlib_resource_entry *resource,
2435 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
2436 resource->uncompressed_size);
2438 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
2439 resource->compressed_size);
2441 tprintf(T("Offset = %"PRIu64" bytes\n"),
2445 tprintf(T("Part Number = %u\n"), resource->part_number);
2446 tprintf(T("Reference Count = %u\n"), resource->reference_count);
2448 tprintf(T("Hash = 0x"));
2449 print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2452 tprintf(T("Flags = "));
2453 if (resource->is_compressed)
2454 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED "));
2455 if (resource->is_metadata)
2456 tprintf(T("WIM_RESHDR_FLAG_METADATA "));
2457 if (resource->is_free)
2458 tprintf(T("WIM_RESHDR_FLAG_FREE "));
2459 if (resource->is_spanned)
2460 tprintf(T("WIM_RESHDR_FLAG_SPANNED "));
2467 print_lookup_table(WIMStruct *wim)
2469 wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2472 /* Prints information about a WIM file; also can mark an image as bootable,
2473 * change the name of an image, or change the description of an image. */
2475 imagex_info(int argc, tchar **argv)
2480 bool header = false;
2481 bool lookup_table = false;
2483 bool metadata = false;
2484 bool short_header = true;
2485 const tchar *xml_out_file = NULL;
2486 const tchar *wimfile;
2487 const tchar *image_num_or_name = T("all");
2488 const tchar *new_name = NULL;
2489 const tchar *new_desc = NULL;
2494 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2495 struct wimlib_wim_info info;
2497 for_opt(c, info_options) {
2499 case IMAGEX_BOOT_OPTION:
2502 case IMAGEX_CHECK_OPTION:
2505 case IMAGEX_HEADER_OPTION:
2507 short_header = false;
2509 case IMAGEX_LOOKUP_TABLE_OPTION:
2510 lookup_table = true;
2511 short_header = false;
2513 case IMAGEX_XML_OPTION:
2515 short_header = false;
2517 case IMAGEX_EXTRACT_XML_OPTION:
2518 xml_out_file = optarg;
2519 short_header = false;
2521 case IMAGEX_METADATA_OPTION:
2523 short_header = false;
2533 if (argc == 0 || argc > 4) {
2539 image_num_or_name = argv[1];
2549 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2551 ret = wimlib_open_wim(wimfile, open_flags, &w,
2552 imagex_progress_func);
2556 wimlib_get_wim_info(w, &info);
2558 image = wimlib_resolve_image(w, image_num_or_name);
2559 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
2560 imagex_error(T("The image \"%"TS"\" does not exist"),
2563 imagex_error(T("If you would like to set the boot "
2564 "index to 0, specify image \"0\" with "
2565 "the --boot flag."));
2567 ret = WIMLIB_ERR_INVALID_IMAGE;
2571 if (info.image_count == 0) {
2573 imagex_error(T("--boot is meaningless on a WIM with no "
2575 ret = WIMLIB_ERR_INVALID_IMAGE;
2580 if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
2582 imagex_error(T("Cannot specify the --boot flag "
2583 "without specifying a specific "
2584 "image in a multi-image WIM"));
2585 ret = WIMLIB_ERR_INVALID_IMAGE;
2589 imagex_error(T("Cannot specify the NEW_NAME "
2590 "without specifying a specific "
2591 "image in a multi-image WIM"));
2592 ret = WIMLIB_ERR_INVALID_IMAGE;
2597 /* Operations that print information are separated from operations that
2598 * recreate the WIM file. */
2599 if (!new_name && !boot) {
2601 /* Read-only operations */
2603 if (image == WIMLIB_NO_IMAGE) {
2604 imagex_error(T("\"%"TS"\" is not a valid image"),
2606 ret = WIMLIB_ERR_INVALID_IMAGE;
2610 if (image == WIMLIB_ALL_IMAGES && short_header)
2611 print_wim_information(wimfile, &info);
2614 wimlib_print_header(w);
2617 if (info.total_parts != 1) {
2618 tfprintf(stderr, T("Warning: Only showing the lookup table "
2619 "for part %d of a %d-part WIM.\n"),
2620 info.part_number, info.total_parts);
2622 print_lookup_table(w);
2626 ret = wimlib_extract_xml_data(w, stdout);
2632 fp = tfopen(xml_out_file, T("wb"));
2634 imagex_error_with_errno(T("Failed to open the "
2635 "file \"%"TS"\" for "
2641 ret = wimlib_extract_xml_data(w, fp);
2642 if (fclose(fp) != 0) {
2643 imagex_error(T("Failed to close the file "
2654 wimlib_print_available_images(w, image);
2657 ret = wimlib_print_metadata(w, image);
2663 /* Modification operations */
2665 if (image == WIMLIB_ALL_IMAGES)
2668 if (image == WIMLIB_NO_IMAGE && new_name) {
2669 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
2670 "when using image 0"), new_name);
2676 if (image == wimlib_get_boot_idx(w)) {
2677 tprintf(T("Image %d is already marked as "
2678 "bootable.\n"), image);
2681 tprintf(T("Marking image %d as bootable.\n"),
2683 info.boot_index = image;
2684 ret = wimlib_set_wim_info(w, &info, WIMLIB_CHANGE_BOOT_INDEX);
2690 if (!tstrcmp(wimlib_get_image_name(w, image), new_name))
2692 tprintf(T("Image %d is already named \"%"TS"\".\n"),
2696 tprintf(T("Changing the name of image %d to "
2697 "\"%"TS"\".\n"), image, new_name);
2698 ret = wimlib_set_image_name(w, image, new_name);
2704 const tchar *old_desc;
2705 old_desc = wimlib_get_image_description(w, image);
2706 if (old_desc && !tstrcmp(old_desc, new_desc)) {
2707 tprintf(T("The description of image %d is already "
2708 "\"%"TS"\".\n"), image, new_desc);
2711 tprintf(T("Changing the description of image %d "
2712 "to \"%"TS"\".\n"), image, new_desc);
2713 ret = wimlib_set_image_descripton(w, image,
2720 /* Only call wimlib_overwrite() if something actually needs to
2722 if (boot || new_name || new_desc ||
2723 (check && !wimlib_has_integrity_table(w)))
2725 int write_flags = 0;
2728 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2729 ret = wimlib_overwrite(w, write_flags, 1,
2730 imagex_progress_func);
2732 tprintf(T("The file \"%"TS"\" was not modified because nothing "
2733 "needed to be done.\n"), wimfile);
2742 /* Join split WIMs into one part WIM */
2744 imagex_join(int argc, tchar **argv)
2747 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2748 int wim_write_flags = 0;
2749 const tchar *output_path;
2751 for_opt(c, join_options) {
2753 case IMAGEX_CHECK_OPTION:
2754 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2755 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2765 imagex_error(T("Must specify one or more split WIM (.swm) "
2769 output_path = argv[0];
2770 return wimlib_join((const tchar * const *)++argv,
2775 imagex_progress_func);
2781 /* Mounts an image using a FUSE mount. */
2783 imagex_mount_rw_or_ro(int argc, tchar **argv)
2786 int mount_flags = 0;
2787 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2788 const tchar *wimfile;
2794 const tchar *swm_glob = NULL;
2795 WIMStruct **additional_swms = NULL;
2796 unsigned num_additional_swms = 0;
2797 const tchar *staging_dir = NULL;
2799 if (!tstrcmp(argv[0], T("mountrw"))) {
2800 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
2801 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2804 for_opt(c, mount_options) {
2806 case IMAGEX_ALLOW_OTHER_OPTION:
2807 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
2809 case IMAGEX_CHECK_OPTION:
2810 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2812 case IMAGEX_DEBUG_OPTION:
2813 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
2815 case IMAGEX_STREAMS_INTERFACE_OPTION:
2816 if (!tstrcasecmp(optarg, T("none")))
2817 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
2818 else if (!tstrcasecmp(optarg, T("xattr")))
2819 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2820 else if (!tstrcasecmp(optarg, T("windows")))
2821 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
2823 imagex_error(T("Unknown stream interface \"%"TS"\""),
2828 case IMAGEX_REF_OPTION:
2831 case IMAGEX_STAGING_DIR_OPTION:
2832 staging_dir = optarg;
2834 case IMAGEX_UNIX_DATA_OPTION:
2835 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
2843 if (argc != 2 && argc != 3)
2848 ret = wimlib_open_wim(wimfile, open_flags, &w,
2849 imagex_progress_func);
2854 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2856 &num_additional_swms);
2863 num_images = wimlib_get_num_images(w);
2864 if (num_images != 1) {
2865 imagex_error(T("\"%"TS"\" contains %d images; Please "
2866 "select one."), wimfile, num_images);
2867 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2874 image = wimlib_resolve_image(w, argv[1]);
2876 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
2881 ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
2882 num_additional_swms, staging_dir);
2884 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
2886 image, wimfile, dir);
2891 if (additional_swms) {
2892 for (unsigned i = 0; i < num_additional_swms; i++)
2893 wimlib_free(additional_swms[i]);
2894 free(additional_swms);
2898 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2903 /* Rebuild a WIM file */
2905 imagex_optimize(int argc, tchar **argv)
2908 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2909 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
2912 const tchar *wimfile;
2915 unsigned num_threads = 0;
2917 for_opt(c, optimize_options) {
2919 case IMAGEX_CHECK_OPTION:
2920 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2921 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2923 case IMAGEX_RECOMPRESS_OPTION:
2924 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
2926 case IMAGEX_THREADS_OPTION:
2927 num_threads = parse_num_threads(optarg);
2928 if (num_threads == UINT_MAX)
2946 ret = wimlib_open_wim(wimfile, open_flags, &w,
2947 imagex_progress_func);
2951 old_size = file_get_size(argv[0]);
2952 tprintf(T("\"%"TS"\" original size: "), wimfile);
2954 tfputs(T("Unknown\n"), stdout);
2956 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
2958 ret = wimlib_overwrite(w, write_flags, num_threads,
2959 imagex_progress_func);
2962 new_size = file_get_size(argv[0]);
2963 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
2965 tfputs(T("Unknown\n"), stdout);
2967 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
2969 tfputs(T("Space saved: "), stdout);
2970 if (new_size != -1 && old_size != -1) {
2971 tprintf(T("%lld KiB\n"),
2972 ((long long)old_size - (long long)new_size) >> 10);
2974 tfputs(T("Unknown\n"), stdout);
2982 /* Split a WIM into a spanned set */
2984 imagex_split(int argc, tchar **argv)
2987 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2988 int write_flags = 0;
2989 unsigned long part_size;
2994 for_opt(c, split_options) {
2996 case IMAGEX_CHECK_OPTION:
2997 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2998 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3012 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3013 if (tmp == argv[2] || *tmp) {
3014 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3015 imagex_error(T("The part size must be an integer or "
3016 "floating-point number of megabytes."));
3019 ret = wimlib_open_wim(argv[0], open_flags, &w, imagex_progress_func);
3022 ret = wimlib_split(w, argv[1], part_size, write_flags, imagex_progress_func);
3027 /* Unmounts a mounted WIM image. */
3029 imagex_unmount(int argc, tchar **argv)
3032 int unmount_flags = 0;
3035 for_opt(c, unmount_options) {
3037 case IMAGEX_COMMIT_OPTION:
3038 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3040 case IMAGEX_CHECK_OPTION:
3041 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3043 case IMAGEX_REBUILD_OPTION:
3044 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3046 case IMAGEX_LAZY_OPTION:
3047 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3061 ret = wimlib_unmount_image(argv[0], unmount_flags,
3062 imagex_progress_func);
3064 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3069 * Add, delete, or rename files in a WIM image.
3072 imagex_update(int argc, tchar **argv)
3074 const tchar *wimfile;
3075 const tchar *image_num_or_name;
3079 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3080 int write_flags = 0;
3081 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3082 int default_add_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
3083 int default_delete_flags = 0;
3084 unsigned num_threads = 0;
3086 tchar *cmd_file_contents = NULL;
3087 size_t cmd_file_nchars;
3088 struct wimlib_update_command *cmds;
3091 tchar *command_str = NULL;
3093 const tchar *config_file = NULL;
3095 struct wimlib_capture_config *config = NULL;
3097 for_opt(c, update_options) {
3099 /* Generic or write options */
3100 case IMAGEX_THREADS_OPTION:
3101 num_threads = parse_num_threads(optarg);
3102 if (num_threads == UINT_MAX) {
3107 case IMAGEX_CHECK_OPTION:
3108 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3109 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3111 case IMAGEX_REBUILD_OPTION:
3112 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3114 case IMAGEX_COMMAND_OPTION:
3116 imagex_error(T("--command may only be specified "
3117 "one time. Please provide\n"
3118 " the update commands "
3119 "on standard input instead."));
3123 command_str = tstrdup(optarg);
3125 imagex_error(T("Out of memory!"));
3130 /* Default delete options */
3131 case IMAGEX_FORCE_OPTION:
3132 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3134 case IMAGEX_RECURSIVE_OPTION:
3135 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3138 /* Global add option */
3139 case IMAGEX_CONFIG_OPTION:
3140 config_file = optarg;
3143 /* Default add options */
3144 case IMAGEX_VERBOSE_OPTION:
3145 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
3147 case IMAGEX_DEREFERENCE_OPTION:
3148 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
3150 case IMAGEX_UNIX_DATA_OPTION:
3151 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
3153 case IMAGEX_NO_ACLS_OPTION:
3154 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
3156 case IMAGEX_STRICT_ACLS_OPTION:
3157 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
3166 if (argc < 1 || argc > 2)
3170 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3175 image_num_or_name = argv[1];
3177 image_num_or_name = T("1");
3179 image = wimlib_resolve_image(wim, image_num_or_name);
3181 ret = verify_image_exists_and_is_single(image, image_num_or_name,
3184 goto out_wimlib_free;
3186 num_images = wimlib_get_num_images(wim);
3187 if (argc == 1 && num_images != 1) {
3188 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3189 wimfile, num_images);
3192 goto out_wimlib_free;
3195 /* Parse capture configuration file if specified */
3199 config_str = file_get_text_contents(config_file, &config_len);
3202 goto out_wimlib_free;
3205 config = alloca(sizeof(*config));
3206 ret = parse_capture_config(&config_str, config_len, config);
3208 goto out_free_config;
3210 config = &default_capture_config;
3213 /* Read update commands from standard input, or the command string if
3216 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3219 if (isatty(STDIN_FILENO)) {
3220 tputs(T("Reading update commands from standard input..."));
3221 recommend_man_page(T("update"));
3223 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3224 if (!cmd_file_contents) {
3226 goto out_free_config;
3229 /* Parse the update commands */
3230 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3235 goto out_free_cmd_file_contents;
3238 /* Set default flags and capture config on the update commands */
3239 bool have_add_command = false;
3240 for (size_t i = 0; i < num_cmds; i++) {
3241 switch (cmds[i].op) {
3242 case WIMLIB_UPDATE_OP_ADD:
3243 cmds[i].add.add_flags |= default_add_flags;
3244 cmds[i].add.config = config;
3245 have_add_command = true;
3247 case WIMLIB_UPDATE_OP_DELETE:
3248 cmds[i].delete.delete_flags |= default_delete_flags;
3256 if (have_add_command)
3257 win32_acquire_capture_privileges();
3260 /* Execute the update commands */
3261 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3262 imagex_progress_func);
3264 goto out_release_privs;
3266 /* Overwrite the updated WIM */
3267 ret = wimlib_overwrite(wim, write_flags, num_threads,
3268 imagex_progress_func);
3271 if (have_add_command)
3272 win32_release_capture_privileges();
3275 out_free_cmd_file_contents:
3276 free(cmd_file_contents);
3278 if (config != NULL && config != &default_capture_config) {
3279 free(config->exclusion_pats.pats);
3280 free(config->exclusion_exception_pats.pats);
3294 struct imagex_command {
3296 int (*func)(int , tchar **);
3301 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
3302 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
3304 static const struct imagex_command imagex_commands[] = {
3305 {T("append"), imagex_capture_or_append, APPEND},
3306 {T("apply"), imagex_apply, APPLY},
3307 {T("capture"), imagex_capture_or_append, CAPTURE},
3308 {T("delete"), imagex_delete, DELETE},
3309 {T("dir"), imagex_dir, DIR},
3310 {T("export"), imagex_export, EXPORT},
3311 {T("extract"), imagex_extract, EXTRACT},
3312 {T("info"), imagex_info, INFO},
3313 {T("join"), imagex_join, JOIN},
3314 {T("mount"), imagex_mount_rw_or_ro, MOUNT},
3315 {T("mountrw"), imagex_mount_rw_or_ro, MOUNTRW},
3316 {T("optimize"),imagex_optimize, OPTIMIZE},
3317 {T("split"), imagex_split, SPLIT},
3318 {T("unmount"), imagex_unmount, UNMOUNT},
3319 {T("update"), imagex_update, UPDATE},
3325 static const tchar *s =
3327 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
3328 "Copyright (C) 2012, 2013 Eric Biggers\n"
3329 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
3330 "This is free software: you are free to change and redistribute it.\n"
3331 "There is NO WARRANTY, to the extent permitted by law.\n"
3333 "Report bugs to "PACKAGE_BUGREPORT".\n"
3340 help_or_version(int argc, tchar **argv)
3344 const struct imagex_command *cmd;
3346 for (i = 1; i < argc; i++) {
3354 if (!tstrcmp(p, T("help"))) {
3355 for_imagex_command(cmd) {
3356 if (!tstrcmp(cmd->name, argv[1])) {
3364 if (!tstrcmp(p, T("version"))) {
3375 const struct imagex_command *cmd;
3376 tprintf(T("Usage:\n%"TS), usage_strings[cmd_type]);
3377 for_imagex_command(cmd) {
3378 if (cmd->cmd == cmd_type) {
3379 tputc(T('\n'), stdout);
3380 recommend_man_page(cmd->name);
3388 tfputs(T("Usage:\n"), stdout);
3389 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
3390 tprintf(T(" %"TS), usage_strings[i]);
3391 static const tchar *extra =
3393 " "IMAGEX_PROGNAME" --help\n"
3394 " "IMAGEX_PROGNAME" --version\n"
3396 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
3399 tfputs(extra, stdout);
3400 recommend_man_page(T(""));
3403 /* Entry point for wimlib's ImageX implementation. On UNIX the command
3404 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
3405 * something else), while an Windows the command arguments will be UTF-16LE
3406 * encoded 'wchar_t' strings. */
3409 wmain(int argc, wchar_t **argv, wchar_t **envp)
3411 main(int argc, char **argv)
3414 const struct imagex_command *cmd;
3419 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
3420 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
3424 setlocale(LC_ALL, "");
3425 codeset = nl_langinfo(CODESET);
3426 if (!strstr(codeset, "UTF-8") &&
3427 !strstr(codeset, "UTF8") &&
3428 !strstr(codeset, "utf-8") &&
3429 !strstr(codeset, "utf8"))
3432 "WARNING: Running "IMAGEX_PROGNAME" in a UTF-8 locale is recommended!\n"
3433 " Maybe try: `export LANG=en_US.UTF-8'?\n"
3434 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
3435 " to any value to force wimlib to use UTF-8.\n",
3440 #endif /* !__WIN32__ */
3443 imagex_error(T("No command specified"));
3449 /* Handle --help and --version for all commands. Note that this will
3450 * not return if either of these arguments are present. */
3451 help_or_version(argc, argv);
3455 /* The user may like to see more informative error messages. */
3456 wimlib_set_print_errors(true);
3458 /* Do any initializations that the library needs */
3459 ret = wimlib_global_init(init_flags);
3461 goto out_check_status;
3463 /* Search for the function to handle the ImageX subcommand. */
3464 for_imagex_command(cmd) {
3465 if (!tstrcmp(cmd->name, *argv)) {
3466 ret = cmd->func(argc, argv);
3467 goto out_check_write_error;
3471 imagex_error(T("Unrecognized command: `%"TS"'"), argv[0]);
3475 out_check_write_error:
3476 /* For 'wimlib-imagex info' and 'wimlib-imagex dir', data printed to
3477 * standard output is part of the program's actual behavior and not just
3478 * for informational purposes, so we should set a failure exit status if
3479 * there was a write error. */
3480 if (cmd == &imagex_commands[INFO] || cmd == &imagex_commands[DIR]) {
3481 if (ferror(stdout) || fclose(stdout)) {
3482 imagex_error_with_errno(T("error writing to standard output"));
3488 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
3489 * outside of the wimlib library code. 0 indicates success. > 0
3490 * indicates a wimlib error code from which an error message can be
3493 imagex_error(T("Exiting with error code %d:\n"
3495 wimlib_get_error_string(ret));
3496 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
3497 imagex_error_with_errno(T("errno"));
3500 /* Make the library free any resources it's holding (not strictly
3501 * necessary because the process is ending anyway). */
3502 wimlib_global_cleanup();