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_susetags.h"
23 #include "repo_content.h"
24 #include "common_write.h"
25 #include "solv_xfopen.h"
30 fprintf(stderr, "\nUsage:\n"
31 "susetags2solv [-b <base>][-c <content>][-d <descrdir>][-h][-n <name>]\n"
32 " reads a 'susetags' repository from <stdin> and writes a .solv file to <stdout>\n"
33 " -b <base>: save as multiple files starting with <base>\n"
34 " -c <contentfile> : parse given contentfile (for product information)\n"
35 " -d <descrdir> : do not read from stdin, but use data in descrdir\n"
36 " -h : print help & exit\n"
37 " -n <name>: save attributes as <name>.attr\n"
42 /* content file query */
44 doquery(Pool *pool, Repo *repo, const char *arg)
50 snprintf(qbuf, sizeof(qbuf), "susetags:%s", arg);
51 id = pool_str2id(pool, qbuf, 0);
54 str = repo_lookup_str(repo, SOLVID_META, id);
60 main(int argc, char **argv)
62 const char *contentfile = 0;
63 const char *attrname = 0;
64 const char *descrdir = 0;
65 const char *basefile = 0;
66 const char *query = 0;
73 while ((c = getopt(argc, argv, "hn:c:d:b:q:")) >= 0)
100 pool = pool_create();
101 repo = repo_create(pool, "<susetags>");
103 repo_add_repodata(repo, 0);
107 FILE *fp = fopen(contentfile, "r");
113 if (repo_add_content(repo, fp, REPO_REUSE_REPODATA))
115 fprintf(stderr, "susetags2solv: %s: %s\n", contentfile, pool_errstr(pool));
118 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
124 /* ensure '.attr' suffix */
125 const char *dot = strrchr(attrname, '.');
126 if (!dot || strcmp(dot, ".attr"))
128 int len = strlen (attrname);
129 char *newname = (char *)malloc(len + 6); /* alloc for <attrname>+'.attr'+'\0' */
130 strcpy (newname, attrname);
131 strcpy (newname+len, ".attr");
137 * descrdir path given, open files and read from there
144 struct dirent **files;
146 ndirs = scandir(descrdir, &files, 0, alphasort);
153 /* bring packages to front */
154 for (i = 0; i < ndirs; i++)
156 char *fn = files[i]->d_name;
157 if (!strcmp(fn, "packages") || !strcmp(fn, "packages.gz"))
162 fprintf(stderr, "found no packages file\n");
167 struct dirent *de = files[i];
168 memmove(files + 1, files, i * sizeof(de));
172 fnp = solv_malloc(strlen(descrdir) + 128);
173 for (i = 0; i < ndirs; i++)
175 char *fn = files[i]->d_name;
177 if (!strcmp(fn, "packages") || !strcmp(fn, "packages.gz"))
180 sprintf(fnp, "%s/%s", descrdir, fn);
181 fp = solv_xfopen(fnp, 0);
187 if (repo_add_susetags(repo, fp, defvendor, 0, flags | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE))
189 fprintf(stderr, "susetags2solv: %s: %s\n", fnp, pool_errstr(pool));
194 else if (!strcmp(fn, "packages.DU") || !strcmp(fn, "packages.DU.gz"))
197 sprintf(fnp, "%s/%s", descrdir, fn);
198 fp = solv_xfopen(fnp, 0);
204 if (repo_add_susetags(repo, fp, defvendor, 0, flags | SUSETAGS_EXTEND | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE))
206 fprintf(stderr, "susetags2solv: %s: %s\n", fnp, pool_errstr(pool));
211 else if (!strcmp(fn, "packages.FL") || !strcmp(fn, "packages.FL.gz"))
214 sprintf(fnp, "%s/%s", descrdir, fn);
215 FILE *fp = solv_xfopen(fnp, 0);
221 if (repo_add_susetags(repo, fp, defvendor, 0, flags | SUSETAGS_EXTEND | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE))
223 fprintf(stderr, "susetags2solv: %s: %s\n", fnp, pool_errstr(pool));
228 /* ignore for now. reactivate when filters work */
232 else if (!strncmp(fn, "packages.", 9))
237 sprintf(fnp, "%s/%s", descrdir, fn);
238 p = strrchr(fnp, '.');
239 if (p && !strcmp(p, ".gz"))
242 p = strrchr(fnp, '.');
244 if (!p || !p[1] || strlen(p + 1) > 5)
247 sprintf(fnp, "%s/%s", descrdir, fn);
248 fp = solv_xfopen(fnp, 0);
254 if (repo_add_susetags(repo, fp, defvendor, lang, flags | SUSETAGS_EXTEND | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE))
256 fprintf(stderr, "susetags2solv: %s: %s\n", fnp, pool_errstr(pool));
262 for (i = 0; i < ndirs; i++)
266 repo_internalize(repo);
270 /* read data from stdin */
271 if (repo_add_susetags(repo, stdin, defvendor, 0, REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE))
273 fprintf(stderr, "susetags2solv: %s\n", pool_errstr(pool));
277 repo_internalize(repo);
280 doquery(pool, repo, query);
282 tool_write(repo, basefile, attrname);