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
,
46 pmconflict_t
*conflict
;
48 MALLOC(conflict
, sizeof(pmconflict_t
), RET_ERR(PM_ERR_MEMORY
, NULL
));
50 STRDUP(conflict
->package1
, package1
, RET_ERR(PM_ERR_MEMORY
, NULL
));
51 STRDUP(conflict
->package2
, package2
, RET_ERR(PM_ERR_MEMORY
, NULL
));
52 STRDUP(conflict
->reason
, reason
, RET_ERR(PM_ERR_MEMORY
, 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
), RET_ERR(PM_ERR_MEMORY
, NULL
));
70 STRDUP(newconflict
->package1
, conflict
->package1
, RET_ERR(PM_ERR_MEMORY
, NULL
));
71 STRDUP(newconflict
->package2
, conflict
->package2
, RET_ERR(PM_ERR_MEMORY
, NULL
));
72 STRDUP(newconflict
->reason
, conflict
->reason
, RET_ERR(PM_ERR_MEMORY
, 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 *baddeps list to add conflict to
98 * @param pkg1 first package
99 * @param pkg2 package causing conflict
101 static void add_conflict(alpm_list_t
**baddeps
, const char *pkg1
,
102 const char *pkg2
, const char *reason
)
104 pmconflict_t
*conflict
= _alpm_conflict_new(pkg1
, pkg2
, reason
);
105 _alpm_log(PM_LOG_DEBUG
, "package %s conflicts with %s (by %s)\n",
107 if(conflict
&& !conflict_isin(conflict
, *baddeps
)) {
108 *baddeps
= alpm_list_add(*baddeps
, conflict
);
110 _alpm_conflict_free(conflict
);
114 /** Check if packages from list1 conflict with packages from list2.
115 * This looks at the conflicts fields of all packages from list1, and sees
116 * if they match packages from list2.
117 * If a conflict (pkg1, pkg2) is found, it is added to the baddeps list
118 * in this order if order >= 0, or reverse order (pkg2,pkg1) otherwise.
120 * @param list1 first list of packages
121 * @param list2 second list of packages
122 * @param *baddeps list to store conflicts
123 * @param order if >= 0 the conflict order is preserved, if < 0 it's reversed
125 static void check_conflict(alpm_list_t
*list1
, alpm_list_t
*list2
,
126 alpm_list_t
**baddeps
, int order
) {
127 alpm_list_t
*i
, *j
, *k
;
132 for(i
= list1
; i
; i
= i
->next
) {
133 pmpkg_t
*pkg1
= i
->data
;
134 const char *pkg1name
= alpm_pkg_get_name(pkg1
);
136 for(j
= alpm_pkg_get_conflicts(pkg1
); j
; j
= j
->next
) {
137 const char *conflict
= j
->data
;
138 pmdepend_t
*parsed_conflict
= _alpm_splitdep(conflict
);
140 for(k
= list2
; k
; k
= k
->next
) {
141 pmpkg_t
*pkg2
= k
->data
;
142 const char *pkg2name
= alpm_pkg_get_name(pkg2
);
144 if(strcmp(pkg1name
, pkg2name
) == 0) {
145 /* skip the package we're currently processing */
149 if(_alpm_depcmp(pkg2
, parsed_conflict
)) {
151 add_conflict(baddeps
, pkg1name
, pkg2name
, conflict
);
153 add_conflict(baddeps
, pkg2name
, pkg1name
, conflict
);
157 _alpm_dep_free(parsed_conflict
);
162 /* Check for inter-conflicts */
163 alpm_list_t
*_alpm_innerconflicts(alpm_list_t
*packages
)
165 alpm_list_t
*baddeps
= NULL
;
167 _alpm_log(PM_LOG_DEBUG
, "check targets vs targets\n");
168 check_conflict(packages
, packages
, &baddeps
, 0);
173 /* Check for target vs (db - target) conflicts
174 * In case of conflict the package1 field of pmdepconflict_t contains
175 * the target package, package2 field contains the local package
177 alpm_list_t
*_alpm_outerconflicts(pmdb_t
*db
, alpm_list_t
*packages
)
179 alpm_list_t
*baddeps
= NULL
;
185 alpm_list_t
*dblist
= alpm_list_diff(_alpm_db_get_pkgcache(db
),
186 packages
, _alpm_pkg_cmp
);
188 /* two checks to be done here for conflicts */
189 _alpm_log(PM_LOG_DEBUG
, "check targets vs db\n");
190 check_conflict(packages
, dblist
, &baddeps
, 1);
191 _alpm_log(PM_LOG_DEBUG
, "check db vs targets\n");
192 check_conflict(dblist
, packages
, &baddeps
, -1);
194 alpm_list_free(dblist
);
198 /** Check the package conflicts in a database
200 * @param pkglist the list of packages to check
201 * @return an alpm_list_t of pmconflict_t
203 alpm_list_t SYMEXPORT
*alpm_checkconflicts(alpm_list_t
*pkglist
) {
204 return _alpm_innerconflicts(pkglist
);
207 static const int DIFFERENCE
= 0;
208 static const int INTERSECT
= 1;
209 /* Returns a set operation on the provided two lists of files.
210 * Pre-condition: both lists are sorted!
213 * DIFFERENCE - a difference operation is performed. filesA - filesB.
214 * INTERSECT - an intersection operation is performed. filesA & filesB.
216 static alpm_list_t
*filelist_operation(alpm_list_t
*filesA
, alpm_list_t
*filesB
,
219 alpm_list_t
*ret
= NULL
;
220 alpm_list_t
*pA
= filesA
, *pB
= filesB
;
223 const char *strA
= pA
->data
;
224 const char *strB
= pB
->data
;
225 /* skip directories, we don't care about them */
226 if(strA
[strlen(strA
)-1] == '/') {
228 } else if(strB
[strlen(strB
)-1] == '/') {
231 int cmp
= strcmp(strA
, strB
);
233 if(operation
== DIFFERENCE
) {
234 /* item only in filesA, qualifies as a difference */
235 ret
= alpm_list_add(ret
, strdup(strA
));
241 if(operation
== INTERSECT
) {
242 /* item in both, qualifies as an intersect */
243 ret
= alpm_list_add(ret
, strdup(strA
));
251 /* if doing a difference, ensure we have completely emptied pA */
252 while(operation
== DIFFERENCE
&& pA
) {
253 const char *strA
= pA
->data
;
254 /* skip directories */
255 if(strA
[strlen(strA
)-1] != '/') {
256 ret
= alpm_list_add(ret
, strdup(strA
));
264 /* Adds pmfileconflict_t to a conflicts list. Pass the conflicts list, type (either
265 * PM_FILECONFLICT_TARGET or PM_FILECONFLICT_FILESYSTEM), a file string, and either
266 * two package names or one package name and NULL. This is a wrapper for former
267 * functionality that was done inline.
269 static alpm_list_t
*add_fileconflict(alpm_list_t
*conflicts
,
270 pmfileconflicttype_t type
, const char *filestr
,
271 const char* name1
, const char* name2
)
273 pmfileconflict_t
*conflict
;
274 MALLOC(conflict
, sizeof(pmfileconflict_t
), RET_ERR(PM_ERR_MEMORY
, NULL
));
276 conflict
->type
= type
;
277 STRDUP(conflict
->target
, name1
, RET_ERR(PM_ERR_MEMORY
, NULL
));
278 STRDUP(conflict
->file
, filestr
, RET_ERR(PM_ERR_MEMORY
, NULL
));
280 STRDUP(conflict
->ctarget
, name2
, RET_ERR(PM_ERR_MEMORY
, NULL
));
282 STRDUP(conflict
->ctarget
, "", RET_ERR(PM_ERR_MEMORY
, NULL
));
285 conflicts
= alpm_list_add(conflicts
, conflict
);
286 _alpm_log(PM_LOG_DEBUG
, "found file conflict %s, packages %s and %s\n",
287 filestr
, name1
, name2
? name2
: "(filesystem)");
292 void _alpm_fileconflict_free(pmfileconflict_t
*conflict
)
294 FREE(conflict
->ctarget
);
295 FREE(conflict
->file
);
296 FREE(conflict
->target
);
300 static int dir_belongsto_pkg(char *dirpath
, pmpkg_t
*pkg
)
302 struct dirent
*ent
= NULL
;
305 char abspath
[PATH_MAX
];
308 snprintf(abspath
, PATH_MAX
, "%s%s", pkg
->handle
->root
, dirpath
);
309 dir
= opendir(abspath
);
313 while((ent
= readdir(dir
)) != NULL
) {
314 const char *name
= ent
->d_name
;
316 if(strcmp(name
, ".") == 0 || strcmp(name
, "..") == 0) {
319 snprintf(path
, PATH_MAX
, "%s/%s", dirpath
, name
);
320 snprintf(abspath
, PATH_MAX
, "%s%s", pkg
->handle
->root
, path
);
321 if(stat(abspath
, &sbuf
) != 0) {
324 if(S_ISDIR(sbuf
.st_mode
)) {
325 if(dir_belongsto_pkg(path
, pkg
)) {
332 if(alpm_list_find_str(alpm_pkg_get_files(pkg
), path
)) {
344 /* Find file conflicts that may occur during the transaction with two checks:
345 * 1: check every target against every target
346 * 2: check every target against the filesystem */
347 alpm_list_t
*_alpm_db_find_fileconflicts(pmhandle_t
*handle
,
348 alpm_list_t
*upgrade
, alpm_list_t
*remove
)
350 alpm_list_t
*i
, *j
, *conflicts
= NULL
;
351 size_t numtargs
= alpm_list_count(upgrade
);
353 pmtrans_t
*trans
= handle
->trans
;
359 /* TODO this whole function needs a huge change, which hopefully will
360 * be possible with real transactions. Right now we only do half as much
361 * here as we do when we actually extract files in add.c with our 12
362 * different cases. */
363 for(current
= 0, i
= upgrade
; i
; i
= i
->next
, current
++) {
364 alpm_list_t
*k
, *tmpfiles
= NULL
;
365 pmpkg_t
*p1
, *p2
, *dbpkg
;
366 char path
[PATH_MAX
+1];
373 int percent
= (current
* 100) / numtargs
;
374 PROGRESS(trans
, PM_TRANS_PROGRESS_CONFLICTS_START
, "", percent
,
376 /* CHECK 1: check every target against every target */
377 _alpm_log(PM_LOG_DEBUG
, "searching for file conflicts: %s\n",
378 alpm_pkg_get_name(p1
));
379 for(j
= i
->next
; j
; j
= j
->next
) {
384 tmpfiles
= filelist_operation( alpm_pkg_get_files(p1
),
385 alpm_pkg_get_files(p2
), INTERSECT
);
388 for(k
= tmpfiles
; k
; k
= k
->next
) {
389 snprintf(path
, PATH_MAX
, "%s%s", handle
->root
, (char *)k
->data
);
390 conflicts
= add_fileconflict(conflicts
, PM_FILECONFLICT_TARGET
, path
,
391 alpm_pkg_get_name(p1
), alpm_pkg_get_name(p2
));
397 /* declarations for second check */
398 struct stat lsbuf
, sbuf
;
399 char *filestr
= NULL
;
401 /* CHECK 2: check every target against the filesystem */
402 _alpm_log(PM_LOG_DEBUG
, "searching for filesystem conflicts: %s\n", p1
->name
);
403 dbpkg
= _alpm_db_get_pkgfromcache(handle
->db_local
, p1
->name
);
405 /* Do two different checks here. If the package is currently installed,
406 * then only check files that are new in the new package. If the package
407 * is not currently installed, then simply stat the whole filelist */
409 /* older ver of package currently installed */
410 tmpfiles
= filelist_operation(alpm_pkg_get_files(p1
),
411 alpm_pkg_get_files(dbpkg
), DIFFERENCE
);
413 /* no version of package currently installed */
414 tmpfiles
= alpm_list_strdup(alpm_pkg_get_files(p1
));
417 for(j
= tmpfiles
; j
; j
= j
->next
) {
420 snprintf(path
, PATH_MAX
, "%s%s", handle
->root
, filestr
);
422 /* stat the file - if it exists, do some checks */
423 if(_alpm_lstat(path
, &lsbuf
) != 0) {
428 if(path
[strlen(path
)-1] == '/') {
429 if(S_ISDIR(lsbuf
.st_mode
)) {
430 _alpm_log(PM_LOG_DEBUG
, "%s is a directory, not a conflict\n", path
);
432 } else if(S_ISLNK(lsbuf
.st_mode
) && S_ISDIR(sbuf
.st_mode
)) {
433 _alpm_log(PM_LOG_DEBUG
,
434 "%s is a symlink to a dir, hopefully not a conflict\n", path
);
438 _alpm_log(PM_LOG_DEBUG
, "checking possible conflict: %s\n", path
);
440 int resolved_conflict
= 0; /* have we acted on this conflict? */
442 /* Check remove list (will we remove the conflicting local file?) */
443 for(k
= remove
; k
&& !resolved_conflict
; k
= k
->next
) {
444 pmpkg_t
*rempkg
= k
->data
;
445 if(rempkg
&& alpm_list_find_str(alpm_pkg_get_files(rempkg
), filestr
)) {
446 _alpm_log(PM_LOG_DEBUG
, "local file will be removed, not a conflict: %s\n", filestr
);
447 resolved_conflict
= 1;
451 /* Look at all the targets to see if file has changed hands */
452 for(k
= upgrade
; k
&& !resolved_conflict
; k
= k
->next
) {
454 if(!p2
|| strcmp(p1
->name
, p2
->name
) == 0) {
457 pmpkg_t
*localp2
= _alpm_db_get_pkgfromcache(handle
->db_local
, p2
->name
);
459 /* localp2->files will be removed (target conflicts are handled by CHECK 1) */
460 if(localp2
&& alpm_list_find_str(alpm_pkg_get_files(localp2
), filestr
)) {
461 /* skip removal of file, but not add. this will prevent a second
462 * package from removing the file when it was already installed
463 * by its new owner (whether the file is in backup array or not */
464 trans
->skip_remove
= alpm_list_add(trans
->skip_remove
, strdup(filestr
));
465 _alpm_log(PM_LOG_DEBUG
, "file changed packages, adding to remove skiplist: %s\n", filestr
);
466 resolved_conflict
= 1;
470 /* check if all files of the dir belong to the installed pkg */
471 if(!resolved_conflict
&& S_ISDIR(lsbuf
.st_mode
) && dbpkg
) {
472 char *dir
= malloc(strlen(filestr
) + 2);
473 sprintf(dir
, "%s/", filestr
);
474 if(alpm_list_find_str(alpm_pkg_get_files(dbpkg
),dir
)) {
475 _alpm_log(PM_LOG_DEBUG
, "check if all files in %s belongs to %s\n",
477 resolved_conflict
= dir_belongsto_pkg(filestr
, dbpkg
);
482 if(!resolved_conflict
&& dbpkg
) {
483 char *rpath
= calloc(PATH_MAX
+1, sizeof(char));
484 if(!realpath(path
, rpath
)) {
488 char *filestr
= rpath
+ strlen(handle
->root
);
489 if(alpm_list_find_str(alpm_pkg_get_files(dbpkg
),filestr
)) {
490 resolved_conflict
= 1;
495 if(!resolved_conflict
) {
496 _alpm_log(PM_LOG_DEBUG
, "file found in conflict: %s\n", path
);
497 conflicts
= add_fileconflict(conflicts
, PM_FILECONFLICT_FILESYSTEM
,
498 path
, p1
->name
, NULL
);
503 PROGRESS(trans
, PM_TRANS_PROGRESS_CONFLICTS_START
, "", 100,
509 const char SYMEXPORT
*alpm_conflict_get_package1(pmconflict_t
*conflict
)
512 ASSERT(conflict
!= NULL
, return NULL
);
514 return conflict
->package1
;
517 const char SYMEXPORT
*alpm_conflict_get_package2(pmconflict_t
*conflict
)
520 ASSERT(conflict
!= NULL
, return NULL
);
522 return conflict
->package2
;
525 const char SYMEXPORT
*alpm_conflict_get_reason(pmconflict_t
*conflict
)
528 ASSERT(conflict
!= NULL
, return NULL
);
530 return conflict
->reason
;
533 const char SYMEXPORT
*alpm_fileconflict_get_target(pmfileconflict_t
*conflict
)
536 ASSERT(conflict
!= NULL
, return NULL
);
538 return conflict
->target
;
541 pmfileconflicttype_t SYMEXPORT
alpm_fileconflict_get_type(pmfileconflict_t
*conflict
)
544 ASSERT(conflict
!= NULL
, return -1);
546 return conflict
->type
;
549 const char SYMEXPORT
*alpm_fileconflict_get_file(pmfileconflict_t
*conflict
)
552 ASSERT(conflict
!= NULL
, return NULL
);
554 return conflict
->file
;
557 const char SYMEXPORT
*alpm_fileconflict_get_ctarget(pmfileconflict_t
*conflict
)
560 ASSERT(conflict
!= NULL
, return NULL
);
562 return conflict
->ctarget
;
564 /* vim: set ts=2 sw=2 noet: */