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 "/"
63 static inline void set_fd_to_binary_mode(int fd)
69 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
71 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
94 static void usage(int cmd, FILE *fp);
95 static void usage_all(FILE *fp);
96 static void recommend_man_page(int cmd, FILE *fp);
97 static const tchar *get_cmd_string(int cmd, bool nospace);
99 static bool imagex_be_quiet = false;
100 static FILE *imagex_info_file;
102 #define imagex_printf(format, ...) \
103 tfprintf(imagex_info_file, format, ##__VA_ARGS__)
106 IMAGEX_ALLOW_OTHER_OPTION,
109 IMAGEX_COMMAND_OPTION,
110 IMAGEX_COMMIT_OPTION,
111 IMAGEX_COMPRESS_OPTION,
112 IMAGEX_CONFIG_OPTION,
114 IMAGEX_DEREFERENCE_OPTION,
115 IMAGEX_DEST_DIR_OPTION,
116 IMAGEX_EXTRACT_XML_OPTION,
119 IMAGEX_HARDLINK_OPTION,
120 IMAGEX_HEADER_OPTION,
121 IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
123 IMAGEX_LOOKUP_TABLE_OPTION,
124 IMAGEX_METADATA_OPTION,
125 IMAGEX_NORPFIX_OPTION,
126 IMAGEX_NOCHECK_OPTION,
127 IMAGEX_NO_ACLS_OPTION,
128 IMAGEX_NOT_PIPABLE_OPTION,
130 IMAGEX_PIPABLE_OPTION,
131 IMAGEX_REBUILD_OPTION,
132 IMAGEX_RECOMPRESS_OPTION,
133 IMAGEX_RECURSIVE_OPTION,
137 IMAGEX_SOURCE_LIST_OPTION,
138 IMAGEX_STAGING_DIR_OPTION,
139 IMAGEX_STREAMS_INTERFACE_OPTION,
140 IMAGEX_STRICT_ACLS_OPTION,
141 IMAGEX_SYMLINK_OPTION,
142 IMAGEX_THREADS_OPTION,
143 IMAGEX_TO_STDOUT_OPTION,
144 IMAGEX_UNIX_DATA_OPTION,
145 IMAGEX_VERBOSE_OPTION,
149 static const struct option apply_options[] = {
150 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
151 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
152 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
153 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
154 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
155 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
156 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
157 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
158 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
159 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
160 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
161 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
165 static const struct option capture_or_append_options[] = {
166 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
167 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
168 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
169 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
170 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
171 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
172 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
173 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
174 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
175 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
176 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
177 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
178 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
179 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
180 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
181 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
182 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
183 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
184 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
185 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
189 static const struct option delete_options[] = {
190 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
191 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
195 static const struct option dir_options[] = {
196 {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
200 static const struct option export_options[] = {
201 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
202 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
203 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
204 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
205 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
206 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
207 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
208 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
209 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
210 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
214 static const struct option extract_options[] = {
215 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
216 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
217 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
218 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
219 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
220 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
221 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
222 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
223 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
224 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
228 static const struct option info_options[] = {
229 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
230 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
231 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
232 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
233 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
234 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
235 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
236 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
237 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
241 static const struct option join_options[] = {
242 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
246 static const struct option mount_options[] = {
247 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
248 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
249 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
250 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
251 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
252 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
253 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
257 static const struct option optimize_options[] = {
258 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
259 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
260 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
261 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
262 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
263 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
264 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
268 static const struct option split_options[] = {
269 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
273 static const struct option unmount_options[] = {
274 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
275 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
276 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
277 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
281 static const struct option update_options[] = {
282 /* Careful: some of the options here set the defaults for update
283 * commands, but the flags given to an actual update command (and not to
284 * `imagex update' itself are also handled in
285 * update_command_add_option(). */
286 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
287 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
288 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
289 {T("command"), required_argument, NULL, IMAGEX_COMMAND_OPTION},
291 /* Default delete options */
292 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
293 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
295 /* Global add option */
296 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
298 /* Default add options */
299 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
300 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
301 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
302 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
303 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
304 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
310 # define _format_attribute(type, format_str, args_start) \
311 __attribute__((format(type, format_str, args_start)))
313 # define _format_attribute(type, format_str, args_start)
316 /* Print formatted error message to stderr. */
317 static void _format_attribute(printf, 1, 2)
318 imagex_error(const tchar *format, ...)
321 va_start(va, format);
322 tfputs(T("ERROR: "), stderr);
323 tvfprintf(stderr, format, va);
324 tputc(T('\n'), stderr);
328 /* Print formatted error message to stderr. */
329 static void _format_attribute(printf, 1, 2)
330 imagex_error_with_errno(const tchar *format, ...)
332 int errno_save = errno;
334 va_start(va, format);
335 tfputs(T("ERROR: "), stderr);
336 tvfprintf(stderr, format, va);
337 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
342 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
344 if (image == WIMLIB_NO_IMAGE) {
345 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
346 " Please specify a 1-based image index or "
348 " You may use `%"TS"' to list the images "
349 "contained in a WIM."),
350 image_name, wim_name, get_cmd_string(CMD_INFO, false));
357 verify_image_is_single(int image)
359 if (image == WIMLIB_ALL_IMAGES) {
360 imagex_error(T("Cannot specify all images for this action!"));
367 verify_image_exists_and_is_single(int image, const tchar *image_name,
368 const tchar *wim_name)
371 ret = verify_image_exists(image, image_name, wim_name);
373 ret = verify_image_is_single(image);
377 /* Parse the argument to --compress */
379 get_compression_type(const tchar *optarg)
381 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
382 return WIMLIB_COMPRESSION_TYPE_LZX;
383 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
384 return WIMLIB_COMPRESSION_TYPE_XPRESS;
385 else if (!tstrcasecmp(optarg, T("none")))
386 return WIMLIB_COMPRESSION_TYPE_NONE;
388 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
389 "\"maximum\", \"fast\", or \"none\"."), optarg);
390 return WIMLIB_COMPRESSION_TYPE_INVALID;
394 /* Returns the size of a file given its name, or -1 if the file does not exist
395 * or its size cannot be determined. */
397 file_get_size(const tchar *filename)
400 if (tstat(filename, &st) == 0)
406 tchar pat_ntfs_log[] = T("/$ntfs.log");
407 tchar pat_hiberfil_sys[] = T("/hiberfil.sys");
408 tchar pat_pagefile_sys[] = T("/pagefile.sys");
409 tchar pat_system_volume_information[] = T("/System Volume Information");
410 tchar pat_recycler[] = T("/RECYCLER");
411 tchar pat_windows_csc[] = T("/Windows/CSC");
413 tchar *default_pats[] = {
417 pat_system_volume_information,
422 static struct wimlib_capture_config default_capture_config = {
424 .num_pats = sizeof(default_pats) / sizeof(default_pats[0]),
425 .pats = default_pats,
430 PARSE_STRING_SUCCESS = 0,
431 PARSE_STRING_FAILURE = 1,
432 PARSE_STRING_NONE = 2,
436 * Parses a string token from an array of characters.
438 * Tokens are either whitespace-delimited, or double or single-quoted.
440 * @line_p: Pointer to the pointer to the line of data. Will be updated
441 * to point past the string token iff the return value is
442 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
445 * @len_p: @len_p initially stores the length of the line of data, which may
446 * be 0, and it will be updated to the number of bytes remaining in
447 * the line iff the return value is PARSE_STRING_SUCCESS.
449 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
450 * parsed string token will be returned here.
452 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
453 * PARSE_STRING_FAILURE if the data was invalid due to a missing
454 * closing quote; or PARSE_STRING_NONE if the line ended before the
455 * beginning of a string token was found.
458 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
461 tchar *line = *line_p;
465 /* Skip leading whitespace */
468 return PARSE_STRING_NONE;
469 if (!istspace(*line) && *line != T('\0'))
475 if (quote_char == T('"') || quote_char == T('\'')) {
480 line = tmemchr(line, quote_char, len);
482 imagex_error(T("Missing closing quote: %"TS), fn - 1);
483 return PARSE_STRING_FAILURE;
486 /* Unquoted string. Go until whitespace. Line is terminated
487 * by '\0', so no need to check 'len'. */
491 } while (!istspace(*line) && *line != T('\0'));
498 return PARSE_STRING_SUCCESS;
501 /* Parses a line of data (not an empty line or comment) in the source list file
502 * format. (See the man page for 'wimlib-imagex capture' for details on this
503 * format and the meaning.)
505 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
506 * len == 0. The data in @line will be modified by this function call.
508 * @len: Length of the line of data.
510 * @source: On success, the capture source and target described by the line is
511 * written into this destination. Note that it will contain pointers
512 * to data in the @line array.
514 * Returns true if the line was valid; false otherwise. */
516 parse_source_list_line(tchar *line, size_t len,
517 struct wimlib_capture_source *source)
521 ret = parse_string(&line, &len, &source->fs_source_path);
522 if (ret != PARSE_STRING_SUCCESS)
524 ret = parse_string(&line, &len, &source->wim_target_path);
525 if (ret == PARSE_STRING_NONE)
526 source->wim_target_path = source->fs_source_path;
527 return ret != PARSE_STRING_FAILURE;
530 /* Returns %true if the given line of length @len > 0 is a comment or empty line
531 * in the source list file format. */
533 is_comment_line(const tchar *line, size_t len)
538 if (!istspace(*line) && *line != T('\0'))
548 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
551 tchar *contents = *contents_p;
552 size_t nchars = *nchars_p;
555 for (i = 0; i < nchars; i++)
556 if (contents[i] == T('\n'))
559 /* Handle last line not terminated by a newline */
560 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
561 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
563 imagex_error(T("Out of memory!"));
566 contents[nchars] = T('\n');
567 *contents_p = contents;
575 /* Parses a file in the source list format. (See the man page for
576 * 'wimlib-imagex capture' for details on this format and the meaning.)
578 * @source_list_contents: Contents of the source list file. Note that this
579 * buffer will be modified to save memory allocations,
580 * and cannot be freed until the returned array of
581 * wimlib_capture_source's has also been freed.
583 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
586 * @nsources_ret: On success, the length of the returned array is
589 * Returns: An array of `struct wimlib_capture_source's that can be passed to
590 * the wimlib_add_image_multisource() function to specify how a WIM image is to
592 static struct wimlib_capture_source *
593 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
594 size_t *nsources_ret)
598 struct wimlib_capture_source *sources;
601 nlines = text_file_count_lines(source_list_contents_p,
602 &source_list_nchars);
606 /* Always allocate at least 1 slot, just in case the implementation of
607 * calloc() returns NULL if 0 bytes are requested. */
608 sources = calloc(nlines ?: 1, sizeof(*sources));
610 imagex_error(T("out of memory"));
613 p = *source_list_contents_p;
615 for (i = 0; i < nlines; i++) {
616 /* XXX: Could use rawmemchr() here instead, but it may not be
617 * available on all platforms. */
618 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
619 size_t len = endp - p + 1;
621 if (!is_comment_line(p, len)) {
622 if (!parse_source_list_line(p, len, &sources[j++])) {
635 enum capture_config_section {
636 CAPTURE_CONFIG_NO_SECTION,
637 CAPTURE_CONFIG_EXCLUSION_SECTION,
638 CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
639 CAPTURE_CONFIG_IGNORE_SECTION,
643 CAPTURE_CONFIG_INVALID_SECTION,
644 CAPTURE_CONFIG_CHANGED_SECTION,
645 CAPTURE_CONFIG_SAME_SECTION,
649 check_config_section(tchar *line, size_t len,
650 enum capture_config_section *cur_section)
652 while (istspace(*line))
656 return CAPTURE_CONFIG_SAME_SECTION;
659 tchar *endbrace = tstrrchr(line, T(']'));
661 return CAPTURE_CONFIG_SAME_SECTION;
663 if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
664 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
665 } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
666 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
667 } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
668 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
669 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
670 "of capture config file\n"),
672 } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
673 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
674 tfputs(T("WARNING: Ignoring [AlignmentList] section "
675 "of capture config file\n"),
678 imagex_error(T("Invalid capture config file section \"%"TS"\""),
680 return CAPTURE_CONFIG_INVALID_SECTION;
682 return CAPTURE_CONFIG_CHANGED_SECTION;
687 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
690 if (pat_list->num_pats == pat_list->num_allocated_pats) {
692 size_t num_allocated_pats = pat_list->num_pats + 8;
694 pats = realloc(pat_list->pats,
695 num_allocated_pats * sizeof(pat_list->pats[0]));
697 imagex_error(T("Out of memory!"));
700 pat_list->pats = pats;
701 pat_list->num_allocated_pats = num_allocated_pats;
703 pat_list->pats[pat_list->num_pats++] = pat;
708 parse_capture_config_line(tchar *line, size_t len,
709 enum capture_config_section *cur_section,
710 struct wimlib_capture_config *config)
715 ret = check_config_section(line, len, cur_section);
716 if (ret == CAPTURE_CONFIG_INVALID_SECTION)
718 if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
721 switch (*cur_section) {
722 case CAPTURE_CONFIG_NO_SECTION:
723 imagex_error(T("Line \"%"TS"\" is not in a section "
724 "(such as [ExclusionList]"), line);
726 case CAPTURE_CONFIG_EXCLUSION_SECTION:
727 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
729 return pattern_list_add_pattern(&config->exclusion_pats,
731 case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
732 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
734 return pattern_list_add_pattern(&config->exclusion_exception_pats,
736 case CAPTURE_CONFIG_IGNORE_SECTION:
743 parse_capture_config(tchar **contents_p, size_t nchars,
744 struct wimlib_capture_config *config)
749 enum capture_config_section cur_section;
751 memset(config, 0, sizeof(*config));
753 nlines = text_file_count_lines(contents_p, &nchars);
757 cur_section = CAPTURE_CONFIG_NO_SECTION;
759 for (i = 0; i < nlines; i++) {
760 tchar *endp = tmemchr(p, T('\n'), nchars);
761 size_t len = endp - p + 1;
763 if (!is_comment_line(p, len))
764 if (!parse_capture_config_line(p, len, &cur_section, config))
772 /* Reads the contents of a file into memory. */
774 file_get_contents(const tchar *filename, size_t *len_ret)
781 if (tstat(filename, &stbuf) != 0) {
782 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
787 fp = tfopen(filename, T("rb"));
789 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
793 buf = malloc(len ? len : 1);
795 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
796 "contents of file \"%"TS"\""), len, filename);
799 if (fread(buf, 1, len, fp) != len) {
800 imagex_error_with_errno(T("Failed to read %zu bytes from the "
801 "file \"%"TS"\""), len, filename);
815 /* Read standard input until EOF and return the full contents in a malloc()ed
816 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
819 stdin_get_contents(size_t *len_ret)
821 /* stdin can, of course, be a pipe or other non-seekable file, so the
822 * total length of the data cannot be pre-determined */
824 size_t newlen = 1024;
828 char *p = realloc(buf, newlen);
829 size_t bytes_read, bytes_to_read;
831 imagex_error(T("out of memory while reading stdin"));
835 bytes_to_read = newlen - pos;
836 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
838 if (bytes_read != bytes_to_read) {
843 imagex_error_with_errno(T("error reading stdin"));
857 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
860 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
862 *num_tchars_ret = num_bytes;
864 #else /* !__WIN32__ */
865 /* On Windows, translate the text to UTF-16LE */
869 if (num_bytes >= 2 &&
870 ((text[0] == 0xff && text[1] == 0xfe) ||
871 (text[0] <= 0x7f && text[1] == 0x00)))
873 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
874 * with something that looks like an ASCII character encoded as
875 * a UTF-16LE code unit. Assume the file is encoded as
876 * UTF-16LE. This is not a 100% reliable check. */
877 num_wchars = num_bytes / 2;
878 text_wstr = (wchar_t*)text;
880 /* File does not look like UTF-16LE. Assume it is encoded in
881 * the current Windows code page. I think these are always
882 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
883 * should work as expected. */
884 text_wstr = win32_mbs_to_wcs(text,
889 *num_tchars_ret = num_wchars;
891 #endif /* __WIN32__ */
895 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
900 contents = file_get_contents(filename, &num_bytes);
903 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
907 stdin_get_text_contents(size_t *num_tchars_ret)
912 contents = stdin_get_contents(&num_bytes);
915 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
918 #define TO_PERCENT(numerator, denominator) \
919 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
921 /* Given an enumerated value for WIM compression type, return a descriptive
924 get_data_type(int ctype)
927 case WIMLIB_COMPRESSION_TYPE_NONE:
928 return T("uncompressed");
929 case WIMLIB_COMPRESSION_TYPE_LZX:
930 return T("LZX-compressed");
931 case WIMLIB_COMPRESSION_TYPE_XPRESS:
932 return T("XPRESS-compressed");
937 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
938 #define MEBIBYTE_MIN_NBYTES 10000000ULL
939 #define KIBIBYTE_MIN_NBYTES 10000ULL
942 get_unit(uint64_t total_bytes, const tchar **name_ret)
944 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
945 *name_ret = T("GiB");
947 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
948 *name_ret = T("MiB");
950 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
951 *name_ret = T("KiB");
954 *name_ret = T("bytes");
959 /* Progress callback function passed to various wimlib functions. */
961 imagex_progress_func(enum wimlib_progress_msg msg,
962 const union wimlib_progress_info *info)
964 unsigned percent_done;
966 const tchar *unit_name;
970 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
971 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
972 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
973 info->write_streams.total_bytes);
974 if (info->write_streams.completed_streams == 0) {
975 const tchar *data_type;
977 data_type = get_data_type(info->write_streams.compression_type);
978 imagex_printf(T("Writing %"TS" data using %u thread%"TS"\n"),
979 data_type, info->write_streams.num_threads,
980 (info->write_streams.num_threads == 1) ? T("") : T("s"));
982 imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
983 "written (%u%% done)"),
984 info->write_streams.completed_bytes >> unit_shift,
986 info->write_streams.total_bytes >> unit_shift,
989 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
990 imagex_printf(T("\n"));
992 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
993 imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
994 if (*info->scan.wim_target_path) {
995 imagex_printf(T(" (loading as WIM path: "
996 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
997 info->scan.wim_target_path);
999 imagex_printf(T(" (loading as root of WIM image)...\n"));
1002 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1003 if (info->scan.excluded)
1004 imagex_printf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1006 imagex_printf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
1008 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
1010 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1011 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1012 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1013 info->integrity.total_bytes);
1014 imagex_printf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1015 "of %"PRIu64" %"TS" (%u%%) done"),
1016 info->integrity.filename,
1017 info->integrity.completed_bytes >> unit_shift,
1019 info->integrity.total_bytes >> unit_shift,
1022 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1023 imagex_printf(T("\n"));
1025 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1026 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1027 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1028 info->integrity.total_bytes);
1029 imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1030 "of %"PRIu64" %"TS" (%u%%) done"),
1031 info->integrity.completed_bytes >> unit_shift,
1033 info->integrity.total_bytes >> unit_shift,
1036 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1037 imagex_printf(T("\n"));
1039 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1040 imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1041 "to %"TS" \"%"TS"\"\n"),
1042 info->extract.image,
1043 info->extract.image_name,
1044 info->extract.wimfile_name,
1045 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1046 T("NTFS volume") : T("directory")),
1047 info->extract.target);
1049 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1050 imagex_printf(T("Extracting "
1051 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\" from image %d (\"%"TS"\") "
1052 "in \"%"TS"\" to \"%"TS"\"\n"),
1053 info->extract.extract_root_wim_source_path,
1054 info->extract.image,
1055 info->extract.image_name,
1056 info->extract.wimfile_name,
1057 info->extract.target);
1059 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
1060 /*imagex_printf(T("Applying directory structure to %"TS"\n"),*/
1061 /*info->extract.target);*/
1063 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1064 percent_done = TO_PERCENT(info->extract.completed_bytes,
1065 info->extract.total_bytes);
1066 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1067 imagex_printf(T("\rExtracting files: "
1068 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1069 info->extract.completed_bytes >> unit_shift,
1071 info->extract.total_bytes >> unit_shift,
1074 if (info->extract.completed_bytes >= info->extract.total_bytes)
1075 imagex_printf(T("\n"));
1077 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1078 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1079 imagex_printf(T("Setting timestamps on all extracted files...\n"));
1081 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1082 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1083 imagex_printf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1084 info->extract.target);
1087 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
1088 percent_done = TO_PERCENT(info->join.completed_bytes,
1089 info->join.total_bytes);
1090 unit_shift = get_unit(info->join.total_bytes, &unit_name);
1091 imagex_printf(T("Writing resources from part %u of %u: "
1092 "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written\n"),
1093 (info->join.completed_parts == info->join.total_parts) ?
1094 info->join.completed_parts : info->join.completed_parts + 1,
1095 info->join.total_parts,
1096 info->join.completed_bytes >> unit_shift,
1098 info->join.total_bytes >> unit_shift,
1102 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1103 percent_done = TO_PERCENT(info->split.completed_bytes,
1104 info->split.total_bytes);
1105 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1106 imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
1107 "%"PRIu64" %"TS" (%u%%) written\n"),
1108 info->split.part_name,
1109 info->split.cur_part_number,
1110 info->split.total_parts,
1111 info->split.completed_bytes >> unit_shift,
1113 info->split.total_bytes >> unit_shift,
1117 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1118 if (info->split.completed_bytes == info->split.total_bytes) {
1119 imagex_printf(T("Finished writing part %u of %u WIM parts\n"),
1120 info->split.cur_part_number,
1121 info->split.total_parts);
1124 case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1125 switch (info->update.command->op) {
1126 case WIMLIB_UPDATE_OP_DELETE:
1127 imagex_printf(T("Deleted WIM path "
1128 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1129 info->update.command->delete.wim_path);
1131 case WIMLIB_UPDATE_OP_RENAME:
1132 imagex_printf(T("Renamed WIM path "
1133 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
1134 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1135 info->update.command->rename.wim_source_path,
1136 info->update.command->rename.wim_target_path);
1138 case WIMLIB_UPDATE_OP_ADD:
1146 fflush(imagex_info_file);
1150 /* Open all the split WIM parts that correspond to a file glob.
1152 * @first_part specifies the first part of the split WIM and it may be either
1153 * included or omitted from the glob. */
1155 open_swms_from_glob(const tchar *swm_glob,
1156 const tchar *first_part,
1158 WIMStruct ***additional_swms_ret,
1159 unsigned *num_additional_swms_ret)
1161 unsigned num_additional_swms = 0;
1162 WIMStruct **additional_swms = NULL;
1166 /* Warning: glob() is replaced in Windows native builds */
1167 ret = tglob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
1169 if (ret == GLOB_NOMATCH) {
1170 imagex_error(T("Found no files for glob \"%"TS"\""),
1173 imagex_error_with_errno(T("Failed to process glob \"%"TS"\""),
1179 num_additional_swms = globbuf.gl_pathc;
1180 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
1181 if (!additional_swms) {
1182 imagex_error(T("Out of memory"));
1186 unsigned offset = 0;
1187 for (unsigned i = 0; i < num_additional_swms; i++) {
1188 if (tstrcmp(globbuf.gl_pathv[i], first_part) == 0) {
1192 ret = wimlib_open_wim(globbuf.gl_pathv[i],
1193 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
1194 &additional_swms[i - offset],
1195 imagex_progress_func);
1197 goto out_close_swms;
1199 *additional_swms_ret = additional_swms;
1200 *num_additional_swms_ret = num_additional_swms - offset;
1204 for (unsigned i = 0; i < num_additional_swms; i++)
1205 wimlib_free(additional_swms[i]);
1206 free(additional_swms);
1215 parse_num_threads(const tchar *optarg)
1218 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1219 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1220 imagex_error(T("Number of threads must be a non-negative integer!"));
1228 * Parse an option passed to an update command.
1230 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1233 * @option: Text string for the option (beginning with --)
1235 * @cmd: `struct wimlib_update_command' that is being constructed for
1238 * Returns true if the option was recognized; false if not.
1241 update_command_add_option(int op, const tchar *option,
1242 struct wimlib_update_command *cmd)
1244 bool recognized = true;
1246 case WIMLIB_UPDATE_OP_ADD:
1247 if (!tstrcmp(option, T("--verbose")))
1248 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1249 else if (!tstrcmp(option, T("--unix-data")))
1250 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1251 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1252 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1253 else if (!tstrcmp(option, T("--strict-acls")))
1254 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1255 else if (!tstrcmp(option, T("--dereference")))
1256 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1260 case WIMLIB_UPDATE_OP_DELETE:
1261 if (!tstrcmp(option, T("--force")))
1262 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1263 else if (!tstrcmp(option, T("--recursive")))
1264 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1275 /* How many nonoption arguments each `imagex update' command expects */
1276 static const unsigned update_command_num_nonoptions[] = {
1277 [WIMLIB_UPDATE_OP_ADD] = 2,
1278 [WIMLIB_UPDATE_OP_DELETE] = 1,
1279 [WIMLIB_UPDATE_OP_RENAME] = 2,
1283 update_command_add_nonoption(int op, const tchar *nonoption,
1284 struct wimlib_update_command *cmd,
1285 unsigned num_nonoptions)
1288 case WIMLIB_UPDATE_OP_ADD:
1289 if (num_nonoptions == 0)
1290 cmd->add.fs_source_path = (tchar*)nonoption;
1292 cmd->add.wim_target_path = (tchar*)nonoption;
1294 case WIMLIB_UPDATE_OP_DELETE:
1295 cmd->delete.wim_path = (tchar*)nonoption;
1297 case WIMLIB_UPDATE_OP_RENAME:
1298 if (num_nonoptions == 0)
1299 cmd->rename.wim_source_path = (tchar*)nonoption;
1301 cmd->rename.wim_target_path = (tchar*)nonoption;
1307 * Parse a command passed on stdin to `imagex update'.
1309 * @line: Text of the command.
1310 * @len: Length of the line, including a null terminator
1313 * @command: A `struct wimlib_update_command' to fill in from the parsed
1316 * @line_number: Line number of the command, for diagnostics.
1318 * Returns true on success; returns false on parse error.
1321 parse_update_command(tchar *line, size_t len,
1322 struct wimlib_update_command *command,
1326 tchar *command_name;
1328 size_t num_nonoptions;
1330 /* Get the command name ("add", "delete", "rename") */
1331 ret = parse_string(&line, &len, &command_name);
1332 if (ret != PARSE_STRING_SUCCESS)
1335 if (!tstrcasecmp(command_name, T("add"))) {
1336 op = WIMLIB_UPDATE_OP_ADD;
1337 } else if (!tstrcasecmp(command_name, T("delete"))) {
1338 op = WIMLIB_UPDATE_OP_DELETE;
1339 } else if (!tstrcasecmp(command_name, T("rename"))) {
1340 op = WIMLIB_UPDATE_OP_RENAME;
1342 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1343 command_name, line_number);
1348 /* Parse additional options and non-options as needed */
1353 ret = parse_string(&line, &len, &next_string);
1354 if (ret == PARSE_STRING_NONE) /* End of line */
1356 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1358 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1360 if (!update_command_add_option(op, next_string, command))
1362 imagex_error(T("Unrecognized option \"%"TS"\" to "
1363 "update command \"%"TS"\" on line %zu"),
1364 next_string, command_name, line_number);
1370 if (num_nonoptions == update_command_num_nonoptions[op])
1372 imagex_error(T("Unexpected argument \"%"TS"\" in "
1373 "update command on line %zu\n"
1374 " (The \"%"TS"\" command only "
1375 "takes %zu nonoption arguments!)\n"),
1376 next_string, line_number,
1377 command_name, num_nonoptions);
1380 update_command_add_nonoption(op, next_string,
1381 command, num_nonoptions);
1386 if (num_nonoptions != update_command_num_nonoptions[op]) {
1387 imagex_error(T("Not enough arguments to update command "
1388 "\"%"TS"\" on line %zu"), command_name, line_number);
1394 static struct wimlib_update_command *
1395 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1396 size_t *num_cmds_ret)
1400 struct wimlib_update_command *cmds;
1403 nlines = text_file_count_lines(cmd_file_contents_p,
1408 /* Always allocate at least 1 slot, just in case the implementation of
1409 * calloc() returns NULL if 0 bytes are requested. */
1410 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1412 imagex_error(T("out of memory"));
1415 p = *cmd_file_contents_p;
1417 for (i = 0; i < nlines; i++) {
1418 /* XXX: Could use rawmemchr() here instead, but it may not be
1419 * available on all platforms. */
1420 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1421 size_t len = endp - p + 1;
1423 if (!is_comment_line(p, len)) {
1424 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1435 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1436 * one image from a WIM file to a NTFS volume. */
1438 imagex_apply(int argc, tchar **argv, int cmd)
1441 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1445 const tchar *wimfile;
1446 const tchar *target;
1447 const tchar *image_num_or_name;
1448 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1450 const tchar *swm_glob = NULL;
1451 WIMStruct **additional_swms;
1452 unsigned num_additional_swms;
1454 for_opt(c, apply_options) {
1456 case IMAGEX_CHECK_OPTION:
1457 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1459 case IMAGEX_HARDLINK_OPTION:
1460 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1462 case IMAGEX_SYMLINK_OPTION:
1463 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1465 case IMAGEX_VERBOSE_OPTION:
1466 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
1468 case IMAGEX_REF_OPTION:
1471 case IMAGEX_UNIX_DATA_OPTION:
1472 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1474 case IMAGEX_NO_ACLS_OPTION:
1475 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1477 case IMAGEX_STRICT_ACLS_OPTION:
1478 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1480 case IMAGEX_NORPFIX_OPTION:
1481 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1483 case IMAGEX_RPFIX_OPTION:
1484 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1486 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1487 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1488 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1496 if (argc != 2 && argc != 3)
1501 if (!tstrcmp(wimfile, T("-"))) {
1502 /* Attempt to apply pipable WIM from standard input. */
1504 imagex_error(T("Imagex index or name must be explicitly "
1505 "specified when applying pipable WIM on "
1506 "standard input."));
1509 image_num_or_name = argv[1];
1512 num_additional_swms = 0;
1513 additional_swms = NULL;
1515 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1516 imagex_progress_func);
1521 /* Image explicitly specified. */
1522 image_num_or_name = argv[1];
1523 image = wimlib_resolve_image(wim, image_num_or_name);
1524 ret = verify_image_exists(image, image_num_or_name, wimfile);
1526 goto out_wimlib_free;
1529 /* No image specified; default to image 1, but only if the WIM
1530 * contains exactly one image. */
1531 struct wimlib_wim_info info;
1533 wimlib_get_wim_info(wim, &info);
1534 if (info.image_count != 1) {
1535 imagex_error(T("\"%"TS"\" contains %d images; "
1536 "Please select one (or all)."),
1537 wimfile, info.image_count);
1546 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1548 &num_additional_swms);
1550 goto out_wimlib_free;
1552 additional_swms = NULL;
1553 num_additional_swms = 0;
1559 /* Interpret a regular file or block device target as a NTFS
1563 if (tstat(target, &stbuf)) {
1564 if (errno != ENOENT) {
1565 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1571 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1572 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1578 win32_acquire_restore_privileges();
1581 ret = wimlib_extract_image(wim, image, target, extract_flags,
1582 additional_swms, num_additional_swms,
1583 imagex_progress_func);
1585 set_fd_to_binary_mode(STDIN_FILENO);
1586 ret = wimlib_extract_image_from_pipe(STDIN_FILENO,
1588 target, extract_flags,
1589 imagex_progress_func);
1592 imagex_printf(T("Done applying WIM image.\n"));
1594 win32_release_restore_privileges();
1597 for (unsigned i = 0; i < num_additional_swms; i++)
1598 wimlib_free(additional_swms[i]);
1599 free(additional_swms);
1606 usage(CMD_APPLY, stderr);
1611 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1612 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1613 * the desired image. 'wimlib-imagex append': add a new image to an existing
1616 imagex_capture_or_append(int argc, tchar **argv, int cmd)
1619 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1620 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
1621 int write_flags = 0;
1622 int compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1623 const tchar *wimfile;
1627 const tchar *flags_element = NULL;
1630 unsigned num_threads = 0;
1635 const tchar *config_file = NULL;
1637 struct wimlib_capture_config *config;
1639 bool source_list = false;
1640 size_t source_list_nchars;
1641 tchar *source_list_contents;
1642 bool capture_sources_malloced;
1643 struct wimlib_capture_source *capture_sources;
1645 bool name_defaulted;
1647 for_opt(c, capture_or_append_options) {
1649 case IMAGEX_BOOT_OPTION:
1650 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1652 case IMAGEX_CHECK_OPTION:
1653 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1654 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1656 case IMAGEX_NOCHECK_OPTION:
1657 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
1659 case IMAGEX_CONFIG_OPTION:
1660 config_file = optarg;
1662 case IMAGEX_COMPRESS_OPTION:
1663 compression_type = get_compression_type(optarg);
1664 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1667 case IMAGEX_FLAGS_OPTION:
1668 flags_element = optarg;
1670 case IMAGEX_DEREFERENCE_OPTION:
1671 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1673 case IMAGEX_VERBOSE_OPTION:
1674 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1676 case IMAGEX_THREADS_OPTION:
1677 num_threads = parse_num_threads(optarg);
1678 if (num_threads == UINT_MAX)
1681 case IMAGEX_REBUILD_OPTION:
1682 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1684 case IMAGEX_UNIX_DATA_OPTION:
1685 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1687 case IMAGEX_SOURCE_LIST_OPTION:
1690 case IMAGEX_NO_ACLS_OPTION:
1691 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1693 case IMAGEX_STRICT_ACLS_OPTION:
1694 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1696 case IMAGEX_RPFIX_OPTION:
1697 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1699 case IMAGEX_NORPFIX_OPTION:
1700 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1702 case IMAGEX_PIPABLE_OPTION:
1703 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1705 case IMAGEX_NOT_PIPABLE_OPTION:
1706 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
1715 if (argc < 2 || argc > 4)
1721 if (!tstrcmp(wimfile, T("-"))) {
1723 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
1724 imagex_error("Can't write a non-pipable WIM to "
1725 "standard output! Specify --pipable\n"
1726 " if you want to create a pipable WIM "
1727 "(but read the docs first).");
1731 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1733 if (cmd == CMD_APPEND) {
1734 imagex_error(T("Using standard output for append does "
1735 "not make sense."));
1738 wim_fd = STDOUT_FILENO;
1740 imagex_info_file = stderr;
1745 name_defaulted = false;
1747 /* Set default name to SOURCE argument, omitting any directory
1748 * prefixes and trailing slashes. This requires making a copy
1749 * of @source. Leave some free characters at the end in case we
1750 * append a number to keep the name unique. */
1751 size_t source_name_len;
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. */
1765 /* Set up capture sources in source list mode */
1766 if (source[0] == T('-') && source[1] == T('\0')) {
1767 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1769 source_list_contents = file_get_text_contents(source,
1770 &source_list_nchars);
1772 if (!source_list_contents)
1775 capture_sources = parse_source_list(&source_list_contents,
1778 if (!capture_sources) {
1780 goto out_free_source_list_contents;
1782 capture_sources_malloced = true;
1784 /* Set up capture source in non-source-list mode (could be
1785 * either "normal" mode or "NTFS mode"--- see the man page). */
1786 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1787 capture_sources[0].fs_source_path = source;
1788 capture_sources[0].wim_target_path = NULL;
1789 capture_sources[0].reserved = 0;
1791 capture_sources_malloced = false;
1792 source_list_contents = NULL;
1798 config_str = file_get_text_contents(config_file, &config_len);
1801 goto out_free_capture_sources;
1804 config = alloca(sizeof(*config));
1805 ret = parse_capture_config(&config_str, config_len, config);
1807 goto out_free_config;
1809 config = &default_capture_config;
1812 if (cmd == CMD_APPEND)
1813 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1814 imagex_progress_func);
1816 ret = wimlib_create_new_wim(compression_type, &wim);
1818 goto out_free_config;
1824 if (tstat(source, &stbuf) == 0) {
1825 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1826 imagex_printf(T("Capturing WIM image from NTFS "
1827 "filesystem on \"%"TS"\"\n"), source);
1828 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1831 if (errno != ENOENT) {
1832 imagex_error_with_errno(T("Failed to stat "
1833 "\"%"TS"\""), source);
1835 goto out_wimlib_free;
1841 if (cmd == CMD_APPEND && name_defaulted) {
1842 /* If the user did not specify an image name, and the basename
1843 * of the source already exists as an image name in the WIM
1844 * file, append a suffix to make it unique. */
1845 unsigned long conflict_idx;
1846 tchar *name_end = tstrchr(name, T('\0'));
1847 for (conflict_idx = 1;
1848 wimlib_image_name_in_use(wim, name);
1851 tsprintf(name_end, T(" (%lu)"), conflict_idx);
1855 win32_acquire_capture_privileges();
1858 ret = wimlib_add_image_multisource(wim,
1864 imagex_progress_func);
1866 goto out_release_privs;
1868 if (desc || flags_element) {
1869 /* User provided <DESCRIPTION> or <FLAGS> element. Get the
1870 * index of the image we just added, then use it to call the
1871 * appropriate functions. */
1872 struct wimlib_wim_info info;
1874 wimlib_get_wim_info(wim, &info);
1877 ret = wimlib_set_image_descripton(wim,
1881 goto out_release_privs;
1884 if (flags_element) {
1885 ret = wimlib_set_image_flags(wim, info.image_count,
1888 goto out_release_privs;
1892 /* Write the new WIM or overwrite the existing WIM with the new image
1894 if (cmd == CMD_APPEND) {
1895 ret = wimlib_overwrite(wim, write_flags, num_threads,
1896 imagex_progress_func);
1897 } else if (wimfile) {
1898 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
1899 write_flags, num_threads,
1900 imagex_progress_func);
1902 ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
1903 write_flags, num_threads,
1904 imagex_progress_func);
1908 win32_release_capture_privileges();
1913 if (config != &default_capture_config) {
1914 free(config->exclusion_pats.pats);
1915 free(config->exclusion_exception_pats.pats);
1918 out_free_capture_sources:
1919 if (capture_sources_malloced)
1920 free(capture_sources);
1921 out_free_source_list_contents:
1922 free(source_list_contents);
1933 /* Remove image(s) from a WIM. */
1935 imagex_delete(int argc, tchar **argv, int cmd)
1938 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1939 int write_flags = 0;
1940 const tchar *wimfile;
1941 const tchar *image_num_or_name;
1946 for_opt(c, delete_options) {
1948 case IMAGEX_CHECK_OPTION:
1949 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1950 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1952 case IMAGEX_SOFT_OPTION:
1953 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
1964 imagex_error(T("Must specify a WIM file"));
1966 imagex_error(T("Must specify an image"));
1970 image_num_or_name = argv[1];
1972 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1973 imagex_progress_func);
1977 image = wimlib_resolve_image(wim, image_num_or_name);
1979 ret = verify_image_exists(image, image_num_or_name, wimfile);
1981 goto out_wimlib_free;
1983 ret = wimlib_delete_image(wim, image);
1985 imagex_error(T("Failed to delete image from \"%"TS"\""),
1987 goto out_wimlib_free;
1990 ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
1992 imagex_error(T("Failed to write the file \"%"TS"\" with image "
1993 "deleted"), wimfile);
2001 usage(CMD_DELETE, stderr);
2007 print_full_path(const struct wimlib_dir_entry *wdentry, void *_ignore)
2009 int ret = tprintf(T("%"TS"\n"), wdentry->full_path);
2010 return (ret >= 0) ? 0 : -1;
2013 /* Print the files contained in an image(s) in a WIM file. */
2015 imagex_dir(int argc, tchar **argv, int cmd)
2017 const tchar *wimfile;
2018 WIMStruct *wim = NULL;
2021 const tchar *path = T("");
2024 for_opt(c, dir_options) {
2026 case IMAGEX_PATH_OPTION:
2037 imagex_error(T("Must specify a WIM file"));
2041 imagex_error(T("Too many arguments"));
2046 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &wim,
2047 imagex_progress_func);
2052 image = wimlib_resolve_image(wim, argv[1]);
2053 ret = verify_image_exists(image, argv[1], wimfile);
2055 goto out_wimlib_free;
2057 /* No image specified; default to image 1, but only if the WIM
2058 * contains exactly one image. */
2060 struct wimlib_wim_info info;
2062 wimlib_get_wim_info(wim, &info);
2063 if (info.image_count != 1) {
2064 imagex_error(T("\"%"TS"\" contains %d images; Please "
2065 "select one (or all)."),
2066 wimfile, info.image_count);
2073 ret = wimlib_iterate_dir_tree(wim, image, path,
2074 WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE,
2075 print_full_path, NULL);
2082 usage(CMD_DIR, stderr);
2087 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2090 imagex_export(int argc, tchar **argv, int cmd)
2094 int export_flags = 0;
2095 int write_flags = 0;
2096 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2097 const tchar *src_wimfile;
2098 const tchar *src_image_num_or_name;
2099 const tchar *dest_wimfile;
2101 const tchar *dest_name;
2102 const tchar *dest_desc;
2104 WIMStruct *dest_wim;
2109 const tchar *swm_glob = NULL;
2110 WIMStruct **additional_swms;
2111 unsigned num_additional_swms;
2112 unsigned num_threads = 0;
2114 for_opt(c, export_options) {
2116 case IMAGEX_BOOT_OPTION:
2117 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2119 case IMAGEX_CHECK_OPTION:
2120 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2121 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2123 case IMAGEX_NOCHECK_OPTION:
2124 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2126 case IMAGEX_COMPRESS_OPTION:
2127 compression_type = get_compression_type(optarg);
2128 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2131 case IMAGEX_REF_OPTION:
2134 case IMAGEX_THREADS_OPTION:
2135 num_threads = parse_num_threads(optarg);
2136 if (num_threads == UINT_MAX)
2139 case IMAGEX_REBUILD_OPTION:
2140 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2142 case IMAGEX_PIPABLE_OPTION:
2143 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2145 case IMAGEX_NOT_PIPABLE_OPTION:
2146 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
2154 if (argc < 3 || argc > 5)
2156 src_wimfile = argv[0];
2157 src_image_num_or_name = argv[1];
2158 dest_wimfile = argv[2];
2159 dest_name = (argc >= 4) ? argv[3] : NULL;
2160 dest_desc = (argc >= 5) ? argv[4] : NULL;
2161 ret = wimlib_open_wim(src_wimfile,
2162 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_wim,
2163 imagex_progress_func);
2167 /* Determine if the destination is an existing file or not. If so, we
2168 * try to append the exported image(s) to it; otherwise, we create a new
2169 * WIM containing the exported image(s). Furthermore, determine if we
2170 * need to write a pipable WIM directly to standard output. */
2172 if (tstrcmp(dest_wimfile, T("-")) == 0) {
2174 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2175 imagex_error("Can't write a non-pipable WIM to "
2176 "standard output! Specify --pipable\n"
2177 " if you want to create a pipable WIM "
2178 "(but read the docs first).");
2180 goto out_free_src_wim;
2183 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2185 dest_wimfile = NULL;
2186 dest_wim_fd = STDOUT_FILENO;
2187 imagex_info_file = stderr;
2190 if (dest_wimfile != NULL && tstat(dest_wimfile, &stbuf) == 0) {
2192 /* Destination file exists. */
2194 if (!S_ISREG(stbuf.st_mode)) {
2195 imagex_error(T("\"%"TS"\" is not a regular file"),
2198 goto out_free_src_wim;
2200 ret = wimlib_open_wim(dest_wimfile, open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2201 &dest_wim, imagex_progress_func);
2203 goto out_free_src_wim;
2205 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2206 /* The user specified a compression type, but we're
2207 * exporting to an existing WIM. Make sure the
2208 * specified compression type is the same as the
2209 * compression type of the existing destination WIM. */
2210 struct wimlib_wim_info dest_info;
2212 wimlib_get_wim_info(dest_wim, &dest_info);
2213 if (compression_type != dest_info.compression_type) {
2214 imagex_error(T("Cannot specify a compression type that is "
2215 "not the same as that used in the "
2216 "destination WIM"));
2218 goto out_free_dest_wim;
2224 if (errno != ENOENT) {
2225 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2228 goto out_free_src_wim;
2231 /* dest_wimfile is not an existing file, so create a new WIM. */
2233 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2234 /* The user did not specify a compression type; default
2235 * to that of the source WIM. */
2237 struct wimlib_wim_info src_info;
2239 wimlib_get_wim_info(src_wim, &src_info);
2240 compression_type = src_info.compression_type;
2242 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2244 goto out_free_src_wim;
2247 image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2248 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2250 goto out_free_dest_wim;
2253 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
2255 &num_additional_swms);
2257 goto out_free_dest_wim;
2259 additional_swms = NULL;
2260 num_additional_swms = 0;
2263 ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2264 dest_desc, export_flags, additional_swms,
2265 num_additional_swms, imagex_progress_func);
2270 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2271 imagex_progress_func);
2272 else if (dest_wimfile)
2273 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2274 write_flags, num_threads,
2275 imagex_progress_func);
2277 ret = wimlib_write_to_fd(dest_wim, dest_wim_fd,
2278 WIMLIB_ALL_IMAGES, write_flags,
2279 num_threads, imagex_progress_func);
2281 imagex_error(T("Export failed."));
2283 for (unsigned i = 0; i < num_additional_swms; i++)
2284 wimlib_free(additional_swms[i]);
2285 free(additional_swms);
2287 wimlib_free(dest_wim);
2289 wimlib_free(src_wim);
2294 usage(CMD_EXPORT, stderr);
2301 is_root_wim_path(const tchar *path)
2304 for (p = path; *p; p++)
2305 if (*p != T('\\') && *p != T('/'))
2311 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2312 const tchar *dest_dir)
2314 for (size_t i = 0; i < num_cmds; i++)
2315 if (cmds[i].fs_dest_path != dest_dir)
2316 free(cmds[i].fs_dest_path);
2320 static struct wimlib_extract_command *
2321 prepare_extract_commands(tchar **paths, unsigned num_paths,
2322 int extract_flags, tchar *dest_dir,
2323 size_t *num_cmds_ret)
2325 struct wimlib_extract_command *cmds;
2327 tchar *emptystr = T("");
2329 if (num_paths == 0) {
2333 num_cmds = num_paths;
2334 cmds = calloc(num_cmds, sizeof(cmds[0]));
2336 imagex_error(T("Out of memory!"));
2340 for (size_t i = 0; i < num_cmds; i++) {
2341 cmds[i].extract_flags = extract_flags;
2342 cmds[i].wim_source_path = paths[i];
2343 if (is_root_wim_path(paths[i])) {
2344 cmds[i].fs_dest_path = dest_dir;
2346 size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2347 cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2348 if (!cmds[i].fs_dest_path) {
2349 free_extract_commands(cmds, num_cmds, dest_dir);
2352 tsprintf(cmds[i].fs_dest_path,
2353 T("%"TS""OS_PREFERRED_PATH_SEPARATOR_STRING"%"TS),
2354 dest_dir, tbasename(paths[i]));
2357 *num_cmds_ret = num_cmds;
2361 /* Extract files or directories from a WIM image */
2363 imagex_extract(int argc, tchar **argv, int cmd)
2366 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2370 const tchar *wimfile;
2371 const tchar *image_num_or_name;
2372 tchar *dest_dir = T(".");
2373 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL | WIMLIB_EXTRACT_FLAG_NORPFIX;
2375 const tchar *swm_glob = NULL;
2376 WIMStruct **additional_swms;
2377 unsigned num_additional_swms;
2379 struct wimlib_extract_command *cmds;
2382 for_opt(c, extract_options) {
2384 case IMAGEX_CHECK_OPTION:
2385 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2387 case IMAGEX_VERBOSE_OPTION:
2388 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
2390 case IMAGEX_REF_OPTION:
2393 case IMAGEX_UNIX_DATA_OPTION:
2394 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2396 case IMAGEX_NO_ACLS_OPTION:
2397 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2399 case IMAGEX_STRICT_ACLS_OPTION:
2400 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2402 case IMAGEX_DEST_DIR_OPTION:
2405 case IMAGEX_TO_STDOUT_OPTION:
2406 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2407 imagex_info_file = stderr;
2408 imagex_be_quiet = true;
2410 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2411 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2412 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2425 image_num_or_name = argv[1];
2430 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2435 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2439 image = wimlib_resolve_image(wim, image_num_or_name);
2440 ret = verify_image_exists_and_is_single(image,
2444 goto out_wimlib_free;
2447 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2449 &num_additional_swms);
2451 goto out_wimlib_free;
2453 additional_swms = NULL;
2454 num_additional_swms = 0;
2458 win32_acquire_restore_privileges();
2461 ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
2462 additional_swms, num_additional_swms,
2463 imagex_progress_func);
2465 if (!imagex_be_quiet)
2466 imagex_printf(T("Done extracting files.\n"));
2467 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2468 tfprintf(stderr, T("Note: You can use `%"TS"' to see what "
2469 "files and directories\n"
2470 " are in the WIM image.\n"),
2471 get_cmd_string(CMD_INFO, false));
2474 win32_release_restore_privileges();
2476 for (unsigned i = 0; i < num_additional_swms; i++)
2477 wimlib_free(additional_swms[i]);
2478 free(additional_swms);
2482 free_extract_commands(cmds, num_cmds, dest_dir);
2487 usage(CMD_EXTRACT, stderr);
2493 static void print_byte_field(const uint8_t field[], size_t len)
2496 tprintf(T("%02hhx"), *field++);
2500 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2502 tputs(T("WIM Information:"));
2503 tputs(T("----------------"));
2504 tprintf(T("Path: %"TS"\n"), wimfile);
2505 tprintf(T("GUID: 0x"));
2506 print_byte_field(info->guid, sizeof(info->guid));
2508 tprintf(T("Image Count: %d\n"), info->image_count);
2509 tprintf(T("Compression: %"TS"\n"),
2510 wimlib_get_compression_type_string(info->compression_type));
2511 tprintf(T("Part Number: %d/%d\n"), info->part_number, info->total_parts);
2512 tprintf(T("Boot Index: %d\n"), info->boot_index);
2513 tprintf(T("Size: %"PRIu64" bytes\n"), info->total_bytes);
2514 tprintf(T("Integrity Info: %"TS"\n"),
2515 info->has_integrity_table ? T("yes") : T("no"));
2516 tprintf(T("Relative path junction: %"TS"\n"),
2517 info->has_rpfix ? T("yes") : T("no"));
2518 tprintf(T("Pipable: %"TS"\n"),
2519 info->pipable ? T("yes") : T("no"));
2524 print_resource(const struct wimlib_resource_entry *resource,
2528 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
2529 resource->uncompressed_size);
2531 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
2532 resource->compressed_size);
2534 tprintf(T("Offset = %"PRIu64" bytes\n"),
2538 tprintf(T("Part Number = %u\n"), resource->part_number);
2539 tprintf(T("Reference Count = %u\n"), resource->reference_count);
2541 tprintf(T("Hash = 0x"));
2542 print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2545 tprintf(T("Flags = "));
2546 if (resource->is_compressed)
2547 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED "));
2548 if (resource->is_metadata)
2549 tprintf(T("WIM_RESHDR_FLAG_METADATA "));
2550 if (resource->is_free)
2551 tprintf(T("WIM_RESHDR_FLAG_FREE "));
2552 if (resource->is_spanned)
2553 tprintf(T("WIM_RESHDR_FLAG_SPANNED "));
2560 print_lookup_table(WIMStruct *wim)
2562 wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2565 /* Prints information about a WIM file; also can mark an image as bootable,
2566 * change the name of an image, or change the description of an image. */
2568 imagex_info(int argc, tchar **argv, int cmd)
2573 bool nocheck = false;
2574 bool header = false;
2575 bool lookup_table = false;
2577 bool metadata = false;
2578 bool short_header = true;
2579 const tchar *xml_out_file = NULL;
2580 const tchar *wimfile;
2581 const tchar *image_num_or_name;
2582 const tchar *new_name;
2583 const tchar *new_desc;
2587 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2588 struct wimlib_wim_info info;
2590 for_opt(c, info_options) {
2592 case IMAGEX_BOOT_OPTION:
2595 case IMAGEX_CHECK_OPTION:
2598 case IMAGEX_NOCHECK_OPTION:
2601 case IMAGEX_HEADER_OPTION:
2603 short_header = false;
2605 case IMAGEX_LOOKUP_TABLE_OPTION:
2606 lookup_table = true;
2607 short_header = false;
2609 case IMAGEX_XML_OPTION:
2611 short_header = false;
2613 case IMAGEX_EXTRACT_XML_OPTION:
2614 xml_out_file = optarg;
2615 short_header = false;
2617 case IMAGEX_METADATA_OPTION:
2619 short_header = false;
2628 if (argc < 1 || argc > 4)
2632 image_num_or_name = (argc >= 2) ? argv[1] : T("all");
2633 new_name = (argc >= 3) ? argv[2] : NULL;
2634 new_desc = (argc >= 4) ? argv[3] : NULL;
2636 if (check && nocheck) {
2637 imagex_error(T("Can't specify both --check and --nocheck"));
2642 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2644 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2648 wimlib_get_wim_info(wim, &info);
2650 image = wimlib_resolve_image(wim, image_num_or_name);
2651 ret = WIMLIB_ERR_INVALID_IMAGE;
2652 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
2653 imagex_error(T("The image \"%"TS"\" does not exist in \"%"TS"\""),
2654 image_num_or_name, wimfile);
2656 imagex_error(T("If you would like to set the boot "
2657 "index to 0, specify image \"0\" with "
2658 "the --boot flag."));
2660 goto out_wimlib_free;
2663 if (boot && info.image_count == 0) {
2664 imagex_error(T("--boot is meaningless on a WIM with no images"));
2665 goto out_wimlib_free;
2668 if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
2670 imagex_error(T("Cannot specify the --boot flag "
2671 "without specifying a specific "
2672 "image in a multi-image WIM"));
2673 goto out_wimlib_free;
2676 imagex_error(T("Cannot specify the NEW_NAME "
2677 "without specifying a specific "
2678 "image in a multi-image WIM"));
2679 goto out_wimlib_free;
2683 /* Operations that print information are separated from operations that
2684 * recreate the WIM file. */
2685 if (!new_name && !boot) {
2687 /* Read-only operations */
2689 if (image == WIMLIB_NO_IMAGE) {
2690 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
2691 image_num_or_name, wimfile);
2692 goto out_wimlib_free;
2695 if (image == WIMLIB_ALL_IMAGES && short_header)
2696 print_wim_information(wimfile, &info);
2699 wimlib_print_header(wim);
2702 if (info.total_parts != 1) {
2703 tfprintf(stderr, T("Warning: Only showing the lookup table "
2704 "for part %d of a %d-part WIM.\n"),
2705 info.part_number, info.total_parts);
2707 print_lookup_table(wim);
2711 ret = wimlib_extract_xml_data(wim, stdout);
2713 goto out_wimlib_free;
2719 fp = tfopen(xml_out_file, T("wb"));
2721 imagex_error_with_errno(T("Failed to open the "
2722 "file \"%"TS"\" for "
2726 goto out_wimlib_free;
2728 ret = wimlib_extract_xml_data(wim, fp);
2730 imagex_error(T("Failed to close the file "
2736 goto out_wimlib_free;
2740 wimlib_print_available_images(wim, image);
2743 ret = wimlib_print_metadata(wim, image);
2745 goto out_wimlib_free;
2750 /* Modification operations */
2752 if (image == WIMLIB_ALL_IMAGES)
2755 if (image == WIMLIB_NO_IMAGE && new_name) {
2756 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
2757 "when using image 0"), new_name);
2759 goto out_wimlib_free;
2763 if (image == info.boot_index) {
2764 imagex_printf(T("Image %d is already marked as "
2765 "bootable.\n"), image);
2768 imagex_printf(T("Marking image %d as bootable.\n"),
2770 info.boot_index = image;
2771 ret = wimlib_set_wim_info(wim, &info,
2772 WIMLIB_CHANGE_BOOT_INDEX);
2774 goto out_wimlib_free;
2778 if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
2780 imagex_printf(T("Image %d is already named \"%"TS"\".\n"),
2784 imagex_printf(T("Changing the name of image %d to "
2785 "\"%"TS"\".\n"), image, new_name);
2786 ret = wimlib_set_image_name(wim, image, new_name);
2788 goto out_wimlib_free;
2792 const tchar *old_desc;
2793 old_desc = wimlib_get_image_description(wim, image);
2794 if (old_desc && !tstrcmp(old_desc, new_desc)) {
2795 imagex_printf(T("The description of image %d is already "
2796 "\"%"TS"\".\n"), image, new_desc);
2799 imagex_printf(T("Changing the description of image %d "
2800 "to \"%"TS"\".\n"), image, new_desc);
2801 ret = wimlib_set_image_descripton(wim, image,
2804 goto out_wimlib_free;
2808 /* Only call wimlib_overwrite() if something actually needs to
2810 if (boot || new_name || new_desc ||
2811 (check && !info.has_integrity_table) ||
2812 (nocheck && info.has_integrity_table))
2814 int write_flags = 0;
2817 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2819 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2820 ret = wimlib_overwrite(wim, write_flags, 1,
2821 imagex_progress_func);
2823 imagex_printf(T("The file \"%"TS"\" was not modified "
2824 "because nothing needed to be done.\n"),
2835 usage(CMD_INFO, stderr);
2841 /* Join split WIMs into one part WIM */
2843 imagex_join(int argc, tchar **argv, int cmd)
2846 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2847 int wim_write_flags = 0;
2848 const tchar *output_path;
2851 for_opt(c, join_options) {
2853 case IMAGEX_CHECK_OPTION:
2854 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2855 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2865 imagex_error(T("Must specify one or more split WIM (.swm) "
2869 output_path = argv[0];
2870 ret = wimlib_join((const tchar * const *)++argv,
2875 imagex_progress_func);
2880 usage(CMD_JOIN, stderr);
2885 /* Mounts an image using a FUSE mount. */
2887 imagex_mount_rw_or_ro(int argc, tchar **argv, int cmd)
2890 int mount_flags = 0;
2891 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2892 const tchar *swm_glob = NULL;
2893 const tchar *staging_dir = NULL;
2894 const tchar *wimfile;
2899 WIMStruct **additional_swms;
2900 unsigned num_additional_swms;
2902 if (cmd == CMD_MOUNTRW) {
2903 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
2904 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2907 for_opt(c, mount_options) {
2909 case IMAGEX_ALLOW_OTHER_OPTION:
2910 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
2912 case IMAGEX_CHECK_OPTION:
2913 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2915 case IMAGEX_DEBUG_OPTION:
2916 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
2918 case IMAGEX_STREAMS_INTERFACE_OPTION:
2919 if (!tstrcasecmp(optarg, T("none")))
2920 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
2921 else if (!tstrcasecmp(optarg, T("xattr")))
2922 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2923 else if (!tstrcasecmp(optarg, T("windows")))
2924 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
2926 imagex_error(T("Unknown stream interface \"%"TS"\""),
2931 case IMAGEX_REF_OPTION:
2934 case IMAGEX_STAGING_DIR_OPTION:
2935 staging_dir = optarg;
2937 case IMAGEX_UNIX_DATA_OPTION:
2938 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
2946 if (argc != 2 && argc != 3)
2951 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2956 /* Image explicitly specified. */
2957 image = wimlib_resolve_image(wim, argv[1]);
2959 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
2963 /* No image specified; default to image 1, but only if the WIM
2964 * contains exactly one image. */
2965 struct wimlib_wim_info info;
2967 wimlib_get_wim_info(wim, &info);
2968 if (info.image_count != 1) {
2969 imagex_error(T("\"%"TS"\" contains %d images; Please "
2970 "select one."), wimfile, info.image_count);
2979 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2981 &num_additional_swms);
2985 additional_swms = NULL;
2986 num_additional_swms = 0;
2989 ret = wimlib_mount_image(wim, image, dir, mount_flags, additional_swms,
2990 num_additional_swms, staging_dir);
2992 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
2994 image, wimfile, dir);
2996 for (unsigned i = 0; i < num_additional_swms; i++)
2997 wimlib_free(additional_swms[i]);
2998 free(additional_swms);
3010 /* Rebuild a WIM file */
3012 imagex_optimize(int argc, tchar **argv, int cmd)
3015 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3016 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
3019 const tchar *wimfile;
3022 unsigned num_threads = 0;
3024 for_opt(c, optimize_options) {
3026 case IMAGEX_CHECK_OPTION:
3027 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3028 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3030 case IMAGEX_NOCHECK_OPTION:
3031 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3033 case IMAGEX_RECOMPRESS_OPTION:
3034 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3036 case IMAGEX_THREADS_OPTION:
3037 num_threads = parse_num_threads(optarg);
3038 if (num_threads == UINT_MAX)
3041 case IMAGEX_PIPABLE_OPTION:
3042 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
3044 case IMAGEX_NOT_PIPABLE_OPTION:
3045 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
3059 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3063 old_size = file_get_size(wimfile);
3064 tprintf(T("\"%"TS"\" original size: "), wimfile);
3066 tputs(T("Unknown"));
3068 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3070 ret = wimlib_overwrite(wim, write_flags, num_threads,
3071 imagex_progress_func);
3073 imagex_error(T("Optimization of \"%"TS"\" failed."), wimfile);
3074 goto out_wimlib_free;
3077 new_size = file_get_size(wimfile);
3078 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3080 tputs(T("Unknown"));
3082 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3084 tfputs(T("Space saved: "), stdout);
3085 if (new_size != -1 && old_size != -1) {
3086 tprintf(T("%lld KiB\n"),
3087 ((long long)old_size - (long long)new_size) >> 10);
3089 tputs(T("Unknown"));
3098 usage(CMD_OPTIMIZE, stderr);
3104 /* Split a WIM into a spanned set */
3106 imagex_split(int argc, tchar **argv, int cmd)
3110 int write_flags = 0;
3111 unsigned long part_size;
3116 for_opt(c, split_options) {
3118 case IMAGEX_CHECK_OPTION:
3119 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3120 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3132 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3133 if (tmp == argv[2] || *tmp) {
3134 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3135 imagex_error(T("The part size must be an integer or "
3136 "floating-point number of megabytes."));
3139 ret = wimlib_open_wim(argv[0], open_flags, &wim, imagex_progress_func);
3143 ret = wimlib_split(wim, argv[1], part_size, write_flags, imagex_progress_func);
3149 usage(CMD_SPLIT, stderr);
3155 /* Unmounts a mounted WIM image. */
3157 imagex_unmount(int argc, tchar **argv, int cmd)
3160 int unmount_flags = 0;
3163 for_opt(c, unmount_options) {
3165 case IMAGEX_COMMIT_OPTION:
3166 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3168 case IMAGEX_CHECK_OPTION:
3169 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3171 case IMAGEX_REBUILD_OPTION:
3172 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3174 case IMAGEX_LAZY_OPTION:
3175 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3186 ret = wimlib_unmount_image(argv[0], unmount_flags,
3187 imagex_progress_func);
3189 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3194 usage(CMD_UNMOUNT, stderr);
3200 * Add, delete, or rename files in a WIM image.
3203 imagex_update(int argc, tchar **argv, int cmd)
3205 const tchar *wimfile;
3209 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3210 int write_flags = 0;
3211 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3212 int default_add_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
3213 int default_delete_flags = 0;
3214 unsigned num_threads = 0;
3216 tchar *cmd_file_contents;
3217 size_t cmd_file_nchars;
3218 struct wimlib_update_command *cmds;
3220 tchar *command_str = NULL;
3222 const tchar *config_file = NULL;
3224 struct wimlib_capture_config *config;
3226 for_opt(c, update_options) {
3228 /* Generic or write options */
3229 case IMAGEX_THREADS_OPTION:
3230 num_threads = parse_num_threads(optarg);
3231 if (num_threads == UINT_MAX)
3234 case IMAGEX_CHECK_OPTION:
3235 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3236 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3238 case IMAGEX_REBUILD_OPTION:
3239 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3241 case IMAGEX_COMMAND_OPTION:
3243 imagex_error(T("--command may only be specified "
3244 "one time. Please provide\n"
3245 " the update commands "
3246 "on standard input instead."));
3249 command_str = tstrdup(optarg);
3251 imagex_error(T("Out of memory!"));
3255 /* Default delete options */
3256 case IMAGEX_FORCE_OPTION:
3257 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3259 case IMAGEX_RECURSIVE_OPTION:
3260 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3263 /* Global add option */
3264 case IMAGEX_CONFIG_OPTION:
3265 config_file = optarg;
3268 /* Default add options */
3269 case IMAGEX_VERBOSE_OPTION:
3270 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
3272 case IMAGEX_DEREFERENCE_OPTION:
3273 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
3275 case IMAGEX_UNIX_DATA_OPTION:
3276 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
3278 case IMAGEX_NO_ACLS_OPTION:
3279 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
3281 case IMAGEX_STRICT_ACLS_OPTION:
3282 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
3291 if (argc != 1 && argc != 2)
3295 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3297 goto out_free_command_str;
3300 /* Image explicitly specified. */
3301 image = wimlib_resolve_image(wim, argv[1]);
3302 ret = verify_image_exists_and_is_single(image, argv[1],
3305 goto out_wimlib_free;
3307 /* No image specified; default to image 1, but only if the WIM
3308 * contains exactly one image. */
3309 struct wimlib_wim_info info;
3311 wimlib_get_wim_info(wim, &info);
3312 if (info.image_count != 1) {
3313 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3314 wimfile, info.image_count);
3321 /* Parse capture configuration file if specified */
3325 config_str = file_get_text_contents(config_file, &config_len);
3328 goto out_wimlib_free;
3331 config = alloca(sizeof(*config));
3332 ret = parse_capture_config(&config_str, config_len, config);
3334 goto out_free_config;
3336 config = &default_capture_config;
3339 /* Read update commands from standard input, or the command string if
3342 cmd_file_contents = NULL;
3343 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3346 if (isatty(STDIN_FILENO)) {
3347 tputs(T("Reading update commands from standard input..."));
3348 recommend_man_page(CMD_UPDATE, stdout);
3350 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3351 if (!cmd_file_contents) {
3353 goto out_free_config;
3356 /* Parse the update commands */
3357 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3362 goto out_free_cmd_file_contents;
3365 /* Set default flags and capture config on the update commands */
3366 bool have_add_command = false;
3367 for (size_t i = 0; i < num_cmds; i++) {
3368 switch (cmds[i].op) {
3369 case WIMLIB_UPDATE_OP_ADD:
3370 cmds[i].add.add_flags |= default_add_flags;
3371 cmds[i].add.config = config;
3372 have_add_command = true;
3374 case WIMLIB_UPDATE_OP_DELETE:
3375 cmds[i].delete.delete_flags |= default_delete_flags;
3383 if (have_add_command)
3384 win32_acquire_capture_privileges();
3387 /* Execute the update commands */
3388 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3389 imagex_progress_func);
3391 goto out_release_privs;
3393 /* Overwrite the updated WIM */
3394 ret = wimlib_overwrite(wim, write_flags, num_threads,
3395 imagex_progress_func);
3398 if (have_add_command)
3399 win32_release_capture_privileges();
3402 out_free_cmd_file_contents:
3403 free(cmd_file_contents);
3405 if (config != &default_capture_config) {
3406 free(config->exclusion_pats.pats);
3407 free(config->exclusion_exception_pats.pats);
3412 out_free_command_str:
3417 usage(CMD_UPDATE, stderr);
3420 goto out_free_command_str;
3425 struct imagex_command {
3427 int (*func)(int argc, tchar **argv, int cmd);
3430 static const struct imagex_command imagex_commands[] = {
3431 [CMD_APPEND] = {T("append"), imagex_capture_or_append},
3432 [CMD_APPLY] = {T("apply"), imagex_apply},
3433 [CMD_CAPTURE] = {T("capture"), imagex_capture_or_append},
3434 [CMD_DELETE] = {T("delete"), imagex_delete},
3435 [CMD_DIR ] = {T("dir"), imagex_dir},
3436 [CMD_EXPORT] = {T("export"), imagex_export},
3437 [CMD_EXTRACT] = {T("extract"), imagex_extract},
3438 [CMD_INFO] = {T("info"), imagex_info},
3439 [CMD_JOIN] = {T("join"), imagex_join},
3440 [CMD_MOUNT] = {T("mount"), imagex_mount_rw_or_ro},
3441 [CMD_MOUNTRW] = {T("mountrw"), imagex_mount_rw_or_ro},
3442 [CMD_OPTIMIZE] = {T("optimize"), imagex_optimize},
3443 [CMD_SPLIT] = {T("split"), imagex_split},
3444 [CMD_UNMOUNT] = {T("unmount"), imagex_unmount},
3445 [CMD_UPDATE] = {T("update"), imagex_update},
3448 static const tchar *usage_strings[] = {
3451 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
3452 " [DESCRIPTION] [--boot] [--check] [--nocheck]\n"
3453 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
3454 " [--config=FILE] [--threads=NUM_THREADS] [--rebuild]\n"
3455 " [--unix-data] [--source-list] [--no-acls]\n"
3456 " [--strict-acls] [--rpfix] [--norpfix] [--pipable]\n"
3457 " [--not-pipable]\n"
3461 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
3462 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
3463 " [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n"
3464 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3465 " [--include-invalid-names]\n"
3469 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
3470 " [DESCRIPTION] [--boot] [--check] [--nocheck]\n"
3471 " [--compress=TYPE] [--flags EDITION_ID] [--verbose]\n"
3472 " [--dereference] [--config=FILE]\n"
3473 " [--threads=NUM_THREADS] [--unix-data] [--source-list]\n"
3474 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3475 " [--pipable] [--not-pipable]\n"
3479 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n"
3484 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH]\n"
3488 " %"TS" SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
3489 " DEST_WIMFILE [DEST_IMAGE_NAME] [DEST_IMAGE_DESCRIPTION]\n"
3490 " [--boot] [--check] [--nocheck] [--compress=TYPE]\n"
3491 " [--ref=\"GLOB\"] [--threads=NUM_THREADS] [--rebuild]\n"
3492 " [--pipable] [--not-pipable]\n"
3496 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME) [PATH...]\n"
3497 " [--check] [--ref=\"GLOB\"] [--verbose] [--unix-data]\n"
3498 " [--no-acls] [--strict-acls] [--to-stdout] [--dest-dir=CMD_DIR]\n"
3499 " [--include-invalid-names]\n"
3503 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
3504 " [NEW_DESC] [--boot] [--check] [--nocheck] [--header]\n"
3505 " [--lookup-table] [--xml] [--extract-xml FILE]\n"
3510 " %"TS" [--check] WIMFILE SPLIT_WIM...\n"
3514 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
3515 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
3516 " [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n"
3520 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
3521 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
3522 " [--staging-dir=CMD_DIR] [--unix-data] [--allow-other]\n"
3526 " %"TS" WIMFILE [--check] [--nocheck] [--recompress]\n"
3527 " [--threads=NUM_THREADS] [--pipable] [--not-pipable]\n"
3531 " %"TS" WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n"
3535 " %"TS" DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
3539 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
3540 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
3541 " [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
3545 static const tchar *invocation_name;
3546 static bool using_cmd_from_invocation_name = false;
3548 static const tchar *get_cmd_string(int cmd, bool nospace)
3551 if (using_cmd_from_invocation_name || cmd == CMD_NONE) {
3552 return invocation_name;
3554 const tchar *format;
3555 static tchar buf[50];
3558 format = T("%"TS"-%"TS"");
3560 format = T("%"TS" %"TS"");
3561 tsprintf(buf, format, invocation_name, imagex_commands[cmd].name);
3569 static const tchar *format =
3571 "%"TS" (" PACKAGE ") " PACKAGE_VERSION "\n"
3572 "Copyright (C) 2012, 2013 Eric Biggers\n"
3573 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
3574 "This is free software: you are free to change and redistribute it.\n"
3575 "There is NO WARRANTY, to the extent permitted by law.\n"
3577 "Report bugs to "PACKAGE_BUGREPORT".\n"
3579 tprintf(format, invocation_name);
3584 help_or_version(int argc, tchar **argv, int cmd)
3589 for (i = 1; i < argc; i++) {
3591 if (p[0] == T('-') && p[1] == T('-')) {
3593 if (!tstrcmp(p, T("help"))) {
3594 if (cmd == CMD_NONE)
3599 } else if (!tstrcmp(p, T("version"))) {
3608 print_usage_string(int cmd, FILE *fp)
3610 tfprintf(fp, usage_strings[cmd], get_cmd_string(cmd, false));
3614 recommend_man_page(int cmd, FILE *fp)
3616 const tchar *format_str;
3618 format_str = T("See %"TS".pdf in the doc directory for more details.\n");
3620 format_str = T("Try `man %"TS"' for more details.\n");
3622 tfprintf(fp, format_str, get_cmd_string(cmd, true));
3626 usage(int cmd, FILE *fp)
3628 tfprintf(fp, T("Usage:\n"));
3629 print_usage_string(cmd, fp);
3630 tfprintf(fp, T("\n"));
3631 recommend_man_page(cmd, fp);
3637 tfprintf(fp, T("Usage:\n"));
3638 for (int cmd = 0; cmd < CMD_MAX; cmd++) {
3639 print_usage_string(cmd, fp);
3640 tfprintf(fp, T("\n"));
3642 static const tchar *extra =
3645 " %"TS" --version\n"
3647 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
3650 tfprintf(fp, extra, invocation_name, invocation_name);
3651 recommend_man_page(CMD_NONE, fp);
3654 /* Entry point for wimlib's ImageX implementation. On UNIX the command
3655 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
3656 * something else), while an Windows the command arguments will be UTF-16LE
3657 * encoded 'wchar_t' strings. */
3660 wmain(int argc, wchar_t **argv, wchar_t **envp)
3662 main(int argc, char **argv)
3669 imagex_info_file = stdout;
3670 invocation_name = basename(argv[0]);
3673 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
3674 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
3678 setlocale(LC_ALL, "");
3679 codeset = nl_langinfo(CODESET);
3680 if (!strstr(codeset, "UTF-8") &&
3681 !strstr(codeset, "UTF8") &&
3682 !strstr(codeset, "utf-8") &&
3683 !strstr(codeset, "utf8"))
3686 "WARNING: Running %"TS" in a UTF-8 locale is recommended!\n"
3687 " Maybe try: `export LANG=en_US.UTF-8'?\n"
3688 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
3689 " to any value to force wimlib to use UTF-8.\n",
3694 #endif /* !__WIN32__ */
3696 /* Allow being invoked as wimCOMMAND (e.g. wimapply). */
3698 if (!tstrncmp(invocation_name, T("wim"), 3) &&
3699 tstrcmp(invocation_name, T(IMAGEX_PROGNAME))) {
3700 for (int i = 0; i < CMD_MAX; i++) {
3701 if (!tstrcmp(invocation_name + 3,
3702 imagex_commands[i].name))
3704 using_cmd_from_invocation_name = true;
3711 /* Unless already known from the invocation name, search for the
3712 * function to handle the specified subcommand. */
3713 if (cmd == CMD_NONE) {
3715 imagex_error(T("No command specified!\n"));
3719 for (int i = 0; i < CMD_MAX; i++) {
3720 if (!tstrcmp(argv[1], imagex_commands[i].name)) {
3725 if (cmd != CMD_NONE) {
3731 /* Handle --help and --version for all commands. Note that this will
3732 * not return if either of these arguments are present. */
3733 help_or_version(argc, argv, cmd);
3735 if (cmd == CMD_NONE) {
3736 imagex_error(T("Unrecognized command: `%"TS"'\n"), argv[1]);
3741 /* The user may like to see more informative error messages. */
3742 wimlib_set_print_errors(true);
3744 /* Initialize the library. */
3745 ret = wimlib_global_init(init_flags);
3747 goto out_check_status;
3749 /* Call the command handler function. */
3750 ret = imagex_commands[cmd].func(argc, argv, cmd);
3752 /* Check for error writing to standard output, especially since for some
3753 * commands, writing to standard output is part of the program's actual
3754 * behavior and not just for informational purposes. */
3755 if (ferror(stdout) || fclose(stdout)) {
3756 imagex_error_with_errno(T("error writing to standard output"));
3761 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
3762 * outside of the wimlib library code. 0 indicates success. > 0
3763 * indicates a wimlib error code from which an error message can be
3766 imagex_error(T("Exiting with error code %d:\n"
3768 wimlib_get_error_string(ret));
3769 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
3770 imagex_error_with_errno(T("errno"));
3772 /* Make the library free any resources it's holding (not strictly
3773 * necessary because the process is ending anyway). */
3774 wimlib_global_cleanup();