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