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