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