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>
21 #include "repo_susetags.h"
22 #include "repo_content.h"
23 #include "common_write.h"
28 fprintf(stderr, "\nUsage:\n"
29 "susetags2solv [-a][-s][-c <content>][-h]\n"
30 " reads a 'susetags' repository from <stdin> and writes a .solv file to <stdout>\n"
31 " -c <contentfile> : parse given contentfile (for product information)\n"
32 " -d <descrdir> : do not read from stdin, but use data in descrdir\n"
33 " -h : print help & exit\n"
34 " -k : don't mix kinds (experimental!)\n"
35 " -b <base>: save fas multiple files starting with <base>\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);
75 main(int argc, char **argv)
77 const char *contentfile = 0;
78 const char *attrname = 0;
79 const char *descrdir = 0;
80 const char *basefile = 0;
85 while ((c = getopt(argc, argv, "hkn:c:d:b:")) >= 0)
93 flags |= SUSETAGS_KINDS_SEPARATELY; /* do not use! */
112 Pool *pool = pool_create();
113 Repo *repo = repo_create(pool, "<susetags>");
116 FILE *fp = fopen (contentfile, "r");
122 repo_add_content(repo, fp);
123 if (!strncmp (id2str(pool, pool->solvables[repo->start].name), "product:", 8))
124 vendor = pool->solvables[repo->start].vendor;
129 /* ensure '.attr' suffix */
130 const char *dot = strrchr(attrname, '.');
131 if (!dot || strcmp(dot, ".attr"))
133 int len = strlen (attrname);
134 char *newname = (char *)malloc(len + 6); /* alloc for <attrname>+'.attr'+'\0' */
135 strcpy (newname, attrname);
136 strcpy (newname+len, ".attr");
145 struct dirent **files;
147 ndirs = scandir(descrdir, &files, 0, alphasort);
154 /* bring packages to front */
155 for (i = 0; i < ndirs; i++)
157 char *fn = files[i]->d_name;
158 if (!strcmp(fn, "packages") || !strcmp(fn, "packages.gz"))
163 fprintf(stderr, "found no packages file\n");
168 struct dirent *de = files[i];
169 memmove(files + 1, files, i * sizeof(de));
173 fnp = sat_malloc(strlen(descrdir) + 128);
174 for (i = 0; i < ndirs; i++)
176 char *fn = files[i]->d_name;
178 if (!strcmp(fn, "packages") || !strcmp(fn, "packages.gz"))
180 sprintf(fnp, "%s/%s", descrdir, fn);
181 FILE *fp = myfopen(fnp);
187 repo_add_susetags(repo, fp, vendor, 0, flags);
190 else if (!strcmp(fn, "packages.DU") || !strcmp(fn, "packages.DU.gz"))
192 sprintf(fnp, "%s/%s", descrdir, fn);
193 FILE *fp = myfopen(fnp);
199 repo_add_susetags(repo, fp, vendor, 0, flags | SUSETAGS_EXTEND);
202 else if (!strncmp(fn, "packages.", 9))
206 sprintf(fnp, "%s/%s", descrdir, fn);
207 p = strrchr(fnp, '.');
208 if (p && !strcmp(p, ".gz"))
211 p = strrchr(fnp, '.');
213 if (!p || !p[1] || strlen(p + 1) > 5)
216 sprintf(fnp, "%s/%s", descrdir, fn);
217 FILE *fp = myfopen(fnp);
223 repo_add_susetags(repo, fp, vendor, lang, flags | SUSETAGS_EXTEND);
227 for (i = 0; i < ndirs; i++)
233 repo_add_susetags(repo, stdin, vendor, 0, flags);
235 tool_write(repo, basefile, attrname);