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"
49 #define WIMLIB_COMPRESSION_TYPE_INVALID (-1)
52 # include "imagex-win32.h"
53 # define print_security_descriptor win32_print_security_descriptor
56 # include <langinfo.h>
57 # define print_security_descriptor default_print_security_descriptor
58 static inline void set_fd_to_binary_mode(int fd)
63 /* Don't confuse the user by presenting the mounting commands on Windows when
64 * they will never work. However on UNIX-like systems we always present them,
65 * even if WITH_FUSE is not defined at this point, as to not tie the build of
66 * wimlib-imagex to a specific build of wimlib. */
68 # define WIM_MOUNTING_SUPPORTED 0
70 # define WIM_MOUNTING_SUPPORTED 1
73 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
76 is_any_path_separator(tchar c)
78 return c == T('/') || c == T('\\');
81 /* Like basename(), but handles both forward and backwards slashes. */
83 tbasename(tchar *path)
85 tchar *p = tstrchr(path, T('\0'));
90 if (!is_any_path_separator(*--p))
98 if (is_any_path_separator(*--p))
103 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
117 #if WIM_MOUNTING_SUPPORTED
123 #if WIM_MOUNTING_SUPPORTED
130 static void usage(int cmd, FILE *fp);
131 static void usage_all(FILE *fp);
132 static void recommend_man_page(int cmd, FILE *fp);
133 static const tchar *get_cmd_string(int cmd, bool nospace);
135 static bool imagex_be_quiet = false;
136 static FILE *imagex_info_file;
138 #define imagex_printf(format, ...) \
139 tfprintf(imagex_info_file, format, ##__VA_ARGS__)
142 IMAGEX_ALLOW_OTHER_OPTION,
145 IMAGEX_CHUNK_SIZE_OPTION,
146 IMAGEX_COMMAND_OPTION,
147 IMAGEX_COMMIT_OPTION,
148 IMAGEX_COMPRESS_OPTION,
149 IMAGEX_COMPRESS_SLOW_OPTION,
150 IMAGEX_CONFIG_OPTION,
152 IMAGEX_DELTA_FROM_OPTION,
153 IMAGEX_DEREFERENCE_OPTION,
154 IMAGEX_DEST_DIR_OPTION,
155 IMAGEX_DETAILED_OPTION,
156 IMAGEX_EXTRACT_XML_OPTION,
159 IMAGEX_HEADER_OPTION,
160 IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
162 IMAGEX_LOOKUP_TABLE_OPTION,
163 IMAGEX_METADATA_OPTION,
164 IMAGEX_NEW_IMAGE_OPTION,
165 IMAGEX_NOCHECK_OPTION,
166 IMAGEX_NORPFIX_OPTION,
167 IMAGEX_NOT_PIPABLE_OPTION,
168 IMAGEX_NO_ACLS_OPTION,
169 IMAGEX_NO_ATTRIBUTES_OPTION,
170 IMAGEX_NO_REPLACE_OPTION,
171 IMAGEX_NO_GLOBS_OPTION,
172 IMAGEX_NULLGLOB_OPTION,
173 IMAGEX_ONE_FILE_ONLY_OPTION,
174 IMAGEX_PACK_CHUNK_SIZE_OPTION,
175 IMAGEX_PACK_COMPRESS_OPTION,
176 IMAGEX_PACK_STREAMS_OPTION,
178 IMAGEX_PIPABLE_OPTION,
179 IMAGEX_PRESERVE_DIR_STRUCTURE_OPTION,
180 IMAGEX_REBUILD_OPTION,
181 IMAGEX_RECOMPRESS_OPTION,
182 IMAGEX_RECURSIVE_OPTION,
184 IMAGEX_RESUME_OPTION,
187 IMAGEX_SOURCE_LIST_OPTION,
188 IMAGEX_STAGING_DIR_OPTION,
189 IMAGEX_STREAMS_INTERFACE_OPTION,
190 IMAGEX_STRICT_ACLS_OPTION,
191 IMAGEX_THREADS_OPTION,
192 IMAGEX_TO_STDOUT_OPTION,
193 IMAGEX_UNIX_DATA_OPTION,
194 IMAGEX_UPDATE_OF_OPTION,
195 IMAGEX_VERBOSE_OPTION,
196 IMAGEX_WIMBOOT_OPTION,
197 IMAGEX_WIMBOOT_CONFIG_OPTION,
201 static const struct option apply_options[] = {
202 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
203 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
204 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
205 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
206 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
207 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
208 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
209 {T("no-attributes"), no_argument, NULL, IMAGEX_NO_ATTRIBUTES_OPTION},
210 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
211 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
212 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
214 /* --resume is undocumented for now as it needs improvement. */
215 {T("resume"), no_argument, NULL, IMAGEX_RESUME_OPTION},
216 {T("wimboot"), no_argument, NULL, IMAGEX_WIMBOOT_OPTION},
220 static const struct option capture_or_append_options[] = {
221 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
222 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
223 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
224 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
225 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
226 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
227 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
228 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
229 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
230 {T("pack-compress"), required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
231 {T("solid-compress"),required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
232 {T("pack-streams"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
233 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
234 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
235 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
236 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
237 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
238 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
239 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
240 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
241 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
242 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
243 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
244 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
245 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
246 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
247 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
248 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
249 {T("update-of"), required_argument, NULL, IMAGEX_UPDATE_OF_OPTION},
250 {T("delta-from"), required_argument, NULL, IMAGEX_DELTA_FROM_OPTION},
251 {T("wimboot"), no_argument, NULL, IMAGEX_WIMBOOT_OPTION},
255 static const struct option delete_options[] = {
256 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
257 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
261 static const struct option dir_options[] = {
262 {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
263 {T("detailed"), no_argument, NULL, IMAGEX_DETAILED_OPTION},
264 {T("one-file-only"), no_argument, NULL, IMAGEX_ONE_FILE_ONLY_OPTION},
268 static const struct option export_options[] = {
269 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
270 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
271 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
272 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
273 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
274 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
275 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
276 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
277 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
278 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
279 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
280 {T("pack-compress"), required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
281 {T("solid-compress"),required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
282 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
283 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
284 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
285 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
286 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
287 {T("wimboot"), no_argument, NULL, IMAGEX_WIMBOOT_OPTION},
291 static const struct option extract_options[] = {
292 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
293 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
294 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
295 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
296 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
297 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
298 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
299 {T("no-attributes"), no_argument, NULL, IMAGEX_NO_ATTRIBUTES_OPTION},
300 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
301 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
302 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
303 {T("no-wildcards"), no_argument, NULL, IMAGEX_NO_GLOBS_OPTION},
304 {T("no-globs"), no_argument, NULL, IMAGEX_NO_GLOBS_OPTION},
305 {T("nullglob"), no_argument, NULL, IMAGEX_NULLGLOB_OPTION},
306 {T("preserve-dir-structure"), no_argument, NULL, IMAGEX_PRESERVE_DIR_STRUCTURE_OPTION},
307 {T("wimboot"), no_argument, NULL, IMAGEX_WIMBOOT_OPTION},
311 static const struct option info_options[] = {
312 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
313 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
314 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
315 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
316 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
317 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
318 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
319 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
320 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
324 static const struct option join_options[] = {
325 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
329 static const struct option mount_options[] = {
330 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
331 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
332 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
333 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
334 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
335 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
336 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
340 static const struct option optimize_options[] = {
341 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
342 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
343 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
344 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
345 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
346 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
347 {T("recompress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
348 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
349 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
350 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
351 {T("pack-compress"), required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
352 {T("solid-compress"),required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
353 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
354 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
355 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
356 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
357 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
361 static const struct option split_options[] = {
362 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
366 static const struct option unmount_options[] = {
367 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
368 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
369 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
370 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
371 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
372 {T("new-image"), no_argument, NULL, IMAGEX_NEW_IMAGE_OPTION},
376 static const struct option update_options[] = {
377 /* Careful: some of the options here set the defaults for update
378 * commands, but the flags given to an actual update command (and not to
379 * `imagex update' itself are also handled in
380 * update_command_add_option(). */
381 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
382 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
383 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
384 {T("command"), required_argument, NULL, IMAGEX_COMMAND_OPTION},
385 {T("wimboot-config"), required_argument, NULL, IMAGEX_WIMBOOT_CONFIG_OPTION},
387 /* Default delete options */
388 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
389 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
391 /* Global add option */
392 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
394 /* Default add options */
395 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
396 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
397 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
398 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
399 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
400 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
401 {T("no-replace"), no_argument, NULL, IMAGEX_NO_REPLACE_OPTION},
407 # define _format_attribute(type, format_str, args_start) \
408 __attribute__((format(type, format_str, args_start)))
410 # define _format_attribute(type, format_str, args_start)
413 /* Print formatted error message to stderr. */
414 static void _format_attribute(printf, 1, 2)
415 imagex_error(const tchar *format, ...)
418 va_start(va, format);
419 tfputs(T("ERROR: "), stderr);
420 tvfprintf(stderr, format, va);
421 tputc(T('\n'), stderr);
425 /* Print formatted error message to stderr. */
426 static void _format_attribute(printf, 1, 2)
427 imagex_error_with_errno(const tchar *format, ...)
429 int errno_save = errno;
431 va_start(va, format);
432 tfputs(T("ERROR: "), stderr);
433 tvfprintf(stderr, format, va);
434 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
439 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
441 if (image == WIMLIB_NO_IMAGE) {
442 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
443 " Please specify a 1-based image index or "
444 "image name. To list the images\n"
445 " contained in the WIM archive, run\n"
447 " %"TS" \"%"TS"\"\n"),
448 image_name, wim_name,
449 get_cmd_string(CMD_INFO, false), wim_name);
450 return WIMLIB_ERR_INVALID_IMAGE;
456 verify_image_is_single(int image)
458 if (image == WIMLIB_ALL_IMAGES) {
459 imagex_error(T("Cannot specify all images for this action!"));
460 return WIMLIB_ERR_INVALID_IMAGE;
466 verify_image_exists_and_is_single(int image, const tchar *image_name,
467 const tchar *wim_name)
470 ret = verify_image_exists(image, image_name, wim_name);
472 ret = verify_image_is_single(image);
476 /* Parse the argument to --compress */
478 get_compression_type(const tchar *optarg)
480 if (!tstrcasecmp(optarg, T("maximum")) ||
481 !tstrcasecmp(optarg, T("lzx")) ||
482 !tstrcasecmp(optarg, T("max")))
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 .main_nostat_cost = 15,
512 .len_nostat_cost = 15,
513 .aligned_nostat_cost = 7,
518 static const struct wimlib_lzms_compressor_params lzms_slow_params = {
520 .size = sizeof(struct wimlib_lzms_compressor_params),
522 .min_match_length = 2,
523 .max_match_length = UINT32_MAX,
524 .nice_match_length = 96,
525 .max_search_depth = 100,
526 .optim_array_length = 1024,
529 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
530 &lzx_slow_params.hdr);
532 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZMS,
533 &lzms_slow_params.hdr);
537 const tchar **strings;
538 unsigned num_strings;
539 unsigned num_alloc_strings;
542 #define STRING_SET_INITIALIZER \
543 { .strings = NULL, .num_strings = 0, .num_alloc_strings = 0, }
545 #define STRING_SET(_strings) \
546 struct string_set _strings = STRING_SET_INITIALIZER
549 string_set_append(struct string_set *set, const tchar *glob)
551 unsigned num_alloc_strings = set->num_alloc_strings;
553 if (set->num_strings == num_alloc_strings) {
554 const tchar **new_strings;
556 num_alloc_strings += 4;
557 new_strings = realloc(set->strings,
558 sizeof(set->strings[0]) * num_alloc_strings);
560 imagex_error(T("Out of memory!"));
563 set->strings = new_strings;
564 set->num_alloc_strings = num_alloc_strings;
566 set->strings[set->num_strings++] = glob;
571 string_set_destroy(struct string_set *set)
577 wim_reference_globs(WIMStruct *wim, struct string_set *set, int open_flags)
579 return wimlib_reference_resource_files(wim, set->strings,
581 WIMLIB_REF_FLAG_GLOB_ENABLE,
586 do_resource_not_found_warning(const tchar *wimfile,
587 const struct wimlib_wim_info *info,
588 const struct string_set *refglobs)
590 if (info->total_parts > 1) {
591 if (refglobs->num_strings == 0) {
592 imagex_error(T("\"%"TS"\" is part of a split WIM. "
593 "Use --ref to specify the other parts."),
596 imagex_error(T("Perhaps the '--ref' argument did not "
597 "specify all other parts of the split "
601 imagex_error(T("If this is a delta WIM, use the --ref argument "
602 "to specify the WIM(s) on which it is based."));
606 /* Returns the size of a file given its name, or -1 if the file does not exist
607 * or its size cannot be determined. */
609 file_get_size(const tchar *filename)
612 if (tstat(filename, &st) == 0)
619 PARSE_STRING_SUCCESS = 0,
620 PARSE_STRING_FAILURE = 1,
621 PARSE_STRING_NONE = 2,
625 * Parses a string token from an array of characters.
627 * Tokens are either whitespace-delimited, or double or single-quoted.
629 * @line_p: Pointer to the pointer to the line of data. Will be updated
630 * to point past the string token iff the return value is
631 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
634 * @len_p: @len_p initially stores the length of the line of data, which may
635 * be 0, and it will be updated to the number of bytes remaining in
636 * the line iff the return value is PARSE_STRING_SUCCESS.
638 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
639 * parsed string token will be returned here.
641 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
642 * PARSE_STRING_FAILURE if the data was invalid due to a missing
643 * closing quote; or PARSE_STRING_NONE if the line ended before the
644 * beginning of a string token was found.
647 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
650 tchar *line = *line_p;
654 /* Skip leading whitespace */
657 return PARSE_STRING_NONE;
658 if (!istspace(*line) && *line != T('\0'))
664 if (quote_char == T('"') || quote_char == T('\'')) {
669 line = tmemchr(line, quote_char, len);
671 imagex_error(T("Missing closing quote: %"TS), fn - 1);
672 return PARSE_STRING_FAILURE;
675 /* Unquoted string. Go until whitespace. Line is terminated
676 * by '\0', so no need to check 'len'. */
680 } while (!istspace(*line) && *line != T('\0'));
687 return PARSE_STRING_SUCCESS;
690 /* Parses a line of data (not an empty line or comment) in the source list file
691 * format. (See the man page for 'wimlib-imagex capture' for details on this
692 * format and the meaning.)
694 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
695 * len == 0. The data in @line will be modified by this function call.
697 * @len: Length of the line of data.
699 * @source: On success, the capture source and target described by the line is
700 * written into this destination. Note that it will contain pointers
701 * to data in the @line array.
703 * Returns true if the line was valid; false otherwise. */
705 parse_source_list_line(tchar *line, size_t len,
706 struct wimlib_capture_source *source)
710 ret = parse_string(&line, &len, &source->fs_source_path);
711 if (ret != PARSE_STRING_SUCCESS)
713 ret = parse_string(&line, &len, &source->wim_target_path);
714 if (ret == PARSE_STRING_NONE)
715 source->wim_target_path = source->fs_source_path;
716 return ret != PARSE_STRING_FAILURE;
719 /* Returns %true if the given line of length @len > 0 is a comment or empty line
720 * in the source list file format. */
722 is_comment_line(const tchar *line, size_t len)
725 if (*line == T('#') || *line == T(';'))
727 if (!istspace(*line) && *line != T('\0'))
737 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
740 tchar *contents = *contents_p;
741 size_t nchars = *nchars_p;
744 for (i = 0; i < nchars; i++)
745 if (contents[i] == T('\n'))
748 /* Handle last line not terminated by a newline */
749 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
750 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
752 imagex_error(T("Out of memory!"));
755 contents[nchars] = T('\n');
756 *contents_p = contents;
764 /* Parses a file in the source list format. (See the man page for
765 * 'wimlib-imagex capture' for details on this format and the meaning.)
767 * @source_list_contents: Contents of the source list file. Note that this
768 * buffer will be modified to save memory allocations,
769 * and cannot be freed until the returned array of
770 * wimlib_capture_source's has also been freed.
772 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
775 * @nsources_ret: On success, the length of the returned array is
778 * Returns: An array of `struct wimlib_capture_source's that can be passed to
779 * the wimlib_add_image_multisource() function to specify how a WIM image is to
781 static struct wimlib_capture_source *
782 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
783 size_t *nsources_ret)
787 struct wimlib_capture_source *sources;
790 nlines = text_file_count_lines(source_list_contents_p,
791 &source_list_nchars);
795 /* Always allocate at least 1 slot, just in case the implementation of
796 * calloc() returns NULL if 0 bytes are requested. */
797 sources = calloc(nlines ?: 1, sizeof(*sources));
799 imagex_error(T("out of memory"));
802 p = *source_list_contents_p;
804 for (i = 0; i < nlines; i++) {
805 /* XXX: Could use rawmemchr() here instead, but it may not be
806 * available on all platforms. */
807 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
808 size_t len = endp - p + 1;
810 if (!is_comment_line(p, len)) {
811 if (!parse_source_list_line(p, len, &sources[j++])) {
823 /* Reads the contents of a file into memory. */
825 file_get_contents(const tchar *filename, size_t *len_ret)
832 if (tstat(filename, &stbuf) != 0) {
833 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
838 fp = tfopen(filename, T("rb"));
840 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
844 buf = malloc(len ? len : 1);
846 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
847 "contents of file \"%"TS"\""), len, filename);
850 if (fread(buf, 1, len, fp) != len) {
851 imagex_error_with_errno(T("Failed to read %zu bytes from the "
852 "file \"%"TS"\""), len, filename);
866 /* Read standard input until EOF and return the full contents in a malloc()ed
867 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
870 stdin_get_contents(size_t *len_ret)
872 /* stdin can, of course, be a pipe or other non-seekable file, so the
873 * total length of the data cannot be pre-determined */
875 size_t newlen = 1024;
879 char *p = realloc(buf, newlen);
880 size_t bytes_read, bytes_to_read;
882 imagex_error(T("out of memory while reading stdin"));
886 bytes_to_read = newlen - pos;
887 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
889 if (bytes_read != bytes_to_read) {
894 imagex_error_with_errno(T("error reading stdin"));
908 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
911 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
913 *num_tchars_ret = num_bytes;
915 #else /* !__WIN32__ */
916 /* On Windows, translate the text to UTF-16LE */
920 if (num_bytes >= 2 &&
921 (((unsigned char)text[0] == 0xff && (unsigned char)text[1] == 0xfe) ||
922 ((unsigned char)text[0] <= 0x7f && (unsigned char)text[1] == 0x00)))
924 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
925 * with something that looks like an ASCII character encoded as
926 * a UTF-16LE code unit. Assume the file is encoded as
927 * UTF-16LE. This is not a 100% reliable check. */
928 num_wchars = num_bytes / 2;
929 text_wstr = (wchar_t*)text;
931 /* File does not look like UTF-16LE. Assume it is encoded in
932 * the current Windows code page. I think these are always
933 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
934 * should work as expected. */
935 text_wstr = win32_mbs_to_wcs(text,
940 *num_tchars_ret = num_wchars;
942 #endif /* __WIN32__ */
946 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
951 contents = file_get_contents(filename, &num_bytes);
954 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
958 stdin_get_text_contents(size_t *num_tchars_ret)
963 contents = stdin_get_contents(&num_bytes);
966 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
969 #define TO_PERCENT(numerator, denominator) \
970 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
972 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
973 #define MEBIBYTE_MIN_NBYTES 10000000ULL
974 #define KIBIBYTE_MIN_NBYTES 10000ULL
977 get_unit(uint64_t total_bytes, const tchar **name_ret)
979 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
980 *name_ret = T("GiB");
982 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
983 *name_ret = T("MiB");
985 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
986 *name_ret = T("KiB");
989 *name_ret = T("bytes");
994 static struct wimlib_progress_info_scan last_scan_progress;
997 report_scan_progress(const struct wimlib_progress_info_scan *scan, bool done)
999 uint64_t prev_count, cur_count;
1001 prev_count = last_scan_progress.num_nondirs_scanned +
1002 last_scan_progress.num_dirs_scanned;
1003 cur_count = scan->num_nondirs_scanned + scan->num_dirs_scanned;
1005 if (done || prev_count == 0 || cur_count >= prev_count + 100 ||
1006 cur_count % 128 == 0)
1008 unsigned unit_shift;
1009 const tchar *unit_name;
1011 unit_shift = get_unit(scan->num_bytes_scanned, &unit_name);
1012 imagex_printf(T("\r%"PRIu64" %"TS" scanned (%"PRIu64" files, "
1013 "%"PRIu64" directories) "),
1014 scan->num_bytes_scanned >> unit_shift,
1016 scan->num_nondirs_scanned,
1017 scan->num_dirs_scanned);
1018 last_scan_progress = *scan;
1021 /* Progress callback function passed to various wimlib functions. */
1022 static enum wimlib_progress_status
1023 imagex_progress_func(enum wimlib_progress_msg msg,
1024 union wimlib_progress_info *info,
1025 void *_ignored_context)
1027 unsigned percent_done;
1028 unsigned unit_shift;
1029 const tchar *unit_name;
1031 if (imagex_be_quiet)
1032 return WIMLIB_PROGRESS_STATUS_CONTINUE;
1034 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1036 static bool first = true;
1038 imagex_printf(T("Writing %"TS"-compressed data "
1039 "using %u thread%"TS"\n"),
1040 wimlib_get_compression_type_string(
1041 info->write_streams.compression_type),
1042 info->write_streams.num_threads,
1043 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1047 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1048 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1049 info->write_streams.total_bytes);
1051 imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1052 "written (%u%% done)"),
1053 info->write_streams.completed_bytes >> unit_shift,
1055 info->write_streams.total_bytes >> unit_shift,
1058 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1059 imagex_printf(T("\n"));
1061 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1062 imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
1063 if (WIMLIB_IS_WIM_ROOT_PATH(info->scan.wim_target_path)) {
1064 imagex_printf(T("\n"));
1066 imagex_printf(T(" (loading as WIM path: \"%"TS"\")...\n"),
1067 info->scan.wim_target_path);
1069 memset(&last_scan_progress, 0, sizeof(last_scan_progress));
1071 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1072 switch (info->scan.status) {
1073 case WIMLIB_SCAN_DENTRY_OK:
1074 report_scan_progress(&info->scan, false);
1076 case WIMLIB_SCAN_DENTRY_EXCLUDED:
1077 imagex_printf(T("\nExcluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1079 case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
1080 imagex_printf(T("\nWARNING: Excluding unsupported file or directory\n"
1081 " \"%"TS"\" from capture\n"), info->scan.cur_path);
1083 case WIMLIB_SCAN_DENTRY_FIXED_SYMLINK:
1084 /* Symlink fixups are enabled by default. This is
1085 * mainly intended for Windows, which for some reason
1086 * uses absolute junctions (with drive letters!) in the
1087 * default installation. On UNIX-like systems, warn the
1088 * user when fixing the target of an absolute symbolic
1089 * link, so they know to disable this if they want. */
1091 imagex_printf(T("\nWARNING: Adjusted target of "
1092 "absolute symbolic link \"%"TS"\"\n"
1093 " (Use --norpfix to capture "
1094 "absolute symbolic links as-is)\n"),
1095 info->scan.cur_path);
1102 case WIMLIB_PROGRESS_MSG_SCAN_END:
1103 report_scan_progress(&info->scan, true);
1104 imagex_printf(T("\n"));
1106 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1107 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1108 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1109 info->integrity.total_bytes);
1110 imagex_printf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1111 "of %"PRIu64" %"TS" (%u%%) done"),
1112 info->integrity.filename,
1113 info->integrity.completed_bytes >> unit_shift,
1115 info->integrity.total_bytes >> unit_shift,
1118 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1119 imagex_printf(T("\n"));
1121 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1122 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1123 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1124 info->integrity.total_bytes);
1125 imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1126 "of %"PRIu64" %"TS" (%u%%) done"),
1127 info->integrity.completed_bytes >> unit_shift,
1129 info->integrity.total_bytes >> unit_shift,
1132 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1133 imagex_printf(T("\n"));
1135 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1136 imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1137 "to %"TS" \"%"TS"\"\n"),
1138 info->extract.image,
1139 info->extract.image_name,
1140 info->extract.wimfile_name,
1141 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1142 T("NTFS volume") : T("directory")),
1143 info->extract.target);
1145 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1146 percent_done = TO_PERCENT(info->extract.completed_bytes,
1147 info->extract.total_bytes);
1148 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1149 imagex_printf(T("\rExtracting files: "
1150 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1151 info->extract.completed_bytes >> unit_shift,
1153 info->extract.total_bytes >> unit_shift,
1156 if (info->extract.completed_bytes >= info->extract.total_bytes)
1157 imagex_printf(T("\n"));
1159 case WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN:
1160 if (info->extract.total_parts != 1) {
1161 imagex_printf(T("\nReading split pipable WIM part %u of %u\n"),
1162 info->extract.part_number,
1163 info->extract.total_parts);
1166 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1167 percent_done = TO_PERCENT(info->split.completed_bytes,
1168 info->split.total_bytes);
1169 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1170 imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
1171 "%"PRIu64" %"TS" (%u%%) written\n"),
1172 info->split.part_name,
1173 info->split.cur_part_number,
1174 info->split.total_parts,
1175 info->split.completed_bytes >> unit_shift,
1177 info->split.total_bytes >> unit_shift,
1181 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1182 if (info->split.completed_bytes == info->split.total_bytes) {
1183 imagex_printf(T("Finished writing split WIM part %u of %u\n"),
1184 info->split.cur_part_number,
1185 info->split.total_parts);
1188 case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1189 switch (info->update.command->op) {
1190 case WIMLIB_UPDATE_OP_DELETE:
1191 imagex_printf(T("Deleted WIM path \"%"TS"\"\n"),
1192 info->update.command->delete_.wim_path);
1194 case WIMLIB_UPDATE_OP_RENAME:
1195 imagex_printf(T("Renamed WIM path \"%"TS"\" => \"%"TS"\"\n"),
1196 info->update.command->rename.wim_source_path,
1197 info->update.command->rename.wim_target_path);
1199 case WIMLIB_UPDATE_OP_ADD:
1204 case WIMLIB_PROGRESS_MSG_REPLACE_FILE_IN_WIM:
1205 imagex_printf(T("Updating \"%"TS"\" in WIM image\n"),
1206 info->replace.path_in_wim);
1208 case WIMLIB_PROGRESS_MSG_WIMBOOT_EXCLUDE:
1209 imagex_printf(T("\nExtracting \"%"TS"\" as normal file (not WIMBoot pointer)\n"),
1210 info->wimboot_exclude.path_in_wim);
1212 case WIMLIB_PROGRESS_MSG_UNMOUNT_BEGIN:
1213 if (info->unmount.mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1214 if (info->unmount.unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT) {
1215 imagex_printf(T("Committing changes to %"TS" (image %d)\n"),
1216 info->unmount.mounted_wim,
1217 info->unmount.mounted_image);
1219 imagex_printf(T("Discarding changes to %"TS" (image %d)\n"),
1220 info->unmount.mounted_wim,
1221 info->unmount.mounted_image);
1222 imagex_printf(T("\t(Use --commit to keep changes.)\n"));
1229 fflush(imagex_info_file);
1230 return WIMLIB_PROGRESS_STATUS_CONTINUE;
1234 parse_num_threads(const tchar *optarg)
1237 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1238 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1239 imagex_error(T("Number of threads must be a non-negative integer!"));
1246 static uint32_t parse_chunk_size(const tchar *optarg)
1249 unsigned long chunk_size = tstrtoul(optarg, &tmp, 10);
1250 if (chunk_size >= UINT32_MAX || *tmp || tmp == optarg) {
1251 imagex_error(T("Chunk size must be a non-negative integer!"));
1260 * Parse an option passed to an update command.
1262 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1265 * @option: Text string for the option (beginning with --)
1267 * @cmd: `struct wimlib_update_command' that is being constructed for
1270 * Returns true if the option was recognized; false if not.
1273 update_command_add_option(int op, const tchar *option,
1274 struct wimlib_update_command *cmd)
1276 bool recognized = true;
1278 case WIMLIB_UPDATE_OP_ADD:
1279 if (!tstrcmp(option, T("--verbose")))
1280 cmd->add.add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
1281 else if (!tstrcmp(option, T("--unix-data")))
1282 cmd->add.add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
1283 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1284 cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
1285 else if (!tstrcmp(option, T("--strict-acls")))
1286 cmd->add.add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
1287 else if (!tstrcmp(option, T("--dereference")))
1288 cmd->add.add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
1289 else if (!tstrcmp(option, T("--no-replace")))
1290 cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_REPLACE;
1294 case WIMLIB_UPDATE_OP_DELETE:
1295 if (!tstrcmp(option, T("--force")))
1296 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1297 else if (!tstrcmp(option, T("--recursive")))
1298 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1309 /* How many nonoption arguments each `imagex update' command expects */
1310 static const unsigned update_command_num_nonoptions[] = {
1311 [WIMLIB_UPDATE_OP_ADD] = 2,
1312 [WIMLIB_UPDATE_OP_DELETE] = 1,
1313 [WIMLIB_UPDATE_OP_RENAME] = 2,
1317 update_command_add_nonoption(int op, const tchar *nonoption,
1318 struct wimlib_update_command *cmd,
1319 unsigned num_nonoptions)
1322 case WIMLIB_UPDATE_OP_ADD:
1323 if (num_nonoptions == 0)
1324 cmd->add.fs_source_path = (tchar*)nonoption;
1326 cmd->add.wim_target_path = (tchar*)nonoption;
1328 case WIMLIB_UPDATE_OP_DELETE:
1329 cmd->delete_.wim_path = (tchar*)nonoption;
1331 case WIMLIB_UPDATE_OP_RENAME:
1332 if (num_nonoptions == 0)
1333 cmd->rename.wim_source_path = (tchar*)nonoption;
1335 cmd->rename.wim_target_path = (tchar*)nonoption;
1341 * Parse a command passed on stdin to `imagex update'.
1343 * @line: Text of the command.
1344 * @len: Length of the line, including a null terminator
1347 * @command: A `struct wimlib_update_command' to fill in from the parsed
1350 * @line_number: Line number of the command, for diagnostics.
1352 * Returns true on success; returns false on parse error.
1355 parse_update_command(tchar *line, size_t len,
1356 struct wimlib_update_command *command,
1360 tchar *command_name;
1362 size_t num_nonoptions;
1364 /* Get the command name ("add", "delete", "rename") */
1365 ret = parse_string(&line, &len, &command_name);
1366 if (ret != PARSE_STRING_SUCCESS)
1369 if (!tstrcasecmp(command_name, T("add"))) {
1370 op = WIMLIB_UPDATE_OP_ADD;
1371 } else if (!tstrcasecmp(command_name, T("delete"))) {
1372 op = WIMLIB_UPDATE_OP_DELETE;
1373 } else if (!tstrcasecmp(command_name, T("rename"))) {
1374 op = WIMLIB_UPDATE_OP_RENAME;
1376 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1377 command_name, line_number);
1382 /* Parse additional options and non-options as needed */
1387 ret = parse_string(&line, &len, &next_string);
1388 if (ret == PARSE_STRING_NONE) /* End of line */
1390 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1392 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1394 if (!update_command_add_option(op, next_string, command))
1396 imagex_error(T("Unrecognized option \"%"TS"\" to "
1397 "update command \"%"TS"\" on line %zu"),
1398 next_string, command_name, line_number);
1404 if (num_nonoptions == update_command_num_nonoptions[op])
1406 imagex_error(T("Unexpected argument \"%"TS"\" in "
1407 "update command on line %zu\n"
1408 " (The \"%"TS"\" command only "
1409 "takes %zu nonoption arguments!)\n"),
1410 next_string, line_number,
1411 command_name, num_nonoptions);
1414 update_command_add_nonoption(op, next_string,
1415 command, num_nonoptions);
1420 if (num_nonoptions != update_command_num_nonoptions[op]) {
1421 imagex_error(T("Not enough arguments to update command "
1422 "\"%"TS"\" on line %zu"), command_name, line_number);
1428 static struct wimlib_update_command *
1429 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1430 size_t *num_cmds_ret)
1434 struct wimlib_update_command *cmds;
1437 nlines = text_file_count_lines(cmd_file_contents_p,
1442 /* Always allocate at least 1 slot, just in case the implementation of
1443 * calloc() returns NULL if 0 bytes are requested. */
1444 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1446 imagex_error(T("out of memory"));
1449 p = *cmd_file_contents_p;
1451 for (i = 0; i < nlines; i++) {
1452 /* XXX: Could use rawmemchr() here instead, but it may not be
1453 * available on all platforms. */
1454 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1455 size_t len = endp - p + 1;
1457 if (!is_comment_line(p, len)) {
1458 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1469 /* Apply one image, or all images, from a WIM file to a directory, OR apply
1470 * one image from a WIM file to an NTFS volume. */
1472 imagex_apply(int argc, tchar **argv, int cmd)
1476 int image = WIMLIB_NO_IMAGE;
1478 struct wimlib_wim_info info;
1480 const tchar *wimfile;
1481 const tchar *target;
1482 const tchar *image_num_or_name = NULL;
1483 int extract_flags = 0;
1485 STRING_SET(refglobs);
1487 for_opt(c, apply_options) {
1489 case IMAGEX_CHECK_OPTION:
1490 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1492 case IMAGEX_VERBOSE_OPTION:
1493 /* No longer does anything. */
1495 case IMAGEX_REF_OPTION:
1496 ret = string_set_append(&refglobs, optarg);
1498 goto out_free_refglobs;
1500 case IMAGEX_UNIX_DATA_OPTION:
1501 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1503 case IMAGEX_NO_ACLS_OPTION:
1504 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1506 case IMAGEX_STRICT_ACLS_OPTION:
1507 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1509 case IMAGEX_NO_ATTRIBUTES_OPTION:
1510 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES;
1512 case IMAGEX_NORPFIX_OPTION:
1513 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1515 case IMAGEX_RPFIX_OPTION:
1516 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1518 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1519 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1520 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1522 case IMAGEX_RESUME_OPTION:
1523 extract_flags |= WIMLIB_EXTRACT_FLAG_RESUME;
1525 case IMAGEX_WIMBOOT_OPTION:
1526 extract_flags |= WIMLIB_EXTRACT_FLAG_WIMBOOT;
1534 if (argc != 2 && argc != 3)
1539 if (!tstrcmp(wimfile, T("-"))) {
1540 /* Attempt to apply pipable WIM from standard input. */
1542 image_num_or_name = NULL;
1545 image_num_or_name = argv[1];
1550 ret = wimlib_open_wim_with_progress(wimfile, open_flags, &wim,
1551 imagex_progress_func, NULL);
1553 goto out_free_refglobs;
1555 wimlib_get_wim_info(wim, &info);
1558 /* Image explicitly specified. */
1559 image_num_or_name = argv[1];
1560 image = wimlib_resolve_image(wim, image_num_or_name);
1561 ret = verify_image_exists(image, image_num_or_name, wimfile);
1563 goto out_wimlib_free;
1566 /* No image specified; default to image 1, but only if the WIM
1567 * contains exactly one image. */
1569 if (info.image_count != 1) {
1570 imagex_error(T("\"%"TS"\" contains %d images; "
1571 "Please select one (or all)."),
1572 wimfile, info.image_count);
1581 if (refglobs.num_strings) {
1583 imagex_error(T("Can't specify --ref when applying from stdin!"));
1585 goto out_wimlib_free;
1587 ret = wim_reference_globs(wim, &refglobs, open_flags);
1589 goto out_wimlib_free;
1594 /* Interpret a regular file or block device target as an NTFS
1598 if (tstat(target, &stbuf)) {
1599 if (errno != ENOENT) {
1600 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1603 goto out_wimlib_free;
1606 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1607 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1613 ret = wimlib_extract_image(wim, image, target, extract_flags);
1615 set_fd_to_binary_mode(STDIN_FILENO);
1616 ret = wimlib_extract_image_from_pipe_with_progress(
1621 imagex_progress_func,
1625 imagex_printf(T("Done applying WIM image.\n"));
1626 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
1628 do_resource_not_found_warning(wimfile, &info, &refglobs);
1630 imagex_error(T( "If you are applying an image "
1631 "from a split pipable WIM,\n"
1632 " make sure you have "
1633 "concatenated together all parts."));
1639 string_set_destroy(&refglobs);
1643 usage(CMD_APPLY, stderr);
1645 goto out_free_refglobs;
1648 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1649 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1650 * the desired image. 'wimlib-imagex append': add a new image to an existing
1653 imagex_capture_or_append(int argc, tchar **argv, int cmd)
1656 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1657 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE |
1658 WIMLIB_ADD_IMAGE_FLAG_WINCONFIG |
1659 WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1660 int write_flags = 0;
1661 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
1662 uint32_t chunk_size = UINT32_MAX;
1663 uint32_t pack_chunk_size = UINT32_MAX;
1664 int pack_ctype = WIMLIB_COMPRESSION_TYPE_INVALID;
1665 const tchar *wimfile;
1669 const tchar *flags_element = NULL;
1672 STRING_SET(base_wimfiles);
1673 WIMStruct **base_wims;
1675 WIMStruct *template_wim;
1676 const tchar *template_wimfile = NULL;
1677 const tchar *template_image_name_or_num = NULL;
1678 int template_image = WIMLIB_NO_IMAGE;
1681 unsigned num_threads = 0;
1686 tchar *config_file = NULL;
1688 bool source_list = false;
1689 size_t source_list_nchars = 0;
1690 tchar *source_list_contents;
1691 bool capture_sources_malloced;
1692 struct wimlib_capture_source *capture_sources;
1694 bool name_defaulted;
1695 bool compress_slow = false;
1697 for_opt(c, capture_or_append_options) {
1699 case IMAGEX_BOOT_OPTION:
1700 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1702 case IMAGEX_CHECK_OPTION:
1703 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1704 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1706 case IMAGEX_NOCHECK_OPTION:
1707 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
1709 case IMAGEX_CONFIG_OPTION:
1710 config_file = optarg;
1711 add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1713 case IMAGEX_COMPRESS_OPTION:
1714 compression_type = get_compression_type(optarg);
1715 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1718 case IMAGEX_COMPRESS_SLOW_OPTION:
1719 compress_slow = true;
1721 case IMAGEX_CHUNK_SIZE_OPTION:
1722 chunk_size = parse_chunk_size(optarg);
1723 if (chunk_size == UINT32_MAX)
1726 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
1727 pack_chunk_size = parse_chunk_size(optarg);
1728 if (pack_chunk_size == UINT32_MAX)
1731 case IMAGEX_PACK_COMPRESS_OPTION:
1732 pack_ctype = get_compression_type(optarg);
1733 if (pack_ctype == WIMLIB_COMPRESSION_TYPE_INVALID)
1736 case IMAGEX_PACK_STREAMS_OPTION:
1737 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
1739 case IMAGEX_FLAGS_OPTION:
1740 flags_element = optarg;
1742 case IMAGEX_DEREFERENCE_OPTION:
1743 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1745 case IMAGEX_VERBOSE_OPTION:
1746 /* No longer does anything. */
1748 case IMAGEX_THREADS_OPTION:
1749 num_threads = parse_num_threads(optarg);
1750 if (num_threads == UINT_MAX)
1753 case IMAGEX_REBUILD_OPTION:
1754 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1756 case IMAGEX_UNIX_DATA_OPTION:
1757 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1759 case IMAGEX_SOURCE_LIST_OPTION:
1762 case IMAGEX_NO_ACLS_OPTION:
1763 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1765 case IMAGEX_STRICT_ACLS_OPTION:
1766 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1768 case IMAGEX_RPFIX_OPTION:
1769 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1771 case IMAGEX_NORPFIX_OPTION:
1772 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1774 case IMAGEX_PIPABLE_OPTION:
1775 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1777 case IMAGEX_NOT_PIPABLE_OPTION:
1778 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
1780 case IMAGEX_UPDATE_OF_OPTION:
1781 if (template_image_name_or_num) {
1782 imagex_error(T("'--update-of' can only be "
1783 "specified one time!"));
1787 colon = tstrrchr(optarg, T(':'));
1790 template_wimfile = optarg;
1792 template_image_name_or_num = colon + 1;
1794 template_wimfile = NULL;
1795 template_image_name_or_num = optarg;
1799 case IMAGEX_DELTA_FROM_OPTION:
1800 if (cmd != CMD_CAPTURE) {
1801 imagex_error(T("'--delta-from' is only "
1802 "valid for capture!"));
1805 ret = string_set_append(&base_wimfiles, optarg);
1807 goto out_free_base_wimfiles;
1808 write_flags |= WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS;
1810 case IMAGEX_WIMBOOT_OPTION:
1811 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_WIMBOOT;
1820 if (argc < 2 || argc > 4)
1826 /* Set default compression type and parameters. */
1829 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
1830 /* No compression type specified. Use the default. */
1832 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_WIMBOOT) {
1833 /* With --wimboot, default to XPRESS compression. */
1834 compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
1835 } else if (write_flags & WIMLIB_WRITE_FLAG_PACK_STREAMS) {
1836 /* With --pack-streams or --solid, default to LZMS
1837 * compression. (However, this will not affect packed
1839 compression_type = WIMLIB_COMPRESSION_TYPE_LZMS;
1841 /* Otherwise, default to LZX compression in fast mode.
1843 compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1844 if (!compress_slow && pack_ctype != WIMLIB_COMPRESSION_TYPE_LZX) {
1845 struct wimlib_lzx_compressor_params params = {
1846 .hdr.size = sizeof(params),
1847 .algorithm = WIMLIB_LZX_ALGORITHM_FAST,
1850 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
1857 set_compress_slow();
1859 if (!tstrcmp(wimfile, T("-"))) {
1860 /* Writing captured WIM to standard output. */
1862 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
1863 imagex_error("Can't write a non-pipable WIM to "
1864 "standard output! Specify --pipable\n"
1865 " if you want to create a pipable WIM "
1866 "(but read the docs first).");
1870 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1872 if (cmd == CMD_APPEND) {
1873 imagex_error(T("Using standard output for append does "
1874 "not make sense."));
1877 wim_fd = STDOUT_FILENO;
1879 imagex_info_file = stderr;
1880 set_fd_to_binary_mode(wim_fd);
1883 /* If template image was specified using --update-of=IMAGE rather
1884 * than --update-of=WIMFILE:IMAGE, set the default WIMFILE. */
1885 if (template_image_name_or_num && !template_wimfile) {
1886 if (base_wimfiles.num_strings == 1) {
1887 /* Capturing delta WIM based on single WIM: default to
1889 template_wimfile = base_wimfiles.strings[0];
1890 } else if (cmd == CMD_APPEND) {
1891 /* Appending to WIM: default to WIM being appended to.
1893 template_wimfile = wimfile;
1895 /* Capturing a normal (non-delta) WIM, so the WIM file
1896 * *must* be explicitly specified. */
1897 if (base_wimfiles.num_strings > 1) {
1898 imagex_error(T("For capture of delta WIM "
1899 "based on multiple existing "
1901 " '--update-of' must "
1902 "specify WIMFILE:IMAGE!"));
1904 imagex_error(T("For capture of non-delta WIM, "
1905 "'--update-of' must specify "
1914 name_defaulted = false;
1916 /* Set default name to SOURCE argument, omitting any directory
1917 * prefixes and trailing slashes. This requires making a copy
1918 * of @source. Leave some free characters at the end in case we
1919 * append a number to keep the name unique. */
1920 size_t source_name_len;
1922 source_name_len = tstrlen(source);
1923 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1924 name = tbasename(tstrcpy(source_copy, source));
1925 name_defaulted = true;
1927 /* Image description defaults to NULL if not given. */
1934 /* Set up capture sources in source list mode */
1935 if (source[0] == T('-') && source[1] == T('\0')) {
1936 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1938 source_list_contents = file_get_text_contents(source,
1939 &source_list_nchars);
1941 if (!source_list_contents)
1944 capture_sources = parse_source_list(&source_list_contents,
1947 if (!capture_sources) {
1949 goto out_free_source_list_contents;
1951 capture_sources_malloced = true;
1953 /* Set up capture source in non-source-list mode. */
1954 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1955 capture_sources[0].fs_source_path = source;
1956 capture_sources[0].wim_target_path = WIMLIB_WIM_ROOT_PATH;
1957 capture_sources[0].reserved = 0;
1959 capture_sources_malloced = false;
1960 source_list_contents = NULL;
1963 /* Open the existing WIM, or create a new one. */
1964 if (cmd == CMD_APPEND) {
1965 ret = wimlib_open_wim_with_progress(wimfile, open_flags, &wim,
1966 imagex_progress_func, NULL);
1968 goto out_free_capture_sources;
1970 ret = wimlib_create_new_wim(compression_type, &wim);
1972 goto out_free_capture_sources;
1973 wimlib_register_progress_function(wim, imagex_progress_func, NULL);
1976 /* Set chunk size if non-default. */
1977 if (chunk_size != UINT32_MAX) {
1978 ret = wimlib_set_output_chunk_size(wim, chunk_size);
1981 } else if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_WIMBOOT) &&
1982 compression_type == WIMLIB_COMPRESSION_TYPE_XPRESS) {
1983 ret = wimlib_set_output_chunk_size(wim, 4096);
1987 if (pack_ctype != WIMLIB_COMPRESSION_TYPE_INVALID) {
1988 ret = wimlib_set_output_pack_compression_type(wim, pack_ctype);
1992 if (pack_chunk_size != UINT32_MAX) {
1993 ret = wimlib_set_output_pack_chunk_size(wim, pack_chunk_size);
1999 /* Detect if source is regular file or block device and set NTFS volume
2004 if (tstat(source, &stbuf) == 0) {
2005 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
2006 imagex_printf(T("Capturing WIM image from NTFS "
2007 "filesystem on \"%"TS"\"\n"), source);
2008 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
2011 if (errno != ENOENT) {
2012 imagex_error_with_errno(T("Failed to stat "
2013 "\"%"TS"\""), source);
2021 /* If the user did not specify an image name, and the basename of the
2022 * source already exists as an image name in the WIM file, append a
2023 * suffix to make it unique. */
2024 if (cmd == CMD_APPEND && name_defaulted) {
2025 unsigned long conflict_idx;
2026 tchar *name_end = tstrchr(name, T('\0'));
2027 for (conflict_idx = 1;
2028 wimlib_image_name_in_use(wim, name);
2031 tsprintf(name_end, T(" (%lu)"), conflict_idx);
2035 /* If capturing a delta WIM, reference resources from the base WIMs
2036 * before adding the new image. */
2037 if (base_wimfiles.num_strings) {
2038 base_wims = calloc(base_wimfiles.num_strings,
2039 sizeof(base_wims[0]));
2040 if (base_wims == NULL) {
2041 imagex_error(T("Out of memory!"));
2046 for (size_t i = 0; i < base_wimfiles.num_strings; i++) {
2047 ret = wimlib_open_wim_with_progress(
2048 base_wimfiles.strings[i],
2049 open_flags, &base_wims[i],
2050 imagex_progress_func, NULL);
2052 goto out_free_base_wims;
2056 ret = wimlib_reference_resources(wim, base_wims,
2057 base_wimfiles.num_strings, 0);
2059 goto out_free_base_wims;
2061 if (base_wimfiles.num_strings == 1) {
2062 imagex_printf(T("Capturing delta WIM based on \"%"TS"\"\n"),
2063 base_wimfiles.strings[0]);
2065 imagex_printf(T("Capturing delta WIM based on %u WIMs\n"),
2066 base_wimfiles.num_strings);
2073 /* If capturing or appending as an update of an existing (template) image,
2074 * open the WIM if needed and parse the image index. */
2075 if (template_image_name_or_num) {
2078 if (base_wimfiles.num_strings == 1 &&
2079 template_wimfile == base_wimfiles.strings[0]) {
2080 template_wim = base_wims[0];
2081 } else if (template_wimfile == wimfile) {
2084 ret = wimlib_open_wim_with_progress(template_wimfile,
2087 imagex_progress_func,
2090 goto out_free_base_wims;
2093 template_image = wimlib_resolve_image(template_wim,
2094 template_image_name_or_num);
2096 if (template_image_name_or_num[0] == T('-')) {
2099 struct wimlib_wim_info info;
2101 wimlib_get_wim_info(template_wim, &info);
2102 n = tstrtoul(template_image_name_or_num + 1, &tmp, 10);
2103 if (n >= 1 && n <= info.image_count &&
2105 tmp != template_image_name_or_num + 1)
2107 template_image = info.image_count - (n - 1);
2110 ret = verify_image_exists_and_is_single(template_image,
2111 template_image_name_or_num,
2114 goto out_free_template_wim;
2116 template_wim = NULL;
2119 ret = wimlib_add_image_multisource(wim,
2126 goto out_free_template_wim;
2128 if (desc || flags_element || template_image_name_or_num) {
2129 /* User provided <DESCRIPTION> or <FLAGS> element, or an image
2130 * on which the added one is to be based has been specified with
2131 * --update-of. Get the index of the image we just
2132 * added, then use it to call the appropriate functions. */
2133 struct wimlib_wim_info info;
2135 wimlib_get_wim_info(wim, &info);
2138 ret = wimlib_set_image_descripton(wim,
2142 goto out_free_template_wim;
2145 if (flags_element) {
2146 ret = wimlib_set_image_flags(wim, info.image_count,
2149 goto out_free_template_wim;
2152 /* Reference template image if the user provided one. */
2153 if (template_image_name_or_num) {
2154 imagex_printf(T("Using image %d "
2155 "from \"%"TS"\" as template\n"),
2156 template_image, template_wimfile);
2157 ret = wimlib_reference_template_image(wim,
2163 goto out_free_template_wim;
2167 /* Write the new WIM or overwrite the existing WIM with the new image
2169 if (cmd == CMD_APPEND) {
2170 ret = wimlib_overwrite(wim, write_flags, num_threads);
2171 } else if (wimfile) {
2172 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
2173 write_flags, num_threads);
2175 ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
2176 write_flags, num_threads);
2178 out_free_template_wim:
2179 /* template_wim may alias base_wims[0] or wim. */
2180 if ((base_wimfiles.num_strings != 1 || template_wim != base_wims[0]) &&
2181 template_wim != wim)
2182 wimlib_free(template_wim);
2184 for (size_t i = 0; i < base_wimfiles.num_strings; i++)
2185 wimlib_free(base_wims[i]);
2189 out_free_capture_sources:
2190 if (capture_sources_malloced)
2191 free(capture_sources);
2192 out_free_source_list_contents:
2193 free(source_list_contents);
2194 out_free_base_wimfiles:
2195 string_set_destroy(&base_wimfiles);
2202 goto out_free_base_wimfiles;
2205 /* Remove image(s) from a WIM. */
2207 imagex_delete(int argc, tchar **argv, int cmd)
2210 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2211 int write_flags = 0;
2212 const tchar *wimfile;
2213 const tchar *image_num_or_name;
2218 for_opt(c, delete_options) {
2220 case IMAGEX_CHECK_OPTION:
2221 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2222 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2224 case IMAGEX_SOFT_OPTION:
2225 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
2236 imagex_error(T("Must specify a WIM file"));
2238 imagex_error(T("Must specify an image"));
2242 image_num_or_name = argv[1];
2244 ret = wimlib_open_wim_with_progress(wimfile, open_flags, &wim,
2245 imagex_progress_func, NULL);
2249 image = wimlib_resolve_image(wim, image_num_or_name);
2251 ret = verify_image_exists(image, image_num_or_name, wimfile);
2253 goto out_wimlib_free;
2255 ret = wimlib_delete_image(wim, image);
2257 imagex_error(T("Failed to delete image from \"%"TS"\""),
2259 goto out_wimlib_free;
2262 ret = wimlib_overwrite(wim, write_flags, 0);
2264 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2265 "deleted"), wimfile);
2273 usage(CMD_DELETE, stderr);
2278 struct print_dentry_options {
2283 print_dentry_full_path(const struct wimlib_dir_entry *dentry)
2285 tprintf(T("%"TS"\n"), dentry->full_path);
2288 static const struct {
2291 } file_attr_flags[] = {
2292 {WIMLIB_FILE_ATTRIBUTE_READONLY, T("READONLY")},
2293 {WIMLIB_FILE_ATTRIBUTE_HIDDEN, T("HIDDEN")},
2294 {WIMLIB_FILE_ATTRIBUTE_SYSTEM, T("SYSTEM")},
2295 {WIMLIB_FILE_ATTRIBUTE_DIRECTORY, T("DIRECTORY")},
2296 {WIMLIB_FILE_ATTRIBUTE_ARCHIVE, T("ARCHIVE")},
2297 {WIMLIB_FILE_ATTRIBUTE_DEVICE, T("DEVICE")},
2298 {WIMLIB_FILE_ATTRIBUTE_NORMAL, T("NORMAL")},
2299 {WIMLIB_FILE_ATTRIBUTE_TEMPORARY, T("TEMPORARY")},
2300 {WIMLIB_FILE_ATTRIBUTE_SPARSE_FILE, T("SPARSE_FILE")},
2301 {WIMLIB_FILE_ATTRIBUTE_REPARSE_POINT, T("REPARSE_POINT")},
2302 {WIMLIB_FILE_ATTRIBUTE_COMPRESSED, T("COMPRESSED")},
2303 {WIMLIB_FILE_ATTRIBUTE_OFFLINE, T("OFFLINE")},
2304 {WIMLIB_FILE_ATTRIBUTE_NOT_CONTENT_INDEXED, T("NOT_CONTENT_INDEXED")},
2305 {WIMLIB_FILE_ATTRIBUTE_ENCRYPTED, T("ENCRYPTED")},
2306 {WIMLIB_FILE_ATTRIBUTE_VIRTUAL, T("VIRTUAL")},
2309 #define TIMESTR_MAX 100
2312 timespec_to_string(const struct timespec *spec, tchar *buf)
2314 time_t t = spec->tv_sec;
2317 tstrftime(buf, TIMESTR_MAX, T("%a %b %d %H:%M:%S %Y UTC"), &tm);
2318 buf[TIMESTR_MAX - 1] = '\0';
2322 print_time(const tchar *type, const struct timespec *spec)
2324 tchar timestr[TIMESTR_MAX];
2326 timespec_to_string(spec, timestr);
2328 tprintf(T("%-20"TS"= %"TS"\n"), type, timestr);
2331 static void print_byte_field(const uint8_t field[], size_t len)
2334 tprintf(T("%02hhx"), *field++);
2338 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2340 tputs(T("WIM Information:"));
2341 tputs(T("----------------"));
2342 tprintf(T("Path: %"TS"\n"), wimfile);
2343 tprintf(T("GUID: 0x"));
2344 print_byte_field(info->guid, sizeof(info->guid));
2346 tprintf(T("Version: %u\n"), info->wim_version);
2347 tprintf(T("Image Count: %d\n"), info->image_count);
2348 tprintf(T("Compression: %"TS"\n"),
2349 wimlib_get_compression_type_string(info->compression_type));
2350 tprintf(T("Chunk Size: %"PRIu32" bytes\n"),
2352 tprintf(T("Part Number: %d/%d\n"), info->part_number, info->total_parts);
2353 tprintf(T("Boot Index: %d\n"), info->boot_index);
2354 tprintf(T("Size: %"PRIu64" bytes\n"), info->total_bytes);
2355 tprintf(T("Integrity Info: %"TS"\n"),
2356 info->has_integrity_table ? T("yes") : T("no"));
2357 tprintf(T("Relative path junction: %"TS"\n"),
2358 info->has_rpfix ? T("yes") : T("no"));
2359 tprintf(T("Pipable: %"TS"\n"),
2360 info->pipable ? T("yes") : T("no"));
2365 print_resource(const struct wimlib_resource_entry *resource,
2368 tprintf(T("Hash = 0x"));
2369 print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2372 if (!resource->is_missing) {
2373 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
2374 resource->uncompressed_size);
2375 if (resource->packed) {
2376 tprintf(T("Raw compressed size = %"PRIu64" bytes\n"),
2377 resource->raw_resource_compressed_size);
2379 tprintf(T("Raw offset in WIM = %"PRIu64" bytes\n"),
2380 resource->raw_resource_offset_in_wim);
2382 tprintf(T("Offset in raw = %"PRIu64" bytes\n"),
2385 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
2386 resource->compressed_size);
2388 tprintf(T("Offset in WIM = %"PRIu64" bytes\n"),
2392 tprintf(T("Part Number = %u\n"), resource->part_number);
2393 tprintf(T("Reference Count = %u\n"), resource->reference_count);
2395 tprintf(T("Flags = "));
2396 if (resource->is_compressed)
2397 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED "));
2398 if (resource->is_metadata)
2399 tprintf(T("WIM_RESHDR_FLAG_METADATA "));
2400 if (resource->is_free)
2401 tprintf(T("WIM_RESHDR_FLAG_FREE "));
2402 if (resource->is_spanned)
2403 tprintf(T("WIM_RESHDR_FLAG_SPANNED "));
2404 if (resource->packed)
2405 tprintf(T("WIM_RESHDR_FLAG_PACKED_STREAMS "));
2413 print_lookup_table(WIMStruct *wim)
2415 wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2419 default_print_security_descriptor(const uint8_t *sd, size_t size)
2421 tprintf(T("Security Descriptor = "));
2422 print_byte_field(sd, size);
2427 print_dentry_detailed(const struct wimlib_dir_entry *dentry)
2431 "----------------------------------------------------------------------------\n"));
2432 tprintf(T("Full Path = \"%"TS"\"\n"), dentry->full_path);
2433 if (dentry->dos_name)
2434 tprintf(T("Short Name = \"%"TS"\"\n"), dentry->dos_name);
2435 tprintf(T("Attributes = 0x%08x\n"), dentry->attributes);
2436 for (size_t i = 0; i < ARRAY_LEN(file_attr_flags); i++)
2437 if (file_attr_flags[i].flag & dentry->attributes)
2438 tprintf(T(" FILE_ATTRIBUTE_%"TS" is set\n"),
2439 file_attr_flags[i].name);
2441 if (dentry->security_descriptor) {
2442 print_security_descriptor(dentry->security_descriptor,
2443 dentry->security_descriptor_size);
2446 print_time(T("Creation Time"), &dentry->creation_time);
2447 print_time(T("Last Write Time"), &dentry->last_write_time);
2448 print_time(T("Last Access Time"), &dentry->last_access_time);
2451 if (dentry->attributes & WIMLIB_FILE_ATTRIBUTE_REPARSE_POINT)
2452 tprintf(T("Reparse Tag = 0x%"PRIx32"\n"), dentry->reparse_tag);
2454 tprintf(T("Link Group ID = 0x%016"PRIx64"\n"), dentry->hard_link_group_id);
2455 tprintf(T("Link Count = %"PRIu32"\n"), dentry->num_links);
2457 if (dentry->unix_mode != 0) {
2458 tprintf(T("UNIX Data = uid:%"PRIu32" gid:%"PRIu32" "
2459 "mode:0%"PRIo32" rdev:0x%"PRIx32"\n"),
2460 dentry->unix_uid, dentry->unix_gid,
2461 dentry->unix_mode, dentry->unix_rdev);
2464 for (uint32_t i = 0; i <= dentry->num_named_streams; i++) {
2465 if (dentry->streams[i].stream_name) {
2466 tprintf(T("\tData stream \"%"TS"\":\n"),
2467 dentry->streams[i].stream_name);
2469 tprintf(T("\tUnnamed data stream:\n"));
2471 print_resource(&dentry->streams[i].resource, NULL);
2476 print_dentry(const struct wimlib_dir_entry *dentry, void *_options)
2478 const struct print_dentry_options *options = _options;
2479 if (!options->detailed)
2480 print_dentry_full_path(dentry);
2482 print_dentry_detailed(dentry);
2486 /* Print the files contained in an image(s) in a WIM file. */
2488 imagex_dir(int argc, tchar **argv, int cmd)
2490 const tchar *wimfile;
2491 WIMStruct *wim = NULL;
2494 const tchar *path = WIMLIB_WIM_ROOT_PATH;
2496 struct print_dentry_options options = {
2499 int iterate_flags = WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE;
2501 for_opt(c, dir_options) {
2503 case IMAGEX_PATH_OPTION:
2506 case IMAGEX_DETAILED_OPTION:
2507 options.detailed = true;
2509 case IMAGEX_ONE_FILE_ONLY_OPTION:
2510 iterate_flags &= ~WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE;
2520 imagex_error(T("Must specify a WIM file"));
2524 imagex_error(T("Too many arguments"));
2529 ret = wimlib_open_wim_with_progress(wimfile, 0, &wim,
2530 imagex_progress_func, NULL);
2535 image = wimlib_resolve_image(wim, argv[1]);
2536 ret = verify_image_exists(image, argv[1], wimfile);
2538 goto out_wimlib_free;
2540 /* No image specified; default to image 1, but only if the WIM
2541 * contains exactly one image. */
2543 struct wimlib_wim_info info;
2545 wimlib_get_wim_info(wim, &info);
2546 if (info.image_count != 1) {
2547 imagex_error(T("\"%"TS"\" contains %d images; Please "
2548 "select one (or all)."),
2549 wimfile, info.image_count);
2556 ret = wimlib_iterate_dir_tree(wim, image, path, iterate_flags,
2557 print_dentry, &options);
2564 usage(CMD_DIR, stderr);
2569 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2572 imagex_export(int argc, tchar **argv, int cmd)
2576 int export_flags = WIMLIB_EXPORT_FLAG_GIFT;
2577 int write_flags = 0;
2578 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2579 const tchar *src_wimfile;
2580 const tchar *src_image_num_or_name;
2581 const tchar *dest_wimfile;
2583 const tchar *dest_name;
2584 const tchar *dest_desc;
2586 struct wimlib_wim_info src_info;
2587 WIMStruct *dest_wim;
2592 STRING_SET(refglobs);
2593 unsigned num_threads = 0;
2594 uint32_t chunk_size = UINT32_MAX;
2595 uint32_t pack_chunk_size = UINT32_MAX;
2596 int pack_ctype = WIMLIB_COMPRESSION_TYPE_INVALID;
2598 for_opt(c, export_options) {
2600 case IMAGEX_BOOT_OPTION:
2601 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2603 case IMAGEX_CHECK_OPTION:
2604 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2605 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2607 case IMAGEX_NOCHECK_OPTION:
2608 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2610 case IMAGEX_COMPRESS_OPTION:
2611 compression_type = get_compression_type(optarg);
2612 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2615 case IMAGEX_COMPRESS_SLOW_OPTION:
2616 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
2617 set_compress_slow();
2619 case IMAGEX_PACK_STREAMS_OPTION:
2620 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
2622 case IMAGEX_CHUNK_SIZE_OPTION:
2623 chunk_size = parse_chunk_size(optarg);
2624 if (chunk_size == UINT32_MAX)
2627 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
2628 pack_chunk_size = parse_chunk_size(optarg);
2629 if (pack_chunk_size == UINT32_MAX)
2632 case IMAGEX_PACK_COMPRESS_OPTION:
2633 pack_ctype = get_compression_type(optarg);
2634 if (pack_ctype == WIMLIB_COMPRESSION_TYPE_INVALID)
2637 case IMAGEX_REF_OPTION:
2638 ret = string_set_append(&refglobs, optarg);
2640 goto out_free_refglobs;
2642 case IMAGEX_THREADS_OPTION:
2643 num_threads = parse_num_threads(optarg);
2644 if (num_threads == UINT_MAX)
2647 case IMAGEX_REBUILD_OPTION:
2648 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2650 case IMAGEX_PIPABLE_OPTION:
2651 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2653 case IMAGEX_NOT_PIPABLE_OPTION:
2654 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
2656 case IMAGEX_WIMBOOT_OPTION:
2657 export_flags |= WIMLIB_EXPORT_FLAG_WIMBOOT;
2665 if (argc < 3 || argc > 5)
2667 src_wimfile = argv[0];
2668 src_image_num_or_name = argv[1];
2669 dest_wimfile = argv[2];
2670 dest_name = (argc >= 4) ? argv[3] : NULL;
2671 dest_desc = (argc >= 5) ? argv[4] : NULL;
2672 ret = wimlib_open_wim_with_progress(src_wimfile, open_flags, &src_wim,
2673 imagex_progress_func, NULL);
2675 goto out_free_refglobs;
2677 wimlib_get_wim_info(src_wim, &src_info);
2679 /* Determine if the destination is an existing file or not. If so, we
2680 * try to append the exported image(s) to it; otherwise, we create a new
2681 * WIM containing the exported image(s). Furthermore, determine if we
2682 * need to write a pipable WIM directly to standard output. */
2684 if (tstrcmp(dest_wimfile, T("-")) == 0) {
2686 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2687 imagex_error("Can't write a non-pipable WIM to "
2688 "standard output! Specify --pipable\n"
2689 " if you want to create a pipable WIM "
2690 "(but read the docs first).");
2692 goto out_free_src_wim;
2695 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2697 dest_wimfile = NULL;
2698 dest_wim_fd = STDOUT_FILENO;
2699 imagex_info_file = stderr;
2700 set_fd_to_binary_mode(dest_wim_fd);
2703 if (dest_wimfile != NULL && tstat(dest_wimfile, &stbuf) == 0) {
2705 /* Destination file exists. */
2707 if (!S_ISREG(stbuf.st_mode)) {
2708 imagex_error(T("\"%"TS"\" is not a regular file"),
2711 goto out_free_src_wim;
2713 ret = wimlib_open_wim_with_progress(dest_wimfile,
2715 WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2717 imagex_progress_func,
2720 goto out_free_src_wim;
2722 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2723 /* The user specified a compression type, but we're
2724 * exporting to an existing WIM. Make sure the
2725 * specified compression type is the same as the
2726 * compression type of the existing destination WIM. */
2727 struct wimlib_wim_info dest_info;
2729 wimlib_get_wim_info(dest_wim, &dest_info);
2730 if (compression_type != dest_info.compression_type) {
2731 imagex_error(T("Cannot specify a compression type that is "
2732 "not the same as that used in the "
2733 "destination WIM"));
2735 goto out_free_dest_wim;
2741 if (errno != ENOENT) {
2742 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2745 goto out_free_src_wim;
2748 /* dest_wimfile is not an existing file, so create a new WIM. */
2750 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2751 /* The user did not specify a compression type; default
2752 * to that of the source WIM, unless --pack-streams,
2753 * --solid, or --wimboot was specified. */
2755 if (write_flags & WIMLIB_WRITE_FLAG_PACK_STREAMS)
2756 compression_type = WIMLIB_COMPRESSION_TYPE_LZMS;
2757 else if (export_flags & WIMLIB_EXPORT_FLAG_WIMBOOT)
2758 compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
2760 compression_type = src_info.compression_type;
2762 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2764 goto out_free_src_wim;
2766 wimlib_register_progress_function(dest_wim,
2767 imagex_progress_func, NULL);
2769 if ((export_flags & WIMLIB_EXPORT_FLAG_WIMBOOT)
2770 && compression_type == WIMLIB_COMPRESSION_TYPE_XPRESS)
2772 /* For --wimboot export, use small XPRESS chunks. */
2773 wimlib_set_output_chunk_size(dest_wim, 4096);
2774 } else if (compression_type == src_info.compression_type &&
2775 chunk_size == UINT32_MAX)
2777 /* Use same chunk size if compression type is the same. */
2778 wimlib_set_output_chunk_size(dest_wim, src_info.chunk_size);
2782 if (chunk_size != UINT32_MAX) {
2783 /* Set destination chunk size. */
2784 ret = wimlib_set_output_chunk_size(dest_wim, chunk_size);
2786 goto out_free_dest_wim;
2788 if (pack_ctype != WIMLIB_COMPRESSION_TYPE_INVALID) {
2789 ret = wimlib_set_output_pack_compression_type(dest_wim, pack_ctype);
2791 goto out_free_dest_wim;
2793 if (pack_chunk_size != UINT32_MAX) {
2794 ret = wimlib_set_output_pack_chunk_size(dest_wim, pack_chunk_size);
2796 goto out_free_dest_wim;
2799 image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2800 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2802 goto out_free_dest_wim;
2804 if (refglobs.num_strings) {
2805 ret = wim_reference_globs(src_wim, &refglobs, open_flags);
2807 goto out_free_dest_wim;
2810 if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
2811 image == WIMLIB_ALL_IMAGES && src_info.boot_index == 0)
2813 imagex_error(T("--boot specified for all-images export, but source WIM "
2814 "has no bootable image."));
2816 goto out_free_dest_wim;
2819 ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2820 dest_desc, export_flags);
2822 if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2823 do_resource_not_found_warning(src_wimfile,
2824 &src_info, &refglobs);
2826 goto out_free_dest_wim;
2830 ret = wimlib_overwrite(dest_wim, write_flags, num_threads);
2831 else if (dest_wimfile)
2832 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2833 write_flags, num_threads);
2835 ret = wimlib_write_to_fd(dest_wim, dest_wim_fd,
2836 WIMLIB_ALL_IMAGES, write_flags,
2839 wimlib_free(dest_wim);
2841 wimlib_free(src_wim);
2843 string_set_destroy(&refglobs);
2847 usage(CMD_EXPORT, stderr);
2850 goto out_free_refglobs;
2853 /* Extract files or directories from a WIM image */
2855 imagex_extract(int argc, tchar **argv, int cmd)
2862 const tchar *wimfile;
2863 const tchar *image_num_or_name;
2864 tchar *dest_dir = T(".");
2865 int extract_flags = WIMLIB_EXTRACT_FLAG_NORPFIX |
2866 WIMLIB_EXTRACT_FLAG_GLOB_PATHS |
2867 WIMLIB_EXTRACT_FLAG_STRICT_GLOB;
2868 int notlist_extract_flags = WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE;
2870 STRING_SET(refglobs);
2872 tchar *root_path = WIMLIB_WIM_ROOT_PATH;
2874 for_opt(c, extract_options) {
2876 case IMAGEX_CHECK_OPTION:
2877 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2879 case IMAGEX_VERBOSE_OPTION:
2880 /* No longer does anything. */
2882 case IMAGEX_REF_OPTION:
2883 ret = string_set_append(&refglobs, optarg);
2885 goto out_free_refglobs;
2887 case IMAGEX_UNIX_DATA_OPTION:
2888 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2890 case IMAGEX_NO_ACLS_OPTION:
2891 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2893 case IMAGEX_STRICT_ACLS_OPTION:
2894 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2896 case IMAGEX_NO_ATTRIBUTES_OPTION:
2897 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES;
2899 case IMAGEX_DEST_DIR_OPTION:
2902 case IMAGEX_TO_STDOUT_OPTION:
2903 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2904 imagex_info_file = stderr;
2905 imagex_be_quiet = true;
2906 set_fd_to_binary_mode(STDOUT_FILENO);
2908 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2909 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2910 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2912 case IMAGEX_NO_GLOBS_OPTION:
2913 extract_flags &= ~WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2915 case IMAGEX_NULLGLOB_OPTION:
2916 extract_flags &= ~WIMLIB_EXTRACT_FLAG_STRICT_GLOB;
2918 case IMAGEX_PRESERVE_DIR_STRUCTURE_OPTION:
2919 notlist_extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE;
2921 case IMAGEX_WIMBOOT_OPTION:
2922 extract_flags |= WIMLIB_EXTRACT_FLAG_WIMBOOT;
2934 if (!(extract_flags & (WIMLIB_EXTRACT_FLAG_GLOB_PATHS |
2935 WIMLIB_EXTRACT_FLAG_STRICT_GLOB)))
2937 imagex_error(T("Can't combine --no-globs and --nullglob!"));
2942 image_num_or_name = argv[1];
2947 ret = wimlib_open_wim_with_progress(wimfile, open_flags, &wim,
2948 imagex_progress_func, NULL);
2950 goto out_free_refglobs;
2952 image = wimlib_resolve_image(wim, image_num_or_name);
2953 ret = verify_image_exists_and_is_single(image,
2957 goto out_wimlib_free;
2959 if (refglobs.num_strings) {
2960 ret = wim_reference_globs(wim, &refglobs, open_flags);
2962 goto out_wimlib_free;
2968 extract_flags &= ~WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2971 while (argc != 0 && ret == 0) {
2975 num_paths < argc && argv[num_paths][0] != T('@');
2980 ret = wimlib_extract_paths(wim, image, dest_dir,
2981 (const tchar **)argv,
2983 extract_flags | notlist_extract_flags);
2987 ret = wimlib_extract_pathlist(wim, image, dest_dir,
2996 if (!imagex_be_quiet)
2997 imagex_printf(T("Done extracting files.\n"));
2998 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2999 if ((extract_flags & (WIMLIB_EXTRACT_FLAG_STRICT_GLOB |
3000 WIMLIB_EXTRACT_FLAG_GLOB_PATHS))
3001 == (WIMLIB_EXTRACT_FLAG_STRICT_GLOB |
3002 WIMLIB_EXTRACT_FLAG_GLOB_PATHS))
3005 T("Note: You can use the '--nullglob' "
3006 "option to ignore missing files.\n"));
3008 tfprintf(stderr, T("Note: You can use `%"TS"' to see what "
3009 "files and directories\n"
3010 " are in the WIM image.\n"),
3011 get_cmd_string(CMD_DIR, false));
3012 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
3013 struct wimlib_wim_info info;
3015 wimlib_get_wim_info(wim, &info);
3016 do_resource_not_found_warning(wimfile, &info, &refglobs);
3021 string_set_destroy(&refglobs);
3025 usage(CMD_EXTRACT, stderr);
3028 goto out_free_refglobs;
3031 /* Prints information about a WIM file; also can mark an image as bootable,
3032 * change the name of an image, or change the description of an image. */
3034 imagex_info(int argc, tchar **argv, int cmd)
3039 bool nocheck = false;
3040 bool header = false;
3041 bool lookup_table = false;
3043 bool short_header = true;
3044 const tchar *xml_out_file = NULL;
3045 const tchar *wimfile;
3046 const tchar *image_num_or_name;
3047 const tchar *new_name;
3048 const tchar *new_desc;
3053 struct wimlib_wim_info info;
3055 for_opt(c, info_options) {
3057 case IMAGEX_BOOT_OPTION:
3060 case IMAGEX_CHECK_OPTION:
3063 case IMAGEX_NOCHECK_OPTION:
3066 case IMAGEX_HEADER_OPTION:
3068 short_header = false;
3070 case IMAGEX_LOOKUP_TABLE_OPTION:
3071 lookup_table = true;
3072 short_header = false;
3074 case IMAGEX_XML_OPTION:
3076 short_header = false;
3078 case IMAGEX_EXTRACT_XML_OPTION:
3079 xml_out_file = optarg;
3080 short_header = false;
3082 case IMAGEX_METADATA_OPTION:
3083 imagex_error(T("The --metadata option has been removed. "
3084 "Use 'wimdir --detail' instead."));
3093 if (argc < 1 || argc > 4)
3097 image_num_or_name = (argc >= 2) ? argv[1] : T("all");
3098 new_name = (argc >= 3) ? argv[2] : NULL;
3099 new_desc = (argc >= 4) ? argv[3] : NULL;
3101 if (check && nocheck) {
3102 imagex_error(T("Can't specify both --check and --nocheck"));
3107 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3109 ret = wimlib_open_wim_with_progress(wimfile, open_flags, &wim,
3110 imagex_progress_func, NULL);
3114 wimlib_get_wim_info(wim, &info);
3116 image = wimlib_resolve_image(wim, image_num_or_name);
3117 ret = WIMLIB_ERR_INVALID_IMAGE;
3118 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
3119 verify_image_exists(image, image_num_or_name, wimfile);
3121 imagex_error(T("If you would like to set the boot "
3122 "index to 0, specify image \"0\" with "
3123 "the --boot flag."));
3125 goto out_wimlib_free;
3128 if (boot && info.image_count == 0) {
3129 imagex_error(T("--boot is meaningless on a WIM with no images"));
3130 goto out_wimlib_free;
3133 if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
3135 imagex_error(T("Cannot specify the --boot flag "
3136 "without specifying a specific "
3137 "image in a multi-image WIM"));
3138 goto out_wimlib_free;
3141 imagex_error(T("Cannot specify the NEW_NAME "
3142 "without specifying a specific "
3143 "image in a multi-image WIM"));
3144 goto out_wimlib_free;
3148 /* Operations that print information are separated from operations that
3149 * recreate the WIM file. */
3150 if (!new_name && !boot) {
3152 /* Read-only operations */
3154 if (image == WIMLIB_NO_IMAGE) {
3155 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
3156 image_num_or_name, wimfile);
3157 goto out_wimlib_free;
3160 if (image == WIMLIB_ALL_IMAGES && short_header)
3161 print_wim_information(wimfile, &info);
3164 wimlib_print_header(wim);
3167 if (info.total_parts != 1) {
3168 tfprintf(stderr, T("Warning: Only showing the lookup table "
3169 "for part %d of a %d-part WIM.\n"),
3170 info.part_number, info.total_parts);
3172 print_lookup_table(wim);
3176 ret = wimlib_extract_xml_data(wim, stdout);
3178 goto out_wimlib_free;
3184 fp = tfopen(xml_out_file, T("wb"));
3186 imagex_error_with_errno(T("Failed to open the "
3187 "file \"%"TS"\" for "
3191 goto out_wimlib_free;
3193 ret = wimlib_extract_xml_data(wim, fp);
3195 imagex_error(T("Failed to close the file "
3201 goto out_wimlib_free;
3205 wimlib_print_available_images(wim, image);
3210 /* Modification operations */
3212 if (image == WIMLIB_ALL_IMAGES)
3215 if (image == WIMLIB_NO_IMAGE && new_name) {
3216 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
3217 "when using image 0"), new_name);
3219 goto out_wimlib_free;
3223 if (image == info.boot_index) {
3224 imagex_printf(T("Image %d is already marked as "
3225 "bootable.\n"), image);
3228 imagex_printf(T("Marking image %d as bootable.\n"),
3230 info.boot_index = image;
3231 ret = wimlib_set_wim_info(wim, &info,
3232 WIMLIB_CHANGE_BOOT_INDEX);
3234 goto out_wimlib_free;
3238 if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
3240 imagex_printf(T("Image %d is already named \"%"TS"\".\n"),
3244 imagex_printf(T("Changing the name of image %d to "
3245 "\"%"TS"\".\n"), image, new_name);
3246 ret = wimlib_set_image_name(wim, image, new_name);
3248 goto out_wimlib_free;
3252 const tchar *old_desc;
3253 old_desc = wimlib_get_image_description(wim, image);
3254 if (old_desc && !tstrcmp(old_desc, new_desc)) {
3255 imagex_printf(T("The description of image %d is already "
3256 "\"%"TS"\".\n"), image, new_desc);
3259 imagex_printf(T("Changing the description of image %d "
3260 "to \"%"TS"\".\n"), image, new_desc);
3261 ret = wimlib_set_image_descripton(wim, image,
3264 goto out_wimlib_free;
3268 /* Only call wimlib_overwrite() if something actually needs to
3270 if (boot || new_name || new_desc ||
3271 (check && !info.has_integrity_table) ||
3272 (nocheck && info.has_integrity_table))
3274 int write_flags = 0;
3277 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3279 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3280 ret = wimlib_overwrite(wim, write_flags, 1);
3282 imagex_printf(T("The file \"%"TS"\" was not modified "
3283 "because nothing needed to be done.\n"),
3294 usage(CMD_INFO, stderr);
3300 /* Join split WIMs into one part WIM */
3302 imagex_join(int argc, tchar **argv, int cmd)
3305 int swm_open_flags = 0;
3306 int wim_write_flags = 0;
3307 const tchar *output_path;
3310 for_opt(c, join_options) {
3312 case IMAGEX_CHECK_OPTION:
3313 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3314 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3324 imagex_error(T("Must specify one or more split WIM (.swm) "
3328 output_path = argv[0];
3329 ret = wimlib_join_with_progress((const tchar * const *)++argv,
3334 imagex_progress_func,
3340 usage(CMD_JOIN, stderr);
3345 #if WIM_MOUNTING_SUPPORTED
3347 /* Mounts a WIM image. */
3349 imagex_mount_rw_or_ro(int argc, tchar **argv, int cmd)
3352 int mount_flags = 0;
3354 const tchar *staging_dir = NULL;
3355 const tchar *wimfile;
3358 struct wimlib_wim_info info;
3362 STRING_SET(refglobs);
3364 if (cmd == CMD_MOUNTRW) {
3365 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
3366 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3369 for_opt(c, mount_options) {
3371 case IMAGEX_ALLOW_OTHER_OPTION:
3372 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
3374 case IMAGEX_CHECK_OPTION:
3375 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3377 case IMAGEX_DEBUG_OPTION:
3378 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
3380 case IMAGEX_STREAMS_INTERFACE_OPTION:
3381 if (!tstrcasecmp(optarg, T("none")))
3382 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
3383 else if (!tstrcasecmp(optarg, T("xattr")))
3384 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
3385 else if (!tstrcasecmp(optarg, T("windows")))
3386 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
3388 imagex_error(T("Unknown stream interface \"%"TS"\""),
3393 case IMAGEX_REF_OPTION:
3394 ret = string_set_append(&refglobs, optarg);
3396 goto out_free_refglobs;
3398 case IMAGEX_STAGING_DIR_OPTION:
3399 staging_dir = optarg;
3401 case IMAGEX_UNIX_DATA_OPTION:
3402 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
3410 if (argc != 2 && argc != 3)
3415 ret = wimlib_open_wim_with_progress(wimfile, open_flags, &wim,
3416 imagex_progress_func, NULL);
3418 goto out_free_refglobs;
3420 wimlib_get_wim_info(wim, &info);
3423 /* Image explicitly specified. */
3424 image = wimlib_resolve_image(wim, argv[1]);
3426 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
3430 /* No image specified; default to image 1, but only if the WIM
3431 * contains exactly one image. */
3433 if (info.image_count != 1) {
3434 imagex_error(T("\"%"TS"\" contains %d images; Please "
3435 "select one."), wimfile, info.image_count);
3443 if (refglobs.num_strings) {
3444 ret = wim_reference_globs(wim, &refglobs, open_flags);
3449 ret = wimlib_mount_image(wim, image, dir, mount_flags, staging_dir);
3451 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
3453 image, wimfile, dir);
3458 string_set_destroy(&refglobs);
3464 goto out_free_refglobs;
3466 #endif /* WIM_MOUNTING_SUPPORTED */
3468 /* Rebuild a WIM file */
3470 imagex_optimize(int argc, tchar **argv, int cmd)
3473 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3474 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
3475 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
3476 uint32_t chunk_size = UINT32_MAX;
3477 uint32_t pack_chunk_size = UINT32_MAX;
3478 int pack_ctype = WIMLIB_COMPRESSION_TYPE_INVALID;
3481 const tchar *wimfile;
3484 unsigned num_threads = 0;
3486 for_opt(c, optimize_options) {
3488 case IMAGEX_CHECK_OPTION:
3489 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3490 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3492 case IMAGEX_NOCHECK_OPTION:
3493 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3495 case IMAGEX_COMPRESS_OPTION:
3496 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3497 compression_type = get_compression_type(optarg);
3498 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
3501 case IMAGEX_RECOMPRESS_OPTION:
3502 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3504 case IMAGEX_COMPRESS_SLOW_OPTION:
3505 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3506 set_compress_slow();
3508 case IMAGEX_CHUNK_SIZE_OPTION:
3509 chunk_size = parse_chunk_size(optarg);
3510 if (chunk_size == UINT32_MAX)
3513 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
3514 pack_chunk_size = parse_chunk_size(optarg);
3515 if (pack_chunk_size == UINT32_MAX)
3518 case IMAGEX_PACK_COMPRESS_OPTION:
3519 pack_ctype = get_compression_type(optarg);
3520 if (pack_ctype == WIMLIB_COMPRESSION_TYPE_INVALID)
3523 case IMAGEX_PACK_STREAMS_OPTION:
3524 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
3525 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3527 case IMAGEX_THREADS_OPTION:
3528 num_threads = parse_num_threads(optarg);
3529 if (num_threads == UINT_MAX)
3532 case IMAGEX_PIPABLE_OPTION:
3533 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
3535 case IMAGEX_NOT_PIPABLE_OPTION:
3536 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
3550 ret = wimlib_open_wim_with_progress(wimfile, open_flags, &wim,
3551 imagex_progress_func, NULL);
3555 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
3556 /* Change compression type. */
3557 ret = wimlib_set_output_compression_type(wim, compression_type);
3559 goto out_wimlib_free;
3562 if (chunk_size != UINT32_MAX) {
3563 /* Change chunk size. */
3564 ret = wimlib_set_output_chunk_size(wim, chunk_size);
3566 goto out_wimlib_free;
3568 if (pack_ctype != WIMLIB_COMPRESSION_TYPE_INVALID) {
3569 ret = wimlib_set_output_pack_compression_type(wim, pack_ctype);
3571 goto out_wimlib_free;
3573 if (pack_chunk_size != UINT32_MAX) {
3574 ret = wimlib_set_output_pack_chunk_size(wim, pack_chunk_size);
3576 goto out_wimlib_free;
3579 old_size = file_get_size(wimfile);
3580 tprintf(T("\"%"TS"\" original size: "), wimfile);
3582 tputs(T("Unknown"));
3584 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3586 ret = wimlib_overwrite(wim, write_flags, num_threads);
3588 imagex_error(T("Optimization of \"%"TS"\" failed."), wimfile);
3589 goto out_wimlib_free;
3592 new_size = file_get_size(wimfile);
3593 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3595 tputs(T("Unknown"));
3597 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3599 tfputs(T("Space saved: "), stdout);
3600 if (new_size != -1 && old_size != -1) {
3601 tprintf(T("%lld KiB\n"),
3602 ((long long)old_size - (long long)new_size) >> 10);
3604 tputs(T("Unknown"));
3613 usage(CMD_OPTIMIZE, stderr);
3619 /* Split a WIM into a spanned set */
3621 imagex_split(int argc, tchar **argv, int cmd)
3625 int write_flags = 0;
3626 unsigned long part_size;
3631 for_opt(c, split_options) {
3633 case IMAGEX_CHECK_OPTION:
3634 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3635 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3647 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3648 if (tmp == argv[2] || *tmp) {
3649 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3650 imagex_error(T("The part size must be an integer or "
3651 "floating-point number of megabytes."));
3654 ret = wimlib_open_wim_with_progress(argv[0], open_flags, &wim,
3655 imagex_progress_func, NULL);
3659 ret = wimlib_split(wim, argv[1], part_size, write_flags);
3665 usage(CMD_SPLIT, stderr);
3671 #if WIM_MOUNTING_SUPPORTED
3672 /* Unmounts a mounted WIM image. */
3674 imagex_unmount(int argc, tchar **argv, int cmd)
3677 int unmount_flags = 0;
3680 for_opt(c, unmount_options) {
3682 case IMAGEX_COMMIT_OPTION:
3683 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3685 case IMAGEX_CHECK_OPTION:
3686 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3688 case IMAGEX_REBUILD_OPTION:
3689 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3691 case IMAGEX_LAZY_OPTION:
3692 case IMAGEX_FORCE_OPTION:
3693 /* Now, unmount is lazy by default. However, committing
3694 * the image will fail with
3695 * WIMLIB_ERR_MOUNTED_IMAGE_IS_BUSY if there are open
3696 * file descriptors on the WIM image. The
3697 * WIMLIB_UNMOUNT_FLAG_FORCE option forces these file
3698 * descriptors to be closed. */
3699 unmount_flags |= WIMLIB_UNMOUNT_FLAG_FORCE;
3701 case IMAGEX_NEW_IMAGE_OPTION:
3702 unmount_flags |= WIMLIB_UNMOUNT_FLAG_NEW_IMAGE;
3713 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_NEW_IMAGE) {
3714 if (!(unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT)) {
3715 imagex_error(T("--new-image is meaningless "
3716 "without --commit also specified!"));
3721 ret = wimlib_unmount_image_with_progress(argv[0], unmount_flags,
3722 imagex_progress_func, NULL);
3724 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3725 if (ret == WIMLIB_ERR_MOUNTED_IMAGE_IS_BUSY) {
3727 "\tNote: Use --commit --force to force changes "
3728 "to be committed, regardless\n"
3729 "\t of open files.\n"));
3736 usage(CMD_UNMOUNT, stderr);
3741 #endif /* WIM_MOUNTING_SUPPORTED */
3744 * Add, delete, or rename files in a WIM image.
3747 imagex_update(int argc, tchar **argv, int cmd)
3749 const tchar *wimfile;
3753 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3754 int write_flags = 0;
3755 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3756 int default_add_flags = WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
3757 WIMLIB_ADD_FLAG_VERBOSE |
3758 WIMLIB_ADD_FLAG_WINCONFIG;
3759 int default_delete_flags = 0;
3760 unsigned num_threads = 0;
3762 tchar *cmd_file_contents;
3763 size_t cmd_file_nchars;
3764 struct wimlib_update_command *cmds;
3766 tchar *command_str = NULL;
3767 tchar *config_file = NULL;
3768 tchar *wimboot_config = NULL;
3770 for_opt(c, update_options) {
3772 /* Generic or write options */
3773 case IMAGEX_THREADS_OPTION:
3774 num_threads = parse_num_threads(optarg);
3775 if (num_threads == UINT_MAX)
3778 case IMAGEX_CHECK_OPTION:
3779 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3780 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3782 case IMAGEX_REBUILD_OPTION:
3783 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3785 case IMAGEX_COMMAND_OPTION:
3787 imagex_error(T("--command may only be specified "
3788 "one time. Please provide\n"
3789 " the update commands "
3790 "on standard input instead."));
3793 command_str = tstrdup(optarg);
3795 imagex_error(T("Out of memory!"));
3799 case IMAGEX_WIMBOOT_CONFIG_OPTION:
3800 wimboot_config = optarg;
3802 /* Default delete options */
3803 case IMAGEX_FORCE_OPTION:
3804 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3806 case IMAGEX_RECURSIVE_OPTION:
3807 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3810 /* Global add option */
3811 case IMAGEX_CONFIG_OPTION:
3812 default_add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
3813 config_file = optarg;
3816 /* Default add options */
3817 case IMAGEX_VERBOSE_OPTION:
3818 /* No longer does anything. */
3820 case IMAGEX_DEREFERENCE_OPTION:
3821 default_add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
3823 case IMAGEX_UNIX_DATA_OPTION:
3824 default_add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
3826 case IMAGEX_NO_ACLS_OPTION:
3827 default_add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
3829 case IMAGEX_STRICT_ACLS_OPTION:
3830 default_add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
3832 case IMAGEX_NO_REPLACE_OPTION:
3833 default_add_flags |= WIMLIB_ADD_FLAG_NO_REPLACE;
3842 if (argc != 1 && argc != 2)
3846 ret = wimlib_open_wim_with_progress(wimfile, open_flags, &wim,
3847 imagex_progress_func, NULL);
3849 goto out_free_command_str;
3852 /* Image explicitly specified. */
3853 image = wimlib_resolve_image(wim, argv[1]);
3854 ret = verify_image_exists_and_is_single(image, argv[1],
3857 goto out_wimlib_free;
3859 /* No image specified; default to image 1, but only if the WIM
3860 * contains exactly one image. */
3861 struct wimlib_wim_info info;
3863 wimlib_get_wim_info(wim, &info);
3864 if (info.image_count != 1) {
3865 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3866 wimfile, info.image_count);
3873 /* Read update commands from standard input, or the command string if
3876 cmd_file_contents = NULL;
3877 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3881 goto out_free_cmd_file_contents;
3883 } else if (!wimboot_config) {
3884 if (isatty(STDIN_FILENO)) {
3885 tputs(T("Reading update commands from standard input..."));
3886 recommend_man_page(CMD_UPDATE, stdout);
3888 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3889 if (!cmd_file_contents) {
3891 goto out_wimlib_free;
3894 /* Parse the update commands */
3895 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3899 goto out_free_cmd_file_contents;
3902 cmd_file_contents = NULL;
3907 /* Set default flags and capture config on the update commands */
3908 for (size_t i = 0; i < num_cmds; i++) {
3909 switch (cmds[i].op) {
3910 case WIMLIB_UPDATE_OP_ADD:
3911 cmds[i].add.add_flags |= default_add_flags;
3912 cmds[i].add.config_file = config_file;
3914 case WIMLIB_UPDATE_OP_DELETE:
3915 cmds[i].delete_.delete_flags |= default_delete_flags;
3922 /* Execute the update commands */
3923 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags);
3927 if (wimboot_config) {
3928 /* --wimboot-config=FILE is short for an
3929 * "add FILE /Windows/System32/WimBootCompress.ini" command.
3931 struct wimlib_update_command cmd;
3933 cmd.op = WIMLIB_UPDATE_OP_ADD;
3934 cmd.add.fs_source_path = wimboot_config;
3935 cmd.add.wim_target_path = T("/Windows/System32/WimBootCompress.ini");
3936 cmd.add.config_file = NULL;
3937 cmd.add.add_flags = 0;
3939 ret = wimlib_update_image(wim, image, &cmd, 1, update_flags);
3944 /* Overwrite the updated WIM */
3945 ret = wimlib_overwrite(wim, write_flags, num_threads);
3948 out_free_cmd_file_contents:
3949 free(cmd_file_contents);
3952 out_free_command_str:
3957 usage(CMD_UPDATE, stderr);
3960 goto out_free_command_str;
3965 struct imagex_command {
3967 int (*func)(int argc, tchar **argv, int cmd);
3970 static const struct imagex_command imagex_commands[] = {
3971 [CMD_APPEND] = {T("append"), imagex_capture_or_append},
3972 [CMD_APPLY] = {T("apply"), imagex_apply},
3973 [CMD_CAPTURE] = {T("capture"), imagex_capture_or_append},
3974 [CMD_DELETE] = {T("delete"), imagex_delete},
3975 [CMD_DIR ] = {T("dir"), imagex_dir},
3976 [CMD_EXPORT] = {T("export"), imagex_export},
3977 [CMD_EXTRACT] = {T("extract"), imagex_extract},
3978 [CMD_INFO] = {T("info"), imagex_info},
3979 [CMD_JOIN] = {T("join"), imagex_join},
3980 #if WIM_MOUNTING_SUPPORTED
3981 [CMD_MOUNT] = {T("mount"), imagex_mount_rw_or_ro},
3982 [CMD_MOUNTRW] = {T("mountrw"), imagex_mount_rw_or_ro},
3984 [CMD_OPTIMIZE] = {T("optimize"), imagex_optimize},
3985 [CMD_SPLIT] = {T("split"), imagex_split},
3986 #if WIM_MOUNTING_SUPPORTED
3987 [CMD_UNMOUNT] = {T("unmount"), imagex_unmount},
3989 [CMD_UPDATE] = {T("update"), imagex_update},
3992 static const tchar *usage_strings[] = {
3995 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3996 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3997 " [--nocheck] [--flags EDITION_ID] [--dereference]\n"
3998 " [--config=FILE] [--threads=NUM_THREADS] [--source-list]\n"
3999 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
4000 " [--update-of=[WIMFILE:]IMAGE] [--wimboot]\n"
4004 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME | all)]\n"
4005 " (DIRECTORY | NTFS_VOLUME) [--check] [--ref=\"GLOB\"]\n"
4006 " [--no-acls] [--strict-acls] [--no-attributes]\n"
4007 " [--rpfix] [--norpfix] [--include-invalid-names]\n"
4008 " [--wimboot] [--unix-data]\n"
4012 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
4013 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
4014 " [--nocheck] [--compress=TYPE] [--flags EDITION_ID]\n"
4015 " [--dereference] [--config=FILE] [--threads=NUM_THREADS]\n"
4016 " [--source-list] [--no-acls] [--strict-acls] [--rpfix]\n"
4017 " [--norpfix] [--update-of=[WIMFILE:]IMAGE]\n"
4018 " [--delta-from=WIMFILE] [--wimboot] [--unix-data]\n"
4022 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n"
4023 " [--check] [--soft]\n"
4027 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH] [--detailed]\n"
4031 " %"TS" SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
4032 " DEST_WIMFILE [DEST_IMAGE_NAME [DEST_IMAGE_DESCRIPTION]]\n"
4033 " [--boot] [--check] [--nocheck] [--compress=TYPE]\n"
4034 " [--ref=\"GLOB\"] [--threads=NUM_THREADS] [--rebuild]\n"
4039 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME) [(PATH | @LISTFILE)...]\n"
4040 " [--check] [--ref=\"GLOB\"] [--dest-dir=CMD_DIR]\n"
4041 " [--to-stdout] [--no-acls] [--strict-acls]\n"
4042 " [--no-attributes] [--include-invalid-names]\n"
4043 " [--no-globs] [--nullglob] [--preserve-dir-structure]\n"
4047 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME) [NEW_NAME\n"
4048 " [NEW_DESC]]] [--boot] [--check] [--nocheck] [--xml]\n"
4049 " [--extract-xml FILE] [--header] [--lookup-table]\n"
4053 " %"TS" OUT_WIMFILE SPLIT_WIM_PART... [--check]\n"
4055 #if WIM_MOUNTING_SUPPORTED
4058 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
4059 " [--check] [--streams-interface=INTERFACE]\n"
4060 " [--ref=\"GLOB\"] [--allow-other] [--unix-data]\n"
4064 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
4065 " [--check] [--streams-interface=INTERFACE]\n"
4066 " [--staging-dir=CMD_DIR] [--allow-other] [--unix-data]\n"
4071 " %"TS" WIMFILE [--check] [--nocheck] [--recompress]\n"
4072 " [--recompress-slow] [--compress=TYPE]\n"
4073 " [--threads=NUM_THREADS]\n"
4077 " %"TS" WIMFILE SPLIT_WIM_PART_1 PART_SIZE_MB [--check]\n"
4079 #if WIM_MOUNTING_SUPPORTED
4082 " %"TS" DIRECTORY [--commit] [--force] [--new-image]\n"
4083 " [--check] [--rebuild]\n"
4088 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
4089 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
4090 " [DEFAULT_DELETE_OPTIONS] [--command=STRING]\n"
4091 " [--wimboot-config=FILE| [< CMDFILE]\n"
4095 static const tchar *invocation_name;
4096 static int invocation_cmd = CMD_NONE;
4098 static const tchar *get_cmd_string(int cmd, bool nospace)
4100 static tchar buf[50];
4101 if (cmd == CMD_NONE) {
4102 tsprintf(buf, T("%"TS), T(IMAGEX_PROGNAME));
4103 } else if (invocation_cmd != CMD_NONE) {
4104 tsprintf(buf, T("wim%"TS), imagex_commands[cmd].name);
4106 const tchar *format;
4109 format = T("%"TS"-%"TS"");
4111 format = T("%"TS" %"TS"");
4112 tsprintf(buf, format, invocation_name, imagex_commands[cmd].name);
4120 static const tchar *s =
4122 IMAGEX_PROGNAME " (distributed with " PACKAGE " " PACKAGE_VERSION ")\n"
4123 "Copyright (C) 2012, 2013, 2014 Eric Biggers\n"
4124 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
4125 "This is free software: you are free to change and redistribute it.\n"
4126 "There is NO WARRANTY, to the extent permitted by law.\n"
4128 "Report bugs to "PACKAGE_BUGREPORT".\n"
4135 help_or_version(int argc, tchar **argv, int cmd)
4140 for (i = 1; i < argc; i++) {
4142 if (p[0] == T('-') && p[1] == T('-')) {
4144 if (!tstrcmp(p, T("help"))) {
4145 if (cmd == CMD_NONE)
4150 } else if (!tstrcmp(p, T("version"))) {
4159 print_usage_string(int cmd, FILE *fp)
4161 tfprintf(fp, usage_strings[cmd], get_cmd_string(cmd, false));
4165 recommend_man_page(int cmd, FILE *fp)
4167 const tchar *format_str;
4169 format_str = T("Uncommon options are not listed;\n"
4170 "See %"TS".pdf in the doc directory for more details.\n");
4172 format_str = T("Uncommon options are not listed;\n"
4173 "Try `man %"TS"' for more details.\n");
4175 tfprintf(fp, format_str, get_cmd_string(cmd, true));
4179 usage(int cmd, FILE *fp)
4181 tfprintf(fp, T("Usage:\n"));
4182 print_usage_string(cmd, fp);
4183 tfprintf(fp, T("\n"));
4184 recommend_man_page(cmd, fp);
4190 tfprintf(fp, T("Usage:\n"));
4191 for (int cmd = 0; cmd < CMD_MAX; cmd++) {
4192 print_usage_string(cmd, fp);
4193 tfprintf(fp, T("\n"));
4195 static const tchar *extra =
4198 " %"TS" --version\n"
4200 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
4203 tfprintf(fp, extra, invocation_name, invocation_name);
4204 recommend_man_page(CMD_NONE, fp);
4207 /* Entry point for wimlib's ImageX implementation. On UNIX the command
4208 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
4209 * something else), while on Windows the command arguments will be UTF-16LE
4210 * encoded 'wchar_t' strings. */
4213 wmain(int argc, wchar_t **argv, wchar_t **envp)
4215 main(int argc, char **argv)
4222 imagex_info_file = stdout;
4223 invocation_name = tbasename(argv[0]);
4226 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
4227 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
4231 setlocale(LC_ALL, "");
4232 codeset = nl_langinfo(CODESET);
4233 if (!strstr(codeset, "UTF-8") &&
4234 !strstr(codeset, "UTF8") &&
4235 !strstr(codeset, "utf-8") &&
4236 !strstr(codeset, "utf8"))
4239 "WARNING: Running %"TS" in a UTF-8 locale is recommended!\n"
4240 " Maybe try: `export LANG=en_US.UTF-8'?\n"
4241 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
4242 " to any value to force wimlib to use UTF-8.\n",
4248 #endif /* !__WIN32__ */
4251 tchar *igcase = tgetenv(T("WIMLIB_IMAGEX_IGNORE_CASE"));
4252 if (igcase != NULL) {
4253 if (!tstrcmp(igcase, T("no")) ||
4254 !tstrcmp(igcase, T("0")))
4255 init_flags |= WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE;
4256 else if (!tstrcmp(igcase, T("yes")) ||
4257 !tstrcmp(igcase, T("1")))
4258 init_flags |= WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE;
4261 "WARNING: Ignoring unknown setting of "
4262 "WIMLIB_IMAGEX_IGNORE_CASE\n");
4267 /* Allow being invoked as wimCOMMAND (e.g. wimapply). */
4269 if (!tstrncmp(invocation_name, T("wim"), 3) &&
4270 tstrcmp(invocation_name, T(IMAGEX_PROGNAME))) {
4271 for (int i = 0; i < CMD_MAX; i++) {
4272 if (!tstrcmp(invocation_name + 3,
4273 imagex_commands[i].name))
4282 /* Unless already known from the invocation name, determine which
4283 * command was specified. */
4284 if (cmd == CMD_NONE) {
4286 imagex_error(T("No command specified!\n"));
4290 for (int i = 0; i < CMD_MAX; i++) {
4291 if (!tstrcmp(argv[1], imagex_commands[i].name)) {
4296 if (cmd != CMD_NONE) {
4302 /* Handle --help and --version. --help can be either for the program as
4303 * a whole (cmd == CMD_NONE) or just for a specific command (cmd !=
4304 * CMD_NONE). Note: help_or_version() will not return if a --help or
4305 * --version argument was found. */
4306 help_or_version(argc, argv, cmd);
4308 /* Bail if a valid command was not specified. */
4309 if (cmd == CMD_NONE) {
4310 imagex_error(T("Unrecognized command: `%"TS"'\n"), argv[1]);
4315 /* Enable warning and error messages in wimlib to be more user-friendly.
4317 wimlib_set_print_errors(true);
4319 /* Initialize wimlib. */
4320 ret = wimlib_global_init(init_flags);
4322 goto out_check_status;
4324 /* Call the command handler function. */
4325 ret = imagex_commands[cmd].func(argc, argv, cmd);
4327 /* Check for error writing to standard output, especially since for some
4328 * commands, writing to standard output is part of the program's actual
4329 * behavior and not just for informational purposes. */
4330 if (ferror(stdout) || fclose(stdout)) {
4331 imagex_error_with_errno(T("error writing to standard output"));
4336 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
4337 * itself (not by wimlib). 0 indicates success. > 0 indicates a wimlib
4338 * error code from which an error message can be printed. */
4340 imagex_error(T("Exiting with error code %d:\n"
4342 wimlib_get_error_string(ret));
4343 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
4344 imagex_error_with_errno(T("errno"));
4346 /* Make wimlib free any resources it's holding (although this is not
4347 * strictly necessary because the process is ending anyway). */
4348 wimlib_global_cleanup();