2 * Copyright (c) 2009-2013, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
15 #include "repo_rpmdb.h"
16 #include "pool_fileconflicts.h"
23 Queue lookat; /* conflict candidates */
24 Queue lookat_dir; /* not yet conflicting directories */
28 unsigned int cflmapused;
32 unsigned int dirmapused;
37 unsigned int lastdiridx; /* last diridx we have seen */
38 unsigned int lastdirhash; /* strhash of last dir we have seen */
41 Id idx; /* index of package we're looking at */
43 unsigned char *filesspace;
44 unsigned int filesspacen;
48 unsigned int normapused;
53 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];
105 /* first pass for non-alias mode:
106 * create hash (dhx, idx) of directories that may have conflicts.
107 * also create map "ixdmap" of packages involved
110 finddirs_cb(void *cbdatav, const char *fn, struct filelistinfo *info)
112 struct cbdata *cbdata = cbdatav;
115 Id oidx, idx = cbdata->idx;
119 dhx = strlen(fn) + 1; /* make sure dhx is not zero */
120 h = dhx & cbdata->dirmapn;
121 hh = HASHCHAIN_START;
124 qx = cbdata->dirmap[2 * h];
129 h = HASHCHAIN_NEXT(h, hh, cbdata->dirmapn);
136 cbdata->dirmap[2 * h] = dhx;
137 cbdata->dirmap[2 * h + 1] = idx;
138 if (++cbdata->dirmapused * 2 > cbdata->dirmapn)
139 cbdata->dirmap = growhash(cbdata->dirmap, &cbdata->dirmapn);
142 /* we saw this dir before */
143 oidx = cbdata->dirmap[2 * h + 1];
146 /* found a conflict, this dir may be used in multiple packages */
149 MAPSET(&cbdata->idxmap, oidx);
150 cbdata->dirmap[2 * h + 1] = -1; /* mark as "multiple packages" */
151 cbdata->dirconflicts++;
153 MAPSET(&cbdata->idxmap, idx);
156 /* check if a dhx value is marked as "multiple" in the dirmap created by finddirs_cb */
158 isindirmap(struct cbdata *cbdata, Id dhx)
163 h = dhx & cbdata->dirmapn;
164 hh = HASHCHAIN_START;
167 qx = cbdata->dirmap[2 * h];
171 return cbdata->dirmap[2 * h + 1] == -1 ? 1 : 0;
172 h = HASHCHAIN_NEXT(h, hh, cbdata->dirmapn);
176 /* collect all possible file conflicts candidates in cbdata->lookat */
177 /* algorithm: hash file name into hx. Use cflmap the check if we have seen
178 * this value before. If yes, we have a file conflict candidate. */
179 /* we also do extra work to ignore all-directory conflicts */
181 findfileconflicts_cb(void *cbdatav, const char *fn, struct filelistinfo *info)
183 struct cbdata *cbdata = cbdatav;
184 int isdir = S_ISDIR(info->mode);
194 dp = fn + info->dirlen;
195 if (info->diridx != cbdata->lastdiridx)
197 cbdata->lastdiridx = info->diridx;
198 cbdata->lastdirhash = strnhash(fn, dp - fn);
200 dhx = cbdata->lastdirhash;
202 /* check if the directory is marked as "multiple" in the dirmap */
203 /* this mirrors the "if (!dhx) dhx = strlen(fn) + 1" used in finddirs_cb */
204 if (!isindirmap(cbdata, dhx ? dhx : dp - fn + 1))
207 hx = strhash_cont(dp, dhx); /* extend hash to complete file name */
209 hx = strlen(fn) + 1; /* make sure hx is not zero */
211 h = hx & cbdata->cflmapn;
212 hh = HASHCHAIN_START;
215 qx = cbdata->cflmap[2 * h];
220 h = HASHCHAIN_NEXT(h, hh, cbdata->cflmapn);
227 cbdata->cflmap[2 * h] = hx;
228 cbdata->cflmap[2 * h + 1] = (isdir ? ~idx : idx);
229 if (++cbdata->cflmapused * 2 > cbdata->cflmapn)
230 cbdata->cflmap = growhash(cbdata->cflmap, &cbdata->cflmapn);
233 /* we have seen this hx before */
234 oidx = cbdata->cflmap[2 * h + 1];
240 /* both are directories. delay the conflict, keep oidx in slot */
241 queue_push2(&cbdata->lookat_dir, hx, idx);
245 /* now have file, had directories before. */
246 cbdata->cflmap[2 * h + 1] = oidx; /* make it a file */
247 /* dump all delayed directory hits for hx */
248 for (i = 0; i < cbdata->lookat_dir.count; i += 2)
249 if (cbdata->lookat_dir.elements[i] == hx)
251 queue_push2(&cbdata->lookat, hx, cbdata->lookat_dir.elements[i + 1]);
252 queue_push2(&cbdata->lookat, 0, 0);
255 else if (oidx == idx)
256 return; /* no conflicts with ourself, please */
257 queue_push2(&cbdata->lookat, hx, oidx);
258 queue_push2(&cbdata->lookat, 0, 0);
259 queue_push2(&cbdata->lookat, hx, idx);
260 queue_push2(&cbdata->lookat, 0, 0);
263 /* same as findfileconflicts_cb, but
264 * - hashes with just the basename
265 * - sets idx in a map instead of pushing to lookat
266 * - sets the hash element to -1 ("multiple") if there may be a conflict
267 * we then use findfileconflicts_alias_cb as second step to generate the candidates.
268 * we do it this way because normailzing file names is expensive and thus we
269 * only want to do it for entries marked as "multiple"
272 findfileconflicts_basename_cb(void *cbdatav, const char *fn, struct filelistinfo *info)
274 struct cbdata *cbdata = cbdatav;
275 int isdir = S_ISDIR(info->mode);
285 dp = fn + info->dirlen;
290 h = hx & cbdata->cflmapn;
291 hh = HASHCHAIN_START;
294 qx = cbdata->cflmap[2 * h];
299 h = HASHCHAIN_NEXT(h, hh, cbdata->cflmapn);
306 cbdata->cflmap[2 * h] = hx;
307 cbdata->cflmap[2 * h + 1] = (isdir ? -idx - 2 : idx);
308 if (++cbdata->cflmapused * 2 > cbdata->cflmapn)
309 cbdata->cflmap = growhash(cbdata->cflmap, &cbdata->cflmapn);
312 oidx = cbdata->cflmap[2 * h + 1];
318 /* both are directories. delay the conflict, keep oidx in slot */
319 queue_push2(&cbdata->lookat_dir, hx, idx);
323 /* now have file, had directories before. */
324 cbdata->cflmap[2 * h + 1] = oidx; /* make it a file */
325 /* dump all delayed directory hits for hx */
326 for (i = 0; i < cbdata->lookat_dir.count; i += 2)
327 if (cbdata->lookat_dir.elements[i] == hx)
328 MAPSET(&cbdata->idxmap, cbdata->lookat_dir.elements[i + 1]);
330 else if (oidx == idx)
331 return; /* no conflicts with ourself, please */
333 MAPSET(&cbdata->idxmap, oidx);
334 MAPSET(&cbdata->idxmap, idx);
336 cbdata->cflmap[2 * h + 1] = -1;
340 addfilesspace(struct cbdata *cbdata, int len)
342 unsigned int off = cbdata->filesspacen;
343 cbdata->filesspace = solv_extend(cbdata->filesspace, cbdata->filesspacen, len, 1, FILESSPACE_BLOCK);
344 cbdata->filesspacen += len;
349 unifywithstat(struct cbdata *cbdata, Id diroff, int dirl)
356 unsigned char statdata[16 + sizeof(stb.st_dev) + sizeof(stb.st_ino)];
358 if (dirl > 1 && cbdata->filesspace[diroff + dirl - 1] == '/')
359 cbdata->filesspace[diroff + dirl - 1] = 0;
361 i = stat((char *)cbdata->filesspace + diroff, &stb);
362 if (dirl > 1 && cbdata->filesspace[diroff + dirl - 1] == 0)
363 cbdata->filesspace[diroff + dirl - 1] = '/';
366 memset(statdata, 0, 16);
367 memcpy(statdata + 8, &stb.st_dev, sizeof(stb.st_dev));
368 memcpy(statdata, &stb.st_ino, sizeof(stb.st_ino));
370 for (i = 15; i >= 0; i--)
371 hx = (unsigned int)hx * 13 + statdata[i];
372 h = hx & cbdata->statmapn;
373 hh = HASHCHAIN_START;
376 qx = cbdata->statmap[2 * h];
381 Id off = cbdata->statmap[2 * h + 1];
382 char *dp = (char *)cbdata->filesspace + cbdata->norq.elements[off];
383 if (!memcmp(dp, statdata, 16))
384 return cbdata->norq.elements[off + 1];
386 h = HASHCHAIN_NEXT(h, hh, cbdata->statmapn);
388 /* new stat result. work. */
389 nspaceoff = addfilesspace(cbdata, 16);
390 memcpy(cbdata->filesspace + nspaceoff, statdata, 16);
391 queue_push2(&cbdata->norq, nspaceoff, nspaceoff);
392 cbdata->statmap[2 * h] = hx;
393 cbdata->statmap[2 * h + 1] = cbdata->norq.count - 2;
394 if (++cbdata->statmapused * 2 > cbdata->statmapn)
395 cbdata->statmap = growhash(cbdata->statmap, &cbdata->statmapn);
399 /* forward declaration */
400 static Id normalizedir(struct cbdata *cbdata, const char *dir, int dirl, Id hx, int create);
403 unifywithcanon(struct cbdata *cbdata, Id diroff, int dirl)
410 printf("UNIFY %.*s\n", dirl, (char *)cbdata->filesspace + diroff);
412 if (!dirl || cbdata->filesspace[diroff] != '/')
415 while (dirl && cbdata->filesspace[diroff + dirl - 1] == '/')
421 for (i = dirl - 1; i > 0; i--)
422 if (cbdata->filesspace[diroff + i] == '/')
424 i++; /* include trailing / */
426 /* normalize dirname */
427 dirnameid = normalizedir(cbdata, (char *)cbdata->filesspace + diroff, i, strnhash((char *)cbdata->filesspace + diroff, i), 1);
429 return diroff; /* hit "in progress" marker, some cyclic link */
431 /* sanity check result */
432 if (cbdata->filesspace[dirnameid] != '/')
433 return diroff; /* hmm */
434 l = strlen((char *)cbdata->filesspace + dirnameid);
435 if (l && cbdata->filesspace[dirnameid + l - 1] != '/')
436 return diroff; /* hmm */
438 /* special handling for "." and ".." basename */
439 if (cbdata->filesspace[diroff + i] == '.')
443 if (dirl - i == 2 && cbdata->filesspace[diroff + i + 1] == '.')
446 return dirnameid; /* we hit our root */
447 for (i = l - 2; i > 0; i--)
448 if (cbdata->filesspace[dirnameid + i] == '/')
450 i++; /* include trailing / */
451 dirnameid = normalizedir(cbdata, (char *)cbdata->filesspace + dirnameid, i, strnhash((char *)cbdata->filesspace + dirnameid, i), 1);
452 return dirnameid == -1 ? diroff : dirnameid;
456 /* append basename to normalized dirname */
457 if (cbdata->rootdirl + l + dirl - i + 1 > cbdata->canonspacen)
459 cbdata->canonspacen = cbdata->rootdirl + l + dirl - i + 20;
460 cbdata->canonspace = solv_realloc(cbdata->canonspace, cbdata->canonspacen);
461 strcpy(cbdata->canonspace, cbdata->rootdir);
463 strcpy(cbdata->canonspace + cbdata->rootdirl, (char *)cbdata->filesspace + dirnameid);
464 strncpy(cbdata->canonspace + cbdata->rootdirl + l, (char *)cbdata->filesspace + diroff + i, dirl - i);
465 cbdata->canonspace[cbdata->rootdirl + l + dirl - i] = 0;
468 printf("stat()ing %s\n", cbdata->canonspace);
471 if (lstat(cbdata->canonspace, &stb) != 0 || !S_ISLNK(stb.st_mode))
473 /* not a symlink or stat failed, have new canon entry */
474 diroff = addfilesspace(cbdata, l + dirl - i + 2);
475 strcpy((char *)cbdata->filesspace + diroff, cbdata->canonspace + cbdata->rootdirl);
478 if (cbdata->filesspace[diroff + l - 1] != '/')
480 cbdata->filesspace[diroff + l++] = '/';
481 cbdata->filesspace[diroff + l] = 0;
483 /* call normalizedir on new entry for unification purposes */
484 dirnameid = normalizedir(cbdata, (char *)cbdata->filesspace + diroff, l, strnhash((char *)cbdata->filesspace + diroff, l), 1);
485 return dirnameid == -1 ? diroff : dirnameid;
487 /* oh no, a symlink! follow */
488 lo = cbdata->rootdirl + l + dirl - i + 1;
489 if (lo + stb.st_size + 2 > cbdata->canonspacen)
491 cbdata->canonspacen = lo + stb.st_size + 20;
492 cbdata->canonspace = solv_realloc(cbdata->canonspace, cbdata->canonspacen);
494 ll = readlink(cbdata->canonspace, cbdata->canonspace + lo, stb.st_size);
495 if (ll < 0 || ll > stb.st_size)
496 return diroff; /* hmm */
498 return dirnameid; /* empty means current dir */
499 if (cbdata->canonspace[lo + ll - 1] != '/')
500 cbdata->canonspace[lo + ll++] = '/'; /* add trailing / */
501 cbdata->canonspace[lo + ll] = 0; /* zero terminate */
502 if (cbdata->canonspace[lo] != '/')
504 /* relative link, concatenate to dirname */
505 memmove(cbdata->canonspace + cbdata->rootdirl + l, cbdata->canonspace + lo, ll + 1);
506 lo = cbdata->rootdirl;
509 dirnameid = normalizedir(cbdata, cbdata->canonspace + lo, ll, strnhash(cbdata->canonspace + lo, ll), 1);
510 return dirnameid == -1 ? diroff : dirnameid;
514 * map a directory (containing a trailing /) into a number.
515 * for unifywithstat this is the offset to the 16 byte stat result.
516 * for unifywithcanon this is the offset to the normailzed dir.
519 normalizedir(struct cbdata *cbdata, const char *dir, int dirl, Id hx, int create)
528 h = hx & cbdata->normapn;
529 hh = HASHCHAIN_START;
532 qx = cbdata->normap[2 * h];
537 Id off = cbdata->normap[2 * h + 1];
538 char *dp = (char *)cbdata->filesspace + cbdata->norq.elements[off];
539 if (!strncmp(dp, dir, dirl) && dp[dirl] == 0)
540 return cbdata->norq.elements[off + 1];
542 h = HASHCHAIN_NEXT(h, hh, cbdata->normapn);
547 if (dir >= (const char *)cbdata->filesspace && dir < (const char *)cbdata->filesspace + cbdata->filesspacen)
549 /* can happen when called from unifywithcanon */
550 Id off = dir - (const char *)cbdata->filesspace;
551 nspaceoff = addfilesspace(cbdata, dirl + 1);
552 dir = (const char *)cbdata->filesspace + off;
555 nspaceoff = addfilesspace(cbdata, dirl + 1);
557 memcpy(cbdata->filesspace + nspaceoff, dir, dirl);
558 cbdata->filesspace[nspaceoff + dirl] = 0;
559 mycnt = cbdata->norq.count;
560 queue_push2(&cbdata->norq, nspaceoff, -1); /* -1: in progress */
561 cbdata->normap[2 * h] = hx;
562 cbdata->normap[2 * h + 1] = mycnt;
563 if (++cbdata->normapused * 2 > cbdata->normapn)
564 cbdata->normap = growhash(cbdata->normap, &cbdata->normapn);
567 nspaceoff = unifywithstat(cbdata, nspaceoff, dirl);
569 nspaceoff = unifywithcanon(cbdata, nspaceoff, dirl);
570 cbdata->norq.elements[mycnt + 1] = nspaceoff; /* patch in result */
572 if (!cbdata->usestat)
573 printf("%s normalized to %d: %s\n", cbdata->filesspace + cbdata->norq.elements[mycnt], nspaceoff, cbdata->filesspace + nspaceoff);
578 /* collect all candidates for cflmap entries marked as "multiple" */
580 findfileconflicts_alias_cb(void *cbdatav, const char *fn, struct filelistinfo *info)
582 int isdir = S_ISDIR(info->mode);
583 struct cbdata *cbdata = cbdatav;
593 if (info->diridx != cbdata->lastdiridx)
595 cbdata->lastdiridx = info->diridx;
596 cbdata->lastdirhash = 0;
598 dp = fn + info->dirlen;
603 h = hx & cbdata->cflmapn;
604 hh = HASHCHAIN_START;
607 qx = cbdata->cflmap[2 * h];
612 h = HASHCHAIN_NEXT(h, hh, cbdata->cflmapn);
614 if (!qx || cbdata->cflmap[2 * h + 1] != -1)
616 /* found entry marked as "multiple", recored as conflict candidate */
617 if (!cbdata->lastdirhash)
618 cbdata->lastdirhash = strnhash(fn, dp - fn);
619 dirid = normalizedir(cbdata, fn, dp - fn, cbdata->lastdirhash, 1);
620 queue_push2(&cbdata->lookat, hx, idx);
621 queue_push2(&cbdata->lookat, cbdata->lastdirhash, isdir ? -dirid : dirid);
624 /* turn (hx, idx, dhx, dirid) entries into (hx, idx, off, dirid) entries,
625 * where off is an offset into the filespace block */
627 findfileconflicts_expand_cb(void *cbdatav, const char *fn, struct filelistinfo *info)
629 struct cbdata *cbdata = cbdatav;
639 dp = fn + info->dirlen;
640 if (info->diridx != cbdata->lastdiridx)
642 cbdata->lastdiridx = info->diridx;
643 cbdata->lastdirhash = strnhash(fn, dp - fn);
645 cbdata->lastdiridxbad = MAPTST(&cbdata->fetchdirmap, cbdata->lastdirhash & cbdata->fetchdirmapn) ? 0 : 1;
647 if (cbdata->lastdiridxbad)
652 dhx = cbdata->lastdirhash;
653 dirid = normalizedir(cbdata, fn, dp - fn, dhx, 0);
657 hx = cbdata->lastdirhash;
658 hx = strhash_cont(dp, hx);
664 h = (hx ^ (dirid * 37)) & cbdata->fetchmapn;
665 hh = HASHCHAIN_START;
668 i = cbdata->fetchmap[h];
671 if (cbdata->lookat.elements[i - 1] == hx && cbdata->lookat.elements[i + 2] == dirid && cbdata->lookat.elements[i + 1] == dhx)
673 /* printf("%d, hx %x dhx %x dirid %d -> %s %d %s %d\n", cbdata->idx, hx, dhx, dirid, fn, info->mode, info->digest, info->color); */
674 strncpy(md5padded, info->digest, 32);
676 md5padded[33] = info->color;
677 off = addfilesspace(cbdata, strlen(fn) + (34 + 1));
678 memcpy(cbdata->filesspace + off, (unsigned char *)md5padded, 34);
679 strcpy((char *)cbdata->filesspace + off + 34, fn);
680 queue_push2(&cbdata->lookat, hx, cbdata->idx);
681 queue_push2(&cbdata->lookat, off, dirid);
683 h = HASHCHAIN_NEXT(h, hh, cbdata->fetchmapn);
688 lookat_idx_cmp(const void *ap, const void *bp, void *dp)
690 const Id *a = ap, *b = bp;
691 unsigned int ahx, bhx;
692 if (a[1] - b[1] != 0) /* idx */
694 if (a[3] - b[3] != 0) /* dirid */
696 ahx = (unsigned int)a[0]; /* can be < 0 */
697 bhx = (unsigned int)b[0];
699 return ahx < bhx ? -1 : 1;
700 ahx = (unsigned int)a[2]; /* dhx */
701 bhx = (unsigned int)b[2];
703 return ahx < bhx ? -1 : 1;
708 lookat_hx_cmp(const void *ap, const void *bp, void *dp)
710 const Id *a = ap, *b = bp;
711 unsigned int ahx, bhx;
713 ahx = (unsigned int)a[0]; /* can be < 0 */
714 bhx = (unsigned int)b[0];
716 return ahx < bhx ? -1 : 1;
717 adirid = a[3] < 0 ? -a[3] : a[3];
718 bdirid = b[3] < 0 ? -b[3] : b[3];
719 if (adirid - bdirid != 0) /* dirid */
720 return adirid - bdirid;
722 return a[3] > 0 ? -1 : 1; /* bring positive dirids to front */
723 if (a[1] - b[1] != 0) /* idx */
725 ahx = (unsigned int)a[2]; /* dhx */
726 bhx = (unsigned int)b[2];
728 return ahx < bhx ? -1 : 1;
733 conflicts_cmp(const void *ap, const void *bp, void *dp)
738 if (a[0] != b[0]) /* filename1 */
739 return strcmp(pool_id2str(pool, a[0]), pool_id2str(pool, b[0]));
740 if (a[3] != b[3]) /* filename2 */
741 return strcmp(pool_id2str(pool, a[3]), pool_id2str(pool, b[3]));
742 if (a[1] != b[1]) /* pkgid1 */
744 if (a[4] != b[4]) /* pkgid2 */
750 iterate_solvable_dirs(Pool *pool, Id p, void (*cb)(void *, const char *, struct filelistinfo *), void *cbdata)
752 Repodata *lastdata = 0;
756 dataiterator_init(&di, pool, 0, p, SOLVABLE_FILELIST, 0, SEARCH_COMPLETE_FILELIST);
757 while (dataiterator_step(&di))
759 if (di.data == lastdata && di.kv.id == lastdirid)
762 lastdirid = di.kv.id;
763 cb(cbdata, repodata_dir2str(di.data, di.kv.id, ""), 0);
765 dataiterator_free(&di);
768 /* before calling the expensive findfileconflicts_cb we check if any of
769 * the files match. This only makes sense when cbdata->create is off.
772 precheck_solvable_files(struct cbdata *cbdata, Pool *pool, Id p)
778 int aliases = cbdata->aliases;
779 unsigned int lastdirid = -1;
780 Hashval lastdirhash = 0;
782 int checkthisdir = 0;
783 Repodata *lastrepodata = 0;
785 dataiterator_init(&di, pool, 0, p, SOLVABLE_FILELIST, 0, SEARCH_COMPLETE_FILELIST);
786 while (dataiterator_step(&di))
790 /* hash just the basename */
791 hx = strhash(di.kv.str);
793 hx = strlen(di.kv.str) + 1;
797 /* hash the full path */
798 if (di.data != lastrepodata || di.kv.id != lastdirid)
801 lastrepodata = di.data;
802 lastdirid = di.kv.id;
803 dir = repodata_dir2str(lastrepodata, lastdirid, "");
804 lastdirlen = strlen(dir);
805 lastdirhash = strhash(dir);
806 checkthisdir = isindirmap(cbdata, lastdirhash ? lastdirhash : lastdirlen + 1);
810 hx = strhash_cont(di.kv.str, lastdirhash);
812 hx = lastdirlen + strlen(di.kv.str) + 1;
814 h = hx & cbdata->cflmapn;
815 hh = HASHCHAIN_START;
818 qx = cbdata->cflmap[2 * h];
826 h = HASHCHAIN_NEXT(h, hh, cbdata->cflmapn);
831 dataiterator_free(&di);
836 /* pool_findfileconflicts: find file conflicts in a set of packages
838 * - pkgs: list of packages to check
839 * - cutoff: packages after this are not checked against each other
840 * this is useful to ignore file conflicts in already installed packages
841 * - flags: see pool_fileconflicts.h
842 * - handle_cb, handle_cbdata: callback for rpm header fetches
844 * - conflicts: list of conflicts
846 * This is designed for needing only little memory while still being reasonable fast.
847 * We do this by hashing the file names and working with the 32bit hash values in the
848 * first steps of the algorithm. A hash conflict is not a problem as it will just
849 * lead to some unneeded extra work later on.
853 pool_findfileconflicts(Pool *pool, Queue *pkgs, int cutoff, Queue *conflicts, int flags, void *(*handle_cb)(Pool *, Id, void *) , void *handle_cbdata)
856 struct cbdata cbdata;
857 unsigned int now, start;
859 Repo *installed = pool->installed;
865 queue_empty(conflicts);
869 now = start = solv_timems(0);
870 /* Hmm, should we have a different flag for this? */
871 usefilecolors = pool_get_flag(pool, POOL_FLAG_IMPLICITOBSOLETEUSESCOLORS);
872 POOL_DEBUG(SOLV_DEBUG_STATS, "searching for file conflicts\n");
873 POOL_DEBUG(SOLV_DEBUG_STATS, "packages: %d, cutoff %d, usefilecolors %d\n", pkgs->count, cutoff, usefilecolors);
875 memset(&cbdata, 0, sizeof(cbdata));
876 cbdata.aliases = flags & FINDFILECONFLICTS_CHECK_DIRALIASING;
878 if (cbdata.aliases && (flags & FINDFILECONFLICTS_USE_ROOTDIR) != 0)
880 cbdata.rootdir = pool_get_rootdir(pool);
881 if (cbdata.rootdir && !strcmp(cbdata.rootdir, "/"))
884 cbdata.rootdirl = strlen(cbdata.rootdir);
888 queue_init(&cbdata.lookat);
889 queue_init(&cbdata.lookat_dir);
890 map_init(&cbdata.idxmap, pkgs->count);
893 cutoff = pkgs->count;
895 /* avarage file list size: 200 files per package */
896 /* avarage dir count: 20 dirs per package */
898 /* first pass: find dirs belonging to multiple packages */
902 cbdata.dirmapn = mkmask((cutoff + 3) * 16);
903 cbdata.dirmap = solv_calloc(cbdata.dirmapn + 1, 2 * sizeof(Id));
906 for (i = 0; i < pkgs->count; i++)
911 p = pkgs->elements[i];
912 if ((flags & FINDFILECONFLICTS_USE_SOLVABLEFILELIST) != 0 && installed)
914 if (p >= installed->start && p < installed->end && pool->solvables[p].repo == installed)
916 iterate_solvable_dirs(pool, p, finddirs_cb, &cbdata);
917 if (MAPTST(&cbdata.idxmap, i))
922 handle = (*handle_cb)(pool, p, handle_cbdata);
926 rpm_iterate_filelist(handle, RPM_ITERATE_FILELIST_ONLYDIRS, finddirs_cb, &cbdata);
927 if (MAPTST(&cbdata.idxmap, i))
930 POOL_DEBUG(SOLV_DEBUG_STATS, "dirmap size: %d, used %d\n", cbdata.dirmapn + 1, cbdata.dirmapused);
931 POOL_DEBUG(SOLV_DEBUG_STATS, "dirmap memory usage: %d K\n", (cbdata.dirmapn + 1) * 2 * (int)sizeof(Id) / 1024);
932 POOL_DEBUG(SOLV_DEBUG_STATS, "header fetches: %d\n", hdrfetches);
933 POOL_DEBUG(SOLV_DEBUG_STATS, "dirmap creation took %d ms\n", solv_timems(now));
934 POOL_DEBUG(SOLV_DEBUG_STATS, "dir conflicts found: %d, idxmap %d of %d\n", cbdata.dirconflicts, idxmapset, pkgs->count);
937 /* second pass: scan files in the directories found above */
938 now = solv_timems(0);
939 cbdata.cflmapn = mkmask((cutoff + 3) * 32);
940 cbdata.cflmap = solv_calloc(cbdata.cflmapn + 1, 2 * sizeof(Id));
943 for (i = 0; i < pkgs->count; i++)
947 if (!cbdata.aliases && !MAPTST(&cbdata.idxmap, i))
950 p = pkgs->elements[i];
951 if (!cbdata.create && (flags & FINDFILECONFLICTS_USE_SOLVABLEFILELIST) != 0 && installed)
953 if (p >= installed->start && p < installed->end && pool->solvables[p].repo == installed)
954 if (!precheck_solvable_files(&cbdata, pool, p))
957 /* can't use FINDFILECONFLICTS_USE_SOLVABLEFILELIST because we have to know if
958 * the file is a directory or not */
959 handle = (*handle_cb)(pool, p, handle_cbdata);
963 cbdata.lastdiridx = -1;
964 rpm_iterate_filelist(handle, RPM_ITERATE_FILELIST_NOGHOSTS, cbdata.aliases ? findfileconflicts_basename_cb : findfileconflicts_cb, &cbdata);
967 POOL_DEBUG(SOLV_DEBUG_STATS, "filemap size: %d, used %d\n", cbdata.cflmapn + 1, cbdata.cflmapused);
968 POOL_DEBUG(SOLV_DEBUG_STATS, "filemap memory usage: %d K\n", (cbdata.cflmapn + 1) * 2 * (int)sizeof(Id) / 1024);
969 POOL_DEBUG(SOLV_DEBUG_STATS, "header fetches: %d\n", hdrfetches);
970 POOL_DEBUG(SOLV_DEBUG_STATS, "filemap creation took %d ms\n", solv_timems(now));
971 POOL_DEBUG(SOLV_DEBUG_STATS, "lookat_dir size: %d\n", cbdata.lookat_dir.count);
972 queue_free(&cbdata.lookat_dir);
974 /* we need another pass for aliases to generate the normalized directory ids */
975 queue_init(&cbdata.norq);
978 now = solv_timems(0);
979 addfilesspace(&cbdata, 1); /* make sure the first offset is not zero */
980 cbdata.normapn = mkmask((cutoff + 3) * 4);
981 cbdata.normap = solv_calloc(cbdata.normapn + 1, 2 * sizeof(Id));
984 cbdata.statmapn = cbdata.normapn;
985 cbdata.statmap = solv_calloc(cbdata.statmapn + 1, 2 * sizeof(Id));
989 for (i = 0; i < pkgs->count; i++)
991 if (!MAPTST(&cbdata.idxmap, i))
993 p = pkgs->elements[i];
995 /* can't use FINDFILECONFLICTS_USE_SOLVABLEFILELIST because we have to know if
996 * the file is a directory or not */
997 handle = (*handle_cb)(pool, p, handle_cbdata);
1001 cbdata.lastdiridx = -1;
1002 rpm_iterate_filelist(handle, RPM_ITERATE_FILELIST_NOGHOSTS, findfileconflicts_alias_cb, &cbdata);
1004 POOL_DEBUG(SOLV_DEBUG_STATS, "normap size: %d, used %d\n", cbdata.normapn + 1, cbdata.normapused);
1005 POOL_DEBUG(SOLV_DEBUG_STATS, "normap memory usage: %d K\n", (cbdata.normapn + 1) * 2 * (int)sizeof(Id) / 1024);
1006 POOL_DEBUG(SOLV_DEBUG_STATS, "header fetches: %d\n", hdrfetches);
1007 POOL_DEBUG(SOLV_DEBUG_STATS, "stats made: %d\n", cbdata.statsmade);
1010 POOL_DEBUG(SOLV_DEBUG_STATS, "statmap size: %d, used %d\n", cbdata.statmapn + 1, cbdata.statmapused);
1011 POOL_DEBUG(SOLV_DEBUG_STATS, "statmap memory usage: %d K\n", (cbdata.statmapn + 1) * 2 * (int)sizeof(Id) / 1024);
1013 cbdata.statmap = solv_free(cbdata.statmap);
1014 cbdata.statmapn = 0;
1015 cbdata.canonspace = solv_free(cbdata.canonspace);
1016 cbdata.canonspacen = 0;
1017 POOL_DEBUG(SOLV_DEBUG_STATS, "alias processing took %d ms\n", solv_timems(now));
1020 /* free no longer used stuff */
1021 cbdata.dirmap = solv_free(cbdata.dirmap);
1023 cbdata.dirmapused = 0;
1024 cbdata.cflmap = solv_free(cbdata.cflmap);
1026 cbdata.cflmapused = 0;
1027 map_free(&cbdata.idxmap);
1029 /* sort and unify/prune */
1030 /* this also makes all dirids positive as side effect */
1031 now = solv_timems(0);
1032 POOL_DEBUG(SOLV_DEBUG_STATS, "raw candidates: %d\n", cbdata.lookat.count / 4);
1033 solv_sort(cbdata.lookat.elements, cbdata.lookat.count / 4, sizeof(Id) * 4, &lookat_hx_cmp, pool);
1034 for (i = j = 0; i < cbdata.lookat.count; )
1036 int first = 1, jstart = j;
1037 Id hx = cbdata.lookat.elements[i];
1038 Id idx = cbdata.lookat.elements[i + 1];
1039 Id dhx = cbdata.lookat.elements[i + 2];
1040 Id dirid = cbdata.lookat.elements[i + 3];
1042 for (; i < cbdata.lookat.count && hx == cbdata.lookat.elements[i] && (dirid == cbdata.lookat.elements[i + 3] || dirid == -cbdata.lookat.elements[i + 3]); i += 4)
1044 if (idx == cbdata.lookat.elements[i + 1] && dhx == cbdata.lookat.elements[i + 2])
1046 if (first && idx < cutoff && cbdata.aliases && dirid >= 0)
1047 first = 0; /* special self-conflict case with dhx hash collision, e.g. /foo/xx and /fpf/xx */
1049 continue; /* ignore duplicates */
1054 continue; /* all have a neg dirid */
1055 cbdata.lookat.elements[j++] = hx;
1056 cbdata.lookat.elements[j++] = idx;
1057 cbdata.lookat.elements[j++] = dhx;
1058 cbdata.lookat.elements[j++] = dirid;
1060 if (jstart >= 0 && idx < cutoff)
1063 idx = cbdata.lookat.elements[i + 1];
1064 dhx = cbdata.lookat.elements[i + 2];
1065 cbdata.lookat.elements[j++] = hx;
1066 cbdata.lookat.elements[j++] = idx;
1067 cbdata.lookat.elements[j++] = dhx;
1068 cbdata.lookat.elements[j++] = dirid;
1069 if (jstart >= 0 && idx < cutoff)
1072 if (jstart >= 0) /* we need at least one new candidate */
1075 queue_truncate(&cbdata.lookat, j);
1076 POOL_DEBUG(SOLV_DEBUG_STATS, "pruned candidates: %d\n", cbdata.lookat.count / 4);
1077 POOL_DEBUG(SOLV_DEBUG_STATS, "pruning took %d ms\n", solv_timems(now));
1079 /* third pass: expand to real file names */
1080 now = solv_timems(0);
1081 /* sort by idx so we can do all files of a package in one go */
1082 solv_sort(cbdata.lookat.elements, cbdata.lookat.count / 4, sizeof(Id) * 4, &lookat_idx_cmp, pool);
1084 queue_init(&cbdata.newlookat);
1085 if (cbdata.lookat.count)
1087 /* setup fetch map space */
1088 cbdata.fetchmapn = mkmask(cbdata.lookat.count + 3);
1089 if (cbdata.fetchmapn < 4095)
1090 cbdata.fetchmapn = 4095;
1091 cbdata.fetchmap = solv_calloc(cbdata.fetchmapn + 1, sizeof(Id));
1094 cbdata.fetchdirmapn = ((cbdata.fetchmapn + 1) / 16) - 1;
1095 map_init(&cbdata.fetchdirmap, cbdata.fetchdirmapn + 1);
1098 lookat_cnt = cbdata.lookat.count;
1101 Id idx = cbdata.lookat.elements[1];
1102 int iterflags = RPM_ITERATE_FILELIST_WITHMD5 | RPM_ITERATE_FILELIST_NOGHOSTS;
1104 iterflags |= RPM_ITERATE_FILELIST_WITHCOL;
1105 /* find end of idx block */
1106 for (j = 4; j < lookat_cnt; j += 4)
1107 if (cbdata.lookat.elements[j + 1] != idx)
1109 p = pkgs->elements[idx];
1110 handle = (*handle_cb)(pool, p, handle_cbdata);
1113 queue_deleten(&cbdata.lookat, 0, j);
1118 /* create hash which maps (hx, dirid) to lookat elements */
1119 /* also create map from dhx values for fast reject */
1120 for (i = 0; i < j; i += 4)
1123 h = (cbdata.lookat.elements[i] ^ (cbdata.lookat.elements[i + 3] * 37)) & cbdata.fetchmapn;
1124 hh = HASHCHAIN_START;
1125 while (cbdata.fetchmap[h])
1126 h = HASHCHAIN_NEXT(h, hh, cbdata.fetchmapn);
1127 cbdata.fetchmap[h] = i + 1;
1128 cbdata.lookat.elements[i + 1] = (Id)h; /* hack: misuse idx for easy hash cleanup */
1129 if (cbdata.fetchdirmapn)
1130 MAPSET(&cbdata.fetchdirmap, cbdata.lookat.elements[i + 2] & cbdata.fetchdirmapn);
1133 cbdata.lastdiridx = -1;
1134 cbdata.lastdiridxbad = 0;
1135 queue_prealloc(&cbdata.newlookat, j + 256);
1136 rpm_iterate_filelist(handle, iterflags, findfileconflicts_expand_cb, &cbdata);
1137 /* clear hash and map again */
1138 for (i = 0; i < j; i += 4)
1140 Hashval h = (Hashval)cbdata.lookat.elements[i + 1];
1141 cbdata.fetchmap[h] = 0;
1142 if (cbdata.fetchdirmapn)
1143 MAPCLR_AT(&cbdata.fetchdirmap, cbdata.lookat.elements[i + 2] & cbdata.fetchdirmapn);
1145 /* now delete old block and add new block to the end */
1146 queue_deleten(&cbdata.lookat, 0, j);
1147 queue_insertn(&cbdata.lookat, cbdata.lookat.count, cbdata.newlookat.count, cbdata.newlookat.elements);
1148 queue_empty(&cbdata.newlookat);
1151 queue_free(&cbdata.newlookat);
1152 POOL_DEBUG(SOLV_DEBUG_STATS, "header fetches: %d\n", hdrfetches);
1153 POOL_DEBUG(SOLV_DEBUG_STATS, "candidates now: %d\n", cbdata.lookat.count / 4);
1154 POOL_DEBUG(SOLV_DEBUG_STATS, "file expansion took %d ms\n", solv_timems(now));
1156 cbdata.fetchmap = solv_free(cbdata.fetchmap);
1157 cbdata.fetchmapn = 0;
1158 if (cbdata.fetchdirmapn)
1159 map_free(&cbdata.fetchdirmap);
1160 cbdata.fetchdirmapn = 0;
1161 cbdata.normap = solv_free(cbdata.normap);
1163 queue_free(&cbdata.norq);
1165 /* forth pass: for each (hx,dirid) we have, compare all matching files against all other matching files */
1166 now = solv_timems(0);
1167 solv_sort(cbdata.lookat.elements, cbdata.lookat.count / 4, sizeof(Id) * 4, &lookat_hx_cmp, pool);
1168 for (i = 0; i < cbdata.lookat.count - 4; i += 4)
1170 Id hx = cbdata.lookat.elements[i];
1171 Id dirid = cbdata.lookat.elements[i + 3];
1172 Id idxi = cbdata.lookat.elements[i + 1];
1173 Id offi = cbdata.lookat.elements[i + 2];
1175 continue; /* no conflicts between packages with idx >= cutoff */
1176 for (j = i + 4; j < cbdata.lookat.count && cbdata.lookat.elements[j] == hx && cbdata.lookat.elements[j + 3] == dirid; j += 4)
1178 Id idxj = cbdata.lookat.elements[j + 1];
1179 Id offj = cbdata.lookat.elements[j + 2];
1180 char *fsi = (char *)cbdata.filesspace + offi;
1181 char *fsj = (char *)cbdata.filesspace + offj;
1184 /* compare just the basenames, the dirs match because of the dirid */
1185 char *bsi = strrchr(fsi + 34, '/');
1186 char *bsj = strrchr(fsj + 34, '/');
1189 if (strcmp(bsi, bsj))
1190 continue; /* different base names */
1194 if (strcmp(fsi + 34, fsj + 34))
1195 continue; /* different file names */
1197 if (!strcmp(fsi, fsj))
1198 continue; /* file digests match, no conflict */
1199 if (usefilecolors && fsi[33] && fsj[33] && (fsi[33] & fsj[33]) == 0)
1200 continue; /* colors do not conflict */
1201 queue_push(conflicts, pool_str2id(pool, fsi + 34, 1));
1202 queue_push(conflicts, pkgs->elements[idxi]);
1203 queue_push(conflicts, pool_str2id(pool, fsi, 1));
1204 queue_push(conflicts, pool_str2id(pool, fsj + 34, 1));
1205 queue_push(conflicts, pkgs->elements[idxj]);
1206 queue_push(conflicts, pool_str2id(pool, fsj, 1));
1209 POOL_DEBUG(SOLV_DEBUG_STATS, "filespace size: %d K\n", cbdata.filesspacen / 1024);
1210 POOL_DEBUG(SOLV_DEBUG_STATS, "candidate check took %d ms\n", solv_timems(now));
1211 cbdata.filesspace = solv_free(cbdata.filesspace);
1212 cbdata.filesspacen = 0;
1213 queue_free(&cbdata.lookat);
1214 if (conflicts->count > 6)
1215 solv_sort(conflicts->elements, conflicts->count / 6, 6 * sizeof(Id), conflicts_cmp, pool);
1216 POOL_DEBUG(SOLV_DEBUG_STATS, "found %d file conflicts\n", conflicts->count / 6);
1217 POOL_DEBUG(SOLV_DEBUG_STATS, "file conflict detection took %d ms\n", solv_timems(start));
1219 return conflicts->count / 6;