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/>.
36 #include "alpm_list.h"
43 pmconflict_t
*_alpm_conflict_new(const char *package1
, const char *package2
, const char *reason
)
45 pmconflict_t
*conflict
;
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
));
58 void _alpm_conflict_free(pmconflict_t
*conflict
)
60 FREE(conflict
->package2
);
61 FREE(conflict
->package1
);
62 FREE(conflict
->reason
);
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
));
78 int _alpm_conflict_isin(pmconflict_t
*needle
, alpm_list_t
*haystack
)
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)) {
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
);
112 match
= _alpm_depcmp(pkg2
, conf
);
114 _alpm_log(PM_LOG_DEBUG
, "package %s conflicts with %s (by %s)\n",
115 pkg1name
, pkg2name
, conflict
);
117 _alpm_dep_free(conf
);
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
);
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
;
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 */
171 if(does_conflict(pkg1
, conflict
, pkg2
)) {
173 add_conflict(baddeps
, pkg1name
, pkg2name
, conflict
);
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
;
190 _alpm_log(PM_LOG_DEBUG
, "check targets vs targets\n");
191 check_conflict(packages
, packages
, &baddeps
, 0);
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
;
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
);
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
;
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] == '/') {
247 } else if(strB
[strlen(strB
)-1] == '/') {
250 int cmp
= strcmp(strA
, strB
);
252 /* item only in filesA, ignore it */
255 /* item only in filesB, ignore it */
258 /* item in both, record it */
259 ret
= alpm_list_add(ret
, strdup(strA
));
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 */
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] == '/') {
285 } else if(strB
[strlen(strB
)-1] == '/') {
288 int cmp
= strcmp(strA
, strB
);
290 /* item only in filesA, record it */
291 ret
= alpm_list_add(ret
, strdup(strA
));
294 /* item only in fileB, but this means nothing */
297 /* item in both, ignore it */
303 /* ensure we have completely emptied pA */
305 const char *strA
= pA
->data
;
306 /* skip directories */
307 if(strA
[strlen(strA
)-1] != '/') {
308 ret
= alpm_list_add(ret
, strdup(strA
));
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
));
332 STRDUP(conflict
->ctarget
, name2
, RET_ERR(PM_ERR_MEMORY
, NULL
));
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)");
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
);
354 static int dir_belongsto_pkg(char *dirpath
, pmpkg_t
*pkg
)
356 struct dirent
*ent
= NULL
;
359 char abspath
[PATH_MAX
];
362 snprintf(abspath
, PATH_MAX
, "%s%s", handle
->root
, dirpath
);
363 dir
= opendir(abspath
);
367 while((ent
= readdir(dir
)) != NULL
) {
368 const char *name
= ent
->d_name
;
370 if(strcmp(name
, ".") == 0 || strcmp(name
, "..") == 0) {
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) {
378 if(S_ISDIR(sbuf
.st_mode
)) {
379 if(dir_belongsto_pkg(path
, pkg
)) {
386 if(alpm_list_find_str(alpm_pkg_get_files(pkg
),path
)) {
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
);
410 if(db
== NULL
|| upgrade
== NULL
|| trans
== 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];
428 int percent
= (current
* 100) / numtargs
;
429 PROGRESS(trans
, PM_TRANS_PROGRESS_CONFLICTS_START
, "", percent
,
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
) {
439 tmpfiles
= chk_fileconflicts(alpm_pkg_get_files(p1
), alpm_pkg_get_files(p2
));
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
));
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 */
463 /* older ver of package currently installed */
464 tmpfiles
= chk_filedifference(alpm_pkg_get_files(p1
),
465 alpm_pkg_get_files(dbpkg
));
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
) {
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) {
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
);
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
);
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
) {
508 if(!p2
|| strcmp(p1
->name
, p2
->name
) == 0) {
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",
531 resolved_conflict
= dir_belongsto_pkg(filestr
, dbpkg
);
536 if(!resolved_conflict
&& dbpkg
) {
537 char *rpath
= calloc(PATH_MAX
+1, sizeof(char));
538 if(!realpath(path
, rpath
)) {
542 char *filestr
= rpath
+ strlen(handle
->root
);
543 if(alpm_list_find_str(alpm_pkg_get_files(dbpkg
),filestr
)) {
544 resolved_conflict
= 1;
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
);
561 const char SYMEXPORT
*alpm_conflict_get_package1(pmconflict_t
*conflict
)
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
)
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
)
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
)
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
)
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
)
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
)
632 ASSERT(handle
!= NULL
, return(NULL
));
633 ASSERT(conflict
!= NULL
, return(NULL
));
635 return conflict
->ctarget
;
637 /* vim: set ts=2 sw=2 noet: */