]> wimlib.net Git - wimlib/blob - src/write.c
Remove more trailing whitespace
[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 /* Reopens the FILE* for a WIM read-write. */
36 static int reopen_rw(WIMStruct *w)
37 {
38         FILE *fp;
39
40         if (fclose(w->fp) != 0)
41                 ERROR_WITH_ERRNO("Failed to close the file `%s'", w->filename);
42         fp = fopen(w->filename, "r+b");
43         if (!fp) {
44                 ERROR_WITH_ERRNO("Failed to open `%s' for reading and writing",
45                                  w->filename);
46                 return WIMLIB_ERR_OPEN;
47         }
48         w->fp = fp;
49         return 0;
50 }
51
52
53
54 /*
55  * Writes a WIM file to the original file that it was read from, overwriting it.
56  */
57 WIMLIBAPI int wimlib_overwrite(WIMStruct *w, int flags)
58 {
59         const char *wimfile_name;
60         size_t wim_name_len;
61         int ret;
62
63         if (!w)
64                 return WIMLIB_ERR_INVALID_PARAM;
65
66         wimfile_name = w->filename;
67
68         DEBUG("Replacing WIM file `%s'.", wimfile_name);
69
70         if (!wimfile_name)
71                 return WIMLIB_ERR_NO_FILENAME;
72
73         /* Write the WIM to a temporary file. */
74         /* XXX should the temporary file be somewhere else? */
75         wim_name_len = strlen(wimfile_name);
76         char tmpfile[wim_name_len + 10];
77         memcpy(tmpfile, wimfile_name, wim_name_len);
78         randomize_char_array_with_alnum(tmpfile + wim_name_len, 9);
79         tmpfile[wim_name_len + 9] = '\0';
80
81         ret = wimlib_write(w, tmpfile, WIM_ALL_IMAGES, flags);
82         if (ret != 0) {
83                 ERROR("Failed to write the WIM file `%s'", tmpfile);
84                 return ret;
85         }
86
87         DEBUG("Closing original WIM file.");
88         /* Close the original WIM file that was opened for reading. */
89         if (w->fp) {
90                 if (fclose(w->fp) != 0) {
91                         WARNING("Failed to close the file `%s'", wimfile_name);
92                 }
93                 w->fp = NULL;
94         }
95
96         DEBUG("Renaming `%s' to `%s'", tmpfile, wimfile_name);
97
98         /* Rename the new file to the old file .*/
99         if (rename(tmpfile, wimfile_name) != 0) {
100                 ERROR_WITH_ERRNO("Failed to rename `%s' to `%s'",
101                                  tmpfile, wimfile_name);
102                 /* Remove temporary file. */
103                 if (unlink(tmpfile) != 0)
104                         ERROR_WITH_ERRNO("Failed to remove `%s'", tmpfile);
105                 return WIMLIB_ERR_RENAME;
106         }
107
108         return 0;
109 }
110
111
112 WIMLIBAPI int wimlib_overwrite_xml_and_header(WIMStruct *w, int flags)
113 {
114         int ret;
115         FILE *fp;
116         u8 *integrity_table = NULL;
117         off_t xml_end;
118         off_t xml_size;
119         size_t bytes_written;
120
121         DEBUG("Overwriting XML and header of `%s', flags = %d",
122               w->filename, flags);
123         if (!w->filename)
124                 return WIMLIB_ERR_NO_FILENAME;
125
126         ret = reopen_rw(w);
127         if (ret != 0)
128                 return ret;
129
130         fp = w->fp;
131
132         /* The old integrity table is still OK, as the SHA1 message digests in
133          * the integrity table include neither the header nor the XML data.
134          * Save it for later if it exists and an integrity table was required.
135          * */
136         if (flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY &&
137                         w->hdr.integrity.offset != 0) {
138                 DEBUG("Reading existing integrity table.");
139                 integrity_table = MALLOC(w->hdr.integrity.size);
140                 if (!integrity_table)
141                         return WIMLIB_ERR_NOMEM;
142
143                 ret = read_uncompressed_resource(fp, w->hdr.integrity.offset,
144                                                  w->hdr.integrity.original_size,
145                                                  integrity_table);
146                 if (ret != 0)
147                         goto err;
148                 DEBUG("Done reading existing integrity table.");
149         }
150
151         DEBUG("Overwriting XML data.");
152         /* Overwrite the XML data. */
153         if (fseeko(fp, w->hdr.xml_res_entry.offset, SEEK_SET) != 0) {
154                 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" "
155                                  "for XML data", w->hdr.xml_res_entry.offset);
156                 ret = WIMLIB_ERR_WRITE;
157                 goto err;
158         }
159         ret = write_xml_data(w->wim_info, WIM_ALL_IMAGES, fp, 0);
160         if (ret != 0)
161                 goto err;
162
163         DEBUG("Updating XML resource entry.");
164         /* Update the XML resource entry in the WIM header. */
165         xml_end = ftello(fp);
166         if (xml_end == -1) {
167                 ret = WIMLIB_ERR_WRITE;
168                 goto err;
169         }
170         xml_size = xml_end - w->hdr.xml_res_entry.offset;
171         w->hdr.xml_res_entry.size = xml_size;
172         w->hdr.xml_res_entry.original_size = xml_size;
173
174         if (flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY) {
175                 DEBUG("Writing integrity table.");
176                 w->hdr.integrity.offset        = xml_end;
177                 if (integrity_table) {
178                         /* The existing integrity table was saved. */
179                         bytes_written = fwrite(integrity_table, 1,
180                                                w->hdr.integrity.size, fp);
181                         if (bytes_written != w->hdr.integrity.size) {
182                                 ERROR_WITH_ERRNO("Failed to write integrity "
183                                                  "table");
184                                 ret = WIMLIB_ERR_WRITE;
185                                 goto err;
186                         }
187                         FREE(integrity_table);
188                 } else {
189                         /* There was no existing integrity table, so a new one
190                          * must be calculated. */
191                         ret = write_integrity_table(fp, WIM_HEADER_DISK_SIZE,
192                                         w->hdr.lookup_table_res_entry.offset +
193                                         w->hdr.lookup_table_res_entry.size,
194                                         flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS);
195                         if (ret != 0)
196                                 goto err;
197
198                         off_t integrity_size           = ftello(fp) - xml_end;
199                         w->hdr.integrity.size          = integrity_size;
200                         w->hdr.integrity.original_size = integrity_size;
201                         w->hdr.integrity.flags         = 0;
202                 }
203         } else {
204                 DEBUG("Truncating file to end of XML data.");
205                 /* No integrity table to write.  The file should be truncated
206                  * because it's possible that the old file was longer (due to it
207                  * including an integrity table, or due to its XML data being
208                  * longer) */
209                 if (fflush(fp) != 0) {
210                         ERROR_WITH_ERRNO("Failed to flush stream for file `%s'",
211                                          w->filename);
212                         return WIMLIB_ERR_WRITE;
213                 }
214                 if (ftruncate(fileno(fp), xml_end) != 0) {
215                         ERROR_WITH_ERRNO("Failed to truncate `%s' to %"PRIu64" "
216                                          "bytes", w->filename, xml_end);
217                         return WIMLIB_ERR_WRITE;
218                 }
219                 memset(&w->hdr.integrity, 0, sizeof(struct resource_entry));
220         }
221
222         DEBUG("Overwriting header.");
223         /* Overwrite the header. */
224         if (fseeko(fp, 0, SEEK_SET) != 0) {
225                 ERROR_WITH_ERRNO("Failed to seek to beginning of `%s'",
226                                  w->filename);
227                 return WIMLIB_ERR_WRITE;
228         }
229
230         ret = write_header(&w->hdr, fp);
231         if (ret != 0)
232                 return ret;
233
234         DEBUG("Closing `%s'.", w->filename);
235         if (fclose(fp) != 0) {
236                 ERROR_WITH_ERRNO("Failed to close `%s'", w->filename);
237                 return WIMLIB_ERR_WRITE;
238         }
239         w->fp = NULL;
240         DEBUG("Done.");
241         return 0;
242 err:
243         FREE(integrity_table);
244         return ret;
245 }
246
247
248 /* Write the file resources for the current image. */
249 static int write_file_resources(WIMStruct *w)
250 {
251         DEBUG("Writing file resources for image %u.", w->current_image);
252         return for_dentry_in_tree(wim_root_dentry(w), write_dentry_resources, w);
253 }
254
255 /* Write the lookup table, xml data, and integrity table, then overwrite the WIM
256  * header.
257  *
258  * write_lt is zero iff the lookup table is not to be written; i.e. it is
259  * handled elsewhere. */
260 int finish_write(WIMStruct *w, int image, int flags, int write_lt)
261 {
262         off_t lookup_table_offset;
263         off_t xml_data_offset;
264         off_t lookup_table_size;
265         off_t integrity_offset;
266         off_t xml_data_size;
267         off_t end_offset;
268         off_t integrity_size;
269         int ret;
270         struct wim_header hdr;
271         FILE *out = w->out_fp;
272
273         if (write_lt) {
274                 lookup_table_offset = ftello(out);
275                 if (lookup_table_offset == -1)
276                         return WIMLIB_ERR_WRITE;
277
278                 DEBUG("Writing lookup table (offset %"PRIu64")", lookup_table_offset);
279                 /* Write the lookup table. */
280                 ret = write_lookup_table(w->lookup_table, out);
281                 if (ret != 0)
282                         return ret;
283         }
284
285
286         xml_data_offset = ftello(out);
287         if (xml_data_offset == -1)
288                 return WIMLIB_ERR_WRITE;
289         DEBUG("Writing XML data (offset %"PRIu64")", xml_data_offset);
290
291         /* @hdr will be the header for the new WIM.  First copy all the data
292          * from the header in the WIMStruct; then set all the fields that may
293          * have changed, including the resource entries, boot index, and image
294          * count.  */
295         memcpy(&hdr, &w->hdr, sizeof(struct wim_header));
296         if (write_lt) {
297                 lookup_table_size = xml_data_offset - lookup_table_offset;
298                 hdr.lookup_table_res_entry.offset        = lookup_table_offset;
299                 hdr.lookup_table_res_entry.size          = lookup_table_size;
300         }
301         hdr.lookup_table_res_entry.original_size = hdr.lookup_table_res_entry.size;
302         hdr.lookup_table_res_entry.flags         = WIM_RESHDR_FLAG_METADATA;
303
304         ret = write_xml_data(w->wim_info, image, out,
305                              write_lt ? 0 : wim_info_get_total_bytes(w->wim_info));
306         if (ret != 0)
307                 return ret;
308
309         integrity_offset = ftello(out);
310         if (integrity_offset == -1)
311                 return WIMLIB_ERR_WRITE;
312         xml_data_size = integrity_offset - xml_data_offset;
313
314         hdr.xml_res_entry.offset                 = xml_data_offset;
315         hdr.xml_res_entry.size                   = xml_data_size;
316         hdr.xml_res_entry.original_size          = xml_data_size;
317         hdr.xml_res_entry.flags                  = 0;
318
319         if (flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY) {
320                 ret = write_integrity_table(out, WIM_HEADER_DISK_SIZE,
321                                             xml_data_offset,
322                                             flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS);
323                 if (ret != 0)
324                         return ret;
325                 end_offset = ftello(out);
326                 if (end_offset == -1)
327                         return WIMLIB_ERR_WRITE;
328                 integrity_size = end_offset - integrity_offset;
329                 hdr.integrity.offset = integrity_offset;
330                 hdr.integrity.size   = integrity_size;
331                 hdr.integrity.original_size = integrity_size;
332         } else {
333                 hdr.integrity.offset        = 0;
334                 hdr.integrity.size          = 0;
335                 hdr.integrity.original_size = 0;
336         }
337         hdr.integrity.flags = 0;
338
339         DEBUG("Updating WIM header.");
340
341         /*
342          * In the WIM header, there is room for the resource entry for a
343          * metadata resource labeled as the "boot metadata".  This entry should
344          * be zeroed out if there is no bootable image (boot_idx 0).  Otherwise,
345          * it should be a copy of the resource entry for the image that is
346          * marked as bootable.  This is not well documented...
347          */
348         if (hdr.boot_idx == 0 || !w->image_metadata
349                         || (image != WIM_ALL_IMAGES && image != hdr.boot_idx)) {
350                 memset(&hdr.boot_metadata_res_entry, 0,
351                        sizeof(struct resource_entry));
352         } else {
353                 memcpy(&hdr.boot_metadata_res_entry,
354                        &w->image_metadata[
355                           hdr.boot_idx - 1].metadata_lte->output_resource_entry,
356                        sizeof(struct resource_entry));
357         }
358
359         /* Set image count and boot index correctly for single image writes */
360         if (image != WIM_ALL_IMAGES) {
361                 hdr.image_count = 1;
362                 if (hdr.boot_idx == image)
363                         hdr.boot_idx = 1;
364                 else
365                         hdr.boot_idx = 0;
366         }
367
368
369         if (fseeko(out, 0, SEEK_SET) != 0)
370                 return WIMLIB_ERR_WRITE;
371
372         return write_header(&hdr, out);
373 }
374
375 /* Open file stream and write dummy header for WIM. */
376 int begin_write(WIMStruct *w, const char *path, int flags)
377 {
378         const char *mode;
379         DEBUG("Opening `%s' for new WIM", path);
380
381         /* checking the integrity requires going back over the file to read it.
382          * XXX
383          * (It also would be possible to keep a running sha1sum as the file
384          * as written-- this would be faster, but a bit more complicated) */
385         if (flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY)
386                 mode = "w+b";
387         else
388                 mode = "wb";
389
390         w->out_fp = fopen(path, mode);
391         if (!w->out_fp) {
392                 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
393                                  path);
394                 return WIMLIB_ERR_OPEN;
395         }
396
397         /* Write dummy header. It will be overwritten later. */
398         return write_header(&w->hdr, w->out_fp);
399 }
400
401 /* Writes the WIM to a file.  */
402 WIMLIBAPI int wimlib_write(WIMStruct *w, const char *path, int image, int flags)
403 {
404         int ret;
405
406         if (!w || !path)
407                 return WIMLIB_ERR_INVALID_PARAM;
408
409         if (image != WIM_ALL_IMAGES &&
410              (image < 1 || image > w->hdr.image_count))
411                 return WIMLIB_ERR_INVALID_IMAGE;
412
413
414         if (w->hdr.total_parts != 1) {
415                 ERROR("Cannot call wimlib_write() on part of a split WIM");
416                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
417         }
418
419         if (image == WIM_ALL_IMAGES)
420                 DEBUG("Writing all images to `%s'.", path);
421         else
422                 DEBUG("Writing image %d to `%s'.", image, path);
423
424         ret = begin_write(w, path, flags);
425         if (ret != 0)
426                 goto done;
427
428         for_lookup_table_entry(w->lookup_table, lte_zero_out_refcnt, NULL);
429
430         w->write_flags = flags;
431
432         ret = for_image(w, image, write_file_resources);
433         if (ret != 0) {
434                 ERROR("Failed to write WIM file resources to `%s'", path);
435                 goto done;
436         }
437
438         ret = for_image(w, image, write_metadata_resource);
439
440         if (ret != 0) {
441                 ERROR("Failed to write WIM image metadata to `%s'", path);
442                 goto done;
443         }
444
445         ret = finish_write(w, image, flags, 1);
446
447 done:
448         DEBUG("Closing output file.");
449         if (w->out_fp != NULL) {
450                 if (fclose(w->out_fp) != 0) {
451                         ERROR_WITH_ERRNO("Failed to close the file `%s'", path);
452                         ret = WIMLIB_ERR_WRITE;
453                 }
454                 w->out_fp = NULL;
455         }
456         return ret;
457 }