]> wimlib.net Git - wimlib/blob - src/join.c
join_wims(): Preserve some header flags
[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/lookup_table.h"
32 #include "wimlib/metadata.h"
33 #include "wimlib/resource.h"
34 #include "wimlib/swm.h"
35 #include "wimlib/write.h"
36 #include "wimlib/xml.h"
37
38 #include <stdlib.h> /* for qsort() */
39
40 static int
41 join_wims(WIMStruct **swms, unsigned num_swms,
42           WIMStruct *joined_wim, int write_flags,
43           wimlib_progress_func_t progress_func)
44 {
45         int ret;
46         unsigned i;
47         union wimlib_progress_info progress;
48         u64 total_bytes = 0;
49         u64 part_bytes;
50         u64 swm_part_sizes[num_swms];
51
52         /* Calculate total size of the streams in the split WIM parts. */
53         for (i = 0; i < num_swms; i++) {
54                 part_bytes = lookup_table_total_stream_size(swms[i]->lookup_table);
55                 swm_part_sizes[i] = part_bytes;
56                 total_bytes += part_bytes;
57         }
58
59         if (progress_func) {
60                 progress.join.total_bytes     = total_bytes;
61                 progress.join.total_parts     = swms[0]->hdr.total_parts;
62                 progress.join.completed_bytes = 0;
63                 progress.join.completed_parts = 0;
64                 progress_func(WIMLIB_PROGRESS_MSG_JOIN_STREAMS, &progress);
65         }
66
67         /* Write the non-metadata resources from each SWM part */
68         for (i = 0; i < num_swms; i++) {
69                 ret = reopen_wim(swms[i]);
70                 if (ret)
71                         return ret;
72                 swms[i]->out_fd = joined_wim->out_fd;
73                 swms[i]->hdr.part_number = 1;
74
75                 ret = for_lookup_table_entry_pos_sorted(swms[i]->lookup_table,
76                                                         copy_resource,
77                                                         swms[i]);
78                 swms[i]->out_fd = -1;
79                 if (i != 0)
80                         close_wim(swms[i]);
81
82                 if (ret)
83                         return ret;
84
85                 if (progress_func) {
86                         progress.join.completed_bytes += swm_part_sizes[i];
87                         progress.join.completed_parts++;
88                         progress_func(WIMLIB_PROGRESS_MSG_JOIN_STREAMS, &progress);
89                 }
90         }
91
92         /* Copy the metadata resources from the first SWM part */
93         joined_wim->hdr.image_count = swms[0]->hdr.image_count;
94         for (i = 0; i < joined_wim->hdr.image_count; i++) {
95                 ret = copy_resource(swms[0]->image_metadata[i]->metadata_lte,
96                                     joined_wim);
97                 if (ret)
98                         return ret;
99         }
100
101         /* Preserve some of the header flags */
102         joined_wim->hdr.flags |= (swms[0]->hdr.flags &
103                               (WIM_HDR_FLAG_RP_FIX | WIM_HDR_FLAG_READONLY));
104
105         /* Write lookup table, XML data, and optional integrity table */
106         merge_lookup_tables(joined_wim, swms, num_swms);
107         free_wim_info(joined_wim->wim_info);
108         joined_wim->wim_info = swms[0]->wim_info;
109         joined_wim->image_metadata = swms[0]->image_metadata;
110         ret = finish_write(joined_wim, WIMLIB_ALL_IMAGES, write_flags, progress_func);
111         joined_wim->wim_info = NULL;
112         joined_wim->image_metadata = NULL;
113         return ret;
114 }
115
116 static int
117 cmp_swms_by_part_number(const void *swm1, const void *swm2)
118 {
119         u16 partno_1 = (*(const WIMStruct**)swm1)->hdr.part_number;
120         u16 partno_2 = (*(const WIMStruct**)swm2)->hdr.part_number;
121         return (int)partno_1 - (int)partno_2;
122 }
123
124 /*
125  * Join a set of split WIMs into a stand-alone WIM.
126  */
127 WIMLIBAPI int
128 wimlib_join(const tchar * const *swm_names,
129             unsigned num_swms,
130             const tchar *output_path,
131             int swm_open_flags,
132             int wim_write_flags,
133             wimlib_progress_func_t progress_func)
134 {
135         int ret;
136         WIMStruct *joined_wim = NULL;
137         unsigned i;
138
139         swm_open_flags |= WIMLIB_OPEN_FLAG_SPLIT_OK;
140         wim_write_flags &= WIMLIB_WRITE_MASK_PUBLIC;
141
142         if (num_swms < 1 || num_swms > 0xffff)
143                 return WIMLIB_ERR_INVALID_PARAM;
144
145         WIMStruct *swms[num_swms];
146         ZERO_ARRAY(swms);
147
148         for (i = 0; i < num_swms; i++) {
149                 ret = wimlib_open_wim(swm_names[i], swm_open_flags, &swms[i],
150                                       progress_func);
151                 if (ret)
152                         goto out_free_wims;
153
154                 /* Don't open all the parts at the same time, in case there are
155                  * a lot of them */
156                 close_wim(swms[i]);
157         }
158
159         qsort(swms, num_swms, sizeof(swms[0]), cmp_swms_by_part_number);
160
161         ret = verify_swm_set(swms[0], &swms[1], num_swms - 1);
162         if (ret)
163                 goto out_free_wims;
164
165         ret = wimlib_create_new_wim(swms[0]->compression_type, &joined_wim);
166         if (ret)
167                 goto out_free_wims;
168
169         ret = begin_write(joined_wim, output_path, wim_write_flags);
170         if (ret)
171                 goto out_free_wims;
172         ret = join_wims(swms, num_swms, joined_wim, wim_write_flags,
173                         progress_func);
174 out_free_wims:
175         for (i = 0; i < num_swms; i++)
176                 wimlib_free(swms[i]);
177         wimlib_free(joined_wim);
178         return ret;
179 }