typo
[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 Lesser General Public License as published by the Free
14  * Software Foundation; either version 2.1 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 Lesser General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU Lesser 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
30 static int join_wims(WIMStruct **swms, uint num_swms, WIMStruct *joined_wim,
31                      int write_flags)
32 {
33         uint i;
34         int ret;
35         FILE *out_fp = joined_wim->out_fp;
36         u64 total_bytes = wim_info_get_total_bytes(swms[0]->wim_info);
37
38         swms[0]->write_metadata = false;
39         for (i = 0; i < num_swms; i++) {
40                 if (write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS) {
41                         off_t cur_offset = ftello(out_fp);
42                         printf("Writing resources from part %u of %u "
43                                "(%"PRIu64" of %"PRIu64" bytes, %.0f%% done)\n",
44                                i + 1, num_swms, cur_offset, total_bytes,
45                                (double)cur_offset / total_bytes * 100.0);
46                 }
47                 swms[i]->fp = fopen(swms[i]->filename, "rb");
48                 if (!swms[i]->fp) {
49                         ERROR_WITH_ERRNO("Failed to reopen `%s'",
50                                          swms[i]->filename);
51                         return WIMLIB_ERR_OPEN;
52                 }
53                 swms[i]->out_fp = out_fp;
54                 swms[i]->hdr.part_number = 1;
55                 ret = for_lookup_table_entry(swms[i]->lookup_table, 
56                                              copy_resource, swms[i]);
57                 if (ret != 0)
58                         return ret;
59                 if (i != 0) {
60                         fclose(swms[i]->fp);
61                         swms[i]->fp = NULL;
62                 }
63         }
64         swms[0]->write_metadata = true;
65         if (write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS)
66                 printf("Writing %d metadata resources\n", 
67                         swms[0]->hdr.image_count);
68
69         for (i = 0; i < swms[0]->hdr.image_count; i++) {
70                 ret = copy_resource(swms[0]->image_metadata[i].metadata_lte, 
71                                     swms[0]);
72                 if (ret != 0)
73                         return ret;
74         }
75
76         off_t lookup_table_offset = ftello(out_fp);
77
78         if (write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS)
79                 printf("Writing lookup tables, XML data, and header\n");
80         /* Now write the lookup table for the joined wim.  Since the lookup
81          * table has no header, we can just concatenate the lookup tables of all
82          * the SWM parts. */
83         for (i = 0; i < num_swms; i++) {
84                 ret = write_lookup_table(swms[i]->lookup_table, out_fp);
85                 if (ret != 0)
86                         return ret;
87         }
88         off_t xml_data_offset = ftello(out_fp);
89
90         if (lookup_table_offset == -1 || xml_data_offset == -1) {
91                 ERROR_WITH_ERRNO("Failed to get file offset");
92                 return WIMLIB_ERR_WRITE;
93         }
94         swms[0]->hdr.lookup_table_res_entry.offset = lookup_table_offset;
95         swms[0]->hdr.lookup_table_res_entry.size = 
96                                         xml_data_offset - lookup_table_offset;
97
98
99         /* finish_write is called on the first swm, not the joined_wim, because
100          * the first swm is the one that has the image metadata and XML data
101          * attached to it.  */
102         swms[0]->hdr.flags &= ~WIM_HDR_FLAG_SPANNED;
103         swms[0]->hdr.total_parts = 1;
104         return finish_write(swms[0], WIM_ALL_IMAGES, write_flags, 0);
105 }
106
107
108 WIMLIBAPI int wimlib_join(const char **swm_names, int num_swms, 
109                           const char *output_path, int flags)
110 {
111         int i;
112         int ret;
113         int part_idx;
114         int write_flags = 0;
115         WIMStruct *w;
116         WIMStruct *joined_wim = NULL;
117         WIMStruct *swms[num_swms];
118
119         /* keep track of ctype and guid just to make sure they are the same for
120          * all the WIMs. */
121         int ctype;
122         u8 *guid;
123
124         ZERO_ARRAY(swms);
125         for (i = 0; i < num_swms; i++) {
126                 ret = wimlib_open_wim(swm_names[i], 
127                                       flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
128                 if (ret != 0)
129                         goto err;
130
131                 /* don't open all the parts at the same time, in case there are
132                  * a lot of them */
133                 fclose(w->fp);
134                 w->fp = NULL;
135
136                 if (i == 0) {
137                         ctype = wimlib_get_compression_type(w);
138                         guid = w->hdr.guid;
139                 } else {
140                         if (wimlib_get_compression_type(w) != ctype) {
141                                 ERROR("The split WIMs do not all have the same "
142                                       "compression type");
143                                 ret = WIMLIB_ERR_SPLIT_INVALID;
144                                 goto err;
145                         }
146                         if (memcmp(guid, w->hdr.guid, WIM_GID_LEN) != 0) {
147                                 ERROR("The split WIMs do not all have the same "
148                                       "GUID");
149                                 ret = WIMLIB_ERR_SPLIT_INVALID;
150                                 goto err;
151                         }
152                 }
153                 if (w->hdr.total_parts != num_swms) {
154                         ERROR("`%s' (part %d) says there are %d total parts, "
155                               "but %d parts were specified",
156                               swm_names[i], w->hdr.part_number,
157                               w->hdr.total_parts, num_swms);
158                         ret = WIMLIB_ERR_SPLIT_INVALID;
159                         goto err;
160                 }
161                 if (w->hdr.part_number == 0 || w->hdr.part_number > num_swms) {
162                         ERROR("`%s' says it is part %d, but expected a number "
163                               "between 1 and %d",
164                               swm_names[i], w->hdr.part_number, num_swms);
165                         ret = WIMLIB_ERR_SPLIT_INVALID;
166                         goto err;
167                 }
168                 part_idx = w->hdr.part_number - 1;
169                 if (swms[part_idx] != NULL) {
170                         ERROR("`%s' and `%s' both say they are part %d of %d",
171                               swm_names[i], swms[part_idx]->filename,
172                               w->hdr.part_number, num_swms);
173                         ret = WIMLIB_ERR_SPLIT_INVALID;
174                         goto err;
175                 }
176                 swms[part_idx] = w;
177
178         }
179         joined_wim = new_wim_struct();
180         if (!joined_wim) {
181                 ret = WIMLIB_ERR_NOMEM;
182                 goto err;
183         }
184
185         if (flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY)
186                 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
187         if (flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
188                 write_flags |= WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
189
190         ret = begin_write(joined_wim, output_path, write_flags);
191         if (ret != 0)
192                 goto err;
193         ret = join_wims(swms, num_swms, joined_wim, write_flags);
194 err:
195         for (i = 0; i < num_swms; i++) {
196                 /* out_fp is the same in all the swms and joined_wim; only close
197                  * it one time, when freeing joined_wim. */
198                 if (swms[i]) {
199                         swms[i]->out_fp = NULL;
200                         wimlib_free(swms[i]);
201                 }
202         }
203         wimlib_free(joined_wim);
204         return ret;
205 }