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 OS_PREFERRED_PATH_SEPARATOR L'\\'
52 # define OS_PREFERRED_PATH_SEPARATOR_STRING L"\\"
53 # define print_security_descriptor win32_print_security_descriptor
56 # include <langinfo.h>
57 # define OS_PREFERRED_PATH_SEPARATOR '/'
58 # define OS_PREFERRED_PATH_SEPARATOR_STRING "/"
59 # define print_security_descriptor default_print_security_descriptor
60 static inline void set_fd_to_binary_mode(int fd)
65 /* Don't confuse the user by presenting the mounting commands on Windows when
66 * they will never work. However on UNIX-like systems we always present them,
67 * even if WITH_FUSE is not defined at this point, as to not tie the build of
68 * wimlib-imagex to a specific build of wimlib. */
70 # define WIM_MOUNTING_SUPPORTED 0
72 # define WIM_MOUNTING_SUPPORTED 1
75 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
78 is_any_path_separator(tchar c)
80 return c == T('/') || c == T('\\');
83 /* Like basename(), but handles both forward and backwards slashes. */
85 tbasename(tchar *path)
87 tchar *p = tstrchr(path, T('\0'));
92 if (!is_any_path_separator(*--p))
100 if (is_any_path_separator(*--p))
105 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
119 #if WIM_MOUNTING_SUPPORTED
125 #if WIM_MOUNTING_SUPPORTED
132 static void usage(int cmd, FILE *fp);
133 static void usage_all(FILE *fp);
134 static void recommend_man_page(int cmd, FILE *fp);
135 static const tchar *get_cmd_string(int cmd, bool nospace);
137 static int imagex_progress_func(enum wimlib_progress_msg msg,
138 const union wimlib_progress_info *info);
140 static bool imagex_be_quiet = false;
141 static FILE *imagex_info_file;
143 #define imagex_printf(format, ...) \
144 tfprintf(imagex_info_file, format, ##__VA_ARGS__)
147 IMAGEX_ALLOW_OTHER_OPTION,
150 IMAGEX_CHUNK_SIZE_OPTION,
151 IMAGEX_COMMAND_OPTION,
152 IMAGEX_COMMIT_OPTION,
153 IMAGEX_COMPRESS_OPTION,
154 IMAGEX_COMPRESS_SLOW_OPTION,
155 IMAGEX_CONFIG_OPTION,
157 IMAGEX_DELTA_FROM_OPTION,
158 IMAGEX_DEREFERENCE_OPTION,
159 IMAGEX_DEST_DIR_OPTION,
160 IMAGEX_DETAILED_OPTION,
161 IMAGEX_EXTRACT_XML_OPTION,
164 IMAGEX_HARDLINK_OPTION,
165 IMAGEX_HEADER_OPTION,
166 IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
168 IMAGEX_LOOKUP_TABLE_OPTION,
169 IMAGEX_METADATA_OPTION,
170 IMAGEX_NOCHECK_OPTION,
171 IMAGEX_NORPFIX_OPTION,
172 IMAGEX_NOT_PIPABLE_OPTION,
173 IMAGEX_NO_ACLS_OPTION,
174 IMAGEX_NO_ATTRIBUTES_OPTION,
175 IMAGEX_NO_WILDCARDS_OPTION,
176 IMAGEX_NULLGLOB_OPTION,
177 IMAGEX_ONE_FILE_ONLY_OPTION,
178 IMAGEX_PACK_CHUNK_SIZE_OPTION,
179 IMAGEX_PACK_STREAMS_OPTION,
181 IMAGEX_PIPABLE_OPTION,
182 IMAGEX_PRESERVE_DIR_STRUCTURE_OPTION,
183 IMAGEX_REBUILD_OPTION,
184 IMAGEX_RECOMPRESS_OPTION,
185 IMAGEX_RECURSIVE_OPTION,
187 IMAGEX_RESUME_OPTION,
190 IMAGEX_SOURCE_LIST_OPTION,
191 IMAGEX_STAGING_DIR_OPTION,
192 IMAGEX_STREAMS_INTERFACE_OPTION,
193 IMAGEX_STRICT_ACLS_OPTION,
194 IMAGEX_SYMLINK_OPTION,
195 IMAGEX_THREADS_OPTION,
196 IMAGEX_TO_STDOUT_OPTION,
197 IMAGEX_UNIX_DATA_OPTION,
198 IMAGEX_UPDATE_OF_OPTION,
199 IMAGEX_VERBOSE_OPTION,
203 static const struct option apply_options[] = {
204 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
205 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
206 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
207 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
208 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
209 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
210 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
211 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
212 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
213 {T("no-attributes"), no_argument, NULL, IMAGEX_NO_ATTRIBUTES_OPTION},
214 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
215 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
216 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
218 /* --resume is undocumented for now as it needs improvement. */
219 {T("resume"), no_argument, NULL, IMAGEX_RESUME_OPTION},
223 static const struct option capture_or_append_options[] = {
224 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
225 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
226 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
227 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
228 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
229 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
230 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
231 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
232 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
233 {T("pack-streams"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
234 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
235 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
236 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
237 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
238 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
239 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
240 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
241 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
242 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
243 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
244 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
245 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
246 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
247 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
248 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
249 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
250 {T("update-of"), required_argument, NULL, IMAGEX_UPDATE_OF_OPTION},
251 {T("delta-from"), required_argument, NULL, IMAGEX_DELTA_FROM_OPTION},
255 static const struct option delete_options[] = {
256 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
257 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
261 static const struct option dir_options[] = {
262 {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
263 {T("detailed"), no_argument, NULL, IMAGEX_DETAILED_OPTION},
264 {T("one-file-only"), no_argument, NULL, IMAGEX_ONE_FILE_ONLY_OPTION},
268 static const struct option export_options[] = {
269 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
270 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
271 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
272 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
273 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
274 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
275 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
276 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
277 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
278 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
279 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
280 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
281 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
282 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
283 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
287 static const struct option extract_options[] = {
288 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
289 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
290 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
291 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
292 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
293 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
294 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
295 {T("no-attributes"), no_argument, NULL, IMAGEX_NO_ATTRIBUTES_OPTION},
296 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
297 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
298 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
299 {T("no-wildcards"), no_argument, NULL, IMAGEX_NO_WILDCARDS_OPTION},
300 {T("nullglob"), no_argument, NULL, IMAGEX_NULLGLOB_OPTION},
301 {T("preserve-dir-structure"), no_argument, NULL, IMAGEX_PRESERVE_DIR_STRUCTURE_OPTION},
305 static const struct option info_options[] = {
306 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
307 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
308 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
309 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
310 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
311 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
312 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
313 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
314 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
318 static const struct option join_options[] = {
319 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
323 static const struct option mount_options[] = {
324 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
325 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
326 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
327 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
328 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
329 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
330 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
334 static const struct option optimize_options[] = {
335 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
336 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
337 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
338 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
339 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
340 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
341 {T("recompress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
342 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
343 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
344 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
345 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
346 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
347 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
348 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
349 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
353 static const struct option split_options[] = {
354 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
358 static const struct option unmount_options[] = {
359 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
360 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
361 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
362 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
366 static const struct option update_options[] = {
367 /* Careful: some of the options here set the defaults for update
368 * commands, but the flags given to an actual update command (and not to
369 * `imagex update' itself are also handled in
370 * update_command_add_option(). */
371 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
372 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
373 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
374 {T("command"), required_argument, NULL, IMAGEX_COMMAND_OPTION},
376 /* Default delete options */
377 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
378 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
380 /* Global add option */
381 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
383 /* Default add options */
384 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
385 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
386 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
387 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
388 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
389 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
395 # define _format_attribute(type, format_str, args_start) \
396 __attribute__((format(type, format_str, args_start)))
398 # define _format_attribute(type, format_str, args_start)
401 /* Print formatted error message to stderr. */
402 static void _format_attribute(printf, 1, 2)
403 imagex_error(const tchar *format, ...)
406 va_start(va, format);
407 tfputs(T("ERROR: "), stderr);
408 tvfprintf(stderr, format, va);
409 tputc(T('\n'), stderr);
413 /* Print formatted error message to stderr. */
414 static void _format_attribute(printf, 1, 2)
415 imagex_error_with_errno(const tchar *format, ...)
417 int errno_save = errno;
419 va_start(va, format);
420 tfputs(T("ERROR: "), stderr);
421 tvfprintf(stderr, format, va);
422 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
427 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
429 if (image == WIMLIB_NO_IMAGE) {
430 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
431 " Please specify a 1-based image index or "
432 "image name. To list the images\n"
433 " contained in the WIM archive, run\n"
435 " %"TS" \"%"TS"\"\n"),
436 image_name, wim_name,
437 get_cmd_string(CMD_INFO, false), wim_name);
438 return WIMLIB_ERR_INVALID_IMAGE;
444 verify_image_is_single(int image)
446 if (image == WIMLIB_ALL_IMAGES) {
447 imagex_error(T("Cannot specify all images for this action!"));
448 return WIMLIB_ERR_INVALID_IMAGE;
454 verify_image_exists_and_is_single(int image, const tchar *image_name,
455 const tchar *wim_name)
458 ret = verify_image_exists(image, image_name, wim_name);
460 ret = verify_image_is_single(image);
464 /* Parse the argument to --compress */
466 get_compression_type(const tchar *optarg)
468 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
469 return WIMLIB_COMPRESSION_TYPE_LZX;
470 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
471 return WIMLIB_COMPRESSION_TYPE_XPRESS;
472 else if (!tstrcasecmp(optarg, T("recovery")) || !tstrcasecmp(optarg, T("lzms")))
473 return WIMLIB_COMPRESSION_TYPE_LZMS;
474 else if (!tstrcasecmp(optarg, T("none")))
475 return WIMLIB_COMPRESSION_TYPE_NONE;
477 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
478 "\"maximum\", \"fast\", or \"none\"."), optarg);
479 return WIMLIB_COMPRESSION_TYPE_INVALID;
484 set_compress_slow(void)
486 static const struct wimlib_lzx_compressor_params lzx_slow_params = {
488 .size = sizeof(struct wimlib_lzx_compressor_params),
490 .algorithm = WIMLIB_LZX_ALGORITHM_SLOW,
493 .use_len2_matches = 1,
494 .nice_match_length = 96,
495 .num_optim_passes = 4,
496 .max_search_depth = 100,
497 .max_matches_per_pos = 10,
498 .main_nostat_cost = 15,
499 .len_nostat_cost = 15,
500 .aligned_nostat_cost = 7,
505 static const struct wimlib_lzms_compressor_params lzms_slow_params = {
507 .size = sizeof(struct wimlib_lzms_compressor_params),
509 .min_match_length = 2,
510 .max_match_length = UINT32_MAX,
511 .nice_match_length = 96,
512 .max_search_depth = 100,
513 .max_matches_per_pos = 10,
514 .optim_array_length = 1024,
517 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
518 &lzx_slow_params.hdr);
520 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZMS,
521 &lzms_slow_params.hdr);
525 const tchar **strings;
526 unsigned num_strings;
527 unsigned num_alloc_strings;
530 #define STRING_SET_INITIALIZER \
531 { .strings = NULL, .num_strings = 0, .num_alloc_strings = 0, }
533 #define STRING_SET(_strings) \
534 struct string_set _strings = STRING_SET_INITIALIZER
537 string_set_append(struct string_set *set, const tchar *glob)
539 unsigned num_alloc_strings = set->num_alloc_strings;
541 if (set->num_strings == num_alloc_strings) {
542 const tchar **new_strings;
544 num_alloc_strings += 4;
545 new_strings = realloc(set->strings,
546 sizeof(set->strings[0]) * num_alloc_strings);
548 imagex_error(T("Out of memory!"));
551 set->strings = new_strings;
552 set->num_alloc_strings = num_alloc_strings;
554 set->strings[set->num_strings++] = glob;
559 string_set_destroy(struct string_set *set)
565 wim_reference_globs(WIMStruct *wim, struct string_set *set, int open_flags)
567 return wimlib_reference_resource_files(wim, set->strings,
569 WIMLIB_REF_FLAG_GLOB_ENABLE,
571 imagex_progress_func);
575 do_resource_not_found_warning(const tchar *wimfile,
576 const struct wimlib_wim_info *info,
577 const struct string_set *refglobs)
579 if (info->total_parts > 1) {
580 if (refglobs->num_strings == 0) {
581 imagex_error(T("\"%"TS"\" is part of a split WIM. "
582 "Use --ref to specify the other parts."),
585 imagex_error(T("Perhaps the '--ref' argument did not "
586 "specify all other parts of the split "
590 imagex_error(T("If this is a delta WIM, use the --ref argument "
591 "to specify the WIM(s) on which it is based."));
595 /* Returns the size of a file given its name, or -1 if the file does not exist
596 * or its size cannot be determined. */
598 file_get_size(const tchar *filename)
601 if (tstat(filename, &st) == 0)
608 PARSE_STRING_SUCCESS = 0,
609 PARSE_STRING_FAILURE = 1,
610 PARSE_STRING_NONE = 2,
614 * Parses a string token from an array of characters.
616 * Tokens are either whitespace-delimited, or double or single-quoted.
618 * @line_p: Pointer to the pointer to the line of data. Will be updated
619 * to point past the string token iff the return value is
620 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
623 * @len_p: @len_p initially stores the length of the line of data, which may
624 * be 0, and it will be updated to the number of bytes remaining in
625 * the line iff the return value is PARSE_STRING_SUCCESS.
627 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
628 * parsed string token will be returned here.
630 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
631 * PARSE_STRING_FAILURE if the data was invalid due to a missing
632 * closing quote; or PARSE_STRING_NONE if the line ended before the
633 * beginning of a string token was found.
636 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
639 tchar *line = *line_p;
643 /* Skip leading whitespace */
646 return PARSE_STRING_NONE;
647 if (!istspace(*line) && *line != T('\0'))
653 if (quote_char == T('"') || quote_char == T('\'')) {
658 line = tmemchr(line, quote_char, len);
660 imagex_error(T("Missing closing quote: %"TS), fn - 1);
661 return PARSE_STRING_FAILURE;
664 /* Unquoted string. Go until whitespace. Line is terminated
665 * by '\0', so no need to check 'len'. */
669 } while (!istspace(*line) && *line != T('\0'));
676 return PARSE_STRING_SUCCESS;
679 /* Parses a line of data (not an empty line or comment) in the source list file
680 * format. (See the man page for 'wimlib-imagex capture' for details on this
681 * format and the meaning.)
683 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
684 * len == 0. The data in @line will be modified by this function call.
686 * @len: Length of the line of data.
688 * @source: On success, the capture source and target described by the line is
689 * written into this destination. Note that it will contain pointers
690 * to data in the @line array.
692 * Returns true if the line was valid; false otherwise. */
694 parse_source_list_line(tchar *line, size_t len,
695 struct wimlib_capture_source *source)
699 ret = parse_string(&line, &len, &source->fs_source_path);
700 if (ret != PARSE_STRING_SUCCESS)
702 ret = parse_string(&line, &len, &source->wim_target_path);
703 if (ret == PARSE_STRING_NONE)
704 source->wim_target_path = source->fs_source_path;
705 return ret != PARSE_STRING_FAILURE;
708 /* Returns %true if the given line of length @len > 0 is a comment or empty line
709 * in the source list file format. */
711 is_comment_line(const tchar *line, size_t len)
716 if (!istspace(*line) && *line != T('\0'))
726 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
729 tchar *contents = *contents_p;
730 size_t nchars = *nchars_p;
733 for (i = 0; i < nchars; i++)
734 if (contents[i] == T('\n'))
737 /* Handle last line not terminated by a newline */
738 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
739 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
741 imagex_error(T("Out of memory!"));
744 contents[nchars] = T('\n');
745 *contents_p = contents;
753 /* Parses a file in the source list format. (See the man page for
754 * 'wimlib-imagex capture' for details on this format and the meaning.)
756 * @source_list_contents: Contents of the source list file. Note that this
757 * buffer will be modified to save memory allocations,
758 * and cannot be freed until the returned array of
759 * wimlib_capture_source's has also been freed.
761 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
764 * @nsources_ret: On success, the length of the returned array is
767 * Returns: An array of `struct wimlib_capture_source's that can be passed to
768 * the wimlib_add_image_multisource() function to specify how a WIM image is to
770 static struct wimlib_capture_source *
771 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
772 size_t *nsources_ret)
776 struct wimlib_capture_source *sources;
779 nlines = text_file_count_lines(source_list_contents_p,
780 &source_list_nchars);
784 /* Always allocate at least 1 slot, just in case the implementation of
785 * calloc() returns NULL if 0 bytes are requested. */
786 sources = calloc(nlines ?: 1, sizeof(*sources));
788 imagex_error(T("out of memory"));
791 p = *source_list_contents_p;
793 for (i = 0; i < nlines; i++) {
794 /* XXX: Could use rawmemchr() here instead, but it may not be
795 * available on all platforms. */
796 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
797 size_t len = endp - p + 1;
799 if (!is_comment_line(p, len)) {
800 if (!parse_source_list_line(p, len, &sources[j++])) {
813 enum capture_config_section {
814 CAPTURE_CONFIG_NO_SECTION,
815 CAPTURE_CONFIG_EXCLUSION_SECTION,
816 CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
817 CAPTURE_CONFIG_IGNORE_SECTION,
821 CAPTURE_CONFIG_INVALID_SECTION,
822 CAPTURE_CONFIG_CHANGED_SECTION,
823 CAPTURE_CONFIG_SAME_SECTION,
827 check_config_section(tchar *line, size_t len,
828 enum capture_config_section *cur_section)
830 while (istspace(*line))
834 return CAPTURE_CONFIG_SAME_SECTION;
837 tchar *endbrace = tstrrchr(line, T(']'));
839 return CAPTURE_CONFIG_SAME_SECTION;
841 if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
842 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
843 } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
844 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
845 } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
846 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
847 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
848 "of capture config file\n"),
850 } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
851 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
852 tfputs(T("WARNING: Ignoring [AlignmentList] section "
853 "of capture config file\n"),
856 imagex_error(T("Invalid capture config file section \"%"TS"\""),
858 return CAPTURE_CONFIG_INVALID_SECTION;
860 return CAPTURE_CONFIG_CHANGED_SECTION;
865 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
868 if (pat_list->num_pats == pat_list->num_allocated_pats) {
870 size_t num_allocated_pats = pat_list->num_pats + 8;
872 pats = realloc(pat_list->pats,
873 num_allocated_pats * sizeof(pat_list->pats[0]));
875 imagex_error(T("Out of memory!"));
878 pat_list->pats = pats;
879 pat_list->num_allocated_pats = num_allocated_pats;
881 pat_list->pats[pat_list->num_pats++] = pat;
886 parse_capture_config_line(tchar *line, size_t len,
887 enum capture_config_section *cur_section,
888 struct wimlib_capture_config *config)
893 ret = check_config_section(line, len, cur_section);
894 if (ret == CAPTURE_CONFIG_INVALID_SECTION)
896 if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
899 switch (*cur_section) {
900 case CAPTURE_CONFIG_NO_SECTION:
901 imagex_error(T("Line \"%"TS"\" is not in a section "
902 "(such as [ExclusionList]"), line);
904 case CAPTURE_CONFIG_EXCLUSION_SECTION:
905 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
907 return pattern_list_add_pattern(&config->exclusion_pats,
909 case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
910 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
912 return pattern_list_add_pattern(&config->exclusion_exception_pats,
914 case CAPTURE_CONFIG_IGNORE_SECTION:
921 parse_capture_config(tchar **contents_p, size_t nchars,
922 struct wimlib_capture_config *config)
927 enum capture_config_section cur_section;
929 memset(config, 0, sizeof(*config));
931 nlines = text_file_count_lines(contents_p, &nchars);
935 cur_section = CAPTURE_CONFIG_NO_SECTION;
937 for (i = 0; i < nlines; i++) {
938 tchar *endp = tmemchr(p, T('\n'), nchars);
939 size_t len = endp - p + 1;
941 if (!is_comment_line(p, len))
942 if (!parse_capture_config_line(p, len, &cur_section, config))
950 /* Reads the contents of a file into memory. */
952 file_get_contents(const tchar *filename, size_t *len_ret)
959 if (tstat(filename, &stbuf) != 0) {
960 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
965 fp = tfopen(filename, T("rb"));
967 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
971 buf = malloc(len ? len : 1);
973 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
974 "contents of file \"%"TS"\""), len, filename);
977 if (fread(buf, 1, len, fp) != len) {
978 imagex_error_with_errno(T("Failed to read %zu bytes from the "
979 "file \"%"TS"\""), len, filename);
993 /* Read standard input until EOF and return the full contents in a malloc()ed
994 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
997 stdin_get_contents(size_t *len_ret)
999 /* stdin can, of course, be a pipe or other non-seekable file, so the
1000 * total length of the data cannot be pre-determined */
1002 size_t newlen = 1024;
1006 char *p = realloc(buf, newlen);
1007 size_t bytes_read, bytes_to_read;
1009 imagex_error(T("out of memory while reading stdin"));
1013 bytes_to_read = newlen - pos;
1014 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
1016 if (bytes_read != bytes_to_read) {
1021 imagex_error_with_errno(T("error reading stdin"));
1035 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
1038 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
1040 *num_tchars_ret = num_bytes;
1042 #else /* !__WIN32__ */
1043 /* On Windows, translate the text to UTF-16LE */
1047 if (num_bytes >= 2 &&
1048 (((unsigned char)text[0] == 0xff && (unsigned char)text[1] == 0xfe) ||
1049 ((unsigned char)text[0] <= 0x7f && (unsigned char)text[1] == 0x00)))
1051 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
1052 * with something that looks like an ASCII character encoded as
1053 * a UTF-16LE code unit. Assume the file is encoded as
1054 * UTF-16LE. This is not a 100% reliable check. */
1055 num_wchars = num_bytes / 2;
1056 text_wstr = (wchar_t*)text;
1058 /* File does not look like UTF-16LE. Assume it is encoded in
1059 * the current Windows code page. I think these are always
1060 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
1061 * should work as expected. */
1062 text_wstr = win32_mbs_to_wcs(text,
1067 *num_tchars_ret = num_wchars;
1069 #endif /* __WIN32__ */
1073 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
1078 contents = file_get_contents(filename, &num_bytes);
1081 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
1085 stdin_get_text_contents(size_t *num_tchars_ret)
1090 contents = stdin_get_contents(&num_bytes);
1093 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
1096 #define TO_PERCENT(numerator, denominator) \
1097 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
1099 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
1100 #define MEBIBYTE_MIN_NBYTES 10000000ULL
1101 #define KIBIBYTE_MIN_NBYTES 10000ULL
1104 get_unit(uint64_t total_bytes, const tchar **name_ret)
1106 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
1107 *name_ret = T("GiB");
1109 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
1110 *name_ret = T("MiB");
1112 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
1113 *name_ret = T("KiB");
1116 *name_ret = T("bytes");
1121 static struct wimlib_progress_info_scan last_scan_progress;
1124 report_scan_progress(const struct wimlib_progress_info_scan *scan, bool done)
1126 uint64_t prev_count, cur_count;
1128 prev_count = last_scan_progress.num_nondirs_scanned +
1129 last_scan_progress.num_dirs_scanned;
1130 cur_count = scan->num_nondirs_scanned + scan->num_dirs_scanned;
1132 if (done || prev_count == 0 || cur_count >= prev_count + 100 ||
1133 cur_count % 128 == 0)
1135 unsigned unit_shift;
1136 const tchar *unit_name;
1138 unit_shift = get_unit(scan->num_bytes_scanned, &unit_name);
1139 imagex_printf(T("\r%"PRIu64" %"TS" scanned (%"PRIu64" files, "
1140 "%"PRIu64" directories) "),
1141 scan->num_bytes_scanned >> unit_shift,
1143 scan->num_nondirs_scanned,
1144 scan->num_dirs_scanned);
1145 last_scan_progress = *scan;
1149 /* Progress callback function passed to various wimlib functions. */
1151 imagex_progress_func(enum wimlib_progress_msg msg,
1152 const union wimlib_progress_info *info)
1154 unsigned percent_done;
1155 unsigned unit_shift;
1156 const tchar *unit_name;
1158 if (imagex_be_quiet)
1161 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1163 static bool first = true;
1165 imagex_printf(T("Writing %"TS"-compressed data "
1166 "using %u thread%"TS"\n"),
1167 wimlib_get_compression_type_string(
1168 info->write_streams.compression_type),
1169 info->write_streams.num_threads,
1170 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1174 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1175 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1176 info->write_streams.total_bytes);
1178 if (info->write_streams.total_parts <= 1) {
1179 imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1180 "written (%u%% done)"),
1181 info->write_streams.completed_bytes >> unit_shift,
1183 info->write_streams.total_bytes >> unit_shift,
1187 imagex_printf(T("\rWriting resources from part %u of %u: "
1188 "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written"),
1189 (info->write_streams.completed_parts ==
1190 info->write_streams.total_parts) ?
1191 info->write_streams.completed_parts :
1192 info->write_streams.completed_parts + 1,
1193 info->write_streams.total_parts,
1194 info->write_streams.completed_bytes >> unit_shift,
1196 info->write_streams.total_bytes >> unit_shift,
1200 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1201 imagex_printf(T("\n"));
1203 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1204 imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
1205 if (*info->scan.wim_target_path) {
1206 imagex_printf(T(" (loading as WIM path: "
1207 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
1208 info->scan.wim_target_path);
1210 imagex_printf(T("\n"));
1212 memset(&last_scan_progress, 0, sizeof(last_scan_progress));
1214 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1215 switch (info->scan.status) {
1216 case WIMLIB_SCAN_DENTRY_OK:
1217 report_scan_progress(&info->scan, false);
1219 case WIMLIB_SCAN_DENTRY_EXCLUDED:
1220 imagex_printf(T("\nExcluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1222 case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
1223 imagex_printf(T("\nWARNING: Excluding unsupported file or directory\n"
1224 " \"%"TS"\" from capture\n"), info->scan.cur_path);
1226 case WIMLIB_SCAN_DENTRY_EXCLUDED_SYMLINK:
1227 imagex_printf(T("\nWARNING: Ignoring absolute symbolic link "
1228 "with out-of-tree target:\n"
1229 " \"%"TS"\" => \"%"TS"\"\n"
1230 " (Use --norpfix to capture "
1231 "absolute symbolic links as-is)\n"),
1232 info->scan.cur_path, info->scan.symlink_target);
1236 case WIMLIB_PROGRESS_MSG_SCAN_END:
1237 report_scan_progress(&info->scan, true);
1238 imagex_printf(T("\n"));
1240 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1241 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1242 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1243 info->integrity.total_bytes);
1244 imagex_printf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1245 "of %"PRIu64" %"TS" (%u%%) done"),
1246 info->integrity.filename,
1247 info->integrity.completed_bytes >> unit_shift,
1249 info->integrity.total_bytes >> unit_shift,
1252 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1253 imagex_printf(T("\n"));
1255 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1256 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1257 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1258 info->integrity.total_bytes);
1259 imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1260 "of %"PRIu64" %"TS" (%u%%) done"),
1261 info->integrity.completed_bytes >> unit_shift,
1263 info->integrity.total_bytes >> unit_shift,
1266 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1267 imagex_printf(T("\n"));
1269 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1270 imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1271 "to %"TS" \"%"TS"\"\n"),
1272 info->extract.image,
1273 info->extract.image_name,
1274 info->extract.wimfile_name,
1275 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1276 T("NTFS volume") : T("directory")),
1277 info->extract.target);
1279 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1280 if (info->extract.extract_root_wim_source_path[0]) {
1281 imagex_printf(T("Extracting \"%"TS"\" from image %d "
1282 "(\"%"TS"\") in \"%"TS"\" to \"%"TS"\"\n"),
1283 info->extract.extract_root_wim_source_path,
1284 info->extract.image,
1285 info->extract.image_name,
1286 info->extract.wimfile_name,
1287 info->extract.target);
1290 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1291 percent_done = TO_PERCENT(info->extract.completed_bytes,
1292 info->extract.total_bytes);
1293 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1294 imagex_printf(T("\rExtracting files: "
1295 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1296 info->extract.completed_bytes >> unit_shift,
1298 info->extract.total_bytes >> unit_shift,
1301 if (info->extract.completed_bytes >= info->extract.total_bytes)
1302 imagex_printf(T("\n"));
1304 case WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN:
1305 if (info->extract.total_parts != 1) {
1306 imagex_printf(T("\nReading split pipable WIM part %u of %u\n"),
1307 info->extract.part_number,
1308 info->extract.total_parts);
1311 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1312 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1313 imagex_printf(T("Setting timestamps on all extracted files...\n"));
1315 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1316 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1317 imagex_printf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1318 info->extract.target);
1321 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1322 percent_done = TO_PERCENT(info->split.completed_bytes,
1323 info->split.total_bytes);
1324 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1325 imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
1326 "%"PRIu64" %"TS" (%u%%) written\n"),
1327 info->split.part_name,
1328 info->split.cur_part_number,
1329 info->split.total_parts,
1330 info->split.completed_bytes >> unit_shift,
1332 info->split.total_bytes >> unit_shift,
1336 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1337 if (info->split.completed_bytes == info->split.total_bytes) {
1338 imagex_printf(T("Finished writing split WIM part %u of %u\n"),
1339 info->split.cur_part_number,
1340 info->split.total_parts);
1343 case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1344 switch (info->update.command->op) {
1345 case WIMLIB_UPDATE_OP_DELETE:
1346 imagex_printf(T("Deleted WIM path "
1347 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1348 info->update.command->delete_.wim_path);
1350 case WIMLIB_UPDATE_OP_RENAME:
1351 imagex_printf(T("Renamed WIM path "
1352 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
1353 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1354 info->update.command->rename.wim_source_path,
1355 info->update.command->rename.wim_target_path);
1357 case WIMLIB_UPDATE_OP_ADD:
1365 fflush(imagex_info_file);
1370 parse_num_threads(const tchar *optarg)
1373 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1374 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1375 imagex_error(T("Number of threads must be a non-negative integer!"));
1382 static uint32_t parse_chunk_size(const tchar *optarg)
1385 unsigned long chunk_size = tstrtoul(optarg, &tmp, 10);
1386 if (chunk_size >= UINT32_MAX || *tmp || tmp == optarg) {
1387 imagex_error(T("Chunk size must be a non-negative integer!"));
1396 * Parse an option passed to an update command.
1398 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1401 * @option: Text string for the option (beginning with --)
1403 * @cmd: `struct wimlib_update_command' that is being constructed for
1406 * Returns true if the option was recognized; false if not.
1409 update_command_add_option(int op, const tchar *option,
1410 struct wimlib_update_command *cmd)
1412 bool recognized = true;
1414 case WIMLIB_UPDATE_OP_ADD:
1415 if (!tstrcmp(option, T("--verbose")))
1416 cmd->add.add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
1417 else if (!tstrcmp(option, T("--unix-data")))
1418 cmd->add.add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
1419 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1420 cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
1421 else if (!tstrcmp(option, T("--strict-acls")))
1422 cmd->add.add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
1423 else if (!tstrcmp(option, T("--dereference")))
1424 cmd->add.add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
1428 case WIMLIB_UPDATE_OP_DELETE:
1429 if (!tstrcmp(option, T("--force")))
1430 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1431 else if (!tstrcmp(option, T("--recursive")))
1432 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1443 /* How many nonoption arguments each `imagex update' command expects */
1444 static const unsigned update_command_num_nonoptions[] = {
1445 [WIMLIB_UPDATE_OP_ADD] = 2,
1446 [WIMLIB_UPDATE_OP_DELETE] = 1,
1447 [WIMLIB_UPDATE_OP_RENAME] = 2,
1451 update_command_add_nonoption(int op, const tchar *nonoption,
1452 struct wimlib_update_command *cmd,
1453 unsigned num_nonoptions)
1456 case WIMLIB_UPDATE_OP_ADD:
1457 if (num_nonoptions == 0)
1458 cmd->add.fs_source_path = (tchar*)nonoption;
1460 cmd->add.wim_target_path = (tchar*)nonoption;
1462 case WIMLIB_UPDATE_OP_DELETE:
1463 cmd->delete_.wim_path = (tchar*)nonoption;
1465 case WIMLIB_UPDATE_OP_RENAME:
1466 if (num_nonoptions == 0)
1467 cmd->rename.wim_source_path = (tchar*)nonoption;
1469 cmd->rename.wim_target_path = (tchar*)nonoption;
1475 * Parse a command passed on stdin to `imagex update'.
1477 * @line: Text of the command.
1478 * @len: Length of the line, including a null terminator
1481 * @command: A `struct wimlib_update_command' to fill in from the parsed
1484 * @line_number: Line number of the command, for diagnostics.
1486 * Returns true on success; returns false on parse error.
1489 parse_update_command(tchar *line, size_t len,
1490 struct wimlib_update_command *command,
1494 tchar *command_name;
1496 size_t num_nonoptions;
1498 /* Get the command name ("add", "delete", "rename") */
1499 ret = parse_string(&line, &len, &command_name);
1500 if (ret != PARSE_STRING_SUCCESS)
1503 if (!tstrcasecmp(command_name, T("add"))) {
1504 op = WIMLIB_UPDATE_OP_ADD;
1505 } else if (!tstrcasecmp(command_name, T("delete"))) {
1506 op = WIMLIB_UPDATE_OP_DELETE;
1507 } else if (!tstrcasecmp(command_name, T("rename"))) {
1508 op = WIMLIB_UPDATE_OP_RENAME;
1510 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1511 command_name, line_number);
1516 /* Parse additional options and non-options as needed */
1521 ret = parse_string(&line, &len, &next_string);
1522 if (ret == PARSE_STRING_NONE) /* End of line */
1524 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1526 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1528 if (!update_command_add_option(op, next_string, command))
1530 imagex_error(T("Unrecognized option \"%"TS"\" to "
1531 "update command \"%"TS"\" on line %zu"),
1532 next_string, command_name, line_number);
1538 if (num_nonoptions == update_command_num_nonoptions[op])
1540 imagex_error(T("Unexpected argument \"%"TS"\" in "
1541 "update command on line %zu\n"
1542 " (The \"%"TS"\" command only "
1543 "takes %zu nonoption arguments!)\n"),
1544 next_string, line_number,
1545 command_name, num_nonoptions);
1548 update_command_add_nonoption(op, next_string,
1549 command, num_nonoptions);
1554 if (num_nonoptions != update_command_num_nonoptions[op]) {
1555 imagex_error(T("Not enough arguments to update command "
1556 "\"%"TS"\" on line %zu"), command_name, line_number);
1562 static struct wimlib_update_command *
1563 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1564 size_t *num_cmds_ret)
1568 struct wimlib_update_command *cmds;
1571 nlines = text_file_count_lines(cmd_file_contents_p,
1576 /* Always allocate at least 1 slot, just in case the implementation of
1577 * calloc() returns NULL if 0 bytes are requested. */
1578 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1580 imagex_error(T("out of memory"));
1583 p = *cmd_file_contents_p;
1585 for (i = 0; i < nlines; i++) {
1586 /* XXX: Could use rawmemchr() here instead, but it may not be
1587 * available on all platforms. */
1588 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1589 size_t len = endp - p + 1;
1591 if (!is_comment_line(p, len)) {
1592 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1603 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1604 * one image from a WIM file to a NTFS volume. */
1606 imagex_apply(int argc, tchar **argv, int cmd)
1610 int image = WIMLIB_NO_IMAGE;
1612 struct wimlib_wim_info info;
1614 const tchar *wimfile;
1615 const tchar *target;
1616 const tchar *image_num_or_name = NULL;
1617 int extract_flags = 0;
1619 STRING_SET(refglobs);
1621 for_opt(c, apply_options) {
1623 case IMAGEX_CHECK_OPTION:
1624 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1626 case IMAGEX_HARDLINK_OPTION:
1627 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1629 case IMAGEX_SYMLINK_OPTION:
1630 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1632 case IMAGEX_VERBOSE_OPTION:
1633 /* No longer does anything. */
1635 case IMAGEX_REF_OPTION:
1636 ret = string_set_append(&refglobs, optarg);
1638 goto out_free_refglobs;
1640 case IMAGEX_UNIX_DATA_OPTION:
1641 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1643 case IMAGEX_NO_ACLS_OPTION:
1644 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1646 case IMAGEX_STRICT_ACLS_OPTION:
1647 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1649 case IMAGEX_NO_ATTRIBUTES_OPTION:
1650 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES;
1652 case IMAGEX_NORPFIX_OPTION:
1653 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1655 case IMAGEX_RPFIX_OPTION:
1656 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1658 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1659 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1660 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1662 case IMAGEX_RESUME_OPTION:
1663 extract_flags |= WIMLIB_EXTRACT_FLAG_RESUME;
1671 if (argc != 2 && argc != 3)
1676 if (!tstrcmp(wimfile, T("-"))) {
1677 /* Attempt to apply pipable WIM from standard input. */
1679 image_num_or_name = NULL;
1682 image_num_or_name = argv[1];
1687 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1688 imagex_progress_func);
1690 goto out_free_refglobs;
1692 wimlib_get_wim_info(wim, &info);
1695 /* Image explicitly specified. */
1696 image_num_or_name = argv[1];
1697 image = wimlib_resolve_image(wim, image_num_or_name);
1698 ret = verify_image_exists(image, image_num_or_name, wimfile);
1700 goto out_wimlib_free;
1703 /* No image specified; default to image 1, but only if the WIM
1704 * contains exactly one image. */
1706 if (info.image_count != 1) {
1707 imagex_error(T("\"%"TS"\" contains %d images; "
1708 "Please select one (or all)."),
1709 wimfile, info.image_count);
1718 if (refglobs.num_strings) {
1720 imagex_error(T("Can't specify --ref when applying from stdin!"));
1722 goto out_wimlib_free;
1724 ret = wim_reference_globs(wim, &refglobs, open_flags);
1726 goto out_wimlib_free;
1731 /* Interpret a regular file or block device target as a NTFS
1735 if (tstat(target, &stbuf)) {
1736 if (errno != ENOENT) {
1737 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1740 goto out_wimlib_free;
1743 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1744 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1750 ret = wimlib_extract_image(wim, image, target, extract_flags,
1751 imagex_progress_func);
1753 set_fd_to_binary_mode(STDIN_FILENO);
1754 ret = wimlib_extract_image_from_pipe(STDIN_FILENO,
1756 target, extract_flags,
1757 imagex_progress_func);
1760 imagex_printf(T("Done applying WIM image.\n"));
1761 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
1763 do_resource_not_found_warning(wimfile, &info, &refglobs);
1765 imagex_error(T( "If you are applying an image "
1766 "from a split pipable WIM,\n"
1767 " make sure you have "
1768 "concatenated together all parts."));
1774 string_set_destroy(&refglobs);
1778 usage(CMD_APPLY, stderr);
1780 goto out_free_refglobs;
1783 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1784 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1785 * the desired image. 'wimlib-imagex append': add a new image to an existing
1788 imagex_capture_or_append(int argc, tchar **argv, int cmd)
1791 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1792 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE |
1793 WIMLIB_ADD_IMAGE_FLAG_WINCONFIG |
1794 WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1795 int write_flags = 0;
1796 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
1797 uint32_t chunk_size = UINT32_MAX;
1798 uint32_t pack_chunk_size = UINT32_MAX;
1799 const tchar *wimfile;
1803 const tchar *flags_element = NULL;
1806 STRING_SET(base_wimfiles);
1807 WIMStruct **base_wims;
1809 WIMStruct *template_wim;
1810 const tchar *template_wimfile = NULL;
1811 const tchar *template_image_name_or_num = NULL;
1812 int template_image = WIMLIB_NO_IMAGE;
1815 unsigned num_threads = 0;
1820 const tchar *config_file = NULL;
1822 struct wimlib_capture_config *config;
1824 bool source_list = false;
1825 size_t source_list_nchars = 0;
1826 tchar *source_list_contents;
1827 bool capture_sources_malloced;
1828 struct wimlib_capture_source *capture_sources;
1830 bool name_defaulted;
1831 bool compress_slow = false;
1833 for_opt(c, capture_or_append_options) {
1835 case IMAGEX_BOOT_OPTION:
1836 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1838 case IMAGEX_CHECK_OPTION:
1839 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1840 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1842 case IMAGEX_NOCHECK_OPTION:
1843 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
1845 case IMAGEX_CONFIG_OPTION:
1846 config_file = optarg;
1847 add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1849 case IMAGEX_COMPRESS_OPTION:
1850 compression_type = get_compression_type(optarg);
1851 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1854 case IMAGEX_COMPRESS_SLOW_OPTION:
1855 compress_slow = true;
1857 case IMAGEX_CHUNK_SIZE_OPTION:
1858 chunk_size = parse_chunk_size(optarg);
1859 if (chunk_size == UINT32_MAX)
1862 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
1863 pack_chunk_size = parse_chunk_size(optarg);
1864 if (pack_chunk_size == UINT32_MAX)
1867 case IMAGEX_PACK_STREAMS_OPTION:
1868 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
1870 case IMAGEX_FLAGS_OPTION:
1871 flags_element = optarg;
1873 case IMAGEX_DEREFERENCE_OPTION:
1874 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1876 case IMAGEX_VERBOSE_OPTION:
1877 /* No longer does anything. */
1879 case IMAGEX_THREADS_OPTION:
1880 num_threads = parse_num_threads(optarg);
1881 if (num_threads == UINT_MAX)
1884 case IMAGEX_REBUILD_OPTION:
1885 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1887 case IMAGEX_UNIX_DATA_OPTION:
1888 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1890 case IMAGEX_SOURCE_LIST_OPTION:
1893 case IMAGEX_NO_ACLS_OPTION:
1894 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1896 case IMAGEX_STRICT_ACLS_OPTION:
1897 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1899 case IMAGEX_RPFIX_OPTION:
1900 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1902 case IMAGEX_NORPFIX_OPTION:
1903 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1905 case IMAGEX_PIPABLE_OPTION:
1906 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1908 case IMAGEX_NOT_PIPABLE_OPTION:
1909 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
1911 case IMAGEX_UPDATE_OF_OPTION:
1912 if (template_image_name_or_num) {
1913 imagex_error(T("'--update-of' can only be "
1914 "specified one time!"));
1918 colon = tstrrchr(optarg, T(':'));
1921 template_wimfile = optarg;
1923 template_image_name_or_num = colon + 1;
1925 template_wimfile = NULL;
1926 template_image_name_or_num = optarg;
1930 case IMAGEX_DELTA_FROM_OPTION:
1931 if (cmd != CMD_CAPTURE) {
1932 imagex_error(T("'--delta-from' is only "
1933 "valid for capture!"));
1936 ret = string_set_append(&base_wimfiles, optarg);
1938 goto out_free_base_wimfiles;
1939 write_flags |= WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS;
1948 if (argc < 2 || argc > 4)
1954 /* Set default compression type and parameters. */
1957 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
1958 compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1960 if (!compress_slow) {
1961 struct wimlib_lzx_compressor_params params = {
1962 .hdr.size = sizeof(params),
1963 .algorithm = WIMLIB_LZX_ALGORITHM_FAST,
1966 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
1972 set_compress_slow();
1974 if (!tstrcmp(wimfile, T("-"))) {
1975 /* Writing captured WIM to standard output. */
1977 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
1978 imagex_error("Can't write a non-pipable WIM to "
1979 "standard output! Specify --pipable\n"
1980 " if you want to create a pipable WIM "
1981 "(but read the docs first).");
1985 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1987 if (cmd == CMD_APPEND) {
1988 imagex_error(T("Using standard output for append does "
1989 "not make sense."));
1992 wim_fd = STDOUT_FILENO;
1994 imagex_info_file = stderr;
1995 set_fd_to_binary_mode(wim_fd);
1998 /* If template image was specified using --update-of=IMAGE rather
1999 * than --update-of=WIMFILE:IMAGE, set the default WIMFILE. */
2000 if (template_image_name_or_num && !template_wimfile) {
2001 if (base_wimfiles.num_strings == 1) {
2002 /* Capturing delta WIM based on single WIM: default to
2004 template_wimfile = base_wimfiles.strings[0];
2005 } else if (cmd == CMD_APPEND) {
2006 /* Appending to WIM: default to WIM being appended to.
2008 template_wimfile = wimfile;
2010 /* Capturing a normal (non-delta) WIM, so the WIM file
2011 * *must* be explicitly specified. */
2012 if (base_wimfiles.num_strings > 1) {
2013 imagex_error(T("For capture of delta WIM "
2014 "based on multiple existing "
2016 " '--update-of' must "
2017 "specify WIMFILE:IMAGE!"));
2019 imagex_error(T("For capture of non-delta WIM, "
2020 "'--update-of' must specify "
2029 name_defaulted = false;
2031 /* Set default name to SOURCE argument, omitting any directory
2032 * prefixes and trailing slashes. This requires making a copy
2033 * of @source. Leave some free characters at the end in case we
2034 * append a number to keep the name unique. */
2035 size_t source_name_len;
2037 source_name_len = tstrlen(source);
2038 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
2039 name = tbasename(tstrcpy(source_copy, source));
2040 name_defaulted = true;
2042 /* Image description defaults to NULL if not given. */
2049 /* Set up capture sources in source list mode */
2050 if (source[0] == T('-') && source[1] == T('\0')) {
2051 source_list_contents = stdin_get_text_contents(&source_list_nchars);
2053 source_list_contents = file_get_text_contents(source,
2054 &source_list_nchars);
2056 if (!source_list_contents)
2059 capture_sources = parse_source_list(&source_list_contents,
2062 if (!capture_sources) {
2064 goto out_free_source_list_contents;
2066 capture_sources_malloced = true;
2068 /* Set up capture source in non-source-list mode. */
2069 capture_sources = alloca(sizeof(struct wimlib_capture_source));
2070 capture_sources[0].fs_source_path = source;
2071 capture_sources[0].wim_target_path = NULL;
2072 capture_sources[0].reserved = 0;
2074 capture_sources_malloced = false;
2075 source_list_contents = NULL;
2079 /* Read and parse capture configuration file. */
2082 config_str = file_get_text_contents(config_file, &config_len);
2085 goto out_free_capture_sources;
2088 config = alloca(sizeof(*config));
2089 ret = parse_capture_config(&config_str, config_len, config);
2091 goto out_free_config;
2093 /* No capture configuration file specified; use default
2094 * configuration for capturing Windows operating systems. */
2096 add_image_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
2099 /* Open the existing WIM, or create a new one. */
2100 if (cmd == CMD_APPEND)
2101 ret = wimlib_open_wim(wimfile, open_flags, &wim,
2102 imagex_progress_func);
2104 ret = wimlib_create_new_wim(compression_type, &wim);
2106 goto out_free_config;
2108 /* Set chunk size if non-default. */
2109 if (chunk_size != UINT32_MAX) {
2110 ret = wimlib_set_output_chunk_size(wim, chunk_size);
2114 if (pack_chunk_size != UINT32_MAX) {
2115 ret = wimlib_set_output_pack_chunk_size(wim, pack_chunk_size);
2121 /* Detect if source is regular file or block device and set NTFS volume
2126 if (tstat(source, &stbuf) == 0) {
2127 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
2128 imagex_printf(T("Capturing WIM image from NTFS "
2129 "filesystem on \"%"TS"\"\n"), source);
2130 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
2133 if (errno != ENOENT) {
2134 imagex_error_with_errno(T("Failed to stat "
2135 "\"%"TS"\""), source);
2143 /* If the user did not specify an image name, and the basename of the
2144 * source already exists as an image name in the WIM file, append a
2145 * suffix to make it unique. */
2146 if (cmd == CMD_APPEND && name_defaulted) {
2147 unsigned long conflict_idx;
2148 tchar *name_end = tstrchr(name, T('\0'));
2149 for (conflict_idx = 1;
2150 wimlib_image_name_in_use(wim, name);
2153 tsprintf(name_end, T(" (%lu)"), conflict_idx);
2157 /* If capturing a delta WIM, reference resources from the base WIMs
2158 * before adding the new image. */
2159 if (base_wimfiles.num_strings) {
2160 base_wims = calloc(base_wimfiles.num_strings,
2161 sizeof(base_wims[0]));
2162 if (base_wims == NULL) {
2163 imagex_error(T("Out of memory!"));
2168 for (size_t i = 0; i < base_wimfiles.num_strings; i++) {
2169 ret = wimlib_open_wim(base_wimfiles.strings[i],
2170 open_flags, &base_wims[i],
2171 imagex_progress_func);
2173 goto out_free_base_wims;
2177 ret = wimlib_reference_resources(wim, base_wims,
2178 base_wimfiles.num_strings, 0);
2180 goto out_free_base_wims;
2182 if (base_wimfiles.num_strings == 1) {
2183 imagex_printf(T("Capturing delta WIM based on \"%"TS"\"\n"),
2184 base_wimfiles.strings[0]);
2186 imagex_printf(T("Capturing delta WIM based on %u WIMs\n"),
2187 base_wimfiles.num_strings);
2194 /* If capturing or appending as an update of an existing (template) image,
2195 * open the WIM if needed and parse the image index. */
2196 if (template_image_name_or_num) {
2199 if (base_wimfiles.num_strings == 1 &&
2200 template_wimfile == base_wimfiles.strings[0]) {
2201 template_wim = base_wims[0];
2202 } else if (template_wimfile == wimfile) {
2205 ret = wimlib_open_wim(template_wimfile, open_flags,
2206 &template_wim, imagex_progress_func);
2208 goto out_free_base_wims;
2211 template_image = wimlib_resolve_image(template_wim,
2212 template_image_name_or_num);
2214 if (template_image_name_or_num[0] == T('-')) {
2217 struct wimlib_wim_info info;
2219 wimlib_get_wim_info(template_wim, &info);
2220 n = tstrtoul(template_image_name_or_num + 1, &tmp, 10);
2221 if (n >= 1 && n <= info.image_count &&
2223 tmp != template_image_name_or_num + 1)
2225 template_image = info.image_count - (n - 1);
2228 ret = verify_image_exists_and_is_single(template_image,
2229 template_image_name_or_num,
2232 goto out_free_template_wim;
2234 template_wim = NULL;
2237 ret = wimlib_add_image_multisource(wim,
2243 imagex_progress_func);
2245 goto out_free_template_wim;
2247 if (desc || flags_element || template_image_name_or_num) {
2248 /* User provided <DESCRIPTION> or <FLAGS> element, or an image
2249 * on which the added one is to be based has been specified with
2250 * --update-of. Get the index of the image we just
2251 * added, then use it to call the appropriate functions. */
2252 struct wimlib_wim_info info;
2254 wimlib_get_wim_info(wim, &info);
2257 ret = wimlib_set_image_descripton(wim,
2261 goto out_free_template_wim;
2264 if (flags_element) {
2265 ret = wimlib_set_image_flags(wim, info.image_count,
2268 goto out_free_template_wim;
2271 /* Reference template image if the user provided one. */
2272 if (template_image_name_or_num) {
2273 imagex_printf(T("Using image %d "
2274 "from \"%"TS"\" as template\n"),
2275 template_image, template_wimfile);
2276 ret = wimlib_reference_template_image(wim,
2282 goto out_free_template_wim;
2286 /* Write the new WIM or overwrite the existing WIM with the new image
2288 if (cmd == CMD_APPEND) {
2289 ret = wimlib_overwrite(wim, write_flags, num_threads,
2290 imagex_progress_func);
2291 } else if (wimfile) {
2292 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
2293 write_flags, num_threads,
2294 imagex_progress_func);
2296 ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
2297 write_flags, num_threads,
2298 imagex_progress_func);
2300 out_free_template_wim:
2301 /* template_wim may alias base_wims[0] or wim. */
2302 if ((base_wimfiles.num_strings != 1 || template_wim != base_wims[0]) &&
2303 template_wim != wim)
2304 wimlib_free(template_wim);
2306 for (size_t i = 0; i < base_wimfiles.num_strings; i++)
2307 wimlib_free(base_wims[i]);
2313 free(config->exclusion_pats.pats);
2314 free(config->exclusion_exception_pats.pats);
2317 out_free_capture_sources:
2318 if (capture_sources_malloced)
2319 free(capture_sources);
2320 out_free_source_list_contents:
2321 free(source_list_contents);
2322 out_free_base_wimfiles:
2323 string_set_destroy(&base_wimfiles);
2330 goto out_free_base_wimfiles;
2333 /* Remove image(s) from a WIM. */
2335 imagex_delete(int argc, tchar **argv, int cmd)
2338 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2339 int write_flags = 0;
2340 const tchar *wimfile;
2341 const tchar *image_num_or_name;
2346 for_opt(c, delete_options) {
2348 case IMAGEX_CHECK_OPTION:
2349 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2350 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2352 case IMAGEX_SOFT_OPTION:
2353 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
2364 imagex_error(T("Must specify a WIM file"));
2366 imagex_error(T("Must specify an image"));
2370 image_num_or_name = argv[1];
2372 ret = wimlib_open_wim(wimfile, open_flags, &wim,
2373 imagex_progress_func);
2377 image = wimlib_resolve_image(wim, image_num_or_name);
2379 ret = verify_image_exists(image, image_num_or_name, wimfile);
2381 goto out_wimlib_free;
2383 ret = wimlib_delete_image(wim, image);
2385 imagex_error(T("Failed to delete image from \"%"TS"\""),
2387 goto out_wimlib_free;
2390 ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
2392 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2393 "deleted"), wimfile);
2401 usage(CMD_DELETE, stderr);
2406 struct print_dentry_options {
2411 print_dentry_full_path(const struct wimlib_dir_entry *dentry)
2413 tprintf(T("%"TS"\n"), dentry->full_path);
2416 static const struct {
2419 } file_attr_flags[] = {
2420 {WIMLIB_FILE_ATTRIBUTE_READONLY, T("READONLY")},
2421 {WIMLIB_FILE_ATTRIBUTE_HIDDEN, T("HIDDEN")},
2422 {WIMLIB_FILE_ATTRIBUTE_SYSTEM, T("SYSTEM")},
2423 {WIMLIB_FILE_ATTRIBUTE_DIRECTORY, T("DIRECTORY")},
2424 {WIMLIB_FILE_ATTRIBUTE_ARCHIVE, T("ARCHIVE")},
2425 {WIMLIB_FILE_ATTRIBUTE_DEVICE, T("DEVICE")},
2426 {WIMLIB_FILE_ATTRIBUTE_NORMAL, T("NORMAL")},
2427 {WIMLIB_FILE_ATTRIBUTE_TEMPORARY, T("TEMPORARY")},
2428 {WIMLIB_FILE_ATTRIBUTE_SPARSE_FILE, T("SPARSE_FILE")},
2429 {WIMLIB_FILE_ATTRIBUTE_REPARSE_POINT, T("REPARSE_POINT")},
2430 {WIMLIB_FILE_ATTRIBUTE_COMPRESSED, T("COMPRESSED")},
2431 {WIMLIB_FILE_ATTRIBUTE_OFFLINE, T("OFFLINE")},
2432 {WIMLIB_FILE_ATTRIBUTE_NOT_CONTENT_INDEXED, T("NOT_CONTENT_INDEXED")},
2433 {WIMLIB_FILE_ATTRIBUTE_ENCRYPTED, T("ENCRYPTED")},
2434 {WIMLIB_FILE_ATTRIBUTE_VIRTUAL, T("VIRTUAL")},
2437 #define TIMESTR_MAX 100
2440 timespec_to_string(const struct timespec *spec, tchar *buf)
2442 time_t t = spec->tv_sec;
2445 tstrftime(buf, TIMESTR_MAX, T("%a %b %d %H:%M:%S %Y UTC"), &tm);
2446 buf[TIMESTR_MAX - 1] = '\0';
2450 print_time(const tchar *type, const struct timespec *spec)
2452 tchar timestr[TIMESTR_MAX];
2454 timespec_to_string(spec, timestr);
2456 tprintf(T("%-20"TS"= %"TS"\n"), type, timestr);
2459 static void print_byte_field(const uint8_t field[], size_t len)
2462 tprintf(T("%02hhx"), *field++);
2466 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2468 tputs(T("WIM Information:"));
2469 tputs(T("----------------"));
2470 tprintf(T("Path: %"TS"\n"), wimfile);
2471 tprintf(T("GUID: 0x"));
2472 print_byte_field(info->guid, sizeof(info->guid));
2474 tprintf(T("Version: %u\n"), info->wim_version);
2475 tprintf(T("Image Count: %d\n"), info->image_count);
2476 tprintf(T("Compression: %"TS"\n"),
2477 wimlib_get_compression_type_string(info->compression_type));
2478 tprintf(T("Chunk Size: %"PRIu32" bytes\n"),
2480 tprintf(T("Part Number: %d/%d\n"), info->part_number, info->total_parts);
2481 tprintf(T("Boot Index: %d\n"), info->boot_index);
2482 tprintf(T("Size: %"PRIu64" bytes\n"), info->total_bytes);
2483 tprintf(T("Integrity Info: %"TS"\n"),
2484 info->has_integrity_table ? T("yes") : T("no"));
2485 tprintf(T("Relative path junction: %"TS"\n"),
2486 info->has_rpfix ? T("yes") : T("no"));
2487 tprintf(T("Pipable: %"TS"\n"),
2488 info->pipable ? T("yes") : T("no"));
2493 print_resource(const struct wimlib_resource_entry *resource,
2496 tprintf(T("Hash = 0x"));
2497 print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2500 if (!resource->is_missing) {
2501 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
2502 resource->uncompressed_size);
2503 if (resource->packed) {
2504 tprintf(T("Raw compressed size = %"PRIu64" bytes\n"),
2505 resource->raw_resource_compressed_size);
2507 tprintf(T("Raw offset in WIM = %"PRIu64" bytes\n"),
2508 resource->raw_resource_offset_in_wim);
2510 tprintf(T("Offset in raw = %"PRIu64" bytes\n"),
2513 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
2514 resource->compressed_size);
2516 tprintf(T("Offset in WIM = %"PRIu64" bytes\n"),
2520 tprintf(T("Part Number = %u\n"), resource->part_number);
2521 tprintf(T("Reference Count = %u\n"), resource->reference_count);
2523 tprintf(T("Flags = "));
2524 if (resource->is_compressed)
2525 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED "));
2526 if (resource->is_metadata)
2527 tprintf(T("WIM_RESHDR_FLAG_METADATA "));
2528 if (resource->is_free)
2529 tprintf(T("WIM_RESHDR_FLAG_FREE "));
2530 if (resource->is_spanned)
2531 tprintf(T("WIM_RESHDR_FLAG_SPANNED "));
2532 if (resource->packed)
2533 tprintf(T("WIM_RESHDR_FLAG_PACKED_STREAMS "));
2541 print_lookup_table(WIMStruct *wim)
2543 wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2547 default_print_security_descriptor(const uint8_t *sd, size_t size)
2549 tprintf(T("Security Descriptor = "));
2550 print_byte_field(sd, size);
2555 print_dentry_detailed(const struct wimlib_dir_entry *dentry)
2559 "----------------------------------------------------------------------------\n"));
2560 tprintf(T("Full Path = \"%"TS"\"\n"), dentry->full_path);
2561 if (dentry->dos_name)
2562 tprintf(T("Short Name = \"%"TS"\"\n"), dentry->dos_name);
2563 tprintf(T("Attributes = 0x%08x\n"), dentry->attributes);
2564 for (size_t i = 0; i < ARRAY_LEN(file_attr_flags); i++)
2565 if (file_attr_flags[i].flag & dentry->attributes)
2566 tprintf(T(" FILE_ATTRIBUTE_%"TS" is set\n"),
2567 file_attr_flags[i].name);
2569 if (dentry->security_descriptor) {
2570 print_security_descriptor(dentry->security_descriptor,
2571 dentry->security_descriptor_size);
2574 print_time(T("Creation Time"), &dentry->creation_time);
2575 print_time(T("Last Write Time"), &dentry->last_write_time);
2576 print_time(T("Last Access Time"), &dentry->last_access_time);
2579 if (dentry->attributes & WIMLIB_FILE_ATTRIBUTE_REPARSE_POINT)
2580 tprintf(T("Reparse Tag = 0x%"PRIx32"\n"), dentry->reparse_tag);
2582 tprintf(T("Link Group ID = 0x%016"PRIx64"\n"), dentry->hard_link_group_id);
2583 tprintf(T("Link Count = %"PRIu32"\n"), dentry->num_links);
2585 for (uint32_t i = 0; i <= dentry->num_named_streams; i++) {
2586 if (dentry->streams[i].stream_name) {
2587 tprintf(T("\tData stream \"%"TS"\":\n"),
2588 dentry->streams[i].stream_name);
2590 tprintf(T("\tUnnamed data stream:\n"));
2592 print_resource(&dentry->streams[i].resource, NULL);
2597 print_dentry(const struct wimlib_dir_entry *dentry, void *_options)
2599 const struct print_dentry_options *options = _options;
2600 if (!options->detailed)
2601 print_dentry_full_path(dentry);
2603 print_dentry_detailed(dentry);
2607 /* Print the files contained in an image(s) in a WIM file. */
2609 imagex_dir(int argc, tchar **argv, int cmd)
2611 const tchar *wimfile;
2612 WIMStruct *wim = NULL;
2615 const tchar *path = T("");
2617 struct print_dentry_options options = {
2620 int iterate_flags = WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE;
2622 for_opt(c, dir_options) {
2624 case IMAGEX_PATH_OPTION:
2627 case IMAGEX_DETAILED_OPTION:
2628 options.detailed = true;
2630 case IMAGEX_ONE_FILE_ONLY_OPTION:
2631 iterate_flags &= ~WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE;
2641 imagex_error(T("Must specify a WIM file"));
2645 imagex_error(T("Too many arguments"));
2650 ret = wimlib_open_wim(wimfile, 0, &wim, imagex_progress_func);
2655 image = wimlib_resolve_image(wim, argv[1]);
2656 ret = verify_image_exists(image, argv[1], wimfile);
2658 goto out_wimlib_free;
2660 /* No image specified; default to image 1, but only if the WIM
2661 * contains exactly one image. */
2663 struct wimlib_wim_info info;
2665 wimlib_get_wim_info(wim, &info);
2666 if (info.image_count != 1) {
2667 imagex_error(T("\"%"TS"\" contains %d images; Please "
2668 "select one (or all)."),
2669 wimfile, info.image_count);
2676 ret = wimlib_iterate_dir_tree(wim, image, path, iterate_flags,
2677 print_dentry, &options);
2684 usage(CMD_DIR, stderr);
2689 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2692 imagex_export(int argc, tchar **argv, int cmd)
2696 int export_flags = 0;
2697 int write_flags = 0;
2698 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2699 const tchar *src_wimfile;
2700 const tchar *src_image_num_or_name;
2701 const tchar *dest_wimfile;
2703 const tchar *dest_name;
2704 const tchar *dest_desc;
2706 struct wimlib_wim_info src_info;
2707 WIMStruct *dest_wim;
2712 STRING_SET(refglobs);
2713 unsigned num_threads = 0;
2714 uint32_t chunk_size = UINT32_MAX;
2715 uint32_t pack_chunk_size = UINT32_MAX;
2717 for_opt(c, export_options) {
2719 case IMAGEX_BOOT_OPTION:
2720 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2722 case IMAGEX_CHECK_OPTION:
2723 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2724 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2726 case IMAGEX_NOCHECK_OPTION:
2727 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2729 case IMAGEX_COMPRESS_OPTION:
2730 compression_type = get_compression_type(optarg);
2731 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2734 case IMAGEX_PACK_STREAMS_OPTION:
2735 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
2737 case IMAGEX_CHUNK_SIZE_OPTION:
2738 chunk_size = parse_chunk_size(optarg);
2739 if (chunk_size == UINT32_MAX)
2742 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
2743 pack_chunk_size = parse_chunk_size(optarg);
2744 if (pack_chunk_size == UINT32_MAX)
2747 case IMAGEX_REF_OPTION:
2748 ret = string_set_append(&refglobs, optarg);
2750 goto out_free_refglobs;
2752 case IMAGEX_THREADS_OPTION:
2753 num_threads = parse_num_threads(optarg);
2754 if (num_threads == UINT_MAX)
2757 case IMAGEX_REBUILD_OPTION:
2758 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2760 case IMAGEX_PIPABLE_OPTION:
2761 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2763 case IMAGEX_NOT_PIPABLE_OPTION:
2764 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
2772 if (argc < 3 || argc > 5)
2774 src_wimfile = argv[0];
2775 src_image_num_or_name = argv[1];
2776 dest_wimfile = argv[2];
2777 dest_name = (argc >= 4) ? argv[3] : NULL;
2778 dest_desc = (argc >= 5) ? argv[4] : NULL;
2779 ret = wimlib_open_wim(src_wimfile, open_flags, &src_wim,
2780 imagex_progress_func);
2782 goto out_free_refglobs;
2784 wimlib_get_wim_info(src_wim, &src_info);
2786 /* Determine if the destination is an existing file or not. If so, we
2787 * try to append the exported image(s) to it; otherwise, we create a new
2788 * WIM containing the exported image(s). Furthermore, determine if we
2789 * need to write a pipable WIM directly to standard output. */
2791 if (tstrcmp(dest_wimfile, T("-")) == 0) {
2793 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2794 imagex_error("Can't write a non-pipable WIM to "
2795 "standard output! Specify --pipable\n"
2796 " if you want to create a pipable WIM "
2797 "(but read the docs first).");
2799 goto out_free_src_wim;
2802 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2804 dest_wimfile = NULL;
2805 dest_wim_fd = STDOUT_FILENO;
2806 imagex_info_file = stderr;
2807 set_fd_to_binary_mode(dest_wim_fd);
2810 if (dest_wimfile != NULL && tstat(dest_wimfile, &stbuf) == 0) {
2812 /* Destination file exists. */
2814 if (!S_ISREG(stbuf.st_mode)) {
2815 imagex_error(T("\"%"TS"\" is not a regular file"),
2818 goto out_free_src_wim;
2820 ret = wimlib_open_wim(dest_wimfile,
2821 open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2822 &dest_wim, imagex_progress_func);
2824 goto out_free_src_wim;
2826 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2827 /* The user specified a compression type, but we're
2828 * exporting to an existing WIM. Make sure the
2829 * specified compression type is the same as the
2830 * compression type of the existing destination WIM. */
2831 struct wimlib_wim_info dest_info;
2833 wimlib_get_wim_info(dest_wim, &dest_info);
2834 if (compression_type != dest_info.compression_type) {
2835 imagex_error(T("Cannot specify a compression type that is "
2836 "not the same as that used in the "
2837 "destination WIM"));
2839 goto out_free_dest_wim;
2845 if (errno != ENOENT) {
2846 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2849 goto out_free_src_wim;
2852 /* dest_wimfile is not an existing file, so create a new WIM. */
2854 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2855 /* The user did not specify a compression type; default
2856 * to that of the source WIM. */
2858 compression_type = src_info.compression_type;
2860 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2862 goto out_free_src_wim;
2864 /* Use same chunk size if compression type is the same. */
2865 if (compression_type == src_info.compression_type &&
2866 chunk_size == UINT32_MAX)
2867 wimlib_set_output_chunk_size(dest_wim, src_info.chunk_size);
2870 if (chunk_size != UINT32_MAX) {
2871 /* Set destination chunk size. */
2872 ret = wimlib_set_output_chunk_size(dest_wim, chunk_size);
2874 goto out_free_dest_wim;
2876 if (pack_chunk_size != UINT32_MAX) {
2877 ret = wimlib_set_output_pack_chunk_size(dest_wim, pack_chunk_size);
2879 goto out_free_dest_wim;
2882 image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2883 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2885 goto out_free_dest_wim;
2887 if (refglobs.num_strings) {
2888 ret = wim_reference_globs(src_wim, &refglobs, open_flags);
2890 goto out_free_dest_wim;
2893 if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
2894 image == WIMLIB_ALL_IMAGES && src_info.boot_index == 0)
2896 imagex_error(T("--boot specified for all-images export, but source WIM "
2897 "has no bootable image."));
2899 goto out_free_dest_wim;
2902 ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2903 dest_desc, export_flags, imagex_progress_func);
2905 if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2906 do_resource_not_found_warning(src_wimfile,
2907 &src_info, &refglobs);
2909 goto out_free_dest_wim;
2913 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2914 imagex_progress_func);
2915 else if (dest_wimfile)
2916 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2917 write_flags, num_threads,
2918 imagex_progress_func);
2920 ret = wimlib_write_to_fd(dest_wim, dest_wim_fd,
2921 WIMLIB_ALL_IMAGES, write_flags,
2922 num_threads, imagex_progress_func);
2924 wimlib_free(dest_wim);
2926 wimlib_free(src_wim);
2928 string_set_destroy(&refglobs);
2932 usage(CMD_EXPORT, stderr);
2935 goto out_free_refglobs;
2938 /* Extract files or directories from a WIM image */
2940 imagex_extract(int argc, tchar **argv, int cmd)
2947 const tchar *wimfile;
2948 const tchar *image_num_or_name;
2949 tchar *dest_dir = T(".");
2950 int extract_flags = WIMLIB_EXTRACT_FLAG_NORPFIX |
2951 WIMLIB_EXTRACT_FLAG_GLOB_PATHS |
2952 WIMLIB_EXTRACT_FLAG_STRICT_GLOB;
2953 int notlist_extract_flags = WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE;
2955 STRING_SET(refglobs);
2957 tchar *root_path = T("");
2959 for_opt(c, extract_options) {
2961 case IMAGEX_CHECK_OPTION:
2962 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2964 case IMAGEX_VERBOSE_OPTION:
2965 /* No longer does anything. */
2967 case IMAGEX_REF_OPTION:
2968 ret = string_set_append(&refglobs, optarg);
2970 goto out_free_refglobs;
2972 case IMAGEX_UNIX_DATA_OPTION:
2973 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2975 case IMAGEX_NO_ACLS_OPTION:
2976 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2978 case IMAGEX_STRICT_ACLS_OPTION:
2979 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2981 case IMAGEX_NO_ATTRIBUTES_OPTION:
2982 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES;
2984 case IMAGEX_DEST_DIR_OPTION:
2987 case IMAGEX_TO_STDOUT_OPTION:
2988 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2989 imagex_info_file = stderr;
2990 imagex_be_quiet = true;
2992 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2993 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2994 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2996 case IMAGEX_NO_WILDCARDS_OPTION:
2997 extract_flags &= ~WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2999 case IMAGEX_NULLGLOB_OPTION:
3000 extract_flags &= ~WIMLIB_EXTRACT_FLAG_STRICT_GLOB;
3002 case IMAGEX_PRESERVE_DIR_STRUCTURE_OPTION:
3003 notlist_extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE;
3015 if (!(extract_flags & (WIMLIB_EXTRACT_FLAG_GLOB_PATHS |
3016 WIMLIB_EXTRACT_FLAG_STRICT_GLOB)))
3018 imagex_error(T("Can't combine --no-wildcards and --nullglob!"));
3023 image_num_or_name = argv[1];
3028 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3030 goto out_free_refglobs;
3032 image = wimlib_resolve_image(wim, image_num_or_name);
3033 ret = verify_image_exists_and_is_single(image,
3037 goto out_wimlib_free;
3039 if (refglobs.num_strings) {
3040 ret = wim_reference_globs(wim, &refglobs, open_flags);
3042 goto out_wimlib_free;
3048 extract_flags &= ~WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
3051 while (argc != 0 && ret == 0) {
3055 num_paths < argc && argv[num_paths][0] != T('@');
3060 ret = wimlib_extract_paths(wim, image, dest_dir,
3061 (const tchar **)argv,
3063 extract_flags | notlist_extract_flags,
3064 imagex_progress_func);
3068 ret = wimlib_extract_pathlist(wim, image, dest_dir,
3071 imagex_progress_func);
3078 if (!imagex_be_quiet)
3079 imagex_printf(T("Done extracting files.\n"));
3080 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
3081 tfprintf(stderr, T("Note: You can use `%"TS"' to see what "
3082 "files and directories\n"
3083 " are in the WIM image.\n"),
3084 get_cmd_string(CMD_DIR, false));
3085 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
3086 struct wimlib_wim_info info;
3088 wimlib_get_wim_info(wim, &info);
3089 do_resource_not_found_warning(wimfile, &info, &refglobs);
3094 string_set_destroy(&refglobs);
3098 usage(CMD_EXTRACT, stderr);
3101 goto out_free_refglobs;
3104 /* Prints information about a WIM file; also can mark an image as bootable,
3105 * change the name of an image, or change the description of an image. */
3107 imagex_info(int argc, tchar **argv, int cmd)
3112 bool nocheck = false;
3113 bool header = false;
3114 bool lookup_table = false;
3116 bool short_header = true;
3117 const tchar *xml_out_file = NULL;
3118 const tchar *wimfile;
3119 const tchar *image_num_or_name;
3120 const tchar *new_name;
3121 const tchar *new_desc;
3126 struct wimlib_wim_info info;
3128 for_opt(c, info_options) {
3130 case IMAGEX_BOOT_OPTION:
3133 case IMAGEX_CHECK_OPTION:
3136 case IMAGEX_NOCHECK_OPTION:
3139 case IMAGEX_HEADER_OPTION:
3141 short_header = false;
3143 case IMAGEX_LOOKUP_TABLE_OPTION:
3144 lookup_table = true;
3145 short_header = false;
3147 case IMAGEX_XML_OPTION:
3149 short_header = false;
3151 case IMAGEX_EXTRACT_XML_OPTION:
3152 xml_out_file = optarg;
3153 short_header = false;
3155 case IMAGEX_METADATA_OPTION:
3156 imagex_error(T("The --metadata option has been removed. "
3157 "Use 'wimdir --detail' instead."));
3166 if (argc < 1 || argc > 4)
3170 image_num_or_name = (argc >= 2) ? argv[1] : T("all");
3171 new_name = (argc >= 3) ? argv[2] : NULL;
3172 new_desc = (argc >= 4) ? argv[3] : NULL;
3174 if (check && nocheck) {
3175 imagex_error(T("Can't specify both --check and --nocheck"));
3180 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3182 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3186 wimlib_get_wim_info(wim, &info);
3188 image = wimlib_resolve_image(wim, image_num_or_name);
3189 ret = WIMLIB_ERR_INVALID_IMAGE;
3190 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
3191 verify_image_exists(image, image_num_or_name, wimfile);
3193 imagex_error(T("If you would like to set the boot "
3194 "index to 0, specify image \"0\" with "
3195 "the --boot flag."));
3197 goto out_wimlib_free;
3200 if (boot && info.image_count == 0) {
3201 imagex_error(T("--boot is meaningless on a WIM with no images"));
3202 goto out_wimlib_free;
3205 if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
3207 imagex_error(T("Cannot specify the --boot flag "
3208 "without specifying a specific "
3209 "image in a multi-image WIM"));
3210 goto out_wimlib_free;
3213 imagex_error(T("Cannot specify the NEW_NAME "
3214 "without specifying a specific "
3215 "image in a multi-image WIM"));
3216 goto out_wimlib_free;
3220 /* Operations that print information are separated from operations that
3221 * recreate the WIM file. */
3222 if (!new_name && !boot) {
3224 /* Read-only operations */
3226 if (image == WIMLIB_NO_IMAGE) {
3227 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
3228 image_num_or_name, wimfile);
3229 goto out_wimlib_free;
3232 if (image == WIMLIB_ALL_IMAGES && short_header)
3233 print_wim_information(wimfile, &info);
3236 wimlib_print_header(wim);
3239 if (info.total_parts != 1) {
3240 tfprintf(stderr, T("Warning: Only showing the lookup table "
3241 "for part %d of a %d-part WIM.\n"),
3242 info.part_number, info.total_parts);
3244 print_lookup_table(wim);
3248 ret = wimlib_extract_xml_data(wim, stdout);
3250 goto out_wimlib_free;
3256 fp = tfopen(xml_out_file, T("wb"));
3258 imagex_error_with_errno(T("Failed to open the "
3259 "file \"%"TS"\" for "
3263 goto out_wimlib_free;
3265 ret = wimlib_extract_xml_data(wim, fp);
3267 imagex_error(T("Failed to close the file "
3273 goto out_wimlib_free;
3277 wimlib_print_available_images(wim, image);
3282 /* Modification operations */
3284 if (image == WIMLIB_ALL_IMAGES)
3287 if (image == WIMLIB_NO_IMAGE && new_name) {
3288 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
3289 "when using image 0"), new_name);
3291 goto out_wimlib_free;
3295 if (image == info.boot_index) {
3296 imagex_printf(T("Image %d is already marked as "
3297 "bootable.\n"), image);
3300 imagex_printf(T("Marking image %d as bootable.\n"),
3302 info.boot_index = image;
3303 ret = wimlib_set_wim_info(wim, &info,
3304 WIMLIB_CHANGE_BOOT_INDEX);
3306 goto out_wimlib_free;
3310 if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
3312 imagex_printf(T("Image %d is already named \"%"TS"\".\n"),
3316 imagex_printf(T("Changing the name of image %d to "
3317 "\"%"TS"\".\n"), image, new_name);
3318 ret = wimlib_set_image_name(wim, image, new_name);
3320 goto out_wimlib_free;
3324 const tchar *old_desc;
3325 old_desc = wimlib_get_image_description(wim, image);
3326 if (old_desc && !tstrcmp(old_desc, new_desc)) {
3327 imagex_printf(T("The description of image %d is already "
3328 "\"%"TS"\".\n"), image, new_desc);
3331 imagex_printf(T("Changing the description of image %d "
3332 "to \"%"TS"\".\n"), image, new_desc);
3333 ret = wimlib_set_image_descripton(wim, image,
3336 goto out_wimlib_free;
3340 /* Only call wimlib_overwrite() if something actually needs to
3342 if (boot || new_name || new_desc ||
3343 (check && !info.has_integrity_table) ||
3344 (nocheck && info.has_integrity_table))
3346 int write_flags = 0;
3349 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3351 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3352 ret = wimlib_overwrite(wim, write_flags, 1,
3353 imagex_progress_func);
3355 imagex_printf(T("The file \"%"TS"\" was not modified "
3356 "because nothing needed to be done.\n"),
3367 usage(CMD_INFO, stderr);
3373 /* Join split WIMs into one part WIM */
3375 imagex_join(int argc, tchar **argv, int cmd)
3378 int swm_open_flags = 0;
3379 int wim_write_flags = 0;
3380 const tchar *output_path;
3383 for_opt(c, join_options) {
3385 case IMAGEX_CHECK_OPTION:
3386 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3387 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3397 imagex_error(T("Must specify one or more split WIM (.swm) "
3401 output_path = argv[0];
3402 ret = wimlib_join((const tchar * const *)++argv,
3407 imagex_progress_func);
3412 usage(CMD_JOIN, stderr);
3417 #if WIM_MOUNTING_SUPPORTED
3419 /* Mounts a WIM image. */
3421 imagex_mount_rw_or_ro(int argc, tchar **argv, int cmd)
3424 int mount_flags = 0;
3426 const tchar *staging_dir = NULL;
3427 const tchar *wimfile;
3430 struct wimlib_wim_info info;
3434 STRING_SET(refglobs);
3436 if (cmd == CMD_MOUNTRW) {
3437 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
3438 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3441 for_opt(c, mount_options) {
3443 case IMAGEX_ALLOW_OTHER_OPTION:
3444 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
3446 case IMAGEX_CHECK_OPTION:
3447 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3449 case IMAGEX_DEBUG_OPTION:
3450 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
3452 case IMAGEX_STREAMS_INTERFACE_OPTION:
3453 if (!tstrcasecmp(optarg, T("none")))
3454 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
3455 else if (!tstrcasecmp(optarg, T("xattr")))
3456 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
3457 else if (!tstrcasecmp(optarg, T("windows")))
3458 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
3460 imagex_error(T("Unknown stream interface \"%"TS"\""),
3465 case IMAGEX_REF_OPTION:
3466 ret = string_set_append(&refglobs, optarg);
3468 goto out_free_refglobs;
3470 case IMAGEX_STAGING_DIR_OPTION:
3471 staging_dir = optarg;
3473 case IMAGEX_UNIX_DATA_OPTION:
3474 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
3482 if (argc != 2 && argc != 3)
3487 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3489 goto out_free_refglobs;
3491 wimlib_get_wim_info(wim, &info);
3494 /* Image explicitly specified. */
3495 image = wimlib_resolve_image(wim, argv[1]);
3497 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
3501 /* No image specified; default to image 1, but only if the WIM
3502 * contains exactly one image. */
3504 if (info.image_count != 1) {
3505 imagex_error(T("\"%"TS"\" contains %d images; Please "
3506 "select one."), wimfile, info.image_count);
3514 if (refglobs.num_strings) {
3515 ret = wim_reference_globs(wim, &refglobs, open_flags);
3520 ret = wimlib_mount_image(wim, image, dir, mount_flags, staging_dir);
3522 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
3524 image, wimfile, dir);
3529 string_set_destroy(&refglobs);
3535 goto out_free_refglobs;
3537 #endif /* WIM_MOUNTING_SUPPORTED */
3539 /* Rebuild a WIM file */
3541 imagex_optimize(int argc, tchar **argv, int cmd)
3544 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3545 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
3546 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
3547 uint32_t chunk_size = UINT32_MAX;
3548 uint32_t pack_chunk_size = UINT32_MAX;
3551 const tchar *wimfile;
3554 unsigned num_threads = 0;
3556 for_opt(c, optimize_options) {
3558 case IMAGEX_CHECK_OPTION:
3559 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3560 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3562 case IMAGEX_NOCHECK_OPTION:
3563 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3565 case IMAGEX_COMPRESS_OPTION:
3566 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3567 compression_type = get_compression_type(optarg);
3568 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
3571 case IMAGEX_RECOMPRESS_OPTION:
3572 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3574 case IMAGEX_COMPRESS_SLOW_OPTION:
3575 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3576 set_compress_slow();
3578 case IMAGEX_CHUNK_SIZE_OPTION:
3579 chunk_size = parse_chunk_size(optarg);
3580 if (chunk_size == UINT32_MAX)
3583 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
3584 pack_chunk_size = parse_chunk_size(optarg);
3585 if (pack_chunk_size == UINT32_MAX)
3588 case IMAGEX_PACK_STREAMS_OPTION:
3589 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
3590 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3592 case IMAGEX_THREADS_OPTION:
3593 num_threads = parse_num_threads(optarg);
3594 if (num_threads == UINT_MAX)
3597 case IMAGEX_PIPABLE_OPTION:
3598 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
3600 case IMAGEX_NOT_PIPABLE_OPTION:
3601 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
3615 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3619 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
3620 /* Change compression type. */
3621 ret = wimlib_set_output_compression_type(wim, compression_type);
3623 goto out_wimlib_free;
3626 if (chunk_size != UINT32_MAX) {
3627 /* Change chunk size. */
3628 ret = wimlib_set_output_chunk_size(wim, chunk_size);
3630 goto out_wimlib_free;
3632 if (pack_chunk_size != UINT32_MAX) {
3633 ret = wimlib_set_output_pack_chunk_size(wim, pack_chunk_size);
3635 goto out_wimlib_free;
3638 old_size = file_get_size(wimfile);
3639 tprintf(T("\"%"TS"\" original size: "), wimfile);
3641 tputs(T("Unknown"));
3643 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3645 ret = wimlib_overwrite(wim, write_flags, num_threads,
3646 imagex_progress_func);
3648 imagex_error(T("Optimization of \"%"TS"\" failed."), wimfile);
3649 goto out_wimlib_free;
3652 new_size = file_get_size(wimfile);
3653 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3655 tputs(T("Unknown"));
3657 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3659 tfputs(T("Space saved: "), stdout);
3660 if (new_size != -1 && old_size != -1) {
3661 tprintf(T("%lld KiB\n"),
3662 ((long long)old_size - (long long)new_size) >> 10);
3664 tputs(T("Unknown"));
3673 usage(CMD_OPTIMIZE, stderr);
3679 /* Split a WIM into a spanned set */
3681 imagex_split(int argc, tchar **argv, int cmd)
3685 int write_flags = 0;
3686 unsigned long part_size;
3691 for_opt(c, split_options) {
3693 case IMAGEX_CHECK_OPTION:
3694 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3695 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3707 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3708 if (tmp == argv[2] || *tmp) {
3709 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3710 imagex_error(T("The part size must be an integer or "
3711 "floating-point number of megabytes."));
3714 ret = wimlib_open_wim(argv[0], open_flags, &wim, imagex_progress_func);
3718 ret = wimlib_split(wim, argv[1], part_size, write_flags, imagex_progress_func);
3724 usage(CMD_SPLIT, stderr);
3730 #if WIM_MOUNTING_SUPPORTED
3731 /* Unmounts a mounted WIM image. */
3733 imagex_unmount(int argc, tchar **argv, int cmd)
3736 int unmount_flags = 0;
3739 for_opt(c, unmount_options) {
3741 case IMAGEX_COMMIT_OPTION:
3742 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3744 case IMAGEX_CHECK_OPTION:
3745 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3747 case IMAGEX_REBUILD_OPTION:
3748 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3750 case IMAGEX_LAZY_OPTION:
3751 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3762 ret = wimlib_unmount_image(argv[0], unmount_flags,
3763 imagex_progress_func);
3765 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3770 usage(CMD_UNMOUNT, stderr);
3774 #endif /* WIM_MOUNTING_SUPPORTED */
3777 * Add, delete, or rename files in a WIM image.
3780 imagex_update(int argc, tchar **argv, int cmd)
3782 const tchar *wimfile;
3786 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3787 int write_flags = 0;
3788 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3789 int default_add_flags = WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
3790 WIMLIB_ADD_FLAG_VERBOSE;
3791 int default_delete_flags = 0;
3792 unsigned num_threads = 0;
3794 tchar *cmd_file_contents;
3795 size_t cmd_file_nchars;
3796 struct wimlib_update_command *cmds;
3798 tchar *command_str = NULL;
3800 const tchar *config_file = NULL;
3802 struct wimlib_capture_config *config;
3804 for_opt(c, update_options) {
3806 /* Generic or write options */
3807 case IMAGEX_THREADS_OPTION:
3808 num_threads = parse_num_threads(optarg);
3809 if (num_threads == UINT_MAX)
3812 case IMAGEX_CHECK_OPTION:
3813 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3814 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3816 case IMAGEX_REBUILD_OPTION:
3817 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3819 case IMAGEX_COMMAND_OPTION:
3821 imagex_error(T("--command may only be specified "
3822 "one time. Please provide\n"
3823 " the update commands "
3824 "on standard input instead."));
3827 command_str = tstrdup(optarg);
3829 imagex_error(T("Out of memory!"));
3833 /* Default delete options */
3834 case IMAGEX_FORCE_OPTION:
3835 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3837 case IMAGEX_RECURSIVE_OPTION:
3838 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3841 /* Global add option */
3842 case IMAGEX_CONFIG_OPTION:
3843 default_add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
3844 config_file = optarg;
3847 /* Default add options */
3848 case IMAGEX_VERBOSE_OPTION:
3849 /* No longer does anything. */
3851 case IMAGEX_DEREFERENCE_OPTION:
3852 default_add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
3854 case IMAGEX_UNIX_DATA_OPTION:
3855 default_add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
3857 case IMAGEX_NO_ACLS_OPTION:
3858 default_add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
3860 case IMAGEX_STRICT_ACLS_OPTION:
3861 default_add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
3870 if (argc != 1 && argc != 2)
3874 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3876 goto out_free_command_str;
3879 /* Image explicitly specified. */
3880 image = wimlib_resolve_image(wim, argv[1]);
3881 ret = verify_image_exists_and_is_single(image, argv[1],
3884 goto out_wimlib_free;
3886 /* No image specified; default to image 1, but only if the WIM
3887 * contains exactly one image. */
3888 struct wimlib_wim_info info;
3890 wimlib_get_wim_info(wim, &info);
3891 if (info.image_count != 1) {
3892 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3893 wimfile, info.image_count);
3900 /* Parse capture configuration file if specified */
3904 config_str = file_get_text_contents(config_file, &config_len);
3907 goto out_wimlib_free;
3910 config = alloca(sizeof(*config));
3911 ret = parse_capture_config(&config_str, config_len, config);
3913 goto out_free_config;
3916 default_add_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
3919 /* Read update commands from standard input, or the command string if
3922 cmd_file_contents = NULL;
3923 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3926 if (isatty(STDIN_FILENO)) {
3927 tputs(T("Reading update commands from standard input..."));
3928 recommend_man_page(CMD_UPDATE, stdout);
3930 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3931 if (!cmd_file_contents) {
3933 goto out_free_config;
3936 /* Parse the update commands */
3937 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3942 goto out_free_cmd_file_contents;
3945 /* Set default flags and capture config on the update commands */
3946 for (size_t i = 0; i < num_cmds; i++) {
3947 switch (cmds[i].op) {
3948 case WIMLIB_UPDATE_OP_ADD:
3949 cmds[i].add.add_flags |= default_add_flags;
3950 cmds[i].add.config = config;
3952 case WIMLIB_UPDATE_OP_DELETE:
3953 cmds[i].delete_.delete_flags |= default_delete_flags;
3960 /* Execute the update commands */
3961 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3962 imagex_progress_func);
3966 /* Overwrite the updated WIM */
3967 ret = wimlib_overwrite(wim, write_flags, num_threads,
3968 imagex_progress_func);
3971 out_free_cmd_file_contents:
3972 free(cmd_file_contents);
3975 free(config->exclusion_pats.pats);
3976 free(config->exclusion_exception_pats.pats);
3981 out_free_command_str:
3986 usage(CMD_UPDATE, stderr);
3989 goto out_free_command_str;
3994 struct imagex_command {
3996 int (*func)(int argc, tchar **argv, int cmd);
3999 static const struct imagex_command imagex_commands[] = {
4000 [CMD_APPEND] = {T("append"), imagex_capture_or_append},
4001 [CMD_APPLY] = {T("apply"), imagex_apply},
4002 [CMD_CAPTURE] = {T("capture"), imagex_capture_or_append},
4003 [CMD_DELETE] = {T("delete"), imagex_delete},
4004 [CMD_DIR ] = {T("dir"), imagex_dir},
4005 [CMD_EXPORT] = {T("export"), imagex_export},
4006 [CMD_EXTRACT] = {T("extract"), imagex_extract},
4007 [CMD_INFO] = {T("info"), imagex_info},
4008 [CMD_JOIN] = {T("join"), imagex_join},
4009 #if WIM_MOUNTING_SUPPORTED
4010 [CMD_MOUNT] = {T("mount"), imagex_mount_rw_or_ro},
4011 [CMD_MOUNTRW] = {T("mountrw"), imagex_mount_rw_or_ro},
4013 [CMD_OPTIMIZE] = {T("optimize"), imagex_optimize},
4014 [CMD_SPLIT] = {T("split"), imagex_split},
4015 #if WIM_MOUNTING_SUPPORTED
4016 [CMD_UNMOUNT] = {T("unmount"), imagex_unmount},
4018 [CMD_UPDATE] = {T("update"), imagex_update},
4021 static const tchar *usage_strings[] = {
4024 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
4025 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
4026 " [--nocheck] [--flags EDITION_ID] [--dereference]\n"
4027 " [--config=FILE] [--threads=NUM_THREADS] [--source-list]\n"
4028 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
4029 " [--update-of=[WIMFILE:]IMAGE]\n"
4033 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME | all)]\n"
4034 " (DIRECTORY | NTFS_VOLUME) [--check] [--ref=\"GLOB\"]\n"
4035 " [--no-acls] [--strict-acls] [--no-attributes]\n"
4036 " [--rpfix] [--norpfix] [--hardlink] [--symlink]\n"
4037 " [--include-invalid-names]\n"
4041 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
4042 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
4043 " [--nocheck] [--compress=TYPE] [--flags EDITION_ID]\n"
4044 " [--dereference] [--config=FILE] [--threads=NUM_THREADS]\n"
4045 " [--source-list] [--no-acls] [--strict-acls] [--rpfix]\n"
4046 " [--norpfix] [--update-of=[WIMFILE:]IMAGE]\n"
4047 " [--delta-from=WIMFILE]\n"
4051 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n"
4052 " [--check] [--soft]\n"
4056 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH] [--detailed]\n"
4060 " %"TS" SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
4061 " DEST_WIMFILE [DEST_IMAGE_NAME [DEST_IMAGE_DESCRIPTION]]\n"
4062 " [--boot] [--check] [--nocheck] [--compress=TYPE]\n"
4063 " [--ref=\"GLOB\"] [--threads=NUM_THREADS] [--rebuild]\n"
4067 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME) [(PATH | @LISTFILE)...]\n"
4068 " [--check] [--ref=\"GLOB\"] [--dest-dir=CMD_DIR]\n"
4069 " [--to-stdout] [--no-acls] [--strict-acls]\n"
4070 " [--no-attributes] [--include-invalid-names]\n"
4071 " [--no-wildcards] [--nullglob] [--preserve-dir-structure]\n"
4075 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME) [NEW_NAME\n"
4076 " [NEW_DESC]]] [--boot] [--check] [--nocheck] [--xml]\n"
4077 " [--extract-xml FILE] [--header] [--lookup-table]\n"
4081 " %"TS" OUT_WIMFILE SPLIT_WIM_PART... [--check]\n"
4083 #if WIM_MOUNTING_SUPPORTED
4086 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
4087 " [--check] [--streams-interface=INTERFACE]\n"
4088 " [--ref=\"GLOB\"] [--allow-other]\n"
4092 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
4093 " [--check] [--streams-interface=INTERFACE]\n"
4094 " [--staging-dir=CMD_DIR] [--allow-other]\n"
4099 " %"TS" WIMFILE [--check] [--nocheck] [--recompress]\n"
4100 " [--recompress-slow] [--compress=TYPE]\n"
4101 " [--threads=NUM_THREADS]\n"
4105 " %"TS" WIMFILE SPLIT_WIM_PART_1 PART_SIZE_MB [--check]\n"
4107 #if WIM_MOUNTING_SUPPORTED
4110 " %"TS" DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
4115 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
4116 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
4117 " [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
4121 static const tchar *invocation_name;
4122 static int invocation_cmd = CMD_NONE;
4124 static const tchar *get_cmd_string(int cmd, bool nospace)
4126 static tchar buf[50];
4127 if (cmd == CMD_NONE) {
4128 tsprintf(buf, T("%"TS), T(IMAGEX_PROGNAME));
4129 } else if (invocation_cmd != CMD_NONE) {
4130 tsprintf(buf, T("wim%"TS), imagex_commands[cmd].name);
4132 const tchar *format;
4135 format = T("%"TS"-%"TS"");
4137 format = T("%"TS" %"TS"");
4138 tsprintf(buf, format, invocation_name, imagex_commands[cmd].name);
4146 static const tchar *s =
4148 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
4149 "Copyright (C) 2012, 2013 Eric Biggers\n"
4150 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
4151 "This is free software: you are free to change and redistribute it.\n"
4152 "There is NO WARRANTY, to the extent permitted by law.\n"
4154 "Report bugs to "PACKAGE_BUGREPORT".\n"
4161 help_or_version(int argc, tchar **argv, int cmd)
4166 for (i = 1; i < argc; i++) {
4168 if (p[0] == T('-') && p[1] == T('-')) {
4170 if (!tstrcmp(p, T("help"))) {
4171 if (cmd == CMD_NONE)
4176 } else if (!tstrcmp(p, T("version"))) {
4185 print_usage_string(int cmd, FILE *fp)
4187 tfprintf(fp, usage_strings[cmd], get_cmd_string(cmd, false));
4191 recommend_man_page(int cmd, FILE *fp)
4193 const tchar *format_str;
4195 format_str = T("Uncommon options are not listed;\n"
4196 "See %"TS".pdf in the doc directory for more details.\n");
4198 format_str = T("Uncommon options are not listed;\n"
4199 "Try `man %"TS"' for more details.\n");
4201 tfprintf(fp, format_str, get_cmd_string(cmd, true));
4205 usage(int cmd, FILE *fp)
4207 tfprintf(fp, T("Usage:\n"));
4208 print_usage_string(cmd, fp);
4209 tfprintf(fp, T("\n"));
4210 recommend_man_page(cmd, fp);
4216 tfprintf(fp, T("Usage:\n"));
4217 for (int cmd = 0; cmd < CMD_MAX; cmd++) {
4218 print_usage_string(cmd, fp);
4219 tfprintf(fp, T("\n"));
4221 static const tchar *extra =
4224 " %"TS" --version\n"
4226 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
4229 tfprintf(fp, extra, invocation_name, invocation_name);
4230 recommend_man_page(CMD_NONE, fp);
4233 /* Entry point for wimlib's ImageX implementation. On UNIX the command
4234 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
4235 * something else), while an Windows the command arguments will be UTF-16LE
4236 * encoded 'wchar_t' strings. */
4239 wmain(int argc, wchar_t **argv, wchar_t **envp)
4241 main(int argc, char **argv)
4248 imagex_info_file = stdout;
4249 invocation_name = tbasename(argv[0]);
4252 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
4253 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
4257 setlocale(LC_ALL, "");
4258 codeset = nl_langinfo(CODESET);
4259 if (!strstr(codeset, "UTF-8") &&
4260 !strstr(codeset, "UTF8") &&
4261 !strstr(codeset, "utf-8") &&
4262 !strstr(codeset, "utf8"))
4265 "WARNING: Running %"TS" in a UTF-8 locale is recommended!\n"
4266 " Maybe try: `export LANG=en_US.UTF-8'?\n"
4267 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
4268 " to any value to force wimlib to use UTF-8.\n",
4274 #endif /* !__WIN32__ */
4277 tchar *igcase = tgetenv(T("WIMLIB_IMAGEX_IGNORE_CASE"));
4278 if (igcase != NULL) {
4279 if (!tstrcmp(igcase, T("no")) ||
4280 !tstrcmp(igcase, T("0")))
4281 init_flags |= WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE;
4282 else if (!tstrcmp(igcase, T("yes")) ||
4283 !tstrcmp(igcase, T("1")))
4284 init_flags |= WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE;
4287 "WARNING: Ignoring unknown setting of "
4288 "WIMLIB_IMAGEX_IGNORE_CASE\n");
4293 /* Allow being invoked as wimCOMMAND (e.g. wimapply). */
4295 if (!tstrncmp(invocation_name, T("wim"), 3) &&
4296 tstrcmp(invocation_name, T(IMAGEX_PROGNAME))) {
4297 for (int i = 0; i < CMD_MAX; i++) {
4298 if (!tstrcmp(invocation_name + 3,
4299 imagex_commands[i].name))
4308 /* Unless already known from the invocation name, determine which
4309 * command was specified. */
4310 if (cmd == CMD_NONE) {
4312 imagex_error(T("No command specified!\n"));
4316 for (int i = 0; i < CMD_MAX; i++) {
4317 if (!tstrcmp(argv[1], imagex_commands[i].name)) {
4322 if (cmd != CMD_NONE) {
4328 /* Handle --help and --version. --help can be either for the program as
4329 * a whole (cmd == CMD_NONE) or just for a specific command (cmd !=
4330 * CMD_NONE). Note: help_or_version() will not return if a --help or
4331 * --version argument was found. */
4332 help_or_version(argc, argv, cmd);
4334 /* Bail if a valid command was not specified. */
4335 if (cmd == CMD_NONE) {
4336 imagex_error(T("Unrecognized command: `%"TS"'\n"), argv[1]);
4341 /* Enable warning and error messages in wimlib be more user-friendly.
4343 wimlib_set_print_errors(true);
4345 /* Initialize wimlib. */
4346 ret = wimlib_global_init(init_flags);
4348 goto out_check_status;
4350 /* Call the command handler function. */
4351 ret = imagex_commands[cmd].func(argc, argv, cmd);
4353 /* Check for error writing to standard output, especially since for some
4354 * commands, writing to standard output is part of the program's actual
4355 * behavior and not just for informational purposes. */
4356 if (ferror(stdout) || fclose(stdout)) {
4357 imagex_error_with_errno(T("error writing to standard output"));
4362 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
4363 * itself (not by wimlib). 0 indicates success. > 0 indicates a wimlib
4364 * error code from which an error message can be printed. */
4366 imagex_error(T("Exiting with error code %d:\n"
4368 wimlib_get_error_string(ret));
4369 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
4370 imagex_error_with_errno(T("errno"));
4372 /* Make wimlib free any resources it's holding (although this is not
4373 * strictly necessary because the process is ending anyway). */
4374 wimlib_global_cleanup();