Move important information up in -Si output
[pacman-ng.git] / lib / libalpm / conflict.c
blobccfe990c12b2954d16f1f05683dfcf6145c4212d
1 /*
2 * conflict.c
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/>.
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <limits.h>
29 #include <sys/stat.h>
30 #include <dirent.h>
32 /* libalpm */
33 #include "conflict.h"
34 #include "alpm_list.h"
35 #include "alpm.h"
36 #include "handle.h"
37 #include "trans.h"
38 #include "util.h"
39 #include "log.h"
40 #include "deps.h"
41 #include "filelist.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;
56 return conflict;
59 void _alpm_conflict_free(alpm_conflict_t *conflict)
61 FREE(conflict->package2);
62 FREE(conflict->package1);
63 FREE(conflict);
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;
77 return newconflict;
80 static int conflict_isin(alpm_conflict_t *needle, alpm_list_t *haystack)
82 alpm_list_t *i;
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) {
89 return 1;
93 return 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);
107 if(!conflict) {
108 return -1;
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);
115 free(conflict_str);
116 } else {
117 _alpm_conflict_free(conflict);
119 return 0;
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) {
137 alpm_list_t *i;
139 if(!baddeps) {
140 return;
142 for(i = list1; i; i = i->next) {
143 alpm_pkg_t *pkg1 = i->data;
144 alpm_list_t *j;
146 for(j = alpm_pkg_get_conflicts(pkg1); j; j = j->next) {
147 alpm_depend_t *conflict = j->data;
148 alpm_list_t *k;
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 */
156 continue;
159 if(_alpm_depcmp(pkg2, conflict)) {
160 if(order >= 0) {
161 add_conflict(handle, baddeps, pkg1, pkg2, conflict);
162 } else {
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);
179 return baddeps;
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;
187 if(db == NULL) {
188 return 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);
201 return baddeps;
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);
229 if(pkg2) {
230 conflict->type = ALPM_FILECONFLICT_TARGET;
231 STRDUP(conflict->ctarget, pkg2->name, goto error);
232 } else {
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)");
241 return conflicts;
243 error:
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);
252 FREE(conflict);
255 static int dir_belongsto_pkg(alpm_handle_t *handle, const char *dirpath,
256 alpm_pkg_t *pkg)
258 alpm_list_t *i;
259 struct stat sbuf;
260 char path[PATH_MAX];
261 char abspath[PATH_MAX];
262 DIR *dir;
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);
270 return 0;
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) {
281 if(pkg == i->data) {
282 continue;
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);
289 return 0;
293 /* check all files in directory are owned by the package */
294 snprintf(abspath, PATH_MAX, "%s%s", root, dirpath);
295 dir = opendir(abspath);
296 if(dir == NULL) {
297 return 1;
300 while((ent = readdir(dir)) != NULL) {
301 const char *name = ent->d_name;
303 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
304 continue;
306 snprintf(path, PATH_MAX, "%s%s", dirpath, name);
307 snprintf(abspath, PATH_MAX, "%s%s", root, path);
308 if(stat(abspath, &sbuf) != 0) {
309 continue;
311 if(S_ISDIR(sbuf.st_mode)) {
312 if(dir_belongsto_pkg(handle, path, pkg)) {
313 continue;
314 } else {
315 closedir(dir);
316 return 0;
318 } else {
319 if(alpm_filelist_contains(alpm_pkg_get_files(pkg), path)) {
320 continue;
321 } else {
322 closedir(dir);
323 _alpm_log(handle, ALPM_LOG_DEBUG,
324 "unowned file %s found in directory\n", path);
325 return 0;
329 closedir(dir);
330 return 1;
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);
341 size_t current;
342 size_t rootlen;
344 if(!upgrade) {
345 return NULL;
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;
356 alpm_list_t *j;
357 alpm_filelist_t tmpfiles;
358 alpm_pkg_t *dbpkg;
359 size_t filenum;
361 int percent = (current * 100) / numtargs;
362 PROGRESS(handle, ALPM_PROGRESS_CONFLICTS_START, "", percent,
363 numtargs, current);
364 /* CHECK 1: check every target against every target */
365 _alpm_log(handle, ALPM_LOG_DEBUG, "searching for file conflicts: %s\n",
366 p1->name);
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));
373 if(common_files) {
374 alpm_list_t *k;
375 char path[PATH_MAX];
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) {
381 FREELIST(conflicts);
382 FREELIST(common_files);
383 return NULL;
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",
392 p1->name);
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
399 * be freed. */
400 if(dbpkg) {
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);
409 } else {
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;
418 alpm_list_t *k;
419 /* have we acted on this conflict? */
420 int resolved_conflict = 0;
421 struct stat lsbuf;
422 char path[PATH_MAX];
423 size_t pathlen;
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) {
429 continue;
432 _alpm_log(handle, ALPM_LOG_DEBUG, "checking possible conflict: %s\n", path);
434 if(S_ISDIR(file->mode)) {
435 struct stat sbuf;
436 if(S_ISDIR(lsbuf.st_mode)) {
437 _alpm_log(handle, ALPM_LOG_DEBUG, "file is a directory, not a conflict\n");
438 continue;
440 stat(path, &sbuf);
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");
444 continue;
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),
458 relative_path)) {
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) {
469 continue;
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",
493 dir, dbpkg->name);
494 resolved_conflict = dir_belongsto_pkg(handle, dir, dbpkg);
496 free(dir);
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);
518 int found = 0;
519 for(k = local_pkgs; k && !found; k = k->next) {
520 if(alpm_filelist_contains(alpm_pkg_get_files(k->data), filestr)) {
521 found = 1;
524 if(!found) {
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) {
534 FREELIST(conflicts);
535 if(dbpkg) {
536 /* only freed if it was generated from _alpm_filelist_difference() */
537 free(tmpfiles.files);
539 return NULL;
543 if(dbpkg) {
544 /* only freed if it was generated from _alpm_filelist_difference() */
545 free(tmpfiles.files);
548 PROGRESS(handle, ALPM_PROGRESS_CONFLICTS_START, "", 100,
549 numtargs, current);
551 return conflicts;
554 /* vim: set ts=2 sw=2 noet: */