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 following copying information applies to this specific source code file:
6 *
7 * Written in 2014-2016 by Eric Biggers <ebiggers3@gmail.com>
8 *
9 * To the extent possible under law, the author(s) have dedicated all copyright
10 * and related and neighboring rights to this software to the public domain
11 * worldwide via the Creative Commons Zero 1.0 Universal Public Domain
12 * Dedication (the "CC0").
13 *
14 * This software is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the CC0 for more details.
17 *
18 * You should have received a copy of the CC0 along with this software; if not
19 * see <http://creativecommons.org/publicdomain/zero/1.0/>.
20 */
21
22 #include <wimlib.h>
23 #include <string.h>
24
25 /*
26 * Windows compatibility defines for string encoding. Applications using wimlib
27 * that need to run on both UNIX and Windows will need to do something similar
28 * to this, whereas applications that only need to run on one or the other can
29 * just use their platform's convention directly.
30 */
31 #ifdef _WIN32
32 # define main wmain
33 typedef wchar_t tchar;
34 # define TS "ls"
35 #else
36 typedef char tchar;
37 # define TS "s"
38 #endif
39
main(int argc,tchar ** argv)40 int main(int argc, tchar **argv)
41 {
42 int ret;
43 tchar *wimfile;
44 tchar *wim_target_path;
45 tchar *fs_source_path;
46 WIMStruct *wim = NULL;
47 struct wimlib_update_command cmds[1];
48
49 /* Check for the correct number of arguments. */
50 if (argc != 4) {
51 fprintf(stderr, "Usage: updatewim WIMFILE WIM_PATH EXTERNAL_PATH\n");
52 return 2;
53 }
54
55 wimfile = argv[1];
56 wim_target_path = argv[2];
57 fs_source_path = argv[3];
58
59 /* Open the WIM file. */
60 ret = wimlib_open_wim(wimfile, 0, &wim);
61 if (ret != 0) /* Always should check the error codes. */
62 goto out;
63
64 /* Update the WIM image. In this simple example, we add a single file
65 * or directory tree to the specified location in the first image of the
66 * WIM file, using the default options.
67 *
68 * wimlib_add_tree() is actually sufficient for this case, but for the
69 * sake of demonstration we will use the more general function
70 * wimlib_update_image(). */
71
72 memset(cmds, 0, sizeof(cmds));
73
74 /* Set up an "add" operation.
75 *
76 * Other available operations include WIMLIB_UPDATE_OP_RENAME and
77 * WIMLIB_UPDATE_OP_DELETE. */
78 cmds[0].op = WIMLIB_UPDATE_OP_ADD;
79
80 /* Set the arguments to the operation.
81 *
82 * Make sure to fill in 'rename' or 'delete_' instead of 'add' if doing
83 * a rename or delete operation instead! */
84 cmds[0].add.wim_target_path = wim_target_path;
85 cmds[0].add.fs_source_path = fs_source_path;
86
87 /* Note: we don't need to explicitly set 'cmds[0].add.config_file' and
88 * 'cmds[0].add.add_flags' because we zeroed the 'struct
89 * wimlib_update_command', and zero means use the defaults. */
90
91 ret = wimlib_update_image(wim, /* WIMStruct to update */
92 1, /* 1-based index of the image to update */
93 cmds, /* Array of command structures */
94 1, /* Number of command structures in array */
95 0); /* WIMLIB_UPDATE_FLAG_* flags (0 for defaults) */
96 if (ret != 0)
97 goto out;
98
99 /* Overwrite the WIM file.
100 *
101 * Normally, this will append new data to the file, rather than
102 * rebuilding the entire file.
103 *
104 * Changes do not take effect on-disk until this is done. */
105
106 ret = wimlib_overwrite(wim, /* WIMStruct to commit to the underlying file */
107 0, /* WIMLIB_WRITE_FLAG_* flags (0 for defaults) */
108 0); /* Number of compressor threads (0 means default) */
109
110 out:
111 /* Free the WIMStruct. Has no effect if the pointer to it is NULL. */
112 wimlib_free(wim);
113
114 /* Check for error status. */
115 if (ret != 0) {
116 fprintf(stderr, "wimlib error %d: %" TS"\n",
117 ret, wimlib_get_error_string((enum wimlib_error_code)ret));
118 }
119
120 /* Free global memory (optional). */
121 wimlib_global_cleanup();
122
123 return ret;
124 }
125