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 dp = fn + info->dirlen;
594 if (info->diridx != cbdata->lastdiridx)
596 cbdata->lastdiridx = info->diridx;
597 cbdata->lastdirhash = 0;
599 dp = fn + info->dirlen;
604 h = hx & cbdata->cflmapn;
605 hh = HASHCHAIN_START;
608 qx = cbdata->cflmap[2 * h];
613 h = HASHCHAIN_NEXT(h, hh, cbdata->cflmapn);
615 if (!qx || cbdata->cflmap[2 * h + 1] != -1)
617 /* found entry marked as "multiple", recored as conflict candidate */
618 if (!cbdata->lastdirhash)
619 cbdata->lastdirhash = strnhash(fn, dp - fn);
620 dirid = normalizedir(cbdata, fn, dp - fn, cbdata->lastdirhash, 1);
621 queue_push2(&cbdata->lookat, hx, idx);
622 queue_push2(&cbdata->lookat, cbdata->lastdirhash, isdir ? -dirid : dirid);
625 /* turn (hx, idx, dhx, dirid) entries into (hx, idx, off, dirid) entries,
626 * where off is an offset into the filespace block */
628 findfileconflicts_expand_cb(void *cbdatav, const char *fn, struct filelistinfo *info)
630 struct cbdata *cbdata = cbdatav;
640 dp = fn + info->dirlen;
641 if (info->diridx != cbdata->lastdiridx)
643 cbdata->lastdiridx = info->diridx;
644 cbdata->lastdirhash = strnhash(fn, dp - fn);
646 cbdata->lastdiridxbad = MAPTST(&cbdata->fetchdirmap, cbdata->lastdirhash & cbdata->fetchdirmapn) ? 0 : 1;
648 if (cbdata->lastdiridxbad)
653 dhx = cbdata->lastdirhash;
654 dirid = normalizedir(cbdata, fn, dp - fn, dhx, 0);
658 hx = cbdata->lastdirhash;
659 hx = strhash_cont(dp, hx);
665 h = (hx ^ (dirid * 37)) & cbdata->fetchmapn;
666 hh = HASHCHAIN_START;
669 i = cbdata->fetchmap[h];
672 if (cbdata->lookat.elements[i - 1] == hx && cbdata->lookat.elements[i + 2] == dirid && cbdata->lookat.elements[i + 1] == dhx)
674 /* 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); */
675 strncpy(md5padded, info->digest, 32);
677 md5padded[33] = info->color;
678 off = addfilesspace(cbdata, strlen(fn) + (34 + 1));
679 memcpy(cbdata->filesspace + off, (unsigned char *)md5padded, 34);
680 strcpy((char *)cbdata->filesspace + off + 34, fn);
681 queue_push2(&cbdata->lookat, hx, cbdata->idx);
682 queue_push2(&cbdata->lookat, off, dirid);
684 h = HASHCHAIN_NEXT(h, hh, cbdata->fetchmapn);
689 lookat_idx_cmp(const void *ap, const void *bp, void *dp)
691 const Id *a = ap, *b = bp;
692 unsigned int ahx, bhx;
693 if (a[1] - b[1] != 0) /* idx */
695 if (a[3] - b[3] != 0) /* dirid */
697 ahx = (unsigned int)a[0]; /* can be < 0 */
698 bhx = (unsigned int)b[0];
700 return ahx < bhx ? -1 : 1;
701 ahx = (unsigned int)a[2]; /* dhx */
702 bhx = (unsigned int)b[2];
704 return ahx < bhx ? -1 : 1;
709 lookat_hx_cmp(const void *ap, const void *bp, void *dp)
711 const Id *a = ap, *b = bp;
712 unsigned int ahx, bhx;
714 ahx = (unsigned int)a[0]; /* can be < 0 */
715 bhx = (unsigned int)b[0];
717 return ahx < bhx ? -1 : 1;
718 adirid = a[3] < 0 ? -a[3] : a[3];
719 bdirid = b[3] < 0 ? -b[3] : b[3];
720 if (adirid - bdirid != 0) /* dirid */
721 return adirid - bdirid;
723 return a[3] > 0 ? -1 : 1; /* bring positive dirids to front */
724 if (a[1] - b[1] != 0) /* idx */
726 ahx = (unsigned int)a[2]; /* dhx */
727 bhx = (unsigned int)b[2];
729 return ahx < bhx ? -1 : 1;
734 conflicts_cmp(const void *ap, const void *bp, void *dp)
739 if (a[0] != b[0]) /* filename1 */
740 return strcmp(pool_id2str(pool, a[0]), pool_id2str(pool, b[0]));
741 if (a[3] != b[3]) /* filename2 */
742 return strcmp(pool_id2str(pool, a[3]), pool_id2str(pool, b[3]));
743 if (a[1] != b[1]) /* pkgid1 */
745 if (a[4] != b[4]) /* pkgid2 */
751 iterate_solvable_dirs(Pool *pool, Id p, void (*cb)(void *, const char *, struct filelistinfo *), void *cbdata)
753 Repodata *lastdata = 0;
757 dataiterator_init(&di, pool, 0, p, SOLVABLE_FILELIST, 0, SEARCH_COMPLETE_FILELIST);
758 while (dataiterator_step(&di))
760 if (di.data == lastdata && di.kv.id == lastdirid)
763 lastdirid = di.kv.id;
764 cb(cbdata, repodata_dir2str(di.data, di.kv.id, ""), 0);
766 dataiterator_free(&di);
769 /* before calling the expensive findfileconflicts_cb we check if any of
770 * the files match. This only makes sense when cbdata->create is off.
773 precheck_solvable_files(struct cbdata *cbdata, Pool *pool, Id p)
779 int aliases = cbdata->aliases;
780 unsigned int lastdirid = -1;
781 Hashval lastdirhash = 0;
783 int checkthisdir = 0;
784 Repodata *lastrepodata = 0;
786 dataiterator_init(&di, pool, 0, p, SOLVABLE_FILELIST, 0, SEARCH_COMPLETE_FILELIST);
787 while (dataiterator_step(&di))
791 /* hash just the basename */
792 hx = strhash(di.kv.str);
794 hx = strlen(di.kv.str) + 1;
798 /* hash the full path */
799 if (di.data != lastrepodata || di.kv.id != lastdirid)
802 lastrepodata = di.data;
803 lastdirid = di.kv.id;
804 dir = repodata_dir2str(lastrepodata, lastdirid, "");
805 lastdirlen = strlen(dir);
806 lastdirhash = strhash(dir);
807 checkthisdir = isindirmap(cbdata, lastdirhash ? lastdirhash : lastdirlen + 1);
811 hx = strhash_cont(di.kv.str, lastdirhash);
813 hx = lastdirlen + strlen(di.kv.str) + 1;
815 h = hx & cbdata->cflmapn;
816 hh = HASHCHAIN_START;
819 qx = cbdata->cflmap[2 * h];
827 h = HASHCHAIN_NEXT(h, hh, cbdata->cflmapn);
832 dataiterator_free(&di);
837 /* pool_findfileconflicts: find file conflicts in a set of packages
839 * - pkgs: list of packages to check
840 * - cutoff: packages after this are not checked against each other
841 * this is useful to ignore file conflicts in already installed packages
842 * - flags: see pool_fileconflicts.h
843 * - handle_cb, handle_cbdata: callback for rpm header fetches
845 * - conflicts: list of conflicts
847 * This is designed for needing only little memory while still being reasonable fast.
848 * We do this by hashing the file names and working with the 32bit hash values in the
849 * first steps of the algorithm. A hash conflict is not a problem as it will just
850 * lead to some unneeded extra work later on.
854 pool_findfileconflicts(Pool *pool, Queue *pkgs, int cutoff, Queue *conflicts, int flags, void *(*handle_cb)(Pool *, Id, void *) , void *handle_cbdata)
857 struct cbdata cbdata;
858 unsigned int now, start;
860 Repo *installed = pool->installed;
866 queue_empty(conflicts);
870 now = start = solv_timems(0);
871 /* Hmm, should we have a different flag for this? */
872 usefilecolors = pool_get_flag(pool, POOL_FLAG_IMPLICITOBSOLETEUSESCOLORS);
873 POOL_DEBUG(SOLV_DEBUG_STATS, "searching for file conflicts\n");
874 POOL_DEBUG(SOLV_DEBUG_STATS, "packages: %d, cutoff %d, usefilecolors %d\n", pkgs->count, cutoff, usefilecolors);
876 memset(&cbdata, 0, sizeof(cbdata));
877 cbdata.aliases = flags & FINDFILECONFLICTS_CHECK_DIRALIASING;
879 if (cbdata.aliases && (flags & FINDFILECONFLICTS_USE_ROOTDIR) != 0)
881 cbdata.rootdir = pool_get_rootdir(pool);
882 if (cbdata.rootdir && !strcmp(cbdata.rootdir, "/"))
885 cbdata.rootdirl = strlen(cbdata.rootdir);
889 queue_init(&cbdata.lookat);
890 queue_init(&cbdata.lookat_dir);
891 map_init(&cbdata.idxmap, pkgs->count);
894 cutoff = pkgs->count;
896 /* avarage file list size: 200 files per package */
897 /* avarage dir count: 20 dirs per package */
899 /* first pass: find dirs belonging to multiple packages */
903 cbdata.dirmapn = mkmask((cutoff + 3) * 16);
904 cbdata.dirmap = solv_calloc(cbdata.dirmapn + 1, 2 * sizeof(Id));
907 for (i = 0; i < pkgs->count; i++)
912 p = pkgs->elements[i];
913 if ((flags & FINDFILECONFLICTS_USE_SOLVABLEFILELIST) != 0 && installed)
915 if (p >= installed->start && p < installed->end && pool->solvables[p].repo == installed)
917 iterate_solvable_dirs(pool, p, finddirs_cb, &cbdata);
918 if (MAPTST(&cbdata.idxmap, i))
923 handle = (*handle_cb)(pool, p, handle_cbdata);
927 rpm_iterate_filelist(handle, RPM_ITERATE_FILELIST_ONLYDIRS, finddirs_cb, &cbdata);
928 if (MAPTST(&cbdata.idxmap, i))
931 POOL_DEBUG(SOLV_DEBUG_STATS, "dirmap size: %d, used %d\n", cbdata.dirmapn + 1, cbdata.dirmapused);
932 POOL_DEBUG(SOLV_DEBUG_STATS, "dirmap memory usage: %d K\n", (cbdata.dirmapn + 1) * 2 * (int)sizeof(Id) / 1024);
933 POOL_DEBUG(SOLV_DEBUG_STATS, "header fetches: %d\n", hdrfetches);
934 POOL_DEBUG(SOLV_DEBUG_STATS, "dirmap creation took %d ms\n", solv_timems(now));
935 POOL_DEBUG(SOLV_DEBUG_STATS, "dir conflicts found: %d, idxmap %d of %d\n", cbdata.dirconflicts, idxmapset, pkgs->count);
938 /* second pass: scan files in the directories found above */
939 now = solv_timems(0);
940 cbdata.cflmapn = mkmask((cutoff + 3) * 32);
941 cbdata.cflmap = solv_calloc(cbdata.cflmapn + 1, 2 * sizeof(Id));
944 for (i = 0; i < pkgs->count; i++)
948 if (!cbdata.aliases && !MAPTST(&cbdata.idxmap, i))
951 p = pkgs->elements[i];
952 if (!cbdata.create && (flags & FINDFILECONFLICTS_USE_SOLVABLEFILELIST) != 0 && installed)
954 if (p >= installed->start && p < installed->end && pool->solvables[p].repo == installed)
955 if (!precheck_solvable_files(&cbdata, pool, p))
958 /* can't use FINDFILECONFLICTS_USE_SOLVABLEFILELIST because we have to know if
959 * the file is a directory or not */
960 handle = (*handle_cb)(pool, p, handle_cbdata);
964 cbdata.lastdiridx = -1;
965 rpm_iterate_filelist(handle, RPM_ITERATE_FILELIST_NOGHOSTS, cbdata.aliases ? findfileconflicts_basename_cb : findfileconflicts_cb, &cbdata);
968 POOL_DEBUG(SOLV_DEBUG_STATS, "filemap size: %d, used %d\n", cbdata.cflmapn + 1, cbdata.cflmapused);
969 POOL_DEBUG(SOLV_DEBUG_STATS, "filemap memory usage: %d K\n", (cbdata.cflmapn + 1) * 2 * (int)sizeof(Id) / 1024);
970 POOL_DEBUG(SOLV_DEBUG_STATS, "header fetches: %d\n", hdrfetches);
971 POOL_DEBUG(SOLV_DEBUG_STATS, "filemap creation took %d ms\n", solv_timems(now));
972 POOL_DEBUG(SOLV_DEBUG_STATS, "lookat_dir size: %d\n", cbdata.lookat_dir.count);
973 queue_free(&cbdata.lookat_dir);
975 /* we need another pass for aliases to generate the normalized directory ids */
976 queue_init(&cbdata.norq);
979 now = solv_timems(0);
980 addfilesspace(&cbdata, 1); /* make sure the first offset is not zero */
981 cbdata.normapn = mkmask((cutoff + 3) * 4);
982 cbdata.normap = solv_calloc(cbdata.normapn + 1, 2 * sizeof(Id));
985 cbdata.statmapn = cbdata.normapn;
986 cbdata.statmap = solv_calloc(cbdata.statmapn + 1, 2 * sizeof(Id));
990 for (i = 0; i < pkgs->count; i++)
992 if (!MAPTST(&cbdata.idxmap, i))
994 p = pkgs->elements[i];
996 /* can't use FINDFILECONFLICTS_USE_SOLVABLEFILELIST because we have to know if
997 * the file is a directory or not */
998 handle = (*handle_cb)(pool, p, handle_cbdata);
1002 cbdata.lastdiridx = -1;
1003 rpm_iterate_filelist(handle, RPM_ITERATE_FILELIST_NOGHOSTS, findfileconflicts_alias_cb, &cbdata);
1005 POOL_DEBUG(SOLV_DEBUG_STATS, "normap size: %d, used %d\n", cbdata.normapn + 1, cbdata.normapused);
1006 POOL_DEBUG(SOLV_DEBUG_STATS, "normap memory usage: %d K\n", (cbdata.normapn + 1) * 2 * (int)sizeof(Id) / 1024);
1007 POOL_DEBUG(SOLV_DEBUG_STATS, "header fetches: %d\n", hdrfetches);
1008 POOL_DEBUG(SOLV_DEBUG_STATS, "stats made: %d\n", cbdata.statsmade);
1011 POOL_DEBUG(SOLV_DEBUG_STATS, "statmap size: %d, used %d\n", cbdata.statmapn + 1, cbdata.statmapused);
1012 POOL_DEBUG(SOLV_DEBUG_STATS, "statmap memory usage: %d K\n", (cbdata.statmapn + 1) * 2 * (int)sizeof(Id) / 1024);
1014 cbdata.statmap = solv_free(cbdata.statmap);
1015 cbdata.statmapn = 0;
1016 cbdata.canonspace = solv_free(cbdata.canonspace);
1017 cbdata.canonspacen = 0;
1018 POOL_DEBUG(SOLV_DEBUG_STATS, "alias processing took %d ms\n", solv_timems(now));
1021 /* free no longer used stuff */
1022 cbdata.dirmap = solv_free(cbdata.dirmap);
1024 cbdata.dirmapused = 0;
1025 cbdata.cflmap = solv_free(cbdata.cflmap);
1027 cbdata.cflmapused = 0;
1028 map_free(&cbdata.idxmap);
1030 /* sort and unify/prune */
1031 /* this also makes all dirids positive as side effect */
1032 now = solv_timems(0);
1033 POOL_DEBUG(SOLV_DEBUG_STATS, "raw candidates: %d\n", cbdata.lookat.count / 4);
1034 solv_sort(cbdata.lookat.elements, cbdata.lookat.count / 4, sizeof(Id) * 4, &lookat_hx_cmp, pool);
1035 for (i = j = 0; i < cbdata.lookat.count; )
1037 int first = 1, jstart = j;
1038 Id hx = cbdata.lookat.elements[i];
1039 Id idx = cbdata.lookat.elements[i + 1];
1040 Id dhx = cbdata.lookat.elements[i + 2];
1041 Id dirid = cbdata.lookat.elements[i + 3];
1043 for (; i < cbdata.lookat.count && hx == cbdata.lookat.elements[i] && (dirid == cbdata.lookat.elements[i + 3] || dirid == -cbdata.lookat.elements[i + 3]); i += 4)
1045 if (idx == cbdata.lookat.elements[i + 1] && dhx == cbdata.lookat.elements[i + 2])
1047 if (first && idx < cutoff && cbdata.aliases && dirid >= 0)
1048 first = 0; /* special self-conflict case with dhx hash collision, e.g. /foo/xx and /fpf/xx */
1050 continue; /* ignore duplicates */
1055 continue; /* all have a neg dirid */
1056 cbdata.lookat.elements[j++] = hx;
1057 cbdata.lookat.elements[j++] = idx;
1058 cbdata.lookat.elements[j++] = dhx;
1059 cbdata.lookat.elements[j++] = dirid;
1061 if (jstart >= 0 && idx < cutoff)
1064 idx = cbdata.lookat.elements[i + 1];
1065 dhx = cbdata.lookat.elements[i + 2];
1066 cbdata.lookat.elements[j++] = hx;
1067 cbdata.lookat.elements[j++] = idx;
1068 cbdata.lookat.elements[j++] = dhx;
1069 cbdata.lookat.elements[j++] = dirid;
1070 if (jstart >= 0 && idx < cutoff)
1073 if (jstart >= 0) /* we need at least one new candidate */
1076 queue_truncate(&cbdata.lookat, j);
1077 POOL_DEBUG(SOLV_DEBUG_STATS, "pruned candidates: %d\n", cbdata.lookat.count / 4);
1078 POOL_DEBUG(SOLV_DEBUG_STATS, "pruning took %d ms\n", solv_timems(now));
1080 /* third pass: expand to real file names */
1081 now = solv_timems(0);
1082 /* sort by idx so we can do all files of a package in one go */
1083 solv_sort(cbdata.lookat.elements, cbdata.lookat.count / 4, sizeof(Id) * 4, &lookat_idx_cmp, pool);
1085 queue_init(&cbdata.newlookat);
1086 if (cbdata.lookat.count)
1088 /* setup fetch map space */
1089 cbdata.fetchmapn = mkmask(cbdata.lookat.count + 3);
1090 if (cbdata.fetchmapn < 4095)
1091 cbdata.fetchmapn = 4095;
1092 cbdata.fetchmap = solv_calloc(cbdata.fetchmapn + 1, sizeof(Id));
1095 cbdata.fetchdirmapn = ((cbdata.fetchmapn + 1) / 16) - 1;
1096 map_init(&cbdata.fetchdirmap, cbdata.fetchdirmapn + 1);
1099 lookat_cnt = cbdata.lookat.count;
1102 Id idx = cbdata.lookat.elements[1];
1103 int iterflags = RPM_ITERATE_FILELIST_WITHMD5 | RPM_ITERATE_FILELIST_NOGHOSTS;
1105 iterflags |= RPM_ITERATE_FILELIST_WITHCOL;
1106 /* find end of idx block */
1107 for (j = 4; j < lookat_cnt; j += 4)
1108 if (cbdata.lookat.elements[j + 1] != idx)
1110 p = pkgs->elements[idx];
1111 handle = (*handle_cb)(pool, p, handle_cbdata);
1114 queue_deleten(&cbdata.lookat, 0, j);
1119 /* create hash which maps (hx, dirid) to lookat elements */
1120 /* also create map from dhx values for fast reject */
1121 for (i = 0; i < j; i += 4)
1124 h = (cbdata.lookat.elements[i] ^ (cbdata.lookat.elements[i + 3] * 37)) & cbdata.fetchmapn;
1125 hh = HASHCHAIN_START;
1126 while (cbdata.fetchmap[h])
1127 h = HASHCHAIN_NEXT(h, hh, cbdata.fetchmapn);
1128 cbdata.fetchmap[h] = i + 1;
1129 cbdata.lookat.elements[i + 1] = (Id)h; /* hack: misuse idx for easy hash cleanup */
1130 if (cbdata.fetchdirmapn)
1131 MAPSET(&cbdata.fetchdirmap, cbdata.lookat.elements[i + 2] & cbdata.fetchdirmapn);
1134 cbdata.lastdiridx = -1;
1135 cbdata.lastdiridxbad = 0;
1136 queue_prealloc(&cbdata.newlookat, j + 256);
1137 rpm_iterate_filelist(handle, iterflags, findfileconflicts_expand_cb, &cbdata);
1138 /* clear hash and map again */
1139 for (i = 0; i < j; i += 4)
1141 Hashval h = (Hashval)cbdata.lookat.elements[i + 1];
1142 cbdata.fetchmap[h] = 0;
1143 if (cbdata.fetchdirmapn)
1144 MAPCLR_AT(&cbdata.fetchdirmap, cbdata.lookat.elements[i + 2] & cbdata.fetchdirmapn);
1146 /* now delete old block and add new block to the end */
1147 queue_deleten(&cbdata.lookat, 0, j);
1148 queue_insertn(&cbdata.lookat, cbdata.lookat.count, cbdata.newlookat.count, cbdata.newlookat.elements);
1149 queue_empty(&cbdata.newlookat);
1152 queue_free(&cbdata.newlookat);
1153 POOL_DEBUG(SOLV_DEBUG_STATS, "header fetches: %d\n", hdrfetches);
1154 POOL_DEBUG(SOLV_DEBUG_STATS, "candidates now: %d\n", cbdata.lookat.count / 4);
1155 POOL_DEBUG(SOLV_DEBUG_STATS, "file expansion took %d ms\n", solv_timems(now));
1157 cbdata.fetchmap = solv_free(cbdata.fetchmap);
1158 cbdata.fetchmapn = 0;
1159 if (cbdata.fetchdirmapn)
1160 map_free(&cbdata.fetchdirmap);
1161 cbdata.fetchdirmapn = 0;
1162 cbdata.normap = solv_free(cbdata.normap);
1164 queue_free(&cbdata.norq);
1166 /* forth pass: for each (hx,dirid) we have, compare all matching files against all other matching files */
1167 now = solv_timems(0);
1168 solv_sort(cbdata.lookat.elements, cbdata.lookat.count / 4, sizeof(Id) * 4, &lookat_hx_cmp, pool);
1169 for (i = 0; i < cbdata.lookat.count - 4; i += 4)
1171 Id hx = cbdata.lookat.elements[i];
1172 Id dirid = cbdata.lookat.elements[i + 3];
1173 Id idxi = cbdata.lookat.elements[i + 1];
1174 Id offi = cbdata.lookat.elements[i + 2];
1176 continue; /* no conflicts between packages with idx >= cutoff */
1177 for (j = i + 4; j < cbdata.lookat.count && cbdata.lookat.elements[j] == hx && cbdata.lookat.elements[j + 3] == dirid; j += 4)
1179 Id idxj = cbdata.lookat.elements[j + 1];
1180 Id offj = cbdata.lookat.elements[j + 2];
1181 char *fsi = (char *)cbdata.filesspace + offi;
1182 char *fsj = (char *)cbdata.filesspace + offj;
1185 /* compare just the basenames, the dirs match because of the dirid */
1186 char *bsi = strrchr(fsi + 34, '/');
1187 char *bsj = strrchr(fsj + 34, '/');
1190 if (strcmp(bsi, bsj))
1191 continue; /* different base names */
1195 if (strcmp(fsi + 34, fsj + 34))
1196 continue; /* different file names */
1198 if (!strcmp(fsi, fsj))
1199 continue; /* file digests match, no conflict */
1200 if (usefilecolors && fsi[33] && fsj[33] && (fsi[33] & fsj[33]) == 0)
1201 continue; /* colors do not conflict */
1202 queue_push(conflicts, pool_str2id(pool, fsi + 34, 1));
1203 queue_push(conflicts, pkgs->elements[idxi]);
1204 queue_push(conflicts, pool_str2id(pool, fsi, 1));
1205 queue_push(conflicts, pool_str2id(pool, fsj + 34, 1));
1206 queue_push(conflicts, pkgs->elements[idxj]);
1207 queue_push(conflicts, pool_str2id(pool, fsj, 1));
1210 POOL_DEBUG(SOLV_DEBUG_STATS, "filespace size: %d K\n", cbdata.filesspacen / 1024);
1211 POOL_DEBUG(SOLV_DEBUG_STATS, "candidate check took %d ms\n", solv_timems(now));
1212 cbdata.filesspace = solv_free(cbdata.filesspace);
1213 cbdata.filesspacen = 0;
1214 queue_free(&cbdata.lookat);
1215 if (conflicts->count > 6)
1216 solv_sort(conflicts->elements, conflicts->count / 6, 6 * sizeof(Id), conflicts_cmp, pool);
1217 POOL_DEBUG(SOLV_DEBUG_STATS, "found %d file conflicts\n", conflicts->count / 6);
1218 POOL_DEBUG(SOLV_DEBUG_STATS, "file conflict detection took %d ms\n", solv_timems(start));
1220 return conflicts->count / 6;