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 Lesser General Public License as published by the Free
16 * Software Foundation; either version 2.1 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 Lesser General Public License for more
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/.
28 #include "wimlib_internal.h"
31 #include "lookup_table.h"
35 /* Reopens the FILE* for a WIM read-write. */
36 static int reopen_rw(WIMStruct *w)
40 if (fclose(w->fp) != 0)
41 ERROR("Failed to close the file `%s': %m\n", w->filename);
42 fp = fopen(w->filename, "r+b");
44 ERROR("Failed to open `%s' for reading and writing: "
46 return WIMLIB_ERR_OPEN;
55 * Writes a WIM file to the original file that it was read from, overwriting it.
57 WIMLIBAPI int wimlib_overwrite(WIMStruct *w, int flags)
59 const char *wimfile_name;
63 wimfile_name = w->filename;
65 DEBUG("Replacing WIM file `%s'\n", wimfile_name);
68 return WIMLIB_ERR_NO_FILENAME;
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';
78 ret = wimlib_write(w, tmpfile, WIM_ALL_IMAGES, flags);
80 ERROR("Failed to write the WIM file `%s'!\n", tmpfile);
84 DEBUG("Closing original WIM file.\n");
85 /* Close the original WIM file that was opened for reading. */
87 if (fclose(w->fp) != 0) {
88 WARNING("Failed to close the file `%s'\n",
94 DEBUG("Renaming `%s' to `%s'\n", tmpfile, wimfile_name);
96 /* Rename the new file to the old file .*/
97 if (rename(tmpfile, wimfile_name) != 0) {
98 ERROR("Failed to rename `%s' to `%s': %m\n", tmpfile,
100 /* Remove temporary file. */
101 if (unlink(tmpfile) != 0)
102 ERROR("Failed to remove `%s': %m\n", tmpfile);
103 return WIMLIB_ERR_RENAME;
110 WIMLIBAPI int wimlib_overwrite_xml_and_header(WIMStruct *w, int flags)
114 u8 *integrity_table = NULL;
117 size_t bytes_written;
119 DEBUG("Overwriting XML and header of `%s', flags = %d\n",
122 return WIMLIB_ERR_NO_FILENAME;
130 /* The old integrity table is still OK, as the SHA1 message digests in
131 * the integrity table include neither the header nor the XML data.
132 * Save it for later if it exists and an integrity table was required.
134 if (flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY &&
135 w->hdr.integrity.offset != 0) {
136 DEBUG("Reading existing integrity table.\n");
137 integrity_table = MALLOC(w->hdr.integrity.size);
138 if (!integrity_table)
139 return WIMLIB_ERR_NOMEM;
141 ret = read_uncompressed_resource(fp, w->hdr.integrity.offset,
142 w->hdr.integrity.original_size,
146 DEBUG("Done reading existing integrity table.\n");
149 DEBUG("Overwriting XML data.\n");
150 /* Overwrite the XML data. */
151 if (fseeko(fp, w->hdr.xml_res_entry.offset, SEEK_SET) != 0) {
152 ERROR("Failed to seek to byte %"PRIu64" for XML data: "
153 "%m\n", w->hdr.xml_res_entry.offset);
154 ret = WIMLIB_ERR_WRITE;
157 ret = write_xml_data(w->wim_info, WIM_ALL_IMAGES, fp, 0);
161 DEBUG("Updating XML resource entry.\n");
162 /* Update the XML resource entry in the WIM header. */
163 xml_end = ftello(fp);
165 ret = WIMLIB_ERR_WRITE;
168 xml_size = xml_end - w->hdr.xml_res_entry.offset;
169 w->hdr.xml_res_entry.size = xml_size;
170 w->hdr.xml_res_entry.original_size = xml_size;
172 if (flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY) {
173 DEBUG("Writing integrity table.\n");
174 w->hdr.integrity.offset = xml_end;
175 if (integrity_table) {
176 /* The existing integrity table was saved. */
177 bytes_written = fwrite(integrity_table, 1,
178 w->hdr.integrity.size, fp);
179 if (bytes_written != w->hdr.integrity.size) {
180 ERROR("Failed to write integrity table: %m\n");
181 ret = WIMLIB_ERR_WRITE;
184 FREE(integrity_table);
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);
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;
201 DEBUG("Truncating file to end of XML data.\n");
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
206 if (fflush(fp) != 0) {
207 ERROR("Failed to flush stream for file `%s': %m\n",
209 return WIMLIB_ERR_WRITE;
211 if (ftruncate(fileno(fp), xml_end) != 0) {
212 ERROR("Failed to truncate `%s' to %"PRIu64" "
214 w->filename, xml_end);
215 return WIMLIB_ERR_WRITE;
217 memset(&w->hdr.integrity, 0, sizeof(struct resource_entry));
220 DEBUG("Overwriting header.\n");
221 /* Overwrite the header. */
222 if (fseeko(fp, 0, SEEK_SET) != 0) {
223 ERROR("Failed to seek to beginning of `%s': %m\n",
225 return WIMLIB_ERR_WRITE;
228 ret = write_header(&w->hdr, fp);
232 DEBUG("Closing file.\n");
233 if (fclose(fp) != 0) {
234 ERROR("Failed to close `%s': %m\n", w->filename);
235 return WIMLIB_ERR_WRITE;
241 FREE(integrity_table);
246 /* Write the file resources for the current image. */
247 static int write_file_resources(WIMStruct *w)
250 DEBUG("Writing file resources for image %u\n", w->current_image);
251 return for_dentry_in_tree(wim_root_dentry(w), write_file_resource, w);
254 /* Write lookup table, xml data, lookup table, and rewrite header
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)
260 off_t lookup_table_offset;
261 off_t xml_data_offset;
262 off_t lookup_table_size;
263 off_t integrity_offset;
266 off_t integrity_size;
269 struct wim_header hdr;
270 FILE *out = w->out_fp;
273 lookup_table_offset = ftello(out);
274 if (lookup_table_offset == -1)
275 return WIMLIB_ERR_WRITE;
277 DEBUG("Writing lookup table (offset %"PRIu64")\n", lookup_table_offset);
278 /* Write the lookup table. */
279 ret = write_lookup_table(w->lookup_table, out);
285 xml_data_offset = ftello(out);
286 if (xml_data_offset == -1)
287 return WIMLIB_ERR_WRITE;
288 DEBUG("Writing XML data (offset %"PRIu64")\n", xml_data_offset);
290 /* @hdr will be the header for the new WIM. First copy all the data
291 * from the header in the WIMStruct; then set all the fields that may
292 * have changed, including the resource entries, boot index, and image
294 memcpy(&hdr, &w->hdr, sizeof(struct wim_header));
296 lookup_table_size = xml_data_offset - lookup_table_offset;
297 hdr.lookup_table_res_entry.offset = lookup_table_offset;
298 hdr.lookup_table_res_entry.size = lookup_table_size;
300 hdr.lookup_table_res_entry.original_size = hdr.lookup_table_res_entry.size;
301 hdr.lookup_table_res_entry.flags = WIM_RESHDR_FLAG_METADATA;
303 ret = write_xml_data(w->wim_info, image, out,
304 write_lt ? 0 : wim_info_get_total_bytes(w->wim_info));
308 integrity_offset = ftello(out);
309 if (integrity_offset == -1)
310 return WIMLIB_ERR_WRITE;
311 xml_data_size = integrity_offset - xml_data_offset;
313 hdr.xml_res_entry.offset = xml_data_offset;
314 hdr.xml_res_entry.size = xml_data_size;
315 hdr.xml_res_entry.original_size = xml_data_size;
316 hdr.xml_res_entry.flags = 0;
318 if (flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY) {
319 ret = write_integrity_table(out, WIM_HEADER_DISK_SIZE,
321 flags & WIMLIB_WRITE_FLAG_SHOW_PROGRESS);
324 end_offset = ftello(out);
325 if (end_offset == -1)
326 return WIMLIB_ERR_WRITE;
327 integrity_size = end_offset - integrity_offset;
328 hdr.integrity.offset = integrity_offset;
329 hdr.integrity.size = integrity_size;
330 hdr.integrity.original_size = integrity_size;
332 hdr.integrity.offset = 0;
333 hdr.integrity.size = 0;
334 hdr.integrity.original_size = 0;
336 hdr.integrity.flags = 0;
338 DEBUG("Updating WIM header.\n");
341 * In the WIM header, there is room for the resource entry for a
342 * metadata resource labeled as the "boot metadata". This entry should
343 * be zeroed out if there is no bootable image (boot_idx 0). Otherwise,
344 * it should be a copy of the resource entry for the image that is
345 * marked as bootable. This is not well documented...
347 if (hdr.boot_idx == 0 || !w->image_metadata
348 || (image != WIM_ALL_IMAGES && image != hdr.boot_idx)) {
349 memset(&hdr.boot_metadata_res_entry, 0,
350 sizeof(struct resource_entry));
352 memcpy(&hdr.boot_metadata_res_entry,
353 &w->image_metadata[hdr.boot_idx - 1].lookup_table_entry->
354 output_resource_entry,
355 sizeof(struct resource_entry));
358 /* Set image count and boot index correctly for single image writes */
359 if (image != WIM_ALL_IMAGES) {
361 if (hdr.boot_idx == image)
368 if (fseeko(out, 0, SEEK_SET) != 0)
369 return WIMLIB_ERR_WRITE;
371 return write_header(&hdr, out);
374 /* Open file stream and write dummy header for WIM. */
375 int begin_write(WIMStruct *w, const char *path, int flags)
378 DEBUG("Opening `%s' for new WIM\n", path);
380 /* checking the integrity requires going back over the file to read it.
382 * (It also would be possible to keep a running sha1sum as the file
383 * as written-- this would be faster, but a bit more complicated) */
384 if (flags & WIMLIB_WRITE_FLAG_CHECK_INTEGRITY)
389 w->out_fp = fopen(path, mode);
391 ERROR("Failed to open the file `%s' for writing!\n", path);
392 return WIMLIB_ERR_OPEN;
395 /* Write dummy header. It will be overwritten later. */
396 return write_header(&w->hdr, w->out_fp);
399 /* Writes the WIM to a file. */
400 WIMLIBAPI int wimlib_write(WIMStruct *w, const char *path, int image, int flags)
404 if (image != WIM_ALL_IMAGES &&
405 (image < 1 || image > w->hdr.image_count))
406 return WIMLIB_ERR_INVALID_IMAGE;
408 if (image == WIM_ALL_IMAGES)
409 DEBUG("Writing all images to `%s'\n", path);
411 DEBUG("Writing image %d to `%s'\n", image, path);
413 ret = begin_write(w, path, flags);
417 for_lookup_table_entry(w->lookup_table, zero_out_refcnts, NULL);
419 ret = for_image(w, image, write_file_resources);
421 ERROR("Failed to write file resources!\n");
425 ret = for_image(w, image, write_metadata_resource);
428 ERROR("Failed to write image metadata!\n");
432 ret = finish_write(w, image, flags, 1);
435 DEBUG("Closing output file.\n");
436 if (w->out_fp != NULL) {
437 if (fclose(w->out_fp) != 0) {
438 ERROR("Failed to close the file `%s': %m\n", path);
439 ret = WIMLIB_ERR_WRITE;