4 * Split a WIM file into parts.
6 * Copyright (C) 2012 Eric Biggers
8 * wimlib - Library for working with WIM files
10 * This library is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU Lesser General Public License as published by the Free
12 * Software Foundation; either version 2.1 of the License, or (at your option) any
15 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
17 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License along
20 * with this library; if not, write to the Free Software Foundation, Inc., 59
21 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wimlib_internal.h"
25 #include "lookup_table.h"
32 size_t swm_base_name_len;
33 const char *swm_suffix;
34 struct lookup_table_entry *lte_chain_head;
35 struct lookup_table_entry *lte_chain_tail;
41 u64 total_bytes_written;
44 static int finish_swm(WIMStruct *w, struct lookup_table_entry *lte_chain_head,
47 off_t lookup_table_offset = ftello(w->out_fp);
50 DEBUG("Writing lookup table for SWM (offset %"PRIu64")\n",
53 while (lte_chain_head != NULL) {
54 ret = write_lookup_table_entry(lte_chain_head, w->out_fp);
57 struct lookup_table_entry *prev = lte_chain_head;
58 lte_chain_head = prev->next_lte_in_swm;
59 prev->next_lte_in_swm = NULL;
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 ret = finish_write(w, WIM_ALL_IMAGES, write_flags, 0);
72 ret = fclose(w->out_fp);
74 ret = WIMLIB_ERR_WRITE;
79 static int copy_resource_to_swm(struct lookup_table_entry *lte, void *__args)
81 struct args *args = (struct args*)__args;
82 WIMStruct *w = args->w;
83 FILE *out_fp = w->out_fp;
86 /* metadata resources were already written. */
87 if (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
90 if (args->size_remaining < 0 ||
91 (u64)args->size_remaining < lte->resource_entry.size) {
93 /* No space for this resource. Finish the previous swm and
96 ret = finish_swm(w, args->lte_chain_head, args->write_flags);
98 args->lte_chain_tail = NULL;
99 args->lte_chain_head = NULL;
101 sprintf(args->swm_base_name + args->swm_base_name_len, "%d",
102 ++args->part_number);
103 strcat(args->swm_base_name, args->swm_suffix);
105 w->hdr.part_number = args->part_number;
107 if (args->write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
108 printf("Writing `%s' (%"PRIu64" of %"PRIu64" bytes, "
111 args->total_bytes_written,
113 (double)args->total_bytes_written /
114 (double)args->total_bytes * 100.0);
116 ret = begin_write(w, args->swm_base_name, args->write_flags);
119 args->size_remaining = args->part_size;
121 args->size_remaining -= lte->resource_entry.size;
122 args->total_bytes_written += lte->resource_entry.size;
123 if (args->lte_chain_tail)
124 args->lte_chain_tail->next_lte_in_swm = lte;
126 args->lte_chain_head = lte;
127 args->lte_chain_tail = lte;
128 return copy_resource(lte, w);
131 /* Splits the WIM file @wimfile into multiple parts prefixed by @swm_name with
132 * size at most @part_size. */
133 WIMLIBAPI int wimlib_split(const char *wimfile, const char *swm_name,
134 size_t part_size, int flags)
139 size_t swm_name_len = strlen(swm_name);
140 size_t swm_base_name_len;
141 char name[swm_name_len + 20];
144 struct lookup_table_entry *lte_chain_head = NULL;
145 struct lookup_table_entry *lte_chain_tail = NULL;
146 long size_remaining = part_size;
147 u64 total_bytes_written = 0;
150 ret = wimlib_open_wim(wimfile, flags, &w);
154 total_bytes = wim_info_get_total_bytes(w->wim_info);
156 if (flags & WIMLIB_OPEN_FLAG_CHECK_INTEGRITY)
157 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
158 if (flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
159 write_flags |= WIMLIB_WRITE_FLAG_SHOW_PROGRESS;
161 w->hdr.flags |= WIM_HDR_FLAG_SPANNED;
163 randomize_byte_array(w->hdr.guid, WIM_GID_LEN);
164 ret = begin_write(w, swm_name, write_flags);
168 swm_suffix = strchr(swm_name, '.');
169 memcpy(name, swm_name, swm_name_len + 1);
171 swm_base_name_len = swm_suffix - swm_name;
173 swm_base_name_len = swm_name_len;
174 name[sizeof(name) - 1] = '\0';
175 swm_suffix = &name[sizeof(name) - 1];
178 if (write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
179 printf("Writing `%s' (%.2f %% done)\n",
181 (double)total_bytes_written /
182 (double)total_bytes * 100.0);
184 w->write_metadata = true;
185 for (int i = 0; i < w->hdr.image_count; i++) {
187 struct lookup_table_entry *metadata_lte;
189 metadata_lte = w->image_metadata[i].lookup_table_entry;
190 ret = copy_resource(metadata_lte, w);
193 size_remaining -= metadata_lte->resource_entry.size;
194 total_bytes_written += metadata_lte->resource_entry.size;
196 lte_chain_tail->next_lte_in_swm = metadata_lte;
198 lte_chain_head = metadata_lte;
199 lte_chain_tail = metadata_lte;
201 w->write_metadata = false;
205 .swm_base_name = name,
206 .swm_base_name_len = swm_base_name_len,
207 .swm_suffix = swm_suffix,
208 .lte_chain_head = lte_chain_head,
209 .lte_chain_tail = lte_chain_tail,
211 .write_flags = write_flags,
212 .size_remaining = size_remaining,
213 .part_size = part_size,
214 .total_bytes = total_bytes,
215 .total_bytes_written = total_bytes_written,
218 ret = for_lookup_table_entry(w->lookup_table, copy_resource_to_swm, &args);
222 ret = finish_swm(w, args.lte_chain_head, write_flags);
227 /* The swms are all ready now, except the total_parts and part_number
228 * fields in their headers are wrong (we don't know the total parts
229 * until they are all written). Fix them. */
230 int total_parts = args.part_number;
231 for (int i = 1; i <= total_parts; i++) {
236 sprintf(name + swm_base_name_len, "%d", i);
237 p = strcat(name, swm_suffix);
240 FILE *fp = fopen(p, "r+b");
242 ERROR("Failed to open `%s': %m\n", p);
243 return WIMLIB_ERR_OPEN;
247 put_u16(buf + 2, total_parts);
249 if (fseek(fp, 40, SEEK_SET) != 0 ||
250 fwrite(buf, 1, sizeof(buf), fp) != sizeof(buf)
251 || fclose(fp) != 0) {
252 ERROR("Error overwriting header of `%s': %m\n", name);
253 return WIMLIB_ERR_WRITE;
256 if (write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)