]> wimlib.net Git - wimlib/blob - src/join.c
94ce400b78bb4a41bb1705752b06172e36c0205b
[wimlib] / src / join.c
1 /*
2  * join.c
3  *
4  * Join split WIMs (sometimes named as .swm files) together into one WIM.
5  *
6  * Copyright (C) 2012 Eric Biggers
7  *
8  * wimlib - Library for working with WIM files 
9  *
10  * This library is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU Lesser General Public License as published by the Free
12  * Software Foundation; either version 2.1 of the License, or (at your option) any
13  * later version.
14  *
15  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
17  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License along
20  * with this library; if not, write to the Free Software Foundation, Inc., 59
21  * Temple Place, Suite 330, Boston, MA 02111-1307 USA 
22  */
23
24 #include "wimlib_internal.h"
25 #include "lookup_table.h"
26 #include "xml.h"
27
28 static int join_wims(WIMStruct **swms, uint num_swms, WIMStruct *joined_wim,
29                      int write_flags)
30 {
31         uint i;
32         int ret;
33         FILE *out_fp = joined_wim->out_fp;
34         u64 total_bytes = wim_info_get_total_bytes(swms[0]->wim_info);
35
36         swms[0]->write_metadata = false;
37         for (i = 0; i < num_swms; i++) {
38                 if (write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS) {
39                         off_t cur_offset = ftello(out_fp);
40                         printf("Writing resources from part %u of %u "
41                                         "(%"PRIu64" of %"PRIu64" bytes, %.0f%% done)\n",
42                                         i + 1, num_swms,
43                                         cur_offset, total_bytes,
44                                         (double)cur_offset / total_bytes * 100.0);
45                 }
46                 swms[i]->fp = fopen(swms[i]->filename, "rb");
47                 if (!swms[i]->fp) {
48                         ERROR("Failed to reopen `%s': %m\n", swms[i]->filename);
49                         return WIMLIB_ERR_OPEN;
50                 }
51                 swms[i]->out_fp = out_fp;
52                 swms[i]->hdr.part_number = 1;
53                 ret = for_lookup_table_entry(swms[i]->lookup_table, 
54                                              copy_resource, swms[i]);
55                 if (ret != 0)
56                         return ret;
57                 if (i != 0) {
58                         fclose(swms[i]->fp);
59                         swms[i]->fp = NULL;
60                 }
61         }
62         swms[0]->write_metadata = true;
63         if (write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS)
64                 printf("Writing %d metadata resources\n", 
65                         swms[0]->hdr.image_count);
66
67         for (i = 0; i < swms[0]->hdr.image_count; i++) {
68                 ret = copy_resource(swms[0]->image_metadata[i].lookup_table_entry, 
69                                     swms[0]);
70                 if (ret != 0)
71                         return ret;
72         }
73
74         off_t lookup_table_offset = ftello(out_fp);
75
76         if (write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS)
77                 printf("Writing lookup tables, XML data, and header\n");
78         /* Now write the lookup table for the joined wim.  Since the lookup
79          * table has no header, we can just concatenate the lookup tables of all
80          * the SWM parts. */
81         for (i = 0; i < num_swms; i++) {
82                 ret = write_lookup_table(swms[i]->lookup_table, out_fp);
83                 if (ret != 0)
84                         return ret;
85         }
86         off_t xml_data_offset = ftello(out_fp);
87
88         if (lookup_table_offset == -1 || xml_data_offset == -1) {
89                 ERROR("Failed to get file offset: %m\n");
90                 return WIMLIB_ERR_WRITE;
91         }
92         swms[0]->hdr.lookup_table_res_entry.offset = lookup_table_offset;
93         swms[0]->hdr.lookup_table_res_entry.size = 
94                                         xml_data_offset - lookup_table_offset;
95
96
97         /* finish_write is called on the first swm, not the joined_wim, because
98          * the first swm is the one that has the image metadata and XML data
99          * attached to it.  */
100         swms[0]->hdr.flags &= ~WIM_HDR_FLAG_SPANNED;
101         swms[0]->hdr.total_parts = 1;
102         return finish_write(swms[0], WIM_ALL_IMAGES, write_flags, 0);
103 }
104
105
106 WIMLIBAPI int wimlib_join(const char **swm_names, int num_swms, 
107                           const char *output_path, int flags)
108 {
109         int i;
110         int ret;
111         int part_idx;
112         int write_flags = 0;
113         WIMStruct *w;
114         WIMStruct *joined_wim = NULL;
115         WIMStruct *swms[num_swms];
116
117         /* keep track of ctype and guid just to make sure they are the same for
118          * all the WIMs. */
119         int ctype;
120         u8 *guid;
121
122         ZERO_ARRAY(swms);
123         for (i = 0; i < num_swms; i++) {
124                 ret = wimlib_open_wim(swm_names[i], 
125                                       flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
126                 if (ret != 0)
127                         goto err;
128
129                 /* don't open all the parts at the same time, in case there are
130                  * a lot af them */
131                 fclose(w->fp);
132                 w->fp = NULL;
133
134                 if (i == 0) {
135                         ctype = wimlib_get_compression_type(w);
136                         guid = w->hdr.guid;
137                 } else {
138                         if (wimlib_get_compression_type(w) != ctype) {
139                                 ERROR("The split WIMs do not all have the same "
140                                                 "compression type!\n");
141                                 ret = WIMLIB_ERR_SPLIT_INVALID;
142                                 goto err;
143                         }
144                         if (memcmp(guid, w->hdr.guid, WIM_GID_LEN) != 0) {
145                                 ERROR("The split WIMs do not all have the "
146                                                 "same GUID!\n");
147                                 ret = WIMLIB_ERR_SPLIT_INVALID;
148                                 goto err;
149                         }
150                 }
151                 if (w->hdr.total_parts != num_swms) {
152                         ERROR("`%s' (part %d) says there are %d total parts, "
153                                         "but %d parts were specified!\n",
154                                         swm_names[i], w->hdr.part_number,
155                                         w->hdr.total_parts, num_swms);
156                         ret = WIMLIB_ERR_SPLIT_INVALID;
157                         goto err;
158                 }
159                 if (w->hdr.part_number == 0 || w->hdr.part_number > num_swms) {
160                         ERROR("`%s' says it is part %d, but expected a number\n"
161                                         "between 1 and %d!\n",
162                                 swm_names[i], w->hdr.part_number, num_swms);
163                         ret = WIMLIB_ERR_SPLIT_INVALID;
164                         goto err;
165                 }
166                 part_idx = w->hdr.part_number - 1;
167                 if (swms[part_idx] != NULL) {
168                         ERROR("`%s' and `%s' both say they are part %d of %d!\n",
169                                 swm_names[i], swms[part_idx]->filename,
170                                 w->hdr.part_number, num_swms);
171                         ret = WIMLIB_ERR_SPLIT_INVALID;
172                         goto err;
173                 }
174                 swms[part_idx] = w;
175
176         }
177         joined_wim = new_wim_struct();
178         if (!joined_wim) {
179                 ret = WIMLIB_ERR_NOMEM;
180                 goto err;
181         }
182
183         if (flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY)
184                 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
185         if (flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
186                 write_flags |= WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
187
188         ret = begin_write(joined_wim, output_path, write_flags);
189         if (ret != 0)
190                 goto err;
191         ret = join_wims(swms, num_swms, joined_wim, write_flags);
192 err:
193         for (i = 0; i < num_swms; i++) {
194                 /* out_fp is the same in all the swms and joined_wim; only close
195                  * it one time, when freeing joined_wim. */
196                 if (swms[i]) {
197                         swms[i]->out_fp = NULL;
198                         wimlib_free(swms[i]);
199                 }
200         }
201         wimlib_free(joined_wim);
202         return ret;
203 }