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"
110 IMAGEX_PROGNAME" capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
111 " [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
112 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
113 " [--config=FILE] [--threads=NUM_THREADS] [--unix-data]\n"
114 " [--source-list] [--no-acls] [--strict-acls]\n"
115 " [--rpfix] [--norpfix]\n"
119 IMAGEX_PROGNAME" delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check] [--soft]\n"
123 IMAGEX_PROGNAME" dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n"
127 IMAGEX_PROGNAME" export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
128 " DEST_WIMFILE [DEST_IMAGE_NAME] [DEST_IMAGE_DESCRIPTION]\n"
129 " [--boot] [--check] [--compress=TYPE] [--ref=\"GLOB\"]\n"
130 " [--threads=NUM_THREADS] [--rebuild]\n"
134 IMAGEX_PROGNAME" extract WIMFILE (IMAGE_NUM | IMAGE_NAME) [PATH...]\n"
135 " [--check] [--ref=\"GLOB\"] [--verbose] [--unix-data]\n"
136 " [--no-acls] [--strict-acls] [--to-stdout] [--dest-dir=DIR]\n"
140 IMAGEX_PROGNAME" info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
141 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
142 " [--xml] [--extract-xml FILE] [--metadata]\n"
146 IMAGEX_PROGNAME" join [--check] WIMFILE SPLIT_WIM...\n"
150 IMAGEX_PROGNAME" mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
151 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
152 " [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n"
156 IMAGEX_PROGNAME" mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
157 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
158 " [--staging-dir=DIR] [--unix-data] [--allow-other]\n"
162 IMAGEX_PROGNAME" optimize WIMFILE [--check] [--recompress]\n"
163 " [--threads=NUM_THREADS]\n"
167 IMAGEX_PROGNAME" split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n"
171 IMAGEX_PROGNAME" unmount DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
175 IMAGEX_PROGNAME" update WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
176 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
177 " [DEFAULT_DELETE_OPTIONS] < CMDFILE\n"
183 recommend_man_page(const tchar *cmd_name)
185 const tchar *format_str;
187 format_str = T("See "IMAGEX_PROGNAME"%"TS"%"TS".pdf in the "
188 "doc directory for more details.\n");
190 format_str = T("Try `man "IMAGEX_PROGNAME"%"TS"%"TS"' "
191 "for more details.\n");
193 tprintf(format_str, *cmd_name ? T("-") : T(""), cmd_name);
197 IMAGEX_ALLOW_OTHER_OPTION,
200 IMAGEX_COMMIT_OPTION,
201 IMAGEX_COMPRESS_OPTION,
202 IMAGEX_CONFIG_OPTION,
204 IMAGEX_DEREFERENCE_OPTION,
205 IMAGEX_DEST_DIR_OPTION,
206 IMAGEX_EXTRACT_XML_OPTION,
209 IMAGEX_HARDLINK_OPTION,
210 IMAGEX_HEADER_OPTION,
212 IMAGEX_LOOKUP_TABLE_OPTION,
213 IMAGEX_METADATA_OPTION,
214 IMAGEX_NORPFIX_OPTION,
215 IMAGEX_NO_ACLS_OPTION,
216 IMAGEX_REBUILD_OPTION,
217 IMAGEX_RECOMPRESS_OPTION,
218 IMAGEX_RECURSIVE_OPTION,
222 IMAGEX_SOURCE_LIST_OPTION,
223 IMAGEX_STAGING_DIR_OPTION,
224 IMAGEX_STREAMS_INTERFACE_OPTION,
225 IMAGEX_STRICT_ACLS_OPTION,
226 IMAGEX_SYMLINK_OPTION,
227 IMAGEX_THREADS_OPTION,
228 IMAGEX_TO_STDOUT_OPTION,
229 IMAGEX_UNIX_DATA_OPTION,
230 IMAGEX_VERBOSE_OPTION,
234 static const struct option apply_options[] = {
235 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
236 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
237 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
238 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
239 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
240 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
241 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
242 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
243 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
244 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
245 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
248 static const struct option capture_or_append_options[] = {
249 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
250 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
251 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
252 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
253 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
254 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
255 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
256 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
257 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
258 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
259 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
260 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
261 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
262 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
263 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
264 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
267 static const struct option delete_options[] = {
268 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
269 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
273 static const struct option export_options[] = {
274 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
275 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
276 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
277 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
278 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
279 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
283 static const struct option extract_options[] = {
284 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
285 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
286 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
287 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
288 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
289 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
290 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
291 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
292 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
296 static const struct option info_options[] = {
297 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
298 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
299 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
300 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
301 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
302 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
303 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
307 static const struct option join_options[] = {
308 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
312 static const struct option mount_options[] = {
313 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
314 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
315 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
316 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
317 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
318 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
319 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
323 static const struct option optimize_options[] = {
324 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
325 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
326 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
330 static const struct option split_options[] = {
331 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
335 static const struct option unmount_options[] = {
336 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
337 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
338 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
339 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
343 static const struct option update_options[] = {
344 /* Careful: some of the options here set the defaults for update
345 * commands, but the flags given to an actual update command (and not to
346 * `imagex update' itself are also handled in
347 * update_command_add_option(). */
348 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
349 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
350 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
352 /* Default delete options */
353 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
354 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
356 /* Global add option */
357 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
359 /* Default add options */
360 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
361 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
362 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
363 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
364 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
365 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
369 /* Print formatted error message to stderr. */
371 imagex_error(const tchar *format, ...)
374 va_start(va, format);
375 tfputs(T("ERROR: "), stderr);
376 tvfprintf(stderr, format, va);
377 tputc(T('\n'), stderr);
381 /* Print formatted error message to stderr. */
383 imagex_error_with_errno(const tchar *format, ...)
385 int errno_save = errno;
387 va_start(va, format);
388 tfputs(T("ERROR: "), stderr);
389 tvfprintf(stderr, format, va);
390 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
395 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
397 if (image == WIMLIB_NO_IMAGE) {
398 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
399 " Please specify a 1-based image index or "
401 " You may use `"IMAGEX_PROGNAME" info' to list the images "
402 "contained in a WIM."),
403 image_name, wim_name);
410 verify_image_is_single(int image)
412 if (image == WIMLIB_ALL_IMAGES) {
413 imagex_error(T("Cannot specify all images for this action!"));
420 verify_image_exists_and_is_single(int image, const tchar *image_name,
421 const tchar *wim_name)
424 ret = verify_image_exists(image, image_name, wim_name);
426 ret = verify_image_is_single(image);
430 /* Parse the argument to --compress */
432 get_compression_type(const tchar *optarg)
434 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
435 return WIMLIB_COMPRESSION_TYPE_LZX;
436 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
437 return WIMLIB_COMPRESSION_TYPE_XPRESS;
438 else if (!tstrcasecmp(optarg, T("none")))
439 return WIMLIB_COMPRESSION_TYPE_NONE;
441 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
442 "\"maximum\", \"fast\", or \"none\"."), optarg);
443 return WIMLIB_COMPRESSION_TYPE_INVALID;
447 /* Returns the size of a file given its name, or -1 if the file does not exist
448 * or its size cannot be determined. */
450 file_get_size(const tchar *filename)
453 if (tstat(filename, &st) == 0)
459 tchar pat_ntfs_log[] = T("/$ntfs.log");
460 tchar pat_hiberfil_sys[] = T("/hiberfil.sys");
461 tchar pat_pagefile_sys[] = T("/pagefile.sys");
462 tchar pat_system_volume_information[] = T("/System Volume Information");
463 tchar pat_recycler[] = T("/RECYCLER");
464 tchar pat_windows_csc[] = T("/Windows/CSC");
466 tchar *default_pats[] = {
470 pat_system_volume_information,
475 static struct wimlib_capture_config default_capture_config = {
477 .num_pats = sizeof(default_pats) / sizeof(*default_pats),
478 .pats = default_pats,
483 PARSE_STRING_SUCCESS = 0,
484 PARSE_STRING_FAILURE = 1,
485 PARSE_STRING_NONE = 2,
489 * Parses a string token from an array of characters.
491 * Tokens are either whitespace-delimited, or double or single-quoted.
493 * @line_p: Pointer to the pointer to the line of data. Will be updated
494 * to point past the string token iff the return value is
495 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
498 * @len_p: @len_p initially stores the length of the line of data, which may
499 * be 0, and it will be updated to the number of bytes remaining in
500 * the line iff the return value is PARSE_STRING_SUCCESS.
502 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
503 * parsed string token will be returned here.
505 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
506 * PARSE_STRING_FAILURE if the data was invalid due to a missing
507 * closing quote; or PARSE_STRING_NONE if the line ended before the
508 * beginning of a string token was found.
511 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
514 tchar *line = *line_p;
518 /* Skip leading whitespace */
521 return PARSE_STRING_NONE;
522 if (!istspace(*line) && *line != T('\0'))
528 if (quote_char == T('"') || quote_char == T('\'')) {
533 line = tmemchr(line, quote_char, len);
535 imagex_error(T("Missing closing quote: %"TS), fn - 1);
536 return PARSE_STRING_FAILURE;
539 /* Unquoted string. Go until whitespace. Line is terminated
540 * by '\0', so no need to check 'len'. */
544 } while (!istspace(*line) && *line != T('\0'));
551 return PARSE_STRING_SUCCESS;
554 /* Parses a line of data (not an empty line or comment) in the source list file
555 * format. (See the man page for 'wimlib-imagex capture' for details on this
556 * format and the meaning.)
558 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
559 * len == 0. The data in @line will be modified by this function call.
561 * @len: Length of the line of data.
563 * @source: On success, the capture source and target described by the line is
564 * written into this destination. Note that it will contain pointers
565 * to data in the @line array.
567 * Returns true if the line was valid; false otherwise. */
569 parse_source_list_line(tchar *line, size_t len,
570 struct wimlib_capture_source *source)
574 ret = parse_string(&line, &len, &source->fs_source_path);
575 if (ret != PARSE_STRING_SUCCESS)
577 ret = parse_string(&line, &len, &source->wim_target_path);
578 if (ret == PARSE_STRING_NONE)
579 source->wim_target_path = source->fs_source_path;
580 return ret != PARSE_STRING_FAILURE;
583 /* Returns %true if the given line of length @len > 0 is a comment or empty line
584 * in the source list file format. */
586 is_comment_line(const tchar *line, size_t len)
591 if (!istspace(*line) && *line != T('\0'))
601 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
604 tchar *contents = *contents_p;
605 size_t nchars = *nchars_p;
608 for (i = 0; i < nchars; i++)
609 if (contents[i] == T('\n'))
612 /* Handle last line not terminated by a newline */
613 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
614 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
616 imagex_error(T("Out of memory!"));
619 contents[nchars] = T('\n');
620 *contents_p = contents;
628 /* Parses a file in the source list format. (See the man page for
629 * 'wimlib-imagex capture' for details on this format and the meaning.)
631 * @source_list_contents: Contents of the source list file. Note that this
632 * buffer will be modified to save memory allocations,
633 * and cannot be freed until the returned array of
634 * wimlib_capture_source's has also been freed.
636 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
639 * @nsources_ret: On success, the length of the returned array is
642 * Returns: An array of `struct wimlib_capture_source's that can be passed to
643 * the wimlib_add_image_multisource() function to specify how a WIM image is to
645 static struct wimlib_capture_source *
646 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
647 size_t *nsources_ret)
651 struct wimlib_capture_source *sources;
654 nlines = text_file_count_lines(source_list_contents_p,
655 &source_list_nchars);
659 /* Always allocate at least 1 slot, just in case the implementation of
660 * calloc() returns NULL if 0 bytes are requested. */
661 sources = calloc(nlines ?: 1, sizeof(*sources));
663 imagex_error(T("out of memory"));
666 p = *source_list_contents_p;
668 for (i = 0; i < nlines; i++) {
669 /* XXX: Could use rawmemchr() here instead, but it may not be
670 * available on all platforms. */
671 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
672 size_t len = endp - p + 1;
674 if (!is_comment_line(p, len)) {
675 if (!parse_source_list_line(p, len, &sources[j++])) {
688 enum capture_config_section {
689 CAPTURE_CONFIG_NO_SECTION,
690 CAPTURE_CONFIG_EXCLUSION_SECTION,
691 CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
692 CAPTURE_CONFIG_IGNORE_SECTION,
696 CAPTURE_CONFIG_INVALID_SECTION,
697 CAPTURE_CONFIG_CHANGED_SECTION,
698 CAPTURE_CONFIG_SAME_SECTION,
702 check_config_section(tchar *line, size_t len,
703 enum capture_config_section *cur_section)
705 while (istspace(*line))
709 return CAPTURE_CONFIG_SAME_SECTION;
712 tchar *endbrace = tstrrchr(line, T(']'));
714 return CAPTURE_CONFIG_SAME_SECTION;
716 if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
717 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
718 } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
719 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
720 } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
721 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
722 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
723 "of capture config file\n"),
725 } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
726 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
727 tfputs(T("WARNING: Ignoring [AlignmentList] section "
728 "of capture config file\n"),
731 imagex_error(T("Invalid capture config file section \"%"TS"\""),
733 return CAPTURE_CONFIG_INVALID_SECTION;
735 return CAPTURE_CONFIG_CHANGED_SECTION;
740 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
743 if (pat_list->num_pats == pat_list->num_allocated_pats) {
745 size_t num_allocated_pats = pat_list->num_pats + 8;
747 pats = realloc(pat_list->pats,
748 num_allocated_pats * sizeof(pat_list->pats[0]));
750 imagex_error(T("Out of memory!"));
753 pat_list->pats = pats;
754 pat_list->num_allocated_pats = num_allocated_pats;
756 pat_list->pats[pat_list->num_pats++] = pat;
761 parse_capture_config_line(tchar *line, size_t len,
762 enum capture_config_section *cur_section,
763 struct wimlib_capture_config *config)
768 ret = check_config_section(line, len, cur_section);
769 if (ret == CAPTURE_CONFIG_INVALID_SECTION)
771 if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
774 switch (*cur_section) {
775 case CAPTURE_CONFIG_NO_SECTION:
776 imagex_error(T("Line \"%"TS"\" is not in a section "
777 "(such as [ExclusionList]"), line);
779 case CAPTURE_CONFIG_EXCLUSION_SECTION:
780 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
782 return pattern_list_add_pattern(&config->exclusion_pats,
784 case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
785 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
787 return pattern_list_add_pattern(&config->exclusion_exception_pats,
789 case CAPTURE_CONFIG_IGNORE_SECTION:
796 parse_capture_config(tchar **contents_p, size_t nchars,
797 struct wimlib_capture_config *config)
802 enum capture_config_section cur_section;
804 memset(config, 0, sizeof(*config));
806 nlines = text_file_count_lines(contents_p, &nchars);
810 cur_section = CAPTURE_CONFIG_NO_SECTION;
812 for (i = 0; i < nlines; i++) {
813 tchar *endp = tmemchr(p, T('\n'), nchars);
814 size_t len = endp - p + 1;
816 if (!is_comment_line(p, len))
817 if (!parse_capture_config_line(p, len, &cur_section, config))
825 /* Reads the contents of a file into memory. */
827 file_get_contents(const tchar *filename, size_t *len_ret)
834 if (tstat(filename, &stbuf) != 0) {
835 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
840 fp = tfopen(filename, T("rb"));
842 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
848 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
849 "contents of file \"%"TS"\""), len, filename);
852 if (fread(buf, 1, len, fp) != len) {
853 imagex_error_with_errno(T("Failed to read %zu bytes from the "
854 "file \"%"TS"\""), len, filename);
868 /* Read standard input until EOF and return the full contents in a malloc()ed
869 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
872 stdin_get_contents(size_t *len_ret)
874 /* stdin can, of course, be a pipe or other non-seekable file, so the
875 * total length of the data cannot be pre-determined */
877 size_t newlen = 1024;
881 char *p = realloc(buf, newlen);
882 size_t bytes_read, bytes_to_read;
884 imagex_error(T("out of memory while reading stdin"));
888 bytes_to_read = newlen - pos;
889 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
891 if (bytes_read != bytes_to_read) {
896 imagex_error_with_errno(T("error reading stdin"));
910 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
913 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
915 *num_tchars_ret = num_bytes;
917 #else /* !__WIN32__ */
918 /* On Windows, translate the text to UTF-16LE */
922 if (num_bytes >= 2 &&
923 ((text[0] == 0xff && text[1] == 0xfe) ||
924 (text[0] <= 0x7f && text[1] == 0x00)))
926 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
927 * with something that looks like an ASCII character encoded as
928 * a UTF-16LE code unit. Assume the file is encoded as
929 * UTF-16LE. This is not a 100% reliable check. */
930 num_wchars = num_bytes / 2;
931 text_wstr = (wchar_t*)text;
933 /* File does not look like UTF-16LE. Assume it is encoded in
934 * the current Windows code page. I think these are always
935 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
936 * should work as expected. */
937 text_wstr = win32_mbs_to_wcs(text,
942 *num_tchars_ret = num_wchars;
944 #endif /* __WIN32__ */
948 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
953 contents = file_get_contents(filename, &num_bytes);
956 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
960 stdin_get_text_contents(size_t *num_tchars_ret)
965 contents = stdin_get_contents(&num_bytes);
968 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
971 /* Return 0 if a path names a file to which the current user has write access;
972 * -1 otherwise (and print an error message). */
974 file_writable(const tchar *path)
977 ret = taccess(path, W_OK);
979 imagex_error_with_errno(T("Can't modify \"%"TS"\""), path);
983 #define TO_PERCENT(numerator, denominator) \
984 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
986 /* Given an enumerated value for WIM compression type, return a descriptive
989 get_data_type(int ctype)
992 case WIMLIB_COMPRESSION_TYPE_NONE:
993 return T("uncompressed");
994 case WIMLIB_COMPRESSION_TYPE_LZX:
995 return T("LZX-compressed");
996 case WIMLIB_COMPRESSION_TYPE_XPRESS:
997 return T("XPRESS-compressed");
1002 /* Progress callback function passed to various wimlib functions. */
1004 imagex_progress_func(enum wimlib_progress_msg msg,
1005 const union wimlib_progress_info *info)
1007 unsigned percent_done;
1008 if (imagex_be_quiet)
1011 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1012 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1013 info->write_streams.total_bytes);
1014 if (info->write_streams.completed_streams == 0) {
1015 const tchar *data_type;
1017 data_type = get_data_type(info->write_streams.compression_type);
1018 tprintf(T("Writing %"TS" data using %u thread%"TS"\n"),
1019 data_type, info->write_streams.num_threads,
1020 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1022 tprintf(T("\r%"PRIu64" MiB of %"PRIu64" MiB (uncompressed) "
1023 "written (%u%% done)"),
1024 info->write_streams.completed_bytes >> 20,
1025 info->write_streams.total_bytes >> 20,
1027 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1030 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1031 tprintf(T("Scanning \"%"TS"\""), info->scan.source);
1032 if (*info->scan.wim_target_path) {
1033 tprintf(T(" (loading as WIM path: \"/%"TS"\")...\n"),
1034 info->scan.wim_target_path);
1036 tprintf(T(" (loading as root of WIM image)...\n"));
1039 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1040 if (info->scan.excluded)
1041 tprintf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1043 tprintf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
1045 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
1047 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1048 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1049 info->integrity.total_bytes);
1050 tprintf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" MiB "
1051 "of %"PRIu64" MiB (%u%%) done"),
1052 info->integrity.filename,
1053 info->integrity.completed_bytes >> 20,
1054 info->integrity.total_bytes >> 20,
1056 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1059 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1060 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1061 info->integrity.total_bytes);
1062 tprintf(T("\rCalculating integrity table for WIM: %"PRIu64" MiB "
1063 "of %"PRIu64" MiB (%u%%) done"),
1064 info->integrity.completed_bytes >> 20,
1065 info->integrity.total_bytes >> 20,
1067 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1070 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1071 tprintf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1072 "to %"TS" \"%"TS"\"\n"),
1073 info->extract.image,
1074 info->extract.image_name,
1075 info->extract.wimfile_name,
1076 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1077 T("NTFS volume") : T("directory")),
1078 info->extract.target);
1080 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1081 tprintf(T("Extracting \"%"TS"\" from image %d (\"%"TS"\") "
1082 "in \"%"TS"\" to \"%"TS"\"\n"),
1083 info->extract.extract_root_wim_source_path,
1084 info->extract.image,
1085 info->extract.image_name,
1086 info->extract.wimfile_name,
1087 info->extract.target);
1089 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
1090 /*tprintf(T("Applying directory structure to %"TS"\n"),*/
1091 /*info->extract.target);*/
1093 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1094 percent_done = TO_PERCENT(info->extract.completed_bytes,
1095 info->extract.total_bytes);
1096 tprintf(T("\rExtracting files: "
1097 "%"PRIu64" MiB of %"PRIu64" MiB (%u%%) done"),
1098 info->extract.completed_bytes >> 20,
1099 info->extract.total_bytes >> 20,
1101 if (info->extract.completed_bytes >= info->extract.total_bytes)
1104 case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
1105 tprintf(T("%"TS"\n"), info->extract.cur_path);
1107 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1108 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1109 tprintf(T("Setting timestamps on all extracted files...\n"));
1111 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1112 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1113 tprintf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1114 info->extract.target);
1117 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
1118 percent_done = TO_PERCENT(info->join.completed_bytes,
1119 info->join.total_bytes);
1120 tprintf(T("Writing resources from part %u of %u: "
1121 "%"PRIu64 " MiB of %"PRIu64" MiB (%u%%) written\n"),
1122 (info->join.completed_parts == info->join.total_parts) ?
1123 info->join.completed_parts : info->join.completed_parts + 1,
1124 info->join.total_parts,
1125 info->join.completed_bytes >> 20,
1126 info->join.total_bytes >> 20,
1129 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1130 percent_done = TO_PERCENT(info->split.completed_bytes,
1131 info->split.total_bytes);
1132 tprintf(T("Writing \"%"TS"\": %"PRIu64" MiB of "
1133 "%"PRIu64" MiB (%u%%) written\n"),
1134 info->split.part_name,
1135 info->split.completed_bytes >> 20,
1136 info->split.total_bytes >> 20,
1139 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1140 if (info->split.completed_bytes == info->split.total_bytes) {
1141 tprintf(T("Finished writing %u split WIM parts\n"),
1142 info->split.cur_part_number);
1152 /* Open all the split WIM parts that correspond to a file glob.
1154 * @first_part specifies the first part of the split WIM and it may be either
1155 * included or omitted from the glob. */
1157 open_swms_from_glob(const tchar *swm_glob,
1158 const tchar *first_part,
1160 WIMStruct ***additional_swms_ret,
1161 unsigned *num_additional_swms_ret)
1163 unsigned num_additional_swms = 0;
1164 WIMStruct **additional_swms = NULL;
1168 /* Warning: glob() is replaced in Windows native builds */
1169 ret = tglob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
1171 if (ret == GLOB_NOMATCH) {
1172 imagex_error(T("Found no files for glob \"%"TS"\""),
1175 imagex_error_with_errno(T("Failed to process glob \"%"TS"\""),
1181 num_additional_swms = globbuf.gl_pathc;
1182 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
1183 if (!additional_swms) {
1184 imagex_error(T("Out of memory"));
1188 unsigned offset = 0;
1189 for (unsigned i = 0; i < num_additional_swms; i++) {
1190 if (tstrcmp(globbuf.gl_pathv[i], first_part) == 0) {
1194 ret = wimlib_open_wim(globbuf.gl_pathv[i],
1195 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
1196 &additional_swms[i - offset],
1197 imagex_progress_func);
1199 goto out_close_swms;
1201 *additional_swms_ret = additional_swms;
1202 *num_additional_swms_ret = num_additional_swms - offset;
1206 for (unsigned i = 0; i < num_additional_swms; i++)
1207 wimlib_free(additional_swms[i]);
1208 free(additional_swms);
1217 parse_num_threads(const tchar *optarg)
1220 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1221 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1222 imagex_error(T("Number of threads must be a non-negative integer!"));
1230 * Parse an option passed to an update command.
1232 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1235 * @option: Text string for the option (beginning with --)
1237 * @cmd: `struct wimlib_update_command' that is being constructed for
1240 * Returns true if the option was recognized; false if not.
1243 update_command_add_option(int op, const tchar *option,
1244 struct wimlib_update_command *cmd)
1246 bool recognized = true;
1248 case WIMLIB_UPDATE_OP_ADD:
1249 if (!tstrcmp(option, T("--verbose")))
1250 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1251 else if (!tstrcmp(option, T("--unix-data")))
1252 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1253 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1254 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1255 else if (!tstrcmp(option, T("--strict-acls")))
1256 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1257 else if (!tstrcmp(option, T("--dereference")))
1258 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1262 case WIMLIB_UPDATE_OP_DELETE:
1263 if (!tstrcmp(option, T("--force")))
1264 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1265 else if (!tstrcmp(option, T("--recursive")))
1266 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1277 /* How many nonoption arguments each `imagex update' command expects */
1278 static const unsigned update_command_num_nonoptions[] = {
1279 [WIMLIB_UPDATE_OP_ADD] = 2,
1280 [WIMLIB_UPDATE_OP_DELETE] = 1,
1281 [WIMLIB_UPDATE_OP_RENAME] = 2,
1285 update_command_add_nonoption(int op, const tchar *nonoption,
1286 struct wimlib_update_command *cmd,
1287 unsigned num_nonoptions)
1290 case WIMLIB_UPDATE_OP_ADD:
1291 if (num_nonoptions == 0)
1292 cmd->add.fs_source_path = (tchar*)nonoption;
1294 cmd->add.wim_target_path = (tchar*)nonoption;
1296 case WIMLIB_UPDATE_OP_DELETE:
1297 cmd->delete.wim_path = (tchar*)nonoption;
1299 case WIMLIB_UPDATE_OP_RENAME:
1300 if (num_nonoptions == 0)
1301 cmd->rename.wim_source_path = (tchar*)nonoption;
1303 cmd->rename.wim_target_path = (tchar*)nonoption;
1309 * Parse a command passed on stdin to `imagex update'.
1311 * @line: Text of the command.
1312 * @len: Length of the line, including a null terminator
1315 * @command: A `struct wimlib_update_command' to fill in from the parsed
1318 * @line_number: Line number of the command, for diagnostics.
1320 * Returns true on success; returns false on parse error.
1323 parse_update_command(tchar *line, size_t len,
1324 struct wimlib_update_command *command,
1328 tchar *command_name;
1330 size_t num_nonoptions;
1332 /* Get the command name ("add", "delete", "rename") */
1333 ret = parse_string(&line, &len, &command_name);
1334 if (ret != PARSE_STRING_SUCCESS)
1337 if (!tstrcasecmp(command_name, T("add"))) {
1338 op = WIMLIB_UPDATE_OP_ADD;
1339 } else if (!tstrcasecmp(command_name, T("delete"))) {
1340 op = WIMLIB_UPDATE_OP_DELETE;
1341 } else if (!tstrcasecmp(command_name, T("rename"))) {
1342 op = WIMLIB_UPDATE_OP_RENAME;
1344 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1345 command_name, line_number);
1350 /* Parse additional options and non-options as needed */
1355 ret = parse_string(&line, &len, &next_string);
1356 if (ret == PARSE_STRING_NONE) /* End of line */
1358 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1360 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1362 if (!update_command_add_option(op, next_string, command))
1364 imagex_error(T("Unrecognized option \"%"TS"\" to "
1365 "update command \"%"TS"\" on line %zu"),
1366 next_string, command_name, line_number);
1372 if (num_nonoptions == update_command_num_nonoptions[op])
1374 imagex_error(T("Unexpected argument \"%"TS"\" in "
1375 "update command on line %zu\n"
1376 " (The \"%"TS"\" command only "
1377 "takes %u nonoption arguments!)\n"),
1378 next_string, line_number,
1379 command_name, num_nonoptions);
1382 update_command_add_nonoption(op, next_string,
1383 command, num_nonoptions);
1388 if (num_nonoptions != update_command_num_nonoptions[op]) {
1389 imagex_error(T("Not enough arguments to update command "
1390 "\"%"TS"\" on line %zu"), command_name, line_number);
1396 static struct wimlib_update_command *
1397 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1398 size_t *num_cmds_ret)
1402 struct wimlib_update_command *cmds;
1405 nlines = text_file_count_lines(cmd_file_contents_p,
1410 /* Always allocate at least 1 slot, just in case the implementation of
1411 * calloc() returns NULL if 0 bytes are requested. */
1412 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1414 imagex_error(T("out of memory"));
1417 p = *cmd_file_contents_p;
1419 for (i = 0; i < nlines; i++) {
1420 /* XXX: Could use rawmemchr() here instead, but it may not be
1421 * available on all platforms. */
1422 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1423 size_t len = endp - p + 1;
1425 if (!is_comment_line(p, len)) {
1426 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1437 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1438 * one image from a WIM file to a NTFS volume. */
1440 imagex_apply(int argc, tchar **argv)
1443 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1448 const tchar *wimfile;
1449 const tchar *target;
1450 const tchar *image_num_or_name;
1451 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1453 const tchar *swm_glob = NULL;
1454 WIMStruct **additional_swms = NULL;
1455 unsigned num_additional_swms = 0;
1457 for_opt(c, apply_options) {
1459 case IMAGEX_CHECK_OPTION:
1460 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1462 case IMAGEX_HARDLINK_OPTION:
1463 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1465 case IMAGEX_SYMLINK_OPTION:
1466 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1468 case IMAGEX_VERBOSE_OPTION:
1469 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
1471 case IMAGEX_REF_OPTION:
1474 case IMAGEX_UNIX_DATA_OPTION:
1475 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1477 case IMAGEX_NO_ACLS_OPTION:
1478 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1480 case IMAGEX_STRICT_ACLS_OPTION:
1481 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1483 case IMAGEX_NORPFIX_OPTION:
1484 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1486 case IMAGEX_RPFIX_OPTION:
1487 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1496 if (argc != 2 && argc != 3) {
1503 image_num_or_name = T("1");
1506 image_num_or_name = argv[1];
1510 ret = wimlib_open_wim(wimfile, open_flags, &w, imagex_progress_func);
1514 image = wimlib_resolve_image(w, image_num_or_name);
1515 ret = verify_image_exists(image, image_num_or_name, wimfile);
1519 num_images = wimlib_get_num_images(w);
1520 if (argc == 2 && num_images != 1) {
1521 imagex_error(T("\"%"TS"\" contains %d images; Please select one "
1522 "(or all)."), wimfile, num_images);
1529 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1531 &num_additional_swms);
1538 ret = tstat(target, &stbuf);
1540 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1541 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1543 if (errno != ENOENT) {
1544 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1552 win32_acquire_restore_privileges();
1554 ret = wimlib_extract_image(w, image, target, extract_flags,
1555 additional_swms, num_additional_swms,
1556 imagex_progress_func);
1558 tprintf(T("Done applying WIM image.\n"));
1560 win32_release_restore_privileges();
1564 if (additional_swms) {
1565 for (unsigned i = 0; i < num_additional_swms; i++)
1566 wimlib_free(additional_swms[i]);
1567 free(additional_swms);
1572 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1573 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1574 * the desired image. 'wimlib-imagex append': add a new image to an existing
1577 imagex_capture_or_append(int argc, tchar **argv)
1581 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
1582 int write_flags = 0;
1583 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
1584 const tchar *wimfile;
1587 const tchar *flags_element = NULL;
1591 int cmd = tstrcmp(argv[0], T("append")) ? CAPTURE : APPEND;
1592 unsigned num_threads = 0;
1595 size_t source_name_len;
1598 const tchar *config_file = NULL;
1600 struct wimlib_capture_config *config = NULL;
1602 bool source_list = false;
1603 size_t source_list_nchars;
1604 tchar *source_list_contents;
1605 bool capture_sources_malloced;
1606 struct wimlib_capture_source *capture_sources;
1609 for_opt(c, capture_or_append_options) {
1611 case IMAGEX_BOOT_OPTION:
1612 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1614 case IMAGEX_CHECK_OPTION:
1615 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1616 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1618 case IMAGEX_CONFIG_OPTION:
1619 config_file = optarg;
1621 case IMAGEX_COMPRESS_OPTION:
1622 compression_type = get_compression_type(optarg);
1623 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1626 case IMAGEX_FLAGS_OPTION:
1627 flags_element = optarg;
1629 case IMAGEX_DEREFERENCE_OPTION:
1630 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1632 case IMAGEX_VERBOSE_OPTION:
1633 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1635 case IMAGEX_THREADS_OPTION:
1636 num_threads = parse_num_threads(optarg);
1637 if (num_threads == UINT_MAX)
1640 case IMAGEX_REBUILD_OPTION:
1641 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1643 case IMAGEX_UNIX_DATA_OPTION:
1644 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1646 case IMAGEX_SOURCE_LIST_OPTION:
1649 case IMAGEX_NO_ACLS_OPTION:
1650 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1652 case IMAGEX_STRICT_ACLS_OPTION:
1653 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1655 case IMAGEX_RPFIX_OPTION:
1656 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1658 case IMAGEX_NORPFIX_OPTION:
1659 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1669 if (argc < 2 || argc > 4) {
1680 /* Set default name to SOURCE argument, omitting any directory
1681 * prefixes and trailing slashes. This requires making a copy
1683 source_name_len = tstrlen(source);
1684 source_copy = alloca((source_name_len + 1) * sizeof(tchar));
1685 name = tbasename(tstrcpy(source_copy, source));
1687 /* Image description defaults to NULL if not given. */
1688 desc = (argc >= 4) ? argv[3] : NULL;
1691 /* Set up capture sources in source list mode */
1692 if (source[0] == T('-') && source[1] == T('\0')) {
1693 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1695 source_list_contents = file_get_text_contents(source,
1696 &source_list_nchars);
1698 if (!source_list_contents)
1701 capture_sources = parse_source_list(&source_list_contents,
1704 if (!capture_sources) {
1706 goto out_free_source_list_contents;
1708 capture_sources_malloced = true;
1710 /* Set up capture source in non-source-list mode (could be
1711 * either "normal" mode or "NTFS mode"--- see the man page). */
1712 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1713 capture_sources[0].fs_source_path = source;
1714 capture_sources[0].wim_target_path = NULL;
1715 capture_sources[0].reserved = 0;
1717 capture_sources_malloced = false;
1718 source_list_contents = NULL;
1724 config_str = file_get_text_contents(config_file, &config_len);
1727 goto out_free_capture_sources;
1730 config = alloca(sizeof(*config));
1731 ret = parse_capture_config(&config_str, config_len, config);
1733 goto out_free_config;
1735 config = &default_capture_config;
1739 ret = wimlib_open_wim(wimfile, open_flags, &w,
1740 imagex_progress_func);
1742 ret = wimlib_create_new_wim(compression_type, &w);
1744 goto out_free_config;
1748 ret = tstat(source, &stbuf);
1750 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1751 tprintf(T("Capturing WIM image from NTFS "
1752 "filesystem on \"%"TS"\"\n"), source);
1753 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1756 if (errno != ENOENT) {
1757 imagex_error_with_errno(T("Failed to stat "
1758 "\"%"TS"\""), source);
1760 goto out_wimlib_free;
1765 win32_acquire_capture_privileges();
1768 ret = wimlib_add_image_multisource(w,
1774 imagex_progress_func);
1776 goto out_release_privs;
1777 cur_image = wimlib_get_num_images(w);
1779 ret = wimlib_set_image_descripton(w, cur_image, desc);
1781 goto out_release_privs;
1783 if (flags_element) {
1784 ret = wimlib_set_image_flags(w, cur_image, flags_element);
1786 goto out_release_privs;
1788 if (cmd == APPEND) {
1789 ret = wimlib_overwrite(w, write_flags, num_threads,
1790 imagex_progress_func);
1792 ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
1793 num_threads, imagex_progress_func);
1795 if (ret == WIMLIB_ERR_REOPEN)
1798 imagex_error(T("Failed to write the WIM file \"%"TS"\""),
1802 win32_release_capture_privileges();
1807 if (config != NULL && config != &default_capture_config) {
1808 free(config->exclusion_pats.pats);
1809 free(config->exclusion_exception_pats.pats);
1812 out_free_capture_sources:
1813 if (capture_sources_malloced)
1814 free(capture_sources);
1815 out_free_source_list_contents:
1816 free(source_list_contents);
1820 /* Remove image(s) from a WIM. */
1822 imagex_delete(int argc, tchar **argv)
1826 int write_flags = 0;
1827 const tchar *wimfile;
1828 const tchar *image_num_or_name;
1833 for_opt(c, delete_options) {
1835 case IMAGEX_CHECK_OPTION:
1836 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1837 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1839 case IMAGEX_SOFT_OPTION:
1840 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
1852 imagex_error(T("Must specify a WIM file"));
1854 imagex_error(T("Must specify an image"));
1859 image_num_or_name = argv[1];
1861 ret = file_writable(wimfile);
1865 ret = wimlib_open_wim(wimfile, open_flags, &w,
1866 imagex_progress_func);
1870 image = wimlib_resolve_image(w, image_num_or_name);
1872 ret = verify_image_exists(image, image_num_or_name, wimfile);
1876 ret = wimlib_delete_image(w, image);
1878 imagex_error(T("Failed to delete image from \"%"TS"\""), wimfile);
1882 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1883 if (ret == WIMLIB_ERR_REOPEN)
1886 imagex_error(T("Failed to write the file \"%"TS"\" with image "
1887 "deleted"), wimfile);
1894 /* Print the files contained in an image(s) in a WIM file. */
1896 imagex_dir(int argc, tchar **argv)
1898 const tchar *wimfile;
1905 imagex_error(T("Must specify a WIM file"));
1910 imagex_error(T("Too many arguments"));
1916 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w,
1917 imagex_progress_func);
1922 image = wimlib_resolve_image(w, argv[2]);
1923 ret = verify_image_exists(image, argv[2], wimfile);
1927 /* Image was not specified. If the WIM only contains one image,
1928 * choose that one; otherwise, print an error. */
1929 num_images = wimlib_get_num_images(w);
1930 if (num_images != 1) {
1931 imagex_error(T("\"%"TS"\" contains %d images; Please "
1932 "select one."), wimfile, num_images);
1940 ret = wimlib_print_files(w, image);
1946 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
1949 imagex_export(int argc, tchar **argv)
1953 int export_flags = 0;
1954 int write_flags = 0;
1955 int compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
1956 bool compression_type_specified = false;
1957 const tchar *src_wimfile;
1958 const tchar *src_image_num_or_name;
1959 const tchar *dest_wimfile;
1960 const tchar *dest_name;
1961 const tchar *dest_desc;
1962 WIMStruct *src_w = NULL;
1963 WIMStruct *dest_w = NULL;
1968 const tchar *swm_glob = NULL;
1969 WIMStruct **additional_swms = NULL;
1970 unsigned num_additional_swms = 0;
1971 unsigned num_threads = 0;
1973 for_opt(c, export_options) {
1975 case IMAGEX_BOOT_OPTION:
1976 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
1978 case IMAGEX_CHECK_OPTION:
1979 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1980 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1982 case IMAGEX_COMPRESS_OPTION:
1983 compression_type = get_compression_type(optarg);
1984 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1986 compression_type_specified = true;
1988 case IMAGEX_REF_OPTION:
1991 case IMAGEX_THREADS_OPTION:
1992 num_threads = parse_num_threads(optarg);
1993 if (num_threads == UINT_MAX)
1996 case IMAGEX_REBUILD_OPTION:
1997 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2006 if (argc < 3 || argc > 5) {
2010 src_wimfile = argv[0];
2011 src_image_num_or_name = argv[1];
2012 dest_wimfile = argv[2];
2013 dest_name = (argc >= 4) ? argv[3] : NULL;
2014 dest_desc = (argc >= 5) ? argv[4] : NULL;
2015 ret = wimlib_open_wim(src_wimfile,
2016 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w,
2017 imagex_progress_func);
2021 /* Determine if the destination is an existing file or not.
2022 * If so, we try to append the exported image(s) to it; otherwise, we
2023 * create a new WIM containing the exported image(s). */
2024 if (tstat(dest_wimfile, &stbuf) == 0) {
2028 /* Destination file exists. */
2030 if (!S_ISREG(stbuf.st_mode)) {
2031 imagex_error(T("\"%"TS"\" is not a regular file"),
2036 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w,
2037 imagex_progress_func);
2041 ret = file_writable(dest_wimfile);
2045 dest_ctype = wimlib_get_compression_type(dest_w);
2046 if (compression_type_specified
2047 && compression_type != dest_ctype)
2049 imagex_error(T("Cannot specify a compression type that is "
2050 "not the same as that used in the "
2051 "destination WIM"));
2057 /* dest_wimfile is not an existing file, so create a new WIM. */
2058 if (!compression_type_specified)
2059 compression_type = wimlib_get_compression_type(src_w);
2060 if (errno == ENOENT) {
2061 ret = wimlib_create_new_wim(compression_type, &dest_w);
2065 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2072 image = wimlib_resolve_image(src_w, src_image_num_or_name);
2073 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2078 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
2080 &num_additional_swms);
2085 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
2086 export_flags, additional_swms,
2087 num_additional_swms, imagex_progress_func);
2093 ret = wimlib_write(dest_w, dest_wimfile, WIMLIB_ALL_IMAGES,
2094 write_flags, num_threads,
2095 imagex_progress_func);
2097 ret = wimlib_overwrite(dest_w, write_flags, num_threads,
2098 imagex_progress_func);
2100 if (ret == WIMLIB_ERR_REOPEN)
2103 wimlib_free(dest_w);
2104 if (additional_swms) {
2105 for (unsigned i = 0; i < num_additional_swms; i++)
2106 wimlib_free(additional_swms[i]);
2107 free(additional_swms);
2113 is_root_wim_path(const tchar *path)
2116 for (p = path; *p; p++)
2117 if (*p != T('\\') && *p != T('/'))
2123 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2124 const tchar *dest_dir)
2126 for (size_t i = 0; i < num_cmds; i++)
2127 if (cmds[i].fs_dest_path != dest_dir)
2128 free(cmds[i].fs_dest_path);
2132 static struct wimlib_extract_command *
2133 prepare_extract_commands(tchar **paths, unsigned num_paths,
2134 int extract_flags, tchar *dest_dir,
2135 size_t *num_cmds_ret)
2137 struct wimlib_extract_command *cmds;
2139 tchar *emptystr = T("");
2141 if (num_paths == 0) {
2145 num_cmds = num_paths;
2146 cmds = calloc(num_cmds, sizeof(cmds[0]));
2148 imagex_error(T("Out of memory!"));
2152 for (size_t i = 0; i < num_cmds; i++) {
2153 cmds[i].extract_flags = extract_flags;
2154 cmds[i].wim_source_path = paths[i];
2155 if (is_root_wim_path(paths[i])) {
2156 cmds[i].fs_dest_path = dest_dir;
2158 size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2159 cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2160 if (!cmds[i].fs_dest_path) {
2161 free_extract_commands(cmds, num_cmds, dest_dir);
2164 tsprintf(cmds[i].fs_dest_path, T("%"TS"/%"TS), dest_dir,
2165 tbasename(paths[i]));
2168 *num_cmds_ret = num_cmds;
2172 /* Extract files or directories from a WIM image */
2174 imagex_extract(int argc, tchar **argv)
2177 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2181 const tchar *wimfile;
2182 const tchar *image_num_or_name;
2183 tchar *dest_dir = T(".");
2184 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL | WIMLIB_EXTRACT_FLAG_NORPFIX;
2186 const tchar *swm_glob = NULL;
2187 WIMStruct **additional_swms = NULL;
2188 unsigned num_additional_swms = 0;
2190 struct wimlib_extract_command *cmds;
2193 for_opt(c, extract_options) {
2195 case IMAGEX_CHECK_OPTION:
2196 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2198 case IMAGEX_VERBOSE_OPTION:
2199 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
2201 case IMAGEX_REF_OPTION:
2204 case IMAGEX_UNIX_DATA_OPTION:
2205 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2207 case IMAGEX_NO_ACLS_OPTION:
2208 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2210 case IMAGEX_STRICT_ACLS_OPTION:
2211 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2213 case IMAGEX_DEST_DIR_OPTION:
2216 case IMAGEX_TO_STDOUT_OPTION:
2217 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2218 imagex_be_quiet = true;
2231 image_num_or_name = argv[1];
2236 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2243 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2247 image = wimlib_resolve_image(wim, image_num_or_name);
2248 ret = verify_image_exists_and_is_single(image,
2252 goto out_wimlib_free;
2255 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2257 &num_additional_swms);
2259 goto out_wimlib_free;
2263 win32_acquire_restore_privileges();
2266 ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
2267 additional_swms, num_additional_swms,
2268 imagex_progress_func);
2270 if (!imagex_be_quiet)
2271 tprintf(T("Done extracting files.\n"));
2272 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2273 tfprintf(stderr, T("Note: You can use `"IMAGEX_PROGNAME" dir' to see what "
2274 "files and directories\n"
2275 " are in the WIM image.\n"));
2278 win32_release_restore_privileges();
2280 if (additional_swms) {
2281 for (unsigned i = 0; i < num_additional_swms; i++)
2282 wimlib_free(additional_swms[i]);
2283 free(additional_swms);
2288 free_extract_commands(cmds, num_cmds, dest_dir);
2297 /* Prints information about a WIM file; also can mark an image as bootable,
2298 * change the name of an image, or change the description of an image. */
2300 imagex_info(int argc, tchar **argv)
2305 bool header = false;
2306 bool lookup_table = false;
2308 bool metadata = false;
2309 bool short_header = true;
2310 const tchar *xml_out_file = NULL;
2311 const tchar *wimfile;
2312 const tchar *image_num_or_name = T("all");
2313 const tchar *new_name = NULL;
2314 const tchar *new_desc = NULL;
2319 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2324 for_opt(c, info_options) {
2326 case IMAGEX_BOOT_OPTION:
2329 case IMAGEX_CHECK_OPTION:
2332 case IMAGEX_HEADER_OPTION:
2334 short_header = false;
2336 case IMAGEX_LOOKUP_TABLE_OPTION:
2337 lookup_table = true;
2338 short_header = false;
2340 case IMAGEX_XML_OPTION:
2342 short_header = false;
2344 case IMAGEX_EXTRACT_XML_OPTION:
2345 xml_out_file = optarg;
2346 short_header = false;
2348 case IMAGEX_METADATA_OPTION:
2350 short_header = false;
2360 if (argc == 0 || argc > 4) {
2366 image_num_or_name = argv[1];
2376 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2378 ret = wimlib_open_wim(wimfile, open_flags, &w,
2379 imagex_progress_func);
2383 part_number = wimlib_get_part_number(w, &total_parts);
2385 image = wimlib_resolve_image(w, image_num_or_name);
2386 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
2387 imagex_error(T("The image \"%"TS"\" does not exist"),
2390 imagex_error(T("If you would like to set the boot "
2391 "index to 0, specify image \"0\" with "
2392 "the --boot flag."));
2394 ret = WIMLIB_ERR_INVALID_IMAGE;
2398 num_images = wimlib_get_num_images(w);
2400 if (num_images == 0) {
2402 imagex_error(T("--boot is meaningless on a WIM with no "
2404 ret = WIMLIB_ERR_INVALID_IMAGE;
2409 if (image == WIMLIB_ALL_IMAGES && num_images > 1) {
2411 imagex_error(T("Cannot specify the --boot flag "
2412 "without specifying a specific "
2413 "image in a multi-image WIM"));
2414 ret = WIMLIB_ERR_INVALID_IMAGE;
2418 imagex_error(T("Cannot specify the NEW_NAME "
2419 "without specifying a specific "
2420 "image in a multi-image WIM"));
2421 ret = WIMLIB_ERR_INVALID_IMAGE;
2426 /* Operations that print information are separated from operations that
2427 * recreate the WIM file. */
2428 if (!new_name && !boot) {
2430 /* Read-only operations */
2432 if (image == WIMLIB_NO_IMAGE) {
2433 imagex_error(T("\"%"TS"\" is not a valid image"),
2435 ret = WIMLIB_ERR_INVALID_IMAGE;
2439 if (image == WIMLIB_ALL_IMAGES && short_header)
2440 wimlib_print_wim_information(w);
2443 wimlib_print_header(w);
2446 if (total_parts != 1) {
2447 tprintf(T("Warning: Only showing the lookup table "
2448 "for part %d of a %d-part WIM.\n"),
2449 part_number, total_parts);
2451 wimlib_print_lookup_table(w);
2455 ret = wimlib_extract_xml_data(w, stdout);
2461 fp = tfopen(xml_out_file, T("wb"));
2463 imagex_error_with_errno(T("Failed to open the "
2464 "file \"%"TS"\" for "
2470 ret = wimlib_extract_xml_data(w, fp);
2471 if (fclose(fp) != 0) {
2472 imagex_error(T("Failed to close the file "
2483 wimlib_print_available_images(w, image);
2486 ret = wimlib_print_metadata(w, image);
2492 /* Modification operations */
2493 if (total_parts != 1) {
2494 imagex_error(T("Modifying a split WIM is not supported."));
2498 if (image == WIMLIB_ALL_IMAGES)
2501 if (image == WIMLIB_NO_IMAGE && new_name) {
2502 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
2503 "when using image 0"), new_name);
2509 if (image == wimlib_get_boot_idx(w)) {
2510 tprintf(T("Image %d is already marked as "
2511 "bootable.\n"), image);
2514 tprintf(T("Marking image %d as bootable.\n"),
2516 wimlib_set_boot_idx(w, image);
2520 if (!tstrcmp(wimlib_get_image_name(w, image), new_name))
2522 tprintf(T("Image %d is already named \"%"TS"\".\n"),
2526 tprintf(T("Changing the name of image %d to "
2527 "\"%"TS"\".\n"), image, new_name);
2528 ret = wimlib_set_image_name(w, image, new_name);
2534 const tchar *old_desc;
2535 old_desc = wimlib_get_image_description(w, image);
2536 if (old_desc && !tstrcmp(old_desc, new_desc)) {
2537 tprintf(T("The description of image %d is already "
2538 "\"%"TS"\".\n"), image, new_desc);
2541 tprintf(T("Changing the description of image %d "
2542 "to \"%"TS"\".\n"), image, new_desc);
2543 ret = wimlib_set_image_descripton(w, image,
2550 /* Only call wimlib_overwrite() if something actually needs to
2552 if (boot || new_name || new_desc ||
2553 (check && !wimlib_has_integrity_table(w)))
2557 ret = file_writable(wimfile);
2562 write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2566 ret = wimlib_overwrite(w, write_flags, 1,
2567 imagex_progress_func);
2568 if (ret == WIMLIB_ERR_REOPEN)
2571 tprintf(T("The file \"%"TS"\" was not modified because nothing "
2572 "needed to be done.\n"), wimfile);
2581 /* Join split WIMs into one part WIM */
2583 imagex_join(int argc, tchar **argv)
2586 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2587 int wim_write_flags = 0;
2588 const tchar *output_path;
2590 for_opt(c, join_options) {
2592 case IMAGEX_CHECK_OPTION:
2593 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2594 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2604 imagex_error(T("Must specify one or more split WIM (.swm) "
2608 output_path = argv[0];
2609 return wimlib_join((const tchar * const *)++argv,
2614 imagex_progress_func);
2620 /* Mounts an image using a FUSE mount. */
2622 imagex_mount_rw_or_ro(int argc, tchar **argv)
2625 int mount_flags = 0;
2626 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2627 const tchar *wimfile;
2633 const tchar *swm_glob = NULL;
2634 WIMStruct **additional_swms = NULL;
2635 unsigned num_additional_swms = 0;
2636 const tchar *staging_dir = NULL;
2638 if (!tstrcmp(argv[0], T("mountrw")))
2639 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
2641 for_opt(c, mount_options) {
2643 case IMAGEX_ALLOW_OTHER_OPTION:
2644 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
2646 case IMAGEX_CHECK_OPTION:
2647 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2649 case IMAGEX_DEBUG_OPTION:
2650 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
2652 case IMAGEX_STREAMS_INTERFACE_OPTION:
2653 if (!tstrcasecmp(optarg, T("none")))
2654 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
2655 else if (!tstrcasecmp(optarg, T("xattr")))
2656 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2657 else if (!tstrcasecmp(optarg, T("windows")))
2658 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
2660 imagex_error(T("Unknown stream interface \"%"TS"\""),
2665 case IMAGEX_REF_OPTION:
2668 case IMAGEX_STAGING_DIR_OPTION:
2669 staging_dir = optarg;
2671 case IMAGEX_UNIX_DATA_OPTION:
2672 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
2680 if (argc != 2 && argc != 3)
2685 ret = wimlib_open_wim(wimfile, open_flags, &w,
2686 imagex_progress_func);
2691 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2693 &num_additional_swms);
2700 num_images = wimlib_get_num_images(w);
2701 if (num_images != 1) {
2702 imagex_error(T("\"%"TS"\" contains %d images; Please "
2703 "select one."), wimfile, num_images);
2704 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2711 image = wimlib_resolve_image(w, argv[1]);
2713 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
2718 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2719 ret = file_writable(wimfile);
2724 ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
2725 num_additional_swms, staging_dir);
2727 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
2729 image, wimfile, dir);
2734 if (additional_swms) {
2735 for (unsigned i = 0; i < num_additional_swms; i++)
2736 wimlib_free(additional_swms[i]);
2737 free(additional_swms);
2741 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2746 /* Rebuild a WIM file */
2748 imagex_optimize(int argc, tchar **argv)
2752 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
2755 const tchar *wimfile;
2758 unsigned num_threads = 0;
2760 for_opt(c, optimize_options) {
2762 case IMAGEX_CHECK_OPTION:
2763 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2764 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2766 case IMAGEX_RECOMPRESS_OPTION:
2767 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
2769 case IMAGEX_THREADS_OPTION:
2770 num_threads = parse_num_threads(optarg);
2771 if (num_threads == UINT_MAX)
2789 ret = file_writable(wimfile);
2793 ret = wimlib_open_wim(wimfile, open_flags, &w,
2794 imagex_progress_func);
2798 old_size = file_get_size(argv[0]);
2799 tprintf(T("\"%"TS"\" original size: "), wimfile);
2801 tfputs(T("Unknown\n"), stdout);
2803 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
2805 ret = wimlib_overwrite(w, write_flags, num_threads,
2806 imagex_progress_func);
2809 new_size = file_get_size(argv[0]);
2810 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
2812 tfputs(T("Unknown\n"), stdout);
2814 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
2816 tfputs(T("Space saved: "), stdout);
2817 if (new_size != -1 && old_size != -1) {
2818 tprintf(T("%lld KiB\n"),
2819 ((long long)old_size - (long long)new_size) >> 10);
2821 tfputs(T("Unknown\n"), stdout);
2829 /* Split a WIM into a spanned set */
2831 imagex_split(int argc, tchar **argv)
2834 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2835 int write_flags = 0;
2836 unsigned long part_size;
2841 for_opt(c, split_options) {
2843 case IMAGEX_CHECK_OPTION:
2844 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2845 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2859 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
2860 if (tmp == argv[2] || *tmp) {
2861 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
2862 imagex_error(T("The part size must be an integer or "
2863 "floating-point number of megabytes."));
2866 ret = wimlib_open_wim(argv[0], open_flags, &w, imagex_progress_func);
2869 ret = wimlib_split(w, argv[1], part_size, write_flags, imagex_progress_func);
2874 /* Unmounts a mounted WIM image. */
2876 imagex_unmount(int argc, tchar **argv)
2879 int unmount_flags = 0;
2882 for_opt(c, unmount_options) {
2884 case IMAGEX_COMMIT_OPTION:
2885 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
2887 case IMAGEX_CHECK_OPTION:
2888 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
2890 case IMAGEX_REBUILD_OPTION:
2891 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
2893 case IMAGEX_LAZY_OPTION:
2894 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
2908 ret = wimlib_unmount_image(argv[0], unmount_flags,
2909 imagex_progress_func);
2911 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
2916 * Add, delete, or rename files in a WIM image.
2919 imagex_update(int argc, tchar **argv)
2921 const tchar *wimfile;
2922 const tchar *image_num_or_name;
2927 int write_flags = 0;
2928 int update_flags = 0;
2929 int default_add_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
2930 int default_delete_flags = 0;
2931 unsigned num_threads = 0;
2933 tchar *cmd_file_contents;
2934 size_t cmd_file_nchars;
2935 struct wimlib_update_command *cmds;
2939 const tchar *config_file = NULL;
2941 struct wimlib_capture_config *config = NULL;
2943 for_opt(c, update_options) {
2945 /* Generic or write options */
2946 case IMAGEX_THREADS_OPTION:
2947 num_threads = parse_num_threads(optarg);
2948 if (num_threads == UINT_MAX) {
2953 case IMAGEX_CHECK_OPTION:
2954 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2955 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2957 case IMAGEX_REBUILD_OPTION:
2958 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2961 /* Default delete options */
2962 case IMAGEX_FORCE_OPTION:
2963 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
2965 case IMAGEX_RECURSIVE_OPTION:
2966 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
2969 /* Global add option */
2970 case IMAGEX_CONFIG_OPTION:
2971 config_file = optarg;
2974 /* Default add options */
2975 case IMAGEX_VERBOSE_OPTION:
2976 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
2978 case IMAGEX_DEREFERENCE_OPTION:
2979 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
2981 case IMAGEX_UNIX_DATA_OPTION:
2982 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
2984 case IMAGEX_NO_ACLS_OPTION:
2985 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
2987 case IMAGEX_STRICT_ACLS_OPTION:
2988 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
2997 if (argc < 1 || argc > 2)
3001 ret = file_writable(wimfile);
3005 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3010 image_num_or_name = argv[1];
3012 image_num_or_name = T("1");
3014 image = wimlib_resolve_image(wim, image_num_or_name);
3016 ret = verify_image_exists_and_is_single(image, image_num_or_name,
3019 goto out_wimlib_free;
3021 num_images = wimlib_get_num_images(wim);
3022 if (argc == 1 && num_images != 1) {
3023 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3024 wimfile, num_images);
3027 goto out_wimlib_free;
3030 /* Parse capture configuration file if specified */
3034 config_str = file_get_text_contents(config_file, &config_len);
3037 goto out_wimlib_free;
3040 config = alloca(sizeof(*config));
3041 ret = parse_capture_config(&config_str, config_len, config);
3043 goto out_free_config;
3045 config = &default_capture_config;
3048 /* Read update commands from standard input */
3049 if (isatty(STDIN_FILENO)) {
3050 tputs(T("Reading update commands from standard input..."));
3051 recommend_man_page(T("update"));
3053 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3054 if (!cmd_file_contents) {
3056 goto out_free_config;
3059 /* Parse the update commands */
3060 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3064 goto out_free_cmd_file_contents;
3067 /* Set default flags and capture config on the update commands */
3068 for (size_t i = 0; i < num_cmds; i++) {
3069 switch (cmds[i].op) {
3070 case WIMLIB_UPDATE_OP_ADD:
3071 cmds[i].add.add_flags |= default_add_flags;
3072 cmds[i].add.config = config;
3074 case WIMLIB_UPDATE_OP_DELETE:
3075 cmds[i].delete.delete_flags |= default_delete_flags;
3083 win32_acquire_capture_privileges();
3086 /* Execute the update commands */
3087 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3088 imagex_progress_func);
3090 goto out_release_privs;
3092 /* Overwrite the updated WIM */
3093 ret = wimlib_overwrite(wim, write_flags, num_threads,
3094 imagex_progress_func);
3097 win32_release_capture_privileges();
3101 out_free_cmd_file_contents:
3102 free(cmd_file_contents);
3104 if (config != NULL && config != &default_capture_config) {
3105 free(config->exclusion_pats.pats);
3106 free(config->exclusion_exception_pats.pats);
3119 struct imagex_command {
3121 int (*func)(int , tchar **);
3126 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
3127 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
3129 static const struct imagex_command imagex_commands[] = {
3130 {T("append"), imagex_capture_or_append, APPEND},
3131 {T("apply"), imagex_apply, APPLY},
3132 {T("capture"), imagex_capture_or_append, CAPTURE},
3133 {T("delete"), imagex_delete, DELETE},
3134 {T("dir"), imagex_dir, DIR},
3135 {T("export"), imagex_export, EXPORT},
3136 {T("extract"), imagex_extract, EXTRACT},
3137 {T("info"), imagex_info, INFO},
3138 {T("join"), imagex_join, JOIN},
3139 {T("mount"), imagex_mount_rw_or_ro, MOUNT},
3140 {T("mountrw"), imagex_mount_rw_or_ro, MOUNTRW},
3141 {T("optimize"),imagex_optimize, OPTIMIZE},
3142 {T("split"), imagex_split, SPLIT},
3143 {T("unmount"), imagex_unmount, UNMOUNT},
3144 {T("update"), imagex_update, UPDATE},
3150 static const tchar *s =
3152 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
3153 "Copyright (C) 2012, 2013 Eric Biggers\n"
3154 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
3155 "This is free software: you are free to change and redistribute it.\n"
3156 "There is NO WARRANTY, to the extent permitted by law.\n"
3158 "Report bugs to "PACKAGE_BUGREPORT".\n"
3165 help_or_version(int argc, tchar **argv)
3169 const struct imagex_command *cmd;
3171 for (i = 1; i < argc; i++) {
3179 if (!tstrcmp(p, T("help"))) {
3180 for_imagex_command(cmd) {
3181 if (!tstrcmp(cmd->name, argv[1])) {
3189 if (!tstrcmp(p, T("version"))) {
3200 const struct imagex_command *cmd;
3201 tprintf(T("Usage:\n%"TS), usage_strings[cmd_type]);
3202 for_imagex_command(cmd) {
3203 if (cmd->cmd == cmd_type) {
3204 tputc(T('\n'), stdout);
3205 recommend_man_page(cmd->name);
3213 tfputs(T("Usage:\n"), stdout);
3214 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
3215 tprintf(T(" %"TS), usage_strings[i]);
3216 static const tchar *extra =
3218 " "IMAGEX_PROGNAME" --help\n"
3219 " "IMAGEX_PROGNAME" --version\n"
3221 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
3224 tfputs(extra, stdout);
3225 recommend_man_page(T(""));
3228 /* Entry point for wimlib's ImageX implementation. On UNIX the command
3229 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
3230 * something else), while an Windows the command arguments will be UTF-16LE
3231 * encoded 'wchar_t' strings. */
3234 wmain(int argc, wchar_t **argv, wchar_t **envp)
3236 main(int argc, char **argv)
3239 const struct imagex_command *cmd;
3244 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
3245 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
3249 setlocale(LC_ALL, "");
3250 codeset = nl_langinfo(CODESET);
3251 if (!strstr(codeset, "UTF-8") &&
3252 !strstr(codeset, "UTF8") &&
3253 !strstr(codeset, "utf-8") &&
3254 !strstr(codeset, "utf8"))
3257 "WARNING: Running "IMAGEX_PROGNAME" in a UTF-8 locale is recommended!\n"
3258 " Maybe try: `export LANG=en_US.UTF-8'?\n"
3259 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
3260 " to any value to force wimlib to use UTF-8.\n",
3265 #endif /* !__WIN32__ */
3268 imagex_error(T("No command specified"));
3274 /* Handle --help and --version for all commands. Note that this will
3275 * not return if either of these arguments are present. */
3276 help_or_version(argc, argv);
3280 /* The user may like to see more informative error messages. */
3281 wimlib_set_print_errors(true);
3283 /* Do any initializations that the library needs */
3284 ret = wimlib_global_init(init_flags);
3286 goto out_check_status;
3288 /* Search for the function to handle the ImageX subcommand. */
3289 for_imagex_command(cmd) {
3290 if (!tstrcmp(cmd->name, *argv)) {
3291 ret = cmd->func(argc, argv);
3292 goto out_check_write_error;
3296 imagex_error(T("Unrecognized command: `%"TS"'"), argv[0]);
3300 out_check_write_error:
3301 /* For 'wimlib-imagex info' and 'wimlib-imagex dir', data printed to
3302 * standard output is part of the program's actual behavior and not just
3303 * for informational purposes, so we should set a failure exit status if
3304 * there was a write error. */
3305 if (cmd == &imagex_commands[INFO] || cmd == &imagex_commands[DIR]) {
3306 if (ferror(stdout) || fclose(stdout)) {
3307 imagex_error_with_errno(T("error writing to standard output"));
3313 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
3314 * outside of the wimlib library code. 0 indicates success. > 0
3315 * indicates a wimlib error code from which an error message can be
3318 imagex_error(T("Exiting with error code %d:\n"
3320 wimlib_get_error_string(ret));
3321 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
3322 imagex_error_with_errno(T("errno"));
3325 /* Make the library free any resources it's holding (not strictly
3326 * necessary because the process is ending anyway). */
3327 wimlib_global_cleanup();