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_UPDATE_OF_OPTION,
162 IMAGEX_VERBOSE_OPTION,
166 static const struct option apply_options[] = {
167 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
168 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
169 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
170 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
171 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
172 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
173 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
174 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
175 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
176 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
177 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
178 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
180 /* --resume is undocumented for now as it needs improvement. */
181 {T("resume"), no_argument, NULL, IMAGEX_RESUME_OPTION},
185 static const struct option capture_or_append_options[] = {
186 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
187 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
188 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
189 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
190 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
191 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
192 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
193 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
194 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
195 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
196 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
197 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
198 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
199 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
200 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
201 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
202 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
203 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
204 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
205 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
206 {T("update-of"), required_argument, NULL, IMAGEX_UPDATE_OF_OPTION},
207 {T("delta-from"), required_argument, NULL, IMAGEX_DELTA_FROM_OPTION},
211 static const struct option delete_options[] = {
212 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
213 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
217 static const struct option dir_options[] = {
218 {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
222 static const struct option export_options[] = {
223 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
224 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
225 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
226 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
227 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
228 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
229 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
230 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
231 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
232 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
236 static const struct option extract_options[] = {
237 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
238 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
239 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
240 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
241 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
242 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
243 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
244 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
245 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
246 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
250 static const struct option info_options[] = {
251 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
252 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
253 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
254 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
255 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
256 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
257 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
258 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
259 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
263 static const struct option join_options[] = {
264 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
268 static const struct option mount_options[] = {
269 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
270 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
271 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
272 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
273 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
274 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
275 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
279 static const struct option optimize_options[] = {
280 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
281 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
282 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
283 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
284 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
285 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
286 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
290 static const struct option split_options[] = {
291 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
295 static const struct option unmount_options[] = {
296 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
297 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
298 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
299 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
303 static const struct option update_options[] = {
304 /* Careful: some of the options here set the defaults for update
305 * commands, but the flags given to an actual update command (and not to
306 * `imagex update' itself are also handled in
307 * update_command_add_option(). */
308 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
309 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
310 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
311 {T("command"), required_argument, NULL, IMAGEX_COMMAND_OPTION},
313 /* Default delete options */
314 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
315 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
317 /* Global add option */
318 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
320 /* Default add options */
321 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
322 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
323 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
324 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
325 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
326 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
332 # define _format_attribute(type, format_str, args_start) \
333 __attribute__((format(type, format_str, args_start)))
335 # define _format_attribute(type, format_str, args_start)
338 /* Print formatted error message to stderr. */
339 static void _format_attribute(printf, 1, 2)
340 imagex_error(const tchar *format, ...)
343 va_start(va, format);
344 tfputs(T("ERROR: "), stderr);
345 tvfprintf(stderr, format, va);
346 tputc(T('\n'), stderr);
350 /* Print formatted error message to stderr. */
351 static void _format_attribute(printf, 1, 2)
352 imagex_error_with_errno(const tchar *format, ...)
354 int errno_save = errno;
356 va_start(va, format);
357 tfputs(T("ERROR: "), stderr);
358 tvfprintf(stderr, format, va);
359 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
364 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
366 if (image == WIMLIB_NO_IMAGE) {
367 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
368 " Please specify a 1-based image index or "
369 "image name. To list the images\n"
370 " contained in the WIM archive, run\n"
372 " %"TS" \"%"TS"\"\n"),
373 image_name, wim_name,
374 get_cmd_string(CMD_INFO, false), wim_name);
375 return WIMLIB_ERR_INVALID_IMAGE;
381 verify_image_is_single(int image)
383 if (image == WIMLIB_ALL_IMAGES) {
384 imagex_error(T("Cannot specify all images for this action!"));
385 return WIMLIB_ERR_INVALID_IMAGE;
391 verify_image_exists_and_is_single(int image, const tchar *image_name,
392 const tchar *wim_name)
395 ret = verify_image_exists(image, image_name, wim_name);
397 ret = verify_image_is_single(image);
401 /* Parse the argument to --compress */
403 get_compression_type(const tchar *optarg)
405 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
406 return WIMLIB_COMPRESSION_TYPE_LZX;
407 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
408 return WIMLIB_COMPRESSION_TYPE_XPRESS;
409 else if (!tstrcasecmp(optarg, T("none")))
410 return WIMLIB_COMPRESSION_TYPE_NONE;
412 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
413 "\"maximum\", \"fast\", or \"none\"."), optarg);
414 return WIMLIB_COMPRESSION_TYPE_INVALID;
419 const tchar **strings;
420 unsigned num_strings;
421 unsigned num_alloc_strings;
424 #define STRING_SET_INITIALIZER \
425 { .strings = NULL, .num_strings = 0, .num_alloc_strings = 0, }
427 #define STRING_SET(_strings) \
428 struct string_set _strings = STRING_SET_INITIALIZER
431 string_set_append(struct string_set *set, const tchar *glob)
433 unsigned num_alloc_strings = set->num_alloc_strings;
435 if (set->num_strings == num_alloc_strings) {
436 const tchar **new_strings;
438 num_alloc_strings += 4;
439 new_strings = realloc(set->strings,
440 sizeof(set->strings[0]) * num_alloc_strings);
442 imagex_error(T("Out of memory!"));
445 set->strings = new_strings;
446 set->num_alloc_strings = num_alloc_strings;
448 set->strings[set->num_strings++] = glob;
453 string_set_destroy(struct string_set *set)
459 wim_reference_globs(WIMStruct *wim, struct string_set *set, int open_flags)
461 return wimlib_reference_resource_files(wim, set->strings,
463 WIMLIB_REF_FLAG_GLOB_ENABLE,
465 imagex_progress_func);
469 do_resource_not_found_warning(const tchar *wimfile,
470 const struct wimlib_wim_info *info,
471 const struct string_set *refglobs)
473 if (info->total_parts > 1) {
474 if (refglobs->num_strings == 0) {
475 imagex_error(T("\"%"TS"\" is part of a split WIM. "
476 "Use --ref to specify the other parts."),
479 imagex_error(T("Perhaps the '--ref' argument did not "
480 "specify all other parts of the split "
484 imagex_error(T("If this is a delta WIM, use the --ref argument "
485 "to specify the WIM on which it is based."));
489 /* Returns the size of a file given its name, or -1 if the file does not exist
490 * or its size cannot be determined. */
492 file_get_size(const tchar *filename)
495 if (tstat(filename, &st) == 0)
502 PARSE_STRING_SUCCESS = 0,
503 PARSE_STRING_FAILURE = 1,
504 PARSE_STRING_NONE = 2,
508 * Parses a string token from an array of characters.
510 * Tokens are either whitespace-delimited, or double or single-quoted.
512 * @line_p: Pointer to the pointer to the line of data. Will be updated
513 * to point past the string token iff the return value is
514 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
517 * @len_p: @len_p initially stores the length of the line of data, which may
518 * be 0, and it will be updated to the number of bytes remaining in
519 * the line iff the return value is PARSE_STRING_SUCCESS.
521 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
522 * parsed string token will be returned here.
524 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
525 * PARSE_STRING_FAILURE if the data was invalid due to a missing
526 * closing quote; or PARSE_STRING_NONE if the line ended before the
527 * beginning of a string token was found.
530 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
533 tchar *line = *line_p;
537 /* Skip leading whitespace */
540 return PARSE_STRING_NONE;
541 if (!istspace(*line) && *line != T('\0'))
547 if (quote_char == T('"') || quote_char == T('\'')) {
552 line = tmemchr(line, quote_char, len);
554 imagex_error(T("Missing closing quote: %"TS), fn - 1);
555 return PARSE_STRING_FAILURE;
558 /* Unquoted string. Go until whitespace. Line is terminated
559 * by '\0', so no need to check 'len'. */
563 } while (!istspace(*line) && *line != T('\0'));
570 return PARSE_STRING_SUCCESS;
573 /* Parses a line of data (not an empty line or comment) in the source list file
574 * format. (See the man page for 'wimlib-imagex capture' for details on this
575 * format and the meaning.)
577 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
578 * len == 0. The data in @line will be modified by this function call.
580 * @len: Length of the line of data.
582 * @source: On success, the capture source and target described by the line is
583 * written into this destination. Note that it will contain pointers
584 * to data in the @line array.
586 * Returns true if the line was valid; false otherwise. */
588 parse_source_list_line(tchar *line, size_t len,
589 struct wimlib_capture_source *source)
593 ret = parse_string(&line, &len, &source->fs_source_path);
594 if (ret != PARSE_STRING_SUCCESS)
596 ret = parse_string(&line, &len, &source->wim_target_path);
597 if (ret == PARSE_STRING_NONE)
598 source->wim_target_path = source->fs_source_path;
599 return ret != PARSE_STRING_FAILURE;
602 /* Returns %true if the given line of length @len > 0 is a comment or empty line
603 * in the source list file format. */
605 is_comment_line(const tchar *line, size_t len)
610 if (!istspace(*line) && *line != T('\0'))
620 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
623 tchar *contents = *contents_p;
624 size_t nchars = *nchars_p;
627 for (i = 0; i < nchars; i++)
628 if (contents[i] == T('\n'))
631 /* Handle last line not terminated by a newline */
632 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
633 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
635 imagex_error(T("Out of memory!"));
638 contents[nchars] = T('\n');
639 *contents_p = contents;
647 /* Parses a file in the source list format. (See the man page for
648 * 'wimlib-imagex capture' for details on this format and the meaning.)
650 * @source_list_contents: Contents of the source list file. Note that this
651 * buffer will be modified to save memory allocations,
652 * and cannot be freed until the returned array of
653 * wimlib_capture_source's has also been freed.
655 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
658 * @nsources_ret: On success, the length of the returned array is
661 * Returns: An array of `struct wimlib_capture_source's that can be passed to
662 * the wimlib_add_image_multisource() function to specify how a WIM image is to
664 static struct wimlib_capture_source *
665 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
666 size_t *nsources_ret)
670 struct wimlib_capture_source *sources;
673 nlines = text_file_count_lines(source_list_contents_p,
674 &source_list_nchars);
678 /* Always allocate at least 1 slot, just in case the implementation of
679 * calloc() returns NULL if 0 bytes are requested. */
680 sources = calloc(nlines ?: 1, sizeof(*sources));
682 imagex_error(T("out of memory"));
685 p = *source_list_contents_p;
687 for (i = 0; i < nlines; i++) {
688 /* XXX: Could use rawmemchr() here instead, but it may not be
689 * available on all platforms. */
690 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
691 size_t len = endp - p + 1;
693 if (!is_comment_line(p, len)) {
694 if (!parse_source_list_line(p, len, &sources[j++])) {
707 enum capture_config_section {
708 CAPTURE_CONFIG_NO_SECTION,
709 CAPTURE_CONFIG_EXCLUSION_SECTION,
710 CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
711 CAPTURE_CONFIG_IGNORE_SECTION,
715 CAPTURE_CONFIG_INVALID_SECTION,
716 CAPTURE_CONFIG_CHANGED_SECTION,
717 CAPTURE_CONFIG_SAME_SECTION,
721 check_config_section(tchar *line, size_t len,
722 enum capture_config_section *cur_section)
724 while (istspace(*line))
728 return CAPTURE_CONFIG_SAME_SECTION;
731 tchar *endbrace = tstrrchr(line, T(']'));
733 return CAPTURE_CONFIG_SAME_SECTION;
735 if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
736 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
737 } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
738 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
739 } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
740 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
741 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
742 "of capture config file\n"),
744 } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
745 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
746 tfputs(T("WARNING: Ignoring [AlignmentList] section "
747 "of capture config file\n"),
750 imagex_error(T("Invalid capture config file section \"%"TS"\""),
752 return CAPTURE_CONFIG_INVALID_SECTION;
754 return CAPTURE_CONFIG_CHANGED_SECTION;
759 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
762 if (pat_list->num_pats == pat_list->num_allocated_pats) {
764 size_t num_allocated_pats = pat_list->num_pats + 8;
766 pats = realloc(pat_list->pats,
767 num_allocated_pats * sizeof(pat_list->pats[0]));
769 imagex_error(T("Out of memory!"));
772 pat_list->pats = pats;
773 pat_list->num_allocated_pats = num_allocated_pats;
775 pat_list->pats[pat_list->num_pats++] = pat;
780 parse_capture_config_line(tchar *line, size_t len,
781 enum capture_config_section *cur_section,
782 struct wimlib_capture_config *config)
787 ret = check_config_section(line, len, cur_section);
788 if (ret == CAPTURE_CONFIG_INVALID_SECTION)
790 if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
793 switch (*cur_section) {
794 case CAPTURE_CONFIG_NO_SECTION:
795 imagex_error(T("Line \"%"TS"\" is not in a section "
796 "(such as [ExclusionList]"), line);
798 case CAPTURE_CONFIG_EXCLUSION_SECTION:
799 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
801 return pattern_list_add_pattern(&config->exclusion_pats,
803 case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
804 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
806 return pattern_list_add_pattern(&config->exclusion_exception_pats,
808 case CAPTURE_CONFIG_IGNORE_SECTION:
815 parse_capture_config(tchar **contents_p, size_t nchars,
816 struct wimlib_capture_config *config)
821 enum capture_config_section cur_section;
823 memset(config, 0, sizeof(*config));
825 nlines = text_file_count_lines(contents_p, &nchars);
829 cur_section = CAPTURE_CONFIG_NO_SECTION;
831 for (i = 0; i < nlines; i++) {
832 tchar *endp = tmemchr(p, T('\n'), nchars);
833 size_t len = endp - p + 1;
835 if (!is_comment_line(p, len))
836 if (!parse_capture_config_line(p, len, &cur_section, config))
844 /* Reads the contents of a file into memory. */
846 file_get_contents(const tchar *filename, size_t *len_ret)
853 if (tstat(filename, &stbuf) != 0) {
854 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
859 fp = tfopen(filename, T("rb"));
861 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
865 buf = malloc(len ? len : 1);
867 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
868 "contents of file \"%"TS"\""), len, filename);
871 if (fread(buf, 1, len, fp) != len) {
872 imagex_error_with_errno(T("Failed to read %zu bytes from the "
873 "file \"%"TS"\""), len, filename);
887 /* Read standard input until EOF and return the full contents in a malloc()ed
888 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
891 stdin_get_contents(size_t *len_ret)
893 /* stdin can, of course, be a pipe or other non-seekable file, so the
894 * total length of the data cannot be pre-determined */
896 size_t newlen = 1024;
900 char *p = realloc(buf, newlen);
901 size_t bytes_read, bytes_to_read;
903 imagex_error(T("out of memory while reading stdin"));
907 bytes_to_read = newlen - pos;
908 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
910 if (bytes_read != bytes_to_read) {
915 imagex_error_with_errno(T("error reading stdin"));
929 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
932 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
934 *num_tchars_ret = num_bytes;
936 #else /* !__WIN32__ */
937 /* On Windows, translate the text to UTF-16LE */
941 if (num_bytes >= 2 &&
942 (((unsigned char)text[0] == 0xff && (unsigned char)text[1] == 0xfe) ||
943 ((unsigned char)text[0] <= 0x7f && (unsigned char)text[1] == 0x00)))
945 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
946 * with something that looks like an ASCII character encoded as
947 * a UTF-16LE code unit. Assume the file is encoded as
948 * UTF-16LE. This is not a 100% reliable check. */
949 num_wchars = num_bytes / 2;
950 text_wstr = (wchar_t*)text;
952 /* File does not look like UTF-16LE. Assume it is encoded in
953 * the current Windows code page. I think these are always
954 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
955 * should work as expected. */
956 text_wstr = win32_mbs_to_wcs(text,
961 *num_tchars_ret = num_wchars;
963 #endif /* __WIN32__ */
967 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
972 contents = file_get_contents(filename, &num_bytes);
975 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
979 stdin_get_text_contents(size_t *num_tchars_ret)
984 contents = stdin_get_contents(&num_bytes);
987 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
990 #define TO_PERCENT(numerator, denominator) \
991 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
993 /* Given an enumerated value for WIM compression type, return a descriptive
996 get_data_type(int ctype)
999 case WIMLIB_COMPRESSION_TYPE_NONE:
1000 return T("uncompressed");
1001 case WIMLIB_COMPRESSION_TYPE_LZX:
1002 return T("LZX-compressed");
1003 case WIMLIB_COMPRESSION_TYPE_XPRESS:
1004 return T("XPRESS-compressed");
1009 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
1010 #define MEBIBYTE_MIN_NBYTES 10000000ULL
1011 #define KIBIBYTE_MIN_NBYTES 10000ULL
1014 get_unit(uint64_t total_bytes, const tchar **name_ret)
1016 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
1017 *name_ret = T("GiB");
1019 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
1020 *name_ret = T("MiB");
1022 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
1023 *name_ret = T("KiB");
1026 *name_ret = T("bytes");
1031 /* Progress callback function passed to various wimlib functions. */
1033 imagex_progress_func(enum wimlib_progress_msg msg,
1034 const union wimlib_progress_info *info)
1036 unsigned percent_done;
1037 unsigned unit_shift;
1038 const tchar *unit_name;
1039 if (imagex_be_quiet)
1042 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1043 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1044 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1045 info->write_streams.total_bytes);
1047 if (info->write_streams.completed_streams == 0) {
1048 const tchar *data_type;
1050 data_type = get_data_type(info->write_streams.compression_type);
1051 imagex_printf(T("Writing %"TS" data using %u thread%"TS"\n"),
1052 data_type, info->write_streams.num_threads,
1053 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1055 if (info->write_streams.total_parts <= 1) {
1056 imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1057 "written (%u%% done)"),
1058 info->write_streams.completed_bytes >> unit_shift,
1060 info->write_streams.total_bytes >> unit_shift,
1064 imagex_printf(T("\rWriting resources from part %u of %u: "
1065 "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written"),
1066 (info->write_streams.completed_parts ==
1067 info->write_streams.total_parts) ?
1068 info->write_streams.completed_parts :
1069 info->write_streams.completed_parts + 1,
1070 info->write_streams.total_parts,
1071 info->write_streams.completed_bytes >> unit_shift,
1073 info->write_streams.total_bytes >> unit_shift,
1077 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1078 imagex_printf(T("\n"));
1080 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1081 imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
1082 if (*info->scan.wim_target_path) {
1083 imagex_printf(T(" (loading as WIM path: "
1084 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
1085 info->scan.wim_target_path);
1087 imagex_printf(T(" (loading as root of WIM image)...\n"));
1090 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1091 switch (info->scan.status) {
1092 case WIMLIB_SCAN_DENTRY_OK:
1093 imagex_printf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
1095 case WIMLIB_SCAN_DENTRY_EXCLUDED:
1096 imagex_printf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1098 case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
1099 imagex_printf(T("WARNING: Excluding unsupported file or directory\n"
1100 " \"%"TS"\" from capture\n"), info->scan.cur_path);
1104 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1105 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1106 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1107 info->integrity.total_bytes);
1108 imagex_printf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1109 "of %"PRIu64" %"TS" (%u%%) done"),
1110 info->integrity.filename,
1111 info->integrity.completed_bytes >> unit_shift,
1113 info->integrity.total_bytes >> unit_shift,
1116 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1117 imagex_printf(T("\n"));
1119 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1120 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1121 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1122 info->integrity.total_bytes);
1123 imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1124 "of %"PRIu64" %"TS" (%u%%) done"),
1125 info->integrity.completed_bytes >> unit_shift,
1127 info->integrity.total_bytes >> unit_shift,
1130 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1131 imagex_printf(T("\n"));
1133 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1134 imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1135 "to %"TS" \"%"TS"\"\n"),
1136 info->extract.image,
1137 info->extract.image_name,
1138 info->extract.wimfile_name,
1139 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1140 T("NTFS volume") : T("directory")),
1141 info->extract.target);
1143 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1144 imagex_printf(T("Extracting "
1145 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\" from image %d (\"%"TS"\") "
1146 "in \"%"TS"\" to \"%"TS"\"\n"),
1147 info->extract.extract_root_wim_source_path,
1148 info->extract.image,
1149 info->extract.image_name,
1150 info->extract.wimfile_name,
1151 info->extract.target);
1153 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1154 percent_done = TO_PERCENT(info->extract.completed_bytes,
1155 info->extract.total_bytes);
1156 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1157 imagex_printf(T("\rExtracting files: "
1158 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1159 info->extract.completed_bytes >> unit_shift,
1161 info->extract.total_bytes >> unit_shift,
1164 if (info->extract.completed_bytes >= info->extract.total_bytes)
1165 imagex_printf(T("\n"));
1167 case WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN:
1168 if (info->extract.total_parts != 1) {
1169 imagex_printf(T("\nReading split pipable WIM part %u of %u\n"),
1170 info->extract.part_number,
1171 info->extract.total_parts);
1174 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1175 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1176 imagex_printf(T("Setting timestamps on all extracted files...\n"));
1178 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1179 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1180 imagex_printf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1181 info->extract.target);
1184 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1185 percent_done = TO_PERCENT(info->split.completed_bytes,
1186 info->split.total_bytes);
1187 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1188 imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
1189 "%"PRIu64" %"TS" (%u%%) written\n"),
1190 info->split.part_name,
1191 info->split.cur_part_number,
1192 info->split.total_parts,
1193 info->split.completed_bytes >> unit_shift,
1195 info->split.total_bytes >> unit_shift,
1199 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1200 if (info->split.completed_bytes == info->split.total_bytes) {
1201 imagex_printf(T("Finished writing split WIM part %u of %u\n"),
1202 info->split.cur_part_number,
1203 info->split.total_parts);
1206 case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1207 switch (info->update.command->op) {
1208 case WIMLIB_UPDATE_OP_DELETE:
1209 imagex_printf(T("Deleted WIM path "
1210 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1211 info->update.command->delete_.wim_path);
1213 case WIMLIB_UPDATE_OP_RENAME:
1214 imagex_printf(T("Renamed WIM path "
1215 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
1216 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1217 info->update.command->rename.wim_source_path,
1218 info->update.command->rename.wim_target_path);
1220 case WIMLIB_UPDATE_OP_ADD:
1228 fflush(imagex_info_file);
1233 parse_num_threads(const tchar *optarg)
1236 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1237 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1238 imagex_error(T("Number of threads must be a non-negative integer!"));
1246 * Parse an option passed to an update command.
1248 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1251 * @option: Text string for the option (beginning with --)
1253 * @cmd: `struct wimlib_update_command' that is being constructed for
1256 * Returns true if the option was recognized; false if not.
1259 update_command_add_option(int op, const tchar *option,
1260 struct wimlib_update_command *cmd)
1262 bool recognized = true;
1264 case WIMLIB_UPDATE_OP_ADD:
1265 if (!tstrcmp(option, T("--verbose")))
1266 cmd->add.add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
1267 else if (!tstrcmp(option, T("--unix-data")))
1268 cmd->add.add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
1269 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1270 cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
1271 else if (!tstrcmp(option, T("--strict-acls")))
1272 cmd->add.add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
1273 else if (!tstrcmp(option, T("--dereference")))
1274 cmd->add.add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
1278 case WIMLIB_UPDATE_OP_DELETE:
1279 if (!tstrcmp(option, T("--force")))
1280 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1281 else if (!tstrcmp(option, T("--recursive")))
1282 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1293 /* How many nonoption arguments each `imagex update' command expects */
1294 static const unsigned update_command_num_nonoptions[] = {
1295 [WIMLIB_UPDATE_OP_ADD] = 2,
1296 [WIMLIB_UPDATE_OP_DELETE] = 1,
1297 [WIMLIB_UPDATE_OP_RENAME] = 2,
1301 update_command_add_nonoption(int op, const tchar *nonoption,
1302 struct wimlib_update_command *cmd,
1303 unsigned num_nonoptions)
1306 case WIMLIB_UPDATE_OP_ADD:
1307 if (num_nonoptions == 0)
1308 cmd->add.fs_source_path = (tchar*)nonoption;
1310 cmd->add.wim_target_path = (tchar*)nonoption;
1312 case WIMLIB_UPDATE_OP_DELETE:
1313 cmd->delete_.wim_path = (tchar*)nonoption;
1315 case WIMLIB_UPDATE_OP_RENAME:
1316 if (num_nonoptions == 0)
1317 cmd->rename.wim_source_path = (tchar*)nonoption;
1319 cmd->rename.wim_target_path = (tchar*)nonoption;
1325 * Parse a command passed on stdin to `imagex update'.
1327 * @line: Text of the command.
1328 * @len: Length of the line, including a null terminator
1331 * @command: A `struct wimlib_update_command' to fill in from the parsed
1334 * @line_number: Line number of the command, for diagnostics.
1336 * Returns true on success; returns false on parse error.
1339 parse_update_command(tchar *line, size_t len,
1340 struct wimlib_update_command *command,
1344 tchar *command_name;
1346 size_t num_nonoptions;
1348 /* Get the command name ("add", "delete", "rename") */
1349 ret = parse_string(&line, &len, &command_name);
1350 if (ret != PARSE_STRING_SUCCESS)
1353 if (!tstrcasecmp(command_name, T("add"))) {
1354 op = WIMLIB_UPDATE_OP_ADD;
1355 } else if (!tstrcasecmp(command_name, T("delete"))) {
1356 op = WIMLIB_UPDATE_OP_DELETE;
1357 } else if (!tstrcasecmp(command_name, T("rename"))) {
1358 op = WIMLIB_UPDATE_OP_RENAME;
1360 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1361 command_name, line_number);
1366 /* Parse additional options and non-options as needed */
1371 ret = parse_string(&line, &len, &next_string);
1372 if (ret == PARSE_STRING_NONE) /* End of line */
1374 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1376 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1378 if (!update_command_add_option(op, next_string, command))
1380 imagex_error(T("Unrecognized option \"%"TS"\" to "
1381 "update command \"%"TS"\" on line %zu"),
1382 next_string, command_name, line_number);
1388 if (num_nonoptions == update_command_num_nonoptions[op])
1390 imagex_error(T("Unexpected argument \"%"TS"\" in "
1391 "update command on line %zu\n"
1392 " (The \"%"TS"\" command only "
1393 "takes %zu nonoption arguments!)\n"),
1394 next_string, line_number,
1395 command_name, num_nonoptions);
1398 update_command_add_nonoption(op, next_string,
1399 command, num_nonoptions);
1404 if (num_nonoptions != update_command_num_nonoptions[op]) {
1405 imagex_error(T("Not enough arguments to update command "
1406 "\"%"TS"\" on line %zu"), command_name, line_number);
1412 static struct wimlib_update_command *
1413 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1414 size_t *num_cmds_ret)
1418 struct wimlib_update_command *cmds;
1421 nlines = text_file_count_lines(cmd_file_contents_p,
1426 /* Always allocate at least 1 slot, just in case the implementation of
1427 * calloc() returns NULL if 0 bytes are requested. */
1428 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1430 imagex_error(T("out of memory"));
1433 p = *cmd_file_contents_p;
1435 for (i = 0; i < nlines; i++) {
1436 /* XXX: Could use rawmemchr() here instead, but it may not be
1437 * available on all platforms. */
1438 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1439 size_t len = endp - p + 1;
1441 if (!is_comment_line(p, len)) {
1442 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1453 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1454 * one image from a WIM file to a NTFS volume. */
1456 imagex_apply(int argc, tchar **argv, int cmd)
1460 int image = WIMLIB_NO_IMAGE;
1462 struct wimlib_wim_info info;
1464 const tchar *wimfile;
1465 const tchar *target;
1466 const tchar *image_num_or_name = NULL;
1467 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1469 STRING_SET(refglobs);
1471 for_opt(c, apply_options) {
1473 case IMAGEX_CHECK_OPTION:
1474 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1476 case IMAGEX_HARDLINK_OPTION:
1477 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1479 case IMAGEX_SYMLINK_OPTION:
1480 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1482 case IMAGEX_VERBOSE_OPTION:
1483 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
1485 case IMAGEX_REF_OPTION:
1486 ret = string_set_append(&refglobs, optarg);
1488 goto out_free_refglobs;
1490 case IMAGEX_UNIX_DATA_OPTION:
1491 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1493 case IMAGEX_NO_ACLS_OPTION:
1494 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1496 case IMAGEX_STRICT_ACLS_OPTION:
1497 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1499 case IMAGEX_NORPFIX_OPTION:
1500 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1502 case IMAGEX_RPFIX_OPTION:
1503 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1505 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1506 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1507 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1509 case IMAGEX_RESUME_OPTION:
1510 extract_flags |= WIMLIB_EXTRACT_FLAG_RESUME;
1518 if (argc != 2 && argc != 3)
1523 if (!tstrcmp(wimfile, T("-"))) {
1524 /* Attempt to apply pipable WIM from standard input. */
1526 image_num_or_name = NULL;
1529 image_num_or_name = argv[1];
1534 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1535 imagex_progress_func);
1537 goto out_free_refglobs;
1539 wimlib_get_wim_info(wim, &info);
1542 /* Image explicitly specified. */
1543 image_num_or_name = argv[1];
1544 image = wimlib_resolve_image(wim, image_num_or_name);
1545 ret = verify_image_exists(image, image_num_or_name, wimfile);
1547 goto out_wimlib_free;
1550 /* No image specified; default to image 1, but only if the WIM
1551 * contains exactly one image. */
1553 if (info.image_count != 1) {
1554 imagex_error(T("\"%"TS"\" contains %d images; "
1555 "Please select one (or all)."),
1556 wimfile, info.image_count);
1565 if (refglobs.num_strings) {
1567 imagex_error(T("Can't specify --ref when applying from stdin!"));
1569 goto out_wimlib_free;
1571 ret = wim_reference_globs(wim, &refglobs, open_flags);
1573 goto out_wimlib_free;
1578 /* Interpret a regular file or block device target as a NTFS
1582 if (tstat(target, &stbuf)) {
1583 if (errno != ENOENT) {
1584 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1587 goto out_wimlib_free;
1590 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1591 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1597 ret = wimlib_extract_image(wim, image, target, extract_flags,
1598 imagex_progress_func);
1600 set_fd_to_binary_mode(STDIN_FILENO);
1601 ret = wimlib_extract_image_from_pipe(STDIN_FILENO,
1603 target, extract_flags,
1604 imagex_progress_func);
1607 imagex_printf(T("Done applying WIM image.\n"));
1608 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
1610 do_resource_not_found_warning(wimfile, &info, &refglobs);
1612 imagex_error(T( "If you are applying an image "
1613 "from a split pipable WIM,\n"
1614 " make sure you have "
1615 "concatenated together all parts."));
1621 string_set_destroy(&refglobs);
1625 usage(CMD_APPLY, stderr);
1627 goto out_free_refglobs;
1630 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1631 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1632 * the desired image. 'wimlib-imagex append': add a new image to an existing
1635 imagex_capture_or_append(int argc, tchar **argv, int cmd)
1638 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1639 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE |
1640 WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1641 int write_flags = 0;
1642 int compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1643 const tchar *wimfile;
1647 const tchar *flags_element = NULL;
1650 WIMStruct *base_wim;
1651 const tchar *base_wimfile = NULL;
1652 WIMStruct *template_wim;
1653 const tchar *template_wimfile = NULL;
1654 const tchar *template_image_name_or_num = NULL;
1655 int template_image = WIMLIB_NO_IMAGE;
1658 unsigned num_threads = 0;
1663 const tchar *config_file = NULL;
1665 struct wimlib_capture_config *config;
1667 bool source_list = false;
1668 size_t source_list_nchars = 0;
1669 tchar *source_list_contents;
1670 bool capture_sources_malloced;
1671 struct wimlib_capture_source *capture_sources;
1673 bool name_defaulted;
1675 for_opt(c, capture_or_append_options) {
1677 case IMAGEX_BOOT_OPTION:
1678 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1680 case IMAGEX_CHECK_OPTION:
1681 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1682 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1684 case IMAGEX_NOCHECK_OPTION:
1685 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
1687 case IMAGEX_CONFIG_OPTION:
1688 config_file = optarg;
1689 add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1691 case IMAGEX_COMPRESS_OPTION:
1692 compression_type = get_compression_type(optarg);
1693 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1696 case IMAGEX_FLAGS_OPTION:
1697 flags_element = optarg;
1699 case IMAGEX_DEREFERENCE_OPTION:
1700 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1702 case IMAGEX_VERBOSE_OPTION:
1703 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1705 case IMAGEX_THREADS_OPTION:
1706 num_threads = parse_num_threads(optarg);
1707 if (num_threads == UINT_MAX)
1710 case IMAGEX_REBUILD_OPTION:
1711 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1713 case IMAGEX_UNIX_DATA_OPTION:
1714 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1716 case IMAGEX_SOURCE_LIST_OPTION:
1719 case IMAGEX_NO_ACLS_OPTION:
1720 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1722 case IMAGEX_STRICT_ACLS_OPTION:
1723 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1725 case IMAGEX_RPFIX_OPTION:
1726 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1728 case IMAGEX_NORPFIX_OPTION:
1729 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1731 case IMAGEX_PIPABLE_OPTION:
1732 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1734 case IMAGEX_NOT_PIPABLE_OPTION:
1735 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
1737 case IMAGEX_UPDATE_OF_OPTION:
1738 if (template_image_name_or_num) {
1739 imagex_error(T("'--update-of' can only be "
1740 "specified one time!"));
1744 colon = tstrrchr(optarg, T(':'));
1747 template_wimfile = optarg;
1749 template_image_name_or_num = colon + 1;
1751 template_wimfile = NULL;
1752 template_image_name_or_num = optarg;
1756 case IMAGEX_DELTA_FROM_OPTION:
1757 if (cmd != CMD_CAPTURE) {
1758 imagex_error(T("'--delta-from' is only "
1759 "valid for capture!"));
1763 imagex_error(T("'--delta-from' can only be "
1764 "specified one time!"));
1767 base_wimfile = optarg;
1768 write_flags |= WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS;
1777 if (argc < 2 || argc > 4)
1783 if (!tstrcmp(wimfile, T("-"))) {
1784 /* Writing captured WIM to standard output. */
1786 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
1787 imagex_error("Can't write a non-pipable WIM to "
1788 "standard output! Specify --pipable\n"
1789 " if you want to create a pipable WIM "
1790 "(but read the docs first).");
1794 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1796 if (cmd == CMD_APPEND) {
1797 imagex_error(T("Using standard output for append does "
1798 "not make sense."));
1801 wim_fd = STDOUT_FILENO;
1803 imagex_info_file = stderr;
1804 set_fd_to_binary_mode(wim_fd);
1807 /* If template image was specified using --update-of=IMAGE rather
1808 * than --update-of=WIMFILE:IMAGE, set the default WIMFILE. */
1809 if (template_image_name_or_num && !template_wimfile) {
1811 /* Capturing delta WIM: default to base WIM. */
1812 template_wimfile = base_wimfile;
1813 } else if (cmd == CMD_APPEND) {
1814 /* Appending to WIM: default to WIM being appended to.
1816 template_wimfile = wimfile;
1818 /* Capturing a normal (non-delta) WIM, so the WIM file
1819 * *must* be explicitly specified. */
1820 imagex_error(T("For capture of non-delta WIM, "
1821 "'--update-of' must specify "
1829 name_defaulted = false;
1831 /* Set default name to SOURCE argument, omitting any directory
1832 * prefixes and trailing slashes. This requires making a copy
1833 * of @source. Leave some free characters at the end in case we
1834 * append a number to keep the name unique. */
1835 size_t source_name_len;
1837 source_name_len = tstrlen(source);
1838 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1839 name = tbasename(tstrcpy(source_copy, source));
1840 name_defaulted = true;
1842 /* Image description defaults to NULL if not given. */
1849 /* Set up capture sources in source list mode */
1850 if (source[0] == T('-') && source[1] == T('\0')) {
1851 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1853 source_list_contents = file_get_text_contents(source,
1854 &source_list_nchars);
1856 if (!source_list_contents)
1859 capture_sources = parse_source_list(&source_list_contents,
1862 if (!capture_sources) {
1864 goto out_free_source_list_contents;
1866 capture_sources_malloced = true;
1868 /* Set up capture source in non-source-list mode. */
1869 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1870 capture_sources[0].fs_source_path = source;
1871 capture_sources[0].wim_target_path = NULL;
1872 capture_sources[0].reserved = 0;
1874 capture_sources_malloced = false;
1875 source_list_contents = NULL;
1879 /* Read and parse capture configuration file. */
1882 config_str = file_get_text_contents(config_file, &config_len);
1885 goto out_free_capture_sources;
1888 config = alloca(sizeof(*config));
1889 ret = parse_capture_config(&config_str, config_len, config);
1891 goto out_free_config;
1893 /* No capture configuration file specified; use default
1894 * configuration for capturing Windows operating systems. */
1896 add_image_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
1899 /* Open the existing WIM, or create a new one. */
1900 if (cmd == CMD_APPEND)
1901 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1902 imagex_progress_func);
1904 ret = wimlib_create_new_wim(compression_type, &wim);
1906 goto out_free_config;
1909 /* Detect if source is regular file or block device and set NTFS volume
1914 if (tstat(source, &stbuf) == 0) {
1915 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1916 imagex_printf(T("Capturing WIM image from NTFS "
1917 "filesystem on \"%"TS"\"\n"), source);
1918 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1921 if (errno != ENOENT) {
1922 imagex_error_with_errno(T("Failed to stat "
1923 "\"%"TS"\""), source);
1931 /* If the user did not specify an image name, and the basename of the
1932 * source already exists as an image name in the WIM file, append a
1933 * suffix to make it unique. */
1934 if (cmd == CMD_APPEND && name_defaulted) {
1935 unsigned long conflict_idx;
1936 tchar *name_end = tstrchr(name, T('\0'));
1937 for (conflict_idx = 1;
1938 wimlib_image_name_in_use(wim, name);
1941 tsprintf(name_end, T(" (%lu)"), conflict_idx);
1945 /* If capturing a delta WIM, reference resources from the base WIM
1946 * before adding the new image. */
1948 ret = wimlib_open_wim(base_wimfile, open_flags,
1949 &base_wim, imagex_progress_func);
1953 imagex_printf(T("Capturing delta WIM based on \"%"TS"\"\n"),
1956 ret = wimlib_reference_resources(wim, &base_wim, 1, 0);
1958 goto out_free_base_wim;
1963 /* If capturing or appending as an update of an existing (template) image,
1964 * open the WIM if needed and parse the image index. */
1965 if (template_image_name_or_num) {
1968 if (template_wimfile == base_wimfile) {
1969 template_wim = base_wim;
1970 } else if (template_wimfile == wimfile) {
1973 ret = wimlib_open_wim(template_wimfile, open_flags,
1974 &template_wim, imagex_progress_func);
1976 goto out_free_base_wim;
1979 template_image = wimlib_resolve_image(template_wim,
1980 template_image_name_or_num);
1982 if (template_image_name_or_num[0] == T('-')) {
1985 struct wimlib_wim_info info;
1987 wimlib_get_wim_info(template_wim, &info);
1988 n = tstrtoul(template_image_name_or_num + 1, &tmp, 10);
1989 if (n >= 1 && n <= info.image_count &&
1991 tmp != template_image_name_or_num + 1)
1993 template_image = info.image_count - (n - 1);
1996 ret = verify_image_exists_and_is_single(template_image,
1997 template_image_name_or_num,
2000 goto out_free_template_wim;
2002 template_wim = NULL;
2005 ret = wimlib_add_image_multisource(wim,
2011 imagex_progress_func);
2013 goto out_free_template_wim;
2015 if (desc || flags_element || template_image_name_or_num) {
2016 /* User provided <DESCRIPTION> or <FLAGS> element, or an image
2017 * on which the added one is to be based has been specified with
2018 * --update-of. Get the index of the image we just
2019 * added, then use it to call the appropriate functions. */
2020 struct wimlib_wim_info info;
2022 wimlib_get_wim_info(wim, &info);
2025 ret = wimlib_set_image_descripton(wim,
2029 goto out_free_template_wim;
2032 if (flags_element) {
2033 ret = wimlib_set_image_flags(wim, info.image_count,
2036 goto out_free_template_wim;
2039 /* Reference template image if the user provided one. */
2040 if (template_image_name_or_num) {
2041 imagex_printf(T("Using image %d "
2042 "from \"%"TS"\" as template\n"),
2043 template_image, template_wimfile);
2044 ret = wimlib_reference_template_image(wim,
2050 goto out_free_template_wim;
2054 /* Write the new WIM or overwrite the existing WIM with the new image
2056 if (cmd == CMD_APPEND) {
2057 ret = wimlib_overwrite(wim, write_flags, num_threads,
2058 imagex_progress_func);
2059 } else if (wimfile) {
2060 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
2061 write_flags, num_threads,
2062 imagex_progress_func);
2064 ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
2065 write_flags, num_threads,
2066 imagex_progress_func);
2068 out_free_template_wim:
2069 /* template_wim may alias base_wim or wim. */
2070 if (template_wim != base_wim && template_wim != wim)
2071 wimlib_free(template_wim);
2073 wimlib_free(base_wim);
2078 free(config->exclusion_pats.pats);
2079 free(config->exclusion_exception_pats.pats);
2082 out_free_capture_sources:
2083 if (capture_sources_malloced)
2084 free(capture_sources);
2085 out_free_source_list_contents:
2086 free(source_list_contents);
2097 /* Remove image(s) from a WIM. */
2099 imagex_delete(int argc, tchar **argv, int cmd)
2102 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2103 int write_flags = 0;
2104 const tchar *wimfile;
2105 const tchar *image_num_or_name;
2110 for_opt(c, delete_options) {
2112 case IMAGEX_CHECK_OPTION:
2113 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2114 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2116 case IMAGEX_SOFT_OPTION:
2117 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
2128 imagex_error(T("Must specify a WIM file"));
2130 imagex_error(T("Must specify an image"));
2134 image_num_or_name = argv[1];
2136 ret = wimlib_open_wim(wimfile, open_flags, &wim,
2137 imagex_progress_func);
2141 image = wimlib_resolve_image(wim, image_num_or_name);
2143 ret = verify_image_exists(image, image_num_or_name, wimfile);
2145 goto out_wimlib_free;
2147 ret = wimlib_delete_image(wim, image);
2149 imagex_error(T("Failed to delete image from \"%"TS"\""),
2151 goto out_wimlib_free;
2154 ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
2156 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2157 "deleted"), wimfile);
2165 usage(CMD_DELETE, stderr);
2171 print_full_path(const struct wimlib_dir_entry *wdentry, void *_ignore)
2173 int ret = tprintf(T("%"TS"\n"), wdentry->full_path);
2174 return (ret >= 0) ? 0 : -1;
2177 /* Print the files contained in an image(s) in a WIM file. */
2179 imagex_dir(int argc, tchar **argv, int cmd)
2181 const tchar *wimfile;
2182 WIMStruct *wim = NULL;
2185 const tchar *path = T("");
2188 for_opt(c, dir_options) {
2190 case IMAGEX_PATH_OPTION:
2201 imagex_error(T("Must specify a WIM file"));
2205 imagex_error(T("Too many arguments"));
2210 ret = wimlib_open_wim(wimfile, 0, &wim, imagex_progress_func);
2215 image = wimlib_resolve_image(wim, argv[1]);
2216 ret = verify_image_exists(image, argv[1], wimfile);
2218 goto out_wimlib_free;
2220 /* No image specified; default to image 1, but only if the WIM
2221 * contains exactly one image. */
2223 struct wimlib_wim_info info;
2225 wimlib_get_wim_info(wim, &info);
2226 if (info.image_count != 1) {
2227 imagex_error(T("\"%"TS"\" contains %d images; Please "
2228 "select one (or all)."),
2229 wimfile, info.image_count);
2236 ret = wimlib_iterate_dir_tree(wim, image, path,
2237 WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE,
2238 print_full_path, NULL);
2245 usage(CMD_DIR, stderr);
2250 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2253 imagex_export(int argc, tchar **argv, int cmd)
2257 int export_flags = 0;
2258 int write_flags = 0;
2259 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2260 const tchar *src_wimfile;
2261 const tchar *src_image_num_or_name;
2262 const tchar *dest_wimfile;
2264 const tchar *dest_name;
2265 const tchar *dest_desc;
2267 struct wimlib_wim_info src_info;
2268 WIMStruct *dest_wim;
2273 STRING_SET(refglobs);
2274 unsigned num_threads = 0;
2276 for_opt(c, export_options) {
2278 case IMAGEX_BOOT_OPTION:
2279 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2281 case IMAGEX_CHECK_OPTION:
2282 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2283 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2285 case IMAGEX_NOCHECK_OPTION:
2286 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2288 case IMAGEX_COMPRESS_OPTION:
2289 compression_type = get_compression_type(optarg);
2290 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2293 case IMAGEX_REF_OPTION:
2294 ret = string_set_append(&refglobs, optarg);
2296 goto out_free_refglobs;
2298 case IMAGEX_THREADS_OPTION:
2299 num_threads = parse_num_threads(optarg);
2300 if (num_threads == UINT_MAX)
2303 case IMAGEX_REBUILD_OPTION:
2304 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2306 case IMAGEX_PIPABLE_OPTION:
2307 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2309 case IMAGEX_NOT_PIPABLE_OPTION:
2310 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
2318 if (argc < 3 || argc > 5)
2320 src_wimfile = argv[0];
2321 src_image_num_or_name = argv[1];
2322 dest_wimfile = argv[2];
2323 dest_name = (argc >= 4) ? argv[3] : NULL;
2324 dest_desc = (argc >= 5) ? argv[4] : NULL;
2325 ret = wimlib_open_wim(src_wimfile, open_flags, &src_wim,
2326 imagex_progress_func);
2328 goto out_free_refglobs;
2330 wimlib_get_wim_info(src_wim, &src_info);
2332 /* Determine if the destination is an existing file or not. If so, we
2333 * try to append the exported image(s) to it; otherwise, we create a new
2334 * WIM containing the exported image(s). Furthermore, determine if we
2335 * need to write a pipable WIM directly to standard output. */
2337 if (tstrcmp(dest_wimfile, T("-")) == 0) {
2339 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2340 imagex_error("Can't write a non-pipable WIM to "
2341 "standard output! Specify --pipable\n"
2342 " if you want to create a pipable WIM "
2343 "(but read the docs first).");
2345 goto out_free_src_wim;
2348 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2350 dest_wimfile = NULL;
2351 dest_wim_fd = STDOUT_FILENO;
2352 imagex_info_file = stderr;
2353 set_fd_to_binary_mode(dest_wim_fd);
2356 if (dest_wimfile != NULL && tstat(dest_wimfile, &stbuf) == 0) {
2358 /* Destination file exists. */
2360 if (!S_ISREG(stbuf.st_mode)) {
2361 imagex_error(T("\"%"TS"\" is not a regular file"),
2364 goto out_free_src_wim;
2366 ret = wimlib_open_wim(dest_wimfile,
2367 open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2368 &dest_wim, imagex_progress_func);
2370 goto out_free_src_wim;
2372 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2373 /* The user specified a compression type, but we're
2374 * exporting to an existing WIM. Make sure the
2375 * specified compression type is the same as the
2376 * compression type of the existing destination WIM. */
2377 struct wimlib_wim_info dest_info;
2379 wimlib_get_wim_info(dest_wim, &dest_info);
2380 if (compression_type != dest_info.compression_type) {
2381 imagex_error(T("Cannot specify a compression type that is "
2382 "not the same as that used in the "
2383 "destination WIM"));
2385 goto out_free_dest_wim;
2391 if (errno != ENOENT) {
2392 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2395 goto out_free_src_wim;
2398 /* dest_wimfile is not an existing file, so create a new WIM. */
2400 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2401 /* The user did not specify a compression type; default
2402 * to that of the source WIM. */
2404 compression_type = src_info.compression_type;
2406 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2408 goto out_free_src_wim;
2411 image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2412 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2414 goto out_free_dest_wim;
2416 if (refglobs.num_strings) {
2417 ret = wim_reference_globs(src_wim, &refglobs, open_flags);
2419 goto out_free_dest_wim;
2422 if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
2423 image == WIMLIB_ALL_IMAGES && src_info.boot_index == 0)
2425 imagex_error(T("--boot specified for all-images export, but source WIM "
2426 "has no bootable image."));
2428 goto out_free_dest_wim;
2431 ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2432 dest_desc, export_flags, imagex_progress_func);
2434 if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2435 do_resource_not_found_warning(src_wimfile,
2436 &src_info, &refglobs);
2438 goto out_free_dest_wim;
2442 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2443 imagex_progress_func);
2444 else if (dest_wimfile)
2445 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2446 write_flags, num_threads,
2447 imagex_progress_func);
2449 ret = wimlib_write_to_fd(dest_wim, dest_wim_fd,
2450 WIMLIB_ALL_IMAGES, write_flags,
2451 num_threads, imagex_progress_func);
2453 wimlib_free(dest_wim);
2455 wimlib_free(src_wim);
2457 string_set_destroy(&refglobs);
2461 usage(CMD_EXPORT, stderr);
2464 goto out_free_refglobs;
2468 is_root_wim_path(const tchar *path)
2471 for (p = path; *p; p++)
2472 if (*p != T('\\') && *p != T('/'))
2478 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2479 const tchar *dest_dir)
2481 for (size_t i = 0; i < num_cmds; i++)
2482 if (cmds[i].fs_dest_path != dest_dir)
2483 free(cmds[i].fs_dest_path);
2487 static struct wimlib_extract_command *
2488 prepare_extract_commands(tchar **paths, unsigned num_paths,
2489 int extract_flags, tchar *dest_dir,
2490 size_t *num_cmds_ret)
2492 struct wimlib_extract_command *cmds;
2494 tchar *emptystr = T("");
2496 if (num_paths == 0) {
2500 num_cmds = num_paths;
2501 cmds = calloc(num_cmds, sizeof(cmds[0]));
2503 imagex_error(T("Out of memory!"));
2507 for (size_t i = 0; i < num_cmds; i++) {
2508 cmds[i].extract_flags = extract_flags;
2509 cmds[i].wim_source_path = paths[i];
2510 if (is_root_wim_path(paths[i])) {
2511 cmds[i].fs_dest_path = dest_dir;
2513 size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2514 cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2515 if (!cmds[i].fs_dest_path) {
2516 free_extract_commands(cmds, num_cmds, dest_dir);
2519 tsprintf(cmds[i].fs_dest_path,
2520 T("%"TS""OS_PREFERRED_PATH_SEPARATOR_STRING"%"TS),
2521 dest_dir, tbasename(paths[i]));
2524 *num_cmds_ret = num_cmds;
2528 /* Extract files or directories from a WIM image */
2530 imagex_extract(int argc, tchar **argv, int cmd)
2537 const tchar *wimfile;
2538 const tchar *image_num_or_name;
2539 tchar *dest_dir = T(".");
2540 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL | WIMLIB_EXTRACT_FLAG_NORPFIX;
2542 STRING_SET(refglobs);
2544 struct wimlib_extract_command *cmds;
2547 for_opt(c, extract_options) {
2549 case IMAGEX_CHECK_OPTION:
2550 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2552 case IMAGEX_VERBOSE_OPTION:
2553 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
2555 case IMAGEX_REF_OPTION:
2556 ret = string_set_append(&refglobs, optarg);
2558 goto out_free_refglobs;
2560 case IMAGEX_UNIX_DATA_OPTION:
2561 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2563 case IMAGEX_NO_ACLS_OPTION:
2564 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2566 case IMAGEX_STRICT_ACLS_OPTION:
2567 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2569 case IMAGEX_DEST_DIR_OPTION:
2572 case IMAGEX_TO_STDOUT_OPTION:
2573 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2574 imagex_info_file = stderr;
2575 imagex_be_quiet = true;
2577 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2578 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2579 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2592 image_num_or_name = argv[1];
2597 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2602 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2606 image = wimlib_resolve_image(wim, image_num_or_name);
2607 ret = verify_image_exists_and_is_single(image,
2611 goto out_wimlib_free;
2613 if (refglobs.num_strings) {
2614 ret = wim_reference_globs(wim, &refglobs, open_flags);
2616 goto out_wimlib_free;
2619 ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
2620 imagex_progress_func);
2622 if (!imagex_be_quiet)
2623 imagex_printf(T("Done extracting files.\n"));
2624 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2625 tfprintf(stderr, T("Note: You can use `%"TS"' to see what "
2626 "files and directories\n"
2627 " are in the WIM image.\n"),
2628 get_cmd_string(CMD_INFO, false));
2629 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2630 struct wimlib_wim_info info;
2632 wimlib_get_wim_info(wim, &info);
2633 do_resource_not_found_warning(wimfile, &info, &refglobs);
2638 free_extract_commands(cmds, num_cmds, dest_dir);
2640 string_set_destroy(&refglobs);
2644 usage(CMD_EXTRACT, stderr);
2647 goto out_free_refglobs;
2650 static void print_byte_field(const uint8_t field[], size_t len)
2653 tprintf(T("%02hhx"), *field++);
2657 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2659 tputs(T("WIM Information:"));
2660 tputs(T("----------------"));
2661 tprintf(T("Path: %"TS"\n"), wimfile);
2662 tprintf(T("GUID: 0x"));
2663 print_byte_field(info->guid, sizeof(info->guid));
2665 tprintf(T("Image Count: %d\n"), info->image_count);
2666 tprintf(T("Compression: %"TS"\n"),
2667 wimlib_get_compression_type_string(info->compression_type));
2668 tprintf(T("Part Number: %d/%d\n"), info->part_number, info->total_parts);
2669 tprintf(T("Boot Index: %d\n"), info->boot_index);
2670 tprintf(T("Size: %"PRIu64" bytes\n"), info->total_bytes);
2671 tprintf(T("Integrity Info: %"TS"\n"),
2672 info->has_integrity_table ? T("yes") : T("no"));
2673 tprintf(T("Relative path junction: %"TS"\n"),
2674 info->has_rpfix ? T("yes") : T("no"));
2675 tprintf(T("Pipable: %"TS"\n"),
2676 info->pipable ? T("yes") : T("no"));
2681 print_resource(const struct wimlib_resource_entry *resource,
2685 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
2686 resource->uncompressed_size);
2688 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
2689 resource->compressed_size);
2691 tprintf(T("Offset = %"PRIu64" bytes\n"),
2695 tprintf(T("Part Number = %u\n"), resource->part_number);
2696 tprintf(T("Reference Count = %u\n"), resource->reference_count);
2698 tprintf(T("Hash = 0x"));
2699 print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2702 tprintf(T("Flags = "));
2703 if (resource->is_compressed)
2704 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED "));
2705 if (resource->is_metadata)
2706 tprintf(T("WIM_RESHDR_FLAG_METADATA "));
2707 if (resource->is_free)
2708 tprintf(T("WIM_RESHDR_FLAG_FREE "));
2709 if (resource->is_spanned)
2710 tprintf(T("WIM_RESHDR_FLAG_SPANNED "));
2717 print_lookup_table(WIMStruct *wim)
2719 wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2722 /* Prints information about a WIM file; also can mark an image as bootable,
2723 * change the name of an image, or change the description of an image. */
2725 imagex_info(int argc, tchar **argv, int cmd)
2730 bool nocheck = false;
2731 bool header = false;
2732 bool lookup_table = false;
2734 bool metadata = false;
2735 bool short_header = true;
2736 const tchar *xml_out_file = NULL;
2737 const tchar *wimfile;
2738 const tchar *image_num_or_name;
2739 const tchar *new_name;
2740 const tchar *new_desc;
2745 struct wimlib_wim_info info;
2747 for_opt(c, info_options) {
2749 case IMAGEX_BOOT_OPTION:
2752 case IMAGEX_CHECK_OPTION:
2755 case IMAGEX_NOCHECK_OPTION:
2758 case IMAGEX_HEADER_OPTION:
2760 short_header = false;
2762 case IMAGEX_LOOKUP_TABLE_OPTION:
2763 lookup_table = true;
2764 short_header = false;
2766 case IMAGEX_XML_OPTION:
2768 short_header = false;
2770 case IMAGEX_EXTRACT_XML_OPTION:
2771 xml_out_file = optarg;
2772 short_header = false;
2774 case IMAGEX_METADATA_OPTION:
2776 short_header = false;
2785 if (argc < 1 || argc > 4)
2789 image_num_or_name = (argc >= 2) ? argv[1] : T("all");
2790 new_name = (argc >= 3) ? argv[2] : NULL;
2791 new_desc = (argc >= 4) ? argv[3] : NULL;
2793 if (check && nocheck) {
2794 imagex_error(T("Can't specify both --check and --nocheck"));
2799 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2801 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2805 wimlib_get_wim_info(wim, &info);
2807 image = wimlib_resolve_image(wim, image_num_or_name);
2808 ret = WIMLIB_ERR_INVALID_IMAGE;
2809 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
2810 verify_image_exists(image, image_num_or_name, wimfile);
2812 imagex_error(T("If you would like to set the boot "
2813 "index to 0, specify image \"0\" with "
2814 "the --boot flag."));
2816 goto out_wimlib_free;
2819 if (boot && info.image_count == 0) {
2820 imagex_error(T("--boot is meaningless on a WIM with no images"));
2821 goto out_wimlib_free;
2824 if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
2826 imagex_error(T("Cannot specify the --boot flag "
2827 "without specifying a specific "
2828 "image in a multi-image WIM"));
2829 goto out_wimlib_free;
2832 imagex_error(T("Cannot specify the NEW_NAME "
2833 "without specifying a specific "
2834 "image in a multi-image WIM"));
2835 goto out_wimlib_free;
2839 /* Operations that print information are separated from operations that
2840 * recreate the WIM file. */
2841 if (!new_name && !boot) {
2843 /* Read-only operations */
2845 if (image == WIMLIB_NO_IMAGE) {
2846 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
2847 image_num_or_name, wimfile);
2848 goto out_wimlib_free;
2851 if (image == WIMLIB_ALL_IMAGES && short_header)
2852 print_wim_information(wimfile, &info);
2855 wimlib_print_header(wim);
2858 if (info.total_parts != 1) {
2859 tfprintf(stderr, T("Warning: Only showing the lookup table "
2860 "for part %d of a %d-part WIM.\n"),
2861 info.part_number, info.total_parts);
2863 print_lookup_table(wim);
2867 ret = wimlib_extract_xml_data(wim, stdout);
2869 goto out_wimlib_free;
2875 fp = tfopen(xml_out_file, T("wb"));
2877 imagex_error_with_errno(T("Failed to open the "
2878 "file \"%"TS"\" for "
2882 goto out_wimlib_free;
2884 ret = wimlib_extract_xml_data(wim, fp);
2886 imagex_error(T("Failed to close the file "
2892 goto out_wimlib_free;
2896 wimlib_print_available_images(wim, image);
2899 ret = wimlib_print_metadata(wim, image);
2901 goto out_wimlib_free;
2906 /* Modification operations */
2908 if (image == WIMLIB_ALL_IMAGES)
2911 if (image == WIMLIB_NO_IMAGE && new_name) {
2912 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
2913 "when using image 0"), new_name);
2915 goto out_wimlib_free;
2919 if (image == info.boot_index) {
2920 imagex_printf(T("Image %d is already marked as "
2921 "bootable.\n"), image);
2924 imagex_printf(T("Marking image %d as bootable.\n"),
2926 info.boot_index = image;
2927 ret = wimlib_set_wim_info(wim, &info,
2928 WIMLIB_CHANGE_BOOT_INDEX);
2930 goto out_wimlib_free;
2934 if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
2936 imagex_printf(T("Image %d is already named \"%"TS"\".\n"),
2940 imagex_printf(T("Changing the name of image %d to "
2941 "\"%"TS"\".\n"), image, new_name);
2942 ret = wimlib_set_image_name(wim, image, new_name);
2944 goto out_wimlib_free;
2948 const tchar *old_desc;
2949 old_desc = wimlib_get_image_description(wim, image);
2950 if (old_desc && !tstrcmp(old_desc, new_desc)) {
2951 imagex_printf(T("The description of image %d is already "
2952 "\"%"TS"\".\n"), image, new_desc);
2955 imagex_printf(T("Changing the description of image %d "
2956 "to \"%"TS"\".\n"), image, new_desc);
2957 ret = wimlib_set_image_descripton(wim, image,
2960 goto out_wimlib_free;
2964 /* Only call wimlib_overwrite() if something actually needs to
2966 if (boot || new_name || new_desc ||
2967 (check && !info.has_integrity_table) ||
2968 (nocheck && info.has_integrity_table))
2970 int write_flags = 0;
2973 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2975 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2976 ret = wimlib_overwrite(wim, write_flags, 1,
2977 imagex_progress_func);
2979 imagex_printf(T("The file \"%"TS"\" was not modified "
2980 "because nothing needed to be done.\n"),
2991 usage(CMD_INFO, stderr);
2997 /* Join split WIMs into one part WIM */
2999 imagex_join(int argc, tchar **argv, int cmd)
3002 int swm_open_flags = 0;
3003 int wim_write_flags = 0;
3004 const tchar *output_path;
3007 for_opt(c, join_options) {
3009 case IMAGEX_CHECK_OPTION:
3010 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3011 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3021 imagex_error(T("Must specify one or more split WIM (.swm) "
3025 output_path = argv[0];
3026 ret = wimlib_join((const tchar * const *)++argv,
3031 imagex_progress_func);
3036 usage(CMD_JOIN, stderr);
3041 #if WIM_MOUNTING_SUPPORTED
3043 /* Mounts a WIM image. */
3045 imagex_mount_rw_or_ro(int argc, tchar **argv, int cmd)
3048 int mount_flags = 0;
3050 const tchar *staging_dir = NULL;
3051 const tchar *wimfile;
3054 struct wimlib_wim_info info;
3058 STRING_SET(refglobs);
3060 if (cmd == CMD_MOUNTRW) {
3061 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
3062 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3065 for_opt(c, mount_options) {
3067 case IMAGEX_ALLOW_OTHER_OPTION:
3068 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
3070 case IMAGEX_CHECK_OPTION:
3071 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3073 case IMAGEX_DEBUG_OPTION:
3074 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
3076 case IMAGEX_STREAMS_INTERFACE_OPTION:
3077 if (!tstrcasecmp(optarg, T("none")))
3078 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
3079 else if (!tstrcasecmp(optarg, T("xattr")))
3080 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
3081 else if (!tstrcasecmp(optarg, T("windows")))
3082 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
3084 imagex_error(T("Unknown stream interface \"%"TS"\""),
3089 case IMAGEX_REF_OPTION:
3090 ret = string_set_append(&refglobs, optarg);
3092 goto out_free_refglobs;
3094 case IMAGEX_STAGING_DIR_OPTION:
3095 staging_dir = optarg;
3097 case IMAGEX_UNIX_DATA_OPTION:
3098 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
3106 if (argc != 2 && argc != 3)
3111 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3113 goto out_free_refglobs;
3115 wimlib_get_wim_info(wim, &info);
3118 /* Image explicitly specified. */
3119 image = wimlib_resolve_image(wim, argv[1]);
3121 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
3125 /* No image specified; default to image 1, but only if the WIM
3126 * contains exactly one image. */
3128 if (info.image_count != 1) {
3129 imagex_error(T("\"%"TS"\" contains %d images; Please "
3130 "select one."), wimfile, info.image_count);
3138 if (refglobs.num_strings) {
3139 ret = wim_reference_globs(wim, &refglobs, open_flags);
3144 ret = wimlib_mount_image(wim, image, dir, mount_flags, staging_dir);
3146 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
3148 image, wimfile, dir);
3153 string_set_destroy(&refglobs);
3159 goto out_free_refglobs;
3161 #endif /* WIM_MOUNTING_SUPPORTED */
3163 /* Rebuild a WIM file */
3165 imagex_optimize(int argc, tchar **argv, int cmd)
3168 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3169 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
3172 const tchar *wimfile;
3175 unsigned num_threads = 0;
3177 for_opt(c, optimize_options) {
3179 case IMAGEX_CHECK_OPTION:
3180 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3181 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3183 case IMAGEX_NOCHECK_OPTION:
3184 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3186 case IMAGEX_RECOMPRESS_OPTION:
3187 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3189 case IMAGEX_THREADS_OPTION:
3190 num_threads = parse_num_threads(optarg);
3191 if (num_threads == UINT_MAX)
3194 case IMAGEX_PIPABLE_OPTION:
3195 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
3197 case IMAGEX_NOT_PIPABLE_OPTION:
3198 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
3212 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3216 old_size = file_get_size(wimfile);
3217 tprintf(T("\"%"TS"\" original size: "), wimfile);
3219 tputs(T("Unknown"));
3221 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3223 ret = wimlib_overwrite(wim, write_flags, num_threads,
3224 imagex_progress_func);
3226 imagex_error(T("Optimization of \"%"TS"\" failed."), wimfile);
3227 goto out_wimlib_free;
3230 new_size = file_get_size(wimfile);
3231 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3233 tputs(T("Unknown"));
3235 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3237 tfputs(T("Space saved: "), stdout);
3238 if (new_size != -1 && old_size != -1) {
3239 tprintf(T("%lld KiB\n"),
3240 ((long long)old_size - (long long)new_size) >> 10);
3242 tputs(T("Unknown"));
3251 usage(CMD_OPTIMIZE, stderr);
3257 /* Split a WIM into a spanned set */
3259 imagex_split(int argc, tchar **argv, int cmd)
3263 int write_flags = 0;
3264 unsigned long part_size;
3269 for_opt(c, split_options) {
3271 case IMAGEX_CHECK_OPTION:
3272 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3273 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3285 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3286 if (tmp == argv[2] || *tmp) {
3287 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3288 imagex_error(T("The part size must be an integer or "
3289 "floating-point number of megabytes."));
3292 ret = wimlib_open_wim(argv[0], open_flags, &wim, imagex_progress_func);
3296 ret = wimlib_split(wim, argv[1], part_size, write_flags, imagex_progress_func);
3302 usage(CMD_SPLIT, stderr);
3308 #if WIM_MOUNTING_SUPPORTED
3309 /* Unmounts a mounted WIM image. */
3311 imagex_unmount(int argc, tchar **argv, int cmd)
3314 int unmount_flags = 0;
3317 for_opt(c, unmount_options) {
3319 case IMAGEX_COMMIT_OPTION:
3320 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3322 case IMAGEX_CHECK_OPTION:
3323 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3325 case IMAGEX_REBUILD_OPTION:
3326 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3328 case IMAGEX_LAZY_OPTION:
3329 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3340 ret = wimlib_unmount_image(argv[0], unmount_flags,
3341 imagex_progress_func);
3343 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3348 usage(CMD_UNMOUNT, stderr);
3352 #endif /* WIM_MOUNTING_SUPPORTED */
3355 * Add, delete, or rename files in a WIM image.
3358 imagex_update(int argc, tchar **argv, int cmd)
3360 const tchar *wimfile;
3364 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3365 int write_flags = 0;
3366 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3367 int default_add_flags = WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
3368 int default_delete_flags = 0;
3369 unsigned num_threads = 0;
3371 tchar *cmd_file_contents;
3372 size_t cmd_file_nchars;
3373 struct wimlib_update_command *cmds;
3375 tchar *command_str = NULL;
3377 const tchar *config_file = NULL;
3379 struct wimlib_capture_config *config;
3381 for_opt(c, update_options) {
3383 /* Generic or write options */
3384 case IMAGEX_THREADS_OPTION:
3385 num_threads = parse_num_threads(optarg);
3386 if (num_threads == UINT_MAX)
3389 case IMAGEX_CHECK_OPTION:
3390 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3391 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3393 case IMAGEX_REBUILD_OPTION:
3394 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3396 case IMAGEX_COMMAND_OPTION:
3398 imagex_error(T("--command may only be specified "
3399 "one time. Please provide\n"
3400 " the update commands "
3401 "on standard input instead."));
3404 command_str = tstrdup(optarg);
3406 imagex_error(T("Out of memory!"));
3410 /* Default delete options */
3411 case IMAGEX_FORCE_OPTION:
3412 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3414 case IMAGEX_RECURSIVE_OPTION:
3415 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3418 /* Global add option */
3419 case IMAGEX_CONFIG_OPTION:
3420 default_add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
3421 config_file = optarg;
3424 /* Default add options */
3425 case IMAGEX_VERBOSE_OPTION:
3426 default_add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
3428 case IMAGEX_DEREFERENCE_OPTION:
3429 default_add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
3431 case IMAGEX_UNIX_DATA_OPTION:
3432 default_add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
3434 case IMAGEX_NO_ACLS_OPTION:
3435 default_add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
3437 case IMAGEX_STRICT_ACLS_OPTION:
3438 default_add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
3447 if (argc != 1 && argc != 2)
3451 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3453 goto out_free_command_str;
3456 /* Image explicitly specified. */
3457 image = wimlib_resolve_image(wim, argv[1]);
3458 ret = verify_image_exists_and_is_single(image, argv[1],
3461 goto out_wimlib_free;
3463 /* No image specified; default to image 1, but only if the WIM
3464 * contains exactly one image. */
3465 struct wimlib_wim_info info;
3467 wimlib_get_wim_info(wim, &info);
3468 if (info.image_count != 1) {
3469 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3470 wimfile, info.image_count);
3477 /* Parse capture configuration file if specified */
3481 config_str = file_get_text_contents(config_file, &config_len);
3484 goto out_wimlib_free;
3487 config = alloca(sizeof(*config));
3488 ret = parse_capture_config(&config_str, config_len, config);
3490 goto out_free_config;
3493 default_add_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
3496 /* Read update commands from standard input, or the command string if
3499 cmd_file_contents = NULL;
3500 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3503 if (isatty(STDIN_FILENO)) {
3504 tputs(T("Reading update commands from standard input..."));
3505 recommend_man_page(CMD_UPDATE, stdout);
3507 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3508 if (!cmd_file_contents) {
3510 goto out_free_config;
3513 /* Parse the update commands */
3514 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3519 goto out_free_cmd_file_contents;
3522 /* Set default flags and capture config on the update commands */
3523 for (size_t i = 0; i < num_cmds; i++) {
3524 switch (cmds[i].op) {
3525 case WIMLIB_UPDATE_OP_ADD:
3526 cmds[i].add.add_flags |= default_add_flags;
3527 cmds[i].add.config = config;
3529 case WIMLIB_UPDATE_OP_DELETE:
3530 cmds[i].delete_.delete_flags |= default_delete_flags;
3537 /* Execute the update commands */
3538 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3539 imagex_progress_func);
3543 /* Overwrite the updated WIM */
3544 ret = wimlib_overwrite(wim, write_flags, num_threads,
3545 imagex_progress_func);
3548 out_free_cmd_file_contents:
3549 free(cmd_file_contents);
3552 free(config->exclusion_pats.pats);
3553 free(config->exclusion_exception_pats.pats);
3558 out_free_command_str:
3563 usage(CMD_UPDATE, stderr);
3566 goto out_free_command_str;
3571 struct imagex_command {
3573 int (*func)(int argc, tchar **argv, int cmd);
3576 static const struct imagex_command imagex_commands[] = {
3577 [CMD_APPEND] = {T("append"), imagex_capture_or_append},
3578 [CMD_APPLY] = {T("apply"), imagex_apply},
3579 [CMD_CAPTURE] = {T("capture"), imagex_capture_or_append},
3580 [CMD_DELETE] = {T("delete"), imagex_delete},
3581 [CMD_DIR ] = {T("dir"), imagex_dir},
3582 [CMD_EXPORT] = {T("export"), imagex_export},
3583 [CMD_EXTRACT] = {T("extract"), imagex_extract},
3584 [CMD_INFO] = {T("info"), imagex_info},
3585 [CMD_JOIN] = {T("join"), imagex_join},
3586 #if WIM_MOUNTING_SUPPORTED
3587 [CMD_MOUNT] = {T("mount"), imagex_mount_rw_or_ro},
3588 [CMD_MOUNTRW] = {T("mountrw"), imagex_mount_rw_or_ro},
3590 [CMD_OPTIMIZE] = {T("optimize"), imagex_optimize},
3591 [CMD_SPLIT] = {T("split"), imagex_split},
3592 #if WIM_MOUNTING_SUPPORTED
3593 [CMD_UNMOUNT] = {T("unmount"), imagex_unmount},
3595 [CMD_UPDATE] = {T("update"), imagex_update},
3598 static const tchar *usage_strings[] = {
3601 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3602 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3603 " [--nocheck] [--flags EDITION_ID] [--verbose]\n"
3604 " [--dereference] [--config=FILE] [--threads=NUM_THREADS]\n"
3605 " [--rebuild] [--unix-data] [--source-list] [--no-acls]\n"
3606 " [--strict-acls] [--rpfix] [--norpfix] [--pipable]\n"
3607 " [--not-pipable] [--update-of=[WIMFILE:]IMAGE]\n"
3611 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME | all)]\n"
3612 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
3613 " [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n"
3614 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3615 " [--include-invalid-names]\n"
3619 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3620 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3621 " [--nocheck] [--compress=TYPE] [--flags EDITION_ID]\n"
3622 " [--verbose] [--dereference] [--config=FILE]\n"
3623 " [--threads=NUM_THREADS] [--unix-data] [--source-list]\n"
3624 " [--no-acls] [--strict-acls] [--norpfix] [--pipable]\n"
3625 " [--update-of=[WIMFILE:]IMAGE] [--delta-from=WIMFILE]\n"
3629 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n"
3634 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH]\n"
3638 " %"TS" SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
3639 " DEST_WIMFILE [DEST_IMAGE_NAME [DEST_IMAGE_DESCRIPTION]]\n"
3640 " [--boot] [--check] [--nocheck] [--compress=TYPE]\n"
3641 " [--ref=\"GLOB\"] [--threads=NUM_THREADS] [--rebuild]\n"
3642 " [--pipable] [--not-pipable]\n"
3646 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME) [PATH...]\n"
3647 " [--check] [--ref=\"GLOB\"] [--verbose] [--unix-data]\n"
3648 " [--no-acls] [--strict-acls] [--to-stdout]\n"
3649 " [--dest-dir=CMD_DIR] [--include-invalid-names]\n"
3653 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME) [NEW_NAME\n"
3654 " [NEW_DESC]]] [--boot] [--check] [--nocheck] [--header]\n"
3655 " [--lookup-table] [--xml] [--extract-xml FILE]\n"
3660 " %"TS" OUT_WIMFILE SPLIT_WIM_PART... [--check]\n"
3662 #if WIM_MOUNTING_SUPPORTED
3665 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3666 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
3667 " [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n"
3671 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3672 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
3673 " [--staging-dir=CMD_DIR] [--unix-data] [--allow-other]\n"
3678 " %"TS" WIMFILE [--check] [--nocheck] [--recompress]\n"
3679 " [--threads=NUM_THREADS] [--pipable] [--not-pipable]\n"
3683 " %"TS" WIMFILE SPLIT_WIM_PART_1 PART_SIZE_MB [--check]\n"
3685 #if WIM_MOUNTING_SUPPORTED
3688 " %"TS" DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
3693 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
3694 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
3695 " [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
3699 static const tchar *invocation_name;
3700 static int invocation_cmd = CMD_NONE;
3702 static const tchar *get_cmd_string(int cmd, bool nospace)
3704 static tchar buf[50];
3705 if (cmd == CMD_NONE) {
3706 tsprintf(buf, T("%"TS), T(IMAGEX_PROGNAME));
3707 } else if (invocation_cmd != CMD_NONE) {
3708 tsprintf(buf, T("wim%"TS), imagex_commands[cmd].name);
3710 const tchar *format;
3713 format = T("%"TS"-%"TS"");
3715 format = T("%"TS" %"TS"");
3716 tsprintf(buf, format, invocation_name, imagex_commands[cmd].name);
3724 static const tchar *s =
3726 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
3727 "Copyright (C) 2012, 2013 Eric Biggers\n"
3728 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
3729 "This is free software: you are free to change and redistribute it.\n"
3730 "There is NO WARRANTY, to the extent permitted by law.\n"
3732 "Report bugs to "PACKAGE_BUGREPORT".\n"
3739 help_or_version(int argc, tchar **argv, int cmd)
3744 for (i = 1; i < argc; i++) {
3746 if (p[0] == T('-') && p[1] == T('-')) {
3748 if (!tstrcmp(p, T("help"))) {
3749 if (cmd == CMD_NONE)
3754 } else if (!tstrcmp(p, T("version"))) {
3763 print_usage_string(int cmd, FILE *fp)
3765 tfprintf(fp, usage_strings[cmd], get_cmd_string(cmd, false));
3769 recommend_man_page(int cmd, FILE *fp)
3771 const tchar *format_str;
3773 format_str = T("See %"TS".pdf in the doc directory for more details.\n");
3775 format_str = T("Try `man %"TS"' for more details.\n");
3777 tfprintf(fp, format_str, get_cmd_string(cmd, true));
3781 usage(int cmd, FILE *fp)
3783 tfprintf(fp, T("Usage:\n"));
3784 print_usage_string(cmd, fp);
3785 tfprintf(fp, T("\n"));
3786 recommend_man_page(cmd, fp);
3792 tfprintf(fp, T("Usage:\n"));
3793 for (int cmd = 0; cmd < CMD_MAX; cmd++) {
3794 print_usage_string(cmd, fp);
3795 tfprintf(fp, T("\n"));
3797 static const tchar *extra =
3800 " %"TS" --version\n"
3802 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
3805 tfprintf(fp, extra, invocation_name, invocation_name);
3806 recommend_man_page(CMD_NONE, fp);
3809 /* Entry point for wimlib's ImageX implementation. On UNIX the command
3810 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
3811 * something else), while an Windows the command arguments will be UTF-16LE
3812 * encoded 'wchar_t' strings. */
3815 wmain(int argc, wchar_t **argv, wchar_t **envp)
3817 main(int argc, char **argv)
3824 imagex_info_file = stdout;
3825 invocation_name = tbasename(argv[0]);
3828 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
3829 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
3833 setlocale(LC_ALL, "");
3834 codeset = nl_langinfo(CODESET);
3835 if (!strstr(codeset, "UTF-8") &&
3836 !strstr(codeset, "UTF8") &&
3837 !strstr(codeset, "utf-8") &&
3838 !strstr(codeset, "utf8"))
3841 "WARNING: Running %"TS" in a UTF-8 locale is recommended!\n"
3842 " Maybe try: `export LANG=en_US.UTF-8'?\n"
3843 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
3844 " to any value to force wimlib to use UTF-8.\n",
3849 #endif /* !__WIN32__ */
3851 /* Allow being invoked as wimCOMMAND (e.g. wimapply). */
3853 if (!tstrncmp(invocation_name, T("wim"), 3) &&
3854 tstrcmp(invocation_name, T(IMAGEX_PROGNAME))) {
3855 for (int i = 0; i < CMD_MAX; i++) {
3856 if (!tstrcmp(invocation_name + 3,
3857 imagex_commands[i].name))
3866 /* Unless already known from the invocation name, determine which
3867 * command was specified. */
3868 if (cmd == CMD_NONE) {
3870 imagex_error(T("No command specified!\n"));
3874 for (int i = 0; i < CMD_MAX; i++) {
3875 if (!tstrcmp(argv[1], imagex_commands[i].name)) {
3880 if (cmd != CMD_NONE) {
3886 /* Handle --help and --version. --help can be either for the program as
3887 * a whole (cmd == CMD_NONE) or just for a specific command (cmd !=
3888 * CMD_NONE). Note: help_or_version() will not return if a --help or
3889 * --version argument was found. */
3890 help_or_version(argc, argv, cmd);
3892 /* Bail if a valid command was not specified. */
3893 if (cmd == CMD_NONE) {
3894 imagex_error(T("Unrecognized command: `%"TS"'\n"), argv[1]);
3899 /* Enable warning and error messages in wimlib be more user-friendly.
3901 wimlib_set_print_errors(true);
3903 /* Initialize wimlib. */
3904 ret = wimlib_global_init(init_flags);
3906 goto out_check_status;
3908 /* Call the command handler function. */
3909 ret = imagex_commands[cmd].func(argc, argv, cmd);
3911 /* Check for error writing to standard output, especially since for some
3912 * commands, writing to standard output is part of the program's actual
3913 * behavior and not just for informational purposes. */
3914 if (ferror(stdout) || fclose(stdout)) {
3915 imagex_error_with_errno(T("error writing to standard output"));
3920 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
3921 * itself (not by wimlib). 0 indicates success. > 0 indicates a wimlib
3922 * error code from which an error message can be printed. */
3924 imagex_error(T("Exiting with error code %d:\n"
3926 wimlib_get_error_string(ret));
3927 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
3928 imagex_error_with_errno(T("errno"));
3930 /* Make wimlib free any resources it's holding (although this is not
3931 * strictly necessary because the process is ending anyway). */
3932 wimlib_global_cleanup();