cdfd3889d27626f70984774c643daaaf25a86c98
[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         const u8 *guid;
60
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;
69         }
70         if (wim->hdr.part_number != 1) {
71                 ERROR("WIM `%"TS"' is not the first part of the split WIM.",
72                       wim->filename);
73                 return WIMLIB_ERR_SPLIT_INVALID;
74         }
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,
81                               total_parts);
82                         return WIMLIB_ERR_SPLIT_INVALID;
83                 }
84         }
85
86         /* keep track of ctype and guid just to make sure they are the same for
87          * all the WIMs. */
88         ctype = wim->compression_type;
89         guid = wim->hdr.guid;
90
91         {
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++) {
99
100                         WIMStruct *swm = additional_swms[i];
101
102                         if (swm->compression_type != ctype) {
103                                 ERROR("The split WIMs do not all have the same "
104                                       "compression type");
105                                 return WIMLIB_ERR_SPLIT_INVALID;
106                         }
107                         if (memcmp(guid, swm->hdr.guid, WIM_GID_LEN) != 0) {
108                                 ERROR("The split WIMs do not all have the same "
109                                       "GUID");
110                                 return WIMLIB_ERR_SPLIT_INVALID;
111                         }
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;
117                         }
118                         if (swm->hdr.part_number == 0 ||
119                             swm->hdr.part_number > total_parts)
120                         {
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;
126                         }
127                         if (parts_to_swms[swm->hdr.part_number - 2])
128                         {
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,
132                                       swm->filename,
133                                       swm->hdr.part_number,
134                                       total_parts);
135                                 return WIMLIB_ERR_SPLIT_INVALID;
136                         } else {
137                                 parts_to_swms[swm->hdr.part_number - 2] = swm;
138                         }
139                 }
140         }
141         return 0;
142 }
143
144 /* API function documented in wimlib.h  */
145 WIMLIBAPI int
146 wimlib_join(const tchar * const *swm_names,
147             unsigned num_swms,
148             const tchar *output_path,
149             int swm_open_flags,
150             int wim_write_flags,
151             wimlib_progress_func_t progress_func)
152 {
153         int ret;
154         unsigned i;
155         unsigned j;
156         WIMStruct *swm0;
157         WIMStruct **additional_swms;
158         unsigned num_additional_swms;
159
160         if (num_swms < 1 || num_swms > 0xffff)
161                 return WIMLIB_ERR_INVALID_PARAM;
162         num_additional_swms = num_swms - 1;
163
164         additional_swms = CALLOC(num_additional_swms, sizeof(additional_swms[0]));
165         if (!additional_swms)
166                 return WIMLIB_ERR_NOMEM;
167
168         swm0 = NULL;
169         for (i = 0, j = 0; i < num_swms; i++) {
170                 WIMStruct *swm;
171
172                 ret = wimlib_open_wim(swm_names[i], swm_open_flags, &swm,
173                                       progress_func);
174                 if (ret)
175                         goto out_free_swms;
176                 if (swm->hdr.part_number == 1 && swm0 == NULL)
177                         swm0 = swm;
178                 else
179                         additional_swms[j++] = swm;
180         }
181
182         if (!swm0) {
183                 ret = WIMLIB_ERR_SPLIT_INVALID;
184                 goto out_free_swms;
185         }
186
187         ret = verify_swm_set(swm0, additional_swms, num_additional_swms);
188         if (ret)
189                 goto out_free_swms;
190
191         ret = wimlib_reference_resources(swm0, additional_swms,
192                                          num_additional_swms, 0);
193         if (ret)
194                 goto out_free_swms;
195
196         /* It is reasonably safe to provide, WIMLIB_WRITE_FLAG_STREAMS_OK, as we
197          * have verified that the specified split WIM parts form a spanned set.
198          */
199         ret = wimlib_write(swm0, output_path, WIMLIB_ALL_IMAGES,
200                            wim_write_flags | WIMLIB_WRITE_FLAG_STREAMS_OK,
201                            1, progress_func);
202 out_free_swms:
203         for (i = 0; i < num_additional_swms; i++)
204                 wimlib_free(additional_swms[i]);
205         FREE(additional_swms);
206         wimlib_free(swm0);
207         return ret;
208 }