4 * Join split WIMs (sometimes named as .swm files) together into one WIM.
8 * Copyright (C) 2012, 2013 Eric Biggers
10 * This file 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 3 of the License, or (at your option) any
15 * This file is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this file; if not, see http://www.gnu.org/licenses/.
29 #include "wimlib/error.h"
30 #include "wimlib/types.h"
31 #include "wimlib/util.h"
32 #include "wimlib/wim.h"
35 * verify_swm_set: - Sanity checks to make sure a set of WIMs correctly
36 * correspond to a spanned set.
42 * All parts of the set other than part 1.
44 * @num_additional_swms:
45 * Number of WIMStructs in @additional_swms. Or, the total number of parts
49 * 0 on success; WIMLIB_ERR_SPLIT_INVALID if the set is not valid.
52 verify_swm_set(WIMStruct *wim, WIMStruct **additional_swms,
53 unsigned num_additional_swms)
55 unsigned total_parts = wim->hdr.total_parts;
60 if (total_parts != num_additional_swms + 1) {
61 ERROR("`%"TS"' says there are %u parts in the spanned set, "
62 "but %"TS"%u part%"TS" provided",
63 wim->filename, total_parts,
64 (num_additional_swms + 1 < total_parts) ? T("only ") : T(""),
65 num_additional_swms + 1,
66 (num_additional_swms) ? T("s were") : T(" was"));
67 return WIMLIB_ERR_SPLIT_INVALID;
69 if (wim->hdr.part_number != 1) {
70 ERROR("WIM `%"TS"' is not the first part of the split WIM.",
72 return WIMLIB_ERR_SPLIT_INVALID;
74 for (unsigned i = 0; i < num_additional_swms; i++) {
75 if (additional_swms[i]->hdr.total_parts != total_parts) {
76 ERROR("WIM `%"TS"' says there are %u parts in the "
77 "spanned set, but %u parts were provided",
78 additional_swms[i]->filename,
79 additional_swms[i]->hdr.total_parts,
81 return WIMLIB_ERR_SPLIT_INVALID;
85 /* Keep track of the compression type, chunk size, and GUID to make sure
86 * they are the same for all the WIMs. */
87 ctype = wim->compression_type;
88 chunk_size = wim->chunk_size;
92 /* parts_to_swms is not allocated at function scope because it
93 * should only be allocated after num_additional_swms was
94 * checked to be the same as wim->hdr.total_parts. Otherwise, it
95 * could be unexpectedly high and cause a stack overflow. */
96 WIMStruct *parts_to_swms[num_additional_swms];
97 memset(parts_to_swms, 0, sizeof(parts_to_swms));
98 for (unsigned i = 0; i < num_additional_swms; i++) {
100 WIMStruct *swm = additional_swms[i];
102 if (swm->compression_type != ctype) {
103 ERROR("The split WIMs do not all have the same "
105 return WIMLIB_ERR_SPLIT_INVALID;
107 if (swm->chunk_size != chunk_size &&
108 ctype != WIMLIB_COMPRESSION_TYPE_NONE) {
109 ERROR("The split WIMs do not all have the same "
111 return WIMLIB_ERR_SPLIT_INVALID;
113 if (!guids_equal(guid, swm->hdr.guid)) {
114 ERROR("The split WIMs do not all have the same "
116 return WIMLIB_ERR_SPLIT_INVALID;
118 if (swm->hdr.part_number == 1) {
119 ERROR("WIMs `%"TS"' and `%"TS"' both are marked "
120 "as the first WIM in the spanned set",
121 wim->filename, swm->filename);
122 return WIMLIB_ERR_SPLIT_INVALID;
124 if (swm->hdr.part_number == 0 ||
125 swm->hdr.part_number > total_parts)
127 ERROR("WIM `%"TS"' says it is part %u in the "
128 "spanned set, but the part number must "
129 "be in the range [1, %u]",
130 swm->filename, swm->hdr.part_number, total_parts);
131 return WIMLIB_ERR_SPLIT_INVALID;
133 if (parts_to_swms[swm->hdr.part_number - 2])
135 ERROR("`%"TS"' and `%"TS"' are both marked as "
136 "part %u of %u in the spanned set",
137 parts_to_swms[swm->hdr.part_number - 2]->filename,
139 swm->hdr.part_number,
141 return WIMLIB_ERR_SPLIT_INVALID;
143 parts_to_swms[swm->hdr.part_number - 2] = swm;
151 wimlib_join_with_progress(const tchar * const *swm_names,
153 const tchar *output_path,
156 wimlib_progress_func_t progfunc,
163 WIMStruct **additional_swms;
164 unsigned num_additional_swms;
166 if (num_swms < 1 || num_swms > 0xffff)
167 return WIMLIB_ERR_INVALID_PARAM;
168 num_additional_swms = num_swms - 1;
170 additional_swms = CALLOC((num_additional_swms + 1),
171 sizeof(additional_swms[0]));
172 if (!additional_swms)
173 return WIMLIB_ERR_NOMEM;
176 for (i = 0, j = 0; i < num_swms; i++) {
179 ret = wimlib_open_wim_with_progress(swm_names[i],
186 if (swm->hdr.part_number == 1 && swm0 == NULL)
189 additional_swms[j++] = swm;
193 ERROR("Part 1 of the split WIM was not specified!");
194 ret = WIMLIB_ERR_SPLIT_INVALID;
198 ret = verify_swm_set(swm0, additional_swms, num_additional_swms);
202 ret = wimlib_reference_resources(swm0, additional_swms,
203 num_additional_swms, 0);
207 /* It is reasonably safe to provide, WIMLIB_WRITE_FLAG_STREAMS_OK, as we
208 * have verified that the specified split WIM parts form a spanned set.
210 ret = wimlib_write(swm0, output_path, WIMLIB_ALL_IMAGES,
212 WIMLIB_WRITE_FLAG_STREAMS_OK |
213 WIMLIB_WRITE_FLAG_RETAIN_GUID,
216 for (i = 0; i < num_additional_swms + 1; i++)
217 wimlib_free(additional_swms[i]);
218 FREE(additional_swms);
223 /* API function documented in wimlib.h */
225 wimlib_join(const tchar * const *swm_names,
227 const tchar *output_path,
231 return wimlib_join_with_progress(swm_names, num_swms, output_path,
232 swm_open_flags, wim_write_flags,