4 * Use wimlib to create, modify, extract, mount, unmount, or display information
9 * Copyright (C) 2012, 2013 Eric Biggers
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 # include "config.h" /* Need for PACKAGE_VERSION, etc. */
30 #include "wimlib_tchar.h"
50 # include "imagex-win32.h"
51 # define tbasename win32_wbasename
52 # define tglob win32_wglob
53 # define OS_PREFERRED_PATH_SEPARATOR L'\\'
54 # define OS_PREFERRED_PATH_SEPARATOR_STRING L"\\"
58 # include <langinfo.h>
59 # define tbasename basename
61 # define OS_PREFERRED_PATH_SEPARATOR '/'
62 # define OS_PREFERRED_PATH_SEPARATOR_STRING "/"
66 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
68 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
89 static void usage(int cmd_type);
90 static void usage_all(void);
92 static bool imagex_be_quiet = false;
95 static const tchar *usage_strings[] = {
98 IMAGEX_PROGNAME" append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
99 " [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
100 " [--verbose] [--dereference] [--config=FILE]\n"
101 " [--threads=NUM_THREADS] [--rebuild] [--unix-data]\n"
102 " [--source-list] [--no-acls] [--strict-acls]\n"
103 " [--rpfix] [--norpfix]\n"
107 IMAGEX_PROGNAME" apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
108 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
109 " [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n"
110 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
111 " [--include-invalid-names]\n"
115 IMAGEX_PROGNAME" capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
116 " [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
117 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
118 " [--config=FILE] [--threads=NUM_THREADS] [--unix-data]\n"
119 " [--source-list] [--no-acls] [--strict-acls]\n"
120 " [--rpfix] [--norpfix]\n"
124 IMAGEX_PROGNAME" delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check] [--soft]\n"
128 IMAGEX_PROGNAME" dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH]\n"
132 IMAGEX_PROGNAME" export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
133 " DEST_WIMFILE [DEST_IMAGE_NAME] [DEST_IMAGE_DESCRIPTION]\n"
134 " [--boot] [--check] [--compress=TYPE] [--ref=\"GLOB\"]\n"
135 " [--threads=NUM_THREADS] [--rebuild]\n"
139 IMAGEX_PROGNAME" extract WIMFILE (IMAGE_NUM | IMAGE_NAME) [PATH...]\n"
140 " [--check] [--ref=\"GLOB\"] [--verbose] [--unix-data]\n"
141 " [--no-acls] [--strict-acls] [--to-stdout] [--dest-dir=DIR]\n"
142 " [--include-invalid-names]\n"
146 IMAGEX_PROGNAME" info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
147 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
148 " [--xml] [--extract-xml FILE] [--metadata]\n"
152 IMAGEX_PROGNAME" join [--check] WIMFILE SPLIT_WIM...\n"
156 IMAGEX_PROGNAME" mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
157 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
158 " [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n"
162 IMAGEX_PROGNAME" mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
163 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
164 " [--staging-dir=DIR] [--unix-data] [--allow-other]\n"
168 IMAGEX_PROGNAME" optimize WIMFILE [--check] [--recompress]\n"
169 " [--threads=NUM_THREADS]\n"
173 IMAGEX_PROGNAME" split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n"
177 IMAGEX_PROGNAME" unmount DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
181 IMAGEX_PROGNAME" update WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
182 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
183 " [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
189 recommend_man_page(const tchar *cmd_name)
191 const tchar *format_str;
193 format_str = T("See "IMAGEX_PROGNAME"%"TS"%"TS".pdf in the "
194 "doc directory for more details.\n");
196 format_str = T("Try `man "IMAGEX_PROGNAME"%"TS"%"TS"' "
197 "for more details.\n");
199 tprintf(format_str, *cmd_name ? T("-") : T(""), cmd_name);
203 IMAGEX_ALLOW_OTHER_OPTION,
206 IMAGEX_COMMAND_OPTION,
207 IMAGEX_COMMIT_OPTION,
208 IMAGEX_COMPRESS_OPTION,
209 IMAGEX_CONFIG_OPTION,
211 IMAGEX_DEREFERENCE_OPTION,
212 IMAGEX_DEST_DIR_OPTION,
213 IMAGEX_EXTRACT_XML_OPTION,
216 IMAGEX_HARDLINK_OPTION,
217 IMAGEX_HEADER_OPTION,
218 IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
220 IMAGEX_LOOKUP_TABLE_OPTION,
221 IMAGEX_METADATA_OPTION,
222 IMAGEX_NORPFIX_OPTION,
223 IMAGEX_NO_ACLS_OPTION,
225 IMAGEX_REBUILD_OPTION,
226 IMAGEX_RECOMPRESS_OPTION,
227 IMAGEX_RECURSIVE_OPTION,
231 IMAGEX_SOURCE_LIST_OPTION,
232 IMAGEX_STAGING_DIR_OPTION,
233 IMAGEX_STREAMS_INTERFACE_OPTION,
234 IMAGEX_STRICT_ACLS_OPTION,
235 IMAGEX_SYMLINK_OPTION,
236 IMAGEX_THREADS_OPTION,
237 IMAGEX_TO_STDOUT_OPTION,
238 IMAGEX_UNIX_DATA_OPTION,
239 IMAGEX_VERBOSE_OPTION,
243 static const struct option apply_options[] = {
244 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
245 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
246 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
247 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
248 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
249 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
250 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
251 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
252 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
253 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
254 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
255 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
258 static const struct option capture_or_append_options[] = {
259 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
260 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
261 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
262 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
263 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
264 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
265 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
266 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
267 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
268 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
269 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
270 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
271 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
272 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
273 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
274 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
277 static const struct option delete_options[] = {
278 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
279 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
283 static const struct option dir_options[] = {
284 {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
288 static const struct option export_options[] = {
289 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
290 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
291 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
292 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
293 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
294 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
298 static const struct option extract_options[] = {
299 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
300 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
301 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
302 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
303 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
304 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
305 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
306 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
307 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
308 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
312 static const struct option info_options[] = {
313 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
314 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
315 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
316 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
317 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
318 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
319 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
323 static const struct option join_options[] = {
324 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
328 static const struct option mount_options[] = {
329 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
330 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
331 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
332 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
333 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
334 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
335 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
339 static const struct option optimize_options[] = {
340 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
341 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
342 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
346 static const struct option split_options[] = {
347 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
351 static const struct option unmount_options[] = {
352 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
353 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
354 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
355 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
359 static const struct option update_options[] = {
360 /* Careful: some of the options here set the defaults for update
361 * commands, but the flags given to an actual update command (and not to
362 * `imagex update' itself are also handled in
363 * update_command_add_option(). */
364 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
365 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
366 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
367 {T("command"), required_argument, NULL, IMAGEX_COMMAND_OPTION},
369 /* Default delete options */
370 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
371 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
373 /* Global add option */
374 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
376 /* Default add options */
377 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
378 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
379 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
380 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
381 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
382 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
388 # define _format_attribute(type, format_str, args_start) \
389 __attribute__((format(type, format_str, args_start)))
391 # define _format_attribute(type, format_str, args_start)
394 /* Print formatted error message to stderr. */
395 static void _format_attribute(printf, 1, 2)
396 imagex_error(const tchar *format, ...)
399 va_start(va, format);
400 tfputs(T("ERROR: "), stderr);
401 tvfprintf(stderr, format, va);
402 tputc(T('\n'), stderr);
406 /* Print formatted error message to stderr. */
407 static void _format_attribute(printf, 1, 2)
408 imagex_error_with_errno(const tchar *format, ...)
410 int errno_save = errno;
412 va_start(va, format);
413 tfputs(T("ERROR: "), stderr);
414 tvfprintf(stderr, format, va);
415 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
420 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
422 if (image == WIMLIB_NO_IMAGE) {
423 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
424 " Please specify a 1-based image index or "
426 " You may use `"IMAGEX_PROGNAME" info' to list the images "
427 "contained in a WIM."),
428 image_name, wim_name);
435 verify_image_is_single(int image)
437 if (image == WIMLIB_ALL_IMAGES) {
438 imagex_error(T("Cannot specify all images for this action!"));
445 verify_image_exists_and_is_single(int image, const tchar *image_name,
446 const tchar *wim_name)
449 ret = verify_image_exists(image, image_name, wim_name);
451 ret = verify_image_is_single(image);
455 /* Parse the argument to --compress */
457 get_compression_type(const tchar *optarg)
459 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
460 return WIMLIB_COMPRESSION_TYPE_LZX;
461 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
462 return WIMLIB_COMPRESSION_TYPE_XPRESS;
463 else if (!tstrcasecmp(optarg, T("none")))
464 return WIMLIB_COMPRESSION_TYPE_NONE;
466 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
467 "\"maximum\", \"fast\", or \"none\"."), optarg);
468 return WIMLIB_COMPRESSION_TYPE_INVALID;
472 /* Returns the size of a file given its name, or -1 if the file does not exist
473 * or its size cannot be determined. */
475 file_get_size(const tchar *filename)
478 if (tstat(filename, &st) == 0)
484 tchar pat_ntfs_log[] = T("/$ntfs.log");
485 tchar pat_hiberfil_sys[] = T("/hiberfil.sys");
486 tchar pat_pagefile_sys[] = T("/pagefile.sys");
487 tchar pat_system_volume_information[] = T("/System Volume Information");
488 tchar pat_recycler[] = T("/RECYCLER");
489 tchar pat_windows_csc[] = T("/Windows/CSC");
491 tchar *default_pats[] = {
495 pat_system_volume_information,
500 static struct wimlib_capture_config default_capture_config = {
502 .num_pats = sizeof(default_pats) / sizeof(default_pats[0]),
503 .pats = default_pats,
508 PARSE_STRING_SUCCESS = 0,
509 PARSE_STRING_FAILURE = 1,
510 PARSE_STRING_NONE = 2,
514 * Parses a string token from an array of characters.
516 * Tokens are either whitespace-delimited, or double or single-quoted.
518 * @line_p: Pointer to the pointer to the line of data. Will be updated
519 * to point past the string token iff the return value is
520 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
523 * @len_p: @len_p initially stores the length of the line of data, which may
524 * be 0, and it will be updated to the number of bytes remaining in
525 * the line iff the return value is PARSE_STRING_SUCCESS.
527 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
528 * parsed string token will be returned here.
530 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
531 * PARSE_STRING_FAILURE if the data was invalid due to a missing
532 * closing quote; or PARSE_STRING_NONE if the line ended before the
533 * beginning of a string token was found.
536 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
539 tchar *line = *line_p;
543 /* Skip leading whitespace */
546 return PARSE_STRING_NONE;
547 if (!istspace(*line) && *line != T('\0'))
553 if (quote_char == T('"') || quote_char == T('\'')) {
558 line = tmemchr(line, quote_char, len);
560 imagex_error(T("Missing closing quote: %"TS), fn - 1);
561 return PARSE_STRING_FAILURE;
564 /* Unquoted string. Go until whitespace. Line is terminated
565 * by '\0', so no need to check 'len'. */
569 } while (!istspace(*line) && *line != T('\0'));
576 return PARSE_STRING_SUCCESS;
579 /* Parses a line of data (not an empty line or comment) in the source list file
580 * format. (See the man page for 'wimlib-imagex capture' for details on this
581 * format and the meaning.)
583 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
584 * len == 0. The data in @line will be modified by this function call.
586 * @len: Length of the line of data.
588 * @source: On success, the capture source and target described by the line is
589 * written into this destination. Note that it will contain pointers
590 * to data in the @line array.
592 * Returns true if the line was valid; false otherwise. */
594 parse_source_list_line(tchar *line, size_t len,
595 struct wimlib_capture_source *source)
599 ret = parse_string(&line, &len, &source->fs_source_path);
600 if (ret != PARSE_STRING_SUCCESS)
602 ret = parse_string(&line, &len, &source->wim_target_path);
603 if (ret == PARSE_STRING_NONE)
604 source->wim_target_path = source->fs_source_path;
605 return ret != PARSE_STRING_FAILURE;
608 /* Returns %true if the given line of length @len > 0 is a comment or empty line
609 * in the source list file format. */
611 is_comment_line(const tchar *line, size_t len)
616 if (!istspace(*line) && *line != T('\0'))
626 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
629 tchar *contents = *contents_p;
630 size_t nchars = *nchars_p;
633 for (i = 0; i < nchars; i++)
634 if (contents[i] == T('\n'))
637 /* Handle last line not terminated by a newline */
638 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
639 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
641 imagex_error(T("Out of memory!"));
644 contents[nchars] = T('\n');
645 *contents_p = contents;
653 /* Parses a file in the source list format. (See the man page for
654 * 'wimlib-imagex capture' for details on this format and the meaning.)
656 * @source_list_contents: Contents of the source list file. Note that this
657 * buffer will be modified to save memory allocations,
658 * and cannot be freed until the returned array of
659 * wimlib_capture_source's has also been freed.
661 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
664 * @nsources_ret: On success, the length of the returned array is
667 * Returns: An array of `struct wimlib_capture_source's that can be passed to
668 * the wimlib_add_image_multisource() function to specify how a WIM image is to
670 static struct wimlib_capture_source *
671 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
672 size_t *nsources_ret)
676 struct wimlib_capture_source *sources;
679 nlines = text_file_count_lines(source_list_contents_p,
680 &source_list_nchars);
684 /* Always allocate at least 1 slot, just in case the implementation of
685 * calloc() returns NULL if 0 bytes are requested. */
686 sources = calloc(nlines ?: 1, sizeof(*sources));
688 imagex_error(T("out of memory"));
691 p = *source_list_contents_p;
693 for (i = 0; i < nlines; i++) {
694 /* XXX: Could use rawmemchr() here instead, but it may not be
695 * available on all platforms. */
696 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
697 size_t len = endp - p + 1;
699 if (!is_comment_line(p, len)) {
700 if (!parse_source_list_line(p, len, &sources[j++])) {
713 enum capture_config_section {
714 CAPTURE_CONFIG_NO_SECTION,
715 CAPTURE_CONFIG_EXCLUSION_SECTION,
716 CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
717 CAPTURE_CONFIG_IGNORE_SECTION,
721 CAPTURE_CONFIG_INVALID_SECTION,
722 CAPTURE_CONFIG_CHANGED_SECTION,
723 CAPTURE_CONFIG_SAME_SECTION,
727 check_config_section(tchar *line, size_t len,
728 enum capture_config_section *cur_section)
730 while (istspace(*line))
734 return CAPTURE_CONFIG_SAME_SECTION;
737 tchar *endbrace = tstrrchr(line, T(']'));
739 return CAPTURE_CONFIG_SAME_SECTION;
741 if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
742 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
743 } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
744 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
745 } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
746 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
747 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
748 "of capture config file\n"),
750 } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
751 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
752 tfputs(T("WARNING: Ignoring [AlignmentList] section "
753 "of capture config file\n"),
756 imagex_error(T("Invalid capture config file section \"%"TS"\""),
758 return CAPTURE_CONFIG_INVALID_SECTION;
760 return CAPTURE_CONFIG_CHANGED_SECTION;
765 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
768 if (pat_list->num_pats == pat_list->num_allocated_pats) {
770 size_t num_allocated_pats = pat_list->num_pats + 8;
772 pats = realloc(pat_list->pats,
773 num_allocated_pats * sizeof(pat_list->pats[0]));
775 imagex_error(T("Out of memory!"));
778 pat_list->pats = pats;
779 pat_list->num_allocated_pats = num_allocated_pats;
781 pat_list->pats[pat_list->num_pats++] = pat;
786 parse_capture_config_line(tchar *line, size_t len,
787 enum capture_config_section *cur_section,
788 struct wimlib_capture_config *config)
793 ret = check_config_section(line, len, cur_section);
794 if (ret == CAPTURE_CONFIG_INVALID_SECTION)
796 if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
799 switch (*cur_section) {
800 case CAPTURE_CONFIG_NO_SECTION:
801 imagex_error(T("Line \"%"TS"\" is not in a section "
802 "(such as [ExclusionList]"), line);
804 case CAPTURE_CONFIG_EXCLUSION_SECTION:
805 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
807 return pattern_list_add_pattern(&config->exclusion_pats,
809 case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
810 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
812 return pattern_list_add_pattern(&config->exclusion_exception_pats,
814 case CAPTURE_CONFIG_IGNORE_SECTION:
821 parse_capture_config(tchar **contents_p, size_t nchars,
822 struct wimlib_capture_config *config)
827 enum capture_config_section cur_section;
829 memset(config, 0, sizeof(*config));
831 nlines = text_file_count_lines(contents_p, &nchars);
835 cur_section = CAPTURE_CONFIG_NO_SECTION;
837 for (i = 0; i < nlines; i++) {
838 tchar *endp = tmemchr(p, T('\n'), nchars);
839 size_t len = endp - p + 1;
841 if (!is_comment_line(p, len))
842 if (!parse_capture_config_line(p, len, &cur_section, config))
850 /* Reads the contents of a file into memory. */
852 file_get_contents(const tchar *filename, size_t *len_ret)
859 if (tstat(filename, &stbuf) != 0) {
860 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
865 fp = tfopen(filename, T("rb"));
867 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
871 buf = malloc(len ? len : 1);
873 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
874 "contents of file \"%"TS"\""), len, filename);
877 if (fread(buf, 1, len, fp) != len) {
878 imagex_error_with_errno(T("Failed to read %zu bytes from the "
879 "file \"%"TS"\""), len, filename);
893 /* Read standard input until EOF and return the full contents in a malloc()ed
894 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
897 stdin_get_contents(size_t *len_ret)
899 /* stdin can, of course, be a pipe or other non-seekable file, so the
900 * total length of the data cannot be pre-determined */
902 size_t newlen = 1024;
906 char *p = realloc(buf, newlen);
907 size_t bytes_read, bytes_to_read;
909 imagex_error(T("out of memory while reading stdin"));
913 bytes_to_read = newlen - pos;
914 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
916 if (bytes_read != bytes_to_read) {
921 imagex_error_with_errno(T("error reading stdin"));
935 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
938 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
940 *num_tchars_ret = num_bytes;
942 #else /* !__WIN32__ */
943 /* On Windows, translate the text to UTF-16LE */
947 if (num_bytes >= 2 &&
948 ((text[0] == 0xff && text[1] == 0xfe) ||
949 (text[0] <= 0x7f && text[1] == 0x00)))
951 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
952 * with something that looks like an ASCII character encoded as
953 * a UTF-16LE code unit. Assume the file is encoded as
954 * UTF-16LE. This is not a 100% reliable check. */
955 num_wchars = num_bytes / 2;
956 text_wstr = (wchar_t*)text;
958 /* File does not look like UTF-16LE. Assume it is encoded in
959 * the current Windows code page. I think these are always
960 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
961 * should work as expected. */
962 text_wstr = win32_mbs_to_wcs(text,
967 *num_tchars_ret = num_wchars;
969 #endif /* __WIN32__ */
973 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
978 contents = file_get_contents(filename, &num_bytes);
981 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
985 stdin_get_text_contents(size_t *num_tchars_ret)
990 contents = stdin_get_contents(&num_bytes);
993 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
996 #define TO_PERCENT(numerator, denominator) \
997 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
999 /* Given an enumerated value for WIM compression type, return a descriptive
1001 static const tchar *
1002 get_data_type(int ctype)
1005 case WIMLIB_COMPRESSION_TYPE_NONE:
1006 return T("uncompressed");
1007 case WIMLIB_COMPRESSION_TYPE_LZX:
1008 return T("LZX-compressed");
1009 case WIMLIB_COMPRESSION_TYPE_XPRESS:
1010 return T("XPRESS-compressed");
1015 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
1016 #define MEBIBYTE_MIN_NBYTES 10000000ULL
1017 #define KIBIBYTE_MIN_NBYTES 10000ULL
1020 get_unit(uint64_t total_bytes, const tchar **name_ret)
1022 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
1023 *name_ret = T("GiB");
1025 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
1026 *name_ret = T("MiB");
1028 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
1029 *name_ret = T("KiB");
1032 *name_ret = T("bytes");
1037 /* Progress callback function passed to various wimlib functions. */
1039 imagex_progress_func(enum wimlib_progress_msg msg,
1040 const union wimlib_progress_info *info)
1042 unsigned percent_done;
1043 unsigned unit_shift;
1044 const tchar *unit_name;
1045 if (imagex_be_quiet)
1048 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1049 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1050 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1051 info->write_streams.total_bytes);
1052 if (info->write_streams.completed_streams == 0) {
1053 const tchar *data_type;
1055 data_type = get_data_type(info->write_streams.compression_type);
1056 tprintf(T("Writing %"TS" data using %u thread%"TS"\n"),
1057 data_type, info->write_streams.num_threads,
1058 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1060 tprintf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1061 "written (%u%% done)"),
1062 info->write_streams.completed_bytes >> unit_shift,
1064 info->write_streams.total_bytes >> unit_shift,
1067 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1070 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1071 tprintf(T("Scanning \"%"TS"\""), info->scan.source);
1072 if (*info->scan.wim_target_path) {
1073 tprintf(T(" (loading as WIM path: "
1074 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
1075 info->scan.wim_target_path);
1077 tprintf(T(" (loading as root of WIM image)...\n"));
1080 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1081 if (info->scan.excluded)
1082 tprintf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1084 tprintf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
1086 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
1088 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1089 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1090 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1091 info->integrity.total_bytes);
1092 tprintf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1093 "of %"PRIu64" %"TS" (%u%%) done"),
1094 info->integrity.filename,
1095 info->integrity.completed_bytes >> unit_shift,
1097 info->integrity.total_bytes >> unit_shift,
1100 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1103 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1104 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1105 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1106 info->integrity.total_bytes);
1107 tprintf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1108 "of %"PRIu64" %"TS" (%u%%) done"),
1109 info->integrity.completed_bytes >> unit_shift,
1111 info->integrity.total_bytes >> unit_shift,
1114 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1117 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1118 tprintf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1119 "to %"TS" \"%"TS"\"\n"),
1120 info->extract.image,
1121 info->extract.image_name,
1122 info->extract.wimfile_name,
1123 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1124 T("NTFS volume") : T("directory")),
1125 info->extract.target);
1127 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1128 tprintf(T("Extracting "
1129 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\" from image %d (\"%"TS"\") "
1130 "in \"%"TS"\" to \"%"TS"\"\n"),
1131 info->extract.extract_root_wim_source_path,
1132 info->extract.image,
1133 info->extract.image_name,
1134 info->extract.wimfile_name,
1135 info->extract.target);
1137 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
1138 /*tprintf(T("Applying directory structure to %"TS"\n"),*/
1139 /*info->extract.target);*/
1141 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1142 percent_done = TO_PERCENT(info->extract.completed_bytes,
1143 info->extract.total_bytes);
1144 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1145 tprintf(T("\rExtracting files: "
1146 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1147 info->extract.completed_bytes >> unit_shift,
1149 info->extract.total_bytes >> unit_shift,
1152 if (info->extract.completed_bytes >= info->extract.total_bytes)
1155 case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
1156 tprintf(T("%"TS"\n"), info->extract.cur_path);
1158 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1159 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1160 tprintf(T("Setting timestamps on all extracted files...\n"));
1162 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1163 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1164 tprintf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1165 info->extract.target);
1168 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
1169 percent_done = TO_PERCENT(info->join.completed_bytes,
1170 info->join.total_bytes);
1171 unit_shift = get_unit(info->join.total_bytes, &unit_name);
1172 tprintf(T("Writing resources from part %u of %u: "
1173 "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written\n"),
1174 (info->join.completed_parts == info->join.total_parts) ?
1175 info->join.completed_parts : info->join.completed_parts + 1,
1176 info->join.total_parts,
1177 info->join.completed_bytes >> unit_shift,
1179 info->join.total_bytes >> unit_shift,
1183 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1184 percent_done = TO_PERCENT(info->split.completed_bytes,
1185 info->split.total_bytes);
1186 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1187 tprintf(T("Writing \"%"TS"\": %"PRIu64" %"TS" of "
1188 "%"PRIu64" %"TS" (%u%%) written\n"),
1189 info->split.part_name,
1190 info->split.completed_bytes >> unit_shift,
1192 info->split.total_bytes >> unit_shift,
1196 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1197 if (info->split.completed_bytes == info->split.total_bytes) {
1198 tprintf(T("Finished writing %u split WIM parts\n"),
1199 info->split.cur_part_number);
1202 case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1203 switch (info->update.command->op) {
1204 case WIMLIB_UPDATE_OP_DELETE:
1205 tprintf(T("Deleted WIM path "
1206 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1207 info->update.command->delete.wim_path);
1209 case WIMLIB_UPDATE_OP_RENAME:
1210 tprintf(T("Renamed WIM path "
1211 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
1212 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1213 info->update.command->rename.wim_source_path,
1214 info->update.command->rename.wim_target_path);
1216 case WIMLIB_UPDATE_OP_ADD:
1228 /* Open all the split WIM parts that correspond to a file glob.
1230 * @first_part specifies the first part of the split WIM and it may be either
1231 * included or omitted from the glob. */
1233 open_swms_from_glob(const tchar *swm_glob,
1234 const tchar *first_part,
1236 WIMStruct ***additional_swms_ret,
1237 unsigned *num_additional_swms_ret)
1239 unsigned num_additional_swms = 0;
1240 WIMStruct **additional_swms = NULL;
1244 /* Warning: glob() is replaced in Windows native builds */
1245 ret = tglob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
1247 if (ret == GLOB_NOMATCH) {
1248 imagex_error(T("Found no files for glob \"%"TS"\""),
1251 imagex_error_with_errno(T("Failed to process glob \"%"TS"\""),
1257 num_additional_swms = globbuf.gl_pathc;
1258 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
1259 if (!additional_swms) {
1260 imagex_error(T("Out of memory"));
1264 unsigned offset = 0;
1265 for (unsigned i = 0; i < num_additional_swms; i++) {
1266 if (tstrcmp(globbuf.gl_pathv[i], first_part) == 0) {
1270 ret = wimlib_open_wim(globbuf.gl_pathv[i],
1271 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
1272 &additional_swms[i - offset],
1273 imagex_progress_func);
1275 goto out_close_swms;
1277 *additional_swms_ret = additional_swms;
1278 *num_additional_swms_ret = num_additional_swms - offset;
1282 for (unsigned i = 0; i < num_additional_swms; i++)
1283 wimlib_free(additional_swms[i]);
1284 free(additional_swms);
1293 parse_num_threads(const tchar *optarg)
1296 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1297 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1298 imagex_error(T("Number of threads must be a non-negative integer!"));
1306 * Parse an option passed to an update command.
1308 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1311 * @option: Text string for the option (beginning with --)
1313 * @cmd: `struct wimlib_update_command' that is being constructed for
1316 * Returns true if the option was recognized; false if not.
1319 update_command_add_option(int op, const tchar *option,
1320 struct wimlib_update_command *cmd)
1322 bool recognized = true;
1324 case WIMLIB_UPDATE_OP_ADD:
1325 if (!tstrcmp(option, T("--verbose")))
1326 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1327 else if (!tstrcmp(option, T("--unix-data")))
1328 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1329 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1330 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1331 else if (!tstrcmp(option, T("--strict-acls")))
1332 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1333 else if (!tstrcmp(option, T("--dereference")))
1334 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1338 case WIMLIB_UPDATE_OP_DELETE:
1339 if (!tstrcmp(option, T("--force")))
1340 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1341 else if (!tstrcmp(option, T("--recursive")))
1342 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1353 /* How many nonoption arguments each `imagex update' command expects */
1354 static const unsigned update_command_num_nonoptions[] = {
1355 [WIMLIB_UPDATE_OP_ADD] = 2,
1356 [WIMLIB_UPDATE_OP_DELETE] = 1,
1357 [WIMLIB_UPDATE_OP_RENAME] = 2,
1361 update_command_add_nonoption(int op, const tchar *nonoption,
1362 struct wimlib_update_command *cmd,
1363 unsigned num_nonoptions)
1366 case WIMLIB_UPDATE_OP_ADD:
1367 if (num_nonoptions == 0)
1368 cmd->add.fs_source_path = (tchar*)nonoption;
1370 cmd->add.wim_target_path = (tchar*)nonoption;
1372 case WIMLIB_UPDATE_OP_DELETE:
1373 cmd->delete.wim_path = (tchar*)nonoption;
1375 case WIMLIB_UPDATE_OP_RENAME:
1376 if (num_nonoptions == 0)
1377 cmd->rename.wim_source_path = (tchar*)nonoption;
1379 cmd->rename.wim_target_path = (tchar*)nonoption;
1385 * Parse a command passed on stdin to `imagex update'.
1387 * @line: Text of the command.
1388 * @len: Length of the line, including a null terminator
1391 * @command: A `struct wimlib_update_command' to fill in from the parsed
1394 * @line_number: Line number of the command, for diagnostics.
1396 * Returns true on success; returns false on parse error.
1399 parse_update_command(tchar *line, size_t len,
1400 struct wimlib_update_command *command,
1404 tchar *command_name;
1406 size_t num_nonoptions;
1408 /* Get the command name ("add", "delete", "rename") */
1409 ret = parse_string(&line, &len, &command_name);
1410 if (ret != PARSE_STRING_SUCCESS)
1413 if (!tstrcasecmp(command_name, T("add"))) {
1414 op = WIMLIB_UPDATE_OP_ADD;
1415 } else if (!tstrcasecmp(command_name, T("delete"))) {
1416 op = WIMLIB_UPDATE_OP_DELETE;
1417 } else if (!tstrcasecmp(command_name, T("rename"))) {
1418 op = WIMLIB_UPDATE_OP_RENAME;
1420 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1421 command_name, line_number);
1426 /* Parse additional options and non-options as needed */
1431 ret = parse_string(&line, &len, &next_string);
1432 if (ret == PARSE_STRING_NONE) /* End of line */
1434 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1436 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1438 if (!update_command_add_option(op, next_string, command))
1440 imagex_error(T("Unrecognized option \"%"TS"\" to "
1441 "update command \"%"TS"\" on line %zu"),
1442 next_string, command_name, line_number);
1448 if (num_nonoptions == update_command_num_nonoptions[op])
1450 imagex_error(T("Unexpected argument \"%"TS"\" in "
1451 "update command on line %zu\n"
1452 " (The \"%"TS"\" command only "
1453 "takes %zu nonoption arguments!)\n"),
1454 next_string, line_number,
1455 command_name, num_nonoptions);
1458 update_command_add_nonoption(op, next_string,
1459 command, num_nonoptions);
1464 if (num_nonoptions != update_command_num_nonoptions[op]) {
1465 imagex_error(T("Not enough arguments to update command "
1466 "\"%"TS"\" on line %zu"), command_name, line_number);
1472 static struct wimlib_update_command *
1473 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1474 size_t *num_cmds_ret)
1478 struct wimlib_update_command *cmds;
1481 nlines = text_file_count_lines(cmd_file_contents_p,
1486 /* Always allocate at least 1 slot, just in case the implementation of
1487 * calloc() returns NULL if 0 bytes are requested. */
1488 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1490 imagex_error(T("out of memory"));
1493 p = *cmd_file_contents_p;
1495 for (i = 0; i < nlines; i++) {
1496 /* XXX: Could use rawmemchr() here instead, but it may not be
1497 * available on all platforms. */
1498 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1499 size_t len = endp - p + 1;
1501 if (!is_comment_line(p, len)) {
1502 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1513 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1514 * one image from a WIM file to a NTFS volume. */
1516 imagex_apply(int argc, tchar **argv)
1519 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1523 const tchar *wimfile;
1524 const tchar *target;
1525 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1527 const tchar *swm_glob = NULL;
1528 WIMStruct **additional_swms;
1529 unsigned num_additional_swms;
1531 for_opt(c, apply_options) {
1533 case IMAGEX_CHECK_OPTION:
1534 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1536 case IMAGEX_HARDLINK_OPTION:
1537 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1539 case IMAGEX_SYMLINK_OPTION:
1540 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1542 case IMAGEX_VERBOSE_OPTION:
1543 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
1545 case IMAGEX_REF_OPTION:
1548 case IMAGEX_UNIX_DATA_OPTION:
1549 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1551 case IMAGEX_NO_ACLS_OPTION:
1552 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1554 case IMAGEX_STRICT_ACLS_OPTION:
1555 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1557 case IMAGEX_NORPFIX_OPTION:
1558 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1560 case IMAGEX_RPFIX_OPTION:
1561 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1563 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1564 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1565 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1573 if (argc != 2 && argc != 3)
1578 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
1583 /* Image explicitly specified. */
1584 image = wimlib_resolve_image(wim, argv[1]);
1585 ret = verify_image_exists(image, argv[1], wimfile);
1587 goto out_wimlib_free;
1590 /* No image specified; default to image 1, but only if the WIM
1591 * contains exactly one image. */
1592 struct wimlib_wim_info info;
1594 wimlib_get_wim_info(wim, &info);
1595 if (info.image_count != 1) {
1596 imagex_error(T("\"%"TS"\" contains %d images; "
1597 "Please select one (or all)."),
1598 wimfile, info.image_count);
1607 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1609 &num_additional_swms);
1611 goto out_wimlib_free;
1613 additional_swms = NULL;
1614 num_additional_swms = 0;
1620 /* Interpret a regular file or block device target as a NTFS
1624 if (tstat(target, &stbuf)) {
1625 if (errno != ENOENT) {
1626 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1632 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1633 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1639 win32_acquire_restore_privileges();
1641 ret = wimlib_extract_image(wim, image, target, extract_flags,
1642 additional_swms, num_additional_swms,
1643 imagex_progress_func);
1645 tprintf(T("Done applying WIM image.\n"));
1647 win32_release_restore_privileges();
1650 for (unsigned i = 0; i < num_additional_swms; i++)
1651 wimlib_free(additional_swms[i]);
1652 free(additional_swms);
1664 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1665 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1666 * the desired image. 'wimlib-imagex append': add a new image to an existing
1669 imagex_capture_or_append(int argc, tchar **argv)
1672 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1673 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
1674 int write_flags = 0;
1675 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
1676 const tchar *wimfile;
1679 const tchar *flags_element = NULL;
1682 int cmd = tstrcmp(argv[0], T("append")) ? CAPTURE : APPEND;
1683 unsigned num_threads = 0;
1688 const tchar *config_file = NULL;
1690 struct wimlib_capture_config *config;
1692 bool source_list = false;
1693 size_t source_list_nchars;
1694 tchar *source_list_contents;
1695 bool capture_sources_malloced;
1696 struct wimlib_capture_source *capture_sources;
1698 bool name_defaulted;
1700 for_opt(c, capture_or_append_options) {
1702 case IMAGEX_BOOT_OPTION:
1703 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1705 case IMAGEX_CHECK_OPTION:
1706 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1707 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1709 case IMAGEX_CONFIG_OPTION:
1710 config_file = optarg;
1712 case IMAGEX_COMPRESS_OPTION:
1713 compression_type = get_compression_type(optarg);
1714 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1717 case IMAGEX_FLAGS_OPTION:
1718 flags_element = optarg;
1720 case IMAGEX_DEREFERENCE_OPTION:
1721 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1723 case IMAGEX_VERBOSE_OPTION:
1724 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1726 case IMAGEX_THREADS_OPTION:
1727 num_threads = parse_num_threads(optarg);
1728 if (num_threads == UINT_MAX)
1731 case IMAGEX_REBUILD_OPTION:
1732 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1734 case IMAGEX_UNIX_DATA_OPTION:
1735 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1737 case IMAGEX_SOURCE_LIST_OPTION:
1740 case IMAGEX_NO_ACLS_OPTION:
1741 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1743 case IMAGEX_STRICT_ACLS_OPTION:
1744 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1746 case IMAGEX_RPFIX_OPTION:
1747 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1749 case IMAGEX_NORPFIX_OPTION:
1750 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1759 if (argc < 2 || argc > 4)
1767 name_defaulted = false;
1769 /* Set default name to SOURCE argument, omitting any directory
1770 * prefixes and trailing slashes. This requires making a copy
1771 * of @source. Leave some free characters at the end in case we
1772 * append a number to keep the name unique. */
1773 size_t source_name_len;
1775 source_name_len = tstrlen(source);
1776 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1777 name = tbasename(tstrcpy(source_copy, source));
1778 name_defaulted = true;
1780 /* Image description defaults to NULL if not given. */
1787 /* Set up capture sources in source list mode */
1788 if (source[0] == T('-') && source[1] == T('\0')) {
1789 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1791 source_list_contents = file_get_text_contents(source,
1792 &source_list_nchars);
1794 if (!source_list_contents)
1797 capture_sources = parse_source_list(&source_list_contents,
1800 if (!capture_sources) {
1802 goto out_free_source_list_contents;
1804 capture_sources_malloced = true;
1806 /* Set up capture source in non-source-list mode (could be
1807 * either "normal" mode or "NTFS mode"--- see the man page). */
1808 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1809 capture_sources[0].fs_source_path = source;
1810 capture_sources[0].wim_target_path = NULL;
1811 capture_sources[0].reserved = 0;
1813 capture_sources_malloced = false;
1814 source_list_contents = NULL;
1820 config_str = file_get_text_contents(config_file, &config_len);
1823 goto out_free_capture_sources;
1826 config = alloca(sizeof(*config));
1827 ret = parse_capture_config(&config_str, config_len, config);
1829 goto out_free_config;
1831 config = &default_capture_config;
1835 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1836 imagex_progress_func);
1838 ret = wimlib_create_new_wim(compression_type, &wim);
1840 goto out_free_config;
1846 if (tstat(source, &stbuf) == 0) {
1847 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1848 tprintf(T("Capturing WIM image from NTFS "
1849 "filesystem on \"%"TS"\"\n"), source);
1850 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1853 if (errno != ENOENT) {
1854 imagex_error_with_errno(T("Failed to stat "
1855 "\"%"TS"\""), source);
1857 goto out_wimlib_free;
1863 if (cmd == APPEND && name_defaulted) {
1864 /* If the user did not specify an image name, and the basename
1865 * of the source already exists as an image name in the WIM
1866 * file, append a suffix to make it unique. */
1867 unsigned long conflict_idx;
1868 tchar *name_end = tstrchr(name, T('\0'));
1869 for (conflict_idx = 1;
1870 wimlib_image_name_in_use(wim, name);
1873 tsprintf(name_end, T(" (%lu)"), conflict_idx);
1877 win32_acquire_capture_privileges();
1880 ret = wimlib_add_image_multisource(wim,
1886 imagex_progress_func);
1888 goto out_release_privs;
1890 if (desc || flags_element) {
1891 /* User provided <DESCRIPTION> or <FLAGS> element. Get the
1892 * index of the image we just added, then use it to call the
1893 * appropriate functions. */
1894 struct wimlib_wim_info info;
1896 wimlib_get_wim_info(wim, &info);
1899 ret = wimlib_set_image_descripton(wim,
1903 goto out_release_privs;
1906 if (flags_element) {
1907 ret = wimlib_set_image_flags(wim, info.image_count,
1910 goto out_release_privs;
1914 if (cmd == APPEND) {
1915 ret = wimlib_overwrite(wim, write_flags, num_threads,
1916 imagex_progress_func);
1918 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES, write_flags,
1919 num_threads, imagex_progress_func);
1922 imagex_error(T("Failed to write the WIM file \"%"TS"\""),
1926 win32_release_capture_privileges();
1931 if (config != &default_capture_config) {
1932 free(config->exclusion_pats.pats);
1933 free(config->exclusion_exception_pats.pats);
1936 out_free_capture_sources:
1937 if (capture_sources_malloced)
1938 free(capture_sources);
1939 out_free_source_list_contents:
1940 free(source_list_contents);
1951 /* Remove image(s) from a WIM. */
1953 imagex_delete(int argc, tchar **argv)
1956 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1957 int write_flags = 0;
1958 const tchar *wimfile;
1959 const tchar *image_num_or_name;
1964 for_opt(c, delete_options) {
1966 case IMAGEX_CHECK_OPTION:
1967 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1968 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1970 case IMAGEX_SOFT_OPTION:
1971 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
1982 imagex_error(T("Must specify a WIM file"));
1984 imagex_error(T("Must specify an image"));
1988 image_num_or_name = argv[1];
1990 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1991 imagex_progress_func);
1995 image = wimlib_resolve_image(wim, image_num_or_name);
1997 ret = verify_image_exists(image, image_num_or_name, wimfile);
1999 goto out_wimlib_free;
2001 ret = wimlib_delete_image(wim, image);
2003 imagex_error(T("Failed to delete image from \"%"TS"\""),
2005 goto out_wimlib_free;
2008 ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
2010 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2011 "deleted"), wimfile);
2025 print_full_path(const struct wimlib_dir_entry *wdentry, void *_ignore)
2027 int ret = tprintf(T("%"TS"\n"), wdentry->full_path);
2028 return (ret >= 0) ? 0 : -1;
2031 /* Print the files contained in an image(s) in a WIM file. */
2033 imagex_dir(int argc, tchar **argv)
2035 const tchar *wimfile;
2036 WIMStruct *wim = NULL;
2039 const tchar *path = T("");
2042 for_opt(c, dir_options) {
2044 case IMAGEX_PATH_OPTION:
2055 imagex_error(T("Must specify a WIM file"));
2059 imagex_error(T("Too many arguments"));
2064 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &wim,
2065 imagex_progress_func);
2070 image = wimlib_resolve_image(wim, argv[1]);
2071 ret = verify_image_exists(image, argv[1], wimfile);
2073 goto out_wimlib_free;
2075 /* No image specified; default to image 1, but only if the WIM
2076 * contains exactly one image. */
2078 struct wimlib_wim_info info;
2080 wimlib_get_wim_info(wim, &info);
2081 if (info.image_count != 1) {
2082 imagex_error(T("\"%"TS"\" contains %d images; Please "
2083 "select one (or all)."),
2084 wimfile, info.image_count);
2091 ret = wimlib_iterate_dir_tree(wim, image, path,
2092 WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE,
2093 print_full_path, NULL);
2105 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2108 imagex_export(int argc, tchar **argv)
2112 int export_flags = 0;
2113 int write_flags = 0;
2114 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2115 const tchar *src_wimfile;
2116 const tchar *src_image_num_or_name;
2117 const tchar *dest_wimfile;
2118 const tchar *dest_name;
2119 const tchar *dest_desc;
2121 WIMStruct *dest_wim;
2126 const tchar *swm_glob = NULL;
2127 WIMStruct **additional_swms;
2128 unsigned num_additional_swms;
2129 unsigned num_threads = 0;
2131 for_opt(c, export_options) {
2133 case IMAGEX_BOOT_OPTION:
2134 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2136 case IMAGEX_CHECK_OPTION:
2137 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2138 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2140 case IMAGEX_COMPRESS_OPTION:
2141 compression_type = get_compression_type(optarg);
2142 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2145 case IMAGEX_REF_OPTION:
2148 case IMAGEX_THREADS_OPTION:
2149 num_threads = parse_num_threads(optarg);
2150 if (num_threads == UINT_MAX)
2153 case IMAGEX_REBUILD_OPTION:
2154 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2162 if (argc < 3 || argc > 5)
2164 src_wimfile = argv[0];
2165 src_image_num_or_name = argv[1];
2166 dest_wimfile = argv[2];
2167 dest_name = (argc >= 4) ? argv[3] : NULL;
2168 dest_desc = (argc >= 5) ? argv[4] : NULL;
2169 ret = wimlib_open_wim(src_wimfile,
2170 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_wim,
2171 imagex_progress_func);
2175 /* Determine if the destination is an existing file or not.
2176 * If so, we try to append the exported image(s) to it; otherwise, we
2177 * create a new WIM containing the exported image(s). */
2178 if (tstat(dest_wimfile, &stbuf) == 0) {
2180 /* Destination file exists. */
2182 if (!S_ISREG(stbuf.st_mode)) {
2183 imagex_error(T("\"%"TS"\" is not a regular file"),
2186 goto out_free_src_wim;
2188 ret = wimlib_open_wim(dest_wimfile, open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2189 &dest_wim, imagex_progress_func);
2191 goto out_free_src_wim;
2193 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2194 /* The user specified a compression type, but we're
2195 * exporting to an existing WIM. Make sure the
2196 * specified compression type is the same as the
2197 * compression type of the existing destination WIM. */
2198 struct wimlib_wim_info dest_info;
2200 wimlib_get_wim_info(dest_wim, &dest_info);
2201 if (compression_type != dest_info.compression_type) {
2202 imagex_error(T("Cannot specify a compression type that is "
2203 "not the same as that used in the "
2204 "destination WIM"));
2206 goto out_free_dest_wim;
2212 if (errno != ENOENT) {
2213 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2216 goto out_free_src_wim;
2219 /* dest_wimfile is not an existing file, so create a new WIM. */
2221 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2222 /* The user did not specify a compression type; default
2223 * to that of the source WIM. */
2225 struct wimlib_wim_info src_info;
2227 wimlib_get_wim_info(src_wim, &src_info);
2228 compression_type = src_info.compression_type;
2230 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2232 goto out_free_src_wim;
2235 image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2236 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2238 goto out_free_dest_wim;
2241 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
2243 &num_additional_swms);
2245 goto out_free_dest_wim;
2247 additional_swms = NULL;
2248 num_additional_swms = 0;
2251 ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2252 dest_desc, export_flags, additional_swms,
2253 num_additional_swms, imagex_progress_func);
2258 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2259 write_flags, num_threads,
2260 imagex_progress_func);
2262 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2263 imagex_progress_func);
2265 for (unsigned i = 0; i < num_additional_swms; i++)
2266 wimlib_free(additional_swms[i]);
2267 free(additional_swms);
2269 wimlib_free(dest_wim);
2271 wimlib_free(src_wim);
2283 is_root_wim_path(const tchar *path)
2286 for (p = path; *p; p++)
2287 if (*p != T('\\') && *p != T('/'))
2293 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2294 const tchar *dest_dir)
2296 for (size_t i = 0; i < num_cmds; i++)
2297 if (cmds[i].fs_dest_path != dest_dir)
2298 free(cmds[i].fs_dest_path);
2302 static struct wimlib_extract_command *
2303 prepare_extract_commands(tchar **paths, unsigned num_paths,
2304 int extract_flags, tchar *dest_dir,
2305 size_t *num_cmds_ret)
2307 struct wimlib_extract_command *cmds;
2309 tchar *emptystr = T("");
2311 if (num_paths == 0) {
2315 num_cmds = num_paths;
2316 cmds = calloc(num_cmds, sizeof(cmds[0]));
2318 imagex_error(T("Out of memory!"));
2322 for (size_t i = 0; i < num_cmds; i++) {
2323 cmds[i].extract_flags = extract_flags;
2324 cmds[i].wim_source_path = paths[i];
2325 if (is_root_wim_path(paths[i])) {
2326 cmds[i].fs_dest_path = dest_dir;
2328 size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2329 cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2330 if (!cmds[i].fs_dest_path) {
2331 free_extract_commands(cmds, num_cmds, dest_dir);
2334 tsprintf(cmds[i].fs_dest_path,
2335 T("%"TS""OS_PREFERRED_PATH_SEPARATOR_STRING"%"TS),
2336 dest_dir, tbasename(paths[i]));
2339 *num_cmds_ret = num_cmds;
2343 /* Extract files or directories from a WIM image */
2345 imagex_extract(int argc, tchar **argv)
2348 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2352 const tchar *wimfile;
2353 const tchar *image_num_or_name;
2354 tchar *dest_dir = T(".");
2355 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL | WIMLIB_EXTRACT_FLAG_NORPFIX;
2357 const tchar *swm_glob = NULL;
2358 WIMStruct **additional_swms;
2359 unsigned num_additional_swms;
2361 struct wimlib_extract_command *cmds;
2364 for_opt(c, extract_options) {
2366 case IMAGEX_CHECK_OPTION:
2367 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2369 case IMAGEX_VERBOSE_OPTION:
2370 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
2372 case IMAGEX_REF_OPTION:
2375 case IMAGEX_UNIX_DATA_OPTION:
2376 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2378 case IMAGEX_NO_ACLS_OPTION:
2379 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2381 case IMAGEX_STRICT_ACLS_OPTION:
2382 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2384 case IMAGEX_DEST_DIR_OPTION:
2387 case IMAGEX_TO_STDOUT_OPTION:
2388 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2389 imagex_be_quiet = true;
2391 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2392 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2393 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2406 image_num_or_name = argv[1];
2411 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2416 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2420 image = wimlib_resolve_image(wim, image_num_or_name);
2421 ret = verify_image_exists_and_is_single(image,
2425 goto out_wimlib_free;
2428 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2430 &num_additional_swms);
2432 goto out_wimlib_free;
2434 additional_swms = NULL;
2435 num_additional_swms = 0;
2439 win32_acquire_restore_privileges();
2442 ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
2443 additional_swms, num_additional_swms,
2444 imagex_progress_func);
2446 if (!imagex_be_quiet)
2447 tprintf(T("Done extracting files.\n"));
2448 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2449 tfprintf(stderr, T("Note: You can use `"IMAGEX_PROGNAME" dir' to see what "
2450 "files and directories\n"
2451 " are in the WIM image.\n"));
2454 win32_release_restore_privileges();
2456 for (unsigned i = 0; i < num_additional_swms; i++)
2457 wimlib_free(additional_swms[i]);
2458 free(additional_swms);
2462 free_extract_commands(cmds, num_cmds, dest_dir);
2473 static void print_byte_field(const uint8_t field[], size_t len)
2476 tprintf(T("%02hhx"), *field++);
2480 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2482 tputs(T("WIM Information:"));
2483 tputs(T("----------------"));
2484 tprintf(T("Path: %"TS"\n"), wimfile);
2485 tprintf(T("GUID: 0x"));
2486 print_byte_field(info->guid, sizeof(info->guid));
2488 tprintf(T("Image Count: %d\n"), info->image_count);
2489 tprintf(T("Compression: %"TS"\n"),
2490 wimlib_get_compression_type_string(info->compression_type));
2491 tprintf(T("Part Number: %d/%d\n"), info->part_number, info->total_parts);
2492 tprintf(T("Boot Index: %d\n"), info->boot_index);
2493 tprintf(T("Size: %"PRIu64" bytes\n"), info->total_bytes);
2494 tprintf(T("Integrity Info: %"TS"\n"),
2495 info->has_integrity_table ? T("yes") : T("no"));
2496 tprintf(T("Relative path junction: %"TS"\n"),
2497 info->has_rpfix ? T("yes") : T("no"));
2502 print_resource(const struct wimlib_resource_entry *resource,
2506 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
2507 resource->uncompressed_size);
2509 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
2510 resource->compressed_size);
2512 tprintf(T("Offset = %"PRIu64" bytes\n"),
2516 tprintf(T("Part Number = %u\n"), resource->part_number);
2517 tprintf(T("Reference Count = %u\n"), resource->reference_count);
2519 tprintf(T("Hash = 0x"));
2520 print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2523 tprintf(T("Flags = "));
2524 if (resource->is_compressed)
2525 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED "));
2526 if (resource->is_metadata)
2527 tprintf(T("WIM_RESHDR_FLAG_METADATA "));
2528 if (resource->is_free)
2529 tprintf(T("WIM_RESHDR_FLAG_FREE "));
2530 if (resource->is_spanned)
2531 tprintf(T("WIM_RESHDR_FLAG_SPANNED "));
2538 print_lookup_table(WIMStruct *wim)
2540 wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2543 /* Prints information about a WIM file; also can mark an image as bootable,
2544 * change the name of an image, or change the description of an image. */
2546 imagex_info(int argc, tchar **argv)
2551 bool header = false;
2552 bool lookup_table = false;
2554 bool metadata = false;
2555 bool short_header = true;
2556 const tchar *xml_out_file = NULL;
2557 const tchar *wimfile;
2558 const tchar *image_num_or_name;
2559 const tchar *new_name;
2560 const tchar *new_desc;
2564 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2565 struct wimlib_wim_info info;
2567 for_opt(c, info_options) {
2569 case IMAGEX_BOOT_OPTION:
2572 case IMAGEX_CHECK_OPTION:
2575 case IMAGEX_HEADER_OPTION:
2577 short_header = false;
2579 case IMAGEX_LOOKUP_TABLE_OPTION:
2580 lookup_table = true;
2581 short_header = false;
2583 case IMAGEX_XML_OPTION:
2585 short_header = false;
2587 case IMAGEX_EXTRACT_XML_OPTION:
2588 xml_out_file = optarg;
2589 short_header = false;
2591 case IMAGEX_METADATA_OPTION:
2593 short_header = false;
2602 if (argc < 1 || argc > 4)
2606 image_num_or_name = (argc >= 2) ? argv[1] : T("all");
2607 new_name = (argc >= 3) ? argv[2] : NULL;
2608 new_desc = (argc >= 4) ? argv[3] : NULL;
2611 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2613 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2617 wimlib_get_wim_info(wim, &info);
2619 image = wimlib_resolve_image(wim, image_num_or_name);
2620 ret = WIMLIB_ERR_INVALID_IMAGE;
2621 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
2622 imagex_error(T("The image \"%"TS"\" does not exist in \"%"TS"\""),
2623 image_num_or_name, wimfile);
2625 imagex_error(T("If you would like to set the boot "
2626 "index to 0, specify image \"0\" with "
2627 "the --boot flag."));
2629 goto out_wimlib_free;
2632 if (boot && info.image_count == 0) {
2633 imagex_error(T("--boot is meaningless on a WIM with no images"));
2634 goto out_wimlib_free;
2637 if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
2639 imagex_error(T("Cannot specify the --boot flag "
2640 "without specifying a specific "
2641 "image in a multi-image WIM"));
2642 goto out_wimlib_free;
2645 imagex_error(T("Cannot specify the NEW_NAME "
2646 "without specifying a specific "
2647 "image in a multi-image WIM"));
2648 goto out_wimlib_free;
2652 /* Operations that print information are separated from operations that
2653 * recreate the WIM file. */
2654 if (!new_name && !boot) {
2656 /* Read-only operations */
2658 if (image == WIMLIB_NO_IMAGE) {
2659 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
2660 image_num_or_name, wimfile);
2661 goto out_wimlib_free;
2664 if (image == WIMLIB_ALL_IMAGES && short_header)
2665 print_wim_information(wimfile, &info);
2668 wimlib_print_header(wim);
2671 if (info.total_parts != 1) {
2672 tfprintf(stderr, T("Warning: Only showing the lookup table "
2673 "for part %d of a %d-part WIM.\n"),
2674 info.part_number, info.total_parts);
2676 print_lookup_table(wim);
2680 ret = wimlib_extract_xml_data(wim, stdout);
2682 goto out_wimlib_free;
2688 fp = tfopen(xml_out_file, T("wb"));
2690 imagex_error_with_errno(T("Failed to open the "
2691 "file \"%"TS"\" for "
2695 goto out_wimlib_free;
2697 ret = wimlib_extract_xml_data(wim, fp);
2699 imagex_error(T("Failed to close the file "
2705 goto out_wimlib_free;
2709 wimlib_print_available_images(wim, image);
2712 ret = wimlib_print_metadata(wim, image);
2714 goto out_wimlib_free;
2719 /* Modification operations */
2721 if (image == WIMLIB_ALL_IMAGES)
2724 if (image == WIMLIB_NO_IMAGE && new_name) {
2725 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
2726 "when using image 0"), new_name);
2728 goto out_wimlib_free;
2732 if (image == info.boot_index) {
2733 tprintf(T("Image %d is already marked as "
2734 "bootable.\n"), image);
2737 tprintf(T("Marking image %d as bootable.\n"),
2739 info.boot_index = image;
2740 ret = wimlib_set_wim_info(wim, &info,
2741 WIMLIB_CHANGE_BOOT_INDEX);
2743 goto out_wimlib_free;
2747 if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
2749 tprintf(T("Image %d is already named \"%"TS"\".\n"),
2753 tprintf(T("Changing the name of image %d to "
2754 "\"%"TS"\".\n"), image, new_name);
2755 ret = wimlib_set_image_name(wim, image, new_name);
2757 goto out_wimlib_free;
2761 const tchar *old_desc;
2762 old_desc = wimlib_get_image_description(wim, image);
2763 if (old_desc && !tstrcmp(old_desc, new_desc)) {
2764 tprintf(T("The description of image %d is already "
2765 "\"%"TS"\".\n"), image, new_desc);
2768 tprintf(T("Changing the description of image %d "
2769 "to \"%"TS"\".\n"), image, new_desc);
2770 ret = wimlib_set_image_descripton(wim, image,
2773 goto out_wimlib_free;
2777 /* Only call wimlib_overwrite() if something actually needs to
2779 if (boot || new_name || new_desc ||
2780 (check && !info.has_integrity_table))
2782 int write_flags = 0;
2785 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2786 ret = wimlib_overwrite(wim, write_flags, 1,
2787 imagex_progress_func);
2789 tprintf(T("The file \"%"TS"\" was not modified because nothing "
2790 "needed to be done.\n"), wimfile);
2805 /* Join split WIMs into one part WIM */
2807 imagex_join(int argc, tchar **argv)
2810 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2811 int wim_write_flags = 0;
2812 const tchar *output_path;
2815 for_opt(c, join_options) {
2817 case IMAGEX_CHECK_OPTION:
2818 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2819 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2829 imagex_error(T("Must specify one or more split WIM (.swm) "
2833 output_path = argv[0];
2834 ret = wimlib_join((const tchar * const *)++argv,
2839 imagex_progress_func);
2849 /* Mounts an image using a FUSE mount. */
2851 imagex_mount_rw_or_ro(int argc, tchar **argv)
2854 int mount_flags = 0;
2855 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2856 const tchar *swm_glob = NULL;
2857 const tchar *staging_dir = NULL;
2858 const tchar *wimfile;
2863 WIMStruct **additional_swms;
2864 unsigned num_additional_swms;
2866 if (!tstrcmp(argv[0], T("mountrw"))) {
2867 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
2868 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2871 for_opt(c, mount_options) {
2873 case IMAGEX_ALLOW_OTHER_OPTION:
2874 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
2876 case IMAGEX_CHECK_OPTION:
2877 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2879 case IMAGEX_DEBUG_OPTION:
2880 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
2882 case IMAGEX_STREAMS_INTERFACE_OPTION:
2883 if (!tstrcasecmp(optarg, T("none")))
2884 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
2885 else if (!tstrcasecmp(optarg, T("xattr")))
2886 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2887 else if (!tstrcasecmp(optarg, T("windows")))
2888 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
2890 imagex_error(T("Unknown stream interface \"%"TS"\""),
2895 case IMAGEX_REF_OPTION:
2898 case IMAGEX_STAGING_DIR_OPTION:
2899 staging_dir = optarg;
2901 case IMAGEX_UNIX_DATA_OPTION:
2902 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
2910 if (argc != 2 && argc != 3)
2915 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2920 /* Image explicitly specified. */
2921 image = wimlib_resolve_image(wim, argv[1]);
2923 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
2927 /* No image specified; default to image 1, but only if the WIM
2928 * contains exactly one image. */
2929 struct wimlib_wim_info info;
2931 wimlib_get_wim_info(wim, &info);
2932 if (info.image_count != 1) {
2933 imagex_error(T("\"%"TS"\" contains %d images; Please "
2934 "select one."), wimfile, info.image_count);
2943 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2945 &num_additional_swms);
2949 additional_swms = NULL;
2950 num_additional_swms = 0;
2953 ret = wimlib_mount_image(wim, image, dir, mount_flags, additional_swms,
2954 num_additional_swms, staging_dir);
2956 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
2958 image, wimfile, dir);
2960 for (unsigned i = 0; i < num_additional_swms; i++)
2961 wimlib_free(additional_swms[i]);
2962 free(additional_swms);
2969 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2975 /* Rebuild a WIM file */
2977 imagex_optimize(int argc, tchar **argv)
2980 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2981 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
2984 const tchar *wimfile;
2987 unsigned num_threads = 0;
2989 for_opt(c, optimize_options) {
2991 case IMAGEX_CHECK_OPTION:
2992 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2993 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2995 case IMAGEX_RECOMPRESS_OPTION:
2996 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
2998 case IMAGEX_THREADS_OPTION:
2999 num_threads = parse_num_threads(optarg);
3000 if (num_threads == UINT_MAX)
3015 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3019 old_size = file_get_size(wimfile);
3020 tprintf(T("\"%"TS"\" original size: "), wimfile);
3022 tputs(T("Unknown"));
3024 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3026 ret = wimlib_overwrite(wim, write_flags, num_threads,
3027 imagex_progress_func);
3029 goto out_wimlib_free;
3031 new_size = file_get_size(wimfile);
3032 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3034 tputs(T("Unknown"));
3036 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3038 tfputs(T("Space saved: "), stdout);
3039 if (new_size != -1 && old_size != -1) {
3040 tprintf(T("%lld KiB\n"),
3041 ((long long)old_size - (long long)new_size) >> 10);
3043 tputs(T("Unknown"));
3058 /* Split a WIM into a spanned set */
3060 imagex_split(int argc, tchar **argv)
3064 int write_flags = 0;
3065 unsigned long part_size;
3070 for_opt(c, split_options) {
3072 case IMAGEX_CHECK_OPTION:
3073 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3074 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3086 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3087 if (tmp == argv[2] || *tmp) {
3088 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3089 imagex_error(T("The part size must be an integer or "
3090 "floating-point number of megabytes."));
3093 ret = wimlib_open_wim(argv[0], open_flags, &wim, imagex_progress_func);
3097 ret = wimlib_split(wim, argv[1], part_size, write_flags, imagex_progress_func);
3109 /* Unmounts a mounted WIM image. */
3111 imagex_unmount(int argc, tchar **argv)
3114 int unmount_flags = 0;
3117 for_opt(c, unmount_options) {
3119 case IMAGEX_COMMIT_OPTION:
3120 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3122 case IMAGEX_CHECK_OPTION:
3123 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3125 case IMAGEX_REBUILD_OPTION:
3126 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3128 case IMAGEX_LAZY_OPTION:
3129 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3140 ret = wimlib_unmount_image(argv[0], unmount_flags,
3141 imagex_progress_func);
3143 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3154 * Add, delete, or rename files in a WIM image.
3157 imagex_update(int argc, tchar **argv)
3159 const tchar *wimfile;
3163 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3164 int write_flags = 0;
3165 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3166 int default_add_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
3167 int default_delete_flags = 0;
3168 unsigned num_threads = 0;
3170 tchar *cmd_file_contents;
3171 size_t cmd_file_nchars;
3172 struct wimlib_update_command *cmds;
3174 tchar *command_str = NULL;
3176 const tchar *config_file = NULL;
3178 struct wimlib_capture_config *config;
3180 for_opt(c, update_options) {
3182 /* Generic or write options */
3183 case IMAGEX_THREADS_OPTION:
3184 num_threads = parse_num_threads(optarg);
3185 if (num_threads == UINT_MAX)
3188 case IMAGEX_CHECK_OPTION:
3189 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3190 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3192 case IMAGEX_REBUILD_OPTION:
3193 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3195 case IMAGEX_COMMAND_OPTION:
3197 imagex_error(T("--command may only be specified "
3198 "one time. Please provide\n"
3199 " the update commands "
3200 "on standard input instead."));
3203 command_str = tstrdup(optarg);
3205 imagex_error(T("Out of memory!"));
3209 /* Default delete options */
3210 case IMAGEX_FORCE_OPTION:
3211 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3213 case IMAGEX_RECURSIVE_OPTION:
3214 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3217 /* Global add option */
3218 case IMAGEX_CONFIG_OPTION:
3219 config_file = optarg;
3222 /* Default add options */
3223 case IMAGEX_VERBOSE_OPTION:
3224 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
3226 case IMAGEX_DEREFERENCE_OPTION:
3227 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
3229 case IMAGEX_UNIX_DATA_OPTION:
3230 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
3232 case IMAGEX_NO_ACLS_OPTION:
3233 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
3235 case IMAGEX_STRICT_ACLS_OPTION:
3236 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
3245 if (argc != 1 && argc != 2)
3249 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3251 goto out_free_command_str;
3254 /* Image explicitly specified. */
3255 image = wimlib_resolve_image(wim, argv[1]);
3256 ret = verify_image_exists_and_is_single(image, argv[1],
3259 goto out_wimlib_free;
3261 /* No image specified; default to image 1, but only if the WIM
3262 * contains exactly one image. */
3263 struct wimlib_wim_info info;
3265 wimlib_get_wim_info(wim, &info);
3266 if (info.image_count != 1) {
3267 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3268 wimfile, info.image_count);
3275 /* Parse capture configuration file if specified */
3279 config_str = file_get_text_contents(config_file, &config_len);
3282 goto out_wimlib_free;
3285 config = alloca(sizeof(*config));
3286 ret = parse_capture_config(&config_str, config_len, config);
3288 goto out_free_config;
3290 config = &default_capture_config;
3293 /* Read update commands from standard input, or the command string if
3296 cmd_file_contents = NULL;
3297 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3300 if (isatty(STDIN_FILENO)) {
3301 tputs(T("Reading update commands from standard input..."));
3302 recommend_man_page(T("update"));
3304 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3305 if (!cmd_file_contents) {
3307 goto out_free_config;
3310 /* Parse the update commands */
3311 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3316 goto out_free_cmd_file_contents;
3319 /* Set default flags and capture config on the update commands */
3320 bool have_add_command = false;
3321 for (size_t i = 0; i < num_cmds; i++) {
3322 switch (cmds[i].op) {
3323 case WIMLIB_UPDATE_OP_ADD:
3324 cmds[i].add.add_flags |= default_add_flags;
3325 cmds[i].add.config = config;
3326 have_add_command = true;
3328 case WIMLIB_UPDATE_OP_DELETE:
3329 cmds[i].delete.delete_flags |= default_delete_flags;
3337 if (have_add_command)
3338 win32_acquire_capture_privileges();
3341 /* Execute the update commands */
3342 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3343 imagex_progress_func);
3345 goto out_release_privs;
3347 /* Overwrite the updated WIM */
3348 ret = wimlib_overwrite(wim, write_flags, num_threads,
3349 imagex_progress_func);
3352 if (have_add_command)
3353 win32_release_capture_privileges();
3356 out_free_cmd_file_contents:
3357 free(cmd_file_contents);
3359 if (config != &default_capture_config) {
3360 free(config->exclusion_pats.pats);
3361 free(config->exclusion_exception_pats.pats);
3366 out_free_command_str:
3374 goto out_free_command_str;
3377 struct imagex_command {
3379 int (*func)(int , tchar **);
3384 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
3385 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
3387 static const struct imagex_command imagex_commands[] = {
3388 {T("append"), imagex_capture_or_append, APPEND},
3389 {T("apply"), imagex_apply, APPLY},
3390 {T("capture"), imagex_capture_or_append, CAPTURE},
3391 {T("delete"), imagex_delete, DELETE},
3392 {T("dir"), imagex_dir, DIR},
3393 {T("export"), imagex_export, EXPORT},
3394 {T("extract"), imagex_extract, EXTRACT},
3395 {T("info"), imagex_info, INFO},
3396 {T("join"), imagex_join, JOIN},
3397 {T("mount"), imagex_mount_rw_or_ro, MOUNT},
3398 {T("mountrw"), imagex_mount_rw_or_ro, MOUNTRW},
3399 {T("optimize"),imagex_optimize, OPTIMIZE},
3400 {T("split"), imagex_split, SPLIT},
3401 {T("unmount"), imagex_unmount, UNMOUNT},
3402 {T("update"), imagex_update, UPDATE},
3408 static const tchar *s =
3410 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
3411 "Copyright (C) 2012, 2013 Eric Biggers\n"
3412 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
3413 "This is free software: you are free to change and redistribute it.\n"
3414 "There is NO WARRANTY, to the extent permitted by law.\n"
3416 "Report bugs to "PACKAGE_BUGREPORT".\n"
3423 help_or_version(int argc, tchar **argv)
3427 const struct imagex_command *cmd;
3429 for (i = 1; i < argc; i++) {
3437 if (!tstrcmp(p, T("help"))) {
3438 for_imagex_command(cmd) {
3439 if (!tstrcmp(cmd->name, argv[1])) {
3447 if (!tstrcmp(p, T("version"))) {
3458 const struct imagex_command *cmd;
3459 tprintf(T("Usage:\n%"TS), usage_strings[cmd_type]);
3460 for_imagex_command(cmd) {
3461 if (cmd->cmd == cmd_type) {
3462 tputc(T('\n'), stdout);
3463 recommend_man_page(cmd->name);
3471 tfputs(T("Usage:\n"), stdout);
3472 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
3473 tprintf(T(" %"TS), usage_strings[i]);
3474 static const tchar *extra =
3476 " "IMAGEX_PROGNAME" --help\n"
3477 " "IMAGEX_PROGNAME" --version\n"
3479 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
3482 tfputs(extra, stdout);
3483 recommend_man_page(T(""));
3486 /* Entry point for wimlib's ImageX implementation. On UNIX the command
3487 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
3488 * something else), while an Windows the command arguments will be UTF-16LE
3489 * encoded 'wchar_t' strings. */
3492 wmain(int argc, wchar_t **argv, wchar_t **envp)
3494 main(int argc, char **argv)
3497 const struct imagex_command *cmd;
3502 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
3503 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
3507 setlocale(LC_ALL, "");
3508 codeset = nl_langinfo(CODESET);
3509 if (!strstr(codeset, "UTF-8") &&
3510 !strstr(codeset, "UTF8") &&
3511 !strstr(codeset, "utf-8") &&
3512 !strstr(codeset, "utf8"))
3515 "WARNING: Running "IMAGEX_PROGNAME" in a UTF-8 locale is recommended!\n"
3516 " Maybe try: `export LANG=en_US.UTF-8'?\n"
3517 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
3518 " to any value to force wimlib to use UTF-8.\n",
3523 #endif /* !__WIN32__ */
3526 imagex_error(T("No command specified"));
3532 /* Handle --help and --version for all commands. Note that this will
3533 * not return if either of these arguments are present. */
3534 help_or_version(argc, argv);
3538 /* The user may like to see more informative error messages. */
3539 wimlib_set_print_errors(true);
3541 /* Do any initializations that the library needs */
3542 ret = wimlib_global_init(init_flags);
3544 goto out_check_status;
3546 /* Search for the function to handle the ImageX subcommand. */
3547 for_imagex_command(cmd) {
3548 if (!tstrcmp(cmd->name, *argv)) {
3549 ret = cmd->func(argc, argv);
3550 goto out_check_write_error;
3554 imagex_error(T("Unrecognized command: `%"TS"'"), argv[0]);
3558 out_check_write_error:
3559 /* For 'wimlib-imagex info' and 'wimlib-imagex dir', data printed to
3560 * standard output is part of the program's actual behavior and not just
3561 * for informational purposes, so we should set a failure exit status if
3562 * there was a write error. */
3563 if (cmd == &imagex_commands[INFO] || cmd == &imagex_commands[DIR]) {
3564 if (ferror(stdout) || fclose(stdout)) {
3565 imagex_error_with_errno(T("error writing to standard output"));
3571 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
3572 * outside of the wimlib library code. 0 indicates success. > 0
3573 * indicates a wimlib error code from which an error message can be
3576 imagex_error(T("Exiting with error code %d:\n"
3578 wimlib_get_error_string(ret));
3579 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
3580 imagex_error_with_errno(T("errno"));
3583 /* Make the library free any resources it's holding (not strictly
3584 * necessary because the process is ending anyway). */
3585 wimlib_global_cleanup();