- fix filelist handling in repo_susetags and testcase
[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   if (!(flags & REPO_NO_INTERNALIZE))
1033     repodata_internalize(data);
1034   return 0;
1035 }
1036
1037 #else
1038
1039 int
1040 testcase_add_susetags(Repo *repo, FILE *fp, int flags)
1041 {
1042   return repo_add_susetags(repo, fp, 0, 0, flags);
1043 }
1044
1045 #endif
1046
1047 const char *
1048 testcase_getsolverflags(Solver *solv)
1049 {
1050   Pool *pool = solv->pool;
1051   const char *str = 0;
1052   int i, v;
1053   for (i = 0; solverflags2str[i].str; i++)
1054     {
1055       v = solver_get_flag(solv, solverflags2str[i].flag);
1056       if (v == solverflags2str[i].def)
1057         continue;
1058       str = pool_tmpappend(pool, str, v ? " " : " !", solverflags2str[i].str);
1059     }
1060   return str ? str + 1 : "";
1061 }
1062
1063 int
1064 testcase_setsolverflags(Solver *solv, const char *str)
1065 {
1066   const char *p = str, *s;
1067   int i, v;
1068   for (;;)
1069     {
1070       while (*p == ' ' || *p == '\t' || *p == ',')
1071         p++;
1072       v = 1;
1073       if (*p == '!')
1074         {
1075           p++;
1076           v = 0;
1077         }
1078       if (!*p)
1079         break;
1080       s = p;
1081       while (*p && *p != ' ' && *p != '\t' && *p != ',')
1082         p++;
1083       for (i = 0; solverflags2str[i].str; i++)
1084         if (!strncmp(solverflags2str[i].str, s, p - s) && solverflags2str[i].str[p - s] == 0)
1085           break;
1086       if (!solverflags2str[i].str)
1087         {
1088           pool_debug(solv->pool, SOLV_ERROR, "setsolverflags: unknown flag '%.*s'\n", p - s, s);
1089           return 0;
1090         }
1091       solver_set_flag(solv, solverflags2str[i].flag, v);
1092     }
1093   return 1;
1094 }
1095
1096 void
1097 testcase_resetsolverflags(Solver *solv)
1098 {
1099   int i;
1100   for (i = 0; solverflags2str[i].str; i++)
1101     solver_set_flag(solv, solverflags2str[i].flag, solverflags2str[i].def);
1102 }
1103
1104 static const char *
1105 testcase_ruleid(Solver *solv, Id rid)
1106 {
1107   Strqueue sq;
1108   Queue q;
1109   int i;
1110   void *chk;
1111   const unsigned char *md5;
1112   int md5l;
1113   const char *s;
1114
1115   queue_init(&q);
1116   strqueue_init(&sq);
1117   solver_ruleliterals(solv, rid, &q);
1118   for (i = 0; i < q.count; i++)
1119     {
1120       Id p = q.elements[i];
1121       s = testcase_solvid2str(solv->pool, p > 0 ? p : -p);
1122       if (p < 0)
1123         s = pool_tmpjoin(solv->pool, "!", s, 0);
1124       strqueue_push(&sq, s);
1125     }
1126   queue_free(&q);
1127   strqueue_sort_u(&sq);
1128   chk = solv_chksum_create(REPOKEY_TYPE_MD5);
1129   for (i = 0; i < sq.nstr; i++)
1130     solv_chksum_add(chk, sq.str[i], strlen(sq.str[i]) + 1);
1131   md5 = solv_chksum_get(chk, &md5l);
1132   s = pool_bin2hex(solv->pool, md5, md5l);
1133   chk = solv_chksum_free(chk, 0);
1134   strqueue_free(&sq);
1135   return s;
1136 }
1137
1138 static const char *
1139 testcase_problemid(Solver *solv, Id problem)
1140 {
1141   Strqueue sq;
1142   Queue q;
1143   void *chk;
1144   const unsigned char *md5;
1145   int i, md5l;
1146   const char *s;
1147
1148   /* we build a hash of all rules that define the problem */
1149   queue_init(&q);
1150   strqueue_init(&sq);
1151   solver_findallproblemrules(solv, problem, &q);
1152   for (i = 0; i < q.count; i++)
1153     strqueue_push(&sq, testcase_ruleid(solv, q.elements[i]));
1154   queue_free(&q);
1155   strqueue_sort_u(&sq);
1156   chk = solv_chksum_create(REPOKEY_TYPE_MD5);
1157   for (i = 0; i < sq.nstr; i++)
1158     solv_chksum_add(chk, sq.str[i], strlen(sq.str[i]) + 1);
1159   md5 = solv_chksum_get(chk, &md5l);
1160   s = pool_bin2hex(solv->pool, md5, 4);
1161   chk = solv_chksum_free(chk, 0);
1162   strqueue_free(&sq);
1163   return s;
1164 }
1165
1166 static const char *
1167 testcase_solutionid(Solver *solv, Id problem, Id solution)
1168 {
1169   Id intid;
1170   void *chk;
1171   const unsigned char *md5;
1172   int md5l;
1173   const char *s;
1174
1175   intid = solver_solutionelement_internalid(solv, problem, solution);
1176   /* internal stuff! handle with care! */
1177   if (intid < 0)
1178     {
1179       /* it's a job */
1180       s = testcase_job2str(solv->pool, solv->job.elements[-intid - 1], solv->job.elements[-intid]);
1181     }
1182   else
1183     {
1184       /* it's a rule */
1185       s = testcase_ruleid(solv, intid);
1186     }
1187   chk = solv_chksum_create(REPOKEY_TYPE_MD5);
1188   solv_chksum_add(chk, s, strlen(s) + 1);
1189   md5 = solv_chksum_get(chk, &md5l);
1190   s = pool_bin2hex(solv->pool, md5, 4);
1191   chk = solv_chksum_free(chk, 0);
1192   return s;
1193 }
1194
1195 static struct class2str {
1196   Id class;
1197   const char *str;
1198 } class2str[] = {
1199   { SOLVER_TRANSACTION_ERASE,          "erase" },
1200   { SOLVER_TRANSACTION_INSTALL,        "install" },
1201   { SOLVER_TRANSACTION_REINSTALLED,    "reinstall" },
1202   { SOLVER_TRANSACTION_DOWNGRADED,     "downgrade" },
1203   { SOLVER_TRANSACTION_CHANGED,        "change" },
1204   { SOLVER_TRANSACTION_UPGRADED,       "upgrade" },
1205   { SOLVER_TRANSACTION_OBSOLETED,      "obsolete" },
1206   { SOLVER_TRANSACTION_MULTIINSTALL,   "multiinstall" },
1207   { SOLVER_TRANSACTION_MULTIREINSTALL, "multireinstall" },
1208   { 0, 0 }
1209 };
1210
1211 char *
1212 testcase_solverresult(Solver *solv, int resultflags)
1213 {
1214   Pool *pool = solv->pool;
1215   int i, j;
1216   Id p, op;
1217   const char *s;
1218   char *result;
1219   Strqueue sq;
1220
1221   strqueue_init(&sq);
1222   if ((resultflags & TESTCASE_RESULT_TRANSACTION) != 0)
1223     {
1224       Transaction *trans = solver_create_transaction(solv);
1225       Queue q;
1226
1227       queue_init(&q);
1228       for (i = 0; class2str[i].str; i++)
1229         {
1230           queue_empty(&q);
1231           transaction_classify_pkgs(trans, 0, class2str[i].class, 0, 0, &q);
1232           for (j = 0; j < q.count; j++)
1233             {
1234               p = q.elements[j];
1235               op = 0;
1236               if (pool->installed && pool->solvables[p].repo == pool->installed)
1237                 op = transaction_obs_pkg(trans, p);
1238               s = pool_tmpjoin(pool, class2str[i].str, " ", testcase_solvid2str(pool, p));
1239               if (op)
1240                 s = pool_tmpjoin(pool, s, " ", testcase_solvid2str(pool, op));
1241               strqueue_push(&sq, s);
1242             }
1243         }
1244       queue_free(&q);
1245       transaction_free(trans);
1246     }
1247   if ((resultflags & TESTCASE_RESULT_PROBLEMS) != 0)
1248     {
1249       char *probprefix, *solprefix;
1250       int problem, solution, element;
1251       int pcnt, scnt;
1252
1253       pcnt = solver_problem_count(solv);
1254       for (problem = 1; problem <= pcnt; problem++)
1255         {
1256           Id rid, from, to, dep;
1257           SolverRuleinfo rinfo;
1258           rid = solver_findproblemrule(solv, problem);
1259           s = testcase_problemid(solv, problem);
1260           probprefix = solv_dupjoin("problem ", s, 0);
1261           rinfo = solver_ruleinfo(solv, rid, &from, &to, &dep);
1262           s = pool_tmpjoin(pool, probprefix, " info ", solver_problemruleinfo2str(solv, rinfo, from, to, dep));
1263           strqueue_push(&sq, s);
1264           scnt = solver_solution_count(solv, problem);
1265           for (solution = 1; solution <= scnt; solution++)
1266             {
1267               s = testcase_solutionid(solv, problem, solution);
1268               solprefix = solv_dupjoin(probprefix, " solution ", s);
1269               element = 0;
1270               while ((element = solver_next_solutionelement(solv, problem, solution, element, &p, &op)) != 0)
1271                 {
1272                   if (p == SOLVER_SOLUTION_JOB)
1273                     s = pool_tmpjoin(pool, solprefix, " deljob ", testcase_job2str(pool, solv->job.elements[op - 1], solv->job.elements[op]));
1274                   else if (p > 0 && op == 0)
1275                     s = pool_tmpjoin(pool, solprefix, " erase ", testcase_solvid2str(pool, p));
1276                   else if (p > 0 && op > 0)
1277                     {
1278                       s = pool_tmpjoin(pool, solprefix, " replace ", testcase_solvid2str(pool, p));
1279                       s = pool_tmpappend(pool, s, " ", testcase_solvid2str(pool, op));
1280                     }
1281                   else if (p < 0 && op > 0)
1282                     s = pool_tmpjoin(pool, solprefix, " allow ", testcase_solvid2str(pool, op));
1283                   else
1284                     s = pool_tmpjoin(pool, solprefix, " unknown", 0);
1285                   strqueue_push(&sq, s);
1286                 }
1287               solv_free(solprefix);
1288             }
1289           solv_free(probprefix);
1290         }
1291     }
1292
1293   if ((resultflags & TESTCASE_RESULT_ORPHANED) != 0)
1294     {
1295       Queue q;
1296
1297       queue_init(&q);
1298       solver_get_orphaned(solv, &q);
1299       for (i = 0; i < q.count; i++)
1300         {
1301           s = pool_tmpjoin(pool, "orphaned ", testcase_solvid2str(pool, q.elements[i]), 0);
1302           strqueue_push(&sq, s);
1303         }
1304       queue_free(&q);
1305     }
1306
1307   if ((resultflags & TESTCASE_RESULT_RECOMMENDED) != 0)
1308     {
1309       Queue qr, qs;
1310
1311       queue_init(&qr);
1312       queue_init(&qs);
1313       solver_get_recommendations(solv, &qr, &qs, 0);
1314       for (i = 0; i < qr.count; i++)
1315         {
1316           s = pool_tmpjoin(pool, "recommended ", testcase_solvid2str(pool, qr.elements[i]), 0);
1317           strqueue_push(&sq, s);
1318         }
1319       for (i = 0; i < qs.count; i++)
1320         {
1321           s = pool_tmpjoin(pool, "suggested ", testcase_solvid2str(pool, qs.elements[i]), 0);
1322           strqueue_push(&sq, s);
1323         }
1324       queue_free(&qr);
1325       queue_free(&qs);
1326     }
1327
1328   strqueue_sort(&sq);
1329   result = strqueue_join(&sq);
1330   strqueue_free(&sq);
1331   return result;
1332 }
1333
1334
1335 int
1336 testcase_write(Solver *solv, char *dir, int resultflags)
1337 {
1338   Pool *pool = solv->pool;
1339   Repo *repo;
1340   int i;
1341   Id arch, repoid;
1342   Id lowscore;
1343   FILE *fp;
1344   Strqueue sq;
1345   char *cmd, *out;
1346   const char *s;
1347
1348   if (mkdir(dir, 0777) && errno != EEXIST)
1349     {
1350       pool_debug(solv->pool, SOLV_ERROR, "testcase_write: could not create directory '%s'\n", dir);
1351       return 0;
1352     }
1353   strqueue_init(&sq);
1354   FOR_REPOS(repoid, repo)
1355     {
1356       const char *name = testcase_repoid2str(pool, repoid);
1357       char priobuf[50];
1358       if (repo->subpriority)
1359         sprintf(priobuf, "%d.%d", repo->priority, repo->subpriority);
1360       else
1361         sprintf(priobuf, "%d", repo->priority);
1362       out = pool_tmpjoin(pool, name, ".repo", ".gz");
1363       cmd = pool_tmpjoin(pool, "repo ", name, " ");
1364       cmd = pool_tmpappend(pool, cmd, priobuf, " ");
1365       cmd = pool_tmpappend(pool, cmd, "susetags ", out);
1366       strqueue_push(&sq, cmd);
1367       out = pool_tmpjoin(pool, dir, "/", out);
1368       if (!(fp = solv_xfopen(out, "w")))
1369         {
1370           pool_debug(solv->pool, SOLV_ERROR, "testcase_write: could not open '%s' for writing\n", out);
1371           strqueue_free(&sq);
1372           return 0;
1373         }
1374       testcase_write_susetags(repo, fp);
1375       if (fclose(fp))
1376         {
1377           pool_debug(solv->pool, SOLV_ERROR, "testcase_write: write error\n");
1378           strqueue_free(&sq);
1379           return 0;
1380         }
1381     }
1382   /* hmm, this is not optimal... we currently search for the lowest score */
1383   lowscore = 0;
1384   arch = ARCH_NOARCH;
1385   for (i = 0; i < pool->lastarch; i++)
1386     {
1387       if (pool->id2arch[i] == 1 && !lowscore)
1388         arch = i;
1389       if (pool->id2arch[i] > 0x10000 && (!lowscore || pool->id2arch[i] < lowscore))
1390         {
1391           arch = i;
1392           lowscore = pool->id2arch[i];
1393         }
1394     }
1395   cmd = pool_tmpjoin(pool, "system ", pool_id2str(pool, arch), pool->disttype == DISTTYPE_DEB ? " deb" : " rpm");
1396   if (pool->installed)
1397     cmd = pool_tmpappend(pool, cmd, " ", testcase_repoid2str(pool, pool->installed->repoid));
1398   strqueue_push(&sq, cmd);
1399
1400   if (pool->vendorclasses)
1401     {
1402       cmd = 0;
1403       for (i = 0; pool->vendorclasses[i]; i++)
1404         {
1405           cmd = pool_tmpappend(pool, cmd ? cmd : "vendorclass", " ", pool->vendorclasses[i]);
1406           if (!pool->vendorclasses[i + 1])
1407             {
1408               strqueue_push(&sq, cmd);
1409               cmd = 0;
1410               i++;
1411             }
1412         }
1413     }
1414
1415   s = testcase_getsolverflags(solv);
1416   if (*s)
1417     {
1418       cmd = pool_tmpjoin(pool, "solverflags ", s, 0);
1419       strqueue_push(&sq, cmd);
1420     }
1421
1422   /* now dump all the ns callback values we know */
1423   if (pool->nscallback)
1424     {
1425       Id rid;
1426       int d;
1427       for (rid = 1; rid < pool->nrels; rid++)
1428         {
1429           Reldep *rd = pool->rels + rid;
1430           if (rd->flags != REL_NAMESPACE || rd->name == NAMESPACE_OTHERPROVIDERS)
1431             continue;
1432           /* check if we evaluated it, also skip empty results */
1433           if (!(d = pool->whatprovides_rel[rid]) || !pool->whatprovidesdata[d])
1434             continue;
1435           cmd = pool_tmpjoin(pool, "namespace ", pool_id2str(pool, rd->name), "(");
1436           cmd = pool_tmpappend(pool, cmd, pool_id2str(pool, rd->evr), ")");
1437           for (;  pool->whatprovidesdata[d]; d++)
1438             cmd = pool_tmpappend(pool, cmd, " ", testcase_solvid2str(pool, pool->whatprovidesdata[d]));
1439           strqueue_push(&sq, cmd);
1440         }
1441     }
1442
1443   for (i = 0; i < solv->job.count; i += 2)
1444     {
1445       cmd = (char *)testcase_job2str(pool, solv->job.elements[i], solv->job.elements[i + 1]);
1446       cmd = pool_tmpjoin(pool, "job ", cmd, 0);
1447       strqueue_push(&sq, cmd);
1448     }
1449
1450   if (resultflags)
1451     {
1452       char *result;
1453       out = pool_tmpjoin(pool, dir, "/", "solver.result");
1454       if (!(fp = fopen(out, "w")))
1455         {
1456           pool_debug(solv->pool, SOLV_ERROR, "testcase_write: could not open '%s' for writing\n", out);
1457           strqueue_free(&sq);
1458           return 0;
1459         }
1460       result = testcase_solverresult(solv, resultflags);
1461       if (fwrite(result, strlen(result), 1, fp) != 1)
1462         {
1463           pool_debug(solv->pool, SOLV_ERROR, "testcase_write: write error\n");
1464           solv_free(result);
1465           strqueue_free(&sq);
1466           return 0;
1467         }
1468       solv_free(result);
1469       if (fclose(fp))
1470         {
1471           pool_debug(solv->pool, SOLV_ERROR, "testcase_write: write error\n");
1472           strqueue_free(&sq);
1473           return 0;
1474         }
1475       cmd = 0;
1476       for (i = 0; resultflags2str[i].str; i++)
1477         if ((resultflags & resultflags2str[i].flag) != 0)
1478           cmd = pool_tmpappend(pool, cmd, cmd ? "," : 0, resultflags2str[i].str);
1479       cmd = pool_tmpjoin(pool, "result ", cmd ? cmd : "?", " solver.result");
1480       strqueue_push(&sq, cmd);
1481     }
1482
1483   cmd = strqueue_join(&sq);
1484   out = pool_tmpjoin(pool, dir, "/", "testcase.t");
1485   if (!(fp = fopen(out, "w")))
1486     {
1487       pool_debug(solv->pool, SOLV_ERROR, "testcase_write: could not open '%s' for writing\n", out);
1488       strqueue_free(&sq);
1489       return 0;
1490     }
1491   if (fwrite(cmd, strlen(cmd), 1, fp) != 1)
1492     {
1493       pool_debug(solv->pool, SOLV_ERROR, "testcase_write: write error\n");
1494       strqueue_free(&sq);
1495       return 0;
1496     }
1497   if (fclose(fp))
1498     {
1499       pool_debug(solv->pool, SOLV_ERROR, "testcase_write: write error\n");
1500       strqueue_free(&sq);
1501       return 0;
1502     }
1503   solv_free(cmd);
1504   strqueue_free(&sq);
1505   return 1;
1506 }
1507
1508 Solver *
1509 testcase_read(Pool *pool, FILE *fp, char *testcase, Queue *job, char **resultp, int *resultflagsp)
1510 {
1511   Solver *solv;
1512   char *buf, *bufp;
1513   int bufl;
1514   char *testcasedir, *s;
1515   int l;
1516   char **pieces = 0;
1517   int npieces = 0;
1518   int prepared = 0;
1519   int closefp = !fp;
1520
1521   if (!fp && !(fp = fopen(testcase, "r")))
1522     {
1523       pool_debug(pool, SOLV_ERROR, "testcase_read: could not open '%s'\n", testcase);
1524       return 0;
1525     }
1526   testcasedir = solv_strdup(testcase);
1527   if ((s = strchr(testcasedir, '/')) != 0)
1528     s[1] = 0;
1529   else
1530     *testcasedir = 0;
1531   bufl = 1024;
1532   buf = solv_malloc(bufl);
1533   bufp = buf;
1534   solv = 0;
1535   for (;;)
1536     {
1537       if (bufp - buf + 16 > bufl)
1538         {
1539           bufl = bufp - buf;
1540           buf = solv_realloc(buf, bufl + 512);
1541           bufp = buf + bufl;
1542           bufl += 512;
1543         }
1544       if (!fgets(bufp, bufl - (bufp - buf), fp))
1545         break;
1546       bufp = buf;
1547       l = strlen(buf);
1548       if (!l || buf[l - 1] != '\n')
1549         {
1550           bufp += l;
1551           continue;
1552         }
1553       buf[--l] = 0;
1554       s = buf;
1555       while (*s && (*s == ' ' || *s == '\t'))
1556         s++;
1557       if (!*s || *s == '#')
1558         continue;
1559       npieces = 0;
1560       /* split it in pieces */
1561       for (;;)
1562         {
1563           while (*s == ' ' || *s == '\t')
1564             s++;
1565           if (!*s)
1566             break;
1567           pieces = solv_extend(pieces, npieces, 1, sizeof(*pieces), 7);
1568           pieces[npieces++] = s;
1569           while (*s && *s != ' ' && *s != '\t')
1570             s++;
1571           if (*s)
1572             *s++ = 0;
1573         }
1574       pieces = solv_extend(pieces, npieces, 1, sizeof(*pieces), 7);
1575       pieces[npieces] = 0;
1576       if (!strcmp(pieces[0], "repo") && npieces >= 4)
1577         {
1578           Repo *repo = repo_create(pool, pieces[1]);
1579           FILE *rfp;
1580           int prio, subprio;
1581           const char *rdata;
1582
1583           prepared = 0;
1584           if (sscanf(pieces[2], "%d.%d", &prio, &subprio) != 2)
1585             {
1586               subprio = 0;
1587               prio = atoi(pieces[2]);
1588             }
1589           repo->priority = prio;
1590           repo->subpriority = subprio;
1591           rdata = pool_tmpjoin(pool, testcasedir, pieces[4], 0);
1592           if ((rfp = solv_xfopen(rdata, "r")) == 0)
1593             {
1594               pool_debug(pool, SOLV_ERROR, "testcase_read: could not open '%s'\n", rdata);
1595             }
1596           else if (!strcmp(pieces[3], "susetags"))
1597             {
1598               testcase_add_susetags(repo, rfp, 0);
1599               fclose(rfp);
1600             }
1601           else
1602             {
1603               fclose(rfp);
1604               pool_debug(pool, SOLV_ERROR, "testcase_read: unknown repo type for repo '%s'\n", repo->name);
1605             }
1606         }
1607       else if (!strcmp(pieces[0], "system") && npieces >= 3)
1608         {
1609           prepared = 0;
1610           pool_setarch(pool, pieces[1]);
1611           if (npieces > 3)
1612             {
1613               Repo *repo = testcase_str2repo(pool, pieces[3]);
1614               if (!repo)
1615                 pool_debug(pool, SOLV_ERROR, "testcase_read: system: unknown repo '%s'\n", pieces[3]);
1616               else
1617                 pool_set_installed(pool, repo);
1618             }
1619         }
1620       else if (!strcmp(pieces[0], "job") && npieces > 1)
1621         {
1622           char *sp;
1623           Id how, what;
1624           if (!prepared)
1625             {
1626               pool_addfileprovides(pool);
1627               pool_createwhatprovides(pool);
1628               prepared = 1;
1629             }
1630           /* rejoin */
1631           for (sp = pieces[1]; sp < pieces[npieces - 1]; sp++)
1632             if (*sp == 0)
1633               *sp = ' ';
1634           how = testcase_str2job(pool, pieces[1], &what);
1635           if (how >= 0 && job)
1636             queue_push2(job, how, what);
1637         }
1638       else if (!strcmp(pieces[0], "vendorclass") && npieces > 1)
1639         {
1640           pool_addvendorclass(pool, (const char **)(pieces + 1));
1641         }
1642       else if (!strcmp(pieces[0], "namespace") && npieces > 1)
1643         {
1644           int i = strlen(pieces[1]);
1645           s = strchr(pieces[1], '(');
1646           if (!s && pieces[1][i - 1] != ')')
1647             {
1648               pool_debug(pool, SOLV_ERROR, "testcase_read: bad namespace '%s'\n", pieces[1]);
1649             }
1650           else
1651             {
1652               Id name, evr, id;
1653               Queue q;
1654               queue_init(&q);
1655               *s = 0;
1656               pieces[1][i - 1] = 0;
1657               name = pool_str2id(pool, pieces[1], 1);
1658               evr = pool_str2id(pool, s + 1, 1);
1659               *s = '(';
1660               pieces[1][i - 1] = ')';
1661               id = pool_rel2id(pool, name, evr, REL_NAMESPACE, 1);
1662               for (i = 2; i < npieces; i++)
1663                 queue_push(&q, testcase_str2solvid(pool, pieces[i]));
1664               /* now do the callback */
1665               if (!prepared)
1666                 {
1667                   pool_addfileprovides(pool);
1668                   pool_createwhatprovides(pool);
1669                   prepared = 1;
1670                 }
1671               pool->whatprovides_rel[GETRELID(id)] = pool_queuetowhatprovides(pool, &q);
1672               queue_free(&q);
1673             }
1674         }
1675       else if (!strcmp(pieces[0], "solverflags") && npieces > 1)
1676         {
1677           int i;
1678           if (!solv)
1679             {
1680               solv = solver_create(pool);
1681               testcase_resetsolverflags(solv);
1682             }
1683           for (i = 1; i < npieces; i++)
1684             testcase_setsolverflags(solv, pieces[i]);
1685         }
1686       else if (!strcmp(pieces[0], "result") && npieces > 1)
1687         {
1688           FILE *rfp;
1689           const char *rdata;
1690           int resultflags = 0;
1691           char *s = pieces[1];
1692           int i;
1693           while (s)
1694             {
1695               char *se = strchr(s, ',');
1696               if (se)
1697                 *se++ = 0;
1698               for (i = 0; resultflags2str[i].str; i++)
1699                 if (!strcmp(s, resultflags2str[i].str))
1700                   {
1701                     resultflags |= resultflags2str[i].flag;
1702                     break;
1703                   }
1704               if (!resultflags2str[i].str)
1705                 pool_debug(pool, SOLV_ERROR, "result: unknown flag '%s'\n", s);
1706               s = se;
1707             }
1708
1709           rdata = pool_tmpjoin(pool, testcasedir, pieces[2], 0);
1710           if (!strcmp(pieces[1], "<inline>"))
1711             rfp = fp;
1712           else
1713             rfp = fopen(rdata, "r");
1714           if (!rfp)
1715             {
1716               pool_debug(pool, SOLV_ERROR, "testcase_read: could not open '%s'\n", rdata);
1717             }
1718           else
1719             {
1720               /* slurp it in... */
1721               char *result = solv_malloc(1024);
1722               char *rp = result;
1723               int resultl = 1024;
1724               for (;;)
1725                 {
1726                   size_t rl;
1727                   if (rp - result + 256 >= resultl)
1728                     {
1729                       resultl = rp - result;
1730                       result = solv_realloc(result, resultl + 1024);
1731                       rp = result + resultl;
1732                       resultl += 1024;
1733                     }
1734                   if (fp == rfp)
1735                     {
1736                       if (!fgets(rp, resultl - (rp - result), fp))
1737                         rl = 0;
1738                       else
1739                         {
1740                           rl = strlen(rp);
1741                           if (rl && (rp == result || rp[-1] == '\n'))
1742                             {
1743                               if (rl > 1 && rp[0] == '#' && rp[1] == '>')
1744                                 {
1745                                   memmove(rp, rp + 2, rl - 2);
1746                                   rl -= 2;
1747                                 }
1748                               else
1749                                 {
1750                                   while (rl + 16 > bufl)
1751                                     {
1752                                       buf = solv_realloc(buf, bufl + 512);
1753                                       bufl += 512;
1754                                     }
1755                                   memmove(buf, rp, rl);
1756                                   if (buf[rl - 1] == '\n')
1757                                     {
1758                                       ungetc('\n', fp);
1759                                       rl--;
1760                                     }
1761                                   bufp = buf + rl;
1762                                   rl = 0;
1763                                 }
1764                             }
1765                         }
1766                     }
1767                   else
1768                     rl = fread(rp, 1, resultl - (rp - result), rfp);
1769                   if (rl <= 0)
1770                     {
1771                       *rp = 0;
1772                       break;
1773                     }
1774                   rp += rl;
1775                 }
1776               if (rfp != fp)
1777                 fclose(rfp);
1778               if (resultp)
1779                 *resultp = result;
1780               else
1781                 solv_free(result);
1782               if (resultflagsp)
1783                 *resultflagsp = resultflags;
1784             }
1785         }
1786       else
1787         {
1788           pool_debug(pool, SOLV_ERROR, "testcase_read: cannot parse command '%s'\n", pieces[0]);
1789         }
1790     }
1791   buf = solv_free(buf);
1792   pieces = solv_free(pieces);
1793   solv_free(testcasedir);
1794   if (!prepared)
1795     {
1796       pool_addfileprovides(pool);
1797       pool_createwhatprovides(pool);
1798     }
1799   if (!solv)
1800     {
1801       solv = solver_create(pool);
1802       testcase_resetsolverflags(solv);
1803     }
1804   if (closefp)
1805     fclose(fp);
1806   return solv;
1807 }
1808
1809 char *
1810 testcase_resultdiff(char *result1, char *result2)
1811 {
1812   Strqueue sq1, sq2, osq;
1813   char *r;
1814   strqueue_init(&sq1);
1815   strqueue_init(&sq2);
1816   strqueue_init(&osq);
1817   strqueue_split(&sq1, result1);
1818   strqueue_split(&sq2, result2);
1819   strqueue_sort(&sq1);
1820   strqueue_sort(&sq2);
1821   strqueue_diff(&sq1, &sq2, &osq);
1822   r = osq.nstr ? strqueue_join(&osq) : 0;
1823   strqueue_free(&sq1);
1824   strqueue_free(&sq2);
1825   strqueue_free(&osq);
1826   return r;
1827 }
1828