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 free_security_data(imd->security_data);
46 /* Get rid of the lookup table entry for this image's metadata resource
48 lookup_table_remove(lt, imd->metadata_lte);
52 * Recursively builds a dentry tree from a directory tree on disk, outside the
55 * @root: A dentry that has already been created for the root of the dentry
57 * @source_path: The path to the root of the tree on disk.
58 * @root_stat: A pointer to a `struct stat' that contains the metadata for the
59 * root of the tree on disk.
60 * @lookup_table: The lookup table for the WIM file. For each file added to the
61 * dentry tree being built, an entry is added to the lookup table,
62 * unless an identical file is already in the lookup table. These
63 * lookup table entries that are added point to the file on disk.
65 * @return: 0 on success, nonzero on failure. It is a failure if any of
66 * the files cannot be `stat'ed, or if any of the needed
67 * directories cannot be opened or read. Failure to add the files
68 * to the WIM may still occur later when trying to actually read
69 * the regular files in the tree into the WIM as file resources.
71 static int build_dentry_tree(struct dentry *root, const char *source_path,
72 struct stat *root_stat,
73 struct lookup_table* lookup_table)
77 stbuf_to_dentry(root_stat, root);
78 if (dentry_is_directory(root)) {
79 /* Open the directory on disk */
82 struct stat child_stat;
85 dir = opendir(source_path);
87 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
89 return WIMLIB_ERR_OPEN;
92 /* Buffer for names of files in directory. */
93 size_t len = strlen(source_path);
94 char name[len + 1 + FILENAME_MAX + 1];
95 memcpy(name, source_path, len);
98 /* Create a dentry for each entry in the directory on disk, and recurse
99 * to any subdirectories. */
100 while ((p = readdir(dir)) != NULL) {
101 if (p->d_name[0] == '.' && (p->d_name[1] == '\0'
102 || (p->d_name[1] == '.' && p->d_name[2] == '\0')))
104 strcpy(name + len + 1, p->d_name);
105 if (stat(name, &child_stat) != 0) {
106 ERROR_WITH_ERRNO("Cannot stat `%s'", name);
107 ret = WIMLIB_ERR_STAT;
110 child = new_dentry(p->d_name);
112 ERROR("No memory to allocate new dentry");
113 return WIMLIB_ERR_NOMEM;
115 ret = build_dentry_tree(child, name, &child_stat,
117 link_dentry(child, root);
123 struct lookup_table_entry *lte;
125 /* For each non-directory, we must check to see if the file is
126 * in the lookup table already; if it is, we increment its
127 * refcnt; otherwise, we create a new lookup table entry and
129 ret = sha1sum(source_path, root->hash);
133 lte = lookup_resource(lookup_table, root->hash);
137 char *file_on_disk = STRDUP(source_path);
139 ERROR("Failed to allocate memory for file path");
140 return WIMLIB_ERR_NOMEM;
142 lte = new_lookup_table_entry();
145 return WIMLIB_ERR_NOMEM;
147 lte->file_on_disk = file_on_disk;
148 lte->resource_entry.flags = 0;
150 lte->part_number = 1;
151 lte->resource_entry.original_size = root_stat->st_size;
152 lte->resource_entry.size = root_stat->st_size;
153 memcpy(lte->hash, root->hash, WIM_HASH_SIZE);
154 lookup_table_insert(lookup_table, lte);
166 * This function takes in a dentry that was previously located only in image(s)
167 * in @src_wim, but now is being added to @dest_wim. If there is in fact already a
168 * lookup table entry for this file in the lookup table of the destination WIM
169 * file, we simply increment its reference count. Otherwise, a new lookup table
170 * entry is created that references the location of the file resource in the
171 * source WIM file through the other_wim_fp field of the lookup table entry.
173 static int add_lookup_table_entry_to_dest_wim(struct dentry *dentry, void *arg)
175 WIMStruct *src_wim, *dest_wim;
176 struct lookup_table_entry *src_table_entry;
177 struct lookup_table_entry *dest_table_entry;
179 src_wim = ((struct wim_pair*)arg)->src_wim;
180 dest_wim = ((struct wim_pair*)arg)->dest_wim;
182 if (dentry_is_directory(dentry))
185 src_table_entry = wim_lookup_resource(src_wim, dentry);
186 if (!src_table_entry)
189 dest_table_entry = wim_lookup_resource(dest_wim, dentry);
190 if (dest_table_entry) {
191 dest_table_entry->refcnt++;
193 dest_table_entry = new_lookup_table_entry();
194 if (!dest_table_entry)
195 return WIMLIB_ERR_NOMEM;
196 dest_table_entry->other_wim_fp = src_wim->fp;
197 dest_table_entry->other_wim_ctype =
198 wimlib_get_compression_type(src_wim);
199 dest_table_entry->refcnt = 1;
200 memcpy(&dest_table_entry->resource_entry,
201 &src_table_entry->resource_entry,
202 sizeof(struct resource_entry));
203 memcpy(dest_table_entry->hash, dentry->hash, WIM_HASH_SIZE);
204 lookup_table_insert(dest_wim->lookup_table, dest_table_entry);
210 * Adds an image (given by its dentry tree) to the image metadata array of a WIM
211 * file, adds an entry to the lookup table for the image metadata, updates the
212 * image count in the header, and selects the new image.
214 * Does not update the XML data.
216 * @w: The WIMStruct for the WIM file.
217 * @root_dentry: The root of the directory tree for the image.
219 static int add_new_dentry_tree(WIMStruct *w, struct dentry *root_dentry)
221 struct lookup_table_entry *metadata_lte;
222 struct image_metadata *imd;
223 struct image_metadata *new_imd;
224 struct wim_security_data *sd;
226 DEBUG("Reallocating image metadata array for image_count = %u",
227 w->hdr.image_count + 1);
228 imd = CALLOC((w->hdr.image_count + 1), sizeof(struct image_metadata));
231 ERROR("Failed to allocate memory for new image metadata array");
232 return WIMLIB_ERR_NOMEM;
235 memcpy(imd, w->image_metadata,
236 w->hdr.image_count * sizeof(struct image_metadata));
238 metadata_lte = new_lookup_table_entry();
241 sd = CALLOC(1, sizeof(struct wim_security_data));
243 goto out_free_metadata_lte;
245 sd->total_length = 8;
247 metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
248 randomize_byte_array(metadata_lte->hash, WIM_HASH_SIZE);
249 lookup_table_insert(w->lookup_table, metadata_lte);
251 w->hdr.image_count++;
253 new_imd = &imd[w->hdr.image_count - 1];
254 new_imd->metadata_lte = metadata_lte;
255 new_imd->modified = true;
256 new_imd->root_dentry = root_dentry;
257 new_imd->security_data = sd;
258 FREE(w->image_metadata);
259 w->image_metadata = imd;
261 /* Change the current image to the new one. */
262 return wimlib_select_image(w, w->hdr.image_count);
263 out_free_metadata_lte:
267 return WIMLIB_ERR_NOMEM;
272 * Copies an image, or all the images, from a WIM file, into another WIM file.
274 WIMLIBAPI int wimlib_export_image(WIMStruct *src_wim,
277 const char *dest_name,
278 const char *dest_description,
284 struct wim_pair wims;
286 if (src_image == WIM_ALL_IMAGES) {
287 if (src_wim->hdr.image_count > 1) {
289 /* multi-image export. */
291 if ((flags & WIMLIB_EXPORT_FLAG_BOOT) &&
292 (src_wim->hdr.boot_idx == 0))
294 /* Specifying the boot flag on a multi-image
295 * source WIM makes the boot index default to
296 * the bootable image in the source WIM. It is
297 * an error if there is no such bootable image.
299 ERROR("Cannot specify `boot' flag when "
300 "exporting multiple images from a WIM "
301 "with no bootable images");
302 return WIMLIB_ERR_INVALID_PARAM;
304 if (dest_name || dest_description) {
305 ERROR("Image name or image description was "
306 "specified, but we are exporting "
308 return WIMLIB_ERR_INVALID_PARAM;
310 for (i = 1; i <= src_wim->hdr.image_count; i++) {
311 int export_flags = flags;
313 if (i != src_wim->hdr.boot_idx)
314 export_flags &= ~WIMLIB_EXPORT_FLAG_BOOT;
316 ret = wimlib_export_image(src_wim, i, dest_wim,
329 ret = wimlib_select_image(src_wim, src_image);
331 ERROR("Could not select image %d from the WIM `%s' "
332 "to export it", src_image, src_wim->filename);
337 dest_name = wimlib_get_image_name(src_wim, src_image);
338 DEBUG("Using name `%s' for source image %d",
339 dest_name, src_image);
342 DEBUG("Exporting image %d from `%s'", src_image, src_wim->filename);
344 if (wimlib_image_name_in_use(dest_wim, dest_name)) {
345 ERROR("There is already an image named `%s' in the "
346 "destination WIM", dest_name);
347 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
351 /* Cleaning up here on failure would be hard. For example, we could
352 * fail to allocate memory in add_lookup_table_entry_to_dest_wim(),
353 * leaving the lookup table entries in the destination WIM in an
354 * inconsistent state. Until these issues can be resolved,
355 * wimlib_export_image() is documented as leaving dest_wim is an
356 * indeterminate state. */
357 root = wim_root_dentry(src_wim);
358 for_dentry_in_tree(root, increment_dentry_refcnt, NULL);
359 wims.src_wim = src_wim;
360 wims.dest_wim = dest_wim;
361 ret = for_dentry_in_tree(root, add_lookup_table_entry_to_dest_wim, &wims);
364 ret = add_new_dentry_tree(dest_wim, root);
367 /* Bring over old security data */
368 struct wim_security_data *sd = wim_security_data(src_wim);
369 struct image_metadata *new_imd = wim_get_current_image_metadata(dest_wim);
371 free_security_data(new_imd->security_data);
372 new_imd->security_data = sd;
375 if (flags & WIMLIB_EXPORT_FLAG_BOOT) {
376 DEBUG("Setting boot_idx to %d", dest_wim->hdr.image_count);
377 dest_wim->hdr.boot_idx = dest_wim->hdr.image_count;
380 return xml_export_image(src_wim->wim_info, src_image, &dest_wim->wim_info,
381 dest_name, dest_description);
385 * Deletes an image from the WIM.
387 WIMLIBAPI int wimlib_delete_image(WIMStruct *w, int image)
393 if (image == WIM_ALL_IMAGES) {
394 num_images = w->hdr.image_count;
395 for (i = 1; i <= num_images; i++) {
396 /* Always delete the first image, since by the end
397 * there won't be any more than that! */
398 ret = wimlib_delete_image(w, 1);
405 DEBUG("Deleting image %d", image);
407 /* Even if the dentry tree is not allocated, we must select it (and
408 * therefore allocate it) so that we can decrement the reference counts
409 * in the lookup table. */
410 ret = wimlib_select_image(w, image);
414 /* Free the dentry tree, any lookup table entries that have their
415 * refcnt decremented to 0, and the security data. */
416 destroy_image_metadata(wim_get_current_image_metadata(w),
419 /* Get rid of the empty slot in the image metadata array. */
420 memmove(&w->image_metadata[image - 1], &w->image_metadata[image],
421 (w->hdr.image_count - image) * sizeof(struct image_metadata));
423 /* Decrement the image count. */
424 if (--w->hdr.image_count == 0) {
425 FREE(w->image_metadata);
426 w->image_metadata = NULL;
429 /* Fix the boot index. */
430 if (w->hdr.boot_idx == image)
432 else if (w->hdr.boot_idx > image)
435 w->current_image = WIM_NO_IMAGE;
437 /* Remove the image from the XML information. */
438 xml_delete_image(&w->wim_info, image);
443 * Adds an image to a WIM file from a directory tree on disk.
445 WIMLIBAPI int wimlib_add_image(WIMStruct *w, const char *dir,
446 const char *name, const char *description,
447 const char *flags_element, int flags)
449 struct dentry *root_dentry;
450 struct stat root_stat;
451 struct image_metadata *imd;
454 DEBUG("Adding dentry tree from dir `%s'.", dir);
456 if (!name || !*name) {
457 ERROR("Must specify a non-empty string for the image name");
458 return WIMLIB_ERR_INVALID_PARAM;
461 ERROR("Must specify the name of a directory");
462 return WIMLIB_ERR_INVALID_PARAM;
465 if (wimlib_image_name_in_use(w, name)) {
466 ERROR("There is already an image named \"%s\" in `%s'",
468 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
471 DEBUG("Creating root dentry.");
473 root_dentry = new_dentry("");
475 return WIMLIB_ERR_NOMEM;
476 ret = calculate_dentry_full_path(root_dentry, NULL);
479 goto out_free_dentry_tree;
481 root_dentry->attributes |= FILE_ATTRIBUTE_DIRECTORY;
483 /* Construct the dentry tree from the outside filesystem. */
484 if (stat(dir, &root_stat) != 0) {
485 ERROR_WITH_ERRNO("Failed to stat `%s'", dir);
486 ret = WIMLIB_ERR_STAT;
487 goto out_free_dentry_tree;
489 if (!S_ISDIR(root_stat.st_mode)) {
490 ERROR("`%s' is not a directory", dir);
491 ret = WIMLIB_ERR_NOTDIR;
492 goto out_free_dentry_tree;
494 DEBUG("Building dentry tree.");
495 ret = build_dentry_tree(root_dentry, dir, &root_stat, w->lookup_table);
498 ERROR("Failed to build dentry tree for `%s'", dir);
499 goto out_free_dentry_tree;
502 DEBUG("Recalculating full paths of dentries.");
503 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
505 goto out_free_dentry_tree;
507 ret = add_new_dentry_tree(w, root_dentry);
509 goto out_free_dentry_tree;
511 if (flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
512 wimlib_set_boot_idx(w, w->hdr.image_count);
514 ret = xml_add_image(w, root_dentry, name, description, flags_element);
516 goto out_destroy_imd;
520 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
523 out_free_dentry_tree:
524 free_dentry_tree(root_dentry, w->lookup_table, true);