]> wimlib.net Git - wimlib/blob - src/delete_image.c
862fb66e53e2efb1f0623aef17ce894d8c746f0f
[wimlib] / src / delete_image.c
1 /*
2  * delete_image.c
3  */
4
5 /*
6  * Copyright (C) 2012, 2013 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 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "wimlib.h"
29 #include "wimlib/error.h"
30 #include "wimlib/metadata.h"
31 #include "wimlib/util.h"
32 #include "wimlib/wim.h"
33 #include "wimlib/xml.h"
34
35 /*
36  * Deletes an image from the WIM.
37  */
38 WIMLIBAPI int
39 wimlib_delete_image(WIMStruct *w, int image)
40 {
41         int ret;
42         int first, last;
43
44         if (w->hdr.total_parts != 1) {
45                 ERROR("Deleting an image from a split WIM is not supported.");
46                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
47         }
48
49         if (!w->all_images_verified) {
50                 ret = wim_run_full_verifications(w);
51                 if (ret)
52                         return ret;
53         }
54
55         if (image == WIMLIB_ALL_IMAGES) {
56                 last = w->hdr.image_count;
57                 first = 1;
58         } else {
59                 last = image;
60                 first = image;
61         }
62
63         for (image = last; image >= first; image--) {
64                 DEBUG("Deleting image %d", image);
65
66                 /* Even if the dentry tree is not allocated, we must select it (and
67                  * therefore allocate it) so that we can decrement the reference counts
68                  * in the lookup table.  */
69                 ret = select_wim_image(w, image);
70                 if (ret)
71                         return ret;
72
73                 /* Unless the image metadata is shared by another WIMStruct, free the
74                  * dentry tree, any lookup table entries that have their refcnt
75                  * decremented to 0, and the security data. */
76                 put_image_metadata(w->image_metadata[image - 1], w->lookup_table);
77
78                 /* Get rid of the empty slot in the image metadata array. */
79                 for (int i = image - 1; i < w->hdr.image_count - 1; i++)
80                         w->image_metadata[i] = w->image_metadata[i + 1];
81
82                 /* Decrement the image count. */
83                 --w->hdr.image_count;
84
85                 /* Fix the boot index. */
86                 if (w->hdr.boot_idx == image)
87                         w->hdr.boot_idx = 0;
88                 else if (w->hdr.boot_idx > image)
89                         w->hdr.boot_idx--;
90
91                 w->current_image = WIMLIB_NO_IMAGE;
92
93                 /* Remove the image from the XML information. */
94                 xml_delete_image(&w->wim_info, image);
95
96                 w->deletion_occurred = 1;
97         }
98         return 0;
99 }