Imported Upstream version 0.6.32
[platform/upstream/libsolv.git] / examples / solv / fileconflicts.c
1 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA) || defined(MANDRIVA) || defined(MAGEIA))
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6
7 #include "pool.h"
8 #include "repo.h"
9 #include "repo_rpmdb.h"
10 #include "pool_fileconflicts.h"
11
12 #include "fileconflicts.h"
13
14 struct fcstate {
15   FILE **newpkgsfps;
16   Queue *checkq;
17   int newpkgscnt;
18   void *rpmstate;
19 };
20
21 static void *
22 fileconflict_cb(Pool *pool, Id p, void *cbdata)
23 {
24   struct fcstate *fcstate = cbdata;
25   Solvable *s;
26   Id rpmdbid;
27   int i;
28   FILE *fp; 
29
30   s = pool_id2solvable(pool, p);
31   if (pool->installed && s->repo == pool->installed)
32     {    
33       if (!s->repo->rpmdbid)
34         return 0;
35       rpmdbid = s->repo->rpmdbid[p - s->repo->start];
36       if (!rpmdbid)
37         return 0;
38       return rpm_byrpmdbid(fcstate->rpmstate, rpmdbid);
39     }    
40   for (i = 0; i < fcstate->newpkgscnt; i++) 
41     if (fcstate->checkq->elements[i] == p)
42       break;
43   if (i == fcstate->newpkgscnt)
44     return 0;
45   fp = fcstate->newpkgsfps[i];
46   if (!fp)
47     return 0;
48   rewind(fp);
49   return rpm_byfp(fcstate->rpmstate, fp, pool_solvable2str(pool, s)); 
50 }
51
52 int
53 checkfileconflicts(Pool *pool, Queue *checkq, int newpkgs, FILE **newpkgsfps, Queue *conflicts)
54 {
55   struct fcstate fcstate;
56   int i;
57
58   printf("Searching for file conflicts\n");
59   queue_init(conflicts);
60   fcstate.rpmstate = rpm_state_create(pool, pool_get_rootdir(pool));
61   fcstate.newpkgscnt = newpkgs;
62   fcstate.checkq = checkq;
63   fcstate.newpkgsfps = newpkgsfps;
64   pool_findfileconflicts(pool, checkq, newpkgs, conflicts, FINDFILECONFLICTS_USE_SOLVABLEFILELIST | FINDFILECONFLICTS_CHECK_DIRALIASING | FINDFILECONFLICTS_USE_ROOTDIR, &fileconflict_cb, &fcstate);
65   fcstate.rpmstate = rpm_state_free(fcstate.rpmstate);
66   if (conflicts->count)
67     {
68       printf("\n");
69       for (i = 0; i < conflicts->count; i += 6)
70         {
71           if (conflicts->elements[i] == conflicts->elements[i + 3])
72             printf("file %s of package %s conflicts with package %s\n", pool_id2str(pool, conflicts->elements[i]), pool_solvid2str(pool, conflicts->elements[i + 1]), pool_solvid2str(pool, conflicts->elements[i + 4]));
73           else
74             printf("file %s of package %s conflicts with file %s of package %s\n", pool_id2str(pool, conflicts->elements[i]), pool_solvid2str(pool, conflicts->elements[i + 1]), pool_id2str(pool, conflicts->elements[i + 3]), pool_solvid2str(pool, conflicts->elements[i + 4]));
75         }
76       printf("\n");
77     }
78   return conflicts->count;
79 }
80
81 #endif