File: pkginfo.c

package info (click to toggle)
deborphan 1.7.28.8
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,916 kB
  • sloc: sh: 4,123; ansic: 1,847; makefile: 152
file content (418 lines) | stat: -rw-r--r-- 11,096 bytes parent folder | download | duplicates (3)
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/* pkginfo.c - Retrieve information about packages for deborphan.
   Copyright (C) 2000, 2001, 2002, 2003 Cris van Pelt
   Copyright (C) 2003, 2004, 2006 Peter Palfrader
   Copyright (C) 2008, 2009 Carsten Hey

   $Id: pkginfo.c 904 2012-04-26 00:19:03Z carsten $

   Distributed under the terms of the Artistic License.
*/

/* This code is not nearly as advanced as dpkg's. It assumes a
   "perfect" statusfile. If the status-file is corrupted it may
   result in deborphan giving the wrong packages, or even crashing. */

#include <stdlib.h>
#include <string.h>
#include <regex.h>

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <deborphan.h>
#include <set.h>

#ifdef USE_XALLOC
#  include <xalloc.h>
#endif

extern int options[];

static regex_t re_statusinst, re_statusnotinst, re_statushold, re_namedev,
               re_gnugrepv, re_descdummy, re_desctransit, re_statusconfig,
               re_status;

void
init_pkg_regex()
{
    regcomp(&re_statusinst,
	    "^Status:.*[^-]installed$", REG_EXTENDED | REG_FLAGS);
    regcomp(&re_statusnotinst,
	    "^Status:.*not\\-installed$", REG_EXTENDED | REG_FLAGS);
    regcomp(&re_statushold,
	    "^Status:hold.*[^-]installed$", REG_EXTENDED | REG_FLAGS);
    regcomp(&re_statusconfig,
	    "^Status:.*config\\-files$", REG_EXTENDED | REG_FLAGS);
    regcomp(&re_status,
	    "^Status:", REG_EXTENDED | REG_FLAGS);

    if (options[GUESS]) {
	char guess[256];
	guess[0] = '\0';

	if (guess_chk(GUESS_PERL))
	    strcat(guess, "^lib.*-perl$|");
	if (guess_chk(GUESS_PYTHON))
	    strcat(guess, "^python[[:digit:].]*-|");
	if (guess_chk(GUESS_PIKE))
	    strcat(guess, "^pike[[:digit:].]*-|");
	if (guess_chk(GUESS_RUBY))
	    strcat(guess, "^lib.*-ruby[[:digit:].]*$|");
	if (guess_chk(GUESS_MONO))
	    strcat(guess, "^libmono|");
	if (guess_chk(GUESS_DEV))
	    strcat(guess, "-dev$|");
	if (guess_chk(GUESS_DEBUG))
	    strcat(guess, "-dbg(|sym)$|");
	if (guess_chk(GUESS_COMMON))
	    strcat(guess, "-common$|");
	if (guess_chk(GUESS_DATA))
	    strcat(guess, "-(data|music)$|");
	if (guess_chk(GUESS_DOC))
	    strcat(guess, "-doc$|");
	if (guess_chk(GUESS_KERNEL))
	    strcat(guess, "(-modules|^nvidia-kernel)-.*[[:digit:]]+\\.[[:digit:]]+\\.[[:digit:]]+");
	if (guess_chk(GUESS_SECTION)) {
	    regcomp(&re_gnugrepv, "(-perl|-dev|-doc|-dbg)$|^lib(mono|pam|reoffice)|-(ruby[[:digit:].]*|bin|tools|utils|dbgsym)$", 
		    REG_EXTENDED | REG_FLAGS);
	}

	/* GUESS_DUMMY is a fake. It is not handled like other guess options
	 * because the package's name is not really checked, but its
	 * description line. The reason we sort of pretend it's a real
	 * guess option is that we can combine it with --guess-only and
	 * guess-all.
	 */
	if (guess_chk(GUESS_DUMMY)) {
	    regcomp(&re_descdummy, "^Description:.*dummy", 
		    REG_EXTENDED | REG_FLAGS);
	    regcomp(&re_desctransit, "^Description:.*transition(|n)($|ing|al|ary| package| purposes)", 
		    REG_EXTENDED | REG_FLAGS);
	}
	if (guess[strlen(guess)-1] == '|')
	    guess[strlen(guess)-1] = '\0';

        if (!guess_unique(GUESS_SECTION))
	    regcomp(&re_namedev, guess, REG_EXTENDED | REG_FLAGS);
    }
}

void
free_pkg_regex()
{
    regfree(&re_statusinst);
    regfree(&re_statusnotinst);
    regfree(&re_statushold);
    regfree(&re_statusconfig);
    regfree(&re_status);
    regfree(&re_namedev);
    regfree(&re_gnugrepv);
    regfree(&re_descdummy);
    regfree(&re_desctransit);
}

/* A similar "hack" was created by Paul Martin a while ago. It was not
 * implemented then for various reasons. This selects the function to
 * call to get the info, based on the first few characters.
 * Not as versatile as regular expressions, but it makes up for that in
 * speed.
 */
void
get_pkg_info(const char *line, pkg_info * package, int *multiarch)
{
    const char c = upcase(line[0]);
    char t;

    if (strchr(line, ':') == 0) {
	exit_invalid_statusfile();
    }

    switch (c) {
    case 'P':
	t = upcase(line[2]);
	switch (t) {
	case 'I':		/* PrIority */
	    get_pkg_priority(line, package);
	    break;
	case 'C':		/* PaCkage */
	    get_pkg_name(line, package);
	    break;
	case 'O':		/* PrOvides */
	    get_pkg_provides(line, package);
	    break;
	case 'E':		/* PrE-depends */
	    get_pkg_deps(line, package);
	    break;
	}
	break;
    case 'D':
	switch (upcase(line[2])) {
	    case 'P':   /* DePends */
		get_pkg_deps(line, package);
		break;
	    case 'S':   /* DeScription */
		get_pkg_dummy(line, package);
		break;
	}
	break;
    case 'E': 			/* Essential */
	get_pkg_essential(line, package);
	break;
    case 'I':
	get_pkg_installed_size(line, package);
	break;
    case 'R':
	switch (upcase(line[2])) {
	    case 'C':		/* ReCommends */
		if (!options[IGNORE_RECOMMENDS] && options[NICE_MODE])
		    get_pkg_deps(line, package);
		break;
	}
	break;
    case 'S':
	t = upcase(line[1]);
	switch (t) {
	case 'E':		/* SEction */
	    get_pkg_section(line, package);
	    break;
	case 'T':		/* STatus */
	    get_pkg_status(line, package);
	    break;
	case 'U':		/* SUggests */
	    if (!options[IGNORE_SUGGESTS] && options[NICE_MODE])
		get_pkg_deps(line, package);
	    break;
	}
	break;
    case 'A':
	if (strncmp("Architecture:", line, sizeof("Architecture:") - 1) == 0) {
	    static char *firstarchfound = NULL;
	    /* Spaces are removed from the line, so the field's value starts
	     * directly after the colon. */
	    const char *arch = line + sizeof("Architecture:") - 1;
	    package->self.arch = xstrdup(arch);
	    if (*multiarch == 1
		    || strcmp(arch, "all") == 0)
		break;
	    if (firstarchfound == NULL)
		firstarchfound = xstrdup(arch);
	    else if (strcmp(arch, firstarchfound) != 0) {
		*multiarch = 1;
		/* The variable firstarchfound is only needed to detect if
		 * packages from multiple architectures are installed (we can't
		 * ask dpkg because the read status file might belong to
		 * a different system).  Now that we know that the status file
		 * contains multiple architectures we can free firstarchfound.*/
		free(firstarchfound);
		firstarchfound = NULL;
	    }
	}
	break;
    }
}

void
get_pkg_essential(const char *line, pkg_info *package)
{
    if (strcasecmp(line, "Essential:yes") == 0)
	package->essential = 1;
}

void
get_pkg_installed_size(const char *line, pkg_info *package)
{
    if (strncasecmp(line, "Installed-Size:", 15) == 0)
	package->installed_size = strtol(line + 15, NULL, 10);
}

void
get_pkg_dummy(const char *line, pkg_info *package)
{
    if (!guess_chk(GUESS_DUMMY)) 
	return;

    if (regexec(&re_descdummy, line, 0, NULL, 0) == 0 || 
        regexec(&re_desctransit, line, 0, NULL, 0) == 0) {
	package->dummy = 1;
    }
}

void
get_pkg_deps(const char *line, pkg_info * package)
{
    char *tok, *line2, *version = NULL;
    signed int num_deps, i;
    unsigned int dup = 0;
    dep d;

    line2 = strchr(line, ':')+1;

    num_deps = package->deps_cnt;

    for (; (tok = strsep(&line2, ",|")); num_deps++) {
	/* Versions are up to dpkg. */
        if ((version = strchr(tok, '(')))
            *version = '\0';

	(void) set_dep(&d, tok);
	dup = 0;
	for (i = 0; i<num_deps; i++ ) {
	    if (pkgcmp(package->deps[i], d)) {
		dup = 1;
                /*@innerbreak@*/
		break;
	    }
	}

	if (dup)
	    num_deps--;
	else {
	    if (num_deps >= package->deps_max) {
		/* grow deps[] array */
		package->deps_max = package->deps_max ? package->deps_max*2 : INIT_DEPENDS_COUNT;
#ifdef DEBUG
		fprintf(stderr, "Growing deps field to %d.\n", package->deps_max);
		fflush(stderr);
#endif /* DEBUG */
		package->deps = xrealloc( package->deps, package->deps_max * sizeof(package->deps[0]) );
	    }
	    package->deps[num_deps] = d;
	}
    }

    package->deps_cnt = num_deps;
}

void
get_pkg_priority(const char *line, pkg_info * package)
{
    set_priority(package, strchr(line, ':')+1);
}

void
get_pkg_provides(const char *line, pkg_info * package)
{
    char *prov, *name;
    int i = 0;

    prov = strchr(line, ':') + 1;

    for (i = 0; (name = strsep(&prov, ",")) || !i; i++) {
	if (!name)
	    name = prov;
	(void) set_provides(package, name, i);
    }
    package->provides_cnt = i;
}

void
get_pkg_name(const char *line, pkg_info * package)
{
    char *name = strchr(line, ':') + 1;

    package->self.name = xstrdup(name);
    package->self.namehash = strhash(name);
}

void
get_pkg_status(const char *line, pkg_info * package)
{
    if (!regexec(&re_statusinst, line, 0, NULL, 0)) {
	set_install(package);
	if (!options[FORCE_HOLD]) {
	    if (!regexec(&re_statushold, line, 0, NULL, 0))
		set_hold(package);
	}
    } else if (!regexec(&re_statusconfig, line, 0, NULL, 0)) {
	if (options[FIND_CONFIG]) set_config(package);
    } else if (regexec(&re_statusnotinst, line, 0, NULL, 0)) {
	/* The package state is neither installed, config-files nor
	 * not-installed.  It is also possible that get_pkg_info()
	 * wrongly detected the current line as a status line.
	 *
	 * Abort with error message "improper state" if we
	 * really parsed a status line.
	 */
	if (!regexec(&re_status, line, 0, NULL, 0))
	    exit_improperstate();
    }
}

/* Okay, this function does not really check the libraryness of a package,
 * but it checks wether the package should be checked (1) or not (0).
 */
unsigned int
is_library(pkg_info *package, int search_libdevel)
{
    if (options[ALL_PACKAGES])
	return 1;

    if (!package->section)
	return 0;

#ifndef IGNORE_ESSENTIAL
    if (package->essential)
	return 0;
#endif

    /* Mono libraries must be handeled especially since Mono puts its
     * development libraries in section libs.
     */
    if (!guess_chk(GUESS_MONO) && !strncmp(package->self.name, "libmono", 7))
	    return 0;

    if (!options[GUESS_ONLY]) {
	if (strstr(package->section, "/libs") || 
	    strstr(package->section, "/oldlibs") ||
	    (search_libdevel && strstr(package->section, "/libdevel")))
	    return 1;
    }

    if (!options[GUESS])
	return 0;

    /* See comments in init_pkg_regex(). */
    if (guess_chk(GUESS_DUMMY) && package->dummy)
	return 1;

    /* Avoid checking package name if we're only checking dummy,
     * which we get from the description line. 
     */
    if (guess_unique(GUESS_DUMMY))
	return 0;

    /* ^lib has been removed from re_namedev in deborphan 1.7.28, thus mark
     * this package as to be checked when re_namedev matches. When
     * GUESS_SECTION is set unique the regex always matches wrongly.
     */
    if (!guess_unique(GUESS_SECTION))
	if (!regexec(&re_namedev, package->self.name, 0, NULL, 0))
	    return 1;

    if (!guess_chk(GUESS_SECTION))
        return 0;

    /* Check whether the package begins with lib, but not if it ends in one of:
     * -dbg, -doc, -perl or -dev. This is what --guess-section does.
     */
    if (!strncmp(package->self.name, "lib", 3))
	if (regexec(&re_gnugrepv, package->self.name, 0, NULL, 0))
	    return 1;

    return 0;
}

void
get_pkg_section(const char *line, pkg_info * package)
{
    char *section;

    section = strchr(line, ':')+1;

    if (strchr(section, '/'))
	set_section(package, section, NULL);
    else
	set_section(package, section, "main");
}