4 * Use wimlib to create, modify, extract, mount, unmount, or display information
9 * Copyright (C) 2012, 2013, 2014 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 print_security_descriptor win32_print_security_descriptor
54 # include <langinfo.h>
55 # define print_security_descriptor default_print_security_descriptor
56 static inline void set_fd_to_binary_mode(int fd)
61 /* Don't confuse the user by presenting the mounting commands on Windows when
62 * they will never work. However on UNIX-like systems we always present them,
63 * even if WITH_FUSE is not defined at this point, as to not tie the build of
64 * wimlib-imagex to a specific build of wimlib. */
66 # define WIM_MOUNTING_SUPPORTED 0
68 # define WIM_MOUNTING_SUPPORTED 1
71 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
74 is_any_path_separator(tchar c)
76 return c == T('/') || c == T('\\');
79 /* Like basename(), but handles both forward and backwards slashes. */
81 tbasename(tchar *path)
83 tchar *p = tstrchr(path, T('\0'));
88 if (!is_any_path_separator(*--p))
96 if (is_any_path_separator(*--p))
101 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
115 #if WIM_MOUNTING_SUPPORTED
121 #if WIM_MOUNTING_SUPPORTED
128 static void usage(int cmd, FILE *fp);
129 static void usage_all(FILE *fp);
130 static void recommend_man_page(int cmd, FILE *fp);
131 static const tchar *get_cmd_string(int cmd, bool nospace);
133 static int imagex_progress_func(enum wimlib_progress_msg msg,
134 const union wimlib_progress_info *info);
136 static bool imagex_be_quiet = false;
137 static FILE *imagex_info_file;
139 #define imagex_printf(format, ...) \
140 tfprintf(imagex_info_file, format, ##__VA_ARGS__)
143 IMAGEX_ALLOW_OTHER_OPTION,
146 IMAGEX_CHUNK_SIZE_OPTION,
147 IMAGEX_COMMAND_OPTION,
148 IMAGEX_COMMIT_OPTION,
149 IMAGEX_COMPRESS_OPTION,
150 IMAGEX_COMPRESS_SLOW_OPTION,
151 IMAGEX_CONFIG_OPTION,
153 IMAGEX_DELTA_FROM_OPTION,
154 IMAGEX_DEREFERENCE_OPTION,
155 IMAGEX_DEST_DIR_OPTION,
156 IMAGEX_DETAILED_OPTION,
157 IMAGEX_EXTRACT_XML_OPTION,
160 IMAGEX_HARDLINK_OPTION,
161 IMAGEX_HEADER_OPTION,
162 IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
164 IMAGEX_LOOKUP_TABLE_OPTION,
165 IMAGEX_METADATA_OPTION,
166 IMAGEX_NEW_IMAGE_OPTION,
167 IMAGEX_NOCHECK_OPTION,
168 IMAGEX_NORPFIX_OPTION,
169 IMAGEX_NOT_PIPABLE_OPTION,
170 IMAGEX_NO_ACLS_OPTION,
171 IMAGEX_NO_ATTRIBUTES_OPTION,
172 IMAGEX_NO_REPLACE_OPTION,
173 IMAGEX_NO_WILDCARDS_OPTION,
174 IMAGEX_NULLGLOB_OPTION,
175 IMAGEX_ONE_FILE_ONLY_OPTION,
176 IMAGEX_PACK_CHUNK_SIZE_OPTION,
177 IMAGEX_PACK_COMPRESS_OPTION,
178 IMAGEX_PACK_STREAMS_OPTION,
180 IMAGEX_PIPABLE_OPTION,
181 IMAGEX_PRESERVE_DIR_STRUCTURE_OPTION,
182 IMAGEX_REBUILD_OPTION,
183 IMAGEX_RECOMPRESS_OPTION,
184 IMAGEX_RECURSIVE_OPTION,
186 IMAGEX_RESUME_OPTION,
189 IMAGEX_SOURCE_LIST_OPTION,
190 IMAGEX_STAGING_DIR_OPTION,
191 IMAGEX_STREAMS_INTERFACE_OPTION,
192 IMAGEX_STRICT_ACLS_OPTION,
193 IMAGEX_SYMLINK_OPTION,
194 IMAGEX_THREADS_OPTION,
195 IMAGEX_TO_STDOUT_OPTION,
196 IMAGEX_UNIX_DATA_OPTION,
197 IMAGEX_UPDATE_OF_OPTION,
198 IMAGEX_VERBOSE_OPTION,
199 IMAGEX_WIMBOOT_OPTION,
200 IMAGEX_WIMBOOT_CONFIG_OPTION,
204 static const struct option apply_options[] = {
205 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
206 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
207 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
208 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
209 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
210 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
211 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
212 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
213 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
214 {T("no-attributes"), no_argument, NULL, IMAGEX_NO_ATTRIBUTES_OPTION},
215 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
216 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
217 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
219 /* --resume is undocumented for now as it needs improvement. */
220 {T("resume"), no_argument, NULL, IMAGEX_RESUME_OPTION},
221 {T("wimboot"), no_argument, NULL, IMAGEX_WIMBOOT_OPTION},
225 static const struct option capture_or_append_options[] = {
226 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
227 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
228 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
229 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
230 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
231 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
232 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
233 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
234 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
235 {T("pack-compress"), required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
236 {T("solid-compress"),required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
237 {T("pack-streams"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
238 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
239 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
240 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
241 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
242 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
243 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
244 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
245 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
246 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
247 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
248 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
249 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
250 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
251 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
252 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
253 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
254 {T("update-of"), required_argument, NULL, IMAGEX_UPDATE_OF_OPTION},
255 {T("delta-from"), required_argument, NULL, IMAGEX_DELTA_FROM_OPTION},
256 {T("wimboot"), no_argument, NULL, IMAGEX_WIMBOOT_OPTION},
260 static const struct option delete_options[] = {
261 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
262 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
266 static const struct option dir_options[] = {
267 {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
268 {T("detailed"), no_argument, NULL, IMAGEX_DETAILED_OPTION},
269 {T("one-file-only"), no_argument, NULL, IMAGEX_ONE_FILE_ONLY_OPTION},
273 static const struct option export_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("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
279 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
280 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
281 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
282 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
283 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
284 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
285 {T("pack-compress"), required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
286 {T("solid-compress"),required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
287 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
288 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
289 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
290 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
291 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
295 static const struct option extract_options[] = {
296 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
297 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
298 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
299 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
300 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
301 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
302 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
303 {T("no-attributes"), no_argument, NULL, IMAGEX_NO_ATTRIBUTES_OPTION},
304 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
305 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
306 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
307 {T("no-wildcards"), no_argument, NULL, IMAGEX_NO_WILDCARDS_OPTION},
308 {T("nullglob"), no_argument, NULL, IMAGEX_NULLGLOB_OPTION},
309 {T("preserve-dir-structure"), no_argument, NULL, IMAGEX_PRESERVE_DIR_STRUCTURE_OPTION},
310 {T("wimboot"), no_argument, NULL, IMAGEX_WIMBOOT_OPTION},
314 static const struct option info_options[] = {
315 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
316 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
317 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
318 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
319 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
320 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
321 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
322 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
323 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
327 static const struct option join_options[] = {
328 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
332 static const struct option mount_options[] = {
333 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
334 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
335 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
336 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
337 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
338 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
339 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
343 static const struct option optimize_options[] = {
344 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
345 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
346 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
347 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
348 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
349 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
350 {T("recompress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
351 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
352 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
353 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
354 {T("pack-compress"), required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
355 {T("solid-compress"),required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
356 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
357 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
358 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
359 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
360 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
364 static const struct option split_options[] = {
365 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
369 static const struct option unmount_options[] = {
370 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
371 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
372 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
373 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
374 {T("new-image"), no_argument, NULL, IMAGEX_NEW_IMAGE_OPTION},
378 static const struct option update_options[] = {
379 /* Careful: some of the options here set the defaults for update
380 * commands, but the flags given to an actual update command (and not to
381 * `imagex update' itself are also handled in
382 * update_command_add_option(). */
383 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
384 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
385 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
386 {T("command"), required_argument, NULL, IMAGEX_COMMAND_OPTION},
387 {T("wimboot-config"), required_argument, NULL, IMAGEX_WIMBOOT_CONFIG_OPTION},
389 /* Default delete options */
390 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
391 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
393 /* Global add option */
394 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
396 /* Default add options */
397 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
398 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
399 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
400 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
401 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
402 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
403 {T("no-replace"), no_argument, NULL, IMAGEX_NO_REPLACE_OPTION},
409 # define _format_attribute(type, format_str, args_start) \
410 __attribute__((format(type, format_str, args_start)))
412 # define _format_attribute(type, format_str, args_start)
415 /* Print formatted error message to stderr. */
416 static void _format_attribute(printf, 1, 2)
417 imagex_error(const tchar *format, ...)
420 va_start(va, format);
421 tfputs(T("ERROR: "), stderr);
422 tvfprintf(stderr, format, va);
423 tputc(T('\n'), stderr);
427 /* Print formatted error message to stderr. */
428 static void _format_attribute(printf, 1, 2)
429 imagex_error_with_errno(const tchar *format, ...)
431 int errno_save = errno;
433 va_start(va, format);
434 tfputs(T("ERROR: "), stderr);
435 tvfprintf(stderr, format, va);
436 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
441 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
443 if (image == WIMLIB_NO_IMAGE) {
444 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
445 " Please specify a 1-based image index or "
446 "image name. To list the images\n"
447 " contained in the WIM archive, run\n"
449 " %"TS" \"%"TS"\"\n"),
450 image_name, wim_name,
451 get_cmd_string(CMD_INFO, false), wim_name);
452 return WIMLIB_ERR_INVALID_IMAGE;
458 verify_image_is_single(int image)
460 if (image == WIMLIB_ALL_IMAGES) {
461 imagex_error(T("Cannot specify all images for this action!"));
462 return WIMLIB_ERR_INVALID_IMAGE;
468 verify_image_exists_and_is_single(int image, const tchar *image_name,
469 const tchar *wim_name)
472 ret = verify_image_exists(image, image_name, wim_name);
474 ret = verify_image_is_single(image);
478 /* Parse the argument to --compress */
480 get_compression_type(const tchar *optarg)
482 if (!tstrcasecmp(optarg, T("maximum")) ||
483 !tstrcasecmp(optarg, T("lzx")) ||
484 !tstrcasecmp(optarg, T("max")))
485 return WIMLIB_COMPRESSION_TYPE_LZX;
486 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
487 return WIMLIB_COMPRESSION_TYPE_XPRESS;
488 else if (!tstrcasecmp(optarg, T("recovery")) || !tstrcasecmp(optarg, T("lzms")))
489 return WIMLIB_COMPRESSION_TYPE_LZMS;
490 else if (!tstrcasecmp(optarg, T("none")))
491 return WIMLIB_COMPRESSION_TYPE_NONE;
493 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
494 "\"maximum\", \"fast\", or \"none\"."), optarg);
495 return WIMLIB_COMPRESSION_TYPE_INVALID;
500 set_compress_slow(void)
502 static const struct wimlib_lzx_compressor_params lzx_slow_params = {
504 .size = sizeof(struct wimlib_lzx_compressor_params),
506 .algorithm = WIMLIB_LZX_ALGORITHM_SLOW,
509 .use_len2_matches = 1,
510 .nice_match_length = 96,
511 .num_optim_passes = 4,
512 .max_search_depth = 100,
513 .max_matches_per_pos = 10,
514 .main_nostat_cost = 15,
515 .len_nostat_cost = 15,
516 .aligned_nostat_cost = 7,
521 static const struct wimlib_lzms_compressor_params lzms_slow_params = {
523 .size = sizeof(struct wimlib_lzms_compressor_params),
525 .min_match_length = 2,
526 .max_match_length = UINT32_MAX,
527 .nice_match_length = 96,
528 .max_search_depth = 100,
529 .max_matches_per_pos = 10,
530 .optim_array_length = 1024,
533 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
534 &lzx_slow_params.hdr);
536 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZMS,
537 &lzms_slow_params.hdr);
541 const tchar **strings;
542 unsigned num_strings;
543 unsigned num_alloc_strings;
546 #define STRING_SET_INITIALIZER \
547 { .strings = NULL, .num_strings = 0, .num_alloc_strings = 0, }
549 #define STRING_SET(_strings) \
550 struct string_set _strings = STRING_SET_INITIALIZER
553 string_set_append(struct string_set *set, const tchar *glob)
555 unsigned num_alloc_strings = set->num_alloc_strings;
557 if (set->num_strings == num_alloc_strings) {
558 const tchar **new_strings;
560 num_alloc_strings += 4;
561 new_strings = realloc(set->strings,
562 sizeof(set->strings[0]) * num_alloc_strings);
564 imagex_error(T("Out of memory!"));
567 set->strings = new_strings;
568 set->num_alloc_strings = num_alloc_strings;
570 set->strings[set->num_strings++] = glob;
575 string_set_destroy(struct string_set *set)
581 wim_reference_globs(WIMStruct *wim, struct string_set *set, int open_flags)
583 return wimlib_reference_resource_files(wim, set->strings,
585 WIMLIB_REF_FLAG_GLOB_ENABLE,
587 imagex_progress_func);
591 do_resource_not_found_warning(const tchar *wimfile,
592 const struct wimlib_wim_info *info,
593 const struct string_set *refglobs)
595 if (info->total_parts > 1) {
596 if (refglobs->num_strings == 0) {
597 imagex_error(T("\"%"TS"\" is part of a split WIM. "
598 "Use --ref to specify the other parts."),
601 imagex_error(T("Perhaps the '--ref' argument did not "
602 "specify all other parts of the split "
606 imagex_error(T("If this is a delta WIM, use the --ref argument "
607 "to specify the WIM(s) on which it is based."));
611 /* Returns the size of a file given its name, or -1 if the file does not exist
612 * or its size cannot be determined. */
614 file_get_size(const tchar *filename)
617 if (tstat(filename, &st) == 0)
624 PARSE_STRING_SUCCESS = 0,
625 PARSE_STRING_FAILURE = 1,
626 PARSE_STRING_NONE = 2,
630 * Parses a string token from an array of characters.
632 * Tokens are either whitespace-delimited, or double or single-quoted.
634 * @line_p: Pointer to the pointer to the line of data. Will be updated
635 * to point past the string token iff the return value is
636 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
639 * @len_p: @len_p initially stores the length of the line of data, which may
640 * be 0, and it will be updated to the number of bytes remaining in
641 * the line iff the return value is PARSE_STRING_SUCCESS.
643 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
644 * parsed string token will be returned here.
646 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
647 * PARSE_STRING_FAILURE if the data was invalid due to a missing
648 * closing quote; or PARSE_STRING_NONE if the line ended before the
649 * beginning of a string token was found.
652 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
655 tchar *line = *line_p;
659 /* Skip leading whitespace */
662 return PARSE_STRING_NONE;
663 if (!istspace(*line) && *line != T('\0'))
669 if (quote_char == T('"') || quote_char == T('\'')) {
674 line = tmemchr(line, quote_char, len);
676 imagex_error(T("Missing closing quote: %"TS), fn - 1);
677 return PARSE_STRING_FAILURE;
680 /* Unquoted string. Go until whitespace. Line is terminated
681 * by '\0', so no need to check 'len'. */
685 } while (!istspace(*line) && *line != T('\0'));
692 return PARSE_STRING_SUCCESS;
695 /* Parses a line of data (not an empty line or comment) in the source list file
696 * format. (See the man page for 'wimlib-imagex capture' for details on this
697 * format and the meaning.)
699 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
700 * len == 0. The data in @line will be modified by this function call.
702 * @len: Length of the line of data.
704 * @source: On success, the capture source and target described by the line is
705 * written into this destination. Note that it will contain pointers
706 * to data in the @line array.
708 * Returns true if the line was valid; false otherwise. */
710 parse_source_list_line(tchar *line, size_t len,
711 struct wimlib_capture_source *source)
715 ret = parse_string(&line, &len, &source->fs_source_path);
716 if (ret != PARSE_STRING_SUCCESS)
718 ret = parse_string(&line, &len, &source->wim_target_path);
719 if (ret == PARSE_STRING_NONE)
720 source->wim_target_path = source->fs_source_path;
721 return ret != PARSE_STRING_FAILURE;
724 /* Returns %true if the given line of length @len > 0 is a comment or empty line
725 * in the source list file format. */
727 is_comment_line(const tchar *line, size_t len)
730 if (*line == T('#') || *line == T(';'))
732 if (!istspace(*line) && *line != T('\0'))
742 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
745 tchar *contents = *contents_p;
746 size_t nchars = *nchars_p;
749 for (i = 0; i < nchars; i++)
750 if (contents[i] == T('\n'))
753 /* Handle last line not terminated by a newline */
754 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
755 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
757 imagex_error(T("Out of memory!"));
760 contents[nchars] = T('\n');
761 *contents_p = contents;
769 /* Parses a file in the source list format. (See the man page for
770 * 'wimlib-imagex capture' for details on this format and the meaning.)
772 * @source_list_contents: Contents of the source list file. Note that this
773 * buffer will be modified to save memory allocations,
774 * and cannot be freed until the returned array of
775 * wimlib_capture_source's has also been freed.
777 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
780 * @nsources_ret: On success, the length of the returned array is
783 * Returns: An array of `struct wimlib_capture_source's that can be passed to
784 * the wimlib_add_image_multisource() function to specify how a WIM image is to
786 static struct wimlib_capture_source *
787 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
788 size_t *nsources_ret)
792 struct wimlib_capture_source *sources;
795 nlines = text_file_count_lines(source_list_contents_p,
796 &source_list_nchars);
800 /* Always allocate at least 1 slot, just in case the implementation of
801 * calloc() returns NULL if 0 bytes are requested. */
802 sources = calloc(nlines ?: 1, sizeof(*sources));
804 imagex_error(T("out of memory"));
807 p = *source_list_contents_p;
809 for (i = 0; i < nlines; i++) {
810 /* XXX: Could use rawmemchr() here instead, but it may not be
811 * available on all platforms. */
812 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
813 size_t len = endp - p + 1;
815 if (!is_comment_line(p, len)) {
816 if (!parse_source_list_line(p, len, &sources[j++])) {
828 /* Reads the contents of a file into memory. */
830 file_get_contents(const tchar *filename, size_t *len_ret)
837 if (tstat(filename, &stbuf) != 0) {
838 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
843 fp = tfopen(filename, T("rb"));
845 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
849 buf = malloc(len ? len : 1);
851 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
852 "contents of file \"%"TS"\""), len, filename);
855 if (fread(buf, 1, len, fp) != len) {
856 imagex_error_with_errno(T("Failed to read %zu bytes from the "
857 "file \"%"TS"\""), len, filename);
871 /* Read standard input until EOF and return the full contents in a malloc()ed
872 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
875 stdin_get_contents(size_t *len_ret)
877 /* stdin can, of course, be a pipe or other non-seekable file, so the
878 * total length of the data cannot be pre-determined */
880 size_t newlen = 1024;
884 char *p = realloc(buf, newlen);
885 size_t bytes_read, bytes_to_read;
887 imagex_error(T("out of memory while reading stdin"));
891 bytes_to_read = newlen - pos;
892 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
894 if (bytes_read != bytes_to_read) {
899 imagex_error_with_errno(T("error reading stdin"));
913 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
916 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
918 *num_tchars_ret = num_bytes;
920 #else /* !__WIN32__ */
921 /* On Windows, translate the text to UTF-16LE */
925 if (num_bytes >= 2 &&
926 (((unsigned char)text[0] == 0xff && (unsigned char)text[1] == 0xfe) ||
927 ((unsigned char)text[0] <= 0x7f && (unsigned char)text[1] == 0x00)))
929 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
930 * with something that looks like an ASCII character encoded as
931 * a UTF-16LE code unit. Assume the file is encoded as
932 * UTF-16LE. This is not a 100% reliable check. */
933 num_wchars = num_bytes / 2;
934 text_wstr = (wchar_t*)text;
936 /* File does not look like UTF-16LE. Assume it is encoded in
937 * the current Windows code page. I think these are always
938 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
939 * should work as expected. */
940 text_wstr = win32_mbs_to_wcs(text,
945 *num_tchars_ret = num_wchars;
947 #endif /* __WIN32__ */
951 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
956 contents = file_get_contents(filename, &num_bytes);
959 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
963 stdin_get_text_contents(size_t *num_tchars_ret)
968 contents = stdin_get_contents(&num_bytes);
971 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
974 #define TO_PERCENT(numerator, denominator) \
975 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
977 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
978 #define MEBIBYTE_MIN_NBYTES 10000000ULL
979 #define KIBIBYTE_MIN_NBYTES 10000ULL
982 get_unit(uint64_t total_bytes, const tchar **name_ret)
984 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
985 *name_ret = T("GiB");
987 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
988 *name_ret = T("MiB");
990 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
991 *name_ret = T("KiB");
994 *name_ret = T("bytes");
999 static struct wimlib_progress_info_scan last_scan_progress;
1002 report_scan_progress(const struct wimlib_progress_info_scan *scan, bool done)
1004 uint64_t prev_count, cur_count;
1006 prev_count = last_scan_progress.num_nondirs_scanned +
1007 last_scan_progress.num_dirs_scanned;
1008 cur_count = scan->num_nondirs_scanned + scan->num_dirs_scanned;
1010 if (done || prev_count == 0 || cur_count >= prev_count + 100 ||
1011 cur_count % 128 == 0)
1013 unsigned unit_shift;
1014 const tchar *unit_name;
1016 unit_shift = get_unit(scan->num_bytes_scanned, &unit_name);
1017 imagex_printf(T("\r%"PRIu64" %"TS" scanned (%"PRIu64" files, "
1018 "%"PRIu64" directories) "),
1019 scan->num_bytes_scanned >> unit_shift,
1021 scan->num_nondirs_scanned,
1022 scan->num_dirs_scanned);
1023 last_scan_progress = *scan;
1027 /* Progress callback function passed to various wimlib functions. */
1029 imagex_progress_func(enum wimlib_progress_msg msg,
1030 const union wimlib_progress_info *info)
1032 unsigned percent_done;
1033 unsigned unit_shift;
1034 const tchar *unit_name;
1036 if (imagex_be_quiet)
1039 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1041 static bool first = true;
1043 imagex_printf(T("Writing %"TS"-compressed data "
1044 "using %u thread%"TS"\n"),
1045 wimlib_get_compression_type_string(
1046 info->write_streams.compression_type),
1047 info->write_streams.num_threads,
1048 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1052 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1053 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1054 info->write_streams.total_bytes);
1056 imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1057 "written (%u%% done)"),
1058 info->write_streams.completed_bytes >> unit_shift,
1060 info->write_streams.total_bytes >> unit_shift,
1063 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1064 imagex_printf(T("\n"));
1066 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1067 imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
1068 if (WIMLIB_IS_WIM_ROOT_PATH(info->scan.wim_target_path)) {
1069 imagex_printf(T("\n"));
1071 imagex_printf(T(" (loading as WIM path: \"%"TS"\")...\n"),
1072 info->scan.wim_target_path);
1074 memset(&last_scan_progress, 0, sizeof(last_scan_progress));
1076 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1077 switch (info->scan.status) {
1078 case WIMLIB_SCAN_DENTRY_OK:
1079 report_scan_progress(&info->scan, false);
1081 case WIMLIB_SCAN_DENTRY_EXCLUDED:
1082 imagex_printf(T("\nExcluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1084 case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
1085 imagex_printf(T("\nWARNING: Excluding unsupported file or directory\n"
1086 " \"%"TS"\" from capture\n"), info->scan.cur_path);
1088 case WIMLIB_SCAN_DENTRY_EXCLUDED_SYMLINK:
1089 imagex_printf(T("\nWARNING: Ignoring absolute symbolic link "
1090 "with out-of-tree target:\n"
1091 " \"%"TS"\" => \"%"TS"\"\n"
1092 " (Use --norpfix to capture "
1093 "absolute symbolic links as-is)\n"),
1094 info->scan.cur_path, info->scan.symlink_target);
1098 case WIMLIB_PROGRESS_MSG_SCAN_END:
1099 report_scan_progress(&info->scan, true);
1100 imagex_printf(T("\n"));
1102 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1103 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1104 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1105 info->integrity.total_bytes);
1106 imagex_printf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1107 "of %"PRIu64" %"TS" (%u%%) done"),
1108 info->integrity.filename,
1109 info->integrity.completed_bytes >> unit_shift,
1111 info->integrity.total_bytes >> unit_shift,
1114 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1115 imagex_printf(T("\n"));
1117 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1118 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1119 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1120 info->integrity.total_bytes);
1121 imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1122 "of %"PRIu64" %"TS" (%u%%) done"),
1123 info->integrity.completed_bytes >> unit_shift,
1125 info->integrity.total_bytes >> unit_shift,
1128 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1129 imagex_printf(T("\n"));
1131 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1132 imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1133 "to %"TS" \"%"TS"\"\n"),
1134 info->extract.image,
1135 info->extract.image_name,
1136 info->extract.wimfile_name,
1137 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1138 T("NTFS volume") : T("directory")),
1139 info->extract.target);
1141 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1142 percent_done = TO_PERCENT(info->extract.completed_bytes,
1143 info->extract.total_bytes);
1144 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1145 imagex_printf(T("\rExtracting files: "
1146 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1147 info->extract.completed_bytes >> unit_shift,
1149 info->extract.total_bytes >> unit_shift,
1152 if (info->extract.completed_bytes >= info->extract.total_bytes)
1153 imagex_printf(T("\n"));
1155 case WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN:
1156 if (info->extract.total_parts != 1) {
1157 imagex_printf(T("\nReading split pipable WIM part %u of %u\n"),
1158 info->extract.part_number,
1159 info->extract.total_parts);
1162 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1163 imagex_printf(T("Setting timestamps on all extracted files...\n"));
1165 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1166 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1167 imagex_printf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1168 info->extract.target);
1171 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1172 percent_done = TO_PERCENT(info->split.completed_bytes,
1173 info->split.total_bytes);
1174 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1175 imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
1176 "%"PRIu64" %"TS" (%u%%) written\n"),
1177 info->split.part_name,
1178 info->split.cur_part_number,
1179 info->split.total_parts,
1180 info->split.completed_bytes >> unit_shift,
1182 info->split.total_bytes >> unit_shift,
1186 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1187 if (info->split.completed_bytes == info->split.total_bytes) {
1188 imagex_printf(T("Finished writing split WIM part %u of %u\n"),
1189 info->split.cur_part_number,
1190 info->split.total_parts);
1193 case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1194 switch (info->update.command->op) {
1195 case WIMLIB_UPDATE_OP_DELETE:
1196 imagex_printf(T("Deleted WIM path \"%"TS"\"\n"),
1197 info->update.command->delete_.wim_path);
1199 case WIMLIB_UPDATE_OP_RENAME:
1200 imagex_printf(T("Renamed WIM path \"%"TS"\" => \"%"TS"\"\n"),
1201 info->update.command->rename.wim_source_path,
1202 info->update.command->rename.wim_target_path);
1204 case WIMLIB_UPDATE_OP_ADD:
1209 case WIMLIB_PROGRESS_MSG_REPLACE_FILE_IN_WIM:
1210 imagex_printf(T("Updating \"%"TS"\" in WIM image\n"),
1211 info->replace.path_in_wim);
1213 case WIMLIB_PROGRESS_MSG_WIMBOOT_EXCLUDE:
1214 imagex_printf(T("\nExtracting \"%"TS"\" as normal file (not WIMBoot pointer)\n"),
1215 info->wimboot_exclude.path_in_wim);
1220 fflush(imagex_info_file);
1225 parse_num_threads(const tchar *optarg)
1228 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1229 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1230 imagex_error(T("Number of threads must be a non-negative integer!"));
1237 static uint32_t parse_chunk_size(const tchar *optarg)
1240 unsigned long chunk_size = tstrtoul(optarg, &tmp, 10);
1241 if (chunk_size >= UINT32_MAX || *tmp || tmp == optarg) {
1242 imagex_error(T("Chunk size must be a non-negative integer!"));
1251 * Parse an option passed to an update command.
1253 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1256 * @option: Text string for the option (beginning with --)
1258 * @cmd: `struct wimlib_update_command' that is being constructed for
1261 * Returns true if the option was recognized; false if not.
1264 update_command_add_option(int op, const tchar *option,
1265 struct wimlib_update_command *cmd)
1267 bool recognized = true;
1269 case WIMLIB_UPDATE_OP_ADD:
1270 if (!tstrcmp(option, T("--verbose")))
1271 cmd->add.add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
1272 else if (!tstrcmp(option, T("--unix-data")))
1273 cmd->add.add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
1274 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1275 cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
1276 else if (!tstrcmp(option, T("--strict-acls")))
1277 cmd->add.add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
1278 else if (!tstrcmp(option, T("--dereference")))
1279 cmd->add.add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
1280 else if (!tstrcmp(option, T("--no-replace")))
1281 cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_REPLACE;
1285 case WIMLIB_UPDATE_OP_DELETE:
1286 if (!tstrcmp(option, T("--force")))
1287 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1288 else if (!tstrcmp(option, T("--recursive")))
1289 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1300 /* How many nonoption arguments each `imagex update' command expects */
1301 static const unsigned update_command_num_nonoptions[] = {
1302 [WIMLIB_UPDATE_OP_ADD] = 2,
1303 [WIMLIB_UPDATE_OP_DELETE] = 1,
1304 [WIMLIB_UPDATE_OP_RENAME] = 2,
1308 update_command_add_nonoption(int op, const tchar *nonoption,
1309 struct wimlib_update_command *cmd,
1310 unsigned num_nonoptions)
1313 case WIMLIB_UPDATE_OP_ADD:
1314 if (num_nonoptions == 0)
1315 cmd->add.fs_source_path = (tchar*)nonoption;
1317 cmd->add.wim_target_path = (tchar*)nonoption;
1319 case WIMLIB_UPDATE_OP_DELETE:
1320 cmd->delete_.wim_path = (tchar*)nonoption;
1322 case WIMLIB_UPDATE_OP_RENAME:
1323 if (num_nonoptions == 0)
1324 cmd->rename.wim_source_path = (tchar*)nonoption;
1326 cmd->rename.wim_target_path = (tchar*)nonoption;
1332 * Parse a command passed on stdin to `imagex update'.
1334 * @line: Text of the command.
1335 * @len: Length of the line, including a null terminator
1338 * @command: A `struct wimlib_update_command' to fill in from the parsed
1341 * @line_number: Line number of the command, for diagnostics.
1343 * Returns true on success; returns false on parse error.
1346 parse_update_command(tchar *line, size_t len,
1347 struct wimlib_update_command *command,
1351 tchar *command_name;
1353 size_t num_nonoptions;
1355 /* Get the command name ("add", "delete", "rename") */
1356 ret = parse_string(&line, &len, &command_name);
1357 if (ret != PARSE_STRING_SUCCESS)
1360 if (!tstrcasecmp(command_name, T("add"))) {
1361 op = WIMLIB_UPDATE_OP_ADD;
1362 } else if (!tstrcasecmp(command_name, T("delete"))) {
1363 op = WIMLIB_UPDATE_OP_DELETE;
1364 } else if (!tstrcasecmp(command_name, T("rename"))) {
1365 op = WIMLIB_UPDATE_OP_RENAME;
1367 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1368 command_name, line_number);
1373 /* Parse additional options and non-options as needed */
1378 ret = parse_string(&line, &len, &next_string);
1379 if (ret == PARSE_STRING_NONE) /* End of line */
1381 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1383 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1385 if (!update_command_add_option(op, next_string, command))
1387 imagex_error(T("Unrecognized option \"%"TS"\" to "
1388 "update command \"%"TS"\" on line %zu"),
1389 next_string, command_name, line_number);
1395 if (num_nonoptions == update_command_num_nonoptions[op])
1397 imagex_error(T("Unexpected argument \"%"TS"\" in "
1398 "update command on line %zu\n"
1399 " (The \"%"TS"\" command only "
1400 "takes %zu nonoption arguments!)\n"),
1401 next_string, line_number,
1402 command_name, num_nonoptions);
1405 update_command_add_nonoption(op, next_string,
1406 command, num_nonoptions);
1411 if (num_nonoptions != update_command_num_nonoptions[op]) {
1412 imagex_error(T("Not enough arguments to update command "
1413 "\"%"TS"\" on line %zu"), command_name, line_number);
1419 static struct wimlib_update_command *
1420 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1421 size_t *num_cmds_ret)
1425 struct wimlib_update_command *cmds;
1428 nlines = text_file_count_lines(cmd_file_contents_p,
1433 /* Always allocate at least 1 slot, just in case the implementation of
1434 * calloc() returns NULL if 0 bytes are requested. */
1435 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1437 imagex_error(T("out of memory"));
1440 p = *cmd_file_contents_p;
1442 for (i = 0; i < nlines; i++) {
1443 /* XXX: Could use rawmemchr() here instead, but it may not be
1444 * available on all platforms. */
1445 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1446 size_t len = endp - p + 1;
1448 if (!is_comment_line(p, len)) {
1449 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1460 /* Apply one image, or all images, from a WIM file to a directory, OR apply
1461 * one image from a WIM file to a NTFS volume. */
1463 imagex_apply(int argc, tchar **argv, int cmd)
1467 int image = WIMLIB_NO_IMAGE;
1469 struct wimlib_wim_info info;
1471 const tchar *wimfile;
1472 const tchar *target;
1473 const tchar *image_num_or_name = NULL;
1474 int extract_flags = 0;
1476 STRING_SET(refglobs);
1478 for_opt(c, apply_options) {
1480 case IMAGEX_CHECK_OPTION:
1481 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1483 case IMAGEX_HARDLINK_OPTION:
1484 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1486 case IMAGEX_SYMLINK_OPTION:
1487 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1489 case IMAGEX_VERBOSE_OPTION:
1490 /* No longer does anything. */
1492 case IMAGEX_REF_OPTION:
1493 ret = string_set_append(&refglobs, optarg);
1495 goto out_free_refglobs;
1497 case IMAGEX_UNIX_DATA_OPTION:
1498 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1500 case IMAGEX_NO_ACLS_OPTION:
1501 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1503 case IMAGEX_STRICT_ACLS_OPTION:
1504 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1506 case IMAGEX_NO_ATTRIBUTES_OPTION:
1507 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES;
1509 case IMAGEX_NORPFIX_OPTION:
1510 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1512 case IMAGEX_RPFIX_OPTION:
1513 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1515 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1516 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1517 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1519 case IMAGEX_RESUME_OPTION:
1520 extract_flags |= WIMLIB_EXTRACT_FLAG_RESUME;
1522 case IMAGEX_WIMBOOT_OPTION:
1523 extract_flags |= WIMLIB_EXTRACT_FLAG_WIMBOOT;
1531 if (argc != 2 && argc != 3)
1536 if (!tstrcmp(wimfile, T("-"))) {
1537 /* Attempt to apply pipable WIM from standard input. */
1539 image_num_or_name = NULL;
1542 image_num_or_name = argv[1];
1547 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1548 imagex_progress_func);
1550 goto out_free_refglobs;
1552 wimlib_get_wim_info(wim, &info);
1555 /* Image explicitly specified. */
1556 image_num_or_name = argv[1];
1557 image = wimlib_resolve_image(wim, image_num_or_name);
1558 ret = verify_image_exists(image, image_num_or_name, wimfile);
1560 goto out_wimlib_free;
1563 /* No image specified; default to image 1, but only if the WIM
1564 * contains exactly one image. */
1566 if (info.image_count != 1) {
1567 imagex_error(T("\"%"TS"\" contains %d images; "
1568 "Please select one (or all)."),
1569 wimfile, info.image_count);
1578 if (refglobs.num_strings) {
1580 imagex_error(T("Can't specify --ref when applying from stdin!"));
1582 goto out_wimlib_free;
1584 ret = wim_reference_globs(wim, &refglobs, open_flags);
1586 goto out_wimlib_free;
1591 /* Interpret a regular file or block device target as a NTFS
1595 if (tstat(target, &stbuf)) {
1596 if (errno != ENOENT) {
1597 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1600 goto out_wimlib_free;
1603 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1604 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1610 ret = wimlib_extract_image(wim, image, target, extract_flags,
1611 imagex_progress_func);
1613 set_fd_to_binary_mode(STDIN_FILENO);
1614 ret = wimlib_extract_image_from_pipe(STDIN_FILENO,
1616 target, extract_flags,
1617 imagex_progress_func);
1620 imagex_printf(T("Done applying WIM image.\n"));
1621 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
1623 do_resource_not_found_warning(wimfile, &info, &refglobs);
1625 imagex_error(T( "If you are applying an image "
1626 "from a split pipable WIM,\n"
1627 " make sure you have "
1628 "concatenated together all parts."));
1634 string_set_destroy(&refglobs);
1638 usage(CMD_APPLY, stderr);
1640 goto out_free_refglobs;
1643 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1644 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1645 * the desired image. 'wimlib-imagex append': add a new image to an existing
1648 imagex_capture_or_append(int argc, tchar **argv, int cmd)
1651 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1652 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE |
1653 WIMLIB_ADD_IMAGE_FLAG_WINCONFIG |
1654 WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1655 int write_flags = 0;
1656 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
1657 uint32_t chunk_size = UINT32_MAX;
1658 uint32_t pack_chunk_size = UINT32_MAX;
1659 int pack_ctype = WIMLIB_COMPRESSION_TYPE_INVALID;
1660 const tchar *wimfile;
1664 const tchar *flags_element = NULL;
1667 STRING_SET(base_wimfiles);
1668 WIMStruct **base_wims;
1670 WIMStruct *template_wim;
1671 const tchar *template_wimfile = NULL;
1672 const tchar *template_image_name_or_num = NULL;
1673 int template_image = WIMLIB_NO_IMAGE;
1676 unsigned num_threads = 0;
1681 tchar *config_file = NULL;
1683 bool source_list = false;
1684 size_t source_list_nchars = 0;
1685 tchar *source_list_contents;
1686 bool capture_sources_malloced;
1687 struct wimlib_capture_source *capture_sources;
1689 bool name_defaulted;
1690 bool compress_slow = false;
1692 for_opt(c, capture_or_append_options) {
1694 case IMAGEX_BOOT_OPTION:
1695 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1697 case IMAGEX_CHECK_OPTION:
1698 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1699 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1701 case IMAGEX_NOCHECK_OPTION:
1702 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
1704 case IMAGEX_CONFIG_OPTION:
1705 config_file = optarg;
1706 add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1708 case IMAGEX_COMPRESS_OPTION:
1709 compression_type = get_compression_type(optarg);
1710 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1713 case IMAGEX_COMPRESS_SLOW_OPTION:
1714 compress_slow = true;
1716 case IMAGEX_CHUNK_SIZE_OPTION:
1717 chunk_size = parse_chunk_size(optarg);
1718 if (chunk_size == UINT32_MAX)
1721 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
1722 pack_chunk_size = parse_chunk_size(optarg);
1723 if (pack_chunk_size == UINT32_MAX)
1726 case IMAGEX_PACK_COMPRESS_OPTION:
1727 pack_ctype = get_compression_type(optarg);
1728 if (pack_ctype == WIMLIB_COMPRESSION_TYPE_INVALID)
1731 case IMAGEX_PACK_STREAMS_OPTION:
1732 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
1734 case IMAGEX_FLAGS_OPTION:
1735 flags_element = optarg;
1737 case IMAGEX_DEREFERENCE_OPTION:
1738 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1740 case IMAGEX_VERBOSE_OPTION:
1741 /* No longer does anything. */
1743 case IMAGEX_THREADS_OPTION:
1744 num_threads = parse_num_threads(optarg);
1745 if (num_threads == UINT_MAX)
1748 case IMAGEX_REBUILD_OPTION:
1749 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1751 case IMAGEX_UNIX_DATA_OPTION:
1752 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1754 case IMAGEX_SOURCE_LIST_OPTION:
1757 case IMAGEX_NO_ACLS_OPTION:
1758 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1760 case IMAGEX_STRICT_ACLS_OPTION:
1761 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1763 case IMAGEX_RPFIX_OPTION:
1764 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1766 case IMAGEX_NORPFIX_OPTION:
1767 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1769 case IMAGEX_PIPABLE_OPTION:
1770 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1772 case IMAGEX_NOT_PIPABLE_OPTION:
1773 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
1775 case IMAGEX_UPDATE_OF_OPTION:
1776 if (template_image_name_or_num) {
1777 imagex_error(T("'--update-of' can only be "
1778 "specified one time!"));
1782 colon = tstrrchr(optarg, T(':'));
1785 template_wimfile = optarg;
1787 template_image_name_or_num = colon + 1;
1789 template_wimfile = NULL;
1790 template_image_name_or_num = optarg;
1794 case IMAGEX_DELTA_FROM_OPTION:
1795 if (cmd != CMD_CAPTURE) {
1796 imagex_error(T("'--delta-from' is only "
1797 "valid for capture!"));
1800 ret = string_set_append(&base_wimfiles, optarg);
1802 goto out_free_base_wimfiles;
1803 write_flags |= WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS;
1805 case IMAGEX_WIMBOOT_OPTION:
1806 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_WIMBOOT;
1815 if (argc < 2 || argc > 4)
1821 /* Set default compression type and parameters. */
1824 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
1825 /* No compression type specified. Use the default. */
1827 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_WIMBOOT) {
1828 /* With --wimboot, default to XPRESS compression. */
1829 compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
1830 } else if (write_flags & WIMLIB_WRITE_FLAG_PACK_STREAMS) {
1831 /* With --pack-streams or --solid, default to LZMS
1832 * compression. (However, this will not affect packed
1834 compression_type = WIMLIB_COMPRESSION_TYPE_LZMS;
1836 /* Otherwise, default to LZX compression in fast mode.
1838 compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1839 if (!compress_slow && pack_ctype != WIMLIB_COMPRESSION_TYPE_LZX) {
1840 struct wimlib_lzx_compressor_params params = {
1841 .hdr.size = sizeof(params),
1842 .algorithm = WIMLIB_LZX_ALGORITHM_FAST,
1845 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
1852 set_compress_slow();
1854 if (!tstrcmp(wimfile, T("-"))) {
1855 /* Writing captured WIM to standard output. */
1857 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
1858 imagex_error("Can't write a non-pipable WIM to "
1859 "standard output! Specify --pipable\n"
1860 " if you want to create a pipable WIM "
1861 "(but read the docs first).");
1865 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1867 if (cmd == CMD_APPEND) {
1868 imagex_error(T("Using standard output for append does "
1869 "not make sense."));
1872 wim_fd = STDOUT_FILENO;
1874 imagex_info_file = stderr;
1875 set_fd_to_binary_mode(wim_fd);
1878 /* If template image was specified using --update-of=IMAGE rather
1879 * than --update-of=WIMFILE:IMAGE, set the default WIMFILE. */
1880 if (template_image_name_or_num && !template_wimfile) {
1881 if (base_wimfiles.num_strings == 1) {
1882 /* Capturing delta WIM based on single WIM: default to
1884 template_wimfile = base_wimfiles.strings[0];
1885 } else if (cmd == CMD_APPEND) {
1886 /* Appending to WIM: default to WIM being appended to.
1888 template_wimfile = wimfile;
1890 /* Capturing a normal (non-delta) WIM, so the WIM file
1891 * *must* be explicitly specified. */
1892 if (base_wimfiles.num_strings > 1) {
1893 imagex_error(T("For capture of delta WIM "
1894 "based on multiple existing "
1896 " '--update-of' must "
1897 "specify WIMFILE:IMAGE!"));
1899 imagex_error(T("For capture of non-delta WIM, "
1900 "'--update-of' must specify "
1909 name_defaulted = false;
1911 /* Set default name to SOURCE argument, omitting any directory
1912 * prefixes and trailing slashes. This requires making a copy
1913 * of @source. Leave some free characters at the end in case we
1914 * append a number to keep the name unique. */
1915 size_t source_name_len;
1917 source_name_len = tstrlen(source);
1918 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1919 name = tbasename(tstrcpy(source_copy, source));
1920 name_defaulted = true;
1922 /* Image description defaults to NULL if not given. */
1929 /* Set up capture sources in source list mode */
1930 if (source[0] == T('-') && source[1] == T('\0')) {
1931 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1933 source_list_contents = file_get_text_contents(source,
1934 &source_list_nchars);
1936 if (!source_list_contents)
1939 capture_sources = parse_source_list(&source_list_contents,
1942 if (!capture_sources) {
1944 goto out_free_source_list_contents;
1946 capture_sources_malloced = true;
1948 /* Set up capture source in non-source-list mode. */
1949 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1950 capture_sources[0].fs_source_path = source;
1951 capture_sources[0].wim_target_path = WIMLIB_WIM_ROOT_PATH;
1952 capture_sources[0].reserved = 0;
1954 capture_sources_malloced = false;
1955 source_list_contents = NULL;
1958 /* Open the existing WIM, or create a new one. */
1959 if (cmd == CMD_APPEND)
1960 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1961 imagex_progress_func);
1963 ret = wimlib_create_new_wim(compression_type, &wim);
1965 goto out_free_capture_sources;
1967 /* Set chunk size if non-default. */
1968 if (chunk_size != UINT32_MAX) {
1969 ret = wimlib_set_output_chunk_size(wim, chunk_size);
1972 } else if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_WIMBOOT) &&
1973 compression_type == WIMLIB_COMPRESSION_TYPE_XPRESS) {
1974 ret = wimlib_set_output_chunk_size(wim, 4096);
1978 if (pack_ctype != WIMLIB_COMPRESSION_TYPE_INVALID) {
1979 ret = wimlib_set_output_pack_compression_type(wim, pack_ctype);
1983 if (pack_chunk_size != UINT32_MAX) {
1984 ret = wimlib_set_output_pack_chunk_size(wim, pack_chunk_size);
1990 /* Detect if source is regular file or block device and set NTFS volume
1995 if (tstat(source, &stbuf) == 0) {
1996 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1997 imagex_printf(T("Capturing WIM image from NTFS "
1998 "filesystem on \"%"TS"\"\n"), source);
1999 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
2002 if (errno != ENOENT) {
2003 imagex_error_with_errno(T("Failed to stat "
2004 "\"%"TS"\""), source);
2012 /* If the user did not specify an image name, and the basename of the
2013 * source already exists as an image name in the WIM file, append a
2014 * suffix to make it unique. */
2015 if (cmd == CMD_APPEND && name_defaulted) {
2016 unsigned long conflict_idx;
2017 tchar *name_end = tstrchr(name, T('\0'));
2018 for (conflict_idx = 1;
2019 wimlib_image_name_in_use(wim, name);
2022 tsprintf(name_end, T(" (%lu)"), conflict_idx);
2026 /* If capturing a delta WIM, reference resources from the base WIMs
2027 * before adding the new image. */
2028 if (base_wimfiles.num_strings) {
2029 base_wims = calloc(base_wimfiles.num_strings,
2030 sizeof(base_wims[0]));
2031 if (base_wims == NULL) {
2032 imagex_error(T("Out of memory!"));
2037 for (size_t i = 0; i < base_wimfiles.num_strings; i++) {
2038 ret = wimlib_open_wim(base_wimfiles.strings[i],
2039 open_flags, &base_wims[i],
2040 imagex_progress_func);
2042 goto out_free_base_wims;
2046 ret = wimlib_reference_resources(wim, base_wims,
2047 base_wimfiles.num_strings, 0);
2049 goto out_free_base_wims;
2051 if (base_wimfiles.num_strings == 1) {
2052 imagex_printf(T("Capturing delta WIM based on \"%"TS"\"\n"),
2053 base_wimfiles.strings[0]);
2055 imagex_printf(T("Capturing delta WIM based on %u WIMs\n"),
2056 base_wimfiles.num_strings);
2063 /* If capturing or appending as an update of an existing (template) image,
2064 * open the WIM if needed and parse the image index. */
2065 if (template_image_name_or_num) {
2068 if (base_wimfiles.num_strings == 1 &&
2069 template_wimfile == base_wimfiles.strings[0]) {
2070 template_wim = base_wims[0];
2071 } else if (template_wimfile == wimfile) {
2074 ret = wimlib_open_wim(template_wimfile, open_flags,
2075 &template_wim, imagex_progress_func);
2077 goto out_free_base_wims;
2080 template_image = wimlib_resolve_image(template_wim,
2081 template_image_name_or_num);
2083 if (template_image_name_or_num[0] == T('-')) {
2086 struct wimlib_wim_info info;
2088 wimlib_get_wim_info(template_wim, &info);
2089 n = tstrtoul(template_image_name_or_num + 1, &tmp, 10);
2090 if (n >= 1 && n <= info.image_count &&
2092 tmp != template_image_name_or_num + 1)
2094 template_image = info.image_count - (n - 1);
2097 ret = verify_image_exists_and_is_single(template_image,
2098 template_image_name_or_num,
2101 goto out_free_template_wim;
2103 template_wim = NULL;
2106 ret = wimlib_add_image_multisource(wim,
2112 imagex_progress_func);
2114 goto out_free_template_wim;
2116 if (desc || flags_element || template_image_name_or_num) {
2117 /* User provided <DESCRIPTION> or <FLAGS> element, or an image
2118 * on which the added one is to be based has been specified with
2119 * --update-of. Get the index of the image we just
2120 * added, then use it to call the appropriate functions. */
2121 struct wimlib_wim_info info;
2123 wimlib_get_wim_info(wim, &info);
2126 ret = wimlib_set_image_descripton(wim,
2130 goto out_free_template_wim;
2133 if (flags_element) {
2134 ret = wimlib_set_image_flags(wim, info.image_count,
2137 goto out_free_template_wim;
2140 /* Reference template image if the user provided one. */
2141 if (template_image_name_or_num) {
2142 imagex_printf(T("Using image %d "
2143 "from \"%"TS"\" as template\n"),
2144 template_image, template_wimfile);
2145 ret = wimlib_reference_template_image(wim,
2151 goto out_free_template_wim;
2155 /* Write the new WIM or overwrite the existing WIM with the new image
2157 if (cmd == CMD_APPEND) {
2158 ret = wimlib_overwrite(wim, write_flags, num_threads,
2159 imagex_progress_func);
2160 } else if (wimfile) {
2161 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
2162 write_flags, num_threads,
2163 imagex_progress_func);
2165 ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
2166 write_flags, num_threads,
2167 imagex_progress_func);
2169 out_free_template_wim:
2170 /* template_wim may alias base_wims[0] or wim. */
2171 if ((base_wimfiles.num_strings != 1 || template_wim != base_wims[0]) &&
2172 template_wim != wim)
2173 wimlib_free(template_wim);
2175 for (size_t i = 0; i < base_wimfiles.num_strings; i++)
2176 wimlib_free(base_wims[i]);
2180 out_free_capture_sources:
2181 if (capture_sources_malloced)
2182 free(capture_sources);
2183 out_free_source_list_contents:
2184 free(source_list_contents);
2185 out_free_base_wimfiles:
2186 string_set_destroy(&base_wimfiles);
2193 goto out_free_base_wimfiles;
2196 /* Remove image(s) from a WIM. */
2198 imagex_delete(int argc, tchar **argv, int cmd)
2201 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2202 int write_flags = 0;
2203 const tchar *wimfile;
2204 const tchar *image_num_or_name;
2209 for_opt(c, delete_options) {
2211 case IMAGEX_CHECK_OPTION:
2212 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2213 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2215 case IMAGEX_SOFT_OPTION:
2216 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
2227 imagex_error(T("Must specify a WIM file"));
2229 imagex_error(T("Must specify an image"));
2233 image_num_or_name = argv[1];
2235 ret = wimlib_open_wim(wimfile, open_flags, &wim,
2236 imagex_progress_func);
2240 image = wimlib_resolve_image(wim, image_num_or_name);
2242 ret = verify_image_exists(image, image_num_or_name, wimfile);
2244 goto out_wimlib_free;
2246 ret = wimlib_delete_image(wim, image);
2248 imagex_error(T("Failed to delete image from \"%"TS"\""),
2250 goto out_wimlib_free;
2253 ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
2255 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2256 "deleted"), wimfile);
2264 usage(CMD_DELETE, stderr);
2269 struct print_dentry_options {
2274 print_dentry_full_path(const struct wimlib_dir_entry *dentry)
2276 tprintf(T("%"TS"\n"), dentry->full_path);
2279 static const struct {
2282 } file_attr_flags[] = {
2283 {WIMLIB_FILE_ATTRIBUTE_READONLY, T("READONLY")},
2284 {WIMLIB_FILE_ATTRIBUTE_HIDDEN, T("HIDDEN")},
2285 {WIMLIB_FILE_ATTRIBUTE_SYSTEM, T("SYSTEM")},
2286 {WIMLIB_FILE_ATTRIBUTE_DIRECTORY, T("DIRECTORY")},
2287 {WIMLIB_FILE_ATTRIBUTE_ARCHIVE, T("ARCHIVE")},
2288 {WIMLIB_FILE_ATTRIBUTE_DEVICE, T("DEVICE")},
2289 {WIMLIB_FILE_ATTRIBUTE_NORMAL, T("NORMAL")},
2290 {WIMLIB_FILE_ATTRIBUTE_TEMPORARY, T("TEMPORARY")},
2291 {WIMLIB_FILE_ATTRIBUTE_SPARSE_FILE, T("SPARSE_FILE")},
2292 {WIMLIB_FILE_ATTRIBUTE_REPARSE_POINT, T("REPARSE_POINT")},
2293 {WIMLIB_FILE_ATTRIBUTE_COMPRESSED, T("COMPRESSED")},
2294 {WIMLIB_FILE_ATTRIBUTE_OFFLINE, T("OFFLINE")},
2295 {WIMLIB_FILE_ATTRIBUTE_NOT_CONTENT_INDEXED, T("NOT_CONTENT_INDEXED")},
2296 {WIMLIB_FILE_ATTRIBUTE_ENCRYPTED, T("ENCRYPTED")},
2297 {WIMLIB_FILE_ATTRIBUTE_VIRTUAL, T("VIRTUAL")},
2300 #define TIMESTR_MAX 100
2303 timespec_to_string(const struct timespec *spec, tchar *buf)
2305 time_t t = spec->tv_sec;
2308 tstrftime(buf, TIMESTR_MAX, T("%a %b %d %H:%M:%S %Y UTC"), &tm);
2309 buf[TIMESTR_MAX - 1] = '\0';
2313 print_time(const tchar *type, const struct timespec *spec)
2315 tchar timestr[TIMESTR_MAX];
2317 timespec_to_string(spec, timestr);
2319 tprintf(T("%-20"TS"= %"TS"\n"), type, timestr);
2322 static void print_byte_field(const uint8_t field[], size_t len)
2325 tprintf(T("%02hhx"), *field++);
2329 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2331 tputs(T("WIM Information:"));
2332 tputs(T("----------------"));
2333 tprintf(T("Path: %"TS"\n"), wimfile);
2334 tprintf(T("GUID: 0x"));
2335 print_byte_field(info->guid, sizeof(info->guid));
2337 tprintf(T("Version: %u\n"), info->wim_version);
2338 tprintf(T("Image Count: %d\n"), info->image_count);
2339 tprintf(T("Compression: %"TS"\n"),
2340 wimlib_get_compression_type_string(info->compression_type));
2341 tprintf(T("Chunk Size: %"PRIu32" bytes\n"),
2343 tprintf(T("Part Number: %d/%d\n"), info->part_number, info->total_parts);
2344 tprintf(T("Boot Index: %d\n"), info->boot_index);
2345 tprintf(T("Size: %"PRIu64" bytes\n"), info->total_bytes);
2346 tprintf(T("Integrity Info: %"TS"\n"),
2347 info->has_integrity_table ? T("yes") : T("no"));
2348 tprintf(T("Relative path junction: %"TS"\n"),
2349 info->has_rpfix ? T("yes") : T("no"));
2350 tprintf(T("Pipable: %"TS"\n"),
2351 info->pipable ? T("yes") : T("no"));
2356 print_resource(const struct wimlib_resource_entry *resource,
2359 tprintf(T("Hash = 0x"));
2360 print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2363 if (!resource->is_missing) {
2364 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
2365 resource->uncompressed_size);
2366 if (resource->packed) {
2367 tprintf(T("Raw compressed size = %"PRIu64" bytes\n"),
2368 resource->raw_resource_compressed_size);
2370 tprintf(T("Raw offset in WIM = %"PRIu64" bytes\n"),
2371 resource->raw_resource_offset_in_wim);
2373 tprintf(T("Offset in raw = %"PRIu64" bytes\n"),
2376 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
2377 resource->compressed_size);
2379 tprintf(T("Offset in WIM = %"PRIu64" bytes\n"),
2383 tprintf(T("Part Number = %u\n"), resource->part_number);
2384 tprintf(T("Reference Count = %u\n"), resource->reference_count);
2386 tprintf(T("Flags = "));
2387 if (resource->is_compressed)
2388 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED "));
2389 if (resource->is_metadata)
2390 tprintf(T("WIM_RESHDR_FLAG_METADATA "));
2391 if (resource->is_free)
2392 tprintf(T("WIM_RESHDR_FLAG_FREE "));
2393 if (resource->is_spanned)
2394 tprintf(T("WIM_RESHDR_FLAG_SPANNED "));
2395 if (resource->packed)
2396 tprintf(T("WIM_RESHDR_FLAG_PACKED_STREAMS "));
2404 print_lookup_table(WIMStruct *wim)
2406 wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2410 default_print_security_descriptor(const uint8_t *sd, size_t size)
2412 tprintf(T("Security Descriptor = "));
2413 print_byte_field(sd, size);
2418 print_dentry_detailed(const struct wimlib_dir_entry *dentry)
2422 "----------------------------------------------------------------------------\n"));
2423 tprintf(T("Full Path = \"%"TS"\"\n"), dentry->full_path);
2424 if (dentry->dos_name)
2425 tprintf(T("Short Name = \"%"TS"\"\n"), dentry->dos_name);
2426 tprintf(T("Attributes = 0x%08x\n"), dentry->attributes);
2427 for (size_t i = 0; i < ARRAY_LEN(file_attr_flags); i++)
2428 if (file_attr_flags[i].flag & dentry->attributes)
2429 tprintf(T(" FILE_ATTRIBUTE_%"TS" is set\n"),
2430 file_attr_flags[i].name);
2432 if (dentry->security_descriptor) {
2433 print_security_descriptor(dentry->security_descriptor,
2434 dentry->security_descriptor_size);
2437 print_time(T("Creation Time"), &dentry->creation_time);
2438 print_time(T("Last Write Time"), &dentry->last_write_time);
2439 print_time(T("Last Access Time"), &dentry->last_access_time);
2442 if (dentry->attributes & WIMLIB_FILE_ATTRIBUTE_REPARSE_POINT)
2443 tprintf(T("Reparse Tag = 0x%"PRIx32"\n"), dentry->reparse_tag);
2445 tprintf(T("Link Group ID = 0x%016"PRIx64"\n"), dentry->hard_link_group_id);
2446 tprintf(T("Link Count = %"PRIu32"\n"), dentry->num_links);
2448 for (uint32_t i = 0; i <= dentry->num_named_streams; i++) {
2449 if (dentry->streams[i].stream_name) {
2450 tprintf(T("\tData stream \"%"TS"\":\n"),
2451 dentry->streams[i].stream_name);
2453 tprintf(T("\tUnnamed data stream:\n"));
2455 print_resource(&dentry->streams[i].resource, NULL);
2460 print_dentry(const struct wimlib_dir_entry *dentry, void *_options)
2462 const struct print_dentry_options *options = _options;
2463 if (!options->detailed)
2464 print_dentry_full_path(dentry);
2466 print_dentry_detailed(dentry);
2470 /* Print the files contained in an image(s) in a WIM file. */
2472 imagex_dir(int argc, tchar **argv, int cmd)
2474 const tchar *wimfile;
2475 WIMStruct *wim = NULL;
2478 const tchar *path = WIMLIB_WIM_ROOT_PATH;
2480 struct print_dentry_options options = {
2483 int iterate_flags = WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE;
2485 for_opt(c, dir_options) {
2487 case IMAGEX_PATH_OPTION:
2490 case IMAGEX_DETAILED_OPTION:
2491 options.detailed = true;
2493 case IMAGEX_ONE_FILE_ONLY_OPTION:
2494 iterate_flags &= ~WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE;
2504 imagex_error(T("Must specify a WIM file"));
2508 imagex_error(T("Too many arguments"));
2513 ret = wimlib_open_wim(wimfile, 0, &wim, imagex_progress_func);
2518 image = wimlib_resolve_image(wim, argv[1]);
2519 ret = verify_image_exists(image, argv[1], wimfile);
2521 goto out_wimlib_free;
2523 /* No image specified; default to image 1, but only if the WIM
2524 * contains exactly one image. */
2526 struct wimlib_wim_info info;
2528 wimlib_get_wim_info(wim, &info);
2529 if (info.image_count != 1) {
2530 imagex_error(T("\"%"TS"\" contains %d images; Please "
2531 "select one (or all)."),
2532 wimfile, info.image_count);
2539 ret = wimlib_iterate_dir_tree(wim, image, path, iterate_flags,
2540 print_dentry, &options);
2547 usage(CMD_DIR, stderr);
2552 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2555 imagex_export(int argc, tchar **argv, int cmd)
2559 int export_flags = 0;
2560 int write_flags = 0;
2561 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2562 const tchar *src_wimfile;
2563 const tchar *src_image_num_or_name;
2564 const tchar *dest_wimfile;
2566 const tchar *dest_name;
2567 const tchar *dest_desc;
2569 struct wimlib_wim_info src_info;
2570 WIMStruct *dest_wim;
2575 STRING_SET(refglobs);
2576 unsigned num_threads = 0;
2577 uint32_t chunk_size = UINT32_MAX;
2578 uint32_t pack_chunk_size = UINT32_MAX;
2579 int pack_ctype = WIMLIB_COMPRESSION_TYPE_INVALID;
2581 for_opt(c, export_options) {
2583 case IMAGEX_BOOT_OPTION:
2584 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2586 case IMAGEX_CHECK_OPTION:
2587 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2588 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2590 case IMAGEX_NOCHECK_OPTION:
2591 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2593 case IMAGEX_COMPRESS_OPTION:
2594 compression_type = get_compression_type(optarg);
2595 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2598 case IMAGEX_COMPRESS_SLOW_OPTION:
2599 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
2600 set_compress_slow();
2602 case IMAGEX_PACK_STREAMS_OPTION:
2603 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
2605 case IMAGEX_CHUNK_SIZE_OPTION:
2606 chunk_size = parse_chunk_size(optarg);
2607 if (chunk_size == UINT32_MAX)
2610 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
2611 pack_chunk_size = parse_chunk_size(optarg);
2612 if (pack_chunk_size == UINT32_MAX)
2615 case IMAGEX_PACK_COMPRESS_OPTION:
2616 pack_ctype = get_compression_type(optarg);
2617 if (pack_ctype == WIMLIB_COMPRESSION_TYPE_INVALID)
2620 case IMAGEX_REF_OPTION:
2621 ret = string_set_append(&refglobs, optarg);
2623 goto out_free_refglobs;
2625 case IMAGEX_THREADS_OPTION:
2626 num_threads = parse_num_threads(optarg);
2627 if (num_threads == UINT_MAX)
2630 case IMAGEX_REBUILD_OPTION:
2631 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2633 case IMAGEX_PIPABLE_OPTION:
2634 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2636 case IMAGEX_NOT_PIPABLE_OPTION:
2637 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
2645 if (argc < 3 || argc > 5)
2647 src_wimfile = argv[0];
2648 src_image_num_or_name = argv[1];
2649 dest_wimfile = argv[2];
2650 dest_name = (argc >= 4) ? argv[3] : NULL;
2651 dest_desc = (argc >= 5) ? argv[4] : NULL;
2652 ret = wimlib_open_wim(src_wimfile, open_flags, &src_wim,
2653 imagex_progress_func);
2655 goto out_free_refglobs;
2657 wimlib_get_wim_info(src_wim, &src_info);
2659 /* Determine if the destination is an existing file or not. If so, we
2660 * try to append the exported image(s) to it; otherwise, we create a new
2661 * WIM containing the exported image(s). Furthermore, determine if we
2662 * need to write a pipable WIM directly to standard output. */
2664 if (tstrcmp(dest_wimfile, T("-")) == 0) {
2666 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2667 imagex_error("Can't write a non-pipable WIM to "
2668 "standard output! Specify --pipable\n"
2669 " if you want to create a pipable WIM "
2670 "(but read the docs first).");
2672 goto out_free_src_wim;
2675 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2677 dest_wimfile = NULL;
2678 dest_wim_fd = STDOUT_FILENO;
2679 imagex_info_file = stderr;
2680 set_fd_to_binary_mode(dest_wim_fd);
2683 if (dest_wimfile != NULL && tstat(dest_wimfile, &stbuf) == 0) {
2685 /* Destination file exists. */
2687 if (!S_ISREG(stbuf.st_mode)) {
2688 imagex_error(T("\"%"TS"\" is not a regular file"),
2691 goto out_free_src_wim;
2693 ret = wimlib_open_wim(dest_wimfile,
2694 open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2695 &dest_wim, imagex_progress_func);
2697 goto out_free_src_wim;
2699 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2700 /* The user specified a compression type, but we're
2701 * exporting to an existing WIM. Make sure the
2702 * specified compression type is the same as the
2703 * compression type of the existing destination WIM. */
2704 struct wimlib_wim_info dest_info;
2706 wimlib_get_wim_info(dest_wim, &dest_info);
2707 if (compression_type != dest_info.compression_type) {
2708 imagex_error(T("Cannot specify a compression type that is "
2709 "not the same as that used in the "
2710 "destination WIM"));
2712 goto out_free_dest_wim;
2718 if (errno != ENOENT) {
2719 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2722 goto out_free_src_wim;
2725 /* dest_wimfile is not an existing file, so create a new WIM. */
2727 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2728 /* The user did not specify a compression type; default
2729 * to that of the source WIM, unless --pack-streams or
2730 * --solid was specified. */
2732 if (write_flags & WIMLIB_WRITE_FLAG_PACK_STREAMS)
2733 compression_type = WIMLIB_COMPRESSION_TYPE_LZMS;
2735 compression_type = src_info.compression_type;
2737 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2739 goto out_free_src_wim;
2741 /* Use same chunk size if compression type is the same. */
2742 if (compression_type == src_info.compression_type &&
2743 chunk_size == UINT32_MAX)
2744 wimlib_set_output_chunk_size(dest_wim, src_info.chunk_size);
2747 if (chunk_size != UINT32_MAX) {
2748 /* Set destination chunk size. */
2749 ret = wimlib_set_output_chunk_size(dest_wim, chunk_size);
2751 goto out_free_dest_wim;
2753 if (pack_ctype != WIMLIB_COMPRESSION_TYPE_INVALID) {
2754 ret = wimlib_set_output_pack_compression_type(dest_wim, pack_ctype);
2756 goto out_free_dest_wim;
2758 if (pack_chunk_size != UINT32_MAX) {
2759 ret = wimlib_set_output_pack_chunk_size(dest_wim, pack_chunk_size);
2761 goto out_free_dest_wim;
2764 image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2765 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2767 goto out_free_dest_wim;
2769 if (refglobs.num_strings) {
2770 ret = wim_reference_globs(src_wim, &refglobs, open_flags);
2772 goto out_free_dest_wim;
2775 if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
2776 image == WIMLIB_ALL_IMAGES && src_info.boot_index == 0)
2778 imagex_error(T("--boot specified for all-images export, but source WIM "
2779 "has no bootable image."));
2781 goto out_free_dest_wim;
2784 ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2785 dest_desc, export_flags, imagex_progress_func);
2787 if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2788 do_resource_not_found_warning(src_wimfile,
2789 &src_info, &refglobs);
2791 goto out_free_dest_wim;
2795 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2796 imagex_progress_func);
2797 else if (dest_wimfile)
2798 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2799 write_flags, num_threads,
2800 imagex_progress_func);
2802 ret = wimlib_write_to_fd(dest_wim, dest_wim_fd,
2803 WIMLIB_ALL_IMAGES, write_flags,
2804 num_threads, imagex_progress_func);
2806 wimlib_free(dest_wim);
2808 wimlib_free(src_wim);
2810 string_set_destroy(&refglobs);
2814 usage(CMD_EXPORT, stderr);
2817 goto out_free_refglobs;
2820 /* Extract files or directories from a WIM image */
2822 imagex_extract(int argc, tchar **argv, int cmd)
2829 const tchar *wimfile;
2830 const tchar *image_num_or_name;
2831 tchar *dest_dir = T(".");
2832 int extract_flags = WIMLIB_EXTRACT_FLAG_NORPFIX |
2833 WIMLIB_EXTRACT_FLAG_GLOB_PATHS |
2834 WIMLIB_EXTRACT_FLAG_STRICT_GLOB;
2835 int notlist_extract_flags = WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE;
2837 STRING_SET(refglobs);
2839 tchar *root_path = WIMLIB_WIM_ROOT_PATH;
2841 for_opt(c, extract_options) {
2843 case IMAGEX_CHECK_OPTION:
2844 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2846 case IMAGEX_VERBOSE_OPTION:
2847 /* No longer does anything. */
2849 case IMAGEX_REF_OPTION:
2850 ret = string_set_append(&refglobs, optarg);
2852 goto out_free_refglobs;
2854 case IMAGEX_UNIX_DATA_OPTION:
2855 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2857 case IMAGEX_NO_ACLS_OPTION:
2858 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2860 case IMAGEX_STRICT_ACLS_OPTION:
2861 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2863 case IMAGEX_NO_ATTRIBUTES_OPTION:
2864 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES;
2866 case IMAGEX_DEST_DIR_OPTION:
2869 case IMAGEX_TO_STDOUT_OPTION:
2870 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2871 imagex_info_file = stderr;
2872 imagex_be_quiet = true;
2874 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2875 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2876 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2878 case IMAGEX_NO_WILDCARDS_OPTION:
2879 extract_flags &= ~WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2881 case IMAGEX_NULLGLOB_OPTION:
2882 extract_flags &= ~WIMLIB_EXTRACT_FLAG_STRICT_GLOB;
2884 case IMAGEX_PRESERVE_DIR_STRUCTURE_OPTION:
2885 notlist_extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE;
2887 case IMAGEX_WIMBOOT_OPTION:
2888 extract_flags |= WIMLIB_EXTRACT_FLAG_WIMBOOT;
2900 if (!(extract_flags & (WIMLIB_EXTRACT_FLAG_GLOB_PATHS |
2901 WIMLIB_EXTRACT_FLAG_STRICT_GLOB)))
2903 imagex_error(T("Can't combine --no-wildcards and --nullglob!"));
2908 image_num_or_name = argv[1];
2913 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2915 goto out_free_refglobs;
2917 image = wimlib_resolve_image(wim, image_num_or_name);
2918 ret = verify_image_exists_and_is_single(image,
2922 goto out_wimlib_free;
2924 if (refglobs.num_strings) {
2925 ret = wim_reference_globs(wim, &refglobs, open_flags);
2927 goto out_wimlib_free;
2933 extract_flags &= ~WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2936 while (argc != 0 && ret == 0) {
2940 num_paths < argc && argv[num_paths][0] != T('@');
2945 ret = wimlib_extract_paths(wim, image, dest_dir,
2946 (const tchar **)argv,
2948 extract_flags | notlist_extract_flags,
2949 imagex_progress_func);
2953 ret = wimlib_extract_pathlist(wim, image, dest_dir,
2956 imagex_progress_func);
2963 if (!imagex_be_quiet)
2964 imagex_printf(T("Done extracting files.\n"));
2965 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2966 tfprintf(stderr, T("Note: You can use `%"TS"' to see what "
2967 "files and directories\n"
2968 " are in the WIM image.\n"),
2969 get_cmd_string(CMD_DIR, false));
2970 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2971 struct wimlib_wim_info info;
2973 wimlib_get_wim_info(wim, &info);
2974 do_resource_not_found_warning(wimfile, &info, &refglobs);
2979 string_set_destroy(&refglobs);
2983 usage(CMD_EXTRACT, stderr);
2986 goto out_free_refglobs;
2989 /* Prints information about a WIM file; also can mark an image as bootable,
2990 * change the name of an image, or change the description of an image. */
2992 imagex_info(int argc, tchar **argv, int cmd)
2997 bool nocheck = false;
2998 bool header = false;
2999 bool lookup_table = false;
3001 bool short_header = true;
3002 const tchar *xml_out_file = NULL;
3003 const tchar *wimfile;
3004 const tchar *image_num_or_name;
3005 const tchar *new_name;
3006 const tchar *new_desc;
3011 struct wimlib_wim_info info;
3013 for_opt(c, info_options) {
3015 case IMAGEX_BOOT_OPTION:
3018 case IMAGEX_CHECK_OPTION:
3021 case IMAGEX_NOCHECK_OPTION:
3024 case IMAGEX_HEADER_OPTION:
3026 short_header = false;
3028 case IMAGEX_LOOKUP_TABLE_OPTION:
3029 lookup_table = true;
3030 short_header = false;
3032 case IMAGEX_XML_OPTION:
3034 short_header = false;
3036 case IMAGEX_EXTRACT_XML_OPTION:
3037 xml_out_file = optarg;
3038 short_header = false;
3040 case IMAGEX_METADATA_OPTION:
3041 imagex_error(T("The --metadata option has been removed. "
3042 "Use 'wimdir --detail' instead."));
3051 if (argc < 1 || argc > 4)
3055 image_num_or_name = (argc >= 2) ? argv[1] : T("all");
3056 new_name = (argc >= 3) ? argv[2] : NULL;
3057 new_desc = (argc >= 4) ? argv[3] : NULL;
3059 if (check && nocheck) {
3060 imagex_error(T("Can't specify both --check and --nocheck"));
3065 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3067 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3071 wimlib_get_wim_info(wim, &info);
3073 image = wimlib_resolve_image(wim, image_num_or_name);
3074 ret = WIMLIB_ERR_INVALID_IMAGE;
3075 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
3076 verify_image_exists(image, image_num_or_name, wimfile);
3078 imagex_error(T("If you would like to set the boot "
3079 "index to 0, specify image \"0\" with "
3080 "the --boot flag."));
3082 goto out_wimlib_free;
3085 if (boot && info.image_count == 0) {
3086 imagex_error(T("--boot is meaningless on a WIM with no images"));
3087 goto out_wimlib_free;
3090 if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
3092 imagex_error(T("Cannot specify the --boot flag "
3093 "without specifying a specific "
3094 "image in a multi-image WIM"));
3095 goto out_wimlib_free;
3098 imagex_error(T("Cannot specify the NEW_NAME "
3099 "without specifying a specific "
3100 "image in a multi-image WIM"));
3101 goto out_wimlib_free;
3105 /* Operations that print information are separated from operations that
3106 * recreate the WIM file. */
3107 if (!new_name && !boot) {
3109 /* Read-only operations */
3111 if (image == WIMLIB_NO_IMAGE) {
3112 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
3113 image_num_or_name, wimfile);
3114 goto out_wimlib_free;
3117 if (image == WIMLIB_ALL_IMAGES && short_header)
3118 print_wim_information(wimfile, &info);
3121 wimlib_print_header(wim);
3124 if (info.total_parts != 1) {
3125 tfprintf(stderr, T("Warning: Only showing the lookup table "
3126 "for part %d of a %d-part WIM.\n"),
3127 info.part_number, info.total_parts);
3129 print_lookup_table(wim);
3133 ret = wimlib_extract_xml_data(wim, stdout);
3135 goto out_wimlib_free;
3141 fp = tfopen(xml_out_file, T("wb"));
3143 imagex_error_with_errno(T("Failed to open the "
3144 "file \"%"TS"\" for "
3148 goto out_wimlib_free;
3150 ret = wimlib_extract_xml_data(wim, fp);
3152 imagex_error(T("Failed to close the file "
3158 goto out_wimlib_free;
3162 wimlib_print_available_images(wim, image);
3167 /* Modification operations */
3169 if (image == WIMLIB_ALL_IMAGES)
3172 if (image == WIMLIB_NO_IMAGE && new_name) {
3173 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
3174 "when using image 0"), new_name);
3176 goto out_wimlib_free;
3180 if (image == info.boot_index) {
3181 imagex_printf(T("Image %d is already marked as "
3182 "bootable.\n"), image);
3185 imagex_printf(T("Marking image %d as bootable.\n"),
3187 info.boot_index = image;
3188 ret = wimlib_set_wim_info(wim, &info,
3189 WIMLIB_CHANGE_BOOT_INDEX);
3191 goto out_wimlib_free;
3195 if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
3197 imagex_printf(T("Image %d is already named \"%"TS"\".\n"),
3201 imagex_printf(T("Changing the name of image %d to "
3202 "\"%"TS"\".\n"), image, new_name);
3203 ret = wimlib_set_image_name(wim, image, new_name);
3205 goto out_wimlib_free;
3209 const tchar *old_desc;
3210 old_desc = wimlib_get_image_description(wim, image);
3211 if (old_desc && !tstrcmp(old_desc, new_desc)) {
3212 imagex_printf(T("The description of image %d is already "
3213 "\"%"TS"\".\n"), image, new_desc);
3216 imagex_printf(T("Changing the description of image %d "
3217 "to \"%"TS"\".\n"), image, new_desc);
3218 ret = wimlib_set_image_descripton(wim, image,
3221 goto out_wimlib_free;
3225 /* Only call wimlib_overwrite() if something actually needs to
3227 if (boot || new_name || new_desc ||
3228 (check && !info.has_integrity_table) ||
3229 (nocheck && info.has_integrity_table))
3231 int write_flags = 0;
3234 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3236 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3237 ret = wimlib_overwrite(wim, write_flags, 1,
3238 imagex_progress_func);
3240 imagex_printf(T("The file \"%"TS"\" was not modified "
3241 "because nothing needed to be done.\n"),
3252 usage(CMD_INFO, stderr);
3258 /* Join split WIMs into one part WIM */
3260 imagex_join(int argc, tchar **argv, int cmd)
3263 int swm_open_flags = 0;
3264 int wim_write_flags = 0;
3265 const tchar *output_path;
3268 for_opt(c, join_options) {
3270 case IMAGEX_CHECK_OPTION:
3271 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3272 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3282 imagex_error(T("Must specify one or more split WIM (.swm) "
3286 output_path = argv[0];
3287 ret = wimlib_join((const tchar * const *)++argv,
3292 imagex_progress_func);
3297 usage(CMD_JOIN, stderr);
3302 #if WIM_MOUNTING_SUPPORTED
3304 /* Mounts a WIM image. */
3306 imagex_mount_rw_or_ro(int argc, tchar **argv, int cmd)
3309 int mount_flags = 0;
3311 const tchar *staging_dir = NULL;
3312 const tchar *wimfile;
3315 struct wimlib_wim_info info;
3319 STRING_SET(refglobs);
3321 if (cmd == CMD_MOUNTRW) {
3322 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
3323 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3326 for_opt(c, mount_options) {
3328 case IMAGEX_ALLOW_OTHER_OPTION:
3329 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
3331 case IMAGEX_CHECK_OPTION:
3332 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3334 case IMAGEX_DEBUG_OPTION:
3335 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
3337 case IMAGEX_STREAMS_INTERFACE_OPTION:
3338 if (!tstrcasecmp(optarg, T("none")))
3339 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
3340 else if (!tstrcasecmp(optarg, T("xattr")))
3341 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
3342 else if (!tstrcasecmp(optarg, T("windows")))
3343 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
3345 imagex_error(T("Unknown stream interface \"%"TS"\""),
3350 case IMAGEX_REF_OPTION:
3351 ret = string_set_append(&refglobs, optarg);
3353 goto out_free_refglobs;
3355 case IMAGEX_STAGING_DIR_OPTION:
3356 staging_dir = optarg;
3358 case IMAGEX_UNIX_DATA_OPTION:
3359 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
3367 if (argc != 2 && argc != 3)
3372 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3374 goto out_free_refglobs;
3376 wimlib_get_wim_info(wim, &info);
3379 /* Image explicitly specified. */
3380 image = wimlib_resolve_image(wim, argv[1]);
3382 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
3386 /* No image specified; default to image 1, but only if the WIM
3387 * contains exactly one image. */
3389 if (info.image_count != 1) {
3390 imagex_error(T("\"%"TS"\" contains %d images; Please "
3391 "select one."), wimfile, info.image_count);
3399 if (refglobs.num_strings) {
3400 ret = wim_reference_globs(wim, &refglobs, open_flags);
3405 ret = wimlib_mount_image(wim, image, dir, mount_flags, staging_dir);
3407 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
3409 image, wimfile, dir);
3414 string_set_destroy(&refglobs);
3420 goto out_free_refglobs;
3422 #endif /* WIM_MOUNTING_SUPPORTED */
3424 /* Rebuild a WIM file */
3426 imagex_optimize(int argc, tchar **argv, int cmd)
3429 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3430 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
3431 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
3432 uint32_t chunk_size = UINT32_MAX;
3433 uint32_t pack_chunk_size = UINT32_MAX;
3434 int pack_ctype = WIMLIB_COMPRESSION_TYPE_INVALID;
3437 const tchar *wimfile;
3440 unsigned num_threads = 0;
3442 for_opt(c, optimize_options) {
3444 case IMAGEX_CHECK_OPTION:
3445 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3446 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3448 case IMAGEX_NOCHECK_OPTION:
3449 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3451 case IMAGEX_COMPRESS_OPTION:
3452 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3453 compression_type = get_compression_type(optarg);
3454 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
3457 case IMAGEX_RECOMPRESS_OPTION:
3458 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3460 case IMAGEX_COMPRESS_SLOW_OPTION:
3461 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3462 set_compress_slow();
3464 case IMAGEX_CHUNK_SIZE_OPTION:
3465 chunk_size = parse_chunk_size(optarg);
3466 if (chunk_size == UINT32_MAX)
3469 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
3470 pack_chunk_size = parse_chunk_size(optarg);
3471 if (pack_chunk_size == UINT32_MAX)
3474 case IMAGEX_PACK_COMPRESS_OPTION:
3475 pack_ctype = get_compression_type(optarg);
3476 if (pack_ctype == WIMLIB_COMPRESSION_TYPE_INVALID)
3479 case IMAGEX_PACK_STREAMS_OPTION:
3480 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
3481 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3483 case IMAGEX_THREADS_OPTION:
3484 num_threads = parse_num_threads(optarg);
3485 if (num_threads == UINT_MAX)
3488 case IMAGEX_PIPABLE_OPTION:
3489 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
3491 case IMAGEX_NOT_PIPABLE_OPTION:
3492 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
3506 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3510 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
3511 /* Change compression type. */
3512 ret = wimlib_set_output_compression_type(wim, compression_type);
3514 goto out_wimlib_free;
3517 if (chunk_size != UINT32_MAX) {
3518 /* Change chunk size. */
3519 ret = wimlib_set_output_chunk_size(wim, chunk_size);
3521 goto out_wimlib_free;
3523 if (pack_ctype != WIMLIB_COMPRESSION_TYPE_INVALID) {
3524 ret = wimlib_set_output_pack_compression_type(wim, pack_ctype);
3526 goto out_wimlib_free;
3528 if (pack_chunk_size != UINT32_MAX) {
3529 ret = wimlib_set_output_pack_chunk_size(wim, pack_chunk_size);
3531 goto out_wimlib_free;
3534 old_size = file_get_size(wimfile);
3535 tprintf(T("\"%"TS"\" original size: "), wimfile);
3537 tputs(T("Unknown"));
3539 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3541 ret = wimlib_overwrite(wim, write_flags, num_threads,
3542 imagex_progress_func);
3544 imagex_error(T("Optimization of \"%"TS"\" failed."), wimfile);
3545 goto out_wimlib_free;
3548 new_size = file_get_size(wimfile);
3549 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3551 tputs(T("Unknown"));
3553 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3555 tfputs(T("Space saved: "), stdout);
3556 if (new_size != -1 && old_size != -1) {
3557 tprintf(T("%lld KiB\n"),
3558 ((long long)old_size - (long long)new_size) >> 10);
3560 tputs(T("Unknown"));
3569 usage(CMD_OPTIMIZE, stderr);
3575 /* Split a WIM into a spanned set */
3577 imagex_split(int argc, tchar **argv, int cmd)
3581 int write_flags = 0;
3582 unsigned long part_size;
3587 for_opt(c, split_options) {
3589 case IMAGEX_CHECK_OPTION:
3590 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3591 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3603 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3604 if (tmp == argv[2] || *tmp) {
3605 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3606 imagex_error(T("The part size must be an integer or "
3607 "floating-point number of megabytes."));
3610 ret = wimlib_open_wim(argv[0], open_flags, &wim, imagex_progress_func);
3614 ret = wimlib_split(wim, argv[1], part_size, write_flags, imagex_progress_func);
3620 usage(CMD_SPLIT, stderr);
3626 #if WIM_MOUNTING_SUPPORTED
3627 /* Unmounts a mounted WIM image. */
3629 imagex_unmount(int argc, tchar **argv, int cmd)
3632 int unmount_flags = 0;
3635 for_opt(c, unmount_options) {
3637 case IMAGEX_COMMIT_OPTION:
3638 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3640 case IMAGEX_CHECK_OPTION:
3641 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3643 case IMAGEX_REBUILD_OPTION:
3644 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3646 case IMAGEX_LAZY_OPTION:
3647 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3649 case IMAGEX_NEW_IMAGE_OPTION:
3650 unmount_flags |= WIMLIB_UNMOUNT_FLAG_NEW_IMAGE;
3661 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_NEW_IMAGE) {
3662 if (!(unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT)) {
3663 imagex_error(T("--new-image is meaningless "
3664 "without --commit also specified!"));
3667 imagex_printf(T("Committing changes as new image...\n"));
3670 ret = wimlib_unmount_image(argv[0], unmount_flags,
3671 imagex_progress_func);
3673 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3678 usage(CMD_UNMOUNT, stderr);
3683 #endif /* WIM_MOUNTING_SUPPORTED */
3686 * Add, delete, or rename files in a WIM image.
3689 imagex_update(int argc, tchar **argv, int cmd)
3691 const tchar *wimfile;
3695 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3696 int write_flags = 0;
3697 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3698 int default_add_flags = WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
3699 WIMLIB_ADD_FLAG_VERBOSE |
3700 WIMLIB_ADD_FLAG_WINCONFIG;
3701 int default_delete_flags = 0;
3702 unsigned num_threads = 0;
3704 tchar *cmd_file_contents;
3705 size_t cmd_file_nchars;
3706 struct wimlib_update_command *cmds;
3708 tchar *command_str = NULL;
3709 tchar *config_file = NULL;
3710 tchar *wimboot_config = NULL;
3712 for_opt(c, update_options) {
3714 /* Generic or write options */
3715 case IMAGEX_THREADS_OPTION:
3716 num_threads = parse_num_threads(optarg);
3717 if (num_threads == UINT_MAX)
3720 case IMAGEX_CHECK_OPTION:
3721 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3722 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3724 case IMAGEX_REBUILD_OPTION:
3725 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3727 case IMAGEX_COMMAND_OPTION:
3729 imagex_error(T("--command may only be specified "
3730 "one time. Please provide\n"
3731 " the update commands "
3732 "on standard input instead."));
3735 command_str = tstrdup(optarg);
3737 imagex_error(T("Out of memory!"));
3741 case IMAGEX_WIMBOOT_CONFIG_OPTION:
3742 wimboot_config = optarg;
3744 /* Default delete options */
3745 case IMAGEX_FORCE_OPTION:
3746 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3748 case IMAGEX_RECURSIVE_OPTION:
3749 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3752 /* Global add option */
3753 case IMAGEX_CONFIG_OPTION:
3754 default_add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
3755 config_file = optarg;
3758 /* Default add options */
3759 case IMAGEX_VERBOSE_OPTION:
3760 /* No longer does anything. */
3762 case IMAGEX_DEREFERENCE_OPTION:
3763 default_add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
3765 case IMAGEX_UNIX_DATA_OPTION:
3766 default_add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
3768 case IMAGEX_NO_ACLS_OPTION:
3769 default_add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
3771 case IMAGEX_STRICT_ACLS_OPTION:
3772 default_add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
3774 case IMAGEX_NO_REPLACE_OPTION:
3775 default_add_flags |= WIMLIB_ADD_FLAG_NO_REPLACE;
3784 if (argc != 1 && argc != 2)
3788 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3790 goto out_free_command_str;
3793 /* Image explicitly specified. */
3794 image = wimlib_resolve_image(wim, argv[1]);
3795 ret = verify_image_exists_and_is_single(image, argv[1],
3798 goto out_wimlib_free;
3800 /* No image specified; default to image 1, but only if the WIM
3801 * contains exactly one image. */
3802 struct wimlib_wim_info info;
3804 wimlib_get_wim_info(wim, &info);
3805 if (info.image_count != 1) {
3806 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3807 wimfile, info.image_count);
3814 /* Read update commands from standard input, or the command string if
3817 cmd_file_contents = NULL;
3818 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3822 goto out_free_cmd_file_contents;
3824 } else if (!wimboot_config) {
3825 if (isatty(STDIN_FILENO)) {
3826 tputs(T("Reading update commands from standard input..."));
3827 recommend_man_page(CMD_UPDATE, stdout);
3829 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3830 if (!cmd_file_contents) {
3832 goto out_wimlib_free;
3835 /* Parse the update commands */
3836 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3840 goto out_free_cmd_file_contents;
3843 cmd_file_contents = NULL;
3848 /* Set default flags and capture config on the update commands */
3849 for (size_t i = 0; i < num_cmds; i++) {
3850 switch (cmds[i].op) {
3851 case WIMLIB_UPDATE_OP_ADD:
3852 cmds[i].add.add_flags |= default_add_flags;
3853 cmds[i].add.config_file = config_file;
3855 case WIMLIB_UPDATE_OP_DELETE:
3856 cmds[i].delete_.delete_flags |= default_delete_flags;
3863 /* Execute the update commands */
3864 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3865 imagex_progress_func);
3869 if (wimboot_config) {
3870 /* --wimboot-config=FILE is short for an
3871 * "add FILE /Windows/System32/WimBootCompress.ini" command.
3873 struct wimlib_update_command cmd;
3875 cmd.op = WIMLIB_UPDATE_OP_ADD;
3876 cmd.add.fs_source_path = wimboot_config;
3877 cmd.add.wim_target_path = T("/Windows/System32/WimBootCompress.ini");
3878 cmd.add.config_file = NULL;
3879 cmd.add.add_flags = 0;
3881 ret = wimlib_update_image(wim, image, &cmd, 1,
3882 update_flags, imagex_progress_func);
3887 /* Overwrite the updated WIM */
3888 ret = wimlib_overwrite(wim, write_flags, num_threads,
3889 imagex_progress_func);
3892 out_free_cmd_file_contents:
3893 free(cmd_file_contents);
3896 out_free_command_str:
3901 usage(CMD_UPDATE, stderr);
3904 goto out_free_command_str;
3909 struct imagex_command {
3911 int (*func)(int argc, tchar **argv, int cmd);
3914 static const struct imagex_command imagex_commands[] = {
3915 [CMD_APPEND] = {T("append"), imagex_capture_or_append},
3916 [CMD_APPLY] = {T("apply"), imagex_apply},
3917 [CMD_CAPTURE] = {T("capture"), imagex_capture_or_append},
3918 [CMD_DELETE] = {T("delete"), imagex_delete},
3919 [CMD_DIR ] = {T("dir"), imagex_dir},
3920 [CMD_EXPORT] = {T("export"), imagex_export},
3921 [CMD_EXTRACT] = {T("extract"), imagex_extract},
3922 [CMD_INFO] = {T("info"), imagex_info},
3923 [CMD_JOIN] = {T("join"), imagex_join},
3924 #if WIM_MOUNTING_SUPPORTED
3925 [CMD_MOUNT] = {T("mount"), imagex_mount_rw_or_ro},
3926 [CMD_MOUNTRW] = {T("mountrw"), imagex_mount_rw_or_ro},
3928 [CMD_OPTIMIZE] = {T("optimize"), imagex_optimize},
3929 [CMD_SPLIT] = {T("split"), imagex_split},
3930 #if WIM_MOUNTING_SUPPORTED
3931 [CMD_UNMOUNT] = {T("unmount"), imagex_unmount},
3933 [CMD_UPDATE] = {T("update"), imagex_update},
3936 static const tchar *usage_strings[] = {
3939 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3940 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3941 " [--nocheck] [--flags EDITION_ID] [--dereference]\n"
3942 " [--config=FILE] [--threads=NUM_THREADS] [--source-list]\n"
3943 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3944 " [--update-of=[WIMFILE:]IMAGE] [--wimboot]\n"
3948 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME | all)]\n"
3949 " (DIRECTORY | NTFS_VOLUME) [--check] [--ref=\"GLOB\"]\n"
3950 " [--no-acls] [--strict-acls] [--no-attributes]\n"
3951 " [--rpfix] [--norpfix] [--hardlink] [--symlink]\n"
3952 " [--include-invalid-names] [--wimboot]\n"
3956 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3957 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3958 " [--nocheck] [--compress=TYPE] [--flags EDITION_ID]\n"
3959 " [--dereference] [--config=FILE] [--threads=NUM_THREADS]\n"
3960 " [--source-list] [--no-acls] [--strict-acls] [--rpfix]\n"
3961 " [--norpfix] [--update-of=[WIMFILE:]IMAGE]\n"
3962 " [--delta-from=WIMFILE] [--wimboot]\n"
3966 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n"
3967 " [--check] [--soft]\n"
3971 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH] [--detailed]\n"
3975 " %"TS" SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
3976 " DEST_WIMFILE [DEST_IMAGE_NAME [DEST_IMAGE_DESCRIPTION]]\n"
3977 " [--boot] [--check] [--nocheck] [--compress=TYPE]\n"
3978 " [--ref=\"GLOB\"] [--threads=NUM_THREADS] [--rebuild]\n"
3982 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME) [(PATH | @LISTFILE)...]\n"
3983 " [--check] [--ref=\"GLOB\"] [--dest-dir=CMD_DIR]\n"
3984 " [--to-stdout] [--no-acls] [--strict-acls]\n"
3985 " [--no-attributes] [--include-invalid-names]\n"
3986 " [--no-wildcards] [--nullglob] [--preserve-dir-structure]\n"
3990 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME) [NEW_NAME\n"
3991 " [NEW_DESC]]] [--boot] [--check] [--nocheck] [--xml]\n"
3992 " [--extract-xml FILE] [--header] [--lookup-table]\n"
3996 " %"TS" OUT_WIMFILE SPLIT_WIM_PART... [--check]\n"
3998 #if WIM_MOUNTING_SUPPORTED
4001 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
4002 " [--check] [--streams-interface=INTERFACE]\n"
4003 " [--ref=\"GLOB\"] [--allow-other]\n"
4007 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
4008 " [--check] [--streams-interface=INTERFACE]\n"
4009 " [--staging-dir=CMD_DIR] [--allow-other]\n"
4014 " %"TS" WIMFILE [--check] [--nocheck] [--recompress]\n"
4015 " [--recompress-slow] [--compress=TYPE]\n"
4016 " [--threads=NUM_THREADS]\n"
4020 " %"TS" WIMFILE SPLIT_WIM_PART_1 PART_SIZE_MB [--check]\n"
4022 #if WIM_MOUNTING_SUPPORTED
4025 " %"TS" DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
4031 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
4032 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
4033 " [DEFAULT_DELETE_OPTIONS] [--command=STRING]\n"
4034 " [--wimboot-config=FILE| [< CMDFILE]\n"
4038 static const tchar *invocation_name;
4039 static int invocation_cmd = CMD_NONE;
4041 static const tchar *get_cmd_string(int cmd, bool nospace)
4043 static tchar buf[50];
4044 if (cmd == CMD_NONE) {
4045 tsprintf(buf, T("%"TS), T(IMAGEX_PROGNAME));
4046 } else if (invocation_cmd != CMD_NONE) {
4047 tsprintf(buf, T("wim%"TS), imagex_commands[cmd].name);
4049 const tchar *format;
4052 format = T("%"TS"-%"TS"");
4054 format = T("%"TS" %"TS"");
4055 tsprintf(buf, format, invocation_name, imagex_commands[cmd].name);
4063 static const tchar *s =
4065 IMAGEX_PROGNAME " (distributed with " PACKAGE " " PACKAGE_VERSION ")\n"
4066 "Copyright (C) 2012, 2013, 2014 Eric Biggers\n"
4067 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
4068 "This is free software: you are free to change and redistribute it.\n"
4069 "There is NO WARRANTY, to the extent permitted by law.\n"
4071 "Report bugs to "PACKAGE_BUGREPORT".\n"
4078 help_or_version(int argc, tchar **argv, int cmd)
4083 for (i = 1; i < argc; i++) {
4085 if (p[0] == T('-') && p[1] == T('-')) {
4087 if (!tstrcmp(p, T("help"))) {
4088 if (cmd == CMD_NONE)
4093 } else if (!tstrcmp(p, T("version"))) {
4102 print_usage_string(int cmd, FILE *fp)
4104 tfprintf(fp, usage_strings[cmd], get_cmd_string(cmd, false));
4108 recommend_man_page(int cmd, FILE *fp)
4110 const tchar *format_str;
4112 format_str = T("Uncommon options are not listed;\n"
4113 "See %"TS".pdf in the doc directory for more details.\n");
4115 format_str = T("Uncommon options are not listed;\n"
4116 "Try `man %"TS"' for more details.\n");
4118 tfprintf(fp, format_str, get_cmd_string(cmd, true));
4122 usage(int cmd, FILE *fp)
4124 tfprintf(fp, T("Usage:\n"));
4125 print_usage_string(cmd, fp);
4126 tfprintf(fp, T("\n"));
4127 recommend_man_page(cmd, fp);
4133 tfprintf(fp, T("Usage:\n"));
4134 for (int cmd = 0; cmd < CMD_MAX; cmd++) {
4135 print_usage_string(cmd, fp);
4136 tfprintf(fp, T("\n"));
4138 static const tchar *extra =
4141 " %"TS" --version\n"
4143 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
4146 tfprintf(fp, extra, invocation_name, invocation_name);
4147 recommend_man_page(CMD_NONE, fp);
4150 /* Entry point for wimlib's ImageX implementation. On UNIX the command
4151 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
4152 * something else), while an Windows the command arguments will be UTF-16LE
4153 * encoded 'wchar_t' strings. */
4156 wmain(int argc, wchar_t **argv, wchar_t **envp)
4158 main(int argc, char **argv)
4165 imagex_info_file = stdout;
4166 invocation_name = tbasename(argv[0]);
4169 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
4170 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
4174 setlocale(LC_ALL, "");
4175 codeset = nl_langinfo(CODESET);
4176 if (!strstr(codeset, "UTF-8") &&
4177 !strstr(codeset, "UTF8") &&
4178 !strstr(codeset, "utf-8") &&
4179 !strstr(codeset, "utf8"))
4182 "WARNING: Running %"TS" in a UTF-8 locale is recommended!\n"
4183 " Maybe try: `export LANG=en_US.UTF-8'?\n"
4184 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
4185 " to any value to force wimlib to use UTF-8.\n",
4191 #endif /* !__WIN32__ */
4194 tchar *igcase = tgetenv(T("WIMLIB_IMAGEX_IGNORE_CASE"));
4195 if (igcase != NULL) {
4196 if (!tstrcmp(igcase, T("no")) ||
4197 !tstrcmp(igcase, T("0")))
4198 init_flags |= WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE;
4199 else if (!tstrcmp(igcase, T("yes")) ||
4200 !tstrcmp(igcase, T("1")))
4201 init_flags |= WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE;
4204 "WARNING: Ignoring unknown setting of "
4205 "WIMLIB_IMAGEX_IGNORE_CASE\n");
4210 /* Allow being invoked as wimCOMMAND (e.g. wimapply). */
4212 if (!tstrncmp(invocation_name, T("wim"), 3) &&
4213 tstrcmp(invocation_name, T(IMAGEX_PROGNAME))) {
4214 for (int i = 0; i < CMD_MAX; i++) {
4215 if (!tstrcmp(invocation_name + 3,
4216 imagex_commands[i].name))
4225 /* Unless already known from the invocation name, determine which
4226 * command was specified. */
4227 if (cmd == CMD_NONE) {
4229 imagex_error(T("No command specified!\n"));
4233 for (int i = 0; i < CMD_MAX; i++) {
4234 if (!tstrcmp(argv[1], imagex_commands[i].name)) {
4239 if (cmd != CMD_NONE) {
4245 /* Handle --help and --version. --help can be either for the program as
4246 * a whole (cmd == CMD_NONE) or just for a specific command (cmd !=
4247 * CMD_NONE). Note: help_or_version() will not return if a --help or
4248 * --version argument was found. */
4249 help_or_version(argc, argv, cmd);
4251 /* Bail if a valid command was not specified. */
4252 if (cmd == CMD_NONE) {
4253 imagex_error(T("Unrecognized command: `%"TS"'\n"), argv[1]);
4258 /* Enable warning and error messages in wimlib to be more user-friendly.
4260 wimlib_set_print_errors(true);
4262 /* Initialize wimlib. */
4263 ret = wimlib_global_init(init_flags);
4265 goto out_check_status;
4267 /* Call the command handler function. */
4268 ret = imagex_commands[cmd].func(argc, argv, cmd);
4270 /* Check for error writing to standard output, especially since for some
4271 * commands, writing to standard output is part of the program's actual
4272 * behavior and not just for informational purposes. */
4273 if (ferror(stdout) || fclose(stdout)) {
4274 imagex_error_with_errno(T("error writing to standard output"));
4279 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
4280 * itself (not by wimlib). 0 indicates success. > 0 indicates a wimlib
4281 * error code from which an error message can be printed. */
4283 imagex_error(T("Exiting with error code %d:\n"
4285 wimlib_get_error_string(ret));
4286 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
4287 imagex_error_with_errno(T("errno"));
4289 /* Make wimlib free any resources it's holding (although this is not
4290 * strictly necessary because the process is ending anyway). */
4291 wimlib_global_cleanup();