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