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 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
1003 #define MEBIBYTE_MIN_NBYTES 10000000ULL
1004 #define KIBIBYTE_MIN_NBYTES 10000ULL
1007 get_unit(uint64_t total_bytes, const tchar **name_ret)
1009 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
1010 *name_ret = T("GiB");
1012 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
1013 *name_ret = T("MiB");
1015 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
1016 *name_ret = T("KiB");
1019 *name_ret = T("bytes");
1024 /* Progress callback function passed to various wimlib functions. */
1026 imagex_progress_func(enum wimlib_progress_msg msg,
1027 const union wimlib_progress_info *info)
1029 unsigned percent_done;
1030 unsigned unit_shift;
1031 const tchar *unit_name;
1032 if (imagex_be_quiet)
1035 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1036 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1037 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1038 info->write_streams.total_bytes);
1039 if (info->write_streams.completed_streams == 0) {
1040 const tchar *data_type;
1042 data_type = get_data_type(info->write_streams.compression_type);
1043 tprintf(T("Writing %"TS" data using %u thread%"TS"\n"),
1044 data_type, info->write_streams.num_threads,
1045 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1047 tprintf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1048 "written (%u%% done)"),
1049 info->write_streams.completed_bytes >> unit_shift,
1051 info->write_streams.total_bytes >> unit_shift,
1054 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1057 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1058 tprintf(T("Scanning \"%"TS"\""), info->scan.source);
1059 if (*info->scan.wim_target_path) {
1060 tprintf(T(" (loading as WIM path: \"/%"TS"\")...\n"),
1061 info->scan.wim_target_path);
1063 tprintf(T(" (loading as root of WIM image)...\n"));
1066 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1067 if (info->scan.excluded)
1068 tprintf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1070 tprintf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
1072 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
1074 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1075 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1076 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1077 info->integrity.total_bytes);
1078 tprintf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" "TS" "
1079 "of %"PRIu64" "TS" (%u%%) done"),
1080 info->integrity.filename,
1081 info->integrity.completed_bytes >> unit_shift,
1083 info->integrity.total_bytes >> unit_shift,
1086 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1089 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1090 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1091 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1092 info->integrity.total_bytes);
1093 tprintf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1094 "of %"PRIu64" %"TS" (%u%%) done"),
1095 info->integrity.completed_bytes >> unit_shift,
1097 info->integrity.total_bytes >> unit_shift,
1100 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1103 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1104 tprintf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1105 "to %"TS" \"%"TS"\"\n"),
1106 info->extract.image,
1107 info->extract.image_name,
1108 info->extract.wimfile_name,
1109 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1110 T("NTFS volume") : T("directory")),
1111 info->extract.target);
1113 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1114 tprintf(T("Extracting \"%"TS"\" from image %d (\"%"TS"\") "
1115 "in \"%"TS"\" to \"%"TS"\"\n"),
1116 info->extract.extract_root_wim_source_path,
1117 info->extract.image,
1118 info->extract.image_name,
1119 info->extract.wimfile_name,
1120 info->extract.target);
1122 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
1123 /*tprintf(T("Applying directory structure to %"TS"\n"),*/
1124 /*info->extract.target);*/
1126 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1127 percent_done = TO_PERCENT(info->extract.completed_bytes,
1128 info->extract.total_bytes);
1129 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1130 tprintf(T("\rExtracting files: "
1131 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1132 info->extract.completed_bytes >> unit_shift,
1134 info->extract.total_bytes >> unit_shift,
1137 if (info->extract.completed_bytes >= info->extract.total_bytes)
1140 case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
1141 tprintf(T("%"TS"\n"), info->extract.cur_path);
1143 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1144 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1145 tprintf(T("Setting timestamps on all extracted files...\n"));
1147 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1148 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1149 tprintf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1150 info->extract.target);
1153 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
1154 percent_done = TO_PERCENT(info->join.completed_bytes,
1155 info->join.total_bytes);
1156 unit_shift = get_unit(info->join.total_bytes, &unit_name);
1157 tprintf(T("Writing resources from part %u of %u: "
1158 "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written\n"),
1159 (info->join.completed_parts == info->join.total_parts) ?
1160 info->join.completed_parts : info->join.completed_parts + 1,
1161 info->join.total_parts,
1162 info->join.completed_bytes >> unit_shift,
1164 info->join.total_bytes >> unit_shift,
1168 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1169 percent_done = TO_PERCENT(info->split.completed_bytes,
1170 info->split.total_bytes);
1171 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1172 tprintf(T("Writing \"%"TS"\": %"PRIu64" %"TS" of "
1173 "%"PRIu64" %"TS" (%u%%) written\n"),
1174 info->split.part_name,
1175 info->split.completed_bytes >> unit_shift,
1177 info->split.total_bytes >> unit_shift,
1181 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1182 if (info->split.completed_bytes == info->split.total_bytes) {
1183 tprintf(T("Finished writing %u split WIM parts\n"),
1184 info->split.cur_part_number);
1194 /* Open all the split WIM parts that correspond to a file glob.
1196 * @first_part specifies the first part of the split WIM and it may be either
1197 * included or omitted from the glob. */
1199 open_swms_from_glob(const tchar *swm_glob,
1200 const tchar *first_part,
1202 WIMStruct ***additional_swms_ret,
1203 unsigned *num_additional_swms_ret)
1205 unsigned num_additional_swms = 0;
1206 WIMStruct **additional_swms = NULL;
1210 /* Warning: glob() is replaced in Windows native builds */
1211 ret = tglob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
1213 if (ret == GLOB_NOMATCH) {
1214 imagex_error(T("Found no files for glob \"%"TS"\""),
1217 imagex_error_with_errno(T("Failed to process glob \"%"TS"\""),
1223 num_additional_swms = globbuf.gl_pathc;
1224 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
1225 if (!additional_swms) {
1226 imagex_error(T("Out of memory"));
1230 unsigned offset = 0;
1231 for (unsigned i = 0; i < num_additional_swms; i++) {
1232 if (tstrcmp(globbuf.gl_pathv[i], first_part) == 0) {
1236 ret = wimlib_open_wim(globbuf.gl_pathv[i],
1237 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
1238 &additional_swms[i - offset],
1239 imagex_progress_func);
1241 goto out_close_swms;
1243 *additional_swms_ret = additional_swms;
1244 *num_additional_swms_ret = num_additional_swms - offset;
1248 for (unsigned i = 0; i < num_additional_swms; i++)
1249 wimlib_free(additional_swms[i]);
1250 free(additional_swms);
1259 parse_num_threads(const tchar *optarg)
1262 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1263 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1264 imagex_error(T("Number of threads must be a non-negative integer!"));
1272 * Parse an option passed to an update command.
1274 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1277 * @option: Text string for the option (beginning with --)
1279 * @cmd: `struct wimlib_update_command' that is being constructed for
1282 * Returns true if the option was recognized; false if not.
1285 update_command_add_option(int op, const tchar *option,
1286 struct wimlib_update_command *cmd)
1288 bool recognized = true;
1290 case WIMLIB_UPDATE_OP_ADD:
1291 if (!tstrcmp(option, T("--verbose")))
1292 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1293 else if (!tstrcmp(option, T("--unix-data")))
1294 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1295 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1296 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1297 else if (!tstrcmp(option, T("--strict-acls")))
1298 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1299 else if (!tstrcmp(option, T("--dereference")))
1300 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1304 case WIMLIB_UPDATE_OP_DELETE:
1305 if (!tstrcmp(option, T("--force")))
1306 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1307 else if (!tstrcmp(option, T("--recursive")))
1308 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1319 /* How many nonoption arguments each `imagex update' command expects */
1320 static const unsigned update_command_num_nonoptions[] = {
1321 [WIMLIB_UPDATE_OP_ADD] = 2,
1322 [WIMLIB_UPDATE_OP_DELETE] = 1,
1323 [WIMLIB_UPDATE_OP_RENAME] = 2,
1327 update_command_add_nonoption(int op, const tchar *nonoption,
1328 struct wimlib_update_command *cmd,
1329 unsigned num_nonoptions)
1332 case WIMLIB_UPDATE_OP_ADD:
1333 if (num_nonoptions == 0)
1334 cmd->add.fs_source_path = (tchar*)nonoption;
1336 cmd->add.wim_target_path = (tchar*)nonoption;
1338 case WIMLIB_UPDATE_OP_DELETE:
1339 cmd->delete.wim_path = (tchar*)nonoption;
1341 case WIMLIB_UPDATE_OP_RENAME:
1342 if (num_nonoptions == 0)
1343 cmd->rename.wim_source_path = (tchar*)nonoption;
1345 cmd->rename.wim_target_path = (tchar*)nonoption;
1351 * Parse a command passed on stdin to `imagex update'.
1353 * @line: Text of the command.
1354 * @len: Length of the line, including a null terminator
1357 * @command: A `struct wimlib_update_command' to fill in from the parsed
1360 * @line_number: Line number of the command, for diagnostics.
1362 * Returns true on success; returns false on parse error.
1365 parse_update_command(tchar *line, size_t len,
1366 struct wimlib_update_command *command,
1370 tchar *command_name;
1372 size_t num_nonoptions;
1374 /* Get the command name ("add", "delete", "rename") */
1375 ret = parse_string(&line, &len, &command_name);
1376 if (ret != PARSE_STRING_SUCCESS)
1379 if (!tstrcasecmp(command_name, T("add"))) {
1380 op = WIMLIB_UPDATE_OP_ADD;
1381 } else if (!tstrcasecmp(command_name, T("delete"))) {
1382 op = WIMLIB_UPDATE_OP_DELETE;
1383 } else if (!tstrcasecmp(command_name, T("rename"))) {
1384 op = WIMLIB_UPDATE_OP_RENAME;
1386 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1387 command_name, line_number);
1392 /* Parse additional options and non-options as needed */
1397 ret = parse_string(&line, &len, &next_string);
1398 if (ret == PARSE_STRING_NONE) /* End of line */
1400 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1402 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1404 if (!update_command_add_option(op, next_string, command))
1406 imagex_error(T("Unrecognized option \"%"TS"\" to "
1407 "update command \"%"TS"\" on line %zu"),
1408 next_string, command_name, line_number);
1414 if (num_nonoptions == update_command_num_nonoptions[op])
1416 imagex_error(T("Unexpected argument \"%"TS"\" in "
1417 "update command on line %zu\n"
1418 " (The \"%"TS"\" command only "
1419 "takes %u nonoption arguments!)\n"),
1420 next_string, line_number,
1421 command_name, num_nonoptions);
1424 update_command_add_nonoption(op, next_string,
1425 command, num_nonoptions);
1430 if (num_nonoptions != update_command_num_nonoptions[op]) {
1431 imagex_error(T("Not enough arguments to update command "
1432 "\"%"TS"\" on line %zu"), command_name, line_number);
1438 static struct wimlib_update_command *
1439 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1440 size_t *num_cmds_ret)
1444 struct wimlib_update_command *cmds;
1447 nlines = text_file_count_lines(cmd_file_contents_p,
1452 /* Always allocate at least 1 slot, just in case the implementation of
1453 * calloc() returns NULL if 0 bytes are requested. */
1454 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1456 imagex_error(T("out of memory"));
1459 p = *cmd_file_contents_p;
1461 for (i = 0; i < nlines; i++) {
1462 /* XXX: Could use rawmemchr() here instead, but it may not be
1463 * available on all platforms. */
1464 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1465 size_t len = endp - p + 1;
1467 if (!is_comment_line(p, len)) {
1468 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1479 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1480 * one image from a WIM file to a NTFS volume. */
1482 imagex_apply(int argc, tchar **argv)
1485 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1490 const tchar *wimfile;
1491 const tchar *target;
1492 const tchar *image_num_or_name;
1493 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1495 const tchar *swm_glob = NULL;
1496 WIMStruct **additional_swms = NULL;
1497 unsigned num_additional_swms = 0;
1499 for_opt(c, apply_options) {
1501 case IMAGEX_CHECK_OPTION:
1502 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1504 case IMAGEX_HARDLINK_OPTION:
1505 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1507 case IMAGEX_SYMLINK_OPTION:
1508 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1510 case IMAGEX_VERBOSE_OPTION:
1511 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
1513 case IMAGEX_REF_OPTION:
1516 case IMAGEX_UNIX_DATA_OPTION:
1517 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1519 case IMAGEX_NO_ACLS_OPTION:
1520 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1522 case IMAGEX_STRICT_ACLS_OPTION:
1523 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1525 case IMAGEX_NORPFIX_OPTION:
1526 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1528 case IMAGEX_RPFIX_OPTION:
1529 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1538 if (argc != 2 && argc != 3) {
1545 image_num_or_name = T("1");
1548 image_num_or_name = argv[1];
1552 ret = wimlib_open_wim(wimfile, open_flags, &w, imagex_progress_func);
1556 image = wimlib_resolve_image(w, image_num_or_name);
1557 ret = verify_image_exists(image, image_num_or_name, wimfile);
1561 num_images = wimlib_get_num_images(w);
1562 if (argc == 2 && num_images != 1) {
1563 imagex_error(T("\"%"TS"\" contains %d images; Please select one "
1564 "(or all)."), wimfile, num_images);
1571 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1573 &num_additional_swms);
1580 ret = tstat(target, &stbuf);
1582 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1583 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1585 if (errno != ENOENT) {
1586 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1594 win32_acquire_restore_privileges();
1596 ret = wimlib_extract_image(w, image, target, extract_flags,
1597 additional_swms, num_additional_swms,
1598 imagex_progress_func);
1600 tprintf(T("Done applying WIM image.\n"));
1602 win32_release_restore_privileges();
1606 if (additional_swms) {
1607 for (unsigned i = 0; i < num_additional_swms; i++)
1608 wimlib_free(additional_swms[i]);
1609 free(additional_swms);
1614 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1615 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1616 * the desired image. 'wimlib-imagex append': add a new image to an existing
1619 imagex_capture_or_append(int argc, tchar **argv)
1623 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
1624 int write_flags = 0;
1625 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
1626 const tchar *wimfile;
1629 const tchar *flags_element = NULL;
1633 int cmd = tstrcmp(argv[0], T("append")) ? CAPTURE : APPEND;
1634 unsigned num_threads = 0;
1637 size_t source_name_len;
1640 const tchar *config_file = NULL;
1642 struct wimlib_capture_config *config = NULL;
1644 bool source_list = false;
1645 size_t source_list_nchars;
1646 tchar *source_list_contents;
1647 bool capture_sources_malloced;
1648 struct wimlib_capture_source *capture_sources;
1651 for_opt(c, capture_or_append_options) {
1653 case IMAGEX_BOOT_OPTION:
1654 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1656 case IMAGEX_CHECK_OPTION:
1657 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1658 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1660 case IMAGEX_CONFIG_OPTION:
1661 config_file = optarg;
1663 case IMAGEX_COMPRESS_OPTION:
1664 compression_type = get_compression_type(optarg);
1665 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1668 case IMAGEX_FLAGS_OPTION:
1669 flags_element = optarg;
1671 case IMAGEX_DEREFERENCE_OPTION:
1672 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1674 case IMAGEX_VERBOSE_OPTION:
1675 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1677 case IMAGEX_THREADS_OPTION:
1678 num_threads = parse_num_threads(optarg);
1679 if (num_threads == UINT_MAX)
1682 case IMAGEX_REBUILD_OPTION:
1683 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1685 case IMAGEX_UNIX_DATA_OPTION:
1686 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1688 case IMAGEX_SOURCE_LIST_OPTION:
1691 case IMAGEX_NO_ACLS_OPTION:
1692 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1694 case IMAGEX_STRICT_ACLS_OPTION:
1695 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1697 case IMAGEX_RPFIX_OPTION:
1698 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1700 case IMAGEX_NORPFIX_OPTION:
1701 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1711 if (argc < 2 || argc > 4) {
1722 /* Set default name to SOURCE argument, omitting any directory
1723 * prefixes and trailing slashes. This requires making a copy
1725 source_name_len = tstrlen(source);
1726 source_copy = alloca((source_name_len + 1) * sizeof(tchar));
1727 name = tbasename(tstrcpy(source_copy, source));
1729 /* Image description defaults to NULL if not given. */
1730 desc = (argc >= 4) ? argv[3] : NULL;
1733 /* Set up capture sources in source list mode */
1734 if (source[0] == T('-') && source[1] == T('\0')) {
1735 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1737 source_list_contents = file_get_text_contents(source,
1738 &source_list_nchars);
1740 if (!source_list_contents)
1743 capture_sources = parse_source_list(&source_list_contents,
1746 if (!capture_sources) {
1748 goto out_free_source_list_contents;
1750 capture_sources_malloced = true;
1752 /* Set up capture source in non-source-list mode (could be
1753 * either "normal" mode or "NTFS mode"--- see the man page). */
1754 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1755 capture_sources[0].fs_source_path = source;
1756 capture_sources[0].wim_target_path = NULL;
1757 capture_sources[0].reserved = 0;
1759 capture_sources_malloced = false;
1760 source_list_contents = NULL;
1766 config_str = file_get_text_contents(config_file, &config_len);
1769 goto out_free_capture_sources;
1772 config = alloca(sizeof(*config));
1773 ret = parse_capture_config(&config_str, config_len, config);
1775 goto out_free_config;
1777 config = &default_capture_config;
1781 ret = wimlib_open_wim(wimfile, open_flags, &w,
1782 imagex_progress_func);
1784 ret = wimlib_create_new_wim(compression_type, &w);
1786 goto out_free_config;
1790 ret = tstat(source, &stbuf);
1792 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1793 tprintf(T("Capturing WIM image from NTFS "
1794 "filesystem on \"%"TS"\"\n"), source);
1795 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1798 if (errno != ENOENT) {
1799 imagex_error_with_errno(T("Failed to stat "
1800 "\"%"TS"\""), source);
1802 goto out_wimlib_free;
1807 win32_acquire_capture_privileges();
1810 ret = wimlib_add_image_multisource(w,
1816 imagex_progress_func);
1818 goto out_release_privs;
1819 cur_image = wimlib_get_num_images(w);
1821 ret = wimlib_set_image_descripton(w, cur_image, desc);
1823 goto out_release_privs;
1825 if (flags_element) {
1826 ret = wimlib_set_image_flags(w, cur_image, flags_element);
1828 goto out_release_privs;
1830 if (cmd == APPEND) {
1831 ret = wimlib_overwrite(w, write_flags, num_threads,
1832 imagex_progress_func);
1834 ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
1835 num_threads, imagex_progress_func);
1837 if (ret == WIMLIB_ERR_REOPEN)
1840 imagex_error(T("Failed to write the WIM file \"%"TS"\""),
1844 win32_release_capture_privileges();
1849 if (config != NULL && config != &default_capture_config) {
1850 free(config->exclusion_pats.pats);
1851 free(config->exclusion_exception_pats.pats);
1854 out_free_capture_sources:
1855 if (capture_sources_malloced)
1856 free(capture_sources);
1857 out_free_source_list_contents:
1858 free(source_list_contents);
1862 /* Remove image(s) from a WIM. */
1864 imagex_delete(int argc, tchar **argv)
1868 int write_flags = 0;
1869 const tchar *wimfile;
1870 const tchar *image_num_or_name;
1875 for_opt(c, delete_options) {
1877 case IMAGEX_CHECK_OPTION:
1878 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1879 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1881 case IMAGEX_SOFT_OPTION:
1882 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
1894 imagex_error(T("Must specify a WIM file"));
1896 imagex_error(T("Must specify an image"));
1901 image_num_or_name = argv[1];
1903 ret = file_writable(wimfile);
1907 ret = wimlib_open_wim(wimfile, open_flags, &w,
1908 imagex_progress_func);
1912 image = wimlib_resolve_image(w, image_num_or_name);
1914 ret = verify_image_exists(image, image_num_or_name, wimfile);
1918 ret = wimlib_delete_image(w, image);
1920 imagex_error(T("Failed to delete image from \"%"TS"\""), wimfile);
1924 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1925 if (ret == WIMLIB_ERR_REOPEN)
1928 imagex_error(T("Failed to write the file \"%"TS"\" with image "
1929 "deleted"), wimfile);
1936 /* Print the files contained in an image(s) in a WIM file. */
1938 imagex_dir(int argc, tchar **argv)
1940 const tchar *wimfile;
1947 imagex_error(T("Must specify a WIM file"));
1952 imagex_error(T("Too many arguments"));
1958 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w,
1959 imagex_progress_func);
1964 image = wimlib_resolve_image(w, argv[2]);
1965 ret = verify_image_exists(image, argv[2], wimfile);
1969 /* Image was not specified. If the WIM only contains one image,
1970 * choose that one; otherwise, print an error. */
1971 num_images = wimlib_get_num_images(w);
1972 if (num_images != 1) {
1973 imagex_error(T("\"%"TS"\" contains %d images; Please "
1974 "select one."), wimfile, num_images);
1982 ret = wimlib_print_files(w, image);
1988 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
1991 imagex_export(int argc, tchar **argv)
1995 int export_flags = 0;
1996 int write_flags = 0;
1997 int compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
1998 bool compression_type_specified = false;
1999 const tchar *src_wimfile;
2000 const tchar *src_image_num_or_name;
2001 const tchar *dest_wimfile;
2002 const tchar *dest_name;
2003 const tchar *dest_desc;
2004 WIMStruct *src_w = NULL;
2005 WIMStruct *dest_w = NULL;
2010 const tchar *swm_glob = NULL;
2011 WIMStruct **additional_swms = NULL;
2012 unsigned num_additional_swms = 0;
2013 unsigned num_threads = 0;
2015 for_opt(c, export_options) {
2017 case IMAGEX_BOOT_OPTION:
2018 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2020 case IMAGEX_CHECK_OPTION:
2021 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2022 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2024 case IMAGEX_COMPRESS_OPTION:
2025 compression_type = get_compression_type(optarg);
2026 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2028 compression_type_specified = true;
2030 case IMAGEX_REF_OPTION:
2033 case IMAGEX_THREADS_OPTION:
2034 num_threads = parse_num_threads(optarg);
2035 if (num_threads == UINT_MAX)
2038 case IMAGEX_REBUILD_OPTION:
2039 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2048 if (argc < 3 || argc > 5) {
2052 src_wimfile = argv[0];
2053 src_image_num_or_name = argv[1];
2054 dest_wimfile = argv[2];
2055 dest_name = (argc >= 4) ? argv[3] : NULL;
2056 dest_desc = (argc >= 5) ? argv[4] : NULL;
2057 ret = wimlib_open_wim(src_wimfile,
2058 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w,
2059 imagex_progress_func);
2063 /* Determine if the destination is an existing file or not.
2064 * If so, we try to append the exported image(s) to it; otherwise, we
2065 * create a new WIM containing the exported image(s). */
2066 if (tstat(dest_wimfile, &stbuf) == 0) {
2070 /* Destination file exists. */
2072 if (!S_ISREG(stbuf.st_mode)) {
2073 imagex_error(T("\"%"TS"\" is not a regular file"),
2078 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w,
2079 imagex_progress_func);
2083 ret = file_writable(dest_wimfile);
2087 dest_ctype = wimlib_get_compression_type(dest_w);
2088 if (compression_type_specified
2089 && compression_type != dest_ctype)
2091 imagex_error(T("Cannot specify a compression type that is "
2092 "not the same as that used in the "
2093 "destination WIM"));
2099 /* dest_wimfile is not an existing file, so create a new WIM. */
2100 if (!compression_type_specified)
2101 compression_type = wimlib_get_compression_type(src_w);
2102 if (errno == ENOENT) {
2103 ret = wimlib_create_new_wim(compression_type, &dest_w);
2107 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2114 image = wimlib_resolve_image(src_w, src_image_num_or_name);
2115 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2120 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
2122 &num_additional_swms);
2127 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
2128 export_flags, additional_swms,
2129 num_additional_swms, imagex_progress_func);
2135 ret = wimlib_write(dest_w, dest_wimfile, WIMLIB_ALL_IMAGES,
2136 write_flags, num_threads,
2137 imagex_progress_func);
2139 ret = wimlib_overwrite(dest_w, write_flags, num_threads,
2140 imagex_progress_func);
2142 if (ret == WIMLIB_ERR_REOPEN)
2145 wimlib_free(dest_w);
2146 if (additional_swms) {
2147 for (unsigned i = 0; i < num_additional_swms; i++)
2148 wimlib_free(additional_swms[i]);
2149 free(additional_swms);
2155 is_root_wim_path(const tchar *path)
2158 for (p = path; *p; p++)
2159 if (*p != T('\\') && *p != T('/'))
2165 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2166 const tchar *dest_dir)
2168 for (size_t i = 0; i < num_cmds; i++)
2169 if (cmds[i].fs_dest_path != dest_dir)
2170 free(cmds[i].fs_dest_path);
2174 static struct wimlib_extract_command *
2175 prepare_extract_commands(tchar **paths, unsigned num_paths,
2176 int extract_flags, tchar *dest_dir,
2177 size_t *num_cmds_ret)
2179 struct wimlib_extract_command *cmds;
2181 tchar *emptystr = T("");
2183 if (num_paths == 0) {
2187 num_cmds = num_paths;
2188 cmds = calloc(num_cmds, sizeof(cmds[0]));
2190 imagex_error(T("Out of memory!"));
2194 for (size_t i = 0; i < num_cmds; i++) {
2195 cmds[i].extract_flags = extract_flags;
2196 cmds[i].wim_source_path = paths[i];
2197 if (is_root_wim_path(paths[i])) {
2198 cmds[i].fs_dest_path = dest_dir;
2200 size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2201 cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2202 if (!cmds[i].fs_dest_path) {
2203 free_extract_commands(cmds, num_cmds, dest_dir);
2206 tsprintf(cmds[i].fs_dest_path, T("%"TS"/%"TS), dest_dir,
2207 tbasename(paths[i]));
2210 *num_cmds_ret = num_cmds;
2214 /* Extract files or directories from a WIM image */
2216 imagex_extract(int argc, tchar **argv)
2219 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2223 const tchar *wimfile;
2224 const tchar *image_num_or_name;
2225 tchar *dest_dir = T(".");
2226 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL | WIMLIB_EXTRACT_FLAG_NORPFIX;
2228 const tchar *swm_glob = NULL;
2229 WIMStruct **additional_swms = NULL;
2230 unsigned num_additional_swms = 0;
2232 struct wimlib_extract_command *cmds;
2235 for_opt(c, extract_options) {
2237 case IMAGEX_CHECK_OPTION:
2238 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2240 case IMAGEX_VERBOSE_OPTION:
2241 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
2243 case IMAGEX_REF_OPTION:
2246 case IMAGEX_UNIX_DATA_OPTION:
2247 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2249 case IMAGEX_NO_ACLS_OPTION:
2250 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2252 case IMAGEX_STRICT_ACLS_OPTION:
2253 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2255 case IMAGEX_DEST_DIR_OPTION:
2258 case IMAGEX_TO_STDOUT_OPTION:
2259 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2260 imagex_be_quiet = true;
2273 image_num_or_name = argv[1];
2278 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2285 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2289 image = wimlib_resolve_image(wim, image_num_or_name);
2290 ret = verify_image_exists_and_is_single(image,
2294 goto out_wimlib_free;
2297 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2299 &num_additional_swms);
2301 goto out_wimlib_free;
2305 win32_acquire_restore_privileges();
2308 ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
2309 additional_swms, num_additional_swms,
2310 imagex_progress_func);
2312 if (!imagex_be_quiet)
2313 tprintf(T("Done extracting files.\n"));
2314 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2315 tfprintf(stderr, T("Note: You can use `"IMAGEX_PROGNAME" dir' to see what "
2316 "files and directories\n"
2317 " are in the WIM image.\n"));
2320 win32_release_restore_privileges();
2322 if (additional_swms) {
2323 for (unsigned i = 0; i < num_additional_swms; i++)
2324 wimlib_free(additional_swms[i]);
2325 free(additional_swms);
2330 free_extract_commands(cmds, num_cmds, dest_dir);
2339 /* Prints information about a WIM file; also can mark an image as bootable,
2340 * change the name of an image, or change the description of an image. */
2342 imagex_info(int argc, tchar **argv)
2347 bool header = false;
2348 bool lookup_table = false;
2350 bool metadata = false;
2351 bool short_header = true;
2352 const tchar *xml_out_file = NULL;
2353 const tchar *wimfile;
2354 const tchar *image_num_or_name = T("all");
2355 const tchar *new_name = NULL;
2356 const tchar *new_desc = NULL;
2361 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2366 for_opt(c, info_options) {
2368 case IMAGEX_BOOT_OPTION:
2371 case IMAGEX_CHECK_OPTION:
2374 case IMAGEX_HEADER_OPTION:
2376 short_header = false;
2378 case IMAGEX_LOOKUP_TABLE_OPTION:
2379 lookup_table = true;
2380 short_header = false;
2382 case IMAGEX_XML_OPTION:
2384 short_header = false;
2386 case IMAGEX_EXTRACT_XML_OPTION:
2387 xml_out_file = optarg;
2388 short_header = false;
2390 case IMAGEX_METADATA_OPTION:
2392 short_header = false;
2402 if (argc == 0 || argc > 4) {
2408 image_num_or_name = argv[1];
2418 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2420 ret = wimlib_open_wim(wimfile, open_flags, &w,
2421 imagex_progress_func);
2425 part_number = wimlib_get_part_number(w, &total_parts);
2427 image = wimlib_resolve_image(w, image_num_or_name);
2428 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
2429 imagex_error(T("The image \"%"TS"\" does not exist"),
2432 imagex_error(T("If you would like to set the boot "
2433 "index to 0, specify image \"0\" with "
2434 "the --boot flag."));
2436 ret = WIMLIB_ERR_INVALID_IMAGE;
2440 num_images = wimlib_get_num_images(w);
2442 if (num_images == 0) {
2444 imagex_error(T("--boot is meaningless on a WIM with no "
2446 ret = WIMLIB_ERR_INVALID_IMAGE;
2451 if (image == WIMLIB_ALL_IMAGES && num_images > 1) {
2453 imagex_error(T("Cannot specify the --boot flag "
2454 "without specifying a specific "
2455 "image in a multi-image WIM"));
2456 ret = WIMLIB_ERR_INVALID_IMAGE;
2460 imagex_error(T("Cannot specify the NEW_NAME "
2461 "without specifying a specific "
2462 "image in a multi-image WIM"));
2463 ret = WIMLIB_ERR_INVALID_IMAGE;
2468 /* Operations that print information are separated from operations that
2469 * recreate the WIM file. */
2470 if (!new_name && !boot) {
2472 /* Read-only operations */
2474 if (image == WIMLIB_NO_IMAGE) {
2475 imagex_error(T("\"%"TS"\" is not a valid image"),
2477 ret = WIMLIB_ERR_INVALID_IMAGE;
2481 if (image == WIMLIB_ALL_IMAGES && short_header)
2482 wimlib_print_wim_information(w);
2485 wimlib_print_header(w);
2488 if (total_parts != 1) {
2489 tprintf(T("Warning: Only showing the lookup table "
2490 "for part %d of a %d-part WIM.\n"),
2491 part_number, total_parts);
2493 wimlib_print_lookup_table(w);
2497 ret = wimlib_extract_xml_data(w, stdout);
2503 fp = tfopen(xml_out_file, T("wb"));
2505 imagex_error_with_errno(T("Failed to open the "
2506 "file \"%"TS"\" for "
2512 ret = wimlib_extract_xml_data(w, fp);
2513 if (fclose(fp) != 0) {
2514 imagex_error(T("Failed to close the file "
2525 wimlib_print_available_images(w, image);
2528 ret = wimlib_print_metadata(w, image);
2534 /* Modification operations */
2535 if (total_parts != 1) {
2536 imagex_error(T("Modifying a split WIM is not supported."));
2540 if (image == WIMLIB_ALL_IMAGES)
2543 if (image == WIMLIB_NO_IMAGE && new_name) {
2544 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
2545 "when using image 0"), new_name);
2551 if (image == wimlib_get_boot_idx(w)) {
2552 tprintf(T("Image %d is already marked as "
2553 "bootable.\n"), image);
2556 tprintf(T("Marking image %d as bootable.\n"),
2558 wimlib_set_boot_idx(w, image);
2562 if (!tstrcmp(wimlib_get_image_name(w, image), new_name))
2564 tprintf(T("Image %d is already named \"%"TS"\".\n"),
2568 tprintf(T("Changing the name of image %d to "
2569 "\"%"TS"\".\n"), image, new_name);
2570 ret = wimlib_set_image_name(w, image, new_name);
2576 const tchar *old_desc;
2577 old_desc = wimlib_get_image_description(w, image);
2578 if (old_desc && !tstrcmp(old_desc, new_desc)) {
2579 tprintf(T("The description of image %d is already "
2580 "\"%"TS"\".\n"), image, new_desc);
2583 tprintf(T("Changing the description of image %d "
2584 "to \"%"TS"\".\n"), image, new_desc);
2585 ret = wimlib_set_image_descripton(w, image,
2592 /* Only call wimlib_overwrite() if something actually needs to
2594 if (boot || new_name || new_desc ||
2595 (check && !wimlib_has_integrity_table(w)))
2599 ret = file_writable(wimfile);
2604 write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2608 ret = wimlib_overwrite(w, write_flags, 1,
2609 imagex_progress_func);
2610 if (ret == WIMLIB_ERR_REOPEN)
2613 tprintf(T("The file \"%"TS"\" was not modified because nothing "
2614 "needed to be done.\n"), wimfile);
2623 /* Join split WIMs into one part WIM */
2625 imagex_join(int argc, tchar **argv)
2628 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2629 int wim_write_flags = 0;
2630 const tchar *output_path;
2632 for_opt(c, join_options) {
2634 case IMAGEX_CHECK_OPTION:
2635 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2636 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2646 imagex_error(T("Must specify one or more split WIM (.swm) "
2650 output_path = argv[0];
2651 return wimlib_join((const tchar * const *)++argv,
2656 imagex_progress_func);
2662 /* Mounts an image using a FUSE mount. */
2664 imagex_mount_rw_or_ro(int argc, tchar **argv)
2667 int mount_flags = 0;
2668 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2669 const tchar *wimfile;
2675 const tchar *swm_glob = NULL;
2676 WIMStruct **additional_swms = NULL;
2677 unsigned num_additional_swms = 0;
2678 const tchar *staging_dir = NULL;
2680 if (!tstrcmp(argv[0], T("mountrw")))
2681 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
2683 for_opt(c, mount_options) {
2685 case IMAGEX_ALLOW_OTHER_OPTION:
2686 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
2688 case IMAGEX_CHECK_OPTION:
2689 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2691 case IMAGEX_DEBUG_OPTION:
2692 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
2694 case IMAGEX_STREAMS_INTERFACE_OPTION:
2695 if (!tstrcasecmp(optarg, T("none")))
2696 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
2697 else if (!tstrcasecmp(optarg, T("xattr")))
2698 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2699 else if (!tstrcasecmp(optarg, T("windows")))
2700 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
2702 imagex_error(T("Unknown stream interface \"%"TS"\""),
2707 case IMAGEX_REF_OPTION:
2710 case IMAGEX_STAGING_DIR_OPTION:
2711 staging_dir = optarg;
2713 case IMAGEX_UNIX_DATA_OPTION:
2714 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
2722 if (argc != 2 && argc != 3)
2727 ret = wimlib_open_wim(wimfile, open_flags, &w,
2728 imagex_progress_func);
2733 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2735 &num_additional_swms);
2742 num_images = wimlib_get_num_images(w);
2743 if (num_images != 1) {
2744 imagex_error(T("\"%"TS"\" contains %d images; Please "
2745 "select one."), wimfile, num_images);
2746 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2753 image = wimlib_resolve_image(w, argv[1]);
2755 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
2760 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2761 ret = file_writable(wimfile);
2766 ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
2767 num_additional_swms, staging_dir);
2769 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
2771 image, wimfile, dir);
2776 if (additional_swms) {
2777 for (unsigned i = 0; i < num_additional_swms; i++)
2778 wimlib_free(additional_swms[i]);
2779 free(additional_swms);
2783 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2788 /* Rebuild a WIM file */
2790 imagex_optimize(int argc, tchar **argv)
2794 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
2797 const tchar *wimfile;
2800 unsigned num_threads = 0;
2802 for_opt(c, optimize_options) {
2804 case IMAGEX_CHECK_OPTION:
2805 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2806 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2808 case IMAGEX_RECOMPRESS_OPTION:
2809 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
2811 case IMAGEX_THREADS_OPTION:
2812 num_threads = parse_num_threads(optarg);
2813 if (num_threads == UINT_MAX)
2831 ret = file_writable(wimfile);
2835 ret = wimlib_open_wim(wimfile, open_flags, &w,
2836 imagex_progress_func);
2840 old_size = file_get_size(argv[0]);
2841 tprintf(T("\"%"TS"\" original size: "), wimfile);
2843 tfputs(T("Unknown\n"), stdout);
2845 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
2847 ret = wimlib_overwrite(w, write_flags, num_threads,
2848 imagex_progress_func);
2851 new_size = file_get_size(argv[0]);
2852 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
2854 tfputs(T("Unknown\n"), stdout);
2856 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
2858 tfputs(T("Space saved: "), stdout);
2859 if (new_size != -1 && old_size != -1) {
2860 tprintf(T("%lld KiB\n"),
2861 ((long long)old_size - (long long)new_size) >> 10);
2863 tfputs(T("Unknown\n"), stdout);
2871 /* Split a WIM into a spanned set */
2873 imagex_split(int argc, tchar **argv)
2876 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2877 int write_flags = 0;
2878 unsigned long part_size;
2883 for_opt(c, split_options) {
2885 case IMAGEX_CHECK_OPTION:
2886 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2887 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2901 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
2902 if (tmp == argv[2] || *tmp) {
2903 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
2904 imagex_error(T("The part size must be an integer or "
2905 "floating-point number of megabytes."));
2908 ret = wimlib_open_wim(argv[0], open_flags, &w, imagex_progress_func);
2911 ret = wimlib_split(w, argv[1], part_size, write_flags, imagex_progress_func);
2916 /* Unmounts a mounted WIM image. */
2918 imagex_unmount(int argc, tchar **argv)
2921 int unmount_flags = 0;
2924 for_opt(c, unmount_options) {
2926 case IMAGEX_COMMIT_OPTION:
2927 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
2929 case IMAGEX_CHECK_OPTION:
2930 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
2932 case IMAGEX_REBUILD_OPTION:
2933 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
2935 case IMAGEX_LAZY_OPTION:
2936 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
2950 ret = wimlib_unmount_image(argv[0], unmount_flags,
2951 imagex_progress_func);
2953 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
2958 * Add, delete, or rename files in a WIM image.
2961 imagex_update(int argc, tchar **argv)
2963 const tchar *wimfile;
2964 const tchar *image_num_or_name;
2969 int write_flags = 0;
2970 int update_flags = 0;
2971 int default_add_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
2972 int default_delete_flags = 0;
2973 unsigned num_threads = 0;
2975 tchar *cmd_file_contents;
2976 size_t cmd_file_nchars;
2977 struct wimlib_update_command *cmds;
2981 const tchar *config_file = NULL;
2983 struct wimlib_capture_config *config = NULL;
2985 for_opt(c, update_options) {
2987 /* Generic or write options */
2988 case IMAGEX_THREADS_OPTION:
2989 num_threads = parse_num_threads(optarg);
2990 if (num_threads == UINT_MAX) {
2995 case IMAGEX_CHECK_OPTION:
2996 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2997 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2999 case IMAGEX_REBUILD_OPTION:
3000 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3003 /* Default delete options */
3004 case IMAGEX_FORCE_OPTION:
3005 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3007 case IMAGEX_RECURSIVE_OPTION:
3008 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3011 /* Global add option */
3012 case IMAGEX_CONFIG_OPTION:
3013 config_file = optarg;
3016 /* Default add options */
3017 case IMAGEX_VERBOSE_OPTION:
3018 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
3020 case IMAGEX_DEREFERENCE_OPTION:
3021 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
3023 case IMAGEX_UNIX_DATA_OPTION:
3024 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
3026 case IMAGEX_NO_ACLS_OPTION:
3027 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
3029 case IMAGEX_STRICT_ACLS_OPTION:
3030 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
3039 if (argc < 1 || argc > 2)
3043 ret = file_writable(wimfile);
3047 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3052 image_num_or_name = argv[1];
3054 image_num_or_name = T("1");
3056 image = wimlib_resolve_image(wim, image_num_or_name);
3058 ret = verify_image_exists_and_is_single(image, image_num_or_name,
3061 goto out_wimlib_free;
3063 num_images = wimlib_get_num_images(wim);
3064 if (argc == 1 && num_images != 1) {
3065 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3066 wimfile, num_images);
3069 goto out_wimlib_free;
3072 /* Parse capture configuration file if specified */
3076 config_str = file_get_text_contents(config_file, &config_len);
3079 goto out_wimlib_free;
3082 config = alloca(sizeof(*config));
3083 ret = parse_capture_config(&config_str, config_len, config);
3085 goto out_free_config;
3087 config = &default_capture_config;
3090 /* Read update commands from standard input */
3091 if (isatty(STDIN_FILENO)) {
3092 tputs(T("Reading update commands from standard input..."));
3093 recommend_man_page(T("update"));
3095 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3096 if (!cmd_file_contents) {
3098 goto out_free_config;
3101 /* Parse the update commands */
3102 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3106 goto out_free_cmd_file_contents;
3109 /* Set default flags and capture config on the update commands */
3110 for (size_t i = 0; i < num_cmds; i++) {
3111 switch (cmds[i].op) {
3112 case WIMLIB_UPDATE_OP_ADD:
3113 cmds[i].add.add_flags |= default_add_flags;
3114 cmds[i].add.config = config;
3116 case WIMLIB_UPDATE_OP_DELETE:
3117 cmds[i].delete.delete_flags |= default_delete_flags;
3125 win32_acquire_capture_privileges();
3128 /* Execute the update commands */
3129 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3130 imagex_progress_func);
3132 goto out_release_privs;
3134 /* Overwrite the updated WIM */
3135 ret = wimlib_overwrite(wim, write_flags, num_threads,
3136 imagex_progress_func);
3139 win32_release_capture_privileges();
3143 out_free_cmd_file_contents:
3144 free(cmd_file_contents);
3146 if (config != NULL && config != &default_capture_config) {
3147 free(config->exclusion_pats.pats);
3148 free(config->exclusion_exception_pats.pats);
3161 struct imagex_command {
3163 int (*func)(int , tchar **);
3168 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
3169 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
3171 static const struct imagex_command imagex_commands[] = {
3172 {T("append"), imagex_capture_or_append, APPEND},
3173 {T("apply"), imagex_apply, APPLY},
3174 {T("capture"), imagex_capture_or_append, CAPTURE},
3175 {T("delete"), imagex_delete, DELETE},
3176 {T("dir"), imagex_dir, DIR},
3177 {T("export"), imagex_export, EXPORT},
3178 {T("extract"), imagex_extract, EXTRACT},
3179 {T("info"), imagex_info, INFO},
3180 {T("join"), imagex_join, JOIN},
3181 {T("mount"), imagex_mount_rw_or_ro, MOUNT},
3182 {T("mountrw"), imagex_mount_rw_or_ro, MOUNTRW},
3183 {T("optimize"),imagex_optimize, OPTIMIZE},
3184 {T("split"), imagex_split, SPLIT},
3185 {T("unmount"), imagex_unmount, UNMOUNT},
3186 {T("update"), imagex_update, UPDATE},
3192 static const tchar *s =
3194 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
3195 "Copyright (C) 2012, 2013 Eric Biggers\n"
3196 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
3197 "This is free software: you are free to change and redistribute it.\n"
3198 "There is NO WARRANTY, to the extent permitted by law.\n"
3200 "Report bugs to "PACKAGE_BUGREPORT".\n"
3207 help_or_version(int argc, tchar **argv)
3211 const struct imagex_command *cmd;
3213 for (i = 1; i < argc; i++) {
3221 if (!tstrcmp(p, T("help"))) {
3222 for_imagex_command(cmd) {
3223 if (!tstrcmp(cmd->name, argv[1])) {
3231 if (!tstrcmp(p, T("version"))) {
3242 const struct imagex_command *cmd;
3243 tprintf(T("Usage:\n%"TS), usage_strings[cmd_type]);
3244 for_imagex_command(cmd) {
3245 if (cmd->cmd == cmd_type) {
3246 tputc(T('\n'), stdout);
3247 recommend_man_page(cmd->name);
3255 tfputs(T("Usage:\n"), stdout);
3256 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
3257 tprintf(T(" %"TS), usage_strings[i]);
3258 static const tchar *extra =
3260 " "IMAGEX_PROGNAME" --help\n"
3261 " "IMAGEX_PROGNAME" --version\n"
3263 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
3266 tfputs(extra, stdout);
3267 recommend_man_page(T(""));
3270 /* Entry point for wimlib's ImageX implementation. On UNIX the command
3271 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
3272 * something else), while an Windows the command arguments will be UTF-16LE
3273 * encoded 'wchar_t' strings. */
3276 wmain(int argc, wchar_t **argv, wchar_t **envp)
3278 main(int argc, char **argv)
3281 const struct imagex_command *cmd;
3286 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
3287 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
3291 setlocale(LC_ALL, "");
3292 codeset = nl_langinfo(CODESET);
3293 if (!strstr(codeset, "UTF-8") &&
3294 !strstr(codeset, "UTF8") &&
3295 !strstr(codeset, "utf-8") &&
3296 !strstr(codeset, "utf8"))
3299 "WARNING: Running "IMAGEX_PROGNAME" in a UTF-8 locale is recommended!\n"
3300 " Maybe try: `export LANG=en_US.UTF-8'?\n"
3301 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
3302 " to any value to force wimlib to use UTF-8.\n",
3307 #endif /* !__WIN32__ */
3310 imagex_error(T("No command specified"));
3316 /* Handle --help and --version for all commands. Note that this will
3317 * not return if either of these arguments are present. */
3318 help_or_version(argc, argv);
3322 /* The user may like to see more informative error messages. */
3323 wimlib_set_print_errors(true);
3325 /* Do any initializations that the library needs */
3326 ret = wimlib_global_init(init_flags);
3328 goto out_check_status;
3330 /* Search for the function to handle the ImageX subcommand. */
3331 for_imagex_command(cmd) {
3332 if (!tstrcmp(cmd->name, *argv)) {
3333 ret = cmd->func(argc, argv);
3334 goto out_check_write_error;
3338 imagex_error(T("Unrecognized command: `%"TS"'"), argv[0]);
3342 out_check_write_error:
3343 /* For 'wimlib-imagex info' and 'wimlib-imagex dir', data printed to
3344 * standard output is part of the program's actual behavior and not just
3345 * for informational purposes, so we should set a failure exit status if
3346 * there was a write error. */
3347 if (cmd == &imagex_commands[INFO] || cmd == &imagex_commands[DIR]) {
3348 if (ferror(stdout) || fclose(stdout)) {
3349 imagex_error_with_errno(T("error writing to standard output"));
3355 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
3356 * outside of the wimlib library code. 0 indicates success. > 0
3357 * indicates a wimlib error code from which an error message can be
3360 imagex_error(T("Exiting with error code %d:\n"
3362 wimlib_get_error_string(ret));
3363 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
3364 imagex_error_with_errno(T("errno"));
3367 /* Make the library free any resources it's holding (not strictly
3368 * necessary because the process is ending anyway). */
3369 wimlib_global_cleanup();