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