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 static pmconflict_t
*conflict_new(const char *package1
, const char *package2
,
46 pmconflict_t
*conflict
;
48 MALLOC(conflict
, sizeof(pmconflict_t
), return NULL
);
50 STRDUP(conflict
->package1
, package1
, return NULL
);
51 STRDUP(conflict
->package2
, package2
, return NULL
);
52 STRDUP(conflict
->reason
, reason
, return NULL
);
57 void _alpm_conflict_free(pmconflict_t
*conflict
)
59 FREE(conflict
->package2
);
60 FREE(conflict
->package1
);
61 FREE(conflict
->reason
);
65 pmconflict_t
*_alpm_conflict_dup(const pmconflict_t
*conflict
)
67 pmconflict_t
*newconflict
;
68 CALLOC(newconflict
, 1, sizeof(pmconflict_t
), );
70 STRDUP(newconflict
->package1
, conflict
->package1
, return NULL
);
71 STRDUP(newconflict
->package2
, conflict
->package2
, return NULL
);
72 STRDUP(newconflict
->reason
, conflict
->reason
, return NULL
);
77 static int conflict_isin(pmconflict_t
*needle
, alpm_list_t
*haystack
)
80 const char *npkg1
= needle
->package1
;
81 const char *npkg2
= needle
->package2
;
83 for(i
= haystack
; i
; i
= i
->next
) {
84 pmconflict_t
*conflict
= i
->data
;
85 const char *cpkg1
= conflict
->package1
;
86 const char *cpkg2
= conflict
->package2
;
87 if((strcmp(cpkg1
, npkg1
) == 0 && strcmp(cpkg2
, npkg2
) == 0)
88 || (strcmp(cpkg1
, npkg2
) == 0 && strcmp(cpkg2
, npkg1
) == 0)) {
96 /** Adds the pkg1/pkg2 conflict to the baddeps list.
97 * @param handle the context handle
98 * @param baddeps list to add conflict to
99 * @param pkg1 first package
100 * @param pkg2 package causing conflict
101 * @param reason reason for this conflict
103 static int add_conflict(alpm_handle_t
*handle
, alpm_list_t
**baddeps
,
104 const char *pkg1
, const char *pkg2
, const char *reason
)
106 pmconflict_t
*conflict
= conflict_new(pkg1
, pkg2
, reason
);
110 _alpm_log(handle
, PM_LOG_DEBUG
, "package %s conflicts with %s (by %s)\n",
112 if(!conflict_isin(conflict
, *baddeps
)) {
113 *baddeps
= alpm_list_add(*baddeps
, conflict
);
115 _alpm_conflict_free(conflict
);
120 /** Check if packages from list1 conflict with packages from list2.
121 * This looks at the conflicts fields of all packages from list1, and sees
122 * if they match packages from list2.
123 * If a conflict (pkg1, pkg2) is found, it is added to the baddeps list
124 * in this order if order >= 0, or reverse order (pkg2,pkg1) otherwise.
126 * @param handle the context handle
127 * @param list1 first list of packages
128 * @param list2 second list of packages
129 * @param baddeps list to store conflicts
130 * @param order if >= 0 the conflict order is preserved, if < 0 it's reversed
132 static void check_conflict(alpm_handle_t
*handle
,
133 alpm_list_t
*list1
, alpm_list_t
*list2
,
134 alpm_list_t
**baddeps
, int order
) {
140 for(i
= list1
; i
; i
= i
->next
) {
141 alpm_pkg_t
*pkg1
= i
->data
;
142 const char *pkg1name
= alpm_pkg_get_name(pkg1
);
145 for(j
= alpm_pkg_get_conflicts(pkg1
); j
; j
= j
->next
) {
146 const char *conflict
= j
->data
;
148 alpm_depend_t
*parsed_conflict
= _alpm_splitdep(conflict
);
150 for(k
= list2
; k
; k
= k
->next
) {
151 alpm_pkg_t
*pkg2
= k
->data
;
152 const char *pkg2name
= alpm_pkg_get_name(pkg2
);
154 if(strcmp(pkg1name
, pkg2name
) == 0) {
155 /* skip the package we're currently processing */
159 if(_alpm_depcmp(pkg2
, parsed_conflict
)) {
161 add_conflict(handle
, baddeps
, pkg1name
, pkg2name
, conflict
);
163 add_conflict(handle
, baddeps
, pkg2name
, pkg1name
, conflict
);
167 _alpm_dep_free(parsed_conflict
);
172 /* Check for inter-conflicts */
173 alpm_list_t
*_alpm_innerconflicts(alpm_handle_t
*handle
, alpm_list_t
*packages
)
175 alpm_list_t
*baddeps
= NULL
;
177 _alpm_log(handle
, PM_LOG_DEBUG
, "check targets vs targets\n");
178 check_conflict(handle
, packages
, packages
, &baddeps
, 0);
183 /* Check for target vs (db - target) conflicts
184 * In case of conflict the package1 field of pmdepconflict_t contains
185 * the target package, package2 field contains the local package
187 alpm_list_t
*_alpm_outerconflicts(alpm_db_t
*db
, alpm_list_t
*packages
)
189 alpm_list_t
*baddeps
= NULL
;
195 alpm_list_t
*dblist
= alpm_list_diff(_alpm_db_get_pkgcache(db
),
196 packages
, _alpm_pkg_cmp
);
198 /* two checks to be done here for conflicts */
199 _alpm_log(db
->handle
, PM_LOG_DEBUG
, "check targets vs db\n");
200 check_conflict(db
->handle
, packages
, dblist
, &baddeps
, 1);
201 _alpm_log(db
->handle
, PM_LOG_DEBUG
, "check db vs targets\n");
202 check_conflict(db
->handle
, dblist
, packages
, &baddeps
, -1);
204 alpm_list_free(dblist
);
208 /** Check the package conflicts in a database
210 * @param handle the context handle
211 * @param pkglist the list of packages to check
212 * @return an alpm_list_t of pmconflict_t
214 alpm_list_t SYMEXPORT
*alpm_checkconflicts(alpm_handle_t
*handle
,
215 alpm_list_t
*pkglist
)
217 CHECK_HANDLE(handle
, return NULL
);
218 return _alpm_innerconflicts(handle
, pkglist
);
221 static const int DIFFERENCE
= 0;
222 static const int INTERSECT
= 1;
223 /* Returns a set operation on the provided two lists of files.
224 * Pre-condition: both lists are sorted!
227 * DIFFERENCE - a difference operation is performed. filesA - filesB.
228 * INTERSECT - an intersection operation is performed. filesA & filesB.
230 static alpm_list_t
*filelist_operation(alpm_list_t
*filesA
, alpm_list_t
*filesB
,
233 alpm_list_t
*ret
= NULL
;
234 alpm_list_t
*pA
= filesA
, *pB
= filesB
;
237 const char *strA
= pA
->data
;
238 const char *strB
= pB
->data
;
239 /* skip directories, we don't care about them */
240 if(strA
[strlen(strA
)-1] == '/') {
242 } else if(strB
[strlen(strB
)-1] == '/') {
245 int cmp
= strcmp(strA
, strB
);
247 if(operation
== DIFFERENCE
) {
248 /* item only in filesA, qualifies as a difference */
249 ret
= alpm_list_add(ret
, strdup(strA
));
255 if(operation
== INTERSECT
) {
256 /* item in both, qualifies as an intersect */
257 ret
= alpm_list_add(ret
, strdup(strA
));
265 /* if doing a difference, ensure we have completely emptied pA */
266 while(operation
== DIFFERENCE
&& pA
) {
267 const char *strA
= pA
->data
;
268 /* skip directories */
269 if(strA
[strlen(strA
)-1] != '/') {
270 ret
= alpm_list_add(ret
, strdup(strA
));
278 /* Adds pmfileconflict_t to a conflicts list. Pass the conflicts list, type
279 * (either PM_FILECONFLICT_TARGET or PM_FILECONFLICT_FILESYSTEM), a file
280 * string, and either two package names or one package name and NULL. This is
281 * a wrapper for former functionality that was done inline.
283 static alpm_list_t
*add_fileconflict(alpm_handle_t
*handle
,
284 alpm_list_t
*conflicts
, alpm_fileconflicttype_t type
, const char *filestr
,
285 const char *name1
, const char *name2
)
287 pmfileconflict_t
*conflict
;
288 MALLOC(conflict
, sizeof(pmfileconflict_t
), goto error
);
290 conflict
->type
= type
;
291 STRDUP(conflict
->target
, name1
, goto error
);
292 STRDUP(conflict
->file
, filestr
, goto error
);
294 STRDUP(conflict
->ctarget
, name2
, goto error
);
296 STRDUP(conflict
->ctarget
, "", goto error
);
299 conflicts
= alpm_list_add(conflicts
, conflict
);
300 _alpm_log(handle
, PM_LOG_DEBUG
, "found file conflict %s, packages %s and %s\n",
301 filestr
, name1
, name2
? name2
: "(filesystem)");
306 RET_ERR(handle
, PM_ERR_MEMORY
, conflicts
);
309 void _alpm_fileconflict_free(pmfileconflict_t
*conflict
)
311 FREE(conflict
->ctarget
);
312 FREE(conflict
->file
);
313 FREE(conflict
->target
);
317 static int dir_belongsto_pkg(const char *root
, const char *dirpath
,
320 struct dirent
*ent
= NULL
;
323 char abspath
[PATH_MAX
];
326 snprintf(abspath
, PATH_MAX
, "%s%s", root
, dirpath
);
327 dir
= opendir(abspath
);
331 while((ent
= readdir(dir
)) != NULL
) {
332 const char *name
= ent
->d_name
;
334 if(strcmp(name
, ".") == 0 || strcmp(name
, "..") == 0) {
337 snprintf(path
, PATH_MAX
, "%s/%s", dirpath
, name
);
338 snprintf(abspath
, PATH_MAX
, "%s%s", root
, path
);
339 if(stat(abspath
, &sbuf
) != 0) {
342 if(S_ISDIR(sbuf
.st_mode
)) {
343 if(dir_belongsto_pkg(root
, path
, pkg
)) {
350 if(alpm_list_find_str(alpm_pkg_get_files(pkg
), path
)) {
362 /* Find file conflicts that may occur during the transaction with two checks:
363 * 1: check every target against every target
364 * 2: check every target against the filesystem */
365 alpm_list_t
*_alpm_db_find_fileconflicts(alpm_handle_t
*handle
,
366 alpm_list_t
*upgrade
, alpm_list_t
*remove
)
368 alpm_list_t
*i
, *j
, *conflicts
= NULL
;
369 size_t numtargs
= alpm_list_count(upgrade
);
371 alpm_trans_t
*trans
= handle
->trans
;
377 /* TODO this whole function needs a huge change, which hopefully will
378 * be possible with real transactions. Right now we only do half as much
379 * here as we do when we actually extract files in add.c with our 12
380 * different cases. */
381 for(current
= 0, i
= upgrade
; i
; i
= i
->next
, current
++) {
382 alpm_list_t
*k
, *tmpfiles
;
383 alpm_pkg_t
*p1
, *p2
, *dbpkg
;
391 int percent
= (current
* 100) / numtargs
;
392 PROGRESS(trans
, PM_TRANS_PROGRESS_CONFLICTS_START
, "", percent
,
394 /* CHECK 1: check every target against every target */
395 _alpm_log(handle
, PM_LOG_DEBUG
, "searching for file conflicts: %s\n",
396 alpm_pkg_get_name(p1
));
397 for(j
= i
->next
; j
; j
= j
->next
) {
398 alpm_list_t
*common_files
;
403 common_files
= filelist_operation(alpm_pkg_get_files(p1
),
404 alpm_pkg_get_files(p2
), INTERSECT
);
407 for(k
= common_files
; k
; k
= k
->next
) {
408 snprintf(path
, PATH_MAX
, "%s%s", handle
->root
, (char *)k
->data
);
409 conflicts
= add_fileconflict(handle
, conflicts
,
410 PM_FILECONFLICT_TARGET
, path
,
411 alpm_pkg_get_name(p1
), alpm_pkg_get_name(p2
));
412 if(handle
->pm_errno
== PM_ERR_MEMORY
) {
414 FREELIST(common_files
);
418 FREELIST(common_files
);
422 /* CHECK 2: check every target against the filesystem */
423 _alpm_log(handle
, PM_LOG_DEBUG
, "searching for filesystem conflicts: %s\n",
425 dbpkg
= _alpm_db_get_pkgfromcache(handle
->db_local
, p1
->name
);
427 /* Do two different checks here. If the package is currently installed,
428 * then only check files that are new in the new package. If the package
429 * is not currently installed, then simply stat the whole filelist. Note
430 * that the former list needs to be freed while the latter list should NOT
433 /* older ver of package currently installed */
434 tmpfiles
= filelist_operation(alpm_pkg_get_files(p1
),
435 alpm_pkg_get_files(dbpkg
), DIFFERENCE
);
437 /* no version of package currently installed */
438 tmpfiles
= alpm_pkg_get_files(p1
);
441 for(j
= tmpfiles
; j
; j
= j
->next
) {
443 const char *filestr
= j
->data
, *relative_path
;
444 /* have we acted on this conflict? */
445 int resolved_conflict
= 0;
447 snprintf(path
, PATH_MAX
, "%s%s", handle
->root
, filestr
);
449 /* stat the file - if it exists, do some checks */
450 if(_alpm_lstat(path
, &lsbuf
) != 0) {
454 if(path
[strlen(path
) - 1] == '/') {
456 if(S_ISDIR(lsbuf
.st_mode
)) {
457 _alpm_log(handle
, PM_LOG_DEBUG
, "%s is a directory, not a conflict\n", path
);
461 if(S_ISLNK(lsbuf
.st_mode
) && S_ISDIR(sbuf
.st_mode
)) {
462 _alpm_log(handle
, PM_LOG_DEBUG
,
463 "%s is a symlink to a dir, hopefully not a conflict\n", path
);
466 /* if we made it to here, we want all subsequent path comparisons to
467 * not include the trailing slash. This allows things like file ->
468 * directory replacements. */
469 path
[strlen(path
) - 1] = '\0';
472 _alpm_log(handle
, PM_LOG_DEBUG
, "checking possible conflict: %s\n", path
);
473 relative_path
= path
+ strlen(handle
->root
);
475 /* Check remove list (will we remove the conflicting local file?) */
476 for(k
= remove
; k
&& !resolved_conflict
; k
= k
->next
) {
477 alpm_pkg_t
*rempkg
= k
->data
;
478 if(alpm_list_find_str(alpm_pkg_get_files(rempkg
), relative_path
)) {
479 _alpm_log(handle
, PM_LOG_DEBUG
,
480 "local file will be removed, not a conflict: %s\n",
482 resolved_conflict
= 1;
486 /* Look at all the targets to see if file has changed hands */
487 for(k
= upgrade
; k
&& !resolved_conflict
; k
= k
->next
) {
489 if(!p2
|| strcmp(p1
->name
, p2
->name
) == 0) {
492 alpm_pkg_t
*localp2
= _alpm_db_get_pkgfromcache(handle
->db_local
, p2
->name
);
494 /* localp2->files will be removed (target conflicts are handled by CHECK 1) */
495 if(localp2
&& alpm_list_find_str(alpm_pkg_get_files(localp2
), filestr
)) {
496 /* skip removal of file, but not add. this will prevent a second
497 * package from removing the file when it was already installed
498 * by its new owner (whether the file is in backup array or not */
499 handle
->trans
->skip_remove
=
500 alpm_list_add(handle
->trans
->skip_remove
, strdup(filestr
));
501 _alpm_log(handle
, PM_LOG_DEBUG
,
502 "file changed packages, adding to remove skiplist: %s\n",
504 resolved_conflict
= 1;
508 /* check if all files of the dir belong to the installed pkg */
509 if(!resolved_conflict
&& S_ISDIR(lsbuf
.st_mode
) && dbpkg
) {
510 char *dir
= malloc(strlen(filestr
) + 2);
511 sprintf(dir
, "%s/", filestr
);
512 if(alpm_list_find_str(alpm_pkg_get_files(dbpkg
),dir
)) {
513 _alpm_log(handle
, PM_LOG_DEBUG
,
514 "check if all files in %s belongs to %s\n",
516 resolved_conflict
= dir_belongsto_pkg(handle
->root
, filestr
, dbpkg
);
521 if(!resolved_conflict
&& dbpkg
) {
522 char *rpath
= calloc(PATH_MAX
, sizeof(char));
523 const char *relative_rpath
;
524 if(!realpath(path
, rpath
)) {
528 relative_rpath
= rpath
+ strlen(handle
->root
);
529 if(alpm_list_find_str(alpm_pkg_get_files(dbpkg
), relative_rpath
)) {
530 resolved_conflict
= 1;
535 if(!resolved_conflict
) {
536 conflicts
= add_fileconflict(handle
, conflicts
,
537 PM_FILECONFLICT_FILESYSTEM
, path
, p1
->name
, NULL
);
538 if(handle
->pm_errno
== PM_ERR_MEMORY
) {
541 /* only freed if it was generated from filelist_operation() */
549 /* only freed if it was generated from filelist_operation() */
553 PROGRESS(trans
, PM_TRANS_PROGRESS_CONFLICTS_START
, "", 100,
559 /* vim: set ts=2 sw=2 noet: */