]> wimlib.net Git - wimlib/blob - src/export_image.c
avl_tree: Optimize swapping node for removal
[wimlib] / src / export_image.c
1 /*
2  * export_image.c
3  */
4
5 /*
6  * Copyright (C) 2012, 2013 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
10  * wimlib is free software; you can redistribute it and/or modify it under the
11  * terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option)
13  * any later version.
14  *
15  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17  * A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with wimlib; if not, see http://www.gnu.org/licenses/.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "wimlib.h"
29 #include "wimlib/dentry.h"
30 #include "wimlib/error.h"
31 #include "wimlib/inode.h"
32 #include "wimlib/lookup_table.h"
33 #include "wimlib/metadata.h"
34 #include "wimlib/xml.h"
35 #include <stdlib.h>
36
37 static int
38 inode_export_streams(struct wim_inode *inode,
39                      const struct wim_lookup_table *src_lookup_table,
40                      struct wim_lookup_table *dest_lookup_table)
41 {
42         unsigned i;
43         const u8 *hash;
44         struct wim_lookup_table_entry *src_lte, *dest_lte;
45
46         inode_unresolve_streams(inode);
47         for (i = 0; i <= inode->i_num_ads; i++) {
48
49                 /* Retrieve SHA1 message digest of stream to export.  */
50                 hash = inode_stream_hash(inode, i);
51                 if (is_zero_hash(hash))  /* Empty stream?  */
52                         continue;
53
54                 /* Search for the stream (via SHA1 message digest) in the
55                  * destination WIM.  */
56                 dest_lte = lookup_stream(dest_lookup_table, hash);
57                 if (!dest_lte) {
58                         /* Stream not yet present in destination WIM.  Search
59                          * for it in the source WIM, then export it into the
60                          * destination WIM.  */
61                         src_lte = lookup_stream(src_lookup_table, hash);
62                         if (!src_lte)
63                                 return stream_not_found_error(inode, hash);
64
65                         dest_lte = clone_lookup_table_entry(src_lte);
66                         if (!dest_lte)
67                                 return WIMLIB_ERR_NOMEM;
68                         dest_lte->refcnt = 0;
69                         dest_lte->out_refcnt = 0;
70                         lookup_table_insert(dest_lookup_table, dest_lte);
71                 }
72
73                 /* Stream is present in destination WIM (either pre-existing,
74                  * already exported, or just exported above).  Increment its
75                  * reference count appropriately.   Note: we use 'refcnt' for
76                  * the raw reference count, but 'out_refcnt' for references
77                  * arising just from the export operation; this is used to roll
78                  * back a failed export if needed.  */
79                 dest_lte->refcnt += inode->i_nlink;
80                 dest_lte->out_refcnt += inode->i_nlink;
81         }
82         return 0;
83 }
84
85 static int
86 lte_unexport(struct wim_lookup_table_entry *lte, void *_lookup_table)
87 {
88         struct wim_lookup_table *lookup_table = _lookup_table;
89
90         lte->refcnt -= lte->out_refcnt;
91         if (lte->refcnt == 0) {
92                 lookup_table_unlink(lookup_table, lte);
93                 free_lookup_table_entry(lte);
94         }
95         return 0;
96 }
97
98 /* API function documented in wimlib.h  */
99 WIMLIBAPI int
100 wimlib_export_image(WIMStruct *src_wim,
101                     int src_image,
102                     WIMStruct *dest_wim,
103                     const tchar *dest_name,
104                     const tchar *dest_description,
105                     int export_flags,
106                     wimlib_progress_func_t progress_func)
107 {
108         int ret;
109         int start_image;
110         int end_image;
111         int image;
112         u32 orig_dest_boot_idx;
113         u32 orig_dest_image_count;
114
115         /* Check for sane parameters.  */
116         if (export_flags & ~(WIMLIB_EXPORT_FLAG_BOOT |
117                              WIMLIB_EXPORT_FLAG_NO_NAMES |
118                              WIMLIB_EXPORT_FLAG_NO_DESCRIPTIONS))
119                 return WIMLIB_ERR_INVALID_PARAM;
120
121         if (src_wim == NULL || dest_wim == NULL)
122                 return WIMLIB_ERR_INVALID_PARAM;
123
124         if (!wim_has_metadata(dest_wim))
125                 return WIMLIB_ERR_METADATA_NOT_FOUND;
126
127         /* Destination WIM must be writable.  */
128         ret = can_modify_wim(dest_wim);
129         if (ret)
130                 return ret;
131
132         if (src_image == WIMLIB_ALL_IMAGES) {
133                 /* Multi-image export.  */
134                 if ((!(export_flags & WIMLIB_EXPORT_FLAG_NO_NAMES) &&
135                         dest_name) ||
136                     (!(export_flags & WIMLIB_EXPORT_FLAG_NO_DESCRIPTIONS) &&
137                         dest_description))
138                 {
139                         ERROR("Image name or image description was "
140                               "specified, but we are exporting "
141                               "multiple images");
142                         return WIMLIB_ERR_INVALID_PARAM;
143                 }
144                 start_image = 1;
145                 end_image = src_wim->hdr.image_count;
146         } else {
147                 start_image = src_image;
148                 end_image = src_image;
149         }
150
151         /* Stream checksums must be known before proceeding.  */
152         ret = wim_checksum_unhashed_streams(src_wim);
153         if (ret)
154                 return ret;
155         ret = wim_checksum_unhashed_streams(dest_wim);
156         if (ret)
157                 return ret;
158
159         /* Zero 'out_refcnt' in all lookup table entries in the destination WIM;
160          * this tracks the number of references found from the source WIM
161          * image(s).  */
162         for_lookup_table_entry(dest_wim->lookup_table, lte_zero_out_refcnt,
163                                NULL);
164
165         /* Save the original count of images in the destination WIM and the boot
166          * index (used if rollback necessary).  */
167         orig_dest_image_count = dest_wim->hdr.image_count;
168         orig_dest_boot_idx = dest_wim->hdr.boot_idx;
169
170         /* Export each requested image.  */
171         for (image = start_image; image <= end_image; image++) {
172                 const tchar *next_dest_name, *next_dest_description;
173                 struct wim_image_metadata *src_imd;
174                 struct wim_inode *inode;
175
176                 DEBUG("Exporting image %d from \"%"TS"\"",
177                       image, src_wim->filename);
178
179                 /* Determine destination image name and description.  */
180
181                 if (export_flags & WIMLIB_EXPORT_FLAG_NO_NAMES) {
182                         next_dest_name = T("");
183                 } else if (dest_name) {
184                         next_dest_name = dest_name;
185                 } else {
186                         next_dest_name = wimlib_get_image_name(src_wim,
187                                                                image);
188                 }
189
190                 DEBUG("Using name \"%"TS"\"", next_dest_name);
191
192                 if (export_flags & WIMLIB_EXPORT_FLAG_NO_DESCRIPTIONS) {
193                         next_dest_description = T("");
194                 } else if (dest_description) {
195                         next_dest_description = dest_description;
196                 } else {
197                         next_dest_description = wimlib_get_image_description(
198                                                         src_wim, image);
199                 }
200
201                 DEBUG("Using description \"%"TS"\"", next_dest_description);
202
203                 /* Check for name conflict.  */
204                 if (wimlib_image_name_in_use(dest_wim, next_dest_name)) {
205                         ERROR("There is already an image named \"%"TS"\" "
206                               "in the destination WIM", next_dest_name);
207                         ret = WIMLIB_ERR_IMAGE_NAME_COLLISION;
208                         goto out_rollback;
209                 }
210
211                 /* Load metadata for source image into memory.  */
212                 ret = select_wim_image(src_wim, image);
213                 if (ret)
214                         goto out_rollback;
215
216                 src_imd = wim_get_current_image_metadata(src_wim);
217
218                 /* Iterate through inodes in the source image and export their
219                  * streams into the destination WIM.  */
220                 image_for_each_inode(inode, src_imd) {
221                         ret = inode_export_streams(inode,
222                                                    src_wim->lookup_table,
223                                                    dest_wim->lookup_table);
224                         if (ret)
225                                 goto out_rollback;
226                 }
227
228                 /* Export XML information into the destination WIM.  */
229                 ret = xml_export_image(src_wim->wim_info, image,
230                                        &dest_wim->wim_info, next_dest_name,
231                                        next_dest_description);
232                 if (ret)
233                         goto out_rollback;
234
235                 /* Reference the source image metadata from the destination WIM.
236                  */
237                 ret = append_image_metadata(dest_wim, src_imd);
238                 if (ret)
239                         goto out_rollback;
240                 src_imd->refcnt++;
241
242                 /* Lock the metadata into memory.  XXX: need better solution for
243                  * this.  */
244                 src_imd->modified = 1;
245
246                 /* Set boot index in destination WIM.  */
247                 if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
248                     (src_image != WIMLIB_ALL_IMAGES ||
249                      image == src_wim->hdr.boot_idx))
250                 {
251                         DEBUG("Marking destination image %u as bootable.",
252                               dest_wim->hdr.image_count);
253                         dest_wim->hdr.boot_idx = dest_wim->hdr.image_count;
254                 }
255
256         }
257         /* Set the reparse point fixup flag on the destination WIM if the flag
258          * is set on the source WIM. */
259         if (src_wim->hdr.flags & WIM_HDR_FLAG_RP_FIX)
260                 dest_wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
261         DEBUG("Export operation successful.");
262         return 0;
263
264 out_rollback:
265         while ((image = wim_info_get_num_images(dest_wim->wim_info))
266                > orig_dest_image_count)
267         {
268                 xml_delete_image(&dest_wim->wim_info, image);
269         }
270         while (dest_wim->hdr.image_count > orig_dest_image_count)
271         {
272                 put_image_metadata(dest_wim->image_metadata[
273                                         --dest_wim->hdr.image_count], NULL);
274         }
275         for_lookup_table_entry(dest_wim->lookup_table, lte_unexport,
276                                dest_wim->lookup_table);
277         dest_wim->hdr.boot_idx = orig_dest_boot_idx;
278         return ret;
279 }