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