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