]> wimlib.net Git - wimlib/blob - src/add_image.c
Stream and blob updates
[wimlib] / src / add_image.c
1 /*
2  * add_image.c - Add an image to a WIM file.
3  */
4
5 /*
6  * Copyright (C) 2012, 2013, 2014 Eric Biggers
7  *
8  * This file is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License as published by the Free
10  * Software Foundation; either version 3 of the License, or (at your option) any
11  * later version.
12  *
13  * This file is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this file; if not, see http://www.gnu.org/licenses/.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25
26 #include "wimlib.h"
27 #include "wimlib/blob_table.h"
28 #include "wimlib/error.h"
29 #include "wimlib/metadata.h"
30 #include "wimlib/security.h"
31 #include "wimlib/xml.h"
32
33 /* Creates and appends a 'struct wim_image_metadata' for an empty image.
34  *
35  * The resulting image will be the last in the WIM, so its index will be
36  * the new value of wim->hdr.image_count.  */
37 static int
38 add_empty_image_metadata(WIMStruct *wim)
39 {
40         int ret;
41         struct blob_descriptor *metadata_blob;
42         struct wim_security_data *sd;
43         struct wim_image_metadata *imd;
44
45         /* Create a blob descriptor for the new metadata resource.  */
46         ret = WIMLIB_ERR_NOMEM;
47         metadata_blob = new_blob_descriptor();
48         if (!metadata_blob)
49                 goto out;
50
51         metadata_blob->flags = WIM_RESHDR_FLAG_METADATA;
52         metadata_blob->unhashed = 1;
53
54         /* Create empty security data (no security descriptors).  */
55         sd = new_wim_security_data();
56         if (!sd)
57                 goto out_free_metadata_blob;
58
59         imd = new_image_metadata();
60         if (!imd)
61                 goto out_free_security_data;
62
63         /* A NULL root_dentry indicates a completely empty image, without even a
64          * root directory.  */
65         imd->root_dentry = NULL;
66         imd->metadata_blob = metadata_blob;
67         imd->security_data = sd;
68         imd->modified = 1;
69
70         /* Append as next image index.  */
71         ret = append_image_metadata(wim, imd);
72         if (ret)
73                 put_image_metadata(imd, NULL);
74         goto out;
75
76 out_free_security_data:
77         free_wim_security_data(sd);
78 out_free_metadata_blob:
79         free_blob_descriptor(metadata_blob);
80 out:
81         return ret;
82 }
83
84 /* API function documented in wimlib.h  */
85 WIMLIBAPI int
86 wimlib_add_empty_image(WIMStruct *wim, const tchar *name, int *new_idx_ret)
87 {
88         int ret;
89
90         if (!name)
91                 name = T("");
92
93         if (wimlib_image_name_in_use(wim, name)) {
94                 ERROR("There is already an image named \"%"TS"\" in the WIM!",
95                       name);
96                 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
97         }
98
99         ret = add_empty_image_metadata(wim);
100         if (ret)
101                 return ret;
102
103         ret = xml_add_image(wim, name);
104         if (ret) {
105                 put_image_metadata(wim->image_metadata[--wim->hdr.image_count],
106                                    NULL);
107                 return ret;
108         }
109
110         if (new_idx_ret)
111                 *new_idx_ret = wim->hdr.image_count;
112         return 0;
113 }
114
115 /* Translate the 'struct wimlib_capture_source's passed to
116  * wimlib_add_image_multisource() into 'struct wimlib_update_command's for
117  * wimlib_update_image().  */
118 static struct wimlib_update_command *
119 capture_sources_to_add_cmds(const struct wimlib_capture_source *sources,
120                             size_t num_sources,
121                             int add_flags,
122                             const tchar *config_file)
123 {
124         struct wimlib_update_command *add_cmds;
125
126         add_cmds = CALLOC(num_sources, sizeof(add_cmds[0]));
127         if (!add_cmds)
128                 return NULL;
129
130         /* WIMLIB_ADD_FLAG_BOOT is handled by wimlib_add_image_multisource(),
131          * not wimlib_update_image(), so mask it out.
132          *
133          * However, WIMLIB_ADD_FLAG_WIMBOOT is handled by both.  */
134         add_flags &= ~WIMLIB_ADD_FLAG_BOOT;
135
136         for (size_t i = 0; i < num_sources; i++) {
137                 add_cmds[i].op = WIMLIB_UPDATE_OP_ADD;
138                 add_cmds[i].add.fs_source_path = sources[i].fs_source_path;
139                 add_cmds[i].add.wim_target_path = sources[i].wim_target_path;
140                 add_cmds[i].add.add_flags = add_flags;
141                 add_cmds[i].add.config_file = (tchar *)config_file;
142         }
143         return add_cmds;
144 }
145
146 /* API function documented in wimlib.h  */
147 WIMLIBAPI int
148 wimlib_add_image_multisource(WIMStruct *wim,
149                              const struct wimlib_capture_source *sources,
150                              size_t num_sources,
151                              const tchar *name,
152                              const tchar *config_file,
153                              int add_flags)
154 {
155         int ret;
156         struct wimlib_update_command *add_cmds;
157
158         /* Make sure no reserved fields are set.  */
159         for (size_t i = 0; i < num_sources; i++)
160                 if (sources[i].reserved != 0)
161                         return WIMLIB_ERR_INVALID_PARAM;
162
163         /* Add the new image (initially empty).  */
164         ret = wimlib_add_empty_image(wim, name, NULL);
165         if (ret)
166                 return ret;
167
168         /* Translate the "capture sources" into generic update commands.  */
169         ret = WIMLIB_ERR_NOMEM;
170         add_cmds = capture_sources_to_add_cmds(sources, num_sources,
171                                                add_flags, config_file);
172         if (!add_cmds)
173                 goto out_delete_image;
174
175         /* Delegate the work to wimlib_update_image().  */
176         ret = wimlib_update_image(wim, wim->hdr.image_count, add_cmds,
177                                   num_sources, 0);
178         FREE(add_cmds);
179         if (ret)
180                 goto out_delete_image;
181
182         /* If requested, set this image as the WIM's bootable image.  */
183         if (add_flags & WIMLIB_ADD_FLAG_BOOT)
184                 wim->hdr.boot_idx = wim->hdr.image_count;
185
186         /* If requested, mark new image as WIMBoot-compatible.  */
187         if (add_flags & WIMLIB_ADD_FLAG_WIMBOOT)
188                 wim_info_set_wimboot(wim->wim_info, wim->hdr.image_count, true);
189
190         return 0;
191
192 out_delete_image:
193         /* Unsuccessful; rollback by removing the new image.  */
194         delete_wim_image(wim, wim->hdr.image_count);
195         return ret;
196 }
197
198 /* API function documented in wimlib.h  */
199 WIMLIBAPI int
200 wimlib_add_image(WIMStruct *wim,
201                  const tchar *source,
202                  const tchar *name,
203                  const tchar *config_file,
204                  int add_flags)
205 {
206         /* Use the more general wimlib_add_image_multisource().  */
207         const struct wimlib_capture_source capture_src = {
208                 .fs_source_path = (tchar *)source,
209                 .wim_target_path = WIMLIB_WIM_ROOT_PATH,
210                 .reserved = 0,
211         };
212         return wimlib_add_image_multisource(wim, &capture_src, 1, name,
213                                             config_file, add_flags);
214 }