summaryrefslogtreecommitdiff
path: root/lib/ts.c
blob: 4e1fd1f57a0bdc81fc04ba7a1d5afafaa246ab9b (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
#include "system.h"
#include <fnmatch.h>
#include <fts.h>

#include <rpmlib.h>
#include <rpmpgp.h>

#include "rpmts.h"

#include "debug.h"

static int _debug = 1;

#define	BHPATH	"/mnt/redhat/beehive/comps/dist"
#define	BHCOLL	"7.2"

#define	BHN	"@(basesystem|bash|filesystem|glibc-common|glibc|ldconfig|libtermcap|mktemp|setup|termcap)"

#define	BHVR	"*"
#define	BHA	"@(i[3456]86|noarch)"

const char * bhpath = BHPATH;
int bhpathlen = sizeof(BHPATH)-1;

int bhlvl = -1;

static char * const ftsSet[] = {
    BHPATH, NULL,
};

struct ftsglob_s {
    const char ** patterns;
    int fnflags;
};

const char * lvl0[] = {
    BHPATH, NULL
};

const char * lvl1[] = {
    BHCOLL, NULL
};

const char * lvl2[] = {
    BHN, NULL
};

const char * lvl3[] = {
    BHVR, NULL
};

const char * lvl4[] = {
    BHA, NULL
};

static struct ftsglob_s bhglobs[] = {
    { lvl0,	(FNM_PATHNAME | FNM_PERIOD | FNM_EXTMATCH) },
    { lvl1,	(FNM_PATHNAME | FNM_PERIOD | FNM_EXTMATCH) },
    { lvl2,	(FNM_PATHNAME | FNM_PERIOD | FNM_EXTMATCH) },
    { lvl3,	(FNM_PATHNAME | FNM_PERIOD | FNM_EXTMATCH) },
    { lvl4,	(FNM_PATHNAME | FNM_PERIOD | FNM_EXTMATCH) },
};
static int nbhglobs = sizeof(bhglobs)/sizeof(bhglobs[0]);

static const char * ftsInfoStrings[] = {
    "UNKNOWN",
    "D",
    "DC",
    "DEFAULT",
    "DNR",
    "DOT",
    "DP",
    "ERR",
    "F",
    "INIT",
    "NS",
    "NSOK",
    "SL",
    "SLNONE",
    "W",
};

static const char * ftsInfoStr(int fts_info) {
    if (!(fts_info >= 1 && fts_info <= 14))
	fts_info = 0;
    return ftsInfoStrings[ fts_info ];
}

static int ftsPrint(FTS * ftsp, FTSENT * fts, rpmTransactionSet ts)
{
    struct ftsglob_s * bhg;
    const char ** patterns;
    const char * pattern;
    const char * n, * v, * r;
    const char * s;
    FD_t fd = NULL;
    Header h = NULL;
    rpmRC rc;
    int indent = 2;
    int lvl;
    int xx;

    switch (fts->fts_info) {
    case FTS_D:		/* preorder directory */
	if (fts->fts_pathlen < bhpathlen)
	    break;

	/* Grab the level of the beehive top directory. */
	if (bhlvl < 0) {
	    if (fts->fts_pathlen == bhpathlen && !strcmp(fts->fts_path, bhpath))
		bhlvl = fts->fts_level;
	    else
		break;
	}
	lvl = fts->fts_level - bhlvl;

	if (lvl < 0)
	    break;

#if 0
	fprintf(stderr, "FTS_%s\t%*s %s\n", ftsInfoStr(fts->fts_info),
		indent * (fts->fts_level < 0 ? 0 : fts->fts_level), "",
		fts->fts_name);
#endif

	/* Full path glob expression check. */
	bhg = bhglobs;

	if ((patterns = bhg->patterns) != NULL)
	while ((pattern = *patterns++) != NULL) {
	    if (*pattern == '/')
		xx = fnmatch(pattern, fts->fts_path, bhg->fnflags);
	    else
		xx = fnmatch(pattern, fts->fts_name, bhg->fnflags);
	    if (xx == 0)
		break;
	}

	/* Level specific glob expression check(s). */
	if (lvl == 0 || lvl >= nbhglobs)
	    break;
	bhg += lvl;

	if ((patterns = bhg->patterns) != NULL)
	while ((pattern = *patterns++) != NULL) {
	    if (*pattern == '/')
		xx = fnmatch(pattern, fts->fts_path, bhg->fnflags);
	    else
		xx = fnmatch(pattern, fts->fts_name, bhg->fnflags);
	    if (xx == 0)
		break;
	    else
		xx = fts_set(ftsp, fts, FTS_SKIP);
	}

	break;
    case FTS_DP:	/* postorder directory */
#if 0
	fprintf(stderr, "FTS_%s\t%*s %s\n", ftsInfoStr(fts->fts_info),
		indent * (fts->fts_level < 0 ? 0 : fts->fts_level), "",
		fts->fts_name);
#endif
	break;
    case FTS_F:		/* regular file */
	if (fts->fts_level >= 0) {
	    if (strcmp(fts->fts_parent->fts_name, "SRPMS")) {
		xx = fts_set(ftsp, fts->fts_parent, FTS_SKIP);
		break;
	    }
	}
	s = fts->fts_name + fts->fts_namelen + 1 - sizeof(".rpm");
	if (strcmp(s, ".rpm"))
	    break;
	rpmMessage(RPMMESS_DEBUG, "============== %s\n", fts->fts_accpath);
	fd = Fopen(fts->fts_accpath, "r");
	if (fd == NULL || Ferror(fd))
	    break;
	rc = rpmReadPackageFile(ts, fd, fts->fts_path, &h);
	xx = Fclose(fd);
	fd = NULL;
	if (rc != RPMRC_OK)
	    break;
	xx = headerNVR(h, &n, &v, &r);
	fprintf(stderr, "\t%*s %s-%s-%s\n",
		indent * (fts->fts_level < 0 ? 0 : fts->fts_level), "",
		n, v, r);
#ifdef NOTYET
	xx = rpmtsAddPackage(ts, h, fts->fts_path, 1, NULL);
#endif

	break;
    case FTS_NS:	/* stat(2) failed */
    case FTS_DNR:	/* unreadable directory */
    case FTS_ERR:	/* error; errno is set */
	fprintf(stderr, "FTS_%s\t%*s %s\n", ftsInfoStr(fts->fts_info),
		indent * (fts->fts_level < 0 ? 0 : fts->fts_level), "",
		fts->fts_name);
	break;
    case FTS_DC:	/* directory that causes cycles */
    case FTS_DEFAULT:	/* none of the above */
    case FTS_DOT:	/* dot or dot-dot */
    case FTS_INIT:	/* initialized only */
    case FTS_NSOK:	/* no stat(2) requested */
    case FTS_SL:	/* symbolic link */
    case FTS_SLNONE:	/* symbolic link without target */
    case FTS_W:		/* whiteout object */
    default:
	fprintf(stderr, "FTS_%s\t%*s %s\n", ftsInfoStr(fts->fts_info),
		indent * (fts->fts_level < 0 ? 0 : fts->fts_level), "",
		fts->fts_name);
	break;
    }

    h = headerFree(h, "ftsPrint exit");
    if (fd)
	xx = Fclose(fd);
    return 0;
}

static int depth;
static int level;
static int globx;

static void go (const char * sb, const char * se)
{
    const char * s = sb;
    int blvl = 0;
    int plvl = 0;
    int c;

    globx = -1;
    while ( s < se && (c = *s) != '\0' ) {
	s++;
	switch (c) {
	case '+':
	case '@':
	case '!':
	    continue;
	case '?':
	case '*':
	    if (*s == '(')
		continue;
	    if (globx < 0)
		globx = (s - sb - 1);
	    continue;
	case '[':
	    blvl++;
	    continue;
	case ']':
	    if (!blvl)
		continue;
	    --blvl;
	    if (!blvl && globx < 0)
		globx = (s - sb - 1);
	    continue;
	case '(':
	    plvl++;
	    continue;
	case '|':
	    continue;
	case ')':
	    if (!plvl)
		continue;
	    --plvl;
	    if (!plvl && globx < 0)
		globx = (s - sb - 1);
	    continue;
	case '\\':
	    if (*s)
		s++;
	    continue;
	case '/':
	    depth++;
	    continue;
	default:
	    continue;
	}
    }
}

static struct poptOption optionsTable[] = {
 { "verbose", 'v', 0, 0, 'v',				NULL, NULL },
 { "debug", 'd', POPT_ARG_VAL,	&_debug, -1,		NULL, NULL },
  POPT_AUTOHELP
  POPT_TABLEEND
};

int
main(int argc, const char *argv[])
{
    poptContext optCon = poptGetContext(argv[0], argc, argv, optionsTable, 0);
    const char * rootDir = "";
    rpmTransactionSet ts = NULL;
    FTS * ftsp;
    int ftsOpts = (FTS_COMFOLLOW | FTS_LOGICAL | FTS_NOSTAT);
    FTSENT * fts;
    char buf[BUFSIZ];
    const char ** args;
    const char * arg;
    char * t;
    int rc;
    int ec = 1;
    int xx;

    while ((rc = poptGetNextOpt(optCon)) > 0) {
	switch (rc) {
	case 'v':
	    rpmIncreaseVerbosity();
	    /*@switchbreak@*/ break;
	default:
            /*@switchbreak@*/ break;
	}
    }

    rpmReadConfigFiles(NULL, NULL);

    if (_debug) {
	rpmIncreaseVerbosity();
	rpmIncreaseVerbosity();
    }

    ts = rpmtsCreate();
    (void) rpmtsSetRootDir(rootDir);
    (void) rpmtsOpenDB(ts, O_RDONLY);

    args = poptGetArgs(optCon);
    
    t = buf;
    *t = '\0';
    t = stpcpy(t, BHPATH);
    *t++ = '/';
    t = stpcpy(t, BHCOLL);
    *t++ = '/';

    if (args == NULL)
	t = stpcpy(t, BHN);
    else {
	t = stpcpy(t, "@(");
	while ((arg = *args++) != NULL) {
	    t = stpcpy(t, arg);
	    *t++ = '|';
	}
	t[-1] = ')';
    }

    *t++ = '/';
    t = stpcpy(t, BHVR);
    *t++ = '/';
    t = stpcpy(t, BHA);

fprintf(stderr, "*** \"%s\"\n", buf);

    level = 0;
    depth = (buf[0] == '/') ? -1 : 0;
    globx = -1;
    go(buf, buf+strlen(buf));

fprintf(stderr, "*** \"%s/%s/%s/%s/%s\"\n", *lvl0, *lvl1, *lvl2, *lvl3, *lvl4);

#if 0
    lvl0[0] = buf;
#endif

if (!_debug) {
    ftsp = fts_open(ftsSet, ftsOpts, NULL);
    while((fts = fts_read(ftsp)) != NULL)
	xx = ftsPrint(ftsp, fts, ts);
    xx = fts_close(ftsp);
}

#ifdef	NOTYET
if (!_debug) {
    {	rpmProblemSet ps;

	xx = rpmtsCheck(ts);

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

    (void) rpmtsOrder(ts);
}
#endif

exit:
    ts = rpmtsFree(ts);

    return ec;
}