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