2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
15 #include "repo_write.h"
16 #include "common_write.h"
17 #include "solvversion.h"
19 /* toolversion history
20 * 1.0: initial tool version
21 * 1.1: changed PRODUCT_ENDOFLIFE parsing
24 static Id verticals[] = {
32 SOLVABLE_CHANGELOG_AUTHOR,
33 SOLVABLE_CHANGELOG_TEXT,
37 static char *languagetags[] = {
39 "solvable:description:",
40 "solvable:messageins:",
41 "solvable:messagedel:",
46 static int test_separate = 0;
48 struct keyfilter_data {
51 int haveaddedfileprovides;
56 keyfilter_solv(Repo *data, Repokey *key, void *kfdata)
58 struct keyfilter_data *kd = kfdata;
62 if (test_separate && key->storage != KEY_STORAGE_SOLVABLE)
63 return KEY_STORAGE_DROPPED;
64 if (!kd->haveaddedfileprovides && key->name == REPOSITORY_ADDEDFILEPROVIDES)
65 return KEY_STORAGE_DROPPED;
66 if (!kd->haveexternal && key->name == REPOSITORY_EXTERNAL)
67 return KEY_STORAGE_DROPPED;
68 for (i = 0; verticals[i]; i++)
69 if (key->name == verticals[i])
70 return KEY_STORAGE_VERTICAL_OFFSET;
71 keyname = pool_id2str(data->pool, key->name);
72 for (i = 0; languagetags[i] != 0; i++)
73 if (!strncmp(keyname, languagetags[i], strlen(languagetags[i])))
74 return KEY_STORAGE_VERTICAL_OFFSET;
75 return KEY_STORAGE_INCORE;
79 keyfilter_attr(Repo *data, Repokey *key, void *kfdata)
83 if (key->storage == KEY_STORAGE_SOLVABLE)
84 return KEY_STORAGE_DROPPED;
85 /* those must only be in the main solv file */
86 if (key->name == REPOSITORY_EXTERNAL || key->name == REPOSITORY_ADDEDFILEPROVIDES || key->name == REPOSITORY_TOOLVERSION)
87 return KEY_STORAGE_DROPPED;
88 for (i = 0; verticals[i]; i++)
89 if (key->name == verticals[i])
90 return KEY_STORAGE_VERTICAL_OFFSET;
91 keyname = pool_id2str(data->pool, key->name);
92 for (i = 0; languagetags[i] != 0; i++)
93 if (!strncmp(keyname, languagetags[i], strlen(languagetags[i])))
94 return KEY_STORAGE_VERTICAL_OFFSET;
95 return KEY_STORAGE_INCORE;
99 keyfilter_language(Repo *repo, Repokey *key, void *kfdata)
101 Pool *pool = repo->pool;
102 const char *name, *p;
106 name = pool_id2str(repo->pool, key->name);
107 p = strrchr(name, ':');
108 if (!p || strcmp(p + 1, lang) != 0)
109 return KEY_STORAGE_DROPPED;
110 for (i = 0; verticals[i]; i++)
112 const char *vname = pool_id2str(pool, verticals[i]);
113 if (!strncmp(name, vname, p - name) && vname[p - name] == 0)
114 return KEY_STORAGE_VERTICAL_OFFSET;
116 return KEY_STORAGE_INCORE;
120 keyfilter_DU(Repo *repo, Repokey *key, void *kfdata)
123 if (key->name != SOLVABLE_DISKUSAGE)
124 return KEY_STORAGE_DROPPED;
125 for (i = 0; verticals[i]; i++)
126 if (key->name == verticals[i])
127 return KEY_STORAGE_VERTICAL_OFFSET;
128 return KEY_STORAGE_INCORE;
132 keyfilter_FL(Repo *repo, Repokey *key, void *kfdata)
135 if (key->name != SOLVABLE_FILELIST)
136 return KEY_STORAGE_DROPPED;
137 for (i = 0; verticals[i]; i++)
138 if (key->name == verticals[i])
139 return KEY_STORAGE_VERTICAL_OFFSET;
140 return KEY_STORAGE_INCORE;
144 keyfilter_other(Repo *repo, Repokey *key, void *kfdata)
146 const char *name, *p;
147 struct keyfilter_data *kd = kfdata;
150 if (!kd->haveaddedfileprovides && key->name == REPOSITORY_ADDEDFILEPROVIDES)
151 return KEY_STORAGE_DROPPED;
152 if (!kd->haveexternal && key->name == REPOSITORY_EXTERNAL)
153 return KEY_STORAGE_DROPPED;
155 if (key->name == SOLVABLE_FILELIST || key->name == SOLVABLE_DISKUSAGE)
156 return KEY_STORAGE_DROPPED;
158 name = pool_id2str(repo->pool, key->name);
159 p = strrchr(name, ':');
162 for (i = 0; i < kd->nlanguages; i++)
163 if (!strcmp(p + 1, kd->languages[i]))
164 return KEY_STORAGE_DROPPED;
166 for (i = 0; verticals[i]; i++)
167 if (key->name == verticals[i])
168 return KEY_STORAGE_VERTICAL_OFFSET;
169 return KEY_STORAGE_INCORE;
173 * Write <repo> to stdout
174 * If <attrname> is given, write attributes to <attrname>
175 * If <basename> is given, split attributes
178 #define REPODATAFILE_BLOCK 15
181 write_info(Repo *repo, FILE *fp, int (*keyfilter)(Repo *repo, Repokey *key, void *kfdata), void *kfdata, Repodata *info, const char *location)
187 if (repo_write_filtered(repo, fp, keyfilter, kfdata, &keyq) != 0)
189 fprintf(stderr, "repo_write failed\n");
192 h = repodata_new_handle(info);
194 repodata_set_idarray(info, h, REPOSITORY_KEYS, &keyq);
196 repodata_set_str(info, h, REPOSITORY_LOCATION, location);
197 repodata_add_flexarray(info, SOLVID_META, REPOSITORY_EXTERNAL, h);
201 tool_write(Repo *repo, const char *basename, const char *attrname)
206 char **languages = 0;
209 struct keyfilter_data kd;
210 Queue addedfileprovides;
212 memset(&kd, 0, sizeof(kd));
213 info = repo_add_repodata(repo, 0);
214 repodata_set_str(info, SOLVID_META, REPOSITORY_TOOLVERSION, LIBSOLV_TOOLVERSION);
215 queue_init(&addedfileprovides);
216 pool_addfileprovides_queue(repo->pool, &addedfileprovides, 0);
217 if (addedfileprovides.count)
219 kd.haveaddedfileprovides = 1;
220 repodata_set_idarray(info, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, &addedfileprovides);
222 queue_free(&addedfileprovides);
224 pool_freeidhashes(repo->pool); /* free some mem */
233 /* find languages and other info */
234 FOR_REPODATAS(repo, i, data)
236 for (j = 1, key = data->keys + j; j < data->nkeys; j++, key++)
238 const char *keyname = pool_id2str(repo->pool, key->name);
239 if (key->name == SOLVABLE_DISKUSAGE)
241 if (key->name == SOLVABLE_FILELIST)
243 for (k = 0; languagetags[k] != 0; k++)
244 if (!strncmp(keyname, languagetags[k], strlen(languagetags[k])))
246 if (!languagetags[k])
248 l = strlen(languagetags[k]);
249 if (strlen(keyname + l) > 5)
251 for (k = 0; k < nlanguages; k++)
252 if (!strcmp(languages[k], keyname + l))
256 languages = solv_realloc2(languages, nlanguages + 1, sizeof(char *));
257 languages[nlanguages++] = strdup(keyname + l);
260 /* write language subfiles */
261 for (i = 0; i < nlanguages; i++)
263 sprintf(fn, "%s.%s.solv", basename, languages[i]);
264 if (!(fp = fopen(fn, "w")))
269 write_info(repo, fp, keyfilter_language, languages[i], info, fn);
273 /* write DU subfile */
276 sprintf(fn, "%s.DU.solv", basename);
277 if (!(fp = fopen(fn, "w")))
282 write_info(repo, fp, keyfilter_DU, 0, info, fn);
289 sprintf(fn, "%s.FL.solv", basename);
290 if (!(fp = fopen(fn, "w")))
295 write_info(repo, fp, keyfilter_FL, 0, info, fn);
299 /* write everything else */
300 sprintf(fn, "%s.solv", basename);
301 if (!(fp = fopen(fn, "w")))
306 kd.languages = languages;
307 kd.nlanguages = nlanguages;
308 repodata_internalize(info);
309 if (repo_write_filtered(repo, fp, keyfilter_other, &kd, 0) != 0)
311 fprintf(stderr, "repo_write failed\n");
319 for (i = 0; i < nlanguages; i++)
321 solv_free(languages);
328 fp = fopen(attrname, "w");
329 write_info(repo, fp, keyfilter_attr, 0, info, attrname);
333 repodata_internalize(info);
334 if (repo_write_filtered(repo, stdout, keyfilter_solv, &kd, 0) != 0)
336 fprintf(stderr, "repo_write failed\n");