]> wimlib.net Git - wimlib/blob - examples/capturewim.c
Update examples
[wimlib] / examples / capturewim.c
1 /*
2  * capturewim.c - A program to capture a directory tree into 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 int
12 write_progress(enum wimlib_progress_msg msg,
13                const union wimlib_progress_info *info)
14 {
15         switch (msg) {
16         case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
17                 printf("Writing WIM: %.2f%% complete\n",
18                        TO_PERCENT(info->write_streams.completed_bytes,
19                                   info->write_streams.total_bytes));
20                 break;
21         default:
22                 break;
23         }
24         return 0;
25 }
26
27 int main(int argc, char **argv)
28 {
29         int ret;
30         WIMStruct *wim = NULL;
31         const char *srcdir;
32         const char *wimpath;
33
34         /* Check for the correct number of arguments.  */
35         if (argc != 3) {
36                 fprintf(stderr, "Usage: capturewim DIR WIM\n");
37                 return 2;
38         }
39
40         srcdir = argv[1];
41         wimpath = argv[2];
42
43         /* Create a WIMStruct for a WIM.  */
44         ret = wimlib_create_new_wim(WIMLIB_COMPRESSION_TYPE_LZX, &wim);
45         if (ret != 0)  /* Always should check the error codes.  */
46                 goto out;
47
48         /* Add the directory tree to the WIMStruct as an image.  */
49
50         ret = wimlib_add_image(wim,     /* WIMStruct to which to add the image    */
51                                srcdir,  /* Directory from which to add the image  */
52                                NULL,    /* Name to give the image (NULL means none)  */
53                                NULL,    /* Capture configuration structure (NULL means none)  */
54                                0,       /* WIMLIB_ADD_FLAG_* flags (0 means all defaults)  */
55                                NULL);   /* Progress function (NULL means none) */
56         if (ret != 0)
57                 goto out;
58
59         /* Write the WIM file.  */
60
61         ret = wimlib_write(wim,      /* WIMStruct from which to write a WIM  */
62                            wimpath,  /* Path to write the WIM to             */
63                            WIMLIB_ALL_IMAGES, /*  Image(s) in the WIM to write */
64                            0,        /* WIMLIB_WRITE_FLAG_* flags (0 means all defaults)   */
65                            0,        /* Number of compressor threads (0 means default)  */
66                            write_progress); /* Progress function  */
67
68 out:
69         /* Free the WIMStruct.  Has no effect if the pointer to it is NULL.  */
70         wimlib_free(wim);
71
72         /* Check for error status.  */
73         if (ret != 0) {
74                 fprintf(stderr, "wimlib error %d: %s\n",
75                         ret, wimlib_get_error_string(ret));
76         }
77
78         /* Free global memory (optional).  */
79         wimlib_global_cleanup();
80
81         return ret;
82 }