2 * updatewim.c - A program to add a file or directory tree to the first image of
5 * Copyright 2022 Eric Biggers
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
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
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.
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.
40 typedef wchar_t tchar;
47 int main(int argc, tchar **argv)
51 tchar *wim_target_path;
52 tchar *fs_source_path;
53 WIMStruct *wim = NULL;
54 struct wimlib_update_command cmds[1];
56 /* Check for the correct number of arguments. */
58 fprintf(stderr, "Usage: updatewim WIMFILE WIM_PATH EXTERNAL_PATH\n");
63 wim_target_path = argv[2];
64 fs_source_path = argv[3];
66 /* Open the WIM file. */
67 ret = wimlib_open_wim(wimfile, 0, &wim);
68 if (ret != 0) /* Always should check the error codes. */
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.
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(). */
79 memset(cmds, 0, sizeof(cmds));
81 /* Set up an "add" operation.
83 * Other available operations include WIMLIB_UPDATE_OP_RENAME and
84 * WIMLIB_UPDATE_OP_DELETE. */
85 cmds[0].op = WIMLIB_UPDATE_OP_ADD;
87 /* Set the arguments to the operation.
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;
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. */
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) */
106 /* Overwrite the WIM file.
108 * Normally, this will append new data to the file, rather than
109 * rebuilding the entire file.
111 * Changes do not take effect on-disk until this is done. */
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) */
118 /* Free the WIMStruct. Has no effect if the pointer to it is NULL. */
121 /* Check for error status. */
123 fprintf(stderr, "wimlib error %d: %" TS"\n",
124 ret, wimlib_get_error_string((enum wimlib_error_code)ret));
127 /* Free global memory (optional). */
128 wimlib_global_cleanup();