Update progress functions
[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_GUID_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 WIMLIBAPI int
153 wimlib_join_with_progress(const tchar * const *swm_names,
154                           unsigned num_swms,
155                           const tchar *output_path,
156                           int swm_open_flags,
157                           int wim_write_flags,
158                           wimlib_progress_func_t progfunc,
159                           void *progctx)
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 + 1),
173                                  sizeof(additional_swms[0]));
174         if (!additional_swms)
175                 return WIMLIB_ERR_NOMEM;
176
177         swm0 = NULL;
178         for (i = 0, j = 0; i < num_swms; i++) {
179                 WIMStruct *swm;
180
181                 ret = wimlib_open_wim_with_progress(swm_names[i],
182                                                     swm_open_flags,
183                                                     &swm,
184                                                     progfunc,
185                                                     progctx);
186                 if (ret)
187                         goto out_free_swms;
188                 if (swm->hdr.part_number == 1 && swm0 == NULL)
189                         swm0 = swm;
190                 else
191                         additional_swms[j++] = swm;
192         }
193
194         if (!swm0) {
195                 ERROR("Part 1 of the split WIM was not specified!");
196                 ret = WIMLIB_ERR_SPLIT_INVALID;
197                 goto out_free_swms;
198         }
199
200         ret = verify_swm_set(swm0, additional_swms, num_additional_swms);
201         if (ret)
202                 goto out_free_swms;
203
204         ret = wimlib_reference_resources(swm0, additional_swms,
205                                          num_additional_swms, 0);
206         if (ret)
207                 goto out_free_swms;
208
209         /* It is reasonably safe to provide, WIMLIB_WRITE_FLAG_STREAMS_OK, as we
210          * have verified that the specified split WIM parts form a spanned set.
211          */
212         ret = wimlib_write(swm0, output_path, WIMLIB_ALL_IMAGES,
213                            wim_write_flags |
214                                 WIMLIB_WRITE_FLAG_STREAMS_OK |
215                                 WIMLIB_WRITE_FLAG_RETAIN_GUID,
216                            1);
217 out_free_swms:
218         for (i = 0; i < num_additional_swms + 1; i++)
219                 wimlib_free(additional_swms[i]);
220         FREE(additional_swms);
221         wimlib_free(swm0);
222         return ret;
223 }
224
225 /* API function documented in wimlib.h  */
226 WIMLIBAPI int
227 wimlib_join(const tchar * const *swm_names,
228             unsigned num_swms,
229             const tchar *output_path,
230             int swm_open_flags,
231             int wim_write_flags)
232 {
233         return wimlib_join_with_progress(swm_names, num_swms, output_path,
234                                          swm_open_flags, wim_write_flags,
235                                          NULL, NULL);
236 }