2 * Copyright (c) 2009-2013, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
17 #include "repo_rpmdb.h"
18 #include "pool_fileconflicts.h"
25 Queue lookat; /* conflict candidates */
26 Queue lookat_dir; /* not yet conflicting directories */
30 unsigned int cflmapused;
34 unsigned int dirmapused;
39 unsigned int lastdiridx; /* last diridx we have seen */
40 unsigned int lastdirhash; /* strhash of last dir we have seen */
42 Id idx; /* index of package we're looking at */
43 Id hx; /* used in findfileconflicts2_cb, limit to files matching hx */
45 Id dirid; /* used in findfileconflicts2_cb, limit to dirs matching dirid */
46 Id dirhash; /* used in findfileconflicts2_cb, limit to dirs matching dirhash */
49 unsigned char *filesspace;
50 unsigned int filesspacen;
54 unsigned int normapused;
59 unsigned int statmapused;
71 #define FILESSPACE_BLOCK 255
74 growhash(Hashtable map, Hashval *mapnp)
76 Hashval mapn = *mapnp;
77 Hashval newn = (mapn + 1) * 2 - 1;
82 m = solv_calloc(newn + 1, 2 * sizeof(Id));
83 for (i = 0; i <= mapn; i++)
95 h = HASHCHAIN_NEXT(h, hh, newn);
98 m[2 * h + 1] = map[2 * i + 1];
106 finddirs_cb(void *cbdatav, const char *fn, struct filelistinfo *info)
108 struct cbdata *cbdata = cbdatav;
111 Id oidx, idx = cbdata->idx;
116 h = hx & cbdata->dirmapn;
117 hh = HASHCHAIN_START;
120 qx = cbdata->dirmap[2 * h];
125 h = HASHCHAIN_NEXT(h, hh, cbdata->dirmapn);
132 cbdata->dirmap[2 * h] = hx;
133 cbdata->dirmap[2 * h + 1] = idx;
134 if (++cbdata->dirmapused * 2 > cbdata->dirmapn)
135 cbdata->dirmap = growhash(cbdata->dirmap, &cbdata->dirmapn);
138 oidx = cbdata->dirmap[2 * h + 1];
141 /* found a conflict, this dir may be used in multiple packages */
144 MAPSET(&cbdata->idxmap, oidx);
145 cbdata->dirmap[2 * h + 1] = -1;
146 cbdata->dirconflicts++;
148 MAPSET(&cbdata->idxmap, idx);
152 isindirmap(struct cbdata *cbdata, Id hx)
157 h = hx & cbdata->dirmapn;
158 hh = HASHCHAIN_START;
161 qx = cbdata->dirmap[2 * h];
165 return cbdata->dirmap[2 * h + 1] == -1 ? 1 : 0;
166 h = HASHCHAIN_NEXT(h, hh, cbdata->dirmapn);
171 findfileconflicts_cb(void *cbdatav, const char *fn, struct filelistinfo *info)
173 struct cbdata *cbdata = cbdatav;
174 int isdir = S_ISDIR(info->mode);
184 dp = fn + info->dirlen;
185 if (info->diridx != cbdata->lastdiridx)
187 cbdata->lastdiridx = info->diridx;
188 cbdata->lastdirhash = strnhash(fn, dp - fn);
190 dhx = cbdata->lastdirhash;
191 /* this mirrors the "if (!hx) hx = strlen(fn) + 1" in finddirs_cb */
192 if (!isindirmap(cbdata, dhx ? dhx : dp - fn + 1))
194 hx = strhash_cont(dp, dhx);
198 h = hx & cbdata->cflmapn;
199 hh = HASHCHAIN_START;
202 qx = cbdata->cflmap[2 * h];
207 h = HASHCHAIN_NEXT(h, hh, cbdata->cflmapn);
214 cbdata->cflmap[2 * h] = hx;
215 cbdata->cflmap[2 * h + 1] = (isdir ? ~idx : idx);
216 if (++cbdata->cflmapused * 2 > cbdata->cflmapn)
217 cbdata->cflmap = growhash(cbdata->cflmap, &cbdata->cflmapn);
220 oidx = cbdata->cflmap[2 * h + 1];
226 /* both are directories. delay the conflict, keep oidx in slot */
227 queue_push2(&cbdata->lookat_dir, hx, idx);
231 /* now have file, had directories before. */
232 cbdata->cflmap[2 * h + 1] = oidx; /* make it a file */
233 /* dump all delayed directory hits for hx */
234 for (i = 0; i < cbdata->lookat_dir.count; i += 2)
235 if (cbdata->lookat_dir.elements[i] == hx)
237 queue_push2(&cbdata->lookat, hx, cbdata->lookat_dir.elements[i + 1]);
238 queue_push2(&cbdata->lookat, 0, 0);
241 else if (oidx == idx)
242 return; /* no conflicts with ourself, please */
243 queue_push2(&cbdata->lookat, hx, oidx);
244 queue_push2(&cbdata->lookat, 0, 0);
245 queue_push2(&cbdata->lookat, hx, idx);
246 queue_push2(&cbdata->lookat, 0, 0);
249 /* same as findfileconflicts_cb, but
250 * - hashes with just the basename
251 * - sets idx in a map instead of pushing to lookat
252 * - sets the hash element to -1 if there may be a conflict
255 findfileconflicts_basename_cb(void *cbdatav, const char *fn, struct filelistinfo *info)
257 struct cbdata *cbdata = cbdatav;
258 int isdir = S_ISDIR(info->mode);
268 dp = fn + info->dirlen;
273 h = hx & cbdata->cflmapn;
274 hh = HASHCHAIN_START;
277 qx = cbdata->cflmap[2 * h];
282 h = HASHCHAIN_NEXT(h, hh, cbdata->cflmapn);
289 cbdata->cflmap[2 * h] = hx;
290 cbdata->cflmap[2 * h + 1] = (isdir ? -idx - 2 : idx);
291 if (++cbdata->cflmapused * 2 > cbdata->cflmapn)
292 cbdata->cflmap = growhash(cbdata->cflmap, &cbdata->cflmapn);
295 oidx = cbdata->cflmap[2 * h + 1];
301 /* both are directories. delay the conflict, keep oidx in slot */
302 queue_push2(&cbdata->lookat_dir, hx, idx);
306 /* now have file, had directories before. */
307 cbdata->cflmap[2 * h + 1] = oidx; /* make it a file */
308 /* dump all delayed directory hits for hx */
309 for (i = 0; i < cbdata->lookat_dir.count; i += 2)
310 if (cbdata->lookat_dir.elements[i] == hx)
311 MAPSET(&cbdata->idxmap, cbdata->lookat_dir.elements[i + 1]);
313 else if (oidx == idx)
314 return; /* no conflicts with ourself, please */
316 MAPSET(&cbdata->idxmap, oidx);
317 MAPSET(&cbdata->idxmap, idx);
319 cbdata->cflmap[2 * h + 1] = -1;
323 addfilesspace(struct cbdata *cbdata, int len)
325 unsigned int off = cbdata->filesspacen;
326 cbdata->filesspace = solv_extend(cbdata->filesspace, cbdata->filesspacen, len, 1, FILESSPACE_BLOCK);
327 cbdata->filesspacen += len;
332 unifywithstat(struct cbdata *cbdata, Id diroff, int dirl)
339 unsigned char statdata[16 + sizeof(stb.st_dev) + sizeof(stb.st_ino)];
341 if (dirl > 1 && cbdata->filesspace[diroff + dirl - 1] == '/')
342 cbdata->filesspace[diroff + dirl - 1] = 0;
344 i = stat((char *)cbdata->filesspace + diroff, &stb);
345 if (dirl > 1 && cbdata->filesspace[diroff + dirl - 1] == 0)
346 cbdata->filesspace[diroff + dirl - 1] = '/';
349 memset(statdata, 0, 16);
350 memcpy(statdata + 8, &stb.st_dev, sizeof(stb.st_dev));
351 memcpy(statdata, &stb.st_ino, sizeof(stb.st_ino));
353 for (i = 15; i >= 0; i--)
354 hx = (unsigned int)hx * 13 + statdata[i];
355 h = hx & cbdata->statmapn;
356 hh = HASHCHAIN_START;
359 qx = cbdata->statmap[2 * h];
364 Id off = cbdata->statmap[2 * h + 1];
365 const char *dp = (const char *)cbdata->filesspace + cbdata->norq.elements[off];
366 if (!memcmp(dp, (const char *)statdata, 16))
367 return cbdata->norq.elements[off + 1];
369 h = HASHCHAIN_NEXT(h, hh, cbdata->statmapn);
371 /* new stat result. work. */
372 nspaceoff = addfilesspace(cbdata, 16);
373 memcpy(cbdata->filesspace + nspaceoff, statdata, 16);
374 queue_push2(&cbdata->norq, nspaceoff, nspaceoff);
375 cbdata->statmap[2 * h] = hx;
376 cbdata->statmap[2 * h + 1] = cbdata->norq.count - 2;
377 if (++cbdata->statmapused * 2 > cbdata->statmapn)
378 cbdata->statmap = growhash(cbdata->statmap, &cbdata->statmapn);
382 /* forward declaration */
383 static Id normalizedir(struct cbdata *cbdata, const char *dir, int dirl, Id hx, int create);
386 unifywithcanon(struct cbdata *cbdata, Id diroff, int dirl)
393 printf("UNIFY %.*s\n", dirl, (char *)cbdata->filesspace + diroff);
395 if (!dirl || cbdata->filesspace[diroff] != '/')
398 while (dirl && cbdata->filesspace[diroff + dirl - 1] == '/')
403 for (i = dirl - 1; i > 0; i--)
404 if (cbdata->filesspace[diroff + i] == '/')
406 i++; /* include trailing / */
407 dirnameid = normalizedir(cbdata, (char *)cbdata->filesspace + diroff, i, strnhash((char *)cbdata->filesspace + diroff, i), 1);
409 return diroff; /* some cyclic link */
410 if (cbdata->filesspace[dirnameid] != '/')
411 return diroff; /* hmm */
412 l = strlen((char *)cbdata->filesspace + dirnameid);
413 if (l && cbdata->filesspace[dirnameid + l - 1] != '/')
415 /* special handling for '.', '..', '' */
416 if (cbdata->filesspace[diroff + i] == '.')
420 if (dirl - i == 2 && cbdata->filesspace[diroff + i + 1] == '.')
422 dirl = strlen((char *)cbdata->filesspace + dirnameid);
425 for (i = dirl - 2; i > 0; i--)
426 if (cbdata->filesspace[diroff + i] == '/')
428 dirnameid = normalizedir(cbdata, (char *)cbdata->filesspace + dirnameid, i + 1, strnhash((char *)cbdata->filesspace + dirnameid, i + 1), 1);
429 return dirnameid == -1 ? diroff : dirnameid;
432 if (cbdata->rootdirl + l + dirl - i + 1 > cbdata->canonspacen)
434 cbdata->canonspacen = cbdata->rootdirl + l + dirl - i + 20;
435 cbdata->canonspace = solv_realloc(cbdata->canonspace, cbdata->canonspacen);
436 strcpy(cbdata->canonspace, cbdata->rootdir);
438 strcpy(cbdata->canonspace + cbdata->rootdirl, (char *)cbdata->filesspace + dirnameid);
439 strncpy(cbdata->canonspace + cbdata->rootdirl + l, (char *)cbdata->filesspace + diroff + i, dirl - i);
440 cbdata->canonspace[cbdata->rootdirl + l + dirl - i] = 0;
443 printf("stat()ing %s\n", cbdata->canonspace);
445 if (lstat(cbdata->canonspace, &stb))
446 return diroff; /* hmm */
447 if (!S_ISLNK(stb.st_mode))
449 /* not a symlink, have canon entry */
450 diroff = addfilesspace(cbdata, l + dirl - i + 2);
451 strcpy((char *)cbdata->filesspace + diroff, cbdata->canonspace + cbdata->rootdirl);
454 if (cbdata->filesspace[diroff + l - 1] != '/')
456 cbdata->filesspace[diroff + l++] = '/';
457 cbdata->filesspace[diroff + l] = 0;
459 dirnameid = normalizedir(cbdata, (char *)cbdata->filesspace + diroff, l, strnhash((char *)cbdata->filesspace + diroff, l), 1);
460 return dirnameid == -1 ? diroff : dirnameid;
462 /* oh no, a symlink! follow */
463 if (cbdata->rootdirl + l + dirl - i + stb.st_size + 2 > cbdata->canonspacen)
465 cbdata->canonspacen = cbdata->rootdirl + l + dirl - i + stb.st_size + 20;
466 cbdata->canonspace = solv_realloc(cbdata->canonspace, cbdata->canonspacen);
468 lo = cbdata->rootdirl + l + dirl - i + 1;
469 ll = readlink(cbdata->canonspace, cbdata->canonspace + lo, stb.st_size);
470 if (ll < 0 || ll > stb.st_size)
471 return diroff; /* hmm */
474 if (cbdata->canonspace[lo + ll - 1] != '/')
475 cbdata->canonspace[lo + ll++] = '/'; /* add trailing / */
476 cbdata->canonspace[lo + ll] = 0;
477 if (cbdata->canonspace[lo] != '/')
479 /* relative link, concatenate */
480 memmove(cbdata->canonspace + cbdata->rootdirl + l, cbdata->canonspace + lo, ll + 1);
481 lo = cbdata->rootdirl;
484 dirnameid = normalizedir(cbdata, (char *)cbdata->canonspace + lo, ll, strnhash((char *)cbdata->canonspace + lo, ll), 1);
485 return dirnameid == -1 ? diroff : dirnameid;
489 * map a directory (containing a trailing /) into a number.
490 * for unifywithstat this is the offset to the 16 byte stat result.
491 * for unifywithcanon this is the offset to the normailzed dir.
494 normalizedir(struct cbdata *cbdata, const char *dir, int dirl, Id hx, int create)
503 h = hx & cbdata->normapn;
504 hh = HASHCHAIN_START;
507 qx = cbdata->normap[2 * h];
512 Id off = cbdata->normap[2 * h + 1];
513 const char *dp = (const char *)cbdata->filesspace + cbdata->norq.elements[off];
514 if (!strncmp(dp, dir, dirl) && dp[dirl] == 0)
515 return cbdata->norq.elements[off + 1];
517 h = HASHCHAIN_NEXT(h, hh, cbdata->normapn);
522 if (dir >= (const char *)cbdata->filesspace && dir < (const char *)cbdata->filesspace + cbdata->filesspacen)
524 /* can happen when called from unifywithcanon */
525 Id off = dir - (const char *)cbdata->filesspace;
526 nspaceoff = addfilesspace(cbdata, dirl + 1);
527 dir = (const char *)cbdata->filesspace + off;
530 nspaceoff = addfilesspace(cbdata, dirl + 1);
532 memcpy(cbdata->filesspace + nspaceoff, dir, dirl);
533 cbdata->filesspace[nspaceoff + dirl] = 0;
534 mycnt = cbdata->norq.count;
535 queue_push2(&cbdata->norq, nspaceoff, -1);
536 cbdata->normap[2 * h] = hx;
537 cbdata->normap[2 * h + 1] = cbdata->norq.count - 2;
540 nspaceoff = unifywithstat(cbdata, nspaceoff, dirl);
542 nspaceoff = unifywithcanon(cbdata, nspaceoff, dirl);
544 cbdata->norq.elements[mycnt + 1] = nspaceoff;
546 if (!cbdata->usestat)
547 printf("%s normalized to %d: %s\n", cbdata->filesspace + cbdata->norq.elements[mycnt], nspaceoff, cbdata->filesspace + nspaceoff);
549 if (++cbdata->normapused * 2 > cbdata->normapn)
550 cbdata->normap = growhash(cbdata->normap, &cbdata->normapn);
555 findfileconflicts_alias_cb(void *cbdatav, const char *fn, struct filelistinfo *info)
557 int isdir = S_ISDIR(info->mode);
558 struct cbdata *cbdata = cbdatav;
568 dp = fn + info->dirlen;
569 if (info->diridx != cbdata->lastdiridx)
571 cbdata->lastdiridx = info->diridx;
572 cbdata->lastdirhash = 0;
574 dp = fn + info->dirlen;
579 h = hx & cbdata->cflmapn;
580 hh = HASHCHAIN_START;
583 qx = cbdata->cflmap[2 * h];
588 h = HASHCHAIN_NEXT(h, hh, cbdata->cflmapn);
590 if (!qx || cbdata->cflmap[2 * h + 1] != -1)
592 if (!cbdata->lastdirhash)
593 cbdata->lastdirhash = strnhash(fn, dp - fn);
594 dirid = normalizedir(cbdata, fn, dp - fn, cbdata->lastdirhash, 1);
595 queue_push2(&cbdata->lookat, hx, idx);
596 queue_push2(&cbdata->lookat, cbdata->lastdirhash, isdir ? -dirid : dirid);
600 findfileconflicts2_cb(void *cbdatav, const char *fn, struct filelistinfo *info)
602 struct cbdata *cbdata = cbdatav;
610 dp = fn + info->dirlen;
611 if (info->diridx != cbdata->lastdiridx)
613 cbdata->lastdiridx = info->diridx;
614 cbdata->lastdirhash = strnhash(fn, dp - fn);
618 if (cbdata->lastdirhash != cbdata->dirhash)
624 hx = cbdata->lastdirhash;
625 hx = strhash_cont(dp, hx);
629 if ((Id)hx != cbdata->hx)
631 if (cbdata->dirid && cbdata->dirid != normalizedir(cbdata, fn, dp - fn, cbdata->dirhash, 0))
633 strncpy(md5padded, info->digest, 32);
635 md5padded[33] = info->color;
636 /* printf("%d, hx %x -> %s %d %s\n", cbdata->idx, hx, fn, info->mode, info->digest); */
637 off = addfilesspace(cbdata, strlen(fn) + (34 + 1));
638 memcpy(cbdata->filesspace + off, (unsigned char *)md5padded, 34);
639 strcpy((char *)cbdata->filesspace + off + 34, fn);
640 queue_push(&cbdata->files, off);
644 lookat_idx_cmp(const void *ap, const void *bp, void *dp)
646 const Id *a = ap, *b = bp;
647 unsigned int ahx, bhx;
648 if (a[1] - b[1] != 0) /* idx */
650 if (a[3] - b[3] != 0) /* dirid */
652 ahx = (unsigned int)a[0]; /* can be < 0 */
653 bhx = (unsigned int)b[0];
655 return ahx < bhx ? -1 : 1;
656 ahx = (unsigned int)a[2]; /* dhx */
657 bhx = (unsigned int)b[2];
659 return ahx < bhx ? -1 : 1;
664 lookat_hx_cmp(const void *ap, const void *bp, void *dp)
666 const Id *a = ap, *b = bp;
667 unsigned int ahx, bhx;
669 ahx = (unsigned int)a[0]; /* can be < 0 */
670 bhx = (unsigned int)b[0];
672 return ahx < bhx ? -1 : 1;
673 adirid = a[3] < 0 ? -a[3] : a[3];
674 bdirid = b[3] < 0 ? -b[3] : b[3];
675 if (adirid - bdirid != 0) /* dirid */
676 return adirid - bdirid;
678 return a[3] > 0 ? -1 : 1; /* bring positive dirids to front */
679 if (a[1] - b[1] != 0) /* idx */
681 ahx = (unsigned int)a[2]; /* dhx */
682 bhx = (unsigned int)b[2];
684 return ahx < bhx ? -1 : 1;
689 conflicts_cmp(const void *ap, const void *bp, void *dp)
694 if (a[0] != b[0]) /* filename1 */
695 return strcmp(pool_id2str(pool, a[0]), pool_id2str(pool, b[0]));
696 if (a[3] != b[3]) /* filename2 */
697 return strcmp(pool_id2str(pool, a[3]), pool_id2str(pool, b[3]));
698 if (a[1] != b[1]) /* pkgid1 */
700 if (a[4] != b[4]) /* pkgid2 */
706 iterate_solvable_dirs(Pool *pool, Id p, void (*cb)(void *, const char *, struct filelistinfo *), void *cbdata)
708 Repodata *lastdata = 0;
712 dataiterator_init(&di, pool, 0, p, SOLVABLE_FILELIST, 0, SEARCH_COMPLETE_FILELIST);
713 while (dataiterator_step(&di))
715 if (di.data == lastdata && di.kv.id == lastdirid)
718 lastdirid = di.kv.id;
719 cb(cbdata, repodata_dir2str(di.data, di.kv.id, ""), 0);
721 dataiterator_free(&di);
724 /* before calling the expensive findfileconflicts_basename_cb we check if any of
725 * the basenames match. This only makes sense when cbdata->create is off.
728 precheck_solvable_files(struct cbdata *cbdata, Pool *pool, Id p)
735 dataiterator_init(&di, pool, 0, p, SOLVABLE_FILELIST, 0, SEARCH_COMPLETE_FILELIST);
736 while (dataiterator_step(&di))
738 hx = strhash(di.kv.str);
740 hx = strlen(di.kv.str) + 1;
741 h = hx & cbdata->cflmapn;
742 hh = HASHCHAIN_START;
745 qx = cbdata->cflmap[2 * h];
753 h = HASHCHAIN_NEXT(h, hh, cbdata->cflmapn);
758 dataiterator_free(&di);
764 pool_findfileconflicts(Pool *pool, Queue *pkgs, int cutoff, Queue *conflicts, int flags, void *(*handle_cb)(Pool *, Id, void *) , void *handle_cbdata)
766 int i, j, cflmapn, idxmapset;
767 struct cbdata cbdata;
768 unsigned int now, start;
770 Repo *installed = pool->installed;
772 int obsoleteusescolors = pool_get_flag(pool, POOL_FLAG_OBSOLETEUSESCOLORS);
774 queue_empty(conflicts);
778 now = start = solv_timems(0);
779 POOL_DEBUG(SOLV_DEBUG_STATS, "searching for file conflicts\n");
780 POOL_DEBUG(SOLV_DEBUG_STATS, "packages: %d, cutoff %d\n", pkgs->count, cutoff);
782 memset(&cbdata, 0, sizeof(cbdata));
783 cbdata.aliases = flags & FINDFILECONFLICTS_CHECK_DIRALIASING;
785 if (cbdata.aliases && (flags & FINDFILECONFLICTS_USE_ROOTDIR) != 0)
787 cbdata.rootdir = pool_get_rootdir(pool);
788 if (cbdata.rootdir && !strcmp(cbdata.rootdir, "/"))
791 cbdata.rootdirl = strlen(cbdata.rootdir);
795 queue_init(&cbdata.lookat);
796 queue_init(&cbdata.lookat_dir);
797 map_init(&cbdata.idxmap, pkgs->count);
800 cutoff = pkgs->count;
802 /* avarage file list size: 200 files per package */
803 /* avarage dir count: 20 dirs per package */
805 /* first pass: scan dirs */
808 cflmapn = (cutoff + 3) * 64;
809 while ((cflmapn & (cflmapn - 1)) != 0)
810 cflmapn = cflmapn & (cflmapn - 1);
811 cbdata.dirmap = solv_calloc(cflmapn, 2 * sizeof(Id));
812 cbdata.dirmapn = cflmapn - 1; /* make it a mask */
815 for (i = 0; i < pkgs->count; i++)
817 p = pkgs->elements[i];
821 if ((flags & FINDFILECONFLICTS_USE_SOLVABLEFILELIST) != 0 && installed)
823 if (p >= installed->start && p < installed->end && pool->solvables[p].repo == installed)
825 iterate_solvable_dirs(pool, p, finddirs_cb, &cbdata);
826 if (MAPTST(&cbdata.idxmap, i))
831 handle = (*handle_cb)(pool, p, handle_cbdata);
834 rpm_iterate_filelist(handle, RPM_ITERATE_FILELIST_ONLYDIRS, finddirs_cb, &cbdata);
835 if (MAPTST(&cbdata.idxmap, i))
838 POOL_DEBUG(SOLV_DEBUG_STATS, "dirmap size: %d, used %d\n", cbdata.dirmapn + 1, cbdata.dirmapused);
839 POOL_DEBUG(SOLV_DEBUG_STATS, "dirmap memory usage: %d K\n", (cbdata.dirmapn + 1) * 2 * (int)sizeof(Id) / 1024);
840 POOL_DEBUG(SOLV_DEBUG_STATS, "dirmap creation took %d ms\n", solv_timems(now));
841 POOL_DEBUG(SOLV_DEBUG_STATS, "dir conflicts found: %d, idxmap %d of %d\n", cbdata.dirconflicts, idxmapset, pkgs->count);
844 /* second pass: scan files */
845 now = solv_timems(0);
846 cflmapn = (cutoff + 3) * 128;
847 while ((cflmapn & (cflmapn - 1)) != 0)
848 cflmapn = cflmapn & (cflmapn - 1);
849 cbdata.cflmap = solv_calloc(cflmapn, 2 * sizeof(Id));
850 cbdata.cflmapn = cflmapn - 1; /* make it a mask */
852 for (i = 0; i < pkgs->count; i++)
854 if (!cbdata.aliases && !MAPTST(&cbdata.idxmap, i))
856 p = pkgs->elements[i];
860 if (cbdata.aliases && !cbdata.create && FINDFILECONFLICTS_USE_SOLVABLEFILELIST)
862 if (p >= installed->start && p < installed->end && pool->solvables[p].repo == installed)
863 if (!precheck_solvable_files(&cbdata, pool, p))
866 /* can't use FINDFILECONFLICTS_USE_SOLVABLEFILELIST because we have to know if
867 * the file is a directory or not */
868 handle = (*handle_cb)(pool, p, handle_cbdata);
871 cbdata.lastdiridx = -1;
872 rpm_iterate_filelist(handle, RPM_ITERATE_FILELIST_NOGHOSTS, cbdata.aliases ? findfileconflicts_basename_cb : findfileconflicts_cb, &cbdata);
875 POOL_DEBUG(SOLV_DEBUG_STATS, "filemap size: %d, used %d\n", cbdata.cflmapn + 1, cbdata.cflmapused);
876 POOL_DEBUG(SOLV_DEBUG_STATS, "filemap memory usage: %d K\n", (cbdata.cflmapn + 1) * 2 * (int)sizeof(Id) / 1024);
877 POOL_DEBUG(SOLV_DEBUG_STATS, "filemap creation took %d ms\n", solv_timems(now));
878 POOL_DEBUG(SOLV_DEBUG_STATS, "lookat_dir size: %d\n", cbdata.lookat_dir.count);
879 queue_free(&cbdata.lookat_dir);
881 /* we need another pass for aliases */
884 now = solv_timems(0);
885 /* make sure the first offset is not zero */
886 addfilesspace(&cbdata, 1);
887 cflmapn = (cutoff + 3) * 16;
888 while ((cflmapn & (cflmapn - 1)) != 0)
889 cflmapn = cflmapn & (cflmapn - 1);
890 cbdata.normap = solv_calloc(cflmapn, 2 * sizeof(Id));
891 cbdata.normapn = cflmapn - 1; /* make it a mask */
894 cbdata.statmap = solv_calloc(cflmapn, 2 * sizeof(Id));
895 cbdata.statmapn = cflmapn - 1; /* make it a mask */
898 for (i = 0; i < pkgs->count; i++)
900 if (!MAPTST(&cbdata.idxmap, i))
902 p = pkgs->elements[i];
904 /* can't use FINDFILECONFLICTS_USE_SOLVABLEFILELIST because we have to know if
905 * the file is a directory or not */
906 handle = (*handle_cb)(pool, p, handle_cbdata);
909 cbdata.lastdiridx = -1;
910 rpm_iterate_filelist(handle, RPM_ITERATE_FILELIST_NOGHOSTS, findfileconflicts_alias_cb, &cbdata);
912 POOL_DEBUG(SOLV_DEBUG_STATS, "normap size: %d, used %d\n", cbdata.normapn + 1, cbdata.normapused);
913 POOL_DEBUG(SOLV_DEBUG_STATS, "normap memory usage: %d K\n", (cbdata.normapn + 1) * 2 * (int)sizeof(Id) / 1024);
914 POOL_DEBUG(SOLV_DEBUG_STATS, "stats made: %d\n", cbdata.statsmade);
917 POOL_DEBUG(SOLV_DEBUG_STATS, "statmap size: %d, used %d\n", cbdata.statmapn + 1, cbdata.statmapused);
918 POOL_DEBUG(SOLV_DEBUG_STATS, "statmap memory usage: %d K\n", (cbdata.statmapn + 1) * 2 * (int)sizeof(Id) / 1024);
920 cbdata.statmap = solv_free(cbdata.statmap);
922 cbdata.canonspace = solv_free(cbdata.canonspace);
923 cbdata.canonspacen = 0;
924 POOL_DEBUG(SOLV_DEBUG_STATS, "alias processing took %d ms\n", solv_timems(now));
927 cbdata.dirmap = solv_free(cbdata.dirmap);
929 cbdata.dirmapused = 0;
930 cbdata.cflmap = solv_free(cbdata.cflmap);
932 cbdata.cflmapused = 0;
934 now = solv_timems(0);
936 map_free(&cbdata.idxmap);
938 /* sort and unify/prune */
939 POOL_DEBUG(SOLV_DEBUG_STATS, "raw candidates: %d, pruning\n", cbdata.lookat.count / 4);
940 solv_sort(cbdata.lookat.elements, cbdata.lookat.count / 4, sizeof(Id) * 4, &lookat_hx_cmp, pool);
941 for (i = j = 0; i < cbdata.lookat.count; )
944 Id hx = cbdata.lookat.elements[i];
945 Id idx = cbdata.lookat.elements[i + 1];
946 Id dhx = cbdata.lookat.elements[i + 2];
947 Id dirid = cbdata.lookat.elements[i + 3];
949 for (; i < cbdata.lookat.count && hx == cbdata.lookat.elements[i] && (dirid == cbdata.lookat.elements[i + 3] || dirid == -cbdata.lookat.elements[i + 3]); i += 4)
951 if (idx == cbdata.lookat.elements[i + 1] && dhx == cbdata.lookat.elements[i + 2])
952 continue; /* ignore duplicates */
956 continue; /* all have a neg dirid */
957 cbdata.lookat.elements[j++] = hx;
958 cbdata.lookat.elements[j++] = idx;
959 cbdata.lookat.elements[j++] = dhx;
960 cbdata.lookat.elements[j++] = dirid;
963 idx = cbdata.lookat.elements[i + 1];
964 dhx = cbdata.lookat.elements[i + 2];
965 cbdata.lookat.elements[j++] = hx;
966 cbdata.lookat.elements[j++] = idx;
967 cbdata.lookat.elements[j++] = dhx;
968 cbdata.lookat.elements[j++] = dirid;
971 queue_truncate(&cbdata.lookat, j);
972 POOL_DEBUG(SOLV_DEBUG_STATS, "candidates now: %d\n", cbdata.lookat.count / 4);
974 /* third pass: collect file info for all files that match a hx */
975 solv_sort(cbdata.lookat.elements, cbdata.lookat.count / 4, sizeof(Id) * 4, &lookat_idx_cmp, pool);
976 queue_init(&cbdata.files);
977 for (i = 0; i < cbdata.lookat.count; i += 4)
979 Id idx = cbdata.lookat.elements[i + 1];
980 int iterflags = RPM_ITERATE_FILELIST_WITHMD5 | RPM_ITERATE_FILELIST_NOGHOSTS;
981 if (obsoleteusescolors)
982 iterflags |= RPM_ITERATE_FILELIST_WITHCOL;
983 p = pkgs->elements[idx];
984 handle = (*handle_cb)(pool, p, handle_cbdata);
987 int fstart = cbdata.files.count;
988 queue_push(&cbdata.files, idx);
989 queue_push(&cbdata.files, 0);
991 cbdata.hx = cbdata.lookat.elements[i];
992 cbdata.dirhash = cbdata.lookat.elements[i + 2];
993 cbdata.dirid = cbdata.lookat.elements[i + 3];
994 cbdata.lastdiridx = -1;
996 rpm_iterate_filelist(handle, iterflags, findfileconflicts2_cb, &cbdata);
997 cbdata.files.elements[fstart + 1] = cbdata.files.count;
998 cbdata.lookat.elements[i + 1] = fstart;
999 if (i + 4 >= cbdata.lookat.count || cbdata.lookat.elements[i + 4 + 1] != idx)
1004 cbdata.normap = solv_free(cbdata.normap);
1007 /* forth pass: for each hx we have, compare all matching files against all other matching files */
1008 solv_sort(cbdata.lookat.elements, cbdata.lookat.count / 4, sizeof(Id) * 4, &lookat_hx_cmp, pool);
1009 for (i = 0; i < cbdata.lookat.count - 4; i += 4)
1011 Id hx = cbdata.lookat.elements[i];
1012 Id pstart = cbdata.lookat.elements[i + 1];
1013 Id dirid = cbdata.lookat.elements[i + 3];
1014 Id pidx = cbdata.files.elements[pstart];
1015 Id pend = cbdata.files.elements[pstart + 1];
1016 if (cbdata.lookat.elements[i + 4] != hx)
1017 continue; /* no package left with that hx */
1018 for (j = i + 4; j < cbdata.lookat.count && cbdata.lookat.elements[j] == hx && cbdata.lookat.elements[j + 3] == dirid; j += 4)
1020 Id qstart = cbdata.lookat.elements[j + 1];
1021 Id qidx = cbdata.files.elements[qstart];
1022 Id qend = cbdata.files.elements[qstart + 1];
1024 if (pidx >= cutoff && qidx >= cutoff)
1025 continue; /* no conflicts between packages with idx >= cutoff */
1026 for (ii = pstart + 2; ii < pend; ii++)
1027 for (jj = qstart + 2; jj < qend; jj++)
1029 char *fsi = (char *)cbdata.filesspace + cbdata.files.elements[ii];
1030 char *fsj = (char *)cbdata.filesspace + cbdata.files.elements[jj];
1033 /* compare just the basenames, the dirs match */
1034 char *bsi = strrchr(fsi + 34, '/');
1035 char *bsj = strrchr(fsj + 34, '/');
1036 if ((!bsi || !bsj) && bsi != bsj)
1038 if (strcmp(bsi, bsj))
1039 continue; /* different file names */
1043 if (strcmp(fsi + 34, fsj + 34))
1044 continue; /* different file names */
1046 if (!strcmp(fsi, fsj))
1047 continue; /* md5 sum matches */
1048 if (obsoleteusescolors && fsi[33] && fsj[33] && (fsi[33] & fsj[33]) == 0)
1049 continue; /* colors do not conflict */
1050 queue_push(conflicts, pool_str2id(pool, fsi + 34, 1));
1051 queue_push(conflicts, pkgs->elements[pidx]);
1052 queue_push(conflicts, pool_str2id(pool, fsi, 1));
1053 queue_push(conflicts, pool_str2id(pool, fsj + 34, 1));
1054 queue_push(conflicts, pkgs->elements[qidx]);
1055 queue_push(conflicts, pool_str2id(pool, fsj, 1));
1059 POOL_DEBUG(SOLV_DEBUG_STATS, "filespace size: %d K\n", cbdata.filesspacen / 1024);
1060 POOL_DEBUG(SOLV_DEBUG_STATS, "candidate check took %d ms\n", solv_timems(now));
1061 cbdata.filesspace = solv_free(cbdata.filesspace);
1062 cbdata.filesspacen = 0;
1063 queue_free(&cbdata.lookat);
1064 queue_free(&cbdata.files);
1065 if (conflicts->count > 6)
1066 solv_sort(conflicts->elements, conflicts->count / 6, 6 * sizeof(Id), conflicts_cmp, pool);
1067 POOL_DEBUG(SOLV_DEBUG_STATS, "found %d file conflicts\n", conflicts->count / 6);
1068 POOL_DEBUG(SOLV_DEBUG_STATS, "file conflict detection took %d ms\n", solv_timems(start));
1070 return conflicts->count;