2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
10 #include <sys/types.h>
22 #include "repo_solv.h"
23 #include "repo_susetags.h"
24 #include "repo_content.h"
25 #include "common_write.h"
26 #include "solv_xfopen.h"
31 fprintf(stderr, "\nUsage:\n"
32 "susetags2solv [-b <base>][-c <content>][-d <descrdir>][-h][-n <name>]\n"
33 " reads a 'susetags' repository from <stdin> and writes a .solv file to <stdout>\n"
34 " -b <base>: save as multiple files starting with <base>\n"
35 " -c <contentfile> : parse given contentfile (for product information)\n"
36 " -d <descrdir> : do not read from stdin, but use data in descrdir\n"
37 " -h : print help & exit\n"
38 " -n <name>: save attributes as <name>.attr\n"
43 /* content file query */
45 doquery(Pool *pool, Repo *repo, const char *arg)
51 snprintf(qbuf, sizeof(qbuf), "susetags:%s", arg);
52 id = pool_str2id(pool, qbuf, 0);
55 str = repo_lookup_str(repo, SOLVID_META, id);
61 main(int argc, char **argv)
63 const char *contentfile = 0;
64 const char *attrname = 0;
65 const char *descrdir = 0;
66 const char *basefile = 0;
67 const char *query = 0;
68 const char *mergefile = 0;
75 while ((c = getopt(argc, argv, "hn:c:d:b:q:M:")) >= 0)
105 pool = pool_create();
106 repo = repo_create(pool, "<susetags>");
108 repo_add_repodata(repo, 0);
112 FILE *fp = fopen(contentfile, "r");
118 if (repo_add_content(repo, fp, REPO_REUSE_REPODATA))
120 fprintf(stderr, "susetags2solv: %s: %s\n", contentfile, pool_errstr(pool));
123 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
129 /* ensure '.attr' suffix */
130 const char *dot = strrchr(attrname, '.');
131 if (!dot || strcmp(dot, ".attr"))
133 int len = strlen (attrname);
134 char *newname = (char *)malloc(len + 6); /* alloc for <attrname>+'.attr'+'\0' */
135 strcpy (newname, attrname);
136 strcpy (newname+len, ".attr");
142 * descrdir path given, open files and read from there
149 struct dirent **files;
151 ndirs = scandir(descrdir, &files, 0, alphasort);
158 /* bring packages to front */
159 for (i = 0; i < ndirs; i++)
161 char *fn = files[i]->d_name;
162 if (!strcmp(fn, "packages") || !strcmp(fn, "packages.gz"))
167 fprintf(stderr, "found no packages file\n");
172 struct dirent *de = files[i];
173 memmove(files + 1, files, i * sizeof(de));
177 fnp = solv_malloc(strlen(descrdir) + 128);
178 for (i = 0; i < ndirs; i++)
180 char *fn = files[i]->d_name;
182 if (!strcmp(fn, "packages") || !strcmp(fn, "packages.gz"))
185 sprintf(fnp, "%s/%s", descrdir, fn);
186 fp = solv_xfopen(fnp, 0);
192 if (repo_add_susetags(repo, fp, defvendor, 0, flags | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE))
194 fprintf(stderr, "susetags2solv: %s: %s\n", fnp, pool_errstr(pool));
199 else if (!strcmp(fn, "packages.DU") || !strcmp(fn, "packages.DU.gz"))
202 sprintf(fnp, "%s/%s", descrdir, fn);
203 fp = solv_xfopen(fnp, 0);
209 if (repo_add_susetags(repo, fp, defvendor, 0, flags | SUSETAGS_EXTEND | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE))
211 fprintf(stderr, "susetags2solv: %s: %s\n", fnp, pool_errstr(pool));
216 else if (!strcmp(fn, "packages.FL") || !strcmp(fn, "packages.FL.gz"))
219 sprintf(fnp, "%s/%s", descrdir, fn);
220 FILE *fp = solv_xfopen(fnp, 0);
226 if (repo_add_susetags(repo, fp, defvendor, 0, flags | SUSETAGS_EXTEND | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE))
228 fprintf(stderr, "susetags2solv: %s: %s\n", fnp, pool_errstr(pool));
233 /* ignore for now. reactivate when filters work */
237 else if (!strncmp(fn, "packages.", 9))
242 sprintf(fnp, "%s/%s", descrdir, fn);
243 p = strrchr(fnp, '.');
244 if (p && !strcmp(p, ".gz"))
247 p = strrchr(fnp, '.');
249 if (!p || !p[1] || strlen(p + 1) > 5)
252 sprintf(fnp, "%s/%s", descrdir, fn);
253 fp = solv_xfopen(fnp, 0);
259 if (repo_add_susetags(repo, fp, defvendor, lang, flags | SUSETAGS_EXTEND | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE))
261 fprintf(stderr, "susetags2solv: %s: %s\n", fnp, pool_errstr(pool));
267 for (i = 0; i < ndirs; i++)
271 repo_internalize(repo);
275 /* read data from stdin */
276 if (repo_add_susetags(repo, stdin, defvendor, 0, REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE))
278 fprintf(stderr, "susetags2solv: %s\n", pool_errstr(pool));
282 repo_internalize(repo);
285 FILE *fp = fopen(mergefile, "r");
291 if (repo_add_solv(repo, fp, 0))
293 fprintf(stderr, "susetags2solv: %s\n", pool_errstr(pool));
300 doquery(pool, repo, query);
302 tool_write(repo, basefile, attrname);