4 * Use wimlib to create, modify, extract, mount, unmount, or display information
9 * Copyright (C) 2012, 2013 Eric Biggers
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 # include "config.h" /* Need for PACKAGE_VERSION, etc. */
30 #include "wimlib_tchar.h"
50 # include "imagex-win32.h"
51 # define tbasename win32_wbasename
52 # define tglob win32_wglob
56 # include <langinfo.h>
57 # define tbasename basename
62 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
64 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
85 static void usage(int cmd_type);
86 static void usage_all(void);
88 static bool imagex_be_quiet = false;
91 static const tchar *usage_strings[] = {
94 IMAGEX_PROGNAME" append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
95 " [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
96 " [--verbose] [--dereference] [--config=FILE]\n"
97 " [--threads=NUM_THREADS] [--rebuild] [--unix-data]\n"
98 " [--source-list] [--no-acls] [--strict-acls]\n"
99 " [--rpfix] [--norpfix]\n"
103 IMAGEX_PROGNAME" apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
104 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
105 " [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n"
106 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
107 " [--include-invalid-names]\n"
111 IMAGEX_PROGNAME" capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
112 " [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
113 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
114 " [--config=FILE] [--threads=NUM_THREADS] [--unix-data]\n"
115 " [--source-list] [--no-acls] [--strict-acls]\n"
116 " [--rpfix] [--norpfix]\n"
120 IMAGEX_PROGNAME" delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check] [--soft]\n"
124 IMAGEX_PROGNAME" dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n"
128 IMAGEX_PROGNAME" export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
129 " DEST_WIMFILE [DEST_IMAGE_NAME] [DEST_IMAGE_DESCRIPTION]\n"
130 " [--boot] [--check] [--compress=TYPE] [--ref=\"GLOB\"]\n"
131 " [--threads=NUM_THREADS] [--rebuild]\n"
135 IMAGEX_PROGNAME" extract WIMFILE (IMAGE_NUM | IMAGE_NAME) [PATH...]\n"
136 " [--check] [--ref=\"GLOB\"] [--verbose] [--unix-data]\n"
137 " [--no-acls] [--strict-acls] [--to-stdout] [--dest-dir=DIR]\n"
138 " [--include-invalid-names]\n"
142 IMAGEX_PROGNAME" info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
143 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
144 " [--xml] [--extract-xml FILE] [--metadata]\n"
148 IMAGEX_PROGNAME" join [--check] WIMFILE SPLIT_WIM...\n"
152 IMAGEX_PROGNAME" mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
153 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
154 " [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n"
158 IMAGEX_PROGNAME" mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
159 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
160 " [--staging-dir=DIR] [--unix-data] [--allow-other]\n"
164 IMAGEX_PROGNAME" optimize WIMFILE [--check] [--recompress]\n"
165 " [--threads=NUM_THREADS]\n"
169 IMAGEX_PROGNAME" split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n"
173 IMAGEX_PROGNAME" unmount DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
177 IMAGEX_PROGNAME" update WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
178 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
179 " [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
185 recommend_man_page(const tchar *cmd_name)
187 const tchar *format_str;
189 format_str = T("See "IMAGEX_PROGNAME"%"TS"%"TS".pdf in the "
190 "doc directory for more details.\n");
192 format_str = T("Try `man "IMAGEX_PROGNAME"%"TS"%"TS"' "
193 "for more details.\n");
195 tprintf(format_str, *cmd_name ? T("-") : T(""), cmd_name);
199 IMAGEX_ALLOW_OTHER_OPTION,
202 IMAGEX_COMMAND_OPTION,
203 IMAGEX_COMMIT_OPTION,
204 IMAGEX_COMPRESS_OPTION,
205 IMAGEX_CONFIG_OPTION,
207 IMAGEX_DEREFERENCE_OPTION,
208 IMAGEX_DEST_DIR_OPTION,
209 IMAGEX_EXTRACT_XML_OPTION,
212 IMAGEX_HARDLINK_OPTION,
213 IMAGEX_HEADER_OPTION,
214 IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
216 IMAGEX_LOOKUP_TABLE_OPTION,
217 IMAGEX_METADATA_OPTION,
218 IMAGEX_NORPFIX_OPTION,
219 IMAGEX_NO_ACLS_OPTION,
220 IMAGEX_REBUILD_OPTION,
221 IMAGEX_RECOMPRESS_OPTION,
222 IMAGEX_RECURSIVE_OPTION,
226 IMAGEX_SOURCE_LIST_OPTION,
227 IMAGEX_STAGING_DIR_OPTION,
228 IMAGEX_STREAMS_INTERFACE_OPTION,
229 IMAGEX_STRICT_ACLS_OPTION,
230 IMAGEX_SYMLINK_OPTION,
231 IMAGEX_THREADS_OPTION,
232 IMAGEX_TO_STDOUT_OPTION,
233 IMAGEX_UNIX_DATA_OPTION,
234 IMAGEX_VERBOSE_OPTION,
238 static const struct option apply_options[] = {
239 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
240 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
241 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
242 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
243 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
244 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
245 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
246 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
247 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
248 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
249 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
250 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
253 static const struct option capture_or_append_options[] = {
254 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
255 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
256 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
257 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
258 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
259 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
260 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
261 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
262 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
263 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
264 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
265 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
266 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
267 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
268 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
269 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
272 static const struct option delete_options[] = {
273 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
274 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
278 static const struct option export_options[] = {
279 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
280 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
281 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
282 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
283 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
284 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
288 static const struct option extract_options[] = {
289 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
290 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
291 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
292 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
293 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
294 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
295 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
296 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
297 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
298 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
302 static const struct option info_options[] = {
303 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
304 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
305 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
306 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
307 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
308 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
309 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
313 static const struct option join_options[] = {
314 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
318 static const struct option mount_options[] = {
319 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
320 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
321 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
322 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
323 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
324 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
325 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
329 static const struct option optimize_options[] = {
330 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
331 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
332 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
336 static const struct option split_options[] = {
337 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
341 static const struct option unmount_options[] = {
342 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
343 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
344 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
345 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
349 static const struct option update_options[] = {
350 /* Careful: some of the options here set the defaults for update
351 * commands, but the flags given to an actual update command (and not to
352 * `imagex update' itself are also handled in
353 * update_command_add_option(). */
354 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
355 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
356 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
357 {T("command"), required_argument, NULL, IMAGEX_COMMAND_OPTION},
359 /* Default delete options */
360 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
361 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
363 /* Global add option */
364 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
366 /* Default add options */
367 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
368 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
369 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
370 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
371 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
372 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
377 /* Print formatted error message to stderr. */
379 imagex_error(const tchar *format, ...)
382 va_start(va, format);
383 tfputs(T("ERROR: "), stderr);
384 tvfprintf(stderr, format, va);
385 tputc(T('\n'), stderr);
389 /* Print formatted error message to stderr. */
391 imagex_error_with_errno(const tchar *format, ...)
393 int errno_save = errno;
395 va_start(va, format);
396 tfputs(T("ERROR: "), stderr);
397 tvfprintf(stderr, format, va);
398 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
403 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
405 if (image == WIMLIB_NO_IMAGE) {
406 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
407 " Please specify a 1-based image index or "
409 " You may use `"IMAGEX_PROGNAME" info' to list the images "
410 "contained in a WIM."),
411 image_name, wim_name);
418 verify_image_is_single(int image)
420 if (image == WIMLIB_ALL_IMAGES) {
421 imagex_error(T("Cannot specify all images for this action!"));
428 verify_image_exists_and_is_single(int image, const tchar *image_name,
429 const tchar *wim_name)
432 ret = verify_image_exists(image, image_name, wim_name);
434 ret = verify_image_is_single(image);
438 /* Parse the argument to --compress */
440 get_compression_type(const tchar *optarg)
442 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
443 return WIMLIB_COMPRESSION_TYPE_LZX;
444 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
445 return WIMLIB_COMPRESSION_TYPE_XPRESS;
446 else if (!tstrcasecmp(optarg, T("none")))
447 return WIMLIB_COMPRESSION_TYPE_NONE;
449 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
450 "\"maximum\", \"fast\", or \"none\"."), optarg);
451 return WIMLIB_COMPRESSION_TYPE_INVALID;
455 /* Returns the size of a file given its name, or -1 if the file does not exist
456 * or its size cannot be determined. */
458 file_get_size(const tchar *filename)
461 if (tstat(filename, &st) == 0)
467 tchar pat_ntfs_log[] = T("/$ntfs.log");
468 tchar pat_hiberfil_sys[] = T("/hiberfil.sys");
469 tchar pat_pagefile_sys[] = T("/pagefile.sys");
470 tchar pat_system_volume_information[] = T("/System Volume Information");
471 tchar pat_recycler[] = T("/RECYCLER");
472 tchar pat_windows_csc[] = T("/Windows/CSC");
474 tchar *default_pats[] = {
478 pat_system_volume_information,
483 static struct wimlib_capture_config default_capture_config = {
485 .num_pats = sizeof(default_pats) / sizeof(*default_pats),
486 .pats = default_pats,
491 PARSE_STRING_SUCCESS = 0,
492 PARSE_STRING_FAILURE = 1,
493 PARSE_STRING_NONE = 2,
497 * Parses a string token from an array of characters.
499 * Tokens are either whitespace-delimited, or double or single-quoted.
501 * @line_p: Pointer to the pointer to the line of data. Will be updated
502 * to point past the string token iff the return value is
503 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
506 * @len_p: @len_p initially stores the length of the line of data, which may
507 * be 0, and it will be updated to the number of bytes remaining in
508 * the line iff the return value is PARSE_STRING_SUCCESS.
510 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
511 * parsed string token will be returned here.
513 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
514 * PARSE_STRING_FAILURE if the data was invalid due to a missing
515 * closing quote; or PARSE_STRING_NONE if the line ended before the
516 * beginning of a string token was found.
519 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
522 tchar *line = *line_p;
526 /* Skip leading whitespace */
529 return PARSE_STRING_NONE;
530 if (!istspace(*line) && *line != T('\0'))
536 if (quote_char == T('"') || quote_char == T('\'')) {
541 line = tmemchr(line, quote_char, len);
543 imagex_error(T("Missing closing quote: %"TS), fn - 1);
544 return PARSE_STRING_FAILURE;
547 /* Unquoted string. Go until whitespace. Line is terminated
548 * by '\0', so no need to check 'len'. */
552 } while (!istspace(*line) && *line != T('\0'));
559 return PARSE_STRING_SUCCESS;
562 /* Parses a line of data (not an empty line or comment) in the source list file
563 * format. (See the man page for 'wimlib-imagex capture' for details on this
564 * format and the meaning.)
566 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
567 * len == 0. The data in @line will be modified by this function call.
569 * @len: Length of the line of data.
571 * @source: On success, the capture source and target described by the line is
572 * written into this destination. Note that it will contain pointers
573 * to data in the @line array.
575 * Returns true if the line was valid; false otherwise. */
577 parse_source_list_line(tchar *line, size_t len,
578 struct wimlib_capture_source *source)
582 ret = parse_string(&line, &len, &source->fs_source_path);
583 if (ret != PARSE_STRING_SUCCESS)
585 ret = parse_string(&line, &len, &source->wim_target_path);
586 if (ret == PARSE_STRING_NONE)
587 source->wim_target_path = source->fs_source_path;
588 return ret != PARSE_STRING_FAILURE;
591 /* Returns %true if the given line of length @len > 0 is a comment or empty line
592 * in the source list file format. */
594 is_comment_line(const tchar *line, size_t len)
599 if (!istspace(*line) && *line != T('\0'))
609 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
612 tchar *contents = *contents_p;
613 size_t nchars = *nchars_p;
616 for (i = 0; i < nchars; i++)
617 if (contents[i] == T('\n'))
620 /* Handle last line not terminated by a newline */
621 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
622 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
624 imagex_error(T("Out of memory!"));
627 contents[nchars] = T('\n');
628 *contents_p = contents;
636 /* Parses a file in the source list format. (See the man page for
637 * 'wimlib-imagex capture' for details on this format and the meaning.)
639 * @source_list_contents: Contents of the source list file. Note that this
640 * buffer will be modified to save memory allocations,
641 * and cannot be freed until the returned array of
642 * wimlib_capture_source's has also been freed.
644 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
647 * @nsources_ret: On success, the length of the returned array is
650 * Returns: An array of `struct wimlib_capture_source's that can be passed to
651 * the wimlib_add_image_multisource() function to specify how a WIM image is to
653 static struct wimlib_capture_source *
654 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
655 size_t *nsources_ret)
659 struct wimlib_capture_source *sources;
662 nlines = text_file_count_lines(source_list_contents_p,
663 &source_list_nchars);
667 /* Always allocate at least 1 slot, just in case the implementation of
668 * calloc() returns NULL if 0 bytes are requested. */
669 sources = calloc(nlines ?: 1, sizeof(*sources));
671 imagex_error(T("out of memory"));
674 p = *source_list_contents_p;
676 for (i = 0; i < nlines; i++) {
677 /* XXX: Could use rawmemchr() here instead, but it may not be
678 * available on all platforms. */
679 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
680 size_t len = endp - p + 1;
682 if (!is_comment_line(p, len)) {
683 if (!parse_source_list_line(p, len, &sources[j++])) {
696 enum capture_config_section {
697 CAPTURE_CONFIG_NO_SECTION,
698 CAPTURE_CONFIG_EXCLUSION_SECTION,
699 CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
700 CAPTURE_CONFIG_IGNORE_SECTION,
704 CAPTURE_CONFIG_INVALID_SECTION,
705 CAPTURE_CONFIG_CHANGED_SECTION,
706 CAPTURE_CONFIG_SAME_SECTION,
710 check_config_section(tchar *line, size_t len,
711 enum capture_config_section *cur_section)
713 while (istspace(*line))
717 return CAPTURE_CONFIG_SAME_SECTION;
720 tchar *endbrace = tstrrchr(line, T(']'));
722 return CAPTURE_CONFIG_SAME_SECTION;
724 if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
725 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
726 } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
727 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
728 } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
729 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
730 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
731 "of capture config file\n"),
733 } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
734 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
735 tfputs(T("WARNING: Ignoring [AlignmentList] section "
736 "of capture config file\n"),
739 imagex_error(T("Invalid capture config file section \"%"TS"\""),
741 return CAPTURE_CONFIG_INVALID_SECTION;
743 return CAPTURE_CONFIG_CHANGED_SECTION;
748 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
751 if (pat_list->num_pats == pat_list->num_allocated_pats) {
753 size_t num_allocated_pats = pat_list->num_pats + 8;
755 pats = realloc(pat_list->pats,
756 num_allocated_pats * sizeof(pat_list->pats[0]));
758 imagex_error(T("Out of memory!"));
761 pat_list->pats = pats;
762 pat_list->num_allocated_pats = num_allocated_pats;
764 pat_list->pats[pat_list->num_pats++] = pat;
769 parse_capture_config_line(tchar *line, size_t len,
770 enum capture_config_section *cur_section,
771 struct wimlib_capture_config *config)
776 ret = check_config_section(line, len, cur_section);
777 if (ret == CAPTURE_CONFIG_INVALID_SECTION)
779 if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
782 switch (*cur_section) {
783 case CAPTURE_CONFIG_NO_SECTION:
784 imagex_error(T("Line \"%"TS"\" is not in a section "
785 "(such as [ExclusionList]"), line);
787 case CAPTURE_CONFIG_EXCLUSION_SECTION:
788 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
790 return pattern_list_add_pattern(&config->exclusion_pats,
792 case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
793 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
795 return pattern_list_add_pattern(&config->exclusion_exception_pats,
797 case CAPTURE_CONFIG_IGNORE_SECTION:
804 parse_capture_config(tchar **contents_p, size_t nchars,
805 struct wimlib_capture_config *config)
810 enum capture_config_section cur_section;
812 memset(config, 0, sizeof(*config));
814 nlines = text_file_count_lines(contents_p, &nchars);
818 cur_section = CAPTURE_CONFIG_NO_SECTION;
820 for (i = 0; i < nlines; i++) {
821 tchar *endp = tmemchr(p, T('\n'), nchars);
822 size_t len = endp - p + 1;
824 if (!is_comment_line(p, len))
825 if (!parse_capture_config_line(p, len, &cur_section, config))
833 /* Reads the contents of a file into memory. */
835 file_get_contents(const tchar *filename, size_t *len_ret)
842 if (tstat(filename, &stbuf) != 0) {
843 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
848 fp = tfopen(filename, T("rb"));
850 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
856 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
857 "contents of file \"%"TS"\""), len, filename);
860 if (fread(buf, 1, len, fp) != len) {
861 imagex_error_with_errno(T("Failed to read %zu bytes from the "
862 "file \"%"TS"\""), len, filename);
876 /* Read standard input until EOF and return the full contents in a malloc()ed
877 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
880 stdin_get_contents(size_t *len_ret)
882 /* stdin can, of course, be a pipe or other non-seekable file, so the
883 * total length of the data cannot be pre-determined */
885 size_t newlen = 1024;
889 char *p = realloc(buf, newlen);
890 size_t bytes_read, bytes_to_read;
892 imagex_error(T("out of memory while reading stdin"));
896 bytes_to_read = newlen - pos;
897 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
899 if (bytes_read != bytes_to_read) {
904 imagex_error_with_errno(T("error reading stdin"));
918 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
921 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
923 *num_tchars_ret = num_bytes;
925 #else /* !__WIN32__ */
926 /* On Windows, translate the text to UTF-16LE */
930 if (num_bytes >= 2 &&
931 ((text[0] == 0xff && text[1] == 0xfe) ||
932 (text[0] <= 0x7f && text[1] == 0x00)))
934 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
935 * with something that looks like an ASCII character encoded as
936 * a UTF-16LE code unit. Assume the file is encoded as
937 * UTF-16LE. This is not a 100% reliable check. */
938 num_wchars = num_bytes / 2;
939 text_wstr = (wchar_t*)text;
941 /* File does not look like UTF-16LE. Assume it is encoded in
942 * the current Windows code page. I think these are always
943 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
944 * should work as expected. */
945 text_wstr = win32_mbs_to_wcs(text,
950 *num_tchars_ret = num_wchars;
952 #endif /* __WIN32__ */
956 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
961 contents = file_get_contents(filename, &num_bytes);
964 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
968 stdin_get_text_contents(size_t *num_tchars_ret)
973 contents = stdin_get_contents(&num_bytes);
976 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
979 /* Return 0 if a path names a file to which the current user has write access;
980 * -1 otherwise (and print an error message). */
982 file_writable(const tchar *path)
985 ret = taccess(path, W_OK);
987 imagex_error_with_errno(T("Can't modify \"%"TS"\""), path);
991 #define TO_PERCENT(numerator, denominator) \
992 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
994 /* Given an enumerated value for WIM compression type, return a descriptive
997 get_data_type(int ctype)
1000 case WIMLIB_COMPRESSION_TYPE_NONE:
1001 return T("uncompressed");
1002 case WIMLIB_COMPRESSION_TYPE_LZX:
1003 return T("LZX-compressed");
1004 case WIMLIB_COMPRESSION_TYPE_XPRESS:
1005 return T("XPRESS-compressed");
1010 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
1011 #define MEBIBYTE_MIN_NBYTES 10000000ULL
1012 #define KIBIBYTE_MIN_NBYTES 10000ULL
1015 get_unit(uint64_t total_bytes, const tchar **name_ret)
1017 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
1018 *name_ret = T("GiB");
1020 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
1021 *name_ret = T("MiB");
1023 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
1024 *name_ret = T("KiB");
1027 *name_ret = T("bytes");
1032 /* Progress callback function passed to various wimlib functions. */
1034 imagex_progress_func(enum wimlib_progress_msg msg,
1035 const union wimlib_progress_info *info)
1037 unsigned percent_done;
1038 unsigned unit_shift;
1039 const tchar *unit_name;
1040 if (imagex_be_quiet)
1043 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1044 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1045 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1046 info->write_streams.total_bytes);
1047 if (info->write_streams.completed_streams == 0) {
1048 const tchar *data_type;
1050 data_type = get_data_type(info->write_streams.compression_type);
1051 tprintf(T("Writing %"TS" data using %u thread%"TS"\n"),
1052 data_type, info->write_streams.num_threads,
1053 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1055 tprintf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1056 "written (%u%% done)"),
1057 info->write_streams.completed_bytes >> unit_shift,
1059 info->write_streams.total_bytes >> unit_shift,
1062 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1065 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1066 tprintf(T("Scanning \"%"TS"\""), info->scan.source);
1067 if (*info->scan.wim_target_path) {
1068 tprintf(T(" (loading as WIM path: \"/%"TS"\")...\n"),
1069 info->scan.wim_target_path);
1071 tprintf(T(" (loading as root of WIM image)...\n"));
1074 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1075 if (info->scan.excluded)
1076 tprintf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1078 tprintf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
1080 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
1082 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1083 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1084 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1085 info->integrity.total_bytes);
1086 tprintf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" "TS" "
1087 "of %"PRIu64" "TS" (%u%%) done"),
1088 info->integrity.filename,
1089 info->integrity.completed_bytes >> unit_shift,
1091 info->integrity.total_bytes >> unit_shift,
1094 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1097 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1098 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1099 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1100 info->integrity.total_bytes);
1101 tprintf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1102 "of %"PRIu64" %"TS" (%u%%) done"),
1103 info->integrity.completed_bytes >> unit_shift,
1105 info->integrity.total_bytes >> unit_shift,
1108 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1111 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1112 tprintf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1113 "to %"TS" \"%"TS"\"\n"),
1114 info->extract.image,
1115 info->extract.image_name,
1116 info->extract.wimfile_name,
1117 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1118 T("NTFS volume") : T("directory")),
1119 info->extract.target);
1121 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1122 tprintf(T("Extracting \"%"TS"\" from image %d (\"%"TS"\") "
1123 "in \"%"TS"\" to \"%"TS"\"\n"),
1124 info->extract.extract_root_wim_source_path,
1125 info->extract.image,
1126 info->extract.image_name,
1127 info->extract.wimfile_name,
1128 info->extract.target);
1130 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
1131 /*tprintf(T("Applying directory structure to %"TS"\n"),*/
1132 /*info->extract.target);*/
1134 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1135 percent_done = TO_PERCENT(info->extract.completed_bytes,
1136 info->extract.total_bytes);
1137 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1138 tprintf(T("\rExtracting files: "
1139 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1140 info->extract.completed_bytes >> unit_shift,
1142 info->extract.total_bytes >> unit_shift,
1145 if (info->extract.completed_bytes >= info->extract.total_bytes)
1148 case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
1149 tprintf(T("%"TS"\n"), info->extract.cur_path);
1151 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1152 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1153 tprintf(T("Setting timestamps on all extracted files...\n"));
1155 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1156 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1157 tprintf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1158 info->extract.target);
1161 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
1162 percent_done = TO_PERCENT(info->join.completed_bytes,
1163 info->join.total_bytes);
1164 unit_shift = get_unit(info->join.total_bytes, &unit_name);
1165 tprintf(T("Writing resources from part %u of %u: "
1166 "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written\n"),
1167 (info->join.completed_parts == info->join.total_parts) ?
1168 info->join.completed_parts : info->join.completed_parts + 1,
1169 info->join.total_parts,
1170 info->join.completed_bytes >> unit_shift,
1172 info->join.total_bytes >> unit_shift,
1176 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1177 percent_done = TO_PERCENT(info->split.completed_bytes,
1178 info->split.total_bytes);
1179 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1180 tprintf(T("Writing \"%"TS"\": %"PRIu64" %"TS" of "
1181 "%"PRIu64" %"TS" (%u%%) written\n"),
1182 info->split.part_name,
1183 info->split.completed_bytes >> unit_shift,
1185 info->split.total_bytes >> unit_shift,
1189 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1190 if (info->split.completed_bytes == info->split.total_bytes) {
1191 tprintf(T("Finished writing %u split WIM parts\n"),
1192 info->split.cur_part_number);
1202 /* Open all the split WIM parts that correspond to a file glob.
1204 * @first_part specifies the first part of the split WIM and it may be either
1205 * included or omitted from the glob. */
1207 open_swms_from_glob(const tchar *swm_glob,
1208 const tchar *first_part,
1210 WIMStruct ***additional_swms_ret,
1211 unsigned *num_additional_swms_ret)
1213 unsigned num_additional_swms = 0;
1214 WIMStruct **additional_swms = NULL;
1218 /* Warning: glob() is replaced in Windows native builds */
1219 ret = tglob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
1221 if (ret == GLOB_NOMATCH) {
1222 imagex_error(T("Found no files for glob \"%"TS"\""),
1225 imagex_error_with_errno(T("Failed to process glob \"%"TS"\""),
1231 num_additional_swms = globbuf.gl_pathc;
1232 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
1233 if (!additional_swms) {
1234 imagex_error(T("Out of memory"));
1238 unsigned offset = 0;
1239 for (unsigned i = 0; i < num_additional_swms; i++) {
1240 if (tstrcmp(globbuf.gl_pathv[i], first_part) == 0) {
1244 ret = wimlib_open_wim(globbuf.gl_pathv[i],
1245 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
1246 &additional_swms[i - offset],
1247 imagex_progress_func);
1249 goto out_close_swms;
1251 *additional_swms_ret = additional_swms;
1252 *num_additional_swms_ret = num_additional_swms - offset;
1256 for (unsigned i = 0; i < num_additional_swms; i++)
1257 wimlib_free(additional_swms[i]);
1258 free(additional_swms);
1267 parse_num_threads(const tchar *optarg)
1270 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1271 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1272 imagex_error(T("Number of threads must be a non-negative integer!"));
1280 * Parse an option passed to an update command.
1282 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1285 * @option: Text string for the option (beginning with --)
1287 * @cmd: `struct wimlib_update_command' that is being constructed for
1290 * Returns true if the option was recognized; false if not.
1293 update_command_add_option(int op, const tchar *option,
1294 struct wimlib_update_command *cmd)
1296 bool recognized = true;
1298 case WIMLIB_UPDATE_OP_ADD:
1299 if (!tstrcmp(option, T("--verbose")))
1300 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1301 else if (!tstrcmp(option, T("--unix-data")))
1302 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1303 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1304 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1305 else if (!tstrcmp(option, T("--strict-acls")))
1306 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1307 else if (!tstrcmp(option, T("--dereference")))
1308 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1312 case WIMLIB_UPDATE_OP_DELETE:
1313 if (!tstrcmp(option, T("--force")))
1314 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1315 else if (!tstrcmp(option, T("--recursive")))
1316 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1327 /* How many nonoption arguments each `imagex update' command expects */
1328 static const unsigned update_command_num_nonoptions[] = {
1329 [WIMLIB_UPDATE_OP_ADD] = 2,
1330 [WIMLIB_UPDATE_OP_DELETE] = 1,
1331 [WIMLIB_UPDATE_OP_RENAME] = 2,
1335 update_command_add_nonoption(int op, const tchar *nonoption,
1336 struct wimlib_update_command *cmd,
1337 unsigned num_nonoptions)
1340 case WIMLIB_UPDATE_OP_ADD:
1341 if (num_nonoptions == 0)
1342 cmd->add.fs_source_path = (tchar*)nonoption;
1344 cmd->add.wim_target_path = (tchar*)nonoption;
1346 case WIMLIB_UPDATE_OP_DELETE:
1347 cmd->delete.wim_path = (tchar*)nonoption;
1349 case WIMLIB_UPDATE_OP_RENAME:
1350 if (num_nonoptions == 0)
1351 cmd->rename.wim_source_path = (tchar*)nonoption;
1353 cmd->rename.wim_target_path = (tchar*)nonoption;
1359 * Parse a command passed on stdin to `imagex update'.
1361 * @line: Text of the command.
1362 * @len: Length of the line, including a null terminator
1365 * @command: A `struct wimlib_update_command' to fill in from the parsed
1368 * @line_number: Line number of the command, for diagnostics.
1370 * Returns true on success; returns false on parse error.
1373 parse_update_command(tchar *line, size_t len,
1374 struct wimlib_update_command *command,
1378 tchar *command_name;
1380 size_t num_nonoptions;
1382 /* Get the command name ("add", "delete", "rename") */
1383 ret = parse_string(&line, &len, &command_name);
1384 if (ret != PARSE_STRING_SUCCESS)
1387 if (!tstrcasecmp(command_name, T("add"))) {
1388 op = WIMLIB_UPDATE_OP_ADD;
1389 } else if (!tstrcasecmp(command_name, T("delete"))) {
1390 op = WIMLIB_UPDATE_OP_DELETE;
1391 } else if (!tstrcasecmp(command_name, T("rename"))) {
1392 op = WIMLIB_UPDATE_OP_RENAME;
1394 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1395 command_name, line_number);
1400 /* Parse additional options and non-options as needed */
1405 ret = parse_string(&line, &len, &next_string);
1406 if (ret == PARSE_STRING_NONE) /* End of line */
1408 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1410 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1412 if (!update_command_add_option(op, next_string, command))
1414 imagex_error(T("Unrecognized option \"%"TS"\" to "
1415 "update command \"%"TS"\" on line %zu"),
1416 next_string, command_name, line_number);
1422 if (num_nonoptions == update_command_num_nonoptions[op])
1424 imagex_error(T("Unexpected argument \"%"TS"\" in "
1425 "update command on line %zu\n"
1426 " (The \"%"TS"\" command only "
1427 "takes %u nonoption arguments!)\n"),
1428 next_string, line_number,
1429 command_name, num_nonoptions);
1432 update_command_add_nonoption(op, next_string,
1433 command, num_nonoptions);
1438 if (num_nonoptions != update_command_num_nonoptions[op]) {
1439 imagex_error(T("Not enough arguments to update command "
1440 "\"%"TS"\" on line %zu"), command_name, line_number);
1446 static struct wimlib_update_command *
1447 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1448 size_t *num_cmds_ret)
1452 struct wimlib_update_command *cmds;
1455 nlines = text_file_count_lines(cmd_file_contents_p,
1460 /* Always allocate at least 1 slot, just in case the implementation of
1461 * calloc() returns NULL if 0 bytes are requested. */
1462 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1464 imagex_error(T("out of memory"));
1467 p = *cmd_file_contents_p;
1469 for (i = 0; i < nlines; i++) {
1470 /* XXX: Could use rawmemchr() here instead, but it may not be
1471 * available on all platforms. */
1472 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1473 size_t len = endp - p + 1;
1475 if (!is_comment_line(p, len)) {
1476 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1487 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1488 * one image from a WIM file to a NTFS volume. */
1490 imagex_apply(int argc, tchar **argv)
1493 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1498 const tchar *wimfile;
1499 const tchar *target;
1500 const tchar *image_num_or_name;
1501 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1503 const tchar *swm_glob = NULL;
1504 WIMStruct **additional_swms = NULL;
1505 unsigned num_additional_swms = 0;
1507 for_opt(c, apply_options) {
1509 case IMAGEX_CHECK_OPTION:
1510 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1512 case IMAGEX_HARDLINK_OPTION:
1513 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1515 case IMAGEX_SYMLINK_OPTION:
1516 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1518 case IMAGEX_VERBOSE_OPTION:
1519 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
1521 case IMAGEX_REF_OPTION:
1524 case IMAGEX_UNIX_DATA_OPTION:
1525 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1527 case IMAGEX_NO_ACLS_OPTION:
1528 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1530 case IMAGEX_STRICT_ACLS_OPTION:
1531 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1533 case IMAGEX_NORPFIX_OPTION:
1534 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1536 case IMAGEX_RPFIX_OPTION:
1537 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1539 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1540 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1541 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1550 if (argc != 2 && argc != 3) {
1557 image_num_or_name = T("1");
1560 image_num_or_name = argv[1];
1564 ret = wimlib_open_wim(wimfile, open_flags, &w, imagex_progress_func);
1568 image = wimlib_resolve_image(w, image_num_or_name);
1569 ret = verify_image_exists(image, image_num_or_name, wimfile);
1573 num_images = wimlib_get_num_images(w);
1574 if (argc == 2 && num_images != 1) {
1575 imagex_error(T("\"%"TS"\" contains %d images; Please select one "
1576 "(or all)."), wimfile, num_images);
1583 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1585 &num_additional_swms);
1592 ret = tstat(target, &stbuf);
1594 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1595 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1597 if (errno != ENOENT) {
1598 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1606 win32_acquire_restore_privileges();
1608 ret = wimlib_extract_image(w, image, target, extract_flags,
1609 additional_swms, num_additional_swms,
1610 imagex_progress_func);
1612 tprintf(T("Done applying WIM image.\n"));
1614 win32_release_restore_privileges();
1618 if (additional_swms) {
1619 for (unsigned i = 0; i < num_additional_swms; i++)
1620 wimlib_free(additional_swms[i]);
1621 free(additional_swms);
1626 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1627 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1628 * the desired image. 'wimlib-imagex append': add a new image to an existing
1631 imagex_capture_or_append(int argc, tchar **argv)
1635 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
1636 int write_flags = 0;
1637 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
1638 const tchar *wimfile;
1641 const tchar *flags_element = NULL;
1645 int cmd = tstrcmp(argv[0], T("append")) ? CAPTURE : APPEND;
1646 unsigned num_threads = 0;
1649 size_t source_name_len;
1652 const tchar *config_file = NULL;
1654 struct wimlib_capture_config *config = NULL;
1656 bool source_list = false;
1657 size_t source_list_nchars;
1658 tchar *source_list_contents;
1659 bool capture_sources_malloced;
1660 struct wimlib_capture_source *capture_sources;
1663 for_opt(c, capture_or_append_options) {
1665 case IMAGEX_BOOT_OPTION:
1666 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1668 case IMAGEX_CHECK_OPTION:
1669 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1670 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1672 case IMAGEX_CONFIG_OPTION:
1673 config_file = optarg;
1675 case IMAGEX_COMPRESS_OPTION:
1676 compression_type = get_compression_type(optarg);
1677 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1680 case IMAGEX_FLAGS_OPTION:
1681 flags_element = optarg;
1683 case IMAGEX_DEREFERENCE_OPTION:
1684 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1686 case IMAGEX_VERBOSE_OPTION:
1687 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1689 case IMAGEX_THREADS_OPTION:
1690 num_threads = parse_num_threads(optarg);
1691 if (num_threads == UINT_MAX)
1694 case IMAGEX_REBUILD_OPTION:
1695 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1697 case IMAGEX_UNIX_DATA_OPTION:
1698 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1700 case IMAGEX_SOURCE_LIST_OPTION:
1703 case IMAGEX_NO_ACLS_OPTION:
1704 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1706 case IMAGEX_STRICT_ACLS_OPTION:
1707 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1709 case IMAGEX_RPFIX_OPTION:
1710 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1712 case IMAGEX_NORPFIX_OPTION:
1713 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1723 if (argc < 2 || argc > 4) {
1734 /* Set default name to SOURCE argument, omitting any directory
1735 * prefixes and trailing slashes. This requires making a copy
1737 source_name_len = tstrlen(source);
1738 source_copy = alloca((source_name_len + 1) * sizeof(tchar));
1739 name = tbasename(tstrcpy(source_copy, source));
1741 /* Image description defaults to NULL if not given. */
1742 desc = (argc >= 4) ? argv[3] : NULL;
1745 /* Set up capture sources in source list mode */
1746 if (source[0] == T('-') && source[1] == T('\0')) {
1747 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1749 source_list_contents = file_get_text_contents(source,
1750 &source_list_nchars);
1752 if (!source_list_contents)
1755 capture_sources = parse_source_list(&source_list_contents,
1758 if (!capture_sources) {
1760 goto out_free_source_list_contents;
1762 capture_sources_malloced = true;
1764 /* Set up capture source in non-source-list mode (could be
1765 * either "normal" mode or "NTFS mode"--- see the man page). */
1766 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1767 capture_sources[0].fs_source_path = source;
1768 capture_sources[0].wim_target_path = NULL;
1769 capture_sources[0].reserved = 0;
1771 capture_sources_malloced = false;
1772 source_list_contents = NULL;
1778 config_str = file_get_text_contents(config_file, &config_len);
1781 goto out_free_capture_sources;
1784 config = alloca(sizeof(*config));
1785 ret = parse_capture_config(&config_str, config_len, config);
1787 goto out_free_config;
1789 config = &default_capture_config;
1793 ret = wimlib_open_wim(wimfile, open_flags, &w,
1794 imagex_progress_func);
1796 ret = wimlib_create_new_wim(compression_type, &w);
1798 goto out_free_config;
1802 ret = tstat(source, &stbuf);
1804 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1805 tprintf(T("Capturing WIM image from NTFS "
1806 "filesystem on \"%"TS"\"\n"), source);
1807 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1810 if (errno != ENOENT) {
1811 imagex_error_with_errno(T("Failed to stat "
1812 "\"%"TS"\""), source);
1814 goto out_wimlib_free;
1819 win32_acquire_capture_privileges();
1822 ret = wimlib_add_image_multisource(w,
1828 imagex_progress_func);
1830 goto out_release_privs;
1831 cur_image = wimlib_get_num_images(w);
1833 ret = wimlib_set_image_descripton(w, cur_image, desc);
1835 goto out_release_privs;
1837 if (flags_element) {
1838 ret = wimlib_set_image_flags(w, cur_image, flags_element);
1840 goto out_release_privs;
1842 if (cmd == APPEND) {
1843 ret = wimlib_overwrite(w, write_flags, num_threads,
1844 imagex_progress_func);
1846 ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
1847 num_threads, imagex_progress_func);
1849 if (ret == WIMLIB_ERR_REOPEN)
1852 imagex_error(T("Failed to write the WIM file \"%"TS"\""),
1856 win32_release_capture_privileges();
1861 if (config != NULL && config != &default_capture_config) {
1862 free(config->exclusion_pats.pats);
1863 free(config->exclusion_exception_pats.pats);
1866 out_free_capture_sources:
1867 if (capture_sources_malloced)
1868 free(capture_sources);
1869 out_free_source_list_contents:
1870 free(source_list_contents);
1874 /* Remove image(s) from a WIM. */
1876 imagex_delete(int argc, tchar **argv)
1880 int write_flags = 0;
1881 const tchar *wimfile;
1882 const tchar *image_num_or_name;
1887 for_opt(c, delete_options) {
1889 case IMAGEX_CHECK_OPTION:
1890 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1891 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1893 case IMAGEX_SOFT_OPTION:
1894 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
1906 imagex_error(T("Must specify a WIM file"));
1908 imagex_error(T("Must specify an image"));
1913 image_num_or_name = argv[1];
1915 ret = file_writable(wimfile);
1919 ret = wimlib_open_wim(wimfile, open_flags, &w,
1920 imagex_progress_func);
1924 image = wimlib_resolve_image(w, image_num_or_name);
1926 ret = verify_image_exists(image, image_num_or_name, wimfile);
1930 ret = wimlib_delete_image(w, image);
1932 imagex_error(T("Failed to delete image from \"%"TS"\""), wimfile);
1936 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1937 if (ret == WIMLIB_ERR_REOPEN)
1940 imagex_error(T("Failed to write the file \"%"TS"\" with image "
1941 "deleted"), wimfile);
1948 /* Print the files contained in an image(s) in a WIM file. */
1950 imagex_dir(int argc, tchar **argv)
1952 const tchar *wimfile;
1959 imagex_error(T("Must specify a WIM file"));
1964 imagex_error(T("Too many arguments"));
1970 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w,
1971 imagex_progress_func);
1976 image = wimlib_resolve_image(w, argv[2]);
1977 ret = verify_image_exists(image, argv[2], wimfile);
1981 /* Image was not specified. If the WIM only contains one image,
1982 * choose that one; otherwise, print an error. */
1983 num_images = wimlib_get_num_images(w);
1984 if (num_images != 1) {
1985 imagex_error(T("\"%"TS"\" contains %d images; Please "
1986 "select one."), wimfile, num_images);
1994 ret = wimlib_print_files(w, image);
2000 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2003 imagex_export(int argc, tchar **argv)
2007 int export_flags = 0;
2008 int write_flags = 0;
2009 int compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
2010 bool compression_type_specified = false;
2011 const tchar *src_wimfile;
2012 const tchar *src_image_num_or_name;
2013 const tchar *dest_wimfile;
2014 const tchar *dest_name;
2015 const tchar *dest_desc;
2016 WIMStruct *src_w = NULL;
2017 WIMStruct *dest_w = NULL;
2022 const tchar *swm_glob = NULL;
2023 WIMStruct **additional_swms = NULL;
2024 unsigned num_additional_swms = 0;
2025 unsigned num_threads = 0;
2027 for_opt(c, export_options) {
2029 case IMAGEX_BOOT_OPTION:
2030 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2032 case IMAGEX_CHECK_OPTION:
2033 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2034 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2036 case IMAGEX_COMPRESS_OPTION:
2037 compression_type = get_compression_type(optarg);
2038 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2040 compression_type_specified = true;
2042 case IMAGEX_REF_OPTION:
2045 case IMAGEX_THREADS_OPTION:
2046 num_threads = parse_num_threads(optarg);
2047 if (num_threads == UINT_MAX)
2050 case IMAGEX_REBUILD_OPTION:
2051 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2060 if (argc < 3 || argc > 5) {
2064 src_wimfile = argv[0];
2065 src_image_num_or_name = argv[1];
2066 dest_wimfile = argv[2];
2067 dest_name = (argc >= 4) ? argv[3] : NULL;
2068 dest_desc = (argc >= 5) ? argv[4] : NULL;
2069 ret = wimlib_open_wim(src_wimfile,
2070 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w,
2071 imagex_progress_func);
2075 /* Determine if the destination is an existing file or not.
2076 * If so, we try to append the exported image(s) to it; otherwise, we
2077 * create a new WIM containing the exported image(s). */
2078 if (tstat(dest_wimfile, &stbuf) == 0) {
2082 /* Destination file exists. */
2084 if (!S_ISREG(stbuf.st_mode)) {
2085 imagex_error(T("\"%"TS"\" is not a regular file"),
2090 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w,
2091 imagex_progress_func);
2095 ret = file_writable(dest_wimfile);
2099 dest_ctype = wimlib_get_compression_type(dest_w);
2100 if (compression_type_specified
2101 && compression_type != dest_ctype)
2103 imagex_error(T("Cannot specify a compression type that is "
2104 "not the same as that used in the "
2105 "destination WIM"));
2111 /* dest_wimfile is not an existing file, so create a new WIM. */
2112 if (!compression_type_specified)
2113 compression_type = wimlib_get_compression_type(src_w);
2114 if (errno == ENOENT) {
2115 ret = wimlib_create_new_wim(compression_type, &dest_w);
2119 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2126 image = wimlib_resolve_image(src_w, src_image_num_or_name);
2127 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2132 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
2134 &num_additional_swms);
2139 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
2140 export_flags, additional_swms,
2141 num_additional_swms, imagex_progress_func);
2147 ret = wimlib_write(dest_w, dest_wimfile, WIMLIB_ALL_IMAGES,
2148 write_flags, num_threads,
2149 imagex_progress_func);
2151 ret = wimlib_overwrite(dest_w, write_flags, num_threads,
2152 imagex_progress_func);
2154 if (ret == WIMLIB_ERR_REOPEN)
2157 wimlib_free(dest_w);
2158 if (additional_swms) {
2159 for (unsigned i = 0; i < num_additional_swms; i++)
2160 wimlib_free(additional_swms[i]);
2161 free(additional_swms);
2167 is_root_wim_path(const tchar *path)
2170 for (p = path; *p; p++)
2171 if (*p != T('\\') && *p != T('/'))
2177 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2178 const tchar *dest_dir)
2180 for (size_t i = 0; i < num_cmds; i++)
2181 if (cmds[i].fs_dest_path != dest_dir)
2182 free(cmds[i].fs_dest_path);
2186 static struct wimlib_extract_command *
2187 prepare_extract_commands(tchar **paths, unsigned num_paths,
2188 int extract_flags, tchar *dest_dir,
2189 size_t *num_cmds_ret)
2191 struct wimlib_extract_command *cmds;
2193 tchar *emptystr = T("");
2195 if (num_paths == 0) {
2199 num_cmds = num_paths;
2200 cmds = calloc(num_cmds, sizeof(cmds[0]));
2202 imagex_error(T("Out of memory!"));
2206 for (size_t i = 0; i < num_cmds; i++) {
2207 cmds[i].extract_flags = extract_flags;
2208 cmds[i].wim_source_path = paths[i];
2209 if (is_root_wim_path(paths[i])) {
2210 cmds[i].fs_dest_path = dest_dir;
2212 size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2213 cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2214 if (!cmds[i].fs_dest_path) {
2215 free_extract_commands(cmds, num_cmds, dest_dir);
2218 tsprintf(cmds[i].fs_dest_path, T("%"TS"/%"TS), dest_dir,
2219 tbasename(paths[i]));
2222 *num_cmds_ret = num_cmds;
2226 /* Extract files or directories from a WIM image */
2228 imagex_extract(int argc, tchar **argv)
2231 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2235 const tchar *wimfile;
2236 const tchar *image_num_or_name;
2237 tchar *dest_dir = T(".");
2238 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL | WIMLIB_EXTRACT_FLAG_NORPFIX;
2240 const tchar *swm_glob = NULL;
2241 WIMStruct **additional_swms = NULL;
2242 unsigned num_additional_swms = 0;
2244 struct wimlib_extract_command *cmds;
2247 for_opt(c, extract_options) {
2249 case IMAGEX_CHECK_OPTION:
2250 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2252 case IMAGEX_VERBOSE_OPTION:
2253 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
2255 case IMAGEX_REF_OPTION:
2258 case IMAGEX_UNIX_DATA_OPTION:
2259 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2261 case IMAGEX_NO_ACLS_OPTION:
2262 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2264 case IMAGEX_STRICT_ACLS_OPTION:
2265 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2267 case IMAGEX_DEST_DIR_OPTION:
2270 case IMAGEX_TO_STDOUT_OPTION:
2271 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2272 imagex_be_quiet = true;
2274 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2275 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2276 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2289 image_num_or_name = argv[1];
2294 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2301 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2305 image = wimlib_resolve_image(wim, image_num_or_name);
2306 ret = verify_image_exists_and_is_single(image,
2310 goto out_wimlib_free;
2313 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2315 &num_additional_swms);
2317 goto out_wimlib_free;
2321 win32_acquire_restore_privileges();
2324 ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
2325 additional_swms, num_additional_swms,
2326 imagex_progress_func);
2328 if (!imagex_be_quiet)
2329 tprintf(T("Done extracting files.\n"));
2330 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2331 tfprintf(stderr, T("Note: You can use `"IMAGEX_PROGNAME" dir' to see what "
2332 "files and directories\n"
2333 " are in the WIM image.\n"));
2336 win32_release_restore_privileges();
2338 if (additional_swms) {
2339 for (unsigned i = 0; i < num_additional_swms; i++)
2340 wimlib_free(additional_swms[i]);
2341 free(additional_swms);
2346 free_extract_commands(cmds, num_cmds, dest_dir);
2355 /* Prints information about a WIM file; also can mark an image as bootable,
2356 * change the name of an image, or change the description of an image. */
2358 imagex_info(int argc, tchar **argv)
2363 bool header = false;
2364 bool lookup_table = false;
2366 bool metadata = false;
2367 bool short_header = true;
2368 const tchar *xml_out_file = NULL;
2369 const tchar *wimfile;
2370 const tchar *image_num_or_name = T("all");
2371 const tchar *new_name = NULL;
2372 const tchar *new_desc = NULL;
2377 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2382 for_opt(c, info_options) {
2384 case IMAGEX_BOOT_OPTION:
2387 case IMAGEX_CHECK_OPTION:
2390 case IMAGEX_HEADER_OPTION:
2392 short_header = false;
2394 case IMAGEX_LOOKUP_TABLE_OPTION:
2395 lookup_table = true;
2396 short_header = false;
2398 case IMAGEX_XML_OPTION:
2400 short_header = false;
2402 case IMAGEX_EXTRACT_XML_OPTION:
2403 xml_out_file = optarg;
2404 short_header = false;
2406 case IMAGEX_METADATA_OPTION:
2408 short_header = false;
2418 if (argc == 0 || argc > 4) {
2424 image_num_or_name = argv[1];
2434 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2436 ret = wimlib_open_wim(wimfile, open_flags, &w,
2437 imagex_progress_func);
2441 part_number = wimlib_get_part_number(w, &total_parts);
2443 image = wimlib_resolve_image(w, image_num_or_name);
2444 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
2445 imagex_error(T("The image \"%"TS"\" does not exist"),
2448 imagex_error(T("If you would like to set the boot "
2449 "index to 0, specify image \"0\" with "
2450 "the --boot flag."));
2452 ret = WIMLIB_ERR_INVALID_IMAGE;
2456 num_images = wimlib_get_num_images(w);
2458 if (num_images == 0) {
2460 imagex_error(T("--boot is meaningless on a WIM with no "
2462 ret = WIMLIB_ERR_INVALID_IMAGE;
2467 if (image == WIMLIB_ALL_IMAGES && num_images > 1) {
2469 imagex_error(T("Cannot specify the --boot flag "
2470 "without specifying a specific "
2471 "image in a multi-image WIM"));
2472 ret = WIMLIB_ERR_INVALID_IMAGE;
2476 imagex_error(T("Cannot specify the NEW_NAME "
2477 "without specifying a specific "
2478 "image in a multi-image WIM"));
2479 ret = WIMLIB_ERR_INVALID_IMAGE;
2484 /* Operations that print information are separated from operations that
2485 * recreate the WIM file. */
2486 if (!new_name && !boot) {
2488 /* Read-only operations */
2490 if (image == WIMLIB_NO_IMAGE) {
2491 imagex_error(T("\"%"TS"\" is not a valid image"),
2493 ret = WIMLIB_ERR_INVALID_IMAGE;
2497 if (image == WIMLIB_ALL_IMAGES && short_header)
2498 wimlib_print_wim_information(w);
2501 wimlib_print_header(w);
2504 if (total_parts != 1) {
2505 tprintf(T("Warning: Only showing the lookup table "
2506 "for part %d of a %d-part WIM.\n"),
2507 part_number, total_parts);
2509 wimlib_print_lookup_table(w);
2513 ret = wimlib_extract_xml_data(w, stdout);
2519 fp = tfopen(xml_out_file, T("wb"));
2521 imagex_error_with_errno(T("Failed to open the "
2522 "file \"%"TS"\" for "
2528 ret = wimlib_extract_xml_data(w, fp);
2529 if (fclose(fp) != 0) {
2530 imagex_error(T("Failed to close the file "
2541 wimlib_print_available_images(w, image);
2544 ret = wimlib_print_metadata(w, image);
2550 /* Modification operations */
2551 if (total_parts != 1) {
2552 imagex_error(T("Modifying a split WIM is not supported."));
2556 if (image == WIMLIB_ALL_IMAGES)
2559 if (image == WIMLIB_NO_IMAGE && new_name) {
2560 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
2561 "when using image 0"), new_name);
2567 if (image == wimlib_get_boot_idx(w)) {
2568 tprintf(T("Image %d is already marked as "
2569 "bootable.\n"), image);
2572 tprintf(T("Marking image %d as bootable.\n"),
2574 wimlib_set_boot_idx(w, image);
2578 if (!tstrcmp(wimlib_get_image_name(w, image), new_name))
2580 tprintf(T("Image %d is already named \"%"TS"\".\n"),
2584 tprintf(T("Changing the name of image %d to "
2585 "\"%"TS"\".\n"), image, new_name);
2586 ret = wimlib_set_image_name(w, image, new_name);
2592 const tchar *old_desc;
2593 old_desc = wimlib_get_image_description(w, image);
2594 if (old_desc && !tstrcmp(old_desc, new_desc)) {
2595 tprintf(T("The description of image %d is already "
2596 "\"%"TS"\".\n"), image, new_desc);
2599 tprintf(T("Changing the description of image %d "
2600 "to \"%"TS"\".\n"), image, new_desc);
2601 ret = wimlib_set_image_descripton(w, image,
2608 /* Only call wimlib_overwrite() if something actually needs to
2610 if (boot || new_name || new_desc ||
2611 (check && !wimlib_has_integrity_table(w)))
2615 ret = file_writable(wimfile);
2620 write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2624 ret = wimlib_overwrite(w, write_flags, 1,
2625 imagex_progress_func);
2626 if (ret == WIMLIB_ERR_REOPEN)
2629 tprintf(T("The file \"%"TS"\" was not modified because nothing "
2630 "needed to be done.\n"), wimfile);
2639 /* Join split WIMs into one part WIM */
2641 imagex_join(int argc, tchar **argv)
2644 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2645 int wim_write_flags = 0;
2646 const tchar *output_path;
2648 for_opt(c, join_options) {
2650 case IMAGEX_CHECK_OPTION:
2651 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2652 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2662 imagex_error(T("Must specify one or more split WIM (.swm) "
2666 output_path = argv[0];
2667 return wimlib_join((const tchar * const *)++argv,
2672 imagex_progress_func);
2678 /* Mounts an image using a FUSE mount. */
2680 imagex_mount_rw_or_ro(int argc, tchar **argv)
2683 int mount_flags = 0;
2684 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2685 const tchar *wimfile;
2691 const tchar *swm_glob = NULL;
2692 WIMStruct **additional_swms = NULL;
2693 unsigned num_additional_swms = 0;
2694 const tchar *staging_dir = NULL;
2696 if (!tstrcmp(argv[0], T("mountrw")))
2697 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
2699 for_opt(c, mount_options) {
2701 case IMAGEX_ALLOW_OTHER_OPTION:
2702 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
2704 case IMAGEX_CHECK_OPTION:
2705 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2707 case IMAGEX_DEBUG_OPTION:
2708 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
2710 case IMAGEX_STREAMS_INTERFACE_OPTION:
2711 if (!tstrcasecmp(optarg, T("none")))
2712 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
2713 else if (!tstrcasecmp(optarg, T("xattr")))
2714 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2715 else if (!tstrcasecmp(optarg, T("windows")))
2716 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
2718 imagex_error(T("Unknown stream interface \"%"TS"\""),
2723 case IMAGEX_REF_OPTION:
2726 case IMAGEX_STAGING_DIR_OPTION:
2727 staging_dir = optarg;
2729 case IMAGEX_UNIX_DATA_OPTION:
2730 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
2738 if (argc != 2 && argc != 3)
2743 ret = wimlib_open_wim(wimfile, open_flags, &w,
2744 imagex_progress_func);
2749 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2751 &num_additional_swms);
2758 num_images = wimlib_get_num_images(w);
2759 if (num_images != 1) {
2760 imagex_error(T("\"%"TS"\" contains %d images; Please "
2761 "select one."), wimfile, num_images);
2762 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2769 image = wimlib_resolve_image(w, argv[1]);
2771 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
2776 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2777 ret = file_writable(wimfile);
2782 ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
2783 num_additional_swms, staging_dir);
2785 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
2787 image, wimfile, dir);
2792 if (additional_swms) {
2793 for (unsigned i = 0; i < num_additional_swms; i++)
2794 wimlib_free(additional_swms[i]);
2795 free(additional_swms);
2799 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2804 /* Rebuild a WIM file */
2806 imagex_optimize(int argc, tchar **argv)
2810 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
2813 const tchar *wimfile;
2816 unsigned num_threads = 0;
2818 for_opt(c, optimize_options) {
2820 case IMAGEX_CHECK_OPTION:
2821 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2822 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2824 case IMAGEX_RECOMPRESS_OPTION:
2825 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
2827 case IMAGEX_THREADS_OPTION:
2828 num_threads = parse_num_threads(optarg);
2829 if (num_threads == UINT_MAX)
2847 ret = file_writable(wimfile);
2851 ret = wimlib_open_wim(wimfile, open_flags, &w,
2852 imagex_progress_func);
2856 old_size = file_get_size(argv[0]);
2857 tprintf(T("\"%"TS"\" original size: "), wimfile);
2859 tfputs(T("Unknown\n"), stdout);
2861 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
2863 ret = wimlib_overwrite(w, write_flags, num_threads,
2864 imagex_progress_func);
2867 new_size = file_get_size(argv[0]);
2868 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
2870 tfputs(T("Unknown\n"), stdout);
2872 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
2874 tfputs(T("Space saved: "), stdout);
2875 if (new_size != -1 && old_size != -1) {
2876 tprintf(T("%lld KiB\n"),
2877 ((long long)old_size - (long long)new_size) >> 10);
2879 tfputs(T("Unknown\n"), stdout);
2887 /* Split a WIM into a spanned set */
2889 imagex_split(int argc, tchar **argv)
2892 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2893 int write_flags = 0;
2894 unsigned long part_size;
2899 for_opt(c, split_options) {
2901 case IMAGEX_CHECK_OPTION:
2902 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2903 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2917 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
2918 if (tmp == argv[2] || *tmp) {
2919 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
2920 imagex_error(T("The part size must be an integer or "
2921 "floating-point number of megabytes."));
2924 ret = wimlib_open_wim(argv[0], open_flags, &w, imagex_progress_func);
2927 ret = wimlib_split(w, argv[1], part_size, write_flags, imagex_progress_func);
2932 /* Unmounts a mounted WIM image. */
2934 imagex_unmount(int argc, tchar **argv)
2937 int unmount_flags = 0;
2940 for_opt(c, unmount_options) {
2942 case IMAGEX_COMMIT_OPTION:
2943 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
2945 case IMAGEX_CHECK_OPTION:
2946 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
2948 case IMAGEX_REBUILD_OPTION:
2949 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
2951 case IMAGEX_LAZY_OPTION:
2952 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
2966 ret = wimlib_unmount_image(argv[0], unmount_flags,
2967 imagex_progress_func);
2969 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
2974 * Add, delete, or rename files in a WIM image.
2977 imagex_update(int argc, tchar **argv)
2979 const tchar *wimfile;
2980 const tchar *image_num_or_name;
2985 int write_flags = 0;
2986 int update_flags = 0;
2987 int default_add_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
2988 int default_delete_flags = 0;
2989 unsigned num_threads = 0;
2991 tchar *cmd_file_contents = NULL;
2992 size_t cmd_file_nchars;
2993 struct wimlib_update_command *cmds;
2996 tchar *command_str = NULL;
2998 const tchar *config_file = NULL;
3000 struct wimlib_capture_config *config = NULL;
3002 for_opt(c, update_options) {
3004 /* Generic or write options */
3005 case IMAGEX_THREADS_OPTION:
3006 num_threads = parse_num_threads(optarg);
3007 if (num_threads == UINT_MAX) {
3012 case IMAGEX_CHECK_OPTION:
3013 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3014 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3016 case IMAGEX_REBUILD_OPTION:
3017 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3019 case IMAGEX_COMMAND_OPTION:
3021 imagex_error(T("--command may only be specified "
3022 "one time. Please provide\n"
3023 " the update commands "
3024 "on standard input instead."));
3028 command_str = tstrdup(optarg);
3030 imagex_error(T("Out of memory!"));
3035 /* Default delete options */
3036 case IMAGEX_FORCE_OPTION:
3037 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3039 case IMAGEX_RECURSIVE_OPTION:
3040 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3043 /* Global add option */
3044 case IMAGEX_CONFIG_OPTION:
3045 config_file = optarg;
3048 /* Default add options */
3049 case IMAGEX_VERBOSE_OPTION:
3050 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
3052 case IMAGEX_DEREFERENCE_OPTION:
3053 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
3055 case IMAGEX_UNIX_DATA_OPTION:
3056 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
3058 case IMAGEX_NO_ACLS_OPTION:
3059 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
3061 case IMAGEX_STRICT_ACLS_OPTION:
3062 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
3071 if (argc < 1 || argc > 2)
3075 ret = file_writable(wimfile);
3079 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3084 image_num_or_name = argv[1];
3086 image_num_or_name = T("1");
3088 image = wimlib_resolve_image(wim, image_num_or_name);
3090 ret = verify_image_exists_and_is_single(image, image_num_or_name,
3093 goto out_wimlib_free;
3095 num_images = wimlib_get_num_images(wim);
3096 if (argc == 1 && num_images != 1) {
3097 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3098 wimfile, num_images);
3101 goto out_wimlib_free;
3104 /* Parse capture configuration file if specified */
3108 config_str = file_get_text_contents(config_file, &config_len);
3111 goto out_wimlib_free;
3114 config = alloca(sizeof(*config));
3115 ret = parse_capture_config(&config_str, config_len, config);
3117 goto out_free_config;
3119 config = &default_capture_config;
3122 /* Read update commands from standard input, or the command string if
3125 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3128 if (isatty(STDIN_FILENO)) {
3129 tputs(T("Reading update commands from standard input..."));
3130 recommend_man_page(T("update"));
3132 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3133 if (!cmd_file_contents) {
3135 goto out_free_config;
3138 /* Parse the update commands */
3139 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3144 goto out_free_cmd_file_contents;
3147 /* Set default flags and capture config on the update commands */
3148 for (size_t i = 0; i < num_cmds; i++) {
3149 switch (cmds[i].op) {
3150 case WIMLIB_UPDATE_OP_ADD:
3151 cmds[i].add.add_flags |= default_add_flags;
3152 cmds[i].add.config = config;
3154 case WIMLIB_UPDATE_OP_DELETE:
3155 cmds[i].delete.delete_flags |= default_delete_flags;
3163 win32_acquire_capture_privileges();
3166 /* Execute the update commands */
3167 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3168 imagex_progress_func);
3170 goto out_release_privs;
3172 /* Overwrite the updated WIM */
3173 ret = wimlib_overwrite(wim, write_flags, num_threads,
3174 imagex_progress_func);
3177 win32_release_capture_privileges();
3181 out_free_cmd_file_contents:
3182 free(cmd_file_contents);
3184 if (config != NULL && config != &default_capture_config) {
3185 free(config->exclusion_pats.pats);
3186 free(config->exclusion_exception_pats.pats);
3200 struct imagex_command {
3202 int (*func)(int , tchar **);
3207 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
3208 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
3210 static const struct imagex_command imagex_commands[] = {
3211 {T("append"), imagex_capture_or_append, APPEND},
3212 {T("apply"), imagex_apply, APPLY},
3213 {T("capture"), imagex_capture_or_append, CAPTURE},
3214 {T("delete"), imagex_delete, DELETE},
3215 {T("dir"), imagex_dir, DIR},
3216 {T("export"), imagex_export, EXPORT},
3217 {T("extract"), imagex_extract, EXTRACT},
3218 {T("info"), imagex_info, INFO},
3219 {T("join"), imagex_join, JOIN},
3220 {T("mount"), imagex_mount_rw_or_ro, MOUNT},
3221 {T("mountrw"), imagex_mount_rw_or_ro, MOUNTRW},
3222 {T("optimize"),imagex_optimize, OPTIMIZE},
3223 {T("split"), imagex_split, SPLIT},
3224 {T("unmount"), imagex_unmount, UNMOUNT},
3225 {T("update"), imagex_update, UPDATE},
3231 static const tchar *s =
3233 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
3234 "Copyright (C) 2012, 2013 Eric Biggers\n"
3235 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
3236 "This is free software: you are free to change and redistribute it.\n"
3237 "There is NO WARRANTY, to the extent permitted by law.\n"
3239 "Report bugs to "PACKAGE_BUGREPORT".\n"
3246 help_or_version(int argc, tchar **argv)
3250 const struct imagex_command *cmd;
3252 for (i = 1; i < argc; i++) {
3260 if (!tstrcmp(p, T("help"))) {
3261 for_imagex_command(cmd) {
3262 if (!tstrcmp(cmd->name, argv[1])) {
3270 if (!tstrcmp(p, T("version"))) {
3281 const struct imagex_command *cmd;
3282 tprintf(T("Usage:\n%"TS), usage_strings[cmd_type]);
3283 for_imagex_command(cmd) {
3284 if (cmd->cmd == cmd_type) {
3285 tputc(T('\n'), stdout);
3286 recommend_man_page(cmd->name);
3294 tfputs(T("Usage:\n"), stdout);
3295 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
3296 tprintf(T(" %"TS), usage_strings[i]);
3297 static const tchar *extra =
3299 " "IMAGEX_PROGNAME" --help\n"
3300 " "IMAGEX_PROGNAME" --version\n"
3302 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
3305 tfputs(extra, stdout);
3306 recommend_man_page(T(""));
3309 /* Entry point for wimlib's ImageX implementation. On UNIX the command
3310 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
3311 * something else), while an Windows the command arguments will be UTF-16LE
3312 * encoded 'wchar_t' strings. */
3315 wmain(int argc, wchar_t **argv, wchar_t **envp)
3317 main(int argc, char **argv)
3320 const struct imagex_command *cmd;
3325 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
3326 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
3330 setlocale(LC_ALL, "");
3331 codeset = nl_langinfo(CODESET);
3332 if (!strstr(codeset, "UTF-8") &&
3333 !strstr(codeset, "UTF8") &&
3334 !strstr(codeset, "utf-8") &&
3335 !strstr(codeset, "utf8"))
3338 "WARNING: Running "IMAGEX_PROGNAME" in a UTF-8 locale is recommended!\n"
3339 " Maybe try: `export LANG=en_US.UTF-8'?\n"
3340 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
3341 " to any value to force wimlib to use UTF-8.\n",
3346 #endif /* !__WIN32__ */
3349 imagex_error(T("No command specified"));
3355 /* Handle --help and --version for all commands. Note that this will
3356 * not return if either of these arguments are present. */
3357 help_or_version(argc, argv);
3361 /* The user may like to see more informative error messages. */
3362 wimlib_set_print_errors(true);
3364 /* Do any initializations that the library needs */
3365 ret = wimlib_global_init(init_flags);
3367 goto out_check_status;
3369 /* Search for the function to handle the ImageX subcommand. */
3370 for_imagex_command(cmd) {
3371 if (!tstrcmp(cmd->name, *argv)) {
3372 ret = cmd->func(argc, argv);
3373 goto out_check_write_error;
3377 imagex_error(T("Unrecognized command: `%"TS"'"), argv[0]);
3381 out_check_write_error:
3382 /* For 'wimlib-imagex info' and 'wimlib-imagex dir', data printed to
3383 * standard output is part of the program's actual behavior and not just
3384 * for informational purposes, so we should set a failure exit status if
3385 * there was a write error. */
3386 if (cmd == &imagex_commands[INFO] || cmd == &imagex_commands[DIR]) {
3387 if (ferror(stdout) || fclose(stdout)) {
3388 imagex_error_with_errno(T("error writing to standard output"));
3394 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
3395 * outside of the wimlib library code. 0 indicates success. > 0
3396 * indicates a wimlib error code from which an error message can be
3399 imagex_error(T("Exiting with error code %d:\n"
3401 wimlib_get_error_string(ret));
3402 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
3403 imagex_error_with_errno(T("errno"));
3406 /* Make the library free any resources it's holding (not strictly
3407 * necessary because the process is ending anyway). */
3408 wimlib_global_cleanup();