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