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