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,
146 IMAGEX_PACK_STREAMS_OPTION,
148 IMAGEX_PIPABLE_OPTION,
149 IMAGEX_REBUILD_OPTION,
150 IMAGEX_RECOMPRESS_OPTION,
151 IMAGEX_RECURSIVE_OPTION,
153 IMAGEX_RESUME_OPTION,
156 IMAGEX_SOURCE_LIST_OPTION,
157 IMAGEX_STAGING_DIR_OPTION,
158 IMAGEX_STREAMS_INTERFACE_OPTION,
159 IMAGEX_STRICT_ACLS_OPTION,
160 IMAGEX_SYMLINK_OPTION,
161 IMAGEX_THREADS_OPTION,
162 IMAGEX_TO_STDOUT_OPTION,
163 IMAGEX_UNIX_DATA_OPTION,
164 IMAGEX_UPDATE_OF_OPTION,
165 IMAGEX_VERBOSE_OPTION,
169 static const struct option apply_options[] = {
170 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
171 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
172 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
173 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
174 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
175 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
176 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
177 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
178 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
179 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
180 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
181 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
183 /* --resume is undocumented for now as it needs improvement. */
184 {T("resume"), no_argument, NULL, IMAGEX_RESUME_OPTION},
188 static const struct option capture_or_append_options[] = {
189 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
190 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
191 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
192 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
193 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
194 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
195 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
196 {T("pack-streams"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
197 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
198 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
199 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
200 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
201 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
202 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
203 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
204 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
205 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
206 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
207 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
208 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
209 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
210 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
211 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
212 {T("update-of"), required_argument, NULL, IMAGEX_UPDATE_OF_OPTION},
213 {T("delta-from"), required_argument, NULL, IMAGEX_DELTA_FROM_OPTION},
217 static const struct option delete_options[] = {
218 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
219 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
223 static const struct option dir_options[] = {
224 {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
228 static const struct option export_options[] = {
229 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
230 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
231 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
232 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
233 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
234 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
235 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
236 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
237 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
238 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
239 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
240 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
244 static const struct option extract_options[] = {
245 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
246 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
247 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
248 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
249 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
250 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
251 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
252 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
253 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
254 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
258 static const struct option info_options[] = {
259 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
260 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
261 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
262 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
263 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
264 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
265 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
266 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
267 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
271 static const struct option join_options[] = {
272 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
276 static const struct option mount_options[] = {
277 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
278 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
279 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
280 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
281 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
282 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
283 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
287 static const struct option optimize_options[] = {
288 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
289 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
290 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
291 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
292 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
293 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
294 {T("recompress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
295 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
296 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
297 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
298 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
299 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
303 static const struct option split_options[] = {
304 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
308 static const struct option unmount_options[] = {
309 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
310 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
311 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
312 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
316 static const struct option update_options[] = {
317 /* Careful: some of the options here set the defaults for update
318 * commands, but the flags given to an actual update command (and not to
319 * `imagex update' itself are also handled in
320 * update_command_add_option(). */
321 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
322 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
323 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
324 {T("command"), required_argument, NULL, IMAGEX_COMMAND_OPTION},
326 /* Default delete options */
327 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
328 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
330 /* Global add option */
331 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
333 /* Default add options */
334 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
335 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
336 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
337 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
338 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
339 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
345 # define _format_attribute(type, format_str, args_start) \
346 __attribute__((format(type, format_str, args_start)))
348 # define _format_attribute(type, format_str, args_start)
351 /* Print formatted error message to stderr. */
352 static void _format_attribute(printf, 1, 2)
353 imagex_error(const tchar *format, ...)
356 va_start(va, format);
357 tfputs(T("ERROR: "), stderr);
358 tvfprintf(stderr, format, va);
359 tputc(T('\n'), stderr);
363 /* Print formatted error message to stderr. */
364 static void _format_attribute(printf, 1, 2)
365 imagex_error_with_errno(const tchar *format, ...)
367 int errno_save = errno;
369 va_start(va, format);
370 tfputs(T("ERROR: "), stderr);
371 tvfprintf(stderr, format, va);
372 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
377 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
379 if (image == WIMLIB_NO_IMAGE) {
380 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
381 " Please specify a 1-based image index or "
382 "image name. To list the images\n"
383 " contained in the WIM archive, run\n"
385 " %"TS" \"%"TS"\"\n"),
386 image_name, wim_name,
387 get_cmd_string(CMD_INFO, false), wim_name);
388 return WIMLIB_ERR_INVALID_IMAGE;
394 verify_image_is_single(int image)
396 if (image == WIMLIB_ALL_IMAGES) {
397 imagex_error(T("Cannot specify all images for this action!"));
398 return WIMLIB_ERR_INVALID_IMAGE;
404 verify_image_exists_and_is_single(int image, const tchar *image_name,
405 const tchar *wim_name)
408 ret = verify_image_exists(image, image_name, wim_name);
410 ret = verify_image_is_single(image);
414 /* Parse the argument to --compress */
416 get_compression_type(const tchar *optarg)
418 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
419 return WIMLIB_COMPRESSION_TYPE_LZX;
420 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
421 return WIMLIB_COMPRESSION_TYPE_XPRESS;
422 else if (!tstrcasecmp(optarg, T("recovery")) || !tstrcasecmp(optarg, T("lzms")))
423 return WIMLIB_COMPRESSION_TYPE_LZMS;
424 else if (!tstrcasecmp(optarg, T("none")))
425 return WIMLIB_COMPRESSION_TYPE_NONE;
427 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
428 "\"maximum\", \"fast\", or \"none\"."), optarg);
429 return WIMLIB_COMPRESSION_TYPE_INVALID;
434 set_compress_slow(void)
437 static const struct wimlib_lzx_compressor_params slow_params = {
439 .size = sizeof(struct wimlib_lzx_compressor_params),
441 .algorithm = WIMLIB_LZX_ALGORITHM_SLOW,
444 .use_len2_matches = 1,
445 .num_fast_bytes = 96,
446 .num_optim_passes = 4,
447 .max_search_depth = 100,
448 .max_matches_per_pos = 10,
449 .main_nostat_cost = 15,
450 .len_nostat_cost = 15,
451 .aligned_nostat_cost = 7,
455 ret = wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
458 imagex_error(T("Couldn't set slow compression parameters.!"));
463 const tchar **strings;
464 unsigned num_strings;
465 unsigned num_alloc_strings;
468 #define STRING_SET_INITIALIZER \
469 { .strings = NULL, .num_strings = 0, .num_alloc_strings = 0, }
471 #define STRING_SET(_strings) \
472 struct string_set _strings = STRING_SET_INITIALIZER
475 string_set_append(struct string_set *set, const tchar *glob)
477 unsigned num_alloc_strings = set->num_alloc_strings;
479 if (set->num_strings == num_alloc_strings) {
480 const tchar **new_strings;
482 num_alloc_strings += 4;
483 new_strings = realloc(set->strings,
484 sizeof(set->strings[0]) * num_alloc_strings);
486 imagex_error(T("Out of memory!"));
489 set->strings = new_strings;
490 set->num_alloc_strings = num_alloc_strings;
492 set->strings[set->num_strings++] = glob;
497 string_set_destroy(struct string_set *set)
503 wim_reference_globs(WIMStruct *wim, struct string_set *set, int open_flags)
505 return wimlib_reference_resource_files(wim, set->strings,
507 WIMLIB_REF_FLAG_GLOB_ENABLE,
509 imagex_progress_func);
513 do_resource_not_found_warning(const tchar *wimfile,
514 const struct wimlib_wim_info *info,
515 const struct string_set *refglobs)
517 if (info->total_parts > 1) {
518 if (refglobs->num_strings == 0) {
519 imagex_error(T("\"%"TS"\" is part of a split WIM. "
520 "Use --ref to specify the other parts."),
523 imagex_error(T("Perhaps the '--ref' argument did not "
524 "specify all other parts of the split "
528 imagex_error(T("If this is a delta WIM, use the --ref argument "
529 "to specify the WIM(s) on which it is based."));
533 /* Returns the size of a file given its name, or -1 if the file does not exist
534 * or its size cannot be determined. */
536 file_get_size(const tchar *filename)
539 if (tstat(filename, &st) == 0)
546 PARSE_STRING_SUCCESS = 0,
547 PARSE_STRING_FAILURE = 1,
548 PARSE_STRING_NONE = 2,
552 * Parses a string token from an array of characters.
554 * Tokens are either whitespace-delimited, or double or single-quoted.
556 * @line_p: Pointer to the pointer to the line of data. Will be updated
557 * to point past the string token iff the return value is
558 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
561 * @len_p: @len_p initially stores the length of the line of data, which may
562 * be 0, and it will be updated to the number of bytes remaining in
563 * the line iff the return value is PARSE_STRING_SUCCESS.
565 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
566 * parsed string token will be returned here.
568 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
569 * PARSE_STRING_FAILURE if the data was invalid due to a missing
570 * closing quote; or PARSE_STRING_NONE if the line ended before the
571 * beginning of a string token was found.
574 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
577 tchar *line = *line_p;
581 /* Skip leading whitespace */
584 return PARSE_STRING_NONE;
585 if (!istspace(*line) && *line != T('\0'))
591 if (quote_char == T('"') || quote_char == T('\'')) {
596 line = tmemchr(line, quote_char, len);
598 imagex_error(T("Missing closing quote: %"TS), fn - 1);
599 return PARSE_STRING_FAILURE;
602 /* Unquoted string. Go until whitespace. Line is terminated
603 * by '\0', so no need to check 'len'. */
607 } while (!istspace(*line) && *line != T('\0'));
614 return PARSE_STRING_SUCCESS;
617 /* Parses a line of data (not an empty line or comment) in the source list file
618 * format. (See the man page for 'wimlib-imagex capture' for details on this
619 * format and the meaning.)
621 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
622 * len == 0. The data in @line will be modified by this function call.
624 * @len: Length of the line of data.
626 * @source: On success, the capture source and target described by the line is
627 * written into this destination. Note that it will contain pointers
628 * to data in the @line array.
630 * Returns true if the line was valid; false otherwise. */
632 parse_source_list_line(tchar *line, size_t len,
633 struct wimlib_capture_source *source)
637 ret = parse_string(&line, &len, &source->fs_source_path);
638 if (ret != PARSE_STRING_SUCCESS)
640 ret = parse_string(&line, &len, &source->wim_target_path);
641 if (ret == PARSE_STRING_NONE)
642 source->wim_target_path = source->fs_source_path;
643 return ret != PARSE_STRING_FAILURE;
646 /* Returns %true if the given line of length @len > 0 is a comment or empty line
647 * in the source list file format. */
649 is_comment_line(const tchar *line, size_t len)
654 if (!istspace(*line) && *line != T('\0'))
664 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
667 tchar *contents = *contents_p;
668 size_t nchars = *nchars_p;
671 for (i = 0; i < nchars; i++)
672 if (contents[i] == T('\n'))
675 /* Handle last line not terminated by a newline */
676 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
677 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
679 imagex_error(T("Out of memory!"));
682 contents[nchars] = T('\n');
683 *contents_p = contents;
691 /* Parses a file in the source list format. (See the man page for
692 * 'wimlib-imagex capture' for details on this format and the meaning.)
694 * @source_list_contents: Contents of the source list file. Note that this
695 * buffer will be modified to save memory allocations,
696 * and cannot be freed until the returned array of
697 * wimlib_capture_source's has also been freed.
699 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
702 * @nsources_ret: On success, the length of the returned array is
705 * Returns: An array of `struct wimlib_capture_source's that can be passed to
706 * the wimlib_add_image_multisource() function to specify how a WIM image is to
708 static struct wimlib_capture_source *
709 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
710 size_t *nsources_ret)
714 struct wimlib_capture_source *sources;
717 nlines = text_file_count_lines(source_list_contents_p,
718 &source_list_nchars);
722 /* Always allocate at least 1 slot, just in case the implementation of
723 * calloc() returns NULL if 0 bytes are requested. */
724 sources = calloc(nlines ?: 1, sizeof(*sources));
726 imagex_error(T("out of memory"));
729 p = *source_list_contents_p;
731 for (i = 0; i < nlines; i++) {
732 /* XXX: Could use rawmemchr() here instead, but it may not be
733 * available on all platforms. */
734 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
735 size_t len = endp - p + 1;
737 if (!is_comment_line(p, len)) {
738 if (!parse_source_list_line(p, len, &sources[j++])) {
751 enum capture_config_section {
752 CAPTURE_CONFIG_NO_SECTION,
753 CAPTURE_CONFIG_EXCLUSION_SECTION,
754 CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
755 CAPTURE_CONFIG_IGNORE_SECTION,
759 CAPTURE_CONFIG_INVALID_SECTION,
760 CAPTURE_CONFIG_CHANGED_SECTION,
761 CAPTURE_CONFIG_SAME_SECTION,
765 check_config_section(tchar *line, size_t len,
766 enum capture_config_section *cur_section)
768 while (istspace(*line))
772 return CAPTURE_CONFIG_SAME_SECTION;
775 tchar *endbrace = tstrrchr(line, T(']'));
777 return CAPTURE_CONFIG_SAME_SECTION;
779 if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
780 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
781 } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
782 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
783 } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
784 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
785 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
786 "of capture config file\n"),
788 } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
789 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
790 tfputs(T("WARNING: Ignoring [AlignmentList] section "
791 "of capture config file\n"),
794 imagex_error(T("Invalid capture config file section \"%"TS"\""),
796 return CAPTURE_CONFIG_INVALID_SECTION;
798 return CAPTURE_CONFIG_CHANGED_SECTION;
803 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
806 if (pat_list->num_pats == pat_list->num_allocated_pats) {
808 size_t num_allocated_pats = pat_list->num_pats + 8;
810 pats = realloc(pat_list->pats,
811 num_allocated_pats * sizeof(pat_list->pats[0]));
813 imagex_error(T("Out of memory!"));
816 pat_list->pats = pats;
817 pat_list->num_allocated_pats = num_allocated_pats;
819 pat_list->pats[pat_list->num_pats++] = pat;
824 parse_capture_config_line(tchar *line, size_t len,
825 enum capture_config_section *cur_section,
826 struct wimlib_capture_config *config)
831 ret = check_config_section(line, len, cur_section);
832 if (ret == CAPTURE_CONFIG_INVALID_SECTION)
834 if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
837 switch (*cur_section) {
838 case CAPTURE_CONFIG_NO_SECTION:
839 imagex_error(T("Line \"%"TS"\" is not in a section "
840 "(such as [ExclusionList]"), line);
842 case CAPTURE_CONFIG_EXCLUSION_SECTION:
843 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
845 return pattern_list_add_pattern(&config->exclusion_pats,
847 case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
848 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
850 return pattern_list_add_pattern(&config->exclusion_exception_pats,
852 case CAPTURE_CONFIG_IGNORE_SECTION:
859 parse_capture_config(tchar **contents_p, size_t nchars,
860 struct wimlib_capture_config *config)
865 enum capture_config_section cur_section;
867 memset(config, 0, sizeof(*config));
869 nlines = text_file_count_lines(contents_p, &nchars);
873 cur_section = CAPTURE_CONFIG_NO_SECTION;
875 for (i = 0; i < nlines; i++) {
876 tchar *endp = tmemchr(p, T('\n'), nchars);
877 size_t len = endp - p + 1;
879 if (!is_comment_line(p, len))
880 if (!parse_capture_config_line(p, len, &cur_section, config))
888 /* Reads the contents of a file into memory. */
890 file_get_contents(const tchar *filename, size_t *len_ret)
897 if (tstat(filename, &stbuf) != 0) {
898 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
903 fp = tfopen(filename, T("rb"));
905 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
909 buf = malloc(len ? len : 1);
911 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
912 "contents of file \"%"TS"\""), len, filename);
915 if (fread(buf, 1, len, fp) != len) {
916 imagex_error_with_errno(T("Failed to read %zu bytes from the "
917 "file \"%"TS"\""), len, filename);
931 /* Read standard input until EOF and return the full contents in a malloc()ed
932 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
935 stdin_get_contents(size_t *len_ret)
937 /* stdin can, of course, be a pipe or other non-seekable file, so the
938 * total length of the data cannot be pre-determined */
940 size_t newlen = 1024;
944 char *p = realloc(buf, newlen);
945 size_t bytes_read, bytes_to_read;
947 imagex_error(T("out of memory while reading stdin"));
951 bytes_to_read = newlen - pos;
952 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
954 if (bytes_read != bytes_to_read) {
959 imagex_error_with_errno(T("error reading stdin"));
973 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
976 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
978 *num_tchars_ret = num_bytes;
980 #else /* !__WIN32__ */
981 /* On Windows, translate the text to UTF-16LE */
985 if (num_bytes >= 2 &&
986 (((unsigned char)text[0] == 0xff && (unsigned char)text[1] == 0xfe) ||
987 ((unsigned char)text[0] <= 0x7f && (unsigned char)text[1] == 0x00)))
989 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
990 * with something that looks like an ASCII character encoded as
991 * a UTF-16LE code unit. Assume the file is encoded as
992 * UTF-16LE. This is not a 100% reliable check. */
993 num_wchars = num_bytes / 2;
994 text_wstr = (wchar_t*)text;
996 /* File does not look like UTF-16LE. Assume it is encoded in
997 * the current Windows code page. I think these are always
998 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
999 * should work as expected. */
1000 text_wstr = win32_mbs_to_wcs(text,
1005 *num_tchars_ret = num_wchars;
1007 #endif /* __WIN32__ */
1011 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
1016 contents = file_get_contents(filename, &num_bytes);
1019 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
1023 stdin_get_text_contents(size_t *num_tchars_ret)
1028 contents = stdin_get_contents(&num_bytes);
1031 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
1034 #define TO_PERCENT(numerator, denominator) \
1035 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
1037 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
1038 #define MEBIBYTE_MIN_NBYTES 10000000ULL
1039 #define KIBIBYTE_MIN_NBYTES 10000ULL
1042 get_unit(uint64_t total_bytes, const tchar **name_ret)
1044 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
1045 *name_ret = T("GiB");
1047 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
1048 *name_ret = T("MiB");
1050 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
1051 *name_ret = T("KiB");
1054 *name_ret = T("bytes");
1059 static struct wimlib_progress_info_scan last_scan_progress;
1062 report_scan_progress(const struct wimlib_progress_info_scan *scan, bool done)
1064 uint64_t prev_count, cur_count;
1066 prev_count = last_scan_progress.num_nondirs_scanned +
1067 last_scan_progress.num_dirs_scanned;
1068 cur_count = scan->num_nondirs_scanned + scan->num_dirs_scanned;
1070 if (done || prev_count == 0 || cur_count >= prev_count + 100 ||
1071 cur_count % 128 == 0)
1073 unsigned unit_shift;
1074 const tchar *unit_name;
1076 unit_shift = get_unit(scan->num_bytes_scanned, &unit_name);
1077 imagex_printf(T("\r%"PRIu64" %"TS" scanned (%"PRIu64" files, "
1078 "%"PRIu64" directories)"),
1079 scan->num_bytes_scanned >> unit_shift,
1081 scan->num_nondirs_scanned,
1082 scan->num_dirs_scanned);
1083 last_scan_progress = *scan;
1087 /* Progress callback function passed to various wimlib functions. */
1089 imagex_progress_func(enum wimlib_progress_msg msg,
1090 const union wimlib_progress_info *info)
1092 unsigned percent_done;
1093 unsigned unit_shift;
1094 const tchar *unit_name;
1096 if (imagex_be_quiet)
1099 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1100 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1101 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1102 info->write_streams.total_bytes);
1104 if (info->write_streams.completed_streams == 0) {
1105 imagex_printf(T("Writing %"TS"-compressed data using %u thread%"TS"\n"),
1106 wimlib_get_compression_type_string(info->write_streams.compression_type),
1107 info->write_streams.num_threads,
1108 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1110 if (info->write_streams.total_parts <= 1) {
1111 imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1112 "written (%u%% done)"),
1113 info->write_streams.completed_bytes >> unit_shift,
1115 info->write_streams.total_bytes >> unit_shift,
1119 imagex_printf(T("\rWriting resources from part %u of %u: "
1120 "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written"),
1121 (info->write_streams.completed_parts ==
1122 info->write_streams.total_parts) ?
1123 info->write_streams.completed_parts :
1124 info->write_streams.completed_parts + 1,
1125 info->write_streams.total_parts,
1126 info->write_streams.completed_bytes >> unit_shift,
1128 info->write_streams.total_bytes >> unit_shift,
1132 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1133 imagex_printf(T("\n"));
1135 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1136 imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
1137 if (*info->scan.wim_target_path) {
1138 imagex_printf(T(" (loading as WIM path: "
1139 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
1140 info->scan.wim_target_path);
1142 imagex_printf(T("\n"));
1144 memset(&last_scan_progress, 0, sizeof(last_scan_progress));
1146 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1147 switch (info->scan.status) {
1148 case WIMLIB_SCAN_DENTRY_OK:
1149 report_scan_progress(&info->scan, false);
1151 case WIMLIB_SCAN_DENTRY_EXCLUDED:
1152 imagex_printf(T("\nExcluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1154 case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
1155 imagex_printf(T("\nWARNING: Excluding unsupported file or directory\n"
1156 " \"%"TS"\" from capture\n"), info->scan.cur_path);
1160 case WIMLIB_PROGRESS_MSG_SCAN_END:
1161 report_scan_progress(&info->scan, true);
1162 imagex_printf(T("\n"));
1164 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1165 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1166 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1167 info->integrity.total_bytes);
1168 imagex_printf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1169 "of %"PRIu64" %"TS" (%u%%) done"),
1170 info->integrity.filename,
1171 info->integrity.completed_bytes >> unit_shift,
1173 info->integrity.total_bytes >> unit_shift,
1176 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1177 imagex_printf(T("\n"));
1179 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1180 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1181 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1182 info->integrity.total_bytes);
1183 imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1184 "of %"PRIu64" %"TS" (%u%%) done"),
1185 info->integrity.completed_bytes >> unit_shift,
1187 info->integrity.total_bytes >> unit_shift,
1190 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1191 imagex_printf(T("\n"));
1193 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1194 imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1195 "to %"TS" \"%"TS"\"\n"),
1196 info->extract.image,
1197 info->extract.image_name,
1198 info->extract.wimfile_name,
1199 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1200 T("NTFS volume") : T("directory")),
1201 info->extract.target);
1203 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1204 imagex_printf(T("Extracting "
1205 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\" from image %d (\"%"TS"\") "
1206 "in \"%"TS"\" to \"%"TS"\"\n"),
1207 info->extract.extract_root_wim_source_path,
1208 info->extract.image,
1209 info->extract.image_name,
1210 info->extract.wimfile_name,
1211 info->extract.target);
1213 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1214 percent_done = TO_PERCENT(info->extract.completed_bytes,
1215 info->extract.total_bytes);
1216 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1217 imagex_printf(T("\rExtracting files: "
1218 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1219 info->extract.completed_bytes >> unit_shift,
1221 info->extract.total_bytes >> unit_shift,
1224 if (info->extract.completed_bytes >= info->extract.total_bytes)
1225 imagex_printf(T("\n"));
1227 case WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN:
1228 if (info->extract.total_parts != 1) {
1229 imagex_printf(T("\nReading split pipable WIM part %u of %u\n"),
1230 info->extract.part_number,
1231 info->extract.total_parts);
1234 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1235 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1236 imagex_printf(T("Setting timestamps on all extracted files...\n"));
1238 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1239 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1240 imagex_printf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1241 info->extract.target);
1244 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1245 percent_done = TO_PERCENT(info->split.completed_bytes,
1246 info->split.total_bytes);
1247 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1248 imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
1249 "%"PRIu64" %"TS" (%u%%) written\n"),
1250 info->split.part_name,
1251 info->split.cur_part_number,
1252 info->split.total_parts,
1253 info->split.completed_bytes >> unit_shift,
1255 info->split.total_bytes >> unit_shift,
1259 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1260 if (info->split.completed_bytes == info->split.total_bytes) {
1261 imagex_printf(T("Finished writing split WIM part %u of %u\n"),
1262 info->split.cur_part_number,
1263 info->split.total_parts);
1266 case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1267 switch (info->update.command->op) {
1268 case WIMLIB_UPDATE_OP_DELETE:
1269 imagex_printf(T("Deleted WIM path "
1270 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1271 info->update.command->delete_.wim_path);
1273 case WIMLIB_UPDATE_OP_RENAME:
1274 imagex_printf(T("Renamed WIM path "
1275 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
1276 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1277 info->update.command->rename.wim_source_path,
1278 info->update.command->rename.wim_target_path);
1280 case WIMLIB_UPDATE_OP_ADD:
1288 fflush(imagex_info_file);
1293 parse_num_threads(const tchar *optarg)
1296 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1297 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1298 imagex_error(T("Number of threads must be a non-negative integer!"));
1305 static uint32_t parse_chunk_size(const tchar *optarg)
1308 unsigned long chunk_size = tstrtoul(optarg, &tmp, 10);
1309 if (chunk_size >= UINT32_MAX || *tmp || tmp == optarg) {
1310 imagex_error(T("Chunk size must be a non-negative integer!"));
1319 * Parse an option passed to an update command.
1321 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1324 * @option: Text string for the option (beginning with --)
1326 * @cmd: `struct wimlib_update_command' that is being constructed for
1329 * Returns true if the option was recognized; false if not.
1332 update_command_add_option(int op, const tchar *option,
1333 struct wimlib_update_command *cmd)
1335 bool recognized = true;
1337 case WIMLIB_UPDATE_OP_ADD:
1338 if (!tstrcmp(option, T("--verbose")))
1339 cmd->add.add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
1340 else if (!tstrcmp(option, T("--unix-data")))
1341 cmd->add.add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
1342 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1343 cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
1344 else if (!tstrcmp(option, T("--strict-acls")))
1345 cmd->add.add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
1346 else if (!tstrcmp(option, T("--dereference")))
1347 cmd->add.add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
1351 case WIMLIB_UPDATE_OP_DELETE:
1352 if (!tstrcmp(option, T("--force")))
1353 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1354 else if (!tstrcmp(option, T("--recursive")))
1355 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1366 /* How many nonoption arguments each `imagex update' command expects */
1367 static const unsigned update_command_num_nonoptions[] = {
1368 [WIMLIB_UPDATE_OP_ADD] = 2,
1369 [WIMLIB_UPDATE_OP_DELETE] = 1,
1370 [WIMLIB_UPDATE_OP_RENAME] = 2,
1374 update_command_add_nonoption(int op, const tchar *nonoption,
1375 struct wimlib_update_command *cmd,
1376 unsigned num_nonoptions)
1379 case WIMLIB_UPDATE_OP_ADD:
1380 if (num_nonoptions == 0)
1381 cmd->add.fs_source_path = (tchar*)nonoption;
1383 cmd->add.wim_target_path = (tchar*)nonoption;
1385 case WIMLIB_UPDATE_OP_DELETE:
1386 cmd->delete_.wim_path = (tchar*)nonoption;
1388 case WIMLIB_UPDATE_OP_RENAME:
1389 if (num_nonoptions == 0)
1390 cmd->rename.wim_source_path = (tchar*)nonoption;
1392 cmd->rename.wim_target_path = (tchar*)nonoption;
1398 * Parse a command passed on stdin to `imagex update'.
1400 * @line: Text of the command.
1401 * @len: Length of the line, including a null terminator
1404 * @command: A `struct wimlib_update_command' to fill in from the parsed
1407 * @line_number: Line number of the command, for diagnostics.
1409 * Returns true on success; returns false on parse error.
1412 parse_update_command(tchar *line, size_t len,
1413 struct wimlib_update_command *command,
1417 tchar *command_name;
1419 size_t num_nonoptions;
1421 /* Get the command name ("add", "delete", "rename") */
1422 ret = parse_string(&line, &len, &command_name);
1423 if (ret != PARSE_STRING_SUCCESS)
1426 if (!tstrcasecmp(command_name, T("add"))) {
1427 op = WIMLIB_UPDATE_OP_ADD;
1428 } else if (!tstrcasecmp(command_name, T("delete"))) {
1429 op = WIMLIB_UPDATE_OP_DELETE;
1430 } else if (!tstrcasecmp(command_name, T("rename"))) {
1431 op = WIMLIB_UPDATE_OP_RENAME;
1433 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1434 command_name, line_number);
1439 /* Parse additional options and non-options as needed */
1444 ret = parse_string(&line, &len, &next_string);
1445 if (ret == PARSE_STRING_NONE) /* End of line */
1447 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1449 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1451 if (!update_command_add_option(op, next_string, command))
1453 imagex_error(T("Unrecognized option \"%"TS"\" to "
1454 "update command \"%"TS"\" on line %zu"),
1455 next_string, command_name, line_number);
1461 if (num_nonoptions == update_command_num_nonoptions[op])
1463 imagex_error(T("Unexpected argument \"%"TS"\" in "
1464 "update command on line %zu\n"
1465 " (The \"%"TS"\" command only "
1466 "takes %zu nonoption arguments!)\n"),
1467 next_string, line_number,
1468 command_name, num_nonoptions);
1471 update_command_add_nonoption(op, next_string,
1472 command, num_nonoptions);
1477 if (num_nonoptions != update_command_num_nonoptions[op]) {
1478 imagex_error(T("Not enough arguments to update command "
1479 "\"%"TS"\" on line %zu"), command_name, line_number);
1485 static struct wimlib_update_command *
1486 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1487 size_t *num_cmds_ret)
1491 struct wimlib_update_command *cmds;
1494 nlines = text_file_count_lines(cmd_file_contents_p,
1499 /* Always allocate at least 1 slot, just in case the implementation of
1500 * calloc() returns NULL if 0 bytes are requested. */
1501 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1503 imagex_error(T("out of memory"));
1506 p = *cmd_file_contents_p;
1508 for (i = 0; i < nlines; i++) {
1509 /* XXX: Could use rawmemchr() here instead, but it may not be
1510 * available on all platforms. */
1511 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1512 size_t len = endp - p + 1;
1514 if (!is_comment_line(p, len)) {
1515 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1526 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1527 * one image from a WIM file to a NTFS volume. */
1529 imagex_apply(int argc, tchar **argv, int cmd)
1533 int image = WIMLIB_NO_IMAGE;
1535 struct wimlib_wim_info info;
1537 const tchar *wimfile;
1538 const tchar *target;
1539 const tchar *image_num_or_name = NULL;
1540 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1542 STRING_SET(refglobs);
1544 for_opt(c, apply_options) {
1546 case IMAGEX_CHECK_OPTION:
1547 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1549 case IMAGEX_HARDLINK_OPTION:
1550 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1552 case IMAGEX_SYMLINK_OPTION:
1553 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1555 case IMAGEX_VERBOSE_OPTION:
1556 /* No longer does anything. */
1558 case IMAGEX_REF_OPTION:
1559 ret = string_set_append(&refglobs, optarg);
1561 goto out_free_refglobs;
1563 case IMAGEX_UNIX_DATA_OPTION:
1564 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1566 case IMAGEX_NO_ACLS_OPTION:
1567 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1569 case IMAGEX_STRICT_ACLS_OPTION:
1570 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1572 case IMAGEX_NORPFIX_OPTION:
1573 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1575 case IMAGEX_RPFIX_OPTION:
1576 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1578 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1579 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1580 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1582 case IMAGEX_RESUME_OPTION:
1583 extract_flags |= WIMLIB_EXTRACT_FLAG_RESUME;
1591 if (argc != 2 && argc != 3)
1596 if (!tstrcmp(wimfile, T("-"))) {
1597 /* Attempt to apply pipable WIM from standard input. */
1599 image_num_or_name = NULL;
1602 image_num_or_name = argv[1];
1607 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1608 imagex_progress_func);
1610 goto out_free_refglobs;
1612 wimlib_get_wim_info(wim, &info);
1615 /* Image explicitly specified. */
1616 image_num_or_name = argv[1];
1617 image = wimlib_resolve_image(wim, image_num_or_name);
1618 ret = verify_image_exists(image, image_num_or_name, wimfile);
1620 goto out_wimlib_free;
1623 /* No image specified; default to image 1, but only if the WIM
1624 * contains exactly one image. */
1626 if (info.image_count != 1) {
1627 imagex_error(T("\"%"TS"\" contains %d images; "
1628 "Please select one (or all)."),
1629 wimfile, info.image_count);
1638 if (refglobs.num_strings) {
1640 imagex_error(T("Can't specify --ref when applying from stdin!"));
1642 goto out_wimlib_free;
1644 ret = wim_reference_globs(wim, &refglobs, open_flags);
1646 goto out_wimlib_free;
1651 /* Interpret a regular file or block device target as a NTFS
1655 if (tstat(target, &stbuf)) {
1656 if (errno != ENOENT) {
1657 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1660 goto out_wimlib_free;
1663 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1664 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1670 ret = wimlib_extract_image(wim, image, target, extract_flags,
1671 imagex_progress_func);
1673 set_fd_to_binary_mode(STDIN_FILENO);
1674 ret = wimlib_extract_image_from_pipe(STDIN_FILENO,
1676 target, extract_flags,
1677 imagex_progress_func);
1680 imagex_printf(T("Done applying WIM image.\n"));
1681 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
1683 do_resource_not_found_warning(wimfile, &info, &refglobs);
1685 imagex_error(T( "If you are applying an image "
1686 "from a split pipable WIM,\n"
1687 " make sure you have "
1688 "concatenated together all parts."));
1694 string_set_destroy(&refglobs);
1698 usage(CMD_APPLY, stderr);
1700 goto out_free_refglobs;
1703 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1704 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1705 * the desired image. 'wimlib-imagex append': add a new image to an existing
1708 imagex_capture_or_append(int argc, tchar **argv, int cmd)
1711 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1712 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE |
1713 WIMLIB_ADD_IMAGE_FLAG_WINCONFIG |
1714 WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1715 int write_flags = 0;
1716 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
1717 uint32_t chunk_size = UINT32_MAX;
1718 const tchar *wimfile;
1722 const tchar *flags_element = NULL;
1725 STRING_SET(base_wimfiles);
1726 WIMStruct **base_wims;
1728 WIMStruct *template_wim;
1729 const tchar *template_wimfile = NULL;
1730 const tchar *template_image_name_or_num = NULL;
1731 int template_image = WIMLIB_NO_IMAGE;
1734 unsigned num_threads = 0;
1739 const tchar *config_file = NULL;
1741 struct wimlib_capture_config *config;
1743 bool source_list = false;
1744 size_t source_list_nchars = 0;
1745 tchar *source_list_contents;
1746 bool capture_sources_malloced;
1747 struct wimlib_capture_source *capture_sources;
1749 bool name_defaulted;
1751 for_opt(c, capture_or_append_options) {
1753 case IMAGEX_BOOT_OPTION:
1754 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1756 case IMAGEX_CHECK_OPTION:
1757 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1758 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1760 case IMAGEX_NOCHECK_OPTION:
1761 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
1763 case IMAGEX_CONFIG_OPTION:
1764 config_file = optarg;
1765 add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1767 case IMAGEX_COMPRESS_OPTION:
1768 compression_type = get_compression_type(optarg);
1769 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1772 case IMAGEX_COMPRESS_SLOW_OPTION:
1773 ret = set_compress_slow();
1776 compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1778 case IMAGEX_CHUNK_SIZE_OPTION:
1779 chunk_size = parse_chunk_size(optarg);
1780 if (chunk_size == UINT32_MAX)
1783 case IMAGEX_PACK_STREAMS_OPTION:
1784 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
1786 case IMAGEX_FLAGS_OPTION:
1787 flags_element = optarg;
1789 case IMAGEX_DEREFERENCE_OPTION:
1790 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1792 case IMAGEX_VERBOSE_OPTION:
1793 /* No longer does anything. */
1795 case IMAGEX_THREADS_OPTION:
1796 num_threads = parse_num_threads(optarg);
1797 if (num_threads == UINT_MAX)
1800 case IMAGEX_REBUILD_OPTION:
1801 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1803 case IMAGEX_UNIX_DATA_OPTION:
1804 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1806 case IMAGEX_SOURCE_LIST_OPTION:
1809 case IMAGEX_NO_ACLS_OPTION:
1810 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1812 case IMAGEX_STRICT_ACLS_OPTION:
1813 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1815 case IMAGEX_RPFIX_OPTION:
1816 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1818 case IMAGEX_NORPFIX_OPTION:
1819 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1821 case IMAGEX_PIPABLE_OPTION:
1822 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1824 case IMAGEX_NOT_PIPABLE_OPTION:
1825 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
1827 case IMAGEX_UPDATE_OF_OPTION:
1828 if (template_image_name_or_num) {
1829 imagex_error(T("'--update-of' can only be "
1830 "specified one time!"));
1834 colon = tstrrchr(optarg, T(':'));
1837 template_wimfile = optarg;
1839 template_image_name_or_num = colon + 1;
1841 template_wimfile = NULL;
1842 template_image_name_or_num = optarg;
1846 case IMAGEX_DELTA_FROM_OPTION:
1847 if (cmd != CMD_CAPTURE) {
1848 imagex_error(T("'--delta-from' is only "
1849 "valid for capture!"));
1852 ret = string_set_append(&base_wimfiles, optarg);
1854 goto out_free_base_wimfiles;
1855 write_flags |= WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS;
1864 if (argc < 2 || argc > 4)
1870 /* Set default compression type. */
1871 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
1872 struct wimlib_lzx_compressor_params params;
1873 memset(¶ms, 0, sizeof(params));
1874 params.hdr.size = sizeof(params);
1875 params.algorithm = WIMLIB_LZX_ALGORITHM_FAST;
1876 params.use_defaults = 1;
1878 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
1880 compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1883 if (!tstrcmp(wimfile, T("-"))) {
1884 /* Writing captured WIM to standard output. */
1886 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
1887 imagex_error("Can't write a non-pipable WIM to "
1888 "standard output! Specify --pipable\n"
1889 " if you want to create a pipable WIM "
1890 "(but read the docs first).");
1894 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1896 if (cmd == CMD_APPEND) {
1897 imagex_error(T("Using standard output for append does "
1898 "not make sense."));
1901 wim_fd = STDOUT_FILENO;
1903 imagex_info_file = stderr;
1904 set_fd_to_binary_mode(wim_fd);
1907 /* If template image was specified using --update-of=IMAGE rather
1908 * than --update-of=WIMFILE:IMAGE, set the default WIMFILE. */
1909 if (template_image_name_or_num && !template_wimfile) {
1910 if (base_wimfiles.num_strings == 1) {
1911 /* Capturing delta WIM based on single WIM: default to
1913 template_wimfile = base_wimfiles.strings[0];
1914 } else if (cmd == CMD_APPEND) {
1915 /* Appending to WIM: default to WIM being appended to.
1917 template_wimfile = wimfile;
1919 /* Capturing a normal (non-delta) WIM, so the WIM file
1920 * *must* be explicitly specified. */
1921 if (base_wimfiles.num_strings > 1) {
1922 imagex_error(T("For capture of delta WIM "
1923 "based on multiple existing "
1925 " '--update-of' must "
1926 "specify WIMFILE:IMAGE!"));
1928 imagex_error(T("For capture of non-delta WIM, "
1929 "'--update-of' must specify "
1938 name_defaulted = false;
1940 /* Set default name to SOURCE argument, omitting any directory
1941 * prefixes and trailing slashes. This requires making a copy
1942 * of @source. Leave some free characters at the end in case we
1943 * append a number to keep the name unique. */
1944 size_t source_name_len;
1946 source_name_len = tstrlen(source);
1947 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1948 name = tbasename(tstrcpy(source_copy, source));
1949 name_defaulted = true;
1951 /* Image description defaults to NULL if not given. */
1958 /* Set up capture sources in source list mode */
1959 if (source[0] == T('-') && source[1] == T('\0')) {
1960 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1962 source_list_contents = file_get_text_contents(source,
1963 &source_list_nchars);
1965 if (!source_list_contents)
1968 capture_sources = parse_source_list(&source_list_contents,
1971 if (!capture_sources) {
1973 goto out_free_source_list_contents;
1975 capture_sources_malloced = true;
1977 /* Set up capture source in non-source-list mode. */
1978 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1979 capture_sources[0].fs_source_path = source;
1980 capture_sources[0].wim_target_path = NULL;
1981 capture_sources[0].reserved = 0;
1983 capture_sources_malloced = false;
1984 source_list_contents = NULL;
1988 /* Read and parse capture configuration file. */
1991 config_str = file_get_text_contents(config_file, &config_len);
1994 goto out_free_capture_sources;
1997 config = alloca(sizeof(*config));
1998 ret = parse_capture_config(&config_str, config_len, config);
2000 goto out_free_config;
2002 /* No capture configuration file specified; use default
2003 * configuration for capturing Windows operating systems. */
2005 add_image_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
2008 /* Open the existing WIM, or create a new one. */
2009 if (cmd == CMD_APPEND)
2010 ret = wimlib_open_wim(wimfile, open_flags, &wim,
2011 imagex_progress_func);
2013 ret = wimlib_create_new_wim(compression_type, &wim);
2015 goto out_free_config;
2017 /* Set chunk size if non-default. */
2018 if (chunk_size != UINT32_MAX) {
2019 ret = wimlib_set_output_chunk_size(wim, chunk_size);
2025 /* Detect if source is regular file or block device and set NTFS volume
2030 if (tstat(source, &stbuf) == 0) {
2031 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
2032 imagex_printf(T("Capturing WIM image from NTFS "
2033 "filesystem on \"%"TS"\"\n"), source);
2034 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
2037 if (errno != ENOENT) {
2038 imagex_error_with_errno(T("Failed to stat "
2039 "\"%"TS"\""), source);
2047 /* If the user did not specify an image name, and the basename of the
2048 * source already exists as an image name in the WIM file, append a
2049 * suffix to make it unique. */
2050 if (cmd == CMD_APPEND && name_defaulted) {
2051 unsigned long conflict_idx;
2052 tchar *name_end = tstrchr(name, T('\0'));
2053 for (conflict_idx = 1;
2054 wimlib_image_name_in_use(wim, name);
2057 tsprintf(name_end, T(" (%lu)"), conflict_idx);
2061 /* If capturing a delta WIM, reference resources from the base WIMs
2062 * before adding the new image. */
2063 if (base_wimfiles.num_strings) {
2064 base_wims = calloc(base_wimfiles.num_strings,
2065 sizeof(base_wims[0]));
2066 if (base_wims == NULL) {
2067 imagex_error(T("Out of memory!"));
2072 for (size_t i = 0; i < base_wimfiles.num_strings; i++) {
2073 ret = wimlib_open_wim(base_wimfiles.strings[i],
2074 open_flags, &base_wims[i],
2075 imagex_progress_func);
2077 goto out_free_base_wims;
2081 ret = wimlib_reference_resources(wim, base_wims,
2082 base_wimfiles.num_strings, 0);
2084 goto out_free_base_wims;
2086 if (base_wimfiles.num_strings == 1) {
2087 imagex_printf(T("Capturing delta WIM based on \"%"TS"\"\n"),
2088 base_wimfiles.strings[0]);
2090 imagex_printf(T("Capturing delta WIM based on %u WIMs\n"),
2091 base_wimfiles.num_strings);
2098 /* If capturing or appending as an update of an existing (template) image,
2099 * open the WIM if needed and parse the image index. */
2100 if (template_image_name_or_num) {
2103 if (base_wimfiles.num_strings == 1 &&
2104 template_wimfile == base_wimfiles.strings[0]) {
2105 template_wim = base_wims[0];
2106 } else if (template_wimfile == wimfile) {
2109 ret = wimlib_open_wim(template_wimfile, open_flags,
2110 &template_wim, imagex_progress_func);
2112 goto out_free_base_wims;
2115 template_image = wimlib_resolve_image(template_wim,
2116 template_image_name_or_num);
2118 if (template_image_name_or_num[0] == T('-')) {
2121 struct wimlib_wim_info info;
2123 wimlib_get_wim_info(template_wim, &info);
2124 n = tstrtoul(template_image_name_or_num + 1, &tmp, 10);
2125 if (n >= 1 && n <= info.image_count &&
2127 tmp != template_image_name_or_num + 1)
2129 template_image = info.image_count - (n - 1);
2132 ret = verify_image_exists_and_is_single(template_image,
2133 template_image_name_or_num,
2136 goto out_free_template_wim;
2138 template_wim = NULL;
2141 ret = wimlib_add_image_multisource(wim,
2147 imagex_progress_func);
2149 goto out_free_template_wim;
2151 if (desc || flags_element || template_image_name_or_num) {
2152 /* User provided <DESCRIPTION> or <FLAGS> element, or an image
2153 * on which the added one is to be based has been specified with
2154 * --update-of. Get the index of the image we just
2155 * added, then use it to call the appropriate functions. */
2156 struct wimlib_wim_info info;
2158 wimlib_get_wim_info(wim, &info);
2161 ret = wimlib_set_image_descripton(wim,
2165 goto out_free_template_wim;
2168 if (flags_element) {
2169 ret = wimlib_set_image_flags(wim, info.image_count,
2172 goto out_free_template_wim;
2175 /* Reference template image if the user provided one. */
2176 if (template_image_name_or_num) {
2177 imagex_printf(T("Using image %d "
2178 "from \"%"TS"\" as template\n"),
2179 template_image, template_wimfile);
2180 ret = wimlib_reference_template_image(wim,
2186 goto out_free_template_wim;
2190 /* Write the new WIM or overwrite the existing WIM with the new image
2192 if (cmd == CMD_APPEND) {
2193 ret = wimlib_overwrite(wim, write_flags, num_threads,
2194 imagex_progress_func);
2195 } else if (wimfile) {
2196 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
2197 write_flags, num_threads,
2198 imagex_progress_func);
2200 ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
2201 write_flags, num_threads,
2202 imagex_progress_func);
2204 out_free_template_wim:
2205 /* template_wim may alias base_wims[0] or wim. */
2206 if ((base_wimfiles.num_strings != 1 || template_wim != base_wims[0]) &&
2207 template_wim != wim)
2208 wimlib_free(template_wim);
2210 for (size_t i = 0; i < base_wimfiles.num_strings; i++)
2211 wimlib_free(base_wims[i]);
2217 free(config->exclusion_pats.pats);
2218 free(config->exclusion_exception_pats.pats);
2221 out_free_capture_sources:
2222 if (capture_sources_malloced)
2223 free(capture_sources);
2224 out_free_source_list_contents:
2225 free(source_list_contents);
2226 out_free_base_wimfiles:
2227 string_set_destroy(&base_wimfiles);
2234 goto out_free_base_wimfiles;
2237 /* Remove image(s) from a WIM. */
2239 imagex_delete(int argc, tchar **argv, int cmd)
2242 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2243 int write_flags = 0;
2244 const tchar *wimfile;
2245 const tchar *image_num_or_name;
2250 for_opt(c, delete_options) {
2252 case IMAGEX_CHECK_OPTION:
2253 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2254 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2256 case IMAGEX_SOFT_OPTION:
2257 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
2268 imagex_error(T("Must specify a WIM file"));
2270 imagex_error(T("Must specify an image"));
2274 image_num_or_name = argv[1];
2276 ret = wimlib_open_wim(wimfile, open_flags, &wim,
2277 imagex_progress_func);
2281 image = wimlib_resolve_image(wim, image_num_or_name);
2283 ret = verify_image_exists(image, image_num_or_name, wimfile);
2285 goto out_wimlib_free;
2287 ret = wimlib_delete_image(wim, image);
2289 imagex_error(T("Failed to delete image from \"%"TS"\""),
2291 goto out_wimlib_free;
2294 ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
2296 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2297 "deleted"), wimfile);
2305 usage(CMD_DELETE, stderr);
2311 print_full_path(const struct wimlib_dir_entry *wdentry, void *_ignore)
2313 int ret = tprintf(T("%"TS"\n"), wdentry->full_path);
2314 return (ret >= 0) ? 0 : -1;
2317 /* Print the files contained in an image(s) in a WIM file. */
2319 imagex_dir(int argc, tchar **argv, int cmd)
2321 const tchar *wimfile;
2322 WIMStruct *wim = NULL;
2325 const tchar *path = T("");
2328 for_opt(c, dir_options) {
2330 case IMAGEX_PATH_OPTION:
2341 imagex_error(T("Must specify a WIM file"));
2345 imagex_error(T("Too many arguments"));
2350 ret = wimlib_open_wim(wimfile, 0, &wim, imagex_progress_func);
2355 image = wimlib_resolve_image(wim, argv[1]);
2356 ret = verify_image_exists(image, argv[1], wimfile);
2358 goto out_wimlib_free;
2360 /* No image specified; default to image 1, but only if the WIM
2361 * contains exactly one image. */
2363 struct wimlib_wim_info info;
2365 wimlib_get_wim_info(wim, &info);
2366 if (info.image_count != 1) {
2367 imagex_error(T("\"%"TS"\" contains %d images; Please "
2368 "select one (or all)."),
2369 wimfile, info.image_count);
2376 ret = wimlib_iterate_dir_tree(wim, image, path,
2377 WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE,
2378 print_full_path, NULL);
2385 usage(CMD_DIR, stderr);
2390 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2393 imagex_export(int argc, tchar **argv, int cmd)
2397 int export_flags = 0;
2398 int write_flags = 0;
2399 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2400 const tchar *src_wimfile;
2401 const tchar *src_image_num_or_name;
2402 const tchar *dest_wimfile;
2404 const tchar *dest_name;
2405 const tchar *dest_desc;
2407 struct wimlib_wim_info src_info;
2408 WIMStruct *dest_wim;
2413 STRING_SET(refglobs);
2414 unsigned num_threads = 0;
2415 uint32_t chunk_size = UINT32_MAX;
2417 for_opt(c, export_options) {
2419 case IMAGEX_BOOT_OPTION:
2420 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2422 case IMAGEX_CHECK_OPTION:
2423 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2424 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2426 case IMAGEX_NOCHECK_OPTION:
2427 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2429 case IMAGEX_COMPRESS_OPTION:
2430 compression_type = get_compression_type(optarg);
2431 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2434 case IMAGEX_PACK_STREAMS_OPTION:
2435 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
2437 case IMAGEX_CHUNK_SIZE_OPTION:
2438 chunk_size = parse_chunk_size(optarg);
2439 if (chunk_size == UINT32_MAX)
2442 case IMAGEX_REF_OPTION:
2443 ret = string_set_append(&refglobs, optarg);
2445 goto out_free_refglobs;
2447 case IMAGEX_THREADS_OPTION:
2448 num_threads = parse_num_threads(optarg);
2449 if (num_threads == UINT_MAX)
2452 case IMAGEX_REBUILD_OPTION:
2453 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2455 case IMAGEX_PIPABLE_OPTION:
2456 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2458 case IMAGEX_NOT_PIPABLE_OPTION:
2459 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
2467 if (argc < 3 || argc > 5)
2469 src_wimfile = argv[0];
2470 src_image_num_or_name = argv[1];
2471 dest_wimfile = argv[2];
2472 dest_name = (argc >= 4) ? argv[3] : NULL;
2473 dest_desc = (argc >= 5) ? argv[4] : NULL;
2474 ret = wimlib_open_wim(src_wimfile, open_flags, &src_wim,
2475 imagex_progress_func);
2477 goto out_free_refglobs;
2479 wimlib_get_wim_info(src_wim, &src_info);
2481 /* Determine if the destination is an existing file or not. If so, we
2482 * try to append the exported image(s) to it; otherwise, we create a new
2483 * WIM containing the exported image(s). Furthermore, determine if we
2484 * need to write a pipable WIM directly to standard output. */
2486 if (tstrcmp(dest_wimfile, T("-")) == 0) {
2488 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2489 imagex_error("Can't write a non-pipable WIM to "
2490 "standard output! Specify --pipable\n"
2491 " if you want to create a pipable WIM "
2492 "(but read the docs first).");
2494 goto out_free_src_wim;
2497 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2499 dest_wimfile = NULL;
2500 dest_wim_fd = STDOUT_FILENO;
2501 imagex_info_file = stderr;
2502 set_fd_to_binary_mode(dest_wim_fd);
2505 if (dest_wimfile != NULL && tstat(dest_wimfile, &stbuf) == 0) {
2507 /* Destination file exists. */
2509 if (!S_ISREG(stbuf.st_mode)) {
2510 imagex_error(T("\"%"TS"\" is not a regular file"),
2513 goto out_free_src_wim;
2515 ret = wimlib_open_wim(dest_wimfile,
2516 open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2517 &dest_wim, imagex_progress_func);
2519 goto out_free_src_wim;
2521 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2522 /* The user specified a compression type, but we're
2523 * exporting to an existing WIM. Make sure the
2524 * specified compression type is the same as the
2525 * compression type of the existing destination WIM. */
2526 struct wimlib_wim_info dest_info;
2528 wimlib_get_wim_info(dest_wim, &dest_info);
2529 if (compression_type != dest_info.compression_type) {
2530 imagex_error(T("Cannot specify a compression type that is "
2531 "not the same as that used in the "
2532 "destination WIM"));
2534 goto out_free_dest_wim;
2540 if (errno != ENOENT) {
2541 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2544 goto out_free_src_wim;
2547 /* dest_wimfile is not an existing file, so create a new WIM. */
2549 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2550 /* The user did not specify a compression type; default
2551 * to that of the source WIM. */
2553 compression_type = src_info.compression_type;
2555 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2557 goto out_free_src_wim;
2559 /* Use same chunk size if compression type is the same. */
2560 if (compression_type == src_info.compression_type &&
2561 chunk_size == UINT32_MAX)
2562 wimlib_set_output_chunk_size(dest_wim, src_info.chunk_size);
2565 if (chunk_size != UINT32_MAX) {
2566 /* Set destination chunk size. */
2567 ret = wimlib_set_output_chunk_size(dest_wim, chunk_size);
2569 goto out_free_dest_wim;
2572 image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2573 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2575 goto out_free_dest_wim;
2577 if (refglobs.num_strings) {
2578 ret = wim_reference_globs(src_wim, &refglobs, open_flags);
2580 goto out_free_dest_wim;
2583 if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
2584 image == WIMLIB_ALL_IMAGES && src_info.boot_index == 0)
2586 imagex_error(T("--boot specified for all-images export, but source WIM "
2587 "has no bootable image."));
2589 goto out_free_dest_wim;
2592 ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2593 dest_desc, export_flags, imagex_progress_func);
2595 if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2596 do_resource_not_found_warning(src_wimfile,
2597 &src_info, &refglobs);
2599 goto out_free_dest_wim;
2603 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2604 imagex_progress_func);
2605 else if (dest_wimfile)
2606 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2607 write_flags, num_threads,
2608 imagex_progress_func);
2610 ret = wimlib_write_to_fd(dest_wim, dest_wim_fd,
2611 WIMLIB_ALL_IMAGES, write_flags,
2612 num_threads, imagex_progress_func);
2614 wimlib_free(dest_wim);
2616 wimlib_free(src_wim);
2618 string_set_destroy(&refglobs);
2622 usage(CMD_EXPORT, stderr);
2625 goto out_free_refglobs;
2629 is_root_wim_path(const tchar *path)
2632 for (p = path; *p; p++)
2633 if (*p != T('\\') && *p != T('/'))
2639 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2640 const tchar *dest_dir)
2642 for (size_t i = 0; i < num_cmds; i++)
2643 if (cmds[i].fs_dest_path != dest_dir)
2644 free(cmds[i].fs_dest_path);
2648 static struct wimlib_extract_command *
2649 prepare_extract_commands(tchar **paths, unsigned num_paths,
2650 int extract_flags, tchar *dest_dir,
2651 size_t *num_cmds_ret)
2653 struct wimlib_extract_command *cmds;
2655 tchar *emptystr = T("");
2657 if (num_paths == 0) {
2661 num_cmds = num_paths;
2662 cmds = calloc(num_cmds, sizeof(cmds[0]));
2664 imagex_error(T("Out of memory!"));
2668 for (size_t i = 0; i < num_cmds; i++) {
2669 cmds[i].extract_flags = extract_flags;
2670 cmds[i].wim_source_path = paths[i];
2671 if (is_root_wim_path(paths[i])) {
2672 cmds[i].fs_dest_path = dest_dir;
2674 size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2675 cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2676 if (!cmds[i].fs_dest_path) {
2677 free_extract_commands(cmds, num_cmds, dest_dir);
2680 tsprintf(cmds[i].fs_dest_path,
2681 T("%"TS""OS_PREFERRED_PATH_SEPARATOR_STRING"%"TS),
2682 dest_dir, tbasename(paths[i]));
2685 *num_cmds_ret = num_cmds;
2689 /* Extract files or directories from a WIM image */
2691 imagex_extract(int argc, tchar **argv, int cmd)
2698 const tchar *wimfile;
2699 const tchar *image_num_or_name;
2700 tchar *dest_dir = T(".");
2701 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL | WIMLIB_EXTRACT_FLAG_NORPFIX;
2703 STRING_SET(refglobs);
2705 struct wimlib_extract_command *cmds;
2708 for_opt(c, extract_options) {
2710 case IMAGEX_CHECK_OPTION:
2711 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2713 case IMAGEX_VERBOSE_OPTION:
2714 /* No longer does anything. */
2716 case IMAGEX_REF_OPTION:
2717 ret = string_set_append(&refglobs, optarg);
2719 goto out_free_refglobs;
2721 case IMAGEX_UNIX_DATA_OPTION:
2722 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2724 case IMAGEX_NO_ACLS_OPTION:
2725 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2727 case IMAGEX_STRICT_ACLS_OPTION:
2728 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2730 case IMAGEX_DEST_DIR_OPTION:
2733 case IMAGEX_TO_STDOUT_OPTION:
2734 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2735 imagex_info_file = stderr;
2736 imagex_be_quiet = true;
2738 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2739 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2740 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2753 image_num_or_name = argv[1];
2758 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2763 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2767 image = wimlib_resolve_image(wim, image_num_or_name);
2768 ret = verify_image_exists_and_is_single(image,
2772 goto out_wimlib_free;
2774 if (refglobs.num_strings) {
2775 ret = wim_reference_globs(wim, &refglobs, open_flags);
2777 goto out_wimlib_free;
2780 ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
2781 imagex_progress_func);
2783 if (!imagex_be_quiet)
2784 imagex_printf(T("Done extracting files.\n"));
2785 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2786 tfprintf(stderr, T("Note: You can use `%"TS"' to see what "
2787 "files and directories\n"
2788 " are in the WIM image.\n"),
2789 get_cmd_string(CMD_INFO, false));
2790 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2791 struct wimlib_wim_info info;
2793 wimlib_get_wim_info(wim, &info);
2794 do_resource_not_found_warning(wimfile, &info, &refglobs);
2799 free_extract_commands(cmds, num_cmds, dest_dir);
2801 string_set_destroy(&refglobs);
2805 usage(CMD_EXTRACT, stderr);
2808 goto out_free_refglobs;
2811 static void print_byte_field(const uint8_t field[], size_t len)
2814 tprintf(T("%02hhx"), *field++);
2818 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2820 tputs(T("WIM Information:"));
2821 tputs(T("----------------"));
2822 tprintf(T("Path: %"TS"\n"), wimfile);
2823 tprintf(T("GUID: 0x"));
2824 print_byte_field(info->guid, sizeof(info->guid));
2826 tprintf(T("Version: %u\n"), info->wim_version);
2827 tprintf(T("Image Count: %d\n"), info->image_count);
2828 tprintf(T("Compression: %"TS"\n"),
2829 wimlib_get_compression_type_string(info->compression_type));
2830 tprintf(T("Chunk Size: %"PRIu32" bytes\n"),
2832 tprintf(T("Part Number: %d/%d\n"), info->part_number, info->total_parts);
2833 tprintf(T("Boot Index: %d\n"), info->boot_index);
2834 tprintf(T("Size: %"PRIu64" bytes\n"), info->total_bytes);
2835 tprintf(T("Integrity Info: %"TS"\n"),
2836 info->has_integrity_table ? T("yes") : T("no"));
2837 tprintf(T("Relative path junction: %"TS"\n"),
2838 info->has_rpfix ? T("yes") : T("no"));
2839 tprintf(T("Pipable: %"TS"\n"),
2840 info->pipable ? T("yes") : T("no"));
2845 print_resource(const struct wimlib_resource_entry *resource,
2848 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
2849 resource->uncompressed_size);
2850 if (resource->packed) {
2851 tprintf(T("Raw compressed size = %"PRIu64" bytes\n"),
2852 resource->raw_resource_compressed_size);
2854 tprintf(T("Raw offset in WIM = %"PRIu64" bytes\n"),
2855 resource->raw_resource_offset_in_wim);
2857 tprintf(T("Offset in raw = %"PRIu64" bytes\n"),
2860 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
2861 resource->compressed_size);
2863 tprintf(T("Offset in WIM = %"PRIu64" bytes\n"),
2867 tprintf(T("Part Number = %u\n"), resource->part_number);
2868 tprintf(T("Reference Count = %u\n"), resource->reference_count);
2870 tprintf(T("Hash = 0x"));
2871 print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2874 tprintf(T("Flags = "));
2875 if (resource->is_compressed)
2876 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED "));
2877 if (resource->is_metadata)
2878 tprintf(T("WIM_RESHDR_FLAG_METADATA "));
2879 if (resource->is_free)
2880 tprintf(T("WIM_RESHDR_FLAG_FREE "));
2881 if (resource->is_spanned)
2882 tprintf(T("WIM_RESHDR_FLAG_SPANNED "));
2883 if (resource->packed)
2884 tprintf(T("WIM_RESHDR_FLAG_PACKED_STREAMS "));
2891 print_lookup_table(WIMStruct *wim)
2893 wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2896 /* Prints information about a WIM file; also can mark an image as bootable,
2897 * change the name of an image, or change the description of an image. */
2899 imagex_info(int argc, tchar **argv, int cmd)
2904 bool nocheck = false;
2905 bool header = false;
2906 bool lookup_table = false;
2908 bool metadata = false;
2909 bool short_header = true;
2910 const tchar *xml_out_file = NULL;
2911 const tchar *wimfile;
2912 const tchar *image_num_or_name;
2913 const tchar *new_name;
2914 const tchar *new_desc;
2919 struct wimlib_wim_info info;
2921 for_opt(c, info_options) {
2923 case IMAGEX_BOOT_OPTION:
2926 case IMAGEX_CHECK_OPTION:
2929 case IMAGEX_NOCHECK_OPTION:
2932 case IMAGEX_HEADER_OPTION:
2934 short_header = false;
2936 case IMAGEX_LOOKUP_TABLE_OPTION:
2937 lookup_table = true;
2938 short_header = false;
2940 case IMAGEX_XML_OPTION:
2942 short_header = false;
2944 case IMAGEX_EXTRACT_XML_OPTION:
2945 xml_out_file = optarg;
2946 short_header = false;
2948 case IMAGEX_METADATA_OPTION:
2950 short_header = false;
2959 if (argc < 1 || argc > 4)
2963 image_num_or_name = (argc >= 2) ? argv[1] : T("all");
2964 new_name = (argc >= 3) ? argv[2] : NULL;
2965 new_desc = (argc >= 4) ? argv[3] : NULL;
2967 if (check && nocheck) {
2968 imagex_error(T("Can't specify both --check and --nocheck"));
2973 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2975 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2979 wimlib_get_wim_info(wim, &info);
2981 image = wimlib_resolve_image(wim, image_num_or_name);
2982 ret = WIMLIB_ERR_INVALID_IMAGE;
2983 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
2984 verify_image_exists(image, image_num_or_name, wimfile);
2986 imagex_error(T("If you would like to set the boot "
2987 "index to 0, specify image \"0\" with "
2988 "the --boot flag."));
2990 goto out_wimlib_free;
2993 if (boot && info.image_count == 0) {
2994 imagex_error(T("--boot is meaningless on a WIM with no images"));
2995 goto out_wimlib_free;
2998 if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
3000 imagex_error(T("Cannot specify the --boot flag "
3001 "without specifying a specific "
3002 "image in a multi-image WIM"));
3003 goto out_wimlib_free;
3006 imagex_error(T("Cannot specify the NEW_NAME "
3007 "without specifying a specific "
3008 "image in a multi-image WIM"));
3009 goto out_wimlib_free;
3013 /* Operations that print information are separated from operations that
3014 * recreate the WIM file. */
3015 if (!new_name && !boot) {
3017 /* Read-only operations */
3019 if (image == WIMLIB_NO_IMAGE) {
3020 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
3021 image_num_or_name, wimfile);
3022 goto out_wimlib_free;
3025 if (image == WIMLIB_ALL_IMAGES && short_header)
3026 print_wim_information(wimfile, &info);
3029 wimlib_print_header(wim);
3032 if (info.total_parts != 1) {
3033 tfprintf(stderr, T("Warning: Only showing the lookup table "
3034 "for part %d of a %d-part WIM.\n"),
3035 info.part_number, info.total_parts);
3037 print_lookup_table(wim);
3041 ret = wimlib_extract_xml_data(wim, stdout);
3043 goto out_wimlib_free;
3049 fp = tfopen(xml_out_file, T("wb"));
3051 imagex_error_with_errno(T("Failed to open the "
3052 "file \"%"TS"\" for "
3056 goto out_wimlib_free;
3058 ret = wimlib_extract_xml_data(wim, fp);
3060 imagex_error(T("Failed to close the file "
3066 goto out_wimlib_free;
3070 wimlib_print_available_images(wim, image);
3073 ret = wimlib_print_metadata(wim, image);
3075 goto out_wimlib_free;
3080 /* Modification operations */
3082 if (image == WIMLIB_ALL_IMAGES)
3085 if (image == WIMLIB_NO_IMAGE && new_name) {
3086 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
3087 "when using image 0"), new_name);
3089 goto out_wimlib_free;
3093 if (image == info.boot_index) {
3094 imagex_printf(T("Image %d is already marked as "
3095 "bootable.\n"), image);
3098 imagex_printf(T("Marking image %d as bootable.\n"),
3100 info.boot_index = image;
3101 ret = wimlib_set_wim_info(wim, &info,
3102 WIMLIB_CHANGE_BOOT_INDEX);
3104 goto out_wimlib_free;
3108 if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
3110 imagex_printf(T("Image %d is already named \"%"TS"\".\n"),
3114 imagex_printf(T("Changing the name of image %d to "
3115 "\"%"TS"\".\n"), image, new_name);
3116 ret = wimlib_set_image_name(wim, image, new_name);
3118 goto out_wimlib_free;
3122 const tchar *old_desc;
3123 old_desc = wimlib_get_image_description(wim, image);
3124 if (old_desc && !tstrcmp(old_desc, new_desc)) {
3125 imagex_printf(T("The description of image %d is already "
3126 "\"%"TS"\".\n"), image, new_desc);
3129 imagex_printf(T("Changing the description of image %d "
3130 "to \"%"TS"\".\n"), image, new_desc);
3131 ret = wimlib_set_image_descripton(wim, image,
3134 goto out_wimlib_free;
3138 /* Only call wimlib_overwrite() if something actually needs to
3140 if (boot || new_name || new_desc ||
3141 (check && !info.has_integrity_table) ||
3142 (nocheck && info.has_integrity_table))
3144 int write_flags = 0;
3147 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3149 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3150 ret = wimlib_overwrite(wim, write_flags, 1,
3151 imagex_progress_func);
3153 imagex_printf(T("The file \"%"TS"\" was not modified "
3154 "because nothing needed to be done.\n"),
3165 usage(CMD_INFO, stderr);
3171 /* Join split WIMs into one part WIM */
3173 imagex_join(int argc, tchar **argv, int cmd)
3176 int swm_open_flags = 0;
3177 int wim_write_flags = 0;
3178 const tchar *output_path;
3181 for_opt(c, join_options) {
3183 case IMAGEX_CHECK_OPTION:
3184 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3185 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3195 imagex_error(T("Must specify one or more split WIM (.swm) "
3199 output_path = argv[0];
3200 ret = wimlib_join((const tchar * const *)++argv,
3205 imagex_progress_func);
3210 usage(CMD_JOIN, stderr);
3215 #if WIM_MOUNTING_SUPPORTED
3217 /* Mounts a WIM image. */
3219 imagex_mount_rw_or_ro(int argc, tchar **argv, int cmd)
3222 int mount_flags = 0;
3224 const tchar *staging_dir = NULL;
3225 const tchar *wimfile;
3228 struct wimlib_wim_info info;
3232 STRING_SET(refglobs);
3234 if (cmd == CMD_MOUNTRW) {
3235 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
3236 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3239 for_opt(c, mount_options) {
3241 case IMAGEX_ALLOW_OTHER_OPTION:
3242 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
3244 case IMAGEX_CHECK_OPTION:
3245 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3247 case IMAGEX_DEBUG_OPTION:
3248 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
3250 case IMAGEX_STREAMS_INTERFACE_OPTION:
3251 if (!tstrcasecmp(optarg, T("none")))
3252 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
3253 else if (!tstrcasecmp(optarg, T("xattr")))
3254 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
3255 else if (!tstrcasecmp(optarg, T("windows")))
3256 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
3258 imagex_error(T("Unknown stream interface \"%"TS"\""),
3263 case IMAGEX_REF_OPTION:
3264 ret = string_set_append(&refglobs, optarg);
3266 goto out_free_refglobs;
3268 case IMAGEX_STAGING_DIR_OPTION:
3269 staging_dir = optarg;
3271 case IMAGEX_UNIX_DATA_OPTION:
3272 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
3280 if (argc != 2 && argc != 3)
3285 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3287 goto out_free_refglobs;
3289 wimlib_get_wim_info(wim, &info);
3292 /* Image explicitly specified. */
3293 image = wimlib_resolve_image(wim, argv[1]);
3295 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
3299 /* No image specified; default to image 1, but only if the WIM
3300 * contains exactly one image. */
3302 if (info.image_count != 1) {
3303 imagex_error(T("\"%"TS"\" contains %d images; Please "
3304 "select one."), wimfile, info.image_count);
3312 if (refglobs.num_strings) {
3313 ret = wim_reference_globs(wim, &refglobs, open_flags);
3318 ret = wimlib_mount_image(wim, image, dir, mount_flags, staging_dir);
3320 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
3322 image, wimfile, dir);
3327 string_set_destroy(&refglobs);
3333 goto out_free_refglobs;
3335 #endif /* WIM_MOUNTING_SUPPORTED */
3337 /* Rebuild a WIM file */
3339 imagex_optimize(int argc, tchar **argv, int cmd)
3342 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3343 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
3344 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
3345 uint32_t chunk_size = UINT32_MAX;
3348 const tchar *wimfile;
3351 unsigned num_threads = 0;
3353 for_opt(c, optimize_options) {
3355 case IMAGEX_CHECK_OPTION:
3356 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3357 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3359 case IMAGEX_NOCHECK_OPTION:
3360 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3362 case IMAGEX_COMPRESS_OPTION:
3363 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3364 compression_type = get_compression_type(optarg);
3365 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
3368 case IMAGEX_RECOMPRESS_OPTION:
3369 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3371 case IMAGEX_COMPRESS_SLOW_OPTION:
3372 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3373 compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
3374 ret = set_compress_slow();
3378 case IMAGEX_CHUNK_SIZE_OPTION:
3379 chunk_size = parse_chunk_size(optarg);
3380 if (chunk_size == UINT32_MAX)
3383 case IMAGEX_PACK_STREAMS_OPTION:
3384 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
3385 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3387 case IMAGEX_THREADS_OPTION:
3388 num_threads = parse_num_threads(optarg);
3389 if (num_threads == UINT_MAX)
3392 case IMAGEX_PIPABLE_OPTION:
3393 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
3395 case IMAGEX_NOT_PIPABLE_OPTION:
3396 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
3410 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3414 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
3415 /* Change compression type. */
3416 ret = wimlib_set_output_compression_type(wim, compression_type);
3418 goto out_wimlib_free;
3421 if (chunk_size != UINT32_MAX) {
3422 /* Change chunk size. */
3423 ret = wimlib_set_output_chunk_size(wim, chunk_size);
3425 goto out_wimlib_free;
3428 old_size = file_get_size(wimfile);
3429 tprintf(T("\"%"TS"\" original size: "), wimfile);
3431 tputs(T("Unknown"));
3433 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3435 ret = wimlib_overwrite(wim, write_flags, num_threads,
3436 imagex_progress_func);
3438 imagex_error(T("Optimization of \"%"TS"\" failed."), wimfile);
3439 goto out_wimlib_free;
3442 new_size = file_get_size(wimfile);
3443 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3445 tputs(T("Unknown"));
3447 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3449 tfputs(T("Space saved: "), stdout);
3450 if (new_size != -1 && old_size != -1) {
3451 tprintf(T("%lld KiB\n"),
3452 ((long long)old_size - (long long)new_size) >> 10);
3454 tputs(T("Unknown"));
3463 usage(CMD_OPTIMIZE, stderr);
3469 /* Split a WIM into a spanned set */
3471 imagex_split(int argc, tchar **argv, int cmd)
3475 int write_flags = 0;
3476 unsigned long part_size;
3481 for_opt(c, split_options) {
3483 case IMAGEX_CHECK_OPTION:
3484 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3485 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3497 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3498 if (tmp == argv[2] || *tmp) {
3499 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3500 imagex_error(T("The part size must be an integer or "
3501 "floating-point number of megabytes."));
3504 ret = wimlib_open_wim(argv[0], open_flags, &wim, imagex_progress_func);
3508 ret = wimlib_split(wim, argv[1], part_size, write_flags, imagex_progress_func);
3514 usage(CMD_SPLIT, stderr);
3520 #if WIM_MOUNTING_SUPPORTED
3521 /* Unmounts a mounted WIM image. */
3523 imagex_unmount(int argc, tchar **argv, int cmd)
3526 int unmount_flags = 0;
3529 for_opt(c, unmount_options) {
3531 case IMAGEX_COMMIT_OPTION:
3532 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3534 case IMAGEX_CHECK_OPTION:
3535 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3537 case IMAGEX_REBUILD_OPTION:
3538 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3540 case IMAGEX_LAZY_OPTION:
3541 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3552 ret = wimlib_unmount_image(argv[0], unmount_flags,
3553 imagex_progress_func);
3555 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3560 usage(CMD_UNMOUNT, stderr);
3564 #endif /* WIM_MOUNTING_SUPPORTED */
3567 * Add, delete, or rename files in a WIM image.
3570 imagex_update(int argc, tchar **argv, int cmd)
3572 const tchar *wimfile;
3576 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3577 int write_flags = 0;
3578 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3579 int default_add_flags = WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
3580 WIMLIB_ADD_FLAG_VERBOSE;
3581 int default_delete_flags = 0;
3582 unsigned num_threads = 0;
3584 tchar *cmd_file_contents;
3585 size_t cmd_file_nchars;
3586 struct wimlib_update_command *cmds;
3588 tchar *command_str = NULL;
3590 const tchar *config_file = NULL;
3592 struct wimlib_capture_config *config;
3594 for_opt(c, update_options) {
3596 /* Generic or write options */
3597 case IMAGEX_THREADS_OPTION:
3598 num_threads = parse_num_threads(optarg);
3599 if (num_threads == UINT_MAX)
3602 case IMAGEX_CHECK_OPTION:
3603 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3604 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3606 case IMAGEX_REBUILD_OPTION:
3607 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3609 case IMAGEX_COMMAND_OPTION:
3611 imagex_error(T("--command may only be specified "
3612 "one time. Please provide\n"
3613 " the update commands "
3614 "on standard input instead."));
3617 command_str = tstrdup(optarg);
3619 imagex_error(T("Out of memory!"));
3623 /* Default delete options */
3624 case IMAGEX_FORCE_OPTION:
3625 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3627 case IMAGEX_RECURSIVE_OPTION:
3628 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3631 /* Global add option */
3632 case IMAGEX_CONFIG_OPTION:
3633 default_add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
3634 config_file = optarg;
3637 /* Default add options */
3638 case IMAGEX_VERBOSE_OPTION:
3639 /* No longer does anything. */
3641 case IMAGEX_DEREFERENCE_OPTION:
3642 default_add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
3644 case IMAGEX_UNIX_DATA_OPTION:
3645 default_add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
3647 case IMAGEX_NO_ACLS_OPTION:
3648 default_add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
3650 case IMAGEX_STRICT_ACLS_OPTION:
3651 default_add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
3660 if (argc != 1 && argc != 2)
3664 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3666 goto out_free_command_str;
3669 /* Image explicitly specified. */
3670 image = wimlib_resolve_image(wim, argv[1]);
3671 ret = verify_image_exists_and_is_single(image, argv[1],
3674 goto out_wimlib_free;
3676 /* No image specified; default to image 1, but only if the WIM
3677 * contains exactly one image. */
3678 struct wimlib_wim_info info;
3680 wimlib_get_wim_info(wim, &info);
3681 if (info.image_count != 1) {
3682 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3683 wimfile, info.image_count);
3690 /* Parse capture configuration file if specified */
3694 config_str = file_get_text_contents(config_file, &config_len);
3697 goto out_wimlib_free;
3700 config = alloca(sizeof(*config));
3701 ret = parse_capture_config(&config_str, config_len, config);
3703 goto out_free_config;
3706 default_add_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
3709 /* Read update commands from standard input, or the command string if
3712 cmd_file_contents = NULL;
3713 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3716 if (isatty(STDIN_FILENO)) {
3717 tputs(T("Reading update commands from standard input..."));
3718 recommend_man_page(CMD_UPDATE, stdout);
3720 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3721 if (!cmd_file_contents) {
3723 goto out_free_config;
3726 /* Parse the update commands */
3727 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3732 goto out_free_cmd_file_contents;
3735 /* Set default flags and capture config on the update commands */
3736 for (size_t i = 0; i < num_cmds; i++) {
3737 switch (cmds[i].op) {
3738 case WIMLIB_UPDATE_OP_ADD:
3739 cmds[i].add.add_flags |= default_add_flags;
3740 cmds[i].add.config = config;
3742 case WIMLIB_UPDATE_OP_DELETE:
3743 cmds[i].delete_.delete_flags |= default_delete_flags;
3750 /* Execute the update commands */
3751 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3752 imagex_progress_func);
3756 /* Overwrite the updated WIM */
3757 ret = wimlib_overwrite(wim, write_flags, num_threads,
3758 imagex_progress_func);
3761 out_free_cmd_file_contents:
3762 free(cmd_file_contents);
3765 free(config->exclusion_pats.pats);
3766 free(config->exclusion_exception_pats.pats);
3771 out_free_command_str:
3776 usage(CMD_UPDATE, stderr);
3779 goto out_free_command_str;
3784 struct imagex_command {
3786 int (*func)(int argc, tchar **argv, int cmd);
3789 static const struct imagex_command imagex_commands[] = {
3790 [CMD_APPEND] = {T("append"), imagex_capture_or_append},
3791 [CMD_APPLY] = {T("apply"), imagex_apply},
3792 [CMD_CAPTURE] = {T("capture"), imagex_capture_or_append},
3793 [CMD_DELETE] = {T("delete"), imagex_delete},
3794 [CMD_DIR ] = {T("dir"), imagex_dir},
3795 [CMD_EXPORT] = {T("export"), imagex_export},
3796 [CMD_EXTRACT] = {T("extract"), imagex_extract},
3797 [CMD_INFO] = {T("info"), imagex_info},
3798 [CMD_JOIN] = {T("join"), imagex_join},
3799 #if WIM_MOUNTING_SUPPORTED
3800 [CMD_MOUNT] = {T("mount"), imagex_mount_rw_or_ro},
3801 [CMD_MOUNTRW] = {T("mountrw"), imagex_mount_rw_or_ro},
3803 [CMD_OPTIMIZE] = {T("optimize"), imagex_optimize},
3804 [CMD_SPLIT] = {T("split"), imagex_split},
3805 #if WIM_MOUNTING_SUPPORTED
3806 [CMD_UNMOUNT] = {T("unmount"), imagex_unmount},
3808 [CMD_UPDATE] = {T("update"), imagex_update},
3811 static const tchar *usage_strings[] = {
3814 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3815 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3816 " [--nocheck] [--flags EDITION_ID] [--dereference]\n"
3817 " [--config=FILE] [--threads=NUM_THREADS] [--source-list]\n"
3818 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3819 " [--update-of=[WIMFILE:]IMAGE]\n"
3823 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME | all)]\n"
3824 " (DIRECTORY | NTFS_VOLUME) [--check] [--ref=\"GLOB\"]\n"
3825 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3826 " [--hardlink] [--symlink] [--include-invalid-names]\n"
3830 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3831 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3832 " [--nocheck] [--compress=TYPE] [--flags EDITION_ID]\n"
3833 " [--dereference] [--config=FILE] [--threads=NUM_THREADS]\n"
3834 " [--source-list] [--no-acls] [--strict-acls] [--rpfix]\n"
3835 " [--norpfix] [--update-of=[WIMFILE:]IMAGE]\n"
3836 " [--delta-from=WIMFILE]\n"
3840 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n"
3841 " [--check] [--soft]\n"
3845 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH]\n"
3849 " %"TS" SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
3850 " DEST_WIMFILE [DEST_IMAGE_NAME [DEST_IMAGE_DESCRIPTION]]\n"
3851 " [--boot] [--check] [--nocheck] [--compress=TYPE]\n"
3852 " [--ref=\"GLOB\"] [--threads=NUM_THREADS] [--rebuild]\n"
3856 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME) [PATH...]\n"
3857 " [--check] [--ref=\"GLOB\"] [--no-acls] [--strict-acls]\n"
3858 " [--to-stdout] [--dest-dir=CMD_DIR]\n"
3859 " [--include-invalid-names]\n"
3863 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME) [NEW_NAME\n"
3864 " [NEW_DESC]]] [--boot] [--check] [--nocheck] [--xml]\n"
3865 " [--extract-xml FILE] [--header] [--lookup-table]\n"
3869 " %"TS" OUT_WIMFILE SPLIT_WIM_PART... [--check]\n"
3871 #if WIM_MOUNTING_SUPPORTED
3874 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3875 " [--check] [--streams-interface=INTERFACE]\n"
3876 " [--ref=\"GLOB\"] [--allow-other]\n"
3880 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3881 " [--check] [--streams-interface=INTERFACE]\n"
3882 " [--staging-dir=CMD_DIR] [--allow-other]\n"
3887 " %"TS" WIMFILE [--check] [--nocheck] [--recompress]\n"
3888 " [--recompress-slow] [--compress=TYPE]\n"
3889 " [--threads=NUM_THREADS]\n"
3893 " %"TS" WIMFILE SPLIT_WIM_PART_1 PART_SIZE_MB [--check]\n"
3895 #if WIM_MOUNTING_SUPPORTED
3898 " %"TS" DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
3903 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
3904 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
3905 " [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
3909 static const tchar *invocation_name;
3910 static int invocation_cmd = CMD_NONE;
3912 static const tchar *get_cmd_string(int cmd, bool nospace)
3914 static tchar buf[50];
3915 if (cmd == CMD_NONE) {
3916 tsprintf(buf, T("%"TS), T(IMAGEX_PROGNAME));
3917 } else if (invocation_cmd != CMD_NONE) {
3918 tsprintf(buf, T("wim%"TS), imagex_commands[cmd].name);
3920 const tchar *format;
3923 format = T("%"TS"-%"TS"");
3925 format = T("%"TS" %"TS"");
3926 tsprintf(buf, format, invocation_name, imagex_commands[cmd].name);
3934 static const tchar *s =
3936 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
3937 "Copyright (C) 2012, 2013 Eric Biggers\n"
3938 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
3939 "This is free software: you are free to change and redistribute it.\n"
3940 "There is NO WARRANTY, to the extent permitted by law.\n"
3942 "Report bugs to "PACKAGE_BUGREPORT".\n"
3949 help_or_version(int argc, tchar **argv, int cmd)
3954 for (i = 1; i < argc; i++) {
3956 if (p[0] == T('-') && p[1] == T('-')) {
3958 if (!tstrcmp(p, T("help"))) {
3959 if (cmd == CMD_NONE)
3964 } else if (!tstrcmp(p, T("version"))) {
3973 print_usage_string(int cmd, FILE *fp)
3975 tfprintf(fp, usage_strings[cmd], get_cmd_string(cmd, false));
3979 recommend_man_page(int cmd, FILE *fp)
3981 const tchar *format_str;
3983 format_str = T("Uncommon options are not listed;\n"
3984 "See %"TS".pdf in the doc directory for more details.\n");
3986 format_str = T("Uncommon options are not listed;\n"
3987 "Try `man %"TS"' for more details.\n");
3989 tfprintf(fp, format_str, get_cmd_string(cmd, true));
3993 usage(int cmd, FILE *fp)
3995 tfprintf(fp, T("Usage:\n"));
3996 print_usage_string(cmd, fp);
3997 tfprintf(fp, T("\n"));
3998 recommend_man_page(cmd, fp);
4004 tfprintf(fp, T("Usage:\n"));
4005 for (int cmd = 0; cmd < CMD_MAX; cmd++) {
4006 print_usage_string(cmd, fp);
4007 tfprintf(fp, T("\n"));
4009 static const tchar *extra =
4012 " %"TS" --version\n"
4014 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
4017 tfprintf(fp, extra, invocation_name, invocation_name);
4018 recommend_man_page(CMD_NONE, fp);
4021 /* Entry point for wimlib's ImageX implementation. On UNIX the command
4022 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
4023 * something else), while an Windows the command arguments will be UTF-16LE
4024 * encoded 'wchar_t' strings. */
4027 wmain(int argc, wchar_t **argv, wchar_t **envp)
4029 main(int argc, char **argv)
4036 imagex_info_file = stdout;
4037 invocation_name = tbasename(argv[0]);
4040 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
4041 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
4045 setlocale(LC_ALL, "");
4046 codeset = nl_langinfo(CODESET);
4047 if (!strstr(codeset, "UTF-8") &&
4048 !strstr(codeset, "UTF8") &&
4049 !strstr(codeset, "utf-8") &&
4050 !strstr(codeset, "utf8"))
4053 "WARNING: Running %"TS" in a UTF-8 locale is recommended!\n"
4054 " Maybe try: `export LANG=en_US.UTF-8'?\n"
4055 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
4056 " to any value to force wimlib to use UTF-8.\n",
4061 #endif /* !__WIN32__ */
4063 /* Allow being invoked as wimCOMMAND (e.g. wimapply). */
4065 if (!tstrncmp(invocation_name, T("wim"), 3) &&
4066 tstrcmp(invocation_name, T(IMAGEX_PROGNAME))) {
4067 for (int i = 0; i < CMD_MAX; i++) {
4068 if (!tstrcmp(invocation_name + 3,
4069 imagex_commands[i].name))
4078 /* Unless already known from the invocation name, determine which
4079 * command was specified. */
4080 if (cmd == CMD_NONE) {
4082 imagex_error(T("No command specified!\n"));
4086 for (int i = 0; i < CMD_MAX; i++) {
4087 if (!tstrcmp(argv[1], imagex_commands[i].name)) {
4092 if (cmd != CMD_NONE) {
4098 /* Handle --help and --version. --help can be either for the program as
4099 * a whole (cmd == CMD_NONE) or just for a specific command (cmd !=
4100 * CMD_NONE). Note: help_or_version() will not return if a --help or
4101 * --version argument was found. */
4102 help_or_version(argc, argv, cmd);
4104 /* Bail if a valid command was not specified. */
4105 if (cmd == CMD_NONE) {
4106 imagex_error(T("Unrecognized command: `%"TS"'\n"), argv[1]);
4111 /* Enable warning and error messages in wimlib be more user-friendly.
4113 wimlib_set_print_errors(true);
4115 /* Initialize wimlib. */
4116 ret = wimlib_global_init(init_flags);
4118 goto out_check_status;
4120 /* Call the command handler function. */
4121 ret = imagex_commands[cmd].func(argc, argv, cmd);
4123 /* Check for error writing to standard output, especially since for some
4124 * commands, writing to standard output is part of the program's actual
4125 * behavior and not just for informational purposes. */
4126 if (ferror(stdout) || fclose(stdout)) {
4127 imagex_error_with_errno(T("error writing to standard output"));
4132 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
4133 * itself (not by wimlib). 0 indicates success. > 0 indicates a wimlib
4134 * error code from which an error message can be printed. */
4136 imagex_error(T("Exiting with error code %d:\n"
4138 wimlib_get_error_string(ret));
4139 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
4140 imagex_error_with_errno(T("errno"));
4142 /* Make wimlib free any resources it's holding (although this is not
4143 * strictly necessary because the process is ending anyway). */
4144 wimlib_global_cleanup();