4 * Join split WIMs (sometimes named as .swm files) together into one WIM.
6 * Copyright (C) 2012 Eric Biggers
8 * wimlib - Library for working with WIM files
10 * This library is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU Lesser General Public License as published by the Free
12 * Software Foundation; either version 2.1 of the License, or (at your option) any
15 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
17 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License along
20 * with this library; if not, write to the Free Software Foundation, Inc., 59
21 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "lookup_table.h"
28 static int join_wims(WIMStruct **swms, uint num_swms, WIMStruct *joined_wim,
33 FILE *out_fp = joined_wim->out_fp;
34 u64 total_bytes = wim_info_get_total_bytes(swms[0]->wim_info);
36 swms[0]->write_metadata = false;
37 for (i = 0; i < num_swms; i++) {
38 if (write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS) {
39 off_t cur_offset = ftello(out_fp);
40 printf("Writing resources from part %u of %u "
41 "(%"PRIu64" of %"PRIu64" bytes, %.0f%% done)\n",
43 cur_offset, total_bytes,
44 (double)cur_offset / total_bytes * 100.0);
46 swms[i]->fp = fopen(swms[i]->filename, "rb");
48 ERROR("Failed to reopen `%s': %m\n", swms[i]->filename);
49 return WIMLIB_ERR_OPEN;
51 swms[i]->out_fp = out_fp;
52 swms[i]->hdr.part_number = 1;
53 ret = for_lookup_table_entry(swms[i]->lookup_table,
54 copy_resource, swms[i]);
62 swms[0]->write_metadata = true;
63 if (write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS)
64 printf("Writing %d metadata resources\n",
65 swms[0]->hdr.image_count);
67 for (i = 0; i < swms[0]->hdr.image_count; i++) {
68 ret = copy_resource(swms[0]->image_metadata[i].lookup_table_entry,
74 off_t lookup_table_offset = ftello(out_fp);
76 if (write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS)
77 printf("Writing lookup tables, XML data, and header\n");
78 /* Now write the lookup table for the joined wim. Since the lookup
79 * table has no header, we can just concatenate the lookup tables of all
81 for (i = 0; i < num_swms; i++) {
82 ret = write_lookup_table(swms[i]->lookup_table, out_fp);
86 off_t xml_data_offset = ftello(out_fp);
88 if (lookup_table_offset == -1 || xml_data_offset == -1) {
89 ERROR("Failed to get file offset: %m\n");
90 return WIMLIB_ERR_WRITE;
92 swms[0]->hdr.lookup_table_res_entry.offset = lookup_table_offset;
93 swms[0]->hdr.lookup_table_res_entry.size =
94 xml_data_offset - lookup_table_offset;
97 /* finish_write is called on the first swm, not the joined_wim, because
98 * the first swm is the one that has the image metadata and XML data
100 swms[0]->hdr.flags &= ~WIM_HDR_FLAG_SPANNED;
101 swms[0]->hdr.total_parts = 1;
102 return finish_write(swms[0], WIM_ALL_IMAGES, write_flags, 0);
106 WIMLIBAPI int wimlib_join(const char **swm_names, int num_swms,
107 const char *output_path, int flags)
114 WIMStruct *joined_wim = NULL;
115 WIMStruct *swms[num_swms];
117 /* keep track of ctype and guid just to make sure they are the same for
123 for (i = 0; i < num_swms; i++) {
124 ret = wimlib_open_wim(swm_names[i],
125 flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
129 /* don't open all the parts at the same time, in case there are
135 ctype = wimlib_get_compression_type(w);
138 if (wimlib_get_compression_type(w) != ctype) {
139 ERROR("The split WIMs do not all have the same "
140 "compression type!\n");
141 ret = WIMLIB_ERR_SPLIT_INVALID;
144 if (memcmp(guid, w->hdr.guid, WIM_GID_LEN) != 0) {
145 ERROR("The split WIMs do not all have the "
147 ret = WIMLIB_ERR_SPLIT_INVALID;
151 if (w->hdr.total_parts != num_swms) {
152 ERROR("`%s' (part %d) says there are %d total parts, "
153 "but %d parts were specified!\n",
154 swm_names[i], w->hdr.part_number,
155 w->hdr.total_parts, num_swms);
156 ret = WIMLIB_ERR_SPLIT_INVALID;
159 if (w->hdr.part_number == 0 || w->hdr.part_number > num_swms) {
160 ERROR("`%s' says it is part %d, but expected a number\n"
161 "between 1 and %d!\n",
162 swm_names[i], w->hdr.part_number, num_swms);
163 ret = WIMLIB_ERR_SPLIT_INVALID;
166 part_idx = w->hdr.part_number - 1;
167 if (swms[part_idx] != NULL) {
168 ERROR("`%s' and `%s' both say they are part %d of %d!\n",
169 swm_names[i], swms[part_idx]->filename,
170 w->hdr.part_number, num_swms);
171 ret = WIMLIB_ERR_SPLIT_INVALID;
177 joined_wim = new_wim_struct();
179 ret = WIMLIB_ERR_NOMEM;
183 if (flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY)
184 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
185 if (flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
186 write_flags |= WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
188 ret = begin_write(joined_wim, output_path, write_flags);
191 ret = join_wims(swms, num_swms, joined_wim, write_flags);
193 for (i = 0; i < num_swms; i++) {
194 /* out_fp is the same in all the swms and joined_wim; only close
195 * it one time, when freeing joined_wim. */
197 swms[i]->out_fp = NULL;
198 wimlib_free(swms[i]);
201 wimlib_free(joined_wim);