Remove global handle dependencies from sync/upgrade paths
[pacman-ng.git] / lib / libalpm / conflict.c
blob89214707b4c5f01dc7066a57d245134ea7302dbd
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 pmconflict_t *_alpm_conflict_new(const char *package1, const char *package2,
44 const char *reason)
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));
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), 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));
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 *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",
106 pkg1, pkg2, reason);
107 if(conflict && !conflict_isin(conflict, *baddeps)) {
108 *baddeps = alpm_list_add(*baddeps, conflict);
109 } else {
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;
129 if(!baddeps) {
130 return;
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 */
146 continue;
149 if(_alpm_depcmp(pkg2, parsed_conflict)) {
150 if(order >= 0) {
151 add_conflict(baddeps, pkg1name, pkg2name, conflict);
152 } else {
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);
170 return baddeps;
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;
181 if(db == NULL) {
182 return 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);
195 return baddeps;
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!
212 * Operations:
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,
217 int operation)
219 alpm_list_t *ret = NULL;
220 alpm_list_t *pA = filesA, *pB = filesB;
222 while(pA && pB) {
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] == '/') {
227 pA = pA->next;
228 } else if(strB[strlen(strB)-1] == '/') {
229 pB = pB->next;
230 } else {
231 int cmp = strcmp(strA, strB);
232 if(cmp < 0) {
233 if(operation == DIFFERENCE) {
234 /* item only in filesA, qualifies as a difference */
235 ret = alpm_list_add(ret, strdup(strA));
237 pA = pA->next;
238 } else if(cmp > 0) {
239 pB = pB->next;
240 } else {
241 if(operation == INTERSECT) {
242 /* item in both, qualifies as an intersect */
243 ret = alpm_list_add(ret, strdup(strA));
245 pA = pA->next;
246 pB = pB->next;
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));
258 pA = pA->next;
261 return ret;
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));
279 if(name2) {
280 STRDUP(conflict->ctarget, name2, RET_ERR(PM_ERR_MEMORY, NULL));
281 } else {
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)");
289 return conflicts;
292 void _alpm_fileconflict_free(pmfileconflict_t *conflict)
294 FREE(conflict->ctarget);
295 FREE(conflict->file);
296 FREE(conflict->target);
297 FREE(conflict);
300 static int dir_belongsto_pkg(char *dirpath, pmpkg_t *pkg)
302 struct dirent *ent = NULL;
303 struct stat sbuf;
304 char path[PATH_MAX];
305 char abspath[PATH_MAX];
306 DIR *dir;
308 snprintf(abspath, PATH_MAX, "%s%s", pkg->handle->root, dirpath);
309 dir = opendir(abspath);
310 if(dir == NULL) {
311 return 1;
313 while((ent = readdir(dir)) != NULL) {
314 const char *name = ent->d_name;
316 if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
317 continue;
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) {
322 continue;
324 if(S_ISDIR(sbuf.st_mode)) {
325 if(dir_belongsto_pkg(path, pkg)) {
326 continue;
327 } else {
328 closedir(dir);
329 return 0;
331 } else {
332 if(alpm_list_find_str(alpm_pkg_get_files(pkg), path)) {
333 continue;
334 } else {
335 closedir(dir);
336 return 0;
340 closedir(dir);
341 return 1;
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);
352 size_t current;
353 pmtrans_t *trans = handle->trans;
355 if(!upgrade) {
356 return NULL;
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];
368 p1 = i->data;
369 if(!p1) {
370 continue;
373 int percent = (current * 100) / numtargs;
374 PROGRESS(trans, PM_TRANS_PROGRESS_CONFLICTS_START, "", percent,
375 numtargs, current);
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) {
380 p2 = j->data;
381 if(!p2) {
382 continue;
384 tmpfiles = filelist_operation( alpm_pkg_get_files(p1),
385 alpm_pkg_get_files(p2), INTERSECT);
387 if(tmpfiles) {
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));
393 FREELIST(tmpfiles);
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 */
408 if(dbpkg) {
409 /* older ver of package currently installed */
410 tmpfiles = filelist_operation(alpm_pkg_get_files(p1),
411 alpm_pkg_get_files(dbpkg), DIFFERENCE);
412 } else {
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) {
418 filestr = j->data;
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) {
424 continue;
426 stat(path, &sbuf);
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);
431 continue;
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);
435 continue;
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) {
453 p2 = k->data;
454 if(!p2 || strcmp(p1->name, p2->name) == 0) {
455 continue;
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",
476 dir, dbpkg->name);
477 resolved_conflict = dir_belongsto_pkg(filestr, dbpkg);
479 free(dir);
482 if(!resolved_conflict && dbpkg) {
483 char *rpath = calloc(PATH_MAX+1, sizeof(char));
484 if(!realpath(path, rpath)) {
485 FREE(rpath);
486 continue;
488 char *filestr = rpath + strlen(handle->root);
489 if(alpm_list_find_str(alpm_pkg_get_files(dbpkg),filestr)) {
490 resolved_conflict = 1;
492 free(rpath);
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);
501 FREELIST(tmpfiles);
503 PROGRESS(trans, PM_TRANS_PROGRESS_CONFLICTS_START, "", 100,
504 numtargs, current);
506 return conflicts;
509 const char SYMEXPORT *alpm_conflict_get_package1(pmconflict_t *conflict)
511 /* Sanity checks */
512 ASSERT(conflict != NULL, return NULL);
514 return conflict->package1;
517 const char SYMEXPORT *alpm_conflict_get_package2(pmconflict_t *conflict)
519 /* Sanity checks */
520 ASSERT(conflict != NULL, return NULL);
522 return conflict->package2;
525 const char SYMEXPORT *alpm_conflict_get_reason(pmconflict_t *conflict)
527 /* Sanity checks */
528 ASSERT(conflict != NULL, return NULL);
530 return conflict->reason;
533 const char SYMEXPORT *alpm_fileconflict_get_target(pmfileconflict_t *conflict)
535 /* Sanity checks */
536 ASSERT(conflict != NULL, return NULL);
538 return conflict->target;
541 pmfileconflicttype_t SYMEXPORT alpm_fileconflict_get_type(pmfileconflict_t *conflict)
543 /* Sanity checks */
544 ASSERT(conflict != NULL, return -1);
546 return conflict->type;
549 const char SYMEXPORT *alpm_fileconflict_get_file(pmfileconflict_t *conflict)
551 /* Sanity checks */
552 ASSERT(conflict != NULL, return NULL);
554 return conflict->file;
557 const char SYMEXPORT *alpm_fileconflict_get_ctarget(pmfileconflict_t *conflict)
559 /* Sanity checks */
560 ASSERT(conflict != NULL, return NULL);
562 return conflict->ctarget;
564 /* vim: set ts=2 sw=2 noet: */