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