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