]> wimlib.net Git - wimlib/blob - src/join.c
b804f8f80991f50e6277e6fb89f50d70072a6e95
[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) 2010 Carl Thijssen
7  * Copyright (C) 2012 Eric Biggers
8  *
9  * wimlib - Library for working with WIM files 
10  *
11  * This library is free software; you can redistribute it and/or modify it under
12  * the terms of the GNU Lesser General Public License as published by the Free
13  * Software Foundation; either version 2.1 of the License, or (at your option) any
14  * later version.
15  *
16  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
17  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
18  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License along
21  * with this library; if not, write to the Free Software Foundation, Inc., 59
22  * Temple Place, Suite 330, Boston, MA 02111-1307 USA 
23  */
24
25 #include "wimlib.h"
26 #include "lookup_table.h"
27 #include "xml.h"
28
29 static int join_resource(struct lookup_table_entry *lte, void *split_wim)
30 {
31         FILE *split_wim_fp = ((WIMStruct*)split_wim)->fp;
32         FILE *joined_wim_fp = ((WIMStruct*)split_wim)->out_fp;
33         int ret;
34
35         u64 size = lte->resource_entry.size;
36         u64 offset = lte->resource_entry.offset;
37         off_t new_offset = ftello(joined_wim_fp);
38
39         if (new_offset == -1)
40                 return WIMLIB_ERR_WRITE;
41
42         ret = copy_between_files(split_wim_fp, offset, joined_wim_fp, size);
43         if (ret != 0)
44                 return ret;
45
46         memcpy(&lte->output_resource_entry, &lte->resource_entry, 
47                         sizeof(struct resource_entry));
48
49         lte->output_resource_entry.offset = new_offset;
50         lte->out_refcnt = lte->refcnt;
51         lte->part_number = 1;
52         return 0;
53 }
54
55 static int join_wims(WIMStruct **swms, uint num_swms, WIMStruct *joined_wim,
56                      int write_flags)
57 {
58         uint i;
59         int ret;
60         FILE *out_fp = joined_wim->out_fp;
61         u64 total_bytes = wim_info_get_total_bytes(swms[0]->wim_info);
62
63         /* The following loop writes both file resources and metadata resources
64          * because it loops over the lookup table entries rather than the dentry
65          * tree for the images */
66         for (i = 0; i < num_swms; i++) {
67                 if (write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS) {
68                         off_t cur_offset = ftello(out_fp);
69                         printf("Writing resources from part %u of %u "
70                                         "(%"PRIu64" of %"PRIu64" bytes, %.2f%% done)\n",
71                                         i + 1, num_swms,
72                                         cur_offset, total_bytes,
73                                         (double)cur_offset / total_bytes * 100.0);
74                 }
75                 swms[i]->out_fp = out_fp;
76                 ret = for_lookup_table_entry(swms[i]->lookup_table, 
77                                              join_resource, swms[i]);
78                 if (ret != 0)
79                         return ret;
80         }
81
82         off_t lookup_table_offset = ftello(out_fp);
83
84         if (write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS)
85                 printf("Writing lookup tables, XML data, and header\n");
86         /* Now write the lookup table for the joined wim.  Since the lookup
87          * table has no header, we can just concatenate the lookup tables of all
88          * the SWM parts. */
89         for (i = 0; i < num_swms; i++) {
90                 ret = write_lookup_table(swms[i]->lookup_table, out_fp);
91                 if (ret != 0)
92                         return ret;
93         }
94         off_t xml_data_offset = ftello(out_fp);
95
96         if (lookup_table_offset == -1 || xml_data_offset == -1) {
97                 ERROR("Failed to get file offset: %m\n");
98                 return WIMLIB_ERR_WRITE;
99         }
100         swms[0]->hdr.lookup_table_res_entry.offset = lookup_table_offset;
101         swms[0]->hdr.lookup_table_res_entry.size = 
102                                         xml_data_offset - lookup_table_offset;
103
104         swms[0]->hdr.flags &= ~WIM_HDR_FLAG_SPANNED;
105
106         /* finish_write is called on the first swm, not the joined_wim, because
107          * the first swm is the one that has the image metadata and XML data
108          * attached to it.  */
109         return finish_write(swms[0], WIM_ALL_IMAGES, write_flags, 0);
110 }
111
112
113 WIMLIBAPI int wimlib_join(const char **swm_names, int num_swms, 
114                           const char *output_path, int flags)
115 {
116         int i;
117         int ret;
118         int part_idx;
119         int write_flags = 0;
120         WIMStruct *w;
121         WIMStruct *joined_wim = NULL;
122         WIMStruct *swms[num_swms];
123
124         /* keep track of ctype and guid just to make sure they are the same for
125          * all the WIMs. */
126         int ctype;
127         u8 *guid;
128
129         ZERO_ARRAY(swms);
130         for (i = 0; i < num_swms; i++) {
131                 ret = wimlib_open_wim(swm_names[i], 
132                                       flags | WIMLIB_OPEN_FLAG_SPLIT_OK, &w);
133                 if (ret != 0)
134                         goto err;
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!\n");
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 "
148                                                 "same GUID!\n");
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,\n"
155                                         "but %d parts were specified!\n",
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\n"
163                                         "between 1 and %d!\n",
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!\n",
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                 swms[i]->out_fp = NULL;
199                 wimlib_free(swms[i]);
200         }
201         wimlib_free(joined_wim);
202         return ret;
203 }