summaryrefslogtreecommitdiffstats
path: root/lib/dpkg/pkg.c
blob: 22bab5bca7429e831e6e7bdd39a611b19873f8f8 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * libdpkg - Debian packaging suite library routines
 * pkg.c - primitives for pkg handling
 *
 * Copyright © 1995, 1996 Ian Jackson <ijackson@chiark.greenend.org.uk>
 * Copyright © 2009-2014 Guillem Jover <guillem@debian.org>
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <compat.h>

#include <string.h>

#include <dpkg/ehandle.h>
#include <dpkg/string.h>
#include <dpkg/dpkg-db.h>
#include <dpkg/pkg.h>

/**
 * Set the package installation status.
 */
void
pkg_set_status(struct pkginfo *pkg, enum pkgstatus status)
{
	if (pkg->status == status)
		return;
	else if (pkg->status == PKG_STAT_NOTINSTALLED)
		pkg->set->installed_instances++;
	else if (status == PKG_STAT_NOTINSTALLED)
		pkg->set->installed_instances--;

	if (pkg->set->installed_instances < 0)
		internerr("pkgset %s went into negative installed instances %d",
		          pkg->set->name, pkg->set->installed_instances);

	pkg->status = status;
	pkg->status_dirty = true;
}

/**
 * Set the specified flags to 1 in the package error flags.
 */
void
pkg_set_eflags(struct pkginfo *pkg, enum pkgeflag eflag)
{
	pkg->eflag |= eflag;
}

/**
 * Clear the specified flags to 0 in the package error flags.
 */
void
pkg_clear_eflags(struct pkginfo *pkg, enum pkgeflag eflag)
{
	pkg->eflag &= ~eflag;
}

/**
 * Reset the package error flags to 0.
 */
void
pkg_reset_eflags(struct pkginfo *pkg)
{
	pkg->eflag = PKG_EFLAG_OK;
}

/**
 * Copy the package error flags to another package.
 */
void
pkg_copy_eflags(struct pkginfo *pkg_dst, struct pkginfo *pkg_src)
{
	pkg_dst->eflag = pkg_src->eflag;
}

/**
 * Set the package selection status.
 */
void
pkg_set_want(struct pkginfo *pkg, enum pkgwant want)
{
	pkg->want = want;
}

void
pkgbin_blank(struct pkgbin *pkgbin)
{
	pkgbin->essential = false;
	pkgbin->is_protected = false;
	pkgbin->depends = NULL;
	pkgbin->pkgname_archqual = NULL;
	pkgbin->description = NULL;
	pkgbin->maintainer = NULL;
	pkgbin->source = NULL;
	pkgbin->installedsize = NULL;
	pkgbin->bugs = NULL;
	pkgbin->origin = NULL;
	dpkg_version_blank(&pkgbin->version);
	pkgbin->conffiles = NULL;
	pkgbin->arbs = NULL;
}

void
pkg_blank(struct pkginfo *pkg)
{
	pkg->status = PKG_STAT_NOTINSTALLED;
	pkg->status_dirty = false;
	pkg->eflag = PKG_EFLAG_OK;
	pkg->want = PKG_WANT_UNKNOWN;
	pkg->priority = PKG_PRIO_UNKNOWN;
	pkg->otherpriority = NULL;
	pkg->section = NULL;
	dpkg_version_blank(&pkg->configversion);
	pkg->files_list_valid = false;
	pkg->files_list_phys_offs = 0;
	pkg->files = NULL;
	pkg->archives = NULL;
	pkg->clientdata = NULL;
	pkg->trigaw.head = NULL;
	pkg->trigaw.tail = NULL;
	pkg->othertrigaw_head = NULL;
	pkg->trigpend_head = NULL;
	pkgbin_blank(&pkg->installed);
	pkgbin_blank(&pkg->available);

	/* The architectures are reset here (instead of in pkgbin_blank),
	 * because they are part of the package specification, and needed
	 * for selections. */
	pkg->installed.arch = dpkg_arch_get(DPKG_ARCH_NONE);
	pkg->installed.multiarch = PKG_MULTIARCH_NO;
	pkg->available.arch = dpkg_arch_get(DPKG_ARCH_NONE);
	pkg->available.multiarch = PKG_MULTIARCH_NO;
}

void
pkgset_blank(struct pkgset *set)
{
	set->name = NULL;
	set->depended.available = NULL;
	set->depended.installed = NULL;
	pkg_blank(&set->pkg);
	set->installed_instances = 0;
	set->pkg.set = set;
	set->pkg.arch_next = NULL;
}

/**
 * Link a pkginfo instance into a package set.
 *
 * @param set The package set to use.
 * @param pkg The package to link into the set.
 */
void
pkgset_link_pkg(struct pkgset *set, struct pkginfo *pkg)
{
	pkg->set = set;
	pkg->arch_next = set->pkg.arch_next;
	set->pkg.arch_next = pkg;
}

/**
 * Get the number of installed package instances in a package set.
 *
 * @param set The package set to use.
 *
 * @return The count of installed packages.
 */
int
pkgset_installed_instances(struct pkgset *set)
{
	return set->installed_instances;
}

/**
 * Check if a pkg is informative.
 *
 * Used by dselect and dpkg query options as an aid to decide whether to
 * display things, and by dump to decide whether to write them out.
 */
bool
pkg_is_informative(struct pkginfo *pkg, struct pkgbin *pkgbin)
{
	/* We ignore Section and Priority, as these tend to hang around. */
	if (pkgbin == &pkg->installed &&
	    (pkg->want != PKG_WANT_UNKNOWN ||
	     pkg->eflag != PKG_EFLAG_OK ||
	     pkg->status != PKG_STAT_NOTINSTALLED ||
	     dpkg_version_is_informative(&pkg->configversion)))
		return true;

	if (pkgbin->depends ||
	    str_is_set(pkgbin->description) ||
	    str_is_set(pkgbin->maintainer) ||
	    str_is_set(pkgbin->origin) ||
	    str_is_set(pkgbin->bugs) ||
	    str_is_set(pkgbin->installedsize) ||
	    str_is_set(pkgbin->source) ||
	    dpkg_version_is_informative(&pkgbin->version) ||
	    pkgbin->conffiles ||
	    pkgbin->arbs)
		return true;

	return false;
}