]> wimlib.net Git - wimlib/blob - src/write.c
Cleanup WIM writing
[wimlib] / src / write.c
1 /*
2  * write.c
3  *
4  * Support for writing WIM files; write a WIM file, overwrite a WIM file, write
5  * compressed file resources, etc.
6  */
7
8 /*
9  * Copyright (C) 2010 Carl Thijssen
10  * Copyright (C) 2012 Eric Biggers
11  *
12  * This file is part of wimlib, a library for working with WIM files.
13  *
14  * wimlib is free software; you can redistribute it and/or modify it under the
15  * terms of the GNU General Public License as published by the Free
16  * Software Foundation; either version 3 of the License, or (at your option)
17  * any later version.
18  *
19  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
20  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
21  * A PARTICULAR PURPOSE. See the GNU General Public License for more
22  * details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with wimlib; if not, see http://www.gnu.org/licenses/.
26  */
27
28 #include "wimlib_internal.h"
29 #include "io.h"
30 #include "dentry.h"
31 #include "lookup_table.h"
32 #include "xml.h"
33 #include <unistd.h>
34
35
36 /* Reopens the FILE* for a WIM read-write. */
37 static int reopen_rw(WIMStruct *w)
38 {
39         FILE *fp;
40
41         if (fclose(w->fp) != 0)
42                 ERROR_WITH_ERRNO("Failed to close the file `%s'", w->filename);
43         w->fp = NULL;
44         fp = fopen(w->filename, "r+b");
45         if (!fp) {
46                 ERROR_WITH_ERRNO("Failed to open `%s' for reading and writing",
47                                  w->filename);
48                 return WIMLIB_ERR_OPEN;
49         }
50         w->fp = fp;
51         return 0;
52 }
53
54
55
56 /*
57  * Writes a WIM file to the original file that it was read from, overwriting it.
58  */
59 WIMLIBAPI int wimlib_overwrite(WIMStruct *w, int write_flags)
60 {
61         const char *wimfile_name;
62         size_t wim_name_len;
63         int ret;
64
65         if (!w)
66                 return WIMLIB_ERR_INVALID_PARAM;
67
68         write_flags &= ~WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE;
69
70         wimfile_name = w->filename;
71
72         DEBUG("Replacing WIM file `%s'.", wimfile_name);
73
74         if (!wimfile_name)
75                 return WIMLIB_ERR_NO_FILENAME;
76
77         /* Write the WIM to a temporary file. */
78         /* XXX should the temporary file be somewhere else? */
79         wim_name_len = strlen(wimfile_name);
80         char tmpfile[wim_name_len + 10];
81         memcpy(tmpfile, wimfile_name, wim_name_len);
82         randomize_char_array_with_alnum(tmpfile + wim_name_len, 9);
83         tmpfile[wim_name_len + 9] = '\0';
84
85         ret = wimlib_write(w, tmpfile, WIM_ALL_IMAGES, write_flags);
86         if (ret != 0) {
87                 ERROR("Failed to write the WIM file `%s'", tmpfile);
88                 return ret;
89         }
90
91         DEBUG("Closing original WIM file.");
92         /* Close the original WIM file that was opened for reading. */
93         if (w->fp) {
94                 if (fclose(w->fp) != 0) {
95                         WARNING("Failed to close the file `%s'", wimfile_name);
96                 }
97                 w->fp = NULL;
98         }
99
100         DEBUG("Renaming `%s' to `%s'", tmpfile, wimfile_name);
101
102         /* Rename the new file to the old file .*/
103         if (rename(tmpfile, wimfile_name) != 0) {
104                 ERROR_WITH_ERRNO("Failed to rename `%s' to `%s'",
105                                  tmpfile, wimfile_name);
106                 /* Remove temporary file. */
107                 if (unlink(tmpfile) != 0)
108                         ERROR_WITH_ERRNO("Failed to remove `%s'", tmpfile);
109                 return WIMLIB_ERR_RENAME;
110         }
111
112         return 0;
113 }
114
115 static int check_resource_offset(struct lookup_table_entry *lte, void *arg)
116 {
117         u64 xml_data_offset = *(u64*)arg;
118         if (lte->resource_entry.offset > xml_data_offset) {
119                 ERROR("The following resource is *after* the XML data:");
120                 print_lookup_table_entry(lte);
121                 return WIMLIB_ERR_RESOURCE_ORDER;
122         }
123         return 0;
124 }
125
126 WIMLIBAPI int wimlib_overwrite_xml_and_header(WIMStruct *w, int write_flags)
127 {
128         int ret;
129         FILE *fp;
130         u8 *integrity_table = NULL;
131         off_t xml_end;
132         off_t xml_size;
133         size_t bytes_written;
134
135         DEBUG("Overwriting XML and header of `%s', write_flags = %#x",
136               w->filename, write_flags);
137
138         if (!w->filename)
139                 return WIMLIB_ERR_NO_FILENAME;
140
141         write_flags &= ~WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE;
142
143         /* Make sure that the integrity table (if present) is after the XML
144          * data, and that there are no stream resources, metadata resources, or
145          * lookup tables after the XML data.  Otherwise, these data would be
146          * destroyed by this function. */
147         if (w->hdr.integrity.offset != 0 &&
148             w->hdr.integrity.offset < w->hdr.xml_res_entry.offset) {
149                 ERROR("Didn't expect the integrity table to be before the XML data");
150                 return WIMLIB_ERR_RESOURCE_ORDER;
151         }
152
153         if (w->hdr.lookup_table_res_entry.offset >
154             w->hdr.xml_res_entry.offset) {
155                 ERROR("Didn't expect the lookup table to be after the XML data");
156                 return WIMLIB_ERR_RESOURCE_ORDER;
157         }
158
159         ret = for_lookup_table_entry(w->lookup_table, check_resource_offset,
160                                      &w->hdr.xml_res_entry.offset);
161         if (ret != 0)
162                 return ret;
163
164         ret = reopen_rw(w);
165         if (ret != 0)
166                 return ret;
167
168         fp = w->fp;
169
170         /* The old integrity table is still OK, as the SHA1 message digests in
171          * the integrity table include neither the header nor the XML data.
172          * Save it for later if it exists and an integrity table was required.
173          * */
174         if ((write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY)
175              && w->hdr.integrity.offset != 0)
176         {
177                 DEBUG("Reading existing integrity table.");
178                 integrity_table = MALLOC(w->hdr.integrity.size);
179                 if (!integrity_table)
180                         return WIMLIB_ERR_NOMEM;
181
182                 ret = read_uncompressed_resource(fp, w->hdr.integrity.offset,
183                                                  w->hdr.integrity.original_size,
184                                                  integrity_table);
185                 if (ret != 0)
186                         goto err;
187                 DEBUG("Done reading existing integrity table.");
188         }
189
190         DEBUG("Overwriting XML data.");
191         /* Overwrite the XML data. */
192         if (fseeko(fp, w->hdr.xml_res_entry.offset, SEEK_SET) != 0) {
193                 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" "
194                                  "for XML data", w->hdr.xml_res_entry.offset);
195                 ret = WIMLIB_ERR_WRITE;
196                 goto err;
197         }
198         ret = write_xml_data(w->wim_info, WIM_ALL_IMAGES, fp, 0);
199         if (ret != 0)
200                 goto err;
201
202         DEBUG("Updating XML resource entry.");
203         /* Update the XML resource entry in the WIM header. */
204         xml_end = ftello(fp);
205         if (xml_end == -1) {
206                 ret = WIMLIB_ERR_WRITE;
207                 goto err;
208         }
209         xml_size = xml_end - w->hdr.xml_res_entry.offset;
210         w->hdr.xml_res_entry.size = xml_size;
211         w->hdr.xml_res_entry.original_size = xml_size;
212         /* XML data offset is unchanged. */
213
214         if (write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY) {
215                 DEBUG("Writing integrity table.");
216                 w->hdr.integrity.offset = xml_end;
217                 if (integrity_table) {
218                         /* The existing integrity table was saved. */
219                         bytes_written = fwrite(integrity_table, 1,
220                                                w->hdr.integrity.size, fp);
221                         if (bytes_written != w->hdr.integrity.size) {
222                                 ERROR_WITH_ERRNO("Failed to write integrity "
223                                                  "table");
224                                 ret = WIMLIB_ERR_WRITE;
225                                 goto err;
226                         }
227                         FREE(integrity_table);
228                 } else {
229                         /* There was no existing integrity table, so a new one
230                          * must be calculated. */
231                         ret = write_integrity_table(fp, WIM_HEADER_DISK_SIZE,
232                                         w->hdr.lookup_table_res_entry.offset +
233                                         w->hdr.lookup_table_res_entry.size,
234                                         write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS);
235                         if (ret != 0)
236                                 return ret;
237
238                         off_t end_integrity = ftello(fp);
239                         if (end_integrity == -1)
240                                 return WIMLIB_ERR_WRITE;
241
242                         off_t integrity_size           = end_integrity - xml_end;
243                         w->hdr.integrity.size          = integrity_size;
244                         w->hdr.integrity.original_size = integrity_size;
245                         w->hdr.integrity.flags         = 0;
246                 }
247         } else {
248                 DEBUG("Truncating file to end of XML data.");
249                 /* No integrity table to write.  The file should be truncated
250                  * because it's possible that the old file was longer (due to it
251                  * including an integrity table, or due to its XML data being
252                  * longer) */
253                 if (fflush(fp) != 0) {
254                         ERROR_WITH_ERRNO("Failed to flush stream for file `%s'",
255                                          w->filename);
256                         return WIMLIB_ERR_WRITE;
257                 }
258                 if (ftruncate(fileno(fp), xml_end) != 0) {
259                         ERROR_WITH_ERRNO("Failed to truncate `%s' to %"PRIu64" "
260                                          "bytes", w->filename, xml_end);
261                         return WIMLIB_ERR_WRITE;
262                 }
263                 memset(&w->hdr.integrity, 0, sizeof(struct resource_entry));
264         }
265
266         DEBUG("Overwriting header.");
267         /* Overwrite the header. */
268         if (fseeko(fp, 0, SEEK_SET) != 0) {
269                 ERROR_WITH_ERRNO("Failed to seek to beginning of `%s'",
270                                  w->filename);
271                 return WIMLIB_ERR_WRITE;
272         }
273
274         ret = write_header(&w->hdr, fp);
275         if (ret != 0)
276                 return ret;
277
278         DEBUG("Closing `%s'.", w->filename);
279         if (fclose(fp) != 0) {
280                 ERROR_WITH_ERRNO("Failed to close `%s'", w->filename);
281                 return WIMLIB_ERR_WRITE;
282         }
283         w->fp = NULL;
284         DEBUG("Done.");
285         return 0;
286 err:
287         FREE(integrity_table);
288         return ret;
289 }
290
291
292 /* Write the file resources for the current image. */
293 static int write_file_resources(WIMStruct *w)
294 {
295         DEBUG("Writing file resources for image %u.", w->current_image);
296         return for_dentry_in_tree(wim_root_dentry(w), write_dentry_resources, w);
297 }
298
299 /*
300  * Write the lookup table, xml data, and integrity table, then overwrite the WIM
301  * header.
302  */
303 int finish_write(WIMStruct *w, int image, int write_flags)
304 {
305         off_t lookup_table_offset;
306         off_t xml_data_offset;
307         off_t lookup_table_size;
308         off_t integrity_offset;
309         off_t xml_data_size;
310         off_t end_offset;
311         off_t integrity_size;
312         int ret;
313         struct wim_header hdr;
314         FILE *out = w->out_fp;
315
316         if (!(write_flags & WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE)) {
317                 /* Write the lookup table. */
318                 lookup_table_offset = ftello(out);
319                 if (lookup_table_offset == -1)
320                         return WIMLIB_ERR_WRITE;
321
322                 DEBUG("Writing lookup table (offset %"PRIu64")",
323                       lookup_table_offset);
324                 ret = write_lookup_table(w->lookup_table, out);
325                 if (ret != 0)
326                         return ret;
327         }
328
329         xml_data_offset = ftello(out);
330         if (xml_data_offset == -1)
331                 return WIMLIB_ERR_WRITE;
332
333         /* @hdr will be the header for the new WIM.  First copy all the data
334          * from the header in the WIMStruct; then set all the fields that may
335          * have changed, including the resource entries, boot index, and image
336          * count.  */
337         memcpy(&hdr, &w->hdr, sizeof(struct wim_header));
338         if (!(write_flags & WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE)) {
339                 lookup_table_size = xml_data_offset - lookup_table_offset;
340                 hdr.lookup_table_res_entry.offset = lookup_table_offset;
341                 hdr.lookup_table_res_entry.size = lookup_table_size;
342         }
343         hdr.lookup_table_res_entry.original_size = hdr.lookup_table_res_entry.size;
344         hdr.lookup_table_res_entry.flags = WIM_RESHDR_FLAG_METADATA;
345
346         DEBUG("Writing XML data (offset %"PRIu64")", xml_data_offset);
347         ret = write_xml_data(w->wim_info, image, out,
348                              (write_flags & WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE) ?
349                                 wim_info_get_total_bytes(w->wim_info) : 0);
350         if (ret != 0)
351                 return ret;
352
353         integrity_offset = ftello(out);
354         if (integrity_offset == -1)
355                 return WIMLIB_ERR_WRITE;
356         xml_data_size = integrity_offset - xml_data_offset;
357
358         hdr.xml_res_entry.offset                 = xml_data_offset;
359         hdr.xml_res_entry.size                   = xml_data_size;
360         hdr.xml_res_entry.original_size          = xml_data_size;
361         hdr.xml_res_entry.flags                  = 0;
362
363         if (write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY) {
364                 ret = write_integrity_table(out, WIM_HEADER_DISK_SIZE,
365                                             xml_data_offset,
366                                             write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS);
367                 if (ret != 0)
368                         return ret;
369                 end_offset = ftello(out);
370                 if (end_offset == -1)
371                         return WIMLIB_ERR_WRITE;
372                 integrity_size              = end_offset - integrity_offset;
373                 hdr.integrity.offset        = integrity_offset;
374                 hdr.integrity.size          = integrity_size;
375                 hdr.integrity.original_size = integrity_size;
376         } else {
377                 hdr.integrity.offset        = 0;
378                 hdr.integrity.size          = 0;
379                 hdr.integrity.original_size = 0;
380         }
381         hdr.integrity.flags = 0;
382
383         DEBUG("Updating WIM header.");
384
385         /*
386          * In the WIM header, there is room for the resource entry for a
387          * metadata resource labeled as the "boot metadata".  This entry should
388          * be zeroed out if there is no bootable image (boot_idx 0).  Otherwise,
389          * it should be a copy of the resource entry for the image that is
390          * marked as bootable.  This is not well documented...
391          */
392         if (hdr.boot_idx == 0 || !w->image_metadata
393                         || (image != WIM_ALL_IMAGES && image != hdr.boot_idx)) {
394                 memset(&hdr.boot_metadata_res_entry, 0,
395                        sizeof(struct resource_entry));
396         } else {
397                 memcpy(&hdr.boot_metadata_res_entry,
398                        &w->image_metadata[
399                           hdr.boot_idx - 1].metadata_lte->output_resource_entry,
400                        sizeof(struct resource_entry));
401         }
402
403         /* Set image count and boot index correctly for single image writes */
404         if (image != WIM_ALL_IMAGES) {
405                 hdr.image_count = 1;
406                 if (hdr.boot_idx == image)
407                         hdr.boot_idx = 1;
408                 else
409                         hdr.boot_idx = 0;
410         }
411
412
413         if (fseeko(out, 0, SEEK_SET) != 0)
414                 return WIMLIB_ERR_WRITE;
415
416         return write_header(&hdr, out);
417 }
418
419 /* Open file stream and write dummy header for WIM. */
420 int begin_write(WIMStruct *w, const char *path, int write_flags)
421 {
422         const char *mode;
423         DEBUG("Opening `%s' for new WIM", path);
424
425         /* checking the integrity requires going back over the file to read it.
426          * XXX
427          * (It also would be possible to keep a running sha1sum as the file is
428          * written-- this would be faster, but a bit more complicated) */
429         if (write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY)
430                 mode = "w+b";
431         else
432                 mode = "wb";
433
434         w->out_fp = fopen(path, mode);
435         if (!w->out_fp) {
436                 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
437                                  path);
438                 return WIMLIB_ERR_OPEN;
439         }
440
441         /* Write dummy header. It will be overwritten later. */
442         return write_header(&w->hdr, w->out_fp);
443 }
444
445 /* Writes a stand-alone WIM to a file.  */
446 WIMLIBAPI int wimlib_write(WIMStruct *w, const char *path,
447                            int image, int write_flags)
448 {
449         int ret;
450
451         if (!w || !path)
452                 return WIMLIB_ERR_INVALID_PARAM;
453
454         write_flags &= ~WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE;
455
456         if (image != WIM_ALL_IMAGES &&
457              (image < 1 || image > w->hdr.image_count))
458                 return WIMLIB_ERR_INVALID_IMAGE;
459
460
461         if (w->hdr.total_parts != 1) {
462                 ERROR("Cannot call wimlib_write() on part of a split WIM");
463                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
464         }
465
466         if (image == WIM_ALL_IMAGES)
467                 DEBUG("Writing all images to `%s'.", path);
468         else
469                 DEBUG("Writing image %d to `%s'.", image, path);
470
471         ret = begin_write(w, path, write_flags);
472         if (ret != 0)
473                 goto out;
474
475         for_lookup_table_entry(w->lookup_table, lte_zero_out_refcnt, NULL);
476
477         w->write_flags = write_flags;
478
479         ret = for_image(w, image, write_file_resources);
480         if (ret != 0) {
481                 ERROR("Failed to write WIM file resources to `%s'", path);
482                 goto out;
483         }
484
485         ret = for_image(w, image, write_metadata_resource);
486
487         if (ret != 0) {
488                 ERROR("Failed to write WIM image metadata to `%s'", path);
489                 goto out;
490         }
491
492         ret = finish_write(w, image, write_flags);
493
494 out:
495         DEBUG("Closing output file.");
496         if (w->out_fp != NULL) {
497                 if (fclose(w->out_fp) != 0) {
498                         ERROR_WITH_ERRNO("Failed to close the file `%s'", path);
499                         ret = WIMLIB_ERR_WRITE;
500                 }
501                 w->out_fp = NULL;
502         }
503         return ret;
504 }