4 * Split a WIM file into parts.
8 * Copyright (C) 2012, 2013 Eric Biggers
10 * This file is part of wimlib, a library for working with WIM files.
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)
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
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/.
26 #include "wimlib_internal.h"
27 #include "lookup_table.h"
29 #include "buffer_io.h"
36 size_t swm_base_name_len;
37 const tchar *swm_suffix;
38 struct list_head lte_list;
43 wimlib_progress_func_t progress_func;
44 union wimlib_progress_info progress;
48 finish_swm(WIMStruct *w, struct list_head *lte_list,
49 int write_flags, wimlib_progress_func_t progress_func)
53 ret = write_lookup_table_from_stream_list(lte_list, w->out_fd,
54 &w->hdr.lookup_table_res_entry);
57 return finish_write(w, WIMLIB_ALL_IMAGES,
58 write_flags | WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE,
63 copy_resource_to_swm(struct wim_lookup_table_entry *lte, void *__args)
65 struct split_args *args = (struct split_args*)__args;
66 WIMStruct *w = args->w;
69 /* metadata resources were already written. */
70 if (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
73 if (args->size_remaining < 0 ||
74 (u64)args->size_remaining < lte->resource_entry.size) {
76 /* No space for this resource. Finish the previous swm and
79 ret = finish_swm(w, &args->lte_list, args->write_flags,
84 if (args->progress_func) {
85 args->progress_func(WIMLIB_PROGRESS_MSG_SPLIT_END_PART,
89 INIT_LIST_HEAD(&args->lte_list);
90 args->cur_part_number++;
92 tsprintf(args->swm_base_name + args->swm_base_name_len, T("%d%"TS),
93 args->cur_part_number, args->swm_suffix);
95 w->hdr.part_number = args->cur_part_number;
97 if (args->progress_func) {
98 args->progress.split.cur_part_number = args->cur_part_number;
99 args->progress_func(WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART,
103 ret = begin_write(w, args->swm_base_name, args->write_flags);
106 args->size_remaining = args->part_size;
108 args->size_remaining -= lte->resource_entry.size;
109 args->progress.split.completed_bytes += lte->resource_entry.size;
110 list_add_tail(<e->swm_stream_list, &args->lte_list);
111 return copy_resource(lte, w);
114 /* Splits the WIM file @w into multiple parts prefixed by @swm_name with size at
115 * most @part_size bytes. */
117 wimlib_split(WIMStruct *w, const tchar *swm_name,
118 size_t part_size, int write_flags,
119 wimlib_progress_func_t progress_func)
122 struct wim_header hdr_save;
123 struct split_args args;
124 const tchar *swm_suffix;
126 size_t swm_base_name_len;
128 if (!swm_name || part_size == 0)
129 return WIMLIB_ERR_INVALID_PARAM;
131 if (w->hdr.total_parts != 1)
132 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
134 write_flags &= WIMLIB_WRITE_MASK_PUBLIC;
136 ret = wim_checksum_unhashed_streams(w);
140 swm_name_len = tstrlen(swm_name);
141 tchar swm_base_name[swm_name_len + 20];
143 memcpy(&hdr_save, &w->hdr, sizeof(struct wim_header));
144 w->hdr.flags |= WIM_HDR_FLAG_SPANNED;
146 randomize_byte_array(w->hdr.guid, WIM_GID_LEN);
147 ret = begin_write(w, swm_name, write_flags);
151 tmemcpy(swm_base_name, swm_name, swm_name_len + 1);
153 swm_suffix = tstrchr(swm_name, T('.'));
155 swm_base_name_len = swm_suffix - swm_name;
157 swm_base_name_len = swm_name_len;
158 swm_base_name[ARRAY_LEN(swm_base_name) - 1] = T('\0');
159 swm_suffix = &swm_base_name[ARRAY_LEN(swm_base_name) - 1];
163 args.swm_base_name = swm_base_name;
164 args.swm_base_name_len = swm_base_name_len;
165 args.swm_suffix = swm_suffix;
166 INIT_LIST_HEAD(&args.lte_list);
167 args.cur_part_number = 1;
168 args.write_flags = write_flags;
169 args.size_remaining = part_size;
170 args.part_size = part_size;
171 args.progress_func = progress_func;
172 args.progress.split.total_bytes = lookup_table_total_stream_size(w->lookup_table);
173 args.progress.split.cur_part_number = 1;
174 args.progress.split.completed_bytes = 0;
175 args.progress.split.part_name = swm_base_name;
178 progress_func(WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART,
182 for (int i = 0; i < w->hdr.image_count; i++) {
183 struct wim_lookup_table_entry *metadata_lte;
184 metadata_lte = w->image_metadata[i]->metadata_lte;
185 ret = copy_resource(metadata_lte, w);
188 args.size_remaining -= metadata_lte->resource_entry.size;
189 args.progress.split.completed_bytes += metadata_lte->resource_entry.size;
190 /* Careful: The metadata lookup table entries must be added in
191 * order of the images. */
192 list_add_tail(&metadata_lte->swm_stream_list, &args.lte_list);
195 ret = for_lookup_table_entry_pos_sorted(w->lookup_table,
196 copy_resource_to_swm,
201 ret = finish_swm(w, &args.lte_list, write_flags, progress_func);
206 progress_func(WIMLIB_PROGRESS_MSG_SPLIT_END_PART,
210 /* The swms are all ready now, except the total_parts and part_number
211 * fields in their headers are wrong (since we don't know the total
212 * parts until they are all written). Fix them. */
213 int total_parts = args.cur_part_number;
214 for (int i = 1; i <= total_parts; i++) {
215 const tchar *part_name;
218 size_t bytes_written;
221 part_name = swm_name;
223 tsprintf(swm_base_name + swm_base_name_len, T("%d%"TS),
225 part_name = swm_base_name;
228 part_fd = topen(part_name, O_WRONLY | O_BINARY);
229 if (part_fd == INVALID_FILEDES) {
230 ERROR_WITH_ERRNO("Failed to open `%"TS"'", part_name);
231 ret = WIMLIB_ERR_OPEN;
234 put_u16(&part_data_buf[0], i);
235 put_u16(&part_data_buf[2], total_parts);
237 bytes_written = full_pwrite(part_fd, part_data_buf,
238 sizeof(part_data_buf), 40);
239 ret = close(part_fd);
240 if (bytes_written != sizeof(part_data_buf) || ret != 0) {
241 ERROR_WITH_ERRNO("Error updating header of `%"TS"'",
243 ret = WIMLIB_ERR_WRITE;
249 close_wim_writable(w);
250 memcpy(&w->hdr, &hdr_save, sizeof(struct wim_header));