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
53 # define OS_PREFERRED_PATH_SEPARATOR L'\\'
54 # define OS_PREFERRED_PATH_SEPARATOR_STRING L"\\"
58 # include <langinfo.h>
59 # define tbasename basename
61 # define OS_PREFERRED_PATH_SEPARATOR '/'
62 # define OS_PREFERRED_PATH_SEPARATOR_STRING "/"
66 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
68 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
89 static void usage(int cmd_type);
90 static void usage_all(void);
92 static bool imagex_be_quiet = false;
95 static const tchar *usage_strings[] = {
98 IMAGEX_PROGNAME" append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
99 " [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
100 " [--verbose] [--dereference] [--config=FILE]\n"
101 " [--threads=NUM_THREADS] [--rebuild] [--unix-data]\n"
102 " [--source-list] [--no-acls] [--strict-acls]\n"
103 " [--rpfix] [--norpfix]\n"
107 IMAGEX_PROGNAME" apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
108 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
109 " [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n"
110 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
111 " [--include-invalid-names]\n"
115 IMAGEX_PROGNAME" capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
116 " [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
117 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
118 " [--config=FILE] [--threads=NUM_THREADS] [--unix-data]\n"
119 " [--source-list] [--no-acls] [--strict-acls]\n"
120 " [--rpfix] [--norpfix]\n"
124 IMAGEX_PROGNAME" delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check] [--soft]\n"
128 IMAGEX_PROGNAME" dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n"
132 IMAGEX_PROGNAME" export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
133 " DEST_WIMFILE [DEST_IMAGE_NAME] [DEST_IMAGE_DESCRIPTION]\n"
134 " [--boot] [--check] [--compress=TYPE] [--ref=\"GLOB\"]\n"
135 " [--threads=NUM_THREADS] [--rebuild]\n"
139 IMAGEX_PROGNAME" extract WIMFILE (IMAGE_NUM | IMAGE_NAME) [PATH...]\n"
140 " [--check] [--ref=\"GLOB\"] [--verbose] [--unix-data]\n"
141 " [--no-acls] [--strict-acls] [--to-stdout] [--dest-dir=DIR]\n"
142 " [--include-invalid-names]\n"
146 IMAGEX_PROGNAME" info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
147 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
148 " [--xml] [--extract-xml FILE] [--metadata]\n"
152 IMAGEX_PROGNAME" join [--check] WIMFILE SPLIT_WIM...\n"
156 IMAGEX_PROGNAME" mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
157 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
158 " [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n"
162 IMAGEX_PROGNAME" mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
163 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
164 " [--staging-dir=DIR] [--unix-data] [--allow-other]\n"
168 IMAGEX_PROGNAME" optimize WIMFILE [--check] [--recompress]\n"
169 " [--threads=NUM_THREADS]\n"
173 IMAGEX_PROGNAME" split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n"
177 IMAGEX_PROGNAME" unmount DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
181 IMAGEX_PROGNAME" update WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
182 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
183 " [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
189 recommend_man_page(const tchar *cmd_name)
191 const tchar *format_str;
193 format_str = T("See "IMAGEX_PROGNAME"%"TS"%"TS".pdf in the "
194 "doc directory for more details.\n");
196 format_str = T("Try `man "IMAGEX_PROGNAME"%"TS"%"TS"' "
197 "for more details.\n");
199 tprintf(format_str, *cmd_name ? T("-") : T(""), cmd_name);
203 IMAGEX_ALLOW_OTHER_OPTION,
206 IMAGEX_COMMAND_OPTION,
207 IMAGEX_COMMIT_OPTION,
208 IMAGEX_COMPRESS_OPTION,
209 IMAGEX_CONFIG_OPTION,
211 IMAGEX_DEREFERENCE_OPTION,
212 IMAGEX_DEST_DIR_OPTION,
213 IMAGEX_EXTRACT_XML_OPTION,
216 IMAGEX_HARDLINK_OPTION,
217 IMAGEX_HEADER_OPTION,
218 IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
220 IMAGEX_LOOKUP_TABLE_OPTION,
221 IMAGEX_METADATA_OPTION,
222 IMAGEX_NORPFIX_OPTION,
223 IMAGEX_NO_ACLS_OPTION,
224 IMAGEX_REBUILD_OPTION,
225 IMAGEX_RECOMPRESS_OPTION,
226 IMAGEX_RECURSIVE_OPTION,
230 IMAGEX_SOURCE_LIST_OPTION,
231 IMAGEX_STAGING_DIR_OPTION,
232 IMAGEX_STREAMS_INTERFACE_OPTION,
233 IMAGEX_STRICT_ACLS_OPTION,
234 IMAGEX_SYMLINK_OPTION,
235 IMAGEX_THREADS_OPTION,
236 IMAGEX_TO_STDOUT_OPTION,
237 IMAGEX_UNIX_DATA_OPTION,
238 IMAGEX_VERBOSE_OPTION,
242 static const struct option apply_options[] = {
243 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
244 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
245 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
246 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
247 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
248 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
249 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
250 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
251 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
252 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
253 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
254 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
257 static const struct option capture_or_append_options[] = {
258 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
259 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
260 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
261 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
262 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
263 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
264 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
265 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
266 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
267 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
268 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
269 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
270 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
271 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
272 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
273 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
276 static const struct option delete_options[] = {
277 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
278 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
282 static const struct option export_options[] = {
283 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
284 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
285 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
286 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
287 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
288 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
292 static const struct option extract_options[] = {
293 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
294 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
295 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
296 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
297 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
298 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
299 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
300 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
301 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
302 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
306 static const struct option info_options[] = {
307 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
308 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
309 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
310 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
311 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
312 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
313 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
317 static const struct option join_options[] = {
318 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
322 static const struct option mount_options[] = {
323 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
324 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
325 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
326 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
327 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
328 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
329 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
333 static const struct option optimize_options[] = {
334 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
335 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
336 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
340 static const struct option split_options[] = {
341 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
345 static const struct option unmount_options[] = {
346 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
347 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
348 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
349 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
353 static const struct option update_options[] = {
354 /* Careful: some of the options here set the defaults for update
355 * commands, but the flags given to an actual update command (and not to
356 * `imagex update' itself are also handled in
357 * update_command_add_option(). */
358 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
359 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
360 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
361 {T("command"), required_argument, NULL, IMAGEX_COMMAND_OPTION},
363 /* Default delete options */
364 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
365 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
367 /* Global add option */
368 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
370 /* Default add options */
371 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
372 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
373 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
374 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
375 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
376 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
381 /* Print formatted error message to stderr. */
383 imagex_error(const tchar *format, ...)
386 va_start(va, format);
387 tfputs(T("ERROR: "), stderr);
388 tvfprintf(stderr, format, va);
389 tputc(T('\n'), stderr);
393 /* Print formatted error message to stderr. */
395 imagex_error_with_errno(const tchar *format, ...)
397 int errno_save = errno;
399 va_start(va, format);
400 tfputs(T("ERROR: "), stderr);
401 tvfprintf(stderr, format, va);
402 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
407 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
409 if (image == WIMLIB_NO_IMAGE) {
410 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
411 " Please specify a 1-based image index or "
413 " You may use `"IMAGEX_PROGNAME" info' to list the images "
414 "contained in a WIM."),
415 image_name, wim_name);
422 verify_image_is_single(int image)
424 if (image == WIMLIB_ALL_IMAGES) {
425 imagex_error(T("Cannot specify all images for this action!"));
432 verify_image_exists_and_is_single(int image, const tchar *image_name,
433 const tchar *wim_name)
436 ret = verify_image_exists(image, image_name, wim_name);
438 ret = verify_image_is_single(image);
442 /* Parse the argument to --compress */
444 get_compression_type(const tchar *optarg)
446 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
447 return WIMLIB_COMPRESSION_TYPE_LZX;
448 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
449 return WIMLIB_COMPRESSION_TYPE_XPRESS;
450 else if (!tstrcasecmp(optarg, T("none")))
451 return WIMLIB_COMPRESSION_TYPE_NONE;
453 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
454 "\"maximum\", \"fast\", or \"none\"."), optarg);
455 return WIMLIB_COMPRESSION_TYPE_INVALID;
459 /* Returns the size of a file given its name, or -1 if the file does not exist
460 * or its size cannot be determined. */
462 file_get_size(const tchar *filename)
465 if (tstat(filename, &st) == 0)
471 tchar pat_ntfs_log[] = T("/$ntfs.log");
472 tchar pat_hiberfil_sys[] = T("/hiberfil.sys");
473 tchar pat_pagefile_sys[] = T("/pagefile.sys");
474 tchar pat_system_volume_information[] = T("/System Volume Information");
475 tchar pat_recycler[] = T("/RECYCLER");
476 tchar pat_windows_csc[] = T("/Windows/CSC");
478 tchar *default_pats[] = {
482 pat_system_volume_information,
487 static struct wimlib_capture_config default_capture_config = {
489 .num_pats = sizeof(default_pats) / sizeof(*default_pats),
490 .pats = default_pats,
495 PARSE_STRING_SUCCESS = 0,
496 PARSE_STRING_FAILURE = 1,
497 PARSE_STRING_NONE = 2,
501 * Parses a string token from an array of characters.
503 * Tokens are either whitespace-delimited, or double or single-quoted.
505 * @line_p: Pointer to the pointer to the line of data. Will be updated
506 * to point past the string token iff the return value is
507 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
510 * @len_p: @len_p initially stores the length of the line of data, which may
511 * be 0, and it will be updated to the number of bytes remaining in
512 * the line iff the return value is PARSE_STRING_SUCCESS.
514 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
515 * parsed string token will be returned here.
517 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
518 * PARSE_STRING_FAILURE if the data was invalid due to a missing
519 * closing quote; or PARSE_STRING_NONE if the line ended before the
520 * beginning of a string token was found.
523 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
526 tchar *line = *line_p;
530 /* Skip leading whitespace */
533 return PARSE_STRING_NONE;
534 if (!istspace(*line) && *line != T('\0'))
540 if (quote_char == T('"') || quote_char == T('\'')) {
545 line = tmemchr(line, quote_char, len);
547 imagex_error(T("Missing closing quote: %"TS), fn - 1);
548 return PARSE_STRING_FAILURE;
551 /* Unquoted string. Go until whitespace. Line is terminated
552 * by '\0', so no need to check 'len'. */
556 } while (!istspace(*line) && *line != T('\0'));
563 return PARSE_STRING_SUCCESS;
566 /* Parses a line of data (not an empty line or comment) in the source list file
567 * format. (See the man page for 'wimlib-imagex capture' for details on this
568 * format and the meaning.)
570 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
571 * len == 0. The data in @line will be modified by this function call.
573 * @len: Length of the line of data.
575 * @source: On success, the capture source and target described by the line is
576 * written into this destination. Note that it will contain pointers
577 * to data in the @line array.
579 * Returns true if the line was valid; false otherwise. */
581 parse_source_list_line(tchar *line, size_t len,
582 struct wimlib_capture_source *source)
586 ret = parse_string(&line, &len, &source->fs_source_path);
587 if (ret != PARSE_STRING_SUCCESS)
589 ret = parse_string(&line, &len, &source->wim_target_path);
590 if (ret == PARSE_STRING_NONE)
591 source->wim_target_path = source->fs_source_path;
592 return ret != PARSE_STRING_FAILURE;
595 /* Returns %true if the given line of length @len > 0 is a comment or empty line
596 * in the source list file format. */
598 is_comment_line(const tchar *line, size_t len)
603 if (!istspace(*line) && *line != T('\0'))
613 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
616 tchar *contents = *contents_p;
617 size_t nchars = *nchars_p;
620 for (i = 0; i < nchars; i++)
621 if (contents[i] == T('\n'))
624 /* Handle last line not terminated by a newline */
625 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
626 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
628 imagex_error(T("Out of memory!"));
631 contents[nchars] = T('\n');
632 *contents_p = contents;
640 /* Parses a file in the source list format. (See the man page for
641 * 'wimlib-imagex capture' for details on this format and the meaning.)
643 * @source_list_contents: Contents of the source list file. Note that this
644 * buffer will be modified to save memory allocations,
645 * and cannot be freed until the returned array of
646 * wimlib_capture_source's has also been freed.
648 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
651 * @nsources_ret: On success, the length of the returned array is
654 * Returns: An array of `struct wimlib_capture_source's that can be passed to
655 * the wimlib_add_image_multisource() function to specify how a WIM image is to
657 static struct wimlib_capture_source *
658 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
659 size_t *nsources_ret)
663 struct wimlib_capture_source *sources;
666 nlines = text_file_count_lines(source_list_contents_p,
667 &source_list_nchars);
671 /* Always allocate at least 1 slot, just in case the implementation of
672 * calloc() returns NULL if 0 bytes are requested. */
673 sources = calloc(nlines ?: 1, sizeof(*sources));
675 imagex_error(T("out of memory"));
678 p = *source_list_contents_p;
680 for (i = 0; i < nlines; i++) {
681 /* XXX: Could use rawmemchr() here instead, but it may not be
682 * available on all platforms. */
683 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
684 size_t len = endp - p + 1;
686 if (!is_comment_line(p, len)) {
687 if (!parse_source_list_line(p, len, &sources[j++])) {
700 enum capture_config_section {
701 CAPTURE_CONFIG_NO_SECTION,
702 CAPTURE_CONFIG_EXCLUSION_SECTION,
703 CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
704 CAPTURE_CONFIG_IGNORE_SECTION,
708 CAPTURE_CONFIG_INVALID_SECTION,
709 CAPTURE_CONFIG_CHANGED_SECTION,
710 CAPTURE_CONFIG_SAME_SECTION,
714 check_config_section(tchar *line, size_t len,
715 enum capture_config_section *cur_section)
717 while (istspace(*line))
721 return CAPTURE_CONFIG_SAME_SECTION;
724 tchar *endbrace = tstrrchr(line, T(']'));
726 return CAPTURE_CONFIG_SAME_SECTION;
728 if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
729 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
730 } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
731 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
732 } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
733 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
734 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
735 "of capture config file\n"),
737 } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
738 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
739 tfputs(T("WARNING: Ignoring [AlignmentList] section "
740 "of capture config file\n"),
743 imagex_error(T("Invalid capture config file section \"%"TS"\""),
745 return CAPTURE_CONFIG_INVALID_SECTION;
747 return CAPTURE_CONFIG_CHANGED_SECTION;
752 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
755 if (pat_list->num_pats == pat_list->num_allocated_pats) {
757 size_t num_allocated_pats = pat_list->num_pats + 8;
759 pats = realloc(pat_list->pats,
760 num_allocated_pats * sizeof(pat_list->pats[0]));
762 imagex_error(T("Out of memory!"));
765 pat_list->pats = pats;
766 pat_list->num_allocated_pats = num_allocated_pats;
768 pat_list->pats[pat_list->num_pats++] = pat;
773 parse_capture_config_line(tchar *line, size_t len,
774 enum capture_config_section *cur_section,
775 struct wimlib_capture_config *config)
780 ret = check_config_section(line, len, cur_section);
781 if (ret == CAPTURE_CONFIG_INVALID_SECTION)
783 if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
786 switch (*cur_section) {
787 case CAPTURE_CONFIG_NO_SECTION:
788 imagex_error(T("Line \"%"TS"\" is not in a section "
789 "(such as [ExclusionList]"), line);
791 case CAPTURE_CONFIG_EXCLUSION_SECTION:
792 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
794 return pattern_list_add_pattern(&config->exclusion_pats,
796 case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
797 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
799 return pattern_list_add_pattern(&config->exclusion_exception_pats,
801 case CAPTURE_CONFIG_IGNORE_SECTION:
808 parse_capture_config(tchar **contents_p, size_t nchars,
809 struct wimlib_capture_config *config)
814 enum capture_config_section cur_section;
816 memset(config, 0, sizeof(*config));
818 nlines = text_file_count_lines(contents_p, &nchars);
822 cur_section = CAPTURE_CONFIG_NO_SECTION;
824 for (i = 0; i < nlines; i++) {
825 tchar *endp = tmemchr(p, T('\n'), nchars);
826 size_t len = endp - p + 1;
828 if (!is_comment_line(p, len))
829 if (!parse_capture_config_line(p, len, &cur_section, config))
837 /* Reads the contents of a file into memory. */
839 file_get_contents(const tchar *filename, size_t *len_ret)
846 if (tstat(filename, &stbuf) != 0) {
847 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
852 fp = tfopen(filename, T("rb"));
854 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
860 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
861 "contents of file \"%"TS"\""), len, filename);
864 if (fread(buf, 1, len, fp) != len) {
865 imagex_error_with_errno(T("Failed to read %zu bytes from the "
866 "file \"%"TS"\""), len, filename);
880 /* Read standard input until EOF and return the full contents in a malloc()ed
881 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
884 stdin_get_contents(size_t *len_ret)
886 /* stdin can, of course, be a pipe or other non-seekable file, so the
887 * total length of the data cannot be pre-determined */
889 size_t newlen = 1024;
893 char *p = realloc(buf, newlen);
894 size_t bytes_read, bytes_to_read;
896 imagex_error(T("out of memory while reading stdin"));
900 bytes_to_read = newlen - pos;
901 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
903 if (bytes_read != bytes_to_read) {
908 imagex_error_with_errno(T("error reading stdin"));
922 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
925 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
927 *num_tchars_ret = num_bytes;
929 #else /* !__WIN32__ */
930 /* On Windows, translate the text to UTF-16LE */
934 if (num_bytes >= 2 &&
935 ((text[0] == 0xff && text[1] == 0xfe) ||
936 (text[0] <= 0x7f && text[1] == 0x00)))
938 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
939 * with something that looks like an ASCII character encoded as
940 * a UTF-16LE code unit. Assume the file is encoded as
941 * UTF-16LE. This is not a 100% reliable check. */
942 num_wchars = num_bytes / 2;
943 text_wstr = (wchar_t*)text;
945 /* File does not look like UTF-16LE. Assume it is encoded in
946 * the current Windows code page. I think these are always
947 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
948 * should work as expected. */
949 text_wstr = win32_mbs_to_wcs(text,
954 *num_tchars_ret = num_wchars;
956 #endif /* __WIN32__ */
960 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
965 contents = file_get_contents(filename, &num_bytes);
968 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
972 stdin_get_text_contents(size_t *num_tchars_ret)
977 contents = stdin_get_contents(&num_bytes);
980 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
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: "
1061 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
1062 info->scan.wim_target_path);
1064 tprintf(T(" (loading as root of WIM image)...\n"));
1067 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1068 if (info->scan.excluded)
1069 tprintf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1071 tprintf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
1073 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
1075 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1076 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1077 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1078 info->integrity.total_bytes);
1079 tprintf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1080 "of %"PRIu64" %"TS" (%u%%) done"),
1081 info->integrity.filename,
1082 info->integrity.completed_bytes >> unit_shift,
1084 info->integrity.total_bytes >> unit_shift,
1087 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1090 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1091 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1092 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1093 info->integrity.total_bytes);
1094 tprintf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1095 "of %"PRIu64" %"TS" (%u%%) done"),
1096 info->integrity.completed_bytes >> unit_shift,
1098 info->integrity.total_bytes >> unit_shift,
1101 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1104 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1105 tprintf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1106 "to %"TS" \"%"TS"\"\n"),
1107 info->extract.image,
1108 info->extract.image_name,
1109 info->extract.wimfile_name,
1110 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1111 T("NTFS volume") : T("directory")),
1112 info->extract.target);
1114 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1115 tprintf(T("Extracting "
1116 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\" from image %d (\"%"TS"\") "
1117 "in \"%"TS"\" to \"%"TS"\"\n"),
1118 info->extract.extract_root_wim_source_path,
1119 info->extract.image,
1120 info->extract.image_name,
1121 info->extract.wimfile_name,
1122 info->extract.target);
1124 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
1125 /*tprintf(T("Applying directory structure to %"TS"\n"),*/
1126 /*info->extract.target);*/
1128 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1129 percent_done = TO_PERCENT(info->extract.completed_bytes,
1130 info->extract.total_bytes);
1131 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1132 tprintf(T("\rExtracting files: "
1133 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1134 info->extract.completed_bytes >> unit_shift,
1136 info->extract.total_bytes >> unit_shift,
1139 if (info->extract.completed_bytes >= info->extract.total_bytes)
1142 case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
1143 tprintf(T("%"TS"\n"), info->extract.cur_path);
1145 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1146 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1147 tprintf(T("Setting timestamps on all extracted files...\n"));
1149 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1150 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1151 tprintf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1152 info->extract.target);
1155 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
1156 percent_done = TO_PERCENT(info->join.completed_bytes,
1157 info->join.total_bytes);
1158 unit_shift = get_unit(info->join.total_bytes, &unit_name);
1159 tprintf(T("Writing resources from part %u of %u: "
1160 "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written\n"),
1161 (info->join.completed_parts == info->join.total_parts) ?
1162 info->join.completed_parts : info->join.completed_parts + 1,
1163 info->join.total_parts,
1164 info->join.completed_bytes >> unit_shift,
1166 info->join.total_bytes >> unit_shift,
1170 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1171 percent_done = TO_PERCENT(info->split.completed_bytes,
1172 info->split.total_bytes);
1173 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1174 tprintf(T("Writing \"%"TS"\": %"PRIu64" %"TS" of "
1175 "%"PRIu64" %"TS" (%u%%) written\n"),
1176 info->split.part_name,
1177 info->split.completed_bytes >> unit_shift,
1179 info->split.total_bytes >> unit_shift,
1183 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1184 if (info->split.completed_bytes == info->split.total_bytes) {
1185 tprintf(T("Finished writing %u split WIM parts\n"),
1186 info->split.cur_part_number);
1189 case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1190 switch (info->update.command->op) {
1191 case WIMLIB_UPDATE_OP_DELETE:
1192 tprintf(T("Deleted WIM path "
1193 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1194 info->update.command->delete.wim_path);
1196 case WIMLIB_UPDATE_OP_RENAME:
1197 tprintf(T("Renamed WIM path "
1198 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
1199 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1200 info->update.command->rename.wim_source_path,
1201 info->update.command->rename.wim_target_path);
1203 case WIMLIB_UPDATE_OP_ADD:
1215 /* Open all the split WIM parts that correspond to a file glob.
1217 * @first_part specifies the first part of the split WIM and it may be either
1218 * included or omitted from the glob. */
1220 open_swms_from_glob(const tchar *swm_glob,
1221 const tchar *first_part,
1223 WIMStruct ***additional_swms_ret,
1224 unsigned *num_additional_swms_ret)
1226 unsigned num_additional_swms = 0;
1227 WIMStruct **additional_swms = NULL;
1231 /* Warning: glob() is replaced in Windows native builds */
1232 ret = tglob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
1234 if (ret == GLOB_NOMATCH) {
1235 imagex_error(T("Found no files for glob \"%"TS"\""),
1238 imagex_error_with_errno(T("Failed to process glob \"%"TS"\""),
1244 num_additional_swms = globbuf.gl_pathc;
1245 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
1246 if (!additional_swms) {
1247 imagex_error(T("Out of memory"));
1251 unsigned offset = 0;
1252 for (unsigned i = 0; i < num_additional_swms; i++) {
1253 if (tstrcmp(globbuf.gl_pathv[i], first_part) == 0) {
1257 ret = wimlib_open_wim(globbuf.gl_pathv[i],
1258 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
1259 &additional_swms[i - offset],
1260 imagex_progress_func);
1262 goto out_close_swms;
1264 *additional_swms_ret = additional_swms;
1265 *num_additional_swms_ret = num_additional_swms - offset;
1269 for (unsigned i = 0; i < num_additional_swms; i++)
1270 wimlib_free(additional_swms[i]);
1271 free(additional_swms);
1280 parse_num_threads(const tchar *optarg)
1283 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1284 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1285 imagex_error(T("Number of threads must be a non-negative integer!"));
1293 * Parse an option passed to an update command.
1295 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1298 * @option: Text string for the option (beginning with --)
1300 * @cmd: `struct wimlib_update_command' that is being constructed for
1303 * Returns true if the option was recognized; false if not.
1306 update_command_add_option(int op, const tchar *option,
1307 struct wimlib_update_command *cmd)
1309 bool recognized = true;
1311 case WIMLIB_UPDATE_OP_ADD:
1312 if (!tstrcmp(option, T("--verbose")))
1313 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1314 else if (!tstrcmp(option, T("--unix-data")))
1315 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1316 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1317 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1318 else if (!tstrcmp(option, T("--strict-acls")))
1319 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1320 else if (!tstrcmp(option, T("--dereference")))
1321 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1325 case WIMLIB_UPDATE_OP_DELETE:
1326 if (!tstrcmp(option, T("--force")))
1327 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1328 else if (!tstrcmp(option, T("--recursive")))
1329 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1340 /* How many nonoption arguments each `imagex update' command expects */
1341 static const unsigned update_command_num_nonoptions[] = {
1342 [WIMLIB_UPDATE_OP_ADD] = 2,
1343 [WIMLIB_UPDATE_OP_DELETE] = 1,
1344 [WIMLIB_UPDATE_OP_RENAME] = 2,
1348 update_command_add_nonoption(int op, const tchar *nonoption,
1349 struct wimlib_update_command *cmd,
1350 unsigned num_nonoptions)
1353 case WIMLIB_UPDATE_OP_ADD:
1354 if (num_nonoptions == 0)
1355 cmd->add.fs_source_path = (tchar*)nonoption;
1357 cmd->add.wim_target_path = (tchar*)nonoption;
1359 case WIMLIB_UPDATE_OP_DELETE:
1360 cmd->delete.wim_path = (tchar*)nonoption;
1362 case WIMLIB_UPDATE_OP_RENAME:
1363 if (num_nonoptions == 0)
1364 cmd->rename.wim_source_path = (tchar*)nonoption;
1366 cmd->rename.wim_target_path = (tchar*)nonoption;
1372 * Parse a command passed on stdin to `imagex update'.
1374 * @line: Text of the command.
1375 * @len: Length of the line, including a null terminator
1378 * @command: A `struct wimlib_update_command' to fill in from the parsed
1381 * @line_number: Line number of the command, for diagnostics.
1383 * Returns true on success; returns false on parse error.
1386 parse_update_command(tchar *line, size_t len,
1387 struct wimlib_update_command *command,
1391 tchar *command_name;
1393 size_t num_nonoptions;
1395 /* Get the command name ("add", "delete", "rename") */
1396 ret = parse_string(&line, &len, &command_name);
1397 if (ret != PARSE_STRING_SUCCESS)
1400 if (!tstrcasecmp(command_name, T("add"))) {
1401 op = WIMLIB_UPDATE_OP_ADD;
1402 } else if (!tstrcasecmp(command_name, T("delete"))) {
1403 op = WIMLIB_UPDATE_OP_DELETE;
1404 } else if (!tstrcasecmp(command_name, T("rename"))) {
1405 op = WIMLIB_UPDATE_OP_RENAME;
1407 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1408 command_name, line_number);
1413 /* Parse additional options and non-options as needed */
1418 ret = parse_string(&line, &len, &next_string);
1419 if (ret == PARSE_STRING_NONE) /* End of line */
1421 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1423 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1425 if (!update_command_add_option(op, next_string, command))
1427 imagex_error(T("Unrecognized option \"%"TS"\" to "
1428 "update command \"%"TS"\" on line %zu"),
1429 next_string, command_name, line_number);
1435 if (num_nonoptions == update_command_num_nonoptions[op])
1437 imagex_error(T("Unexpected argument \"%"TS"\" in "
1438 "update command on line %zu\n"
1439 " (The \"%"TS"\" command only "
1440 "takes %u nonoption arguments!)\n"),
1441 next_string, line_number,
1442 command_name, num_nonoptions);
1445 update_command_add_nonoption(op, next_string,
1446 command, num_nonoptions);
1451 if (num_nonoptions != update_command_num_nonoptions[op]) {
1452 imagex_error(T("Not enough arguments to update command "
1453 "\"%"TS"\" on line %zu"), command_name, line_number);
1459 static struct wimlib_update_command *
1460 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1461 size_t *num_cmds_ret)
1465 struct wimlib_update_command *cmds;
1468 nlines = text_file_count_lines(cmd_file_contents_p,
1473 /* Always allocate at least 1 slot, just in case the implementation of
1474 * calloc() returns NULL if 0 bytes are requested. */
1475 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1477 imagex_error(T("out of memory"));
1480 p = *cmd_file_contents_p;
1482 for (i = 0; i < nlines; i++) {
1483 /* XXX: Could use rawmemchr() here instead, but it may not be
1484 * available on all platforms. */
1485 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1486 size_t len = endp - p + 1;
1488 if (!is_comment_line(p, len)) {
1489 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1500 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1501 * one image from a WIM file to a NTFS volume. */
1503 imagex_apply(int argc, tchar **argv)
1506 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1511 const tchar *wimfile;
1512 const tchar *target;
1513 const tchar *image_num_or_name;
1514 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1516 const tchar *swm_glob = NULL;
1517 WIMStruct **additional_swms = NULL;
1518 unsigned num_additional_swms = 0;
1520 for_opt(c, apply_options) {
1522 case IMAGEX_CHECK_OPTION:
1523 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1525 case IMAGEX_HARDLINK_OPTION:
1526 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1528 case IMAGEX_SYMLINK_OPTION:
1529 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1531 case IMAGEX_VERBOSE_OPTION:
1532 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
1534 case IMAGEX_REF_OPTION:
1537 case IMAGEX_UNIX_DATA_OPTION:
1538 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1540 case IMAGEX_NO_ACLS_OPTION:
1541 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1543 case IMAGEX_STRICT_ACLS_OPTION:
1544 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1546 case IMAGEX_NORPFIX_OPTION:
1547 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1549 case IMAGEX_RPFIX_OPTION:
1550 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1552 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1553 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1554 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1563 if (argc != 2 && argc != 3) {
1570 image_num_or_name = T("1");
1573 image_num_or_name = argv[1];
1577 ret = wimlib_open_wim(wimfile, open_flags, &w, imagex_progress_func);
1581 image = wimlib_resolve_image(w, image_num_or_name);
1582 ret = verify_image_exists(image, image_num_or_name, wimfile);
1586 num_images = wimlib_get_num_images(w);
1587 if (argc == 2 && num_images != 1) {
1588 imagex_error(T("\"%"TS"\" contains %d images; Please select one "
1589 "(or all)."), wimfile, num_images);
1596 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1598 &num_additional_swms);
1605 ret = tstat(target, &stbuf);
1607 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1608 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1610 if (errno != ENOENT) {
1611 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1619 win32_acquire_restore_privileges();
1621 ret = wimlib_extract_image(w, image, target, extract_flags,
1622 additional_swms, num_additional_swms,
1623 imagex_progress_func);
1625 tprintf(T("Done applying WIM image.\n"));
1627 win32_release_restore_privileges();
1631 if (additional_swms) {
1632 for (unsigned i = 0; i < num_additional_swms; i++)
1633 wimlib_free(additional_swms[i]);
1634 free(additional_swms);
1639 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1640 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1641 * the desired image. 'wimlib-imagex append': add a new image to an existing
1644 imagex_capture_or_append(int argc, tchar **argv)
1647 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1648 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
1649 int write_flags = 0;
1650 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
1651 const tchar *wimfile;
1654 const tchar *flags_element = NULL;
1658 int cmd = tstrcmp(argv[0], T("append")) ? CAPTURE : APPEND;
1659 unsigned num_threads = 0;
1662 size_t source_name_len;
1665 const tchar *config_file = NULL;
1667 struct wimlib_capture_config *config = NULL;
1669 bool source_list = false;
1670 size_t source_list_nchars;
1671 tchar *source_list_contents;
1672 bool capture_sources_malloced;
1673 struct wimlib_capture_source *capture_sources;
1675 bool name_defaulted;
1677 for_opt(c, capture_or_append_options) {
1679 case IMAGEX_BOOT_OPTION:
1680 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1682 case IMAGEX_CHECK_OPTION:
1683 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1684 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1686 case IMAGEX_CONFIG_OPTION:
1687 config_file = optarg;
1689 case IMAGEX_COMPRESS_OPTION:
1690 compression_type = get_compression_type(optarg);
1691 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1694 case IMAGEX_FLAGS_OPTION:
1695 flags_element = optarg;
1697 case IMAGEX_DEREFERENCE_OPTION:
1698 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1700 case IMAGEX_VERBOSE_OPTION:
1701 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1703 case IMAGEX_THREADS_OPTION:
1704 num_threads = parse_num_threads(optarg);
1705 if (num_threads == UINT_MAX)
1708 case IMAGEX_REBUILD_OPTION:
1709 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1711 case IMAGEX_UNIX_DATA_OPTION:
1712 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1714 case IMAGEX_SOURCE_LIST_OPTION:
1717 case IMAGEX_NO_ACLS_OPTION:
1718 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1720 case IMAGEX_STRICT_ACLS_OPTION:
1721 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1723 case IMAGEX_RPFIX_OPTION:
1724 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1726 case IMAGEX_NORPFIX_OPTION:
1727 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1737 if (argc < 2 || argc > 4) {
1747 name_defaulted = false;
1749 /* Set default name to SOURCE argument, omitting any directory
1750 * prefixes and trailing slashes. This requires making a copy
1751 * of @source. Leave some free characters at the end in case we
1752 * append a number to keep the name unique. */
1753 source_name_len = tstrlen(source);
1754 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1755 name = tbasename(tstrcpy(source_copy, source));
1756 name_defaulted = true;
1758 /* Image description defaults to NULL if not given. */
1759 desc = (argc >= 4) ? argv[3] : NULL;
1762 /* Set up capture sources in source list mode */
1763 if (source[0] == T('-') && source[1] == T('\0')) {
1764 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1766 source_list_contents = file_get_text_contents(source,
1767 &source_list_nchars);
1769 if (!source_list_contents)
1772 capture_sources = parse_source_list(&source_list_contents,
1775 if (!capture_sources) {
1777 goto out_free_source_list_contents;
1779 capture_sources_malloced = true;
1781 /* Set up capture source in non-source-list mode (could be
1782 * either "normal" mode or "NTFS mode"--- see the man page). */
1783 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1784 capture_sources[0].fs_source_path = source;
1785 capture_sources[0].wim_target_path = NULL;
1786 capture_sources[0].reserved = 0;
1788 capture_sources_malloced = false;
1789 source_list_contents = NULL;
1795 config_str = file_get_text_contents(config_file, &config_len);
1798 goto out_free_capture_sources;
1801 config = alloca(sizeof(*config));
1802 ret = parse_capture_config(&config_str, config_len, config);
1804 goto out_free_config;
1806 config = &default_capture_config;
1810 ret = wimlib_open_wim(wimfile, open_flags, &w,
1811 imagex_progress_func);
1813 ret = wimlib_create_new_wim(compression_type, &w);
1815 goto out_free_config;
1819 ret = tstat(source, &stbuf);
1821 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1822 tprintf(T("Capturing WIM image from NTFS "
1823 "filesystem on \"%"TS"\"\n"), source);
1824 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1827 if (errno != ENOENT) {
1828 imagex_error_with_errno(T("Failed to stat "
1829 "\"%"TS"\""), source);
1831 goto out_wimlib_free;
1836 if (cmd == APPEND && name_defaulted) {
1837 /* If the user did not specify an image name, and the basename
1838 * of the source already exists as an image name in the WIM
1839 * file, append a suffix to make it unique. */
1840 unsigned long conflict_idx;
1841 tchar *name_end = tstrchr(name, T('\0'));
1842 for (conflict_idx = 1;
1843 wimlib_image_name_in_use(w, name);
1846 tsprintf(name_end, T(" (%lu)"), conflict_idx);
1850 win32_acquire_capture_privileges();
1853 ret = wimlib_add_image_multisource(w,
1859 imagex_progress_func);
1861 goto out_release_privs;
1862 cur_image = wimlib_get_num_images(w);
1864 ret = wimlib_set_image_descripton(w, cur_image, desc);
1866 goto out_release_privs;
1868 if (flags_element) {
1869 ret = wimlib_set_image_flags(w, cur_image, flags_element);
1871 goto out_release_privs;
1873 if (cmd == APPEND) {
1874 ret = wimlib_overwrite(w, write_flags, num_threads,
1875 imagex_progress_func);
1877 ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
1878 num_threads, imagex_progress_func);
1881 imagex_error(T("Failed to write the WIM file \"%"TS"\""),
1885 win32_release_capture_privileges();
1890 if (config != NULL && config != &default_capture_config) {
1891 free(config->exclusion_pats.pats);
1892 free(config->exclusion_exception_pats.pats);
1895 out_free_capture_sources:
1896 if (capture_sources_malloced)
1897 free(capture_sources);
1898 out_free_source_list_contents:
1899 free(source_list_contents);
1903 /* Remove image(s) from a WIM. */
1905 imagex_delete(int argc, tchar **argv)
1908 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1909 int write_flags = 0;
1910 const tchar *wimfile;
1911 const tchar *image_num_or_name;
1916 for_opt(c, delete_options) {
1918 case IMAGEX_CHECK_OPTION:
1919 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1920 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1922 case IMAGEX_SOFT_OPTION:
1923 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
1935 imagex_error(T("Must specify a WIM file"));
1937 imagex_error(T("Must specify an image"));
1942 image_num_or_name = argv[1];
1944 ret = wimlib_open_wim(wimfile, open_flags, &w,
1945 imagex_progress_func);
1949 image = wimlib_resolve_image(w, image_num_or_name);
1951 ret = verify_image_exists(image, image_num_or_name, wimfile);
1955 ret = wimlib_delete_image(w, image);
1957 imagex_error(T("Failed to delete image from \"%"TS"\""), wimfile);
1961 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1963 imagex_error(T("Failed to write the file \"%"TS"\" with image "
1964 "deleted"), wimfile);
1971 /* Print the files contained in an image(s) in a WIM file. */
1973 imagex_dir(int argc, tchar **argv)
1975 const tchar *wimfile;
1982 imagex_error(T("Must specify a WIM file"));
1987 imagex_error(T("Too many arguments"));
1993 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w,
1994 imagex_progress_func);
1999 image = wimlib_resolve_image(w, argv[2]);
2000 ret = verify_image_exists(image, argv[2], wimfile);
2004 /* Image was not specified. If the WIM only contains one image,
2005 * choose that one; otherwise, print an error. */
2006 num_images = wimlib_get_num_images(w);
2007 if (num_images != 1) {
2008 imagex_error(T("\"%"TS"\" contains %d images; Please "
2009 "select one."), wimfile, num_images);
2017 ret = wimlib_print_files(w, image);
2023 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2026 imagex_export(int argc, tchar **argv)
2030 int export_flags = 0;
2031 int write_flags = 0;
2032 int compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
2033 bool compression_type_specified = false;
2034 const tchar *src_wimfile;
2035 const tchar *src_image_num_or_name;
2036 const tchar *dest_wimfile;
2037 const tchar *dest_name;
2038 const tchar *dest_desc;
2039 WIMStruct *src_w = NULL;
2040 WIMStruct *dest_w = NULL;
2045 const tchar *swm_glob = NULL;
2046 WIMStruct **additional_swms = NULL;
2047 unsigned num_additional_swms = 0;
2048 unsigned num_threads = 0;
2050 for_opt(c, export_options) {
2052 case IMAGEX_BOOT_OPTION:
2053 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2055 case IMAGEX_CHECK_OPTION:
2056 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2057 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2059 case IMAGEX_COMPRESS_OPTION:
2060 compression_type = get_compression_type(optarg);
2061 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2063 compression_type_specified = true;
2065 case IMAGEX_REF_OPTION:
2068 case IMAGEX_THREADS_OPTION:
2069 num_threads = parse_num_threads(optarg);
2070 if (num_threads == UINT_MAX)
2073 case IMAGEX_REBUILD_OPTION:
2074 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2083 if (argc < 3 || argc > 5) {
2087 src_wimfile = argv[0];
2088 src_image_num_or_name = argv[1];
2089 dest_wimfile = argv[2];
2090 dest_name = (argc >= 4) ? argv[3] : NULL;
2091 dest_desc = (argc >= 5) ? argv[4] : NULL;
2092 ret = wimlib_open_wim(src_wimfile,
2093 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w,
2094 imagex_progress_func);
2098 /* Determine if the destination is an existing file or not.
2099 * If so, we try to append the exported image(s) to it; otherwise, we
2100 * create a new WIM containing the exported image(s). */
2101 if (tstat(dest_wimfile, &stbuf) == 0) {
2105 /* Destination file exists. */
2107 if (!S_ISREG(stbuf.st_mode)) {
2108 imagex_error(T("\"%"TS"\" is not a regular file"),
2113 ret = wimlib_open_wim(dest_wimfile, open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2114 &dest_w, imagex_progress_func);
2118 dest_ctype = wimlib_get_compression_type(dest_w);
2119 if (compression_type_specified
2120 && compression_type != dest_ctype)
2122 imagex_error(T("Cannot specify a compression type that is "
2123 "not the same as that used in the "
2124 "destination WIM"));
2130 /* dest_wimfile is not an existing file, so create a new WIM. */
2131 if (!compression_type_specified)
2132 compression_type = wimlib_get_compression_type(src_w);
2133 if (errno == ENOENT) {
2134 ret = wimlib_create_new_wim(compression_type, &dest_w);
2138 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2145 image = wimlib_resolve_image(src_w, src_image_num_or_name);
2146 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2151 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
2153 &num_additional_swms);
2158 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
2159 export_flags, additional_swms,
2160 num_additional_swms, imagex_progress_func);
2166 ret = wimlib_write(dest_w, dest_wimfile, WIMLIB_ALL_IMAGES,
2167 write_flags, num_threads,
2168 imagex_progress_func);
2170 ret = wimlib_overwrite(dest_w, write_flags, num_threads,
2171 imagex_progress_func);
2174 wimlib_free(dest_w);
2175 if (additional_swms) {
2176 for (unsigned i = 0; i < num_additional_swms; i++)
2177 wimlib_free(additional_swms[i]);
2178 free(additional_swms);
2184 is_root_wim_path(const tchar *path)
2187 for (p = path; *p; p++)
2188 if (*p != T('\\') && *p != T('/'))
2194 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2195 const tchar *dest_dir)
2197 for (size_t i = 0; i < num_cmds; i++)
2198 if (cmds[i].fs_dest_path != dest_dir)
2199 free(cmds[i].fs_dest_path);
2203 static struct wimlib_extract_command *
2204 prepare_extract_commands(tchar **paths, unsigned num_paths,
2205 int extract_flags, tchar *dest_dir,
2206 size_t *num_cmds_ret)
2208 struct wimlib_extract_command *cmds;
2210 tchar *emptystr = T("");
2212 if (num_paths == 0) {
2216 num_cmds = num_paths;
2217 cmds = calloc(num_cmds, sizeof(cmds[0]));
2219 imagex_error(T("Out of memory!"));
2223 for (size_t i = 0; i < num_cmds; i++) {
2224 cmds[i].extract_flags = extract_flags;
2225 cmds[i].wim_source_path = paths[i];
2226 if (is_root_wim_path(paths[i])) {
2227 cmds[i].fs_dest_path = dest_dir;
2229 size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2230 cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2231 if (!cmds[i].fs_dest_path) {
2232 free_extract_commands(cmds, num_cmds, dest_dir);
2235 tsprintf(cmds[i].fs_dest_path, T("%"TS""OS_PREFERRED_PATH_SEPARATOR_STRING"%"TS),
2236 dest_dir, tbasename(paths[i]));
2239 *num_cmds_ret = num_cmds;
2243 /* Extract files or directories from a WIM image */
2245 imagex_extract(int argc, tchar **argv)
2248 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2252 const tchar *wimfile;
2253 const tchar *image_num_or_name;
2254 tchar *dest_dir = T(".");
2255 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL | WIMLIB_EXTRACT_FLAG_NORPFIX;
2257 const tchar *swm_glob = NULL;
2258 WIMStruct **additional_swms = NULL;
2259 unsigned num_additional_swms = 0;
2261 struct wimlib_extract_command *cmds;
2264 for_opt(c, extract_options) {
2266 case IMAGEX_CHECK_OPTION:
2267 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2269 case IMAGEX_VERBOSE_OPTION:
2270 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
2272 case IMAGEX_REF_OPTION:
2275 case IMAGEX_UNIX_DATA_OPTION:
2276 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2278 case IMAGEX_NO_ACLS_OPTION:
2279 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2281 case IMAGEX_STRICT_ACLS_OPTION:
2282 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2284 case IMAGEX_DEST_DIR_OPTION:
2287 case IMAGEX_TO_STDOUT_OPTION:
2288 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2289 imagex_be_quiet = true;
2291 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2292 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2293 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2306 image_num_or_name = argv[1];
2311 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2318 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2322 image = wimlib_resolve_image(wim, image_num_or_name);
2323 ret = verify_image_exists_and_is_single(image,
2327 goto out_wimlib_free;
2330 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2332 &num_additional_swms);
2334 goto out_wimlib_free;
2338 win32_acquire_restore_privileges();
2341 ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
2342 additional_swms, num_additional_swms,
2343 imagex_progress_func);
2345 if (!imagex_be_quiet)
2346 tprintf(T("Done extracting files.\n"));
2347 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2348 tfprintf(stderr, T("Note: You can use `"IMAGEX_PROGNAME" dir' to see what "
2349 "files and directories\n"
2350 " are in the WIM image.\n"));
2353 win32_release_restore_privileges();
2355 if (additional_swms) {
2356 for (unsigned i = 0; i < num_additional_swms; i++)
2357 wimlib_free(additional_swms[i]);
2358 free(additional_swms);
2363 free_extract_commands(cmds, num_cmds, dest_dir);
2372 /* Prints information about a WIM file; also can mark an image as bootable,
2373 * change the name of an image, or change the description of an image. */
2375 imagex_info(int argc, tchar **argv)
2380 bool header = false;
2381 bool lookup_table = false;
2383 bool metadata = false;
2384 bool short_header = true;
2385 const tchar *xml_out_file = NULL;
2386 const tchar *wimfile;
2387 const tchar *image_num_or_name = T("all");
2388 const tchar *new_name = NULL;
2389 const tchar *new_desc = NULL;
2394 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2399 for_opt(c, info_options) {
2401 case IMAGEX_BOOT_OPTION:
2404 case IMAGEX_CHECK_OPTION:
2407 case IMAGEX_HEADER_OPTION:
2409 short_header = false;
2411 case IMAGEX_LOOKUP_TABLE_OPTION:
2412 lookup_table = true;
2413 short_header = false;
2415 case IMAGEX_XML_OPTION:
2417 short_header = false;
2419 case IMAGEX_EXTRACT_XML_OPTION:
2420 xml_out_file = optarg;
2421 short_header = false;
2423 case IMAGEX_METADATA_OPTION:
2425 short_header = false;
2435 if (argc == 0 || argc > 4) {
2441 image_num_or_name = argv[1];
2451 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2453 ret = wimlib_open_wim(wimfile, open_flags, &w,
2454 imagex_progress_func);
2458 part_number = wimlib_get_part_number(w, &total_parts);
2460 image = wimlib_resolve_image(w, image_num_or_name);
2461 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
2462 imagex_error(T("The image \"%"TS"\" does not exist"),
2465 imagex_error(T("If you would like to set the boot "
2466 "index to 0, specify image \"0\" with "
2467 "the --boot flag."));
2469 ret = WIMLIB_ERR_INVALID_IMAGE;
2473 num_images = wimlib_get_num_images(w);
2475 if (num_images == 0) {
2477 imagex_error(T("--boot is meaningless on a WIM with no "
2479 ret = WIMLIB_ERR_INVALID_IMAGE;
2484 if (image == WIMLIB_ALL_IMAGES && num_images > 1) {
2486 imagex_error(T("Cannot specify the --boot flag "
2487 "without specifying a specific "
2488 "image in a multi-image WIM"));
2489 ret = WIMLIB_ERR_INVALID_IMAGE;
2493 imagex_error(T("Cannot specify the NEW_NAME "
2494 "without specifying a specific "
2495 "image in a multi-image WIM"));
2496 ret = WIMLIB_ERR_INVALID_IMAGE;
2501 /* Operations that print information are separated from operations that
2502 * recreate the WIM file. */
2503 if (!new_name && !boot) {
2505 /* Read-only operations */
2507 if (image == WIMLIB_NO_IMAGE) {
2508 imagex_error(T("\"%"TS"\" is not a valid image"),
2510 ret = WIMLIB_ERR_INVALID_IMAGE;
2514 if (image == WIMLIB_ALL_IMAGES && short_header)
2515 wimlib_print_wim_information(w);
2518 wimlib_print_header(w);
2521 if (total_parts != 1) {
2522 tprintf(T("Warning: Only showing the lookup table "
2523 "for part %d of a %d-part WIM.\n"),
2524 part_number, total_parts);
2526 wimlib_print_lookup_table(w);
2530 ret = wimlib_extract_xml_data(w, stdout);
2536 fp = tfopen(xml_out_file, T("wb"));
2538 imagex_error_with_errno(T("Failed to open the "
2539 "file \"%"TS"\" for "
2545 ret = wimlib_extract_xml_data(w, fp);
2546 if (fclose(fp) != 0) {
2547 imagex_error(T("Failed to close the file "
2558 wimlib_print_available_images(w, image);
2561 ret = wimlib_print_metadata(w, image);
2567 /* Modification operations */
2569 if (image == WIMLIB_ALL_IMAGES)
2572 if (image == WIMLIB_NO_IMAGE && new_name) {
2573 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
2574 "when using image 0"), new_name);
2580 if (image == wimlib_get_boot_idx(w)) {
2581 tprintf(T("Image %d is already marked as "
2582 "bootable.\n"), image);
2585 tprintf(T("Marking image %d as bootable.\n"),
2587 ret = wimlib_set_boot_idx(w, image);
2593 if (!tstrcmp(wimlib_get_image_name(w, image), new_name))
2595 tprintf(T("Image %d is already named \"%"TS"\".\n"),
2599 tprintf(T("Changing the name of image %d to "
2600 "\"%"TS"\".\n"), image, new_name);
2601 ret = wimlib_set_image_name(w, image, new_name);
2607 const tchar *old_desc;
2608 old_desc = wimlib_get_image_description(w, image);
2609 if (old_desc && !tstrcmp(old_desc, new_desc)) {
2610 tprintf(T("The description of image %d is already "
2611 "\"%"TS"\".\n"), image, new_desc);
2614 tprintf(T("Changing the description of image %d "
2615 "to \"%"TS"\".\n"), image, new_desc);
2616 ret = wimlib_set_image_descripton(w, image,
2623 /* Only call wimlib_overwrite() if something actually needs to
2625 if (boot || new_name || new_desc ||
2626 (check && !wimlib_has_integrity_table(w)))
2628 int write_flags = 0;
2631 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2632 ret = wimlib_overwrite(w, write_flags, 1,
2633 imagex_progress_func);
2635 tprintf(T("The file \"%"TS"\" was not modified because nothing "
2636 "needed to be done.\n"), wimfile);
2645 /* Join split WIMs into one part WIM */
2647 imagex_join(int argc, tchar **argv)
2650 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2651 int wim_write_flags = 0;
2652 const tchar *output_path;
2654 for_opt(c, join_options) {
2656 case IMAGEX_CHECK_OPTION:
2657 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2658 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2668 imagex_error(T("Must specify one or more split WIM (.swm) "
2672 output_path = argv[0];
2673 return wimlib_join((const tchar * const *)++argv,
2678 imagex_progress_func);
2684 /* Mounts an image using a FUSE mount. */
2686 imagex_mount_rw_or_ro(int argc, tchar **argv)
2689 int mount_flags = 0;
2690 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2691 const tchar *wimfile;
2697 const tchar *swm_glob = NULL;
2698 WIMStruct **additional_swms = NULL;
2699 unsigned num_additional_swms = 0;
2700 const tchar *staging_dir = NULL;
2702 if (!tstrcmp(argv[0], T("mountrw"))) {
2703 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
2704 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2707 for_opt(c, mount_options) {
2709 case IMAGEX_ALLOW_OTHER_OPTION:
2710 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
2712 case IMAGEX_CHECK_OPTION:
2713 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2715 case IMAGEX_DEBUG_OPTION:
2716 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
2718 case IMAGEX_STREAMS_INTERFACE_OPTION:
2719 if (!tstrcasecmp(optarg, T("none")))
2720 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
2721 else if (!tstrcasecmp(optarg, T("xattr")))
2722 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2723 else if (!tstrcasecmp(optarg, T("windows")))
2724 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
2726 imagex_error(T("Unknown stream interface \"%"TS"\""),
2731 case IMAGEX_REF_OPTION:
2734 case IMAGEX_STAGING_DIR_OPTION:
2735 staging_dir = optarg;
2737 case IMAGEX_UNIX_DATA_OPTION:
2738 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
2746 if (argc != 2 && argc != 3)
2751 ret = wimlib_open_wim(wimfile, open_flags, &w,
2752 imagex_progress_func);
2757 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2759 &num_additional_swms);
2766 num_images = wimlib_get_num_images(w);
2767 if (num_images != 1) {
2768 imagex_error(T("\"%"TS"\" contains %d images; Please "
2769 "select one."), wimfile, num_images);
2770 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2777 image = wimlib_resolve_image(w, argv[1]);
2779 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
2784 ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
2785 num_additional_swms, staging_dir);
2787 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
2789 image, wimfile, dir);
2794 if (additional_swms) {
2795 for (unsigned i = 0; i < num_additional_swms; i++)
2796 wimlib_free(additional_swms[i]);
2797 free(additional_swms);
2801 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2806 /* Rebuild a WIM file */
2808 imagex_optimize(int argc, tchar **argv)
2811 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2812 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
2815 const tchar *wimfile;
2818 unsigned num_threads = 0;
2820 for_opt(c, optimize_options) {
2822 case IMAGEX_CHECK_OPTION:
2823 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2824 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2826 case IMAGEX_RECOMPRESS_OPTION:
2827 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
2829 case IMAGEX_THREADS_OPTION:
2830 num_threads = parse_num_threads(optarg);
2831 if (num_threads == UINT_MAX)
2849 ret = wimlib_open_wim(wimfile, open_flags, &w,
2850 imagex_progress_func);
2854 old_size = file_get_size(argv[0]);
2855 tprintf(T("\"%"TS"\" original size: "), wimfile);
2857 tfputs(T("Unknown\n"), stdout);
2859 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
2861 ret = wimlib_overwrite(w, write_flags, num_threads,
2862 imagex_progress_func);
2865 new_size = file_get_size(argv[0]);
2866 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
2868 tfputs(T("Unknown\n"), stdout);
2870 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
2872 tfputs(T("Space saved: "), stdout);
2873 if (new_size != -1 && old_size != -1) {
2874 tprintf(T("%lld KiB\n"),
2875 ((long long)old_size - (long long)new_size) >> 10);
2877 tfputs(T("Unknown\n"), stdout);
2885 /* Split a WIM into a spanned set */
2887 imagex_split(int argc, tchar **argv)
2890 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2891 int write_flags = 0;
2892 unsigned long part_size;
2897 for_opt(c, split_options) {
2899 case IMAGEX_CHECK_OPTION:
2900 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2901 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2915 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
2916 if (tmp == argv[2] || *tmp) {
2917 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
2918 imagex_error(T("The part size must be an integer or "
2919 "floating-point number of megabytes."));
2922 ret = wimlib_open_wim(argv[0], open_flags, &w, imagex_progress_func);
2925 ret = wimlib_split(w, argv[1], part_size, write_flags, imagex_progress_func);
2930 /* Unmounts a mounted WIM image. */
2932 imagex_unmount(int argc, tchar **argv)
2935 int unmount_flags = 0;
2938 for_opt(c, unmount_options) {
2940 case IMAGEX_COMMIT_OPTION:
2941 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
2943 case IMAGEX_CHECK_OPTION:
2944 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
2946 case IMAGEX_REBUILD_OPTION:
2947 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
2949 case IMAGEX_LAZY_OPTION:
2950 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
2964 ret = wimlib_unmount_image(argv[0], unmount_flags,
2965 imagex_progress_func);
2967 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
2972 * Add, delete, or rename files in a WIM image.
2975 imagex_update(int argc, tchar **argv)
2977 const tchar *wimfile;
2978 const tchar *image_num_or_name;
2982 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2983 int write_flags = 0;
2984 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
2985 int default_add_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
2986 int default_delete_flags = 0;
2987 unsigned num_threads = 0;
2989 tchar *cmd_file_contents = NULL;
2990 size_t cmd_file_nchars;
2991 struct wimlib_update_command *cmds;
2994 tchar *command_str = NULL;
2996 const tchar *config_file = NULL;
2998 struct wimlib_capture_config *config = NULL;
3000 for_opt(c, update_options) {
3002 /* Generic or write options */
3003 case IMAGEX_THREADS_OPTION:
3004 num_threads = parse_num_threads(optarg);
3005 if (num_threads == UINT_MAX) {
3010 case IMAGEX_CHECK_OPTION:
3011 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3012 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3014 case IMAGEX_REBUILD_OPTION:
3015 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3017 case IMAGEX_COMMAND_OPTION:
3019 imagex_error(T("--command may only be specified "
3020 "one time. Please provide\n"
3021 " the update commands "
3022 "on standard input instead."));
3026 command_str = tstrdup(optarg);
3028 imagex_error(T("Out of memory!"));
3033 /* Default delete options */
3034 case IMAGEX_FORCE_OPTION:
3035 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3037 case IMAGEX_RECURSIVE_OPTION:
3038 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3041 /* Global add option */
3042 case IMAGEX_CONFIG_OPTION:
3043 config_file = optarg;
3046 /* Default add options */
3047 case IMAGEX_VERBOSE_OPTION:
3048 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
3050 case IMAGEX_DEREFERENCE_OPTION:
3051 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
3053 case IMAGEX_UNIX_DATA_OPTION:
3054 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
3056 case IMAGEX_NO_ACLS_OPTION:
3057 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
3059 case IMAGEX_STRICT_ACLS_OPTION:
3060 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
3069 if (argc < 1 || argc > 2)
3073 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3078 image_num_or_name = argv[1];
3080 image_num_or_name = T("1");
3082 image = wimlib_resolve_image(wim, image_num_or_name);
3084 ret = verify_image_exists_and_is_single(image, image_num_or_name,
3087 goto out_wimlib_free;
3089 num_images = wimlib_get_num_images(wim);
3090 if (argc == 1 && num_images != 1) {
3091 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3092 wimfile, num_images);
3095 goto out_wimlib_free;
3098 /* Parse capture configuration file if specified */
3102 config_str = file_get_text_contents(config_file, &config_len);
3105 goto out_wimlib_free;
3108 config = alloca(sizeof(*config));
3109 ret = parse_capture_config(&config_str, config_len, config);
3111 goto out_free_config;
3113 config = &default_capture_config;
3116 /* Read update commands from standard input, or the command string if
3119 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3122 if (isatty(STDIN_FILENO)) {
3123 tputs(T("Reading update commands from standard input..."));
3124 recommend_man_page(T("update"));
3126 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3127 if (!cmd_file_contents) {
3129 goto out_free_config;
3132 /* Parse the update commands */
3133 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3138 goto out_free_cmd_file_contents;
3141 /* Set default flags and capture config on the update commands */
3142 bool have_add_command = false;
3143 for (size_t i = 0; i < num_cmds; i++) {
3144 switch (cmds[i].op) {
3145 case WIMLIB_UPDATE_OP_ADD:
3146 cmds[i].add.add_flags |= default_add_flags;
3147 cmds[i].add.config = config;
3148 have_add_command = true;
3150 case WIMLIB_UPDATE_OP_DELETE:
3151 cmds[i].delete.delete_flags |= default_delete_flags;
3159 if (have_add_command)
3160 win32_acquire_capture_privileges();
3163 /* Execute the update commands */
3164 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3165 imagex_progress_func);
3167 goto out_release_privs;
3169 /* Overwrite the updated WIM */
3170 ret = wimlib_overwrite(wim, write_flags, num_threads,
3171 imagex_progress_func);
3174 if (have_add_command)
3175 win32_release_capture_privileges();
3178 out_free_cmd_file_contents:
3179 free(cmd_file_contents);
3181 if (config != NULL && config != &default_capture_config) {
3182 free(config->exclusion_pats.pats);
3183 free(config->exclusion_exception_pats.pats);
3197 struct imagex_command {
3199 int (*func)(int , tchar **);
3204 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
3205 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
3207 static const struct imagex_command imagex_commands[] = {
3208 {T("append"), imagex_capture_or_append, APPEND},
3209 {T("apply"), imagex_apply, APPLY},
3210 {T("capture"), imagex_capture_or_append, CAPTURE},
3211 {T("delete"), imagex_delete, DELETE},
3212 {T("dir"), imagex_dir, DIR},
3213 {T("export"), imagex_export, EXPORT},
3214 {T("extract"), imagex_extract, EXTRACT},
3215 {T("info"), imagex_info, INFO},
3216 {T("join"), imagex_join, JOIN},
3217 {T("mount"), imagex_mount_rw_or_ro, MOUNT},
3218 {T("mountrw"), imagex_mount_rw_or_ro, MOUNTRW},
3219 {T("optimize"),imagex_optimize, OPTIMIZE},
3220 {T("split"), imagex_split, SPLIT},
3221 {T("unmount"), imagex_unmount, UNMOUNT},
3222 {T("update"), imagex_update, UPDATE},
3228 static const tchar *s =
3230 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
3231 "Copyright (C) 2012, 2013 Eric Biggers\n"
3232 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
3233 "This is free software: you are free to change and redistribute it.\n"
3234 "There is NO WARRANTY, to the extent permitted by law.\n"
3236 "Report bugs to "PACKAGE_BUGREPORT".\n"
3243 help_or_version(int argc, tchar **argv)
3247 const struct imagex_command *cmd;
3249 for (i = 1; i < argc; i++) {
3257 if (!tstrcmp(p, T("help"))) {
3258 for_imagex_command(cmd) {
3259 if (!tstrcmp(cmd->name, argv[1])) {
3267 if (!tstrcmp(p, T("version"))) {
3278 const struct imagex_command *cmd;
3279 tprintf(T("Usage:\n%"TS), usage_strings[cmd_type]);
3280 for_imagex_command(cmd) {
3281 if (cmd->cmd == cmd_type) {
3282 tputc(T('\n'), stdout);
3283 recommend_man_page(cmd->name);
3291 tfputs(T("Usage:\n"), stdout);
3292 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
3293 tprintf(T(" %"TS), usage_strings[i]);
3294 static const tchar *extra =
3296 " "IMAGEX_PROGNAME" --help\n"
3297 " "IMAGEX_PROGNAME" --version\n"
3299 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
3302 tfputs(extra, stdout);
3303 recommend_man_page(T(""));
3306 /* Entry point for wimlib's ImageX implementation. On UNIX the command
3307 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
3308 * something else), while an Windows the command arguments will be UTF-16LE
3309 * encoded 'wchar_t' strings. */
3312 wmain(int argc, wchar_t **argv, wchar_t **envp)
3314 main(int argc, char **argv)
3317 const struct imagex_command *cmd;
3322 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
3323 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
3327 setlocale(LC_ALL, "");
3328 codeset = nl_langinfo(CODESET);
3329 if (!strstr(codeset, "UTF-8") &&
3330 !strstr(codeset, "UTF8") &&
3331 !strstr(codeset, "utf-8") &&
3332 !strstr(codeset, "utf8"))
3335 "WARNING: Running "IMAGEX_PROGNAME" in a UTF-8 locale is recommended!\n"
3336 " Maybe try: `export LANG=en_US.UTF-8'?\n"
3337 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
3338 " to any value to force wimlib to use UTF-8.\n",
3343 #endif /* !__WIN32__ */
3346 imagex_error(T("No command specified"));
3352 /* Handle --help and --version for all commands. Note that this will
3353 * not return if either of these arguments are present. */
3354 help_or_version(argc, argv);
3358 /* The user may like to see more informative error messages. */
3359 wimlib_set_print_errors(true);
3361 /* Do any initializations that the library needs */
3362 ret = wimlib_global_init(init_flags);
3364 goto out_check_status;
3366 /* Search for the function to handle the ImageX subcommand. */
3367 for_imagex_command(cmd) {
3368 if (!tstrcmp(cmd->name, *argv)) {
3369 ret = cmd->func(argc, argv);
3370 goto out_check_write_error;
3374 imagex_error(T("Unrecognized command: `%"TS"'"), argv[0]);
3378 out_check_write_error:
3379 /* For 'wimlib-imagex info' and 'wimlib-imagex dir', data printed to
3380 * standard output is part of the program's actual behavior and not just
3381 * for informational purposes, so we should set a failure exit status if
3382 * there was a write error. */
3383 if (cmd == &imagex_commands[INFO] || cmd == &imagex_commands[DIR]) {
3384 if (ferror(stdout) || fclose(stdout)) {
3385 imagex_error_with_errno(T("error writing to standard output"));
3391 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
3392 * outside of the wimlib library code. 0 indicates success. > 0
3393 * indicates a wimlib error code from which an error message can be
3396 imagex_error(T("Exiting with error code %d:\n"
3398 wimlib_get_error_string(ret));
3399 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
3400 imagex_error_with_errno(T("errno"));
3403 /* Make the library free any resources it's holding (not strictly
3404 * necessary because the process is ending anyway). */
3405 wimlib_global_cleanup();