]> wimlib.net Git - wimlib/blob - examples/applywim.c
mount_image.c: add fallback definitions of RENAME_* constants
[wimlib] / examples / applywim.c
1 /*
2  * applywim.c - A program to extract the first image from a WIM file.
3  *
4  * Copyright 2022 Eric Biggers
5  *
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
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
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.
26  */
27
28 #include <wimlib.h>
29 #include <stdio.h>
30
31 /*
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.
36  */
37 #ifdef _WIN32
38 #  define main          wmain
39    typedef wchar_t      tchar;
40 #  define TS            "ls"
41 #else
42    typedef char         tchar;
43 #  define TS            "s"
44 #endif
45
46 #define TO_PERCENT(numerator, denominator) \
47         ((float)(((denominator) == 0) ? 0 : ((numerator) * 100 / (float)(denominator))))
48
49 static enum wimlib_progress_status
50 extract_progress(enum wimlib_progress_msg msg,
51                  union wimlib_progress_info *info, void *progctx)
52 {
53         switch (msg) {
54         case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
55                 printf("Extracting files: %.2f%% complete\n",
56                        TO_PERCENT(info->extract.completed_bytes,
57                                   info->extract.total_bytes));
58                 break;
59         default:
60                 break;
61         }
62         return WIMLIB_PROGRESS_STATUS_CONTINUE;
63 }
64
65 int main(int argc, tchar **argv)
66 {
67         int ret;
68         WIMStruct *wim = NULL;
69         const tchar *wimpath;
70         const tchar *destdir;
71
72         /* Check for the correct number of arguments.  */
73         if (argc != 3) {
74                 fprintf(stderr, "Usage: applywim WIM DIR\n");
75                 return 2;
76         }
77
78         wimpath = argv[1];
79         destdir = argv[2];
80
81         /* Open the WIM file as a WIMStruct.  */
82         ret = wimlib_open_wim(wimpath,  /* Path of WIM file to open  */
83                               0,        /* WIMLIB_OPEN_FLAG_* flags (0 means all defaults)  */
84                               &wim);    /* Return the WIMStruct pointer in this location  */
85         if (ret != 0) /* Always should check the error codes.  */
86                 goto out;
87
88         /* Register our progress function.  */
89         wimlib_register_progress_function(wim, extract_progress, NULL);
90
91         /* Extract the first image.  */
92         ret = wimlib_extract_image(wim,     /* WIMStruct from which to extract the image  */
93                                    1,       /* Image to extract  */
94                                    destdir, /* Directory to extract the image to  */
95                                    0);      /* WIMLIB_EXTRACT_FLAG_* flags (0 means all defaults)  */
96
97 out:
98         /* Free the WIMStruct.  Has no effect if the pointer to it is NULL.  */
99         wimlib_free(wim);
100
101         /* Check for error status.  */
102         if (ret != 0) {
103                 fprintf(stderr, "wimlib error %d: %" TS"\n",
104                         ret, wimlib_get_error_string((enum wimlib_error_code)ret));
105         }
106
107         /* Free global memory (optional).  */
108         wimlib_global_cleanup();
109
110         return ret;
111 }