-
Notifications
You must be signed in to change notification settings - Fork 21
/
delete_image.c
103 lines (87 loc) · 2.65 KB
/
delete_image.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* delete_image.c
*/
/*
* Copyright (C) 2012-2016 Eric Biggers
*
* This file is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option) any
* later version.
*
* This file is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this file; if not, see https://www.gnu.org/licenses/.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include "wimlib.h"
#include "wimlib/dentry.h"
#include "wimlib/metadata.h"
#include "wimlib/wim.h"
#include "wimlib/xml.h"
/* Internal method for single-image deletion. This doesn't set the
* image_deletion_occurred' flag on the WIMStruct. */
int
delete_wim_image(WIMStruct *wim, int image)
{
int ret;
struct wim_image_metadata *imd;
/* Load the metadata for the image to be deleted. This is necessary
* because blobs referenced by files in the image need to have their
* reference counts decremented. */
ret = select_wim_image(wim, image);
if (ret)
return ret;
/* Release the files and decrement the reference counts of the blobs
* they reference. */
imd = wim->image_metadata[image - 1];
free_dentry_tree(imd->root_dentry, wim->blob_table);
imd->root_dentry = NULL;
/* Deselect the image and release its metadata. */
deselect_current_wim_image(wim);
put_image_metadata(imd);
/* Remove the empty slot from the image metadata array. */
memmove(&wim->image_metadata[image - 1], &wim->image_metadata[image],
(wim->hdr.image_count - image) *
sizeof(wim->image_metadata[0]));
/* Decrement the image count. */
wim->hdr.image_count--;
/* Remove the image from the XML information. */
xml_delete_image(wim->xml_info, image);
/* Fix the boot index. */
if (wim->hdr.boot_idx == image)
wim->hdr.boot_idx = 0;
else if (wim->hdr.boot_idx > image)
wim->hdr.boot_idx--;
return 0;
}
/* API function documented in wimlib.h */
WIMLIBAPI int
wimlib_delete_image(WIMStruct *wim, int image)
{
int ret;
int first, last;
if (image == WIMLIB_ALL_IMAGES) {
/* Deleting all images */
last = wim->hdr.image_count;
first = 1;
} else {
/* Deleting one image */
last = image;
first = image;
}
for (image = last; image >= first; image--) {
ret = delete_wim_image(wim, image);
if (ret)
return ret;
wim->image_deletion_occurred = 1;
}
return 0;
}