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();
1651 for (unsigned i = 0; i < num_additional_swms; i++)
1652 wimlib_free(additional_swms[i]);
1653 free(additional_swms);
1666 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1667 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1668 * the desired image. 'wimlib-imagex append': add a new image to an existing
1671 imagex_capture_or_append(int argc, tchar **argv)
1674 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1675 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
1676 int write_flags = 0;
1677 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
1678 const tchar *wimfile;
1681 const tchar *flags_element = NULL;
1684 int cmd = tstrcmp(argv[0], T("append")) ? CAPTURE : APPEND;
1685 unsigned num_threads = 0;
1690 const tchar *config_file = NULL;
1692 struct wimlib_capture_config *config;
1694 bool source_list = false;
1695 size_t source_list_nchars;
1696 tchar *source_list_contents;
1697 bool capture_sources_malloced;
1698 struct wimlib_capture_source *capture_sources;
1700 bool name_defaulted;
1702 for_opt(c, capture_or_append_options) {
1704 case IMAGEX_BOOT_OPTION:
1705 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1707 case IMAGEX_CHECK_OPTION:
1708 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1709 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1711 case IMAGEX_CONFIG_OPTION:
1712 config_file = optarg;
1714 case IMAGEX_COMPRESS_OPTION:
1715 compression_type = get_compression_type(optarg);
1716 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1719 case IMAGEX_FLAGS_OPTION:
1720 flags_element = optarg;
1722 case IMAGEX_DEREFERENCE_OPTION:
1723 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1725 case IMAGEX_VERBOSE_OPTION:
1726 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1728 case IMAGEX_THREADS_OPTION:
1729 num_threads = parse_num_threads(optarg);
1730 if (num_threads == UINT_MAX)
1733 case IMAGEX_REBUILD_OPTION:
1734 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1736 case IMAGEX_UNIX_DATA_OPTION:
1737 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1739 case IMAGEX_SOURCE_LIST_OPTION:
1742 case IMAGEX_NO_ACLS_OPTION:
1743 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1745 case IMAGEX_STRICT_ACLS_OPTION:
1746 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1748 case IMAGEX_RPFIX_OPTION:
1749 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1751 case IMAGEX_NORPFIX_OPTION:
1752 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1761 if (argc < 2 || argc > 4)
1769 name_defaulted = false;
1771 /* Set default name to SOURCE argument, omitting any directory
1772 * prefixes and trailing slashes. This requires making a copy
1773 * of @source. Leave some free characters at the end in case we
1774 * append a number to keep the name unique. */
1775 size_t source_name_len;
1777 source_name_len = tstrlen(source);
1778 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1779 name = tbasename(tstrcpy(source_copy, source));
1780 name_defaulted = true;
1782 /* Image description defaults to NULL if not given. */
1789 /* Set up capture sources in source list mode */
1790 if (source[0] == T('-') && source[1] == T('\0')) {
1791 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1793 source_list_contents = file_get_text_contents(source,
1794 &source_list_nchars);
1796 if (!source_list_contents)
1799 capture_sources = parse_source_list(&source_list_contents,
1802 if (!capture_sources) {
1804 goto out_free_source_list_contents;
1806 capture_sources_malloced = true;
1808 /* Set up capture source in non-source-list mode (could be
1809 * either "normal" mode or "NTFS mode"--- see the man page). */
1810 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1811 capture_sources[0].fs_source_path = source;
1812 capture_sources[0].wim_target_path = NULL;
1813 capture_sources[0].reserved = 0;
1815 capture_sources_malloced = false;
1816 source_list_contents = NULL;
1822 config_str = file_get_text_contents(config_file, &config_len);
1825 goto out_free_capture_sources;
1828 config = alloca(sizeof(*config));
1829 ret = parse_capture_config(&config_str, config_len, config);
1831 goto out_free_config;
1833 config = &default_capture_config;
1837 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1838 imagex_progress_func);
1840 ret = wimlib_create_new_wim(compression_type, &wim);
1842 goto out_free_config;
1848 if (tstat(source, &stbuf) == 0) {
1849 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1850 tprintf(T("Capturing WIM image from NTFS "
1851 "filesystem on \"%"TS"\"\n"), source);
1852 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1855 if (errno != ENOENT) {
1856 imagex_error_with_errno(T("Failed to stat "
1857 "\"%"TS"\""), source);
1859 goto out_wimlib_free;
1865 if (cmd == APPEND && name_defaulted) {
1866 /* If the user did not specify an image name, and the basename
1867 * of the source already exists as an image name in the WIM
1868 * file, append a suffix to make it unique. */
1869 unsigned long conflict_idx;
1870 tchar *name_end = tstrchr(name, T('\0'));
1871 for (conflict_idx = 1;
1872 wimlib_image_name_in_use(wim, name);
1875 tsprintf(name_end, T(" (%lu)"), conflict_idx);
1879 win32_acquire_capture_privileges();
1882 ret = wimlib_add_image_multisource(wim,
1888 imagex_progress_func);
1890 goto out_release_privs;
1892 if (desc || flags_element) {
1893 /* User provided <DESCRIPTION> or <FLAGS> element. Get the
1894 * index of the image we just added, then use it to call the
1895 * appropriate functions. */
1896 struct wimlib_wim_info info;
1898 wimlib_get_wim_info(wim, &info);
1901 ret = wimlib_set_image_descripton(wim,
1905 goto out_release_privs;
1908 if (flags_element) {
1909 ret = wimlib_set_image_flags(wim, info.image_count,
1912 goto out_release_privs;
1916 if (cmd == APPEND) {
1917 ret = wimlib_overwrite(wim, write_flags, num_threads,
1918 imagex_progress_func);
1920 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES, write_flags,
1921 num_threads, imagex_progress_func);
1924 imagex_error(T("Failed to write the WIM file \"%"TS"\""),
1928 win32_release_capture_privileges();
1933 if (config != &default_capture_config) {
1934 free(config->exclusion_pats.pats);
1935 free(config->exclusion_exception_pats.pats);
1938 out_free_capture_sources:
1939 if (capture_sources_malloced)
1940 free(capture_sources);
1941 out_free_source_list_contents:
1942 free(source_list_contents);
1953 /* Remove image(s) from a WIM. */
1955 imagex_delete(int argc, tchar **argv)
1958 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1959 int write_flags = 0;
1960 const tchar *wimfile;
1961 const tchar *image_num_or_name;
1966 for_opt(c, delete_options) {
1968 case IMAGEX_CHECK_OPTION:
1969 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1970 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1972 case IMAGEX_SOFT_OPTION:
1973 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
1984 imagex_error(T("Must specify a WIM file"));
1986 imagex_error(T("Must specify an image"));
1990 image_num_or_name = argv[1];
1992 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1993 imagex_progress_func);
1997 image = wimlib_resolve_image(wim, image_num_or_name);
1999 ret = verify_image_exists(image, image_num_or_name, wimfile);
2001 goto out_wimlib_free;
2003 ret = wimlib_delete_image(wim, image);
2005 imagex_error(T("Failed to delete image from \"%"TS"\""),
2007 goto out_wimlib_free;
2010 ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
2012 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2013 "deleted"), wimfile);
2027 print_full_path(const struct wimlib_dir_entry *wdentry, void *_ignore)
2029 int ret = tprintf(T("%"TS"\n"), wdentry->full_path);
2030 return (ret >= 0) ? 0 : -1;
2033 /* Print the files contained in an image(s) in a WIM file. */
2035 imagex_dir(int argc, tchar **argv)
2037 const tchar *wimfile;
2038 WIMStruct *wim = NULL;
2041 const tchar *path = T("");
2044 for_opt(c, dir_options) {
2046 case IMAGEX_PATH_OPTION:
2057 imagex_error(T("Must specify a WIM file"));
2061 imagex_error(T("Too many arguments"));
2066 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &wim,
2067 imagex_progress_func);
2072 image = wimlib_resolve_image(wim, argv[1]);
2073 ret = verify_image_exists(image, argv[1], wimfile);
2075 goto out_wimlib_free;
2077 /* No image specified; default to image 1, but only if the WIM
2078 * contains exactly one image. */
2080 struct wimlib_wim_info info;
2082 wimlib_get_wim_info(wim, &info);
2083 if (info.image_count != 1) {
2084 imagex_error(T("\"%"TS"\" contains %d images; Please "
2085 "select one (or all)."),
2086 wimfile, info.image_count);
2093 ret = wimlib_iterate_dir_tree(wim, image, path,
2094 WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE,
2095 print_full_path, NULL);
2107 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2110 imagex_export(int argc, tchar **argv)
2114 int export_flags = 0;
2115 int write_flags = 0;
2116 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2117 const tchar *src_wimfile;
2118 const tchar *src_image_num_or_name;
2119 const tchar *dest_wimfile;
2120 const tchar *dest_name;
2121 const tchar *dest_desc;
2123 WIMStruct *dest_wim;
2128 const tchar *swm_glob = NULL;
2129 WIMStruct **additional_swms;
2130 unsigned num_additional_swms;
2131 unsigned num_threads = 0;
2133 for_opt(c, export_options) {
2135 case IMAGEX_BOOT_OPTION:
2136 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2138 case IMAGEX_CHECK_OPTION:
2139 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2140 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2142 case IMAGEX_COMPRESS_OPTION:
2143 compression_type = get_compression_type(optarg);
2144 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2147 case IMAGEX_REF_OPTION:
2150 case IMAGEX_THREADS_OPTION:
2151 num_threads = parse_num_threads(optarg);
2152 if (num_threads == UINT_MAX)
2155 case IMAGEX_REBUILD_OPTION:
2156 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2164 if (argc < 3 || argc > 5)
2166 src_wimfile = argv[0];
2167 src_image_num_or_name = argv[1];
2168 dest_wimfile = argv[2];
2169 dest_name = (argc >= 4) ? argv[3] : NULL;
2170 dest_desc = (argc >= 5) ? argv[4] : NULL;
2171 ret = wimlib_open_wim(src_wimfile,
2172 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_wim,
2173 imagex_progress_func);
2177 /* Determine if the destination is an existing file or not.
2178 * If so, we try to append the exported image(s) to it; otherwise, we
2179 * create a new WIM containing the exported image(s). */
2180 if (tstat(dest_wimfile, &stbuf) == 0) {
2182 /* Destination file exists. */
2184 if (!S_ISREG(stbuf.st_mode)) {
2185 imagex_error(T("\"%"TS"\" is not a regular file"),
2188 goto out_free_src_wim;
2190 ret = wimlib_open_wim(dest_wimfile, open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2191 &dest_wim, imagex_progress_func);
2193 goto out_free_src_wim;
2195 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2196 /* The user specified a compression type, but we're
2197 * exporting to an existing WIM. Make sure the
2198 * specified compression type is the same as the
2199 * compression type of the existing destination WIM. */
2200 struct wimlib_wim_info dest_info;
2202 wimlib_get_wim_info(dest_wim, &dest_info);
2203 if (compression_type != dest_info.compression_type) {
2204 imagex_error(T("Cannot specify a compression type that is "
2205 "not the same as that used in the "
2206 "destination WIM"));
2208 goto out_free_dest_wim;
2214 if (errno != ENOENT) {
2215 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2218 goto out_free_src_wim;
2221 /* dest_wimfile is not an existing file, so create a new WIM. */
2223 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2224 /* The user did not specify a compression type; default
2225 * to that of the source WIM. */
2227 struct wimlib_wim_info src_info;
2229 wimlib_get_wim_info(src_wim, &src_info);
2230 compression_type = src_info.compression_type;
2232 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2234 goto out_free_src_wim;
2237 image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2238 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2240 goto out_free_dest_wim;
2243 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
2245 &num_additional_swms);
2247 goto out_free_dest_wim;
2249 additional_swms = NULL;
2250 num_additional_swms = 0;
2253 ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2254 dest_desc, export_flags, additional_swms,
2255 num_additional_swms, imagex_progress_func);
2260 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2261 write_flags, num_threads,
2262 imagex_progress_func);
2264 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2265 imagex_progress_func);
2268 for (unsigned i = 0; i < num_additional_swms; i++)
2269 wimlib_free(additional_swms[i]);
2270 free(additional_swms);
2273 wimlib_free(dest_wim);
2275 wimlib_free(src_wim);
2287 is_root_wim_path(const tchar *path)
2290 for (p = path; *p; p++)
2291 if (*p != T('\\') && *p != T('/'))
2297 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2298 const tchar *dest_dir)
2300 for (size_t i = 0; i < num_cmds; i++)
2301 if (cmds[i].fs_dest_path != dest_dir)
2302 free(cmds[i].fs_dest_path);
2306 static struct wimlib_extract_command *
2307 prepare_extract_commands(tchar **paths, unsigned num_paths,
2308 int extract_flags, tchar *dest_dir,
2309 size_t *num_cmds_ret)
2311 struct wimlib_extract_command *cmds;
2313 tchar *emptystr = T("");
2315 if (num_paths == 0) {
2319 num_cmds = num_paths;
2320 cmds = calloc(num_cmds, sizeof(cmds[0]));
2322 imagex_error(T("Out of memory!"));
2326 for (size_t i = 0; i < num_cmds; i++) {
2327 cmds[i].extract_flags = extract_flags;
2328 cmds[i].wim_source_path = paths[i];
2329 if (is_root_wim_path(paths[i])) {
2330 cmds[i].fs_dest_path = dest_dir;
2332 size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2333 cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2334 if (!cmds[i].fs_dest_path) {
2335 free_extract_commands(cmds, num_cmds, dest_dir);
2338 tsprintf(cmds[i].fs_dest_path,
2339 T("%"TS""OS_PREFERRED_PATH_SEPARATOR_STRING"%"TS),
2340 dest_dir, tbasename(paths[i]));
2343 *num_cmds_ret = num_cmds;
2347 /* Extract files or directories from a WIM image */
2349 imagex_extract(int argc, tchar **argv)
2352 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2356 const tchar *wimfile;
2357 const tchar *image_num_or_name;
2358 tchar *dest_dir = T(".");
2359 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL | WIMLIB_EXTRACT_FLAG_NORPFIX;
2361 const tchar *swm_glob = NULL;
2362 WIMStruct **additional_swms;
2363 unsigned num_additional_swms;
2365 struct wimlib_extract_command *cmds;
2368 for_opt(c, extract_options) {
2370 case IMAGEX_CHECK_OPTION:
2371 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2373 case IMAGEX_VERBOSE_OPTION:
2374 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
2376 case IMAGEX_REF_OPTION:
2379 case IMAGEX_UNIX_DATA_OPTION:
2380 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2382 case IMAGEX_NO_ACLS_OPTION:
2383 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2385 case IMAGEX_STRICT_ACLS_OPTION:
2386 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2388 case IMAGEX_DEST_DIR_OPTION:
2391 case IMAGEX_TO_STDOUT_OPTION:
2392 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2393 imagex_be_quiet = true;
2395 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2396 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2397 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2410 image_num_or_name = argv[1];
2415 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2420 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2424 image = wimlib_resolve_image(wim, image_num_or_name);
2425 ret = verify_image_exists_and_is_single(image,
2429 goto out_wimlib_free;
2432 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2434 &num_additional_swms);
2436 goto out_wimlib_free;
2438 additional_swms = NULL;
2439 num_additional_swms = 0;
2443 win32_acquire_restore_privileges();
2446 ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
2447 additional_swms, num_additional_swms,
2448 imagex_progress_func);
2450 if (!imagex_be_quiet)
2451 tprintf(T("Done extracting files.\n"));
2452 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2453 tfprintf(stderr, T("Note: You can use `"IMAGEX_PROGNAME" dir' to see what "
2454 "files and directories\n"
2455 " are in the WIM image.\n"));
2458 win32_release_restore_privileges();
2461 for (unsigned i = 0; i < num_additional_swms; i++)
2462 wimlib_free(additional_swms[i]);
2463 free(additional_swms);
2468 free_extract_commands(cmds, num_cmds, dest_dir);
2479 static void print_byte_field(const uint8_t field[], size_t len)
2482 tprintf(T("%02hhx"), *field++);
2486 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2488 tputs(T("WIM Information:"));
2489 tputs(T("----------------"));
2490 tprintf(T("Path: %"TS"\n"), wimfile);
2491 tprintf(T("GUID: 0x"));
2492 print_byte_field(info->guid, sizeof(info->guid));
2494 tprintf(T("Image Count: %d\n"), info->image_count);
2495 tprintf(T("Compression: %"TS"\n"),
2496 wimlib_get_compression_type_string(info->compression_type));
2497 tprintf(T("Part Number: %d/%d\n"), info->part_number, info->total_parts);
2498 tprintf(T("Boot Index: %d\n"), info->boot_index);
2499 tprintf(T("Size: %"PRIu64" bytes\n"), info->total_bytes);
2500 tprintf(T("Integrity Info: %"TS"\n"),
2501 info->has_integrity_table ? T("yes") : T("no"));
2502 tprintf(T("Relative path junction: %"TS"\n"),
2503 info->has_rpfix ? T("yes") : T("no"));
2508 print_resource(const struct wimlib_resource_entry *resource,
2512 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
2513 resource->uncompressed_size);
2515 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
2516 resource->compressed_size);
2518 tprintf(T("Offset = %"PRIu64" bytes\n"),
2522 tprintf(T("Part Number = %u\n"), resource->part_number);
2523 tprintf(T("Reference Count = %u\n"), resource->reference_count);
2525 tprintf(T("Hash = 0x"));
2526 print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2529 tprintf(T("Flags = "));
2530 if (resource->is_compressed)
2531 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED "));
2532 if (resource->is_metadata)
2533 tprintf(T("WIM_RESHDR_FLAG_METADATA "));
2534 if (resource->is_free)
2535 tprintf(T("WIM_RESHDR_FLAG_FREE "));
2536 if (resource->is_spanned)
2537 tprintf(T("WIM_RESHDR_FLAG_SPANNED "));
2544 print_lookup_table(WIMStruct *wim)
2546 wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2549 /* Prints information about a WIM file; also can mark an image as bootable,
2550 * change the name of an image, or change the description of an image. */
2552 imagex_info(int argc, tchar **argv)
2557 bool header = false;
2558 bool lookup_table = false;
2560 bool metadata = false;
2561 bool short_header = true;
2562 const tchar *xml_out_file = NULL;
2563 const tchar *wimfile;
2564 const tchar *image_num_or_name;
2565 const tchar *new_name;
2566 const tchar *new_desc;
2570 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2571 struct wimlib_wim_info info;
2573 for_opt(c, info_options) {
2575 case IMAGEX_BOOT_OPTION:
2578 case IMAGEX_CHECK_OPTION:
2581 case IMAGEX_HEADER_OPTION:
2583 short_header = false;
2585 case IMAGEX_LOOKUP_TABLE_OPTION:
2586 lookup_table = true;
2587 short_header = false;
2589 case IMAGEX_XML_OPTION:
2591 short_header = false;
2593 case IMAGEX_EXTRACT_XML_OPTION:
2594 xml_out_file = optarg;
2595 short_header = false;
2597 case IMAGEX_METADATA_OPTION:
2599 short_header = false;
2608 if (argc < 1 || argc > 4)
2612 image_num_or_name = (argc >= 2) ? argv[1] : T("all");
2613 new_name = (argc >= 3) ? argv[2] : NULL;
2614 new_desc = (argc >= 4) ? argv[3] : NULL;
2617 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2619 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2623 wimlib_get_wim_info(wim, &info);
2625 image = wimlib_resolve_image(wim, image_num_or_name);
2626 ret = WIMLIB_ERR_INVALID_IMAGE;
2627 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
2628 imagex_error(T("The image \"%"TS"\" does not exist in \"%"TS"\""),
2629 image_num_or_name, wimfile);
2631 imagex_error(T("If you would like to set the boot "
2632 "index to 0, specify image \"0\" with "
2633 "the --boot flag."));
2635 goto out_wimlib_free;
2638 if (boot && info.image_count == 0) {
2639 imagex_error(T("--boot is meaningless on a WIM with no images"));
2640 goto out_wimlib_free;
2643 if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
2645 imagex_error(T("Cannot specify the --boot flag "
2646 "without specifying a specific "
2647 "image in a multi-image WIM"));
2648 goto out_wimlib_free;
2651 imagex_error(T("Cannot specify the NEW_NAME "
2652 "without specifying a specific "
2653 "image in a multi-image WIM"));
2654 goto out_wimlib_free;
2658 /* Operations that print information are separated from operations that
2659 * recreate the WIM file. */
2660 if (!new_name && !boot) {
2662 /* Read-only operations */
2664 if (image == WIMLIB_NO_IMAGE) {
2665 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
2666 image_num_or_name, wimfile);
2667 goto out_wimlib_free;
2670 if (image == WIMLIB_ALL_IMAGES && short_header)
2671 print_wim_information(wimfile, &info);
2674 wimlib_print_header(wim);
2677 if (info.total_parts != 1) {
2678 tfprintf(stderr, T("Warning: Only showing the lookup table "
2679 "for part %d of a %d-part WIM.\n"),
2680 info.part_number, info.total_parts);
2682 print_lookup_table(wim);
2686 ret = wimlib_extract_xml_data(wim, stdout);
2688 goto out_wimlib_free;
2694 fp = tfopen(xml_out_file, T("wb"));
2696 imagex_error_with_errno(T("Failed to open the "
2697 "file \"%"TS"\" for "
2701 goto out_wimlib_free;
2703 ret = wimlib_extract_xml_data(wim, fp);
2705 imagex_error(T("Failed to close the file "
2711 goto out_wimlib_free;
2715 wimlib_print_available_images(wim, image);
2718 ret = wimlib_print_metadata(wim, image);
2720 goto out_wimlib_free;
2725 /* Modification operations */
2727 if (image == WIMLIB_ALL_IMAGES)
2730 if (image == WIMLIB_NO_IMAGE && new_name) {
2731 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
2732 "when using image 0"), new_name);
2734 goto out_wimlib_free;
2738 if (image == info.boot_index) {
2739 tprintf(T("Image %d is already marked as "
2740 "bootable.\n"), image);
2743 tprintf(T("Marking image %d as bootable.\n"),
2745 info.boot_index = image;
2746 ret = wimlib_set_wim_info(wim, &info,
2747 WIMLIB_CHANGE_BOOT_INDEX);
2749 goto out_wimlib_free;
2753 if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
2755 tprintf(T("Image %d is already named \"%"TS"\".\n"),
2759 tprintf(T("Changing the name of image %d to "
2760 "\"%"TS"\".\n"), image, new_name);
2761 ret = wimlib_set_image_name(wim, image, new_name);
2763 goto out_wimlib_free;
2767 const tchar *old_desc;
2768 old_desc = wimlib_get_image_description(wim, image);
2769 if (old_desc && !tstrcmp(old_desc, new_desc)) {
2770 tprintf(T("The description of image %d is already "
2771 "\"%"TS"\".\n"), image, new_desc);
2774 tprintf(T("Changing the description of image %d "
2775 "to \"%"TS"\".\n"), image, new_desc);
2776 ret = wimlib_set_image_descripton(wim, image,
2779 goto out_wimlib_free;
2783 /* Only call wimlib_overwrite() if something actually needs to
2785 if (boot || new_name || new_desc ||
2786 (check && !info.has_integrity_table))
2788 int write_flags = 0;
2791 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2792 ret = wimlib_overwrite(wim, write_flags, 1,
2793 imagex_progress_func);
2795 tprintf(T("The file \"%"TS"\" was not modified because nothing "
2796 "needed to be done.\n"), wimfile);
2811 /* Join split WIMs into one part WIM */
2813 imagex_join(int argc, tchar **argv)
2816 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2817 int wim_write_flags = 0;
2818 const tchar *output_path;
2821 for_opt(c, join_options) {
2823 case IMAGEX_CHECK_OPTION:
2824 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2825 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2835 imagex_error(T("Must specify one or more split WIM (.swm) "
2839 output_path = argv[0];
2840 ret = wimlib_join((const tchar * const *)++argv,
2845 imagex_progress_func);
2855 /* Mounts an image using a FUSE mount. */
2857 imagex_mount_rw_or_ro(int argc, tchar **argv)
2860 int mount_flags = 0;
2861 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2862 const tchar *swm_glob = NULL;
2863 const tchar *staging_dir = NULL;
2864 const tchar *wimfile;
2869 WIMStruct **additional_swms;
2870 unsigned num_additional_swms;
2872 if (!tstrcmp(argv[0], T("mountrw"))) {
2873 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
2874 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2877 for_opt(c, mount_options) {
2879 case IMAGEX_ALLOW_OTHER_OPTION:
2880 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
2882 case IMAGEX_CHECK_OPTION:
2883 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2885 case IMAGEX_DEBUG_OPTION:
2886 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
2888 case IMAGEX_STREAMS_INTERFACE_OPTION:
2889 if (!tstrcasecmp(optarg, T("none")))
2890 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
2891 else if (!tstrcasecmp(optarg, T("xattr")))
2892 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2893 else if (!tstrcasecmp(optarg, T("windows")))
2894 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
2896 imagex_error(T("Unknown stream interface \"%"TS"\""),
2901 case IMAGEX_REF_OPTION:
2904 case IMAGEX_STAGING_DIR_OPTION:
2905 staging_dir = optarg;
2907 case IMAGEX_UNIX_DATA_OPTION:
2908 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
2916 if (argc != 2 && argc != 3)
2921 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2926 /* Image explicitly specified. */
2927 image = wimlib_resolve_image(wim, argv[1]);
2929 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
2933 /* No image specified; default to image 1, but only if the WIM
2934 * contains exactly one image. */
2935 struct wimlib_wim_info info;
2937 wimlib_get_wim_info(wim, &info);
2938 if (info.image_count != 1) {
2939 imagex_error(T("\"%"TS"\" contains %d images; Please "
2940 "select one."), wimfile, info.image_count);
2949 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2951 &num_additional_swms);
2955 additional_swms = NULL;
2956 num_additional_swms = 0;
2959 ret = wimlib_mount_image(wim, image, dir, mount_flags, additional_swms,
2960 num_additional_swms, staging_dir);
2962 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
2964 image, wimfile, dir);
2967 for (unsigned i = 0; i < num_additional_swms; i++)
2968 wimlib_free(additional_swms[i]);
2969 free(additional_swms);
2977 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2983 /* Rebuild a WIM file */
2985 imagex_optimize(int argc, tchar **argv)
2988 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2989 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
2992 const tchar *wimfile;
2995 unsigned num_threads = 0;
2997 for_opt(c, optimize_options) {
2999 case IMAGEX_CHECK_OPTION:
3000 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3001 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3003 case IMAGEX_RECOMPRESS_OPTION:
3004 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3006 case IMAGEX_THREADS_OPTION:
3007 num_threads = parse_num_threads(optarg);
3008 if (num_threads == UINT_MAX)
3023 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3027 old_size = file_get_size(wimfile);
3028 tprintf(T("\"%"TS"\" original size: "), wimfile);
3030 tputs(T("Unknown"));
3032 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3034 ret = wimlib_overwrite(wim, write_flags, num_threads,
3035 imagex_progress_func);
3037 goto out_wimlib_free;
3039 new_size = file_get_size(wimfile);
3040 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3042 tputs(T("Unknown"));
3044 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3046 tfputs(T("Space saved: "), stdout);
3047 if (new_size != -1 && old_size != -1) {
3048 tprintf(T("%lld KiB\n"),
3049 ((long long)old_size - (long long)new_size) >> 10);
3051 tputs(T("Unknown"));
3066 /* Split a WIM into a spanned set */
3068 imagex_split(int argc, tchar **argv)
3072 int write_flags = 0;
3073 unsigned long part_size;
3078 for_opt(c, split_options) {
3080 case IMAGEX_CHECK_OPTION:
3081 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3082 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3094 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3095 if (tmp == argv[2] || *tmp) {
3096 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3097 imagex_error(T("The part size must be an integer or "
3098 "floating-point number of megabytes."));
3101 ret = wimlib_open_wim(argv[0], open_flags, &wim, imagex_progress_func);
3105 ret = wimlib_split(wim, argv[1], part_size, write_flags, imagex_progress_func);
3117 /* Unmounts a mounted WIM image. */
3119 imagex_unmount(int argc, tchar **argv)
3122 int unmount_flags = 0;
3125 for_opt(c, unmount_options) {
3127 case IMAGEX_COMMIT_OPTION:
3128 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3130 case IMAGEX_CHECK_OPTION:
3131 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3133 case IMAGEX_REBUILD_OPTION:
3134 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3136 case IMAGEX_LAZY_OPTION:
3137 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3148 ret = wimlib_unmount_image(argv[0], unmount_flags,
3149 imagex_progress_func);
3151 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3162 * Add, delete, or rename files in a WIM image.
3165 imagex_update(int argc, tchar **argv)
3167 const tchar *wimfile;
3171 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3172 int write_flags = 0;
3173 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3174 int default_add_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
3175 int default_delete_flags = 0;
3176 unsigned num_threads = 0;
3178 tchar *cmd_file_contents;
3179 size_t cmd_file_nchars;
3180 struct wimlib_update_command *cmds;
3182 tchar *command_str = NULL;
3184 const tchar *config_file = NULL;
3186 struct wimlib_capture_config *config;
3188 for_opt(c, update_options) {
3190 /* Generic or write options */
3191 case IMAGEX_THREADS_OPTION:
3192 num_threads = parse_num_threads(optarg);
3193 if (num_threads == UINT_MAX)
3196 case IMAGEX_CHECK_OPTION:
3197 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3198 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3200 case IMAGEX_REBUILD_OPTION:
3201 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3203 case IMAGEX_COMMAND_OPTION:
3205 imagex_error(T("--command may only be specified "
3206 "one time. Please provide\n"
3207 " the update commands "
3208 "on standard input instead."));
3211 command_str = tstrdup(optarg);
3213 imagex_error(T("Out of memory!"));
3217 /* Default delete options */
3218 case IMAGEX_FORCE_OPTION:
3219 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3221 case IMAGEX_RECURSIVE_OPTION:
3222 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3225 /* Global add option */
3226 case IMAGEX_CONFIG_OPTION:
3227 config_file = optarg;
3230 /* Default add options */
3231 case IMAGEX_VERBOSE_OPTION:
3232 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
3234 case IMAGEX_DEREFERENCE_OPTION:
3235 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
3237 case IMAGEX_UNIX_DATA_OPTION:
3238 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
3240 case IMAGEX_NO_ACLS_OPTION:
3241 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
3243 case IMAGEX_STRICT_ACLS_OPTION:
3244 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
3253 if (argc != 1 && argc != 2)
3257 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3259 goto out_free_command_str;
3262 /* Image explicitly specified. */
3263 image = wimlib_resolve_image(wim, argv[1]);
3264 ret = verify_image_exists_and_is_single(image, argv[1],
3267 goto out_wimlib_free;
3269 /* No image specified; default to image 1, but only if the WIM
3270 * contains exactly one image. */
3271 struct wimlib_wim_info info;
3273 wimlib_get_wim_info(wim, &info);
3274 if (info.image_count != 1) {
3275 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3276 wimfile, info.image_count);
3283 /* Parse capture configuration file if specified */
3287 config_str = file_get_text_contents(config_file, &config_len);
3290 goto out_wimlib_free;
3293 config = alloca(sizeof(*config));
3294 ret = parse_capture_config(&config_str, config_len, config);
3296 goto out_free_config;
3298 config = &default_capture_config;
3301 /* Read update commands from standard input, or the command string if
3304 cmd_file_contents = NULL;
3305 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3308 if (isatty(STDIN_FILENO)) {
3309 tputs(T("Reading update commands from standard input..."));
3310 recommend_man_page(T("update"));
3312 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3313 if (!cmd_file_contents) {
3315 goto out_free_config;
3318 /* Parse the update commands */
3319 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3324 goto out_free_cmd_file_contents;
3327 /* Set default flags and capture config on the update commands */
3328 bool have_add_command = false;
3329 for (size_t i = 0; i < num_cmds; i++) {
3330 switch (cmds[i].op) {
3331 case WIMLIB_UPDATE_OP_ADD:
3332 cmds[i].add.add_flags |= default_add_flags;
3333 cmds[i].add.config = config;
3334 have_add_command = true;
3336 case WIMLIB_UPDATE_OP_DELETE:
3337 cmds[i].delete.delete_flags |= default_delete_flags;
3345 if (have_add_command)
3346 win32_acquire_capture_privileges();
3349 /* Execute the update commands */
3350 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3351 imagex_progress_func);
3353 goto out_release_privs;
3355 /* Overwrite the updated WIM */
3356 ret = wimlib_overwrite(wim, write_flags, num_threads,
3357 imagex_progress_func);
3360 if (have_add_command)
3361 win32_release_capture_privileges();
3364 out_free_cmd_file_contents:
3365 free(cmd_file_contents);
3367 if (config != &default_capture_config) {
3368 free(config->exclusion_pats.pats);
3369 free(config->exclusion_exception_pats.pats);
3374 out_free_command_str:
3382 goto out_free_command_str;
3385 struct imagex_command {
3387 int (*func)(int , tchar **);
3392 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
3393 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
3395 static const struct imagex_command imagex_commands[] = {
3396 {T("append"), imagex_capture_or_append, APPEND},
3397 {T("apply"), imagex_apply, APPLY},
3398 {T("capture"), imagex_capture_or_append, CAPTURE},
3399 {T("delete"), imagex_delete, DELETE},
3400 {T("dir"), imagex_dir, DIR},
3401 {T("export"), imagex_export, EXPORT},
3402 {T("extract"), imagex_extract, EXTRACT},
3403 {T("info"), imagex_info, INFO},
3404 {T("join"), imagex_join, JOIN},
3405 {T("mount"), imagex_mount_rw_or_ro, MOUNT},
3406 {T("mountrw"), imagex_mount_rw_or_ro, MOUNTRW},
3407 {T("optimize"),imagex_optimize, OPTIMIZE},
3408 {T("split"), imagex_split, SPLIT},
3409 {T("unmount"), imagex_unmount, UNMOUNT},
3410 {T("update"), imagex_update, UPDATE},
3416 static const tchar *s =
3418 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
3419 "Copyright (C) 2012, 2013 Eric Biggers\n"
3420 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
3421 "This is free software: you are free to change and redistribute it.\n"
3422 "There is NO WARRANTY, to the extent permitted by law.\n"
3424 "Report bugs to "PACKAGE_BUGREPORT".\n"
3431 help_or_version(int argc, tchar **argv)
3435 const struct imagex_command *cmd;
3437 for (i = 1; i < argc; i++) {
3445 if (!tstrcmp(p, T("help"))) {
3446 for_imagex_command(cmd) {
3447 if (!tstrcmp(cmd->name, argv[1])) {
3455 if (!tstrcmp(p, T("version"))) {
3466 const struct imagex_command *cmd;
3467 tprintf(T("Usage:\n%"TS), usage_strings[cmd_type]);
3468 for_imagex_command(cmd) {
3469 if (cmd->cmd == cmd_type) {
3470 tputc(T('\n'), stdout);
3471 recommend_man_page(cmd->name);
3479 tfputs(T("Usage:\n"), stdout);
3480 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
3481 tprintf(T(" %"TS), usage_strings[i]);
3482 static const tchar *extra =
3484 " "IMAGEX_PROGNAME" --help\n"
3485 " "IMAGEX_PROGNAME" --version\n"
3487 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
3490 tfputs(extra, stdout);
3491 recommend_man_page(T(""));
3494 /* Entry point for wimlib's ImageX implementation. On UNIX the command
3495 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
3496 * something else), while an Windows the command arguments will be UTF-16LE
3497 * encoded 'wchar_t' strings. */
3500 wmain(int argc, wchar_t **argv, wchar_t **envp)
3502 main(int argc, char **argv)
3505 const struct imagex_command *cmd;
3510 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
3511 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
3515 setlocale(LC_ALL, "");
3516 codeset = nl_langinfo(CODESET);
3517 if (!strstr(codeset, "UTF-8") &&
3518 !strstr(codeset, "UTF8") &&
3519 !strstr(codeset, "utf-8") &&
3520 !strstr(codeset, "utf8"))
3523 "WARNING: Running "IMAGEX_PROGNAME" in a UTF-8 locale is recommended!\n"
3524 " Maybe try: `export LANG=en_US.UTF-8'?\n"
3525 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
3526 " to any value to force wimlib to use UTF-8.\n",
3531 #endif /* !__WIN32__ */
3534 imagex_error(T("No command specified"));
3540 /* Handle --help and --version for all commands. Note that this will
3541 * not return if either of these arguments are present. */
3542 help_or_version(argc, argv);
3546 /* The user may like to see more informative error messages. */
3547 wimlib_set_print_errors(true);
3549 /* Do any initializations that the library needs */
3550 ret = wimlib_global_init(init_flags);
3552 goto out_check_status;
3554 /* Search for the function to handle the ImageX subcommand. */
3555 for_imagex_command(cmd) {
3556 if (!tstrcmp(cmd->name, *argv)) {
3557 ret = cmd->func(argc, argv);
3558 goto out_check_write_error;
3562 imagex_error(T("Unrecognized command: `%"TS"'"), argv[0]);
3566 out_check_write_error:
3567 /* For 'wimlib-imagex info' and 'wimlib-imagex dir', data printed to
3568 * standard output is part of the program's actual behavior and not just
3569 * for informational purposes, so we should set a failure exit status if
3570 * there was a write error. */
3571 if (cmd == &imagex_commands[INFO] || cmd == &imagex_commands[DIR]) {
3572 if (ferror(stdout) || fclose(stdout)) {
3573 imagex_error_with_errno(T("error writing to standard output"));
3579 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
3580 * outside of the wimlib library code. 0 indicates success. > 0
3581 * indicates a wimlib error code from which an error message can be
3584 imagex_error(T("Exiting with error code %d:\n"
3586 wimlib_get_error_string(ret));
3587 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
3588 imagex_error_with_errno(T("errno"));
3591 /* Make the library free any resources it's holding (not strictly
3592 * necessary because the process is ending anyway). */
3593 wimlib_global_cleanup();