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/.
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"
40 #include <fcntl.h> /* for open() */
41 #include <unistd.h> /* for close() */
46 size_t swm_base_name_len;
47 const tchar *swm_suffix;
48 struct list_head lte_list;
53 wimlib_progress_func_t progress_func;
54 union wimlib_progress_info progress;
58 finish_swm(WIMStruct *wim, struct list_head *lte_list,
59 int write_flags, wimlib_progress_func_t progress_func)
63 ret = write_lookup_table_from_stream_list(lte_list, wim->out_fd,
64 &wim->hdr.lookup_table_res_entry);
67 return finish_write(wim, WIMLIB_ALL_IMAGES,
68 write_flags | WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE,
73 copy_resource_to_swm(struct wim_lookup_table_entry *lte, void *_args)
75 struct split_args *args = (struct split_args*)_args;
76 WIMStruct *wim = args->wim;
79 if (args->size_remaining < 0 ||
80 (u64)args->size_remaining < lte->resource_entry.size) {
82 /* No space for this resource. Finish the previous swm and
85 ret = finish_swm(wim, &args->lte_list, args->write_flags,
90 if (args->progress_func) {
91 args->progress_func(WIMLIB_PROGRESS_MSG_SPLIT_END_PART,
95 INIT_LIST_HEAD(&args->lte_list);
96 args->cur_part_number++;
98 tsprintf(args->swm_base_name + args->swm_base_name_len, T("%d%"TS),
99 args->cur_part_number, args->swm_suffix);
101 wim->hdr.part_number = args->cur_part_number;
103 if (args->progress_func) {
104 args->progress.split.cur_part_number = args->cur_part_number;
105 args->progress_func(WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART,
109 ret = begin_write(wim, args->swm_base_name, args->write_flags);
112 args->size_remaining = args->part_size;
114 args->size_remaining -= lte->resource_entry.size;
115 args->progress.split.completed_bytes += lte->resource_entry.size;
116 list_add_tail(<e->swm_stream_list, &args->lte_list);
117 return copy_resource(lte, wim);
120 /* Splits the WIM file @wim into multiple parts prefixed by @swm_name with size
121 * at most @part_size bytes. */
123 wimlib_split(WIMStruct *wim, const tchar *swm_name,
124 size_t part_size, int write_flags,
125 wimlib_progress_func_t progress_func)
128 struct wim_header hdr_save;
129 struct split_args args;
130 const tchar *swm_suffix;
132 size_t swm_base_name_len;
134 if (!swm_name || part_size == 0)
135 return WIMLIB_ERR_INVALID_PARAM;
137 if (wim->hdr.total_parts != 1)
138 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
140 write_flags &= WIMLIB_WRITE_MASK_PUBLIC;
142 swm_name_len = tstrlen(swm_name);
143 tchar swm_base_name[swm_name_len + 20];
145 memcpy(&hdr_save, &wim->hdr, sizeof(struct wim_header));
146 wim->hdr.flags |= WIM_HDR_FLAG_SPANNED;
147 wim->hdr.boot_idx = 0;
148 randomize_byte_array(wim->hdr.guid, WIM_GID_LEN);
149 ret = begin_write(wim, swm_name, write_flags);
153 tmemcpy(swm_base_name, swm_name, swm_name_len + 1);
155 swm_suffix = tstrchr(swm_name, T('.'));
157 swm_base_name_len = swm_suffix - swm_name;
159 swm_base_name_len = swm_name_len;
160 swm_base_name[ARRAY_LEN(swm_base_name) - 1] = T('\0');
161 swm_suffix = &swm_base_name[ARRAY_LEN(swm_base_name) - 1];
165 args.swm_base_name = swm_base_name;
166 args.swm_base_name_len = swm_base_name_len;
167 args.swm_suffix = swm_suffix;
168 INIT_LIST_HEAD(&args.lte_list);
169 args.cur_part_number = 1;
170 args.write_flags = write_flags;
171 args.size_remaining = part_size;
172 args.part_size = part_size;
173 args.progress_func = progress_func;
174 args.progress.split.total_bytes = lookup_table_total_stream_size(wim->lookup_table);
175 args.progress.split.cur_part_number = 1;
176 args.progress.split.completed_bytes = 0;
177 args.progress.split.part_name = swm_base_name;
180 progress_func(WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART,
184 for (int i = 0; i < wim->hdr.image_count; i++) {
185 struct wim_lookup_table_entry *metadata_lte;
186 metadata_lte = wim->image_metadata[i]->metadata_lte;
187 ret = copy_resource(metadata_lte, wim);
190 args.size_remaining -= metadata_lte->resource_entry.size;
191 args.progress.split.completed_bytes += metadata_lte->resource_entry.size;
192 /* Careful: The metadata lookup table entries must be added in
193 * order of the images. */
194 list_add_tail(&metadata_lte->swm_stream_list, &args.lte_list);
197 ret = for_lookup_table_entry_pos_sorted(wim->lookup_table,
198 copy_resource_to_swm,
203 ret = finish_swm(wim, &args.lte_list, write_flags, progress_func);
208 progress_func(WIMLIB_PROGRESS_MSG_SPLIT_END_PART,
212 /* The swms are all ready now, except the total_parts and part_number
213 * fields in their headers are wrong (since we don't know the total
214 * parts until they are all written). Fix them. */
215 int total_parts = args.cur_part_number;
216 for (int i = 1; i <= total_parts; i++) {
217 const tchar *part_name;
222 part_name = swm_name;
224 tsprintf(swm_base_name + swm_base_name_len, T("%d%"TS),
226 part_name = swm_base_name;
229 part_fd = topen(part_name, O_WRONLY | O_BINARY);
231 ERROR_WITH_ERRNO("Failed to open `%"TS"'", part_name);
232 ret = WIMLIB_ERR_OPEN;
236 ret = write_header_part_data(i, total_parts, part_fd);
237 ret2 = close(part_fd);
238 if (ret == 0 && ret2 != 0)
239 ret = WIMLIB_ERR_WRITE;
241 ERROR_WITH_ERRNO("Error updating header of `%"TS"'",
248 close_wim_writable(wim);
249 memcpy(&wim->hdr, &hdr_save, sizeof(struct wim_header));