]> wimlib.net Git - wimlib/blob - src/export_image.c
Don't immediately discard streams with 0 references
[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         if (lte->out_refcnt) {
91                 lte->refcnt -= lte->out_refcnt;
92                 if (lte->refcnt == 0) {
93                         lookup_table_unlink(lookup_table, lte);
94                         free_lookup_table_entry(lte);
95                 }
96         }
97         return 0;
98 }
99
100 /* API function documented in wimlib.h  */
101 WIMLIBAPI int
102 wimlib_export_image(WIMStruct *src_wim,
103                     int src_image,
104                     WIMStruct *dest_wim,
105                     const tchar *dest_name,
106                     const tchar *dest_description,
107                     int export_flags,
108                     wimlib_progress_func_t progress_func)
109 {
110         int ret;
111         int start_image;
112         int end_image;
113         int image;
114         u32 orig_dest_boot_idx;
115         u32 orig_dest_image_count;
116
117         /* Check for sane parameters.  */
118         if (export_flags & ~(WIMLIB_EXPORT_FLAG_BOOT |
119                              WIMLIB_EXPORT_FLAG_NO_NAMES |
120                              WIMLIB_EXPORT_FLAG_NO_DESCRIPTIONS))
121                 return WIMLIB_ERR_INVALID_PARAM;
122
123         if (src_wim == NULL || dest_wim == NULL)
124                 return WIMLIB_ERR_INVALID_PARAM;
125
126         if (!wim_has_metadata(dest_wim))
127                 return WIMLIB_ERR_METADATA_NOT_FOUND;
128
129         /* Destination WIM must be writable.  */
130         ret = can_modify_wim(dest_wim);
131         if (ret)
132                 return ret;
133
134         if (src_image == WIMLIB_ALL_IMAGES) {
135                 /* Multi-image export.  */
136                 if ((!(export_flags & WIMLIB_EXPORT_FLAG_NO_NAMES) &&
137                         dest_name) ||
138                     (!(export_flags & WIMLIB_EXPORT_FLAG_NO_DESCRIPTIONS) &&
139                         dest_description))
140                 {
141                         ERROR("Image name or image description was "
142                               "specified, but we are exporting "
143                               "multiple images");
144                         return WIMLIB_ERR_INVALID_PARAM;
145                 }
146                 start_image = 1;
147                 end_image = src_wim->hdr.image_count;
148         } else {
149                 start_image = src_image;
150                 end_image = src_image;
151         }
152
153         /* Stream checksums must be known before proceeding.  */
154         ret = wim_checksum_unhashed_streams(src_wim);
155         if (ret)
156                 return ret;
157         ret = wim_checksum_unhashed_streams(dest_wim);
158         if (ret)
159                 return ret;
160
161         /* Zero 'out_refcnt' in all lookup table entries in the destination WIM;
162          * this tracks the number of references found from the source WIM
163          * image(s).  */
164         for_lookup_table_entry(dest_wim->lookup_table, lte_zero_out_refcnt,
165                                NULL);
166
167         /* Save the original count of images in the destination WIM and the boot
168          * index (used if rollback necessary).  */
169         orig_dest_image_count = dest_wim->hdr.image_count;
170         orig_dest_boot_idx = dest_wim->hdr.boot_idx;
171
172         /* Export each requested image.  */
173         for (image = start_image; image <= end_image; image++) {
174                 const tchar *next_dest_name, *next_dest_description;
175                 struct wim_image_metadata *src_imd;
176                 struct wim_inode *inode;
177
178                 DEBUG("Exporting image %d from \"%"TS"\"",
179                       image, src_wim->filename);
180
181                 /* Determine destination image name and description.  */
182
183                 if (export_flags & WIMLIB_EXPORT_FLAG_NO_NAMES) {
184                         next_dest_name = T("");
185                 } else if (dest_name) {
186                         next_dest_name = dest_name;
187                 } else {
188                         next_dest_name = wimlib_get_image_name(src_wim,
189                                                                image);
190                 }
191
192                 DEBUG("Using name \"%"TS"\"", next_dest_name);
193
194                 if (export_flags & WIMLIB_EXPORT_FLAG_NO_DESCRIPTIONS) {
195                         next_dest_description = T("");
196                 } else if (dest_description) {
197                         next_dest_description = dest_description;
198                 } else {
199                         next_dest_description = wimlib_get_image_description(
200                                                         src_wim, image);
201                 }
202
203                 DEBUG("Using description \"%"TS"\"", next_dest_description);
204
205                 /* Check for name conflict.  */
206                 if (wimlib_image_name_in_use(dest_wim, next_dest_name)) {
207                         ERROR("There is already an image named \"%"TS"\" "
208                               "in the destination WIM", next_dest_name);
209                         ret = WIMLIB_ERR_IMAGE_NAME_COLLISION;
210                         goto out_rollback;
211                 }
212
213                 /* Load metadata for source image into memory.  */
214                 ret = select_wim_image(src_wim, image);
215                 if (ret)
216                         goto out_rollback;
217
218                 src_imd = wim_get_current_image_metadata(src_wim);
219
220                 /* Iterate through inodes in the source image and export their
221                  * streams into the destination WIM.  */
222                 image_for_each_inode(inode, src_imd) {
223                         ret = inode_export_streams(inode,
224                                                    src_wim->lookup_table,
225                                                    dest_wim->lookup_table);
226                         if (ret)
227                                 goto out_rollback;
228                 }
229
230                 /* Export XML information into the destination WIM.  */
231                 ret = xml_export_image(src_wim->wim_info, image,
232                                        &dest_wim->wim_info, next_dest_name,
233                                        next_dest_description);
234                 if (ret)
235                         goto out_rollback;
236
237                 /* Reference the source image metadata from the destination WIM.
238                  */
239                 ret = append_image_metadata(dest_wim, src_imd);
240                 if (ret)
241                         goto out_rollback;
242                 src_imd->refcnt++;
243
244                 /* Lock the metadata into memory.  XXX: need better solution for
245                  * this.  */
246                 src_imd->modified = 1;
247
248                 /* Set boot index in destination WIM.  */
249                 if ((export_flags & WIMLIB_EXPORT_FLAG_BOOT) &&
250                     (src_image != WIMLIB_ALL_IMAGES ||
251                      image == src_wim->hdr.boot_idx))
252                 {
253                         DEBUG("Marking destination image %u as bootable.",
254                               dest_wim->hdr.image_count);
255                         dest_wim->hdr.boot_idx = dest_wim->hdr.image_count;
256                 }
257
258         }
259         /* Set the reparse point fixup flag on the destination WIM if the flag
260          * is set on the source WIM. */
261         if (src_wim->hdr.flags & WIM_HDR_FLAG_RP_FIX)
262                 dest_wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
263         DEBUG("Export operation successful.");
264         return 0;
265
266 out_rollback:
267         while ((image = wim_info_get_num_images(dest_wim->wim_info))
268                > orig_dest_image_count)
269         {
270                 xml_delete_image(&dest_wim->wim_info, image);
271         }
272         while (dest_wim->hdr.image_count > orig_dest_image_count)
273         {
274                 put_image_metadata(dest_wim->image_metadata[
275                                         --dest_wim->hdr.image_count], NULL);
276         }
277         for_lookup_table_entry(dest_wim->lookup_table, lte_unexport,
278                                dest_wim->lookup_table);
279         dest_wim->hdr.boot_idx = orig_dest_boot_idx;
280         return ret;
281 }