summaryrefslogtreecommitdiff
path: root/tools/rpmsort.c
blob: 5b8acea5a6fb60a227ef1ce693c80f500cb5f9dc (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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include "system.h"

#include <rpmlib.h>
#include <rpmmacro.h>
#include <rpmurl.h>

#include "rpmdb.h"
#include "rpmte.h"
#include "rpmts.h"

#include "manifest.h"
#include "misc.h"
#include "debug.h"

static int _depends_debug;

static int noAvailable = 1;
static const char * avdbpath =
	"/usr/lib/rpmdb/%{_arch}-%{_vendor}-%{_os}/redhat";
static int noChainsaw = 0;
static int noDeps = 0;

static inline /*@observer@*/ const char * const identifyDepend(int_32 f)
	/*@*/
{
    if (isLegacyPreReq(f))
	return "PreReq:";
    f = _notpre(f);
    if (f & RPMSENSE_SCRIPT_PRE)
	return "Requires(pre):";
    if (f & RPMSENSE_SCRIPT_POST)
	return "Requires(post):";
    if (f & RPMSENSE_SCRIPT_PREUN)
	return "Requires(preun):";
    if (f & RPMSENSE_SCRIPT_POSTUN)
	return "Requires(postun):";
    if (f & RPMSENSE_SCRIPT_VERIFY)
	return "Requires(verify):";
    if (f & RPMSENSE_FIND_REQUIRES)
	return "Requires(auto):";
    return "Requires:";
}

static int
do_tsort(const char *fileArgv[])
{
    const char * rootdir = "/";
    rpmTransactionSet ts = NULL;
    const char ** pkgURL = NULL;
    char * pkgState = NULL;
    const char ** fnp;
    const char * fileURL = NULL;
    int numPkgs = 0;
    int numFailed = 0;
    int prevx;
    int pkgx;
    const char ** argv = NULL;
    int argc = 0;
    const char ** av = NULL;
    int ac = 0;
    Header h;
    int rc = 0;
    int i;

    if (fileArgv == NULL)
	return 0;

    ts = rpmtsCreate();
    if (!noChainsaw)
	ts->transFlags = RPMTRANS_FLAG_CHAINSAW;

    rc = rpmtsOpenDB(ts, O_RDONLY);
    if (rc) {
	rpmMessage(RPMMESS_ERROR, _("cannot open Packages database\n"));
	rc = -1;
	goto exit;
    }

    /* Load all the available packages. */
    if (!(noDeps || noAvailable)) {
	rpmdbMatchIterator mi = NULL;
	struct rpmdb_s * avdb = NULL;

	addMacro(NULL, "_dbpath", NULL, avdbpath, RMIL_CMDLINE);
	rc = rpmdbOpen(rootdir, &avdb, O_RDONLY, 0644);
	delMacro(NULL, "_dbpath");
	if (rc) {
	    rpmMessage(RPMMESS_ERROR, _("cannot open Available database\n"));
	    goto endavail;
	}
        mi = rpmdbInitIterator(avdb, RPMDBI_PACKAGES, NULL, 0);
	while ((h = rpmdbNextIterator(mi)) != NULL) {
	    rpmtsAvailablePackage(ts, h, NULL);
	}

endavail:
	if (mi) rpmdbFreeIterator(mi);
	if (avdb) rpmdbClose(avdb);
    }

    /* Build fully globbed list of arguments in argv[argc]. */
    for (fnp = fileArgv; *fnp; fnp++) {
	av = _free(av);
	ac = 0;
	rc = rpmGlob(*fnp, &ac, &av);
	if (rc || ac == 0) continue;

	argv = (argc == 0)
	    ? xmalloc((argc+2) * sizeof(*argv))
	    : xrealloc(argv, (argc+2) * sizeof(*argv));
	memcpy(argv+argc, av, ac * sizeof(*av));
	argc += ac;
	argv[argc] = NULL;
    }
    av = _free(av);

    numPkgs = 0;
    prevx = 0;
    pkgx = 0;

restart:
    /* Allocate sufficient storage for next set of args. */
    if (pkgx >= numPkgs) {
	numPkgs = pkgx + argc;
	pkgURL = (pkgURL == NULL)
	    ? xmalloc( (numPkgs + 1) * sizeof(*pkgURL))
	    : xrealloc(pkgURL, (numPkgs + 1) * sizeof(*pkgURL));
	memset(pkgURL + pkgx, 0, ((argc + 1) * sizeof(*pkgURL)));
	pkgState = (pkgState == NULL)
	    ? xmalloc( (numPkgs + 1) * sizeof(*pkgState))
	    : xrealloc(pkgState, (numPkgs + 1) * sizeof(*pkgState));
	memset(pkgState + pkgx, 0, ((argc + 1) * sizeof(*pkgState)));
    }

    /* Copy next set of args. */
    for (i = 0; i < argc; i++) {
	fileURL = _free(fileURL);
	fileURL = argv[i];
	argv[i] = NULL;
	pkgURL[pkgx] = fileURL;
	fileURL = NULL;
	pkgx++;
    }
    fileURL = _free(fileURL);

    /* Continue processing file arguments, building transaction set. */
    for (fnp = pkgURL+prevx; *fnp; fnp++, prevx++) {
	const char * fileName;
	FD_t fd;

	(void) urlPath(*fnp, &fileName);

	/* Try to read the header from a package file. */
	fd = Fopen(*fnp, "r.ufdio");
	if (fd == NULL || Ferror(fd)) {
	    rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), *fnp,
			Fstrerror(fd));
	    if (fd) Fclose(fd);
	    numFailed++; *fnp = NULL;
	    continue;
	}

	rc = rpmReadPackageFile(ts, fd, *fnp, &h);
	Fclose(fd);

	if (rc == 2) {
	    numFailed++; *fnp = NULL;
	    continue;
	}

	if (rc == 0) {
	    rc = rpmtsAddPackage(ts, h, (fnpyKey)fileName, 0, NULL);
	    headerFree(h, "do_tsort"); 
	    continue;
	}

	if (rc != 1) {
	    rpmMessage(RPMMESS_ERROR, _("%s cannot be installed\n"), *fnp);
	    numFailed++; *fnp = NULL;
	    break;
	}

	/* Try to read a package manifest. */
	fd = Fopen(*fnp, "r.fpio");
	if (fd == NULL || Ferror(fd)) {
	    rpmError(RPMERR_OPEN, _("open of %s failed: %s\n"), *fnp,
			Fstrerror(fd));
	    if (fd) Fclose(fd);
	    numFailed++; *fnp = NULL;
	    break;
	}

	/* Read list of packages from manifest. */
	rc = rpmReadPackageManifest(fd, &argc, &argv);
	if (rc)
	    rpmError(RPMERR_MANIFEST, _("%s: read manifest failed: %s\n"),
			fileURL, Fstrerror(fd));
	Fclose(fd);

	/* If successful, restart the query loop. */
	if (rc == 0) {
	    prevx++;
	    goto restart;
	}

	numFailed++; *fnp = NULL;
	break;
    }

    if (numFailed) goto exit;

    if (!noDeps) {
	rpmProblemSet ps;

	rc = rpmtsCheck(ts);

	ps = rpmtsGetProblems(ts);
	if (ps) {
	    rpmMessage(RPMMESS_ERROR, _("Failed dependencies:\n"));
	    printDepProblems(stderr, ps);
	    ps = rpmProblemSetFree(ps);
	    rc = -1;
	    goto exit;
	}
	ps = rpmProblemSetFree(ps);
    }

    rc = rpmtsOrder(ts);
    if (rc)
	goto exit;

    {	rpmDepSet requires;
	teIterator pi; transactionElement p;
	teIterator qi; transactionElement q;
	unsigned char * selected =
			alloca(sizeof(*selected) * (ts->orderCount + 1));
	int oType = TR_ADDED;

fprintf(stdout, "digraph XXX {\n");

fprintf(stdout, "  rankdir=LR\n");

fprintf(stdout, "//===== Packages:\n");
	pi = teInitIterator(ts);
	while ((p = teNext(pi, oType)) != NULL) {
fprintf(stdout, "//%5d%5d %s\n", teGetTree(p), teGetDepth(p), teGetN(p));
	    q = teGetParent(p);
	    if (q != NULL)
fprintf(stdout, "  \"%s\" -> \"%s\"\n", teGetN(p), teGetN(q));
	    else {
fprintf(stdout, "  \"%s\"\n", teGetN(p));
fprintf(stdout, "  { rank=max ; \"%s\" }\n", teGetN(p));
	    }
	}
	pi = teFreeIterator(pi);

#ifdef	NOTNOW
fprintf(stdout, "//===== Relations:\n");
	pi = teInitIterator(ts);
	while ((p = teNext(pi, oType)) != NULL) {
	    int printed;

	    if ((requires = teGetDS(p, RPMTAG_REQUIRENAME)) == NULL)
		continue;

	    memset(selected, 0, sizeof(*selected) * ts->orderCount);
	    selected[teiGetOc(pi)] = 1;
	    printed = 0;

	    requires = dsiInit(requires);
	    while (dsiNext(requires) >= 0) {
		int_32 Flags;
		const char * qName;
		fnpyKey key;
		alKey pkgKey;
		int i;

		Flags = dsiGetFlags(requires);

		switch (teGetType(p)) {
		case TR_REMOVED:
		    /* Skip if not %preun/%postun requires or legacy prereq. */
		    if (isInstallPreReq(Flags)
#ifdef	NOTYET
		     || !( isErasePreReq(Flags)
		        || isLegacyPreReq(Flags) )
#endif
		        )
		        /*@innercontinue@*/ continue;
		    /*@switchbreak@*/ break;
		case TR_ADDED:
		    /* Skip if not %pre/%post requires or legacy prereq. */
		    if (isErasePreReq(Flags)
#ifdef	NOTYET
		     || !( isInstallPreReq(Flags)
		        || isLegacyPreReq(Flags) )
#endif
		        )
		        /*@innercontinue@*/ continue;
		    /*@switchbreak@*/ break;
		}

		if ((qName = dsiGetN(requires)) == NULL)
		    continue;	/* XXX can't happen */
		if (!strncmp(qName, "rpmlib(", sizeof("rpmlib(")-1))
		    continue;

		pkgKey = RPMAL_NOMATCH;
		key = alSatisfiesDepend(ts->addedPackages, requires, &pkgKey);
		if (pkgKey == RPMAL_NOMATCH)
		    continue;

		for (qi = teInitIterator(ts), i = 0;
		     (q = teNextIterator(qi)) != NULL; i++)
		{
		    if (teGetType(q) == TR_REMOVED)
			continue;
		    if (pkgKey == teGetAddedKey(q))
			break;
		}
		qi = teFreeIterator(qi);

		if (q == NULL || i == ts->orderCount)
		    continue;
		if (selected[i] != 0)
		    continue;
		selected[i] = 1;

		if (teGetTree(p) == teGetTree(q)) {
		    int pdepth = teGetDepth(p);
		    int qdepth = teGetDepth(q);

#if 0
		    if (pdepth == qdepth)
			continue;
		    if (pdepth < qdepth) {
			if ((qdepth - pdepth) > 1)	continue;
			if (!strcmp(teGetN(q), "glibc"))	continue;
			if (!strcmp(teGetN(q), "bash"))	continue;
		    }
#endif
		    if (pdepth > qdepth) {
			if (!strcmp(teGetN(q), "glibc"))	continue;
			if (!strcmp(teGetN(q), "bash"))		continue;
			if (!strcmp(teGetN(q), "info"))		continue;
			if (!strcmp(teGetN(q), "mktemp"))	continue;
		    }
		}

if (!printed) {
fprintf(stdout, "// %s\n", teGetN(p));
printed = 1;
}
fprintf(stdout, "//\t%s (0x%x)\n", dsDNEVR(identifyDepend(Flags), requires), Flags);
fprintf(stdout, "\t\"%s\" -> \"%s\"\n", teGetN(p), teGetN(q));

	    }

	}
	pi = teFreeIterator(pi);
#endif	/* NOTNOW */

fprintf(stdout, "}\n");

    }

    rc = 0;

exit:
    for (i = 0; i < numPkgs; i++) {
        pkgURL[i] = _free(pkgURL[i]);
    }
    pkgState = _free(pkgState);
    pkgURL = _free(pkgURL);
    argv = _free(argv);

    ts = rpmtsFree(ts);
    return rc;
}

static struct poptOption optionsTable[] = {
 { "noavailable", '\0', 0, &noAvailable, 0,	NULL, NULL},
 { "nochainsaw", '\0', 0, &noChainsaw, 0,	NULL, NULL},
 { "nodeps", '\0', 0, &noDeps, 0,		NULL, NULL},
 { "verbose", 'v', 0, 0, 'v',			NULL, NULL},
 { NULL,	0, 0, 0, 0,			NULL, NULL}
};

int
main(int argc, const char *argv[])
{
    poptContext optCon;
    const char * optArg;
    int arg;
    int ec = 0;

#if HAVE_MCHECK_H && HAVE_MTRACE
    mtrace();	/* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
#endif
    setprogname(argv[0]);	/* Retrofit glibc __progname */
    (void)setlocale(LC_ALL, "" );

#ifdef  __LCLINT__
#define LOCALEDIR	"/usr/share/locale"
#endif
    (void)bindtextdomain(PACKAGE, LOCALEDIR);
    (void)textdomain(PACKAGE);

    _depends_debug = 1;

    optCon = poptGetContext("rpmsort", argc, argv, optionsTable, 0);
    poptReadDefaultConfig(optCon, 1);

    while ((arg = poptGetNextOpt(optCon)) > 0) {
	optArg = poptGetOptArg(optCon);
	switch (arg) {
	case 'v':
	    rpmIncreaseVerbosity();
	    break;
	default:
	    fprintf(stderr, _("unknown popt return (%d)"), arg);
	    exit(EXIT_FAILURE);
	    /*@notreached@*/ break;
	}
    }

    rpmReadConfigFiles(NULL, NULL);

    ec = do_tsort(poptGetArgs(optCon));

    optCon = poptFreeContext(optCon);

    return ec;
}