4 * Use wimlib to create, modify, extract, mount, unmount, or display information
9 * Copyright (C) 2012, 2013 Eric Biggers
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.
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.
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/>.
26 #include "wimlib_tchar.h"
47 # include "imagex-win32.h"
48 # define tbasename win32_wbasename
49 # define tglob win32_wglob
53 # include <langinfo.h>
54 # define tbasename basename
59 #define ARRAY_LEN(array) (sizeof(array) / sizeof(array[0]))
61 #define for_opt(c, opts) while ((c = getopt_long_only(argc, (tchar**)argv, T(""), \
80 static void usage(int cmd_type);
81 static void usage_all();
84 static const tchar *usage_strings[] = {
87 IMAGEX_PROGNAME" append (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
88 " [DESCRIPTION] [--boot] [--check] [--flags EDITION_ID]\n"
89 " [--verbose] [--dereference] [--config=FILE]\n"
90 " [--threads=NUM_THREADS] [--rebuild] [--unix-data]\n"
91 " [--source-list] [--no-acls] [--strict-acls]\n"
95 IMAGEX_PROGNAME" apply WIMFILE [IMAGE_NUM | IMAGE_NAME | all]\n"
96 " (DIRECTORY | NTFS_VOLUME) [--check] [--hardlink]\n"
97 " [--symlink] [--verbose] [--ref=\"GLOB\"] [--unix-data]\n"
98 " [--no-acls] [--strict-acls]\n"
102 IMAGEX_PROGNAME" capture (DIRECTORY | NTFS_VOLUME) WIMFILE [IMAGE_NAME]\n"
103 " [DESCRIPTION] [--boot] [--check] [--compress=TYPE]\n"
104 " [--flags EDITION_ID] [--verbose] [--dereference]\n"
105 " [--config=FILE] [--threads=NUM_THREADS] [--unix-data]\n"
106 " [--source-list] [--no-acls] [--strict-acls]\n"
110 IMAGEX_PROGNAME" delete WIMFILE (IMAGE_NUM | IMAGE_NAME | all) [--check] [--soft]\n"
114 IMAGEX_PROGNAME" dir WIMFILE (IMAGE_NUM | IMAGE_NAME | all)\n"
118 IMAGEX_PROGNAME" export SRC_WIMFILE (SRC_IMAGE_NUM | SRC_IMAGE_NAME | all ) \n"
119 " DEST_WIMFILE [DEST_IMAGE_NAME] [DEST_IMAGE_DESCRIPTION]\n"
120 " [--boot] [--check] [--compress=TYPE] [--ref=\"GLOB\"]\n"
121 " [--threads=NUM_THREADS] [--rebuild]\n"
125 IMAGEX_PROGNAME" info WIMFILE [IMAGE_NUM | IMAGE_NAME] [NEW_NAME]\n"
126 " [NEW_DESC] [--boot] [--check] [--header] [--lookup-table]\n"
127 " [--xml] [--extract-xml FILE] [--metadata]\n"
131 IMAGEX_PROGNAME" join [--check] WIMFILE SPLIT_WIM...\n"
135 IMAGEX_PROGNAME" mount WIMFILE (IMAGE_NUM | IMAGE_NAME) DIRECTORY\n"
136 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
137 " [--ref=\"GLOB\"] [--unix-data] [--allow-other]\n"
141 IMAGEX_PROGNAME" mountrw WIMFILE [IMAGE_NUM | IMAGE_NAME] DIRECTORY\n"
142 " [--check] [--debug] [--streams-interface=INTERFACE]\n"
143 " [--staging-dir=DIR] [--unix-data] [--allow-other]\n"
147 IMAGEX_PROGNAME" optimize WIMFILE [--check] [--recompress]\n"
151 IMAGEX_PROGNAME" split WIMFILE SPLIT_WIMFILE PART_SIZE_MB [--check]\n"
155 IMAGEX_PROGNAME" unmount DIRECTORY [--commit] [--check] [--rebuild]\n"
160 IMAGEX_ALLOW_OTHER_OPTION,
163 IMAGEX_COMMIT_OPTION,
164 IMAGEX_COMPRESS_OPTION,
165 IMAGEX_CONFIG_OPTION,
167 IMAGEX_DEREFERENCE_OPTION,
168 IMAGEX_EXTRACT_XML_OPTION,
170 IMAGEX_HARDLINK_OPTION,
171 IMAGEX_HEADER_OPTION,
172 IMAGEX_LOOKUP_TABLE_OPTION,
173 IMAGEX_METADATA_OPTION,
174 IMAGEX_NO_ACLS_OPTION,
175 IMAGEX_REBULID_OPTION,
176 IMAGEX_RECOMPRESS_OPTION,
179 IMAGEX_SOURCE_LIST_OPTION,
180 IMAGEX_STAGING_DIR_OPTION,
181 IMAGEX_STREAMS_INTERFACE_OPTION,
182 IMAGEX_STRICT_ACLS_OPTION,
183 IMAGEX_SYMLINK_OPTION,
184 IMAGEX_THREADS_OPTION,
185 IMAGEX_UNIX_DATA_OPTION,
186 IMAGEX_VERBOSE_OPTION,
190 static const struct option apply_options[] = {
191 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
192 {T("hardlink"), no_argument, NULL, IMAGEX_HARDLINK_OPTION},
193 {T("symlink"), no_argument, NULL, IMAGEX_SYMLINK_OPTION},
194 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
195 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
196 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
197 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
198 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
199 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
202 static const struct option capture_or_append_options[] = {
203 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
204 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
205 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
206 {T("config"), required_argument, NULL, IMAGEX_CONFIG_OPTION},
207 {T("dereference"), no_argument, NULL, IMAGEX_DEREFERENCE_OPTION},
208 {T("flags"), required_argument, NULL, IMAGEX_FLAGS_OPTION},
209 {T("verbose"), no_argument, NULL, IMAGEX_VERBOSE_OPTION},
210 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
211 {T("rebuild"), no_argument, NULL, IMAGEX_REBULID_OPTION},
212 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
213 {T("source-list"), no_argument, NULL, IMAGEX_SOURCE_LIST_OPTION},
214 {T("noacls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
215 {T("no-acls"), no_argument, NULL, IMAGEX_NO_ACLS_OPTION},
216 {T("strict-acls"), no_argument, NULL, IMAGEX_STRICT_ACLS_OPTION},
219 static const struct option delete_options[] = {
220 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
221 {T("soft"), no_argument, NULL, IMAGEX_SOFT_OPTION},
225 static const struct option export_options[] = {
226 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
227 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
228 {T("compress"), required_argument, NULL, IMAGEX_COMPRESS_OPTION},
229 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
230 {T("threads"), required_argument, NULL, IMAGEX_THREADS_OPTION},
231 {T("rebuild"), no_argument, NULL, IMAGEX_REBULID_OPTION},
235 static const struct option info_options[] = {
236 {T("boot"), no_argument, NULL, IMAGEX_BOOT_OPTION},
237 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
238 {T("extract-xml"), required_argument, NULL, IMAGEX_EXTRACT_XML_OPTION},
239 {T("header"), no_argument, NULL, IMAGEX_HEADER_OPTION},
240 {T("lookup-table"), no_argument, NULL, IMAGEX_LOOKUP_TABLE_OPTION},
241 {T("metadata"), no_argument, NULL, IMAGEX_METADATA_OPTION},
242 {T("xml"), no_argument, NULL, IMAGEX_XML_OPTION},
246 static const struct option join_options[] = {
247 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
251 static const struct option mount_options[] = {
252 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
253 {T("debug"), no_argument, NULL, IMAGEX_DEBUG_OPTION},
254 {T("streams-interface"), required_argument, NULL, IMAGEX_STREAMS_INTERFACE_OPTION},
255 {T("ref"), required_argument, NULL, IMAGEX_REF_OPTION},
256 {T("staging-dir"), required_argument, NULL, IMAGEX_STAGING_DIR_OPTION},
257 {T("unix-data"), no_argument, NULL, IMAGEX_UNIX_DATA_OPTION},
258 {T("allow-other"), no_argument, NULL, IMAGEX_ALLOW_OTHER_OPTION},
262 static const struct option optimize_options[] = {
263 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
264 {T("recompress"), no_argument, NULL, IMAGEX_RECOMPRESS_OPTION},
268 static const struct option split_options[] = {
269 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
273 static const struct option unmount_options[] = {
274 {T("commit"), no_argument, NULL, IMAGEX_COMMIT_OPTION},
275 {T("check"), no_argument, NULL, IMAGEX_CHECK_OPTION},
276 {T("rebuild"), no_argument, NULL, IMAGEX_REBULID_OPTION},
282 /* Print formatted error message to stderr. */
284 imagex_error(const tchar *format, ...)
287 va_start(va, format);
288 tfputs(T("ERROR: "), stderr);
289 tvfprintf(stderr, format, va);
290 tputc(T('\n'), stderr);
294 /* Print formatted error message to stderr. */
296 imagex_error_with_errno(const tchar *format, ...)
298 int errno_save = errno;
300 va_start(va, format);
301 tfputs(T("ERROR: "), stderr);
302 tvfprintf(stderr, format, va);
303 tfprintf(stderr, T(": %"TS"\n"), tstrerror(errno_save));
308 verify_image_exists(int image, const tchar *image_name, const tchar *wim_name)
310 if (image == WIMLIB_NO_IMAGE) {
311 imagex_error(T("\"%"TS"\" is not a valid image in \"%"TS"\"!\n"
312 " Please specify a 1-based image index or "
314 " You may use `"IMAGEX_PROGNAME" info' to list the images "
315 "contained in a WIM."),
316 image_name, wim_name);
323 verify_image_is_single(int image)
325 if (image == WIMLIB_ALL_IMAGES) {
326 imagex_error(T("Cannot specify all images for this action!"));
333 verify_image_exists_and_is_single(int image, const tchar *image_name,
334 const tchar *wim_name)
337 ret = verify_image_exists(image, image_name, wim_name);
339 ret = verify_image_is_single(image);
343 /* Parse the argument to --compress */
345 get_compression_type(const tchar *optarg)
347 if (!tstrcasecmp(optarg, T("maximum")) || !tstrcasecmp(optarg, T("lzx")))
348 return WIMLIB_COMPRESSION_TYPE_LZX;
349 else if (!tstrcasecmp(optarg, T("fast")) || !tstrcasecmp(optarg, T("xpress")))
350 return WIMLIB_COMPRESSION_TYPE_XPRESS;
351 else if (!tstrcasecmp(optarg, T("none")))
352 return WIMLIB_COMPRESSION_TYPE_NONE;
354 imagex_error(T("Invalid compression type \"%"TS"\"! Must be "
355 "\"maximum\", \"fast\", or \"none\"."), optarg);
356 return WIMLIB_COMPRESSION_TYPE_INVALID;
360 /* Returns the size of a file given its name, or -1 if the file does not exist
361 * or its size cannot be determined. */
363 file_get_size(const tchar *filename)
366 if (tstat(filename, &st) == 0)
372 static const tchar *default_capture_config =
378 "\\System Volume Information\n"
382 "[CompressionExclusionList]\n"
386 "\\WINDOWS\\inf\\*.pnf\n"
390 PARSE_FILENAME_SUCCESS = 0,
391 PARSE_FILENAME_FAILURE = 1,
392 PARSE_FILENAME_NONE = 2,
396 * Parses a filename in the source list file format. (See the man page for
397 * 'wimlib-imagex capture' for details on this format and the meaning.)
398 * Accepted formats for filenames are an unquoted string (whitespace-delimited),
399 * or a double or single-quoted string.
401 * @line_p: Pointer to the pointer to the line of data. Will be updated
402 * to point past the filename iff the return value is
403 * PARSE_FILENAME_SUCCESS. If *len_p > 0, (*line_p)[*len_p - 1] must
406 * @len_p: @len_p initially stores the length of the line of data, which may
407 * be 0, and it will be updated to the number of bytes remaining in
408 * the line iff the return value is PARSE_FILENAME_SUCCESS.
410 * @fn_ret: Iff the return value is PARSE_FILENAME_SUCCESS, a pointer to the
411 * parsed filename will be returned here.
413 * Returns: PARSE_FILENAME_SUCCESS if a filename was successfully parsed; or
414 * PARSE_FILENAME_FAILURE if the data was invalid due to a missing
415 * closing quote; or PARSE_FILENAME_NONE if the line ended before the
416 * beginning of a filename was found.
419 parse_filename(tchar **line_p, size_t *len_p, tchar **fn_ret)
422 tchar *line = *line_p;
426 /* Skip leading whitespace */
429 return PARSE_FILENAME_NONE;
430 if (!istspace(*line) && *line != T('\0'))
436 if (quote_char == T('"') || quote_char == T('\'')) {
437 /* Quoted filename */
441 line = tmemchr(line, quote_char, len);
443 imagex_error(T("Missing closing quote: %"TS), fn - 1);
444 return PARSE_FILENAME_FAILURE;
447 /* Unquoted filename. Go until whitespace. Line is terminated
448 * by '\0', so no need to check 'len'. */
452 } while (!istspace(*line) && *line != T('\0'));
459 return PARSE_FILENAME_SUCCESS;
462 /* Parses a line of data (not an empty line or comment) in the source list file
463 * format. (See the man page for 'wimlib-imagex capture' for details on this
464 * format and the meaning.)
466 * @line: Line of data to be parsed. line[len - 1] must be '\0', unless
467 * len == 0. The data in @line will be modified by this function call.
469 * @len: Length of the line of data.
471 * @source: On success, the capture source and target described by the line is
472 * written into this destination. Note that it will contain pointers
473 * to data in the @line array.
475 * Returns true if the line was valid; false otherwise. */
477 parse_source_list_line(tchar *line, size_t len,
478 struct wimlib_capture_source *source)
482 ret = parse_filename(&line, &len, &source->fs_source_path);
483 if (ret != PARSE_FILENAME_SUCCESS)
485 ret = parse_filename(&line, &len, &source->wim_target_path);
486 if (ret == PARSE_FILENAME_NONE)
487 source->wim_target_path = source->fs_source_path;
488 return ret != PARSE_FILENAME_FAILURE;
491 /* Returns %true if the given line of length @len > 0 is a comment or empty line
492 * in the source list file format. */
494 is_comment_line(const tchar *line, size_t len)
499 if (!istspace(*line) && *line != T('\0'))
508 /* Parses a file in the source list format. (See the man page for
509 * 'wimlib-imagex capture' for details on this format and the meaning.)
511 * @source_list_contents: Contents of the source list file. Note that this
512 * buffer will be modified to save memory allocations,
513 * and cannot be freed until the returned array of
514 * wimlib_capture_source's has also been freed.
516 * @source_list_nbytes: Number of bytes of data in the @source_list_contents
519 * @nsources_ret: On success, the length of the returned array is
522 * Returns: An array of `struct wimlib_capture_source's that can be passed to
523 * the wimlib_add_image_multisource() function to specify how a WIM image is to
525 static struct wimlib_capture_source *
526 parse_source_list(tchar **source_list_contents_p, size_t source_list_nchars,
527 size_t *nsources_ret)
531 struct wimlib_capture_source *sources;
533 tchar *source_list_contents = *source_list_contents_p;
536 for (i = 0; i < source_list_nchars; i++)
537 if (source_list_contents[i] == T('\n'))
540 /* Handle last line not terminated by a newline */
541 if (source_list_nchars != 0 &&
542 source_list_contents[source_list_nchars - 1] != T('\n'))
544 source_list_contents = realloc(source_list_contents,
545 (source_list_nchars + 1) * sizeof(tchar));
546 if (!source_list_contents)
548 source_list_contents[source_list_nchars] = T('\n');
549 *source_list_contents_p = source_list_contents;
550 source_list_nchars++;
554 /* Always allocate at least 1 slot, just in case the implementation of
555 * calloc() returns NULL if 0 bytes are requested. */
556 sources = calloc(nlines ?: 1, sizeof(*sources));
559 p = source_list_contents;
561 for (i = 0; i < nlines; i++) {
562 /* XXX: Could use rawmemchr() here instead, but it may not be
563 * available on all platforms. */
564 tchar *endp = tmemchr(p, T('\n'), source_list_nchars);
565 size_t len = endp - p + 1;
567 if (!is_comment_line(p, len)) {
568 if (!parse_source_list_line(p, len, &sources[j++])) {
579 imagex_error(T("out of memory"));
583 /* Reads the contents of a file into memory. */
585 file_get_contents(const tchar *filename, size_t *len_ret)
592 if (tstat(filename, &stbuf) != 0) {
593 imagex_error_with_errno(T("Failed to stat the file \"%"TS"\""), filename);
598 fp = tfopen(filename, T("rb"));
600 imagex_error_with_errno(T("Failed to open the file \"%"TS"\""), filename);
606 imagex_error(T("Failed to allocate buffer of %zu bytes to hold "
607 "contents of file \"%"TS"\""), len, filename);
610 if (fread(buf, 1, len, fp) != len) {
611 imagex_error_with_errno(T("Failed to read %zu bytes from the "
612 "file \"%"TS"\""), len, filename);
626 /* Read standard input until EOF and return the full contents in a malloc()ed
627 * buffer and the number of bytes of data in @len_ret. Returns NULL on read
630 stdin_get_contents(size_t *len_ret)
632 /* stdin can, of course, be a pipe or other non-seekable file, so the
633 * total length of the data cannot be pre-determined */
635 size_t newlen = 1024;
639 char *p = realloc(buf, newlen);
640 size_t bytes_read, bytes_to_read;
642 imagex_error(T("out of memory while reading stdin"));
646 bytes_to_read = newlen - pos;
647 bytes_read = fread(&buf[pos], 1, bytes_to_read, stdin);
649 if (bytes_read != bytes_to_read) {
654 imagex_error_with_errno(T("error reading stdin"));
668 translate_text_to_tstr(char *text, size_t num_bytes,
669 size_t *num_tchars_ret)
672 /* On non-Windows, assume an ASCII-compatible encoding, such as UTF-8.
674 *num_tchars_ret = num_bytes;
676 #else /* !__WIN32__ */
677 /* On Windows, translate the text to UTF-16LE */
681 if (num_bytes >= 2 &&
682 ((text[0] == 0xff && text[1] == 0xfe) ||
683 (text[0] <= 0x7f && text[1] == 0x00)))
685 /* File begins with 0xfeff, the BOM for UTF-16LE, or it begins
686 * with something that looks like an ASCII character encoded as
687 * a UTF-16LE code unit. Assume the file is encoded as
688 * UTF-16LE. This is not a 100% reliable check. */
689 num_wchars = num_bytes / 2;
690 text_wstr = (wchar_t*)text;
692 /* File does not look like UTF-16LE. Assume it is encoded in
693 * the current Windows code page. I think these are always
694 * ASCII-compatible, so any so-called "plain-text" (ASCII) files
695 * should work as expected. */
696 text_wstr = win32_mbs_to_wcs(text,
701 *num_tchars_ret = num_wchars;
703 #endif /* __WIN32__ */
707 file_get_text_contents(const tchar *filename, size_t *num_tchars_ret)
712 contents = file_get_contents(filename, &num_bytes);
715 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
719 stdin_get_text_contents(size_t *num_tchars_ret)
724 contents = stdin_get_contents(&num_bytes);
727 return translate_text_to_tstr(contents, num_bytes, num_tchars_ret);
730 /* Return 0 if a path names a file to which the current user has write access;
731 * -1 otherwise (and print an error message). */
733 file_writable(const tchar *path)
736 ret = taccess(path, W_OK);
738 imagex_error_with_errno(T("Can't modify \"%"TS"\""), path);
742 #define TO_PERCENT(numerator, denominator) \
743 (((denominator) == 0) ? 0 : ((numerator) * 100 / (denominator)))
745 /* Given an enumerated value for WIM compression type, return a descriptive
748 get_data_type(int ctype)
751 case WIMLIB_COMPRESSION_TYPE_NONE:
752 return T("uncompressed");
753 case WIMLIB_COMPRESSION_TYPE_LZX:
754 return T("LZX-compressed");
755 case WIMLIB_COMPRESSION_TYPE_XPRESS:
756 return T("XPRESS-compressed");
761 /* Progress callback function passed to various wimlib functions. */
763 imagex_progress_func(enum wimlib_progress_msg msg,
764 const union wimlib_progress_info *info)
766 unsigned percent_done;
768 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
769 percent_done = TO_PERCENT(info->write_streams.completed_bytes,
770 info->write_streams.total_bytes);
771 if (info->write_streams.completed_streams == 0) {
772 const tchar *data_type;
774 data_type = get_data_type(info->write_streams.compression_type);
775 tprintf(T("Writing %"TS" data using %u thread%"TS"\n"),
776 data_type, info->write_streams.num_threads,
777 (info->write_streams.num_threads == 1) ? T("") : T("s"));
779 tprintf(T("\r%"PRIu64" MiB of %"PRIu64" MiB (uncompressed) "
780 "written (%u%% done)"),
781 info->write_streams.completed_bytes >> 20,
782 info->write_streams.total_bytes >> 20,
784 if (info->write_streams.completed_bytes >= info->write_streams.total_bytes)
787 case WIMLIB_PROGRESS_MSG_SCAN_BEGIN:
788 tprintf(T("Scanning \"%"TS"\""), info->scan.source);
789 if (*info->scan.wim_target_path) {
790 tprintf(T(" (loading as WIM path: \"/%"TS"\")...\n"),
791 info->scan.wim_target_path);
793 tprintf(T(" (loading as root of WIM image)...\n"));
796 case WIMLIB_PROGRESS_MSG_SCAN_DENTRY:
797 if (info->scan.excluded)
798 tprintf(T("Excluding \"%"TS"\" from capture\n"), info->scan.cur_path);
800 tprintf(T("Scanning \"%"TS"\"\n"), info->scan.cur_path);
802 /*case WIMLIB_PROGRESS_MSG_SCAN_END:*/
804 case WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY:
805 percent_done = TO_PERCENT(info->integrity.completed_bytes,
806 info->integrity.total_bytes);
807 tprintf(T("\rVerifying integrity of \"%"TS"\": %"PRIu64" MiB "
808 "of %"PRIu64" MiB (%u%%) done"),
809 info->integrity.filename,
810 info->integrity.completed_bytes >> 20,
811 info->integrity.total_bytes >> 20,
813 if (info->integrity.completed_bytes == info->integrity.total_bytes)
816 case WIMLIB_PROGRESS_MSG_CALC_INTEGRITY:
817 percent_done = TO_PERCENT(info->integrity.completed_bytes,
818 info->integrity.total_bytes);
819 tprintf(T("\rCalculating integrity table for WIM: %"PRIu64" MiB "
820 "of %"PRIu64" MiB (%u%%) done"),
821 info->integrity.completed_bytes >> 20,
822 info->integrity.total_bytes >> 20,
824 if (info->integrity.completed_bytes == info->integrity.total_bytes)
827 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN:
828 tprintf(T("Applying image %d (%"TS") from \"%"TS"\" "
829 "to %"TS" \"%"TS"\"\n"),
831 info->extract.image_name,
832 info->extract.wimfile_name,
833 ((info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) ?
834 T("NTFS volume") : T("directory")),
835 info->extract.target);
837 /*case WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN:*/
838 /*tprintf(T("Applying directory structure to %"TS"\n"),*/
839 /*info->extract.target);*/
841 case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
842 percent_done = TO_PERCENT(info->extract.completed_bytes,
843 info->extract.total_bytes);
844 tprintf(T("\rExtracting files: "
845 "%"PRIu64" MiB of %"PRIu64" MiB (%u%%) done"),
846 info->extract.completed_bytes >> 20,
847 info->extract.total_bytes >> 20,
849 if (info->extract.completed_bytes >= info->extract.total_bytes)
852 case WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY:
853 tprintf(T("%"TS"\n"), info->extract.cur_path);
855 case WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS:
856 tprintf(T("Setting timestamps on all extracted files...\n"));
858 case WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END:
859 if (info->extract.extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
860 tprintf(T("Unmounting NTFS volume \"%"TS"\"...\n"),
861 info->extract.target);
864 case WIMLIB_PROGRESS_MSG_JOIN_STREAMS:
865 percent_done = TO_PERCENT(info->join.completed_bytes,
866 info->join.total_bytes);
867 tprintf(T("Writing resources from part %u of %u: "
868 "%"PRIu64 " MiB of %"PRIu64" MiB (%u%%) written\n"),
869 (info->join.completed_parts == info->join.total_parts) ?
870 info->join.completed_parts : info->join.completed_parts + 1,
871 info->join.total_parts,
872 info->join.completed_bytes >> 20,
873 info->join.total_bytes >> 20,
876 case WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART:
877 percent_done = TO_PERCENT(info->split.completed_bytes,
878 info->split.total_bytes);
879 tprintf(T("Writing \"%"TS"\": %"PRIu64" MiB of "
880 "%"PRIu64" MiB (%u%%) written\n"),
881 info->split.part_name,
882 info->split.completed_bytes >> 20,
883 info->split.total_bytes >> 20,
886 case WIMLIB_PROGRESS_MSG_SPLIT_END_PART:
887 if (info->split.completed_bytes == info->split.total_bytes) {
888 tprintf(T("Finished writing %u split WIM parts\n"),
889 info->split.cur_part_number);
899 /* Open all the split WIM parts that correspond to a file glob.
901 * @first_part specifies the first part of the split WIM and it may be either
902 * included or omitted from the glob. */
904 open_swms_from_glob(const tchar *swm_glob,
905 const tchar *first_part,
907 WIMStruct ***additional_swms_ret,
908 unsigned *num_additional_swms_ret)
910 unsigned num_additional_swms = 0;
911 WIMStruct **additional_swms = NULL;
915 /* Warning: glob() is replaced in Windows native builds */
916 ret = tglob(swm_glob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
918 if (ret == GLOB_NOMATCH) {
919 imagex_error(T("Found no files for glob \"%"TS"\""),
922 imagex_error_with_errno(T("Failed to process glob \"%"TS"\""),
928 num_additional_swms = globbuf.gl_pathc;
929 additional_swms = calloc(num_additional_swms, sizeof(additional_swms[0]));
930 if (!additional_swms) {
931 imagex_error(T("Out of memory"));
936 for (unsigned i = 0; i < num_additional_swms; i++) {
937 if (tstrcmp(globbuf.gl_pathv[i], first_part) == 0) {
941 ret = wimlib_open_wim(globbuf.gl_pathv[i],
942 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK,
943 &additional_swms[i - offset],
944 imagex_progress_func);
948 *additional_swms_ret = additional_swms;
949 *num_additional_swms_ret = num_additional_swms - offset;
953 for (unsigned i = 0; i < num_additional_swms; i++)
954 wimlib_free(additional_swms[i]);
955 free(additional_swms);
964 parse_num_threads(const tchar *optarg)
967 unsigned long ul_nthreads = tstrtoul(optarg, &tmp, 10);
968 if (ul_nthreads >= UINT_MAX || *tmp || tmp == optarg) {
969 imagex_error(T("Number of threads must be a non-negative integer!"));
977 /* Apply one image, or all images, from a WIM file into a directory, OR apply
978 * one image from a WIM file to a NTFS volume. */
980 imagex_apply(int argc, tchar **argv)
983 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
988 const tchar *wimfile;
990 const tchar *image_num_or_name;
991 int extract_flags = WIMLIB_EXTRACT_FLAG_SEQUENTIAL;
993 const tchar *swm_glob = NULL;
994 WIMStruct **additional_swms = NULL;
995 unsigned num_additional_swms = 0;
997 for_opt(c, apply_options) {
999 case IMAGEX_CHECK_OPTION:
1000 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1002 case IMAGEX_HARDLINK_OPTION:
1003 extract_flags |= WIMLIB_EXTRACT_FLAG_HARDLINK;
1005 case IMAGEX_SYMLINK_OPTION:
1006 extract_flags |= WIMLIB_EXTRACT_FLAG_SYMLINK;
1008 case IMAGEX_VERBOSE_OPTION:
1009 extract_flags |= WIMLIB_EXTRACT_FLAG_VERBOSE;
1011 case IMAGEX_REF_OPTION:
1014 case IMAGEX_UNIX_DATA_OPTION:
1015 extract_flags |= WIMLIB_EXTRACT_FLAG_UNIX_DATA;
1017 case IMAGEX_NO_ACLS_OPTION:
1018 extract_flags |= WIMLIB_EXTRACT_FLAG_NO_ACLS;
1020 case IMAGEX_STRICT_ACLS_OPTION:
1021 extract_flags |= WIMLIB_EXTRACT_FLAG_STRICT_ACLS;
1030 if (argc != 2 && argc != 3) {
1037 image_num_or_name = T("1");
1040 image_num_or_name = argv[1];
1044 ret = wimlib_open_wim(wimfile, open_flags, &w, imagex_progress_func);
1048 image = wimlib_resolve_image(w, image_num_or_name);
1049 ret = verify_image_exists(image, image_num_or_name, wimfile);
1053 num_images = wimlib_get_num_images(w);
1054 if (argc == 2 && num_images != 1) {
1055 imagex_error(T("\"%"TS"\" contains %d images; Please select one "
1056 "(or all)"), wimfile, num_images);
1063 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
1065 &num_additional_swms);
1072 ret = tstat(target, &stbuf);
1074 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode))
1075 extract_flags |= WIMLIB_EXTRACT_FLAG_NTFS;
1077 if (errno != ENOENT) {
1078 imagex_error_with_errno(T("Failed to stat \"%"TS"\""),
1086 win32_acquire_restore_privileges();
1088 ret = wimlib_extract_image(w, image, target, extract_flags,
1089 additional_swms, num_additional_swms,
1090 imagex_progress_func);
1092 tprintf(T("Done applying WIM image.\n"));
1094 win32_release_restore_privileges();
1098 if (additional_swms) {
1099 for (unsigned i = 0; i < num_additional_swms; i++)
1100 wimlib_free(additional_swms[i]);
1101 free(additional_swms);
1106 /* Create a WIM image from a directory tree, NTFS volume, or multiple files or
1107 * directory trees. 'wimlib-imagex capture': create a new WIM file containing
1108 * the desired image. 'wimlib-imagex append': add a new image to an existing
1111 imagex_capture_or_append(int argc, tchar **argv)
1115 int add_image_flags = 0;
1116 int write_flags = 0;
1117 int compression_type = WIMLIB_COMPRESSION_TYPE_XPRESS;
1118 const tchar *wimfile;
1121 const tchar *flags_element = NULL;
1122 WIMStruct *w = NULL;
1125 int cmd = tstrcmp(argv[0], T("append")) ? CAPTURE : APPEND;
1126 unsigned num_threads = 0;
1129 size_t source_name_len;
1132 const tchar *config_file = NULL;
1133 tchar *config_str = NULL;
1136 bool source_list = false;
1137 size_t source_list_nchars;
1138 tchar *source_list_contents = NULL;
1139 bool capture_sources_malloced = false;
1140 struct wimlib_capture_source *capture_sources;
1143 for_opt(c, capture_or_append_options) {
1145 case IMAGEX_BOOT_OPTION:
1146 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_BOOT;
1148 case IMAGEX_CHECK_OPTION:
1149 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1150 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1152 case IMAGEX_CONFIG_OPTION:
1153 config_file = optarg;
1155 case IMAGEX_COMPRESS_OPTION:
1156 compression_type = get_compression_type(optarg);
1157 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1160 case IMAGEX_FLAGS_OPTION:
1161 flags_element = optarg;
1163 case IMAGEX_DEREFERENCE_OPTION:
1164 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE;
1166 case IMAGEX_VERBOSE_OPTION:
1167 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_VERBOSE;
1169 case IMAGEX_THREADS_OPTION:
1170 num_threads = parse_num_threads(optarg);
1171 if (num_threads == UINT_MAX)
1174 case IMAGEX_REBULID_OPTION:
1175 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1177 case IMAGEX_UNIX_DATA_OPTION:
1178 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA;
1180 case IMAGEX_SOURCE_LIST_OPTION:
1183 case IMAGEX_NO_ACLS_OPTION:
1184 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NO_ACLS;
1186 case IMAGEX_STRICT_ACLS_OPTION:
1187 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_STRICT_ACLS;
1197 if (argc < 2 || argc > 4) {
1208 /* Set default name to SOURCE argument, omitting any directory
1209 * prefixes and trailing slashes. This requires making a copy
1211 source_name_len = tstrlen(source);
1212 source_copy = alloca((source_name_len + 1) * sizeof(tchar));
1213 name = tbasename(tstrcpy(source_copy, source));
1215 /* Image description defaults to NULL if not given. */
1216 desc = (argc >= 4) ? argv[3] : NULL;
1219 /* Set up capture sources in source list mode */
1220 if (source[0] == T('-') && source[1] == T('\0')) {
1221 source_list_contents = stdin_get_text_contents(&source_list_nchars);
1223 source_list_contents = file_get_text_contents(source,
1224 &source_list_nchars);
1226 if (!source_list_contents)
1229 capture_sources = parse_source_list(&source_list_contents,
1232 if (!capture_sources) {
1236 capture_sources_malloced = true;
1238 /* Set up capture source in non-source-list mode (could be
1239 * either "normal" mode or "NTFS mode"--- see the man page). */
1240 capture_sources = alloca(sizeof(struct wimlib_capture_source));
1241 capture_sources[0].fs_source_path = source;
1242 capture_sources[0].wim_target_path = NULL;
1243 capture_sources[0].reserved = 0;
1248 config_str = file_get_text_contents(config_file, &config_len);
1256 ret = wimlib_open_wim(wimfile, open_flags, &w,
1257 imagex_progress_func);
1259 ret = wimlib_create_new_wim(compression_type, &w);
1265 ret = tstat(source, &stbuf);
1267 if (S_ISBLK(stbuf.st_mode) || S_ISREG(stbuf.st_mode)) {
1268 tprintf(T("Capturing WIM image from NTFS "
1269 "filesystem on \"%"TS"\"\n"), source);
1270 add_image_flags |= WIMLIB_ADD_IMAGE_FLAG_NTFS;
1273 if (errno != ENOENT) {
1274 imagex_error_with_errno(T("Failed to stat "
1275 "\"%"TS"\""), source);
1282 win32_acquire_capture_privileges();
1285 ret = wimlib_add_image_multisource(w, capture_sources,
1287 (config_str ? config_str :
1288 default_capture_config),
1289 (config_str ? config_len :
1290 tstrlen(default_capture_config)),
1292 imagex_progress_func);
1294 goto out_release_privs;
1295 cur_image = wimlib_get_num_images(w);
1297 ret = wimlib_set_image_descripton(w, cur_image, desc);
1299 goto out_release_privs;
1301 if (flags_element) {
1302 ret = wimlib_set_image_flags(w, cur_image, flags_element);
1304 goto out_release_privs;
1306 if (cmd == APPEND) {
1307 ret = wimlib_overwrite(w, write_flags, num_threads,
1308 imagex_progress_func);
1310 ret = wimlib_write(w, wimfile, WIMLIB_ALL_IMAGES, write_flags,
1311 num_threads, imagex_progress_func);
1313 if (ret == WIMLIB_ERR_REOPEN)
1316 imagex_error(T("Failed to write the WIM file \"%"TS"\""),
1320 win32_release_capture_privileges();
1325 free(source_list_contents);
1326 if (capture_sources_malloced)
1327 free(capture_sources);
1331 /* Remove image(s) from a WIM. */
1333 imagex_delete(int argc, tchar **argv)
1337 int write_flags = 0;
1338 const tchar *wimfile;
1339 const tchar *image_num_or_name;
1344 for_opt(c, delete_options) {
1346 case IMAGEX_CHECK_OPTION:
1347 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1348 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1350 case IMAGEX_SOFT_OPTION:
1351 write_flags |= WIMLIB_WRITE_FLAG_SOFT_DELETE;
1363 imagex_error(T("Must specify a WIM file"));
1365 imagex_error(T("Must specify an image"));
1370 image_num_or_name = argv[1];
1372 ret = file_writable(wimfile);
1376 ret = wimlib_open_wim(wimfile, open_flags, &w,
1377 imagex_progress_func);
1381 image = wimlib_resolve_image(w, image_num_or_name);
1383 ret = verify_image_exists(image, image_num_or_name, wimfile);
1387 ret = wimlib_delete_image(w, image);
1389 imagex_error(T("Failed to delete image from \"%"TS"\""), wimfile);
1393 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
1394 if (ret == WIMLIB_ERR_REOPEN)
1397 imagex_error(T("Failed to write the file \"%"TS"\" with image "
1398 "deleted"), wimfile);
1405 /* Print the files contained in an image(s) in a WIM file. */
1407 imagex_dir(int argc, tchar **argv)
1409 const tchar *wimfile;
1416 imagex_error(T("Must specify a WIM file"));
1421 imagex_error(T("Too many arguments"));
1427 ret = wimlib_open_wim(wimfile, WIMLIB_OPEN_FLAG_SPLIT_OK, &w,
1428 imagex_progress_func);
1433 image = wimlib_resolve_image(w, argv[2]);
1434 ret = verify_image_exists(image, argv[2], wimfile);
1438 /* Image was not specified. If the WIM only contains one image,
1439 * choose that one; otherwise, print an error. */
1440 num_images = wimlib_get_num_images(w);
1441 if (num_images != 1) {
1442 imagex_error(T("The file \"%"TS"\" contains %d images; Please "
1443 "select one."), wimfile, num_images);
1451 ret = wimlib_print_files(w, image);
1457 /* Exports one, or all, images from a WIM file to a new WIM file or an existing
1460 imagex_export(int argc, tchar **argv)
1464 int export_flags = 0;
1465 int write_flags = 0;
1466 int compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
1467 bool compression_type_specified = false;
1468 const tchar *src_wimfile;
1469 const tchar *src_image_num_or_name;
1470 const tchar *dest_wimfile;
1471 const tchar *dest_name;
1472 const tchar *dest_desc;
1473 WIMStruct *src_w = NULL;
1474 WIMStruct *dest_w = NULL;
1479 const tchar *swm_glob = NULL;
1480 WIMStruct **additional_swms = NULL;
1481 unsigned num_additional_swms = 0;
1482 unsigned num_threads = 0;
1484 for_opt(c, export_options) {
1486 case IMAGEX_BOOT_OPTION:
1487 export_flags |= WIMLIB_EXPORT_FLAG_BOOT;
1489 case IMAGEX_CHECK_OPTION:
1490 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1491 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1493 case IMAGEX_COMPRESS_OPTION:
1494 compression_type = get_compression_type(optarg);
1495 if (compression_type == WIMLIB_COMPRESSION_TYPE_INVALID)
1497 compression_type_specified = true;
1499 case IMAGEX_REF_OPTION:
1502 case IMAGEX_THREADS_OPTION:
1503 num_threads = parse_num_threads(optarg);
1504 if (num_threads == UINT_MAX)
1507 case IMAGEX_REBULID_OPTION:
1508 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1517 if (argc < 3 || argc > 5) {
1521 src_wimfile = argv[0];
1522 src_image_num_or_name = argv[1];
1523 dest_wimfile = argv[2];
1524 dest_name = (argc >= 4) ? argv[3] : NULL;
1525 dest_desc = (argc >= 5) ? argv[4] : NULL;
1526 ret = wimlib_open_wim(src_wimfile,
1527 open_flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &src_w,
1528 imagex_progress_func);
1532 /* Determine if the destination is an existing file or not.
1533 * If so, we try to append the exported image(s) to it; otherwise, we
1534 * create a new WIM containing the exported image(s). */
1535 if (tstat(dest_wimfile, &stbuf) == 0) {
1539 /* Destination file exists. */
1541 if (!S_ISREG(stbuf.st_mode)) {
1542 imagex_error(T("\"%"TS"\" is not a regular file"),
1547 ret = wimlib_open_wim(dest_wimfile, open_flags, &dest_w,
1548 imagex_progress_func);
1552 ret = file_writable(dest_wimfile);
1556 dest_ctype = wimlib_get_compression_type(dest_w);
1557 if (compression_type_specified
1558 && compression_type != dest_ctype)
1560 imagex_error(T("Cannot specify a compression type that is "
1561 "not the same as that used in the "
1562 "destination WIM"));
1568 /* dest_wimfile is not an existing file, so create a new WIM. */
1569 if (!compression_type_specified)
1570 compression_type = wimlib_get_compression_type(src_w);
1571 if (errno == ENOENT) {
1572 ret = wimlib_create_new_wim(compression_type, &dest_w);
1576 imagex_error_with_errno(T("Cannot stat file \"%"TS"\""),
1583 image = wimlib_resolve_image(src_w, src_image_num_or_name);
1584 ret = verify_image_exists(image, src_image_num_or_name, src_wimfile);
1589 ret = open_swms_from_glob(swm_glob, src_wimfile, open_flags,
1591 &num_additional_swms);
1596 ret = wimlib_export_image(src_w, image, dest_w, dest_name, dest_desc,
1597 export_flags, additional_swms,
1598 num_additional_swms, imagex_progress_func);
1604 ret = wimlib_write(dest_w, dest_wimfile, WIMLIB_ALL_IMAGES,
1605 write_flags, num_threads,
1606 imagex_progress_func);
1608 ret = wimlib_overwrite(dest_w, write_flags, num_threads,
1609 imagex_progress_func);
1611 if (ret == WIMLIB_ERR_REOPEN)
1614 wimlib_free(dest_w);
1615 if (additional_swms) {
1616 for (unsigned i = 0; i < num_additional_swms; i++)
1617 wimlib_free(additional_swms[i]);
1618 free(additional_swms);
1623 /* Prints information about a WIM file; also can mark an image as bootable,
1624 * change the name of an image, or change the description of an image. */
1626 imagex_info(int argc, tchar **argv)
1631 bool header = false;
1632 bool lookup_table = false;
1634 bool metadata = false;
1635 bool short_header = true;
1636 const tchar *xml_out_file = NULL;
1637 const tchar *wimfile;
1638 const tchar *image_num_or_name = T("all");
1639 const tchar *new_name = NULL;
1640 const tchar *new_desc = NULL;
1645 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1650 for_opt(c, info_options) {
1652 case IMAGEX_BOOT_OPTION:
1655 case IMAGEX_CHECK_OPTION:
1658 case IMAGEX_HEADER_OPTION:
1660 short_header = false;
1662 case IMAGEX_LOOKUP_TABLE_OPTION:
1663 lookup_table = true;
1664 short_header = false;
1666 case IMAGEX_XML_OPTION:
1668 short_header = false;
1670 case IMAGEX_EXTRACT_XML_OPTION:
1671 xml_out_file = optarg;
1672 short_header = false;
1674 case IMAGEX_METADATA_OPTION:
1676 short_header = false;
1686 if (argc == 0 || argc > 4) {
1692 image_num_or_name = argv[1];
1702 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1704 ret = wimlib_open_wim(wimfile, open_flags, &w,
1705 imagex_progress_func);
1709 part_number = wimlib_get_part_number(w, &total_parts);
1711 image = wimlib_resolve_image(w, image_num_or_name);
1712 if (image == WIMLIB_NO_IMAGE && tstrcmp(image_num_or_name, T("0"))) {
1713 imagex_error(T("The image \"%"TS"\" does not exist"),
1716 imagex_error(T("If you would like to set the boot "
1717 "index to 0, specify image \"0\" with "
1718 "the --boot flag."));
1720 ret = WIMLIB_ERR_INVALID_IMAGE;
1724 num_images = wimlib_get_num_images(w);
1726 if (num_images == 0) {
1728 imagex_error(T("--boot is meaningless on a WIM with no "
1730 ret = WIMLIB_ERR_INVALID_IMAGE;
1735 if (image == WIMLIB_ALL_IMAGES && num_images > 1) {
1737 imagex_error(T("Cannot specify the --boot flag "
1738 "without specifying a specific "
1739 "image in a multi-image WIM"));
1740 ret = WIMLIB_ERR_INVALID_IMAGE;
1744 imagex_error(T("Cannot specify the NEW_NAME "
1745 "without specifying a specific "
1746 "image in a multi-image WIM"));
1747 ret = WIMLIB_ERR_INVALID_IMAGE;
1752 /* Operations that print information are separated from operations that
1753 * recreate the WIM file. */
1754 if (!new_name && !boot) {
1756 /* Read-only operations */
1758 if (image == WIMLIB_NO_IMAGE) {
1759 imagex_error(T("\"%"TS"\" is not a valid image"),
1761 ret = WIMLIB_ERR_INVALID_IMAGE;
1765 if (image == WIMLIB_ALL_IMAGES && short_header)
1766 wimlib_print_wim_information(w);
1769 wimlib_print_header(w);
1772 if (total_parts != 1) {
1773 tprintf(T("Warning: Only showing the lookup table "
1774 "for part %d of a %d-part WIM.\n"),
1775 part_number, total_parts);
1777 wimlib_print_lookup_table(w);
1781 ret = wimlib_extract_xml_data(w, stdout);
1787 fp = tfopen(xml_out_file, T("wb"));
1789 imagex_error_with_errno(T("Failed to open the "
1790 "file \"%"TS"\" for "
1796 ret = wimlib_extract_xml_data(w, fp);
1797 if (fclose(fp) != 0) {
1798 imagex_error(T("Failed to close the file "
1809 wimlib_print_available_images(w, image);
1812 ret = wimlib_print_metadata(w, image);
1818 /* Modification operations */
1819 if (total_parts != 1) {
1820 imagex_error(T("Modifying a split WIM is not supported."));
1824 if (image == WIMLIB_ALL_IMAGES)
1827 if (image == WIMLIB_NO_IMAGE && new_name) {
1828 imagex_error(T("Cannot specify new_name (\"%"TS"\") "
1829 "when using image 0"), new_name);
1835 if (image == wimlib_get_boot_idx(w)) {
1836 tprintf(T("Image %d is already marked as "
1837 "bootable.\n"), image);
1840 tprintf(T("Marking image %d as bootable.\n"),
1842 wimlib_set_boot_idx(w, image);
1846 if (!tstrcmp(wimlib_get_image_name(w, image), new_name))
1848 tprintf(T("Image %d is already named \"%"TS"\".\n"),
1852 tprintf(T("Changing the name of image %d to "
1853 "\"%"TS"\".\n"), image, new_name);
1854 ret = wimlib_set_image_name(w, image, new_name);
1860 const tchar *old_desc;
1861 old_desc = wimlib_get_image_description(w, image);
1862 if (old_desc && !tstrcmp(old_desc, new_desc)) {
1863 tprintf(T("The description of image %d is already "
1864 "\"%"TS"\".\n"), image, new_desc);
1867 tprintf(T("Changing the description of image %d "
1868 "to \"%"TS"\".\n"), image, new_desc);
1869 ret = wimlib_set_image_descripton(w, image,
1876 /* Only call wimlib_overwrite() if something actually needs to
1878 if (boot || new_name || new_desc ||
1879 (check && !wimlib_has_integrity_table(w)))
1883 ret = file_writable(wimfile);
1888 write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1892 ret = wimlib_overwrite(w, write_flags, 1,
1893 imagex_progress_func);
1894 if (ret == WIMLIB_ERR_REOPEN)
1897 tprintf(T("The file \"%"TS"\" was not modified because nothing "
1898 "needed to be done.\n"), wimfile);
1907 /* Join split WIMs into one part WIM */
1909 imagex_join(int argc, tchar **argv)
1912 int swm_open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1913 int wim_write_flags = 0;
1914 const tchar *output_path;
1916 for_opt(c, join_options) {
1918 case IMAGEX_CHECK_OPTION:
1919 swm_open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1920 wim_write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1930 imagex_error(T("Must specify one or more split WIM (.swm) "
1934 output_path = argv[0];
1935 return wimlib_join((const tchar * const *)++argv,
1940 imagex_progress_func);
1946 /* Mounts an image using a FUSE mount. */
1948 imagex_mount_rw_or_ro(int argc, tchar **argv)
1951 int mount_flags = 0;
1952 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
1953 const tchar *wimfile;
1959 const tchar *swm_glob = NULL;
1960 WIMStruct **additional_swms = NULL;
1961 unsigned num_additional_swms = 0;
1962 const tchar *staging_dir = NULL;
1964 if (!tstrcmp(argv[0], T("mountrw")))
1965 mount_flags |= WIMLIB_MOUNT_FLAG_READWRITE;
1967 for_opt(c, mount_options) {
1969 case IMAGEX_ALLOW_OTHER_OPTION:
1970 mount_flags |= WIMLIB_MOUNT_FLAG_ALLOW_OTHER;
1972 case IMAGEX_CHECK_OPTION:
1973 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
1975 case IMAGEX_DEBUG_OPTION:
1976 mount_flags |= WIMLIB_MOUNT_FLAG_DEBUG;
1978 case IMAGEX_STREAMS_INTERFACE_OPTION:
1979 if (!tstrcasecmp(optarg, T("none")))
1980 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE;
1981 else if (!tstrcasecmp(optarg, T("xattr")))
1982 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1983 else if (!tstrcasecmp(optarg, T("windows")))
1984 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS;
1986 imagex_error(T("Unknown stream interface \"%"TS"\""),
1991 case IMAGEX_REF_OPTION:
1994 case IMAGEX_STAGING_DIR_OPTION:
1995 staging_dir = optarg;
1997 case IMAGEX_UNIX_DATA_OPTION:
1998 mount_flags |= WIMLIB_MOUNT_FLAG_UNIX_DATA;
2006 if (argc != 2 && argc != 3)
2011 ret = wimlib_open_wim(wimfile, open_flags, &w,
2012 imagex_progress_func);
2017 ret = open_swms_from_glob(swm_glob, wimfile, open_flags,
2019 &num_additional_swms);
2026 num_images = wimlib_get_num_images(w);
2027 if (num_images != 1) {
2028 imagex_error(T("The file \"%"TS"\" contains %d images; Please "
2029 "select one."), wimfile, num_images);
2030 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2037 image = wimlib_resolve_image(w, argv[1]);
2039 ret = verify_image_exists_and_is_single(image, argv[1], wimfile);
2044 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2045 ret = file_writable(wimfile);
2050 ret = wimlib_mount_image(w, image, dir, mount_flags, additional_swms,
2051 num_additional_swms, staging_dir);
2053 imagex_error(T("Failed to mount image %d from \"%"TS"\" "
2055 image, wimfile, dir);
2060 if (additional_swms) {
2061 for (unsigned i = 0; i < num_additional_swms; i++)
2062 wimlib_free(additional_swms[i]);
2063 free(additional_swms);
2067 usage((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2072 /* Rebuild a WIM file */
2074 imagex_optimize(int argc, tchar **argv)
2078 int write_flags = WIMLIB_WRITE_FLAG_REBUILD;
2081 const tchar *wimfile;
2085 for_opt(c, optimize_options) {
2087 case IMAGEX_CHECK_OPTION:
2088 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2089 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2091 case IMAGEX_RECOMPRESS_OPTION:
2092 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
2109 ret = wimlib_open_wim(wimfile, open_flags, &w,
2110 imagex_progress_func);
2114 old_size = file_get_size(argv[0]);
2115 tprintf(T("\"%"TS"\" original size: "), wimfile);
2117 tfputs(T("Unknown\n"), stdout);
2119 tprintf(T("%"PRIu64" KiB\n"), old_size >> 10);
2121 ret = wimlib_overwrite(w, write_flags, 0, imagex_progress_func);
2124 new_size = file_get_size(argv[0]);
2125 tprintf(T("\"%"TS"\" optimized size: "), wimfile);
2127 tfputs(T("Unknown\n"), stdout);
2129 tprintf(T("%"PRIu64" KiB\n"), new_size >> 10);
2131 tfputs(T("Space saved: "), stdout);
2132 if (new_size != -1 && old_size != -1) {
2133 tprintf(T("%lld KiB\n"),
2134 ((long long)old_size - (long long)new_size) >> 10);
2136 tfputs(T("Unknown\n"), stdout);
2144 /* Split a WIM into a spanned set */
2146 imagex_split(int argc, tchar **argv)
2149 int open_flags = WIMLIB_OPEN_FLAG_SPLIT_OK;
2150 int write_flags = 0;
2151 unsigned long part_size;
2156 for_opt(c, split_options) {
2158 case IMAGEX_CHECK_OPTION:
2159 open_flags |= WIMLIB_OPEN_FLAG_CHECK_INTEGRITY;
2160 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
2174 part_size = tstrtod(argv[2], &tmp) * (1 << 20);
2175 if (tmp == argv[2] || *tmp) {
2176 imagex_error(T("Invalid part size \"%"TS"\""), argv[2]);
2177 imagex_error(T("The part size must be an integer or "
2178 "floating-point number of megabytes."));
2181 ret = wimlib_open_wim(argv[0], open_flags, &w, imagex_progress_func);
2184 ret = wimlib_split(w, argv[1], part_size, write_flags, imagex_progress_func);
2189 /* Unmounts a mounted WIM image. */
2191 imagex_unmount(int argc, tchar **argv)
2194 int unmount_flags = 0;
2197 for_opt(c, unmount_options) {
2199 case IMAGEX_COMMIT_OPTION:
2200 unmount_flags |= WIMLIB_UNMOUNT_FLAG_COMMIT;
2202 case IMAGEX_CHECK_OPTION:
2203 unmount_flags |= WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY;
2205 case IMAGEX_REBULID_OPTION:
2206 unmount_flags |= WIMLIB_UNMOUNT_FLAG_REBUILD;
2220 ret = wimlib_unmount_image(argv[0], unmount_flags,
2221 imagex_progress_func);
2223 imagex_error(T("Failed to unmount \"%"TS"\""), argv[0]);
2227 struct imagex_command {
2229 int (*func)(int , tchar **);
2234 #define for_imagex_command(p) for (p = &imagex_commands[0]; \
2235 p != &imagex_commands[ARRAY_LEN(imagex_commands)]; p++)
2237 static const struct imagex_command imagex_commands[] = {
2238 {T("append"), imagex_capture_or_append, APPEND},
2239 {T("apply"), imagex_apply, APPLY},
2240 {T("capture"), imagex_capture_or_append, CAPTURE},
2241 {T("delete"), imagex_delete, DELETE},
2242 {T("dir"), imagex_dir, DIR},
2243 {T("export"), imagex_export, EXPORT},
2244 {T("info"), imagex_info, INFO},
2245 {T("join"), imagex_join, JOIN},
2246 {T("mount"), imagex_mount_rw_or_ro, MOUNT},
2247 {T("mountrw"), imagex_mount_rw_or_ro, MOUNTRW},
2248 {T("optimize"),imagex_optimize, OPTIMIZE},
2249 {T("split"), imagex_split, SPLIT},
2250 {T("unmount"), imagex_unmount, UNMOUNT},
2256 static const tchar *s =
2258 IMAGEX_PROGNAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
2259 "Copyright (C) 2012, 2013 Eric Biggers\n"
2260 "License GPLv3+; GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n"
2261 "This is free software: you are free to change and redistribute it.\n"
2262 "There is NO WARRANTY, to the extent permitted by law.\n"
2264 "Report bugs to "PACKAGE_BUGREPORT".\n"
2271 help_or_version(int argc, tchar **argv)
2275 const struct imagex_command *cmd;
2277 for (i = 1; i < argc; i++) {
2285 if (!tstrcmp(p, T("help"))) {
2286 for_imagex_command(cmd) {
2287 if (!tstrcmp(cmd->name, argv[1])) {
2295 if (!tstrcmp(p, T("version"))) {
2306 const struct imagex_command *cmd;
2307 tprintf(T("Usage:\n%"TS), usage_strings[cmd_type]);
2308 for_imagex_command(cmd) {
2309 if (cmd->cmd == cmd_type) {
2310 tprintf(T("\nTry `man "IMAGEX_PROGNAME"-%"TS"' "
2311 "for more details.\n"), cmd->name);
2319 tfputs(T("Usage:\n"), stdout);
2320 for (int i = 0; i < ARRAY_LEN(usage_strings); i++)
2321 tprintf(T(" %"TS), usage_strings[i]);
2322 static const tchar *extra =
2324 " "IMAGEX_PROGNAME" --help\n"
2325 " "IMAGEX_PROGNAME" --version\n"
2327 " The compression TYPE may be \"maximum\", \"fast\", or \"none\".\n"
2329 " Try `man "IMAGEX_PROGNAME"' for more information.\n"
2331 tfputs(extra, stdout);
2334 /* Entry point for wimlib's ImageX implementation. On UNIX the command
2335 * arguments will just be 'char' strings (ideally UTF-8 encoded, but could be
2336 * something else), while an Windows the command arguments will be UTF-16LE
2337 * encoded 'wchar_t' strings. */
2340 wmain(int argc, wchar_t **argv, wchar_t **envp)
2342 main(int argc, char **argv)
2345 const struct imagex_command *cmd;
2349 setlocale(LC_ALL, "");
2351 char *codeset = nl_langinfo(CODESET);
2352 if (!strstr(codeset, "UTF-8") &&
2353 !strstr(codeset, "UTF8") &&
2354 !strstr(codeset, "utf-8") &&
2355 !strstr(codeset, "utf8"))
2358 "WARNING: Running "IMAGEX_PROGNAME" in a UTF-8 locale is recommended!\n"
2359 " (Maybe try: `export LANG=en_US.UTF-8'?\n", stderr);
2363 #endif /* !__WIN32__ */
2366 imagex_error(T("No command specified"));
2372 /* Handle --help and --version for all commands. Note that this will
2373 * not return if either of these arguments are present. */
2374 help_or_version(argc, argv);
2378 /* The user may like to see more informative error messages. */
2379 wimlib_set_print_errors(true);
2381 /* Do any initializations that the library needs */
2382 ret = wimlib_global_init();
2384 goto out_check_status;
2386 /* Search for the function to handle the ImageX subcommand. */
2387 for_imagex_command(cmd) {
2388 if (!tstrcmp(cmd->name, *argv)) {
2389 ret = cmd->func(argc, argv);
2390 goto out_check_write_error;
2394 imagex_error(T("Unrecognized command: `%"TS"'"), argv[0]);
2398 out_check_write_error:
2399 /* For 'wimlib-imagex info' and 'wimlib-imagex dir', data printed to
2400 * standard output is part of the program's actual behavior and not just
2401 * for informational purposes, so we should set a failure exit status if
2402 * there was a write error. */
2403 if (cmd == &imagex_commands[INFO] || cmd == &imagex_commands[DIR]) {
2404 if (ferror(stdout) || fclose(stdout)) {
2405 imagex_error_with_errno(T("error writing to standard output"));
2411 /* Exit status (ret): -1 indicates an error found by 'wimlib-imagex'
2412 * outside of the wimlib library code. 0 indicates success. > 0
2413 * indicates a wimlib error code from which an error message can be
2416 imagex_error(T("Exiting with error code %d:\n"
2418 wimlib_get_error_string(ret));
2419 if (ret == WIMLIB_ERR_NTFS_3G && errno != 0)
2420 imagex_error_with_errno(T("errno"));
2423 /* Make the library free any resources it's holding (not strictly
2424 * necessary because the process is ending anyway). */
2425 wimlib_global_cleanup();