91eba34c7e7b7feece9a601b8c7c6b66fa302dea
[platform/upstream/libsolv.git] / src / poolid.c
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * poolid.c
10  *
11  * Id management
12  */
13
14 #include <stdlib.h>
15 #include <string.h>
16 #include <stdio.h>
17
18 #include "pool.h"
19 #include "poolid.h"
20 #include "poolid_private.h"
21 #include "util.h"
22
23
24 /* intern string into pool, return id */
25
26 Id
27 pool_str2id(Pool *pool, const char *str, int create)
28 {
29   int oldnstrings = pool->ss.nstrings;
30   Id id = stringpool_str2id(&pool->ss, str, create);
31   if (create && pool->whatprovides && oldnstrings != pool->ss.nstrings && (id & WHATPROVIDES_BLOCK) == 0)
32     {
33       /* grow whatprovides array */
34       pool->whatprovides = solv_realloc(pool->whatprovides, (id + (WHATPROVIDES_BLOCK + 1)) * sizeof(Offset));
35       memset(pool->whatprovides + id, 0, (WHATPROVIDES_BLOCK + 1) * sizeof(Offset));
36     }
37   return id;
38 }
39
40 Id
41 pool_strn2id(Pool *pool, const char *str, unsigned int len, int create)
42 {
43   int oldnstrings = pool->ss.nstrings;
44   Id id = stringpool_strn2id(&pool->ss, str, len, create);
45   if (create && pool->whatprovides && oldnstrings != pool->ss.nstrings && (id & WHATPROVIDES_BLOCK) == 0)
46     {
47       /* grow whatprovides array */
48       pool->whatprovides = solv_realloc(pool->whatprovides, (id + (WHATPROVIDES_BLOCK + 1)) * sizeof(Offset));
49       memset(pool->whatprovides + id, 0, (WHATPROVIDES_BLOCK + 1) * sizeof(Offset));
50     }
51   return id;
52 }
53
54 Id
55 pool_rel2id(Pool *pool, Id name, Id evr, int flags, int create)
56 {
57   Hashval h, hh, hashmask;
58   int i;
59   Id id;
60   Hashtable hashtbl;
61   Reldep *ran;
62
63   hashmask = pool->relhashmask;
64   hashtbl = pool->relhashtbl;
65   ran = pool->rels;
66
67   /* extend hashtable if needed */
68   if ((Hashval)pool->nrels * 2 > hashmask)
69     {
70       solv_free(pool->relhashtbl);
71       pool->relhashmask = hashmask = mkmask(pool->nrels + REL_BLOCK);
72       pool->relhashtbl = hashtbl = solv_calloc(hashmask + 1, sizeof(Id));
73       /* rehash all rels into new hashtable */
74       for (i = 1; i < pool->nrels; i++)
75         {
76           h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
77           hh = HASHCHAIN_START;
78           while (hashtbl[h])
79             h = HASHCHAIN_NEXT(h, hh, hashmask);
80           hashtbl[h] = i;
81         }
82     }
83
84   /* compute hash and check for match */
85   h = relhash(name, evr, flags) & hashmask;
86   hh = HASHCHAIN_START;
87   while ((id = hashtbl[h]) != 0)
88     {
89       if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == flags)
90         break;
91       h = HASHCHAIN_NEXT(h, hh, hashmask);
92     }
93   if (id)
94     return MAKERELDEP(id);
95
96   if (!create)
97     return ID_NULL;
98
99   id = pool->nrels++;
100   /* extend rel space if needed */
101   pool->rels = solv_extend(pool->rels, id, 1, sizeof(Reldep), REL_BLOCK);
102   hashtbl[h] = id;
103   ran = pool->rels + id;
104   ran->name = name;
105   ran->evr = evr;
106   ran->flags = flags;
107
108   /* extend whatprovides_rel if needed */
109   if (pool->whatprovides_rel && (id & WHATPROVIDES_BLOCK) == 0)
110     {
111       pool->whatprovides_rel = solv_realloc2(pool->whatprovides_rel, id + (WHATPROVIDES_BLOCK + 1), sizeof(Offset));
112       memset(pool->whatprovides_rel + id, 0, (WHATPROVIDES_BLOCK + 1) * sizeof(Offset));
113     }
114   return MAKERELDEP(id);
115 }
116
117
118 /* Id -> String
119  * for rels (returns name only) and strings
120  */
121 const char *
122 pool_id2str(const Pool *pool, Id id)
123 {
124   while (ISRELDEP(id))
125     {
126       Reldep *rd = GETRELDEP(pool, id);
127       id = rd->name;
128     }
129   return pool->ss.stringspace + pool->ss.strings[id];
130 }
131
132 static const char *rels[] = {
133   " ! ",
134   " > ",
135   " = ",
136   " >= ",
137   " < ",
138   " <> ",
139   " <= ",
140   " <=> "
141 };
142
143
144 /* get operator for RelId */
145 const char *
146 pool_id2rel(const Pool *pool, Id id)
147 {
148   Reldep *rd;
149   if (!ISRELDEP(id))
150     return "";
151   rd = GETRELDEP(pool, id);
152   switch (rd->flags)
153     {
154     /* debian special cases < and > */
155     /* haiku special cases <> (maybe we should use != for the others as well */
156     case 0: case REL_EQ: case REL_GT | REL_EQ:
157     case REL_LT | REL_EQ: case REL_LT | REL_EQ | REL_GT:
158 #if !defined(DEBIAN) && !defined(MULTI_SEMANTICS)
159     case REL_LT: case REL_GT:
160 #endif
161 #if !defined(HAIKU) && !defined(MULTI_SEMANTICS)
162     case REL_LT | REL_GT:
163 #endif
164       return rels[rd->flags];
165 #if defined(DEBIAN) || defined(MULTI_SEMANTICS)
166     case REL_GT:
167       return pool->disttype == DISTTYPE_DEB ? " >> " : rels[rd->flags];
168     case REL_LT:
169       return pool->disttype == DISTTYPE_DEB ? " << " : rels[rd->flags];
170 #endif
171 #if defined(HAIKU) || defined(MULTI_SEMANTICS)
172     case REL_LT | REL_GT:
173       return pool->disttype == DISTTYPE_HAIKU ? " != " : rels[rd->flags];
174 #endif
175     case REL_AND:
176       return pool->disttype == DISTTYPE_RPM ? " and " : " & ";
177     case REL_OR:
178       return pool->disttype == DISTTYPE_RPM ? " or " : " | ";
179     case REL_WITH:
180       return pool->disttype == DISTTYPE_RPM ? " with " : " + ";
181     case REL_NAMESPACE:
182       return " NAMESPACE ";     /* actually not used in dep2str */
183     case REL_ARCH:
184       return ".";
185     case REL_MULTIARCH:
186       return ":";
187     case REL_FILECONFLICT:
188       return " FILECONFLICT ";
189     case REL_COND:
190       return pool->disttype == DISTTYPE_RPM ? " if " : " IF ";
191     case REL_COMPAT:
192       return " compat >= ";
193     case REL_KIND:
194       return " KIND ";
195     case REL_ELSE:
196       return pool->disttype == DISTTYPE_RPM ? " else " : " ELSE ";
197     default:
198       break;
199     }
200   return " ??? ";
201 }
202
203
204 /* get e:v.r for Id */
205 const char *
206 pool_id2evr(const Pool *pool, Id id)
207 {
208   Reldep *rd;
209   if (!ISRELDEP(id))
210     return "";
211   rd = GETRELDEP(pool, id);
212   if (ISRELDEP(rd->evr))
213     return "(REL)";
214   return pool->ss.stringspace + pool->ss.strings[rd->evr];
215 }
216
217 static int
218 dep2strlen(const Pool *pool, Id id)
219 {
220   int l = 0;
221
222   while (ISRELDEP(id))
223     {
224       Reldep *rd = GETRELDEP(pool, id);
225       /* add 2 for parens */
226       l += 2 + dep2strlen(pool, rd->name) + strlen(pool_id2rel(pool, id));
227       id = rd->evr;
228     }
229   return l + strlen(pool->ss.stringspace + pool->ss.strings[id]);
230 }
231
232 static void
233 dep2strcpy(const Pool *pool, char *p, Id id, int oldrel)
234 {
235   while (ISRELDEP(id))
236     {
237       Reldep *rd = GETRELDEP(pool, id);
238       int rel = rd->flags;
239       if (oldrel == REL_AND || oldrel == REL_OR || oldrel == REL_WITH || oldrel == REL_COND || oldrel == REL_ELSE || oldrel == -1)
240         if (rel == REL_AND || rel == REL_OR || rel == REL_WITH || rel == REL_COND || rel == REL_ELSE)
241           if ((oldrel != rel || rel == REL_COND || rel == REL_ELSE) && !(oldrel == REL_COND && rel == REL_ELSE))
242             {
243               *p++ = '(';
244               dep2strcpy(pool, p, rd->name, rd->flags);
245               p += strlen(p);
246               strcpy(p, pool_id2rel(pool, id));
247               p += strlen(p);
248               dep2strcpy(pool, p, rd->evr, rd->flags);
249               strcat(p, ")");
250               return;
251             }
252       if (rd->flags == REL_KIND)
253         {
254           dep2strcpy(pool, p, rd->evr, rd->flags);
255           p += strlen(p);
256           *p++ = ':';
257           id = rd->name;
258           oldrel = rd->flags;
259           continue;
260         }
261       dep2strcpy(pool, p, rd->name, rd->flags);
262       p += strlen(p);
263       if (rd->flags == REL_NAMESPACE)
264         {
265           *p++ = '(';
266           dep2strcpy(pool, p, rd->evr, rd->flags);
267           strcat(p, ")");
268           return;
269         }
270       if (rd->flags == REL_FILECONFLICT)
271         {
272           *p = 0;
273           return;
274         }
275       strcpy(p, pool_id2rel(pool, id));
276       p += strlen(p);
277       id = rd->evr;
278       oldrel = rd->flags;
279     }
280   strcpy(p, pool->ss.stringspace + pool->ss.strings[id]);
281 }
282
283 const char *
284 pool_dep2str(Pool *pool, Id id)
285 {
286   char *p;
287   if (!ISRELDEP(id))
288     return pool->ss.stringspace + pool->ss.strings[id];
289   p = pool_alloctmpspace(pool, dep2strlen(pool, id) + 1);
290   dep2strcpy(pool, p, id, pool->disttype == DISTTYPE_RPM ? -1 : 0);
291   return p;
292 }
293
294 void
295 pool_shrink_strings(Pool *pool)
296 {
297   stringpool_shrink(&pool->ss);
298 }
299
300 void
301 pool_shrink_rels(Pool *pool)
302 {
303   pool->rels = solv_extend_resize(pool->rels, pool->nrels, sizeof(Reldep), REL_BLOCK);
304 }
305
306 /* free all hash tables */
307 void
308 pool_freeidhashes(Pool *pool)
309 {
310   stringpool_freehash(&pool->ss);
311   pool->relhashtbl = solv_free(pool->relhashtbl);
312   pool->relhashmask = 0;
313 }
314
315 /* EOF */