]> wimlib.net Git - wimlib/blob - src/join.c
Refactor headers
[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 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include "wimlib.h"
31 #include "wimlib/lookup_table.h"
32 #include "wimlib/metadata.h"
33 #include "wimlib/resource.h"
34 #include "wimlib/swm.h"
35 #include "wimlib/wim.h"
36 #include "wimlib/write.h"
37 #include "wimlib/xml.h"
38
39 #include <stdlib.h> /* for qsort() */
40
41 static int
42 move_lte_to_table(struct wim_lookup_table_entry *lte, void *combined_table)
43 {
44         hlist_del(&lte->hash_list);
45         lookup_table_insert((struct wim_lookup_table*)combined_table, lte);
46         return 0;
47 }
48
49 static void
50 lookup_table_join(struct wim_lookup_table *combined_table,
51                   struct wim_lookup_table *part_table)
52 {
53         for_lookup_table_entry(part_table, move_lte_to_table, combined_table);
54         part_table->num_entries = 0;
55 }
56
57 /*
58  * merge_lookup_tables() - Merge lookup tables from the parts of a split WIM.
59  *
60  * @w specifies the first part, while @additional_swms and @num_additional_swms
61  * specify an array of pointers to the WIMStruct's for additional split WIM parts.
62  *
63  * The reason we join the lookup tables is so we only have to search one lookup
64  * table to find the location of a resource in the entire WIM.
65  */
66 void
67 merge_lookup_tables(WIMStruct *w,
68                     WIMStruct **additional_swms,
69                     unsigned num_additional_swms)
70 {
71         for (unsigned i = 0; i < num_additional_swms; i++)
72                 lookup_table_join(w->lookup_table, additional_swms[i]->lookup_table);
73 }
74
75 static int
76 move_lte_to_orig_table(struct wim_lookup_table_entry *lte, void *_wim)
77 {
78         WIMStruct *wim = _wim;
79         if (lte->wim != wim) {
80                 move_lte_to_table(lte, lte->wim->lookup_table);
81                 wim->lookup_table->num_entries--;
82         }
83         return 0;
84 }
85
86 /* Undo merge_lookup_tables(), given the first WIM part that contains the merged
87  * lookup table. */
88 void
89 unmerge_lookup_table(WIMStruct *wim)
90 {
91         for_lookup_table_entry(wim->lookup_table, move_lte_to_orig_table, wim);
92 }
93
94
95 static int
96 join_wims(WIMStruct **swms, unsigned num_swms,
97           WIMStruct *joined_wim, int write_flags,
98           wimlib_progress_func_t progress_func)
99 {
100         int ret;
101         unsigned i;
102         union wimlib_progress_info progress;
103         u64 total_bytes = 0;
104         u64 part_bytes;
105         u64 swm_part_sizes[num_swms];
106
107         /* Calculate total size of the streams in the split WIM parts. */
108         for (i = 0; i < num_swms; i++) {
109                 part_bytes = lookup_table_total_stream_size(swms[i]->lookup_table);
110                 swm_part_sizes[i] = part_bytes;
111                 total_bytes += part_bytes;
112         }
113
114         if (progress_func) {
115                 progress.join.total_bytes     = total_bytes;
116                 progress.join.total_parts     = swms[0]->hdr.total_parts;
117                 progress.join.completed_bytes = 0;
118                 progress.join.completed_parts = 0;
119                 progress_func(WIMLIB_PROGRESS_MSG_JOIN_STREAMS, &progress);
120         }
121
122         /* Write the non-metadata resources from each SWM part */
123         for (i = 0; i < num_swms; i++) {
124                 ret = reopen_wim(swms[i]);
125                 if (ret)
126                         return ret;
127                 swms[i]->out_fd = joined_wim->out_fd;
128                 swms[i]->hdr.part_number = 1;
129
130                 ret = for_lookup_table_entry_pos_sorted(swms[i]->lookup_table,
131                                                         copy_resource,
132                                                         swms[i]);
133                 swms[i]->out_fd = -1;
134                 if (i != 0)
135                         close_wim(swms[i]);
136
137                 if (ret)
138                         return ret;
139
140                 if (progress_func) {
141                         progress.join.completed_bytes += swm_part_sizes[i];
142                         progress.join.completed_parts++;
143                         progress_func(WIMLIB_PROGRESS_MSG_JOIN_STREAMS, &progress);
144                 }
145         }
146
147         /* Copy the metadata resources from the first SWM part */
148         joined_wim->hdr.image_count = swms[0]->hdr.image_count;
149         for (i = 0; i < joined_wim->hdr.image_count; i++) {
150                 ret = copy_resource(swms[0]->image_metadata[i]->metadata_lte,
151                                     joined_wim);
152                 if (ret)
153                         return ret;
154         }
155
156         /* Write lookup table, XML data, and optional integrity table */
157         for (i = 0; i < num_swms; i++)
158                 lookup_table_join(joined_wim->lookup_table, swms[i]->lookup_table);
159
160         free_wim_info(joined_wim->wim_info);
161         joined_wim->wim_info = swms[0]->wim_info;
162         joined_wim->image_metadata = swms[0]->image_metadata;
163         ret = finish_write(joined_wim, WIMLIB_ALL_IMAGES, write_flags, progress_func);
164         joined_wim->wim_info = NULL;
165         joined_wim->image_metadata = NULL;
166         return ret;
167 }
168
169 static int
170 cmp_swms_by_part_number(const void *swm1, const void *swm2)
171 {
172         u16 partno_1 = (*(const WIMStruct**)swm1)->hdr.part_number;
173         u16 partno_2 = (*(const WIMStruct**)swm2)->hdr.part_number;
174         return (int)partno_1 - (int)partno_2;
175 }
176
177 /*
178  * Join a set of split WIMs into a stand-alone WIM.
179  */
180 WIMLIBAPI int
181 wimlib_join(const tchar * const *swm_names,
182             unsigned num_swms,
183             const tchar *output_path,
184             int swm_open_flags,
185             int wim_write_flags,
186             wimlib_progress_func_t progress_func)
187 {
188         int ret;
189         WIMStruct *joined_wim = NULL;
190         unsigned i;
191
192         swm_open_flags |= WIMLIB_OPEN_FLAG_SPLIT_OK;
193         wim_write_flags &= WIMLIB_WRITE_MASK_PUBLIC;
194
195         if (num_swms < 1 || num_swms > 0xffff)
196                 return WIMLIB_ERR_INVALID_PARAM;
197
198         WIMStruct *swms[num_swms];
199         ZERO_ARRAY(swms);
200
201         for (i = 0; i < num_swms; i++) {
202                 ret = wimlib_open_wim(swm_names[i], swm_open_flags, &swms[i],
203                                       progress_func);
204                 if (ret)
205                         goto out_free_wims;
206
207                 /* Don't open all the parts at the same time, in case there are
208                  * a lot of them */
209                 close_wim(swms[i]);
210         }
211
212         qsort(swms, num_swms, sizeof(swms[0]), cmp_swms_by_part_number);
213
214         ret = verify_swm_set(swms[0], &swms[1], num_swms - 1);
215         if (ret)
216                 goto out_free_wims;
217
218         ret = wimlib_create_new_wim(wimlib_get_compression_type(swms[0]),
219                                     &joined_wim);
220         if (ret)
221                 goto out_free_wims;
222
223         ret = begin_write(joined_wim, output_path, wim_write_flags);
224         if (ret)
225                 goto out_free_wims;
226         ret = join_wims(swms, num_swms, joined_wim, wim_write_flags,
227                         progress_func);
228 out_free_wims:
229         for (i = 0; i < num_swms; i++)
230                 wimlib_free(swms[i]);
231         wimlib_free(joined_wim);
232         return ret;
233 }