]> wimlib.net Git - wimlib/blob - src/split.c
Windows: improved error messages
[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 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 3 of the License, or (at your option) any
13  * later version.
14  *
15  * This file is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this file; if not, see http://www.gnu.org/licenses/.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "wimlib.h"
29 #include "wimlib/alloca.h"
30 #include "wimlib/error.h"
31 #include "wimlib/list.h"
32 #include "wimlib/lookup_table.h"
33 #include "wimlib/metadata.h"
34 #include "wimlib/progress.h"
35 #include "wimlib/resource.h"
36 #include "wimlib/wim.h"
37 #include "wimlib/write.h"
38
39 struct swm_part_info {
40         struct list_head stream_list;
41         u64 size;
42 };
43
44 static void
45 copy_part_info(struct swm_part_info *dst, struct swm_part_info *src)
46 {
47         list_transfer(&src->stream_list, &dst->stream_list);
48         dst->size = src->size;
49 }
50
51 struct swm_info {
52         struct swm_part_info *parts;
53         unsigned num_parts;
54         unsigned num_alloc_parts;
55         u64 total_bytes;
56         u64 max_part_size;
57 };
58
59 static int
60 write_split_wim(WIMStruct *orig_wim, const tchar *swm_name,
61                 struct swm_info *swm_info, int write_flags)
62 {
63         size_t swm_name_len;
64         tchar *swm_name_buf;
65         const tchar *dot;
66         tchar *swm_suffix;
67         size_t swm_base_name_len;
68
69         union wimlib_progress_info progress;
70         unsigned part_number;
71         int ret;
72         u8 guid[WIMLIB_GUID_LEN];
73
74         swm_name_len = tstrlen(swm_name);
75         swm_name_buf = alloca((swm_name_len + 20) * sizeof(tchar));
76         tstrcpy(swm_name_buf, swm_name);
77         dot = tstrchr(swm_name_buf, T('.'));
78         if (dot) {
79                 swm_base_name_len = dot - swm_name_buf;
80                 swm_suffix = alloca((tstrlen(dot) + 1) * sizeof(tchar));
81                 tstrcpy(swm_suffix, dot);
82         } else {
83                 swm_base_name_len = swm_name_len;
84                 swm_suffix = alloca(1 * sizeof(tchar));
85                 swm_suffix[0] = T('\0');
86         }
87
88         progress.split.completed_bytes = 0;
89         progress.split.total_bytes = 0;
90         for (part_number = 1; part_number <= swm_info->num_parts; part_number++)
91                 progress.split.total_bytes += swm_info->parts[part_number - 1].size;
92         progress.split.total_parts = swm_info->num_parts;
93
94         randomize_byte_array(guid, WIMLIB_GUID_LEN);
95
96         for (part_number = 1; part_number <= swm_info->num_parts; part_number++) {
97                 int part_write_flags;
98                 wimlib_progress_func_t progfunc;
99
100                 if (part_number != 1) {
101                         tsprintf(swm_name_buf + swm_base_name_len,
102                                  T("%u%"TS), part_number, swm_suffix);
103                 }
104
105                 progress.split.cur_part_number = part_number;
106                 progress.split.part_name = swm_name_buf;
107
108                 ret = call_progress(orig_wim->progfunc,
109                                     WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART,
110                                     &progress,
111                                     orig_wim->progctx);
112                 if (ret)
113                         return ret;
114
115                 part_write_flags = write_flags;
116                 part_write_flags |= WIMLIB_WRITE_FLAG_USE_EXISTING_TOTALBYTES;
117                 if (part_number != 1)
118                         part_write_flags |= WIMLIB_WRITE_FLAG_NO_METADATA;
119
120                 progfunc = orig_wim->progfunc;
121                 orig_wim->progfunc = NULL;
122                 ret = write_wim_part(orig_wim,
123                                      progress.split.part_name,
124                                      WIMLIB_ALL_IMAGES,
125                                      part_write_flags,
126                                      1,
127                                      part_number,
128                                      swm_info->num_parts,
129                                      &swm_info->parts[part_number - 1].stream_list,
130                                      guid);
131                 orig_wim->progfunc = progfunc;
132                 if (ret)
133                         return ret;
134
135                 progress.split.completed_bytes += swm_info->parts[part_number - 1].size;
136
137                 ret = call_progress(orig_wim->progfunc,
138                                     WIMLIB_PROGRESS_MSG_SPLIT_END_PART,
139                                     &progress,
140                                     orig_wim->progctx);
141                 if (ret)
142                         return ret;
143         }
144         return 0;
145 }
146
147 static int
148 add_stream_to_swm(struct wim_lookup_table_entry *lte, void *_swm_info)
149 {
150         struct swm_info *swm_info = _swm_info;
151         u64 stream_size;
152
153         if (lte_is_partial(lte)) {
154                 ERROR("Splitting of WIM containing packed streams is not supported.\n"
155                       "        Export it in the default format first.");
156                 return WIMLIB_ERR_UNSUPPORTED;
157         }
158         if (lte->resource_location == RESOURCE_IN_WIM)
159                 stream_size = lte->rspec->size_in_wim;
160         else
161                 stream_size = lte->size;
162
163         /* - Start first part if no parts have been started so far;
164          * - Start next part if adding this stream exceeds maximum part size,
165          *   UNLESS the stream is metadata or if no streams at all have been
166          *   added to the current part.
167          */
168         if (swm_info->num_parts == 0 ||
169             ((swm_info->parts[swm_info->num_parts - 1].size +
170                         stream_size >= swm_info->max_part_size)
171              && !((lte->flags & WIM_RESHDR_FLAG_METADATA) ||
172                    swm_info->parts[swm_info->num_parts - 1].size == 0)))
173         {
174                 if (swm_info->num_parts == swm_info->num_alloc_parts) {
175                         struct swm_part_info *parts;
176                         size_t num_alloc_parts = swm_info->num_alloc_parts;
177
178                         num_alloc_parts += 8;
179                         parts = MALLOC(num_alloc_parts * sizeof(parts[0]));
180                         if (!parts)
181                                 return WIMLIB_ERR_NOMEM;
182
183                         for (unsigned i = 0; i < swm_info->num_parts; i++)
184                                 copy_part_info(&parts[i], &swm_info->parts[i]);
185
186                         FREE(swm_info->parts);
187                         swm_info->parts = parts;
188                         swm_info->num_alloc_parts = num_alloc_parts;
189                 }
190                 swm_info->num_parts++;
191                 INIT_LIST_HEAD(&swm_info->parts[swm_info->num_parts - 1].stream_list);
192                 swm_info->parts[swm_info->num_parts - 1].size = 0;
193         }
194         swm_info->parts[swm_info->num_parts - 1].size += stream_size;
195         if (!(lte->flags & WIM_RESHDR_FLAG_METADATA)) {
196                 list_add_tail(&lte->write_streams_list,
197                               &swm_info->parts[swm_info->num_parts - 1].stream_list);
198         }
199         swm_info->total_bytes += stream_size;
200         return 0;
201 }
202
203 /* API function documented in wimlib.h  */
204 WIMLIBAPI int
205 wimlib_split(WIMStruct *wim, const tchar *swm_name,
206              u64 part_size, int write_flags)
207 {
208         struct swm_info swm_info;
209         unsigned i;
210         int ret;
211
212         if (swm_name == NULL || swm_name[0] == T('\0') || part_size == 0)
213                 return WIMLIB_ERR_INVALID_PARAM;
214
215         if (write_flags & ~WIMLIB_WRITE_MASK_PUBLIC)
216                 return WIMLIB_ERR_INVALID_PARAM;
217
218         if (!wim_has_metadata(wim))
219                 return WIMLIB_ERR_METADATA_NOT_FOUND;
220
221         memset(&swm_info, 0, sizeof(swm_info));
222         swm_info.max_part_size = part_size;
223
224         for (i = 0; i < wim->hdr.image_count; i++) {
225                 ret = add_stream_to_swm(wim->image_metadata[i]->metadata_lte,
226                                         &swm_info);
227                 if (ret)
228                         goto out_free_swm_info;
229         }
230
231         ret = for_lookup_table_entry_pos_sorted(wim->lookup_table,
232                                                 add_stream_to_swm,
233                                                 &swm_info);
234         if (ret)
235                 goto out_free_swm_info;
236
237         ret = write_split_wim(wim, swm_name, &swm_info, write_flags);
238 out_free_swm_info:
239         FREE(swm_info.parts);
240         return ret;
241 }