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"
49 #define WIMLIB_COMPRESSION_TYPE_INVALID (-1)
52 # include "imagex-win32.h"
53 # define print_security_descriptor win32_print_security_descriptor
56 # include <langinfo.h>
57 # define print_security_descriptor default_print_security_descriptor
58 static inline void set_fd_to_binary_mode(int fd)
63 /* Don't confuse the user by presenting the mounting commands on Windows when
64 * they will never work. However on UNIX-like systems we always present them,
65 * even if WITH_FUSE is not defined at this point, as to not tie the build of
66 * wimlib-imagex to a specific build of wimlib. */
68 # define WIM_MOUNTING_SUPPORTED 0
70 # define WIM_MOUNTING_SUPPORTED 1
73 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
76 is_any_path_separator(tchar c)
78 return c == T('/') || c == T('\\');
81 /* Like basename(), but handles both forward and backwards slashes. */
83 tbasename(tchar *path)
85 tchar *p = tstrchr(path, T('\0'));
90 if (!is_any_path_separator(*--p))
98 if (is_any_path_separator(*--p))
103 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
117 #if WIM_MOUNTING_SUPPORTED
123 #if WIM_MOUNTING_SUPPORTED
130 static void usage(int cmd, FILE *fp);
131 static void usage_all(FILE *fp);
132 static void recommend_man_page(int cmd, FILE *fp);
133 static const tchar *get_cmd_string(int cmd, bool nospace);
135 static bool imagex_be_quiet = false;
136 static FILE *imagex_info_file;
138 #define imagex_printf(format, ...) \
139 tfprintf(imagex_info_file, format, ##__VA_ARGS__)
142 IMAGEX_ALLOW_OTHER_OPTION,
145 IMAGEX_CHUNK_SIZE_OPTION,
146 IMAGEX_COMMAND_OPTION,
147 IMAGEX_COMMIT_OPTION,
148 IMAGEX_COMPRESS_OPTION,
149 IMAGEX_COMPRESS_SLOW_OPTION,
150 IMAGEX_CONFIG_OPTION,
152 IMAGEX_DELTA_FROM_OPTION,
153 IMAGEX_DEREFERENCE_OPTION,
154 IMAGEX_DEST_DIR_OPTION,
155 IMAGEX_DETAILED_OPTION,
156 IMAGEX_EXTRACT_XML_OPTION,
159 IMAGEX_HEADER_OPTION,
160 IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
162 IMAGEX_LOOKUP_TABLE_OPTION,
163 IMAGEX_METADATA_OPTION,
164 IMAGEX_NEW_IMAGE_OPTION,
165 IMAGEX_NOCHECK_OPTION,
166 IMAGEX_NORPFIX_OPTION,
167 IMAGEX_NOT_PIPABLE_OPTION,
168 IMAGEX_NO_ACLS_OPTION,
169 IMAGEX_NO_ATTRIBUTES_OPTION,
170 IMAGEX_NO_REPLACE_OPTION,
171 IMAGEX_NO_GLOBS_OPTION,
172 IMAGEX_NULLGLOB_OPTION,
173 IMAGEX_ONE_FILE_ONLY_OPTION,
174 IMAGEX_PACK_CHUNK_SIZE_OPTION,
175 IMAGEX_PACK_COMPRESS_OPTION,
176 IMAGEX_PACK_STREAMS_OPTION,
178 IMAGEX_PIPABLE_OPTION,
179 IMAGEX_PRESERVE_DIR_STRUCTURE_OPTION,
180 IMAGEX_REBUILD_OPTION,
181 IMAGEX_RECOMPRESS_OPTION,
182 IMAGEX_RECURSIVE_OPTION,
184 IMAGEX_RESUME_OPTION,
187 IMAGEX_SOURCE_LIST_OPTION,
188 IMAGEX_STAGING_DIR_OPTION,
189 IMAGEX_STREAMS_INTERFACE_OPTION,
190 IMAGEX_STRICT_ACLS_OPTION,
191 IMAGEX_THREADS_OPTION,
192 IMAGEX_TO_STDOUT_OPTION,
193 IMAGEX_UNIX_DATA_OPTION,
194 IMAGEX_UPDATE_OF_OPTION,
195 IMAGEX_VERBOSE_OPTION,
196 IMAGEX_WIMBOOT_OPTION,
197 IMAGEX_WIMBOOT_CONFIG_OPTION,
201 static const struct option apply_options[] = {
202 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
203 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
204 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
205 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
206 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
207 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
208 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
209 {T("no-attributes"), no_argument, NULL, IMAGEX_NO_ATTRIBUTES_OPTION},
210 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
211 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
212 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
214 /* --resume is undocumented for now as it needs improvement. */
215 {T("resume"), no_argument, NULL, IMAGEX_RESUME_OPTION},
216 {T("wimboot"), no_argument, NULL, IMAGEX_WIMBOOT_OPTION},
220 static const struct option capture_or_append_options[] = {
221 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
222 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
223 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
224 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
225 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
226 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
227 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
228 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
229 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
230 {T("pack-compress"), required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
231 {T("solid-compress"),required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
232 {T("pack-streams"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
233 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
234 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
235 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
236 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
237 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
238 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
239 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
240 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
241 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
242 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
243 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
244 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
245 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
246 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
247 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
248 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
249 {T("update-of"), required_argument, NULL, IMAGEX_UPDATE_OF_OPTION},
250 {T("delta-from"), required_argument, NULL, IMAGEX_DELTA_FROM_OPTION},
251 {T("wimboot"), no_argument, NULL, IMAGEX_WIMBOOT_OPTION},
255 static const struct option delete_options[] = {
256 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
257 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
261 static const struct option dir_options[] = {
262 {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
263 {T("detailed"), no_argument, NULL, IMAGEX_DETAILED_OPTION},
264 {T("one-file-only"), no_argument, NULL, IMAGEX_ONE_FILE_ONLY_OPTION},
268 static const struct option export_options[] = {
269 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
270 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
271 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
272 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
273 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
274 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
275 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
276 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
277 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
278 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
279 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
280 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
281 {T("pack-compress"), required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
282 {T("solid-compress"),required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
283 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
284 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
285 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
286 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
287 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
288 {T("wimboot"), no_argument, NULL, IMAGEX_WIMBOOT_OPTION},
292 static const struct option extract_options[] = {
293 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
294 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
295 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
296 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
297 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
298 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
299 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
300 {T("no-attributes"), no_argument, NULL, IMAGEX_NO_ATTRIBUTES_OPTION},
301 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
302 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
303 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
304 {T("no-wildcards"), no_argument, NULL, IMAGEX_NO_GLOBS_OPTION},
305 {T("no-globs"), no_argument, NULL, IMAGEX_NO_GLOBS_OPTION},
306 {T("nullglob"), no_argument, NULL, IMAGEX_NULLGLOB_OPTION},
307 {T("preserve-dir-structure"), no_argument, NULL, IMAGEX_PRESERVE_DIR_STRUCTURE_OPTION},
308 {T("wimboot"), no_argument, NULL, IMAGEX_WIMBOOT_OPTION},
312 static const struct option info_options[] = {
313 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
314 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
315 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
316 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
317 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
318 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
319 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
320 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
321 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
325 static const struct option join_options[] = {
326 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
330 static const struct option mount_options[] = {
331 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
332 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
333 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
334 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
335 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
336 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
337 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
341 static const struct option optimize_options[] = {
342 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
343 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
344 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
345 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
346 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
347 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
348 {T("recompress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
349 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
350 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
351 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
352 {T("pack-compress"), required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
353 {T("solid-compress"),required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
354 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
355 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
356 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
357 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
358 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
362 static const struct option split_options[] = {
363 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
367 static const struct option unmount_options[] = {
368 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
369 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
370 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
371 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
372 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
373 {T("new-image"), no_argument, NULL, IMAGEX_NEW_IMAGE_OPTION},
377 static const struct option update_options[] = {
378 /* Careful: some of the options here set the defaults for update
379 * commands, but the flags given to an actual update command (and not to
380 * `imagex update' itself are also handled in
381 * update_command_add_option(). */
382 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
383 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
384 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
385 {T("command"), required_argument, NULL, IMAGEX_COMMAND_OPTION},
386 {T("wimboot-config"), required_argument, NULL, IMAGEX_WIMBOOT_CONFIG_OPTION},
388 /* Default delete options */
389 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
390 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
392 /* Global add option */
393 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
395 /* Default add options */
396 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
397 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
398 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
399 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
400 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
401 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
402 {T("no-replace"), no_argument, NULL, IMAGEX_NO_REPLACE_OPTION},
408 # define _format_attribute(type, format_str, args_start) \
409 __attribute__((format(type, format_str, args_start)))
411 # define _format_attribute(type, format_str, args_start)
414 /* Print formatted error message to stderr. */
415 static void _format_attribute(printf, 1, 2)
416 imagex_error(const tchar *format, ...)
419 va_start(va, format);
420 tfputs(T("ERROR: "), stderr);
421 tvfprintf(stderr, format, va);
422 tputc(T('\n'), stderr);
426 /* Print formatted error message to stderr. */
427 static void _format_attribute(printf, 1, 2)
428 imagex_error_with_errno(const tchar *format, ...)
430 int errno_save = errno;
432 va_start(va, format);
433 tfputs(T("ERROR: "), stderr);
434 tvfprintf(stderr, format, va);
435 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
440 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
442 if (image == WIMLIB_NO_IMAGE) {
443 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
444 " Please specify a 1-based image index or "
445 "image name. To list the images\n"
446 " contained in the WIM archive, run\n"
448 " %"TS" \"%"TS"\"\n"),
449 image_name, wim_name,
450 get_cmd_string(CMD_INFO, false), wim_name);
451 return WIMLIB_ERR_INVALID_IMAGE;
457 verify_image_is_single(int image)
459 if (image == WIMLIB_ALL_IMAGES) {
460 imagex_error(T("Cannot specify all images for this action!"));
461 return WIMLIB_ERR_INVALID_IMAGE;
467 verify_image_exists_and_is_single(int image, const tchar *image_name,
468 const tchar *wim_name)
471 ret = verify_image_exists(image, image_name, wim_name);
473 ret = verify_image_is_single(image);
477 /* Parse the argument to --compress */
479 get_compression_type(const tchar *optarg)
481 if (!tstrcasecmp(optarg, T("maximum")) ||
482 !tstrcasecmp(optarg, T("lzx")) ||
483 !tstrcasecmp(optarg, T("max")))
484 return WIMLIB_COMPRESSION_TYPE_LZX;
485 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
486 return WIMLIB_COMPRESSION_TYPE_XPRESS;
487 else if (!tstrcasecmp(optarg, T("recovery")) || !tstrcasecmp(optarg, T("lzms")))
488 return WIMLIB_COMPRESSION_TYPE_LZMS;
489 else if (!tstrcasecmp(optarg, T("none")))
490 return WIMLIB_COMPRESSION_TYPE_NONE;
492 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
493 "\"maximum\", \"fast\", or \"none\"."), optarg);
494 return WIMLIB_COMPRESSION_TYPE_INVALID;
499 set_compress_slow(void)
501 static const struct wimlib_lzx_compressor_params lzx_slow_params = {
503 .size = sizeof(struct wimlib_lzx_compressor_params),
505 .algorithm = WIMLIB_LZX_ALGORITHM_SLOW,
508 .use_len2_matches = 1,
509 .nice_match_length = 96,
510 .num_optim_passes = 4,
511 .max_search_depth = 100,
512 .main_nostat_cost = 15,
513 .len_nostat_cost = 15,
514 .aligned_nostat_cost = 7,
519 static const struct wimlib_lzms_compressor_params lzms_slow_params = {
521 .size = sizeof(struct wimlib_lzms_compressor_params),
523 .min_match_length = 2,
524 .max_match_length = UINT32_MAX,
525 .nice_match_length = 96,
526 .max_search_depth = 100,
527 .optim_array_length = 1024,
530 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
531 &lzx_slow_params.hdr);
533 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZMS,
534 &lzms_slow_params.hdr);
538 const tchar **strings;
539 unsigned num_strings;
540 unsigned num_alloc_strings;
543 #define STRING_SET_INITIALIZER \
544 { .strings = NULL, .num_strings = 0, .num_alloc_strings = 0, }
546 #define STRING_SET(_strings) \
547 struct string_set _strings = STRING_SET_INITIALIZER
550 string_set_append(struct string_set *set, const tchar *glob)
552 unsigned num_alloc_strings = set->num_alloc_strings;
554 if (set->num_strings == num_alloc_strings) {
555 const tchar **new_strings;
557 num_alloc_strings += 4;
558 new_strings = realloc(set->strings,
559 sizeof(set->strings[0]) * num_alloc_strings);
561 imagex_error(T("Out of memory!"));
564 set->strings = new_strings;
565 set->num_alloc_strings = num_alloc_strings;
567 set->strings[set->num_strings++] = glob;
572 string_set_destroy(struct string_set *set)
578 wim_reference_globs(WIMStruct *wim, struct string_set *set, int open_flags)
580 return wimlib_reference_resource_files(wim, set->strings,
582 WIMLIB_REF_FLAG_GLOB_ENABLE,
587 do_resource_not_found_warning(const tchar *wimfile,
588 const struct wimlib_wim_info *info,
589 const struct string_set *refglobs)
591 if (info->total_parts > 1) {
592 if (refglobs->num_strings == 0) {
593 imagex_error(T("\"%"TS"\" is part of a split WIM. "
594 "Use --ref to specify the other parts."),
597 imagex_error(T("Perhaps the '--ref' argument did not "
598 "specify all other parts of the split "
602 imagex_error(T("If this is a delta WIM, use the --ref argument "
603 "to specify the WIM(s) on which it is based."));
607 /* Returns the size of a file given its name, or -1 if the file does not exist
608 * or its size cannot be determined. */
610 file_get_size(const tchar *filename)
613 if (tstat(filename, &st) == 0)
620 PARSE_STRING_SUCCESS = 0,
621 PARSE_STRING_FAILURE = 1,
622 PARSE_STRING_NONE = 2,
626 * Parses a string token from an array of characters.
628 * Tokens are either whitespace-delimited, or double or single-quoted.
630 * @line_p: Pointer to the pointer to the line of data. Will be updated
631 * to point past the string token iff the return value is
632 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
635 * @len_p: @len_p initially stores the length of the line of data, which may
636 * be 0, and it will be updated to the number of bytes remaining in
637 * the line iff the return value is PARSE_STRING_SUCCESS.
639 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
640 * parsed string token will be returned here.
642 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
643 * PARSE_STRING_FAILURE if the data was invalid due to a missing
644 * closing quote; or PARSE_STRING_NONE if the line ended before the
645 * beginning of a string token was found.
648 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
651 tchar *line = *line_p;
655 /* Skip leading whitespace */
658 return PARSE_STRING_NONE;
659 if (!istspace(*line) && *line != T('\0'))
665 if (quote_char == T('"') || quote_char == T('\'')) {
670 line = tmemchr(line, quote_char, len);
672 imagex_error(T("Missing closing quote: %"TS), fn - 1);
673 return PARSE_STRING_FAILURE;
676 /* Unquoted string. Go until whitespace. Line is terminated
677 * by '\0', so no need to check 'len'. */
681 } while (!istspace(*line) && *line != T('\0'));
688 return PARSE_STRING_SUCCESS;
691 /* Parses a line of data (not an empty line or comment) in the source list file
692 * format. (See the man page for 'wimlib-imagex capture' for details on this
693 * format and the meaning.)
695 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
696 * len == 0. The data in @line will be modified by this function call.
698 * @len: Length of the line of data.
700 * @source: On success, the capture source and target described by the line is
701 * written into this destination. Note that it will contain pointers
702 * to data in the @line array.
704 * Returns true if the line was valid; false otherwise. */
706 parse_source_list_line(tchar *line, size_t len,
707 struct wimlib_capture_source *source)
711 ret = parse_string(&line, &len, &source->fs_source_path);
712 if (ret != PARSE_STRING_SUCCESS)
714 ret = parse_string(&line, &len, &source->wim_target_path);
715 if (ret == PARSE_STRING_NONE)
716 source->wim_target_path = source->fs_source_path;
717 return ret != PARSE_STRING_FAILURE;
720 /* Returns %true if the given line of length @len > 0 is a comment or empty line
721 * in the source list file format. */
723 is_comment_line(const tchar *line, size_t len)
726 if (*line == T('#') || *line == T(';'))
728 if (!istspace(*line) && *line != T('\0'))
738 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
741 tchar *contents = *contents_p;
742 size_t nchars = *nchars_p;
745 for (i = 0; i < nchars; i++)
746 if (contents[i] == T('\n'))
749 /* Handle last line not terminated by a newline */
750 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
751 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
753 imagex_error(T("Out of memory!"));
756 contents[nchars] = T('\n');
757 *contents_p = contents;
765 /* Parses a file in the source list format. (See the man page for
766 * 'wimlib-imagex capture' for details on this format and the meaning.)
768 * @source_list_contents: Contents of the source list file. Note that this
769 * buffer will be modified to save memory allocations,
770 * and cannot be freed until the returned array of
771 * wimlib_capture_source's has also been freed.
773 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
776 * @nsources_ret: On success, the length of the returned array is
779 * Returns: An array of `struct wimlib_capture_source's that can be passed to
780 * the wimlib_add_image_multisource() function to specify how a WIM image is to
782 static struct wimlib_capture_source *
783 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
784 size_t *nsources_ret)
788 struct wimlib_capture_source *sources;
791 nlines = text_file_count_lines(source_list_contents_p,
792 &source_list_nchars);
796 /* Always allocate at least 1 slot, just in case the implementation of
797 * calloc() returns NULL if 0 bytes are requested. */
798 sources = calloc(nlines ?: 1, sizeof(*sources));
800 imagex_error(T("out of memory"));
803 p = *source_list_contents_p;
805 for (i = 0; i < nlines; i++) {
806 /* XXX: Could use rawmemchr() here instead, but it may not be
807 * available on all platforms. */
808 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
809 size_t len = endp - p + 1;
811 if (!is_comment_line(p, len)) {
812 if (!parse_source_list_line(p, len, &sources[j++])) {
824 /* Reads the contents of a file into memory. */
826 file_get_contents(const tchar *filename, size_t *len_ret)
833 if (tstat(filename, &stbuf) != 0) {
834 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
839 fp = tfopen(filename, T("rb"));
841 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
845 buf = malloc(len ? len : 1);
847 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
848 "contents of file \"%"TS"\""), len, filename);
851 if (fread(buf, 1, len, fp) != len) {
852 imagex_error_with_errno(T("Failed to read %zu bytes from the "
853 "file \"%"TS"\""), len, filename);
867 /* Read standard input until EOF and return the full contents in a malloc()ed
868 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
871 stdin_get_contents(size_t *len_ret)
873 /* stdin can, of course, be a pipe or other non-seekable file, so the
874 * total length of the data cannot be pre-determined */
876 size_t newlen = 1024;
880 char *p = realloc(buf, newlen);
881 size_t bytes_read, bytes_to_read;
883 imagex_error(T("out of memory while reading stdin"));
887 bytes_to_read = newlen - pos;
888 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
890 if (bytes_read != bytes_to_read) {
895 imagex_error_with_errno(T("error reading stdin"));
909 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
912 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
914 *num_tchars_ret = num_bytes;
916 #else /* !__WIN32__ */
917 /* On Windows, translate the text to UTF-16LE */
921 if (num_bytes >= 2 &&
922 (((unsigned char)text[0] == 0xff && (unsigned char)text[1] == 0xfe) ||
923 ((unsigned char)text[0] <= 0x7f && (unsigned char)text[1] == 0x00)))
925 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
926 * with something that looks like an ASCII character encoded as
927 * a UTF-16LE code unit. Assume the file is encoded as
928 * UTF-16LE. This is not a 100% reliable check. */
929 num_wchars = num_bytes / 2;
930 text_wstr = (wchar_t*)text;
932 /* File does not look like UTF-16LE. Assume it is encoded in
933 * the current Windows code page. I think these are always
934 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
935 * should work as expected. */
936 text_wstr = win32_mbs_to_wcs(text,
941 *num_tchars_ret = num_wchars;
943 #endif /* __WIN32__ */
947 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
952 contents = file_get_contents(filename, &num_bytes);
955 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
959 stdin_get_text_contents(size_t *num_tchars_ret)
964 contents = stdin_get_contents(&num_bytes);
967 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
970 #define TO_PERCENT(numerator, denominator) \
971 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
973 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
974 #define MEBIBYTE_MIN_NBYTES 10000000ULL
975 #define KIBIBYTE_MIN_NBYTES 10000ULL
978 get_unit(uint64_t total_bytes, const tchar **name_ret)
980 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
981 *name_ret = T("GiB");
983 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
984 *name_ret = T("MiB");
986 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
987 *name_ret = T("KiB");
990 *name_ret = T("bytes");
995 static struct wimlib_progress_info_scan last_scan_progress;
998 report_scan_progress(const struct wimlib_progress_info_scan *scan, bool done)
1000 uint64_t prev_count, cur_count;
1002 prev_count = last_scan_progress.num_nondirs_scanned +
1003 last_scan_progress.num_dirs_scanned;
1004 cur_count = scan->num_nondirs_scanned + scan->num_dirs_scanned;
1006 if (done || prev_count == 0 || cur_count >= prev_count + 100 ||
1007 cur_count % 128 == 0)
1009 unsigned unit_shift;
1010 const tchar *unit_name;
1012 unit_shift = get_unit(scan->num_bytes_scanned, &unit_name);
1013 imagex_printf(T("\r%"PRIu64" %"TS" scanned (%"PRIu64" files, "
1014 "%"PRIu64" directories) "),
1015 scan->num_bytes_scanned >> unit_shift,
1017 scan->num_nondirs_scanned,
1018 scan->num_dirs_scanned);
1019 last_scan_progress = *scan;
1022 /* Progress callback function passed to various wimlib functions. */
1023 static enum wimlib_progress_status
1024 imagex_progress_func(enum wimlib_progress_msg msg,
1025 union wimlib_progress_info *info,
1026 void *_ignored_context)
1028 unsigned percent_done;
1029 unsigned unit_shift;
1030 const tchar *unit_name;
1032 if (imagex_be_quiet)
1033 return WIMLIB_PROGRESS_STATUS_CONTINUE;
1035 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1037 static bool first = true;
1039 imagex_printf(T("Writing %"TS"-compressed data "
1040 "using %u thread%"TS"\n"),
1041 wimlib_get_compression_type_string(
1042 info->write_streams.compression_type),
1043 info->write_streams.num_threads,
1044 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1048 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1049 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1050 info->write_streams.total_bytes);
1052 imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1053 "written (%u%% done)"),
1054 info->write_streams.completed_bytes >> unit_shift,
1056 info->write_streams.total_bytes >> unit_shift,
1059 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1060 imagex_printf(T("\n"));
1062 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1063 imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
1064 if (WIMLIB_IS_WIM_ROOT_PATH(info->scan.wim_target_path)) {
1065 imagex_printf(T("\n"));
1067 imagex_printf(T(" (loading as WIM path: \"%"TS"\")...\n"),
1068 info->scan.wim_target_path);
1070 memset(&last_scan_progress, 0, sizeof(last_scan_progress));
1072 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1073 switch (info->scan.status) {
1074 case WIMLIB_SCAN_DENTRY_OK:
1075 report_scan_progress(&info->scan, false);
1077 case WIMLIB_SCAN_DENTRY_EXCLUDED:
1078 imagex_printf(T("\nExcluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1080 case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
1081 imagex_printf(T("\nWARNING: Excluding unsupported file or directory\n"
1082 " \"%"TS"\" from capture\n"), info->scan.cur_path);
1084 case WIMLIB_SCAN_DENTRY_FIXED_SYMLINK:
1085 /* Symlink fixups are enabled by default. This is
1086 * mainly intended for Windows, which for some reason
1087 * uses absolute junctions (with drive letters!) in the
1088 * default installation. On UNIX-like systems, warn the
1089 * user when fixing the target of an absolute symbolic
1090 * link, so they know to disable this if they want. */
1092 imagex_printf(T("\nWARNING: Adjusted target of "
1093 "absolute symbolic link \"%"TS"\"\n"
1094 " (Use --norpfix to capture "
1095 "absolute symbolic links as-is)\n"),
1096 info->scan.cur_path);
1103 case WIMLIB_PROGRESS_MSG_SCAN_END:
1104 report_scan_progress(&info->scan, true);
1105 imagex_printf(T("\n"));
1107 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1108 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1109 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1110 info->integrity.total_bytes);
1111 imagex_printf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1112 "of %"PRIu64" %"TS" (%u%%) done"),
1113 info->integrity.filename,
1114 info->integrity.completed_bytes >> unit_shift,
1116 info->integrity.total_bytes >> unit_shift,
1119 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1120 imagex_printf(T("\n"));
1122 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1123 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1124 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1125 info->integrity.total_bytes);
1126 imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1127 "of %"PRIu64" %"TS" (%u%%) done"),
1128 info->integrity.completed_bytes >> unit_shift,
1130 info->integrity.total_bytes >> unit_shift,
1133 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1134 imagex_printf(T("\n"));
1136 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1137 imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1138 "to %"TS" \"%"TS"\"\n"),
1139 info->extract.image,
1140 info->extract.image_name,
1141 info->extract.wimfile_name,
1142 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1143 T("NTFS volume") : T("directory")),
1144 info->extract.target);
1146 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1147 percent_done = TO_PERCENT(info->extract.completed_bytes,
1148 info->extract.total_bytes);
1149 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1150 imagex_printf(T("\rExtracting files: "
1151 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1152 info->extract.completed_bytes >> unit_shift,
1154 info->extract.total_bytes >> unit_shift,
1157 if (info->extract.completed_bytes >= info->extract.total_bytes)
1158 imagex_printf(T("\n"));
1160 case WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN:
1161 if (info->extract.total_parts != 1) {
1162 imagex_printf(T("\nReading split pipable WIM part %u of %u\n"),
1163 info->extract.part_number,
1164 info->extract.total_parts);
1167 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1168 percent_done = TO_PERCENT(info->split.completed_bytes,
1169 info->split.total_bytes);
1170 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1171 imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
1172 "%"PRIu64" %"TS" (%u%%) written\n"),
1173 info->split.part_name,
1174 info->split.cur_part_number,
1175 info->split.total_parts,
1176 info->split.completed_bytes >> unit_shift,
1178 info->split.total_bytes >> unit_shift,
1182 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1183 if (info->split.completed_bytes == info->split.total_bytes) {
1184 imagex_printf(T("Finished writing split WIM part %u of %u\n"),
1185 info->split.cur_part_number,
1186 info->split.total_parts);
1189 case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1190 switch (info->update.command->op) {
1191 case WIMLIB_UPDATE_OP_DELETE:
1192 imagex_printf(T("Deleted WIM path \"%"TS"\"\n"),
1193 info->update.command->delete_.wim_path);
1195 case WIMLIB_UPDATE_OP_RENAME:
1196 imagex_printf(T("Renamed WIM path \"%"TS"\" => \"%"TS"\"\n"),
1197 info->update.command->rename.wim_source_path,
1198 info->update.command->rename.wim_target_path);
1200 case WIMLIB_UPDATE_OP_ADD:
1205 case WIMLIB_PROGRESS_MSG_REPLACE_FILE_IN_WIM:
1206 imagex_printf(T("Updating \"%"TS"\" in WIM image\n"),
1207 info->replace.path_in_wim);
1209 case WIMLIB_PROGRESS_MSG_WIMBOOT_EXCLUDE:
1210 imagex_printf(T("\nExtracting \"%"TS"\" as normal file (not WIMBoot pointer)\n"),
1211 info->wimboot_exclude.path_in_wim);
1213 case WIMLIB_PROGRESS_MSG_UNMOUNT_BEGIN:
1214 if (info->unmount.mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1215 if (info->unmount.unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT) {
1216 imagex_printf(T("Committing changes to %"TS" (image %d)\n"),
1217 info->unmount.mounted_wim,
1218 info->unmount.mounted_image);
1220 imagex_printf(T("Discarding changes to %"TS" (image %d)\n"),
1221 info->unmount.mounted_wim,
1222 info->unmount.mounted_image);
1223 imagex_printf(T("\t(Use --commit to keep changes.)\n"));
1230 fflush(imagex_info_file);
1231 return WIMLIB_PROGRESS_STATUS_CONTINUE;
1235 parse_num_threads(const tchar *optarg)
1238 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1239 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1240 imagex_error(T("Number of threads must be a non-negative integer!"));
1247 static uint32_t parse_chunk_size(const tchar *optarg)
1250 unsigned long chunk_size = tstrtoul(optarg, &tmp, 10);
1251 if (chunk_size >= UINT32_MAX || *tmp || tmp == optarg) {
1252 imagex_error(T("Chunk size must be a non-negative integer!"));
1261 * Parse an option passed to an update command.
1263 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1266 * @option: Text string for the option (beginning with --)
1268 * @cmd: `struct wimlib_update_command' that is being constructed for
1271 * Returns true if the option was recognized; false if not.
1274 update_command_add_option(int op, const tchar *option,
1275 struct wimlib_update_command *cmd)
1277 bool recognized = true;
1279 case WIMLIB_UPDATE_OP_ADD:
1280 if (!tstrcmp(option, T("--verbose")))
1281 cmd->add.add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
1282 else if (!tstrcmp(option, T("--unix-data")))
1283 cmd->add.add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
1284 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1285 cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
1286 else if (!tstrcmp(option, T("--strict-acls")))
1287 cmd->add.add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
1288 else if (!tstrcmp(option, T("--dereference")))
1289 cmd->add.add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
1290 else if (!tstrcmp(option, T("--no-replace")))
1291 cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_REPLACE;
1295 case WIMLIB_UPDATE_OP_DELETE:
1296 if (!tstrcmp(option, T("--force")))
1297 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1298 else if (!tstrcmp(option, T("--recursive")))
1299 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1310 /* How many nonoption arguments each `imagex update' command expects */
1311 static const unsigned update_command_num_nonoptions[] = {
1312 [WIMLIB_UPDATE_OP_ADD] = 2,
1313 [WIMLIB_UPDATE_OP_DELETE] = 1,
1314 [WIMLIB_UPDATE_OP_RENAME] = 2,
1318 update_command_add_nonoption(int op, const tchar *nonoption,
1319 struct wimlib_update_command *cmd,
1320 unsigned num_nonoptions)
1323 case WIMLIB_UPDATE_OP_ADD:
1324 if (num_nonoptions == 0)
1325 cmd->add.fs_source_path = (tchar*)nonoption;
1327 cmd->add.wim_target_path = (tchar*)nonoption;
1329 case WIMLIB_UPDATE_OP_DELETE:
1330 cmd->delete_.wim_path = (tchar*)nonoption;
1332 case WIMLIB_UPDATE_OP_RENAME:
1333 if (num_nonoptions == 0)
1334 cmd->rename.wim_source_path = (tchar*)nonoption;
1336 cmd->rename.wim_target_path = (tchar*)nonoption;
1342 * Parse a command passed on stdin to `imagex update'.
1344 * @line: Text of the command.
1345 * @len: Length of the line, including a null terminator
1348 * @command: A `struct wimlib_update_command' to fill in from the parsed
1351 * @line_number: Line number of the command, for diagnostics.
1353 * Returns true on success; returns false on parse error.
1356 parse_update_command(tchar *line, size_t len,
1357 struct wimlib_update_command *command,
1361 tchar *command_name;
1363 size_t num_nonoptions;
1365 /* Get the command name ("add", "delete", "rename") */
1366 ret = parse_string(&line, &len, &command_name);
1367 if (ret != PARSE_STRING_SUCCESS)
1370 if (!tstrcasecmp(command_name, T("add"))) {
1371 op = WIMLIB_UPDATE_OP_ADD;
1372 } else if (!tstrcasecmp(command_name, T("delete"))) {
1373 op = WIMLIB_UPDATE_OP_DELETE;
1374 } else if (!tstrcasecmp(command_name, T("rename"))) {
1375 op = WIMLIB_UPDATE_OP_RENAME;
1377 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1378 command_name, line_number);
1383 /* Parse additional options and non-options as needed */
1388 ret = parse_string(&line, &len, &next_string);
1389 if (ret == PARSE_STRING_NONE) /* End of line */
1391 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1393 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1395 if (!update_command_add_option(op, next_string, command))
1397 imagex_error(T("Unrecognized option \"%"TS"\" to "
1398 "update command \"%"TS"\" on line %zu"),
1399 next_string, command_name, line_number);
1405 if (num_nonoptions == update_command_num_nonoptions[op])
1407 imagex_error(T("Unexpected argument \"%"TS"\" in "
1408 "update command on line %zu\n"
1409 " (The \"%"TS"\" command only "
1410 "takes %zu nonoption arguments!)\n"),
1411 next_string, line_number,
1412 command_name, num_nonoptions);
1415 update_command_add_nonoption(op, next_string,
1416 command, num_nonoptions);
1421 if (num_nonoptions != update_command_num_nonoptions[op]) {
1422 imagex_error(T("Not enough arguments to update command "
1423 "\"%"TS"\" on line %zu"), command_name, line_number);
1429 static struct wimlib_update_command *
1430 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1431 size_t *num_cmds_ret)
1435 struct wimlib_update_command *cmds;
1438 nlines = text_file_count_lines(cmd_file_contents_p,
1443 /* Always allocate at least 1 slot, just in case the implementation of
1444 * calloc() returns NULL if 0 bytes are requested. */
1445 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1447 imagex_error(T("out of memory"));
1450 p = *cmd_file_contents_p;
1452 for (i = 0; i < nlines; i++) {
1453 /* XXX: Could use rawmemchr() here instead, but it may not be
1454 * available on all platforms. */
1455 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1456 size_t len = endp - p + 1;
1458 if (!is_comment_line(p, len)) {
1459 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1470 /* Apply one image, or all images, from a WIM file to a directory, OR apply
1471 * one image from a WIM file to an NTFS volume. */
1473 imagex_apply(int argc, tchar **argv, int cmd)
1477 int image = WIMLIB_NO_IMAGE;
1479 struct wimlib_wim_info info;
1481 const tchar *wimfile;
1482 const tchar *target;
1483 const tchar *image_num_or_name = NULL;
1484 int extract_flags = 0;
1486 STRING_SET(refglobs);
1488 for_opt(c, apply_options) {
1490 case IMAGEX_CHECK_OPTION:
1491 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1493 case IMAGEX_VERBOSE_OPTION:
1494 /* No longer does anything. */
1496 case IMAGEX_REF_OPTION:
1497 ret = string_set_append(&refglobs, optarg);
1499 goto out_free_refglobs;
1501 case IMAGEX_UNIX_DATA_OPTION:
1502 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1504 case IMAGEX_NO_ACLS_OPTION:
1505 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1507 case IMAGEX_STRICT_ACLS_OPTION:
1508 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1510 case IMAGEX_NO_ATTRIBUTES_OPTION:
1511 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES;
1513 case IMAGEX_NORPFIX_OPTION:
1514 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1516 case IMAGEX_RPFIX_OPTION:
1517 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1519 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1520 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1521 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1523 case IMAGEX_RESUME_OPTION:
1524 extract_flags |= WIMLIB_EXTRACT_FLAG_RESUME;
1526 case IMAGEX_WIMBOOT_OPTION:
1527 extract_flags |= WIMLIB_EXTRACT_FLAG_WIMBOOT;
1535 if (argc != 2 && argc != 3)
1540 if (!tstrcmp(wimfile, T("-"))) {
1541 /* Attempt to apply pipable WIM from standard input. */
1543 image_num_or_name = NULL;
1546 image_num_or_name = argv[1];
1551 ret = wimlib_open_wim_with_progress(wimfile, open_flags, &wim,
1552 imagex_progress_func, NULL);
1554 goto out_free_refglobs;
1556 wimlib_get_wim_info(wim, &info);
1559 /* Image explicitly specified. */
1560 image_num_or_name = argv[1];
1561 image = wimlib_resolve_image(wim, image_num_or_name);
1562 ret = verify_image_exists(image, image_num_or_name, wimfile);
1564 goto out_wimlib_free;
1567 /* No image specified; default to image 1, but only if the WIM
1568 * contains exactly one image. */
1570 if (info.image_count != 1) {
1571 imagex_error(T("\"%"TS"\" contains %d images; "
1572 "Please select one (or all)."),
1573 wimfile, info.image_count);
1582 if (refglobs.num_strings) {
1584 imagex_error(T("Can't specify --ref when applying from stdin!"));
1586 goto out_wimlib_free;
1588 ret = wim_reference_globs(wim, &refglobs, open_flags);
1590 goto out_wimlib_free;
1595 /* Interpret a regular file or block device target as an NTFS
1599 if (tstat(target, &stbuf)) {
1600 if (errno != ENOENT) {
1601 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1604 goto out_wimlib_free;
1607 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1608 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1614 ret = wimlib_extract_image(wim, image, target, extract_flags);
1616 set_fd_to_binary_mode(STDIN_FILENO);
1617 ret = wimlib_extract_image_from_pipe_with_progress(
1622 imagex_progress_func,
1626 imagex_printf(T("Done applying WIM image.\n"));
1627 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
1629 do_resource_not_found_warning(wimfile, &info, &refglobs);
1631 imagex_error(T( "If you are applying an image "
1632 "from a split pipable WIM,\n"
1633 " make sure you have "
1634 "concatenated together all parts."));
1640 string_set_destroy(&refglobs);
1644 usage(CMD_APPLY, stderr);
1646 goto out_free_refglobs;
1649 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1650 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1651 * the desired image. 'wimlib-imagex append': add a new image to an existing
1654 imagex_capture_or_append(int argc, tchar **argv, int cmd)
1657 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1658 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE |
1659 WIMLIB_ADD_IMAGE_FLAG_WINCONFIG |
1660 WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1661 int write_flags = 0;
1662 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
1663 uint32_t chunk_size = UINT32_MAX;
1664 uint32_t pack_chunk_size = UINT32_MAX;
1665 int pack_ctype = WIMLIB_COMPRESSION_TYPE_INVALID;
1666 const tchar *wimfile;
1670 const tchar *flags_element = NULL;
1673 STRING_SET(base_wimfiles);
1674 WIMStruct **base_wims;
1676 WIMStruct *template_wim;
1677 const tchar *template_wimfile = NULL;
1678 const tchar *template_image_name_or_num = NULL;
1679 int template_image = WIMLIB_NO_IMAGE;
1682 unsigned num_threads = 0;
1687 tchar *config_file = NULL;
1689 bool source_list = false;
1690 size_t source_list_nchars = 0;
1691 tchar *source_list_contents;
1692 bool capture_sources_malloced;
1693 struct wimlib_capture_source *capture_sources;
1695 bool name_defaulted;
1696 bool compress_slow = false;
1698 for_opt(c, capture_or_append_options) {
1700 case IMAGEX_BOOT_OPTION:
1701 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1703 case IMAGEX_CHECK_OPTION:
1704 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1705 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1707 case IMAGEX_NOCHECK_OPTION:
1708 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
1710 case IMAGEX_CONFIG_OPTION:
1711 config_file = optarg;
1712 add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1714 case IMAGEX_COMPRESS_OPTION:
1715 compression_type = get_compression_type(optarg);
1716 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1719 case IMAGEX_COMPRESS_SLOW_OPTION:
1720 compress_slow = true;
1722 case IMAGEX_CHUNK_SIZE_OPTION:
1723 chunk_size = parse_chunk_size(optarg);
1724 if (chunk_size == UINT32_MAX)
1727 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
1728 pack_chunk_size = parse_chunk_size(optarg);
1729 if (pack_chunk_size == UINT32_MAX)
1732 case IMAGEX_PACK_COMPRESS_OPTION:
1733 pack_ctype = get_compression_type(optarg);
1734 if (pack_ctype == WIMLIB_COMPRESSION_TYPE_INVALID)
1737 case IMAGEX_PACK_STREAMS_OPTION:
1738 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
1740 case IMAGEX_FLAGS_OPTION:
1741 flags_element = optarg;
1743 case IMAGEX_DEREFERENCE_OPTION:
1744 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1746 case IMAGEX_VERBOSE_OPTION:
1747 /* No longer does anything. */
1749 case IMAGEX_THREADS_OPTION:
1750 num_threads = parse_num_threads(optarg);
1751 if (num_threads == UINT_MAX)
1754 case IMAGEX_REBUILD_OPTION:
1755 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1757 case IMAGEX_UNIX_DATA_OPTION:
1758 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1760 case IMAGEX_SOURCE_LIST_OPTION:
1763 case IMAGEX_NO_ACLS_OPTION:
1764 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1766 case IMAGEX_STRICT_ACLS_OPTION:
1767 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1769 case IMAGEX_RPFIX_OPTION:
1770 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1772 case IMAGEX_NORPFIX_OPTION:
1773 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1775 case IMAGEX_PIPABLE_OPTION:
1776 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1778 case IMAGEX_NOT_PIPABLE_OPTION:
1779 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
1781 case IMAGEX_UPDATE_OF_OPTION:
1782 if (template_image_name_or_num) {
1783 imagex_error(T("'--update-of' can only be "
1784 "specified one time!"));
1788 colon = tstrrchr(optarg, T(':'));
1791 template_wimfile = optarg;
1793 template_image_name_or_num = colon + 1;
1795 template_wimfile = NULL;
1796 template_image_name_or_num = optarg;
1800 case IMAGEX_DELTA_FROM_OPTION:
1801 if (cmd != CMD_CAPTURE) {
1802 imagex_error(T("'--delta-from' is only "
1803 "valid for capture!"));
1806 ret = string_set_append(&base_wimfiles, optarg);
1808 goto out_free_base_wimfiles;
1809 write_flags |= WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS;
1811 case IMAGEX_WIMBOOT_OPTION:
1812 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_WIMBOOT;
1821 if (argc < 2 || argc > 4)
1827 /* Set default compression type and parameters. */
1830 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
1831 /* No compression type specified. Use the default. */
1833 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_WIMBOOT) {
1834 /* With --wimboot, default to XPRESS compression. */
1835 compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
1836 } else if (write_flags & WIMLIB_WRITE_FLAG_PACK_STREAMS) {
1837 /* With --pack-streams or --solid, default to LZMS
1838 * compression. (However, this will not affect packed
1840 compression_type = WIMLIB_COMPRESSION_TYPE_LZMS;
1842 /* Otherwise, default to LZX compression in fast mode.
1844 compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1845 if (!compress_slow && pack_ctype != WIMLIB_COMPRESSION_TYPE_LZX) {
1846 struct wimlib_lzx_compressor_params params = {
1847 .hdr.size = sizeof(params),
1848 .algorithm = WIMLIB_LZX_ALGORITHM_FAST,
1851 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
1858 set_compress_slow();
1860 if (!tstrcmp(wimfile, T("-"))) {
1861 /* Writing captured WIM to standard output. */
1863 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
1864 imagex_error("Can't write a non-pipable WIM to "
1865 "standard output! Specify --pipable\n"
1866 " if you want to create a pipable WIM "
1867 "(but read the docs first).");
1871 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1873 if (cmd == CMD_APPEND) {
1874 imagex_error(T("Using standard output for append does "
1875 "not make sense."));
1878 wim_fd = STDOUT_FILENO;
1880 imagex_info_file = stderr;
1881 set_fd_to_binary_mode(wim_fd);
1884 /* If template image was specified using --update-of=IMAGE rather
1885 * than --update-of=WIMFILE:IMAGE, set the default WIMFILE. */
1886 if (template_image_name_or_num && !template_wimfile) {
1887 if (base_wimfiles.num_strings == 1) {
1888 /* Capturing delta WIM based on single WIM: default to
1890 template_wimfile = base_wimfiles.strings[0];
1891 } else if (cmd == CMD_APPEND) {
1892 /* Appending to WIM: default to WIM being appended to.
1894 template_wimfile = wimfile;
1896 /* Capturing a normal (non-delta) WIM, so the WIM file
1897 * *must* be explicitly specified. */
1898 if (base_wimfiles.num_strings > 1) {
1899 imagex_error(T("For capture of delta WIM "
1900 "based on multiple existing "
1902 " '--update-of' must "
1903 "specify WIMFILE:IMAGE!"));
1905 imagex_error(T("For capture of non-delta WIM, "
1906 "'--update-of' must specify "
1915 name_defaulted = false;
1917 /* Set default name to SOURCE argument, omitting any directory
1918 * prefixes and trailing slashes. This requires making a copy
1919 * of @source. Leave some free characters at the end in case we
1920 * append a number to keep the name unique. */
1921 size_t source_name_len;
1923 source_name_len = tstrlen(source);
1924 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1925 name = tbasename(tstrcpy(source_copy, source));
1926 name_defaulted = true;
1928 /* Image description defaults to NULL if not given. */
1935 /* Set up capture sources in source list mode */
1936 if (source[0] == T('-') && source[1] == T('\0')) {
1937 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1939 source_list_contents = file_get_text_contents(source,
1940 &source_list_nchars);
1942 if (!source_list_contents)
1945 capture_sources = parse_source_list(&source_list_contents,
1948 if (!capture_sources) {
1950 goto out_free_source_list_contents;
1952 capture_sources_malloced = true;
1954 /* Set up capture source in non-source-list mode. */
1955 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1956 capture_sources[0].fs_source_path = source;
1957 capture_sources[0].wim_target_path = WIMLIB_WIM_ROOT_PATH;
1958 capture_sources[0].reserved = 0;
1960 capture_sources_malloced = false;
1961 source_list_contents = NULL;
1964 /* Open the existing WIM, or create a new one. */
1965 if (cmd == CMD_APPEND) {
1966 ret = wimlib_open_wim_with_progress(wimfile, open_flags, &wim,
1967 imagex_progress_func, NULL);
1969 goto out_free_capture_sources;
1971 ret = wimlib_create_new_wim(compression_type, &wim);
1973 goto out_free_capture_sources;
1974 wimlib_register_progress_function(wim, imagex_progress_func, NULL);
1977 /* Set chunk size if non-default. */
1978 if (chunk_size != UINT32_MAX) {
1979 ret = wimlib_set_output_chunk_size(wim, chunk_size);
1982 } else if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_WIMBOOT) &&
1983 compression_type == WIMLIB_COMPRESSION_TYPE_XPRESS) {
1984 ret = wimlib_set_output_chunk_size(wim, 4096);
1988 if (pack_ctype != WIMLIB_COMPRESSION_TYPE_INVALID) {
1989 ret = wimlib_set_output_pack_compression_type(wim, pack_ctype);
1993 if (pack_chunk_size != UINT32_MAX) {
1994 ret = wimlib_set_output_pack_chunk_size(wim, pack_chunk_size);
2000 /* Detect if source is regular file or block device and set NTFS volume
2005 if (tstat(source, &stbuf) == 0) {
2006 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
2007 imagex_printf(T("Capturing WIM image from NTFS "
2008 "filesystem on \"%"TS"\"\n"), source);
2009 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
2012 if (errno != ENOENT) {
2013 imagex_error_with_errno(T("Failed to stat "
2014 "\"%"TS"\""), source);
2022 /* If the user did not specify an image name, and the basename of the
2023 * source already exists as an image name in the WIM file, append a
2024 * suffix to make it unique. */
2025 if (cmd == CMD_APPEND && name_defaulted) {
2026 unsigned long conflict_idx;
2027 tchar *name_end = tstrchr(name, T('\0'));
2028 for (conflict_idx = 1;
2029 wimlib_image_name_in_use(wim, name);
2032 tsprintf(name_end, T(" (%lu)"), conflict_idx);
2036 /* If capturing a delta WIM, reference resources from the base WIMs
2037 * before adding the new image. */
2038 if (base_wimfiles.num_strings) {
2039 base_wims = calloc(base_wimfiles.num_strings,
2040 sizeof(base_wims[0]));
2041 if (base_wims == NULL) {
2042 imagex_error(T("Out of memory!"));
2047 for (size_t i = 0; i < base_wimfiles.num_strings; i++) {
2048 ret = wimlib_open_wim_with_progress(
2049 base_wimfiles.strings[i],
2050 open_flags, &base_wims[i],
2051 imagex_progress_func, NULL);
2053 goto out_free_base_wims;
2057 ret = wimlib_reference_resources(wim, base_wims,
2058 base_wimfiles.num_strings, 0);
2060 goto out_free_base_wims;
2062 if (base_wimfiles.num_strings == 1) {
2063 imagex_printf(T("Capturing delta WIM based on \"%"TS"\"\n"),
2064 base_wimfiles.strings[0]);
2066 imagex_printf(T("Capturing delta WIM based on %u WIMs\n"),
2067 base_wimfiles.num_strings);
2074 /* If capturing or appending as an update of an existing (template) image,
2075 * open the WIM if needed and parse the image index. */
2076 if (template_image_name_or_num) {
2079 if (base_wimfiles.num_strings == 1 &&
2080 template_wimfile == base_wimfiles.strings[0]) {
2081 template_wim = base_wims[0];
2082 } else if (template_wimfile == wimfile) {
2085 ret = wimlib_open_wim_with_progress(template_wimfile,
2088 imagex_progress_func,
2091 goto out_free_base_wims;
2094 template_image = wimlib_resolve_image(template_wim,
2095 template_image_name_or_num);
2097 if (template_image_name_or_num[0] == T('-')) {
2100 struct wimlib_wim_info info;
2102 wimlib_get_wim_info(template_wim, &info);
2103 n = tstrtoul(template_image_name_or_num + 1, &tmp, 10);
2104 if (n >= 1 && n <= info.image_count &&
2106 tmp != template_image_name_or_num + 1)
2108 template_image = info.image_count - (n - 1);
2111 ret = verify_image_exists_and_is_single(template_image,
2112 template_image_name_or_num,
2115 goto out_free_template_wim;
2117 template_wim = NULL;
2120 ret = wimlib_add_image_multisource(wim,
2127 goto out_free_template_wim;
2129 if (desc || flags_element || template_image_name_or_num) {
2130 /* User provided <DESCRIPTION> or <FLAGS> element, or an image
2131 * on which the added one is to be based has been specified with
2132 * --update-of. Get the index of the image we just
2133 * added, then use it to call the appropriate functions. */
2134 struct wimlib_wim_info info;
2136 wimlib_get_wim_info(wim, &info);
2139 ret = wimlib_set_image_descripton(wim,
2143 goto out_free_template_wim;
2146 if (flags_element) {
2147 ret = wimlib_set_image_flags(wim, info.image_count,
2150 goto out_free_template_wim;
2153 /* Reference template image if the user provided one. */
2154 if (template_image_name_or_num) {
2155 imagex_printf(T("Using image %d "
2156 "from \"%"TS"\" as template\n"),
2157 template_image, template_wimfile);
2158 ret = wimlib_reference_template_image(wim,
2164 goto out_free_template_wim;
2168 /* Write the new WIM or overwrite the existing WIM with the new image
2170 if (cmd == CMD_APPEND) {
2171 ret = wimlib_overwrite(wim, write_flags, num_threads);
2172 } else if (wimfile) {
2173 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
2174 write_flags, num_threads);
2176 ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
2177 write_flags, num_threads);
2179 out_free_template_wim:
2180 /* template_wim may alias base_wims[0] or wim. */
2181 if ((base_wimfiles.num_strings != 1 || template_wim != base_wims[0]) &&
2182 template_wim != wim)
2183 wimlib_free(template_wim);
2185 for (size_t i = 0; i < base_wimfiles.num_strings; i++)
2186 wimlib_free(base_wims[i]);
2190 out_free_capture_sources:
2191 if (capture_sources_malloced)
2192 free(capture_sources);
2193 out_free_source_list_contents:
2194 free(source_list_contents);
2195 out_free_base_wimfiles:
2196 string_set_destroy(&base_wimfiles);
2203 goto out_free_base_wimfiles;
2206 /* Remove image(s) from a WIM. */
2208 imagex_delete(int argc, tchar **argv, int cmd)
2211 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2212 int write_flags = 0;
2213 const tchar *wimfile;
2214 const tchar *image_num_or_name;
2219 for_opt(c, delete_options) {
2221 case IMAGEX_CHECK_OPTION:
2222 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2223 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2225 case IMAGEX_SOFT_OPTION:
2226 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
2237 imagex_error(T("Must specify a WIM file"));
2239 imagex_error(T("Must specify an image"));
2243 image_num_or_name = argv[1];
2245 ret = wimlib_open_wim_with_progress(wimfile, open_flags, &wim,
2246 imagex_progress_func, NULL);
2250 image = wimlib_resolve_image(wim, image_num_or_name);
2252 ret = verify_image_exists(image, image_num_or_name, wimfile);
2254 goto out_wimlib_free;
2256 ret = wimlib_delete_image(wim, image);
2258 imagex_error(T("Failed to delete image from \"%"TS"\""),
2260 goto out_wimlib_free;
2263 ret = wimlib_overwrite(wim, write_flags, 0);
2265 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2266 "deleted"), wimfile);
2274 usage(CMD_DELETE, stderr);
2279 struct print_dentry_options {
2284 print_dentry_full_path(const struct wimlib_dir_entry *dentry)
2286 tprintf(T("%"TS"\n"), dentry->full_path);
2289 static const struct {
2292 } file_attr_flags[] = {
2293 {WIMLIB_FILE_ATTRIBUTE_READONLY, T("READONLY")},
2294 {WIMLIB_FILE_ATTRIBUTE_HIDDEN, T("HIDDEN")},
2295 {WIMLIB_FILE_ATTRIBUTE_SYSTEM, T("SYSTEM")},
2296 {WIMLIB_FILE_ATTRIBUTE_DIRECTORY, T("DIRECTORY")},
2297 {WIMLIB_FILE_ATTRIBUTE_ARCHIVE, T("ARCHIVE")},
2298 {WIMLIB_FILE_ATTRIBUTE_DEVICE, T("DEVICE")},
2299 {WIMLIB_FILE_ATTRIBUTE_NORMAL, T("NORMAL")},
2300 {WIMLIB_FILE_ATTRIBUTE_TEMPORARY, T("TEMPORARY")},
2301 {WIMLIB_FILE_ATTRIBUTE_SPARSE_FILE, T("SPARSE_FILE")},
2302 {WIMLIB_FILE_ATTRIBUTE_REPARSE_POINT, T("REPARSE_POINT")},
2303 {WIMLIB_FILE_ATTRIBUTE_COMPRESSED, T("COMPRESSED")},
2304 {WIMLIB_FILE_ATTRIBUTE_OFFLINE, T("OFFLINE")},
2305 {WIMLIB_FILE_ATTRIBUTE_NOT_CONTENT_INDEXED, T("NOT_CONTENT_INDEXED")},
2306 {WIMLIB_FILE_ATTRIBUTE_ENCRYPTED, T("ENCRYPTED")},
2307 {WIMLIB_FILE_ATTRIBUTE_VIRTUAL, T("VIRTUAL")},
2310 #define TIMESTR_MAX 100
2313 timespec_to_string(const struct timespec *spec, tchar *buf)
2315 time_t t = spec->tv_sec;
2318 tstrftime(buf, TIMESTR_MAX, T("%a %b %d %H:%M:%S %Y UTC"), &tm);
2319 buf[TIMESTR_MAX - 1] = '\0';
2323 print_time(const tchar *type, const struct timespec *spec)
2325 tchar timestr[TIMESTR_MAX];
2327 timespec_to_string(spec, timestr);
2329 tprintf(T("%-20"TS"= %"TS"\n"), type, timestr);
2332 static void print_byte_field(const uint8_t field[], size_t len)
2335 tprintf(T("%02hhx"), *field++);
2339 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2341 tputs(T("WIM Information:"));
2342 tputs(T("----------------"));
2343 tprintf(T("Path: %"TS"\n"), wimfile);
2344 tprintf(T("GUID: 0x"));
2345 print_byte_field(info->guid, sizeof(info->guid));
2347 tprintf(T("Version: %u\n"), info->wim_version);
2348 tprintf(T("Image Count: %d\n"), info->image_count);
2349 tprintf(T("Compression: %"TS"\n"),
2350 wimlib_get_compression_type_string(info->compression_type));
2351 tprintf(T("Chunk Size: %"PRIu32" bytes\n"),
2353 tprintf(T("Part Number: %d/%d\n"), info->part_number, info->total_parts);
2354 tprintf(T("Boot Index: %d\n"), info->boot_index);
2355 tprintf(T("Size: %"PRIu64" bytes\n"), info->total_bytes);
2356 tprintf(T("Integrity Info: %"TS"\n"),
2357 info->has_integrity_table ? T("yes") : T("no"));
2358 tprintf(T("Relative path junction: %"TS"\n"),
2359 info->has_rpfix ? T("yes") : T("no"));
2360 tprintf(T("Pipable: %"TS"\n"),
2361 info->pipable ? T("yes") : T("no"));
2366 print_resource(const struct wimlib_resource_entry *resource,
2369 tprintf(T("Hash = 0x"));
2370 print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2373 if (!resource->is_missing) {
2374 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
2375 resource->uncompressed_size);
2376 if (resource->packed) {
2377 tprintf(T("Raw compressed size = %"PRIu64" bytes\n"),
2378 resource->raw_resource_compressed_size);
2380 tprintf(T("Raw offset in WIM = %"PRIu64" bytes\n"),
2381 resource->raw_resource_offset_in_wim);
2383 tprintf(T("Offset in raw = %"PRIu64" bytes\n"),
2386 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
2387 resource->compressed_size);
2389 tprintf(T("Offset in WIM = %"PRIu64" bytes\n"),
2393 tprintf(T("Part Number = %u\n"), resource->part_number);
2394 tprintf(T("Reference Count = %u\n"), resource->reference_count);
2396 tprintf(T("Flags = "));
2397 if (resource->is_compressed)
2398 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED "));
2399 if (resource->is_metadata)
2400 tprintf(T("WIM_RESHDR_FLAG_METADATA "));
2401 if (resource->is_free)
2402 tprintf(T("WIM_RESHDR_FLAG_FREE "));
2403 if (resource->is_spanned)
2404 tprintf(T("WIM_RESHDR_FLAG_SPANNED "));
2405 if (resource->packed)
2406 tprintf(T("WIM_RESHDR_FLAG_PACKED_STREAMS "));
2414 print_lookup_table(WIMStruct *wim)
2416 wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2420 default_print_security_descriptor(const uint8_t *sd, size_t size)
2422 tprintf(T("Security Descriptor = "));
2423 print_byte_field(sd, size);
2428 print_dentry_detailed(const struct wimlib_dir_entry *dentry)
2432 "----------------------------------------------------------------------------\n"));
2433 tprintf(T("Full Path = \"%"TS"\"\n"), dentry->full_path);
2434 if (dentry->dos_name)
2435 tprintf(T("Short Name = \"%"TS"\"\n"), dentry->dos_name);
2436 tprintf(T("Attributes = 0x%08x\n"), dentry->attributes);
2437 for (size_t i = 0; i < ARRAY_LEN(file_attr_flags); i++)
2438 if (file_attr_flags[i].flag & dentry->attributes)
2439 tprintf(T(" FILE_ATTRIBUTE_%"TS" is set\n"),
2440 file_attr_flags[i].name);
2442 if (dentry->security_descriptor) {
2443 print_security_descriptor(dentry->security_descriptor,
2444 dentry->security_descriptor_size);
2447 print_time(T("Creation Time"), &dentry->creation_time);
2448 print_time(T("Last Write Time"), &dentry->last_write_time);
2449 print_time(T("Last Access Time"), &dentry->last_access_time);
2452 if (dentry->attributes & WIMLIB_FILE_ATTRIBUTE_REPARSE_POINT)
2453 tprintf(T("Reparse Tag = 0x%"PRIx32"\n"), dentry->reparse_tag);
2455 tprintf(T("Link Group ID = 0x%016"PRIx64"\n"), dentry->hard_link_group_id);
2456 tprintf(T("Link Count = %"PRIu32"\n"), dentry->num_links);
2458 if (dentry->unix_mode != 0) {
2459 tprintf(T("UNIX Data = uid:%"PRIu32" gid:%"PRIu32" "
2460 "mode:0%"PRIo32" rdev:0x%"PRIx32"\n"),
2461 dentry->unix_uid, dentry->unix_gid,
2462 dentry->unix_mode, dentry->unix_rdev);
2465 for (uint32_t i = 0; i <= dentry->num_named_streams; i++) {
2466 if (dentry->streams[i].stream_name) {
2467 tprintf(T("\tData stream \"%"TS"\":\n"),
2468 dentry->streams[i].stream_name);
2470 tprintf(T("\tUnnamed data stream:\n"));
2472 print_resource(&dentry->streams[i].resource, NULL);
2477 print_dentry(const struct wimlib_dir_entry *dentry, void *_options)
2479 const struct print_dentry_options *options = _options;
2480 if (!options->detailed)
2481 print_dentry_full_path(dentry);
2483 print_dentry_detailed(dentry);
2487 /* Print the files contained in an image(s) in a WIM file. */
2489 imagex_dir(int argc, tchar **argv, int cmd)
2491 const tchar *wimfile;
2492 WIMStruct *wim = NULL;
2495 const tchar *path = WIMLIB_WIM_ROOT_PATH;
2497 struct print_dentry_options options = {
2500 int iterate_flags = WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE;
2502 for_opt(c, dir_options) {
2504 case IMAGEX_PATH_OPTION:
2507 case IMAGEX_DETAILED_OPTION:
2508 options.detailed = true;
2510 case IMAGEX_ONE_FILE_ONLY_OPTION:
2511 iterate_flags &= ~WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE;
2521 imagex_error(T("Must specify a WIM file"));
2525 imagex_error(T("Too many arguments"));
2530 ret = wimlib_open_wim_with_progress(wimfile, 0, &wim,
2531 imagex_progress_func, NULL);
2536 image = wimlib_resolve_image(wim, argv[1]);
2537 ret = verify_image_exists(image, argv[1], wimfile);
2539 goto out_wimlib_free;
2541 /* No image specified; default to image 1, but only if the WIM
2542 * contains exactly one image. */
2544 struct wimlib_wim_info info;
2546 wimlib_get_wim_info(wim, &info);
2547 if (info.image_count != 1) {
2548 imagex_error(T("\"%"TS"\" contains %d images; Please "
2549 "select one (or all)."),
2550 wimfile, info.image_count);
2557 ret = wimlib_iterate_dir_tree(wim, image, path, iterate_flags,
2558 print_dentry, &options);
2565 usage(CMD_DIR, stderr);
2570 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2573 imagex_export(int argc, tchar **argv, int cmd)
2577 int export_flags = WIMLIB_EXPORT_FLAG_GIFT;
2578 int write_flags = 0;
2579 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2580 const tchar *src_wimfile;
2581 const tchar *src_image_num_or_name;
2582 const tchar *dest_wimfile;
2584 const tchar *dest_name;
2585 const tchar *dest_desc;
2587 struct wimlib_wim_info src_info;
2588 WIMStruct *dest_wim;
2593 STRING_SET(refglobs);
2594 unsigned num_threads = 0;
2595 uint32_t chunk_size = UINT32_MAX;
2596 uint32_t pack_chunk_size = UINT32_MAX;
2597 int pack_ctype = WIMLIB_COMPRESSION_TYPE_INVALID;
2599 for_opt(c, export_options) {
2601 case IMAGEX_BOOT_OPTION:
2602 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2604 case IMAGEX_CHECK_OPTION:
2605 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2606 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2608 case IMAGEX_NOCHECK_OPTION:
2609 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2611 case IMAGEX_COMPRESS_OPTION:
2612 compression_type = get_compression_type(optarg);
2613 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2616 case IMAGEX_RECOMPRESS_OPTION:
2617 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
2619 case IMAGEX_COMPRESS_SLOW_OPTION:
2620 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
2621 set_compress_slow();
2623 case IMAGEX_PACK_STREAMS_OPTION:
2624 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
2626 case IMAGEX_CHUNK_SIZE_OPTION:
2627 chunk_size = parse_chunk_size(optarg);
2628 if (chunk_size == UINT32_MAX)
2631 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
2632 pack_chunk_size = parse_chunk_size(optarg);
2633 if (pack_chunk_size == UINT32_MAX)
2636 case IMAGEX_PACK_COMPRESS_OPTION:
2637 pack_ctype = get_compression_type(optarg);
2638 if (pack_ctype == WIMLIB_COMPRESSION_TYPE_INVALID)
2641 case IMAGEX_REF_OPTION:
2642 ret = string_set_append(&refglobs, optarg);
2644 goto out_free_refglobs;
2646 case IMAGEX_THREADS_OPTION:
2647 num_threads = parse_num_threads(optarg);
2648 if (num_threads == UINT_MAX)
2651 case IMAGEX_REBUILD_OPTION:
2652 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2654 case IMAGEX_PIPABLE_OPTION:
2655 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2657 case IMAGEX_NOT_PIPABLE_OPTION:
2658 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
2660 case IMAGEX_WIMBOOT_OPTION:
2661 export_flags |= WIMLIB_EXPORT_FLAG_WIMBOOT;
2669 if (argc < 3 || argc > 5)
2671 src_wimfile = argv[0];
2672 src_image_num_or_name = argv[1];
2673 dest_wimfile = argv[2];
2674 dest_name = (argc >= 4) ? argv[3] : NULL;
2675 dest_desc = (argc >= 5) ? argv[4] : NULL;
2676 ret = wimlib_open_wim_with_progress(src_wimfile, open_flags, &src_wim,
2677 imagex_progress_func, NULL);
2679 goto out_free_refglobs;
2681 wimlib_get_wim_info(src_wim, &src_info);
2683 /* Determine if the destination is an existing file or not. If so, we
2684 * try to append the exported image(s) to it; otherwise, we create a new
2685 * WIM containing the exported image(s). Furthermore, determine if we
2686 * need to write a pipable WIM directly to standard output. */
2688 if (tstrcmp(dest_wimfile, T("-")) == 0) {
2690 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2691 imagex_error("Can't write a non-pipable WIM to "
2692 "standard output! Specify --pipable\n"
2693 " if you want to create a pipable WIM "
2694 "(but read the docs first).");
2696 goto out_free_src_wim;
2699 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2701 dest_wimfile = NULL;
2702 dest_wim_fd = STDOUT_FILENO;
2703 imagex_info_file = stderr;
2704 set_fd_to_binary_mode(dest_wim_fd);
2707 if (dest_wimfile != NULL && tstat(dest_wimfile, &stbuf) == 0) {
2709 /* Destination file exists. */
2711 if (!S_ISREG(stbuf.st_mode)) {
2712 imagex_error(T("\"%"TS"\" is not a regular file"),
2715 goto out_free_src_wim;
2717 ret = wimlib_open_wim_with_progress(dest_wimfile,
2719 WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2721 imagex_progress_func,
2724 goto out_free_src_wim;
2726 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2727 /* The user specified a compression type, but we're
2728 * exporting to an existing WIM. Make sure the
2729 * specified compression type is the same as the
2730 * compression type of the existing destination WIM. */
2731 struct wimlib_wim_info dest_info;
2733 wimlib_get_wim_info(dest_wim, &dest_info);
2734 if (compression_type != dest_info.compression_type) {
2735 imagex_error(T("Cannot specify a compression type that is "
2736 "not the same as that used in the "
2737 "destination WIM"));
2739 goto out_free_dest_wim;
2745 if (errno != ENOENT) {
2746 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2749 goto out_free_src_wim;
2752 /* dest_wimfile is not an existing file, so create a new WIM. */
2754 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2755 /* The user did not specify a compression type; default
2756 * to that of the source WIM, unless --pack-streams,
2757 * --solid, or --wimboot was specified. */
2759 if (write_flags & WIMLIB_WRITE_FLAG_PACK_STREAMS)
2760 compression_type = WIMLIB_COMPRESSION_TYPE_LZMS;
2761 else if (export_flags & WIMLIB_EXPORT_FLAG_WIMBOOT)
2762 compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
2764 compression_type = src_info.compression_type;
2766 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2768 goto out_free_src_wim;
2770 wimlib_register_progress_function(dest_wim,
2771 imagex_progress_func, NULL);
2773 if ((export_flags & WIMLIB_EXPORT_FLAG_WIMBOOT)
2774 && compression_type == WIMLIB_COMPRESSION_TYPE_XPRESS)
2776 /* For --wimboot export, use small XPRESS chunks. */
2777 wimlib_set_output_chunk_size(dest_wim, 4096);
2778 } else if (compression_type == src_info.compression_type &&
2779 chunk_size == UINT32_MAX)
2781 /* Use same chunk size if compression type is the same. */
2782 wimlib_set_output_chunk_size(dest_wim, src_info.chunk_size);
2786 if (chunk_size != UINT32_MAX) {
2787 /* Set destination chunk size. */
2788 ret = wimlib_set_output_chunk_size(dest_wim, chunk_size);
2790 goto out_free_dest_wim;
2792 if (pack_ctype != WIMLIB_COMPRESSION_TYPE_INVALID) {
2793 ret = wimlib_set_output_pack_compression_type(dest_wim, pack_ctype);
2795 goto out_free_dest_wim;
2797 if (pack_chunk_size != UINT32_MAX) {
2798 ret = wimlib_set_output_pack_chunk_size(dest_wim, pack_chunk_size);
2800 goto out_free_dest_wim;
2803 image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2804 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2806 goto out_free_dest_wim;
2808 if (refglobs.num_strings) {
2809 ret = wim_reference_globs(src_wim, &refglobs, open_flags);
2811 goto out_free_dest_wim;
2814 if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
2815 image == WIMLIB_ALL_IMAGES && src_info.boot_index == 0)
2817 imagex_error(T("--boot specified for all-images export, but source WIM "
2818 "has no bootable image."));
2820 goto out_free_dest_wim;
2823 ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2824 dest_desc, export_flags);
2826 if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2827 do_resource_not_found_warning(src_wimfile,
2828 &src_info, &refglobs);
2830 goto out_free_dest_wim;
2834 ret = wimlib_overwrite(dest_wim, write_flags, num_threads);
2835 else if (dest_wimfile)
2836 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2837 write_flags, num_threads);
2839 ret = wimlib_write_to_fd(dest_wim, dest_wim_fd,
2840 WIMLIB_ALL_IMAGES, write_flags,
2843 wimlib_free(dest_wim);
2845 wimlib_free(src_wim);
2847 string_set_destroy(&refglobs);
2851 usage(CMD_EXPORT, stderr);
2854 goto out_free_refglobs;
2857 /* Extract files or directories from a WIM image */
2859 imagex_extract(int argc, tchar **argv, int cmd)
2866 const tchar *wimfile;
2867 const tchar *image_num_or_name;
2868 tchar *dest_dir = T(".");
2869 int extract_flags = WIMLIB_EXTRACT_FLAG_NORPFIX |
2870 WIMLIB_EXTRACT_FLAG_GLOB_PATHS |
2871 WIMLIB_EXTRACT_FLAG_STRICT_GLOB;
2872 int notlist_extract_flags = WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE;
2874 STRING_SET(refglobs);
2876 tchar *root_path = WIMLIB_WIM_ROOT_PATH;
2878 for_opt(c, extract_options) {
2880 case IMAGEX_CHECK_OPTION:
2881 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2883 case IMAGEX_VERBOSE_OPTION:
2884 /* No longer does anything. */
2886 case IMAGEX_REF_OPTION:
2887 ret = string_set_append(&refglobs, optarg);
2889 goto out_free_refglobs;
2891 case IMAGEX_UNIX_DATA_OPTION:
2892 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2894 case IMAGEX_NO_ACLS_OPTION:
2895 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2897 case IMAGEX_STRICT_ACLS_OPTION:
2898 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2900 case IMAGEX_NO_ATTRIBUTES_OPTION:
2901 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES;
2903 case IMAGEX_DEST_DIR_OPTION:
2906 case IMAGEX_TO_STDOUT_OPTION:
2907 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2908 imagex_info_file = stderr;
2909 imagex_be_quiet = true;
2910 set_fd_to_binary_mode(STDOUT_FILENO);
2912 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2913 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2914 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2916 case IMAGEX_NO_GLOBS_OPTION:
2917 extract_flags &= ~WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2919 case IMAGEX_NULLGLOB_OPTION:
2920 extract_flags &= ~WIMLIB_EXTRACT_FLAG_STRICT_GLOB;
2922 case IMAGEX_PRESERVE_DIR_STRUCTURE_OPTION:
2923 notlist_extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE;
2925 case IMAGEX_WIMBOOT_OPTION:
2926 extract_flags |= WIMLIB_EXTRACT_FLAG_WIMBOOT;
2938 if (!(extract_flags & (WIMLIB_EXTRACT_FLAG_GLOB_PATHS |
2939 WIMLIB_EXTRACT_FLAG_STRICT_GLOB)))
2941 imagex_error(T("Can't combine --no-globs and --nullglob!"));
2946 image_num_or_name = argv[1];
2951 ret = wimlib_open_wim_with_progress(wimfile, open_flags, &wim,
2952 imagex_progress_func, NULL);
2954 goto out_free_refglobs;
2956 image = wimlib_resolve_image(wim, image_num_or_name);
2957 ret = verify_image_exists_and_is_single(image,
2961 goto out_wimlib_free;
2963 if (refglobs.num_strings) {
2964 ret = wim_reference_globs(wim, &refglobs, open_flags);
2966 goto out_wimlib_free;
2972 extract_flags &= ~WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2975 while (argc != 0 && ret == 0) {
2979 num_paths < argc && argv[num_paths][0] != T('@');
2984 ret = wimlib_extract_paths(wim, image, dest_dir,
2985 (const tchar **)argv,
2987 extract_flags | notlist_extract_flags);
2991 ret = wimlib_extract_pathlist(wim, image, dest_dir,
3000 if (!imagex_be_quiet)
3001 imagex_printf(T("Done extracting files.\n"));
3002 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
3003 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_STRICT_GLOB |
3004 WIMLIB_EXTRACT_FLAG_GLOB_PATHS))
3005 == (WIMLIB_EXTRACT_FLAG_STRICT_GLOB |
3006 WIMLIB_EXTRACT_FLAG_GLOB_PATHS))
3009 T("Note: You can use the '--nullglob' "
3010 "option to ignore missing files.\n"));
3012 tfprintf(stderr, T("Note: You can use `%"TS"' to see what "
3013 "files and directories\n"
3014 " are in the WIM image.\n"),
3015 get_cmd_string(CMD_DIR, false));
3016 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
3017 struct wimlib_wim_info info;
3019 wimlib_get_wim_info(wim, &info);
3020 do_resource_not_found_warning(wimfile, &info, &refglobs);
3025 string_set_destroy(&refglobs);
3029 usage(CMD_EXTRACT, stderr);
3032 goto out_free_refglobs;
3035 /* Prints information about a WIM file; also can mark an image as bootable,
3036 * change the name of an image, or change the description of an image. */
3038 imagex_info(int argc, tchar **argv, int cmd)
3043 bool nocheck = false;
3044 bool header = false;
3045 bool lookup_table = false;
3047 bool short_header = true;
3048 const tchar *xml_out_file = NULL;
3049 const tchar *wimfile;
3050 const tchar *image_num_or_name;
3051 const tchar *new_name;
3052 const tchar *new_desc;
3057 struct wimlib_wim_info info;
3059 for_opt(c, info_options) {
3061 case IMAGEX_BOOT_OPTION:
3064 case IMAGEX_CHECK_OPTION:
3067 case IMAGEX_NOCHECK_OPTION:
3070 case IMAGEX_HEADER_OPTION:
3072 short_header = false;
3074 case IMAGEX_LOOKUP_TABLE_OPTION:
3075 lookup_table = true;
3076 short_header = false;
3078 case IMAGEX_XML_OPTION:
3080 short_header = false;
3082 case IMAGEX_EXTRACT_XML_OPTION:
3083 xml_out_file = optarg;
3084 short_header = false;
3086 case IMAGEX_METADATA_OPTION:
3087 imagex_error(T("The --metadata option has been removed. "
3088 "Use 'wimdir --detail' instead."));
3097 if (argc < 1 || argc > 4)
3101 image_num_or_name = (argc >= 2) ? argv[1] : T("all");
3102 new_name = (argc >= 3) ? argv[2] : NULL;
3103 new_desc = (argc >= 4) ? argv[3] : NULL;
3105 if (check && nocheck) {
3106 imagex_error(T("Can't specify both --check and --nocheck"));
3111 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3113 ret = wimlib_open_wim_with_progress(wimfile, open_flags, &wim,
3114 imagex_progress_func, NULL);
3118 wimlib_get_wim_info(wim, &info);
3120 image = wimlib_resolve_image(wim, image_num_or_name);
3121 ret = WIMLIB_ERR_INVALID_IMAGE;
3122 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
3123 verify_image_exists(image, image_num_or_name, wimfile);
3125 imagex_error(T("If you would like to set the boot "
3126 "index to 0, specify image \"0\" with "
3127 "the --boot flag."));
3129 goto out_wimlib_free;
3132 if (boot && info.image_count == 0) {
3133 imagex_error(T("--boot is meaningless on a WIM with no images"));
3134 goto out_wimlib_free;
3137 if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
3139 imagex_error(T("Cannot specify the --boot flag "
3140 "without specifying a specific "
3141 "image in a multi-image WIM"));
3142 goto out_wimlib_free;
3145 imagex_error(T("Cannot specify the NEW_NAME "
3146 "without specifying a specific "
3147 "image in a multi-image WIM"));
3148 goto out_wimlib_free;
3152 /* Operations that print information are separated from operations that
3153 * recreate the WIM file. */
3154 if (!new_name && !boot) {
3156 /* Read-only operations */
3158 if (image == WIMLIB_NO_IMAGE) {
3159 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
3160 image_num_or_name, wimfile);
3161 goto out_wimlib_free;
3164 if (image == WIMLIB_ALL_IMAGES && short_header)
3165 print_wim_information(wimfile, &info);
3168 wimlib_print_header(wim);
3171 if (info.total_parts != 1) {
3172 tfprintf(stderr, T("Warning: Only showing the lookup table "
3173 "for part %d of a %d-part WIM.\n"),
3174 info.part_number, info.total_parts);
3176 print_lookup_table(wim);
3180 ret = wimlib_extract_xml_data(wim, stdout);
3182 goto out_wimlib_free;
3188 fp = tfopen(xml_out_file, T("wb"));
3190 imagex_error_with_errno(T("Failed to open the "
3191 "file \"%"TS"\" for "
3195 goto out_wimlib_free;
3197 ret = wimlib_extract_xml_data(wim, fp);
3199 imagex_error(T("Failed to close the file "
3205 goto out_wimlib_free;
3209 wimlib_print_available_images(wim, image);
3214 /* Modification operations */
3216 if (image == WIMLIB_ALL_IMAGES)
3219 if (image == WIMLIB_NO_IMAGE && new_name) {
3220 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
3221 "when using image 0"), new_name);
3223 goto out_wimlib_free;
3227 if (image == info.boot_index) {
3228 imagex_printf(T("Image %d is already marked as "
3229 "bootable.\n"), image);
3232 imagex_printf(T("Marking image %d as bootable.\n"),
3234 info.boot_index = image;
3235 ret = wimlib_set_wim_info(wim, &info,
3236 WIMLIB_CHANGE_BOOT_INDEX);
3238 goto out_wimlib_free;
3242 if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
3244 imagex_printf(T("Image %d is already named \"%"TS"\".\n"),
3248 imagex_printf(T("Changing the name of image %d to "
3249 "\"%"TS"\".\n"), image, new_name);
3250 ret = wimlib_set_image_name(wim, image, new_name);
3252 goto out_wimlib_free;
3256 const tchar *old_desc;
3257 old_desc = wimlib_get_image_description(wim, image);
3258 if (old_desc && !tstrcmp(old_desc, new_desc)) {
3259 imagex_printf(T("The description of image %d is already "
3260 "\"%"TS"\".\n"), image, new_desc);
3263 imagex_printf(T("Changing the description of image %d "
3264 "to \"%"TS"\".\n"), image, new_desc);
3265 ret = wimlib_set_image_descripton(wim, image,
3268 goto out_wimlib_free;
3272 /* Only call wimlib_overwrite() if something actually needs to
3274 if (boot || new_name || new_desc ||
3275 (check && !info.has_integrity_table) ||
3276 (nocheck && info.has_integrity_table))
3278 int write_flags = 0;
3281 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3283 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3284 ret = wimlib_overwrite(wim, write_flags, 1);
3286 imagex_printf(T("The file \"%"TS"\" was not modified "
3287 "because nothing needed to be done.\n"),
3298 usage(CMD_INFO, stderr);
3304 /* Join split WIMs into one part WIM */
3306 imagex_join(int argc, tchar **argv, int cmd)
3309 int swm_open_flags = 0;
3310 int wim_write_flags = 0;
3311 const tchar *output_path;
3314 for_opt(c, join_options) {
3316 case IMAGEX_CHECK_OPTION:
3317 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3318 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3328 imagex_error(T("Must specify one or more split WIM (.swm) "
3332 output_path = argv[0];
3333 ret = wimlib_join_with_progress((const tchar * const *)++argv,
3338 imagex_progress_func,
3344 usage(CMD_JOIN, stderr);
3349 #if WIM_MOUNTING_SUPPORTED
3351 /* Mounts a WIM image. */
3353 imagex_mount_rw_or_ro(int argc, tchar **argv, int cmd)
3356 int mount_flags = 0;
3358 const tchar *staging_dir = NULL;
3359 const tchar *wimfile;
3362 struct wimlib_wim_info info;
3366 STRING_SET(refglobs);
3368 if (cmd == CMD_MOUNTRW) {
3369 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
3370 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3373 for_opt(c, mount_options) {
3375 case IMAGEX_ALLOW_OTHER_OPTION:
3376 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
3378 case IMAGEX_CHECK_OPTION:
3379 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3381 case IMAGEX_DEBUG_OPTION:
3382 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
3384 case IMAGEX_STREAMS_INTERFACE_OPTION:
3385 if (!tstrcasecmp(optarg, T("none")))
3386 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
3387 else if (!tstrcasecmp(optarg, T("xattr")))
3388 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
3389 else if (!tstrcasecmp(optarg, T("windows")))
3390 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
3392 imagex_error(T("Unknown stream interface \"%"TS"\""),
3397 case IMAGEX_REF_OPTION:
3398 ret = string_set_append(&refglobs, optarg);
3400 goto out_free_refglobs;
3402 case IMAGEX_STAGING_DIR_OPTION:
3403 staging_dir = optarg;
3405 case IMAGEX_UNIX_DATA_OPTION:
3406 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
3414 if (argc != 2 && argc != 3)
3419 ret = wimlib_open_wim_with_progress(wimfile, open_flags, &wim,
3420 imagex_progress_func, NULL);
3422 goto out_free_refglobs;
3424 wimlib_get_wim_info(wim, &info);
3427 /* Image explicitly specified. */
3428 image = wimlib_resolve_image(wim, argv[1]);
3430 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
3434 /* No image specified; default to image 1, but only if the WIM
3435 * contains exactly one image. */
3437 if (info.image_count != 1) {
3438 imagex_error(T("\"%"TS"\" contains %d images; Please "
3439 "select one."), wimfile, info.image_count);
3447 if (refglobs.num_strings) {
3448 ret = wim_reference_globs(wim, &refglobs, open_flags);
3453 ret = wimlib_mount_image(wim, image, dir, mount_flags, staging_dir);
3455 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
3457 image, wimfile, dir);
3462 string_set_destroy(&refglobs);
3468 goto out_free_refglobs;
3470 #endif /* WIM_MOUNTING_SUPPORTED */
3472 /* Rebuild a WIM file */
3474 imagex_optimize(int argc, tchar **argv, int cmd)
3477 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3478 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
3479 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
3480 uint32_t chunk_size = UINT32_MAX;
3481 uint32_t pack_chunk_size = UINT32_MAX;
3482 int pack_ctype = WIMLIB_COMPRESSION_TYPE_INVALID;
3485 const tchar *wimfile;
3488 unsigned num_threads = 0;
3490 for_opt(c, optimize_options) {
3492 case IMAGEX_CHECK_OPTION:
3493 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3494 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3496 case IMAGEX_NOCHECK_OPTION:
3497 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3499 case IMAGEX_COMPRESS_OPTION:
3500 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3501 compression_type = get_compression_type(optarg);
3502 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
3505 case IMAGEX_RECOMPRESS_OPTION:
3506 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3508 case IMAGEX_COMPRESS_SLOW_OPTION:
3509 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3510 set_compress_slow();
3512 case IMAGEX_CHUNK_SIZE_OPTION:
3513 chunk_size = parse_chunk_size(optarg);
3514 if (chunk_size == UINT32_MAX)
3517 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
3518 pack_chunk_size = parse_chunk_size(optarg);
3519 if (pack_chunk_size == UINT32_MAX)
3522 case IMAGEX_PACK_COMPRESS_OPTION:
3523 pack_ctype = get_compression_type(optarg);
3524 if (pack_ctype == WIMLIB_COMPRESSION_TYPE_INVALID)
3527 case IMAGEX_PACK_STREAMS_OPTION:
3528 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
3529 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3531 case IMAGEX_THREADS_OPTION:
3532 num_threads = parse_num_threads(optarg);
3533 if (num_threads == UINT_MAX)
3536 case IMAGEX_PIPABLE_OPTION:
3537 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
3539 case IMAGEX_NOT_PIPABLE_OPTION:
3540 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
3554 ret = wimlib_open_wim_with_progress(wimfile, open_flags, &wim,
3555 imagex_progress_func, NULL);
3559 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
3560 /* Change compression type. */
3561 ret = wimlib_set_output_compression_type(wim, compression_type);
3563 goto out_wimlib_free;
3566 if (chunk_size != UINT32_MAX) {
3567 /* Change chunk size. */
3568 ret = wimlib_set_output_chunk_size(wim, chunk_size);
3570 goto out_wimlib_free;
3572 if (pack_ctype != WIMLIB_COMPRESSION_TYPE_INVALID) {
3573 ret = wimlib_set_output_pack_compression_type(wim, pack_ctype);
3575 goto out_wimlib_free;
3577 if (pack_chunk_size != UINT32_MAX) {
3578 ret = wimlib_set_output_pack_chunk_size(wim, pack_chunk_size);
3580 goto out_wimlib_free;
3583 old_size = file_get_size(wimfile);
3584 tprintf(T("\"%"TS"\" original size: "), wimfile);
3586 tputs(T("Unknown"));
3588 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3590 ret = wimlib_overwrite(wim, write_flags, num_threads);
3592 imagex_error(T("Optimization of \"%"TS"\" failed."), wimfile);
3593 goto out_wimlib_free;
3596 new_size = file_get_size(wimfile);
3597 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3599 tputs(T("Unknown"));
3601 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3603 tfputs(T("Space saved: "), stdout);
3604 if (new_size != -1 && old_size != -1) {
3605 tprintf(T("%lld KiB\n"),
3606 ((long long)old_size - (long long)new_size) >> 10);
3608 tputs(T("Unknown"));
3617 usage(CMD_OPTIMIZE, stderr);
3623 /* Split a WIM into a spanned set */
3625 imagex_split(int argc, tchar **argv, int cmd)
3629 int write_flags = 0;
3630 unsigned long part_size;
3635 for_opt(c, split_options) {
3637 case IMAGEX_CHECK_OPTION:
3638 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3639 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3651 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3652 if (tmp == argv[2] || *tmp) {
3653 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3654 imagex_error(T("The part size must be an integer or "
3655 "floating-point number of megabytes."));
3658 ret = wimlib_open_wim_with_progress(argv[0], open_flags, &wim,
3659 imagex_progress_func, NULL);
3663 ret = wimlib_split(wim, argv[1], part_size, write_flags);
3669 usage(CMD_SPLIT, stderr);
3675 #if WIM_MOUNTING_SUPPORTED
3676 /* Unmounts a mounted WIM image. */
3678 imagex_unmount(int argc, tchar **argv, int cmd)
3681 int unmount_flags = 0;
3684 for_opt(c, unmount_options) {
3686 case IMAGEX_COMMIT_OPTION:
3687 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3689 case IMAGEX_CHECK_OPTION:
3690 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3692 case IMAGEX_REBUILD_OPTION:
3693 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3695 case IMAGEX_LAZY_OPTION:
3696 case IMAGEX_FORCE_OPTION:
3697 /* Now, unmount is lazy by default. However, committing
3698 * the image will fail with
3699 * WIMLIB_ERR_MOUNTED_IMAGE_IS_BUSY if there are open
3700 * file descriptors on the WIM image. The
3701 * WIMLIB_UNMOUNT_FLAG_FORCE option forces these file
3702 * descriptors to be closed. */
3703 unmount_flags |= WIMLIB_UNMOUNT_FLAG_FORCE;
3705 case IMAGEX_NEW_IMAGE_OPTION:
3706 unmount_flags |= WIMLIB_UNMOUNT_FLAG_NEW_IMAGE;
3717 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_NEW_IMAGE) {
3718 if (!(unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT)) {
3719 imagex_error(T("--new-image is meaningless "
3720 "without --commit also specified!"));
3725 ret = wimlib_unmount_image_with_progress(argv[0], unmount_flags,
3726 imagex_progress_func, NULL);
3728 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3729 if (ret == WIMLIB_ERR_MOUNTED_IMAGE_IS_BUSY) {
3731 "\tNote: Use --commit --force to force changes "
3732 "to be committed, regardless\n"
3733 "\t of open files.\n"));
3740 usage(CMD_UNMOUNT, stderr);
3745 #endif /* WIM_MOUNTING_SUPPORTED */
3748 * Add, delete, or rename files in a WIM image.
3751 imagex_update(int argc, tchar **argv, int cmd)
3753 const tchar *wimfile;
3757 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3758 int write_flags = 0;
3759 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3760 int default_add_flags = WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
3761 WIMLIB_ADD_FLAG_VERBOSE |
3762 WIMLIB_ADD_FLAG_WINCONFIG;
3763 int default_delete_flags = 0;
3764 unsigned num_threads = 0;
3766 tchar *cmd_file_contents;
3767 size_t cmd_file_nchars;
3768 struct wimlib_update_command *cmds;
3770 tchar *command_str = NULL;
3771 tchar *config_file = NULL;
3772 tchar *wimboot_config = NULL;
3774 for_opt(c, update_options) {
3776 /* Generic or write options */
3777 case IMAGEX_THREADS_OPTION:
3778 num_threads = parse_num_threads(optarg);
3779 if (num_threads == UINT_MAX)
3782 case IMAGEX_CHECK_OPTION:
3783 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3784 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3786 case IMAGEX_REBUILD_OPTION:
3787 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3789 case IMAGEX_COMMAND_OPTION:
3791 imagex_error(T("--command may only be specified "
3792 "one time. Please provide\n"
3793 " the update commands "
3794 "on standard input instead."));
3797 command_str = tstrdup(optarg);
3799 imagex_error(T("Out of memory!"));
3803 case IMAGEX_WIMBOOT_CONFIG_OPTION:
3804 wimboot_config = optarg;
3806 /* Default delete options */
3807 case IMAGEX_FORCE_OPTION:
3808 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3810 case IMAGEX_RECURSIVE_OPTION:
3811 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3814 /* Global add option */
3815 case IMAGEX_CONFIG_OPTION:
3816 default_add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
3817 config_file = optarg;
3820 /* Default add options */
3821 case IMAGEX_VERBOSE_OPTION:
3822 /* No longer does anything. */
3824 case IMAGEX_DEREFERENCE_OPTION:
3825 default_add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
3827 case IMAGEX_UNIX_DATA_OPTION:
3828 default_add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
3830 case IMAGEX_NO_ACLS_OPTION:
3831 default_add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
3833 case IMAGEX_STRICT_ACLS_OPTION:
3834 default_add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
3836 case IMAGEX_NO_REPLACE_OPTION:
3837 default_add_flags |= WIMLIB_ADD_FLAG_NO_REPLACE;
3846 if (argc != 1 && argc != 2)
3850 ret = wimlib_open_wim_with_progress(wimfile, open_flags, &wim,
3851 imagex_progress_func, NULL);
3853 goto out_free_command_str;
3856 /* Image explicitly specified. */
3857 image = wimlib_resolve_image(wim, argv[1]);
3858 ret = verify_image_exists_and_is_single(image, argv[1],
3861 goto out_wimlib_free;
3863 /* No image specified; default to image 1, but only if the WIM
3864 * contains exactly one image. */
3865 struct wimlib_wim_info info;
3867 wimlib_get_wim_info(wim, &info);
3868 if (info.image_count != 1) {
3869 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3870 wimfile, info.image_count);
3877 /* Read update commands from standard input, or the command string if
3880 cmd_file_contents = NULL;
3881 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3885 goto out_free_cmd_file_contents;
3887 } else if (!wimboot_config) {
3888 if (isatty(STDIN_FILENO)) {
3889 tputs(T("Reading update commands from standard input..."));
3890 recommend_man_page(CMD_UPDATE, stdout);
3892 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3893 if (!cmd_file_contents) {
3895 goto out_wimlib_free;
3898 /* Parse the update commands */
3899 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3903 goto out_free_cmd_file_contents;
3906 cmd_file_contents = NULL;
3911 /* Set default flags and capture config on the update commands */
3912 for (size_t i = 0; i < num_cmds; i++) {
3913 switch (cmds[i].op) {
3914 case WIMLIB_UPDATE_OP_ADD:
3915 cmds[i].add.add_flags |= default_add_flags;
3916 cmds[i].add.config_file = config_file;
3918 case WIMLIB_UPDATE_OP_DELETE:
3919 cmds[i].delete_.delete_flags |= default_delete_flags;
3926 /* Execute the update commands */
3927 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags);
3931 if (wimboot_config) {
3932 /* --wimboot-config=FILE is short for an
3933 * "add FILE /Windows/System32/WimBootCompress.ini" command.
3935 struct wimlib_update_command cmd;
3937 cmd.op = WIMLIB_UPDATE_OP_ADD;
3938 cmd.add.fs_source_path = wimboot_config;
3939 cmd.add.wim_target_path = T("/Windows/System32/WimBootCompress.ini");
3940 cmd.add.config_file = NULL;
3941 cmd.add.add_flags = 0;
3943 ret = wimlib_update_image(wim, image, &cmd, 1, update_flags);
3948 /* Overwrite the updated WIM */
3949 ret = wimlib_overwrite(wim, write_flags, num_threads);
3952 out_free_cmd_file_contents:
3953 free(cmd_file_contents);
3956 out_free_command_str:
3961 usage(CMD_UPDATE, stderr);
3964 goto out_free_command_str;
3969 struct imagex_command {
3971 int (*func)(int argc, tchar **argv, int cmd);
3974 static const struct imagex_command imagex_commands[] = {
3975 [CMD_APPEND] = {T("append"), imagex_capture_or_append},
3976 [CMD_APPLY] = {T("apply"), imagex_apply},
3977 [CMD_CAPTURE] = {T("capture"), imagex_capture_or_append},
3978 [CMD_DELETE] = {T("delete"), imagex_delete},
3979 [CMD_DIR ] = {T("dir"), imagex_dir},
3980 [CMD_EXPORT] = {T("export"), imagex_export},
3981 [CMD_EXTRACT] = {T("extract"), imagex_extract},
3982 [CMD_INFO] = {T("info"), imagex_info},
3983 [CMD_JOIN] = {T("join"), imagex_join},
3984 #if WIM_MOUNTING_SUPPORTED
3985 [CMD_MOUNT] = {T("mount"), imagex_mount_rw_or_ro},
3986 [CMD_MOUNTRW] = {T("mountrw"), imagex_mount_rw_or_ro},
3988 [CMD_OPTIMIZE] = {T("optimize"), imagex_optimize},
3989 [CMD_SPLIT] = {T("split"), imagex_split},
3990 #if WIM_MOUNTING_SUPPORTED
3991 [CMD_UNMOUNT] = {T("unmount"), imagex_unmount},
3993 [CMD_UPDATE] = {T("update"), imagex_update},
3996 static const tchar *usage_strings[] = {
3999 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
4000 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
4001 " [--nocheck] [--flags EDITION_ID] [--dereference]\n"
4002 " [--config=FILE] [--threads=NUM_THREADS] [--source-list]\n"
4003 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
4004 " [--update-of=[WIMFILE:]IMAGE] [--wimboot]\n"
4008 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME | all)]\n"
4009 " (DIRECTORY | NTFS_VOLUME) [--check] [--ref=\"GLOB\"]\n"
4010 " [--no-acls] [--strict-acls] [--no-attributes]\n"
4011 " [--rpfix] [--norpfix] [--include-invalid-names]\n"
4012 " [--wimboot] [--unix-data]\n"
4016 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
4017 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
4018 " [--nocheck] [--compress=TYPE] [--flags EDITION_ID]\n"
4019 " [--dereference] [--config=FILE] [--threads=NUM_THREADS]\n"
4020 " [--source-list] [--no-acls] [--strict-acls] [--rpfix]\n"
4021 " [--norpfix] [--update-of=[WIMFILE:]IMAGE]\n"
4022 " [--delta-from=WIMFILE] [--wimboot] [--unix-data]\n"
4026 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n"
4027 " [--check] [--soft]\n"
4031 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH] [--detailed]\n"
4035 " %"TS" SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
4036 " DEST_WIMFILE [DEST_IMAGE_NAME [DEST_IMAGE_DESCRIPTION]]\n"
4037 " [--boot] [--check] [--nocheck] [--compress=TYPE]\n"
4038 " [--ref=\"GLOB\"] [--threads=NUM_THREADS] [--rebuild]\n"
4043 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME) [(PATH | @LISTFILE)...]\n"
4044 " [--check] [--ref=\"GLOB\"] [--dest-dir=CMD_DIR]\n"
4045 " [--to-stdout] [--no-acls] [--strict-acls]\n"
4046 " [--no-attributes] [--include-invalid-names]\n"
4047 " [--no-globs] [--nullglob] [--preserve-dir-structure]\n"
4051 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME) [NEW_NAME\n"
4052 " [NEW_DESC]]] [--boot] [--check] [--nocheck] [--xml]\n"
4053 " [--extract-xml FILE] [--header] [--lookup-table]\n"
4057 " %"TS" OUT_WIMFILE SPLIT_WIM_PART... [--check]\n"
4059 #if WIM_MOUNTING_SUPPORTED
4062 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
4063 " [--check] [--streams-interface=INTERFACE]\n"
4064 " [--ref=\"GLOB\"] [--allow-other] [--unix-data]\n"
4068 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
4069 " [--check] [--streams-interface=INTERFACE]\n"
4070 " [--staging-dir=CMD_DIR] [--allow-other] [--unix-data]\n"
4075 " %"TS" WIMFILE [--check] [--nocheck] [--recompress]\n"
4076 " [--recompress-slow] [--compress=TYPE]\n"
4077 " [--threads=NUM_THREADS]\n"
4081 " %"TS" WIMFILE SPLIT_WIM_PART_1 PART_SIZE_MB [--check]\n"
4083 #if WIM_MOUNTING_SUPPORTED
4086 " %"TS" DIRECTORY [--commit] [--force] [--new-image]\n"
4087 " [--check] [--rebuild]\n"
4092 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
4093 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
4094 " [DEFAULT_DELETE_OPTIONS] [--command=STRING]\n"
4095 " [--wimboot-config=FILE| [< CMDFILE]\n"
4099 static const tchar *invocation_name;
4100 static int invocation_cmd = CMD_NONE;
4102 static const tchar *get_cmd_string(int cmd, bool nospace)
4104 static tchar buf[50];
4105 if (cmd == CMD_NONE) {
4106 tsprintf(buf, T("%"TS), T(IMAGEX_PROGNAME));
4107 } else if (invocation_cmd != CMD_NONE) {
4108 tsprintf(buf, T("wim%"TS), imagex_commands[cmd].name);
4110 const tchar *format;
4113 format = T("%"TS"-%"TS"");
4115 format = T("%"TS" %"TS"");
4116 tsprintf(buf, format, invocation_name, imagex_commands[cmd].name);
4124 static const tchar *s =
4126 IMAGEX_PROGNAME " (distributed with " PACKAGE " " PACKAGE_VERSION ")\n"
4127 "Copyright (C) 2012, 2013, 2014 Eric Biggers\n"
4128 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
4129 "This is free software: you are free to change and redistribute it.\n"
4130 "There is NO WARRANTY, to the extent permitted by law.\n"
4132 "Report bugs to "PACKAGE_BUGREPORT".\n"
4139 help_or_version(int argc, tchar **argv, int cmd)
4144 for (i = 1; i < argc; i++) {
4146 if (p[0] == T('-') && p[1] == T('-')) {
4148 if (!tstrcmp(p, T("help"))) {
4149 if (cmd == CMD_NONE)
4154 } else if (!tstrcmp(p, T("version"))) {
4163 print_usage_string(int cmd, FILE *fp)
4165 tfprintf(fp, usage_strings[cmd], get_cmd_string(cmd, false));
4169 recommend_man_page(int cmd, FILE *fp)
4171 const tchar *format_str;
4173 format_str = T("Uncommon options are not listed;\n"
4174 "See %"TS".pdf in the doc directory for more details.\n");
4176 format_str = T("Uncommon options are not listed;\n"
4177 "Try `man %"TS"' for more details.\n");
4179 tfprintf(fp, format_str, get_cmd_string(cmd, true));
4183 usage(int cmd, FILE *fp)
4185 tfprintf(fp, T("Usage:\n"));
4186 print_usage_string(cmd, fp);
4187 tfprintf(fp, T("\n"));
4188 recommend_man_page(cmd, fp);
4194 tfprintf(fp, T("Usage:\n"));
4195 for (int cmd = 0; cmd < CMD_MAX; cmd++) {
4196 print_usage_string(cmd, fp);
4197 tfprintf(fp, T("\n"));
4199 static const tchar *extra =
4202 " %"TS" --version\n"
4204 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
4207 tfprintf(fp, extra, invocation_name, invocation_name);
4208 recommend_man_page(CMD_NONE, fp);
4211 /* Entry point for wimlib's ImageX implementation. On UNIX the command
4212 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
4213 * something else), while on Windows the command arguments will be UTF-16LE
4214 * encoded 'wchar_t' strings. */
4217 wmain(int argc, wchar_t **argv, wchar_t **envp)
4219 main(int argc, char **argv)
4226 imagex_info_file = stdout;
4227 invocation_name = tbasename(argv[0]);
4230 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
4231 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
4235 setlocale(LC_ALL, "");
4236 codeset = nl_langinfo(CODESET);
4237 if (!strstr(codeset, "UTF-8") &&
4238 !strstr(codeset, "UTF8") &&
4239 !strstr(codeset, "utf-8") &&
4240 !strstr(codeset, "utf8"))
4243 "WARNING: Running %"TS" in a UTF-8 locale is recommended!\n"
4244 " Maybe try: `export LANG=en_US.UTF-8'?\n"
4245 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
4246 " to any value to force wimlib to use UTF-8.\n",
4252 #endif /* !__WIN32__ */
4255 tchar *igcase = tgetenv(T("WIMLIB_IMAGEX_IGNORE_CASE"));
4256 if (igcase != NULL) {
4257 if (!tstrcmp(igcase, T("no")) ||
4258 !tstrcmp(igcase, T("0")))
4259 init_flags |= WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE;
4260 else if (!tstrcmp(igcase, T("yes")) ||
4261 !tstrcmp(igcase, T("1")))
4262 init_flags |= WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE;
4265 "WARNING: Ignoring unknown setting of "
4266 "WIMLIB_IMAGEX_IGNORE_CASE\n");
4271 /* Allow being invoked as wimCOMMAND (e.g. wimapply). */
4273 if (!tstrncmp(invocation_name, T("wim"), 3) &&
4274 tstrcmp(invocation_name, T(IMAGEX_PROGNAME))) {
4275 for (int i = 0; i < CMD_MAX; i++) {
4276 if (!tstrcmp(invocation_name + 3,
4277 imagex_commands[i].name))
4286 /* Unless already known from the invocation name, determine which
4287 * command was specified. */
4288 if (cmd == CMD_NONE) {
4290 imagex_error(T("No command specified!\n"));
4294 for (int i = 0; i < CMD_MAX; i++) {
4295 if (!tstrcmp(argv[1], imagex_commands[i].name)) {
4300 if (cmd != CMD_NONE) {
4306 /* Handle --help and --version. --help can be either for the program as
4307 * a whole (cmd == CMD_NONE) or just for a specific command (cmd !=
4308 * CMD_NONE). Note: help_or_version() will not return if a --help or
4309 * --version argument was found. */
4310 help_or_version(argc, argv, cmd);
4312 /* Bail if a valid command was not specified. */
4313 if (cmd == CMD_NONE) {
4314 imagex_error(T("Unrecognized command: `%"TS"'\n"), argv[1]);
4319 /* Enable warning and error messages in wimlib to be more user-friendly.
4321 wimlib_set_print_errors(true);
4323 /* Initialize wimlib. */
4324 ret = wimlib_global_init(init_flags);
4326 goto out_check_status;
4328 /* Call the command handler function. */
4329 ret = imagex_commands[cmd].func(argc, argv, cmd);
4331 /* Check for error writing to standard output, especially since for some
4332 * commands, writing to standard output is part of the program's actual
4333 * behavior and not just for informational purposes. */
4334 if (ferror(stdout) || fclose(stdout)) {
4335 imagex_error_with_errno(T("error writing to standard output"));
4340 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
4341 * itself (not by wimlib). 0 indicates success. > 0 indicates a wimlib
4342 * error code from which an error message can be printed. */
4344 imagex_error(T("Exiting with error code %d:\n"
4346 wimlib_get_error_string(ret));
4347 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
4348 imagex_error_with_errno(T("errno"));
4350 /* Make wimlib free any resources it's holding (although this is not
4351 * strictly necessary because the process is ending anyway). */
4352 wimlib_global_cleanup();