533d62cb502675dfaf0a94ec8c8d3b3d6456ff1d
[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 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
11  * later version.
12  *
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
16  * details.
17  *
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/.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25
26 #include "wimlib.h"
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"
32
33 /* API function documented in wimlib.h  */
34 WIMLIBAPI int
35 wimlib_delete_image(WIMStruct *wim, int image)
36 {
37         int ret;
38         int first, last;
39
40         if (image == WIMLIB_ALL_IMAGES) {
41                 last = wim->hdr.image_count;
42                 first = 1;
43         } else {
44                 last = image;
45                 first = image;
46         }
47
48         for (image = last; image >= first; image--) {
49                 DEBUG("Deleting image %d", image);
50
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);
55                 if (ret)
56                         return ret;
57
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);
62
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];
66
67                 /* Decrement the image count. */
68                 --wim->hdr.image_count;
69
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)
74                         wim->hdr.boot_idx--;
75
76                 wim->current_image = WIMLIB_NO_IMAGE;
77
78                 /* Remove the image from the XML information. */
79                 xml_delete_image(&wim->wim_info, image);
80
81                 wim->image_deletion_occurred = 1;
82         }
83         return 0;
84 }