6 * Copyright (C) 2012, 2013 Eric Biggers
8 * This file is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at your option) any
13 * This file is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this file; if not, see http://www.gnu.org/licenses/.
27 #include "wimlib/error.h"
28 #include "wimlib/metadata.h"
29 #include "wimlib/util.h"
30 #include "wimlib/wim.h"
31 #include "wimlib/xml.h"
33 /* API function documented in wimlib.h */
35 wimlib_delete_image(WIMStruct *wim, int image)
40 ret = can_delete_from_wim(wim);
44 if (image == WIMLIB_ALL_IMAGES) {
45 last = wim->hdr.image_count;
52 for (image = last; image >= first; image--) {
53 DEBUG("Deleting image %d", image);
55 /* Even if the dentry tree is not allocated, we must select it (and
56 * therefore allocate it) so that we can decrement the reference counts
57 * in the lookup table. */
58 ret = select_wim_image(wim, image);
62 /* Unless the image metadata is shared by another WIMStruct, free the
63 * dentry tree, any lookup table entries that have their refcnt
64 * decremented to 0, and the security data. */
65 put_image_metadata(wim->image_metadata[image - 1], wim->lookup_table);
67 /* Get rid of the empty slot in the image metadata array. */
68 for (int i = image - 1; i < wim->hdr.image_count - 1; i++)
69 wim->image_metadata[i] = wim->image_metadata[i + 1];
71 /* Decrement the image count. */
72 --wim->hdr.image_count;
74 /* Fix the boot index. */
75 if (wim->hdr.boot_idx == image)
76 wim->hdr.boot_idx = 0;
77 else if (wim->hdr.boot_idx > image)
80 wim->current_image = WIMLIB_NO_IMAGE;
82 /* Remove the image from the XML information. */
83 xml_delete_image(&wim->wim_info, image);
85 wim->deletion_occurred = 1;