6 * Copyright (C) 2012, 2013 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.
32 wimlib_delete_image(WIMStruct *w, int image)
37 if (w->hdr.total_parts != 1) {
38 ERROR("Deleting an image from a split WIM is not supported.");
39 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
42 if (!w->all_images_verified) {
43 ret = wim_run_full_verifications(w);
48 if (image == WIMLIB_ALL_IMAGES) {
49 last = w->hdr.image_count;
56 for (image = last; image >= first; image--) {
57 DEBUG("Deleting image %d", image);
59 /* Even if the dentry tree is not allocated, we must select it (and
60 * therefore allocate it) so that we can decrement the reference counts
61 * in the lookup table. */
62 ret = select_wim_image(w, image);
66 /* Unless the image metadata is shared by another WIMStruct, free the
67 * dentry tree, any lookup table entries that have their refcnt
68 * decremented to 0, and the security data. */
69 put_image_metadata(w->image_metadata[image - 1], w->lookup_table);
71 /* Get rid of the empty slot in the image metadata array. */
72 memmove(w->image_metadata[image - 1],
73 &w->image_metadata[image],
74 (w->hdr.image_count - image) * sizeof(w->image_metadata[0]));
76 /* Decrement the image count. */
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 = 1;