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