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"
26 #include "repo_autopattern.h"
28 #include "common_write.h"
29 #include "solv_xfopen.h"
34 fprintf(stderr, "\nUsage:\n"
35 "susetags2solv [-b <base>][-c <content>][-d <descrdir>][-h][-n <name>]\n"
36 " reads a 'susetags' repository from <stdin> and writes a .solv file to <stdout>\n"
37 " -b <base>: save as multiple files starting with <base>\n"
38 " -c <contentfile> : parse given contentfile (for product information)\n"
39 " -d <descrdir> : do not read from stdin, but use data in descrdir\n"
40 " -h : print help & exit\n"
41 " -n <name>: save attributes as <name>.attr\n"
46 /* content file query */
48 doquery(Pool *pool, Repo *repo, const char *arg)
54 snprintf(qbuf, sizeof(qbuf), "susetags:%s", arg);
55 id = pool_str2id(pool, qbuf, 0);
58 str = repo_lookup_str(repo, SOLVID_META, id);
64 main(int argc, char **argv)
66 const char *contentfile = 0;
67 const char *attrname = 0;
68 const char *descrdir = 0;
69 const char *basefile = 0;
70 const char *query = 0;
71 const char *mergefile = 0;
81 while ((c = getopt(argc, argv, "hn:c:d:b:q:M:X")) >= 0)
116 pool = pool_create();
117 repo = repo_create(pool, "<susetags>");
119 repo_add_repodata(repo, 0);
123 FILE *fp = fopen(contentfile, "r");
129 if (repo_add_content(repo, fp, REPO_REUSE_REPODATA))
131 fprintf(stderr, "susetags2solv: %s: %s\n", contentfile, pool_errstr(pool));
134 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
140 /* ensure '.attr' suffix */
141 const char *dot = strrchr(attrname, '.');
142 if (!dot || strcmp(dot, ".attr"))
144 int len = strlen (attrname);
145 char *newname = (char *)malloc(len + 6); /* alloc for <attrname>+'.attr'+'\0' */
146 strcpy (newname, attrname);
147 strcpy (newname+len, ".attr");
153 * descrdir path given, open files and read from there
160 struct dirent **files;
162 ndirs = scandir(descrdir, &files, 0, alphasort);
169 /* bring packages to front */
170 for (i = 0; i < ndirs; i++)
172 char *fn = files[i]->d_name;
173 if (!strcmp(fn, "packages") || !strcmp(fn, "packages.gz"))
178 fprintf(stderr, "found no packages file\n");
183 struct dirent *de = files[i];
184 memmove(files + 1, files, i * sizeof(de));
188 fnp = solv_malloc(strlen(descrdir) + 128);
189 for (i = 0; i < ndirs; i++)
191 char *fn = files[i]->d_name;
193 if (!strcmp(fn, "packages") || !strcmp(fn, "packages.gz"))
196 sprintf(fnp, "%s/%s", descrdir, fn);
197 fp = solv_xfopen(fnp, 0);
203 if (repo_add_susetags(repo, fp, defvendor, 0, flags | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE))
205 fprintf(stderr, "susetags2solv: %s: %s\n", fnp, pool_errstr(pool));
210 else if (!strcmp(fn, "packages.DU") || !strcmp(fn, "packages.DU.gz"))
213 sprintf(fnp, "%s/%s", descrdir, fn);
214 fp = solv_xfopen(fnp, 0);
220 if (repo_add_susetags(repo, fp, defvendor, 0, flags | SUSETAGS_EXTEND | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE))
222 fprintf(stderr, "susetags2solv: %s: %s\n", fnp, pool_errstr(pool));
227 else if (!strcmp(fn, "packages.FL") || !strcmp(fn, "packages.FL.gz"))
230 sprintf(fnp, "%s/%s", descrdir, fn);
231 FILE *fp = solv_xfopen(fnp, 0);
237 if (repo_add_susetags(repo, fp, defvendor, 0, flags | SUSETAGS_EXTEND | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE))
239 fprintf(stderr, "susetags2solv: %s: %s\n", fnp, pool_errstr(pool));
244 /* ignore for now. reactivate when filters work */
248 else if (!strncmp(fn, "packages.", 9))
253 sprintf(fnp, "%s/%s", descrdir, fn);
254 p = strrchr(fnp, '.');
255 if (p && !strcmp(p, ".gz"))
258 p = strrchr(fnp, '.');
260 if (!p || !p[1] || strlen(p + 1) > 5)
263 sprintf(fnp, "%s/%s", descrdir, fn);
264 fp = solv_xfopen(fnp, 0);
270 if (repo_add_susetags(repo, fp, defvendor, lang, flags | SUSETAGS_EXTEND | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE))
272 fprintf(stderr, "susetags2solv: %s: %s\n", fnp, pool_errstr(pool));
278 for (i = 0; i < ndirs; i++)
282 repo_internalize(repo);
286 /* read data from stdin */
287 if (repo_add_susetags(repo, stdin, defvendor, 0, REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE))
289 fprintf(stderr, "susetags2solv: %s\n", pool_errstr(pool));
293 repo_internalize(repo);
296 FILE *fp = fopen(mergefile, "r");
302 if (repo_add_solv(repo, fp, 0))
304 fprintf(stderr, "susetags2solv: %s\n", pool_errstr(pool));
311 repo_add_autopattern(repo, 0);
315 doquery(pool, repo, query);
317 tool_write(repo, basefile, attrname);