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