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_WILDCARDS_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_STRICT_WILDCARDS_OPTION,
162 IMAGEX_SYMLINK_OPTION,
163 IMAGEX_THREADS_OPTION,
164 IMAGEX_TO_STDOUT_OPTION,
165 IMAGEX_UNIX_DATA_OPTION,
166 IMAGEX_UPDATE_OF_OPTION,
167 IMAGEX_VERBOSE_OPTION,
171 static const struct option apply_options[] = {
172 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
173 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
174 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
175 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
176 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
177 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
178 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
179 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
180 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
181 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
182 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
183 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
185 /* --resume is undocumented for now as it needs improvement. */
186 {T("resume"), no_argument, NULL, IMAGEX_RESUME_OPTION},
190 static const struct option capture_or_append_options[] = {
191 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
192 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
193 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
194 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
195 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
196 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
197 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
198 {T("pack-streams"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
199 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
200 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
201 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
202 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
203 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
204 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
205 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
206 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
207 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
208 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
209 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
210 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
211 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
212 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
213 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
214 {T("update-of"), required_argument, NULL, IMAGEX_UPDATE_OF_OPTION},
215 {T("delta-from"), required_argument, NULL, IMAGEX_DELTA_FROM_OPTION},
219 static const struct option delete_options[] = {
220 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
221 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
225 static const struct option dir_options[] = {
226 {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
230 static const struct option export_options[] = {
231 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
232 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
233 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
234 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
235 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
236 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
237 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
238 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
239 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
240 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
241 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
242 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
246 static const struct option extract_options[] = {
247 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
248 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
249 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
250 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
251 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
252 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
253 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
254 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
255 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
256 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
257 {T("strict-wildcards"), no_argument, NULL, IMAGEX_STRICT_WILDCARDS_OPTION},
258 {T("no-wildcards"), no_argument, NULL, IMAGEX_NO_WILDCARDS_OPTION},
262 static const struct option info_options[] = {
263 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
264 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
265 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
266 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
267 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
268 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
269 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
270 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
271 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
275 static const struct option join_options[] = {
276 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
280 static const struct option mount_options[] = {
281 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
282 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
283 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
284 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
285 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
286 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
287 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
291 static const struct option optimize_options[] = {
292 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
293 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
294 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
295 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
296 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
297 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
298 {T("recompress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
299 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
300 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
301 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
302 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
303 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
307 static const struct option split_options[] = {
308 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
312 static const struct option unmount_options[] = {
313 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
314 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
315 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
316 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
320 static const struct option update_options[] = {
321 /* Careful: some of the options here set the defaults for update
322 * commands, but the flags given to an actual update command (and not to
323 * `imagex update' itself are also handled in
324 * update_command_add_option(). */
325 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
326 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
327 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
328 {T("command"), required_argument, NULL, IMAGEX_COMMAND_OPTION},
330 /* Default delete options */
331 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
332 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
334 /* Global add option */
335 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
337 /* Default add options */
338 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
339 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
340 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
341 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
342 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
343 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
349 # define _format_attribute(type, format_str, args_start) \
350 __attribute__((format(type, format_str, args_start)))
352 # define _format_attribute(type, format_str, args_start)
355 /* Print formatted error message to stderr. */
356 static void _format_attribute(printf, 1, 2)
357 imagex_error(const tchar *format, ...)
360 va_start(va, format);
361 tfputs(T("ERROR: "), stderr);
362 tvfprintf(stderr, format, va);
363 tputc(T('\n'), stderr);
367 /* Print formatted error message to stderr. */
368 static void _format_attribute(printf, 1, 2)
369 imagex_error_with_errno(const tchar *format, ...)
371 int errno_save = errno;
373 va_start(va, format);
374 tfputs(T("ERROR: "), stderr);
375 tvfprintf(stderr, format, va);
376 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
381 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
383 if (image == WIMLIB_NO_IMAGE) {
384 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
385 " Please specify a 1-based image index or "
386 "image name. To list the images\n"
387 " contained in the WIM archive, run\n"
389 " %"TS" \"%"TS"\"\n"),
390 image_name, wim_name,
391 get_cmd_string(CMD_INFO, false), wim_name);
392 return WIMLIB_ERR_INVALID_IMAGE;
398 verify_image_is_single(int image)
400 if (image == WIMLIB_ALL_IMAGES) {
401 imagex_error(T("Cannot specify all images for this action!"));
402 return WIMLIB_ERR_INVALID_IMAGE;
408 verify_image_exists_and_is_single(int image, const tchar *image_name,
409 const tchar *wim_name)
412 ret = verify_image_exists(image, image_name, wim_name);
414 ret = verify_image_is_single(image);
418 /* Parse the argument to --compress */
420 get_compression_type(const tchar *optarg)
422 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
423 return WIMLIB_COMPRESSION_TYPE_LZX;
424 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
425 return WIMLIB_COMPRESSION_TYPE_XPRESS;
426 else if (!tstrcasecmp(optarg, T("recovery")) || !tstrcasecmp(optarg, T("lzms")))
427 return WIMLIB_COMPRESSION_TYPE_LZMS;
428 else if (!tstrcasecmp(optarg, T("none")))
429 return WIMLIB_COMPRESSION_TYPE_NONE;
431 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
432 "\"maximum\", \"fast\", or \"none\"."), optarg);
433 return WIMLIB_COMPRESSION_TYPE_INVALID;
438 set_compress_slow(void)
441 static const struct wimlib_lzx_compressor_params lzx_slow_params = {
443 .size = sizeof(struct wimlib_lzx_compressor_params),
445 .algorithm = WIMLIB_LZX_ALGORITHM_SLOW,
448 .use_len2_matches = 1,
449 .nice_match_length = 96,
450 .num_optim_passes = 4,
451 .max_search_depth = 100,
452 .max_matches_per_pos = 10,
453 .main_nostat_cost = 15,
454 .len_nostat_cost = 15,
455 .aligned_nostat_cost = 7,
460 static const struct wimlib_lzms_compressor_params lzms_slow_params = {
462 .size = sizeof(struct wimlib_lzms_compressor_params),
464 .min_match_length = 2,
465 .max_match_length = UINT32_MAX,
466 .nice_match_length = 96,
467 .max_search_depth = 100,
468 .max_matches_per_pos = 10,
469 .optim_array_length = 1024,
472 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
473 &lzx_slow_params.hdr);
475 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZMS,
476 &lzms_slow_params.hdr);
480 const tchar **strings;
481 unsigned num_strings;
482 unsigned num_alloc_strings;
485 #define STRING_SET_INITIALIZER \
486 { .strings = NULL, .num_strings = 0, .num_alloc_strings = 0, }
488 #define STRING_SET(_strings) \
489 struct string_set _strings = STRING_SET_INITIALIZER
492 string_set_append(struct string_set *set, const tchar *glob)
494 unsigned num_alloc_strings = set->num_alloc_strings;
496 if (set->num_strings == num_alloc_strings) {
497 const tchar **new_strings;
499 num_alloc_strings += 4;
500 new_strings = realloc(set->strings,
501 sizeof(set->strings[0]) * num_alloc_strings);
503 imagex_error(T("Out of memory!"));
506 set->strings = new_strings;
507 set->num_alloc_strings = num_alloc_strings;
509 set->strings[set->num_strings++] = glob;
514 string_set_destroy(struct string_set *set)
520 wim_reference_globs(WIMStruct *wim, struct string_set *set, int open_flags)
522 return wimlib_reference_resource_files(wim, set->strings,
524 WIMLIB_REF_FLAG_GLOB_ENABLE,
526 imagex_progress_func);
530 do_resource_not_found_warning(const tchar *wimfile,
531 const struct wimlib_wim_info *info,
532 const struct string_set *refglobs)
534 if (info->total_parts > 1) {
535 if (refglobs->num_strings == 0) {
536 imagex_error(T("\"%"TS"\" is part of a split WIM. "
537 "Use --ref to specify the other parts."),
540 imagex_error(T("Perhaps the '--ref' argument did not "
541 "specify all other parts of the split "
545 imagex_error(T("If this is a delta WIM, use the --ref argument "
546 "to specify the WIM(s) on which it is based."));
550 /* Returns the size of a file given its name, or -1 if the file does not exist
551 * or its size cannot be determined. */
553 file_get_size(const tchar *filename)
556 if (tstat(filename, &st) == 0)
563 PARSE_STRING_SUCCESS = 0,
564 PARSE_STRING_FAILURE = 1,
565 PARSE_STRING_NONE = 2,
569 * Parses a string token from an array of characters.
571 * Tokens are either whitespace-delimited, or double or single-quoted.
573 * @line_p: Pointer to the pointer to the line of data. Will be updated
574 * to point past the string token iff the return value is
575 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
578 * @len_p: @len_p initially stores the length of the line of data, which may
579 * be 0, and it will be updated to the number of bytes remaining in
580 * the line iff the return value is PARSE_STRING_SUCCESS.
582 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
583 * parsed string token will be returned here.
585 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
586 * PARSE_STRING_FAILURE if the data was invalid due to a missing
587 * closing quote; or PARSE_STRING_NONE if the line ended before the
588 * beginning of a string token was found.
591 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
594 tchar *line = *line_p;
598 /* Skip leading whitespace */
601 return PARSE_STRING_NONE;
602 if (!istspace(*line) && *line != T('\0'))
608 if (quote_char == T('"') || quote_char == T('\'')) {
613 line = tmemchr(line, quote_char, len);
615 imagex_error(T("Missing closing quote: %"TS), fn - 1);
616 return PARSE_STRING_FAILURE;
619 /* Unquoted string. Go until whitespace. Line is terminated
620 * by '\0', so no need to check 'len'. */
624 } while (!istspace(*line) && *line != T('\0'));
631 return PARSE_STRING_SUCCESS;
634 /* Parses a line of data (not an empty line or comment) in the source list file
635 * format. (See the man page for 'wimlib-imagex capture' for details on this
636 * format and the meaning.)
638 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
639 * len == 0. The data in @line will be modified by this function call.
641 * @len: Length of the line of data.
643 * @source: On success, the capture source and target described by the line is
644 * written into this destination. Note that it will contain pointers
645 * to data in the @line array.
647 * Returns true if the line was valid; false otherwise. */
649 parse_source_list_line(tchar *line, size_t len,
650 struct wimlib_capture_source *source)
654 ret = parse_string(&line, &len, &source->fs_source_path);
655 if (ret != PARSE_STRING_SUCCESS)
657 ret = parse_string(&line, &len, &source->wim_target_path);
658 if (ret == PARSE_STRING_NONE)
659 source->wim_target_path = source->fs_source_path;
660 return ret != PARSE_STRING_FAILURE;
663 /* Returns %true if the given line of length @len > 0 is a comment or empty line
664 * in the source list file format. */
666 is_comment_line(const tchar *line, size_t len)
671 if (!istspace(*line) && *line != T('\0'))
681 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
684 tchar *contents = *contents_p;
685 size_t nchars = *nchars_p;
688 for (i = 0; i < nchars; i++)
689 if (contents[i] == T('\n'))
692 /* Handle last line not terminated by a newline */
693 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
694 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
696 imagex_error(T("Out of memory!"));
699 contents[nchars] = T('\n');
700 *contents_p = contents;
708 /* Parses a file in the source list format. (See the man page for
709 * 'wimlib-imagex capture' for details on this format and the meaning.)
711 * @source_list_contents: Contents of the source list file. Note that this
712 * buffer will be modified to save memory allocations,
713 * and cannot be freed until the returned array of
714 * wimlib_capture_source's has also been freed.
716 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
719 * @nsources_ret: On success, the length of the returned array is
722 * Returns: An array of `struct wimlib_capture_source's that can be passed to
723 * the wimlib_add_image_multisource() function to specify how a WIM image is to
725 static struct wimlib_capture_source *
726 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
727 size_t *nsources_ret)
731 struct wimlib_capture_source *sources;
734 nlines = text_file_count_lines(source_list_contents_p,
735 &source_list_nchars);
739 /* Always allocate at least 1 slot, just in case the implementation of
740 * calloc() returns NULL if 0 bytes are requested. */
741 sources = calloc(nlines ?: 1, sizeof(*sources));
743 imagex_error(T("out of memory"));
746 p = *source_list_contents_p;
748 for (i = 0; i < nlines; i++) {
749 /* XXX: Could use rawmemchr() here instead, but it may not be
750 * available on all platforms. */
751 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
752 size_t len = endp - p + 1;
754 if (!is_comment_line(p, len)) {
755 if (!parse_source_list_line(p, len, &sources[j++])) {
768 enum capture_config_section {
769 CAPTURE_CONFIG_NO_SECTION,
770 CAPTURE_CONFIG_EXCLUSION_SECTION,
771 CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
772 CAPTURE_CONFIG_IGNORE_SECTION,
776 CAPTURE_CONFIG_INVALID_SECTION,
777 CAPTURE_CONFIG_CHANGED_SECTION,
778 CAPTURE_CONFIG_SAME_SECTION,
782 check_config_section(tchar *line, size_t len,
783 enum capture_config_section *cur_section)
785 while (istspace(*line))
789 return CAPTURE_CONFIG_SAME_SECTION;
792 tchar *endbrace = tstrrchr(line, T(']'));
794 return CAPTURE_CONFIG_SAME_SECTION;
796 if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
797 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
798 } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
799 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
800 } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
801 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
802 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
803 "of capture config file\n"),
805 } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
806 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
807 tfputs(T("WARNING: Ignoring [AlignmentList] section "
808 "of capture config file\n"),
811 imagex_error(T("Invalid capture config file section \"%"TS"\""),
813 return CAPTURE_CONFIG_INVALID_SECTION;
815 return CAPTURE_CONFIG_CHANGED_SECTION;
820 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
823 if (pat_list->num_pats == pat_list->num_allocated_pats) {
825 size_t num_allocated_pats = pat_list->num_pats + 8;
827 pats = realloc(pat_list->pats,
828 num_allocated_pats * sizeof(pat_list->pats[0]));
830 imagex_error(T("Out of memory!"));
833 pat_list->pats = pats;
834 pat_list->num_allocated_pats = num_allocated_pats;
836 pat_list->pats[pat_list->num_pats++] = pat;
841 parse_capture_config_line(tchar *line, size_t len,
842 enum capture_config_section *cur_section,
843 struct wimlib_capture_config *config)
848 ret = check_config_section(line, len, cur_section);
849 if (ret == CAPTURE_CONFIG_INVALID_SECTION)
851 if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
854 switch (*cur_section) {
855 case CAPTURE_CONFIG_NO_SECTION:
856 imagex_error(T("Line \"%"TS"\" is not in a section "
857 "(such as [ExclusionList]"), line);
859 case CAPTURE_CONFIG_EXCLUSION_SECTION:
860 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
862 return pattern_list_add_pattern(&config->exclusion_pats,
864 case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
865 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
867 return pattern_list_add_pattern(&config->exclusion_exception_pats,
869 case CAPTURE_CONFIG_IGNORE_SECTION:
876 parse_capture_config(tchar **contents_p, size_t nchars,
877 struct wimlib_capture_config *config)
882 enum capture_config_section cur_section;
884 memset(config, 0, sizeof(*config));
886 nlines = text_file_count_lines(contents_p, &nchars);
890 cur_section = CAPTURE_CONFIG_NO_SECTION;
892 for (i = 0; i < nlines; i++) {
893 tchar *endp = tmemchr(p, T('\n'), nchars);
894 size_t len = endp - p + 1;
896 if (!is_comment_line(p, len))
897 if (!parse_capture_config_line(p, len, &cur_section, config))
905 /* Reads the contents of a file into memory. */
907 file_get_contents(const tchar *filename, size_t *len_ret)
914 if (tstat(filename, &stbuf) != 0) {
915 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
920 fp = tfopen(filename, T("rb"));
922 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
926 buf = malloc(len ? len : 1);
928 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
929 "contents of file \"%"TS"\""), len, filename);
932 if (fread(buf, 1, len, fp) != len) {
933 imagex_error_with_errno(T("Failed to read %zu bytes from the "
934 "file \"%"TS"\""), len, filename);
948 /* Read standard input until EOF and return the full contents in a malloc()ed
949 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
952 stdin_get_contents(size_t *len_ret)
954 /* stdin can, of course, be a pipe or other non-seekable file, so the
955 * total length of the data cannot be pre-determined */
957 size_t newlen = 1024;
961 char *p = realloc(buf, newlen);
962 size_t bytes_read, bytes_to_read;
964 imagex_error(T("out of memory while reading stdin"));
968 bytes_to_read = newlen - pos;
969 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
971 if (bytes_read != bytes_to_read) {
976 imagex_error_with_errno(T("error reading stdin"));
990 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
993 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
995 *num_tchars_ret = num_bytes;
997 #else /* !__WIN32__ */
998 /* On Windows, translate the text to UTF-16LE */
1002 if (num_bytes >= 2 &&
1003 (((unsigned char)text[0] == 0xff && (unsigned char)text[1] == 0xfe) ||
1004 ((unsigned char)text[0] <= 0x7f && (unsigned char)text[1] == 0x00)))
1006 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
1007 * with something that looks like an ASCII character encoded as
1008 * a UTF-16LE code unit. Assume the file is encoded as
1009 * UTF-16LE. This is not a 100% reliable check. */
1010 num_wchars = num_bytes / 2;
1011 text_wstr = (wchar_t*)text;
1013 /* File does not look like UTF-16LE. Assume it is encoded in
1014 * the current Windows code page. I think these are always
1015 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
1016 * should work as expected. */
1017 text_wstr = win32_mbs_to_wcs(text,
1022 *num_tchars_ret = num_wchars;
1024 #endif /* __WIN32__ */
1028 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
1033 contents = file_get_contents(filename, &num_bytes);
1036 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
1040 stdin_get_text_contents(size_t *num_tchars_ret)
1045 contents = stdin_get_contents(&num_bytes);
1048 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
1051 #define TO_PERCENT(numerator, denominator) \
1052 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
1054 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
1055 #define MEBIBYTE_MIN_NBYTES 10000000ULL
1056 #define KIBIBYTE_MIN_NBYTES 10000ULL
1059 get_unit(uint64_t total_bytes, const tchar **name_ret)
1061 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
1062 *name_ret = T("GiB");
1064 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
1065 *name_ret = T("MiB");
1067 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
1068 *name_ret = T("KiB");
1071 *name_ret = T("bytes");
1076 static struct wimlib_progress_info_scan last_scan_progress;
1079 report_scan_progress(const struct wimlib_progress_info_scan *scan, bool done)
1081 uint64_t prev_count, cur_count;
1083 prev_count = last_scan_progress.num_nondirs_scanned +
1084 last_scan_progress.num_dirs_scanned;
1085 cur_count = scan->num_nondirs_scanned + scan->num_dirs_scanned;
1087 if (done || prev_count == 0 || cur_count >= prev_count + 100 ||
1088 cur_count % 128 == 0)
1090 unsigned unit_shift;
1091 const tchar *unit_name;
1093 unit_shift = get_unit(scan->num_bytes_scanned, &unit_name);
1094 imagex_printf(T("\r%"PRIu64" %"TS" scanned (%"PRIu64" files, "
1095 "%"PRIu64" directories) "),
1096 scan->num_bytes_scanned >> unit_shift,
1098 scan->num_nondirs_scanned,
1099 scan->num_dirs_scanned);
1100 last_scan_progress = *scan;
1104 /* Progress callback function passed to various wimlib functions. */
1106 imagex_progress_func(enum wimlib_progress_msg msg,
1107 const union wimlib_progress_info *info)
1109 unsigned percent_done;
1110 unsigned unit_shift;
1111 const tchar *unit_name;
1113 if (imagex_be_quiet)
1116 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1118 static bool first = true;
1120 imagex_printf(T("Writing %"TS"-compressed data "
1121 "using %u thread%"TS"\n"),
1122 wimlib_get_compression_type_string(
1123 info->write_streams.compression_type),
1124 info->write_streams.num_threads,
1125 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1129 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1130 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1131 info->write_streams.total_bytes);
1133 if (info->write_streams.total_parts <= 1) {
1134 imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1135 "written (%u%% done)"),
1136 info->write_streams.completed_bytes >> unit_shift,
1138 info->write_streams.total_bytes >> unit_shift,
1142 imagex_printf(T("\rWriting resources from part %u of %u: "
1143 "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written"),
1144 (info->write_streams.completed_parts ==
1145 info->write_streams.total_parts) ?
1146 info->write_streams.completed_parts :
1147 info->write_streams.completed_parts + 1,
1148 info->write_streams.total_parts,
1149 info->write_streams.completed_bytes >> unit_shift,
1151 info->write_streams.total_bytes >> unit_shift,
1155 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1156 imagex_printf(T("\n"));
1158 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1159 imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
1160 if (*info->scan.wim_target_path) {
1161 imagex_printf(T(" (loading as WIM path: "
1162 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
1163 info->scan.wim_target_path);
1165 imagex_printf(T("\n"));
1167 memset(&last_scan_progress, 0, sizeof(last_scan_progress));
1169 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1170 switch (info->scan.status) {
1171 case WIMLIB_SCAN_DENTRY_OK:
1172 report_scan_progress(&info->scan, false);
1174 case WIMLIB_SCAN_DENTRY_EXCLUDED:
1175 imagex_printf(T("\nExcluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1177 case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
1178 imagex_printf(T("\nWARNING: Excluding unsupported file or directory\n"
1179 " \"%"TS"\" from capture\n"), info->scan.cur_path);
1181 case WIMLIB_SCAN_DENTRY_EXCLUDED_SYMLINK:
1182 imagex_printf(T("\nWARNING: Ignoring absolute symbolic link "
1183 "with out-of-tree target:\n"
1184 " \"%"TS"\" => \"%"TS"\"\n"
1185 " (Use --norpfix to capture "
1186 "absolute symbolic links as-is)\n"),
1187 info->scan.cur_path, info->scan.symlink_target);
1191 case WIMLIB_PROGRESS_MSG_SCAN_END:
1192 report_scan_progress(&info->scan, true);
1193 imagex_printf(T("\n"));
1195 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1196 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1197 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1198 info->integrity.total_bytes);
1199 imagex_printf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1200 "of %"PRIu64" %"TS" (%u%%) done"),
1201 info->integrity.filename,
1202 info->integrity.completed_bytes >> unit_shift,
1204 info->integrity.total_bytes >> unit_shift,
1207 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1208 imagex_printf(T("\n"));
1210 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1211 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1212 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1213 info->integrity.total_bytes);
1214 imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1215 "of %"PRIu64" %"TS" (%u%%) done"),
1216 info->integrity.completed_bytes >> unit_shift,
1218 info->integrity.total_bytes >> unit_shift,
1221 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1222 imagex_printf(T("\n"));
1224 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1225 imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1226 "to %"TS" \"%"TS"\"\n"),
1227 info->extract.image,
1228 info->extract.image_name,
1229 info->extract.wimfile_name,
1230 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1231 T("NTFS volume") : T("directory")),
1232 info->extract.target);
1234 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1235 if (info->extract.extract_root_wim_source_path[0] != T('\0')) {
1236 imagex_printf(T("Extracting "
1237 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\" "
1238 "from image %d (\"%"TS"\") "
1239 "in \"%"TS"\" to \"%"TS"\"\n"),
1240 info->extract.extract_root_wim_source_path,
1241 info->extract.image,
1242 info->extract.image_name,
1243 info->extract.wimfile_name,
1244 info->extract.target);
1247 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1248 percent_done = TO_PERCENT(info->extract.completed_bytes,
1249 info->extract.total_bytes);
1250 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1251 imagex_printf(T("\rExtracting files: "
1252 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1253 info->extract.completed_bytes >> unit_shift,
1255 info->extract.total_bytes >> unit_shift,
1258 if (info->extract.completed_bytes >= info->extract.total_bytes)
1259 imagex_printf(T("\n"));
1261 case WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN:
1262 if (info->extract.total_parts != 1) {
1263 imagex_printf(T("\nReading split pipable WIM part %u of %u\n"),
1264 info->extract.part_number,
1265 info->extract.total_parts);
1268 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1269 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1270 imagex_printf(T("Setting timestamps on all extracted files...\n"));
1272 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1273 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1274 imagex_printf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1275 info->extract.target);
1278 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1279 percent_done = TO_PERCENT(info->split.completed_bytes,
1280 info->split.total_bytes);
1281 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1282 imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
1283 "%"PRIu64" %"TS" (%u%%) written\n"),
1284 info->split.part_name,
1285 info->split.cur_part_number,
1286 info->split.total_parts,
1287 info->split.completed_bytes >> unit_shift,
1289 info->split.total_bytes >> unit_shift,
1293 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1294 if (info->split.completed_bytes == info->split.total_bytes) {
1295 imagex_printf(T("Finished writing split WIM part %u of %u\n"),
1296 info->split.cur_part_number,
1297 info->split.total_parts);
1300 case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1301 switch (info->update.command->op) {
1302 case WIMLIB_UPDATE_OP_DELETE:
1303 imagex_printf(T("Deleted WIM path "
1304 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1305 info->update.command->delete_.wim_path);
1307 case WIMLIB_UPDATE_OP_RENAME:
1308 imagex_printf(T("Renamed WIM path "
1309 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
1310 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1311 info->update.command->rename.wim_source_path,
1312 info->update.command->rename.wim_target_path);
1314 case WIMLIB_UPDATE_OP_ADD:
1322 fflush(imagex_info_file);
1327 parse_num_threads(const tchar *optarg)
1330 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1331 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1332 imagex_error(T("Number of threads must be a non-negative integer!"));
1339 static uint32_t parse_chunk_size(const tchar *optarg)
1342 unsigned long chunk_size = tstrtoul(optarg, &tmp, 10);
1343 if (chunk_size >= UINT32_MAX || *tmp || tmp == optarg) {
1344 imagex_error(T("Chunk size must be a non-negative integer!"));
1353 * Parse an option passed to an update command.
1355 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1358 * @option: Text string for the option (beginning with --)
1360 * @cmd: `struct wimlib_update_command' that is being constructed for
1363 * Returns true if the option was recognized; false if not.
1366 update_command_add_option(int op, const tchar *option,
1367 struct wimlib_update_command *cmd)
1369 bool recognized = true;
1371 case WIMLIB_UPDATE_OP_ADD:
1372 if (!tstrcmp(option, T("--verbose")))
1373 cmd->add.add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
1374 else if (!tstrcmp(option, T("--unix-data")))
1375 cmd->add.add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
1376 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1377 cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
1378 else if (!tstrcmp(option, T("--strict-acls")))
1379 cmd->add.add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
1380 else if (!tstrcmp(option, T("--dereference")))
1381 cmd->add.add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
1385 case WIMLIB_UPDATE_OP_DELETE:
1386 if (!tstrcmp(option, T("--force")))
1387 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1388 else if (!tstrcmp(option, T("--recursive")))
1389 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1400 /* How many nonoption arguments each `imagex update' command expects */
1401 static const unsigned update_command_num_nonoptions[] = {
1402 [WIMLIB_UPDATE_OP_ADD] = 2,
1403 [WIMLIB_UPDATE_OP_DELETE] = 1,
1404 [WIMLIB_UPDATE_OP_RENAME] = 2,
1408 update_command_add_nonoption(int op, const tchar *nonoption,
1409 struct wimlib_update_command *cmd,
1410 unsigned num_nonoptions)
1413 case WIMLIB_UPDATE_OP_ADD:
1414 if (num_nonoptions == 0)
1415 cmd->add.fs_source_path = (tchar*)nonoption;
1417 cmd->add.wim_target_path = (tchar*)nonoption;
1419 case WIMLIB_UPDATE_OP_DELETE:
1420 cmd->delete_.wim_path = (tchar*)nonoption;
1422 case WIMLIB_UPDATE_OP_RENAME:
1423 if (num_nonoptions == 0)
1424 cmd->rename.wim_source_path = (tchar*)nonoption;
1426 cmd->rename.wim_target_path = (tchar*)nonoption;
1432 * Parse a command passed on stdin to `imagex update'.
1434 * @line: Text of the command.
1435 * @len: Length of the line, including a null terminator
1438 * @command: A `struct wimlib_update_command' to fill in from the parsed
1441 * @line_number: Line number of the command, for diagnostics.
1443 * Returns true on success; returns false on parse error.
1446 parse_update_command(tchar *line, size_t len,
1447 struct wimlib_update_command *command,
1451 tchar *command_name;
1453 size_t num_nonoptions;
1455 /* Get the command name ("add", "delete", "rename") */
1456 ret = parse_string(&line, &len, &command_name);
1457 if (ret != PARSE_STRING_SUCCESS)
1460 if (!tstrcasecmp(command_name, T("add"))) {
1461 op = WIMLIB_UPDATE_OP_ADD;
1462 } else if (!tstrcasecmp(command_name, T("delete"))) {
1463 op = WIMLIB_UPDATE_OP_DELETE;
1464 } else if (!tstrcasecmp(command_name, T("rename"))) {
1465 op = WIMLIB_UPDATE_OP_RENAME;
1467 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1468 command_name, line_number);
1473 /* Parse additional options and non-options as needed */
1478 ret = parse_string(&line, &len, &next_string);
1479 if (ret == PARSE_STRING_NONE) /* End of line */
1481 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1483 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1485 if (!update_command_add_option(op, next_string, command))
1487 imagex_error(T("Unrecognized option \"%"TS"\" to "
1488 "update command \"%"TS"\" on line %zu"),
1489 next_string, command_name, line_number);
1495 if (num_nonoptions == update_command_num_nonoptions[op])
1497 imagex_error(T("Unexpected argument \"%"TS"\" in "
1498 "update command on line %zu\n"
1499 " (The \"%"TS"\" command only "
1500 "takes %zu nonoption arguments!)\n"),
1501 next_string, line_number,
1502 command_name, num_nonoptions);
1505 update_command_add_nonoption(op, next_string,
1506 command, num_nonoptions);
1511 if (num_nonoptions != update_command_num_nonoptions[op]) {
1512 imagex_error(T("Not enough arguments to update command "
1513 "\"%"TS"\" on line %zu"), command_name, line_number);
1519 static struct wimlib_update_command *
1520 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1521 size_t *num_cmds_ret)
1525 struct wimlib_update_command *cmds;
1528 nlines = text_file_count_lines(cmd_file_contents_p,
1533 /* Always allocate at least 1 slot, just in case the implementation of
1534 * calloc() returns NULL if 0 bytes are requested. */
1535 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1537 imagex_error(T("out of memory"));
1540 p = *cmd_file_contents_p;
1542 for (i = 0; i < nlines; i++) {
1543 /* XXX: Could use rawmemchr() here instead, but it may not be
1544 * available on all platforms. */
1545 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1546 size_t len = endp - p + 1;
1548 if (!is_comment_line(p, len)) {
1549 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1560 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1561 * one image from a WIM file to a NTFS volume. */
1563 imagex_apply(int argc, tchar **argv, int cmd)
1567 int image = WIMLIB_NO_IMAGE;
1569 struct wimlib_wim_info info;
1571 const tchar *wimfile;
1572 const tchar *target;
1573 const tchar *image_num_or_name = NULL;
1574 int extract_flags = 0;
1576 STRING_SET(refglobs);
1578 for_opt(c, apply_options) {
1580 case IMAGEX_CHECK_OPTION:
1581 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1583 case IMAGEX_HARDLINK_OPTION:
1584 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1586 case IMAGEX_SYMLINK_OPTION:
1587 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1589 case IMAGEX_VERBOSE_OPTION:
1590 /* No longer does anything. */
1592 case IMAGEX_REF_OPTION:
1593 ret = string_set_append(&refglobs, optarg);
1595 goto out_free_refglobs;
1597 case IMAGEX_UNIX_DATA_OPTION:
1598 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1600 case IMAGEX_NO_ACLS_OPTION:
1601 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1603 case IMAGEX_STRICT_ACLS_OPTION:
1604 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1606 case IMAGEX_NORPFIX_OPTION:
1607 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1609 case IMAGEX_RPFIX_OPTION:
1610 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1612 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1613 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1614 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1616 case IMAGEX_RESUME_OPTION:
1617 extract_flags |= WIMLIB_EXTRACT_FLAG_RESUME;
1625 if (argc != 2 && argc != 3)
1630 if (!tstrcmp(wimfile, T("-"))) {
1631 /* Attempt to apply pipable WIM from standard input. */
1633 image_num_or_name = NULL;
1636 image_num_or_name = argv[1];
1641 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1642 imagex_progress_func);
1644 goto out_free_refglobs;
1646 wimlib_get_wim_info(wim, &info);
1649 /* Image explicitly specified. */
1650 image_num_or_name = argv[1];
1651 image = wimlib_resolve_image(wim, image_num_or_name);
1652 ret = verify_image_exists(image, image_num_or_name, wimfile);
1654 goto out_wimlib_free;
1657 /* No image specified; default to image 1, but only if the WIM
1658 * contains exactly one image. */
1660 if (info.image_count != 1) {
1661 imagex_error(T("\"%"TS"\" contains %d images; "
1662 "Please select one (or all)."),
1663 wimfile, info.image_count);
1672 if (refglobs.num_strings) {
1674 imagex_error(T("Can't specify --ref when applying from stdin!"));
1676 goto out_wimlib_free;
1678 ret = wim_reference_globs(wim, &refglobs, open_flags);
1680 goto out_wimlib_free;
1685 /* Interpret a regular file or block device target as a NTFS
1689 if (tstat(target, &stbuf)) {
1690 if (errno != ENOENT) {
1691 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1694 goto out_wimlib_free;
1697 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1698 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1704 ret = wimlib_extract_image(wim, image, target, extract_flags,
1705 imagex_progress_func);
1707 set_fd_to_binary_mode(STDIN_FILENO);
1708 ret = wimlib_extract_image_from_pipe(STDIN_FILENO,
1710 target, extract_flags,
1711 imagex_progress_func);
1714 imagex_printf(T("Done applying WIM image.\n"));
1715 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
1717 do_resource_not_found_warning(wimfile, &info, &refglobs);
1719 imagex_error(T( "If you are applying an image "
1720 "from a split pipable WIM,\n"
1721 " make sure you have "
1722 "concatenated together all parts."));
1728 string_set_destroy(&refglobs);
1732 usage(CMD_APPLY, stderr);
1734 goto out_free_refglobs;
1737 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1738 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1739 * the desired image. 'wimlib-imagex append': add a new image to an existing
1742 imagex_capture_or_append(int argc, tchar **argv, int cmd)
1745 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1746 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE |
1747 WIMLIB_ADD_IMAGE_FLAG_WINCONFIG |
1748 WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1749 int write_flags = 0;
1750 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
1751 uint32_t chunk_size = UINT32_MAX;
1752 const tchar *wimfile;
1756 const tchar *flags_element = NULL;
1759 STRING_SET(base_wimfiles);
1760 WIMStruct **base_wims;
1762 WIMStruct *template_wim;
1763 const tchar *template_wimfile = NULL;
1764 const tchar *template_image_name_or_num = NULL;
1765 int template_image = WIMLIB_NO_IMAGE;
1768 unsigned num_threads = 0;
1773 const tchar *config_file = NULL;
1775 struct wimlib_capture_config *config;
1777 bool source_list = false;
1778 size_t source_list_nchars = 0;
1779 tchar *source_list_contents;
1780 bool capture_sources_malloced;
1781 struct wimlib_capture_source *capture_sources;
1783 bool name_defaulted;
1784 bool compress_slow = false;
1786 for_opt(c, capture_or_append_options) {
1788 case IMAGEX_BOOT_OPTION:
1789 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1791 case IMAGEX_CHECK_OPTION:
1792 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1793 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1795 case IMAGEX_NOCHECK_OPTION:
1796 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
1798 case IMAGEX_CONFIG_OPTION:
1799 config_file = optarg;
1800 add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1802 case IMAGEX_COMPRESS_OPTION:
1803 compression_type = get_compression_type(optarg);
1804 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1807 case IMAGEX_COMPRESS_SLOW_OPTION:
1808 compress_slow = true;
1810 case IMAGEX_CHUNK_SIZE_OPTION:
1811 chunk_size = parse_chunk_size(optarg);
1812 if (chunk_size == UINT32_MAX)
1815 case IMAGEX_PACK_STREAMS_OPTION:
1816 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
1818 case IMAGEX_FLAGS_OPTION:
1819 flags_element = optarg;
1821 case IMAGEX_DEREFERENCE_OPTION:
1822 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1824 case IMAGEX_VERBOSE_OPTION:
1825 /* No longer does anything. */
1827 case IMAGEX_THREADS_OPTION:
1828 num_threads = parse_num_threads(optarg);
1829 if (num_threads == UINT_MAX)
1832 case IMAGEX_REBUILD_OPTION:
1833 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1835 case IMAGEX_UNIX_DATA_OPTION:
1836 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1838 case IMAGEX_SOURCE_LIST_OPTION:
1841 case IMAGEX_NO_ACLS_OPTION:
1842 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1844 case IMAGEX_STRICT_ACLS_OPTION:
1845 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1847 case IMAGEX_RPFIX_OPTION:
1848 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1850 case IMAGEX_NORPFIX_OPTION:
1851 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1853 case IMAGEX_PIPABLE_OPTION:
1854 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1856 case IMAGEX_NOT_PIPABLE_OPTION:
1857 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
1859 case IMAGEX_UPDATE_OF_OPTION:
1860 if (template_image_name_or_num) {
1861 imagex_error(T("'--update-of' can only be "
1862 "specified one time!"));
1866 colon = tstrrchr(optarg, T(':'));
1869 template_wimfile = optarg;
1871 template_image_name_or_num = colon + 1;
1873 template_wimfile = NULL;
1874 template_image_name_or_num = optarg;
1878 case IMAGEX_DELTA_FROM_OPTION:
1879 if (cmd != CMD_CAPTURE) {
1880 imagex_error(T("'--delta-from' is only "
1881 "valid for capture!"));
1884 ret = string_set_append(&base_wimfiles, optarg);
1886 goto out_free_base_wimfiles;
1887 write_flags |= WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS;
1896 if (argc < 2 || argc > 4)
1902 /* Set default compression type and parameters. */
1905 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
1906 compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1908 if (!compress_slow) {
1909 struct wimlib_lzx_compressor_params params = {
1910 .hdr.size = sizeof(params),
1911 .algorithm = WIMLIB_LZX_ALGORITHM_FAST,
1914 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
1920 set_compress_slow();
1922 if (!tstrcmp(wimfile, T("-"))) {
1923 /* Writing captured WIM to standard output. */
1925 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
1926 imagex_error("Can't write a non-pipable WIM to "
1927 "standard output! Specify --pipable\n"
1928 " if you want to create a pipable WIM "
1929 "(but read the docs first).");
1933 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1935 if (cmd == CMD_APPEND) {
1936 imagex_error(T("Using standard output for append does "
1937 "not make sense."));
1940 wim_fd = STDOUT_FILENO;
1942 imagex_info_file = stderr;
1943 set_fd_to_binary_mode(wim_fd);
1946 /* If template image was specified using --update-of=IMAGE rather
1947 * than --update-of=WIMFILE:IMAGE, set the default WIMFILE. */
1948 if (template_image_name_or_num && !template_wimfile) {
1949 if (base_wimfiles.num_strings == 1) {
1950 /* Capturing delta WIM based on single WIM: default to
1952 template_wimfile = base_wimfiles.strings[0];
1953 } else if (cmd == CMD_APPEND) {
1954 /* Appending to WIM: default to WIM being appended to.
1956 template_wimfile = wimfile;
1958 /* Capturing a normal (non-delta) WIM, so the WIM file
1959 * *must* be explicitly specified. */
1960 if (base_wimfiles.num_strings > 1) {
1961 imagex_error(T("For capture of delta WIM "
1962 "based on multiple existing "
1964 " '--update-of' must "
1965 "specify WIMFILE:IMAGE!"));
1967 imagex_error(T("For capture of non-delta WIM, "
1968 "'--update-of' must specify "
1977 name_defaulted = false;
1979 /* Set default name to SOURCE argument, omitting any directory
1980 * prefixes and trailing slashes. This requires making a copy
1981 * of @source. Leave some free characters at the end in case we
1982 * append a number to keep the name unique. */
1983 size_t source_name_len;
1985 source_name_len = tstrlen(source);
1986 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1987 name = tbasename(tstrcpy(source_copy, source));
1988 name_defaulted = true;
1990 /* Image description defaults to NULL if not given. */
1997 /* Set up capture sources in source list mode */
1998 if (source[0] == T('-') && source[1] == T('\0')) {
1999 source_list_contents = stdin_get_text_contents(&source_list_nchars);
2001 source_list_contents = file_get_text_contents(source,
2002 &source_list_nchars);
2004 if (!source_list_contents)
2007 capture_sources = parse_source_list(&source_list_contents,
2010 if (!capture_sources) {
2012 goto out_free_source_list_contents;
2014 capture_sources_malloced = true;
2016 /* Set up capture source in non-source-list mode. */
2017 capture_sources = alloca(sizeof(struct wimlib_capture_source));
2018 capture_sources[0].fs_source_path = source;
2019 capture_sources[0].wim_target_path = NULL;
2020 capture_sources[0].reserved = 0;
2022 capture_sources_malloced = false;
2023 source_list_contents = NULL;
2027 /* Read and parse capture configuration file. */
2030 config_str = file_get_text_contents(config_file, &config_len);
2033 goto out_free_capture_sources;
2036 config = alloca(sizeof(*config));
2037 ret = parse_capture_config(&config_str, config_len, config);
2039 goto out_free_config;
2041 /* No capture configuration file specified; use default
2042 * configuration for capturing Windows operating systems. */
2044 add_image_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
2047 /* Open the existing WIM, or create a new one. */
2048 if (cmd == CMD_APPEND)
2049 ret = wimlib_open_wim(wimfile, open_flags, &wim,
2050 imagex_progress_func);
2052 ret = wimlib_create_new_wim(compression_type, &wim);
2054 goto out_free_config;
2056 /* Set chunk size if non-default. */
2057 if (chunk_size != UINT32_MAX) {
2058 ret = wimlib_set_output_chunk_size(wim, chunk_size);
2064 /* Detect if source is regular file or block device and set NTFS volume
2069 if (tstat(source, &stbuf) == 0) {
2070 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
2071 imagex_printf(T("Capturing WIM image from NTFS "
2072 "filesystem on \"%"TS"\"\n"), source);
2073 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
2076 if (errno != ENOENT) {
2077 imagex_error_with_errno(T("Failed to stat "
2078 "\"%"TS"\""), source);
2086 /* If the user did not specify an image name, and the basename of the
2087 * source already exists as an image name in the WIM file, append a
2088 * suffix to make it unique. */
2089 if (cmd == CMD_APPEND && name_defaulted) {
2090 unsigned long conflict_idx;
2091 tchar *name_end = tstrchr(name, T('\0'));
2092 for (conflict_idx = 1;
2093 wimlib_image_name_in_use(wim, name);
2096 tsprintf(name_end, T(" (%lu)"), conflict_idx);
2100 /* If capturing a delta WIM, reference resources from the base WIMs
2101 * before adding the new image. */
2102 if (base_wimfiles.num_strings) {
2103 base_wims = calloc(base_wimfiles.num_strings,
2104 sizeof(base_wims[0]));
2105 if (base_wims == NULL) {
2106 imagex_error(T("Out of memory!"));
2111 for (size_t i = 0; i < base_wimfiles.num_strings; i++) {
2112 ret = wimlib_open_wim(base_wimfiles.strings[i],
2113 open_flags, &base_wims[i],
2114 imagex_progress_func);
2116 goto out_free_base_wims;
2120 ret = wimlib_reference_resources(wim, base_wims,
2121 base_wimfiles.num_strings, 0);
2123 goto out_free_base_wims;
2125 if (base_wimfiles.num_strings == 1) {
2126 imagex_printf(T("Capturing delta WIM based on \"%"TS"\"\n"),
2127 base_wimfiles.strings[0]);
2129 imagex_printf(T("Capturing delta WIM based on %u WIMs\n"),
2130 base_wimfiles.num_strings);
2137 /* If capturing or appending as an update of an existing (template) image,
2138 * open the WIM if needed and parse the image index. */
2139 if (template_image_name_or_num) {
2142 if (base_wimfiles.num_strings == 1 &&
2143 template_wimfile == base_wimfiles.strings[0]) {
2144 template_wim = base_wims[0];
2145 } else if (template_wimfile == wimfile) {
2148 ret = wimlib_open_wim(template_wimfile, open_flags,
2149 &template_wim, imagex_progress_func);
2151 goto out_free_base_wims;
2154 template_image = wimlib_resolve_image(template_wim,
2155 template_image_name_or_num);
2157 if (template_image_name_or_num[0] == T('-')) {
2160 struct wimlib_wim_info info;
2162 wimlib_get_wim_info(template_wim, &info);
2163 n = tstrtoul(template_image_name_or_num + 1, &tmp, 10);
2164 if (n >= 1 && n <= info.image_count &&
2166 tmp != template_image_name_or_num + 1)
2168 template_image = info.image_count - (n - 1);
2171 ret = verify_image_exists_and_is_single(template_image,
2172 template_image_name_or_num,
2175 goto out_free_template_wim;
2177 template_wim = NULL;
2180 ret = wimlib_add_image_multisource(wim,
2186 imagex_progress_func);
2188 goto out_free_template_wim;
2190 if (desc || flags_element || template_image_name_or_num) {
2191 /* User provided <DESCRIPTION> or <FLAGS> element, or an image
2192 * on which the added one is to be based has been specified with
2193 * --update-of. Get the index of the image we just
2194 * added, then use it to call the appropriate functions. */
2195 struct wimlib_wim_info info;
2197 wimlib_get_wim_info(wim, &info);
2200 ret = wimlib_set_image_descripton(wim,
2204 goto out_free_template_wim;
2207 if (flags_element) {
2208 ret = wimlib_set_image_flags(wim, info.image_count,
2211 goto out_free_template_wim;
2214 /* Reference template image if the user provided one. */
2215 if (template_image_name_or_num) {
2216 imagex_printf(T("Using image %d "
2217 "from \"%"TS"\" as template\n"),
2218 template_image, template_wimfile);
2219 ret = wimlib_reference_template_image(wim,
2225 goto out_free_template_wim;
2229 /* Write the new WIM or overwrite the existing WIM with the new image
2231 if (cmd == CMD_APPEND) {
2232 ret = wimlib_overwrite(wim, write_flags, num_threads,
2233 imagex_progress_func);
2234 } else if (wimfile) {
2235 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
2236 write_flags, num_threads,
2237 imagex_progress_func);
2239 ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
2240 write_flags, num_threads,
2241 imagex_progress_func);
2243 out_free_template_wim:
2244 /* template_wim may alias base_wims[0] or wim. */
2245 if ((base_wimfiles.num_strings != 1 || template_wim != base_wims[0]) &&
2246 template_wim != wim)
2247 wimlib_free(template_wim);
2249 for (size_t i = 0; i < base_wimfiles.num_strings; i++)
2250 wimlib_free(base_wims[i]);
2256 free(config->exclusion_pats.pats);
2257 free(config->exclusion_exception_pats.pats);
2260 out_free_capture_sources:
2261 if (capture_sources_malloced)
2262 free(capture_sources);
2263 out_free_source_list_contents:
2264 free(source_list_contents);
2265 out_free_base_wimfiles:
2266 string_set_destroy(&base_wimfiles);
2273 goto out_free_base_wimfiles;
2276 /* Remove image(s) from a WIM. */
2278 imagex_delete(int argc, tchar **argv, int cmd)
2281 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2282 int write_flags = 0;
2283 const tchar *wimfile;
2284 const tchar *image_num_or_name;
2289 for_opt(c, delete_options) {
2291 case IMAGEX_CHECK_OPTION:
2292 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2293 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2295 case IMAGEX_SOFT_OPTION:
2296 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
2307 imagex_error(T("Must specify a WIM file"));
2309 imagex_error(T("Must specify an image"));
2313 image_num_or_name = argv[1];
2315 ret = wimlib_open_wim(wimfile, open_flags, &wim,
2316 imagex_progress_func);
2320 image = wimlib_resolve_image(wim, image_num_or_name);
2322 ret = verify_image_exists(image, image_num_or_name, wimfile);
2324 goto out_wimlib_free;
2326 ret = wimlib_delete_image(wim, image);
2328 imagex_error(T("Failed to delete image from \"%"TS"\""),
2330 goto out_wimlib_free;
2333 ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
2335 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2336 "deleted"), wimfile);
2344 usage(CMD_DELETE, stderr);
2350 print_full_path(const struct wimlib_dir_entry *wdentry, void *_ignore)
2352 int ret = tprintf(T("%"TS"\n"), wdentry->full_path);
2353 return (ret >= 0) ? 0 : -1;
2356 /* Print the files contained in an image(s) in a WIM file. */
2358 imagex_dir(int argc, tchar **argv, int cmd)
2360 const tchar *wimfile;
2361 WIMStruct *wim = NULL;
2364 const tchar *path = T("");
2367 for_opt(c, dir_options) {
2369 case IMAGEX_PATH_OPTION:
2380 imagex_error(T("Must specify a WIM file"));
2384 imagex_error(T("Too many arguments"));
2389 ret = wimlib_open_wim(wimfile, 0, &wim, imagex_progress_func);
2394 image = wimlib_resolve_image(wim, argv[1]);
2395 ret = verify_image_exists(image, argv[1], wimfile);
2397 goto out_wimlib_free;
2399 /* No image specified; default to image 1, but only if the WIM
2400 * contains exactly one image. */
2402 struct wimlib_wim_info info;
2404 wimlib_get_wim_info(wim, &info);
2405 if (info.image_count != 1) {
2406 imagex_error(T("\"%"TS"\" contains %d images; Please "
2407 "select one (or all)."),
2408 wimfile, info.image_count);
2415 ret = wimlib_iterate_dir_tree(wim, image, path,
2416 WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE,
2417 print_full_path, NULL);
2424 usage(CMD_DIR, stderr);
2429 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2432 imagex_export(int argc, tchar **argv, int cmd)
2436 int export_flags = 0;
2437 int write_flags = 0;
2438 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2439 const tchar *src_wimfile;
2440 const tchar *src_image_num_or_name;
2441 const tchar *dest_wimfile;
2443 const tchar *dest_name;
2444 const tchar *dest_desc;
2446 struct wimlib_wim_info src_info;
2447 WIMStruct *dest_wim;
2452 STRING_SET(refglobs);
2453 unsigned num_threads = 0;
2454 uint32_t chunk_size = UINT32_MAX;
2456 for_opt(c, export_options) {
2458 case IMAGEX_BOOT_OPTION:
2459 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2461 case IMAGEX_CHECK_OPTION:
2462 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2463 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2465 case IMAGEX_NOCHECK_OPTION:
2466 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2468 case IMAGEX_COMPRESS_OPTION:
2469 compression_type = get_compression_type(optarg);
2470 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2473 case IMAGEX_PACK_STREAMS_OPTION:
2474 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
2476 case IMAGEX_CHUNK_SIZE_OPTION:
2477 chunk_size = parse_chunk_size(optarg);
2478 if (chunk_size == UINT32_MAX)
2481 case IMAGEX_REF_OPTION:
2482 ret = string_set_append(&refglobs, optarg);
2484 goto out_free_refglobs;
2486 case IMAGEX_THREADS_OPTION:
2487 num_threads = parse_num_threads(optarg);
2488 if (num_threads == UINT_MAX)
2491 case IMAGEX_REBUILD_OPTION:
2492 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2494 case IMAGEX_PIPABLE_OPTION:
2495 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2497 case IMAGEX_NOT_PIPABLE_OPTION:
2498 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
2506 if (argc < 3 || argc > 5)
2508 src_wimfile = argv[0];
2509 src_image_num_or_name = argv[1];
2510 dest_wimfile = argv[2];
2511 dest_name = (argc >= 4) ? argv[3] : NULL;
2512 dest_desc = (argc >= 5) ? argv[4] : NULL;
2513 ret = wimlib_open_wim(src_wimfile, open_flags, &src_wim,
2514 imagex_progress_func);
2516 goto out_free_refglobs;
2518 wimlib_get_wim_info(src_wim, &src_info);
2520 /* Determine if the destination is an existing file or not. If so, we
2521 * try to append the exported image(s) to it; otherwise, we create a new
2522 * WIM containing the exported image(s). Furthermore, determine if we
2523 * need to write a pipable WIM directly to standard output. */
2525 if (tstrcmp(dest_wimfile, T("-")) == 0) {
2527 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2528 imagex_error("Can't write a non-pipable WIM to "
2529 "standard output! Specify --pipable\n"
2530 " if you want to create a pipable WIM "
2531 "(but read the docs first).");
2533 goto out_free_src_wim;
2536 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2538 dest_wimfile = NULL;
2539 dest_wim_fd = STDOUT_FILENO;
2540 imagex_info_file = stderr;
2541 set_fd_to_binary_mode(dest_wim_fd);
2544 if (dest_wimfile != NULL && tstat(dest_wimfile, &stbuf) == 0) {
2546 /* Destination file exists. */
2548 if (!S_ISREG(stbuf.st_mode)) {
2549 imagex_error(T("\"%"TS"\" is not a regular file"),
2552 goto out_free_src_wim;
2554 ret = wimlib_open_wim(dest_wimfile,
2555 open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2556 &dest_wim, imagex_progress_func);
2558 goto out_free_src_wim;
2560 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2561 /* The user specified a compression type, but we're
2562 * exporting to an existing WIM. Make sure the
2563 * specified compression type is the same as the
2564 * compression type of the existing destination WIM. */
2565 struct wimlib_wim_info dest_info;
2567 wimlib_get_wim_info(dest_wim, &dest_info);
2568 if (compression_type != dest_info.compression_type) {
2569 imagex_error(T("Cannot specify a compression type that is "
2570 "not the same as that used in the "
2571 "destination WIM"));
2573 goto out_free_dest_wim;
2579 if (errno != ENOENT) {
2580 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2583 goto out_free_src_wim;
2586 /* dest_wimfile is not an existing file, so create a new WIM. */
2588 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2589 /* The user did not specify a compression type; default
2590 * to that of the source WIM. */
2592 compression_type = src_info.compression_type;
2594 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2596 goto out_free_src_wim;
2598 /* Use same chunk size if compression type is the same. */
2599 if (compression_type == src_info.compression_type &&
2600 chunk_size == UINT32_MAX)
2601 wimlib_set_output_chunk_size(dest_wim, src_info.chunk_size);
2604 if (chunk_size != UINT32_MAX) {
2605 /* Set destination chunk size. */
2606 ret = wimlib_set_output_chunk_size(dest_wim, chunk_size);
2608 goto out_free_dest_wim;
2611 image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2612 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2614 goto out_free_dest_wim;
2616 if (refglobs.num_strings) {
2617 ret = wim_reference_globs(src_wim, &refglobs, open_flags);
2619 goto out_free_dest_wim;
2622 if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
2623 image == WIMLIB_ALL_IMAGES && src_info.boot_index == 0)
2625 imagex_error(T("--boot specified for all-images export, but source WIM "
2626 "has no bootable image."));
2628 goto out_free_dest_wim;
2631 ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2632 dest_desc, export_flags, imagex_progress_func);
2634 if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2635 do_resource_not_found_warning(src_wimfile,
2636 &src_info, &refglobs);
2638 goto out_free_dest_wim;
2642 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2643 imagex_progress_func);
2644 else if (dest_wimfile)
2645 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2646 write_flags, num_threads,
2647 imagex_progress_func);
2649 ret = wimlib_write_to_fd(dest_wim, dest_wim_fd,
2650 WIMLIB_ALL_IMAGES, write_flags,
2651 num_threads, imagex_progress_func);
2653 wimlib_free(dest_wim);
2655 wimlib_free(src_wim);
2657 string_set_destroy(&refglobs);
2661 usage(CMD_EXPORT, stderr);
2664 goto out_free_refglobs;
2668 is_root_wim_path(const tchar *path)
2671 for (p = path; *p; p++)
2672 if (*p != T('\\') && *p != T('/'))
2678 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2679 const tchar *dest_dir)
2681 for (size_t i = 0; i < num_cmds; i++)
2682 if (cmds[i].fs_dest_path != dest_dir)
2683 free(cmds[i].fs_dest_path);
2687 static struct wimlib_extract_command *
2688 prepare_extract_commands(tchar **paths, unsigned num_paths,
2689 int extract_flags, tchar *dest_dir,
2690 size_t *num_cmds_ret)
2692 struct wimlib_extract_command *cmds;
2694 tchar *emptystr = T("");
2696 if (num_paths == 0) {
2700 num_cmds = num_paths;
2701 cmds = calloc(num_cmds, sizeof(cmds[0]));
2703 imagex_error(T("Out of memory!"));
2707 for (size_t i = 0; i < num_cmds; i++) {
2708 cmds[i].extract_flags = extract_flags;
2709 cmds[i].wim_source_path = paths[i];
2710 if (is_root_wim_path(paths[i])) {
2711 cmds[i].fs_dest_path = dest_dir;
2713 size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2714 cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2715 if (!cmds[i].fs_dest_path) {
2716 free_extract_commands(cmds, num_cmds, dest_dir);
2719 tsprintf(cmds[i].fs_dest_path,
2720 T("%"TS""OS_PREFERRED_PATH_SEPARATOR_STRING"%"TS),
2721 dest_dir, tbasename(paths[i]));
2724 *num_cmds_ret = num_cmds;
2728 /* Extract files or directories from a WIM image */
2730 imagex_extract(int argc, tchar **argv, int cmd)
2737 const tchar *wimfile;
2738 const tchar *image_num_or_name;
2739 const tchar *pathlist;
2740 tchar *dest_dir = T(".");
2741 int extract_flags = WIMLIB_EXTRACT_FLAG_NORPFIX;
2742 int listfile_extract_flags = WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2744 STRING_SET(refglobs);
2746 struct wimlib_extract_command *cmds;
2749 for_opt(c, extract_options) {
2751 case IMAGEX_CHECK_OPTION:
2752 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2754 case IMAGEX_VERBOSE_OPTION:
2755 /* No longer does anything. */
2757 case IMAGEX_REF_OPTION:
2758 ret = string_set_append(&refglobs, optarg);
2760 goto out_free_refglobs;
2762 case IMAGEX_UNIX_DATA_OPTION:
2763 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2765 case IMAGEX_NO_ACLS_OPTION:
2766 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2768 case IMAGEX_STRICT_ACLS_OPTION:
2769 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2771 case IMAGEX_DEST_DIR_OPTION:
2774 case IMAGEX_TO_STDOUT_OPTION:
2775 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2776 imagex_info_file = stderr;
2777 imagex_be_quiet = true;
2779 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2780 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2781 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2783 case IMAGEX_NO_WILDCARDS_OPTION:
2784 listfile_extract_flags &= ~WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2786 case IMAGEX_STRICT_WILDCARDS_OPTION:
2787 listfile_extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_GLOB;
2800 image_num_or_name = argv[1];
2805 if (argc == 1 && argv[0][0] == T('@')) {
2806 pathlist = argv[0] + 1;
2810 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2817 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2821 image = wimlib_resolve_image(wim, image_num_or_name);
2822 ret = verify_image_exists_and_is_single(image,
2826 goto out_wimlib_free;
2828 if (refglobs.num_strings) {
2829 ret = wim_reference_globs(wim, &refglobs, open_flags);
2831 goto out_wimlib_free;
2835 if (ret == 0 && cmds != NULL) {
2836 ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
2837 imagex_progress_func);
2839 if (ret == 0 && pathlist != NULL) {
2840 ret = wimlib_extract_pathlist(wim, image, dest_dir,
2842 extract_flags | listfile_extract_flags,
2843 imagex_progress_func);
2846 if (!imagex_be_quiet)
2847 imagex_printf(T("Done extracting files.\n"));
2848 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2849 tfprintf(stderr, T("Note: You can use `%"TS"' to see what "
2850 "files and directories\n"
2851 " are in the WIM image.\n"),
2852 get_cmd_string(CMD_DIR, false));
2853 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2854 struct wimlib_wim_info info;
2856 wimlib_get_wim_info(wim, &info);
2857 do_resource_not_found_warning(wimfile, &info, &refglobs);
2862 free_extract_commands(cmds, num_cmds, dest_dir);
2864 string_set_destroy(&refglobs);
2868 usage(CMD_EXTRACT, stderr);
2871 goto out_free_refglobs;
2874 static void print_byte_field(const uint8_t field[], size_t len)
2877 tprintf(T("%02hhx"), *field++);
2881 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2883 tputs(T("WIM Information:"));
2884 tputs(T("----------------"));
2885 tprintf(T("Path: %"TS"\n"), wimfile);
2886 tprintf(T("GUID: 0x"));
2887 print_byte_field(info->guid, sizeof(info->guid));
2889 tprintf(T("Version: %u\n"), info->wim_version);
2890 tprintf(T("Image Count: %d\n"), info->image_count);
2891 tprintf(T("Compression: %"TS"\n"),
2892 wimlib_get_compression_type_string(info->compression_type));
2893 tprintf(T("Chunk Size: %"PRIu32" bytes\n"),
2895 tprintf(T("Part Number: %d/%d\n"), info->part_number, info->total_parts);
2896 tprintf(T("Boot Index: %d\n"), info->boot_index);
2897 tprintf(T("Size: %"PRIu64" bytes\n"), info->total_bytes);
2898 tprintf(T("Integrity Info: %"TS"\n"),
2899 info->has_integrity_table ? T("yes") : T("no"));
2900 tprintf(T("Relative path junction: %"TS"\n"),
2901 info->has_rpfix ? T("yes") : T("no"));
2902 tprintf(T("Pipable: %"TS"\n"),
2903 info->pipable ? T("yes") : T("no"));
2908 print_resource(const struct wimlib_resource_entry *resource,
2911 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
2912 resource->uncompressed_size);
2913 if (resource->packed) {
2914 tprintf(T("Raw compressed size = %"PRIu64" bytes\n"),
2915 resource->raw_resource_compressed_size);
2917 tprintf(T("Raw offset in WIM = %"PRIu64" bytes\n"),
2918 resource->raw_resource_offset_in_wim);
2920 tprintf(T("Offset in raw = %"PRIu64" bytes\n"),
2923 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
2924 resource->compressed_size);
2926 tprintf(T("Offset in WIM = %"PRIu64" bytes\n"),
2930 tprintf(T("Part Number = %u\n"), resource->part_number);
2931 tprintf(T("Reference Count = %u\n"), resource->reference_count);
2933 tprintf(T("Hash = 0x"));
2934 print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2937 tprintf(T("Flags = "));
2938 if (resource->is_compressed)
2939 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED "));
2940 if (resource->is_metadata)
2941 tprintf(T("WIM_RESHDR_FLAG_METADATA "));
2942 if (resource->is_free)
2943 tprintf(T("WIM_RESHDR_FLAG_FREE "));
2944 if (resource->is_spanned)
2945 tprintf(T("WIM_RESHDR_FLAG_SPANNED "));
2946 if (resource->packed)
2947 tprintf(T("WIM_RESHDR_FLAG_PACKED_STREAMS "));
2954 print_lookup_table(WIMStruct *wim)
2956 wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2959 /* Prints information about a WIM file; also can mark an image as bootable,
2960 * change the name of an image, or change the description of an image. */
2962 imagex_info(int argc, tchar **argv, int cmd)
2967 bool nocheck = false;
2968 bool header = false;
2969 bool lookup_table = false;
2971 bool metadata = false;
2972 bool short_header = true;
2973 const tchar *xml_out_file = NULL;
2974 const tchar *wimfile;
2975 const tchar *image_num_or_name;
2976 const tchar *new_name;
2977 const tchar *new_desc;
2982 struct wimlib_wim_info info;
2984 for_opt(c, info_options) {
2986 case IMAGEX_BOOT_OPTION:
2989 case IMAGEX_CHECK_OPTION:
2992 case IMAGEX_NOCHECK_OPTION:
2995 case IMAGEX_HEADER_OPTION:
2997 short_header = false;
2999 case IMAGEX_LOOKUP_TABLE_OPTION:
3000 lookup_table = true;
3001 short_header = false;
3003 case IMAGEX_XML_OPTION:
3005 short_header = false;
3007 case IMAGEX_EXTRACT_XML_OPTION:
3008 xml_out_file = optarg;
3009 short_header = false;
3011 case IMAGEX_METADATA_OPTION:
3013 short_header = false;
3022 if (argc < 1 || argc > 4)
3026 image_num_or_name = (argc >= 2) ? argv[1] : T("all");
3027 new_name = (argc >= 3) ? argv[2] : NULL;
3028 new_desc = (argc >= 4) ? argv[3] : NULL;
3030 if (check && nocheck) {
3031 imagex_error(T("Can't specify both --check and --nocheck"));
3036 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3038 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3042 wimlib_get_wim_info(wim, &info);
3044 image = wimlib_resolve_image(wim, image_num_or_name);
3045 ret = WIMLIB_ERR_INVALID_IMAGE;
3046 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
3047 verify_image_exists(image, image_num_or_name, wimfile);
3049 imagex_error(T("If you would like to set the boot "
3050 "index to 0, specify image \"0\" with "
3051 "the --boot flag."));
3053 goto out_wimlib_free;
3056 if (boot && info.image_count == 0) {
3057 imagex_error(T("--boot is meaningless on a WIM with no images"));
3058 goto out_wimlib_free;
3061 if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
3063 imagex_error(T("Cannot specify the --boot flag "
3064 "without specifying a specific "
3065 "image in a multi-image WIM"));
3066 goto out_wimlib_free;
3069 imagex_error(T("Cannot specify the NEW_NAME "
3070 "without specifying a specific "
3071 "image in a multi-image WIM"));
3072 goto out_wimlib_free;
3076 /* Operations that print information are separated from operations that
3077 * recreate the WIM file. */
3078 if (!new_name && !boot) {
3080 /* Read-only operations */
3082 if (image == WIMLIB_NO_IMAGE) {
3083 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
3084 image_num_or_name, wimfile);
3085 goto out_wimlib_free;
3088 if (image == WIMLIB_ALL_IMAGES && short_header)
3089 print_wim_information(wimfile, &info);
3092 wimlib_print_header(wim);
3095 if (info.total_parts != 1) {
3096 tfprintf(stderr, T("Warning: Only showing the lookup table "
3097 "for part %d of a %d-part WIM.\n"),
3098 info.part_number, info.total_parts);
3100 print_lookup_table(wim);
3104 ret = wimlib_extract_xml_data(wim, stdout);
3106 goto out_wimlib_free;
3112 fp = tfopen(xml_out_file, T("wb"));
3114 imagex_error_with_errno(T("Failed to open the "
3115 "file \"%"TS"\" for "
3119 goto out_wimlib_free;
3121 ret = wimlib_extract_xml_data(wim, fp);
3123 imagex_error(T("Failed to close the file "
3129 goto out_wimlib_free;
3133 wimlib_print_available_images(wim, image);
3136 ret = wimlib_print_metadata(wim, image);
3138 goto out_wimlib_free;
3143 /* Modification operations */
3145 if (image == WIMLIB_ALL_IMAGES)
3148 if (image == WIMLIB_NO_IMAGE && new_name) {
3149 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
3150 "when using image 0"), new_name);
3152 goto out_wimlib_free;
3156 if (image == info.boot_index) {
3157 imagex_printf(T("Image %d is already marked as "
3158 "bootable.\n"), image);
3161 imagex_printf(T("Marking image %d as bootable.\n"),
3163 info.boot_index = image;
3164 ret = wimlib_set_wim_info(wim, &info,
3165 WIMLIB_CHANGE_BOOT_INDEX);
3167 goto out_wimlib_free;
3171 if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
3173 imagex_printf(T("Image %d is already named \"%"TS"\".\n"),
3177 imagex_printf(T("Changing the name of image %d to "
3178 "\"%"TS"\".\n"), image, new_name);
3179 ret = wimlib_set_image_name(wim, image, new_name);
3181 goto out_wimlib_free;
3185 const tchar *old_desc;
3186 old_desc = wimlib_get_image_description(wim, image);
3187 if (old_desc && !tstrcmp(old_desc, new_desc)) {
3188 imagex_printf(T("The description of image %d is already "
3189 "\"%"TS"\".\n"), image, new_desc);
3192 imagex_printf(T("Changing the description of image %d "
3193 "to \"%"TS"\".\n"), image, new_desc);
3194 ret = wimlib_set_image_descripton(wim, image,
3197 goto out_wimlib_free;
3201 /* Only call wimlib_overwrite() if something actually needs to
3203 if (boot || new_name || new_desc ||
3204 (check && !info.has_integrity_table) ||
3205 (nocheck && info.has_integrity_table))
3207 int write_flags = 0;
3210 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3212 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3213 ret = wimlib_overwrite(wim, write_flags, 1,
3214 imagex_progress_func);
3216 imagex_printf(T("The file \"%"TS"\" was not modified "
3217 "because nothing needed to be done.\n"),
3228 usage(CMD_INFO, stderr);
3234 /* Join split WIMs into one part WIM */
3236 imagex_join(int argc, tchar **argv, int cmd)
3239 int swm_open_flags = 0;
3240 int wim_write_flags = 0;
3241 const tchar *output_path;
3244 for_opt(c, join_options) {
3246 case IMAGEX_CHECK_OPTION:
3247 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3248 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3258 imagex_error(T("Must specify one or more split WIM (.swm) "
3262 output_path = argv[0];
3263 ret = wimlib_join((const tchar * const *)++argv,
3268 imagex_progress_func);
3273 usage(CMD_JOIN, stderr);
3278 #if WIM_MOUNTING_SUPPORTED
3280 /* Mounts a WIM image. */
3282 imagex_mount_rw_or_ro(int argc, tchar **argv, int cmd)
3285 int mount_flags = 0;
3287 const tchar *staging_dir = NULL;
3288 const tchar *wimfile;
3291 struct wimlib_wim_info info;
3295 STRING_SET(refglobs);
3297 if (cmd == CMD_MOUNTRW) {
3298 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
3299 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3302 for_opt(c, mount_options) {
3304 case IMAGEX_ALLOW_OTHER_OPTION:
3305 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
3307 case IMAGEX_CHECK_OPTION:
3308 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3310 case IMAGEX_DEBUG_OPTION:
3311 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
3313 case IMAGEX_STREAMS_INTERFACE_OPTION:
3314 if (!tstrcasecmp(optarg, T("none")))
3315 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
3316 else if (!tstrcasecmp(optarg, T("xattr")))
3317 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
3318 else if (!tstrcasecmp(optarg, T("windows")))
3319 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
3321 imagex_error(T("Unknown stream interface \"%"TS"\""),
3326 case IMAGEX_REF_OPTION:
3327 ret = string_set_append(&refglobs, optarg);
3329 goto out_free_refglobs;
3331 case IMAGEX_STAGING_DIR_OPTION:
3332 staging_dir = optarg;
3334 case IMAGEX_UNIX_DATA_OPTION:
3335 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
3343 if (argc != 2 && argc != 3)
3348 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3350 goto out_free_refglobs;
3352 wimlib_get_wim_info(wim, &info);
3355 /* Image explicitly specified. */
3356 image = wimlib_resolve_image(wim, argv[1]);
3358 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
3362 /* No image specified; default to image 1, but only if the WIM
3363 * contains exactly one image. */
3365 if (info.image_count != 1) {
3366 imagex_error(T("\"%"TS"\" contains %d images; Please "
3367 "select one."), wimfile, info.image_count);
3375 if (refglobs.num_strings) {
3376 ret = wim_reference_globs(wim, &refglobs, open_flags);
3381 ret = wimlib_mount_image(wim, image, dir, mount_flags, staging_dir);
3383 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
3385 image, wimfile, dir);
3390 string_set_destroy(&refglobs);
3396 goto out_free_refglobs;
3398 #endif /* WIM_MOUNTING_SUPPORTED */
3400 /* Rebuild a WIM file */
3402 imagex_optimize(int argc, tchar **argv, int cmd)
3405 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3406 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
3407 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
3408 uint32_t chunk_size = UINT32_MAX;
3411 const tchar *wimfile;
3414 unsigned num_threads = 0;
3416 for_opt(c, optimize_options) {
3418 case IMAGEX_CHECK_OPTION:
3419 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3420 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3422 case IMAGEX_NOCHECK_OPTION:
3423 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3425 case IMAGEX_COMPRESS_OPTION:
3426 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3427 compression_type = get_compression_type(optarg);
3428 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
3431 case IMAGEX_RECOMPRESS_OPTION:
3432 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3434 case IMAGEX_COMPRESS_SLOW_OPTION:
3435 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3436 set_compress_slow();
3438 case IMAGEX_CHUNK_SIZE_OPTION:
3439 chunk_size = parse_chunk_size(optarg);
3440 if (chunk_size == UINT32_MAX)
3443 case IMAGEX_PACK_STREAMS_OPTION:
3444 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
3445 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3447 case IMAGEX_THREADS_OPTION:
3448 num_threads = parse_num_threads(optarg);
3449 if (num_threads == UINT_MAX)
3452 case IMAGEX_PIPABLE_OPTION:
3453 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
3455 case IMAGEX_NOT_PIPABLE_OPTION:
3456 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
3470 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3474 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
3475 /* Change compression type. */
3476 ret = wimlib_set_output_compression_type(wim, compression_type);
3478 goto out_wimlib_free;
3481 if (chunk_size != UINT32_MAX) {
3482 /* Change chunk size. */
3483 ret = wimlib_set_output_chunk_size(wim, chunk_size);
3485 goto out_wimlib_free;
3488 old_size = file_get_size(wimfile);
3489 tprintf(T("\"%"TS"\" original size: "), wimfile);
3491 tputs(T("Unknown"));
3493 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3495 ret = wimlib_overwrite(wim, write_flags, num_threads,
3496 imagex_progress_func);
3498 imagex_error(T("Optimization of \"%"TS"\" failed."), wimfile);
3499 goto out_wimlib_free;
3502 new_size = file_get_size(wimfile);
3503 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3505 tputs(T("Unknown"));
3507 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3509 tfputs(T("Space saved: "), stdout);
3510 if (new_size != -1 && old_size != -1) {
3511 tprintf(T("%lld KiB\n"),
3512 ((long long)old_size - (long long)new_size) >> 10);
3514 tputs(T("Unknown"));
3523 usage(CMD_OPTIMIZE, stderr);
3529 /* Split a WIM into a spanned set */
3531 imagex_split(int argc, tchar **argv, int cmd)
3535 int write_flags = 0;
3536 unsigned long part_size;
3541 for_opt(c, split_options) {
3543 case IMAGEX_CHECK_OPTION:
3544 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3545 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3557 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3558 if (tmp == argv[2] || *tmp) {
3559 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3560 imagex_error(T("The part size must be an integer or "
3561 "floating-point number of megabytes."));
3564 ret = wimlib_open_wim(argv[0], open_flags, &wim, imagex_progress_func);
3568 ret = wimlib_split(wim, argv[1], part_size, write_flags, imagex_progress_func);
3574 usage(CMD_SPLIT, stderr);
3580 #if WIM_MOUNTING_SUPPORTED
3581 /* Unmounts a mounted WIM image. */
3583 imagex_unmount(int argc, tchar **argv, int cmd)
3586 int unmount_flags = 0;
3589 for_opt(c, unmount_options) {
3591 case IMAGEX_COMMIT_OPTION:
3592 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3594 case IMAGEX_CHECK_OPTION:
3595 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3597 case IMAGEX_REBUILD_OPTION:
3598 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3600 case IMAGEX_LAZY_OPTION:
3601 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3612 ret = wimlib_unmount_image(argv[0], unmount_flags,
3613 imagex_progress_func);
3615 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3620 usage(CMD_UNMOUNT, stderr);
3624 #endif /* WIM_MOUNTING_SUPPORTED */
3627 * Add, delete, or rename files in a WIM image.
3630 imagex_update(int argc, tchar **argv, int cmd)
3632 const tchar *wimfile;
3636 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3637 int write_flags = 0;
3638 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3639 int default_add_flags = WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
3640 WIMLIB_ADD_FLAG_VERBOSE;
3641 int default_delete_flags = 0;
3642 unsigned num_threads = 0;
3644 tchar *cmd_file_contents;
3645 size_t cmd_file_nchars;
3646 struct wimlib_update_command *cmds;
3648 tchar *command_str = NULL;
3650 const tchar *config_file = NULL;
3652 struct wimlib_capture_config *config;
3654 for_opt(c, update_options) {
3656 /* Generic or write options */
3657 case IMAGEX_THREADS_OPTION:
3658 num_threads = parse_num_threads(optarg);
3659 if (num_threads == UINT_MAX)
3662 case IMAGEX_CHECK_OPTION:
3663 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3664 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3666 case IMAGEX_REBUILD_OPTION:
3667 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3669 case IMAGEX_COMMAND_OPTION:
3671 imagex_error(T("--command may only be specified "
3672 "one time. Please provide\n"
3673 " the update commands "
3674 "on standard input instead."));
3677 command_str = tstrdup(optarg);
3679 imagex_error(T("Out of memory!"));
3683 /* Default delete options */
3684 case IMAGEX_FORCE_OPTION:
3685 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3687 case IMAGEX_RECURSIVE_OPTION:
3688 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3691 /* Global add option */
3692 case IMAGEX_CONFIG_OPTION:
3693 default_add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
3694 config_file = optarg;
3697 /* Default add options */
3698 case IMAGEX_VERBOSE_OPTION:
3699 /* No longer does anything. */
3701 case IMAGEX_DEREFERENCE_OPTION:
3702 default_add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
3704 case IMAGEX_UNIX_DATA_OPTION:
3705 default_add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
3707 case IMAGEX_NO_ACLS_OPTION:
3708 default_add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
3710 case IMAGEX_STRICT_ACLS_OPTION:
3711 default_add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
3720 if (argc != 1 && argc != 2)
3724 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3726 goto out_free_command_str;
3729 /* Image explicitly specified. */
3730 image = wimlib_resolve_image(wim, argv[1]);
3731 ret = verify_image_exists_and_is_single(image, argv[1],
3734 goto out_wimlib_free;
3736 /* No image specified; default to image 1, but only if the WIM
3737 * contains exactly one image. */
3738 struct wimlib_wim_info info;
3740 wimlib_get_wim_info(wim, &info);
3741 if (info.image_count != 1) {
3742 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3743 wimfile, info.image_count);
3750 /* Parse capture configuration file if specified */
3754 config_str = file_get_text_contents(config_file, &config_len);
3757 goto out_wimlib_free;
3760 config = alloca(sizeof(*config));
3761 ret = parse_capture_config(&config_str, config_len, config);
3763 goto out_free_config;
3766 default_add_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
3769 /* Read update commands from standard input, or the command string if
3772 cmd_file_contents = NULL;
3773 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3776 if (isatty(STDIN_FILENO)) {
3777 tputs(T("Reading update commands from standard input..."));
3778 recommend_man_page(CMD_UPDATE, stdout);
3780 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3781 if (!cmd_file_contents) {
3783 goto out_free_config;
3786 /* Parse the update commands */
3787 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3792 goto out_free_cmd_file_contents;
3795 /* Set default flags and capture config on the update commands */
3796 for (size_t i = 0; i < num_cmds; i++) {
3797 switch (cmds[i].op) {
3798 case WIMLIB_UPDATE_OP_ADD:
3799 cmds[i].add.add_flags |= default_add_flags;
3800 cmds[i].add.config = config;
3802 case WIMLIB_UPDATE_OP_DELETE:
3803 cmds[i].delete_.delete_flags |= default_delete_flags;
3810 /* Execute the update commands */
3811 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3812 imagex_progress_func);
3816 /* Overwrite the updated WIM */
3817 ret = wimlib_overwrite(wim, write_flags, num_threads,
3818 imagex_progress_func);
3821 out_free_cmd_file_contents:
3822 free(cmd_file_contents);
3825 free(config->exclusion_pats.pats);
3826 free(config->exclusion_exception_pats.pats);
3831 out_free_command_str:
3836 usage(CMD_UPDATE, stderr);
3839 goto out_free_command_str;
3844 struct imagex_command {
3846 int (*func)(int argc, tchar **argv, int cmd);
3849 static const struct imagex_command imagex_commands[] = {
3850 [CMD_APPEND] = {T("append"), imagex_capture_or_append},
3851 [CMD_APPLY] = {T("apply"), imagex_apply},
3852 [CMD_CAPTURE] = {T("capture"), imagex_capture_or_append},
3853 [CMD_DELETE] = {T("delete"), imagex_delete},
3854 [CMD_DIR ] = {T("dir"), imagex_dir},
3855 [CMD_EXPORT] = {T("export"), imagex_export},
3856 [CMD_EXTRACT] = {T("extract"), imagex_extract},
3857 [CMD_INFO] = {T("info"), imagex_info},
3858 [CMD_JOIN] = {T("join"), imagex_join},
3859 #if WIM_MOUNTING_SUPPORTED
3860 [CMD_MOUNT] = {T("mount"), imagex_mount_rw_or_ro},
3861 [CMD_MOUNTRW] = {T("mountrw"), imagex_mount_rw_or_ro},
3863 [CMD_OPTIMIZE] = {T("optimize"), imagex_optimize},
3864 [CMD_SPLIT] = {T("split"), imagex_split},
3865 #if WIM_MOUNTING_SUPPORTED
3866 [CMD_UNMOUNT] = {T("unmount"), imagex_unmount},
3868 [CMD_UPDATE] = {T("update"), imagex_update},
3871 static const tchar *usage_strings[] = {
3874 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3875 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3876 " [--nocheck] [--flags EDITION_ID] [--dereference]\n"
3877 " [--config=FILE] [--threads=NUM_THREADS] [--source-list]\n"
3878 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3879 " [--update-of=[WIMFILE:]IMAGE]\n"
3883 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME | all)]\n"
3884 " (DIRECTORY | NTFS_VOLUME) [--check] [--ref=\"GLOB\"]\n"
3885 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3886 " [--hardlink] [--symlink] [--include-invalid-names]\n"
3890 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3891 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3892 " [--nocheck] [--compress=TYPE] [--flags EDITION_ID]\n"
3893 " [--dereference] [--config=FILE] [--threads=NUM_THREADS]\n"
3894 " [--source-list] [--no-acls] [--strict-acls] [--rpfix]\n"
3895 " [--norpfix] [--update-of=[WIMFILE:]IMAGE]\n"
3896 " [--delta-from=WIMFILE]\n"
3900 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n"
3901 " [--check] [--soft]\n"
3905 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH]\n"
3909 " %"TS" SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
3910 " DEST_WIMFILE [DEST_IMAGE_NAME [DEST_IMAGE_DESCRIPTION]]\n"
3911 " [--boot] [--check] [--nocheck] [--compress=TYPE]\n"
3912 " [--ref=\"GLOB\"] [--threads=NUM_THREADS] [--rebuild]\n"
3916 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME) ([PATH...] | @LISTFILE)\n"
3917 " [--check] [--ref=\"GLOB\"] [--no-acls] [--strict-acls]\n"
3918 " [--to-stdout] [--dest-dir=CMD_DIR]\n"
3919 " [--include-invalid-names]\n"
3923 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME) [NEW_NAME\n"
3924 " [NEW_DESC]]] [--boot] [--check] [--nocheck] [--xml]\n"
3925 " [--extract-xml FILE] [--header] [--lookup-table]\n"
3929 " %"TS" OUT_WIMFILE SPLIT_WIM_PART... [--check]\n"
3931 #if WIM_MOUNTING_SUPPORTED
3934 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3935 " [--check] [--streams-interface=INTERFACE]\n"
3936 " [--ref=\"GLOB\"] [--allow-other]\n"
3940 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3941 " [--check] [--streams-interface=INTERFACE]\n"
3942 " [--staging-dir=CMD_DIR] [--allow-other]\n"
3947 " %"TS" WIMFILE [--check] [--nocheck] [--recompress]\n"
3948 " [--recompress-slow] [--compress=TYPE]\n"
3949 " [--threads=NUM_THREADS]\n"
3953 " %"TS" WIMFILE SPLIT_WIM_PART_1 PART_SIZE_MB [--check]\n"
3955 #if WIM_MOUNTING_SUPPORTED
3958 " %"TS" DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
3963 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
3964 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
3965 " [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
3969 static const tchar *invocation_name;
3970 static int invocation_cmd = CMD_NONE;
3972 static const tchar *get_cmd_string(int cmd, bool nospace)
3974 static tchar buf[50];
3975 if (cmd == CMD_NONE) {
3976 tsprintf(buf, T("%"TS), T(IMAGEX_PROGNAME));
3977 } else if (invocation_cmd != CMD_NONE) {
3978 tsprintf(buf, T("wim%"TS), imagex_commands[cmd].name);
3980 const tchar *format;
3983 format = T("%"TS"-%"TS"");
3985 format = T("%"TS" %"TS"");
3986 tsprintf(buf, format, invocation_name, imagex_commands[cmd].name);
3994 static const tchar *s =
3996 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
3997 "Copyright (C) 2012, 2013 Eric Biggers\n"
3998 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
3999 "This is free software: you are free to change and redistribute it.\n"
4000 "There is NO WARRANTY, to the extent permitted by law.\n"
4002 "Report bugs to "PACKAGE_BUGREPORT".\n"
4009 help_or_version(int argc, tchar **argv, int cmd)
4014 for (i = 1; i < argc; i++) {
4016 if (p[0] == T('-') && p[1] == T('-')) {
4018 if (!tstrcmp(p, T("help"))) {
4019 if (cmd == CMD_NONE)
4024 } else if (!tstrcmp(p, T("version"))) {
4033 print_usage_string(int cmd, FILE *fp)
4035 tfprintf(fp, usage_strings[cmd], get_cmd_string(cmd, false));
4039 recommend_man_page(int cmd, FILE *fp)
4041 const tchar *format_str;
4043 format_str = T("Uncommon options are not listed;\n"
4044 "See %"TS".pdf in the doc directory for more details.\n");
4046 format_str = T("Uncommon options are not listed;\n"
4047 "Try `man %"TS"' for more details.\n");
4049 tfprintf(fp, format_str, get_cmd_string(cmd, true));
4053 usage(int cmd, FILE *fp)
4055 tfprintf(fp, T("Usage:\n"));
4056 print_usage_string(cmd, fp);
4057 tfprintf(fp, T("\n"));
4058 recommend_man_page(cmd, fp);
4064 tfprintf(fp, T("Usage:\n"));
4065 for (int cmd = 0; cmd < CMD_MAX; cmd++) {
4066 print_usage_string(cmd, fp);
4067 tfprintf(fp, T("\n"));
4069 static const tchar *extra =
4072 " %"TS" --version\n"
4074 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
4077 tfprintf(fp, extra, invocation_name, invocation_name);
4078 recommend_man_page(CMD_NONE, fp);
4081 /* Entry point for wimlib's ImageX implementation. On UNIX the command
4082 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
4083 * something else), while an Windows the command arguments will be UTF-16LE
4084 * encoded 'wchar_t' strings. */
4087 wmain(int argc, wchar_t **argv, wchar_t **envp)
4089 main(int argc, char **argv)
4096 imagex_info_file = stdout;
4097 invocation_name = tbasename(argv[0]);
4100 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
4101 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
4105 setlocale(LC_ALL, "");
4106 codeset = nl_langinfo(CODESET);
4107 if (!strstr(codeset, "UTF-8") &&
4108 !strstr(codeset, "UTF8") &&
4109 !strstr(codeset, "utf-8") &&
4110 !strstr(codeset, "utf8"))
4113 "WARNING: Running %"TS" in a UTF-8 locale is recommended!\n"
4114 " Maybe try: `export LANG=en_US.UTF-8'?\n"
4115 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
4116 " to any value to force wimlib to use UTF-8.\n",
4122 #endif /* !__WIN32__ */
4125 tchar *igcase = tgetenv(T("WIMLIB_IMAGEX_IGNORE_CASE"));
4126 if (igcase != NULL) {
4127 if (!tstrcmp(igcase, T("no")) ||
4128 !tstrcmp(igcase, T("0")))
4129 init_flags |= WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE;
4130 else if (!tstrcmp(igcase, T("yes")) ||
4131 !tstrcmp(igcase, T("1")))
4132 init_flags |= WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE;
4135 "WARNING: Ignoring unknown setting of "
4136 "WIMLIB_IMAGEX_IGNORE_CASE\n");
4141 /* Allow being invoked as wimCOMMAND (e.g. wimapply). */
4143 if (!tstrncmp(invocation_name, T("wim"), 3) &&
4144 tstrcmp(invocation_name, T(IMAGEX_PROGNAME))) {
4145 for (int i = 0; i < CMD_MAX; i++) {
4146 if (!tstrcmp(invocation_name + 3,
4147 imagex_commands[i].name))
4156 /* Unless already known from the invocation name, determine which
4157 * command was specified. */
4158 if (cmd == CMD_NONE) {
4160 imagex_error(T("No command specified!\n"));
4164 for (int i = 0; i < CMD_MAX; i++) {
4165 if (!tstrcmp(argv[1], imagex_commands[i].name)) {
4170 if (cmd != CMD_NONE) {
4176 /* Handle --help and --version. --help can be either for the program as
4177 * a whole (cmd == CMD_NONE) or just for a specific command (cmd !=
4178 * CMD_NONE). Note: help_or_version() will not return if a --help or
4179 * --version argument was found. */
4180 help_or_version(argc, argv, cmd);
4182 /* Bail if a valid command was not specified. */
4183 if (cmd == CMD_NONE) {
4184 imagex_error(T("Unrecognized command: `%"TS"'\n"), argv[1]);
4189 /* Enable warning and error messages in wimlib be more user-friendly.
4191 wimlib_set_print_errors(true);
4193 /* Initialize wimlib. */
4194 ret = wimlib_global_init(init_flags);
4196 goto out_check_status;
4198 /* Call the command handler function. */
4199 ret = imagex_commands[cmd].func(argc, argv, cmd);
4201 /* Check for error writing to standard output, especially since for some
4202 * commands, writing to standard output is part of the program's actual
4203 * behavior and not just for informational purposes. */
4204 if (ferror(stdout) || fclose(stdout)) {
4205 imagex_error_with_errno(T("error writing to standard output"));
4210 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
4211 * itself (not by wimlib). 0 indicates success. > 0 indicates a wimlib
4212 * error code from which an error message can be printed. */
4214 imagex_error(T("Exiting with error code %d:\n"
4216 wimlib_get_error_string(ret));
4217 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
4218 imagex_error_with_errno(T("errno"));
4220 /* Make wimlib free any resources it's holding (although this is not
4221 * strictly necessary because the process is ending anyway). */
4222 wimlib_global_cleanup();