91f7c289f3edd4e5f4717fdb4d1d9b9bc491a729
[platform/upstream/libsolv.git] / examples / solv / repoinfo.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4
5 #include "pool.h"
6 #include "repo.h"
7 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA) || defined(MANDRIVA) || defined(MAGEIA))
8 #include "repo_rpmdb.h"
9 #endif
10 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
11 #include "repo_deb.h"
12 #endif
13 #ifdef SUSE
14 #include "repo_autopattern.h"
15 #endif
16
17
18 #include "repoinfo.h"
19 #include "repoinfo_cache.h"
20
21 #if defined(SUSE) || defined(FEDORA)
22 #include "repoinfo_config_yum.h"
23 #endif
24 #if defined(DEBIAN)
25 #include "repoinfo_config_debian.h"
26 #endif
27 #if defined(MANDRIVA) || defined(MAGEIA)
28 #include "repoinfo_config_urpmi.h"
29 #endif
30
31 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA) || defined(MANDRIVA) || defined(MAGEIA))
32 #include "repoinfo_system_rpm.h"
33 #endif
34 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
35 #include "repoinfo_system_debian.h"
36 #endif
37
38 #ifdef ENABLE_RPMMD
39 #include "repoinfo_type_rpmmd.h"
40 #endif
41 #ifdef ENABLE_SUSEREPO
42 #include "repoinfo_type_susetags.h"
43 #endif
44 #ifdef ENABLE_DEBIAN
45 #include "repoinfo_type_debian.h"
46 #endif
47 #ifdef ENABLE_MDKREPO
48 #include "repoinfo_type_mdk.h"
49 #endif
50
51 static int
52 repoinfos_sort_cmp(const void *ap, const void *bp)
53 {
54   const struct repoinfo *a = ap;
55   const struct repoinfo *b = bp;
56   return strcmp(a->alias, b->alias);
57 }
58
59 void
60 sort_repoinfos(struct repoinfo *repoinfos, int nrepoinfos)
61 {
62   qsort(repoinfos, nrepoinfos, sizeof(*repoinfos), repoinfos_sort_cmp);
63 }
64
65 void
66 free_repoinfos(struct repoinfo *repoinfos, int nrepoinfos)
67 {
68   int i, j;
69   for (i = 0; i < nrepoinfos; i++)
70     {
71       struct repoinfo *cinfo = repoinfos + i;
72       solv_free(cinfo->name);
73       solv_free(cinfo->alias);
74       solv_free(cinfo->path);
75       solv_free(cinfo->metalink);
76       solv_free(cinfo->mirrorlist);
77       solv_free(cinfo->baseurl);
78       for (j = 0; j < cinfo->ncomponents; j++)
79         solv_free(cinfo->components[j]);
80       solv_free(cinfo->components);
81     }
82   solv_free(repoinfos);
83 #if defined(SUSE) || defined(FEDORA)
84   yum_substitute((Pool *)0, 0);         /* free data */
85 #endif
86 }
87
88 struct repoinfo *
89 read_repoinfos(Pool *pool, int *nrepoinfosp)
90 {
91   struct repoinfo *repoinfos = 0;
92 #if defined(SUSE) || defined(FEDORA)
93   repoinfos = read_repoinfos_yum(pool, nrepoinfosp);
94 #endif
95 #if defined(MANDRIVA) || defined(MAGEIA)
96   repoinfos = read_repoinfos_urpmi(pool, nrepoinfosp);
97 #endif
98 #if defined(DEBIAN)
99   repoinfos = read_repoinfos_debian(pool, nrepoinfosp);
100 #endif
101   return repoinfos;
102 }
103
104 int
105 read_installed_repo(struct repoinfo *cinfo, Pool *pool)
106 {
107   int r = 1;
108   cinfo->type = TYPE_INSTALLED;
109   cinfo->repo = repo_create(pool, "@System");
110   cinfo->repo->appdata = cinfo;
111 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA) || defined(MANDRIVA) || defined(MAGEIA))
112   r = read_installed_rpm(cinfo);
113 #endif
114 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
115   r = read_installed_debian(cinfo);
116 #endif
117 #ifdef SUSE
118   repo_add_autopattern(cinfo->repo, 0);
119 #endif
120   pool_set_installed(pool, cinfo->repo);
121   return r;
122 }
123
124 int
125 is_cmdline_package(const char *filename)
126 {
127   int l = strlen(filename);
128 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA) || defined(MANDRIVA) || defined(MAGEIA))
129   if (l > 4 && !strcmp(filename + l - 4, ".rpm"))
130     return 1;
131 #endif
132 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
133   if (l > 4 && !strcmp(filename + l - 4, ".deb"))
134     return 1;
135 #endif
136   return 0;
137 }
138
139 Id
140 add_cmdline_package(Repo *repo, const char *filename)
141 {
142 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA) || defined(MANDRIVA) || defined(MAGEIA))
143   return repo_add_rpm(repo, filename, REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE);
144 #endif
145 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
146   return repo_add_deb(repo, filename, REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE);
147 #endif
148   return 0;
149 }
150
151 void
152 commit_transactionelement(Pool *pool, Id type, Id p, FILE *fp)
153 {
154 #if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA) || defined(MANDRIVA) || defined(MAGEIA))
155   commit_transactionelement_rpm(pool, type, p, fp);
156 #endif
157 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
158   commit_transactionelement_debian(pool, type, p, fp);
159 #endif
160 }
161
162 void
163 add_ext_keys(Repodata *data, Id handle, const char *ext)
164 {
165   static Id langtags[] = {
166     SOLVABLE_SUMMARY,     REPOKEY_TYPE_STR,
167     SOLVABLE_DESCRIPTION, REPOKEY_TYPE_STR,
168     SOLVABLE_EULA,        REPOKEY_TYPE_STR,
169     SOLVABLE_MESSAGEINS,  REPOKEY_TYPE_STR,
170     SOLVABLE_MESSAGEDEL,  REPOKEY_TYPE_STR,
171     SOLVABLE_CATEGORY,    REPOKEY_TYPE_ID,
172     0, 0
173   };
174   if (!strcmp(ext, "DL"))
175     {
176       repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOSITORY_DELTAINFO);
177       repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_FLEXARRAY);
178     }
179   else if (!strcmp(ext, "FL"))
180     {
181       repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_FILELIST);
182       repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRSTRARRAY);
183     }
184   else if (!strcmp(ext, "DU"))
185     {
186       repodata_add_idarray(data, handle, REPOSITORY_KEYS, SOLVABLE_DISKUSAGE);
187       repodata_add_idarray(data, handle, REPOSITORY_KEYS, REPOKEY_TYPE_DIRNUMNUMARRAY);
188     }
189   else
190     {
191       Pool *pool = data->repo->pool;
192       int i;
193       for (i = 0; langtags[i]; i += 2)
194         {
195           repodata_add_idarray(data, handle, REPOSITORY_KEYS, pool_id2langid(pool, langtags[i], ext, 1));
196           repodata_add_idarray(data, handle, REPOSITORY_KEYS, langtags[i + 1]);
197         }
198     }
199 }
200
201 int
202 load_stub(Pool *pool, Repodata *data, void *dp)
203 {
204   struct repoinfo *cinfo = data->repo->appdata;
205   switch (cinfo->type)
206     {
207 #ifdef ENABLE_SUSEREPO
208     case TYPE_SUSETAGS:
209       return susetags_load_ext(data->repo, data);
210 #endif
211 #ifdef ENABLE_RPMMD
212     case TYPE_RPMMD:
213       return repomd_load_ext(data->repo, data);
214 #endif
215 #ifdef ENABLE_MDKREPO
216     case TYPE_MDK:
217       return mdk_load_ext(data->repo, data);
218 #endif
219     default:
220       /* debian does not have any ext data yet */
221       return 0;
222     }
223 }
224
225 void
226 read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
227 {
228   Repo *repo;
229   int i;
230   Pool *sigpool = 0;
231
232   for (i = 0; i < nrepoinfos; i++)
233     {
234       struct repoinfo *cinfo = repoinfos + i;
235       if (!cinfo->enabled)
236         continue;
237
238       repo = repo_create(pool, cinfo->alias);
239       cinfo->repo = repo;
240       repo->appdata = cinfo;
241       repo->priority = 99 - cinfo->priority;
242
243       if ((!cinfo->autorefresh || cinfo->metadata_expire) && usecachedrepo(cinfo, 0, 0))
244         {
245 #ifdef SUSE
246           repo_add_autopattern(cinfo->repo, 0);
247 #endif
248           printf("repo '%s':", cinfo->alias);
249           printf(" cached\n");
250           continue;
251         }
252
253       switch (cinfo->type)
254         {
255 #ifdef ENABLE_RPMMD
256         case TYPE_RPMMD:
257           repomd_load(cinfo, &sigpool);
258           break;
259 #endif
260 #ifdef ENABLE_SUSEREPO
261         case TYPE_SUSETAGS:
262           susetags_load(cinfo, &sigpool);
263           break;
264 #endif
265 #ifdef ENABLE_DEBIAN
266         case TYPE_DEBIAN:
267           debian_load(cinfo, &sigpool);
268           break;
269 #endif
270 #ifdef ENABLE_MDKREPO
271         case TYPE_MDK:
272           mdk_load(cinfo, &sigpool);
273           break;
274 #endif
275         default:
276           printf("unsupported repo '%s': skipped\n", cinfo->alias);
277           repo_free(repo, 1);
278           cinfo->repo = 0;
279           break;
280         }
281 #ifdef SUSE
282       if (cinfo->repo)
283         repo_add_autopattern(cinfo->repo, 0);
284 #endif
285     }
286   if (sigpool)
287     pool_free(sigpool);
288 }
289