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 OS_PREFERRED_PATH_SEPARATOR L'\\'
53 # define OS_PREFERRED_PATH_SEPARATOR_STRING L"\\"
57 # include <langinfo.h>
58 # define tbasename basename
59 # define OS_PREFERRED_PATH_SEPARATOR '/'
60 # define OS_PREFERRED_PATH_SEPARATOR_STRING "/"
61 static inline void set_fd_to_binary_mode(int fd)
66 /* Don't confuse the user by presenting the mounting commands on Windows when
67 * they will never work. However on UNIX-like systems we always present them,
68 * even if WITH_FUSE is not defined at this point, as to not tie the build of
69 * wimlib-imagex to a specific build of wimlib. */
71 # define WIM_MOUNTING_SUPPORTED 0
73 # define WIM_MOUNTING_SUPPORTED 1
76 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
78 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
92 #if WIM_MOUNTING_SUPPORTED
98 #if WIM_MOUNTING_SUPPORTED
105 static void usage(int cmd, FILE *fp);
106 static void usage_all(FILE *fp);
107 static void recommend_man_page(int cmd, FILE *fp);
108 static const tchar *get_cmd_string(int cmd, bool nospace);
110 static int imagex_progress_func(enum wimlib_progress_msg msg,
111 const union wimlib_progress_info *info);
113 static bool imagex_be_quiet = false;
114 static FILE *imagex_info_file;
116 #define imagex_printf(format, ...) \
117 tfprintf(imagex_info_file, format, ##__VA_ARGS__)
120 IMAGEX_ALLOW_OTHER_OPTION,
123 IMAGEX_COMMAND_OPTION,
124 IMAGEX_COMMIT_OPTION,
125 IMAGEX_COMPRESS_OPTION,
126 IMAGEX_CONFIG_OPTION,
128 IMAGEX_DELTA_FROM_OPTION,
129 IMAGEX_DEREFERENCE_OPTION,
130 IMAGEX_DEST_DIR_OPTION,
131 IMAGEX_EXTRACT_XML_OPTION,
134 IMAGEX_HARDLINK_OPTION,
135 IMAGEX_HEADER_OPTION,
136 IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
138 IMAGEX_LOOKUP_TABLE_OPTION,
139 IMAGEX_METADATA_OPTION,
140 IMAGEX_NORPFIX_OPTION,
141 IMAGEX_NOCHECK_OPTION,
142 IMAGEX_NO_ACLS_OPTION,
143 IMAGEX_NOT_PIPABLE_OPTION,
145 IMAGEX_PIPABLE_OPTION,
146 IMAGEX_REBUILD_OPTION,
147 IMAGEX_RECOMPRESS_OPTION,
148 IMAGEX_RECURSIVE_OPTION,
150 IMAGEX_RESUME_OPTION,
153 IMAGEX_SOURCE_LIST_OPTION,
154 IMAGEX_STAGING_DIR_OPTION,
155 IMAGEX_STREAMS_INTERFACE_OPTION,
156 IMAGEX_STRICT_ACLS_OPTION,
157 IMAGEX_SYMLINK_OPTION,
158 IMAGEX_THREADS_OPTION,
159 IMAGEX_TO_STDOUT_OPTION,
160 IMAGEX_UNIX_DATA_OPTION,
161 IMAGEX_VERBOSE_OPTION,
165 static const struct option apply_options[] = {
166 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
167 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
168 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
169 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
170 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
171 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
172 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
173 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
174 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
175 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
176 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
177 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
179 /* --resume is undocumented for now as it needs improvement. */
180 {T("resume"), no_argument, NULL, IMAGEX_RESUME_OPTION},
184 static const struct option capture_or_append_options[] = {
185 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
186 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
187 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
188 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
189 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
190 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
191 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
192 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
193 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
194 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
195 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
196 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
197 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
198 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
199 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
200 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
201 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
202 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
203 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
204 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
205 {T("delta-from"), required_argument, NULL, IMAGEX_DELTA_FROM_OPTION},
209 static const struct option delete_options[] = {
210 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
211 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
215 static const struct option dir_options[] = {
216 {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
220 static const struct option export_options[] = {
221 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
222 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
223 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
224 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
225 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
226 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
227 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
228 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
229 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
230 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
234 static const struct option extract_options[] = {
235 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
236 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
237 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
238 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
239 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
240 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
241 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
242 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
243 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
244 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
248 static const struct option info_options[] = {
249 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
250 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
251 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
252 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
253 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
254 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
255 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
256 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
257 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
261 static const struct option join_options[] = {
262 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
266 static const struct option mount_options[] = {
267 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
268 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
269 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
270 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
271 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
272 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
273 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
277 static const struct option optimize_options[] = {
278 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
279 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
280 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
281 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
282 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
283 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
284 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
288 static const struct option split_options[] = {
289 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
293 static const struct option unmount_options[] = {
294 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
295 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
296 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
297 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
301 static const struct option update_options[] = {
302 /* Careful: some of the options here set the defaults for update
303 * commands, but the flags given to an actual update command (and not to
304 * `imagex update' itself are also handled in
305 * update_command_add_option(). */
306 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
307 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
308 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
309 {T("command"), required_argument, NULL, IMAGEX_COMMAND_OPTION},
311 /* Default delete options */
312 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
313 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
315 /* Global add option */
316 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
318 /* Default add options */
319 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
320 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
321 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
322 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
323 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
324 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
330 # define _format_attribute(type, format_str, args_start) \
331 __attribute__((format(type, format_str, args_start)))
333 # define _format_attribute(type, format_str, args_start)
336 /* Print formatted error message to stderr. */
337 static void _format_attribute(printf, 1, 2)
338 imagex_error(const tchar *format, ...)
341 va_start(va, format);
342 tfputs(T("ERROR: "), stderr);
343 tvfprintf(stderr, format, va);
344 tputc(T('\n'), stderr);
348 /* Print formatted error message to stderr. */
349 static void _format_attribute(printf, 1, 2)
350 imagex_error_with_errno(const tchar *format, ...)
352 int errno_save = errno;
354 va_start(va, format);
355 tfputs(T("ERROR: "), stderr);
356 tvfprintf(stderr, format, va);
357 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
362 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
364 if (image == WIMLIB_NO_IMAGE) {
365 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
366 " Please specify a 1-based image index or "
368 " You may use `%"TS"' to list the images "
369 "contained in a WIM."),
370 image_name, wim_name, get_cmd_string(CMD_INFO, false));
377 verify_image_is_single(int image)
379 if (image == WIMLIB_ALL_IMAGES) {
380 imagex_error(T("Cannot specify all images for this action!"));
387 verify_image_exists_and_is_single(int image, const tchar *image_name,
388 const tchar *wim_name)
391 ret = verify_image_exists(image, image_name, wim_name);
393 ret = verify_image_is_single(image);
397 /* Parse the argument to --compress */
399 get_compression_type(const tchar *optarg)
401 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
402 return WIMLIB_COMPRESSION_TYPE_LZX;
403 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
404 return WIMLIB_COMPRESSION_TYPE_XPRESS;
405 else if (!tstrcasecmp(optarg, T("none")))
406 return WIMLIB_COMPRESSION_TYPE_NONE;
408 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
409 "\"maximum\", \"fast\", or \"none\"."), optarg);
410 return WIMLIB_COMPRESSION_TYPE_INVALID;
417 unsigned num_alloc_globs;
420 #define REFGLOB_SET_INITIALIZER \
421 { .globs = NULL, .num_globs = 0, .num_alloc_globs = 0, }
423 #define REFGLOB_SET(_refglobs) \
424 struct refglob_set _refglobs = REFGLOB_SET_INITIALIZER
427 refglob_set_append(struct refglob_set *set, const tchar *glob)
429 unsigned num_alloc_globs = set->num_alloc_globs;
431 if (set->num_globs == num_alloc_globs) {
432 const tchar **new_globs;
434 num_alloc_globs += 4;
435 new_globs = realloc(set->globs, sizeof(set->globs[0]) * num_alloc_globs);
437 imagex_error(T("Out of memory!"));
440 set->globs = new_globs;
441 set->num_alloc_globs = num_alloc_globs;
443 set->globs[set->num_globs++] = glob;
448 wim_reference_globs(WIMStruct *wim, struct refglob_set *set, int open_flags)
450 return wimlib_reference_resource_files(wim, set->globs, set->num_globs,
451 WIMLIB_REF_FLAG_GLOB_ENABLE,
453 imagex_progress_func);
457 refglob_set_destroy(struct refglob_set *set)
463 do_resource_not_found_warning(const tchar *wimfile,
464 const struct wimlib_wim_info *info,
465 const struct refglob_set *refglobs)
467 if (info->total_parts > 1) {
468 if (refglobs->num_globs == 0) {
469 imagex_error(T("\"%"TS"\" is part of a split WIM. "
470 "Use --ref to specify the other parts."),
473 imagex_error(T("Perhaps the '--ref' argument did not "
474 "specify all other parts of the split "
478 imagex_error(T("If this is a delta WIM, use the --ref argument "
479 "to specify the WIM on which it is based."));
483 /* Returns the size of a file given its name, or -1 if the file does not exist
484 * or its size cannot be determined. */
486 file_get_size(const tchar *filename)
489 if (tstat(filename, &st) == 0)
496 PARSE_STRING_SUCCESS = 0,
497 PARSE_STRING_FAILURE = 1,
498 PARSE_STRING_NONE = 2,
502 * Parses a string token from an array of characters.
504 * Tokens are either whitespace-delimited, or double or single-quoted.
506 * @line_p: Pointer to the pointer to the line of data. Will be updated
507 * to point past the string token iff the return value is
508 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
511 * @len_p: @len_p initially stores the length of the line of data, which may
512 * be 0, and it will be updated to the number of bytes remaining in
513 * the line iff the return value is PARSE_STRING_SUCCESS.
515 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
516 * parsed string token will be returned here.
518 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
519 * PARSE_STRING_FAILURE if the data was invalid due to a missing
520 * closing quote; or PARSE_STRING_NONE if the line ended before the
521 * beginning of a string token was found.
524 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
527 tchar *line = *line_p;
531 /* Skip leading whitespace */
534 return PARSE_STRING_NONE;
535 if (!istspace(*line) && *line != T('\0'))
541 if (quote_char == T('"') || quote_char == T('\'')) {
546 line = tmemchr(line, quote_char, len);
548 imagex_error(T("Missing closing quote: %"TS), fn - 1);
549 return PARSE_STRING_FAILURE;
552 /* Unquoted string. Go until whitespace. Line is terminated
553 * by '\0', so no need to check 'len'. */
557 } while (!istspace(*line) && *line != T('\0'));
564 return PARSE_STRING_SUCCESS;
567 /* Parses a line of data (not an empty line or comment) in the source list file
568 * format. (See the man page for 'wimlib-imagex capture' for details on this
569 * format and the meaning.)
571 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
572 * len == 0. The data in @line will be modified by this function call.
574 * @len: Length of the line of data.
576 * @source: On success, the capture source and target described by the line is
577 * written into this destination. Note that it will contain pointers
578 * to data in the @line array.
580 * Returns true if the line was valid; false otherwise. */
582 parse_source_list_line(tchar *line, size_t len,
583 struct wimlib_capture_source *source)
587 ret = parse_string(&line, &len, &source->fs_source_path);
588 if (ret != PARSE_STRING_SUCCESS)
590 ret = parse_string(&line, &len, &source->wim_target_path);
591 if (ret == PARSE_STRING_NONE)
592 source->wim_target_path = source->fs_source_path;
593 return ret != PARSE_STRING_FAILURE;
596 /* Returns %true if the given line of length @len > 0 is a comment or empty line
597 * in the source list file format. */
599 is_comment_line(const tchar *line, size_t len)
604 if (!istspace(*line) && *line != T('\0'))
614 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
617 tchar *contents = *contents_p;
618 size_t nchars = *nchars_p;
621 for (i = 0; i < nchars; i++)
622 if (contents[i] == T('\n'))
625 /* Handle last line not terminated by a newline */
626 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
627 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
629 imagex_error(T("Out of memory!"));
632 contents[nchars] = T('\n');
633 *contents_p = contents;
641 /* Parses a file in the source list format. (See the man page for
642 * 'wimlib-imagex capture' for details on this format and the meaning.)
644 * @source_list_contents: Contents of the source list file. Note that this
645 * buffer will be modified to save memory allocations,
646 * and cannot be freed until the returned array of
647 * wimlib_capture_source's has also been freed.
649 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
652 * @nsources_ret: On success, the length of the returned array is
655 * Returns: An array of `struct wimlib_capture_source's that can be passed to
656 * the wimlib_add_image_multisource() function to specify how a WIM image is to
658 static struct wimlib_capture_source *
659 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
660 size_t *nsources_ret)
664 struct wimlib_capture_source *sources;
667 nlines = text_file_count_lines(source_list_contents_p,
668 &source_list_nchars);
672 /* Always allocate at least 1 slot, just in case the implementation of
673 * calloc() returns NULL if 0 bytes are requested. */
674 sources = calloc(nlines ?: 1, sizeof(*sources));
676 imagex_error(T("out of memory"));
679 p = *source_list_contents_p;
681 for (i = 0; i < nlines; i++) {
682 /* XXX: Could use rawmemchr() here instead, but it may not be
683 * available on all platforms. */
684 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
685 size_t len = endp - p + 1;
687 if (!is_comment_line(p, len)) {
688 if (!parse_source_list_line(p, len, &sources[j++])) {
701 enum capture_config_section {
702 CAPTURE_CONFIG_NO_SECTION,
703 CAPTURE_CONFIG_EXCLUSION_SECTION,
704 CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
705 CAPTURE_CONFIG_IGNORE_SECTION,
709 CAPTURE_CONFIG_INVALID_SECTION,
710 CAPTURE_CONFIG_CHANGED_SECTION,
711 CAPTURE_CONFIG_SAME_SECTION,
715 check_config_section(tchar *line, size_t len,
716 enum capture_config_section *cur_section)
718 while (istspace(*line))
722 return CAPTURE_CONFIG_SAME_SECTION;
725 tchar *endbrace = tstrrchr(line, T(']'));
727 return CAPTURE_CONFIG_SAME_SECTION;
729 if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
730 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
731 } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
732 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
733 } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
734 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
735 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
736 "of capture config file\n"),
738 } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
739 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
740 tfputs(T("WARNING: Ignoring [AlignmentList] section "
741 "of capture config file\n"),
744 imagex_error(T("Invalid capture config file section \"%"TS"\""),
746 return CAPTURE_CONFIG_INVALID_SECTION;
748 return CAPTURE_CONFIG_CHANGED_SECTION;
753 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
756 if (pat_list->num_pats == pat_list->num_allocated_pats) {
758 size_t num_allocated_pats = pat_list->num_pats + 8;
760 pats = realloc(pat_list->pats,
761 num_allocated_pats * sizeof(pat_list->pats[0]));
763 imagex_error(T("Out of memory!"));
766 pat_list->pats = pats;
767 pat_list->num_allocated_pats = num_allocated_pats;
769 pat_list->pats[pat_list->num_pats++] = pat;
774 parse_capture_config_line(tchar *line, size_t len,
775 enum capture_config_section *cur_section,
776 struct wimlib_capture_config *config)
781 ret = check_config_section(line, len, cur_section);
782 if (ret == CAPTURE_CONFIG_INVALID_SECTION)
784 if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
787 switch (*cur_section) {
788 case CAPTURE_CONFIG_NO_SECTION:
789 imagex_error(T("Line \"%"TS"\" is not in a section "
790 "(such as [ExclusionList]"), line);
792 case CAPTURE_CONFIG_EXCLUSION_SECTION:
793 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
795 return pattern_list_add_pattern(&config->exclusion_pats,
797 case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
798 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
800 return pattern_list_add_pattern(&config->exclusion_exception_pats,
802 case CAPTURE_CONFIG_IGNORE_SECTION:
809 parse_capture_config(tchar **contents_p, size_t nchars,
810 struct wimlib_capture_config *config)
815 enum capture_config_section cur_section;
817 memset(config, 0, sizeof(*config));
819 nlines = text_file_count_lines(contents_p, &nchars);
823 cur_section = CAPTURE_CONFIG_NO_SECTION;
825 for (i = 0; i < nlines; i++) {
826 tchar *endp = tmemchr(p, T('\n'), nchars);
827 size_t len = endp - p + 1;
829 if (!is_comment_line(p, len))
830 if (!parse_capture_config_line(p, len, &cur_section, config))
838 /* Reads the contents of a file into memory. */
840 file_get_contents(const tchar *filename, size_t *len_ret)
847 if (tstat(filename, &stbuf) != 0) {
848 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
853 fp = tfopen(filename, T("rb"));
855 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
859 buf = malloc(len ? len : 1);
861 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
862 "contents of file \"%"TS"\""), len, filename);
865 if (fread(buf, 1, len, fp) != len) {
866 imagex_error_with_errno(T("Failed to read %zu bytes from the "
867 "file \"%"TS"\""), len, filename);
881 /* Read standard input until EOF and return the full contents in a malloc()ed
882 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
885 stdin_get_contents(size_t *len_ret)
887 /* stdin can, of course, be a pipe or other non-seekable file, so the
888 * total length of the data cannot be pre-determined */
890 size_t newlen = 1024;
894 char *p = realloc(buf, newlen);
895 size_t bytes_read, bytes_to_read;
897 imagex_error(T("out of memory while reading stdin"));
901 bytes_to_read = newlen - pos;
902 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
904 if (bytes_read != bytes_to_read) {
909 imagex_error_with_errno(T("error reading stdin"));
923 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
926 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
928 *num_tchars_ret = num_bytes;
930 #else /* !__WIN32__ */
931 /* On Windows, translate the text to UTF-16LE */
935 if (num_bytes >= 2 &&
936 ((text[0] == 0xff && text[1] == 0xfe) ||
937 (text[0] <= 0x7f && text[1] == 0x00)))
939 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
940 * with something that looks like an ASCII character encoded as
941 * a UTF-16LE code unit. Assume the file is encoded as
942 * UTF-16LE. This is not a 100% reliable check. */
943 num_wchars = num_bytes / 2;
944 text_wstr = (wchar_t*)text;
946 /* File does not look like UTF-16LE. Assume it is encoded in
947 * the current Windows code page. I think these are always
948 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
949 * should work as expected. */
950 text_wstr = win32_mbs_to_wcs(text,
955 *num_tchars_ret = num_wchars;
957 #endif /* __WIN32__ */
961 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
966 contents = file_get_contents(filename, &num_bytes);
969 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
973 stdin_get_text_contents(size_t *num_tchars_ret)
978 contents = stdin_get_contents(&num_bytes);
981 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
984 #define TO_PERCENT(numerator, denominator) \
985 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
987 /* Given an enumerated value for WIM compression type, return a descriptive
990 get_data_type(int ctype)
993 case WIMLIB_COMPRESSION_TYPE_NONE:
994 return T("uncompressed");
995 case WIMLIB_COMPRESSION_TYPE_LZX:
996 return T("LZX-compressed");
997 case WIMLIB_COMPRESSION_TYPE_XPRESS:
998 return T("XPRESS-compressed");
1003 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
1004 #define MEBIBYTE_MIN_NBYTES 10000000ULL
1005 #define KIBIBYTE_MIN_NBYTES 10000ULL
1008 get_unit(uint64_t total_bytes, const tchar **name_ret)
1010 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
1011 *name_ret = T("GiB");
1013 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
1014 *name_ret = T("MiB");
1016 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
1017 *name_ret = T("KiB");
1020 *name_ret = T("bytes");
1025 /* Progress callback function passed to various wimlib functions. */
1027 imagex_progress_func(enum wimlib_progress_msg msg,
1028 const union wimlib_progress_info *info)
1030 unsigned percent_done;
1031 unsigned unit_shift;
1032 const tchar *unit_name;
1033 if (imagex_be_quiet)
1036 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1037 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1038 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1039 info->write_streams.total_bytes);
1041 if (info->write_streams.completed_streams == 0) {
1042 const tchar *data_type;
1044 data_type = get_data_type(info->write_streams.compression_type);
1045 imagex_printf(T("Writing %"TS" data using %u thread%"TS"\n"),
1046 data_type, info->write_streams.num_threads,
1047 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1049 if (info->write_streams.total_parts <= 1) {
1050 imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1051 "written (%u%% done)"),
1052 info->write_streams.completed_bytes >> unit_shift,
1054 info->write_streams.total_bytes >> unit_shift,
1058 imagex_printf(T("\rWriting resources from part %u of %u: "
1059 "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written"),
1060 (info->write_streams.completed_parts ==
1061 info->write_streams.total_parts) ?
1062 info->write_streams.completed_parts :
1063 info->write_streams.completed_parts + 1,
1064 info->write_streams.total_parts,
1065 info->write_streams.completed_bytes >> unit_shift,
1067 info->write_streams.total_bytes >> unit_shift,
1071 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1072 imagex_printf(T("\n"));
1074 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1075 imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
1076 if (*info->scan.wim_target_path) {
1077 imagex_printf(T(" (loading as WIM path: "
1078 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
1079 info->scan.wim_target_path);
1081 imagex_printf(T(" (loading as root of WIM image)...\n"));
1084 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1085 switch (info->scan.status) {
1086 case WIMLIB_SCAN_DENTRY_OK:
1087 imagex_printf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
1089 case WIMLIB_SCAN_DENTRY_EXCLUDED:
1090 imagex_printf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1092 case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
1093 imagex_printf(T("WARNING: Excluding unsupported file or directory\n"
1094 " \"%"TS"\" from capture\n"), info->scan.cur_path);
1098 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1099 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1100 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1101 info->integrity.total_bytes);
1102 imagex_printf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1103 "of %"PRIu64" %"TS" (%u%%) done"),
1104 info->integrity.filename,
1105 info->integrity.completed_bytes >> unit_shift,
1107 info->integrity.total_bytes >> unit_shift,
1110 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1111 imagex_printf(T("\n"));
1113 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1114 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1115 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1116 info->integrity.total_bytes);
1117 imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1118 "of %"PRIu64" %"TS" (%u%%) done"),
1119 info->integrity.completed_bytes >> unit_shift,
1121 info->integrity.total_bytes >> unit_shift,
1124 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1125 imagex_printf(T("\n"));
1127 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1128 imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1129 "to %"TS" \"%"TS"\"\n"),
1130 info->extract.image,
1131 info->extract.image_name,
1132 info->extract.wimfile_name,
1133 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1134 T("NTFS volume") : T("directory")),
1135 info->extract.target);
1137 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1138 imagex_printf(T("Extracting "
1139 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\" from image %d (\"%"TS"\") "
1140 "in \"%"TS"\" to \"%"TS"\"\n"),
1141 info->extract.extract_root_wim_source_path,
1142 info->extract.image,
1143 info->extract.image_name,
1144 info->extract.wimfile_name,
1145 info->extract.target);
1147 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1148 percent_done = TO_PERCENT(info->extract.completed_bytes,
1149 info->extract.total_bytes);
1150 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1151 imagex_printf(T("\rExtracting files: "
1152 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1153 info->extract.completed_bytes >> unit_shift,
1155 info->extract.total_bytes >> unit_shift,
1158 if (info->extract.completed_bytes >= info->extract.total_bytes)
1159 imagex_printf(T("\n"));
1161 case WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN:
1162 if (info->extract.total_parts != 1) {
1163 imagex_printf(T("\nReading split pipable WIM part %u of %u\n"),
1164 info->extract.part_number,
1165 info->extract.total_parts);
1168 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1169 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1170 imagex_printf(T("Setting timestamps on all extracted files...\n"));
1172 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1173 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1174 imagex_printf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1175 info->extract.target);
1178 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1179 percent_done = TO_PERCENT(info->split.completed_bytes,
1180 info->split.total_bytes);
1181 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1182 imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
1183 "%"PRIu64" %"TS" (%u%%) written\n"),
1184 info->split.part_name,
1185 info->split.cur_part_number,
1186 info->split.total_parts,
1187 info->split.completed_bytes >> unit_shift,
1189 info->split.total_bytes >> unit_shift,
1193 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1194 if (info->split.completed_bytes == info->split.total_bytes) {
1195 imagex_printf(T("Finished writing split WIM part %u of %u\n"),
1196 info->split.cur_part_number,
1197 info->split.total_parts);
1200 case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1201 switch (info->update.command->op) {
1202 case WIMLIB_UPDATE_OP_DELETE:
1203 imagex_printf(T("Deleted WIM path "
1204 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1205 info->update.command->delete.wim_path);
1207 case WIMLIB_UPDATE_OP_RENAME:
1208 imagex_printf(T("Renamed WIM path "
1209 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
1210 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1211 info->update.command->rename.wim_source_path,
1212 info->update.command->rename.wim_target_path);
1214 case WIMLIB_UPDATE_OP_ADD:
1222 fflush(imagex_info_file);
1227 parse_num_threads(const tchar *optarg)
1230 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1231 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1232 imagex_error(T("Number of threads must be a non-negative integer!"));
1240 * Parse an option passed to an update command.
1242 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1245 * @option: Text string for the option (beginning with --)
1247 * @cmd: `struct wimlib_update_command' that is being constructed for
1250 * Returns true if the option was recognized; false if not.
1253 update_command_add_option(int op, const tchar *option,
1254 struct wimlib_update_command *cmd)
1256 bool recognized = true;
1258 case WIMLIB_UPDATE_OP_ADD:
1259 if (!tstrcmp(option, T("--verbose")))
1260 cmd->add.add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
1261 else if (!tstrcmp(option, T("--unix-data")))
1262 cmd->add.add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
1263 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1264 cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
1265 else if (!tstrcmp(option, T("--strict-acls")))
1266 cmd->add.add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
1267 else if (!tstrcmp(option, T("--dereference")))
1268 cmd->add.add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
1272 case WIMLIB_UPDATE_OP_DELETE:
1273 if (!tstrcmp(option, T("--force")))
1274 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1275 else if (!tstrcmp(option, T("--recursive")))
1276 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1287 /* How many nonoption arguments each `imagex update' command expects */
1288 static const unsigned update_command_num_nonoptions[] = {
1289 [WIMLIB_UPDATE_OP_ADD] = 2,
1290 [WIMLIB_UPDATE_OP_DELETE] = 1,
1291 [WIMLIB_UPDATE_OP_RENAME] = 2,
1295 update_command_add_nonoption(int op, const tchar *nonoption,
1296 struct wimlib_update_command *cmd,
1297 unsigned num_nonoptions)
1300 case WIMLIB_UPDATE_OP_ADD:
1301 if (num_nonoptions == 0)
1302 cmd->add.fs_source_path = (tchar*)nonoption;
1304 cmd->add.wim_target_path = (tchar*)nonoption;
1306 case WIMLIB_UPDATE_OP_DELETE:
1307 cmd->delete.wim_path = (tchar*)nonoption;
1309 case WIMLIB_UPDATE_OP_RENAME:
1310 if (num_nonoptions == 0)
1311 cmd->rename.wim_source_path = (tchar*)nonoption;
1313 cmd->rename.wim_target_path = (tchar*)nonoption;
1319 * Parse a command passed on stdin to `imagex update'.
1321 * @line: Text of the command.
1322 * @len: Length of the line, including a null terminator
1325 * @command: A `struct wimlib_update_command' to fill in from the parsed
1328 * @line_number: Line number of the command, for diagnostics.
1330 * Returns true on success; returns false on parse error.
1333 parse_update_command(tchar *line, size_t len,
1334 struct wimlib_update_command *command,
1338 tchar *command_name;
1340 size_t num_nonoptions;
1342 /* Get the command name ("add", "delete", "rename") */
1343 ret = parse_string(&line, &len, &command_name);
1344 if (ret != PARSE_STRING_SUCCESS)
1347 if (!tstrcasecmp(command_name, T("add"))) {
1348 op = WIMLIB_UPDATE_OP_ADD;
1349 } else if (!tstrcasecmp(command_name, T("delete"))) {
1350 op = WIMLIB_UPDATE_OP_DELETE;
1351 } else if (!tstrcasecmp(command_name, T("rename"))) {
1352 op = WIMLIB_UPDATE_OP_RENAME;
1354 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1355 command_name, line_number);
1360 /* Parse additional options and non-options as needed */
1365 ret = parse_string(&line, &len, &next_string);
1366 if (ret == PARSE_STRING_NONE) /* End of line */
1368 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1370 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1372 if (!update_command_add_option(op, next_string, command))
1374 imagex_error(T("Unrecognized option \"%"TS"\" to "
1375 "update command \"%"TS"\" on line %zu"),
1376 next_string, command_name, line_number);
1382 if (num_nonoptions == update_command_num_nonoptions[op])
1384 imagex_error(T("Unexpected argument \"%"TS"\" in "
1385 "update command on line %zu\n"
1386 " (The \"%"TS"\" command only "
1387 "takes %zu nonoption arguments!)\n"),
1388 next_string, line_number,
1389 command_name, num_nonoptions);
1392 update_command_add_nonoption(op, next_string,
1393 command, num_nonoptions);
1398 if (num_nonoptions != update_command_num_nonoptions[op]) {
1399 imagex_error(T("Not enough arguments to update command "
1400 "\"%"TS"\" on line %zu"), command_name, line_number);
1406 static struct wimlib_update_command *
1407 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1408 size_t *num_cmds_ret)
1412 struct wimlib_update_command *cmds;
1415 nlines = text_file_count_lines(cmd_file_contents_p,
1420 /* Always allocate at least 1 slot, just in case the implementation of
1421 * calloc() returns NULL if 0 bytes are requested. */
1422 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1424 imagex_error(T("out of memory"));
1427 p = *cmd_file_contents_p;
1429 for (i = 0; i < nlines; i++) {
1430 /* XXX: Could use rawmemchr() here instead, but it may not be
1431 * available on all platforms. */
1432 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1433 size_t len = endp - p + 1;
1435 if (!is_comment_line(p, len)) {
1436 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1447 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1448 * one image from a WIM file to a NTFS volume. */
1450 imagex_apply(int argc, tchar **argv, int cmd)
1453 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1456 struct wimlib_wim_info info;
1458 const tchar *wimfile;
1459 const tchar *target;
1460 const tchar *image_num_or_name;
1461 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1463 REFGLOB_SET(refglobs);
1465 for_opt(c, apply_options) {
1467 case IMAGEX_CHECK_OPTION:
1468 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1470 case IMAGEX_HARDLINK_OPTION:
1471 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1473 case IMAGEX_SYMLINK_OPTION:
1474 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1476 case IMAGEX_VERBOSE_OPTION:
1477 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
1479 case IMAGEX_REF_OPTION:
1480 ret = refglob_set_append(&refglobs, optarg);
1482 goto out_free_refglobs;
1484 case IMAGEX_UNIX_DATA_OPTION:
1485 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1487 case IMAGEX_NO_ACLS_OPTION:
1488 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1490 case IMAGEX_STRICT_ACLS_OPTION:
1491 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1493 case IMAGEX_NORPFIX_OPTION:
1494 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1496 case IMAGEX_RPFIX_OPTION:
1497 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1499 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1500 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1501 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1503 case IMAGEX_RESUME_OPTION:
1504 extract_flags |= WIMLIB_EXTRACT_FLAG_RESUME;
1512 if (argc != 2 && argc != 3)
1517 if (!tstrcmp(wimfile, T("-"))) {
1518 /* Attempt to apply pipable WIM from standard input. */
1520 image_num_or_name = NULL;
1523 image_num_or_name = argv[1];
1528 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1529 imagex_progress_func);
1531 goto out_free_refglobs;
1533 wimlib_get_wim_info(wim, &info);
1536 /* Image explicitly specified. */
1537 image_num_or_name = argv[1];
1538 image = wimlib_resolve_image(wim, image_num_or_name);
1539 ret = verify_image_exists(image, image_num_or_name, wimfile);
1541 goto out_wimlib_free;
1544 /* No image specified; default to image 1, but only if the WIM
1545 * contains exactly one image. */
1547 if (info.image_count != 1) {
1548 imagex_error(T("\"%"TS"\" contains %d images; "
1549 "Please select one (or all)."),
1550 wimfile, info.image_count);
1559 if (refglobs.num_globs) {
1561 imagex_error(T("Can't specify --ref when applying from stdin!"));
1563 goto out_wimlib_free;
1565 ret = wim_reference_globs(wim, &refglobs, open_flags);
1567 goto out_wimlib_free;
1572 /* Interpret a regular file or block device target as a NTFS
1576 if (tstat(target, &stbuf)) {
1577 if (errno != ENOENT) {
1578 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1581 goto out_wimlib_free;
1584 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1585 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1591 ret = wimlib_extract_image(wim, image, target, extract_flags,
1592 imagex_progress_func);
1594 set_fd_to_binary_mode(STDIN_FILENO);
1595 ret = wimlib_extract_image_from_pipe(STDIN_FILENO,
1597 target, extract_flags,
1598 imagex_progress_func);
1601 imagex_printf(T("Done applying WIM image.\n"));
1602 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
1604 do_resource_not_found_warning(wimfile, &info, &refglobs);
1606 imagex_error(T( "If you are applying an image "
1607 "from a split pipable WIM,\n"
1608 " make sure you have "
1609 "concatenated together all parts."));
1615 refglob_set_destroy(&refglobs);
1619 usage(CMD_APPLY, stderr);
1621 goto out_free_refglobs;
1624 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1625 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1626 * the desired image. 'wimlib-imagex append': add a new image to an existing
1629 imagex_capture_or_append(int argc, tchar **argv, int cmd)
1632 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1633 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE |
1634 WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1635 int write_flags = 0;
1636 int compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1637 const tchar *wimfile;
1641 const tchar *flags_element = NULL;
1642 const tchar *template_image_name_or_num = NULL;
1646 unsigned num_threads = 0;
1651 const tchar *config_file = NULL;
1653 struct wimlib_capture_config *config;
1655 bool source_list = false;
1656 size_t source_list_nchars;
1657 tchar *source_list_contents;
1658 bool capture_sources_malloced;
1659 struct wimlib_capture_source *capture_sources;
1661 bool name_defaulted;
1663 for_opt(c, capture_or_append_options) {
1665 case IMAGEX_BOOT_OPTION:
1666 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1668 case IMAGEX_CHECK_OPTION:
1669 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1670 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1672 case IMAGEX_NOCHECK_OPTION:
1673 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
1675 case IMAGEX_CONFIG_OPTION:
1676 config_file = optarg;
1677 add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1679 case IMAGEX_COMPRESS_OPTION:
1680 compression_type = get_compression_type(optarg);
1681 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1684 case IMAGEX_FLAGS_OPTION:
1685 flags_element = optarg;
1687 case IMAGEX_DEREFERENCE_OPTION:
1688 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1690 case IMAGEX_VERBOSE_OPTION:
1691 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1693 case IMAGEX_THREADS_OPTION:
1694 num_threads = parse_num_threads(optarg);
1695 if (num_threads == UINT_MAX)
1698 case IMAGEX_REBUILD_OPTION:
1699 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1701 case IMAGEX_UNIX_DATA_OPTION:
1702 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1704 case IMAGEX_SOURCE_LIST_OPTION:
1707 case IMAGEX_NO_ACLS_OPTION:
1708 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1710 case IMAGEX_STRICT_ACLS_OPTION:
1711 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1713 case IMAGEX_RPFIX_OPTION:
1714 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1716 case IMAGEX_NORPFIX_OPTION:
1717 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1719 case IMAGEX_PIPABLE_OPTION:
1720 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1722 case IMAGEX_NOT_PIPABLE_OPTION:
1723 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
1725 case IMAGEX_DELTA_FROM_OPTION:
1726 if (cmd == CMD_CAPTURE) {
1727 imagex_error(T("--delta-from=IMAGE is only "
1728 "valid for append."));
1731 template_image_name_or_num = optarg;
1740 if (argc < 2 || argc > 4)
1746 if (!tstrcmp(wimfile, T("-"))) {
1747 /* Writing captured WIM to standard output. */
1749 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
1750 imagex_error("Can't write a non-pipable WIM to "
1751 "standard output! Specify --pipable\n"
1752 " if you want to create a pipable WIM "
1753 "(but read the docs first).");
1757 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1759 if (cmd == CMD_APPEND) {
1760 imagex_error(T("Using standard output for append does "
1761 "not make sense."));
1764 wim_fd = STDOUT_FILENO;
1766 imagex_info_file = stderr;
1767 set_fd_to_binary_mode(wim_fd);
1772 name_defaulted = false;
1774 /* Set default name to SOURCE argument, omitting any directory
1775 * prefixes and trailing slashes. This requires making a copy
1776 * of @source. Leave some free characters at the end in case we
1777 * append a number to keep the name unique. */
1778 size_t source_name_len;
1780 source_name_len = tstrlen(source);
1781 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1782 name = tbasename(tstrcpy(source_copy, source));
1783 name_defaulted = true;
1785 /* Image description defaults to NULL if not given. */
1792 /* Set up capture sources in source list mode */
1793 if (source[0] == T('-') && source[1] == T('\0')) {
1794 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1796 source_list_contents = file_get_text_contents(source,
1797 &source_list_nchars);
1799 if (!source_list_contents)
1802 capture_sources = parse_source_list(&source_list_contents,
1805 if (!capture_sources) {
1807 goto out_free_source_list_contents;
1809 capture_sources_malloced = true;
1811 /* Set up capture source in non-source-list mode (could be
1812 * either "normal" mode or "NTFS mode"--- see the man page). */
1813 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1814 capture_sources[0].fs_source_path = source;
1815 capture_sources[0].wim_target_path = NULL;
1816 capture_sources[0].reserved = 0;
1818 capture_sources_malloced = false;
1819 source_list_contents = NULL;
1825 config_str = file_get_text_contents(config_file, &config_len);
1828 goto out_free_capture_sources;
1831 config = alloca(sizeof(*config));
1832 ret = parse_capture_config(&config_str, config_len, config);
1834 goto out_free_config;
1839 if (cmd == CMD_APPEND)
1840 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1841 imagex_progress_func);
1843 ret = wimlib_create_new_wim(compression_type, &wim);
1845 goto out_free_config;
1851 if (tstat(source, &stbuf) == 0) {
1852 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1853 imagex_printf(T("Capturing WIM image from NTFS "
1854 "filesystem on \"%"TS"\"\n"), source);
1855 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1858 if (errno != ENOENT) {
1859 imagex_error_with_errno(T("Failed to stat "
1860 "\"%"TS"\""), source);
1862 goto out_wimlib_free;
1868 if (cmd == CMD_APPEND && name_defaulted) {
1869 /* If the user did not specify an image name, and the basename
1870 * of the source already exists as an image name in the WIM
1871 * file, append a suffix to make it unique. */
1872 unsigned long conflict_idx;
1873 tchar *name_end = tstrchr(name, T('\0'));
1874 for (conflict_idx = 1;
1875 wimlib_image_name_in_use(wim, name);
1878 tsprintf(name_end, T(" (%lu)"), conflict_idx);
1882 if (template_image_name_or_num) {
1883 template_image = wimlib_resolve_image(wim, template_image_name_or_num);
1884 if (template_image_name_or_num[0] == T('-')) {
1887 struct wimlib_wim_info info;
1889 wimlib_get_wim_info(wim, &info);
1890 n = tstrtoul(template_image_name_or_num + 1, &tmp, 10);
1891 if (n >= 1 && n <= info.image_count &&
1893 tmp != template_image_name_or_num + 1)
1895 template_image = info.image_count - (n - 1);
1898 ret = verify_image_exists_and_is_single(template_image,
1899 template_image_name_or_num,
1902 goto out_wimlib_free;
1905 ret = wimlib_add_image_multisource(wim,
1911 imagex_progress_func);
1913 goto out_wimlib_free;
1915 if (desc || flags_element || template_image_name_or_num) {
1916 /* User provided <DESCRIPTION> or <FLAGS> element, or an image
1917 * on which the added one is to be based has been specified with
1918 * --delta-from=IMAGE. Get the index of the image we just
1919 * added, then use it to call the appropriate functions. */
1920 struct wimlib_wim_info info;
1922 wimlib_get_wim_info(wim, &info);
1925 ret = wimlib_set_image_descripton(wim,
1929 goto out_wimlib_free;
1932 if (flags_element) {
1933 ret = wimlib_set_image_flags(wim, info.image_count,
1936 goto out_wimlib_free;
1939 if (template_image_name_or_num) {
1940 ret = wimlib_reference_template_image(wim,
1945 goto out_wimlib_free;
1949 /* Write the new WIM or overwrite the existing WIM with the new image
1951 if (cmd == CMD_APPEND) {
1952 ret = wimlib_overwrite(wim, write_flags, num_threads,
1953 imagex_progress_func);
1954 } else if (wimfile) {
1955 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
1956 write_flags, num_threads,
1957 imagex_progress_func);
1959 ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
1960 write_flags, num_threads,
1961 imagex_progress_func);
1967 free(config->exclusion_pats.pats);
1968 free(config->exclusion_exception_pats.pats);
1971 out_free_capture_sources:
1972 if (capture_sources_malloced)
1973 free(capture_sources);
1974 out_free_source_list_contents:
1975 free(source_list_contents);
1986 /* Remove image(s) from a WIM. */
1988 imagex_delete(int argc, tchar **argv, int cmd)
1991 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1992 int write_flags = 0;
1993 const tchar *wimfile;
1994 const tchar *image_num_or_name;
1999 for_opt(c, delete_options) {
2001 case IMAGEX_CHECK_OPTION:
2002 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2003 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2005 case IMAGEX_SOFT_OPTION:
2006 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
2017 imagex_error(T("Must specify a WIM file"));
2019 imagex_error(T("Must specify an image"));
2023 image_num_or_name = argv[1];
2025 ret = wimlib_open_wim(wimfile, open_flags, &wim,
2026 imagex_progress_func);
2030 image = wimlib_resolve_image(wim, image_num_or_name);
2032 ret = verify_image_exists(image, image_num_or_name, wimfile);
2034 goto out_wimlib_free;
2036 ret = wimlib_delete_image(wim, image);
2038 imagex_error(T("Failed to delete image from \"%"TS"\""),
2040 goto out_wimlib_free;
2043 ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
2045 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2046 "deleted"), wimfile);
2054 usage(CMD_DELETE, stderr);
2060 print_full_path(const struct wimlib_dir_entry *wdentry, void *_ignore)
2062 int ret = tprintf(T("%"TS"\n"), wdentry->full_path);
2063 return (ret >= 0) ? 0 : -1;
2066 /* Print the files contained in an image(s) in a WIM file. */
2068 imagex_dir(int argc, tchar **argv, int cmd)
2070 const tchar *wimfile;
2071 WIMStruct *wim = NULL;
2074 const tchar *path = T("");
2077 for_opt(c, dir_options) {
2079 case IMAGEX_PATH_OPTION:
2090 imagex_error(T("Must specify a WIM file"));
2094 imagex_error(T("Too many arguments"));
2099 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &wim,
2100 imagex_progress_func);
2105 image = wimlib_resolve_image(wim, argv[1]);
2106 ret = verify_image_exists(image, argv[1], wimfile);
2108 goto out_wimlib_free;
2110 /* No image specified; default to image 1, but only if the WIM
2111 * contains exactly one image. */
2113 struct wimlib_wim_info info;
2115 wimlib_get_wim_info(wim, &info);
2116 if (info.image_count != 1) {
2117 imagex_error(T("\"%"TS"\" contains %d images; Please "
2118 "select one (or all)."),
2119 wimfile, info.image_count);
2126 ret = wimlib_iterate_dir_tree(wim, image, path,
2127 WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE,
2128 print_full_path, NULL);
2135 usage(CMD_DIR, stderr);
2140 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2143 imagex_export(int argc, tchar **argv, int cmd)
2147 int export_flags = 0;
2148 int write_flags = 0;
2149 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2150 const tchar *src_wimfile;
2151 const tchar *src_image_num_or_name;
2152 const tchar *dest_wimfile;
2154 const tchar *dest_name;
2155 const tchar *dest_desc;
2157 struct wimlib_wim_info src_info;
2158 WIMStruct *dest_wim;
2163 REFGLOB_SET(refglobs);
2164 unsigned num_threads = 0;
2166 for_opt(c, export_options) {
2168 case IMAGEX_BOOT_OPTION:
2169 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2171 case IMAGEX_CHECK_OPTION:
2172 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2173 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2175 case IMAGEX_NOCHECK_OPTION:
2176 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2178 case IMAGEX_COMPRESS_OPTION:
2179 compression_type = get_compression_type(optarg);
2180 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2183 case IMAGEX_REF_OPTION:
2184 ret = refglob_set_append(&refglobs, optarg);
2186 goto out_free_refglobs;
2188 case IMAGEX_THREADS_OPTION:
2189 num_threads = parse_num_threads(optarg);
2190 if (num_threads == UINT_MAX)
2193 case IMAGEX_REBUILD_OPTION:
2194 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2196 case IMAGEX_PIPABLE_OPTION:
2197 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2199 case IMAGEX_NOT_PIPABLE_OPTION:
2200 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
2208 if (argc < 3 || argc > 5)
2210 src_wimfile = argv[0];
2211 src_image_num_or_name = argv[1];
2212 dest_wimfile = argv[2];
2213 dest_name = (argc >= 4) ? argv[3] : NULL;
2214 dest_desc = (argc >= 5) ? argv[4] : NULL;
2215 ret = wimlib_open_wim(src_wimfile,
2216 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_wim,
2217 imagex_progress_func);
2219 goto out_free_refglobs;
2221 wimlib_get_wim_info(src_wim, &src_info);
2223 /* Determine if the destination is an existing file or not. If so, we
2224 * try to append the exported image(s) to it; otherwise, we create a new
2225 * WIM containing the exported image(s). Furthermore, determine if we
2226 * need to write a pipable WIM directly to standard output. */
2228 if (tstrcmp(dest_wimfile, T("-")) == 0) {
2230 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2231 imagex_error("Can't write a non-pipable WIM to "
2232 "standard output! Specify --pipable\n"
2233 " if you want to create a pipable WIM "
2234 "(but read the docs first).");
2236 goto out_free_src_wim;
2239 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2241 dest_wimfile = NULL;
2242 dest_wim_fd = STDOUT_FILENO;
2243 imagex_info_file = stderr;
2244 set_fd_to_binary_mode(dest_wim_fd);
2247 if (dest_wimfile != NULL && tstat(dest_wimfile, &stbuf) == 0) {
2249 /* Destination file exists. */
2251 if (!S_ISREG(stbuf.st_mode)) {
2252 imagex_error(T("\"%"TS"\" is not a regular file"),
2255 goto out_free_src_wim;
2257 ret = wimlib_open_wim(dest_wimfile, open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2258 &dest_wim, imagex_progress_func);
2260 goto out_free_src_wim;
2262 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2263 /* The user specified a compression type, but we're
2264 * exporting to an existing WIM. Make sure the
2265 * specified compression type is the same as the
2266 * compression type of the existing destination WIM. */
2267 struct wimlib_wim_info dest_info;
2269 wimlib_get_wim_info(dest_wim, &dest_info);
2270 if (compression_type != dest_info.compression_type) {
2271 imagex_error(T("Cannot specify a compression type that is "
2272 "not the same as that used in the "
2273 "destination WIM"));
2275 goto out_free_dest_wim;
2281 if (errno != ENOENT) {
2282 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2285 goto out_free_src_wim;
2288 /* dest_wimfile is not an existing file, so create a new WIM. */
2290 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2291 /* The user did not specify a compression type; default
2292 * to that of the source WIM. */
2294 compression_type = src_info.compression_type;
2296 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2298 goto out_free_src_wim;
2301 image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2302 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2304 goto out_free_dest_wim;
2306 if (refglobs.num_globs) {
2307 ret = wim_reference_globs(src_wim, &refglobs, open_flags);
2309 goto out_free_dest_wim;
2312 if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
2313 image == WIMLIB_ALL_IMAGES && src_info.boot_index == 0)
2315 imagex_error(T("--boot specified for all-images export, but source WIM "
2316 "has no bootable image."));
2318 goto out_free_dest_wim;
2321 ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2322 dest_desc, export_flags, imagex_progress_func);
2324 if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2325 do_resource_not_found_warning(src_wimfile,
2326 &src_info, &refglobs);
2328 goto out_free_dest_wim;
2332 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2333 imagex_progress_func);
2334 else if (dest_wimfile)
2335 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2336 write_flags, num_threads,
2337 imagex_progress_func);
2339 ret = wimlib_write_to_fd(dest_wim, dest_wim_fd,
2340 WIMLIB_ALL_IMAGES, write_flags,
2341 num_threads, imagex_progress_func);
2343 wimlib_free(dest_wim);
2345 wimlib_free(src_wim);
2347 refglob_set_destroy(&refglobs);
2351 usage(CMD_EXPORT, stderr);
2354 goto out_free_refglobs;
2358 is_root_wim_path(const tchar *path)
2361 for (p = path; *p; p++)
2362 if (*p != T('\\') && *p != T('/'))
2368 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2369 const tchar *dest_dir)
2371 for (size_t i = 0; i < num_cmds; i++)
2372 if (cmds[i].fs_dest_path != dest_dir)
2373 free(cmds[i].fs_dest_path);
2377 static struct wimlib_extract_command *
2378 prepare_extract_commands(tchar **paths, unsigned num_paths,
2379 int extract_flags, tchar *dest_dir,
2380 size_t *num_cmds_ret)
2382 struct wimlib_extract_command *cmds;
2384 tchar *emptystr = T("");
2386 if (num_paths == 0) {
2390 num_cmds = num_paths;
2391 cmds = calloc(num_cmds, sizeof(cmds[0]));
2393 imagex_error(T("Out of memory!"));
2397 for (size_t i = 0; i < num_cmds; i++) {
2398 cmds[i].extract_flags = extract_flags;
2399 cmds[i].wim_source_path = paths[i];
2400 if (is_root_wim_path(paths[i])) {
2401 cmds[i].fs_dest_path = dest_dir;
2403 size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2404 cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2405 if (!cmds[i].fs_dest_path) {
2406 free_extract_commands(cmds, num_cmds, dest_dir);
2409 tsprintf(cmds[i].fs_dest_path,
2410 T("%"TS""OS_PREFERRED_PATH_SEPARATOR_STRING"%"TS),
2411 dest_dir, tbasename(paths[i]));
2414 *num_cmds_ret = num_cmds;
2418 /* Extract files or directories from a WIM image */
2420 imagex_extract(int argc, tchar **argv, int cmd)
2423 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2427 const tchar *wimfile;
2428 const tchar *image_num_or_name;
2429 tchar *dest_dir = T(".");
2430 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL | WIMLIB_EXTRACT_FLAG_NORPFIX;
2432 REFGLOB_SET(refglobs);
2434 struct wimlib_extract_command *cmds;
2437 for_opt(c, extract_options) {
2439 case IMAGEX_CHECK_OPTION:
2440 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2442 case IMAGEX_VERBOSE_OPTION:
2443 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
2445 case IMAGEX_REF_OPTION:
2446 ret = refglob_set_append(&refglobs, optarg);
2448 goto out_free_refglobs;
2450 case IMAGEX_UNIX_DATA_OPTION:
2451 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2453 case IMAGEX_NO_ACLS_OPTION:
2454 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2456 case IMAGEX_STRICT_ACLS_OPTION:
2457 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2459 case IMAGEX_DEST_DIR_OPTION:
2462 case IMAGEX_TO_STDOUT_OPTION:
2463 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2464 imagex_info_file = stderr;
2465 imagex_be_quiet = true;
2467 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2468 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2469 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2482 image_num_or_name = argv[1];
2487 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2492 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2496 image = wimlib_resolve_image(wim, image_num_or_name);
2497 ret = verify_image_exists_and_is_single(image,
2501 goto out_wimlib_free;
2503 if (refglobs.num_globs) {
2504 ret = wim_reference_globs(wim, &refglobs, open_flags);
2506 goto out_wimlib_free;
2509 ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
2510 imagex_progress_func);
2512 if (!imagex_be_quiet)
2513 imagex_printf(T("Done extracting files.\n"));
2514 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2515 tfprintf(stderr, T("Note: You can use `%"TS"' to see what "
2516 "files and directories\n"
2517 " are in the WIM image.\n"),
2518 get_cmd_string(CMD_INFO, false));
2519 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2520 struct wimlib_wim_info info;
2522 wimlib_get_wim_info(wim, &info);
2523 do_resource_not_found_warning(wimfile, &info, &refglobs);
2528 free_extract_commands(cmds, num_cmds, dest_dir);
2530 refglob_set_destroy(&refglobs);
2534 usage(CMD_EXTRACT, stderr);
2537 goto out_free_refglobs;
2540 static void print_byte_field(const uint8_t field[], size_t len)
2543 tprintf(T("%02hhx"), *field++);
2547 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2549 tputs(T("WIM Information:"));
2550 tputs(T("----------------"));
2551 tprintf(T("Path: %"TS"\n"), wimfile);
2552 tprintf(T("GUID: 0x"));
2553 print_byte_field(info->guid, sizeof(info->guid));
2555 tprintf(T("Image Count: %d\n"), info->image_count);
2556 tprintf(T("Compression: %"TS"\n"),
2557 wimlib_get_compression_type_string(info->compression_type));
2558 tprintf(T("Part Number: %d/%d\n"), info->part_number, info->total_parts);
2559 tprintf(T("Boot Index: %d\n"), info->boot_index);
2560 tprintf(T("Size: %"PRIu64" bytes\n"), info->total_bytes);
2561 tprintf(T("Integrity Info: %"TS"\n"),
2562 info->has_integrity_table ? T("yes") : T("no"));
2563 tprintf(T("Relative path junction: %"TS"\n"),
2564 info->has_rpfix ? T("yes") : T("no"));
2565 tprintf(T("Pipable: %"TS"\n"),
2566 info->pipable ? T("yes") : T("no"));
2571 print_resource(const struct wimlib_resource_entry *resource,
2575 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
2576 resource->uncompressed_size);
2578 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
2579 resource->compressed_size);
2581 tprintf(T("Offset = %"PRIu64" bytes\n"),
2585 tprintf(T("Part Number = %u\n"), resource->part_number);
2586 tprintf(T("Reference Count = %u\n"), resource->reference_count);
2588 tprintf(T("Hash = 0x"));
2589 print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2592 tprintf(T("Flags = "));
2593 if (resource->is_compressed)
2594 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED "));
2595 if (resource->is_metadata)
2596 tprintf(T("WIM_RESHDR_FLAG_METADATA "));
2597 if (resource->is_free)
2598 tprintf(T("WIM_RESHDR_FLAG_FREE "));
2599 if (resource->is_spanned)
2600 tprintf(T("WIM_RESHDR_FLAG_SPANNED "));
2607 print_lookup_table(WIMStruct *wim)
2609 wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2612 /* Prints information about a WIM file; also can mark an image as bootable,
2613 * change the name of an image, or change the description of an image. */
2615 imagex_info(int argc, tchar **argv, int cmd)
2620 bool nocheck = false;
2621 bool header = false;
2622 bool lookup_table = false;
2624 bool metadata = false;
2625 bool short_header = true;
2626 const tchar *xml_out_file = NULL;
2627 const tchar *wimfile;
2628 const tchar *image_num_or_name;
2629 const tchar *new_name;
2630 const tchar *new_desc;
2634 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2635 struct wimlib_wim_info info;
2637 for_opt(c, info_options) {
2639 case IMAGEX_BOOT_OPTION:
2642 case IMAGEX_CHECK_OPTION:
2645 case IMAGEX_NOCHECK_OPTION:
2648 case IMAGEX_HEADER_OPTION:
2650 short_header = false;
2652 case IMAGEX_LOOKUP_TABLE_OPTION:
2653 lookup_table = true;
2654 short_header = false;
2656 case IMAGEX_XML_OPTION:
2658 short_header = false;
2660 case IMAGEX_EXTRACT_XML_OPTION:
2661 xml_out_file = optarg;
2662 short_header = false;
2664 case IMAGEX_METADATA_OPTION:
2666 short_header = false;
2675 if (argc < 1 || argc > 4)
2679 image_num_or_name = (argc >= 2) ? argv[1] : T("all");
2680 new_name = (argc >= 3) ? argv[2] : NULL;
2681 new_desc = (argc >= 4) ? argv[3] : NULL;
2683 if (check && nocheck) {
2684 imagex_error(T("Can't specify both --check and --nocheck"));
2689 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2691 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2695 wimlib_get_wim_info(wim, &info);
2697 image = wimlib_resolve_image(wim, image_num_or_name);
2698 ret = WIMLIB_ERR_INVALID_IMAGE;
2699 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
2700 imagex_error(T("The image \"%"TS"\" does not exist in \"%"TS"\""),
2701 image_num_or_name, wimfile);
2703 imagex_error(T("If you would like to set the boot "
2704 "index to 0, specify image \"0\" with "
2705 "the --boot flag."));
2707 goto out_wimlib_free;
2710 if (boot && info.image_count == 0) {
2711 imagex_error(T("--boot is meaningless on a WIM with no images"));
2712 goto out_wimlib_free;
2715 if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
2717 imagex_error(T("Cannot specify the --boot flag "
2718 "without specifying a specific "
2719 "image in a multi-image WIM"));
2720 goto out_wimlib_free;
2723 imagex_error(T("Cannot specify the NEW_NAME "
2724 "without specifying a specific "
2725 "image in a multi-image WIM"));
2726 goto out_wimlib_free;
2730 /* Operations that print information are separated from operations that
2731 * recreate the WIM file. */
2732 if (!new_name && !boot) {
2734 /* Read-only operations */
2736 if (image == WIMLIB_NO_IMAGE) {
2737 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
2738 image_num_or_name, wimfile);
2739 goto out_wimlib_free;
2742 if (image == WIMLIB_ALL_IMAGES && short_header)
2743 print_wim_information(wimfile, &info);
2746 wimlib_print_header(wim);
2749 if (info.total_parts != 1) {
2750 tfprintf(stderr, T("Warning: Only showing the lookup table "
2751 "for part %d of a %d-part WIM.\n"),
2752 info.part_number, info.total_parts);
2754 print_lookup_table(wim);
2758 ret = wimlib_extract_xml_data(wim, stdout);
2760 goto out_wimlib_free;
2766 fp = tfopen(xml_out_file, T("wb"));
2768 imagex_error_with_errno(T("Failed to open the "
2769 "file \"%"TS"\" for "
2773 goto out_wimlib_free;
2775 ret = wimlib_extract_xml_data(wim, fp);
2777 imagex_error(T("Failed to close the file "
2783 goto out_wimlib_free;
2787 wimlib_print_available_images(wim, image);
2790 ret = wimlib_print_metadata(wim, image);
2792 goto out_wimlib_free;
2797 /* Modification operations */
2799 if (image == WIMLIB_ALL_IMAGES)
2802 if (image == WIMLIB_NO_IMAGE && new_name) {
2803 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
2804 "when using image 0"), new_name);
2806 goto out_wimlib_free;
2810 if (image == info.boot_index) {
2811 imagex_printf(T("Image %d is already marked as "
2812 "bootable.\n"), image);
2815 imagex_printf(T("Marking image %d as bootable.\n"),
2817 info.boot_index = image;
2818 ret = wimlib_set_wim_info(wim, &info,
2819 WIMLIB_CHANGE_BOOT_INDEX);
2821 goto out_wimlib_free;
2825 if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
2827 imagex_printf(T("Image %d is already named \"%"TS"\".\n"),
2831 imagex_printf(T("Changing the name of image %d to "
2832 "\"%"TS"\".\n"), image, new_name);
2833 ret = wimlib_set_image_name(wim, image, new_name);
2835 goto out_wimlib_free;
2839 const tchar *old_desc;
2840 old_desc = wimlib_get_image_description(wim, image);
2841 if (old_desc && !tstrcmp(old_desc, new_desc)) {
2842 imagex_printf(T("The description of image %d is already "
2843 "\"%"TS"\".\n"), image, new_desc);
2846 imagex_printf(T("Changing the description of image %d "
2847 "to \"%"TS"\".\n"), image, new_desc);
2848 ret = wimlib_set_image_descripton(wim, image,
2851 goto out_wimlib_free;
2855 /* Only call wimlib_overwrite() if something actually needs to
2857 if (boot || new_name || new_desc ||
2858 (check && !info.has_integrity_table) ||
2859 (nocheck && info.has_integrity_table))
2861 int write_flags = 0;
2864 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2866 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2867 ret = wimlib_overwrite(wim, write_flags, 1,
2868 imagex_progress_func);
2870 imagex_printf(T("The file \"%"TS"\" was not modified "
2871 "because nothing needed to be done.\n"),
2882 usage(CMD_INFO, stderr);
2888 /* Join split WIMs into one part WIM */
2890 imagex_join(int argc, tchar **argv, int cmd)
2893 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2894 int wim_write_flags = 0;
2895 const tchar *output_path;
2898 for_opt(c, join_options) {
2900 case IMAGEX_CHECK_OPTION:
2901 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2902 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2912 imagex_error(T("Must specify one or more split WIM (.swm) "
2916 output_path = argv[0];
2917 ret = wimlib_join((const tchar * const *)++argv,
2922 imagex_progress_func);
2927 usage(CMD_JOIN, stderr);
2932 #if WIM_MOUNTING_SUPPORTED
2934 /* Mounts a WIM image. */
2936 imagex_mount_rw_or_ro(int argc, tchar **argv, int cmd)
2939 int mount_flags = 0;
2940 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2941 const tchar *staging_dir = NULL;
2942 const tchar *wimfile;
2945 struct wimlib_wim_info info;
2949 REFGLOB_SET(refglobs);
2951 if (cmd == CMD_MOUNTRW) {
2952 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
2953 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2956 for_opt(c, mount_options) {
2958 case IMAGEX_ALLOW_OTHER_OPTION:
2959 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
2961 case IMAGEX_CHECK_OPTION:
2962 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2964 case IMAGEX_DEBUG_OPTION:
2965 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
2967 case IMAGEX_STREAMS_INTERFACE_OPTION:
2968 if (!tstrcasecmp(optarg, T("none")))
2969 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
2970 else if (!tstrcasecmp(optarg, T("xattr")))
2971 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2972 else if (!tstrcasecmp(optarg, T("windows")))
2973 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
2975 imagex_error(T("Unknown stream interface \"%"TS"\""),
2980 case IMAGEX_REF_OPTION:
2981 ret = refglob_set_append(&refglobs, optarg);
2983 goto out_free_refglobs;
2985 case IMAGEX_STAGING_DIR_OPTION:
2986 staging_dir = optarg;
2988 case IMAGEX_UNIX_DATA_OPTION:
2989 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
2997 if (argc != 2 && argc != 3)
3002 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3004 goto out_free_refglobs;
3006 wimlib_get_wim_info(wim, &info);
3009 /* Image explicitly specified. */
3010 image = wimlib_resolve_image(wim, argv[1]);
3012 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
3016 /* No image specified; default to image 1, but only if the WIM
3017 * contains exactly one image. */
3019 if (info.image_count != 1) {
3020 imagex_error(T("\"%"TS"\" contains %d images; Please "
3021 "select one."), wimfile, info.image_count);
3029 if (refglobs.num_globs) {
3030 ret = wim_reference_globs(wim, &refglobs, open_flags);
3035 ret = wimlib_mount_image(wim, image, dir, mount_flags, staging_dir);
3037 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
3039 image, wimfile, dir);
3044 refglob_set_destroy(&refglobs);
3050 goto out_free_refglobs;
3052 #endif /* WIM_MOUNTING_SUPPORTED */
3054 /* Rebuild a WIM file */
3056 imagex_optimize(int argc, tchar **argv, int cmd)
3059 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3060 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
3063 const tchar *wimfile;
3066 unsigned num_threads = 0;
3068 for_opt(c, optimize_options) {
3070 case IMAGEX_CHECK_OPTION:
3071 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3072 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3074 case IMAGEX_NOCHECK_OPTION:
3075 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3077 case IMAGEX_RECOMPRESS_OPTION:
3078 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3080 case IMAGEX_THREADS_OPTION:
3081 num_threads = parse_num_threads(optarg);
3082 if (num_threads == UINT_MAX)
3085 case IMAGEX_PIPABLE_OPTION:
3086 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
3088 case IMAGEX_NOT_PIPABLE_OPTION:
3089 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
3103 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3107 old_size = file_get_size(wimfile);
3108 tprintf(T("\"%"TS"\" original size: "), wimfile);
3110 tputs(T("Unknown"));
3112 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3114 ret = wimlib_overwrite(wim, write_flags, num_threads,
3115 imagex_progress_func);
3117 imagex_error(T("Optimization of \"%"TS"\" failed."), wimfile);
3118 goto out_wimlib_free;
3121 new_size = file_get_size(wimfile);
3122 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3124 tputs(T("Unknown"));
3126 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3128 tfputs(T("Space saved: "), stdout);
3129 if (new_size != -1 && old_size != -1) {
3130 tprintf(T("%lld KiB\n"),
3131 ((long long)old_size - (long long)new_size) >> 10);
3133 tputs(T("Unknown"));
3142 usage(CMD_OPTIMIZE, stderr);
3148 /* Split a WIM into a spanned set */
3150 imagex_split(int argc, tchar **argv, int cmd)
3154 int write_flags = 0;
3155 unsigned long part_size;
3160 for_opt(c, split_options) {
3162 case IMAGEX_CHECK_OPTION:
3163 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3164 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3176 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3177 if (tmp == argv[2] || *tmp) {
3178 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3179 imagex_error(T("The part size must be an integer or "
3180 "floating-point number of megabytes."));
3183 ret = wimlib_open_wim(argv[0], open_flags, &wim, imagex_progress_func);
3187 ret = wimlib_split(wim, argv[1], part_size, write_flags, imagex_progress_func);
3193 usage(CMD_SPLIT, stderr);
3199 #if WIM_MOUNTING_SUPPORTED
3200 /* Unmounts a mounted WIM image. */
3202 imagex_unmount(int argc, tchar **argv, int cmd)
3205 int unmount_flags = 0;
3208 for_opt(c, unmount_options) {
3210 case IMAGEX_COMMIT_OPTION:
3211 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3213 case IMAGEX_CHECK_OPTION:
3214 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3216 case IMAGEX_REBUILD_OPTION:
3217 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3219 case IMAGEX_LAZY_OPTION:
3220 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3231 ret = wimlib_unmount_image(argv[0], unmount_flags,
3232 imagex_progress_func);
3234 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3239 usage(CMD_UNMOUNT, stderr);
3243 #endif /* WIM_MOUNTING_SUPPORTED */
3246 * Add, delete, or rename files in a WIM image.
3249 imagex_update(int argc, tchar **argv, int cmd)
3251 const tchar *wimfile;
3255 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3256 int write_flags = 0;
3257 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3258 int default_add_flags = WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
3259 WIMLIB_ADD_FLAG_WINCONFIG;
3260 int default_delete_flags = 0;
3261 unsigned num_threads = 0;
3263 tchar *cmd_file_contents;
3264 size_t cmd_file_nchars;
3265 struct wimlib_update_command *cmds;
3267 tchar *command_str = NULL;
3269 const tchar *config_file = NULL;
3271 struct wimlib_capture_config *config;
3273 for_opt(c, update_options) {
3275 /* Generic or write options */
3276 case IMAGEX_THREADS_OPTION:
3277 num_threads = parse_num_threads(optarg);
3278 if (num_threads == UINT_MAX)
3281 case IMAGEX_CHECK_OPTION:
3282 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3283 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3285 case IMAGEX_REBUILD_OPTION:
3286 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3288 case IMAGEX_COMMAND_OPTION:
3290 imagex_error(T("--command may only be specified "
3291 "one time. Please provide\n"
3292 " the update commands "
3293 "on standard input instead."));
3296 command_str = tstrdup(optarg);
3298 imagex_error(T("Out of memory!"));
3302 /* Default delete options */
3303 case IMAGEX_FORCE_OPTION:
3304 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3306 case IMAGEX_RECURSIVE_OPTION:
3307 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3310 /* Global add option */
3311 case IMAGEX_CONFIG_OPTION:
3312 default_add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
3313 config_file = optarg;
3316 /* Default add options */
3317 case IMAGEX_VERBOSE_OPTION:
3318 default_add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
3320 case IMAGEX_DEREFERENCE_OPTION:
3321 default_add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
3323 case IMAGEX_UNIX_DATA_OPTION:
3324 default_add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
3326 case IMAGEX_NO_ACLS_OPTION:
3327 default_add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
3329 case IMAGEX_STRICT_ACLS_OPTION:
3330 default_add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
3339 if (argc != 1 && argc != 2)
3343 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3345 goto out_free_command_str;
3348 /* Image explicitly specified. */
3349 image = wimlib_resolve_image(wim, argv[1]);
3350 ret = verify_image_exists_and_is_single(image, argv[1],
3353 goto out_wimlib_free;
3355 /* No image specified; default to image 1, but only if the WIM
3356 * contains exactly one image. */
3357 struct wimlib_wim_info info;
3359 wimlib_get_wim_info(wim, &info);
3360 if (info.image_count != 1) {
3361 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3362 wimfile, info.image_count);
3369 /* Parse capture configuration file if specified */
3373 config_str = file_get_text_contents(config_file, &config_len);
3376 goto out_wimlib_free;
3379 config = alloca(sizeof(*config));
3380 ret = parse_capture_config(&config_str, config_len, config);
3382 goto out_free_config;
3387 /* Read update commands from standard input, or the command string if
3390 cmd_file_contents = NULL;
3391 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3394 if (isatty(STDIN_FILENO)) {
3395 tputs(T("Reading update commands from standard input..."));
3396 recommend_man_page(CMD_UPDATE, stdout);
3398 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3399 if (!cmd_file_contents) {
3401 goto out_free_config;
3404 /* Parse the update commands */
3405 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3410 goto out_free_cmd_file_contents;
3413 /* Set default flags and capture config on the update commands */
3414 for (size_t i = 0; i < num_cmds; i++) {
3415 switch (cmds[i].op) {
3416 case WIMLIB_UPDATE_OP_ADD:
3417 cmds[i].add.add_flags |= default_add_flags;
3418 cmds[i].add.config = config;
3420 case WIMLIB_UPDATE_OP_DELETE:
3421 cmds[i].delete.delete_flags |= default_delete_flags;
3428 /* Execute the update commands */
3429 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3430 imagex_progress_func);
3434 /* Overwrite the updated WIM */
3435 ret = wimlib_overwrite(wim, write_flags, num_threads,
3436 imagex_progress_func);
3439 out_free_cmd_file_contents:
3440 free(cmd_file_contents);
3443 free(config->exclusion_pats.pats);
3444 free(config->exclusion_exception_pats.pats);
3449 out_free_command_str:
3454 usage(CMD_UPDATE, stderr);
3457 goto out_free_command_str;
3462 struct imagex_command {
3464 int (*func)(int argc, tchar **argv, int cmd);
3467 static const struct imagex_command imagex_commands[] = {
3468 [CMD_APPEND] = {T("append"), imagex_capture_or_append},
3469 [CMD_APPLY] = {T("apply"), imagex_apply},
3470 [CMD_CAPTURE] = {T("capture"), imagex_capture_or_append},
3471 [CMD_DELETE] = {T("delete"), imagex_delete},
3472 [CMD_DIR ] = {T("dir"), imagex_dir},
3473 [CMD_EXPORT] = {T("export"), imagex_export},
3474 [CMD_EXTRACT] = {T("extract"), imagex_extract},
3475 [CMD_INFO] = {T("info"), imagex_info},
3476 [CMD_JOIN] = {T("join"), imagex_join},
3477 #if WIM_MOUNTING_SUPPORTED
3478 [CMD_MOUNT] = {T("mount"), imagex_mount_rw_or_ro},
3479 [CMD_MOUNTRW] = {T("mountrw"), imagex_mount_rw_or_ro},
3481 [CMD_OPTIMIZE] = {T("optimize"), imagex_optimize},
3482 [CMD_SPLIT] = {T("split"), imagex_split},
3483 #if WIM_MOUNTING_SUPPORTED
3484 [CMD_UNMOUNT] = {T("unmount"), imagex_unmount},
3486 [CMD_UPDATE] = {T("update"), imagex_update},
3489 static const tchar *usage_strings[] = {
3492 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3493 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3494 " [--nocheck] [--flags EDITION_ID] [--verbose]\n"
3495 " [--dereference] [--config=FILE] [--threads=NUM_THREADS]\n"
3496 " [--rebuild] [--unix-data] [--source-list] [--no-acls]\n"
3497 " [--strict-acls] [--rpfix] [--norpfix] [--pipable]\n"
3498 " [--not-pipable] [--delta-from=IMAGE]\n"
3502 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME | all)]\n"
3503 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
3504 " [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n"
3505 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3506 " [--include-invalid-names]\n"
3510 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3511 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3512 " [--nocheck] [--compress=TYPE] [--flags EDITION_ID]\n"
3513 " [--verbose] [--dereference] [--config=FILE]\n"
3514 " [--threads=NUM_THREADS] [--unix-data] [--source-list]\n"
3515 " [--no-acls] [--strict-acls] [--norpfix] [--pipable]\n"
3519 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n"
3524 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH]\n"
3528 " %"TS" SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
3529 " DEST_WIMFILE [DEST_IMAGE_NAME [DEST_IMAGE_DESCRIPTION]]\n"
3530 " [--boot] [--check] [--nocheck] [--compress=TYPE]\n"
3531 " [--ref=\"GLOB\"] [--threads=NUM_THREADS] [--rebuild]\n"
3532 " [--pipable] [--not-pipable]\n"
3536 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME) [PATH...]\n"
3537 " [--check] [--ref=\"GLOB\"] [--verbose] [--unix-data]\n"
3538 " [--no-acls] [--strict-acls] [--to-stdout]\n"
3539 " [--dest-dir=CMD_DIR] [--include-invalid-names]\n"
3543 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME) [NEW_NAME\n"
3544 " [NEW_DESC]]] [--boot] [--check] [--nocheck] [--header]\n"
3545 " [--lookup-table] [--xml] [--extract-xml FILE]\n"
3550 " %"TS" OUT_WIMFILE SPLIT_WIM_PART... [--check]\n"
3552 #if WIM_MOUNTING_SUPPORTED
3555 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3556 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
3557 " [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n"
3561 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3562 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
3563 " [--staging-dir=CMD_DIR] [--unix-data] [--allow-other]\n"
3568 " %"TS" WIMFILE [--check] [--nocheck] [--recompress]\n"
3569 " [--threads=NUM_THREADS] [--pipable] [--not-pipable]\n"
3573 " %"TS" WIMFILE SPLIT_WIM_PART_1 PART_SIZE_MB [--check]\n"
3575 #if WIM_MOUNTING_SUPPORTED
3578 " %"TS" DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
3583 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
3584 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
3585 " [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
3589 static const tchar *invocation_name;
3590 static int invocation_cmd = CMD_NONE;
3592 static const tchar *get_cmd_string(int cmd, bool nospace)
3594 static tchar buf[50];
3595 if (cmd == CMD_NONE) {
3596 tsprintf(buf, T("%"TS), T(IMAGEX_PROGNAME));
3597 } else if (invocation_cmd != CMD_NONE) {
3598 tsprintf(buf, T("wim%"TS), imagex_commands[cmd].name);
3600 const tchar *format;
3603 format = T("%"TS"-%"TS"");
3605 format = T("%"TS" %"TS"");
3606 tsprintf(buf, format, invocation_name, imagex_commands[cmd].name);
3614 static const tchar *s =
3616 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
3617 "Copyright (C) 2012, 2013 Eric Biggers\n"
3618 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
3619 "This is free software: you are free to change and redistribute it.\n"
3620 "There is NO WARRANTY, to the extent permitted by law.\n"
3622 "Report bugs to "PACKAGE_BUGREPORT".\n"
3629 help_or_version(int argc, tchar **argv, int cmd)
3634 for (i = 1; i < argc; i++) {
3636 if (p[0] == T('-') && p[1] == T('-')) {
3638 if (!tstrcmp(p, T("help"))) {
3639 if (cmd == CMD_NONE)
3644 } else if (!tstrcmp(p, T("version"))) {
3653 print_usage_string(int cmd, FILE *fp)
3655 tfprintf(fp, usage_strings[cmd], get_cmd_string(cmd, false));
3659 recommend_man_page(int cmd, FILE *fp)
3661 const tchar *format_str;
3663 format_str = T("See %"TS".pdf in the doc directory for more details.\n");
3665 format_str = T("Try `man %"TS"' for more details.\n");
3667 tfprintf(fp, format_str, get_cmd_string(cmd, true));
3671 usage(int cmd, FILE *fp)
3673 tfprintf(fp, T("Usage:\n"));
3674 print_usage_string(cmd, fp);
3675 tfprintf(fp, T("\n"));
3676 recommend_man_page(cmd, fp);
3682 tfprintf(fp, T("Usage:\n"));
3683 for (int cmd = 0; cmd < CMD_MAX; cmd++) {
3684 print_usage_string(cmd, fp);
3685 tfprintf(fp, T("\n"));
3687 static const tchar *extra =
3690 " %"TS" --version\n"
3692 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
3695 tfprintf(fp, extra, invocation_name, invocation_name);
3696 recommend_man_page(CMD_NONE, fp);
3699 /* Entry point for wimlib's ImageX implementation. On UNIX the command
3700 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
3701 * something else), while an Windows the command arguments will be UTF-16LE
3702 * encoded 'wchar_t' strings. */
3705 wmain(int argc, wchar_t **argv, wchar_t **envp)
3707 main(int argc, char **argv)
3714 imagex_info_file = stdout;
3715 invocation_name = tbasename(argv[0]);
3718 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
3719 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
3723 setlocale(LC_ALL, "");
3724 codeset = nl_langinfo(CODESET);
3725 if (!strstr(codeset, "UTF-8") &&
3726 !strstr(codeset, "UTF8") &&
3727 !strstr(codeset, "utf-8") &&
3728 !strstr(codeset, "utf8"))
3731 "WARNING: Running %"TS" in a UTF-8 locale is recommended!\n"
3732 " Maybe try: `export LANG=en_US.UTF-8'?\n"
3733 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
3734 " to any value to force wimlib to use UTF-8.\n",
3739 #endif /* !__WIN32__ */
3741 /* Allow being invoked as wimCOMMAND (e.g. wimapply). */
3743 if (!tstrncmp(invocation_name, T("wim"), 3) &&
3744 tstrcmp(invocation_name, T(IMAGEX_PROGNAME))) {
3745 for (int i = 0; i < CMD_MAX; i++) {
3746 if (!tstrcmp(invocation_name + 3,
3747 imagex_commands[i].name))
3756 /* Unless already known from the invocation name, determine which
3757 * command was specified. */
3758 if (cmd == CMD_NONE) {
3760 imagex_error(T("No command specified!\n"));
3764 for (int i = 0; i < CMD_MAX; i++) {
3765 if (!tstrcmp(argv[1], imagex_commands[i].name)) {
3770 if (cmd != CMD_NONE) {
3776 /* Handle --help and --version. --help can be either for the program as
3777 * a whole (cmd == CMD_NONE) or just for a specific command (cmd !=
3778 * CMD_NONE). Note: help_or_version() will not return if a --help or
3779 * --version argument was found. */
3780 help_or_version(argc, argv, cmd);
3782 /* Bail if a valid command was not specified. */
3783 if (cmd == CMD_NONE) {
3784 imagex_error(T("Unrecognized command: `%"TS"'\n"), argv[1]);
3789 /* Enable warning and error messages in wimlib be more user-friendly.
3791 wimlib_set_print_errors(true);
3793 /* Initialize wimlib. */
3794 ret = wimlib_global_init(init_flags);
3796 goto out_check_status;
3798 /* Call the command handler function. */
3799 ret = imagex_commands[cmd].func(argc, argv, cmd);
3801 /* Check for error writing to standard output, especially since for some
3802 * commands, writing to standard output is part of the program's actual
3803 * behavior and not just for informational purposes. */
3804 if (ferror(stdout) || fclose(stdout)) {
3805 imagex_error_with_errno(T("error writing to standard output"));
3810 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
3811 * itself (not by wimlib). 0 indicates success. > 0 indicates a wimlib
3812 * error code from which an error message can be printed. */
3814 imagex_error(T("Exiting with error code %d:\n"
3816 wimlib_get_error_string(ret));
3817 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
3818 imagex_error_with_errno(T("errno"));
3820 /* Make wimlib free any resources it's holding (although this is not
3821 * strictly necessary because the process is ending anyway). */
3822 wimlib_global_cleanup();