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 "sat_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 = 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;
71 while ((c = getopt(argc, argv, "hn:c:d:b:q:")) >= 0)
98 Pool *pool = pool_create();
99 Repo *repo = repo_create(pool, "<susetags>");
101 repo_add_repodata(repo, 0);
105 FILE *fp = fopen(contentfile, "r");
111 repo_add_content(repo, fp, REPO_REUSE_REPODATA);
112 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
118 /* ensure '.attr' suffix */
119 const char *dot = strrchr(attrname, '.');
120 if (!dot || strcmp(dot, ".attr"))
122 int len = strlen (attrname);
123 char *newname = (char *)malloc(len + 6); /* alloc for <attrname>+'.attr'+'\0' */
124 strcpy (newname, attrname);
125 strcpy (newname+len, ".attr");
131 * descrdir path given, open files and read from there
138 struct dirent **files;
140 ndirs = scandir(descrdir, &files, 0, alphasort);
147 /* bring packages to front */
148 for (i = 0; i < ndirs; i++)
150 char *fn = files[i]->d_name;
151 if (!strcmp(fn, "packages") || !strcmp(fn, "packages.gz"))
156 fprintf(stderr, "found no packages file\n");
161 struct dirent *de = files[i];
162 memmove(files + 1, files, i * sizeof(de));
166 fnp = sat_malloc(strlen(descrdir) + 128);
167 for (i = 0; i < ndirs; i++)
169 char *fn = files[i]->d_name;
171 if (!strcmp(fn, "packages") || !strcmp(fn, "packages.gz"))
173 sprintf(fnp, "%s/%s", descrdir, fn);
174 FILE *fp = sat_xfopen(fnp);
180 repo_add_susetags(repo, fp, defvendor, 0, flags | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE);
183 else if (!strcmp(fn, "packages.DU") || !strcmp(fn, "packages.DU.gz"))
185 sprintf(fnp, "%s/%s", descrdir, fn);
186 FILE *fp = sat_xfopen(fnp);
192 repo_add_susetags(repo, fp, defvendor, 0, flags | SUSETAGS_EXTEND | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE);
195 else if (!strcmp(fn, "packages.FL") || !strcmp(fn, "packages.FL.gz"))
198 sprintf(fnp, "%s/%s", descrdir, fn);
199 FILE *fp = sat_xfopen(fnp);
205 repo_add_susetags(repo, fp, defvendor, 0, flags | SUSETAGS_EXTEND | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE);
208 /* ignore for now. reactivate when filters work */
212 else if (!strncmp(fn, "packages.", 9))
216 sprintf(fnp, "%s/%s", descrdir, fn);
217 p = strrchr(fnp, '.');
218 if (p && !strcmp(p, ".gz"))
221 p = strrchr(fnp, '.');
223 if (!p || !p[1] || strlen(p + 1) > 5)
226 sprintf(fnp, "%s/%s", descrdir, fn);
227 FILE *fp = sat_xfopen(fnp);
233 repo_add_susetags(repo, fp, defvendor, lang, flags | SUSETAGS_EXTEND | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE);
237 for (i = 0; i < ndirs; i++)
241 repo_internalize(repo);
244 /* read data from stdin */
245 repo_add_susetags(repo, stdin, defvendor, 0, REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE);
246 repo_internalize(repo);
249 doquery(pool, repo, query);
251 tool_write(repo, basefile, attrname);