]> wimlib.net Git - wimlib/blob - src/join.c
bf84efa503d96e7364725d423808d1986484c2be
[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 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 copy_lte_to_table(struct lookup_table_entry *lte, void *table)
32 {
33         struct lookup_table_entry *copy;
34         copy = MALLOC(sizeof(struct lookup_table_entry));
35         if (!copy)
36                 return WIMLIB_ERR_NOMEM;
37         memcpy(copy, lte, sizeof(struct lookup_table_entry));
38         lookup_table_insert(table, copy);
39         return 0;
40 }
41
42 static int lookup_table_join(struct lookup_table *table,
43                              struct lookup_table *new)
44 {
45         return for_lookup_table_entry(new, copy_lte_to_table, table);
46 }
47
48
49 static int cmp_swms_by_part_number(const void *swm1, const void *swm2)
50 {
51         u16 partno_1 = (*(WIMStruct**)swm1)->hdr.part_number;
52         u16 partno_2 = (*(WIMStruct**)swm2)->hdr.part_number;
53         return (int)partno_1 - (int)partno_2;
54 }
55
56 int verify_swm_set(WIMStruct *w, WIMStruct **additional_swms,
57                    unsigned num_additional_swms)
58 {
59         unsigned total_parts = w->hdr.total_parts;
60         int ctype;
61         const u8 *guid;
62
63         if (total_parts != num_additional_swms + 1) {
64                 ERROR("`%s' says there are %u parts in the spanned set, "
65                       "but %s%u part%s provided",
66                       w->filename, w->hdr.total_parts,
67                       (num_additional_swms + 1 < w->hdr.total_parts) ? "only " : "",
68                       num_additional_swms + 1,
69                       (num_additional_swms) ? "s were" : " was");
70                 return WIMLIB_ERR_SPLIT_INVALID;
71         }
72         if (w->hdr.part_number != 1) {
73                 ERROR("WIM `%s' is not the first part of the split WIM.",
74                       w->filename);
75                 return WIMLIB_ERR_SPLIT_INVALID;
76         }
77         for (unsigned i = 0; i < num_additional_swms; i++) {
78                 if (additional_swms[i]->hdr.total_parts != total_parts) {
79                         ERROR("WIM `%s' says there are %u parts in the spanned set, "
80                               "but %u parts were provided", 
81                               additional_swms[i]->filename,
82                               additional_swms[i]->hdr.total_parts,
83                               total_parts);
84                         return WIMLIB_ERR_SPLIT_INVALID;
85                 }
86         }
87
88         /* keep track of ctype and guid just to make sure they are the same for
89          * all the WIMs. */
90         ctype = wimlib_get_compression_type(w);
91         guid = w->hdr.guid;
92
93         WIMStruct *parts_to_swms[num_additional_swms];
94         ZERO_ARRAY(parts_to_swms);
95         for (unsigned i = 0; i < num_additional_swms; i++) {
96
97                 WIMStruct *swm = additional_swms[i];
98
99                 if (wimlib_get_compression_type(swm) != ctype) {
100                         ERROR("The split WIMs do not all have the same "
101                               "compression type");
102                         return WIMLIB_ERR_SPLIT_INVALID;
103                 }
104                 if (memcmp(guid, swm->hdr.guid, WIM_GID_LEN) != 0) {
105                         ERROR("The split WIMs do not all have the same "
106                               "GUID");
107                         return WIMLIB_ERR_SPLIT_INVALID;
108                 }
109                 if (swm->hdr.part_number == 1) {
110                         ERROR("WIMs `%s' and `%s' both are marked as the "
111                               "first WIM in the spanned set", 
112                               w->filename, swm->filename);
113                         return WIMLIB_ERR_SPLIT_INVALID;
114                 }
115                 if (swm->hdr.part_number == 0 ||
116                     swm->hdr.part_number > total_parts)
117                 {
118                         ERROR("WIM `%s' says it is part %u in the spanned set, "
119                               "but the part number must be in the range "
120                               "[1, %u]",
121                               swm->filename, swm->hdr.part_number, total_parts);
122                         return WIMLIB_ERR_SPLIT_INVALID;
123                 }
124                 if (parts_to_swms[swm->hdr.part_number - 2])
125                 {
126                         ERROR("`%s' and `%s' are both marked as part %u of %u "
127                               "in the spanned set",
128                               parts_to_swms[swm->hdr.part_number - 2]->filename,
129                               swm->filename,
130                               swm->hdr.part_number,
131                               total_parts);
132                         return WIMLIB_ERR_SPLIT_INVALID;
133                 } else {
134                         parts_to_swms[swm->hdr.part_number - 2] = swm;
135                 }
136         }
137         return 0;
138 }
139
140 /* 
141  * Joins lookup tables from the parts of a split WIM.
142  *
143  * @w specifies the first part, while @additional_swms and @num_additional_swms
144  * specify an array of points to the WIMStruct's for additional split WIM parts.
145  *
146  * On success, 0 is returned on a pointer to the joined lookup table is returned
147  * in @table_ret.
148  *
149  * The reason we join the lookup tables is so:
150  *      - We only have to search one lookup table to find the location of a
151  *      resource in the entire split WIM.
152  *      - Each lookup table entry will have a pointer to its split WIM part (and
153  *      a part number field, although we don't really use it).
154  */
155 int new_joined_lookup_table(WIMStruct *w,
156                             WIMStruct **additional_swms,
157                             unsigned num_additional_swms,
158                             struct lookup_table **table_ret)
159 {
160         struct lookup_table *table;
161         int ret;
162         unsigned i;
163
164
165         table = new_lookup_table(9001);
166         if (!table)
167                 return WIMLIB_ERR_NOMEM;
168         ret = lookup_table_join(table, w->lookup_table);
169         if (ret != 0)
170                 goto out_free_table;
171         for (i = 0; i < num_additional_swms; i++) {
172                 ret = lookup_table_join(table, additional_swms[i]->lookup_table);
173                 if (ret != 0)
174                         goto out_free_table;
175         }
176         *table_ret = table;
177         return 0;
178 out_free_table:
179         free_lookup_table(table);
180         return ret;
181 }
182
183
184 static int join_wims(WIMStruct **swms, uint num_swms, WIMStruct *joined_wim,
185                      int write_flags)
186 {
187         uint i;
188         int ret;
189         FILE *out_fp = joined_wim->out_fp;
190         u64 total_bytes = wim_info_get_total_bytes(swms[0]->wim_info);
191
192         swms[0]->write_metadata = false;
193         for (i = 0; i < num_swms; i++) {
194                 if (write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS) {
195                         off_t cur_offset = ftello(out_fp);
196                         printf("Writing resources from part %u of %u "
197                                "(%"PRIu64" of %"PRIu64" bytes, %.0f%% done)\n",
198                                i + 1, num_swms, cur_offset, total_bytes,
199                                (double)cur_offset / total_bytes * 100.0);
200                 }
201                 swms[i]->fp = fopen(swms[i]->filename, "rb");
202                 if (!swms[i]->fp) {
203                         ERROR_WITH_ERRNO("Failed to reopen `%s'",
204                                          swms[i]->filename);
205                         return WIMLIB_ERR_OPEN;
206                 }
207                 swms[i]->out_fp = out_fp;
208                 swms[i]->hdr.part_number = 1;
209                 ret = for_lookup_table_entry(swms[i]->lookup_table, 
210                                              copy_resource, swms[i]);
211                 if (ret != 0)
212                         return ret;
213                 if (i != 0) {
214                         fclose(swms[i]->fp);
215                         swms[i]->fp = NULL;
216                 }
217         }
218         swms[0]->write_metadata = true;
219         if (write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS)
220                 printf("Writing %d metadata resources\n", 
221                         swms[0]->hdr.image_count);
222
223         for (i = 0; i < swms[0]->hdr.image_count; i++) {
224                 ret = copy_resource(swms[0]->image_metadata[i].metadata_lte, 
225                                     swms[0]);
226                 if (ret != 0)
227                         return ret;
228         }
229
230         off_t lookup_table_offset = ftello(out_fp);
231
232         if (write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS)
233                 printf("Writing lookup tables, XML data, and header\n");
234         /* Now write the lookup table for the joined wim.  Since the lookup
235          * table has no header, we can just concatenate the lookup tables of all
236          * the SWM parts. */
237         for (i = 0; i < num_swms; i++) {
238                 ret = write_lookup_table(swms[i]->lookup_table, out_fp);
239                 if (ret != 0)
240                         return ret;
241         }
242         off_t xml_data_offset = ftello(out_fp);
243
244         if (lookup_table_offset == -1 || xml_data_offset == -1) {
245                 ERROR_WITH_ERRNO("Failed to get file offset");
246                 return WIMLIB_ERR_WRITE;
247         }
248         swms[0]->hdr.lookup_table_res_entry.offset = lookup_table_offset;
249         swms[0]->hdr.lookup_table_res_entry.size = 
250                                         xml_data_offset - lookup_table_offset;
251
252
253         /* finish_write is called on the first swm, not the joined_wim, because
254          * the first swm is the one that has the image metadata and XML data
255          * attached to it.  */
256         swms[0]->hdr.flags &= ~WIM_HDR_FLAG_SPANNED;
257         swms[0]->hdr.total_parts = 1;
258         return finish_write(swms[0], WIM_ALL_IMAGES, write_flags, 0);
259 }
260
261
262 WIMLIBAPI int wimlib_join(const char **swm_names, unsigned num_swms, 
263                           const char *output_path, int flags)
264 {
265         int i;
266         int ret;
267         int part_idx;
268         int write_flags = 0;
269         WIMStruct *joined_wim = NULL;
270         WIMStruct *swms[num_swms];
271
272         int ctype;
273         u8 *guid;
274
275         if (num_swms < 1)
276                 return WIMLIB_ERR_INVALID_PARAM;
277
278         ZERO_ARRAY(swms);
279
280         for (i = 0; i < num_swms; i++) {
281                 ret = wimlib_open_wim(swm_names[i], 
282                                       flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &swms[i]);
283                 if (ret != 0)
284                         goto out;
285
286                 /* don't open all the parts at the same time, in case there are
287                  * a lot of them */
288                 fclose(swms[i]->fp);
289                 swms[i]->fp = NULL;
290         }
291
292         qsort(swms, num_swms, sizeof(swms[0]), cmp_swms_by_part_number);
293
294         ret = verify_swm_set(swms[0], &swms[1], num_swms - 1);
295         if (ret != 0)
296                 goto out;
297
298         joined_wim = new_wim_struct();
299         if (!joined_wim) {
300                 ret = WIMLIB_ERR_NOMEM;
301                 goto out;
302         }
303
304         if (flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY)
305                 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
306         if (flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
307                 write_flags |= WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
308
309         ret = begin_write(joined_wim, output_path, write_flags);
310         if (ret != 0)
311                 goto out;
312         ret = join_wims(swms, num_swms, joined_wim, write_flags);
313 out:
314         for (i = 0; i < num_swms; i++) {
315                 /* out_fp is the same in all the swms and joined_wim; only close
316                  * it one time, when freeing joined_wim. */
317                 if (swms[i]) {
318                         swms[i]->out_fp = NULL;
319                         wimlib_free(swms[i]);
320                 }
321         }
322         wimlib_free(joined_wim);
323         return ret;
324 }