4 * Use wimlib to create, modify, extract, mount, unmount, or display information
9 * Copyright (C) 2012, 2013, 2014 Eric Biggers
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 # include "config.h" /* Need for PACKAGE_VERSION, etc. */
30 #include "wimlib_tchar.h"
50 # include "imagex-win32.h"
51 # define OS_PREFERRED_PATH_SEPARATOR L'\\'
52 # define OS_PREFERRED_PATH_SEPARATOR_STRING L"\\"
53 # define print_security_descriptor win32_print_security_descriptor
56 # include <langinfo.h>
57 # define OS_PREFERRED_PATH_SEPARATOR '/'
58 # define OS_PREFERRED_PATH_SEPARATOR_STRING "/"
59 # define print_security_descriptor default_print_security_descriptor
60 static inline void set_fd_to_binary_mode(int fd)
65 /* Don't confuse the user by presenting the mounting commands on Windows when
66 * they will never work. However on UNIX-like systems we always present them,
67 * even if WITH_FUSE is not defined at this point, as to not tie the build of
68 * wimlib-imagex to a specific build of wimlib. */
70 # define WIM_MOUNTING_SUPPORTED 0
72 # define WIM_MOUNTING_SUPPORTED 1
75 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
78 is_any_path_separator(tchar c)
80 return c == T('/') || c == T('\\');
83 /* Like basename(), but handles both forward and backwards slashes. */
85 tbasename(tchar *path)
87 tchar *p = tstrchr(path, T('\0'));
92 if (!is_any_path_separator(*--p))
100 if (is_any_path_separator(*--p))
105 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
119 #if WIM_MOUNTING_SUPPORTED
125 #if WIM_MOUNTING_SUPPORTED
132 static void usage(int cmd, FILE *fp);
133 static void usage_all(FILE *fp);
134 static void recommend_man_page(int cmd, FILE *fp);
135 static const tchar *get_cmd_string(int cmd, bool nospace);
137 static int imagex_progress_func(enum wimlib_progress_msg msg,
138 const union wimlib_progress_info *info);
140 static bool imagex_be_quiet = false;
141 static FILE *imagex_info_file;
143 #define imagex_printf(format, ...) \
144 tfprintf(imagex_info_file, format, ##__VA_ARGS__)
147 IMAGEX_ALLOW_OTHER_OPTION,
150 IMAGEX_CHUNK_SIZE_OPTION,
151 IMAGEX_COMMAND_OPTION,
152 IMAGEX_COMMIT_OPTION,
153 IMAGEX_COMPRESS_OPTION,
154 IMAGEX_COMPRESS_SLOW_OPTION,
155 IMAGEX_CONFIG_OPTION,
157 IMAGEX_DELTA_FROM_OPTION,
158 IMAGEX_DEREFERENCE_OPTION,
159 IMAGEX_DEST_DIR_OPTION,
160 IMAGEX_DETAILED_OPTION,
161 IMAGEX_EXTRACT_XML_OPTION,
164 IMAGEX_HARDLINK_OPTION,
165 IMAGEX_HEADER_OPTION,
166 IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
168 IMAGEX_LOOKUP_TABLE_OPTION,
169 IMAGEX_METADATA_OPTION,
170 IMAGEX_NEW_IMAGE_OPTION,
171 IMAGEX_NOCHECK_OPTION,
172 IMAGEX_NORPFIX_OPTION,
173 IMAGEX_NOT_PIPABLE_OPTION,
174 IMAGEX_NO_ACLS_OPTION,
175 IMAGEX_NO_ATTRIBUTES_OPTION,
176 IMAGEX_NO_WILDCARDS_OPTION,
177 IMAGEX_NULLGLOB_OPTION,
178 IMAGEX_ONE_FILE_ONLY_OPTION,
179 IMAGEX_PACK_CHUNK_SIZE_OPTION,
180 IMAGEX_PACK_COMPRESS_OPTION,
181 IMAGEX_PACK_STREAMS_OPTION,
183 IMAGEX_PIPABLE_OPTION,
184 IMAGEX_PRESERVE_DIR_STRUCTURE_OPTION,
185 IMAGEX_REBUILD_OPTION,
186 IMAGEX_RECOMPRESS_OPTION,
187 IMAGEX_RECURSIVE_OPTION,
189 IMAGEX_RESUME_OPTION,
192 IMAGEX_SOURCE_LIST_OPTION,
193 IMAGEX_STAGING_DIR_OPTION,
194 IMAGEX_STREAMS_INTERFACE_OPTION,
195 IMAGEX_STRICT_ACLS_OPTION,
196 IMAGEX_SYMLINK_OPTION,
197 IMAGEX_THREADS_OPTION,
198 IMAGEX_TO_STDOUT_OPTION,
199 IMAGEX_UNIX_DATA_OPTION,
200 IMAGEX_UPDATE_OF_OPTION,
201 IMAGEX_VERBOSE_OPTION,
202 IMAGEX_WIMBOOT_OPTION,
206 static const struct option apply_options[] = {
207 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
208 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
209 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
210 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
211 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
212 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
213 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
214 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
215 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
216 {T("no-attributes"), no_argument, NULL, IMAGEX_NO_ATTRIBUTES_OPTION},
217 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
218 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
219 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
221 /* --resume is undocumented for now as it needs improvement. */
222 {T("resume"), no_argument, NULL, IMAGEX_RESUME_OPTION},
223 {T("wimboot"), no_argument, NULL, IMAGEX_WIMBOOT_OPTION},
227 static const struct option capture_or_append_options[] = {
228 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
229 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
230 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
231 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
232 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
233 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
234 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
235 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
236 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
237 {T("pack-compress"), required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
238 {T("solid-compress"),required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
239 {T("pack-streams"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
240 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
241 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
242 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
243 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
244 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
245 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
246 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
247 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
248 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
249 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
250 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
251 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
252 {T("rpfix"), no_argument, NULL, IMAGEX_RPFIX_OPTION},
253 {T("norpfix"), no_argument, NULL, IMAGEX_NORPFIX_OPTION},
254 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
255 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
256 {T("update-of"), required_argument, NULL, IMAGEX_UPDATE_OF_OPTION},
257 {T("delta-from"), required_argument, NULL, IMAGEX_DELTA_FROM_OPTION},
258 {T("wimboot"), no_argument, NULL, IMAGEX_WIMBOOT_OPTION},
262 static const struct option delete_options[] = {
263 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
264 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
268 static const struct option dir_options[] = {
269 {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
270 {T("detailed"), no_argument, NULL, IMAGEX_DETAILED_OPTION},
271 {T("one-file-only"), no_argument, NULL, IMAGEX_ONE_FILE_ONLY_OPTION},
275 static const struct option export_options[] = {
276 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
277 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
278 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
279 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
280 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
281 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
282 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
283 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
284 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
285 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
286 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
287 {T("pack-compress"), required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
288 {T("solid-compress"),required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
289 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
290 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
291 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
292 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
293 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
297 static const struct option extract_options[] = {
298 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
299 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
300 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
301 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
302 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
303 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
304 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
305 {T("no-attributes"), no_argument, NULL, IMAGEX_NO_ATTRIBUTES_OPTION},
306 {T("dest-dir"), required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
307 {T("to-stdout"), no_argument, NULL, IMAGEX_TO_STDOUT_OPTION},
308 {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
309 {T("no-wildcards"), no_argument, NULL, IMAGEX_NO_WILDCARDS_OPTION},
310 {T("nullglob"), no_argument, NULL, IMAGEX_NULLGLOB_OPTION},
311 {T("preserve-dir-structure"), no_argument, NULL, IMAGEX_PRESERVE_DIR_STRUCTURE_OPTION},
312 {T("wimboot"), no_argument, NULL, IMAGEX_WIMBOOT_OPTION},
316 static const struct option info_options[] = {
317 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
318 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
319 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
320 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
321 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
322 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
323 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
324 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
325 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
329 static const struct option join_options[] = {
330 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
334 static const struct option mount_options[] = {
335 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
336 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
337 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
338 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
339 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
340 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
341 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
345 static const struct option optimize_options[] = {
346 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
347 {T("nocheck"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
348 {T("no-check"), no_argument, NULL, IMAGEX_NOCHECK_OPTION},
349 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
350 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
351 {T("compress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
352 {T("recompress-slow"), no_argument, NULL, IMAGEX_COMPRESS_SLOW_OPTION},
353 {T("chunk-size"), required_argument, NULL, IMAGEX_CHUNK_SIZE_OPTION},
354 {T("pack-chunk-size"), required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
355 {T("solid-chunk-size"),required_argument, NULL, IMAGEX_PACK_CHUNK_SIZE_OPTION},
356 {T("pack-compress"), required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
357 {T("solid-compress"),required_argument, NULL, IMAGEX_PACK_COMPRESS_OPTION},
358 {T("pack-streams"),no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
359 {T("solid"), no_argument, NULL, IMAGEX_PACK_STREAMS_OPTION},
360 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
361 {T("pipable"), no_argument, NULL, IMAGEX_PIPABLE_OPTION},
362 {T("not-pipable"), no_argument, NULL, IMAGEX_NOT_PIPABLE_OPTION},
366 static const struct option split_options[] = {
367 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
371 static const struct option unmount_options[] = {
372 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
373 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
374 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
375 {T("lazy"), no_argument, NULL, IMAGEX_LAZY_OPTION},
376 {T("new-image"), no_argument, NULL, IMAGEX_NEW_IMAGE_OPTION},
380 static const struct option update_options[] = {
381 /* Careful: some of the options here set the defaults for update
382 * commands, but the flags given to an actual update command (and not to
383 * `imagex update' itself are also handled in
384 * update_command_add_option(). */
385 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
386 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
387 {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
388 {T("command"), required_argument, NULL, IMAGEX_COMMAND_OPTION},
390 /* Default delete options */
391 {T("force"), no_argument, NULL, IMAGEX_FORCE_OPTION},
392 {T("recursive"), no_argument, NULL, IMAGEX_RECURSIVE_OPTION},
394 /* Global add option */
395 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
397 /* Default add options */
398 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
399 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
400 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
401 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
402 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
403 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
409 # define _format_attribute(type, format_str, args_start) \
410 __attribute__((format(type, format_str, args_start)))
412 # define _format_attribute(type, format_str, args_start)
415 /* Print formatted error message to stderr. */
416 static void _format_attribute(printf, 1, 2)
417 imagex_error(const tchar *format, ...)
420 va_start(va, format);
421 tfputs(T("ERROR: "), stderr);
422 tvfprintf(stderr, format, va);
423 tputc(T('\n'), stderr);
427 /* Print formatted error message to stderr. */
428 static void _format_attribute(printf, 1, 2)
429 imagex_error_with_errno(const tchar *format, ...)
431 int errno_save = errno;
433 va_start(va, format);
434 tfputs(T("ERROR: "), stderr);
435 tvfprintf(stderr, format, va);
436 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
441 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
443 if (image == WIMLIB_NO_IMAGE) {
444 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
445 " Please specify a 1-based image index or "
446 "image name. To list the images\n"
447 " contained in the WIM archive, run\n"
449 " %"TS" \"%"TS"\"\n"),
450 image_name, wim_name,
451 get_cmd_string(CMD_INFO, false), wim_name);
452 return WIMLIB_ERR_INVALID_IMAGE;
458 verify_image_is_single(int image)
460 if (image == WIMLIB_ALL_IMAGES) {
461 imagex_error(T("Cannot specify all images for this action!"));
462 return WIMLIB_ERR_INVALID_IMAGE;
468 verify_image_exists_and_is_single(int image, const tchar *image_name,
469 const tchar *wim_name)
472 ret = verify_image_exists(image, image_name, wim_name);
474 ret = verify_image_is_single(image);
478 /* Parse the argument to --compress */
480 get_compression_type(const tchar *optarg)
482 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
483 return WIMLIB_COMPRESSION_TYPE_LZX;
484 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
485 return WIMLIB_COMPRESSION_TYPE_XPRESS;
486 else if (!tstrcasecmp(optarg, T("recovery")) || !tstrcasecmp(optarg, T("lzms")))
487 return WIMLIB_COMPRESSION_TYPE_LZMS;
488 else if (!tstrcasecmp(optarg, T("none")))
489 return WIMLIB_COMPRESSION_TYPE_NONE;
491 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
492 "\"maximum\", \"fast\", or \"none\"."), optarg);
493 return WIMLIB_COMPRESSION_TYPE_INVALID;
498 set_compress_slow(void)
500 static const struct wimlib_lzx_compressor_params lzx_slow_params = {
502 .size = sizeof(struct wimlib_lzx_compressor_params),
504 .algorithm = WIMLIB_LZX_ALGORITHM_SLOW,
507 .use_len2_matches = 1,
508 .nice_match_length = 96,
509 .num_optim_passes = 4,
510 .max_search_depth = 100,
511 .max_matches_per_pos = 10,
512 .main_nostat_cost = 15,
513 .len_nostat_cost = 15,
514 .aligned_nostat_cost = 7,
519 static const struct wimlib_lzms_compressor_params lzms_slow_params = {
521 .size = sizeof(struct wimlib_lzms_compressor_params),
523 .min_match_length = 2,
524 .max_match_length = UINT32_MAX,
525 .nice_match_length = 96,
526 .max_search_depth = 100,
527 .max_matches_per_pos = 10,
528 .optim_array_length = 1024,
531 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
532 &lzx_slow_params.hdr);
534 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZMS,
535 &lzms_slow_params.hdr);
539 const tchar **strings;
540 unsigned num_strings;
541 unsigned num_alloc_strings;
544 #define STRING_SET_INITIALIZER \
545 { .strings = NULL, .num_strings = 0, .num_alloc_strings = 0, }
547 #define STRING_SET(_strings) \
548 struct string_set _strings = STRING_SET_INITIALIZER
551 string_set_append(struct string_set *set, const tchar *glob)
553 unsigned num_alloc_strings = set->num_alloc_strings;
555 if (set->num_strings == num_alloc_strings) {
556 const tchar **new_strings;
558 num_alloc_strings += 4;
559 new_strings = realloc(set->strings,
560 sizeof(set->strings[0]) * num_alloc_strings);
562 imagex_error(T("Out of memory!"));
565 set->strings = new_strings;
566 set->num_alloc_strings = num_alloc_strings;
568 set->strings[set->num_strings++] = glob;
573 string_set_destroy(struct string_set *set)
579 wim_reference_globs(WIMStruct *wim, struct string_set *set, int open_flags)
581 return wimlib_reference_resource_files(wim, set->strings,
583 WIMLIB_REF_FLAG_GLOB_ENABLE,
585 imagex_progress_func);
589 do_resource_not_found_warning(const tchar *wimfile,
590 const struct wimlib_wim_info *info,
591 const struct string_set *refglobs)
593 if (info->total_parts > 1) {
594 if (refglobs->num_strings == 0) {
595 imagex_error(T("\"%"TS"\" is part of a split WIM. "
596 "Use --ref to specify the other parts."),
599 imagex_error(T("Perhaps the '--ref' argument did not "
600 "specify all other parts of the split "
604 imagex_error(T("If this is a delta WIM, use the --ref argument "
605 "to specify the WIM(s) on which it is based."));
609 /* Returns the size of a file given its name, or -1 if the file does not exist
610 * or its size cannot be determined. */
612 file_get_size(const tchar *filename)
615 if (tstat(filename, &st) == 0)
622 PARSE_STRING_SUCCESS = 0,
623 PARSE_STRING_FAILURE = 1,
624 PARSE_STRING_NONE = 2,
628 * Parses a string token from an array of characters.
630 * Tokens are either whitespace-delimited, or double or single-quoted.
632 * @line_p: Pointer to the pointer to the line of data. Will be updated
633 * to point past the string token iff the return value is
634 * PARSE_STRING_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
637 * @len_p: @len_p initially stores the length of the line of data, which may
638 * be 0, and it will be updated to the number of bytes remaining in
639 * the line iff the return value is PARSE_STRING_SUCCESS.
641 * @fn_ret: Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
642 * parsed string token will be returned here.
644 * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
645 * PARSE_STRING_FAILURE if the data was invalid due to a missing
646 * closing quote; or PARSE_STRING_NONE if the line ended before the
647 * beginning of a string token was found.
650 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
653 tchar *line = *line_p;
657 /* Skip leading whitespace */
660 return PARSE_STRING_NONE;
661 if (!istspace(*line) && *line != T('\0'))
667 if (quote_char == T('"') || quote_char == T('\'')) {
672 line = tmemchr(line, quote_char, len);
674 imagex_error(T("Missing closing quote: %"TS), fn - 1);
675 return PARSE_STRING_FAILURE;
678 /* Unquoted string. Go until whitespace. Line is terminated
679 * by '\0', so no need to check 'len'. */
683 } while (!istspace(*line) && *line != T('\0'));
690 return PARSE_STRING_SUCCESS;
693 /* Parses a line of data (not an empty line or comment) in the source list file
694 * format. (See the man page for 'wimlib-imagex capture' for details on this
695 * format and the meaning.)
697 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
698 * len == 0. The data in @line will be modified by this function call.
700 * @len: Length of the line of data.
702 * @source: On success, the capture source and target described by the line is
703 * written into this destination. Note that it will contain pointers
704 * to data in the @line array.
706 * Returns true if the line was valid; false otherwise. */
708 parse_source_list_line(tchar *line, size_t len,
709 struct wimlib_capture_source *source)
713 ret = parse_string(&line, &len, &source->fs_source_path);
714 if (ret != PARSE_STRING_SUCCESS)
716 ret = parse_string(&line, &len, &source->wim_target_path);
717 if (ret == PARSE_STRING_NONE)
718 source->wim_target_path = source->fs_source_path;
719 return ret != PARSE_STRING_FAILURE;
722 /* Returns %true if the given line of length @len > 0 is a comment or empty line
723 * in the source list file format. */
725 is_comment_line(const tchar *line, size_t len)
728 if (*line == T('#') || *line == T(';'))
730 if (!istspace(*line) && *line != T('\0'))
740 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
743 tchar *contents = *contents_p;
744 size_t nchars = *nchars_p;
747 for (i = 0; i < nchars; i++)
748 if (contents[i] == T('\n'))
751 /* Handle last line not terminated by a newline */
752 if (nchars != 0 && contents[nchars - 1] != T('\n')) {
753 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
755 imagex_error(T("Out of memory!"));
758 contents[nchars] = T('\n');
759 *contents_p = contents;
767 /* Parses a file in the source list format. (See the man page for
768 * 'wimlib-imagex capture' for details on this format and the meaning.)
770 * @source_list_contents: Contents of the source list file. Note that this
771 * buffer will be modified to save memory allocations,
772 * and cannot be freed until the returned array of
773 * wimlib_capture_source's has also been freed.
775 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
778 * @nsources_ret: On success, the length of the returned array is
781 * Returns: An array of `struct wimlib_capture_source's that can be passed to
782 * the wimlib_add_image_multisource() function to specify how a WIM image is to
784 static struct wimlib_capture_source *
785 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
786 size_t *nsources_ret)
790 struct wimlib_capture_source *sources;
793 nlines = text_file_count_lines(source_list_contents_p,
794 &source_list_nchars);
798 /* Always allocate at least 1 slot, just in case the implementation of
799 * calloc() returns NULL if 0 bytes are requested. */
800 sources = calloc(nlines ?: 1, sizeof(*sources));
802 imagex_error(T("out of memory"));
805 p = *source_list_contents_p;
807 for (i = 0; i < nlines; i++) {
808 /* XXX: Could use rawmemchr() here instead, but it may not be
809 * available on all platforms. */
810 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
811 size_t len = endp - p + 1;
813 if (!is_comment_line(p, len)) {
814 if (!parse_source_list_line(p, len, &sources[j++])) {
826 /* Reads the contents of a file into memory. */
828 file_get_contents(const tchar *filename, size_t *len_ret)
835 if (tstat(filename, &stbuf) != 0) {
836 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
841 fp = tfopen(filename, T("rb"));
843 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
847 buf = malloc(len ? len : 1);
849 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
850 "contents of file \"%"TS"\""), len, filename);
853 if (fread(buf, 1, len, fp) != len) {
854 imagex_error_with_errno(T("Failed to read %zu bytes from the "
855 "file \"%"TS"\""), len, filename);
869 /* Read standard input until EOF and return the full contents in a malloc()ed
870 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
873 stdin_get_contents(size_t *len_ret)
875 /* stdin can, of course, be a pipe or other non-seekable file, so the
876 * total length of the data cannot be pre-determined */
878 size_t newlen = 1024;
882 char *p = realloc(buf, newlen);
883 size_t bytes_read, bytes_to_read;
885 imagex_error(T("out of memory while reading stdin"));
889 bytes_to_read = newlen - pos;
890 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
892 if (bytes_read != bytes_to_read) {
897 imagex_error_with_errno(T("error reading stdin"));
911 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
914 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
916 *num_tchars_ret = num_bytes;
918 #else /* !__WIN32__ */
919 /* On Windows, translate the text to UTF-16LE */
923 if (num_bytes >= 2 &&
924 (((unsigned char)text[0] == 0xff && (unsigned char)text[1] == 0xfe) ||
925 ((unsigned char)text[0] <= 0x7f && (unsigned char)text[1] == 0x00)))
927 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
928 * with something that looks like an ASCII character encoded as
929 * a UTF-16LE code unit. Assume the file is encoded as
930 * UTF-16LE. This is not a 100% reliable check. */
931 num_wchars = num_bytes / 2;
932 text_wstr = (wchar_t*)text;
934 /* File does not look like UTF-16LE. Assume it is encoded in
935 * the current Windows code page. I think these are always
936 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
937 * should work as expected. */
938 text_wstr = win32_mbs_to_wcs(text,
943 *num_tchars_ret = num_wchars;
945 #endif /* __WIN32__ */
949 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
954 contents = file_get_contents(filename, &num_bytes);
957 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
961 stdin_get_text_contents(size_t *num_tchars_ret)
966 contents = stdin_get_contents(&num_bytes);
969 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
972 #define TO_PERCENT(numerator, denominator) \
973 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
975 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
976 #define MEBIBYTE_MIN_NBYTES 10000000ULL
977 #define KIBIBYTE_MIN_NBYTES 10000ULL
980 get_unit(uint64_t total_bytes, const tchar **name_ret)
982 if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
983 *name_ret = T("GiB");
985 } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
986 *name_ret = T("MiB");
988 } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
989 *name_ret = T("KiB");
992 *name_ret = T("bytes");
997 static struct wimlib_progress_info_scan last_scan_progress;
1000 report_scan_progress(const struct wimlib_progress_info_scan *scan, bool done)
1002 uint64_t prev_count, cur_count;
1004 prev_count = last_scan_progress.num_nondirs_scanned +
1005 last_scan_progress.num_dirs_scanned;
1006 cur_count = scan->num_nondirs_scanned + scan->num_dirs_scanned;
1008 if (done || prev_count == 0 || cur_count >= prev_count + 100 ||
1009 cur_count % 128 == 0)
1011 unsigned unit_shift;
1012 const tchar *unit_name;
1014 unit_shift = get_unit(scan->num_bytes_scanned, &unit_name);
1015 imagex_printf(T("\r%"PRIu64" %"TS" scanned (%"PRIu64" files, "
1016 "%"PRIu64" directories) "),
1017 scan->num_bytes_scanned >> unit_shift,
1019 scan->num_nondirs_scanned,
1020 scan->num_dirs_scanned);
1021 last_scan_progress = *scan;
1025 /* Progress callback function passed to various wimlib functions. */
1027 imagex_progress_func(enum wimlib_progress_msg msg,
1028 const union wimlib_progress_info *info)
1030 unsigned percent_done;
1031 unsigned unit_shift;
1032 const tchar *unit_name;
1034 if (imagex_be_quiet)
1037 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1039 static bool first = true;
1041 imagex_printf(T("Writing %"TS"-compressed data "
1042 "using %u thread%"TS"\n"),
1043 wimlib_get_compression_type_string(
1044 info->write_streams.compression_type),
1045 info->write_streams.num_threads,
1046 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1050 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1051 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1052 info->write_streams.total_bytes);
1054 if (info->write_streams.total_parts <= 1) {
1055 imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1056 "written (%u%% done)"),
1057 info->write_streams.completed_bytes >> unit_shift,
1059 info->write_streams.total_bytes >> unit_shift,
1063 imagex_printf(T("\rWriting resources from part %u of %u: "
1064 "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written"),
1065 (info->write_streams.completed_parts ==
1066 info->write_streams.total_parts) ?
1067 info->write_streams.completed_parts :
1068 info->write_streams.completed_parts + 1,
1069 info->write_streams.total_parts,
1070 info->write_streams.completed_bytes >> unit_shift,
1072 info->write_streams.total_bytes >> unit_shift,
1076 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1077 imagex_printf(T("\n"));
1079 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1080 imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
1081 if (*info->scan.wim_target_path) {
1082 imagex_printf(T(" (loading as WIM path: "
1083 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
1084 info->scan.wim_target_path);
1086 imagex_printf(T("\n"));
1088 memset(&last_scan_progress, 0, sizeof(last_scan_progress));
1090 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1091 switch (info->scan.status) {
1092 case WIMLIB_SCAN_DENTRY_OK:
1093 report_scan_progress(&info->scan, false);
1095 case WIMLIB_SCAN_DENTRY_EXCLUDED:
1096 imagex_printf(T("\nExcluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1098 case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
1099 imagex_printf(T("\nWARNING: Excluding unsupported file or directory\n"
1100 " \"%"TS"\" from capture\n"), info->scan.cur_path);
1102 case WIMLIB_SCAN_DENTRY_EXCLUDED_SYMLINK:
1103 imagex_printf(T("\nWARNING: Ignoring absolute symbolic link "
1104 "with out-of-tree target:\n"
1105 " \"%"TS"\" => \"%"TS"\"\n"
1106 " (Use --norpfix to capture "
1107 "absolute symbolic links as-is)\n"),
1108 info->scan.cur_path, info->scan.symlink_target);
1112 case WIMLIB_PROGRESS_MSG_SCAN_END:
1113 report_scan_progress(&info->scan, true);
1114 imagex_printf(T("\n"));
1116 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
1117 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1118 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1119 info->integrity.total_bytes);
1120 imagex_printf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1121 "of %"PRIu64" %"TS" (%u%%) done"),
1122 info->integrity.filename,
1123 info->integrity.completed_bytes >> unit_shift,
1125 info->integrity.total_bytes >> unit_shift,
1128 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1129 imagex_printf(T("\n"));
1131 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1132 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1133 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1134 info->integrity.total_bytes);
1135 imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1136 "of %"PRIu64" %"TS" (%u%%) done"),
1137 info->integrity.completed_bytes >> unit_shift,
1139 info->integrity.total_bytes >> unit_shift,
1142 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1143 imagex_printf(T("\n"));
1145 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1146 imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1147 "to %"TS" \"%"TS"\"\n"),
1148 info->extract.image,
1149 info->extract.image_name,
1150 info->extract.wimfile_name,
1151 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1152 T("NTFS volume") : T("directory")),
1153 info->extract.target);
1155 case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1156 if (info->extract.extract_root_wim_source_path[0]) {
1157 imagex_printf(T("Extracting \"%"TS"\" from image %d "
1158 "(\"%"TS"\") in \"%"TS"\" to \"%"TS"\"\n"),
1159 info->extract.extract_root_wim_source_path,
1160 info->extract.image,
1161 info->extract.image_name,
1162 info->extract.wimfile_name,
1163 info->extract.target);
1166 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1167 percent_done = TO_PERCENT(info->extract.completed_bytes,
1168 info->extract.total_bytes);
1169 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1170 imagex_printf(T("\rExtracting files: "
1171 "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1172 info->extract.completed_bytes >> unit_shift,
1174 info->extract.total_bytes >> unit_shift,
1177 if (info->extract.completed_bytes >= info->extract.total_bytes)
1178 imagex_printf(T("\n"));
1180 case WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN:
1181 if (info->extract.total_parts != 1) {
1182 imagex_printf(T("\nReading split pipable WIM part %u of %u\n"),
1183 info->extract.part_number,
1184 info->extract.total_parts);
1187 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1188 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1189 imagex_printf(T("Setting timestamps on all extracted files...\n"));
1191 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1192 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1193 imagex_printf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1194 info->extract.target);
1197 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1198 percent_done = TO_PERCENT(info->split.completed_bytes,
1199 info->split.total_bytes);
1200 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1201 imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
1202 "%"PRIu64" %"TS" (%u%%) written\n"),
1203 info->split.part_name,
1204 info->split.cur_part_number,
1205 info->split.total_parts,
1206 info->split.completed_bytes >> unit_shift,
1208 info->split.total_bytes >> unit_shift,
1212 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1213 if (info->split.completed_bytes == info->split.total_bytes) {
1214 imagex_printf(T("Finished writing split WIM part %u of %u\n"),
1215 info->split.cur_part_number,
1216 info->split.total_parts);
1219 case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1220 switch (info->update.command->op) {
1221 case WIMLIB_UPDATE_OP_DELETE:
1222 imagex_printf(T("Deleted WIM path "
1223 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1224 info->update.command->delete_.wim_path);
1226 case WIMLIB_UPDATE_OP_RENAME:
1227 imagex_printf(T("Renamed WIM path "
1228 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
1229 "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1230 info->update.command->rename.wim_source_path,
1231 info->update.command->rename.wim_target_path);
1233 case WIMLIB_UPDATE_OP_ADD:
1241 fflush(imagex_info_file);
1246 parse_num_threads(const tchar *optarg)
1249 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1250 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1251 imagex_error(T("Number of threads must be a non-negative integer!"));
1258 static uint32_t parse_chunk_size(const tchar *optarg)
1261 unsigned long chunk_size = tstrtoul(optarg, &tmp, 10);
1262 if (chunk_size >= UINT32_MAX || *tmp || tmp == optarg) {
1263 imagex_error(T("Chunk size must be a non-negative integer!"));
1272 * Parse an option passed to an update command.
1274 * @op: One of WIMLIB_UPDATE_OP_* that indicates the command being
1277 * @option: Text string for the option (beginning with --)
1279 * @cmd: `struct wimlib_update_command' that is being constructed for
1282 * Returns true if the option was recognized; false if not.
1285 update_command_add_option(int op, const tchar *option,
1286 struct wimlib_update_command *cmd)
1288 bool recognized = true;
1290 case WIMLIB_UPDATE_OP_ADD:
1291 if (!tstrcmp(option, T("--verbose")))
1292 cmd->add.add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
1293 else if (!tstrcmp(option, T("--unix-data")))
1294 cmd->add.add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
1295 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1296 cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
1297 else if (!tstrcmp(option, T("--strict-acls")))
1298 cmd->add.add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
1299 else if (!tstrcmp(option, T("--dereference")))
1300 cmd->add.add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
1304 case WIMLIB_UPDATE_OP_DELETE:
1305 if (!tstrcmp(option, T("--force")))
1306 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1307 else if (!tstrcmp(option, T("--recursive")))
1308 cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1319 /* How many nonoption arguments each `imagex update' command expects */
1320 static const unsigned update_command_num_nonoptions[] = {
1321 [WIMLIB_UPDATE_OP_ADD] = 2,
1322 [WIMLIB_UPDATE_OP_DELETE] = 1,
1323 [WIMLIB_UPDATE_OP_RENAME] = 2,
1327 update_command_add_nonoption(int op, const tchar *nonoption,
1328 struct wimlib_update_command *cmd,
1329 unsigned num_nonoptions)
1332 case WIMLIB_UPDATE_OP_ADD:
1333 if (num_nonoptions == 0)
1334 cmd->add.fs_source_path = (tchar*)nonoption;
1336 cmd->add.wim_target_path = (tchar*)nonoption;
1338 case WIMLIB_UPDATE_OP_DELETE:
1339 cmd->delete_.wim_path = (tchar*)nonoption;
1341 case WIMLIB_UPDATE_OP_RENAME:
1342 if (num_nonoptions == 0)
1343 cmd->rename.wim_source_path = (tchar*)nonoption;
1345 cmd->rename.wim_target_path = (tchar*)nonoption;
1351 * Parse a command passed on stdin to `imagex update'.
1353 * @line: Text of the command.
1354 * @len: Length of the line, including a null terminator
1357 * @command: A `struct wimlib_update_command' to fill in from the parsed
1360 * @line_number: Line number of the command, for diagnostics.
1362 * Returns true on success; returns false on parse error.
1365 parse_update_command(tchar *line, size_t len,
1366 struct wimlib_update_command *command,
1370 tchar *command_name;
1372 size_t num_nonoptions;
1374 /* Get the command name ("add", "delete", "rename") */
1375 ret = parse_string(&line, &len, &command_name);
1376 if (ret != PARSE_STRING_SUCCESS)
1379 if (!tstrcasecmp(command_name, T("add"))) {
1380 op = WIMLIB_UPDATE_OP_ADD;
1381 } else if (!tstrcasecmp(command_name, T("delete"))) {
1382 op = WIMLIB_UPDATE_OP_DELETE;
1383 } else if (!tstrcasecmp(command_name, T("rename"))) {
1384 op = WIMLIB_UPDATE_OP_RENAME;
1386 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1387 command_name, line_number);
1392 /* Parse additional options and non-options as needed */
1397 ret = parse_string(&line, &len, &next_string);
1398 if (ret == PARSE_STRING_NONE) /* End of line */
1400 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1402 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1404 if (!update_command_add_option(op, next_string, command))
1406 imagex_error(T("Unrecognized option \"%"TS"\" to "
1407 "update command \"%"TS"\" on line %zu"),
1408 next_string, command_name, line_number);
1414 if (num_nonoptions == update_command_num_nonoptions[op])
1416 imagex_error(T("Unexpected argument \"%"TS"\" in "
1417 "update command on line %zu\n"
1418 " (The \"%"TS"\" command only "
1419 "takes %zu nonoption arguments!)\n"),
1420 next_string, line_number,
1421 command_name, num_nonoptions);
1424 update_command_add_nonoption(op, next_string,
1425 command, num_nonoptions);
1430 if (num_nonoptions != update_command_num_nonoptions[op]) {
1431 imagex_error(T("Not enough arguments to update command "
1432 "\"%"TS"\" on line %zu"), command_name, line_number);
1438 static struct wimlib_update_command *
1439 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1440 size_t *num_cmds_ret)
1444 struct wimlib_update_command *cmds;
1447 nlines = text_file_count_lines(cmd_file_contents_p,
1452 /* Always allocate at least 1 slot, just in case the implementation of
1453 * calloc() returns NULL if 0 bytes are requested. */
1454 cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1456 imagex_error(T("out of memory"));
1459 p = *cmd_file_contents_p;
1461 for (i = 0; i < nlines; i++) {
1462 /* XXX: Could use rawmemchr() here instead, but it may not be
1463 * available on all platforms. */
1464 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1465 size_t len = endp - p + 1;
1467 if (!is_comment_line(p, len)) {
1468 if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1479 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1480 * one image from a WIM file to a NTFS volume. */
1482 imagex_apply(int argc, tchar **argv, int cmd)
1486 int image = WIMLIB_NO_IMAGE;
1488 struct wimlib_wim_info info;
1490 const tchar *wimfile;
1491 const tchar *target;
1492 const tchar *image_num_or_name = NULL;
1493 int extract_flags = 0;
1495 STRING_SET(refglobs);
1497 for_opt(c, apply_options) {
1499 case IMAGEX_CHECK_OPTION:
1500 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1502 case IMAGEX_HARDLINK_OPTION:
1503 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1505 case IMAGEX_SYMLINK_OPTION:
1506 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1508 case IMAGEX_VERBOSE_OPTION:
1509 /* No longer does anything. */
1511 case IMAGEX_REF_OPTION:
1512 ret = string_set_append(&refglobs, optarg);
1514 goto out_free_refglobs;
1516 case IMAGEX_UNIX_DATA_OPTION:
1517 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1519 case IMAGEX_NO_ACLS_OPTION:
1520 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1522 case IMAGEX_STRICT_ACLS_OPTION:
1523 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1525 case IMAGEX_NO_ATTRIBUTES_OPTION:
1526 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES;
1528 case IMAGEX_NORPFIX_OPTION:
1529 extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1531 case IMAGEX_RPFIX_OPTION:
1532 extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1534 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1535 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1536 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1538 case IMAGEX_RESUME_OPTION:
1539 extract_flags |= WIMLIB_EXTRACT_FLAG_RESUME;
1541 case IMAGEX_WIMBOOT_OPTION:
1542 extract_flags |= WIMLIB_EXTRACT_FLAG_WIMBOOT;
1550 if (argc != 2 && argc != 3)
1555 if (!tstrcmp(wimfile, T("-"))) {
1556 /* Attempt to apply pipable WIM from standard input. */
1558 image_num_or_name = NULL;
1561 image_num_or_name = argv[1];
1566 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1567 imagex_progress_func);
1569 goto out_free_refglobs;
1571 wimlib_get_wim_info(wim, &info);
1574 /* Image explicitly specified. */
1575 image_num_or_name = argv[1];
1576 image = wimlib_resolve_image(wim, image_num_or_name);
1577 ret = verify_image_exists(image, image_num_or_name, wimfile);
1579 goto out_wimlib_free;
1582 /* No image specified; default to image 1, but only if the WIM
1583 * contains exactly one image. */
1585 if (info.image_count != 1) {
1586 imagex_error(T("\"%"TS"\" contains %d images; "
1587 "Please select one (or all)."),
1588 wimfile, info.image_count);
1597 if (refglobs.num_strings) {
1599 imagex_error(T("Can't specify --ref when applying from stdin!"));
1601 goto out_wimlib_free;
1603 ret = wim_reference_globs(wim, &refglobs, open_flags);
1605 goto out_wimlib_free;
1610 /* Interpret a regular file or block device target as a NTFS
1614 if (tstat(target, &stbuf)) {
1615 if (errno != ENOENT) {
1616 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1619 goto out_wimlib_free;
1622 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1623 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1629 ret = wimlib_extract_image(wim, image, target, extract_flags,
1630 imagex_progress_func);
1632 set_fd_to_binary_mode(STDIN_FILENO);
1633 ret = wimlib_extract_image_from_pipe(STDIN_FILENO,
1635 target, extract_flags,
1636 imagex_progress_func);
1639 imagex_printf(T("Done applying WIM image.\n"));
1640 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
1642 do_resource_not_found_warning(wimfile, &info, &refglobs);
1644 imagex_error(T( "If you are applying an image "
1645 "from a split pipable WIM,\n"
1646 " make sure you have "
1647 "concatenated together all parts."));
1653 string_set_destroy(&refglobs);
1657 usage(CMD_APPLY, stderr);
1659 goto out_free_refglobs;
1662 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1663 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1664 * the desired image. 'wimlib-imagex append': add a new image to an existing
1667 imagex_capture_or_append(int argc, tchar **argv, int cmd)
1670 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1671 int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE |
1672 WIMLIB_ADD_IMAGE_FLAG_WINCONFIG |
1673 WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1674 int write_flags = 0;
1675 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
1676 uint32_t chunk_size = UINT32_MAX;
1677 uint32_t pack_chunk_size = UINT32_MAX;
1678 int pack_ctype = WIMLIB_COMPRESSION_TYPE_INVALID;
1679 const tchar *wimfile;
1683 const tchar *flags_element = NULL;
1686 STRING_SET(base_wimfiles);
1687 WIMStruct **base_wims;
1689 WIMStruct *template_wim;
1690 const tchar *template_wimfile = NULL;
1691 const tchar *template_image_name_or_num = NULL;
1692 int template_image = WIMLIB_NO_IMAGE;
1695 unsigned num_threads = 0;
1700 tchar *config_file = NULL;
1702 bool source_list = false;
1703 size_t source_list_nchars = 0;
1704 tchar *source_list_contents;
1705 bool capture_sources_malloced;
1706 struct wimlib_capture_source *capture_sources;
1708 bool name_defaulted;
1709 bool compress_slow = false;
1711 for_opt(c, capture_or_append_options) {
1713 case IMAGEX_BOOT_OPTION:
1714 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1716 case IMAGEX_CHECK_OPTION:
1717 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1718 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1720 case IMAGEX_NOCHECK_OPTION:
1721 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
1723 case IMAGEX_CONFIG_OPTION:
1724 config_file = optarg;
1725 add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1727 case IMAGEX_COMPRESS_OPTION:
1728 compression_type = get_compression_type(optarg);
1729 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1732 case IMAGEX_COMPRESS_SLOW_OPTION:
1733 compress_slow = true;
1735 case IMAGEX_CHUNK_SIZE_OPTION:
1736 chunk_size = parse_chunk_size(optarg);
1737 if (chunk_size == UINT32_MAX)
1740 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
1741 pack_chunk_size = parse_chunk_size(optarg);
1742 if (pack_chunk_size == UINT32_MAX)
1745 case IMAGEX_PACK_COMPRESS_OPTION:
1746 pack_ctype = get_compression_type(optarg);
1747 if (pack_ctype == WIMLIB_COMPRESSION_TYPE_INVALID)
1750 case IMAGEX_PACK_STREAMS_OPTION:
1751 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
1753 case IMAGEX_FLAGS_OPTION:
1754 flags_element = optarg;
1756 case IMAGEX_DEREFERENCE_OPTION:
1757 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1759 case IMAGEX_VERBOSE_OPTION:
1760 /* No longer does anything. */
1762 case IMAGEX_THREADS_OPTION:
1763 num_threads = parse_num_threads(optarg);
1764 if (num_threads == UINT_MAX)
1767 case IMAGEX_REBUILD_OPTION:
1768 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1770 case IMAGEX_UNIX_DATA_OPTION:
1771 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1773 case IMAGEX_SOURCE_LIST_OPTION:
1776 case IMAGEX_NO_ACLS_OPTION:
1777 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1779 case IMAGEX_STRICT_ACLS_OPTION:
1780 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1782 case IMAGEX_RPFIX_OPTION:
1783 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1785 case IMAGEX_NORPFIX_OPTION:
1786 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1788 case IMAGEX_PIPABLE_OPTION:
1789 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1791 case IMAGEX_NOT_PIPABLE_OPTION:
1792 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
1794 case IMAGEX_UPDATE_OF_OPTION:
1795 if (template_image_name_or_num) {
1796 imagex_error(T("'--update-of' can only be "
1797 "specified one time!"));
1801 colon = tstrrchr(optarg, T(':'));
1804 template_wimfile = optarg;
1806 template_image_name_or_num = colon + 1;
1808 template_wimfile = NULL;
1809 template_image_name_or_num = optarg;
1813 case IMAGEX_DELTA_FROM_OPTION:
1814 if (cmd != CMD_CAPTURE) {
1815 imagex_error(T("'--delta-from' is only "
1816 "valid for capture!"));
1819 ret = string_set_append(&base_wimfiles, optarg);
1821 goto out_free_base_wimfiles;
1822 write_flags |= WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS;
1824 case IMAGEX_WIMBOOT_OPTION:
1825 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_WIMBOOT;
1834 if (argc < 2 || argc > 4)
1840 /* Set default compression type and parameters. */
1843 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
1844 if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_WIMBOOT) {
1845 compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
1847 compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1848 if (!compress_slow && pack_ctype != WIMLIB_COMPRESSION_TYPE_LZX) {
1849 struct wimlib_lzx_compressor_params params = {
1850 .hdr.size = sizeof(params),
1851 .algorithm = WIMLIB_LZX_ALGORITHM_FAST,
1854 wimlib_set_default_compressor_params(WIMLIB_COMPRESSION_TYPE_LZX,
1861 set_compress_slow();
1863 if (!tstrcmp(wimfile, T("-"))) {
1864 /* Writing captured WIM to standard output. */
1866 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
1867 imagex_error("Can't write a non-pipable WIM to "
1868 "standard output! Specify --pipable\n"
1869 " if you want to create a pipable WIM "
1870 "(but read the docs first).");
1874 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1876 if (cmd == CMD_APPEND) {
1877 imagex_error(T("Using standard output for append does "
1878 "not make sense."));
1881 wim_fd = STDOUT_FILENO;
1883 imagex_info_file = stderr;
1884 set_fd_to_binary_mode(wim_fd);
1887 /* If template image was specified using --update-of=IMAGE rather
1888 * than --update-of=WIMFILE:IMAGE, set the default WIMFILE. */
1889 if (template_image_name_or_num && !template_wimfile) {
1890 if (base_wimfiles.num_strings == 1) {
1891 /* Capturing delta WIM based on single WIM: default to
1893 template_wimfile = base_wimfiles.strings[0];
1894 } else if (cmd == CMD_APPEND) {
1895 /* Appending to WIM: default to WIM being appended to.
1897 template_wimfile = wimfile;
1899 /* Capturing a normal (non-delta) WIM, so the WIM file
1900 * *must* be explicitly specified. */
1901 if (base_wimfiles.num_strings > 1) {
1902 imagex_error(T("For capture of delta WIM "
1903 "based on multiple existing "
1905 " '--update-of' must "
1906 "specify WIMFILE:IMAGE!"));
1908 imagex_error(T("For capture of non-delta WIM, "
1909 "'--update-of' must specify "
1918 name_defaulted = false;
1920 /* Set default name to SOURCE argument, omitting any directory
1921 * prefixes and trailing slashes. This requires making a copy
1922 * of @source. Leave some free characters at the end in case we
1923 * append a number to keep the name unique. */
1924 size_t source_name_len;
1926 source_name_len = tstrlen(source);
1927 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1928 name = tbasename(tstrcpy(source_copy, source));
1929 name_defaulted = true;
1931 /* Image description defaults to NULL if not given. */
1938 /* Set up capture sources in source list mode */
1939 if (source[0] == T('-') && source[1] == T('\0')) {
1940 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1942 source_list_contents = file_get_text_contents(source,
1943 &source_list_nchars);
1945 if (!source_list_contents)
1948 capture_sources = parse_source_list(&source_list_contents,
1951 if (!capture_sources) {
1953 goto out_free_source_list_contents;
1955 capture_sources_malloced = true;
1957 /* Set up capture source in non-source-list mode. */
1958 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1959 capture_sources[0].fs_source_path = source;
1960 capture_sources[0].wim_target_path = NULL;
1961 capture_sources[0].reserved = 0;
1963 capture_sources_malloced = false;
1964 source_list_contents = NULL;
1967 /* Open the existing WIM, or create a new one. */
1968 if (cmd == CMD_APPEND)
1969 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1970 imagex_progress_func);
1972 ret = wimlib_create_new_wim(compression_type, &wim);
1974 goto out_free_capture_sources;
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(base_wimfiles.strings[i],
2048 open_flags, &base_wims[i],
2049 imagex_progress_func);
2051 goto out_free_base_wims;
2055 ret = wimlib_reference_resources(wim, base_wims,
2056 base_wimfiles.num_strings, 0);
2058 goto out_free_base_wims;
2060 if (base_wimfiles.num_strings == 1) {
2061 imagex_printf(T("Capturing delta WIM based on \"%"TS"\"\n"),
2062 base_wimfiles.strings[0]);
2064 imagex_printf(T("Capturing delta WIM based on %u WIMs\n"),
2065 base_wimfiles.num_strings);
2072 /* If capturing or appending as an update of an existing (template) image,
2073 * open the WIM if needed and parse the image index. */
2074 if (template_image_name_or_num) {
2077 if (base_wimfiles.num_strings == 1 &&
2078 template_wimfile == base_wimfiles.strings[0]) {
2079 template_wim = base_wims[0];
2080 } else if (template_wimfile == wimfile) {
2083 ret = wimlib_open_wim(template_wimfile, open_flags,
2084 &template_wim, imagex_progress_func);
2086 goto out_free_base_wims;
2089 template_image = wimlib_resolve_image(template_wim,
2090 template_image_name_or_num);
2092 if (template_image_name_or_num[0] == T('-')) {
2095 struct wimlib_wim_info info;
2097 wimlib_get_wim_info(template_wim, &info);
2098 n = tstrtoul(template_image_name_or_num + 1, &tmp, 10);
2099 if (n >= 1 && n <= info.image_count &&
2101 tmp != template_image_name_or_num + 1)
2103 template_image = info.image_count - (n - 1);
2106 ret = verify_image_exists_and_is_single(template_image,
2107 template_image_name_or_num,
2110 goto out_free_template_wim;
2112 template_wim = NULL;
2115 ret = wimlib_add_image_multisource(wim,
2121 imagex_progress_func);
2123 goto out_free_template_wim;
2125 if (desc || flags_element || template_image_name_or_num) {
2126 /* User provided <DESCRIPTION> or <FLAGS> element, or an image
2127 * on which the added one is to be based has been specified with
2128 * --update-of. Get the index of the image we just
2129 * added, then use it to call the appropriate functions. */
2130 struct wimlib_wim_info info;
2132 wimlib_get_wim_info(wim, &info);
2135 ret = wimlib_set_image_descripton(wim,
2139 goto out_free_template_wim;
2142 if (flags_element) {
2143 ret = wimlib_set_image_flags(wim, info.image_count,
2146 goto out_free_template_wim;
2149 /* Reference template image if the user provided one. */
2150 if (template_image_name_or_num) {
2151 imagex_printf(T("Using image %d "
2152 "from \"%"TS"\" as template\n"),
2153 template_image, template_wimfile);
2154 ret = wimlib_reference_template_image(wim,
2160 goto out_free_template_wim;
2164 /* Write the new WIM or overwrite the existing WIM with the new image
2166 if (cmd == CMD_APPEND) {
2167 ret = wimlib_overwrite(wim, write_flags, num_threads,
2168 imagex_progress_func);
2169 } else if (wimfile) {
2170 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
2171 write_flags, num_threads,
2172 imagex_progress_func);
2174 ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
2175 write_flags, num_threads,
2176 imagex_progress_func);
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(wimfile, open_flags, &wim,
2245 imagex_progress_func);
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, imagex_progress_func);
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 for (uint32_t i = 0; i <= dentry->num_named_streams; i++) {
2458 if (dentry->streams[i].stream_name) {
2459 tprintf(T("\tData stream \"%"TS"\":\n"),
2460 dentry->streams[i].stream_name);
2462 tprintf(T("\tUnnamed data stream:\n"));
2464 print_resource(&dentry->streams[i].resource, NULL);
2469 print_dentry(const struct wimlib_dir_entry *dentry, void *_options)
2471 const struct print_dentry_options *options = _options;
2472 if (!options->detailed)
2473 print_dentry_full_path(dentry);
2475 print_dentry_detailed(dentry);
2479 /* Print the files contained in an image(s) in a WIM file. */
2481 imagex_dir(int argc, tchar **argv, int cmd)
2483 const tchar *wimfile;
2484 WIMStruct *wim = NULL;
2487 const tchar *path = T("");
2489 struct print_dentry_options options = {
2492 int iterate_flags = WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE;
2494 for_opt(c, dir_options) {
2496 case IMAGEX_PATH_OPTION:
2499 case IMAGEX_DETAILED_OPTION:
2500 options.detailed = true;
2502 case IMAGEX_ONE_FILE_ONLY_OPTION:
2503 iterate_flags &= ~WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE;
2513 imagex_error(T("Must specify a WIM file"));
2517 imagex_error(T("Too many arguments"));
2522 ret = wimlib_open_wim(wimfile, 0, &wim, imagex_progress_func);
2527 image = wimlib_resolve_image(wim, argv[1]);
2528 ret = verify_image_exists(image, argv[1], wimfile);
2530 goto out_wimlib_free;
2532 /* No image specified; default to image 1, but only if the WIM
2533 * contains exactly one image. */
2535 struct wimlib_wim_info info;
2537 wimlib_get_wim_info(wim, &info);
2538 if (info.image_count != 1) {
2539 imagex_error(T("\"%"TS"\" contains %d images; Please "
2540 "select one (or all)."),
2541 wimfile, info.image_count);
2548 ret = wimlib_iterate_dir_tree(wim, image, path, iterate_flags,
2549 print_dentry, &options);
2556 usage(CMD_DIR, stderr);
2561 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2564 imagex_export(int argc, tchar **argv, int cmd)
2568 int export_flags = 0;
2569 int write_flags = 0;
2570 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2571 const tchar *src_wimfile;
2572 const tchar *src_image_num_or_name;
2573 const tchar *dest_wimfile;
2575 const tchar *dest_name;
2576 const tchar *dest_desc;
2578 struct wimlib_wim_info src_info;
2579 WIMStruct *dest_wim;
2584 STRING_SET(refglobs);
2585 unsigned num_threads = 0;
2586 uint32_t chunk_size = UINT32_MAX;
2587 uint32_t pack_chunk_size = UINT32_MAX;
2588 int pack_ctype = WIMLIB_COMPRESSION_TYPE_INVALID;
2590 for_opt(c, export_options) {
2592 case IMAGEX_BOOT_OPTION:
2593 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2595 case IMAGEX_CHECK_OPTION:
2596 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2597 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2599 case IMAGEX_NOCHECK_OPTION:
2600 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2602 case IMAGEX_COMPRESS_OPTION:
2603 compression_type = get_compression_type(optarg);
2604 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2607 case IMAGEX_COMPRESS_SLOW_OPTION:
2608 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
2609 set_compress_slow();
2611 case IMAGEX_PACK_STREAMS_OPTION:
2612 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
2614 case IMAGEX_CHUNK_SIZE_OPTION:
2615 chunk_size = parse_chunk_size(optarg);
2616 if (chunk_size == UINT32_MAX)
2619 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
2620 pack_chunk_size = parse_chunk_size(optarg);
2621 if (pack_chunk_size == UINT32_MAX)
2624 case IMAGEX_PACK_COMPRESS_OPTION:
2625 pack_ctype = get_compression_type(optarg);
2626 if (pack_ctype == WIMLIB_COMPRESSION_TYPE_INVALID)
2629 case IMAGEX_REF_OPTION:
2630 ret = string_set_append(&refglobs, optarg);
2632 goto out_free_refglobs;
2634 case IMAGEX_THREADS_OPTION:
2635 num_threads = parse_num_threads(optarg);
2636 if (num_threads == UINT_MAX)
2639 case IMAGEX_REBUILD_OPTION:
2640 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2642 case IMAGEX_PIPABLE_OPTION:
2643 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2645 case IMAGEX_NOT_PIPABLE_OPTION:
2646 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
2654 if (argc < 3 || argc > 5)
2656 src_wimfile = argv[0];
2657 src_image_num_or_name = argv[1];
2658 dest_wimfile = argv[2];
2659 dest_name = (argc >= 4) ? argv[3] : NULL;
2660 dest_desc = (argc >= 5) ? argv[4] : NULL;
2661 ret = wimlib_open_wim(src_wimfile, open_flags, &src_wim,
2662 imagex_progress_func);
2664 goto out_free_refglobs;
2666 wimlib_get_wim_info(src_wim, &src_info);
2668 /* Determine if the destination is an existing file or not. If so, we
2669 * try to append the exported image(s) to it; otherwise, we create a new
2670 * WIM containing the exported image(s). Furthermore, determine if we
2671 * need to write a pipable WIM directly to standard output. */
2673 if (tstrcmp(dest_wimfile, T("-")) == 0) {
2675 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2676 imagex_error("Can't write a non-pipable WIM to "
2677 "standard output! Specify --pipable\n"
2678 " if you want to create a pipable WIM "
2679 "(but read the docs first).");
2681 goto out_free_src_wim;
2684 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2686 dest_wimfile = NULL;
2687 dest_wim_fd = STDOUT_FILENO;
2688 imagex_info_file = stderr;
2689 set_fd_to_binary_mode(dest_wim_fd);
2692 if (dest_wimfile != NULL && tstat(dest_wimfile, &stbuf) == 0) {
2694 /* Destination file exists. */
2696 if (!S_ISREG(stbuf.st_mode)) {
2697 imagex_error(T("\"%"TS"\" is not a regular file"),
2700 goto out_free_src_wim;
2702 ret = wimlib_open_wim(dest_wimfile,
2703 open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2704 &dest_wim, imagex_progress_func);
2706 goto out_free_src_wim;
2708 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2709 /* The user specified a compression type, but we're
2710 * exporting to an existing WIM. Make sure the
2711 * specified compression type is the same as the
2712 * compression type of the existing destination WIM. */
2713 struct wimlib_wim_info dest_info;
2715 wimlib_get_wim_info(dest_wim, &dest_info);
2716 if (compression_type != dest_info.compression_type) {
2717 imagex_error(T("Cannot specify a compression type that is "
2718 "not the same as that used in the "
2719 "destination WIM"));
2721 goto out_free_dest_wim;
2727 if (errno != ENOENT) {
2728 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2731 goto out_free_src_wim;
2734 /* dest_wimfile is not an existing file, so create a new WIM. */
2736 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2737 /* The user did not specify a compression type; default
2738 * to that of the source WIM. */
2740 compression_type = src_info.compression_type;
2742 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2744 goto out_free_src_wim;
2746 /* Use same chunk size if compression type is the same. */
2747 if (compression_type == src_info.compression_type &&
2748 chunk_size == UINT32_MAX)
2749 wimlib_set_output_chunk_size(dest_wim, src_info.chunk_size);
2752 if (chunk_size != UINT32_MAX) {
2753 /* Set destination chunk size. */
2754 ret = wimlib_set_output_chunk_size(dest_wim, chunk_size);
2756 goto out_free_dest_wim;
2758 if (pack_ctype != WIMLIB_COMPRESSION_TYPE_INVALID) {
2759 ret = wimlib_set_output_pack_compression_type(dest_wim, pack_ctype);
2761 goto out_free_dest_wim;
2763 if (pack_chunk_size != UINT32_MAX) {
2764 ret = wimlib_set_output_pack_chunk_size(dest_wim, pack_chunk_size);
2766 goto out_free_dest_wim;
2769 image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2770 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2772 goto out_free_dest_wim;
2774 if (refglobs.num_strings) {
2775 ret = wim_reference_globs(src_wim, &refglobs, open_flags);
2777 goto out_free_dest_wim;
2780 if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
2781 image == WIMLIB_ALL_IMAGES && src_info.boot_index == 0)
2783 imagex_error(T("--boot specified for all-images export, but source WIM "
2784 "has no bootable image."));
2786 goto out_free_dest_wim;
2789 ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2790 dest_desc, export_flags, imagex_progress_func);
2792 if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2793 do_resource_not_found_warning(src_wimfile,
2794 &src_info, &refglobs);
2796 goto out_free_dest_wim;
2800 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2801 imagex_progress_func);
2802 else if (dest_wimfile)
2803 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2804 write_flags, num_threads,
2805 imagex_progress_func);
2807 ret = wimlib_write_to_fd(dest_wim, dest_wim_fd,
2808 WIMLIB_ALL_IMAGES, write_flags,
2809 num_threads, imagex_progress_func);
2811 wimlib_free(dest_wim);
2813 wimlib_free(src_wim);
2815 string_set_destroy(&refglobs);
2819 usage(CMD_EXPORT, stderr);
2822 goto out_free_refglobs;
2825 /* Extract files or directories from a WIM image */
2827 imagex_extract(int argc, tchar **argv, int cmd)
2834 const tchar *wimfile;
2835 const tchar *image_num_or_name;
2836 tchar *dest_dir = T(".");
2837 int extract_flags = WIMLIB_EXTRACT_FLAG_NORPFIX |
2838 WIMLIB_EXTRACT_FLAG_GLOB_PATHS |
2839 WIMLIB_EXTRACT_FLAG_STRICT_GLOB;
2840 int notlist_extract_flags = WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE;
2842 STRING_SET(refglobs);
2844 tchar *root_path = T("");
2846 for_opt(c, extract_options) {
2848 case IMAGEX_CHECK_OPTION:
2849 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2851 case IMAGEX_VERBOSE_OPTION:
2852 /* No longer does anything. */
2854 case IMAGEX_REF_OPTION:
2855 ret = string_set_append(&refglobs, optarg);
2857 goto out_free_refglobs;
2859 case IMAGEX_UNIX_DATA_OPTION:
2860 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2862 case IMAGEX_NO_ACLS_OPTION:
2863 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2865 case IMAGEX_STRICT_ACLS_OPTION:
2866 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2868 case IMAGEX_NO_ATTRIBUTES_OPTION:
2869 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES;
2871 case IMAGEX_DEST_DIR_OPTION:
2874 case IMAGEX_TO_STDOUT_OPTION:
2875 extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2876 imagex_info_file = stderr;
2877 imagex_be_quiet = true;
2879 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2880 extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2881 extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2883 case IMAGEX_NO_WILDCARDS_OPTION:
2884 extract_flags &= ~WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2886 case IMAGEX_NULLGLOB_OPTION:
2887 extract_flags &= ~WIMLIB_EXTRACT_FLAG_STRICT_GLOB;
2889 case IMAGEX_PRESERVE_DIR_STRUCTURE_OPTION:
2890 notlist_extract_flags &= ~WIMLIB_EXTRACT_FLAG_NO_PRESERVE_DIR_STRUCTURE;
2892 case IMAGEX_WIMBOOT_OPTION:
2893 extract_flags |= WIMLIB_EXTRACT_FLAG_WIMBOOT;
2905 if (!(extract_flags & (WIMLIB_EXTRACT_FLAG_GLOB_PATHS |
2906 WIMLIB_EXTRACT_FLAG_STRICT_GLOB)))
2908 imagex_error(T("Can't combine --no-wildcards and --nullglob!"));
2913 image_num_or_name = argv[1];
2918 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2920 goto out_free_refglobs;
2922 image = wimlib_resolve_image(wim, image_num_or_name);
2923 ret = verify_image_exists_and_is_single(image,
2927 goto out_wimlib_free;
2929 if (refglobs.num_strings) {
2930 ret = wim_reference_globs(wim, &refglobs, open_flags);
2932 goto out_wimlib_free;
2938 extract_flags &= ~WIMLIB_EXTRACT_FLAG_GLOB_PATHS;
2941 while (argc != 0 && ret == 0) {
2945 num_paths < argc && argv[num_paths][0] != T('@');
2950 ret = wimlib_extract_paths(wim, image, dest_dir,
2951 (const tchar **)argv,
2953 extract_flags | notlist_extract_flags,
2954 imagex_progress_func);
2958 ret = wimlib_extract_pathlist(wim, image, dest_dir,
2961 imagex_progress_func);
2968 if (!imagex_be_quiet)
2969 imagex_printf(T("Done extracting files.\n"));
2970 } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2971 tfprintf(stderr, T("Note: You can use `%"TS"' to see what "
2972 "files and directories\n"
2973 " are in the WIM image.\n"),
2974 get_cmd_string(CMD_DIR, false));
2975 } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2976 struct wimlib_wim_info info;
2978 wimlib_get_wim_info(wim, &info);
2979 do_resource_not_found_warning(wimfile, &info, &refglobs);
2984 string_set_destroy(&refglobs);
2988 usage(CMD_EXTRACT, stderr);
2991 goto out_free_refglobs;
2994 /* Prints information about a WIM file; also can mark an image as bootable,
2995 * change the name of an image, or change the description of an image. */
2997 imagex_info(int argc, tchar **argv, int cmd)
3002 bool nocheck = false;
3003 bool header = false;
3004 bool lookup_table = false;
3006 bool short_header = true;
3007 const tchar *xml_out_file = NULL;
3008 const tchar *wimfile;
3009 const tchar *image_num_or_name;
3010 const tchar *new_name;
3011 const tchar *new_desc;
3016 struct wimlib_wim_info info;
3018 for_opt(c, info_options) {
3020 case IMAGEX_BOOT_OPTION:
3023 case IMAGEX_CHECK_OPTION:
3026 case IMAGEX_NOCHECK_OPTION:
3029 case IMAGEX_HEADER_OPTION:
3031 short_header = false;
3033 case IMAGEX_LOOKUP_TABLE_OPTION:
3034 lookup_table = true;
3035 short_header = false;
3037 case IMAGEX_XML_OPTION:
3039 short_header = false;
3041 case IMAGEX_EXTRACT_XML_OPTION:
3042 xml_out_file = optarg;
3043 short_header = false;
3045 case IMAGEX_METADATA_OPTION:
3046 imagex_error(T("The --metadata option has been removed. "
3047 "Use 'wimdir --detail' instead."));
3056 if (argc < 1 || argc > 4)
3060 image_num_or_name = (argc >= 2) ? argv[1] : T("all");
3061 new_name = (argc >= 3) ? argv[2] : NULL;
3062 new_desc = (argc >= 4) ? argv[3] : NULL;
3064 if (check && nocheck) {
3065 imagex_error(T("Can't specify both --check and --nocheck"));
3070 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3072 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3076 wimlib_get_wim_info(wim, &info);
3078 image = wimlib_resolve_image(wim, image_num_or_name);
3079 ret = WIMLIB_ERR_INVALID_IMAGE;
3080 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
3081 verify_image_exists(image, image_num_or_name, wimfile);
3083 imagex_error(T("If you would like to set the boot "
3084 "index to 0, specify image \"0\" with "
3085 "the --boot flag."));
3087 goto out_wimlib_free;
3090 if (boot && info.image_count == 0) {
3091 imagex_error(T("--boot is meaningless on a WIM with no images"));
3092 goto out_wimlib_free;
3095 if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
3097 imagex_error(T("Cannot specify the --boot flag "
3098 "without specifying a specific "
3099 "image in a multi-image WIM"));
3100 goto out_wimlib_free;
3103 imagex_error(T("Cannot specify the NEW_NAME "
3104 "without specifying a specific "
3105 "image in a multi-image WIM"));
3106 goto out_wimlib_free;
3110 /* Operations that print information are separated from operations that
3111 * recreate the WIM file. */
3112 if (!new_name && !boot) {
3114 /* Read-only operations */
3116 if (image == WIMLIB_NO_IMAGE) {
3117 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
3118 image_num_or_name, wimfile);
3119 goto out_wimlib_free;
3122 if (image == WIMLIB_ALL_IMAGES && short_header)
3123 print_wim_information(wimfile, &info);
3126 wimlib_print_header(wim);
3129 if (info.total_parts != 1) {
3130 tfprintf(stderr, T("Warning: Only showing the lookup table "
3131 "for part %d of a %d-part WIM.\n"),
3132 info.part_number, info.total_parts);
3134 print_lookup_table(wim);
3138 ret = wimlib_extract_xml_data(wim, stdout);
3140 goto out_wimlib_free;
3146 fp = tfopen(xml_out_file, T("wb"));
3148 imagex_error_with_errno(T("Failed to open the "
3149 "file \"%"TS"\" for "
3153 goto out_wimlib_free;
3155 ret = wimlib_extract_xml_data(wim, fp);
3157 imagex_error(T("Failed to close the file "
3163 goto out_wimlib_free;
3167 wimlib_print_available_images(wim, image);
3172 /* Modification operations */
3174 if (image == WIMLIB_ALL_IMAGES)
3177 if (image == WIMLIB_NO_IMAGE && new_name) {
3178 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
3179 "when using image 0"), new_name);
3181 goto out_wimlib_free;
3185 if (image == info.boot_index) {
3186 imagex_printf(T("Image %d is already marked as "
3187 "bootable.\n"), image);
3190 imagex_printf(T("Marking image %d as bootable.\n"),
3192 info.boot_index = image;
3193 ret = wimlib_set_wim_info(wim, &info,
3194 WIMLIB_CHANGE_BOOT_INDEX);
3196 goto out_wimlib_free;
3200 if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
3202 imagex_printf(T("Image %d is already named \"%"TS"\".\n"),
3206 imagex_printf(T("Changing the name of image %d to "
3207 "\"%"TS"\".\n"), image, new_name);
3208 ret = wimlib_set_image_name(wim, image, new_name);
3210 goto out_wimlib_free;
3214 const tchar *old_desc;
3215 old_desc = wimlib_get_image_description(wim, image);
3216 if (old_desc && !tstrcmp(old_desc, new_desc)) {
3217 imagex_printf(T("The description of image %d is already "
3218 "\"%"TS"\".\n"), image, new_desc);
3221 imagex_printf(T("Changing the description of image %d "
3222 "to \"%"TS"\".\n"), image, new_desc);
3223 ret = wimlib_set_image_descripton(wim, image,
3226 goto out_wimlib_free;
3230 /* Only call wimlib_overwrite() if something actually needs to
3232 if (boot || new_name || new_desc ||
3233 (check && !info.has_integrity_table) ||
3234 (nocheck && info.has_integrity_table))
3236 int write_flags = 0;
3239 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3241 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3242 ret = wimlib_overwrite(wim, write_flags, 1,
3243 imagex_progress_func);
3245 imagex_printf(T("The file \"%"TS"\" was not modified "
3246 "because nothing needed to be done.\n"),
3257 usage(CMD_INFO, stderr);
3263 /* Join split WIMs into one part WIM */
3265 imagex_join(int argc, tchar **argv, int cmd)
3268 int swm_open_flags = 0;
3269 int wim_write_flags = 0;
3270 const tchar *output_path;
3273 for_opt(c, join_options) {
3275 case IMAGEX_CHECK_OPTION:
3276 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3277 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3287 imagex_error(T("Must specify one or more split WIM (.swm) "
3291 output_path = argv[0];
3292 ret = wimlib_join((const tchar * const *)++argv,
3297 imagex_progress_func);
3302 usage(CMD_JOIN, stderr);
3307 #if WIM_MOUNTING_SUPPORTED
3309 /* Mounts a WIM image. */
3311 imagex_mount_rw_or_ro(int argc, tchar **argv, int cmd)
3314 int mount_flags = 0;
3316 const tchar *staging_dir = NULL;
3317 const tchar *wimfile;
3320 struct wimlib_wim_info info;
3324 STRING_SET(refglobs);
3326 if (cmd == CMD_MOUNTRW) {
3327 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
3328 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3331 for_opt(c, mount_options) {
3333 case IMAGEX_ALLOW_OTHER_OPTION:
3334 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
3336 case IMAGEX_CHECK_OPTION:
3337 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3339 case IMAGEX_DEBUG_OPTION:
3340 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
3342 case IMAGEX_STREAMS_INTERFACE_OPTION:
3343 if (!tstrcasecmp(optarg, T("none")))
3344 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
3345 else if (!tstrcasecmp(optarg, T("xattr")))
3346 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
3347 else if (!tstrcasecmp(optarg, T("windows")))
3348 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
3350 imagex_error(T("Unknown stream interface \"%"TS"\""),
3355 case IMAGEX_REF_OPTION:
3356 ret = string_set_append(&refglobs, optarg);
3358 goto out_free_refglobs;
3360 case IMAGEX_STAGING_DIR_OPTION:
3361 staging_dir = optarg;
3363 case IMAGEX_UNIX_DATA_OPTION:
3364 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
3372 if (argc != 2 && argc != 3)
3377 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3379 goto out_free_refglobs;
3381 wimlib_get_wim_info(wim, &info);
3384 /* Image explicitly specified. */
3385 image = wimlib_resolve_image(wim, argv[1]);
3387 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
3391 /* No image specified; default to image 1, but only if the WIM
3392 * contains exactly one image. */
3394 if (info.image_count != 1) {
3395 imagex_error(T("\"%"TS"\" contains %d images; Please "
3396 "select one."), wimfile, info.image_count);
3404 if (refglobs.num_strings) {
3405 ret = wim_reference_globs(wim, &refglobs, open_flags);
3410 ret = wimlib_mount_image(wim, image, dir, mount_flags, staging_dir);
3412 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
3414 image, wimfile, dir);
3419 string_set_destroy(&refglobs);
3425 goto out_free_refglobs;
3427 #endif /* WIM_MOUNTING_SUPPORTED */
3429 /* Rebuild a WIM file */
3431 imagex_optimize(int argc, tchar **argv, int cmd)
3434 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3435 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
3436 int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
3437 uint32_t chunk_size = UINT32_MAX;
3438 uint32_t pack_chunk_size = UINT32_MAX;
3439 int pack_ctype = WIMLIB_COMPRESSION_TYPE_INVALID;
3442 const tchar *wimfile;
3445 unsigned num_threads = 0;
3447 for_opt(c, optimize_options) {
3449 case IMAGEX_CHECK_OPTION:
3450 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3451 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3453 case IMAGEX_NOCHECK_OPTION:
3454 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3456 case IMAGEX_COMPRESS_OPTION:
3457 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3458 compression_type = get_compression_type(optarg);
3459 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
3462 case IMAGEX_RECOMPRESS_OPTION:
3463 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3465 case IMAGEX_COMPRESS_SLOW_OPTION:
3466 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3467 set_compress_slow();
3469 case IMAGEX_CHUNK_SIZE_OPTION:
3470 chunk_size = parse_chunk_size(optarg);
3471 if (chunk_size == UINT32_MAX)
3474 case IMAGEX_PACK_CHUNK_SIZE_OPTION:
3475 pack_chunk_size = parse_chunk_size(optarg);
3476 if (pack_chunk_size == UINT32_MAX)
3479 case IMAGEX_PACK_COMPRESS_OPTION:
3480 pack_ctype = get_compression_type(optarg);
3481 if (pack_ctype == WIMLIB_COMPRESSION_TYPE_INVALID)
3484 case IMAGEX_PACK_STREAMS_OPTION:
3485 write_flags |= WIMLIB_WRITE_FLAG_PACK_STREAMS;
3486 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3488 case IMAGEX_THREADS_OPTION:
3489 num_threads = parse_num_threads(optarg);
3490 if (num_threads == UINT_MAX)
3493 case IMAGEX_PIPABLE_OPTION:
3494 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
3496 case IMAGEX_NOT_PIPABLE_OPTION:
3497 write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
3511 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3515 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
3516 /* Change compression type. */
3517 ret = wimlib_set_output_compression_type(wim, compression_type);
3519 goto out_wimlib_free;
3522 if (chunk_size != UINT32_MAX) {
3523 /* Change chunk size. */
3524 ret = wimlib_set_output_chunk_size(wim, chunk_size);
3526 goto out_wimlib_free;
3528 if (pack_ctype != WIMLIB_COMPRESSION_TYPE_INVALID) {
3529 ret = wimlib_set_output_pack_compression_type(wim, pack_ctype);
3531 goto out_wimlib_free;
3533 if (pack_chunk_size != UINT32_MAX) {
3534 ret = wimlib_set_output_pack_chunk_size(wim, pack_chunk_size);
3536 goto out_wimlib_free;
3539 old_size = file_get_size(wimfile);
3540 tprintf(T("\"%"TS"\" original size: "), wimfile);
3542 tputs(T("Unknown"));
3544 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3546 ret = wimlib_overwrite(wim, write_flags, num_threads,
3547 imagex_progress_func);
3549 imagex_error(T("Optimization of \"%"TS"\" failed."), wimfile);
3550 goto out_wimlib_free;
3553 new_size = file_get_size(wimfile);
3554 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3556 tputs(T("Unknown"));
3558 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3560 tfputs(T("Space saved: "), stdout);
3561 if (new_size != -1 && old_size != -1) {
3562 tprintf(T("%lld KiB\n"),
3563 ((long long)old_size - (long long)new_size) >> 10);
3565 tputs(T("Unknown"));
3574 usage(CMD_OPTIMIZE, stderr);
3580 /* Split a WIM into a spanned set */
3582 imagex_split(int argc, tchar **argv, int cmd)
3586 int write_flags = 0;
3587 unsigned long part_size;
3592 for_opt(c, split_options) {
3594 case IMAGEX_CHECK_OPTION:
3595 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3596 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3608 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3609 if (tmp == argv[2] || *tmp) {
3610 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3611 imagex_error(T("The part size must be an integer or "
3612 "floating-point number of megabytes."));
3615 ret = wimlib_open_wim(argv[0], open_flags, &wim, imagex_progress_func);
3619 ret = wimlib_split(wim, argv[1], part_size, write_flags, imagex_progress_func);
3625 usage(CMD_SPLIT, stderr);
3631 #if WIM_MOUNTING_SUPPORTED
3632 /* Unmounts a mounted WIM image. */
3634 imagex_unmount(int argc, tchar **argv, int cmd)
3637 int unmount_flags = 0;
3640 for_opt(c, unmount_options) {
3642 case IMAGEX_COMMIT_OPTION:
3643 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3645 case IMAGEX_CHECK_OPTION:
3646 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3648 case IMAGEX_REBUILD_OPTION:
3649 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3651 case IMAGEX_LAZY_OPTION:
3652 unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3654 case IMAGEX_NEW_IMAGE_OPTION:
3655 unmount_flags |= WIMLIB_UNMOUNT_FLAG_NEW_IMAGE;
3666 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_NEW_IMAGE) {
3667 if (!(unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT)) {
3668 imagex_error(T("--new-image is meaningless "
3669 "without --commit also specified!"));
3672 imagex_printf(T("Committing changes as new image...\n"));
3675 ret = wimlib_unmount_image(argv[0], unmount_flags,
3676 imagex_progress_func);
3678 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3683 usage(CMD_UNMOUNT, stderr);
3688 #endif /* WIM_MOUNTING_SUPPORTED */
3691 * Add, delete, or rename files in a WIM image.
3694 imagex_update(int argc, tchar **argv, int cmd)
3696 const tchar *wimfile;
3700 int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3701 int write_flags = 0;
3702 int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3703 int default_add_flags = WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
3704 WIMLIB_ADD_FLAG_VERBOSE |
3705 WIMLIB_ADD_FLAG_WINCONFIG;
3706 int default_delete_flags = 0;
3707 unsigned num_threads = 0;
3709 tchar *cmd_file_contents;
3710 size_t cmd_file_nchars;
3711 struct wimlib_update_command *cmds;
3713 tchar *command_str = NULL;
3714 tchar *config_file = NULL;
3716 for_opt(c, update_options) {
3718 /* Generic or write options */
3719 case IMAGEX_THREADS_OPTION:
3720 num_threads = parse_num_threads(optarg);
3721 if (num_threads == UINT_MAX)
3724 case IMAGEX_CHECK_OPTION:
3725 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3726 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3728 case IMAGEX_REBUILD_OPTION:
3729 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3731 case IMAGEX_COMMAND_OPTION:
3733 imagex_error(T("--command may only be specified "
3734 "one time. Please provide\n"
3735 " the update commands "
3736 "on standard input instead."));
3739 command_str = tstrdup(optarg);
3741 imagex_error(T("Out of memory!"));
3745 /* Default delete options */
3746 case IMAGEX_FORCE_OPTION:
3747 default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3749 case IMAGEX_RECURSIVE_OPTION:
3750 default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3753 /* Global add option */
3754 case IMAGEX_CONFIG_OPTION:
3755 default_add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
3756 config_file = optarg;
3759 /* Default add options */
3760 case IMAGEX_VERBOSE_OPTION:
3761 /* No longer does anything. */
3763 case IMAGEX_DEREFERENCE_OPTION:
3764 default_add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
3766 case IMAGEX_UNIX_DATA_OPTION:
3767 default_add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
3769 case IMAGEX_NO_ACLS_OPTION:
3770 default_add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
3772 case IMAGEX_STRICT_ACLS_OPTION:
3773 default_add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
3782 if (argc != 1 && argc != 2)
3786 ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3788 goto out_free_command_str;
3791 /* Image explicitly specified. */
3792 image = wimlib_resolve_image(wim, argv[1]);
3793 ret = verify_image_exists_and_is_single(image, argv[1],
3796 goto out_wimlib_free;
3798 /* No image specified; default to image 1, but only if the WIM
3799 * contains exactly one image. */
3800 struct wimlib_wim_info info;
3802 wimlib_get_wim_info(wim, &info);
3803 if (info.image_count != 1) {
3804 imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3805 wimfile, info.image_count);
3812 /* Read update commands from standard input, or the command string if
3815 cmd_file_contents = NULL;
3816 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3819 if (isatty(STDIN_FILENO)) {
3820 tputs(T("Reading update commands from standard input..."));
3821 recommend_man_page(CMD_UPDATE, stdout);
3823 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3824 if (!cmd_file_contents) {
3826 goto out_wimlib_free;
3829 /* Parse the update commands */
3830 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3835 goto out_free_cmd_file_contents;
3838 /* Set default flags and capture config on the update commands */
3839 for (size_t i = 0; i < num_cmds; i++) {
3840 switch (cmds[i].op) {
3841 case WIMLIB_UPDATE_OP_ADD:
3842 cmds[i].add.add_flags |= default_add_flags;
3843 cmds[i].add.config_file = config_file;
3845 case WIMLIB_UPDATE_OP_DELETE:
3846 cmds[i].delete_.delete_flags |= default_delete_flags;
3853 /* Execute the update commands */
3854 ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3855 imagex_progress_func);
3859 /* Overwrite the updated WIM */
3860 ret = wimlib_overwrite(wim, write_flags, num_threads,
3861 imagex_progress_func);
3864 out_free_cmd_file_contents:
3865 free(cmd_file_contents);
3868 out_free_command_str:
3873 usage(CMD_UPDATE, stderr);
3876 goto out_free_command_str;
3881 struct imagex_command {
3883 int (*func)(int argc, tchar **argv, int cmd);
3886 static const struct imagex_command imagex_commands[] = {
3887 [CMD_APPEND] = {T("append"), imagex_capture_or_append},
3888 [CMD_APPLY] = {T("apply"), imagex_apply},
3889 [CMD_CAPTURE] = {T("capture"), imagex_capture_or_append},
3890 [CMD_DELETE] = {T("delete"), imagex_delete},
3891 [CMD_DIR ] = {T("dir"), imagex_dir},
3892 [CMD_EXPORT] = {T("export"), imagex_export},
3893 [CMD_EXTRACT] = {T("extract"), imagex_extract},
3894 [CMD_INFO] = {T("info"), imagex_info},
3895 [CMD_JOIN] = {T("join"), imagex_join},
3896 #if WIM_MOUNTING_SUPPORTED
3897 [CMD_MOUNT] = {T("mount"), imagex_mount_rw_or_ro},
3898 [CMD_MOUNTRW] = {T("mountrw"), imagex_mount_rw_or_ro},
3900 [CMD_OPTIMIZE] = {T("optimize"), imagex_optimize},
3901 [CMD_SPLIT] = {T("split"), imagex_split},
3902 #if WIM_MOUNTING_SUPPORTED
3903 [CMD_UNMOUNT] = {T("unmount"), imagex_unmount},
3905 [CMD_UPDATE] = {T("update"), imagex_update},
3908 static const tchar *usage_strings[] = {
3911 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3912 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3913 " [--nocheck] [--flags EDITION_ID] [--dereference]\n"
3914 " [--config=FILE] [--threads=NUM_THREADS] [--source-list]\n"
3915 " [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3916 " [--update-of=[WIMFILE:]IMAGE]\n"
3920 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME | all)]\n"
3921 " (DIRECTORY | NTFS_VOLUME) [--check] [--ref=\"GLOB\"]\n"
3922 " [--no-acls] [--strict-acls] [--no-attributes]\n"
3923 " [--rpfix] [--norpfix] [--hardlink] [--symlink]\n"
3924 " [--include-invalid-names]\n"
3928 " %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3929 " [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot] [--check]\n"
3930 " [--nocheck] [--compress=TYPE] [--flags EDITION_ID]\n"
3931 " [--dereference] [--config=FILE] [--threads=NUM_THREADS]\n"
3932 " [--source-list] [--no-acls] [--strict-acls] [--rpfix]\n"
3933 " [--norpfix] [--update-of=[WIMFILE:]IMAGE]\n"
3934 " [--delta-from=WIMFILE]\n"
3938 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n"
3939 " [--check] [--soft]\n"
3943 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH] [--detailed]\n"
3947 " %"TS" SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
3948 " DEST_WIMFILE [DEST_IMAGE_NAME [DEST_IMAGE_DESCRIPTION]]\n"
3949 " [--boot] [--check] [--nocheck] [--compress=TYPE]\n"
3950 " [--ref=\"GLOB\"] [--threads=NUM_THREADS] [--rebuild]\n"
3954 " %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME) [(PATH | @LISTFILE)...]\n"
3955 " [--check] [--ref=\"GLOB\"] [--dest-dir=CMD_DIR]\n"
3956 " [--to-stdout] [--no-acls] [--strict-acls]\n"
3957 " [--no-attributes] [--include-invalid-names]\n"
3958 " [--no-wildcards] [--nullglob] [--preserve-dir-structure]\n"
3962 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME) [NEW_NAME\n"
3963 " [NEW_DESC]]] [--boot] [--check] [--nocheck] [--xml]\n"
3964 " [--extract-xml FILE] [--header] [--lookup-table]\n"
3968 " %"TS" OUT_WIMFILE SPLIT_WIM_PART... [--check]\n"
3970 #if WIM_MOUNTING_SUPPORTED
3973 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3974 " [--check] [--streams-interface=INTERFACE]\n"
3975 " [--ref=\"GLOB\"] [--allow-other]\n"
3979 " %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3980 " [--check] [--streams-interface=INTERFACE]\n"
3981 " [--staging-dir=CMD_DIR] [--allow-other]\n"
3986 " %"TS" WIMFILE [--check] [--nocheck] [--recompress]\n"
3987 " [--recompress-slow] [--compress=TYPE]\n"
3988 " [--threads=NUM_THREADS]\n"
3992 " %"TS" WIMFILE SPLIT_WIM_PART_1 PART_SIZE_MB [--check]\n"
3994 #if WIM_MOUNTING_SUPPORTED
3997 " %"TS" DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
4003 " %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
4004 " [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
4005 " [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
4009 static const tchar *invocation_name;
4010 static int invocation_cmd = CMD_NONE;
4012 static const tchar *get_cmd_string(int cmd, bool nospace)
4014 static tchar buf[50];
4015 if (cmd == CMD_NONE) {
4016 tsprintf(buf, T("%"TS), T(IMAGEX_PROGNAME));
4017 } else if (invocation_cmd != CMD_NONE) {
4018 tsprintf(buf, T("wim%"TS), imagex_commands[cmd].name);
4020 const tchar *format;
4023 format = T("%"TS"-%"TS"");
4025 format = T("%"TS" %"TS"");
4026 tsprintf(buf, format, invocation_name, imagex_commands[cmd].name);
4034 static const tchar *s =
4036 IMAGEX_PROGNAME " (distributed with " PACKAGE " " PACKAGE_VERSION ")\n"
4037 "Copyright (C) 2012, 2013, 2014 Eric Biggers\n"
4038 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
4039 "This is free software: you are free to change and redistribute it.\n"
4040 "There is NO WARRANTY, to the extent permitted by law.\n"
4042 "Report bugs to "PACKAGE_BUGREPORT".\n"
4049 help_or_version(int argc, tchar **argv, int cmd)
4054 for (i = 1; i < argc; i++) {
4056 if (p[0] == T('-') && p[1] == T('-')) {
4058 if (!tstrcmp(p, T("help"))) {
4059 if (cmd == CMD_NONE)
4064 } else if (!tstrcmp(p, T("version"))) {
4073 print_usage_string(int cmd, FILE *fp)
4075 tfprintf(fp, usage_strings[cmd], get_cmd_string(cmd, false));
4079 recommend_man_page(int cmd, FILE *fp)
4081 const tchar *format_str;
4083 format_str = T("Uncommon options are not listed;\n"
4084 "See %"TS".pdf in the doc directory for more details.\n");
4086 format_str = T("Uncommon options are not listed;\n"
4087 "Try `man %"TS"' for more details.\n");
4089 tfprintf(fp, format_str, get_cmd_string(cmd, true));
4093 usage(int cmd, FILE *fp)
4095 tfprintf(fp, T("Usage:\n"));
4096 print_usage_string(cmd, fp);
4097 tfprintf(fp, T("\n"));
4098 recommend_man_page(cmd, fp);
4104 tfprintf(fp, T("Usage:\n"));
4105 for (int cmd = 0; cmd < CMD_MAX; cmd++) {
4106 print_usage_string(cmd, fp);
4107 tfprintf(fp, T("\n"));
4109 static const tchar *extra =
4112 " %"TS" --version\n"
4114 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
4117 tfprintf(fp, extra, invocation_name, invocation_name);
4118 recommend_man_page(CMD_NONE, fp);
4121 /* Entry point for wimlib's ImageX implementation. On UNIX the command
4122 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
4123 * something else), while an Windows the command arguments will be UTF-16LE
4124 * encoded 'wchar_t' strings. */
4127 wmain(int argc, wchar_t **argv, wchar_t **envp)
4129 main(int argc, char **argv)
4136 imagex_info_file = stdout;
4137 invocation_name = tbasename(argv[0]);
4140 if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
4141 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
4145 setlocale(LC_ALL, "");
4146 codeset = nl_langinfo(CODESET);
4147 if (!strstr(codeset, "UTF-8") &&
4148 !strstr(codeset, "UTF8") &&
4149 !strstr(codeset, "utf-8") &&
4150 !strstr(codeset, "utf8"))
4153 "WARNING: Running %"TS" in a UTF-8 locale is recommended!\n"
4154 " Maybe try: `export LANG=en_US.UTF-8'?\n"
4155 " Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
4156 " to any value to force wimlib to use UTF-8.\n",
4162 #endif /* !__WIN32__ */
4165 tchar *igcase = tgetenv(T("WIMLIB_IMAGEX_IGNORE_CASE"));
4166 if (igcase != NULL) {
4167 if (!tstrcmp(igcase, T("no")) ||
4168 !tstrcmp(igcase, T("0")))
4169 init_flags |= WIMLIB_INIT_FLAG_DEFAULT_CASE_SENSITIVE;
4170 else if (!tstrcmp(igcase, T("yes")) ||
4171 !tstrcmp(igcase, T("1")))
4172 init_flags |= WIMLIB_INIT_FLAG_DEFAULT_CASE_INSENSITIVE;
4175 "WARNING: Ignoring unknown setting of "
4176 "WIMLIB_IMAGEX_IGNORE_CASE\n");
4181 /* Allow being invoked as wimCOMMAND (e.g. wimapply). */
4183 if (!tstrncmp(invocation_name, T("wim"), 3) &&
4184 tstrcmp(invocation_name, T(IMAGEX_PROGNAME))) {
4185 for (int i = 0; i < CMD_MAX; i++) {
4186 if (!tstrcmp(invocation_name + 3,
4187 imagex_commands[i].name))
4196 /* Unless already known from the invocation name, determine which
4197 * command was specified. */
4198 if (cmd == CMD_NONE) {
4200 imagex_error(T("No command specified!\n"));
4204 for (int i = 0; i < CMD_MAX; i++) {
4205 if (!tstrcmp(argv[1], imagex_commands[i].name)) {
4210 if (cmd != CMD_NONE) {
4216 /* Handle --help and --version. --help can be either for the program as
4217 * a whole (cmd == CMD_NONE) or just for a specific command (cmd !=
4218 * CMD_NONE). Note: help_or_version() will not return if a --help or
4219 * --version argument was found. */
4220 help_or_version(argc, argv, cmd);
4222 /* Bail if a valid command was not specified. */
4223 if (cmd == CMD_NONE) {
4224 imagex_error(T("Unrecognized command: `%"TS"'\n"), argv[1]);
4229 /* Enable warning and error messages in wimlib be more user-friendly.
4231 wimlib_set_print_errors(true);
4233 /* Initialize wimlib. */
4234 ret = wimlib_global_init(init_flags);
4236 goto out_check_status;
4238 /* Call the command handler function. */
4239 ret = imagex_commands[cmd].func(argc, argv, cmd);
4241 /* Check for error writing to standard output, especially since for some
4242 * commands, writing to standard output is part of the program's actual
4243 * behavior and not just for informational purposes. */
4244 if (ferror(stdout) || fclose(stdout)) {
4245 imagex_error_with_errno(T("error writing to standard output"));
4250 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
4251 * itself (not by wimlib). 0 indicates success. > 0 indicates a wimlib
4252 * error code from which an error message can be printed. */
4254 imagex_error(T("Exiting with error code %d:\n"
4256 wimlib_get_error_string(ret));
4257 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
4258 imagex_error_with_errno(T("errno"));
4260 /* Make wimlib free any resources it's holding (although this is not
4261 * strictly necessary because the process is ending anyway). */
4262 wimlib_global_cleanup();