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 #define SATSOLVER_TOOLVERSION "1.0"
22 static Id verticals[] = {
33 static char *languagetags[] = {
35 "solvable:description:",
36 "solvable:messageins:",
37 "solvable:messagedel:",
42 static int test_separate = 0;
44 struct keyfilter_data {
47 int haveaddedfileprovides;
52 keyfilter_solv(Repo *data, Repokey *key, void *kfdata)
54 struct keyfilter_data *kd = kfdata;
58 if (test_separate && key->storage != KEY_STORAGE_SOLVABLE)
59 return KEY_STORAGE_DROPPED;
60 if (!kd->haveaddedfileprovides && key->name == REPOSITORY_ADDEDFILEPROVIDES)
61 return KEY_STORAGE_DROPPED;
62 if (!kd->haveexternal && key->name == REPOSITORY_EXTERNAL)
63 return KEY_STORAGE_DROPPED;
64 for (i = 0; verticals[i]; i++)
65 if (key->name == verticals[i])
66 return KEY_STORAGE_VERTICAL_OFFSET;
67 keyname = id2str(data->pool, key->name);
68 for (i = 0; languagetags[i] != 0; i++)
69 if (!strncmp(keyname, languagetags[i], strlen(languagetags[i])))
70 return KEY_STORAGE_VERTICAL_OFFSET;
71 return KEY_STORAGE_INCORE;
75 keyfilter_attr(Repo *data, Repokey *key, void *kfdata)
79 if (key->storage == KEY_STORAGE_SOLVABLE)
80 return KEY_STORAGE_DROPPED;
81 /* those must only be in the main solv file */
82 if (key->name == REPOSITORY_EXTERNAL || key->name == REPOSITORY_ADDEDFILEPROVIDES || key->name == REPOSITORY_TOOLVERSION)
83 return KEY_STORAGE_DROPPED;
84 for (i = 0; verticals[i]; i++)
85 if (key->name == verticals[i])
86 return KEY_STORAGE_VERTICAL_OFFSET;
87 keyname = id2str(data->pool, key->name);
88 for (i = 0; languagetags[i] != 0; i++)
89 if (!strncmp(keyname, languagetags[i], strlen(languagetags[i])))
90 return KEY_STORAGE_VERTICAL_OFFSET;
91 return KEY_STORAGE_INCORE;
95 keyfilter_language(Repo *repo, Repokey *key, void *kfdata)
97 Pool *pool = repo->pool;
102 name = id2str(repo->pool, key->name);
103 p = strrchr(name, ':');
104 if (!p || strcmp(p + 1, lang) != 0)
105 return KEY_STORAGE_DROPPED;
106 for (i = 0; verticals[i]; i++)
108 const char *vname = id2str(pool, verticals[i]);
109 if (!strncmp(name, vname, p - name) && vname[p - name] == 0)
110 return KEY_STORAGE_VERTICAL_OFFSET;
112 return KEY_STORAGE_INCORE;
116 keyfilter_DU(Repo *repo, Repokey *key, void *kfdata)
119 if (key->name != SOLVABLE_DISKUSAGE)
120 return KEY_STORAGE_DROPPED;
121 for (i = 0; verticals[i]; i++)
122 if (key->name == verticals[i])
123 return KEY_STORAGE_VERTICAL_OFFSET;
124 return KEY_STORAGE_INCORE;
128 keyfilter_FL(Repo *repo, Repokey *key, void *kfdata)
131 if (key->name != SOLVABLE_FILELIST)
132 return KEY_STORAGE_DROPPED;
133 for (i = 0; verticals[i]; i++)
134 if (key->name == verticals[i])
135 return KEY_STORAGE_VERTICAL_OFFSET;
136 return KEY_STORAGE_INCORE;
140 keyfilter_other(Repo *repo, Repokey *key, void *kfdata)
142 const char *name, *p;
143 struct keyfilter_data *kd = kfdata;
146 if (!kd->haveaddedfileprovides && key->name == REPOSITORY_ADDEDFILEPROVIDES)
147 return KEY_STORAGE_DROPPED;
148 if (!kd->haveexternal && key->name == REPOSITORY_EXTERNAL)
149 return KEY_STORAGE_DROPPED;
151 if (key->name == SOLVABLE_FILELIST || key->name == SOLVABLE_DISKUSAGE)
152 return KEY_STORAGE_DROPPED;
154 name = id2str(repo->pool, key->name);
155 p = strrchr(name, ':');
158 for (i = 0; i < kd->nlanguages; i++)
159 if (!strcmp(p + 1, kd->languages[i]))
160 return KEY_STORAGE_DROPPED;
162 for (i = 0; verticals[i]; i++)
163 if (key->name == verticals[i])
164 return KEY_STORAGE_VERTICAL_OFFSET;
165 return KEY_STORAGE_INCORE;
169 * Write <repo> to stdout
170 * If <attrname> is given, write attributes to <attrname>
171 * If <basename> is given, split attributes
174 #define REPODATAFILE_BLOCK 15
177 write_info(Repo *repo, FILE *fp, int (*keyfilter)(Repo *repo, Repokey *key, void *kfdata), void *kfdata, Repodata *info, const char *location)
182 repo_write(repo, fp, keyfilter, kfdata, &keyarray);
183 h = repodata_new_handle(info);
186 for (i = 0; keyarray[i]; i++)
187 repodata_add_idarray(info, h, REPOSITORY_KEYS, keyarray[i]);
190 repodata_set_str(info, h, REPOSITORY_LOCATION, location);
191 repodata_add_flexarray(info, SOLVID_META, REPOSITORY_EXTERNAL, h);
195 tool_write(Repo *repo, const char *basename, const char *attrname)
200 char **languages = 0;
203 Id *addedfileprovides = 0;
204 struct keyfilter_data kd;
206 memset(&kd, 0, sizeof(kd));
207 info = repo_add_repodata(repo, 0);
208 repodata_set_str(info, SOLVID_META, REPOSITORY_TOOLVERSION, SATSOLVER_TOOLVERSION);
209 pool_addfileprovides_ids(repo->pool, 0, &addedfileprovides);
210 if (addedfileprovides && *addedfileprovides)
212 kd.haveaddedfileprovides = 1;
213 for (i = 0; addedfileprovides[i]; i++)
214 repodata_add_idarray(info, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, addedfileprovides[i]);
216 sat_free(addedfileprovides);
218 pool_freeidhashes(repo->pool); /* free some mem */
227 /* find languages and other info */
228 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
230 for (j = 1, key = data->keys + j; j < data->nkeys; j++, key++)
232 const char *keyname = id2str(repo->pool, key->name);
233 if (key->name == SOLVABLE_DISKUSAGE)
235 if (key->name == SOLVABLE_FILELIST)
237 for (k = 0; languagetags[k] != 0; k++)
238 if (!strncmp(keyname, languagetags[k], strlen(languagetags[k])))
240 if (!languagetags[k])
242 l = strlen(languagetags[k]);
243 if (strlen(keyname + l) > 5)
245 for (k = 0; k < nlanguages; k++)
246 if (!strcmp(languages[k], keyname + l))
250 languages = sat_realloc2(languages, nlanguages + 1, sizeof(char *));
251 languages[nlanguages++] = strdup(keyname + l);
254 /* write language subfiles */
255 for (i = 0; i < nlanguages; i++)
257 sprintf(fn, "%s.%s.solv", basename, languages[i]);
258 if (!(fp = fopen(fn, "w")))
263 write_info(repo, fp, keyfilter_language, languages[i], info, fn);
267 /* write DU subfile */
270 sprintf(fn, "%s.DU.solv", basename);
271 if (!(fp = fopen(fn, "w")))
276 write_info(repo, fp, keyfilter_DU, 0, info, fn);
283 sprintf(fn, "%s.FL.solv", basename);
284 if (!(fp = fopen(fn, "w")))
289 write_info(repo, fp, keyfilter_FL, 0, info, fn);
293 /* write everything else */
294 sprintf(fn, "%s.solv", basename);
295 if (!(fp = fopen(fn, "w")))
300 kd.languages = languages;
301 kd.nlanguages = nlanguages;
302 repodata_internalize(info);
303 repo_write(repo, fp, keyfilter_other, &kd, 0);
305 for (i = 0; i < nlanguages; i++)
314 FILE *fp = fopen(attrname, "w");
315 write_info(repo, fp, keyfilter_attr, 0, info, attrname);
319 repodata_internalize(info);
320 repo_write(repo, stdout, keyfilter_solv, &kd, 0);