08b360bad3dd704fbfb40b1e0cd7451086dc5cd8
[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 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 #include "wimlib_internal.h"
27 #include "lookup_table.h"
28 #include "xml.h"
29 #include <stdlib.h>
30
31 static int move_lte_to_table(struct wim_lookup_table_entry *lte,
32                              void *other_tab)
33 {
34         hlist_del(&lte->hash_list);
35         lookup_table_insert((struct wim_lookup_table*)other_tab, lte);
36         return 0;
37 }
38
39 static int lookup_table_join(struct wim_lookup_table *table,
40                              struct wim_lookup_table *new)
41 {
42         return for_lookup_table_entry(new, move_lte_to_table, table);
43 }
44
45 /*
46  * new_joined_lookup_table: - Join lookup tables from the parts of a split WIM.
47  *
48  * @w specifies the first part, while @additional_swms and @num_additional_swms
49  * specify an array of pointers to the WIMStruct's for additional split WIM parts.
50  *
51  * The lookup table entries are *moved* to the new table.
52  *
53  * On success, 0 is returned on a pointer to the joined lookup table is returned
54  * in @table_ret.
55  *
56  * The reason we join the lookup tables is so we only have to search one lookup
57  * table to find the location of a resource in the entire WIM.
58  */
59 int new_joined_lookup_table(WIMStruct *w,
60                             WIMStruct **additional_swms,
61                             unsigned num_additional_swms,
62                             struct wim_lookup_table **table_ret)
63 {
64         struct wim_lookup_table *table;
65         int ret;
66         unsigned i;
67
68         table = new_lookup_table(9001);
69         if (!table)
70                 return WIMLIB_ERR_NOMEM;
71
72         if (w)
73                 lookup_table_join(table, w->lookup_table);
74
75         for (i = 0; i < num_additional_swms; i++) {
76                 ret = lookup_table_join(table, additional_swms[i]->lookup_table);
77                 if (ret != 0)
78                         goto out_free_table;
79         }
80         *table_ret = table;
81         return 0;
82 out_free_table:
83         free_lookup_table(table);
84         return ret;
85 }
86
87
88 static int join_wims(WIMStruct **swms, unsigned num_swms,
89                      WIMStruct *joined_wim, int write_flags,
90                      wimlib_progress_func_t progress_func)
91 {
92         int ret;
93         unsigned i;
94         union wimlib_progress_info progress;
95         u64 total_bytes = 0;
96         u64 part_bytes;
97         u64 swm_part_sizes[num_swms];
98
99         /* Calculate total size of the streams in the split WIM parts. */
100         for (i = 0; i < num_swms; i++) {
101                 part_bytes = lookup_table_total_stream_size(swms[i]->lookup_table);
102                 swm_part_sizes[i] = part_bytes;
103                 total_bytes += part_bytes;
104         }
105
106         if (progress_func) {
107                 progress.join.total_bytes        = total_bytes;
108                 progress.join.total_parts        = swms[0]->hdr.total_parts;
109                 progress.join.completed_bytes    = 0;
110                 progress.join.completed_parts    = 0;
111                 progress_func(WIMLIB_PROGRESS_MSG_JOIN_STREAMS, &progress);
112         }
113
114         /* Write the resources (streams and metadata resources) from each SWM
115          * part */
116         swms[0]->write_metadata = true;
117         for (i = 0; i < num_swms; i++) {
118                 swms[i]->fp = fopen(swms[i]->filename, "rb");
119                 if (!swms[i]->fp) {
120                         ERROR_WITH_ERRNO("Failed to reopen `%s'",
121                                          swms[i]->filename);
122                         return WIMLIB_ERR_OPEN;
123                 }
124                 swms[i]->out_fp = joined_wim->out_fp;
125                 swms[i]->hdr.part_number = 1;
126                 ret = for_lookup_table_entry(swms[i]->lookup_table,
127                                              copy_resource, swms[i]);
128                 swms[i]->out_fp = NULL;
129                 fclose(swms[i]->fp);
130                 swms[i]->fp = NULL;
131
132                 if (ret != 0)
133                         return ret;
134
135                 if (progress_func) {
136                         progress.join.completed_bytes += swm_part_sizes[i];
137                         progress.join.completed_parts++;
138                         progress_func(WIMLIB_PROGRESS_MSG_JOIN_STREAMS, &progress);
139                 }
140         }
141
142         joined_wim->hdr.image_count = swms[0]->hdr.image_count;
143         for (i = 0; i < num_swms; i++)
144                 lookup_table_join(joined_wim->lookup_table, swms[i]->lookup_table);
145
146         free_wim_info(joined_wim->wim_info);
147         joined_wim->wim_info = swms[0]->wim_info;
148         ret = finish_write(joined_wim, WIMLIB_ALL_IMAGES, write_flags, progress_func);
149         joined_wim->wim_info = NULL;
150         return ret;
151 }
152
153 static int cmp_swms_by_part_number(const void *swm1, const void *swm2)
154 {
155         u16 partno_1 = (*(const WIMStruct**)swm1)->hdr.part_number;
156         u16 partno_2 = (*(const WIMStruct**)swm2)->hdr.part_number;
157         return (int)partno_1 - (int)partno_2;
158 }
159
160 /*
161  * Join a set of split WIMs into a stand-alone WIM.
162  */
163 WIMLIBAPI int wimlib_join(const char **swm_names, unsigned num_swms,
164                           const char *output_path, int swm_open_flags,
165                           int wim_write_flags,
166                           wimlib_progress_func_t progress_func)
167 {
168         int ret;
169         WIMStruct *joined_wim = NULL;
170         unsigned i;
171
172         swm_open_flags |= WIMLIB_OPEN_FLAG_SPLIT_OK;
173         wim_write_flags &= WIMLIB_WRITE_MASK_PUBLIC;
174
175         if (num_swms < 1 || num_swms > 0xffff)
176                 return WIMLIB_ERR_INVALID_PARAM;
177
178         WIMStruct *swms[num_swms];
179         ZERO_ARRAY(swms);
180
181         for (i = 0; i < num_swms; i++) {
182                 ret = wimlib_open_wim(swm_names[i], swm_open_flags, &swms[i],
183                                       progress_func);
184                 if (ret != 0)
185                         goto out;
186
187                 /* Don't open all the parts at the same time, in case there are
188                  * a lot of them */
189                 fclose(swms[i]->fp);
190                 swms[i]->fp = NULL;
191         }
192
193         qsort(swms, num_swms, sizeof(swms[0]), cmp_swms_by_part_number);
194
195         ret = verify_swm_set(swms[0], &swms[1], num_swms - 1);
196         if (ret != 0)
197                 goto out;
198
199         ret = wimlib_create_new_wim(wimlib_get_compression_type(swms[0]),
200                                     &joined_wim);
201         if (ret != 0)
202                 goto out;
203
204         ret = begin_write(joined_wim, output_path, wim_write_flags);
205         if (ret != 0)
206                 goto out;
207         ret = join_wims(swms, num_swms, joined_wim, wim_write_flags,
208                         progress_func);
209 out:
210         for (i = 0; i < num_swms; i++)
211                 wimlib_free(swms[i]);
212         wimlib_free(joined_wim);
213         return ret;
214 }