2 * capturewim.c - A program to capture a directory tree into a WIM file.
4 * Copyright 2022 Eric Biggers
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
32 * Windows compatibility defines for string encoding. Applications using wimlib
33 * that need to run on both UNIX and Windows will need to do something similar
34 * to this, whereas applications that only need to run on one or the other can
35 * just use their platform's convention directly.
39 typedef wchar_t tchar;
46 #define TO_PERCENT(numerator, denominator) \
47 ((float)(((denominator) == 0) ? 0 : ((numerator) * 100 / (float)(denominator))))
49 static enum wimlib_progress_status
50 write_progress(enum wimlib_progress_msg msg,
51 union wimlib_progress_info *info, void *progctx)
54 case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
55 printf("Writing WIM: %.2f%% complete\n",
56 TO_PERCENT(info->write_streams.completed_bytes,
57 info->write_streams.total_bytes));
62 return WIMLIB_PROGRESS_STATUS_CONTINUE;
65 int main(int argc, tchar **argv)
68 WIMStruct *wim = NULL;
72 /* Check for the correct number of arguments. */
74 fprintf(stderr, "Usage: capturewim DIR WIM\n");
81 /* Create a WIMStruct for a WIM. */
82 ret = wimlib_create_new_wim(WIMLIB_COMPRESSION_TYPE_LZX, &wim);
83 if (ret != 0) /* Always should check the error codes. */
86 /* Register our progress function. */
87 wimlib_register_progress_function(wim, write_progress, NULL);
89 /* Add the directory tree to the WIMStruct as an image. */
91 ret = wimlib_add_image(wim, /* WIMStruct to which to add the image */
92 srcdir, /* Directory from which to add the image */
93 NULL, /* Name to give the image (NULL means none) */
94 NULL, /* Capture configuration structure (NULL means none) */
95 0); /* WIMLIB_ADD_FLAG_* flags (0 means all defaults) */
99 /* Write the WIM file. */
101 ret = wimlib_write(wim, /* WIMStruct from which to write a WIM */
102 wimpath, /* Path to write the WIM to */
103 WIMLIB_ALL_IMAGES, /* Image(s) in the WIM to write */
104 0, /* WIMLIB_WRITE_FLAG_* flags (0 means all defaults) */
105 0); /* Number of compressor threads (0 means default) */
108 /* Free the WIMStruct. Has no effect if the pointer to it is NULL. */
111 /* Check for error status. */
113 fprintf(stderr, "wimlib error %d: %" TS"\n",
114 ret, wimlib_get_error_string((enum wimlib_error_code)ret));
117 /* Free global memory (optional). */
118 wimlib_global_cleanup();