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 if (image == WIMLIB_ALL_IMAGES) {
41 last = wim->hdr.image_count;
48 for (image = last; image >= first; image--) {
49 DEBUG("Deleting image %d", image);
51 /* Even if the dentry tree is not allocated, we must select it
52 * (and therefore allocate it) so that we can decrement stream
53 * reference counts. */
54 ret = select_wim_image(wim, image);
58 /* Unless the image metadata is shared by another WIMStruct,
59 * free the dentry tree, free the security data, and decrement
60 * stream reference counts. */
61 put_image_metadata(wim->image_metadata[image - 1], wim->lookup_table);
63 /* Get rid of the empty slot in the image metadata array. */
64 for (int i = image - 1; i < wim->hdr.image_count - 1; i++)
65 wim->image_metadata[i] = wim->image_metadata[i + 1];
67 /* Decrement the image count. */
68 --wim->hdr.image_count;
70 /* Fix the boot index. */
71 if (wim->hdr.boot_idx == image)
72 wim->hdr.boot_idx = 0;
73 else if (wim->hdr.boot_idx > image)
76 wim->current_image = WIMLIB_NO_IMAGE;
78 /* Remove the image from the XML information. */
79 xml_delete_image(&wim->wim_info, image);
81 wim->deletion_occurred = 1;