4 * Use wimlib to create, modify, extract, mount, unmount, or display information
9 * Copyright (C) 2012, 2013, 2014 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 print_security_descriptor win32_print_security_descriptor
54 # include <langinfo.h>
55 # define print_security_descriptor default_print_security_descriptor
56 static inline void set_fd_to_binary_mode(int fd)
61 /* Don't confuse the user by presenting the mounting commands on Windows when
62 * they will never work. However on UNIX-like systems we always present them,
63 * even if WITH_FUSE is not defined at this point, as to not tie the build of
64 * wimlib-imagex to a specific build of wimlib. */
66 # define WIM_MOUNTING_SUPPORTED 0
68 # define WIM_MOUNTING_SUPPORTED 1
71 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
74 is_any_path_separator(tchar c)
76 return c == T('/') || c == T('\\');
79 /* Like basename(), but handles both forward and backwards slashes. */
81 tbasename(tchar *path)
83 tchar *p = tstrchr(path, T('\0'));
88 if (!is_any_path_separator(*--p))
96 if (is_any_path_separator(*--p))
101 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
115 #if WIM_MOUNTING_SUPPORTED
121 #if WIM_MOUNTING_SUPPORTED
128 static void usage(int cmd, FILE *fp);
129 static void usage_all(FILE *fp);
130 static void recommend_man_page(int cmd, FILE *fp);
131 static const tchar *get_cmd_string(int cmd, bool nospace);
133 static int imagex_progress_func(enum wimlib_progress_msg msg,
134 const union wimlib_progress_info *info);
136 static bool imagex_be_quiet = false;
137 static FILE *imagex_info_file;
139 #define imagex_printf(format, ...) \
140 tfprintf(imagex_info_file, format, ##__VA_ARGS__)
143 IMAGEX_ALLOW_OTHER_OPTION,
146 IMAGEX_CHUNK_SIZE_OPTION,
147 IMAGEX_COMMAND_OPTION,
148 IMAGEX_COMMIT_OPTION,
149 IMAGEX_COMPRESS_OPTION,
150 IMAGEX_COMPRESS_SLOW_OPTION,
151 IMAGEX_CONFIG_OPTION,
153 IMAGEX_DELTA_FROM_OPTION,
154 IMAGEX_DEREFERENCE_OPTION,
155 IMAGEX_DEST_DIR_OPTION,
156 IMAGEX_DETAILED_OPTION,
157 IMAGEX_EXTRACT_XML_OPTION,
160 IMAGEX_HARDLINK_OPTION,
161 IMAGEX_HEADER_OPTION,
162 IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
164 IMAGEX_LOOKUP_TABLE_OPTION,
165 IMAGEX_METADATA_OPTION,
166 IMAGEX_NEW_IMAGE_OPTION,
167 IMAGEX_NOCHECK_OPTION,
168 IMAGEX_NORPFIX_OPTION,
169 IMAGEX_NOT_PIPABLE_OPTION,
170 IMAGEX_NO_ACLS_OPTION,
171 IMAGEX_NO_ATTRIBUTES_OPTION,
172 IMAGEX_NO_REPLACE_OPTION,
173 IMAGEX_NO_WILDCARDS_OPTION,
174 IMAGEX_NULLGLOB_OPTION,
175 IMAGEX_ONE_FILE_ONLY_OPTION,
176 IMAGEX_PACK_CHUNK_SIZE_OPTION,
177 IMAGEX_PACK_COMPRESS_OPTION,
178 IMAGEX_PACK_STREAMS_OPTION,
180 IMAGEX_PIPABLE_OPTION,
181 IMAGEX_PRESERVE_DIR_STRUCTURE_OPTION,
182 IMAGEX_REBUILD_OPTION,
183 IMAGEX_RECOMPRESS_OPTION,
184 IMAGEX_RECURSIVE_OPTION,
186 IMAGEX_RESUME_OPTION,
189 IMAGEX_SOURCE_LIST_OPTION,
190 IMAGEX_STAGING_DIR_OPTION,
191 IMAGEX_STREAMS_INTERFACE_OPTION,
192 IMAGEX_STRICT_ACLS_OPTION,
193 IMAGEX_SYMLINK_OPTION,
194 IMAGEX_THREADS_OPTION,
195 IMAGEX_TO_STDOUT_OPTION,
196 IMAGEX_UNIX_DATA_OPTION,
197 IMAGEX_UPDATE_OF_OPTION,
198 IMAGEX_VERBOSE_OPTION,
199 IMAGEX_WIMBOOT_OPTION,
200 IMAGEX_WIMBOOT_CONFIG_OPTION,
204 static const struct option apply_options[] = {
205 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
206 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
207 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
208 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
209 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
210 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
211 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
212 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
213 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
214 {T("no-attributes"), no_argument, NULL, IMAGEX_NO_ATTRIBUTES_OPTION},
215 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
216 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
217 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
219 /* --resume is undocumented for now as it needs improvement. */
220 {T("resume"), no_argument, NULL, IMAGEX_RESUME_OPTION},
221 {T("wimboot"), no_argument, NULL, IMAGEX_WIMBOOT_OPTION},
225 static const struct option capture_or_append_options[] = {
226 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
227 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
228 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
229 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
230 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
231 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
232 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
233 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
234 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
235 {T("pack-compress"), required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
236 {T("solid-compress"),required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
237 {T("pack-streams"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
238 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
239 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
240 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
241 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
242 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
243 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
244 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
245 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
246 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
247 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
248 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
249 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
250 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
251 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
252 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
253 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
254 {T("update-of"), required_argument, NULL, IMAGEX_UPDATE_OF_OPTION},
255 {T("delta-from"), required_argument, NULL, IMAGEX_DELTA_FROM_OPTION},
256 {T("wimboot"), no_argument, NULL, IMAGEX_WIMBOOT_OPTION},
260 static const struct option delete_options[] = {
261 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
262 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
266 static const struct option dir_options[] = {
267 {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
268 {T("detailed"), no_argument, NULL, IMAGEX_DETAILED_OPTION},
269 {T("one-file-only"), no_argument, NULL, IMAGEX_ONE_FILE_ONLY_OPTION},
273 static const struct option export_options[] = {
274 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
275 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
276 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
277 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
278 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
279 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
280 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
281 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
282 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
283 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
284 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
285 {T("pack-compress"), required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
286 {T("solid-compress"),required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
287 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
288 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
289 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
290 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
291 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
295 static const struct option extract_options[] = {
296 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
297 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
298 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
299 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
300 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
301 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
302 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
303 {T("no-attributes"), no_argument, NULL, IMAGEX_NO_ATTRIBUTES_OPTION},
304 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
305 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
306 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
307 {T("no-wildcards"), no_argument, NULL, IMAGEX_NO_WILDCARDS_OPTION},
308 {T("nullglob"), no_argument, NULL, IMAGEX_NULLGLOB_OPTION},
309 {T("preserve-dir-structure"), no_argument, NULL, IMAGEX_PRESERVE_DIR_STRUCTURE_OPTION},
310 {T("wimboot"), no_argument, NULL, IMAGEX_WIMBOOT_OPTION},
314 static const struct option info_options[] = {
315 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
316 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
317 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
318 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
319 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
320 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
321 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
322 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
323 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
327 static const struct option join_options[] = {
328 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
332 static const struct option mount_options[] = {
333 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
334 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
335 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
336 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
337 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
338 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
339 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
343 static const struct option optimize_options[] = {
344 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
345 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
346 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
347 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
348 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
349 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
350 {T("recompress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
351 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
352 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
353 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
354 {T("pack-compress"), required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
355 {T("solid-compress"),required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
356 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
357 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
358 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
359 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
360 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
364 static const struct option split_options[] = {
365 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
369 static const struct option unmount_options[] = {
370 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
371 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
372 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
373 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
374 {T("new-image"), no_argument, NULL, IMAGEX_NEW_IMAGE_OPTION},
378 static const struct option update_options[] = {
379 /* Careful: some of the options here set the defaults for update
380 * commands, but the flags given to an actual update command (and not to
381 * `imagex update' itself are also handled in
382 * update_command_add_option(). */
383 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
384 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
385 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
386 {T("command"), required_argument, NULL, IMAGEX_COMMAND_OPTION},
387 {T("wimboot-config"), required_argument, NULL, IMAGEX_WIMBOOT_CONFIG_OPTION},
389 /* Default delete options */
390 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
391 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
393 /* Global add option */
394 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
396 /* Default add options */
397 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
398 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
399 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
400 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
401 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
402 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
403 {T("no-replace"), no_argument, NULL, IMAGEX_NO_REPLACE_OPTION},
409 # define _format_attribute(type, format_str, args_start) \
410 __attribute__((format(type, format_str, args_start)))
412 # define _format_attribute(type, format_str, args_start)
415 /* Print formatted error message to stderr. */
416 static void _format_attribute(printf, 1, 2)
417 imagex_error(const tchar *format, ...)
420 va_start(va, format);
421 tfputs(T("ERROR: "), stderr);
422 tvfprintf(stderr, format, va);
423 tputc(T('\n'), stderr);
427 /* Print formatted error message to stderr. */
428 static void _format_attribute(printf, 1, 2)
429 imagex_error_with_errno(const tchar *format, ...)
431 int errno_save = errno;
433 va_start(va, format);
434 tfputs(T("ERROR: "), stderr);
435 tvfprintf(stderr, format, va);
436 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
441 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
443 if (image == WIMLIB_NO_IMAGE) {
444 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
445 " Please specify a 1-based image index or "
446 "image name. To list the images\n"
447 " contained in the WIM archive, run\n"
449 " %"TS" \"%"TS"\"\n"),
450 image_name, wim_name,
451 get_cmd_string(CMD_INFO, false), wim_name);
452 return WIMLIB_ERR_INVALID_IMAGE;
458 verify_image_is_single(int image)
460 if (image == WIMLIB_ALL_IMAGES) {
461 imagex_error(T("Cannot specify all images for this action!"));
462 return WIMLIB_ERR_INVALID_IMAGE;
468 verify_image_exists_and_is_single(int image, const tchar *image_name,
469 const tchar *wim_name)
472 ret = verify_image_exists(image, image_name, wim_name);
474 ret = verify_image_is_single(image);
478 /* Parse the argument to --compress */
480 get_compression_type(const tchar *optarg)
482 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
483 return WIMLIB_COMPRESSION_TYPE_LZX;
484 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
485 return WIMLIB_COMPRESSION_TYPE_XPRESS;
486 else if (!tstrcasecmp(optarg, T("recovery")) || !tstrcasecmp(optarg, T("lzms")))
487 return WIMLIB_COMPRESSION_TYPE_LZMS;
488 else if (!tstrcasecmp(optarg, T("none")))
489 return WIMLIB_COMPRESSION_TYPE_NONE;
491 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
492 "\"maximum\", \"fast\", or \"none\"."), optarg);
493 return WIMLIB_COMPRESSION_TYPE_INVALID;
498 set_compress_slow(void)
500 static const struct wimlib_lzx_compressor_params lzx_slow_params = {
502 .size = sizeof(struct wimlib_lzx_compressor_params),
504 .algorithm = WIMLIB_LZX_ALGORITHM_SLOW,
507 .use_len2_matches = 1,
508 .nice_match_length = 96,
509 .num_optim_passes = 4,
510 .max_search_depth = 100,
511 .max_matches_per_pos = 10,
512 .main_nostat_cost = 15,
513 .len_nostat_cost = 15,
514 .aligned_nostat_cost = 7,
519 static const struct wimlib_lzms_compressor_params lzms_slow_params = {
521 .size = sizeof(struct wimlib_lzms_compressor_params),
523 .min_match_length = 2,
524 .max_match_length = UINT32_MAX,
525 .nice_match_length = 96,
526 .max_search_depth = 100,
527 .max_matches_per_pos = 10,
528 .optim_array_length = 1024,
531 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
532 &lzx_slow_params.hdr);
534 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZMS,
535 &lzms_slow_params.hdr);
539 const tchar **strings;
540 unsigned num_strings;
541 unsigned num_alloc_strings;
544 #define STRING_SET_INITIALIZER \
545 { .strings = NULL, .num_strings = 0, .num_alloc_strings = 0, }
547 #define STRING_SET(_strings) \
548 struct string_set _strings = STRING_SET_INITIALIZER
551 string_set_append(struct string_set *set, const tchar *glob)
553 unsigned num_alloc_strings = set->num_alloc_strings;
555 if (set->num_strings == num_alloc_strings) {
556 const tchar **new_strings;
558 num_alloc_strings += 4;
559 new_strings = realloc(set->strings,
560 sizeof(set->strings[0]) * num_alloc_strings);
562 imagex_error(T("Out of memory!"));
565 set->strings = new_strings;
566 set->num_alloc_strings = num_alloc_strings;
568 set->strings[set->num_strings++] = glob;
573 string_set_destroy(struct string_set *set)
579 wim_reference_globs(WIMStruct *wim, struct string_set *set, int open_flags)
581 return wimlib_reference_resource_files(wim, set->strings,
583 WIMLIB_REF_FLAG_GLOB_ENABLE,
585 imagex_progress_func);
589 do_resource_not_found_warning(const tchar *wimfile,
590 const struct wimlib_wim_info *info,
591 const struct string_set *refglobs)
593 if (info->total_parts > 1) {
594 if (refglobs->num_strings == 0) {
595 imagex_error(T("\"%"TS"\" is part of a split WIM. "
596 "Use --ref to specify the other parts."),
599 imagex_error(T("Perhaps the '--ref' argument did not "
600 "specify all other parts of the split "
604 imagex_error(T("If this is a delta WIM, use the --ref argument "
605 "to specify the WIM(s) on which it is based."));
609 /* Returns the size of a file given its name, or -1 if the file does not exist
610 * or its size cannot be determined. */
612 file_get_size(const tchar *filename)
615 if (tstat(filename, &st) == 0)
622 PARSE_STRING_SUCCESS = 0,
623 PARSE_STRING_FAILURE = 1,
624 PARSE_STRING_NONE = 2,
628 * Parses a string token from an array of characters.
630 * Tokens are either whitespace-delimited, or double or single-quoted.
632 * @line_p: Pointer to the pointer to the line of data. Will be updated
633 * to point past the string token iff the return value is
634 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
637 * @len_p: @len_p initially stores the length of the line of data, which may
638 * be 0, and it will be updated to the number of bytes remaining in
639 * the line iff the return value is PARSE_STRING_SUCCESS.
641 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
642 * parsed string token will be returned here.
644 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
645 * PARSE_STRING_FAILURE if the data was invalid due to a missing
646 * closing quote; or PARSE_STRING_NONE if the line ended before the
647 * beginning of a string token was found.
650 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
653 tchar *line = *line_p;
657 /* Skip leading whitespace */
660 return PARSE_STRING_NONE;
661 if (!istspace(*line) && *line != T('\0'))
667 if (quote_char == T('"') || quote_char == T('\'')) {
672 line = tmemchr(line, quote_char, len);
674 imagex_error(T("Missing closing quote: %"TS), fn - 1);
675 return PARSE_STRING_FAILURE;
678 /* Unquoted string. Go until whitespace. Line is terminated
679 * by '\0', so no need to check 'len'. */
683 } while (!istspace(*line) && *line != T('\0'));
690 return PARSE_STRING_SUCCESS;
693 /* Parses a line of data (not an empty line or comment) in the source list file
694 * format. (See the man page for 'wimlib-imagex capture' for details on this
695 * format and the meaning.)
697 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
698 * len == 0. The data in @line will be modified by this function call.
700 * @len: Length of the line of data.
702 * @source: On success, the capture source and target described by the line is
703 * written into this destination. Note that it will contain pointers
704 * to data in the @line array.
706 * Returns true if the line was valid; false otherwise. */
708 parse_source_list_line(tchar *line, size_t len,
709 struct wimlib_capture_source *source)
713 ret = parse_string(&line, &len, &source->fs_source_path);
714 if (ret != PARSE_STRING_SUCCESS)
716 ret = parse_string(&line, &len, &source->wim_target_path);
717 if (ret == PARSE_STRING_NONE)
718 source->wim_target_path = source->fs_source_path;
719 return ret != PARSE_STRING_FAILURE;
722 /* Returns %true if the given line of length @len > 0 is a comment or empty line
723 * in the source list file format. */
725 is_comment_line(const tchar *line, size_t len)
728 if (*line == T('#') || *line == T(';'))
730 if (!istspace(*line) && *line != T('\0'))
740 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
743 tchar *contents = *contents_p;
744 size_t nchars = *nchars_p;
747 for (i = 0; i < nchars; i++)
748 if (contents[i] == T('\n'))
751 /* Handle last line not terminated by a newline */
752 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
753 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
755 imagex_error(T("Out of memory!"));
758 contents[nchars] = T('\n');
759 *contents_p = contents;
767 /* Parses a file in the source list format. (See the man page for
768 * 'wimlib-imagex capture' for details on this format and the meaning.)
770 * @source_list_contents: Contents of the source list file. Note that this
771 * buffer will be modified to save memory allocations,
772 * and cannot be freed until the returned array of
773 * wimlib_capture_source's has also been freed.
775 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
778 * @nsources_ret: On success, the length of the returned array is
781 * Returns: An array of `struct wimlib_capture_source's that can be passed to
782 * the wimlib_add_image_multisource() function to specify how a WIM image is to
784 static struct wimlib_capture_source *
785 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
786 size_t *nsources_ret)
790 struct wimlib_capture_source *sources;
793 nlines = text_file_count_lines(source_list_contents_p,
794 &source_list_nchars);
798 /* Always allocate at least 1 slot, just in case the implementation of
799 * calloc() returns NULL if 0 bytes are requested. */
800 sources = calloc(nlines ?: 1, sizeof(*sources));
802 imagex_error(T("out of memory"));
805 p = *source_list_contents_p;
807 for (i = 0; i < nlines; i++) {
808 /* XXX: Could use rawmemchr() here instead, but it may not be
809 * available on all platforms. */
810 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
811 size_t len = endp - p + 1;
813 if (!is_comment_line(p, len)) {
814 if (!parse_source_list_line(p, len, &sources[j++])) {
826 /* Reads the contents of a file into memory. */
828 file_get_contents(const tchar *filename, size_t *len_ret)
835 if (tstat(filename, &stbuf) != 0) {
836 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
841 fp = tfopen(filename, T("rb"));
843 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
847 buf = malloc(len ? len : 1);
849 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
850 "contents of file \"%"TS"\""), len, filename);
853 if (fread(buf, 1, len, fp) != len) {
854 imagex_error_with_errno(T("Failed to read %zu bytes from the "
855 "file \"%"TS"\""), len, filename);
869 /* Read standard input until EOF and return the full contents in a malloc()ed
870 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
873 stdin_get_contents(size_t *len_ret)
875 /* stdin can, of course, be a pipe or other non-seekable file, so the
876 * total length of the data cannot be pre-determined */
878 size_t newlen = 1024;
882 char *p = realloc(buf, newlen);
883 size_t bytes_read, bytes_to_read;
885 imagex_error(T("out of memory while reading stdin"));
889 bytes_to_read = newlen - pos;
890 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
892 if (bytes_read != bytes_to_read) {
897 imagex_error_with_errno(T("error reading stdin"));
911 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
914 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
916 *num_tchars_ret = num_bytes;
918 #else /* !__WIN32__ */
919 /* On Windows, translate the text to UTF-16LE */
923 if (num_bytes >= 2 &&
924 (((unsigned char)text[0] == 0xff && (unsigned char)text[1] == 0xfe) ||
925 ((unsigned char)text[0] <= 0x7f && (unsigned char)text[1] == 0x00)))
927 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
928 * with something that looks like an ASCII character encoded as
929 * a UTF-16LE code unit. Assume the file is encoded as
930 * UTF-16LE. This is not a 100% reliable check. */
931 num_wchars = num_bytes / 2;
932 text_wstr = (wchar_t*)text;
934 /* File does not look like UTF-16LE. Assume it is encoded in
935 * the current Windows code page. I think these are always
936 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
937 * should work as expected. */
938 text_wstr = win32_mbs_to_wcs(text,
943 *num_tchars_ret = num_wchars;
945 #endif /* __WIN32__ */
949 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
954 contents = file_get_contents(filename, &num_bytes);
957 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
961 stdin_get_text_contents(size_t *num_tchars_ret)
966 contents = stdin_get_contents(&num_bytes);
969 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
972 #define TO_PERCENT(numerator, denominator) \
973 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
975 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
976 #define MEBIBYTE_MIN_NBYTES 10000000ULL
977 #define KIBIBYTE_MIN_NBYTES 10000ULL
980 get_unit(uint64_t total_bytes, const tchar **name_ret)
982 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
983 *name_ret = T("GiB");
985 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
986 *name_ret = T("MiB");
988 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
989 *name_ret = T("KiB");
992 *name_ret = T("bytes");
997 static struct wimlib_progress_info_scan last_scan_progress;
1000 report_scan_progress(const struct wimlib_progress_info_scan *scan, bool done)
1002 uint64_t prev_count, cur_count;
1004 prev_count = last_scan_progress.num_nondirs_scanned +
1005 last_scan_progress.num_dirs_scanned;
1006 cur_count = scan->num_nondirs_scanned + scan->num_dirs_scanned;
1008 if (done || prev_count == 0 || cur_count >= prev_count + 100 ||
1009 cur_count % 128 == 0)
1011 unsigned unit_shift;
1012 const tchar *unit_name;
1014 unit_shift = get_unit(scan->num_bytes_scanned, &unit_name);
1015 imagex_printf(T("\r%"PRIu64" %"TS" scanned (%"PRIu64" files, "
1016 "%"PRIu64" directories) "),
1017 scan->num_bytes_scanned >> unit_shift,
1019 scan->num_nondirs_scanned,
1020 scan->num_dirs_scanned);
1021 last_scan_progress = *scan;
1025 /* Progress callback function passed to various wimlib functions. */
1027 imagex_progress_func(enum wimlib_progress_msg msg,
1028 const union wimlib_progress_info *info)
1030 unsigned percent_done;
1031 unsigned unit_shift;
1032 const tchar *unit_name;
1034 if (imagex_be_quiet)
1037 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1039 static bool first = true;
1041 imagex_printf(T("Writing %"TS"-compressed data "
1042 "using %u thread%"TS"\n"),
1043 wimlib_get_compression_type_string(
1044 info->write_streams.compression_type),
1045 info->write_streams.num_threads,
1046 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1050 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1051 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1052 info->write_streams.total_bytes);
1054 imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1055 "written (%u%% done)"),
1056 info->write_streams.completed_bytes >> unit_shift,
1058 info->write_streams.total_bytes >> unit_shift,
1061 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1062 imagex_printf(T("\n"));
1064 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1065 imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
1066 if (WIMLIB_IS_WIM_ROOT_PATH(info->scan.wim_target_path)) {
1067 imagex_printf(T("\n"));
1069 imagex_printf(T(" (loading as WIM path: \"%"TS"\")...\n"),
1070 info->scan.wim_target_path);
1072 memset(&last_scan_progress, 0, sizeof(last_scan_progress));
1074 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1075 switch (info->scan.status) {
1076 case WIMLIB_SCAN_DENTRY_OK:
1077 report_scan_progress(&info->scan, false);
1079 case WIMLIB_SCAN_DENTRY_EXCLUDED:
1080 imagex_printf(T("\nExcluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1082 case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
1083 imagex_printf(T("\nWARNING: Excluding unsupported file or directory\n"
1084 " \"%"TS"\" from capture\n"), info->scan.cur_path);
1086 case WIMLIB_SCAN_DENTRY_EXCLUDED_SYMLINK:
1087 imagex_printf(T("\nWARNING: Ignoring absolute symbolic link "
1088 "with out-of-tree target:\n"
1089 " \"%"TS"\" => \"%"TS"\"\n"
1090 " (Use --norpfix to capture "
1091 "absolute symbolic links as-is)\n"),
1092 info->scan.cur_path, info->scan.symlink_target);
1096 case WIMLIB_PROGRESS_MSG_SCAN_END:
1097 report_scan_progress(&info->scan, true);
1098 imagex_printf(T("\n"));
1100 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1101 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1102 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1103 info->integrity.total_bytes);
1104 imagex_printf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1105 "of %"PRIu64" %"TS" (%u%%) done"),
1106 info->integrity.filename,
1107 info->integrity.completed_bytes >> unit_shift,
1109 info->integrity.total_bytes >> unit_shift,
1112 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1113 imagex_printf(T("\n"));
1115 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1116 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1117 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1118 info->integrity.total_bytes);
1119 imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1120 "of %"PRIu64" %"TS" (%u%%) done"),
1121 info->integrity.completed_bytes >> unit_shift,
1123 info->integrity.total_bytes >> unit_shift,
1126 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1127 imagex_printf(T("\n"));
1129 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1130 imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1131 "to %"TS" \"%"TS"\"\n"),
1132 info->extract.image,
1133 info->extract.image_name,
1134 info->extract.wimfile_name,
1135 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1136 T("NTFS volume") : T("directory")),
1137 info->extract.target);
1139 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1140 percent_done = TO_PERCENT(info->extract.completed_bytes,
1141 info->extract.total_bytes);
1142 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1143 imagex_printf(T("\rExtracting files: "
1144 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1145 info->extract.completed_bytes >> unit_shift,
1147 info->extract.total_bytes >> unit_shift,
1150 if (info->extract.completed_bytes >= info->extract.total_bytes)
1151 imagex_printf(T("\n"));
1153 case WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN:
1154 if (info->extract.total_parts != 1) {
1155 imagex_printf(T("\nReading split pipable WIM part %u of %u\n"),
1156 info->extract.part_number,
1157 info->extract.total_parts);
1160 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1161 imagex_printf(T("Setting timestamps on all extracted files...\n"));
1163 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1164 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1165 imagex_printf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1166 info->extract.target);
1169 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1170 percent_done = TO_PERCENT(info->split.completed_bytes,
1171 info->split.total_bytes);
1172 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1173 imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
1174 "%"PRIu64" %"TS" (%u%%) written\n"),
1175 info->split.part_name,
1176 info->split.cur_part_number,
1177 info->split.total_parts,
1178 info->split.completed_bytes >> unit_shift,
1180 info->split.total_bytes >> unit_shift,
1184 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1185 if (info->split.completed_bytes == info->split.total_bytes) {
1186 imagex_printf(T("Finished writing split WIM part %u of %u\n"),
1187 info->split.cur_part_number,
1188 info->split.total_parts);
1191 case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1192 switch (info->update.command->op) {
1193 case WIMLIB_UPDATE_OP_DELETE:
1194 imagex_printf(T("Deleted WIM path \"%"TS"\"\n"),
1195 info->update.command->delete_.wim_path);
1197 case WIMLIB_UPDATE_OP_RENAME:
1198 imagex_printf(T("Renamed WIM path \"%"TS"\" => \"%"TS"\"\n"),
1199 info->update.command->rename.wim_source_path,
1200 info->update.command->rename.wim_target_path);
1202 case WIMLIB_UPDATE_OP_ADD:
1207 case WIMLIB_PROGRESS_MSG_REPLACE_FILE_IN_WIM:
1208 imagex_printf(T("Updating \"%"TS"\" in WIM image\n"),
1209 info->replace.path_in_wim);
1214 fflush(imagex_info_file);
1219 parse_num_threads(const tchar *optarg)
1222 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1223 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1224 imagex_error(T("Number of threads must be a non-negative integer!"));
1231 static uint32_t parse_chunk_size(const tchar *optarg)
1234 unsigned long chunk_size = tstrtoul(optarg, &tmp, 10);
1235 if (chunk_size >= UINT32_MAX || *tmp || tmp == optarg) {
1236 imagex_error(T("Chunk size must be a non-negative integer!"));
1245 * Parse an option passed to an update command.
1247 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1250 * @option: Text string for the option (beginning with --)
1252 * @cmd: `struct wimlib_update_command' that is being constructed for
1255 * Returns true if the option was recognized; false if not.
1258 update_command_add_option(int op, const tchar *option,
1259 struct wimlib_update_command *cmd)
1261 bool recognized = true;
1263 case WIMLIB_UPDATE_OP_ADD:
1264 if (!tstrcmp(option, T("--verbose")))
1265 cmd->add.add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
1266 else if (!tstrcmp(option, T("--unix-data")))
1267 cmd->add.add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
1268 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1269 cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
1270 else if (!tstrcmp(option, T("--strict-acls")))
1271 cmd->add.add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
1272 else if (!tstrcmp(option, T("--dereference")))
1273 cmd->add.add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
1274 else if (!tstrcmp(option, T("--no-replace")))
1275 cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_REPLACE;
1279 case WIMLIB_UPDATE_OP_DELETE:
1280 if (!tstrcmp(option, T("--force")))
1281 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1282 else if (!tstrcmp(option, T("--recursive")))
1283 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1294 /* How many nonoption arguments each `imagex update' command expects */
1295 static const unsigned update_command_num_nonoptions[] = {
1296 [WIMLIB_UPDATE_OP_ADD] = 2,
1297 [WIMLIB_UPDATE_OP_DELETE] = 1,
1298 [WIMLIB_UPDATE_OP_RENAME] = 2,
1302 update_command_add_nonoption(int op, const tchar *nonoption,
1303 struct wimlib_update_command *cmd,
1304 unsigned num_nonoptions)
1307 case WIMLIB_UPDATE_OP_ADD:
1308 if (num_nonoptions == 0)
1309 cmd->add.fs_source_path = (tchar*)nonoption;
1311 cmd->add.wim_target_path = (tchar*)nonoption;
1313 case WIMLIB_UPDATE_OP_DELETE:
1314 cmd->delete_.wim_path = (tchar*)nonoption;
1316 case WIMLIB_UPDATE_OP_RENAME:
1317 if (num_nonoptions == 0)
1318 cmd->rename.wim_source_path = (tchar*)nonoption;
1320 cmd->rename.wim_target_path = (tchar*)nonoption;
1326 * Parse a command passed on stdin to `imagex update'.
1328 * @line: Text of the command.
1329 * @len: Length of the line, including a null terminator
1332 * @command: A `struct wimlib_update_command' to fill in from the parsed
1335 * @line_number: Line number of the command, for diagnostics.
1337 * Returns true on success; returns false on parse error.
1340 parse_update_command(tchar *line, size_t len,
1341 struct wimlib_update_command *command,
1345 tchar *command_name;
1347 size_t num_nonoptions;
1349 /* Get the command name ("add", "delete", "rename") */
1350 ret = parse_string(&line, &len, &command_name);
1351 if (ret != PARSE_STRING_SUCCESS)
1354 if (!tstrcasecmp(command_name, T("add"))) {
1355 op = WIMLIB_UPDATE_OP_ADD;
1356 } else if (!tstrcasecmp(command_name, T("delete"))) {
1357 op = WIMLIB_UPDATE_OP_DELETE;
1358 } else if (!tstrcasecmp(command_name, T("rename"))) {
1359 op = WIMLIB_UPDATE_OP_RENAME;
1361 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1362 command_name, line_number);
1367 /* Parse additional options and non-options as needed */
1372 ret = parse_string(&line, &len, &next_string);
1373 if (ret == PARSE_STRING_NONE) /* End of line */
1375 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1377 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1379 if (!update_command_add_option(op, next_string, command))
1381 imagex_error(T("Unrecognized option \"%"TS"\" to "
1382 "update command \"%"TS"\" on line %zu"),
1383 next_string, command_name, line_number);
1389 if (num_nonoptions == update_command_num_nonoptions[op])
1391 imagex_error(T("Unexpected argument \"%"TS"\" in "
1392 "update command on line %zu\n"
1393 " (The \"%"TS"\" command only "
1394 "takes %zu nonoption arguments!)\n"),
1395 next_string, line_number,
1396 command_name, num_nonoptions);
1399 update_command_add_nonoption(op, next_string,
1400 command, num_nonoptions);
1405 if (num_nonoptions != update_command_num_nonoptions[op]) {
1406 imagex_error(T("Not enough arguments to update command "
1407 "\"%"TS"\" on line %zu"), command_name, line_number);
1413 static struct wimlib_update_command *
1414 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1415 size_t *num_cmds_ret)
1419 struct wimlib_update_command *cmds;
1422 nlines = text_file_count_lines(cmd_file_contents_p,
1427 /* Always allocate at least 1 slot, just in case the implementation of
1428 * calloc() returns NULL if 0 bytes are requested. */
1429 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1431 imagex_error(T("out of memory"));
1434 p = *cmd_file_contents_p;
1436 for (i = 0; i < nlines; i++) {
1437 /* XXX: Could use rawmemchr() here instead, but it may not be
1438 * available on all platforms. */
1439 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1440 size_t len = endp - p + 1;
1442 if (!is_comment_line(p, len)) {
1443 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1454 /* Apply one image, or all images, from a WIM file to a directory, OR apply
1455 * one image from a WIM file to a NTFS volume. */
1457 imagex_apply(int argc, tchar **argv, int cmd)
1461 int image = WIMLIB_NO_IMAGE;
1463 struct wimlib_wim_info info;
1465 const tchar *wimfile;
1466 const tchar *target;
1467 const tchar *image_num_or_name = NULL;
1468 int extract_flags = 0;
1470 STRING_SET(refglobs);
1472 for_opt(c, apply_options) {
1474 case IMAGEX_CHECK_OPTION:
1475 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1477 case IMAGEX_HARDLINK_OPTION:
1478 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1480 case IMAGEX_SYMLINK_OPTION:
1481 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1483 case IMAGEX_VERBOSE_OPTION:
1484 /* No longer does anything. */
1486 case IMAGEX_REF_OPTION:
1487 ret = string_set_append(&refglobs, optarg);
1489 goto out_free_refglobs;
1491 case IMAGEX_UNIX_DATA_OPTION:
1492 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1494 case IMAGEX_NO_ACLS_OPTION:
1495 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1497 case IMAGEX_STRICT_ACLS_OPTION:
1498 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1500 case IMAGEX_NO_ATTRIBUTES_OPTION:
1501 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES;
1503 case IMAGEX_NORPFIX_OPTION:
1504 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1506 case IMAGEX_RPFIX_OPTION:
1507 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1509 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1510 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1511 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1513 case IMAGEX_RESUME_OPTION:
1514 extract_flags |= WIMLIB_EXTRACT_FLAG_RESUME;
1516 case IMAGEX_WIMBOOT_OPTION:
1517 extract_flags |= WIMLIB_EXTRACT_FLAG_WIMBOOT;
1525 if (argc != 2 && argc != 3)
1530 if (!tstrcmp(wimfile, T("-"))) {
1531 /* Attempt to apply pipable WIM from standard input. */
1533 image_num_or_name = NULL;
1536 image_num_or_name = argv[1];
1541 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1542 imagex_progress_func);
1544 goto out_free_refglobs;
1546 wimlib_get_wim_info(wim, &info);
1549 /* Image explicitly specified. */
1550 image_num_or_name = argv[1];
1551 image = wimlib_resolve_image(wim, image_num_or_name);
1552 ret = verify_image_exists(image, image_num_or_name, wimfile);
1554 goto out_wimlib_free;
1557 /* No image specified; default to image 1, but only if the WIM
1558 * contains exactly one image. */
1560 if (info.image_count != 1) {
1561 imagex_error(T("\"%"TS"\" contains %d images; "
1562 "Please select one (or all)."),
1563 wimfile, info.image_count);
1572 if (refglobs.num_strings) {
1574 imagex_error(T("Can't specify --ref when applying from stdin!"));
1576 goto out_wimlib_free;
1578 ret = wim_reference_globs(wim, &refglobs, open_flags);
1580 goto out_wimlib_free;
1585 /* Interpret a regular file or block device target as a NTFS
1589 if (tstat(target, &stbuf)) {
1590 if (errno != ENOENT) {
1591 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1594 goto out_wimlib_free;
1597 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1598 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1604 ret = wimlib_extract_image(wim, image, target, extract_flags,
1605 imagex_progress_func);
1607 set_fd_to_binary_mode(STDIN_FILENO);
1608 ret = wimlib_extract_image_from_pipe(STDIN_FILENO,
1610 target, extract_flags,
1611 imagex_progress_func);
1614 imagex_printf(T("Done applying WIM image.\n"));
1615 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
1617 do_resource_not_found_warning(wimfile, &info, &refglobs);
1619 imagex_error(T( "If you are applying an image "
1620 "from a split pipable WIM,\n"
1621 " make sure you have "
1622 "concatenated together all parts."));
1628 string_set_destroy(&refglobs);
1632 usage(CMD_APPLY, stderr);
1634 goto out_free_refglobs;
1637 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1638 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1639 * the desired image. 'wimlib-imagex append': add a new image to an existing
1642 imagex_capture_or_append(int argc, tchar **argv, int cmd)
1645 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1646 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE |
1647 WIMLIB_ADD_IMAGE_FLAG_WINCONFIG |
1648 WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1649 int write_flags = 0;
1650 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
1651 uint32_t chunk_size = UINT32_MAX;
1652 uint32_t pack_chunk_size = UINT32_MAX;
1653 int pack_ctype = WIMLIB_COMPRESSION_TYPE_INVALID;
1654 const tchar *wimfile;
1658 const tchar *flags_element = NULL;
1661 STRING_SET(base_wimfiles);
1662 WIMStruct **base_wims;
1664 WIMStruct *template_wim;
1665 const tchar *template_wimfile = NULL;
1666 const tchar *template_image_name_or_num = NULL;
1667 int template_image = WIMLIB_NO_IMAGE;
1670 unsigned num_threads = 0;
1675 tchar *config_file = NULL;
1677 bool source_list = false;
1678 size_t source_list_nchars = 0;
1679 tchar *source_list_contents;
1680 bool capture_sources_malloced;
1681 struct wimlib_capture_source *capture_sources;
1683 bool name_defaulted;
1684 bool compress_slow = false;
1686 for_opt(c, capture_or_append_options) {
1688 case IMAGEX_BOOT_OPTION:
1689 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1691 case IMAGEX_CHECK_OPTION:
1692 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1693 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1695 case IMAGEX_NOCHECK_OPTION:
1696 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
1698 case IMAGEX_CONFIG_OPTION:
1699 config_file = optarg;
1700 add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1702 case IMAGEX_COMPRESS_OPTION:
1703 compression_type = get_compression_type(optarg);
1704 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1707 case IMAGEX_COMPRESS_SLOW_OPTION:
1708 compress_slow = true;
1710 case IMAGEX_CHUNK_SIZE_OPTION:
1711 chunk_size = parse_chunk_size(optarg);
1712 if (chunk_size == UINT32_MAX)
1715 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
1716 pack_chunk_size = parse_chunk_size(optarg);
1717 if (pack_chunk_size == UINT32_MAX)
1720 case IMAGEX_PACK_COMPRESS_OPTION:
1721 pack_ctype = get_compression_type(optarg);
1722 if (pack_ctype == WIMLIB_COMPRESSION_TYPE_INVALID)
1725 case IMAGEX_PACK_STREAMS_OPTION:
1726 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
1728 case IMAGEX_FLAGS_OPTION:
1729 flags_element = optarg;
1731 case IMAGEX_DEREFERENCE_OPTION:
1732 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1734 case IMAGEX_VERBOSE_OPTION:
1735 /* No longer does anything. */
1737 case IMAGEX_THREADS_OPTION:
1738 num_threads = parse_num_threads(optarg);
1739 if (num_threads == UINT_MAX)
1742 case IMAGEX_REBUILD_OPTION:
1743 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1745 case IMAGEX_UNIX_DATA_OPTION:
1746 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1748 case IMAGEX_SOURCE_LIST_OPTION:
1751 case IMAGEX_NO_ACLS_OPTION:
1752 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1754 case IMAGEX_STRICT_ACLS_OPTION:
1755 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1757 case IMAGEX_RPFIX_OPTION:
1758 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1760 case IMAGEX_NORPFIX_OPTION:
1761 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1763 case IMAGEX_PIPABLE_OPTION:
1764 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1766 case IMAGEX_NOT_PIPABLE_OPTION:
1767 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
1769 case IMAGEX_UPDATE_OF_OPTION:
1770 if (template_image_name_or_num) {
1771 imagex_error(T("'--update-of' can only be "
1772 "specified one time!"));
1776 colon = tstrrchr(optarg, T(':'));
1779 template_wimfile = optarg;
1781 template_image_name_or_num = colon + 1;
1783 template_wimfile = NULL;
1784 template_image_name_or_num = optarg;
1788 case IMAGEX_DELTA_FROM_OPTION:
1789 if (cmd != CMD_CAPTURE) {
1790 imagex_error(T("'--delta-from' is only "
1791 "valid for capture!"));
1794 ret = string_set_append(&base_wimfiles, optarg);
1796 goto out_free_base_wimfiles;
1797 write_flags |= WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS;
1799 case IMAGEX_WIMBOOT_OPTION:
1800 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_WIMBOOT;
1809 if (argc < 2 || argc > 4)
1815 /* Set default compression type and parameters. */
1818 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
1819 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_WIMBOOT) {
1820 compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
1822 compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1823 if (!compress_slow && pack_ctype != WIMLIB_COMPRESSION_TYPE_LZX) {
1824 struct wimlib_lzx_compressor_params params = {
1825 .hdr.size = sizeof(params),
1826 .algorithm = WIMLIB_LZX_ALGORITHM_FAST,
1829 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
1836 set_compress_slow();
1838 if (!tstrcmp(wimfile, T("-"))) {
1839 /* Writing captured WIM to standard output. */
1841 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
1842 imagex_error("Can't write a non-pipable WIM to "
1843 "standard output! Specify --pipable\n"
1844 " if you want to create a pipable WIM "
1845 "(but read the docs first).");
1849 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1851 if (cmd == CMD_APPEND) {
1852 imagex_error(T("Using standard output for append does "
1853 "not make sense."));
1856 wim_fd = STDOUT_FILENO;
1858 imagex_info_file = stderr;
1859 set_fd_to_binary_mode(wim_fd);
1862 /* If template image was specified using --update-of=IMAGE rather
1863 * than --update-of=WIMFILE:IMAGE, set the default WIMFILE. */
1864 if (template_image_name_or_num && !template_wimfile) {
1865 if (base_wimfiles.num_strings == 1) {
1866 /* Capturing delta WIM based on single WIM: default to
1868 template_wimfile = base_wimfiles.strings[0];
1869 } else if (cmd == CMD_APPEND) {
1870 /* Appending to WIM: default to WIM being appended to.
1872 template_wimfile = wimfile;
1874 /* Capturing a normal (non-delta) WIM, so the WIM file
1875 * *must* be explicitly specified. */
1876 if (base_wimfiles.num_strings > 1) {
1877 imagex_error(T("For capture of delta WIM "
1878 "based on multiple existing "
1880 " '--update-of' must "
1881 "specify WIMFILE:IMAGE!"));
1883 imagex_error(T("For capture of non-delta WIM, "
1884 "'--update-of' must specify "
1893 name_defaulted = false;
1895 /* Set default name to SOURCE argument, omitting any directory
1896 * prefixes and trailing slashes. This requires making a copy
1897 * of @source. Leave some free characters at the end in case we
1898 * append a number to keep the name unique. */
1899 size_t source_name_len;
1901 source_name_len = tstrlen(source);
1902 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1903 name = tbasename(tstrcpy(source_copy, source));
1904 name_defaulted = true;
1906 /* Image description defaults to NULL if not given. */
1913 /* Set up capture sources in source list mode */
1914 if (source[0] == T('-') && source[1] == T('\0')) {
1915 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1917 source_list_contents = file_get_text_contents(source,
1918 &source_list_nchars);
1920 if (!source_list_contents)
1923 capture_sources = parse_source_list(&source_list_contents,
1926 if (!capture_sources) {
1928 goto out_free_source_list_contents;
1930 capture_sources_malloced = true;
1932 /* Set up capture source in non-source-list mode. */
1933 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1934 capture_sources[0].fs_source_path = source;
1935 capture_sources[0].wim_target_path = WIMLIB_WIM_ROOT_PATH;
1936 capture_sources[0].reserved = 0;
1938 capture_sources_malloced = false;
1939 source_list_contents = NULL;
1942 /* Open the existing WIM, or create a new one. */
1943 if (cmd == CMD_APPEND)
1944 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1945 imagex_progress_func);
1947 ret = wimlib_create_new_wim(compression_type, &wim);
1949 goto out_free_capture_sources;
1951 /* Set chunk size if non-default. */
1952 if (chunk_size != UINT32_MAX) {
1953 ret = wimlib_set_output_chunk_size(wim, chunk_size);
1956 } else if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_WIMBOOT) &&
1957 compression_type == WIMLIB_COMPRESSION_TYPE_XPRESS) {
1958 ret = wimlib_set_output_chunk_size(wim, 4096);
1962 if (pack_ctype != WIMLIB_COMPRESSION_TYPE_INVALID) {
1963 ret = wimlib_set_output_pack_compression_type(wim, pack_ctype);
1967 if (pack_chunk_size != UINT32_MAX) {
1968 ret = wimlib_set_output_pack_chunk_size(wim, pack_chunk_size);
1974 /* Detect if source is regular file or block device and set NTFS volume
1979 if (tstat(source, &stbuf) == 0) {
1980 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1981 imagex_printf(T("Capturing WIM image from NTFS "
1982 "filesystem on \"%"TS"\"\n"), source);
1983 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1986 if (errno != ENOENT) {
1987 imagex_error_with_errno(T("Failed to stat "
1988 "\"%"TS"\""), source);
1996 /* If the user did not specify an image name, and the basename of the
1997 * source already exists as an image name in the WIM file, append a
1998 * suffix to make it unique. */
1999 if (cmd == CMD_APPEND && name_defaulted) {
2000 unsigned long conflict_idx;
2001 tchar *name_end = tstrchr(name, T('\0'));
2002 for (conflict_idx = 1;
2003 wimlib_image_name_in_use(wim, name);
2006 tsprintf(name_end, T(" (%lu)"), conflict_idx);
2010 /* If capturing a delta WIM, reference resources from the base WIMs
2011 * before adding the new image. */
2012 if (base_wimfiles.num_strings) {
2013 base_wims = calloc(base_wimfiles.num_strings,
2014 sizeof(base_wims[0]));
2015 if (base_wims == NULL) {
2016 imagex_error(T("Out of memory!"));
2021 for (size_t i = 0; i < base_wimfiles.num_strings; i++) {
2022 ret = wimlib_open_wim(base_wimfiles.strings[i],
2023 open_flags, &base_wims[i],
2024 imagex_progress_func);
2026 goto out_free_base_wims;
2030 ret = wimlib_reference_resources(wim, base_wims,
2031 base_wimfiles.num_strings, 0);
2033 goto out_free_base_wims;
2035 if (base_wimfiles.num_strings == 1) {
2036 imagex_printf(T("Capturing delta WIM based on \"%"TS"\"\n"),
2037 base_wimfiles.strings[0]);
2039 imagex_printf(T("Capturing delta WIM based on %u WIMs\n"),
2040 base_wimfiles.num_strings);
2047 /* If capturing or appending as an update of an existing (template) image,
2048 * open the WIM if needed and parse the image index. */
2049 if (template_image_name_or_num) {
2052 if (base_wimfiles.num_strings == 1 &&
2053 template_wimfile == base_wimfiles.strings[0]) {
2054 template_wim = base_wims[0];
2055 } else if (template_wimfile == wimfile) {
2058 ret = wimlib_open_wim(template_wimfile, open_flags,
2059 &template_wim, imagex_progress_func);
2061 goto out_free_base_wims;
2064 template_image = wimlib_resolve_image(template_wim,
2065 template_image_name_or_num);
2067 if (template_image_name_or_num[0] == T('-')) {
2070 struct wimlib_wim_info info;
2072 wimlib_get_wim_info(template_wim, &info);
2073 n = tstrtoul(template_image_name_or_num + 1, &tmp, 10);
2074 if (n >= 1 && n <= info.image_count &&
2076 tmp != template_image_name_or_num + 1)
2078 template_image = info.image_count - (n - 1);
2081 ret = verify_image_exists_and_is_single(template_image,
2082 template_image_name_or_num,
2085 goto out_free_template_wim;
2087 template_wim = NULL;
2090 ret = wimlib_add_image_multisource(wim,
2096 imagex_progress_func);
2098 goto out_free_template_wim;
2100 if (desc || flags_element || template_image_name_or_num) {
2101 /* User provided <DESCRIPTION> or <FLAGS> element, or an image
2102 * on which the added one is to be based has been specified with
2103 * --update-of. Get the index of the image we just
2104 * added, then use it to call the appropriate functions. */
2105 struct wimlib_wim_info info;
2107 wimlib_get_wim_info(wim, &info);
2110 ret = wimlib_set_image_descripton(wim,
2114 goto out_free_template_wim;
2117 if (flags_element) {
2118 ret = wimlib_set_image_flags(wim, info.image_count,
2121 goto out_free_template_wim;
2124 /* Reference template image if the user provided one. */
2125 if (template_image_name_or_num) {
2126 imagex_printf(T("Using image %d "
2127 "from \"%"TS"\" as template\n"),
2128 template_image, template_wimfile);
2129 ret = wimlib_reference_template_image(wim,
2135 goto out_free_template_wim;
2139 /* Write the new WIM or overwrite the existing WIM with the new image
2141 if (cmd == CMD_APPEND) {
2142 ret = wimlib_overwrite(wim, write_flags, num_threads,
2143 imagex_progress_func);
2144 } else if (wimfile) {
2145 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
2146 write_flags, num_threads,
2147 imagex_progress_func);
2149 ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
2150 write_flags, num_threads,
2151 imagex_progress_func);
2153 out_free_template_wim:
2154 /* template_wim may alias base_wims[0] or wim. */
2155 if ((base_wimfiles.num_strings != 1 || template_wim != base_wims[0]) &&
2156 template_wim != wim)
2157 wimlib_free(template_wim);
2159 for (size_t i = 0; i < base_wimfiles.num_strings; i++)
2160 wimlib_free(base_wims[i]);
2164 out_free_capture_sources:
2165 if (capture_sources_malloced)
2166 free(capture_sources);
2167 out_free_source_list_contents:
2168 free(source_list_contents);
2169 out_free_base_wimfiles:
2170 string_set_destroy(&base_wimfiles);
2177 goto out_free_base_wimfiles;
2180 /* Remove image(s) from a WIM. */
2182 imagex_delete(int argc, tchar **argv, int cmd)
2185 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2186 int write_flags = 0;
2187 const tchar *wimfile;
2188 const tchar *image_num_or_name;
2193 for_opt(c, delete_options) {
2195 case IMAGEX_CHECK_OPTION:
2196 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2197 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2199 case IMAGEX_SOFT_OPTION:
2200 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
2211 imagex_error(T("Must specify a WIM file"));
2213 imagex_error(T("Must specify an image"));
2217 image_num_or_name = argv[1];
2219 ret = wimlib_open_wim(wimfile, open_flags, &wim,
2220 imagex_progress_func);
2224 image = wimlib_resolve_image(wim, image_num_or_name);
2226 ret = verify_image_exists(image, image_num_or_name, wimfile);
2228 goto out_wimlib_free;
2230 ret = wimlib_delete_image(wim, image);
2232 imagex_error(T("Failed to delete image from \"%"TS"\""),
2234 goto out_wimlib_free;
2237 ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
2239 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2240 "deleted"), wimfile);
2248 usage(CMD_DELETE, stderr);
2253 struct print_dentry_options {
2258 print_dentry_full_path(const struct wimlib_dir_entry *dentry)
2260 tprintf(T("%"TS"\n"), dentry->full_path);
2263 static const struct {
2266 } file_attr_flags[] = {
2267 {WIMLIB_FILE_ATTRIBUTE_READONLY, T("READONLY")},
2268 {WIMLIB_FILE_ATTRIBUTE_HIDDEN, T("HIDDEN")},
2269 {WIMLIB_FILE_ATTRIBUTE_SYSTEM, T("SYSTEM")},
2270 {WIMLIB_FILE_ATTRIBUTE_DIRECTORY, T("DIRECTORY")},
2271 {WIMLIB_FILE_ATTRIBUTE_ARCHIVE, T("ARCHIVE")},
2272 {WIMLIB_FILE_ATTRIBUTE_DEVICE, T("DEVICE")},
2273 {WIMLIB_FILE_ATTRIBUTE_NORMAL, T("NORMAL")},
2274 {WIMLIB_FILE_ATTRIBUTE_TEMPORARY, T("TEMPORARY")},
2275 {WIMLIB_FILE_ATTRIBUTE_SPARSE_FILE, T("SPARSE_FILE")},
2276 {WIMLIB_FILE_ATTRIBUTE_REPARSE_POINT, T("REPARSE_POINT")},
2277 {WIMLIB_FILE_ATTRIBUTE_COMPRESSED, T("COMPRESSED")},
2278 {WIMLIB_FILE_ATTRIBUTE_OFFLINE, T("OFFLINE")},
2279 {WIMLIB_FILE_ATTRIBUTE_NOT_CONTENT_INDEXED, T("NOT_CONTENT_INDEXED")},
2280 {WIMLIB_FILE_ATTRIBUTE_ENCRYPTED, T("ENCRYPTED")},
2281 {WIMLIB_FILE_ATTRIBUTE_VIRTUAL, T("VIRTUAL")},
2284 #define TIMESTR_MAX 100
2287 timespec_to_string(const struct timespec *spec, tchar *buf)
2289 time_t t = spec->tv_sec;
2292 tstrftime(buf, TIMESTR_MAX, T("%a %b %d %H:%M:%S %Y UTC"), &tm);
2293 buf[TIMESTR_MAX - 1] = '\0';
2297 print_time(const tchar *type, const struct timespec *spec)
2299 tchar timestr[TIMESTR_MAX];
2301 timespec_to_string(spec, timestr);
2303 tprintf(T("%-20"TS"= %"TS"\n"), type, timestr);
2306 static void print_byte_field(const uint8_t field[], size_t len)
2309 tprintf(T("%02hhx"), *field++);
2313 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2315 tputs(T("WIM Information:"));
2316 tputs(T("----------------"));
2317 tprintf(T("Path: %"TS"\n"), wimfile);
2318 tprintf(T("GUID: 0x"));
2319 print_byte_field(info->guid, sizeof(info->guid));
2321 tprintf(T("Version: %u\n"), info->wim_version);
2322 tprintf(T("Image Count: %d\n"), info->image_count);
2323 tprintf(T("Compression: %"TS"\n"),
2324 wimlib_get_compression_type_string(info->compression_type));
2325 tprintf(T("Chunk Size: %"PRIu32" bytes\n"),
2327 tprintf(T("Part Number: %d/%d\n"), info->part_number, info->total_parts);
2328 tprintf(T("Boot Index: %d\n"), info->boot_index);
2329 tprintf(T("Size: %"PRIu64" bytes\n"), info->total_bytes);
2330 tprintf(T("Integrity Info: %"TS"\n"),
2331 info->has_integrity_table ? T("yes") : T("no"));
2332 tprintf(T("Relative path junction: %"TS"\n"),
2333 info->has_rpfix ? T("yes") : T("no"));
2334 tprintf(T("Pipable: %"TS"\n"),
2335 info->pipable ? T("yes") : T("no"));
2340 print_resource(const struct wimlib_resource_entry *resource,
2343 tprintf(T("Hash = 0x"));
2344 print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2347 if (!resource->is_missing) {
2348 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
2349 resource->uncompressed_size);
2350 if (resource->packed) {
2351 tprintf(T("Raw compressed size = %"PRIu64" bytes\n"),
2352 resource->raw_resource_compressed_size);
2354 tprintf(T("Raw offset in WIM = %"PRIu64" bytes\n"),
2355 resource->raw_resource_offset_in_wim);
2357 tprintf(T("Offset in raw = %"PRIu64" bytes\n"),
2360 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
2361 resource->compressed_size);
2363 tprintf(T("Offset in WIM = %"PRIu64" bytes\n"),
2367 tprintf(T("Part Number = %u\n"), resource->part_number);
2368 tprintf(T("Reference Count = %u\n"), resource->reference_count);
2370 tprintf(T("Flags = "));
2371 if (resource->is_compressed)
2372 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED "));
2373 if (resource->is_metadata)
2374 tprintf(T("WIM_RESHDR_FLAG_METADATA "));
2375 if (resource->is_free)
2376 tprintf(T("WIM_RESHDR_FLAG_FREE "));
2377 if (resource->is_spanned)
2378 tprintf(T("WIM_RESHDR_FLAG_SPANNED "));
2379 if (resource->packed)
2380 tprintf(T("WIM_RESHDR_FLAG_PACKED_STREAMS "));
2388 print_lookup_table(WIMStruct *wim)
2390 wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2394 default_print_security_descriptor(const uint8_t *sd, size_t size)
2396 tprintf(T("Security Descriptor = "));
2397 print_byte_field(sd, size);
2402 print_dentry_detailed(const struct wimlib_dir_entry *dentry)
2406 "----------------------------------------------------------------------------\n"));
2407 tprintf(T("Full Path = \"%"TS"\"\n"), dentry->full_path);
2408 if (dentry->dos_name)
2409 tprintf(T("Short Name = \"%"TS"\"\n"), dentry->dos_name);
2410 tprintf(T("Attributes = 0x%08x\n"), dentry->attributes);
2411 for (size_t i = 0; i < ARRAY_LEN(file_attr_flags); i++)
2412 if (file_attr_flags[i].flag & dentry->attributes)
2413 tprintf(T(" FILE_ATTRIBUTE_%"TS" is set\n"),
2414 file_attr_flags[i].name);
2416 if (dentry->security_descriptor) {
2417 print_security_descriptor(dentry->security_descriptor,
2418 dentry->security_descriptor_size);
2421 print_time(T("Creation Time"), &dentry->creation_time);
2422 print_time(T("Last Write Time"), &dentry->last_write_time);
2423 print_time(T("Last Access Time"), &dentry->last_access_time);
2426 if (dentry->attributes & WIMLIB_FILE_ATTRIBUTE_REPARSE_POINT)
2427 tprintf(T("Reparse Tag = 0x%"PRIx32"\n"), dentry->reparse_tag);
2429 tprintf(T("Link Group ID = 0x%016"PRIx64"\n"), dentry->hard_link_group_id);
2430 tprintf(T("Link Count = %"PRIu32"\n"), dentry->num_links);
2432 for (uint32_t i = 0; i <= dentry->num_named_streams; i++) {
2433 if (dentry->streams[i].stream_name) {
2434 tprintf(T("\tData stream \"%"TS"\":\n"),
2435 dentry->streams[i].stream_name);
2437 tprintf(T("\tUnnamed data stream:\n"));
2439 print_resource(&dentry->streams[i].resource, NULL);
2444 print_dentry(const struct wimlib_dir_entry *dentry, void *_options)
2446 const struct print_dentry_options *options = _options;
2447 if (!options->detailed)
2448 print_dentry_full_path(dentry);
2450 print_dentry_detailed(dentry);
2454 /* Print the files contained in an image(s) in a WIM file. */
2456 imagex_dir(int argc, tchar **argv, int cmd)
2458 const tchar *wimfile;
2459 WIMStruct *wim = NULL;
2462 const tchar *path = WIMLIB_WIM_ROOT_PATH;
2464 struct print_dentry_options options = {
2467 int iterate_flags = WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE;
2469 for_opt(c, dir_options) {
2471 case IMAGEX_PATH_OPTION:
2474 case IMAGEX_DETAILED_OPTION:
2475 options.detailed = true;
2477 case IMAGEX_ONE_FILE_ONLY_OPTION:
2478 iterate_flags &= ~WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE;
2488 imagex_error(T("Must specify a WIM file"));
2492 imagex_error(T("Too many arguments"));
2497 ret = wimlib_open_wim(wimfile, 0, &wim, imagex_progress_func);
2502 image = wimlib_resolve_image(wim, argv[1]);
2503 ret = verify_image_exists(image, argv[1], wimfile);
2505 goto out_wimlib_free;
2507 /* No image specified; default to image 1, but only if the WIM
2508 * contains exactly one image. */
2510 struct wimlib_wim_info info;
2512 wimlib_get_wim_info(wim, &info);
2513 if (info.image_count != 1) {
2514 imagex_error(T("\"%"TS"\" contains %d images; Please "
2515 "select one (or all)."),
2516 wimfile, info.image_count);
2523 ret = wimlib_iterate_dir_tree(wim, image, path, iterate_flags,
2524 print_dentry, &options);
2531 usage(CMD_DIR, stderr);
2536 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2539 imagex_export(int argc, tchar **argv, int cmd)
2543 int export_flags = 0;
2544 int write_flags = 0;
2545 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2546 const tchar *src_wimfile;
2547 const tchar *src_image_num_or_name;
2548 const tchar *dest_wimfile;
2550 const tchar *dest_name;
2551 const tchar *dest_desc;
2553 struct wimlib_wim_info src_info;
2554 WIMStruct *dest_wim;
2559 STRING_SET(refglobs);
2560 unsigned num_threads = 0;
2561 uint32_t chunk_size = UINT32_MAX;
2562 uint32_t pack_chunk_size = UINT32_MAX;
2563 int pack_ctype = WIMLIB_COMPRESSION_TYPE_INVALID;
2565 for_opt(c, export_options) {
2567 case IMAGEX_BOOT_OPTION:
2568 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2570 case IMAGEX_CHECK_OPTION:
2571 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2572 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2574 case IMAGEX_NOCHECK_OPTION:
2575 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2577 case IMAGEX_COMPRESS_OPTION:
2578 compression_type = get_compression_type(optarg);
2579 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2582 case IMAGEX_COMPRESS_SLOW_OPTION:
2583 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
2584 set_compress_slow();
2586 case IMAGEX_PACK_STREAMS_OPTION:
2587 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
2589 case IMAGEX_CHUNK_SIZE_OPTION:
2590 chunk_size = parse_chunk_size(optarg);
2591 if (chunk_size == UINT32_MAX)
2594 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
2595 pack_chunk_size = parse_chunk_size(optarg);
2596 if (pack_chunk_size == UINT32_MAX)
2599 case IMAGEX_PACK_COMPRESS_OPTION:
2600 pack_ctype = get_compression_type(optarg);
2601 if (pack_ctype == WIMLIB_COMPRESSION_TYPE_INVALID)
2604 case IMAGEX_REF_OPTION:
2605 ret = string_set_append(&refglobs, optarg);
2607 goto out_free_refglobs;
2609 case IMAGEX_THREADS_OPTION:
2610 num_threads = parse_num_threads(optarg);
2611 if (num_threads == UINT_MAX)
2614 case IMAGEX_REBUILD_OPTION:
2615 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2617 case IMAGEX_PIPABLE_OPTION:
2618 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2620 case IMAGEX_NOT_PIPABLE_OPTION:
2621 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
2629 if (argc < 3 || argc > 5)
2631 src_wimfile = argv[0];
2632 src_image_num_or_name = argv[1];
2633 dest_wimfile = argv[2];
2634 dest_name = (argc >= 4) ? argv[3] : NULL;
2635 dest_desc = (argc >= 5) ? argv[4] : NULL;
2636 ret = wimlib_open_wim(src_wimfile, open_flags, &src_wim,
2637 imagex_progress_func);
2639 goto out_free_refglobs;
2641 wimlib_get_wim_info(src_wim, &src_info);
2643 /* Determine if the destination is an existing file or not. If so, we
2644 * try to append the exported image(s) to it; otherwise, we create a new
2645 * WIM containing the exported image(s). Furthermore, determine if we
2646 * need to write a pipable WIM directly to standard output. */
2648 if (tstrcmp(dest_wimfile, T("-")) == 0) {
2650 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2651 imagex_error("Can't write a non-pipable WIM to "
2652 "standard output! Specify --pipable\n"
2653 " if you want to create a pipable WIM "
2654 "(but read the docs first).");
2656 goto out_free_src_wim;
2659 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2661 dest_wimfile = NULL;
2662 dest_wim_fd = STDOUT_FILENO;
2663 imagex_info_file = stderr;
2664 set_fd_to_binary_mode(dest_wim_fd);
2667 if (dest_wimfile != NULL && tstat(dest_wimfile, &stbuf) == 0) {
2669 /* Destination file exists. */
2671 if (!S_ISREG(stbuf.st_mode)) {
2672 imagex_error(T("\"%"TS"\" is not a regular file"),
2675 goto out_free_src_wim;
2677 ret = wimlib_open_wim(dest_wimfile,
2678 open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2679 &dest_wim, imagex_progress_func);
2681 goto out_free_src_wim;
2683 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2684 /* The user specified a compression type, but we're
2685 * exporting to an existing WIM. Make sure the
2686 * specified compression type is the same as the
2687 * compression type of the existing destination WIM. */
2688 struct wimlib_wim_info dest_info;
2690 wimlib_get_wim_info(dest_wim, &dest_info);
2691 if (compression_type != dest_info.compression_type) {
2692 imagex_error(T("Cannot specify a compression type that is "
2693 "not the same as that used in the "
2694 "destination WIM"));
2696 goto out_free_dest_wim;
2702 if (errno != ENOENT) {
2703 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2706 goto out_free_src_wim;
2709 /* dest_wimfile is not an existing file, so create a new WIM. */
2711 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2712 /* The user did not specify a compression type; default
2713 * to that of the source WIM. */
2715 compression_type = src_info.compression_type;
2717 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2719 goto out_free_src_wim;
2721 /* Use same chunk size if compression type is the same. */
2722 if (compression_type == src_info.compression_type &&
2723 chunk_size == UINT32_MAX)
2724 wimlib_set_output_chunk_size(dest_wim, src_info.chunk_size);
2727 if (chunk_size != UINT32_MAX) {
2728 /* Set destination chunk size. */
2729 ret = wimlib_set_output_chunk_size(dest_wim, chunk_size);
2731 goto out_free_dest_wim;
2733 if (pack_ctype != WIMLIB_COMPRESSION_TYPE_INVALID) {
2734 ret = wimlib_set_output_pack_compression_type(dest_wim, pack_ctype);
2736 goto out_free_dest_wim;
2738 if (pack_chunk_size != UINT32_MAX) {
2739 ret = wimlib_set_output_pack_chunk_size(dest_wim, pack_chunk_size);
2741 goto out_free_dest_wim;
2744 image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2745 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2747 goto out_free_dest_wim;
2749 if (refglobs.num_strings) {
2750 ret = wim_reference_globs(src_wim, &refglobs, open_flags);
2752 goto out_free_dest_wim;
2755 if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
2756 image == WIMLIB_ALL_IMAGES && src_info.boot_index == 0)
2758 imagex_error(T("--boot specified for all-images export, but source WIM "
2759 "has no bootable image."));
2761 goto out_free_dest_wim;
2764 ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2765 dest_desc, export_flags, imagex_progress_func);
2767 if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2768 do_resource_not_found_warning(src_wimfile,
2769 &src_info, &refglobs);
2771 goto out_free_dest_wim;
2775 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2776 imagex_progress_func);
2777 else if (dest_wimfile)
2778 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2779 write_flags, num_threads,
2780 imagex_progress_func);
2782 ret = wimlib_write_to_fd(dest_wim, dest_wim_fd,
2783 WIMLIB_ALL_IMAGES, write_flags,
2784 num_threads, imagex_progress_func);
2786 wimlib_free(dest_wim);
2788 wimlib_free(src_wim);
2790 string_set_destroy(&refglobs);
2794 usage(CMD_EXPORT, stderr);
2797 goto out_free_refglobs;
2800 /* Extract files or directories from a WIM image */
2802 imagex_extract(int argc, tchar **argv, int cmd)
2809 const tchar *wimfile;
2810 const tchar *image_num_or_name;
2811 tchar *dest_dir = T(".");
2812 int extract_flags = WIMLIB_EXTRACT_FLAG_NORPFIX |
2813 WIMLIB_EXTRACT_FLAG_GLOB_PATHS |
2814 WIMLIB_EXTRACT_FLAG_STRICT_GLOB;
2815 int notlist_extract_flags = WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE;
2817 STRING_SET(refglobs);
2819 tchar *root_path = WIMLIB_WIM_ROOT_PATH;
2821 for_opt(c, extract_options) {
2823 case IMAGEX_CHECK_OPTION:
2824 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2826 case IMAGEX_VERBOSE_OPTION:
2827 /* No longer does anything. */
2829 case IMAGEX_REF_OPTION:
2830 ret = string_set_append(&refglobs, optarg);
2832 goto out_free_refglobs;
2834 case IMAGEX_UNIX_DATA_OPTION:
2835 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2837 case IMAGEX_NO_ACLS_OPTION:
2838 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2840 case IMAGEX_STRICT_ACLS_OPTION:
2841 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2843 case IMAGEX_NO_ATTRIBUTES_OPTION:
2844 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES;
2846 case IMAGEX_DEST_DIR_OPTION:
2849 case IMAGEX_TO_STDOUT_OPTION:
2850 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2851 imagex_info_file = stderr;
2852 imagex_be_quiet = true;
2854 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2855 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2856 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2858 case IMAGEX_NO_WILDCARDS_OPTION:
2859 extract_flags &= ~WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2861 case IMAGEX_NULLGLOB_OPTION:
2862 extract_flags &= ~WIMLIB_EXTRACT_FLAG_STRICT_GLOB;
2864 case IMAGEX_PRESERVE_DIR_STRUCTURE_OPTION:
2865 notlist_extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE;
2867 case IMAGEX_WIMBOOT_OPTION:
2868 extract_flags |= WIMLIB_EXTRACT_FLAG_WIMBOOT;
2880 if (!(extract_flags & (WIMLIB_EXTRACT_FLAG_GLOB_PATHS |
2881 WIMLIB_EXTRACT_FLAG_STRICT_GLOB)))
2883 imagex_error(T("Can't combine --no-wildcards and --nullglob!"));
2888 image_num_or_name = argv[1];
2893 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2895 goto out_free_refglobs;
2897 image = wimlib_resolve_image(wim, image_num_or_name);
2898 ret = verify_image_exists_and_is_single(image,
2902 goto out_wimlib_free;
2904 if (refglobs.num_strings) {
2905 ret = wim_reference_globs(wim, &refglobs, open_flags);
2907 goto out_wimlib_free;
2913 extract_flags &= ~WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2916 while (argc != 0 && ret == 0) {
2920 num_paths < argc && argv[num_paths][0] != T('@');
2925 ret = wimlib_extract_paths(wim, image, dest_dir,
2926 (const tchar **)argv,
2928 extract_flags | notlist_extract_flags,
2929 imagex_progress_func);
2933 ret = wimlib_extract_pathlist(wim, image, dest_dir,
2936 imagex_progress_func);
2943 if (!imagex_be_quiet)
2944 imagex_printf(T("Done extracting files.\n"));
2945 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2946 tfprintf(stderr, T("Note: You can use `%"TS"' to see what "
2947 "files and directories\n"
2948 " are in the WIM image.\n"),
2949 get_cmd_string(CMD_DIR, false));
2950 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2951 struct wimlib_wim_info info;
2953 wimlib_get_wim_info(wim, &info);
2954 do_resource_not_found_warning(wimfile, &info, &refglobs);
2959 string_set_destroy(&refglobs);
2963 usage(CMD_EXTRACT, stderr);
2966 goto out_free_refglobs;
2969 /* Prints information about a WIM file; also can mark an image as bootable,
2970 * change the name of an image, or change the description of an image. */
2972 imagex_info(int argc, tchar **argv, int cmd)
2977 bool nocheck = false;
2978 bool header = false;
2979 bool lookup_table = false;
2981 bool short_header = true;
2982 const tchar *xml_out_file = NULL;
2983 const tchar *wimfile;
2984 const tchar *image_num_or_name;
2985 const tchar *new_name;
2986 const tchar *new_desc;
2991 struct wimlib_wim_info info;
2993 for_opt(c, info_options) {
2995 case IMAGEX_BOOT_OPTION:
2998 case IMAGEX_CHECK_OPTION:
3001 case IMAGEX_NOCHECK_OPTION:
3004 case IMAGEX_HEADER_OPTION:
3006 short_header = false;
3008 case IMAGEX_LOOKUP_TABLE_OPTION:
3009 lookup_table = true;
3010 short_header = false;
3012 case IMAGEX_XML_OPTION:
3014 short_header = false;
3016 case IMAGEX_EXTRACT_XML_OPTION:
3017 xml_out_file = optarg;
3018 short_header = false;
3020 case IMAGEX_METADATA_OPTION:
3021 imagex_error(T("The --metadata option has been removed. "
3022 "Use 'wimdir --detail' instead."));
3031 if (argc < 1 || argc > 4)
3035 image_num_or_name = (argc >= 2) ? argv[1] : T("all");
3036 new_name = (argc >= 3) ? argv[2] : NULL;
3037 new_desc = (argc >= 4) ? argv[3] : NULL;
3039 if (check && nocheck) {
3040 imagex_error(T("Can't specify both --check and --nocheck"));
3045 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3047 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3051 wimlib_get_wim_info(wim, &info);
3053 image = wimlib_resolve_image(wim, image_num_or_name);
3054 ret = WIMLIB_ERR_INVALID_IMAGE;
3055 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
3056 verify_image_exists(image, image_num_or_name, wimfile);
3058 imagex_error(T("If you would like to set the boot "
3059 "index to 0, specify image \"0\" with "
3060 "the --boot flag."));
3062 goto out_wimlib_free;
3065 if (boot && info.image_count == 0) {
3066 imagex_error(T("--boot is meaningless on a WIM with no images"));
3067 goto out_wimlib_free;
3070 if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
3072 imagex_error(T("Cannot specify the --boot flag "
3073 "without specifying a specific "
3074 "image in a multi-image WIM"));
3075 goto out_wimlib_free;
3078 imagex_error(T("Cannot specify the NEW_NAME "
3079 "without specifying a specific "
3080 "image in a multi-image WIM"));
3081 goto out_wimlib_free;
3085 /* Operations that print information are separated from operations that
3086 * recreate the WIM file. */
3087 if (!new_name && !boot) {
3089 /* Read-only operations */
3091 if (image == WIMLIB_NO_IMAGE) {
3092 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
3093 image_num_or_name, wimfile);
3094 goto out_wimlib_free;
3097 if (image == WIMLIB_ALL_IMAGES && short_header)
3098 print_wim_information(wimfile, &info);
3101 wimlib_print_header(wim);
3104 if (info.total_parts != 1) {
3105 tfprintf(stderr, T("Warning: Only showing the lookup table "
3106 "for part %d of a %d-part WIM.\n"),
3107 info.part_number, info.total_parts);
3109 print_lookup_table(wim);
3113 ret = wimlib_extract_xml_data(wim, stdout);
3115 goto out_wimlib_free;
3121 fp = tfopen(xml_out_file, T("wb"));
3123 imagex_error_with_errno(T("Failed to open the "
3124 "file \"%"TS"\" for "
3128 goto out_wimlib_free;
3130 ret = wimlib_extract_xml_data(wim, fp);
3132 imagex_error(T("Failed to close the file "
3138 goto out_wimlib_free;
3142 wimlib_print_available_images(wim, image);
3147 /* Modification operations */
3149 if (image == WIMLIB_ALL_IMAGES)
3152 if (image == WIMLIB_NO_IMAGE && new_name) {
3153 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
3154 "when using image 0"), new_name);
3156 goto out_wimlib_free;
3160 if (image == info.boot_index) {
3161 imagex_printf(T("Image %d is already marked as "
3162 "bootable.\n"), image);
3165 imagex_printf(T("Marking image %d as bootable.\n"),
3167 info.boot_index = image;
3168 ret = wimlib_set_wim_info(wim, &info,
3169 WIMLIB_CHANGE_BOOT_INDEX);
3171 goto out_wimlib_free;
3175 if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
3177 imagex_printf(T("Image %d is already named \"%"TS"\".\n"),
3181 imagex_printf(T("Changing the name of image %d to "
3182 "\"%"TS"\".\n"), image, new_name);
3183 ret = wimlib_set_image_name(wim, image, new_name);
3185 goto out_wimlib_free;
3189 const tchar *old_desc;
3190 old_desc = wimlib_get_image_description(wim, image);
3191 if (old_desc && !tstrcmp(old_desc, new_desc)) {
3192 imagex_printf(T("The description of image %d is already "
3193 "\"%"TS"\".\n"), image, new_desc);
3196 imagex_printf(T("Changing the description of image %d "
3197 "to \"%"TS"\".\n"), image, new_desc);
3198 ret = wimlib_set_image_descripton(wim, image,
3201 goto out_wimlib_free;
3205 /* Only call wimlib_overwrite() if something actually needs to
3207 if (boot || new_name || new_desc ||
3208 (check && !info.has_integrity_table) ||
3209 (nocheck && info.has_integrity_table))
3211 int write_flags = 0;
3214 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3216 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3217 ret = wimlib_overwrite(wim, write_flags, 1,
3218 imagex_progress_func);
3220 imagex_printf(T("The file \"%"TS"\" was not modified "
3221 "because nothing needed to be done.\n"),
3232 usage(CMD_INFO, stderr);
3238 /* Join split WIMs into one part WIM */
3240 imagex_join(int argc, tchar **argv, int cmd)
3243 int swm_open_flags = 0;
3244 int wim_write_flags = 0;
3245 const tchar *output_path;
3248 for_opt(c, join_options) {
3250 case IMAGEX_CHECK_OPTION:
3251 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3252 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3262 imagex_error(T("Must specify one or more split WIM (.swm) "
3266 output_path = argv[0];
3267 ret = wimlib_join((const tchar * const *)++argv,
3272 imagex_progress_func);
3277 usage(CMD_JOIN, stderr);
3282 #if WIM_MOUNTING_SUPPORTED
3284 /* Mounts a WIM image. */
3286 imagex_mount_rw_or_ro(int argc, tchar **argv, int cmd)
3289 int mount_flags = 0;
3291 const tchar *staging_dir = NULL;
3292 const tchar *wimfile;
3295 struct wimlib_wim_info info;
3299 STRING_SET(refglobs);
3301 if (cmd == CMD_MOUNTRW) {
3302 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
3303 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3306 for_opt(c, mount_options) {
3308 case IMAGEX_ALLOW_OTHER_OPTION:
3309 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
3311 case IMAGEX_CHECK_OPTION:
3312 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3314 case IMAGEX_DEBUG_OPTION:
3315 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
3317 case IMAGEX_STREAMS_INTERFACE_OPTION:
3318 if (!tstrcasecmp(optarg, T("none")))
3319 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
3320 else if (!tstrcasecmp(optarg, T("xattr")))
3321 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
3322 else if (!tstrcasecmp(optarg, T("windows")))
3323 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
3325 imagex_error(T("Unknown stream interface \"%"TS"\""),
3330 case IMAGEX_REF_OPTION:
3331 ret = string_set_append(&refglobs, optarg);
3333 goto out_free_refglobs;
3335 case IMAGEX_STAGING_DIR_OPTION:
3336 staging_dir = optarg;
3338 case IMAGEX_UNIX_DATA_OPTION:
3339 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
3347 if (argc != 2 && argc != 3)
3352 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3354 goto out_free_refglobs;
3356 wimlib_get_wim_info(wim, &info);
3359 /* Image explicitly specified. */
3360 image = wimlib_resolve_image(wim, argv[1]);
3362 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
3366 /* No image specified; default to image 1, but only if the WIM
3367 * contains exactly one image. */
3369 if (info.image_count != 1) {
3370 imagex_error(T("\"%"TS"\" contains %d images; Please "
3371 "select one."), wimfile, info.image_count);
3379 if (refglobs.num_strings) {
3380 ret = wim_reference_globs(wim, &refglobs, open_flags);
3385 ret = wimlib_mount_image(wim, image, dir, mount_flags, staging_dir);
3387 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
3389 image, wimfile, dir);
3394 string_set_destroy(&refglobs);
3400 goto out_free_refglobs;
3402 #endif /* WIM_MOUNTING_SUPPORTED */
3404 /* Rebuild a WIM file */
3406 imagex_optimize(int argc, tchar **argv, int cmd)
3409 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3410 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
3411 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
3412 uint32_t chunk_size = UINT32_MAX;
3413 uint32_t pack_chunk_size = UINT32_MAX;
3414 int pack_ctype = WIMLIB_COMPRESSION_TYPE_INVALID;
3417 const tchar *wimfile;
3420 unsigned num_threads = 0;
3422 for_opt(c, optimize_options) {
3424 case IMAGEX_CHECK_OPTION:
3425 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3426 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3428 case IMAGEX_NOCHECK_OPTION:
3429 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3431 case IMAGEX_COMPRESS_OPTION:
3432 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3433 compression_type = get_compression_type(optarg);
3434 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
3437 case IMAGEX_RECOMPRESS_OPTION:
3438 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3440 case IMAGEX_COMPRESS_SLOW_OPTION:
3441 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3442 set_compress_slow();
3444 case IMAGEX_CHUNK_SIZE_OPTION:
3445 chunk_size = parse_chunk_size(optarg);
3446 if (chunk_size == UINT32_MAX)
3449 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
3450 pack_chunk_size = parse_chunk_size(optarg);
3451 if (pack_chunk_size == UINT32_MAX)
3454 case IMAGEX_PACK_COMPRESS_OPTION:
3455 pack_ctype = get_compression_type(optarg);
3456 if (pack_ctype == WIMLIB_COMPRESSION_TYPE_INVALID)
3459 case IMAGEX_PACK_STREAMS_OPTION:
3460 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
3461 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3463 case IMAGEX_THREADS_OPTION:
3464 num_threads = parse_num_threads(optarg);
3465 if (num_threads == UINT_MAX)
3468 case IMAGEX_PIPABLE_OPTION:
3469 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
3471 case IMAGEX_NOT_PIPABLE_OPTION:
3472 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
3486 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3490 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
3491 /* Change compression type. */
3492 ret = wimlib_set_output_compression_type(wim, compression_type);
3494 goto out_wimlib_free;
3497 if (chunk_size != UINT32_MAX) {
3498 /* Change chunk size. */
3499 ret = wimlib_set_output_chunk_size(wim, chunk_size);
3501 goto out_wimlib_free;
3503 if (pack_ctype != WIMLIB_COMPRESSION_TYPE_INVALID) {
3504 ret = wimlib_set_output_pack_compression_type(wim, pack_ctype);
3506 goto out_wimlib_free;
3508 if (pack_chunk_size != UINT32_MAX) {
3509 ret = wimlib_set_output_pack_chunk_size(wim, pack_chunk_size);
3511 goto out_wimlib_free;
3514 old_size = file_get_size(wimfile);
3515 tprintf(T("\"%"TS"\" original size: "), wimfile);
3517 tputs(T("Unknown"));
3519 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3521 ret = wimlib_overwrite(wim, write_flags, num_threads,
3522 imagex_progress_func);
3524 imagex_error(T("Optimization of \"%"TS"\" failed."), wimfile);
3525 goto out_wimlib_free;
3528 new_size = file_get_size(wimfile);
3529 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3531 tputs(T("Unknown"));
3533 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3535 tfputs(T("Space saved: "), stdout);
3536 if (new_size != -1 && old_size != -1) {
3537 tprintf(T("%lld KiB\n"),
3538 ((long long)old_size - (long long)new_size) >> 10);
3540 tputs(T("Unknown"));
3549 usage(CMD_OPTIMIZE, stderr);
3555 /* Split a WIM into a spanned set */
3557 imagex_split(int argc, tchar **argv, int cmd)
3561 int write_flags = 0;
3562 unsigned long part_size;
3567 for_opt(c, split_options) {
3569 case IMAGEX_CHECK_OPTION:
3570 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3571 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3583 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3584 if (tmp == argv[2] || *tmp) {
3585 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3586 imagex_error(T("The part size must be an integer or "
3587 "floating-point number of megabytes."));
3590 ret = wimlib_open_wim(argv[0], open_flags, &wim, imagex_progress_func);
3594 ret = wimlib_split(wim, argv[1], part_size, write_flags, imagex_progress_func);
3600 usage(CMD_SPLIT, stderr);
3606 #if WIM_MOUNTING_SUPPORTED
3607 /* Unmounts a mounted WIM image. */
3609 imagex_unmount(int argc, tchar **argv, int cmd)
3612 int unmount_flags = 0;
3615 for_opt(c, unmount_options) {
3617 case IMAGEX_COMMIT_OPTION:
3618 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3620 case IMAGEX_CHECK_OPTION:
3621 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3623 case IMAGEX_REBUILD_OPTION:
3624 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3626 case IMAGEX_LAZY_OPTION:
3627 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3629 case IMAGEX_NEW_IMAGE_OPTION:
3630 unmount_flags |= WIMLIB_UNMOUNT_FLAG_NEW_IMAGE;
3641 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_NEW_IMAGE) {
3642 if (!(unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT)) {
3643 imagex_error(T("--new-image is meaningless "
3644 "without --commit also specified!"));
3647 imagex_printf(T("Committing changes as new image...\n"));
3650 ret = wimlib_unmount_image(argv[0], unmount_flags,
3651 imagex_progress_func);
3653 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3658 usage(CMD_UNMOUNT, stderr);
3663 #endif /* WIM_MOUNTING_SUPPORTED */
3666 * Add, delete, or rename files in a WIM image.
3669 imagex_update(int argc, tchar **argv, int cmd)
3671 const tchar *wimfile;
3675 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3676 int write_flags = 0;
3677 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3678 int default_add_flags = WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
3679 WIMLIB_ADD_FLAG_VERBOSE |
3680 WIMLIB_ADD_FLAG_WINCONFIG;
3681 int default_delete_flags = 0;
3682 unsigned num_threads = 0;
3684 tchar *cmd_file_contents;
3685 size_t cmd_file_nchars;
3686 struct wimlib_update_command *cmds;
3688 tchar *command_str = NULL;
3689 tchar *config_file = NULL;
3690 tchar *wimboot_config = NULL;
3692 for_opt(c, update_options) {
3694 /* Generic or write options */
3695 case IMAGEX_THREADS_OPTION:
3696 num_threads = parse_num_threads(optarg);
3697 if (num_threads == UINT_MAX)
3700 case IMAGEX_CHECK_OPTION:
3701 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3702 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3704 case IMAGEX_REBUILD_OPTION:
3705 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3707 case IMAGEX_COMMAND_OPTION:
3709 imagex_error(T("--command may only be specified "
3710 "one time. Please provide\n"
3711 " the update commands "
3712 "on standard input instead."));
3715 command_str = tstrdup(optarg);
3717 imagex_error(T("Out of memory!"));
3721 case IMAGEX_WIMBOOT_CONFIG_OPTION:
3722 wimboot_config = optarg;
3724 /* Default delete options */
3725 case IMAGEX_FORCE_OPTION:
3726 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3728 case IMAGEX_RECURSIVE_OPTION:
3729 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3732 /* Global add option */
3733 case IMAGEX_CONFIG_OPTION:
3734 default_add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
3735 config_file = optarg;
3738 /* Default add options */
3739 case IMAGEX_VERBOSE_OPTION:
3740 /* No longer does anything. */
3742 case IMAGEX_DEREFERENCE_OPTION:
3743 default_add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
3745 case IMAGEX_UNIX_DATA_OPTION:
3746 default_add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
3748 case IMAGEX_NO_ACLS_OPTION:
3749 default_add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
3751 case IMAGEX_STRICT_ACLS_OPTION:
3752 default_add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
3754 case IMAGEX_NO_REPLACE_OPTION:
3755 default_add_flags |= WIMLIB_ADD_FLAG_NO_REPLACE;
3764 if (argc != 1 && argc != 2)
3768 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3770 goto out_free_command_str;
3773 /* Image explicitly specified. */
3774 image = wimlib_resolve_image(wim, argv[1]);
3775 ret = verify_image_exists_and_is_single(image, argv[1],
3778 goto out_wimlib_free;
3780 /* No image specified; default to image 1, but only if the WIM
3781 * contains exactly one image. */
3782 struct wimlib_wim_info info;
3784 wimlib_get_wim_info(wim, &info);
3785 if (info.image_count != 1) {
3786 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3787 wimfile, info.image_count);
3794 /* Read update commands from standard input, or the command string if
3797 cmd_file_contents = NULL;
3798 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3802 goto out_free_cmd_file_contents;
3804 } else if (!wimboot_config) {
3805 if (isatty(STDIN_FILENO)) {
3806 tputs(T("Reading update commands from standard input..."));
3807 recommend_man_page(CMD_UPDATE, stdout);
3809 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3810 if (!cmd_file_contents) {
3812 goto out_wimlib_free;
3815 /* Parse the update commands */
3816 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3820 goto out_free_cmd_file_contents;
3823 cmd_file_contents = NULL;
3828 /* Set default flags and capture config on the update commands */
3829 for (size_t i = 0; i < num_cmds; i++) {
3830 switch (cmds[i].op) {
3831 case WIMLIB_UPDATE_OP_ADD:
3832 cmds[i].add.add_flags |= default_add_flags;
3833 cmds[i].add.config_file = config_file;
3835 case WIMLIB_UPDATE_OP_DELETE:
3836 cmds[i].delete_.delete_flags |= default_delete_flags;
3843 /* Execute the update commands */
3844 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3845 imagex_progress_func);
3849 if (wimboot_config) {
3850 /* --wimboot-config=FILE is short for an
3851 * "add FILE /Windows/System32/WimBootCompress.ini" command.
3853 struct wimlib_update_command cmd;
3855 cmd.op = WIMLIB_UPDATE_OP_ADD;
3856 cmd.add.fs_source_path = wimboot_config;
3857 cmd.add.wim_target_path = T("/Windows/System32/WimBootCompress.ini");
3858 cmd.add.config_file = NULL;
3859 cmd.add.add_flags = 0;
3861 ret = wimlib_update_image(wim, image, &cmd, 1,
3862 update_flags, imagex_progress_func);
3867 /* Overwrite the updated WIM */
3868 ret = wimlib_overwrite(wim, write_flags, num_threads,
3869 imagex_progress_func);
3872 out_free_cmd_file_contents:
3873 free(cmd_file_contents);
3876 out_free_command_str:
3881 usage(CMD_UPDATE, stderr);
3884 goto out_free_command_str;
3889 struct imagex_command {
3891 int (*func)(int argc, tchar **argv, int cmd);
3894 static const struct imagex_command imagex_commands[] = {
3895 [CMD_APPEND] = {T("append"), imagex_capture_or_append},
3896 [CMD_APPLY] = {T("apply"), imagex_apply},
3897 [CMD_CAPTURE] = {T("capture"), imagex_capture_or_append},
3898 [CMD_DELETE] = {T("delete"), imagex_delete},
3899 [CMD_DIR ] = {T("dir"), imagex_dir},
3900 [CMD_EXPORT] = {T("export"), imagex_export},
3901 [CMD_EXTRACT] = {T("extract"), imagex_extract},
3902 [CMD_INFO] = {T("info"), imagex_info},
3903 [CMD_JOIN] = {T("join"), imagex_join},
3904 #if WIM_MOUNTING_SUPPORTED
3905 [CMD_MOUNT] = {T("mount"), imagex_mount_rw_or_ro},
3906 [CMD_MOUNTRW] = {T("mountrw"), imagex_mount_rw_or_ro},
3908 [CMD_OPTIMIZE] = {T("optimize"), imagex_optimize},
3909 [CMD_SPLIT] = {T("split"), imagex_split},
3910 #if WIM_MOUNTING_SUPPORTED
3911 [CMD_UNMOUNT] = {T("unmount"), imagex_unmount},
3913 [CMD_UPDATE] = {T("update"), imagex_update},
3916 static const tchar *usage_strings[] = {
3919 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3920 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3921 " [--nocheck] [--flags EDITION_ID] [--dereference]\n"
3922 " [--config=FILE] [--threads=NUM_THREADS] [--source-list]\n"
3923 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3924 " [--update-of=[WIMFILE:]IMAGE] [--wimboot]\n"
3928 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME | all)]\n"
3929 " (DIRECTORY | NTFS_VOLUME) [--check] [--ref=\"GLOB\"]\n"
3930 " [--no-acls] [--strict-acls] [--no-attributes]\n"
3931 " [--rpfix] [--norpfix] [--hardlink] [--symlink]\n"
3932 " [--include-invalid-names] [--wimboot]\n"
3936 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3937 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3938 " [--nocheck] [--compress=TYPE] [--flags EDITION_ID]\n"
3939 " [--dereference] [--config=FILE] [--threads=NUM_THREADS]\n"
3940 " [--source-list] [--no-acls] [--strict-acls] [--rpfix]\n"
3941 " [--norpfix] [--update-of=[WIMFILE:]IMAGE]\n"
3942 " [--delta-from=WIMFILE] [--wimboot]\n"
3946 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n"
3947 " [--check] [--soft]\n"
3951 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH] [--detailed]\n"
3955 " %"TS" SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
3956 " DEST_WIMFILE [DEST_IMAGE_NAME [DEST_IMAGE_DESCRIPTION]]\n"
3957 " [--boot] [--check] [--nocheck] [--compress=TYPE]\n"
3958 " [--ref=\"GLOB\"] [--threads=NUM_THREADS] [--rebuild]\n"
3962 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME) [(PATH | @LISTFILE)...]\n"
3963 " [--check] [--ref=\"GLOB\"] [--dest-dir=CMD_DIR]\n"
3964 " [--to-stdout] [--no-acls] [--strict-acls]\n"
3965 " [--no-attributes] [--include-invalid-names]\n"
3966 " [--no-wildcards] [--nullglob] [--preserve-dir-structure]\n"
3970 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME) [NEW_NAME\n"
3971 " [NEW_DESC]]] [--boot] [--check] [--nocheck] [--xml]\n"
3972 " [--extract-xml FILE] [--header] [--lookup-table]\n"
3976 " %"TS" OUT_WIMFILE SPLIT_WIM_PART... [--check]\n"
3978 #if WIM_MOUNTING_SUPPORTED
3981 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3982 " [--check] [--streams-interface=INTERFACE]\n"
3983 " [--ref=\"GLOB\"] [--allow-other]\n"
3987 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3988 " [--check] [--streams-interface=INTERFACE]\n"
3989 " [--staging-dir=CMD_DIR] [--allow-other]\n"
3994 " %"TS" WIMFILE [--check] [--nocheck] [--recompress]\n"
3995 " [--recompress-slow] [--compress=TYPE]\n"
3996 " [--threads=NUM_THREADS]\n"
4000 " %"TS" WIMFILE SPLIT_WIM_PART_1 PART_SIZE_MB [--check]\n"
4002 #if WIM_MOUNTING_SUPPORTED
4005 " %"TS" DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
4011 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
4012 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
4013 " [DEFAULT_DELETE_OPTIONS] [--command=STRING]\n"
4014 " [--wimboot-config=FILE| [< CMDFILE]\n"
4018 static const tchar *invocation_name;
4019 static int invocation_cmd = CMD_NONE;
4021 static const tchar *get_cmd_string(int cmd, bool nospace)
4023 static tchar buf[50];
4024 if (cmd == CMD_NONE) {
4025 tsprintf(buf, T("%"TS), T(IMAGEX_PROGNAME));
4026 } else if (invocation_cmd != CMD_NONE) {
4027 tsprintf(buf, T("wim%"TS), imagex_commands[cmd].name);
4029 const tchar *format;
4032 format = T("%"TS"-%"TS"");
4034 format = T("%"TS" %"TS"");
4035 tsprintf(buf, format, invocation_name, imagex_commands[cmd].name);
4043 static const tchar *s =
4045 IMAGEX_PROGNAME " (distributed with " PACKAGE " " PACKAGE_VERSION ")\n"
4046 "Copyright (C) 2012, 2013, 2014 Eric Biggers\n"
4047 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
4048 "This is free software: you are free to change and redistribute it.\n"
4049 "There is NO WARRANTY, to the extent permitted by law.\n"
4051 "Report bugs to "PACKAGE_BUGREPORT".\n"
4058 help_or_version(int argc, tchar **argv, int cmd)
4063 for (i = 1; i < argc; i++) {
4065 if (p[0] == T('-') && p[1] == T('-')) {
4067 if (!tstrcmp(p, T("help"))) {
4068 if (cmd == CMD_NONE)
4073 } else if (!tstrcmp(p, T("version"))) {
4082 print_usage_string(int cmd, FILE *fp)
4084 tfprintf(fp, usage_strings[cmd], get_cmd_string(cmd, false));
4088 recommend_man_page(int cmd, FILE *fp)
4090 const tchar *format_str;
4092 format_str = T("Uncommon options are not listed;\n"
4093 "See %"TS".pdf in the doc directory for more details.\n");
4095 format_str = T("Uncommon options are not listed;\n"
4096 "Try `man %"TS"' for more details.\n");
4098 tfprintf(fp, format_str, get_cmd_string(cmd, true));
4102 usage(int cmd, FILE *fp)
4104 tfprintf(fp, T("Usage:\n"));
4105 print_usage_string(cmd, fp);
4106 tfprintf(fp, T("\n"));
4107 recommend_man_page(cmd, fp);
4113 tfprintf(fp, T("Usage:\n"));
4114 for (int cmd = 0; cmd < CMD_MAX; cmd++) {
4115 print_usage_string(cmd, fp);
4116 tfprintf(fp, T("\n"));
4118 static const tchar *extra =
4121 " %"TS" --version\n"
4123 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
4126 tfprintf(fp, extra, invocation_name, invocation_name);
4127 recommend_man_page(CMD_NONE, fp);
4130 /* Entry point for wimlib's ImageX implementation. On UNIX the command
4131 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
4132 * something else), while an Windows the command arguments will be UTF-16LE
4133 * encoded 'wchar_t' strings. */
4136 wmain(int argc, wchar_t **argv, wchar_t **envp)
4138 main(int argc, char **argv)
4145 imagex_info_file = stdout;
4146 invocation_name = tbasename(argv[0]);
4149 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
4150 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
4154 setlocale(LC_ALL, "");
4155 codeset = nl_langinfo(CODESET);
4156 if (!strstr(codeset, "UTF-8") &&
4157 !strstr(codeset, "UTF8") &&
4158 !strstr(codeset, "utf-8") &&
4159 !strstr(codeset, "utf8"))
4162 "WARNING: Running %"TS" in a UTF-8 locale is recommended!\n"
4163 " Maybe try: `export LANG=en_US.UTF-8'?\n"
4164 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
4165 " to any value to force wimlib to use UTF-8.\n",
4171 #endif /* !__WIN32__ */
4174 tchar *igcase = tgetenv(T("WIMLIB_IMAGEX_IGNORE_CASE"));
4175 if (igcase != NULL) {
4176 if (!tstrcmp(igcase, T("no")) ||
4177 !tstrcmp(igcase, T("0")))
4178 init_flags |= WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE;
4179 else if (!tstrcmp(igcase, T("yes")) ||
4180 !tstrcmp(igcase, T("1")))
4181 init_flags |= WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE;
4184 "WARNING: Ignoring unknown setting of "
4185 "WIMLIB_IMAGEX_IGNORE_CASE\n");
4190 /* Allow being invoked as wimCOMMAND (e.g. wimapply). */
4192 if (!tstrncmp(invocation_name, T("wim"), 3) &&
4193 tstrcmp(invocation_name, T(IMAGEX_PROGNAME))) {
4194 for (int i = 0; i < CMD_MAX; i++) {
4195 if (!tstrcmp(invocation_name + 3,
4196 imagex_commands[i].name))
4205 /* Unless already known from the invocation name, determine which
4206 * command was specified. */
4207 if (cmd == CMD_NONE) {
4209 imagex_error(T("No command specified!\n"));
4213 for (int i = 0; i < CMD_MAX; i++) {
4214 if (!tstrcmp(argv[1], imagex_commands[i].name)) {
4219 if (cmd != CMD_NONE) {
4225 /* Handle --help and --version. --help can be either for the program as
4226 * a whole (cmd == CMD_NONE) or just for a specific command (cmd !=
4227 * CMD_NONE). Note: help_or_version() will not return if a --help or
4228 * --version argument was found. */
4229 help_or_version(argc, argv, cmd);
4231 /* Bail if a valid command was not specified. */
4232 if (cmd == CMD_NONE) {
4233 imagex_error(T("Unrecognized command: `%"TS"'\n"), argv[1]);
4238 /* Enable warning and error messages in wimlib to be more user-friendly.
4240 wimlib_set_print_errors(true);
4242 /* Initialize wimlib. */
4243 ret = wimlib_global_init(init_flags);
4245 goto out_check_status;
4247 /* Call the command handler function. */
4248 ret = imagex_commands[cmd].func(argc, argv, cmd);
4250 /* Check for error writing to standard output, especially since for some
4251 * commands, writing to standard output is part of the program's actual
4252 * behavior and not just for informational purposes. */
4253 if (ferror(stdout) || fclose(stdout)) {
4254 imagex_error_with_errno(T("error writing to standard output"));
4259 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
4260 * itself (not by wimlib). 0 indicates success. > 0 indicates a wimlib
4261 * error code from which an error message can be printed. */
4263 imagex_error(T("Exiting with error code %d:\n"
4265 wimlib_get_error_string(ret));
4266 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
4267 imagex_error_with_errno(T("errno"));
4269 /* Make wimlib free any resources it's holding (although this is not
4270 * strictly necessary because the process is ending anyway). */
4271 wimlib_global_cleanup();