Read pkgcache into hash
[pacman-ng.git] / lib / libalpm / conflict.c
blob17e728a586dfc953a8e1a0a878676ef10d8244ee
1 /*
2 * conflict.c
4 * Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
6 * Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
7 * Copyright (c) 2006 by David Kimpe <dnaku@frugalware.org>
8 * Copyright (c) 2006 by Miklos Vajna <vmiklos@frugalware.org>
9 * Copyright (c) 2006 by Christian Hamar <krics@linuxforum.hu>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "config.h"
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <limits.h>
31 #include <sys/stat.h>
32 #include <dirent.h>
34 /* libalpm */
35 #include "conflict.h"
36 #include "alpm_list.h"
37 #include "handle.h"
38 #include "trans.h"
39 #include "util.h"
40 #include "log.h"
41 #include "deps.h"
43 pmconflict_t *_alpm_conflict_new(const char *package1, const char *package2, const char *reason)
45 pmconflict_t *conflict;
47 ALPM_LOG_FUNC;
49 MALLOC(conflict, sizeof(pmconflict_t), RET_ERR(PM_ERR_MEMORY, NULL));
51 STRDUP(conflict->package1, package1, RET_ERR(PM_ERR_MEMORY, NULL));
52 STRDUP(conflict->package2, package2, RET_ERR(PM_ERR_MEMORY, NULL));
53 STRDUP(conflict->reason, reason, RET_ERR(PM_ERR_MEMORY, NULL));
55 return(conflict);
58 void _alpm_conflict_free(pmconflict_t *conflict)
60 FREE(conflict->package2);
61 FREE(conflict->package1);
62 FREE(conflict->reason);
63 FREE(conflict);
66 pmconflict_t *_alpm_conflict_dup(const pmconflict_t *conflict)
68 pmconflict_t *newconflict;
69 CALLOC(newconflict, 1, sizeof(pmconflict_t), RET_ERR(PM_ERR_MEMORY, NULL));
71 STRDUP(newconflict->package1, conflict->package1, RET_ERR(PM_ERR_MEMORY, NULL));
72 STRDUP(newconflict->package2, conflict->package2, RET_ERR(PM_ERR_MEMORY, NULL));
73 STRDUP(newconflict->reason, conflict->reason, RET_ERR(PM_ERR_MEMORY, NULL));
75 return(newconflict);
78 int _alpm_conflict_isin(pmconflict_t *needle, alpm_list_t *haystack)
80 alpm_list_t *i;
82 ALPM_LOG_FUNC;
84 for(i = haystack; i; i = i->next) {
85 pmconflict_t *conflict = i->data;
86 char *cpkg1 = conflict->package1;
87 char *cpkg2 = conflict->package2;
88 char *npkg1 = needle->package1;
89 char *npkg2 = needle->package2;
90 if((strcmp(cpkg1, npkg1) == 0 && strcmp(cpkg2, npkg2) == 0)
91 || (strcmp(cpkg1, npkg2) == 0 && strcmp(cpkg2, npkg1) == 0)) {
92 return(1);
96 return(0);
99 /** Check if pkg1 conflicts with pkg2
100 * @param pkg1 package we are looking at
101 * @param conflict name of the possible conflict
102 * @param pkg2 package to check
103 * @return 0 for no conflict, non-zero otherwise
105 static int does_conflict(pmpkg_t *pkg1, const char *conflict, pmpkg_t *pkg2)
107 const char *pkg1name = alpm_pkg_get_name(pkg1);
108 const char *pkg2name = alpm_pkg_get_name(pkg2);
109 pmdepend_t *conf = _alpm_splitdep(conflict);
110 int match = 0;
112 match = _alpm_depcmp(pkg2, conf);
113 if(match) {
114 _alpm_log(PM_LOG_DEBUG, "package %s conflicts with %s (by %s)\n",
115 pkg1name, pkg2name, conflict);
117 _alpm_dep_free(conf);
118 return(match);
121 /** Adds the pkg1/pkg2 conflict to the baddeps list
122 * @param *baddeps list to add conflict to
123 * @param pkg1 first package
124 * @param pkg2 package causing conflict
126 static void add_conflict(alpm_list_t **baddeps, const char *pkg1,
127 const char *pkg2, const char *reason)
129 pmconflict_t *conflict = _alpm_conflict_new(pkg1, pkg2, reason);
130 if(conflict && !_alpm_conflict_isin(conflict, *baddeps)) {
131 *baddeps = alpm_list_add(*baddeps, conflict);
132 } else {
133 _alpm_conflict_free(conflict);
137 /** Check if packages from list1 conflict with packages from list2.
138 * This looks at the conflicts fields of all packages from list1, and sees
139 * if they match packages from list2.
140 * If a conflict (pkg1, pkg2) is found, it is added to the baddeps list
141 * in this order if order >= 0, or reverse order (pkg2,pkg1) otherwise.
143 * @param list1 first list of packages
144 * @param list2 second list of packages
145 * @param *baddeps list to store conflicts
146 * @param order if >= 0 the conflict order is preserved, if < 0 it's reversed
148 static void check_conflict(alpm_list_t *list1, alpm_list_t *list2,
149 alpm_list_t **baddeps, int order) {
150 alpm_list_t *i, *j, *k;
152 if(!baddeps) {
153 return;
155 for(i = list1; i; i = i->next) {
156 pmpkg_t *pkg1 = i->data;
157 const char *pkg1name = alpm_pkg_get_name(pkg1);
159 for(j = alpm_pkg_get_conflicts(pkg1); j; j = j->next) {
160 const char *conflict = j->data;
162 for(k = list2; k; k = k->next) {
163 pmpkg_t *pkg2 = k->data;
164 const char *pkg2name = alpm_pkg_get_name(pkg2);
166 if(strcmp(pkg1name, pkg2name) == 0) {
167 /* skip the package we're currently processing */
168 continue;
171 if(does_conflict(pkg1, conflict, pkg2)) {
172 if(order >= 0) {
173 add_conflict(baddeps, pkg1name, pkg2name, conflict);
174 } else {
175 add_conflict(baddeps, pkg2name, pkg1name, conflict);
183 /* Check for inter-conflicts */
184 alpm_list_t *_alpm_innerconflicts(alpm_list_t *packages)
186 alpm_list_t *baddeps = NULL;
188 ALPM_LOG_FUNC;
190 _alpm_log(PM_LOG_DEBUG, "check targets vs targets\n");
191 check_conflict(packages, packages, &baddeps, 0);
193 return(baddeps);
196 /* Check for target vs (db - target) conflicts
197 * In case of conflict the package1 field of pmdepconflict_t contains
198 * the target package, package2 field contains the local package
200 alpm_list_t *_alpm_outerconflicts(pmdb_t *db, alpm_list_t *packages)
202 alpm_list_t *baddeps = NULL;
204 ALPM_LOG_FUNC;
206 if(db == NULL) {
207 return(NULL);
210 alpm_list_t *dblist = alpm_list_diff(_alpm_db_get_pkgcache_list(db),
211 packages, _alpm_pkg_cmp);
213 /* two checks to be done here for conflicts */
214 _alpm_log(PM_LOG_DEBUG, "check targets vs db\n");
215 check_conflict(packages, dblist, &baddeps, 1);
216 _alpm_log(PM_LOG_DEBUG, "check db vs targets\n");
217 check_conflict(dblist, packages, &baddeps, -1);
219 alpm_list_free(dblist);
220 return(baddeps);
223 /** Check the package conflicts in a database
225 * @param pkglist the list of packages to check
226 * @return an alpm_list_t of pmconflict_t
228 alpm_list_t SYMEXPORT *alpm_checkconflicts(alpm_list_t *pkglist) {
229 return(_alpm_innerconflicts(pkglist));
232 /* Returns a alpm_list_t* of file conflicts.
233 * Hooray for set-intersects!
234 * Pre-condition: both lists are sorted!
236 static alpm_list_t *chk_fileconflicts(alpm_list_t *filesA, alpm_list_t *filesB)
238 alpm_list_t *ret = NULL;
239 alpm_list_t *pA = filesA, *pB = filesB;
241 while(pA && pB) {
242 const char *strA = pA->data;
243 const char *strB = pB->data;
244 /* skip directories, we don't care about dir conflicts */
245 if(strA[strlen(strA)-1] == '/') {
246 pA = pA->next;
247 } else if(strB[strlen(strB)-1] == '/') {
248 pB = pB->next;
249 } else {
250 int cmp = strcmp(strA, strB);
251 if(cmp < 0) {
252 /* item only in filesA, ignore it */
253 pA = pA->next;
254 } else if(cmp > 0) {
255 /* item only in filesB, ignore it */
256 pB = pB->next;
257 } else {
258 /* item in both, record it */
259 ret = alpm_list_add(ret, strdup(strA));
260 pA = pA->next;
261 pB = pB->next;
266 return(ret);
269 /* Returns a alpm_list_t* of files that are in filesA but *NOT* in filesB
270 * This is an 'A minus B' set operation
271 * Pre-condition: both lists are sorted!
273 static alpm_list_t *chk_filedifference(alpm_list_t *filesA, alpm_list_t *filesB)
275 alpm_list_t *ret = NULL;
276 alpm_list_t *pA = filesA, *pB = filesB;
278 /* if both filesA and filesB have entries, do this loop */
279 while(pA && pB) {
280 const char *strA = pA->data;
281 const char *strB = pB->data;
282 /* skip directories, we don't care about dir conflicts */
283 if(strA[strlen(strA)-1] == '/') {
284 pA = pA->next;
285 } else if(strB[strlen(strB)-1] == '/') {
286 pB = pB->next;
287 } else {
288 int cmp = strcmp(strA, strB);
289 if(cmp < 0) {
290 /* item only in filesA, record it */
291 ret = alpm_list_add(ret, strdup(strA));
292 pA = pA->next;
293 } else if(cmp > 0) {
294 /* item only in fileB, but this means nothing */
295 pB = pB->next;
296 } else {
297 /* item in both, ignore it */
298 pA = pA->next;
299 pB = pB->next;
303 /* ensure we have completely emptied pA */
304 while(pA) {
305 const char *strA = pA->data;
306 /* skip directories */
307 if(strA[strlen(strA)-1] != '/') {
308 ret = alpm_list_add(ret, strdup(strA));
310 pA = pA->next;
313 return(ret);
316 /* Adds pmfileconflict_t to a conflicts list. Pass the conflicts list, type (either
317 * PM_FILECONFLICT_TARGET or PM_FILECONFLICT_FILESYSTEM), a file string, and either
318 * two package names or one package name and NULL. This is a wrapper for former
319 * functionality that was done inline.
321 static alpm_list_t *add_fileconflict(alpm_list_t *conflicts,
322 pmfileconflicttype_t type, const char *filestr,
323 const char* name1, const char* name2)
325 pmfileconflict_t *conflict;
326 MALLOC(conflict, sizeof(pmfileconflict_t), RET_ERR(PM_ERR_MEMORY, NULL));
328 conflict->type = type;
329 STRDUP(conflict->target, name1, RET_ERR(PM_ERR_MEMORY, NULL));
330 STRDUP(conflict->file, filestr, RET_ERR(PM_ERR_MEMORY, NULL));
331 if(name2) {
332 STRDUP(conflict->ctarget, name2, RET_ERR(PM_ERR_MEMORY, NULL));
333 } else {
334 conflict->ctarget = "";
337 conflicts = alpm_list_add(conflicts, conflict);
338 _alpm_log(PM_LOG_DEBUG, "found file conflict %s, packages %s and %s\n",
339 filestr, name1, name2 ? name2 : "(filesystem)");
341 return(conflicts);
344 void _alpm_fileconflict_free(pmfileconflict_t *conflict)
346 if(strlen(conflict->ctarget) > 0) {
347 FREE(conflict->ctarget);
349 FREE(conflict->file);;
350 FREE(conflict->target);
351 FREE(conflict);
354 static int dir_belongsto_pkg(char *dirpath, pmpkg_t *pkg)
356 struct dirent *ent = NULL;
357 struct stat sbuf;
358 char path[PATH_MAX];
359 char abspath[PATH_MAX];
360 DIR *dir;
362 snprintf(abspath, PATH_MAX, "%s%s", handle->root, dirpath);
363 dir = opendir(abspath);
364 if(dir == NULL) {
365 return(1);
367 while((ent = readdir(dir)) != NULL) {
368 const char *name = ent->d_name;
370 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
371 continue;
373 snprintf(path, PATH_MAX, "%s/%s", dirpath, name);
374 snprintf(abspath, PATH_MAX, "%s%s", handle->root, path);
375 if(stat(abspath, &sbuf) != 0) {
376 continue;
378 if(S_ISDIR(sbuf.st_mode)) {
379 if(dir_belongsto_pkg(path, pkg)) {
380 continue;
381 } else {
382 closedir(dir);
383 return(0);
385 } else {
386 if(alpm_list_find_str(alpm_pkg_get_files(pkg),path)) {
387 continue;
388 } else {
389 closedir(dir);
390 return(0);
394 closedir(dir);
395 return(1);
398 /* Find file conflicts that may occur during the transaction with two checks:
399 * 1: check every target against every target
400 * 2: check every target against the filesystem */
401 alpm_list_t *_alpm_db_find_fileconflicts(pmdb_t *db, pmtrans_t *trans,
402 alpm_list_t *upgrade, alpm_list_t *remove)
404 alpm_list_t *i, *j, *conflicts = NULL;
405 size_t numtargs = alpm_list_count(upgrade);
406 size_t current;
408 ALPM_LOG_FUNC;
410 if(db == NULL || upgrade == NULL || trans == NULL) {
411 return(NULL);
414 /* TODO this whole function needs a huge change, which hopefully will
415 * be possible with real transactions. Right now we only do half as much
416 * here as we do when we actually extract files in add.c with our 12
417 * different cases. */
418 for(current = 1, i = upgrade; i; i = i->next, current++) {
419 alpm_list_t *k, *tmpfiles = NULL;
420 pmpkg_t *p1, *p2, *dbpkg;
421 char path[PATH_MAX+1];
423 p1 = i->data;
424 if(!p1) {
425 continue;
428 int percent = (current * 100) / numtargs;
429 PROGRESS(trans, PM_TRANS_PROGRESS_CONFLICTS_START, "", percent,
430 numtargs, current);
431 /* CHECK 1: check every target against every target */
432 _alpm_log(PM_LOG_DEBUG, "searching for file conflicts: %s\n",
433 alpm_pkg_get_name(p1));
434 for(j = i->next; j; j = j->next) {
435 p2 = j->data;
436 if(!p2) {
437 continue;
439 tmpfiles = chk_fileconflicts(alpm_pkg_get_files(p1), alpm_pkg_get_files(p2));
441 if(tmpfiles) {
442 for(k = tmpfiles; k; k = k->next) {
443 snprintf(path, PATH_MAX, "%s%s", handle->root, (char *)k->data);
444 conflicts = add_fileconflict(conflicts, PM_FILECONFLICT_TARGET, path,
445 alpm_pkg_get_name(p1), alpm_pkg_get_name(p2));
447 FREELIST(tmpfiles);
451 /* declarations for second check */
452 struct stat lsbuf, sbuf;
453 char *filestr = NULL;
455 /* CHECK 2: check every target against the filesystem */
456 _alpm_log(PM_LOG_DEBUG, "searching for filesystem conflicts: %s\n", p1->name);
457 dbpkg = _alpm_db_get_pkgfromcache(db, p1->name);
459 /* Do two different checks here. If the package is currently installed,
460 * then only check files that are new in the new package. If the package
461 * is not currently installed, then simply stat the whole filelist */
462 if(dbpkg) {
463 /* older ver of package currently installed */
464 tmpfiles = chk_filedifference(alpm_pkg_get_files(p1),
465 alpm_pkg_get_files(dbpkg));
466 } else {
467 /* no version of package currently installed */
468 tmpfiles = alpm_list_strdup(alpm_pkg_get_files(p1));
471 for(j = tmpfiles; j; j = j->next) {
472 filestr = j->data;
474 snprintf(path, PATH_MAX, "%s%s", handle->root, filestr);
476 /* stat the file - if it exists, do some checks */
477 if(_alpm_lstat(path, &lsbuf) != 0) {
478 continue;
480 stat(path, &sbuf);
482 if(path[strlen(path)-1] == '/') {
483 if(S_ISDIR(lsbuf.st_mode)) {
484 _alpm_log(PM_LOG_DEBUG, "%s is a directory, not a conflict\n", path);
485 continue;
486 } else if(S_ISLNK(lsbuf.st_mode) && S_ISDIR(sbuf.st_mode)) {
487 _alpm_log(PM_LOG_DEBUG,
488 "%s is a symlink to a dir, hopefully not a conflict\n", path);
489 continue;
492 _alpm_log(PM_LOG_DEBUG, "checking possible conflict: %s\n", path);
494 int resolved_conflict = 0; /* have we acted on this conflict? */
496 /* Check remove list (will we remove the conflicting local file?) */
497 for(k = remove; k && !resolved_conflict; k = k->next) {
498 pmpkg_t *rempkg = k->data;
499 if(rempkg && alpm_list_find_str(alpm_pkg_get_files(rempkg), filestr)) {
500 _alpm_log(PM_LOG_DEBUG, "local file will be removed, not a conflict: %s\n", filestr);
501 resolved_conflict = 1;
505 /* Look at all the targets to see if file has changed hands */
506 for(k = upgrade; k && !resolved_conflict; k = k->next) {
507 p2 = k->data;
508 if(!p2 || strcmp(p1->name, p2->name) == 0) {
509 continue;
511 pmpkg_t *localp2 = _alpm_db_get_pkgfromcache(db, p2->name);
513 /* localp2->files will be removed (target conflicts are handled by CHECK 1) */
514 if(localp2 && alpm_list_find_str(alpm_pkg_get_files(localp2), filestr)) {
515 /* skip removal of file, but not add. this will prevent a second
516 * package from removing the file when it was already installed
517 * by its new owner (whether the file is in backup array or not */
518 trans->skip_remove = alpm_list_add(trans->skip_remove, strdup(filestr));
519 _alpm_log(PM_LOG_DEBUG, "file changed packages, adding to remove skiplist: %s\n", filestr);
520 resolved_conflict = 1;
524 /* check if all files of the dir belong to the installed pkg */
525 if(!resolved_conflict && S_ISDIR(lsbuf.st_mode) && dbpkg) {
526 char *dir = malloc(strlen(filestr) + 2);
527 sprintf(dir, "%s/", filestr);
528 if(alpm_list_find_str(alpm_pkg_get_files(dbpkg),dir)) {
529 _alpm_log(PM_LOG_DEBUG, "check if all files in %s belongs to %s\n",
530 dir, dbpkg->name);
531 resolved_conflict = dir_belongsto_pkg(filestr, dbpkg);
533 free(dir);
536 if(!resolved_conflict && dbpkg) {
537 char *rpath = calloc(PATH_MAX+1, sizeof(char));
538 if(!realpath(path, rpath)) {
539 FREE(rpath);
540 continue;
542 char *filestr = rpath + strlen(handle->root);
543 if(alpm_list_find_str(alpm_pkg_get_files(dbpkg),filestr)) {
544 resolved_conflict = 1;
546 free(rpath);
549 if(!resolved_conflict) {
550 _alpm_log(PM_LOG_DEBUG, "file found in conflict: %s\n", path);
551 conflicts = add_fileconflict(conflicts, PM_FILECONFLICT_FILESYSTEM,
552 path, p1->name, NULL);
555 FREELIST(tmpfiles);
558 return(conflicts);
561 const char SYMEXPORT *alpm_conflict_get_package1(pmconflict_t *conflict)
563 ALPM_LOG_FUNC;
565 /* Sanity checks */
566 ASSERT(handle != NULL, return(NULL));
567 ASSERT(conflict != NULL, return(NULL));
569 return conflict->package1;
572 const char SYMEXPORT *alpm_conflict_get_package2(pmconflict_t *conflict)
574 ALPM_LOG_FUNC;
576 /* Sanity checks */
577 ASSERT(handle != NULL, return(NULL));
578 ASSERT(conflict != NULL, return(NULL));
580 return conflict->package2;
583 const char SYMEXPORT *alpm_conflict_get_reason(pmconflict_t *conflict)
585 ALPM_LOG_FUNC;
587 /* Sanity checks */
588 ASSERT(handle != NULL, return(NULL));
589 ASSERT(conflict != NULL, return(NULL));
591 return conflict->reason;
594 const char SYMEXPORT *alpm_fileconflict_get_target(pmfileconflict_t *conflict)
596 ALPM_LOG_FUNC;
598 /* Sanity checks */
599 ASSERT(handle != NULL, return(NULL));
600 ASSERT(conflict != NULL, return(NULL));
602 return conflict->target;
605 pmfileconflicttype_t SYMEXPORT alpm_fileconflict_get_type(pmfileconflict_t *conflict)
607 ALPM_LOG_FUNC;
609 /* Sanity checks */
610 ASSERT(handle != NULL, return(-1));
611 ASSERT(conflict != NULL, return(-1));
613 return conflict->type;
616 const char SYMEXPORT *alpm_fileconflict_get_file(pmfileconflict_t *conflict)
618 ALPM_LOG_FUNC;
620 /* Sanity checks */
621 ASSERT(handle != NULL, return(NULL));
622 ASSERT(conflict != NULL, return(NULL));
624 return conflict->file;
627 const char SYMEXPORT *alpm_fileconflict_get_ctarget(pmfileconflict_t *conflict)
629 ALPM_LOG_FUNC;
631 /* Sanity checks */
632 ASSERT(handle != NULL, return(NULL));
633 ASSERT(conflict != NULL, return(NULL));
635 return conflict->ctarget;
637 /* vim: set ts=2 sw=2 noet: */