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/.
31 #include "wimlib/error.h"
32 #include "wimlib/types.h"
33 #include "wimlib/util.h"
34 #include "wimlib/wim.h"
37 * verify_swm_set: - Sanity checks to make sure a set of WIMs correctly
38 * correspond to a spanned set.
44 * All parts of the set other than part 1.
46 * @num_additional_swms:
47 * Number of WIMStructs in @additional_swms. Or, the total number of parts
51 * 0 on success; WIMLIB_ERR_SPLIT_INVALID if the set is not valid.
54 verify_swm_set(WIMStruct *wim, WIMStruct **additional_swms,
55 unsigned num_additional_swms)
57 unsigned total_parts = wim->hdr.total_parts;
62 if (total_parts != num_additional_swms + 1) {
63 ERROR("`%"TS"' says there are %u parts in the spanned set, "
64 "but %"TS"%u part%"TS" provided",
65 wim->filename, total_parts,
66 (num_additional_swms + 1 < total_parts) ? T("only ") : T(""),
67 num_additional_swms + 1,
68 (num_additional_swms) ? T("s were") : T(" was"));
69 return WIMLIB_ERR_SPLIT_INVALID;
71 if (wim->hdr.part_number != 1) {
72 ERROR("WIM `%"TS"' is not the first part of the split WIM.",
74 return WIMLIB_ERR_SPLIT_INVALID;
76 for (unsigned i = 0; i < num_additional_swms; i++) {
77 if (additional_swms[i]->hdr.total_parts != total_parts) {
78 ERROR("WIM `%"TS"' says there are %u parts in the "
79 "spanned set, but %u parts were provided",
80 additional_swms[i]->filename,
81 additional_swms[i]->hdr.total_parts,
83 return WIMLIB_ERR_SPLIT_INVALID;
87 /* Keep track of the compression type, chunk size, and GUID to make sure
88 * they are the same for all the WIMs. */
89 ctype = wim->compression_type;
90 chunk_size = wim->chunk_size;
94 /* parts_to_swms is not allocated at function scope because it
95 * should only be allocated after num_additional_swms was
96 * checked to be the same as wim->hdr.total_parts. Otherwise, it
97 * could be unexpectedly high and cause a stack overflow. */
98 WIMStruct *parts_to_swms[num_additional_swms];
99 ZERO_ARRAY(parts_to_swms);
100 for (unsigned i = 0; i < num_additional_swms; i++) {
102 WIMStruct *swm = additional_swms[i];
104 if (swm->compression_type != ctype) {
105 ERROR("The split WIMs do not all have the same "
107 return WIMLIB_ERR_SPLIT_INVALID;
109 if (swm->chunk_size != chunk_size &&
110 ctype != WIMLIB_COMPRESSION_TYPE_NONE) {
111 ERROR("The split WIMs do not all have the same "
113 return WIMLIB_ERR_SPLIT_INVALID;
115 if (memcmp(guid, swm->hdr.guid, WIM_GUID_LEN) != 0) {
116 ERROR("The split WIMs do not all have the same "
118 return WIMLIB_ERR_SPLIT_INVALID;
120 if (swm->hdr.part_number == 1) {
121 ERROR("WIMs `%"TS"' and `%"TS"' both are marked "
122 "as the first WIM in the spanned set",
123 wim->filename, swm->filename);
124 return WIMLIB_ERR_SPLIT_INVALID;
126 if (swm->hdr.part_number == 0 ||
127 swm->hdr.part_number > total_parts)
129 ERROR("WIM `%"TS"' says it is part %u in the "
130 "spanned set, but the part number must "
131 "be in the range [1, %u]",
132 swm->filename, swm->hdr.part_number, total_parts);
133 return WIMLIB_ERR_SPLIT_INVALID;
135 if (parts_to_swms[swm->hdr.part_number - 2])
137 ERROR("`%"TS"' and `%"TS"' are both marked as "
138 "part %u of %u in the spanned set",
139 parts_to_swms[swm->hdr.part_number - 2]->filename,
141 swm->hdr.part_number,
143 return WIMLIB_ERR_SPLIT_INVALID;
145 parts_to_swms[swm->hdr.part_number - 2] = swm;
152 /* API function documented in wimlib.h */
154 wimlib_join(const tchar * const *swm_names,
156 const tchar *output_path,
159 wimlib_progress_func_t progress_func)
165 WIMStruct **additional_swms;
166 unsigned num_additional_swms;
168 if (num_swms < 1 || num_swms > 0xffff)
169 return WIMLIB_ERR_INVALID_PARAM;
170 num_additional_swms = num_swms - 1;
172 additional_swms = CALLOC((num_additional_swms + 1),
173 sizeof(additional_swms[0]));
174 if (!additional_swms)
175 return WIMLIB_ERR_NOMEM;
178 for (i = 0, j = 0; i < num_swms; i++) {
181 ret = wimlib_open_wim(swm_names[i], swm_open_flags, &swm,
185 if (swm->hdr.part_number == 1 && swm0 == NULL)
188 additional_swms[j++] = swm;
192 ERROR("Part 1 of the split WIM was not specified!");
193 ret = WIMLIB_ERR_SPLIT_INVALID;
197 ret = verify_swm_set(swm0, additional_swms, num_additional_swms);
201 ret = wimlib_reference_resources(swm0, additional_swms,
202 num_additional_swms, 0);
206 /* It is reasonably safe to provide, WIMLIB_WRITE_FLAG_STREAMS_OK, as we
207 * have verified that the specified split WIM parts form a spanned set.
209 ret = wimlib_write(swm0, output_path, WIMLIB_ALL_IMAGES,
211 WIMLIB_WRITE_FLAG_STREAMS_OK |
212 WIMLIB_WRITE_FLAG_RETAIN_GUID,
215 for (i = 0; i < num_additional_swms + 1; i++)
216 wimlib_free(additional_swms[i]);
217 FREE(additional_swms);