]> wimlib.net Git - wimlib/blob - examples/capturewim.c
lzms-compress.c: Don't do redundant work in cost calculations
[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 enum wimlib_progress_status
12 write_progress(enum wimlib_progress_msg msg,
13                union wimlib_progress_info *info, void *progctx)
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 WIMLIB_PROGRESS_STATUS_CONTINUE;
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         /* Register our progress function.  */
49         wimlib_register_progress_function(wim, write_progress, NULL);
50
51         /* Add the directory tree to the WIMStruct as an image.  */
52
53         ret = wimlib_add_image(wim,     /* WIMStruct to which to add the image    */
54                                srcdir,  /* Directory from which to add the image  */
55                                NULL,    /* Name to give the image (NULL means none)  */
56                                NULL,    /* Capture configuration structure (NULL means none)  */
57                                0);      /* WIMLIB_ADD_FLAG_* flags (0 means all defaults)  */
58         if (ret != 0)
59                 goto out;
60
61         /* Write the WIM file.  */
62
63         ret = wimlib_write(wim,      /* WIMStruct from which to write a WIM  */
64                            wimpath,  /* Path to write the WIM to             */
65                            WIMLIB_ALL_IMAGES, /*  Image(s) in the WIM to write */
66                            0,        /* WIMLIB_WRITE_FLAG_* flags (0 means all defaults)   */
67                            0);       /* Number of compressor threads (0 means default)  */
68
69 out:
70         /* Free the WIMStruct.  Has no effect if the pointer to it is NULL.  */
71         wimlib_free(wim);
72
73         /* Check for error status.  */
74         if (ret != 0) {
75                 fprintf(stderr, "wimlib error %d: %s\n",
76                         ret, wimlib_get_error_string(ret));
77         }
78
79         /* Free global memory (optional).  */
80         wimlib_global_cleanup();
81
82         return ret;
83 }