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