]> wimlib.net Git - wimlib/blob - examples/capturewim.c
win32_apply.c: WRITE_DAC is no longer needed when creating directory
[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 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 #define TO_PERCENT(numerator, denominator) \
25         ((float)(((denominator) == 0) ? 0 : ((numerator) * 100 / (float)(denominator))))
26
27 static enum wimlib_progress_status
28 write_progress(enum wimlib_progress_msg msg,
29                union wimlib_progress_info *info, void *progctx)
30 {
31         switch (msg) {
32         case WIMLIB_PROGRESS_MSG_WRITE_STREAMS:
33                 printf("Writing WIM: %.2f%% complete\n",
34                        TO_PERCENT(info->write_streams.completed_bytes,
35                                   info->write_streams.total_bytes));
36                 break;
37         default:
38                 break;
39         }
40         return WIMLIB_PROGRESS_STATUS_CONTINUE;
41 }
42
43 int main(int argc, char **argv)
44 {
45         int ret;
46         WIMStruct *wim = NULL;
47         const char *srcdir;
48         const char *wimpath;
49
50         /* Check for the correct number of arguments.  */
51         if (argc != 3) {
52                 fprintf(stderr, "Usage: capturewim DIR WIM\n");
53                 return 2;
54         }
55
56         srcdir = argv[1];
57         wimpath = argv[2];
58
59         /* Create a WIMStruct for a WIM.  */
60         ret = wimlib_create_new_wim(WIMLIB_COMPRESSION_TYPE_LZX, &wim);
61         if (ret != 0)  /* Always should check the error codes.  */
62                 goto out;
63
64         /* Register our progress function.  */
65         wimlib_register_progress_function(wim, write_progress, NULL);
66
67         /* Add the directory tree to the WIMStruct as an image.  */
68
69         ret = wimlib_add_image(wim,     /* WIMStruct to which to add the image    */
70                                srcdir,  /* Directory from which to add the image  */
71                                NULL,    /* Name to give the image (NULL means none)  */
72                                NULL,    /* Capture configuration structure (NULL means none)  */
73                                0);      /* WIMLIB_ADD_FLAG_* flags (0 means all defaults)  */
74         if (ret != 0)
75                 goto out;
76
77         /* Write the WIM file.  */
78
79         ret = wimlib_write(wim,      /* WIMStruct from which to write a WIM  */
80                            wimpath,  /* Path to write the WIM to             */
81                            WIMLIB_ALL_IMAGES, /*  Image(s) in the WIM to write */
82                            0,        /* WIMLIB_WRITE_FLAG_* flags (0 means all defaults)   */
83                            0);       /* Number of compressor threads (0 means default)  */
84
85 out:
86         /* Free the WIMStruct.  Has no effect if the pointer to it is NULL.  */
87         wimlib_free(wim);
88
89         /* Check for error status.  */
90         if (ret != 0) {
91                 fprintf(stderr, "wimlib error %d: %s\n",
92                         ret, wimlib_get_error_string(ret));
93         }
94
95         /* Free global memory (optional).  */
96         wimlib_global_cleanup();
97
98         return ret;
99 }