]> wimlib.net Git - wimlib/blob - examples/updatewim.c
mount_image.c: add fallback definitions of RENAME_* constants
[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  * Copyright 2022 Eric Biggers
6  *
7  * Permission is hereby granted, free of charge, to any person
8  * obtaining a copy of this software and associated documentation
9  * files (the "Software"), to deal in the Software without
10  * restriction, including without limitation the rights to use,
11  * copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following
14  * conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26  * OTHER DEALINGS IN THE SOFTWARE.
27  */
28
29 #include <wimlib.h>
30 #include <string.h>
31
32 /*
33  * Windows compatibility defines for string encoding.  Applications using wimlib
34  * that need to run on both UNIX and Windows will need to do something similar
35  * to this, whereas applications that only need to run on one or the other can
36  * just use their platform's convention directly.
37  */
38 #ifdef _WIN32
39 #  define main          wmain
40    typedef wchar_t      tchar;
41 #  define TS            "ls"
42 #else
43    typedef char         tchar;
44 #  define TS            "s"
45 #endif
46
47 int main(int argc, tchar **argv)
48 {
49         int ret;
50         tchar *wimfile;
51         tchar *wim_target_path;
52         tchar *fs_source_path;
53         WIMStruct *wim = NULL;
54         struct wimlib_update_command cmds[1];
55
56         /* Check for the correct number of arguments.  */
57         if (argc != 4) {
58                 fprintf(stderr, "Usage: updatewim WIMFILE WIM_PATH EXTERNAL_PATH\n");
59                 return 2;
60         }
61
62         wimfile = argv[1];
63         wim_target_path = argv[2];
64         fs_source_path = argv[3];
65
66         /* Open the WIM file.  */
67         ret = wimlib_open_wim(wimfile, 0, &wim);
68         if (ret != 0)  /* Always should check the error codes.  */
69                 goto out;
70
71         /* Update the WIM image.  In this simple example, we add a single file
72          * or directory tree to the specified location in the first image of the
73          * WIM file, using the default options.
74          *
75          * wimlib_add_tree() is actually sufficient for this case, but for the
76          * sake of demonstration we will use the more general function
77          * wimlib_update_image().  */
78
79         memset(cmds, 0, sizeof(cmds));
80
81         /* Set up an "add" operation.
82          *
83          * Other available operations include WIMLIB_UPDATE_OP_RENAME and
84          * WIMLIB_UPDATE_OP_DELETE.  */
85         cmds[0].op = WIMLIB_UPDATE_OP_ADD;
86
87         /* Set the arguments to the operation.
88          *
89          * Make sure to fill in 'rename' or 'delete_' instead of 'add' if doing
90          * a rename or delete operation instead!  */
91         cmds[0].add.wim_target_path = wim_target_path;
92         cmds[0].add.fs_source_path = fs_source_path;
93
94         /* Note: we don't need to explicitly set 'cmds[0].add.config_file' and
95          * 'cmds[0].add.add_flags' because we zeroed the 'struct
96          * wimlib_update_command', and zero means use the defaults.  */
97
98         ret = wimlib_update_image(wim,  /* WIMStruct to update  */
99                                   1,    /* 1-based index of the image to update  */
100                                   cmds, /* Array of command structures  */
101                                   1,    /* Number of command structures in array  */
102                                   0);   /* WIMLIB_UPDATE_FLAG_* flags (0 for defaults)  */
103         if (ret != 0)
104                 goto out;
105
106         /* Overwrite the WIM file.
107          *
108          * Normally, this will append new data to the file, rather than
109          * rebuilding the entire file.
110          *
111          * Changes do not take effect on-disk until this is done.  */
112
113         ret = wimlib_overwrite(wim, /* WIMStruct to commit to the underlying file  */
114                                0,   /* WIMLIB_WRITE_FLAG_* flags (0 for defaults)   */
115                                0);  /* Number of compressor threads (0 means default)  */
116
117 out:
118         /* Free the WIMStruct.  Has no effect if the pointer to it is NULL.  */
119         wimlib_free(wim);
120
121         /* Check for error status.  */
122         if (ret != 0) {
123                 fprintf(stderr, "wimlib error %d: %" TS"\n",
124                         ret, wimlib_get_error_string((enum wimlib_error_code)ret));
125         }
126
127         /* Free global memory (optional).  */
128         wimlib_global_cleanup();
129
130         return ret;
131 }