Imported Upstream version 0.6.13
[platform/upstream/libsolv.git] / examples / solv / repoinfo_system_rpm.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 #include <fcntl.h>
7 #include <sys/types.h>
8 #include <sys/wait.h>
9 #include <sys/stat.h>
10
11 #include "pool.h"
12 #include "repo.h"
13 #include "repo_rpmdb.h"
14 #if defined(ENABLE_SUSEREPO) && defined(SUSE)
15 #include "repo_products.h"
16 #endif
17 #if defined(ENABLE_APPDATA)
18 #include "repo_appdata.h"
19 #endif
20 #include "transaction.h"
21
22 #include "repoinfo.h"
23 #include "repoinfo_cache.h"
24 #include "repoinfo_system_rpm.h"
25
26 #ifdef SUSE
27 # define PRODUCTS_PATH "/etc/products.d"
28 #endif
29 #ifdef ENABLE_APPDATA
30 # define APPDATA_PATH "/usr/share/appdata"
31 #endif
32
33 static void
34 runrpm(const char *arg, const char *name, int dupfd3, const char *rootdir)
35 {
36   pid_t pid;
37   int status;
38
39   if ((pid = fork()) == (pid_t)-1)
40     {
41       perror("fork");
42       exit(1);
43     }
44   if (pid == 0)
45     {
46       if (!rootdir)
47         rootdir = "/";
48       if (dupfd3 != -1 && dupfd3 != 3)
49         {
50           dup2(dupfd3, 3);
51           close(dupfd3);
52         }
53       if (dupfd3 != -1)
54         fcntl(3, F_SETFD, 0);   /* clear CLOEXEC */
55       if (strcmp(arg, "-e") == 0)
56         execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", "--root", rootdir, name, (char *)0);
57       else
58         execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", "--root", rootdir, name, (char *)0);
59       perror("rpm");
60       _exit(0);
61     }
62   while (waitpid(pid, &status, 0) != pid)
63     ;
64   if (status)
65     {
66       printf("rpm failed\n");
67       exit(1);
68     }
69 }
70
71 int
72 read_installed_rpm(struct repoinfo *cinfo)
73 {
74   Repo *repo = cinfo->repo;
75   Pool *pool = repo->pool;
76   FILE *ofp = 0;
77   struct stat stb;
78
79   memset(&stb, 0, sizeof(stb));
80   printf("rpm database:");
81   if (stat(pool_prepend_rootdir_tmp(pool, "/var/lib/rpm/Packages"), &stb))
82     memset(&stb, 0, sizeof(stb));
83   calc_cookie_stat(&stb, REPOKEY_TYPE_SHA256, 0, cinfo->cookie);
84   cinfo->cookieset = 1;
85   if (usecachedrepo(cinfo, 0, 0))
86     {
87       printf(" cached\n");
88       return 1;
89     }
90   printf(" reading\n");
91 #if defined(ENABLE_SUSEREPO) && defined(PRODUCTS_PATH)
92   if (repo_add_products(repo, PRODUCTS_PATH, REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE | REPO_USE_ROOTDIR))
93     {
94       fprintf(stderr, "product reading failed: %s\n", pool_errstr(pool));
95       return 0;
96     }
97 #endif
98 #if defined(ENABLE_APPDATA) && defined(APPDATA_PATH)
99   if (repo_add_appdata_dir(repo, APPDATA_PATH, REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE | REPO_USE_ROOTDIR))
100     {
101       fprintf(stderr, "appdata reading failed: %s\n", pool_errstr(pool));
102       return 0;
103     }
104 #endif
105   ofp = fopen(calc_cachepath(repo, 0, 0), "r");
106   if (repo_add_rpmdb_reffp(repo, ofp, REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE | REPO_USE_ROOTDIR))
107     {
108       fprintf(stderr, "installed db: %s\n", pool_errstr(pool));
109       return 0;
110     }
111   if (ofp)
112     fclose(ofp);
113   repo_internalize(repo);
114   writecachedrepo(cinfo, 0, 0);
115   return 1;
116 }
117
118 void
119 commit_transactionelement_rpm(Pool *pool, Id type, Id p, FILE *fp)
120 {
121   Solvable *s = pool_id2solvable(pool, p);
122   const char *rootdir = pool_get_rootdir(pool);
123   const char *evr, *evrp, *nvra;
124
125   switch(type)
126     {
127     case SOLVER_TRANSACTION_ERASE:
128       if (!s->repo->rpmdbid || !s->repo->rpmdbid[p - s->repo->start])
129         break;
130       /* strip epoch from evr */
131       evr = evrp = pool_id2str(pool, s->evr);
132       while (*evrp >= '0' && *evrp <= '9')
133         evrp++;
134       if (evrp > evr && evrp[0] == ':' && evrp[1])
135         evr = evrp + 1;
136       nvra = pool_tmpjoin(pool, pool_id2str(pool, s->name), "-", evr);
137       nvra = pool_tmpappend(pool, nvra, ".", pool_id2str(pool, s->arch));
138       runrpm("-e", nvra, -1, rootdir);      /* too bad that --querybynumber doesn't work */
139       break;
140     case SOLVER_TRANSACTION_INSTALL:
141     case SOLVER_TRANSACTION_MULTIINSTALL:
142       rewind(fp);
143       lseek(fileno(fp), 0, SEEK_SET);
144       runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp), rootdir);
145       break;
146     default:
147       break;
148     }
149 }
150
151 #endif