4 * Use wimlib to create, modify, extract, mount, unmount, or display information
9 * Copyright (C) 2012, 2013 Eric Biggers
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 # include "config.h" /* Need for PACKAGE_VERSION, etc. */
30 #include "wimlib_tchar.h"
50 # include "imagex-win32.h"
51 # define tbasename win32_wbasename
52 # define tglob win32_wglob
53 # define OS_PREFERRED_PATH_SEPARATOR L'\\'
54 # define OS_PREFERRED_PATH_SEPARATOR_STRING L"\\"
58 # include <langinfo.h>
59 # define tbasename basename
61 # define OS_PREFERRED_PATH_SEPARATOR '/'
62 # define OS_PREFERRED_PATH_SEPARATOR_STRING "/"
63 static inline void set_fd_to_binary_mode(int fd)
69 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
71 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
92 static void usage(int cmd_type);
93 static void usage_all(void);
95 static bool imagex_be_quiet = false;
96 static FILE *imagex_info_file;
98 #define imagex_printf(format, ...) \
99 tfprintf(imagex_info_file, format, ##__VA_ARGS__)
102 static const tchar *usage_strings[] = {
105 IMAGEX_PROGNAME" append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
106 " [DESCRIPTION] [--boot] [--check] [--nocheck]\n"
107 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
108 " [--config=FILE] [--threads=NUM_THREADS] [--rebuild]\n"
109 " [--unix-data] [--source-list] [--no-acls]\n"
110 " [--strict-acls] [--rpfix] [--norpfix] [--pipable]\n"
115 IMAGEX_PROGNAME" apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
116 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
117 " [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n"
118 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
119 " [--include-invalid-names]\n"
123 IMAGEX_PROGNAME" capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
124 " [DESCRIPTION] [--boot] [--check] [--nocheck]\n"
125 " [--compress=TYPE] [--flags EDITION_ID] [--verbose]\n"
126 " [--dereference] [--config=FILE]\n"
127 " [--threads=NUM_THREADS] [--unix-data] [--source-list]\n"
128 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
129 " [--pipable] [--not-pipable]\n"
133 IMAGEX_PROGNAME" delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n"
138 IMAGEX_PROGNAME" dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH]\n"
142 IMAGEX_PROGNAME" export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
143 " DEST_WIMFILE [DEST_IMAGE_NAME] [DEST_IMAGE_DESCRIPTION]\n"
144 " [--boot] [--check] [--nocheck] [--compress=TYPE]\n"
145 " [--ref=\"GLOB\"] [--threads=NUM_THREADS] [--rebuild]\n"
146 " [--pipable] [--not-pipable]\n"
150 IMAGEX_PROGNAME" extract WIMFILE (IMAGE_NUM | IMAGE_NAME) [PATH...]\n"
151 " [--check] [--ref=\"GLOB\"] [--verbose] [--unix-data]\n"
152 " [--no-acls] [--strict-acls] [--to-stdout] [--dest-dir=DIR]\n"
153 " [--include-invalid-names]\n"
157 IMAGEX_PROGNAME" info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
158 " [NEW_DESC] [--boot] [--check] [--nocheck] [--header]\n"
159 " [--lookup-table] [--xml] [--extract-xml FILE]\n"
164 IMAGEX_PROGNAME" join [--check] WIMFILE SPLIT_WIM...\n"
168 IMAGEX_PROGNAME" mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
169 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
170 " [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n"
174 IMAGEX_PROGNAME" mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
175 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
176 " [--staging-dir=DIR] [--unix-data] [--allow-other]\n"
180 IMAGEX_PROGNAME" optimize WIMFILE [--check] [--nocheck] [--recompress]\n"
181 " [--threads=NUM_THREADS] [--pipable] [--not-pipable]\n"
185 IMAGEX_PROGNAME" split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n"
189 IMAGEX_PROGNAME" unmount DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
193 IMAGEX_PROGNAME" update WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
194 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
195 " [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
201 recommend_man_page(const tchar *cmd_name)
203 const tchar *format_str;
205 format_str = T("See "IMAGEX_PROGNAME"%"TS"%"TS".pdf in the "
206 "doc directory for more details.\n");
208 format_str = T("Try `man "IMAGEX_PROGNAME"%"TS"%"TS"' "
209 "for more details.\n");
211 tprintf(format_str, *cmd_name ? T("-") : T(""), cmd_name);
215 IMAGEX_ALLOW_OTHER_OPTION,
218 IMAGEX_COMMAND_OPTION,
219 IMAGEX_COMMIT_OPTION,
220 IMAGEX_COMPRESS_OPTION,
221 IMAGEX_CONFIG_OPTION,
223 IMAGEX_DEREFERENCE_OPTION,
224 IMAGEX_DEST_DIR_OPTION,
225 IMAGEX_EXTRACT_XML_OPTION,
228 IMAGEX_HARDLINK_OPTION,
229 IMAGEX_HEADER_OPTION,
230 IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
232 IMAGEX_LOOKUP_TABLE_OPTION,
233 IMAGEX_METADATA_OPTION,
234 IMAGEX_NORPFIX_OPTION,
235 IMAGEX_NOCHECK_OPTION,
236 IMAGEX_NO_ACLS_OPTION,
237 IMAGEX_NOT_PIPABLE_OPTION,
239 IMAGEX_PIPABLE_OPTION,
240 IMAGEX_REBUILD_OPTION,
241 IMAGEX_RECOMPRESS_OPTION,
242 IMAGEX_RECURSIVE_OPTION,
246 IMAGEX_SOURCE_LIST_OPTION,
247 IMAGEX_STAGING_DIR_OPTION,
248 IMAGEX_STREAMS_INTERFACE_OPTION,
249 IMAGEX_STRICT_ACLS_OPTION,
250 IMAGEX_SYMLINK_OPTION,
251 IMAGEX_THREADS_OPTION,
252 IMAGEX_TO_STDOUT_OPTION,
253 IMAGEX_UNIX_DATA_OPTION,
254 IMAGEX_VERBOSE_OPTION,
258 static const struct option apply_options[] = {
259 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
260 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
261 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
262 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
263 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
264 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
265 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
266 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
267 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
268 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
269 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
270 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
273 static const struct option capture_or_append_options[] = {
274 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
275 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
276 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
277 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
278 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
279 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
280 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
281 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
282 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
283 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
284 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
285 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
286 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
287 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
288 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
289 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
290 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
291 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
292 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
293 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
296 static const struct option delete_options[] = {
297 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
298 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
302 static const struct option dir_options[] = {
303 {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
307 static const struct option export_options[] = {
308 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
309 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
310 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
311 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
312 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
313 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
314 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
315 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
316 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
317 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
321 static const struct option extract_options[] = {
322 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
323 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
324 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
325 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
326 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
327 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
328 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
329 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
330 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
331 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
335 static const struct option info_options[] = {
336 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
337 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
338 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
339 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
340 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
341 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
342 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
343 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
344 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
348 static const struct option join_options[] = {
349 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
353 static const struct option mount_options[] = {
354 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
355 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
356 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
357 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
358 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
359 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
360 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
364 static const struct option optimize_options[] = {
365 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
366 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
367 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
368 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
369 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
370 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
371 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
375 static const struct option split_options[] = {
376 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
380 static const struct option unmount_options[] = {
381 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
382 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
383 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
384 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
388 static const struct option update_options[] = {
389 /* Careful: some of the options here set the defaults for update
390 * commands, but the flags given to an actual update command (and not to
391 * `imagex update' itself are also handled in
392 * update_command_add_option(). */
393 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
394 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
395 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
396 {T("command"), required_argument, NULL, IMAGEX_COMMAND_OPTION},
398 /* Default delete options */
399 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
400 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
402 /* Global add option */
403 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
405 /* Default add options */
406 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
407 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
408 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
409 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
410 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
411 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
417 # define _format_attribute(type, format_str, args_start) \
418 __attribute__((format(type, format_str, args_start)))
420 # define _format_attribute(type, format_str, args_start)
423 /* Print formatted error message to stderr. */
424 static void _format_attribute(printf, 1, 2)
425 imagex_error(const tchar *format, ...)
428 va_start(va, format);
429 tfputs(T("ERROR: "), stderr);
430 tvfprintf(stderr, format, va);
431 tputc(T('\n'), stderr);
435 /* Print formatted error message to stderr. */
436 static void _format_attribute(printf, 1, 2)
437 imagex_error_with_errno(const tchar *format, ...)
439 int errno_save = errno;
441 va_start(va, format);
442 tfputs(T("ERROR: "), stderr);
443 tvfprintf(stderr, format, va);
444 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
449 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
451 if (image == WIMLIB_NO_IMAGE) {
452 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
453 " Please specify a 1-based image index or "
455 " You may use `"IMAGEX_PROGNAME" info' to list the images "
456 "contained in a WIM."),
457 image_name, wim_name);
464 verify_image_is_single(int image)
466 if (image == WIMLIB_ALL_IMAGES) {
467 imagex_error(T("Cannot specify all images for this action!"));
474 verify_image_exists_and_is_single(int image, const tchar *image_name,
475 const tchar *wim_name)
478 ret = verify_image_exists(image, image_name, wim_name);
480 ret = verify_image_is_single(image);
484 /* Parse the argument to --compress */
486 get_compression_type(const tchar *optarg)
488 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
489 return WIMLIB_COMPRESSION_TYPE_LZX;
490 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
491 return WIMLIB_COMPRESSION_TYPE_XPRESS;
492 else if (!tstrcasecmp(optarg, T("none")))
493 return WIMLIB_COMPRESSION_TYPE_NONE;
495 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
496 "\"maximum\", \"fast\", or \"none\"."), optarg);
497 return WIMLIB_COMPRESSION_TYPE_INVALID;
501 /* Returns the size of a file given its name, or -1 if the file does not exist
502 * or its size cannot be determined. */
504 file_get_size(const tchar *filename)
507 if (tstat(filename, &st) == 0)
513 tchar pat_ntfs_log[] = T("/$ntfs.log");
514 tchar pat_hiberfil_sys[] = T("/hiberfil.sys");
515 tchar pat_pagefile_sys[] = T("/pagefile.sys");
516 tchar pat_system_volume_information[] = T("/System Volume Information");
517 tchar pat_recycler[] = T("/RECYCLER");
518 tchar pat_windows_csc[] = T("/Windows/CSC");
520 tchar *default_pats[] = {
524 pat_system_volume_information,
529 static struct wimlib_capture_config default_capture_config = {
531 .num_pats = sizeof(default_pats) / sizeof(default_pats[0]),
532 .pats = default_pats,
537 PARSE_STRING_SUCCESS = 0,
538 PARSE_STRING_FAILURE = 1,
539 PARSE_STRING_NONE = 2,
543 * Parses a string token from an array of characters.
545 * Tokens are either whitespace-delimited, or double or single-quoted.
547 * @line_p: Pointer to the pointer to the line of data. Will be updated
548 * to point past the string token iff the return value is
549 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
552 * @len_p: @len_p initially stores the length of the line of data, which may
553 * be 0, and it will be updated to the number of bytes remaining in
554 * the line iff the return value is PARSE_STRING_SUCCESS.
556 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
557 * parsed string token will be returned here.
559 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
560 * PARSE_STRING_FAILURE if the data was invalid due to a missing
561 * closing quote; or PARSE_STRING_NONE if the line ended before the
562 * beginning of a string token was found.
565 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
568 tchar *line = *line_p;
572 /* Skip leading whitespace */
575 return PARSE_STRING_NONE;
576 if (!istspace(*line) && *line != T('\0'))
582 if (quote_char == T('"') || quote_char == T('\'')) {
587 line = tmemchr(line, quote_char, len);
589 imagex_error(T("Missing closing quote: %"TS), fn - 1);
590 return PARSE_STRING_FAILURE;
593 /* Unquoted string. Go until whitespace. Line is terminated
594 * by '\0', so no need to check 'len'. */
598 } while (!istspace(*line) && *line != T('\0'));
605 return PARSE_STRING_SUCCESS;
608 /* Parses a line of data (not an empty line or comment) in the source list file
609 * format. (See the man page for 'wimlib-imagex capture' for details on this
610 * format and the meaning.)
612 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
613 * len == 0. The data in @line will be modified by this function call.
615 * @len: Length of the line of data.
617 * @source: On success, the capture source and target described by the line is
618 * written into this destination. Note that it will contain pointers
619 * to data in the @line array.
621 * Returns true if the line was valid; false otherwise. */
623 parse_source_list_line(tchar *line, size_t len,
624 struct wimlib_capture_source *source)
628 ret = parse_string(&line, &len, &source->fs_source_path);
629 if (ret != PARSE_STRING_SUCCESS)
631 ret = parse_string(&line, &len, &source->wim_target_path);
632 if (ret == PARSE_STRING_NONE)
633 source->wim_target_path = source->fs_source_path;
634 return ret != PARSE_STRING_FAILURE;
637 /* Returns %true if the given line of length @len > 0 is a comment or empty line
638 * in the source list file format. */
640 is_comment_line(const tchar *line, size_t len)
645 if (!istspace(*line) && *line != T('\0'))
655 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
658 tchar *contents = *contents_p;
659 size_t nchars = *nchars_p;
662 for (i = 0; i < nchars; i++)
663 if (contents[i] == T('\n'))
666 /* Handle last line not terminated by a newline */
667 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
668 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
670 imagex_error(T("Out of memory!"));
673 contents[nchars] = T('\n');
674 *contents_p = contents;
682 /* Parses a file in the source list format. (See the man page for
683 * 'wimlib-imagex capture' for details on this format and the meaning.)
685 * @source_list_contents: Contents of the source list file. Note that this
686 * buffer will be modified to save memory allocations,
687 * and cannot be freed until the returned array of
688 * wimlib_capture_source's has also been freed.
690 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
693 * @nsources_ret: On success, the length of the returned array is
696 * Returns: An array of `struct wimlib_capture_source's that can be passed to
697 * the wimlib_add_image_multisource() function to specify how a WIM image is to
699 static struct wimlib_capture_source *
700 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
701 size_t *nsources_ret)
705 struct wimlib_capture_source *sources;
708 nlines = text_file_count_lines(source_list_contents_p,
709 &source_list_nchars);
713 /* Always allocate at least 1 slot, just in case the implementation of
714 * calloc() returns NULL if 0 bytes are requested. */
715 sources = calloc(nlines ?: 1, sizeof(*sources));
717 imagex_error(T("out of memory"));
720 p = *source_list_contents_p;
722 for (i = 0; i < nlines; i++) {
723 /* XXX: Could use rawmemchr() here instead, but it may not be
724 * available on all platforms. */
725 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
726 size_t len = endp - p + 1;
728 if (!is_comment_line(p, len)) {
729 if (!parse_source_list_line(p, len, &sources[j++])) {
742 enum capture_config_section {
743 CAPTURE_CONFIG_NO_SECTION,
744 CAPTURE_CONFIG_EXCLUSION_SECTION,
745 CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
746 CAPTURE_CONFIG_IGNORE_SECTION,
750 CAPTURE_CONFIG_INVALID_SECTION,
751 CAPTURE_CONFIG_CHANGED_SECTION,
752 CAPTURE_CONFIG_SAME_SECTION,
756 check_config_section(tchar *line, size_t len,
757 enum capture_config_section *cur_section)
759 while (istspace(*line))
763 return CAPTURE_CONFIG_SAME_SECTION;
766 tchar *endbrace = tstrrchr(line, T(']'));
768 return CAPTURE_CONFIG_SAME_SECTION;
770 if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
771 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
772 } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
773 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
774 } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
775 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
776 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
777 "of capture config file\n"),
779 } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
780 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
781 tfputs(T("WARNING: Ignoring [AlignmentList] section "
782 "of capture config file\n"),
785 imagex_error(T("Invalid capture config file section \"%"TS"\""),
787 return CAPTURE_CONFIG_INVALID_SECTION;
789 return CAPTURE_CONFIG_CHANGED_SECTION;
794 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
797 if (pat_list->num_pats == pat_list->num_allocated_pats) {
799 size_t num_allocated_pats = pat_list->num_pats + 8;
801 pats = realloc(pat_list->pats,
802 num_allocated_pats * sizeof(pat_list->pats[0]));
804 imagex_error(T("Out of memory!"));
807 pat_list->pats = pats;
808 pat_list->num_allocated_pats = num_allocated_pats;
810 pat_list->pats[pat_list->num_pats++] = pat;
815 parse_capture_config_line(tchar *line, size_t len,
816 enum capture_config_section *cur_section,
817 struct wimlib_capture_config *config)
822 ret = check_config_section(line, len, cur_section);
823 if (ret == CAPTURE_CONFIG_INVALID_SECTION)
825 if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
828 switch (*cur_section) {
829 case CAPTURE_CONFIG_NO_SECTION:
830 imagex_error(T("Line \"%"TS"\" is not in a section "
831 "(such as [ExclusionList]"), line);
833 case CAPTURE_CONFIG_EXCLUSION_SECTION:
834 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
836 return pattern_list_add_pattern(&config->exclusion_pats,
838 case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
839 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
841 return pattern_list_add_pattern(&config->exclusion_exception_pats,
843 case CAPTURE_CONFIG_IGNORE_SECTION:
850 parse_capture_config(tchar **contents_p, size_t nchars,
851 struct wimlib_capture_config *config)
856 enum capture_config_section cur_section;
858 memset(config, 0, sizeof(*config));
860 nlines = text_file_count_lines(contents_p, &nchars);
864 cur_section = CAPTURE_CONFIG_NO_SECTION;
866 for (i = 0; i < nlines; i++) {
867 tchar *endp = tmemchr(p, T('\n'), nchars);
868 size_t len = endp - p + 1;
870 if (!is_comment_line(p, len))
871 if (!parse_capture_config_line(p, len, &cur_section, config))
879 /* Reads the contents of a file into memory. */
881 file_get_contents(const tchar *filename, size_t *len_ret)
888 if (tstat(filename, &stbuf) != 0) {
889 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
894 fp = tfopen(filename, T("rb"));
896 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
900 buf = malloc(len ? len : 1);
902 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
903 "contents of file \"%"TS"\""), len, filename);
906 if (fread(buf, 1, len, fp) != len) {
907 imagex_error_with_errno(T("Failed to read %zu bytes from the "
908 "file \"%"TS"\""), len, filename);
922 /* Read standard input until EOF and return the full contents in a malloc()ed
923 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
926 stdin_get_contents(size_t *len_ret)
928 /* stdin can, of course, be a pipe or other non-seekable file, so the
929 * total length of the data cannot be pre-determined */
931 size_t newlen = 1024;
935 char *p = realloc(buf, newlen);
936 size_t bytes_read, bytes_to_read;
938 imagex_error(T("out of memory while reading stdin"));
942 bytes_to_read = newlen - pos;
943 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
945 if (bytes_read != bytes_to_read) {
950 imagex_error_with_errno(T("error reading stdin"));
964 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
967 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
969 *num_tchars_ret = num_bytes;
971 #else /* !__WIN32__ */
972 /* On Windows, translate the text to UTF-16LE */
976 if (num_bytes >= 2 &&
977 ((text[0] == 0xff && text[1] == 0xfe) ||
978 (text[0] <= 0x7f && text[1] == 0x00)))
980 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
981 * with something that looks like an ASCII character encoded as
982 * a UTF-16LE code unit. Assume the file is encoded as
983 * UTF-16LE. This is not a 100% reliable check. */
984 num_wchars = num_bytes / 2;
985 text_wstr = (wchar_t*)text;
987 /* File does not look like UTF-16LE. Assume it is encoded in
988 * the current Windows code page. I think these are always
989 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
990 * should work as expected. */
991 text_wstr = win32_mbs_to_wcs(text,
996 *num_tchars_ret = num_wchars;
998 #endif /* __WIN32__ */
1002 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
1007 contents = file_get_contents(filename, &num_bytes);
1010 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
1014 stdin_get_text_contents(size_t *num_tchars_ret)
1019 contents = stdin_get_contents(&num_bytes);
1022 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
1025 #define TO_PERCENT(numerator, denominator) \
1026 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
1028 /* Given an enumerated value for WIM compression type, return a descriptive
1030 static const tchar *
1031 get_data_type(int ctype)
1034 case WIMLIB_COMPRESSION_TYPE_NONE:
1035 return T("uncompressed");
1036 case WIMLIB_COMPRESSION_TYPE_LZX:
1037 return T("LZX-compressed");
1038 case WIMLIB_COMPRESSION_TYPE_XPRESS:
1039 return T("XPRESS-compressed");
1044 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
1045 #define MEBIBYTE_MIN_NBYTES 10000000ULL
1046 #define KIBIBYTE_MIN_NBYTES 10000ULL
1049 get_unit(uint64_t total_bytes, const tchar **name_ret)
1051 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
1052 *name_ret = T("GiB");
1054 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
1055 *name_ret = T("MiB");
1057 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
1058 *name_ret = T("KiB");
1061 *name_ret = T("bytes");
1066 /* Progress callback function passed to various wimlib functions. */
1068 imagex_progress_func(enum wimlib_progress_msg msg,
1069 const union wimlib_progress_info *info)
1071 unsigned percent_done;
1072 unsigned unit_shift;
1073 const tchar *unit_name;
1074 if (imagex_be_quiet)
1077 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1078 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1079 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1080 info->write_streams.total_bytes);
1081 if (info->write_streams.completed_streams == 0) {
1082 const tchar *data_type;
1084 data_type = get_data_type(info->write_streams.compression_type);
1085 imagex_printf(T("Writing %"TS" data using %u thread%"TS"\n"),
1086 data_type, info->write_streams.num_threads,
1087 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1089 imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1090 "written (%u%% done)"),
1091 info->write_streams.completed_bytes >> unit_shift,
1093 info->write_streams.total_bytes >> unit_shift,
1096 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1097 imagex_printf(T("\n"));
1099 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1100 imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
1101 if (*info->scan.wim_target_path) {
1102 imagex_printf(T(" (loading as WIM path: "
1103 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
1104 info->scan.wim_target_path);
1106 imagex_printf(T(" (loading as root of WIM image)...\n"));
1109 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1110 if (info->scan.excluded)
1111 imagex_printf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1113 imagex_printf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
1115 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
1117 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1118 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1119 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1120 info->integrity.total_bytes);
1121 imagex_printf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1122 "of %"PRIu64" %"TS" (%u%%) done"),
1123 info->integrity.filename,
1124 info->integrity.completed_bytes >> unit_shift,
1126 info->integrity.total_bytes >> unit_shift,
1129 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1130 imagex_printf(T("\n"));
1132 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1133 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1134 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1135 info->integrity.total_bytes);
1136 imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1137 "of %"PRIu64" %"TS" (%u%%) done"),
1138 info->integrity.completed_bytes >> unit_shift,
1140 info->integrity.total_bytes >> unit_shift,
1143 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1144 imagex_printf(T("\n"));
1146 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1147 imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1148 "to %"TS" \"%"TS"\"\n"),
1149 info->extract.image,
1150 info->extract.image_name,
1151 info->extract.wimfile_name,
1152 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1153 T("NTFS volume") : T("directory")),
1154 info->extract.target);
1156 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1157 imagex_printf(T("Extracting "
1158 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\" from image %d (\"%"TS"\") "
1159 "in \"%"TS"\" to \"%"TS"\"\n"),
1160 info->extract.extract_root_wim_source_path,
1161 info->extract.image,
1162 info->extract.image_name,
1163 info->extract.wimfile_name,
1164 info->extract.target);
1166 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
1167 /*imagex_printf(T("Applying directory structure to %"TS"\n"),*/
1168 /*info->extract.target);*/
1170 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1171 percent_done = TO_PERCENT(info->extract.completed_bytes,
1172 info->extract.total_bytes);
1173 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1174 imagex_printf(T("\rExtracting files: "
1175 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1176 info->extract.completed_bytes >> unit_shift,
1178 info->extract.total_bytes >> unit_shift,
1181 if (info->extract.completed_bytes >= info->extract.total_bytes)
1182 imagex_printf(T("\n"));
1184 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1185 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1186 imagex_printf(T("Setting timestamps on all extracted files...\n"));
1188 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1189 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1190 imagex_printf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1191 info->extract.target);
1194 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
1195 percent_done = TO_PERCENT(info->join.completed_bytes,
1196 info->join.total_bytes);
1197 unit_shift = get_unit(info->join.total_bytes, &unit_name);
1198 imagex_printf(T("Writing resources from part %u of %u: "
1199 "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written\n"),
1200 (info->join.completed_parts == info->join.total_parts) ?
1201 info->join.completed_parts : info->join.completed_parts + 1,
1202 info->join.total_parts,
1203 info->join.completed_bytes >> unit_shift,
1205 info->join.total_bytes >> unit_shift,
1209 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1210 percent_done = TO_PERCENT(info->split.completed_bytes,
1211 info->split.total_bytes);
1212 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1213 imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
1214 "%"PRIu64" %"TS" (%u%%) written\n"),
1215 info->split.part_name,
1216 info->split.cur_part_number,
1217 info->split.total_parts,
1218 info->split.completed_bytes >> unit_shift,
1220 info->split.total_bytes >> unit_shift,
1224 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1225 if (info->split.completed_bytes == info->split.total_bytes) {
1226 imagex_printf(T("Finished writing part %u of %u WIM parts\n"),
1227 info->split.cur_part_number,
1228 info->split.total_parts);
1231 case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1232 switch (info->update.command->op) {
1233 case WIMLIB_UPDATE_OP_DELETE:
1234 imagex_printf(T("Deleted WIM path "
1235 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1236 info->update.command->delete.wim_path);
1238 case WIMLIB_UPDATE_OP_RENAME:
1239 imagex_printf(T("Renamed WIM path "
1240 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
1241 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1242 info->update.command->rename.wim_source_path,
1243 info->update.command->rename.wim_target_path);
1245 case WIMLIB_UPDATE_OP_ADD:
1257 /* Open all the split WIM parts that correspond to a file glob.
1259 * @first_part specifies the first part of the split WIM and it may be either
1260 * included or omitted from the glob. */
1262 open_swms_from_glob(const tchar *swm_glob,
1263 const tchar *first_part,
1265 WIMStruct ***additional_swms_ret,
1266 unsigned *num_additional_swms_ret)
1268 unsigned num_additional_swms = 0;
1269 WIMStruct **additional_swms = NULL;
1273 /* Warning: glob() is replaced in Windows native builds */
1274 ret = tglob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
1276 if (ret == GLOB_NOMATCH) {
1277 imagex_error(T("Found no files for glob \"%"TS"\""),
1280 imagex_error_with_errno(T("Failed to process glob \"%"TS"\""),
1286 num_additional_swms = globbuf.gl_pathc;
1287 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
1288 if (!additional_swms) {
1289 imagex_error(T("Out of memory"));
1293 unsigned offset = 0;
1294 for (unsigned i = 0; i < num_additional_swms; i++) {
1295 if (tstrcmp(globbuf.gl_pathv[i], first_part) == 0) {
1299 ret = wimlib_open_wim(globbuf.gl_pathv[i],
1300 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
1301 &additional_swms[i - offset],
1302 imagex_progress_func);
1304 goto out_close_swms;
1306 *additional_swms_ret = additional_swms;
1307 *num_additional_swms_ret = num_additional_swms - offset;
1311 for (unsigned i = 0; i < num_additional_swms; i++)
1312 wimlib_free(additional_swms[i]);
1313 free(additional_swms);
1322 parse_num_threads(const tchar *optarg)
1325 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1326 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1327 imagex_error(T("Number of threads must be a non-negative integer!"));
1335 * Parse an option passed to an update command.
1337 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1340 * @option: Text string for the option (beginning with --)
1342 * @cmd: `struct wimlib_update_command' that is being constructed for
1345 * Returns true if the option was recognized; false if not.
1348 update_command_add_option(int op, const tchar *option,
1349 struct wimlib_update_command *cmd)
1351 bool recognized = true;
1353 case WIMLIB_UPDATE_OP_ADD:
1354 if (!tstrcmp(option, T("--verbose")))
1355 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1356 else if (!tstrcmp(option, T("--unix-data")))
1357 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1358 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1359 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1360 else if (!tstrcmp(option, T("--strict-acls")))
1361 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1362 else if (!tstrcmp(option, T("--dereference")))
1363 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1367 case WIMLIB_UPDATE_OP_DELETE:
1368 if (!tstrcmp(option, T("--force")))
1369 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1370 else if (!tstrcmp(option, T("--recursive")))
1371 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1382 /* How many nonoption arguments each `imagex update' command expects */
1383 static const unsigned update_command_num_nonoptions[] = {
1384 [WIMLIB_UPDATE_OP_ADD] = 2,
1385 [WIMLIB_UPDATE_OP_DELETE] = 1,
1386 [WIMLIB_UPDATE_OP_RENAME] = 2,
1390 update_command_add_nonoption(int op, const tchar *nonoption,
1391 struct wimlib_update_command *cmd,
1392 unsigned num_nonoptions)
1395 case WIMLIB_UPDATE_OP_ADD:
1396 if (num_nonoptions == 0)
1397 cmd->add.fs_source_path = (tchar*)nonoption;
1399 cmd->add.wim_target_path = (tchar*)nonoption;
1401 case WIMLIB_UPDATE_OP_DELETE:
1402 cmd->delete.wim_path = (tchar*)nonoption;
1404 case WIMLIB_UPDATE_OP_RENAME:
1405 if (num_nonoptions == 0)
1406 cmd->rename.wim_source_path = (tchar*)nonoption;
1408 cmd->rename.wim_target_path = (tchar*)nonoption;
1414 * Parse a command passed on stdin to `imagex update'.
1416 * @line: Text of the command.
1417 * @len: Length of the line, including a null terminator
1420 * @command: A `struct wimlib_update_command' to fill in from the parsed
1423 * @line_number: Line number of the command, for diagnostics.
1425 * Returns true on success; returns false on parse error.
1428 parse_update_command(tchar *line, size_t len,
1429 struct wimlib_update_command *command,
1433 tchar *command_name;
1435 size_t num_nonoptions;
1437 /* Get the command name ("add", "delete", "rename") */
1438 ret = parse_string(&line, &len, &command_name);
1439 if (ret != PARSE_STRING_SUCCESS)
1442 if (!tstrcasecmp(command_name, T("add"))) {
1443 op = WIMLIB_UPDATE_OP_ADD;
1444 } else if (!tstrcasecmp(command_name, T("delete"))) {
1445 op = WIMLIB_UPDATE_OP_DELETE;
1446 } else if (!tstrcasecmp(command_name, T("rename"))) {
1447 op = WIMLIB_UPDATE_OP_RENAME;
1449 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1450 command_name, line_number);
1455 /* Parse additional options and non-options as needed */
1460 ret = parse_string(&line, &len, &next_string);
1461 if (ret == PARSE_STRING_NONE) /* End of line */
1463 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1465 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1467 if (!update_command_add_option(op, next_string, command))
1469 imagex_error(T("Unrecognized option \"%"TS"\" to "
1470 "update command \"%"TS"\" on line %zu"),
1471 next_string, command_name, line_number);
1477 if (num_nonoptions == update_command_num_nonoptions[op])
1479 imagex_error(T("Unexpected argument \"%"TS"\" in "
1480 "update command on line %zu\n"
1481 " (The \"%"TS"\" command only "
1482 "takes %zu nonoption arguments!)\n"),
1483 next_string, line_number,
1484 command_name, num_nonoptions);
1487 update_command_add_nonoption(op, next_string,
1488 command, num_nonoptions);
1493 if (num_nonoptions != update_command_num_nonoptions[op]) {
1494 imagex_error(T("Not enough arguments to update command "
1495 "\"%"TS"\" on line %zu"), command_name, line_number);
1501 static struct wimlib_update_command *
1502 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1503 size_t *num_cmds_ret)
1507 struct wimlib_update_command *cmds;
1510 nlines = text_file_count_lines(cmd_file_contents_p,
1515 /* Always allocate at least 1 slot, just in case the implementation of
1516 * calloc() returns NULL if 0 bytes are requested. */
1517 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1519 imagex_error(T("out of memory"));
1522 p = *cmd_file_contents_p;
1524 for (i = 0; i < nlines; i++) {
1525 /* XXX: Could use rawmemchr() here instead, but it may not be
1526 * available on all platforms. */
1527 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1528 size_t len = endp - p + 1;
1530 if (!is_comment_line(p, len)) {
1531 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1542 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1543 * one image from a WIM file to a NTFS volume. */
1545 imagex_apply(int argc, tchar **argv)
1548 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1552 const tchar *wimfile;
1553 const tchar *target;
1554 const tchar *image_num_or_name;
1555 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1557 const tchar *swm_glob = NULL;
1558 WIMStruct **additional_swms;
1559 unsigned num_additional_swms;
1561 for_opt(c, apply_options) {
1563 case IMAGEX_CHECK_OPTION:
1564 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1566 case IMAGEX_HARDLINK_OPTION:
1567 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1569 case IMAGEX_SYMLINK_OPTION:
1570 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1572 case IMAGEX_VERBOSE_OPTION:
1573 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
1575 case IMAGEX_REF_OPTION:
1578 case IMAGEX_UNIX_DATA_OPTION:
1579 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1581 case IMAGEX_NO_ACLS_OPTION:
1582 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1584 case IMAGEX_STRICT_ACLS_OPTION:
1585 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1587 case IMAGEX_NORPFIX_OPTION:
1588 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1590 case IMAGEX_RPFIX_OPTION:
1591 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1593 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1594 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1595 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1603 if (argc != 2 && argc != 3)
1608 if (!tstrcmp(wimfile, T("-"))) {
1609 /* Attempt to apply pipable WIM from standard input. */
1611 imagex_error(T("Imagex index or name must be explicitly "
1612 "specified when applying pipable WIM on "
1613 "standard input."));
1616 image_num_or_name = argv[1];
1619 num_additional_swms = 0;
1620 additional_swms = NULL;
1622 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1623 imagex_progress_func);
1628 /* Image explicitly specified. */
1629 image_num_or_name = argv[1];
1630 image = wimlib_resolve_image(wim, image_num_or_name);
1631 ret = verify_image_exists(image, image_num_or_name, wimfile);
1633 goto out_wimlib_free;
1636 /* No image specified; default to image 1, but only if the WIM
1637 * contains exactly one image. */
1638 struct wimlib_wim_info info;
1640 wimlib_get_wim_info(wim, &info);
1641 if (info.image_count != 1) {
1642 imagex_error(T("\"%"TS"\" contains %d images; "
1643 "Please select one (or all)."),
1644 wimfile, info.image_count);
1653 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1655 &num_additional_swms);
1657 goto out_wimlib_free;
1659 additional_swms = NULL;
1660 num_additional_swms = 0;
1666 /* Interpret a regular file or block device target as a NTFS
1670 if (tstat(target, &stbuf)) {
1671 if (errno != ENOENT) {
1672 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1678 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1679 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1685 win32_acquire_restore_privileges();
1688 ret = wimlib_extract_image(wim, image, target, extract_flags,
1689 additional_swms, num_additional_swms,
1690 imagex_progress_func);
1692 set_fd_to_binary_mode(STDIN_FILENO);
1693 ret = wimlib_extract_image_from_pipe(STDIN_FILENO,
1695 target, extract_flags,
1696 imagex_progress_func);
1699 imagex_printf(T("Done applying WIM image.\n"));
1701 win32_release_restore_privileges();
1704 for (unsigned i = 0; i < num_additional_swms; i++)
1705 wimlib_free(additional_swms[i]);
1706 free(additional_swms);
1718 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1719 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1720 * the desired image. 'wimlib-imagex append': add a new image to an existing
1723 imagex_capture_or_append(int argc, tchar **argv)
1726 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1727 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
1728 int write_flags = 0;
1729 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
1730 const tchar *wimfile;
1734 const tchar *flags_element = NULL;
1737 int cmd = tstrcmp(argv[0], T("append")) ? CAPTURE : APPEND;
1738 unsigned num_threads = 0;
1743 const tchar *config_file = NULL;
1745 struct wimlib_capture_config *config;
1747 bool source_list = false;
1748 size_t source_list_nchars;
1749 tchar *source_list_contents;
1750 bool capture_sources_malloced;
1751 struct wimlib_capture_source *capture_sources;
1753 bool name_defaulted;
1755 for_opt(c, capture_or_append_options) {
1757 case IMAGEX_BOOT_OPTION:
1758 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1760 case IMAGEX_CHECK_OPTION:
1761 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1762 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1764 case IMAGEX_NOCHECK_OPTION:
1765 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
1767 case IMAGEX_CONFIG_OPTION:
1768 config_file = optarg;
1770 case IMAGEX_COMPRESS_OPTION:
1771 compression_type = get_compression_type(optarg);
1772 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1775 case IMAGEX_FLAGS_OPTION:
1776 flags_element = optarg;
1778 case IMAGEX_DEREFERENCE_OPTION:
1779 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1781 case IMAGEX_VERBOSE_OPTION:
1782 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1784 case IMAGEX_THREADS_OPTION:
1785 num_threads = parse_num_threads(optarg);
1786 if (num_threads == UINT_MAX)
1789 case IMAGEX_REBUILD_OPTION:
1790 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1792 case IMAGEX_UNIX_DATA_OPTION:
1793 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1795 case IMAGEX_SOURCE_LIST_OPTION:
1798 case IMAGEX_NO_ACLS_OPTION:
1799 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1801 case IMAGEX_STRICT_ACLS_OPTION:
1802 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1804 case IMAGEX_RPFIX_OPTION:
1805 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1807 case IMAGEX_NORPFIX_OPTION:
1808 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1810 case IMAGEX_PIPABLE_OPTION:
1811 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1813 case IMAGEX_NOT_PIPABLE_OPTION:
1814 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
1823 if (argc < 2 || argc > 4)
1829 if (!tstrcmp(wimfile, T("-"))) {
1831 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
1832 imagex_error("Can't write a non-pipable WIM to "
1833 "standard output! Specify --pipable\n"
1834 " if you want to create a pipable WIM "
1835 "(but read the docs first).");
1839 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1841 if (cmd == APPEND) {
1842 imagex_error(T("Using standard output for append does "
1843 "not make sense."));
1846 wim_fd = STDOUT_FILENO;
1848 imagex_info_file = stderr;
1853 name_defaulted = false;
1855 /* Set default name to SOURCE argument, omitting any directory
1856 * prefixes and trailing slashes. This requires making a copy
1857 * of @source. Leave some free characters at the end in case we
1858 * append a number to keep the name unique. */
1859 size_t source_name_len;
1861 source_name_len = tstrlen(source);
1862 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1863 name = tbasename(tstrcpy(source_copy, source));
1864 name_defaulted = true;
1866 /* Image description defaults to NULL if not given. */
1873 /* Set up capture sources in source list mode */
1874 if (source[0] == T('-') && source[1] == T('\0')) {
1875 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1877 source_list_contents = file_get_text_contents(source,
1878 &source_list_nchars);
1880 if (!source_list_contents)
1883 capture_sources = parse_source_list(&source_list_contents,
1886 if (!capture_sources) {
1888 goto out_free_source_list_contents;
1890 capture_sources_malloced = true;
1892 /* Set up capture source in non-source-list mode (could be
1893 * either "normal" mode or "NTFS mode"--- see the man page). */
1894 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1895 capture_sources[0].fs_source_path = source;
1896 capture_sources[0].wim_target_path = NULL;
1897 capture_sources[0].reserved = 0;
1899 capture_sources_malloced = false;
1900 source_list_contents = NULL;
1906 config_str = file_get_text_contents(config_file, &config_len);
1909 goto out_free_capture_sources;
1912 config = alloca(sizeof(*config));
1913 ret = parse_capture_config(&config_str, config_len, config);
1915 goto out_free_config;
1917 config = &default_capture_config;
1921 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1922 imagex_progress_func);
1924 ret = wimlib_create_new_wim(compression_type, &wim);
1926 goto out_free_config;
1932 if (tstat(source, &stbuf) == 0) {
1933 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1934 imagex_printf(T("Capturing WIM image from NTFS "
1935 "filesystem on \"%"TS"\"\n"), source);
1936 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1939 if (errno != ENOENT) {
1940 imagex_error_with_errno(T("Failed to stat "
1941 "\"%"TS"\""), source);
1943 goto out_wimlib_free;
1949 if (cmd == APPEND && name_defaulted) {
1950 /* If the user did not specify an image name, and the basename
1951 * of the source already exists as an image name in the WIM
1952 * file, append a suffix to make it unique. */
1953 unsigned long conflict_idx;
1954 tchar *name_end = tstrchr(name, T('\0'));
1955 for (conflict_idx = 1;
1956 wimlib_image_name_in_use(wim, name);
1959 tsprintf(name_end, T(" (%lu)"), conflict_idx);
1963 win32_acquire_capture_privileges();
1966 ret = wimlib_add_image_multisource(wim,
1972 imagex_progress_func);
1974 goto out_release_privs;
1976 if (desc || flags_element) {
1977 /* User provided <DESCRIPTION> or <FLAGS> element. Get the
1978 * index of the image we just added, then use it to call the
1979 * appropriate functions. */
1980 struct wimlib_wim_info info;
1982 wimlib_get_wim_info(wim, &info);
1985 ret = wimlib_set_image_descripton(wim,
1989 goto out_release_privs;
1992 if (flags_element) {
1993 ret = wimlib_set_image_flags(wim, info.image_count,
1996 goto out_release_privs;
2000 /* Write the new WIM or overwrite the existing WIM with the new image
2002 if (cmd == APPEND) {
2003 ret = wimlib_overwrite(wim, write_flags, num_threads,
2004 imagex_progress_func);
2005 } else if (wimfile) {
2006 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
2007 write_flags, num_threads,
2008 imagex_progress_func);
2010 ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
2011 write_flags, num_threads,
2012 imagex_progress_func);
2016 win32_release_capture_privileges();
2021 if (config != &default_capture_config) {
2022 free(config->exclusion_pats.pats);
2023 free(config->exclusion_exception_pats.pats);
2026 out_free_capture_sources:
2027 if (capture_sources_malloced)
2028 free(capture_sources);
2029 out_free_source_list_contents:
2030 free(source_list_contents);
2041 /* Remove image(s) from a WIM. */
2043 imagex_delete(int argc, tchar **argv)
2046 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2047 int write_flags = 0;
2048 const tchar *wimfile;
2049 const tchar *image_num_or_name;
2054 for_opt(c, delete_options) {
2056 case IMAGEX_CHECK_OPTION:
2057 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2058 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2060 case IMAGEX_SOFT_OPTION:
2061 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
2072 imagex_error(T("Must specify a WIM file"));
2074 imagex_error(T("Must specify an image"));
2078 image_num_or_name = argv[1];
2080 ret = wimlib_open_wim(wimfile, open_flags, &wim,
2081 imagex_progress_func);
2085 image = wimlib_resolve_image(wim, image_num_or_name);
2087 ret = verify_image_exists(image, image_num_or_name, wimfile);
2089 goto out_wimlib_free;
2091 ret = wimlib_delete_image(wim, image);
2093 imagex_error(T("Failed to delete image from \"%"TS"\""),
2095 goto out_wimlib_free;
2098 ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
2100 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2101 "deleted"), wimfile);
2115 print_full_path(const struct wimlib_dir_entry *wdentry, void *_ignore)
2117 int ret = tprintf(T("%"TS"\n"), wdentry->full_path);
2118 return (ret >= 0) ? 0 : -1;
2121 /* Print the files contained in an image(s) in a WIM file. */
2123 imagex_dir(int argc, tchar **argv)
2125 const tchar *wimfile;
2126 WIMStruct *wim = NULL;
2129 const tchar *path = T("");
2132 for_opt(c, dir_options) {
2134 case IMAGEX_PATH_OPTION:
2145 imagex_error(T("Must specify a WIM file"));
2149 imagex_error(T("Too many arguments"));
2154 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &wim,
2155 imagex_progress_func);
2160 image = wimlib_resolve_image(wim, argv[1]);
2161 ret = verify_image_exists(image, argv[1], wimfile);
2163 goto out_wimlib_free;
2165 /* No image specified; default to image 1, but only if the WIM
2166 * contains exactly one image. */
2168 struct wimlib_wim_info info;
2170 wimlib_get_wim_info(wim, &info);
2171 if (info.image_count != 1) {
2172 imagex_error(T("\"%"TS"\" contains %d images; Please "
2173 "select one (or all)."),
2174 wimfile, info.image_count);
2181 ret = wimlib_iterate_dir_tree(wim, image, path,
2182 WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE,
2183 print_full_path, NULL);
2195 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2198 imagex_export(int argc, tchar **argv)
2202 int export_flags = 0;
2203 int write_flags = 0;
2204 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2205 const tchar *src_wimfile;
2206 const tchar *src_image_num_or_name;
2207 const tchar *dest_wimfile;
2209 const tchar *dest_name;
2210 const tchar *dest_desc;
2212 WIMStruct *dest_wim;
2217 const tchar *swm_glob = NULL;
2218 WIMStruct **additional_swms;
2219 unsigned num_additional_swms;
2220 unsigned num_threads = 0;
2222 for_opt(c, export_options) {
2224 case IMAGEX_BOOT_OPTION:
2225 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2227 case IMAGEX_CHECK_OPTION:
2228 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2229 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2231 case IMAGEX_NOCHECK_OPTION:
2232 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2234 case IMAGEX_COMPRESS_OPTION:
2235 compression_type = get_compression_type(optarg);
2236 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2239 case IMAGEX_REF_OPTION:
2242 case IMAGEX_THREADS_OPTION:
2243 num_threads = parse_num_threads(optarg);
2244 if (num_threads == UINT_MAX)
2247 case IMAGEX_REBUILD_OPTION:
2248 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2250 case IMAGEX_PIPABLE_OPTION:
2251 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2253 case IMAGEX_NOT_PIPABLE_OPTION:
2254 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
2262 if (argc < 3 || argc > 5)
2264 src_wimfile = argv[0];
2265 src_image_num_or_name = argv[1];
2266 dest_wimfile = argv[2];
2267 dest_name = (argc >= 4) ? argv[3] : NULL;
2268 dest_desc = (argc >= 5) ? argv[4] : NULL;
2269 ret = wimlib_open_wim(src_wimfile,
2270 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_wim,
2271 imagex_progress_func);
2275 /* Determine if the destination is an existing file or not. If so, we
2276 * try to append the exported image(s) to it; otherwise, we create a new
2277 * WIM containing the exported image(s). Furthermore, determine if we
2278 * need to write a pipable WIM directly to standard output. */
2280 if (tstrcmp(dest_wimfile, T("-")) == 0) {
2282 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2283 imagex_error("Can't write a non-pipable WIM to "
2284 "standard output! Specify --pipable\n"
2285 " if you want to create a pipable WIM "
2286 "(but read the docs first).");
2288 goto out_free_src_wim;
2291 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2293 dest_wimfile = NULL;
2294 dest_wim_fd = STDOUT_FILENO;
2295 imagex_info_file = stderr;
2298 if (dest_wimfile != NULL && tstat(dest_wimfile, &stbuf) == 0) {
2300 /* Destination file exists. */
2302 if (!S_ISREG(stbuf.st_mode)) {
2303 imagex_error(T("\"%"TS"\" is not a regular file"),
2306 goto out_free_src_wim;
2308 ret = wimlib_open_wim(dest_wimfile, open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2309 &dest_wim, imagex_progress_func);
2311 goto out_free_src_wim;
2313 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2314 /* The user specified a compression type, but we're
2315 * exporting to an existing WIM. Make sure the
2316 * specified compression type is the same as the
2317 * compression type of the existing destination WIM. */
2318 struct wimlib_wim_info dest_info;
2320 wimlib_get_wim_info(dest_wim, &dest_info);
2321 if (compression_type != dest_info.compression_type) {
2322 imagex_error(T("Cannot specify a compression type that is "
2323 "not the same as that used in the "
2324 "destination WIM"));
2326 goto out_free_dest_wim;
2332 if (errno != ENOENT) {
2333 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2336 goto out_free_src_wim;
2339 /* dest_wimfile is not an existing file, so create a new WIM. */
2341 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2342 /* The user did not specify a compression type; default
2343 * to that of the source WIM. */
2345 struct wimlib_wim_info src_info;
2347 wimlib_get_wim_info(src_wim, &src_info);
2348 compression_type = src_info.compression_type;
2350 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2352 goto out_free_src_wim;
2355 image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2356 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2358 goto out_free_dest_wim;
2361 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
2363 &num_additional_swms);
2365 goto out_free_dest_wim;
2367 additional_swms = NULL;
2368 num_additional_swms = 0;
2371 ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2372 dest_desc, export_flags, additional_swms,
2373 num_additional_swms, imagex_progress_func);
2378 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2379 imagex_progress_func);
2380 else if (dest_wimfile)
2381 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2382 write_flags, num_threads,
2383 imagex_progress_func);
2385 ret = wimlib_write_to_fd(dest_wim, dest_wim_fd,
2386 WIMLIB_ALL_IMAGES, write_flags,
2387 num_threads, imagex_progress_func);
2389 imagex_error(T("Export failed."));
2391 for (unsigned i = 0; i < num_additional_swms; i++)
2392 wimlib_free(additional_swms[i]);
2393 free(additional_swms);
2395 wimlib_free(dest_wim);
2397 wimlib_free(src_wim);
2409 is_root_wim_path(const tchar *path)
2412 for (p = path; *p; p++)
2413 if (*p != T('\\') && *p != T('/'))
2419 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2420 const tchar *dest_dir)
2422 for (size_t i = 0; i < num_cmds; i++)
2423 if (cmds[i].fs_dest_path != dest_dir)
2424 free(cmds[i].fs_dest_path);
2428 static struct wimlib_extract_command *
2429 prepare_extract_commands(tchar **paths, unsigned num_paths,
2430 int extract_flags, tchar *dest_dir,
2431 size_t *num_cmds_ret)
2433 struct wimlib_extract_command *cmds;
2435 tchar *emptystr = T("");
2437 if (num_paths == 0) {
2441 num_cmds = num_paths;
2442 cmds = calloc(num_cmds, sizeof(cmds[0]));
2444 imagex_error(T("Out of memory!"));
2448 for (size_t i = 0; i < num_cmds; i++) {
2449 cmds[i].extract_flags = extract_flags;
2450 cmds[i].wim_source_path = paths[i];
2451 if (is_root_wim_path(paths[i])) {
2452 cmds[i].fs_dest_path = dest_dir;
2454 size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2455 cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2456 if (!cmds[i].fs_dest_path) {
2457 free_extract_commands(cmds, num_cmds, dest_dir);
2460 tsprintf(cmds[i].fs_dest_path,
2461 T("%"TS""OS_PREFERRED_PATH_SEPARATOR_STRING"%"TS),
2462 dest_dir, tbasename(paths[i]));
2465 *num_cmds_ret = num_cmds;
2469 /* Extract files or directories from a WIM image */
2471 imagex_extract(int argc, tchar **argv)
2474 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2478 const tchar *wimfile;
2479 const tchar *image_num_or_name;
2480 tchar *dest_dir = T(".");
2481 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL | WIMLIB_EXTRACT_FLAG_NORPFIX;
2483 const tchar *swm_glob = NULL;
2484 WIMStruct **additional_swms;
2485 unsigned num_additional_swms;
2487 struct wimlib_extract_command *cmds;
2490 for_opt(c, extract_options) {
2492 case IMAGEX_CHECK_OPTION:
2493 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2495 case IMAGEX_VERBOSE_OPTION:
2496 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
2498 case IMAGEX_REF_OPTION:
2501 case IMAGEX_UNIX_DATA_OPTION:
2502 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2504 case IMAGEX_NO_ACLS_OPTION:
2505 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2507 case IMAGEX_STRICT_ACLS_OPTION:
2508 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2510 case IMAGEX_DEST_DIR_OPTION:
2513 case IMAGEX_TO_STDOUT_OPTION:
2514 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2515 imagex_info_file = stderr;
2516 imagex_be_quiet = true;
2518 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2519 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2520 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2533 image_num_or_name = argv[1];
2538 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2543 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2547 image = wimlib_resolve_image(wim, image_num_or_name);
2548 ret = verify_image_exists_and_is_single(image,
2552 goto out_wimlib_free;
2555 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2557 &num_additional_swms);
2559 goto out_wimlib_free;
2561 additional_swms = NULL;
2562 num_additional_swms = 0;
2566 win32_acquire_restore_privileges();
2569 ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
2570 additional_swms, num_additional_swms,
2571 imagex_progress_func);
2573 if (!imagex_be_quiet)
2574 imagex_printf(T("Done extracting files.\n"));
2575 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2576 tfprintf(stderr, T("Note: You can use `"IMAGEX_PROGNAME" dir' to see what "
2577 "files and directories\n"
2578 " are in the WIM image.\n"));
2581 win32_release_restore_privileges();
2583 for (unsigned i = 0; i < num_additional_swms; i++)
2584 wimlib_free(additional_swms[i]);
2585 free(additional_swms);
2589 free_extract_commands(cmds, num_cmds, dest_dir);
2600 static void print_byte_field(const uint8_t field[], size_t len)
2603 tprintf(T("%02hhx"), *field++);
2607 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2609 tputs(T("WIM Information:"));
2610 tputs(T("----------------"));
2611 tprintf(T("Path: %"TS"\n"), wimfile);
2612 tprintf(T("GUID: 0x"));
2613 print_byte_field(info->guid, sizeof(info->guid));
2615 tprintf(T("Image Count: %d\n"), info->image_count);
2616 tprintf(T("Compression: %"TS"\n"),
2617 wimlib_get_compression_type_string(info->compression_type));
2618 tprintf(T("Part Number: %d/%d\n"), info->part_number, info->total_parts);
2619 tprintf(T("Boot Index: %d\n"), info->boot_index);
2620 tprintf(T("Size: %"PRIu64" bytes\n"), info->total_bytes);
2621 tprintf(T("Integrity Info: %"TS"\n"),
2622 info->has_integrity_table ? T("yes") : T("no"));
2623 tprintf(T("Relative path junction: %"TS"\n"),
2624 info->has_rpfix ? T("yes") : T("no"));
2625 tprintf(T("Pipable: %"TS"\n"),
2626 info->pipable ? T("yes") : T("no"));
2631 print_resource(const struct wimlib_resource_entry *resource,
2635 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
2636 resource->uncompressed_size);
2638 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
2639 resource->compressed_size);
2641 tprintf(T("Offset = %"PRIu64" bytes\n"),
2645 tprintf(T("Part Number = %u\n"), resource->part_number);
2646 tprintf(T("Reference Count = %u\n"), resource->reference_count);
2648 tprintf(T("Hash = 0x"));
2649 print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2652 tprintf(T("Flags = "));
2653 if (resource->is_compressed)
2654 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED "));
2655 if (resource->is_metadata)
2656 tprintf(T("WIM_RESHDR_FLAG_METADATA "));
2657 if (resource->is_free)
2658 tprintf(T("WIM_RESHDR_FLAG_FREE "));
2659 if (resource->is_spanned)
2660 tprintf(T("WIM_RESHDR_FLAG_SPANNED "));
2667 print_lookup_table(WIMStruct *wim)
2669 wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2672 /* Prints information about a WIM file; also can mark an image as bootable,
2673 * change the name of an image, or change the description of an image. */
2675 imagex_info(int argc, tchar **argv)
2680 bool nocheck = false;
2681 bool header = false;
2682 bool lookup_table = false;
2684 bool metadata = false;
2685 bool short_header = true;
2686 const tchar *xml_out_file = NULL;
2687 const tchar *wimfile;
2688 const tchar *image_num_or_name;
2689 const tchar *new_name;
2690 const tchar *new_desc;
2694 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2695 struct wimlib_wim_info info;
2697 for_opt(c, info_options) {
2699 case IMAGEX_BOOT_OPTION:
2702 case IMAGEX_CHECK_OPTION:
2705 case IMAGEX_NOCHECK_OPTION:
2708 case IMAGEX_HEADER_OPTION:
2710 short_header = false;
2712 case IMAGEX_LOOKUP_TABLE_OPTION:
2713 lookup_table = true;
2714 short_header = false;
2716 case IMAGEX_XML_OPTION:
2718 short_header = false;
2720 case IMAGEX_EXTRACT_XML_OPTION:
2721 xml_out_file = optarg;
2722 short_header = false;
2724 case IMAGEX_METADATA_OPTION:
2726 short_header = false;
2735 if (argc < 1 || argc > 4)
2739 image_num_or_name = (argc >= 2) ? argv[1] : T("all");
2740 new_name = (argc >= 3) ? argv[2] : NULL;
2741 new_desc = (argc >= 4) ? argv[3] : NULL;
2743 if (check && nocheck) {
2744 imagex_error(T("Can't specify both --check and --nocheck"));
2749 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2751 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2755 wimlib_get_wim_info(wim, &info);
2757 image = wimlib_resolve_image(wim, image_num_or_name);
2758 ret = WIMLIB_ERR_INVALID_IMAGE;
2759 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
2760 imagex_error(T("The image \"%"TS"\" does not exist in \"%"TS"\""),
2761 image_num_or_name, wimfile);
2763 imagex_error(T("If you would like to set the boot "
2764 "index to 0, specify image \"0\" with "
2765 "the --boot flag."));
2767 goto out_wimlib_free;
2770 if (boot && info.image_count == 0) {
2771 imagex_error(T("--boot is meaningless on a WIM with no images"));
2772 goto out_wimlib_free;
2775 if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
2777 imagex_error(T("Cannot specify the --boot flag "
2778 "without specifying a specific "
2779 "image in a multi-image WIM"));
2780 goto out_wimlib_free;
2783 imagex_error(T("Cannot specify the NEW_NAME "
2784 "without specifying a specific "
2785 "image in a multi-image WIM"));
2786 goto out_wimlib_free;
2790 /* Operations that print information are separated from operations that
2791 * recreate the WIM file. */
2792 if (!new_name && !boot) {
2794 /* Read-only operations */
2796 if (image == WIMLIB_NO_IMAGE) {
2797 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
2798 image_num_or_name, wimfile);
2799 goto out_wimlib_free;
2802 if (image == WIMLIB_ALL_IMAGES && short_header)
2803 print_wim_information(wimfile, &info);
2806 wimlib_print_header(wim);
2809 if (info.total_parts != 1) {
2810 tfprintf(stderr, T("Warning: Only showing the lookup table "
2811 "for part %d of a %d-part WIM.\n"),
2812 info.part_number, info.total_parts);
2814 print_lookup_table(wim);
2818 ret = wimlib_extract_xml_data(wim, stdout);
2820 goto out_wimlib_free;
2826 fp = tfopen(xml_out_file, T("wb"));
2828 imagex_error_with_errno(T("Failed to open the "
2829 "file \"%"TS"\" for "
2833 goto out_wimlib_free;
2835 ret = wimlib_extract_xml_data(wim, fp);
2837 imagex_error(T("Failed to close the file "
2843 goto out_wimlib_free;
2847 wimlib_print_available_images(wim, image);
2850 ret = wimlib_print_metadata(wim, image);
2852 goto out_wimlib_free;
2857 /* Modification operations */
2859 if (image == WIMLIB_ALL_IMAGES)
2862 if (image == WIMLIB_NO_IMAGE && new_name) {
2863 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
2864 "when using image 0"), new_name);
2866 goto out_wimlib_free;
2870 if (image == info.boot_index) {
2871 imagex_printf(T("Image %d is already marked as "
2872 "bootable.\n"), image);
2875 imagex_printf(T("Marking image %d as bootable.\n"),
2877 info.boot_index = image;
2878 ret = wimlib_set_wim_info(wim, &info,
2879 WIMLIB_CHANGE_BOOT_INDEX);
2881 goto out_wimlib_free;
2885 if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
2887 imagex_printf(T("Image %d is already named \"%"TS"\".\n"),
2891 imagex_printf(T("Changing the name of image %d to "
2892 "\"%"TS"\".\n"), image, new_name);
2893 ret = wimlib_set_image_name(wim, image, new_name);
2895 goto out_wimlib_free;
2899 const tchar *old_desc;
2900 old_desc = wimlib_get_image_description(wim, image);
2901 if (old_desc && !tstrcmp(old_desc, new_desc)) {
2902 imagex_printf(T("The description of image %d is already "
2903 "\"%"TS"\".\n"), image, new_desc);
2906 imagex_printf(T("Changing the description of image %d "
2907 "to \"%"TS"\".\n"), image, new_desc);
2908 ret = wimlib_set_image_descripton(wim, image,
2911 goto out_wimlib_free;
2915 /* Only call wimlib_overwrite() if something actually needs to
2917 if (boot || new_name || new_desc ||
2918 (check && !info.has_integrity_table) ||
2919 (nocheck && info.has_integrity_table))
2921 int write_flags = 0;
2924 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2926 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2927 ret = wimlib_overwrite(wim, write_flags, 1,
2928 imagex_progress_func);
2930 imagex_printf(T("The file \"%"TS"\" was not modified "
2931 "because nothing needed to be done.\n"),
2948 /* Join split WIMs into one part WIM */
2950 imagex_join(int argc, tchar **argv)
2953 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2954 int wim_write_flags = 0;
2955 const tchar *output_path;
2958 for_opt(c, join_options) {
2960 case IMAGEX_CHECK_OPTION:
2961 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2962 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2972 imagex_error(T("Must specify one or more split WIM (.swm) "
2976 output_path = argv[0];
2977 ret = wimlib_join((const tchar * const *)++argv,
2982 imagex_progress_func);
2992 /* Mounts an image using a FUSE mount. */
2994 imagex_mount_rw_or_ro(int argc, tchar **argv)
2997 int mount_flags = 0;
2998 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2999 const tchar *swm_glob = NULL;
3000 const tchar *staging_dir = NULL;
3001 const tchar *wimfile;
3006 WIMStruct **additional_swms;
3007 unsigned num_additional_swms;
3009 if (!tstrcmp(argv[0], T("mountrw"))) {
3010 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
3011 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3014 for_opt(c, mount_options) {
3016 case IMAGEX_ALLOW_OTHER_OPTION:
3017 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
3019 case IMAGEX_CHECK_OPTION:
3020 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3022 case IMAGEX_DEBUG_OPTION:
3023 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
3025 case IMAGEX_STREAMS_INTERFACE_OPTION:
3026 if (!tstrcasecmp(optarg, T("none")))
3027 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
3028 else if (!tstrcasecmp(optarg, T("xattr")))
3029 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
3030 else if (!tstrcasecmp(optarg, T("windows")))
3031 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
3033 imagex_error(T("Unknown stream interface \"%"TS"\""),
3038 case IMAGEX_REF_OPTION:
3041 case IMAGEX_STAGING_DIR_OPTION:
3042 staging_dir = optarg;
3044 case IMAGEX_UNIX_DATA_OPTION:
3045 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
3053 if (argc != 2 && argc != 3)
3058 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3063 /* Image explicitly specified. */
3064 image = wimlib_resolve_image(wim, argv[1]);
3066 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
3070 /* No image specified; default to image 1, but only if the WIM
3071 * contains exactly one image. */
3072 struct wimlib_wim_info info;
3074 wimlib_get_wim_info(wim, &info);
3075 if (info.image_count != 1) {
3076 imagex_error(T("\"%"TS"\" contains %d images; Please "
3077 "select one."), wimfile, info.image_count);
3086 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
3088 &num_additional_swms);
3092 additional_swms = NULL;
3093 num_additional_swms = 0;
3096 ret = wimlib_mount_image(wim, image, dir, mount_flags, additional_swms,
3097 num_additional_swms, staging_dir);
3099 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
3101 image, wimfile, dir);
3103 for (unsigned i = 0; i < num_additional_swms; i++)
3104 wimlib_free(additional_swms[i]);
3105 free(additional_swms);
3112 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
3118 /* Rebuild a WIM file */
3120 imagex_optimize(int argc, tchar **argv)
3123 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3124 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
3127 const tchar *wimfile;
3130 unsigned num_threads = 0;
3132 for_opt(c, optimize_options) {
3134 case IMAGEX_CHECK_OPTION:
3135 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3136 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3138 case IMAGEX_NOCHECK_OPTION:
3139 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3141 case IMAGEX_RECOMPRESS_OPTION:
3142 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3144 case IMAGEX_THREADS_OPTION:
3145 num_threads = parse_num_threads(optarg);
3146 if (num_threads == UINT_MAX)
3149 case IMAGEX_PIPABLE_OPTION:
3150 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
3152 case IMAGEX_NOT_PIPABLE_OPTION:
3153 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
3167 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3171 old_size = file_get_size(wimfile);
3172 tprintf(T("\"%"TS"\" original size: "), wimfile);
3174 tputs(T("Unknown"));
3176 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3178 ret = wimlib_overwrite(wim, write_flags, num_threads,
3179 imagex_progress_func);
3181 imagex_error(T("Optimization of \"%"TS"\" failed."), wimfile);
3182 goto out_wimlib_free;
3185 new_size = file_get_size(wimfile);
3186 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3188 tputs(T("Unknown"));
3190 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3192 tfputs(T("Space saved: "), stdout);
3193 if (new_size != -1 && old_size != -1) {
3194 tprintf(T("%lld KiB\n"),
3195 ((long long)old_size - (long long)new_size) >> 10);
3197 tputs(T("Unknown"));
3212 /* Split a WIM into a spanned set */
3214 imagex_split(int argc, tchar **argv)
3218 int write_flags = 0;
3219 unsigned long part_size;
3224 for_opt(c, split_options) {
3226 case IMAGEX_CHECK_OPTION:
3227 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3228 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3240 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3241 if (tmp == argv[2] || *tmp) {
3242 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3243 imagex_error(T("The part size must be an integer or "
3244 "floating-point number of megabytes."));
3247 ret = wimlib_open_wim(argv[0], open_flags, &wim, imagex_progress_func);
3251 ret = wimlib_split(wim, argv[1], part_size, write_flags, imagex_progress_func);
3263 /* Unmounts a mounted WIM image. */
3265 imagex_unmount(int argc, tchar **argv)
3268 int unmount_flags = 0;
3271 for_opt(c, unmount_options) {
3273 case IMAGEX_COMMIT_OPTION:
3274 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3276 case IMAGEX_CHECK_OPTION:
3277 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3279 case IMAGEX_REBUILD_OPTION:
3280 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3282 case IMAGEX_LAZY_OPTION:
3283 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3294 ret = wimlib_unmount_image(argv[0], unmount_flags,
3295 imagex_progress_func);
3297 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3308 * Add, delete, or rename files in a WIM image.
3311 imagex_update(int argc, tchar **argv)
3313 const tchar *wimfile;
3317 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3318 int write_flags = 0;
3319 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3320 int default_add_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
3321 int default_delete_flags = 0;
3322 unsigned num_threads = 0;
3324 tchar *cmd_file_contents;
3325 size_t cmd_file_nchars;
3326 struct wimlib_update_command *cmds;
3328 tchar *command_str = NULL;
3330 const tchar *config_file = NULL;
3332 struct wimlib_capture_config *config;
3334 for_opt(c, update_options) {
3336 /* Generic or write options */
3337 case IMAGEX_THREADS_OPTION:
3338 num_threads = parse_num_threads(optarg);
3339 if (num_threads == UINT_MAX)
3342 case IMAGEX_CHECK_OPTION:
3343 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3344 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3346 case IMAGEX_REBUILD_OPTION:
3347 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3349 case IMAGEX_COMMAND_OPTION:
3351 imagex_error(T("--command may only be specified "
3352 "one time. Please provide\n"
3353 " the update commands "
3354 "on standard input instead."));
3357 command_str = tstrdup(optarg);
3359 imagex_error(T("Out of memory!"));
3363 /* Default delete options */
3364 case IMAGEX_FORCE_OPTION:
3365 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3367 case IMAGEX_RECURSIVE_OPTION:
3368 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3371 /* Global add option */
3372 case IMAGEX_CONFIG_OPTION:
3373 config_file = optarg;
3376 /* Default add options */
3377 case IMAGEX_VERBOSE_OPTION:
3378 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
3380 case IMAGEX_DEREFERENCE_OPTION:
3381 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
3383 case IMAGEX_UNIX_DATA_OPTION:
3384 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
3386 case IMAGEX_NO_ACLS_OPTION:
3387 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
3389 case IMAGEX_STRICT_ACLS_OPTION:
3390 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
3399 if (argc != 1 && argc != 2)
3403 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3405 goto out_free_command_str;
3408 /* Image explicitly specified. */
3409 image = wimlib_resolve_image(wim, argv[1]);
3410 ret = verify_image_exists_and_is_single(image, argv[1],
3413 goto out_wimlib_free;
3415 /* No image specified; default to image 1, but only if the WIM
3416 * contains exactly one image. */
3417 struct wimlib_wim_info info;
3419 wimlib_get_wim_info(wim, &info);
3420 if (info.image_count != 1) {
3421 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3422 wimfile, info.image_count);
3429 /* Parse capture configuration file if specified */
3433 config_str = file_get_text_contents(config_file, &config_len);
3436 goto out_wimlib_free;
3439 config = alloca(sizeof(*config));
3440 ret = parse_capture_config(&config_str, config_len, config);
3442 goto out_free_config;
3444 config = &default_capture_config;
3447 /* Read update commands from standard input, or the command string if
3450 cmd_file_contents = NULL;
3451 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3454 if (isatty(STDIN_FILENO)) {
3455 tputs(T("Reading update commands from standard input..."));
3456 recommend_man_page(T("update"));
3458 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3459 if (!cmd_file_contents) {
3461 goto out_free_config;
3464 /* Parse the update commands */
3465 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3470 goto out_free_cmd_file_contents;
3473 /* Set default flags and capture config on the update commands */
3474 bool have_add_command = false;
3475 for (size_t i = 0; i < num_cmds; i++) {
3476 switch (cmds[i].op) {
3477 case WIMLIB_UPDATE_OP_ADD:
3478 cmds[i].add.add_flags |= default_add_flags;
3479 cmds[i].add.config = config;
3480 have_add_command = true;
3482 case WIMLIB_UPDATE_OP_DELETE:
3483 cmds[i].delete.delete_flags |= default_delete_flags;
3491 if (have_add_command)
3492 win32_acquire_capture_privileges();
3495 /* Execute the update commands */
3496 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3497 imagex_progress_func);
3499 goto out_release_privs;
3501 /* Overwrite the updated WIM */
3502 ret = wimlib_overwrite(wim, write_flags, num_threads,
3503 imagex_progress_func);
3506 if (have_add_command)
3507 win32_release_capture_privileges();
3510 out_free_cmd_file_contents:
3511 free(cmd_file_contents);
3513 if (config != &default_capture_config) {
3514 free(config->exclusion_pats.pats);
3515 free(config->exclusion_exception_pats.pats);
3520 out_free_command_str:
3528 goto out_free_command_str;
3531 struct imagex_command {
3533 int (*func)(int , tchar **);
3538 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
3539 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
3541 static const struct imagex_command imagex_commands[] = {
3542 {T("append"), imagex_capture_or_append, APPEND},
3543 {T("apply"), imagex_apply, APPLY},
3544 {T("capture"), imagex_capture_or_append, CAPTURE},
3545 {T("delete"), imagex_delete, DELETE},
3546 {T("dir"), imagex_dir, DIR},
3547 {T("export"), imagex_export, EXPORT},
3548 {T("extract"), imagex_extract, EXTRACT},
3549 {T("info"), imagex_info, INFO},
3550 {T("join"), imagex_join, JOIN},
3551 {T("mount"), imagex_mount_rw_or_ro, MOUNT},
3552 {T("mountrw"), imagex_mount_rw_or_ro, MOUNTRW},
3553 {T("optimize"),imagex_optimize, OPTIMIZE},
3554 {T("split"), imagex_split, SPLIT},
3555 {T("unmount"), imagex_unmount, UNMOUNT},
3556 {T("update"), imagex_update, UPDATE},
3562 static const tchar *s =
3564 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
3565 "Copyright (C) 2012, 2013 Eric Biggers\n"
3566 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
3567 "This is free software: you are free to change and redistribute it.\n"
3568 "There is NO WARRANTY, to the extent permitted by law.\n"
3570 "Report bugs to "PACKAGE_BUGREPORT".\n"
3577 help_or_version(int argc, tchar **argv)
3581 const struct imagex_command *cmd;
3583 for (i = 1; i < argc; i++) {
3591 if (!tstrcmp(p, T("help"))) {
3592 for_imagex_command(cmd) {
3593 if (!tstrcmp(cmd->name, argv[1])) {
3601 if (!tstrcmp(p, T("version"))) {
3612 const struct imagex_command *cmd;
3613 tprintf(T("Usage:\n%"TS), usage_strings[cmd_type]);
3614 for_imagex_command(cmd) {
3615 if (cmd->cmd == cmd_type) {
3616 tputc(T('\n'), stdout);
3617 recommend_man_page(cmd->name);
3625 tfputs(T("Usage:\n"), stdout);
3626 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
3627 tprintf(T(" %"TS"\n"), usage_strings[i]);
3628 static const tchar *extra =
3630 " "IMAGEX_PROGNAME" --help\n"
3631 " "IMAGEX_PROGNAME" --version\n"
3633 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
3636 tfputs(extra, stdout);
3637 recommend_man_page(T(""));
3640 /* Entry point for wimlib's ImageX implementation. On UNIX the command
3641 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
3642 * something else), while an Windows the command arguments will be UTF-16LE
3643 * encoded 'wchar_t' strings. */
3646 wmain(int argc, wchar_t **argv, wchar_t **envp)
3648 main(int argc, char **argv)
3651 const struct imagex_command *cmd;
3655 imagex_info_file = stdout;
3658 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
3659 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
3663 setlocale(LC_ALL, "");
3664 codeset = nl_langinfo(CODESET);
3665 if (!strstr(codeset, "UTF-8") &&
3666 !strstr(codeset, "UTF8") &&
3667 !strstr(codeset, "utf-8") &&
3668 !strstr(codeset, "utf8"))
3671 "WARNING: Running "IMAGEX_PROGNAME" in a UTF-8 locale is recommended!\n"
3672 " Maybe try: `export LANG=en_US.UTF-8'?\n"
3673 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
3674 " to any value to force wimlib to use UTF-8.\n",
3679 #endif /* !__WIN32__ */
3682 imagex_error(T("No command specified"));
3688 /* Handle --help and --version for all commands. Note that this will
3689 * not return if either of these arguments are present. */
3690 help_or_version(argc, argv);
3694 /* The user may like to see more informative error messages. */
3695 wimlib_set_print_errors(true);
3697 /* Do any initializations that the library needs */
3698 ret = wimlib_global_init(init_flags);
3700 goto out_check_status;
3702 /* Search for the function to handle the ImageX subcommand. */
3703 for_imagex_command(cmd) {
3704 if (!tstrcmp(cmd->name, *argv)) {
3705 ret = cmd->func(argc, argv);
3706 goto out_check_write_error;
3710 imagex_error(T("Unrecognized command: `%"TS"'"), argv[0]);
3714 out_check_write_error:
3715 /* For 'wimlib-imagex info' and 'wimlib-imagex dir', data printed to
3716 * standard output is part of the program's actual behavior and not just
3717 * for informational purposes, so we should set a failure exit status if
3718 * there was a write error. */
3719 if (cmd == &imagex_commands[INFO] || cmd == &imagex_commands[DIR]) {
3720 if (ferror(stdout) || fclose(stdout)) {
3721 imagex_error_with_errno(T("error writing to standard output"));
3727 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
3728 * outside of the wimlib library code. 0 indicates success. > 0
3729 * indicates a wimlib error code from which an error message can be
3732 imagex_error(T("Exiting with error code %d:\n"
3734 wimlib_get_error_string(ret));
3735 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
3736 imagex_error_with_errno(T("errno"));
3739 /* Make the library free any resources it's holding (not strictly
3740 * necessary because the process is ending anyway). */
3741 wimlib_global_cleanup();