- make file list conversion twice as fast by adding a 1k dir cache
[platform/upstream/libsolv.git] / ext / testcase.c
1 /*
2  * Copyright (c) 2012, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <limits.h>
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <errno.h>
16
17 #include "pool.h"
18 #include "poolarch.h"
19 #include "poolvendor.h"
20 #include "repo.h"
21 #include "repo_susetags.h"
22 #include "solver.h"
23 #include "solverdebug.h"
24 #include "chksum.h"
25 #include "testcase.h"
26 #include "solv_xfopen.h"
27
28 #ifndef ENABLE_SUSEREPO
29 # define DISABLE_JOIN2
30 # include "tools_util.h"
31 #else
32 # include "repo_susetags.h"
33 #endif
34
35 static struct job2str {
36   Id job;
37   const char *str;
38 } job2str[] = {
39   { SOLVER_NOOP,          "noop" },
40   { SOLVER_INSTALL,       "install" },
41   { SOLVER_ERASE,         "erase" },
42   { SOLVER_UPDATE,        "update" },
43   { SOLVER_WEAKENDEPS,    "weakendeps" },
44   { SOLVER_NOOBSOLETES,   "noobsoletes" },
45   { SOLVER_LOCK,          "lock" },
46   { SOLVER_DISTUPGRADE,   "distupgrade" },
47   { SOLVER_VERIFY,        "verify" },
48   { SOLVER_DROP_ORPHANED, "droporphaned" },
49   { SOLVER_USERINSTALLED, "userinstalled" },
50   { 0, 0 }
51 };
52
53 static struct jobflags2str {
54   Id flag;
55   const char *str;
56 } jobflags2str[] = {
57   { SOLVER_WEAK,      "weak" },
58   { SOLVER_ESSENTIAL, "essential" },
59   { SOLVER_CLEANDEPS, "cleandeps" },
60   { SOLVER_SETEV,     "setev" },
61   { SOLVER_SETEVR,    "setevr" },
62   { SOLVER_SETARCH,   "setarch" },
63   { SOLVER_SETVENDOR, "setvendor" },
64   { SOLVER_SETREPO,   "setrepo" },
65   { SOLVER_NOAUTOSET, "noautoset" },
66   { 0, 0 }
67 };
68
69 static struct resultflags2str {
70   Id flag;
71   const char *str;
72 } resultflags2str[] = {
73   { TESTCASE_RESULT_TRANSACTION,        "transaction" },
74   { TESTCASE_RESULT_PROBLEMS,           "problems" },
75   { TESTCASE_RESULT_ORPHANED,           "orphaned" },
76   { TESTCASE_RESULT_RECOMMENDED,        "recommended" },
77   { TESTCASE_RESULT_UNNEEDED,           "unneeded" },
78   { 0, 0 }
79 };
80
81 static struct solverflags2str {
82   Id flag;
83   const char *str;
84   int def;
85 } solverflags2str[] = {
86   { SOLVER_FLAG_ALLOW_DOWNGRADE,            "allowdowngrade", 0 },
87   { SOLVER_FLAG_ALLOW_ARCHCHANGE,           "allowarchchange", 0 },
88   { SOLVER_FLAG_ALLOW_VENDORCHANGE,         "allowvendorchange", 0 },
89   { SOLVER_FLAG_ALLOW_UNINSTALL,            "allowuninstall", 0 },
90   { SOLVER_FLAG_NO_UPDATEPROVIDE,           "noupdateprovide", 0 },
91   { SOLVER_FLAG_SPLITPROVIDES,              "splitprovides", 0 },
92   { SOLVER_FLAG_IGNORE_RECOMMENDED,         "ignorerecommended", 0 },
93   { SOLVER_FLAG_IGNORE_ALREADY_RECOMMENDED, "ignorealreadyrecommended", 0 },
94   { SOLVER_FLAG_NO_INFARCHCHECK,            "noinfarchcheck", 0 },
95   { 0, 0, 0 }
96 };
97
98
99 typedef struct strqueue {
100   char **str;
101   int nstr;
102 } Strqueue;
103
104 #define STRQUEUE_BLOCK 63
105
106 static void
107 strqueue_init(Strqueue *q)
108 {
109   q->str = 0;
110   q->nstr = 0;
111 }
112
113 static void
114 strqueue_free(Strqueue *q)
115 {
116   int i;
117   for (i = 0; i < q->nstr; i++)
118     solv_free(q->str[i]);
119   q->str = solv_free(q->str);
120   q->nstr = 0;
121 }
122
123 static void
124 strqueue_push(Strqueue *q, const char *s)
125 {
126   q->str = solv_extend(q->str, q->nstr, 1, sizeof(*q->str), STRQUEUE_BLOCK);
127   q->str[q->nstr++] = solv_strdup(s);
128 }
129
130 static void
131 strqueue_pushjoin(Strqueue *q, const char *s1, const char *s2, const char *s3)
132 {
133   q->str = solv_extend(q->str, q->nstr, 1, sizeof(*q->str), STRQUEUE_BLOCK);
134   q->str[q->nstr++] = solv_dupjoin(s1, s2, s3);
135 }
136
137 static int
138 strqueue_sort_cmp(const void *ap, const void *bp, void *dp)
139 {
140   const char *a = *(const char **)ap;
141   const char *b = *(const char **)bp;
142   return strcmp(a ? a : "", b ? b : "");
143 }
144
145 static void
146 strqueue_sort(Strqueue *q)
147 {
148   if (q->nstr > 1)
149     solv_sort(q->str, q->nstr, sizeof(*q->str), strqueue_sort_cmp, 0);
150 }
151
152 static void
153 strqueue_sort_u(Strqueue *q)
154 {
155   int i, j;
156   strqueue_sort(q);
157   for (i = j = 0; i < q->nstr; i++)
158     if (!j || strqueue_sort_cmp(q->str + i, q->str + j - 1, 0) != 0)
159       q->str[j++] = q->str[i];
160   q->nstr = j;
161 }
162
163 static char *
164 strqueue_join(Strqueue *q)
165 {
166   int i, l = 0;
167   char *r, *rp;
168   for (i = 0; i < q->nstr; i++)
169     if (q->str[i])
170       l += strlen(q->str[i]) + 1;
171   l++;  /* trailing \0 */
172   r = solv_malloc(l);
173   rp = r;
174   for (i = 0; i < q->nstr; i++)
175     if (q->str[i])
176       {
177         strcpy(rp, q->str[i]);
178         rp += strlen(rp);
179         *rp++ = '\n';
180       }
181   *rp = 0;
182   return r;
183 }
184
185 static void
186 strqueue_split(Strqueue *q, const char *s)
187 {
188   const char *p;
189   while ((p = strchr(s, '\n')) != 0)
190     {
191       q->str = solv_extend(q->str, q->nstr, 1, sizeof(*q->str), STRQUEUE_BLOCK);
192       q->str[q->nstr] = solv_malloc(p - s + 1);
193       if (p > s)
194         memcpy(q->str[q->nstr], s, p - s);
195       q->str[q->nstr][p - s] = 0;
196       q->nstr++;
197       s = p + 1;
198     }
199   if (*s)
200     strqueue_push(q, s);
201 }
202
203 static void
204 strqueue_diff(Strqueue *sq1, Strqueue *sq2, Strqueue *osq)
205 {
206   int i = 0, j = 0;
207   while (i < sq1->nstr && j < sq2->nstr)
208     {
209       int r = strqueue_sort_cmp(sq1->str + i, sq2->str + j, 0);
210       if (!r)
211         i++, j++;
212       else if (r < 0)
213         strqueue_pushjoin(osq, "-", sq1->str[i++], 0);
214       else
215         strqueue_pushjoin(osq, "+", sq2->str[j++], 0);
216     }
217   while (i < sq1->nstr)
218     strqueue_pushjoin(osq, "-", sq1->str[i++], 0);
219   while (j < sq2->nstr)
220     strqueue_pushjoin(osq, "+", sq2->str[j++], 0);
221 }
222
223 static inline int
224 pool_isknownarch(Pool *pool, Id id)
225 {
226   if (!id || id == ID_EMPTY)
227     return 0;
228   if (id == ARCH_SRC || id == ARCH_NOSRC || id == ARCH_NOARCH)
229     return 1;
230   if (!pool->id2arch || (id > pool->lastarch || !pool->id2arch[id]))
231     return 0;
232   return 1;
233 }
234
235 Id
236 testcase_str2dep(Pool *pool, char *s)
237 {
238   char *n, *a;
239   Id id;
240   int flags;
241
242   if ((n = strchr(s, '|')) != 0)
243     {    
244       id = testcase_str2dep(pool, n + 1);
245       *n = 0; 
246       id = pool_rel2id(pool, testcase_str2dep(pool, s), id, REL_OR, 1);
247       *n = '|'; 
248       return id;
249     }
250   while (*s == ' ' || *s == '\t')
251     s++;
252   n = s;
253   while (*s && *s != ' ' && *s != '\t' && *s != '<' && *s != '=' && *s != '>')
254     {
255       if (*s == '(')
256         {
257           while (*s && *s != ')')
258             s++;
259         }
260       else
261         s++;
262     }
263   if ((a = strchr(n, '.')) != 0 && a + 1 < s && s[-1] != ')')
264     {
265       Id archid = pool_strn2id(pool, a + 1, s - (a + 1), 0);
266       if (pool_isknownarch(pool, archid))
267         {
268           id = pool_strn2id(pool, n, a - n, 1);
269           id = pool_rel2id(pool, id, archid, REL_ARCH, 1);
270         }
271       else
272         id = pool_strn2id(pool, n, s - n, 1);
273     }
274   else
275     id = pool_strn2id(pool, n, s - n, 1);
276   if (!*s)
277     return id;
278   while (*s == ' ' || *s == '\t')
279     s++;
280   flags = 0;
281   for (;;s++)
282     {  
283       if (*s == '<')
284         flags |= REL_LT;
285       else if (*s == '=')
286         flags |= REL_EQ;
287       else if (*s == '>')
288         flags |= REL_GT;
289       else
290         break;
291     }
292   if (!flags)
293     return id;
294   while (*s == ' ' || *s == '\t')
295     s++;
296   n = s;
297   while (*s && *s != ' ' && *s != '\t')
298     s++;
299   return pool_rel2id(pool, id, pool_strn2id(pool, n, s - n, 1), flags, 1);
300 }
301
302 const char *
303 testcase_repoid2str(Pool *pool, Id repoid)
304 {
305   Repo *repo = pool_id2repo(pool, repoid);
306   if (repo->name)
307     {
308       char *r = pool_tmpjoin(pool, repo->name, 0, 0);
309       char *rp;
310       for (rp = r; *rp; rp++)
311         if (*rp == ' ' || *rp == '\t')
312           *rp = '_';
313       return r;
314     }
315   else
316     {
317       char buf[20];
318       sprintf(buf, "@#%d", repoid);
319       return pool_tmpjoin(pool, buf, 0, 0);
320     }
321 }
322
323 const char *
324 testcase_solvid2str(Pool *pool, Id p)
325 {
326   Solvable *s = pool->solvables + p;
327   const char *str;
328   char buf[20];
329
330   if (p == SYSTEMSOLVABLE)
331     return "@SYSTEM";
332   str = pool_solvid2str(pool, p);
333   if (!s->repo)
334     return pool_tmpappend(pool, str, "@", 0);
335   if (s->repo->name)
336     {
337       int l = strlen(str);
338       char *str2 = pool_tmpappend(pool, str, "@", s->repo->name);
339       for (; str2[l]; l++)
340         if (str2[l] == ' ' || str2[l] == '\t')
341           str2[l] = '_';
342       return str2;
343     }
344   sprintf(buf, "@#%d", s->repo->repoid);
345   return pool_tmpappend(pool, str, buf, 0);
346 }
347
348 Repo *
349 testcase_str2repo(Pool *pool, const char *str)
350 {
351   int repoid;
352   Repo *repo = 0;
353   if (str[0] == '#' && (str[1] >= '0' && str[1] <= '9'))
354     {
355       int j;
356       repoid = 0;
357       for (j = 1; str[j] >= '0' && str[j] <= '9'; j++)
358         repoid = repoid * 10 + (str[j] - '0');
359       if (!str[j] && repoid > 0 && repoid < pool->nrepos)
360         repo = pool_id2repo(pool, repoid);
361     }
362   if (!repo)
363     {
364       FOR_REPOS(repoid, repo)
365         {
366           int i, l;
367           if (!repo->name)
368             continue;
369           l = strlen(repo->name);
370           for (i = 0; i < l; i++)
371             {
372               int c = repo->name[i];
373               if (c == ' ' || c == '\t')
374                 c = '_';
375               if (c != str[i])
376                 break;
377             }
378           if (i == l && !str[l])
379             break;
380         }
381       if (repoid >= pool->nrepos)
382         repo = 0;
383     }
384   return repo;
385 }
386
387 Id
388 testcase_str2solvid(Pool *pool, const char *str)
389 {
390   int i, l = strlen(str);
391   int repostart;
392   Repo *repo;
393   Id arch;
394
395   if (!l)
396     return 0;
397   if (*str == '@' && !strcmp(str, "@SYSTEM"))
398     return SYSTEMSOLVABLE;
399   repo = 0;
400   for (i = l - 1; i >= 0; i--)
401     if (str[i] == '@' && (repo = testcase_str2repo(pool, str + i + 1)) != 0)
402       break;
403   if (i < 0)
404     i = l;
405   repostart = i;
406   /* now find the arch (if present) */
407   arch = 0;
408   for (i = repostart - 1; i > 0; i--)
409     if (str[i] == '.')
410       {
411         arch = pool_strn2id(pool, str + i + 1, repostart - (i + 1), 0);
412         if (arch)
413           repostart = i;
414         break;
415       }
416   /* now find the name */
417   for (i = repostart - 1; i > 0; i--)
418     {
419       if (str[i] == '-')
420         {
421           Id nid, evrid, p, pp;
422           nid = pool_strn2id(pool, str, i, 0);
423           if (!nid)
424             continue;
425           evrid = pool_strn2id(pool, str + i + 1, repostart - (i + 1), 0);
426           if (!evrid)
427             continue;
428           FOR_PROVIDES(p, pp, nid)
429             {
430               Solvable *s = pool->solvables + p;
431               if (s->name != nid || s->evr != evrid)
432                 continue;
433               if (repo && s->repo != repo)
434                 continue;
435               if (arch && s->arch != arch)
436                 continue;
437               return p;
438             }
439         }
440     }
441   return 0;
442 }
443
444 const char *
445 testcase_job2str(Pool *pool, Id how, Id what)
446 {
447   char *ret;
448   const char *jobstr;
449   const char *selstr;
450   const char *pkgstr;
451   int i, o;
452   Id select = how & SOLVER_SELECTMASK;
453
454   for (i = 0; job2str[i].str; i++)
455     if ((how & SOLVER_JOBMASK) == job2str[i].job)
456       break;
457   jobstr = job2str[i].str ? job2str[i].str : "unknown";
458   if (select == SOLVER_SOLVABLE)
459     {
460       selstr = " pkg ";
461       pkgstr = testcase_solvid2str(pool, what);
462     }
463   else if (select == SOLVER_SOLVABLE_NAME)
464     {
465       selstr = " name ";
466       pkgstr = pool_dep2str(pool, what);
467     }
468   else if (select == SOLVER_SOLVABLE_PROVIDES)
469     {
470       selstr = " provides ";
471       pkgstr = pool_dep2str(pool, what);
472     }
473   else if (select == SOLVER_SOLVABLE_ONE_OF)
474     {
475       Id p;
476       selstr = " oneof ";
477       pkgstr = 0;
478       while ((p = pool->whatprovidesdata[what++]) != 0)
479         {
480           const char *s = testcase_solvid2str(pool, p);
481           if (pkgstr)
482             {
483               pkgstr = pool_tmpappend(pool, pkgstr, " ", s);
484               pool_freetmpspace(pool, s);
485             }
486           else
487             pkgstr = s;
488         }
489       if (!pkgstr)
490         pkgstr = "nothing";
491     }
492   else if (select == SOLVER_SOLVABLE_REPO)
493     {
494       Repo *repo = pool_id2repo(pool, what);
495       selstr = " repo ";
496       if (!repo->name)
497         {
498           char buf[20];
499           sprintf(buf, "#%d", repo->repoid);
500           pkgstr = pool_tmpjoin(pool, buf, 0, 0);
501         }
502       else
503         pkgstr = pool_tmpjoin(pool, repo->name, 0, 0);
504     }
505   else if (select == SOLVER_SOLVABLE_ALL)
506     {
507       selstr = " all ";
508       pkgstr = "packages";
509     }
510   else
511     {
512       selstr = " unknown ";
513       pkgstr = "unknown";
514     }
515   ret = pool_tmpjoin(pool, jobstr, selstr, pkgstr);
516   o = strlen(ret);
517   ret = pool_tmpappend(pool, ret, " ", 0);
518   for (i = 0; jobflags2str[i].str; i++)
519     if ((how & jobflags2str[i].flag) != 0)
520       ret = pool_tmpappend(pool, ret, ",", jobflags2str[i].str);
521   if (!ret[o + 1])
522     ret[o] = 0;
523   else
524     {
525       ret[o + 1] = '[';
526       ret = pool_tmpappend(pool, ret, "]", 0);
527     }
528   return ret;
529 }
530
531 Id
532 testcase_str2job(Pool *pool, const char *str, Id *whatp)
533 {
534   int i;
535   Id job;
536   Id what;
537   char *s;
538   char **pieces = 0;
539   int npieces = 0;
540
541   *whatp = 0;
542   /* so we can patch it */
543   s = pool_tmpjoin(pool, str, 0, 0);
544   /* split it in pieces */
545   for (;;)
546     {
547       while (*s == ' ' || *s == '\t')
548         s++;
549       if (!*s)
550         break;
551       pieces = solv_extend(pieces, npieces, 1, sizeof(*pieces), 7);
552       pieces[npieces++] = s;
553       while (*s && *s != ' ' && *s != '\t')
554         s++;
555       if (*s)
556         *s++ = 0;
557     }
558   if (npieces < 3)
559     {
560       pool_debug(pool, SOLV_ERROR, "str2job: bad line '%s'\n", str);
561       return 0;
562     }
563
564   for (i = 0; job2str[i].str; i++)
565     if (!strcmp(pieces[0], job2str[i].str))
566       break;
567   if (!job2str[i].str)
568     {
569       pool_debug(pool, SOLV_ERROR, "str2job: unknown job '%s'\n", str);
570       return 0;
571     }
572   job = job2str[i].job;
573   if (npieces > 3)
574     {
575       char *flags = pieces[npieces - 1];
576       char *nf;
577       if (*flags == '[' && flags[strlen(flags) - 1] == ']')
578         {
579           npieces--;
580           flags++;
581           flags[strlen(flags) - 1] = ',';
582           while (*flags)
583             {
584               for (nf = flags; *nf != ','; nf++)
585                 ;
586               *nf++ = 0;
587               for (i = 0; jobflags2str[i].str; i++)
588                 if (!strcmp(flags, jobflags2str[i].str))
589                   break;
590               if (!jobflags2str[i].str)
591                 {
592                   pool_debug(pool, SOLV_ERROR, "str2job: unknown jobflags in '%s'\n", str);
593                   return 0;
594                 }
595               job |= jobflags2str[i].flag;
596               flags = nf;
597             }
598         }
599     }
600   if (!strcmp(pieces[1], "pkg"))
601     {
602       if (npieces != 3)
603         {
604           pool_debug(pool, SOLV_ERROR, "str2job: bad pkg selector in '%s'\n", str);
605           return 0;
606         }
607       job |= SOLVER_SOLVABLE;
608       what = testcase_str2solvid(pool, pieces[2]);
609       if (!what)
610         {
611           pool_debug(pool, SOLV_ERROR, "str2job: unknown package '%s'\n", pieces[2]);
612           return 0;
613         }
614     }
615   else if (!strcmp(pieces[1], "name") || !strcmp(pieces[1], "provides"))
616     {
617       /* join em again for dep2str... */
618       char *sp;
619       for (sp = pieces[2]; sp < pieces[npieces - 1]; sp++)
620         if (*sp == 0)
621           *sp = ' ';
622       what = testcase_str2dep(pool, pieces[2]);
623       if (pieces[1][0] == 'n')
624         job |= SOLVER_SOLVABLE_NAME;
625       else
626         job |= SOLVER_SOLVABLE_PROVIDES;
627     }
628   else if (!strcmp(pieces[1], "oneof"))
629     {
630       Queue q;
631       job |= SOLVER_SOLVABLE_ONE_OF;
632       queue_init(&q);
633       if (npieces > 3 && strcmp(pieces[2], "nothing") != 0)
634         {
635           for (i = 2; i < npieces; i++)
636             {
637               Id p = testcase_str2solvid(pool, pieces[i]);
638               if (!p)
639                 {
640                   pool_debug(pool, SOLV_ERROR, "str2job: unknown package '%s'\n", pieces[i]);
641                   queue_free(&q);
642                   return 0;
643                 }
644               queue_push(&q, p);
645             }
646         }
647       what = pool_queuetowhatprovides(pool, &q);
648       queue_free(&q);
649     }
650   else if (!strcmp(pieces[1], "repo"))
651     {
652       Repo *repo;
653       if (npieces != 3)
654         {
655           pool_debug(pool, SOLV_ERROR, "str2job: bad line '%s'\n", str);
656           return 0;
657         }
658       repo = testcase_str2repo(pool, pieces[2]);
659       if (!repo)
660         {
661           pool_debug(pool, SOLV_ERROR, "str2job: unknown repo '%s'\n", pieces[2]);
662           return 0;
663         }
664       job |= SOLVER_SOLVABLE_REPO;
665       what = repo->repoid;
666     }
667   else if (!strcmp(pieces[1], "all"))
668     {
669       if (npieces != 3 && strcmp(pieces[2], "packages") != 0)
670         {
671           pool_debug(pool, SOLV_ERROR, "str2job: bad line '%s'\n", str);
672           return 0;
673         }
674       job |= SOLVER_SOLVABLE_ALL;
675       what = 0;
676     }
677   else
678     {
679       pool_debug(pool, SOLV_ERROR, "str2job: unknown selection in '%s'\n", str);
680       return 0;
681     }
682   *whatp = what;
683   return job;
684 }
685
686 static void
687 writedeps(Repo *repo, FILE *fp, const char *tag, Id key, Solvable *s, Offset off)
688 {
689   Pool *pool = repo->pool;
690   Id id, *dp, *prvdp;
691   int tagwritten = 0;
692   const char *idstr;
693
694   if (!off)
695     return;
696   dp = repo->idarraydata + off;
697   prvdp = 0;
698   while ((id = *dp++) != 0)
699     {
700       if (key == SOLVABLE_REQUIRES && id == SOLVABLE_PREREQMARKER)
701         {
702           if (tagwritten)
703             fprintf(fp, "-%s\n", tag);
704           tagwritten = 0;
705           tag = "Prq:";
706           continue;
707         }
708       if (key == SOLVABLE_PROVIDES && id == SOLVABLE_FILEMARKER)
709         {
710           prvdp = dp;
711           continue;
712         }
713       idstr = pool_dep2str(pool, id);
714       if (ISRELDEP(id))
715         {
716           Reldep *rd = GETRELDEP(pool, id);
717           if (key == SOLVABLE_CONFLICTS && rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_OTHERPROVIDERS)
718             {
719               if (!strncmp(idstr, "namespace:", 10))
720                 idstr += 10;
721             }
722           if (key == SOLVABLE_SUPPLEMENTS)
723             {
724               if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_FILESYSTEM)
725                 {
726                   if (!strncmp(idstr, "namespace:", 10))
727                     idstr += 10;
728                 }
729               else if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_MODALIAS)
730                 {
731                   if (!strncmp(idstr, "namespace:", 10))
732                     idstr += 10;
733                 }
734               else if (rd->flags == REL_AND)
735                 {
736                   /* either packageand chain or modalias */
737                   idstr = 0;
738                   if (ISRELDEP(rd->evr))
739                     {
740                       Reldep *mrd = GETRELDEP(pool, rd->evr);
741                       if (mrd->flags == REL_NAMESPACE && mrd->name == NAMESPACE_MODALIAS)
742                         {
743                           idstr = pool_tmpjoin(pool, "modalias(", pool_dep2str(pool, rd->name), ":");
744                           idstr = pool_tmpappend(pool, idstr, pool_dep2str(pool, mrd->evr), ")");
745                         }
746                       else if (mrd->flags >= 8)
747                         continue;
748                     }
749                   if (!idstr)
750                     {
751                       /* must be and chain */
752                       idstr = pool_dep2str(pool, rd->evr);
753                       for (;;)
754                         {
755                           id = rd->name;
756                           if (!ISRELDEP(id))
757                             break;
758                           rd = GETRELDEP(pool, id);
759                           if (rd->flags != REL_AND)
760                             break;
761                           idstr = pool_tmpjoin(pool, pool_dep2str(pool, rd->evr), ":", idstr);
762                         }
763                       idstr = pool_tmpjoin(pool, pool_dep2str(pool, id), ":", idstr);
764                       idstr = pool_tmpjoin(pool, "packageand(", idstr, ")");
765                     }
766                 }
767               else if (rd->flags >= 8)
768                 continue;
769             }
770         }
771       if (!tagwritten)
772         {
773           fprintf(fp, "+%s\n", tag);
774           tagwritten = 1;
775         }
776       fprintf(fp, "%s\n", idstr);
777     }
778   if (key == SOLVABLE_PROVIDES)
779     {
780       /* add the filelist */
781       Dataiterator di;
782       dataiterator_init(&di, pool, repo, s - pool->solvables, SOLVABLE_FILELIST, 0, 0);
783       while (dataiterator_step(&di))
784         {
785           const char *s = repodata_dir2str(di.data, di.kv.id, di.kv.str);
786           if (prvdp)
787             {
788               Id id = pool_str2id(pool, s, 0);
789               if (id)
790                 {
791                   for (dp = prvdp; *dp; dp++)
792                     if (*dp == id)
793                       break;
794                   if (*dp)
795                     continue;   /* already included */
796                 }
797             }
798           if (!tagwritten)
799             {
800               fprintf(fp, "+%s", tag);
801               tagwritten = 1;
802             }
803           fprintf(fp, "%s\n", s);
804         }
805     }
806   if (tagwritten)
807     fprintf(fp, "-%s\n", tag);
808 }
809
810 int
811 testcase_write_susetags(Repo *repo, FILE *fp)
812 {
813   Pool *pool = repo->pool;
814   Solvable *s;
815   Id p;
816   const char *name;
817   const char *evr;
818   const char *arch;
819   const char *release;
820   const char *tmp;
821   unsigned int ti;
822
823   fprintf(fp, "=Ver: 2.0\n");
824   FOR_REPO_SOLVABLES(repo, p, s)
825     {
826       name = pool_id2str(pool, s->name);
827       evr = pool_id2str(pool, s->evr);
828       arch = pool_id2str(pool, s->arch);
829       release = strrchr(evr, '-');
830       if (!release)
831         release = evr + strlen(evr);
832       fprintf(fp, "=Pkg: %s %.*s %s %s\n", name, release - evr, evr, *release && release[1] ? release + 1 : "-", arch);
833       tmp = solvable_lookup_str(s, SOLVABLE_SUMMARY);
834       if (tmp)
835         fprintf(fp, "=Sum: %s\n", tmp);
836       writedeps(repo, fp, "Req:", SOLVABLE_REQUIRES, s, s->requires);
837       writedeps(repo, fp, "Prv:", SOLVABLE_PROVIDES, s, s->provides);
838       writedeps(repo, fp, "Obs:", SOLVABLE_OBSOLETES, s, s->obsoletes);
839       writedeps(repo, fp, "Con:", SOLVABLE_CONFLICTS, s, s->conflicts);
840       writedeps(repo, fp, "Rec:", SOLVABLE_RECOMMENDS, s, s->recommends);
841       writedeps(repo, fp, "Sup:", SOLVABLE_SUPPLEMENTS, s, s->supplements);
842       writedeps(repo, fp, "Sug:", SOLVABLE_SUGGESTS, s, s->suggests);
843       writedeps(repo, fp, "Enh:", SOLVABLE_ENHANCES, s, s->enhances);
844       if (s->vendor)
845         fprintf(fp, "=Vnd: %s\n", pool_id2str(pool, s->vendor));
846       ti = solvable_lookup_num(s, SOLVABLE_BUILDTIME, 0);
847       if (ti)
848         fprintf(fp, "=Tim: %u\n", ti);
849     }
850   return 0;
851 }
852
853 #ifndef ENABLE_SUSEREPO
854
855 static inline Offset
856 adddep(Repo *repo, Offset olddeps, char *str, Id marker)
857 {
858   Id id = *str == '/' ? pool_str2id(repo->pool, str, 1) : testcase_str2dep(repo->pool, str);
859   return repo_addid_dep(repo, olddeps, id, marker);
860 }
861
862 static void
863 finish_solvable(Pool *pool, Repodata *data, Solvable *s, char *filelist, int nfilelist)
864 {
865   if (nfilelist)
866     {
867       int l;
868       Id did; 
869       for (l = 0; l < nfilelist; l += strlen(filelist + l) + 1) 
870         {
871           char *p = strrchr(filelist + l, '/');
872           if (!p) 
873             continue;
874           *p++ = 0; 
875           did = repodata_str2dir(data, filelist + l, 1);
876           p[-1] = '/'; 
877           if (!did)
878             did = repodata_str2dir(data, "/", 1);
879           repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, p);
880         }
881     }
882   if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
883     s->provides = repo_addid_dep(s->repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
884   s->supplements = repo_fix_supplements(s->repo, s->provides, s->supplements, 0);
885   s->conflicts = repo_fix_conflicts(s->repo, s->conflicts);
886 }
887
888 /* stripped down version of susetags parser used for testcases */
889 int
890 testcase_add_susetags(Repo *repo, FILE *fp, int flags)
891 {
892   Pool *pool = repo->pool;
893   char *line, *linep;
894   int aline;
895   int tag;
896   Repodata *data;
897   Solvable *s;
898   char *sp[5];
899   unsigned int t;
900   int intag;
901   char *filelist = 0;
902   int afilelist = 0;
903   int nfilelist = 0;
904
905   data = repo_add_repodata(repo, flags);
906   s = 0;
907   intag = 0;
908
909   aline = 1024;
910   line = solv_malloc(aline);
911   linep = line;
912   for (;;)
913     {
914       if (linep - line + 16 > aline)
915         {
916           aline = linep - line;
917           line = solv_realloc(line, aline + 512);
918           linep = line + aline;
919           aline += 512;
920         }
921       if (!fgets(linep, aline - (linep - line), fp))
922         break;
923       linep += strlen(linep);
924       if (linep == line || linep[-1] != '\n')
925         continue;
926       *--linep = 0;
927       linep = line + intag;
928       if (intag)
929         {
930           if (line[intag] == '-' && !strncmp(line + 1, line + intag + 1, intag - 2))
931             {
932               intag = 0;
933               linep = line;
934               continue;
935             }
936         }
937       else if (line[0] == '+' && line[1] && line[1] != ':')
938         {
939           char *tagend = strchr(line, ':');
940           if (!tagend)
941             continue;
942           line[0] = '=';
943           tagend[1] = ' ';
944           intag = tagend + 2 - line;
945           linep = line + intag;
946           continue;
947         }
948       if (*line != '=' || !line[1] || !line[2] || !line[3] || line[4] != ':')
949         continue;
950       tag = line[1] << 16 | line[2] << 8 | line[3];
951       switch(tag)
952         {
953         case 'P' << 16 | 'k' << 8 | 'g':
954           if (s)
955             finish_solvable(pool, data, s, filelist, nfilelist);
956           nfilelist = 0;
957           if (split(line + 5, sp, 5) != 4)
958             break;
959           s = pool_id2solvable(pool, repo_add_solvable(repo));
960           s->name = pool_str2id(pool, sp[0], 1);
961           /* join back version and release */
962           if (sp[2] && !(sp[2][0] == '-' && !sp[2][1]))
963             sp[2][-1] = '-';
964           s->evr = makeevr(pool, sp[1]);
965           s->arch = pool_str2id(pool, sp[3], 1);
966           break;
967         case 'S' << 16 | 'u' << 8 | 'm':
968           repodata_set_str(data, s - pool->solvables, SOLVABLE_SUMMARY, line + 6);
969           break;
970         case 'V' << 16 | 'n' << 8 | 'd':
971           s->vendor = pool_str2id(pool, line + 6, 1);
972           break;
973         case 'T' << 16 | 'i' << 8 | 'm':
974           t = atoi(line + 6);
975           if (t)
976             repodata_set_num(data, s - pool->solvables, SOLVABLE_BUILDTIME, t);
977           break;
978         case 'R' << 16 | 'e' << 8 | 'q':
979           s->requires = adddep(repo, s->requires, line + 6, -SOLVABLE_PREREQMARKER);
980           break;
981         case 'P' << 16 | 'r' << 8 | 'q':
982           s->requires = adddep(repo, s->requires, line + 6, SOLVABLE_PREREQMARKER);
983           break;
984         case 'P' << 16 | 'r' << 8 | 'v':
985           if (line[6] == '/')
986             {
987               int l = strlen(line + 6) + 1;
988               if (nfilelist + l > afilelist)
989                 {
990                   afilelist = nfilelist + l + 512;
991                   filelist = solv_realloc(filelist, afilelist);
992                 }
993               memcpy(filelist + nfilelist, line + 6, l);
994               nfilelist += l;
995               break;
996             }
997           if (nfilelist)
998             {
999               int l;
1000               for (l = 0; l < nfilelist; l += strlen(filelist + l) + 1)
1001                 s->provides = repo_addid_dep(repo, s->provides, pool_str2id(pool, filelist + l, 1), 0);
1002               nfilelist = 0;
1003             }
1004           s->provides = adddep(repo, s->provides, line + 6, 0);
1005           break;
1006         case 'O' << 16 | 'b' << 8 | 's':
1007           s->obsoletes = adddep(repo, s->obsoletes, line + 6, 0);
1008           break;
1009         case 'C' << 16 | 'o' << 8 | 'n':
1010           s->conflicts = adddep(repo, s->conflicts, line + 6, 0);
1011           break;
1012         case 'R' << 16 | 'e' << 8 | 'c':
1013           s->recommends = adddep(repo, s->recommends, line + 6, 0);
1014           break;
1015         case 'S' << 16 | 'u' << 8 | 'p':
1016           s->supplements = adddep(repo, s->supplements, line + 6, 0);
1017           break;
1018         case 'S' << 16 | 'u' << 8 | 'g':
1019           s->suggests = adddep(repo, s->suggests, line + 6, 0);
1020           break;
1021         case 'E' << 16 | 'n' << 8 | 'h':
1022           s->enhances = adddep(repo, s->enhances, line + 6, 0);
1023           break;
1024         default:
1025           break;
1026         }
1027     }
1028   if (s)
1029     finish_solvable(pool, data, s, filelist, nfilelist);
1030   solv_free(line);
1031   solv_free(filelist);
1032   repodata_free_dircache(data);
1033   if (!(flags & REPO_NO_INTERNALIZE))
1034     repodata_internalize(data);
1035   return 0;
1036 }
1037
1038 #else
1039
1040 int
1041 testcase_add_susetags(Repo *repo, FILE *fp, int flags)
1042 {
1043   return repo_add_susetags(repo, fp, 0, 0, flags);
1044 }
1045
1046 #endif
1047
1048 const char *
1049 testcase_getsolverflags(Solver *solv)
1050 {
1051   Pool *pool = solv->pool;
1052   const char *str = 0;
1053   int i, v;
1054   for (i = 0; solverflags2str[i].str; i++)
1055     {
1056       v = solver_get_flag(solv, solverflags2str[i].flag);
1057       if (v == solverflags2str[i].def)
1058         continue;
1059       str = pool_tmpappend(pool, str, v ? " " : " !", solverflags2str[i].str);
1060     }
1061   return str ? str + 1 : "";
1062 }
1063
1064 int
1065 testcase_setsolverflags(Solver *solv, const char *str)
1066 {
1067   const char *p = str, *s;
1068   int i, v;
1069   for (;;)
1070     {
1071       while (*p == ' ' || *p == '\t' || *p == ',')
1072         p++;
1073       v = 1;
1074       if (*p == '!')
1075         {
1076           p++;
1077           v = 0;
1078         }
1079       if (!*p)
1080         break;
1081       s = p;
1082       while (*p && *p != ' ' && *p != '\t' && *p != ',')
1083         p++;
1084       for (i = 0; solverflags2str[i].str; i++)
1085         if (!strncmp(solverflags2str[i].str, s, p - s) && solverflags2str[i].str[p - s] == 0)
1086           break;
1087       if (!solverflags2str[i].str)
1088         {
1089           pool_debug(solv->pool, SOLV_ERROR, "setsolverflags: unknown flag '%.*s'\n", p - s, s);
1090           return 0;
1091         }
1092       solver_set_flag(solv, solverflags2str[i].flag, v);
1093     }
1094   return 1;
1095 }
1096
1097 void
1098 testcase_resetsolverflags(Solver *solv)
1099 {
1100   int i;
1101   for (i = 0; solverflags2str[i].str; i++)
1102     solver_set_flag(solv, solverflags2str[i].flag, solverflags2str[i].def);
1103 }
1104
1105 static const char *
1106 testcase_ruleid(Solver *solv, Id rid)
1107 {
1108   Strqueue sq;
1109   Queue q;
1110   int i;
1111   void *chk;
1112   const unsigned char *md5;
1113   int md5l;
1114   const char *s;
1115
1116   queue_init(&q);
1117   strqueue_init(&sq);
1118   solver_ruleliterals(solv, rid, &q);
1119   for (i = 0; i < q.count; i++)
1120     {
1121       Id p = q.elements[i];
1122       s = testcase_solvid2str(solv->pool, p > 0 ? p : -p);
1123       if (p < 0)
1124         s = pool_tmpjoin(solv->pool, "!", s, 0);
1125       strqueue_push(&sq, s);
1126     }
1127   queue_free(&q);
1128   strqueue_sort_u(&sq);
1129   chk = solv_chksum_create(REPOKEY_TYPE_MD5);
1130   for (i = 0; i < sq.nstr; i++)
1131     solv_chksum_add(chk, sq.str[i], strlen(sq.str[i]) + 1);
1132   md5 = solv_chksum_get(chk, &md5l);
1133   s = pool_bin2hex(solv->pool, md5, md5l);
1134   chk = solv_chksum_free(chk, 0);
1135   strqueue_free(&sq);
1136   return s;
1137 }
1138
1139 static const char *
1140 testcase_problemid(Solver *solv, Id problem)
1141 {
1142   Strqueue sq;
1143   Queue q;
1144   void *chk;
1145   const unsigned char *md5;
1146   int i, md5l;
1147   const char *s;
1148
1149   /* we build a hash of all rules that define the problem */
1150   queue_init(&q);
1151   strqueue_init(&sq);
1152   solver_findallproblemrules(solv, problem, &q);
1153   for (i = 0; i < q.count; i++)
1154     strqueue_push(&sq, testcase_ruleid(solv, q.elements[i]));
1155   queue_free(&q);
1156   strqueue_sort_u(&sq);
1157   chk = solv_chksum_create(REPOKEY_TYPE_MD5);
1158   for (i = 0; i < sq.nstr; i++)
1159     solv_chksum_add(chk, sq.str[i], strlen(sq.str[i]) + 1);
1160   md5 = solv_chksum_get(chk, &md5l);
1161   s = pool_bin2hex(solv->pool, md5, 4);
1162   chk = solv_chksum_free(chk, 0);
1163   strqueue_free(&sq);
1164   return s;
1165 }
1166
1167 static const char *
1168 testcase_solutionid(Solver *solv, Id problem, Id solution)
1169 {
1170   Id intid;
1171   void *chk;
1172   const unsigned char *md5;
1173   int md5l;
1174   const char *s;
1175
1176   intid = solver_solutionelement_internalid(solv, problem, solution);
1177   /* internal stuff! handle with care! */
1178   if (intid < 0)
1179     {
1180       /* it's a job */
1181       s = testcase_job2str(solv->pool, solv->job.elements[-intid - 1], solv->job.elements[-intid]);
1182     }
1183   else
1184     {
1185       /* it's a rule */
1186       s = testcase_ruleid(solv, intid);
1187     }
1188   chk = solv_chksum_create(REPOKEY_TYPE_MD5);
1189   solv_chksum_add(chk, s, strlen(s) + 1);
1190   md5 = solv_chksum_get(chk, &md5l);
1191   s = pool_bin2hex(solv->pool, md5, 4);
1192   chk = solv_chksum_free(chk, 0);
1193   return s;
1194 }
1195
1196 static struct class2str {
1197   Id class;
1198   const char *str;
1199 } class2str[] = {
1200   { SOLVER_TRANSACTION_ERASE,          "erase" },
1201   { SOLVER_TRANSACTION_INSTALL,        "install" },
1202   { SOLVER_TRANSACTION_REINSTALLED,    "reinstall" },
1203   { SOLVER_TRANSACTION_DOWNGRADED,     "downgrade" },
1204   { SOLVER_TRANSACTION_CHANGED,        "change" },
1205   { SOLVER_TRANSACTION_UPGRADED,       "upgrade" },
1206   { SOLVER_TRANSACTION_OBSOLETED,      "obsolete" },
1207   { SOLVER_TRANSACTION_MULTIINSTALL,   "multiinstall" },
1208   { SOLVER_TRANSACTION_MULTIREINSTALL, "multireinstall" },
1209   { 0, 0 }
1210 };
1211
1212 char *
1213 testcase_solverresult(Solver *solv, int resultflags)
1214 {
1215   Pool *pool = solv->pool;
1216   int i, j;
1217   Id p, op;
1218   const char *s;
1219   char *result;
1220   Strqueue sq;
1221
1222   strqueue_init(&sq);
1223   if ((resultflags & TESTCASE_RESULT_TRANSACTION) != 0)
1224     {
1225       Transaction *trans = solver_create_transaction(solv);
1226       Queue q;
1227
1228       queue_init(&q);
1229       for (i = 0; class2str[i].str; i++)
1230         {
1231           queue_empty(&q);
1232           transaction_classify_pkgs(trans, 0, class2str[i].class, 0, 0, &q);
1233           for (j = 0; j < q.count; j++)
1234             {
1235               p = q.elements[j];
1236               op = 0;
1237               if (pool->installed && pool->solvables[p].repo == pool->installed)
1238                 op = transaction_obs_pkg(trans, p);
1239               s = pool_tmpjoin(pool, class2str[i].str, " ", testcase_solvid2str(pool, p));
1240               if (op)
1241                 s = pool_tmpjoin(pool, s, " ", testcase_solvid2str(pool, op));
1242               strqueue_push(&sq, s);
1243             }
1244         }
1245       queue_free(&q);
1246       transaction_free(trans);
1247     }
1248   if ((resultflags & TESTCASE_RESULT_PROBLEMS) != 0)
1249     {
1250       char *probprefix, *solprefix;
1251       int problem, solution, element;
1252       int pcnt, scnt;
1253
1254       pcnt = solver_problem_count(solv);
1255       for (problem = 1; problem <= pcnt; problem++)
1256         {
1257           Id rid, from, to, dep;
1258           SolverRuleinfo rinfo;
1259           rid = solver_findproblemrule(solv, problem);
1260           s = testcase_problemid(solv, problem);
1261           probprefix = solv_dupjoin("problem ", s, 0);
1262           rinfo = solver_ruleinfo(solv, rid, &from, &to, &dep);
1263           s = pool_tmpjoin(pool, probprefix, " info ", solver_problemruleinfo2str(solv, rinfo, from, to, dep));
1264           strqueue_push(&sq, s);
1265           scnt = solver_solution_count(solv, problem);
1266           for (solution = 1; solution <= scnt; solution++)
1267             {
1268               s = testcase_solutionid(solv, problem, solution);
1269               solprefix = solv_dupjoin(probprefix, " solution ", s);
1270               element = 0;
1271               while ((element = solver_next_solutionelement(solv, problem, solution, element, &p, &op)) != 0)
1272                 {
1273                   if (p == SOLVER_SOLUTION_JOB)
1274                     s = pool_tmpjoin(pool, solprefix, " deljob ", testcase_job2str(pool, solv->job.elements[op - 1], solv->job.elements[op]));
1275                   else if (p > 0 && op == 0)
1276                     s = pool_tmpjoin(pool, solprefix, " erase ", testcase_solvid2str(pool, p));
1277                   else if (p > 0 && op > 0)
1278                     {
1279                       s = pool_tmpjoin(pool, solprefix, " replace ", testcase_solvid2str(pool, p));
1280                       s = pool_tmpappend(pool, s, " ", testcase_solvid2str(pool, op));
1281                     }
1282                   else if (p < 0 && op > 0)
1283                     s = pool_tmpjoin(pool, solprefix, " allow ", testcase_solvid2str(pool, op));
1284                   else
1285                     s = pool_tmpjoin(pool, solprefix, " unknown", 0);
1286                   strqueue_push(&sq, s);
1287                 }
1288               solv_free(solprefix);
1289             }
1290           solv_free(probprefix);
1291         }
1292     }
1293
1294   if ((resultflags & TESTCASE_RESULT_ORPHANED) != 0)
1295     {
1296       Queue q;
1297
1298       queue_init(&q);
1299       solver_get_orphaned(solv, &q);
1300       for (i = 0; i < q.count; i++)
1301         {
1302           s = pool_tmpjoin(pool, "orphaned ", testcase_solvid2str(pool, q.elements[i]), 0);
1303           strqueue_push(&sq, s);
1304         }
1305       queue_free(&q);
1306     }
1307
1308   if ((resultflags & TESTCASE_RESULT_RECOMMENDED) != 0)
1309     {
1310       Queue qr, qs;
1311
1312       queue_init(&qr);
1313       queue_init(&qs);
1314       solver_get_recommendations(solv, &qr, &qs, 0);
1315       for (i = 0; i < qr.count; i++)
1316         {
1317           s = pool_tmpjoin(pool, "recommended ", testcase_solvid2str(pool, qr.elements[i]), 0);
1318           strqueue_push(&sq, s);
1319         }
1320       for (i = 0; i < qs.count; i++)
1321         {
1322           s = pool_tmpjoin(pool, "suggested ", testcase_solvid2str(pool, qs.elements[i]), 0);
1323           strqueue_push(&sq, s);
1324         }
1325       queue_free(&qr);
1326       queue_free(&qs);
1327     }
1328
1329   strqueue_sort(&sq);
1330   result = strqueue_join(&sq);
1331   strqueue_free(&sq);
1332   return result;
1333 }
1334
1335
1336 int
1337 testcase_write(Solver *solv, char *dir, int resultflags)
1338 {
1339   Pool *pool = solv->pool;
1340   Repo *repo;
1341   int i;
1342   Id arch, repoid;
1343   Id lowscore;
1344   FILE *fp;
1345   Strqueue sq;
1346   char *cmd, *out;
1347   const char *s;
1348
1349   if (mkdir(dir, 0777) && errno != EEXIST)
1350     {
1351       pool_debug(solv->pool, SOLV_ERROR, "testcase_write: could not create directory '%s'\n", dir);
1352       return 0;
1353     }
1354   strqueue_init(&sq);
1355   FOR_REPOS(repoid, repo)
1356     {
1357       const char *name = testcase_repoid2str(pool, repoid);
1358       char priobuf[50];
1359       if (repo->subpriority)
1360         sprintf(priobuf, "%d.%d", repo->priority, repo->subpriority);
1361       else
1362         sprintf(priobuf, "%d", repo->priority);
1363       out = pool_tmpjoin(pool, name, ".repo", ".gz");
1364       cmd = pool_tmpjoin(pool, "repo ", name, " ");
1365       cmd = pool_tmpappend(pool, cmd, priobuf, " ");
1366       cmd = pool_tmpappend(pool, cmd, "susetags ", out);
1367       strqueue_push(&sq, cmd);
1368       out = pool_tmpjoin(pool, dir, "/", out);
1369       if (!(fp = solv_xfopen(out, "w")))
1370         {
1371           pool_debug(solv->pool, SOLV_ERROR, "testcase_write: could not open '%s' for writing\n", out);
1372           strqueue_free(&sq);
1373           return 0;
1374         }
1375       testcase_write_susetags(repo, fp);
1376       if (fclose(fp))
1377         {
1378           pool_debug(solv->pool, SOLV_ERROR, "testcase_write: write error\n");
1379           strqueue_free(&sq);
1380           return 0;
1381         }
1382     }
1383   /* hmm, this is not optimal... we currently search for the lowest score */
1384   lowscore = 0;
1385   arch = ARCH_NOARCH;
1386   for (i = 0; i < pool->lastarch; i++)
1387     {
1388       if (pool->id2arch[i] == 1 && !lowscore)
1389         arch = i;
1390       if (pool->id2arch[i] > 0x10000 && (!lowscore || pool->id2arch[i] < lowscore))
1391         {
1392           arch = i;
1393           lowscore = pool->id2arch[i];
1394         }
1395     }
1396   cmd = pool_tmpjoin(pool, "system ", pool_id2str(pool, arch), pool->disttype == DISTTYPE_DEB ? " deb" : " rpm");
1397   if (pool->installed)
1398     cmd = pool_tmpappend(pool, cmd, " ", testcase_repoid2str(pool, pool->installed->repoid));
1399   strqueue_push(&sq, cmd);
1400
1401   if (pool->vendorclasses)
1402     {
1403       cmd = 0;
1404       for (i = 0; pool->vendorclasses[i]; i++)
1405         {
1406           cmd = pool_tmpappend(pool, cmd ? cmd : "vendorclass", " ", pool->vendorclasses[i]);
1407           if (!pool->vendorclasses[i + 1])
1408             {
1409               strqueue_push(&sq, cmd);
1410               cmd = 0;
1411               i++;
1412             }
1413         }
1414     }
1415
1416   s = testcase_getsolverflags(solv);
1417   if (*s)
1418     {
1419       cmd = pool_tmpjoin(pool, "solverflags ", s, 0);
1420       strqueue_push(&sq, cmd);
1421     }
1422
1423   /* now dump all the ns callback values we know */
1424   if (pool->nscallback)
1425     {
1426       Id rid;
1427       int d;
1428       for (rid = 1; rid < pool->nrels; rid++)
1429         {
1430           Reldep *rd = pool->rels + rid;
1431           if (rd->flags != REL_NAMESPACE || rd->name == NAMESPACE_OTHERPROVIDERS)
1432             continue;
1433           /* check if we evaluated it, also skip empty results */
1434           if (!(d = pool->whatprovides_rel[rid]) || !pool->whatprovidesdata[d])
1435             continue;
1436           cmd = pool_tmpjoin(pool, "namespace ", pool_id2str(pool, rd->name), "(");
1437           cmd = pool_tmpappend(pool, cmd, pool_id2str(pool, rd->evr), ")");
1438           for (;  pool->whatprovidesdata[d]; d++)
1439             cmd = pool_tmpappend(pool, cmd, " ", testcase_solvid2str(pool, pool->whatprovidesdata[d]));
1440           strqueue_push(&sq, cmd);
1441         }
1442     }
1443
1444   for (i = 0; i < solv->job.count; i += 2)
1445     {
1446       cmd = (char *)testcase_job2str(pool, solv->job.elements[i], solv->job.elements[i + 1]);
1447       cmd = pool_tmpjoin(pool, "job ", cmd, 0);
1448       strqueue_push(&sq, cmd);
1449     }
1450
1451   if (resultflags)
1452     {
1453       char *result;
1454       out = pool_tmpjoin(pool, dir, "/", "solver.result");
1455       if (!(fp = fopen(out, "w")))
1456         {
1457           pool_debug(solv->pool, SOLV_ERROR, "testcase_write: could not open '%s' for writing\n", out);
1458           strqueue_free(&sq);
1459           return 0;
1460         }
1461       result = testcase_solverresult(solv, resultflags);
1462       if (fwrite(result, strlen(result), 1, fp) != 1)
1463         {
1464           pool_debug(solv->pool, SOLV_ERROR, "testcase_write: write error\n");
1465           solv_free(result);
1466           strqueue_free(&sq);
1467           return 0;
1468         }
1469       solv_free(result);
1470       if (fclose(fp))
1471         {
1472           pool_debug(solv->pool, SOLV_ERROR, "testcase_write: write error\n");
1473           strqueue_free(&sq);
1474           return 0;
1475         }
1476       cmd = 0;
1477       for (i = 0; resultflags2str[i].str; i++)
1478         if ((resultflags & resultflags2str[i].flag) != 0)
1479           cmd = pool_tmpappend(pool, cmd, cmd ? "," : 0, resultflags2str[i].str);
1480       cmd = pool_tmpjoin(pool, "result ", cmd ? cmd : "?", " solver.result");
1481       strqueue_push(&sq, cmd);
1482     }
1483
1484   cmd = strqueue_join(&sq);
1485   out = pool_tmpjoin(pool, dir, "/", "testcase.t");
1486   if (!(fp = fopen(out, "w")))
1487     {
1488       pool_debug(solv->pool, SOLV_ERROR, "testcase_write: could not open '%s' for writing\n", out);
1489       strqueue_free(&sq);
1490       return 0;
1491     }
1492   if (fwrite(cmd, strlen(cmd), 1, fp) != 1)
1493     {
1494       pool_debug(solv->pool, SOLV_ERROR, "testcase_write: write error\n");
1495       strqueue_free(&sq);
1496       return 0;
1497     }
1498   if (fclose(fp))
1499     {
1500       pool_debug(solv->pool, SOLV_ERROR, "testcase_write: write error\n");
1501       strqueue_free(&sq);
1502       return 0;
1503     }
1504   solv_free(cmd);
1505   strqueue_free(&sq);
1506   return 1;
1507 }
1508
1509 Solver *
1510 testcase_read(Pool *pool, FILE *fp, char *testcase, Queue *job, char **resultp, int *resultflagsp)
1511 {
1512   Solver *solv;
1513   char *buf, *bufp;
1514   int bufl;
1515   char *testcasedir, *s;
1516   int l;
1517   char **pieces = 0;
1518   int npieces = 0;
1519   int prepared = 0;
1520   int closefp = !fp;
1521
1522   if (!fp && !(fp = fopen(testcase, "r")))
1523     {
1524       pool_debug(pool, SOLV_ERROR, "testcase_read: could not open '%s'\n", testcase);
1525       return 0;
1526     }
1527   testcasedir = solv_strdup(testcase);
1528   if ((s = strchr(testcasedir, '/')) != 0)
1529     s[1] = 0;
1530   else
1531     *testcasedir = 0;
1532   bufl = 1024;
1533   buf = solv_malloc(bufl);
1534   bufp = buf;
1535   solv = 0;
1536   for (;;)
1537     {
1538       if (bufp - buf + 16 > bufl)
1539         {
1540           bufl = bufp - buf;
1541           buf = solv_realloc(buf, bufl + 512);
1542           bufp = buf + bufl;
1543           bufl += 512;
1544         }
1545       if (!fgets(bufp, bufl - (bufp - buf), fp))
1546         break;
1547       bufp = buf;
1548       l = strlen(buf);
1549       if (!l || buf[l - 1] != '\n')
1550         {
1551           bufp += l;
1552           continue;
1553         }
1554       buf[--l] = 0;
1555       s = buf;
1556       while (*s && (*s == ' ' || *s == '\t'))
1557         s++;
1558       if (!*s || *s == '#')
1559         continue;
1560       npieces = 0;
1561       /* split it in pieces */
1562       for (;;)
1563         {
1564           while (*s == ' ' || *s == '\t')
1565             s++;
1566           if (!*s)
1567             break;
1568           pieces = solv_extend(pieces, npieces, 1, sizeof(*pieces), 7);
1569           pieces[npieces++] = s;
1570           while (*s && *s != ' ' && *s != '\t')
1571             s++;
1572           if (*s)
1573             *s++ = 0;
1574         }
1575       pieces = solv_extend(pieces, npieces, 1, sizeof(*pieces), 7);
1576       pieces[npieces] = 0;
1577       if (!strcmp(pieces[0], "repo") && npieces >= 4)
1578         {
1579           Repo *repo = repo_create(pool, pieces[1]);
1580           FILE *rfp;
1581           int prio, subprio;
1582           const char *rdata;
1583
1584           prepared = 0;
1585           if (sscanf(pieces[2], "%d.%d", &prio, &subprio) != 2)
1586             {
1587               subprio = 0;
1588               prio = atoi(pieces[2]);
1589             }
1590           repo->priority = prio;
1591           repo->subpriority = subprio;
1592           rdata = pool_tmpjoin(pool, testcasedir, pieces[4], 0);
1593           if ((rfp = solv_xfopen(rdata, "r")) == 0)
1594             {
1595               pool_debug(pool, SOLV_ERROR, "testcase_read: could not open '%s'\n", rdata);
1596             }
1597           else if (!strcmp(pieces[3], "susetags"))
1598             {
1599               testcase_add_susetags(repo, rfp, 0);
1600               fclose(rfp);
1601             }
1602           else
1603             {
1604               fclose(rfp);
1605               pool_debug(pool, SOLV_ERROR, "testcase_read: unknown repo type for repo '%s'\n", repo->name);
1606             }
1607         }
1608       else if (!strcmp(pieces[0], "system") && npieces >= 3)
1609         {
1610           prepared = 0;
1611           pool_setarch(pool, pieces[1]);
1612           if (npieces > 3)
1613             {
1614               Repo *repo = testcase_str2repo(pool, pieces[3]);
1615               if (!repo)
1616                 pool_debug(pool, SOLV_ERROR, "testcase_read: system: unknown repo '%s'\n", pieces[3]);
1617               else
1618                 pool_set_installed(pool, repo);
1619             }
1620         }
1621       else if (!strcmp(pieces[0], "job") && npieces > 1)
1622         {
1623           char *sp;
1624           Id how, what;
1625           if (!prepared)
1626             {
1627               pool_addfileprovides(pool);
1628               pool_createwhatprovides(pool);
1629               prepared = 1;
1630             }
1631           /* rejoin */
1632           for (sp = pieces[1]; sp < pieces[npieces - 1]; sp++)
1633             if (*sp == 0)
1634               *sp = ' ';
1635           how = testcase_str2job(pool, pieces[1], &what);
1636           if (how >= 0 && job)
1637             queue_push2(job, how, what);
1638         }
1639       else if (!strcmp(pieces[0], "vendorclass") && npieces > 1)
1640         {
1641           pool_addvendorclass(pool, (const char **)(pieces + 1));
1642         }
1643       else if (!strcmp(pieces[0], "namespace") && npieces > 1)
1644         {
1645           int i = strlen(pieces[1]);
1646           s = strchr(pieces[1], '(');
1647           if (!s && pieces[1][i - 1] != ')')
1648             {
1649               pool_debug(pool, SOLV_ERROR, "testcase_read: bad namespace '%s'\n", pieces[1]);
1650             }
1651           else
1652             {
1653               Id name, evr, id;
1654               Queue q;
1655               queue_init(&q);
1656               *s = 0;
1657               pieces[1][i - 1] = 0;
1658               name = pool_str2id(pool, pieces[1], 1);
1659               evr = pool_str2id(pool, s + 1, 1);
1660               *s = '(';
1661               pieces[1][i - 1] = ')';
1662               id = pool_rel2id(pool, name, evr, REL_NAMESPACE, 1);
1663               for (i = 2; i < npieces; i++)
1664                 queue_push(&q, testcase_str2solvid(pool, pieces[i]));
1665               /* now do the callback */
1666               if (!prepared)
1667                 {
1668                   pool_addfileprovides(pool);
1669                   pool_createwhatprovides(pool);
1670                   prepared = 1;
1671                 }
1672               pool->whatprovides_rel[GETRELID(id)] = pool_queuetowhatprovides(pool, &q);
1673               queue_free(&q);
1674             }
1675         }
1676       else if (!strcmp(pieces[0], "solverflags") && npieces > 1)
1677         {
1678           int i;
1679           if (!solv)
1680             {
1681               solv = solver_create(pool);
1682               testcase_resetsolverflags(solv);
1683             }
1684           for (i = 1; i < npieces; i++)
1685             testcase_setsolverflags(solv, pieces[i]);
1686         }
1687       else if (!strcmp(pieces[0], "result") && npieces > 1)
1688         {
1689           FILE *rfp;
1690           const char *rdata;
1691           int resultflags = 0;
1692           char *s = pieces[1];
1693           int i;
1694           while (s)
1695             {
1696               char *se = strchr(s, ',');
1697               if (se)
1698                 *se++ = 0;
1699               for (i = 0; resultflags2str[i].str; i++)
1700                 if (!strcmp(s, resultflags2str[i].str))
1701                   {
1702                     resultflags |= resultflags2str[i].flag;
1703                     break;
1704                   }
1705               if (!resultflags2str[i].str)
1706                 pool_debug(pool, SOLV_ERROR, "result: unknown flag '%s'\n", s);
1707               s = se;
1708             }
1709
1710           rdata = pool_tmpjoin(pool, testcasedir, pieces[2], 0);
1711           if (!strcmp(pieces[1], "<inline>"))
1712             rfp = fp;
1713           else
1714             rfp = fopen(rdata, "r");
1715           if (!rfp)
1716             {
1717               pool_debug(pool, SOLV_ERROR, "testcase_read: could not open '%s'\n", rdata);
1718             }
1719           else
1720             {
1721               /* slurp it in... */
1722               char *result = solv_malloc(1024);
1723               char *rp = result;
1724               int resultl = 1024;
1725               for (;;)
1726                 {
1727                   size_t rl;
1728                   if (rp - result + 256 >= resultl)
1729                     {
1730                       resultl = rp - result;
1731                       result = solv_realloc(result, resultl + 1024);
1732                       rp = result + resultl;
1733                       resultl += 1024;
1734                     }
1735                   if (fp == rfp)
1736                     {
1737                       if (!fgets(rp, resultl - (rp - result), fp))
1738                         rl = 0;
1739                       else
1740                         {
1741                           rl = strlen(rp);
1742                           if (rl && (rp == result || rp[-1] == '\n'))
1743                             {
1744                               if (rl > 1 && rp[0] == '#' && rp[1] == '>')
1745                                 {
1746                                   memmove(rp, rp + 2, rl - 2);
1747                                   rl -= 2;
1748                                 }
1749                               else
1750                                 {
1751                                   while (rl + 16 > bufl)
1752                                     {
1753                                       buf = solv_realloc(buf, bufl + 512);
1754                                       bufl += 512;
1755                                     }
1756                                   memmove(buf, rp, rl);
1757                                   if (buf[rl - 1] == '\n')
1758                                     {
1759                                       ungetc('\n', fp);
1760                                       rl--;
1761                                     }
1762                                   bufp = buf + rl;
1763                                   rl = 0;
1764                                 }
1765                             }
1766                         }
1767                     }
1768                   else
1769                     rl = fread(rp, 1, resultl - (rp - result), rfp);
1770                   if (rl <= 0)
1771                     {
1772                       *rp = 0;
1773                       break;
1774                     }
1775                   rp += rl;
1776                 }
1777               if (rfp != fp)
1778                 fclose(rfp);
1779               if (resultp)
1780                 *resultp = result;
1781               else
1782                 solv_free(result);
1783               if (resultflagsp)
1784                 *resultflagsp = resultflags;
1785             }
1786         }
1787       else
1788         {
1789           pool_debug(pool, SOLV_ERROR, "testcase_read: cannot parse command '%s'\n", pieces[0]);
1790         }
1791     }
1792   buf = solv_free(buf);
1793   pieces = solv_free(pieces);
1794   solv_free(testcasedir);
1795   if (!prepared)
1796     {
1797       pool_addfileprovides(pool);
1798       pool_createwhatprovides(pool);
1799     }
1800   if (!solv)
1801     {
1802       solv = solver_create(pool);
1803       testcase_resetsolverflags(solv);
1804     }
1805   if (closefp)
1806     fclose(fp);
1807   return solv;
1808 }
1809
1810 char *
1811 testcase_resultdiff(char *result1, char *result2)
1812 {
1813   Strqueue sq1, sq2, osq;
1814   char *r;
1815   strqueue_init(&sq1);
1816   strqueue_init(&sq2);
1817   strqueue_init(&osq);
1818   strqueue_split(&sq1, result1);
1819   strqueue_split(&sq2, result2);
1820   strqueue_sort(&sq1);
1821   strqueue_sort(&sq2);
1822   strqueue_diff(&sq1, &sq2, &osq);
1823   r = osq.nstr ? strqueue_join(&osq) : 0;
1824   strqueue_free(&sq1);
1825   strqueue_free(&sq2);
1826   strqueue_free(&osq);
1827   return r;
1828 }
1829