- add '-d' option to susetags2solv, reads all packages files
[platform/upstream/libsolv.git] / tools / common_write.c
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 #include <sys/types.h>
9 #include <limits.h>
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include "pool.h"
16 #include "repo.h"
17 #include "repo_write.h"
18 #include "common_write.h"
19
20 static Id verticals[] = {
21   SOLVABLE_AUTHORS,
22   SOLVABLE_DESCRIPTION,
23   SOLVABLE_MESSAGEDEL,
24   SOLVABLE_MESSAGEINS,
25   SOLVABLE_EULA,
26   SOLVABLE_DISKUSAGE,
27   SOLVABLE_FILELIST,
28   0
29 };
30
31 static char *languagetags[] = {
32   "solvable:summary:",
33   "solvable:description:",
34   "solvable:messageins:",
35   "solvable:messagedel:",
36   0
37 };
38
39 static int test_separate = 0;
40
41 static int
42 keyfilter_solv(Repo *data, Repokey *key, void *kfdata)
43 {
44   int i;
45   if (test_separate && key->storage != KEY_STORAGE_SOLVABLE)
46     return KEY_STORAGE_DROPPED;
47   for (i = 0; verticals[i]; i++)
48     if (key->name == verticals[i])
49       return KEY_STORAGE_VERTICAL_OFFSET;
50   return KEY_STORAGE_INCORE;
51 }
52
53 static int
54 keyfilter_attr(Repo *data, Repokey *key, void *kfdata)
55 {
56   int i;
57   if (key->storage == KEY_STORAGE_SOLVABLE)
58     return KEY_STORAGE_DROPPED;
59   for (i = 0; verticals[i]; i++)
60     if (key->name == verticals[i])
61       return KEY_STORAGE_VERTICAL_OFFSET;
62   return KEY_STORAGE_INCORE;
63 }
64
65 static int
66 keyfilter_language(Repo *repo, Repokey *key, void *kfdata)
67 {
68   const char *name, *p;
69   char *lang = kfdata, *bname;
70   int i;
71   Id id;
72
73   name = id2str(repo->pool, key->name);
74   p = strrchr(name, ':');
75   if (!p || strcmp(p + 1, lang) != 0)
76     return KEY_STORAGE_DROPPED;
77   /* find base name id */
78   bname = strdup(name);
79   bname[p - name] = 0;
80   id = str2id(repo->pool, bname, 1);
81   for (i = 0; verticals[i]; i++)
82     if (id == verticals[i])
83       return KEY_STORAGE_VERTICAL_OFFSET;
84   return KEY_STORAGE_INCORE;
85 }
86
87 static int
88 keyfilter_DU(Repo *repo, Repokey *key, void *kfdata)
89 {
90   int i;
91   if (key->name != SOLVABLE_DISKUSAGE)
92     return KEY_STORAGE_DROPPED;
93   for (i = 0; verticals[i]; i++)
94     if (key->name == verticals[i])
95       return KEY_STORAGE_VERTICAL_OFFSET;
96   return KEY_STORAGE_INCORE;
97 }
98
99 static int
100 keyfilter_FL(Repo *repo, Repokey *key, void *kfdata)
101 {
102   int i;
103   if (key->name != SOLVABLE_FILELIST)
104     return KEY_STORAGE_DROPPED;
105   for (i = 0; verticals[i]; i++)
106     if (key->name == verticals[i])
107       return KEY_STORAGE_VERTICAL_OFFSET;
108   return KEY_STORAGE_INCORE;
109 }
110
111 struct keyfilter_other_data {
112   char **languages;
113   int nlanguages;
114 };
115
116 static int
117 keyfilter_other(Repo *repo, Repokey *key, void *kfdata)
118 {
119   const char *name, *p;
120   struct keyfilter_other_data *kd = kfdata;
121   int i;
122
123   if (key->name == SOLVABLE_FILELIST || key->name == SOLVABLE_DISKUSAGE)
124     return KEY_STORAGE_DROPPED;
125   name = id2str(repo->pool, key->name);
126   p = strrchr(name, ':');
127   if (p)
128     {
129       for (i = 0; i < kd->nlanguages; i++)
130         if (!strcmp(p + 1, kd->languages[i]))
131           return KEY_STORAGE_DROPPED;
132     }
133   for (i = 0; verticals[i]; i++)
134     if (key->name == verticals[i])
135       return KEY_STORAGE_VERTICAL_OFFSET;
136   return KEY_STORAGE_INCORE;
137 }
138
139 /*
140  * Write <repo> to stdout
141  * If <attrname> is given, write attributes to <attrname>
142  * If <basename> is given, split attributes
143  */
144
145 int
146 tool_write(Repo *repo, const char *basename, const char *attrname)
147 {
148   Repodata *data;
149   Repokey *key;
150   Repodatafile *fileinfos = 0;
151   int nfileinfos = 0;
152   char **languages = 0;
153   int nlanguages = 0;
154   int i, j, k, l;
155
156   if (basename)
157     {
158       struct keyfilter_other_data kd;
159       char fn[4096];
160       FILE *fp;
161       int has_DU = 0;
162       int has_FL = 0;
163
164       /* find languages and other info */
165       for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
166         {
167           for (j = 1, key = data->keys + j; j < data->nkeys; j++, key++)
168             {
169               const char *keyname = id2str(repo->pool, key->name);
170               if (key->name == SOLVABLE_DISKUSAGE)
171                 has_DU = 1;
172               if (key->name == SOLVABLE_FILELIST)
173                 has_FL = 1;
174               for (k = 0; languagetags[k] != 0; k++)
175                 if (!strncmp(keyname, languagetags[k], strlen(languagetags[k])))
176                   break;
177               if (!languagetags[k])
178                 continue;
179               l = strlen(languagetags[k]);
180               if (strlen(keyname + l) > 5)
181                 continue;
182               for (k = 0; k < nlanguages; k++)
183                 if (!strcmp(languages[k], keyname + l))
184                   break;
185               if (k < nlanguages)
186                 continue;
187               languages = sat_realloc2(languages, nlanguages + 1, sizeof(char *));
188               languages[nlanguages++] = strdup(keyname + l);
189             }
190         }
191       fileinfos = sat_calloc(nlanguages + 2, sizeof(Repodatafile));
192       /* write language subfiles */
193       for (i = 0; i < nlanguages; i++)
194         {
195           sprintf(fn, "%s.%s.solv", basename, languages[i]);
196           if (!(fp = fopen(fn, "w")))
197             {
198               perror(fn);
199               exit(1);
200             }
201           repo_write(repo, fp, keyfilter_language, languages[i], fileinfos + nfileinfos, 0);
202           fileinfos[nfileinfos++].location = strdup(fn);
203           fclose(fp);
204         }
205       /* write DU subfile */
206       if (has_DU)
207         {
208           sprintf(fn, "%s.DU.solv", basename);
209           if (!(fp = fopen(fn, "w")))
210             {
211               perror(fn);
212               exit(1);
213             }
214           repo_write(repo, fp, keyfilter_DU, 0, fileinfos + nfileinfos, 0);
215           fileinfos[nfileinfos++].location = strdup(fn);
216           fclose(fp);
217         }
218       /* write filelist */
219       if (has_FL)
220         {
221           sprintf(fn, "%s.FL.solv", basename);
222           if (!(fp = fopen(fn, "w")))
223             {
224               perror(fn);
225               exit(1);
226             }
227           repo_write(repo, fp, keyfilter_FL, 0, fileinfos + nfileinfos, 0);
228           fileinfos[nfileinfos++].location = strdup(fn);
229           fclose(fp);
230         }
231       /* write everything else */
232       sprintf(fn, "%s.solv", basename);
233       if (!(fp = fopen(fn, "w")))
234         {
235           perror(fn);
236           exit(1);
237         }
238       kd.languages = languages;
239       kd.nlanguages = nlanguages;
240       repo_write(repo, fp, keyfilter_other, &kd, fileinfos, nfileinfos);
241       fclose(fp);
242       for (i = 0; i < nlanguages; i++)
243         free(languages[i]);
244       sat_free(languages);
245       for (i = 0; i < nfileinfos; i++)
246         {
247           sat_free(fileinfos[i].location);
248           sat_free(fileinfos[i].keys);
249         }
250       sat_free(fileinfos);
251       exit(0);
252     }
253   if (attrname)
254     {
255       fileinfos = sat_calloc(1, sizeof(Repodatafile));
256       test_separate = 1;
257       FILE *fp = fopen (attrname, "w");
258       repo_write(repo, fp, keyfilter_attr, 0, fileinfos + nfileinfos++, 0);
259       fileinfos[nfileinfos - 1].location = strdup(attrname);
260       fclose(fp);
261     }
262   repo_write(repo, stdout, keyfilter_solv, 0, fileinfos, nfileinfos);
263   if (fileinfos)
264     {
265       free(fileinfos[0].location);
266       free(fileinfos[0].keys);
267       free(fileinfos);
268     }
269   return 0;
270 }