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