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