]> wimlib.net Git - wimlib/blob - examples/capturewim.c
examples: C++ and Windows compatibility
[wimlib] / examples / capturewim.c
1 /*
2  * capturewim.c - A program to capture a directory tree into a WIM file.
3  *
4  * The following copying information applies to this specific source code file:
5  *
6  * Written in 2014-2016 by Eric Biggers <ebiggers3@gmail.com>
7  *
8  * To the extent possible under law, the author(s) have dedicated all copyright
9  * and related and neighboring rights to this software to the public domain
10  * worldwide via the Creative Commons Zero 1.0 Universal Public Domain
11  * Dedication (the "CC0").
12  *
13  * This software is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the CC0 for more details.
16  *
17  * You should have received a copy of the CC0 along with this software; if not
18  * see <http://creativecommons.org/publicdomain/zero/1.0/>.
19  */
20
21 #include <wimlib.h>
22 #include <stdio.h>
23
24 /*
25  * Windows compatibility defines for string encoding.  Applications using wimlib
26  * that need to run on both UNIX and Windows will need to do something similar
27  * to this, whereas applications that only need to run on one or the other can
28  * just use their platform's convention directly.
29  */
30 #ifdef _WIN32
31 #  define main          wmain
32    typedef wchar_t      tchar;
33 #  define TS            "ls"
34 #else
35    typedef char         tchar;
36 #  define TS            "s"
37 #endif
38
39 #define TO_PERCENT(numerator, denominator) \
40         ((float)(((denominator) == 0) ? 0 : ((numerator) * 100 / (float)(denominator))))
41
42 static enum wimlib_progress_status
43 write_progress(enum wimlib_progress_msg msg,
44                union wimlib_progress_info *info, void *progctx)
45 {
46         switch (msg) {
47         case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
48                 printf("Writing WIM: %.2f%% complete\n",
49                        TO_PERCENT(info->write_streams.completed_bytes,
50                                   info->write_streams.total_bytes));
51                 break;
52         default:
53                 break;
54         }
55         return WIMLIB_PROGRESS_STATUS_CONTINUE;
56 }
57
58 int main(int argc, tchar **argv)
59 {
60         int ret;
61         WIMStruct *wim = NULL;
62         const tchar *srcdir;
63         const tchar *wimpath;
64
65         /* Check for the correct number of arguments.  */
66         if (argc != 3) {
67                 fprintf(stderr, "Usage: capturewim DIR WIM\n");
68                 return 2;
69         }
70
71         srcdir = argv[1];
72         wimpath = argv[2];
73
74         /* Create a WIMStruct for a WIM.  */
75         ret = wimlib_create_new_wim(WIMLIB_COMPRESSION_TYPE_LZX, &wim);
76         if (ret != 0)  /* Always should check the error codes.  */
77                 goto out;
78
79         /* Register our progress function.  */
80         wimlib_register_progress_function(wim, write_progress, NULL);
81
82         /* Add the directory tree to the WIMStruct as an image.  */
83
84         ret = wimlib_add_image(wim,     /* WIMStruct to which to add the image    */
85                                srcdir,  /* Directory from which to add the image  */
86                                NULL,    /* Name to give the image (NULL means none)  */
87                                NULL,    /* Capture configuration structure (NULL means none)  */
88                                0);      /* WIMLIB_ADD_FLAG_* flags (0 means all defaults)  */
89         if (ret != 0)
90                 goto out;
91
92         /* Write the WIM file.  */
93
94         ret = wimlib_write(wim,      /* WIMStruct from which to write a WIM  */
95                            wimpath,  /* Path to write the WIM to             */
96                            WIMLIB_ALL_IMAGES, /*  Image(s) in the WIM to write */
97                            0,        /* WIMLIB_WRITE_FLAG_* flags (0 means all defaults)   */
98                            0);       /* Number of compressor threads (0 means default)  */
99
100 out:
101         /* Free the WIMStruct.  Has no effect if the pointer to it is NULL.  */
102         wimlib_free(wim);
103
104         /* Check for error status.  */
105         if (ret != 0) {
106                 fprintf(stderr, "wimlib error %d: %" TS"\n",
107                         ret, wimlib_get_error_string((enum wimlib_error_code)ret));
108         }
109
110         /* Free global memory (optional).  */
111         wimlib_global_cleanup();
112
113         return ret;
114 }