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