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