4 * Use wimlib to create, modify, extract, mount, unmount, or display information
9 * Copyright (C) 2012, 2013 Eric Biggers
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "wimlib_tchar.h"
47 # include "imagex-win32.h"
48 # define tbasename win32_wbasename
49 # define tglob win32_wglob
53 # include <langinfo.h>
54 # define tbasename basename
59 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
61 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
82 static void usage(int cmd_type);
83 static void usage_all();
85 static bool imagex_be_quiet = false;
88 static const tchar *usage_strings[] = {
91 IMAGEX_PROGNAME" append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
92 " [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
93 " [--verbose] [--dereference] [--config=FILE]\n"
94 " [--threads=NUM_THREADS] [--rebuild] [--unix-data]\n"
95 " [--source-list] [--no-acls] [--strict-acls]\n"
96 " [--rpfix] [--norpfix]\n"
100 IMAGEX_PROGNAME" apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
101 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
102 " [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n"
103 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
107 IMAGEX_PROGNAME" capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
108 " [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
109 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
110 " [--config=FILE] [--threads=NUM_THREADS] [--unix-data]\n"
111 " [--source-list] [--no-acls] [--strict-acls]\n"
112 " [--rpfix] [--norpfix]\n"
116 IMAGEX_PROGNAME" delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check] [--soft]\n"
120 IMAGEX_PROGNAME" dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n"
124 IMAGEX_PROGNAME" export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
125 " DEST_WIMFILE [DEST_IMAGE_NAME] [DEST_IMAGE_DESCRIPTION]\n"
126 " [--boot] [--check] [--compress=TYPE] [--ref=\"GLOB\"]\n"
127 " [--threads=NUM_THREADS] [--rebuild]\n"
131 IMAGEX_PROGNAME" extract WIMFILE (IMAGE_NUM | IMAGE_NAME) [PATH...]\n"
132 " [--check] [--ref=\"GLOB\"] [--verbose] [--unix-data]\n"
133 " [--no-acls] [--strict-acls] [--to-stdout] [--dest-dir=DIR]\n"
137 IMAGEX_PROGNAME" info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
138 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
139 " [--xml] [--extract-xml FILE] [--metadata]\n"
143 IMAGEX_PROGNAME" join [--check] WIMFILE SPLIT_WIM...\n"
147 IMAGEX_PROGNAME" mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
148 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
149 " [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n"
153 IMAGEX_PROGNAME" mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
154 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
155 " [--staging-dir=DIR] [--unix-data] [--allow-other]\n"
159 IMAGEX_PROGNAME" optimize WIMFILE [--check] [--recompress]\n"
160 " [--threads=NUM_THREADS]\n"
164 IMAGEX_PROGNAME" split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n"
168 IMAGEX_PROGNAME" unmount DIRECTORY [--commit] [--check] [--rebuild]\n"
172 IMAGEX_PROGNAME" update WIMFILE IMAGE [--check] [--rebuild]\n"
173 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
174 " [DEFAULT_DELETE_OPTIONS] < CMDFILE\n"
180 recommend_man_page(const tchar *cmd_name)
183 tprintf(T("See "IMAGEX_PROGNAME"-%"TS".pdf in the "
184 "doc directory for more details.\n"),
187 tprintf(T("Try `man "IMAGEX_PROGNAME"-%"TS"' "
188 "for more details.\n"), cmd_name);
193 IMAGEX_ALLOW_OTHER_OPTION,
196 IMAGEX_COMMIT_OPTION,
197 IMAGEX_COMPRESS_OPTION,
198 IMAGEX_CONFIG_OPTION,
200 IMAGEX_DEREFERENCE_OPTION,
201 IMAGEX_DEST_DIR_OPTION,
202 IMAGEX_EXTRACT_XML_OPTION,
205 IMAGEX_HARDLINK_OPTION,
206 IMAGEX_HEADER_OPTION,
207 IMAGEX_LOOKUP_TABLE_OPTION,
208 IMAGEX_METADATA_OPTION,
209 IMAGEX_NORPFIX_OPTION,
210 IMAGEX_NO_ACLS_OPTION,
211 IMAGEX_REBUILD_OPTION,
212 IMAGEX_RECOMPRESS_OPTION,
213 IMAGEX_RECURSIVE_OPTION,
217 IMAGEX_SOURCE_LIST_OPTION,
218 IMAGEX_STAGING_DIR_OPTION,
219 IMAGEX_STREAMS_INTERFACE_OPTION,
220 IMAGEX_STRICT_ACLS_OPTION,
221 IMAGEX_SYMLINK_OPTION,
222 IMAGEX_THREADS_OPTION,
223 IMAGEX_TO_STDOUT_OPTION,
224 IMAGEX_UNIX_DATA_OPTION,
225 IMAGEX_VERBOSE_OPTION,
229 static const struct option apply_options[] = {
230 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
231 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
232 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
233 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
234 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
235 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
236 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
237 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
238 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
239 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
240 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
243 static const struct option capture_or_append_options[] = {
244 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
245 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
246 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
247 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
248 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
249 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
250 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
251 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
252 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
253 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
254 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
255 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
256 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
257 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
258 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
259 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
262 static const struct option delete_options[] = {
263 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
264 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
268 static const struct option export_options[] = {
269 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
270 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
271 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
272 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
273 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
274 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
278 static const struct option extract_options[] = {
279 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
280 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
281 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
282 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
283 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
284 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
285 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
286 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
287 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
291 static const struct option info_options[] = {
292 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
293 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
294 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
295 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
296 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
297 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
298 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
302 static const struct option join_options[] = {
303 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
307 static const struct option mount_options[] = {
308 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
309 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
310 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
311 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
312 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
313 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
314 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
318 static const struct option optimize_options[] = {
319 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
320 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
321 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
325 static const struct option split_options[] = {
326 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
330 static const struct option unmount_options[] = {
331 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
332 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
333 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
337 static const struct option update_options[] = {
338 /* Careful: some of the options here set the defaults for update
339 * commands, but the flags given to an actual update command (and not to
340 * `imagex update' itself are also handled in
341 * update_command_add_option(). */
342 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
343 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
344 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
345 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
346 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
347 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
348 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
349 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
350 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
351 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
352 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
356 /* Print formatted error message to stderr. */
358 imagex_error(const tchar *format, ...)
361 va_start(va, format);
362 tfputs(T("ERROR: "), stderr);
363 tvfprintf(stderr, format, va);
364 tputc(T('\n'), stderr);
368 /* Print formatted error message to stderr. */
370 imagex_error_with_errno(const tchar *format, ...)
372 int errno_save = errno;
374 va_start(va, format);
375 tfputs(T("ERROR: "), stderr);
376 tvfprintf(stderr, format, va);
377 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
382 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
384 if (image == WIMLIB_NO_IMAGE) {
385 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
386 " Please specify a 1-based image index or "
388 " You may use `"IMAGEX_PROGNAME" info' to list the images "
389 "contained in a WIM."),
390 image_name, wim_name);
397 verify_image_is_single(int image)
399 if (image == WIMLIB_ALL_IMAGES) {
400 imagex_error(T("Cannot specify all images for this action!"));
407 verify_image_exists_and_is_single(int image, const tchar *image_name,
408 const tchar *wim_name)
411 ret = verify_image_exists(image, image_name, wim_name);
413 ret = verify_image_is_single(image);
417 /* Parse the argument to --compress */
419 get_compression_type(const tchar *optarg)
421 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
422 return WIMLIB_COMPRESSION_TYPE_LZX;
423 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
424 return WIMLIB_COMPRESSION_TYPE_XPRESS;
425 else if (!tstrcasecmp(optarg, T("none")))
426 return WIMLIB_COMPRESSION_TYPE_NONE;
428 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
429 "\"maximum\", \"fast\", or \"none\"."), optarg);
430 return WIMLIB_COMPRESSION_TYPE_INVALID;
434 /* Returns the size of a file given its name, or -1 if the file does not exist
435 * or its size cannot be determined. */
437 file_get_size(const tchar *filename)
440 if (tstat(filename, &st) == 0)
446 tchar pat_ntfs_log[] = T("/$ntfs.log");
447 tchar pat_hiberfil_sys[] = T("/hiberfil.sys");
448 tchar pat_pagefile_sys[] = T("/pagefile.sys");
449 tchar pat_system_volume_information[] = T("/System Volume Information");
450 tchar pat_recycler[] = T("/RECYCLER");
451 tchar pat_windows_csc[] = T("/Windows/CSC");
453 tchar *default_pats[] = {
457 pat_system_volume_information,
462 static struct wimlib_capture_config default_capture_config = {
464 .num_pats = sizeof(default_pats) / sizeof(*default_pats),
465 .pats = default_pats,
470 PARSE_STRING_SUCCESS = 0,
471 PARSE_STRING_FAILURE = 1,
472 PARSE_STRING_NONE = 2,
476 * Parses a string token from an array of characters.
478 * Tokens are either whitespace-delimited, or double or single-quoted.
480 * @line_p: Pointer to the pointer to the line of data. Will be updated
481 * to point past the string token iff the return value is
482 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
485 * @len_p: @len_p initially stores the length of the line of data, which may
486 * be 0, and it will be updated to the number of bytes remaining in
487 * the line iff the return value is PARSE_STRING_SUCCESS.
489 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
490 * parsed string token will be returned here.
492 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
493 * PARSE_STRING_FAILURE if the data was invalid due to a missing
494 * closing quote; or PARSE_STRING_NONE if the line ended before the
495 * beginning of a string token was found.
498 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
501 tchar *line = *line_p;
505 /* Skip leading whitespace */
508 return PARSE_STRING_NONE;
509 if (!istspace(*line) && *line != T('\0'))
515 if (quote_char == T('"') || quote_char == T('\'')) {
520 line = tmemchr(line, quote_char, len);
522 imagex_error(T("Missing closing quote: %"TS), fn - 1);
523 return PARSE_STRING_FAILURE;
526 /* Unquoted string. Go until whitespace. Line is terminated
527 * by '\0', so no need to check 'len'. */
531 } while (!istspace(*line) && *line != T('\0'));
538 return PARSE_STRING_SUCCESS;
541 /* Parses a line of data (not an empty line or comment) in the source list file
542 * format. (See the man page for 'wimlib-imagex capture' for details on this
543 * format and the meaning.)
545 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
546 * len == 0. The data in @line will be modified by this function call.
548 * @len: Length of the line of data.
550 * @source: On success, the capture source and target described by the line is
551 * written into this destination. Note that it will contain pointers
552 * to data in the @line array.
554 * Returns true if the line was valid; false otherwise. */
556 parse_source_list_line(tchar *line, size_t len,
557 struct wimlib_capture_source *source)
561 ret = parse_string(&line, &len, &source->fs_source_path);
562 if (ret != PARSE_STRING_SUCCESS)
564 ret = parse_string(&line, &len, &source->wim_target_path);
565 if (ret == PARSE_STRING_NONE)
566 source->wim_target_path = source->fs_source_path;
567 return ret != PARSE_STRING_FAILURE;
570 /* Returns %true if the given line of length @len > 0 is a comment or empty line
571 * in the source list file format. */
573 is_comment_line(const tchar *line, size_t len)
578 if (!istspace(*line) && *line != T('\0'))
588 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
591 tchar *contents = *contents_p;
592 size_t nchars = *nchars_p;
595 for (i = 0; i < nchars; i++)
596 if (contents[i] == T('\n'))
599 /* Handle last line not terminated by a newline */
600 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
601 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
603 imagex_error(T("Out of memory!"));
606 contents[nchars] = T('\n');
607 *contents_p = contents;
615 /* Parses a file in the source list format. (See the man page for
616 * 'wimlib-imagex capture' for details on this format and the meaning.)
618 * @source_list_contents: Contents of the source list file. Note that this
619 * buffer will be modified to save memory allocations,
620 * and cannot be freed until the returned array of
621 * wimlib_capture_source's has also been freed.
623 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
626 * @nsources_ret: On success, the length of the returned array is
629 * Returns: An array of `struct wimlib_capture_source's that can be passed to
630 * the wimlib_add_image_multisource() function to specify how a WIM image is to
632 static struct wimlib_capture_source *
633 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
634 size_t *nsources_ret)
638 struct wimlib_capture_source *sources;
641 nlines = text_file_count_lines(source_list_contents_p,
642 &source_list_nchars);
646 /* Always allocate at least 1 slot, just in case the implementation of
647 * calloc() returns NULL if 0 bytes are requested. */
648 sources = calloc(nlines ?: 1, sizeof(*sources));
650 imagex_error(T("out of memory"));
653 p = *source_list_contents_p;
655 for (i = 0; i < nlines; i++) {
656 /* XXX: Could use rawmemchr() here instead, but it may not be
657 * available on all platforms. */
658 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
659 size_t len = endp - p + 1;
661 if (!is_comment_line(p, len)) {
662 if (!parse_source_list_line(p, len, &sources[j++])) {
675 enum capture_config_section {
676 CAPTURE_CONFIG_NO_SECTION,
677 CAPTURE_CONFIG_EXCLUSION_SECTION,
678 CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
679 CAPTURE_CONFIG_IGNORE_SECTION,
683 CAPTURE_CONFIG_INVALID_SECTION,
684 CAPTURE_CONFIG_CHANGED_SECTION,
685 CAPTURE_CONFIG_SAME_SECTION,
689 check_config_section(tchar *line, size_t len,
690 enum capture_config_section *cur_section)
692 while (istspace(*line))
696 return CAPTURE_CONFIG_SAME_SECTION;
699 tchar *endbrace = tstrrchr(line, T(']'));
701 return CAPTURE_CONFIG_SAME_SECTION;
703 if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
704 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
705 } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
706 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
707 } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
708 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
709 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
710 "of capture config file\n"),
712 } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
713 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
714 tfputs(T("WARNING: Ignoring [AlignmentList] section "
715 "of capture config file\n"),
718 imagex_error(T("Invalid capture config file section \"%"TS"\""),
720 return CAPTURE_CONFIG_INVALID_SECTION;
722 return CAPTURE_CONFIG_CHANGED_SECTION;
727 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
730 if (pat_list->num_pats == pat_list->num_allocated_pats) {
732 size_t num_allocated_pats = pat_list->num_pats + 8;
734 pats = realloc(pat_list->pats,
735 num_allocated_pats * sizeof(pat_list->pats[0]));
737 imagex_error(T("Out of memory!"));
740 pat_list->pats = pats;
741 pat_list->num_allocated_pats = num_allocated_pats;
743 pat_list->pats[pat_list->num_pats++] = pat;
748 parse_capture_config_line(tchar *line, size_t len,
749 enum capture_config_section *cur_section,
750 struct wimlib_capture_config *config)
755 ret = check_config_section(line, len, cur_section);
756 if (ret == CAPTURE_CONFIG_INVALID_SECTION)
758 if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
761 switch (*cur_section) {
762 case CAPTURE_CONFIG_NO_SECTION:
763 imagex_error(T("Line \"%"TS"\" is not in a section "
764 "(such as [ExclusionList]"), line);
766 case CAPTURE_CONFIG_EXCLUSION_SECTION:
767 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
769 return pattern_list_add_pattern(&config->exclusion_pats,
771 case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
772 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
774 return pattern_list_add_pattern(&config->exclusion_exception_pats,
776 case CAPTURE_CONFIG_IGNORE_SECTION:
783 parse_capture_config(tchar **contents_p, size_t nchars,
784 struct wimlib_capture_config *config)
789 enum capture_config_section cur_section;
791 memset(config, 0, sizeof(*config));
793 nlines = text_file_count_lines(contents_p, &nchars);
797 cur_section = CAPTURE_CONFIG_NO_SECTION;
799 for (i = 0; i < nlines; i++) {
800 tchar *endp = tmemchr(p, T('\n'), nchars);
801 size_t len = endp - p + 1;
803 if (!is_comment_line(p, len))
804 if (!parse_capture_config_line(p, len, &cur_section, config))
812 /* Reads the contents of a file into memory. */
814 file_get_contents(const tchar *filename, size_t *len_ret)
821 if (tstat(filename, &stbuf) != 0) {
822 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
827 fp = tfopen(filename, T("rb"));
829 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
835 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
836 "contents of file \"%"TS"\""), len, filename);
839 if (fread(buf, 1, len, fp) != len) {
840 imagex_error_with_errno(T("Failed to read %zu bytes from the "
841 "file \"%"TS"\""), len, filename);
855 /* Read standard input until EOF and return the full contents in a malloc()ed
856 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
859 stdin_get_contents(size_t *len_ret)
861 /* stdin can, of course, be a pipe or other non-seekable file, so the
862 * total length of the data cannot be pre-determined */
864 size_t newlen = 1024;
868 char *p = realloc(buf, newlen);
869 size_t bytes_read, bytes_to_read;
871 imagex_error(T("out of memory while reading stdin"));
875 bytes_to_read = newlen - pos;
876 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
878 if (bytes_read != bytes_to_read) {
883 imagex_error_with_errno(T("error reading stdin"));
897 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
900 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
902 *num_tchars_ret = num_bytes;
904 #else /* !__WIN32__ */
905 /* On Windows, translate the text to UTF-16LE */
909 if (num_bytes >= 2 &&
910 ((text[0] == 0xff && text[1] == 0xfe) ||
911 (text[0] <= 0x7f && text[1] == 0x00)))
913 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
914 * with something that looks like an ASCII character encoded as
915 * a UTF-16LE code unit. Assume the file is encoded as
916 * UTF-16LE. This is not a 100% reliable check. */
917 num_wchars = num_bytes / 2;
918 text_wstr = (wchar_t*)text;
920 /* File does not look like UTF-16LE. Assume it is encoded in
921 * the current Windows code page. I think these are always
922 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
923 * should work as expected. */
924 text_wstr = win32_mbs_to_wcs(text,
929 *num_tchars_ret = num_wchars;
931 #endif /* __WIN32__ */
935 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
940 contents = file_get_contents(filename, &num_bytes);
943 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
947 stdin_get_text_contents(size_t *num_tchars_ret)
952 contents = stdin_get_contents(&num_bytes);
955 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
958 /* Return 0 if a path names a file to which the current user has write access;
959 * -1 otherwise (and print an error message). */
961 file_writable(const tchar *path)
964 ret = taccess(path, W_OK);
966 imagex_error_with_errno(T("Can't modify \"%"TS"\""), path);
970 #define TO_PERCENT(numerator, denominator) \
971 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
973 /* Given an enumerated value for WIM compression type, return a descriptive
976 get_data_type(int ctype)
979 case WIMLIB_COMPRESSION_TYPE_NONE:
980 return T("uncompressed");
981 case WIMLIB_COMPRESSION_TYPE_LZX:
982 return T("LZX-compressed");
983 case WIMLIB_COMPRESSION_TYPE_XPRESS:
984 return T("XPRESS-compressed");
989 /* Progress callback function passed to various wimlib functions. */
991 imagex_progress_func(enum wimlib_progress_msg msg,
992 const union wimlib_progress_info *info)
994 unsigned percent_done;
998 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
999 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1000 info->write_streams.total_bytes);
1001 if (info->write_streams.completed_streams == 0) {
1002 const tchar *data_type;
1004 data_type = get_data_type(info->write_streams.compression_type);
1005 tprintf(T("Writing %"TS" data using %u thread%"TS"\n"),
1006 data_type, info->write_streams.num_threads,
1007 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1009 tprintf(T("\r%"PRIu64" MiB of %"PRIu64" MiB (uncompressed) "
1010 "written (%u%% done)"),
1011 info->write_streams.completed_bytes >> 20,
1012 info->write_streams.total_bytes >> 20,
1014 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1017 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1018 tprintf(T("Scanning \"%"TS"\""), info->scan.source);
1019 if (*info->scan.wim_target_path) {
1020 tprintf(T(" (loading as WIM path: \"/%"TS"\")...\n"),
1021 info->scan.wim_target_path);
1023 tprintf(T(" (loading as root of WIM image)...\n"));
1026 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1027 if (info->scan.excluded)
1028 tprintf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1030 tprintf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
1032 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
1034 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1035 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1036 info->integrity.total_bytes);
1037 tprintf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" MiB "
1038 "of %"PRIu64" MiB (%u%%) done"),
1039 info->integrity.filename,
1040 info->integrity.completed_bytes >> 20,
1041 info->integrity.total_bytes >> 20,
1043 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1046 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1047 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1048 info->integrity.total_bytes);
1049 tprintf(T("\rCalculating integrity table for WIM: %"PRIu64" MiB "
1050 "of %"PRIu64" MiB (%u%%) done"),
1051 info->integrity.completed_bytes >> 20,
1052 info->integrity.total_bytes >> 20,
1054 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1057 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1058 tprintf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1059 "to %"TS" \"%"TS"\"\n"),
1060 info->extract.image,
1061 info->extract.image_name,
1062 info->extract.wimfile_name,
1063 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1064 T("NTFS volume") : T("directory")),
1065 info->extract.target);
1067 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1068 tprintf(T("Extracting \"%"TS"\" from image %d (\"%"TS"\") "
1069 "in \"%"TS"\" to \"%"TS"\"\n"),
1070 info->extract.extract_root_wim_source_path,
1071 info->extract.image,
1072 info->extract.image_name,
1073 info->extract.wimfile_name,
1074 info->extract.target);
1076 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
1077 /*tprintf(T("Applying directory structure to %"TS"\n"),*/
1078 /*info->extract.target);*/
1080 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1081 percent_done = TO_PERCENT(info->extract.completed_bytes,
1082 info->extract.total_bytes);
1083 tprintf(T("\rExtracting files: "
1084 "%"PRIu64" MiB of %"PRIu64" MiB (%u%%) done"),
1085 info->extract.completed_bytes >> 20,
1086 info->extract.total_bytes >> 20,
1088 if (info->extract.completed_bytes >= info->extract.total_bytes)
1091 case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
1092 tprintf(T("%"TS"\n"), info->extract.cur_path);
1094 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1095 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1096 tprintf(T("Setting timestamps on all extracted files...\n"));
1098 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1099 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1100 tprintf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1101 info->extract.target);
1104 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
1105 percent_done = TO_PERCENT(info->join.completed_bytes,
1106 info->join.total_bytes);
1107 tprintf(T("Writing resources from part %u of %u: "
1108 "%"PRIu64 " MiB of %"PRIu64" MiB (%u%%) written\n"),
1109 (info->join.completed_parts == info->join.total_parts) ?
1110 info->join.completed_parts : info->join.completed_parts + 1,
1111 info->join.total_parts,
1112 info->join.completed_bytes >> 20,
1113 info->join.total_bytes >> 20,
1116 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1117 percent_done = TO_PERCENT(info->split.completed_bytes,
1118 info->split.total_bytes);
1119 tprintf(T("Writing \"%"TS"\": %"PRIu64" MiB of "
1120 "%"PRIu64" MiB (%u%%) written\n"),
1121 info->split.part_name,
1122 info->split.completed_bytes >> 20,
1123 info->split.total_bytes >> 20,
1126 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1127 if (info->split.completed_bytes == info->split.total_bytes) {
1128 tprintf(T("Finished writing %u split WIM parts\n"),
1129 info->split.cur_part_number);
1139 /* Open all the split WIM parts that correspond to a file glob.
1141 * @first_part specifies the first part of the split WIM and it may be either
1142 * included or omitted from the glob. */
1144 open_swms_from_glob(const tchar *swm_glob,
1145 const tchar *first_part,
1147 WIMStruct ***additional_swms_ret,
1148 unsigned *num_additional_swms_ret)
1150 unsigned num_additional_swms = 0;
1151 WIMStruct **additional_swms = NULL;
1155 /* Warning: glob() is replaced in Windows native builds */
1156 ret = tglob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
1158 if (ret == GLOB_NOMATCH) {
1159 imagex_error(T("Found no files for glob \"%"TS"\""),
1162 imagex_error_with_errno(T("Failed to process glob \"%"TS"\""),
1168 num_additional_swms = globbuf.gl_pathc;
1169 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
1170 if (!additional_swms) {
1171 imagex_error(T("Out of memory"));
1175 unsigned offset = 0;
1176 for (unsigned i = 0; i < num_additional_swms; i++) {
1177 if (tstrcmp(globbuf.gl_pathv[i], first_part) == 0) {
1181 ret = wimlib_open_wim(globbuf.gl_pathv[i],
1182 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
1183 &additional_swms[i - offset],
1184 imagex_progress_func);
1186 goto out_close_swms;
1188 *additional_swms_ret = additional_swms;
1189 *num_additional_swms_ret = num_additional_swms - offset;
1193 for (unsigned i = 0; i < num_additional_swms; i++)
1194 wimlib_free(additional_swms[i]);
1195 free(additional_swms);
1204 parse_num_threads(const tchar *optarg)
1207 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1208 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1209 imagex_error(T("Number of threads must be a non-negative integer!"));
1217 * Parse an option passed to an update command.
1219 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1222 * @option: Text string for the option (beginning with --)
1224 * @cmd: `struct wimlib_update_command' that is being constructed for
1227 * Returns true if the option was recognized; false if not.
1230 update_command_add_option(int op, const tchar *option,
1231 struct wimlib_update_command *cmd)
1233 bool recognized = true;
1235 case WIMLIB_UPDATE_OP_ADD:
1236 if (!tstrcmp(option, T("--unix-data")))
1237 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1238 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1239 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1240 else if (!tstrcmp(option, T("--strict-acls")))
1241 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1242 else if (!tstrcmp(option, T("--dereference")))
1243 cmd->add.add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1247 case WIMLIB_UPDATE_OP_DELETE:
1248 if (!tstrcmp(option, T("--force")))
1249 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1250 else if (!tstrcmp(option, T("--recursive")))
1251 cmd->delete.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1255 case WIMLIB_UPDATE_OP_RENAME:
1262 /* How many nonoption arguments each `imagex update' command expects */
1263 static const unsigned update_command_num_nonoptions[] = {
1264 [WIMLIB_UPDATE_OP_ADD] = 2,
1265 [WIMLIB_UPDATE_OP_DELETE] = 1,
1266 [WIMLIB_UPDATE_OP_RENAME] = 2,
1270 update_command_add_nonoption(int op, const tchar *nonoption,
1271 struct wimlib_update_command *cmd,
1272 unsigned num_nonoptions)
1275 case WIMLIB_UPDATE_OP_ADD:
1276 if (num_nonoptions == 0)
1277 cmd->add.fs_source_path = (tchar*)nonoption;
1279 cmd->add.wim_target_path = (tchar*)nonoption;
1281 case WIMLIB_UPDATE_OP_DELETE:
1282 cmd->delete.wim_path = (tchar*)nonoption;
1284 case WIMLIB_UPDATE_OP_RENAME:
1285 if (num_nonoptions == 0)
1286 cmd->rename.wim_source_path = (tchar*)nonoption;
1288 cmd->rename.wim_target_path = (tchar*)nonoption;
1294 * Parse a command passed on stdin to `imagex update'.
1296 * @line: Text of the command.
1297 * @len: Length of the line, including a null terminator
1300 * @command: A `struct wimlib_update_command' to fill in from the parsed
1303 * @line_number: Line number of the command, for diagnostics.
1305 * Returns true on success; returns false on parse error.
1308 parse_update_command(tchar *line, size_t len,
1309 struct wimlib_update_command *command,
1313 tchar *command_name;
1315 size_t num_nonoptions;
1317 /* Get the command name ("add", "delete", "rename") */
1318 ret = parse_string(&line, &len, &command_name);
1319 if (ret != PARSE_STRING_SUCCESS)
1322 if (!tstrcasecmp(command_name, T("add"))) {
1323 op = WIMLIB_UPDATE_OP_ADD;
1324 } else if (!tstrcasecmp(command_name, T("delete"))) {
1325 op = WIMLIB_UPDATE_OP_DELETE;
1326 } else if (!tstrcasecmp(command_name, T("rename"))) {
1327 op = WIMLIB_UPDATE_OP_RENAME;
1329 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1330 command_name, line_number);
1335 /* Parse additional options and non-options as needed */
1340 ret = parse_string(&line, &len, &next_string);
1341 if (ret == PARSE_STRING_NONE) /* End of line */
1343 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1345 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1347 if (!update_command_add_option(op, next_string, command))
1349 imagex_error(T("Unrecognized option \"%"TS"\" to "
1350 "update command on line %zu"),
1351 next_string, line_number);
1357 if (num_nonoptions == update_command_num_nonoptions[op])
1359 imagex_error(T("Unexpected argument \"%"TS"\" in "
1360 "update command on line %zu\n"
1361 " (The \"%"TS"\" command only "
1362 "takes %u nonoption arguments!)\n"),
1363 next_string, line_number,
1364 command_name, num_nonoptions);
1367 update_command_add_nonoption(op, next_string,
1368 command, num_nonoptions);
1373 if (num_nonoptions != update_command_num_nonoptions[op]) {
1374 imagex_error(T("Not enough arguments to update command "
1375 "\"%"TS"\" on line %zu"), command_name, line_number);
1381 static struct wimlib_update_command *
1382 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1383 size_t *num_cmds_ret)
1387 struct wimlib_update_command *cmds;
1390 nlines = text_file_count_lines(cmd_file_contents_p,
1395 /* Always allocate at least 1 slot, just in case the implementation of
1396 * calloc() returns NULL if 0 bytes are requested. */
1397 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1399 imagex_error(T("out of memory"));
1402 p = *cmd_file_contents_p;
1404 for (i = 0; i < nlines; i++) {
1405 /* XXX: Could use rawmemchr() here instead, but it may not be
1406 * available on all platforms. */
1407 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1408 size_t len = endp - p + 1;
1410 if (!is_comment_line(p, len)) {
1411 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1422 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1423 * one image from a WIM file to a NTFS volume. */
1425 imagex_apply(int argc, tchar **argv)
1428 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1433 const tchar *wimfile;
1434 const tchar *target;
1435 const tchar *image_num_or_name;
1436 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1438 const tchar *swm_glob = NULL;
1439 WIMStruct **additional_swms = NULL;
1440 unsigned num_additional_swms = 0;
1442 for_opt(c, apply_options) {
1444 case IMAGEX_CHECK_OPTION:
1445 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1447 case IMAGEX_HARDLINK_OPTION:
1448 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1450 case IMAGEX_SYMLINK_OPTION:
1451 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1453 case IMAGEX_VERBOSE_OPTION:
1454 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
1456 case IMAGEX_REF_OPTION:
1459 case IMAGEX_UNIX_DATA_OPTION:
1460 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1462 case IMAGEX_NO_ACLS_OPTION:
1463 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1465 case IMAGEX_STRICT_ACLS_OPTION:
1466 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1468 case IMAGEX_NORPFIX_OPTION:
1469 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1471 case IMAGEX_RPFIX_OPTION:
1472 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1481 if (argc != 2 && argc != 3) {
1488 image_num_or_name = T("1");
1491 image_num_or_name = argv[1];
1495 ret = wimlib_open_wim(wimfile, open_flags, &w, imagex_progress_func);
1499 image = wimlib_resolve_image(w, image_num_or_name);
1500 ret = verify_image_exists(image, image_num_or_name, wimfile);
1504 num_images = wimlib_get_num_images(w);
1505 if (argc == 2 && num_images != 1) {
1506 imagex_error(T("\"%"TS"\" contains %d images; Please select one "
1507 "(or all)"), wimfile, num_images);
1514 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1516 &num_additional_swms);
1523 ret = tstat(target, &stbuf);
1525 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1526 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1528 if (errno != ENOENT) {
1529 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1537 win32_acquire_restore_privileges();
1539 ret = wimlib_extract_image(w, image, target, extract_flags,
1540 additional_swms, num_additional_swms,
1541 imagex_progress_func);
1543 tprintf(T("Done applying WIM image.\n"));
1545 win32_release_restore_privileges();
1549 if (additional_swms) {
1550 for (unsigned i = 0; i < num_additional_swms; i++)
1551 wimlib_free(additional_swms[i]);
1552 free(additional_swms);
1557 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1558 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1559 * the desired image. 'wimlib-imagex append': add a new image to an existing
1562 imagex_capture_or_append(int argc, tchar **argv)
1566 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
1567 int write_flags = 0;
1568 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
1569 const tchar *wimfile;
1572 const tchar *flags_element = NULL;
1576 int cmd = tstrcmp(argv[0], T("append")) ? CAPTURE : APPEND;
1577 unsigned num_threads = 0;
1580 size_t source_name_len;
1583 const tchar *config_file = NULL;
1585 struct wimlib_capture_config *config = NULL;
1587 bool source_list = false;
1588 size_t source_list_nchars;
1589 tchar *source_list_contents;
1590 bool capture_sources_malloced;
1591 struct wimlib_capture_source *capture_sources;
1594 for_opt(c, capture_or_append_options) {
1596 case IMAGEX_BOOT_OPTION:
1597 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1599 case IMAGEX_CHECK_OPTION:
1600 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1601 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1603 case IMAGEX_CONFIG_OPTION:
1604 config_file = optarg;
1606 case IMAGEX_COMPRESS_OPTION:
1607 compression_type = get_compression_type(optarg);
1608 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1611 case IMAGEX_FLAGS_OPTION:
1612 flags_element = optarg;
1614 case IMAGEX_DEREFERENCE_OPTION:
1615 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1617 case IMAGEX_VERBOSE_OPTION:
1618 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1620 case IMAGEX_THREADS_OPTION:
1621 num_threads = parse_num_threads(optarg);
1622 if (num_threads == UINT_MAX)
1625 case IMAGEX_REBUILD_OPTION:
1626 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1628 case IMAGEX_UNIX_DATA_OPTION:
1629 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1631 case IMAGEX_SOURCE_LIST_OPTION:
1634 case IMAGEX_NO_ACLS_OPTION:
1635 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1637 case IMAGEX_STRICT_ACLS_OPTION:
1638 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1640 case IMAGEX_RPFIX_OPTION:
1641 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1643 case IMAGEX_NORPFIX_OPTION:
1644 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1654 if (argc < 2 || argc > 4) {
1665 /* Set default name to SOURCE argument, omitting any directory
1666 * prefixes and trailing slashes. This requires making a copy
1668 source_name_len = tstrlen(source);
1669 source_copy = alloca((source_name_len + 1) * sizeof(tchar));
1670 name = tbasename(tstrcpy(source_copy, source));
1672 /* Image description defaults to NULL if not given. */
1673 desc = (argc >= 4) ? argv[3] : NULL;
1676 /* Set up capture sources in source list mode */
1677 if (source[0] == T('-') && source[1] == T('\0')) {
1678 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1680 source_list_contents = file_get_text_contents(source,
1681 &source_list_nchars);
1683 if (!source_list_contents)
1686 capture_sources = parse_source_list(&source_list_contents,
1689 if (!capture_sources) {
1691 goto out_free_source_list_contents;
1693 capture_sources_malloced = true;
1695 /* Set up capture source in non-source-list mode (could be
1696 * either "normal" mode or "NTFS mode"--- see the man page). */
1697 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1698 capture_sources[0].fs_source_path = source;
1699 capture_sources[0].wim_target_path = NULL;
1700 capture_sources[0].reserved = 0;
1702 capture_sources_malloced = false;
1703 source_list_contents = NULL;
1709 config_str = file_get_text_contents(config_file, &config_len);
1712 goto out_free_capture_sources;
1715 config = alloca(sizeof(*config));
1716 ret = parse_capture_config(&config_str, config_len, config);
1718 goto out_free_config;
1720 config = &default_capture_config;
1724 ret = wimlib_open_wim(wimfile, open_flags, &w,
1725 imagex_progress_func);
1727 ret = wimlib_create_new_wim(compression_type, &w);
1729 goto out_free_config;
1733 ret = tstat(source, &stbuf);
1735 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1736 tprintf(T("Capturing WIM image from NTFS "
1737 "filesystem on \"%"TS"\"\n"), source);
1738 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1741 if (errno != ENOENT) {
1742 imagex_error_with_errno(T("Failed to stat "
1743 "\"%"TS"\""), source);
1745 goto out_wimlib_free;
1750 win32_acquire_capture_privileges();
1753 ret = wimlib_add_image_multisource(w,
1759 imagex_progress_func);
1761 goto out_release_privs;
1762 cur_image = wimlib_get_num_images(w);
1764 ret = wimlib_set_image_descripton(w, cur_image, desc);
1766 goto out_release_privs;
1768 if (flags_element) {
1769 ret = wimlib_set_image_flags(w, cur_image, flags_element);
1771 goto out_release_privs;
1773 if (cmd == APPEND) {
1774 ret = wimlib_overwrite(w, write_flags, num_threads,
1775 imagex_progress_func);
1777 ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
1778 num_threads, imagex_progress_func);
1780 if (ret == WIMLIB_ERR_REOPEN)
1783 imagex_error(T("Failed to write the WIM file \"%"TS"\""),
1787 win32_release_capture_privileges();
1792 if (config != NULL && config != &default_capture_config) {
1793 free(config->exclusion_pats.pats);
1794 free(config->exclusion_exception_pats.pats);
1797 out_free_capture_sources:
1798 if (capture_sources_malloced)
1799 free(capture_sources);
1800 out_free_source_list_contents:
1801 free(source_list_contents);
1805 /* Remove image(s) from a WIM. */
1807 imagex_delete(int argc, tchar **argv)
1811 int write_flags = 0;
1812 const tchar *wimfile;
1813 const tchar *image_num_or_name;
1818 for_opt(c, delete_options) {
1820 case IMAGEX_CHECK_OPTION:
1821 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1822 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1824 case IMAGEX_SOFT_OPTION:
1825 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
1837 imagex_error(T("Must specify a WIM file"));
1839 imagex_error(T("Must specify an image"));
1844 image_num_or_name = argv[1];
1846 ret = file_writable(wimfile);
1850 ret = wimlib_open_wim(wimfile, open_flags, &w,
1851 imagex_progress_func);
1855 image = wimlib_resolve_image(w, image_num_or_name);
1857 ret = verify_image_exists(image, image_num_or_name, wimfile);
1861 ret = wimlib_delete_image(w, image);
1863 imagex_error(T("Failed to delete image from \"%"TS"\""), wimfile);
1867 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1868 if (ret == WIMLIB_ERR_REOPEN)
1871 imagex_error(T("Failed to write the file \"%"TS"\" with image "
1872 "deleted"), wimfile);
1879 /* Print the files contained in an image(s) in a WIM file. */
1881 imagex_dir(int argc, tchar **argv)
1883 const tchar *wimfile;
1890 imagex_error(T("Must specify a WIM file"));
1895 imagex_error(T("Too many arguments"));
1901 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w,
1902 imagex_progress_func);
1907 image = wimlib_resolve_image(w, argv[2]);
1908 ret = verify_image_exists(image, argv[2], wimfile);
1912 /* Image was not specified. If the WIM only contains one image,
1913 * choose that one; otherwise, print an error. */
1914 num_images = wimlib_get_num_images(w);
1915 if (num_images != 1) {
1916 imagex_error(T("The file \"%"TS"\" contains %d images; Please "
1917 "select one."), wimfile, num_images);
1925 ret = wimlib_print_files(w, image);
1931 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
1934 imagex_export(int argc, tchar **argv)
1938 int export_flags = 0;
1939 int write_flags = 0;
1940 int compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
1941 bool compression_type_specified = false;
1942 const tchar *src_wimfile;
1943 const tchar *src_image_num_or_name;
1944 const tchar *dest_wimfile;
1945 const tchar *dest_name;
1946 const tchar *dest_desc;
1947 WIMStruct *src_w = NULL;
1948 WIMStruct *dest_w = NULL;
1953 const tchar *swm_glob = NULL;
1954 WIMStruct **additional_swms = NULL;
1955 unsigned num_additional_swms = 0;
1956 unsigned num_threads = 0;
1958 for_opt(c, export_options) {
1960 case IMAGEX_BOOT_OPTION:
1961 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
1963 case IMAGEX_CHECK_OPTION:
1964 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1965 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1967 case IMAGEX_COMPRESS_OPTION:
1968 compression_type = get_compression_type(optarg);
1969 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1971 compression_type_specified = true;
1973 case IMAGEX_REF_OPTION:
1976 case IMAGEX_THREADS_OPTION:
1977 num_threads = parse_num_threads(optarg);
1978 if (num_threads == UINT_MAX)
1981 case IMAGEX_REBUILD_OPTION:
1982 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1991 if (argc < 3 || argc > 5) {
1995 src_wimfile = argv[0];
1996 src_image_num_or_name = argv[1];
1997 dest_wimfile = argv[2];
1998 dest_name = (argc >= 4) ? argv[3] : NULL;
1999 dest_desc = (argc >= 5) ? argv[4] : NULL;
2000 ret = wimlib_open_wim(src_wimfile,
2001 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w,
2002 imagex_progress_func);
2006 /* Determine if the destination is an existing file or not.
2007 * If so, we try to append the exported image(s) to it; otherwise, we
2008 * create a new WIM containing the exported image(s). */
2009 if (tstat(dest_wimfile, &stbuf) == 0) {
2013 /* Destination file exists. */
2015 if (!S_ISREG(stbuf.st_mode)) {
2016 imagex_error(T("\"%"TS"\" is not a regular file"),
2021 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w,
2022 imagex_progress_func);
2026 ret = file_writable(dest_wimfile);
2030 dest_ctype = wimlib_get_compression_type(dest_w);
2031 if (compression_type_specified
2032 && compression_type != dest_ctype)
2034 imagex_error(T("Cannot specify a compression type that is "
2035 "not the same as that used in the "
2036 "destination WIM"));
2042 /* dest_wimfile is not an existing file, so create a new WIM. */
2043 if (!compression_type_specified)
2044 compression_type = wimlib_get_compression_type(src_w);
2045 if (errno == ENOENT) {
2046 ret = wimlib_create_new_wim(compression_type, &dest_w);
2050 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2057 image = wimlib_resolve_image(src_w, src_image_num_or_name);
2058 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2063 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
2065 &num_additional_swms);
2070 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
2071 export_flags, additional_swms,
2072 num_additional_swms, imagex_progress_func);
2078 ret = wimlib_write(dest_w, dest_wimfile, WIMLIB_ALL_IMAGES,
2079 write_flags, num_threads,
2080 imagex_progress_func);
2082 ret = wimlib_overwrite(dest_w, write_flags, num_threads,
2083 imagex_progress_func);
2085 if (ret == WIMLIB_ERR_REOPEN)
2088 wimlib_free(dest_w);
2089 if (additional_swms) {
2090 for (unsigned i = 0; i < num_additional_swms; i++)
2091 wimlib_free(additional_swms[i]);
2092 free(additional_swms);
2098 is_root_wim_path(const tchar *path)
2101 for (p = path; *p; p++)
2102 if (*p != T('\\') && *p != T('/'))
2108 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2109 const tchar *dest_dir)
2111 for (size_t i = 0; i < num_cmds; i++)
2112 if (cmds[i].fs_dest_path != dest_dir)
2113 free(cmds[i].fs_dest_path);
2117 static struct wimlib_extract_command *
2118 prepare_extract_commands(tchar **paths, unsigned num_paths,
2119 int extract_flags, tchar *dest_dir,
2120 size_t *num_cmds_ret)
2122 struct wimlib_extract_command *cmds;
2124 tchar *emptystr = T("");
2126 if (num_paths == 0) {
2130 num_cmds = num_paths;
2131 cmds = calloc(num_cmds, sizeof(cmds[0]));
2133 imagex_error(T("Out of memory!"));
2137 for (size_t i = 0; i < num_cmds; i++) {
2138 cmds[i].extract_flags = extract_flags;
2139 cmds[i].wim_source_path = paths[i];
2140 if (is_root_wim_path(paths[i])) {
2141 cmds[i].fs_dest_path = dest_dir;
2143 size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2144 cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2145 if (!cmds[i].fs_dest_path) {
2146 free_extract_commands(cmds, num_cmds, dest_dir);
2149 tsprintf(cmds[i].fs_dest_path, T("%"TS"/%"TS), dest_dir,
2150 tbasename(paths[i]));
2153 *num_cmds_ret = num_cmds;
2157 /* Extract files or directories from a WIM image */
2159 imagex_extract(int argc, tchar **argv)
2162 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2166 const tchar *wimfile;
2167 const tchar *image_num_or_name;
2168 tchar *dest_dir = T(".");
2169 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL | WIMLIB_EXTRACT_FLAG_NORPFIX;
2171 const tchar *swm_glob = NULL;
2172 WIMStruct **additional_swms = NULL;
2173 unsigned num_additional_swms = 0;
2175 struct wimlib_extract_command *cmds;
2178 for_opt(c, extract_options) {
2180 case IMAGEX_CHECK_OPTION:
2181 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2183 case IMAGEX_VERBOSE_OPTION:
2184 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
2186 case IMAGEX_REF_OPTION:
2189 case IMAGEX_UNIX_DATA_OPTION:
2190 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2192 case IMAGEX_NO_ACLS_OPTION:
2193 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2195 case IMAGEX_STRICT_ACLS_OPTION:
2196 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2198 case IMAGEX_DEST_DIR_OPTION:
2201 case IMAGEX_TO_STDOUT_OPTION:
2202 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2203 imagex_be_quiet = true;
2216 image_num_or_name = argv[1];
2221 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2228 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2232 image = wimlib_resolve_image(wim, image_num_or_name);
2233 ret = verify_image_exists_and_is_single(image,
2237 goto out_wimlib_free;
2240 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2242 &num_additional_swms);
2244 goto out_wimlib_free;
2248 win32_acquire_restore_privileges();
2251 ret = wimlib_extract_files(wim, image, 0, cmds, num_cmds,
2252 additional_swms, num_additional_swms,
2253 imagex_progress_func);
2255 if (!imagex_be_quiet)
2256 tprintf(T("Done extracting files.\n"));
2257 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2258 tfprintf(stderr, T("Note: You can use `"IMAGEX_PROGNAME" dir' to see what "
2259 "files and directories\n"
2260 " are in the WIM image.\n"));
2263 win32_release_restore_privileges();
2265 if (additional_swms) {
2266 for (unsigned i = 0; i < num_additional_swms; i++)
2267 wimlib_free(additional_swms[i]);
2268 free(additional_swms);
2273 free_extract_commands(cmds, num_cmds, dest_dir);
2282 /* Prints information about a WIM file; also can mark an image as bootable,
2283 * change the name of an image, or change the description of an image. */
2285 imagex_info(int argc, tchar **argv)
2290 bool header = false;
2291 bool lookup_table = false;
2293 bool metadata = false;
2294 bool short_header = true;
2295 const tchar *xml_out_file = NULL;
2296 const tchar *wimfile;
2297 const tchar *image_num_or_name = T("all");
2298 const tchar *new_name = NULL;
2299 const tchar *new_desc = NULL;
2304 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2309 for_opt(c, info_options) {
2311 case IMAGEX_BOOT_OPTION:
2314 case IMAGEX_CHECK_OPTION:
2317 case IMAGEX_HEADER_OPTION:
2319 short_header = false;
2321 case IMAGEX_LOOKUP_TABLE_OPTION:
2322 lookup_table = true;
2323 short_header = false;
2325 case IMAGEX_XML_OPTION:
2327 short_header = false;
2329 case IMAGEX_EXTRACT_XML_OPTION:
2330 xml_out_file = optarg;
2331 short_header = false;
2333 case IMAGEX_METADATA_OPTION:
2335 short_header = false;
2345 if (argc == 0 || argc > 4) {
2351 image_num_or_name = argv[1];
2361 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2363 ret = wimlib_open_wim(wimfile, open_flags, &w,
2364 imagex_progress_func);
2368 part_number = wimlib_get_part_number(w, &total_parts);
2370 image = wimlib_resolve_image(w, image_num_or_name);
2371 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
2372 imagex_error(T("The image \"%"TS"\" does not exist"),
2375 imagex_error(T("If you would like to set the boot "
2376 "index to 0, specify image \"0\" with "
2377 "the --boot flag."));
2379 ret = WIMLIB_ERR_INVALID_IMAGE;
2383 num_images = wimlib_get_num_images(w);
2385 if (num_images == 0) {
2387 imagex_error(T("--boot is meaningless on a WIM with no "
2389 ret = WIMLIB_ERR_INVALID_IMAGE;
2394 if (image == WIMLIB_ALL_IMAGES && num_images > 1) {
2396 imagex_error(T("Cannot specify the --boot flag "
2397 "without specifying a specific "
2398 "image in a multi-image WIM"));
2399 ret = WIMLIB_ERR_INVALID_IMAGE;
2403 imagex_error(T("Cannot specify the NEW_NAME "
2404 "without specifying a specific "
2405 "image in a multi-image WIM"));
2406 ret = WIMLIB_ERR_INVALID_IMAGE;
2411 /* Operations that print information are separated from operations that
2412 * recreate the WIM file. */
2413 if (!new_name && !boot) {
2415 /* Read-only operations */
2417 if (image == WIMLIB_NO_IMAGE) {
2418 imagex_error(T("\"%"TS"\" is not a valid image"),
2420 ret = WIMLIB_ERR_INVALID_IMAGE;
2424 if (image == WIMLIB_ALL_IMAGES && short_header)
2425 wimlib_print_wim_information(w);
2428 wimlib_print_header(w);
2431 if (total_parts != 1) {
2432 tprintf(T("Warning: Only showing the lookup table "
2433 "for part %d of a %d-part WIM.\n"),
2434 part_number, total_parts);
2436 wimlib_print_lookup_table(w);
2440 ret = wimlib_extract_xml_data(w, stdout);
2446 fp = tfopen(xml_out_file, T("wb"));
2448 imagex_error_with_errno(T("Failed to open the "
2449 "file \"%"TS"\" for "
2455 ret = wimlib_extract_xml_data(w, fp);
2456 if (fclose(fp) != 0) {
2457 imagex_error(T("Failed to close the file "
2468 wimlib_print_available_images(w, image);
2471 ret = wimlib_print_metadata(w, image);
2477 /* Modification operations */
2478 if (total_parts != 1) {
2479 imagex_error(T("Modifying a split WIM is not supported."));
2483 if (image == WIMLIB_ALL_IMAGES)
2486 if (image == WIMLIB_NO_IMAGE && new_name) {
2487 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
2488 "when using image 0"), new_name);
2494 if (image == wimlib_get_boot_idx(w)) {
2495 tprintf(T("Image %d is already marked as "
2496 "bootable.\n"), image);
2499 tprintf(T("Marking image %d as bootable.\n"),
2501 wimlib_set_boot_idx(w, image);
2505 if (!tstrcmp(wimlib_get_image_name(w, image), new_name))
2507 tprintf(T("Image %d is already named \"%"TS"\".\n"),
2511 tprintf(T("Changing the name of image %d to "
2512 "\"%"TS"\".\n"), image, new_name);
2513 ret = wimlib_set_image_name(w, image, new_name);
2519 const tchar *old_desc;
2520 old_desc = wimlib_get_image_description(w, image);
2521 if (old_desc && !tstrcmp(old_desc, new_desc)) {
2522 tprintf(T("The description of image %d is already "
2523 "\"%"TS"\".\n"), image, new_desc);
2526 tprintf(T("Changing the description of image %d "
2527 "to \"%"TS"\".\n"), image, new_desc);
2528 ret = wimlib_set_image_descripton(w, image,
2535 /* Only call wimlib_overwrite() if something actually needs to
2537 if (boot || new_name || new_desc ||
2538 (check && !wimlib_has_integrity_table(w)))
2542 ret = file_writable(wimfile);
2547 write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2551 ret = wimlib_overwrite(w, write_flags, 1,
2552 imagex_progress_func);
2553 if (ret == WIMLIB_ERR_REOPEN)
2556 tprintf(T("The file \"%"TS"\" was not modified because nothing "
2557 "needed to be done.\n"), wimfile);
2566 /* Join split WIMs into one part WIM */
2568 imagex_join(int argc, tchar **argv)
2571 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2572 int wim_write_flags = 0;
2573 const tchar *output_path;
2575 for_opt(c, join_options) {
2577 case IMAGEX_CHECK_OPTION:
2578 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2579 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2589 imagex_error(T("Must specify one or more split WIM (.swm) "
2593 output_path = argv[0];
2594 return wimlib_join((const tchar * const *)++argv,
2599 imagex_progress_func);
2605 /* Mounts an image using a FUSE mount. */
2607 imagex_mount_rw_or_ro(int argc, tchar **argv)
2610 int mount_flags = 0;
2611 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2612 const tchar *wimfile;
2618 const tchar *swm_glob = NULL;
2619 WIMStruct **additional_swms = NULL;
2620 unsigned num_additional_swms = 0;
2621 const tchar *staging_dir = NULL;
2623 if (!tstrcmp(argv[0], T("mountrw")))
2624 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
2626 for_opt(c, mount_options) {
2628 case IMAGEX_ALLOW_OTHER_OPTION:
2629 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
2631 case IMAGEX_CHECK_OPTION:
2632 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2634 case IMAGEX_DEBUG_OPTION:
2635 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
2637 case IMAGEX_STREAMS_INTERFACE_OPTION:
2638 if (!tstrcasecmp(optarg, T("none")))
2639 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
2640 else if (!tstrcasecmp(optarg, T("xattr")))
2641 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2642 else if (!tstrcasecmp(optarg, T("windows")))
2643 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
2645 imagex_error(T("Unknown stream interface \"%"TS"\""),
2650 case IMAGEX_REF_OPTION:
2653 case IMAGEX_STAGING_DIR_OPTION:
2654 staging_dir = optarg;
2656 case IMAGEX_UNIX_DATA_OPTION:
2657 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
2665 if (argc != 2 && argc != 3)
2670 ret = wimlib_open_wim(wimfile, open_flags, &w,
2671 imagex_progress_func);
2676 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2678 &num_additional_swms);
2685 num_images = wimlib_get_num_images(w);
2686 if (num_images != 1) {
2687 imagex_error(T("The file \"%"TS"\" contains %d images; Please "
2688 "select one."), wimfile, num_images);
2689 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2696 image = wimlib_resolve_image(w, argv[1]);
2698 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
2703 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2704 ret = file_writable(wimfile);
2709 ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
2710 num_additional_swms, staging_dir);
2712 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
2714 image, wimfile, dir);
2719 if (additional_swms) {
2720 for (unsigned i = 0; i < num_additional_swms; i++)
2721 wimlib_free(additional_swms[i]);
2722 free(additional_swms);
2726 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2731 /* Rebuild a WIM file */
2733 imagex_optimize(int argc, tchar **argv)
2737 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
2740 const tchar *wimfile;
2743 unsigned num_threads = 0;
2745 for_opt(c, optimize_options) {
2747 case IMAGEX_CHECK_OPTION:
2748 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2749 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2751 case IMAGEX_RECOMPRESS_OPTION:
2752 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
2754 case IMAGEX_THREADS_OPTION:
2755 num_threads = parse_num_threads(optarg);
2756 if (num_threads == UINT_MAX)
2774 ret = wimlib_open_wim(wimfile, open_flags, &w,
2775 imagex_progress_func);
2779 old_size = file_get_size(argv[0]);
2780 tprintf(T("\"%"TS"\" original size: "), wimfile);
2782 tfputs(T("Unknown\n"), stdout);
2784 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
2786 ret = wimlib_overwrite(w, write_flags, num_threads,
2787 imagex_progress_func);
2790 new_size = file_get_size(argv[0]);
2791 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
2793 tfputs(T("Unknown\n"), stdout);
2795 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
2797 tfputs(T("Space saved: "), stdout);
2798 if (new_size != -1 && old_size != -1) {
2799 tprintf(T("%lld KiB\n"),
2800 ((long long)old_size - (long long)new_size) >> 10);
2802 tfputs(T("Unknown\n"), stdout);
2810 /* Split a WIM into a spanned set */
2812 imagex_split(int argc, tchar **argv)
2815 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2816 int write_flags = 0;
2817 unsigned long part_size;
2822 for_opt(c, split_options) {
2824 case IMAGEX_CHECK_OPTION:
2825 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2826 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2840 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
2841 if (tmp == argv[2] || *tmp) {
2842 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
2843 imagex_error(T("The part size must be an integer or "
2844 "floating-point number of megabytes."));
2847 ret = wimlib_open_wim(argv[0], open_flags, &w, imagex_progress_func);
2850 ret = wimlib_split(w, argv[1], part_size, write_flags, imagex_progress_func);
2855 /* Unmounts a mounted WIM image. */
2857 imagex_unmount(int argc, tchar **argv)
2860 int unmount_flags = 0;
2863 for_opt(c, unmount_options) {
2865 case IMAGEX_COMMIT_OPTION:
2866 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
2868 case IMAGEX_CHECK_OPTION:
2869 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
2871 case IMAGEX_REBUILD_OPTION:
2872 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
2886 ret = wimlib_unmount_image(argv[0], unmount_flags,
2887 imagex_progress_func);
2889 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
2894 * Add, delete, or rename files in a WIM image.
2897 imagex_update(int argc, tchar **argv)
2899 const tchar *wimfile;
2900 const tchar *image_num_or_name;
2905 int write_flags = WIMLIB_WRITE_FLAG_SOFT_DELETE;
2906 int update_flags = 0;
2907 int default_add_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE;
2908 int default_delete_flags = 0;
2909 unsigned num_threads = 0;
2911 tchar *cmd_file_contents;
2912 size_t cmd_file_nchars;
2913 struct wimlib_update_command *cmds;
2916 const tchar *config_file = NULL;
2918 struct wimlib_capture_config *config = NULL;
2920 for_opt(c, update_options) {
2922 case IMAGEX_THREADS_OPTION:
2923 num_threads = parse_num_threads(optarg);
2924 if (num_threads == UINT_MAX) {
2929 case IMAGEX_CHECK_OPTION:
2930 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2931 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2933 case IMAGEX_REBUILD_OPTION:
2934 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2936 case IMAGEX_FORCE_OPTION:
2937 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
2939 case IMAGEX_RECURSIVE_OPTION:
2940 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
2942 case IMAGEX_CONFIG_OPTION:
2943 config_file = optarg;
2945 case IMAGEX_DEREFERENCE_OPTION:
2946 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
2948 case IMAGEX_UNIX_DATA_OPTION:
2949 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
2951 case IMAGEX_NO_ACLS_OPTION:
2952 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
2954 case IMAGEX_STRICT_ACLS_OPTION:
2955 default_add_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
2964 if (argc < 1 || argc > 2)
2967 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2972 image_num_or_name = argv[1];
2974 image_num_or_name = T("1");
2976 image = wimlib_resolve_image(wim, image_num_or_name);
2978 ret = verify_image_exists_and_is_single(image, image_num_or_name,
2981 goto out_wimlib_free;
2983 /* Parse capture configuration file if specified */
2987 config_str = file_get_text_contents(config_file, &config_len);
2990 goto out_wimlib_free;
2993 config = alloca(sizeof(*config));
2994 ret = parse_capture_config(&config_str, config_len, config);
2996 goto out_free_config;
2998 config = &default_capture_config;
3001 /* Read update commands from standard input */
3002 if (isatty(STDIN_FILENO)) {
3003 tputs(T("Reading update commands from standard input..."));
3004 recommend_man_page(T("update"));
3006 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3007 if (!cmd_file_contents) {
3009 goto out_free_config;
3012 /* Parse the update commands */
3013 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3017 goto out_free_cmd_file_contents;
3020 /* Set default flags and capture config on the update commands */
3021 for (size_t i = 0; i < num_cmds; i++) {
3022 switch (cmds[i].op) {
3023 case WIMLIB_UPDATE_OP_ADD:
3024 cmds[i].add.add_flags |= default_add_flags;
3025 cmds[i].add.config = config;
3027 case WIMLIB_UPDATE_OP_DELETE:
3028 cmds[i].delete.delete_flags |= default_delete_flags;
3035 /* Execute the update commands */
3036 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3037 imagex_progress_func);
3041 /* Overwrite the updated WIM */
3042 ret = wimlib_overwrite(wim, write_flags, num_threads,
3043 imagex_progress_func);
3046 out_free_cmd_file_contents:
3047 free(cmd_file_contents);
3049 if (config != NULL && config != &default_capture_config) {
3050 free(config->exclusion_pats.pats);
3051 free(config->exclusion_exception_pats.pats);
3064 struct imagex_command {
3066 int (*func)(int , tchar **);
3071 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
3072 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
3074 static const struct imagex_command imagex_commands[] = {
3075 {T("append"), imagex_capture_or_append, APPEND},
3076 {T("apply"), imagex_apply, APPLY},
3077 {T("capture"), imagex_capture_or_append, CAPTURE},
3078 {T("delete"), imagex_delete, DELETE},
3079 {T("dir"), imagex_dir, DIR},
3080 {T("export"), imagex_export, EXPORT},
3081 {T("extract"), imagex_extract, EXTRACT},
3082 {T("info"), imagex_info, INFO},
3083 {T("join"), imagex_join, JOIN},
3084 {T("mount"), imagex_mount_rw_or_ro, MOUNT},
3085 {T("mountrw"), imagex_mount_rw_or_ro, MOUNTRW},
3086 {T("optimize"),imagex_optimize, OPTIMIZE},
3087 {T("split"), imagex_split, SPLIT},
3088 {T("unmount"), imagex_unmount, UNMOUNT},
3089 {T("update"), imagex_update, UPDATE},
3095 static const tchar *s =
3097 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
3098 "Copyright (C) 2012, 2013 Eric Biggers\n"
3099 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
3100 "This is free software: you are free to change and redistribute it.\n"
3101 "There is NO WARRANTY, to the extent permitted by law.\n"
3103 "Report bugs to "PACKAGE_BUGREPORT".\n"
3110 help_or_version(int argc, tchar **argv)
3114 const struct imagex_command *cmd;
3116 for (i = 1; i < argc; i++) {
3124 if (!tstrcmp(p, T("help"))) {
3125 for_imagex_command(cmd) {
3126 if (!tstrcmp(cmd->name, argv[1])) {
3134 if (!tstrcmp(p, T("version"))) {
3145 const struct imagex_command *cmd;
3146 tprintf(T("Usage:\n%"TS), usage_strings[cmd_type]);
3147 for_imagex_command(cmd) {
3148 if (cmd->cmd == cmd_type) {
3149 tputc(T('\n'), stdout);
3150 recommend_man_page(cmd->name);
3158 tfputs(T("Usage:\n"), stdout);
3159 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
3160 tprintf(T(" %"TS), usage_strings[i]);
3161 static const tchar *extra =
3163 " "IMAGEX_PROGNAME" --help\n"
3164 " "IMAGEX_PROGNAME" --version\n"
3166 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
3168 " Try `man "IMAGEX_PROGNAME"' for more information.\n"
3170 tfputs(extra, stdout);
3173 /* Entry point for wimlib's ImageX implementation. On UNIX the command
3174 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
3175 * something else), while an Windows the command arguments will be UTF-16LE
3176 * encoded 'wchar_t' strings. */
3179 wmain(int argc, wchar_t **argv, wchar_t **envp)
3181 main(int argc, char **argv)
3184 const struct imagex_command *cmd;
3189 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
3190 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
3194 setlocale(LC_ALL, "");
3195 codeset = nl_langinfo(CODESET);
3196 if (!strstr(codeset, "UTF-8") &&
3197 !strstr(codeset, "UTF8") &&
3198 !strstr(codeset, "utf-8") &&
3199 !strstr(codeset, "utf8"))
3202 "WARNING: Running "IMAGEX_PROGNAME" in a UTF-8 locale is recommended!\n"
3203 " Maybe try: `export LANG=en_US.UTF-8'?\n"
3204 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
3205 " to any value to force wimlib to use UTF-8.\n",
3210 #endif /* !__WIN32__ */
3213 imagex_error(T("No command specified"));
3219 /* Handle --help and --version for all commands. Note that this will
3220 * not return if either of these arguments are present. */
3221 help_or_version(argc, argv);
3225 /* The user may like to see more informative error messages. */
3226 wimlib_set_print_errors(true);
3228 /* Do any initializations that the library needs */
3229 ret = wimlib_global_init(init_flags);
3231 goto out_check_status;
3233 /* Search for the function to handle the ImageX subcommand. */
3234 for_imagex_command(cmd) {
3235 if (!tstrcmp(cmd->name, *argv)) {
3236 ret = cmd->func(argc, argv);
3237 goto out_check_write_error;
3241 imagex_error(T("Unrecognized command: `%"TS"'"), argv[0]);
3245 out_check_write_error:
3246 /* For 'wimlib-imagex info' and 'wimlib-imagex dir', data printed to
3247 * standard output is part of the program's actual behavior and not just
3248 * for informational purposes, so we should set a failure exit status if
3249 * there was a write error. */
3250 if (cmd == &imagex_commands[INFO] || cmd == &imagex_commands[DIR]) {
3251 if (ferror(stdout) || fclose(stdout)) {
3252 imagex_error_with_errno(T("error writing to standard output"));
3258 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
3259 * outside of the wimlib library code. 0 indicates success. > 0
3260 * indicates a wimlib error code from which an error message can be
3263 imagex_error(T("Exiting with error code %d:\n"
3265 wimlib_get_error_string(ret));
3266 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
3267 imagex_error_with_errno(T("errno"));
3270 /* Make the library free any resources it's holding (not strictly
3271 * necessary because the process is ending anyway). */
3272 wimlib_global_cleanup();