]> wimlib.net Git - wimlib/blob - programs/imagex.c
wimoptimize: Add --recompress-slow, imply --recompress
[wimlib] / programs / imagex.c
1 /*
2  * imagex.c
3  *
4  * Use wimlib to create, modify, extract, mount, unmount, or display information
5  * about a WIM file
6  */
7
8 /*
9  * Copyright (C) 2012, 2013 Eric Biggers
10  *
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.
15  *
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.
20  *
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/>.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #  include "config.h" /* Need for PACKAGE_VERSION, etc. */
27 #endif
28
29 #include "wimlib.h"
30 #include "wimlib_tchar.h"
31
32 #include <ctype.h>
33 #include <errno.h>
34
35 #include <inttypes.h>
36 #include <libgen.h>
37 #include <limits.h>
38 #include <stdarg.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/stat.h>
42 #include <unistd.h>
43 #include <locale.h>
44
45 #ifdef HAVE_ALLOCA_H
46 #  include <alloca.h>
47 #endif
48
49 #ifdef __WIN32__
50 #  include "imagex-win32.h"
51 #  define tbasename     win32_wbasename
52 #  define OS_PREFERRED_PATH_SEPARATOR L'\\'
53 #  define OS_PREFERRED_PATH_SEPARATOR_STRING L"\\"
54 #else /* __WIN32__ */
55 #  include <glob.h>
56 #  include <getopt.h>
57 #  include <langinfo.h>
58 #  define tbasename     basename
59 #  define OS_PREFERRED_PATH_SEPARATOR '/'
60 #  define OS_PREFERRED_PATH_SEPARATOR_STRING "/"
61 static inline void set_fd_to_binary_mode(int fd)
62 {
63 }
64 #endif /* !__WIN32 */
65
66 /* Don't confuse the user by presenting the mounting commands on Windows when
67  * they will never work.  However on UNIX-like systems we always present them,
68  * even if WITH_FUSE is not defined at this point, as to not tie the build of
69  * wimlib-imagex to a specific build of wimlib.  */
70 #ifdef __WIN32__
71 #  define WIM_MOUNTING_SUPPORTED 0
72 #else
73 #  define WIM_MOUNTING_SUPPORTED 1
74 #endif
75
76 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
77
78 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
79                                 opts, NULL)) != -1)
80
81 enum {
82         CMD_NONE = -1,
83         CMD_APPEND = 0,
84         CMD_APPLY,
85         CMD_CAPTURE,
86         CMD_DELETE,
87         CMD_DIR,
88         CMD_EXPORT,
89         CMD_EXTRACT,
90         CMD_INFO,
91         CMD_JOIN,
92 #if WIM_MOUNTING_SUPPORTED
93         CMD_MOUNT,
94         CMD_MOUNTRW,
95 #endif
96         CMD_OPTIMIZE,
97         CMD_SPLIT,
98 #if WIM_MOUNTING_SUPPORTED
99         CMD_UNMOUNT,
100 #endif
101         CMD_UPDATE,
102         CMD_MAX,
103 };
104
105 static void usage(int cmd, FILE *fp);
106 static void usage_all(FILE *fp);
107 static void recommend_man_page(int cmd, FILE *fp);
108 static const tchar *get_cmd_string(int cmd, bool nospace);
109
110 static int imagex_progress_func(enum wimlib_progress_msg msg,
111                                 const union wimlib_progress_info *info);
112
113 static bool imagex_be_quiet = false;
114 static FILE *imagex_info_file;
115
116 #define imagex_printf(format, ...) \
117                 tfprintf(imagex_info_file, format, ##__VA_ARGS__)
118
119 enum {
120         IMAGEX_ALLOW_OTHER_OPTION,
121         IMAGEX_BOOT_OPTION,
122         IMAGEX_CHECK_OPTION,
123         IMAGEX_COMMAND_OPTION,
124         IMAGEX_COMMIT_OPTION,
125         IMAGEX_COMPRESS_OPTION,
126         IMAGEX_COMPRESS_SLOW_OPTION,
127         IMAGEX_CONFIG_OPTION,
128         IMAGEX_DEBUG_OPTION,
129         IMAGEX_DELTA_FROM_OPTION,
130         IMAGEX_DEREFERENCE_OPTION,
131         IMAGEX_DEST_DIR_OPTION,
132         IMAGEX_EXTRACT_XML_OPTION,
133         IMAGEX_FLAGS_OPTION,
134         IMAGEX_FORCE_OPTION,
135         IMAGEX_HARDLINK_OPTION,
136         IMAGEX_HEADER_OPTION,
137         IMAGEX_INCLUDE_INVALID_NAMES_OPTION,
138         IMAGEX_LAZY_OPTION,
139         IMAGEX_LOOKUP_TABLE_OPTION,
140         IMAGEX_METADATA_OPTION,
141         IMAGEX_NORPFIX_OPTION,
142         IMAGEX_NOCHECK_OPTION,
143         IMAGEX_NO_ACLS_OPTION,
144         IMAGEX_NOT_PIPABLE_OPTION,
145         IMAGEX_PATH_OPTION,
146         IMAGEX_PIPABLE_OPTION,
147         IMAGEX_REBUILD_OPTION,
148         IMAGEX_RECOMPRESS_OPTION,
149         IMAGEX_RECURSIVE_OPTION,
150         IMAGEX_REF_OPTION,
151         IMAGEX_RESUME_OPTION,
152         IMAGEX_RPFIX_OPTION,
153         IMAGEX_SOFT_OPTION,
154         IMAGEX_SOURCE_LIST_OPTION,
155         IMAGEX_STAGING_DIR_OPTION,
156         IMAGEX_STREAMS_INTERFACE_OPTION,
157         IMAGEX_STRICT_ACLS_OPTION,
158         IMAGEX_SYMLINK_OPTION,
159         IMAGEX_THREADS_OPTION,
160         IMAGEX_TO_STDOUT_OPTION,
161         IMAGEX_UNIX_DATA_OPTION,
162         IMAGEX_UPDATE_OF_OPTION,
163         IMAGEX_VERBOSE_OPTION,
164         IMAGEX_XML_OPTION,
165 };
166
167 static const struct option apply_options[] = {
168         {T("check"),       no_argument,       NULL, IMAGEX_CHECK_OPTION},
169         {T("hardlink"),    no_argument,       NULL, IMAGEX_HARDLINK_OPTION},
170         {T("symlink"),     no_argument,       NULL, IMAGEX_SYMLINK_OPTION},
171         {T("verbose"),     no_argument,       NULL, IMAGEX_VERBOSE_OPTION},
172         {T("ref"),         required_argument, NULL, IMAGEX_REF_OPTION},
173         {T("unix-data"),   no_argument,       NULL, IMAGEX_UNIX_DATA_OPTION},
174         {T("noacls"),      no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
175         {T("no-acls"),     no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
176         {T("strict-acls"), no_argument,       NULL, IMAGEX_STRICT_ACLS_OPTION},
177         {T("rpfix"),       no_argument,       NULL, IMAGEX_RPFIX_OPTION},
178         {T("norpfix"),     no_argument,       NULL, IMAGEX_NORPFIX_OPTION},
179         {T("include-invalid-names"), no_argument,       NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
180
181         /* --resume is undocumented for now as it needs improvement.  */
182         {T("resume"),      no_argument,       NULL, IMAGEX_RESUME_OPTION},
183         {NULL, 0, NULL, 0},
184 };
185
186 static const struct option capture_or_append_options[] = {
187         {T("boot"),        no_argument,       NULL, IMAGEX_BOOT_OPTION},
188         {T("check"),       no_argument,       NULL, IMAGEX_CHECK_OPTION},
189         {T("no-check"),    no_argument,       NULL, IMAGEX_NOCHECK_OPTION},
190         {T("nocheck"),     no_argument,       NULL, IMAGEX_NOCHECK_OPTION},
191         {T("compress"),    required_argument, NULL, IMAGEX_COMPRESS_OPTION},
192         {T("compress-slow"), no_argument,     NULL, IMAGEX_COMPRESS_SLOW_OPTION},
193         {T("config"),      required_argument, NULL, IMAGEX_CONFIG_OPTION},
194         {T("dereference"), no_argument,       NULL, IMAGEX_DEREFERENCE_OPTION},
195         {T("flags"),       required_argument, NULL, IMAGEX_FLAGS_OPTION},
196         {T("verbose"),     no_argument,       NULL, IMAGEX_VERBOSE_OPTION},
197         {T("threads"),     required_argument, NULL, IMAGEX_THREADS_OPTION},
198         {T("rebuild"),     no_argument,       NULL, IMAGEX_REBUILD_OPTION},
199         {T("unix-data"),   no_argument,       NULL, IMAGEX_UNIX_DATA_OPTION},
200         {T("source-list"), no_argument,       NULL, IMAGEX_SOURCE_LIST_OPTION},
201         {T("noacls"),      no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
202         {T("no-acls"),     no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
203         {T("strict-acls"), no_argument,       NULL, IMAGEX_STRICT_ACLS_OPTION},
204         {T("rpfix"),       no_argument,       NULL, IMAGEX_RPFIX_OPTION},
205         {T("norpfix"),     no_argument,       NULL, IMAGEX_NORPFIX_OPTION},
206         {T("pipable"),     no_argument,       NULL, IMAGEX_PIPABLE_OPTION},
207         {T("not-pipable"), no_argument,       NULL, IMAGEX_NOT_PIPABLE_OPTION},
208         {T("update-of"),   required_argument, NULL, IMAGEX_UPDATE_OF_OPTION},
209         {T("delta-from"),  required_argument, NULL, IMAGEX_DELTA_FROM_OPTION},
210         {NULL, 0, NULL, 0},
211 };
212
213 static const struct option delete_options[] = {
214         {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
215         {T("soft"),  no_argument, NULL, IMAGEX_SOFT_OPTION},
216         {NULL, 0, NULL, 0},
217 };
218
219 static const struct option dir_options[] = {
220         {T("path"), required_argument, NULL, IMAGEX_PATH_OPTION},
221         {NULL, 0, NULL, 0},
222 };
223
224 static const struct option export_options[] = {
225         {T("boot"),        no_argument,       NULL, IMAGEX_BOOT_OPTION},
226         {T("check"),       no_argument,       NULL, IMAGEX_CHECK_OPTION},
227         {T("nocheck"),     no_argument,       NULL, IMAGEX_NOCHECK_OPTION},
228         {T("no-check"),    no_argument,       NULL, IMAGEX_NOCHECK_OPTION},
229         {T("compress"),    required_argument, NULL, IMAGEX_COMPRESS_OPTION},
230         {T("ref"),         required_argument, NULL, IMAGEX_REF_OPTION},
231         {T("threads"),     required_argument, NULL, IMAGEX_THREADS_OPTION},
232         {T("rebuild"),     no_argument,       NULL, IMAGEX_REBUILD_OPTION},
233         {T("pipable"),     no_argument,       NULL, IMAGEX_PIPABLE_OPTION},
234         {T("not-pipable"), no_argument,       NULL, IMAGEX_NOT_PIPABLE_OPTION},
235         {NULL, 0, NULL, 0},
236 };
237
238 static const struct option extract_options[] = {
239         {T("check"),       no_argument,       NULL, IMAGEX_CHECK_OPTION},
240         {T("verbose"),     no_argument,       NULL, IMAGEX_VERBOSE_OPTION},
241         {T("ref"),         required_argument, NULL, IMAGEX_REF_OPTION},
242         {T("unix-data"),   no_argument,       NULL, IMAGEX_UNIX_DATA_OPTION},
243         {T("noacls"),      no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
244         {T("no-acls"),     no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
245         {T("strict-acls"), no_argument,       NULL, IMAGEX_STRICT_ACLS_OPTION},
246         {T("dest-dir"),    required_argument, NULL, IMAGEX_DEST_DIR_OPTION},
247         {T("to-stdout"),   no_argument,       NULL, IMAGEX_TO_STDOUT_OPTION},
248         {T("include-invalid-names"), no_argument, NULL, IMAGEX_INCLUDE_INVALID_NAMES_OPTION},
249         {NULL, 0, NULL, 0},
250 };
251
252 static const struct option info_options[] = {
253         {T("boot"),         no_argument,       NULL, IMAGEX_BOOT_OPTION},
254         {T("check"),        no_argument,       NULL, IMAGEX_CHECK_OPTION},
255         {T("nocheck"),      no_argument,       NULL, IMAGEX_NOCHECK_OPTION},
256         {T("no-check"),     no_argument,       NULL, IMAGEX_NOCHECK_OPTION},
257         {T("extract-xml"),  required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
258         {T("header"),       no_argument,       NULL, IMAGEX_HEADER_OPTION},
259         {T("lookup-table"), no_argument,       NULL, IMAGEX_LOOKUP_TABLE_OPTION},
260         {T("metadata"),     no_argument,       NULL, IMAGEX_METADATA_OPTION},
261         {T("xml"),          no_argument,       NULL, IMAGEX_XML_OPTION},
262         {NULL, 0, NULL, 0},
263 };
264
265 static const struct option join_options[] = {
266         {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
267         {NULL, 0, NULL, 0},
268 };
269
270 static const struct option mount_options[] = {
271         {T("check"),             no_argument,       NULL, IMAGEX_CHECK_OPTION},
272         {T("debug"),             no_argument,       NULL, IMAGEX_DEBUG_OPTION},
273         {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
274         {T("ref"),               required_argument, NULL, IMAGEX_REF_OPTION},
275         {T("staging-dir"),       required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
276         {T("unix-data"),         no_argument,       NULL, IMAGEX_UNIX_DATA_OPTION},
277         {T("allow-other"),       no_argument,       NULL, IMAGEX_ALLOW_OTHER_OPTION},
278         {NULL, 0, NULL, 0},
279 };
280
281 static const struct option optimize_options[] = {
282         {T("check"),       no_argument,       NULL, IMAGEX_CHECK_OPTION},
283         {T("nocheck"),     no_argument,       NULL, IMAGEX_NOCHECK_OPTION},
284         {T("no-check"),    no_argument,       NULL, IMAGEX_NOCHECK_OPTION},
285         {T("recompress"),  no_argument,       NULL, IMAGEX_RECOMPRESS_OPTION},
286         {T("compress-slow"), no_argument,     NULL, IMAGEX_COMPRESS_SLOW_OPTION},
287         {T("recompress-slow"), no_argument,     NULL, IMAGEX_COMPRESS_SLOW_OPTION},
288         {T("threads"),     required_argument, NULL, IMAGEX_THREADS_OPTION},
289         {T("pipable"),     no_argument,       NULL, IMAGEX_PIPABLE_OPTION},
290         {T("not-pipable"), no_argument,       NULL, IMAGEX_NOT_PIPABLE_OPTION},
291         {NULL, 0, NULL, 0},
292 };
293
294 static const struct option split_options[] = {
295         {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
296         {NULL, 0, NULL, 0},
297 };
298
299 static const struct option unmount_options[] = {
300         {T("commit"),  no_argument, NULL, IMAGEX_COMMIT_OPTION},
301         {T("check"),   no_argument, NULL, IMAGEX_CHECK_OPTION},
302         {T("rebuild"), no_argument, NULL, IMAGEX_REBUILD_OPTION},
303         {T("lazy"),    no_argument, NULL, IMAGEX_LAZY_OPTION},
304         {NULL, 0, NULL, 0},
305 };
306
307 static const struct option update_options[] = {
308         /* Careful: some of the options here set the defaults for update
309          * commands, but the flags given to an actual update command (and not to
310          * `imagex update' itself are also handled in
311          * update_command_add_option().  */
312         {T("threads"),     required_argument, NULL, IMAGEX_THREADS_OPTION},
313         {T("check"),       no_argument,       NULL, IMAGEX_CHECK_OPTION},
314         {T("rebuild"),     no_argument,       NULL, IMAGEX_REBUILD_OPTION},
315         {T("command"),     required_argument, NULL, IMAGEX_COMMAND_OPTION},
316
317         /* Default delete options */
318         {T("force"),       no_argument,       NULL, IMAGEX_FORCE_OPTION},
319         {T("recursive"),   no_argument,       NULL, IMAGEX_RECURSIVE_OPTION},
320
321         /* Global add option */
322         {T("config"),      required_argument, NULL, IMAGEX_CONFIG_OPTION},
323
324         /* Default add options */
325         {T("verbose"),     no_argument,       NULL, IMAGEX_VERBOSE_OPTION},
326         {T("dereference"), no_argument,       NULL, IMAGEX_DEREFERENCE_OPTION},
327         {T("unix-data"),   no_argument,       NULL, IMAGEX_UNIX_DATA_OPTION},
328         {T("noacls"),      no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
329         {T("no-acls"),     no_argument,       NULL, IMAGEX_NO_ACLS_OPTION},
330         {T("strict-acls"), no_argument,       NULL, IMAGEX_STRICT_ACLS_OPTION},
331
332         {NULL, 0, NULL, 0},
333 };
334
335 #if 0
336 #       define _format_attribute(type, format_str, args_start) \
337                         __attribute__((format(type, format_str, args_start)))
338 #else
339 #       define _format_attribute(type, format_str, args_start)
340 #endif
341
342 /* Print formatted error message to stderr. */
343 static void _format_attribute(printf, 1, 2)
344 imagex_error(const tchar *format, ...)
345 {
346         va_list va;
347         va_start(va, format);
348         tfputs(T("ERROR: "), stderr);
349         tvfprintf(stderr, format, va);
350         tputc(T('\n'), stderr);
351         va_end(va);
352 }
353
354 /* Print formatted error message to stderr. */
355 static void _format_attribute(printf, 1, 2)
356 imagex_error_with_errno(const tchar *format, ...)
357 {
358         int errno_save = errno;
359         va_list va;
360         va_start(va, format);
361         tfputs(T("ERROR: "), stderr);
362         tvfprintf(stderr, format, va);
363         tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
364         va_end(va);
365 }
366
367 static int
368 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
369 {
370         if (image == WIMLIB_NO_IMAGE) {
371                 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
372                              "       Please specify a 1-based image index or "
373                              "image name.  To list the images\n"
374                              "       contained in the WIM archive, run\n"
375                              "\n"
376                              "           %"TS" \"%"TS"\"\n"),
377                              image_name, wim_name,
378                              get_cmd_string(CMD_INFO, false), wim_name);
379                 return WIMLIB_ERR_INVALID_IMAGE;
380         }
381         return 0;
382 }
383
384 static int
385 verify_image_is_single(int image)
386 {
387         if (image == WIMLIB_ALL_IMAGES) {
388                 imagex_error(T("Cannot specify all images for this action!"));
389                 return WIMLIB_ERR_INVALID_IMAGE;
390         }
391         return 0;
392 }
393
394 static int
395 verify_image_exists_and_is_single(int image, const tchar *image_name,
396                                   const tchar *wim_name)
397 {
398         int ret;
399         ret = verify_image_exists(image, image_name, wim_name);
400         if (ret == 0)
401                 ret = verify_image_is_single(image);
402         return ret;
403 }
404
405 /* Parse the argument to --compress */
406 static int
407 get_compression_type(const tchar *optarg)
408 {
409         if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
410                 return WIMLIB_COMPRESSION_TYPE_LZX;
411         else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
412                 return WIMLIB_COMPRESSION_TYPE_XPRESS;
413         else if (!tstrcasecmp(optarg, T("none")))
414                 return WIMLIB_COMPRESSION_TYPE_NONE;
415         else {
416                 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
417                              "\"maximum\", \"fast\", or \"none\"."), optarg);
418                 return WIMLIB_COMPRESSION_TYPE_INVALID;
419         }
420 }
421
422 static void
423 set_compress_slow(void)
424 {
425         static const struct wimlib_lzx_params slow_params = {
426                 .size_of_this = sizeof(struct wimlib_lzx_params),
427                 .algorithm = WIMLIB_LZX_ALGORITHM_SLOW,
428                 .alg_params = {
429                         .slow = {
430                                 .use_len2_matches = 1,
431                                 .num_fast_bytes = 96,
432                                 .num_optim_passes = 4,
433                                 .max_search_depth = 100,
434                                 .max_matches_per_pos = 10,
435                                 .main_nostat_cost = 15,
436                                 .len_nostat_cost = 15,
437                                 .aligned_nostat_cost = 7,
438                         },
439                 },
440         };
441         if (wimlib_lzx_set_default_params(&slow_params))
442                 imagex_error(T("Couldn't set slow compression parameters.!"));
443 }
444
445 struct string_set {
446         const tchar **strings;
447         unsigned num_strings;
448         unsigned num_alloc_strings;
449 };
450
451 #define STRING_SET_INITIALIZER \
452         { .strings = NULL, .num_strings = 0, .num_alloc_strings = 0, }
453
454 #define STRING_SET(_strings) \
455         struct string_set _strings = STRING_SET_INITIALIZER
456
457 static int
458 string_set_append(struct string_set *set, const tchar *glob)
459 {
460         unsigned num_alloc_strings = set->num_alloc_strings;
461
462         if (set->num_strings == num_alloc_strings) {
463                 const tchar **new_strings;
464
465                 num_alloc_strings += 4;
466                 new_strings = realloc(set->strings,
467                                       sizeof(set->strings[0]) * num_alloc_strings);
468                 if (!new_strings) {
469                         imagex_error(T("Out of memory!"));
470                         return -1;
471                 }
472                 set->strings = new_strings;
473                 set->num_alloc_strings = num_alloc_strings;
474         }
475         set->strings[set->num_strings++] = glob;
476         return 0;
477 }
478
479 static void
480 string_set_destroy(struct string_set *set)
481 {
482         free(set->strings);
483 }
484
485 static int
486 wim_reference_globs(WIMStruct *wim, struct string_set *set, int open_flags)
487 {
488         return wimlib_reference_resource_files(wim, set->strings,
489                                                set->num_strings,
490                                                WIMLIB_REF_FLAG_GLOB_ENABLE,
491                                                open_flags,
492                                                imagex_progress_func);
493 }
494
495 static void
496 do_resource_not_found_warning(const tchar *wimfile,
497                               const struct wimlib_wim_info *info,
498                               const struct string_set *refglobs)
499 {
500         if (info->total_parts > 1) {
501                 if (refglobs->num_strings == 0) {
502                         imagex_error(T("\"%"TS"\" is part of a split WIM. "
503                                        "Use --ref to specify the other parts."),
504                                      wimfile);
505                 } else {
506                         imagex_error(T("Perhaps the '--ref' argument did not "
507                                        "specify all other parts of the split "
508                                        "WIM?"));
509                 }
510         } else {
511                 imagex_error(T("If this is a delta WIM, use the --ref argument "
512                                "to specify the WIM(s) on which it is based."));
513         }
514 }
515
516 /* Returns the size of a file given its name, or -1 if the file does not exist
517  * or its size cannot be determined.  */
518 static off_t
519 file_get_size(const tchar *filename)
520 {
521         struct stat st;
522         if (tstat(filename, &st) == 0)
523                 return st.st_size;
524         else
525                 return (off_t)-1;
526 }
527
528 enum {
529         PARSE_STRING_SUCCESS = 0,
530         PARSE_STRING_FAILURE = 1,
531         PARSE_STRING_NONE = 2,
532 };
533
534 /*
535  * Parses a string token from an array of characters.
536  *
537  * Tokens are either whitespace-delimited, or double or single-quoted.
538  *
539  * @line_p:  Pointer to the pointer to the line of data.  Will be updated
540  *           to point past the string token iff the return value is
541  *           PARSE_STRING_SUCCESS.  If *len_p > 0, (*line_p)[*len_p - 1] must
542  *           be '\0'.
543  *
544  * @len_p:   @len_p initially stores the length of the line of data, which may
545  *           be 0, and it will be updated to the number of bytes remaining in
546  *           the line iff the return value is PARSE_STRING_SUCCESS.
547  *
548  * @fn_ret:  Iff the return value is PARSE_STRING_SUCCESS, a pointer to the
549  *           parsed string token will be returned here.
550  *
551  * Returns: PARSE_STRING_SUCCESS if a string token was successfully parsed; or
552  *          PARSE_STRING_FAILURE if the data was invalid due to a missing
553  *          closing quote; or PARSE_STRING_NONE if the line ended before the
554  *          beginning of a string token was found.
555  */
556 static int
557 parse_string(tchar **line_p, size_t *len_p, tchar **fn_ret)
558 {
559         size_t len = *len_p;
560         tchar *line = *line_p;
561         tchar *fn;
562         tchar quote_char;
563
564         /* Skip leading whitespace */
565         for (;;) {
566                 if (len == 0)
567                         return PARSE_STRING_NONE;
568                 if (!istspace(*line) && *line != T('\0'))
569                         break;
570                 line++;
571                 len--;
572         }
573         quote_char = *line;
574         if (quote_char == T('"') || quote_char == T('\'')) {
575                 /* Quoted string */
576                 line++;
577                 len--;
578                 fn = line;
579                 line = tmemchr(line, quote_char, len);
580                 if (!line) {
581                         imagex_error(T("Missing closing quote: %"TS), fn - 1);
582                         return PARSE_STRING_FAILURE;
583                 }
584         } else {
585                 /* Unquoted string.  Go until whitespace.  Line is terminated
586                  * by '\0', so no need to check 'len'. */
587                 fn = line;
588                 do {
589                         line++;
590                 } while (!istspace(*line) && *line != T('\0'));
591         }
592         *line = T('\0');
593         len -= line - fn;
594         *len_p = len;
595         *line_p = line;
596         *fn_ret = fn;
597         return PARSE_STRING_SUCCESS;
598 }
599
600 /* Parses a line of data (not an empty line or comment) in the source list file
601  * format.  (See the man page for 'wimlib-imagex capture' for details on this
602  * format and the meaning.)
603  *
604  * @line:  Line of data to be parsed.  line[len - 1] must be '\0', unless
605  *         len == 0.  The data in @line will be modified by this function call.
606  *
607  * @len:   Length of the line of data.
608  *
609  * @source:  On success, the capture source and target described by the line is
610  *           written into this destination.  Note that it will contain pointers
611  *           to data in the @line array.
612  *
613  * Returns true if the line was valid; false otherwise.  */
614 static bool
615 parse_source_list_line(tchar *line, size_t len,
616                        struct wimlib_capture_source *source)
617 {
618         /* SOURCE [DEST] */
619         int ret;
620         ret = parse_string(&line, &len, &source->fs_source_path);
621         if (ret != PARSE_STRING_SUCCESS)
622                 return false;
623         ret = parse_string(&line, &len, &source->wim_target_path);
624         if (ret == PARSE_STRING_NONE)
625                 source->wim_target_path = source->fs_source_path;
626         return ret != PARSE_STRING_FAILURE;
627 }
628
629 /* Returns %true if the given line of length @len > 0 is a comment or empty line
630  * in the source list file format. */
631 static bool
632 is_comment_line(const tchar *line, size_t len)
633 {
634         for (;;) {
635                 if (*line == T('#'))
636                         return true;
637                 if (!istspace(*line) && *line != T('\0'))
638                         return false;
639                 ++line;
640                 --len;
641                 if (len == 0)
642                         return true;
643         }
644 }
645
646 static ssize_t
647 text_file_count_lines(tchar **contents_p, size_t *nchars_p)
648 {
649         ssize_t nlines = 0;
650         tchar *contents = *contents_p;
651         size_t nchars = *nchars_p;
652         size_t i;
653
654         for (i = 0; i < nchars; i++)
655                 if (contents[i] == T('\n'))
656                         nlines++;
657
658         /* Handle last line not terminated by a newline */
659         if (nchars != 0 && contents[nchars - 1] != T('\n')) {
660                 contents = realloc(contents, (nchars + 1) * sizeof(tchar));
661                 if (!contents) {
662                         imagex_error(T("Out of memory!"));
663                         return -1;
664                 }
665                 contents[nchars] = T('\n');
666                 *contents_p = contents;
667                 nchars++;
668                 nlines++;
669         }
670         *nchars_p = nchars;
671         return nlines;
672 }
673
674 /* Parses a file in the source list format.  (See the man page for
675  * 'wimlib-imagex capture' for details on this format and the meaning.)
676  *
677  * @source_list_contents:  Contents of the source list file.  Note that this
678  *                         buffer will be modified to save memory allocations,
679  *                         and cannot be freed until the returned array of
680  *                         wimlib_capture_source's has also been freed.
681  *
682  * @source_list_nbytes:    Number of bytes of data in the @source_list_contents
683  *                         buffer.
684  *
685  * @nsources_ret:          On success, the length of the returned array is
686  *                         returned here.
687  *
688  * Returns:   An array of `struct wimlib_capture_source's that can be passed to
689  * the wimlib_add_image_multisource() function to specify how a WIM image is to
690  * be created.  */
691 static struct wimlib_capture_source *
692 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
693                   size_t *nsources_ret)
694 {
695         ssize_t nlines;
696         tchar *p;
697         struct wimlib_capture_source *sources;
698         size_t i, j;
699
700         nlines = text_file_count_lines(source_list_contents_p,
701                                        &source_list_nchars);
702         if (nlines < 0)
703                 return NULL;
704
705         /* Always allocate at least 1 slot, just in case the implementation of
706          * calloc() returns NULL if 0 bytes are requested. */
707         sources = calloc(nlines ?: 1, sizeof(*sources));
708         if (!sources) {
709                 imagex_error(T("out of memory"));
710                 return NULL;
711         }
712         p = *source_list_contents_p;
713         j = 0;
714         for (i = 0; i < nlines; i++) {
715                 /* XXX: Could use rawmemchr() here instead, but it may not be
716                  * available on all platforms. */
717                 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
718                 size_t len = endp - p + 1;
719                 *endp = T('\0');
720                 if (!is_comment_line(p, len)) {
721                         if (!parse_source_list_line(p, len, &sources[j++])) {
722                                 free(sources);
723                                 return NULL;
724                         }
725                 }
726                 p = endp + 1;
727
728         }
729         *nsources_ret = j;
730         return sources;
731 }
732
733
734 enum capture_config_section {
735         CAPTURE_CONFIG_NO_SECTION,
736         CAPTURE_CONFIG_EXCLUSION_SECTION,
737         CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION,
738         CAPTURE_CONFIG_IGNORE_SECTION,
739 };
740
741 enum {
742         CAPTURE_CONFIG_INVALID_SECTION,
743         CAPTURE_CONFIG_CHANGED_SECTION,
744         CAPTURE_CONFIG_SAME_SECTION,
745 };
746
747 static int
748 check_config_section(tchar *line, size_t len,
749                      enum capture_config_section *cur_section)
750 {
751         while (istspace(*line))
752                 line++;
753
754         if (*line != T('['))
755                 return CAPTURE_CONFIG_SAME_SECTION;
756
757         line++;
758         tchar *endbrace = tstrrchr(line, T(']'));
759         if (!endbrace)
760                 return CAPTURE_CONFIG_SAME_SECTION;
761
762         if (!tmemcmp(line, T("ExclusionList"), endbrace - line)) {
763                 *cur_section = CAPTURE_CONFIG_EXCLUSION_SECTION;
764         } else if (!tmemcmp(line, T("ExclusionException"), endbrace - line)) {
765                 *cur_section = CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION;
766         } else if (!tmemcmp(line, T("CompressionExclusionList"), endbrace - line)) {
767                 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
768                 tfputs(T("WARNING: Ignoring [CompressionExclusionList] section "
769                          "of capture config file\n"),
770                        stderr);
771         } else if (!tmemcmp(line, T("AlignmentList"), endbrace - line)) {
772                 *cur_section = CAPTURE_CONFIG_IGNORE_SECTION;
773                 tfputs(T("WARNING: Ignoring [AlignmentList] section "
774                          "of capture config file\n"),
775                        stderr);
776         } else {
777                 imagex_error(T("Invalid capture config file section \"%"TS"\""),
778                              line - 1);
779                 return CAPTURE_CONFIG_INVALID_SECTION;
780         }
781         return CAPTURE_CONFIG_CHANGED_SECTION;
782 }
783
784
785 static bool
786 pattern_list_add_pattern(struct wimlib_pattern_list *pat_list,
787                          tchar *pat)
788 {
789         if (pat_list->num_pats == pat_list->num_allocated_pats) {
790                 tchar **pats;
791                 size_t num_allocated_pats = pat_list->num_pats + 8;
792
793                 pats = realloc(pat_list->pats,
794                                num_allocated_pats * sizeof(pat_list->pats[0]));
795                 if (!pats) {
796                         imagex_error(T("Out of memory!"));
797                         return false;
798                 }
799                 pat_list->pats = pats;
800                 pat_list->num_allocated_pats = num_allocated_pats;
801         }
802         pat_list->pats[pat_list->num_pats++] = pat;
803         return true;
804 }
805
806 static bool
807 parse_capture_config_line(tchar *line, size_t len,
808                           enum capture_config_section *cur_section,
809                           struct wimlib_capture_config *config)
810 {
811         tchar *filename;
812         int ret;
813
814         ret = check_config_section(line, len, cur_section);
815         if (ret == CAPTURE_CONFIG_INVALID_SECTION)
816                 return false;
817         if (ret == CAPTURE_CONFIG_CHANGED_SECTION)
818                 return true;
819
820         switch (*cur_section) {
821         case CAPTURE_CONFIG_NO_SECTION:
822                 imagex_error(T("Line \"%"TS"\" is not in a section "
823                                "(such as [ExclusionList]"), line);
824                 return false;
825         case CAPTURE_CONFIG_EXCLUSION_SECTION:
826                 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
827                         return false;
828                 return pattern_list_add_pattern(&config->exclusion_pats,
829                                                 filename);
830         case CAPTURE_CONFIG_EXCLUSION_EXCEPTION_SECTION:
831                 if (parse_string(&line, &len, &filename) != PARSE_STRING_SUCCESS)
832                         return false;
833                 return pattern_list_add_pattern(&config->exclusion_exception_pats,
834                                                 filename);
835         case CAPTURE_CONFIG_IGNORE_SECTION:
836                 return true;
837         }
838         return false;
839 }
840
841 static int
842 parse_capture_config(tchar **contents_p, size_t nchars,
843                      struct wimlib_capture_config *config)
844 {
845         ssize_t nlines;
846         tchar *p;
847         size_t i;
848         enum capture_config_section cur_section;
849
850         memset(config, 0, sizeof(*config));
851
852         nlines = text_file_count_lines(contents_p, &nchars);
853         if (nlines < 0)
854                 return -1;
855
856         cur_section = CAPTURE_CONFIG_NO_SECTION;
857         p = *contents_p;
858         for (i = 0; i < nlines; i++) {
859                 tchar *endp = tmemchr(p, T('\n'), nchars);
860                 size_t len = endp - p + 1;
861                 *endp = T('\0');
862                 if (!is_comment_line(p, len))
863                         if (!parse_capture_config_line(p, len, &cur_section, config))
864                                 return -1;
865                 p = endp + 1;
866
867         }
868         return 0;
869 }
870
871 /* Reads the contents of a file into memory. */
872 static char *
873 file_get_contents(const tchar *filename, size_t *len_ret)
874 {
875         struct stat stbuf;
876         void *buf = NULL;
877         size_t len;
878         FILE *fp;
879
880         if (tstat(filename, &stbuf) != 0) {
881                 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
882                 goto out;
883         }
884         len = stbuf.st_size;
885
886         fp = tfopen(filename, T("rb"));
887         if (!fp) {
888                 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
889                 goto out;
890         }
891
892         buf = malloc(len ? len : 1);
893         if (!buf) {
894                 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
895                                "contents of file \"%"TS"\""), len, filename);
896                 goto out_fclose;
897         }
898         if (fread(buf, 1, len, fp) != len) {
899                 imagex_error_with_errno(T("Failed to read %zu bytes from the "
900                                           "file \"%"TS"\""), len, filename);
901                 goto out_free_buf;
902         }
903         *len_ret = len;
904         goto out_fclose;
905 out_free_buf:
906         free(buf);
907         buf = NULL;
908 out_fclose:
909         fclose(fp);
910 out:
911         return buf;
912 }
913
914 /* Read standard input until EOF and return the full contents in a malloc()ed
915  * buffer and the number of bytes of data in @len_ret.  Returns NULL on read
916  * error. */
917 static char *
918 stdin_get_contents(size_t *len_ret)
919 {
920         /* stdin can, of course, be a pipe or other non-seekable file, so the
921          * total length of the data cannot be pre-determined */
922         char *buf = NULL;
923         size_t newlen = 1024;
924         size_t pos = 0;
925         size_t inc = 1024;
926         for (;;) {
927                 char *p = realloc(buf, newlen);
928                 size_t bytes_read, bytes_to_read;
929                 if (!p) {
930                         imagex_error(T("out of memory while reading stdin"));
931                         break;
932                 }
933                 buf = p;
934                 bytes_to_read = newlen - pos;
935                 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
936                 pos += bytes_read;
937                 if (bytes_read != bytes_to_read) {
938                         if (feof(stdin)) {
939                                 *len_ret = pos;
940                                 return buf;
941                         } else {
942                                 imagex_error_with_errno(T("error reading stdin"));
943                                 break;
944                         }
945                 }
946                 newlen += inc;
947                 inc *= 3;
948                 inc /= 2;
949         }
950         free(buf);
951         return NULL;
952 }
953
954
955 static tchar *
956 translate_text_to_tstr(char *text, size_t num_bytes, size_t *num_tchars_ret)
957 {
958 #ifndef __WIN32__
959         /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
960          * */
961         *num_tchars_ret = num_bytes;
962         return text;
963 #else /* !__WIN32__ */
964         /* On Windows, translate the text to UTF-16LE */
965         wchar_t *text_wstr;
966         size_t num_wchars;
967
968         if (num_bytes >= 2 &&
969             (((unsigned char)text[0] == 0xff && (unsigned char)text[1] == 0xfe) ||
970              ((unsigned char)text[0] <= 0x7f && (unsigned char)text[1] == 0x00)))
971         {
972                 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
973                  * with something that looks like an ASCII character encoded as
974                  * a UTF-16LE code unit.  Assume the file is encoded as
975                  * UTF-16LE.  This is not a 100% reliable check. */
976                 num_wchars = num_bytes / 2;
977                 text_wstr = (wchar_t*)text;
978         } else {
979                 /* File does not look like UTF-16LE.  Assume it is encoded in
980                  * the current Windows code page.  I think these are always
981                  * ASCII-compatible, so any so-called "plain-text" (ASCII) files
982                  * should work as expected. */
983                 text_wstr = win32_mbs_to_wcs(text,
984                                              num_bytes,
985                                              &num_wchars);
986                 free(text);
987         }
988         *num_tchars_ret = num_wchars;
989         return text_wstr;
990 #endif /* __WIN32__ */
991 }
992
993 static tchar *
994 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
995 {
996         char *contents;
997         size_t num_bytes;
998
999         contents = file_get_contents(filename, &num_bytes);
1000         if (!contents)
1001                 return NULL;
1002         return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
1003 }
1004
1005 static tchar *
1006 stdin_get_text_contents(size_t *num_tchars_ret)
1007 {
1008         char *contents;
1009         size_t num_bytes;
1010
1011         contents = stdin_get_contents(&num_bytes);
1012         if (!contents)
1013                 return NULL;
1014         return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
1015 }
1016
1017 #define TO_PERCENT(numerator, denominator) \
1018         (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
1019
1020 /* Given an enumerated value for WIM compression type, return a descriptive
1021  * string. */
1022 static const tchar *
1023 get_data_type(int ctype)
1024 {
1025         switch (ctype) {
1026         case WIMLIB_COMPRESSION_TYPE_NONE:
1027                 return T("uncompressed");
1028         case WIMLIB_COMPRESSION_TYPE_LZX:
1029                 return T("LZX-compressed");
1030         case WIMLIB_COMPRESSION_TYPE_XPRESS:
1031                 return T("XPRESS-compressed");
1032         }
1033         return NULL;
1034 }
1035
1036 #define GIBIBYTE_MIN_NBYTES 10000000000ULL
1037 #define MEBIBYTE_MIN_NBYTES 10000000ULL
1038 #define KIBIBYTE_MIN_NBYTES 10000ULL
1039
1040 static unsigned
1041 get_unit(uint64_t total_bytes, const tchar **name_ret)
1042 {
1043         if (total_bytes >= GIBIBYTE_MIN_NBYTES) {
1044                 *name_ret = T("GiB");
1045                 return 30;
1046         } else if (total_bytes >= MEBIBYTE_MIN_NBYTES) {
1047                 *name_ret = T("MiB");
1048                 return 20;
1049         } else if (total_bytes >= KIBIBYTE_MIN_NBYTES) {
1050                 *name_ret = T("KiB");
1051                 return 10;
1052         } else {
1053                 *name_ret = T("bytes");
1054                 return 0;
1055         }
1056 }
1057
1058 /* Progress callback function passed to various wimlib functions. */
1059 static int
1060 imagex_progress_func(enum wimlib_progress_msg msg,
1061                      const union wimlib_progress_info *info)
1062 {
1063         unsigned percent_done;
1064         unsigned unit_shift;
1065         const tchar *unit_name;
1066         if (imagex_be_quiet)
1067                 return 0;
1068         switch (msg) {
1069         case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
1070                 unit_shift = get_unit(info->write_streams.total_bytes, &unit_name);
1071                 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
1072                                           info->write_streams.total_bytes);
1073
1074                 if (info->write_streams.completed_streams == 0) {
1075                         const tchar *data_type;
1076
1077                         data_type = get_data_type(info->write_streams.compression_type);
1078                         imagex_printf(T("Writing %"TS" data using %u thread%"TS"\n"),
1079                                 data_type, info->write_streams.num_threads,
1080                                 (info->write_streams.num_threads == 1) ? T("") : T("s"));
1081                 }
1082                 if (info->write_streams.total_parts <= 1) {
1083                         imagex_printf(T("\r%"PRIu64" %"TS" of %"PRIu64" %"TS" (uncompressed) "
1084                                 "written (%u%% done)"),
1085                                 info->write_streams.completed_bytes >> unit_shift,
1086                                 unit_name,
1087                                 info->write_streams.total_bytes >> unit_shift,
1088                                 unit_name,
1089                                 percent_done);
1090                 } else {
1091                         imagex_printf(T("\rWriting resources from part %u of %u: "
1092                                   "%"PRIu64 " %"TS" of %"PRIu64" %"TS" (%u%%) written"),
1093                                 (info->write_streams.completed_parts ==
1094                                         info->write_streams.total_parts) ?
1095                                                 info->write_streams.completed_parts :
1096                                                 info->write_streams.completed_parts + 1,
1097                                 info->write_streams.total_parts,
1098                                 info->write_streams.completed_bytes >> unit_shift,
1099                                 unit_name,
1100                                 info->write_streams.total_bytes >> unit_shift,
1101                                 unit_name,
1102                                 percent_done);
1103                 }
1104                 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
1105                         imagex_printf(T("\n"));
1106                 break;
1107         case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
1108                 imagex_printf(T("Scanning \"%"TS"\""), info->scan.source);
1109                 if (*info->scan.wim_target_path) {
1110                         imagex_printf(T(" (loading as WIM path: "
1111                                   "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\")...\n"),
1112                                info->scan.wim_target_path);
1113                 } else {
1114                         imagex_printf(T(" (loading as root of WIM image)...\n"));
1115                 }
1116                 break;
1117         case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
1118                 switch (info->scan.status) {
1119                 case WIMLIB_SCAN_DENTRY_OK:
1120                         imagex_printf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
1121                         break;
1122                 case WIMLIB_SCAN_DENTRY_EXCLUDED:
1123                         imagex_printf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
1124                         break;
1125                 case WIMLIB_SCAN_DENTRY_UNSUPPORTED:
1126                         imagex_printf(T("WARNING: Excluding unsupported file or directory\n"
1127                                         "         \"%"TS"\" from capture\n"), info->scan.cur_path);
1128                         break;
1129                 }
1130                 break;
1131         case WIMLIB_PROGRESS_MSG_VERIFY_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("\rVerifying integrity of \"%"TS"\": %"PRIu64" %"TS" "
1136                         "of %"PRIu64" %"TS" (%u%%) done"),
1137                         info->integrity.filename,
1138                         info->integrity.completed_bytes >> unit_shift,
1139                         unit_name,
1140                         info->integrity.total_bytes >> unit_shift,
1141                         unit_name,
1142                         percent_done);
1143                 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1144                         imagex_printf(T("\n"));
1145                 break;
1146         case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
1147                 unit_shift = get_unit(info->integrity.total_bytes, &unit_name);
1148                 percent_done = TO_PERCENT(info->integrity.completed_bytes,
1149                                           info->integrity.total_bytes);
1150                 imagex_printf(T("\rCalculating integrity table for WIM: %"PRIu64" %"TS" "
1151                           "of %"PRIu64" %"TS" (%u%%) done"),
1152                         info->integrity.completed_bytes >> unit_shift,
1153                         unit_name,
1154                         info->integrity.total_bytes >> unit_shift,
1155                         unit_name,
1156                         percent_done);
1157                 if (info->integrity.completed_bytes == info->integrity.total_bytes)
1158                         imagex_printf(T("\n"));
1159                 break;
1160         case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
1161                 imagex_printf(T("Applying image %d (\"%"TS"\") from \"%"TS"\" "
1162                           "to %"TS" \"%"TS"\"\n"),
1163                         info->extract.image,
1164                         info->extract.image_name,
1165                         info->extract.wimfile_name,
1166                         ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
1167                          T("NTFS volume") : T("directory")),
1168                         info->extract.target);
1169                 break;
1170         case WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN:
1171                 imagex_printf(T("Extracting "
1172                           "\""WIMLIB_WIM_PATH_SEPARATOR_STRING"%"TS"\" from image %d (\"%"TS"\") "
1173                           "in \"%"TS"\" to \"%"TS"\"\n"),
1174                         info->extract.extract_root_wim_source_path,
1175                         info->extract.image,
1176                         info->extract.image_name,
1177                         info->extract.wimfile_name,
1178                         info->extract.target);
1179                 break;
1180         case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
1181                 percent_done = TO_PERCENT(info->extract.completed_bytes,
1182                                           info->extract.total_bytes);
1183                 unit_shift = get_unit(info->extract.total_bytes, &unit_name);
1184                 imagex_printf(T("\rExtracting files: "
1185                           "%"PRIu64" %"TS" of %"PRIu64" %"TS" (%u%%) done"),
1186                         info->extract.completed_bytes >> unit_shift,
1187                         unit_name,
1188                         info->extract.total_bytes >> unit_shift,
1189                         unit_name,
1190                         percent_done);
1191                 if (info->extract.completed_bytes >= info->extract.total_bytes)
1192                         imagex_printf(T("\n"));
1193                 break;
1194         case WIMLIB_PROGRESS_MSG_EXTRACT_SPWM_PART_BEGIN:
1195                 if (info->extract.total_parts != 1) {
1196                         imagex_printf(T("\nReading split pipable WIM part %u of %u\n"),
1197                                       info->extract.part_number,
1198                                       info->extract.total_parts);
1199                 }
1200                 break;
1201         case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
1202                 if (info->extract.extract_root_wim_source_path[0] == T('\0'))
1203                         imagex_printf(T("Setting timestamps on all extracted files...\n"));
1204                 break;
1205         case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
1206                 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
1207                         imagex_printf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
1208                                 info->extract.target);
1209                 }
1210                 break;
1211         case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
1212                 percent_done = TO_PERCENT(info->split.completed_bytes,
1213                                           info->split.total_bytes);
1214                 unit_shift = get_unit(info->split.total_bytes, &unit_name);
1215                 imagex_printf(T("Writing \"%"TS"\" (part %u of %u): %"PRIu64" %"TS" of "
1216                           "%"PRIu64" %"TS" (%u%%) written\n"),
1217                         info->split.part_name,
1218                         info->split.cur_part_number,
1219                         info->split.total_parts,
1220                         info->split.completed_bytes >> unit_shift,
1221                         unit_name,
1222                         info->split.total_bytes >> unit_shift,
1223                         unit_name,
1224                         percent_done);
1225                 break;
1226         case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
1227                 if (info->split.completed_bytes == info->split.total_bytes) {
1228                         imagex_printf(T("Finished writing split WIM part %u of %u\n"),
1229                                 info->split.cur_part_number,
1230                                 info->split.total_parts);
1231                 }
1232                 break;
1233         case WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND:
1234                 switch (info->update.command->op) {
1235                 case WIMLIB_UPDATE_OP_DELETE:
1236                         imagex_printf(T("Deleted WIM path "
1237                                   "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1238                                 info->update.command->delete_.wim_path);
1239                         break;
1240                 case WIMLIB_UPDATE_OP_RENAME:
1241                         imagex_printf(T("Renamed WIM path "
1242                                   "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\" => "
1243                                   "\""WIMLIB_WIM_PATH_SEPARATOR_STRING "%"TS"\"\n"),
1244                                 info->update.command->rename.wim_source_path,
1245                                 info->update.command->rename.wim_target_path);
1246                         break;
1247                 case WIMLIB_UPDATE_OP_ADD:
1248                 default:
1249                         break;
1250                 }
1251                 break;
1252         default:
1253                 break;
1254         }
1255         fflush(imagex_info_file);
1256         return 0;
1257 }
1258
1259 static unsigned
1260 parse_num_threads(const tchar *optarg)
1261 {
1262         tchar *tmp;
1263         unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
1264         if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
1265                 imagex_error(T("Number of threads must be a non-negative integer!"));
1266                 return UINT_MAX;
1267         } else {
1268                 return ul_nthreads;
1269         }
1270 }
1271
1272 /*
1273  * Parse an option passed to an update command.
1274  *
1275  * @op:         One of WIMLIB_UPDATE_OP_* that indicates the command being
1276  *              parsed.
1277  *
1278  * @option:     Text string for the option (beginning with --)
1279  *
1280  * @cmd:        `struct wimlib_update_command' that is being constructed for
1281  *              this command.
1282  *
1283  * Returns true if the option was recognized; false if not.
1284  */
1285 static bool
1286 update_command_add_option(int op, const tchar *option,
1287                           struct wimlib_update_command *cmd)
1288 {
1289         bool recognized = true;
1290         switch (op) {
1291         case WIMLIB_UPDATE_OP_ADD:
1292                 if (!tstrcmp(option, T("--verbose")))
1293                         cmd->add.add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
1294                 else if (!tstrcmp(option, T("--unix-data")))
1295                         cmd->add.add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
1296                 else if (!tstrcmp(option, T("--no-acls")) || !tstrcmp(option, T("--noacls")))
1297                         cmd->add.add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
1298                 else if (!tstrcmp(option, T("--strict-acls")))
1299                         cmd->add.add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
1300                 else if (!tstrcmp(option, T("--dereference")))
1301                         cmd->add.add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
1302                 else
1303                         recognized = false;
1304                 break;
1305         case WIMLIB_UPDATE_OP_DELETE:
1306                 if (!tstrcmp(option, T("--force")))
1307                         cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
1308                 else if (!tstrcmp(option, T("--recursive")))
1309                         cmd->delete_.delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
1310                 else
1311                         recognized = false;
1312                 break;
1313         default:
1314                 recognized = false;
1315                 break;
1316         }
1317         return recognized;
1318 }
1319
1320 /* How many nonoption arguments each `imagex update' command expects */
1321 static const unsigned update_command_num_nonoptions[] = {
1322         [WIMLIB_UPDATE_OP_ADD] = 2,
1323         [WIMLIB_UPDATE_OP_DELETE] = 1,
1324         [WIMLIB_UPDATE_OP_RENAME] = 2,
1325 };
1326
1327 static void
1328 update_command_add_nonoption(int op, const tchar *nonoption,
1329                              struct wimlib_update_command *cmd,
1330                              unsigned num_nonoptions)
1331 {
1332         switch (op) {
1333         case WIMLIB_UPDATE_OP_ADD:
1334                 if (num_nonoptions == 0)
1335                         cmd->add.fs_source_path = (tchar*)nonoption;
1336                 else
1337                         cmd->add.wim_target_path = (tchar*)nonoption;
1338                 break;
1339         case WIMLIB_UPDATE_OP_DELETE:
1340                 cmd->delete_.wim_path = (tchar*)nonoption;
1341                 break;
1342         case WIMLIB_UPDATE_OP_RENAME:
1343                 if (num_nonoptions == 0)
1344                         cmd->rename.wim_source_path = (tchar*)nonoption;
1345                 else
1346                         cmd->rename.wim_target_path = (tchar*)nonoption;
1347                 break;
1348         }
1349 }
1350
1351 /*
1352  * Parse a command passed on stdin to `imagex update'.
1353  *
1354  * @line:       Text of the command.
1355  * @len:        Length of the line, including a null terminator
1356  *              at line[len - 1].
1357  *
1358  * @command:    A `struct wimlib_update_command' to fill in from the parsed
1359  *              line.
1360  *
1361  * @line_number: Line number of the command, for diagnostics.
1362  *
1363  * Returns true on success; returns false on parse error.
1364  */
1365 static bool
1366 parse_update_command(tchar *line, size_t len,
1367                      struct wimlib_update_command *command,
1368                      size_t line_number)
1369 {
1370         int ret;
1371         tchar *command_name;
1372         int op;
1373         size_t num_nonoptions;
1374
1375         /* Get the command name ("add", "delete", "rename") */
1376         ret = parse_string(&line, &len, &command_name);
1377         if (ret != PARSE_STRING_SUCCESS)
1378                 return false;
1379
1380         if (!tstrcasecmp(command_name, T("add"))) {
1381                 op = WIMLIB_UPDATE_OP_ADD;
1382         } else if (!tstrcasecmp(command_name, T("delete"))) {
1383                 op = WIMLIB_UPDATE_OP_DELETE;
1384         } else if (!tstrcasecmp(command_name, T("rename"))) {
1385                 op = WIMLIB_UPDATE_OP_RENAME;
1386         } else {
1387                 imagex_error(T("Unknown update command \"%"TS"\" on line %zu"),
1388                              command_name, line_number);
1389                 return false;
1390         }
1391         command->op = op;
1392
1393         /* Parse additional options and non-options as needed */
1394         num_nonoptions = 0;
1395         for (;;) {
1396                 tchar *next_string;
1397
1398                 ret = parse_string(&line, &len, &next_string);
1399                 if (ret == PARSE_STRING_NONE) /* End of line */
1400                         break;
1401                 else if (ret != PARSE_STRING_SUCCESS) /* Parse failure */
1402                         return false;
1403                 if (next_string[0] == T('-') && next_string[1] == T('-')) {
1404                         /* Option */
1405                         if (!update_command_add_option(op, next_string, command))
1406                         {
1407                                 imagex_error(T("Unrecognized option \"%"TS"\" to "
1408                                                "update command \"%"TS"\" on line %zu"),
1409                                              next_string, command_name, line_number);
1410
1411                                 return false;
1412                         }
1413                 } else {
1414                         /* Nonoption */
1415                         if (num_nonoptions == update_command_num_nonoptions[op])
1416                         {
1417                                 imagex_error(T("Unexpected argument \"%"TS"\" in "
1418                                                "update command on line %zu\n"
1419                                                "       (The \"%"TS"\" command only "
1420                                                "takes %zu nonoption arguments!)\n"),
1421                                              next_string, line_number,
1422                                              command_name, num_nonoptions);
1423                                 return false;
1424                         }
1425                         update_command_add_nonoption(op, next_string,
1426                                                      command, num_nonoptions);
1427                         num_nonoptions++;
1428                 }
1429         }
1430
1431         if (num_nonoptions != update_command_num_nonoptions[op]) {
1432                 imagex_error(T("Not enough arguments to update command "
1433                                "\"%"TS"\" on line %zu"), command_name, line_number);
1434                 return false;
1435         }
1436         return true;
1437 }
1438
1439 static struct wimlib_update_command *
1440 parse_update_command_file(tchar **cmd_file_contents_p, size_t cmd_file_nchars,
1441                           size_t *num_cmds_ret)
1442 {
1443         ssize_t nlines;
1444         tchar *p;
1445         struct wimlib_update_command *cmds;
1446         size_t i, j;
1447
1448         nlines = text_file_count_lines(cmd_file_contents_p,
1449                                        &cmd_file_nchars);
1450         if (nlines < 0)
1451                 return NULL;
1452
1453         /* Always allocate at least 1 slot, just in case the implementation of
1454          * calloc() returns NULL if 0 bytes are requested. */
1455         cmds = calloc(nlines ?: 1, sizeof(struct wimlib_update_command));
1456         if (!cmds) {
1457                 imagex_error(T("out of memory"));
1458                 return NULL;
1459         }
1460         p = *cmd_file_contents_p;
1461         j = 0;
1462         for (i = 0; i < nlines; i++) {
1463                 /* XXX: Could use rawmemchr() here instead, but it may not be
1464                  * available on all platforms. */
1465                 tchar *endp = tmemchr(p, T('\n'), cmd_file_nchars);
1466                 size_t len = endp - p + 1;
1467                 *endp = T('\0');
1468                 if (!is_comment_line(p, len)) {
1469                         if (!parse_update_command(p, len, &cmds[j++], i + 1)) {
1470                                 free(cmds);
1471                                 return NULL;
1472                         }
1473                 }
1474                 p = endp + 1;
1475         }
1476         *num_cmds_ret = j;
1477         return cmds;
1478 }
1479
1480 /* Apply one image, or all images, from a WIM file into a directory, OR apply
1481  * one image from a WIM file to a NTFS volume.  */
1482 static int
1483 imagex_apply(int argc, tchar **argv, int cmd)
1484 {
1485         int c;
1486         int open_flags = 0;
1487         int image = WIMLIB_NO_IMAGE;
1488         WIMStruct *wim;
1489         struct wimlib_wim_info info;
1490         int ret;
1491         const tchar *wimfile;
1492         const tchar *target;
1493         const tchar *image_num_or_name = NULL;
1494         int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
1495
1496         STRING_SET(refglobs);
1497
1498         for_opt(c, apply_options) {
1499                 switch (c) {
1500                 case IMAGEX_CHECK_OPTION:
1501                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1502                         break;
1503                 case IMAGEX_HARDLINK_OPTION:
1504                         extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1505                         break;
1506                 case IMAGEX_SYMLINK_OPTION:
1507                         extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1508                         break;
1509                 case IMAGEX_VERBOSE_OPTION:
1510                         extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
1511                         break;
1512                 case IMAGEX_REF_OPTION:
1513                         ret = string_set_append(&refglobs, optarg);
1514                         if (ret)
1515                                 goto out_free_refglobs;
1516                         break;
1517                 case IMAGEX_UNIX_DATA_OPTION:
1518                         extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1519                         break;
1520                 case IMAGEX_NO_ACLS_OPTION:
1521                         extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1522                         break;
1523                 case IMAGEX_STRICT_ACLS_OPTION:
1524                         extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1525                         break;
1526                 case IMAGEX_NORPFIX_OPTION:
1527                         extract_flags |= WIMLIB_EXTRACT_FLAG_NORPFIX;
1528                         break;
1529                 case IMAGEX_RPFIX_OPTION:
1530                         extract_flags |= WIMLIB_EXTRACT_FLAG_RPFIX;
1531                         break;
1532                 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
1533                         extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
1534                         extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
1535                         break;
1536                 case IMAGEX_RESUME_OPTION:
1537                         extract_flags |= WIMLIB_EXTRACT_FLAG_RESUME;
1538                         break;
1539                 default:
1540                         goto out_usage;
1541                 }
1542         }
1543         argc -= optind;
1544         argv += optind;
1545         if (argc != 2 && argc != 3)
1546                 goto out_usage;
1547
1548         wimfile = argv[0];
1549
1550         if (!tstrcmp(wimfile, T("-"))) {
1551                 /* Attempt to apply pipable WIM from standard input.  */
1552                 if (argc == 2) {
1553                         image_num_or_name = NULL;
1554                         target = argv[1];
1555                 } else {
1556                         image_num_or_name = argv[1];
1557                         target = argv[2];
1558                 }
1559                 wim = NULL;
1560         } else {
1561                 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1562                                       imagex_progress_func);
1563                 if (ret)
1564                         goto out_free_refglobs;
1565
1566                 wimlib_get_wim_info(wim, &info);
1567
1568                 if (argc >= 3) {
1569                         /* Image explicitly specified.  */
1570                         image_num_or_name = argv[1];
1571                         image = wimlib_resolve_image(wim, image_num_or_name);
1572                         ret = verify_image_exists(image, image_num_or_name, wimfile);
1573                         if (ret)
1574                                 goto out_wimlib_free;
1575                         target = argv[2];
1576                 } else {
1577                         /* No image specified; default to image 1, but only if the WIM
1578                          * contains exactly one image.  */
1579
1580                         if (info.image_count != 1) {
1581                                 imagex_error(T("\"%"TS"\" contains %d images; "
1582                                                "Please select one (or all)."),
1583                                              wimfile, info.image_count);
1584                                 wimlib_free(wim);
1585                                 goto out_usage;
1586                         }
1587                         image = 1;
1588                         target = argv[1];
1589                 }
1590         }
1591
1592         if (refglobs.num_strings) {
1593                 if (wim == NULL) {
1594                         imagex_error(T("Can't specify --ref when applying from stdin!"));
1595                         ret = -1;
1596                         goto out_wimlib_free;
1597                 }
1598                 ret = wim_reference_globs(wim, &refglobs, open_flags);
1599                 if (ret)
1600                         goto out_wimlib_free;
1601         }
1602
1603 #ifndef __WIN32__
1604         {
1605                 /* Interpret a regular file or block device target as a NTFS
1606                  * volume.  */
1607                 struct stat stbuf;
1608
1609                 if (tstat(target, &stbuf)) {
1610                         if (errno != ENOENT) {
1611                                 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1612                                                         target);
1613                                 ret = -1;
1614                                 goto out_wimlib_free;
1615                         }
1616                 } else {
1617                         if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1618                                 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1619                 }
1620         }
1621 #endif
1622
1623         if (wim) {
1624                 ret = wimlib_extract_image(wim, image, target, extract_flags,
1625                                            imagex_progress_func);
1626         } else {
1627                 set_fd_to_binary_mode(STDIN_FILENO);
1628                 ret = wimlib_extract_image_from_pipe(STDIN_FILENO,
1629                                                      image_num_or_name,
1630                                                      target, extract_flags,
1631                                                      imagex_progress_func);
1632         }
1633         if (ret == 0) {
1634                 imagex_printf(T("Done applying WIM image.\n"));
1635         } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
1636                 if (wim) {
1637                         do_resource_not_found_warning(wimfile, &info, &refglobs);
1638                 } else {
1639                         imagex_error(T(        "If you are applying an image "
1640                                                "from a split pipable WIM,\n"
1641                                        "       make sure you have "
1642                                        "concatenated together all parts."));
1643                 }
1644         }
1645 out_wimlib_free:
1646         wimlib_free(wim);
1647 out_free_refglobs:
1648         string_set_destroy(&refglobs);
1649         return ret;
1650
1651 out_usage:
1652         usage(CMD_APPLY, stderr);
1653         ret = -1;
1654         goto out_free_refglobs;
1655 }
1656
1657 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1658  * directory trees.  'wimlib-imagex capture': create a new WIM file containing
1659  * the desired image.  'wimlib-imagex append': add a new image to an existing
1660  * WIM file. */
1661 static int
1662 imagex_capture_or_append(int argc, tchar **argv, int cmd)
1663 {
1664         int c;
1665         int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
1666         int add_image_flags = WIMLIB_ADD_IMAGE_FLAG_EXCLUDE_VERBOSE |
1667                               WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1668         int write_flags = 0;
1669         int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
1670         const tchar *wimfile;
1671         int wim_fd;
1672         const tchar *name;
1673         const tchar *desc;
1674         const tchar *flags_element = NULL;
1675
1676         WIMStruct *wim;
1677         STRING_SET(base_wimfiles);
1678         WIMStruct **base_wims;
1679
1680         WIMStruct *template_wim;
1681         const tchar *template_wimfile = NULL;
1682         const tchar *template_image_name_or_num = NULL;
1683         int template_image = WIMLIB_NO_IMAGE;
1684
1685         int ret;
1686         unsigned num_threads = 0;
1687
1688         tchar *source;
1689         tchar *source_copy;
1690
1691         const tchar *config_file = NULL;
1692         tchar *config_str;
1693         struct wimlib_capture_config *config;
1694
1695         bool source_list = false;
1696         size_t source_list_nchars = 0;
1697         tchar *source_list_contents;
1698         bool capture_sources_malloced;
1699         struct wimlib_capture_source *capture_sources;
1700         size_t num_sources;
1701         bool name_defaulted;
1702
1703         for_opt(c, capture_or_append_options) {
1704                 switch (c) {
1705                 case IMAGEX_BOOT_OPTION:
1706                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1707                         break;
1708                 case IMAGEX_CHECK_OPTION:
1709                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1710                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1711                         break;
1712                 case IMAGEX_NOCHECK_OPTION:
1713                         write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
1714                         break;
1715                 case IMAGEX_CONFIG_OPTION:
1716                         config_file = optarg;
1717                         add_image_flags &= ~WIMLIB_ADD_IMAGE_FLAG_WINCONFIG;
1718                         break;
1719                 case IMAGEX_COMPRESS_OPTION:
1720                         compression_type = get_compression_type(optarg);
1721                         if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1722                                 goto out_err;
1723                         break;
1724                 case IMAGEX_COMPRESS_SLOW_OPTION:
1725                         set_compress_slow();
1726                         break;
1727                 case IMAGEX_FLAGS_OPTION:
1728                         flags_element = optarg;
1729                         break;
1730                 case IMAGEX_DEREFERENCE_OPTION:
1731                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1732                         break;
1733                 case IMAGEX_VERBOSE_OPTION:
1734                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1735                         break;
1736                 case IMAGEX_THREADS_OPTION:
1737                         num_threads = parse_num_threads(optarg);
1738                         if (num_threads == UINT_MAX)
1739                                 goto out_err;
1740                         break;
1741                 case IMAGEX_REBUILD_OPTION:
1742                         write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1743                         break;
1744                 case IMAGEX_UNIX_DATA_OPTION:
1745                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1746                         break;
1747                 case IMAGEX_SOURCE_LIST_OPTION:
1748                         source_list = true;
1749                         break;
1750                 case IMAGEX_NO_ACLS_OPTION:
1751                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1752                         break;
1753                 case IMAGEX_STRICT_ACLS_OPTION:
1754                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1755                         break;
1756                 case IMAGEX_RPFIX_OPTION:
1757                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_RPFIX;
1758                         break;
1759                 case IMAGEX_NORPFIX_OPTION:
1760                         add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NORPFIX;
1761                         break;
1762                 case IMAGEX_PIPABLE_OPTION:
1763                         write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1764                         break;
1765                 case IMAGEX_NOT_PIPABLE_OPTION:
1766                         write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
1767                         break;
1768                 case IMAGEX_UPDATE_OF_OPTION:
1769                         if (template_image_name_or_num) {
1770                                 imagex_error(T("'--update-of' can only be "
1771                                                "specified one time!"));
1772                                 goto out_err;
1773                         } else {
1774                                 tchar *colon;
1775                                 colon = tstrrchr(optarg, T(':'));
1776
1777                                 if (colon) {
1778                                         template_wimfile = optarg;
1779                                         *colon = T('\0');
1780                                         template_image_name_or_num = colon + 1;
1781                                 } else {
1782                                         template_wimfile = NULL;
1783                                         template_image_name_or_num = optarg;
1784                                 }
1785                         }
1786                         break;
1787                 case IMAGEX_DELTA_FROM_OPTION:
1788                         if (cmd != CMD_CAPTURE) {
1789                                 imagex_error(T("'--delta-from' is only "
1790                                                "valid for capture!"));
1791                                 goto out_usage;
1792                         }
1793                         ret = string_set_append(&base_wimfiles, optarg);
1794                         if (ret)
1795                                 goto out_free_base_wimfiles;
1796                         write_flags |= WIMLIB_WRITE_FLAG_SKIP_EXTERNAL_WIMS;
1797                         break;
1798                 default:
1799                         goto out_usage;
1800                 }
1801         }
1802         argc -= optind;
1803         argv += optind;
1804
1805         if (argc < 2 || argc > 4)
1806                 goto out_usage;
1807
1808         source = argv[0];
1809         wimfile = argv[1];
1810
1811         /* Set default compression type.  */
1812         if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
1813                 struct wimlib_lzx_params params;
1814                 memset(&params, 0, sizeof(params));
1815                 params.size_of_this = sizeof(params);
1816                 params.algorithm = WIMLIB_LZX_ALGORITHM_FAST;
1817                 params.use_defaults = 1;
1818
1819                 wimlib_lzx_set_default_params(&params);
1820                 compression_type = WIMLIB_COMPRESSION_TYPE_LZX;
1821         }
1822
1823         if (!tstrcmp(wimfile, T("-"))) {
1824                 /* Writing captured WIM to standard output.  */
1825         #if 0
1826                 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
1827                         imagex_error("Can't write a non-pipable WIM to "
1828                                      "standard output!  Specify --pipable\n"
1829                                      "       if you want to create a pipable WIM "
1830                                      "(but read the docs first).");
1831                         goto out_err;
1832                 }
1833         #else
1834                 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
1835         #endif
1836                 if (cmd == CMD_APPEND) {
1837                         imagex_error(T("Using standard output for append does "
1838                                        "not make sense."));
1839                         goto out_err;
1840                 }
1841                 wim_fd = STDOUT_FILENO;
1842                 wimfile = NULL;
1843                 imagex_info_file = stderr;
1844                 set_fd_to_binary_mode(wim_fd);
1845         }
1846
1847         /* If template image was specified using --update-of=IMAGE rather
1848          * than --update-of=WIMFILE:IMAGE, set the default WIMFILE.  */
1849         if (template_image_name_or_num && !template_wimfile) {
1850                 if (base_wimfiles.num_strings == 1) {
1851                         /* Capturing delta WIM based on single WIM:  default to
1852                          * base WIM.  */
1853                         template_wimfile = base_wimfiles.strings[0];
1854                 } else if (cmd == CMD_APPEND) {
1855                         /* Appending to WIM:  default to WIM being appended to.
1856                          */
1857                         template_wimfile = wimfile;
1858                 } else {
1859                         /* Capturing a normal (non-delta) WIM, so the WIM file
1860                          * *must* be explicitly specified.  */
1861                         if (base_wimfiles.num_strings > 1) {
1862                                 imagex_error(T("For capture of delta WIM "
1863                                                "based on multiple existing "
1864                                                "WIMs,\n"
1865                                                "      '--update-of' must "
1866                                                "specify WIMFILE:IMAGE!"));
1867                         } else {
1868                                 imagex_error(T("For capture of non-delta WIM, "
1869                                                "'--update-of' must specify "
1870                                                "WIMFILE:IMAGE!"));
1871                         }
1872                         goto out_usage;
1873                 }
1874         }
1875
1876         if (argc >= 3) {
1877                 name = argv[2];
1878                 name_defaulted = false;
1879         } else {
1880                 /* Set default name to SOURCE argument, omitting any directory
1881                  * prefixes and trailing slashes.  This requires making a copy
1882                  * of @source.  Leave some free characters at the end in case we
1883                  * append a number to keep the name unique. */
1884                 size_t source_name_len;
1885
1886                 source_name_len = tstrlen(source);
1887                 source_copy = alloca((source_name_len + 1 + 25) * sizeof(tchar));
1888                 name = tbasename(tstrcpy(source_copy, source));
1889                 name_defaulted = true;
1890         }
1891         /* Image description defaults to NULL if not given. */
1892         if (argc >= 4)
1893                 desc = argv[3];
1894         else
1895                 desc = NULL;
1896
1897         if (source_list) {
1898                 /* Set up capture sources in source list mode */
1899                 if (source[0] == T('-') && source[1] == T('\0')) {
1900                         source_list_contents = stdin_get_text_contents(&source_list_nchars);
1901                 } else {
1902                         source_list_contents = file_get_text_contents(source,
1903                                                                       &source_list_nchars);
1904                 }
1905                 if (!source_list_contents)
1906                         goto out_err;
1907
1908                 capture_sources = parse_source_list(&source_list_contents,
1909                                                     source_list_nchars,
1910                                                     &num_sources);
1911                 if (!capture_sources) {
1912                         ret = -1;
1913                         goto out_free_source_list_contents;
1914                 }
1915                 capture_sources_malloced = true;
1916         } else {
1917                 /* Set up capture source in non-source-list mode.  */
1918                 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1919                 capture_sources[0].fs_source_path = source;
1920                 capture_sources[0].wim_target_path = NULL;
1921                 capture_sources[0].reserved = 0;
1922                 num_sources = 1;
1923                 capture_sources_malloced = false;
1924                 source_list_contents = NULL;
1925         }
1926
1927         if (config_file) {
1928                 /* Read and parse capture configuration file.  */
1929                 size_t config_len;
1930
1931                 config_str = file_get_text_contents(config_file, &config_len);
1932                 if (!config_str) {
1933                         ret = -1;
1934                         goto out_free_capture_sources;
1935                 }
1936
1937                 config = alloca(sizeof(*config));
1938                 ret = parse_capture_config(&config_str, config_len, config);
1939                 if (ret)
1940                         goto out_free_config;
1941         } else {
1942                 /* No capture configuration file specified; use default
1943                  * configuration for capturing Windows operating systems.  */
1944                 config = NULL;
1945                 add_image_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
1946         }
1947
1948         /* Open the existing WIM, or create a new one.  */
1949         if (cmd == CMD_APPEND)
1950                 ret = wimlib_open_wim(wimfile, open_flags, &wim,
1951                                       imagex_progress_func);
1952         else
1953                 ret = wimlib_create_new_wim(compression_type, &wim);
1954         if (ret)
1955                 goto out_free_config;
1956
1957 #ifndef __WIN32__
1958         /* Detect if source is regular file or block device and set NTFS volume
1959          * capture mode.  */
1960         if (!source_list) {
1961                 struct stat stbuf;
1962
1963                 if (tstat(source, &stbuf) == 0) {
1964                         if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1965                                 imagex_printf(T("Capturing WIM image from NTFS "
1966                                           "filesystem on \"%"TS"\"\n"), source);
1967                                 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1968                         }
1969                 } else {
1970                         if (errno != ENOENT) {
1971                                 imagex_error_with_errno(T("Failed to stat "
1972                                                           "\"%"TS"\""), source);
1973                                 ret = -1;
1974                                 goto out_free_wim;
1975                         }
1976                 }
1977         }
1978 #endif
1979
1980         /* If the user did not specify an image name, and the basename of the
1981          * source already exists as an image name in the WIM file, append a
1982          * suffix to make it unique. */
1983         if (cmd == CMD_APPEND && name_defaulted) {
1984                 unsigned long conflict_idx;
1985                 tchar *name_end = tstrchr(name, T('\0'));
1986                 for (conflict_idx = 1;
1987                      wimlib_image_name_in_use(wim, name);
1988                      conflict_idx++)
1989                 {
1990                         tsprintf(name_end, T(" (%lu)"), conflict_idx);
1991                 }
1992         }
1993
1994         /* If capturing a delta WIM, reference resources from the base WIMs
1995          * before adding the new image.  */
1996         if (base_wimfiles.num_strings) {
1997                 base_wims = calloc(base_wimfiles.num_strings,
1998                                    sizeof(base_wims[0]));
1999                 if (base_wims == NULL) {
2000                         imagex_error(T("Out of memory!"));
2001                         ret = -1;
2002                         goto out_free_wim;
2003                 }
2004
2005                 for (size_t i = 0; i < base_wimfiles.num_strings; i++) {
2006                         ret = wimlib_open_wim(base_wimfiles.strings[i],
2007                                               open_flags, &base_wims[i],
2008                                               imagex_progress_func);
2009                         if (ret)
2010                                 goto out_free_base_wims;
2011
2012                 }
2013
2014                 ret = wimlib_reference_resources(wim, base_wims,
2015                                                  base_wimfiles.num_strings, 0);
2016                 if (ret)
2017                         goto out_free_base_wims;
2018
2019                 if (base_wimfiles.num_strings == 1) {
2020                         imagex_printf(T("Capturing delta WIM based on \"%"TS"\"\n"),
2021                                       base_wimfiles.strings[0]);
2022                 } else {
2023                         imagex_printf(T("Capturing delta WIM based on %u WIMs\n"),
2024                                       base_wimfiles.num_strings);
2025                 }
2026
2027         } else {
2028                 base_wims = NULL;
2029         }
2030
2031         /* If capturing or appending as an update of an existing (template) image,
2032          * open the WIM if needed and parse the image index.  */
2033         if (template_image_name_or_num) {
2034
2035
2036                 if (base_wimfiles.num_strings == 1 &&
2037                     template_wimfile == base_wimfiles.strings[0]) {
2038                         template_wim = base_wims[0];
2039                 } else if (template_wimfile == wimfile) {
2040                         template_wim = wim;
2041                 } else {
2042                         ret = wimlib_open_wim(template_wimfile, open_flags,
2043                                               &template_wim, imagex_progress_func);
2044                         if (ret)
2045                                 goto out_free_base_wims;
2046                 }
2047
2048                 template_image = wimlib_resolve_image(template_wim,
2049                                                       template_image_name_or_num);
2050
2051                 if (template_image_name_or_num[0] == T('-')) {
2052                         tchar *tmp;
2053                         unsigned long n;
2054                         struct wimlib_wim_info info;
2055
2056                         wimlib_get_wim_info(template_wim, &info);
2057                         n = tstrtoul(template_image_name_or_num + 1, &tmp, 10);
2058                         if (n >= 1 && n <= info.image_count &&
2059                             *tmp == T('\0') &&
2060                             tmp != template_image_name_or_num + 1)
2061                         {
2062                                 template_image = info.image_count - (n - 1);
2063                         }
2064                 }
2065                 ret = verify_image_exists_and_is_single(template_image,
2066                                                         template_image_name_or_num,
2067                                                         template_wimfile);
2068                 if (ret)
2069                         goto out_free_template_wim;
2070         } else {
2071                 template_wim = NULL;
2072         }
2073
2074         ret = wimlib_add_image_multisource(wim,
2075                                            capture_sources,
2076                                            num_sources,
2077                                            name,
2078                                            config,
2079                                            add_image_flags,
2080                                            imagex_progress_func);
2081         if (ret)
2082                 goto out_free_template_wim;
2083
2084         if (desc || flags_element || template_image_name_or_num) {
2085                 /* User provided <DESCRIPTION> or <FLAGS> element, or an image
2086                  * on which the added one is to be based has been specified with
2087                  * --update-of.  Get the index of the image we just
2088                  *  added, then use it to call the appropriate functions.  */
2089                 struct wimlib_wim_info info;
2090
2091                 wimlib_get_wim_info(wim, &info);
2092
2093                 if (desc) {
2094                         ret = wimlib_set_image_descripton(wim,
2095                                                           info.image_count,
2096                                                           desc);
2097                         if (ret)
2098                                 goto out_free_template_wim;
2099                 }
2100
2101                 if (flags_element) {
2102                         ret = wimlib_set_image_flags(wim, info.image_count,
2103                                                      flags_element);
2104                         if (ret)
2105                                 goto out_free_template_wim;
2106                 }
2107
2108                 /* Reference template image if the user provided one.  */
2109                 if (template_image_name_or_num) {
2110                         imagex_printf(T("Using image %d "
2111                                         "from \"%"TS"\" as template\n"),
2112                                         template_image, template_wimfile);
2113                         ret = wimlib_reference_template_image(wim,
2114                                                               info.image_count,
2115                                                               template_wim,
2116                                                               template_image,
2117                                                               0, NULL);
2118                         if (ret)
2119                                 goto out_free_template_wim;
2120                 }
2121         }
2122
2123         /* Write the new WIM or overwrite the existing WIM with the new image
2124          * appended.  */
2125         if (cmd == CMD_APPEND) {
2126                 ret = wimlib_overwrite(wim, write_flags, num_threads,
2127                                        imagex_progress_func);
2128         } else if (wimfile) {
2129                 ret = wimlib_write(wim, wimfile, WIMLIB_ALL_IMAGES,
2130                                    write_flags, num_threads,
2131                                    imagex_progress_func);
2132         } else {
2133                 ret = wimlib_write_to_fd(wim, wim_fd, WIMLIB_ALL_IMAGES,
2134                                          write_flags, num_threads,
2135                                          imagex_progress_func);
2136         }
2137 out_free_template_wim:
2138         /* template_wim may alias base_wims[0] or wim.  */
2139         if ((base_wimfiles.num_strings != 1 || template_wim != base_wims[0]) &&
2140             template_wim != wim)
2141                 wimlib_free(template_wim);
2142 out_free_base_wims:
2143         for (size_t i = 0; i < base_wimfiles.num_strings; i++)
2144                 wimlib_free(base_wims[i]);
2145         free(base_wims);
2146 out_free_wim:
2147         wimlib_free(wim);
2148 out_free_config:
2149         if (config) {
2150                 free(config->exclusion_pats.pats);
2151                 free(config->exclusion_exception_pats.pats);
2152                 free(config_str);
2153         }
2154 out_free_capture_sources:
2155         if (capture_sources_malloced)
2156                 free(capture_sources);
2157 out_free_source_list_contents:
2158         free(source_list_contents);
2159 out_free_base_wimfiles:
2160         string_set_destroy(&base_wimfiles);
2161         return ret;
2162
2163 out_usage:
2164         usage(cmd, stderr);
2165 out_err:
2166         ret = -1;
2167         goto out_free_base_wimfiles;
2168 }
2169
2170 /* Remove image(s) from a WIM. */
2171 static int
2172 imagex_delete(int argc, tchar **argv, int cmd)
2173 {
2174         int c;
2175         int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
2176         int write_flags = 0;
2177         const tchar *wimfile;
2178         const tchar *image_num_or_name;
2179         WIMStruct *wim;
2180         int image;
2181         int ret;
2182
2183         for_opt(c, delete_options) {
2184                 switch (c) {
2185                 case IMAGEX_CHECK_OPTION:
2186                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2187                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2188                         break;
2189                 case IMAGEX_SOFT_OPTION:
2190                         write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
2191                         break;
2192                 default:
2193                         goto out_usage;
2194                 }
2195         }
2196         argc -= optind;
2197         argv += optind;
2198
2199         if (argc != 2) {
2200                 if (argc < 1)
2201                         imagex_error(T("Must specify a WIM file"));
2202                 if (argc < 2)
2203                         imagex_error(T("Must specify an image"));
2204                 goto out_usage;
2205         }
2206         wimfile = argv[0];
2207         image_num_or_name = argv[1];
2208
2209         ret = wimlib_open_wim(wimfile, open_flags, &wim,
2210                               imagex_progress_func);
2211         if (ret)
2212                 goto out;
2213
2214         image = wimlib_resolve_image(wim, image_num_or_name);
2215
2216         ret = verify_image_exists(image, image_num_or_name, wimfile);
2217         if (ret)
2218                 goto out_wimlib_free;
2219
2220         ret = wimlib_delete_image(wim, image);
2221         if (ret) {
2222                 imagex_error(T("Failed to delete image from \"%"TS"\""),
2223                              wimfile);
2224                 goto out_wimlib_free;
2225         }
2226
2227         ret = wimlib_overwrite(wim, write_flags, 0, imagex_progress_func);
2228         if (ret) {
2229                 imagex_error(T("Failed to write the file \"%"TS"\" with image "
2230                                "deleted"), wimfile);
2231         }
2232 out_wimlib_free:
2233         wimlib_free(wim);
2234 out:
2235         return ret;
2236
2237 out_usage:
2238         usage(CMD_DELETE, stderr);
2239         ret = -1;
2240         goto out;
2241 }
2242
2243 static int
2244 print_full_path(const struct wimlib_dir_entry *wdentry, void *_ignore)
2245 {
2246         int ret = tprintf(T("%"TS"\n"), wdentry->full_path);
2247         return (ret >= 0) ? 0 : -1;
2248 }
2249
2250 /* Print the files contained in an image(s) in a WIM file. */
2251 static int
2252 imagex_dir(int argc, tchar **argv, int cmd)
2253 {
2254         const tchar *wimfile;
2255         WIMStruct *wim = NULL;
2256         int image;
2257         int ret;
2258         const tchar *path = T("");
2259         int c;
2260
2261         for_opt(c, dir_options) {
2262                 switch (c) {
2263                 case IMAGEX_PATH_OPTION:
2264                         path = optarg;
2265                         break;
2266                 default:
2267                         goto out_usage;
2268                 }
2269         }
2270         argc -= optind;
2271         argv += optind;
2272
2273         if (argc < 1) {
2274                 imagex_error(T("Must specify a WIM file"));
2275                 goto out_usage;
2276         }
2277         if (argc > 2) {
2278                 imagex_error(T("Too many arguments"));
2279                 goto out_usage;
2280         }
2281
2282         wimfile = argv[0];
2283         ret = wimlib_open_wim(wimfile, 0, &wim, imagex_progress_func);
2284         if (ret)
2285                 goto out;
2286
2287         if (argc >= 2) {
2288                 image = wimlib_resolve_image(wim, argv[1]);
2289                 ret = verify_image_exists(image, argv[1], wimfile);
2290                 if (ret)
2291                         goto out_wimlib_free;
2292         } else {
2293                 /* No image specified; default to image 1, but only if the WIM
2294                  * contains exactly one image.  */
2295
2296                 struct wimlib_wim_info info;
2297
2298                 wimlib_get_wim_info(wim, &info);
2299                 if (info.image_count != 1) {
2300                         imagex_error(T("\"%"TS"\" contains %d images; Please "
2301                                        "select one (or all)."),
2302                                      wimfile, info.image_count);
2303                         wimlib_free(wim);
2304                         goto out_usage;
2305                 }
2306                 image = 1;
2307         }
2308
2309         ret = wimlib_iterate_dir_tree(wim, image, path,
2310                                       WIMLIB_ITERATE_DIR_TREE_FLAG_RECURSIVE,
2311                                       print_full_path, NULL);
2312 out_wimlib_free:
2313         wimlib_free(wim);
2314 out:
2315         return ret;
2316
2317 out_usage:
2318         usage(CMD_DIR, stderr);
2319         ret = -1;
2320         goto out;
2321 }
2322
2323 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
2324  * WIM file. */
2325 static int
2326 imagex_export(int argc, tchar **argv, int cmd)
2327 {
2328         int c;
2329         int open_flags = 0;
2330         int export_flags = 0;
2331         int write_flags = 0;
2332         int compression_type = WIMLIB_COMPRESSION_TYPE_INVALID;
2333         const tchar *src_wimfile;
2334         const tchar *src_image_num_or_name;
2335         const tchar *dest_wimfile;
2336         int dest_wim_fd;
2337         const tchar *dest_name;
2338         const tchar *dest_desc;
2339         WIMStruct *src_wim;
2340         struct wimlib_wim_info src_info;
2341         WIMStruct *dest_wim;
2342         int ret;
2343         int image;
2344         struct stat stbuf;
2345         bool wim_is_new;
2346         STRING_SET(refglobs);
2347         unsigned num_threads = 0;
2348
2349         for_opt(c, export_options) {
2350                 switch (c) {
2351                 case IMAGEX_BOOT_OPTION:
2352                         export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
2353                         break;
2354                 case IMAGEX_CHECK_OPTION:
2355                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2356                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2357                         break;
2358                 case IMAGEX_NOCHECK_OPTION:
2359                         write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
2360                         break;
2361                 case IMAGEX_COMPRESS_OPTION:
2362                         compression_type = get_compression_type(optarg);
2363                         if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
2364                                 goto out_err;
2365                         break;
2366                 case IMAGEX_REF_OPTION:
2367                         ret = string_set_append(&refglobs, optarg);
2368                         if (ret)
2369                                 goto out_free_refglobs;
2370                         break;
2371                 case IMAGEX_THREADS_OPTION:
2372                         num_threads = parse_num_threads(optarg);
2373                         if (num_threads == UINT_MAX)
2374                                 goto out_err;
2375                         break;
2376                 case IMAGEX_REBUILD_OPTION:
2377                         write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
2378                         break;
2379                 case IMAGEX_PIPABLE_OPTION:
2380                         write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2381                         break;
2382                 case IMAGEX_NOT_PIPABLE_OPTION:
2383                         write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
2384                         break;
2385                 default:
2386                         goto out_usage;
2387                 }
2388         }
2389         argc -= optind;
2390         argv += optind;
2391         if (argc < 3 || argc > 5)
2392                 goto out_usage;
2393         src_wimfile           = argv[0];
2394         src_image_num_or_name = argv[1];
2395         dest_wimfile          = argv[2];
2396         dest_name             = (argc >= 4) ? argv[3] : NULL;
2397         dest_desc             = (argc >= 5) ? argv[4] : NULL;
2398         ret = wimlib_open_wim(src_wimfile, open_flags, &src_wim,
2399                               imagex_progress_func);
2400         if (ret)
2401                 goto out_free_refglobs;
2402
2403         wimlib_get_wim_info(src_wim, &src_info);
2404
2405         /* Determine if the destination is an existing file or not.  If so, we
2406          * try to append the exported image(s) to it; otherwise, we create a new
2407          * WIM containing the exported image(s).  Furthermore, determine if we
2408          * need to write a pipable WIM directly to standard output.  */
2409
2410         if (tstrcmp(dest_wimfile, T("-")) == 0) {
2411         #if 0
2412                 if (!(write_flags & WIMLIB_WRITE_FLAG_PIPABLE)) {
2413                         imagex_error("Can't write a non-pipable WIM to "
2414                                      "standard output!  Specify --pipable\n"
2415                                      "       if you want to create a pipable WIM "
2416                                      "(but read the docs first).");
2417                         ret = -1;
2418                         goto out_free_src_wim;
2419                 }
2420         #else
2421                 write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
2422         #endif
2423                 dest_wimfile = NULL;
2424                 dest_wim_fd = STDOUT_FILENO;
2425                 imagex_info_file = stderr;
2426                 set_fd_to_binary_mode(dest_wim_fd);
2427         }
2428         errno = ENOENT;
2429         if (dest_wimfile != NULL && tstat(dest_wimfile, &stbuf) == 0) {
2430                 wim_is_new = false;
2431                 /* Destination file exists. */
2432
2433                 if (!S_ISREG(stbuf.st_mode)) {
2434                         imagex_error(T("\"%"TS"\" is not a regular file"),
2435                                      dest_wimfile);
2436                         ret = -1;
2437                         goto out_free_src_wim;
2438                 }
2439                 ret = wimlib_open_wim(dest_wimfile,
2440                                       open_flags | WIMLIB_OPEN_FLAG_WRITE_ACCESS,
2441                                       &dest_wim, imagex_progress_func);
2442                 if (ret)
2443                         goto out_free_src_wim;
2444
2445                 if (compression_type != WIMLIB_COMPRESSION_TYPE_INVALID) {
2446                         /* The user specified a compression type, but we're
2447                          * exporting to an existing WIM.  Make sure the
2448                          * specified compression type is the same as the
2449                          * compression type of the existing destination WIM. */
2450                         struct wimlib_wim_info dest_info;
2451
2452                         wimlib_get_wim_info(dest_wim, &dest_info);
2453                         if (compression_type != dest_info.compression_type) {
2454                                 imagex_error(T("Cannot specify a compression type that is "
2455                                                "not the same as that used in the "
2456                                                "destination WIM"));
2457                                 ret = -1;
2458                                 goto out_free_dest_wim;
2459                         }
2460                 }
2461         } else {
2462                 wim_is_new = true;
2463
2464                 if (errno != ENOENT) {
2465                         imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
2466                                                 dest_wimfile);
2467                         ret = -1;
2468                         goto out_free_src_wim;
2469                 }
2470
2471                 /* dest_wimfile is not an existing file, so create a new WIM. */
2472
2473                 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID) {
2474                         /* The user did not specify a compression type; default
2475                          * to that of the source WIM.  */
2476
2477                         compression_type = src_info.compression_type;
2478                 }
2479                 ret = wimlib_create_new_wim(compression_type, &dest_wim);
2480                 if (ret)
2481                         goto out_free_src_wim;
2482         }
2483
2484         image = wimlib_resolve_image(src_wim, src_image_num_or_name);
2485         ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
2486         if (ret)
2487                 goto out_free_dest_wim;
2488
2489         if (refglobs.num_strings) {
2490                 ret = wim_reference_globs(src_wim, &refglobs, open_flags);
2491                 if (ret)
2492                         goto out_free_dest_wim;
2493         }
2494
2495         if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
2496             image == WIMLIB_ALL_IMAGES && src_info.boot_index == 0)
2497         {
2498                 imagex_error(T("--boot specified for all-images export, but source WIM "
2499                                "has no bootable image."));
2500                 ret = -1;
2501                 goto out_free_dest_wim;
2502         }
2503
2504         ret = wimlib_export_image(src_wim, image, dest_wim, dest_name,
2505                                   dest_desc, export_flags, imagex_progress_func);
2506         if (ret) {
2507                 if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2508                         do_resource_not_found_warning(src_wimfile,
2509                                                       &src_info, &refglobs);
2510                 }
2511                 goto out_free_dest_wim;
2512         }
2513
2514         if (!wim_is_new)
2515                 ret = wimlib_overwrite(dest_wim, write_flags, num_threads,
2516                                        imagex_progress_func);
2517         else if (dest_wimfile)
2518                 ret = wimlib_write(dest_wim, dest_wimfile, WIMLIB_ALL_IMAGES,
2519                                    write_flags, num_threads,
2520                                    imagex_progress_func);
2521         else
2522                 ret = wimlib_write_to_fd(dest_wim, dest_wim_fd,
2523                                          WIMLIB_ALL_IMAGES, write_flags,
2524                                          num_threads, imagex_progress_func);
2525 out_free_dest_wim:
2526         wimlib_free(dest_wim);
2527 out_free_src_wim:
2528         wimlib_free(src_wim);
2529 out_free_refglobs:
2530         string_set_destroy(&refglobs);
2531         return ret;
2532
2533 out_usage:
2534         usage(CMD_EXPORT, stderr);
2535 out_err:
2536         ret = -1;
2537         goto out_free_refglobs;
2538 }
2539
2540 static bool
2541 is_root_wim_path(const tchar *path)
2542 {
2543         const tchar *p;
2544         for (p = path; *p; p++)
2545                 if (*p != T('\\') && *p != T('/'))
2546                         return false;
2547         return true;
2548 }
2549
2550 static void
2551 free_extract_commands(struct wimlib_extract_command *cmds, size_t num_cmds,
2552                       const tchar *dest_dir)
2553 {
2554         for (size_t i = 0; i < num_cmds; i++)
2555                 if (cmds[i].fs_dest_path != dest_dir)
2556                         free(cmds[i].fs_dest_path);
2557         free(cmds);
2558 }
2559
2560 static struct wimlib_extract_command *
2561 prepare_extract_commands(tchar **paths, unsigned num_paths,
2562                          int extract_flags, tchar *dest_dir,
2563                          size_t *num_cmds_ret)
2564 {
2565         struct wimlib_extract_command *cmds;
2566         size_t num_cmds;
2567         tchar *emptystr = T("");
2568
2569         if (num_paths == 0) {
2570                 num_paths = 1;
2571                 paths = &emptystr;
2572         }
2573         num_cmds = num_paths;
2574         cmds = calloc(num_cmds, sizeof(cmds[0]));
2575         if (!cmds) {
2576                 imagex_error(T("Out of memory!"));
2577                 return NULL;
2578         }
2579
2580         for (size_t i = 0; i < num_cmds; i++) {
2581                 cmds[i].extract_flags = extract_flags;
2582                 cmds[i].wim_source_path = paths[i];
2583                 if (is_root_wim_path(paths[i])) {
2584                         cmds[i].fs_dest_path = dest_dir;
2585                 } else {
2586                         size_t len = tstrlen(dest_dir) + 1 + tstrlen(paths[i]);
2587                         cmds[i].fs_dest_path = malloc((len + 1) * sizeof(tchar));
2588                         if (!cmds[i].fs_dest_path) {
2589                                 free_extract_commands(cmds, num_cmds, dest_dir);
2590                                 return NULL;
2591                         }
2592                         tsprintf(cmds[i].fs_dest_path,
2593                                  T("%"TS""OS_PREFERRED_PATH_SEPARATOR_STRING"%"TS),
2594                                  dest_dir, tbasename(paths[i]));
2595                 }
2596         }
2597         *num_cmds_ret = num_cmds;
2598         return cmds;
2599 }
2600
2601 /* Extract files or directories from a WIM image */
2602 static int
2603 imagex_extract(int argc, tchar **argv, int cmd)
2604 {
2605         int c;
2606         int open_flags = 0;
2607         int image;
2608         WIMStruct *wim;
2609         int ret;
2610         const tchar *wimfile;
2611         const tchar *image_num_or_name;
2612         tchar *dest_dir = T(".");
2613         int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL | WIMLIB_EXTRACT_FLAG_NORPFIX;
2614
2615         STRING_SET(refglobs);
2616
2617         struct wimlib_extract_command *cmds;
2618         size_t num_cmds;
2619
2620         for_opt(c, extract_options) {
2621                 switch (c) {
2622                 case IMAGEX_CHECK_OPTION:
2623                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2624                         break;
2625                 case IMAGEX_VERBOSE_OPTION:
2626                         extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
2627                         break;
2628                 case IMAGEX_REF_OPTION:
2629                         ret = string_set_append(&refglobs, optarg);
2630                         if (ret)
2631                                 goto out_free_refglobs;
2632                         break;
2633                 case IMAGEX_UNIX_DATA_OPTION:
2634                         extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
2635                         break;
2636                 case IMAGEX_NO_ACLS_OPTION:
2637                         extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
2638                         break;
2639                 case IMAGEX_STRICT_ACLS_OPTION:
2640                         extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
2641                         break;
2642                 case IMAGEX_DEST_DIR_OPTION:
2643                         dest_dir = optarg;
2644                         break;
2645                 case IMAGEX_TO_STDOUT_OPTION:
2646                         extract_flags |= WIMLIB_EXTRACT_FLAG_TO_STDOUT;
2647                         imagex_info_file = stderr;
2648                         imagex_be_quiet = true;
2649                         break;
2650                 case IMAGEX_INCLUDE_INVALID_NAMES_OPTION:
2651                         extract_flags |= WIMLIB_EXTRACT_FLAG_REPLACE_INVALID_FILENAMES;
2652                         extract_flags |= WIMLIB_EXTRACT_FLAG_ALL_CASE_CONFLICTS;
2653                         break;
2654                 default:
2655                         goto out_usage;
2656                 }
2657         }
2658         argc -= optind;
2659         argv += optind;
2660
2661         if (argc < 2)
2662                 goto out_usage;
2663
2664         wimfile = argv[0];
2665         image_num_or_name = argv[1];
2666
2667         argc -= 2;
2668         argv += 2;
2669
2670         cmds = prepare_extract_commands(argv, argc, extract_flags, dest_dir,
2671                                         &num_cmds);
2672         if (!cmds)
2673                 goto out_err;
2674
2675         ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2676         if (ret)
2677                 goto out_free_cmds;
2678
2679         image = wimlib_resolve_image(wim, image_num_or_name);
2680         ret = verify_image_exists_and_is_single(image,
2681                                                 image_num_or_name,
2682                                                 wimfile);
2683         if (ret)
2684                 goto out_wimlib_free;
2685
2686         if (refglobs.num_strings) {
2687                 ret = wim_reference_globs(wim, &refglobs, open_flags);
2688                 if (ret)
2689                         goto out_wimlib_free;
2690         }
2691
2692         ret = wimlib_extract_files(wim, image, cmds, num_cmds, 0,
2693                                    imagex_progress_func);
2694         if (ret == 0) {
2695                 if (!imagex_be_quiet)
2696                         imagex_printf(T("Done extracting files.\n"));
2697         } else if (ret == WIMLIB_ERR_PATH_DOES_NOT_EXIST) {
2698                 tfprintf(stderr, T("Note: You can use `%"TS"' to see what "
2699                                    "files and directories\n"
2700                                    "      are in the WIM image.\n"),
2701                                 get_cmd_string(CMD_INFO, false));
2702         } else if (ret == WIMLIB_ERR_RESOURCE_NOT_FOUND) {
2703                 struct wimlib_wim_info info;
2704
2705                 wimlib_get_wim_info(wim, &info);
2706                 do_resource_not_found_warning(wimfile, &info, &refglobs);
2707         }
2708 out_wimlib_free:
2709         wimlib_free(wim);
2710 out_free_cmds:
2711         free_extract_commands(cmds, num_cmds, dest_dir);
2712 out_free_refglobs:
2713         string_set_destroy(&refglobs);
2714         return ret;
2715
2716 out_usage:
2717         usage(CMD_EXTRACT, stderr);
2718 out_err:
2719         ret = -1;
2720         goto out_free_refglobs;
2721 }
2722
2723 static void print_byte_field(const uint8_t field[], size_t len)
2724 {
2725         while (len--)
2726                 tprintf(T("%02hhx"), *field++);
2727 }
2728
2729 static void
2730 print_wim_information(const tchar *wimfile, const struct wimlib_wim_info *info)
2731 {
2732         tputs(T("WIM Information:"));
2733         tputs(T("----------------"));
2734         tprintf(T("Path:           %"TS"\n"), wimfile);
2735         tprintf(T("GUID:           0x"));
2736         print_byte_field(info->guid, sizeof(info->guid));
2737         tputchar(T('\n'));
2738         tprintf(T("Image Count:    %d\n"), info->image_count);
2739         tprintf(T("Compression:    %"TS"\n"),
2740                 wimlib_get_compression_type_string(info->compression_type));
2741         tprintf(T("Part Number:    %d/%d\n"), info->part_number, info->total_parts);
2742         tprintf(T("Boot Index:     %d\n"), info->boot_index);
2743         tprintf(T("Size:           %"PRIu64" bytes\n"), info->total_bytes);
2744         tprintf(T("Integrity Info: %"TS"\n"),
2745                 info->has_integrity_table ? T("yes") : T("no"));
2746         tprintf(T("Relative path junction: %"TS"\n"),
2747                 info->has_rpfix ? T("yes") : T("no"));
2748         tprintf(T("Pipable:        %"TS"\n"),
2749                 info->pipable ? T("yes") : T("no"));
2750         tputchar(T('\n'));
2751 }
2752
2753 static int
2754 print_resource(const struct wimlib_resource_entry *resource,
2755                void *_ignore)
2756 {
2757
2758         tprintf(T("Uncompressed size   = %"PRIu64" bytes\n"),
2759                 resource->uncompressed_size);
2760
2761         tprintf(T("Compressed size     = %"PRIu64" bytes\n"),
2762                 resource->compressed_size);
2763
2764         tprintf(T("Offset              = %"PRIu64" bytes\n"),
2765                 resource->offset);
2766
2767
2768         tprintf(T("Part Number         = %u\n"), resource->part_number);
2769         tprintf(T("Reference Count     = %u\n"), resource->reference_count);
2770
2771         tprintf(T("Hash                = 0x"));
2772         print_byte_field(resource->sha1_hash, sizeof(resource->sha1_hash));
2773         tputchar(T('\n'));
2774
2775         tprintf(T("Flags               = "));
2776         if (resource->is_compressed)
2777                 tprintf(T("WIM_RESHDR_FLAG_COMPRESSED  "));
2778         if (resource->is_metadata)
2779                 tprintf(T("WIM_RESHDR_FLAG_METADATA  "));
2780         if (resource->is_free)
2781                 tprintf(T("WIM_RESHDR_FLAG_FREE  "));
2782         if (resource->is_spanned)
2783                 tprintf(T("WIM_RESHDR_FLAG_SPANNED  "));
2784         tputchar(T('\n'));
2785         tputchar(T('\n'));
2786         return 0;
2787 }
2788
2789 static void
2790 print_lookup_table(WIMStruct *wim)
2791 {
2792         wimlib_iterate_lookup_table(wim, 0, print_resource, NULL);
2793 }
2794
2795 /* Prints information about a WIM file; also can mark an image as bootable,
2796  * change the name of an image, or change the description of an image. */
2797 static int
2798 imagex_info(int argc, tchar **argv, int cmd)
2799 {
2800         int c;
2801         bool boot         = false;
2802         bool check        = false;
2803         bool nocheck      = false;
2804         bool header       = false;
2805         bool lookup_table = false;
2806         bool xml          = false;
2807         bool metadata     = false;
2808         bool short_header = true;
2809         const tchar *xml_out_file = NULL;
2810         const tchar *wimfile;
2811         const tchar *image_num_or_name;
2812         const tchar *new_name;
2813         const tchar *new_desc;
2814         WIMStruct *wim;
2815         int image;
2816         int ret;
2817         int open_flags = 0;
2818         struct wimlib_wim_info info;
2819
2820         for_opt(c, info_options) {
2821                 switch (c) {
2822                 case IMAGEX_BOOT_OPTION:
2823                         boot = true;
2824                         break;
2825                 case IMAGEX_CHECK_OPTION:
2826                         check = true;
2827                         break;
2828                 case IMAGEX_NOCHECK_OPTION:
2829                         nocheck = true;
2830                         break;
2831                 case IMAGEX_HEADER_OPTION:
2832                         header = true;
2833                         short_header = false;
2834                         break;
2835                 case IMAGEX_LOOKUP_TABLE_OPTION:
2836                         lookup_table = true;
2837                         short_header = false;
2838                         break;
2839                 case IMAGEX_XML_OPTION:
2840                         xml = true;
2841                         short_header = false;
2842                         break;
2843                 case IMAGEX_EXTRACT_XML_OPTION:
2844                         xml_out_file = optarg;
2845                         short_header = false;
2846                         break;
2847                 case IMAGEX_METADATA_OPTION:
2848                         metadata = true;
2849                         short_header = false;
2850                         break;
2851                 default:
2852                         goto out_usage;
2853                 }
2854         }
2855
2856         argc -= optind;
2857         argv += optind;
2858         if (argc < 1 || argc > 4)
2859                 goto out_usage;
2860
2861         wimfile           = argv[0];
2862         image_num_or_name = (argc >= 2) ? argv[1] : T("all");
2863         new_name          = (argc >= 3) ? argv[2] : NULL;
2864         new_desc          = (argc >= 4) ? argv[3] : NULL;
2865
2866         if (check && nocheck) {
2867                 imagex_error(T("Can't specify both --check and --nocheck"));
2868                 goto out_err;
2869         }
2870
2871         if (check)
2872                 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2873
2874         ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
2875         if (ret)
2876                 goto out;
2877
2878         wimlib_get_wim_info(wim, &info);
2879
2880         image = wimlib_resolve_image(wim, image_num_or_name);
2881         ret = WIMLIB_ERR_INVALID_IMAGE;
2882         if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
2883                 verify_image_exists(image, image_num_or_name, wimfile);
2884                 if (boot) {
2885                         imagex_error(T("If you would like to set the boot "
2886                                        "index to 0, specify image \"0\" with "
2887                                        "the --boot flag."));
2888                 }
2889                 goto out_wimlib_free;
2890         }
2891
2892         if (boot && info.image_count == 0) {
2893                 imagex_error(T("--boot is meaningless on a WIM with no images"));
2894                 goto out_wimlib_free;
2895         }
2896
2897         if (image == WIMLIB_ALL_IMAGES && info.image_count > 1) {
2898                 if (boot) {
2899                         imagex_error(T("Cannot specify the --boot flag "
2900                                        "without specifying a specific "
2901                                        "image in a multi-image WIM"));
2902                         goto out_wimlib_free;
2903                 }
2904                 if (new_name) {
2905                         imagex_error(T("Cannot specify the NEW_NAME "
2906                                        "without specifying a specific "
2907                                        "image in a multi-image WIM"));
2908                         goto out_wimlib_free;
2909                 }
2910         }
2911
2912         /* Operations that print information are separated from operations that
2913          * recreate the WIM file. */
2914         if (!new_name && !boot) {
2915
2916                 /* Read-only operations */
2917
2918                 if (image == WIMLIB_NO_IMAGE) {
2919                         imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\""),
2920                                      image_num_or_name, wimfile);
2921                         goto out_wimlib_free;
2922                 }
2923
2924                 if (image == WIMLIB_ALL_IMAGES && short_header)
2925                         print_wim_information(wimfile, &info);
2926
2927                 if (header)
2928                         wimlib_print_header(wim);
2929
2930                 if (lookup_table) {
2931                         if (info.total_parts != 1) {
2932                                 tfprintf(stderr, T("Warning: Only showing the lookup table "
2933                                                    "for part %d of a %d-part WIM.\n"),
2934                                          info.part_number, info.total_parts);
2935                         }
2936                         print_lookup_table(wim);
2937                 }
2938
2939                 if (xml) {
2940                         ret = wimlib_extract_xml_data(wim, stdout);
2941                         if (ret)
2942                                 goto out_wimlib_free;
2943                 }
2944
2945                 if (xml_out_file) {
2946                         FILE *fp;
2947
2948                         fp = tfopen(xml_out_file, T("wb"));
2949                         if (!fp) {
2950                                 imagex_error_with_errno(T("Failed to open the "
2951                                                           "file \"%"TS"\" for "
2952                                                           "writing"),
2953                                                         xml_out_file);
2954                                 ret = -1;
2955                                 goto out_wimlib_free;
2956                         }
2957                         ret = wimlib_extract_xml_data(wim, fp);
2958                         if (fclose(fp)) {
2959                                 imagex_error(T("Failed to close the file "
2960                                                "\"%"TS"\""),
2961                                              xml_out_file);
2962                                 ret = -1;
2963                         }
2964                         if (ret)
2965                                 goto out_wimlib_free;
2966                 }
2967
2968                 if (short_header)
2969                         wimlib_print_available_images(wim, image);
2970
2971                 if (metadata) {
2972                         ret = wimlib_print_metadata(wim, image);
2973                         if (ret)
2974                                 goto out_wimlib_free;
2975                 }
2976                 ret = 0;
2977         } else {
2978
2979                 /* Modification operations */
2980
2981                 if (image == WIMLIB_ALL_IMAGES)
2982                         image = 1;
2983
2984                 if (image == WIMLIB_NO_IMAGE && new_name) {
2985                         imagex_error(T("Cannot specify new_name (\"%"TS"\") "
2986                                        "when using image 0"), new_name);
2987                         ret = -1;
2988                         goto out_wimlib_free;
2989                 }
2990
2991                 if (boot) {
2992                         if (image == info.boot_index) {
2993                                 imagex_printf(T("Image %d is already marked as "
2994                                           "bootable.\n"), image);
2995                                 boot = false;
2996                         } else {
2997                                 imagex_printf(T("Marking image %d as bootable.\n"),
2998                                         image);
2999                                 info.boot_index = image;
3000                                 ret = wimlib_set_wim_info(wim, &info,
3001                                                           WIMLIB_CHANGE_BOOT_INDEX);
3002                                 if (ret)
3003                                         goto out_wimlib_free;
3004                         }
3005                 }
3006                 if (new_name) {
3007                         if (!tstrcmp(wimlib_get_image_name(wim, image), new_name))
3008                         {
3009                                 imagex_printf(T("Image %d is already named \"%"TS"\".\n"),
3010                                         image, new_name);
3011                                 new_name = NULL;
3012                         } else {
3013                                 imagex_printf(T("Changing the name of image %d to "
3014                                           "\"%"TS"\".\n"), image, new_name);
3015                                 ret = wimlib_set_image_name(wim, image, new_name);
3016                                 if (ret)
3017                                         goto out_wimlib_free;
3018                         }
3019                 }
3020                 if (new_desc) {
3021                         const tchar *old_desc;
3022                         old_desc = wimlib_get_image_description(wim, image);
3023                         if (old_desc && !tstrcmp(old_desc, new_desc)) {
3024                                 imagex_printf(T("The description of image %d is already "
3025                                           "\"%"TS"\".\n"), image, new_desc);
3026                                 new_desc = NULL;
3027                         } else {
3028                                 imagex_printf(T("Changing the description of image %d "
3029                                           "to \"%"TS"\".\n"), image, new_desc);
3030                                 ret = wimlib_set_image_descripton(wim, image,
3031                                                                   new_desc);
3032                                 if (ret)
3033                                         goto out_wimlib_free;
3034                         }
3035                 }
3036
3037                 /* Only call wimlib_overwrite() if something actually needs to
3038                  * be changed.  */
3039                 if (boot || new_name || new_desc ||
3040                     (check && !info.has_integrity_table) ||
3041                     (nocheck && info.has_integrity_table))
3042                 {
3043                         int write_flags = 0;
3044
3045                         if (check)
3046                                 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3047                         if (nocheck)
3048                                 write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3049                         ret = wimlib_overwrite(wim, write_flags, 1,
3050                                                imagex_progress_func);
3051                 } else {
3052                         imagex_printf(T("The file \"%"TS"\" was not modified "
3053                                         "because nothing needed to be done.\n"),
3054                                       wimfile);
3055                         ret = 0;
3056                 }
3057         }
3058 out_wimlib_free:
3059         wimlib_free(wim);
3060 out:
3061         return ret;
3062
3063 out_usage:
3064         usage(CMD_INFO, stderr);
3065 out_err:
3066         ret = -1;
3067         goto out;
3068 }
3069
3070 /* Join split WIMs into one part WIM */
3071 static int
3072 imagex_join(int argc, tchar **argv, int cmd)
3073 {
3074         int c;
3075         int swm_open_flags = 0;
3076         int wim_write_flags = 0;
3077         const tchar *output_path;
3078         int ret;
3079
3080         for_opt(c, join_options) {
3081                 switch (c) {
3082                 case IMAGEX_CHECK_OPTION:
3083                         swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3084                         wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3085                         break;
3086                 default:
3087                         goto out_usage;
3088                 }
3089         }
3090         argc -= optind;
3091         argv += optind;
3092
3093         if (argc < 2) {
3094                 imagex_error(T("Must specify one or more split WIM (.swm) "
3095                                "parts to join"));
3096                 goto out_usage;
3097         }
3098         output_path = argv[0];
3099         ret = wimlib_join((const tchar * const *)++argv,
3100                           --argc,
3101                           output_path,
3102                           swm_open_flags,
3103                           wim_write_flags,
3104                           imagex_progress_func);
3105 out:
3106         return ret;
3107
3108 out_usage:
3109         usage(CMD_JOIN, stderr);
3110         ret = -1;
3111         goto out;
3112 }
3113
3114 #if WIM_MOUNTING_SUPPORTED
3115
3116 /* Mounts a WIM image.  */
3117 static int
3118 imagex_mount_rw_or_ro(int argc, tchar **argv, int cmd)
3119 {
3120         int c;
3121         int mount_flags = 0;
3122         int open_flags = 0;
3123         const tchar *staging_dir = NULL;
3124         const tchar *wimfile;
3125         const tchar *dir;
3126         WIMStruct *wim;
3127         struct wimlib_wim_info info;
3128         int image;
3129         int ret;
3130
3131         STRING_SET(refglobs);
3132
3133         if (cmd == CMD_MOUNTRW) {
3134                 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
3135                 open_flags |= WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3136         }
3137
3138         for_opt(c, mount_options) {
3139                 switch (c) {
3140                 case IMAGEX_ALLOW_OTHER_OPTION:
3141                         mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
3142                         break;
3143                 case IMAGEX_CHECK_OPTION:
3144                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3145                         break;
3146                 case IMAGEX_DEBUG_OPTION:
3147                         mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
3148                         break;
3149                 case IMAGEX_STREAMS_INTERFACE_OPTION:
3150                         if (!tstrcasecmp(optarg, T("none")))
3151                                 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
3152                         else if (!tstrcasecmp(optarg, T("xattr")))
3153                                 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
3154                         else if (!tstrcasecmp(optarg, T("windows")))
3155                                 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
3156                         else {
3157                                 imagex_error(T("Unknown stream interface \"%"TS"\""),
3158                                              optarg);
3159                                 goto out_usage;
3160                         }
3161                         break;
3162                 case IMAGEX_REF_OPTION:
3163                         ret = string_set_append(&refglobs, optarg);
3164                         if (ret)
3165                                 goto out_free_refglobs;
3166                         break;
3167                 case IMAGEX_STAGING_DIR_OPTION:
3168                         staging_dir = optarg;
3169                         break;
3170                 case IMAGEX_UNIX_DATA_OPTION:
3171                         mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
3172                         break;
3173                 default:
3174                         goto out_usage;
3175                 }
3176         }
3177         argc -= optind;
3178         argv += optind;
3179         if (argc != 2 && argc != 3)
3180                 goto out_usage;
3181
3182         wimfile = argv[0];
3183
3184         ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3185         if (ret)
3186                 goto out_free_refglobs;
3187
3188         wimlib_get_wim_info(wim, &info);
3189
3190         if (argc >= 3) {
3191                 /* Image explicitly specified.  */
3192                 image = wimlib_resolve_image(wim, argv[1]);
3193                 dir = argv[2];
3194                 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
3195                 if (ret)
3196                         goto out_free_wim;
3197         } else {
3198                 /* No image specified; default to image 1, but only if the WIM
3199                  * contains exactly one image.  */
3200
3201                 if (info.image_count != 1) {
3202                         imagex_error(T("\"%"TS"\" contains %d images; Please "
3203                                        "select one."), wimfile, info.image_count);
3204                         wimlib_free(wim);
3205                         goto out_usage;
3206                 }
3207                 image = 1;
3208                 dir = argv[1];
3209         }
3210
3211         if (refglobs.num_strings) {
3212                 ret = wim_reference_globs(wim, &refglobs, open_flags);
3213                 if (ret)
3214                         goto out_free_wim;
3215         }
3216
3217         ret = wimlib_mount_image(wim, image, dir, mount_flags, staging_dir);
3218         if (ret) {
3219                 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
3220                                "on \"%"TS"\""),
3221                              image, wimfile, dir);
3222         }
3223 out_free_wim:
3224         wimlib_free(wim);
3225 out_free_refglobs:
3226         string_set_destroy(&refglobs);
3227         return ret;
3228
3229 out_usage:
3230         usage(cmd, stderr);
3231         ret = -1;
3232         goto out_free_refglobs;
3233 }
3234 #endif /* WIM_MOUNTING_SUPPORTED */
3235
3236 /* Rebuild a WIM file */
3237 static int
3238 imagex_optimize(int argc, tchar **argv, int cmd)
3239 {
3240         int c;
3241         int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3242         int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
3243         int ret;
3244         WIMStruct *wim;
3245         const tchar *wimfile;
3246         off_t old_size;
3247         off_t new_size;
3248         unsigned num_threads = 0;
3249
3250         for_opt(c, optimize_options) {
3251                 switch (c) {
3252                 case IMAGEX_CHECK_OPTION:
3253                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3254                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3255                         break;
3256                 case IMAGEX_NOCHECK_OPTION:
3257                         write_flags |= WIMLIB_WRITE_FLAG_NO_CHECK_INTEGRITY;
3258                         break;
3259                 case IMAGEX_RECOMPRESS_OPTION:
3260                         write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3261                         break;
3262                 case IMAGEX_COMPRESS_SLOW_OPTION:
3263                         write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
3264                         set_compress_slow();
3265                         break;
3266                 case IMAGEX_THREADS_OPTION:
3267                         num_threads = parse_num_threads(optarg);
3268                         if (num_threads == UINT_MAX)
3269                                 goto out_err;
3270                         break;
3271                 case IMAGEX_PIPABLE_OPTION:
3272                         write_flags |= WIMLIB_WRITE_FLAG_PIPABLE;
3273                         break;
3274                 case IMAGEX_NOT_PIPABLE_OPTION:
3275                         write_flags |= WIMLIB_WRITE_FLAG_NOT_PIPABLE;
3276                         break;
3277                 default:
3278                         goto out_usage;
3279                 }
3280         }
3281         argc -= optind;
3282         argv += optind;
3283
3284         if (argc != 1)
3285                 goto out_usage;
3286
3287         wimfile = argv[0];
3288
3289         ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3290         if (ret)
3291                 goto out;
3292
3293         old_size = file_get_size(wimfile);
3294         tprintf(T("\"%"TS"\" original size: "), wimfile);
3295         if (old_size == -1)
3296                 tputs(T("Unknown"));
3297         else
3298                 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
3299
3300         ret = wimlib_overwrite(wim, write_flags, num_threads,
3301                                imagex_progress_func);
3302         if (ret) {
3303                 imagex_error(T("Optimization of \"%"TS"\" failed."), wimfile);
3304                 goto out_wimlib_free;
3305         }
3306
3307         new_size = file_get_size(wimfile);
3308         tprintf(T("\"%"TS"\" optimized size: "), wimfile);
3309         if (new_size == -1)
3310                 tputs(T("Unknown"));
3311         else
3312                 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
3313
3314         tfputs(T("Space saved: "), stdout);
3315         if (new_size != -1 && old_size != -1) {
3316                 tprintf(T("%lld KiB\n"),
3317                        ((long long)old_size - (long long)new_size) >> 10);
3318         } else {
3319                 tputs(T("Unknown"));
3320         }
3321         ret = 0;
3322 out_wimlib_free:
3323         wimlib_free(wim);
3324 out:
3325         return ret;
3326
3327 out_usage:
3328         usage(CMD_OPTIMIZE, stderr);
3329 out_err:
3330         ret = -1;
3331         goto out;
3332 }
3333
3334 /* Split a WIM into a spanned set */
3335 static int
3336 imagex_split(int argc, tchar **argv, int cmd)
3337 {
3338         int c;
3339         int open_flags = 0;
3340         int write_flags = 0;
3341         unsigned long part_size;
3342         tchar *tmp;
3343         int ret;
3344         WIMStruct *wim;
3345
3346         for_opt(c, split_options) {
3347                 switch (c) {
3348                 case IMAGEX_CHECK_OPTION:
3349                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3350                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3351                         break;
3352                 default:
3353                         goto out_usage;
3354                 }
3355         }
3356         argc -= optind;
3357         argv += optind;
3358
3359         if (argc != 3)
3360                 goto out_usage;
3361
3362         part_size = tstrtod(argv[2], &tmp) * (1 << 20);
3363         if (tmp == argv[2] || *tmp) {
3364                 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
3365                 imagex_error(T("The part size must be an integer or "
3366                                "floating-point number of megabytes."));
3367                 goto out_err;
3368         }
3369         ret = wimlib_open_wim(argv[0], open_flags, &wim, imagex_progress_func);
3370         if (ret)
3371                 goto out;
3372
3373         ret = wimlib_split(wim, argv[1], part_size, write_flags, imagex_progress_func);
3374         wimlib_free(wim);
3375 out:
3376         return ret;
3377
3378 out_usage:
3379         usage(CMD_SPLIT, stderr);
3380 out_err:
3381         ret = -1;
3382         goto out;
3383 }
3384
3385 #if WIM_MOUNTING_SUPPORTED
3386 /* Unmounts a mounted WIM image. */
3387 static int
3388 imagex_unmount(int argc, tchar **argv, int cmd)
3389 {
3390         int c;
3391         int unmount_flags = 0;
3392         int ret;
3393
3394         for_opt(c, unmount_options) {
3395                 switch (c) {
3396                 case IMAGEX_COMMIT_OPTION:
3397                         unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
3398                         break;
3399                 case IMAGEX_CHECK_OPTION:
3400                         unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
3401                         break;
3402                 case IMAGEX_REBUILD_OPTION:
3403                         unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
3404                         break;
3405                 case IMAGEX_LAZY_OPTION:
3406                         unmount_flags |= WIMLIB_UNMOUNT_FLAG_LAZY;
3407                         break;
3408                 default:
3409                         goto out_usage;
3410                 }
3411         }
3412         argc -= optind;
3413         argv += optind;
3414         if (argc != 1)
3415                 goto out_usage;
3416
3417         ret = wimlib_unmount_image(argv[0], unmount_flags,
3418                                    imagex_progress_func);
3419         if (ret)
3420                 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
3421 out:
3422         return ret;
3423
3424 out_usage:
3425         usage(CMD_UNMOUNT, stderr);
3426         ret = -1;
3427         goto out;
3428 }
3429 #endif /* WIM_MOUNTING_SUPPORTED */
3430
3431 /*
3432  * Add, delete, or rename files in a WIM image.
3433  */
3434 static int
3435 imagex_update(int argc, tchar **argv, int cmd)
3436 {
3437         const tchar *wimfile;
3438         int image;
3439         WIMStruct *wim;
3440         int ret;
3441         int open_flags = WIMLIB_OPEN_FLAG_WRITE_ACCESS;
3442         int write_flags = 0;
3443         int update_flags = WIMLIB_UPDATE_FLAG_SEND_PROGRESS;
3444         int default_add_flags = WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
3445         int default_delete_flags = 0;
3446         unsigned num_threads = 0;
3447         int c;
3448         tchar *cmd_file_contents;
3449         size_t cmd_file_nchars;
3450         struct wimlib_update_command *cmds;
3451         size_t num_cmds;
3452         tchar *command_str = NULL;
3453
3454         const tchar *config_file = NULL;
3455         tchar *config_str;
3456         struct wimlib_capture_config *config;
3457
3458         for_opt(c, update_options) {
3459                 switch (c) {
3460                 /* Generic or write options */
3461                 case IMAGEX_THREADS_OPTION:
3462                         num_threads = parse_num_threads(optarg);
3463                         if (num_threads == UINT_MAX)
3464                                 goto out_err;
3465                         break;
3466                 case IMAGEX_CHECK_OPTION:
3467                         open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
3468                         write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
3469                         break;
3470                 case IMAGEX_REBUILD_OPTION:
3471                         write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
3472                         break;
3473                 case IMAGEX_COMMAND_OPTION:
3474                         if (command_str) {
3475                                 imagex_error(T("--command may only be specified "
3476                                                "one time.  Please provide\n"
3477                                                "       the update commands "
3478                                                "on standard input instead."));
3479                                 goto out_err;
3480                         }
3481                         command_str = tstrdup(optarg);
3482                         if (!command_str) {
3483                                 imagex_error(T("Out of memory!"));
3484                                 goto out_err;
3485                         }
3486                         break;
3487                 /* Default delete options */
3488                 case IMAGEX_FORCE_OPTION:
3489                         default_delete_flags |= WIMLIB_DELETE_FLAG_FORCE;
3490                         break;
3491                 case IMAGEX_RECURSIVE_OPTION:
3492                         default_delete_flags |= WIMLIB_DELETE_FLAG_RECURSIVE;
3493                         break;
3494
3495                 /* Global add option */
3496                 case IMAGEX_CONFIG_OPTION:
3497                         default_add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
3498                         config_file = optarg;
3499                         break;
3500
3501                 /* Default add options */
3502                 case IMAGEX_VERBOSE_OPTION:
3503                         default_add_flags |= WIMLIB_ADD_FLAG_VERBOSE;
3504                         break;
3505                 case IMAGEX_DEREFERENCE_OPTION:
3506                         default_add_flags |= WIMLIB_ADD_FLAG_DEREFERENCE;
3507                         break;
3508                 case IMAGEX_UNIX_DATA_OPTION:
3509                         default_add_flags |= WIMLIB_ADD_FLAG_UNIX_DATA;
3510                         break;
3511                 case IMAGEX_NO_ACLS_OPTION:
3512                         default_add_flags |= WIMLIB_ADD_FLAG_NO_ACLS;
3513                         break;
3514                 case IMAGEX_STRICT_ACLS_OPTION:
3515                         default_add_flags |= WIMLIB_ADD_FLAG_STRICT_ACLS;
3516                         break;
3517                 default:
3518                         goto out_usage;
3519                 }
3520         }
3521         argv += optind;
3522         argc -= optind;
3523
3524         if (argc != 1 && argc != 2)
3525                 goto out_usage;
3526         wimfile = argv[0];
3527
3528         ret = wimlib_open_wim(wimfile, open_flags, &wim, imagex_progress_func);
3529         if (ret)
3530                 goto out_free_command_str;
3531
3532         if (argc >= 2) {
3533                 /* Image explicitly specified.  */
3534                 image = wimlib_resolve_image(wim, argv[1]);
3535                 ret = verify_image_exists_and_is_single(image, argv[1],
3536                                                         wimfile);
3537                 if (ret)
3538                         goto out_wimlib_free;
3539         } else {
3540                 /* No image specified; default to image 1, but only if the WIM
3541                  * contains exactly one image.  */
3542                 struct wimlib_wim_info info;
3543
3544                 wimlib_get_wim_info(wim, &info);
3545                 if (info.image_count != 1) {
3546                         imagex_error(T("\"%"TS"\" contains %d images; Please select one."),
3547                                      wimfile, info.image_count);
3548                         wimlib_free(wim);
3549                         goto out_usage;
3550                 }
3551                 image = 1;
3552         }
3553
3554         /* Parse capture configuration file if specified */
3555         if (config_file) {
3556                 size_t config_len;
3557
3558                 config_str = file_get_text_contents(config_file, &config_len);
3559                 if (!config_str) {
3560                         ret = -1;
3561                         goto out_wimlib_free;
3562                 }
3563
3564                 config = alloca(sizeof(*config));
3565                 ret = parse_capture_config(&config_str, config_len, config);
3566                 if (ret)
3567                         goto out_free_config;
3568         } else {
3569                 config = NULL;
3570                 default_add_flags |= WIMLIB_ADD_FLAG_WINCONFIG;
3571         }
3572
3573         /* Read update commands from standard input, or the command string if
3574          * specified.  */
3575         if (command_str) {
3576                 cmd_file_contents = NULL;
3577                 cmds = parse_update_command_file(&command_str, tstrlen(command_str),
3578                                                  &num_cmds);
3579         } else {
3580                 if (isatty(STDIN_FILENO)) {
3581                         tputs(T("Reading update commands from standard input..."));
3582                         recommend_man_page(CMD_UPDATE, stdout);
3583                 }
3584                 cmd_file_contents = stdin_get_text_contents(&cmd_file_nchars);
3585                 if (!cmd_file_contents) {
3586                         ret = -1;
3587                         goto out_free_config;
3588                 }
3589
3590                 /* Parse the update commands */
3591                 cmds = parse_update_command_file(&cmd_file_contents, cmd_file_nchars,
3592                                                  &num_cmds);
3593         }
3594         if (!cmds) {
3595                 ret = -1;
3596                 goto out_free_cmd_file_contents;
3597         }
3598
3599         /* Set default flags and capture config on the update commands */
3600         for (size_t i = 0; i < num_cmds; i++) {
3601                 switch (cmds[i].op) {
3602                 case WIMLIB_UPDATE_OP_ADD:
3603                         cmds[i].add.add_flags |= default_add_flags;
3604                         cmds[i].add.config = config;
3605                         break;
3606                 case WIMLIB_UPDATE_OP_DELETE:
3607                         cmds[i].delete_.delete_flags |= default_delete_flags;
3608                         break;
3609                 default:
3610                         break;
3611                 }
3612         }
3613
3614         /* Execute the update commands */
3615         ret = wimlib_update_image(wim, image, cmds, num_cmds, update_flags,
3616                                   imagex_progress_func);
3617         if (ret)
3618                 goto out_free_cmds;
3619
3620         /* Overwrite the updated WIM */
3621         ret = wimlib_overwrite(wim, write_flags, num_threads,
3622                                imagex_progress_func);
3623 out_free_cmds:
3624         free(cmds);
3625 out_free_cmd_file_contents:
3626         free(cmd_file_contents);
3627 out_free_config:
3628         if (config) {
3629                 free(config->exclusion_pats.pats);
3630                 free(config->exclusion_exception_pats.pats);
3631                 free(config_str);
3632         }
3633 out_wimlib_free:
3634         wimlib_free(wim);
3635 out_free_command_str:
3636         free(command_str);
3637         return ret;
3638
3639 out_usage:
3640         usage(CMD_UPDATE, stderr);
3641 out_err:
3642         ret = -1;
3643         goto out_free_command_str;
3644 }
3645
3646
3647
3648 struct imagex_command {
3649         const tchar *name;
3650         int (*func)(int argc, tchar **argv, int cmd);
3651 };
3652
3653 static const struct imagex_command imagex_commands[] = {
3654         [CMD_APPEND]   = {T("append"),   imagex_capture_or_append},
3655         [CMD_APPLY]    = {T("apply"),    imagex_apply},
3656         [CMD_CAPTURE]  = {T("capture"),  imagex_capture_or_append},
3657         [CMD_DELETE]   = {T("delete"),   imagex_delete},
3658         [CMD_DIR ]     = {T("dir"),      imagex_dir},
3659         [CMD_EXPORT]   = {T("export"),   imagex_export},
3660         [CMD_EXTRACT]  = {T("extract"),  imagex_extract},
3661         [CMD_INFO]     = {T("info"),     imagex_info},
3662         [CMD_JOIN]     = {T("join"),     imagex_join},
3663 #if WIM_MOUNTING_SUPPORTED
3664         [CMD_MOUNT]    = {T("mount"),    imagex_mount_rw_or_ro},
3665         [CMD_MOUNTRW]  = {T("mountrw"),  imagex_mount_rw_or_ro},
3666 #endif
3667         [CMD_OPTIMIZE] = {T("optimize"), imagex_optimize},
3668         [CMD_SPLIT]    = {T("split"),    imagex_split},
3669 #if WIM_MOUNTING_SUPPORTED
3670         [CMD_UNMOUNT]  = {T("unmount"),  imagex_unmount},
3671 #endif
3672         [CMD_UPDATE]   = {T("update"),   imagex_update},
3673 };
3674
3675 static const tchar *usage_strings[] = {
3676 [CMD_APPEND] =
3677 T(
3678 "    %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3679 "                    [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot]\n"
3680 "                    [--check] [--nocheck] [--flags EDITION_ID] [--verbose]\n"
3681 "                    [--dereference] [--config=FILE] [--threads=NUM_THREADS]\n"
3682 "                    [--source-list] [--no-acls] [--strict-acls] [--rpfix]\n"
3683 "                    [--norpfix] [--unix-data] [--pipable]\n"
3684 "                    [--update-of=[WIMFILE:]IMAGE]\n"
3685 ),
3686 [CMD_APPLY] =
3687 T(
3688 "    %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME | all)]\n"
3689 "                    (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
3690 "                    [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n"
3691 "                    [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3692 "                    [--include-invalid-names]\n"
3693 ),
3694 [CMD_CAPTURE] =
3695 T(
3696 "    %"TS" (DIRECTORY | NTFS_VOLUME) WIMFILE\n"
3697 "                    [IMAGE_NAME [IMAGE_DESCRIPTION]] [--boot]\n"
3698 "                    [--check] [--nocheck] [--compress=TYPE]\n"
3699 "                    [--flags EDITION_ID] [--verbose] [--dereference]\n"
3700 "                    [--config=FILE] [--threads=NUM_THREADS] [--source-list]\n"
3701 "                    [--no-acls] [--strict-acls] [--rpfix] [--norpfix]\n"
3702 "                    [--unix-data] [--pipable] [--update-of=[WIMFILE:]IMAGE]\n"
3703 "                    [--delta-from=WIMFILE]\n"
3704 ),
3705 [CMD_DELETE] =
3706 T(
3707 "    %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check]\n"
3708 "                    [--soft]\n"
3709 ),
3710 [CMD_DIR] =
3711 T(
3712 "    %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--path=PATH]\n"
3713 ),
3714 [CMD_EXPORT] =
3715 T(
3716 "    %"TS" SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
3717 "                    DEST_WIMFILE [DEST_IMAGE_NAME [DEST_IMAGE_DESCRIPTION]]\n"
3718 "                    [--boot] [--check] [--nocheck] [--compress=TYPE]\n"
3719 "                    [--ref=\"GLOB\"] [--threads=NUM_THREADS] [--rebuild]\n"
3720 "                    [--pipable] [--not-pipable]\n"
3721 ),
3722 [CMD_EXTRACT] =
3723 T(
3724 "    %"TS" WIMFILE (IMAGE_NUM | IMAGE_NAME) [PATH...]\n"
3725 "                    [--check] [--ref=\"GLOB\"] [--verbose] [--unix-data]\n"
3726 "                    [--no-acls] [--strict-acls] [--to-stdout]\n"
3727 "                    [--dest-dir=CMD_DIR] [--include-invalid-names]\n"
3728 ),
3729 [CMD_INFO] =
3730 T(
3731 "    %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME) [NEW_NAME\n"
3732 "                    [NEW_DESC]]] [--boot] [--check] [--nocheck] [--header]\n"
3733 "                    [--lookup-table] [--xml] [--extract-xml FILE]\n"
3734 "                    [--metadata]\n"
3735 ),
3736 [CMD_JOIN] =
3737 T(
3738 "    %"TS" OUT_WIMFILE SPLIT_WIM_PART... [--check]\n"
3739 ),
3740 #if WIM_MOUNTING_SUPPORTED
3741 [CMD_MOUNT] =
3742 T(
3743 "    %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3744 "                    [--check] [--debug] [--streams-interface=INTERFACE]\n"
3745 "                    [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n"
3746 ),
3747 [CMD_MOUNTRW] =
3748 T(
3749 "    %"TS" WIMFILE [(IMAGE_NUM | IMAGE_NAME)] DIRECTORY\n"
3750 "                    [--check] [--debug] [--streams-interface=INTERFACE]\n"
3751 "                    [--staging-dir=CMD_DIR] [--unix-data] [--allow-other]\n"
3752 ),
3753 #endif
3754 [CMD_OPTIMIZE] =
3755 T(
3756 "    %"TS" WIMFILE [--check] [--nocheck] [--recompress]\n"
3757 "                    [--recompress-slow] [--threads=NUM_THREADS] [--pipable]\n"
3758 "                    [--not-pipable]\n"
3759 ),
3760 [CMD_SPLIT] =
3761 T(
3762 "    %"TS" WIMFILE SPLIT_WIM_PART_1 PART_SIZE_MB [--check]\n"
3763 ),
3764 #if WIM_MOUNTING_SUPPORTED
3765 [CMD_UNMOUNT] =
3766 T(
3767 "    %"TS" DIRECTORY [--commit] [--check] [--rebuild] [--lazy]\n"
3768 ),
3769 #endif
3770 [CMD_UPDATE] =
3771 T(
3772 "    %"TS" WIMFILE [IMAGE_NUM | IMAGE_NAME] [--check] [--rebuild]\n"
3773 "                    [--threads=NUM_THREADS] [DEFAULT_ADD_OPTIONS]\n"
3774 "                    [DEFAULT_DELETE_OPTIONS] [--command=STRING] [< CMDFILE]\n"
3775 ),
3776 };
3777
3778 static const tchar *invocation_name;
3779 static int invocation_cmd = CMD_NONE;
3780
3781 static const tchar *get_cmd_string(int cmd, bool nospace)
3782 {
3783         static tchar buf[50];
3784         if (cmd == CMD_NONE) {
3785                 tsprintf(buf, T("%"TS), T(IMAGEX_PROGNAME));
3786         } else if (invocation_cmd != CMD_NONE) {
3787                 tsprintf(buf, T("wim%"TS), imagex_commands[cmd].name);
3788         } else {
3789                 const tchar *format;
3790
3791                 if (nospace)
3792                         format = T("%"TS"-%"TS"");
3793                 else
3794                         format = T("%"TS" %"TS"");
3795                 tsprintf(buf, format, invocation_name, imagex_commands[cmd].name);
3796         }
3797         return buf;
3798 }
3799
3800 static void
3801 version(void)
3802 {
3803         static const tchar *s =
3804         T(
3805 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
3806 "Copyright (C) 2012, 2013 Eric Biggers\n"
3807 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
3808 "This is free software: you are free to change and redistribute it.\n"
3809 "There is NO WARRANTY, to the extent permitted by law.\n"
3810 "\n"
3811 "Report bugs to "PACKAGE_BUGREPORT".\n"
3812         );
3813         tfputs(s, stdout);
3814 }
3815
3816
3817 static void
3818 help_or_version(int argc, tchar **argv, int cmd)
3819 {
3820         int i;
3821         const tchar *p;
3822
3823         for (i = 1; i < argc; i++) {
3824                 p = argv[i];
3825                 if (p[0] == T('-') && p[1] == T('-')) {
3826                         p += 2;
3827                         if (!tstrcmp(p, T("help"))) {
3828                                 if (cmd == CMD_NONE)
3829                                         usage_all(stdout);
3830                                 else
3831                                         usage(cmd, stdout);
3832                                 exit(0);
3833                         } else if (!tstrcmp(p, T("version"))) {
3834                                 version();
3835                                 exit(0);
3836                         }
3837                 }
3838         }
3839 }
3840
3841 static void
3842 print_usage_string(int cmd, FILE *fp)
3843 {
3844         tfprintf(fp, usage_strings[cmd], get_cmd_string(cmd, false));
3845 }
3846
3847 static void
3848 recommend_man_page(int cmd, FILE *fp)
3849 {
3850         const tchar *format_str;
3851 #ifdef __WIN32__
3852         format_str = T("See %"TS".pdf in the doc directory for more details.\n");
3853 #else
3854         format_str = T("Try `man %"TS"' for more details.\n");
3855 #endif
3856         tfprintf(fp, format_str, get_cmd_string(cmd, true));
3857 }
3858
3859 static void
3860 usage(int cmd, FILE *fp)
3861 {
3862         tfprintf(fp, T("Usage:\n"));
3863         print_usage_string(cmd, fp);
3864         tfprintf(fp, T("\n"));
3865         recommend_man_page(cmd, fp);
3866 }
3867
3868 static void
3869 usage_all(FILE *fp)
3870 {
3871         tfprintf(fp, T("Usage:\n"));
3872         for (int cmd = 0; cmd < CMD_MAX; cmd++) {
3873                 print_usage_string(cmd, fp);
3874                 tfprintf(fp, T("\n"));
3875         }
3876         static const tchar *extra =
3877         T(
3878 "    %"TS" --help\n"
3879 "    %"TS" --version\n"
3880 "\n"
3881 "    The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
3882 "\n"
3883         );
3884         tfprintf(fp, extra, invocation_name, invocation_name);
3885         recommend_man_page(CMD_NONE, fp);
3886 }
3887
3888 /* Entry point for wimlib's ImageX implementation.  On UNIX the command
3889  * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
3890  * something else), while an Windows the command arguments will be UTF-16LE
3891  * encoded 'wchar_t' strings. */
3892 int
3893 #ifdef __WIN32__
3894 wmain(int argc, wchar_t **argv, wchar_t **envp)
3895 #else
3896 main(int argc, char **argv)
3897 #endif
3898 {
3899         int ret;
3900         int init_flags = 0;
3901         int cmd;
3902
3903         imagex_info_file = stdout;
3904         invocation_name = tbasename(argv[0]);
3905
3906 #ifndef __WIN32__
3907         if (getenv("WIMLIB_IMAGEX_USE_UTF8")) {
3908                 init_flags |= WIMLIB_INIT_FLAG_ASSUME_UTF8;
3909         } else {
3910                 char *codeset;
3911
3912                 setlocale(LC_ALL, "");
3913                 codeset = nl_langinfo(CODESET);
3914                 if (!strstr(codeset, "UTF-8") &&
3915                     !strstr(codeset, "UTF8") &&
3916                     !strstr(codeset, "utf-8") &&
3917                     !strstr(codeset, "utf8"))
3918                 {
3919                         fprintf(stderr,
3920 "WARNING: Running %"TS" in a UTF-8 locale is recommended!\n"
3921 "         Maybe try: `export LANG=en_US.UTF-8'?\n"
3922 "         Alternatively, set the environmental variable WIMLIB_IMAGEX_USE_UTF8\n"
3923 "         to any value to force wimlib to use UTF-8.\n",
3924                         invocation_name);
3925
3926                 }
3927         }
3928 #endif /* !__WIN32__ */
3929
3930         /* Allow being invoked as wimCOMMAND (e.g. wimapply).  */
3931         cmd = CMD_NONE;
3932         if (!tstrncmp(invocation_name, T("wim"), 3) &&
3933             tstrcmp(invocation_name, T(IMAGEX_PROGNAME))) {
3934                 for (int i = 0; i < CMD_MAX; i++) {
3935                         if (!tstrcmp(invocation_name + 3,
3936                                      imagex_commands[i].name))
3937                         {
3938                                 invocation_cmd = i;
3939                                 cmd = i;
3940                                 break;
3941                         }
3942                 }
3943         }
3944
3945         /* Unless already known from the invocation name, determine which
3946          * command was specified.  */
3947         if (cmd == CMD_NONE) {
3948                 if (argc < 2) {
3949                         imagex_error(T("No command specified!\n"));
3950                         usage_all(stderr);
3951                         exit(2);
3952                 }
3953                 for (int i = 0; i < CMD_MAX; i++) {
3954                         if (!tstrcmp(argv[1], imagex_commands[i].name)) {
3955                                 cmd = i;
3956                                 break;
3957                         }
3958                 }
3959                 if (cmd != CMD_NONE) {
3960                         argc--;
3961                         argv++;
3962                 }
3963         }
3964
3965         /* Handle --help and --version.  --help can be either for the program as
3966          * a whole (cmd == CMD_NONE) or just for a specific command (cmd !=
3967          * CMD_NONE).  Note: help_or_version() will not return if a --help or
3968          * --version argument was found.  */
3969         help_or_version(argc, argv, cmd);
3970
3971         /* Bail if a valid command was not specified.  */
3972         if (cmd == CMD_NONE) {
3973                 imagex_error(T("Unrecognized command: `%"TS"'\n"), argv[1]);
3974                 usage_all(stderr);
3975                 exit(2);
3976         }
3977
3978         /* Enable warning and error messages in wimlib be more user-friendly.
3979          * */
3980         wimlib_set_print_errors(true);
3981
3982         /* Initialize wimlib.  */
3983         ret = wimlib_global_init(init_flags);
3984         if (ret)
3985                 goto out_check_status;
3986
3987         /* Call the command handler function.  */
3988         ret = imagex_commands[cmd].func(argc, argv, cmd);
3989
3990         /* Check for error writing to standard output, especially since for some
3991          * commands, writing to standard output is part of the program's actual
3992          * behavior and not just for informational purposes.  */
3993         if (ferror(stdout) || fclose(stdout)) {
3994                 imagex_error_with_errno(T("error writing to standard output"));
3995                 if (ret == 0)
3996                         ret = -1;
3997         }
3998 out_check_status:
3999         /* Exit status (ret):  -1 indicates an error found by 'wimlib-imagex'
4000          * itself (not by wimlib).  0 indicates success.  > 0 indicates a wimlib
4001          * error code from which an error message can be printed.  */
4002         if (ret > 0) {
4003                 imagex_error(T("Exiting with error code %d:\n"
4004                                "       %"TS"."), ret,
4005                              wimlib_get_error_string(ret));
4006                 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
4007                         imagex_error_with_errno(T("errno"));
4008         }
4009         /* Make wimlib free any resources it's holding (although this is not
4010          * strictly necessary because the process is ending anyway).  */
4011         wimlib_global_cleanup();
4012         return ret;
4013 }