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"\\"
54 # define print_security_descriptor win32_print_security_descriptor
58 # include <langinfo.h>
59 # define tbasename basename
60 # define OS_PREFERRED_PATH_SEPARATOR '/'
61 # define OS_PREFERRED_PATH_SEPARATOR_STRING "/"
62 # define print_security_descriptor default_print_security_descriptor
63 static inline void set_fd_to_binary_mode(int fd)
68 /* Don't confuse the user by presenting the mounting commands on Windows when
69 * they will never work. However on UNIX-like systems we always present them,
70 * even if WITH_FUSE is not defined at this point, as to not tie the build of
71 * wimlib-imagex to a specific build of wimlib. */
73 # define WIM_MOUNTING_SUPPORTED 0
75 # define WIM_MOUNTING_SUPPORTED 1
78 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
80 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
94 #if WIM_MOUNTING_SUPPORTED
100 #if WIM_MOUNTING_SUPPORTED
107 static void usage(int cmd, FILE *fp);
108 static void usage_all(FILE *fp);
109 static void recommend_man_page(int cmd, FILE *fp);
110 static const tchar *get_cmd_string(int cmd, bool nospace);
112 static int imagex_progress_func(enum wimlib_progress_msg msg,
113 const union wimlib_progress_info *info);
115 static bool imagex_be_quiet = false;
116 static FILE *imagex_info_file;
118 #define imagex_printf(format, ...) \
119 tfprintf(imagex_info_file, format, ##__VA_ARGS__)
122 IMAGEX_ALLOW_OTHER_OPTION,
125 IMAGEX_CHUNK_SIZE_OPTION,
126 IMAGEX_COMMAND_OPTION,
127 IMAGEX_COMMIT_OPTION,
128 IMAGEX_COMPRESS_OPTION,
129 IMAGEX_COMPRESS_SLOW_OPTION,
130 IMAGEX_CONFIG_OPTION,
132 IMAGEX_DELTA_FROM_OPTION,
133 IMAGEX_DEREFERENCE_OPTION,
134 IMAGEX_DEST_DIR_OPTION,
135 IMAGEX_DETAILED_OPTION,
136 IMAGEX_EXTRACT_XML_OPTION,
139 IMAGEX_HARDLINK_OPTION,
140 IMAGEX_HEADER_OPTION,
141 IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
143 IMAGEX_LOOKUP_TABLE_OPTION,
144 IMAGEX_METADATA_OPTION,
145 IMAGEX_NORPFIX_OPTION,
146 IMAGEX_NOCHECK_OPTION,
147 IMAGEX_NO_ACLS_OPTION,
148 IMAGEX_NO_WILDCARDS_OPTION,
149 IMAGEX_NOT_PIPABLE_OPTION,
150 IMAGEX_PACK_CHUNK_SIZE_OPTION,
151 IMAGEX_PACK_STREAMS_OPTION,
153 IMAGEX_PIPABLE_OPTION,
154 IMAGEX_REBUILD_OPTION,
155 IMAGEX_RECOMPRESS_OPTION,
156 IMAGEX_RECURSIVE_OPTION,
158 IMAGEX_RESUME_OPTION,
161 IMAGEX_SOURCE_LIST_OPTION,
162 IMAGEX_STAGING_DIR_OPTION,
163 IMAGEX_STREAMS_INTERFACE_OPTION,
164 IMAGEX_STRICT_ACLS_OPTION,
165 IMAGEX_STRICT_WILDCARDS_OPTION,
166 IMAGEX_SYMLINK_OPTION,
167 IMAGEX_THREADS_OPTION,
168 IMAGEX_TO_STDOUT_OPTION,
169 IMAGEX_UNIX_DATA_OPTION,
170 IMAGEX_UPDATE_OF_OPTION,
171 IMAGEX_VERBOSE_OPTION,
175 static const struct option apply_options[] = {
176 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
177 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
178 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
179 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
180 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
181 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
182 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
183 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
184 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
185 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
186 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
187 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
189 /* --resume is undocumented for now as it needs improvement. */
190 {T("resume"), no_argument, NULL, IMAGEX_RESUME_OPTION},
194 static const struct option capture_or_append_options[] = {
195 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
196 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
197 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
198 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
199 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
200 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
201 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
202 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
203 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
204 {T("pack-streams"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
205 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
206 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
207 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
208 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
209 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
210 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
211 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
212 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
213 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
214 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
215 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
216 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
217 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
218 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
219 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
220 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
221 {T("update-of"), required_argument, NULL, IMAGEX_UPDATE_OF_OPTION},
222 {T("delta-from"), required_argument, NULL, IMAGEX_DELTA_FROM_OPTION},
226 static const struct option delete_options[] = {
227 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
228 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
232 static const struct option dir_options[] = {
233 {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
234 {T("detailed"), no_argument, NULL, IMAGEX_DETAILED_OPTION},
238 static const struct option export_options[] = {
239 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
240 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
241 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
242 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
243 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
244 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
245 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
246 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
247 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
248 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
249 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
250 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
251 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
252 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
253 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
257 static const struct option extract_options[] = {
258 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
259 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
260 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
261 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
262 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
263 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
264 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
265 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
266 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
267 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
268 {T("strict-wildcards"), no_argument, NULL, IMAGEX_STRICT_WILDCARDS_OPTION},
269 {T("no-wildcards"), no_argument, NULL, IMAGEX_NO_WILDCARDS_OPTION},
273 static const struct option info_options[] = {
274 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
275 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
276 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
277 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
278 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
279 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
280 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
281 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
282 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
286 static const struct option join_options[] = {
287 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
291 static const struct option mount_options[] = {
292 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
293 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
294 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
295 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
296 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
297 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
298 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
302 static const struct option optimize_options[] = {
303 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
304 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
305 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
306 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
307 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
308 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
309 {T("recompress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
310 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
311 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
312 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
313 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
314 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
315 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
316 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
317 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
321 static const struct option split_options[] = {
322 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
326 static const struct option unmount_options[] = {
327 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
328 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
329 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
330 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
334 static const struct option update_options[] = {
335 /* Careful: some of the options here set the defaults for update
336 * commands, but the flags given to an actual update command (and not to
337 * `imagex update' itself are also handled in
338 * update_command_add_option(). */
339 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
340 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
341 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
342 {T("command"), required_argument, NULL, IMAGEX_COMMAND_OPTION},
344 /* Default delete options */
345 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
346 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
348 /* Global add option */
349 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
351 /* Default add options */
352 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
353 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
354 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
355 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
356 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
357 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
363 # define _format_attribute(type, format_str, args_start) \
364 __attribute__((format(type, format_str, args_start)))
366 # define _format_attribute(type, format_str, args_start)
369 /* Print formatted error message to stderr. */
370 static void _format_attribute(printf, 1, 2)
371 imagex_error(const tchar *format, ...)
374 va_start(va, format);
375 tfputs(T("ERROR: "), stderr);
376 tvfprintf(stderr, format, va);
377 tputc(T('\n'), stderr);
381 /* Print formatted error message to stderr. */
382 static void _format_attribute(printf, 1, 2)
383 imagex_error_with_errno(const tchar *format, ...)
385 int errno_save = errno;
387 va_start(va, format);
388 tfputs(T("ERROR: "), stderr);
389 tvfprintf(stderr, format, va);
390 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
395 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
397 if (image == WIMLIB_NO_IMAGE) {
398 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
399 " Please specify a 1-based image index or "
400 "image name. To list the images\n"
401 " contained in the WIM archive, run\n"
403 " %"TS" \"%"TS"\"\n"),
404 image_name, wim_name,
405 get_cmd_string(CMD_INFO, false), wim_name);
406 return WIMLIB_ERR_INVALID_IMAGE;
412 verify_image_is_single(int image)
414 if (image == WIMLIB_ALL_IMAGES) {
415 imagex_error(T("Cannot specify all images for this action!"));
416 return WIMLIB_ERR_INVALID_IMAGE;
422 verify_image_exists_and_is_single(int image, const tchar *image_name,
423 const tchar *wim_name)
426 ret = verify_image_exists(image, image_name, wim_name);
428 ret = verify_image_is_single(image);
432 /* Parse the argument to --compress */
434 get_compression_type(const tchar *optarg)
436 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
437 return WIMLIB_COMPRESSION_TYPE_LZX;
438 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
439 return WIMLIB_COMPRESSION_TYPE_XPRESS;
440 else if (!tstrcasecmp(optarg, T("recovery")) || !tstrcasecmp(optarg, T("lzms")))
441 return WIMLIB_COMPRESSION_TYPE_LZMS;
442 else if (!tstrcasecmp(optarg, T("none")))
443 return WIMLIB_COMPRESSION_TYPE_NONE;
445 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
446 "\"maximum\", \"fast\", or \"none\"."), optarg);
447 return WIMLIB_COMPRESSION_TYPE_INVALID;
452 set_compress_slow(void)
454 static const struct wimlib_lzx_compressor_params lzx_slow_params = {
456 .size = sizeof(struct wimlib_lzx_compressor_params),
458 .algorithm = WIMLIB_LZX_ALGORITHM_SLOW,
461 .use_len2_matches = 1,
462 .nice_match_length = 96,
463 .num_optim_passes = 4,
464 .max_search_depth = 100,
465 .max_matches_per_pos = 10,
466 .main_nostat_cost = 15,
467 .len_nostat_cost = 15,
468 .aligned_nostat_cost = 7,
473 static const struct wimlib_lzms_compressor_params lzms_slow_params = {
475 .size = sizeof(struct wimlib_lzms_compressor_params),
477 .min_match_length = 2,
478 .max_match_length = UINT32_MAX,
479 .nice_match_length = 96,
480 .max_search_depth = 100,
481 .max_matches_per_pos = 10,
482 .optim_array_length = 1024,
485 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
486 &lzx_slow_params.hdr);
488 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZMS,
489 &lzms_slow_params.hdr);
493 const tchar **strings;
494 unsigned num_strings;
495 unsigned num_alloc_strings;
498 #define STRING_SET_INITIALIZER \
499 { .strings = NULL, .num_strings = 0, .num_alloc_strings = 0, }
501 #define STRING_SET(_strings) \
502 struct string_set _strings = STRING_SET_INITIALIZER
505 string_set_append(struct string_set *set, const tchar *glob)
507 unsigned num_alloc_strings = set->num_alloc_strings;
509 if (set->num_strings == num_alloc_strings) {
510 const tchar **new_strings;
512 num_alloc_strings += 4;
513 new_strings = realloc(set->strings,
514 sizeof(set->strings[0]) * num_alloc_strings);
516 imagex_error(T("Out of memory!"));
519 set->strings = new_strings;
520 set->num_alloc_strings = num_alloc_strings;
522 set->strings[set->num_strings++] = glob;
527 string_set_destroy(struct string_set *set)
533 wim_reference_globs(WIMStruct *wim, struct string_set *set, int open_flags)
535 return wimlib_reference_resource_files(wim, set->strings,
537 WIMLIB_REF_FLAG_GLOB_ENABLE,
539 imagex_progress_func);
543 do_resource_not_found_warning(const tchar *wimfile,
544 const struct wimlib_wim_info *info,
545 const struct string_set *refglobs)
547 if (info->total_parts > 1) {
548 if (refglobs->num_strings == 0) {
549 imagex_error(T("\"%"TS"\" is part of a split WIM. "
550 "Use --ref to specify the other parts."),
553 imagex_error(T("Perhaps the '--ref' argument did not "
554 "specify all other parts of the split "
558 imagex_error(T("If this is a delta WIM, use the --ref argument "
559 "to specify the WIM(s) on which it is based."));
563 /* Returns the size of a file given its name, or -1 if the file does not exist
564 * or its size cannot be determined. */
566 file_get_size(const tchar *filename)
569 if (tstat(filename, &st) == 0)
576 PARSE_STRING_SUCCESS = 0,
577 PARSE_STRING_FAILURE = 1,
578 PARSE_STRING_NONE = 2,
582 * Parses a string token from an array of characters.
584 * Tokens are either whitespace-delimited, or double or single-quoted.
586 * @line_p: Pointer to the pointer to the line of data. Will be updated
587 * to point past the string token iff the return value is
588 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
591 * @len_p: @len_p initially stores the length of the line of data, which may
592 * be 0, and it will be updated to the number of bytes remaining in
593 * the line iff the return value is PARSE_STRING_SUCCESS.
595 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
596 * parsed string token will be returned here.
598 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
599 * PARSE_STRING_FAILURE if the data was invalid due to a missing
600 * closing quote; or PARSE_STRING_NONE if the line ended before the
601 * beginning of a string token was found.
604 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
607 tchar *line = *line_p;
611 /* Skip leading whitespace */
614 return PARSE_STRING_NONE;
615 if (!istspace(*line) && *line != T('\0'))
621 if (quote_char == T('"') || quote_char == T('\'')) {
626 line = tmemchr(line, quote_char, len);
628 imagex_error(T("Missing closing quote: %"TS), fn - 1);
629 return PARSE_STRING_FAILURE;
632 /* Unquoted string. Go until whitespace. Line is terminated
633 * by '\0', so no need to check 'len'. */
637 } while (!istspace(*line) && *line != T('\0'));
644 return PARSE_STRING_SUCCESS;
647 /* Parses a line of data (not an empty line or comment) in the source list file
648 * format. (See the man page for 'wimlib-imagex capture' for details on this
649 * format and the meaning.)
651 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
652 * len == 0. The data in @line will be modified by this function call.
654 * @len: Length of the line of data.
656 * @source: On success, the capture source and target described by the line is
657 * written into this destination. Note that it will contain pointers
658 * to data in the @line array.
660 * Returns true if the line was valid; false otherwise. */
662 parse_source_list_line(tchar *line, size_t len,
663 struct wimlib_capture_source *source)
667 ret = parse_string(&line, &len, &source->fs_source_path);
668 if (ret != PARSE_STRING_SUCCESS)
670 ret = parse_string(&line, &len, &source->wim_target_path);
671 if (ret == PARSE_STRING_NONE)
672 source->wim_target_path = source->fs_source_path;
673 return ret != PARSE_STRING_FAILURE;
676 /* Returns %true if the given line of length @len > 0 is a comment or empty line
677 * in the source list file format. */
679 is_comment_line(const tchar *line, size_t len)
684 if (!istspace(*line) && *line != T('\0'))
694 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
697 tchar *contents = *contents_p;
698 size_t nchars = *nchars_p;
701 for (i = 0; i < nchars; i++)
702 if (contents[i] == T('\n'))
705 /* Handle last line not terminated by a newline */
706 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
707 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
709 imagex_error(T("Out of memory!"));
712 contents[nchars] = T('\n');
713 *contents_p = contents;
721 /* Parses a file in the source list format. (See the man page for
722 * 'wimlib-imagex capture' for details on this format and the meaning.)
724 * @source_list_contents: Contents of the source list file. Note that this
725 * buffer will be modified to save memory allocations,
726 * and cannot be freed until the returned array of
727 * wimlib_capture_source's has also been freed.
729 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
732 * @nsources_ret: On success, the length of the returned array is
735 * Returns: An array of `struct wimlib_capture_source's that can be passed to
736 * the wimlib_add_image_multisource() function to specify how a WIM image is to
738 static struct wimlib_capture_source *
739 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
740 size_t *nsources_ret)
744 struct wimlib_capture_source *sources;
747 nlines = text_file_count_lines(source_list_contents_p,
748 &source_list_nchars);
752 /* Always allocate at least 1 slot, just in case the implementation of
753 * calloc() returns NULL if 0 bytes are requested. */
754 sources = calloc(nlines ?: 1, sizeof(*sources));
756 imagex_error(T("out of memory"));
759 p = *source_list_contents_p;
761 for (i = 0; i < nlines; i++) {
762 /* XXX: Could use rawmemchr() here instead, but it may not be
763 * available on all platforms. */
764 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
765 size_t len = endp - p + 1;
767 if (!is_comment_line(p, len)) {
768 if (!parse_source_list_line(p, len, &sources[j++])) {
781 enum capture_config_section {
782 CAPTURE_CONFIG_NO_SECTION,
783 CAPTURE_CONFIG_EXCLUSION_SECTION,
784 CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
785 CAPTURE_CONFIG_IGNORE_SECTION,
789 CAPTURE_CONFIG_INVALID_SECTION,
790 CAPTURE_CONFIG_CHANGED_SECTION,
791 CAPTURE_CONFIG_SAME_SECTION,
795 check_config_section(tchar *line, size_t len,
796 enum capture_config_section *cur_section)
798 while (istspace(*line))
802 return CAPTURE_CONFIG_SAME_SECTION;
805 tchar *endbrace = tstrrchr(line, T(']'));
807 return CAPTURE_CONFIG_SAME_SECTION;
809 if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
810 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
811 } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
812 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
813 } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
814 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
815 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
816 "of capture config file\n"),
818 } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
819 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
820 tfputs(T("WARNING: Ignoring [AlignmentList] section "
821 "of capture config file\n"),
824 imagex_error(T("Invalid capture config file section \"%"TS"\""),
826 return CAPTURE_CONFIG_INVALID_SECTION;
828 return CAPTURE_CONFIG_CHANGED_SECTION;
833 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
836 if (pat_list->num_pats == pat_list->num_allocated_pats) {
838 size_t num_allocated_pats = pat_list->num_pats + 8;
840 pats = realloc(pat_list->pats,
841 num_allocated_pats * sizeof(pat_list->pats[0]));
843 imagex_error(T("Out of memory!"));
846 pat_list->pats = pats;
847 pat_list->num_allocated_pats = num_allocated_pats;
849 pat_list->pats[pat_list->num_pats++] = pat;
854 parse_capture_config_line(tchar *line, size_t len,
855 enum capture_config_section *cur_section,
856 struct wimlib_capture_config *config)
861 ret = check_config_section(line, len, cur_section);
862 if (ret == CAPTURE_CONFIG_INVALID_SECTION)
864 if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
867 switch (*cur_section) {
868 case CAPTURE_CONFIG_NO_SECTION:
869 imagex_error(T("Line \"%"TS"\" is not in a section "
870 "(such as [ExclusionList]"), line);
872 case CAPTURE_CONFIG_EXCLUSION_SECTION:
873 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
875 return pattern_list_add_pattern(&config->exclusion_pats,
877 case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
878 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
880 return pattern_list_add_pattern(&config->exclusion_exception_pats,
882 case CAPTURE_CONFIG_IGNORE_SECTION:
889 parse_capture_config(tchar **contents_p, size_t nchars,
890 struct wimlib_capture_config *config)
895 enum capture_config_section cur_section;
897 memset(config, 0, sizeof(*config));
899 nlines = text_file_count_lines(contents_p, &nchars);
903 cur_section = CAPTURE_CONFIG_NO_SECTION;
905 for (i = 0; i < nlines; i++) {
906 tchar *endp = tmemchr(p, T('\n'), nchars);
907 size_t len = endp - p + 1;
909 if (!is_comment_line(p, len))
910 if (!parse_capture_config_line(p, len, &cur_section, config))
918 /* Reads the contents of a file into memory. */
920 file_get_contents(const tchar *filename, size_t *len_ret)
927 if (tstat(filename, &stbuf) != 0) {
928 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
933 fp = tfopen(filename, T("rb"));
935 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
939 buf = malloc(len ? len : 1);
941 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
942 "contents of file \"%"TS"\""), len, filename);
945 if (fread(buf, 1, len, fp) != len) {
946 imagex_error_with_errno(T("Failed to read %zu bytes from the "
947 "file \"%"TS"\""), len, filename);
961 /* Read standard input until EOF and return the full contents in a malloc()ed
962 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
965 stdin_get_contents(size_t *len_ret)
967 /* stdin can, of course, be a pipe or other non-seekable file, so the
968 * total length of the data cannot be pre-determined */
970 size_t newlen = 1024;
974 char *p = realloc(buf, newlen);
975 size_t bytes_read, bytes_to_read;
977 imagex_error(T("out of memory while reading stdin"));
981 bytes_to_read = newlen - pos;
982 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
984 if (bytes_read != bytes_to_read) {
989 imagex_error_with_errno(T("error reading stdin"));
1003 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
1006 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
1008 *num_tchars_ret = num_bytes;
1010 #else /* !__WIN32__ */
1011 /* On Windows, translate the text to UTF-16LE */
1015 if (num_bytes >= 2 &&
1016 (((unsigned char)text[0] == 0xff && (unsigned char)text[1] == 0xfe) ||
1017 ((unsigned char)text[0] <= 0x7f && (unsigned char)text[1] == 0x00)))
1019 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
1020 * with something that looks like an ASCII character encoded as
1021 * a UTF-16LE code unit. Assume the file is encoded as
1022 * UTF-16LE. This is not a 100% reliable check. */
1023 num_wchars = num_bytes / 2;
1024 text_wstr = (wchar_t*)text;
1026 /* File does not look like UTF-16LE. Assume it is encoded in
1027 * the current Windows code page. I think these are always
1028 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
1029 * should work as expected. */
1030 text_wstr = win32_mbs_to_wcs(text,
1035 *num_tchars_ret = num_wchars;
1037 #endif /* __WIN32__ */
1041 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
1046 contents = file_get_contents(filename, &num_bytes);
1049 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
1053 stdin_get_text_contents(size_t *num_tchars_ret)
1058 contents = stdin_get_contents(&num_bytes);
1061 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
1064 #define TO_PERCENT(numerator, denominator) \
1065 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
1067 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
1068 #define MEBIBYTE_MIN_NBYTES 10000000ULL
1069 #define KIBIBYTE_MIN_NBYTES 10000ULL
1072 get_unit(uint64_t total_bytes, const tchar **name_ret)
1074 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
1075 *name_ret = T("GiB");
1077 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
1078 *name_ret = T("MiB");
1080 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
1081 *name_ret = T("KiB");
1084 *name_ret = T("bytes");
1089 static struct wimlib_progress_info_scan last_scan_progress;
1092 report_scan_progress(const struct wimlib_progress_info_scan *scan, bool done)
1094 uint64_t prev_count, cur_count;
1096 prev_count = last_scan_progress.num_nondirs_scanned +
1097 last_scan_progress.num_dirs_scanned;
1098 cur_count = scan->num_nondirs_scanned + scan->num_dirs_scanned;
1100 if (done || prev_count == 0 || cur_count >= prev_count + 100 ||
1101 cur_count % 128 == 0)
1103 unsigned unit_shift;
1104 const tchar *unit_name;
1106 unit_shift = get_unit(scan->num_bytes_scanned, &unit_name);
1107 imagex_printf(T("\r%"PRIu64" %"TS" scanned (%"PRIu64" files, "
1108 "%"PRIu64" directories) "),
1109 scan->num_bytes_scanned >> unit_shift,
1111 scan->num_nondirs_scanned,
1112 scan->num_dirs_scanned);
1113 last_scan_progress = *scan;
1117 /* Progress callback function passed to various wimlib functions. */
1119 imagex_progress_func(enum wimlib_progress_msg msg,
1120 const union wimlib_progress_info *info)
1122 unsigned percent_done;
1123 unsigned unit_shift;
1124 const tchar *unit_name;
1126 if (imagex_be_quiet)
1129 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1131 static bool first = true;
1133 imagex_printf(T("Writing %"TS"-compressed data "
1134 "using %u thread%"TS"\n"),
1135 wimlib_get_compression_type_string(
1136 info->write_streams.compression_type),
1137 info->write_streams.num_threads,
1138 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1142 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1143 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1144 info->write_streams.total_bytes);
1146 if (info->write_streams.total_parts <= 1) {
1147 imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1148 "written (%u%% done)"),
1149 info->write_streams.completed_bytes >> unit_shift,
1151 info->write_streams.total_bytes >> unit_shift,
1155 imagex_printf(T("\rWriting resources from part %u of %u: "
1156 "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written"),
1157 (info->write_streams.completed_parts ==
1158 info->write_streams.total_parts) ?
1159 info->write_streams.completed_parts :
1160 info->write_streams.completed_parts + 1,
1161 info->write_streams.total_parts,
1162 info->write_streams.completed_bytes >> unit_shift,
1164 info->write_streams.total_bytes >> unit_shift,
1168 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1169 imagex_printf(T("\n"));
1171 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1172 imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
1173 if (*info->scan.wim_target_path) {
1174 imagex_printf(T(" (loading as WIM path: "
1175 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
1176 info->scan.wim_target_path);
1178 imagex_printf(T("\n"));
1180 memset(&last_scan_progress, 0, sizeof(last_scan_progress));
1182 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1183 switch (info->scan.status) {
1184 case WIMLIB_SCAN_DENTRY_OK:
1185 report_scan_progress(&info->scan, false);
1187 case WIMLIB_SCAN_DENTRY_EXCLUDED:
1188 imagex_printf(T("\nExcluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1190 case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
1191 imagex_printf(T("\nWARNING: Excluding unsupported file or directory\n"
1192 " \"%"TS"\" from capture\n"), info->scan.cur_path);
1194 case WIMLIB_SCAN_DENTRY_EXCLUDED_SYMLINK:
1195 imagex_printf(T("\nWARNING: Ignoring absolute symbolic link "
1196 "with out-of-tree target:\n"
1197 " \"%"TS"\" => \"%"TS"\"\n"
1198 " (Use --norpfix to capture "
1199 "absolute symbolic links as-is)\n"),
1200 info->scan.cur_path, info->scan.symlink_target);
1204 case WIMLIB_PROGRESS_MSG_SCAN_END:
1205 report_scan_progress(&info->scan, true);
1206 imagex_printf(T("\n"));
1208 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1209 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1210 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1211 info->integrity.total_bytes);
1212 imagex_printf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1213 "of %"PRIu64" %"TS" (%u%%) done"),
1214 info->integrity.filename,
1215 info->integrity.completed_bytes >> unit_shift,
1217 info->integrity.total_bytes >> unit_shift,
1220 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1221 imagex_printf(T("\n"));
1223 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1224 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1225 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1226 info->integrity.total_bytes);
1227 imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1228 "of %"PRIu64" %"TS" (%u%%) done"),
1229 info->integrity.completed_bytes >> unit_shift,
1231 info->integrity.total_bytes >> unit_shift,
1234 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1235 imagex_printf(T("\n"));
1237 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1238 imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1239 "to %"TS" \"%"TS"\"\n"),
1240 info->extract.image,
1241 info->extract.image_name,
1242 info->extract.wimfile_name,
1243 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1244 T("NTFS volume") : T("directory")),
1245 info->extract.target);
1247 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1248 if (info->extract.extract_root_wim_source_path[0] != T('\0')) {
1249 imagex_printf(T("Extracting "
1250 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\" "
1251 "from image %d (\"%"TS"\") "
1252 "in \"%"TS"\" to \"%"TS"\"\n"),
1253 info->extract.extract_root_wim_source_path,
1254 info->extract.image,
1255 info->extract.image_name,
1256 info->extract.wimfile_name,
1257 info->extract.target);
1260 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1261 percent_done = TO_PERCENT(info->extract.completed_bytes,
1262 info->extract.total_bytes);
1263 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1264 imagex_printf(T("\rExtracting files: "
1265 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1266 info->extract.completed_bytes >> unit_shift,
1268 info->extract.total_bytes >> unit_shift,
1271 if (info->extract.completed_bytes >= info->extract.total_bytes)
1272 imagex_printf(T("\n"));
1274 case WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN:
1275 if (info->extract.total_parts != 1) {
1276 imagex_printf(T("\nReading split pipable WIM part %u of %u\n"),
1277 info->extract.part_number,
1278 info->extract.total_parts);
1281 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1282 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1283 imagex_printf(T("Setting timestamps on all extracted files...\n"));
1285 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1286 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1287 imagex_printf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1288 info->extract.target);
1291 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1292 percent_done = TO_PERCENT(info->split.completed_bytes,
1293 info->split.total_bytes);
1294 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1295 imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
1296 "%"PRIu64" %"TS" (%u%%) written\n"),
1297 info->split.part_name,
1298 info->split.cur_part_number,
1299 info->split.total_parts,
1300 info->split.completed_bytes >> unit_shift,
1302 info->split.total_bytes >> unit_shift,
1306 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1307 if (info->split.completed_bytes == info->split.total_bytes) {
1308 imagex_printf(T("Finished writing split WIM part %u of %u\n"),
1309 info->split.cur_part_number,
1310 info->split.total_parts);
1313 case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1314 switch (info->update.command->op) {
1315 case WIMLIB_UPDATE_OP_DELETE:
1316 imagex_printf(T("Deleted WIM path "
1317 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1318 info->update.command->delete_.wim_path);
1320 case WIMLIB_UPDATE_OP_RENAME:
1321 imagex_printf(T("Renamed WIM path "
1322 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
1323 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1324 info->update.command->rename.wim_source_path,
1325 info->update.command->rename.wim_target_path);
1327 case WIMLIB_UPDATE_OP_ADD:
1335 fflush(imagex_info_file);
1340 parse_num_threads(const tchar *optarg)
1343 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1344 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1345 imagex_error(T("Number of threads must be a non-negative integer!"));
1352 static uint32_t parse_chunk_size(const tchar *optarg)
1355 unsigned long chunk_size = tstrtoul(optarg, &tmp, 10);
1356 if (chunk_size >= UINT32_MAX || *tmp || tmp == optarg) {
1357 imagex_error(T("Chunk size must be a non-negative integer!"));
1366 * Parse an option passed to an update command.
1368 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1371 * @option: Text string for the option (beginning with --)
1373 * @cmd: `struct wimlib_update_command' that is being constructed for
1376 * Returns true if the option was recognized; false if not.
1379 update_command_add_option(int op, const tchar *option,
1380 struct wimlib_update_command *cmd)
1382 bool recognized = true;
1384 case WIMLIB_UPDATE_OP_ADD:
1385 if (!tstrcmp(option, T("--verbose")))
1386 cmd->add.add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
1387 else if (!tstrcmp(option, T("--unix-data")))
1388 cmd->add.add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
1389 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1390 cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
1391 else if (!tstrcmp(option, T("--strict-acls")))
1392 cmd->add.add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
1393 else if (!tstrcmp(option, T("--dereference")))
1394 cmd->add.add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
1398 case WIMLIB_UPDATE_OP_DELETE:
1399 if (!tstrcmp(option, T("--force")))
1400 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1401 else if (!tstrcmp(option, T("--recursive")))
1402 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1413 /* How many nonoption arguments each `imagex update' command expects */
1414 static const unsigned update_command_num_nonoptions[] = {
1415 [WIMLIB_UPDATE_OP_ADD] = 2,
1416 [WIMLIB_UPDATE_OP_DELETE] = 1,
1417 [WIMLIB_UPDATE_OP_RENAME] = 2,
1421 update_command_add_nonoption(int op, const tchar *nonoption,
1422 struct wimlib_update_command *cmd,
1423 unsigned num_nonoptions)
1426 case WIMLIB_UPDATE_OP_ADD:
1427 if (num_nonoptions == 0)
1428 cmd->add.fs_source_path = (tchar*)nonoption;
1430 cmd->add.wim_target_path = (tchar*)nonoption;
1432 case WIMLIB_UPDATE_OP_DELETE:
1433 cmd->delete_.wim_path = (tchar*)nonoption;
1435 case WIMLIB_UPDATE_OP_RENAME:
1436 if (num_nonoptions == 0)
1437 cmd->rename.wim_source_path = (tchar*)nonoption;
1439 cmd->rename.wim_target_path = (tchar*)nonoption;
1445 * Parse a command passed on stdin to `imagex update'.
1447 * @line: Text of the command.
1448 * @len: Length of the line, including a null terminator
1451 * @command: A `struct wimlib_update_command' to fill in from the parsed
1454 * @line_number: Line number of the command, for diagnostics.
1456 * Returns true on success; returns false on parse error.
1459 parse_update_command(tchar *line, size_t len,
1460 struct wimlib_update_command *command,
1464 tchar *command_name;
1466 size_t num_nonoptions;
1468 /* Get the command name ("add", "delete", "rename") */
1469 ret = parse_string(&line, &len, &command_name);
1470 if (ret != PARSE_STRING_SUCCESS)
1473 if (!tstrcasecmp(command_name, T("add"))) {
1474 op = WIMLIB_UPDATE_OP_ADD;
1475 } else if (!tstrcasecmp(command_name, T("delete"))) {
1476 op = WIMLIB_UPDATE_OP_DELETE;
1477 } else if (!tstrcasecmp(command_name, T("rename"))) {
1478 op = WIMLIB_UPDATE_OP_RENAME;
1480 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1481 command_name, line_number);
1486 /* Parse additional options and non-options as needed */
1491 ret = parse_string(&line, &len, &next_string);
1492 if (ret == PARSE_STRING_NONE) /* End of line */
1494 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1496 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1498 if (!update_command_add_option(op, next_string, command))
1500 imagex_error(T("Unrecognized option \"%"TS"\" to "
1501 "update command \"%"TS"\" on line %zu"),
1502 next_string, command_name, line_number);
1508 if (num_nonoptions == update_command_num_nonoptions[op])
1510 imagex_error(T("Unexpected argument \"%"TS"\" in "
1511 "update command on line %zu\n"
1512 " (The \"%"TS"\" command only "
1513 "takes %zu nonoption arguments!)\n"),
1514 next_string, line_number,
1515 command_name, num_nonoptions);
1518 update_command_add_nonoption(op, next_string,
1519 command, num_nonoptions);
1524 if (num_nonoptions != update_command_num_nonoptions[op]) {
1525 imagex_error(T("Not enough arguments to update command "
1526 "\"%"TS"\" on line %zu"), command_name, line_number);
1532 static struct wimlib_update_command *
1533 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1534 size_t *num_cmds_ret)
1538 struct wimlib_update_command *cmds;
1541 nlines = text_file_count_lines(cmd_file_contents_p,
1546 /* Always allocate at least 1 slot, just in case the implementation of
1547 * calloc() returns NULL if 0 bytes are requested. */
1548 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1550 imagex_error(T("out of memory"));
1553 p = *cmd_file_contents_p;
1555 for (i = 0; i < nlines; i++) {
1556 /* XXX: Could use rawmemchr() here instead, but it may not be
1557 * available on all platforms. */
1558 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1559 size_t len = endp - p + 1;
1561 if (!is_comment_line(p, len)) {
1562 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1573 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1574 * one image from a WIM file to a NTFS volume. */
1576 imagex_apply(int argc, tchar **argv, int cmd)
1580 int image = WIMLIB_NO_IMAGE;
1582 struct wimlib_wim_info info;
1584 const tchar *wimfile;
1585 const tchar *target;
1586 const tchar *image_num_or_name = NULL;
1587 int extract_flags = 0;
1589 STRING_SET(refglobs);
1591 for_opt(c, apply_options) {
1593 case IMAGEX_CHECK_OPTION:
1594 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1596 case IMAGEX_HARDLINK_OPTION:
1597 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1599 case IMAGEX_SYMLINK_OPTION:
1600 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1602 case IMAGEX_VERBOSE_OPTION:
1603 /* No longer does anything. */
1605 case IMAGEX_REF_OPTION:
1606 ret = string_set_append(&refglobs, optarg);
1608 goto out_free_refglobs;
1610 case IMAGEX_UNIX_DATA_OPTION:
1611 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1613 case IMAGEX_NO_ACLS_OPTION:
1614 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1616 case IMAGEX_STRICT_ACLS_OPTION:
1617 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1619 case IMAGEX_NORPFIX_OPTION:
1620 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1622 case IMAGEX_RPFIX_OPTION:
1623 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1625 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1626 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1627 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1629 case IMAGEX_RESUME_OPTION:
1630 extract_flags |= WIMLIB_EXTRACT_FLAG_RESUME;
1638 if (argc != 2 && argc != 3)
1643 if (!tstrcmp(wimfile, T("-"))) {
1644 /* Attempt to apply pipable WIM from standard input. */
1646 image_num_or_name = NULL;
1649 image_num_or_name = argv[1];
1654 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1655 imagex_progress_func);
1657 goto out_free_refglobs;
1659 wimlib_get_wim_info(wim, &info);
1662 /* Image explicitly specified. */
1663 image_num_or_name = argv[1];
1664 image = wimlib_resolve_image(wim, image_num_or_name);
1665 ret = verify_image_exists(image, image_num_or_name, wimfile);
1667 goto out_wimlib_free;
1670 /* No image specified; default to image 1, but only if the WIM
1671 * contains exactly one image. */
1673 if (info.image_count != 1) {
1674 imagex_error(T("\"%"TS"\" contains %d images; "
1675 "Please select one (or all)."),
1676 wimfile, info.image_count);
1685 if (refglobs.num_strings) {
1687 imagex_error(T("Can't specify --ref when applying from stdin!"));
1689 goto out_wimlib_free;
1691 ret = wim_reference_globs(wim, &refglobs, open_flags);
1693 goto out_wimlib_free;
1698 /* Interpret a regular file or block device target as a NTFS
1702 if (tstat(target, &stbuf)) {
1703 if (errno != ENOENT) {
1704 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1707 goto out_wimlib_free;
1710 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1711 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1717 ret = wimlib_extract_image(wim, image, target, extract_flags,
1718 imagex_progress_func);
1720 set_fd_to_binary_mode(STDIN_FILENO);
1721 ret = wimlib_extract_image_from_pipe(STDIN_FILENO,
1723 target, extract_flags,
1724 imagex_progress_func);
1727 imagex_printf(T("Done applying WIM image.\n"));
1728 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
1730 do_resource_not_found_warning(wimfile, &info, &refglobs);
1732 imagex_error(T( "If you are applying an image "
1733 "from a split pipable WIM,\n"
1734 " make sure you have "
1735 "concatenated together all parts."));
1741 string_set_destroy(&refglobs);
1745 usage(CMD_APPLY, stderr);
1747 goto out_free_refglobs;
1750 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1751 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1752 * the desired image. 'wimlib-imagex append': add a new image to an existing
1755 imagex_capture_or_append(int argc, tchar **argv, int cmd)
1758 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1759 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE |
1760 WIMLIB_ADD_IMAGE_FLAG_WINCONFIG |
1761 WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1762 int write_flags = 0;
1763 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
1764 uint32_t chunk_size = UINT32_MAX;
1765 uint32_t pack_chunk_size = UINT32_MAX;
1766 const tchar *wimfile;
1770 const tchar *flags_element = NULL;
1773 STRING_SET(base_wimfiles);
1774 WIMStruct **base_wims;
1776 WIMStruct *template_wim;
1777 const tchar *template_wimfile = NULL;
1778 const tchar *template_image_name_or_num = NULL;
1779 int template_image = WIMLIB_NO_IMAGE;
1782 unsigned num_threads = 0;
1787 const tchar *config_file = NULL;
1789 struct wimlib_capture_config *config;
1791 bool source_list = false;
1792 size_t source_list_nchars = 0;
1793 tchar *source_list_contents;
1794 bool capture_sources_malloced;
1795 struct wimlib_capture_source *capture_sources;
1797 bool name_defaulted;
1798 bool compress_slow = false;
1800 for_opt(c, capture_or_append_options) {
1802 case IMAGEX_BOOT_OPTION:
1803 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1805 case IMAGEX_CHECK_OPTION:
1806 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1807 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1809 case IMAGEX_NOCHECK_OPTION:
1810 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
1812 case IMAGEX_CONFIG_OPTION:
1813 config_file = optarg;
1814 add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1816 case IMAGEX_COMPRESS_OPTION:
1817 compression_type = get_compression_type(optarg);
1818 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1821 case IMAGEX_COMPRESS_SLOW_OPTION:
1822 compress_slow = true;
1824 case IMAGEX_CHUNK_SIZE_OPTION:
1825 chunk_size = parse_chunk_size(optarg);
1826 if (chunk_size == UINT32_MAX)
1829 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
1830 pack_chunk_size = parse_chunk_size(optarg);
1831 if (pack_chunk_size == UINT32_MAX)
1834 case IMAGEX_PACK_STREAMS_OPTION:
1835 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
1837 case IMAGEX_FLAGS_OPTION:
1838 flags_element = optarg;
1840 case IMAGEX_DEREFERENCE_OPTION:
1841 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1843 case IMAGEX_VERBOSE_OPTION:
1844 /* No longer does anything. */
1846 case IMAGEX_THREADS_OPTION:
1847 num_threads = parse_num_threads(optarg);
1848 if (num_threads == UINT_MAX)
1851 case IMAGEX_REBUILD_OPTION:
1852 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1854 case IMAGEX_UNIX_DATA_OPTION:
1855 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1857 case IMAGEX_SOURCE_LIST_OPTION:
1860 case IMAGEX_NO_ACLS_OPTION:
1861 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1863 case IMAGEX_STRICT_ACLS_OPTION:
1864 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1866 case IMAGEX_RPFIX_OPTION:
1867 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1869 case IMAGEX_NORPFIX_OPTION:
1870 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1872 case IMAGEX_PIPABLE_OPTION:
1873 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1875 case IMAGEX_NOT_PIPABLE_OPTION:
1876 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
1878 case IMAGEX_UPDATE_OF_OPTION:
1879 if (template_image_name_or_num) {
1880 imagex_error(T("'--update-of' can only be "
1881 "specified one time!"));
1885 colon = tstrrchr(optarg, T(':'));
1888 template_wimfile = optarg;
1890 template_image_name_or_num = colon + 1;
1892 template_wimfile = NULL;
1893 template_image_name_or_num = optarg;
1897 case IMAGEX_DELTA_FROM_OPTION:
1898 if (cmd != CMD_CAPTURE) {
1899 imagex_error(T("'--delta-from' is only "
1900 "valid for capture!"));
1903 ret = string_set_append(&base_wimfiles, optarg);
1905 goto out_free_base_wimfiles;
1906 write_flags |= WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS;
1915 if (argc < 2 || argc > 4)
1921 /* Set default compression type and parameters. */
1924 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
1925 compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1927 if (!compress_slow) {
1928 struct wimlib_lzx_compressor_params params = {
1929 .hdr.size = sizeof(params),
1930 .algorithm = WIMLIB_LZX_ALGORITHM_FAST,
1933 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
1939 set_compress_slow();
1941 if (!tstrcmp(wimfile, T("-"))) {
1942 /* Writing captured WIM to standard output. */
1944 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
1945 imagex_error("Can't write a non-pipable WIM to "
1946 "standard output! Specify --pipable\n"
1947 " if you want to create a pipable WIM "
1948 "(but read the docs first).");
1952 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1954 if (cmd == CMD_APPEND) {
1955 imagex_error(T("Using standard output for append does "
1956 "not make sense."));
1959 wim_fd = STDOUT_FILENO;
1961 imagex_info_file = stderr;
1962 set_fd_to_binary_mode(wim_fd);
1965 /* If template image was specified using --update-of=IMAGE rather
1966 * than --update-of=WIMFILE:IMAGE, set the default WIMFILE. */
1967 if (template_image_name_or_num && !template_wimfile) {
1968 if (base_wimfiles.num_strings == 1) {
1969 /* Capturing delta WIM based on single WIM: default to
1971 template_wimfile = base_wimfiles.strings[0];
1972 } else if (cmd == CMD_APPEND) {
1973 /* Appending to WIM: default to WIM being appended to.
1975 template_wimfile = wimfile;
1977 /* Capturing a normal (non-delta) WIM, so the WIM file
1978 * *must* be explicitly specified. */
1979 if (base_wimfiles.num_strings > 1) {
1980 imagex_error(T("For capture of delta WIM "
1981 "based on multiple existing "
1983 " '--update-of' must "
1984 "specify WIMFILE:IMAGE!"));
1986 imagex_error(T("For capture of non-delta WIM, "
1987 "'--update-of' must specify "
1996 name_defaulted = false;
1998 /* Set default name to SOURCE argument, omitting any directory
1999 * prefixes and trailing slashes. This requires making a copy
2000 * of @source. Leave some free characters at the end in case we
2001 * append a number to keep the name unique. */
2002 size_t source_name_len;
2004 source_name_len = tstrlen(source);
2005 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
2006 name = tbasename(tstrcpy(source_copy, source));
2007 name_defaulted = true;
2009 /* Image description defaults to NULL if not given. */
2016 /* Set up capture sources in source list mode */
2017 if (source[0] == T('-') && source[1] == T('\0')) {
2018 source_list_contents = stdin_get_text_contents(&source_list_nchars);
2020 source_list_contents = file_get_text_contents(source,
2021 &source_list_nchars);
2023 if (!source_list_contents)
2026 capture_sources = parse_source_list(&source_list_contents,
2029 if (!capture_sources) {
2031 goto out_free_source_list_contents;
2033 capture_sources_malloced = true;
2035 /* Set up capture source in non-source-list mode. */
2036 capture_sources = alloca(sizeof(struct wimlib_capture_source));
2037 capture_sources[0].fs_source_path = source;
2038 capture_sources[0].wim_target_path = NULL;
2039 capture_sources[0].reserved = 0;
2041 capture_sources_malloced = false;
2042 source_list_contents = NULL;
2046 /* Read and parse capture configuration file. */
2049 config_str = file_get_text_contents(config_file, &config_len);
2052 goto out_free_capture_sources;
2055 config = alloca(sizeof(*config));
2056 ret = parse_capture_config(&config_str, config_len, config);
2058 goto out_free_config;
2060 /* No capture configuration file specified; use default
2061 * configuration for capturing Windows operating systems. */
2063 add_image_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
2066 /* Open the existing WIM, or create a new one. */
2067 if (cmd == CMD_APPEND)
2068 ret = wimlib_open_wim(wimfile, open_flags, &wim,
2069 imagex_progress_func);
2071 ret = wimlib_create_new_wim(compression_type, &wim);
2073 goto out_free_config;
2075 /* Set chunk size if non-default. */
2076 if (chunk_size != UINT32_MAX) {
2077 ret = wimlib_set_output_chunk_size(wim, chunk_size);
2081 if (pack_chunk_size != UINT32_MAX) {
2082 ret = wimlib_set_output_pack_chunk_size(wim, pack_chunk_size);
2088 /* Detect if source is regular file or block device and set NTFS volume
2093 if (tstat(source, &stbuf) == 0) {
2094 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
2095 imagex_printf(T("Capturing WIM image from NTFS "
2096 "filesystem on \"%"TS"\"\n"), source);
2097 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
2100 if (errno != ENOENT) {
2101 imagex_error_with_errno(T("Failed to stat "
2102 "\"%"TS"\""), source);
2110 /* If the user did not specify an image name, and the basename of the
2111 * source already exists as an image name in the WIM file, append a
2112 * suffix to make it unique. */
2113 if (cmd == CMD_APPEND && name_defaulted) {
2114 unsigned long conflict_idx;
2115 tchar *name_end = tstrchr(name, T('\0'));
2116 for (conflict_idx = 1;
2117 wimlib_image_name_in_use(wim, name);
2120 tsprintf(name_end, T(" (%lu)"), conflict_idx);
2124 /* If capturing a delta WIM, reference resources from the base WIMs
2125 * before adding the new image. */
2126 if (base_wimfiles.num_strings) {
2127 base_wims = calloc(base_wimfiles.num_strings,
2128 sizeof(base_wims[0]));
2129 if (base_wims == NULL) {
2130 imagex_error(T("Out of memory!"));
2135 for (size_t i = 0; i < base_wimfiles.num_strings; i++) {
2136 ret = wimlib_open_wim(base_wimfiles.strings[i],
2137 open_flags, &base_wims[i],
2138 imagex_progress_func);
2140 goto out_free_base_wims;
2144 ret = wimlib_reference_resources(wim, base_wims,
2145 base_wimfiles.num_strings, 0);
2147 goto out_free_base_wims;
2149 if (base_wimfiles.num_strings == 1) {
2150 imagex_printf(T("Capturing delta WIM based on \"%"TS"\"\n"),
2151 base_wimfiles.strings[0]);
2153 imagex_printf(T("Capturing delta WIM based on %u WIMs\n"),
2154 base_wimfiles.num_strings);
2161 /* If capturing or appending as an update of an existing (template) image,
2162 * open the WIM if needed and parse the image index. */
2163 if (template_image_name_or_num) {
2166 if (base_wimfiles.num_strings == 1 &&
2167 template_wimfile == base_wimfiles.strings[0]) {
2168 template_wim = base_wims[0];
2169 } else if (template_wimfile == wimfile) {
2172 ret = wimlib_open_wim(template_wimfile, open_flags,
2173 &template_wim, imagex_progress_func);
2175 goto out_free_base_wims;
2178 template_image = wimlib_resolve_image(template_wim,
2179 template_image_name_or_num);
2181 if (template_image_name_or_num[0] == T('-')) {
2184 struct wimlib_wim_info info;
2186 wimlib_get_wim_info(template_wim, &info);
2187 n = tstrtoul(template_image_name_or_num + 1, &tmp, 10);
2188 if (n >= 1 && n <= info.image_count &&
2190 tmp != template_image_name_or_num + 1)
2192 template_image = info.image_count - (n - 1);
2195 ret = verify_image_exists_and_is_single(template_image,
2196 template_image_name_or_num,
2199 goto out_free_template_wim;
2201 template_wim = NULL;
2204 ret = wimlib_add_image_multisource(wim,
2210 imagex_progress_func);
2212 goto out_free_template_wim;
2214 if (desc || flags_element || template_image_name_or_num) {
2215 /* User provided <DESCRIPTION> or <FLAGS> element, or an image
2216 * on which the added one is to be based has been specified with
2217 * --update-of. Get the index of the image we just
2218 * added, then use it to call the appropriate functions. */
2219 struct wimlib_wim_info info;
2221 wimlib_get_wim_info(wim, &info);
2224 ret = wimlib_set_image_descripton(wim,
2228 goto out_free_template_wim;
2231 if (flags_element) {
2232 ret = wimlib_set_image_flags(wim, info.image_count,
2235 goto out_free_template_wim;
2238 /* Reference template image if the user provided one. */
2239 if (template_image_name_or_num) {
2240 imagex_printf(T("Using image %d "
2241 "from \"%"TS"\" as template\n"),
2242 template_image, template_wimfile);
2243 ret = wimlib_reference_template_image(wim,
2249 goto out_free_template_wim;
2253 /* Write the new WIM or overwrite the existing WIM with the new image
2255 if (cmd == CMD_APPEND) {
2256 ret = wimlib_overwrite(wim, write_flags, num_threads,
2257 imagex_progress_func);
2258 } else if (wimfile) {
2259 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
2260 write_flags, num_threads,
2261 imagex_progress_func);
2263 ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
2264 write_flags, num_threads,
2265 imagex_progress_func);
2267 out_free_template_wim:
2268 /* template_wim may alias base_wims[0] or wim. */
2269 if ((base_wimfiles.num_strings != 1 || template_wim != base_wims[0]) &&
2270 template_wim != wim)
2271 wimlib_free(template_wim);
2273 for (size_t i = 0; i < base_wimfiles.num_strings; i++)
2274 wimlib_free(base_wims[i]);
2280 free(config->exclusion_pats.pats);
2281 free(config->exclusion_exception_pats.pats);
2284 out_free_capture_sources:
2285 if (capture_sources_malloced)
2286 free(capture_sources);
2287 out_free_source_list_contents:
2288 free(source_list_contents);
2289 out_free_base_wimfiles:
2290 string_set_destroy(&base_wimfiles);
2297 goto out_free_base_wimfiles;
2300 /* Remove image(s) from a WIM. */
2302 imagex_delete(int argc, tchar **argv, int cmd)
2305 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2306 int write_flags = 0;
2307 const tchar *wimfile;
2308 const tchar *image_num_or_name;
2313 for_opt(c, delete_options) {
2315 case IMAGEX_CHECK_OPTION:
2316 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2317 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2319 case IMAGEX_SOFT_OPTION:
2320 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
2331 imagex_error(T("Must specify a WIM file"));
2333 imagex_error(T("Must specify an image"));
2337 image_num_or_name = argv[1];
2339 ret = wimlib_open_wim(wimfile, open_flags, &wim,
2340 imagex_progress_func);
2344 image = wimlib_resolve_image(wim, image_num_or_name);
2346 ret = verify_image_exists(image, image_num_or_name, wimfile);
2348 goto out_wimlib_free;
2350 ret = wimlib_delete_image(wim, image);
2352 imagex_error(T("Failed to delete image from \"%"TS"\""),
2354 goto out_wimlib_free;
2357 ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
2359 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2360 "deleted"), wimfile);
2368 usage(CMD_DELETE, stderr);
2373 struct print_dentry_options {
2378 print_dentry_full_path(const struct wimlib_dir_entry *dentry)
2380 tprintf(T("%"TS"\n"), dentry->full_path);
2383 static const struct {
2386 } file_attr_flags[] = {
2387 {WIMLIB_FILE_ATTRIBUTE_READONLY, T("READONLY")},
2388 {WIMLIB_FILE_ATTRIBUTE_HIDDEN, T("HIDDEN")},
2389 {WIMLIB_FILE_ATTRIBUTE_SYSTEM, T("SYSTEM")},
2390 {WIMLIB_FILE_ATTRIBUTE_DIRECTORY, T("DIRECTORY")},
2391 {WIMLIB_FILE_ATTRIBUTE_ARCHIVE, T("ARCHIVE")},
2392 {WIMLIB_FILE_ATTRIBUTE_DEVICE, T("DEVICE")},
2393 {WIMLIB_FILE_ATTRIBUTE_NORMAL, T("NORMAL")},
2394 {WIMLIB_FILE_ATTRIBUTE_TEMPORARY, T("TEMPORARY")},
2395 {WIMLIB_FILE_ATTRIBUTE_SPARSE_FILE, T("SPARSE_FILE")},
2396 {WIMLIB_FILE_ATTRIBUTE_REPARSE_POINT, T("REPARSE_POINT")},
2397 {WIMLIB_FILE_ATTRIBUTE_COMPRESSED, T("COMPRESSED")},
2398 {WIMLIB_FILE_ATTRIBUTE_OFFLINE, T("OFFLINE")},
2399 {WIMLIB_FILE_ATTRIBUTE_NOT_CONTENT_INDEXED, T("NOT_CONTENT_INDEXED")},
2400 {WIMLIB_FILE_ATTRIBUTE_ENCRYPTED, T("ENCRYPTED")},
2401 {WIMLIB_FILE_ATTRIBUTE_VIRTUAL, T("VIRTUAL")},
2404 #define TIMESTR_MAX 100
2407 timespec_to_string(const struct timespec *spec, tchar *buf)
2409 time_t t = spec->tv_sec;
2412 tstrftime(buf, TIMESTR_MAX, T("%a %b %d %H:%M:%S %Y UTC"), &tm);
2413 buf[TIMESTR_MAX - 1] = '\0';
2417 print_time(const tchar *type, const struct timespec *spec)
2419 tchar timestr[TIMESTR_MAX];
2421 timespec_to_string(spec, timestr);
2423 tprintf(T("%-20"TS"= %"TS"\n"), type, timestr);
2426 static void print_byte_field(const uint8_t field[], size_t len)
2429 tprintf(T("%02hhx"), *field++);
2433 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2435 tputs(T("WIM Information:"));
2436 tputs(T("----------------"));
2437 tprintf(T("Path: %"TS"\n"), wimfile);
2438 tprintf(T("GUID: 0x"));
2439 print_byte_field(info->guid, sizeof(info->guid));
2441 tprintf(T("Version: %u\n"), info->wim_version);
2442 tprintf(T("Image Count: %d\n"), info->image_count);
2443 tprintf(T("Compression: %"TS"\n"),
2444 wimlib_get_compression_type_string(info->compression_type));
2445 tprintf(T("Chunk Size: %"PRIu32" bytes\n"),
2447 tprintf(T("Part Number: %d/%d\n"), info->part_number, info->total_parts);
2448 tprintf(T("Boot Index: %d\n"), info->boot_index);
2449 tprintf(T("Size: %"PRIu64" bytes\n"), info->total_bytes);
2450 tprintf(T("Integrity Info: %"TS"\n"),
2451 info->has_integrity_table ? T("yes") : T("no"));
2452 tprintf(T("Relative path junction: %"TS"\n"),
2453 info->has_rpfix ? T("yes") : T("no"));
2454 tprintf(T("Pipable: %"TS"\n"),
2455 info->pipable ? T("yes") : T("no"));
2460 print_resource(const struct wimlib_resource_entry *resource,
2463 tprintf(T("Hash = 0x"));
2464 print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2467 if (!resource->is_missing) {
2468 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
2469 resource->uncompressed_size);
2470 if (resource->packed) {
2471 tprintf(T("Raw compressed size = %"PRIu64" bytes\n"),
2472 resource->raw_resource_compressed_size);
2474 tprintf(T("Raw offset in WIM = %"PRIu64" bytes\n"),
2475 resource->raw_resource_offset_in_wim);
2477 tprintf(T("Offset in raw = %"PRIu64" bytes\n"),
2480 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
2481 resource->compressed_size);
2483 tprintf(T("Offset in WIM = %"PRIu64" bytes\n"),
2487 tprintf(T("Part Number = %u\n"), resource->part_number);
2488 tprintf(T("Reference Count = %u\n"), resource->reference_count);
2490 tprintf(T("Flags = "));
2491 if (resource->is_compressed)
2492 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED "));
2493 if (resource->is_metadata)
2494 tprintf(T("WIM_RESHDR_FLAG_METADATA "));
2495 if (resource->is_free)
2496 tprintf(T("WIM_RESHDR_FLAG_FREE "));
2497 if (resource->is_spanned)
2498 tprintf(T("WIM_RESHDR_FLAG_SPANNED "));
2499 if (resource->packed)
2500 tprintf(T("WIM_RESHDR_FLAG_PACKED_STREAMS "));
2508 print_lookup_table(WIMStruct *wim)
2510 wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2514 default_print_security_descriptor(const uint8_t *sd, size_t size)
2516 tprintf(T("Security Descriptor = "));
2517 print_byte_field(sd, size);
2522 print_dentry_detailed(const struct wimlib_dir_entry *dentry)
2526 "----------------------------------------------------------------------------\n"));
2527 tprintf(T("Full Path = \"%"TS"\"\n"), dentry->full_path);
2528 if (dentry->dos_name)
2529 tprintf(T("Short Name = \"%"TS"\"\n"), dentry->dos_name);
2530 tprintf(T("Attributes = 0x%08x\n"), dentry->attributes);
2531 for (size_t i = 0; i < ARRAY_LEN(file_attr_flags); i++)
2532 if (file_attr_flags[i].flag & dentry->attributes)
2533 tprintf(T(" FILE_ATTRIBUTE_%"TS" is set\n"),
2534 file_attr_flags[i].name);
2536 if (dentry->security_descriptor) {
2537 print_security_descriptor(dentry->security_descriptor,
2538 dentry->security_descriptor_size);
2541 print_time(T("Creation Time"), &dentry->creation_time);
2542 print_time(T("Last Write Time"), &dentry->last_write_time);
2543 print_time(T("Last Access Time"), &dentry->last_access_time);
2546 if (dentry->attributes & WIMLIB_FILE_ATTRIBUTE_REPARSE_POINT)
2547 tprintf(T("Reparse Tag = 0x%"PRIx32"\n"), dentry->reparse_tag);
2549 tprintf(T("Link Group ID = 0x%016"PRIx64"\n"), dentry->hard_link_group_id);
2550 tprintf(T("Link Count = %"PRIu32"\n"), dentry->num_links);
2552 for (uint32_t i = 0; i <= dentry->num_named_streams; i++) {
2553 if (dentry->streams[i].stream_name) {
2554 tprintf(T("\tData stream \"%"TS"\":\n"),
2555 dentry->streams[i].stream_name);
2557 tprintf(T("\tUnnamed data stream:\n"));
2559 print_resource(&dentry->streams[i].resource, NULL);
2564 print_dentry(const struct wimlib_dir_entry *dentry, void *_options)
2566 const struct print_dentry_options *options = _options;
2567 if (!options->detailed)
2568 print_dentry_full_path(dentry);
2570 print_dentry_detailed(dentry);
2574 /* Print the files contained in an image(s) in a WIM file. */
2576 imagex_dir(int argc, tchar **argv, int cmd)
2578 const tchar *wimfile;
2579 WIMStruct *wim = NULL;
2582 const tchar *path = T("");
2584 struct print_dentry_options options = {
2588 for_opt(c, dir_options) {
2590 case IMAGEX_PATH_OPTION:
2593 case IMAGEX_DETAILED_OPTION:
2594 options.detailed = true;
2604 imagex_error(T("Must specify a WIM file"));
2608 imagex_error(T("Too many arguments"));
2613 ret = wimlib_open_wim(wimfile, 0, &wim, imagex_progress_func);
2618 image = wimlib_resolve_image(wim, argv[1]);
2619 ret = verify_image_exists(image, argv[1], wimfile);
2621 goto out_wimlib_free;
2623 /* No image specified; default to image 1, but only if the WIM
2624 * contains exactly one image. */
2626 struct wimlib_wim_info info;
2628 wimlib_get_wim_info(wim, &info);
2629 if (info.image_count != 1) {
2630 imagex_error(T("\"%"TS"\" contains %d images; Please "
2631 "select one (or all)."),
2632 wimfile, info.image_count);
2639 ret = wimlib_iterate_dir_tree(wim, image, path,
2640 WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE,
2641 print_dentry, &options);
2648 usage(CMD_DIR, stderr);
2653 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2656 imagex_export(int argc, tchar **argv, int cmd)
2660 int export_flags = 0;
2661 int write_flags = 0;
2662 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2663 const tchar *src_wimfile;
2664 const tchar *src_image_num_or_name;
2665 const tchar *dest_wimfile;
2667 const tchar *dest_name;
2668 const tchar *dest_desc;
2670 struct wimlib_wim_info src_info;
2671 WIMStruct *dest_wim;
2676 STRING_SET(refglobs);
2677 unsigned num_threads = 0;
2678 uint32_t chunk_size = UINT32_MAX;
2679 uint32_t pack_chunk_size = UINT32_MAX;
2681 for_opt(c, export_options) {
2683 case IMAGEX_BOOT_OPTION:
2684 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2686 case IMAGEX_CHECK_OPTION:
2687 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2688 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2690 case IMAGEX_NOCHECK_OPTION:
2691 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2693 case IMAGEX_COMPRESS_OPTION:
2694 compression_type = get_compression_type(optarg);
2695 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2698 case IMAGEX_PACK_STREAMS_OPTION:
2699 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
2701 case IMAGEX_CHUNK_SIZE_OPTION:
2702 chunk_size = parse_chunk_size(optarg);
2703 if (chunk_size == UINT32_MAX)
2706 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
2707 pack_chunk_size = parse_chunk_size(optarg);
2708 if (pack_chunk_size == UINT32_MAX)
2711 case IMAGEX_REF_OPTION:
2712 ret = string_set_append(&refglobs, optarg);
2714 goto out_free_refglobs;
2716 case IMAGEX_THREADS_OPTION:
2717 num_threads = parse_num_threads(optarg);
2718 if (num_threads == UINT_MAX)
2721 case IMAGEX_REBUILD_OPTION:
2722 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2724 case IMAGEX_PIPABLE_OPTION:
2725 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2727 case IMAGEX_NOT_PIPABLE_OPTION:
2728 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
2736 if (argc < 3 || argc > 5)
2738 src_wimfile = argv[0];
2739 src_image_num_or_name = argv[1];
2740 dest_wimfile = argv[2];
2741 dest_name = (argc >= 4) ? argv[3] : NULL;
2742 dest_desc = (argc >= 5) ? argv[4] : NULL;
2743 ret = wimlib_open_wim(src_wimfile, open_flags, &src_wim,
2744 imagex_progress_func);
2746 goto out_free_refglobs;
2748 wimlib_get_wim_info(src_wim, &src_info);
2750 /* Determine if the destination is an existing file or not. If so, we
2751 * try to append the exported image(s) to it; otherwise, we create a new
2752 * WIM containing the exported image(s). Furthermore, determine if we
2753 * need to write a pipable WIM directly to standard output. */
2755 if (tstrcmp(dest_wimfile, T("-")) == 0) {
2757 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2758 imagex_error("Can't write a non-pipable WIM to "
2759 "standard output! Specify --pipable\n"
2760 " if you want to create a pipable WIM "
2761 "(but read the docs first).");
2763 goto out_free_src_wim;
2766 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2768 dest_wimfile = NULL;
2769 dest_wim_fd = STDOUT_FILENO;
2770 imagex_info_file = stderr;
2771 set_fd_to_binary_mode(dest_wim_fd);
2774 if (dest_wimfile != NULL && tstat(dest_wimfile, &stbuf) == 0) {
2776 /* Destination file exists. */
2778 if (!S_ISREG(stbuf.st_mode)) {
2779 imagex_error(T("\"%"TS"\" is not a regular file"),
2782 goto out_free_src_wim;
2784 ret = wimlib_open_wim(dest_wimfile,
2785 open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2786 &dest_wim, imagex_progress_func);
2788 goto out_free_src_wim;
2790 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2791 /* The user specified a compression type, but we're
2792 * exporting to an existing WIM. Make sure the
2793 * specified compression type is the same as the
2794 * compression type of the existing destination WIM. */
2795 struct wimlib_wim_info dest_info;
2797 wimlib_get_wim_info(dest_wim, &dest_info);
2798 if (compression_type != dest_info.compression_type) {
2799 imagex_error(T("Cannot specify a compression type that is "
2800 "not the same as that used in the "
2801 "destination WIM"));
2803 goto out_free_dest_wim;
2809 if (errno != ENOENT) {
2810 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2813 goto out_free_src_wim;
2816 /* dest_wimfile is not an existing file, so create a new WIM. */
2818 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2819 /* The user did not specify a compression type; default
2820 * to that of the source WIM. */
2822 compression_type = src_info.compression_type;
2824 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2826 goto out_free_src_wim;
2828 /* Use same chunk size if compression type is the same. */
2829 if (compression_type == src_info.compression_type &&
2830 chunk_size == UINT32_MAX)
2831 wimlib_set_output_chunk_size(dest_wim, src_info.chunk_size);
2834 if (chunk_size != UINT32_MAX) {
2835 /* Set destination chunk size. */
2836 ret = wimlib_set_output_chunk_size(dest_wim, chunk_size);
2838 goto out_free_dest_wim;
2840 if (pack_chunk_size != UINT32_MAX) {
2841 ret = wimlib_set_output_pack_chunk_size(dest_wim, pack_chunk_size);
2843 goto out_free_dest_wim;
2846 image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2847 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2849 goto out_free_dest_wim;
2851 if (refglobs.num_strings) {
2852 ret = wim_reference_globs(src_wim, &refglobs, open_flags);
2854 goto out_free_dest_wim;
2857 if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
2858 image == WIMLIB_ALL_IMAGES && src_info.boot_index == 0)
2860 imagex_error(T("--boot specified for all-images export, but source WIM "
2861 "has no bootable image."));
2863 goto out_free_dest_wim;
2866 ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2867 dest_desc, export_flags, imagex_progress_func);
2869 if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2870 do_resource_not_found_warning(src_wimfile,
2871 &src_info, &refglobs);
2873 goto out_free_dest_wim;
2877 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2878 imagex_progress_func);
2879 else if (dest_wimfile)
2880 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2881 write_flags, num_threads,
2882 imagex_progress_func);
2884 ret = wimlib_write_to_fd(dest_wim, dest_wim_fd,
2885 WIMLIB_ALL_IMAGES, write_flags,
2886 num_threads, imagex_progress_func);
2888 wimlib_free(dest_wim);
2890 wimlib_free(src_wim);
2892 string_set_destroy(&refglobs);
2896 usage(CMD_EXPORT, stderr);
2899 goto out_free_refglobs;
2903 is_root_wim_path(const tchar *path)
2906 for (p = path; *p; p++)
2907 if (*p != T('\\') && *p != T('/'))
2913 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2914 const tchar *dest_dir)
2916 for (size_t i = 0; i < num_cmds; i++)
2917 if (cmds[i].fs_dest_path != dest_dir)
2918 free(cmds[i].fs_dest_path);
2922 static struct wimlib_extract_command *
2923 prepare_extract_commands(tchar **paths, unsigned num_paths,
2924 int extract_flags, tchar *dest_dir,
2925 size_t *num_cmds_ret)
2927 struct wimlib_extract_command *cmds;
2929 tchar *emptystr = T("");
2931 if (num_paths == 0) {
2935 num_cmds = num_paths;
2936 cmds = calloc(num_cmds, sizeof(cmds[0]));
2938 imagex_error(T("Out of memory!"));
2942 for (size_t i = 0; i < num_cmds; i++) {
2943 cmds[i].extract_flags = extract_flags;
2944 cmds[i].wim_source_path = paths[i];
2945 if (is_root_wim_path(paths[i])) {
2946 cmds[i].fs_dest_path = dest_dir;
2948 size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2949 cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2950 if (!cmds[i].fs_dest_path) {
2951 free_extract_commands(cmds, num_cmds, dest_dir);
2954 tsprintf(cmds[i].fs_dest_path,
2955 T("%"TS""OS_PREFERRED_PATH_SEPARATOR_STRING"%"TS),
2956 dest_dir, tbasename(paths[i]));
2959 *num_cmds_ret = num_cmds;
2963 /* Extract files or directories from a WIM image */
2965 imagex_extract(int argc, tchar **argv, int cmd)
2972 const tchar *wimfile;
2973 const tchar *image_num_or_name;
2974 const tchar *pathlist;
2975 tchar *dest_dir = T(".");
2976 int extract_flags = WIMLIB_EXTRACT_FLAG_NORPFIX;
2977 int listfile_extract_flags = WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2979 STRING_SET(refglobs);
2981 struct wimlib_extract_command *cmds;
2984 for_opt(c, extract_options) {
2986 case IMAGEX_CHECK_OPTION:
2987 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2989 case IMAGEX_VERBOSE_OPTION:
2990 /* No longer does anything. */
2992 case IMAGEX_REF_OPTION:
2993 ret = string_set_append(&refglobs, optarg);
2995 goto out_free_refglobs;
2997 case IMAGEX_UNIX_DATA_OPTION:
2998 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
3000 case IMAGEX_NO_ACLS_OPTION:
3001 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
3003 case IMAGEX_STRICT_ACLS_OPTION:
3004 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
3006 case IMAGEX_DEST_DIR_OPTION:
3009 case IMAGEX_TO_STDOUT_OPTION:
3010 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
3011 imagex_info_file = stderr;
3012 imagex_be_quiet = true;
3014 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
3015 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
3016 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
3018 case IMAGEX_NO_WILDCARDS_OPTION:
3019 listfile_extract_flags &= ~WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
3021 case IMAGEX_STRICT_WILDCARDS_OPTION:
3022 listfile_extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_GLOB;
3035 image_num_or_name = argv[1];
3040 if (argc == 1 && argv[0][0] == T('@')) {
3041 pathlist = argv[0] + 1;
3045 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
3052 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3056 image = wimlib_resolve_image(wim, image_num_or_name);
3057 ret = verify_image_exists_and_is_single(image,
3061 goto out_wimlib_free;
3063 if (refglobs.num_strings) {
3064 ret = wim_reference_globs(wim, &refglobs, open_flags);
3066 goto out_wimlib_free;
3070 if (ret == 0 && cmds != NULL) {
3071 ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
3072 imagex_progress_func);
3074 if (ret == 0 && pathlist != NULL) {
3075 ret = wimlib_extract_pathlist(wim, image, dest_dir,
3077 extract_flags | listfile_extract_flags,
3078 imagex_progress_func);
3081 if (!imagex_be_quiet)
3082 imagex_printf(T("Done extracting files.\n"));
3083 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
3084 tfprintf(stderr, T("Note: You can use `%"TS"' to see what "
3085 "files and directories\n"
3086 " are in the WIM image.\n"),
3087 get_cmd_string(CMD_DIR, false));
3088 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
3089 struct wimlib_wim_info info;
3091 wimlib_get_wim_info(wim, &info);
3092 do_resource_not_found_warning(wimfile, &info, &refglobs);
3097 free_extract_commands(cmds, num_cmds, dest_dir);
3099 string_set_destroy(&refglobs);
3103 usage(CMD_EXTRACT, stderr);
3106 goto out_free_refglobs;
3109 /* Prints information about a WIM file; also can mark an image as bootable,
3110 * change the name of an image, or change the description of an image. */
3112 imagex_info(int argc, tchar **argv, int cmd)
3117 bool nocheck = false;
3118 bool header = false;
3119 bool lookup_table = false;
3121 bool short_header = true;
3122 const tchar *xml_out_file = NULL;
3123 const tchar *wimfile;
3124 const tchar *image_num_or_name;
3125 const tchar *new_name;
3126 const tchar *new_desc;
3131 struct wimlib_wim_info info;
3133 for_opt(c, info_options) {
3135 case IMAGEX_BOOT_OPTION:
3138 case IMAGEX_CHECK_OPTION:
3141 case IMAGEX_NOCHECK_OPTION:
3144 case IMAGEX_HEADER_OPTION:
3146 short_header = false;
3148 case IMAGEX_LOOKUP_TABLE_OPTION:
3149 lookup_table = true;
3150 short_header = false;
3152 case IMAGEX_XML_OPTION:
3154 short_header = false;
3156 case IMAGEX_EXTRACT_XML_OPTION:
3157 xml_out_file = optarg;
3158 short_header = false;
3160 case IMAGEX_METADATA_OPTION:
3161 imagex_error(T("The --metadata option has been removed. "
3162 "Use 'wimdir --detail' instead."));
3171 if (argc < 1 || argc > 4)
3175 image_num_or_name = (argc >= 2) ? argv[1] : T("all");
3176 new_name = (argc >= 3) ? argv[2] : NULL;
3177 new_desc = (argc >= 4) ? argv[3] : NULL;
3179 if (check && nocheck) {
3180 imagex_error(T("Can't specify both --check and --nocheck"));
3185 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3187 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3191 wimlib_get_wim_info(wim, &info);
3193 image = wimlib_resolve_image(wim, image_num_or_name);
3194 ret = WIMLIB_ERR_INVALID_IMAGE;
3195 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
3196 verify_image_exists(image, image_num_or_name, wimfile);
3198 imagex_error(T("If you would like to set the boot "
3199 "index to 0, specify image \"0\" with "
3200 "the --boot flag."));
3202 goto out_wimlib_free;
3205 if (boot && info.image_count == 0) {
3206 imagex_error(T("--boot is meaningless on a WIM with no images"));
3207 goto out_wimlib_free;
3210 if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
3212 imagex_error(T("Cannot specify the --boot flag "
3213 "without specifying a specific "
3214 "image in a multi-image WIM"));
3215 goto out_wimlib_free;
3218 imagex_error(T("Cannot specify the NEW_NAME "
3219 "without specifying a specific "
3220 "image in a multi-image WIM"));
3221 goto out_wimlib_free;
3225 /* Operations that print information are separated from operations that
3226 * recreate the WIM file. */
3227 if (!new_name && !boot) {
3229 /* Read-only operations */
3231 if (image == WIMLIB_NO_IMAGE) {
3232 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
3233 image_num_or_name, wimfile);
3234 goto out_wimlib_free;
3237 if (image == WIMLIB_ALL_IMAGES && short_header)
3238 print_wim_information(wimfile, &info);
3241 wimlib_print_header(wim);
3244 if (info.total_parts != 1) {
3245 tfprintf(stderr, T("Warning: Only showing the lookup table "
3246 "for part %d of a %d-part WIM.\n"),
3247 info.part_number, info.total_parts);
3249 print_lookup_table(wim);
3253 ret = wimlib_extract_xml_data(wim, stdout);
3255 goto out_wimlib_free;
3261 fp = tfopen(xml_out_file, T("wb"));
3263 imagex_error_with_errno(T("Failed to open the "
3264 "file \"%"TS"\" for "
3268 goto out_wimlib_free;
3270 ret = wimlib_extract_xml_data(wim, fp);
3272 imagex_error(T("Failed to close the file "
3278 goto out_wimlib_free;
3282 wimlib_print_available_images(wim, image);
3287 /* Modification operations */
3289 if (image == WIMLIB_ALL_IMAGES)
3292 if (image == WIMLIB_NO_IMAGE && new_name) {
3293 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
3294 "when using image 0"), new_name);
3296 goto out_wimlib_free;
3300 if (image == info.boot_index) {
3301 imagex_printf(T("Image %d is already marked as "
3302 "bootable.\n"), image);
3305 imagex_printf(T("Marking image %d as bootable.\n"),
3307 info.boot_index = image;
3308 ret = wimlib_set_wim_info(wim, &info,
3309 WIMLIB_CHANGE_BOOT_INDEX);
3311 goto out_wimlib_free;
3315 if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
3317 imagex_printf(T("Image %d is already named \"%"TS"\".\n"),
3321 imagex_printf(T("Changing the name of image %d to "
3322 "\"%"TS"\".\n"), image, new_name);
3323 ret = wimlib_set_image_name(wim, image, new_name);
3325 goto out_wimlib_free;
3329 const tchar *old_desc;
3330 old_desc = wimlib_get_image_description(wim, image);
3331 if (old_desc && !tstrcmp(old_desc, new_desc)) {
3332 imagex_printf(T("The description of image %d is already "
3333 "\"%"TS"\".\n"), image, new_desc);
3336 imagex_printf(T("Changing the description of image %d "
3337 "to \"%"TS"\".\n"), image, new_desc);
3338 ret = wimlib_set_image_descripton(wim, image,
3341 goto out_wimlib_free;
3345 /* Only call wimlib_overwrite() if something actually needs to
3347 if (boot || new_name || new_desc ||
3348 (check && !info.has_integrity_table) ||
3349 (nocheck && info.has_integrity_table))
3351 int write_flags = 0;
3354 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3356 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3357 ret = wimlib_overwrite(wim, write_flags, 1,
3358 imagex_progress_func);
3360 imagex_printf(T("The file \"%"TS"\" was not modified "
3361 "because nothing needed to be done.\n"),
3372 usage(CMD_INFO, stderr);
3378 /* Join split WIMs into one part WIM */
3380 imagex_join(int argc, tchar **argv, int cmd)
3383 int swm_open_flags = 0;
3384 int wim_write_flags = 0;
3385 const tchar *output_path;
3388 for_opt(c, join_options) {
3390 case IMAGEX_CHECK_OPTION:
3391 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3392 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3402 imagex_error(T("Must specify one or more split WIM (.swm) "
3406 output_path = argv[0];
3407 ret = wimlib_join((const tchar * const *)++argv,
3412 imagex_progress_func);
3417 usage(CMD_JOIN, stderr);
3422 #if WIM_MOUNTING_SUPPORTED
3424 /* Mounts a WIM image. */
3426 imagex_mount_rw_or_ro(int argc, tchar **argv, int cmd)
3429 int mount_flags = 0;
3431 const tchar *staging_dir = NULL;
3432 const tchar *wimfile;
3435 struct wimlib_wim_info info;
3439 STRING_SET(refglobs);
3441 if (cmd == CMD_MOUNTRW) {
3442 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
3443 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3446 for_opt(c, mount_options) {
3448 case IMAGEX_ALLOW_OTHER_OPTION:
3449 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
3451 case IMAGEX_CHECK_OPTION:
3452 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3454 case IMAGEX_DEBUG_OPTION:
3455 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
3457 case IMAGEX_STREAMS_INTERFACE_OPTION:
3458 if (!tstrcasecmp(optarg, T("none")))
3459 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
3460 else if (!tstrcasecmp(optarg, T("xattr")))
3461 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
3462 else if (!tstrcasecmp(optarg, T("windows")))
3463 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
3465 imagex_error(T("Unknown stream interface \"%"TS"\""),
3470 case IMAGEX_REF_OPTION:
3471 ret = string_set_append(&refglobs, optarg);
3473 goto out_free_refglobs;
3475 case IMAGEX_STAGING_DIR_OPTION:
3476 staging_dir = optarg;
3478 case IMAGEX_UNIX_DATA_OPTION:
3479 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
3487 if (argc != 2 && argc != 3)
3492 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3494 goto out_free_refglobs;
3496 wimlib_get_wim_info(wim, &info);
3499 /* Image explicitly specified. */
3500 image = wimlib_resolve_image(wim, argv[1]);
3502 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
3506 /* No image specified; default to image 1, but only if the WIM
3507 * contains exactly one image. */
3509 if (info.image_count != 1) {
3510 imagex_error(T("\"%"TS"\" contains %d images; Please "
3511 "select one."), wimfile, info.image_count);
3519 if (refglobs.num_strings) {
3520 ret = wim_reference_globs(wim, &refglobs, open_flags);
3525 ret = wimlib_mount_image(wim, image, dir, mount_flags, staging_dir);
3527 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
3529 image, wimfile, dir);
3534 string_set_destroy(&refglobs);
3540 goto out_free_refglobs;
3542 #endif /* WIM_MOUNTING_SUPPORTED */
3544 /* Rebuild a WIM file */
3546 imagex_optimize(int argc, tchar **argv, int cmd)
3549 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3550 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
3551 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
3552 uint32_t chunk_size = UINT32_MAX;
3553 uint32_t pack_chunk_size = UINT32_MAX;
3556 const tchar *wimfile;
3559 unsigned num_threads = 0;
3561 for_opt(c, optimize_options) {
3563 case IMAGEX_CHECK_OPTION:
3564 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3565 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3567 case IMAGEX_NOCHECK_OPTION:
3568 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3570 case IMAGEX_COMPRESS_OPTION:
3571 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3572 compression_type = get_compression_type(optarg);
3573 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
3576 case IMAGEX_RECOMPRESS_OPTION:
3577 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3579 case IMAGEX_COMPRESS_SLOW_OPTION:
3580 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3581 set_compress_slow();
3583 case IMAGEX_CHUNK_SIZE_OPTION:
3584 chunk_size = parse_chunk_size(optarg);
3585 if (chunk_size == UINT32_MAX)
3588 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
3589 pack_chunk_size = parse_chunk_size(optarg);
3590 if (pack_chunk_size == UINT32_MAX)
3593 case IMAGEX_PACK_STREAMS_OPTION:
3594 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
3595 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3597 case IMAGEX_THREADS_OPTION:
3598 num_threads = parse_num_threads(optarg);
3599 if (num_threads == UINT_MAX)
3602 case IMAGEX_PIPABLE_OPTION:
3603 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
3605 case IMAGEX_NOT_PIPABLE_OPTION:
3606 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
3620 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3624 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
3625 /* Change compression type. */
3626 ret = wimlib_set_output_compression_type(wim, compression_type);
3628 goto out_wimlib_free;
3631 if (chunk_size != UINT32_MAX) {
3632 /* Change chunk size. */
3633 ret = wimlib_set_output_chunk_size(wim, chunk_size);
3635 goto out_wimlib_free;
3637 if (pack_chunk_size != UINT32_MAX) {
3638 ret = wimlib_set_output_pack_chunk_size(wim, pack_chunk_size);
3640 goto out_wimlib_free;
3643 old_size = file_get_size(wimfile);
3644 tprintf(T("\"%"TS"\" original size: "), wimfile);
3646 tputs(T("Unknown"));
3648 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3650 ret = wimlib_overwrite(wim, write_flags, num_threads,
3651 imagex_progress_func);
3653 imagex_error(T("Optimization of \"%"TS"\" failed."), wimfile);
3654 goto out_wimlib_free;
3657 new_size = file_get_size(wimfile);
3658 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3660 tputs(T("Unknown"));
3662 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3664 tfputs(T("Space saved: "), stdout);
3665 if (new_size != -1 && old_size != -1) {
3666 tprintf(T("%lld KiB\n"),
3667 ((long long)old_size - (long long)new_size) >> 10);
3669 tputs(T("Unknown"));
3678 usage(CMD_OPTIMIZE, stderr);
3684 /* Split a WIM into a spanned set */
3686 imagex_split(int argc, tchar **argv, int cmd)
3690 int write_flags = 0;
3691 unsigned long part_size;
3696 for_opt(c, split_options) {
3698 case IMAGEX_CHECK_OPTION:
3699 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3700 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3712 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3713 if (tmp == argv[2] || *tmp) {
3714 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3715 imagex_error(T("The part size must be an integer or "
3716 "floating-point number of megabytes."));
3719 ret = wimlib_open_wim(argv[0], open_flags, &wim, imagex_progress_func);
3723 ret = wimlib_split(wim, argv[1], part_size, write_flags, imagex_progress_func);
3729 usage(CMD_SPLIT, stderr);
3735 #if WIM_MOUNTING_SUPPORTED
3736 /* Unmounts a mounted WIM image. */
3738 imagex_unmount(int argc, tchar **argv, int cmd)
3741 int unmount_flags = 0;
3744 for_opt(c, unmount_options) {
3746 case IMAGEX_COMMIT_OPTION:
3747 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3749 case IMAGEX_CHECK_OPTION:
3750 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3752 case IMAGEX_REBUILD_OPTION:
3753 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3755 case IMAGEX_LAZY_OPTION:
3756 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3767 ret = wimlib_unmount_image(argv[0], unmount_flags,
3768 imagex_progress_func);
3770 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3775 usage(CMD_UNMOUNT, stderr);
3779 #endif /* WIM_MOUNTING_SUPPORTED */
3782 * Add, delete, or rename files in a WIM image.
3785 imagex_update(int argc, tchar **argv, int cmd)
3787 const tchar *wimfile;
3791 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3792 int write_flags = 0;
3793 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3794 int default_add_flags = WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
3795 WIMLIB_ADD_FLAG_VERBOSE;
3796 int default_delete_flags = 0;
3797 unsigned num_threads = 0;
3799 tchar *cmd_file_contents;
3800 size_t cmd_file_nchars;
3801 struct wimlib_update_command *cmds;
3803 tchar *command_str = NULL;
3805 const tchar *config_file = NULL;
3807 struct wimlib_capture_config *config;
3809 for_opt(c, update_options) {
3811 /* Generic or write options */
3812 case IMAGEX_THREADS_OPTION:
3813 num_threads = parse_num_threads(optarg);
3814 if (num_threads == UINT_MAX)
3817 case IMAGEX_CHECK_OPTION:
3818 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3819 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3821 case IMAGEX_REBUILD_OPTION:
3822 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3824 case IMAGEX_COMMAND_OPTION:
3826 imagex_error(T("--command may only be specified "
3827 "one time. Please provide\n"
3828 " the update commands "
3829 "on standard input instead."));
3832 command_str = tstrdup(optarg);
3834 imagex_error(T("Out of memory!"));
3838 /* Default delete options */
3839 case IMAGEX_FORCE_OPTION:
3840 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3842 case IMAGEX_RECURSIVE_OPTION:
3843 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3846 /* Global add option */
3847 case IMAGEX_CONFIG_OPTION:
3848 default_add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
3849 config_file = optarg;
3852 /* Default add options */
3853 case IMAGEX_VERBOSE_OPTION:
3854 /* No longer does anything. */
3856 case IMAGEX_DEREFERENCE_OPTION:
3857 default_add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
3859 case IMAGEX_UNIX_DATA_OPTION:
3860 default_add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
3862 case IMAGEX_NO_ACLS_OPTION:
3863 default_add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
3865 case IMAGEX_STRICT_ACLS_OPTION:
3866 default_add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
3875 if (argc != 1 && argc != 2)
3879 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3881 goto out_free_command_str;
3884 /* Image explicitly specified. */
3885 image = wimlib_resolve_image(wim, argv[1]);
3886 ret = verify_image_exists_and_is_single(image, argv[1],
3889 goto out_wimlib_free;
3891 /* No image specified; default to image 1, but only if the WIM
3892 * contains exactly one image. */
3893 struct wimlib_wim_info info;
3895 wimlib_get_wim_info(wim, &info);
3896 if (info.image_count != 1) {
3897 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3898 wimfile, info.image_count);
3905 /* Parse capture configuration file if specified */
3909 config_str = file_get_text_contents(config_file, &config_len);
3912 goto out_wimlib_free;
3915 config = alloca(sizeof(*config));
3916 ret = parse_capture_config(&config_str, config_len, config);
3918 goto out_free_config;
3921 default_add_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
3924 /* Read update commands from standard input, or the command string if
3927 cmd_file_contents = NULL;
3928 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3931 if (isatty(STDIN_FILENO)) {
3932 tputs(T("Reading update commands from standard input..."));
3933 recommend_man_page(CMD_UPDATE, stdout);
3935 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3936 if (!cmd_file_contents) {
3938 goto out_free_config;
3941 /* Parse the update commands */
3942 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3947 goto out_free_cmd_file_contents;
3950 /* Set default flags and capture config on the update commands */
3951 for (size_t i = 0; i < num_cmds; i++) {
3952 switch (cmds[i].op) {
3953 case WIMLIB_UPDATE_OP_ADD:
3954 cmds[i].add.add_flags |= default_add_flags;
3955 cmds[i].add.config = config;
3957 case WIMLIB_UPDATE_OP_DELETE:
3958 cmds[i].delete_.delete_flags |= default_delete_flags;
3965 /* Execute the update commands */
3966 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3967 imagex_progress_func);
3971 /* Overwrite the updated WIM */
3972 ret = wimlib_overwrite(wim, write_flags, num_threads,
3973 imagex_progress_func);
3976 out_free_cmd_file_contents:
3977 free(cmd_file_contents);
3980 free(config->exclusion_pats.pats);
3981 free(config->exclusion_exception_pats.pats);
3986 out_free_command_str:
3991 usage(CMD_UPDATE, stderr);
3994 goto out_free_command_str;
3999 struct imagex_command {
4001 int (*func)(int argc, tchar **argv, int cmd);
4004 static const struct imagex_command imagex_commands[] = {
4005 [CMD_APPEND] = {T("append"), imagex_capture_or_append},
4006 [CMD_APPLY] = {T("apply"), imagex_apply},
4007 [CMD_CAPTURE] = {T("capture"), imagex_capture_or_append},
4008 [CMD_DELETE] = {T("delete"), imagex_delete},
4009 [CMD_DIR ] = {T("dir"), imagex_dir},
4010 [CMD_EXPORT] = {T("export"), imagex_export},
4011 [CMD_EXTRACT] = {T("extract"), imagex_extract},
4012 [CMD_INFO] = {T("info"), imagex_info},
4013 [CMD_JOIN] = {T("join"), imagex_join},
4014 #if WIM_MOUNTING_SUPPORTED
4015 [CMD_MOUNT] = {T("mount"), imagex_mount_rw_or_ro},
4016 [CMD_MOUNTRW] = {T("mountrw"), imagex_mount_rw_or_ro},
4018 [CMD_OPTIMIZE] = {T("optimize"), imagex_optimize},
4019 [CMD_SPLIT] = {T("split"), imagex_split},
4020 #if WIM_MOUNTING_SUPPORTED
4021 [CMD_UNMOUNT] = {T("unmount"), imagex_unmount},
4023 [CMD_UPDATE] = {T("update"), imagex_update},
4026 static const tchar *usage_strings[] = {
4029 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
4030 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
4031 " [--nocheck] [--flags EDITION_ID] [--dereference]\n"
4032 " [--config=FILE] [--threads=NUM_THREADS] [--source-list]\n"
4033 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
4034 " [--update-of=[WIMFILE:]IMAGE]\n"
4038 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME | all)]\n"
4039 " (DIRECTORY | NTFS_VOLUME) [--check] [--ref=\"GLOB\"]\n"
4040 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
4041 " [--hardlink] [--symlink] [--include-invalid-names]\n"
4045 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
4046 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
4047 " [--nocheck] [--compress=TYPE] [--flags EDITION_ID]\n"
4048 " [--dereference] [--config=FILE] [--threads=NUM_THREADS]\n"
4049 " [--source-list] [--no-acls] [--strict-acls] [--rpfix]\n"
4050 " [--norpfix] [--update-of=[WIMFILE:]IMAGE]\n"
4051 " [--delta-from=WIMFILE]\n"
4055 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n"
4056 " [--check] [--soft]\n"
4060 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH] [--detailed]\n"
4064 " %"TS" SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
4065 " DEST_WIMFILE [DEST_IMAGE_NAME [DEST_IMAGE_DESCRIPTION]]\n"
4066 " [--boot] [--check] [--nocheck] [--compress=TYPE]\n"
4067 " [--ref=\"GLOB\"] [--threads=NUM_THREADS] [--rebuild]\n"
4071 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME) ([PATH...] | @LISTFILE)\n"
4072 " [--check] [--ref=\"GLOB\"] [--no-acls] [--strict-acls]\n"
4073 " [--to-stdout] [--dest-dir=CMD_DIR]\n"
4074 " [--include-invalid-names]\n"
4078 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME) [NEW_NAME\n"
4079 " [NEW_DESC]]] [--boot] [--check] [--nocheck] [--xml]\n"
4080 " [--extract-xml FILE] [--header] [--lookup-table]\n"
4084 " %"TS" OUT_WIMFILE SPLIT_WIM_PART... [--check]\n"
4086 #if WIM_MOUNTING_SUPPORTED
4089 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
4090 " [--check] [--streams-interface=INTERFACE]\n"
4091 " [--ref=\"GLOB\"] [--allow-other]\n"
4095 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
4096 " [--check] [--streams-interface=INTERFACE]\n"
4097 " [--staging-dir=CMD_DIR] [--allow-other]\n"
4102 " %"TS" WIMFILE [--check] [--nocheck] [--recompress]\n"
4103 " [--recompress-slow] [--compress=TYPE]\n"
4104 " [--threads=NUM_THREADS]\n"
4108 " %"TS" WIMFILE SPLIT_WIM_PART_1 PART_SIZE_MB [--check]\n"
4110 #if WIM_MOUNTING_SUPPORTED
4113 " %"TS" DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
4118 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
4119 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
4120 " [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
4124 static const tchar *invocation_name;
4125 static int invocation_cmd = CMD_NONE;
4127 static const tchar *get_cmd_string(int cmd, bool nospace)
4129 static tchar buf[50];
4130 if (cmd == CMD_NONE) {
4131 tsprintf(buf, T("%"TS), T(IMAGEX_PROGNAME));
4132 } else if (invocation_cmd != CMD_NONE) {
4133 tsprintf(buf, T("wim%"TS), imagex_commands[cmd].name);
4135 const tchar *format;
4138 format = T("%"TS"-%"TS"");
4140 format = T("%"TS" %"TS"");
4141 tsprintf(buf, format, invocation_name, imagex_commands[cmd].name);
4149 static const tchar *s =
4151 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
4152 "Copyright (C) 2012, 2013 Eric Biggers\n"
4153 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
4154 "This is free software: you are free to change and redistribute it.\n"
4155 "There is NO WARRANTY, to the extent permitted by law.\n"
4157 "Report bugs to "PACKAGE_BUGREPORT".\n"
4164 help_or_version(int argc, tchar **argv, int cmd)
4169 for (i = 1; i < argc; i++) {
4171 if (p[0] == T('-') && p[1] == T('-')) {
4173 if (!tstrcmp(p, T("help"))) {
4174 if (cmd == CMD_NONE)
4179 } else if (!tstrcmp(p, T("version"))) {
4188 print_usage_string(int cmd, FILE *fp)
4190 tfprintf(fp, usage_strings[cmd], get_cmd_string(cmd, false));
4194 recommend_man_page(int cmd, FILE *fp)
4196 const tchar *format_str;
4198 format_str = T("Uncommon options are not listed;\n"
4199 "See %"TS".pdf in the doc directory for more details.\n");
4201 format_str = T("Uncommon options are not listed;\n"
4202 "Try `man %"TS"' for more details.\n");
4204 tfprintf(fp, format_str, get_cmd_string(cmd, true));
4208 usage(int cmd, FILE *fp)
4210 tfprintf(fp, T("Usage:\n"));
4211 print_usage_string(cmd, fp);
4212 tfprintf(fp, T("\n"));
4213 recommend_man_page(cmd, fp);
4219 tfprintf(fp, T("Usage:\n"));
4220 for (int cmd = 0; cmd < CMD_MAX; cmd++) {
4221 print_usage_string(cmd, fp);
4222 tfprintf(fp, T("\n"));
4224 static const tchar *extra =
4227 " %"TS" --version\n"
4229 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
4232 tfprintf(fp, extra, invocation_name, invocation_name);
4233 recommend_man_page(CMD_NONE, fp);
4236 /* Entry point for wimlib's ImageX implementation. On UNIX the command
4237 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
4238 * something else), while an Windows the command arguments will be UTF-16LE
4239 * encoded 'wchar_t' strings. */
4242 wmain(int argc, wchar_t **argv, wchar_t **envp)
4244 main(int argc, char **argv)
4251 imagex_info_file = stdout;
4252 invocation_name = tbasename(argv[0]);
4255 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
4256 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
4260 setlocale(LC_ALL, "");
4261 codeset = nl_langinfo(CODESET);
4262 if (!strstr(codeset, "UTF-8") &&
4263 !strstr(codeset, "UTF8") &&
4264 !strstr(codeset, "utf-8") &&
4265 !strstr(codeset, "utf8"))
4268 "WARNING: Running %"TS" in a UTF-8 locale is recommended!\n"
4269 " Maybe try: `export LANG=en_US.UTF-8'?\n"
4270 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
4271 " to any value to force wimlib to use UTF-8.\n",
4277 #endif /* !__WIN32__ */
4280 tchar *igcase = tgetenv(T("WIMLIB_IMAGEX_IGNORE_CASE"));
4281 if (igcase != NULL) {
4282 if (!tstrcmp(igcase, T("no")) ||
4283 !tstrcmp(igcase, T("0")))
4284 init_flags |= WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE;
4285 else if (!tstrcmp(igcase, T("yes")) ||
4286 !tstrcmp(igcase, T("1")))
4287 init_flags |= WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE;
4290 "WARNING: Ignoring unknown setting of "
4291 "WIMLIB_IMAGEX_IGNORE_CASE\n");
4296 /* Allow being invoked as wimCOMMAND (e.g. wimapply). */
4298 if (!tstrncmp(invocation_name, T("wim"), 3) &&
4299 tstrcmp(invocation_name, T(IMAGEX_PROGNAME))) {
4300 for (int i = 0; i < CMD_MAX; i++) {
4301 if (!tstrcmp(invocation_name + 3,
4302 imagex_commands[i].name))
4311 /* Unless already known from the invocation name, determine which
4312 * command was specified. */
4313 if (cmd == CMD_NONE) {
4315 imagex_error(T("No command specified!\n"));
4319 for (int i = 0; i < CMD_MAX; i++) {
4320 if (!tstrcmp(argv[1], imagex_commands[i].name)) {
4325 if (cmd != CMD_NONE) {
4331 /* Handle --help and --version. --help can be either for the program as
4332 * a whole (cmd == CMD_NONE) or just for a specific command (cmd !=
4333 * CMD_NONE). Note: help_or_version() will not return if a --help or
4334 * --version argument was found. */
4335 help_or_version(argc, argv, cmd);
4337 /* Bail if a valid command was not specified. */
4338 if (cmd == CMD_NONE) {
4339 imagex_error(T("Unrecognized command: `%"TS"'\n"), argv[1]);
4344 /* Enable warning and error messages in wimlib be more user-friendly.
4346 wimlib_set_print_errors(true);
4348 /* Initialize wimlib. */
4349 ret = wimlib_global_init(init_flags);
4351 goto out_check_status;
4353 /* Call the command handler function. */
4354 ret = imagex_commands[cmd].func(argc, argv, cmd);
4356 /* Check for error writing to standard output, especially since for some
4357 * commands, writing to standard output is part of the program's actual
4358 * behavior and not just for informational purposes. */
4359 if (ferror(stdout) || fclose(stdout)) {
4360 imagex_error_with_errno(T("error writing to standard output"));
4365 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
4366 * itself (not by wimlib). 0 indicates success. > 0 indicates a wimlib
4367 * error code from which an error message can be printed. */
4369 imagex_error(T("Exiting with error code %d:\n"
4371 wimlib_get_error_string(ret));
4372 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
4373 imagex_error_with_errno(T("errno"));
4375 /* Make wimlib free any resources it's holding (although this is not
4376 * strictly necessary because the process is ending anyway). */
4377 wimlib_global_cleanup();