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>
19 #include "repo_solv.h"
20 #include "repo_susetags.h"
21 #include "repo_content.h"
23 #include "repo_autopattern.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;
78 while ((c = getopt(argc, argv, "hn:c:d:b:q:M:X")) >= 0)
113 pool = pool_create();
114 repo = repo_create(pool, "<susetags>");
116 repo_add_repodata(repo, 0);
120 FILE *fp = fopen(contentfile, "r");
126 if (repo_add_content(repo, fp, REPO_REUSE_REPODATA))
128 fprintf(stderr, "susetags2solv: %s: %s\n", contentfile, pool_errstr(pool));
131 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
137 /* ensure '.attr' suffix */
138 const char *dot = strrchr(attrname, '.');
139 if (!dot || strcmp(dot, ".attr"))
141 int len = strlen (attrname);
142 char *newname = (char *)malloc(len + 6); /* alloc for <attrname>+'.attr'+'\0' */
143 strcpy (newname, attrname);
144 strcpy (newname+len, ".attr");
150 * descrdir path given, open files and read from there
157 struct dirent **files;
159 ndirs = scandir(descrdir, &files, 0, alphasort);
166 /* bring packages to front */
167 for (i = 0; i < ndirs; i++)
169 char *fn = files[i]->d_name;
170 if (!strcmp(fn, "packages") || !strcmp(fn, "packages.gz"))
175 fprintf(stderr, "found no packages file\n");
180 struct dirent *de = files[i];
181 memmove(files + 1, files, i * sizeof(de));
185 fnp = solv_malloc(strlen(descrdir) + 128);
186 for (i = 0; i < ndirs; i++)
188 char *fn = files[i]->d_name;
190 if (!strcmp(fn, "packages") || !strcmp(fn, "packages.gz"))
193 sprintf(fnp, "%s/%s", descrdir, fn);
194 fp = solv_xfopen(fnp, 0);
200 if (repo_add_susetags(repo, fp, defvendor, 0, flags | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE | SUSETAGS_RECORD_SHARES))
202 fprintf(stderr, "susetags2solv: %s: %s\n", fnp, pool_errstr(pool));
207 else if (!strcmp(fn, "packages.DU") || !strcmp(fn, "packages.DU.gz"))
210 sprintf(fnp, "%s/%s", descrdir, fn);
211 fp = solv_xfopen(fnp, 0);
217 if (repo_add_susetags(repo, fp, defvendor, 0, flags | SUSETAGS_EXTEND | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE))
219 fprintf(stderr, "susetags2solv: %s: %s\n", fnp, pool_errstr(pool));
224 else if (!strcmp(fn, "packages.FL") || !strcmp(fn, "packages.FL.gz"))
227 sprintf(fnp, "%s/%s", descrdir, fn);
228 FILE *fp = solv_xfopen(fnp, 0);
234 if (repo_add_susetags(repo, fp, defvendor, 0, flags | SUSETAGS_EXTEND | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE))
236 fprintf(stderr, "susetags2solv: %s: %s\n", fnp, pool_errstr(pool));
241 /* ignore for now. reactivate when filters work */
245 else if (!strncmp(fn, "packages.", 9))
250 sprintf(fnp, "%s/%s", descrdir, fn);
251 p = strrchr(fnp, '.');
252 if (p && !strcmp(p, ".gz"))
255 p = strrchr(fnp, '.');
257 if (!p || !p[1] || strlen(p + 1) > 5)
260 sprintf(fnp, "%s/%s", descrdir, fn);
261 fp = solv_xfopen(fnp, 0);
267 if (repo_add_susetags(repo, fp, defvendor, lang, flags | SUSETAGS_EXTEND | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE))
269 fprintf(stderr, "susetags2solv: %s: %s\n", fnp, pool_errstr(pool));
275 for (i = 0; i < ndirs; i++)
279 repo_internalize(repo);
283 /* read data from stdin */
284 if (repo_add_susetags(repo, stdin, defvendor, 0, REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE))
286 fprintf(stderr, "susetags2solv: %s\n", pool_errstr(pool));
290 repo_internalize(repo);
293 FILE *fp = fopen(mergefile, "r");
299 if (repo_add_solv(repo, fp, 0))
301 fprintf(stderr, "susetags2solv: %s\n", pool_errstr(pool));
308 repo_add_autopattern(repo, 0);
312 doquery(pool, repo, query);
314 tool_write(repo, basefile, attrname);