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 INIT_LIST_HEAD(©->lte_group_list);
39 lookup_table_insert(table, copy);
43 static int lookup_table_join(struct lookup_table *table,
44 struct lookup_table *new)
46 return for_lookup_table_entry(new, copy_lte_to_table, table);
50 static int cmp_swms_by_part_number(const void *swm1, const void *swm2)
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;
57 int verify_swm_set(WIMStruct *w, WIMStruct **additional_swms,
58 unsigned num_additional_swms)
60 unsigned total_parts = w->hdr.total_parts;
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;
73 if (w->hdr.part_number != 1) {
74 ERROR("WIM `%s' is not the first part of the split WIM.",
76 return WIMLIB_ERR_SPLIT_INVALID;
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,
85 return WIMLIB_ERR_SPLIT_INVALID;
89 /* keep track of ctype and guid just to make sure they are the same for
91 ctype = wimlib_get_compression_type(w);
94 WIMStruct *parts_to_swms[num_additional_swms];
95 ZERO_ARRAY(parts_to_swms);
96 for (unsigned i = 0; i < num_additional_swms; i++) {
98 WIMStruct *swm = additional_swms[i];
100 if (wimlib_get_compression_type(swm) != ctype) {
101 ERROR("The split WIMs do not all have the same "
103 return WIMLIB_ERR_SPLIT_INVALID;
105 if (memcmp(guid, swm->hdr.guid, WIM_GID_LEN) != 0) {
106 ERROR("The split WIMs do not all have the same "
108 return WIMLIB_ERR_SPLIT_INVALID;
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;
116 if (swm->hdr.part_number == 0 ||
117 swm->hdr.part_number > total_parts)
119 ERROR("WIM `%s' says it is part %u in the spanned set, "
120 "but the part number must be in the range "
122 swm->filename, swm->hdr.part_number, total_parts);
123 return WIMLIB_ERR_SPLIT_INVALID;
125 if (parts_to_swms[swm->hdr.part_number - 2])
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,
131 swm->hdr.part_number,
133 return WIMLIB_ERR_SPLIT_INVALID;
135 parts_to_swms[swm->hdr.part_number - 2] = swm;
142 * Joins lookup tables from the parts of a split WIM.
144 * @w specifies the first part, while @additional_swms and @num_additional_swms
145 * specify an array of points to the WIMStruct's for additional split WIM parts.
147 * On success, 0 is returned on a pointer to the joined lookup table is returned
150 * The reason we join the lookup tables is so:
151 * - We only have to search one lookup table to find the location of a
152 * resource in the entire split WIM.
153 * - Each lookup table entry will have a pointer to its split WIM part (and
154 * a part number field, although we don't really use it).
156 int new_joined_lookup_table(WIMStruct *w,
157 WIMStruct **additional_swms,
158 unsigned num_additional_swms,
159 struct lookup_table **table_ret)
161 struct lookup_table *table;
166 table = new_lookup_table(9001);
168 return WIMLIB_ERR_NOMEM;
169 ret = lookup_table_join(table, w->lookup_table);
172 for (i = 0; i < num_additional_swms; i++) {
173 ret = lookup_table_join(table, additional_swms[i]->lookup_table);
180 free_lookup_table(table);
185 static int join_wims(WIMStruct **swms, uint num_swms, WIMStruct *joined_wim,
190 FILE *out_fp = joined_wim->out_fp;
191 u64 total_bytes = wim_info_get_total_bytes(swms[0]->wim_info);
193 swms[0]->write_metadata = false;
194 for (i = 0; i < num_swms; i++) {
195 if (write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS) {
196 off_t cur_offset = ftello(out_fp);
197 printf("Writing resources from part %u of %u "
198 "(%"PRIu64" of %"PRIu64" bytes, %.0f%% done)\n",
199 i + 1, num_swms, cur_offset, total_bytes,
200 (double)cur_offset / total_bytes * 100.0);
202 swms[i]->fp = fopen(swms[i]->filename, "rb");
204 ERROR_WITH_ERRNO("Failed to reopen `%s'",
206 return WIMLIB_ERR_OPEN;
208 swms[i]->out_fp = out_fp;
209 swms[i]->hdr.part_number = 1;
210 ret = for_lookup_table_entry(swms[i]->lookup_table,
211 copy_resource, swms[i]);
219 swms[0]->write_metadata = true;
220 if (write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS)
221 printf("Writing %d metadata resources\n",
222 swms[0]->hdr.image_count);
224 for (i = 0; i < swms[0]->hdr.image_count; i++) {
225 ret = copy_resource(swms[0]->image_metadata[i].metadata_lte,
231 off_t lookup_table_offset = ftello(out_fp);
233 if (write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS)
234 printf("Writing lookup tables, XML data, and header\n");
235 /* Now write the lookup table for the joined wim. Since the lookup
236 * table has no header, we can just concatenate the lookup tables of all
238 for (i = 0; i < num_swms; i++) {
239 ret = write_lookup_table(swms[i]->lookup_table, out_fp);
243 off_t xml_data_offset = ftello(out_fp);
245 if (lookup_table_offset == -1 || xml_data_offset == -1) {
246 ERROR_WITH_ERRNO("Failed to get file offset");
247 return WIMLIB_ERR_WRITE;
249 swms[0]->hdr.lookup_table_res_entry.offset = lookup_table_offset;
250 swms[0]->hdr.lookup_table_res_entry.size =
251 xml_data_offset - lookup_table_offset;
254 /* finish_write is called on the first swm, not the joined_wim, because
255 * the first swm is the one that has the image metadata and XML data
257 swms[0]->hdr.flags &= ~WIM_HDR_FLAG_SPANNED;
258 swms[0]->hdr.total_parts = 1;
259 return finish_write(swms[0], WIM_ALL_IMAGES, write_flags, 0);
263 WIMLIBAPI int wimlib_join(const char **swm_names, unsigned num_swms,
264 const char *output_path, int flags)
270 WIMStruct *joined_wim = NULL;
271 WIMStruct *swms[num_swms];
277 return WIMLIB_ERR_INVALID_PARAM;
281 for (i = 0; i < num_swms; i++) {
282 ret = wimlib_open_wim(swm_names[i],
283 flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &swms[i]);
287 /* don't open all the parts at the same time, in case there are
293 qsort(swms, num_swms, sizeof(swms[0]), cmp_swms_by_part_number);
295 ret = verify_swm_set(swms[0], &swms[1], num_swms - 1);
299 joined_wim = new_wim_struct();
301 ret = WIMLIB_ERR_NOMEM;
305 if (flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY)
306 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
307 if (flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
308 write_flags |= WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
310 ret = begin_write(joined_wim, output_path, write_flags);
313 ret = join_wims(swms, num_swms, joined_wim, write_flags);
315 for (i = 0; i < num_swms; i++) {
316 /* out_fp is the same in all the swms and joined_wim; only close
317 * it one time, when freeing joined_wim. */
319 swms[i]->out_fp = NULL;
320 wimlib_free(swms[i]);
323 wimlib_free(joined_wim);