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