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_NO_PACK_STREAMS_OPTION,
146 IMAGEX_NOT_PIPABLE_OPTION,
147 IMAGEX_PACK_STREAMS_OPTION,
149 IMAGEX_PIPABLE_OPTION,
150 IMAGEX_REBUILD_OPTION,
151 IMAGEX_RECOMPRESS_OPTION,
152 IMAGEX_RECURSIVE_OPTION,
154 IMAGEX_RESUME_OPTION,
157 IMAGEX_SOURCE_LIST_OPTION,
158 IMAGEX_STAGING_DIR_OPTION,
159 IMAGEX_STREAMS_INTERFACE_OPTION,
160 IMAGEX_STRICT_ACLS_OPTION,
161 IMAGEX_SYMLINK_OPTION,
162 IMAGEX_THREADS_OPTION,
163 IMAGEX_TO_STDOUT_OPTION,
164 IMAGEX_UNIX_DATA_OPTION,
165 IMAGEX_UPDATE_OF_OPTION,
166 IMAGEX_VERBOSE_OPTION,
170 static const struct option apply_options[] = {
171 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
172 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
173 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
174 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
175 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
176 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
177 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
178 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
179 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
180 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
181 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
182 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
184 /* --resume is undocumented for now as it needs improvement. */
185 {T("resume"), no_argument, NULL, IMAGEX_RESUME_OPTION},
189 static const struct option capture_or_append_options[] = {
190 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
191 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
192 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
193 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
194 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
195 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
196 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
197 {T("pack-streams"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
198 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
199 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
200 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
201 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
202 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
203 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
204 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
205 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
206 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
207 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
208 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
209 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
210 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
211 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
212 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
213 {T("update-of"), required_argument, NULL, IMAGEX_UPDATE_OF_OPTION},
214 {T("delta-from"), required_argument, NULL, IMAGEX_DELTA_FROM_OPTION},
218 static const struct option delete_options[] = {
219 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
220 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
224 static const struct option dir_options[] = {
225 {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
229 static const struct option export_options[] = {
230 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
231 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
232 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
233 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
234 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
235 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
236 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
237 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
238 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
239 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
243 static const struct option extract_options[] = {
244 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
245 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
246 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
247 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
248 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
249 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
250 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
251 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
252 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
253 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
257 static const struct option info_options[] = {
258 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
259 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
260 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
261 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
262 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
263 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
264 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
265 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
266 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
270 static const struct option join_options[] = {
271 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
275 static const struct option mount_options[] = {
276 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
277 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
278 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
279 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
280 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
281 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
282 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
286 static const struct option optimize_options[] = {
287 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
288 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
289 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
290 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
291 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
292 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
293 {T("recompress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
294 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
295 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
296 {T("no-pack-streams"), no_argument, NULL, IMAGEX_NO_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_params slow_params = {
438 .size_of_this = sizeof(struct wimlib_lzx_params),
439 .algorithm = WIMLIB_LZX_ALGORITHM_SLOW,
442 .use_len2_matches = 1,
443 .num_fast_bytes = 96,
444 .num_optim_passes = 4,
445 .max_search_depth = 100,
446 .max_matches_per_pos = 10,
447 .main_nostat_cost = 15,
448 .len_nostat_cost = 15,
449 .aligned_nostat_cost = 7,
453 ret = wimlib_lzx_set_default_params(&slow_params);
455 imagex_error(T("Couldn't set slow compression parameters.!"));
460 const tchar **strings;
461 unsigned num_strings;
462 unsigned num_alloc_strings;
465 #define STRING_SET_INITIALIZER \
466 { .strings = NULL, .num_strings = 0, .num_alloc_strings = 0, }
468 #define STRING_SET(_strings) \
469 struct string_set _strings = STRING_SET_INITIALIZER
472 string_set_append(struct string_set *set, const tchar *glob)
474 unsigned num_alloc_strings = set->num_alloc_strings;
476 if (set->num_strings == num_alloc_strings) {
477 const tchar **new_strings;
479 num_alloc_strings += 4;
480 new_strings = realloc(set->strings,
481 sizeof(set->strings[0]) * num_alloc_strings);
483 imagex_error(T("Out of memory!"));
486 set->strings = new_strings;
487 set->num_alloc_strings = num_alloc_strings;
489 set->strings[set->num_strings++] = glob;
494 string_set_destroy(struct string_set *set)
500 wim_reference_globs(WIMStruct *wim, struct string_set *set, int open_flags)
502 return wimlib_reference_resource_files(wim, set->strings,
504 WIMLIB_REF_FLAG_GLOB_ENABLE,
506 imagex_progress_func);
510 do_resource_not_found_warning(const tchar *wimfile,
511 const struct wimlib_wim_info *info,
512 const struct string_set *refglobs)
514 if (info->total_parts > 1) {
515 if (refglobs->num_strings == 0) {
516 imagex_error(T("\"%"TS"\" is part of a split WIM. "
517 "Use --ref to specify the other parts."),
520 imagex_error(T("Perhaps the '--ref' argument did not "
521 "specify all other parts of the split "
525 imagex_error(T("If this is a delta WIM, use the --ref argument "
526 "to specify the WIM(s) on which it is based."));
530 /* Returns the size of a file given its name, or -1 if the file does not exist
531 * or its size cannot be determined. */
533 file_get_size(const tchar *filename)
536 if (tstat(filename, &st) == 0)
543 PARSE_STRING_SUCCESS = 0,
544 PARSE_STRING_FAILURE = 1,
545 PARSE_STRING_NONE = 2,
549 * Parses a string token from an array of characters.
551 * Tokens are either whitespace-delimited, or double or single-quoted.
553 * @line_p: Pointer to the pointer to the line of data. Will be updated
554 * to point past the string token iff the return value is
555 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
558 * @len_p: @len_p initially stores the length of the line of data, which may
559 * be 0, and it will be updated to the number of bytes remaining in
560 * the line iff the return value is PARSE_STRING_SUCCESS.
562 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
563 * parsed string token will be returned here.
565 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
566 * PARSE_STRING_FAILURE if the data was invalid due to a missing
567 * closing quote; or PARSE_STRING_NONE if the line ended before the
568 * beginning of a string token was found.
571 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
574 tchar *line = *line_p;
578 /* Skip leading whitespace */
581 return PARSE_STRING_NONE;
582 if (!istspace(*line) && *line != T('\0'))
588 if (quote_char == T('"') || quote_char == T('\'')) {
593 line = tmemchr(line, quote_char, len);
595 imagex_error(T("Missing closing quote: %"TS), fn - 1);
596 return PARSE_STRING_FAILURE;
599 /* Unquoted string. Go until whitespace. Line is terminated
600 * by '\0', so no need to check 'len'. */
604 } while (!istspace(*line) && *line != T('\0'));
611 return PARSE_STRING_SUCCESS;
614 /* Parses a line of data (not an empty line or comment) in the source list file
615 * format. (See the man page for 'wimlib-imagex capture' for details on this
616 * format and the meaning.)
618 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
619 * len == 0. The data in @line will be modified by this function call.
621 * @len: Length of the line of data.
623 * @source: On success, the capture source and target described by the line is
624 * written into this destination. Note that it will contain pointers
625 * to data in the @line array.
627 * Returns true if the line was valid; false otherwise. */
629 parse_source_list_line(tchar *line, size_t len,
630 struct wimlib_capture_source *source)
634 ret = parse_string(&line, &len, &source->fs_source_path);
635 if (ret != PARSE_STRING_SUCCESS)
637 ret = parse_string(&line, &len, &source->wim_target_path);
638 if (ret == PARSE_STRING_NONE)
639 source->wim_target_path = source->fs_source_path;
640 return ret != PARSE_STRING_FAILURE;
643 /* Returns %true if the given line of length @len > 0 is a comment or empty line
644 * in the source list file format. */
646 is_comment_line(const tchar *line, size_t len)
651 if (!istspace(*line) && *line != T('\0'))
661 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
664 tchar *contents = *contents_p;
665 size_t nchars = *nchars_p;
668 for (i = 0; i < nchars; i++)
669 if (contents[i] == T('\n'))
672 /* Handle last line not terminated by a newline */
673 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
674 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
676 imagex_error(T("Out of memory!"));
679 contents[nchars] = T('\n');
680 *contents_p = contents;
688 /* Parses a file in the source list format. (See the man page for
689 * 'wimlib-imagex capture' for details on this format and the meaning.)
691 * @source_list_contents: Contents of the source list file. Note that this
692 * buffer will be modified to save memory allocations,
693 * and cannot be freed until the returned array of
694 * wimlib_capture_source's has also been freed.
696 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
699 * @nsources_ret: On success, the length of the returned array is
702 * Returns: An array of `struct wimlib_capture_source's that can be passed to
703 * the wimlib_add_image_multisource() function to specify how a WIM image is to
705 static struct wimlib_capture_source *
706 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
707 size_t *nsources_ret)
711 struct wimlib_capture_source *sources;
714 nlines = text_file_count_lines(source_list_contents_p,
715 &source_list_nchars);
719 /* Always allocate at least 1 slot, just in case the implementation of
720 * calloc() returns NULL if 0 bytes are requested. */
721 sources = calloc(nlines ?: 1, sizeof(*sources));
723 imagex_error(T("out of memory"));
726 p = *source_list_contents_p;
728 for (i = 0; i < nlines; i++) {
729 /* XXX: Could use rawmemchr() here instead, but it may not be
730 * available on all platforms. */
731 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
732 size_t len = endp - p + 1;
734 if (!is_comment_line(p, len)) {
735 if (!parse_source_list_line(p, len, &sources[j++])) {
748 enum capture_config_section {
749 CAPTURE_CONFIG_NO_SECTION,
750 CAPTURE_CONFIG_EXCLUSION_SECTION,
751 CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
752 CAPTURE_CONFIG_IGNORE_SECTION,
756 CAPTURE_CONFIG_INVALID_SECTION,
757 CAPTURE_CONFIG_CHANGED_SECTION,
758 CAPTURE_CONFIG_SAME_SECTION,
762 check_config_section(tchar *line, size_t len,
763 enum capture_config_section *cur_section)
765 while (istspace(*line))
769 return CAPTURE_CONFIG_SAME_SECTION;
772 tchar *endbrace = tstrrchr(line, T(']'));
774 return CAPTURE_CONFIG_SAME_SECTION;
776 if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
777 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
778 } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
779 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
780 } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
781 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
782 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
783 "of capture config file\n"),
785 } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
786 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
787 tfputs(T("WARNING: Ignoring [AlignmentList] section "
788 "of capture config file\n"),
791 imagex_error(T("Invalid capture config file section \"%"TS"\""),
793 return CAPTURE_CONFIG_INVALID_SECTION;
795 return CAPTURE_CONFIG_CHANGED_SECTION;
800 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
803 if (pat_list->num_pats == pat_list->num_allocated_pats) {
805 size_t num_allocated_pats = pat_list->num_pats + 8;
807 pats = realloc(pat_list->pats,
808 num_allocated_pats * sizeof(pat_list->pats[0]));
810 imagex_error(T("Out of memory!"));
813 pat_list->pats = pats;
814 pat_list->num_allocated_pats = num_allocated_pats;
816 pat_list->pats[pat_list->num_pats++] = pat;
821 parse_capture_config_line(tchar *line, size_t len,
822 enum capture_config_section *cur_section,
823 struct wimlib_capture_config *config)
828 ret = check_config_section(line, len, cur_section);
829 if (ret == CAPTURE_CONFIG_INVALID_SECTION)
831 if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
834 switch (*cur_section) {
835 case CAPTURE_CONFIG_NO_SECTION:
836 imagex_error(T("Line \"%"TS"\" is not in a section "
837 "(such as [ExclusionList]"), line);
839 case CAPTURE_CONFIG_EXCLUSION_SECTION:
840 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
842 return pattern_list_add_pattern(&config->exclusion_pats,
844 case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
845 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
847 return pattern_list_add_pattern(&config->exclusion_exception_pats,
849 case CAPTURE_CONFIG_IGNORE_SECTION:
856 parse_capture_config(tchar **contents_p, size_t nchars,
857 struct wimlib_capture_config *config)
862 enum capture_config_section cur_section;
864 memset(config, 0, sizeof(*config));
866 nlines = text_file_count_lines(contents_p, &nchars);
870 cur_section = CAPTURE_CONFIG_NO_SECTION;
872 for (i = 0; i < nlines; i++) {
873 tchar *endp = tmemchr(p, T('\n'), nchars);
874 size_t len = endp - p + 1;
876 if (!is_comment_line(p, len))
877 if (!parse_capture_config_line(p, len, &cur_section, config))
885 /* Reads the contents of a file into memory. */
887 file_get_contents(const tchar *filename, size_t *len_ret)
894 if (tstat(filename, &stbuf) != 0) {
895 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
900 fp = tfopen(filename, T("rb"));
902 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
906 buf = malloc(len ? len : 1);
908 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
909 "contents of file \"%"TS"\""), len, filename);
912 if (fread(buf, 1, len, fp) != len) {
913 imagex_error_with_errno(T("Failed to read %zu bytes from the "
914 "file \"%"TS"\""), len, filename);
928 /* Read standard input until EOF and return the full contents in a malloc()ed
929 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
932 stdin_get_contents(size_t *len_ret)
934 /* stdin can, of course, be a pipe or other non-seekable file, so the
935 * total length of the data cannot be pre-determined */
937 size_t newlen = 1024;
941 char *p = realloc(buf, newlen);
942 size_t bytes_read, bytes_to_read;
944 imagex_error(T("out of memory while reading stdin"));
948 bytes_to_read = newlen - pos;
949 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
951 if (bytes_read != bytes_to_read) {
956 imagex_error_with_errno(T("error reading stdin"));
970 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
973 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
975 *num_tchars_ret = num_bytes;
977 #else /* !__WIN32__ */
978 /* On Windows, translate the text to UTF-16LE */
982 if (num_bytes >= 2 &&
983 (((unsigned char)text[0] == 0xff && (unsigned char)text[1] == 0xfe) ||
984 ((unsigned char)text[0] <= 0x7f && (unsigned char)text[1] == 0x00)))
986 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
987 * with something that looks like an ASCII character encoded as
988 * a UTF-16LE code unit. Assume the file is encoded as
989 * UTF-16LE. This is not a 100% reliable check. */
990 num_wchars = num_bytes / 2;
991 text_wstr = (wchar_t*)text;
993 /* File does not look like UTF-16LE. Assume it is encoded in
994 * the current Windows code page. I think these are always
995 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
996 * should work as expected. */
997 text_wstr = win32_mbs_to_wcs(text,
1002 *num_tchars_ret = num_wchars;
1004 #endif /* __WIN32__ */
1008 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
1013 contents = file_get_contents(filename, &num_bytes);
1016 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
1020 stdin_get_text_contents(size_t *num_tchars_ret)
1025 contents = stdin_get_contents(&num_bytes);
1028 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
1031 #define TO_PERCENT(numerator, denominator) \
1032 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
1034 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
1035 #define MEBIBYTE_MIN_NBYTES 10000000ULL
1036 #define KIBIBYTE_MIN_NBYTES 10000ULL
1039 get_unit(uint64_t total_bytes, const tchar **name_ret)
1041 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
1042 *name_ret = T("GiB");
1044 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
1045 *name_ret = T("MiB");
1047 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
1048 *name_ret = T("KiB");
1051 *name_ret = T("bytes");
1056 /* Progress callback function passed to various wimlib functions. */
1058 imagex_progress_func(enum wimlib_progress_msg msg,
1059 const union wimlib_progress_info *info)
1061 unsigned percent_done;
1062 unsigned unit_shift;
1063 const tchar *unit_name;
1064 if (imagex_be_quiet)
1067 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1068 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1069 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1070 info->write_streams.total_bytes);
1072 if (info->write_streams.completed_streams == 0) {
1073 imagex_printf(T("Writing %"TS"-compressed data using %u thread%"TS"\n"),
1074 wimlib_get_compression_type_string(info->write_streams.compression_type),
1075 info->write_streams.num_threads,
1076 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1078 if (info->write_streams.total_parts <= 1) {
1079 imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1080 "written (%u%% done)"),
1081 info->write_streams.completed_bytes >> unit_shift,
1083 info->write_streams.total_bytes >> unit_shift,
1087 imagex_printf(T("\rWriting resources from part %u of %u: "
1088 "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written"),
1089 (info->write_streams.completed_parts ==
1090 info->write_streams.total_parts) ?
1091 info->write_streams.completed_parts :
1092 info->write_streams.completed_parts + 1,
1093 info->write_streams.total_parts,
1094 info->write_streams.completed_bytes >> unit_shift,
1096 info->write_streams.total_bytes >> unit_shift,
1100 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1101 imagex_printf(T("\n"));
1103 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1104 imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
1105 if (*info->scan.wim_target_path) {
1106 imagex_printf(T(" (loading as WIM path: "
1107 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
1108 info->scan.wim_target_path);
1110 imagex_printf(T(" (loading as root of WIM image)...\n"));
1113 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1114 switch (info->scan.status) {
1115 case WIMLIB_SCAN_DENTRY_OK:
1116 imagex_printf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
1118 case WIMLIB_SCAN_DENTRY_EXCLUDED:
1119 imagex_printf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1121 case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
1122 imagex_printf(T("WARNING: Excluding unsupported file or directory\n"
1123 " \"%"TS"\" from capture\n"), info->scan.cur_path);
1127 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1128 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1129 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1130 info->integrity.total_bytes);
1131 imagex_printf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1132 "of %"PRIu64" %"TS" (%u%%) done"),
1133 info->integrity.filename,
1134 info->integrity.completed_bytes >> unit_shift,
1136 info->integrity.total_bytes >> unit_shift,
1139 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1140 imagex_printf(T("\n"));
1142 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1143 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1144 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1145 info->integrity.total_bytes);
1146 imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1147 "of %"PRIu64" %"TS" (%u%%) done"),
1148 info->integrity.completed_bytes >> unit_shift,
1150 info->integrity.total_bytes >> unit_shift,
1153 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1154 imagex_printf(T("\n"));
1156 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1157 imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1158 "to %"TS" \"%"TS"\"\n"),
1159 info->extract.image,
1160 info->extract.image_name,
1161 info->extract.wimfile_name,
1162 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1163 T("NTFS volume") : T("directory")),
1164 info->extract.target);
1166 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1167 imagex_printf(T("Extracting "
1168 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\" from image %d (\"%"TS"\") "
1169 "in \"%"TS"\" to \"%"TS"\"\n"),
1170 info->extract.extract_root_wim_source_path,
1171 info->extract.image,
1172 info->extract.image_name,
1173 info->extract.wimfile_name,
1174 info->extract.target);
1176 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1177 percent_done = TO_PERCENT(info->extract.completed_bytes,
1178 info->extract.total_bytes);
1179 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1180 imagex_printf(T("\rExtracting files: "
1181 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1182 info->extract.completed_bytes >> unit_shift,
1184 info->extract.total_bytes >> unit_shift,
1187 if (info->extract.completed_bytes >= info->extract.total_bytes)
1188 imagex_printf(T("\n"));
1190 case WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN:
1191 if (info->extract.total_parts != 1) {
1192 imagex_printf(T("\nReading split pipable WIM part %u of %u\n"),
1193 info->extract.part_number,
1194 info->extract.total_parts);
1197 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1198 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1199 imagex_printf(T("Setting timestamps on all extracted files...\n"));
1201 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1202 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1203 imagex_printf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1204 info->extract.target);
1207 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1208 percent_done = TO_PERCENT(info->split.completed_bytes,
1209 info->split.total_bytes);
1210 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1211 imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
1212 "%"PRIu64" %"TS" (%u%%) written\n"),
1213 info->split.part_name,
1214 info->split.cur_part_number,
1215 info->split.total_parts,
1216 info->split.completed_bytes >> unit_shift,
1218 info->split.total_bytes >> unit_shift,
1222 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1223 if (info->split.completed_bytes == info->split.total_bytes) {
1224 imagex_printf(T("Finished writing split WIM part %u of %u\n"),
1225 info->split.cur_part_number,
1226 info->split.total_parts);
1229 case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1230 switch (info->update.command->op) {
1231 case WIMLIB_UPDATE_OP_DELETE:
1232 imagex_printf(T("Deleted WIM path "
1233 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1234 info->update.command->delete_.wim_path);
1236 case WIMLIB_UPDATE_OP_RENAME:
1237 imagex_printf(T("Renamed WIM path "
1238 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
1239 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1240 info->update.command->rename.wim_source_path,
1241 info->update.command->rename.wim_target_path);
1243 case WIMLIB_UPDATE_OP_ADD:
1251 fflush(imagex_info_file);
1256 parse_num_threads(const tchar *optarg)
1259 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1260 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1261 imagex_error(T("Number of threads must be a non-negative integer!"));
1268 static uint32_t parse_chunk_size(const tchar *optarg)
1271 unsigned long chunk_size = tstrtoul(optarg, &tmp, 10);
1272 if (chunk_size >= UINT32_MAX || *tmp || tmp == optarg) {
1273 imagex_error(T("Chunk size must be a non-negative integer!"));
1282 * Parse an option passed to an update command.
1284 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1287 * @option: Text string for the option (beginning with --)
1289 * @cmd: `struct wimlib_update_command' that is being constructed for
1292 * Returns true if the option was recognized; false if not.
1295 update_command_add_option(int op, const tchar *option,
1296 struct wimlib_update_command *cmd)
1298 bool recognized = true;
1300 case WIMLIB_UPDATE_OP_ADD:
1301 if (!tstrcmp(option, T("--verbose")))
1302 cmd->add.add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
1303 else if (!tstrcmp(option, T("--unix-data")))
1304 cmd->add.add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
1305 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1306 cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
1307 else if (!tstrcmp(option, T("--strict-acls")))
1308 cmd->add.add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
1309 else if (!tstrcmp(option, T("--dereference")))
1310 cmd->add.add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
1314 case WIMLIB_UPDATE_OP_DELETE:
1315 if (!tstrcmp(option, T("--force")))
1316 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1317 else if (!tstrcmp(option, T("--recursive")))
1318 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1329 /* How many nonoption arguments each `imagex update' command expects */
1330 static const unsigned update_command_num_nonoptions[] = {
1331 [WIMLIB_UPDATE_OP_ADD] = 2,
1332 [WIMLIB_UPDATE_OP_DELETE] = 1,
1333 [WIMLIB_UPDATE_OP_RENAME] = 2,
1337 update_command_add_nonoption(int op, const tchar *nonoption,
1338 struct wimlib_update_command *cmd,
1339 unsigned num_nonoptions)
1342 case WIMLIB_UPDATE_OP_ADD:
1343 if (num_nonoptions == 0)
1344 cmd->add.fs_source_path = (tchar*)nonoption;
1346 cmd->add.wim_target_path = (tchar*)nonoption;
1348 case WIMLIB_UPDATE_OP_DELETE:
1349 cmd->delete_.wim_path = (tchar*)nonoption;
1351 case WIMLIB_UPDATE_OP_RENAME:
1352 if (num_nonoptions == 0)
1353 cmd->rename.wim_source_path = (tchar*)nonoption;
1355 cmd->rename.wim_target_path = (tchar*)nonoption;
1361 * Parse a command passed on stdin to `imagex update'.
1363 * @line: Text of the command.
1364 * @len: Length of the line, including a null terminator
1367 * @command: A `struct wimlib_update_command' to fill in from the parsed
1370 * @line_number: Line number of the command, for diagnostics.
1372 * Returns true on success; returns false on parse error.
1375 parse_update_command(tchar *line, size_t len,
1376 struct wimlib_update_command *command,
1380 tchar *command_name;
1382 size_t num_nonoptions;
1384 /* Get the command name ("add", "delete", "rename") */
1385 ret = parse_string(&line, &len, &command_name);
1386 if (ret != PARSE_STRING_SUCCESS)
1389 if (!tstrcasecmp(command_name, T("add"))) {
1390 op = WIMLIB_UPDATE_OP_ADD;
1391 } else if (!tstrcasecmp(command_name, T("delete"))) {
1392 op = WIMLIB_UPDATE_OP_DELETE;
1393 } else if (!tstrcasecmp(command_name, T("rename"))) {
1394 op = WIMLIB_UPDATE_OP_RENAME;
1396 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1397 command_name, line_number);
1402 /* Parse additional options and non-options as needed */
1407 ret = parse_string(&line, &len, &next_string);
1408 if (ret == PARSE_STRING_NONE) /* End of line */
1410 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1412 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1414 if (!update_command_add_option(op, next_string, command))
1416 imagex_error(T("Unrecognized option \"%"TS"\" to "
1417 "update command \"%"TS"\" on line %zu"),
1418 next_string, command_name, line_number);
1424 if (num_nonoptions == update_command_num_nonoptions[op])
1426 imagex_error(T("Unexpected argument \"%"TS"\" in "
1427 "update command on line %zu\n"
1428 " (The \"%"TS"\" command only "
1429 "takes %zu nonoption arguments!)\n"),
1430 next_string, line_number,
1431 command_name, num_nonoptions);
1434 update_command_add_nonoption(op, next_string,
1435 command, num_nonoptions);
1440 if (num_nonoptions != update_command_num_nonoptions[op]) {
1441 imagex_error(T("Not enough arguments to update command "
1442 "\"%"TS"\" on line %zu"), command_name, line_number);
1448 static struct wimlib_update_command *
1449 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1450 size_t *num_cmds_ret)
1454 struct wimlib_update_command *cmds;
1457 nlines = text_file_count_lines(cmd_file_contents_p,
1462 /* Always allocate at least 1 slot, just in case the implementation of
1463 * calloc() returns NULL if 0 bytes are requested. */
1464 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1466 imagex_error(T("out of memory"));
1469 p = *cmd_file_contents_p;
1471 for (i = 0; i < nlines; i++) {
1472 /* XXX: Could use rawmemchr() here instead, but it may not be
1473 * available on all platforms. */
1474 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1475 size_t len = endp - p + 1;
1477 if (!is_comment_line(p, len)) {
1478 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1489 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1490 * one image from a WIM file to a NTFS volume. */
1492 imagex_apply(int argc, tchar **argv, int cmd)
1496 int image = WIMLIB_NO_IMAGE;
1498 struct wimlib_wim_info info;
1500 const tchar *wimfile;
1501 const tchar *target;
1502 const tchar *image_num_or_name = NULL;
1503 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1505 STRING_SET(refglobs);
1507 for_opt(c, apply_options) {
1509 case IMAGEX_CHECK_OPTION:
1510 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1512 case IMAGEX_HARDLINK_OPTION:
1513 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1515 case IMAGEX_SYMLINK_OPTION:
1516 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1518 case IMAGEX_VERBOSE_OPTION:
1519 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
1521 case IMAGEX_REF_OPTION:
1522 ret = string_set_append(&refglobs, optarg);
1524 goto out_free_refglobs;
1526 case IMAGEX_UNIX_DATA_OPTION:
1527 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1529 case IMAGEX_NO_ACLS_OPTION:
1530 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1532 case IMAGEX_STRICT_ACLS_OPTION:
1533 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1535 case IMAGEX_NORPFIX_OPTION:
1536 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1538 case IMAGEX_RPFIX_OPTION:
1539 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1541 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1542 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1543 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1545 case IMAGEX_RESUME_OPTION:
1546 extract_flags |= WIMLIB_EXTRACT_FLAG_RESUME;
1554 if (argc != 2 && argc != 3)
1559 if (!tstrcmp(wimfile, T("-"))) {
1560 /* Attempt to apply pipable WIM from standard input. */
1562 image_num_or_name = NULL;
1565 image_num_or_name = argv[1];
1570 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1571 imagex_progress_func);
1573 goto out_free_refglobs;
1575 wimlib_get_wim_info(wim, &info);
1578 /* Image explicitly specified. */
1579 image_num_or_name = argv[1];
1580 image = wimlib_resolve_image(wim, image_num_or_name);
1581 ret = verify_image_exists(image, image_num_or_name, wimfile);
1583 goto out_wimlib_free;
1586 /* No image specified; default to image 1, but only if the WIM
1587 * contains exactly one image. */
1589 if (info.image_count != 1) {
1590 imagex_error(T("\"%"TS"\" contains %d images; "
1591 "Please select one (or all)."),
1592 wimfile, info.image_count);
1601 if (refglobs.num_strings) {
1603 imagex_error(T("Can't specify --ref when applying from stdin!"));
1605 goto out_wimlib_free;
1607 ret = wim_reference_globs(wim, &refglobs, open_flags);
1609 goto out_wimlib_free;
1614 /* Interpret a regular file or block device target as a NTFS
1618 if (tstat(target, &stbuf)) {
1619 if (errno != ENOENT) {
1620 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1623 goto out_wimlib_free;
1626 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1627 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1633 ret = wimlib_extract_image(wim, image, target, extract_flags,
1634 imagex_progress_func);
1636 set_fd_to_binary_mode(STDIN_FILENO);
1637 ret = wimlib_extract_image_from_pipe(STDIN_FILENO,
1639 target, extract_flags,
1640 imagex_progress_func);
1643 imagex_printf(T("Done applying WIM image.\n"));
1644 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
1646 do_resource_not_found_warning(wimfile, &info, &refglobs);
1648 imagex_error(T( "If you are applying an image "
1649 "from a split pipable WIM,\n"
1650 " make sure you have "
1651 "concatenated together all parts."));
1657 string_set_destroy(&refglobs);
1661 usage(CMD_APPLY, stderr);
1663 goto out_free_refglobs;
1666 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1667 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1668 * the desired image. 'wimlib-imagex append': add a new image to an existing
1671 imagex_capture_or_append(int argc, tchar **argv, int cmd)
1674 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1675 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE |
1676 WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1677 int write_flags = 0;
1678 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
1679 uint32_t chunk_size = UINT32_MAX;
1680 const tchar *wimfile;
1684 const tchar *flags_element = NULL;
1687 STRING_SET(base_wimfiles);
1688 WIMStruct **base_wims;
1690 WIMStruct *template_wim;
1691 const tchar *template_wimfile = NULL;
1692 const tchar *template_image_name_or_num = NULL;
1693 int template_image = WIMLIB_NO_IMAGE;
1696 unsigned num_threads = 0;
1701 const tchar *config_file = NULL;
1703 struct wimlib_capture_config *config;
1705 bool source_list = false;
1706 size_t source_list_nchars = 0;
1707 tchar *source_list_contents;
1708 bool capture_sources_malloced;
1709 struct wimlib_capture_source *capture_sources;
1711 bool name_defaulted;
1713 for_opt(c, capture_or_append_options) {
1715 case IMAGEX_BOOT_OPTION:
1716 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1718 case IMAGEX_CHECK_OPTION:
1719 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1720 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1722 case IMAGEX_NOCHECK_OPTION:
1723 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
1725 case IMAGEX_CONFIG_OPTION:
1726 config_file = optarg;
1727 add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1729 case IMAGEX_COMPRESS_OPTION:
1730 compression_type = get_compression_type(optarg);
1731 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1734 case IMAGEX_COMPRESS_SLOW_OPTION:
1735 ret = set_compress_slow();
1738 compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1740 case IMAGEX_CHUNK_SIZE_OPTION:
1741 chunk_size = parse_chunk_size(optarg);
1742 if (chunk_size == UINT32_MAX)
1745 case IMAGEX_PACK_STREAMS_OPTION:
1746 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
1748 case IMAGEX_FLAGS_OPTION:
1749 flags_element = optarg;
1751 case IMAGEX_DEREFERENCE_OPTION:
1752 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1754 case IMAGEX_VERBOSE_OPTION:
1755 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1757 case IMAGEX_THREADS_OPTION:
1758 num_threads = parse_num_threads(optarg);
1759 if (num_threads == UINT_MAX)
1762 case IMAGEX_REBUILD_OPTION:
1763 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1765 case IMAGEX_UNIX_DATA_OPTION:
1766 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1768 case IMAGEX_SOURCE_LIST_OPTION:
1771 case IMAGEX_NO_ACLS_OPTION:
1772 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1774 case IMAGEX_STRICT_ACLS_OPTION:
1775 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1777 case IMAGEX_RPFIX_OPTION:
1778 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1780 case IMAGEX_NORPFIX_OPTION:
1781 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1783 case IMAGEX_PIPABLE_OPTION:
1784 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1786 case IMAGEX_NOT_PIPABLE_OPTION:
1787 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
1789 case IMAGEX_UPDATE_OF_OPTION:
1790 if (template_image_name_or_num) {
1791 imagex_error(T("'--update-of' can only be "
1792 "specified one time!"));
1796 colon = tstrrchr(optarg, T(':'));
1799 template_wimfile = optarg;
1801 template_image_name_or_num = colon + 1;
1803 template_wimfile = NULL;
1804 template_image_name_or_num = optarg;
1808 case IMAGEX_DELTA_FROM_OPTION:
1809 if (cmd != CMD_CAPTURE) {
1810 imagex_error(T("'--delta-from' is only "
1811 "valid for capture!"));
1814 ret = string_set_append(&base_wimfiles, optarg);
1816 goto out_free_base_wimfiles;
1817 write_flags |= WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS;
1826 if (argc < 2 || argc > 4)
1832 /* Set default compression type. */
1833 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
1834 struct wimlib_lzx_params params;
1835 memset(¶ms, 0, sizeof(params));
1836 params.size_of_this = sizeof(params);
1837 params.algorithm = WIMLIB_LZX_ALGORITHM_FAST;
1838 params.use_defaults = 1;
1840 wimlib_lzx_set_default_params(¶ms);
1841 compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1844 if (!tstrcmp(wimfile, T("-"))) {
1845 /* Writing captured WIM to standard output. */
1847 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
1848 imagex_error("Can't write a non-pipable WIM to "
1849 "standard output! Specify --pipable\n"
1850 " if you want to create a pipable WIM "
1851 "(but read the docs first).");
1855 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1857 if (cmd == CMD_APPEND) {
1858 imagex_error(T("Using standard output for append does "
1859 "not make sense."));
1862 wim_fd = STDOUT_FILENO;
1864 imagex_info_file = stderr;
1865 set_fd_to_binary_mode(wim_fd);
1868 /* If template image was specified using --update-of=IMAGE rather
1869 * than --update-of=WIMFILE:IMAGE, set the default WIMFILE. */
1870 if (template_image_name_or_num && !template_wimfile) {
1871 if (base_wimfiles.num_strings == 1) {
1872 /* Capturing delta WIM based on single WIM: default to
1874 template_wimfile = base_wimfiles.strings[0];
1875 } else if (cmd == CMD_APPEND) {
1876 /* Appending to WIM: default to WIM being appended to.
1878 template_wimfile = wimfile;
1880 /* Capturing a normal (non-delta) WIM, so the WIM file
1881 * *must* be explicitly specified. */
1882 if (base_wimfiles.num_strings > 1) {
1883 imagex_error(T("For capture of delta WIM "
1884 "based on multiple existing "
1886 " '--update-of' must "
1887 "specify WIMFILE:IMAGE!"));
1889 imagex_error(T("For capture of non-delta WIM, "
1890 "'--update-of' must specify "
1899 name_defaulted = false;
1901 /* Set default name to SOURCE argument, omitting any directory
1902 * prefixes and trailing slashes. This requires making a copy
1903 * of @source. Leave some free characters at the end in case we
1904 * append a number to keep the name unique. */
1905 size_t source_name_len;
1907 source_name_len = tstrlen(source);
1908 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1909 name = tbasename(tstrcpy(source_copy, source));
1910 name_defaulted = true;
1912 /* Image description defaults to NULL if not given. */
1919 /* Set up capture sources in source list mode */
1920 if (source[0] == T('-') && source[1] == T('\0')) {
1921 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1923 source_list_contents = file_get_text_contents(source,
1924 &source_list_nchars);
1926 if (!source_list_contents)
1929 capture_sources = parse_source_list(&source_list_contents,
1932 if (!capture_sources) {
1934 goto out_free_source_list_contents;
1936 capture_sources_malloced = true;
1938 /* Set up capture source in non-source-list mode. */
1939 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1940 capture_sources[0].fs_source_path = source;
1941 capture_sources[0].wim_target_path = NULL;
1942 capture_sources[0].reserved = 0;
1944 capture_sources_malloced = false;
1945 source_list_contents = NULL;
1949 /* Read and parse capture configuration file. */
1952 config_str = file_get_text_contents(config_file, &config_len);
1955 goto out_free_capture_sources;
1958 config = alloca(sizeof(*config));
1959 ret = parse_capture_config(&config_str, config_len, config);
1961 goto out_free_config;
1963 /* No capture configuration file specified; use default
1964 * configuration for capturing Windows operating systems. */
1966 add_image_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
1969 /* Open the existing WIM, or create a new one. */
1970 if (cmd == CMD_APPEND)
1971 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1972 imagex_progress_func);
1974 ret = wimlib_create_new_wim(compression_type, &wim);
1976 goto out_free_config;
1978 /* Set chunk size if non-default. */
1979 if (chunk_size != UINT32_MAX) {
1980 ret = wimlib_set_output_chunk_size(wim, chunk_size);
1986 /* Detect if source is regular file or block device and set NTFS volume
1991 if (tstat(source, &stbuf) == 0) {
1992 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1993 imagex_printf(T("Capturing WIM image from NTFS "
1994 "filesystem on \"%"TS"\"\n"), source);
1995 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1998 if (errno != ENOENT) {
1999 imagex_error_with_errno(T("Failed to stat "
2000 "\"%"TS"\""), source);
2008 /* If the user did not specify an image name, and the basename of the
2009 * source already exists as an image name in the WIM file, append a
2010 * suffix to make it unique. */
2011 if (cmd == CMD_APPEND && name_defaulted) {
2012 unsigned long conflict_idx;
2013 tchar *name_end = tstrchr(name, T('\0'));
2014 for (conflict_idx = 1;
2015 wimlib_image_name_in_use(wim, name);
2018 tsprintf(name_end, T(" (%lu)"), conflict_idx);
2022 /* If capturing a delta WIM, reference resources from the base WIMs
2023 * before adding the new image. */
2024 if (base_wimfiles.num_strings) {
2025 base_wims = calloc(base_wimfiles.num_strings,
2026 sizeof(base_wims[0]));
2027 if (base_wims == NULL) {
2028 imagex_error(T("Out of memory!"));
2033 for (size_t i = 0; i < base_wimfiles.num_strings; i++) {
2034 ret = wimlib_open_wim(base_wimfiles.strings[i],
2035 open_flags, &base_wims[i],
2036 imagex_progress_func);
2038 goto out_free_base_wims;
2042 ret = wimlib_reference_resources(wim, base_wims,
2043 base_wimfiles.num_strings, 0);
2045 goto out_free_base_wims;
2047 if (base_wimfiles.num_strings == 1) {
2048 imagex_printf(T("Capturing delta WIM based on \"%"TS"\"\n"),
2049 base_wimfiles.strings[0]);
2051 imagex_printf(T("Capturing delta WIM based on %u WIMs\n"),
2052 base_wimfiles.num_strings);
2059 /* If capturing or appending as an update of an existing (template) image,
2060 * open the WIM if needed and parse the image index. */
2061 if (template_image_name_or_num) {
2064 if (base_wimfiles.num_strings == 1 &&
2065 template_wimfile == base_wimfiles.strings[0]) {
2066 template_wim = base_wims[0];
2067 } else if (template_wimfile == wimfile) {
2070 ret = wimlib_open_wim(template_wimfile, open_flags,
2071 &template_wim, imagex_progress_func);
2073 goto out_free_base_wims;
2076 template_image = wimlib_resolve_image(template_wim,
2077 template_image_name_or_num);
2079 if (template_image_name_or_num[0] == T('-')) {
2082 struct wimlib_wim_info info;
2084 wimlib_get_wim_info(template_wim, &info);
2085 n = tstrtoul(template_image_name_or_num + 1, &tmp, 10);
2086 if (n >= 1 && n <= info.image_count &&
2088 tmp != template_image_name_or_num + 1)
2090 template_image = info.image_count - (n - 1);
2093 ret = verify_image_exists_and_is_single(template_image,
2094 template_image_name_or_num,
2097 goto out_free_template_wim;
2099 template_wim = NULL;
2102 ret = wimlib_add_image_multisource(wim,
2108 imagex_progress_func);
2110 goto out_free_template_wim;
2112 if (desc || flags_element || template_image_name_or_num) {
2113 /* User provided <DESCRIPTION> or <FLAGS> element, or an image
2114 * on which the added one is to be based has been specified with
2115 * --update-of. Get the index of the image we just
2116 * added, then use it to call the appropriate functions. */
2117 struct wimlib_wim_info info;
2119 wimlib_get_wim_info(wim, &info);
2122 ret = wimlib_set_image_descripton(wim,
2126 goto out_free_template_wim;
2129 if (flags_element) {
2130 ret = wimlib_set_image_flags(wim, info.image_count,
2133 goto out_free_template_wim;
2136 /* Reference template image if the user provided one. */
2137 if (template_image_name_or_num) {
2138 imagex_printf(T("Using image %d "
2139 "from \"%"TS"\" as template\n"),
2140 template_image, template_wimfile);
2141 ret = wimlib_reference_template_image(wim,
2147 goto out_free_template_wim;
2151 /* Write the new WIM or overwrite the existing WIM with the new image
2153 if (cmd == CMD_APPEND) {
2154 ret = wimlib_overwrite(wim, write_flags, num_threads,
2155 imagex_progress_func);
2156 } else if (wimfile) {
2157 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
2158 write_flags, num_threads,
2159 imagex_progress_func);
2161 ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
2162 write_flags, num_threads,
2163 imagex_progress_func);
2165 out_free_template_wim:
2166 /* template_wim may alias base_wims[0] or wim. */
2167 if ((base_wimfiles.num_strings != 1 || template_wim != base_wims[0]) &&
2168 template_wim != wim)
2169 wimlib_free(template_wim);
2171 for (size_t i = 0; i < base_wimfiles.num_strings; i++)
2172 wimlib_free(base_wims[i]);
2178 free(config->exclusion_pats.pats);
2179 free(config->exclusion_exception_pats.pats);
2182 out_free_capture_sources:
2183 if (capture_sources_malloced)
2184 free(capture_sources);
2185 out_free_source_list_contents:
2186 free(source_list_contents);
2187 out_free_base_wimfiles:
2188 string_set_destroy(&base_wimfiles);
2195 goto out_free_base_wimfiles;
2198 /* Remove image(s) from a WIM. */
2200 imagex_delete(int argc, tchar **argv, int cmd)
2203 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2204 int write_flags = 0;
2205 const tchar *wimfile;
2206 const tchar *image_num_or_name;
2211 for_opt(c, delete_options) {
2213 case IMAGEX_CHECK_OPTION:
2214 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2215 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2217 case IMAGEX_SOFT_OPTION:
2218 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
2229 imagex_error(T("Must specify a WIM file"));
2231 imagex_error(T("Must specify an image"));
2235 image_num_or_name = argv[1];
2237 ret = wimlib_open_wim(wimfile, open_flags, &wim,
2238 imagex_progress_func);
2242 image = wimlib_resolve_image(wim, image_num_or_name);
2244 ret = verify_image_exists(image, image_num_or_name, wimfile);
2246 goto out_wimlib_free;
2248 ret = wimlib_delete_image(wim, image);
2250 imagex_error(T("Failed to delete image from \"%"TS"\""),
2252 goto out_wimlib_free;
2255 ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
2257 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2258 "deleted"), wimfile);
2266 usage(CMD_DELETE, stderr);
2272 print_full_path(const struct wimlib_dir_entry *wdentry, void *_ignore)
2274 int ret = tprintf(T("%"TS"\n"), wdentry->full_path);
2275 return (ret >= 0) ? 0 : -1;
2278 /* Print the files contained in an image(s) in a WIM file. */
2280 imagex_dir(int argc, tchar **argv, int cmd)
2282 const tchar *wimfile;
2283 WIMStruct *wim = NULL;
2286 const tchar *path = T("");
2289 for_opt(c, dir_options) {
2291 case IMAGEX_PATH_OPTION:
2302 imagex_error(T("Must specify a WIM file"));
2306 imagex_error(T("Too many arguments"));
2311 ret = wimlib_open_wim(wimfile, 0, &wim, imagex_progress_func);
2316 image = wimlib_resolve_image(wim, argv[1]);
2317 ret = verify_image_exists(image, argv[1], wimfile);
2319 goto out_wimlib_free;
2321 /* No image specified; default to image 1, but only if the WIM
2322 * contains exactly one image. */
2324 struct wimlib_wim_info info;
2326 wimlib_get_wim_info(wim, &info);
2327 if (info.image_count != 1) {
2328 imagex_error(T("\"%"TS"\" contains %d images; Please "
2329 "select one (or all)."),
2330 wimfile, info.image_count);
2337 ret = wimlib_iterate_dir_tree(wim, image, path,
2338 WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE,
2339 print_full_path, NULL);
2346 usage(CMD_DIR, stderr);
2351 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2354 imagex_export(int argc, tchar **argv, int cmd)
2358 int export_flags = 0;
2359 int write_flags = 0;
2360 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2361 const tchar *src_wimfile;
2362 const tchar *src_image_num_or_name;
2363 const tchar *dest_wimfile;
2365 const tchar *dest_name;
2366 const tchar *dest_desc;
2368 struct wimlib_wim_info src_info;
2369 WIMStruct *dest_wim;
2374 STRING_SET(refglobs);
2375 unsigned num_threads = 0;
2377 for_opt(c, export_options) {
2379 case IMAGEX_BOOT_OPTION:
2380 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2382 case IMAGEX_CHECK_OPTION:
2383 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2384 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2386 case IMAGEX_NOCHECK_OPTION:
2387 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2389 case IMAGEX_COMPRESS_OPTION:
2390 compression_type = get_compression_type(optarg);
2391 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2394 case IMAGEX_REF_OPTION:
2395 ret = string_set_append(&refglobs, optarg);
2397 goto out_free_refglobs;
2399 case IMAGEX_THREADS_OPTION:
2400 num_threads = parse_num_threads(optarg);
2401 if (num_threads == UINT_MAX)
2404 case IMAGEX_REBUILD_OPTION:
2405 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2407 case IMAGEX_PIPABLE_OPTION:
2408 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2410 case IMAGEX_NOT_PIPABLE_OPTION:
2411 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
2419 if (argc < 3 || argc > 5)
2421 src_wimfile = argv[0];
2422 src_image_num_or_name = argv[1];
2423 dest_wimfile = argv[2];
2424 dest_name = (argc >= 4) ? argv[3] : NULL;
2425 dest_desc = (argc >= 5) ? argv[4] : NULL;
2426 ret = wimlib_open_wim(src_wimfile, open_flags, &src_wim,
2427 imagex_progress_func);
2429 goto out_free_refglobs;
2431 wimlib_get_wim_info(src_wim, &src_info);
2433 /* Determine if the destination is an existing file or not. If so, we
2434 * try to append the exported image(s) to it; otherwise, we create a new
2435 * WIM containing the exported image(s). Furthermore, determine if we
2436 * need to write a pipable WIM directly to standard output. */
2438 if (tstrcmp(dest_wimfile, T("-")) == 0) {
2440 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2441 imagex_error("Can't write a non-pipable WIM to "
2442 "standard output! Specify --pipable\n"
2443 " if you want to create a pipable WIM "
2444 "(but read the docs first).");
2446 goto out_free_src_wim;
2449 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2451 dest_wimfile = NULL;
2452 dest_wim_fd = STDOUT_FILENO;
2453 imagex_info_file = stderr;
2454 set_fd_to_binary_mode(dest_wim_fd);
2457 if (dest_wimfile != NULL && tstat(dest_wimfile, &stbuf) == 0) {
2459 /* Destination file exists. */
2461 if (!S_ISREG(stbuf.st_mode)) {
2462 imagex_error(T("\"%"TS"\" is not a regular file"),
2465 goto out_free_src_wim;
2467 ret = wimlib_open_wim(dest_wimfile,
2468 open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2469 &dest_wim, imagex_progress_func);
2471 goto out_free_src_wim;
2473 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2474 /* The user specified a compression type, but we're
2475 * exporting to an existing WIM. Make sure the
2476 * specified compression type is the same as the
2477 * compression type of the existing destination WIM. */
2478 struct wimlib_wim_info dest_info;
2480 wimlib_get_wim_info(dest_wim, &dest_info);
2481 if (compression_type != dest_info.compression_type) {
2482 imagex_error(T("Cannot specify a compression type that is "
2483 "not the same as that used in the "
2484 "destination WIM"));
2486 goto out_free_dest_wim;
2492 if (errno != ENOENT) {
2493 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2496 goto out_free_src_wim;
2499 /* dest_wimfile is not an existing file, so create a new WIM. */
2501 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2502 /* The user did not specify a compression type; default
2503 * to that of the source WIM. */
2505 compression_type = src_info.compression_type;
2507 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2509 goto out_free_src_wim;
2511 wimlib_set_output_chunk_size(dest_wim, src_info.chunk_size);
2514 image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2515 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2517 goto out_free_dest_wim;
2519 if (refglobs.num_strings) {
2520 ret = wim_reference_globs(src_wim, &refglobs, open_flags);
2522 goto out_free_dest_wim;
2525 if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
2526 image == WIMLIB_ALL_IMAGES && src_info.boot_index == 0)
2528 imagex_error(T("--boot specified for all-images export, but source WIM "
2529 "has no bootable image."));
2531 goto out_free_dest_wim;
2534 ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2535 dest_desc, export_flags, imagex_progress_func);
2537 if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2538 do_resource_not_found_warning(src_wimfile,
2539 &src_info, &refglobs);
2541 goto out_free_dest_wim;
2545 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2546 imagex_progress_func);
2547 else if (dest_wimfile)
2548 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2549 write_flags, num_threads,
2550 imagex_progress_func);
2552 ret = wimlib_write_to_fd(dest_wim, dest_wim_fd,
2553 WIMLIB_ALL_IMAGES, write_flags,
2554 num_threads, imagex_progress_func);
2556 wimlib_free(dest_wim);
2558 wimlib_free(src_wim);
2560 string_set_destroy(&refglobs);
2564 usage(CMD_EXPORT, stderr);
2567 goto out_free_refglobs;
2571 is_root_wim_path(const tchar *path)
2574 for (p = path; *p; p++)
2575 if (*p != T('\\') && *p != T('/'))
2581 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2582 const tchar *dest_dir)
2584 for (size_t i = 0; i < num_cmds; i++)
2585 if (cmds[i].fs_dest_path != dest_dir)
2586 free(cmds[i].fs_dest_path);
2590 static struct wimlib_extract_command *
2591 prepare_extract_commands(tchar **paths, unsigned num_paths,
2592 int extract_flags, tchar *dest_dir,
2593 size_t *num_cmds_ret)
2595 struct wimlib_extract_command *cmds;
2597 tchar *emptystr = T("");
2599 if (num_paths == 0) {
2603 num_cmds = num_paths;
2604 cmds = calloc(num_cmds, sizeof(cmds[0]));
2606 imagex_error(T("Out of memory!"));
2610 for (size_t i = 0; i < num_cmds; i++) {
2611 cmds[i].extract_flags = extract_flags;
2612 cmds[i].wim_source_path = paths[i];
2613 if (is_root_wim_path(paths[i])) {
2614 cmds[i].fs_dest_path = dest_dir;
2616 size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2617 cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2618 if (!cmds[i].fs_dest_path) {
2619 free_extract_commands(cmds, num_cmds, dest_dir);
2622 tsprintf(cmds[i].fs_dest_path,
2623 T("%"TS""OS_PREFERRED_PATH_SEPARATOR_STRING"%"TS),
2624 dest_dir, tbasename(paths[i]));
2627 *num_cmds_ret = num_cmds;
2631 /* Extract files or directories from a WIM image */
2633 imagex_extract(int argc, tchar **argv, int cmd)
2640 const tchar *wimfile;
2641 const tchar *image_num_or_name;
2642 tchar *dest_dir = T(".");
2643 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL | WIMLIB_EXTRACT_FLAG_NORPFIX;
2645 STRING_SET(refglobs);
2647 struct wimlib_extract_command *cmds;
2650 for_opt(c, extract_options) {
2652 case IMAGEX_CHECK_OPTION:
2653 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2655 case IMAGEX_VERBOSE_OPTION:
2656 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
2658 case IMAGEX_REF_OPTION:
2659 ret = string_set_append(&refglobs, optarg);
2661 goto out_free_refglobs;
2663 case IMAGEX_UNIX_DATA_OPTION:
2664 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2666 case IMAGEX_NO_ACLS_OPTION:
2667 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2669 case IMAGEX_STRICT_ACLS_OPTION:
2670 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2672 case IMAGEX_DEST_DIR_OPTION:
2675 case IMAGEX_TO_STDOUT_OPTION:
2676 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2677 imagex_info_file = stderr;
2678 imagex_be_quiet = true;
2680 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2681 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2682 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2695 image_num_or_name = argv[1];
2700 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2705 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2709 image = wimlib_resolve_image(wim, image_num_or_name);
2710 ret = verify_image_exists_and_is_single(image,
2714 goto out_wimlib_free;
2716 if (refglobs.num_strings) {
2717 ret = wim_reference_globs(wim, &refglobs, open_flags);
2719 goto out_wimlib_free;
2722 ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
2723 imagex_progress_func);
2725 if (!imagex_be_quiet)
2726 imagex_printf(T("Done extracting files.\n"));
2727 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2728 tfprintf(stderr, T("Note: You can use `%"TS"' to see what "
2729 "files and directories\n"
2730 " are in the WIM image.\n"),
2731 get_cmd_string(CMD_INFO, false));
2732 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2733 struct wimlib_wim_info info;
2735 wimlib_get_wim_info(wim, &info);
2736 do_resource_not_found_warning(wimfile, &info, &refglobs);
2741 free_extract_commands(cmds, num_cmds, dest_dir);
2743 string_set_destroy(&refglobs);
2747 usage(CMD_EXTRACT, stderr);
2750 goto out_free_refglobs;
2753 static void print_byte_field(const uint8_t field[], size_t len)
2756 tprintf(T("%02hhx"), *field++);
2760 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2762 tputs(T("WIM Information:"));
2763 tputs(T("----------------"));
2764 tprintf(T("Path: %"TS"\n"), wimfile);
2765 tprintf(T("GUID: 0x"));
2766 print_byte_field(info->guid, sizeof(info->guid));
2768 tprintf(T("Version: %u\n"), info->wim_version);
2769 tprintf(T("Image Count: %d\n"), info->image_count);
2770 tprintf(T("Compression: %"TS"\n"),
2771 wimlib_get_compression_type_string(info->compression_type));
2772 tprintf(T("Chunk Size: %"PRIu32" bytes\n"),
2774 tprintf(T("Part Number: %d/%d\n"), info->part_number, info->total_parts);
2775 tprintf(T("Boot Index: %d\n"), info->boot_index);
2776 tprintf(T("Size: %"PRIu64" bytes\n"), info->total_bytes);
2777 tprintf(T("Integrity Info: %"TS"\n"),
2778 info->has_integrity_table ? T("yes") : T("no"));
2779 tprintf(T("Relative path junction: %"TS"\n"),
2780 info->has_rpfix ? T("yes") : T("no"));
2781 tprintf(T("Pipable: %"TS"\n"),
2782 info->pipable ? T("yes") : T("no"));
2787 print_resource(const struct wimlib_resource_entry *resource,
2790 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
2791 resource->uncompressed_size);
2792 if (resource->packed) {
2793 tprintf(T("Raw compressed size = %"PRIu64" bytes\n"),
2794 resource->raw_resource_compressed_size);
2796 tprintf(T("Raw offset in WIM = %"PRIu64" bytes\n"),
2797 resource->raw_resource_offset_in_wim);
2799 tprintf(T("Offset in raw = %"PRIu64" bytes\n"),
2802 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
2803 resource->compressed_size);
2805 tprintf(T("Offset in WIM = %"PRIu64" bytes\n"),
2809 tprintf(T("Part Number = %u\n"), resource->part_number);
2810 tprintf(T("Reference Count = %u\n"), resource->reference_count);
2812 tprintf(T("Hash = 0x"));
2813 print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2816 tprintf(T("Flags = "));
2817 if (resource->is_compressed)
2818 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED "));
2819 if (resource->is_metadata)
2820 tprintf(T("WIM_RESHDR_FLAG_METADATA "));
2821 if (resource->is_free)
2822 tprintf(T("WIM_RESHDR_FLAG_FREE "));
2823 if (resource->is_spanned)
2824 tprintf(T("WIM_RESHDR_FLAG_SPANNED "));
2825 if (resource->packed)
2826 tprintf(T("WIM_RESHDR_FLAG_PACKED_STREAMS "));
2833 print_lookup_table(WIMStruct *wim)
2835 wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2838 /* Prints information about a WIM file; also can mark an image as bootable,
2839 * change the name of an image, or change the description of an image. */
2841 imagex_info(int argc, tchar **argv, int cmd)
2846 bool nocheck = false;
2847 bool header = false;
2848 bool lookup_table = false;
2850 bool metadata = false;
2851 bool short_header = true;
2852 const tchar *xml_out_file = NULL;
2853 const tchar *wimfile;
2854 const tchar *image_num_or_name;
2855 const tchar *new_name;
2856 const tchar *new_desc;
2861 struct wimlib_wim_info info;
2863 for_opt(c, info_options) {
2865 case IMAGEX_BOOT_OPTION:
2868 case IMAGEX_CHECK_OPTION:
2871 case IMAGEX_NOCHECK_OPTION:
2874 case IMAGEX_HEADER_OPTION:
2876 short_header = false;
2878 case IMAGEX_LOOKUP_TABLE_OPTION:
2879 lookup_table = true;
2880 short_header = false;
2882 case IMAGEX_XML_OPTION:
2884 short_header = false;
2886 case IMAGEX_EXTRACT_XML_OPTION:
2887 xml_out_file = optarg;
2888 short_header = false;
2890 case IMAGEX_METADATA_OPTION:
2892 short_header = false;
2901 if (argc < 1 || argc > 4)
2905 image_num_or_name = (argc >= 2) ? argv[1] : T("all");
2906 new_name = (argc >= 3) ? argv[2] : NULL;
2907 new_desc = (argc >= 4) ? argv[3] : NULL;
2909 if (check && nocheck) {
2910 imagex_error(T("Can't specify both --check and --nocheck"));
2915 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2917 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2921 wimlib_get_wim_info(wim, &info);
2923 image = wimlib_resolve_image(wim, image_num_or_name);
2924 ret = WIMLIB_ERR_INVALID_IMAGE;
2925 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
2926 verify_image_exists(image, image_num_or_name, wimfile);
2928 imagex_error(T("If you would like to set the boot "
2929 "index to 0, specify image \"0\" with "
2930 "the --boot flag."));
2932 goto out_wimlib_free;
2935 if (boot && info.image_count == 0) {
2936 imagex_error(T("--boot is meaningless on a WIM with no images"));
2937 goto out_wimlib_free;
2940 if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
2942 imagex_error(T("Cannot specify the --boot flag "
2943 "without specifying a specific "
2944 "image in a multi-image WIM"));
2945 goto out_wimlib_free;
2948 imagex_error(T("Cannot specify the NEW_NAME "
2949 "without specifying a specific "
2950 "image in a multi-image WIM"));
2951 goto out_wimlib_free;
2955 /* Operations that print information are separated from operations that
2956 * recreate the WIM file. */
2957 if (!new_name && !boot) {
2959 /* Read-only operations */
2961 if (image == WIMLIB_NO_IMAGE) {
2962 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
2963 image_num_or_name, wimfile);
2964 goto out_wimlib_free;
2967 if (image == WIMLIB_ALL_IMAGES && short_header)
2968 print_wim_information(wimfile, &info);
2971 wimlib_print_header(wim);
2974 if (info.total_parts != 1) {
2975 tfprintf(stderr, T("Warning: Only showing the lookup table "
2976 "for part %d of a %d-part WIM.\n"),
2977 info.part_number, info.total_parts);
2979 print_lookup_table(wim);
2983 ret = wimlib_extract_xml_data(wim, stdout);
2985 goto out_wimlib_free;
2991 fp = tfopen(xml_out_file, T("wb"));
2993 imagex_error_with_errno(T("Failed to open the "
2994 "file \"%"TS"\" for "
2998 goto out_wimlib_free;
3000 ret = wimlib_extract_xml_data(wim, fp);
3002 imagex_error(T("Failed to close the file "
3008 goto out_wimlib_free;
3012 wimlib_print_available_images(wim, image);
3015 ret = wimlib_print_metadata(wim, image);
3017 goto out_wimlib_free;
3022 /* Modification operations */
3024 if (image == WIMLIB_ALL_IMAGES)
3027 if (image == WIMLIB_NO_IMAGE && new_name) {
3028 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
3029 "when using image 0"), new_name);
3031 goto out_wimlib_free;
3035 if (image == info.boot_index) {
3036 imagex_printf(T("Image %d is already marked as "
3037 "bootable.\n"), image);
3040 imagex_printf(T("Marking image %d as bootable.\n"),
3042 info.boot_index = image;
3043 ret = wimlib_set_wim_info(wim, &info,
3044 WIMLIB_CHANGE_BOOT_INDEX);
3046 goto out_wimlib_free;
3050 if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
3052 imagex_printf(T("Image %d is already named \"%"TS"\".\n"),
3056 imagex_printf(T("Changing the name of image %d to "
3057 "\"%"TS"\".\n"), image, new_name);
3058 ret = wimlib_set_image_name(wim, image, new_name);
3060 goto out_wimlib_free;
3064 const tchar *old_desc;
3065 old_desc = wimlib_get_image_description(wim, image);
3066 if (old_desc && !tstrcmp(old_desc, new_desc)) {
3067 imagex_printf(T("The description of image %d is already "
3068 "\"%"TS"\".\n"), image, new_desc);
3071 imagex_printf(T("Changing the description of image %d "
3072 "to \"%"TS"\".\n"), image, new_desc);
3073 ret = wimlib_set_image_descripton(wim, image,
3076 goto out_wimlib_free;
3080 /* Only call wimlib_overwrite() if something actually needs to
3082 if (boot || new_name || new_desc ||
3083 (check && !info.has_integrity_table) ||
3084 (nocheck && info.has_integrity_table))
3086 int write_flags = 0;
3089 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3091 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3092 ret = wimlib_overwrite(wim, write_flags, 1,
3093 imagex_progress_func);
3095 imagex_printf(T("The file \"%"TS"\" was not modified "
3096 "because nothing needed to be done.\n"),
3107 usage(CMD_INFO, stderr);
3113 /* Join split WIMs into one part WIM */
3115 imagex_join(int argc, tchar **argv, int cmd)
3118 int swm_open_flags = 0;
3119 int wim_write_flags = 0;
3120 const tchar *output_path;
3123 for_opt(c, join_options) {
3125 case IMAGEX_CHECK_OPTION:
3126 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3127 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3137 imagex_error(T("Must specify one or more split WIM (.swm) "
3141 output_path = argv[0];
3142 ret = wimlib_join((const tchar * const *)++argv,
3147 imagex_progress_func);
3152 usage(CMD_JOIN, stderr);
3157 #if WIM_MOUNTING_SUPPORTED
3159 /* Mounts a WIM image. */
3161 imagex_mount_rw_or_ro(int argc, tchar **argv, int cmd)
3164 int mount_flags = 0;
3166 const tchar *staging_dir = NULL;
3167 const tchar *wimfile;
3170 struct wimlib_wim_info info;
3174 STRING_SET(refglobs);
3176 if (cmd == CMD_MOUNTRW) {
3177 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
3178 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3181 for_opt(c, mount_options) {
3183 case IMAGEX_ALLOW_OTHER_OPTION:
3184 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
3186 case IMAGEX_CHECK_OPTION:
3187 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3189 case IMAGEX_DEBUG_OPTION:
3190 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
3192 case IMAGEX_STREAMS_INTERFACE_OPTION:
3193 if (!tstrcasecmp(optarg, T("none")))
3194 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
3195 else if (!tstrcasecmp(optarg, T("xattr")))
3196 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
3197 else if (!tstrcasecmp(optarg, T("windows")))
3198 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
3200 imagex_error(T("Unknown stream interface \"%"TS"\""),
3205 case IMAGEX_REF_OPTION:
3206 ret = string_set_append(&refglobs, optarg);
3208 goto out_free_refglobs;
3210 case IMAGEX_STAGING_DIR_OPTION:
3211 staging_dir = optarg;
3213 case IMAGEX_UNIX_DATA_OPTION:
3214 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
3222 if (argc != 2 && argc != 3)
3227 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3229 goto out_free_refglobs;
3231 wimlib_get_wim_info(wim, &info);
3234 /* Image explicitly specified. */
3235 image = wimlib_resolve_image(wim, argv[1]);
3237 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
3241 /* No image specified; default to image 1, but only if the WIM
3242 * contains exactly one image. */
3244 if (info.image_count != 1) {
3245 imagex_error(T("\"%"TS"\" contains %d images; Please "
3246 "select one."), wimfile, info.image_count);
3254 if (refglobs.num_strings) {
3255 ret = wim_reference_globs(wim, &refglobs, open_flags);
3260 ret = wimlib_mount_image(wim, image, dir, mount_flags, staging_dir);
3262 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
3264 image, wimfile, dir);
3269 string_set_destroy(&refglobs);
3275 goto out_free_refglobs;
3277 #endif /* WIM_MOUNTING_SUPPORTED */
3279 /* Rebuild a WIM file */
3281 imagex_optimize(int argc, tchar **argv, int cmd)
3284 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3285 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
3286 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
3287 uint32_t chunk_size = UINT32_MAX;
3290 const tchar *wimfile;
3293 unsigned num_threads = 0;
3295 for_opt(c, optimize_options) {
3297 case IMAGEX_CHECK_OPTION:
3298 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3299 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3301 case IMAGEX_NOCHECK_OPTION:
3302 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3304 case IMAGEX_COMPRESS_OPTION:
3305 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3306 compression_type = get_compression_type(optarg);
3307 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
3310 case IMAGEX_RECOMPRESS_OPTION:
3311 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3313 case IMAGEX_COMPRESS_SLOW_OPTION:
3314 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3315 compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
3316 ret = set_compress_slow();
3320 case IMAGEX_CHUNK_SIZE_OPTION:
3321 chunk_size = parse_chunk_size(optarg);
3322 if (chunk_size == UINT32_MAX)
3325 case IMAGEX_PACK_STREAMS_OPTION:
3326 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
3327 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3329 case IMAGEX_NO_PACK_STREAMS_OPTION:
3330 write_flags |= WIMLIB_WRITE_FLAG_NO_PACK_STREAMS;
3332 case IMAGEX_THREADS_OPTION:
3333 num_threads = parse_num_threads(optarg);
3334 if (num_threads == UINT_MAX)
3337 case IMAGEX_PIPABLE_OPTION:
3338 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
3340 case IMAGEX_NOT_PIPABLE_OPTION:
3341 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
3355 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3359 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
3360 /* Change compression type. */
3361 ret = wimlib_set_output_compression_type(wim, compression_type);
3363 goto out_wimlib_free;
3366 if (chunk_size != UINT32_MAX) {
3367 /* Change chunk size. */
3368 ret = wimlib_set_output_chunk_size(wim, chunk_size);
3370 goto out_wimlib_free;
3373 old_size = file_get_size(wimfile);
3374 tprintf(T("\"%"TS"\" original size: "), wimfile);
3376 tputs(T("Unknown"));
3378 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3380 ret = wimlib_overwrite(wim, write_flags, num_threads,
3381 imagex_progress_func);
3383 imagex_error(T("Optimization of \"%"TS"\" failed."), wimfile);
3384 goto out_wimlib_free;
3387 new_size = file_get_size(wimfile);
3388 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3390 tputs(T("Unknown"));
3392 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3394 tfputs(T("Space saved: "), stdout);
3395 if (new_size != -1 && old_size != -1) {
3396 tprintf(T("%lld KiB\n"),
3397 ((long long)old_size - (long long)new_size) >> 10);
3399 tputs(T("Unknown"));
3408 usage(CMD_OPTIMIZE, stderr);
3414 /* Split a WIM into a spanned set */
3416 imagex_split(int argc, tchar **argv, int cmd)
3420 int write_flags = 0;
3421 unsigned long part_size;
3426 for_opt(c, split_options) {
3428 case IMAGEX_CHECK_OPTION:
3429 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3430 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3442 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3443 if (tmp == argv[2] || *tmp) {
3444 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3445 imagex_error(T("The part size must be an integer or "
3446 "floating-point number of megabytes."));
3449 ret = wimlib_open_wim(argv[0], open_flags, &wim, imagex_progress_func);
3453 ret = wimlib_split(wim, argv[1], part_size, write_flags, imagex_progress_func);
3459 usage(CMD_SPLIT, stderr);
3465 #if WIM_MOUNTING_SUPPORTED
3466 /* Unmounts a mounted WIM image. */
3468 imagex_unmount(int argc, tchar **argv, int cmd)
3471 int unmount_flags = 0;
3474 for_opt(c, unmount_options) {
3476 case IMAGEX_COMMIT_OPTION:
3477 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3479 case IMAGEX_CHECK_OPTION:
3480 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3482 case IMAGEX_REBUILD_OPTION:
3483 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3485 case IMAGEX_LAZY_OPTION:
3486 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3497 ret = wimlib_unmount_image(argv[0], unmount_flags,
3498 imagex_progress_func);
3500 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3505 usage(CMD_UNMOUNT, stderr);
3509 #endif /* WIM_MOUNTING_SUPPORTED */
3512 * Add, delete, or rename files in a WIM image.
3515 imagex_update(int argc, tchar **argv, int cmd)
3517 const tchar *wimfile;
3521 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3522 int write_flags = 0;
3523 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3524 int default_add_flags = WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
3525 int default_delete_flags = 0;
3526 unsigned num_threads = 0;
3528 tchar *cmd_file_contents;
3529 size_t cmd_file_nchars;
3530 struct wimlib_update_command *cmds;
3532 tchar *command_str = NULL;
3534 const tchar *config_file = NULL;
3536 struct wimlib_capture_config *config;
3538 for_opt(c, update_options) {
3540 /* Generic or write options */
3541 case IMAGEX_THREADS_OPTION:
3542 num_threads = parse_num_threads(optarg);
3543 if (num_threads == UINT_MAX)
3546 case IMAGEX_CHECK_OPTION:
3547 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3548 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3550 case IMAGEX_REBUILD_OPTION:
3551 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3553 case IMAGEX_COMMAND_OPTION:
3555 imagex_error(T("--command may only be specified "
3556 "one time. Please provide\n"
3557 " the update commands "
3558 "on standard input instead."));
3561 command_str = tstrdup(optarg);
3563 imagex_error(T("Out of memory!"));
3567 /* Default delete options */
3568 case IMAGEX_FORCE_OPTION:
3569 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3571 case IMAGEX_RECURSIVE_OPTION:
3572 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3575 /* Global add option */
3576 case IMAGEX_CONFIG_OPTION:
3577 default_add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
3578 config_file = optarg;
3581 /* Default add options */
3582 case IMAGEX_VERBOSE_OPTION:
3583 default_add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
3585 case IMAGEX_DEREFERENCE_OPTION:
3586 default_add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
3588 case IMAGEX_UNIX_DATA_OPTION:
3589 default_add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
3591 case IMAGEX_NO_ACLS_OPTION:
3592 default_add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
3594 case IMAGEX_STRICT_ACLS_OPTION:
3595 default_add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
3604 if (argc != 1 && argc != 2)
3608 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3610 goto out_free_command_str;
3613 /* Image explicitly specified. */
3614 image = wimlib_resolve_image(wim, argv[1]);
3615 ret = verify_image_exists_and_is_single(image, argv[1],
3618 goto out_wimlib_free;
3620 /* No image specified; default to image 1, but only if the WIM
3621 * contains exactly one image. */
3622 struct wimlib_wim_info info;
3624 wimlib_get_wim_info(wim, &info);
3625 if (info.image_count != 1) {
3626 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3627 wimfile, info.image_count);
3634 /* Parse capture configuration file if specified */
3638 config_str = file_get_text_contents(config_file, &config_len);
3641 goto out_wimlib_free;
3644 config = alloca(sizeof(*config));
3645 ret = parse_capture_config(&config_str, config_len, config);
3647 goto out_free_config;
3650 default_add_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
3653 /* Read update commands from standard input, or the command string if
3656 cmd_file_contents = NULL;
3657 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3660 if (isatty(STDIN_FILENO)) {
3661 tputs(T("Reading update commands from standard input..."));
3662 recommend_man_page(CMD_UPDATE, stdout);
3664 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3665 if (!cmd_file_contents) {
3667 goto out_free_config;
3670 /* Parse the update commands */
3671 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3676 goto out_free_cmd_file_contents;
3679 /* Set default flags and capture config on the update commands */
3680 for (size_t i = 0; i < num_cmds; i++) {
3681 switch (cmds[i].op) {
3682 case WIMLIB_UPDATE_OP_ADD:
3683 cmds[i].add.add_flags |= default_add_flags;
3684 cmds[i].add.config = config;
3686 case WIMLIB_UPDATE_OP_DELETE:
3687 cmds[i].delete_.delete_flags |= default_delete_flags;
3694 /* Execute the update commands */
3695 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3696 imagex_progress_func);
3700 /* Overwrite the updated WIM */
3701 ret = wimlib_overwrite(wim, write_flags, num_threads,
3702 imagex_progress_func);
3705 out_free_cmd_file_contents:
3706 free(cmd_file_contents);
3709 free(config->exclusion_pats.pats);
3710 free(config->exclusion_exception_pats.pats);
3715 out_free_command_str:
3720 usage(CMD_UPDATE, stderr);
3723 goto out_free_command_str;
3728 struct imagex_command {
3730 int (*func)(int argc, tchar **argv, int cmd);
3733 static const struct imagex_command imagex_commands[] = {
3734 [CMD_APPEND] = {T("append"), imagex_capture_or_append},
3735 [CMD_APPLY] = {T("apply"), imagex_apply},
3736 [CMD_CAPTURE] = {T("capture"), imagex_capture_or_append},
3737 [CMD_DELETE] = {T("delete"), imagex_delete},
3738 [CMD_DIR ] = {T("dir"), imagex_dir},
3739 [CMD_EXPORT] = {T("export"), imagex_export},
3740 [CMD_EXTRACT] = {T("extract"), imagex_extract},
3741 [CMD_INFO] = {T("info"), imagex_info},
3742 [CMD_JOIN] = {T("join"), imagex_join},
3743 #if WIM_MOUNTING_SUPPORTED
3744 [CMD_MOUNT] = {T("mount"), imagex_mount_rw_or_ro},
3745 [CMD_MOUNTRW] = {T("mountrw"), imagex_mount_rw_or_ro},
3747 [CMD_OPTIMIZE] = {T("optimize"), imagex_optimize},
3748 [CMD_SPLIT] = {T("split"), imagex_split},
3749 #if WIM_MOUNTING_SUPPORTED
3750 [CMD_UNMOUNT] = {T("unmount"), imagex_unmount},
3752 [CMD_UPDATE] = {T("update"), imagex_update},
3755 static const tchar *usage_strings[] = {
3758 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3759 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot]\n"
3760 " [--check] [--nocheck] [--flags EDITION_ID] [--verbose]\n"
3761 " [--dereference] [--config=FILE] [--threads=NUM_THREADS]\n"
3762 " [--source-list] [--no-acls] [--strict-acls] [--rpfix]\n"
3763 " [--norpfix] [--unix-data] [--pipable]\n"
3764 " [--update-of=[WIMFILE:]IMAGE]\n"
3768 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME | all)]\n"
3769 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
3770 " [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n"
3771 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3772 " [--include-invalid-names]\n"
3776 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3777 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot]\n"
3778 " [--check] [--nocheck] [--compress=TYPE]\n"
3779 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
3780 " [--config=FILE] [--threads=NUM_THREADS] [--source-list]\n"
3781 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3782 " [--unix-data] [--pipable] [--update-of=[WIMFILE:]IMAGE]\n"
3783 " [--delta-from=WIMFILE]\n"
3787 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n"
3792 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH]\n"
3796 " %"TS" SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
3797 " DEST_WIMFILE [DEST_IMAGE_NAME [DEST_IMAGE_DESCRIPTION]]\n"
3798 " [--boot] [--check] [--nocheck] [--compress=TYPE]\n"
3799 " [--ref=\"GLOB\"] [--threads=NUM_THREADS] [--rebuild]\n"
3800 " [--pipable] [--not-pipable]\n"
3804 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME) [PATH...]\n"
3805 " [--check] [--ref=\"GLOB\"] [--verbose] [--unix-data]\n"
3806 " [--no-acls] [--strict-acls] [--to-stdout]\n"
3807 " [--dest-dir=CMD_DIR] [--include-invalid-names]\n"
3811 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME) [NEW_NAME\n"
3812 " [NEW_DESC]]] [--boot] [--check] [--nocheck] [--header]\n"
3813 " [--lookup-table] [--xml] [--extract-xml FILE]\n"
3818 " %"TS" OUT_WIMFILE SPLIT_WIM_PART... [--check]\n"
3820 #if WIM_MOUNTING_SUPPORTED
3823 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3824 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
3825 " [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n"
3829 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3830 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
3831 " [--staging-dir=CMD_DIR] [--unix-data] [--allow-other]\n"
3836 " %"TS" WIMFILE [--check] [--nocheck] [--recompress]\n"
3837 " [--recompress-slow] [--compress=TYPE]\n"
3838 " [--threads=NUM_THREADS] [--pipable] [--not-pipable]\n"
3842 " %"TS" WIMFILE SPLIT_WIM_PART_1 PART_SIZE_MB [--check]\n"
3844 #if WIM_MOUNTING_SUPPORTED
3847 " %"TS" DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
3852 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
3853 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
3854 " [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
3858 static const tchar *invocation_name;
3859 static int invocation_cmd = CMD_NONE;
3861 static const tchar *get_cmd_string(int cmd, bool nospace)
3863 static tchar buf[50];
3864 if (cmd == CMD_NONE) {
3865 tsprintf(buf, T("%"TS), T(IMAGEX_PROGNAME));
3866 } else if (invocation_cmd != CMD_NONE) {
3867 tsprintf(buf, T("wim%"TS), imagex_commands[cmd].name);
3869 const tchar *format;
3872 format = T("%"TS"-%"TS"");
3874 format = T("%"TS" %"TS"");
3875 tsprintf(buf, format, invocation_name, imagex_commands[cmd].name);
3883 static const tchar *s =
3885 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
3886 "Copyright (C) 2012, 2013 Eric Biggers\n"
3887 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
3888 "This is free software: you are free to change and redistribute it.\n"
3889 "There is NO WARRANTY, to the extent permitted by law.\n"
3891 "Report bugs to "PACKAGE_BUGREPORT".\n"
3898 help_or_version(int argc, tchar **argv, int cmd)
3903 for (i = 1; i < argc; i++) {
3905 if (p[0] == T('-') && p[1] == T('-')) {
3907 if (!tstrcmp(p, T("help"))) {
3908 if (cmd == CMD_NONE)
3913 } else if (!tstrcmp(p, T("version"))) {
3922 print_usage_string(int cmd, FILE *fp)
3924 tfprintf(fp, usage_strings[cmd], get_cmd_string(cmd, false));
3928 recommend_man_page(int cmd, FILE *fp)
3930 const tchar *format_str;
3932 format_str = T("See %"TS".pdf in the doc directory for more details.\n");
3934 format_str = T("Try `man %"TS"' for more details.\n");
3936 tfprintf(fp, format_str, get_cmd_string(cmd, true));
3940 usage(int cmd, FILE *fp)
3942 tfprintf(fp, T("Usage:\n"));
3943 print_usage_string(cmd, fp);
3944 tfprintf(fp, T("\n"));
3945 recommend_man_page(cmd, fp);
3951 tfprintf(fp, T("Usage:\n"));
3952 for (int cmd = 0; cmd < CMD_MAX; cmd++) {
3953 print_usage_string(cmd, fp);
3954 tfprintf(fp, T("\n"));
3956 static const tchar *extra =
3959 " %"TS" --version\n"
3961 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
3964 tfprintf(fp, extra, invocation_name, invocation_name);
3965 recommend_man_page(CMD_NONE, fp);
3968 /* Entry point for wimlib's ImageX implementation. On UNIX the command
3969 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
3970 * something else), while an Windows the command arguments will be UTF-16LE
3971 * encoded 'wchar_t' strings. */
3974 wmain(int argc, wchar_t **argv, wchar_t **envp)
3976 main(int argc, char **argv)
3983 imagex_info_file = stdout;
3984 invocation_name = tbasename(argv[0]);
3987 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
3988 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
3992 setlocale(LC_ALL, "");
3993 codeset = nl_langinfo(CODESET);
3994 if (!strstr(codeset, "UTF-8") &&
3995 !strstr(codeset, "UTF8") &&
3996 !strstr(codeset, "utf-8") &&
3997 !strstr(codeset, "utf8"))
4000 "WARNING: Running %"TS" in a UTF-8 locale is recommended!\n"
4001 " Maybe try: `export LANG=en_US.UTF-8'?\n"
4002 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
4003 " to any value to force wimlib to use UTF-8.\n",
4008 #endif /* !__WIN32__ */
4010 /* Allow being invoked as wimCOMMAND (e.g. wimapply). */
4012 if (!tstrncmp(invocation_name, T("wim"), 3) &&
4013 tstrcmp(invocation_name, T(IMAGEX_PROGNAME))) {
4014 for (int i = 0; i < CMD_MAX; i++) {
4015 if (!tstrcmp(invocation_name + 3,
4016 imagex_commands[i].name))
4025 /* Unless already known from the invocation name, determine which
4026 * command was specified. */
4027 if (cmd == CMD_NONE) {
4029 imagex_error(T("No command specified!\n"));
4033 for (int i = 0; i < CMD_MAX; i++) {
4034 if (!tstrcmp(argv[1], imagex_commands[i].name)) {
4039 if (cmd != CMD_NONE) {
4045 /* Handle --help and --version. --help can be either for the program as
4046 * a whole (cmd == CMD_NONE) or just for a specific command (cmd !=
4047 * CMD_NONE). Note: help_or_version() will not return if a --help or
4048 * --version argument was found. */
4049 help_or_version(argc, argv, cmd);
4051 /* Bail if a valid command was not specified. */
4052 if (cmd == CMD_NONE) {
4053 imagex_error(T("Unrecognized command: `%"TS"'\n"), argv[1]);
4058 /* Enable warning and error messages in wimlib be more user-friendly.
4060 wimlib_set_print_errors(true);
4062 /* Initialize wimlib. */
4063 ret = wimlib_global_init(init_flags);
4065 goto out_check_status;
4067 /* Call the command handler function. */
4068 ret = imagex_commands[cmd].func(argc, argv, cmd);
4070 /* Check for error writing to standard output, especially since for some
4071 * commands, writing to standard output is part of the program's actual
4072 * behavior and not just for informational purposes. */
4073 if (ferror(stdout) || fclose(stdout)) {
4074 imagex_error_with_errno(T("error writing to standard output"));
4079 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
4080 * itself (not by wimlib). 0 indicates success. > 0 indicates a wimlib
4081 * error code from which an error message can be printed. */
4083 imagex_error(T("Exiting with error code %d:\n"
4085 wimlib_get_error_string(ret));
4086 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
4087 imagex_error_with_errno(T("errno"));
4089 /* Make wimlib free any resources it's holding (although this is not
4090 * strictly necessary because the process is ending anyway). */
4091 wimlib_global_cleanup();