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