4 * Join split WIMs (sometimes named as .swm files) together into one WIM.
8 * Copyright (C) 2012, 2013 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"
32 move_lte_to_table(struct wim_lookup_table_entry *lte, void *other_tab)
34 hlist_del(<e->hash_list);
35 lookup_table_insert((struct wim_lookup_table*)other_tab, lte);
40 lookup_table_join(struct wim_lookup_table *table,
41 struct wim_lookup_table *new)
43 return for_lookup_table_entry(new, move_lte_to_table, table);
47 * new_joined_lookup_table: - Join lookup tables from the parts of a split WIM.
49 * @w specifies the first part, while @additional_swms and @num_additional_swms
50 * specify an array of pointers to the WIMStruct's for additional split WIM parts.
52 * The lookup table entries are *moved* to the new table.
54 * On success, 0 is returned on a pointer to the joined lookup table is returned
57 * The reason we join the lookup tables is so we only have to search one lookup
58 * table to find the location of a resource in the entire WIM.
61 new_joined_lookup_table(WIMStruct *w,
62 WIMStruct **additional_swms,
63 unsigned num_additional_swms,
64 struct wim_lookup_table **table_ret)
66 struct wim_lookup_table *table;
70 table = new_lookup_table(9001);
72 return WIMLIB_ERR_NOMEM;
75 lookup_table_join(table, w->lookup_table);
77 for (i = 0; i < num_additional_swms; i++) {
78 ret = lookup_table_join(table, additional_swms[i]->lookup_table);
85 free_lookup_table(table);
91 join_wims(WIMStruct **swms, unsigned num_swms,
92 WIMStruct *joined_wim, int write_flags,
93 wimlib_progress_func_t progress_func)
97 union wimlib_progress_info progress;
100 u64 swm_part_sizes[num_swms];
102 /* Calculate total size of the streams in the split WIM parts. */
103 for (i = 0; i < num_swms; i++) {
104 part_bytes = lookup_table_total_stream_size(swms[i]->lookup_table);
105 swm_part_sizes[i] = part_bytes;
106 total_bytes += part_bytes;
110 progress.join.total_bytes = total_bytes;
111 progress.join.total_parts = swms[0]->hdr.total_parts;
112 progress.join.completed_bytes = 0;
113 progress.join.completed_parts = 0;
114 progress_func(WIMLIB_PROGRESS_MSG_JOIN_STREAMS, &progress);
117 /* Write the non-metadata resources from each SWM part */
118 for (i = 0; i < num_swms; i++) {
119 swms[i]->fp = tfopen(swms[i]->filename, T("rb"));
121 ERROR_WITH_ERRNO("Failed to reopen `%"TS"'",
123 return WIMLIB_ERR_OPEN;
125 swms[i]->out_fp = joined_wim->out_fp;
126 swms[i]->hdr.part_number = 1;
127 ret = for_lookup_table_entry(swms[i]->lookup_table,
128 copy_resource, swms[i]);
129 swms[i]->out_fp = NULL;
140 progress.join.completed_bytes += swm_part_sizes[i];
141 progress.join.completed_parts++;
142 progress_func(WIMLIB_PROGRESS_MSG_JOIN_STREAMS, &progress);
146 /* Write metadata resources from the first SWM part */
147 swms[0]->out_fp = joined_wim->out_fp;
148 ret = for_image(swms[0], WIMLIB_ALL_IMAGES, write_metadata_resource);
149 swms[0]->out_fp = NULL;
156 /* Write lookup table, XML data, and optional integrity table */
157 joined_wim->hdr.image_count = swms[0]->hdr.image_count;
158 for (i = 0; i < num_swms; i++)
159 lookup_table_join(joined_wim->lookup_table, swms[i]->lookup_table);
161 free_wim_info(joined_wim->wim_info);
162 joined_wim->wim_info = swms[0]->wim_info;
163 joined_wim->image_metadata = swms[0]->image_metadata;
164 ret = finish_write(joined_wim, WIMLIB_ALL_IMAGES, write_flags, progress_func);
165 joined_wim->wim_info = NULL;
166 joined_wim->image_metadata = NULL;
171 cmp_swms_by_part_number(const void *swm1, const void *swm2)
173 u16 partno_1 = (*(const WIMStruct**)swm1)->hdr.part_number;
174 u16 partno_2 = (*(const WIMStruct**)swm2)->hdr.part_number;
175 return (int)partno_1 - (int)partno_2;
179 * Join a set of split WIMs into a stand-alone WIM.
182 wimlib_join(const tchar * const *swm_names,
184 const tchar *output_path,
187 wimlib_progress_func_t progress_func)
190 WIMStruct *joined_wim = NULL;
193 swm_open_flags |= WIMLIB_OPEN_FLAG_SPLIT_OK;
194 wim_write_flags &= WIMLIB_WRITE_MASK_PUBLIC;
196 if (num_swms < 1 || num_swms > 0xffff)
197 return WIMLIB_ERR_INVALID_PARAM;
199 WIMStruct *swms[num_swms];
202 for (i = 0; i < num_swms; i++) {
203 ret = wimlib_open_wim(swm_names[i], swm_open_flags, &swms[i],
208 /* Don't open all the parts at the same time, in case there are
214 qsort(swms, num_swms, sizeof(swms[0]), cmp_swms_by_part_number);
216 ret = verify_swm_set(swms[0], &swms[1], num_swms - 1);
220 ret = wimlib_create_new_wim(wimlib_get_compression_type(swms[0]),
225 ret = begin_write(joined_wim, output_path, wim_write_flags);
228 ret = join_wims(swms, num_swms, joined_wim, wim_write_flags,
231 for (i = 0; i < num_swms; i++)
232 wimlib_free(swms[i]);
233 wimlib_free(joined_wim);