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_CHUNK_SIZE_OPTION,
124 IMAGEX_COMMAND_OPTION,
125 IMAGEX_COMMIT_OPTION,
126 IMAGEX_COMPRESS_OPTION,
127 IMAGEX_COMPRESS_SLOW_OPTION,
128 IMAGEX_CONFIG_OPTION,
130 IMAGEX_DELTA_FROM_OPTION,
131 IMAGEX_DEREFERENCE_OPTION,
132 IMAGEX_DEST_DIR_OPTION,
133 IMAGEX_EXTRACT_XML_OPTION,
136 IMAGEX_HARDLINK_OPTION,
137 IMAGEX_HEADER_OPTION,
138 IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
140 IMAGEX_LOOKUP_TABLE_OPTION,
141 IMAGEX_METADATA_OPTION,
142 IMAGEX_NORPFIX_OPTION,
143 IMAGEX_NOCHECK_OPTION,
144 IMAGEX_NO_ACLS_OPTION,
145 IMAGEX_NOT_PIPABLE_OPTION,
147 IMAGEX_PIPABLE_OPTION,
148 IMAGEX_REBUILD_OPTION,
149 IMAGEX_RECOMPRESS_OPTION,
150 IMAGEX_RECURSIVE_OPTION,
152 IMAGEX_RESUME_OPTION,
155 IMAGEX_SOURCE_LIST_OPTION,
156 IMAGEX_STAGING_DIR_OPTION,
157 IMAGEX_STREAMS_INTERFACE_OPTION,
158 IMAGEX_STRICT_ACLS_OPTION,
159 IMAGEX_SYMLINK_OPTION,
160 IMAGEX_THREADS_OPTION,
161 IMAGEX_TO_STDOUT_OPTION,
162 IMAGEX_UNIX_DATA_OPTION,
163 IMAGEX_UPDATE_OF_OPTION,
164 IMAGEX_VERBOSE_OPTION,
168 static const struct option apply_options[] = {
169 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
170 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
171 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
172 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
173 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
174 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
175 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
176 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
177 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
178 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
179 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
180 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
182 /* --resume is undocumented for now as it needs improvement. */
183 {T("resume"), no_argument, NULL, IMAGEX_RESUME_OPTION},
187 static const struct option capture_or_append_options[] = {
188 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
189 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
190 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
191 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
192 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
193 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
194 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
195 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
196 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
197 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
198 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
199 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
200 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
201 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
202 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
203 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
204 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
205 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
206 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
207 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
208 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
209 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
210 {T("update-of"), required_argument, NULL, IMAGEX_UPDATE_OF_OPTION},
211 {T("delta-from"), required_argument, NULL, IMAGEX_DELTA_FROM_OPTION},
215 static const struct option delete_options[] = {
216 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
217 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
221 static const struct option dir_options[] = {
222 {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
226 static const struct option export_options[] = {
227 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
228 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
229 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
230 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
231 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
232 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
233 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
234 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
235 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
236 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
240 static const struct option extract_options[] = {
241 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
242 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
243 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
244 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
245 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
246 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
247 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
248 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
249 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
250 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
254 static const struct option info_options[] = {
255 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
256 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
257 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
258 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
259 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
260 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
261 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
262 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
263 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
267 static const struct option join_options[] = {
268 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
272 static const struct option mount_options[] = {
273 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
274 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
275 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
276 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
277 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
278 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
279 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
283 static const struct option optimize_options[] = {
284 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
285 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
286 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
287 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
288 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
289 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
290 {T("recompress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
291 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
292 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
293 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
294 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
298 static const struct option split_options[] = {
299 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
303 static const struct option unmount_options[] = {
304 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
305 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
306 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
307 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
311 static const struct option update_options[] = {
312 /* Careful: some of the options here set the defaults for update
313 * commands, but the flags given to an actual update command (and not to
314 * `imagex update' itself are also handled in
315 * update_command_add_option(). */
316 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
317 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
318 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
319 {T("command"), required_argument, NULL, IMAGEX_COMMAND_OPTION},
321 /* Default delete options */
322 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
323 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
325 /* Global add option */
326 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
328 /* Default add options */
329 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
330 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
331 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
332 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
333 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
334 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
340 # define _format_attribute(type, format_str, args_start) \
341 __attribute__((format(type, format_str, args_start)))
343 # define _format_attribute(type, format_str, args_start)
346 /* Print formatted error message to stderr. */
347 static void _format_attribute(printf, 1, 2)
348 imagex_error(const tchar *format, ...)
351 va_start(va, format);
352 tfputs(T("ERROR: "), stderr);
353 tvfprintf(stderr, format, va);
354 tputc(T('\n'), stderr);
358 /* Print formatted error message to stderr. */
359 static void _format_attribute(printf, 1, 2)
360 imagex_error_with_errno(const tchar *format, ...)
362 int errno_save = errno;
364 va_start(va, format);
365 tfputs(T("ERROR: "), stderr);
366 tvfprintf(stderr, format, va);
367 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
372 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
374 if (image == WIMLIB_NO_IMAGE) {
375 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
376 " Please specify a 1-based image index or "
377 "image name. To list the images\n"
378 " contained in the WIM archive, run\n"
380 " %"TS" \"%"TS"\"\n"),
381 image_name, wim_name,
382 get_cmd_string(CMD_INFO, false), wim_name);
383 return WIMLIB_ERR_INVALID_IMAGE;
389 verify_image_is_single(int image)
391 if (image == WIMLIB_ALL_IMAGES) {
392 imagex_error(T("Cannot specify all images for this action!"));
393 return WIMLIB_ERR_INVALID_IMAGE;
399 verify_image_exists_and_is_single(int image, const tchar *image_name,
400 const tchar *wim_name)
403 ret = verify_image_exists(image, image_name, wim_name);
405 ret = verify_image_is_single(image);
409 /* Parse the argument to --compress */
411 get_compression_type(const tchar *optarg)
413 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
414 return WIMLIB_COMPRESSION_TYPE_LZX;
415 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
416 return WIMLIB_COMPRESSION_TYPE_XPRESS;
417 else if (!tstrcasecmp(optarg, T("none")))
418 return WIMLIB_COMPRESSION_TYPE_NONE;
420 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
421 "\"maximum\", \"fast\", or \"none\"."), optarg);
422 return WIMLIB_COMPRESSION_TYPE_INVALID;
427 set_compress_slow(void)
430 static const struct wimlib_lzx_params slow_params = {
431 .size_of_this = sizeof(struct wimlib_lzx_params),
432 .algorithm = WIMLIB_LZX_ALGORITHM_SLOW,
435 .use_len2_matches = 1,
436 .num_fast_bytes = 96,
437 .num_optim_passes = 4,
438 .max_search_depth = 100,
439 .max_matches_per_pos = 10,
440 .main_nostat_cost = 15,
441 .len_nostat_cost = 15,
442 .aligned_nostat_cost = 7,
446 ret = wimlib_lzx_set_default_params(&slow_params);
448 imagex_error(T("Couldn't set slow compression parameters.!"));
453 const tchar **strings;
454 unsigned num_strings;
455 unsigned num_alloc_strings;
458 #define STRING_SET_INITIALIZER \
459 { .strings = NULL, .num_strings = 0, .num_alloc_strings = 0, }
461 #define STRING_SET(_strings) \
462 struct string_set _strings = STRING_SET_INITIALIZER
465 string_set_append(struct string_set *set, const tchar *glob)
467 unsigned num_alloc_strings = set->num_alloc_strings;
469 if (set->num_strings == num_alloc_strings) {
470 const tchar **new_strings;
472 num_alloc_strings += 4;
473 new_strings = realloc(set->strings,
474 sizeof(set->strings[0]) * num_alloc_strings);
476 imagex_error(T("Out of memory!"));
479 set->strings = new_strings;
480 set->num_alloc_strings = num_alloc_strings;
482 set->strings[set->num_strings++] = glob;
487 string_set_destroy(struct string_set *set)
493 wim_reference_globs(WIMStruct *wim, struct string_set *set, int open_flags)
495 return wimlib_reference_resource_files(wim, set->strings,
497 WIMLIB_REF_FLAG_GLOB_ENABLE,
499 imagex_progress_func);
503 do_resource_not_found_warning(const tchar *wimfile,
504 const struct wimlib_wim_info *info,
505 const struct string_set *refglobs)
507 if (info->total_parts > 1) {
508 if (refglobs->num_strings == 0) {
509 imagex_error(T("\"%"TS"\" is part of a split WIM. "
510 "Use --ref to specify the other parts."),
513 imagex_error(T("Perhaps the '--ref' argument did not "
514 "specify all other parts of the split "
518 imagex_error(T("If this is a delta WIM, use the --ref argument "
519 "to specify the WIM(s) on which it is based."));
523 /* Returns the size of a file given its name, or -1 if the file does not exist
524 * or its size cannot be determined. */
526 file_get_size(const tchar *filename)
529 if (tstat(filename, &st) == 0)
536 PARSE_STRING_SUCCESS = 0,
537 PARSE_STRING_FAILURE = 1,
538 PARSE_STRING_NONE = 2,
542 * Parses a string token from an array of characters.
544 * Tokens are either whitespace-delimited, or double or single-quoted.
546 * @line_p: Pointer to the pointer to the line of data. Will be updated
547 * to point past the string token iff the return value is
548 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
551 * @len_p: @len_p initially stores the length of the line of data, which may
552 * be 0, and it will be updated to the number of bytes remaining in
553 * the line iff the return value is PARSE_STRING_SUCCESS.
555 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
556 * parsed string token will be returned here.
558 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
559 * PARSE_STRING_FAILURE if the data was invalid due to a missing
560 * closing quote; or PARSE_STRING_NONE if the line ended before the
561 * beginning of a string token was found.
564 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
567 tchar *line = *line_p;
571 /* Skip leading whitespace */
574 return PARSE_STRING_NONE;
575 if (!istspace(*line) && *line != T('\0'))
581 if (quote_char == T('"') || quote_char == T('\'')) {
586 line = tmemchr(line, quote_char, len);
588 imagex_error(T("Missing closing quote: %"TS), fn - 1);
589 return PARSE_STRING_FAILURE;
592 /* Unquoted string. Go until whitespace. Line is terminated
593 * by '\0', so no need to check 'len'. */
597 } while (!istspace(*line) && *line != T('\0'));
604 return PARSE_STRING_SUCCESS;
607 /* Parses a line of data (not an empty line or comment) in the source list file
608 * format. (See the man page for 'wimlib-imagex capture' for details on this
609 * format and the meaning.)
611 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
612 * len == 0. The data in @line will be modified by this function call.
614 * @len: Length of the line of data.
616 * @source: On success, the capture source and target described by the line is
617 * written into this destination. Note that it will contain pointers
618 * to data in the @line array.
620 * Returns true if the line was valid; false otherwise. */
622 parse_source_list_line(tchar *line, size_t len,
623 struct wimlib_capture_source *source)
627 ret = parse_string(&line, &len, &source->fs_source_path);
628 if (ret != PARSE_STRING_SUCCESS)
630 ret = parse_string(&line, &len, &source->wim_target_path);
631 if (ret == PARSE_STRING_NONE)
632 source->wim_target_path = source->fs_source_path;
633 return ret != PARSE_STRING_FAILURE;
636 /* Returns %true if the given line of length @len > 0 is a comment or empty line
637 * in the source list file format. */
639 is_comment_line(const tchar *line, size_t len)
644 if (!istspace(*line) && *line != T('\0'))
654 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
657 tchar *contents = *contents_p;
658 size_t nchars = *nchars_p;
661 for (i = 0; i < nchars; i++)
662 if (contents[i] == T('\n'))
665 /* Handle last line not terminated by a newline */
666 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
667 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
669 imagex_error(T("Out of memory!"));
672 contents[nchars] = T('\n');
673 *contents_p = contents;
681 /* Parses a file in the source list format. (See the man page for
682 * 'wimlib-imagex capture' for details on this format and the meaning.)
684 * @source_list_contents: Contents of the source list file. Note that this
685 * buffer will be modified to save memory allocations,
686 * and cannot be freed until the returned array of
687 * wimlib_capture_source's has also been freed.
689 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
692 * @nsources_ret: On success, the length of the returned array is
695 * Returns: An array of `struct wimlib_capture_source's that can be passed to
696 * the wimlib_add_image_multisource() function to specify how a WIM image is to
698 static struct wimlib_capture_source *
699 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
700 size_t *nsources_ret)
704 struct wimlib_capture_source *sources;
707 nlines = text_file_count_lines(source_list_contents_p,
708 &source_list_nchars);
712 /* Always allocate at least 1 slot, just in case the implementation of
713 * calloc() returns NULL if 0 bytes are requested. */
714 sources = calloc(nlines ?: 1, sizeof(*sources));
716 imagex_error(T("out of memory"));
719 p = *source_list_contents_p;
721 for (i = 0; i < nlines; i++) {
722 /* XXX: Could use rawmemchr() here instead, but it may not be
723 * available on all platforms. */
724 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
725 size_t len = endp - p + 1;
727 if (!is_comment_line(p, len)) {
728 if (!parse_source_list_line(p, len, &sources[j++])) {
741 enum capture_config_section {
742 CAPTURE_CONFIG_NO_SECTION,
743 CAPTURE_CONFIG_EXCLUSION_SECTION,
744 CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
745 CAPTURE_CONFIG_IGNORE_SECTION,
749 CAPTURE_CONFIG_INVALID_SECTION,
750 CAPTURE_CONFIG_CHANGED_SECTION,
751 CAPTURE_CONFIG_SAME_SECTION,
755 check_config_section(tchar *line, size_t len,
756 enum capture_config_section *cur_section)
758 while (istspace(*line))
762 return CAPTURE_CONFIG_SAME_SECTION;
765 tchar *endbrace = tstrrchr(line, T(']'));
767 return CAPTURE_CONFIG_SAME_SECTION;
769 if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
770 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
771 } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
772 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
773 } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
774 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
775 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
776 "of capture config file\n"),
778 } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
779 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
780 tfputs(T("WARNING: Ignoring [AlignmentList] section "
781 "of capture config file\n"),
784 imagex_error(T("Invalid capture config file section \"%"TS"\""),
786 return CAPTURE_CONFIG_INVALID_SECTION;
788 return CAPTURE_CONFIG_CHANGED_SECTION;
793 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
796 if (pat_list->num_pats == pat_list->num_allocated_pats) {
798 size_t num_allocated_pats = pat_list->num_pats + 8;
800 pats = realloc(pat_list->pats,
801 num_allocated_pats * sizeof(pat_list->pats[0]));
803 imagex_error(T("Out of memory!"));
806 pat_list->pats = pats;
807 pat_list->num_allocated_pats = num_allocated_pats;
809 pat_list->pats[pat_list->num_pats++] = pat;
814 parse_capture_config_line(tchar *line, size_t len,
815 enum capture_config_section *cur_section,
816 struct wimlib_capture_config *config)
821 ret = check_config_section(line, len, cur_section);
822 if (ret == CAPTURE_CONFIG_INVALID_SECTION)
824 if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
827 switch (*cur_section) {
828 case CAPTURE_CONFIG_NO_SECTION:
829 imagex_error(T("Line \"%"TS"\" is not in a section "
830 "(such as [ExclusionList]"), line);
832 case CAPTURE_CONFIG_EXCLUSION_SECTION:
833 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
835 return pattern_list_add_pattern(&config->exclusion_pats,
837 case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
838 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
840 return pattern_list_add_pattern(&config->exclusion_exception_pats,
842 case CAPTURE_CONFIG_IGNORE_SECTION:
849 parse_capture_config(tchar **contents_p, size_t nchars,
850 struct wimlib_capture_config *config)
855 enum capture_config_section cur_section;
857 memset(config, 0, sizeof(*config));
859 nlines = text_file_count_lines(contents_p, &nchars);
863 cur_section = CAPTURE_CONFIG_NO_SECTION;
865 for (i = 0; i < nlines; i++) {
866 tchar *endp = tmemchr(p, T('\n'), nchars);
867 size_t len = endp - p + 1;
869 if (!is_comment_line(p, len))
870 if (!parse_capture_config_line(p, len, &cur_section, config))
878 /* Reads the contents of a file into memory. */
880 file_get_contents(const tchar *filename, size_t *len_ret)
887 if (tstat(filename, &stbuf) != 0) {
888 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
893 fp = tfopen(filename, T("rb"));
895 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
899 buf = malloc(len ? len : 1);
901 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
902 "contents of file \"%"TS"\""), len, filename);
905 if (fread(buf, 1, len, fp) != len) {
906 imagex_error_with_errno(T("Failed to read %zu bytes from the "
907 "file \"%"TS"\""), len, filename);
921 /* Read standard input until EOF and return the full contents in a malloc()ed
922 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
925 stdin_get_contents(size_t *len_ret)
927 /* stdin can, of course, be a pipe or other non-seekable file, so the
928 * total length of the data cannot be pre-determined */
930 size_t newlen = 1024;
934 char *p = realloc(buf, newlen);
935 size_t bytes_read, bytes_to_read;
937 imagex_error(T("out of memory while reading stdin"));
941 bytes_to_read = newlen - pos;
942 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
944 if (bytes_read != bytes_to_read) {
949 imagex_error_with_errno(T("error reading stdin"));
963 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
966 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
968 *num_tchars_ret = num_bytes;
970 #else /* !__WIN32__ */
971 /* On Windows, translate the text to UTF-16LE */
975 if (num_bytes >= 2 &&
976 (((unsigned char)text[0] == 0xff && (unsigned char)text[1] == 0xfe) ||
977 ((unsigned char)text[0] <= 0x7f && (unsigned char)text[1] == 0x00)))
979 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
980 * with something that looks like an ASCII character encoded as
981 * a UTF-16LE code unit. Assume the file is encoded as
982 * UTF-16LE. This is not a 100% reliable check. */
983 num_wchars = num_bytes / 2;
984 text_wstr = (wchar_t*)text;
986 /* File does not look like UTF-16LE. Assume it is encoded in
987 * the current Windows code page. I think these are always
988 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
989 * should work as expected. */
990 text_wstr = win32_mbs_to_wcs(text,
995 *num_tchars_ret = num_wchars;
997 #endif /* __WIN32__ */
1001 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
1006 contents = file_get_contents(filename, &num_bytes);
1009 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
1013 stdin_get_text_contents(size_t *num_tchars_ret)
1018 contents = stdin_get_contents(&num_bytes);
1021 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
1024 #define TO_PERCENT(numerator, denominator) \
1025 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
1027 /* Given an enumerated value for WIM compression type, return a descriptive
1029 static const tchar *
1030 get_data_type(int ctype)
1033 case WIMLIB_COMPRESSION_TYPE_NONE:
1034 return T("uncompressed");
1035 case WIMLIB_COMPRESSION_TYPE_LZX:
1036 return T("LZX-compressed");
1037 case WIMLIB_COMPRESSION_TYPE_XPRESS:
1038 return T("XPRESS-compressed");
1043 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
1044 #define MEBIBYTE_MIN_NBYTES 10000000ULL
1045 #define KIBIBYTE_MIN_NBYTES 10000ULL
1048 get_unit(uint64_t total_bytes, const tchar **name_ret)
1050 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
1051 *name_ret = T("GiB");
1053 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
1054 *name_ret = T("MiB");
1056 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
1057 *name_ret = T("KiB");
1060 *name_ret = T("bytes");
1065 /* Progress callback function passed to various wimlib functions. */
1067 imagex_progress_func(enum wimlib_progress_msg msg,
1068 const union wimlib_progress_info *info)
1070 unsigned percent_done;
1071 unsigned unit_shift;
1072 const tchar *unit_name;
1073 if (imagex_be_quiet)
1076 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1077 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1078 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1079 info->write_streams.total_bytes);
1081 if (info->write_streams.completed_streams == 0) {
1082 const tchar *data_type;
1084 data_type = get_data_type(info->write_streams.compression_type);
1085 imagex_printf(T("Writing %"TS" data using %u thread%"TS"\n"),
1086 data_type, info->write_streams.num_threads,
1087 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1089 if (info->write_streams.total_parts <= 1) {
1090 imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1091 "written (%u%% done)"),
1092 info->write_streams.completed_bytes >> unit_shift,
1094 info->write_streams.total_bytes >> unit_shift,
1098 imagex_printf(T("\rWriting resources from part %u of %u: "
1099 "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written"),
1100 (info->write_streams.completed_parts ==
1101 info->write_streams.total_parts) ?
1102 info->write_streams.completed_parts :
1103 info->write_streams.completed_parts + 1,
1104 info->write_streams.total_parts,
1105 info->write_streams.completed_bytes >> unit_shift,
1107 info->write_streams.total_bytes >> unit_shift,
1111 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1112 imagex_printf(T("\n"));
1114 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1115 imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
1116 if (*info->scan.wim_target_path) {
1117 imagex_printf(T(" (loading as WIM path: "
1118 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
1119 info->scan.wim_target_path);
1121 imagex_printf(T(" (loading as root of WIM image)...\n"));
1124 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1125 switch (info->scan.status) {
1126 case WIMLIB_SCAN_DENTRY_OK:
1127 imagex_printf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
1129 case WIMLIB_SCAN_DENTRY_EXCLUDED:
1130 imagex_printf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1132 case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
1133 imagex_printf(T("WARNING: Excluding unsupported file or directory\n"
1134 " \"%"TS"\" from capture\n"), info->scan.cur_path);
1138 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1139 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1140 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1141 info->integrity.total_bytes);
1142 imagex_printf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1143 "of %"PRIu64" %"TS" (%u%%) done"),
1144 info->integrity.filename,
1145 info->integrity.completed_bytes >> unit_shift,
1147 info->integrity.total_bytes >> unit_shift,
1150 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1151 imagex_printf(T("\n"));
1153 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1154 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1155 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1156 info->integrity.total_bytes);
1157 imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1158 "of %"PRIu64" %"TS" (%u%%) done"),
1159 info->integrity.completed_bytes >> unit_shift,
1161 info->integrity.total_bytes >> unit_shift,
1164 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1165 imagex_printf(T("\n"));
1167 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1168 imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1169 "to %"TS" \"%"TS"\"\n"),
1170 info->extract.image,
1171 info->extract.image_name,
1172 info->extract.wimfile_name,
1173 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1174 T("NTFS volume") : T("directory")),
1175 info->extract.target);
1177 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1178 imagex_printf(T("Extracting "
1179 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\" from image %d (\"%"TS"\") "
1180 "in \"%"TS"\" to \"%"TS"\"\n"),
1181 info->extract.extract_root_wim_source_path,
1182 info->extract.image,
1183 info->extract.image_name,
1184 info->extract.wimfile_name,
1185 info->extract.target);
1187 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1188 percent_done = TO_PERCENT(info->extract.completed_bytes,
1189 info->extract.total_bytes);
1190 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1191 imagex_printf(T("\rExtracting files: "
1192 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1193 info->extract.completed_bytes >> unit_shift,
1195 info->extract.total_bytes >> unit_shift,
1198 if (info->extract.completed_bytes >= info->extract.total_bytes)
1199 imagex_printf(T("\n"));
1201 case WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN:
1202 if (info->extract.total_parts != 1) {
1203 imagex_printf(T("\nReading split pipable WIM part %u of %u\n"),
1204 info->extract.part_number,
1205 info->extract.total_parts);
1208 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1209 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1210 imagex_printf(T("Setting timestamps on all extracted files...\n"));
1212 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1213 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1214 imagex_printf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1215 info->extract.target);
1218 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1219 percent_done = TO_PERCENT(info->split.completed_bytes,
1220 info->split.total_bytes);
1221 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1222 imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
1223 "%"PRIu64" %"TS" (%u%%) written\n"),
1224 info->split.part_name,
1225 info->split.cur_part_number,
1226 info->split.total_parts,
1227 info->split.completed_bytes >> unit_shift,
1229 info->split.total_bytes >> unit_shift,
1233 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1234 if (info->split.completed_bytes == info->split.total_bytes) {
1235 imagex_printf(T("Finished writing split WIM part %u of %u\n"),
1236 info->split.cur_part_number,
1237 info->split.total_parts);
1240 case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1241 switch (info->update.command->op) {
1242 case WIMLIB_UPDATE_OP_DELETE:
1243 imagex_printf(T("Deleted WIM path "
1244 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1245 info->update.command->delete_.wim_path);
1247 case WIMLIB_UPDATE_OP_RENAME:
1248 imagex_printf(T("Renamed WIM path "
1249 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
1250 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1251 info->update.command->rename.wim_source_path,
1252 info->update.command->rename.wim_target_path);
1254 case WIMLIB_UPDATE_OP_ADD:
1262 fflush(imagex_info_file);
1267 parse_num_threads(const tchar *optarg)
1270 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1271 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1272 imagex_error(T("Number of threads must be a non-negative integer!"));
1279 static uint32_t parse_chunk_size(const char *optarg)
1282 unsigned long chunk_size = strtoul(optarg, &tmp, 10);
1283 if (chunk_size >= UINT32_MAX || *tmp || tmp == optarg) {
1284 imagex_error(T("Chunk size must be a non-negative integer!"));
1293 * Parse an option passed to an update command.
1295 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1298 * @option: Text string for the option (beginning with --)
1300 * @cmd: `struct wimlib_update_command' that is being constructed for
1303 * Returns true if the option was recognized; false if not.
1306 update_command_add_option(int op, const tchar *option,
1307 struct wimlib_update_command *cmd)
1309 bool recognized = true;
1311 case WIMLIB_UPDATE_OP_ADD:
1312 if (!tstrcmp(option, T("--verbose")))
1313 cmd->add.add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
1314 else if (!tstrcmp(option, T("--unix-data")))
1315 cmd->add.add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
1316 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1317 cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
1318 else if (!tstrcmp(option, T("--strict-acls")))
1319 cmd->add.add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
1320 else if (!tstrcmp(option, T("--dereference")))
1321 cmd->add.add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
1325 case WIMLIB_UPDATE_OP_DELETE:
1326 if (!tstrcmp(option, T("--force")))
1327 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1328 else if (!tstrcmp(option, T("--recursive")))
1329 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1340 /* How many nonoption arguments each `imagex update' command expects */
1341 static const unsigned update_command_num_nonoptions[] = {
1342 [WIMLIB_UPDATE_OP_ADD] = 2,
1343 [WIMLIB_UPDATE_OP_DELETE] = 1,
1344 [WIMLIB_UPDATE_OP_RENAME] = 2,
1348 update_command_add_nonoption(int op, const tchar *nonoption,
1349 struct wimlib_update_command *cmd,
1350 unsigned num_nonoptions)
1353 case WIMLIB_UPDATE_OP_ADD:
1354 if (num_nonoptions == 0)
1355 cmd->add.fs_source_path = (tchar*)nonoption;
1357 cmd->add.wim_target_path = (tchar*)nonoption;
1359 case WIMLIB_UPDATE_OP_DELETE:
1360 cmd->delete_.wim_path = (tchar*)nonoption;
1362 case WIMLIB_UPDATE_OP_RENAME:
1363 if (num_nonoptions == 0)
1364 cmd->rename.wim_source_path = (tchar*)nonoption;
1366 cmd->rename.wim_target_path = (tchar*)nonoption;
1372 * Parse a command passed on stdin to `imagex update'.
1374 * @line: Text of the command.
1375 * @len: Length of the line, including a null terminator
1378 * @command: A `struct wimlib_update_command' to fill in from the parsed
1381 * @line_number: Line number of the command, for diagnostics.
1383 * Returns true on success; returns false on parse error.
1386 parse_update_command(tchar *line, size_t len,
1387 struct wimlib_update_command *command,
1391 tchar *command_name;
1393 size_t num_nonoptions;
1395 /* Get the command name ("add", "delete", "rename") */
1396 ret = parse_string(&line, &len, &command_name);
1397 if (ret != PARSE_STRING_SUCCESS)
1400 if (!tstrcasecmp(command_name, T("add"))) {
1401 op = WIMLIB_UPDATE_OP_ADD;
1402 } else if (!tstrcasecmp(command_name, T("delete"))) {
1403 op = WIMLIB_UPDATE_OP_DELETE;
1404 } else if (!tstrcasecmp(command_name, T("rename"))) {
1405 op = WIMLIB_UPDATE_OP_RENAME;
1407 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1408 command_name, line_number);
1413 /* Parse additional options and non-options as needed */
1418 ret = parse_string(&line, &len, &next_string);
1419 if (ret == PARSE_STRING_NONE) /* End of line */
1421 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1423 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1425 if (!update_command_add_option(op, next_string, command))
1427 imagex_error(T("Unrecognized option \"%"TS"\" to "
1428 "update command \"%"TS"\" on line %zu"),
1429 next_string, command_name, line_number);
1435 if (num_nonoptions == update_command_num_nonoptions[op])
1437 imagex_error(T("Unexpected argument \"%"TS"\" in "
1438 "update command on line %zu\n"
1439 " (The \"%"TS"\" command only "
1440 "takes %zu nonoption arguments!)\n"),
1441 next_string, line_number,
1442 command_name, num_nonoptions);
1445 update_command_add_nonoption(op, next_string,
1446 command, num_nonoptions);
1451 if (num_nonoptions != update_command_num_nonoptions[op]) {
1452 imagex_error(T("Not enough arguments to update command "
1453 "\"%"TS"\" on line %zu"), command_name, line_number);
1459 static struct wimlib_update_command *
1460 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1461 size_t *num_cmds_ret)
1465 struct wimlib_update_command *cmds;
1468 nlines = text_file_count_lines(cmd_file_contents_p,
1473 /* Always allocate at least 1 slot, just in case the implementation of
1474 * calloc() returns NULL if 0 bytes are requested. */
1475 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1477 imagex_error(T("out of memory"));
1480 p = *cmd_file_contents_p;
1482 for (i = 0; i < nlines; i++) {
1483 /* XXX: Could use rawmemchr() here instead, but it may not be
1484 * available on all platforms. */
1485 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1486 size_t len = endp - p + 1;
1488 if (!is_comment_line(p, len)) {
1489 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1500 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1501 * one image from a WIM file to a NTFS volume. */
1503 imagex_apply(int argc, tchar **argv, int cmd)
1507 int image = WIMLIB_NO_IMAGE;
1509 struct wimlib_wim_info info;
1511 const tchar *wimfile;
1512 const tchar *target;
1513 const tchar *image_num_or_name = NULL;
1514 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1516 STRING_SET(refglobs);
1518 for_opt(c, apply_options) {
1520 case IMAGEX_CHECK_OPTION:
1521 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1523 case IMAGEX_HARDLINK_OPTION:
1524 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1526 case IMAGEX_SYMLINK_OPTION:
1527 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1529 case IMAGEX_VERBOSE_OPTION:
1530 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
1532 case IMAGEX_REF_OPTION:
1533 ret = string_set_append(&refglobs, optarg);
1535 goto out_free_refglobs;
1537 case IMAGEX_UNIX_DATA_OPTION:
1538 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1540 case IMAGEX_NO_ACLS_OPTION:
1541 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1543 case IMAGEX_STRICT_ACLS_OPTION:
1544 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1546 case IMAGEX_NORPFIX_OPTION:
1547 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1549 case IMAGEX_RPFIX_OPTION:
1550 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1552 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1553 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1554 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1556 case IMAGEX_RESUME_OPTION:
1557 extract_flags |= WIMLIB_EXTRACT_FLAG_RESUME;
1565 if (argc != 2 && argc != 3)
1570 if (!tstrcmp(wimfile, T("-"))) {
1571 /* Attempt to apply pipable WIM from standard input. */
1573 image_num_or_name = NULL;
1576 image_num_or_name = argv[1];
1581 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1582 imagex_progress_func);
1584 goto out_free_refglobs;
1586 wimlib_get_wim_info(wim, &info);
1589 /* Image explicitly specified. */
1590 image_num_or_name = argv[1];
1591 image = wimlib_resolve_image(wim, image_num_or_name);
1592 ret = verify_image_exists(image, image_num_or_name, wimfile);
1594 goto out_wimlib_free;
1597 /* No image specified; default to image 1, but only if the WIM
1598 * contains exactly one image. */
1600 if (info.image_count != 1) {
1601 imagex_error(T("\"%"TS"\" contains %d images; "
1602 "Please select one (or all)."),
1603 wimfile, info.image_count);
1612 if (refglobs.num_strings) {
1614 imagex_error(T("Can't specify --ref when applying from stdin!"));
1616 goto out_wimlib_free;
1618 ret = wim_reference_globs(wim, &refglobs, open_flags);
1620 goto out_wimlib_free;
1625 /* Interpret a regular file or block device target as a NTFS
1629 if (tstat(target, &stbuf)) {
1630 if (errno != ENOENT) {
1631 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1634 goto out_wimlib_free;
1637 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1638 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1644 ret = wimlib_extract_image(wim, image, target, extract_flags,
1645 imagex_progress_func);
1647 set_fd_to_binary_mode(STDIN_FILENO);
1648 ret = wimlib_extract_image_from_pipe(STDIN_FILENO,
1650 target, extract_flags,
1651 imagex_progress_func);
1654 imagex_printf(T("Done applying WIM image.\n"));
1655 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
1657 do_resource_not_found_warning(wimfile, &info, &refglobs);
1659 imagex_error(T( "If you are applying an image "
1660 "from a split pipable WIM,\n"
1661 " make sure you have "
1662 "concatenated together all parts."));
1668 string_set_destroy(&refglobs);
1672 usage(CMD_APPLY, stderr);
1674 goto out_free_refglobs;
1677 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1678 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1679 * the desired image. 'wimlib-imagex append': add a new image to an existing
1682 imagex_capture_or_append(int argc, tchar **argv, int cmd)
1685 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1686 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE |
1687 WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1688 int write_flags = 0;
1689 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
1690 uint32_t chunk_size = UINT32_MAX;
1691 const tchar *wimfile;
1695 const tchar *flags_element = NULL;
1698 STRING_SET(base_wimfiles);
1699 WIMStruct **base_wims;
1701 WIMStruct *template_wim;
1702 const tchar *template_wimfile = NULL;
1703 const tchar *template_image_name_or_num = NULL;
1704 int template_image = WIMLIB_NO_IMAGE;
1707 unsigned num_threads = 0;
1712 const tchar *config_file = NULL;
1714 struct wimlib_capture_config *config;
1716 bool source_list = false;
1717 size_t source_list_nchars = 0;
1718 tchar *source_list_contents;
1719 bool capture_sources_malloced;
1720 struct wimlib_capture_source *capture_sources;
1722 bool name_defaulted;
1724 for_opt(c, capture_or_append_options) {
1726 case IMAGEX_BOOT_OPTION:
1727 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1729 case IMAGEX_CHECK_OPTION:
1730 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1731 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1733 case IMAGEX_NOCHECK_OPTION:
1734 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
1736 case IMAGEX_CONFIG_OPTION:
1737 config_file = optarg;
1738 add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1740 case IMAGEX_COMPRESS_OPTION:
1741 compression_type = get_compression_type(optarg);
1742 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1745 case IMAGEX_COMPRESS_SLOW_OPTION:
1746 ret = set_compress_slow();
1749 compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1751 case IMAGEX_CHUNK_SIZE_OPTION:
1752 chunk_size = parse_chunk_size(optarg);
1753 if (chunk_size == UINT32_MAX)
1756 case IMAGEX_FLAGS_OPTION:
1757 flags_element = optarg;
1759 case IMAGEX_DEREFERENCE_OPTION:
1760 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1762 case IMAGEX_VERBOSE_OPTION:
1763 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1765 case IMAGEX_THREADS_OPTION:
1766 num_threads = parse_num_threads(optarg);
1767 if (num_threads == UINT_MAX)
1770 case IMAGEX_REBUILD_OPTION:
1771 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1773 case IMAGEX_UNIX_DATA_OPTION:
1774 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1776 case IMAGEX_SOURCE_LIST_OPTION:
1779 case IMAGEX_NO_ACLS_OPTION:
1780 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1782 case IMAGEX_STRICT_ACLS_OPTION:
1783 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1785 case IMAGEX_RPFIX_OPTION:
1786 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1788 case IMAGEX_NORPFIX_OPTION:
1789 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1791 case IMAGEX_PIPABLE_OPTION:
1792 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1794 case IMAGEX_NOT_PIPABLE_OPTION:
1795 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
1797 case IMAGEX_UPDATE_OF_OPTION:
1798 if (template_image_name_or_num) {
1799 imagex_error(T("'--update-of' can only be "
1800 "specified one time!"));
1804 colon = tstrrchr(optarg, T(':'));
1807 template_wimfile = optarg;
1809 template_image_name_or_num = colon + 1;
1811 template_wimfile = NULL;
1812 template_image_name_or_num = optarg;
1816 case IMAGEX_DELTA_FROM_OPTION:
1817 if (cmd != CMD_CAPTURE) {
1818 imagex_error(T("'--delta-from' is only "
1819 "valid for capture!"));
1822 ret = string_set_append(&base_wimfiles, optarg);
1824 goto out_free_base_wimfiles;
1825 write_flags |= WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS;
1834 if (argc < 2 || argc > 4)
1840 /* Set default compression type. */
1841 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
1842 struct wimlib_lzx_params params;
1843 memset(¶ms, 0, sizeof(params));
1844 params.size_of_this = sizeof(params);
1845 params.algorithm = WIMLIB_LZX_ALGORITHM_FAST;
1846 params.use_defaults = 1;
1848 wimlib_lzx_set_default_params(¶ms);
1849 compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1852 if (!tstrcmp(wimfile, T("-"))) {
1853 /* Writing captured WIM to standard output. */
1855 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
1856 imagex_error("Can't write a non-pipable WIM to "
1857 "standard output! Specify --pipable\n"
1858 " if you want to create a pipable WIM "
1859 "(but read the docs first).");
1863 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1865 if (cmd == CMD_APPEND) {
1866 imagex_error(T("Using standard output for append does "
1867 "not make sense."));
1870 wim_fd = STDOUT_FILENO;
1872 imagex_info_file = stderr;
1873 set_fd_to_binary_mode(wim_fd);
1876 /* If template image was specified using --update-of=IMAGE rather
1877 * than --update-of=WIMFILE:IMAGE, set the default WIMFILE. */
1878 if (template_image_name_or_num && !template_wimfile) {
1879 if (base_wimfiles.num_strings == 1) {
1880 /* Capturing delta WIM based on single WIM: default to
1882 template_wimfile = base_wimfiles.strings[0];
1883 } else if (cmd == CMD_APPEND) {
1884 /* Appending to WIM: default to WIM being appended to.
1886 template_wimfile = wimfile;
1888 /* Capturing a normal (non-delta) WIM, so the WIM file
1889 * *must* be explicitly specified. */
1890 if (base_wimfiles.num_strings > 1) {
1891 imagex_error(T("For capture of delta WIM "
1892 "based on multiple existing "
1894 " '--update-of' must "
1895 "specify WIMFILE:IMAGE!"));
1897 imagex_error(T("For capture of non-delta WIM, "
1898 "'--update-of' must specify "
1907 name_defaulted = false;
1909 /* Set default name to SOURCE argument, omitting any directory
1910 * prefixes and trailing slashes. This requires making a copy
1911 * of @source. Leave some free characters at the end in case we
1912 * append a number to keep the name unique. */
1913 size_t source_name_len;
1915 source_name_len = tstrlen(source);
1916 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1917 name = tbasename(tstrcpy(source_copy, source));
1918 name_defaulted = true;
1920 /* Image description defaults to NULL if not given. */
1927 /* Set up capture sources in source list mode */
1928 if (source[0] == T('-') && source[1] == T('\0')) {
1929 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1931 source_list_contents = file_get_text_contents(source,
1932 &source_list_nchars);
1934 if (!source_list_contents)
1937 capture_sources = parse_source_list(&source_list_contents,
1940 if (!capture_sources) {
1942 goto out_free_source_list_contents;
1944 capture_sources_malloced = true;
1946 /* Set up capture source in non-source-list mode. */
1947 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1948 capture_sources[0].fs_source_path = source;
1949 capture_sources[0].wim_target_path = NULL;
1950 capture_sources[0].reserved = 0;
1952 capture_sources_malloced = false;
1953 source_list_contents = NULL;
1957 /* Read and parse capture configuration file. */
1960 config_str = file_get_text_contents(config_file, &config_len);
1963 goto out_free_capture_sources;
1966 config = alloca(sizeof(*config));
1967 ret = parse_capture_config(&config_str, config_len, config);
1969 goto out_free_config;
1971 /* No capture configuration file specified; use default
1972 * configuration for capturing Windows operating systems. */
1974 add_image_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
1977 /* Open the existing WIM, or create a new one. */
1978 if (cmd == CMD_APPEND)
1979 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1980 imagex_progress_func);
1982 ret = wimlib_create_new_wim(compression_type, &wim);
1984 goto out_free_config;
1986 /* Set chunk size if non-default. */
1987 if (chunk_size != UINT32_MAX) {
1988 ret = wimlib_set_output_chunk_size(wim, chunk_size);
1994 /* Detect if source is regular file or block device and set NTFS volume
1999 if (tstat(source, &stbuf) == 0) {
2000 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
2001 imagex_printf(T("Capturing WIM image from NTFS "
2002 "filesystem on \"%"TS"\"\n"), source);
2003 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
2006 if (errno != ENOENT) {
2007 imagex_error_with_errno(T("Failed to stat "
2008 "\"%"TS"\""), source);
2016 /* If the user did not specify an image name, and the basename of the
2017 * source already exists as an image name in the WIM file, append a
2018 * suffix to make it unique. */
2019 if (cmd == CMD_APPEND && name_defaulted) {
2020 unsigned long conflict_idx;
2021 tchar *name_end = tstrchr(name, T('\0'));
2022 for (conflict_idx = 1;
2023 wimlib_image_name_in_use(wim, name);
2026 tsprintf(name_end, T(" (%lu)"), conflict_idx);
2030 /* If capturing a delta WIM, reference resources from the base WIMs
2031 * before adding the new image. */
2032 if (base_wimfiles.num_strings) {
2033 base_wims = calloc(base_wimfiles.num_strings,
2034 sizeof(base_wims[0]));
2035 if (base_wims == NULL) {
2036 imagex_error(T("Out of memory!"));
2041 for (size_t i = 0; i < base_wimfiles.num_strings; i++) {
2042 ret = wimlib_open_wim(base_wimfiles.strings[i],
2043 open_flags, &base_wims[i],
2044 imagex_progress_func);
2046 goto out_free_base_wims;
2050 ret = wimlib_reference_resources(wim, base_wims,
2051 base_wimfiles.num_strings, 0);
2053 goto out_free_base_wims;
2055 if (base_wimfiles.num_strings == 1) {
2056 imagex_printf(T("Capturing delta WIM based on \"%"TS"\"\n"),
2057 base_wimfiles.strings[0]);
2059 imagex_printf(T("Capturing delta WIM based on %u WIMs\n"),
2060 base_wimfiles.num_strings);
2067 /* If capturing or appending as an update of an existing (template) image,
2068 * open the WIM if needed and parse the image index. */
2069 if (template_image_name_or_num) {
2072 if (base_wimfiles.num_strings == 1 &&
2073 template_wimfile == base_wimfiles.strings[0]) {
2074 template_wim = base_wims[0];
2075 } else if (template_wimfile == wimfile) {
2078 ret = wimlib_open_wim(template_wimfile, open_flags,
2079 &template_wim, imagex_progress_func);
2081 goto out_free_base_wims;
2084 template_image = wimlib_resolve_image(template_wim,
2085 template_image_name_or_num);
2087 if (template_image_name_or_num[0] == T('-')) {
2090 struct wimlib_wim_info info;
2092 wimlib_get_wim_info(template_wim, &info);
2093 n = tstrtoul(template_image_name_or_num + 1, &tmp, 10);
2094 if (n >= 1 && n <= info.image_count &&
2096 tmp != template_image_name_or_num + 1)
2098 template_image = info.image_count - (n - 1);
2101 ret = verify_image_exists_and_is_single(template_image,
2102 template_image_name_or_num,
2105 goto out_free_template_wim;
2107 template_wim = NULL;
2110 ret = wimlib_add_image_multisource(wim,
2116 imagex_progress_func);
2118 goto out_free_template_wim;
2120 if (desc || flags_element || template_image_name_or_num) {
2121 /* User provided <DESCRIPTION> or <FLAGS> element, or an image
2122 * on which the added one is to be based has been specified with
2123 * --update-of. Get the index of the image we just
2124 * added, then use it to call the appropriate functions. */
2125 struct wimlib_wim_info info;
2127 wimlib_get_wim_info(wim, &info);
2130 ret = wimlib_set_image_descripton(wim,
2134 goto out_free_template_wim;
2137 if (flags_element) {
2138 ret = wimlib_set_image_flags(wim, info.image_count,
2141 goto out_free_template_wim;
2144 /* Reference template image if the user provided one. */
2145 if (template_image_name_or_num) {
2146 imagex_printf(T("Using image %d "
2147 "from \"%"TS"\" as template\n"),
2148 template_image, template_wimfile);
2149 ret = wimlib_reference_template_image(wim,
2155 goto out_free_template_wim;
2159 /* Write the new WIM or overwrite the existing WIM with the new image
2161 if (cmd == CMD_APPEND) {
2162 ret = wimlib_overwrite(wim, write_flags, num_threads,
2163 imagex_progress_func);
2164 } else if (wimfile) {
2165 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
2166 write_flags, num_threads,
2167 imagex_progress_func);
2169 ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
2170 write_flags, num_threads,
2171 imagex_progress_func);
2173 out_free_template_wim:
2174 /* template_wim may alias base_wims[0] or wim. */
2175 if ((base_wimfiles.num_strings != 1 || template_wim != base_wims[0]) &&
2176 template_wim != wim)
2177 wimlib_free(template_wim);
2179 for (size_t i = 0; i < base_wimfiles.num_strings; i++)
2180 wimlib_free(base_wims[i]);
2186 free(config->exclusion_pats.pats);
2187 free(config->exclusion_exception_pats.pats);
2190 out_free_capture_sources:
2191 if (capture_sources_malloced)
2192 free(capture_sources);
2193 out_free_source_list_contents:
2194 free(source_list_contents);
2195 out_free_base_wimfiles:
2196 string_set_destroy(&base_wimfiles);
2203 goto out_free_base_wimfiles;
2206 /* Remove image(s) from a WIM. */
2208 imagex_delete(int argc, tchar **argv, int cmd)
2211 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2212 int write_flags = 0;
2213 const tchar *wimfile;
2214 const tchar *image_num_or_name;
2219 for_opt(c, delete_options) {
2221 case IMAGEX_CHECK_OPTION:
2222 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2223 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2225 case IMAGEX_SOFT_OPTION:
2226 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
2237 imagex_error(T("Must specify a WIM file"));
2239 imagex_error(T("Must specify an image"));
2243 image_num_or_name = argv[1];
2245 ret = wimlib_open_wim(wimfile, open_flags, &wim,
2246 imagex_progress_func);
2250 image = wimlib_resolve_image(wim, image_num_or_name);
2252 ret = verify_image_exists(image, image_num_or_name, wimfile);
2254 goto out_wimlib_free;
2256 ret = wimlib_delete_image(wim, image);
2258 imagex_error(T("Failed to delete image from \"%"TS"\""),
2260 goto out_wimlib_free;
2263 ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
2265 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2266 "deleted"), wimfile);
2274 usage(CMD_DELETE, stderr);
2280 print_full_path(const struct wimlib_dir_entry *wdentry, void *_ignore)
2282 int ret = tprintf(T("%"TS"\n"), wdentry->full_path);
2283 return (ret >= 0) ? 0 : -1;
2286 /* Print the files contained in an image(s) in a WIM file. */
2288 imagex_dir(int argc, tchar **argv, int cmd)
2290 const tchar *wimfile;
2291 WIMStruct *wim = NULL;
2294 const tchar *path = T("");
2297 for_opt(c, dir_options) {
2299 case IMAGEX_PATH_OPTION:
2310 imagex_error(T("Must specify a WIM file"));
2314 imagex_error(T("Too many arguments"));
2319 ret = wimlib_open_wim(wimfile, 0, &wim, imagex_progress_func);
2324 image = wimlib_resolve_image(wim, argv[1]);
2325 ret = verify_image_exists(image, argv[1], wimfile);
2327 goto out_wimlib_free;
2329 /* No image specified; default to image 1, but only if the WIM
2330 * contains exactly one image. */
2332 struct wimlib_wim_info info;
2334 wimlib_get_wim_info(wim, &info);
2335 if (info.image_count != 1) {
2336 imagex_error(T("\"%"TS"\" contains %d images; Please "
2337 "select one (or all)."),
2338 wimfile, info.image_count);
2345 ret = wimlib_iterate_dir_tree(wim, image, path,
2346 WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE,
2347 print_full_path, NULL);
2354 usage(CMD_DIR, stderr);
2359 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2362 imagex_export(int argc, tchar **argv, int cmd)
2366 int export_flags = 0;
2367 int write_flags = 0;
2368 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2369 const tchar *src_wimfile;
2370 const tchar *src_image_num_or_name;
2371 const tchar *dest_wimfile;
2373 const tchar *dest_name;
2374 const tchar *dest_desc;
2376 struct wimlib_wim_info src_info;
2377 WIMStruct *dest_wim;
2382 STRING_SET(refglobs);
2383 unsigned num_threads = 0;
2385 for_opt(c, export_options) {
2387 case IMAGEX_BOOT_OPTION:
2388 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2390 case IMAGEX_CHECK_OPTION:
2391 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2392 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2394 case IMAGEX_NOCHECK_OPTION:
2395 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2397 case IMAGEX_COMPRESS_OPTION:
2398 compression_type = get_compression_type(optarg);
2399 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2402 case IMAGEX_REF_OPTION:
2403 ret = string_set_append(&refglobs, optarg);
2405 goto out_free_refglobs;
2407 case IMAGEX_THREADS_OPTION:
2408 num_threads = parse_num_threads(optarg);
2409 if (num_threads == UINT_MAX)
2412 case IMAGEX_REBUILD_OPTION:
2413 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2415 case IMAGEX_PIPABLE_OPTION:
2416 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2418 case IMAGEX_NOT_PIPABLE_OPTION:
2419 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
2427 if (argc < 3 || argc > 5)
2429 src_wimfile = argv[0];
2430 src_image_num_or_name = argv[1];
2431 dest_wimfile = argv[2];
2432 dest_name = (argc >= 4) ? argv[3] : NULL;
2433 dest_desc = (argc >= 5) ? argv[4] : NULL;
2434 ret = wimlib_open_wim(src_wimfile, open_flags, &src_wim,
2435 imagex_progress_func);
2437 goto out_free_refglobs;
2439 wimlib_get_wim_info(src_wim, &src_info);
2441 /* Determine if the destination is an existing file or not. If so, we
2442 * try to append the exported image(s) to it; otherwise, we create a new
2443 * WIM containing the exported image(s). Furthermore, determine if we
2444 * need to write a pipable WIM directly to standard output. */
2446 if (tstrcmp(dest_wimfile, T("-")) == 0) {
2448 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2449 imagex_error("Can't write a non-pipable WIM to "
2450 "standard output! Specify --pipable\n"
2451 " if you want to create a pipable WIM "
2452 "(but read the docs first).");
2454 goto out_free_src_wim;
2457 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2459 dest_wimfile = NULL;
2460 dest_wim_fd = STDOUT_FILENO;
2461 imagex_info_file = stderr;
2462 set_fd_to_binary_mode(dest_wim_fd);
2465 if (dest_wimfile != NULL && tstat(dest_wimfile, &stbuf) == 0) {
2467 /* Destination file exists. */
2469 if (!S_ISREG(stbuf.st_mode)) {
2470 imagex_error(T("\"%"TS"\" is not a regular file"),
2473 goto out_free_src_wim;
2475 ret = wimlib_open_wim(dest_wimfile,
2476 open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2477 &dest_wim, imagex_progress_func);
2479 goto out_free_src_wim;
2481 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2482 /* The user specified a compression type, but we're
2483 * exporting to an existing WIM. Make sure the
2484 * specified compression type is the same as the
2485 * compression type of the existing destination WIM. */
2486 struct wimlib_wim_info dest_info;
2488 wimlib_get_wim_info(dest_wim, &dest_info);
2489 if (compression_type != dest_info.compression_type) {
2490 imagex_error(T("Cannot specify a compression type that is "
2491 "not the same as that used in the "
2492 "destination WIM"));
2494 goto out_free_dest_wim;
2500 if (errno != ENOENT) {
2501 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2504 goto out_free_src_wim;
2507 /* dest_wimfile is not an existing file, so create a new WIM. */
2509 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2510 /* The user did not specify a compression type; default
2511 * to that of the source WIM. */
2513 compression_type = src_info.compression_type;
2515 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2517 goto out_free_src_wim;
2519 wimlib_set_output_chunk_size(dest_wim, src_info.chunk_size);
2522 image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2523 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2525 goto out_free_dest_wim;
2527 if (refglobs.num_strings) {
2528 ret = wim_reference_globs(src_wim, &refglobs, open_flags);
2530 goto out_free_dest_wim;
2533 if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
2534 image == WIMLIB_ALL_IMAGES && src_info.boot_index == 0)
2536 imagex_error(T("--boot specified for all-images export, but source WIM "
2537 "has no bootable image."));
2539 goto out_free_dest_wim;
2542 ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2543 dest_desc, export_flags, imagex_progress_func);
2545 if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2546 do_resource_not_found_warning(src_wimfile,
2547 &src_info, &refglobs);
2549 goto out_free_dest_wim;
2553 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2554 imagex_progress_func);
2555 else if (dest_wimfile)
2556 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2557 write_flags, num_threads,
2558 imagex_progress_func);
2560 ret = wimlib_write_to_fd(dest_wim, dest_wim_fd,
2561 WIMLIB_ALL_IMAGES, write_flags,
2562 num_threads, imagex_progress_func);
2564 wimlib_free(dest_wim);
2566 wimlib_free(src_wim);
2568 string_set_destroy(&refglobs);
2572 usage(CMD_EXPORT, stderr);
2575 goto out_free_refglobs;
2579 is_root_wim_path(const tchar *path)
2582 for (p = path; *p; p++)
2583 if (*p != T('\\') && *p != T('/'))
2589 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2590 const tchar *dest_dir)
2592 for (size_t i = 0; i < num_cmds; i++)
2593 if (cmds[i].fs_dest_path != dest_dir)
2594 free(cmds[i].fs_dest_path);
2598 static struct wimlib_extract_command *
2599 prepare_extract_commands(tchar **paths, unsigned num_paths,
2600 int extract_flags, tchar *dest_dir,
2601 size_t *num_cmds_ret)
2603 struct wimlib_extract_command *cmds;
2605 tchar *emptystr = T("");
2607 if (num_paths == 0) {
2611 num_cmds = num_paths;
2612 cmds = calloc(num_cmds, sizeof(cmds[0]));
2614 imagex_error(T("Out of memory!"));
2618 for (size_t i = 0; i < num_cmds; i++) {
2619 cmds[i].extract_flags = extract_flags;
2620 cmds[i].wim_source_path = paths[i];
2621 if (is_root_wim_path(paths[i])) {
2622 cmds[i].fs_dest_path = dest_dir;
2624 size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2625 cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2626 if (!cmds[i].fs_dest_path) {
2627 free_extract_commands(cmds, num_cmds, dest_dir);
2630 tsprintf(cmds[i].fs_dest_path,
2631 T("%"TS""OS_PREFERRED_PATH_SEPARATOR_STRING"%"TS),
2632 dest_dir, tbasename(paths[i]));
2635 *num_cmds_ret = num_cmds;
2639 /* Extract files or directories from a WIM image */
2641 imagex_extract(int argc, tchar **argv, int cmd)
2648 const tchar *wimfile;
2649 const tchar *image_num_or_name;
2650 tchar *dest_dir = T(".");
2651 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL | WIMLIB_EXTRACT_FLAG_NORPFIX;
2653 STRING_SET(refglobs);
2655 struct wimlib_extract_command *cmds;
2658 for_opt(c, extract_options) {
2660 case IMAGEX_CHECK_OPTION:
2661 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2663 case IMAGEX_VERBOSE_OPTION:
2664 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
2666 case IMAGEX_REF_OPTION:
2667 ret = string_set_append(&refglobs, optarg);
2669 goto out_free_refglobs;
2671 case IMAGEX_UNIX_DATA_OPTION:
2672 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2674 case IMAGEX_NO_ACLS_OPTION:
2675 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2677 case IMAGEX_STRICT_ACLS_OPTION:
2678 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2680 case IMAGEX_DEST_DIR_OPTION:
2683 case IMAGEX_TO_STDOUT_OPTION:
2684 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2685 imagex_info_file = stderr;
2686 imagex_be_quiet = true;
2688 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2689 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2690 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2703 image_num_or_name = argv[1];
2708 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2713 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2717 image = wimlib_resolve_image(wim, image_num_or_name);
2718 ret = verify_image_exists_and_is_single(image,
2722 goto out_wimlib_free;
2724 if (refglobs.num_strings) {
2725 ret = wim_reference_globs(wim, &refglobs, open_flags);
2727 goto out_wimlib_free;
2730 ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
2731 imagex_progress_func);
2733 if (!imagex_be_quiet)
2734 imagex_printf(T("Done extracting files.\n"));
2735 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2736 tfprintf(stderr, T("Note: You can use `%"TS"' to see what "
2737 "files and directories\n"
2738 " are in the WIM image.\n"),
2739 get_cmd_string(CMD_INFO, false));
2740 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2741 struct wimlib_wim_info info;
2743 wimlib_get_wim_info(wim, &info);
2744 do_resource_not_found_warning(wimfile, &info, &refglobs);
2749 free_extract_commands(cmds, num_cmds, dest_dir);
2751 string_set_destroy(&refglobs);
2755 usage(CMD_EXTRACT, stderr);
2758 goto out_free_refglobs;
2761 static void print_byte_field(const uint8_t field[], size_t len)
2764 tprintf(T("%02hhx"), *field++);
2768 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2770 tputs(T("WIM Information:"));
2771 tputs(T("----------------"));
2772 tprintf(T("Path: %"TS"\n"), wimfile);
2773 tprintf(T("GUID: 0x"));
2774 print_byte_field(info->guid, sizeof(info->guid));
2776 tprintf(T("Image Count: %d\n"), info->image_count);
2777 tprintf(T("Compression: %"TS"\n"),
2778 wimlib_get_compression_type_string(info->compression_type));
2779 tprintf(T("Chunk Size: %"PRIu32" bytes\n"),
2781 tprintf(T("Part Number: %d/%d\n"), info->part_number, info->total_parts);
2782 tprintf(T("Boot Index: %d\n"), info->boot_index);
2783 tprintf(T("Size: %"PRIu64" bytes\n"), info->total_bytes);
2784 tprintf(T("Integrity Info: %"TS"\n"),
2785 info->has_integrity_table ? T("yes") : T("no"));
2786 tprintf(T("Relative path junction: %"TS"\n"),
2787 info->has_rpfix ? T("yes") : T("no"));
2788 tprintf(T("Pipable: %"TS"\n"),
2789 info->pipable ? T("yes") : T("no"));
2794 print_resource(const struct wimlib_resource_entry *resource,
2798 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
2799 resource->uncompressed_size);
2801 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
2802 resource->compressed_size);
2804 tprintf(T("Offset = %"PRIu64" bytes\n"),
2808 tprintf(T("Part Number = %u\n"), resource->part_number);
2809 tprintf(T("Reference Count = %u\n"), resource->reference_count);
2811 tprintf(T("Hash = 0x"));
2812 print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2815 tprintf(T("Flags = "));
2816 if (resource->is_compressed)
2817 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED "));
2818 if (resource->is_metadata)
2819 tprintf(T("WIM_RESHDR_FLAG_METADATA "));
2820 if (resource->is_free)
2821 tprintf(T("WIM_RESHDR_FLAG_FREE "));
2822 if (resource->is_spanned)
2823 tprintf(T("WIM_RESHDR_FLAG_SPANNED "));
2830 print_lookup_table(WIMStruct *wim)
2832 wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2835 /* Prints information about a WIM file; also can mark an image as bootable,
2836 * change the name of an image, or change the description of an image. */
2838 imagex_info(int argc, tchar **argv, int cmd)
2843 bool nocheck = false;
2844 bool header = false;
2845 bool lookup_table = false;
2847 bool metadata = false;
2848 bool short_header = true;
2849 const tchar *xml_out_file = NULL;
2850 const tchar *wimfile;
2851 const tchar *image_num_or_name;
2852 const tchar *new_name;
2853 const tchar *new_desc;
2858 struct wimlib_wim_info info;
2860 for_opt(c, info_options) {
2862 case IMAGEX_BOOT_OPTION:
2865 case IMAGEX_CHECK_OPTION:
2868 case IMAGEX_NOCHECK_OPTION:
2871 case IMAGEX_HEADER_OPTION:
2873 short_header = false;
2875 case IMAGEX_LOOKUP_TABLE_OPTION:
2876 lookup_table = true;
2877 short_header = false;
2879 case IMAGEX_XML_OPTION:
2881 short_header = false;
2883 case IMAGEX_EXTRACT_XML_OPTION:
2884 xml_out_file = optarg;
2885 short_header = false;
2887 case IMAGEX_METADATA_OPTION:
2889 short_header = false;
2898 if (argc < 1 || argc > 4)
2902 image_num_or_name = (argc >= 2) ? argv[1] : T("all");
2903 new_name = (argc >= 3) ? argv[2] : NULL;
2904 new_desc = (argc >= 4) ? argv[3] : NULL;
2906 if (check && nocheck) {
2907 imagex_error(T("Can't specify both --check and --nocheck"));
2912 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2914 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2918 wimlib_get_wim_info(wim, &info);
2920 image = wimlib_resolve_image(wim, image_num_or_name);
2921 ret = WIMLIB_ERR_INVALID_IMAGE;
2922 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
2923 verify_image_exists(image, image_num_or_name, wimfile);
2925 imagex_error(T("If you would like to set the boot "
2926 "index to 0, specify image \"0\" with "
2927 "the --boot flag."));
2929 goto out_wimlib_free;
2932 if (boot && info.image_count == 0) {
2933 imagex_error(T("--boot is meaningless on a WIM with no images"));
2934 goto out_wimlib_free;
2937 if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
2939 imagex_error(T("Cannot specify the --boot flag "
2940 "without specifying a specific "
2941 "image in a multi-image WIM"));
2942 goto out_wimlib_free;
2945 imagex_error(T("Cannot specify the NEW_NAME "
2946 "without specifying a specific "
2947 "image in a multi-image WIM"));
2948 goto out_wimlib_free;
2952 /* Operations that print information are separated from operations that
2953 * recreate the WIM file. */
2954 if (!new_name && !boot) {
2956 /* Read-only operations */
2958 if (image == WIMLIB_NO_IMAGE) {
2959 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
2960 image_num_or_name, wimfile);
2961 goto out_wimlib_free;
2964 if (image == WIMLIB_ALL_IMAGES && short_header)
2965 print_wim_information(wimfile, &info);
2968 wimlib_print_header(wim);
2971 if (info.total_parts != 1) {
2972 tfprintf(stderr, T("Warning: Only showing the lookup table "
2973 "for part %d of a %d-part WIM.\n"),
2974 info.part_number, info.total_parts);
2976 print_lookup_table(wim);
2980 ret = wimlib_extract_xml_data(wim, stdout);
2982 goto out_wimlib_free;
2988 fp = tfopen(xml_out_file, T("wb"));
2990 imagex_error_with_errno(T("Failed to open the "
2991 "file \"%"TS"\" for "
2995 goto out_wimlib_free;
2997 ret = wimlib_extract_xml_data(wim, fp);
2999 imagex_error(T("Failed to close the file "
3005 goto out_wimlib_free;
3009 wimlib_print_available_images(wim, image);
3012 ret = wimlib_print_metadata(wim, image);
3014 goto out_wimlib_free;
3019 /* Modification operations */
3021 if (image == WIMLIB_ALL_IMAGES)
3024 if (image == WIMLIB_NO_IMAGE && new_name) {
3025 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
3026 "when using image 0"), new_name);
3028 goto out_wimlib_free;
3032 if (image == info.boot_index) {
3033 imagex_printf(T("Image %d is already marked as "
3034 "bootable.\n"), image);
3037 imagex_printf(T("Marking image %d as bootable.\n"),
3039 info.boot_index = image;
3040 ret = wimlib_set_wim_info(wim, &info,
3041 WIMLIB_CHANGE_BOOT_INDEX);
3043 goto out_wimlib_free;
3047 if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
3049 imagex_printf(T("Image %d is already named \"%"TS"\".\n"),
3053 imagex_printf(T("Changing the name of image %d to "
3054 "\"%"TS"\".\n"), image, new_name);
3055 ret = wimlib_set_image_name(wim, image, new_name);
3057 goto out_wimlib_free;
3061 const tchar *old_desc;
3062 old_desc = wimlib_get_image_description(wim, image);
3063 if (old_desc && !tstrcmp(old_desc, new_desc)) {
3064 imagex_printf(T("The description of image %d is already "
3065 "\"%"TS"\".\n"), image, new_desc);
3068 imagex_printf(T("Changing the description of image %d "
3069 "to \"%"TS"\".\n"), image, new_desc);
3070 ret = wimlib_set_image_descripton(wim, image,
3073 goto out_wimlib_free;
3077 /* Only call wimlib_overwrite() if something actually needs to
3079 if (boot || new_name || new_desc ||
3080 (check && !info.has_integrity_table) ||
3081 (nocheck && info.has_integrity_table))
3083 int write_flags = 0;
3086 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3088 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3089 ret = wimlib_overwrite(wim, write_flags, 1,
3090 imagex_progress_func);
3092 imagex_printf(T("The file \"%"TS"\" was not modified "
3093 "because nothing needed to be done.\n"),
3104 usage(CMD_INFO, stderr);
3110 /* Join split WIMs into one part WIM */
3112 imagex_join(int argc, tchar **argv, int cmd)
3115 int swm_open_flags = 0;
3116 int wim_write_flags = 0;
3117 const tchar *output_path;
3120 for_opt(c, join_options) {
3122 case IMAGEX_CHECK_OPTION:
3123 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3124 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3134 imagex_error(T("Must specify one or more split WIM (.swm) "
3138 output_path = argv[0];
3139 ret = wimlib_join((const tchar * const *)++argv,
3144 imagex_progress_func);
3149 usage(CMD_JOIN, stderr);
3154 #if WIM_MOUNTING_SUPPORTED
3156 /* Mounts a WIM image. */
3158 imagex_mount_rw_or_ro(int argc, tchar **argv, int cmd)
3161 int mount_flags = 0;
3163 const tchar *staging_dir = NULL;
3164 const tchar *wimfile;
3167 struct wimlib_wim_info info;
3171 STRING_SET(refglobs);
3173 if (cmd == CMD_MOUNTRW) {
3174 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
3175 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3178 for_opt(c, mount_options) {
3180 case IMAGEX_ALLOW_OTHER_OPTION:
3181 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
3183 case IMAGEX_CHECK_OPTION:
3184 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3186 case IMAGEX_DEBUG_OPTION:
3187 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
3189 case IMAGEX_STREAMS_INTERFACE_OPTION:
3190 if (!tstrcasecmp(optarg, T("none")))
3191 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
3192 else if (!tstrcasecmp(optarg, T("xattr")))
3193 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
3194 else if (!tstrcasecmp(optarg, T("windows")))
3195 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
3197 imagex_error(T("Unknown stream interface \"%"TS"\""),
3202 case IMAGEX_REF_OPTION:
3203 ret = string_set_append(&refglobs, optarg);
3205 goto out_free_refglobs;
3207 case IMAGEX_STAGING_DIR_OPTION:
3208 staging_dir = optarg;
3210 case IMAGEX_UNIX_DATA_OPTION:
3211 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
3219 if (argc != 2 && argc != 3)
3224 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3226 goto out_free_refglobs;
3228 wimlib_get_wim_info(wim, &info);
3231 /* Image explicitly specified. */
3232 image = wimlib_resolve_image(wim, argv[1]);
3234 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
3238 /* No image specified; default to image 1, but only if the WIM
3239 * contains exactly one image. */
3241 if (info.image_count != 1) {
3242 imagex_error(T("\"%"TS"\" contains %d images; Please "
3243 "select one."), wimfile, info.image_count);
3251 if (refglobs.num_strings) {
3252 ret = wim_reference_globs(wim, &refglobs, open_flags);
3257 ret = wimlib_mount_image(wim, image, dir, mount_flags, staging_dir);
3259 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
3261 image, wimfile, dir);
3266 string_set_destroy(&refglobs);
3272 goto out_free_refglobs;
3274 #endif /* WIM_MOUNTING_SUPPORTED */
3276 /* Rebuild a WIM file */
3278 imagex_optimize(int argc, tchar **argv, int cmd)
3281 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3282 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
3283 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
3284 uint32_t chunk_size = UINT32_MAX;
3287 const tchar *wimfile;
3290 unsigned num_threads = 0;
3292 for_opt(c, optimize_options) {
3294 case IMAGEX_CHECK_OPTION:
3295 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3296 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3298 case IMAGEX_NOCHECK_OPTION:
3299 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3301 case IMAGEX_COMPRESS_OPTION:
3302 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3303 compression_type = get_compression_type(optarg);
3304 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
3307 case IMAGEX_RECOMPRESS_OPTION:
3308 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3310 case IMAGEX_COMPRESS_SLOW_OPTION:
3311 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3312 compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
3313 ret = set_compress_slow();
3317 case IMAGEX_CHUNK_SIZE_OPTION:
3318 chunk_size = parse_chunk_size(optarg);
3319 if (chunk_size == UINT32_MAX)
3322 case IMAGEX_THREADS_OPTION:
3323 num_threads = parse_num_threads(optarg);
3324 if (num_threads == UINT_MAX)
3327 case IMAGEX_PIPABLE_OPTION:
3328 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
3330 case IMAGEX_NOT_PIPABLE_OPTION:
3331 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
3345 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3349 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
3350 /* Change compression type. */
3351 ret = wimlib_set_output_compression_type(wim, compression_type);
3353 goto out_wimlib_free;
3356 if (chunk_size != UINT32_MAX) {
3357 /* Change chunk size. */
3358 ret = wimlib_set_output_chunk_size(wim, chunk_size);
3360 goto out_wimlib_free;
3363 old_size = file_get_size(wimfile);
3364 tprintf(T("\"%"TS"\" original size: "), wimfile);
3366 tputs(T("Unknown"));
3368 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3370 ret = wimlib_overwrite(wim, write_flags, num_threads,
3371 imagex_progress_func);
3373 imagex_error(T("Optimization of \"%"TS"\" failed."), wimfile);
3374 goto out_wimlib_free;
3377 new_size = file_get_size(wimfile);
3378 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3380 tputs(T("Unknown"));
3382 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3384 tfputs(T("Space saved: "), stdout);
3385 if (new_size != -1 && old_size != -1) {
3386 tprintf(T("%lld KiB\n"),
3387 ((long long)old_size - (long long)new_size) >> 10);
3389 tputs(T("Unknown"));
3398 usage(CMD_OPTIMIZE, stderr);
3404 /* Split a WIM into a spanned set */
3406 imagex_split(int argc, tchar **argv, int cmd)
3410 int write_flags = 0;
3411 unsigned long part_size;
3416 for_opt(c, split_options) {
3418 case IMAGEX_CHECK_OPTION:
3419 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3420 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3432 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3433 if (tmp == argv[2] || *tmp) {
3434 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3435 imagex_error(T("The part size must be an integer or "
3436 "floating-point number of megabytes."));
3439 ret = wimlib_open_wim(argv[0], open_flags, &wim, imagex_progress_func);
3443 ret = wimlib_split(wim, argv[1], part_size, write_flags, imagex_progress_func);
3449 usage(CMD_SPLIT, stderr);
3455 #if WIM_MOUNTING_SUPPORTED
3456 /* Unmounts a mounted WIM image. */
3458 imagex_unmount(int argc, tchar **argv, int cmd)
3461 int unmount_flags = 0;
3464 for_opt(c, unmount_options) {
3466 case IMAGEX_COMMIT_OPTION:
3467 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3469 case IMAGEX_CHECK_OPTION:
3470 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3472 case IMAGEX_REBUILD_OPTION:
3473 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3475 case IMAGEX_LAZY_OPTION:
3476 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3487 ret = wimlib_unmount_image(argv[0], unmount_flags,
3488 imagex_progress_func);
3490 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3495 usage(CMD_UNMOUNT, stderr);
3499 #endif /* WIM_MOUNTING_SUPPORTED */
3502 * Add, delete, or rename files in a WIM image.
3505 imagex_update(int argc, tchar **argv, int cmd)
3507 const tchar *wimfile;
3511 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3512 int write_flags = 0;
3513 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3514 int default_add_flags = WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
3515 int default_delete_flags = 0;
3516 unsigned num_threads = 0;
3518 tchar *cmd_file_contents;
3519 size_t cmd_file_nchars;
3520 struct wimlib_update_command *cmds;
3522 tchar *command_str = NULL;
3524 const tchar *config_file = NULL;
3526 struct wimlib_capture_config *config;
3528 for_opt(c, update_options) {
3530 /* Generic or write options */
3531 case IMAGEX_THREADS_OPTION:
3532 num_threads = parse_num_threads(optarg);
3533 if (num_threads == UINT_MAX)
3536 case IMAGEX_CHECK_OPTION:
3537 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3538 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3540 case IMAGEX_REBUILD_OPTION:
3541 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3543 case IMAGEX_COMMAND_OPTION:
3545 imagex_error(T("--command may only be specified "
3546 "one time. Please provide\n"
3547 " the update commands "
3548 "on standard input instead."));
3551 command_str = tstrdup(optarg);
3553 imagex_error(T("Out of memory!"));
3557 /* Default delete options */
3558 case IMAGEX_FORCE_OPTION:
3559 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3561 case IMAGEX_RECURSIVE_OPTION:
3562 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3565 /* Global add option */
3566 case IMAGEX_CONFIG_OPTION:
3567 default_add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
3568 config_file = optarg;
3571 /* Default add options */
3572 case IMAGEX_VERBOSE_OPTION:
3573 default_add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
3575 case IMAGEX_DEREFERENCE_OPTION:
3576 default_add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
3578 case IMAGEX_UNIX_DATA_OPTION:
3579 default_add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
3581 case IMAGEX_NO_ACLS_OPTION:
3582 default_add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
3584 case IMAGEX_STRICT_ACLS_OPTION:
3585 default_add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
3594 if (argc != 1 && argc != 2)
3598 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3600 goto out_free_command_str;
3603 /* Image explicitly specified. */
3604 image = wimlib_resolve_image(wim, argv[1]);
3605 ret = verify_image_exists_and_is_single(image, argv[1],
3608 goto out_wimlib_free;
3610 /* No image specified; default to image 1, but only if the WIM
3611 * contains exactly one image. */
3612 struct wimlib_wim_info info;
3614 wimlib_get_wim_info(wim, &info);
3615 if (info.image_count != 1) {
3616 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3617 wimfile, info.image_count);
3624 /* Parse capture configuration file if specified */
3628 config_str = file_get_text_contents(config_file, &config_len);
3631 goto out_wimlib_free;
3634 config = alloca(sizeof(*config));
3635 ret = parse_capture_config(&config_str, config_len, config);
3637 goto out_free_config;
3640 default_add_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
3643 /* Read update commands from standard input, or the command string if
3646 cmd_file_contents = NULL;
3647 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3650 if (isatty(STDIN_FILENO)) {
3651 tputs(T("Reading update commands from standard input..."));
3652 recommend_man_page(CMD_UPDATE, stdout);
3654 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3655 if (!cmd_file_contents) {
3657 goto out_free_config;
3660 /* Parse the update commands */
3661 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3666 goto out_free_cmd_file_contents;
3669 /* Set default flags and capture config on the update commands */
3670 for (size_t i = 0; i < num_cmds; i++) {
3671 switch (cmds[i].op) {
3672 case WIMLIB_UPDATE_OP_ADD:
3673 cmds[i].add.add_flags |= default_add_flags;
3674 cmds[i].add.config = config;
3676 case WIMLIB_UPDATE_OP_DELETE:
3677 cmds[i].delete_.delete_flags |= default_delete_flags;
3684 /* Execute the update commands */
3685 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3686 imagex_progress_func);
3690 /* Overwrite the updated WIM */
3691 ret = wimlib_overwrite(wim, write_flags, num_threads,
3692 imagex_progress_func);
3695 out_free_cmd_file_contents:
3696 free(cmd_file_contents);
3699 free(config->exclusion_pats.pats);
3700 free(config->exclusion_exception_pats.pats);
3705 out_free_command_str:
3710 usage(CMD_UPDATE, stderr);
3713 goto out_free_command_str;
3718 struct imagex_command {
3720 int (*func)(int argc, tchar **argv, int cmd);
3723 static const struct imagex_command imagex_commands[] = {
3724 [CMD_APPEND] = {T("append"), imagex_capture_or_append},
3725 [CMD_APPLY] = {T("apply"), imagex_apply},
3726 [CMD_CAPTURE] = {T("capture"), imagex_capture_or_append},
3727 [CMD_DELETE] = {T("delete"), imagex_delete},
3728 [CMD_DIR ] = {T("dir"), imagex_dir},
3729 [CMD_EXPORT] = {T("export"), imagex_export},
3730 [CMD_EXTRACT] = {T("extract"), imagex_extract},
3731 [CMD_INFO] = {T("info"), imagex_info},
3732 [CMD_JOIN] = {T("join"), imagex_join},
3733 #if WIM_MOUNTING_SUPPORTED
3734 [CMD_MOUNT] = {T("mount"), imagex_mount_rw_or_ro},
3735 [CMD_MOUNTRW] = {T("mountrw"), imagex_mount_rw_or_ro},
3737 [CMD_OPTIMIZE] = {T("optimize"), imagex_optimize},
3738 [CMD_SPLIT] = {T("split"), imagex_split},
3739 #if WIM_MOUNTING_SUPPORTED
3740 [CMD_UNMOUNT] = {T("unmount"), imagex_unmount},
3742 [CMD_UPDATE] = {T("update"), imagex_update},
3745 static const tchar *usage_strings[] = {
3748 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3749 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot]\n"
3750 " [--check] [--nocheck] [--flags EDITION_ID] [--verbose]\n"
3751 " [--dereference] [--config=FILE] [--threads=NUM_THREADS]\n"
3752 " [--source-list] [--no-acls] [--strict-acls] [--rpfix]\n"
3753 " [--norpfix] [--unix-data] [--pipable]\n"
3754 " [--update-of=[WIMFILE:]IMAGE]\n"
3758 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME | all)]\n"
3759 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
3760 " [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n"
3761 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3762 " [--include-invalid-names]\n"
3766 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3767 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot]\n"
3768 " [--check] [--nocheck] [--compress=TYPE]\n"
3769 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
3770 " [--config=FILE] [--threads=NUM_THREADS] [--source-list]\n"
3771 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3772 " [--unix-data] [--pipable] [--update-of=[WIMFILE:]IMAGE]\n"
3773 " [--delta-from=WIMFILE]\n"
3777 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n"
3782 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH]\n"
3786 " %"TS" SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
3787 " DEST_WIMFILE [DEST_IMAGE_NAME [DEST_IMAGE_DESCRIPTION]]\n"
3788 " [--boot] [--check] [--nocheck] [--compress=TYPE]\n"
3789 " [--ref=\"GLOB\"] [--threads=NUM_THREADS] [--rebuild]\n"
3790 " [--pipable] [--not-pipable]\n"
3794 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME) [PATH...]\n"
3795 " [--check] [--ref=\"GLOB\"] [--verbose] [--unix-data]\n"
3796 " [--no-acls] [--strict-acls] [--to-stdout]\n"
3797 " [--dest-dir=CMD_DIR] [--include-invalid-names]\n"
3801 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME) [NEW_NAME\n"
3802 " [NEW_DESC]]] [--boot] [--check] [--nocheck] [--header]\n"
3803 " [--lookup-table] [--xml] [--extract-xml FILE]\n"
3808 " %"TS" OUT_WIMFILE SPLIT_WIM_PART... [--check]\n"
3810 #if WIM_MOUNTING_SUPPORTED
3813 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3814 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
3815 " [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n"
3819 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3820 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
3821 " [--staging-dir=CMD_DIR] [--unix-data] [--allow-other]\n"
3826 " %"TS" WIMFILE [--check] [--nocheck] [--recompress]\n"
3827 " [--recompress-slow] [--compress=TYPE]\n"
3828 " [--threads=NUM_THREADS] [--pipable] [--not-pipable]\n"
3832 " %"TS" WIMFILE SPLIT_WIM_PART_1 PART_SIZE_MB [--check]\n"
3834 #if WIM_MOUNTING_SUPPORTED
3837 " %"TS" DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
3842 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
3843 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
3844 " [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
3848 static const tchar *invocation_name;
3849 static int invocation_cmd = CMD_NONE;
3851 static const tchar *get_cmd_string(int cmd, bool nospace)
3853 static tchar buf[50];
3854 if (cmd == CMD_NONE) {
3855 tsprintf(buf, T("%"TS), T(IMAGEX_PROGNAME));
3856 } else if (invocation_cmd != CMD_NONE) {
3857 tsprintf(buf, T("wim%"TS), imagex_commands[cmd].name);
3859 const tchar *format;
3862 format = T("%"TS"-%"TS"");
3864 format = T("%"TS" %"TS"");
3865 tsprintf(buf, format, invocation_name, imagex_commands[cmd].name);
3873 static const tchar *s =
3875 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
3876 "Copyright (C) 2012, 2013 Eric Biggers\n"
3877 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
3878 "This is free software: you are free to change and redistribute it.\n"
3879 "There is NO WARRANTY, to the extent permitted by law.\n"
3881 "Report bugs to "PACKAGE_BUGREPORT".\n"
3888 help_or_version(int argc, tchar **argv, int cmd)
3893 for (i = 1; i < argc; i++) {
3895 if (p[0] == T('-') && p[1] == T('-')) {
3897 if (!tstrcmp(p, T("help"))) {
3898 if (cmd == CMD_NONE)
3903 } else if (!tstrcmp(p, T("version"))) {
3912 print_usage_string(int cmd, FILE *fp)
3914 tfprintf(fp, usage_strings[cmd], get_cmd_string(cmd, false));
3918 recommend_man_page(int cmd, FILE *fp)
3920 const tchar *format_str;
3922 format_str = T("See %"TS".pdf in the doc directory for more details.\n");
3924 format_str = T("Try `man %"TS"' for more details.\n");
3926 tfprintf(fp, format_str, get_cmd_string(cmd, true));
3930 usage(int cmd, FILE *fp)
3932 tfprintf(fp, T("Usage:\n"));
3933 print_usage_string(cmd, fp);
3934 tfprintf(fp, T("\n"));
3935 recommend_man_page(cmd, fp);
3941 tfprintf(fp, T("Usage:\n"));
3942 for (int cmd = 0; cmd < CMD_MAX; cmd++) {
3943 print_usage_string(cmd, fp);
3944 tfprintf(fp, T("\n"));
3946 static const tchar *extra =
3949 " %"TS" --version\n"
3951 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
3954 tfprintf(fp, extra, invocation_name, invocation_name);
3955 recommend_man_page(CMD_NONE, fp);
3958 /* Entry point for wimlib's ImageX implementation. On UNIX the command
3959 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
3960 * something else), while an Windows the command arguments will be UTF-16LE
3961 * encoded 'wchar_t' strings. */
3964 wmain(int argc, wchar_t **argv, wchar_t **envp)
3966 main(int argc, char **argv)
3973 imagex_info_file = stdout;
3974 invocation_name = tbasename(argv[0]);
3977 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
3978 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
3982 setlocale(LC_ALL, "");
3983 codeset = nl_langinfo(CODESET);
3984 if (!strstr(codeset, "UTF-8") &&
3985 !strstr(codeset, "UTF8") &&
3986 !strstr(codeset, "utf-8") &&
3987 !strstr(codeset, "utf8"))
3990 "WARNING: Running %"TS" in a UTF-8 locale is recommended!\n"
3991 " Maybe try: `export LANG=en_US.UTF-8'?\n"
3992 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
3993 " to any value to force wimlib to use UTF-8.\n",
3998 #endif /* !__WIN32__ */
4000 /* Allow being invoked as wimCOMMAND (e.g. wimapply). */
4002 if (!tstrncmp(invocation_name, T("wim"), 3) &&
4003 tstrcmp(invocation_name, T(IMAGEX_PROGNAME))) {
4004 for (int i = 0; i < CMD_MAX; i++) {
4005 if (!tstrcmp(invocation_name + 3,
4006 imagex_commands[i].name))
4015 /* Unless already known from the invocation name, determine which
4016 * command was specified. */
4017 if (cmd == CMD_NONE) {
4019 imagex_error(T("No command specified!\n"));
4023 for (int i = 0; i < CMD_MAX; i++) {
4024 if (!tstrcmp(argv[1], imagex_commands[i].name)) {
4029 if (cmd != CMD_NONE) {
4035 /* Handle --help and --version. --help can be either for the program as
4036 * a whole (cmd == CMD_NONE) or just for a specific command (cmd !=
4037 * CMD_NONE). Note: help_or_version() will not return if a --help or
4038 * --version argument was found. */
4039 help_or_version(argc, argv, cmd);
4041 /* Bail if a valid command was not specified. */
4042 if (cmd == CMD_NONE) {
4043 imagex_error(T("Unrecognized command: `%"TS"'\n"), argv[1]);
4048 /* Enable warning and error messages in wimlib be more user-friendly.
4050 wimlib_set_print_errors(true);
4052 /* Initialize wimlib. */
4053 ret = wimlib_global_init(init_flags);
4055 goto out_check_status;
4057 /* Call the command handler function. */
4058 ret = imagex_commands[cmd].func(argc, argv, cmd);
4060 /* Check for error writing to standard output, especially since for some
4061 * commands, writing to standard output is part of the program's actual
4062 * behavior and not just for informational purposes. */
4063 if (ferror(stdout) || fclose(stdout)) {
4064 imagex_error_with_errno(T("error writing to standard output"));
4069 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
4070 * itself (not by wimlib). 0 indicates success. > 0 indicates a wimlib
4071 * error code from which an error message can be printed. */
4073 imagex_error(T("Exiting with error code %d:\n"
4075 wimlib_get_error_string(ret));
4076 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
4077 imagex_error_with_errno(T("errno"));
4079 /* Make wimlib free any resources it's holding (although this is not
4080 * strictly necessary because the process is ending anyway). */
4081 wimlib_global_cleanup();