6 * Copyright (C) 2012 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
10 * wimlib is free software; you can redistribute it and/or modify it under the
11 * terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at your option)
15 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 * A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with wimlib; if not, see http://www.gnu.org/licenses/.
24 #include "wimlib_internal.h"
29 * Deletes an image from the WIM.
31 WIMLIBAPI int wimlib_delete_image(WIMStruct *w, int image)
36 if (w->hdr.total_parts != 1) {
37 ERROR("Deleting an image from a split WIM is not supported.");
38 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
41 if (image == WIMLIB_ALL_IMAGES) {
42 for (i = w->hdr.image_count; i >= 1; i--) {
43 ret = wimlib_delete_image(w, i);
50 if (!w->all_images_verified) {
51 ret = wim_run_full_verifications(w);
56 DEBUG("Deleting image %d", image);
58 /* Even if the dentry tree is not allocated, we must select it (and
59 * therefore allocate it) so that we can decrement the reference counts
60 * in the lookup table. */
61 ret = select_wim_image(w, image);
65 /* Free the dentry tree, any lookup table entries that have their
66 * refcnt decremented to 0, and the security data. */
67 destroy_image_metadata(&w->image_metadata[image - 1], w->lookup_table);
69 /* Get rid of the empty slot in the image metadata array. */
70 memmove(&w->image_metadata[image - 1], &w->image_metadata[image],
71 (w->hdr.image_count - image) * sizeof(struct image_metadata));
73 /* Decrement the image count. */
74 if (--w->hdr.image_count == 0) {
75 FREE(w->image_metadata);
76 w->image_metadata = NULL;
79 /* Fix the boot index. */
80 if (w->hdr.boot_idx == image)
82 else if (w->hdr.boot_idx > image)
85 w->current_image = WIMLIB_NO_IMAGE;
87 /* Remove the image from the XML information. */
88 xml_delete_image(&w->wim_info, image);
90 w->deletion_occurred = true;