]> wimlib.net Git - wimlib/blob - examples/applywim.c
unix_apply.c: Honor i_not_rpfixed
[wimlib] / examples / applywim.c
1 /*
2  * applywim.c - A program to extract the first image from a WIM file.
3  */
4
5 #include <wimlib.h>
6 #include <stdio.h>
7
8 #define TO_PERCENT(numerator, denominator) \
9         ((float)(((denominator) == 0) ? 0 : ((numerator) * 100 / (float)(denominator))))
10
11 static enum wimlib_progress_status
12 extract_progress(enum wimlib_progress_msg msg,
13                  union wimlib_progress_info *info, void *progctx)
14 {
15         switch (msg) {
16         case WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS:
17                 printf("Extracting files: %.2f%% complete\n",
18                        TO_PERCENT(info->extract.completed_bytes,
19                                   info->extract.total_bytes));
20                 break;
21         default:
22                 break;
23         }
24         return WIMLIB_PROGRESS_STATUS_CONTINUE;
25 }
26
27 int main(int argc, char **argv)
28 {
29         int ret;
30         WIMStruct *wim = NULL;
31         const char *wimpath;
32         const char *destdir;
33
34         /* Check for the correct number of arguments.  */
35         if (argc != 3) {
36                 fprintf(stderr, "Usage: applywim WIM DIR\n");
37                 return 2;
38         }
39
40         wimpath = argv[1];
41         destdir = argv[2];
42
43         /* Open the WIM file as a WIMStruct.  */
44         ret = wimlib_open_wim(wimpath,  /* Path of WIM file to open  */
45                               0,        /* WIMLIB_OPEN_FLAG_* flags (0 means all defaults)  */
46                               &wim);    /* Return the WIMStruct pointer in this location  */
47         if (ret != 0) /* Always should check the error codes.  */
48                 goto out;
49
50         /* Register our progress function.  */
51         wimlib_register_progress_function(wim, extract_progress, NULL);
52
53         /* Extract the first image.  */
54         ret = wimlib_extract_image(wim,     /* WIMStruct from which to extract the image  */
55                                    1,       /* Image to extract  */
56                                    destdir, /* Directory to extract the image to  */
57                                    0);      /* WIMLIB_EXTRACT_FLAG_* flags (0 means all defaults)  */
58
59 out:
60         /* Free the WIMStruct.  Has no effect if the pointer to it is NULL.  */
61         wimlib_free(wim);
62
63         /* Check for error status.  */
64         if (ret != 0) {
65                 fprintf(stderr, "wimlib error %d: %s\n",
66                         ret, wimlib_get_error_string(ret));
67         }
68
69         /* Free global memory (optional).  */
70         wimlib_global_cleanup();
71
72         return ret;
73 }