2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
17 #include "repo_write.h"
18 #include "common_write.h"
20 static Id verticals[] = {
31 static char *languagetags[] = {
33 "solvable:description:",
34 "solvable:messageins:",
35 "solvable:messagedel:",
40 static int test_separate = 0;
43 keyfilter_solv(Repo *data, Repokey *key, void *kfdata)
47 if (test_separate && key->storage != KEY_STORAGE_SOLVABLE)
48 return KEY_STORAGE_DROPPED;
49 for (i = 0; verticals[i]; i++)
50 if (key->name == verticals[i])
51 return KEY_STORAGE_VERTICAL_OFFSET;
52 keyname = id2str(data->pool, key->name);
53 for (i = 0; languagetags[i] != 0; i++)
54 if (!strncmp(keyname, languagetags[i], strlen(languagetags[i])))
55 return KEY_STORAGE_VERTICAL_OFFSET;
56 return KEY_STORAGE_INCORE;
60 keyfilter_attr(Repo *data, Repokey *key, void *kfdata)
64 if (key->storage == KEY_STORAGE_SOLVABLE)
65 return KEY_STORAGE_DROPPED;
66 for (i = 0; verticals[i]; i++)
67 if (key->name == verticals[i])
68 return KEY_STORAGE_VERTICAL_OFFSET;
69 keyname = id2str(data->pool, key->name);
70 for (i = 0; languagetags[i] != 0; i++)
71 if (!strncmp(keyname, languagetags[i], strlen(languagetags[i])))
72 return KEY_STORAGE_VERTICAL_OFFSET;
73 return KEY_STORAGE_INCORE;
77 keyfilter_language(Repo *repo, Repokey *key, void *kfdata)
80 char *lang = kfdata, *bname;
84 name = id2str(repo->pool, key->name);
85 p = strrchr(name, ':');
86 if (!p || strcmp(p + 1, lang) != 0)
87 return KEY_STORAGE_DROPPED;
88 /* find base name id */
91 id = str2id(repo->pool, bname, 1);
92 for (i = 0; verticals[i]; i++)
93 if (id == verticals[i])
94 return KEY_STORAGE_VERTICAL_OFFSET;
95 return KEY_STORAGE_INCORE;
99 keyfilter_DU(Repo *repo, Repokey *key, void *kfdata)
102 if (key->name != SOLVABLE_DISKUSAGE)
103 return KEY_STORAGE_DROPPED;
104 for (i = 0; verticals[i]; i++)
105 if (key->name == verticals[i])
106 return KEY_STORAGE_VERTICAL_OFFSET;
107 return KEY_STORAGE_INCORE;
111 keyfilter_FL(Repo *repo, Repokey *key, void *kfdata)
114 if (key->name != SOLVABLE_FILELIST)
115 return KEY_STORAGE_DROPPED;
116 for (i = 0; verticals[i]; i++)
117 if (key->name == verticals[i])
118 return KEY_STORAGE_VERTICAL_OFFSET;
119 return KEY_STORAGE_INCORE;
122 struct keyfilter_other_data {
128 keyfilter_other(Repo *repo, Repokey *key, void *kfdata)
130 const char *name, *p;
131 struct keyfilter_other_data *kd = kfdata;
134 if (key->name == SOLVABLE_FILELIST || key->name == SOLVABLE_DISKUSAGE)
135 return KEY_STORAGE_DROPPED;
137 name = id2str(repo->pool, key->name);
138 p = strrchr(name, ':');
141 for (i = 0; i < kd->nlanguages; i++)
142 if (!strcmp(p + 1, kd->languages[i]))
143 return KEY_STORAGE_DROPPED;
145 for (i = 0; verticals[i]; i++)
146 if (key->name == verticals[i])
147 return KEY_STORAGE_VERTICAL_OFFSET;
148 return KEY_STORAGE_INCORE;
152 * Write <repo> to stdout
153 * If <attrname> is given, write attributes to <attrname>
154 * If <basename> is given, split attributes
157 #define REPODATAFILE_BLOCK 15
160 tool_write(Repo *repo, const char *basename, const char *attrname)
164 Repodatafile *fileinfos = 0;
166 char **languages = 0;
170 fileinfos = sat_zextend(fileinfos, nfileinfos, 1, sizeof(Repodatafile), REPODATAFILE_BLOCK);
171 pool_addfileprovides_ids(repo->pool, 0, &fileinfos[nfileinfos].addedfileprovides);
172 for (i = 0; i < 32; i++)
173 if (repo->rpmdbcookie[i])
176 fileinfos[nfileinfos].rpmdbcookie = repo->rpmdbcookie;
177 if (fileinfos[nfileinfos].addedfileprovides || fileinfos[nfileinfos].rpmdbcookie)
182 struct keyfilter_other_data kd;
188 /* find languages and other info */
189 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
191 for (j = 1, key = data->keys + j; j < data->nkeys; j++, key++)
193 const char *keyname = id2str(repo->pool, key->name);
194 if (key->name == SOLVABLE_DISKUSAGE)
196 if (key->name == SOLVABLE_FILELIST)
198 for (k = 0; languagetags[k] != 0; k++)
199 if (!strncmp(keyname, languagetags[k], strlen(languagetags[k])))
201 if (!languagetags[k])
203 l = strlen(languagetags[k]);
204 if (strlen(keyname + l) > 5)
206 for (k = 0; k < nlanguages; k++)
207 if (!strcmp(languages[k], keyname + l))
211 languages = sat_realloc2(languages, nlanguages + 1, sizeof(char *));
212 languages[nlanguages++] = strdup(keyname + l);
215 fileinfos = sat_zextend(fileinfos, nfileinfos, nlanguages + 2, sizeof(Repodatafile), REPODATAFILE_BLOCK);
216 /* write language subfiles */
217 for (i = 0; i < nlanguages; i++)
219 sprintf(fn, "%s.%s.solv", basename, languages[i]);
220 if (!(fp = fopen(fn, "w")))
225 repo_write(repo, fp, keyfilter_language, languages[i], fileinfos + nfileinfos, 0);
226 fileinfos[nfileinfos].location = strdup(fn);
230 /* write DU subfile */
233 sprintf(fn, "%s.DU.solv", basename);
234 if (!(fp = fopen(fn, "w")))
239 repo_write(repo, fp, keyfilter_DU, 0, fileinfos + nfileinfos, 0);
240 fileinfos[nfileinfos].location = strdup(fn);
247 sprintf(fn, "%s.FL.solv", basename);
248 if (!(fp = fopen(fn, "w")))
253 repo_write(repo, fp, keyfilter_FL, 0, fileinfos + nfileinfos, 0);
254 fileinfos[nfileinfos].location = strdup(fn);
258 /* write everything else */
259 sprintf(fn, "%s.solv", basename);
260 if (!(fp = fopen(fn, "w")))
265 kd.languages = languages;
266 kd.nlanguages = nlanguages;
267 repo_write(repo, fp, keyfilter_other, &kd, nfileinfos ? fileinfos : 0, nfileinfos);
269 for (i = 0; i < nlanguages; i++)
272 for (i = 0; i < nfileinfos; i++)
274 sat_free(fileinfos[i].addedfileprovides);
275 sat_free(fileinfos[i].location);
276 sat_free(fileinfos[i].keys);
283 fileinfos = sat_zextend(fileinfos, nfileinfos, 1, sizeof(Repodatafile), REPODATAFILE_BLOCK);
285 FILE *fp = fopen (attrname, "w");
286 repo_write(repo, fp, keyfilter_attr, 0, fileinfos + nfileinfos, 0);
287 fileinfos[nfileinfos].location = strdup(attrname);
291 repo_write(repo, stdout, keyfilter_solv, 0, nfileinfos ? fileinfos : 0, nfileinfos);
292 for (i = 0; i < nfileinfos; i++)
294 sat_free(fileinfos[i].addedfileprovides);
295 sat_free(fileinfos[i].location);
296 sat_free(fileinfos[i].keys);