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"
41 /** Private flag: Used to mark that we currently adding the root directory of
43 #define WIMLIB_ADD_IMAGE_FLAG_ROOT 0x80000000
45 void destroy_image_metadata(struct image_metadata *imd,struct lookup_table *lt)
47 free_dentry_tree(imd->root_dentry, lt);
48 free_security_data(imd->security_data);
49 free_link_group_table(imd->lgt);
51 /* Get rid of the lookup table entry for this image's metadata resource
54 lookup_table_remove(lt, imd->metadata_lte);
58 * Recursively builds a dentry tree from a directory tree on disk, outside the
61 * @root: A dentry that has already been created for the root of the dentry
63 * @root_disk_path: The path to the root of the tree on disk.
64 * @lookup_table: The lookup table for the WIM file. For each file added to the
65 * dentry tree being built, an entry is added to the lookup table,
66 * unless an identical file is already in the lookup table. These
67 * lookup table entries that are added point to the file on disk.
69 * @return: 0 on success, nonzero on failure. It is a failure if any of
70 * the files cannot be `stat'ed, or if any of the needed
71 * directories cannot be opened or read. Failure to add the files
72 * to the WIM may still occur later when trying to actually read
73 * the regular files in the tree into the WIM as file resources.
75 static int build_dentry_tree(struct dentry *root, const char *root_disk_path,
76 struct lookup_table* lookup_table,
79 DEBUG("%s", root_disk_path);
80 struct stat root_stbuf;
82 int (*stat_fn)(const char *restrict, struct stat *restrict);
84 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
90 ret = (*stat_fn)(root_disk_path, &root_stbuf);
92 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
93 return WIMLIB_ERR_STAT;
96 if ((add_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
97 !S_ISDIR(root_stbuf.st_mode)) {
98 ERROR("`%s' is not a directory", root_disk_path);
99 return WIMLIB_ERR_NOTDIR;
101 if (!S_ISREG(root_stbuf.st_mode) && !S_ISDIR(root_stbuf.st_mode)
102 && !S_ISLNK(root_stbuf.st_mode)) {
103 ERROR("`%s' is not a regular file, directory, or symbolic link.");
104 return WIMLIB_ERR_SPECIAL_FILE;
106 stbuf_to_dentry(&root_stbuf, root);
107 add_flags &= ~WIMLIB_ADD_IMAGE_FLAG_ROOT;
108 root->resolved = true;
110 if (dentry_is_directory(root)) {
111 /* Open the directory on disk */
114 struct dentry *child;
116 dir = opendir(root_disk_path);
118 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
120 return WIMLIB_ERR_OPEN;
123 /* Buffer for names of files in directory. */
124 size_t len = strlen(root_disk_path);
125 char name[len + 1 + FILENAME_MAX + 1];
126 memcpy(name, root_disk_path, len);
129 /* Create a dentry for each entry in the directory on disk, and recurse
130 * to any subdirectories. */
131 while ((p = readdir(dir)) != NULL) {
132 if (p->d_name[0] == '.' && (p->d_name[1] == '\0'
133 || (p->d_name[1] == '.' && p->d_name[2] == '\0')))
135 strcpy(name + len + 1, p->d_name);
136 child = new_dentry(p->d_name);
138 return WIMLIB_ERR_NOMEM;
139 ret = build_dentry_tree(child, name, lookup_table,
141 link_dentry(child, root);
146 } else if (dentry_is_symlink(root)) {
147 /* Archiving a symbolic link */
148 size_t symlink_buf_len;
149 char deref_name_buf[4096];
150 ssize_t deref_name_len;
152 deref_name_len = readlink(root_disk_path, deref_name_buf,
153 sizeof(deref_name_buf) - 1);
154 if (deref_name_len == -1) {
155 ERROR_WITH_ERRNO("Failed to read target of "
156 "symbolic link `%s'", root_disk_path);
157 return WIMLIB_ERR_READLINK;
159 deref_name_buf[deref_name_len] = '\0';
160 DEBUG("Read symlink `%s'", deref_name_buf);
161 ret = dentry_set_symlink(root, deref_name_buf,
165 struct lookup_table_entry *lte;
166 u8 hash[SHA1_HASH_SIZE];
168 /* For each regular file, we must check to see if the file is in
169 * the lookup table already; if it is, we increment its refcnt;
170 * otherwise, we create a new lookup table entry and insert it.
172 ret = sha1sum(root_disk_path, hash);
176 lte = __lookup_resource(lookup_table, hash);
179 DEBUG("Add lte reference %u for `%s'", lte->refcnt,
182 char *file_on_disk = STRDUP(root_disk_path);
184 ERROR("Failed to allocate memory for file path");
185 return WIMLIB_ERR_NOMEM;
187 lte = new_lookup_table_entry();
190 return WIMLIB_ERR_NOMEM;
192 lte->file_on_disk = file_on_disk;
193 lte->resource_entry.original_size = root_stbuf.st_size;
194 lte->resource_entry.size = root_stbuf.st_size;
195 copy_hash(lte->hash, hash);
196 lookup_table_insert(lookup_table, lte);
209 * This function takes in a dentry that was previously located only in image(s)
210 * in @src_wim, but now is being added to @dest_wim. If there is in fact already a
211 * lookup table entry for this file in the lookup table of the destination WIM
212 * file, we simply increment its reference count. Otherwise, a new lookup table
213 * entry is created that references the location of the file resource in the
214 * source WIM file through the other_wim_fp field of the lookup table entry.
216 static int add_lte_to_dest_wim(struct dentry *dentry, void *arg)
218 WIMStruct *src_wim, *dest_wim;
220 src_wim = ((struct wim_pair*)arg)->src_wim;
221 dest_wim = ((struct wim_pair*)arg)->dest_wim;
223 wimlib_assert(!dentry->resolved);
225 for (unsigned i = 0; i < (unsigned)dentry->num_ads + 1; i++) {
226 struct lookup_table_entry *src_lte, *dest_lte;
227 src_lte = dentry_stream_lte_unresolved(dentry, i,
228 src_wim->lookup_table);
231 dest_lte = dentry_stream_lte_unresolved(dentry, i,
232 dest_wim->lookup_table);
236 dest_lte = new_lookup_table_entry();
238 return WIMLIB_ERR_NOMEM;
239 dest_lte->other_wim_fp = src_wim->fp;
240 dest_lte->other_wim_ctype =
241 wimlib_get_compression_type(src_wim);
242 memcpy(&dest_lte->resource_entry,
243 &src_lte->resource_entry,
244 sizeof(struct resource_entry));
245 copy_hash(dest_lte->hash,
246 dentry_stream_hash_unresolved(dentry, i));
247 lookup_table_insert(dest_wim->lookup_table, dest_lte);
254 * Adds an image (given by its dentry tree) to the image metadata array of a WIM
255 * file, adds an entry to the lookup table for the image metadata, updates the
256 * image count in the header, and selects the new image.
258 * Does not update the XML data.
260 * @w: The WIMStruct for the WIM file.
261 * @root_dentry: The root of the directory tree for the image.
263 static int add_new_dentry_tree(WIMStruct *w, struct dentry *root_dentry)
265 struct lookup_table_entry *metadata_lte;
266 struct image_metadata *imd;
267 struct image_metadata *new_imd;
268 struct wim_security_data *sd;
269 struct link_group_table *lgt;
271 DEBUG("Reallocating image metadata array for image_count = %u",
272 w->hdr.image_count + 1);
273 imd = CALLOC((w->hdr.image_count + 1), sizeof(struct image_metadata));
276 ERROR("Failed to allocate memory for new image metadata array");
277 return WIMLIB_ERR_NOMEM;
280 memcpy(imd, w->image_metadata,
281 w->hdr.image_count * sizeof(struct image_metadata));
283 metadata_lte = new_lookup_table_entry();
286 sd = CALLOC(1, sizeof(struct wim_security_data));
288 goto out_free_metadata_lte;
290 sd->total_length = 8;
292 lgt = new_link_group_table(9001);
294 goto out_free_security_data;
296 metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
297 random_hash(metadata_lte->hash);
298 lookup_table_insert(w->lookup_table, metadata_lte);
300 new_imd = &imd[w->hdr.image_count];
302 new_imd->root_dentry = root_dentry;
303 new_imd->metadata_lte = metadata_lte;
304 new_imd->security_data = sd;
306 new_imd->modified = true;
308 FREE(w->image_metadata);
309 w->image_metadata = imd;
310 w->hdr.image_count++;
312 /* Change the current image to the new one. */
313 return wimlib_select_image(w, w->hdr.image_count);
314 out_free_security_data:
316 out_free_metadata_lte:
320 return WIMLIB_ERR_NOMEM;
325 * Copies an image, or all the images, from a WIM file, into another WIM file.
327 WIMLIBAPI int wimlib_export_image(WIMStruct *src_wim,
330 const char *dest_name,
331 const char *dest_description,
337 struct wim_pair wims;
339 if (src_image == WIM_ALL_IMAGES) {
340 if (src_wim->hdr.image_count > 1) {
342 /* multi-image export. */
344 if ((flags & WIMLIB_EXPORT_FLAG_BOOT) &&
345 (src_wim->hdr.boot_idx == 0))
347 /* Specifying the boot flag on a multi-image
348 * source WIM makes the boot index default to
349 * the bootable image in the source WIM. It is
350 * an error if there is no such bootable image.
352 ERROR("Cannot specify `boot' flag when "
353 "exporting multiple images from a WIM "
354 "with no bootable images");
355 return WIMLIB_ERR_INVALID_PARAM;
357 if (dest_name || dest_description) {
358 ERROR("Image name or image description was "
359 "specified, but we are exporting "
361 return WIMLIB_ERR_INVALID_PARAM;
363 for (i = 1; i <= src_wim->hdr.image_count; i++) {
364 int export_flags = flags;
366 if (i != src_wim->hdr.boot_idx)
367 export_flags &= ~WIMLIB_EXPORT_FLAG_BOOT;
369 ret = wimlib_export_image(src_wim, i, dest_wim,
382 ret = wimlib_select_image(src_wim, src_image);
384 ERROR("Could not select image %d from the WIM `%s' "
385 "to export it", src_image, src_wim->filename);
390 dest_name = wimlib_get_image_name(src_wim, src_image);
391 DEBUG("Using name `%s' for source image %d",
392 dest_name, src_image);
395 DEBUG("Exporting image %d from `%s'", src_image, src_wim->filename);
397 if (wimlib_image_name_in_use(dest_wim, dest_name)) {
398 ERROR("There is already an image named `%s' in the "
399 "destination WIM", dest_name);
400 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
404 /* Cleaning up here on failure would be hard. For example, we could
405 * fail to allocate memory in add_lte_to_dest_wim(),
406 * leaving the lookup table entries in the destination WIM in an
407 * inconsistent state. Until these issues can be resolved,
408 * wimlib_export_image() is documented as leaving dest_wim is an
409 * indeterminate state. */
410 root = wim_root_dentry(src_wim);
411 for_dentry_in_tree(root, increment_dentry_refcnt, NULL);
412 wims.src_wim = src_wim;
413 wims.dest_wim = dest_wim;
414 ret = for_dentry_in_tree(root, add_lte_to_dest_wim, &wims);
417 ret = add_new_dentry_tree(dest_wim, root);
420 /* Bring over old security data */
421 struct wim_security_data *sd = wim_security_data(src_wim);
422 struct image_metadata *new_imd = wim_get_current_image_metadata(dest_wim);
424 free_security_data(new_imd->security_data);
425 new_imd->security_data = sd;
428 if (flags & WIMLIB_EXPORT_FLAG_BOOT) {
429 DEBUG("Setting boot_idx to %d", dest_wim->hdr.image_count);
430 dest_wim->hdr.boot_idx = dest_wim->hdr.image_count;
433 return xml_export_image(src_wim->wim_info, src_image, &dest_wim->wim_info,
434 dest_name, dest_description);
438 * Deletes an image from the WIM.
440 WIMLIBAPI int wimlib_delete_image(WIMStruct *w, int image)
446 if (image == WIM_ALL_IMAGES) {
447 num_images = w->hdr.image_count;
448 for (i = 1; i <= num_images; i++) {
449 /* Always delete the first image, since by the end
450 * there won't be any more than that! */
451 ret = wimlib_delete_image(w, 1);
458 DEBUG("Deleting image %d", image);
460 /* Even if the dentry tree is not allocated, we must select it (and
461 * therefore allocate it) so that we can decrement the reference counts
462 * in the lookup table. */
463 ret = wimlib_select_image(w, image);
467 /* Free the dentry tree, any lookup table entries that have their
468 * refcnt decremented to 0, and the security data. */
469 destroy_image_metadata(wim_get_current_image_metadata(w),
472 /* Get rid of the empty slot in the image metadata array. */
473 memmove(&w->image_metadata[image - 1], &w->image_metadata[image],
474 (w->hdr.image_count - image) * sizeof(struct image_metadata));
476 /* Decrement the image count. */
477 if (--w->hdr.image_count == 0) {
478 FREE(w->image_metadata);
479 w->image_metadata = NULL;
482 /* Fix the boot index. */
483 if (w->hdr.boot_idx == image)
485 else if (w->hdr.boot_idx > image)
488 w->current_image = WIM_NO_IMAGE;
490 /* Remove the image from the XML information. */
491 xml_delete_image(&w->wim_info, image);
496 * Adds an image to a WIM file from a directory tree on disk.
498 WIMLIBAPI int wimlib_add_image(WIMStruct *w, const char *dir,
499 const char *name, const char *description,
500 const char *flags_element, int flags)
502 struct dentry *root_dentry;
503 struct image_metadata *imd;
506 DEBUG("Adding dentry tree from dir `%s'.", dir);
508 if (!name || !*name) {
509 ERROR("Must specify a non-empty string for the image name");
510 return WIMLIB_ERR_INVALID_PARAM;
513 ERROR("Must specify the name of a directory");
514 return WIMLIB_ERR_INVALID_PARAM;
517 if (wimlib_image_name_in_use(w, name)) {
518 ERROR("There is already an image named \"%s\" in `%s'",
520 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
523 DEBUG("Creating root dentry.");
525 root_dentry = new_dentry("");
527 return WIMLIB_ERR_NOMEM;
529 DEBUG("Building dentry tree.");
530 ret = build_dentry_tree(root_dentry, dir, w->lookup_table,
531 flags | WIMLIB_ADD_IMAGE_FLAG_ROOT);
534 ERROR("Failed to build dentry tree for `%s'", dir);
535 goto out_free_dentry_tree;
538 DEBUG("Calculating full paths of dentries.");
539 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
541 goto out_free_dentry_tree;
543 ret = add_new_dentry_tree(w, root_dentry);
545 goto out_free_dentry_tree;
547 DEBUG("Inserting dentries into hard link group table");
548 ret = for_dentry_in_tree(root_dentry, link_group_table_insert,
549 w->image_metadata[w->hdr.image_count - 1].lgt);
551 goto out_destroy_imd;
552 DEBUG("Assigning hard link groups");
553 assign_link_groups(w->image_metadata[w->hdr.image_count - 1].lgt);
555 if (flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
556 wimlib_set_boot_idx(w, w->hdr.image_count);
558 ret = xml_add_image(w, root_dentry, name, description, flags_element);
560 goto out_destroy_imd;
564 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
566 w->hdr.image_count--;
568 out_free_dentry_tree:
569 free_dentry_tree(root_dentry, w->lookup_table);