4 * Split a WIM file into parts.
8 * Copyright (C) 2012 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"
34 size_t swm_base_name_len;
35 const char *swm_suffix;
36 struct list_head *lte_list;
42 u64 total_bytes_written;
45 static int finish_swm(WIMStruct *w, struct list_head *lte_list,
48 off_t lookup_table_offset = ftello(w->out_fp);
50 struct lookup_table_entry *lte;
52 DEBUG("Writing lookup table for SWM (offset %"PRIu64")",
55 list_for_each_entry(lte, lte_list, staging_list) {
56 ret = write_lookup_table_entry(lte, w->out_fp);
61 off_t xml_data_offset = ftello(w->out_fp);
63 if (lookup_table_offset == -1 || xml_data_offset == -1)
64 return WIMLIB_ERR_WRITE;
65 w->hdr.lookup_table_res_entry.offset = lookup_table_offset;
66 w->hdr.lookup_table_res_entry.size =
67 xml_data_offset - lookup_table_offset;
68 w->hdr.lookup_table_res_entry.original_size =
69 xml_data_offset - lookup_table_offset;
70 w->hdr.lookup_table_res_entry.flags = WIM_RESHDR_FLAG_METADATA;
71 return finish_write(w, WIM_ALL_IMAGES,
72 write_flags | WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE);
75 static int copy_resource_to_swm(struct lookup_table_entry *lte, void *__args)
77 struct split_args *args = (struct split_args*)__args;
78 WIMStruct *w = args->w;
81 /* metadata resources were already written. */
82 if (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
85 if (args->size_remaining < 0 ||
86 (u64)args->size_remaining < lte->resource_entry.size) {
88 /* No space for this resource. Finish the previous swm and
91 ret = finish_swm(w, args->lte_list, args->write_flags);
94 INIT_LIST_HEAD(args->lte_list);
96 sprintf(args->swm_base_name + args->swm_base_name_len, "%d%s",
97 ++args->part_number, args->swm_suffix);
99 w->hdr.part_number = args->part_number;
101 if (args->write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
102 printf("Writing `%s' (%"PRIu64" of %"PRIu64" bytes, "
105 args->total_bytes_written,
107 (double)args->total_bytes_written /
108 (double)args->total_bytes * 100.0);
110 ret = begin_write(w, args->swm_base_name, args->write_flags);
113 args->size_remaining = args->part_size;
115 args->size_remaining -= lte->resource_entry.size;
116 args->total_bytes_written += lte->resource_entry.size;
117 list_add(<e->staging_list, args->lte_list);
118 return copy_resource(lte, w);
121 /* Splits the WIM file @wimfile into multiple parts prefixed by @swm_name with
122 * size at most @part_size. */
123 WIMLIBAPI int wimlib_split(const char *wimfile, const char *swm_name,
124 size_t part_size, int flags)
129 size_t swm_name_len = strlen(swm_name);
130 size_t swm_base_name_len;
131 char name[swm_name_len + 20];
134 long size_remaining = part_size;
135 u64 total_bytes_written = 0;
138 ret = wimlib_open_wim(wimfile, flags, &w);
142 total_bytes = wim_info_get_total_bytes(w->wim_info);
144 if (flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY)
145 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
146 if (flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
147 write_flags |= WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
149 w->hdr.flags |= WIM_HDR_FLAG_SPANNED;
151 randomize_byte_array(w->hdr.guid, WIM_GID_LEN);
152 ret = begin_write(w, swm_name, write_flags);
156 swm_suffix = strchr(swm_name, '.');
157 memcpy(name, swm_name, swm_name_len + 1);
159 swm_base_name_len = swm_suffix - swm_name;
161 swm_base_name_len = swm_name_len;
162 name[sizeof(name) - 1] = '\0';
163 swm_suffix = &name[sizeof(name) - 1];
166 if (write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
167 printf("Writing `%s' (%.2f %% done)\n",
169 (double)total_bytes_written /
170 (double)total_bytes * 100.0);
172 w->write_metadata = true;
174 for (int i = 0; i < w->hdr.image_count; i++) {
175 struct lookup_table_entry *metadata_lte;
177 DEBUG("Writing metadata resource %d", i);
179 metadata_lte = w->image_metadata[i].metadata_lte;
180 ret = copy_resource(metadata_lte, w);
183 size_remaining -= metadata_lte->resource_entry.size;
184 total_bytes_written += metadata_lte->resource_entry.size;
185 list_add(&metadata_lte->staging_list, <e_list);
187 w->write_metadata = false;
189 struct split_args args = {
191 .swm_base_name = name,
192 .swm_base_name_len = swm_base_name_len,
193 .swm_suffix = swm_suffix,
194 .lte_list = <e_list,
196 .write_flags = write_flags,
197 .size_remaining = size_remaining,
198 .part_size = part_size,
199 .total_bytes = total_bytes,
200 .total_bytes_written = total_bytes_written,
203 ret = for_lookup_table_entry(w->lookup_table, copy_resource_to_swm, &args);
207 ret = finish_swm(w, <e_list, write_flags);
212 /* The swms are all ready now, except the total_parts and part_number
213 * fields in their headers are wrong (we don't know the total parts
214 * until they are all written). Fix them. */
215 int total_parts = args.part_number;
216 for (int i = 1; i <= total_parts; i++) {
221 sprintf(name + swm_base_name_len, "%d", i);
222 p = strcat(name, swm_suffix);
225 FILE *fp = fopen(p, "r+b");
227 ERROR_WITH_ERRNO("Failed to open `%s'", p);
228 return WIMLIB_ERR_OPEN;
232 put_u16(&buf[2], total_parts);
234 if (fseek(fp, 40, SEEK_SET) != 0 ||
235 fwrite(buf, 1, sizeof(buf), fp) != sizeof(buf)
236 || fclose(fp) != 0) {
237 ERROR_WITH_ERRNO("Error overwriting header of `%s'",
239 return WIMLIB_ERR_WRITE;
242 if (write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)