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"
29 fprintf(stderr, "\nUsage:\n"
30 "susetags2solv [-b <base>][-c <content>][-d <descrdir>][-h][-n <name>]\n"
31 " reads a 'susetags' repository from <stdin> and writes a .solv file to <stdout>\n"
32 " -b <base>: save as multiple files starting with <base>\n"
33 " -c <contentfile> : parse given contentfile (for product information)\n"
34 " -d <descrdir> : do not read from stdin, but use data in descrdir\n"
35 " -h : print help & exit\n"
36 " -n <name>: save attributes as <name>.attr\n"
42 cookie_gzread(void *cookie, char *buf, size_t nbytes)
44 return gzread((gzFile *)cookie, buf, nbytes);
48 cookie_gzclose(void *cookie)
50 return gzclose((gzFile *)cookie);
54 myfopen(const char *fn)
56 cookie_io_functions_t cio;
62 suf = strrchr(fn, '.');
63 if (!suf || strcmp(suf, ".gz") != 0)
64 return fopen(fn, "r");
65 gzf = gzopen(fn, "r");
68 memset(&cio, 0, sizeof(cio));
69 cio.read = cookie_gzread;
70 cio.close = cookie_gzclose;
71 return fopencookie(gzf, "r", cio);
74 /* content file query */
76 doquery(Pool *pool, Repo *repo, const char *arg)
82 snprintf(qbuf, sizeof(qbuf), "susetags:%s", arg);
83 id = str2id(pool, qbuf, 0);
86 str = repo_lookup_str(repo, SOLVID_META, id);
92 main(int argc, char **argv)
94 const char *contentfile = 0;
95 const char *attrname = 0;
96 const char *descrdir = 0;
97 const char *basefile = 0;
98 const char *query = 0;
103 while ((c = getopt(argc, argv, "hn:c:d:b:q:")) >= 0)
114 contentfile = optarg;
130 Pool *pool = pool_create();
131 Repo *repo = repo_create(pool, "<susetags>");
133 repo_add_repodata(repo, 0);
137 FILE *fp = fopen(contentfile, "r");
143 repo_add_content(repo, fp, REPO_REUSE_REPODATA);
144 defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
150 /* ensure '.attr' suffix */
151 const char *dot = strrchr(attrname, '.');
152 if (!dot || strcmp(dot, ".attr"))
154 int len = strlen (attrname);
155 char *newname = (char *)malloc(len + 6); /* alloc for <attrname>+'.attr'+'\0' */
156 strcpy (newname, attrname);
157 strcpy (newname+len, ".attr");
163 * descrdir path given, open files and read from there
170 struct dirent **files;
172 ndirs = scandir(descrdir, &files, 0, alphasort);
179 /* bring packages to front */
180 for (i = 0; i < ndirs; i++)
182 char *fn = files[i]->d_name;
183 if (!strcmp(fn, "packages") || !strcmp(fn, "packages.gz"))
188 fprintf(stderr, "found no packages file\n");
193 struct dirent *de = files[i];
194 memmove(files + 1, files, i * sizeof(de));
198 fnp = sat_malloc(strlen(descrdir) + 128);
199 for (i = 0; i < ndirs; i++)
201 char *fn = files[i]->d_name;
203 if (!strcmp(fn, "packages") || !strcmp(fn, "packages.gz"))
205 sprintf(fnp, "%s/%s", descrdir, fn);
206 FILE *fp = myfopen(fnp);
212 repo_add_susetags(repo, fp, defvendor, 0, flags | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE);
215 else if (!strcmp(fn, "packages.DU") || !strcmp(fn, "packages.DU.gz"))
217 sprintf(fnp, "%s/%s", descrdir, fn);
218 FILE *fp = myfopen(fnp);
224 repo_add_susetags(repo, fp, defvendor, 0, flags | SUSETAGS_EXTEND | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE);
227 else if (!strcmp(fn, "packages.FL") || !strcmp(fn, "packages.FL.gz"))
230 sprintf(fnp, "%s/%s", descrdir, fn);
231 FILE *fp = myfopen(fnp);
237 repo_add_susetags(repo, fp, defvendor, 0, flags | SUSETAGS_EXTEND | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE);
240 /* ignore for now. reactivate when filters work */
244 else if (!strncmp(fn, "packages.", 9))
248 sprintf(fnp, "%s/%s", descrdir, fn);
249 p = strrchr(fnp, '.');
250 if (p && !strcmp(p, ".gz"))
253 p = strrchr(fnp, '.');
255 if (!p || !p[1] || strlen(p + 1) > 5)
258 sprintf(fnp, "%s/%s", descrdir, fn);
259 FILE *fp = myfopen(fnp);
265 repo_add_susetags(repo, fp, defvendor, lang, flags | SUSETAGS_EXTEND | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE);
269 for (i = 0; i < ndirs; i++)
273 repo_internalize(repo);
276 /* read data from stdin */
277 repo_add_susetags(repo, stdin, defvendor, 0, REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE);
278 repo_internalize(repo);
281 doquery(pool, repo, query);
283 tool_write(repo, basefile, attrname);