4 * Join split WIMs (sometimes named as .swm files) together into one WIM.
8 * Copyright (C) 2012 Eric Biggers
10 * This file is part of wimlib, a library for working with WIM files.
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)
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
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/.
26 #include "wimlib_internal.h"
27 #include "lookup_table.h"
31 static int copy_lte_to_table(struct lookup_table_entry *lte, void *table)
33 struct lookup_table_entry *copy;
34 copy = MALLOC(sizeof(struct lookup_table_entry));
36 return WIMLIB_ERR_NOMEM;
37 memcpy(copy, lte, sizeof(struct lookup_table_entry));
38 lookup_table_insert(table, copy);
42 static int lookup_table_join(struct lookup_table *table,
43 struct lookup_table *new)
45 return for_lookup_table_entry(new, copy_lte_to_table, table);
49 static int cmp_swms_by_part_number(const void *swm1, const void *swm2)
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;
57 * Sanity checks to make sure a set of WIMs correctly correspond to a spanned
64 * All parts of the set other than part 1.
66 * @num_additional_swms:
67 * Number of WIMStructs in @additional_swms. Or, the total number of parts
71 * 0 on success; WIMLIB_ERR_SPLIT_INVALID if the set is not valid.
73 int verify_swm_set(WIMStruct *w, WIMStruct **additional_swms,
74 unsigned num_additional_swms)
76 unsigned total_parts = w->hdr.total_parts;
80 if (total_parts != num_additional_swms + 1) {
81 ERROR("`%s' says there are %u parts in the spanned set, "
82 "but %s%u part%s provided",
83 w->filename, total_parts,
84 (num_additional_swms + 1 < total_parts) ? "only " : "",
85 num_additional_swms + 1,
86 (num_additional_swms) ? "s were" : " was");
87 return WIMLIB_ERR_SPLIT_INVALID;
89 if (w->hdr.part_number != 1) {
90 ERROR("WIM `%s' is not the first part of the split WIM.",
92 return WIMLIB_ERR_SPLIT_INVALID;
94 for (unsigned i = 0; i < num_additional_swms; i++) {
95 if (additional_swms[i]->hdr.total_parts != total_parts) {
96 ERROR("WIM `%s' says there are %u parts in the spanned set, "
97 "but %u parts were provided",
98 additional_swms[i]->filename,
99 additional_swms[i]->hdr.total_parts,
101 return WIMLIB_ERR_SPLIT_INVALID;
105 /* keep track of ctype and guid just to make sure they are the same for
107 ctype = wimlib_get_compression_type(w);
110 WIMStruct *parts_to_swms[num_additional_swms];
111 ZERO_ARRAY(parts_to_swms);
112 for (unsigned i = 0; i < num_additional_swms; i++) {
114 WIMStruct *swm = additional_swms[i];
116 if (wimlib_get_compression_type(swm) != ctype) {
117 ERROR("The split WIMs do not all have the same "
119 return WIMLIB_ERR_SPLIT_INVALID;
121 if (memcmp(guid, swm->hdr.guid, WIM_GID_LEN) != 0) {
122 ERROR("The split WIMs do not all have the same "
124 return WIMLIB_ERR_SPLIT_INVALID;
126 if (swm->hdr.part_number == 1) {
127 ERROR("WIMs `%s' and `%s' both are marked as the "
128 "first WIM in the spanned set",
129 w->filename, swm->filename);
130 return WIMLIB_ERR_SPLIT_INVALID;
132 if (swm->hdr.part_number == 0 ||
133 swm->hdr.part_number > total_parts)
135 ERROR("WIM `%s' says it is part %u in the spanned set, "
136 "but the part number must be in the range "
138 swm->filename, swm->hdr.part_number, total_parts);
139 return WIMLIB_ERR_SPLIT_INVALID;
141 if (parts_to_swms[swm->hdr.part_number - 2])
143 ERROR("`%s' and `%s' are both marked as part %u of %u "
144 "in the spanned set",
145 parts_to_swms[swm->hdr.part_number - 2]->filename,
147 swm->hdr.part_number,
149 return WIMLIB_ERR_SPLIT_INVALID;
151 parts_to_swms[swm->hdr.part_number - 2] = swm;
158 * Joins lookup tables from the parts of a split WIM.
160 * @w specifies the first part, while @additional_swms and @num_additional_swms
161 * specify an array of points to the WIMStruct's for additional split WIM parts.
163 * On success, 0 is returned on a pointer to the joined lookup table is returned
166 * The reason we join the lookup tables is so:
167 * - We only have to search one lookup table to find the location of a
168 * resource in the entire split WIM.
169 * - Each lookup table entry will have a pointer to its split WIM part (and
170 * a part number field, although we don't really use it).
172 int new_joined_lookup_table(WIMStruct *w,
173 WIMStruct **additional_swms,
174 unsigned num_additional_swms,
175 struct lookup_table **table_ret)
177 struct lookup_table *table;
182 table = new_lookup_table(9001);
184 return WIMLIB_ERR_NOMEM;
185 ret = lookup_table_join(table, w->lookup_table);
188 for (i = 0; i < num_additional_swms; i++) {
189 ret = lookup_table_join(table, additional_swms[i]->lookup_table);
196 free_lookup_table(table);
201 static int join_wims(WIMStruct **swms, uint num_swms, WIMStruct *joined_wim,
206 FILE *out_fp = joined_wim->out_fp;
207 u64 total_bytes = wim_info_get_total_bytes(swms[0]->wim_info);
209 swms[0]->write_metadata = false;
210 for (i = 0; i < num_swms; i++) {
211 if (write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS) {
212 off_t cur_offset = ftello(out_fp);
213 printf("Writing resources from part %u of %u "
214 "(%"PRIu64" of %"PRIu64" bytes, %.0f%% done)\n",
215 i + 1, num_swms, cur_offset, total_bytes,
216 (double)cur_offset / total_bytes * 100.0);
218 swms[i]->fp = fopen(swms[i]->filename, "rb");
220 ERROR_WITH_ERRNO("Failed to reopen `%s'",
222 return WIMLIB_ERR_OPEN;
224 swms[i]->out_fp = out_fp;
225 swms[i]->hdr.part_number = 1;
226 ret = for_lookup_table_entry(swms[i]->lookup_table,
227 copy_resource, swms[i]);
235 swms[0]->write_metadata = true;
236 if (write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS)
237 printf("Writing %d metadata resources\n",
238 swms[0]->hdr.image_count);
240 for (i = 0; i < swms[0]->hdr.image_count; i++) {
241 ret = copy_resource(swms[0]->image_metadata[i].metadata_lte,
247 off_t lookup_table_offset = ftello(out_fp);
249 if (write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS)
250 printf("Writing lookup tables, XML data, and header\n");
251 /* Now write the lookup table for the joined wim. Since the lookup
252 * table has no header, we can just concatenate the lookup tables of all
254 for (i = 0; i < num_swms; i++) {
255 ret = write_lookup_table(swms[i]->lookup_table, out_fp);
259 off_t xml_data_offset = ftello(out_fp);
261 if (lookup_table_offset == -1 || xml_data_offset == -1) {
262 ERROR_WITH_ERRNO("Failed to get file offset");
263 return WIMLIB_ERR_WRITE;
265 swms[0]->hdr.lookup_table_res_entry.offset = lookup_table_offset;
266 swms[0]->hdr.lookup_table_res_entry.size =
267 xml_data_offset - lookup_table_offset;
270 /* finish_write is called on the first swm, not the joined_wim, because
271 * the first swm is the one that has the image metadata and XML data
273 swms[0]->hdr.flags &= ~WIM_HDR_FLAG_SPANNED;
274 swms[0]->hdr.total_parts = 1;
275 return finish_write(swms[0], WIM_ALL_IMAGES,
276 write_flags | WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE);
280 WIMLIBAPI int wimlib_join(const char **swm_names, unsigned num_swms,
281 const char *output_path, int flags)
285 WIMStruct *joined_wim = NULL;
286 WIMStruct *swms[num_swms];
289 return WIMLIB_ERR_INVALID_PARAM;
293 for (unsigned i = 0; i < num_swms; i++) {
294 ret = wimlib_open_wim(swm_names[i],
295 flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &swms[i]);
299 /* don't open all the parts at the same time, in case there are
305 qsort(swms, num_swms, sizeof(swms[0]), cmp_swms_by_part_number);
307 ret = verify_swm_set(swms[0], &swms[1], num_swms - 1);
311 joined_wim = new_wim_struct();
313 ret = WIMLIB_ERR_NOMEM;
317 if (flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY)
318 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
319 if (flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
320 write_flags |= WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
322 ret = begin_write(joined_wim, output_path, write_flags);
325 ret = join_wims(swms, num_swms, joined_wim, write_flags);
327 /* out_fp is the same in all the swms and joined_wim. And it was
328 * already closed in the call to finish_write(). */
329 for (unsigned i = 0; i < num_swms; i++) {
330 swms[i]->out_fp = NULL;
331 wimlib_free(swms[i]);
333 joined_wim->out_fp = NULL;
334 wimlib_free(joined_wim);