4 * Support for writing WIM files; write a WIM file, overwrite a WIM file, write
5 * compressed file resources, etc.
9 * Copyright (C) 2010 Carl Thijssen
10 * Copyright (C) 2012 Eric Biggers
12 * This file is part of wimlib, a library for working with WIM files.
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)
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
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/.
28 #include "wimlib_internal.h"
31 #include "lookup_table.h"
36 /* Reopens the FILE* for a WIM read-write. */
37 static int reopen_rw(WIMStruct *w)
41 if (fclose(w->fp) != 0)
42 ERROR_WITH_ERRNO("Failed to close the file `%s'", w->filename);
44 fp = fopen(w->filename, "r+b");
46 ERROR_WITH_ERRNO("Failed to open `%s' for reading and writing",
48 return WIMLIB_ERR_OPEN;
57 * Writes a WIM file to the original file that it was read from, overwriting it.
59 WIMLIBAPI int wimlib_overwrite(WIMStruct *w, int write_flags)
61 const char *wimfile_name;
66 return WIMLIB_ERR_INVALID_PARAM;
68 write_flags &= ~WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE;
70 wimfile_name = w->filename;
72 DEBUG("Replacing WIM file `%s'.", wimfile_name);
75 return WIMLIB_ERR_NO_FILENAME;
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';
85 ret = wimlib_write(w, tmpfile, WIM_ALL_IMAGES, write_flags);
87 ERROR("Failed to write the WIM file `%s'", tmpfile);
91 DEBUG("Closing original WIM file.");
92 /* Close the original WIM file that was opened for reading. */
94 if (fclose(w->fp) != 0) {
95 WARNING("Failed to close the file `%s'", wimfile_name);
100 DEBUG("Renaming `%s' to `%s'", tmpfile, wimfile_name);
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;
115 static int check_resource_offset(struct lookup_table_entry *lte, void *arg)
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;
126 WIMLIBAPI int wimlib_overwrite_xml_and_header(WIMStruct *w, int write_flags)
130 u8 *integrity_table = NULL;
133 size_t bytes_written;
135 DEBUG("Overwriting XML and header of `%s', write_flags = %#x",
136 w->filename, write_flags);
139 return WIMLIB_ERR_NO_FILENAME;
141 write_flags &= ~WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE;
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;
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;
159 ret = for_lookup_table_entry(w->lookup_table, check_resource_offset,
160 &w->hdr.xml_res_entry.offset);
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.
174 if ((write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY)
175 && w->hdr.integrity.offset != 0)
177 DEBUG("Reading existing integrity table.");
178 integrity_table = MALLOC(w->hdr.integrity.size);
179 if (!integrity_table)
180 return WIMLIB_ERR_NOMEM;
182 ret = read_uncompressed_resource(fp, w->hdr.integrity.offset,
183 w->hdr.integrity.original_size,
187 DEBUG("Done reading existing integrity table.");
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;
198 ret = write_xml_data(w->wim_info, WIM_ALL_IMAGES, fp, 0);
202 DEBUG("Updating XML resource entry.");
203 /* Update the XML resource entry in the WIM header. */
204 xml_end = ftello(fp);
206 ret = WIMLIB_ERR_WRITE;
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. */
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 "
224 ret = WIMLIB_ERR_WRITE;
227 FREE(integrity_table);
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);
238 off_t end_integrity = ftello(fp);
239 if (end_integrity == -1)
240 return WIMLIB_ERR_WRITE;
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;
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
253 if (fflush(fp) != 0) {
254 ERROR_WITH_ERRNO("Failed to flush stream for file `%s'",
256 return WIMLIB_ERR_WRITE;
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;
263 memset(&w->hdr.integrity, 0, sizeof(struct resource_entry));
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'",
271 return WIMLIB_ERR_WRITE;
274 ret = write_header(&w->hdr, fp);
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;
287 FREE(integrity_table);
292 /* Write the file resources for the current image. */
293 static int write_file_resources(WIMStruct *w)
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);
300 * Write the lookup table, xml data, and integrity table, then overwrite the WIM
303 int finish_write(WIMStruct *w, int image, int write_flags)
305 off_t lookup_table_offset;
306 off_t xml_data_offset;
307 off_t lookup_table_size;
308 off_t integrity_offset;
311 off_t integrity_size;
313 struct wim_header hdr;
314 FILE *out = w->out_fp;
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;
322 DEBUG("Writing lookup table (offset %"PRIu64")",
323 lookup_table_offset);
324 ret = write_lookup_table(w->lookup_table, out);
329 xml_data_offset = ftello(out);
330 if (xml_data_offset == -1)
331 return WIMLIB_ERR_WRITE;
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
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;
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;
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);
353 integrity_offset = ftello(out);
354 if (integrity_offset == -1)
355 return WIMLIB_ERR_WRITE;
356 xml_data_size = integrity_offset - xml_data_offset;
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;
363 if (write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY) {
364 ret = write_integrity_table(out, WIM_HEADER_DISK_SIZE,
366 write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS);
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;
377 hdr.integrity.offset = 0;
378 hdr.integrity.size = 0;
379 hdr.integrity.original_size = 0;
381 hdr.integrity.flags = 0;
383 DEBUG("Updating WIM header.");
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...
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));
397 memcpy(&hdr.boot_metadata_res_entry,
399 hdr.boot_idx - 1].metadata_lte->output_resource_entry,
400 sizeof(struct resource_entry));
403 /* Set image count and boot index correctly for single image writes */
404 if (image != WIM_ALL_IMAGES) {
406 if (hdr.boot_idx == image)
413 if (fseeko(out, 0, SEEK_SET) != 0)
414 return WIMLIB_ERR_WRITE;
416 return write_header(&hdr, out);
419 /* Open file stream and write dummy header for WIM. */
420 int begin_write(WIMStruct *w, const char *path, int write_flags)
423 DEBUG("Opening `%s' for new WIM", path);
425 /* checking the integrity requires going back over the file to read it.
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)
434 w->out_fp = fopen(path, mode);
436 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
438 return WIMLIB_ERR_OPEN;
441 /* Write dummy header. It will be overwritten later. */
442 return write_header(&w->hdr, w->out_fp);
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)
452 return WIMLIB_ERR_INVALID_PARAM;
454 write_flags &= ~WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE;
456 if (image != WIM_ALL_IMAGES &&
457 (image < 1 || image > w->hdr.image_count))
458 return WIMLIB_ERR_INVALID_IMAGE;
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;
466 if (image == WIM_ALL_IMAGES)
467 DEBUG("Writing all images to `%s'.", path);
469 DEBUG("Writing image %d to `%s'.", image, path);
471 ret = begin_write(w, path, write_flags);
475 for_lookup_table_entry(w->lookup_table, lte_zero_out_refcnt, NULL);
477 w->write_flags = write_flags;
479 ret = for_image(w, image, write_file_resources);
481 ERROR("Failed to write WIM file resources to `%s'", path);
485 ret = for_image(w, image, write_metadata_resource);
488 ERROR("Failed to write WIM image metadata to `%s'", path);
492 ret = finish_write(w, image, write_flags);
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;