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