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

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

#include "depends.h"

#include "debug.h"

static int _debug = 0;

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

#if 0
#define	BHN	"{basesystem,bash,filesystem,glibc-common,glibc,ldconfig,libtermcap,mktemp,setup,termcap}"
#else
#define	BHN	"setup"
#endif

#define	BHVR	"*"
#define	BHA	"*"

const char * bhpath = BHPATH;
int bhpathlen = sizeof(BHPATH)-1;
const char * bhcoll = BHCOLL;
const char * bhn = BHN;
const char * bhvr = BHVR;
const char * bha = BHA;

int bhlvl = -1;

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

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

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

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

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

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

const char * lvl5[] = {
    NULL, NULL
};

const char * lvl6[] = {
    NULL, NULL
};

const char * lvl7[] = {
    NULL, NULL
};

static struct ftsglob_s bhglobs[] = {
#if	0
    { BHPATH "/" BHCOLL "/" BHN "/" BHVR "/" BHA "/*",	0 },
    { BHCOLL,	FNM_PERIOD },
    { BHN,	FNM_PERIOD },
    { BHVR,	FNM_PERIOD },
    { BHA,	FNM_PERIOD },
#else
    { lvl0, 	FNM_PERIOD },
    { lvl1, 	FNM_PERIOD },
    { lvl2, 	FNM_PERIOD },
    { lvl3, 	FNM_PERIOD },
    { lvl4, 	FNM_PERIOD },
    { lvl5, 	FNM_PERIOD },
    { lvl6, 	FNM_PERIOD },
    { lvl7, 	FNM_PERIOD },
#endif
};
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;
#ifdef DYING
	if (lvl <= 0) {
	    break;
	} else if (lvl == 1) {
	    /* Skip collections we're not interested in. */
	    if (!strcmp(fts->fts_name, bhcoll))
		break;
	    xx = fts_set(ftsp, fts, FTS_SKIP);
	    break;
	} else if (lvl == 2) {
	    /* Skip names we're not interested in. */
	    if (!strcmp(fts->fts_name, bhn))
		break;
	    xx = fts_set(ftsp, fts, FTS_SKIP);
	    break;
	} else if (lvl == 3) {
	    /* Skip version-release we're not interested in. */
	    if (!strcmp(fts->fts_name, bhvr))
		break;
	    xx = fts_set(ftsp, fts, FTS_SKIP);
	    break;
	} else if (lvl == 4) {
	    /* Skip arch we're not interested in. */
	    if (!strcmp(fts->fts_name, bha))
		break;
	    xx = fts_set(ftsp, fts, FTS_SKIP);
	    break;
	}
#else
	if (lvl < 0 || lvl >= nbhglobs)
	    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)
	    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);
	}
#endif
	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);
	xx = rpmtransAddPackage(ts, h, NULL, fts->fts_path, 1, NULL);
	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);
    if (fd)
	xx = Fclose(fd);
    return 0;
}

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


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 = "";
    rpmdb db = NULL;
    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);

    ts = rpmtransCreateSet(db, rootDir);
    (void) rpmtsOpenDB(ts, O_RDONLY);

    args = poptGetArgs(optCon);
    if (args)
    while ((arg = *args++) != NULL) {
    
	t = buf;
	*t = '\0';
	t = stpcpy(t, BHPATH);
	*t++ = '/';
	t = stpcpy(t, BHCOLL);
	*t++ = '/';
	t = stpcpy(t, arg);
	*t++ = '/';
	t = stpcpy(t, "*");
	*t++ = '/';
	t = stpcpy(t, "i386");
	*t++ = '/';
	t = stpcpy(t, "*.rpm");

fprintf(stderr, "*** \"%s\"\n", buf);
	lvl0[0] = buf;
	lvl1[0] = BHCOLL;
	lvl2[0] = arg;

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

    }

if (!_debug) {
    {	rpmDependencyConflict conflicts = NULL;
	int numConflicts = 0;

	(void) rpmdepCheck(ts, &conflicts, &numConflicts);

	/*@-branchstate@*/
	if (conflicts) {
	    rpmMessage(RPMMESS_ERROR, _("failed dependencies:\n"));
	    printDepProblems(stderr, conflicts, numConflicts);
	    conflicts = rpmdepFreeConflicts(conflicts, numConflicts);
	}
	/*@=branchstate@*/
    }

    (void) rpmdepOrder(ts);
}

exit:
    ts = rpmtransFree(ts);

    return ec;
}