Replace rename()
[wimlib] / src / split.c
1 /*
2  * split.c
3  *
4  * Split a WIM file into parts.
5  */
6
7 /*
8  * Copyright (C) 2012, 2013 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 "buffer_io.h"
30
31 struct split_args {
32         WIMStruct *w;
33         mbchar *swm_base_name;
34         size_t swm_base_name_len;
35         const mbchar *swm_suffix;
36         struct list_head lte_list;
37         int cur_part_number;
38         int write_flags;
39         long size_remaining;
40         size_t part_size;
41         wimlib_progress_func_t progress_func;
42         union wimlib_progress_info progress;
43 };
44
45 static int
46 finish_swm(WIMStruct *w, struct list_head *lte_list,
47            int write_flags, wimlib_progress_func_t progress_func)
48 {
49         off_t lookup_table_offset = ftello(w->out_fp);
50         int ret;
51         struct wim_lookup_table_entry *lte;
52
53         list_for_each_entry(lte, lte_list, staging_list) {
54                 ret = write_lookup_table_entry(lte, w->out_fp);
55                 if (ret != 0)
56                         return ret;
57         }
58
59         off_t xml_data_offset = ftello(w->out_fp);
60
61         if (lookup_table_offset == -1 || xml_data_offset == -1)
62                 return WIMLIB_ERR_WRITE;
63         w->hdr.lookup_table_res_entry.offset = lookup_table_offset;
64         w->hdr.lookup_table_res_entry.size =
65                                 xml_data_offset - lookup_table_offset;
66         w->hdr.lookup_table_res_entry.original_size =
67                                 xml_data_offset - lookup_table_offset;
68         w->hdr.lookup_table_res_entry.flags = WIM_RESHDR_FLAG_METADATA;
69         return finish_write(w, WIMLIB_ALL_IMAGES,
70                             write_flags | WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE,
71                             progress_func);
72 }
73
74 static int
75 copy_resource_to_swm(struct wim_lookup_table_entry *lte, void *__args)
76 {
77         struct split_args *args = (struct split_args*)__args;
78         WIMStruct *w = args->w;
79         int ret;
80
81         /* metadata resources were already written. */
82         if (lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA)
83                 return 0;
84
85         if (args->size_remaining < 0 ||
86                         (u64)args->size_remaining < lte->resource_entry.size) {
87
88                 /* No space for this resource.  Finish the previous swm and
89                  * start a new one. */
90
91                 ret = finish_swm(w, &args->lte_list, args->write_flags,
92                                  args->progress_func);
93                 if (ret != 0)
94                         return ret;
95
96                 if (args->progress_func) {
97                         args->progress_func(WIMLIB_PROGRESS_MSG_SPLIT_END_PART,
98                                             &args->progress);
99                 }
100
101                 INIT_LIST_HEAD(&args->lte_list);
102                 args->cur_part_number++;
103
104                 sprintf(args->swm_base_name + args->swm_base_name_len, "%d%s",
105                         args->cur_part_number, args->swm_suffix);
106
107                 w->hdr.part_number = args->cur_part_number;
108
109                 if (args->progress_func) {
110                         args->progress.split.cur_part_number = args->cur_part_number;
111                         args->progress_func(WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART,
112                                             &args->progress);
113                 }
114
115                 ret = begin_write(w, args->swm_base_name, args->write_flags);
116                 if (ret != 0)
117                         return ret;
118                 args->size_remaining = args->part_size;
119         }
120         args->size_remaining -= lte->resource_entry.size;
121         args->progress.split.completed_bytes += lte->resource_entry.size;
122         list_add(&lte->staging_list, &args->lte_list);
123         return copy_resource(lte, w);
124 }
125
126 /* Splits the WIM file @w into multiple parts prefixed by @swm_name with size at
127  * most @part_size bytes. */
128 WIMLIBAPI int
129 wimlib_split(WIMStruct *w, const mbchar *swm_name,
130              size_t part_size, int write_flags,
131              wimlib_progress_func_t progress_func)
132 {
133         int ret;
134         struct wim_header hdr_save;
135         struct split_args args;
136         const mbchar *swm_suffix;
137         size_t swm_name_len;
138         size_t swm_base_name_len;
139
140         if (!swm_name || part_size == 0)
141                 return WIMLIB_ERR_INVALID_PARAM;
142
143         if (w->hdr.total_parts != 1)
144                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
145
146         write_flags &= WIMLIB_WRITE_MASK_PUBLIC;
147
148         swm_name_len = strlen(swm_name);
149         mbchar swm_base_name[swm_name_len + 20];
150
151         memcpy(&hdr_save, &w->hdr, sizeof(struct wim_header));
152         w->hdr.flags |= WIM_HDR_FLAG_SPANNED;
153         w->hdr.boot_idx = 0;
154         randomize_byte_array(w->hdr.guid, WIM_GID_LEN);
155         ret = begin_write(w, swm_name, write_flags);
156         if (ret != 0)
157                 goto out;
158
159         memcpy(swm_base_name, swm_name, swm_name_len + 1);
160
161         swm_suffix = strchr(swm_name, '.');
162         if (swm_suffix) {
163                 swm_base_name_len = swm_suffix - swm_name;
164         } else {
165                 swm_base_name_len = swm_name_len;
166                 swm_base_name[sizeof(swm_base_name) - 1] = '\0';
167                 swm_suffix = &swm_base_name[sizeof(swm_base_name) - 1];
168         }
169
170         args.w                              = w;
171         args.swm_base_name                  = swm_base_name;
172         args.swm_base_name_len              = swm_base_name_len;
173         args.swm_suffix                     = swm_suffix;
174         INIT_LIST_HEAD(&args.lte_list);
175         args.cur_part_number                = 1;
176         args.write_flags                    = write_flags;
177         args.size_remaining                 = part_size;
178         args.part_size                      = part_size;
179         args.progress_func                  = progress_func;
180         args.progress.split.total_bytes     = lookup_table_total_stream_size(w->lookup_table);
181         args.progress.split.cur_part_number = 1;
182         args.progress.split.completed_bytes = 0;
183         args.progress.split.part_name       = swm_base_name;
184
185         if (progress_func) {
186                 progress_func(WIMLIB_PROGRESS_MSG_SPLIT_BEGIN_PART,
187                               &args.progress);
188         }
189
190         w->write_metadata = true;
191         for (int i = 0; i < w->hdr.image_count; i++) {
192                 struct wim_lookup_table_entry *metadata_lte;
193                 metadata_lte = w->image_metadata[i].metadata_lte;
194                 ret = copy_resource(metadata_lte, w);
195                 if (ret != 0)
196                         goto out;
197                 args.size_remaining -= metadata_lte->resource_entry.size;
198                 args.progress.split.completed_bytes += metadata_lte->resource_entry.size;
199                 list_add(&metadata_lte->staging_list, &args.lte_list);
200         }
201         w->write_metadata = false;
202
203         ret = for_lookup_table_entry(w->lookup_table,
204                                      copy_resource_to_swm, &args);
205         if (ret != 0)
206                 goto out;
207
208         ret = finish_swm(w, &args.lte_list, write_flags, progress_func);
209         if (ret != 0)
210                 goto out;
211
212         if (progress_func) {
213                 progress_func(WIMLIB_PROGRESS_MSG_SPLIT_END_PART,
214                               &args.progress);
215         }
216
217         /* The swms are all ready now, except the total_parts and part_number
218          * fields in their headers are wrong (since we don't know the total
219          * parts until they are all written).  Fix them. */
220         int total_parts = args.cur_part_number;
221         for (int i = 1; i <= total_parts; i++) {
222                 const mbchar *part_name;
223                 if (i == 1) {
224                         part_name = swm_name;
225                 } else {
226                         sprintf(swm_base_name + swm_base_name_len, "%d%s",
227                                 i, swm_suffix);
228                         part_name = swm_base_name;
229                 }
230
231                 FILE *fp = fopen(part_name, "r+b");
232                 if (!fp) {
233                         ERROR_WITH_ERRNO("Failed to open `%s'", part_name);
234                         ret = WIMLIB_ERR_OPEN;
235                         goto out;
236                 }
237                 u8 buf[4];
238                 put_u16(&buf[0], i);
239                 put_u16(&buf[2], total_parts);
240
241                 if (fseek(fp, 40, SEEK_SET) != 0 ||
242                     fwrite(buf, 1, sizeof(buf), fp) != sizeof(buf) ||
243                     fclose(fp) != 0)
244                 {
245                         ERROR_WITH_ERRNO("Error overwriting header of `%s'",
246                                          part_name);
247                         ret = WIMLIB_ERR_WRITE;
248                         break;
249                 }
250         }
251 out:
252         close_wim_writable(w);
253         memcpy(&w->hdr, &hdr_save, sizeof(struct wim_header));
254         return ret;
255 }