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;
61 if (total_parts != num_additional_swms + 1) {
62 ERROR("`%"TS"' says there are %u parts in the spanned set, "
63 "but %"TS"%u part%"TS" provided",
64 wim->filename, total_parts,
65 (num_additional_swms + 1 < total_parts) ? T("only ") : T(""),
66 num_additional_swms + 1,
67 (num_additional_swms) ? T("s were") : T(" was"));
68 return WIMLIB_ERR_SPLIT_INVALID;
70 if (wim->hdr.part_number != 1) {
71 ERROR("WIM `%"TS"' is not the first part of the split WIM.",
73 return WIMLIB_ERR_SPLIT_INVALID;
75 for (unsigned i = 0; i < num_additional_swms; i++) {
76 if (additional_swms[i]->hdr.total_parts != total_parts) {
77 ERROR("WIM `%"TS"' says there are %u parts in the "
78 "spanned set, but %u parts were provided",
79 additional_swms[i]->filename,
80 additional_swms[i]->hdr.total_parts,
82 return WIMLIB_ERR_SPLIT_INVALID;
86 /* keep track of ctype and guid just to make sure they are the same for
88 ctype = wim->compression_type;
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 ZERO_ARRAY(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 (memcmp(guid, swm->hdr.guid, WIM_GID_LEN) != 0) {
108 ERROR("The split WIMs do not all have the same "
110 return WIMLIB_ERR_SPLIT_INVALID;
112 if (swm->hdr.part_number == 1) {
113 ERROR("WIMs `%"TS"' and `%"TS"' both are marked "
114 "as the first WIM in the spanned set",
115 wim->filename, swm->filename);
116 return WIMLIB_ERR_SPLIT_INVALID;
118 if (swm->hdr.part_number == 0 ||
119 swm->hdr.part_number > total_parts)
121 ERROR("WIM `%"TS"' says it is part %u in the "
122 "spanned set, but the part number must "
123 "be in the range [1, %u]",
124 swm->filename, swm->hdr.part_number, total_parts);
125 return WIMLIB_ERR_SPLIT_INVALID;
127 if (parts_to_swms[swm->hdr.part_number - 2])
129 ERROR("`%"TS"' and `%"TS"' are both marked as "
130 "part %u of %u in the spanned set",
131 parts_to_swms[swm->hdr.part_number - 2]->filename,
133 swm->hdr.part_number,
135 return WIMLIB_ERR_SPLIT_INVALID;
137 parts_to_swms[swm->hdr.part_number - 2] = swm;
144 /* API function documented in wimlib.h */
146 wimlib_join(const tchar * const *swm_names,
148 const tchar *output_path,
151 wimlib_progress_func_t progress_func)
157 WIMStruct **additional_swms;
158 unsigned num_additional_swms;
160 swm_open_flags |= WIMLIB_OPEN_FLAG_SPLIT_OK;
162 if (num_swms < 1 || num_swms > 0xffff)
163 return WIMLIB_ERR_INVALID_PARAM;
164 num_additional_swms = num_swms - 1;
166 additional_swms = CALLOC(num_additional_swms, sizeof(additional_swms[0]));
167 if (!additional_swms)
168 return WIMLIB_ERR_NOMEM;
171 for (i = 0, j = 0; i < num_swms; i++) {
174 ret = wimlib_open_wim(swm_names[i], swm_open_flags, &swm,
178 if (swm->hdr.part_number == 1 && swm0 == NULL)
181 additional_swms[j++] = swm;
185 ret = WIMLIB_ERR_SPLIT_INVALID;
189 ret = verify_swm_set(swm0, additional_swms, num_additional_swms);
193 ret = wimlib_reference_resources(swm0, additional_swms,
194 num_additional_swms, 0);
198 /* It is reasonably safe to provide, WIMLIB_WRITE_FLAG_STREAMS_OK, as we
199 * have verified that the specified split WIM parts form a spanned set.
201 ret = wimlib_write(swm0, output_path, WIMLIB_ALL_IMAGES,
202 wim_write_flags | WIMLIB_WRITE_FLAG_STREAMS_OK,
205 for (i = 0; i < num_additional_swms; i++)
206 wimlib_free(additional_swms[i]);
207 FREE(additional_swms);