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