4 * Support for modifying WIM files with image-level operations (delete an image,
5 * add an image, export an imagex from one WIM to another.) There is nothing
6 * here that lets you change individual files in the WIM; for that you will need
7 * to look at the filesystem implementation in mount.c.
11 * Copyright (C) 2012 Eric Biggers
13 * This file is part of wimlib, a library for working with WIM files.
15 * wimlib is free software; you can redistribute it and/or modify it under the
16 * terms of the GNU Lesser General Public License as published by the Free
17 * Software Foundation; either version 2.1 of the License, or (at your option)
20 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
25 * You should have received a copy of the GNU Lesser General Public License
26 * along with wimlib; if not, see http://www.gnu.org/licenses/.
29 #include "wimlib_internal.h"
34 #include "lookup_table.h"
40 static void destroy_image_metadata(struct image_metadata *imd,
41 struct lookup_table *lt)
43 free_dentry_tree(imd->root_dentry, lt, true);
44 #ifdef ENABLE_SECURITY_DATA
45 free_security_data(imd->security_data);
48 /* Get rid of the lookup table entry for this image's metadata resource
50 lookup_table_remove(lt, imd->metadata_lte);
54 * Recursively builds a dentry tree from a directory tree on disk, outside the
57 * @root: A dentry that has already been created for the root of the dentry
59 * @source_path: The path to the root of the tree on disk.
60 * @root_stat: A pointer to a `struct stat' that contains the metadata for the
61 * root of the tree on disk.
62 * @lookup_table: The lookup table for the WIM file. For each file added to the
63 * dentry tree being built, an entry is added to the lookup table,
64 * unless an identical file is already in the lookup table. These
65 * lookup table entries that are added point to the file on disk.
67 * @return: 0 on success, nonzero on failure. It is a failure if any of
68 * the files cannot be `stat'ed, or if any of the needed
69 * directories cannot be opened or read. Failure to add the files
70 * to the WIM may still occur later when trying to actually read
71 * the regular files in the tree into the WIM as file resources.
73 static int build_dentry_tree(struct dentry *root, const char *source_path,
74 struct stat *root_stat,
75 struct lookup_table* lookup_table)
79 stbuf_to_dentry(root_stat, root);
80 if (dentry_is_directory(root)) {
81 /* Open the directory on disk */
84 struct stat child_stat;
87 dir = opendir(source_path);
89 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
91 return WIMLIB_ERR_OPEN;
94 /* Buffer for names of files in directory. */
95 size_t len = strlen(source_path);
96 char name[len + 1 + FILENAME_MAX + 1];
97 memcpy(name, source_path, len);
100 /* Create a dentry for each entry in the directory on disk, and recurse
101 * to any subdirectories. */
102 while ((p = readdir(dir)) != NULL) {
103 if (p->d_name[0] == '.' && (p->d_name[1] == '\0'
104 || (p->d_name[1] == '.' && p->d_name[2] == '\0')))
106 strcpy(name + len + 1, p->d_name);
107 if (stat(name, &child_stat) != 0) {
108 ERROR_WITH_ERRNO("Cannot stat `%s'", name);
109 ret = WIMLIB_ERR_STAT;
112 child = new_dentry(p->d_name);
114 ERROR("No memory to allocate new dentry");
115 return WIMLIB_ERR_NOMEM;
117 ret = build_dentry_tree(child, name, &child_stat,
119 link_dentry(child, root);
125 struct lookup_table_entry *lte;
127 /* For each non-directory, we must check to see if the file is
128 * in the lookup table already; if it is, we increment its
129 * refcnt; otherwise, we create a new lookup table entry and
131 ret = sha1sum(source_path, root->hash);
135 lte = lookup_resource(lookup_table, root->hash);
139 char *file_on_disk = STRDUP(source_path);
141 ERROR("Failed to allocate memory for file path");
142 return WIMLIB_ERR_NOMEM;
144 lte = new_lookup_table_entry();
147 return WIMLIB_ERR_NOMEM;
149 lte->file_on_disk = file_on_disk;
150 lte->resource_entry.flags = 0;
152 lte->part_number = 1;
153 lte->resource_entry.original_size = root_stat->st_size;
154 lte->resource_entry.size = root_stat->st_size;
155 memcpy(lte->hash, root->hash, WIM_HASH_SIZE);
156 lookup_table_insert(lookup_table, lte);
168 * This function takes in a dentry that was previously located only in image(s)
169 * in @src_wim, but now is being added to @dest_wim. If there is in fact already a
170 * lookup table entry for this file in the lookup table of the destination WIM
171 * file, we simply increment its reference count. Otherwise, a new lookup table
172 * entry is created that references the location of the file resource in the
173 * source WIM file through the other_wim_fp field of the lookup table entry.
175 static int add_lookup_table_entry_to_dest_wim(struct dentry *dentry, void *arg)
177 WIMStruct *src_wim, *dest_wim;
178 struct lookup_table_entry *src_table_entry;
179 struct lookup_table_entry *dest_table_entry;
181 src_wim = ((struct wim_pair*)arg)->src_wim;
182 dest_wim = ((struct wim_pair*)arg)->dest_wim;
184 if (dentry_is_directory(dentry))
187 src_table_entry = wim_lookup_resource(src_wim, dentry);
188 if (!src_table_entry)
191 dest_table_entry = wim_lookup_resource(dest_wim, dentry);
192 if (dest_table_entry) {
193 dest_table_entry->refcnt++;
195 dest_table_entry = new_lookup_table_entry();
196 if (!dest_table_entry)
197 return WIMLIB_ERR_NOMEM;
198 dest_table_entry->other_wim_fp = src_wim->fp;
199 dest_table_entry->other_wim_ctype =
200 wimlib_get_compression_type(src_wim);
201 dest_table_entry->refcnt = 1;
202 memcpy(&dest_table_entry->resource_entry,
203 &src_table_entry->resource_entry,
204 sizeof(struct resource_entry));
205 memcpy(dest_table_entry->hash, dentry->hash, WIM_HASH_SIZE);
206 lookup_table_insert(dest_wim->lookup_table, dest_table_entry);
212 * Adds an image (given by its dentry tree) to the image metadata array of a WIM
213 * file, adds an entry to the lookup table for the image metadata, updates the
214 * image count in the header, and selects the new image.
216 * Does not update the XML data.
218 * @w: The WIMStruct for the WIM file.
219 * @root_dentry: The root of the directory tree for the image.
221 static int add_new_dentry_tree(WIMStruct *w, struct dentry *root_dentry)
223 struct lookup_table_entry *metadata_lte;
224 struct image_metadata *imd;
225 struct image_metadata *new_imd;
226 #ifdef ENABLE_SECURITY_DATA
227 struct wim_security_data *sd;
230 DEBUG("Reallocating image metadata array for image_count = %u",
231 w->hdr.image_count + 1);
232 imd = CALLOC((w->hdr.image_count + 1), sizeof(struct image_metadata));
235 ERROR("Failed to allocate memory for new image metadata array");
236 return WIMLIB_ERR_NOMEM;
239 memcpy(imd, w->image_metadata,
240 w->hdr.image_count * sizeof(struct image_metadata));
242 metadata_lte = new_lookup_table_entry();
245 #ifdef ENABLE_SECURITY_DATA
246 sd = CALLOC(1, sizeof(struct wim_security_data));
248 goto out_free_metadata_lte;
250 sd->total_length = 8;
253 metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
254 randomize_byte_array(metadata_lte->hash, WIM_HASH_SIZE);
255 lookup_table_insert(w->lookup_table, metadata_lte);
257 w->hdr.image_count++;
259 new_imd = &imd[w->hdr.image_count - 1];
260 new_imd->metadata_lte = metadata_lte;
261 new_imd->modified = true;
262 new_imd->root_dentry = root_dentry;
263 #ifdef ENABLE_SECURITY_DATA
264 new_imd->security_data = sd;
266 FREE(w->image_metadata);
267 w->image_metadata = imd;
269 /* Change the current image to the new one. */
270 return wimlib_select_image(w, w->hdr.image_count);
271 out_free_metadata_lte:
275 return WIMLIB_ERR_NOMEM;
280 * Copies an image, or all the images, from a WIM file, into another WIM file.
282 WIMLIBAPI int wimlib_export_image(WIMStruct *src_wim,
285 const char *dest_name,
286 const char *dest_description,
292 struct wim_pair wims;
294 if (src_image == WIM_ALL_IMAGES) {
295 if (src_wim->hdr.image_count > 1) {
297 /* multi-image export. */
299 if ((flags & WIMLIB_EXPORT_FLAG_BOOT) &&
300 (src_wim->hdr.boot_idx == 0))
302 /* Specifying the boot flag on a multi-image
303 * source WIM makes the boot index default to
304 * the bootable image in the source WIM. It is
305 * an error if there is no such bootable image.
307 ERROR("Cannot specify `boot' flag when "
308 "exporting multiple images from a WIM "
309 "with no bootable images");
310 return WIMLIB_ERR_INVALID_PARAM;
312 if (dest_name || dest_description) {
313 ERROR("Image name or image description was "
314 "specified, but we are exporting "
316 return WIMLIB_ERR_INVALID_PARAM;
318 for (i = 1; i <= src_wim->hdr.image_count; i++) {
319 int export_flags = flags;
321 if (i != src_wim->hdr.boot_idx)
322 export_flags &= ~WIMLIB_EXPORT_FLAG_BOOT;
324 ret = wimlib_export_image(src_wim, i, dest_wim,
337 ret = wimlib_select_image(src_wim, src_image);
339 ERROR("Could not select image %d from the WIM `%s' "
340 "to export it", src_image, src_wim->filename);
345 dest_name = wimlib_get_image_name(src_wim, src_image);
346 DEBUG("Using name `%s' for source image %d",
347 dest_name, src_image);
350 DEBUG("Exporting image %d from `%s'", src_image, src_wim->filename);
352 if (wimlib_image_name_in_use(dest_wim, dest_name)) {
353 ERROR("There is already an image named `%s' in the "
354 "destination WIM", dest_name);
355 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
359 /* Cleaning up here on failure would be hard. For example, we could
360 * fail to allocate memory in add_lookup_table_entry_to_dest_wim(),
361 * leaving the lookup table entries in the destination WIM in an
362 * inconsistent state. Until these issues can be resolved,
363 * wimlib_export_image() is documented as leaving dest_wim is an
364 * indeterminate state. */
365 root = wim_root_dentry(src_wim);
366 for_dentry_in_tree(root, increment_dentry_refcnt, NULL);
367 wims.src_wim = src_wim;
368 wims.dest_wim = dest_wim;
369 ret = for_dentry_in_tree(root, add_lookup_table_entry_to_dest_wim, &wims);
372 ret = add_new_dentry_tree(dest_wim, root);
375 #ifdef ENABLE_SECURITY_DATA
376 /* Bring over old security data */
377 struct wim_security_data *sd = wim_security_data(src_wim);
378 struct image_metadata *new_imd = wim_get_current_image_metadata(dest_wim);
380 free_security_data(new_imd->security_data);
381 new_imd->security_data = sd;
385 if (flags & WIMLIB_EXPORT_FLAG_BOOT) {
386 DEBUG("Setting boot_idx to %d", dest_wim->hdr.image_count);
387 dest_wim->hdr.boot_idx = dest_wim->hdr.image_count;
390 return xml_export_image(src_wim->wim_info, src_image, &dest_wim->wim_info,
391 dest_name, dest_description);
395 * Deletes an image from the WIM.
397 WIMLIBAPI int wimlib_delete_image(WIMStruct *w, int image)
402 struct image_metadata *imd;
404 if (image == WIM_ALL_IMAGES) {
405 num_images = w->hdr.image_count;
406 for (i = 1; i <= num_images; i++) {
407 /* Always delete the first image, since by the end
408 * there won't be any more than that! */
409 ret = wimlib_delete_image(w, 1);
416 DEBUG("Deleting image %d", image);
418 /* Even if the dentry tree is not allocated, we must select it (and
419 * therefore allocate it) so that we can decrement the reference counts
420 * in the lookup table. */
421 ret = wimlib_select_image(w, image);
425 /* Free the dentry tree, any lookup table entries that have their
426 * refcnt decremented to 0, and the security data. */
427 destroy_image_metadata(wim_get_current_image_metadata(w),
430 /* Get rid of the empty slot in the image metadata array. */
431 memmove(&w->image_metadata[image - 1], &w->image_metadata[image],
432 (w->hdr.image_count - image) * sizeof(struct image_metadata));
434 /* Decrement the image count. */
435 if (--w->hdr.image_count == 0) {
436 FREE(w->image_metadata);
437 w->image_metadata = NULL;
440 /* Fix the boot index. */
441 if (w->hdr.boot_idx == image)
443 else if (w->hdr.boot_idx > image)
446 w->current_image = WIM_NO_IMAGE;
448 /* Remove the image from the XML information. */
449 xml_delete_image(&w->wim_info, image);
454 * Adds an image to a WIM file from a directory tree on disk.
456 WIMLIBAPI int wimlib_add_image(WIMStruct *w, const char *dir,
457 const char *name, const char *description,
458 const char *flags_element, int flags)
460 struct dentry *root_dentry;
461 struct stat root_stat;
462 struct image_metadata *imd;
465 DEBUG("Adding dentry tree from dir `%s'.", dir);
467 if (!name || !*name) {
468 ERROR("Must specify a non-empty string for the image name");
469 return WIMLIB_ERR_INVALID_PARAM;
472 ERROR("Must specify the name of a directory");
473 return WIMLIB_ERR_INVALID_PARAM;
476 if (wimlib_image_name_in_use(w, name)) {
477 ERROR("There is already an image named \"%s\" in `%s'",
479 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
482 DEBUG("Creating root dentry.");
484 root_dentry = new_dentry("");
486 return WIMLIB_ERR_NOMEM;
487 ret = calculate_dentry_full_path(root_dentry, NULL);
490 goto out_free_dentry_tree;
492 root_dentry->attributes |= WIM_FILE_ATTRIBUTE_DIRECTORY;
494 /* Construct the dentry tree from the outside filesystem. */
495 if (stat(dir, &root_stat) != 0) {
496 ERROR_WITH_ERRNO("Failed to stat `%s'", dir);
497 ret = WIMLIB_ERR_STAT;
498 goto out_free_dentry_tree;
500 if (!S_ISDIR(root_stat.st_mode)) {
501 ERROR("`%s' is not a directory", dir);
502 ret = WIMLIB_ERR_NOTDIR;
503 goto out_free_dentry_tree;
505 DEBUG("Building dentry tree.");
506 ret = build_dentry_tree(root_dentry, dir, &root_stat, w->lookup_table);
509 ERROR("Failed to build dentry tree for `%s'", dir);
510 goto out_free_dentry_tree;
513 DEBUG("Recalculating full paths of dentries.");
514 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
516 goto out_free_dentry_tree;
518 ret = add_new_dentry_tree(w, root_dentry);
520 goto out_free_dentry_tree;
522 if (flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
523 wimlib_set_boot_idx(w, w->hdr.image_count);
525 ret = xml_add_image(w, root_dentry, name, description, flags_element);
527 goto out_destroy_imd;
531 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
534 out_free_dentry_tree:
535 free_dentry_tree(root_dentry, w->lookup_table, true);