]> wimlib.net Git - wimlib/blob - src/join.c
Simplify merging/unmerging of lookup tables when handling split WIMs
[wimlib] / src / join.c
1 /*
2  * join.c
3  *
4  * Join split WIMs (sometimes named as .swm files) together into one WIM.
5  */
6
7 /*
8  * Copyright (C) 2012, 2013 Eric Biggers
9  *
10  * This file is part of wimlib, a library for working with WIM files.
11  *
12  * wimlib is free software; you can redistribute it and/or modify it under the
13  * terms of the GNU General Public License as published by the Free
14  * Software Foundation; either version 3 of the License, or (at your option)
15  * any later version.
16  *
17  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19  * A PARTICULAR PURPOSE. See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with wimlib; if not, see http://www.gnu.org/licenses/.
24  */
25
26 #include "wimlib_internal.h"
27 #include "lookup_table.h"
28 #include "xml.h"
29 #include <stdlib.h>
30
31 static int
32 move_lte_to_table(struct wim_lookup_table_entry *lte, void *combined_table)
33 {
34         hlist_del(&lte->hash_list);
35         lookup_table_insert((struct wim_lookup_table*)combined_table, lte);
36         return 0;
37 }
38
39 static void
40 lookup_table_join(struct wim_lookup_table *combined_table,
41                   struct wim_lookup_table *part_table)
42 {
43         for_lookup_table_entry(part_table, move_lte_to_table, combined_table);
44         part_table->num_entries = 0;
45 }
46
47 /*
48  * merge_lookup_tables() - Merge lookup tables from the parts of a split WIM.
49  *
50  * @w specifies the first part, while @additional_swms and @num_additional_swms
51  * specify an array of pointers to the WIMStruct's for additional split WIM parts.
52  *
53  * The reason we join the lookup tables is so we only have to search one lookup
54  * table to find the location of a resource in the entire WIM.
55  */
56 void
57 merge_lookup_tables(WIMStruct *w,
58                     WIMStruct **additional_swms,
59                     unsigned num_additional_swms)
60 {
61         for (unsigned i = 0; i < num_additional_swms; i++)
62                 lookup_table_join(w->lookup_table, additional_swms[i]->lookup_table);
63 }
64
65 static int
66 move_lte_to_orig_table(struct wim_lookup_table_entry *lte, void *_wim)
67 {
68         WIMStruct *wim = _wim;
69         if (lte->wim != wim) {
70                 move_lte_to_table(lte, lte->wim->lookup_table);
71                 wim->lookup_table->num_entries--;
72         }
73         return 0;
74 }
75
76 /* Undo merge_lookup_tables(), given the first WIM part that contains the merged
77  * lookup table. */
78 void
79 unmerge_lookup_table(WIMStruct *wim)
80 {
81         for_lookup_table_entry(wim->lookup_table, move_lte_to_orig_table, wim);
82 }
83
84
85 static int
86 join_wims(WIMStruct **swms, unsigned num_swms,
87           WIMStruct *joined_wim, int write_flags,
88           wimlib_progress_func_t progress_func)
89 {
90         int ret;
91         unsigned i;
92         union wimlib_progress_info progress;
93         u64 total_bytes = 0;
94         u64 part_bytes;
95         u64 swm_part_sizes[num_swms];
96
97         /* Calculate total size of the streams in the split WIM parts. */
98         for (i = 0; i < num_swms; i++) {
99                 part_bytes = lookup_table_total_stream_size(swms[i]->lookup_table);
100                 swm_part_sizes[i] = part_bytes;
101                 total_bytes += part_bytes;
102         }
103
104         if (progress_func) {
105                 progress.join.total_bytes     = total_bytes;
106                 progress.join.total_parts     = swms[0]->hdr.total_parts;
107                 progress.join.completed_bytes = 0;
108                 progress.join.completed_parts = 0;
109                 progress_func(WIMLIB_PROGRESS_MSG_JOIN_STREAMS, &progress);
110         }
111
112         /* Write the non-metadata resources from each SWM part */
113         for (i = 0; i < num_swms; i++) {
114                 ret = reopen_wim(swms[i]);
115                 if (ret)
116                         return ret;
117                 swms[i]->out_fd = joined_wim->out_fd;
118                 swms[i]->hdr.part_number = 1;
119
120                 ret = for_lookup_table_entry_pos_sorted(swms[i]->lookup_table,
121                                                         copy_resource,
122                                                         swms[i]);
123                 swms[i]->out_fd = -1;
124                 if (i != 0)
125                         close_wim(swms[i]);
126
127                 if (ret)
128                         return ret;
129
130                 if (progress_func) {
131                         progress.join.completed_bytes += swm_part_sizes[i];
132                         progress.join.completed_parts++;
133                         progress_func(WIMLIB_PROGRESS_MSG_JOIN_STREAMS, &progress);
134                 }
135         }
136
137         /* Copy the metadata resources from the first SWM part */
138         joined_wim->hdr.image_count = swms[0]->hdr.image_count;
139         for (i = 0; i < joined_wim->hdr.image_count; i++) {
140                 ret = copy_resource(swms[0]->image_metadata[i]->metadata_lte,
141                                     joined_wim);
142                 if (ret)
143                         return ret;
144         }
145
146         /* Write lookup table, XML data, and optional integrity table */
147         for (i = 0; i < num_swms; i++)
148                 lookup_table_join(joined_wim->lookup_table, swms[i]->lookup_table);
149
150         free_wim_info(joined_wim->wim_info);
151         joined_wim->wim_info = swms[0]->wim_info;
152         joined_wim->image_metadata = swms[0]->image_metadata;
153         ret = finish_write(joined_wim, WIMLIB_ALL_IMAGES, write_flags, progress_func);
154         joined_wim->wim_info = NULL;
155         joined_wim->image_metadata = NULL;
156         return ret;
157 }
158
159 static int
160 cmp_swms_by_part_number(const void *swm1, const void *swm2)
161 {
162         u16 partno_1 = (*(const WIMStruct**)swm1)->hdr.part_number;
163         u16 partno_2 = (*(const WIMStruct**)swm2)->hdr.part_number;
164         return (int)partno_1 - (int)partno_2;
165 }
166
167 /*
168  * Join a set of split WIMs into a stand-alone WIM.
169  */
170 WIMLIBAPI int
171 wimlib_join(const tchar * const *swm_names,
172             unsigned num_swms,
173             const tchar *output_path,
174             int swm_open_flags,
175             int wim_write_flags,
176             wimlib_progress_func_t progress_func)
177 {
178         int ret;
179         WIMStruct *joined_wim = NULL;
180         unsigned i;
181
182         swm_open_flags |= WIMLIB_OPEN_FLAG_SPLIT_OK;
183         wim_write_flags &= WIMLIB_WRITE_MASK_PUBLIC;
184
185         if (num_swms < 1 || num_swms > 0xffff)
186                 return WIMLIB_ERR_INVALID_PARAM;
187
188         WIMStruct *swms[num_swms];
189         ZERO_ARRAY(swms);
190
191         for (i = 0; i < num_swms; i++) {
192                 ret = wimlib_open_wim(swm_names[i], swm_open_flags, &swms[i],
193                                       progress_func);
194                 if (ret)
195                         goto out_free_wims;
196
197                 /* Don't open all the parts at the same time, in case there are
198                  * a lot of them */
199                 close_wim(swms[i]);
200         }
201
202         qsort(swms, num_swms, sizeof(swms[0]), cmp_swms_by_part_number);
203
204         ret = verify_swm_set(swms[0], &swms[1], num_swms - 1);
205         if (ret)
206                 goto out_free_wims;
207
208         ret = wimlib_create_new_wim(wimlib_get_compression_type(swms[0]),
209                                     &joined_wim);
210         if (ret)
211                 goto out_free_wims;
212
213         ret = begin_write(joined_wim, output_path, wim_write_flags);
214         if (ret)
215                 goto out_free_wims;
216         ret = join_wims(swms, num_swms, joined_wim, wim_write_flags,
217                         progress_func);
218 out_free_wims:
219         for (i = 0; i < num_swms; i++)
220                 wimlib_free(swms[i]);
221         wimlib_free(joined_wim);
222         return ret;
223 }