Rename pmdepend_t to alpm_depend_t
[pacman-ng.git] / lib / libalpm / conflict.c
blob1e535122e6c490fba88145299cdc8f771749804c
1 /*
2 * conflict.c
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/>.
25 #include "config.h"
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <limits.h>
31 #include <sys/stat.h>
32 #include <dirent.h>
34 /* libalpm */
35 #include "conflict.h"
36 #include "alpm_list.h"
37 #include "handle.h"
38 #include "trans.h"
39 #include "util.h"
40 #include "log.h"
41 #include "deps.h"
43 static pmconflict_t *conflict_new(const char *package1, const char *package2,
44 const char *reason)
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);
54 return conflict;
57 void _alpm_conflict_free(pmconflict_t *conflict)
59 FREE(conflict->package2);
60 FREE(conflict->package1);
61 FREE(conflict->reason);
62 FREE(conflict);
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);
74 return newconflict;
77 static int conflict_isin(pmconflict_t *needle, alpm_list_t *haystack)
79 alpm_list_t *i;
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)) {
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 const char *pkg1, const char *pkg2, const char *reason)
106 pmconflict_t *conflict = conflict_new(pkg1, pkg2, reason);
107 if(!conflict) {
108 return -1;
110 _alpm_log(handle, PM_LOG_DEBUG, "package %s conflicts with %s (by %s)\n",
111 pkg1, pkg2, reason);
112 if(!conflict_isin(conflict, *baddeps)) {
113 *baddeps = alpm_list_add(*baddeps, conflict);
114 } else {
115 _alpm_conflict_free(conflict);
117 return 0;
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) {
135 alpm_list_t *i;
137 if(!baddeps) {
138 return;
140 for(i = list1; i; i = i->next) {
141 alpm_pkg_t *pkg1 = i->data;
142 const char *pkg1name = alpm_pkg_get_name(pkg1);
143 alpm_list_t *j;
145 for(j = alpm_pkg_get_conflicts(pkg1); j; j = j->next) {
146 const char *conflict = j->data;
147 alpm_list_t *k;
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 */
156 continue;
159 if(_alpm_depcmp(pkg2, parsed_conflict)) {
160 if(order >= 0) {
161 add_conflict(handle, baddeps, pkg1name, pkg2name, conflict);
162 } else {
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);
180 return baddeps;
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;
191 if(db == NULL) {
192 return 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);
205 return baddeps;
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!
226 * Operations:
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,
231 int operation)
233 alpm_list_t *ret = NULL;
234 alpm_list_t *pA = filesA, *pB = filesB;
236 while(pA && pB) {
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] == '/') {
241 pA = pA->next;
242 } else if(strB[strlen(strB)-1] == '/') {
243 pB = pB->next;
244 } else {
245 int cmp = strcmp(strA, strB);
246 if(cmp < 0) {
247 if(operation == DIFFERENCE) {
248 /* item only in filesA, qualifies as a difference */
249 ret = alpm_list_add(ret, strdup(strA));
251 pA = pA->next;
252 } else if(cmp > 0) {
253 pB = pB->next;
254 } else {
255 if(operation == INTERSECT) {
256 /* item in both, qualifies as an intersect */
257 ret = alpm_list_add(ret, strdup(strA));
259 pA = pA->next;
260 pB = pB->next;
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));
272 pA = pA->next;
275 return ret;
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);
293 if(name2) {
294 STRDUP(conflict->ctarget, name2, goto error);
295 } else {
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)");
303 return conflicts;
305 error:
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);
314 FREE(conflict);
317 static int dir_belongsto_pkg(const char *root, const char *dirpath,
318 alpm_pkg_t *pkg)
320 struct dirent *ent = NULL;
321 struct stat sbuf;
322 char path[PATH_MAX];
323 char abspath[PATH_MAX];
324 DIR *dir;
326 snprintf(abspath, PATH_MAX, "%s%s", root, dirpath);
327 dir = opendir(abspath);
328 if(dir == NULL) {
329 return 1;
331 while((ent = readdir(dir)) != NULL) {
332 const char *name = ent->d_name;
334 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
335 continue;
337 snprintf(path, PATH_MAX, "%s/%s", dirpath, name);
338 snprintf(abspath, PATH_MAX, "%s%s", root, path);
339 if(stat(abspath, &sbuf) != 0) {
340 continue;
342 if(S_ISDIR(sbuf.st_mode)) {
343 if(dir_belongsto_pkg(root, path, pkg)) {
344 continue;
345 } else {
346 closedir(dir);
347 return 0;
349 } else {
350 if(alpm_list_find_str(alpm_pkg_get_files(pkg), path)) {
351 continue;
352 } else {
353 closedir(dir);
354 return 0;
358 closedir(dir);
359 return 1;
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);
370 size_t current;
371 alpm_trans_t *trans = handle->trans;
373 if(!upgrade) {
374 return NULL;
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;
384 char path[PATH_MAX];
386 p1 = i->data;
387 if(!p1) {
388 continue;
391 int percent = (current * 100) / numtargs;
392 PROGRESS(trans, PM_TRANS_PROGRESS_CONFLICTS_START, "", percent,
393 numtargs, current);
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;
399 p2 = j->data;
400 if(!p2) {
401 continue;
403 common_files = filelist_operation(alpm_pkg_get_files(p1),
404 alpm_pkg_get_files(p2), INTERSECT);
406 if(common_files) {
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) {
413 FREELIST(conflicts);
414 FREELIST(common_files);
415 return NULL;
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",
424 p1->name);
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
431 * be freed. */
432 if(dbpkg) {
433 /* older ver of package currently installed */
434 tmpfiles = filelist_operation(alpm_pkg_get_files(p1),
435 alpm_pkg_get_files(dbpkg), DIFFERENCE);
436 } else {
437 /* no version of package currently installed */
438 tmpfiles = alpm_pkg_get_files(p1);
441 for(j = tmpfiles; j; j = j->next) {
442 struct stat lsbuf;
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) {
451 continue;
454 if(path[strlen(path) - 1] == '/') {
455 struct stat sbuf;
456 if(S_ISDIR(lsbuf.st_mode)) {
457 _alpm_log(handle, PM_LOG_DEBUG, "%s is a directory, not a conflict\n", path);
458 continue;
460 stat(path, &sbuf);
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);
464 continue;
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",
481 relative_path);
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) {
488 p2 = k->data;
489 if(!p2 || strcmp(p1->name, p2->name) == 0) {
490 continue;
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",
503 filestr);
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",
515 dir, dbpkg->name);
516 resolved_conflict = dir_belongsto_pkg(handle->root, filestr, dbpkg);
518 free(dir);
521 if(!resolved_conflict && dbpkg) {
522 char *rpath = calloc(PATH_MAX, sizeof(char));
523 const char *relative_rpath;
524 if(!realpath(path, rpath)) {
525 free(rpath);
526 continue;
528 relative_rpath = rpath + strlen(handle->root);
529 if(alpm_list_find_str(alpm_pkg_get_files(dbpkg), relative_rpath)) {
530 resolved_conflict = 1;
532 free(rpath);
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) {
539 FREELIST(conflicts);
540 if(dbpkg) {
541 /* only freed if it was generated from filelist_operation() */
542 FREELIST(tmpfiles);
544 return NULL;
548 if(dbpkg) {
549 /* only freed if it was generated from filelist_operation() */
550 FREELIST(tmpfiles);
553 PROGRESS(trans, PM_TRANS_PROGRESS_CONFLICTS_START, "", 100,
554 numtargs, current);
556 return conflicts;
559 /* vim: set ts=2 sw=2 noet: */