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_DELTA_FROM_OPTION,
129 IMAGEX_DEREFERENCE_OPTION,
130 IMAGEX_DEST_DIR_OPTION,
131 IMAGEX_EXTRACT_XML_OPTION,
134 IMAGEX_HARDLINK_OPTION,
135 IMAGEX_HEADER_OPTION,
136 IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
138 IMAGEX_LOOKUP_TABLE_OPTION,
139 IMAGEX_METADATA_OPTION,
140 IMAGEX_NORPFIX_OPTION,
141 IMAGEX_NOCHECK_OPTION,
142 IMAGEX_NO_ACLS_OPTION,
143 IMAGEX_NOT_PIPABLE_OPTION,
145 IMAGEX_PIPABLE_OPTION,
146 IMAGEX_REBUILD_OPTION,
147 IMAGEX_RECOMPRESS_OPTION,
148 IMAGEX_RECURSIVE_OPTION,
150 IMAGEX_RESUME_OPTION,
153 IMAGEX_SOURCE_LIST_OPTION,
154 IMAGEX_STAGING_DIR_OPTION,
155 IMAGEX_STREAMS_INTERFACE_OPTION,
156 IMAGEX_STRICT_ACLS_OPTION,
157 IMAGEX_SYMLINK_OPTION,
158 IMAGEX_THREADS_OPTION,
159 IMAGEX_TO_STDOUT_OPTION,
160 IMAGEX_UNIX_DATA_OPTION,
161 IMAGEX_VERBOSE_OPTION,
165 static const struct option apply_options[] = {
166 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
167 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
168 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
169 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
170 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
171 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
172 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
173 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
174 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
175 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
176 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
177 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
179 /* --resume is undocumented for now as it needs improvement. */
180 {T("resume"), no_argument, NULL, IMAGEX_RESUME_OPTION},
184 static const struct option capture_or_append_options[] = {
185 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
186 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
187 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
188 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
189 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
190 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
191 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
192 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
193 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
194 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
195 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
196 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
197 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
198 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
199 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
200 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
201 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
202 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
203 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
204 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
205 {T("delta-from"), required_argument, NULL, IMAGEX_DELTA_FROM_OPTION},
209 static const struct option delete_options[] = {
210 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
211 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
215 static const struct option dir_options[] = {
216 {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
220 static const struct option export_options[] = {
221 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
222 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
223 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
224 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
225 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
226 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
227 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
228 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
229 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
230 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
234 static const struct option extract_options[] = {
235 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
236 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
237 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
238 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
239 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
240 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
241 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
242 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
243 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
244 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
248 static const struct option info_options[] = {
249 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
250 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
251 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
252 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
253 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
254 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
255 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
256 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
257 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
261 static const struct option join_options[] = {
262 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
266 static const struct option mount_options[] = {
267 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
268 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
269 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
270 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
271 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
272 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
273 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
277 static const struct option optimize_options[] = {
278 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
279 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
280 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
281 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
282 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
283 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
284 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
288 static const struct option split_options[] = {
289 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
293 static const struct option unmount_options[] = {
294 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
295 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
296 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
297 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
301 static const struct option update_options[] = {
302 /* Careful: some of the options here set the defaults for update
303 * commands, but the flags given to an actual update command (and not to
304 * `imagex update' itself are also handled in
305 * update_command_add_option(). */
306 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
307 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
308 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
309 {T("command"), required_argument, NULL, IMAGEX_COMMAND_OPTION},
311 /* Default delete options */
312 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
313 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
315 /* Global add option */
316 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
318 /* Default add options */
319 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
320 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
321 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
322 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
323 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
324 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
330 # define _format_attribute(type, format_str, args_start) \
331 __attribute__((format(type, format_str, args_start)))
333 # define _format_attribute(type, format_str, args_start)
336 /* Print formatted error message to stderr. */
337 static void _format_attribute(printf, 1, 2)
338 imagex_error(const tchar *format, ...)
341 va_start(va, format);
342 tfputs(T("ERROR: "), stderr);
343 tvfprintf(stderr, format, va);
344 tputc(T('\n'), stderr);
348 /* Print formatted error message to stderr. */
349 static void _format_attribute(printf, 1, 2)
350 imagex_error_with_errno(const tchar *format, ...)
352 int errno_save = errno;
354 va_start(va, format);
355 tfputs(T("ERROR: "), stderr);
356 tvfprintf(stderr, format, va);
357 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
362 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
364 if (image == WIMLIB_NO_IMAGE) {
365 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
366 " Please specify a 1-based image index or "
368 " You may use `%"TS"' to list the images "
369 "contained in a WIM."),
370 image_name, wim_name, get_cmd_string(CMD_INFO, false));
377 verify_image_is_single(int image)
379 if (image == WIMLIB_ALL_IMAGES) {
380 imagex_error(T("Cannot specify all images for this action!"));
387 verify_image_exists_and_is_single(int image, const tchar *image_name,
388 const tchar *wim_name)
391 ret = verify_image_exists(image, image_name, wim_name);
393 ret = verify_image_is_single(image);
397 /* Parse the argument to --compress */
399 get_compression_type(const tchar *optarg)
401 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
402 return WIMLIB_COMPRESSION_TYPE_LZX;
403 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
404 return WIMLIB_COMPRESSION_TYPE_XPRESS;
405 else if (!tstrcasecmp(optarg, T("none")))
406 return WIMLIB_COMPRESSION_TYPE_NONE;
408 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
409 "\"maximum\", \"fast\", or \"none\"."), optarg);
410 return WIMLIB_COMPRESSION_TYPE_INVALID;
414 /* Returns the size of a file given its name, or -1 if the file does not exist
415 * or its size cannot be determined. */
417 file_get_size(const tchar *filename)
420 if (tstat(filename, &st) == 0)
427 PARSE_STRING_SUCCESS = 0,
428 PARSE_STRING_FAILURE = 1,
429 PARSE_STRING_NONE = 2,
433 * Parses a string token from an array of characters.
435 * Tokens are either whitespace-delimited, or double or single-quoted.
437 * @line_p: Pointer to the pointer to the line of data. Will be updated
438 * to point past the string token iff the return value is
439 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
442 * @len_p: @len_p initially stores the length of the line of data, which may
443 * be 0, and it will be updated to the number of bytes remaining in
444 * the line iff the return value is PARSE_STRING_SUCCESS.
446 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
447 * parsed string token will be returned here.
449 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
450 * PARSE_STRING_FAILURE if the data was invalid due to a missing
451 * closing quote; or PARSE_STRING_NONE if the line ended before the
452 * beginning of a string token was found.
455 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
458 tchar *line = *line_p;
462 /* Skip leading whitespace */
465 return PARSE_STRING_NONE;
466 if (!istspace(*line) && *line != T('\0'))
472 if (quote_char == T('"') || quote_char == T('\'')) {
477 line = tmemchr(line, quote_char, len);
479 imagex_error(T("Missing closing quote: %"TS), fn - 1);
480 return PARSE_STRING_FAILURE;
483 /* Unquoted string. Go until whitespace. Line is terminated
484 * by '\0', so no need to check 'len'. */
488 } while (!istspace(*line) && *line != T('\0'));
495 return PARSE_STRING_SUCCESS;
498 /* Parses a line of data (not an empty line or comment) in the source list file
499 * format. (See the man page for 'wimlib-imagex capture' for details on this
500 * format and the meaning.)
502 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
503 * len == 0. The data in @line will be modified by this function call.
505 * @len: Length of the line of data.
507 * @source: On success, the capture source and target described by the line is
508 * written into this destination. Note that it will contain pointers
509 * to data in the @line array.
511 * Returns true if the line was valid; false otherwise. */
513 parse_source_list_line(tchar *line, size_t len,
514 struct wimlib_capture_source *source)
518 ret = parse_string(&line, &len, &source->fs_source_path);
519 if (ret != PARSE_STRING_SUCCESS)
521 ret = parse_string(&line, &len, &source->wim_target_path);
522 if (ret == PARSE_STRING_NONE)
523 source->wim_target_path = source->fs_source_path;
524 return ret != PARSE_STRING_FAILURE;
527 /* Returns %true if the given line of length @len > 0 is a comment or empty line
528 * in the source list file format. */
530 is_comment_line(const tchar *line, size_t len)
535 if (!istspace(*line) && *line != T('\0'))
545 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
548 tchar *contents = *contents_p;
549 size_t nchars = *nchars_p;
552 for (i = 0; i < nchars; i++)
553 if (contents[i] == T('\n'))
556 /* Handle last line not terminated by a newline */
557 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
558 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
560 imagex_error(T("Out of memory!"));
563 contents[nchars] = T('\n');
564 *contents_p = contents;
572 /* Parses a file in the source list format. (See the man page for
573 * 'wimlib-imagex capture' for details on this format and the meaning.)
575 * @source_list_contents: Contents of the source list file. Note that this
576 * buffer will be modified to save memory allocations,
577 * and cannot be freed until the returned array of
578 * wimlib_capture_source's has also been freed.
580 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
583 * @nsources_ret: On success, the length of the returned array is
586 * Returns: An array of `struct wimlib_capture_source's that can be passed to
587 * the wimlib_add_image_multisource() function to specify how a WIM image is to
589 static struct wimlib_capture_source *
590 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
591 size_t *nsources_ret)
595 struct wimlib_capture_source *sources;
598 nlines = text_file_count_lines(source_list_contents_p,
599 &source_list_nchars);
603 /* Always allocate at least 1 slot, just in case the implementation of
604 * calloc() returns NULL if 0 bytes are requested. */
605 sources = calloc(nlines ?: 1, sizeof(*sources));
607 imagex_error(T("out of memory"));
610 p = *source_list_contents_p;
612 for (i = 0; i < nlines; i++) {
613 /* XXX: Could use rawmemchr() here instead, but it may not be
614 * available on all platforms. */
615 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
616 size_t len = endp - p + 1;
618 if (!is_comment_line(p, len)) {
619 if (!parse_source_list_line(p, len, &sources[j++])) {
632 enum capture_config_section {
633 CAPTURE_CONFIG_NO_SECTION,
634 CAPTURE_CONFIG_EXCLUSION_SECTION,
635 CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
636 CAPTURE_CONFIG_IGNORE_SECTION,
640 CAPTURE_CONFIG_INVALID_SECTION,
641 CAPTURE_CONFIG_CHANGED_SECTION,
642 CAPTURE_CONFIG_SAME_SECTION,
646 check_config_section(tchar *line, size_t len,
647 enum capture_config_section *cur_section)
649 while (istspace(*line))
653 return CAPTURE_CONFIG_SAME_SECTION;
656 tchar *endbrace = tstrrchr(line, T(']'));
658 return CAPTURE_CONFIG_SAME_SECTION;
660 if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
661 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
662 } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
663 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
664 } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
665 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
666 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
667 "of capture config file\n"),
669 } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
670 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
671 tfputs(T("WARNING: Ignoring [AlignmentList] section "
672 "of capture config file\n"),
675 imagex_error(T("Invalid capture config file section \"%"TS"\""),
677 return CAPTURE_CONFIG_INVALID_SECTION;
679 return CAPTURE_CONFIG_CHANGED_SECTION;
684 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
687 if (pat_list->num_pats == pat_list->num_allocated_pats) {
689 size_t num_allocated_pats = pat_list->num_pats + 8;
691 pats = realloc(pat_list->pats,
692 num_allocated_pats * sizeof(pat_list->pats[0]));
694 imagex_error(T("Out of memory!"));
697 pat_list->pats = pats;
698 pat_list->num_allocated_pats = num_allocated_pats;
700 pat_list->pats[pat_list->num_pats++] = pat;
705 parse_capture_config_line(tchar *line, size_t len,
706 enum capture_config_section *cur_section,
707 struct wimlib_capture_config *config)
712 ret = check_config_section(line, len, cur_section);
713 if (ret == CAPTURE_CONFIG_INVALID_SECTION)
715 if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
718 switch (*cur_section) {
719 case CAPTURE_CONFIG_NO_SECTION:
720 imagex_error(T("Line \"%"TS"\" is not in a section "
721 "(such as [ExclusionList]"), line);
723 case CAPTURE_CONFIG_EXCLUSION_SECTION:
724 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
726 return pattern_list_add_pattern(&config->exclusion_pats,
728 case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
729 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
731 return pattern_list_add_pattern(&config->exclusion_exception_pats,
733 case CAPTURE_CONFIG_IGNORE_SECTION:
740 parse_capture_config(tchar **contents_p, size_t nchars,
741 struct wimlib_capture_config *config)
746 enum capture_config_section cur_section;
748 memset(config, 0, sizeof(*config));
750 nlines = text_file_count_lines(contents_p, &nchars);
754 cur_section = CAPTURE_CONFIG_NO_SECTION;
756 for (i = 0; i < nlines; i++) {
757 tchar *endp = tmemchr(p, T('\n'), nchars);
758 size_t len = endp - p + 1;
760 if (!is_comment_line(p, len))
761 if (!parse_capture_config_line(p, len, &cur_section, config))
769 /* Reads the contents of a file into memory. */
771 file_get_contents(const tchar *filename, size_t *len_ret)
778 if (tstat(filename, &stbuf) != 0) {
779 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
784 fp = tfopen(filename, T("rb"));
786 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
790 buf = malloc(len ? len : 1);
792 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
793 "contents of file \"%"TS"\""), len, filename);
796 if (fread(buf, 1, len, fp) != len) {
797 imagex_error_with_errno(T("Failed to read %zu bytes from the "
798 "file \"%"TS"\""), len, filename);
812 /* Read standard input until EOF and return the full contents in a malloc()ed
813 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
816 stdin_get_contents(size_t *len_ret)
818 /* stdin can, of course, be a pipe or other non-seekable file, so the
819 * total length of the data cannot be pre-determined */
821 size_t newlen = 1024;
825 char *p = realloc(buf, newlen);
826 size_t bytes_read, bytes_to_read;
828 imagex_error(T("out of memory while reading stdin"));
832 bytes_to_read = newlen - pos;
833 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
835 if (bytes_read != bytes_to_read) {
840 imagex_error_with_errno(T("error reading stdin"));
854 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
857 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
859 *num_tchars_ret = num_bytes;
861 #else /* !__WIN32__ */
862 /* On Windows, translate the text to UTF-16LE */
866 if (num_bytes >= 2 &&
867 ((text[0] == 0xff && text[1] == 0xfe) ||
868 (text[0] <= 0x7f && text[1] == 0x00)))
870 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
871 * with something that looks like an ASCII character encoded as
872 * a UTF-16LE code unit. Assume the file is encoded as
873 * UTF-16LE. This is not a 100% reliable check. */
874 num_wchars = num_bytes / 2;
875 text_wstr = (wchar_t*)text;
877 /* File does not look like UTF-16LE. Assume it is encoded in
878 * the current Windows code page. I think these are always
879 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
880 * should work as expected. */
881 text_wstr = win32_mbs_to_wcs(text,
886 *num_tchars_ret = num_wchars;
888 #endif /* __WIN32__ */
892 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
897 contents = file_get_contents(filename, &num_bytes);
900 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
904 stdin_get_text_contents(size_t *num_tchars_ret)
909 contents = stdin_get_contents(&num_bytes);
912 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
915 #define TO_PERCENT(numerator, denominator) \
916 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
918 /* Given an enumerated value for WIM compression type, return a descriptive
921 get_data_type(int ctype)
924 case WIMLIB_COMPRESSION_TYPE_NONE:
925 return T("uncompressed");
926 case WIMLIB_COMPRESSION_TYPE_LZX:
927 return T("LZX-compressed");
928 case WIMLIB_COMPRESSION_TYPE_XPRESS:
929 return T("XPRESS-compressed");
934 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
935 #define MEBIBYTE_MIN_NBYTES 10000000ULL
936 #define KIBIBYTE_MIN_NBYTES 10000ULL
939 get_unit(uint64_t total_bytes, const tchar **name_ret)
941 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
942 *name_ret = T("GiB");
944 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
945 *name_ret = T("MiB");
947 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
948 *name_ret = T("KiB");
951 *name_ret = T("bytes");
956 /* Progress callback function passed to various wimlib functions. */
958 imagex_progress_func(enum wimlib_progress_msg msg,
959 const union wimlib_progress_info *info)
961 unsigned percent_done;
963 const tchar *unit_name;
967 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
968 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
969 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
970 info->write_streams.total_bytes);
972 if (info->write_streams.completed_streams == 0) {
973 const tchar *data_type;
975 data_type = get_data_type(info->write_streams.compression_type);
976 imagex_printf(T("Writing %"TS" data using %u thread%"TS"\n"),
977 data_type, info->write_streams.num_threads,
978 (info->write_streams.num_threads == 1) ? T("") : T("s"));
980 if (info->write_streams.total_parts <= 1) {
981 imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
982 "written (%u%% done)"),
983 info->write_streams.completed_bytes >> unit_shift,
985 info->write_streams.total_bytes >> unit_shift,
989 imagex_printf(T("\rWriting resources from part %u of %u: "
990 "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written"),
991 (info->write_streams.completed_parts ==
992 info->write_streams.total_parts) ?
993 info->write_streams.completed_parts :
994 info->write_streams.completed_parts + 1,
995 info->write_streams.total_parts,
996 info->write_streams.completed_bytes >> unit_shift,
998 info->write_streams.total_bytes >> unit_shift,
1002 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1003 imagex_printf(T("\n"));
1005 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1006 imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
1007 if (*info->scan.wim_target_path) {
1008 imagex_printf(T(" (loading as WIM path: "
1009 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
1010 info->scan.wim_target_path);
1012 imagex_printf(T(" (loading as root of WIM image)...\n"));
1015 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1016 switch (info->scan.status) {
1017 case WIMLIB_SCAN_DENTRY_OK:
1018 imagex_printf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
1020 case WIMLIB_SCAN_DENTRY_EXCLUDED:
1021 imagex_printf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1023 case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
1024 imagex_printf(T("WARNING: Excluding unsupported file or directory\n"
1025 " \"%"TS"\" from capture\n"), info->scan.cur_path);
1029 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1030 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1031 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1032 info->integrity.total_bytes);
1033 imagex_printf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1034 "of %"PRIu64" %"TS" (%u%%) done"),
1035 info->integrity.filename,
1036 info->integrity.completed_bytes >> unit_shift,
1038 info->integrity.total_bytes >> unit_shift,
1041 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1042 imagex_printf(T("\n"));
1044 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1045 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1046 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1047 info->integrity.total_bytes);
1048 imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1049 "of %"PRIu64" %"TS" (%u%%) done"),
1050 info->integrity.completed_bytes >> unit_shift,
1052 info->integrity.total_bytes >> unit_shift,
1055 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1056 imagex_printf(T("\n"));
1058 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1059 imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1060 "to %"TS" \"%"TS"\"\n"),
1061 info->extract.image,
1062 info->extract.image_name,
1063 info->extract.wimfile_name,
1064 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1065 T("NTFS volume") : T("directory")),
1066 info->extract.target);
1068 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1069 imagex_printf(T("Extracting "
1070 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\" from image %d (\"%"TS"\") "
1071 "in \"%"TS"\" to \"%"TS"\"\n"),
1072 info->extract.extract_root_wim_source_path,
1073 info->extract.image,
1074 info->extract.image_name,
1075 info->extract.wimfile_name,
1076 info->extract.target);
1078 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1079 percent_done = TO_PERCENT(info->extract.completed_bytes,
1080 info->extract.total_bytes);
1081 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1082 imagex_printf(T("\rExtracting files: "
1083 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1084 info->extract.completed_bytes >> unit_shift,
1086 info->extract.total_bytes >> unit_shift,
1089 if (info->extract.completed_bytes >= info->extract.total_bytes)
1090 imagex_printf(T("\n"));
1092 case WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN:
1093 imagex_printf(T("\nReading split pipable WIM part %u of %u\n"),
1094 info->extract.part_number,
1095 info->extract.total_parts);
1097 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1098 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1099 imagex_printf(T("Setting timestamps on all extracted files...\n"));
1101 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1102 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1103 imagex_printf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1104 info->extract.target);
1107 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1108 percent_done = TO_PERCENT(info->split.completed_bytes,
1109 info->split.total_bytes);
1110 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1111 imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
1112 "%"PRIu64" %"TS" (%u%%) written\n"),
1113 info->split.part_name,
1114 info->split.cur_part_number,
1115 info->split.total_parts,
1116 info->split.completed_bytes >> unit_shift,
1118 info->split.total_bytes >> unit_shift,
1122 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1123 if (info->split.completed_bytes == info->split.total_bytes) {
1124 imagex_printf(T("Finished writing split WIM part %u of %u\n"),
1125 info->split.cur_part_number,
1126 info->split.total_parts);
1129 case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1130 switch (info->update.command->op) {
1131 case WIMLIB_UPDATE_OP_DELETE:
1132 imagex_printf(T("Deleted WIM path "
1133 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1134 info->update.command->delete.wim_path);
1136 case WIMLIB_UPDATE_OP_RENAME:
1137 imagex_printf(T("Renamed WIM path "
1138 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
1139 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1140 info->update.command->rename.wim_source_path,
1141 info->update.command->rename.wim_target_path);
1143 case WIMLIB_UPDATE_OP_ADD:
1151 fflush(imagex_info_file);
1155 /* Open all the split WIM parts that correspond to a file glob.
1157 * @first_part specifies the first part of the split WIM and it may be either
1158 * included or omitted from the glob. */
1160 open_swms_from_glob(const tchar *swm_glob,
1161 const tchar *first_part,
1163 WIMStruct ***additional_swms_ret,
1164 unsigned *num_additional_swms_ret)
1166 unsigned num_additional_swms = 0;
1167 WIMStruct **additional_swms = NULL;
1171 /* Warning: glob() is replaced in Windows native builds */
1172 ret = tglob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
1174 if (ret == GLOB_NOMATCH) {
1175 imagex_error(T("Found no files for glob \"%"TS"\""),
1178 imagex_error_with_errno(T("Failed to process glob \"%"TS"\""),
1184 num_additional_swms = globbuf.gl_pathc;
1185 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
1186 if (!additional_swms) {
1187 imagex_error(T("Out of memory"));
1191 unsigned offset = 0;
1192 for (unsigned i = 0; i < num_additional_swms; i++) {
1193 if (tstrcmp(globbuf.gl_pathv[i], first_part) == 0) {
1197 ret = wimlib_open_wim(globbuf.gl_pathv[i],
1198 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
1199 &additional_swms[i - offset],
1200 imagex_progress_func);
1202 goto out_close_swms;
1204 *additional_swms_ret = additional_swms;
1205 *num_additional_swms_ret = num_additional_swms - offset;
1209 for (unsigned i = 0; i < num_additional_swms; i++)
1210 wimlib_free(additional_swms[i]);
1211 free(additional_swms);
1220 parse_num_threads(const tchar *optarg)
1223 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1224 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1225 imagex_error(T("Number of threads must be a non-negative integer!"));
1233 * Parse an option passed to an update command.
1235 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1238 * @option: Text string for the option (beginning with --)
1240 * @cmd: `struct wimlib_update_command' that is being constructed for
1243 * Returns true if the option was recognized; false if not.
1246 update_command_add_option(int op, const tchar *option,
1247 struct wimlib_update_command *cmd)
1249 bool recognized = true;
1251 case WIMLIB_UPDATE_OP_ADD:
1252 if (!tstrcmp(option, T("--verbose")))
1253 cmd->add.add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
1254 else if (!tstrcmp(option, T("--unix-data")))
1255 cmd->add.add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
1256 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1257 cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
1258 else if (!tstrcmp(option, T("--strict-acls")))
1259 cmd->add.add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
1260 else if (!tstrcmp(option, T("--dereference")))
1261 cmd->add.add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
1265 case WIMLIB_UPDATE_OP_DELETE:
1266 if (!tstrcmp(option, T("--force")))
1267 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1268 else if (!tstrcmp(option, T("--recursive")))
1269 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1280 /* How many nonoption arguments each `imagex update' command expects */
1281 static const unsigned update_command_num_nonoptions[] = {
1282 [WIMLIB_UPDATE_OP_ADD] = 2,
1283 [WIMLIB_UPDATE_OP_DELETE] = 1,
1284 [WIMLIB_UPDATE_OP_RENAME] = 2,
1288 update_command_add_nonoption(int op, const tchar *nonoption,
1289 struct wimlib_update_command *cmd,
1290 unsigned num_nonoptions)
1293 case WIMLIB_UPDATE_OP_ADD:
1294 if (num_nonoptions == 0)
1295 cmd->add.fs_source_path = (tchar*)nonoption;
1297 cmd->add.wim_target_path = (tchar*)nonoption;
1299 case WIMLIB_UPDATE_OP_DELETE:
1300 cmd->delete.wim_path = (tchar*)nonoption;
1302 case WIMLIB_UPDATE_OP_RENAME:
1303 if (num_nonoptions == 0)
1304 cmd->rename.wim_source_path = (tchar*)nonoption;
1306 cmd->rename.wim_target_path = (tchar*)nonoption;
1312 * Parse a command passed on stdin to `imagex update'.
1314 * @line: Text of the command.
1315 * @len: Length of the line, including a null terminator
1318 * @command: A `struct wimlib_update_command' to fill in from the parsed
1321 * @line_number: Line number of the command, for diagnostics.
1323 * Returns true on success; returns false on parse error.
1326 parse_update_command(tchar *line, size_t len,
1327 struct wimlib_update_command *command,
1331 tchar *command_name;
1333 size_t num_nonoptions;
1335 /* Get the command name ("add", "delete", "rename") */
1336 ret = parse_string(&line, &len, &command_name);
1337 if (ret != PARSE_STRING_SUCCESS)
1340 if (!tstrcasecmp(command_name, T("add"))) {
1341 op = WIMLIB_UPDATE_OP_ADD;
1342 } else if (!tstrcasecmp(command_name, T("delete"))) {
1343 op = WIMLIB_UPDATE_OP_DELETE;
1344 } else if (!tstrcasecmp(command_name, T("rename"))) {
1345 op = WIMLIB_UPDATE_OP_RENAME;
1347 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1348 command_name, line_number);
1353 /* Parse additional options and non-options as needed */
1358 ret = parse_string(&line, &len, &next_string);
1359 if (ret == PARSE_STRING_NONE) /* End of line */
1361 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1363 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1365 if (!update_command_add_option(op, next_string, command))
1367 imagex_error(T("Unrecognized option \"%"TS"\" to "
1368 "update command \"%"TS"\" on line %zu"),
1369 next_string, command_name, line_number);
1375 if (num_nonoptions == update_command_num_nonoptions[op])
1377 imagex_error(T("Unexpected argument \"%"TS"\" in "
1378 "update command on line %zu\n"
1379 " (The \"%"TS"\" command only "
1380 "takes %zu nonoption arguments!)\n"),
1381 next_string, line_number,
1382 command_name, num_nonoptions);
1385 update_command_add_nonoption(op, next_string,
1386 command, num_nonoptions);
1391 if (num_nonoptions != update_command_num_nonoptions[op]) {
1392 imagex_error(T("Not enough arguments to update command "
1393 "\"%"TS"\" on line %zu"), command_name, line_number);
1399 static struct wimlib_update_command *
1400 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1401 size_t *num_cmds_ret)
1405 struct wimlib_update_command *cmds;
1408 nlines = text_file_count_lines(cmd_file_contents_p,
1413 /* Always allocate at least 1 slot, just in case the implementation of
1414 * calloc() returns NULL if 0 bytes are requested. */
1415 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1417 imagex_error(T("out of memory"));
1420 p = *cmd_file_contents_p;
1422 for (i = 0; i < nlines; i++) {
1423 /* XXX: Could use rawmemchr() here instead, but it may not be
1424 * available on all platforms. */
1425 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1426 size_t len = endp - p + 1;
1428 if (!is_comment_line(p, len)) {
1429 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1440 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1441 * one image from a WIM file to a NTFS volume. */
1443 imagex_apply(int argc, tchar **argv, int cmd)
1446 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1450 const tchar *wimfile;
1451 const tchar *target;
1452 const tchar *image_num_or_name;
1453 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1455 const tchar *swm_glob = NULL;
1456 WIMStruct **additional_swms;
1457 unsigned num_additional_swms;
1459 for_opt(c, apply_options) {
1461 case IMAGEX_CHECK_OPTION:
1462 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1464 case IMAGEX_HARDLINK_OPTION:
1465 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1467 case IMAGEX_SYMLINK_OPTION:
1468 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1470 case IMAGEX_VERBOSE_OPTION:
1471 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
1473 case IMAGEX_REF_OPTION:
1476 case IMAGEX_UNIX_DATA_OPTION:
1477 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1479 case IMAGEX_NO_ACLS_OPTION:
1480 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1482 case IMAGEX_STRICT_ACLS_OPTION:
1483 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1485 case IMAGEX_NORPFIX_OPTION:
1486 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1488 case IMAGEX_RPFIX_OPTION:
1489 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1491 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1492 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1493 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1495 case IMAGEX_RESUME_OPTION:
1496 extract_flags |= WIMLIB_EXTRACT_FLAG_RESUME;
1504 if (argc != 2 && argc != 3)
1509 if (!tstrcmp(wimfile, T("-"))) {
1510 /* Attempt to apply pipable WIM from standard input. */
1512 image_num_or_name = NULL;
1515 image_num_or_name = argv[1];
1519 num_additional_swms = 0;
1520 additional_swms = NULL;
1522 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1523 imagex_progress_func);
1528 /* Image explicitly specified. */
1529 image_num_or_name = argv[1];
1530 image = wimlib_resolve_image(wim, image_num_or_name);
1531 ret = verify_image_exists(image, image_num_or_name, wimfile);
1533 goto out_wimlib_free;
1536 /* No image specified; default to image 1, but only if the WIM
1537 * contains exactly one image. */
1538 struct wimlib_wim_info info;
1540 wimlib_get_wim_info(wim, &info);
1541 if (info.image_count != 1) {
1542 imagex_error(T("\"%"TS"\" contains %d images; "
1543 "Please select one (or all)."),
1544 wimfile, info.image_count);
1553 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1555 &num_additional_swms);
1557 goto out_wimlib_free;
1559 additional_swms = NULL;
1560 num_additional_swms = 0;
1566 /* Interpret a regular file or block device target as a NTFS
1570 if (tstat(target, &stbuf)) {
1571 if (errno != ENOENT) {
1572 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1578 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1579 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1585 ret = wimlib_extract_image(wim, image, target, extract_flags,
1586 additional_swms, num_additional_swms,
1587 imagex_progress_func);
1589 set_fd_to_binary_mode(STDIN_FILENO);
1590 ret = wimlib_extract_image_from_pipe(STDIN_FILENO,
1592 target, extract_flags,
1593 imagex_progress_func);
1596 imagex_printf(T("Done applying WIM image.\n"));
1598 for (unsigned i = 0; i < num_additional_swms; i++)
1599 wimlib_free(additional_swms[i]);
1600 free(additional_swms);
1607 usage(CMD_APPLY, stderr);
1612 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1613 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1614 * the desired image. 'wimlib-imagex append': add a new image to an existing
1617 imagex_capture_or_append(int argc, tchar **argv, int cmd)
1620 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1621 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE |
1622 WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1623 int write_flags = 0;
1624 int compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1625 const tchar *wimfile;
1629 const tchar *flags_element = NULL;
1630 const tchar *template_image_name_or_num = NULL;
1634 unsigned num_threads = 0;
1639 const tchar *config_file = NULL;
1641 struct wimlib_capture_config *config;
1643 bool source_list = false;
1644 size_t source_list_nchars;
1645 tchar *source_list_contents;
1646 bool capture_sources_malloced;
1647 struct wimlib_capture_source *capture_sources;
1649 bool name_defaulted;
1651 for_opt(c, capture_or_append_options) {
1653 case IMAGEX_BOOT_OPTION:
1654 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1656 case IMAGEX_CHECK_OPTION:
1657 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1658 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1660 case IMAGEX_NOCHECK_OPTION:
1661 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
1663 case IMAGEX_CONFIG_OPTION:
1664 config_file = optarg;
1665 add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1667 case IMAGEX_COMPRESS_OPTION:
1668 compression_type = get_compression_type(optarg);
1669 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1672 case IMAGEX_FLAGS_OPTION:
1673 flags_element = optarg;
1675 case IMAGEX_DEREFERENCE_OPTION:
1676 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1678 case IMAGEX_VERBOSE_OPTION:
1679 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1681 case IMAGEX_THREADS_OPTION:
1682 num_threads = parse_num_threads(optarg);
1683 if (num_threads == UINT_MAX)
1686 case IMAGEX_REBUILD_OPTION:
1687 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1689 case IMAGEX_UNIX_DATA_OPTION:
1690 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1692 case IMAGEX_SOURCE_LIST_OPTION:
1695 case IMAGEX_NO_ACLS_OPTION:
1696 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1698 case IMAGEX_STRICT_ACLS_OPTION:
1699 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1701 case IMAGEX_RPFIX_OPTION:
1702 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1704 case IMAGEX_NORPFIX_OPTION:
1705 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1707 case IMAGEX_PIPABLE_OPTION:
1708 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1710 case IMAGEX_NOT_PIPABLE_OPTION:
1711 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
1713 case IMAGEX_DELTA_FROM_OPTION:
1714 if (cmd == CMD_CAPTURE) {
1715 imagex_error(T("--delta-from=IMAGE is only "
1716 "valid for append."));
1719 template_image_name_or_num = optarg;
1728 if (argc < 2 || argc > 4)
1734 if (!tstrcmp(wimfile, T("-"))) {
1735 /* Writing captured WIM to standard output. */
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;
1755 set_fd_to_binary_mode(wim_fd);
1760 name_defaulted = false;
1762 /* Set default name to SOURCE argument, omitting any directory
1763 * prefixes and trailing slashes. This requires making a copy
1764 * of @source. Leave some free characters at the end in case we
1765 * append a number to keep the name unique. */
1766 size_t source_name_len;
1768 source_name_len = tstrlen(source);
1769 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1770 name = tbasename(tstrcpy(source_copy, source));
1771 name_defaulted = true;
1773 /* Image description defaults to NULL if not given. */
1780 /* Set up capture sources in source list mode */
1781 if (source[0] == T('-') && source[1] == T('\0')) {
1782 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1784 source_list_contents = file_get_text_contents(source,
1785 &source_list_nchars);
1787 if (!source_list_contents)
1790 capture_sources = parse_source_list(&source_list_contents,
1793 if (!capture_sources) {
1795 goto out_free_source_list_contents;
1797 capture_sources_malloced = true;
1799 /* Set up capture source in non-source-list mode (could be
1800 * either "normal" mode or "NTFS mode"--- see the man page). */
1801 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1802 capture_sources[0].fs_source_path = source;
1803 capture_sources[0].wim_target_path = NULL;
1804 capture_sources[0].reserved = 0;
1806 capture_sources_malloced = false;
1807 source_list_contents = NULL;
1813 config_str = file_get_text_contents(config_file, &config_len);
1816 goto out_free_capture_sources;
1819 config = alloca(sizeof(*config));
1820 ret = parse_capture_config(&config_str, config_len, config);
1822 goto out_free_config;
1827 if (cmd == CMD_APPEND)
1828 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1829 imagex_progress_func);
1831 ret = wimlib_create_new_wim(compression_type, &wim);
1833 goto out_free_config;
1839 if (tstat(source, &stbuf) == 0) {
1840 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1841 imagex_printf(T("Capturing WIM image from NTFS "
1842 "filesystem on \"%"TS"\"\n"), source);
1843 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1846 if (errno != ENOENT) {
1847 imagex_error_with_errno(T("Failed to stat "
1848 "\"%"TS"\""), source);
1850 goto out_wimlib_free;
1856 if (cmd == CMD_APPEND && name_defaulted) {
1857 /* If the user did not specify an image name, and the basename
1858 * of the source already exists as an image name in the WIM
1859 * file, append a suffix to make it unique. */
1860 unsigned long conflict_idx;
1861 tchar *name_end = tstrchr(name, T('\0'));
1862 for (conflict_idx = 1;
1863 wimlib_image_name_in_use(wim, name);
1866 tsprintf(name_end, T(" (%lu)"), conflict_idx);
1870 if (template_image_name_or_num) {
1871 template_image = wimlib_resolve_image(wim, template_image_name_or_num);
1872 if (template_image_name_or_num[0] == T('-')) {
1875 struct wimlib_wim_info info;
1877 wimlib_get_wim_info(wim, &info);
1878 n = tstrtoul(template_image_name_or_num + 1, &tmp, 10);
1879 if (n >= 1 && n <= info.image_count &&
1881 tmp != template_image_name_or_num + 1)
1883 template_image = info.image_count - (n - 1);
1886 ret = verify_image_exists_and_is_single(template_image,
1887 template_image_name_or_num,
1890 goto out_wimlib_free;
1893 ret = wimlib_add_image_multisource(wim,
1899 imagex_progress_func);
1901 goto out_wimlib_free;
1903 if (desc || flags_element || template_image_name_or_num) {
1904 /* User provided <DESCRIPTION> or <FLAGS> element, or an image
1905 * on which the added one is to be based has been specified with
1906 * --delta-from=IMAGE. Get the index of the image we just
1907 * added, then use it to call the appropriate functions. */
1908 struct wimlib_wim_info info;
1910 wimlib_get_wim_info(wim, &info);
1913 ret = wimlib_set_image_descripton(wim,
1917 goto out_wimlib_free;
1920 if (flags_element) {
1921 ret = wimlib_set_image_flags(wim, info.image_count,
1924 goto out_wimlib_free;
1927 if (template_image_name_or_num) {
1928 ret = wimlib_reference_template_image(wim,
1933 goto out_wimlib_free;
1937 /* Write the new WIM or overwrite the existing WIM with the new image
1939 if (cmd == CMD_APPEND) {
1940 ret = wimlib_overwrite(wim, write_flags, num_threads,
1941 imagex_progress_func);
1942 } else if (wimfile) {
1943 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
1944 write_flags, num_threads,
1945 imagex_progress_func);
1947 ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
1948 write_flags, num_threads,
1949 imagex_progress_func);
1955 free(config->exclusion_pats.pats);
1956 free(config->exclusion_exception_pats.pats);
1959 out_free_capture_sources:
1960 if (capture_sources_malloced)
1961 free(capture_sources);
1962 out_free_source_list_contents:
1963 free(source_list_contents);
1974 /* Remove image(s) from a WIM. */
1976 imagex_delete(int argc, tchar **argv, int cmd)
1979 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1980 int write_flags = 0;
1981 const tchar *wimfile;
1982 const tchar *image_num_or_name;
1987 for_opt(c, delete_options) {
1989 case IMAGEX_CHECK_OPTION:
1990 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1991 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1993 case IMAGEX_SOFT_OPTION:
1994 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
2005 imagex_error(T("Must specify a WIM file"));
2007 imagex_error(T("Must specify an image"));
2011 image_num_or_name = argv[1];
2013 ret = wimlib_open_wim(wimfile, open_flags, &wim,
2014 imagex_progress_func);
2018 image = wimlib_resolve_image(wim, image_num_or_name);
2020 ret = verify_image_exists(image, image_num_or_name, wimfile);
2022 goto out_wimlib_free;
2024 ret = wimlib_delete_image(wim, image);
2026 imagex_error(T("Failed to delete image from \"%"TS"\""),
2028 goto out_wimlib_free;
2031 ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
2033 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2034 "deleted"), wimfile);
2042 usage(CMD_DELETE, stderr);
2048 print_full_path(const struct wimlib_dir_entry *wdentry, void *_ignore)
2050 int ret = tprintf(T("%"TS"\n"), wdentry->full_path);
2051 return (ret >= 0) ? 0 : -1;
2054 /* Print the files contained in an image(s) in a WIM file. */
2056 imagex_dir(int argc, tchar **argv, int cmd)
2058 const tchar *wimfile;
2059 WIMStruct *wim = NULL;
2062 const tchar *path = T("");
2065 for_opt(c, dir_options) {
2067 case IMAGEX_PATH_OPTION:
2078 imagex_error(T("Must specify a WIM file"));
2082 imagex_error(T("Too many arguments"));
2087 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &wim,
2088 imagex_progress_func);
2093 image = wimlib_resolve_image(wim, argv[1]);
2094 ret = verify_image_exists(image, argv[1], wimfile);
2096 goto out_wimlib_free;
2098 /* No image specified; default to image 1, but only if the WIM
2099 * contains exactly one image. */
2101 struct wimlib_wim_info info;
2103 wimlib_get_wim_info(wim, &info);
2104 if (info.image_count != 1) {
2105 imagex_error(T("\"%"TS"\" contains %d images; Please "
2106 "select one (or all)."),
2107 wimfile, info.image_count);
2114 ret = wimlib_iterate_dir_tree(wim, image, path,
2115 WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE,
2116 print_full_path, NULL);
2123 usage(CMD_DIR, stderr);
2128 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2131 imagex_export(int argc, tchar **argv, int cmd)
2135 int export_flags = 0;
2136 int write_flags = 0;
2137 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2138 const tchar *src_wimfile;
2139 const tchar *src_image_num_or_name;
2140 const tchar *dest_wimfile;
2142 const tchar *dest_name;
2143 const tchar *dest_desc;
2145 WIMStruct *dest_wim;
2150 const tchar *swm_glob = NULL;
2151 WIMStruct **additional_swms;
2152 unsigned num_additional_swms;
2153 unsigned num_threads = 0;
2155 for_opt(c, export_options) {
2157 case IMAGEX_BOOT_OPTION:
2158 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2160 case IMAGEX_CHECK_OPTION:
2161 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2162 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2164 case IMAGEX_NOCHECK_OPTION:
2165 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2167 case IMAGEX_COMPRESS_OPTION:
2168 compression_type = get_compression_type(optarg);
2169 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2172 case IMAGEX_REF_OPTION:
2175 case IMAGEX_THREADS_OPTION:
2176 num_threads = parse_num_threads(optarg);
2177 if (num_threads == UINT_MAX)
2180 case IMAGEX_REBUILD_OPTION:
2181 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2183 case IMAGEX_PIPABLE_OPTION:
2184 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2186 case IMAGEX_NOT_PIPABLE_OPTION:
2187 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
2195 if (argc < 3 || argc > 5)
2197 src_wimfile = argv[0];
2198 src_image_num_or_name = argv[1];
2199 dest_wimfile = argv[2];
2200 dest_name = (argc >= 4) ? argv[3] : NULL;
2201 dest_desc = (argc >= 5) ? argv[4] : NULL;
2202 ret = wimlib_open_wim(src_wimfile,
2203 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_wim,
2204 imagex_progress_func);
2208 /* Determine if the destination is an existing file or not. If so, we
2209 * try to append the exported image(s) to it; otherwise, we create a new
2210 * WIM containing the exported image(s). Furthermore, determine if we
2211 * need to write a pipable WIM directly to standard output. */
2213 if (tstrcmp(dest_wimfile, T("-")) == 0) {
2215 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2216 imagex_error("Can't write a non-pipable WIM to "
2217 "standard output! Specify --pipable\n"
2218 " if you want to create a pipable WIM "
2219 "(but read the docs first).");
2221 goto out_free_src_wim;
2224 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2226 dest_wimfile = NULL;
2227 dest_wim_fd = STDOUT_FILENO;
2228 imagex_info_file = stderr;
2229 set_fd_to_binary_mode(dest_wim_fd);
2232 if (dest_wimfile != NULL && tstat(dest_wimfile, &stbuf) == 0) {
2234 /* Destination file exists. */
2236 if (!S_ISREG(stbuf.st_mode)) {
2237 imagex_error(T("\"%"TS"\" is not a regular file"),
2240 goto out_free_src_wim;
2242 ret = wimlib_open_wim(dest_wimfile, open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2243 &dest_wim, imagex_progress_func);
2245 goto out_free_src_wim;
2247 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2248 /* The user specified a compression type, but we're
2249 * exporting to an existing WIM. Make sure the
2250 * specified compression type is the same as the
2251 * compression type of the existing destination WIM. */
2252 struct wimlib_wim_info dest_info;
2254 wimlib_get_wim_info(dest_wim, &dest_info);
2255 if (compression_type != dest_info.compression_type) {
2256 imagex_error(T("Cannot specify a compression type that is "
2257 "not the same as that used in the "
2258 "destination WIM"));
2260 goto out_free_dest_wim;
2266 if (errno != ENOENT) {
2267 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2270 goto out_free_src_wim;
2273 /* dest_wimfile is not an existing file, so create a new WIM. */
2275 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2276 /* The user did not specify a compression type; default
2277 * to that of the source WIM. */
2279 struct wimlib_wim_info src_info;
2281 wimlib_get_wim_info(src_wim, &src_info);
2282 compression_type = src_info.compression_type;
2284 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2286 goto out_free_src_wim;
2289 image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2290 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2292 goto out_free_dest_wim;
2295 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
2297 &num_additional_swms);
2299 goto out_free_dest_wim;
2301 additional_swms = NULL;
2302 num_additional_swms = 0;
2305 ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2306 dest_desc, export_flags, additional_swms,
2307 num_additional_swms, imagex_progress_func);
2312 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2313 imagex_progress_func);
2314 else if (dest_wimfile)
2315 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2316 write_flags, num_threads,
2317 imagex_progress_func);
2319 ret = wimlib_write_to_fd(dest_wim, dest_wim_fd,
2320 WIMLIB_ALL_IMAGES, write_flags,
2321 num_threads, imagex_progress_func);
2323 imagex_error(T("Export failed."));
2325 for (unsigned i = 0; i < num_additional_swms; i++)
2326 wimlib_free(additional_swms[i]);
2327 free(additional_swms);
2329 wimlib_free(dest_wim);
2331 wimlib_free(src_wim);
2336 usage(CMD_EXPORT, stderr);
2343 is_root_wim_path(const tchar *path)
2346 for (p = path; *p; p++)
2347 if (*p != T('\\') && *p != T('/'))
2353 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2354 const tchar *dest_dir)
2356 for (size_t i = 0; i < num_cmds; i++)
2357 if (cmds[i].fs_dest_path != dest_dir)
2358 free(cmds[i].fs_dest_path);
2362 static struct wimlib_extract_command *
2363 prepare_extract_commands(tchar **paths, unsigned num_paths,
2364 int extract_flags, tchar *dest_dir,
2365 size_t *num_cmds_ret)
2367 struct wimlib_extract_command *cmds;
2369 tchar *emptystr = T("");
2371 if (num_paths == 0) {
2375 num_cmds = num_paths;
2376 cmds = calloc(num_cmds, sizeof(cmds[0]));
2378 imagex_error(T("Out of memory!"));
2382 for (size_t i = 0; i < num_cmds; i++) {
2383 cmds[i].extract_flags = extract_flags;
2384 cmds[i].wim_source_path = paths[i];
2385 if (is_root_wim_path(paths[i])) {
2386 cmds[i].fs_dest_path = dest_dir;
2388 size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2389 cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2390 if (!cmds[i].fs_dest_path) {
2391 free_extract_commands(cmds, num_cmds, dest_dir);
2394 tsprintf(cmds[i].fs_dest_path,
2395 T("%"TS""OS_PREFERRED_PATH_SEPARATOR_STRING"%"TS),
2396 dest_dir, tbasename(paths[i]));
2399 *num_cmds_ret = num_cmds;
2403 /* Extract files or directories from a WIM image */
2405 imagex_extract(int argc, tchar **argv, int cmd)
2408 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2412 const tchar *wimfile;
2413 const tchar *image_num_or_name;
2414 tchar *dest_dir = T(".");
2415 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL | WIMLIB_EXTRACT_FLAG_NORPFIX;
2417 const tchar *swm_glob = NULL;
2418 WIMStruct **additional_swms;
2419 unsigned num_additional_swms;
2421 struct wimlib_extract_command *cmds;
2424 for_opt(c, extract_options) {
2426 case IMAGEX_CHECK_OPTION:
2427 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2429 case IMAGEX_VERBOSE_OPTION:
2430 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
2432 case IMAGEX_REF_OPTION:
2435 case IMAGEX_UNIX_DATA_OPTION:
2436 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2438 case IMAGEX_NO_ACLS_OPTION:
2439 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2441 case IMAGEX_STRICT_ACLS_OPTION:
2442 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2444 case IMAGEX_DEST_DIR_OPTION:
2447 case IMAGEX_TO_STDOUT_OPTION:
2448 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2449 imagex_info_file = stderr;
2450 imagex_be_quiet = true;
2452 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2453 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2454 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2467 image_num_or_name = argv[1];
2472 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2477 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2481 image = wimlib_resolve_image(wim, image_num_or_name);
2482 ret = verify_image_exists_and_is_single(image,
2486 goto out_wimlib_free;
2489 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2491 &num_additional_swms);
2493 goto out_wimlib_free;
2495 additional_swms = NULL;
2496 num_additional_swms = 0;
2499 ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
2500 additional_swms, num_additional_swms,
2501 imagex_progress_func);
2503 if (!imagex_be_quiet)
2504 imagex_printf(T("Done extracting files.\n"));
2505 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2506 tfprintf(stderr, T("Note: You can use `%"TS"' to see what "
2507 "files and directories\n"
2508 " are in the WIM image.\n"),
2509 get_cmd_string(CMD_INFO, false));
2511 for (unsigned i = 0; i < num_additional_swms; i++)
2512 wimlib_free(additional_swms[i]);
2513 free(additional_swms);
2517 free_extract_commands(cmds, num_cmds, dest_dir);
2522 usage(CMD_EXTRACT, stderr);
2528 static void print_byte_field(const uint8_t field[], size_t len)
2531 tprintf(T("%02hhx"), *field++);
2535 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2537 tputs(T("WIM Information:"));
2538 tputs(T("----------------"));
2539 tprintf(T("Path: %"TS"\n"), wimfile);
2540 tprintf(T("GUID: 0x"));
2541 print_byte_field(info->guid, sizeof(info->guid));
2543 tprintf(T("Image Count: %d\n"), info->image_count);
2544 tprintf(T("Compression: %"TS"\n"),
2545 wimlib_get_compression_type_string(info->compression_type));
2546 tprintf(T("Part Number: %d/%d\n"), info->part_number, info->total_parts);
2547 tprintf(T("Boot Index: %d\n"), info->boot_index);
2548 tprintf(T("Size: %"PRIu64" bytes\n"), info->total_bytes);
2549 tprintf(T("Integrity Info: %"TS"\n"),
2550 info->has_integrity_table ? T("yes") : T("no"));
2551 tprintf(T("Relative path junction: %"TS"\n"),
2552 info->has_rpfix ? T("yes") : T("no"));
2553 tprintf(T("Pipable: %"TS"\n"),
2554 info->pipable ? T("yes") : T("no"));
2559 print_resource(const struct wimlib_resource_entry *resource,
2563 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
2564 resource->uncompressed_size);
2566 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
2567 resource->compressed_size);
2569 tprintf(T("Offset = %"PRIu64" bytes\n"),
2573 tprintf(T("Part Number = %u\n"), resource->part_number);
2574 tprintf(T("Reference Count = %u\n"), resource->reference_count);
2576 tprintf(T("Hash = 0x"));
2577 print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2580 tprintf(T("Flags = "));
2581 if (resource->is_compressed)
2582 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED "));
2583 if (resource->is_metadata)
2584 tprintf(T("WIM_RESHDR_FLAG_METADATA "));
2585 if (resource->is_free)
2586 tprintf(T("WIM_RESHDR_FLAG_FREE "));
2587 if (resource->is_spanned)
2588 tprintf(T("WIM_RESHDR_FLAG_SPANNED "));
2595 print_lookup_table(WIMStruct *wim)
2597 wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2600 /* Prints information about a WIM file; also can mark an image as bootable,
2601 * change the name of an image, or change the description of an image. */
2603 imagex_info(int argc, tchar **argv, int cmd)
2608 bool nocheck = false;
2609 bool header = false;
2610 bool lookup_table = false;
2612 bool metadata = false;
2613 bool short_header = true;
2614 const tchar *xml_out_file = NULL;
2615 const tchar *wimfile;
2616 const tchar *image_num_or_name;
2617 const tchar *new_name;
2618 const tchar *new_desc;
2622 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2623 struct wimlib_wim_info info;
2625 for_opt(c, info_options) {
2627 case IMAGEX_BOOT_OPTION:
2630 case IMAGEX_CHECK_OPTION:
2633 case IMAGEX_NOCHECK_OPTION:
2636 case IMAGEX_HEADER_OPTION:
2638 short_header = false;
2640 case IMAGEX_LOOKUP_TABLE_OPTION:
2641 lookup_table = true;
2642 short_header = false;
2644 case IMAGEX_XML_OPTION:
2646 short_header = false;
2648 case IMAGEX_EXTRACT_XML_OPTION:
2649 xml_out_file = optarg;
2650 short_header = false;
2652 case IMAGEX_METADATA_OPTION:
2654 short_header = false;
2663 if (argc < 1 || argc > 4)
2667 image_num_or_name = (argc >= 2) ? argv[1] : T("all");
2668 new_name = (argc >= 3) ? argv[2] : NULL;
2669 new_desc = (argc >= 4) ? argv[3] : NULL;
2671 if (check && nocheck) {
2672 imagex_error(T("Can't specify both --check and --nocheck"));
2677 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2679 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2683 wimlib_get_wim_info(wim, &info);
2685 image = wimlib_resolve_image(wim, image_num_or_name);
2686 ret = WIMLIB_ERR_INVALID_IMAGE;
2687 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
2688 imagex_error(T("The image \"%"TS"\" does not exist in \"%"TS"\""),
2689 image_num_or_name, wimfile);
2691 imagex_error(T("If you would like to set the boot "
2692 "index to 0, specify image \"0\" with "
2693 "the --boot flag."));
2695 goto out_wimlib_free;
2698 if (boot && info.image_count == 0) {
2699 imagex_error(T("--boot is meaningless on a WIM with no images"));
2700 goto out_wimlib_free;
2703 if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
2705 imagex_error(T("Cannot specify the --boot flag "
2706 "without specifying a specific "
2707 "image in a multi-image WIM"));
2708 goto out_wimlib_free;
2711 imagex_error(T("Cannot specify the NEW_NAME "
2712 "without specifying a specific "
2713 "image in a multi-image WIM"));
2714 goto out_wimlib_free;
2718 /* Operations that print information are separated from operations that
2719 * recreate the WIM file. */
2720 if (!new_name && !boot) {
2722 /* Read-only operations */
2724 if (image == WIMLIB_NO_IMAGE) {
2725 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
2726 image_num_or_name, wimfile);
2727 goto out_wimlib_free;
2730 if (image == WIMLIB_ALL_IMAGES && short_header)
2731 print_wim_information(wimfile, &info);
2734 wimlib_print_header(wim);
2737 if (info.total_parts != 1) {
2738 tfprintf(stderr, T("Warning: Only showing the lookup table "
2739 "for part %d of a %d-part WIM.\n"),
2740 info.part_number, info.total_parts);
2742 print_lookup_table(wim);
2746 ret = wimlib_extract_xml_data(wim, stdout);
2748 goto out_wimlib_free;
2754 fp = tfopen(xml_out_file, T("wb"));
2756 imagex_error_with_errno(T("Failed to open the "
2757 "file \"%"TS"\" for "
2761 goto out_wimlib_free;
2763 ret = wimlib_extract_xml_data(wim, fp);
2765 imagex_error(T("Failed to close the file "
2771 goto out_wimlib_free;
2775 wimlib_print_available_images(wim, image);
2778 ret = wimlib_print_metadata(wim, image);
2780 goto out_wimlib_free;
2785 /* Modification operations */
2787 if (image == WIMLIB_ALL_IMAGES)
2790 if (image == WIMLIB_NO_IMAGE && new_name) {
2791 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
2792 "when using image 0"), new_name);
2794 goto out_wimlib_free;
2798 if (image == info.boot_index) {
2799 imagex_printf(T("Image %d is already marked as "
2800 "bootable.\n"), image);
2803 imagex_printf(T("Marking image %d as bootable.\n"),
2805 info.boot_index = image;
2806 ret = wimlib_set_wim_info(wim, &info,
2807 WIMLIB_CHANGE_BOOT_INDEX);
2809 goto out_wimlib_free;
2813 if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
2815 imagex_printf(T("Image %d is already named \"%"TS"\".\n"),
2819 imagex_printf(T("Changing the name of image %d to "
2820 "\"%"TS"\".\n"), image, new_name);
2821 ret = wimlib_set_image_name(wim, image, new_name);
2823 goto out_wimlib_free;
2827 const tchar *old_desc;
2828 old_desc = wimlib_get_image_description(wim, image);
2829 if (old_desc && !tstrcmp(old_desc, new_desc)) {
2830 imagex_printf(T("The description of image %d is already "
2831 "\"%"TS"\".\n"), image, new_desc);
2834 imagex_printf(T("Changing the description of image %d "
2835 "to \"%"TS"\".\n"), image, new_desc);
2836 ret = wimlib_set_image_descripton(wim, image,
2839 goto out_wimlib_free;
2843 /* Only call wimlib_overwrite() if something actually needs to
2845 if (boot || new_name || new_desc ||
2846 (check && !info.has_integrity_table) ||
2847 (nocheck && info.has_integrity_table))
2849 int write_flags = 0;
2852 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2854 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2855 ret = wimlib_overwrite(wim, write_flags, 1,
2856 imagex_progress_func);
2858 imagex_printf(T("The file \"%"TS"\" was not modified "
2859 "because nothing needed to be done.\n"),
2870 usage(CMD_INFO, stderr);
2876 /* Join split WIMs into one part WIM */
2878 imagex_join(int argc, tchar **argv, int cmd)
2881 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2882 int wim_write_flags = 0;
2883 const tchar *output_path;
2886 for_opt(c, join_options) {
2888 case IMAGEX_CHECK_OPTION:
2889 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2890 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2900 imagex_error(T("Must specify one or more split WIM (.swm) "
2904 output_path = argv[0];
2905 ret = wimlib_join((const tchar * const *)++argv,
2910 imagex_progress_func);
2915 usage(CMD_JOIN, stderr);
2920 #if WIM_MOUNTING_SUPPORTED
2922 /* Mounts a WIM image. */
2924 imagex_mount_rw_or_ro(int argc, tchar **argv, int cmd)
2927 int mount_flags = 0;
2928 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2929 const tchar *swm_glob = NULL;
2930 const tchar *staging_dir = NULL;
2931 const tchar *wimfile;
2936 WIMStruct **additional_swms;
2937 unsigned num_additional_swms;
2939 if (cmd == CMD_MOUNTRW) {
2940 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
2941 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2944 for_opt(c, mount_options) {
2946 case IMAGEX_ALLOW_OTHER_OPTION:
2947 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
2949 case IMAGEX_CHECK_OPTION:
2950 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2952 case IMAGEX_DEBUG_OPTION:
2953 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
2955 case IMAGEX_STREAMS_INTERFACE_OPTION:
2956 if (!tstrcasecmp(optarg, T("none")))
2957 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
2958 else if (!tstrcasecmp(optarg, T("xattr")))
2959 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2960 else if (!tstrcasecmp(optarg, T("windows")))
2961 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
2963 imagex_error(T("Unknown stream interface \"%"TS"\""),
2968 case IMAGEX_REF_OPTION:
2971 case IMAGEX_STAGING_DIR_OPTION:
2972 staging_dir = optarg;
2974 case IMAGEX_UNIX_DATA_OPTION:
2975 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
2983 if (argc != 2 && argc != 3)
2988 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2993 /* Image explicitly specified. */
2994 image = wimlib_resolve_image(wim, argv[1]);
2996 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
3000 /* No image specified; default to image 1, but only if the WIM
3001 * contains exactly one image. */
3002 struct wimlib_wim_info info;
3004 wimlib_get_wim_info(wim, &info);
3005 if (info.image_count != 1) {
3006 imagex_error(T("\"%"TS"\" contains %d images; Please "
3007 "select one."), wimfile, info.image_count);
3016 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
3018 &num_additional_swms);
3022 additional_swms = NULL;
3023 num_additional_swms = 0;
3026 ret = wimlib_mount_image(wim, image, dir, mount_flags, additional_swms,
3027 num_additional_swms, staging_dir);
3029 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
3031 image, wimfile, dir);
3033 for (unsigned i = 0; i < num_additional_swms; i++)
3034 wimlib_free(additional_swms[i]);
3035 free(additional_swms);
3046 #endif /* WIM_MOUNTING_SUPPORTED */
3048 /* Rebuild a WIM file */
3050 imagex_optimize(int argc, tchar **argv, int cmd)
3053 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3054 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
3057 const tchar *wimfile;
3060 unsigned num_threads = 0;
3062 for_opt(c, optimize_options) {
3064 case IMAGEX_CHECK_OPTION:
3065 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3066 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3068 case IMAGEX_NOCHECK_OPTION:
3069 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3071 case IMAGEX_RECOMPRESS_OPTION:
3072 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3074 case IMAGEX_THREADS_OPTION:
3075 num_threads = parse_num_threads(optarg);
3076 if (num_threads == UINT_MAX)
3079 case IMAGEX_PIPABLE_OPTION:
3080 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
3082 case IMAGEX_NOT_PIPABLE_OPTION:
3083 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
3097 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3101 old_size = file_get_size(wimfile);
3102 tprintf(T("\"%"TS"\" original size: "), wimfile);
3104 tputs(T("Unknown"));
3106 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3108 ret = wimlib_overwrite(wim, write_flags, num_threads,
3109 imagex_progress_func);
3111 imagex_error(T("Optimization of \"%"TS"\" failed."), wimfile);
3112 goto out_wimlib_free;
3115 new_size = file_get_size(wimfile);
3116 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3118 tputs(T("Unknown"));
3120 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3122 tfputs(T("Space saved: "), stdout);
3123 if (new_size != -1 && old_size != -1) {
3124 tprintf(T("%lld KiB\n"),
3125 ((long long)old_size - (long long)new_size) >> 10);
3127 tputs(T("Unknown"));
3136 usage(CMD_OPTIMIZE, stderr);
3142 /* Split a WIM into a spanned set */
3144 imagex_split(int argc, tchar **argv, int cmd)
3148 int write_flags = 0;
3149 unsigned long part_size;
3154 for_opt(c, split_options) {
3156 case IMAGEX_CHECK_OPTION:
3157 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3158 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3170 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3171 if (tmp == argv[2] || *tmp) {
3172 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3173 imagex_error(T("The part size must be an integer or "
3174 "floating-point number of megabytes."));
3177 ret = wimlib_open_wim(argv[0], open_flags, &wim, imagex_progress_func);
3181 ret = wimlib_split(wim, argv[1], part_size, write_flags, imagex_progress_func);
3187 usage(CMD_SPLIT, stderr);
3193 #if WIM_MOUNTING_SUPPORTED
3194 /* Unmounts a mounted WIM image. */
3196 imagex_unmount(int argc, tchar **argv, int cmd)
3199 int unmount_flags = 0;
3202 for_opt(c, unmount_options) {
3204 case IMAGEX_COMMIT_OPTION:
3205 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3207 case IMAGEX_CHECK_OPTION:
3208 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3210 case IMAGEX_REBUILD_OPTION:
3211 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3213 case IMAGEX_LAZY_OPTION:
3214 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3225 ret = wimlib_unmount_image(argv[0], unmount_flags,
3226 imagex_progress_func);
3228 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3233 usage(CMD_UNMOUNT, stderr);
3237 #endif /* WIM_MOUNTING_SUPPORTED */
3240 * Add, delete, or rename files in a WIM image.
3243 imagex_update(int argc, tchar **argv, int cmd)
3245 const tchar *wimfile;
3249 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3250 int write_flags = 0;
3251 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3252 int default_add_flags = WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
3253 WIMLIB_ADD_FLAG_WINCONFIG;
3254 int default_delete_flags = 0;
3255 unsigned num_threads = 0;
3257 tchar *cmd_file_contents;
3258 size_t cmd_file_nchars;
3259 struct wimlib_update_command *cmds;
3261 tchar *command_str = NULL;
3263 const tchar *config_file = NULL;
3265 struct wimlib_capture_config *config;
3267 for_opt(c, update_options) {
3269 /* Generic or write options */
3270 case IMAGEX_THREADS_OPTION:
3271 num_threads = parse_num_threads(optarg);
3272 if (num_threads == UINT_MAX)
3275 case IMAGEX_CHECK_OPTION:
3276 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3277 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3279 case IMAGEX_REBUILD_OPTION:
3280 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3282 case IMAGEX_COMMAND_OPTION:
3284 imagex_error(T("--command may only be specified "
3285 "one time. Please provide\n"
3286 " the update commands "
3287 "on standard input instead."));
3290 command_str = tstrdup(optarg);
3292 imagex_error(T("Out of memory!"));
3296 /* Default delete options */
3297 case IMAGEX_FORCE_OPTION:
3298 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3300 case IMAGEX_RECURSIVE_OPTION:
3301 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3304 /* Global add option */
3305 case IMAGEX_CONFIG_OPTION:
3306 default_add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
3307 config_file = optarg;
3310 /* Default add options */
3311 case IMAGEX_VERBOSE_OPTION:
3312 default_add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
3314 case IMAGEX_DEREFERENCE_OPTION:
3315 default_add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
3317 case IMAGEX_UNIX_DATA_OPTION:
3318 default_add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
3320 case IMAGEX_NO_ACLS_OPTION:
3321 default_add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
3323 case IMAGEX_STRICT_ACLS_OPTION:
3324 default_add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
3333 if (argc != 1 && argc != 2)
3337 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3339 goto out_free_command_str;
3342 /* Image explicitly specified. */
3343 image = wimlib_resolve_image(wim, argv[1]);
3344 ret = verify_image_exists_and_is_single(image, argv[1],
3347 goto out_wimlib_free;
3349 /* No image specified; default to image 1, but only if the WIM
3350 * contains exactly one image. */
3351 struct wimlib_wim_info info;
3353 wimlib_get_wim_info(wim, &info);
3354 if (info.image_count != 1) {
3355 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3356 wimfile, info.image_count);
3363 /* Parse capture configuration file if specified */
3367 config_str = file_get_text_contents(config_file, &config_len);
3370 goto out_wimlib_free;
3373 config = alloca(sizeof(*config));
3374 ret = parse_capture_config(&config_str, config_len, config);
3376 goto out_free_config;
3381 /* Read update commands from standard input, or the command string if
3384 cmd_file_contents = NULL;
3385 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3388 if (isatty(STDIN_FILENO)) {
3389 tputs(T("Reading update commands from standard input..."));
3390 recommend_man_page(CMD_UPDATE, stdout);
3392 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3393 if (!cmd_file_contents) {
3395 goto out_free_config;
3398 /* Parse the update commands */
3399 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3404 goto out_free_cmd_file_contents;
3407 /* Set default flags and capture config on the update commands */
3408 for (size_t i = 0; i < num_cmds; i++) {
3409 switch (cmds[i].op) {
3410 case WIMLIB_UPDATE_OP_ADD:
3411 cmds[i].add.add_flags |= default_add_flags;
3412 cmds[i].add.config = config;
3414 case WIMLIB_UPDATE_OP_DELETE:
3415 cmds[i].delete.delete_flags |= default_delete_flags;
3422 /* Execute the update commands */
3423 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3424 imagex_progress_func);
3428 /* Overwrite the updated WIM */
3429 ret = wimlib_overwrite(wim, write_flags, num_threads,
3430 imagex_progress_func);
3433 out_free_cmd_file_contents:
3434 free(cmd_file_contents);
3437 free(config->exclusion_pats.pats);
3438 free(config->exclusion_exception_pats.pats);
3443 out_free_command_str:
3448 usage(CMD_UPDATE, stderr);
3451 goto out_free_command_str;
3456 struct imagex_command {
3458 int (*func)(int argc, tchar **argv, int cmd);
3461 static const struct imagex_command imagex_commands[] = {
3462 [CMD_APPEND] = {T("append"), imagex_capture_or_append},
3463 [CMD_APPLY] = {T("apply"), imagex_apply},
3464 [CMD_CAPTURE] = {T("capture"), imagex_capture_or_append},
3465 [CMD_DELETE] = {T("delete"), imagex_delete},
3466 [CMD_DIR ] = {T("dir"), imagex_dir},
3467 [CMD_EXPORT] = {T("export"), imagex_export},
3468 [CMD_EXTRACT] = {T("extract"), imagex_extract},
3469 [CMD_INFO] = {T("info"), imagex_info},
3470 [CMD_JOIN] = {T("join"), imagex_join},
3471 #if WIM_MOUNTING_SUPPORTED
3472 [CMD_MOUNT] = {T("mount"), imagex_mount_rw_or_ro},
3473 [CMD_MOUNTRW] = {T("mountrw"), imagex_mount_rw_or_ro},
3475 [CMD_OPTIMIZE] = {T("optimize"), imagex_optimize},
3476 [CMD_SPLIT] = {T("split"), imagex_split},
3477 #if WIM_MOUNTING_SUPPORTED
3478 [CMD_UNMOUNT] = {T("unmount"), imagex_unmount},
3480 [CMD_UPDATE] = {T("update"), imagex_update},
3483 static const tchar *usage_strings[] = {
3486 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3487 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3488 " [--nocheck] [--flags EDITION_ID] [--verbose]\n"
3489 " [--dereference] [--config=FILE] [--threads=NUM_THREADS]\n"
3490 " [--rebuild] [--unix-data] [--source-list] [--no-acls]\n"
3491 " [--strict-acls] [--rpfix] [--norpfix] [--pipable]\n"
3492 " [--not-pipable] [--delta-from=IMAGE]\n"
3496 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME | all)]\n"
3497 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
3498 " [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n"
3499 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3500 " [--include-invalid-names]\n"
3504 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3505 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3506 " [--nocheck] [--compress=TYPE] [--flags EDITION_ID]\n"
3507 " [--verbose] [--dereference] [--config=FILE]\n"
3508 " [--threads=NUM_THREADS] [--unix-data] [--source-list]\n"
3509 " [--no-acls] [--strict-acls] [--norpfix] [--pipable]\n"
3513 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n"
3518 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH]\n"
3522 " %"TS" SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
3523 " DEST_WIMFILE [DEST_IMAGE_NAME [DEST_IMAGE_DESCRIPTION]]\n"
3524 " [--boot] [--check] [--nocheck] [--compress=TYPE]\n"
3525 " [--ref=\"GLOB\"] [--threads=NUM_THREADS] [--rebuild]\n"
3526 " [--pipable] [--not-pipable]\n"
3530 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME) [PATH...]\n"
3531 " [--check] [--ref=\"GLOB\"] [--verbose] [--unix-data]\n"
3532 " [--no-acls] [--strict-acls] [--to-stdout]\n"
3533 " [--dest-dir=CMD_DIR] [--include-invalid-names]\n"
3537 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME) [NEW_NAME\n"
3538 " [NEW_DESC]]] [--boot] [--check] [--nocheck] [--header]\n"
3539 " [--lookup-table] [--xml] [--extract-xml FILE]\n"
3544 " %"TS" OUT_WIMFILE SPLIT_WIM_PART... [--check]\n"
3546 #if WIM_MOUNTING_SUPPORTED
3549 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3550 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
3551 " [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n"
3555 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3556 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
3557 " [--staging-dir=CMD_DIR] [--unix-data] [--allow-other]\n"
3562 " %"TS" WIMFILE [--check] [--nocheck] [--recompress]\n"
3563 " [--threads=NUM_THREADS] [--pipable] [--not-pipable]\n"
3567 " %"TS" WIMFILE SPLIT_WIM_PART_1 PART_SIZE_MB [--check]\n"
3569 #if WIM_MOUNTING_SUPPORTED
3572 " %"TS" DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
3577 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
3578 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
3579 " [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
3583 static const tchar *invocation_name;
3584 static int invocation_cmd = CMD_NONE;
3586 static const tchar *get_cmd_string(int cmd, bool nospace)
3588 static tchar buf[50];
3589 if (cmd == CMD_NONE) {
3590 tsprintf(buf, T("%"TS), T(IMAGEX_PROGNAME));
3591 } else if (invocation_cmd != CMD_NONE) {
3592 tsprintf(buf, T("wim%"TS), imagex_commands[cmd].name);
3594 const tchar *format;
3597 format = T("%"TS"-%"TS"");
3599 format = T("%"TS" %"TS"");
3600 tsprintf(buf, format, invocation_name, imagex_commands[cmd].name);
3608 static const tchar *s =
3610 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
3611 "Copyright (C) 2012, 2013 Eric Biggers\n"
3612 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
3613 "This is free software: you are free to change and redistribute it.\n"
3614 "There is NO WARRANTY, to the extent permitted by law.\n"
3616 "Report bugs to "PACKAGE_BUGREPORT".\n"
3623 help_or_version(int argc, tchar **argv, int cmd)
3628 for (i = 1; i < argc; i++) {
3630 if (p[0] == T('-') && p[1] == T('-')) {
3632 if (!tstrcmp(p, T("help"))) {
3633 if (cmd == CMD_NONE)
3638 } else if (!tstrcmp(p, T("version"))) {
3647 print_usage_string(int cmd, FILE *fp)
3649 tfprintf(fp, usage_strings[cmd], get_cmd_string(cmd, false));
3653 recommend_man_page(int cmd, FILE *fp)
3655 const tchar *format_str;
3657 format_str = T("See %"TS".pdf in the doc directory for more details.\n");
3659 format_str = T("Try `man %"TS"' for more details.\n");
3661 tfprintf(fp, format_str, get_cmd_string(cmd, true));
3665 usage(int cmd, FILE *fp)
3667 tfprintf(fp, T("Usage:\n"));
3668 print_usage_string(cmd, fp);
3669 tfprintf(fp, T("\n"));
3670 recommend_man_page(cmd, fp);
3676 tfprintf(fp, T("Usage:\n"));
3677 for (int cmd = 0; cmd < CMD_MAX; cmd++) {
3678 print_usage_string(cmd, fp);
3679 tfprintf(fp, T("\n"));
3681 static const tchar *extra =
3684 " %"TS" --version\n"
3686 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
3689 tfprintf(fp, extra, invocation_name, invocation_name);
3690 recommend_man_page(CMD_NONE, fp);
3693 /* Entry point for wimlib's ImageX implementation. On UNIX the command
3694 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
3695 * something else), while an Windows the command arguments will be UTF-16LE
3696 * encoded 'wchar_t' strings. */
3699 wmain(int argc, wchar_t **argv, wchar_t **envp)
3701 main(int argc, char **argv)
3708 imagex_info_file = stdout;
3709 invocation_name = tbasename(argv[0]);
3712 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
3713 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
3717 setlocale(LC_ALL, "");
3718 codeset = nl_langinfo(CODESET);
3719 if (!strstr(codeset, "UTF-8") &&
3720 !strstr(codeset, "UTF8") &&
3721 !strstr(codeset, "utf-8") &&
3722 !strstr(codeset, "utf8"))
3725 "WARNING: Running %"TS" in a UTF-8 locale is recommended!\n"
3726 " Maybe try: `export LANG=en_US.UTF-8'?\n"
3727 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
3728 " to any value to force wimlib to use UTF-8.\n",
3733 #endif /* !__WIN32__ */
3735 /* Allow being invoked as wimCOMMAND (e.g. wimapply). */
3737 if (!tstrncmp(invocation_name, T("wim"), 3) &&
3738 tstrcmp(invocation_name, T(IMAGEX_PROGNAME))) {
3739 for (int i = 0; i < CMD_MAX; i++) {
3740 if (!tstrcmp(invocation_name + 3,
3741 imagex_commands[i].name))
3750 /* Unless already known from the invocation name, determine which
3751 * command was specified. */
3752 if (cmd == CMD_NONE) {
3754 imagex_error(T("No command specified!\n"));
3758 for (int i = 0; i < CMD_MAX; i++) {
3759 if (!tstrcmp(argv[1], imagex_commands[i].name)) {
3764 if (cmd != CMD_NONE) {
3770 /* Handle --help and --version. --help can be either for the program as
3771 * a whole (cmd == CMD_NONE) or just for a specific command (cmd !=
3772 * CMD_NONE). Note: help_or_version() will not return if a --help or
3773 * --version argument was found. */
3774 help_or_version(argc, argv, cmd);
3776 /* Bail if a valid command was not specified. */
3777 if (cmd == CMD_NONE) {
3778 imagex_error(T("Unrecognized command: `%"TS"'\n"), argv[1]);
3783 /* Enable warning and error messages in wimlib be more user-friendly.
3785 wimlib_set_print_errors(true);
3787 /* Initialize wimlib. */
3788 ret = wimlib_global_init(init_flags);
3790 goto out_check_status;
3792 /* Call the command handler function. */
3793 ret = imagex_commands[cmd].func(argc, argv, cmd);
3795 /* Check for error writing to standard output, especially since for some
3796 * commands, writing to standard output is part of the program's actual
3797 * behavior and not just for informational purposes. */
3798 if (ferror(stdout) || fclose(stdout)) {
3799 imagex_error_with_errno(T("error writing to standard output"));
3804 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
3805 * itself (not by wimlib). 0 indicates success. > 0 indicates a wimlib
3806 * error code from which an error message can be printed. */
3808 imagex_error(T("Exiting with error code %d:\n"
3810 wimlib_get_error_string(ret));
3811 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
3812 imagex_error_with_errno(T("errno"));
3814 /* Make wimlib free any resources it's holding (although this is not
3815 * strictly necessary because the process is ending anyway). */
3816 wimlib_global_cleanup();