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 lookup_table_entry *lte_chain_head;
37 struct lookup_table_entry *lte_chain_tail;
43 u64 total_bytes_written;
46 static int finish_swm(WIMStruct *w, struct lookup_table_entry *lte_chain_head,
49 off_t lookup_table_offset = ftello(w->out_fp);
52 DEBUG("Writing lookup table for SWM (offset %"PRIu64")",
55 while (lte_chain_head != NULL) {
56 ret = write_lookup_table_entry(lte_chain_head, w->out_fp);
59 struct lookup_table_entry *prev = lte_chain_head;
60 lte_chain_head = lte_chain_head->next_lte_in_swm;
61 prev->next_lte_in_swm = NULL;
63 off_t xml_data_offset = ftello(w->out_fp);
65 if (lookup_table_offset == -1 || xml_data_offset == -1)
66 return WIMLIB_ERR_WRITE;
67 w->hdr.lookup_table_res_entry.offset = lookup_table_offset;
68 w->hdr.lookup_table_res_entry.size =
69 xml_data_offset - lookup_table_offset;
70 return finish_write(w, WIM_ALL_IMAGES,
71 write_flags | WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE);
74 static int copy_resource_to_swm(struct lookup_table_entry *lte, void *__args)
76 struct split_args *args = (struct split_args*)__args;
77 WIMStruct *w = args->w;
80 /* metadata resources were already written. */
81 if (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
84 if (args->size_remaining < 0 ||
85 (u64)args->size_remaining < lte->resource_entry.size) {
87 /* No space for this resource. Finish the previous swm and
90 ret = finish_swm(w, args->lte_chain_head, args->write_flags);
92 args->lte_chain_tail = NULL;
93 args->lte_chain_head = NULL;
95 sprintf(args->swm_base_name + args->swm_base_name_len, "%d%s",
96 ++args->part_number, args->swm_suffix);
98 w->hdr.part_number = args->part_number;
100 if (args->write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
101 printf("Writing `%s' (%"PRIu64" of %"PRIu64" bytes, "
104 args->total_bytes_written,
106 (double)args->total_bytes_written /
107 (double)args->total_bytes * 100.0);
109 ret = begin_write(w, args->swm_base_name, args->write_flags);
112 args->size_remaining = args->part_size;
114 args->size_remaining -= lte->resource_entry.size;
115 args->total_bytes_written += lte->resource_entry.size;
116 if (args->lte_chain_tail)
117 args->lte_chain_tail->next_lte_in_swm = lte;
119 args->lte_chain_head = lte;
120 args->lte_chain_tail = lte;
121 return copy_resource(lte, w);
124 /* Splits the WIM file @wimfile into multiple parts prefixed by @swm_name with
125 * size at most @part_size. */
126 WIMLIBAPI int wimlib_split(const char *wimfile, const char *swm_name,
127 size_t part_size, int flags)
132 size_t swm_name_len = strlen(swm_name);
133 size_t swm_base_name_len;
134 char name[swm_name_len + 20];
137 struct lookup_table_entry *lte_chain_head = NULL;
138 struct lookup_table_entry *lte_chain_tail = NULL;
139 long size_remaining = part_size;
140 u64 total_bytes_written = 0;
143 ret = wimlib_open_wim(wimfile, flags, &w);
147 total_bytes = wim_info_get_total_bytes(w->wim_info);
149 if (flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY)
150 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
151 if (flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
152 write_flags |= WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
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 swm_suffix = strchr(swm_name, '.');
162 memcpy(name, swm_name, swm_name_len + 1);
164 swm_base_name_len = swm_suffix - swm_name;
166 swm_base_name_len = swm_name_len;
167 name[sizeof(name) - 1] = '\0';
168 swm_suffix = &name[sizeof(name) - 1];
171 if (write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
172 printf("Writing `%s' (%.2f %% done)\n",
174 (double)total_bytes_written /
175 (double)total_bytes * 100.0);
177 w->write_metadata = true;
178 for (int i = 0; i < w->hdr.image_count; i++) {
179 struct lookup_table_entry *metadata_lte;
181 DEBUG("Writing metadata resource %d", i);
183 metadata_lte = w->image_metadata[i].metadata_lte;
184 ret = copy_resource(metadata_lte, w);
187 size_remaining -= metadata_lte->resource_entry.size;
188 total_bytes_written += metadata_lte->resource_entry.size;
190 lte_chain_tail->next_lte_in_swm = metadata_lte;
192 lte_chain_head = metadata_lte;
193 lte_chain_tail = metadata_lte;
195 w->write_metadata = false;
197 struct split_args args = {
199 .swm_base_name = name,
200 .swm_base_name_len = swm_base_name_len,
201 .swm_suffix = swm_suffix,
202 .lte_chain_head = lte_chain_head,
203 .lte_chain_tail = lte_chain_tail,
205 .write_flags = write_flags,
206 .size_remaining = size_remaining,
207 .part_size = part_size,
208 .total_bytes = total_bytes,
209 .total_bytes_written = total_bytes_written,
212 ret = for_lookup_table_entry(w->lookup_table, copy_resource_to_swm, &args);
216 ret = finish_swm(w, args.lte_chain_head, write_flags);
221 /* The swms are all ready now, except the total_parts and part_number
222 * fields in their headers are wrong (we don't know the total parts
223 * until they are all written). Fix them. */
224 int total_parts = args.part_number;
225 for (int i = 1; i <= total_parts; i++) {
230 sprintf(name + swm_base_name_len, "%d", i);
231 p = strcat(name, swm_suffix);
234 FILE *fp = fopen(p, "r+b");
236 ERROR_WITH_ERRNO("Failed to open `%s'", p);
237 return WIMLIB_ERR_OPEN;
241 put_u16(&buf[2], total_parts);
243 if (fseek(fp, 40, SEEK_SET) != 0 ||
244 fwrite(buf, 1, sizeof(buf), fp) != sizeof(buf)
245 || fclose(fp) != 0) {
246 ERROR_WITH_ERRNO("Error overwriting header of `%s'",
248 return WIMLIB_ERR_WRITE;
251 if (write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)