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)
48 free_dentry_tree(imd->root_dentry, lt, true);
50 free_dentry_tree(imd->root_dentry, NULL, false);
51 free_security_data(imd->security_data);
52 free_link_group_table(imd->lgt);
54 /* Get rid of the lookup table entry for this image's metadata resource
57 lookup_table_remove(lt, imd->metadata_lte);
61 * Recursively builds a dentry tree from a directory tree on disk, outside the
64 * @root: A dentry that has already been created for the root of the dentry
66 * @root_disk_path: The path to the root of the tree on disk.
67 * @lookup_table: The lookup table for the WIM file. For each file added to the
68 * dentry tree being built, an entry is added to the lookup table,
69 * unless an identical file is already in the lookup table. These
70 * lookup table entries that are added point to the file on disk.
72 * @return: 0 on success, nonzero on failure. It is a failure if any of
73 * the files cannot be `stat'ed, or if any of the needed
74 * directories cannot be opened or read. Failure to add the files
75 * to the WIM may still occur later when trying to actually read
76 * the regular files in the tree into the WIM as file resources.
78 static int build_dentry_tree(struct dentry *root, const char *root_disk_path,
79 struct lookup_table* lookup_table,
82 DEBUG("%s", root_disk_path);
83 struct stat root_stbuf;
85 int (*stat_fn)(const char *restrict, struct stat *restrict);
87 if (add_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
93 ret = (*stat_fn)(root_disk_path, &root_stbuf);
95 ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
96 return WIMLIB_ERR_STAT;
99 if ((add_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
100 !S_ISDIR(root_stbuf.st_mode)) {
101 ERROR("`%s' is not a directory", root_disk_path);
102 return WIMLIB_ERR_NOTDIR;
104 stbuf_to_dentry(&root_stbuf, root);
105 add_flags &= ~WIMLIB_ADD_IMAGE_FLAG_ROOT;
107 if (dentry_is_directory(root)) {
108 /* Open the directory on disk */
111 struct dentry *child;
113 dir = opendir(root_disk_path);
115 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
117 return WIMLIB_ERR_OPEN;
120 /* Buffer for names of files in directory. */
121 size_t len = strlen(root_disk_path);
122 char name[len + 1 + FILENAME_MAX + 1];
123 memcpy(name, root_disk_path, len);
126 /* Create a dentry for each entry in the directory on disk, and recurse
127 * to any subdirectories. */
128 while ((p = readdir(dir)) != NULL) {
129 if (p->d_name[0] == '.' && (p->d_name[1] == '\0'
130 || (p->d_name[1] == '.' && p->d_name[2] == '\0')))
132 strcpy(name + len + 1, p->d_name);
133 child = new_dentry(p->d_name);
135 return WIMLIB_ERR_NOMEM;
136 ret = build_dentry_tree(child, name, lookup_table,
138 link_dentry(child, root);
143 } else if (dentry_is_symlink(root)) {
144 /* Archiving a symbolic link */
145 size_t symlink_buf_len;
146 char deref_name_buf[4096];
147 ssize_t ret = readlink(root_disk_path, deref_name_buf,
148 sizeof(deref_name_buf) - 1);
150 ERROR_WITH_ERRNO("Failed to read target of "
151 "symbolic link `%s'", root_disk_path);
152 return WIMLIB_ERR_STAT;
154 deref_name_buf[ret] = '\0';
155 DEBUG("Read symlink `%s'", deref_name_buf);
156 ret = dentry_set_symlink(root, deref_name_buf,
160 struct lookup_table_entry *lte;
162 /* For each regular file, we must check to see if the file is in
163 * the lookup table already; if it is, we increment its refcnt;
164 * otherwise, we create a new lookup table entry and insert it.
166 ret = sha1sum(root_disk_path, root->hash);
170 lte = __lookup_resource(lookup_table, root->hash);
174 char *file_on_disk = STRDUP(root_disk_path);
176 ERROR("Failed to allocate memory for file path");
177 return WIMLIB_ERR_NOMEM;
179 lte = new_lookup_table_entry();
182 return WIMLIB_ERR_NOMEM;
184 lte->file_on_disk = file_on_disk;
185 lte->resource_entry.original_size = root_stbuf.st_size;
186 lte->resource_entry.size = root_stbuf.st_size;
187 memcpy(lte->hash, root->hash, WIM_HASH_SIZE);
188 lookup_table_insert(lookup_table, lte);
200 * This function takes in a dentry that was previously located only in image(s)
201 * in @src_wim, but now is being added to @dest_wim. If there is in fact already a
202 * lookup table entry for this file in the lookup table of the destination WIM
203 * file, we simply increment its reference count. Otherwise, a new lookup table
204 * entry is created that references the location of the file resource in the
205 * source WIM file through the other_wim_fp field of the lookup table entry.
207 static int add_lookup_table_entry_to_dest_wim(struct dentry *dentry, void *arg)
209 WIMStruct *src_wim, *dest_wim;
210 struct lookup_table_entry *src_table_entry;
211 struct lookup_table_entry *dest_table_entry;
213 src_wim = ((struct wim_pair*)arg)->src_wim;
214 dest_wim = ((struct wim_pair*)arg)->dest_wim;
216 if (dentry_is_directory(dentry))
220 src_table_entry = __lookup_resource(src_wim->lookup_table, dentry->hash);
221 if (!src_table_entry)
225 dest_table_entry = __lookup_resource(dest_wim->lookup_table, dentry->hash);
226 if (dest_table_entry) {
227 dest_table_entry->refcnt++;
229 dest_table_entry = new_lookup_table_entry();
230 if (!dest_table_entry)
231 return WIMLIB_ERR_NOMEM;
232 dest_table_entry->other_wim_fp = src_wim->fp;
233 dest_table_entry->other_wim_ctype =
234 wimlib_get_compression_type(src_wim);
235 dest_table_entry->refcnt = 1;
236 memcpy(&dest_table_entry->resource_entry,
237 &src_table_entry->resource_entry,
238 sizeof(struct resource_entry));
239 memcpy(dest_table_entry->hash, dentry->hash, WIM_HASH_SIZE);
240 lookup_table_insert(dest_wim->lookup_table, dest_table_entry);
246 * Adds an image (given by its dentry tree) to the image metadata array of a WIM
247 * file, adds an entry to the lookup table for the image metadata, updates the
248 * image count in the header, and selects the new image.
250 * Does not update the XML data.
252 * @w: The WIMStruct for the WIM file.
253 * @root_dentry: The root of the directory tree for the image.
255 static int add_new_dentry_tree(WIMStruct *w, struct dentry *root_dentry)
257 struct lookup_table_entry *metadata_lte;
258 struct image_metadata *imd;
259 struct image_metadata *new_imd;
260 struct wim_security_data *sd;
261 struct link_group_table *lgt;
263 DEBUG("Reallocating image metadata array for image_count = %u",
264 w->hdr.image_count + 1);
265 imd = CALLOC((w->hdr.image_count + 1), sizeof(struct image_metadata));
268 ERROR("Failed to allocate memory for new image metadata array");
269 return WIMLIB_ERR_NOMEM;
272 memcpy(imd, w->image_metadata,
273 w->hdr.image_count * sizeof(struct image_metadata));
275 metadata_lte = new_lookup_table_entry();
278 sd = CALLOC(1, sizeof(struct wim_security_data));
280 goto out_free_metadata_lte;
282 sd->total_length = 8;
284 lgt = new_link_group_table(9001);
286 goto out_free_security_data;
288 metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
289 randomize_byte_array(metadata_lte->hash, WIM_HASH_SIZE);
290 lookup_table_insert(w->lookup_table, metadata_lte);
292 new_imd = &imd[w->hdr.image_count];
294 new_imd->root_dentry = root_dentry;
295 new_imd->metadata_lte = metadata_lte;
296 new_imd->security_data = sd;
298 new_imd->modified = true;
300 FREE(w->image_metadata);
301 w->image_metadata = imd;
302 w->hdr.image_count++;
304 /* Change the current image to the new one. */
305 return wimlib_select_image(w, w->hdr.image_count);
306 out_free_security_data:
308 out_free_metadata_lte:
312 return WIMLIB_ERR_NOMEM;
317 * Copies an image, or all the images, from a WIM file, into another WIM file.
319 WIMLIBAPI int wimlib_export_image(WIMStruct *src_wim,
322 const char *dest_name,
323 const char *dest_description,
329 struct wim_pair wims;
331 if (src_image == WIM_ALL_IMAGES) {
332 if (src_wim->hdr.image_count > 1) {
334 /* multi-image export. */
336 if ((flags & WIMLIB_EXPORT_FLAG_BOOT) &&
337 (src_wim->hdr.boot_idx == 0))
339 /* Specifying the boot flag on a multi-image
340 * source WIM makes the boot index default to
341 * the bootable image in the source WIM. It is
342 * an error if there is no such bootable image.
344 ERROR("Cannot specify `boot' flag when "
345 "exporting multiple images from a WIM "
346 "with no bootable images");
347 return WIMLIB_ERR_INVALID_PARAM;
349 if (dest_name || dest_description) {
350 ERROR("Image name or image description was "
351 "specified, but we are exporting "
353 return WIMLIB_ERR_INVALID_PARAM;
355 for (i = 1; i <= src_wim->hdr.image_count; i++) {
356 int export_flags = flags;
358 if (i != src_wim->hdr.boot_idx)
359 export_flags &= ~WIMLIB_EXPORT_FLAG_BOOT;
361 ret = wimlib_export_image(src_wim, i, dest_wim,
374 ret = wimlib_select_image(src_wim, src_image);
376 ERROR("Could not select image %d from the WIM `%s' "
377 "to export it", src_image, src_wim->filename);
382 dest_name = wimlib_get_image_name(src_wim, src_image);
383 DEBUG("Using name `%s' for source image %d",
384 dest_name, src_image);
387 DEBUG("Exporting image %d from `%s'", src_image, src_wim->filename);
389 if (wimlib_image_name_in_use(dest_wim, dest_name)) {
390 ERROR("There is already an image named `%s' in the "
391 "destination WIM", dest_name);
392 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
396 /* Cleaning up here on failure would be hard. For example, we could
397 * fail to allocate memory in add_lookup_table_entry_to_dest_wim(),
398 * leaving the lookup table entries in the destination WIM in an
399 * inconsistent state. Until these issues can be resolved,
400 * wimlib_export_image() is documented as leaving dest_wim is an
401 * indeterminate state. */
402 root = wim_root_dentry(src_wim);
403 for_dentry_in_tree(root, increment_dentry_refcnt, NULL);
404 wims.src_wim = src_wim;
405 wims.dest_wim = dest_wim;
406 ret = for_dentry_in_tree(root, add_lookup_table_entry_to_dest_wim, &wims);
409 ret = add_new_dentry_tree(dest_wim, root);
412 /* Bring over old security data */
413 struct wim_security_data *sd = wim_security_data(src_wim);
414 struct image_metadata *new_imd = wim_get_current_image_metadata(dest_wim);
416 free_security_data(new_imd->security_data);
417 new_imd->security_data = sd;
420 if (flags & WIMLIB_EXPORT_FLAG_BOOT) {
421 DEBUG("Setting boot_idx to %d", dest_wim->hdr.image_count);
422 dest_wim->hdr.boot_idx = dest_wim->hdr.image_count;
425 return xml_export_image(src_wim->wim_info, src_image, &dest_wim->wim_info,
426 dest_name, dest_description);
430 * Deletes an image from the WIM.
432 WIMLIBAPI int wimlib_delete_image(WIMStruct *w, int image)
438 if (image == WIM_ALL_IMAGES) {
439 num_images = w->hdr.image_count;
440 for (i = 1; i <= num_images; i++) {
441 /* Always delete the first image, since by the end
442 * there won't be any more than that! */
443 ret = wimlib_delete_image(w, 1);
450 DEBUG("Deleting image %d", image);
452 /* Even if the dentry tree is not allocated, we must select it (and
453 * therefore allocate it) so that we can decrement the reference counts
454 * in the lookup table. */
455 ret = wimlib_select_image(w, image);
459 /* Free the dentry tree, any lookup table entries that have their
460 * refcnt decremented to 0, and the security data. */
461 destroy_image_metadata(wim_get_current_image_metadata(w),
464 /* Get rid of the empty slot in the image metadata array. */
465 memmove(&w->image_metadata[image - 1], &w->image_metadata[image],
466 (w->hdr.image_count - image) * sizeof(struct image_metadata));
468 /* Decrement the image count. */
469 if (--w->hdr.image_count == 0) {
470 FREE(w->image_metadata);
471 w->image_metadata = NULL;
474 /* Fix the boot index. */
475 if (w->hdr.boot_idx == image)
477 else if (w->hdr.boot_idx > image)
480 w->current_image = WIM_NO_IMAGE;
482 /* Remove the image from the XML information. */
483 xml_delete_image(&w->wim_info, image);
488 * Adds an image to a WIM file from a directory tree on disk.
490 WIMLIBAPI int wimlib_add_image(WIMStruct *w, const char *dir,
491 const char *name, const char *description,
492 const char *flags_element, int flags)
494 struct dentry *root_dentry;
495 struct image_metadata *imd;
498 DEBUG("Adding dentry tree from dir `%s'.", dir);
500 if (!name || !*name) {
501 ERROR("Must specify a non-empty string for the image name");
502 return WIMLIB_ERR_INVALID_PARAM;
505 ERROR("Must specify the name of a directory");
506 return WIMLIB_ERR_INVALID_PARAM;
509 if (wimlib_image_name_in_use(w, name)) {
510 ERROR("There is already an image named \"%s\" in `%s'",
512 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
515 DEBUG("Creating root dentry.");
517 root_dentry = new_dentry("");
519 return WIMLIB_ERR_NOMEM;
520 ret = calculate_dentry_full_path(root_dentry, NULL);
523 goto out_free_dentry_tree;
525 root_dentry->attributes |= FILE_ATTRIBUTE_DIRECTORY;
527 DEBUG("Building dentry tree.");
528 ret = build_dentry_tree(root_dentry, dir, w->lookup_table,
529 flags | WIMLIB_ADD_IMAGE_FLAG_ROOT);
532 ERROR("Failed to build dentry tree for `%s'", dir);
533 goto out_free_dentry_tree;
536 DEBUG("Recalculating full paths of dentries.");
537 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
539 goto out_free_dentry_tree;
541 ret = add_new_dentry_tree(w, root_dentry);
543 goto out_free_dentry_tree;
545 DEBUG("Inserting dentries into hard link group table");
546 ret = for_dentry_in_tree(root_dentry, link_group_table_insert,
547 w->image_metadata[w->hdr.image_count - 1].lgt);
549 goto out_destroy_imd;
550 DEBUG("Assigning hard link groups");
551 assign_link_groups(w->image_metadata[w->hdr.image_count - 1].lgt);
553 if (flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
554 wimlib_set_boot_idx(w, w->hdr.image_count);
556 ret = xml_add_image(w, root_dentry, name, description, flags_element);
558 goto out_destroy_imd;
562 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
564 w->hdr.image_count--;
566 out_free_dentry_tree:
567 free_dentry_tree(root_dentry, w->lookup_table, true);