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