]> wimlib.net Git - wimlib/blob - src/split.c
Support for splitting WIMs
[wimlib] / src / split.c
1 /*
2  * split.c
3  *
4  * Split a WIM file into parts.
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 #include "io.h"
28
29 struct args {
30         WIMStruct *w;
31         char *swm_base_name;
32         size_t swm_base_name_len;
33         const char *swm_suffix;
34         struct lookup_table_entry *lte_chain_head;
35         struct lookup_table_entry *lte_chain_tail;
36         int    part_number;
37         int    write_flags;
38         long   size_remaining;
39         size_t part_size;
40         u64    total_bytes;
41         u64    total_bytes_written;
42 };
43
44 static int finish_swm(WIMStruct *w, struct lookup_table_entry *lte_chain_head,
45                       int write_flags)
46 {
47         off_t lookup_table_offset = ftello(w->out_fp);
48         int ret;
49
50         DEBUG("Writing lookup table for SWM (offset %"PRIu64")\n", 
51                         lookup_table_offset);
52
53         while (lte_chain_head != NULL) {
54                 ret = write_lookup_table_entry(lte_chain_head, w->out_fp);
55                 if (ret != 0)
56                         return ret;
57                 struct lookup_table_entry *prev = lte_chain_head;
58                 lte_chain_head = prev->next_lte_in_swm;
59                 prev->next_lte_in_swm = NULL;
60         }
61         off_t xml_data_offset = ftello(w->out_fp);
62
63         if (lookup_table_offset == -1 || xml_data_offset == -1)
64                 return WIMLIB_ERR_WRITE;
65         w->hdr.lookup_table_res_entry.offset = lookup_table_offset;
66         w->hdr.lookup_table_res_entry.size = 
67                                 xml_data_offset - lookup_table_offset;
68         ret = finish_write(w, WIM_ALL_IMAGES, write_flags, 0);
69         if (ret != 0)
70                 return ret;
71
72         ret = fclose(w->out_fp);
73         if (ret != 0)
74                 ret = WIMLIB_ERR_WRITE;
75         w->out_fp = NULL;
76         return ret;
77 }
78
79 static int copy_resource_to_swm(struct lookup_table_entry *lte, void *__args)
80 {
81         struct args *args = (struct args*)__args;
82         WIMStruct *w = args->w;
83         FILE *out_fp = w->out_fp;
84         int ret;
85
86         /* metadata resources were already written. */
87         if (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
88                 return 0;
89
90         if (args->size_remaining < 0 || 
91                         (u64)args->size_remaining < lte->resource_entry.size) {
92
93                 /* No space for this resource.  Finish the previous swm and
94                  * start a new one. */
95
96                 ret = finish_swm(w, args->lte_chain_head, args->write_flags);
97
98                 args->lte_chain_tail = NULL;
99                 args->lte_chain_head = NULL;
100
101                 sprintf(args->swm_base_name + args->swm_base_name_len, "%d", 
102                         ++args->part_number);
103                 strcat(args->swm_base_name, args->swm_suffix);
104
105                 w->hdr.part_number = args->part_number;
106
107                 if (args->write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
108                         printf("Writing `%s' (%"PRIu64" of %"PRIu64" bytes, "
109                                         "%.0f%% done)\n", 
110                                 args->swm_base_name, 
111                                 args->total_bytes_written,
112                                 args->total_bytes,
113                                 (double)args->total_bytes_written /
114                                         (double)args->total_bytes * 100.0);
115
116                 ret = begin_write(w, args->swm_base_name, args->write_flags);
117                 if (ret != 0)
118                         return ret;
119                 args->size_remaining = args->part_size;
120         }
121         args->size_remaining -= lte->resource_entry.size;
122         args->total_bytes_written += lte->resource_entry.size;
123         if (args->lte_chain_tail)
124                 args->lte_chain_tail->next_lte_in_swm = lte;
125         else
126                 args->lte_chain_head = lte;
127         args->lte_chain_tail = lte;
128         return copy_resource(lte, w);
129 }
130
131 /* Splits the WIM file @wimfile into multiple parts prefixed by @swm_name with
132  * size at most @part_size. */
133 WIMLIBAPI int wimlib_split(const char *wimfile, const char *swm_name, 
134                            size_t part_size, int flags)
135 {
136         int ret;
137         WIMStruct *w;
138         int write_flags = 0;
139         size_t swm_name_len = strlen(swm_name);
140         size_t swm_base_name_len;
141         char name[swm_name_len + 20];
142         char *swm_suffix;
143
144         struct lookup_table_entry *lte_chain_head = NULL;
145         struct lookup_table_entry *lte_chain_tail = NULL;
146         long size_remaining = part_size;
147         u64 total_bytes_written = 0;
148         u64 total_bytes;
149
150         ret = wimlib_open_wim(wimfile, flags, &w);
151         if (ret != 0)
152                 return ret;
153
154         total_bytes = wim_info_get_total_bytes(w->wim_info);
155
156         if (flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY)
157                 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
158         if (flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
159                 write_flags |= WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
160
161         w->hdr.flags |= WIM_HDR_FLAG_SPANNED;
162         w->hdr.boot_idx = 0;
163         randomize_byte_array(w->hdr.guid, WIM_GID_LEN);
164         ret = begin_write(w, swm_name, write_flags);
165         if (ret != 0)
166                 return ret;
167
168         swm_suffix = strchr(swm_name, '.');
169         memcpy(name, swm_name, swm_name_len + 1);
170         if (swm_suffix) {
171                 swm_base_name_len = swm_suffix - swm_name;
172         } else {
173                 swm_base_name_len = swm_name_len;
174                 name[sizeof(name) - 1] = '\0';
175                 swm_suffix = &name[sizeof(name) - 1];
176         }
177
178         if (write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
179                 printf("Writing `%s' (%.2f %% done)\n", 
180                         swm_name, 
181                         (double)total_bytes_written /
182                                 (double)total_bytes * 100.0);
183
184         w->write_metadata = true;
185         for (int i = 0; i < w->hdr.image_count; i++) {
186
187                 struct lookup_table_entry *metadata_lte;
188
189                 metadata_lte = w->image_metadata[i].lookup_table_entry;
190                 ret = copy_resource(metadata_lte, w);
191                 if (ret != 0)
192                         return ret;
193                 size_remaining -= metadata_lte->resource_entry.size;
194                 total_bytes_written += metadata_lte->resource_entry.size;
195                 if (lte_chain_tail)
196                         lte_chain_tail->next_lte_in_swm = metadata_lte;
197                 else
198                         lte_chain_head = metadata_lte;
199                 lte_chain_tail = metadata_lte;
200         }
201         w->write_metadata = false;
202
203         struct args args = {
204                 .w                 = w,
205                 .swm_base_name     = name,
206                 .swm_base_name_len = swm_base_name_len,
207                 .swm_suffix        = swm_suffix,
208                 .lte_chain_head    = lte_chain_head,
209                 .lte_chain_tail    = lte_chain_tail,
210                 .part_number       = 1,
211                 .write_flags       = write_flags,
212                 .size_remaining    = size_remaining,
213                 .part_size         = part_size,
214                 .total_bytes        = total_bytes,
215                 .total_bytes_written = total_bytes_written,
216         };
217
218         ret = for_lookup_table_entry(w->lookup_table, copy_resource_to_swm, &args);
219         if (ret != 0)
220                 return ret;
221
222         ret = finish_swm(w, args.lte_chain_head, write_flags);
223         if (ret != 0)
224                 return ret;
225
226
227         /* The swms are all ready now, except the total_parts and part_number
228          * fields in their headers are wrong (we don't know the total parts
229          * until they are all written).  Fix them. */
230         int total_parts = args.part_number;
231         for (int i = 1; i <= total_parts; i++) {
232                 const char *p;
233                 if (i == 1) {
234                         p = swm_name;
235                 } else {
236                         sprintf(name + swm_base_name_len, "%d", i);
237                         p = strcat(name, swm_suffix);
238                 }
239
240                 FILE *fp = fopen(p, "r+b");
241                 if (!fp) {
242                         ERROR("Failed to open `%s': %m\n", p);
243                         return WIMLIB_ERR_OPEN;
244                 }
245                 char buf[4];
246                 put_u16(buf, i);
247                 put_u16(buf + 2, total_parts);
248
249                 if (fseek(fp, 40, SEEK_SET) != 0 || 
250                                 fwrite(buf, 1, sizeof(buf), fp) != sizeof(buf)
251                                 || fclose(fp) != 0) {
252                         ERROR("Error overwriting header of `%s': %m\n", name);
253                         return WIMLIB_ERR_WRITE;
254                 }
255         }
256         if (write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
257                 printf("Done!\n");
258         wimlib_free(w);
259         return 0;
260 }