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