]> wimlib.net Git - wimlib/blob - examples/updatewim.c
system compression: add several more exclusion patterns
[wimlib] / examples / updatewim.c
1 /*
2  * updatewim.c - A program to add a file or directory tree to the first image of
3  * a WIM file.
4  *
5  * The author dedicates this file to the public domain.
6  * You can do whatever you want with this file.
7  */
8
9 #include <wimlib.h>
10 #include <string.h>
11
12 int main(int argc, char **argv)
13 {
14         int ret;
15         char *wimfile;
16         char *wim_target_path;
17         char *fs_source_path;
18         WIMStruct *wim = NULL;
19
20         /* Check for the correct number of arguments.  */
21         if (argc != 4) {
22                 fprintf(stderr, "Usage: updatewim WIMFILE WIM_PATH EXTERNAL_PATH\n");
23                 return 2;
24         }
25
26         wimfile = argv[1];
27         wim_target_path = argv[2];
28         fs_source_path = argv[3];
29
30         /* Open the WIM file.  */
31         ret = wimlib_open_wim(wimfile, 0, &wim);
32         if (ret != 0)  /* Always should check the error codes.  */
33                 goto out;
34
35         /* Update the WIM image.  In this simple example, we add a single file
36          * or directory tree to the specified location in the first image of the
37          * WIM file, using the default options.
38          *
39          * wimlib_add_tree() is actually sufficient for this case, but for the
40          * sake of demonstration we will use the more general function
41          * wimlib_update_image().  */
42
43         struct wimlib_update_command cmds[1];
44
45         memset(cmds, 0, sizeof(cmds));
46
47         /* Set up an "add" operation.
48          *
49          * Other available operations include WIMLIB_UPDATE_OP_RENAME and
50          * WIMLIB_UPDATE_OP_DELETE.  */
51         cmds[0].op = WIMLIB_UPDATE_OP_ADD;
52
53         /* Set the arguments to the operation.
54          *
55          * Make sure to fill in 'rename' or 'delete_' instead of 'add' if doing
56          * a rename or delete operation instead!  */
57         cmds[0].add.wim_target_path = wim_target_path;
58         cmds[0].add.fs_source_path = fs_source_path;
59
60         /* Note: we don't need to explicitly set 'cmds[0].add.config_file' and
61          * 'cmds[0].add.add_flags' because we zeroed the 'struct
62          * wimlib_update_command', and zero means use the defaults.  */
63
64         ret = wimlib_update_image(wim,  /* WIMStruct to update  */
65                                   1,    /* 1-based index of the image to update  */
66                                   cmds, /* Array of command structures  */
67                                   1,    /* Number of command structures in array  */
68                                   0);   /* WIMLIB_UPDATE_FLAG_* flags (0 for defaults)  */
69         if (ret != 0)
70                 goto out;
71
72         /* Overwrite the WIM file.
73          *
74          * Normally, this will append new data to the file, rather than
75          * rebuilding the entire file.
76          *
77          * Changes do not take effect on-disk until this is done.  */
78
79         ret = wimlib_overwrite(wim, /* WIMStruct to commit to the underlying file  */
80                                0,   /* WIMLIB_WRITE_FLAG_* flags (0 for defaults)   */
81                                0);  /* Number of compressor threads (0 means default)  */
82
83 out:
84         /* Free the WIMStruct.  Has no effect if the pointer to it is NULL.  */
85         wimlib_free(wim);
86
87         /* Check for error status.  */
88         if (ret != 0) {
89                 fprintf(stderr, "wimlib error %d: %s\n",
90                         ret, wimlib_get_error_string(ret));
91         }
92
93         /* Free global memory (optional).  */
94         wimlib_global_cleanup();
95
96         return ret;
97 }