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_location = RESOURCE_IN_FILE_ON_DISK;
194 lte->resource_entry.original_size = root_stbuf.st_size;
195 lte->resource_entry.size = root_stbuf.st_size;
196 copy_hash(lte->hash, hash);
197 lookup_table_insert(lookup_table, lte);
210 * This function takes in a dentry that was previously located only in image(s)
211 * in @src_wim, but now is being added to @dest_wim. If there is in fact already a
212 * lookup table entry for this file in the lookup table of the destination WIM
213 * file, we simply increment its reference count. Otherwise, a new lookup table
214 * entry is created that references the location of the file resource in the
215 * source WIM file through the other_wim_fp field of the lookup table entry.
217 static int add_lte_to_dest_wim(struct dentry *dentry, void *arg)
219 WIMStruct *src_wim, *dest_wim;
221 src_wim = ((struct wim_pair*)arg)->src_wim;
222 dest_wim = ((struct wim_pair*)arg)->dest_wim;
224 wimlib_assert(!dentry->resolved);
226 for (unsigned i = 0; i < (unsigned)dentry->num_ads + 1; i++) {
227 struct lookup_table_entry *src_lte, *dest_lte;
228 src_lte = dentry_stream_lte_unresolved(dentry, i,
229 src_wim->lookup_table);
232 dest_lte = dentry_stream_lte_unresolved(dentry, i,
233 dest_wim->lookup_table);
237 dest_lte = new_lookup_table_entry();
239 return WIMLIB_ERR_NOMEM;
240 dest_lte->wim = src_wim;
241 memcpy(&dest_lte->resource_entry,
242 &src_lte->resource_entry,
243 sizeof(struct resource_entry));
244 copy_hash(dest_lte->hash,
245 dentry_stream_hash_unresolved(dentry, i));
246 lookup_table_insert(dest_wim->lookup_table, dest_lte);
253 * Adds an image (given by its dentry tree) to the image metadata array of a WIM
254 * file, adds an entry to the lookup table for the image metadata, updates the
255 * image count in the header, and selects the new image.
257 * Does not update the XML data.
259 * @w: The WIMStruct for the WIM file.
260 * @root_dentry: The root of the directory tree for the image.
262 static int add_new_dentry_tree(WIMStruct *w, struct dentry *root_dentry)
264 struct lookup_table_entry *metadata_lte;
265 struct image_metadata *imd;
266 struct image_metadata *new_imd;
267 struct wim_security_data *sd;
268 struct link_group_table *lgt;
270 DEBUG("Reallocating image metadata array for image_count = %u",
271 w->hdr.image_count + 1);
272 imd = CALLOC((w->hdr.image_count + 1), sizeof(struct image_metadata));
275 ERROR("Failed to allocate memory for new image metadata array");
276 return WIMLIB_ERR_NOMEM;
279 memcpy(imd, w->image_metadata,
280 w->hdr.image_count * sizeof(struct image_metadata));
282 metadata_lte = new_lookup_table_entry();
285 sd = CALLOC(1, sizeof(struct wim_security_data));
287 goto out_free_metadata_lte;
289 sd->total_length = 8;
291 lgt = new_link_group_table(9001);
293 goto out_free_security_data;
295 metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
296 random_hash(metadata_lte->hash);
297 lookup_table_insert(w->lookup_table, metadata_lte);
299 new_imd = &imd[w->hdr.image_count];
301 new_imd->root_dentry = root_dentry;
302 new_imd->metadata_lte = metadata_lte;
303 new_imd->security_data = sd;
305 new_imd->modified = true;
307 FREE(w->image_metadata);
308 w->image_metadata = imd;
309 w->hdr.image_count++;
311 /* Change the current image to the new one. */
312 return wimlib_select_image(w, w->hdr.image_count);
313 out_free_security_data:
315 out_free_metadata_lte:
319 return WIMLIB_ERR_NOMEM;
324 * Copies an image, or all the images, from a WIM file, into another WIM file.
326 WIMLIBAPI int wimlib_export_image(WIMStruct *src_wim,
329 const char *dest_name,
330 const char *dest_description,
336 struct wim_pair wims;
338 if (src_image == WIM_ALL_IMAGES) {
339 if (src_wim->hdr.image_count > 1) {
341 /* multi-image export. */
343 if ((flags & WIMLIB_EXPORT_FLAG_BOOT) &&
344 (src_wim->hdr.boot_idx == 0))
346 /* Specifying the boot flag on a multi-image
347 * source WIM makes the boot index default to
348 * the bootable image in the source WIM. It is
349 * an error if there is no such bootable image.
351 ERROR("Cannot specify `boot' flag when "
352 "exporting multiple images from a WIM "
353 "with no bootable images");
354 return WIMLIB_ERR_INVALID_PARAM;
356 if (dest_name || dest_description) {
357 ERROR("Image name or image description was "
358 "specified, but we are exporting "
360 return WIMLIB_ERR_INVALID_PARAM;
362 for (i = 1; i <= src_wim->hdr.image_count; i++) {
363 int export_flags = flags;
365 if (i != src_wim->hdr.boot_idx)
366 export_flags &= ~WIMLIB_EXPORT_FLAG_BOOT;
368 ret = wimlib_export_image(src_wim, i, dest_wim,
381 ret = wimlib_select_image(src_wim, src_image);
383 ERROR("Could not select image %d from the WIM `%s' "
384 "to export it", src_image, src_wim->filename);
389 dest_name = wimlib_get_image_name(src_wim, src_image);
390 DEBUG("Using name `%s' for source image %d",
391 dest_name, src_image);
394 DEBUG("Exporting image %d from `%s'", src_image, src_wim->filename);
396 if (wimlib_image_name_in_use(dest_wim, dest_name)) {
397 ERROR("There is already an image named `%s' in the "
398 "destination WIM", dest_name);
399 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
403 /* Cleaning up here on failure would be hard. For example, we could
404 * fail to allocate memory in add_lte_to_dest_wim(),
405 * leaving the lookup table entries in the destination WIM in an
406 * inconsistent state. Until these issues can be resolved,
407 * wimlib_export_image() is documented as leaving dest_wim is an
408 * indeterminate state. */
409 root = wim_root_dentry(src_wim);
410 for_dentry_in_tree(root, increment_dentry_refcnt, NULL);
411 wims.src_wim = src_wim;
412 wims.dest_wim = dest_wim;
413 ret = for_dentry_in_tree(root, add_lte_to_dest_wim, &wims);
416 ret = add_new_dentry_tree(dest_wim, root);
419 /* Bring over old security data */
420 struct wim_security_data *sd = wim_security_data(src_wim);
421 struct image_metadata *new_imd = wim_get_current_image_metadata(dest_wim);
423 free_security_data(new_imd->security_data);
424 new_imd->security_data = sd;
427 if (flags & WIMLIB_EXPORT_FLAG_BOOT) {
428 DEBUG("Setting boot_idx to %d", dest_wim->hdr.image_count);
429 dest_wim->hdr.boot_idx = dest_wim->hdr.image_count;
432 return xml_export_image(src_wim->wim_info, src_image, &dest_wim->wim_info,
433 dest_name, dest_description);
437 * Deletes an image from the WIM.
439 WIMLIBAPI int wimlib_delete_image(WIMStruct *w, int image)
445 if (image == WIM_ALL_IMAGES) {
446 num_images = w->hdr.image_count;
447 for (i = 1; i <= num_images; i++) {
448 /* Always delete the first image, since by the end
449 * there won't be any more than that! */
450 ret = wimlib_delete_image(w, 1);
457 DEBUG("Deleting image %d", image);
459 /* Even if the dentry tree is not allocated, we must select it (and
460 * therefore allocate it) so that we can decrement the reference counts
461 * in the lookup table. */
462 ret = wimlib_select_image(w, image);
466 /* Free the dentry tree, any lookup table entries that have their
467 * refcnt decremented to 0, and the security data. */
468 destroy_image_metadata(wim_get_current_image_metadata(w),
471 /* Get rid of the empty slot in the image metadata array. */
472 memmove(&w->image_metadata[image - 1], &w->image_metadata[image],
473 (w->hdr.image_count - image) * sizeof(struct image_metadata));
475 /* Decrement the image count. */
476 if (--w->hdr.image_count == 0) {
477 FREE(w->image_metadata);
478 w->image_metadata = NULL;
481 /* Fix the boot index. */
482 if (w->hdr.boot_idx == image)
484 else if (w->hdr.boot_idx > image)
487 w->current_image = WIM_NO_IMAGE;
489 /* Remove the image from the XML information. */
490 xml_delete_image(&w->wim_info, image);
495 * Adds an image to a WIM file from a directory tree on disk.
497 WIMLIBAPI int wimlib_add_image(WIMStruct *w, const char *dir,
498 const char *name, const char *description,
499 const char *flags_element, int flags)
501 struct dentry *root_dentry;
502 struct image_metadata *imd;
505 DEBUG("Adding dentry tree from dir `%s'.", dir);
507 if (!name || !*name) {
508 ERROR("Must specify a non-empty string for the image name");
509 return WIMLIB_ERR_INVALID_PARAM;
512 ERROR("Must specify the name of a directory");
513 return WIMLIB_ERR_INVALID_PARAM;
516 if (wimlib_image_name_in_use(w, name)) {
517 ERROR("There is already an image named \"%s\" in `%s'",
519 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
522 DEBUG("Creating root dentry.");
524 root_dentry = new_dentry("");
526 return WIMLIB_ERR_NOMEM;
528 DEBUG("Building dentry tree.");
529 ret = build_dentry_tree(root_dentry, dir, w->lookup_table,
530 flags | WIMLIB_ADD_IMAGE_FLAG_ROOT);
533 ERROR("Failed to build dentry tree for `%s'", dir);
534 goto out_free_dentry_tree;
537 DEBUG("Calculating full paths of dentries.");
538 ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
540 goto out_free_dentry_tree;
542 ret = add_new_dentry_tree(w, root_dentry);
544 goto out_free_dentry_tree;
546 DEBUG("Inserting dentries into hard link group table");
547 ret = for_dentry_in_tree(root_dentry, link_group_table_insert,
548 w->image_metadata[w->hdr.image_count - 1].lgt);
550 goto out_destroy_imd;
551 DEBUG("Assigning hard link groups");
552 assign_link_groups(w->image_metadata[w->hdr.image_count - 1].lgt);
554 if (flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
555 wimlib_set_boot_idx(w, w->hdr.image_count);
557 ret = xml_add_image(w, root_dentry, name, description, flags_element);
559 goto out_destroy_imd;
563 destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
565 w->hdr.image_count--;
567 out_free_dentry_tree:
568 free_dentry_tree(root_dentry, w->lookup_table);