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