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);
88 if (unlink(tmpfile) != 0)
89 WARNING("Failed to remove `%s'", tmpfile);
93 DEBUG("Closing original WIM file.");
94 /* Close the original WIM file that was opened for reading. */
96 if (fclose(w->fp) != 0) {
97 WARNING("Failed to close the file `%s'", wimfile_name);
102 DEBUG("Renaming `%s' to `%s'", tmpfile, wimfile_name);
104 /* Rename the new file to the old file .*/
105 if (rename(tmpfile, wimfile_name) != 0) {
106 ERROR_WITH_ERRNO("Failed to rename `%s' to `%s'",
107 tmpfile, wimfile_name);
108 /* Remove temporary file. */
109 if (unlink(tmpfile) != 0)
110 ERROR_WITH_ERRNO("Failed to remove `%s'", tmpfile);
111 return WIMLIB_ERR_RENAME;
117 static int check_resource_offset(struct lookup_table_entry *lte, void *arg)
119 u64 xml_data_offset = *(u64*)arg;
120 if (lte->resource_entry.offset > xml_data_offset) {
121 ERROR("The following resource is *after* the XML data:");
122 print_lookup_table_entry(lte);
123 return WIMLIB_ERR_RESOURCE_ORDER;
128 WIMLIBAPI int wimlib_overwrite_xml_and_header(WIMStruct *w, int write_flags)
132 u8 *integrity_table = NULL;
135 size_t bytes_written;
137 DEBUG("Overwriting XML and header of `%s', write_flags = %#x",
138 w->filename, write_flags);
141 return WIMLIB_ERR_NO_FILENAME;
143 write_flags &= ~WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE;
145 /* Make sure that the integrity table (if present) is after the XML
146 * data, and that there are no stream resources, metadata resources, or
147 * lookup tables after the XML data. Otherwise, these data would be
148 * destroyed by this function. */
149 if (w->hdr.integrity.offset != 0 &&
150 w->hdr.integrity.offset < w->hdr.xml_res_entry.offset) {
151 ERROR("Didn't expect the integrity table to be before the XML data");
152 return WIMLIB_ERR_RESOURCE_ORDER;
155 if (w->hdr.lookup_table_res_entry.offset >
156 w->hdr.xml_res_entry.offset) {
157 ERROR("Didn't expect the lookup table to be after the XML data");
158 return WIMLIB_ERR_RESOURCE_ORDER;
161 ret = for_lookup_table_entry(w->lookup_table, check_resource_offset,
162 &w->hdr.xml_res_entry.offset);
172 /* The old integrity table is still OK, as the SHA1 message digests in
173 * the integrity table include neither the header nor the XML data.
174 * Save it for later if it exists and an integrity table was required.
176 if ((write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY)
177 && w->hdr.integrity.offset != 0)
179 DEBUG("Reading existing integrity table.");
180 integrity_table = MALLOC(w->hdr.integrity.size);
181 if (!integrity_table)
182 return WIMLIB_ERR_NOMEM;
184 ret = read_uncompressed_resource(fp, w->hdr.integrity.offset,
185 w->hdr.integrity.original_size,
189 DEBUG("Done reading existing integrity table.");
192 DEBUG("Overwriting XML data.");
193 /* Overwrite the XML data. */
194 if (fseeko(fp, w->hdr.xml_res_entry.offset, SEEK_SET) != 0) {
195 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" "
196 "for XML data", w->hdr.xml_res_entry.offset);
197 ret = WIMLIB_ERR_WRITE;
200 ret = write_xml_data(w->wim_info, WIM_ALL_IMAGES, fp, 0);
204 DEBUG("Updating XML resource entry.");
205 /* Update the XML resource entry in the WIM header. */
206 xml_end = ftello(fp);
208 ret = WIMLIB_ERR_WRITE;
211 xml_size = xml_end - w->hdr.xml_res_entry.offset;
212 w->hdr.xml_res_entry.size = xml_size;
213 w->hdr.xml_res_entry.original_size = xml_size;
214 /* XML data offset is unchanged. */
216 if (write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY) {
217 DEBUG("Writing integrity table.");
218 w->hdr.integrity.offset = xml_end;
219 if (integrity_table) {
220 /* The existing integrity table was saved. */
221 bytes_written = fwrite(integrity_table, 1,
222 w->hdr.integrity.size, fp);
223 if (bytes_written != w->hdr.integrity.size) {
224 ERROR_WITH_ERRNO("Failed to write integrity "
226 ret = WIMLIB_ERR_WRITE;
229 FREE(integrity_table);
231 /* There was no existing integrity table, so a new one
232 * must be calculated. */
233 ret = write_integrity_table(fp, WIM_HEADER_DISK_SIZE,
234 w->hdr.lookup_table_res_entry.offset +
235 w->hdr.lookup_table_res_entry.size,
236 write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS);
240 off_t end_integrity = ftello(fp);
241 if (end_integrity == -1)
242 return WIMLIB_ERR_WRITE;
244 off_t integrity_size = end_integrity - xml_end;
245 w->hdr.integrity.size = integrity_size;
246 w->hdr.integrity.original_size = integrity_size;
247 w->hdr.integrity.flags = 0;
250 DEBUG("Truncating file to end of XML data.");
251 /* No integrity table to write. The file should be truncated
252 * because it's possible that the old file was longer (due to it
253 * including an integrity table, or due to its XML data being
255 if (fflush(fp) != 0) {
256 ERROR_WITH_ERRNO("Failed to flush stream for file `%s'",
258 return WIMLIB_ERR_WRITE;
260 if (ftruncate(fileno(fp), xml_end) != 0) {
261 ERROR_WITH_ERRNO("Failed to truncate `%s' to %"PRIu64" "
262 "bytes", w->filename, xml_end);
263 return WIMLIB_ERR_WRITE;
265 memset(&w->hdr.integrity, 0, sizeof(struct resource_entry));
268 DEBUG("Overwriting header.");
269 /* Overwrite the header. */
270 if (fseeko(fp, 0, SEEK_SET) != 0) {
271 ERROR_WITH_ERRNO("Failed to seek to beginning of `%s'",
273 return WIMLIB_ERR_WRITE;
276 ret = write_header(&w->hdr, fp);
280 DEBUG("Closing `%s'.", w->filename);
281 if (fclose(fp) != 0) {
282 ERROR_WITH_ERRNO("Failed to close `%s'", w->filename);
283 return WIMLIB_ERR_WRITE;
289 FREE(integrity_table);
294 /* Write the file resources for the current image. */
295 static int write_file_resources(WIMStruct *w)
297 DEBUG("Writing file resources for image %u.", w->current_image);
298 return for_dentry_in_tree(wim_root_dentry(w), write_dentry_resources, w);
302 * Write the lookup table, xml data, and integrity table, then overwrite the WIM
305 int finish_write(WIMStruct *w, int image, int write_flags)
307 off_t lookup_table_offset;
308 off_t xml_data_offset;
309 off_t lookup_table_size;
310 off_t integrity_offset;
313 off_t integrity_size;
315 struct wim_header hdr;
316 FILE *out = w->out_fp;
318 if (!(write_flags & WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE)) {
319 /* Write the lookup table. */
320 lookup_table_offset = ftello(out);
321 if (lookup_table_offset == -1)
322 return WIMLIB_ERR_WRITE;
324 DEBUG("Writing lookup table (offset %"PRIu64")",
325 lookup_table_offset);
326 ret = write_lookup_table(w->lookup_table, out);
331 xml_data_offset = ftello(out);
332 if (xml_data_offset == -1)
333 return WIMLIB_ERR_WRITE;
335 /* @hdr will be the header for the new WIM. First copy all the data
336 * from the header in the WIMStruct; then set all the fields that may
337 * have changed, including the resource entries, boot index, and image
339 memcpy(&hdr, &w->hdr, sizeof(struct wim_header));
340 if (!(write_flags & WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE)) {
341 lookup_table_size = xml_data_offset - lookup_table_offset;
342 hdr.lookup_table_res_entry.offset = lookup_table_offset;
343 hdr.lookup_table_res_entry.size = lookup_table_size;
345 hdr.lookup_table_res_entry.original_size = hdr.lookup_table_res_entry.size;
346 hdr.lookup_table_res_entry.flags = WIM_RESHDR_FLAG_METADATA;
348 DEBUG("Writing XML data (offset %"PRIu64")", xml_data_offset);
349 ret = write_xml_data(w->wim_info, image, out,
350 (write_flags & WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE) ?
351 wim_info_get_total_bytes(w->wim_info) : 0);
355 integrity_offset = ftello(out);
356 if (integrity_offset == -1)
357 return WIMLIB_ERR_WRITE;
358 xml_data_size = integrity_offset - xml_data_offset;
360 hdr.xml_res_entry.offset = xml_data_offset;
361 hdr.xml_res_entry.size = xml_data_size;
362 hdr.xml_res_entry.original_size = xml_data_size;
363 hdr.xml_res_entry.flags = 0;
365 if (write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY) {
366 ret = write_integrity_table(out, WIM_HEADER_DISK_SIZE,
368 write_flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS);
371 end_offset = ftello(out);
372 if (end_offset == -1)
373 return WIMLIB_ERR_WRITE;
374 integrity_size = end_offset - integrity_offset;
375 hdr.integrity.offset = integrity_offset;
376 hdr.integrity.size = integrity_size;
377 hdr.integrity.original_size = integrity_size;
379 hdr.integrity.offset = 0;
380 hdr.integrity.size = 0;
381 hdr.integrity.original_size = 0;
383 hdr.integrity.flags = 0;
385 DEBUG("Updating WIM header.");
388 * In the WIM header, there is room for the resource entry for a
389 * metadata resource labeled as the "boot metadata". This entry should
390 * be zeroed out if there is no bootable image (boot_idx 0). Otherwise,
391 * it should be a copy of the resource entry for the image that is
392 * marked as bootable. This is not well documented...
394 if (hdr.boot_idx == 0 || !w->image_metadata
395 || (image != WIM_ALL_IMAGES && image != hdr.boot_idx)) {
396 memset(&hdr.boot_metadata_res_entry, 0,
397 sizeof(struct resource_entry));
399 memcpy(&hdr.boot_metadata_res_entry,
401 hdr.boot_idx - 1].metadata_lte->output_resource_entry,
402 sizeof(struct resource_entry));
405 /* Set image count and boot index correctly for single image writes */
406 if (image != WIM_ALL_IMAGES) {
408 if (hdr.boot_idx == image)
415 if (fseeko(out, 0, SEEK_SET) != 0)
416 return WIMLIB_ERR_WRITE;
418 ret = write_header(&hdr, out);
422 DEBUG("Closing output file.");
423 wimlib_assert(w->out_fp != NULL);
424 if (fclose(w->out_fp) != 0) {
425 ERROR_WITH_ERRNO("Failed to close the WIM file");
426 ret = WIMLIB_ERR_WRITE;
432 /* Open file stream and write dummy header for WIM. */
433 int begin_write(WIMStruct *w, const char *path, int write_flags)
436 DEBUG("Opening `%s' for new WIM", path);
438 /* checking the integrity requires going back over the file to read it.
440 * (It also would be possible to keep a running sha1sum as the file is
441 * written-- this would be faster, but a bit more complicated) */
442 if (write_flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY)
450 w->out_fp = fopen(path, mode);
452 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
454 return WIMLIB_ERR_OPEN;
457 /* Write dummy header. It will be overwritten later. */
458 return write_header(&w->hdr, w->out_fp);
461 /* Writes a stand-alone WIM to a file. */
462 WIMLIBAPI int wimlib_write(WIMStruct *w, const char *path,
463 int image, int write_flags)
468 return WIMLIB_ERR_INVALID_PARAM;
470 write_flags &= ~WIMLIB_WRITE_FLAG_NO_LOOKUP_TABLE;
472 if (image != WIM_ALL_IMAGES &&
473 (image < 1 || image > w->hdr.image_count))
474 return WIMLIB_ERR_INVALID_IMAGE;
477 if (w->hdr.total_parts != 1) {
478 ERROR("Cannot call wimlib_write() on part of a split WIM");
479 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
482 if (image == WIM_ALL_IMAGES)
483 DEBUG("Writing all images to `%s'.", path);
485 DEBUG("Writing image %d to `%s'.", image, path);
487 ret = begin_write(w, path, write_flags);
491 for_lookup_table_entry(w->lookup_table, lte_zero_out_refcnt, NULL);
493 w->write_flags = write_flags;
495 ret = for_image(w, image, write_file_resources);
497 ERROR("Failed to write WIM file resources to `%s'", path);
501 ret = for_image(w, image, write_metadata_resource);
504 /*ERROR("Failed to write WIM image metadata to `%s'", path);*/
508 return finish_write(w, image, write_flags);