4 * Use wimlib to create, modify, extract, mount, unmount, or display information
9 * Copyright (C) 2012, 2013, 2014 Eric Biggers
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 # include "config.h" /* Need for PACKAGE_VERSION, etc. */
30 #include "wimlib_tchar.h"
50 # include "imagex-win32.h"
51 # define OS_PREFERRED_PATH_SEPARATOR L'\\'
52 # define OS_PREFERRED_PATH_SEPARATOR_STRING L"\\"
53 # define print_security_descriptor win32_print_security_descriptor
56 # include <langinfo.h>
57 # define OS_PREFERRED_PATH_SEPARATOR '/'
58 # define OS_PREFERRED_PATH_SEPARATOR_STRING "/"
59 # define print_security_descriptor default_print_security_descriptor
60 static inline void set_fd_to_binary_mode(int fd)
65 /* Don't confuse the user by presenting the mounting commands on Windows when
66 * they will never work. However on UNIX-like systems we always present them,
67 * even if WITH_FUSE is not defined at this point, as to not tie the build of
68 * wimlib-imagex to a specific build of wimlib. */
70 # define WIM_MOUNTING_SUPPORTED 0
72 # define WIM_MOUNTING_SUPPORTED 1
75 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
78 is_any_path_separator(tchar c)
80 return c == T('/') || c == T('\\');
83 /* Like basename(), but handles both forward and backwards slashes. */
85 tbasename(tchar *path)
87 tchar *p = tstrchr(path, T('\0'));
92 if (!is_any_path_separator(*--p))
100 if (is_any_path_separator(*--p))
105 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
119 #if WIM_MOUNTING_SUPPORTED
125 #if WIM_MOUNTING_SUPPORTED
132 static void usage(int cmd, FILE *fp);
133 static void usage_all(FILE *fp);
134 static void recommend_man_page(int cmd, FILE *fp);
135 static const tchar *get_cmd_string(int cmd, bool nospace);
137 static int imagex_progress_func(enum wimlib_progress_msg msg,
138 const union wimlib_progress_info *info);
140 static bool imagex_be_quiet = false;
141 static FILE *imagex_info_file;
143 #define imagex_printf(format, ...) \
144 tfprintf(imagex_info_file, format, ##__VA_ARGS__)
147 IMAGEX_ALLOW_OTHER_OPTION,
150 IMAGEX_CHUNK_SIZE_OPTION,
151 IMAGEX_COMMAND_OPTION,
152 IMAGEX_COMMIT_OPTION,
153 IMAGEX_COMPRESS_OPTION,
154 IMAGEX_COMPRESS_SLOW_OPTION,
155 IMAGEX_CONFIG_OPTION,
157 IMAGEX_DELTA_FROM_OPTION,
158 IMAGEX_DEREFERENCE_OPTION,
159 IMAGEX_DEST_DIR_OPTION,
160 IMAGEX_DETAILED_OPTION,
161 IMAGEX_EXTRACT_XML_OPTION,
164 IMAGEX_HARDLINK_OPTION,
165 IMAGEX_HEADER_OPTION,
166 IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
168 IMAGEX_LOOKUP_TABLE_OPTION,
169 IMAGEX_METADATA_OPTION,
170 IMAGEX_NEW_IMAGE_OPTION,
171 IMAGEX_NOCHECK_OPTION,
172 IMAGEX_NORPFIX_OPTION,
173 IMAGEX_NOT_PIPABLE_OPTION,
174 IMAGEX_NO_ACLS_OPTION,
175 IMAGEX_NO_ATTRIBUTES_OPTION,
176 IMAGEX_NO_WILDCARDS_OPTION,
177 IMAGEX_NULLGLOB_OPTION,
178 IMAGEX_ONE_FILE_ONLY_OPTION,
179 IMAGEX_PACK_CHUNK_SIZE_OPTION,
180 IMAGEX_PACK_STREAMS_OPTION,
182 IMAGEX_PIPABLE_OPTION,
183 IMAGEX_PRESERVE_DIR_STRUCTURE_OPTION,
184 IMAGEX_REBUILD_OPTION,
185 IMAGEX_RECOMPRESS_OPTION,
186 IMAGEX_RECURSIVE_OPTION,
188 IMAGEX_RESUME_OPTION,
191 IMAGEX_SOURCE_LIST_OPTION,
192 IMAGEX_STAGING_DIR_OPTION,
193 IMAGEX_STREAMS_INTERFACE_OPTION,
194 IMAGEX_STRICT_ACLS_OPTION,
195 IMAGEX_SYMLINK_OPTION,
196 IMAGEX_THREADS_OPTION,
197 IMAGEX_TO_STDOUT_OPTION,
198 IMAGEX_UNIX_DATA_OPTION,
199 IMAGEX_UPDATE_OF_OPTION,
200 IMAGEX_VERBOSE_OPTION,
201 IMAGEX_WIMBOOT_OPTION,
205 static const struct option apply_options[] = {
206 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
207 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
208 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
209 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
210 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
211 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
212 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
213 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
214 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
215 {T("no-attributes"), no_argument, NULL, IMAGEX_NO_ATTRIBUTES_OPTION},
216 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
217 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
218 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
220 /* --resume is undocumented for now as it needs improvement. */
221 {T("resume"), no_argument, NULL, IMAGEX_RESUME_OPTION},
222 {T("wimboot"), no_argument, NULL, IMAGEX_WIMBOOT_OPTION},
226 static const struct option capture_or_append_options[] = {
227 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
228 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
229 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
230 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
231 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
232 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
233 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
234 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
235 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
236 {T("pack-streams"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
237 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
238 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
239 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
240 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
241 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
242 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
243 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
244 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
245 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
246 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
247 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
248 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
249 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
250 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
251 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
252 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
253 {T("update-of"), required_argument, NULL, IMAGEX_UPDATE_OF_OPTION},
254 {T("delta-from"), required_argument, NULL, IMAGEX_DELTA_FROM_OPTION},
255 {T("wimboot"), no_argument, NULL, IMAGEX_WIMBOOT_OPTION},
259 static const struct option delete_options[] = {
260 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
261 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
265 static const struct option dir_options[] = {
266 {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
267 {T("detailed"), no_argument, NULL, IMAGEX_DETAILED_OPTION},
268 {T("one-file-only"), no_argument, NULL, IMAGEX_ONE_FILE_ONLY_OPTION},
272 static const struct option export_options[] = {
273 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
274 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
275 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
276 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
277 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
278 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
279 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
280 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
281 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
282 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
283 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
284 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
285 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
286 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
287 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
288 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_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_WILDCARDS_OPTION},
305 {T("nullglob"), no_argument, NULL, IMAGEX_NULLGLOB_OPTION},
306 {T("preserve-dir-structure"), no_argument, NULL, IMAGEX_PRESERVE_DIR_STRUCTURE_OPTION},
307 {T("wimboot"), no_argument, NULL, IMAGEX_WIMBOOT_OPTION},
311 static const struct option info_options[] = {
312 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
313 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
314 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
315 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
316 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
317 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
318 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
319 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
320 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
324 static const struct option join_options[] = {
325 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
329 static const struct option mount_options[] = {
330 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
331 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
332 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
333 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
334 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
335 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
336 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
340 static const struct option optimize_options[] = {
341 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
342 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
343 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
344 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
345 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
346 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
347 {T("recompress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
348 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
349 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
350 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
351 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
352 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
353 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
354 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
355 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
359 static const struct option split_options[] = {
360 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
364 static const struct option unmount_options[] = {
365 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
366 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
367 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
368 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
369 {T("new-image"), no_argument, NULL, IMAGEX_NEW_IMAGE_OPTION},
373 static const struct option update_options[] = {
374 /* Careful: some of the options here set the defaults for update
375 * commands, but the flags given to an actual update command (and not to
376 * `imagex update' itself are also handled in
377 * update_command_add_option(). */
378 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
379 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
380 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
381 {T("command"), required_argument, NULL, IMAGEX_COMMAND_OPTION},
383 /* Default delete options */
384 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
385 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
387 /* Global add option */
388 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
390 /* Default add options */
391 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
392 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
393 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
394 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
395 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
396 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
402 # define _format_attribute(type, format_str, args_start) \
403 __attribute__((format(type, format_str, args_start)))
405 # define _format_attribute(type, format_str, args_start)
408 /* Print formatted error message to stderr. */
409 static void _format_attribute(printf, 1, 2)
410 imagex_error(const tchar *format, ...)
413 va_start(va, format);
414 tfputs(T("ERROR: "), stderr);
415 tvfprintf(stderr, format, va);
416 tputc(T('\n'), stderr);
420 /* Print formatted error message to stderr. */
421 static void _format_attribute(printf, 1, 2)
422 imagex_error_with_errno(const tchar *format, ...)
424 int errno_save = errno;
426 va_start(va, format);
427 tfputs(T("ERROR: "), stderr);
428 tvfprintf(stderr, format, va);
429 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
434 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
436 if (image == WIMLIB_NO_IMAGE) {
437 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
438 " Please specify a 1-based image index or "
439 "image name. To list the images\n"
440 " contained in the WIM archive, run\n"
442 " %"TS" \"%"TS"\"\n"),
443 image_name, wim_name,
444 get_cmd_string(CMD_INFO, false), wim_name);
445 return WIMLIB_ERR_INVALID_IMAGE;
451 verify_image_is_single(int image)
453 if (image == WIMLIB_ALL_IMAGES) {
454 imagex_error(T("Cannot specify all images for this action!"));
455 return WIMLIB_ERR_INVALID_IMAGE;
461 verify_image_exists_and_is_single(int image, const tchar *image_name,
462 const tchar *wim_name)
465 ret = verify_image_exists(image, image_name, wim_name);
467 ret = verify_image_is_single(image);
471 /* Parse the argument to --compress */
473 get_compression_type(const tchar *optarg)
475 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
476 return WIMLIB_COMPRESSION_TYPE_LZX;
477 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
478 return WIMLIB_COMPRESSION_TYPE_XPRESS;
479 else if (!tstrcasecmp(optarg, T("recovery")) || !tstrcasecmp(optarg, T("lzms")))
480 return WIMLIB_COMPRESSION_TYPE_LZMS;
481 else if (!tstrcasecmp(optarg, T("none")))
482 return WIMLIB_COMPRESSION_TYPE_NONE;
484 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
485 "\"maximum\", \"fast\", or \"none\"."), optarg);
486 return WIMLIB_COMPRESSION_TYPE_INVALID;
491 set_compress_slow(void)
493 static const struct wimlib_lzx_compressor_params lzx_slow_params = {
495 .size = sizeof(struct wimlib_lzx_compressor_params),
497 .algorithm = WIMLIB_LZX_ALGORITHM_SLOW,
500 .use_len2_matches = 1,
501 .nice_match_length = 96,
502 .num_optim_passes = 4,
503 .max_search_depth = 100,
504 .max_matches_per_pos = 10,
505 .main_nostat_cost = 15,
506 .len_nostat_cost = 15,
507 .aligned_nostat_cost = 7,
512 static const struct wimlib_lzms_compressor_params lzms_slow_params = {
514 .size = sizeof(struct wimlib_lzms_compressor_params),
516 .min_match_length = 2,
517 .max_match_length = UINT32_MAX,
518 .nice_match_length = 96,
519 .max_search_depth = 100,
520 .max_matches_per_pos = 10,
521 .optim_array_length = 1024,
524 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
525 &lzx_slow_params.hdr);
527 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZMS,
528 &lzms_slow_params.hdr);
532 const tchar **strings;
533 unsigned num_strings;
534 unsigned num_alloc_strings;
537 #define STRING_SET_INITIALIZER \
538 { .strings = NULL, .num_strings = 0, .num_alloc_strings = 0, }
540 #define STRING_SET(_strings) \
541 struct string_set _strings = STRING_SET_INITIALIZER
544 string_set_append(struct string_set *set, const tchar *glob)
546 unsigned num_alloc_strings = set->num_alloc_strings;
548 if (set->num_strings == num_alloc_strings) {
549 const tchar **new_strings;
551 num_alloc_strings += 4;
552 new_strings = realloc(set->strings,
553 sizeof(set->strings[0]) * num_alloc_strings);
555 imagex_error(T("Out of memory!"));
558 set->strings = new_strings;
559 set->num_alloc_strings = num_alloc_strings;
561 set->strings[set->num_strings++] = glob;
566 string_set_destroy(struct string_set *set)
572 wim_reference_globs(WIMStruct *wim, struct string_set *set, int open_flags)
574 return wimlib_reference_resource_files(wim, set->strings,
576 WIMLIB_REF_FLAG_GLOB_ENABLE,
578 imagex_progress_func);
582 do_resource_not_found_warning(const tchar *wimfile,
583 const struct wimlib_wim_info *info,
584 const struct string_set *refglobs)
586 if (info->total_parts > 1) {
587 if (refglobs->num_strings == 0) {
588 imagex_error(T("\"%"TS"\" is part of a split WIM. "
589 "Use --ref to specify the other parts."),
592 imagex_error(T("Perhaps the '--ref' argument did not "
593 "specify all other parts of the split "
597 imagex_error(T("If this is a delta WIM, use the --ref argument "
598 "to specify the WIM(s) on which it is based."));
602 /* Returns the size of a file given its name, or -1 if the file does not exist
603 * or its size cannot be determined. */
605 file_get_size(const tchar *filename)
608 if (tstat(filename, &st) == 0)
615 PARSE_STRING_SUCCESS = 0,
616 PARSE_STRING_FAILURE = 1,
617 PARSE_STRING_NONE = 2,
621 * Parses a string token from an array of characters.
623 * Tokens are either whitespace-delimited, or double or single-quoted.
625 * @line_p: Pointer to the pointer to the line of data. Will be updated
626 * to point past the string token iff the return value is
627 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
630 * @len_p: @len_p initially stores the length of the line of data, which may
631 * be 0, and it will be updated to the number of bytes remaining in
632 * the line iff the return value is PARSE_STRING_SUCCESS.
634 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
635 * parsed string token will be returned here.
637 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
638 * PARSE_STRING_FAILURE if the data was invalid due to a missing
639 * closing quote; or PARSE_STRING_NONE if the line ended before the
640 * beginning of a string token was found.
643 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
646 tchar *line = *line_p;
650 /* Skip leading whitespace */
653 return PARSE_STRING_NONE;
654 if (!istspace(*line) && *line != T('\0'))
660 if (quote_char == T('"') || quote_char == T('\'')) {
665 line = tmemchr(line, quote_char, len);
667 imagex_error(T("Missing closing quote: %"TS), fn - 1);
668 return PARSE_STRING_FAILURE;
671 /* Unquoted string. Go until whitespace. Line is terminated
672 * by '\0', so no need to check 'len'. */
676 } while (!istspace(*line) && *line != T('\0'));
683 return PARSE_STRING_SUCCESS;
686 /* Parses a line of data (not an empty line or comment) in the source list file
687 * format. (See the man page for 'wimlib-imagex capture' for details on this
688 * format and the meaning.)
690 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
691 * len == 0. The data in @line will be modified by this function call.
693 * @len: Length of the line of data.
695 * @source: On success, the capture source and target described by the line is
696 * written into this destination. Note that it will contain pointers
697 * to data in the @line array.
699 * Returns true if the line was valid; false otherwise. */
701 parse_source_list_line(tchar *line, size_t len,
702 struct wimlib_capture_source *source)
706 ret = parse_string(&line, &len, &source->fs_source_path);
707 if (ret != PARSE_STRING_SUCCESS)
709 ret = parse_string(&line, &len, &source->wim_target_path);
710 if (ret == PARSE_STRING_NONE)
711 source->wim_target_path = source->fs_source_path;
712 return ret != PARSE_STRING_FAILURE;
715 /* Returns %true if the given line of length @len > 0 is a comment or empty line
716 * in the source list file format. */
718 is_comment_line(const tchar *line, size_t len)
721 if (*line == T('#') || *line == T(';'))
723 if (!istspace(*line) && *line != T('\0'))
733 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
736 tchar *contents = *contents_p;
737 size_t nchars = *nchars_p;
740 for (i = 0; i < nchars; i++)
741 if (contents[i] == T('\n'))
744 /* Handle last line not terminated by a newline */
745 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
746 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
748 imagex_error(T("Out of memory!"));
751 contents[nchars] = T('\n');
752 *contents_p = contents;
760 /* Parses a file in the source list format. (See the man page for
761 * 'wimlib-imagex capture' for details on this format and the meaning.)
763 * @source_list_contents: Contents of the source list file. Note that this
764 * buffer will be modified to save memory allocations,
765 * and cannot be freed until the returned array of
766 * wimlib_capture_source's has also been freed.
768 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
771 * @nsources_ret: On success, the length of the returned array is
774 * Returns: An array of `struct wimlib_capture_source's that can be passed to
775 * the wimlib_add_image_multisource() function to specify how a WIM image is to
777 static struct wimlib_capture_source *
778 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
779 size_t *nsources_ret)
783 struct wimlib_capture_source *sources;
786 nlines = text_file_count_lines(source_list_contents_p,
787 &source_list_nchars);
791 /* Always allocate at least 1 slot, just in case the implementation of
792 * calloc() returns NULL if 0 bytes are requested. */
793 sources = calloc(nlines ?: 1, sizeof(*sources));
795 imagex_error(T("out of memory"));
798 p = *source_list_contents_p;
800 for (i = 0; i < nlines; i++) {
801 /* XXX: Could use rawmemchr() here instead, but it may not be
802 * available on all platforms. */
803 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
804 size_t len = endp - p + 1;
806 if (!is_comment_line(p, len)) {
807 if (!parse_source_list_line(p, len, &sources[j++])) {
819 /* Reads the contents of a file into memory. */
821 file_get_contents(const tchar *filename, size_t *len_ret)
828 if (tstat(filename, &stbuf) != 0) {
829 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
834 fp = tfopen(filename, T("rb"));
836 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
840 buf = malloc(len ? len : 1);
842 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
843 "contents of file \"%"TS"\""), len, filename);
846 if (fread(buf, 1, len, fp) != len) {
847 imagex_error_with_errno(T("Failed to read %zu bytes from the "
848 "file \"%"TS"\""), len, filename);
862 /* Read standard input until EOF and return the full contents in a malloc()ed
863 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
866 stdin_get_contents(size_t *len_ret)
868 /* stdin can, of course, be a pipe or other non-seekable file, so the
869 * total length of the data cannot be pre-determined */
871 size_t newlen = 1024;
875 char *p = realloc(buf, newlen);
876 size_t bytes_read, bytes_to_read;
878 imagex_error(T("out of memory while reading stdin"));
882 bytes_to_read = newlen - pos;
883 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
885 if (bytes_read != bytes_to_read) {
890 imagex_error_with_errno(T("error reading stdin"));
904 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
907 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
909 *num_tchars_ret = num_bytes;
911 #else /* !__WIN32__ */
912 /* On Windows, translate the text to UTF-16LE */
916 if (num_bytes >= 2 &&
917 (((unsigned char)text[0] == 0xff && (unsigned char)text[1] == 0xfe) ||
918 ((unsigned char)text[0] <= 0x7f && (unsigned char)text[1] == 0x00)))
920 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
921 * with something that looks like an ASCII character encoded as
922 * a UTF-16LE code unit. Assume the file is encoded as
923 * UTF-16LE. This is not a 100% reliable check. */
924 num_wchars = num_bytes / 2;
925 text_wstr = (wchar_t*)text;
927 /* File does not look like UTF-16LE. Assume it is encoded in
928 * the current Windows code page. I think these are always
929 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
930 * should work as expected. */
931 text_wstr = win32_mbs_to_wcs(text,
936 *num_tchars_ret = num_wchars;
938 #endif /* __WIN32__ */
942 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
947 contents = file_get_contents(filename, &num_bytes);
950 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
954 stdin_get_text_contents(size_t *num_tchars_ret)
959 contents = stdin_get_contents(&num_bytes);
962 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
965 #define TO_PERCENT(numerator, denominator) \
966 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
968 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
969 #define MEBIBYTE_MIN_NBYTES 10000000ULL
970 #define KIBIBYTE_MIN_NBYTES 10000ULL
973 get_unit(uint64_t total_bytes, const tchar **name_ret)
975 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
976 *name_ret = T("GiB");
978 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
979 *name_ret = T("MiB");
981 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
982 *name_ret = T("KiB");
985 *name_ret = T("bytes");
990 static struct wimlib_progress_info_scan last_scan_progress;
993 report_scan_progress(const struct wimlib_progress_info_scan *scan, bool done)
995 uint64_t prev_count, cur_count;
997 prev_count = last_scan_progress.num_nondirs_scanned +
998 last_scan_progress.num_dirs_scanned;
999 cur_count = scan->num_nondirs_scanned + scan->num_dirs_scanned;
1001 if (done || prev_count == 0 || cur_count >= prev_count + 100 ||
1002 cur_count % 128 == 0)
1004 unsigned unit_shift;
1005 const tchar *unit_name;
1007 unit_shift = get_unit(scan->num_bytes_scanned, &unit_name);
1008 imagex_printf(T("\r%"PRIu64" %"TS" scanned (%"PRIu64" files, "
1009 "%"PRIu64" directories) "),
1010 scan->num_bytes_scanned >> unit_shift,
1012 scan->num_nondirs_scanned,
1013 scan->num_dirs_scanned);
1014 last_scan_progress = *scan;
1018 /* Progress callback function passed to various wimlib functions. */
1020 imagex_progress_func(enum wimlib_progress_msg msg,
1021 const union wimlib_progress_info *info)
1023 unsigned percent_done;
1024 unsigned unit_shift;
1025 const tchar *unit_name;
1027 if (imagex_be_quiet)
1030 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1032 static bool first = true;
1034 imagex_printf(T("Writing %"TS"-compressed data "
1035 "using %u thread%"TS"\n"),
1036 wimlib_get_compression_type_string(
1037 info->write_streams.compression_type),
1038 info->write_streams.num_threads,
1039 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1043 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1044 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1045 info->write_streams.total_bytes);
1047 if (info->write_streams.total_parts <= 1) {
1048 imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1049 "written (%u%% done)"),
1050 info->write_streams.completed_bytes >> unit_shift,
1052 info->write_streams.total_bytes >> unit_shift,
1056 imagex_printf(T("\rWriting resources from part %u of %u: "
1057 "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written"),
1058 (info->write_streams.completed_parts ==
1059 info->write_streams.total_parts) ?
1060 info->write_streams.completed_parts :
1061 info->write_streams.completed_parts + 1,
1062 info->write_streams.total_parts,
1063 info->write_streams.completed_bytes >> unit_shift,
1065 info->write_streams.total_bytes >> unit_shift,
1069 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1070 imagex_printf(T("\n"));
1072 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1073 imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
1074 if (*info->scan.wim_target_path) {
1075 imagex_printf(T(" (loading as WIM path: "
1076 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
1077 info->scan.wim_target_path);
1079 imagex_printf(T("\n"));
1081 memset(&last_scan_progress, 0, sizeof(last_scan_progress));
1083 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1084 switch (info->scan.status) {
1085 case WIMLIB_SCAN_DENTRY_OK:
1086 report_scan_progress(&info->scan, false);
1088 case WIMLIB_SCAN_DENTRY_EXCLUDED:
1089 imagex_printf(T("\nExcluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1091 case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
1092 imagex_printf(T("\nWARNING: Excluding unsupported file or directory\n"
1093 " \"%"TS"\" from capture\n"), info->scan.cur_path);
1095 case WIMLIB_SCAN_DENTRY_EXCLUDED_SYMLINK:
1096 imagex_printf(T("\nWARNING: Ignoring absolute symbolic link "
1097 "with out-of-tree target:\n"
1098 " \"%"TS"\" => \"%"TS"\"\n"
1099 " (Use --norpfix to capture "
1100 "absolute symbolic links as-is)\n"),
1101 info->scan.cur_path, info->scan.symlink_target);
1105 case WIMLIB_PROGRESS_MSG_SCAN_END:
1106 report_scan_progress(&info->scan, true);
1107 imagex_printf(T("\n"));
1109 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1110 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1111 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1112 info->integrity.total_bytes);
1113 imagex_printf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1114 "of %"PRIu64" %"TS" (%u%%) done"),
1115 info->integrity.filename,
1116 info->integrity.completed_bytes >> unit_shift,
1118 info->integrity.total_bytes >> unit_shift,
1121 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1122 imagex_printf(T("\n"));
1124 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1125 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1126 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1127 info->integrity.total_bytes);
1128 imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1129 "of %"PRIu64" %"TS" (%u%%) done"),
1130 info->integrity.completed_bytes >> unit_shift,
1132 info->integrity.total_bytes >> unit_shift,
1135 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1136 imagex_printf(T("\n"));
1138 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1139 imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1140 "to %"TS" \"%"TS"\"\n"),
1141 info->extract.image,
1142 info->extract.image_name,
1143 info->extract.wimfile_name,
1144 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1145 T("NTFS volume") : T("directory")),
1146 info->extract.target);
1148 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1149 if (info->extract.extract_root_wim_source_path[0]) {
1150 imagex_printf(T("Extracting \"%"TS"\" from image %d "
1151 "(\"%"TS"\") in \"%"TS"\" to \"%"TS"\"\n"),
1152 info->extract.extract_root_wim_source_path,
1153 info->extract.image,
1154 info->extract.image_name,
1155 info->extract.wimfile_name,
1156 info->extract.target);
1159 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1160 percent_done = TO_PERCENT(info->extract.completed_bytes,
1161 info->extract.total_bytes);
1162 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1163 imagex_printf(T("\rExtracting files: "
1164 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1165 info->extract.completed_bytes >> unit_shift,
1167 info->extract.total_bytes >> unit_shift,
1170 if (info->extract.completed_bytes >= info->extract.total_bytes)
1171 imagex_printf(T("\n"));
1173 case WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN:
1174 if (info->extract.total_parts != 1) {
1175 imagex_printf(T("\nReading split pipable WIM part %u of %u\n"),
1176 info->extract.part_number,
1177 info->extract.total_parts);
1180 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1181 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1182 imagex_printf(T("Setting timestamps on all extracted files...\n"));
1184 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1185 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1186 imagex_printf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1187 info->extract.target);
1190 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1191 percent_done = TO_PERCENT(info->split.completed_bytes,
1192 info->split.total_bytes);
1193 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1194 imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
1195 "%"PRIu64" %"TS" (%u%%) written\n"),
1196 info->split.part_name,
1197 info->split.cur_part_number,
1198 info->split.total_parts,
1199 info->split.completed_bytes >> unit_shift,
1201 info->split.total_bytes >> unit_shift,
1205 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1206 if (info->split.completed_bytes == info->split.total_bytes) {
1207 imagex_printf(T("Finished writing split WIM part %u of %u\n"),
1208 info->split.cur_part_number,
1209 info->split.total_parts);
1212 case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1213 switch (info->update.command->op) {
1214 case WIMLIB_UPDATE_OP_DELETE:
1215 imagex_printf(T("Deleted WIM path "
1216 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1217 info->update.command->delete_.wim_path);
1219 case WIMLIB_UPDATE_OP_RENAME:
1220 imagex_printf(T("Renamed WIM path "
1221 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
1222 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1223 info->update.command->rename.wim_source_path,
1224 info->update.command->rename.wim_target_path);
1226 case WIMLIB_UPDATE_OP_ADD:
1234 fflush(imagex_info_file);
1239 parse_num_threads(const tchar *optarg)
1242 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1243 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1244 imagex_error(T("Number of threads must be a non-negative integer!"));
1251 static uint32_t parse_chunk_size(const tchar *optarg)
1254 unsigned long chunk_size = tstrtoul(optarg, &tmp, 10);
1255 if (chunk_size >= UINT32_MAX || *tmp || tmp == optarg) {
1256 imagex_error(T("Chunk size must be a non-negative integer!"));
1265 * Parse an option passed to an update command.
1267 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1270 * @option: Text string for the option (beginning with --)
1272 * @cmd: `struct wimlib_update_command' that is being constructed for
1275 * Returns true if the option was recognized; false if not.
1278 update_command_add_option(int op, const tchar *option,
1279 struct wimlib_update_command *cmd)
1281 bool recognized = true;
1283 case WIMLIB_UPDATE_OP_ADD:
1284 if (!tstrcmp(option, T("--verbose")))
1285 cmd->add.add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
1286 else if (!tstrcmp(option, T("--unix-data")))
1287 cmd->add.add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
1288 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1289 cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
1290 else if (!tstrcmp(option, T("--strict-acls")))
1291 cmd->add.add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
1292 else if (!tstrcmp(option, T("--dereference")))
1293 cmd->add.add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
1297 case WIMLIB_UPDATE_OP_DELETE:
1298 if (!tstrcmp(option, T("--force")))
1299 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1300 else if (!tstrcmp(option, T("--recursive")))
1301 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1312 /* How many nonoption arguments each `imagex update' command expects */
1313 static const unsigned update_command_num_nonoptions[] = {
1314 [WIMLIB_UPDATE_OP_ADD] = 2,
1315 [WIMLIB_UPDATE_OP_DELETE] = 1,
1316 [WIMLIB_UPDATE_OP_RENAME] = 2,
1320 update_command_add_nonoption(int op, const tchar *nonoption,
1321 struct wimlib_update_command *cmd,
1322 unsigned num_nonoptions)
1325 case WIMLIB_UPDATE_OP_ADD:
1326 if (num_nonoptions == 0)
1327 cmd->add.fs_source_path = (tchar*)nonoption;
1329 cmd->add.wim_target_path = (tchar*)nonoption;
1331 case WIMLIB_UPDATE_OP_DELETE:
1332 cmd->delete_.wim_path = (tchar*)nonoption;
1334 case WIMLIB_UPDATE_OP_RENAME:
1335 if (num_nonoptions == 0)
1336 cmd->rename.wim_source_path = (tchar*)nonoption;
1338 cmd->rename.wim_target_path = (tchar*)nonoption;
1344 * Parse a command passed on stdin to `imagex update'.
1346 * @line: Text of the command.
1347 * @len: Length of the line, including a null terminator
1350 * @command: A `struct wimlib_update_command' to fill in from the parsed
1353 * @line_number: Line number of the command, for diagnostics.
1355 * Returns true on success; returns false on parse error.
1358 parse_update_command(tchar *line, size_t len,
1359 struct wimlib_update_command *command,
1363 tchar *command_name;
1365 size_t num_nonoptions;
1367 /* Get the command name ("add", "delete", "rename") */
1368 ret = parse_string(&line, &len, &command_name);
1369 if (ret != PARSE_STRING_SUCCESS)
1372 if (!tstrcasecmp(command_name, T("add"))) {
1373 op = WIMLIB_UPDATE_OP_ADD;
1374 } else if (!tstrcasecmp(command_name, T("delete"))) {
1375 op = WIMLIB_UPDATE_OP_DELETE;
1376 } else if (!tstrcasecmp(command_name, T("rename"))) {
1377 op = WIMLIB_UPDATE_OP_RENAME;
1379 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1380 command_name, line_number);
1385 /* Parse additional options and non-options as needed */
1390 ret = parse_string(&line, &len, &next_string);
1391 if (ret == PARSE_STRING_NONE) /* End of line */
1393 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1395 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1397 if (!update_command_add_option(op, next_string, command))
1399 imagex_error(T("Unrecognized option \"%"TS"\" to "
1400 "update command \"%"TS"\" on line %zu"),
1401 next_string, command_name, line_number);
1407 if (num_nonoptions == update_command_num_nonoptions[op])
1409 imagex_error(T("Unexpected argument \"%"TS"\" in "
1410 "update command on line %zu\n"
1411 " (The \"%"TS"\" command only "
1412 "takes %zu nonoption arguments!)\n"),
1413 next_string, line_number,
1414 command_name, num_nonoptions);
1417 update_command_add_nonoption(op, next_string,
1418 command, num_nonoptions);
1423 if (num_nonoptions != update_command_num_nonoptions[op]) {
1424 imagex_error(T("Not enough arguments to update command "
1425 "\"%"TS"\" on line %zu"), command_name, line_number);
1431 static struct wimlib_update_command *
1432 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1433 size_t *num_cmds_ret)
1437 struct wimlib_update_command *cmds;
1440 nlines = text_file_count_lines(cmd_file_contents_p,
1445 /* Always allocate at least 1 slot, just in case the implementation of
1446 * calloc() returns NULL if 0 bytes are requested. */
1447 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1449 imagex_error(T("out of memory"));
1452 p = *cmd_file_contents_p;
1454 for (i = 0; i < nlines; i++) {
1455 /* XXX: Could use rawmemchr() here instead, but it may not be
1456 * available on all platforms. */
1457 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1458 size_t len = endp - p + 1;
1460 if (!is_comment_line(p, len)) {
1461 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1472 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1473 * one image from a WIM file to a NTFS volume. */
1475 imagex_apply(int argc, tchar **argv, int cmd)
1479 int image = WIMLIB_NO_IMAGE;
1481 struct wimlib_wim_info info;
1483 const tchar *wimfile;
1484 const tchar *target;
1485 const tchar *image_num_or_name = NULL;
1486 int extract_flags = 0;
1488 STRING_SET(refglobs);
1490 for_opt(c, apply_options) {
1492 case IMAGEX_CHECK_OPTION:
1493 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1495 case IMAGEX_HARDLINK_OPTION:
1496 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1498 case IMAGEX_SYMLINK_OPTION:
1499 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1501 case IMAGEX_VERBOSE_OPTION:
1502 /* No longer does anything. */
1504 case IMAGEX_REF_OPTION:
1505 ret = string_set_append(&refglobs, optarg);
1507 goto out_free_refglobs;
1509 case IMAGEX_UNIX_DATA_OPTION:
1510 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1512 case IMAGEX_NO_ACLS_OPTION:
1513 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1515 case IMAGEX_STRICT_ACLS_OPTION:
1516 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1518 case IMAGEX_NO_ATTRIBUTES_OPTION:
1519 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES;
1521 case IMAGEX_NORPFIX_OPTION:
1522 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1524 case IMAGEX_RPFIX_OPTION:
1525 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1527 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1528 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1529 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1531 case IMAGEX_RESUME_OPTION:
1532 extract_flags |= WIMLIB_EXTRACT_FLAG_RESUME;
1534 case IMAGEX_WIMBOOT_OPTION:
1535 extract_flags |= WIMLIB_EXTRACT_FLAG_WIMBOOT;
1543 if (argc != 2 && argc != 3)
1548 if (!tstrcmp(wimfile, T("-"))) {
1549 /* Attempt to apply pipable WIM from standard input. */
1551 image_num_or_name = NULL;
1554 image_num_or_name = argv[1];
1559 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1560 imagex_progress_func);
1562 goto out_free_refglobs;
1564 wimlib_get_wim_info(wim, &info);
1567 /* Image explicitly specified. */
1568 image_num_or_name = argv[1];
1569 image = wimlib_resolve_image(wim, image_num_or_name);
1570 ret = verify_image_exists(image, image_num_or_name, wimfile);
1572 goto out_wimlib_free;
1575 /* No image specified; default to image 1, but only if the WIM
1576 * contains exactly one image. */
1578 if (info.image_count != 1) {
1579 imagex_error(T("\"%"TS"\" contains %d images; "
1580 "Please select one (or all)."),
1581 wimfile, info.image_count);
1590 if (refglobs.num_strings) {
1592 imagex_error(T("Can't specify --ref when applying from stdin!"));
1594 goto out_wimlib_free;
1596 ret = wim_reference_globs(wim, &refglobs, open_flags);
1598 goto out_wimlib_free;
1603 /* Interpret a regular file or block device target as a NTFS
1607 if (tstat(target, &stbuf)) {
1608 if (errno != ENOENT) {
1609 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1612 goto out_wimlib_free;
1615 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1616 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1622 ret = wimlib_extract_image(wim, image, target, extract_flags,
1623 imagex_progress_func);
1625 set_fd_to_binary_mode(STDIN_FILENO);
1626 ret = wimlib_extract_image_from_pipe(STDIN_FILENO,
1628 target, extract_flags,
1629 imagex_progress_func);
1632 imagex_printf(T("Done applying WIM image.\n"));
1633 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
1635 do_resource_not_found_warning(wimfile, &info, &refglobs);
1637 imagex_error(T( "If you are applying an image "
1638 "from a split pipable WIM,\n"
1639 " make sure you have "
1640 "concatenated together all parts."));
1646 string_set_destroy(&refglobs);
1650 usage(CMD_APPLY, stderr);
1652 goto out_free_refglobs;
1655 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1656 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1657 * the desired image. 'wimlib-imagex append': add a new image to an existing
1660 imagex_capture_or_append(int argc, tchar **argv, int cmd)
1663 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1664 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE |
1665 WIMLIB_ADD_IMAGE_FLAG_WINCONFIG |
1666 WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1667 int write_flags = 0;
1668 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
1669 uint32_t chunk_size = UINT32_MAX;
1670 uint32_t pack_chunk_size = UINT32_MAX;
1671 const tchar *wimfile;
1675 const tchar *flags_element = NULL;
1678 STRING_SET(base_wimfiles);
1679 WIMStruct **base_wims;
1681 WIMStruct *template_wim;
1682 const tchar *template_wimfile = NULL;
1683 const tchar *template_image_name_or_num = NULL;
1684 int template_image = WIMLIB_NO_IMAGE;
1687 unsigned num_threads = 0;
1692 tchar *config_file = NULL;
1694 bool source_list = false;
1695 size_t source_list_nchars = 0;
1696 tchar *source_list_contents;
1697 bool capture_sources_malloced;
1698 struct wimlib_capture_source *capture_sources;
1700 bool name_defaulted;
1701 bool compress_slow = false;
1703 for_opt(c, capture_or_append_options) {
1705 case IMAGEX_BOOT_OPTION:
1706 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1708 case IMAGEX_CHECK_OPTION:
1709 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1710 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1712 case IMAGEX_NOCHECK_OPTION:
1713 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
1715 case IMAGEX_CONFIG_OPTION:
1716 config_file = optarg;
1717 add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1719 case IMAGEX_COMPRESS_OPTION:
1720 compression_type = get_compression_type(optarg);
1721 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1724 case IMAGEX_COMPRESS_SLOW_OPTION:
1725 compress_slow = true;
1727 case IMAGEX_CHUNK_SIZE_OPTION:
1728 chunk_size = parse_chunk_size(optarg);
1729 if (chunk_size == UINT32_MAX)
1732 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
1733 pack_chunk_size = parse_chunk_size(optarg);
1734 if (pack_chunk_size == UINT32_MAX)
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 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_WIMBOOT) {
1832 compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
1834 compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1835 if (!compress_slow) {
1836 struct wimlib_lzx_compressor_params params = {
1837 .hdr.size = sizeof(params),
1838 .algorithm = WIMLIB_LZX_ALGORITHM_FAST,
1841 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
1849 set_compress_slow();
1851 if (!tstrcmp(wimfile, T("-"))) {
1852 /* Writing captured WIM to standard output. */
1854 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
1855 imagex_error("Can't write a non-pipable WIM to "
1856 "standard output! Specify --pipable\n"
1857 " if you want to create a pipable WIM "
1858 "(but read the docs first).");
1862 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1864 if (cmd == CMD_APPEND) {
1865 imagex_error(T("Using standard output for append does "
1866 "not make sense."));
1869 wim_fd = STDOUT_FILENO;
1871 imagex_info_file = stderr;
1872 set_fd_to_binary_mode(wim_fd);
1875 /* If template image was specified using --update-of=IMAGE rather
1876 * than --update-of=WIMFILE:IMAGE, set the default WIMFILE. */
1877 if (template_image_name_or_num && !template_wimfile) {
1878 if (base_wimfiles.num_strings == 1) {
1879 /* Capturing delta WIM based on single WIM: default to
1881 template_wimfile = base_wimfiles.strings[0];
1882 } else if (cmd == CMD_APPEND) {
1883 /* Appending to WIM: default to WIM being appended to.
1885 template_wimfile = wimfile;
1887 /* Capturing a normal (non-delta) WIM, so the WIM file
1888 * *must* be explicitly specified. */
1889 if (base_wimfiles.num_strings > 1) {
1890 imagex_error(T("For capture of delta WIM "
1891 "based on multiple existing "
1893 " '--update-of' must "
1894 "specify WIMFILE:IMAGE!"));
1896 imagex_error(T("For capture of non-delta WIM, "
1897 "'--update-of' must specify "
1906 name_defaulted = false;
1908 /* Set default name to SOURCE argument, omitting any directory
1909 * prefixes and trailing slashes. This requires making a copy
1910 * of @source. Leave some free characters at the end in case we
1911 * append a number to keep the name unique. */
1912 size_t source_name_len;
1914 source_name_len = tstrlen(source);
1915 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1916 name = tbasename(tstrcpy(source_copy, source));
1917 name_defaulted = true;
1919 /* Image description defaults to NULL if not given. */
1926 /* Set up capture sources in source list mode */
1927 if (source[0] == T('-') && source[1] == T('\0')) {
1928 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1930 source_list_contents = file_get_text_contents(source,
1931 &source_list_nchars);
1933 if (!source_list_contents)
1936 capture_sources = parse_source_list(&source_list_contents,
1939 if (!capture_sources) {
1941 goto out_free_source_list_contents;
1943 capture_sources_malloced = true;
1945 /* Set up capture source in non-source-list mode. */
1946 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1947 capture_sources[0].fs_source_path = source;
1948 capture_sources[0].wim_target_path = NULL;
1949 capture_sources[0].reserved = 0;
1951 capture_sources_malloced = false;
1952 source_list_contents = NULL;
1955 /* Open the existing WIM, or create a new one. */
1956 if (cmd == CMD_APPEND)
1957 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1958 imagex_progress_func);
1960 ret = wimlib_create_new_wim(compression_type, &wim);
1962 goto out_free_capture_sources;
1964 /* Set chunk size if non-default. */
1965 if (chunk_size != UINT32_MAX) {
1966 ret = wimlib_set_output_chunk_size(wim, chunk_size);
1969 } else if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_WIMBOOT) &&
1970 compression_type == WIMLIB_COMPRESSION_TYPE_XPRESS) {
1971 ret = wimlib_set_output_chunk_size(wim, 4096);
1975 if (pack_chunk_size != UINT32_MAX) {
1976 ret = wimlib_set_output_pack_chunk_size(wim, pack_chunk_size);
1982 /* Detect if source is regular file or block device and set NTFS volume
1987 if (tstat(source, &stbuf) == 0) {
1988 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1989 imagex_printf(T("Capturing WIM image from NTFS "
1990 "filesystem on \"%"TS"\"\n"), source);
1991 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1994 if (errno != ENOENT) {
1995 imagex_error_with_errno(T("Failed to stat "
1996 "\"%"TS"\""), source);
2004 /* If the user did not specify an image name, and the basename of the
2005 * source already exists as an image name in the WIM file, append a
2006 * suffix to make it unique. */
2007 if (cmd == CMD_APPEND && name_defaulted) {
2008 unsigned long conflict_idx;
2009 tchar *name_end = tstrchr(name, T('\0'));
2010 for (conflict_idx = 1;
2011 wimlib_image_name_in_use(wim, name);
2014 tsprintf(name_end, T(" (%lu)"), conflict_idx);
2018 /* If capturing a delta WIM, reference resources from the base WIMs
2019 * before adding the new image. */
2020 if (base_wimfiles.num_strings) {
2021 base_wims = calloc(base_wimfiles.num_strings,
2022 sizeof(base_wims[0]));
2023 if (base_wims == NULL) {
2024 imagex_error(T("Out of memory!"));
2029 for (size_t i = 0; i < base_wimfiles.num_strings; i++) {
2030 ret = wimlib_open_wim(base_wimfiles.strings[i],
2031 open_flags, &base_wims[i],
2032 imagex_progress_func);
2034 goto out_free_base_wims;
2038 ret = wimlib_reference_resources(wim, base_wims,
2039 base_wimfiles.num_strings, 0);
2041 goto out_free_base_wims;
2043 if (base_wimfiles.num_strings == 1) {
2044 imagex_printf(T("Capturing delta WIM based on \"%"TS"\"\n"),
2045 base_wimfiles.strings[0]);
2047 imagex_printf(T("Capturing delta WIM based on %u WIMs\n"),
2048 base_wimfiles.num_strings);
2055 /* If capturing or appending as an update of an existing (template) image,
2056 * open the WIM if needed and parse the image index. */
2057 if (template_image_name_or_num) {
2060 if (base_wimfiles.num_strings == 1 &&
2061 template_wimfile == base_wimfiles.strings[0]) {
2062 template_wim = base_wims[0];
2063 } else if (template_wimfile == wimfile) {
2066 ret = wimlib_open_wim(template_wimfile, open_flags,
2067 &template_wim, imagex_progress_func);
2069 goto out_free_base_wims;
2072 template_image = wimlib_resolve_image(template_wim,
2073 template_image_name_or_num);
2075 if (template_image_name_or_num[0] == T('-')) {
2078 struct wimlib_wim_info info;
2080 wimlib_get_wim_info(template_wim, &info);
2081 n = tstrtoul(template_image_name_or_num + 1, &tmp, 10);
2082 if (n >= 1 && n <= info.image_count &&
2084 tmp != template_image_name_or_num + 1)
2086 template_image = info.image_count - (n - 1);
2089 ret = verify_image_exists_and_is_single(template_image,
2090 template_image_name_or_num,
2093 goto out_free_template_wim;
2095 template_wim = NULL;
2098 ret = wimlib_add_image_multisource(wim,
2104 imagex_progress_func);
2106 goto out_free_template_wim;
2108 if (desc || flags_element || template_image_name_or_num) {
2109 /* User provided <DESCRIPTION> or <FLAGS> element, or an image
2110 * on which the added one is to be based has been specified with
2111 * --update-of. Get the index of the image we just
2112 * added, then use it to call the appropriate functions. */
2113 struct wimlib_wim_info info;
2115 wimlib_get_wim_info(wim, &info);
2118 ret = wimlib_set_image_descripton(wim,
2122 goto out_free_template_wim;
2125 if (flags_element) {
2126 ret = wimlib_set_image_flags(wim, info.image_count,
2129 goto out_free_template_wim;
2132 /* Reference template image if the user provided one. */
2133 if (template_image_name_or_num) {
2134 imagex_printf(T("Using image %d "
2135 "from \"%"TS"\" as template\n"),
2136 template_image, template_wimfile);
2137 ret = wimlib_reference_template_image(wim,
2143 goto out_free_template_wim;
2147 /* Write the new WIM or overwrite the existing WIM with the new image
2149 if (cmd == CMD_APPEND) {
2150 ret = wimlib_overwrite(wim, write_flags, num_threads,
2151 imagex_progress_func);
2152 } else if (wimfile) {
2153 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
2154 write_flags, num_threads,
2155 imagex_progress_func);
2157 ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
2158 write_flags, num_threads,
2159 imagex_progress_func);
2161 out_free_template_wim:
2162 /* template_wim may alias base_wims[0] or wim. */
2163 if ((base_wimfiles.num_strings != 1 || template_wim != base_wims[0]) &&
2164 template_wim != wim)
2165 wimlib_free(template_wim);
2167 for (size_t i = 0; i < base_wimfiles.num_strings; i++)
2168 wimlib_free(base_wims[i]);
2172 out_free_capture_sources:
2173 if (capture_sources_malloced)
2174 free(capture_sources);
2175 out_free_source_list_contents:
2176 free(source_list_contents);
2177 out_free_base_wimfiles:
2178 string_set_destroy(&base_wimfiles);
2185 goto out_free_base_wimfiles;
2188 /* Remove image(s) from a WIM. */
2190 imagex_delete(int argc, tchar **argv, int cmd)
2193 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2194 int write_flags = 0;
2195 const tchar *wimfile;
2196 const tchar *image_num_or_name;
2201 for_opt(c, delete_options) {
2203 case IMAGEX_CHECK_OPTION:
2204 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2205 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2207 case IMAGEX_SOFT_OPTION:
2208 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
2219 imagex_error(T("Must specify a WIM file"));
2221 imagex_error(T("Must specify an image"));
2225 image_num_or_name = argv[1];
2227 ret = wimlib_open_wim(wimfile, open_flags, &wim,
2228 imagex_progress_func);
2232 image = wimlib_resolve_image(wim, image_num_or_name);
2234 ret = verify_image_exists(image, image_num_or_name, wimfile);
2236 goto out_wimlib_free;
2238 ret = wimlib_delete_image(wim, image);
2240 imagex_error(T("Failed to delete image from \"%"TS"\""),
2242 goto out_wimlib_free;
2245 ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
2247 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2248 "deleted"), wimfile);
2256 usage(CMD_DELETE, stderr);
2261 struct print_dentry_options {
2266 print_dentry_full_path(const struct wimlib_dir_entry *dentry)
2268 tprintf(T("%"TS"\n"), dentry->full_path);
2271 static const struct {
2274 } file_attr_flags[] = {
2275 {WIMLIB_FILE_ATTRIBUTE_READONLY, T("READONLY")},
2276 {WIMLIB_FILE_ATTRIBUTE_HIDDEN, T("HIDDEN")},
2277 {WIMLIB_FILE_ATTRIBUTE_SYSTEM, T("SYSTEM")},
2278 {WIMLIB_FILE_ATTRIBUTE_DIRECTORY, T("DIRECTORY")},
2279 {WIMLIB_FILE_ATTRIBUTE_ARCHIVE, T("ARCHIVE")},
2280 {WIMLIB_FILE_ATTRIBUTE_DEVICE, T("DEVICE")},
2281 {WIMLIB_FILE_ATTRIBUTE_NORMAL, T("NORMAL")},
2282 {WIMLIB_FILE_ATTRIBUTE_TEMPORARY, T("TEMPORARY")},
2283 {WIMLIB_FILE_ATTRIBUTE_SPARSE_FILE, T("SPARSE_FILE")},
2284 {WIMLIB_FILE_ATTRIBUTE_REPARSE_POINT, T("REPARSE_POINT")},
2285 {WIMLIB_FILE_ATTRIBUTE_COMPRESSED, T("COMPRESSED")},
2286 {WIMLIB_FILE_ATTRIBUTE_OFFLINE, T("OFFLINE")},
2287 {WIMLIB_FILE_ATTRIBUTE_NOT_CONTENT_INDEXED, T("NOT_CONTENT_INDEXED")},
2288 {WIMLIB_FILE_ATTRIBUTE_ENCRYPTED, T("ENCRYPTED")},
2289 {WIMLIB_FILE_ATTRIBUTE_VIRTUAL, T("VIRTUAL")},
2292 #define TIMESTR_MAX 100
2295 timespec_to_string(const struct timespec *spec, tchar *buf)
2297 time_t t = spec->tv_sec;
2300 tstrftime(buf, TIMESTR_MAX, T("%a %b %d %H:%M:%S %Y UTC"), &tm);
2301 buf[TIMESTR_MAX - 1] = '\0';
2305 print_time(const tchar *type, const struct timespec *spec)
2307 tchar timestr[TIMESTR_MAX];
2309 timespec_to_string(spec, timestr);
2311 tprintf(T("%-20"TS"= %"TS"\n"), type, timestr);
2314 static void print_byte_field(const uint8_t field[], size_t len)
2317 tprintf(T("%02hhx"), *field++);
2321 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2323 tputs(T("WIM Information:"));
2324 tputs(T("----------------"));
2325 tprintf(T("Path: %"TS"\n"), wimfile);
2326 tprintf(T("GUID: 0x"));
2327 print_byte_field(info->guid, sizeof(info->guid));
2329 tprintf(T("Version: %u\n"), info->wim_version);
2330 tprintf(T("Image Count: %d\n"), info->image_count);
2331 tprintf(T("Compression: %"TS"\n"),
2332 wimlib_get_compression_type_string(info->compression_type));
2333 tprintf(T("Chunk Size: %"PRIu32" bytes\n"),
2335 tprintf(T("Part Number: %d/%d\n"), info->part_number, info->total_parts);
2336 tprintf(T("Boot Index: %d\n"), info->boot_index);
2337 tprintf(T("Size: %"PRIu64" bytes\n"), info->total_bytes);
2338 tprintf(T("Integrity Info: %"TS"\n"),
2339 info->has_integrity_table ? T("yes") : T("no"));
2340 tprintf(T("Relative path junction: %"TS"\n"),
2341 info->has_rpfix ? T("yes") : T("no"));
2342 tprintf(T("Pipable: %"TS"\n"),
2343 info->pipable ? T("yes") : T("no"));
2348 print_resource(const struct wimlib_resource_entry *resource,
2351 tprintf(T("Hash = 0x"));
2352 print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2355 if (!resource->is_missing) {
2356 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
2357 resource->uncompressed_size);
2358 if (resource->packed) {
2359 tprintf(T("Raw compressed size = %"PRIu64" bytes\n"),
2360 resource->raw_resource_compressed_size);
2362 tprintf(T("Raw offset in WIM = %"PRIu64" bytes\n"),
2363 resource->raw_resource_offset_in_wim);
2365 tprintf(T("Offset in raw = %"PRIu64" bytes\n"),
2368 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
2369 resource->compressed_size);
2371 tprintf(T("Offset in WIM = %"PRIu64" bytes\n"),
2375 tprintf(T("Part Number = %u\n"), resource->part_number);
2376 tprintf(T("Reference Count = %u\n"), resource->reference_count);
2378 tprintf(T("Flags = "));
2379 if (resource->is_compressed)
2380 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED "));
2381 if (resource->is_metadata)
2382 tprintf(T("WIM_RESHDR_FLAG_METADATA "));
2383 if (resource->is_free)
2384 tprintf(T("WIM_RESHDR_FLAG_FREE "));
2385 if (resource->is_spanned)
2386 tprintf(T("WIM_RESHDR_FLAG_SPANNED "));
2387 if (resource->packed)
2388 tprintf(T("WIM_RESHDR_FLAG_PACKED_STREAMS "));
2396 print_lookup_table(WIMStruct *wim)
2398 wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2402 default_print_security_descriptor(const uint8_t *sd, size_t size)
2404 tprintf(T("Security Descriptor = "));
2405 print_byte_field(sd, size);
2410 print_dentry_detailed(const struct wimlib_dir_entry *dentry)
2414 "----------------------------------------------------------------------------\n"));
2415 tprintf(T("Full Path = \"%"TS"\"\n"), dentry->full_path);
2416 if (dentry->dos_name)
2417 tprintf(T("Short Name = \"%"TS"\"\n"), dentry->dos_name);
2418 tprintf(T("Attributes = 0x%08x\n"), dentry->attributes);
2419 for (size_t i = 0; i < ARRAY_LEN(file_attr_flags); i++)
2420 if (file_attr_flags[i].flag & dentry->attributes)
2421 tprintf(T(" FILE_ATTRIBUTE_%"TS" is set\n"),
2422 file_attr_flags[i].name);
2424 if (dentry->security_descriptor) {
2425 print_security_descriptor(dentry->security_descriptor,
2426 dentry->security_descriptor_size);
2429 print_time(T("Creation Time"), &dentry->creation_time);
2430 print_time(T("Last Write Time"), &dentry->last_write_time);
2431 print_time(T("Last Access Time"), &dentry->last_access_time);
2434 if (dentry->attributes & WIMLIB_FILE_ATTRIBUTE_REPARSE_POINT)
2435 tprintf(T("Reparse Tag = 0x%"PRIx32"\n"), dentry->reparse_tag);
2437 tprintf(T("Link Group ID = 0x%016"PRIx64"\n"), dentry->hard_link_group_id);
2438 tprintf(T("Link Count = %"PRIu32"\n"), dentry->num_links);
2440 for (uint32_t i = 0; i <= dentry->num_named_streams; i++) {
2441 if (dentry->streams[i].stream_name) {
2442 tprintf(T("\tData stream \"%"TS"\":\n"),
2443 dentry->streams[i].stream_name);
2445 tprintf(T("\tUnnamed data stream:\n"));
2447 print_resource(&dentry->streams[i].resource, NULL);
2452 print_dentry(const struct wimlib_dir_entry *dentry, void *_options)
2454 const struct print_dentry_options *options = _options;
2455 if (!options->detailed)
2456 print_dentry_full_path(dentry);
2458 print_dentry_detailed(dentry);
2462 /* Print the files contained in an image(s) in a WIM file. */
2464 imagex_dir(int argc, tchar **argv, int cmd)
2466 const tchar *wimfile;
2467 WIMStruct *wim = NULL;
2470 const tchar *path = T("");
2472 struct print_dentry_options options = {
2475 int iterate_flags = WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE;
2477 for_opt(c, dir_options) {
2479 case IMAGEX_PATH_OPTION:
2482 case IMAGEX_DETAILED_OPTION:
2483 options.detailed = true;
2485 case IMAGEX_ONE_FILE_ONLY_OPTION:
2486 iterate_flags &= ~WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE;
2496 imagex_error(T("Must specify a WIM file"));
2500 imagex_error(T("Too many arguments"));
2505 ret = wimlib_open_wim(wimfile, 0, &wim, imagex_progress_func);
2510 image = wimlib_resolve_image(wim, argv[1]);
2511 ret = verify_image_exists(image, argv[1], wimfile);
2513 goto out_wimlib_free;
2515 /* No image specified; default to image 1, but only if the WIM
2516 * contains exactly one image. */
2518 struct wimlib_wim_info info;
2520 wimlib_get_wim_info(wim, &info);
2521 if (info.image_count != 1) {
2522 imagex_error(T("\"%"TS"\" contains %d images; Please "
2523 "select one (or all)."),
2524 wimfile, info.image_count);
2531 ret = wimlib_iterate_dir_tree(wim, image, path, iterate_flags,
2532 print_dentry, &options);
2539 usage(CMD_DIR, stderr);
2544 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2547 imagex_export(int argc, tchar **argv, int cmd)
2551 int export_flags = 0;
2552 int write_flags = 0;
2553 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2554 const tchar *src_wimfile;
2555 const tchar *src_image_num_or_name;
2556 const tchar *dest_wimfile;
2558 const tchar *dest_name;
2559 const tchar *dest_desc;
2561 struct wimlib_wim_info src_info;
2562 WIMStruct *dest_wim;
2567 STRING_SET(refglobs);
2568 unsigned num_threads = 0;
2569 uint32_t chunk_size = UINT32_MAX;
2570 uint32_t pack_chunk_size = UINT32_MAX;
2572 for_opt(c, export_options) {
2574 case IMAGEX_BOOT_OPTION:
2575 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2577 case IMAGEX_CHECK_OPTION:
2578 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2579 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2581 case IMAGEX_NOCHECK_OPTION:
2582 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2584 case IMAGEX_COMPRESS_OPTION:
2585 compression_type = get_compression_type(optarg);
2586 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2589 case IMAGEX_COMPRESS_SLOW_OPTION:
2590 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
2591 set_compress_slow();
2593 case IMAGEX_PACK_STREAMS_OPTION:
2594 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
2596 case IMAGEX_CHUNK_SIZE_OPTION:
2597 chunk_size = parse_chunk_size(optarg);
2598 if (chunk_size == UINT32_MAX)
2601 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
2602 pack_chunk_size = parse_chunk_size(optarg);
2603 if (pack_chunk_size == UINT32_MAX)
2606 case IMAGEX_REF_OPTION:
2607 ret = string_set_append(&refglobs, optarg);
2609 goto out_free_refglobs;
2611 case IMAGEX_THREADS_OPTION:
2612 num_threads = parse_num_threads(optarg);
2613 if (num_threads == UINT_MAX)
2616 case IMAGEX_REBUILD_OPTION:
2617 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2619 case IMAGEX_PIPABLE_OPTION:
2620 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2622 case IMAGEX_NOT_PIPABLE_OPTION:
2623 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
2631 if (argc < 3 || argc > 5)
2633 src_wimfile = argv[0];
2634 src_image_num_or_name = argv[1];
2635 dest_wimfile = argv[2];
2636 dest_name = (argc >= 4) ? argv[3] : NULL;
2637 dest_desc = (argc >= 5) ? argv[4] : NULL;
2638 ret = wimlib_open_wim(src_wimfile, open_flags, &src_wim,
2639 imagex_progress_func);
2641 goto out_free_refglobs;
2643 wimlib_get_wim_info(src_wim, &src_info);
2645 /* Determine if the destination is an existing file or not. If so, we
2646 * try to append the exported image(s) to it; otherwise, we create a new
2647 * WIM containing the exported image(s). Furthermore, determine if we
2648 * need to write a pipable WIM directly to standard output. */
2650 if (tstrcmp(dest_wimfile, T("-")) == 0) {
2652 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2653 imagex_error("Can't write a non-pipable WIM to "
2654 "standard output! Specify --pipable\n"
2655 " if you want to create a pipable WIM "
2656 "(but read the docs first).");
2658 goto out_free_src_wim;
2661 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2663 dest_wimfile = NULL;
2664 dest_wim_fd = STDOUT_FILENO;
2665 imagex_info_file = stderr;
2666 set_fd_to_binary_mode(dest_wim_fd);
2669 if (dest_wimfile != NULL && tstat(dest_wimfile, &stbuf) == 0) {
2671 /* Destination file exists. */
2673 if (!S_ISREG(stbuf.st_mode)) {
2674 imagex_error(T("\"%"TS"\" is not a regular file"),
2677 goto out_free_src_wim;
2679 ret = wimlib_open_wim(dest_wimfile,
2680 open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2681 &dest_wim, imagex_progress_func);
2683 goto out_free_src_wim;
2685 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2686 /* The user specified a compression type, but we're
2687 * exporting to an existing WIM. Make sure the
2688 * specified compression type is the same as the
2689 * compression type of the existing destination WIM. */
2690 struct wimlib_wim_info dest_info;
2692 wimlib_get_wim_info(dest_wim, &dest_info);
2693 if (compression_type != dest_info.compression_type) {
2694 imagex_error(T("Cannot specify a compression type that is "
2695 "not the same as that used in the "
2696 "destination WIM"));
2698 goto out_free_dest_wim;
2704 if (errno != ENOENT) {
2705 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2708 goto out_free_src_wim;
2711 /* dest_wimfile is not an existing file, so create a new WIM. */
2713 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2714 /* The user did not specify a compression type; default
2715 * to that of the source WIM. */
2717 compression_type = src_info.compression_type;
2719 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2721 goto out_free_src_wim;
2723 /* Use same chunk size if compression type is the same. */
2724 if (compression_type == src_info.compression_type &&
2725 chunk_size == UINT32_MAX)
2726 wimlib_set_output_chunk_size(dest_wim, src_info.chunk_size);
2729 if (chunk_size != UINT32_MAX) {
2730 /* Set destination chunk size. */
2731 ret = wimlib_set_output_chunk_size(dest_wim, chunk_size);
2733 goto out_free_dest_wim;
2735 if (pack_chunk_size != UINT32_MAX) {
2736 ret = wimlib_set_output_pack_chunk_size(dest_wim, pack_chunk_size);
2738 goto out_free_dest_wim;
2741 image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2742 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2744 goto out_free_dest_wim;
2746 if (refglobs.num_strings) {
2747 ret = wim_reference_globs(src_wim, &refglobs, open_flags);
2749 goto out_free_dest_wim;
2752 if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
2753 image == WIMLIB_ALL_IMAGES && src_info.boot_index == 0)
2755 imagex_error(T("--boot specified for all-images export, but source WIM "
2756 "has no bootable image."));
2758 goto out_free_dest_wim;
2761 ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2762 dest_desc, export_flags, imagex_progress_func);
2764 if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2765 do_resource_not_found_warning(src_wimfile,
2766 &src_info, &refglobs);
2768 goto out_free_dest_wim;
2772 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2773 imagex_progress_func);
2774 else if (dest_wimfile)
2775 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2776 write_flags, num_threads,
2777 imagex_progress_func);
2779 ret = wimlib_write_to_fd(dest_wim, dest_wim_fd,
2780 WIMLIB_ALL_IMAGES, write_flags,
2781 num_threads, imagex_progress_func);
2783 wimlib_free(dest_wim);
2785 wimlib_free(src_wim);
2787 string_set_destroy(&refglobs);
2791 usage(CMD_EXPORT, stderr);
2794 goto out_free_refglobs;
2797 /* Extract files or directories from a WIM image */
2799 imagex_extract(int argc, tchar **argv, int cmd)
2806 const tchar *wimfile;
2807 const tchar *image_num_or_name;
2808 tchar *dest_dir = T(".");
2809 int extract_flags = WIMLIB_EXTRACT_FLAG_NORPFIX |
2810 WIMLIB_EXTRACT_FLAG_GLOB_PATHS |
2811 WIMLIB_EXTRACT_FLAG_STRICT_GLOB;
2812 int notlist_extract_flags = WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE;
2814 STRING_SET(refglobs);
2816 tchar *root_path = T("");
2818 for_opt(c, extract_options) {
2820 case IMAGEX_CHECK_OPTION:
2821 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2823 case IMAGEX_VERBOSE_OPTION:
2824 /* No longer does anything. */
2826 case IMAGEX_REF_OPTION:
2827 ret = string_set_append(&refglobs, optarg);
2829 goto out_free_refglobs;
2831 case IMAGEX_UNIX_DATA_OPTION:
2832 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2834 case IMAGEX_NO_ACLS_OPTION:
2835 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2837 case IMAGEX_STRICT_ACLS_OPTION:
2838 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2840 case IMAGEX_NO_ATTRIBUTES_OPTION:
2841 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES;
2843 case IMAGEX_DEST_DIR_OPTION:
2846 case IMAGEX_TO_STDOUT_OPTION:
2847 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2848 imagex_info_file = stderr;
2849 imagex_be_quiet = true;
2851 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2852 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2853 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2855 case IMAGEX_NO_WILDCARDS_OPTION:
2856 extract_flags &= ~WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2858 case IMAGEX_NULLGLOB_OPTION:
2859 extract_flags &= ~WIMLIB_EXTRACT_FLAG_STRICT_GLOB;
2861 case IMAGEX_PRESERVE_DIR_STRUCTURE_OPTION:
2862 notlist_extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE;
2864 case IMAGEX_WIMBOOT_OPTION:
2865 extract_flags |= WIMLIB_EXTRACT_FLAG_WIMBOOT;
2877 if (!(extract_flags & (WIMLIB_EXTRACT_FLAG_GLOB_PATHS |
2878 WIMLIB_EXTRACT_FLAG_STRICT_GLOB)))
2880 imagex_error(T("Can't combine --no-wildcards and --nullglob!"));
2885 image_num_or_name = argv[1];
2890 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2892 goto out_free_refglobs;
2894 image = wimlib_resolve_image(wim, image_num_or_name);
2895 ret = verify_image_exists_and_is_single(image,
2899 goto out_wimlib_free;
2901 if (refglobs.num_strings) {
2902 ret = wim_reference_globs(wim, &refglobs, open_flags);
2904 goto out_wimlib_free;
2910 extract_flags &= ~WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2913 while (argc != 0 && ret == 0) {
2917 num_paths < argc && argv[num_paths][0] != T('@');
2922 ret = wimlib_extract_paths(wim, image, dest_dir,
2923 (const tchar **)argv,
2925 extract_flags | notlist_extract_flags,
2926 imagex_progress_func);
2930 ret = wimlib_extract_pathlist(wim, image, dest_dir,
2933 imagex_progress_func);
2940 if (!imagex_be_quiet)
2941 imagex_printf(T("Done extracting files.\n"));
2942 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2943 tfprintf(stderr, T("Note: You can use `%"TS"' to see what "
2944 "files and directories\n"
2945 " are in the WIM image.\n"),
2946 get_cmd_string(CMD_DIR, false));
2947 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2948 struct wimlib_wim_info info;
2950 wimlib_get_wim_info(wim, &info);
2951 do_resource_not_found_warning(wimfile, &info, &refglobs);
2956 string_set_destroy(&refglobs);
2960 usage(CMD_EXTRACT, stderr);
2963 goto out_free_refglobs;
2966 /* Prints information about a WIM file; also can mark an image as bootable,
2967 * change the name of an image, or change the description of an image. */
2969 imagex_info(int argc, tchar **argv, int cmd)
2974 bool nocheck = false;
2975 bool header = false;
2976 bool lookup_table = false;
2978 bool short_header = true;
2979 const tchar *xml_out_file = NULL;
2980 const tchar *wimfile;
2981 const tchar *image_num_or_name;
2982 const tchar *new_name;
2983 const tchar *new_desc;
2988 struct wimlib_wim_info info;
2990 for_opt(c, info_options) {
2992 case IMAGEX_BOOT_OPTION:
2995 case IMAGEX_CHECK_OPTION:
2998 case IMAGEX_NOCHECK_OPTION:
3001 case IMAGEX_HEADER_OPTION:
3003 short_header = false;
3005 case IMAGEX_LOOKUP_TABLE_OPTION:
3006 lookup_table = true;
3007 short_header = false;
3009 case IMAGEX_XML_OPTION:
3011 short_header = false;
3013 case IMAGEX_EXTRACT_XML_OPTION:
3014 xml_out_file = optarg;
3015 short_header = false;
3017 case IMAGEX_METADATA_OPTION:
3018 imagex_error(T("The --metadata option has been removed. "
3019 "Use 'wimdir --detail' instead."));
3028 if (argc < 1 || argc > 4)
3032 image_num_or_name = (argc >= 2) ? argv[1] : T("all");
3033 new_name = (argc >= 3) ? argv[2] : NULL;
3034 new_desc = (argc >= 4) ? argv[3] : NULL;
3036 if (check && nocheck) {
3037 imagex_error(T("Can't specify both --check and --nocheck"));
3042 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3044 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3048 wimlib_get_wim_info(wim, &info);
3050 image = wimlib_resolve_image(wim, image_num_or_name);
3051 ret = WIMLIB_ERR_INVALID_IMAGE;
3052 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
3053 verify_image_exists(image, image_num_or_name, wimfile);
3055 imagex_error(T("If you would like to set the boot "
3056 "index to 0, specify image \"0\" with "
3057 "the --boot flag."));
3059 goto out_wimlib_free;
3062 if (boot && info.image_count == 0) {
3063 imagex_error(T("--boot is meaningless on a WIM with no images"));
3064 goto out_wimlib_free;
3067 if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
3069 imagex_error(T("Cannot specify the --boot flag "
3070 "without specifying a specific "
3071 "image in a multi-image WIM"));
3072 goto out_wimlib_free;
3075 imagex_error(T("Cannot specify the NEW_NAME "
3076 "without specifying a specific "
3077 "image in a multi-image WIM"));
3078 goto out_wimlib_free;
3082 /* Operations that print information are separated from operations that
3083 * recreate the WIM file. */
3084 if (!new_name && !boot) {
3086 /* Read-only operations */
3088 if (image == WIMLIB_NO_IMAGE) {
3089 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
3090 image_num_or_name, wimfile);
3091 goto out_wimlib_free;
3094 if (image == WIMLIB_ALL_IMAGES && short_header)
3095 print_wim_information(wimfile, &info);
3098 wimlib_print_header(wim);
3101 if (info.total_parts != 1) {
3102 tfprintf(stderr, T("Warning: Only showing the lookup table "
3103 "for part %d of a %d-part WIM.\n"),
3104 info.part_number, info.total_parts);
3106 print_lookup_table(wim);
3110 ret = wimlib_extract_xml_data(wim, stdout);
3112 goto out_wimlib_free;
3118 fp = tfopen(xml_out_file, T("wb"));
3120 imagex_error_with_errno(T("Failed to open the "
3121 "file \"%"TS"\" for "
3125 goto out_wimlib_free;
3127 ret = wimlib_extract_xml_data(wim, fp);
3129 imagex_error(T("Failed to close the file "
3135 goto out_wimlib_free;
3139 wimlib_print_available_images(wim, image);
3144 /* Modification operations */
3146 if (image == WIMLIB_ALL_IMAGES)
3149 if (image == WIMLIB_NO_IMAGE && new_name) {
3150 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
3151 "when using image 0"), new_name);
3153 goto out_wimlib_free;
3157 if (image == info.boot_index) {
3158 imagex_printf(T("Image %d is already marked as "
3159 "bootable.\n"), image);
3162 imagex_printf(T("Marking image %d as bootable.\n"),
3164 info.boot_index = image;
3165 ret = wimlib_set_wim_info(wim, &info,
3166 WIMLIB_CHANGE_BOOT_INDEX);
3168 goto out_wimlib_free;
3172 if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
3174 imagex_printf(T("Image %d is already named \"%"TS"\".\n"),
3178 imagex_printf(T("Changing the name of image %d to "
3179 "\"%"TS"\".\n"), image, new_name);
3180 ret = wimlib_set_image_name(wim, image, new_name);
3182 goto out_wimlib_free;
3186 const tchar *old_desc;
3187 old_desc = wimlib_get_image_description(wim, image);
3188 if (old_desc && !tstrcmp(old_desc, new_desc)) {
3189 imagex_printf(T("The description of image %d is already "
3190 "\"%"TS"\".\n"), image, new_desc);
3193 imagex_printf(T("Changing the description of image %d "
3194 "to \"%"TS"\".\n"), image, new_desc);
3195 ret = wimlib_set_image_descripton(wim, image,
3198 goto out_wimlib_free;
3202 /* Only call wimlib_overwrite() if something actually needs to
3204 if (boot || new_name || new_desc ||
3205 (check && !info.has_integrity_table) ||
3206 (nocheck && info.has_integrity_table))
3208 int write_flags = 0;
3211 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3213 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3214 ret = wimlib_overwrite(wim, write_flags, 1,
3215 imagex_progress_func);
3217 imagex_printf(T("The file \"%"TS"\" was not modified "
3218 "because nothing needed to be done.\n"),
3229 usage(CMD_INFO, stderr);
3235 /* Join split WIMs into one part WIM */
3237 imagex_join(int argc, tchar **argv, int cmd)
3240 int swm_open_flags = 0;
3241 int wim_write_flags = 0;
3242 const tchar *output_path;
3245 for_opt(c, join_options) {
3247 case IMAGEX_CHECK_OPTION:
3248 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3249 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3259 imagex_error(T("Must specify one or more split WIM (.swm) "
3263 output_path = argv[0];
3264 ret = wimlib_join((const tchar * const *)++argv,
3269 imagex_progress_func);
3274 usage(CMD_JOIN, stderr);
3279 #if WIM_MOUNTING_SUPPORTED
3281 /* Mounts a WIM image. */
3283 imagex_mount_rw_or_ro(int argc, tchar **argv, int cmd)
3286 int mount_flags = 0;
3288 const tchar *staging_dir = NULL;
3289 const tchar *wimfile;
3292 struct wimlib_wim_info info;
3296 STRING_SET(refglobs);
3298 if (cmd == CMD_MOUNTRW) {
3299 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
3300 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3303 for_opt(c, mount_options) {
3305 case IMAGEX_ALLOW_OTHER_OPTION:
3306 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
3308 case IMAGEX_CHECK_OPTION:
3309 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3311 case IMAGEX_DEBUG_OPTION:
3312 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
3314 case IMAGEX_STREAMS_INTERFACE_OPTION:
3315 if (!tstrcasecmp(optarg, T("none")))
3316 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
3317 else if (!tstrcasecmp(optarg, T("xattr")))
3318 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
3319 else if (!tstrcasecmp(optarg, T("windows")))
3320 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
3322 imagex_error(T("Unknown stream interface \"%"TS"\""),
3327 case IMAGEX_REF_OPTION:
3328 ret = string_set_append(&refglobs, optarg);
3330 goto out_free_refglobs;
3332 case IMAGEX_STAGING_DIR_OPTION:
3333 staging_dir = optarg;
3335 case IMAGEX_UNIX_DATA_OPTION:
3336 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
3344 if (argc != 2 && argc != 3)
3349 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3351 goto out_free_refglobs;
3353 wimlib_get_wim_info(wim, &info);
3356 /* Image explicitly specified. */
3357 image = wimlib_resolve_image(wim, argv[1]);
3359 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
3363 /* No image specified; default to image 1, but only if the WIM
3364 * contains exactly one image. */
3366 if (info.image_count != 1) {
3367 imagex_error(T("\"%"TS"\" contains %d images; Please "
3368 "select one."), wimfile, info.image_count);
3376 if (refglobs.num_strings) {
3377 ret = wim_reference_globs(wim, &refglobs, open_flags);
3382 ret = wimlib_mount_image(wim, image, dir, mount_flags, staging_dir);
3384 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
3386 image, wimfile, dir);
3391 string_set_destroy(&refglobs);
3397 goto out_free_refglobs;
3399 #endif /* WIM_MOUNTING_SUPPORTED */
3401 /* Rebuild a WIM file */
3403 imagex_optimize(int argc, tchar **argv, int cmd)
3406 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3407 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
3408 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
3409 uint32_t chunk_size = UINT32_MAX;
3410 uint32_t pack_chunk_size = UINT32_MAX;
3413 const tchar *wimfile;
3416 unsigned num_threads = 0;
3418 for_opt(c, optimize_options) {
3420 case IMAGEX_CHECK_OPTION:
3421 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3422 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3424 case IMAGEX_NOCHECK_OPTION:
3425 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3427 case IMAGEX_COMPRESS_OPTION:
3428 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3429 compression_type = get_compression_type(optarg);
3430 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
3433 case IMAGEX_RECOMPRESS_OPTION:
3434 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3436 case IMAGEX_COMPRESS_SLOW_OPTION:
3437 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3438 set_compress_slow();
3440 case IMAGEX_CHUNK_SIZE_OPTION:
3441 chunk_size = parse_chunk_size(optarg);
3442 if (chunk_size == UINT32_MAX)
3445 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
3446 pack_chunk_size = parse_chunk_size(optarg);
3447 if (pack_chunk_size == UINT32_MAX)
3450 case IMAGEX_PACK_STREAMS_OPTION:
3451 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
3452 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3454 case IMAGEX_THREADS_OPTION:
3455 num_threads = parse_num_threads(optarg);
3456 if (num_threads == UINT_MAX)
3459 case IMAGEX_PIPABLE_OPTION:
3460 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
3462 case IMAGEX_NOT_PIPABLE_OPTION:
3463 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
3477 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3481 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
3482 /* Change compression type. */
3483 ret = wimlib_set_output_compression_type(wim, compression_type);
3485 goto out_wimlib_free;
3488 if (chunk_size != UINT32_MAX) {
3489 /* Change chunk size. */
3490 ret = wimlib_set_output_chunk_size(wim, chunk_size);
3492 goto out_wimlib_free;
3494 if (pack_chunk_size != UINT32_MAX) {
3495 ret = wimlib_set_output_pack_chunk_size(wim, pack_chunk_size);
3497 goto out_wimlib_free;
3500 old_size = file_get_size(wimfile);
3501 tprintf(T("\"%"TS"\" original size: "), wimfile);
3503 tputs(T("Unknown"));
3505 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3507 ret = wimlib_overwrite(wim, write_flags, num_threads,
3508 imagex_progress_func);
3510 imagex_error(T("Optimization of \"%"TS"\" failed."), wimfile);
3511 goto out_wimlib_free;
3514 new_size = file_get_size(wimfile);
3515 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3517 tputs(T("Unknown"));
3519 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3521 tfputs(T("Space saved: "), stdout);
3522 if (new_size != -1 && old_size != -1) {
3523 tprintf(T("%lld KiB\n"),
3524 ((long long)old_size - (long long)new_size) >> 10);
3526 tputs(T("Unknown"));
3535 usage(CMD_OPTIMIZE, stderr);
3541 /* Split a WIM into a spanned set */
3543 imagex_split(int argc, tchar **argv, int cmd)
3547 int write_flags = 0;
3548 unsigned long part_size;
3553 for_opt(c, split_options) {
3555 case IMAGEX_CHECK_OPTION:
3556 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3557 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3569 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3570 if (tmp == argv[2] || *tmp) {
3571 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3572 imagex_error(T("The part size must be an integer or "
3573 "floating-point number of megabytes."));
3576 ret = wimlib_open_wim(argv[0], open_flags, &wim, imagex_progress_func);
3580 ret = wimlib_split(wim, argv[1], part_size, write_flags, imagex_progress_func);
3586 usage(CMD_SPLIT, stderr);
3592 #if WIM_MOUNTING_SUPPORTED
3593 /* Unmounts a mounted WIM image. */
3595 imagex_unmount(int argc, tchar **argv, int cmd)
3598 int unmount_flags = 0;
3601 for_opt(c, unmount_options) {
3603 case IMAGEX_COMMIT_OPTION:
3604 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3606 case IMAGEX_CHECK_OPTION:
3607 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3609 case IMAGEX_REBUILD_OPTION:
3610 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3612 case IMAGEX_LAZY_OPTION:
3613 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3615 case IMAGEX_NEW_IMAGE_OPTION:
3616 unmount_flags |= WIMLIB_UNMOUNT_FLAG_NEW_IMAGE;
3627 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_NEW_IMAGE) {
3628 if (!(unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT)) {
3629 imagex_error(T("--new-image is meaningless "
3630 "without --commit also specified!"));
3633 imagex_printf(T("Committing changes as new image...\n"));
3636 ret = wimlib_unmount_image(argv[0], unmount_flags,
3637 imagex_progress_func);
3639 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3644 usage(CMD_UNMOUNT, stderr);
3649 #endif /* WIM_MOUNTING_SUPPORTED */
3652 * Add, delete, or rename files in a WIM image.
3655 imagex_update(int argc, tchar **argv, int cmd)
3657 const tchar *wimfile;
3661 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3662 int write_flags = 0;
3663 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3664 int default_add_flags = WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
3665 WIMLIB_ADD_FLAG_VERBOSE |
3666 WIMLIB_ADD_FLAG_WINCONFIG;
3667 int default_delete_flags = 0;
3668 unsigned num_threads = 0;
3670 tchar *cmd_file_contents;
3671 size_t cmd_file_nchars;
3672 struct wimlib_update_command *cmds;
3674 tchar *command_str = NULL;
3675 tchar *config_file = NULL;
3677 for_opt(c, update_options) {
3679 /* Generic or write options */
3680 case IMAGEX_THREADS_OPTION:
3681 num_threads = parse_num_threads(optarg);
3682 if (num_threads == UINT_MAX)
3685 case IMAGEX_CHECK_OPTION:
3686 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3687 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3689 case IMAGEX_REBUILD_OPTION:
3690 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3692 case IMAGEX_COMMAND_OPTION:
3694 imagex_error(T("--command may only be specified "
3695 "one time. Please provide\n"
3696 " the update commands "
3697 "on standard input instead."));
3700 command_str = tstrdup(optarg);
3702 imagex_error(T("Out of memory!"));
3706 /* Default delete options */
3707 case IMAGEX_FORCE_OPTION:
3708 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3710 case IMAGEX_RECURSIVE_OPTION:
3711 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3714 /* Global add option */
3715 case IMAGEX_CONFIG_OPTION:
3716 default_add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
3717 config_file = optarg;
3720 /* Default add options */
3721 case IMAGEX_VERBOSE_OPTION:
3722 /* No longer does anything. */
3724 case IMAGEX_DEREFERENCE_OPTION:
3725 default_add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
3727 case IMAGEX_UNIX_DATA_OPTION:
3728 default_add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
3730 case IMAGEX_NO_ACLS_OPTION:
3731 default_add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
3733 case IMAGEX_STRICT_ACLS_OPTION:
3734 default_add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
3743 if (argc != 1 && argc != 2)
3747 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3749 goto out_free_command_str;
3752 /* Image explicitly specified. */
3753 image = wimlib_resolve_image(wim, argv[1]);
3754 ret = verify_image_exists_and_is_single(image, argv[1],
3757 goto out_wimlib_free;
3759 /* No image specified; default to image 1, but only if the WIM
3760 * contains exactly one image. */
3761 struct wimlib_wim_info info;
3763 wimlib_get_wim_info(wim, &info);
3764 if (info.image_count != 1) {
3765 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3766 wimfile, info.image_count);
3773 /* Read update commands from standard input, or the command string if
3776 cmd_file_contents = NULL;
3777 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3780 if (isatty(STDIN_FILENO)) {
3781 tputs(T("Reading update commands from standard input..."));
3782 recommend_man_page(CMD_UPDATE, stdout);
3784 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3785 if (!cmd_file_contents) {
3787 goto out_wimlib_free;
3790 /* Parse the update commands */
3791 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3796 goto out_free_cmd_file_contents;
3799 /* Set default flags and capture config on the update commands */
3800 for (size_t i = 0; i < num_cmds; i++) {
3801 switch (cmds[i].op) {
3802 case WIMLIB_UPDATE_OP_ADD:
3803 cmds[i].add.add_flags |= default_add_flags;
3804 cmds[i].add.config_file = config_file;
3806 case WIMLIB_UPDATE_OP_DELETE:
3807 cmds[i].delete_.delete_flags |= default_delete_flags;
3814 /* Execute the update commands */
3815 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3816 imagex_progress_func);
3820 /* Overwrite the updated WIM */
3821 ret = wimlib_overwrite(wim, write_flags, num_threads,
3822 imagex_progress_func);
3825 out_free_cmd_file_contents:
3826 free(cmd_file_contents);
3829 out_free_command_str:
3834 usage(CMD_UPDATE, stderr);
3837 goto out_free_command_str;
3842 struct imagex_command {
3844 int (*func)(int argc, tchar **argv, int cmd);
3847 static const struct imagex_command imagex_commands[] = {
3848 [CMD_APPEND] = {T("append"), imagex_capture_or_append},
3849 [CMD_APPLY] = {T("apply"), imagex_apply},
3850 [CMD_CAPTURE] = {T("capture"), imagex_capture_or_append},
3851 [CMD_DELETE] = {T("delete"), imagex_delete},
3852 [CMD_DIR ] = {T("dir"), imagex_dir},
3853 [CMD_EXPORT] = {T("export"), imagex_export},
3854 [CMD_EXTRACT] = {T("extract"), imagex_extract},
3855 [CMD_INFO] = {T("info"), imagex_info},
3856 [CMD_JOIN] = {T("join"), imagex_join},
3857 #if WIM_MOUNTING_SUPPORTED
3858 [CMD_MOUNT] = {T("mount"), imagex_mount_rw_or_ro},
3859 [CMD_MOUNTRW] = {T("mountrw"), imagex_mount_rw_or_ro},
3861 [CMD_OPTIMIZE] = {T("optimize"), imagex_optimize},
3862 [CMD_SPLIT] = {T("split"), imagex_split},
3863 #if WIM_MOUNTING_SUPPORTED
3864 [CMD_UNMOUNT] = {T("unmount"), imagex_unmount},
3866 [CMD_UPDATE] = {T("update"), imagex_update},
3869 static const tchar *usage_strings[] = {
3872 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3873 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3874 " [--nocheck] [--flags EDITION_ID] [--dereference]\n"
3875 " [--config=FILE] [--threads=NUM_THREADS] [--source-list]\n"
3876 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3877 " [--update-of=[WIMFILE:]IMAGE]\n"
3881 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME | all)]\n"
3882 " (DIRECTORY | NTFS_VOLUME) [--check] [--ref=\"GLOB\"]\n"
3883 " [--no-acls] [--strict-acls] [--no-attributes]\n"
3884 " [--rpfix] [--norpfix] [--hardlink] [--symlink]\n"
3885 " [--include-invalid-names]\n"
3889 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3890 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3891 " [--nocheck] [--compress=TYPE] [--flags EDITION_ID]\n"
3892 " [--dereference] [--config=FILE] [--threads=NUM_THREADS]\n"
3893 " [--source-list] [--no-acls] [--strict-acls] [--rpfix]\n"
3894 " [--norpfix] [--update-of=[WIMFILE:]IMAGE]\n"
3895 " [--delta-from=WIMFILE]\n"
3899 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n"
3900 " [--check] [--soft]\n"
3904 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH] [--detailed]\n"
3908 " %"TS" SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
3909 " DEST_WIMFILE [DEST_IMAGE_NAME [DEST_IMAGE_DESCRIPTION]]\n"
3910 " [--boot] [--check] [--nocheck] [--compress=TYPE]\n"
3911 " [--ref=\"GLOB\"] [--threads=NUM_THREADS] [--rebuild]\n"
3915 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME) [(PATH | @LISTFILE)...]\n"
3916 " [--check] [--ref=\"GLOB\"] [--dest-dir=CMD_DIR]\n"
3917 " [--to-stdout] [--no-acls] [--strict-acls]\n"
3918 " [--no-attributes] [--include-invalid-names]\n"
3919 " [--no-wildcards] [--nullglob] [--preserve-dir-structure]\n"
3923 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME) [NEW_NAME\n"
3924 " [NEW_DESC]]] [--boot] [--check] [--nocheck] [--xml]\n"
3925 " [--extract-xml FILE] [--header] [--lookup-table]\n"
3929 " %"TS" OUT_WIMFILE SPLIT_WIM_PART... [--check]\n"
3931 #if WIM_MOUNTING_SUPPORTED
3934 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3935 " [--check] [--streams-interface=INTERFACE]\n"
3936 " [--ref=\"GLOB\"] [--allow-other]\n"
3940 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3941 " [--check] [--streams-interface=INTERFACE]\n"
3942 " [--staging-dir=CMD_DIR] [--allow-other]\n"
3947 " %"TS" WIMFILE [--check] [--nocheck] [--recompress]\n"
3948 " [--recompress-slow] [--compress=TYPE]\n"
3949 " [--threads=NUM_THREADS]\n"
3953 " %"TS" WIMFILE SPLIT_WIM_PART_1 PART_SIZE_MB [--check]\n"
3955 #if WIM_MOUNTING_SUPPORTED
3958 " %"TS" DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
3964 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
3965 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
3966 " [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
3970 static const tchar *invocation_name;
3971 static int invocation_cmd = CMD_NONE;
3973 static const tchar *get_cmd_string(int cmd, bool nospace)
3975 static tchar buf[50];
3976 if (cmd == CMD_NONE) {
3977 tsprintf(buf, T("%"TS), T(IMAGEX_PROGNAME));
3978 } else if (invocation_cmd != CMD_NONE) {
3979 tsprintf(buf, T("wim%"TS), imagex_commands[cmd].name);
3981 const tchar *format;
3984 format = T("%"TS"-%"TS"");
3986 format = T("%"TS" %"TS"");
3987 tsprintf(buf, format, invocation_name, imagex_commands[cmd].name);
3995 static const tchar *s =
3997 IMAGEX_PROGNAME " (distributed with " PACKAGE " " PACKAGE_VERSION ")\n"
3998 "Copyright (C) 2012, 2013, 2014 Eric Biggers\n"
3999 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
4000 "This is free software: you are free to change and redistribute it.\n"
4001 "There is NO WARRANTY, to the extent permitted by law.\n"
4003 "Report bugs to "PACKAGE_BUGREPORT".\n"
4010 help_or_version(int argc, tchar **argv, int cmd)
4015 for (i = 1; i < argc; i++) {
4017 if (p[0] == T('-') && p[1] == T('-')) {
4019 if (!tstrcmp(p, T("help"))) {
4020 if (cmd == CMD_NONE)
4025 } else if (!tstrcmp(p, T("version"))) {
4034 print_usage_string(int cmd, FILE *fp)
4036 tfprintf(fp, usage_strings[cmd], get_cmd_string(cmd, false));
4040 recommend_man_page(int cmd, FILE *fp)
4042 const tchar *format_str;
4044 format_str = T("Uncommon options are not listed;\n"
4045 "See %"TS".pdf in the doc directory for more details.\n");
4047 format_str = T("Uncommon options are not listed;\n"
4048 "Try `man %"TS"' for more details.\n");
4050 tfprintf(fp, format_str, get_cmd_string(cmd, true));
4054 usage(int cmd, FILE *fp)
4056 tfprintf(fp, T("Usage:\n"));
4057 print_usage_string(cmd, fp);
4058 tfprintf(fp, T("\n"));
4059 recommend_man_page(cmd, fp);
4065 tfprintf(fp, T("Usage:\n"));
4066 for (int cmd = 0; cmd < CMD_MAX; cmd++) {
4067 print_usage_string(cmd, fp);
4068 tfprintf(fp, T("\n"));
4070 static const tchar *extra =
4073 " %"TS" --version\n"
4075 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
4078 tfprintf(fp, extra, invocation_name, invocation_name);
4079 recommend_man_page(CMD_NONE, fp);
4082 /* Entry point for wimlib's ImageX implementation. On UNIX the command
4083 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
4084 * something else), while an Windows the command arguments will be UTF-16LE
4085 * encoded 'wchar_t' strings. */
4088 wmain(int argc, wchar_t **argv, wchar_t **envp)
4090 main(int argc, char **argv)
4097 imagex_info_file = stdout;
4098 invocation_name = tbasename(argv[0]);
4101 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
4102 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
4106 setlocale(LC_ALL, "");
4107 codeset = nl_langinfo(CODESET);
4108 if (!strstr(codeset, "UTF-8") &&
4109 !strstr(codeset, "UTF8") &&
4110 !strstr(codeset, "utf-8") &&
4111 !strstr(codeset, "utf8"))
4114 "WARNING: Running %"TS" in a UTF-8 locale is recommended!\n"
4115 " Maybe try: `export LANG=en_US.UTF-8'?\n"
4116 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
4117 " to any value to force wimlib to use UTF-8.\n",
4123 #endif /* !__WIN32__ */
4126 tchar *igcase = tgetenv(T("WIMLIB_IMAGEX_IGNORE_CASE"));
4127 if (igcase != NULL) {
4128 if (!tstrcmp(igcase, T("no")) ||
4129 !tstrcmp(igcase, T("0")))
4130 init_flags |= WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE;
4131 else if (!tstrcmp(igcase, T("yes")) ||
4132 !tstrcmp(igcase, T("1")))
4133 init_flags |= WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE;
4136 "WARNING: Ignoring unknown setting of "
4137 "WIMLIB_IMAGEX_IGNORE_CASE\n");
4142 /* Allow being invoked as wimCOMMAND (e.g. wimapply). */
4144 if (!tstrncmp(invocation_name, T("wim"), 3) &&
4145 tstrcmp(invocation_name, T(IMAGEX_PROGNAME))) {
4146 for (int i = 0; i < CMD_MAX; i++) {
4147 if (!tstrcmp(invocation_name + 3,
4148 imagex_commands[i].name))
4157 /* Unless already known from the invocation name, determine which
4158 * command was specified. */
4159 if (cmd == CMD_NONE) {
4161 imagex_error(T("No command specified!\n"));
4165 for (int i = 0; i < CMD_MAX; i++) {
4166 if (!tstrcmp(argv[1], imagex_commands[i].name)) {
4171 if (cmd != CMD_NONE) {
4177 /* Handle --help and --version. --help can be either for the program as
4178 * a whole (cmd == CMD_NONE) or just for a specific command (cmd !=
4179 * CMD_NONE). Note: help_or_version() will not return if a --help or
4180 * --version argument was found. */
4181 help_or_version(argc, argv, cmd);
4183 /* Bail if a valid command was not specified. */
4184 if (cmd == CMD_NONE) {
4185 imagex_error(T("Unrecognized command: `%"TS"'\n"), argv[1]);
4190 /* Enable warning and error messages in wimlib be more user-friendly.
4192 wimlib_set_print_errors(true);
4194 /* Initialize wimlib. */
4195 ret = wimlib_global_init(init_flags);
4197 goto out_check_status;
4199 /* Call the command handler function. */
4200 ret = imagex_commands[cmd].func(argc, argv, cmd);
4202 /* Check for error writing to standard output, especially since for some
4203 * commands, writing to standard output is part of the program's actual
4204 * behavior and not just for informational purposes. */
4205 if (ferror(stdout) || fclose(stdout)) {
4206 imagex_error_with_errno(T("error writing to standard output"));
4211 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
4212 * itself (not by wimlib). 0 indicates success. > 0 indicates a wimlib
4213 * error code from which an error message can be printed. */
4215 imagex_error(T("Exiting with error code %d:\n"
4217 wimlib_get_error_string(ret));
4218 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
4219 imagex_error_with_errno(T("errno"));
4221 /* Make wimlib free any resources it's holding (although this is not
4222 * strictly necessary because the process is ending anyway). */
4223 wimlib_global_cleanup();