]> wimlib.net Git - wimlib/blob - src/delete_image.c
Update version number to 1.3.1
[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 #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
32 wimlib_delete_image(WIMStruct *w, int image)
33 {
34         int i;
35         int ret;
36
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;
40         }
41
42         if (image == WIMLIB_ALL_IMAGES) {
43                 for (i = w->hdr.image_count; i >= 1; i--) {
44                         ret = wimlib_delete_image(w, i);
45                         if (ret != 0)
46                                 return ret;
47                 }
48                 return 0;
49         }
50
51         if (!w->all_images_verified) {
52                 ret = wim_run_full_verifications(w);
53                 if (ret != 0)
54                         return ret;
55         }
56
57         DEBUG("Deleting image %d", image);
58
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);
63         if (ret != 0)
64                 return ret;
65
66         /* Free the dentry tree, any lookup table entries that have their refcnt
67          * decremented to 0, and the security data. */
68         destroy_image_metadata(&w->image_metadata[image - 1], w->lookup_table);
69
70         /* Get rid of the empty slot in the image metadata array. */
71         memmove(&w->image_metadata[image - 1], &w->image_metadata[image],
72                 (w->hdr.image_count - image) * sizeof(struct wim_image_metadata));
73
74         /* Decrement the image count. */
75         if (--w->hdr.image_count == 0) {
76                 FREE(w->image_metadata);
77                 w->image_metadata = NULL;
78         }
79
80         /* Fix the boot index. */
81         if (w->hdr.boot_idx == image)
82                 w->hdr.boot_idx = 0;
83         else if (w->hdr.boot_idx > image)
84                 w->hdr.boot_idx--;
85
86         w->current_image = WIMLIB_NO_IMAGE;
87
88         /* Remove the image from the XML information. */
89         xml_delete_image(&w->wim_info, image);
90
91         w->deletion_occurred = 1;
92         return 0;
93 }