]> wimlib.net Git - wimlib/blob - src/delete_image.c
ecd80478f7cf73d62461c53c5fb00b4294db7f0a
[wimlib] / src / delete_image.c
1 /*
2  * delete_image.c
3  */
4
5 /*
6  * Copyright (C) 2012 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
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)
13  * any later version.
14  *
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
18  * details.
19  *
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/.
22  */
23
24 #include "wimlib_internal.h"
25 #include "xml.h"
26 #include <string.h>
27
28 /*
29  * Deletes an image from the WIM.
30  */
31 WIMLIBAPI int wimlib_delete_image(WIMStruct *w, int image)
32 {
33         int i;
34         int ret;
35
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;
39         }
40
41         if (image == WIMLIB_ALL_IMAGES) {
42                 for (i = w->hdr.image_count; i >= 1; i--) {
43                         ret = wimlib_delete_image(w, i);
44                         if (ret != 0)
45                                 return ret;
46                 }
47                 return 0;
48         }
49
50         if (!w->all_images_verified) {
51                 ret = wim_run_full_verifications(w);
52                 if (ret != 0)
53                         return ret;
54         }
55
56         DEBUG("Deleting image %d", image);
57
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);
62         if (ret != 0)
63                 return ret;
64
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);
68
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));
72
73         /* Decrement the image count. */
74         if (--w->hdr.image_count == 0) {
75                 FREE(w->image_metadata);
76                 w->image_metadata = NULL;
77         }
78
79         /* Fix the boot index. */
80         if (w->hdr.boot_idx == image)
81                 w->hdr.boot_idx = 0;
82         else if (w->hdr.boot_idx > image)
83                 w->hdr.boot_idx--;
84
85         w->current_image = WIMLIB_NO_IMAGE;
86
87         /* Remove the image from the XML information. */
88         xml_delete_image(&w->wim_info, image);
89
90         w->deletion_occurred = true;
91         return 0;
92 }