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 [-a][-s][-c <content>][-h]\n"
31 " reads a 'susetags' repository from <stdin> and writes a .solv file to <stdout>\n"
32 " -c <contentfile> : parse given contentfile (for product information)\n"
33 " -d <descrdir> : do not read from stdin, but use data in descrdir\n"
34 " -h : print help & exit\n"
35 " -k : don't mix kinds (experimental!)\n"
36 " -b <base>: save as multiple files starting with <base>\n"
37 " -n <name>: save attributes as <name>.attr\n"
43 cookie_gzread(void *cookie, char *buf, size_t nbytes)
45 return gzread((gzFile *)cookie, buf, nbytes);
49 cookie_gzclose(void *cookie)
51 return gzclose((gzFile *)cookie);
55 myfopen(const char *fn)
57 cookie_io_functions_t cio;
63 suf = strrchr(fn, '.');
64 if (!suf || strcmp(suf, ".gz") != 0)
65 return fopen(fn, "r");
66 gzf = gzopen(fn, "r");
69 memset(&cio, 0, sizeof(cio));
70 cio.read = cookie_gzread;
71 cio.close = cookie_gzclose;
72 return fopencookie(gzf, "r", cio);
76 main(int argc, char **argv)
78 const char *contentfile = 0;
79 const char *attrname = 0;
80 const char *descrdir = 0;
81 const char *basefile = 0;
86 while ((c = getopt(argc, argv, "hkn:c:d:b:")) >= 0)
94 flags |= SUSETAGS_KINDS_SEPARATELY; /* do not use! */
100 contentfile = optarg;
113 Pool *pool = pool_create();
114 Repo *repo = repo_create(pool, "<susetags>");
116 repo_add_repodata(repo, 0);
120 FILE *fp = fopen (contentfile, "r");
126 repo_add_content(repo, fp, REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE);
127 product = repo->start;
133 /* ensure '.attr' suffix */
134 const char *dot = strrchr(attrname, '.');
135 if (!dot || strcmp(dot, ".attr"))
137 int len = strlen (attrname);
138 char *newname = (char *)malloc(len + 6); /* alloc for <attrname>+'.attr'+'\0' */
139 strcpy (newname, attrname);
140 strcpy (newname+len, ".attr");
146 * descrdir path given, open files and read from there
153 struct dirent **files;
155 ndirs = scandir(descrdir, &files, 0, alphasort);
162 /* bring packages to front */
163 for (i = 0; i < ndirs; i++)
165 char *fn = files[i]->d_name;
166 if (!strcmp(fn, "packages") || !strcmp(fn, "packages.gz"))
171 fprintf(stderr, "found no packages file\n");
176 struct dirent *de = files[i];
177 memmove(files + 1, files, i * sizeof(de));
181 fnp = sat_malloc(strlen(descrdir) + 128);
182 for (i = 0; i < ndirs; i++)
184 char *fn = files[i]->d_name;
186 if (!strcmp(fn, "packages") || !strcmp(fn, "packages.gz"))
188 sprintf(fnp, "%s/%s", descrdir, fn);
189 FILE *fp = myfopen(fnp);
195 repo_add_susetags(repo, fp, product, 0, flags | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE);
198 else if (!strcmp(fn, "packages.DU") || !strcmp(fn, "packages.DU.gz"))
200 sprintf(fnp, "%s/%s", descrdir, fn);
201 FILE *fp = myfopen(fnp);
207 repo_add_susetags(repo, fp, product, 0, flags | SUSETAGS_EXTEND | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE);
210 else if (!strcmp(fn, "packages.FL") || !strcmp(fn, "packages.FL.gz"))
213 sprintf(fnp, "%s/%s", descrdir, fn);
214 FILE *fp = myfopen(fnp);
220 repo_add_susetags(repo, fp, product, 0, flags | SUSETAGS_EXTEND | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE);
223 /* ignore for now. reactivate when filters work */
227 else if (!strncmp(fn, "packages.", 9))
231 sprintf(fnp, "%s/%s", descrdir, fn);
232 p = strrchr(fnp, '.');
233 if (p && !strcmp(p, ".gz"))
236 p = strrchr(fnp, '.');
238 if (!p || !p[1] || strlen(p + 1) > 5)
241 sprintf(fnp, "%s/%s", descrdir, fn);
242 FILE *fp = myfopen(fnp);
248 repo_add_susetags(repo, fp, product, lang, flags | SUSETAGS_EXTEND | REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE);
252 for (i = 0; i < ndirs; i++)
256 repo_internalize(repo);
259 /* read data from stdin */
260 repo_add_susetags(repo, stdin, product, 0, REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE);
261 repo_internalize(repo);
262 tool_write(repo, basefile, attrname);