]> wimlib.net Git - wimlib/blob - src/export_image.c
export_image.c: adjust error message
[wimlib] / src / export_image.c
1 /*
2  * export_image.c
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/error.h"
28 #include "wimlib/inode.h"
29 #include "wimlib/lookup_table.h"
30 #include "wimlib/metadata.h"
31 #include "wimlib/xml.h"
32
33 static int
34 lte_set_not_exported(struct wim_lookup_table_entry *lte, void *_ignore)
35 {
36         lte->out_refcnt = 0;
37         lte->was_exported = 0;
38         return 0;
39 }
40
41 static int
42 lte_rollback_export(struct wim_lookup_table_entry *lte, void *_lookup_table)
43 {
44         struct wim_lookup_table *lookup_table = _lookup_table;
45
46         lte->refcnt -= lte->out_refcnt;
47         if (lte->was_exported) {
48                 lookup_table_unlink(lookup_table, lte);
49                 free_lookup_table_entry(lte);
50         }
51         return 0;
52 }
53
54 static int
55 inode_export_streams(struct wim_inode *inode,
56                      struct wim_lookup_table *src_lookup_table,
57                      struct wim_lookup_table *dest_lookup_table,
58                      bool gift)
59 {
60         unsigned i;
61         const u8 *hash;
62         struct wim_lookup_table_entry *src_lte, *dest_lte;
63
64         inode_unresolve_streams(inode);
65         for (i = 0; i <= inode->i_num_ads; i++) {
66
67                 /* Retrieve SHA1 message digest of stream to export.  */
68                 hash = inode_stream_hash(inode, i);
69                 if (is_zero_hash(hash))  /* Empty stream?  */
70                         continue;
71
72                 /* Search for the stream (via SHA1 message digest) in the
73                  * destination WIM.  */
74                 dest_lte = lookup_stream(dest_lookup_table, hash);
75                 if (!dest_lte) {
76                         /* Stream not yet present in destination WIM.  Search
77                          * for it in the source WIM, then export it into the
78                          * destination WIM.  */
79                         src_lte = lookup_stream(src_lookup_table, hash);
80                         if (!src_lte)
81                                 return stream_not_found_error(inode, hash);
82
83                         if (gift) {
84                                 dest_lte = src_lte;
85                                 lookup_table_unlink(src_lookup_table, src_lte);
86                         } else {
87                                 dest_lte = clone_lookup_table_entry(src_lte);
88                                 if (!dest_lte)
89                                         return WIMLIB_ERR_NOMEM;
90                         }
91                         dest_lte->refcnt = 0;
92                         dest_lte->out_refcnt = 0;
93                         dest_lte->was_exported = 1;
94                         lookup_table_insert(dest_lookup_table, dest_lte);
95                 }
96
97                 /* Stream is present in destination WIM (either pre-existing,
98                  * already exported, or just exported above).  Increment its
99                  * reference count appropriately.   Note: we use 'refcnt' for
100                  * the raw reference count, but 'out_refcnt' for references
101                  * arising just from the export operation; this is used to roll
102                  * back a failed export if needed.  */
103                 dest_lte->refcnt += inode->i_nlink;
104                 dest_lte->out_refcnt += inode->i_nlink;
105         }
106         return 0;
107 }
108
109 /* API function documented in wimlib.h  */
110 WIMLIBAPI int
111 wimlib_export_image(WIMStruct *src_wim,
112                     int src_image,
113                     WIMStruct *dest_wim,
114                     const tchar *dest_name,
115                     const tchar *dest_description,
116                     int export_flags)
117 {
118         int ret;
119         int start_src_image;
120         int end_src_image;
121         int orig_dest_image_count;
122         int image;
123         bool all_images = (src_image == WIMLIB_ALL_IMAGES);
124
125         /* Check for sane parameters.  */
126         if (export_flags & ~(WIMLIB_EXPORT_FLAG_BOOT |
127                              WIMLIB_EXPORT_FLAG_NO_NAMES |
128                              WIMLIB_EXPORT_FLAG_NO_DESCRIPTIONS |
129                              WIMLIB_EXPORT_FLAG_GIFT |
130                              WIMLIB_EXPORT_FLAG_WIMBOOT))
131                 return WIMLIB_ERR_INVALID_PARAM;
132
133         if (src_wim == NULL || dest_wim == NULL)
134                 return WIMLIB_ERR_INVALID_PARAM;
135
136         if (!wim_has_metadata(src_wim) || !wim_has_metadata(dest_wim))
137                 return WIMLIB_ERR_METADATA_NOT_FOUND;
138
139         if (all_images) {
140                 /* Multi-image export.  */
141                 if ((!(export_flags & WIMLIB_EXPORT_FLAG_NO_NAMES) &&
142                         dest_name) ||
143                     (!(export_flags & WIMLIB_EXPORT_FLAG_NO_DESCRIPTIONS) &&
144                         dest_description))
145                 {
146                         ERROR("Image name and description must be "
147                               "left NULL for multi-image export");
148                         return WIMLIB_ERR_INVALID_PARAM;
149                 }
150                 start_src_image = 1;
151                 end_src_image = src_wim->hdr.image_count;
152         } else {
153                 start_src_image = src_image;
154                 end_src_image = src_image;
155         }
156         orig_dest_image_count = dest_wim->hdr.image_count;
157
158         /* Stream checksums must be known before proceeding.  */
159         ret = wim_checksum_unhashed_streams(src_wim);
160         if (ret)
161                 return ret;
162         ret = wim_checksum_unhashed_streams(dest_wim);
163         if (ret)
164                 return ret;
165
166         /* Enable rollbacks  */
167         for_lookup_table_entry(dest_wim->lookup_table, lte_set_not_exported, NULL);
168
169         /* Export each requested image.  */
170         for (src_image = start_src_image;
171              src_image <= end_src_image;
172              src_image++)
173         {
174                 const tchar *next_dest_name, *next_dest_description;
175                 struct wim_image_metadata *src_imd;
176                 struct wim_inode *inode;
177
178                 /* Determine destination image name and description.  */
179
180                 if (export_flags & WIMLIB_EXPORT_FLAG_NO_NAMES)
181                         next_dest_name = T("");
182                 else if (dest_name)
183                         next_dest_name = dest_name;
184                 else
185                         next_dest_name = wimlib_get_image_name(src_wim, src_image);
186
187                 if (export_flags & WIMLIB_EXPORT_FLAG_NO_DESCRIPTIONS)
188                         next_dest_description = T("");
189                 else if (dest_description)
190                         next_dest_description = dest_description;
191                 else
192                         next_dest_description = wimlib_get_image_description(src_wim, src_image);
193
194                 /* Check for name conflict.  */
195                 if (wimlib_image_name_in_use(dest_wim, next_dest_name)) {
196                         ERROR("There is already an image named \"%"TS"\" "
197                               "in the destination WIM", next_dest_name);
198                         ret = WIMLIB_ERR_IMAGE_NAME_COLLISION;
199                         goto out_rollback;
200                 }
201
202                 /* Load metadata for source image into memory.  */
203                 ret = select_wim_image(src_wim, src_image);
204                 if (ret)
205                         goto out_rollback;
206
207                 src_imd = wim_get_current_image_metadata(src_wim);
208
209                 /* Iterate through inodes in the source image and export their
210                  * streams into the destination WIM.  */
211                 image_for_each_inode(inode, src_imd) {
212                         ret = inode_export_streams(inode,
213                                                    src_wim->lookup_table,
214                                                    dest_wim->lookup_table,
215                                                    export_flags & WIMLIB_EXPORT_FLAG_GIFT);
216                         if (ret)
217                                 goto out_rollback;
218                 }
219
220                 /* Export XML information into the destination WIM.  */
221                 ret = xml_export_image(src_wim->wim_info, src_image,
222                                        &dest_wim->wim_info, next_dest_name,
223                                        next_dest_description);
224                 if (ret)
225                         goto out_rollback;
226
227                 /* Reference the source image metadata from the destination WIM.
228                  */
229                 ret = append_image_metadata(dest_wim, src_imd);
230                 if (ret)
231                         goto out_rollback;
232                 src_imd->refcnt++;
233
234                 /* Lock the metadata into memory.  XXX: need better solution for
235                  * this.  */
236                 src_imd->modified = 1;
237
238         }
239
240         /* Image export complete.  Finish by setting any needed special metadata
241          * on the destination WIM.  */
242
243         if (src_wim->hdr.flags & WIM_HDR_FLAG_RP_FIX)
244                 dest_wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
245
246         for (src_image = start_src_image;
247              src_image <= end_src_image;
248              src_image++)
249         {
250                 int dst_image = orig_dest_image_count + 1 +
251                                 (src_image - start_src_image);
252
253                 if (export_flags & WIMLIB_EXPORT_FLAG_WIMBOOT)
254                         wim_info_set_wimboot(dest_wim->wim_info, dst_image, true);
255
256                 if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
257                     (!all_images || src_image == src_wim->hdr.boot_idx))
258                         dest_wim->hdr.boot_idx = dst_image;
259         }
260
261         if (export_flags & WIMLIB_EXPORT_FLAG_GIFT) {
262                 free_lookup_table(src_wim->lookup_table);
263                 src_wim->lookup_table = NULL;
264         }
265         return 0;
266
267 out_rollback:
268         while ((image = wim_info_get_num_images(dest_wim->wim_info))
269                > orig_dest_image_count)
270         {
271                 xml_delete_image(&dest_wim->wim_info, image);
272         }
273         while (dest_wim->hdr.image_count > orig_dest_image_count)
274         {
275                 put_image_metadata(dest_wim->image_metadata[
276                                         --dest_wim->hdr.image_count], NULL);
277         }
278         for_lookup_table_entry(dest_wim->lookup_table, lte_rollback_export,
279                                dest_wim->lookup_table);
280         return ret;
281 }