summaryrefslogtreecommitdiffstats
path: root/lib/dpkg/pkg-spec.c
blob: 3e3f59fa0d47c5c9be1a43306252ff3891cd50bf (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 * libdpkg - Debian packaging suite library routines
 * pkg-spec.c - primitives for pkg specifier handling
 *
 * Copyright © 2011 Linaro Limited
 * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
 * Copyright © 2011-2015 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 <stdlib.h>
#include <fnmatch.h>
#include <string.h>

#include <dpkg/i18n.h>
#include <dpkg/dpkg.h>
#include <dpkg/dpkg-db.h>
#include <dpkg/arch.h>
#include <dpkg/pkg-spec.h>

static void
pkg_spec_blank(struct pkg_spec *ps)
{
	ps->name = NULL;
	ps->arch = NULL;

	ps->name_is_pattern = false;
	ps->arch_is_pattern = false;
}

static void
pkg_spec_iter_blank(struct pkg_spec *ps)
{
	ps->pkg_iter = NULL;
	ps->pkg_next = NULL;
}

void
pkg_spec_init(struct pkg_spec *ps, enum pkg_spec_flags flags)
{
	ps->flags = flags;

	pkg_spec_blank(ps);
	pkg_spec_iter_blank(ps);
}

const char *
pkg_spec_is_illegal(struct pkg_spec *ps)
{
	static char msg[1024];
	const char *emsg;

	if (!ps->name_is_pattern &&
	    (emsg = pkg_name_is_illegal(ps->name))) {
		const char *arch_sep;

		/* Only check for DPKG_ARCH_NONE, because for everything else
		 * we want to see the passed package specification, even if
		 * the architecture is empty. */
		if (ps->arch->type == DPKG_ARCH_NONE)
			arch_sep = "";
		else
			arch_sep = ":";

		snprintf(msg, sizeof(msg),
		         _("illegal package name in specifier '%s%s%s': %s"),
		         ps->name, arch_sep, ps->arch->name, emsg);
		return msg;
	}

	if ((!ps->arch_is_pattern && ps->arch->type == DPKG_ARCH_ILLEGAL) ||
	    ps->arch->type == DPKG_ARCH_EMPTY) {
		emsg = dpkg_arch_name_is_illegal(ps->arch->name);
		snprintf(msg, sizeof(msg),
		         _("illegal architecture name in specifier '%s:%s': %s"),
		         ps->name, ps->arch->name, emsg);
		return msg;
	}

	/* If we have been requested a single instance, check that the
	 * package does not contain other instances. */
	if (!ps->arch_is_pattern && ps->flags & PKG_SPEC_ARCH_SINGLE) {
		struct pkgset *set;

		set = pkg_hash_find_set(ps->name);

		/* Single instancing only applies with no architecture. */
		if (ps->arch->type == DPKG_ARCH_NONE &&
		    pkgset_installed_instances(set) > 1) {
			snprintf(msg, sizeof(msg),
			         _("ambiguous package name '%s' with more "
			           "than one installed instance"), ps->name);
			return msg;
		}
	}

	return NULL;
}

static const char *
pkg_spec_prep(struct pkg_spec *ps, char *pkgname, const char *archname)
{
	ps->name = pkgname;
	ps->arch = dpkg_arch_find(archname);

	ps->name_is_pattern = false;
	ps->arch_is_pattern = false;

	/* Detect if we have patterns and/or illegal names. */
	if ((ps->flags & PKG_SPEC_PATTERNS) && strpbrk(ps->name, "*[?\\"))
		ps->name_is_pattern = true;

	if ((ps->flags & PKG_SPEC_PATTERNS) && strpbrk(ps->arch->name, "*[?\\"))
		ps->arch_is_pattern = true;

	return pkg_spec_is_illegal(ps);
}

const char *
pkg_spec_set(struct pkg_spec *ps, const char *pkgname, const char *archname)
{
	return pkg_spec_prep(ps, m_strdup(pkgname), archname);
}

const char *
pkg_spec_parse(struct pkg_spec *ps, const char *str)
{
	char *pkgname, *archname;

	archname = strchr(str, ':');
	if (archname == NULL) {
		pkgname = m_strdup(str);
	} else {
		pkgname = m_strndup(str, archname - str);
		archname++;
	}

	return pkg_spec_prep(ps, pkgname, archname);
}

static bool
pkg_spec_match_name(struct pkg_spec *ps, const char *name)
{
	if (ps->name_is_pattern)
		return (fnmatch(ps->name, name, 0) == 0);
	else
		return (strcmp(ps->name, name) == 0);
}

static bool
pkg_spec_match_arch(struct pkg_spec *ps, struct pkginfo *pkg,
                    const struct dpkg_arch *arch)
{
	if (ps->arch_is_pattern)
		return (fnmatch(ps->arch->name, arch->name, 0) == 0);
	else if (ps->arch->type != DPKG_ARCH_NONE) /* !arch_is_pattern */
		return (ps->arch == arch);

	/* No arch specified. */
	switch (ps->flags & PKG_SPEC_ARCH_MASK) {
	case PKG_SPEC_ARCH_SINGLE:
		return pkgset_installed_instances(pkg->set) <= 1;
	case PKG_SPEC_ARCH_WILDCARD:
		return true;
	default:
		internerr("unknown PKG_SPEC_ARCH_* flags %d in pkg_spec",
		          ps->flags & PKG_SPEC_ARCH_MASK);
	}
}

bool
pkg_spec_match_pkg(struct pkg_spec *ps, struct pkginfo *pkg,
                   struct pkgbin *pkgbin)
{
	return (pkg_spec_match_name(ps, pkg->set->name) &&
	        pkg_spec_match_arch(ps, pkg, pkgbin->arch));
}

static struct pkginfo *
pkg_spec_get_pkg(struct pkg_spec *ps)
{
	if (ps->arch->type == DPKG_ARCH_NONE)
		return pkg_hash_find_singleton(ps->name);
	else
		return pkg_hash_find_pkg(ps->name, ps->arch);
}

struct pkginfo *
pkg_spec_parse_pkg(const char *str, struct dpkg_error *err)
{
	struct pkg_spec ps;
	struct pkginfo *pkg;
	const char *emsg;

	pkg_spec_init(&ps, PKG_SPEC_ARCH_SINGLE);
	emsg = pkg_spec_parse(&ps, str);
	if (emsg) {
		dpkg_put_error(err, "%s", emsg);
		pkg = NULL;
	} else {
		pkg = pkg_spec_get_pkg(&ps);
	}
	pkg_spec_destroy(&ps);

	return pkg;
}

struct pkginfo *
pkg_spec_find_pkg(const char *pkgname, const char *archname,
                  struct dpkg_error *err)
{
	struct pkg_spec ps;
	struct pkginfo *pkg;
	const char *emsg;

	pkg_spec_init(&ps, PKG_SPEC_ARCH_SINGLE);
	emsg = pkg_spec_set(&ps, pkgname, archname);
	if (emsg) {
		dpkg_put_error(err, "%s", emsg);
		pkg = NULL;
	} else {
		pkg = pkg_spec_get_pkg(&ps);
	}
	pkg_spec_destroy(&ps);

	return pkg;
}

void
pkg_spec_iter_init(struct pkg_spec *ps)
{
	if (ps->name_is_pattern)
		ps->pkg_iter = pkg_hash_iter_new();
	else
		ps->pkg_next = &pkg_hash_find_set(ps->name)->pkg;
}

static struct pkginfo *
pkg_spec_iter_next_pkgname(struct pkg_spec *ps)
{
	struct pkginfo *pkg;

	while ((pkg = pkg_hash_iter_next_pkg(ps->pkg_iter))) {
		if (pkg_spec_match_pkg(ps, pkg, &pkg->installed))
			return pkg;
	}

	return NULL;
}

static struct pkginfo *
pkg_spec_iter_next_pkgarch(struct pkg_spec *ps)
{
	struct pkginfo *pkg;

	while ((pkg = ps->pkg_next)) {
		ps->pkg_next = pkg->arch_next;

		if (pkg_spec_match_arch(ps, pkg, pkg->installed.arch))
			return pkg;
	}

	return NULL;
}

struct pkginfo *
pkg_spec_iter_next_pkg(struct pkg_spec *ps)
{
	if (ps->name_is_pattern)
		return pkg_spec_iter_next_pkgname(ps);
	else
		return pkg_spec_iter_next_pkgarch(ps);
}

void
pkg_spec_iter_destroy(struct pkg_spec *ps)
{
	pkg_hash_iter_free(ps->pkg_iter);
	pkg_spec_iter_blank(ps);
}

void
pkg_spec_destroy(struct pkg_spec *ps)
{
	free(ps->name);
	pkg_spec_blank(ps);
	pkg_spec_iter_destroy(ps);
}