Close out_fp in finish_write()
[wimlib] / src / split.c
1 /*
2  * split.c
3  *
4  * Split a WIM file into parts.
5  */
6
7 /*
8  * Copyright (C) 2012 Eric Biggers
9  *
10  * This file is part of wimlib, a library for working with WIM files.
11  *
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)
15  * any later version.
16  *
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
20  * details.
21  *
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/.
24  */
25
26 #include "wimlib_internal.h"
27 #include "lookup_table.h"
28 #include "xml.h"
29 #include "io.h"
30
31 struct split_args {
32         WIMStruct *w;
33         char *swm_base_name;
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;
38         int    part_number;
39         int    write_flags;
40         long   size_remaining;
41         size_t part_size;
42         u64    total_bytes;
43         u64    total_bytes_written;
44 };
45
46 static int finish_swm(WIMStruct *w, struct lookup_table_entry *lte_chain_head,
47                       int write_flags)
48 {
49         off_t lookup_table_offset = ftello(w->out_fp);
50         int ret;
51
52         DEBUG("Writing lookup table for SWM (offset %"PRIu64")",
53                         lookup_table_offset);
54
55         while (lte_chain_head != NULL) {
56                 ret = write_lookup_table_entry(lte_chain_head, w->out_fp);
57                 if (ret != 0)
58                         return ret;
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;
62         }
63         off_t xml_data_offset = ftello(w->out_fp);
64
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);
72 }
73
74 static int copy_resource_to_swm(struct lookup_table_entry *lte, void *__args)
75 {
76         struct split_args *args = (struct split_args*)__args;
77         WIMStruct *w = args->w;
78         int ret;
79
80         /* metadata resources were already written. */
81         if (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
82                 return 0;
83
84         if (args->size_remaining < 0 ||
85                         (u64)args->size_remaining < lte->resource_entry.size) {
86
87                 /* No space for this resource.  Finish the previous swm and
88                  * start a new one. */
89
90                 ret = finish_swm(w, args->lte_chain_head, args->write_flags);
91
92                 args->lte_chain_tail = NULL;
93                 args->lte_chain_head = NULL;
94
95                 sprintf(args->swm_base_name + args->swm_base_name_len, "%d%s",
96                         ++args->part_number, args->swm_suffix);
97
98                 w->hdr.part_number = args->part_number;
99
100                 if (args->write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
101                         printf("Writing `%s' (%"PRIu64" of %"PRIu64" bytes, "
102                                "%.0f%% done)\n",
103                                args->swm_base_name,
104                                args->total_bytes_written,
105                                args->total_bytes,
106                                (double)args->total_bytes_written /
107                                    (double)args->total_bytes * 100.0);
108
109                 ret = begin_write(w, args->swm_base_name, args->write_flags);
110                 if (ret != 0)
111                         return ret;
112                 args->size_remaining = args->part_size;
113         }
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;
118         else
119                 args->lte_chain_head = lte;
120         args->lte_chain_tail = lte;
121         return copy_resource(lte, w);
122 }
123
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)
128 {
129         int ret;
130         WIMStruct *w;
131         int write_flags = 0;
132         size_t swm_name_len = strlen(swm_name);
133         size_t swm_base_name_len;
134         char name[swm_name_len + 20];
135         char *swm_suffix;
136
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;
141         u64 total_bytes;
142
143         ret = wimlib_open_wim(wimfile, flags, &w);
144         if (ret != 0)
145                 return ret;
146
147         total_bytes = wim_info_get_total_bytes(w->wim_info);
148
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;
153
154         w->hdr.flags |= WIM_HDR_FLAG_SPANNED;
155         w->hdr.boot_idx = 0;
156         randomize_byte_array(w->hdr.guid, WIM_GID_LEN);
157         ret = begin_write(w, swm_name, write_flags);
158         if (ret != 0)
159                 return ret;
160
161         swm_suffix = strchr(swm_name, '.');
162         memcpy(name, swm_name, swm_name_len + 1);
163         if (swm_suffix) {
164                 swm_base_name_len = swm_suffix - swm_name;
165         } else {
166                 swm_base_name_len = swm_name_len;
167                 name[sizeof(name) - 1] = '\0';
168                 swm_suffix = &name[sizeof(name) - 1];
169         }
170
171         if (write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
172                 printf("Writing `%s' (%.2f %% done)\n",
173                         swm_name,
174                         (double)total_bytes_written /
175                                 (double)total_bytes * 100.0);
176
177         w->write_metadata = true;
178         for (int i = 0; i < w->hdr.image_count; i++) {
179                 struct lookup_table_entry *metadata_lte;
180
181                 DEBUG("Writing metadata resource %d", i);
182
183                 metadata_lte = w->image_metadata[i].metadata_lte;
184                 ret = copy_resource(metadata_lte, w);
185                 if (ret != 0)
186                         return ret;
187                 size_remaining -= metadata_lte->resource_entry.size;
188                 total_bytes_written += metadata_lte->resource_entry.size;
189                 if (lte_chain_tail)
190                         lte_chain_tail->next_lte_in_swm = metadata_lte;
191                 else
192                         lte_chain_head = metadata_lte;
193                 lte_chain_tail = metadata_lte;
194         }
195         w->write_metadata = false;
196
197         struct split_args args = {
198                 .w                 = w,
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,
204                 .part_number       = 1,
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,
210         };
211
212         ret = for_lookup_table_entry(w->lookup_table, copy_resource_to_swm, &args);
213         if (ret != 0)
214                 return ret;
215
216         ret = finish_swm(w, args.lte_chain_head, write_flags);
217         if (ret != 0)
218                 return ret;
219
220
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++) {
226                 const char *p;
227                 if (i == 1) {
228                         p = swm_name;
229                 } else {
230                         sprintf(name + swm_base_name_len, "%d", i);
231                         p = strcat(name, swm_suffix);
232                 }
233
234                 FILE *fp = fopen(p, "r+b");
235                 if (!fp) {
236                         ERROR_WITH_ERRNO("Failed to open `%s'", p);
237                         return WIMLIB_ERR_OPEN;
238                 }
239                 u8 buf[4];
240                 put_u16(&buf[0], i);
241                 put_u16(&buf[2], total_parts);
242
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'",
247                                          name);
248                         return WIMLIB_ERR_WRITE;
249                 }
250         }
251         if (write_flags & WIMLIB_OPEN_FLAG_SHOW_PROGRESS)
252                 puts("Done!");
253         wimlib_free(w);
254         return 0;
255 }