2 * Copyright (c) 2019, SUSE LLC
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
20 #include "tools_util.h"
23 testcase_id2str(Pool *pool, Id id, int isname)
25 const char *s = pool_id2str(pool, id);
28 int bad = 0, paren = 0, parenbad = 0;
34 if (strchr("[(<=>!", *s))
36 if (!strncmp(s, "namespace:", 10))
38 for (ss = s + bad; *ss; ss++)
40 if (*ss == ' ' || *ss == '\\' || *(unsigned char *)ss < 32 || *ss == '(' || *ss == ')')
43 paren = paren == 0 ? 1 : -1;
46 paren = paren == 1 ? 0 : -1;
51 if (isname && ss - s > 4 && !strcmp(ss - 4, ":any"))
53 if (!paren && !(bad - parenbad))
56 /* we need escaping! */
57 s2 = s2p = pool_alloctmpspace(pool, strlen(s) + bad * 2 + 1);
58 if (!strncmp(s, "namespace:", 10))
60 strcpy(s2p, "namespace\\3a");
68 if ((ss == s && strchr("[(<=>!", *s)) || *ss == ' ' || *ss == '\\' || *(unsigned char *)ss < 32 || *ss == '(' || *ss == ')')
71 solv_bin2hex((unsigned char *)ss, 1, s2p);
76 if (isname && s2p - s2 > 4 && !strcmp(s2p - 4, ":any"))
77 strcpy(s2p - 4, "\\3aany");
86 { REL_GT | REL_LT | REL_EQ, "<=>" },
87 { REL_LT | REL_EQ, "<=" },
88 { REL_GT | REL_EQ, ">=" },
90 { REL_GT | REL_LT, "<>" },
94 { REL_WITHOUT , "-" },
95 { REL_NAMESPACE , "<NAMESPACE>" },
97 { REL_MULTIARCH, "<MULTIARCH>" },
98 { REL_FILECONFLICT, "<FILECONFLICT>" },
100 { REL_COMPAT, "compat >=" },
101 { REL_KIND, "<KIND>" },
102 { REL_ELSE, "<ELSE>" },
103 { REL_ERROR, "<ERROR>" },
104 { REL_UNLESS, "<UNLESS>" },
105 { REL_CONDA, "<CONDA>" },
111 testcase_dep2str_complex(Pool *pool, char *s, Id id, int addparens)
120 s2 = testcase_id2str(pool, id, 1);
121 s = pool_tmpappend(pool, s, s2, 0);
122 pool_freetmpspace(pool, s2);
125 rd = GETRELDEP(pool, id);
127 /* check for special shortcuts */
128 if (rd->flags == REL_NAMESPACE && !ISRELDEP(rd->name) && !strncmp(pool_id2str(pool, rd->name), "namespace:", 10))
130 s = pool_tmpappend(pool, s, pool_id2str(pool, rd->name), "(");
131 s = testcase_dep2str_complex(pool, s, rd->evr, 0);
132 return pool_tmpappend(pool, s, ")", 0);
134 if (rd->flags == REL_MULTIARCH && !ISRELDEP(rd->name) && rd->evr == ARCH_ANY)
136 /* append special :any suffix */
137 s2 = testcase_id2str(pool, rd->name, 1);
138 s = pool_tmpappend(pool, s, s2, ":any");
139 pool_freetmpspace(pool, s2);
144 if (ISRELDEP(rd->name))
146 Reldep *rd2 = GETRELDEP(pool, rd->name);
148 if (rd->flags > 7 && rd->flags != REL_COMPAT && rd2->flags && rd2->flags <= 7)
153 s = pool_tmpappend(pool, s, "(", 0);
154 s = testcase_dep2str_complex(pool, s, rd->name, needparens);
156 for (op = oplist; op->flags; op++)
157 if (rd->flags == op->flags)
161 s = pool_tmpappend(pool, s, " ", op->opname);
162 s = pool_tmpappend(pool, s, " ", 0);
167 sprintf(buf, " <%u> ", rd->flags);
168 s = pool_tmpappend(pool, s, buf, 0);
172 if (ISRELDEP(rd->evr))
174 Reldep *rd2 = GETRELDEP(pool, rd->evr);
176 if (rd->flags > 7 && rd2->flags && rd2->flags <= 7)
178 if (rd->flags == REL_AND && rd2->flags == REL_AND)
179 needparens = 0; /* chain */
180 if (rd->flags == REL_OR && rd2->flags == REL_OR)
181 needparens = 0; /* chain */
182 if (rd->flags > 0 && rd->flags < 8 && rd2->flags == REL_COMPAT)
183 needparens = 0; /* chain */
185 if (!ISRELDEP(rd->evr))
187 s2 = testcase_id2str(pool, rd->evr, 0);
188 s = pool_tmpappend(pool, s, s2, 0);
189 pool_freetmpspace(pool, s2);
192 s = (char *)testcase_dep2str_complex(pool, s, rd->evr, needparens);
194 s = pool_tmpappend(pool, s, ")", 0);
199 testcase_dep2str(Pool *pool, Id id)
203 return testcase_id2str(pool, id, 1);
204 s = pool_alloctmpspace(pool, 1);
206 return testcase_dep2str_complex(pool, s, id, 0);
210 /* Convert a simple string. Also handle the :any suffix */
212 testcase_str2dep_simple(Pool *pool, const char **sp, int isname)
219 for (s = *sp; *s; s++)
223 if (*s == ' ' || *(unsigned char *)s < 32)
227 if (*s == ')' && paren-- <= 0)
230 if (isname && s - *sp > 4 && !strncmp(s - 4, ":any", 4))
237 if (s - *sp == 6 && !strncmp(*sp, "<NULL>", 6))
240 id = pool_strn2id(pool, *sp, s - *sp, 1);
242 else if (s - *sp == 3 && !strncmp(*sp, "\\00", 3))
246 char buf[128], *bp, *bp2;
248 bp = s - *sp >= 128 ? solv_malloc(s - *sp + 1) : buf;
249 for (bp2 = bp, sp2 = *sp; sp2 < s;)
253 solv_hex2bin(&sp2, (unsigned char *)bp2 - 1, 1);
256 id = pool_str2id(pool, bp, 1);
262 id = pool_rel2id(pool, id, ARCH_ANY, REL_MULTIARCH, 1);
271 testcase_str2dep_complex(Pool *pool, const char **sp, int relop)
274 Id flags, id, id2, namespaceid = 0;
279 while (*s == ' ' || *s == '\t')
281 if (!strncmp(s, "namespace:", 10))
283 /* special namespace hack */
285 for (s2 = s + 10; *s2 && *s2 != '('; s2++)
289 namespaceid = pool_strn2id(pool, s, s2 - s, 1);
296 id = testcase_str2dep_complex(pool, &s, 0);
305 id = testcase_str2dep_simple(pool, &s, relop ? 0 : 1);
307 id = pool_rel2id(pool, namespaceid, id, REL_NAMESPACE, 1);
311 while (*s == ' ' || *s == '\t')
313 if (!*s || *s == ')' || (relop && strncmp(s, "compat >= ", 10) != 0))
319 /* we have an op! Find the end */
321 if (s[0] == '<' && (s[1] >= '0' && s[1] <= '9'))
324 for (s2 = s + 1; *s2 >= '0' && *s2 <= '9'; s2++)
328 flags = strtoul(s + 1, 0, 10);
334 for (op = oplist; op->flags; op++)
335 if (!strncmp(s, op->opname, strlen(op->opname)))
343 s += strlen(op->opname);
345 id2 = testcase_str2dep_complex(pool, &s, flags > 0 && flags < 8);
351 id = pool_rel2id(pool, id, id2, flags, 1);
356 testcase_str2dep(Pool *pool, const char *s)
358 Id id = testcase_str2dep_complex(pool, &s, 0);
359 return s && !*s ? id : 0;
363 writedeps(Repo *repo, FILE *fp, const char *tag, Id key, Solvable *s, Offset off)
365 Pool *pool = repo->pool;
372 dp = repo->idarraydata + off;
373 while ((id = *dp++) != 0)
375 if (key == SOLVABLE_REQUIRES && id == SOLVABLE_PREREQMARKER)
378 fprintf(fp, "-%s\n", tag);
383 if (key == SOLVABLE_PROVIDES && id == SOLVABLE_FILEMARKER)
385 idstr = testcase_dep2str(pool, id);
388 fprintf(fp, "+%s\n", tag);
391 fprintf(fp, "%s\n", idstr);
394 fprintf(fp, "-%s\n", tag);
398 writefilelist(Repo *repo, FILE *fp, const char *tag, Solvable *s)
400 Pool *pool = repo->pool;
404 dataiterator_init(&di, pool, repo, s - pool->solvables, SOLVABLE_FILELIST, 0, 0);
405 while (dataiterator_step(&di))
407 const char *s = repodata_dir2str(di.data, di.kv.id, di.kv.str);
410 fprintf(fp, "+%s\n", tag);
413 fprintf(fp, "%s\n", s);
416 fprintf(fp, "-%s\n", tag);
417 dataiterator_free(&di);
421 testcase_write_testtags(Repo *repo, FILE *fp)
423 Pool *pool = repo->pool;
434 fprintf(fp, "=Ver: 3.0\n");
436 FOR_REPO_SOLVABLES(repo, p, s)
438 name = pool_id2str(pool, s->name);
439 evr = pool_id2str(pool, s->evr);
440 arch = s->arch ? pool_id2str(pool, s->arch) : "-";
441 release = strrchr(evr, '-');
443 release = evr + strlen(evr);
444 fprintf(fp, "=Pkg: %s %.*s %s %s\n", name, (int)(release - evr), evr, *release && release[1] ? release + 1 : "-", arch);
445 tmp = solvable_lookup_str(s, SOLVABLE_SUMMARY);
447 fprintf(fp, "=Sum: %s\n", tmp);
448 writedeps(repo, fp, "Req:", SOLVABLE_REQUIRES, s, s->requires);
449 writedeps(repo, fp, "Prv:", SOLVABLE_PROVIDES, s, s->provides);
450 writedeps(repo, fp, "Obs:", SOLVABLE_OBSOLETES, s, s->obsoletes);
451 writedeps(repo, fp, "Con:", SOLVABLE_CONFLICTS, s, s->conflicts);
452 writedeps(repo, fp, "Rec:", SOLVABLE_RECOMMENDS, s, s->recommends);
453 writedeps(repo, fp, "Sup:", SOLVABLE_SUPPLEMENTS, s, s->supplements);
454 writedeps(repo, fp, "Sug:", SOLVABLE_SUGGESTS, s, s->suggests);
455 writedeps(repo, fp, "Enh:", SOLVABLE_ENHANCES, s, s->enhances);
456 if (solvable_lookup_idarray(s, SOLVABLE_PREREQ_IGNOREINST, &q))
459 fprintf(fp, "+Ipr:\n");
460 for (i = 0; i < q.count; i++)
461 fprintf(fp, "%s\n", testcase_dep2str(pool, q.elements[i]));
462 fprintf(fp, "-Ipr:\n");
464 if (solvable_lookup_idarray(s, SOLVABLE_CONSTRAINS, &q))
467 fprintf(fp, "+Cns:\n");
468 for (i = 0; i < q.count; i++)
469 fprintf(fp, "%s\n", testcase_dep2str(pool, q.elements[i]));
470 fprintf(fp, "-Cns:\n");
473 fprintf(fp, "=Vnd: %s\n", pool_id2str(pool, s->vendor));
474 if (solvable_lookup_idarray(s, SOLVABLE_BUILDFLAVOR, &q))
477 for (i = 0; i < q.count; i++)
478 fprintf(fp, "=Flv: %s\n", pool_id2str(pool, q.elements[i]));
480 tmp = solvable_lookup_str(s, SOLVABLE_BUILDVERSION);
482 fprintf(fp, "=Bvr: %s\n", tmp);
483 ti = solvable_lookup_num(s, SOLVABLE_BUILDTIME, 0);
485 fprintf(fp, "=Tim: %u\n", ti);
486 writefilelist(repo, fp, "Fls:", s);
493 adddep(Repo *repo, Offset olddeps, char *str, Id marker)
495 Id id = *str == '/' ? pool_str2id(repo->pool, str, 1) : testcase_str2dep(repo->pool, str);
496 return repo_addid_dep(repo, olddeps, id, marker);
500 finish_v2_solvable(Pool *pool, Repodata *data, Solvable *s, char *filelist, int nfilelist)
506 for (l = 0; l < nfilelist; l += strlen(filelist + l) + 1)
508 char *p = strrchr(filelist + l, '/');
512 did = repodata_str2dir(data, filelist + l, 1);
515 did = repodata_str2dir(data, "/", 1);
516 repodata_add_dirstr(data, s - pool->solvables, SOLVABLE_FILELIST, did, p);
519 repo_rewrite_suse_deps(s, 0);
522 /* stripped down version of susetags parser used for testcases */
524 testcase_add_testtags(Repo *repo, FILE *fp, int flags)
526 Pool *pool = repo->pool;
539 int addselfprovides = 1; /* for compat reasons */
541 data = repo_add_repodata(repo, flags);
546 line = solv_malloc(aline);
550 if (linep - line + 16 > aline)
552 aline = linep - line;
553 line = solv_realloc(line, aline + 512);
554 linep = line + aline;
557 if (!fgets(linep, aline - (linep - line), fp))
559 linep += strlen(linep);
560 if (linep == line || linep[-1] != '\n')
563 linep = line + intag;
566 if (line[intag] == '-' && !strncmp(line + 1, line + intag + 1, intag - 2))
573 else if (line[0] == '+' && line[1] && line[1] != ':')
575 char *tagend = strchr(line, ':');
580 intag = tagend + 2 - line;
581 linep = line + intag;
584 if (*line != '=' || !line[1] || !line[2] || !line[3] || line[4] != ':')
586 tag = line[1] << 16 | line[2] << 8 | line[3];
587 /* tags that do not need a solvable */
590 case 'V' << 16 | 'e' << 8 | 'r':
591 tagsversion = atoi(line + 6);
592 addselfprovides = tagsversion < 3 || strstr(line + 6, "addselfprovides") != 0;
594 case 'P' << 16 | 'k' << 8 | 'g':
597 if (tagsversion == 2)
598 finish_v2_solvable(pool, data, s, filelist, nfilelist);
599 if (addselfprovides && s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
600 s->provides = repo_addid_dep(s->repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
603 if (split(line + 5, sp, 5) != 4)
605 s = pool_id2solvable(pool, repo_add_solvable(repo));
606 s->name = pool_str2id(pool, sp[0], 1);
607 /* join back version and release */
608 if (sp[2] && !(sp[2][0] == '-' && !sp[2][1]))
610 s->evr = makeevr(pool, sp[1]);
611 s->arch = strcmp(sp[3], "-") ? pool_str2id(pool, sp[3], 1) : 0;
618 /* tags that need a solvable */
621 case 'S' << 16 | 'u' << 8 | 'm':
622 repodata_set_str(data, s - pool->solvables, SOLVABLE_SUMMARY, line + 6);
624 case 'V' << 16 | 'n' << 8 | 'd':
625 s->vendor = pool_str2id(pool, line + 6, 1);
627 case 'T' << 16 | 'i' << 8 | 'm':
630 repodata_set_num(data, s - pool->solvables, SOLVABLE_BUILDTIME, t);
632 case 'R' << 16 | 'e' << 8 | 'q':
633 s->requires = adddep(repo, s->requires, line + 6, -SOLVABLE_PREREQMARKER);
635 case 'P' << 16 | 'r' << 8 | 'q':
636 s->requires = adddep(repo, s->requires, line + 6, SOLVABLE_PREREQMARKER);
638 case 'P' << 16 | 'r' << 8 | 'v':
639 /* version 2 had the file list at the end of the provides */
640 if (tagsversion == 2)
644 int l = strlen(line + 6) + 1;
645 if (nfilelist + l > afilelist)
647 afilelist = nfilelist + l + 512;
648 filelist = solv_realloc(filelist, afilelist);
650 memcpy(filelist + nfilelist, line + 6, l);
657 for (l = 0; l < nfilelist; l += strlen(filelist + l) + 1)
658 s->provides = repo_addid_dep(repo, s->provides, pool_str2id(pool, filelist + l, 1), 0);
662 s->provides = adddep(repo, s->provides, line + 6, 0);
664 case 'F' << 16 | 'l' << 8 | 's':
666 char *p = strrchr(line + 6, '/');
671 did = repodata_str2dir(data, line + 6, 1);
673 did = repodata_str2dir(data, "/", 1);
674 repodata_add_dirstr(data, s - pool->solvables, SOLVABLE_FILELIST, did, p);
677 case 'O' << 16 | 'b' << 8 | 's':
678 s->obsoletes = adddep(repo, s->obsoletes, line + 6, 0);
680 case 'C' << 16 | 'o' << 8 | 'n':
681 s->conflicts = adddep(repo, s->conflicts, line + 6, 0);
683 case 'R' << 16 | 'e' << 8 | 'c':
684 s->recommends = adddep(repo, s->recommends, line + 6, 0);
686 case 'S' << 16 | 'u' << 8 | 'p':
687 s->supplements = adddep(repo, s->supplements, line + 6, 0);
689 case 'S' << 16 | 'u' << 8 | 'g':
690 s->suggests = adddep(repo, s->suggests, line + 6, 0);
692 case 'E' << 16 | 'n' << 8 | 'h':
693 s->enhances = adddep(repo, s->enhances, line + 6, 0);
695 case 'I' << 16 | 'p' << 8 | 'r':
697 Id id = line[6] == '/' ? pool_str2id(pool, line + 6, 1) : testcase_str2dep(pool, line + 6);
698 repodata_add_idarray(data, s - pool->solvables, SOLVABLE_PREREQ_IGNOREINST, id);
701 case 'C' << 16 | 'n' << 8 | 's':
702 repodata_add_idarray(data, s - pool->solvables, SOLVABLE_CONSTRAINS, testcase_str2dep(pool, line + 6));
704 case 'F' << 16 | 'l' << 8 | 'v':
705 repodata_add_poolstr_array(data, s - pool->solvables, SOLVABLE_BUILDFLAVOR, line + 6);
707 case 'B' << 16 | 'v' << 8 | 'r':
708 repodata_set_str(data, s - pool->solvables, SOLVABLE_BUILDVERSION, line + 6);
716 if (tagsversion == 2)
717 finish_v2_solvable(pool, data, s, filelist, nfilelist);
718 if (addselfprovides && s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
719 s->provides = repo_addid_dep(s->repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
723 repodata_free_dircache(data);
724 if (!(flags & REPO_NO_INTERNALIZE))
725 repodata_internalize(data);