4 * Copyright (c) 2006-2012 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/>.
34 #include "alpm_list.h"
43 static alpm_conflict_t
*conflict_new(alpm_pkg_t
*pkg1
, alpm_pkg_t
*pkg2
,
44 alpm_depend_t
*reason
)
46 alpm_conflict_t
*conflict
;
48 MALLOC(conflict
, sizeof(alpm_conflict_t
), return NULL
);
50 conflict
->package1_hash
= pkg1
->name_hash
;
51 conflict
->package2_hash
= pkg2
->name_hash
;
52 STRDUP(conflict
->package1
, pkg1
->name
, return NULL
);
53 STRDUP(conflict
->package2
, pkg2
->name
, return NULL
);
54 conflict
->reason
= reason
;
59 void _alpm_conflict_free(alpm_conflict_t
*conflict
)
61 FREE(conflict
->package2
);
62 FREE(conflict
->package1
);
66 alpm_conflict_t
*_alpm_conflict_dup(const alpm_conflict_t
*conflict
)
68 alpm_conflict_t
*newconflict
;
69 CALLOC(newconflict
, 1, sizeof(alpm_conflict_t
), return NULL
);
71 newconflict
->package1_hash
= conflict
->package1_hash
;
72 newconflict
->package2_hash
= conflict
->package2_hash
;
73 STRDUP(newconflict
->package1
, conflict
->package1
, return NULL
);
74 STRDUP(newconflict
->package2
, conflict
->package2
, return NULL
);
75 newconflict
->reason
= conflict
->reason
;
80 static int conflict_isin(alpm_conflict_t
*needle
, alpm_list_t
*haystack
)
83 for(i
= haystack
; i
; i
= i
->next
) {
84 alpm_conflict_t
*conflict
= i
->data
;
85 if(needle
->package1_hash
== conflict
->package1_hash
86 && needle
->package2_hash
== conflict
->package2_hash
87 && strcmp(needle
->package1
, conflict
->package1
) == 0
88 && strcmp(needle
->package2
, conflict
->package2
) == 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 alpm_pkg_t
*pkg1
, alpm_pkg_t
*pkg2
, alpm_depend_t
*reason
)
106 alpm_conflict_t
*conflict
= conflict_new(pkg1
, pkg2
, reason
);
110 if(!conflict_isin(conflict
, *baddeps
)) {
111 char *conflict_str
= alpm_dep_compute_string(reason
);
112 *baddeps
= alpm_list_add(*baddeps
, conflict
);
113 _alpm_log(handle
, ALPM_LOG_DEBUG
, "package %s conflicts with %s (by %s)\n",
114 pkg1
->name
, pkg2
->name
, conflict_str
);
117 _alpm_conflict_free(conflict
);
122 /** Check if packages from list1 conflict with packages from list2.
123 * This looks at the conflicts fields of all packages from list1, and sees
124 * if they match packages from list2.
125 * If a conflict (pkg1, pkg2) is found, it is added to the baddeps list
126 * in this order if order >= 0, or reverse order (pkg2,pkg1) otherwise.
128 * @param handle the context handle
129 * @param list1 first list of packages
130 * @param list2 second list of packages
131 * @param baddeps list to store conflicts
132 * @param order if >= 0 the conflict order is preserved, if < 0 it's reversed
134 static void check_conflict(alpm_handle_t
*handle
,
135 alpm_list_t
*list1
, alpm_list_t
*list2
,
136 alpm_list_t
**baddeps
, int order
) {
142 for(i
= list1
; i
; i
= i
->next
) {
143 alpm_pkg_t
*pkg1
= i
->data
;
146 for(j
= alpm_pkg_get_conflicts(pkg1
); j
; j
= j
->next
) {
147 alpm_depend_t
*conflict
= j
->data
;
150 for(k
= list2
; k
; k
= k
->next
) {
151 alpm_pkg_t
*pkg2
= k
->data
;
153 if(pkg1
->name_hash
== pkg2
->name_hash
154 && strcmp(pkg1
->name
, pkg2
->name
) == 0) {
155 /* skip the package we're currently processing */
159 if(_alpm_depcmp(pkg2
, conflict
)) {
161 add_conflict(handle
, baddeps
, pkg1
, pkg2
, conflict
);
163 add_conflict(handle
, baddeps
, pkg2
, pkg1
, conflict
);
171 /* Check for inter-conflicts */
172 alpm_list_t
*_alpm_innerconflicts(alpm_handle_t
*handle
, alpm_list_t
*packages
)
174 alpm_list_t
*baddeps
= NULL
;
176 _alpm_log(handle
, ALPM_LOG_DEBUG
, "check targets vs targets\n");
177 check_conflict(handle
, packages
, packages
, &baddeps
, 0);
182 /* Check for target vs (db - target) conflicts */
183 alpm_list_t
*_alpm_outerconflicts(alpm_db_t
*db
, alpm_list_t
*packages
)
185 alpm_list_t
*baddeps
= NULL
;
191 alpm_list_t
*dblist
= alpm_list_diff(_alpm_db_get_pkgcache(db
),
192 packages
, _alpm_pkg_cmp
);
194 /* two checks to be done here for conflicts */
195 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
, "check targets vs db\n");
196 check_conflict(db
->handle
, packages
, dblist
, &baddeps
, 1);
197 _alpm_log(db
->handle
, ALPM_LOG_DEBUG
, "check db vs targets\n");
198 check_conflict(db
->handle
, dblist
, packages
, &baddeps
, -1);
200 alpm_list_free(dblist
);
204 /** Check the package conflicts in a database
206 * @param handle the context handle
207 * @param pkglist the list of packages to check
208 * @return an alpm_list_t of alpm_conflict_t
210 alpm_list_t SYMEXPORT
*alpm_checkconflicts(alpm_handle_t
*handle
,
211 alpm_list_t
*pkglist
)
213 CHECK_HANDLE(handle
, return NULL
);
214 return _alpm_innerconflicts(handle
, pkglist
);
217 /* Adds alpm_fileconflict_t to a conflicts list. Pass the conflicts list, the
218 * conflicting file path, and either two packages or one package and NULL.
220 static alpm_list_t
*add_fileconflict(alpm_handle_t
*handle
,
221 alpm_list_t
*conflicts
, const char *filestr
,
222 alpm_pkg_t
*pkg1
, alpm_pkg_t
*pkg2
)
224 alpm_fileconflict_t
*conflict
;
225 MALLOC(conflict
, sizeof(alpm_fileconflict_t
), goto error
);
227 STRDUP(conflict
->target
, pkg1
->name
, goto error
);
228 STRDUP(conflict
->file
, filestr
, goto error
);
230 conflict
->type
= ALPM_FILECONFLICT_TARGET
;
231 STRDUP(conflict
->ctarget
, pkg2
->name
, goto error
);
233 conflict
->type
= ALPM_FILECONFLICT_FILESYSTEM
;
234 STRDUP(conflict
->ctarget
, "", goto error
);
237 conflicts
= alpm_list_add(conflicts
, conflict
);
238 _alpm_log(handle
, ALPM_LOG_DEBUG
, "found file conflict %s, packages %s and %s\n",
239 filestr
, pkg1
->name
, pkg2
? pkg2
->name
: "(filesystem)");
244 RET_ERR(handle
, ALPM_ERR_MEMORY
, conflicts
);
247 void _alpm_fileconflict_free(alpm_fileconflict_t
*conflict
)
249 FREE(conflict
->ctarget
);
250 FREE(conflict
->file
);
251 FREE(conflict
->target
);
255 static int dir_belongsto_pkg(alpm_handle_t
*handle
, const char *dirpath
,
261 char abspath
[PATH_MAX
];
263 struct dirent
*ent
= NULL
;
264 const char *root
= handle
->root
;
266 /* check directory is actually in package - used for subdirectory checks */
267 if(!alpm_filelist_contains(alpm_pkg_get_files(pkg
), dirpath
)) {
268 _alpm_log(handle
, ALPM_LOG_DEBUG
,
269 "directory %s not in package %s\n", dirpath
, pkg
->name
);
273 /* TODO: this is an overly strict check but currently pacman will not
274 * overwrite a directory with a file (case 10/11 in add.c). Adjusting that
275 * is not simple as even if the directory is being unowned by a conflicting
276 * package, pacman does not sort this to ensure all required directory
277 * "removals" happen before installation of file/symlink */
279 /* check that no other _installed_ package owns the directory */
280 for(i
= _alpm_db_get_pkgcache(handle
->db_local
); i
; i
= i
->next
) {
285 if(alpm_filelist_contains(alpm_pkg_get_files(i
->data
), dirpath
)) {
286 _alpm_log(handle
, ALPM_LOG_DEBUG
,
287 "file %s also in package %s\n", dirpath
,
288 ((alpm_pkg_t
*)i
->data
)->name
);
293 /* check all files in directory are owned by the package */
294 snprintf(abspath
, PATH_MAX
, "%s%s", root
, dirpath
);
295 dir
= opendir(abspath
);
300 while((ent
= readdir(dir
)) != NULL
) {
301 const char *name
= ent
->d_name
;
303 if(strcmp(name
, ".") == 0 || strcmp(name
, "..") == 0) {
306 snprintf(path
, PATH_MAX
, "%s%s", dirpath
, name
);
307 snprintf(abspath
, PATH_MAX
, "%s%s", root
, path
);
308 if(stat(abspath
, &sbuf
) != 0) {
311 if(S_ISDIR(sbuf
.st_mode
)) {
312 if(dir_belongsto_pkg(handle
, path
, pkg
)) {
319 if(alpm_filelist_contains(alpm_pkg_get_files(pkg
), path
)) {
323 _alpm_log(handle
, ALPM_LOG_DEBUG
,
324 "unowned file %s found in directory\n", path
);
333 /* Find file conflicts that may occur during the transaction with two checks:
334 * 1: check every target against every target
335 * 2: check every target against the filesystem */
336 alpm_list_t
*_alpm_db_find_fileconflicts(alpm_handle_t
*handle
,
337 alpm_list_t
*upgrade
, alpm_list_t
*rem
)
339 alpm_list_t
*i
, *conflicts
= NULL
;
340 size_t numtargs
= alpm_list_count(upgrade
);
348 rootlen
= strlen(handle
->root
);
350 /* TODO this whole function needs a huge change, which hopefully will
351 * be possible with real transactions. Right now we only do half as much
352 * here as we do when we actually extract files in add.c with our 12
353 * different cases. */
354 for(current
= 0, i
= upgrade
; i
; i
= i
->next
, current
++) {
355 alpm_pkg_t
*p1
= i
->data
;
357 alpm_filelist_t tmpfiles
;
361 int percent
= (current
* 100) / numtargs
;
362 PROGRESS(handle
, ALPM_PROGRESS_CONFLICTS_START
, "", percent
,
364 /* CHECK 1: check every target against every target */
365 _alpm_log(handle
, ALPM_LOG_DEBUG
, "searching for file conflicts: %s\n",
367 for(j
= i
->next
; j
; j
= j
->next
) {
368 alpm_list_t
*common_files
;
369 alpm_pkg_t
*p2
= j
->data
;
370 common_files
= _alpm_filelist_intersection(alpm_pkg_get_files(p1
),
371 alpm_pkg_get_files(p2
));
376 for(k
= common_files
; k
; k
= k
->next
) {
377 alpm_file_t
*file
= k
->data
;
378 snprintf(path
, PATH_MAX
, "%s%s", handle
->root
, file
->name
);
379 conflicts
= add_fileconflict(handle
, conflicts
, path
, p1
, p2
);
380 if(handle
->pm_errno
== ALPM_ERR_MEMORY
) {
382 FREELIST(common_files
);
386 alpm_list_free(common_files
);
390 /* CHECK 2: check every target against the filesystem */
391 _alpm_log(handle
, ALPM_LOG_DEBUG
, "searching for filesystem conflicts: %s\n",
393 dbpkg
= _alpm_db_get_pkgfromcache(handle
->db_local
, p1
->name
);
395 /* Do two different checks here. If the package is currently installed,
396 * then only check files that are new in the new package. If the package
397 * is not currently installed, then simply stat the whole filelist. Note
398 * that the former list needs to be freed while the latter list should NOT
401 alpm_list_t
*difference
;
402 /* older ver of package currently installed */
403 difference
= _alpm_filelist_difference(alpm_pkg_get_files(p1
),
404 alpm_pkg_get_files(dbpkg
));
405 tmpfiles
.count
= alpm_list_count(difference
);
406 tmpfiles
.files
= alpm_list_to_array(difference
, tmpfiles
.count
,
407 sizeof(alpm_file_t
));
408 alpm_list_free(difference
);
410 /* no version of package currently installed */
411 tmpfiles
= *alpm_pkg_get_files(p1
);
414 for(filenum
= 0; filenum
< tmpfiles
.count
; filenum
++) {
415 alpm_file_t
*file
= tmpfiles
.files
+ filenum
;
416 const char *filestr
= file
->name
;
417 const char *relative_path
;
419 /* have we acted on this conflict? */
420 int resolved_conflict
= 0;
425 pathlen
= snprintf(path
, PATH_MAX
, "%s%s", handle
->root
, filestr
);
427 /* stat the file - if it exists, do some checks */
428 if(_alpm_lstat(path
, &lsbuf
) != 0) {
432 _alpm_log(handle
, ALPM_LOG_DEBUG
, "checking possible conflict: %s\n", path
);
434 if(S_ISDIR(file
->mode
)) {
436 if(S_ISDIR(lsbuf
.st_mode
)) {
437 _alpm_log(handle
, ALPM_LOG_DEBUG
, "file is a directory, not a conflict\n");
441 if(S_ISLNK(lsbuf
.st_mode
) && S_ISDIR(sbuf
.st_mode
)) {
442 _alpm_log(handle
, ALPM_LOG_DEBUG
,
443 "file is a symlink to a dir, hopefully not a conflict\n");
446 /* if we made it to here, we want all subsequent path comparisons to
447 * not include the trailing slash. This allows things like file ->
448 * directory replacements. */
449 path
[pathlen
- 1] = '\0';
452 relative_path
= path
+ rootlen
;
454 /* Check remove list (will we remove the conflicting local file?) */
455 for(k
= rem
; k
&& !resolved_conflict
; k
= k
->next
) {
456 alpm_pkg_t
*rempkg
= k
->data
;
457 if(rempkg
&& alpm_filelist_contains(alpm_pkg_get_files(rempkg
),
459 _alpm_log(handle
, ALPM_LOG_DEBUG
,
460 "local file will be removed, not a conflict\n");
461 resolved_conflict
= 1;
465 /* Look at all the targets to see if file has changed hands */
466 for(k
= upgrade
; k
&& !resolved_conflict
; k
= k
->next
) {
467 alpm_pkg_t
*p2
= k
->data
;
468 if(!p2
|| strcmp(p1
->name
, p2
->name
) == 0) {
471 alpm_pkg_t
*localp2
= _alpm_db_get_pkgfromcache(handle
->db_local
, p2
->name
);
473 /* localp2->files will be removed (target conflicts are handled by CHECK 1) */
474 if(localp2
&& alpm_filelist_contains(alpm_pkg_get_files(localp2
), filestr
)) {
475 /* skip removal of file, but not add. this will prevent a second
476 * package from removing the file when it was already installed
477 * by its new owner (whether the file is in backup array or not */
478 handle
->trans
->skip_remove
=
479 alpm_list_add(handle
->trans
->skip_remove
, strdup(filestr
));
480 _alpm_log(handle
, ALPM_LOG_DEBUG
,
481 "file changed packages, adding to remove skiplist\n");
482 resolved_conflict
= 1;
486 /* check if all files of the dir belong to the installed pkg */
487 if(!resolved_conflict
&& S_ISDIR(lsbuf
.st_mode
) && dbpkg
) {
488 char *dir
= malloc(strlen(filestr
) + 2);
489 sprintf(dir
, "%s/", filestr
);
490 if(alpm_filelist_contains(alpm_pkg_get_files(dbpkg
), dir
)) {
491 _alpm_log(handle
, ALPM_LOG_DEBUG
,
492 "checking if all files in %s belong to %s\n",
494 resolved_conflict
= dir_belongsto_pkg(handle
, dir
, dbpkg
);
499 /* check if a component of the filepath was a link. canonicalize the path
500 * and look for it in the old package. note that the actual file under
501 * consideration cannot itself be a link, as it might be unowned- path
502 * components can be safely checked as all directories are "unowned". */
503 if(!resolved_conflict
&& dbpkg
&& !S_ISLNK(lsbuf
.st_mode
)) {
504 char rpath
[PATH_MAX
];
505 if(realpath(path
, rpath
)) {
506 const char *relative_rpath
= rpath
+ rootlen
;
507 if(alpm_filelist_contains(alpm_pkg_get_files(dbpkg
), relative_rpath
)) {
508 _alpm_log(handle
, ALPM_LOG_DEBUG
,
509 "package contained the resolved realpath\n");
510 resolved_conflict
= 1;
515 /* is the file unowned and in the backup list of the new package? */
516 if(!resolved_conflict
&& _alpm_needbackup(filestr
, p1
)) {
517 alpm_list_t
*local_pkgs
= _alpm_db_get_pkgcache(handle
->db_local
);
519 for(k
= local_pkgs
; k
&& !found
; k
= k
->next
) {
520 if(alpm_filelist_contains(alpm_pkg_get_files(k
->data
), filestr
)) {
525 _alpm_log(handle
, ALPM_LOG_DEBUG
,
526 "file was unowned but in new backup list\n");
527 resolved_conflict
= 1;
531 if(!resolved_conflict
) {
532 conflicts
= add_fileconflict(handle
, conflicts
, path
, p1
, NULL
);
533 if(handle
->pm_errno
== ALPM_ERR_MEMORY
) {
536 /* only freed if it was generated from _alpm_filelist_difference() */
537 free(tmpfiles
.files
);
544 /* only freed if it was generated from _alpm_filelist_difference() */
545 free(tmpfiles
.files
);
548 PROGRESS(handle
, ALPM_PROGRESS_CONFLICTS_START
, "", 100,
554 /* vim: set ts=2 sw=2 noet: */