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