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/>.
27 #include "wimlib_tchar.h"
47 # include "imagex-win32.h"
48 # define tbasename win32_wbasename
49 # define tglob win32_wglob
53 # include <langinfo.h>
54 # define tbasename basename
59 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
61 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
82 static void usage(int cmd_type);
83 static void usage_all();
85 static bool imagex_be_quiet = false;
88 static const tchar *usage_strings[] = {
91 IMAGEX_PROGNAME" append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
92 " [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
93 " [--verbose] [--dereference] [--config=FILE]\n"
94 " [--threads=NUM_THREADS] [--rebuild] [--unix-data]\n"
95 " [--source-list] [--no-acls] [--strict-acls]\n"
96 " [--rpfix] [--norpfix]\n"
100 IMAGEX_PROGNAME" apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
101 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
102 " [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n"
103 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
107 IMAGEX_PROGNAME" capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
108 " [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
109 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
110 " [--config=FILE] [--threads=NUM_THREADS] [--unix-data]\n"
111 " [--source-list] [--no-acls] [--strict-acls]\n"
112 " [--rpfix] [--norpfix]\n"
116 IMAGEX_PROGNAME" delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check] [--soft]\n"
120 IMAGEX_PROGNAME" dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n"
124 IMAGEX_PROGNAME" export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
125 " DEST_WIMFILE [DEST_IMAGE_NAME] [DEST_IMAGE_DESCRIPTION]\n"
126 " [--boot] [--check] [--compress=TYPE] [--ref=\"GLOB\"]\n"
127 " [--threads=NUM_THREADS] [--rebuild]\n"
131 IMAGEX_PROGNAME" extract WIMFILE (IMAGE_NUM | IMAGE_NAME) [PATH...]\n"
132 " [--check] [--ref=\"GLOB\"] [--verbose] [--unix-data]\n"
133 " [--no-acls] [--strict-acls] [--to-stdout] [--dest-dir=DIR]\n"
137 IMAGEX_PROGNAME" info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
138 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
139 " [--xml] [--extract-xml FILE] [--metadata]\n"
143 IMAGEX_PROGNAME" join [--check] WIMFILE SPLIT_WIM...\n"
147 IMAGEX_PROGNAME" mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
148 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
149 " [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n"
153 IMAGEX_PROGNAME" mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
154 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
155 " [--staging-dir=DIR] [--unix-data] [--allow-other]\n"
159 IMAGEX_PROGNAME" optimize WIMFILE [--check] [--recompress]\n"
160 " [--threads=NUM_THREADS]\n"
164 IMAGEX_PROGNAME" split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n"
168 IMAGEX_PROGNAME" unmount DIRECTORY [--commit] [--check] [--rebuild]\n"
172 IMAGEX_PROGNAME" update WIMFILE IMAGE [--check] [--rebuild]\n"
173 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
174 " [DEFAULT_DELETE_OPTIONS] [< CMDFILE]\n"
175 " ... where each CMD is:\n"
176 " add [--unix-data] [--no-acls] [--strict-acls] [--dereference]\n"
177 " [--verbose] FILE_OR_DIRECTORY DEST_WIM_PATH\n"
178 " delete [--force] [--recursive] WIM_PATH\n"
179 " rename SRC_PATH_IN_WIM DEST_PATH_IN_WIM\n"
184 IMAGEX_ALLOW_OTHER_OPTION,
187 IMAGEX_COMMIT_OPTION,
188 IMAGEX_COMPRESS_OPTION,
189 IMAGEX_CONFIG_OPTION,
191 IMAGEX_DEREFERENCE_OPTION,
192 IMAGEX_DEST_DIR_OPTION,
193 IMAGEX_EXTRACT_XML_OPTION,
196 IMAGEX_HARDLINK_OPTION,
197 IMAGEX_HEADER_OPTION,
198 IMAGEX_LOOKUP_TABLE_OPTION,
199 IMAGEX_METADATA_OPTION,
200 IMAGEX_NORPFIX_OPTION,
201 IMAGEX_NO_ACLS_OPTION,
202 IMAGEX_REBUILD_OPTION,
203 IMAGEX_RECOMPRESS_OPTION,
204 IMAGEX_RECURSIVE_OPTION,
208 IMAGEX_SOURCE_LIST_OPTION,
209 IMAGEX_STAGING_DIR_OPTION,
210 IMAGEX_STREAMS_INTERFACE_OPTION,
211 IMAGEX_STRICT_ACLS_OPTION,
212 IMAGEX_SYMLINK_OPTION,
213 IMAGEX_THREADS_OPTION,
214 IMAGEX_TO_STDOUT_OPTION,
215 IMAGEX_UNIX_DATA_OPTION,
216 IMAGEX_VERBOSE_OPTION,
220 static const struct option apply_options[] = {
221 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
222 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
223 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
224 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
225 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
226 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
227 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
228 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
229 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
230 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
231 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
234 static const struct option capture_or_append_options[] = {
235 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
236 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
237 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
238 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
239 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
240 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
241 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
242 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
243 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
244 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
245 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
246 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
247 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
248 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
249 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
250 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
253 static const struct option delete_options[] = {
254 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
255 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
259 static const struct option export_options[] = {
260 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
261 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
262 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
263 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
264 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
265 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
269 static const struct option extract_options[] = {
270 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
271 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
272 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
273 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
274 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
275 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
276 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
277 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
278 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
282 static const struct option info_options[] = {
283 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
284 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
285 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
286 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
287 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
288 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
289 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
293 static const struct option join_options[] = {
294 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
298 static const struct option mount_options[] = {
299 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
300 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
301 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
302 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
303 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
304 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
305 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
309 static const struct option optimize_options[] = {
310 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
311 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
312 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
316 static const struct option split_options[] = {
317 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
321 static const struct option unmount_options[] = {
322 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
323 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
324 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
328 static const struct option update_options[] = {
329 /* Careful: some of the options here set the defaults for update
330 * commands, but the flags given to an actual update command (and not to
331 * `imagex update' itself are also handled in
332 * update_command_add_option(). */
333 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
334 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
335 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
336 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
337 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
338 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
339 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
340 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
341 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
342 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
343 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
347 /* Print formatted error message to stderr. */
349 imagex_error(const tchar *format, ...)
352 va_start(va, format);
353 tfputs(T("ERROR: "), stderr);
354 tvfprintf(stderr, format, va);
355 tputc(T('\n'), stderr);
359 /* Print formatted error message to stderr. */
361 imagex_error_with_errno(const tchar *format, ...)
363 int errno_save = errno;
365 va_start(va, format);
366 tfputs(T("ERROR: "), stderr);
367 tvfprintf(stderr, format, va);
368 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
373 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
375 if (image == WIMLIB_NO_IMAGE) {
376 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
377 " Please specify a 1-based image index or "
379 " You may use `"IMAGEX_PROGNAME" info' to list the images "
380 "contained in a WIM."),
381 image_name, wim_name);
388 verify_image_is_single(int image)
390 if (image == WIMLIB_ALL_IMAGES) {
391 imagex_error(T("Cannot specify all images for this action!"));
398 verify_image_exists_and_is_single(int image, const tchar *image_name,
399 const tchar *wim_name)
402 ret = verify_image_exists(image, image_name, wim_name);
404 ret = verify_image_is_single(image);
408 /* Parse the argument to --compress */
410 get_compression_type(const tchar *optarg)
412 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
413 return WIMLIB_COMPRESSION_TYPE_LZX;
414 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
415 return WIMLIB_COMPRESSION_TYPE_XPRESS;
416 else if (!tstrcasecmp(optarg, T("none")))
417 return WIMLIB_COMPRESSION_TYPE_NONE;
419 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
420 "\"maximum\", \"fast\", or \"none\"."), optarg);
421 return WIMLIB_COMPRESSION_TYPE_INVALID;
425 /* Returns the size of a file given its name, or -1 if the file does not exist
426 * or its size cannot be determined. */
428 file_get_size(const tchar *filename)
431 if (tstat(filename, &st) == 0)
437 tchar pat_ntfs_log[] = T("/$ntfs.log");
438 tchar pat_hiberfil_sys[] = T("/hiberfil.sys");
439 tchar pat_pagefile_sys[] = T("/pagefile.sys");
440 tchar pat_system_volume_information[] = T("/System Volume Information");
441 tchar pat_recycler[] = T("/RECYCLER");
442 tchar pat_windows_csc[] = T("/Windows/CSC");
444 tchar *default_pats[] = {
448 pat_system_volume_information,
453 static struct wimlib_capture_config default_capture_config = {
455 .num_pats = sizeof(default_pats) / sizeof(*default_pats),
456 .pats = default_pats,
461 PARSE_STRING_SUCCESS = 0,
462 PARSE_STRING_FAILURE = 1,
463 PARSE_STRING_NONE = 2,
467 * Parses a string token from an array of characters.
469 * Tokens are either whitespace-delimited, or double or single-quoted.
471 * @line_p: Pointer to the pointer to the line of data. Will be updated
472 * to point past the string token iff the return value is
473 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
476 * @len_p: @len_p initially stores the length of the line of data, which may
477 * be 0, and it will be updated to the number of bytes remaining in
478 * the line iff the return value is PARSE_STRING_SUCCESS.
480 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
481 * parsed string token will be returned here.
483 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
484 * PARSE_STRING_FAILURE if the data was invalid due to a missing
485 * closing quote; or PARSE_STRING_NONE if the line ended before the
486 * beginning of a string token was found.
489 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
492 tchar *line = *line_p;
496 /* Skip leading whitespace */
499 return PARSE_STRING_NONE;
500 if (!istspace(*line) && *line != T('\0'))
506 if (quote_char == T('"') || quote_char == T('\'')) {
511 line = tmemchr(line, quote_char, len);
513 imagex_error(T("Missing closing quote: %"TS), fn - 1);
514 return PARSE_STRING_FAILURE;
517 /* Unquoted string. Go until whitespace. Line is terminated
518 * by '\0', so no need to check 'len'. */
522 } while (!istspace(*line) && *line != T('\0'));
529 return PARSE_STRING_SUCCESS;
532 /* Parses a line of data (not an empty line or comment) in the source list file
533 * format. (See the man page for 'wimlib-imagex capture' for details on this
534 * format and the meaning.)
536 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
537 * len == 0. The data in @line will be modified by this function call.
539 * @len: Length of the line of data.
541 * @source: On success, the capture source and target described by the line is
542 * written into this destination. Note that it will contain pointers
543 * to data in the @line array.
545 * Returns true if the line was valid; false otherwise. */
547 parse_source_list_line(tchar *line, size_t len,
548 struct wimlib_capture_source *source)
552 ret = parse_string(&line, &len, &source->fs_source_path);
553 if (ret != PARSE_STRING_SUCCESS)
555 ret = parse_string(&line, &len, &source->wim_target_path);
556 if (ret == PARSE_STRING_NONE)
557 source->wim_target_path = source->fs_source_path;
558 return ret != PARSE_STRING_FAILURE;
561 /* Returns %true if the given line of length @len > 0 is a comment or empty line
562 * in the source list file format. */
564 is_comment_line(const tchar *line, size_t len)
569 if (!istspace(*line) && *line != T('\0'))
579 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
582 tchar *contents = *contents_p;
583 size_t nchars = *nchars_p;
586 for (i = 0; i < nchars; i++)
587 if (contents[i] == T('\n'))
590 /* Handle last line not terminated by a newline */
591 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
592 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
594 imagex_error(T("Out of memory!"));
597 contents[nchars] = T('\n');
598 *contents_p = contents;
606 /* Parses a file in the source list format. (See the man page for
607 * 'wimlib-imagex capture' for details on this format and the meaning.)
609 * @source_list_contents: Contents of the source list file. Note that this
610 * buffer will be modified to save memory allocations,
611 * and cannot be freed until the returned array of
612 * wimlib_capture_source's has also been freed.
614 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
617 * @nsources_ret: On success, the length of the returned array is
620 * Returns: An array of `struct wimlib_capture_source's that can be passed to
621 * the wimlib_add_image_multisource() function to specify how a WIM image is to
623 static struct wimlib_capture_source *
624 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
625 size_t *nsources_ret)
629 struct wimlib_capture_source *sources;
632 nlines = text_file_count_lines(source_list_contents_p,
633 &source_list_nchars);
637 /* Always allocate at least 1 slot, just in case the implementation of
638 * calloc() returns NULL if 0 bytes are requested. */
639 sources = calloc(nlines ?: 1, sizeof(*sources));
641 imagex_error(T("out of memory"));
644 p = *source_list_contents_p;
646 for (i = 0; i < nlines; i++) {
647 /* XXX: Could use rawmemchr() here instead, but it may not be
648 * available on all platforms. */
649 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
650 size_t len = endp - p + 1;
652 if (!is_comment_line(p, len)) {
653 if (!parse_source_list_line(p, len, &sources[j++])) {
666 enum capture_config_section {
667 CAPTURE_CONFIG_NO_SECTION,
668 CAPTURE_CONFIG_EXCLUSION_SECTION,
669 CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
670 CAPTURE_CONFIG_IGNORE_SECTION,
674 CAPTURE_CONFIG_INVALID_SECTION,
675 CAPTURE_CONFIG_CHANGED_SECTION,
676 CAPTURE_CONFIG_SAME_SECTION,
680 check_config_section(tchar *line, size_t len,
681 enum capture_config_section *cur_section)
683 while (istspace(*line))
687 return CAPTURE_CONFIG_SAME_SECTION;
690 tchar *endbrace = tstrrchr(line, T(']'));
692 return CAPTURE_CONFIG_SAME_SECTION;
694 if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
695 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
696 } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
697 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
698 } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
699 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
700 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
701 "of capture config file\n"),
703 } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
704 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
705 tfputs(T("WARNING: Ignoring [AlignmentList] section "
706 "of capture config file\n"),
709 imagex_error(T("Invalid capture config file section \"%"TS"\""),
711 return CAPTURE_CONFIG_INVALID_SECTION;
713 return CAPTURE_CONFIG_CHANGED_SECTION;
718 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
721 if (pat_list->num_pats == pat_list->num_allocated_pats) {
723 size_t num_allocated_pats = pat_list->num_pats + 8;
725 pats = realloc(pat_list->pats,
726 num_allocated_pats * sizeof(pat_list->pats[0]));
728 imagex_error(T("Out of memory!"));
731 pat_list->pats = pats;
732 pat_list->num_allocated_pats = num_allocated_pats;
734 pat_list->pats[pat_list->num_pats++] = pat;
739 parse_capture_config_line(tchar *line, size_t len,
740 enum capture_config_section *cur_section,
741 struct wimlib_capture_config *config)
746 ret = check_config_section(line, len, cur_section);
747 if (ret == CAPTURE_CONFIG_INVALID_SECTION)
749 if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
752 switch (*cur_section) {
753 case CAPTURE_CONFIG_NO_SECTION:
754 imagex_error(T("Line \"%"TS"\" is not in a section "
755 "(such as [ExclusionList]"), line);
757 case CAPTURE_CONFIG_EXCLUSION_SECTION:
758 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
760 return pattern_list_add_pattern(&config->exclusion_pats,
762 case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
763 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
765 return pattern_list_add_pattern(&config->exclusion_exception_pats,
767 case CAPTURE_CONFIG_IGNORE_SECTION:
774 parse_capture_config(tchar **contents_p, size_t nchars,
775 struct wimlib_capture_config *config)
780 enum capture_config_section cur_section;
782 memset(config, 0, sizeof(*config));
784 nlines = text_file_count_lines(contents_p, &nchars);
788 cur_section = CAPTURE_CONFIG_NO_SECTION;
790 for (i = 0; i < nlines; i++) {
791 tchar *endp = tmemchr(p, T('\n'), nchars);
792 size_t len = endp - p + 1;
794 if (!is_comment_line(p, len))
795 if (!parse_capture_config_line(p, len, &cur_section, config))
803 /* Reads the contents of a file into memory. */
805 file_get_contents(const tchar *filename, size_t *len_ret)
812 if (tstat(filename, &stbuf) != 0) {
813 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
818 fp = tfopen(filename, T("rb"));
820 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
826 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
827 "contents of file \"%"TS"\""), len, filename);
830 if (fread(buf, 1, len, fp) != len) {
831 imagex_error_with_errno(T("Failed to read %zu bytes from the "
832 "file \"%"TS"\""), len, filename);
846 /* Read standard input until EOF and return the full contents in a malloc()ed
847 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
850 stdin_get_contents(size_t *len_ret)
852 /* stdin can, of course, be a pipe or other non-seekable file, so the
853 * total length of the data cannot be pre-determined */
855 size_t newlen = 1024;
859 char *p = realloc(buf, newlen);
860 size_t bytes_read, bytes_to_read;
862 imagex_error(T("out of memory while reading stdin"));
866 bytes_to_read = newlen - pos;
867 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
869 if (bytes_read != bytes_to_read) {
874 imagex_error_with_errno(T("error reading stdin"));
888 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
891 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
893 *num_tchars_ret = num_bytes;
895 #else /* !__WIN32__ */
896 /* On Windows, translate the text to UTF-16LE */
900 if (num_bytes >= 2 &&
901 ((text[0] == 0xff && text[1] == 0xfe) ||
902 (text[0] <= 0x7f && text[1] == 0x00)))
904 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
905 * with something that looks like an ASCII character encoded as
906 * a UTF-16LE code unit. Assume the file is encoded as
907 * UTF-16LE. This is not a 100% reliable check. */
908 num_wchars = num_bytes / 2;
909 text_wstr = (wchar_t*)text;
911 /* File does not look like UTF-16LE. Assume it is encoded in
912 * the current Windows code page. I think these are always
913 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
914 * should work as expected. */
915 text_wstr = win32_mbs_to_wcs(text,
920 *num_tchars_ret = num_wchars;
922 #endif /* __WIN32__ */
926 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
931 contents = file_get_contents(filename, &num_bytes);
934 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
938 stdin_get_text_contents(size_t *num_tchars_ret)
943 contents = stdin_get_contents(&num_bytes);
946 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
949 /* Return 0 if a path names a file to which the current user has write access;
950 * -1 otherwise (and print an error message). */
952 file_writable(const tchar *path)
955 ret = taccess(path, W_OK);
957 imagex_error_with_errno(T("Can't modify \"%"TS"\""), path);
961 #define TO_PERCENT(numerator, denominator) \
962 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
964 /* Given an enumerated value for WIM compression type, return a descriptive
967 get_data_type(int ctype)
970 case WIMLIB_COMPRESSION_TYPE_NONE:
971 return T("uncompressed");
972 case WIMLIB_COMPRESSION_TYPE_LZX:
973 return T("LZX-compressed");
974 case WIMLIB_COMPRESSION_TYPE_XPRESS:
975 return T("XPRESS-compressed");
980 /* Progress callback function passed to various wimlib functions. */
982 imagex_progress_func(enum wimlib_progress_msg msg,
983 const union wimlib_progress_info *info)
985 unsigned percent_done;
989 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
990 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
991 info->write_streams.total_bytes);
992 if (info->write_streams.completed_streams == 0) {
993 const tchar *data_type;
995 data_type = get_data_type(info->write_streams.compression_type);
996 tprintf(T("Writing %"TS" data using %u thread%"TS"\n"),
997 data_type, info->write_streams.num_threads,
998 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1000 tprintf(T("\r%"PRIu64" MiB of %"PRIu64" MiB (uncompressed) "
1001 "written (%u%% done)"),
1002 info->write_streams.completed_bytes >> 20,
1003 info->write_streams.total_bytes >> 20,
1005 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1008 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1009 tprintf(T("Scanning \"%"TS"\""), info->scan.source);
1010 if (*info->scan.wim_target_path) {
1011 tprintf(T(" (loading as WIM path: \"/%"TS"\")...\n"),
1012 info->scan.wim_target_path);
1014 tprintf(T(" (loading as root of WIM image)...\n"));
1017 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1018 if (info->scan.excluded)
1019 tprintf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1021 tprintf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
1023 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
1025 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1026 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1027 info->integrity.total_bytes);
1028 tprintf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" MiB "
1029 "of %"PRIu64" MiB (%u%%) done"),
1030 info->integrity.filename,
1031 info->integrity.completed_bytes >> 20,
1032 info->integrity.total_bytes >> 20,
1034 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1037 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1038 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1039 info->integrity.total_bytes);
1040 tprintf(T("\rCalculating integrity table for WIM: %"PRIu64" MiB "
1041 "of %"PRIu64" MiB (%u%%) done"),
1042 info->integrity.completed_bytes >> 20,
1043 info->integrity.total_bytes >> 20,
1045 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1048 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1049 tprintf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1050 "to %"TS" \"%"TS"\"\n"),
1051 info->extract.image,
1052 info->extract.image_name,
1053 info->extract.wimfile_name,
1054 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1055 T("NTFS volume") : T("directory")),
1056 info->extract.target);
1058 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1059 tprintf(T("Extracting \"%"TS"\" from image %d (\"%"TS"\") "
1060 "in \"%"TS"\" to \"%"TS"\"\n"),
1061 info->extract.extract_root_wim_source_path,
1062 info->extract.image,
1063 info->extract.image_name,
1064 info->extract.wimfile_name,
1065 info->extract.target);
1067 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
1068 /*tprintf(T("Applying directory structure to %"TS"\n"),*/
1069 /*info->extract.target);*/
1071 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1072 percent_done = TO_PERCENT(info->extract.completed_bytes,
1073 info->extract.total_bytes);
1074 tprintf(T("\rExtracting files: "
1075 "%"PRIu64" MiB of %"PRIu64" MiB (%u%%) done"),
1076 info->extract.completed_bytes >> 20,
1077 info->extract.total_bytes >> 20,
1079 if (info->extract.completed_bytes >= info->extract.total_bytes)
1082 case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
1083 tprintf(T("%"TS"\n"), info->extract.cur_path);
1085 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1086 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1087 tprintf(T("Setting timestamps on all extracted files...\n"));
1089 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1090 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1091 tprintf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1092 info->extract.target);
1095 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
1096 percent_done = TO_PERCENT(info->join.completed_bytes,
1097 info->join.total_bytes);
1098 tprintf(T("Writing resources from part %u of %u: "
1099 "%"PRIu64 " MiB of %"PRIu64" MiB (%u%%) written\n"),
1100 (info->join.completed_parts == info->join.total_parts) ?
1101 info->join.completed_parts : info->join.completed_parts + 1,
1102 info->join.total_parts,
1103 info->join.completed_bytes >> 20,
1104 info->join.total_bytes >> 20,
1107 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1108 percent_done = TO_PERCENT(info->split.completed_bytes,
1109 info->split.total_bytes);
1110 tprintf(T("Writing \"%"TS"\": %"PRIu64" MiB of "
1111 "%"PRIu64" MiB (%u%%) written\n"),
1112 info->split.part_name,
1113 info->split.completed_bytes >> 20,
1114 info->split.total_bytes >> 20,
1117 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1118 if (info->split.completed_bytes == info->split.total_bytes) {
1119 tprintf(T("Finished writing %u split WIM parts\n"),
1120 info->split.cur_part_number);
1130 /* Open all the split WIM parts that correspond to a file glob.
1132 * @first_part specifies the first part of the split WIM and it may be either
1133 * included or omitted from the glob. */
1135 open_swms_from_glob(const tchar *swm_glob,
1136 const tchar *first_part,
1138 WIMStruct ***additional_swms_ret,
1139 unsigned *num_additional_swms_ret)
1141 unsigned num_additional_swms = 0;
1142 WIMStruct **additional_swms = NULL;
1146 /* Warning: glob() is replaced in Windows native builds */
1147 ret = tglob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
1149 if (ret == GLOB_NOMATCH) {
1150 imagex_error(T("Found no files for glob \"%"TS"\""),
1153 imagex_error_with_errno(T("Failed to process glob \"%"TS"\""),
1159 num_additional_swms = globbuf.gl_pathc;
1160 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
1161 if (!additional_swms) {
1162 imagex_error(T("Out of memory"));
1166 unsigned offset = 0;
1167 for (unsigned i = 0; i < num_additional_swms; i++) {
1168 if (tstrcmp(globbuf.gl_pathv[i], first_part) == 0) {
1172 ret = wimlib_open_wim(globbuf.gl_pathv[i],
1173 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
1174 &additional_swms[i - offset],
1175 imagex_progress_func);
1177 goto out_close_swms;
1179 *additional_swms_ret = additional_swms;
1180 *num_additional_swms_ret = num_additional_swms - offset;
1184 for (unsigned i = 0; i < num_additional_swms; i++)
1185 wimlib_free(additional_swms[i]);
1186 free(additional_swms);
1195 parse_num_threads(const tchar *optarg)
1198 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1199 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1200 imagex_error(T("Number of threads must be a non-negative integer!"));
1208 * Parse an option passed to an update command.
1210 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1213 * @option: Text string for the option (beginning with --)
1215 * @cmd: `struct wimlib_update_command' that is being constructed for
1218 * Returns true if the option was recognized; false if not.
1221 update_command_add_option(int op, const tchar *option,
1222 struct wimlib_update_command *cmd)
1224 bool recognized = true;
1226 case WIMLIB_UPDATE_OP_ADD:
1227 if (!tstrcmp(option, T("--unix-data")))
1228 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1229 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1230 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1231 else if (!tstrcmp(option, T("--strict-acls")))
1232 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1233 else if (!tstrcmp(option, T("--dereference")))
1234 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1238 case WIMLIB_UPDATE_OP_DELETE:
1239 if (!tstrcmp(option, T("--force")))
1240 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1241 else if (!tstrcmp(option, T("--recursive")))
1242 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1246 case WIMLIB_UPDATE_OP_RENAME:
1253 /* How many nonoption arguments each `imagex update' command expects */
1254 static const unsigned update_command_num_nonoptions[] = {
1255 [WIMLIB_UPDATE_OP_ADD] = 2,
1256 [WIMLIB_UPDATE_OP_DELETE] = 1,
1257 [WIMLIB_UPDATE_OP_RENAME] = 2,
1261 update_command_add_nonoption(int op, const tchar *nonoption,
1262 struct wimlib_update_command *cmd,
1263 unsigned num_nonoptions)
1266 case WIMLIB_UPDATE_OP_ADD:
1267 if (num_nonoptions == 0)
1268 cmd->add.fs_source_path = (tchar*)nonoption;
1270 cmd->add.wim_target_path = (tchar*)nonoption;
1272 case WIMLIB_UPDATE_OP_DELETE:
1273 cmd->delete.wim_path = (tchar*)nonoption;
1275 case WIMLIB_UPDATE_OP_RENAME:
1276 if (num_nonoptions == 0)
1277 cmd->rename.wim_source_path = (tchar*)nonoption;
1279 cmd->rename.wim_target_path = (tchar*)nonoption;
1285 * Parse a command passed on stdin to `imagex update'.
1287 * @line: Text of the command.
1288 * @len: Length of the line, including a null terminator
1291 * @command: A `struct wimlib_update_command' to fill in from the parsed
1294 * @line_number: Line number of the command, for diagnostics.
1296 * Returns true on success; returns false on parse error.
1299 parse_update_command(tchar *line, size_t len,
1300 struct wimlib_update_command *command,
1304 tchar *command_name;
1306 size_t num_nonoptions;
1308 /* Get the command name ("add", "delete", "rename") */
1309 ret = parse_string(&line, &len, &command_name);
1310 if (ret != PARSE_STRING_SUCCESS)
1313 if (!tstrcasecmp(command_name, T("add"))) {
1314 op = WIMLIB_UPDATE_OP_ADD;
1315 } else if (!tstrcasecmp(command_name, T("delete"))) {
1316 op = WIMLIB_UPDATE_OP_DELETE;
1317 } else if (!tstrcasecmp(command_name, T("rename"))) {
1318 op = WIMLIB_UPDATE_OP_RENAME;
1320 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1321 command_name, line_number);
1326 /* Parse additional options and non-options as needed */
1331 ret = parse_string(&line, &len, &next_string);
1332 if (ret == PARSE_STRING_NONE) /* End of line */
1334 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1336 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1338 if (!update_command_add_option(op, next_string, command))
1340 imagex_error(T("Unrecognized option \"%"TS"\" to "
1341 "update command on line %zu"),
1342 next_string, line_number);
1348 if (num_nonoptions == update_command_num_nonoptions[op])
1350 imagex_error(T("Unexpected argument \"%"TS"\" in "
1351 "update command on line %zu\n"
1352 " (The \"%"TS"\" command only "
1353 "takes %u nonoption arguments!)\n"),
1354 next_string, line_number,
1355 command_name, num_nonoptions);
1358 update_command_add_nonoption(op, next_string,
1359 command, num_nonoptions);
1364 if (num_nonoptions != update_command_num_nonoptions[op]) {
1365 imagex_error(T("Not enough arguments to update command "
1366 "\"%"TS"\" on line %zu"), command_name, line_number);
1372 static struct wimlib_update_command *
1373 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1374 size_t *num_cmds_ret)
1378 struct wimlib_update_command *cmds;
1381 nlines = text_file_count_lines(cmd_file_contents_p,
1386 /* Always allocate at least 1 slot, just in case the implementation of
1387 * calloc() returns NULL if 0 bytes are requested. */
1388 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1390 imagex_error(T("out of memory"));
1393 p = *cmd_file_contents_p;
1395 for (i = 0; i < nlines; i++) {
1396 /* XXX: Could use rawmemchr() here instead, but it may not be
1397 * available on all platforms. */
1398 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1399 size_t len = endp - p + 1;
1401 if (!is_comment_line(p, len)) {
1402 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1413 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1414 * one image from a WIM file to a NTFS volume. */
1416 imagex_apply(int argc, tchar **argv)
1419 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1424 const tchar *wimfile;
1425 const tchar *target;
1426 const tchar *image_num_or_name;
1427 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1429 const tchar *swm_glob = NULL;
1430 WIMStruct **additional_swms = NULL;
1431 unsigned num_additional_swms = 0;
1433 for_opt(c, apply_options) {
1435 case IMAGEX_CHECK_OPTION:
1436 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1438 case IMAGEX_HARDLINK_OPTION:
1439 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1441 case IMAGEX_SYMLINK_OPTION:
1442 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1444 case IMAGEX_VERBOSE_OPTION:
1445 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
1447 case IMAGEX_REF_OPTION:
1450 case IMAGEX_UNIX_DATA_OPTION:
1451 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1453 case IMAGEX_NO_ACLS_OPTION:
1454 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1456 case IMAGEX_STRICT_ACLS_OPTION:
1457 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1459 case IMAGEX_NORPFIX_OPTION:
1460 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1462 case IMAGEX_RPFIX_OPTION:
1463 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1472 if (argc != 2 && argc != 3) {
1479 image_num_or_name = T("1");
1482 image_num_or_name = argv[1];
1486 ret = wimlib_open_wim(wimfile, open_flags, &w, imagex_progress_func);
1490 image = wimlib_resolve_image(w, image_num_or_name);
1491 ret = verify_image_exists(image, image_num_or_name, wimfile);
1495 num_images = wimlib_get_num_images(w);
1496 if (argc == 2 && num_images != 1) {
1497 imagex_error(T("\"%"TS"\" contains %d images; Please select one "
1498 "(or all)"), wimfile, num_images);
1505 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1507 &num_additional_swms);
1514 ret = tstat(target, &stbuf);
1516 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1517 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1519 if (errno != ENOENT) {
1520 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1528 win32_acquire_restore_privileges();
1530 ret = wimlib_extract_image(w, image, target, extract_flags,
1531 additional_swms, num_additional_swms,
1532 imagex_progress_func);
1534 tprintf(T("Done applying WIM image.\n"));
1536 win32_release_restore_privileges();
1540 if (additional_swms) {
1541 for (unsigned i = 0; i < num_additional_swms; i++)
1542 wimlib_free(additional_swms[i]);
1543 free(additional_swms);
1548 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1549 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1550 * the desired image. 'wimlib-imagex append': add a new image to an existing
1553 imagex_capture_or_append(int argc, tchar **argv)
1557 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
1558 int write_flags = 0;
1559 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
1560 const tchar *wimfile;
1563 const tchar *flags_element = NULL;
1567 int cmd = tstrcmp(argv[0], T("append")) ? CAPTURE : APPEND;
1568 unsigned num_threads = 0;
1571 size_t source_name_len;
1574 const tchar *config_file = NULL;
1576 struct wimlib_capture_config *config = NULL;
1578 bool source_list = false;
1579 size_t source_list_nchars;
1580 tchar *source_list_contents;
1581 bool capture_sources_malloced;
1582 struct wimlib_capture_source *capture_sources;
1585 for_opt(c, capture_or_append_options) {
1587 case IMAGEX_BOOT_OPTION:
1588 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1590 case IMAGEX_CHECK_OPTION:
1591 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1592 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1594 case IMAGEX_CONFIG_OPTION:
1595 config_file = optarg;
1597 case IMAGEX_COMPRESS_OPTION:
1598 compression_type = get_compression_type(optarg);
1599 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1602 case IMAGEX_FLAGS_OPTION:
1603 flags_element = optarg;
1605 case IMAGEX_DEREFERENCE_OPTION:
1606 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1608 case IMAGEX_VERBOSE_OPTION:
1609 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1611 case IMAGEX_THREADS_OPTION:
1612 num_threads = parse_num_threads(optarg);
1613 if (num_threads == UINT_MAX)
1616 case IMAGEX_REBUILD_OPTION:
1617 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1619 case IMAGEX_UNIX_DATA_OPTION:
1620 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1622 case IMAGEX_SOURCE_LIST_OPTION:
1625 case IMAGEX_NO_ACLS_OPTION:
1626 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1628 case IMAGEX_STRICT_ACLS_OPTION:
1629 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1631 case IMAGEX_RPFIX_OPTION:
1632 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1634 case IMAGEX_NORPFIX_OPTION:
1635 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1645 if (argc < 2 || argc > 4) {
1656 /* Set default name to SOURCE argument, omitting any directory
1657 * prefixes and trailing slashes. This requires making a copy
1659 source_name_len = tstrlen(source);
1660 source_copy = alloca((source_name_len + 1) * sizeof(tchar));
1661 name = tbasename(tstrcpy(source_copy, source));
1663 /* Image description defaults to NULL if not given. */
1664 desc = (argc >= 4) ? argv[3] : NULL;
1667 /* Set up capture sources in source list mode */
1668 if (source[0] == T('-') && source[1] == T('\0')) {
1669 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1671 source_list_contents = file_get_text_contents(source,
1672 &source_list_nchars);
1674 if (!source_list_contents)
1677 capture_sources = parse_source_list(&source_list_contents,
1680 if (!capture_sources) {
1682 goto out_free_source_list_contents;
1684 capture_sources_malloced = true;
1686 /* Set up capture source in non-source-list mode (could be
1687 * either "normal" mode or "NTFS mode"--- see the man page). */
1688 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1689 capture_sources[0].fs_source_path = source;
1690 capture_sources[0].wim_target_path = NULL;
1691 capture_sources[0].reserved = 0;
1693 capture_sources_malloced = false;
1694 source_list_contents = NULL;
1700 config_str = file_get_text_contents(config_file, &config_len);
1703 goto out_free_capture_sources;
1706 config = alloca(sizeof(*config));
1707 ret = parse_capture_config(&config_str, config_len, config);
1709 goto out_free_config;
1711 config = &default_capture_config;
1715 ret = wimlib_open_wim(wimfile, open_flags, &w,
1716 imagex_progress_func);
1718 ret = wimlib_create_new_wim(compression_type, &w);
1720 goto out_free_config;
1724 ret = tstat(source, &stbuf);
1726 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1727 tprintf(T("Capturing WIM image from NTFS "
1728 "filesystem on \"%"TS"\"\n"), source);
1729 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1732 if (errno != ENOENT) {
1733 imagex_error_with_errno(T("Failed to stat "
1734 "\"%"TS"\""), source);
1736 goto out_wimlib_free;
1741 win32_acquire_capture_privileges();
1744 ret = wimlib_add_image_multisource(w,
1750 imagex_progress_func);
1752 goto out_release_privs;
1753 cur_image = wimlib_get_num_images(w);
1755 ret = wimlib_set_image_descripton(w, cur_image, desc);
1757 goto out_release_privs;
1759 if (flags_element) {
1760 ret = wimlib_set_image_flags(w, cur_image, flags_element);
1762 goto out_release_privs;
1764 if (cmd == APPEND) {
1765 ret = wimlib_overwrite(w, write_flags, num_threads,
1766 imagex_progress_func);
1768 ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
1769 num_threads, imagex_progress_func);
1771 if (ret == WIMLIB_ERR_REOPEN)
1774 imagex_error(T("Failed to write the WIM file \"%"TS"\""),
1778 win32_release_capture_privileges();
1783 if (config != NULL && config != &default_capture_config) {
1784 free(config->exclusion_pats.pats);
1785 free(config->exclusion_exception_pats.pats);
1788 out_free_capture_sources:
1789 if (capture_sources_malloced)
1790 free(capture_sources);
1791 out_free_source_list_contents:
1792 free(source_list_contents);
1796 /* Remove image(s) from a WIM. */
1798 imagex_delete(int argc, tchar **argv)
1802 int write_flags = 0;
1803 const tchar *wimfile;
1804 const tchar *image_num_or_name;
1809 for_opt(c, delete_options) {
1811 case IMAGEX_CHECK_OPTION:
1812 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1813 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1815 case IMAGEX_SOFT_OPTION:
1816 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
1828 imagex_error(T("Must specify a WIM file"));
1830 imagex_error(T("Must specify an image"));
1835 image_num_or_name = argv[1];
1837 ret = file_writable(wimfile);
1841 ret = wimlib_open_wim(wimfile, open_flags, &w,
1842 imagex_progress_func);
1846 image = wimlib_resolve_image(w, image_num_or_name);
1848 ret = verify_image_exists(image, image_num_or_name, wimfile);
1852 ret = wimlib_delete_image(w, image);
1854 imagex_error(T("Failed to delete image from \"%"TS"\""), wimfile);
1858 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1859 if (ret == WIMLIB_ERR_REOPEN)
1862 imagex_error(T("Failed to write the file \"%"TS"\" with image "
1863 "deleted"), wimfile);
1870 /* Print the files contained in an image(s) in a WIM file. */
1872 imagex_dir(int argc, tchar **argv)
1874 const tchar *wimfile;
1881 imagex_error(T("Must specify a WIM file"));
1886 imagex_error(T("Too many arguments"));
1892 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w,
1893 imagex_progress_func);
1898 image = wimlib_resolve_image(w, argv[2]);
1899 ret = verify_image_exists(image, argv[2], wimfile);
1903 /* Image was not specified. If the WIM only contains one image,
1904 * choose that one; otherwise, print an error. */
1905 num_images = wimlib_get_num_images(w);
1906 if (num_images != 1) {
1907 imagex_error(T("The file \"%"TS"\" contains %d images; Please "
1908 "select one."), wimfile, num_images);
1916 ret = wimlib_print_files(w, image);
1922 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
1925 imagex_export(int argc, tchar **argv)
1929 int export_flags = 0;
1930 int write_flags = 0;
1931 int compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
1932 bool compression_type_specified = false;
1933 const tchar *src_wimfile;
1934 const tchar *src_image_num_or_name;
1935 const tchar *dest_wimfile;
1936 const tchar *dest_name;
1937 const tchar *dest_desc;
1938 WIMStruct *src_w = NULL;
1939 WIMStruct *dest_w = NULL;
1944 const tchar *swm_glob = NULL;
1945 WIMStruct **additional_swms = NULL;
1946 unsigned num_additional_swms = 0;
1947 unsigned num_threads = 0;
1949 for_opt(c, export_options) {
1951 case IMAGEX_BOOT_OPTION:
1952 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
1954 case IMAGEX_CHECK_OPTION:
1955 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1956 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1958 case IMAGEX_COMPRESS_OPTION:
1959 compression_type = get_compression_type(optarg);
1960 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1962 compression_type_specified = true;
1964 case IMAGEX_REF_OPTION:
1967 case IMAGEX_THREADS_OPTION:
1968 num_threads = parse_num_threads(optarg);
1969 if (num_threads == UINT_MAX)
1972 case IMAGEX_REBUILD_OPTION:
1973 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1982 if (argc < 3 || argc > 5) {
1986 src_wimfile = argv[0];
1987 src_image_num_or_name = argv[1];
1988 dest_wimfile = argv[2];
1989 dest_name = (argc >= 4) ? argv[3] : NULL;
1990 dest_desc = (argc >= 5) ? argv[4] : NULL;
1991 ret = wimlib_open_wim(src_wimfile,
1992 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w,
1993 imagex_progress_func);
1997 /* Determine if the destination is an existing file or not.
1998 * If so, we try to append the exported image(s) to it; otherwise, we
1999 * create a new WIM containing the exported image(s). */
2000 if (tstat(dest_wimfile, &stbuf) == 0) {
2004 /* Destination file exists. */
2006 if (!S_ISREG(stbuf.st_mode)) {
2007 imagex_error(T("\"%"TS"\" is not a regular file"),
2012 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w,
2013 imagex_progress_func);
2017 ret = file_writable(dest_wimfile);
2021 dest_ctype = wimlib_get_compression_type(dest_w);
2022 if (compression_type_specified
2023 && compression_type != dest_ctype)
2025 imagex_error(T("Cannot specify a compression type that is "
2026 "not the same as that used in the "
2027 "destination WIM"));
2033 /* dest_wimfile is not an existing file, so create a new WIM. */
2034 if (!compression_type_specified)
2035 compression_type = wimlib_get_compression_type(src_w);
2036 if (errno == ENOENT) {
2037 ret = wimlib_create_new_wim(compression_type, &dest_w);
2041 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2048 image = wimlib_resolve_image(src_w, src_image_num_or_name);
2049 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2054 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
2056 &num_additional_swms);
2061 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
2062 export_flags, additional_swms,
2063 num_additional_swms, imagex_progress_func);
2069 ret = wimlib_write(dest_w, dest_wimfile, WIMLIB_ALL_IMAGES,
2070 write_flags, num_threads,
2071 imagex_progress_func);
2073 ret = wimlib_overwrite(dest_w, write_flags, num_threads,
2074 imagex_progress_func);
2076 if (ret == WIMLIB_ERR_REOPEN)
2079 wimlib_free(dest_w);
2080 if (additional_swms) {
2081 for (unsigned i = 0; i < num_additional_swms; i++)
2082 wimlib_free(additional_swms[i]);
2083 free(additional_swms);
2089 is_root_wim_path(const tchar *path)
2092 for (p = path; *p; p++)
2093 if (*p != T('\\') && *p != T('/'))
2099 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2100 const tchar *dest_dir)
2102 for (size_t i = 0; i < num_cmds; i++)
2103 if (cmds[i].fs_dest_path != dest_dir)
2104 free(cmds[i].fs_dest_path);
2108 static struct wimlib_extract_command *
2109 prepare_extract_commands(tchar **argv, int argc, int extract_flags,
2110 tchar *dest_dir, size_t *num_cmds_ret)
2112 struct wimlib_extract_command *cmds;
2114 tchar *emptystr = T("");
2121 cmds = calloc(num_cmds, sizeof(cmds[0]));
2123 imagex_error(T("Out of memory!"));
2127 for (size_t i = 0; i < num_cmds; i++) {
2128 cmds[i].extract_flags = extract_flags;
2129 cmds[i].wim_source_path = argv[i];
2130 if (is_root_wim_path(argv[i])) {
2131 cmds[i].fs_dest_path = dest_dir;
2133 size_t len = tstrlen(dest_dir) + 1 + tstrlen(argv[i]);
2134 cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2135 if (!cmds[i].fs_dest_path) {
2136 free_extract_commands(cmds, num_cmds, dest_dir);
2139 tsprintf(cmds[i].fs_dest_path, T("%"TS"/%"TS), dest_dir,
2140 tbasename(argv[i]));
2143 *num_cmds_ret = num_cmds;
2147 /* Extract files or directories from a WIM image */
2149 imagex_extract(int argc, tchar **argv)
2152 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2156 const tchar *wimfile;
2157 const tchar *image_num_or_name;
2158 tchar *dest_dir = T(".");
2159 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL | WIMLIB_EXTRACT_FLAG_NORPFIX;
2161 const tchar *swm_glob = NULL;
2162 WIMStruct **additional_swms = NULL;
2163 unsigned num_additional_swms = 0;
2165 struct wimlib_extract_command *cmds;
2168 for_opt(c, extract_options) {
2170 case IMAGEX_CHECK_OPTION:
2171 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2173 case IMAGEX_VERBOSE_OPTION:
2174 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
2176 case IMAGEX_REF_OPTION:
2179 case IMAGEX_UNIX_DATA_OPTION:
2180 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2182 case IMAGEX_NO_ACLS_OPTION:
2183 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2185 case IMAGEX_STRICT_ACLS_OPTION:
2186 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2188 case IMAGEX_DEST_DIR_OPTION:
2191 case IMAGEX_TO_STDOUT_OPTION:
2192 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2193 imagex_be_quiet = true;
2210 image_num_or_name = argv[1];
2215 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2222 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2226 image = wimlib_resolve_image(wim, image_num_or_name);
2227 ret = verify_image_exists_and_is_single(image,
2231 goto out_wimlib_free;
2234 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2236 &num_additional_swms);
2238 goto out_wimlib_free;
2242 win32_acquire_restore_privileges();
2245 ret = wimlib_extract_files(wim, image, 0, cmds, num_cmds,
2246 additional_swms, num_additional_swms,
2247 imagex_progress_func);
2249 if (!imagex_be_quiet)
2250 tprintf(T("Done extracting files.\n"));
2251 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2252 tfprintf(stderr, T("Note: You can use `"IMAGEX_PROGNAME" dir' to see what "
2253 "files and directories\n"
2254 " are in the WIM image.\n"));
2257 win32_release_restore_privileges();
2259 if (additional_swms) {
2260 for (unsigned i = 0; i < num_additional_swms; i++)
2261 wimlib_free(additional_swms[i]);
2262 free(additional_swms);
2267 free_extract_commands(cmds, num_cmds, dest_dir);
2272 /* Prints information about a WIM file; also can mark an image as bootable,
2273 * change the name of an image, or change the description of an image. */
2275 imagex_info(int argc, tchar **argv)
2280 bool header = false;
2281 bool lookup_table = false;
2283 bool metadata = false;
2284 bool short_header = true;
2285 const tchar *xml_out_file = NULL;
2286 const tchar *wimfile;
2287 const tchar *image_num_or_name = T("all");
2288 const tchar *new_name = NULL;
2289 const tchar *new_desc = NULL;
2294 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2299 for_opt(c, info_options) {
2301 case IMAGEX_BOOT_OPTION:
2304 case IMAGEX_CHECK_OPTION:
2307 case IMAGEX_HEADER_OPTION:
2309 short_header = false;
2311 case IMAGEX_LOOKUP_TABLE_OPTION:
2312 lookup_table = true;
2313 short_header = false;
2315 case IMAGEX_XML_OPTION:
2317 short_header = false;
2319 case IMAGEX_EXTRACT_XML_OPTION:
2320 xml_out_file = optarg;
2321 short_header = false;
2323 case IMAGEX_METADATA_OPTION:
2325 short_header = false;
2335 if (argc == 0 || argc > 4) {
2341 image_num_or_name = argv[1];
2351 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2353 ret = wimlib_open_wim(wimfile, open_flags, &w,
2354 imagex_progress_func);
2358 part_number = wimlib_get_part_number(w, &total_parts);
2360 image = wimlib_resolve_image(w, image_num_or_name);
2361 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
2362 imagex_error(T("The image \"%"TS"\" does not exist"),
2365 imagex_error(T("If you would like to set the boot "
2366 "index to 0, specify image \"0\" with "
2367 "the --boot flag."));
2369 ret = WIMLIB_ERR_INVALID_IMAGE;
2373 num_images = wimlib_get_num_images(w);
2375 if (num_images == 0) {
2377 imagex_error(T("--boot is meaningless on a WIM with no "
2379 ret = WIMLIB_ERR_INVALID_IMAGE;
2384 if (image == WIMLIB_ALL_IMAGES && num_images > 1) {
2386 imagex_error(T("Cannot specify the --boot flag "
2387 "without specifying a specific "
2388 "image in a multi-image WIM"));
2389 ret = WIMLIB_ERR_INVALID_IMAGE;
2393 imagex_error(T("Cannot specify the NEW_NAME "
2394 "without specifying a specific "
2395 "image in a multi-image WIM"));
2396 ret = WIMLIB_ERR_INVALID_IMAGE;
2401 /* Operations that print information are separated from operations that
2402 * recreate the WIM file. */
2403 if (!new_name && !boot) {
2405 /* Read-only operations */
2407 if (image == WIMLIB_NO_IMAGE) {
2408 imagex_error(T("\"%"TS"\" is not a valid image"),
2410 ret = WIMLIB_ERR_INVALID_IMAGE;
2414 if (image == WIMLIB_ALL_IMAGES && short_header)
2415 wimlib_print_wim_information(w);
2418 wimlib_print_header(w);
2421 if (total_parts != 1) {
2422 tprintf(T("Warning: Only showing the lookup table "
2423 "for part %d of a %d-part WIM.\n"),
2424 part_number, total_parts);
2426 wimlib_print_lookup_table(w);
2430 ret = wimlib_extract_xml_data(w, stdout);
2436 fp = tfopen(xml_out_file, T("wb"));
2438 imagex_error_with_errno(T("Failed to open the "
2439 "file \"%"TS"\" for "
2445 ret = wimlib_extract_xml_data(w, fp);
2446 if (fclose(fp) != 0) {
2447 imagex_error(T("Failed to close the file "
2458 wimlib_print_available_images(w, image);
2461 ret = wimlib_print_metadata(w, image);
2467 /* Modification operations */
2468 if (total_parts != 1) {
2469 imagex_error(T("Modifying a split WIM is not supported."));
2473 if (image == WIMLIB_ALL_IMAGES)
2476 if (image == WIMLIB_NO_IMAGE && new_name) {
2477 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
2478 "when using image 0"), new_name);
2484 if (image == wimlib_get_boot_idx(w)) {
2485 tprintf(T("Image %d is already marked as "
2486 "bootable.\n"), image);
2489 tprintf(T("Marking image %d as bootable.\n"),
2491 wimlib_set_boot_idx(w, image);
2495 if (!tstrcmp(wimlib_get_image_name(w, image), new_name))
2497 tprintf(T("Image %d is already named \"%"TS"\".\n"),
2501 tprintf(T("Changing the name of image %d to "
2502 "\"%"TS"\".\n"), image, new_name);
2503 ret = wimlib_set_image_name(w, image, new_name);
2509 const tchar *old_desc;
2510 old_desc = wimlib_get_image_description(w, image);
2511 if (old_desc && !tstrcmp(old_desc, new_desc)) {
2512 tprintf(T("The description of image %d is already "
2513 "\"%"TS"\".\n"), image, new_desc);
2516 tprintf(T("Changing the description of image %d "
2517 "to \"%"TS"\".\n"), image, new_desc);
2518 ret = wimlib_set_image_descripton(w, image,
2525 /* Only call wimlib_overwrite() if something actually needs to
2527 if (boot || new_name || new_desc ||
2528 (check && !wimlib_has_integrity_table(w)))
2532 ret = file_writable(wimfile);
2537 write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2541 ret = wimlib_overwrite(w, write_flags, 1,
2542 imagex_progress_func);
2543 if (ret == WIMLIB_ERR_REOPEN)
2546 tprintf(T("The file \"%"TS"\" was not modified because nothing "
2547 "needed to be done.\n"), wimfile);
2556 /* Join split WIMs into one part WIM */
2558 imagex_join(int argc, tchar **argv)
2561 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2562 int wim_write_flags = 0;
2563 const tchar *output_path;
2565 for_opt(c, join_options) {
2567 case IMAGEX_CHECK_OPTION:
2568 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2569 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2579 imagex_error(T("Must specify one or more split WIM (.swm) "
2583 output_path = argv[0];
2584 return wimlib_join((const tchar * const *)++argv,
2589 imagex_progress_func);
2595 /* Mounts an image using a FUSE mount. */
2597 imagex_mount_rw_or_ro(int argc, tchar **argv)
2600 int mount_flags = 0;
2601 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2602 const tchar *wimfile;
2608 const tchar *swm_glob = NULL;
2609 WIMStruct **additional_swms = NULL;
2610 unsigned num_additional_swms = 0;
2611 const tchar *staging_dir = NULL;
2613 if (!tstrcmp(argv[0], T("mountrw")))
2614 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
2616 for_opt(c, mount_options) {
2618 case IMAGEX_ALLOW_OTHER_OPTION:
2619 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
2621 case IMAGEX_CHECK_OPTION:
2622 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2624 case IMAGEX_DEBUG_OPTION:
2625 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
2627 case IMAGEX_STREAMS_INTERFACE_OPTION:
2628 if (!tstrcasecmp(optarg, T("none")))
2629 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
2630 else if (!tstrcasecmp(optarg, T("xattr")))
2631 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2632 else if (!tstrcasecmp(optarg, T("windows")))
2633 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
2635 imagex_error(T("Unknown stream interface \"%"TS"\""),
2640 case IMAGEX_REF_OPTION:
2643 case IMAGEX_STAGING_DIR_OPTION:
2644 staging_dir = optarg;
2646 case IMAGEX_UNIX_DATA_OPTION:
2647 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
2655 if (argc != 2 && argc != 3)
2660 ret = wimlib_open_wim(wimfile, open_flags, &w,
2661 imagex_progress_func);
2666 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2668 &num_additional_swms);
2675 num_images = wimlib_get_num_images(w);
2676 if (num_images != 1) {
2677 imagex_error(T("The file \"%"TS"\" contains %d images; Please "
2678 "select one."), wimfile, num_images);
2679 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2686 image = wimlib_resolve_image(w, argv[1]);
2688 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
2693 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2694 ret = file_writable(wimfile);
2699 ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
2700 num_additional_swms, staging_dir);
2702 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
2704 image, wimfile, dir);
2709 if (additional_swms) {
2710 for (unsigned i = 0; i < num_additional_swms; i++)
2711 wimlib_free(additional_swms[i]);
2712 free(additional_swms);
2716 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2721 /* Rebuild a WIM file */
2723 imagex_optimize(int argc, tchar **argv)
2727 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
2730 const tchar *wimfile;
2733 unsigned num_threads = 0;
2735 for_opt(c, optimize_options) {
2737 case IMAGEX_CHECK_OPTION:
2738 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2739 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2741 case IMAGEX_RECOMPRESS_OPTION:
2742 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
2744 case IMAGEX_THREADS_OPTION:
2745 num_threads = parse_num_threads(optarg);
2746 if (num_threads == UINT_MAX)
2764 ret = wimlib_open_wim(wimfile, open_flags, &w,
2765 imagex_progress_func);
2769 old_size = file_get_size(argv[0]);
2770 tprintf(T("\"%"TS"\" original size: "), wimfile);
2772 tfputs(T("Unknown\n"), stdout);
2774 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
2776 ret = wimlib_overwrite(w, write_flags, num_threads,
2777 imagex_progress_func);
2780 new_size = file_get_size(argv[0]);
2781 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
2783 tfputs(T("Unknown\n"), stdout);
2785 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
2787 tfputs(T("Space saved: "), stdout);
2788 if (new_size != -1 && old_size != -1) {
2789 tprintf(T("%lld KiB\n"),
2790 ((long long)old_size - (long long)new_size) >> 10);
2792 tfputs(T("Unknown\n"), stdout);
2800 /* Split a WIM into a spanned set */
2802 imagex_split(int argc, tchar **argv)
2805 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2806 int write_flags = 0;
2807 unsigned long part_size;
2812 for_opt(c, split_options) {
2814 case IMAGEX_CHECK_OPTION:
2815 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2816 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2830 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
2831 if (tmp == argv[2] || *tmp) {
2832 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
2833 imagex_error(T("The part size must be an integer or "
2834 "floating-point number of megabytes."));
2837 ret = wimlib_open_wim(argv[0], open_flags, &w, imagex_progress_func);
2840 ret = wimlib_split(w, argv[1], part_size, write_flags, imagex_progress_func);
2845 /* Unmounts a mounted WIM image. */
2847 imagex_unmount(int argc, tchar **argv)
2850 int unmount_flags = 0;
2853 for_opt(c, unmount_options) {
2855 case IMAGEX_COMMIT_OPTION:
2856 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
2858 case IMAGEX_CHECK_OPTION:
2859 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
2861 case IMAGEX_REBUILD_OPTION:
2862 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
2876 ret = wimlib_unmount_image(argv[0], unmount_flags,
2877 imagex_progress_func);
2879 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
2884 * Add, delete, or rename files in a WIM image.
2887 imagex_update(int argc, tchar **argv)
2889 const tchar *wimfile;
2890 const tchar *image_num_or_name;
2895 int write_flags = WIMLIB_WRITE_FLAG_SOFT_DELETE;
2896 int update_flags = 0;
2897 int default_add_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
2898 int default_delete_flags = 0;
2899 unsigned num_threads = 0;
2901 tchar *cmd_file_contents;
2902 size_t cmd_file_nchars;
2903 struct wimlib_update_command *cmds;
2906 const tchar *config_file = NULL;
2908 struct wimlib_capture_config *config = NULL;
2910 for_opt(c, update_options) {
2912 case IMAGEX_THREADS_OPTION:
2913 num_threads = parse_num_threads(optarg);
2914 if (num_threads == UINT_MAX) {
2919 case IMAGEX_CHECK_OPTION:
2920 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2921 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2923 case IMAGEX_REBUILD_OPTION:
2924 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2926 case IMAGEX_FORCE_OPTION:
2927 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
2929 case IMAGEX_RECURSIVE_OPTION:
2930 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
2932 case IMAGEX_CONFIG_OPTION:
2933 config_file = optarg;
2935 case IMAGEX_DEREFERENCE_OPTION:
2936 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
2938 case IMAGEX_UNIX_DATA_OPTION:
2939 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
2941 case IMAGEX_NO_ACLS_OPTION:
2942 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
2944 case IMAGEX_STRICT_ACLS_OPTION:
2945 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
2954 if (argc < 1 || argc > 2)
2957 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2962 image_num_or_name = argv[1];
2964 image_num_or_name = T("1");
2966 image = wimlib_resolve_image(wim, image_num_or_name);
2968 ret = verify_image_exists_and_is_single(image, image_num_or_name,
2971 goto out_wimlib_free;
2973 /* Parse capture configuration file if specified */
2977 config_str = file_get_text_contents(config_file, &config_len);
2980 goto out_wimlib_free;
2983 config = alloca(sizeof(*config));
2984 ret = parse_capture_config(&config_str, config_len, config);
2986 goto out_free_config;
2988 config = &default_capture_config;
2991 /* Read update commands from standard input */
2992 if (isatty(STDIN_FILENO))
2993 tputs(T("Reading update commands from standard input..."));
2994 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
2995 if (!cmd_file_contents) {
2997 goto out_free_config;
3000 /* Parse the update commands */
3001 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3005 goto out_free_cmd_file_contents;
3008 /* Set default flags and capture config on the update commands */
3009 for (size_t i = 0; i < num_cmds; i++) {
3010 switch (cmds[i].op) {
3011 case WIMLIB_UPDATE_OP_ADD:
3012 cmds[i].add.add_flags |= default_add_flags;
3013 cmds[i].add.config = config;
3015 case WIMLIB_UPDATE_OP_DELETE:
3016 cmds[i].delete.delete_flags |= default_delete_flags;
3023 /* Execute the update commands */
3024 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3025 imagex_progress_func);
3029 /* Overwrite the updated WIM */
3030 ret = wimlib_overwrite(wim, write_flags, num_threads,
3031 imagex_progress_func);
3034 out_free_cmd_file_contents:
3035 free(cmd_file_contents);
3037 if (config != NULL && config != &default_capture_config) {
3038 free(config->exclusion_pats.pats);
3039 free(config->exclusion_exception_pats.pats);
3052 struct imagex_command {
3054 int (*func)(int , tchar **);
3059 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
3060 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
3062 static const struct imagex_command imagex_commands[] = {
3063 {T("append"), imagex_capture_or_append, APPEND},
3064 {T("apply"), imagex_apply, APPLY},
3065 {T("capture"), imagex_capture_or_append, CAPTURE},
3066 {T("delete"), imagex_delete, DELETE},
3067 {T("dir"), imagex_dir, DIR},
3068 {T("export"), imagex_export, EXPORT},
3069 {T("extract"), imagex_extract, EXTRACT},
3070 {T("info"), imagex_info, INFO},
3071 {T("join"), imagex_join, JOIN},
3072 {T("mount"), imagex_mount_rw_or_ro, MOUNT},
3073 {T("mountrw"), imagex_mount_rw_or_ro, MOUNTRW},
3074 {T("optimize"),imagex_optimize, OPTIMIZE},
3075 {T("split"), imagex_split, SPLIT},
3076 {T("unmount"), imagex_unmount, UNMOUNT},
3077 {T("update"), imagex_update, UPDATE},
3083 static const tchar *s =
3085 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
3086 "Copyright (C) 2012, 2013 Eric Biggers\n"
3087 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
3088 "This is free software: you are free to change and redistribute it.\n"
3089 "There is NO WARRANTY, to the extent permitted by law.\n"
3091 "Report bugs to "PACKAGE_BUGREPORT".\n"
3098 help_or_version(int argc, tchar **argv)
3102 const struct imagex_command *cmd;
3104 for (i = 1; i < argc; i++) {
3112 if (!tstrcmp(p, T("help"))) {
3113 for_imagex_command(cmd) {
3114 if (!tstrcmp(cmd->name, argv[1])) {
3122 if (!tstrcmp(p, T("version"))) {
3133 const struct imagex_command *cmd;
3134 tprintf(T("Usage:\n%"TS), usage_strings[cmd_type]);
3135 for_imagex_command(cmd) {
3136 if (cmd->cmd == cmd_type) {
3137 tprintf(T("\nTry `man "IMAGEX_PROGNAME"-%"TS"' "
3138 "for more details.\n"), cmd->name);
3146 tfputs(T("Usage:\n"), stdout);
3147 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
3148 tprintf(T(" %"TS), usage_strings[i]);
3149 static const tchar *extra =
3151 " "IMAGEX_PROGNAME" --help\n"
3152 " "IMAGEX_PROGNAME" --version\n"
3154 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
3156 " Try `man "IMAGEX_PROGNAME"' for more information.\n"
3158 tfputs(extra, stdout);
3161 /* Entry point for wimlib's ImageX implementation. On UNIX the command
3162 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
3163 * something else), while an Windows the command arguments will be UTF-16LE
3164 * encoded 'wchar_t' strings. */
3167 wmain(int argc, wchar_t **argv, wchar_t **envp)
3169 main(int argc, char **argv)
3172 const struct imagex_command *cmd;
3177 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
3178 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
3182 setlocale(LC_ALL, "");
3183 codeset = nl_langinfo(CODESET);
3184 if (!strstr(codeset, "UTF-8") &&
3185 !strstr(codeset, "UTF8") &&
3186 !strstr(codeset, "utf-8") &&
3187 !strstr(codeset, "utf8"))
3190 "WARNING: Running "IMAGEX_PROGNAME" in a UTF-8 locale is recommended!\n"
3191 " Maybe try: `export LANG=en_US.UTF-8'?\n"
3192 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
3193 " to any value to force wimlib to use UTF-8.\n",
3198 #endif /* !__WIN32__ */
3201 imagex_error(T("No command specified"));
3207 /* Handle --help and --version for all commands. Note that this will
3208 * not return if either of these arguments are present. */
3209 help_or_version(argc, argv);
3213 /* The user may like to see more informative error messages. */
3214 wimlib_set_print_errors(true);
3216 /* Do any initializations that the library needs */
3217 ret = wimlib_global_init(init_flags);
3219 goto out_check_status;
3221 /* Search for the function to handle the ImageX subcommand. */
3222 for_imagex_command(cmd) {
3223 if (!tstrcmp(cmd->name, *argv)) {
3224 ret = cmd->func(argc, argv);
3225 goto out_check_write_error;
3229 imagex_error(T("Unrecognized command: `%"TS"'"), argv[0]);
3233 out_check_write_error:
3234 /* For 'wimlib-imagex info' and 'wimlib-imagex dir', data printed to
3235 * standard output is part of the program's actual behavior and not just
3236 * for informational purposes, so we should set a failure exit status if
3237 * there was a write error. */
3238 if (cmd == &imagex_commands[INFO] || cmd == &imagex_commands[DIR]) {
3239 if (ferror(stdout) || fclose(stdout)) {
3240 imagex_error_with_errno(T("error writing to standard output"));
3246 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
3247 * outside of the wimlib library code. 0 indicates success. > 0
3248 * indicates a wimlib error code from which an error message can be
3251 imagex_error(T("Exiting with error code %d:\n"
3253 wimlib_get_error_string(ret));
3254 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
3255 imagex_error_with_errno(T("errno"));
3258 /* Make the library free any resources it's holding (not strictly
3259 * necessary because the process is ending anyway). */
3260 wimlib_global_cleanup();