]> wimlib.net Git - wimlib/blob - src/delete_image.c
LZMS: Do not do x86 translations starting in last 16 bytes
[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 /* API function documented in wimlib.h  */
36 WIMLIBAPI int
37 wimlib_delete_image(WIMStruct *wim, int image)
38 {
39         int ret;
40         int first, last;
41
42         ret = can_delete_from_wim(wim);
43         if (ret)
44                 return ret;
45
46         if (image == WIMLIB_ALL_IMAGES) {
47                 last = wim->hdr.image_count;
48                 first = 1;
49         } else {
50                 last = image;
51                 first = image;
52         }
53
54         for (image = last; image >= first; image--) {
55                 DEBUG("Deleting image %d", image);
56
57                 /* Even if the dentry tree is not allocated, we must select it (and
58                  * therefore allocate it) so that we can decrement the reference counts
59                  * in the lookup table.  */
60                 ret = select_wim_image(wim, image);
61                 if (ret)
62                         return ret;
63
64                 /* Unless the image metadata is shared by another WIMStruct, free the
65                  * dentry tree, any lookup table entries that have their refcnt
66                  * decremented to 0, and the security data. */
67                 put_image_metadata(wim->image_metadata[image - 1], wim->lookup_table);
68
69                 /* Get rid of the empty slot in the image metadata array. */
70                 for (int i = image - 1; i < wim->hdr.image_count - 1; i++)
71                         wim->image_metadata[i] = wim->image_metadata[i + 1];
72
73                 /* Decrement the image count. */
74                 --wim->hdr.image_count;
75
76                 /* Fix the boot index. */
77                 if (wim->hdr.boot_idx == image)
78                         wim->hdr.boot_idx = 0;
79                 else if (wim->hdr.boot_idx > image)
80                         wim->hdr.boot_idx--;
81
82                 wim->current_image = WIMLIB_NO_IMAGE;
83
84                 /* Remove the image from the XML information. */
85                 xml_delete_image(&wim->wim_info, image);
86
87                 wim->deletion_occurred = 1;
88         }
89         return 0;
90 }