summaryrefslogtreecommitdiff
path: root/lib/tgi.c
blob: cc23fd9878d5ab1f69efd55fefca818486e97b2a (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
#include "system.h"

#include <rpm/rpmgi.h>
#include <rpm/rpmcli.h>

#include <rpm/rpmte.h>

#include <rpm/rpmmacro.h>
#include "rpmio/fts.h"
#include <popt.h>

#include "debug.h"

static const char * gitagstr = "packages";
static const char * gikeystr = NULL;
static rpmtransFlags transFlags = 0;
#ifdef	DYING
static rpmgiFlags giFlags = 0;
#endif

static const char * queryFormat = NULL;
static const char * defaultQueryFormat =
	"%{name}-%{version}-%{release}.%|SOURCERPM?{%{arch}.rpm}:{%|ARCH?{src.rpm}:{pubkey}|}|";

static const char * rpmgiPathOrQF(const rpmgi gi)
{
    const char * fmt = ((queryFormat != NULL)
	? queryFormat : defaultQueryFormat);
    const char * val = NULL;
    Header h = rpmgiHeader(gi);

    if (h != NULL)
	val = headerSprintf(h, fmt, rpmTagTable, rpmHeaderFormats, NULL);
    else {
	const char * fn = rpmgiHdrPath(gi);
	val = (fn != NULL ? xstrdup(fn) : NULL);
    }

    return val;
}

static struct poptOption optionsTable[] = {
 { "rpmgidebug", 'd', POPT_ARG_VAL|POPT_ARGFLAG_DOC_HIDDEN, &_rpmgi_debug, -1,
	N_("debug generalized iterator"), NULL},

 { "tag", '\0', POPT_ARG_STRING|POPT_ARGFLAG_SHOW_DEFAULT, &gitagstr, 0,
	N_("iterate tag index"), NULL },
 { "key", '\0', POPT_ARG_STRING|POPT_ARGFLAG_SHOW_DEFAULT, &gikeystr, 0,
	N_("tag value key"), NULL },

 { "transaction", 'T', POPT_BIT_SET, &giFlags, (RPMGI_TSADD|RPMGI_TSORDER),
	N_("create transaction set"), NULL},
 { "noorder", '\0', POPT_BIT_CLR, &giFlags, RPMGI_TSORDER,
	N_("do not order transaction set"), NULL},
 { "noglob", '\0', POPT_BIT_SET, &giFlags, RPMGI_NOGLOB,
	N_("do not glob arguments"), NULL},
 { "nomanifest", '\0', POPT_BIT_SET, &giFlags, RPMGI_NOMANIFEST,
	N_("do not process non-package files as manifests"), NULL},
 { "noheader", '\0', POPT_BIT_SET, &giFlags, RPMGI_NOHEADER,
	N_("do not read headers"), NULL},

 { "qf", '\0', POPT_ARG_STRING, &queryFormat, 0,
        N_("use the following query format"), "QUERYFORMAT" },
 { "queryformat", '\0', POPT_ARG_STRING, &queryFormat, 0,
        N_("use the following query format"), "QUERYFORMAT" },

 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliFtsPoptTable, 0,
        N_("File tree walk options for fts(3):"),
        NULL },

 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
        N_("Common options for all rpm modes and executables:"),
        NULL },

  POPT_AUTOALIAS
  POPT_AUTOHELP
  POPT_TABLEEND
};

int
main(int argc, char *argv[])
{
    poptContext optCon;
    rpmts ts = NULL;
    rpmVSFlags vsflags;
    rpmgi gi = NULL;
    int gitag = RPMDBI_PACKAGES;
    const char ** av;
    int ac;
    int rc = 0;

    optCon = rpmcliInit(argc, argv, optionsTable);
    if (optCon == NULL)
        exit(EXIT_FAILURE);

    if (ftsOpts == 0)
	ftsOpts = (FTS_COMFOLLOW | FTS_LOGICAL | FTS_NOSTAT);

    if (gitagstr != NULL) {
	gitag = rpmTagGetValue(gitagstr);
	if (gitag < 0) {
	    fprintf(stderr, _("unknown --tag argument: %s\n"), gitagstr);
	    exit(EXIT_FAILURE);
	}
    }

    /* XXX ftswalk segfault with no args. */

    ts = rpmtsCreate();
    (void) rpmtsSetFlags(ts, transFlags);

    vsflags = rpmExpandNumeric("%{?_vsflags_query}");
    if (rpmcliQueryFlags & VERIFY_DIGEST)
	vsflags |= _RPMVSF_NODIGESTS;
    if (rpmcliQueryFlags & VERIFY_SIGNATURE)
	vsflags |= _RPMVSF_NOSIGNATURES;
    if (rpmcliQueryFlags & VERIFY_HDRCHK)
	vsflags |= RPMVSF_NOHDRCHK;
    (void) rpmtsSetVSFlags(ts, vsflags);

    {   int32_t tid = (int32_t) time(NULL);
	(void) rpmtsSetTid(ts, tid);
    }

    gi = rpmgiNew(ts, gitag, gikeystr, 0);

    av = poptGetArgs(optCon);
    (void) rpmgiSetArgs(gi, av, ftsOpts, giFlags);

    ac = 0;
    while (rpmgiNext(gi) == RPMRC_OK) {
	if (!(giFlags & RPMGI_TSADD)) {
	    const char * arg = rpmgiPathOrQF(gi);

	    fprintf(stdout, "%5d %s\n", ac, arg);
	    arg = _free(arg);
	}
	ac++;
    }

    if (giFlags & RPMGI_TSORDER) {
	rpmtsi tsi;
	rpmte q;
	int i;
	
fprintf(stdout, "======================= %d transaction elements\n\
    # Tree Depth Degree Package\n\
=======================\n", rpmtsNElements(ts));

	i = 0;
	tsi = rpmtsiInit(ts);
	while((q = rpmtsiNext(tsi, 0)) != NULL) {
	    char deptypechar;

	    if (i == rpmtsUnorderedSuccessors(ts, -1))
		fprintf(stdout, "======================= leaf nodes only:\n");

	    deptypechar = (rpmteType(q) == TR_REMOVED ? '-' : '+');
	    fprintf(stdout, "%5d%5d%6d%7d %*s%c%s\n",
		i, rpmteTree(q), rpmteDepth(q), rpmteDegree(q),
		(2 * rpmteDepth(q)), "",
		deptypechar, rpmteNEVRA(q));
	    i++;
	}
	tsi = rpmtsiFree(tsi);
    }

    gi = rpmgiFree(gi);
    ts = rpmtsFree(ts);
    optCon = rpmcliFini(optCon);

    return rc;
}