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 Lesser General Public License as published by the Free
14 * Software Foundation; either version 2.1 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 Lesser General Public License for more
22 * You should have received a copy of the GNU Lesser 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 print_lookup_table_entry(lte_chain_head);
58 ret = write_lookup_table_entry(lte_chain_head, w->out_fp);
61 struct lookup_table_entry *prev = lte_chain_head;
62 lte_chain_head = lte_chain_head->next_lte_in_swm;
63 prev->next_lte_in_swm = NULL;
65 off_t xml_data_offset = ftello(w->out_fp);
67 if (lookup_table_offset == -1 || xml_data_offset == -1)
68 return WIMLIB_ERR_WRITE;
69 w->hdr.lookup_table_res_entry.offset = lookup_table_offset;
70 w->hdr.lookup_table_res_entry.size =
71 xml_data_offset - lookup_table_offset;
72 ret = finish_write(w, WIM_ALL_IMAGES, write_flags, 0);
76 ret = fclose(w->out_fp);
78 ret = WIMLIB_ERR_WRITE;
83 static int copy_resource_to_swm(struct lookup_table_entry *lte, void *__args)
85 struct args *args = (struct args*)__args;
86 WIMStruct *w = args->w;
89 /* metadata resources were already written. */
90 if (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
93 if (args->size_remaining < 0 ||
94 (u64)args->size_remaining < lte->resource_entry.size) {
96 /* No space for this resource. Finish the previous swm and
99 ret = finish_swm(w, args->lte_chain_head, args->write_flags);
101 args->lte_chain_tail = NULL;
102 args->lte_chain_head = NULL;
104 sprintf(args->swm_base_name + args->swm_base_name_len, "%d",
105 ++args->part_number);
106 strcat(args->swm_base_name, args->swm_suffix);
108 w->hdr.part_number = args->part_number;
110 if (args->write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
111 printf("Writing `%s' (%"PRIu64" of %"PRIu64" bytes, "
114 args->total_bytes_written,
116 (double)args->total_bytes_written /
117 (double)args->total_bytes * 100.0);
119 ret = begin_write(w, args->swm_base_name, args->write_flags);
122 args->size_remaining = args->part_size;
124 args->size_remaining -= lte->resource_entry.size;
125 args->total_bytes_written += lte->resource_entry.size;
126 if (args->lte_chain_tail)
127 args->lte_chain_tail->next_lte_in_swm = lte;
129 args->lte_chain_head = lte;
130 args->lte_chain_tail = lte;
131 return copy_resource(lte, w);
134 /* Splits the WIM file @wimfile into multiple parts prefixed by @swm_name with
135 * size at most @part_size. */
136 WIMLIBAPI int wimlib_split(const char *wimfile, const char *swm_name,
137 size_t part_size, int flags)
142 size_t swm_name_len = strlen(swm_name);
143 size_t swm_base_name_len;
144 char name[swm_name_len + 20];
147 struct lookup_table_entry *lte_chain_head = NULL;
148 struct lookup_table_entry *lte_chain_tail = NULL;
149 long size_remaining = part_size;
150 u64 total_bytes_written = 0;
153 ret = wimlib_open_wim(wimfile, flags, &w);
157 total_bytes = wim_info_get_total_bytes(w->wim_info);
159 if (flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY)
160 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
161 if (flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
162 write_flags |= WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
164 w->hdr.flags |= WIM_HDR_FLAG_SPANNED;
166 randomize_byte_array(w->hdr.guid, WIM_GID_LEN);
167 ret = begin_write(w, swm_name, write_flags);
171 swm_suffix = strchr(swm_name, '.');
172 memcpy(name, swm_name, swm_name_len + 1);
174 swm_base_name_len = swm_suffix - swm_name;
176 swm_base_name_len = swm_name_len;
177 name[sizeof(name) - 1] = '\0';
178 swm_suffix = &name[sizeof(name) - 1];
181 if (write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
182 printf("Writing `%s' (%.2f %% done)\n",
184 (double)total_bytes_written /
185 (double)total_bytes * 100.0);
187 w->write_metadata = true;
188 for (int i = 0; i < w->hdr.image_count; i++) {
189 struct lookup_table_entry *metadata_lte;
191 DEBUG("Writing metadata resource %d", i);
193 metadata_lte = w->image_metadata[i].metadata_lte;
194 ret = copy_resource(metadata_lte, w);
197 size_remaining -= metadata_lte->resource_entry.size;
198 total_bytes_written += metadata_lte->resource_entry.size;
200 lte_chain_tail->next_lte_in_swm = metadata_lte;
202 lte_chain_head = metadata_lte;
203 lte_chain_tail = metadata_lte;
205 w->write_metadata = false;
209 .swm_base_name = name,
210 .swm_base_name_len = swm_base_name_len,
211 .swm_suffix = swm_suffix,
212 .lte_chain_head = lte_chain_head,
213 .lte_chain_tail = lte_chain_tail,
215 .write_flags = write_flags,
216 .size_remaining = size_remaining,
217 .part_size = part_size,
218 .total_bytes = total_bytes,
219 .total_bytes_written = total_bytes_written,
222 ret = for_lookup_table_entry(w->lookup_table, copy_resource_to_swm, &args);
226 ret = finish_swm(w, args.lte_chain_head, write_flags);
231 /* The swms are all ready now, except the total_parts and part_number
232 * fields in their headers are wrong (we don't know the total parts
233 * until they are all written). Fix them. */
234 int total_parts = args.part_number;
235 for (int i = 1; i <= total_parts; i++) {
240 sprintf(name + swm_base_name_len, "%d", i);
241 p = strcat(name, swm_suffix);
244 FILE *fp = fopen(p, "r+b");
246 ERROR_WITH_ERRNO("Failed to open `%s'", p);
247 return WIMLIB_ERR_OPEN;
251 put_u16(&buf[2], total_parts);
253 if (fseek(fp, 40, SEEK_SET) != 0 ||
254 fwrite(buf, 1, sizeof(buf), fp) != sizeof(buf)
255 || fclose(fp) != 0) {
256 ERROR_WITH_ERRNO("Error overwriting header of `%s'",
258 return WIMLIB_ERR_WRITE;
261 if (write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)