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