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 *w, 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, w->out_fd,
64 &w->hdr.lookup_table_res_entry);
67 return finish_write(w, 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 *w = args->w;
79 /* metadata resources were already written. */
80 if (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
83 if (args->size_remaining < 0 ||
84 (u64)args->size_remaining < lte->resource_entry.size) {
86 /* No space for this resource. Finish the previous swm and
89 ret = finish_swm(w, &args->lte_list, args->write_flags,
94 if (args->progress_func) {
95 args->progress_func(WIMLIB_PROGRESS_MSG_SPLIT_END_PART,
99 INIT_LIST_HEAD(&args->lte_list);
100 args->cur_part_number++;
102 tsprintf(args->swm_base_name + args->swm_base_name_len, T("%d%"TS),
103 args->cur_part_number, args->swm_suffix);
105 w->hdr.part_number = args->cur_part_number;
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,
113 ret = begin_write(w, args->swm_base_name, args->write_flags);
116 args->size_remaining = args->part_size;
118 args->size_remaining -= lte->resource_entry.size;
119 args->progress.split.completed_bytes += lte->resource_entry.size;
120 list_add_tail(<e->swm_stream_list, &args->lte_list);
121 return copy_resource(lte, w);
124 /* Splits the WIM file @w into multiple parts prefixed by @swm_name with size at
125 * most @part_size bytes. */
127 wimlib_split(WIMStruct *w, const tchar *swm_name,
128 size_t part_size, int write_flags,
129 wimlib_progress_func_t progress_func)
132 struct wim_header hdr_save;
133 struct split_args args;
134 const tchar *swm_suffix;
136 size_t swm_base_name_len;
138 if (!swm_name || part_size == 0)
139 return WIMLIB_ERR_INVALID_PARAM;
141 if (w->hdr.total_parts != 1)
142 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
144 write_flags &= WIMLIB_WRITE_MASK_PUBLIC;
146 ret = wim_checksum_unhashed_streams(w);
150 swm_name_len = tstrlen(swm_name);
151 tchar swm_base_name[swm_name_len + 20];
153 memcpy(&hdr_save, &w->hdr, sizeof(struct wim_header));
154 w->hdr.flags |= WIM_HDR_FLAG_SPANNED;
156 randomize_byte_array(w->hdr.guid, WIM_GID_LEN);
157 ret = begin_write(w, swm_name, write_flags);
161 tmemcpy(swm_base_name, swm_name, swm_name_len + 1);
163 swm_suffix = tstrchr(swm_name, T('.'));
165 swm_base_name_len = swm_suffix - swm_name;
167 swm_base_name_len = swm_name_len;
168 swm_base_name[ARRAY_LEN(swm_base_name) - 1] = T('\0');
169 swm_suffix = &swm_base_name[ARRAY_LEN(swm_base_name) - 1];
173 args.swm_base_name = swm_base_name;
174 args.swm_base_name_len = swm_base_name_len;
175 args.swm_suffix = swm_suffix;
176 INIT_LIST_HEAD(&args.lte_list);
177 args.cur_part_number = 1;
178 args.write_flags = write_flags;
179 args.size_remaining = part_size;
180 args.part_size = part_size;
181 args.progress_func = progress_func;
182 args.progress.split.total_bytes = lookup_table_total_stream_size(w->lookup_table);
183 args.progress.split.cur_part_number = 1;
184 args.progress.split.completed_bytes = 0;
185 args.progress.split.part_name = swm_base_name;
188 progress_func(WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART,
192 for (int i = 0; i < w->hdr.image_count; i++) {
193 struct wim_lookup_table_entry *metadata_lte;
194 metadata_lte = w->image_metadata[i]->metadata_lte;
195 ret = copy_resource(metadata_lte, w);
198 args.size_remaining -= metadata_lte->resource_entry.size;
199 args.progress.split.completed_bytes += metadata_lte->resource_entry.size;
200 /* Careful: The metadata lookup table entries must be added in
201 * order of the images. */
202 list_add_tail(&metadata_lte->swm_stream_list, &args.lte_list);
205 ret = for_lookup_table_entry_pos_sorted(w->lookup_table,
206 copy_resource_to_swm,
211 ret = finish_swm(w, &args.lte_list, write_flags, progress_func);
216 progress_func(WIMLIB_PROGRESS_MSG_SPLIT_END_PART,
220 /* The swms are all ready now, except the total_parts and part_number
221 * fields in their headers are wrong (since we don't know the total
222 * parts until they are all written). Fix them. */
223 int total_parts = args.cur_part_number;
224 for (int i = 1; i <= total_parts; i++) {
225 const tchar *part_name;
228 size_t bytes_written;
231 part_name = swm_name;
233 tsprintf(swm_base_name + swm_base_name_len, T("%d%"TS),
235 part_name = swm_base_name;
238 part_fd = topen(part_name, O_WRONLY | O_BINARY);
240 ERROR_WITH_ERRNO("Failed to open `%"TS"'", part_name);
241 ret = WIMLIB_ERR_OPEN;
244 part_data[0] = cpu_to_le16(i);
245 part_data[1] = cpu_to_le16(total_parts);
247 bytes_written = full_pwrite(part_fd, part_data,
248 sizeof(part_data), 40);
249 ret = close(part_fd);
250 if (bytes_written != sizeof(part_data) || ret != 0) {
251 ERROR_WITH_ERRNO("Error updating header of `%"TS"'",
253 ret = WIMLIB_ERR_WRITE;
259 close_wim_writable(w);
260 memcpy(&w->hdr, &hdr_save, sizeof(struct wim_header));