7 #include "repo_rpmdb.h"
8 #include "pool_fileconflicts.h"
19 unsigned int cflmapused;
23 unsigned int dirmapused;
32 unsigned char *filesspace;
33 unsigned int filesspacen;
36 #define FILESSPACE_BLOCK 255
39 doublehash(Hashval *map, Hashmask *mapnp)
41 Hashmask mapn = *mapnp;
42 Hashmask i, hx, qx, h, hh;
43 Hashmask nn = (mapn + 1) * 2 - 1;
46 m = solv_calloc(nn + 1, 2 * sizeof(Id));
47 for (i = 0; i <= mapn; i++)
59 h = HASHCHAIN_NEXT(h, hh, nn);
62 m[2 * h + 1] = map[2 * i + 1];
70 finddirs_cb(void *cbdatav, const char *fn, int fmode, const char *md5)
72 struct cbdata *cbdata = cbdatav;
73 Hashmask h, hh, hx, qx;
74 Hashval idx = cbdata->idx;
79 h = hx & cbdata->dirmapn;
83 qx = cbdata->dirmap[2 * h];
88 h = HASHCHAIN_NEXT(h, hh, cbdata->dirmapn);
95 cbdata->dirmap[2 * h] = hx;
96 cbdata->dirmap[2 * h + 1] = idx;
98 if (cbdata->dirmapused * 2 > cbdata->dirmapn)
99 cbdata->dirmap = doublehash(cbdata->dirmap, &cbdata->dirmapn);
102 if (cbdata->dirmap[2 * h + 1] == idx)
104 /* found a conflict, this dir is used in multiple packages */
105 if (cbdata->dirmap[2 * h + 1] != -1)
107 MAPSET(&cbdata->idxmap, cbdata->dirmap[2 * h + 1]);
108 cbdata->dirmap[2 * h + 1] = -1;
109 cbdata->dirconflicts++;
111 MAPSET(&cbdata->idxmap, idx);
115 isindirmap(struct cbdata *cbdata, Hashmask hx)
119 h = hx & cbdata->dirmapn;
120 hh = HASHCHAIN_START;
123 qx = cbdata->dirmap[2 * h];
127 return cbdata->dirmap[2 * h + 1] == -1 ? 1 : 0;
128 h = HASHCHAIN_NEXT(h, hh, cbdata->dirmapn);
133 findfileconflicts_cb(void *cbdatav, const char *fn, int fmode, const char *md5)
135 struct cbdata *cbdata = cbdatav;
136 int isdir = S_ISDIR(fmode);
139 Hashmask qx, h, hx, hh, dhx;
143 dp = strrchr(fn, '/');
146 dhx = strnhash(fn, dp + 1 - fn);
148 dhx = 1 + dp + 1 - fn;
150 if (!isindirmap(cbdata, dhx))
154 hx = strhash_cont(dp + 1, dhx);
158 h = hx & cbdata->cflmapn;
159 hh = HASHCHAIN_START;
162 qx = cbdata->cflmap[2 * h];
167 h = HASHCHAIN_NEXT(h, hh, cbdata->cflmapn);
174 cbdata->cflmap[2 * h] = hx;
175 cbdata->cflmap[2 * h + 1] = (isdir ? ~idx : idx);
176 cbdata->cflmapused++;
177 if (cbdata->cflmapused * 2 > cbdata->cflmapn)
178 cbdata->cflmap = doublehash(cbdata->cflmap, &cbdata->cflmapn);
181 qidx = cbdata->cflmap[2 * h + 1];
188 /* delay the conflict */
189 queue_push2(&cbdata->lookat_dir, hx, qidx);
190 queue_push2(&cbdata->lookat_dir, hx, idx);
193 cbdata->cflmap[2 * h + 1] = qidx;
194 for (i = 0; i < cbdata->lookat_dir.count; i += 2)
195 if (cbdata->lookat_dir.elements[i] == hx)
196 queue_push2(&cbdata->lookat, hx, cbdata->lookat_dir.elements[i + 1]);
199 return; /* no conflicts with ourself, please */
200 queue_push2(&cbdata->lookat, hx, qidx);
201 queue_push2(&cbdata->lookat, hx, idx);
205 addfilesspace(struct cbdata *cbdata, unsigned char *data, int len)
207 cbdata->filesspace = solv_extend(cbdata->filesspace, cbdata->filesspacen, len, 1, FILESSPACE_BLOCK);
208 memcpy(cbdata->filesspace + cbdata->filesspacen, data, len);
209 cbdata->filesspacen += len;
213 findfileconflicts2_cb(void *cbdatav, const char *fn, int fmode, const char *md5)
215 struct cbdata *cbdata = cbdatav;
216 unsigned int hx = strhash(fn);
221 if (hx != cbdata->hx)
223 strncpy(md5padded, md5, 32);
225 md5padded[33] = fmode >> 24;
226 // printf("%d, hx %x -> %s %d %s\n", cbdata->idx, hx, fn, fmode, md5);
227 queue_push(&cbdata->files, cbdata->filesspacen);
228 addfilesspace(cbdata, (unsigned char *)md5padded, 34);
229 addfilesspace(cbdata, (unsigned char *)fn, strlen(fn) + 1);
232 static int cand_sort(const void *ap, const void *bp, void *dp)
237 unsigned int ax = (unsigned int)a[0];
238 unsigned int bx = (unsigned int)b[0];
243 return (a[1] < 0 ? -a[1] : a[1]) - (b[1] < 0 ? -b[1] : b[1]);
246 static int conflicts_cmp(const void *ap, const void *bp, void *dp)
252 return strcmp(pool_id2str(pool, a[0]), pool_id2str(pool, b[0]));
261 pool_findfileconflicts(Pool *pool, Queue *pkgs, int cutoff, Queue *conflicts, void *(*handle_cb)(Pool *, Id, void *) , void *handle_cbdata)
263 int i, j, cflmapn, idxmapset;
265 struct cbdata cbdata;
266 unsigned int now, start;
270 queue_empty(conflicts);
274 now = start = solv_timems(0);
275 POOL_DEBUG(SOLV_DEBUG_STATS, "searching for file conflicts\n");
276 POOL_DEBUG(SOLV_DEBUG_STATS, "packages: %d, cutoff %d\n", pkgs->count, cutoff);
278 memset(&cbdata, 0, sizeof(cbdata));
280 queue_init(&cbdata.lookat);
281 queue_init(&cbdata.lookat_dir);
282 queue_init(&cbdata.files);
283 map_init(&cbdata.idxmap, pkgs->count);
286 cutoff = pkgs->count;
288 /* avarage file list size: 200 files per package */
289 /* avarage dir count: 20 dirs per package */
291 /* first pass: scan dirs */
292 cflmapn = (cutoff + 3) * 64;
293 while ((cflmapn & (cflmapn - 1)) != 0)
294 cflmapn = cflmapn & (cflmapn - 1);
295 cbdata.dirmap = solv_calloc(cflmapn, 2 * sizeof(Id));
296 cbdata.dirmapn = cflmapn - 1; /* make it a mask */
299 for (i = 0; i < pkgs->count; i++)
301 p = pkgs->elements[i];
305 handle = (*handle_cb)(pool, p, handle_cbdata);
307 rpm_iterate_filelist(handle, RPM_ITERATE_FILELIST_ONLYDIRS, finddirs_cb, &cbdata);
308 if (MAPTST(&cbdata.idxmap, i))
312 POOL_DEBUG(SOLV_DEBUG_STATS, "dirmap size: %d used %d\n", cbdata.dirmapn + 1, cbdata.dirmapused);
313 POOL_DEBUG(SOLV_DEBUG_STATS, "dirmap memory usage: %d K\n", (cbdata.dirmapn + 1) * 2 * (int)sizeof(Id) / 1024);
314 POOL_DEBUG(SOLV_DEBUG_STATS, "dirmap creation took %d ms\n", solv_timems(now));
315 POOL_DEBUG(SOLV_DEBUG_STATS, "dir conflicts found: %d, idxmap %d of %d\n", cbdata.dirconflicts, idxmapset, pkgs->count);
317 /* second pass: scan files */
318 now = solv_timems(0);
319 cflmapn = (cutoff + 3) * 128;
320 while ((cflmapn & (cflmapn - 1)) != 0)
321 cflmapn = cflmapn & (cflmapn - 1);
322 cbdata.cflmap = solv_calloc(cflmapn, 2 * sizeof(Id));
323 cbdata.cflmapn = cflmapn - 1; /* make it a mask */
325 for (i = 0; i < pkgs->count; i++)
327 if (!MAPTST(&cbdata.idxmap, i))
329 p = pkgs->elements[i];
333 handle = (*handle_cb)(pool, p, handle_cbdata);
335 rpm_iterate_filelist(handle, RPM_ITERATE_FILELIST_NOGHOSTS, findfileconflicts_cb, &cbdata);
338 POOL_DEBUG(SOLV_DEBUG_STATS, "filemap size: %d used %d\n", cbdata.cflmapn + 1, cbdata.cflmapused);
339 POOL_DEBUG(SOLV_DEBUG_STATS, "filemap memory usage: %d K\n", (cbdata.cflmapn + 1) * 2 * (int)sizeof(Id) / 1024);
340 POOL_DEBUG(SOLV_DEBUG_STATS, "filemap creation took %d ms\n", solv_timems(now));
342 cbdata.dirmap = solv_free(cbdata.dirmap);
344 cbdata.dirmapused = 0;
345 cbdata.cflmap = solv_free(cbdata.cflmap);
347 cbdata.cflmapused = 0;
348 map_free(&cbdata.idxmap);
350 now = solv_timems(0);
351 POOL_DEBUG(SOLV_DEBUG_STATS, "lookat_dir size: %d\n", cbdata.lookat_dir.count);
352 queue_free(&cbdata.lookat_dir);
353 solv_sort(cbdata.lookat.elements, cbdata.lookat.count / 2, sizeof(Id) * 2, &cand_sort, pool);
355 for (i = j = 0; i < cbdata.lookat.count; i += 2)
357 hx = cbdata.lookat.elements[i];
358 Id idx = cbdata.lookat.elements[i + 1];
359 if (j && hx == cbdata.lookat.elements[j - 2] && idx == cbdata.lookat.elements[j - 1])
361 cbdata.lookat.elements[j++] = hx;
362 cbdata.lookat.elements[j++] = idx;
364 POOL_DEBUG(SOLV_DEBUG_STATS, "candidates: %d\n", cbdata.lookat.count / 2);
366 /* third pass: scan candidates */
367 for (i = 0; i < cbdata.lookat.count - 2; i += 2)
370 int pidx = cbdata.lookat.elements[i + 1];
373 iterflags = RPM_ITERATE_FILELIST_WITHMD5 | RPM_ITERATE_FILELIST_NOGHOSTS;
374 if (pool->obsoleteusescolors)
375 iterflags |= RPM_ITERATE_FILELIST_WITHCOL;
376 p = pkgs->elements[pidx];
377 hx = cbdata.lookat.elements[i];
378 if (cbdata.lookat.elements[i + 2] != hx)
379 continue; /* no package left */
380 queue_empty(&cbdata.files);
381 cbdata.filesspace = solv_free(cbdata.filesspace);
382 cbdata.filesspacen = 0;
385 cbdata.hx = cbdata.lookat.elements[i];
386 handle = (*handle_cb)(pool, p, handle_cbdata);
389 rpm_iterate_filelist(handle, iterflags, findfileconflicts2_cb, &cbdata);
391 pend = cbdata.files.count;
392 for (j = i + 2; j < cbdata.lookat.count && cbdata.lookat.elements[j] == hx; j++)
394 int qidx = cbdata.lookat.elements[j + 1];
395 Id q = pkgs->elements[qidx];
396 if (pidx >= cutoff && qidx >= cutoff)
397 continue; /* no conflicts between packages with idx >= cutoff */
399 handle = (*handle_cb)(pool, q, handle_cbdata);
402 rpm_iterate_filelist(handle, iterflags, findfileconflicts2_cb, &cbdata);
403 for (ii = 0; ii < pend; ii++)
404 for (jj = pend; jj < cbdata.files.count; jj++)
406 char *fsi = (char *)cbdata.filesspace + cbdata.files.elements[ii];
407 char *fsj = (char *)cbdata.filesspace + cbdata.files.elements[jj];
408 if (strcmp(fsi + 34, fsj + 34))
409 continue; /* different file names */
410 if (!strcmp(fsi, fsj))
411 continue; /* md5 sum matches */
412 if (pool->obsoleteusescolors && fsi[33] && fsj[33] && (fsi[33] & fsj[33]) == 0)
413 continue; /* colors do not conflict */
414 queue_push(conflicts, pool_str2id(pool, (char *)cbdata.filesspace + cbdata.files.elements[ii] + 34, 1));
415 queue_push(conflicts, p);
416 queue_push(conflicts, pool_str2id(pool, (char *)cbdata.filesspace + cbdata.files.elements[ii], 1));
417 queue_push(conflicts, q);
418 queue_push(conflicts, pool_str2id(pool, (char *)cbdata.filesspace + cbdata.files.elements[jj], 1));
422 cbdata.filesspace = solv_free(cbdata.filesspace);
423 cbdata.filesspacen = 0;
424 queue_free(&cbdata.lookat);
425 queue_free(&cbdata.files);
426 POOL_DEBUG(SOLV_DEBUG_STATS, "candidate check took %d ms\n", solv_timems(now));
427 if (conflicts->count > 5)
428 solv_sort(conflicts->elements, conflicts->count / 5, 5 * sizeof(Id), conflicts_cmp, pool);
429 (*handle_cb)(pool, 0, handle_cbdata);
430 POOL_DEBUG(SOLV_DEBUG_STATS, "found %d file conflicts\n", conflicts->count / 5);
431 POOL_DEBUG(SOLV_DEBUG_STATS, "file conflict detection took %d ms\n", solv_timems(start));
432 return conflicts->count;