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