summaryrefslogtreecommitdiff
path: root/lib/rpmal.c
blob: c878050fb061b62875922ced6964827d1c707f1f (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
/** \ingroup rpmdep
 * \file lib/rpmal.c
 */

#include "system.h"


#include <rpm/rpmds.h>
#include <rpm/rpmte.h>
#include <rpm/rpmfi.h>

#include "lib/rpmal.h"
#include "lib/rpmhash.h"

#include "debug.h"

typedef struct availablePackage_s * availablePackage;
typedef int rpmalNum;

int _rpmal_debug = 0;


/** \ingroup rpmdep
 * Info about a single package to be installed.
 */
struct availablePackage_s {
    rpmte p;                    /*!< transaction member */
    rpmds provides;		/*!< Provides: dependencies. */
    rpmfi fi;			/*!< File info set. */
    fnpyKey key;		/*!< Associated file name/python object */
};

typedef struct availableIndexEntry_s *	availableIndexEntry;

/** \ingroup rpmdep
 * A single available item (e.g. a Provides: dependency).
 */
struct availableIndexEntry_s {
    rpmalNum pkgNum;	        /*!< Containing package index. */
    unsigned int entryIx;	/*!< Dependency index. */
};

struct fileNameEntry_s {
    const char * dirName;
    const char * baseName;
};


/** \ingroup rpmdep
 * A file to be installed/removed.
 */
typedef struct fileIndexEntry_s * fileIndex;

struct fileIndexEntry_s {
    rpmalNum pkgNum;		/*!< Containing package index. */
    unsigned int entryIx;
};

#undef HASHTYPE
#undef HTKEYTYPE
#undef HTDATATYPE
#define HASHTYPE rpmalProvidesHash
#define HTKEYTYPE const char *
#define HTDATATYPE struct availableIndexEntry_s
#include "lib/rpmhash.H"
#include "lib/rpmhash.C"

#undef HASHTYPE
#undef HTKEYTYPE
#undef HTDATATYPE
#define HASHTYPE rpmalFileHash
#define HTKEYTYPE struct fileNameEntry_s
#define HTDATATYPE struct fileIndexEntry_s
#include "lib/rpmhash.H"
#include "lib/rpmhash.C"



/** \ingroup rpmdep
 * Set of available packages, items, and directories.
 */
struct rpmal_s {
    availablePackage list;	/*!< Set of packages. */
    rpmalProvidesHash providesHash;
    rpmalFileHash fileHash;
    int delta;			/*!< Delta for pkg list reallocation. */
    int size;			/*!< No. of pkgs in list. */
    int alloced;		/*!< No. of pkgs allocated for list. */
    rpm_color_t tscolor;	/*!< Transaction color. */
    rpm_color_t prefcolor;	/*!< Transaction preferred color. */
};

/**
 * Destroy available item index.
 * @param al		available list
 */
static void rpmalFreeIndex(rpmal al)
{
    al->providesHash = rpmalProvidesHashFree(al->providesHash);
    al->fileHash = rpmalFileHashFree(al->fileHash);
}

rpmal rpmalCreate(int delta, rpm_color_t tscolor, rpm_color_t prefcolor)
{
    rpmal al = xcalloc(1, sizeof(*al));

    al->delta = delta;
    al->size = 0;
    al->alloced = al->delta;
    al->list = xmalloc(sizeof(*al->list) * al->alloced);;

    al->providesHash = NULL;
    al->fileHash = NULL;
    al->tscolor = tscolor;
    al->prefcolor = prefcolor;

    return al;
}

rpmal rpmalFree(rpmal al)
{
    availablePackage alp;
    int i;

    if (al == NULL)
	return NULL;

    if ((alp = al->list) != NULL)
    for (i = 0; i < al->size; i++, alp++) {
	alp->provides = rpmdsFree(alp->provides);
	alp->fi = rpmfiFree(alp->fi);
    }
    al->list = _free(al->list);
    al->alloced = 0;

    rpmalFreeIndex(al);
    al = _free(al);
    return NULL;
}

static unsigned int fileHash(struct fileNameEntry_s file){
    return hashFunctionString(file.dirName) ^ hashFunctionString(file.baseName);
}

static int fileCompare(struct fileNameEntry_s one, struct fileNameEntry_s two) {
    int rc = 0;
    rc = strcmp(one.dirName, two.dirName);
    if (!rc)
	rc = strcmp(one.baseName, two.baseName);
    return rc;
}

void rpmalDel(rpmal al, rpmte p)
{
    availablePackage alp;
    rpmalNum pkgNum;

    if (al == NULL || al->list == NULL)
	return;		/* XXX can't happen */

    // XXX use a search for self provide
    for (pkgNum=0; pkgNum<al->size; pkgNum++) {
	if (al->list[pkgNum].p == p) {
	    break;
	}
    }
    if (pkgNum == al->size ) return; // Not found!

    alp = al->list + pkgNum;
    // do not actually delete, just set p to NULL
    // and later filter that out of the results
    alp->p = NULL;
}

static void rpmalAddFiles(rpmal al, rpmalNum pkgNum, rpmfi fi){
    struct fileNameEntry_s fileName;
    struct fileIndexEntry_s fileEntry;
    int i;
    rpm_color_t ficolor;

    fileEntry.pkgNum = pkgNum;

    fi = rpmfiInit(fi, 0);
    while ((i = rpmfiNext(fi)) >= 0) {
	/* Ignore colored provides not in our rainbow. */
        ficolor = rpmfiFColor(fi);
        if (al->tscolor && ficolor && !(al->tscolor & ficolor))
            continue;

	fileName.dirName = rpmfiDN(fi);
	fileName.baseName = rpmfiBN(fi);

	fileEntry.entryIx = i;

	rpmalFileHashAddEntry(al->fileHash, fileName, fileEntry);
    }
}

static void rpmalAddProvides(rpmal al, rpmalNum pkgNum, rpmds provides){
    struct availableIndexEntry_s indexEntry;
    rpm_color_t dscolor;

    indexEntry.pkgNum = pkgNum;

    if (rpmdsInit(provides) != NULL)
    while (rpmdsNext(provides) >= 0) {
        /* Ignore colored provides not in our rainbow. */
        dscolor = rpmdsColor(provides);
        if (al->tscolor && dscolor && !(al->tscolor & dscolor))
            continue;

	indexEntry.entryIx = rpmdsIx(provides);
	rpmalProvidesHashAddEntry(al->providesHash, rpmdsN(provides), indexEntry);
    }
}

void rpmalAdd(rpmal al, rpmte p)
{
    rpmalNum pkgNum;
    availablePackage alp;

    if (al->size == al->alloced) {
	al->alloced += al->delta;
	al->list = xrealloc(al->list, sizeof(*al->list) * al->alloced);
    }
    pkgNum = al->size++;

    alp = al->list + pkgNum;

    alp->p = p;

    if (_rpmal_debug)
	fprintf(stderr, "*** add %p[%d]\n", al->list, (int) pkgNum);

    alp->provides = rpmdsLink(rpmteDS(p, RPMTAG_PROVIDENAME),
			      RPMDBG_M("Provides (rpmalAdd)"));
    alp->fi = rpmfiLink(rpmteFI(p), RPMDBG_M("Files (rpmalAdd)"));

    if (al->providesHash != NULL) { // index is already created
	rpmalAddProvides(al, pkgNum, alp->provides);
	rpmalAddFiles(al, pkgNum, alp->fi);
    }

    assert(((rpmalNum)(alp - al->list)) == pkgNum);
}

void rpmalMakeIndex(rpmal al)
{
    availablePackage alp;
    int i;
    int providesCnt = 0;
    int fileCnt = 0;

    if (al == NULL || al->list == NULL) return;
    if (al->providesHash != NULL || al->fileHash != NULL)
	return;
    for (i = 0; i < al->size; i++) {
	alp = al->list + i;
	if (alp->provides != NULL)
	    providesCnt += rpmdsCount(alp->provides);
	if (alp->fi != NULL)
	    fileCnt += rpmfiFC(alp->fi);
    }

    al->providesHash = rpmalProvidesHashCreate(providesCnt/4+128, hashFunctionString,
					       strcmp, NULL, NULL);
    al->fileHash = rpmalFileHashCreate(fileCnt/4+128, fileHash, fileCompare,
				       NULL, NULL);

    for (i = 0; i < al->size; i++) {
	alp = al->list + i;
	rpmalAddProvides(al, i, alp->provides);
	rpmalAddFiles(al, i, alp->fi);
    }
}

rpmte *
rpmalAllFileSatisfiesDepend(const rpmal al, const rpmds ds)
{
    struct fileNameEntry_s fileNameEntry;
    const char * fileName;
    char * t;

    fileIndex result;
    int resultCnt;
    int i, found;
    availablePackage alp;
    rpmte * ret = NULL;
    /* FIX: ret is a problem */


    if (al == NULL || (fileName = rpmdsN(ds)) == NULL || *fileName != '/')
	return NULL;

    fileNameEntry.dirName = t = xstrdup(fileName);
    if ((t = strrchr(t, '/')) != NULL) {
	t++;		/* leave the trailing '/' */
	*t = '\0';
    }
    if ((fileNameEntry.baseName = strrchr(fileName, '/')) == NULL)
	goto exit;
    fileNameEntry.baseName++;

    rpmalFileHashGetEntry(al->fileHash, fileNameEntry, &result,
			  &resultCnt, NULL);

    if (resultCnt==0)
	goto exit;

    ret = xmalloc((resultCnt+1) * sizeof(*ret));

    for (found=i=0; i<resultCnt; i++) {
	alp = al->list + result[i].pkgNum;
	if (alp->p == NULL) // deleted
	    continue;

	rpmdsNotify(ds, _("(added files)"), 0);

	ret[found] = alp->p;
	found++;
    }
    ret[found] = NULL;

exit:
    fileNameEntry.dirName = _constfree(fileNameEntry.dirName);
    return ret;
}

rpmte *
rpmalAllSatisfiesDepend(const rpmal al, const rpmds ds)
{
    rpmte * ret = NULL;
    int i, found;
    const char * name;
    availableIndexEntry result;
    int resultCnt;

    availablePackage alp;
    int rc;

    if (al == NULL || ds == NULL || (name = rpmdsN(ds)) == NULL)
	return ret;

    if (*name == '/') {
	/* First, look for files "contained" in package ... */
	ret = rpmalAllFileSatisfiesDepend(al, ds);
	if (ret != NULL && *ret != NULL)
	    return ret;
	/* ... then, look for files "provided" by package. */
	ret = _free(ret);
    }

    rpmalProvidesHashGetEntry(al->providesHash, name, &result,
			      &resultCnt, NULL);

    if (resultCnt==0) return NULL;

    ret = xmalloc((resultCnt+1) * sizeof(*ret));

    for (found=i=0; i<resultCnt; i++) {
	alp = al->list + result[i].pkgNum;
	if (alp->p == NULL) // deleted
	    continue;
	(void) rpmdsSetIx(alp->provides, result[i].entryIx);
	rc = 0;
	if (rpmdsIx(alp->provides) >= 0)
	    rc = rpmdsCompare(alp->provides, ds);

	if (rc) {
	    rpmdsNotify(ds, _("(added provide)"), 0);
	    ret[found] = alp->p;
	    found++;
	}
    }
    ret[found] = NULL;

    return ret;
}

rpmte
rpmalSatisfiesDepend(const rpmal al, const rpmds ds)
{
    rpmte *providers = rpmalAllSatisfiesDepend(al, ds);
    rpmte best = NULL;

    if (providers) {
	if (al->tscolor) {
	    /*
	     * For colored dependencies, try to find a matching provider.
	     * Otherwise prefer provider of ts preferred color.
	     */
	    rpm_color_t dscolor = rpmdsColor(ds);
	    for (rpmte *p = providers; *p; p++) {
		rpm_color_t tecolor = rpmteColor(*p);
		if (dscolor) {
		    if (dscolor == tecolor) best = *p;
		} else if (al->prefcolor) {
		    if (al->prefcolor == tecolor) best = *p;
		}
		if (best) break;
	    }
	}
	/* if not decided by now, just pick first match */
	if (!best) best = providers[0];
	free(providers);
    }
    return best;
}