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 OS_PREFERRED_PATH_SEPARATOR L'\\'
53 # define OS_PREFERRED_PATH_SEPARATOR_STRING L"\\"
57 # include <langinfo.h>
58 # define tbasename basename
59 # define OS_PREFERRED_PATH_SEPARATOR '/'
60 # define OS_PREFERRED_PATH_SEPARATOR_STRING "/"
61 static inline void set_fd_to_binary_mode(int fd)
66 /* Don't confuse the user by presenting the mounting commands on Windows when
67 * they will never work. However on UNIX-like systems we always present them,
68 * even if WITH_FUSE is not defined at this point, as to not tie the build of
69 * wimlib-imagex to a specific build of wimlib. */
71 # define WIM_MOUNTING_SUPPORTED 0
73 # define WIM_MOUNTING_SUPPORTED 1
76 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
78 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
92 #if WIM_MOUNTING_SUPPORTED
98 #if WIM_MOUNTING_SUPPORTED
105 static void usage(int cmd, FILE *fp);
106 static void usage_all(FILE *fp);
107 static void recommend_man_page(int cmd, FILE *fp);
108 static const tchar *get_cmd_string(int cmd, bool nospace);
110 static int imagex_progress_func(enum wimlib_progress_msg msg,
111 const union wimlib_progress_info *info);
113 static bool imagex_be_quiet = false;
114 static FILE *imagex_info_file;
116 #define imagex_printf(format, ...) \
117 tfprintf(imagex_info_file, format, ##__VA_ARGS__)
120 IMAGEX_ALLOW_OTHER_OPTION,
123 IMAGEX_CHUNK_SIZE_OPTION,
124 IMAGEX_COMMAND_OPTION,
125 IMAGEX_COMMIT_OPTION,
126 IMAGEX_COMPRESS_OPTION,
127 IMAGEX_COMPRESS_SLOW_OPTION,
128 IMAGEX_CONFIG_OPTION,
130 IMAGEX_DELTA_FROM_OPTION,
131 IMAGEX_DEREFERENCE_OPTION,
132 IMAGEX_DEST_DIR_OPTION,
133 IMAGEX_EXTRACT_XML_OPTION,
136 IMAGEX_HARDLINK_OPTION,
137 IMAGEX_HEADER_OPTION,
138 IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
140 IMAGEX_LOOKUP_TABLE_OPTION,
141 IMAGEX_METADATA_OPTION,
142 IMAGEX_NORPFIX_OPTION,
143 IMAGEX_NOCHECK_OPTION,
144 IMAGEX_NO_ACLS_OPTION,
145 IMAGEX_NO_WILDCARDS_OPTION,
146 IMAGEX_NOT_PIPABLE_OPTION,
147 IMAGEX_PACK_STREAMS_OPTION,
149 IMAGEX_PIPABLE_OPTION,
150 IMAGEX_REBUILD_OPTION,
151 IMAGEX_RECOMPRESS_OPTION,
152 IMAGEX_RECURSIVE_OPTION,
154 IMAGEX_RESUME_OPTION,
157 IMAGEX_SOURCE_LIST_OPTION,
158 IMAGEX_STAGING_DIR_OPTION,
159 IMAGEX_STREAMS_INTERFACE_OPTION,
160 IMAGEX_STRICT_ACLS_OPTION,
161 IMAGEX_STRICT_WILDCARDS_OPTION,
162 IMAGEX_SYMLINK_OPTION,
163 IMAGEX_THREADS_OPTION,
164 IMAGEX_TO_STDOUT_OPTION,
165 IMAGEX_UNIX_DATA_OPTION,
166 IMAGEX_UPDATE_OF_OPTION,
167 IMAGEX_VERBOSE_OPTION,
171 static const struct option apply_options[] = {
172 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
173 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
174 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
175 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
176 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
177 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
178 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
179 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
180 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
181 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
182 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
183 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
185 /* --resume is undocumented for now as it needs improvement. */
186 {T("resume"), no_argument, NULL, IMAGEX_RESUME_OPTION},
190 static const struct option capture_or_append_options[] = {
191 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
192 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
193 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
194 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
195 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
196 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
197 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
198 {T("pack-streams"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
199 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
200 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
201 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
202 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
203 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
204 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
205 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
206 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
207 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
208 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
209 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
210 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
211 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
212 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
213 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
214 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
215 {T("update-of"), required_argument, NULL, IMAGEX_UPDATE_OF_OPTION},
216 {T("delta-from"), required_argument, NULL, IMAGEX_DELTA_FROM_OPTION},
220 static const struct option delete_options[] = {
221 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
222 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
226 static const struct option dir_options[] = {
227 {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
231 static const struct option export_options[] = {
232 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
233 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
234 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
235 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
236 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
237 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
238 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
239 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
240 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
241 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
242 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
243 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
244 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
248 static const struct option extract_options[] = {
249 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
250 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
251 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
252 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
253 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
254 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
255 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
256 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
257 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
258 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
259 {T("strict-wildcards"), no_argument, NULL, IMAGEX_STRICT_WILDCARDS_OPTION},
260 {T("no-wildcards"), no_argument, NULL, IMAGEX_NO_WILDCARDS_OPTION},
264 static const struct option info_options[] = {
265 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
266 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
267 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
268 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
269 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
270 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
271 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
272 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
273 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
277 static const struct option join_options[] = {
278 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
282 static const struct option mount_options[] = {
283 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
284 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
285 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
286 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
287 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
288 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
289 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
293 static const struct option optimize_options[] = {
294 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
295 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
296 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
297 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
298 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
299 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
300 {T("recompress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
301 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
302 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
303 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
304 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
305 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
306 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
310 static const struct option split_options[] = {
311 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
315 static const struct option unmount_options[] = {
316 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
317 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
318 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
319 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
323 static const struct option update_options[] = {
324 /* Careful: some of the options here set the defaults for update
325 * commands, but the flags given to an actual update command (and not to
326 * `imagex update' itself are also handled in
327 * update_command_add_option(). */
328 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
329 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
330 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
331 {T("command"), required_argument, NULL, IMAGEX_COMMAND_OPTION},
333 /* Default delete options */
334 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
335 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
337 /* Global add option */
338 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
340 /* Default add options */
341 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
342 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
343 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
344 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
345 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
346 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
352 # define _format_attribute(type, format_str, args_start) \
353 __attribute__((format(type, format_str, args_start)))
355 # define _format_attribute(type, format_str, args_start)
358 /* Print formatted error message to stderr. */
359 static void _format_attribute(printf, 1, 2)
360 imagex_error(const tchar *format, ...)
363 va_start(va, format);
364 tfputs(T("ERROR: "), stderr);
365 tvfprintf(stderr, format, va);
366 tputc(T('\n'), stderr);
370 /* Print formatted error message to stderr. */
371 static void _format_attribute(printf, 1, 2)
372 imagex_error_with_errno(const tchar *format, ...)
374 int errno_save = errno;
376 va_start(va, format);
377 tfputs(T("ERROR: "), stderr);
378 tvfprintf(stderr, format, va);
379 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
384 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
386 if (image == WIMLIB_NO_IMAGE) {
387 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
388 " Please specify a 1-based image index or "
389 "image name. To list the images\n"
390 " contained in the WIM archive, run\n"
392 " %"TS" \"%"TS"\"\n"),
393 image_name, wim_name,
394 get_cmd_string(CMD_INFO, false), wim_name);
395 return WIMLIB_ERR_INVALID_IMAGE;
401 verify_image_is_single(int image)
403 if (image == WIMLIB_ALL_IMAGES) {
404 imagex_error(T("Cannot specify all images for this action!"));
405 return WIMLIB_ERR_INVALID_IMAGE;
411 verify_image_exists_and_is_single(int image, const tchar *image_name,
412 const tchar *wim_name)
415 ret = verify_image_exists(image, image_name, wim_name);
417 ret = verify_image_is_single(image);
421 /* Parse the argument to --compress */
423 get_compression_type(const tchar *optarg)
425 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
426 return WIMLIB_COMPRESSION_TYPE_LZX;
427 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
428 return WIMLIB_COMPRESSION_TYPE_XPRESS;
429 else if (!tstrcasecmp(optarg, T("recovery")) || !tstrcasecmp(optarg, T("lzms")))
430 return WIMLIB_COMPRESSION_TYPE_LZMS;
431 else if (!tstrcasecmp(optarg, T("none")))
432 return WIMLIB_COMPRESSION_TYPE_NONE;
434 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
435 "\"maximum\", \"fast\", or \"none\"."), optarg);
436 return WIMLIB_COMPRESSION_TYPE_INVALID;
441 set_compress_slow(void)
443 static const struct wimlib_lzx_compressor_params lzx_slow_params = {
445 .size = sizeof(struct wimlib_lzx_compressor_params),
447 .algorithm = WIMLIB_LZX_ALGORITHM_SLOW,
450 .use_len2_matches = 1,
451 .nice_match_length = 96,
452 .num_optim_passes = 4,
453 .max_search_depth = 100,
454 .max_matches_per_pos = 10,
455 .main_nostat_cost = 15,
456 .len_nostat_cost = 15,
457 .aligned_nostat_cost = 7,
462 static const struct wimlib_lzms_compressor_params lzms_slow_params = {
464 .size = sizeof(struct wimlib_lzms_compressor_params),
466 .min_match_length = 2,
467 .max_match_length = UINT32_MAX,
468 .nice_match_length = 96,
469 .max_search_depth = 100,
470 .max_matches_per_pos = 10,
471 .optim_array_length = 1024,
474 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
475 &lzx_slow_params.hdr);
477 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZMS,
478 &lzms_slow_params.hdr);
482 const tchar **strings;
483 unsigned num_strings;
484 unsigned num_alloc_strings;
487 #define STRING_SET_INITIALIZER \
488 { .strings = NULL, .num_strings = 0, .num_alloc_strings = 0, }
490 #define STRING_SET(_strings) \
491 struct string_set _strings = STRING_SET_INITIALIZER
494 string_set_append(struct string_set *set, const tchar *glob)
496 unsigned num_alloc_strings = set->num_alloc_strings;
498 if (set->num_strings == num_alloc_strings) {
499 const tchar **new_strings;
501 num_alloc_strings += 4;
502 new_strings = realloc(set->strings,
503 sizeof(set->strings[0]) * num_alloc_strings);
505 imagex_error(T("Out of memory!"));
508 set->strings = new_strings;
509 set->num_alloc_strings = num_alloc_strings;
511 set->strings[set->num_strings++] = glob;
516 string_set_destroy(struct string_set *set)
522 wim_reference_globs(WIMStruct *wim, struct string_set *set, int open_flags)
524 return wimlib_reference_resource_files(wim, set->strings,
526 WIMLIB_REF_FLAG_GLOB_ENABLE,
528 imagex_progress_func);
532 do_resource_not_found_warning(const tchar *wimfile,
533 const struct wimlib_wim_info *info,
534 const struct string_set *refglobs)
536 if (info->total_parts > 1) {
537 if (refglobs->num_strings == 0) {
538 imagex_error(T("\"%"TS"\" is part of a split WIM. "
539 "Use --ref to specify the other parts."),
542 imagex_error(T("Perhaps the '--ref' argument did not "
543 "specify all other parts of the split "
547 imagex_error(T("If this is a delta WIM, use the --ref argument "
548 "to specify the WIM(s) on which it is based."));
552 /* Returns the size of a file given its name, or -1 if the file does not exist
553 * or its size cannot be determined. */
555 file_get_size(const tchar *filename)
558 if (tstat(filename, &st) == 0)
565 PARSE_STRING_SUCCESS = 0,
566 PARSE_STRING_FAILURE = 1,
567 PARSE_STRING_NONE = 2,
571 * Parses a string token from an array of characters.
573 * Tokens are either whitespace-delimited, or double or single-quoted.
575 * @line_p: Pointer to the pointer to the line of data. Will be updated
576 * to point past the string token iff the return value is
577 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
580 * @len_p: @len_p initially stores the length of the line of data, which may
581 * be 0, and it will be updated to the number of bytes remaining in
582 * the line iff the return value is PARSE_STRING_SUCCESS.
584 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
585 * parsed string token will be returned here.
587 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
588 * PARSE_STRING_FAILURE if the data was invalid due to a missing
589 * closing quote; or PARSE_STRING_NONE if the line ended before the
590 * beginning of a string token was found.
593 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
596 tchar *line = *line_p;
600 /* Skip leading whitespace */
603 return PARSE_STRING_NONE;
604 if (!istspace(*line) && *line != T('\0'))
610 if (quote_char == T('"') || quote_char == T('\'')) {
615 line = tmemchr(line, quote_char, len);
617 imagex_error(T("Missing closing quote: %"TS), fn - 1);
618 return PARSE_STRING_FAILURE;
621 /* Unquoted string. Go until whitespace. Line is terminated
622 * by '\0', so no need to check 'len'. */
626 } while (!istspace(*line) && *line != T('\0'));
633 return PARSE_STRING_SUCCESS;
636 /* Parses a line of data (not an empty line or comment) in the source list file
637 * format. (See the man page for 'wimlib-imagex capture' for details on this
638 * format and the meaning.)
640 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
641 * len == 0. The data in @line will be modified by this function call.
643 * @len: Length of the line of data.
645 * @source: On success, the capture source and target described by the line is
646 * written into this destination. Note that it will contain pointers
647 * to data in the @line array.
649 * Returns true if the line was valid; false otherwise. */
651 parse_source_list_line(tchar *line, size_t len,
652 struct wimlib_capture_source *source)
656 ret = parse_string(&line, &len, &source->fs_source_path);
657 if (ret != PARSE_STRING_SUCCESS)
659 ret = parse_string(&line, &len, &source->wim_target_path);
660 if (ret == PARSE_STRING_NONE)
661 source->wim_target_path = source->fs_source_path;
662 return ret != PARSE_STRING_FAILURE;
665 /* Returns %true if the given line of length @len > 0 is a comment or empty line
666 * in the source list file format. */
668 is_comment_line(const tchar *line, size_t len)
673 if (!istspace(*line) && *line != T('\0'))
683 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
686 tchar *contents = *contents_p;
687 size_t nchars = *nchars_p;
690 for (i = 0; i < nchars; i++)
691 if (contents[i] == T('\n'))
694 /* Handle last line not terminated by a newline */
695 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
696 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
698 imagex_error(T("Out of memory!"));
701 contents[nchars] = T('\n');
702 *contents_p = contents;
710 /* Parses a file in the source list format. (See the man page for
711 * 'wimlib-imagex capture' for details on this format and the meaning.)
713 * @source_list_contents: Contents of the source list file. Note that this
714 * buffer will be modified to save memory allocations,
715 * and cannot be freed until the returned array of
716 * wimlib_capture_source's has also been freed.
718 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
721 * @nsources_ret: On success, the length of the returned array is
724 * Returns: An array of `struct wimlib_capture_source's that can be passed to
725 * the wimlib_add_image_multisource() function to specify how a WIM image is to
727 static struct wimlib_capture_source *
728 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
729 size_t *nsources_ret)
733 struct wimlib_capture_source *sources;
736 nlines = text_file_count_lines(source_list_contents_p,
737 &source_list_nchars);
741 /* Always allocate at least 1 slot, just in case the implementation of
742 * calloc() returns NULL if 0 bytes are requested. */
743 sources = calloc(nlines ?: 1, sizeof(*sources));
745 imagex_error(T("out of memory"));
748 p = *source_list_contents_p;
750 for (i = 0; i < nlines; i++) {
751 /* XXX: Could use rawmemchr() here instead, but it may not be
752 * available on all platforms. */
753 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
754 size_t len = endp - p + 1;
756 if (!is_comment_line(p, len)) {
757 if (!parse_source_list_line(p, len, &sources[j++])) {
770 enum capture_config_section {
771 CAPTURE_CONFIG_NO_SECTION,
772 CAPTURE_CONFIG_EXCLUSION_SECTION,
773 CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
774 CAPTURE_CONFIG_IGNORE_SECTION,
778 CAPTURE_CONFIG_INVALID_SECTION,
779 CAPTURE_CONFIG_CHANGED_SECTION,
780 CAPTURE_CONFIG_SAME_SECTION,
784 check_config_section(tchar *line, size_t len,
785 enum capture_config_section *cur_section)
787 while (istspace(*line))
791 return CAPTURE_CONFIG_SAME_SECTION;
794 tchar *endbrace = tstrrchr(line, T(']'));
796 return CAPTURE_CONFIG_SAME_SECTION;
798 if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
799 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
800 } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
801 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
802 } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
803 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
804 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
805 "of capture config file\n"),
807 } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
808 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
809 tfputs(T("WARNING: Ignoring [AlignmentList] section "
810 "of capture config file\n"),
813 imagex_error(T("Invalid capture config file section \"%"TS"\""),
815 return CAPTURE_CONFIG_INVALID_SECTION;
817 return CAPTURE_CONFIG_CHANGED_SECTION;
822 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
825 if (pat_list->num_pats == pat_list->num_allocated_pats) {
827 size_t num_allocated_pats = pat_list->num_pats + 8;
829 pats = realloc(pat_list->pats,
830 num_allocated_pats * sizeof(pat_list->pats[0]));
832 imagex_error(T("Out of memory!"));
835 pat_list->pats = pats;
836 pat_list->num_allocated_pats = num_allocated_pats;
838 pat_list->pats[pat_list->num_pats++] = pat;
843 parse_capture_config_line(tchar *line, size_t len,
844 enum capture_config_section *cur_section,
845 struct wimlib_capture_config *config)
850 ret = check_config_section(line, len, cur_section);
851 if (ret == CAPTURE_CONFIG_INVALID_SECTION)
853 if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
856 switch (*cur_section) {
857 case CAPTURE_CONFIG_NO_SECTION:
858 imagex_error(T("Line \"%"TS"\" is not in a section "
859 "(such as [ExclusionList]"), line);
861 case CAPTURE_CONFIG_EXCLUSION_SECTION:
862 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
864 return pattern_list_add_pattern(&config->exclusion_pats,
866 case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
867 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
869 return pattern_list_add_pattern(&config->exclusion_exception_pats,
871 case CAPTURE_CONFIG_IGNORE_SECTION:
878 parse_capture_config(tchar **contents_p, size_t nchars,
879 struct wimlib_capture_config *config)
884 enum capture_config_section cur_section;
886 memset(config, 0, sizeof(*config));
888 nlines = text_file_count_lines(contents_p, &nchars);
892 cur_section = CAPTURE_CONFIG_NO_SECTION;
894 for (i = 0; i < nlines; i++) {
895 tchar *endp = tmemchr(p, T('\n'), nchars);
896 size_t len = endp - p + 1;
898 if (!is_comment_line(p, len))
899 if (!parse_capture_config_line(p, len, &cur_section, config))
907 /* Reads the contents of a file into memory. */
909 file_get_contents(const tchar *filename, size_t *len_ret)
916 if (tstat(filename, &stbuf) != 0) {
917 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
922 fp = tfopen(filename, T("rb"));
924 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
928 buf = malloc(len ? len : 1);
930 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
931 "contents of file \"%"TS"\""), len, filename);
934 if (fread(buf, 1, len, fp) != len) {
935 imagex_error_with_errno(T("Failed to read %zu bytes from the "
936 "file \"%"TS"\""), len, filename);
950 /* Read standard input until EOF and return the full contents in a malloc()ed
951 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
954 stdin_get_contents(size_t *len_ret)
956 /* stdin can, of course, be a pipe or other non-seekable file, so the
957 * total length of the data cannot be pre-determined */
959 size_t newlen = 1024;
963 char *p = realloc(buf, newlen);
964 size_t bytes_read, bytes_to_read;
966 imagex_error(T("out of memory while reading stdin"));
970 bytes_to_read = newlen - pos;
971 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
973 if (bytes_read != bytes_to_read) {
978 imagex_error_with_errno(T("error reading stdin"));
992 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
995 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
997 *num_tchars_ret = num_bytes;
999 #else /* !__WIN32__ */
1000 /* On Windows, translate the text to UTF-16LE */
1004 if (num_bytes >= 2 &&
1005 (((unsigned char)text[0] == 0xff && (unsigned char)text[1] == 0xfe) ||
1006 ((unsigned char)text[0] <= 0x7f && (unsigned char)text[1] == 0x00)))
1008 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
1009 * with something that looks like an ASCII character encoded as
1010 * a UTF-16LE code unit. Assume the file is encoded as
1011 * UTF-16LE. This is not a 100% reliable check. */
1012 num_wchars = num_bytes / 2;
1013 text_wstr = (wchar_t*)text;
1015 /* File does not look like UTF-16LE. Assume it is encoded in
1016 * the current Windows code page. I think these are always
1017 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
1018 * should work as expected. */
1019 text_wstr = win32_mbs_to_wcs(text,
1024 *num_tchars_ret = num_wchars;
1026 #endif /* __WIN32__ */
1030 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
1035 contents = file_get_contents(filename, &num_bytes);
1038 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
1042 stdin_get_text_contents(size_t *num_tchars_ret)
1047 contents = stdin_get_contents(&num_bytes);
1050 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
1053 #define TO_PERCENT(numerator, denominator) \
1054 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
1056 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
1057 #define MEBIBYTE_MIN_NBYTES 10000000ULL
1058 #define KIBIBYTE_MIN_NBYTES 10000ULL
1061 get_unit(uint64_t total_bytes, const tchar **name_ret)
1063 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
1064 *name_ret = T("GiB");
1066 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
1067 *name_ret = T("MiB");
1069 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
1070 *name_ret = T("KiB");
1073 *name_ret = T("bytes");
1078 static struct wimlib_progress_info_scan last_scan_progress;
1081 report_scan_progress(const struct wimlib_progress_info_scan *scan, bool done)
1083 uint64_t prev_count, cur_count;
1085 prev_count = last_scan_progress.num_nondirs_scanned +
1086 last_scan_progress.num_dirs_scanned;
1087 cur_count = scan->num_nondirs_scanned + scan->num_dirs_scanned;
1089 if (done || prev_count == 0 || cur_count >= prev_count + 100 ||
1090 cur_count % 128 == 0)
1092 unsigned unit_shift;
1093 const tchar *unit_name;
1095 unit_shift = get_unit(scan->num_bytes_scanned, &unit_name);
1096 imagex_printf(T("\r%"PRIu64" %"TS" scanned (%"PRIu64" files, "
1097 "%"PRIu64" directories) "),
1098 scan->num_bytes_scanned >> unit_shift,
1100 scan->num_nondirs_scanned,
1101 scan->num_dirs_scanned);
1102 last_scan_progress = *scan;
1106 /* Progress callback function passed to various wimlib functions. */
1108 imagex_progress_func(enum wimlib_progress_msg msg,
1109 const union wimlib_progress_info *info)
1111 unsigned percent_done;
1112 unsigned unit_shift;
1113 const tchar *unit_name;
1115 if (imagex_be_quiet)
1118 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1120 static bool first = true;
1122 imagex_printf(T("Writing %"TS"-compressed data "
1123 "using %u thread%"TS"\n"),
1124 wimlib_get_compression_type_string(
1125 info->write_streams.compression_type),
1126 info->write_streams.num_threads,
1127 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1131 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1132 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1133 info->write_streams.total_bytes);
1135 if (info->write_streams.total_parts <= 1) {
1136 imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1137 "written (%u%% done)"),
1138 info->write_streams.completed_bytes >> unit_shift,
1140 info->write_streams.total_bytes >> unit_shift,
1144 imagex_printf(T("\rWriting resources from part %u of %u: "
1145 "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written"),
1146 (info->write_streams.completed_parts ==
1147 info->write_streams.total_parts) ?
1148 info->write_streams.completed_parts :
1149 info->write_streams.completed_parts + 1,
1150 info->write_streams.total_parts,
1151 info->write_streams.completed_bytes >> unit_shift,
1153 info->write_streams.total_bytes >> unit_shift,
1157 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1158 imagex_printf(T("\n"));
1160 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1161 imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
1162 if (*info->scan.wim_target_path) {
1163 imagex_printf(T(" (loading as WIM path: "
1164 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
1165 info->scan.wim_target_path);
1167 imagex_printf(T("\n"));
1169 memset(&last_scan_progress, 0, sizeof(last_scan_progress));
1171 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1172 switch (info->scan.status) {
1173 case WIMLIB_SCAN_DENTRY_OK:
1174 report_scan_progress(&info->scan, false);
1176 case WIMLIB_SCAN_DENTRY_EXCLUDED:
1177 imagex_printf(T("\nExcluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1179 case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
1180 imagex_printf(T("\nWARNING: Excluding unsupported file or directory\n"
1181 " \"%"TS"\" from capture\n"), info->scan.cur_path);
1183 case WIMLIB_SCAN_DENTRY_EXCLUDED_SYMLINK:
1184 imagex_printf(T("\nWARNING: Ignoring absolute symbolic link "
1185 "with out-of-tree target:\n"
1186 " \"%"TS"\" => \"%"TS"\"\n"
1187 " (Use --norpfix to capture "
1188 "absolute symbolic links as-is)\n"),
1189 info->scan.cur_path, info->scan.symlink_target);
1193 case WIMLIB_PROGRESS_MSG_SCAN_END:
1194 report_scan_progress(&info->scan, true);
1195 imagex_printf(T("\n"));
1197 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1198 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1199 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1200 info->integrity.total_bytes);
1201 imagex_printf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1202 "of %"PRIu64" %"TS" (%u%%) done"),
1203 info->integrity.filename,
1204 info->integrity.completed_bytes >> unit_shift,
1206 info->integrity.total_bytes >> unit_shift,
1209 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1210 imagex_printf(T("\n"));
1212 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1213 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1214 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1215 info->integrity.total_bytes);
1216 imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1217 "of %"PRIu64" %"TS" (%u%%) done"),
1218 info->integrity.completed_bytes >> unit_shift,
1220 info->integrity.total_bytes >> unit_shift,
1223 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1224 imagex_printf(T("\n"));
1226 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1227 imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1228 "to %"TS" \"%"TS"\"\n"),
1229 info->extract.image,
1230 info->extract.image_name,
1231 info->extract.wimfile_name,
1232 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1233 T("NTFS volume") : T("directory")),
1234 info->extract.target);
1236 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1237 if (info->extract.extract_root_wim_source_path[0] != T('\0')) {
1238 imagex_printf(T("Extracting "
1239 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\" "
1240 "from image %d (\"%"TS"\") "
1241 "in \"%"TS"\" to \"%"TS"\"\n"),
1242 info->extract.extract_root_wim_source_path,
1243 info->extract.image,
1244 info->extract.image_name,
1245 info->extract.wimfile_name,
1246 info->extract.target);
1249 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1250 percent_done = TO_PERCENT(info->extract.completed_bytes,
1251 info->extract.total_bytes);
1252 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1253 imagex_printf(T("\rExtracting files: "
1254 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1255 info->extract.completed_bytes >> unit_shift,
1257 info->extract.total_bytes >> unit_shift,
1260 if (info->extract.completed_bytes >= info->extract.total_bytes)
1261 imagex_printf(T("\n"));
1263 case WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN:
1264 if (info->extract.total_parts != 1) {
1265 imagex_printf(T("\nReading split pipable WIM part %u of %u\n"),
1266 info->extract.part_number,
1267 info->extract.total_parts);
1270 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1271 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1272 imagex_printf(T("Setting timestamps on all extracted files...\n"));
1274 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1275 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1276 imagex_printf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1277 info->extract.target);
1280 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1281 percent_done = TO_PERCENT(info->split.completed_bytes,
1282 info->split.total_bytes);
1283 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1284 imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
1285 "%"PRIu64" %"TS" (%u%%) written\n"),
1286 info->split.part_name,
1287 info->split.cur_part_number,
1288 info->split.total_parts,
1289 info->split.completed_bytes >> unit_shift,
1291 info->split.total_bytes >> unit_shift,
1295 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1296 if (info->split.completed_bytes == info->split.total_bytes) {
1297 imagex_printf(T("Finished writing split WIM part %u of %u\n"),
1298 info->split.cur_part_number,
1299 info->split.total_parts);
1302 case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1303 switch (info->update.command->op) {
1304 case WIMLIB_UPDATE_OP_DELETE:
1305 imagex_printf(T("Deleted WIM path "
1306 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1307 info->update.command->delete_.wim_path);
1309 case WIMLIB_UPDATE_OP_RENAME:
1310 imagex_printf(T("Renamed WIM path "
1311 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
1312 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1313 info->update.command->rename.wim_source_path,
1314 info->update.command->rename.wim_target_path);
1316 case WIMLIB_UPDATE_OP_ADD:
1324 fflush(imagex_info_file);
1329 parse_num_threads(const tchar *optarg)
1332 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1333 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1334 imagex_error(T("Number of threads must be a non-negative integer!"));
1341 static uint32_t parse_chunk_size(const tchar *optarg)
1344 unsigned long chunk_size = tstrtoul(optarg, &tmp, 10);
1345 if (chunk_size >= UINT32_MAX || *tmp || tmp == optarg) {
1346 imagex_error(T("Chunk size must be a non-negative integer!"));
1355 * Parse an option passed to an update command.
1357 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1360 * @option: Text string for the option (beginning with --)
1362 * @cmd: `struct wimlib_update_command' that is being constructed for
1365 * Returns true if the option was recognized; false if not.
1368 update_command_add_option(int op, const tchar *option,
1369 struct wimlib_update_command *cmd)
1371 bool recognized = true;
1373 case WIMLIB_UPDATE_OP_ADD:
1374 if (!tstrcmp(option, T("--verbose")))
1375 cmd->add.add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
1376 else if (!tstrcmp(option, T("--unix-data")))
1377 cmd->add.add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
1378 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1379 cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
1380 else if (!tstrcmp(option, T("--strict-acls")))
1381 cmd->add.add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
1382 else if (!tstrcmp(option, T("--dereference")))
1383 cmd->add.add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
1387 case WIMLIB_UPDATE_OP_DELETE:
1388 if (!tstrcmp(option, T("--force")))
1389 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1390 else if (!tstrcmp(option, T("--recursive")))
1391 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1402 /* How many nonoption arguments each `imagex update' command expects */
1403 static const unsigned update_command_num_nonoptions[] = {
1404 [WIMLIB_UPDATE_OP_ADD] = 2,
1405 [WIMLIB_UPDATE_OP_DELETE] = 1,
1406 [WIMLIB_UPDATE_OP_RENAME] = 2,
1410 update_command_add_nonoption(int op, const tchar *nonoption,
1411 struct wimlib_update_command *cmd,
1412 unsigned num_nonoptions)
1415 case WIMLIB_UPDATE_OP_ADD:
1416 if (num_nonoptions == 0)
1417 cmd->add.fs_source_path = (tchar*)nonoption;
1419 cmd->add.wim_target_path = (tchar*)nonoption;
1421 case WIMLIB_UPDATE_OP_DELETE:
1422 cmd->delete_.wim_path = (tchar*)nonoption;
1424 case WIMLIB_UPDATE_OP_RENAME:
1425 if (num_nonoptions == 0)
1426 cmd->rename.wim_source_path = (tchar*)nonoption;
1428 cmd->rename.wim_target_path = (tchar*)nonoption;
1434 * Parse a command passed on stdin to `imagex update'.
1436 * @line: Text of the command.
1437 * @len: Length of the line, including a null terminator
1440 * @command: A `struct wimlib_update_command' to fill in from the parsed
1443 * @line_number: Line number of the command, for diagnostics.
1445 * Returns true on success; returns false on parse error.
1448 parse_update_command(tchar *line, size_t len,
1449 struct wimlib_update_command *command,
1453 tchar *command_name;
1455 size_t num_nonoptions;
1457 /* Get the command name ("add", "delete", "rename") */
1458 ret = parse_string(&line, &len, &command_name);
1459 if (ret != PARSE_STRING_SUCCESS)
1462 if (!tstrcasecmp(command_name, T("add"))) {
1463 op = WIMLIB_UPDATE_OP_ADD;
1464 } else if (!tstrcasecmp(command_name, T("delete"))) {
1465 op = WIMLIB_UPDATE_OP_DELETE;
1466 } else if (!tstrcasecmp(command_name, T("rename"))) {
1467 op = WIMLIB_UPDATE_OP_RENAME;
1469 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1470 command_name, line_number);
1475 /* Parse additional options and non-options as needed */
1480 ret = parse_string(&line, &len, &next_string);
1481 if (ret == PARSE_STRING_NONE) /* End of line */
1483 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1485 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1487 if (!update_command_add_option(op, next_string, command))
1489 imagex_error(T("Unrecognized option \"%"TS"\" to "
1490 "update command \"%"TS"\" on line %zu"),
1491 next_string, command_name, line_number);
1497 if (num_nonoptions == update_command_num_nonoptions[op])
1499 imagex_error(T("Unexpected argument \"%"TS"\" in "
1500 "update command on line %zu\n"
1501 " (The \"%"TS"\" command only "
1502 "takes %zu nonoption arguments!)\n"),
1503 next_string, line_number,
1504 command_name, num_nonoptions);
1507 update_command_add_nonoption(op, next_string,
1508 command, num_nonoptions);
1513 if (num_nonoptions != update_command_num_nonoptions[op]) {
1514 imagex_error(T("Not enough arguments to update command "
1515 "\"%"TS"\" on line %zu"), command_name, line_number);
1521 static struct wimlib_update_command *
1522 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1523 size_t *num_cmds_ret)
1527 struct wimlib_update_command *cmds;
1530 nlines = text_file_count_lines(cmd_file_contents_p,
1535 /* Always allocate at least 1 slot, just in case the implementation of
1536 * calloc() returns NULL if 0 bytes are requested. */
1537 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1539 imagex_error(T("out of memory"));
1542 p = *cmd_file_contents_p;
1544 for (i = 0; i < nlines; i++) {
1545 /* XXX: Could use rawmemchr() here instead, but it may not be
1546 * available on all platforms. */
1547 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1548 size_t len = endp - p + 1;
1550 if (!is_comment_line(p, len)) {
1551 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1562 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1563 * one image from a WIM file to a NTFS volume. */
1565 imagex_apply(int argc, tchar **argv, int cmd)
1569 int image = WIMLIB_NO_IMAGE;
1571 struct wimlib_wim_info info;
1573 const tchar *wimfile;
1574 const tchar *target;
1575 const tchar *image_num_or_name = NULL;
1576 int extract_flags = 0;
1578 STRING_SET(refglobs);
1580 for_opt(c, apply_options) {
1582 case IMAGEX_CHECK_OPTION:
1583 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1585 case IMAGEX_HARDLINK_OPTION:
1586 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1588 case IMAGEX_SYMLINK_OPTION:
1589 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1591 case IMAGEX_VERBOSE_OPTION:
1592 /* No longer does anything. */
1594 case IMAGEX_REF_OPTION:
1595 ret = string_set_append(&refglobs, optarg);
1597 goto out_free_refglobs;
1599 case IMAGEX_UNIX_DATA_OPTION:
1600 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1602 case IMAGEX_NO_ACLS_OPTION:
1603 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1605 case IMAGEX_STRICT_ACLS_OPTION:
1606 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1608 case IMAGEX_NORPFIX_OPTION:
1609 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1611 case IMAGEX_RPFIX_OPTION:
1612 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1614 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1615 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1616 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1618 case IMAGEX_RESUME_OPTION:
1619 extract_flags |= WIMLIB_EXTRACT_FLAG_RESUME;
1627 if (argc != 2 && argc != 3)
1632 if (!tstrcmp(wimfile, T("-"))) {
1633 /* Attempt to apply pipable WIM from standard input. */
1635 image_num_or_name = NULL;
1638 image_num_or_name = argv[1];
1643 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1644 imagex_progress_func);
1646 goto out_free_refglobs;
1648 wimlib_get_wim_info(wim, &info);
1651 /* Image explicitly specified. */
1652 image_num_or_name = argv[1];
1653 image = wimlib_resolve_image(wim, image_num_or_name);
1654 ret = verify_image_exists(image, image_num_or_name, wimfile);
1656 goto out_wimlib_free;
1659 /* No image specified; default to image 1, but only if the WIM
1660 * contains exactly one image. */
1662 if (info.image_count != 1) {
1663 imagex_error(T("\"%"TS"\" contains %d images; "
1664 "Please select one (or all)."),
1665 wimfile, info.image_count);
1674 if (refglobs.num_strings) {
1676 imagex_error(T("Can't specify --ref when applying from stdin!"));
1678 goto out_wimlib_free;
1680 ret = wim_reference_globs(wim, &refglobs, open_flags);
1682 goto out_wimlib_free;
1687 /* Interpret a regular file or block device target as a NTFS
1691 if (tstat(target, &stbuf)) {
1692 if (errno != ENOENT) {
1693 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1696 goto out_wimlib_free;
1699 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1700 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1706 ret = wimlib_extract_image(wim, image, target, extract_flags,
1707 imagex_progress_func);
1709 set_fd_to_binary_mode(STDIN_FILENO);
1710 ret = wimlib_extract_image_from_pipe(STDIN_FILENO,
1712 target, extract_flags,
1713 imagex_progress_func);
1716 imagex_printf(T("Done applying WIM image.\n"));
1717 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
1719 do_resource_not_found_warning(wimfile, &info, &refglobs);
1721 imagex_error(T( "If you are applying an image "
1722 "from a split pipable WIM,\n"
1723 " make sure you have "
1724 "concatenated together all parts."));
1730 string_set_destroy(&refglobs);
1734 usage(CMD_APPLY, stderr);
1736 goto out_free_refglobs;
1739 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1740 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1741 * the desired image. 'wimlib-imagex append': add a new image to an existing
1744 imagex_capture_or_append(int argc, tchar **argv, int cmd)
1747 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1748 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE |
1749 WIMLIB_ADD_IMAGE_FLAG_WINCONFIG |
1750 WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1751 int write_flags = 0;
1752 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
1753 uint32_t chunk_size = UINT32_MAX;
1754 const tchar *wimfile;
1758 const tchar *flags_element = NULL;
1761 STRING_SET(base_wimfiles);
1762 WIMStruct **base_wims;
1764 WIMStruct *template_wim;
1765 const tchar *template_wimfile = NULL;
1766 const tchar *template_image_name_or_num = NULL;
1767 int template_image = WIMLIB_NO_IMAGE;
1770 unsigned num_threads = 0;
1775 const tchar *config_file = NULL;
1777 struct wimlib_capture_config *config;
1779 bool source_list = false;
1780 size_t source_list_nchars = 0;
1781 tchar *source_list_contents;
1782 bool capture_sources_malloced;
1783 struct wimlib_capture_source *capture_sources;
1785 bool name_defaulted;
1786 bool compress_slow = false;
1788 for_opt(c, capture_or_append_options) {
1790 case IMAGEX_BOOT_OPTION:
1791 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1793 case IMAGEX_CHECK_OPTION:
1794 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1795 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1797 case IMAGEX_NOCHECK_OPTION:
1798 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
1800 case IMAGEX_CONFIG_OPTION:
1801 config_file = optarg;
1802 add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1804 case IMAGEX_COMPRESS_OPTION:
1805 compression_type = get_compression_type(optarg);
1806 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1809 case IMAGEX_COMPRESS_SLOW_OPTION:
1810 compress_slow = true;
1812 case IMAGEX_CHUNK_SIZE_OPTION:
1813 chunk_size = parse_chunk_size(optarg);
1814 if (chunk_size == UINT32_MAX)
1817 case IMAGEX_PACK_STREAMS_OPTION:
1818 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
1820 case IMAGEX_FLAGS_OPTION:
1821 flags_element = optarg;
1823 case IMAGEX_DEREFERENCE_OPTION:
1824 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1826 case IMAGEX_VERBOSE_OPTION:
1827 /* No longer does anything. */
1829 case IMAGEX_THREADS_OPTION:
1830 num_threads = parse_num_threads(optarg);
1831 if (num_threads == UINT_MAX)
1834 case IMAGEX_REBUILD_OPTION:
1835 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1837 case IMAGEX_UNIX_DATA_OPTION:
1838 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1840 case IMAGEX_SOURCE_LIST_OPTION:
1843 case IMAGEX_NO_ACLS_OPTION:
1844 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1846 case IMAGEX_STRICT_ACLS_OPTION:
1847 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1849 case IMAGEX_RPFIX_OPTION:
1850 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1852 case IMAGEX_NORPFIX_OPTION:
1853 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1855 case IMAGEX_PIPABLE_OPTION:
1856 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1858 case IMAGEX_NOT_PIPABLE_OPTION:
1859 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
1861 case IMAGEX_UPDATE_OF_OPTION:
1862 if (template_image_name_or_num) {
1863 imagex_error(T("'--update-of' can only be "
1864 "specified one time!"));
1868 colon = tstrrchr(optarg, T(':'));
1871 template_wimfile = optarg;
1873 template_image_name_or_num = colon + 1;
1875 template_wimfile = NULL;
1876 template_image_name_or_num = optarg;
1880 case IMAGEX_DELTA_FROM_OPTION:
1881 if (cmd != CMD_CAPTURE) {
1882 imagex_error(T("'--delta-from' is only "
1883 "valid for capture!"));
1886 ret = string_set_append(&base_wimfiles, optarg);
1888 goto out_free_base_wimfiles;
1889 write_flags |= WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS;
1898 if (argc < 2 || argc > 4)
1904 /* Set default compression type and parameters. */
1907 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
1908 compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1910 if (!compress_slow) {
1911 struct wimlib_lzx_compressor_params params = {
1912 .hdr.size = sizeof(params),
1913 .algorithm = WIMLIB_LZX_ALGORITHM_FAST,
1916 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
1922 set_compress_slow();
1924 if (!tstrcmp(wimfile, T("-"))) {
1925 /* Writing captured WIM to standard output. */
1927 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
1928 imagex_error("Can't write a non-pipable WIM to "
1929 "standard output! Specify --pipable\n"
1930 " if you want to create a pipable WIM "
1931 "(but read the docs first).");
1935 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1937 if (cmd == CMD_APPEND) {
1938 imagex_error(T("Using standard output for append does "
1939 "not make sense."));
1942 wim_fd = STDOUT_FILENO;
1944 imagex_info_file = stderr;
1945 set_fd_to_binary_mode(wim_fd);
1948 /* If template image was specified using --update-of=IMAGE rather
1949 * than --update-of=WIMFILE:IMAGE, set the default WIMFILE. */
1950 if (template_image_name_or_num && !template_wimfile) {
1951 if (base_wimfiles.num_strings == 1) {
1952 /* Capturing delta WIM based on single WIM: default to
1954 template_wimfile = base_wimfiles.strings[0];
1955 } else if (cmd == CMD_APPEND) {
1956 /* Appending to WIM: default to WIM being appended to.
1958 template_wimfile = wimfile;
1960 /* Capturing a normal (non-delta) WIM, so the WIM file
1961 * *must* be explicitly specified. */
1962 if (base_wimfiles.num_strings > 1) {
1963 imagex_error(T("For capture of delta WIM "
1964 "based on multiple existing "
1966 " '--update-of' must "
1967 "specify WIMFILE:IMAGE!"));
1969 imagex_error(T("For capture of non-delta WIM, "
1970 "'--update-of' must specify "
1979 name_defaulted = false;
1981 /* Set default name to SOURCE argument, omitting any directory
1982 * prefixes and trailing slashes. This requires making a copy
1983 * of @source. Leave some free characters at the end in case we
1984 * append a number to keep the name unique. */
1985 size_t source_name_len;
1987 source_name_len = tstrlen(source);
1988 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1989 name = tbasename(tstrcpy(source_copy, source));
1990 name_defaulted = true;
1992 /* Image description defaults to NULL if not given. */
1999 /* Set up capture sources in source list mode */
2000 if (source[0] == T('-') && source[1] == T('\0')) {
2001 source_list_contents = stdin_get_text_contents(&source_list_nchars);
2003 source_list_contents = file_get_text_contents(source,
2004 &source_list_nchars);
2006 if (!source_list_contents)
2009 capture_sources = parse_source_list(&source_list_contents,
2012 if (!capture_sources) {
2014 goto out_free_source_list_contents;
2016 capture_sources_malloced = true;
2018 /* Set up capture source in non-source-list mode. */
2019 capture_sources = alloca(sizeof(struct wimlib_capture_source));
2020 capture_sources[0].fs_source_path = source;
2021 capture_sources[0].wim_target_path = NULL;
2022 capture_sources[0].reserved = 0;
2024 capture_sources_malloced = false;
2025 source_list_contents = NULL;
2029 /* Read and parse capture configuration file. */
2032 config_str = file_get_text_contents(config_file, &config_len);
2035 goto out_free_capture_sources;
2038 config = alloca(sizeof(*config));
2039 ret = parse_capture_config(&config_str, config_len, config);
2041 goto out_free_config;
2043 /* No capture configuration file specified; use default
2044 * configuration for capturing Windows operating systems. */
2046 add_image_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
2049 /* Open the existing WIM, or create a new one. */
2050 if (cmd == CMD_APPEND)
2051 ret = wimlib_open_wim(wimfile, open_flags, &wim,
2052 imagex_progress_func);
2054 ret = wimlib_create_new_wim(compression_type, &wim);
2056 goto out_free_config;
2058 /* Set chunk size if non-default. */
2059 if (chunk_size != UINT32_MAX) {
2060 ret = wimlib_set_output_chunk_size(wim, chunk_size);
2066 /* Detect if source is regular file or block device and set NTFS volume
2071 if (tstat(source, &stbuf) == 0) {
2072 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
2073 imagex_printf(T("Capturing WIM image from NTFS "
2074 "filesystem on \"%"TS"\"\n"), source);
2075 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
2078 if (errno != ENOENT) {
2079 imagex_error_with_errno(T("Failed to stat "
2080 "\"%"TS"\""), source);
2088 /* If the user did not specify an image name, and the basename of the
2089 * source already exists as an image name in the WIM file, append a
2090 * suffix to make it unique. */
2091 if (cmd == CMD_APPEND && name_defaulted) {
2092 unsigned long conflict_idx;
2093 tchar *name_end = tstrchr(name, T('\0'));
2094 for (conflict_idx = 1;
2095 wimlib_image_name_in_use(wim, name);
2098 tsprintf(name_end, T(" (%lu)"), conflict_idx);
2102 /* If capturing a delta WIM, reference resources from the base WIMs
2103 * before adding the new image. */
2104 if (base_wimfiles.num_strings) {
2105 base_wims = calloc(base_wimfiles.num_strings,
2106 sizeof(base_wims[0]));
2107 if (base_wims == NULL) {
2108 imagex_error(T("Out of memory!"));
2113 for (size_t i = 0; i < base_wimfiles.num_strings; i++) {
2114 ret = wimlib_open_wim(base_wimfiles.strings[i],
2115 open_flags, &base_wims[i],
2116 imagex_progress_func);
2118 goto out_free_base_wims;
2122 ret = wimlib_reference_resources(wim, base_wims,
2123 base_wimfiles.num_strings, 0);
2125 goto out_free_base_wims;
2127 if (base_wimfiles.num_strings == 1) {
2128 imagex_printf(T("Capturing delta WIM based on \"%"TS"\"\n"),
2129 base_wimfiles.strings[0]);
2131 imagex_printf(T("Capturing delta WIM based on %u WIMs\n"),
2132 base_wimfiles.num_strings);
2139 /* If capturing or appending as an update of an existing (template) image,
2140 * open the WIM if needed and parse the image index. */
2141 if (template_image_name_or_num) {
2144 if (base_wimfiles.num_strings == 1 &&
2145 template_wimfile == base_wimfiles.strings[0]) {
2146 template_wim = base_wims[0];
2147 } else if (template_wimfile == wimfile) {
2150 ret = wimlib_open_wim(template_wimfile, open_flags,
2151 &template_wim, imagex_progress_func);
2153 goto out_free_base_wims;
2156 template_image = wimlib_resolve_image(template_wim,
2157 template_image_name_or_num);
2159 if (template_image_name_or_num[0] == T('-')) {
2162 struct wimlib_wim_info info;
2164 wimlib_get_wim_info(template_wim, &info);
2165 n = tstrtoul(template_image_name_or_num + 1, &tmp, 10);
2166 if (n >= 1 && n <= info.image_count &&
2168 tmp != template_image_name_or_num + 1)
2170 template_image = info.image_count - (n - 1);
2173 ret = verify_image_exists_and_is_single(template_image,
2174 template_image_name_or_num,
2177 goto out_free_template_wim;
2179 template_wim = NULL;
2182 ret = wimlib_add_image_multisource(wim,
2188 imagex_progress_func);
2190 goto out_free_template_wim;
2192 if (desc || flags_element || template_image_name_or_num) {
2193 /* User provided <DESCRIPTION> or <FLAGS> element, or an image
2194 * on which the added one is to be based has been specified with
2195 * --update-of. Get the index of the image we just
2196 * added, then use it to call the appropriate functions. */
2197 struct wimlib_wim_info info;
2199 wimlib_get_wim_info(wim, &info);
2202 ret = wimlib_set_image_descripton(wim,
2206 goto out_free_template_wim;
2209 if (flags_element) {
2210 ret = wimlib_set_image_flags(wim, info.image_count,
2213 goto out_free_template_wim;
2216 /* Reference template image if the user provided one. */
2217 if (template_image_name_or_num) {
2218 imagex_printf(T("Using image %d "
2219 "from \"%"TS"\" as template\n"),
2220 template_image, template_wimfile);
2221 ret = wimlib_reference_template_image(wim,
2227 goto out_free_template_wim;
2231 /* Write the new WIM or overwrite the existing WIM with the new image
2233 if (cmd == CMD_APPEND) {
2234 ret = wimlib_overwrite(wim, write_flags, num_threads,
2235 imagex_progress_func);
2236 } else if (wimfile) {
2237 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
2238 write_flags, num_threads,
2239 imagex_progress_func);
2241 ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
2242 write_flags, num_threads,
2243 imagex_progress_func);
2245 out_free_template_wim:
2246 /* template_wim may alias base_wims[0] or wim. */
2247 if ((base_wimfiles.num_strings != 1 || template_wim != base_wims[0]) &&
2248 template_wim != wim)
2249 wimlib_free(template_wim);
2251 for (size_t i = 0; i < base_wimfiles.num_strings; i++)
2252 wimlib_free(base_wims[i]);
2258 free(config->exclusion_pats.pats);
2259 free(config->exclusion_exception_pats.pats);
2262 out_free_capture_sources:
2263 if (capture_sources_malloced)
2264 free(capture_sources);
2265 out_free_source_list_contents:
2266 free(source_list_contents);
2267 out_free_base_wimfiles:
2268 string_set_destroy(&base_wimfiles);
2275 goto out_free_base_wimfiles;
2278 /* Remove image(s) from a WIM. */
2280 imagex_delete(int argc, tchar **argv, int cmd)
2283 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2284 int write_flags = 0;
2285 const tchar *wimfile;
2286 const tchar *image_num_or_name;
2291 for_opt(c, delete_options) {
2293 case IMAGEX_CHECK_OPTION:
2294 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2295 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2297 case IMAGEX_SOFT_OPTION:
2298 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
2309 imagex_error(T("Must specify a WIM file"));
2311 imagex_error(T("Must specify an image"));
2315 image_num_or_name = argv[1];
2317 ret = wimlib_open_wim(wimfile, open_flags, &wim,
2318 imagex_progress_func);
2322 image = wimlib_resolve_image(wim, image_num_or_name);
2324 ret = verify_image_exists(image, image_num_or_name, wimfile);
2326 goto out_wimlib_free;
2328 ret = wimlib_delete_image(wim, image);
2330 imagex_error(T("Failed to delete image from \"%"TS"\""),
2332 goto out_wimlib_free;
2335 ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
2337 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2338 "deleted"), wimfile);
2346 usage(CMD_DELETE, stderr);
2352 print_full_path(const struct wimlib_dir_entry *wdentry, void *_ignore)
2354 int ret = tprintf(T("%"TS"\n"), wdentry->full_path);
2355 return (ret >= 0) ? 0 : -1;
2358 /* Print the files contained in an image(s) in a WIM file. */
2360 imagex_dir(int argc, tchar **argv, int cmd)
2362 const tchar *wimfile;
2363 WIMStruct *wim = NULL;
2366 const tchar *path = T("");
2369 for_opt(c, dir_options) {
2371 case IMAGEX_PATH_OPTION:
2382 imagex_error(T("Must specify a WIM file"));
2386 imagex_error(T("Too many arguments"));
2391 ret = wimlib_open_wim(wimfile, 0, &wim, imagex_progress_func);
2396 image = wimlib_resolve_image(wim, argv[1]);
2397 ret = verify_image_exists(image, argv[1], wimfile);
2399 goto out_wimlib_free;
2401 /* No image specified; default to image 1, but only if the WIM
2402 * contains exactly one image. */
2404 struct wimlib_wim_info info;
2406 wimlib_get_wim_info(wim, &info);
2407 if (info.image_count != 1) {
2408 imagex_error(T("\"%"TS"\" contains %d images; Please "
2409 "select one (or all)."),
2410 wimfile, info.image_count);
2417 ret = wimlib_iterate_dir_tree(wim, image, path,
2418 WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE,
2419 print_full_path, NULL);
2426 usage(CMD_DIR, stderr);
2431 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2434 imagex_export(int argc, tchar **argv, int cmd)
2438 int export_flags = 0;
2439 int write_flags = 0;
2440 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2441 const tchar *src_wimfile;
2442 const tchar *src_image_num_or_name;
2443 const tchar *dest_wimfile;
2445 const tchar *dest_name;
2446 const tchar *dest_desc;
2448 struct wimlib_wim_info src_info;
2449 WIMStruct *dest_wim;
2454 STRING_SET(refglobs);
2455 unsigned num_threads = 0;
2456 uint32_t chunk_size = UINT32_MAX;
2458 for_opt(c, export_options) {
2460 case IMAGEX_BOOT_OPTION:
2461 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2463 case IMAGEX_CHECK_OPTION:
2464 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2465 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2467 case IMAGEX_NOCHECK_OPTION:
2468 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2470 case IMAGEX_COMPRESS_OPTION:
2471 compression_type = get_compression_type(optarg);
2472 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2475 case IMAGEX_PACK_STREAMS_OPTION:
2476 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
2478 case IMAGEX_CHUNK_SIZE_OPTION:
2479 chunk_size = parse_chunk_size(optarg);
2480 if (chunk_size == UINT32_MAX)
2483 case IMAGEX_REF_OPTION:
2484 ret = string_set_append(&refglobs, optarg);
2486 goto out_free_refglobs;
2488 case IMAGEX_THREADS_OPTION:
2489 num_threads = parse_num_threads(optarg);
2490 if (num_threads == UINT_MAX)
2493 case IMAGEX_REBUILD_OPTION:
2494 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2496 case IMAGEX_PIPABLE_OPTION:
2497 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2499 case IMAGEX_NOT_PIPABLE_OPTION:
2500 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
2508 if (argc < 3 || argc > 5)
2510 src_wimfile = argv[0];
2511 src_image_num_or_name = argv[1];
2512 dest_wimfile = argv[2];
2513 dest_name = (argc >= 4) ? argv[3] : NULL;
2514 dest_desc = (argc >= 5) ? argv[4] : NULL;
2515 ret = wimlib_open_wim(src_wimfile, open_flags, &src_wim,
2516 imagex_progress_func);
2518 goto out_free_refglobs;
2520 wimlib_get_wim_info(src_wim, &src_info);
2522 /* Determine if the destination is an existing file or not. If so, we
2523 * try to append the exported image(s) to it; otherwise, we create a new
2524 * WIM containing the exported image(s). Furthermore, determine if we
2525 * need to write a pipable WIM directly to standard output. */
2527 if (tstrcmp(dest_wimfile, T("-")) == 0) {
2529 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2530 imagex_error("Can't write a non-pipable WIM to "
2531 "standard output! Specify --pipable\n"
2532 " if you want to create a pipable WIM "
2533 "(but read the docs first).");
2535 goto out_free_src_wim;
2538 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2540 dest_wimfile = NULL;
2541 dest_wim_fd = STDOUT_FILENO;
2542 imagex_info_file = stderr;
2543 set_fd_to_binary_mode(dest_wim_fd);
2546 if (dest_wimfile != NULL && tstat(dest_wimfile, &stbuf) == 0) {
2548 /* Destination file exists. */
2550 if (!S_ISREG(stbuf.st_mode)) {
2551 imagex_error(T("\"%"TS"\" is not a regular file"),
2554 goto out_free_src_wim;
2556 ret = wimlib_open_wim(dest_wimfile,
2557 open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2558 &dest_wim, imagex_progress_func);
2560 goto out_free_src_wim;
2562 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2563 /* The user specified a compression type, but we're
2564 * exporting to an existing WIM. Make sure the
2565 * specified compression type is the same as the
2566 * compression type of the existing destination WIM. */
2567 struct wimlib_wim_info dest_info;
2569 wimlib_get_wim_info(dest_wim, &dest_info);
2570 if (compression_type != dest_info.compression_type) {
2571 imagex_error(T("Cannot specify a compression type that is "
2572 "not the same as that used in the "
2573 "destination WIM"));
2575 goto out_free_dest_wim;
2581 if (errno != ENOENT) {
2582 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2585 goto out_free_src_wim;
2588 /* dest_wimfile is not an existing file, so create a new WIM. */
2590 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2591 /* The user did not specify a compression type; default
2592 * to that of the source WIM. */
2594 compression_type = src_info.compression_type;
2596 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2598 goto out_free_src_wim;
2600 /* Use same chunk size if compression type is the same. */
2601 if (compression_type == src_info.compression_type &&
2602 chunk_size == UINT32_MAX)
2603 wimlib_set_output_chunk_size(dest_wim, src_info.chunk_size);
2606 if (chunk_size != UINT32_MAX) {
2607 /* Set destination chunk size. */
2608 ret = wimlib_set_output_chunk_size(dest_wim, chunk_size);
2610 goto out_free_dest_wim;
2613 image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2614 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2616 goto out_free_dest_wim;
2618 if (refglobs.num_strings) {
2619 ret = wim_reference_globs(src_wim, &refglobs, open_flags);
2621 goto out_free_dest_wim;
2624 if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
2625 image == WIMLIB_ALL_IMAGES && src_info.boot_index == 0)
2627 imagex_error(T("--boot specified for all-images export, but source WIM "
2628 "has no bootable image."));
2630 goto out_free_dest_wim;
2633 ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2634 dest_desc, export_flags, imagex_progress_func);
2636 if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2637 do_resource_not_found_warning(src_wimfile,
2638 &src_info, &refglobs);
2640 goto out_free_dest_wim;
2644 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2645 imagex_progress_func);
2646 else if (dest_wimfile)
2647 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2648 write_flags, num_threads,
2649 imagex_progress_func);
2651 ret = wimlib_write_to_fd(dest_wim, dest_wim_fd,
2652 WIMLIB_ALL_IMAGES, write_flags,
2653 num_threads, imagex_progress_func);
2655 wimlib_free(dest_wim);
2657 wimlib_free(src_wim);
2659 string_set_destroy(&refglobs);
2663 usage(CMD_EXPORT, stderr);
2666 goto out_free_refglobs;
2670 is_root_wim_path(const tchar *path)
2673 for (p = path; *p; p++)
2674 if (*p != T('\\') && *p != T('/'))
2680 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2681 const tchar *dest_dir)
2683 for (size_t i = 0; i < num_cmds; i++)
2684 if (cmds[i].fs_dest_path != dest_dir)
2685 free(cmds[i].fs_dest_path);
2689 static struct wimlib_extract_command *
2690 prepare_extract_commands(tchar **paths, unsigned num_paths,
2691 int extract_flags, tchar *dest_dir,
2692 size_t *num_cmds_ret)
2694 struct wimlib_extract_command *cmds;
2696 tchar *emptystr = T("");
2698 if (num_paths == 0) {
2702 num_cmds = num_paths;
2703 cmds = calloc(num_cmds, sizeof(cmds[0]));
2705 imagex_error(T("Out of memory!"));
2709 for (size_t i = 0; i < num_cmds; i++) {
2710 cmds[i].extract_flags = extract_flags;
2711 cmds[i].wim_source_path = paths[i];
2712 if (is_root_wim_path(paths[i])) {
2713 cmds[i].fs_dest_path = dest_dir;
2715 size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2716 cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2717 if (!cmds[i].fs_dest_path) {
2718 free_extract_commands(cmds, num_cmds, dest_dir);
2721 tsprintf(cmds[i].fs_dest_path,
2722 T("%"TS""OS_PREFERRED_PATH_SEPARATOR_STRING"%"TS),
2723 dest_dir, tbasename(paths[i]));
2726 *num_cmds_ret = num_cmds;
2730 /* Extract files or directories from a WIM image */
2732 imagex_extract(int argc, tchar **argv, int cmd)
2739 const tchar *wimfile;
2740 const tchar *image_num_or_name;
2741 const tchar *pathlist;
2742 tchar *dest_dir = T(".");
2743 int extract_flags = WIMLIB_EXTRACT_FLAG_NORPFIX;
2744 int listfile_extract_flags = WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2746 STRING_SET(refglobs);
2748 struct wimlib_extract_command *cmds;
2751 for_opt(c, extract_options) {
2753 case IMAGEX_CHECK_OPTION:
2754 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2756 case IMAGEX_VERBOSE_OPTION:
2757 /* No longer does anything. */
2759 case IMAGEX_REF_OPTION:
2760 ret = string_set_append(&refglobs, optarg);
2762 goto out_free_refglobs;
2764 case IMAGEX_UNIX_DATA_OPTION:
2765 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2767 case IMAGEX_NO_ACLS_OPTION:
2768 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2770 case IMAGEX_STRICT_ACLS_OPTION:
2771 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2773 case IMAGEX_DEST_DIR_OPTION:
2776 case IMAGEX_TO_STDOUT_OPTION:
2777 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2778 imagex_info_file = stderr;
2779 imagex_be_quiet = true;
2781 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2782 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2783 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2785 case IMAGEX_NO_WILDCARDS_OPTION:
2786 listfile_extract_flags &= ~WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2788 case IMAGEX_STRICT_WILDCARDS_OPTION:
2789 listfile_extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_GLOB;
2802 image_num_or_name = argv[1];
2807 if (argc == 1 && argv[0][0] == T('@')) {
2808 pathlist = argv[0] + 1;
2812 cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2819 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2823 image = wimlib_resolve_image(wim, image_num_or_name);
2824 ret = verify_image_exists_and_is_single(image,
2828 goto out_wimlib_free;
2830 if (refglobs.num_strings) {
2831 ret = wim_reference_globs(wim, &refglobs, open_flags);
2833 goto out_wimlib_free;
2837 if (ret == 0 && cmds != NULL) {
2838 ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
2839 imagex_progress_func);
2841 if (ret == 0 && pathlist != NULL) {
2842 ret = wimlib_extract_pathlist(wim, image, dest_dir,
2844 extract_flags | listfile_extract_flags,
2845 imagex_progress_func);
2848 if (!imagex_be_quiet)
2849 imagex_printf(T("Done extracting files.\n"));
2850 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2851 tfprintf(stderr, T("Note: You can use `%"TS"' to see what "
2852 "files and directories\n"
2853 " are in the WIM image.\n"),
2854 get_cmd_string(CMD_DIR, false));
2855 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2856 struct wimlib_wim_info info;
2858 wimlib_get_wim_info(wim, &info);
2859 do_resource_not_found_warning(wimfile, &info, &refglobs);
2864 free_extract_commands(cmds, num_cmds, dest_dir);
2866 string_set_destroy(&refglobs);
2870 usage(CMD_EXTRACT, stderr);
2873 goto out_free_refglobs;
2876 static void print_byte_field(const uint8_t field[], size_t len)
2879 tprintf(T("%02hhx"), *field++);
2883 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2885 tputs(T("WIM Information:"));
2886 tputs(T("----------------"));
2887 tprintf(T("Path: %"TS"\n"), wimfile);
2888 tprintf(T("GUID: 0x"));
2889 print_byte_field(info->guid, sizeof(info->guid));
2891 tprintf(T("Version: %u\n"), info->wim_version);
2892 tprintf(T("Image Count: %d\n"), info->image_count);
2893 tprintf(T("Compression: %"TS"\n"),
2894 wimlib_get_compression_type_string(info->compression_type));
2895 tprintf(T("Chunk Size: %"PRIu32" bytes\n"),
2897 tprintf(T("Part Number: %d/%d\n"), info->part_number, info->total_parts);
2898 tprintf(T("Boot Index: %d\n"), info->boot_index);
2899 tprintf(T("Size: %"PRIu64" bytes\n"), info->total_bytes);
2900 tprintf(T("Integrity Info: %"TS"\n"),
2901 info->has_integrity_table ? T("yes") : T("no"));
2902 tprintf(T("Relative path junction: %"TS"\n"),
2903 info->has_rpfix ? T("yes") : T("no"));
2904 tprintf(T("Pipable: %"TS"\n"),
2905 info->pipable ? T("yes") : T("no"));
2910 print_resource(const struct wimlib_resource_entry *resource,
2913 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
2914 resource->uncompressed_size);
2915 if (resource->packed) {
2916 tprintf(T("Raw compressed size = %"PRIu64" bytes\n"),
2917 resource->raw_resource_compressed_size);
2919 tprintf(T("Raw offset in WIM = %"PRIu64" bytes\n"),
2920 resource->raw_resource_offset_in_wim);
2922 tprintf(T("Offset in raw = %"PRIu64" bytes\n"),
2925 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
2926 resource->compressed_size);
2928 tprintf(T("Offset in WIM = %"PRIu64" bytes\n"),
2932 tprintf(T("Part Number = %u\n"), resource->part_number);
2933 tprintf(T("Reference Count = %u\n"), resource->reference_count);
2935 tprintf(T("Hash = 0x"));
2936 print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2939 tprintf(T("Flags = "));
2940 if (resource->is_compressed)
2941 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED "));
2942 if (resource->is_metadata)
2943 tprintf(T("WIM_RESHDR_FLAG_METADATA "));
2944 if (resource->is_free)
2945 tprintf(T("WIM_RESHDR_FLAG_FREE "));
2946 if (resource->is_spanned)
2947 tprintf(T("WIM_RESHDR_FLAG_SPANNED "));
2948 if (resource->packed)
2949 tprintf(T("WIM_RESHDR_FLAG_PACKED_STREAMS "));
2956 print_lookup_table(WIMStruct *wim)
2958 wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2961 /* Prints information about a WIM file; also can mark an image as bootable,
2962 * change the name of an image, or change the description of an image. */
2964 imagex_info(int argc, tchar **argv, int cmd)
2969 bool nocheck = false;
2970 bool header = false;
2971 bool lookup_table = false;
2973 bool metadata = false;
2974 bool short_header = true;
2975 const tchar *xml_out_file = NULL;
2976 const tchar *wimfile;
2977 const tchar *image_num_or_name;
2978 const tchar *new_name;
2979 const tchar *new_desc;
2984 struct wimlib_wim_info info;
2986 for_opt(c, info_options) {
2988 case IMAGEX_BOOT_OPTION:
2991 case IMAGEX_CHECK_OPTION:
2994 case IMAGEX_NOCHECK_OPTION:
2997 case IMAGEX_HEADER_OPTION:
2999 short_header = false;
3001 case IMAGEX_LOOKUP_TABLE_OPTION:
3002 lookup_table = true;
3003 short_header = false;
3005 case IMAGEX_XML_OPTION:
3007 short_header = false;
3009 case IMAGEX_EXTRACT_XML_OPTION:
3010 xml_out_file = optarg;
3011 short_header = false;
3013 case IMAGEX_METADATA_OPTION:
3015 short_header = false;
3024 if (argc < 1 || argc > 4)
3028 image_num_or_name = (argc >= 2) ? argv[1] : T("all");
3029 new_name = (argc >= 3) ? argv[2] : NULL;
3030 new_desc = (argc >= 4) ? argv[3] : NULL;
3032 if (check && nocheck) {
3033 imagex_error(T("Can't specify both --check and --nocheck"));
3038 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3040 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3044 wimlib_get_wim_info(wim, &info);
3046 image = wimlib_resolve_image(wim, image_num_or_name);
3047 ret = WIMLIB_ERR_INVALID_IMAGE;
3048 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
3049 verify_image_exists(image, image_num_or_name, wimfile);
3051 imagex_error(T("If you would like to set the boot "
3052 "index to 0, specify image \"0\" with "
3053 "the --boot flag."));
3055 goto out_wimlib_free;
3058 if (boot && info.image_count == 0) {
3059 imagex_error(T("--boot is meaningless on a WIM with no images"));
3060 goto out_wimlib_free;
3063 if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
3065 imagex_error(T("Cannot specify the --boot flag "
3066 "without specifying a specific "
3067 "image in a multi-image WIM"));
3068 goto out_wimlib_free;
3071 imagex_error(T("Cannot specify the NEW_NAME "
3072 "without specifying a specific "
3073 "image in a multi-image WIM"));
3074 goto out_wimlib_free;
3078 /* Operations that print information are separated from operations that
3079 * recreate the WIM file. */
3080 if (!new_name && !boot) {
3082 /* Read-only operations */
3084 if (image == WIMLIB_NO_IMAGE) {
3085 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
3086 image_num_or_name, wimfile);
3087 goto out_wimlib_free;
3090 if (image == WIMLIB_ALL_IMAGES && short_header)
3091 print_wim_information(wimfile, &info);
3094 wimlib_print_header(wim);
3097 if (info.total_parts != 1) {
3098 tfprintf(stderr, T("Warning: Only showing the lookup table "
3099 "for part %d of a %d-part WIM.\n"),
3100 info.part_number, info.total_parts);
3102 print_lookup_table(wim);
3106 ret = wimlib_extract_xml_data(wim, stdout);
3108 goto out_wimlib_free;
3114 fp = tfopen(xml_out_file, T("wb"));
3116 imagex_error_with_errno(T("Failed to open the "
3117 "file \"%"TS"\" for "
3121 goto out_wimlib_free;
3123 ret = wimlib_extract_xml_data(wim, fp);
3125 imagex_error(T("Failed to close the file "
3131 goto out_wimlib_free;
3135 wimlib_print_available_images(wim, image);
3138 ret = wimlib_print_metadata(wim, image);
3140 goto out_wimlib_free;
3145 /* Modification operations */
3147 if (image == WIMLIB_ALL_IMAGES)
3150 if (image == WIMLIB_NO_IMAGE && new_name) {
3151 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
3152 "when using image 0"), new_name);
3154 goto out_wimlib_free;
3158 if (image == info.boot_index) {
3159 imagex_printf(T("Image %d is already marked as "
3160 "bootable.\n"), image);
3163 imagex_printf(T("Marking image %d as bootable.\n"),
3165 info.boot_index = image;
3166 ret = wimlib_set_wim_info(wim, &info,
3167 WIMLIB_CHANGE_BOOT_INDEX);
3169 goto out_wimlib_free;
3173 if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
3175 imagex_printf(T("Image %d is already named \"%"TS"\".\n"),
3179 imagex_printf(T("Changing the name of image %d to "
3180 "\"%"TS"\".\n"), image, new_name);
3181 ret = wimlib_set_image_name(wim, image, new_name);
3183 goto out_wimlib_free;
3187 const tchar *old_desc;
3188 old_desc = wimlib_get_image_description(wim, image);
3189 if (old_desc && !tstrcmp(old_desc, new_desc)) {
3190 imagex_printf(T("The description of image %d is already "
3191 "\"%"TS"\".\n"), image, new_desc);
3194 imagex_printf(T("Changing the description of image %d "
3195 "to \"%"TS"\".\n"), image, new_desc);
3196 ret = wimlib_set_image_descripton(wim, image,
3199 goto out_wimlib_free;
3203 /* Only call wimlib_overwrite() if something actually needs to
3205 if (boot || new_name || new_desc ||
3206 (check && !info.has_integrity_table) ||
3207 (nocheck && info.has_integrity_table))
3209 int write_flags = 0;
3212 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3214 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3215 ret = wimlib_overwrite(wim, write_flags, 1,
3216 imagex_progress_func);
3218 imagex_printf(T("The file \"%"TS"\" was not modified "
3219 "because nothing needed to be done.\n"),
3230 usage(CMD_INFO, stderr);
3236 /* Join split WIMs into one part WIM */
3238 imagex_join(int argc, tchar **argv, int cmd)
3241 int swm_open_flags = 0;
3242 int wim_write_flags = 0;
3243 const tchar *output_path;
3246 for_opt(c, join_options) {
3248 case IMAGEX_CHECK_OPTION:
3249 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3250 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3260 imagex_error(T("Must specify one or more split WIM (.swm) "
3264 output_path = argv[0];
3265 ret = wimlib_join((const tchar * const *)++argv,
3270 imagex_progress_func);
3275 usage(CMD_JOIN, stderr);
3280 #if WIM_MOUNTING_SUPPORTED
3282 /* Mounts a WIM image. */
3284 imagex_mount_rw_or_ro(int argc, tchar **argv, int cmd)
3287 int mount_flags = 0;
3289 const tchar *staging_dir = NULL;
3290 const tchar *wimfile;
3293 struct wimlib_wim_info info;
3297 STRING_SET(refglobs);
3299 if (cmd == CMD_MOUNTRW) {
3300 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
3301 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3304 for_opt(c, mount_options) {
3306 case IMAGEX_ALLOW_OTHER_OPTION:
3307 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
3309 case IMAGEX_CHECK_OPTION:
3310 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3312 case IMAGEX_DEBUG_OPTION:
3313 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
3315 case IMAGEX_STREAMS_INTERFACE_OPTION:
3316 if (!tstrcasecmp(optarg, T("none")))
3317 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
3318 else if (!tstrcasecmp(optarg, T("xattr")))
3319 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
3320 else if (!tstrcasecmp(optarg, T("windows")))
3321 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
3323 imagex_error(T("Unknown stream interface \"%"TS"\""),
3328 case IMAGEX_REF_OPTION:
3329 ret = string_set_append(&refglobs, optarg);
3331 goto out_free_refglobs;
3333 case IMAGEX_STAGING_DIR_OPTION:
3334 staging_dir = optarg;
3336 case IMAGEX_UNIX_DATA_OPTION:
3337 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
3345 if (argc != 2 && argc != 3)
3350 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3352 goto out_free_refglobs;
3354 wimlib_get_wim_info(wim, &info);
3357 /* Image explicitly specified. */
3358 image = wimlib_resolve_image(wim, argv[1]);
3360 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
3364 /* No image specified; default to image 1, but only if the WIM
3365 * contains exactly one image. */
3367 if (info.image_count != 1) {
3368 imagex_error(T("\"%"TS"\" contains %d images; Please "
3369 "select one."), wimfile, info.image_count);
3377 if (refglobs.num_strings) {
3378 ret = wim_reference_globs(wim, &refglobs, open_flags);
3383 ret = wimlib_mount_image(wim, image, dir, mount_flags, staging_dir);
3385 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
3387 image, wimfile, dir);
3392 string_set_destroy(&refglobs);
3398 goto out_free_refglobs;
3400 #endif /* WIM_MOUNTING_SUPPORTED */
3402 /* Rebuild a WIM file */
3404 imagex_optimize(int argc, tchar **argv, int cmd)
3407 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3408 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
3409 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
3410 uint32_t chunk_size = UINT32_MAX;
3413 const tchar *wimfile;
3416 unsigned num_threads = 0;
3418 for_opt(c, optimize_options) {
3420 case IMAGEX_CHECK_OPTION:
3421 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3422 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3424 case IMAGEX_NOCHECK_OPTION:
3425 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3427 case IMAGEX_COMPRESS_OPTION:
3428 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3429 compression_type = get_compression_type(optarg);
3430 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
3433 case IMAGEX_RECOMPRESS_OPTION:
3434 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3436 case IMAGEX_COMPRESS_SLOW_OPTION:
3437 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3438 set_compress_slow();
3440 case IMAGEX_CHUNK_SIZE_OPTION:
3441 chunk_size = parse_chunk_size(optarg);
3442 if (chunk_size == UINT32_MAX)
3445 case IMAGEX_PACK_STREAMS_OPTION:
3446 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
3447 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3449 case IMAGEX_THREADS_OPTION:
3450 num_threads = parse_num_threads(optarg);
3451 if (num_threads == UINT_MAX)
3454 case IMAGEX_PIPABLE_OPTION:
3455 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
3457 case IMAGEX_NOT_PIPABLE_OPTION:
3458 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
3472 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3476 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
3477 /* Change compression type. */
3478 ret = wimlib_set_output_compression_type(wim, compression_type);
3480 goto out_wimlib_free;
3483 if (chunk_size != UINT32_MAX) {
3484 /* Change chunk size. */
3485 ret = wimlib_set_output_chunk_size(wim, chunk_size);
3487 goto out_wimlib_free;
3490 old_size = file_get_size(wimfile);
3491 tprintf(T("\"%"TS"\" original size: "), wimfile);
3493 tputs(T("Unknown"));
3495 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3497 ret = wimlib_overwrite(wim, write_flags, num_threads,
3498 imagex_progress_func);
3500 imagex_error(T("Optimization of \"%"TS"\" failed."), wimfile);
3501 goto out_wimlib_free;
3504 new_size = file_get_size(wimfile);
3505 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3507 tputs(T("Unknown"));
3509 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3511 tfputs(T("Space saved: "), stdout);
3512 if (new_size != -1 && old_size != -1) {
3513 tprintf(T("%lld KiB\n"),
3514 ((long long)old_size - (long long)new_size) >> 10);
3516 tputs(T("Unknown"));
3525 usage(CMD_OPTIMIZE, stderr);
3531 /* Split a WIM into a spanned set */
3533 imagex_split(int argc, tchar **argv, int cmd)
3537 int write_flags = 0;
3538 unsigned long part_size;
3543 for_opt(c, split_options) {
3545 case IMAGEX_CHECK_OPTION:
3546 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3547 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3559 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3560 if (tmp == argv[2] || *tmp) {
3561 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3562 imagex_error(T("The part size must be an integer or "
3563 "floating-point number of megabytes."));
3566 ret = wimlib_open_wim(argv[0], open_flags, &wim, imagex_progress_func);
3570 ret = wimlib_split(wim, argv[1], part_size, write_flags, imagex_progress_func);
3576 usage(CMD_SPLIT, stderr);
3582 #if WIM_MOUNTING_SUPPORTED
3583 /* Unmounts a mounted WIM image. */
3585 imagex_unmount(int argc, tchar **argv, int cmd)
3588 int unmount_flags = 0;
3591 for_opt(c, unmount_options) {
3593 case IMAGEX_COMMIT_OPTION:
3594 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3596 case IMAGEX_CHECK_OPTION:
3597 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3599 case IMAGEX_REBUILD_OPTION:
3600 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3602 case IMAGEX_LAZY_OPTION:
3603 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3614 ret = wimlib_unmount_image(argv[0], unmount_flags,
3615 imagex_progress_func);
3617 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3622 usage(CMD_UNMOUNT, stderr);
3626 #endif /* WIM_MOUNTING_SUPPORTED */
3629 * Add, delete, or rename files in a WIM image.
3632 imagex_update(int argc, tchar **argv, int cmd)
3634 const tchar *wimfile;
3638 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3639 int write_flags = 0;
3640 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3641 int default_add_flags = WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
3642 WIMLIB_ADD_FLAG_VERBOSE;
3643 int default_delete_flags = 0;
3644 unsigned num_threads = 0;
3646 tchar *cmd_file_contents;
3647 size_t cmd_file_nchars;
3648 struct wimlib_update_command *cmds;
3650 tchar *command_str = NULL;
3652 const tchar *config_file = NULL;
3654 struct wimlib_capture_config *config;
3656 for_opt(c, update_options) {
3658 /* Generic or write options */
3659 case IMAGEX_THREADS_OPTION:
3660 num_threads = parse_num_threads(optarg);
3661 if (num_threads == UINT_MAX)
3664 case IMAGEX_CHECK_OPTION:
3665 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3666 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3668 case IMAGEX_REBUILD_OPTION:
3669 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3671 case IMAGEX_COMMAND_OPTION:
3673 imagex_error(T("--command may only be specified "
3674 "one time. Please provide\n"
3675 " the update commands "
3676 "on standard input instead."));
3679 command_str = tstrdup(optarg);
3681 imagex_error(T("Out of memory!"));
3685 /* Default delete options */
3686 case IMAGEX_FORCE_OPTION:
3687 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3689 case IMAGEX_RECURSIVE_OPTION:
3690 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3693 /* Global add option */
3694 case IMAGEX_CONFIG_OPTION:
3695 default_add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
3696 config_file = optarg;
3699 /* Default add options */
3700 case IMAGEX_VERBOSE_OPTION:
3701 /* No longer does anything. */
3703 case IMAGEX_DEREFERENCE_OPTION:
3704 default_add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
3706 case IMAGEX_UNIX_DATA_OPTION:
3707 default_add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
3709 case IMAGEX_NO_ACLS_OPTION:
3710 default_add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
3712 case IMAGEX_STRICT_ACLS_OPTION:
3713 default_add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
3722 if (argc != 1 && argc != 2)
3726 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3728 goto out_free_command_str;
3731 /* Image explicitly specified. */
3732 image = wimlib_resolve_image(wim, argv[1]);
3733 ret = verify_image_exists_and_is_single(image, argv[1],
3736 goto out_wimlib_free;
3738 /* No image specified; default to image 1, but only if the WIM
3739 * contains exactly one image. */
3740 struct wimlib_wim_info info;
3742 wimlib_get_wim_info(wim, &info);
3743 if (info.image_count != 1) {
3744 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3745 wimfile, info.image_count);
3752 /* Parse capture configuration file if specified */
3756 config_str = file_get_text_contents(config_file, &config_len);
3759 goto out_wimlib_free;
3762 config = alloca(sizeof(*config));
3763 ret = parse_capture_config(&config_str, config_len, config);
3765 goto out_free_config;
3768 default_add_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
3771 /* Read update commands from standard input, or the command string if
3774 cmd_file_contents = NULL;
3775 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3778 if (isatty(STDIN_FILENO)) {
3779 tputs(T("Reading update commands from standard input..."));
3780 recommend_man_page(CMD_UPDATE, stdout);
3782 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3783 if (!cmd_file_contents) {
3785 goto out_free_config;
3788 /* Parse the update commands */
3789 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3794 goto out_free_cmd_file_contents;
3797 /* Set default flags and capture config on the update commands */
3798 for (size_t i = 0; i < num_cmds; i++) {
3799 switch (cmds[i].op) {
3800 case WIMLIB_UPDATE_OP_ADD:
3801 cmds[i].add.add_flags |= default_add_flags;
3802 cmds[i].add.config = config;
3804 case WIMLIB_UPDATE_OP_DELETE:
3805 cmds[i].delete_.delete_flags |= default_delete_flags;
3812 /* Execute the update commands */
3813 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3814 imagex_progress_func);
3818 /* Overwrite the updated WIM */
3819 ret = wimlib_overwrite(wim, write_flags, num_threads,
3820 imagex_progress_func);
3823 out_free_cmd_file_contents:
3824 free(cmd_file_contents);
3827 free(config->exclusion_pats.pats);
3828 free(config->exclusion_exception_pats.pats);
3833 out_free_command_str:
3838 usage(CMD_UPDATE, stderr);
3841 goto out_free_command_str;
3846 struct imagex_command {
3848 int (*func)(int argc, tchar **argv, int cmd);
3851 static const struct imagex_command imagex_commands[] = {
3852 [CMD_APPEND] = {T("append"), imagex_capture_or_append},
3853 [CMD_APPLY] = {T("apply"), imagex_apply},
3854 [CMD_CAPTURE] = {T("capture"), imagex_capture_or_append},
3855 [CMD_DELETE] = {T("delete"), imagex_delete},
3856 [CMD_DIR ] = {T("dir"), imagex_dir},
3857 [CMD_EXPORT] = {T("export"), imagex_export},
3858 [CMD_EXTRACT] = {T("extract"), imagex_extract},
3859 [CMD_INFO] = {T("info"), imagex_info},
3860 [CMD_JOIN] = {T("join"), imagex_join},
3861 #if WIM_MOUNTING_SUPPORTED
3862 [CMD_MOUNT] = {T("mount"), imagex_mount_rw_or_ro},
3863 [CMD_MOUNTRW] = {T("mountrw"), imagex_mount_rw_or_ro},
3865 [CMD_OPTIMIZE] = {T("optimize"), imagex_optimize},
3866 [CMD_SPLIT] = {T("split"), imagex_split},
3867 #if WIM_MOUNTING_SUPPORTED
3868 [CMD_UNMOUNT] = {T("unmount"), imagex_unmount},
3870 [CMD_UPDATE] = {T("update"), imagex_update},
3873 static const tchar *usage_strings[] = {
3876 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3877 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3878 " [--nocheck] [--flags EDITION_ID] [--dereference]\n"
3879 " [--config=FILE] [--threads=NUM_THREADS] [--source-list]\n"
3880 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3881 " [--update-of=[WIMFILE:]IMAGE]\n"
3885 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME | all)]\n"
3886 " (DIRECTORY | NTFS_VOLUME) [--check] [--ref=\"GLOB\"]\n"
3887 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3888 " [--hardlink] [--symlink] [--include-invalid-names]\n"
3892 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3893 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3894 " [--nocheck] [--compress=TYPE] [--flags EDITION_ID]\n"
3895 " [--dereference] [--config=FILE] [--threads=NUM_THREADS]\n"
3896 " [--source-list] [--no-acls] [--strict-acls] [--rpfix]\n"
3897 " [--norpfix] [--update-of=[WIMFILE:]IMAGE]\n"
3898 " [--delta-from=WIMFILE]\n"
3902 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n"
3903 " [--check] [--soft]\n"
3907 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH]\n"
3911 " %"TS" SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
3912 " DEST_WIMFILE [DEST_IMAGE_NAME [DEST_IMAGE_DESCRIPTION]]\n"
3913 " [--boot] [--check] [--nocheck] [--compress=TYPE]\n"
3914 " [--ref=\"GLOB\"] [--threads=NUM_THREADS] [--rebuild]\n"
3918 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME) ([PATH...] | @LISTFILE)\n"
3919 " [--check] [--ref=\"GLOB\"] [--no-acls] [--strict-acls]\n"
3920 " [--to-stdout] [--dest-dir=CMD_DIR]\n"
3921 " [--include-invalid-names]\n"
3925 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME) [NEW_NAME\n"
3926 " [NEW_DESC]]] [--boot] [--check] [--nocheck] [--xml]\n"
3927 " [--extract-xml FILE] [--header] [--lookup-table]\n"
3931 " %"TS" OUT_WIMFILE SPLIT_WIM_PART... [--check]\n"
3933 #if WIM_MOUNTING_SUPPORTED
3936 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3937 " [--check] [--streams-interface=INTERFACE]\n"
3938 " [--ref=\"GLOB\"] [--allow-other]\n"
3942 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3943 " [--check] [--streams-interface=INTERFACE]\n"
3944 " [--staging-dir=CMD_DIR] [--allow-other]\n"
3949 " %"TS" WIMFILE [--check] [--nocheck] [--recompress]\n"
3950 " [--recompress-slow] [--compress=TYPE]\n"
3951 " [--threads=NUM_THREADS]\n"
3955 " %"TS" WIMFILE SPLIT_WIM_PART_1 PART_SIZE_MB [--check]\n"
3957 #if WIM_MOUNTING_SUPPORTED
3960 " %"TS" DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
3965 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
3966 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
3967 " [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
3971 static const tchar *invocation_name;
3972 static int invocation_cmd = CMD_NONE;
3974 static const tchar *get_cmd_string(int cmd, bool nospace)
3976 static tchar buf[50];
3977 if (cmd == CMD_NONE) {
3978 tsprintf(buf, T("%"TS), T(IMAGEX_PROGNAME));
3979 } else if (invocation_cmd != CMD_NONE) {
3980 tsprintf(buf, T("wim%"TS), imagex_commands[cmd].name);
3982 const tchar *format;
3985 format = T("%"TS"-%"TS"");
3987 format = T("%"TS" %"TS"");
3988 tsprintf(buf, format, invocation_name, imagex_commands[cmd].name);
3996 static const tchar *s =
3998 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
3999 "Copyright (C) 2012, 2013 Eric Biggers\n"
4000 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
4001 "This is free software: you are free to change and redistribute it.\n"
4002 "There is NO WARRANTY, to the extent permitted by law.\n"
4004 "Report bugs to "PACKAGE_BUGREPORT".\n"
4011 help_or_version(int argc, tchar **argv, int cmd)
4016 for (i = 1; i < argc; i++) {
4018 if (p[0] == T('-') && p[1] == T('-')) {
4020 if (!tstrcmp(p, T("help"))) {
4021 if (cmd == CMD_NONE)
4026 } else if (!tstrcmp(p, T("version"))) {
4035 print_usage_string(int cmd, FILE *fp)
4037 tfprintf(fp, usage_strings[cmd], get_cmd_string(cmd, false));
4041 recommend_man_page(int cmd, FILE *fp)
4043 const tchar *format_str;
4045 format_str = T("Uncommon options are not listed;\n"
4046 "See %"TS".pdf in the doc directory for more details.\n");
4048 format_str = T("Uncommon options are not listed;\n"
4049 "Try `man %"TS"' for more details.\n");
4051 tfprintf(fp, format_str, get_cmd_string(cmd, true));
4055 usage(int cmd, FILE *fp)
4057 tfprintf(fp, T("Usage:\n"));
4058 print_usage_string(cmd, fp);
4059 tfprintf(fp, T("\n"));
4060 recommend_man_page(cmd, fp);
4066 tfprintf(fp, T("Usage:\n"));
4067 for (int cmd = 0; cmd < CMD_MAX; cmd++) {
4068 print_usage_string(cmd, fp);
4069 tfprintf(fp, T("\n"));
4071 static const tchar *extra =
4074 " %"TS" --version\n"
4076 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
4079 tfprintf(fp, extra, invocation_name, invocation_name);
4080 recommend_man_page(CMD_NONE, fp);
4083 /* Entry point for wimlib's ImageX implementation. On UNIX the command
4084 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
4085 * something else), while an Windows the command arguments will be UTF-16LE
4086 * encoded 'wchar_t' strings. */
4089 wmain(int argc, wchar_t **argv, wchar_t **envp)
4091 main(int argc, char **argv)
4098 imagex_info_file = stdout;
4099 invocation_name = tbasename(argv[0]);
4102 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
4103 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
4107 setlocale(LC_ALL, "");
4108 codeset = nl_langinfo(CODESET);
4109 if (!strstr(codeset, "UTF-8") &&
4110 !strstr(codeset, "UTF8") &&
4111 !strstr(codeset, "utf-8") &&
4112 !strstr(codeset, "utf8"))
4115 "WARNING: Running %"TS" in a UTF-8 locale is recommended!\n"
4116 " Maybe try: `export LANG=en_US.UTF-8'?\n"
4117 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
4118 " to any value to force wimlib to use UTF-8.\n",
4124 #endif /* !__WIN32__ */
4127 tchar *igcase = tgetenv(T("WIMLIB_IMAGEX_IGNORE_CASE"));
4128 if (igcase != NULL) {
4129 if (!tstrcmp(igcase, T("no")) ||
4130 !tstrcmp(igcase, T("0")))
4131 init_flags |= WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE;
4132 else if (!tstrcmp(igcase, T("yes")) ||
4133 !tstrcmp(igcase, T("1")))
4134 init_flags |= WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE;
4137 "WARNING: Ignoring unknown setting of "
4138 "WIMLIB_IMAGEX_IGNORE_CASE\n");
4143 /* Allow being invoked as wimCOMMAND (e.g. wimapply). */
4145 if (!tstrncmp(invocation_name, T("wim"), 3) &&
4146 tstrcmp(invocation_name, T(IMAGEX_PROGNAME))) {
4147 for (int i = 0; i < CMD_MAX; i++) {
4148 if (!tstrcmp(invocation_name + 3,
4149 imagex_commands[i].name))
4158 /* Unless already known from the invocation name, determine which
4159 * command was specified. */
4160 if (cmd == CMD_NONE) {
4162 imagex_error(T("No command specified!\n"));
4166 for (int i = 0; i < CMD_MAX; i++) {
4167 if (!tstrcmp(argv[1], imagex_commands[i].name)) {
4172 if (cmd != CMD_NONE) {
4178 /* Handle --help and --version. --help can be either for the program as
4179 * a whole (cmd == CMD_NONE) or just for a specific command (cmd !=
4180 * CMD_NONE). Note: help_or_version() will not return if a --help or
4181 * --version argument was found. */
4182 help_or_version(argc, argv, cmd);
4184 /* Bail if a valid command was not specified. */
4185 if (cmd == CMD_NONE) {
4186 imagex_error(T("Unrecognized command: `%"TS"'\n"), argv[1]);
4191 /* Enable warning and error messages in wimlib be more user-friendly.
4193 wimlib_set_print_errors(true);
4195 /* Initialize wimlib. */
4196 ret = wimlib_global_init(init_flags);
4198 goto out_check_status;
4200 /* Call the command handler function. */
4201 ret = imagex_commands[cmd].func(argc, argv, cmd);
4203 /* Check for error writing to standard output, especially since for some
4204 * commands, writing to standard output is part of the program's actual
4205 * behavior and not just for informational purposes. */
4206 if (ferror(stdout) || fclose(stdout)) {
4207 imagex_error_with_errno(T("error writing to standard output"));
4212 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
4213 * itself (not by wimlib). 0 indicates success. > 0 indicates a wimlib
4214 * error code from which an error message can be printed. */
4216 imagex_error(T("Exiting with error code %d:\n"
4218 wimlib_get_error_string(ret));
4219 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
4220 imagex_error_with_errno(T("errno"));
4222 /* Make wimlib free any resources it's holding (although this is not
4223 * strictly necessary because the process is ending anyway). */
4224 wimlib_global_cleanup();