]> wimlib.net Git - wimlib/blob - src/join.c
1904935311f96efae5f98a6ce85eb1bb034d1b2e
[wimlib] / src / join.c
1 /*
2  * join.c
3  *
4  * Join split WIMs (sometimes named as .swm files) together into one WIM.
5  */
6
7 /*
8  * Copyright (C) 2012, 2013 Eric Biggers
9  *
10  * This file is part of wimlib, a library for working with WIM files.
11  *
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)
15  * any later version.
16  *
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
20  * details.
21  *
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/.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include "wimlib.h"
31 #include "wimlib/error.h"
32 #include "wimlib/types.h"
33 #include "wimlib/util.h"
34 #include "wimlib/wim.h"
35
36 /*
37  * verify_swm_set: - Sanity checks to make sure a set of WIMs correctly
38  *                   correspond to a spanned set.
39  *
40  * @wim:
41  *      Part 1 of the set.
42  *
43  * @additional_swms:
44  *      All parts of the set other than part 1.
45  *
46  * @num_additional_swms:
47  *      Number of WIMStructs in @additional_swms.  Or, the total number of parts
48  *      in the set minus 1.
49  *
50  * @return:
51  *      0 on success; WIMLIB_ERR_SPLIT_INVALID if the set is not valid.
52  */
53 static int
54 verify_swm_set(WIMStruct *wim, WIMStruct **additional_swms,
55                unsigned num_additional_swms)
56 {
57         unsigned total_parts = wim->hdr.total_parts;
58         int ctype;
59         u32 chunk_size;
60         const u8 *guid;
61
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;
70         }
71         if (wim->hdr.part_number != 1) {
72                 ERROR("WIM `%"TS"' is not the first part of the split WIM.",
73                       wim->filename);
74                 return WIMLIB_ERR_SPLIT_INVALID;
75         }
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,
82                               total_parts);
83                         return WIMLIB_ERR_SPLIT_INVALID;
84                 }
85         }
86
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;
91         guid = wim->hdr.guid;
92
93         {
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++) {
101
102                         WIMStruct *swm = additional_swms[i];
103
104                         if (swm->compression_type != ctype) {
105                                 ERROR("The split WIMs do not all have the same "
106                                       "compression type");
107                                 return WIMLIB_ERR_SPLIT_INVALID;
108                         }
109                         if (swm->chunk_size != chunk_size &&
110                             ctype != WIMLIB_COMPRESSION_TYPE_NONE) {
111                                 ERROR("The split WIMs do not all have the same "
112                                       "chunk size");
113                                 return WIMLIB_ERR_SPLIT_INVALID;
114                         }
115                         if (memcmp(guid, swm->hdr.guid, WIM_GID_LEN) != 0) {
116                                 ERROR("The split WIMs do not all have the same "
117                                       "GUID");
118                                 return WIMLIB_ERR_SPLIT_INVALID;
119                         }
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;
125                         }
126                         if (swm->hdr.part_number == 0 ||
127                             swm->hdr.part_number > total_parts)
128                         {
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;
134                         }
135                         if (parts_to_swms[swm->hdr.part_number - 2])
136                         {
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,
140                                       swm->filename,
141                                       swm->hdr.part_number,
142                                       total_parts);
143                                 return WIMLIB_ERR_SPLIT_INVALID;
144                         } else {
145                                 parts_to_swms[swm->hdr.part_number - 2] = swm;
146                         }
147                 }
148         }
149         return 0;
150 }
151
152 /* API function documented in wimlib.h  */
153 WIMLIBAPI int
154 wimlib_join(const tchar * const *swm_names,
155             unsigned num_swms,
156             const tchar *output_path,
157             int swm_open_flags,
158             int wim_write_flags,
159             wimlib_progress_func_t progress_func)
160 {
161         int ret;
162         unsigned i;
163         unsigned j;
164         WIMStruct *swm0;
165         WIMStruct **additional_swms;
166         unsigned num_additional_swms;
167
168         if (num_swms < 1 || num_swms > 0xffff)
169                 return WIMLIB_ERR_INVALID_PARAM;
170         num_additional_swms = num_swms - 1;
171
172         additional_swms = CALLOC(num_additional_swms, sizeof(additional_swms[0]));
173         if (!additional_swms)
174                 return WIMLIB_ERR_NOMEM;
175
176         swm0 = NULL;
177         for (i = 0, j = 0; i < num_swms; i++) {
178                 WIMStruct *swm;
179
180                 ret = wimlib_open_wim(swm_names[i], swm_open_flags, &swm,
181                                       progress_func);
182                 if (ret)
183                         goto out_free_swms;
184                 if (swm->hdr.part_number == 1 && swm0 == NULL)
185                         swm0 = swm;
186                 else
187                         additional_swms[j++] = swm;
188         }
189
190         if (!swm0) {
191                 ret = WIMLIB_ERR_SPLIT_INVALID;
192                 goto out_free_swms;
193         }
194
195         ret = verify_swm_set(swm0, additional_swms, num_additional_swms);
196         if (ret)
197                 goto out_free_swms;
198
199         ret = wimlib_reference_resources(swm0, additional_swms,
200                                          num_additional_swms, 0);
201         if (ret)
202                 goto out_free_swms;
203
204         /* It is reasonably safe to provide, WIMLIB_WRITE_FLAG_STREAMS_OK, as we
205          * have verified that the specified split WIM parts form a spanned set.
206          */
207         ret = wimlib_write(swm0, output_path, WIMLIB_ALL_IMAGES,
208                            wim_write_flags | WIMLIB_WRITE_FLAG_STREAMS_OK,
209                            1, progress_func);
210 out_free_swms:
211         for (i = 0; i < num_additional_swms; i++)
212                 wimlib_free(additional_swms[i]);
213         FREE(additional_swms);
214         wimlib_free(swm0);
215         return ret;
216 }