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)
68 /* Don't confuse the user by presenting the mounting commands on Windows when
69 * they will never work. However on UNIX-like systems we always present them,
70 * even if WITH_FUSE is not defined at this point, as to not tie the build of
71 * wimlib-imagex to a specific build of wimlib. */
73 # define WIM_MOUNTING_SUPPORTED 0
75 # define WIM_MOUNTING_SUPPORTED 1
78 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
80 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
95 #if WIM_MOUNTING_SUPPORTED
101 #if WIM_MOUNTING_SUPPORTED
108 static void usage(int cmd, FILE *fp);
109 static void usage_all(FILE *fp);
110 static void recommend_man_page(int cmd, FILE *fp);
111 static const tchar *get_cmd_string(int cmd, bool nospace);
113 static bool imagex_be_quiet = false;
114 static FILE *imagex_info_file;
116 #define imagex_printf(format, ...) \
117 tfprintf(imagex_info_file, format, ##__VA_ARGS__)
120 IMAGEX_ALLOW_OTHER_OPTION,
123 IMAGEX_COMMAND_OPTION,
124 IMAGEX_COMMIT_OPTION,
125 IMAGEX_COMPRESS_OPTION,
126 IMAGEX_CONFIG_OPTION,
128 IMAGEX_DEREFERENCE_OPTION,
129 IMAGEX_DEST_DIR_OPTION,
130 IMAGEX_EXTRACT_XML_OPTION,
133 IMAGEX_HARDLINK_OPTION,
134 IMAGEX_HEADER_OPTION,
135 IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
137 IMAGEX_LOOKUP_TABLE_OPTION,
138 IMAGEX_METADATA_OPTION,
139 IMAGEX_NORPFIX_OPTION,
140 IMAGEX_NOCHECK_OPTION,
141 IMAGEX_NO_ACLS_OPTION,
142 IMAGEX_NOT_PIPABLE_OPTION,
144 IMAGEX_PIPABLE_OPTION,
145 IMAGEX_REBUILD_OPTION,
146 IMAGEX_RECOMPRESS_OPTION,
147 IMAGEX_RECURSIVE_OPTION,
151 IMAGEX_SOURCE_LIST_OPTION,
152 IMAGEX_STAGING_DIR_OPTION,
153 IMAGEX_STREAMS_INTERFACE_OPTION,
154 IMAGEX_STRICT_ACLS_OPTION,
155 IMAGEX_SYMLINK_OPTION,
156 IMAGEX_THREADS_OPTION,
157 IMAGEX_TO_STDOUT_OPTION,
158 IMAGEX_UNIX_DATA_OPTION,
159 IMAGEX_VERBOSE_OPTION,
163 static const struct option apply_options[] = {
164 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
165 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
166 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
167 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
168 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
169 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
170 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
171 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
172 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
173 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
174 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
175 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
179 static const struct option capture_or_append_options[] = {
180 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
181 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
182 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
183 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
184 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
185 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
186 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
187 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
188 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
189 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
190 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
191 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
192 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
193 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
194 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
195 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
196 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
197 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
198 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
199 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
203 static const struct option delete_options[] = {
204 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
205 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
209 static const struct option dir_options[] = {
210 {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
214 static const struct option export_options[] = {
215 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
216 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
217 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
218 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
219 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
220 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
221 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
222 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
223 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
224 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
228 static const struct option extract_options[] = {
229 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
230 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
231 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
232 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
233 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
234 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
235 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
236 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
237 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
238 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
242 static const struct option info_options[] = {
243 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
244 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
245 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
246 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
247 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
248 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
249 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
250 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
251 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
255 static const struct option join_options[] = {
256 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
260 static const struct option mount_options[] = {
261 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
262 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
263 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
264 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
265 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
266 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
267 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
271 static const struct option optimize_options[] = {
272 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
273 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
274 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
275 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
276 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
277 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
278 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
282 static const struct option split_options[] = {
283 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
287 static const struct option unmount_options[] = {
288 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
289 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
290 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
291 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
295 static const struct option update_options[] = {
296 /* Careful: some of the options here set the defaults for update
297 * commands, but the flags given to an actual update command (and not to
298 * `imagex update' itself are also handled in
299 * update_command_add_option(). */
300 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
301 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
302 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
303 {T("command"), required_argument, NULL, IMAGEX_COMMAND_OPTION},
305 /* Default delete options */
306 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
307 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
309 /* Global add option */
310 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
312 /* Default add options */
313 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
314 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
315 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
316 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
317 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
318 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
324 # define _format_attribute(type, format_str, args_start) \
325 __attribute__((format(type, format_str, args_start)))
327 # define _format_attribute(type, format_str, args_start)
330 /* Print formatted error message to stderr. */
331 static void _format_attribute(printf, 1, 2)
332 imagex_error(const tchar *format, ...)
335 va_start(va, format);
336 tfputs(T("ERROR: "), stderr);
337 tvfprintf(stderr, format, va);
338 tputc(T('\n'), stderr);
342 /* Print formatted error message to stderr. */
343 static void _format_attribute(printf, 1, 2)
344 imagex_error_with_errno(const tchar *format, ...)
346 int errno_save = errno;
348 va_start(va, format);
349 tfputs(T("ERROR: "), stderr);
350 tvfprintf(stderr, format, va);
351 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
356 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
358 if (image == WIMLIB_NO_IMAGE) {
359 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
360 " Please specify a 1-based image index or "
362 " You may use `%"TS"' to list the images "
363 "contained in a WIM."),
364 image_name, wim_name, get_cmd_string(CMD_INFO, false));
371 verify_image_is_single(int image)
373 if (image == WIMLIB_ALL_IMAGES) {
374 imagex_error(T("Cannot specify all images for this action!"));
381 verify_image_exists_and_is_single(int image, const tchar *image_name,
382 const tchar *wim_name)
385 ret = verify_image_exists(image, image_name, wim_name);
387 ret = verify_image_is_single(image);
391 /* Parse the argument to --compress */
393 get_compression_type(const tchar *optarg)
395 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
396 return WIMLIB_COMPRESSION_TYPE_LZX;
397 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
398 return WIMLIB_COMPRESSION_TYPE_XPRESS;
399 else if (!tstrcasecmp(optarg, T("none")))
400 return WIMLIB_COMPRESSION_TYPE_NONE;
402 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
403 "\"maximum\", \"fast\", or \"none\"."), optarg);
404 return WIMLIB_COMPRESSION_TYPE_INVALID;
408 /* Returns the size of a file given its name, or -1 if the file does not exist
409 * or its size cannot be determined. */
411 file_get_size(const tchar *filename)
414 if (tstat(filename, &st) == 0)
420 tchar pat_ntfs_log[] = T("/$ntfs.log");
421 tchar pat_hiberfil_sys[] = T("/hiberfil.sys");
422 tchar pat_pagefile_sys[] = T("/pagefile.sys");
423 tchar pat_system_volume_information[] = T("/System Volume Information");
424 tchar pat_recycler[] = T("/RECYCLER");
425 tchar pat_windows_csc[] = T("/Windows/CSC");
427 tchar *default_pats[] = {
431 pat_system_volume_information,
436 static struct wimlib_capture_config default_capture_config = {
438 .num_pats = sizeof(default_pats) / sizeof(default_pats[0]),
439 .pats = default_pats,
444 PARSE_STRING_SUCCESS = 0,
445 PARSE_STRING_FAILURE = 1,
446 PARSE_STRING_NONE = 2,
450 * Parses a string token from an array of characters.
452 * Tokens are either whitespace-delimited, or double or single-quoted.
454 * @line_p: Pointer to the pointer to the line of data. Will be updated
455 * to point past the string token iff the return value is
456 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
459 * @len_p: @len_p initially stores the length of the line of data, which may
460 * be 0, and it will be updated to the number of bytes remaining in
461 * the line iff the return value is PARSE_STRING_SUCCESS.
463 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
464 * parsed string token will be returned here.
466 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
467 * PARSE_STRING_FAILURE if the data was invalid due to a missing
468 * closing quote; or PARSE_STRING_NONE if the line ended before the
469 * beginning of a string token was found.
472 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
475 tchar *line = *line_p;
479 /* Skip leading whitespace */
482 return PARSE_STRING_NONE;
483 if (!istspace(*line) && *line != T('\0'))
489 if (quote_char == T('"') || quote_char == T('\'')) {
494 line = tmemchr(line, quote_char, len);
496 imagex_error(T("Missing closing quote: %"TS), fn - 1);
497 return PARSE_STRING_FAILURE;
500 /* Unquoted string. Go until whitespace. Line is terminated
501 * by '\0', so no need to check 'len'. */
505 } while (!istspace(*line) && *line != T('\0'));
512 return PARSE_STRING_SUCCESS;
515 /* Parses a line of data (not an empty line or comment) in the source list file
516 * format. (See the man page for 'wimlib-imagex capture' for details on this
517 * format and the meaning.)
519 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
520 * len == 0. The data in @line will be modified by this function call.
522 * @len: Length of the line of data.
524 * @source: On success, the capture source and target described by the line is
525 * written into this destination. Note that it will contain pointers
526 * to data in the @line array.
528 * Returns true if the line was valid; false otherwise. */
530 parse_source_list_line(tchar *line, size_t len,
531 struct wimlib_capture_source *source)
535 ret = parse_string(&line, &len, &source->fs_source_path);
536 if (ret != PARSE_STRING_SUCCESS)
538 ret = parse_string(&line, &len, &source->wim_target_path);
539 if (ret == PARSE_STRING_NONE)
540 source->wim_target_path = source->fs_source_path;
541 return ret != PARSE_STRING_FAILURE;
544 /* Returns %true if the given line of length @len > 0 is a comment or empty line
545 * in the source list file format. */
547 is_comment_line(const tchar *line, size_t len)
552 if (!istspace(*line) && *line != T('\0'))
562 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
565 tchar *contents = *contents_p;
566 size_t nchars = *nchars_p;
569 for (i = 0; i < nchars; i++)
570 if (contents[i] == T('\n'))
573 /* Handle last line not terminated by a newline */
574 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
575 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
577 imagex_error(T("Out of memory!"));
580 contents[nchars] = T('\n');
581 *contents_p = contents;
589 /* Parses a file in the source list format. (See the man page for
590 * 'wimlib-imagex capture' for details on this format and the meaning.)
592 * @source_list_contents: Contents of the source list file. Note that this
593 * buffer will be modified to save memory allocations,
594 * and cannot be freed until the returned array of
595 * wimlib_capture_source's has also been freed.
597 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
600 * @nsources_ret: On success, the length of the returned array is
603 * Returns: An array of `struct wimlib_capture_source's that can be passed to
604 * the wimlib_add_image_multisource() function to specify how a WIM image is to
606 static struct wimlib_capture_source *
607 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
608 size_t *nsources_ret)
612 struct wimlib_capture_source *sources;
615 nlines = text_file_count_lines(source_list_contents_p,
616 &source_list_nchars);
620 /* Always allocate at least 1 slot, just in case the implementation of
621 * calloc() returns NULL if 0 bytes are requested. */
622 sources = calloc(nlines ?: 1, sizeof(*sources));
624 imagex_error(T("out of memory"));
627 p = *source_list_contents_p;
629 for (i = 0; i < nlines; i++) {
630 /* XXX: Could use rawmemchr() here instead, but it may not be
631 * available on all platforms. */
632 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
633 size_t len = endp - p + 1;
635 if (!is_comment_line(p, len)) {
636 if (!parse_source_list_line(p, len, &sources[j++])) {
649 enum capture_config_section {
650 CAPTURE_CONFIG_NO_SECTION,
651 CAPTURE_CONFIG_EXCLUSION_SECTION,
652 CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
653 CAPTURE_CONFIG_IGNORE_SECTION,
657 CAPTURE_CONFIG_INVALID_SECTION,
658 CAPTURE_CONFIG_CHANGED_SECTION,
659 CAPTURE_CONFIG_SAME_SECTION,
663 check_config_section(tchar *line, size_t len,
664 enum capture_config_section *cur_section)
666 while (istspace(*line))
670 return CAPTURE_CONFIG_SAME_SECTION;
673 tchar *endbrace = tstrrchr(line, T(']'));
675 return CAPTURE_CONFIG_SAME_SECTION;
677 if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
678 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
679 } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
680 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
681 } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
682 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
683 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
684 "of capture config file\n"),
686 } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
687 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
688 tfputs(T("WARNING: Ignoring [AlignmentList] section "
689 "of capture config file\n"),
692 imagex_error(T("Invalid capture config file section \"%"TS"\""),
694 return CAPTURE_CONFIG_INVALID_SECTION;
696 return CAPTURE_CONFIG_CHANGED_SECTION;
701 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
704 if (pat_list->num_pats == pat_list->num_allocated_pats) {
706 size_t num_allocated_pats = pat_list->num_pats + 8;
708 pats = realloc(pat_list->pats,
709 num_allocated_pats * sizeof(pat_list->pats[0]));
711 imagex_error(T("Out of memory!"));
714 pat_list->pats = pats;
715 pat_list->num_allocated_pats = num_allocated_pats;
717 pat_list->pats[pat_list->num_pats++] = pat;
722 parse_capture_config_line(tchar *line, size_t len,
723 enum capture_config_section *cur_section,
724 struct wimlib_capture_config *config)
729 ret = check_config_section(line, len, cur_section);
730 if (ret == CAPTURE_CONFIG_INVALID_SECTION)
732 if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
735 switch (*cur_section) {
736 case CAPTURE_CONFIG_NO_SECTION:
737 imagex_error(T("Line \"%"TS"\" is not in a section "
738 "(such as [ExclusionList]"), line);
740 case CAPTURE_CONFIG_EXCLUSION_SECTION:
741 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
743 return pattern_list_add_pattern(&config->exclusion_pats,
745 case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
746 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
748 return pattern_list_add_pattern(&config->exclusion_exception_pats,
750 case CAPTURE_CONFIG_IGNORE_SECTION:
757 parse_capture_config(tchar **contents_p, size_t nchars,
758 struct wimlib_capture_config *config)
763 enum capture_config_section cur_section;
765 memset(config, 0, sizeof(*config));
767 nlines = text_file_count_lines(contents_p, &nchars);
771 cur_section = CAPTURE_CONFIG_NO_SECTION;
773 for (i = 0; i < nlines; i++) {
774 tchar *endp = tmemchr(p, T('\n'), nchars);
775 size_t len = endp - p + 1;
777 if (!is_comment_line(p, len))
778 if (!parse_capture_config_line(p, len, &cur_section, config))
786 /* Reads the contents of a file into memory. */
788 file_get_contents(const tchar *filename, size_t *len_ret)
795 if (tstat(filename, &stbuf) != 0) {
796 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
801 fp = tfopen(filename, T("rb"));
803 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
807 buf = malloc(len ? len : 1);
809 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
810 "contents of file \"%"TS"\""), len, filename);
813 if (fread(buf, 1, len, fp) != len) {
814 imagex_error_with_errno(T("Failed to read %zu bytes from the "
815 "file \"%"TS"\""), len, filename);
829 /* Read standard input until EOF and return the full contents in a malloc()ed
830 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
833 stdin_get_contents(size_t *len_ret)
835 /* stdin can, of course, be a pipe or other non-seekable file, so the
836 * total length of the data cannot be pre-determined */
838 size_t newlen = 1024;
842 char *p = realloc(buf, newlen);
843 size_t bytes_read, bytes_to_read;
845 imagex_error(T("out of memory while reading stdin"));
849 bytes_to_read = newlen - pos;
850 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
852 if (bytes_read != bytes_to_read) {
857 imagex_error_with_errno(T("error reading stdin"));
871 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
874 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
876 *num_tchars_ret = num_bytes;
878 #else /* !__WIN32__ */
879 /* On Windows, translate the text to UTF-16LE */
883 if (num_bytes >= 2 &&
884 ((text[0] == 0xff && text[1] == 0xfe) ||
885 (text[0] <= 0x7f && text[1] == 0x00)))
887 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
888 * with something that looks like an ASCII character encoded as
889 * a UTF-16LE code unit. Assume the file is encoded as
890 * UTF-16LE. This is not a 100% reliable check. */
891 num_wchars = num_bytes / 2;
892 text_wstr = (wchar_t*)text;
894 /* File does not look like UTF-16LE. Assume it is encoded in
895 * the current Windows code page. I think these are always
896 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
897 * should work as expected. */
898 text_wstr = win32_mbs_to_wcs(text,
903 *num_tchars_ret = num_wchars;
905 #endif /* __WIN32__ */
909 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
914 contents = file_get_contents(filename, &num_bytes);
917 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
921 stdin_get_text_contents(size_t *num_tchars_ret)
926 contents = stdin_get_contents(&num_bytes);
929 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
932 #define TO_PERCENT(numerator, denominator) \
933 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
935 /* Given an enumerated value for WIM compression type, return a descriptive
938 get_data_type(int ctype)
941 case WIMLIB_COMPRESSION_TYPE_NONE:
942 return T("uncompressed");
943 case WIMLIB_COMPRESSION_TYPE_LZX:
944 return T("LZX-compressed");
945 case WIMLIB_COMPRESSION_TYPE_XPRESS:
946 return T("XPRESS-compressed");
951 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
952 #define MEBIBYTE_MIN_NBYTES 10000000ULL
953 #define KIBIBYTE_MIN_NBYTES 10000ULL
956 get_unit(uint64_t total_bytes, const tchar **name_ret)
958 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
959 *name_ret = T("GiB");
961 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
962 *name_ret = T("MiB");
964 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
965 *name_ret = T("KiB");
968 *name_ret = T("bytes");
973 /* Progress callback function passed to various wimlib functions. */
975 imagex_progress_func(enum wimlib_progress_msg msg,
976 const union wimlib_progress_info *info)
978 unsigned percent_done;
980 const tchar *unit_name;
984 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
985 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
986 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
987 info->write_streams.total_bytes);
988 if (info->write_streams.completed_streams == 0) {
989 const tchar *data_type;
991 data_type = get_data_type(info->write_streams.compression_type);
992 imagex_printf(T("Writing %"TS" data using %u thread%"TS"\n"),
993 data_type, info->write_streams.num_threads,
994 (info->write_streams.num_threads == 1) ? T("") : T("s"));
996 imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
997 "written (%u%% done)"),
998 info->write_streams.completed_bytes >> unit_shift,
1000 info->write_streams.total_bytes >> unit_shift,
1003 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1004 imagex_printf(T("\n"));
1006 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1007 imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
1008 if (*info->scan.wim_target_path) {
1009 imagex_printf(T(" (loading as WIM path: "
1010 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
1011 info->scan.wim_target_path);
1013 imagex_printf(T(" (loading as root of WIM image)...\n"));
1016 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1017 if (info->scan.excluded)
1018 imagex_printf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1020 imagex_printf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
1022 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
1024 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1025 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1026 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1027 info->integrity.total_bytes);
1028 imagex_printf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1029 "of %"PRIu64" %"TS" (%u%%) done"),
1030 info->integrity.filename,
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_CALC_INTEGRITY:
1040 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1041 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1042 info->integrity.total_bytes);
1043 imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1044 "of %"PRIu64" %"TS" (%u%%) done"),
1045 info->integrity.completed_bytes >> unit_shift,
1047 info->integrity.total_bytes >> unit_shift,
1050 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1051 imagex_printf(T("\n"));
1053 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1054 imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1055 "to %"TS" \"%"TS"\"\n"),
1056 info->extract.image,
1057 info->extract.image_name,
1058 info->extract.wimfile_name,
1059 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1060 T("NTFS volume") : T("directory")),
1061 info->extract.target);
1063 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1064 imagex_printf(T("Extracting "
1065 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\" from image %d (\"%"TS"\") "
1066 "in \"%"TS"\" to \"%"TS"\"\n"),
1067 info->extract.extract_root_wim_source_path,
1068 info->extract.image,
1069 info->extract.image_name,
1070 info->extract.wimfile_name,
1071 info->extract.target);
1073 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
1074 /*imagex_printf(T("Applying directory structure to %"TS"\n"),*/
1075 /*info->extract.target);*/
1077 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1078 percent_done = TO_PERCENT(info->extract.completed_bytes,
1079 info->extract.total_bytes);
1080 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1081 imagex_printf(T("\rExtracting files: "
1082 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1083 info->extract.completed_bytes >> unit_shift,
1085 info->extract.total_bytes >> unit_shift,
1088 if (info->extract.completed_bytes >= info->extract.total_bytes)
1089 imagex_printf(T("\n"));
1091 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1092 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1093 imagex_printf(T("Setting timestamps on all extracted files...\n"));
1095 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1096 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1097 imagex_printf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1098 info->extract.target);
1101 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
1102 percent_done = TO_PERCENT(info->join.completed_bytes,
1103 info->join.total_bytes);
1104 unit_shift = get_unit(info->join.total_bytes, &unit_name);
1105 imagex_printf(T("Writing resources from part %u of %u: "
1106 "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written\n"),
1107 (info->join.completed_parts == info->join.total_parts) ?
1108 info->join.completed_parts : info->join.completed_parts + 1,
1109 info->join.total_parts,
1110 info->join.completed_bytes >> unit_shift,
1112 info->join.total_bytes >> unit_shift,
1116 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1117 percent_done = TO_PERCENT(info->split.completed_bytes,
1118 info->split.total_bytes);
1119 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1120 imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
1121 "%"PRIu64" %"TS" (%u%%) written\n"),
1122 info->split.part_name,
1123 info->split.cur_part_number,
1124 info->split.total_parts,
1125 info->split.completed_bytes >> unit_shift,
1127 info->split.total_bytes >> unit_shift,
1131 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1132 if (info->split.completed_bytes == info->split.total_bytes) {
1133 imagex_printf(T("Finished writing part %u of %u WIM parts\n"),
1134 info->split.cur_part_number,
1135 info->split.total_parts);
1138 case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1139 switch (info->update.command->op) {
1140 case WIMLIB_UPDATE_OP_DELETE:
1141 imagex_printf(T("Deleted WIM path "
1142 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1143 info->update.command->delete.wim_path);
1145 case WIMLIB_UPDATE_OP_RENAME:
1146 imagex_printf(T("Renamed WIM path "
1147 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
1148 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1149 info->update.command->rename.wim_source_path,
1150 info->update.command->rename.wim_target_path);
1152 case WIMLIB_UPDATE_OP_ADD:
1160 fflush(imagex_info_file);
1164 /* Open all the split WIM parts that correspond to a file glob.
1166 * @first_part specifies the first part of the split WIM and it may be either
1167 * included or omitted from the glob. */
1169 open_swms_from_glob(const tchar *swm_glob,
1170 const tchar *first_part,
1172 WIMStruct ***additional_swms_ret,
1173 unsigned *num_additional_swms_ret)
1175 unsigned num_additional_swms = 0;
1176 WIMStruct **additional_swms = NULL;
1180 /* Warning: glob() is replaced in Windows native builds */
1181 ret = tglob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
1183 if (ret == GLOB_NOMATCH) {
1184 imagex_error(T("Found no files for glob \"%"TS"\""),
1187 imagex_error_with_errno(T("Failed to process glob \"%"TS"\""),
1193 num_additional_swms = globbuf.gl_pathc;
1194 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
1195 if (!additional_swms) {
1196 imagex_error(T("Out of memory"));
1200 unsigned offset = 0;
1201 for (unsigned i = 0; i < num_additional_swms; i++) {
1202 if (tstrcmp(globbuf.gl_pathv[i], first_part) == 0) {
1206 ret = wimlib_open_wim(globbuf.gl_pathv[i],
1207 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
1208 &additional_swms[i - offset],
1209 imagex_progress_func);
1211 goto out_close_swms;
1213 *additional_swms_ret = additional_swms;
1214 *num_additional_swms_ret = num_additional_swms - offset;
1218 for (unsigned i = 0; i < num_additional_swms; i++)
1219 wimlib_free(additional_swms[i]);
1220 free(additional_swms);
1229 parse_num_threads(const tchar *optarg)
1232 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1233 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1234 imagex_error(T("Number of threads must be a non-negative integer!"));
1242 * Parse an option passed to an update command.
1244 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1247 * @option: Text string for the option (beginning with --)
1249 * @cmd: `struct wimlib_update_command' that is being constructed for
1252 * Returns true if the option was recognized; false if not.
1255 update_command_add_option(int op, const tchar *option,
1256 struct wimlib_update_command *cmd)
1258 bool recognized = true;
1260 case WIMLIB_UPDATE_OP_ADD:
1261 if (!tstrcmp(option, T("--verbose")))
1262 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1263 else if (!tstrcmp(option, T("--unix-data")))
1264 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1265 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1266 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1267 else if (!tstrcmp(option, T("--strict-acls")))
1268 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1269 else if (!tstrcmp(option, T("--dereference")))
1270 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1274 case WIMLIB_UPDATE_OP_DELETE:
1275 if (!tstrcmp(option, T("--force")))
1276 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1277 else if (!tstrcmp(option, T("--recursive")))
1278 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1289 /* How many nonoption arguments each `imagex update' command expects */
1290 static const unsigned update_command_num_nonoptions[] = {
1291 [WIMLIB_UPDATE_OP_ADD] = 2,
1292 [WIMLIB_UPDATE_OP_DELETE] = 1,
1293 [WIMLIB_UPDATE_OP_RENAME] = 2,
1297 update_command_add_nonoption(int op, const tchar *nonoption,
1298 struct wimlib_update_command *cmd,
1299 unsigned num_nonoptions)
1302 case WIMLIB_UPDATE_OP_ADD:
1303 if (num_nonoptions == 0)
1304 cmd->add.fs_source_path = (tchar*)nonoption;
1306 cmd->add.wim_target_path = (tchar*)nonoption;
1308 case WIMLIB_UPDATE_OP_DELETE:
1309 cmd->delete.wim_path = (tchar*)nonoption;
1311 case WIMLIB_UPDATE_OP_RENAME:
1312 if (num_nonoptions == 0)
1313 cmd->rename.wim_source_path = (tchar*)nonoption;
1315 cmd->rename.wim_target_path = (tchar*)nonoption;
1321 * Parse a command passed on stdin to `imagex update'.
1323 * @line: Text of the command.
1324 * @len: Length of the line, including a null terminator
1327 * @command: A `struct wimlib_update_command' to fill in from the parsed
1330 * @line_number: Line number of the command, for diagnostics.
1332 * Returns true on success; returns false on parse error.
1335 parse_update_command(tchar *line, size_t len,
1336 struct wimlib_update_command *command,
1340 tchar *command_name;
1342 size_t num_nonoptions;
1344 /* Get the command name ("add", "delete", "rename") */
1345 ret = parse_string(&line, &len, &command_name);
1346 if (ret != PARSE_STRING_SUCCESS)
1349 if (!tstrcasecmp(command_name, T("add"))) {
1350 op = WIMLIB_UPDATE_OP_ADD;
1351 } else if (!tstrcasecmp(command_name, T("delete"))) {
1352 op = WIMLIB_UPDATE_OP_DELETE;
1353 } else if (!tstrcasecmp(command_name, T("rename"))) {
1354 op = WIMLIB_UPDATE_OP_RENAME;
1356 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1357 command_name, line_number);
1362 /* Parse additional options and non-options as needed */
1367 ret = parse_string(&line, &len, &next_string);
1368 if (ret == PARSE_STRING_NONE) /* End of line */
1370 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1372 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1374 if (!update_command_add_option(op, next_string, command))
1376 imagex_error(T("Unrecognized option \"%"TS"\" to "
1377 "update command \"%"TS"\" on line %zu"),
1378 next_string, command_name, line_number);
1384 if (num_nonoptions == update_command_num_nonoptions[op])
1386 imagex_error(T("Unexpected argument \"%"TS"\" in "
1387 "update command on line %zu\n"
1388 " (The \"%"TS"\" command only "
1389 "takes %zu nonoption arguments!)\n"),
1390 next_string, line_number,
1391 command_name, num_nonoptions);
1394 update_command_add_nonoption(op, next_string,
1395 command, num_nonoptions);
1400 if (num_nonoptions != update_command_num_nonoptions[op]) {
1401 imagex_error(T("Not enough arguments to update command "
1402 "\"%"TS"\" on line %zu"), command_name, line_number);
1408 static struct wimlib_update_command *
1409 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1410 size_t *num_cmds_ret)
1414 struct wimlib_update_command *cmds;
1417 nlines = text_file_count_lines(cmd_file_contents_p,
1422 /* Always allocate at least 1 slot, just in case the implementation of
1423 * calloc() returns NULL if 0 bytes are requested. */
1424 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1426 imagex_error(T("out of memory"));
1429 p = *cmd_file_contents_p;
1431 for (i = 0; i < nlines; i++) {
1432 /* XXX: Could use rawmemchr() here instead, but it may not be
1433 * available on all platforms. */
1434 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1435 size_t len = endp - p + 1;
1437 if (!is_comment_line(p, len)) {
1438 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1449 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1450 * one image from a WIM file to a NTFS volume. */
1452 imagex_apply(int argc, tchar **argv, int cmd)
1455 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1459 const tchar *wimfile;
1460 const tchar *target;
1461 const tchar *image_num_or_name;
1462 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1464 const tchar *swm_glob = NULL;
1465 WIMStruct **additional_swms;
1466 unsigned num_additional_swms;
1468 for_opt(c, apply_options) {
1470 case IMAGEX_CHECK_OPTION:
1471 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1473 case IMAGEX_HARDLINK_OPTION:
1474 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1476 case IMAGEX_SYMLINK_OPTION:
1477 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1479 case IMAGEX_VERBOSE_OPTION:
1480 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
1482 case IMAGEX_REF_OPTION:
1485 case IMAGEX_UNIX_DATA_OPTION:
1486 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1488 case IMAGEX_NO_ACLS_OPTION:
1489 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1491 case IMAGEX_STRICT_ACLS_OPTION:
1492 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1494 case IMAGEX_NORPFIX_OPTION:
1495 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1497 case IMAGEX_RPFIX_OPTION:
1498 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1500 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1501 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1502 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1510 if (argc != 2 && argc != 3)
1515 if (!tstrcmp(wimfile, T("-"))) {
1516 /* Attempt to apply pipable WIM from standard input. */
1518 imagex_error(T("Imagex index or name must be explicitly "
1519 "specified when applying pipable WIM on "
1520 "standard input."));
1523 image_num_or_name = argv[1];
1526 num_additional_swms = 0;
1527 additional_swms = NULL;
1529 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1530 imagex_progress_func);
1535 /* Image explicitly specified. */
1536 image_num_or_name = argv[1];
1537 image = wimlib_resolve_image(wim, image_num_or_name);
1538 ret = verify_image_exists(image, image_num_or_name, wimfile);
1540 goto out_wimlib_free;
1543 /* No image specified; default to image 1, but only if the WIM
1544 * contains exactly one image. */
1545 struct wimlib_wim_info info;
1547 wimlib_get_wim_info(wim, &info);
1548 if (info.image_count != 1) {
1549 imagex_error(T("\"%"TS"\" contains %d images; "
1550 "Please select one (or all)."),
1551 wimfile, info.image_count);
1560 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1562 &num_additional_swms);
1564 goto out_wimlib_free;
1566 additional_swms = NULL;
1567 num_additional_swms = 0;
1573 /* Interpret a regular file or block device target as a NTFS
1577 if (tstat(target, &stbuf)) {
1578 if (errno != ENOENT) {
1579 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1585 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1586 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1592 win32_acquire_restore_privileges();
1595 ret = wimlib_extract_image(wim, image, target, extract_flags,
1596 additional_swms, num_additional_swms,
1597 imagex_progress_func);
1599 set_fd_to_binary_mode(STDIN_FILENO);
1600 ret = wimlib_extract_image_from_pipe(STDIN_FILENO,
1602 target, extract_flags,
1603 imagex_progress_func);
1606 imagex_printf(T("Done applying WIM image.\n"));
1608 win32_release_restore_privileges();
1611 for (unsigned i = 0; i < num_additional_swms; i++)
1612 wimlib_free(additional_swms[i]);
1613 free(additional_swms);
1620 usage(CMD_APPLY, stderr);
1625 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1626 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1627 * the desired image. 'wimlib-imagex append': add a new image to an existing
1630 imagex_capture_or_append(int argc, tchar **argv, int cmd)
1633 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1634 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
1635 int write_flags = 0;
1636 int compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1637 const tchar *wimfile;
1641 const tchar *flags_element = NULL;
1644 unsigned num_threads = 0;
1649 const tchar *config_file = NULL;
1651 struct wimlib_capture_config *config;
1653 bool source_list = false;
1654 size_t source_list_nchars;
1655 tchar *source_list_contents;
1656 bool capture_sources_malloced;
1657 struct wimlib_capture_source *capture_sources;
1659 bool name_defaulted;
1661 for_opt(c, capture_or_append_options) {
1663 case IMAGEX_BOOT_OPTION:
1664 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1666 case IMAGEX_CHECK_OPTION:
1667 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1668 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1670 case IMAGEX_NOCHECK_OPTION:
1671 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
1673 case IMAGEX_CONFIG_OPTION:
1674 config_file = optarg;
1676 case IMAGEX_COMPRESS_OPTION:
1677 compression_type = get_compression_type(optarg);
1678 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1681 case IMAGEX_FLAGS_OPTION:
1682 flags_element = optarg;
1684 case IMAGEX_DEREFERENCE_OPTION:
1685 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1687 case IMAGEX_VERBOSE_OPTION:
1688 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1690 case IMAGEX_THREADS_OPTION:
1691 num_threads = parse_num_threads(optarg);
1692 if (num_threads == UINT_MAX)
1695 case IMAGEX_REBUILD_OPTION:
1696 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1698 case IMAGEX_UNIX_DATA_OPTION:
1699 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1701 case IMAGEX_SOURCE_LIST_OPTION:
1704 case IMAGEX_NO_ACLS_OPTION:
1705 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1707 case IMAGEX_STRICT_ACLS_OPTION:
1708 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1710 case IMAGEX_RPFIX_OPTION:
1711 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1713 case IMAGEX_NORPFIX_OPTION:
1714 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1716 case IMAGEX_PIPABLE_OPTION:
1717 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1719 case IMAGEX_NOT_PIPABLE_OPTION:
1720 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
1729 if (argc < 2 || argc > 4)
1735 if (!tstrcmp(wimfile, T("-"))) {
1737 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
1738 imagex_error("Can't write a non-pipable WIM to "
1739 "standard output! Specify --pipable\n"
1740 " if you want to create a pipable WIM "
1741 "(but read the docs first).");
1745 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1747 if (cmd == CMD_APPEND) {
1748 imagex_error(T("Using standard output for append does "
1749 "not make sense."));
1752 wim_fd = STDOUT_FILENO;
1754 imagex_info_file = stderr;
1759 name_defaulted = false;
1761 /* Set default name to SOURCE argument, omitting any directory
1762 * prefixes and trailing slashes. This requires making a copy
1763 * of @source. Leave some free characters at the end in case we
1764 * append a number to keep the name unique. */
1765 size_t source_name_len;
1767 source_name_len = tstrlen(source);
1768 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1769 name = tbasename(tstrcpy(source_copy, source));
1770 name_defaulted = true;
1772 /* Image description defaults to NULL if not given. */
1779 /* Set up capture sources in source list mode */
1780 if (source[0] == T('-') && source[1] == T('\0')) {
1781 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1783 source_list_contents = file_get_text_contents(source,
1784 &source_list_nchars);
1786 if (!source_list_contents)
1789 capture_sources = parse_source_list(&source_list_contents,
1792 if (!capture_sources) {
1794 goto out_free_source_list_contents;
1796 capture_sources_malloced = true;
1798 /* Set up capture source in non-source-list mode (could be
1799 * either "normal" mode or "NTFS mode"--- see the man page). */
1800 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1801 capture_sources[0].fs_source_path = source;
1802 capture_sources[0].wim_target_path = NULL;
1803 capture_sources[0].reserved = 0;
1805 capture_sources_malloced = false;
1806 source_list_contents = NULL;
1812 config_str = file_get_text_contents(config_file, &config_len);
1815 goto out_free_capture_sources;
1818 config = alloca(sizeof(*config));
1819 ret = parse_capture_config(&config_str, config_len, config);
1821 goto out_free_config;
1823 config = &default_capture_config;
1826 if (cmd == CMD_APPEND)
1827 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1828 imagex_progress_func);
1830 ret = wimlib_create_new_wim(compression_type, &wim);
1832 goto out_free_config;
1838 if (tstat(source, &stbuf) == 0) {
1839 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1840 imagex_printf(T("Capturing WIM image from NTFS "
1841 "filesystem on \"%"TS"\"\n"), source);
1842 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1845 if (errno != ENOENT) {
1846 imagex_error_with_errno(T("Failed to stat "
1847 "\"%"TS"\""), source);
1849 goto out_wimlib_free;
1855 if (cmd == CMD_APPEND && name_defaulted) {
1856 /* If the user did not specify an image name, and the basename
1857 * of the source already exists as an image name in the WIM
1858 * file, append a suffix to make it unique. */
1859 unsigned long conflict_idx;
1860 tchar *name_end = tstrchr(name, T('\0'));
1861 for (conflict_idx = 1;
1862 wimlib_image_name_in_use(wim, name);
1865 tsprintf(name_end, T(" (%lu)"), conflict_idx);
1869 win32_acquire_capture_privileges();
1872 ret = wimlib_add_image_multisource(wim,
1878 imagex_progress_func);
1880 goto out_release_privs;
1882 if (desc || flags_element) {
1883 /* User provided <DESCRIPTION> or <FLAGS> element. Get the
1884 * index of the image we just added, then use it to call the
1885 * appropriate functions. */
1886 struct wimlib_wim_info info;
1888 wimlib_get_wim_info(wim, &info);
1891 ret = wimlib_set_image_descripton(wim,
1895 goto out_release_privs;
1898 if (flags_element) {
1899 ret = wimlib_set_image_flags(wim, info.image_count,
1902 goto out_release_privs;
1906 /* Write the new WIM or overwrite the existing WIM with the new image
1908 if (cmd == CMD_APPEND) {
1909 ret = wimlib_overwrite(wim, write_flags, num_threads,
1910 imagex_progress_func);
1911 } else if (wimfile) {
1912 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
1913 write_flags, num_threads,
1914 imagex_progress_func);
1916 ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
1917 write_flags, num_threads,
1918 imagex_progress_func);
1922 win32_release_capture_privileges();
1927 if (config != &default_capture_config) {
1928 free(config->exclusion_pats.pats);
1929 free(config->exclusion_exception_pats.pats);
1932 out_free_capture_sources:
1933 if (capture_sources_malloced)
1934 free(capture_sources);
1935 out_free_source_list_contents:
1936 free(source_list_contents);
1947 /* Remove image(s) from a WIM. */
1949 imagex_delete(int argc, tchar **argv, int cmd)
1952 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1953 int write_flags = 0;
1954 const tchar *wimfile;
1955 const tchar *image_num_or_name;
1960 for_opt(c, delete_options) {
1962 case IMAGEX_CHECK_OPTION:
1963 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1964 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1966 case IMAGEX_SOFT_OPTION:
1967 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
1978 imagex_error(T("Must specify a WIM file"));
1980 imagex_error(T("Must specify an image"));
1984 image_num_or_name = argv[1];
1986 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1987 imagex_progress_func);
1991 image = wimlib_resolve_image(wim, image_num_or_name);
1993 ret = verify_image_exists(image, image_num_or_name, wimfile);
1995 goto out_wimlib_free;
1997 ret = wimlib_delete_image(wim, image);
1999 imagex_error(T("Failed to delete image from \"%"TS"\""),
2001 goto out_wimlib_free;
2004 ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
2006 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2007 "deleted"), wimfile);
2015 usage(CMD_DELETE, stderr);
2021 print_full_path(const struct wimlib_dir_entry *wdentry, void *_ignore)
2023 int ret = tprintf(T("%"TS"\n"), wdentry->full_path);
2024 return (ret >= 0) ? 0 : -1;
2027 /* Print the files contained in an image(s) in a WIM file. */
2029 imagex_dir(int argc, tchar **argv, int cmd)
2031 const tchar *wimfile;
2032 WIMStruct *wim = NULL;
2035 const tchar *path = T("");
2038 for_opt(c, dir_options) {
2040 case IMAGEX_PATH_OPTION:
2051 imagex_error(T("Must specify a WIM file"));
2055 imagex_error(T("Too many arguments"));
2060 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &wim,
2061 imagex_progress_func);
2066 image = wimlib_resolve_image(wim, argv[1]);
2067 ret = verify_image_exists(image, argv[1], wimfile);
2069 goto out_wimlib_free;
2071 /* No image specified; default to image 1, but only if the WIM
2072 * contains exactly one image. */
2074 struct wimlib_wim_info info;
2076 wimlib_get_wim_info(wim, &info);
2077 if (info.image_count != 1) {
2078 imagex_error(T("\"%"TS"\" contains %d images; Please "
2079 "select one (or all)."),
2080 wimfile, info.image_count);
2087 ret = wimlib_iterate_dir_tree(wim, image, path,
2088 WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE,
2089 print_full_path, NULL);
2096 usage(CMD_DIR, stderr);
2101 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2104 imagex_export(int argc, tchar **argv, int cmd)
2108 int export_flags = 0;
2109 int write_flags = 0;
2110 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2111 const tchar *src_wimfile;
2112 const tchar *src_image_num_or_name;
2113 const tchar *dest_wimfile;
2115 const tchar *dest_name;
2116 const tchar *dest_desc;
2118 WIMStruct *dest_wim;
2123 const tchar *swm_glob = NULL;
2124 WIMStruct **additional_swms;
2125 unsigned num_additional_swms;
2126 unsigned num_threads = 0;
2128 for_opt(c, export_options) {
2130 case IMAGEX_BOOT_OPTION:
2131 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2133 case IMAGEX_CHECK_OPTION:
2134 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2135 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2137 case IMAGEX_NOCHECK_OPTION:
2138 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2140 case IMAGEX_COMPRESS_OPTION:
2141 compression_type = get_compression_type(optarg);
2142 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2145 case IMAGEX_REF_OPTION:
2148 case IMAGEX_THREADS_OPTION:
2149 num_threads = parse_num_threads(optarg);
2150 if (num_threads == UINT_MAX)
2153 case IMAGEX_REBUILD_OPTION:
2154 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2156 case IMAGEX_PIPABLE_OPTION:
2157 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2159 case IMAGEX_NOT_PIPABLE_OPTION:
2160 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
2168 if (argc < 3 || argc > 5)
2170 src_wimfile = argv[0];
2171 src_image_num_or_name = argv[1];
2172 dest_wimfile = argv[2];
2173 dest_name = (argc >= 4) ? argv[3] : NULL;
2174 dest_desc = (argc >= 5) ? argv[4] : NULL;
2175 ret = wimlib_open_wim(src_wimfile,
2176 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_wim,
2177 imagex_progress_func);
2181 /* Determine if the destination is an existing file or not. If so, we
2182 * try to append the exported image(s) to it; otherwise, we create a new
2183 * WIM containing the exported image(s). Furthermore, determine if we
2184 * need to write a pipable WIM directly to standard output. */
2186 if (tstrcmp(dest_wimfile, T("-")) == 0) {
2188 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2189 imagex_error("Can't write a non-pipable WIM to "
2190 "standard output! Specify --pipable\n"
2191 " if you want to create a pipable WIM "
2192 "(but read the docs first).");
2194 goto out_free_src_wim;
2197 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2199 dest_wimfile = NULL;
2200 dest_wim_fd = STDOUT_FILENO;
2201 imagex_info_file = stderr;
2204 if (dest_wimfile != NULL && tstat(dest_wimfile, &stbuf) == 0) {
2206 /* Destination file exists. */
2208 if (!S_ISREG(stbuf.st_mode)) {
2209 imagex_error(T("\"%"TS"\" is not a regular file"),
2212 goto out_free_src_wim;
2214 ret = wimlib_open_wim(dest_wimfile, open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2215 &dest_wim, imagex_progress_func);
2217 goto out_free_src_wim;
2219 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2220 /* The user specified a compression type, but we're
2221 * exporting to an existing WIM. Make sure the
2222 * specified compression type is the same as the
2223 * compression type of the existing destination WIM. */
2224 struct wimlib_wim_info dest_info;
2226 wimlib_get_wim_info(dest_wim, &dest_info);
2227 if (compression_type != dest_info.compression_type) {
2228 imagex_error(T("Cannot specify a compression type that is "
2229 "not the same as that used in the "
2230 "destination WIM"));
2232 goto out_free_dest_wim;
2238 if (errno != ENOENT) {
2239 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2242 goto out_free_src_wim;
2245 /* dest_wimfile is not an existing file, so create a new WIM. */
2247 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2248 /* The user did not specify a compression type; default
2249 * to that of the source WIM. */
2251 struct wimlib_wim_info src_info;
2253 wimlib_get_wim_info(src_wim, &src_info);
2254 compression_type = src_info.compression_type;
2256 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2258 goto out_free_src_wim;
2261 image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2262 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2264 goto out_free_dest_wim;
2267 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
2269 &num_additional_swms);
2271 goto out_free_dest_wim;
2273 additional_swms = NULL;
2274 num_additional_swms = 0;
2277 ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2278 dest_desc, export_flags, additional_swms,
2279 num_additional_swms, imagex_progress_func);
2284 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2285 imagex_progress_func);
2286 else if (dest_wimfile)
2287 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2288 write_flags, num_threads,
2289 imagex_progress_func);
2291 ret = wimlib_write_to_fd(dest_wim, dest_wim_fd,
2292 WIMLIB_ALL_IMAGES, write_flags,
2293 num_threads, imagex_progress_func);
2295 imagex_error(T("Export failed."));
2297 for (unsigned i = 0; i < num_additional_swms; i++)
2298 wimlib_free(additional_swms[i]);
2299 free(additional_swms);
2301 wimlib_free(dest_wim);
2303 wimlib_free(src_wim);
2308 usage(CMD_EXPORT, stderr);
2315 is_root_wim_path(const tchar *path)
2318 for (p = path; *p; p++)
2319 if (*p != T('\\') && *p != T('/'))
2325 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2326 const tchar *dest_dir)
2328 for (size_t i = 0; i < num_cmds; i++)
2329 if (cmds[i].fs_dest_path != dest_dir)
2330 free(cmds[i].fs_dest_path);
2334 static struct wimlib_extract_command *
2335 prepare_extract_commands(tchar **paths, unsigned num_paths,
2336 int extract_flags, tchar *dest_dir,
2337 size_t *num_cmds_ret)
2339 struct wimlib_extract_command *cmds;
2341 tchar *emptystr = T("");
2343 if (num_paths == 0) {
2347 num_cmds = num_paths;
2348 cmds = calloc(num_cmds, sizeof(cmds[0]));
2350 imagex_error(T("Out of memory!"));
2354 for (size_t i = 0; i < num_cmds; i++) {
2355 cmds[i].extract_flags = extract_flags;
2356 cmds[i].wim_source_path = paths[i];
2357 if (is_root_wim_path(paths[i])) {
2358 cmds[i].fs_dest_path = dest_dir;
2360 size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2361 cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2362 if (!cmds[i].fs_dest_path) {
2363 free_extract_commands(cmds, num_cmds, dest_dir);
2366 tsprintf(cmds[i].fs_dest_path,
2367 T("%"TS""OS_PREFERRED_PATH_SEPARATOR_STRING"%"TS),
2368 dest_dir, tbasename(paths[i]));
2371 *num_cmds_ret = num_cmds;
2375 /* Extract files or directories from a WIM image */
2377 imagex_extract(int argc, tchar **argv, int cmd)
2380 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2384 const tchar *wimfile;
2385 const tchar *image_num_or_name;
2386 tchar *dest_dir = T(".");
2387 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL | WIMLIB_EXTRACT_FLAG_NORPFIX;
2389 const tchar *swm_glob = NULL;
2390 WIMStruct **additional_swms;
2391 unsigned num_additional_swms;
2393 struct wimlib_extract_command *cmds;
2396 for_opt(c, extract_options) {
2398 case IMAGEX_CHECK_OPTION:
2399 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2401 case IMAGEX_VERBOSE_OPTION:
2402 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
2404 case IMAGEX_REF_OPTION:
2407 case IMAGEX_UNIX_DATA_OPTION:
2408 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2410 case IMAGEX_NO_ACLS_OPTION:
2411 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2413 case IMAGEX_STRICT_ACLS_OPTION:
2414 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2416 case IMAGEX_DEST_DIR_OPTION:
2419 case IMAGEX_TO_STDOUT_OPTION:
2420 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2421 imagex_info_file = stderr;
2422 imagex_be_quiet = true;
2424 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2425 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2426 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2439 image_num_or_name = argv[1];
2444 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2449 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2453 image = wimlib_resolve_image(wim, image_num_or_name);
2454 ret = verify_image_exists_and_is_single(image,
2458 goto out_wimlib_free;
2461 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2463 &num_additional_swms);
2465 goto out_wimlib_free;
2467 additional_swms = NULL;
2468 num_additional_swms = 0;
2472 win32_acquire_restore_privileges();
2475 ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
2476 additional_swms, num_additional_swms,
2477 imagex_progress_func);
2479 if (!imagex_be_quiet)
2480 imagex_printf(T("Done extracting files.\n"));
2481 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2482 tfprintf(stderr, T("Note: You can use `%"TS"' to see what "
2483 "files and directories\n"
2484 " are in the WIM image.\n"),
2485 get_cmd_string(CMD_INFO, false));
2488 win32_release_restore_privileges();
2490 for (unsigned i = 0; i < num_additional_swms; i++)
2491 wimlib_free(additional_swms[i]);
2492 free(additional_swms);
2496 free_extract_commands(cmds, num_cmds, dest_dir);
2501 usage(CMD_EXTRACT, stderr);
2507 static void print_byte_field(const uint8_t field[], size_t len)
2510 tprintf(T("%02hhx"), *field++);
2514 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2516 tputs(T("WIM Information:"));
2517 tputs(T("----------------"));
2518 tprintf(T("Path: %"TS"\n"), wimfile);
2519 tprintf(T("GUID: 0x"));
2520 print_byte_field(info->guid, sizeof(info->guid));
2522 tprintf(T("Image Count: %d\n"), info->image_count);
2523 tprintf(T("Compression: %"TS"\n"),
2524 wimlib_get_compression_type_string(info->compression_type));
2525 tprintf(T("Part Number: %d/%d\n"), info->part_number, info->total_parts);
2526 tprintf(T("Boot Index: %d\n"), info->boot_index);
2527 tprintf(T("Size: %"PRIu64" bytes\n"), info->total_bytes);
2528 tprintf(T("Integrity Info: %"TS"\n"),
2529 info->has_integrity_table ? T("yes") : T("no"));
2530 tprintf(T("Relative path junction: %"TS"\n"),
2531 info->has_rpfix ? T("yes") : T("no"));
2532 tprintf(T("Pipable: %"TS"\n"),
2533 info->pipable ? T("yes") : T("no"));
2538 print_resource(const struct wimlib_resource_entry *resource,
2542 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
2543 resource->uncompressed_size);
2545 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
2546 resource->compressed_size);
2548 tprintf(T("Offset = %"PRIu64" bytes\n"),
2552 tprintf(T("Part Number = %u\n"), resource->part_number);
2553 tprintf(T("Reference Count = %u\n"), resource->reference_count);
2555 tprintf(T("Hash = 0x"));
2556 print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2559 tprintf(T("Flags = "));
2560 if (resource->is_compressed)
2561 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED "));
2562 if (resource->is_metadata)
2563 tprintf(T("WIM_RESHDR_FLAG_METADATA "));
2564 if (resource->is_free)
2565 tprintf(T("WIM_RESHDR_FLAG_FREE "));
2566 if (resource->is_spanned)
2567 tprintf(T("WIM_RESHDR_FLAG_SPANNED "));
2574 print_lookup_table(WIMStruct *wim)
2576 wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2579 /* Prints information about a WIM file; also can mark an image as bootable,
2580 * change the name of an image, or change the description of an image. */
2582 imagex_info(int argc, tchar **argv, int cmd)
2587 bool nocheck = false;
2588 bool header = false;
2589 bool lookup_table = false;
2591 bool metadata = false;
2592 bool short_header = true;
2593 const tchar *xml_out_file = NULL;
2594 const tchar *wimfile;
2595 const tchar *image_num_or_name;
2596 const tchar *new_name;
2597 const tchar *new_desc;
2601 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2602 struct wimlib_wim_info info;
2604 for_opt(c, info_options) {
2606 case IMAGEX_BOOT_OPTION:
2609 case IMAGEX_CHECK_OPTION:
2612 case IMAGEX_NOCHECK_OPTION:
2615 case IMAGEX_HEADER_OPTION:
2617 short_header = false;
2619 case IMAGEX_LOOKUP_TABLE_OPTION:
2620 lookup_table = true;
2621 short_header = false;
2623 case IMAGEX_XML_OPTION:
2625 short_header = false;
2627 case IMAGEX_EXTRACT_XML_OPTION:
2628 xml_out_file = optarg;
2629 short_header = false;
2631 case IMAGEX_METADATA_OPTION:
2633 short_header = false;
2642 if (argc < 1 || argc > 4)
2646 image_num_or_name = (argc >= 2) ? argv[1] : T("all");
2647 new_name = (argc >= 3) ? argv[2] : NULL;
2648 new_desc = (argc >= 4) ? argv[3] : NULL;
2650 if (check && nocheck) {
2651 imagex_error(T("Can't specify both --check and --nocheck"));
2656 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2658 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2662 wimlib_get_wim_info(wim, &info);
2664 image = wimlib_resolve_image(wim, image_num_or_name);
2665 ret = WIMLIB_ERR_INVALID_IMAGE;
2666 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
2667 imagex_error(T("The image \"%"TS"\" does not exist in \"%"TS"\""),
2668 image_num_or_name, wimfile);
2670 imagex_error(T("If you would like to set the boot "
2671 "index to 0, specify image \"0\" with "
2672 "the --boot flag."));
2674 goto out_wimlib_free;
2677 if (boot && info.image_count == 0) {
2678 imagex_error(T("--boot is meaningless on a WIM with no images"));
2679 goto out_wimlib_free;
2682 if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
2684 imagex_error(T("Cannot specify the --boot flag "
2685 "without specifying a specific "
2686 "image in a multi-image WIM"));
2687 goto out_wimlib_free;
2690 imagex_error(T("Cannot specify the NEW_NAME "
2691 "without specifying a specific "
2692 "image in a multi-image WIM"));
2693 goto out_wimlib_free;
2697 /* Operations that print information are separated from operations that
2698 * recreate the WIM file. */
2699 if (!new_name && !boot) {
2701 /* Read-only operations */
2703 if (image == WIMLIB_NO_IMAGE) {
2704 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
2705 image_num_or_name, wimfile);
2706 goto out_wimlib_free;
2709 if (image == WIMLIB_ALL_IMAGES && short_header)
2710 print_wim_information(wimfile, &info);
2713 wimlib_print_header(wim);
2716 if (info.total_parts != 1) {
2717 tfprintf(stderr, T("Warning: Only showing the lookup table "
2718 "for part %d of a %d-part WIM.\n"),
2719 info.part_number, info.total_parts);
2721 print_lookup_table(wim);
2725 ret = wimlib_extract_xml_data(wim, stdout);
2727 goto out_wimlib_free;
2733 fp = tfopen(xml_out_file, T("wb"));
2735 imagex_error_with_errno(T("Failed to open the "
2736 "file \"%"TS"\" for "
2740 goto out_wimlib_free;
2742 ret = wimlib_extract_xml_data(wim, fp);
2744 imagex_error(T("Failed to close the file "
2750 goto out_wimlib_free;
2754 wimlib_print_available_images(wim, image);
2757 ret = wimlib_print_metadata(wim, image);
2759 goto out_wimlib_free;
2764 /* Modification operations */
2766 if (image == WIMLIB_ALL_IMAGES)
2769 if (image == WIMLIB_NO_IMAGE && new_name) {
2770 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
2771 "when using image 0"), new_name);
2773 goto out_wimlib_free;
2777 if (image == info.boot_index) {
2778 imagex_printf(T("Image %d is already marked as "
2779 "bootable.\n"), image);
2782 imagex_printf(T("Marking image %d as bootable.\n"),
2784 info.boot_index = image;
2785 ret = wimlib_set_wim_info(wim, &info,
2786 WIMLIB_CHANGE_BOOT_INDEX);
2788 goto out_wimlib_free;
2792 if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
2794 imagex_printf(T("Image %d is already named \"%"TS"\".\n"),
2798 imagex_printf(T("Changing the name of image %d to "
2799 "\"%"TS"\".\n"), image, new_name);
2800 ret = wimlib_set_image_name(wim, image, new_name);
2802 goto out_wimlib_free;
2806 const tchar *old_desc;
2807 old_desc = wimlib_get_image_description(wim, image);
2808 if (old_desc && !tstrcmp(old_desc, new_desc)) {
2809 imagex_printf(T("The description of image %d is already "
2810 "\"%"TS"\".\n"), image, new_desc);
2813 imagex_printf(T("Changing the description of image %d "
2814 "to \"%"TS"\".\n"), image, new_desc);
2815 ret = wimlib_set_image_descripton(wim, image,
2818 goto out_wimlib_free;
2822 /* Only call wimlib_overwrite() if something actually needs to
2824 if (boot || new_name || new_desc ||
2825 (check && !info.has_integrity_table) ||
2826 (nocheck && info.has_integrity_table))
2828 int write_flags = 0;
2831 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2833 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2834 ret = wimlib_overwrite(wim, write_flags, 1,
2835 imagex_progress_func);
2837 imagex_printf(T("The file \"%"TS"\" was not modified "
2838 "because nothing needed to be done.\n"),
2849 usage(CMD_INFO, stderr);
2855 /* Join split WIMs into one part WIM */
2857 imagex_join(int argc, tchar **argv, int cmd)
2860 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2861 int wim_write_flags = 0;
2862 const tchar *output_path;
2865 for_opt(c, join_options) {
2867 case IMAGEX_CHECK_OPTION:
2868 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2869 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2879 imagex_error(T("Must specify one or more split WIM (.swm) "
2883 output_path = argv[0];
2884 ret = wimlib_join((const tchar * const *)++argv,
2889 imagex_progress_func);
2894 usage(CMD_JOIN, stderr);
2899 #if WIM_MOUNTING_SUPPORTED
2901 /* Mounts a WIM image. */
2903 imagex_mount_rw_or_ro(int argc, tchar **argv, int cmd)
2906 int mount_flags = 0;
2907 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2908 const tchar *swm_glob = NULL;
2909 const tchar *staging_dir = NULL;
2910 const tchar *wimfile;
2915 WIMStruct **additional_swms;
2916 unsigned num_additional_swms;
2918 if (cmd == CMD_MOUNTRW) {
2919 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
2920 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2923 for_opt(c, mount_options) {
2925 case IMAGEX_ALLOW_OTHER_OPTION:
2926 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
2928 case IMAGEX_CHECK_OPTION:
2929 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2931 case IMAGEX_DEBUG_OPTION:
2932 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
2934 case IMAGEX_STREAMS_INTERFACE_OPTION:
2935 if (!tstrcasecmp(optarg, T("none")))
2936 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
2937 else if (!tstrcasecmp(optarg, T("xattr")))
2938 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2939 else if (!tstrcasecmp(optarg, T("windows")))
2940 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
2942 imagex_error(T("Unknown stream interface \"%"TS"\""),
2947 case IMAGEX_REF_OPTION:
2950 case IMAGEX_STAGING_DIR_OPTION:
2951 staging_dir = optarg;
2953 case IMAGEX_UNIX_DATA_OPTION:
2954 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
2962 if (argc != 2 && argc != 3)
2967 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2972 /* Image explicitly specified. */
2973 image = wimlib_resolve_image(wim, argv[1]);
2975 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
2979 /* No image specified; default to image 1, but only if the WIM
2980 * contains exactly one image. */
2981 struct wimlib_wim_info info;
2983 wimlib_get_wim_info(wim, &info);
2984 if (info.image_count != 1) {
2985 imagex_error(T("\"%"TS"\" contains %d images; Please "
2986 "select one."), wimfile, info.image_count);
2995 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2997 &num_additional_swms);
3001 additional_swms = NULL;
3002 num_additional_swms = 0;
3005 ret = wimlib_mount_image(wim, image, dir, mount_flags, additional_swms,
3006 num_additional_swms, staging_dir);
3008 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
3010 image, wimfile, dir);
3012 for (unsigned i = 0; i < num_additional_swms; i++)
3013 wimlib_free(additional_swms[i]);
3014 free(additional_swms);
3025 #endif /* WIM_MOUNTING_SUPPORTED */
3027 /* Rebuild a WIM file */
3029 imagex_optimize(int argc, tchar **argv, int cmd)
3032 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3033 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
3036 const tchar *wimfile;
3039 unsigned num_threads = 0;
3041 for_opt(c, optimize_options) {
3043 case IMAGEX_CHECK_OPTION:
3044 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3045 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3047 case IMAGEX_NOCHECK_OPTION:
3048 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3050 case IMAGEX_RECOMPRESS_OPTION:
3051 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3053 case IMAGEX_THREADS_OPTION:
3054 num_threads = parse_num_threads(optarg);
3055 if (num_threads == UINT_MAX)
3058 case IMAGEX_PIPABLE_OPTION:
3059 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
3061 case IMAGEX_NOT_PIPABLE_OPTION:
3062 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
3076 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3080 old_size = file_get_size(wimfile);
3081 tprintf(T("\"%"TS"\" original size: "), wimfile);
3083 tputs(T("Unknown"));
3085 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3087 ret = wimlib_overwrite(wim, write_flags, num_threads,
3088 imagex_progress_func);
3090 imagex_error(T("Optimization of \"%"TS"\" failed."), wimfile);
3091 goto out_wimlib_free;
3094 new_size = file_get_size(wimfile);
3095 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3097 tputs(T("Unknown"));
3099 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3101 tfputs(T("Space saved: "), stdout);
3102 if (new_size != -1 && old_size != -1) {
3103 tprintf(T("%lld KiB\n"),
3104 ((long long)old_size - (long long)new_size) >> 10);
3106 tputs(T("Unknown"));
3115 usage(CMD_OPTIMIZE, stderr);
3121 /* Split a WIM into a spanned set */
3123 imagex_split(int argc, tchar **argv, int cmd)
3127 int write_flags = 0;
3128 unsigned long part_size;
3133 for_opt(c, split_options) {
3135 case IMAGEX_CHECK_OPTION:
3136 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3137 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3149 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3150 if (tmp == argv[2] || *tmp) {
3151 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3152 imagex_error(T("The part size must be an integer or "
3153 "floating-point number of megabytes."));
3156 ret = wimlib_open_wim(argv[0], open_flags, &wim, imagex_progress_func);
3160 ret = wimlib_split(wim, argv[1], part_size, write_flags, imagex_progress_func);
3166 usage(CMD_SPLIT, stderr);
3172 #if WIM_MOUNTING_SUPPORTED
3173 /* Unmounts a mounted WIM image. */
3175 imagex_unmount(int argc, tchar **argv, int cmd)
3178 int unmount_flags = 0;
3181 for_opt(c, unmount_options) {
3183 case IMAGEX_COMMIT_OPTION:
3184 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3186 case IMAGEX_CHECK_OPTION:
3187 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3189 case IMAGEX_REBUILD_OPTION:
3190 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3192 case IMAGEX_LAZY_OPTION:
3193 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3204 ret = wimlib_unmount_image(argv[0], unmount_flags,
3205 imagex_progress_func);
3207 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3212 usage(CMD_UNMOUNT, stderr);
3216 #endif /* WIM_MOUNTING_SUPPORTED */
3219 * Add, delete, or rename files in a WIM image.
3222 imagex_update(int argc, tchar **argv, int cmd)
3224 const tchar *wimfile;
3228 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3229 int write_flags = 0;
3230 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3231 int default_add_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
3232 int default_delete_flags = 0;
3233 unsigned num_threads = 0;
3235 tchar *cmd_file_contents;
3236 size_t cmd_file_nchars;
3237 struct wimlib_update_command *cmds;
3239 tchar *command_str = NULL;
3241 const tchar *config_file = NULL;
3243 struct wimlib_capture_config *config;
3245 for_opt(c, update_options) {
3247 /* Generic or write options */
3248 case IMAGEX_THREADS_OPTION:
3249 num_threads = parse_num_threads(optarg);
3250 if (num_threads == UINT_MAX)
3253 case IMAGEX_CHECK_OPTION:
3254 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3255 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3257 case IMAGEX_REBUILD_OPTION:
3258 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3260 case IMAGEX_COMMAND_OPTION:
3262 imagex_error(T("--command may only be specified "
3263 "one time. Please provide\n"
3264 " the update commands "
3265 "on standard input instead."));
3268 command_str = tstrdup(optarg);
3270 imagex_error(T("Out of memory!"));
3274 /* Default delete options */
3275 case IMAGEX_FORCE_OPTION:
3276 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3278 case IMAGEX_RECURSIVE_OPTION:
3279 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3282 /* Global add option */
3283 case IMAGEX_CONFIG_OPTION:
3284 config_file = optarg;
3287 /* Default add options */
3288 case IMAGEX_VERBOSE_OPTION:
3289 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
3291 case IMAGEX_DEREFERENCE_OPTION:
3292 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
3294 case IMAGEX_UNIX_DATA_OPTION:
3295 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
3297 case IMAGEX_NO_ACLS_OPTION:
3298 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
3300 case IMAGEX_STRICT_ACLS_OPTION:
3301 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
3310 if (argc != 1 && argc != 2)
3314 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3316 goto out_free_command_str;
3319 /* Image explicitly specified. */
3320 image = wimlib_resolve_image(wim, argv[1]);
3321 ret = verify_image_exists_and_is_single(image, argv[1],
3324 goto out_wimlib_free;
3326 /* No image specified; default to image 1, but only if the WIM
3327 * contains exactly one image. */
3328 struct wimlib_wim_info info;
3330 wimlib_get_wim_info(wim, &info);
3331 if (info.image_count != 1) {
3332 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3333 wimfile, info.image_count);
3340 /* Parse capture configuration file if specified */
3344 config_str = file_get_text_contents(config_file, &config_len);
3347 goto out_wimlib_free;
3350 config = alloca(sizeof(*config));
3351 ret = parse_capture_config(&config_str, config_len, config);
3353 goto out_free_config;
3355 config = &default_capture_config;
3358 /* Read update commands from standard input, or the command string if
3361 cmd_file_contents = NULL;
3362 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3365 if (isatty(STDIN_FILENO)) {
3366 tputs(T("Reading update commands from standard input..."));
3367 recommend_man_page(CMD_UPDATE, stdout);
3369 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3370 if (!cmd_file_contents) {
3372 goto out_free_config;
3375 /* Parse the update commands */
3376 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3381 goto out_free_cmd_file_contents;
3384 /* Set default flags and capture config on the update commands */
3385 bool have_add_command = false;
3386 for (size_t i = 0; i < num_cmds; i++) {
3387 switch (cmds[i].op) {
3388 case WIMLIB_UPDATE_OP_ADD:
3389 cmds[i].add.add_flags |= default_add_flags;
3390 cmds[i].add.config = config;
3391 have_add_command = true;
3393 case WIMLIB_UPDATE_OP_DELETE:
3394 cmds[i].delete.delete_flags |= default_delete_flags;
3402 if (have_add_command)
3403 win32_acquire_capture_privileges();
3406 /* Execute the update commands */
3407 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3408 imagex_progress_func);
3410 goto out_release_privs;
3412 /* Overwrite the updated WIM */
3413 ret = wimlib_overwrite(wim, write_flags, num_threads,
3414 imagex_progress_func);
3417 if (have_add_command)
3418 win32_release_capture_privileges();
3421 out_free_cmd_file_contents:
3422 free(cmd_file_contents);
3424 if (config != &default_capture_config) {
3425 free(config->exclusion_pats.pats);
3426 free(config->exclusion_exception_pats.pats);
3431 out_free_command_str:
3436 usage(CMD_UPDATE, stderr);
3439 goto out_free_command_str;
3444 struct imagex_command {
3446 int (*func)(int argc, tchar **argv, int cmd);
3449 static const struct imagex_command imagex_commands[] = {
3450 [CMD_APPEND] = {T("append"), imagex_capture_or_append},
3451 [CMD_APPLY] = {T("apply"), imagex_apply},
3452 [CMD_CAPTURE] = {T("capture"), imagex_capture_or_append},
3453 [CMD_DELETE] = {T("delete"), imagex_delete},
3454 [CMD_DIR ] = {T("dir"), imagex_dir},
3455 [CMD_EXPORT] = {T("export"), imagex_export},
3456 [CMD_EXTRACT] = {T("extract"), imagex_extract},
3457 [CMD_INFO] = {T("info"), imagex_info},
3458 [CMD_JOIN] = {T("join"), imagex_join},
3459 #if WIM_MOUNTING_SUPPORTED
3460 [CMD_MOUNT] = {T("mount"), imagex_mount_rw_or_ro},
3461 [CMD_MOUNTRW] = {T("mountrw"), imagex_mount_rw_or_ro},
3463 [CMD_OPTIMIZE] = {T("optimize"), imagex_optimize},
3464 [CMD_SPLIT] = {T("split"), imagex_split},
3465 #if WIM_MOUNTING_SUPPORTED
3466 [CMD_UNMOUNT] = {T("unmount"), imagex_unmount},
3468 [CMD_UPDATE] = {T("update"), imagex_update},
3471 static const tchar *usage_strings[] = {
3474 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
3475 " [DESCRIPTION] [--boot] [--check] [--nocheck]\n"
3476 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
3477 " [--config=FILE] [--threads=NUM_THREADS] [--rebuild]\n"
3478 " [--unix-data] [--source-list] [--no-acls]\n"
3479 " [--strict-acls] [--rpfix] [--norpfix] [--pipable]\n"
3480 " [--not-pipable]\n"
3484 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
3485 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
3486 " [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n"
3487 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3488 " [--include-invalid-names]\n"
3492 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
3493 " [DESCRIPTION] [--boot] [--check] [--nocheck]\n"
3494 " [--compress=TYPE] [--flags EDITION_ID] [--verbose]\n"
3495 " [--dereference] [--config=FILE]\n"
3496 " [--threads=NUM_THREADS] [--unix-data] [--source-list]\n"
3497 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3498 " [--pipable] [--not-pipable]\n"
3502 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n"
3507 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH]\n"
3511 " %"TS" SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
3512 " DEST_WIMFILE [DEST_IMAGE_NAME] [DEST_IMAGE_DESCRIPTION]\n"
3513 " [--boot] [--check] [--nocheck] [--compress=TYPE]\n"
3514 " [--ref=\"GLOB\"] [--threads=NUM_THREADS] [--rebuild]\n"
3515 " [--pipable] [--not-pipable]\n"
3519 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME) [PATH...]\n"
3520 " [--check] [--ref=\"GLOB\"] [--verbose] [--unix-data]\n"
3521 " [--no-acls] [--strict-acls] [--to-stdout] [--dest-dir=CMD_DIR]\n"
3522 " [--include-invalid-names]\n"
3526 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
3527 " [NEW_DESC] [--boot] [--check] [--nocheck] [--header]\n"
3528 " [--lookup-table] [--xml] [--extract-xml FILE]\n"
3533 " %"TS" [--check] WIMFILE SPLIT_WIM...\n"
3535 #if WIM_MOUNTING_SUPPORTED
3538 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
3539 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
3540 " [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n"
3544 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
3545 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
3546 " [--staging-dir=CMD_DIR] [--unix-data] [--allow-other]\n"
3551 " %"TS" WIMFILE [--check] [--nocheck] [--recompress]\n"
3552 " [--threads=NUM_THREADS] [--pipable] [--not-pipable]\n"
3556 " %"TS" WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n"
3558 #if WIM_MOUNTING_SUPPORTED
3561 " %"TS" DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
3566 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
3567 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
3568 " [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
3572 static const tchar *invocation_name;
3573 static bool using_cmd_from_invocation_name = false;
3575 static const tchar *get_cmd_string(int cmd, bool nospace)
3578 if (using_cmd_from_invocation_name || cmd == CMD_NONE) {
3579 return invocation_name;
3581 const tchar *format;
3582 static tchar buf[50];
3585 format = T("%"TS"-%"TS"");
3587 format = T("%"TS" %"TS"");
3588 tsprintf(buf, format, invocation_name, imagex_commands[cmd].name);
3596 static const tchar *format =
3598 "%"TS" (" PACKAGE ") " PACKAGE_VERSION "\n"
3599 "Copyright (C) 2012, 2013 Eric Biggers\n"
3600 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
3601 "This is free software: you are free to change and redistribute it.\n"
3602 "There is NO WARRANTY, to the extent permitted by law.\n"
3604 "Report bugs to "PACKAGE_BUGREPORT".\n"
3606 tprintf(format, invocation_name);
3611 help_or_version(int argc, tchar **argv, int cmd)
3616 for (i = 1; i < argc; i++) {
3618 if (p[0] == T('-') && p[1] == T('-')) {
3620 if (!tstrcmp(p, T("help"))) {
3621 if (cmd == CMD_NONE)
3626 } else if (!tstrcmp(p, T("version"))) {
3635 print_usage_string(int cmd, FILE *fp)
3637 tfprintf(fp, usage_strings[cmd], get_cmd_string(cmd, false));
3641 recommend_man_page(int cmd, FILE *fp)
3643 const tchar *format_str;
3645 format_str = T("See %"TS".pdf in the doc directory for more details.\n");
3647 format_str = T("Try `man %"TS"' for more details.\n");
3649 tfprintf(fp, format_str, get_cmd_string(cmd, true));
3653 usage(int cmd, FILE *fp)
3655 tfprintf(fp, T("Usage:\n"));
3656 print_usage_string(cmd, fp);
3657 tfprintf(fp, T("\n"));
3658 recommend_man_page(cmd, fp);
3664 tfprintf(fp, T("Usage:\n"));
3665 for (int cmd = 0; cmd < CMD_MAX; cmd++) {
3666 print_usage_string(cmd, fp);
3667 tfprintf(fp, T("\n"));
3669 static const tchar *extra =
3672 " %"TS" --version\n"
3674 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
3677 tfprintf(fp, extra, invocation_name, invocation_name);
3678 recommend_man_page(CMD_NONE, fp);
3681 /* Entry point for wimlib's ImageX implementation. On UNIX the command
3682 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
3683 * something else), while an Windows the command arguments will be UTF-16LE
3684 * encoded 'wchar_t' strings. */
3687 wmain(int argc, wchar_t **argv, wchar_t **envp)
3689 main(int argc, char **argv)
3696 imagex_info_file = stdout;
3697 invocation_name = tbasename(argv[0]);
3700 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
3701 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
3705 setlocale(LC_ALL, "");
3706 codeset = nl_langinfo(CODESET);
3707 if (!strstr(codeset, "UTF-8") &&
3708 !strstr(codeset, "UTF8") &&
3709 !strstr(codeset, "utf-8") &&
3710 !strstr(codeset, "utf8"))
3713 "WARNING: Running %"TS" in a UTF-8 locale is recommended!\n"
3714 " Maybe try: `export LANG=en_US.UTF-8'?\n"
3715 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
3716 " to any value to force wimlib to use UTF-8.\n",
3721 #endif /* !__WIN32__ */
3723 /* Allow being invoked as wimCOMMAND (e.g. wimapply). */
3725 if (!tstrncmp(invocation_name, T("wim"), 3) &&
3726 tstrcmp(invocation_name, T(IMAGEX_PROGNAME))) {
3727 for (int i = 0; i < CMD_MAX; i++) {
3728 if (!tstrcmp(invocation_name + 3,
3729 imagex_commands[i].name))
3731 using_cmd_from_invocation_name = true;
3738 /* Unless already known from the invocation name, search for the
3739 * function to handle the specified subcommand. */
3740 if (cmd == CMD_NONE) {
3742 imagex_error(T("No command specified!\n"));
3746 for (int i = 0; i < CMD_MAX; i++) {
3747 if (!tstrcmp(argv[1], imagex_commands[i].name)) {
3752 if (cmd != CMD_NONE) {
3758 /* Handle --help and --version for all commands. Note that this will
3759 * not return if either of these arguments are present. */
3760 help_or_version(argc, argv, cmd);
3762 if (cmd == CMD_NONE) {
3763 imagex_error(T("Unrecognized command: `%"TS"'\n"), argv[1]);
3768 /* The user may like to see more informative error messages. */
3769 wimlib_set_print_errors(true);
3771 /* Initialize the library. */
3772 ret = wimlib_global_init(init_flags);
3774 goto out_check_status;
3776 /* Call the command handler function. */
3777 ret = imagex_commands[cmd].func(argc, argv, cmd);
3779 /* Check for error writing to standard output, especially since for some
3780 * commands, writing to standard output is part of the program's actual
3781 * behavior and not just for informational purposes. */
3782 if (ferror(stdout) || fclose(stdout)) {
3783 imagex_error_with_errno(T("error writing to standard output"));
3788 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
3789 * outside of the wimlib library code. 0 indicates success. > 0
3790 * indicates a wimlib error code from which an error message can be
3793 imagex_error(T("Exiting with error code %d:\n"
3795 wimlib_get_error_string(ret));
3796 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
3797 imagex_error_with_errno(T("errno"));
3799 /* Make the library free any resources it's holding (not strictly
3800 * necessary because the process is ending anyway). */
3801 wimlib_global_cleanup();