]> wimlib.net Git - wimlib/blob - src/split.c
2636164b31cf83ee1ab746a7b236ee1e9ef2a065
[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 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include "wimlib.h"
31 #include "wimlib/endianness.h"
32 #include "wimlib/error.h"
33 #include "wimlib/file_io.h"
34 #include "wimlib/lookup_table.h"
35 #include "wimlib/metadata.h"
36 #include "wimlib/types.h"
37 #include "wimlib/write.h"
38 #include "wimlib/list.h"
39
40 #include <fcntl.h> /* for open() */
41 #include <unistd.h> /* for close() */
42
43 struct split_args {
44         WIMStruct *w;
45         tchar *swm_base_name;
46         size_t swm_base_name_len;
47         const tchar *swm_suffix;
48         struct list_head lte_list;
49         int cur_part_number;
50         int write_flags;
51         long size_remaining;
52         size_t part_size;
53         wimlib_progress_func_t progress_func;
54         union wimlib_progress_info progress;
55 };
56
57 static int
58 finish_swm(WIMStruct *w, struct list_head *lte_list,
59            int write_flags, wimlib_progress_func_t progress_func)
60 {
61         int ret;
62
63         ret = write_lookup_table_from_stream_list(lte_list, w->out_fd,
64                                                   &w->hdr.lookup_table_res_entry);
65         if (ret)
66                 return ret;
67         return finish_write(w, WIMLIB_ALL_IMAGES,
68                             write_flags | WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE,
69                             progress_func);
70 }
71
72 static int
73 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)
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                 tsprintf(args->swm_base_name + args->swm_base_name_len, T("%d%"TS),
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)
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_tail(&lte->swm_stream_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
127 wimlib_split(WIMStruct *w, const tchar *swm_name,
128              size_t part_size, int write_flags,
129              wimlib_progress_func_t progress_func)
130 {
131         int ret;
132         struct wim_header hdr_save;
133         struct split_args args;
134         const tchar *swm_suffix;
135         size_t swm_name_len;
136         size_t swm_base_name_len;
137
138         if (!swm_name || part_size == 0)
139                 return WIMLIB_ERR_INVALID_PARAM;
140
141         if (w->hdr.total_parts != 1)
142                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
143
144         write_flags &= WIMLIB_WRITE_MASK_PUBLIC;
145
146         swm_name_len = tstrlen(swm_name);
147         tchar swm_base_name[swm_name_len + 20];
148
149         memcpy(&hdr_save, &w->hdr, sizeof(struct wim_header));
150         w->hdr.flags |= WIM_HDR_FLAG_SPANNED;
151         w->hdr.boot_idx = 0;
152         randomize_byte_array(w->hdr.guid, WIM_GID_LEN);
153         ret = begin_write(w, swm_name, write_flags);
154         if (ret)
155                 goto out;
156
157         tmemcpy(swm_base_name, swm_name, swm_name_len + 1);
158
159         swm_suffix = tstrchr(swm_name, T('.'));
160         if (swm_suffix) {
161                 swm_base_name_len = swm_suffix - swm_name;
162         } else {
163                 swm_base_name_len = swm_name_len;
164                 swm_base_name[ARRAY_LEN(swm_base_name) - 1] = T('\0');
165                 swm_suffix = &swm_base_name[ARRAY_LEN(swm_base_name) - 1];
166         }
167
168         args.w                              = w;
169         args.swm_base_name                  = swm_base_name;
170         args.swm_base_name_len              = swm_base_name_len;
171         args.swm_suffix                     = swm_suffix;
172         INIT_LIST_HEAD(&args.lte_list);
173         args.cur_part_number                = 1;
174         args.write_flags                    = write_flags;
175         args.size_remaining                 = part_size;
176         args.part_size                      = part_size;
177         args.progress_func                  = progress_func;
178         args.progress.split.total_bytes     = lookup_table_total_stream_size(w->lookup_table);
179         args.progress.split.cur_part_number = 1;
180         args.progress.split.completed_bytes = 0;
181         args.progress.split.part_name       = swm_base_name;
182
183         if (progress_func) {
184                 progress_func(WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART,
185                               &args.progress);
186         }
187
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)
193                         goto out;
194                 args.size_remaining -= metadata_lte->resource_entry.size;
195                 args.progress.split.completed_bytes += metadata_lte->resource_entry.size;
196                 /* Careful: The metadata lookup table entries must be added in
197                  * order of the images. */
198                 list_add_tail(&metadata_lte->swm_stream_list, &args.lte_list);
199         }
200
201         ret = for_lookup_table_entry_pos_sorted(w->lookup_table,
202                                                 copy_resource_to_swm,
203                                                 &args);
204         if (ret)
205                 goto out;
206
207         ret = finish_swm(w, &args.lte_list, write_flags, progress_func);
208         if (ret)
209                 goto out;
210
211         if (progress_func) {
212                 progress_func(WIMLIB_PROGRESS_MSG_SPLIT_END_PART,
213                               &args.progress);
214         }
215
216         /* The swms are all ready now, except the total_parts and part_number
217          * fields in their headers are wrong (since we don't know the total
218          * parts until they are all written).  Fix them. */
219         int total_parts = args.cur_part_number;
220         for (int i = 1; i <= total_parts; i++) {
221                 const tchar *part_name;
222                 int part_fd;
223                 le16 part_data[2];
224                 size_t bytes_written;
225
226                 if (i == 1) {
227                         part_name = swm_name;
228                 } else {
229                         tsprintf(swm_base_name + swm_base_name_len, T("%d%"TS),
230                                  i, swm_suffix);
231                         part_name = swm_base_name;
232                 }
233
234                 part_fd = topen(part_name, O_WRONLY | O_BINARY);
235                 if (part_fd == -1) {
236                         ERROR_WITH_ERRNO("Failed to open `%"TS"'", part_name);
237                         ret = WIMLIB_ERR_OPEN;
238                         goto out;
239                 }
240                 part_data[0] = cpu_to_le16(i);
241                 part_data[1] = cpu_to_le16(total_parts);
242
243                 bytes_written = full_pwrite(part_fd, part_data,
244                                             sizeof(part_data), 40);
245                 ret = close(part_fd);
246                 if (bytes_written != sizeof(part_data) || ret != 0) {
247                         ERROR_WITH_ERRNO("Error updating header of `%"TS"'",
248                                          part_name);
249                         ret = WIMLIB_ERR_WRITE;
250                         goto out;
251                 }
252         }
253         ret = 0;
254 out:
255         close_wim_writable(w);
256         memcpy(&w->hdr, &hdr_save, sizeof(struct wim_header));
257         return ret;
258 }