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