4 * Join split WIMs (sometimes named as .swm files) together into one WIM.
6 * Copyright (C) 2010 Carl Thijssen
7 * Copyright (C) 2012 Eric Biggers
9 * wimlib - Library for working with WIM files
11 * This library is free software; you can redistribute it and/or modify it under
12 * the terms of the GNU Lesser General Public License as published by the Free
13 * Software Foundation; either version 2.1 of the License, or (at your option) any
16 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
17 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
18 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License along
21 * with this library; if not, write to the Free Software Foundation, Inc., 59
22 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "lookup_table.h"
29 static int join_resource(struct lookup_table_entry *lte, void *split_wim)
31 FILE *split_wim_fp = ((WIMStruct*)split_wim)->fp;
32 FILE *joined_wim_fp = ((WIMStruct*)split_wim)->out_fp;
35 u64 size = lte->resource_entry.size;
36 u64 offset = lte->resource_entry.offset;
37 off_t new_offset = ftello(joined_wim_fp);
40 return WIMLIB_ERR_WRITE;
42 ret = copy_between_files(split_wim_fp, offset, joined_wim_fp, size);
46 memcpy(<e->output_resource_entry, <e->resource_entry,
47 sizeof(struct resource_entry));
49 lte->output_resource_entry.offset = new_offset;
50 lte->out_refcnt = lte->refcnt;
55 static int join_wims(WIMStruct **swms, uint num_swms, WIMStruct *joined_wim,
60 FILE *out_fp = joined_wim->out_fp;
61 u64 total_bytes = wim_info_get_total_bytes(swms[0]->wim_info);
63 /* The following loop writes both file resources and metadata resources
64 * because it loops over the lookup table entries rather than the dentry
65 * tree for the images */
66 for (i = 0; i < num_swms; i++) {
67 if (write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS) {
68 off_t cur_offset = ftello(out_fp);
69 printf("Writing resources from part %u of %u "
70 "(%"PRIu64" of %"PRIu64" bytes, %.2f%% done)\n",
72 cur_offset, total_bytes,
73 (double)cur_offset / total_bytes * 100.0);
75 swms[i]->out_fp = out_fp;
76 ret = for_lookup_table_entry(swms[i]->lookup_table,
77 join_resource, swms[i]);
82 off_t lookup_table_offset = ftello(out_fp);
84 if (write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS)
85 printf("Writing lookup tables, XML data, and header\n");
86 /* Now write the lookup table for the joined wim. Since the lookup
87 * table has no header, we can just concatenate the lookup tables of all
89 for (i = 0; i < num_swms; i++) {
90 ret = write_lookup_table(swms[i]->lookup_table, out_fp);
94 off_t xml_data_offset = ftello(out_fp);
96 if (lookup_table_offset == -1 || xml_data_offset == -1) {
97 ERROR("Failed to get file offset: %m\n");
98 return WIMLIB_ERR_WRITE;
100 swms[0]->hdr.lookup_table_res_entry.offset = lookup_table_offset;
101 swms[0]->hdr.lookup_table_res_entry.size =
102 xml_data_offset - lookup_table_offset;
104 swms[0]->hdr.flags &= ~WIM_HDR_FLAG_SPANNED;
106 /* finish_write is called on the first swm, not the joined_wim, because
107 * the first swm is the one that has the image metadata and XML data
109 return finish_write(swms[0], WIM_ALL_IMAGES, write_flags, 0);
113 WIMLIBAPI int wimlib_join(const char **swm_names, int num_swms,
114 const char *output_path, int flags)
121 WIMStruct *joined_wim = NULL;
122 WIMStruct *swms[num_swms];
124 /* keep track of ctype and guid just to make sure they are the same for
130 for (i = 0; i < num_swms; i++) {
131 ret = wimlib_open_wim(swm_names[i],
132 flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
137 ctype = wimlib_get_compression_type(w);
140 if (wimlib_get_compression_type(w) != ctype) {
141 ERROR("The split WIMs do not all have the same "
142 "compression type!\n");
143 ret = WIMLIB_ERR_SPLIT_INVALID;
146 if (memcmp(guid, w->hdr.guid, WIM_GID_LEN) != 0) {
147 ERROR("The split WIMs do not all have the "
149 ret = WIMLIB_ERR_SPLIT_INVALID;
153 if (w->hdr.total_parts != num_swms) {
154 ERROR("`%s' (part %d) says there are %d total parts,\n"
155 "but %d parts were specified!\n",
156 swm_names[i], w->hdr.part_number,
157 w->hdr.total_parts, num_swms);
158 ret = WIMLIB_ERR_SPLIT_INVALID;
161 if (w->hdr.part_number == 0 || w->hdr.part_number > num_swms) {
162 ERROR("`%s' says it is part %d, but expected a number\n"
163 "between 1 and %d!\n",
164 swm_names[i], w->hdr.part_number, num_swms);
165 ret = WIMLIB_ERR_SPLIT_INVALID;
168 part_idx = w->hdr.part_number - 1;
169 if (swms[part_idx] != NULL) {
170 ERROR("`%s' and `%s' both say they are part %d of %d!\n",
171 swm_names[i], swms[part_idx]->filename,
172 w->hdr.part_number, num_swms);
173 ret = WIMLIB_ERR_SPLIT_INVALID;
179 joined_wim = new_wim_struct();
181 ret = WIMLIB_ERR_NOMEM;
185 if (flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY)
186 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
187 if (flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
188 write_flags |= WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
190 ret = begin_write(joined_wim, output_path, write_flags);
193 ret = join_wims(swms, num_swms, joined_wim, write_flags);
195 for (i = 0; i < num_swms; i++) {
196 /* out_fp is the same in all the swms and joined_wim; only close
197 * it one time, when freeing joined_wim. */
198 swms[i]->out_fp = NULL;
199 wimlib_free(swms[i]);
201 wimlib_free(joined_wim);