summaryrefslogtreecommitdiff
path: root/meta/recipes-devtools/rpm/rpmresolve/rpmresolve.c
blob: 7f4caf9886ce1295fcc30c5af1a5a55de8609ed6 (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
/* OpenEmbedded RPM resolver utility

  Written by: Paul Eggleton <paul.eggleton@linux.intel.com>

  Copyright 2012 Intel Corporation

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License version 2 as
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

*/

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

#include <rpmdb.h>
#include <rpmtypes.h>
#include <rpmtag.h>
#include <rpmts.h>
#include <rpmmacro.h>
#include <rpmcb.h>
#include <rpmlog.h>
#include <argv.h>
#include <mire.h>

int debugmode;
FILE *outf;

int getPackageStr(rpmts ts, const char *NVRA, rpmTag tag, char **value)
{
    int rc = -1;
    rpmmi mi = rpmtsInitIterator(ts, RPMTAG_NVRA, NVRA, 0);
    Header h;
    if ((h = rpmmiNext(mi)) != NULL) {
        HE_t he = (HE_t) memset(alloca(sizeof(*he)), 0, sizeof(*he));
        he->tag = tag;
        rc = (headerGet(h, he, 0) != 1);
        if(rc==0)
            *value = strdup((char *)he->p.ptr);
    }
    (void)rpmmiFree(mi);
    return rc;
}

int loadTs(rpmts **ts, int *tsct, const char *dblistfn)
{
    int count = 0;
    int sz = 5;
    int rc = 0;
    int listfile = 1;
    struct stat st_buf;

    rc = stat(dblistfn, &st_buf);
    if(rc != 0) {
        perror("stat");
        return 1;
    }
    if(S_ISDIR(st_buf.st_mode))
        listfile = 0;

    if(listfile) {
        if(debugmode)
            printf("DEBUG: reading database list file '%s'\n", dblistfn);
        *ts = malloc(sz * sizeof(rpmts));
        FILE *f = fopen(dblistfn, "r" );
        if(f) {
            char line[2048];
            while(fgets(line, sizeof(line), f)) {
                int len = strlen(line) - 1;
                if(len > 0)
                    // Trim trailing whitespace
                    while(len > 0 && isspace(line[len]))
                        line[len--] = '\0';

                if(len > 0) {
                    // Expand array if needed
                    if(count == sz) {
                        sz += 5;
                        *ts = (rpmts *)realloc(*ts, sz);
                    }

                    if(debugmode)
                        printf("DEBUG: opening database '%s'\n", line);
                    char *dbpathm = malloc(strlen(line) + 10);
                    sprintf(dbpathm, "_dbpath %s", line);
                    rpmDefineMacro(NULL, dbpathm, RMIL_CMDLINE);
                    free(dbpathm);

                    rpmts tsi = rpmtsCreate();
                    (*ts)[count] = tsi;
                    rc = rpmtsOpenDB(tsi, O_RDONLY);
                    if( rc ) {
                        fprintf(stderr, "Failed to open database %s\n", line);
                        rc = -1;
                        break;
                    }

                    count++;
                }
            }
            fclose(f);
            *tsct = count;
        }
        else {
            perror(dblistfn);
            rc = -1;
        }
    }
    else {
        if(debugmode)
            printf("DEBUG: opening database '%s'\n", dblistfn);
        // Load from single database
        *ts = malloc(sizeof(rpmts));
        char *dbpathm = malloc(strlen(dblistfn) + 10);
        sprintf(dbpathm, "_dbpath %s", dblistfn);
        rpmDefineMacro(NULL, dbpathm, RMIL_CMDLINE);
        free(dbpathm);

        rpmts tsi = rpmtsCreate();
        (*ts)[0] = tsi;
        rc = rpmtsOpenDB(tsi, O_RDONLY);
        if( rc ) {
            fprintf(stderr, "Failed to open database %s\n", dblistfn);
            rc = -1;
        }
        *tsct = 1;
    }

    return rc;
}

int processPackages(rpmts *ts, int tscount, const char *packagelistfn, int ignoremissing)
{
    int rc = 0;
    int count = 0;
    int sz = 100;
    int i = 0;
    int missing = 0;

    FILE *f = fopen(packagelistfn, "r" );
    if(f) {
        char line[255];
        while(fgets(line, sizeof(line), f)) {
            int len = strlen(line) - 1;
            if(len > 0)
                // Trim trailing whitespace
                while(len > 0 && isspace(line[len]))
                    line[len--] = '\0';

            if(len > 0) {
                int found = 0;
                for(i=0; i<tscount; i++) {
                    ARGV_t keys = NULL;
                    rpmdb db = rpmtsGetRdb(ts[i]);
                    rc = rpmdbMireApply(db, RPMTAG_NAME,
                                RPMMIRE_STRCMP, line, &keys);
                    if (keys) {
                        int nkeys = argvCount(keys);
                        if( nkeys == 1 ) {
                            char *value = NULL;
                            rc = getPackageStr(ts[i], keys[0], RPMTAG_PACKAGEORIGIN, &value);
                            if(rc == 0)
                                fprintf(outf, "%s\n", value);
                            else
                                fprintf(stderr, "Failed to get package origin for %s\n", line);
                            found = 1;
                        }
                        else if( nkeys > 1 ) {
                            int keyindex = 0;
                            fprintf(stderr, "Multiple matches for %s:\n", line);
                            for( keyindex=0; keyindex<nkeys; keyindex++) {
                                char *value = NULL;
                                rc = getPackageStr(ts[i], keys[keyindex], RPMTAG_PACKAGEORIGIN, &value);
                                if(rc == 0)
                                    fprintf(stderr, "  %s\n", value);
                                else
                                    fprintf(stderr, "  (%s)\n", keys[keyindex]);
                            }
                        }
                    }
                    if(found)
                        break;
                }

                if( !found ) {
                    if( ignoremissing ) {
                        fprintf(stderr, "Unable to resolve package %s - ignoring\n", line);
                    }
                    else {
                        fprintf(stderr, "Unable to resolve package %s\n", line);
                        missing = 1;
                    }
                }
            }
            count++;
        }
        fclose(f);

        if( missing ) {
            fprintf(stderr, "ERROR: some packages were missing\n");
            rc = 1;
        }
    }
    else {
        perror(packagelistfn);
        rc = -1;
    }

    return rc;
}

int lookupProvider(rpmts ts, const char *req, char **provider)
{
    int rc = 0;
    rpmmi provmi = rpmtsInitIterator(ts, RPMTAG_PROVIDENAME, req, 0);
    if(provmi) {
        Header h;
        if ((h = rpmmiNext(provmi)) != NULL) {
            HE_t he = (HE_t) memset(alloca(sizeof(*he)), 0, sizeof(*he));
            he->tag = RPMTAG_NAME;
            rc = (headerGet(h, he, 0) != 1);
            if(rc==0)
                *provider = strdup((char *)he->p.ptr);
        }
        (void)rpmmiFree(provmi);
    }
    else {
        rc = -1;
    }
    return rc;
}

int printDepList(rpmts *ts, int tscount)
{
    int rc = 0;

    if( tscount > 1 )
        fprintf(stderr, ">1 database specified with dependency list, using first only\n");

    /* Get list of names */
    rpmdb db = rpmtsGetRdb(ts[0]);
    ARGV_t names = NULL;
    rc = rpmdbMireApply(db, RPMTAG_NAME,
                RPMMIRE_STRCMP, NULL, &names);
    int nnames = argvCount(names);

    /* Get list of NVRAs */
    ARGV_t keys = NULL;
    rc = rpmdbMireApply(db, RPMTAG_NVRA,
                RPMMIRE_STRCMP, NULL, &keys);
    if (keys) {
        int i, j;
        HE_t he = (HE_t) memset(alloca(sizeof(*he)), 0, sizeof(*he));
        int nkeys = argvCount(keys);
        for(i=0; i<nkeys; i++) {
            rpmmi mi = rpmtsInitIterator(ts[0], RPMTAG_NVRA, keys[i], 0);
            Header h;
            if ((h = rpmmiNext(mi)) != NULL) {
                /* Get name of package */
                he->tag = RPMTAG_NAME;
                rc = (headerGet(h, he, 0) != 1);
                char *name = strdup((char *)he->p.ptr);
                /* Get its requires */
                he->tag = RPMTAG_REQUIRENAME;
                if (rc = (headerGet(h, he, 0) != 1)) {
                    if (debugmode) {
                        printf("DEBUG: %s requires null\n", name);
                    }
                    rc = 0;
                    continue;
                }
                ARGV_t reqs = (ARGV_t)he->p.ptr;
                /* Get its requireflags */
                he->tag = RPMTAG_REQUIREFLAGS;
                rc = (headerGet(h, he, 0) != 1);
                rpmuint32_t *reqflags = (rpmuint32_t *)he->p.ui32p;
                for(j=0; j<he->c; j++) {
                    int k;
                    char *prov = NULL;
                    for(k=0; k<nnames; k++) {
                        if(strcmp(names[k], reqs[j]) == 0) {
                            prov = names[k];
                            break;
                        }
                    }
                    if(prov) {
                        if((int)reqflags[j] & 0x80000)
                            fprintf(outf, "%s|%s [REC]\n", name, prov);
                        else
                            fprintf(outf, "%s|%s\n", name, prov);
                    }
                    else {
                        rc = lookupProvider(ts[0], reqs[j], &prov);
                        if(rc==0 && prov) {
                            if((int)reqflags[j] & 0x80000)
                                fprintf(outf, "%s|%s [REC]\n", name, prov);
                            else
                                fprintf(outf, "%s|%s\n", name, prov);
                            free(prov);
                        }
                    }
                }
                free(name);
            }
            (void)rpmmiFree(mi);
        }
    }

    return rc;
}

void usage()
{
    fprintf(stderr, "OpenEmbedded rpm resolver utility\n");
    fprintf(stderr, "syntax: rpmresolve [-i] [-d] [-t] <dblistfile> <packagelistfile>\n");
}

int main(int argc, char **argv)
{
    rpmts *ts = NULL;
    int tscount = 0;
    int rc = 0;
    int i;
    int c;
    int ignoremissing = 0;
    int deplistmode = 0;
    char *outfile = NULL;

    debugmode = 0;
    outf = stdout;

    opterr = 0;
    while ((c = getopt (argc, argv, "itdo:")) != -1) {
        switch (c) {
            case 'i':
                ignoremissing = 1;
                break;
            case 't':
                deplistmode = 1;
                break;
            case 'd':
                debugmode = 1;
                break;
            case 'o':
                outfile = strdup(optarg);
                break;
            case '?':
                if(isprint(optopt))
                    fprintf(stderr, "Unknown option `-%c'.\n", optopt);
                else
                    fprintf(stderr, "Unknown option character `\\x%x'.\n",
                        optopt);
                usage();
                return 1;
            default:
                abort();
        }
    }

    if( argc - optind < 1 ) {
        usage();
        return 1;
    }

    if( outfile ) {
        if(debugmode)
            printf("DEBUG: Using output file %s\n", outfile);
        outf = fopen(outfile, "w");
    }

    const char *dblistfn = argv[optind];

    rpmcliInit(argc, argv, NULL);

    if(debugmode)
        rpmSetVerbosity(RPMLOG_DEBUG);

    rpmDefineMacro(NULL, "__dbi_txn create nofsync", RMIL_CMDLINE);

    rc = loadTs(&ts, &tscount, dblistfn);
    if( rc )
        return 1;
    if( tscount == 0 ) {
        fprintf(stderr, "Please specify database list file or database location\n");
        return 1;
    }

    if(deplistmode) {
        rc = printDepList(ts, tscount);
    }
    else {
        if( argc - optind < 2 ) {
            fprintf(stderr, "Please specify package list file\n");
        }
        else {
            const char *pkglistfn = argv[optind+1];
            rc = processPackages(ts, tscount, pkglistfn, ignoremissing);
        }
    }

    for(i=0; i<tscount; i++)
        (void) rpmtsCloseDB(ts[i]);
    free(ts);

    if( outfile ) {
        fclose(outf);
        free(outfile);
    }

    return rc;
}