conflict.c

Go to the documentation of this file.
00001 /*
00002  *  conflict.c
00003  *
00004  *  Copyright (c) 2002-2007 by Judd Vinet <jvinet@zeroflux.org>
00005  *  Copyright (c) 2005 by Aurelien Foret <orelien@chez.com>
00006  *  Copyright (c) 2006 by David Kimpe <dnaku@frugalware.org>
00007  *  Copyright (c) 2006 by Miklos Vajna <vmiklos@frugalware.org>
00008  *  Copyright (c) 2006 by Christian Hamar <krics@linuxforum.hu>
00009  *
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License
00021  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00022  */
00023 
00024 #include "config.h"
00025 
00026 #include <stdlib.h>
00027 #include <stdio.h>
00028 #include <unistd.h>
00029 #include <string.h>
00030 #include <limits.h>
00031 #include <sys/stat.h>
00032 
00033 /* libalpm */
00034 #include "conflict.h"
00035 #include "alpm_list.h"
00036 #include "handle.h"
00037 #include "trans.h"
00038 #include "util.h"
00039 #include "error.h"
00040 #include "log.h"
00041 #include "cache.h"
00042 #include "deps.h"
00043 
00044 pmconflict_t *_alpm_conflict_new(const char *package1, const char *package2)
00045 {
00046     pmconflict_t *conflict;
00047 
00048     ALPM_LOG_FUNC;
00049 
00050     MALLOC(conflict, sizeof(pmconflict_t), RET_ERR(PM_ERR_MEMORY, NULL));
00051 
00052     STRDUP(conflict->package1, package1, RET_ERR(PM_ERR_MEMORY, NULL));
00053     STRDUP(conflict->package2, package2, RET_ERR(PM_ERR_MEMORY, NULL));
00054 
00055     return(conflict);
00056 }
00057 
00058 void _alpm_conflict_free(pmconflict_t *conflict)
00059 {
00060     FREE(conflict->package2);
00061     FREE(conflict->package1);
00062     FREE(conflict);
00063 }
00064 
00065 int _alpm_conflict_isin(pmconflict_t *needle, alpm_list_t *haystack)
00066 {
00067     alpm_list_t *i;
00068 
00069     ALPM_LOG_FUNC;
00070 
00071     for(i = haystack; i; i = i->next) {
00072         pmconflict_t *conflict = i->data;
00073         char *cpkg1 = conflict->package1;
00074         char *cpkg2 = conflict->package2;
00075         char *npkg1 = needle->package1;
00076         char *npkg2 = needle->package2;
00077         if((!strcmp(cpkg1, npkg1)  && !strcmp(cpkg2, npkg2))
00078                 || (!strcmp(cpkg1, npkg2) && !strcmp(cpkg2, npkg1))) {
00079             return(1);
00080         }
00081     }
00082 
00083     return(0);
00084 }
00085 
00086 /** Check if pkg1 conflicts with pkg2
00087  * @param pkg1 package we are looking at
00088  * @param conflict name of the possible conflict
00089  * @param pkg2 package to check
00090  * @return 0 for no conflict, non-zero otherwise
00091  */
00092 static int does_conflict(pmpkg_t *pkg1, const char *conflict, pmpkg_t *pkg2)
00093 {
00094     const char *pkg1name = alpm_pkg_get_name(pkg1);
00095     const char *pkg2name = alpm_pkg_get_name(pkg2);
00096     pmdepend_t *conf = alpm_splitdep(conflict);
00097     int match = 0;
00098 
00099     match = alpm_depcmp(pkg2, conf);
00100     if(match) {
00101         _alpm_log(PM_LOG_DEBUG, "package %s conflicts with %s (by %s)\n",
00102                 pkg1name, pkg2name, conflict);
00103     }
00104     _alpm_dep_free(conf);
00105     return(match);
00106 }
00107 
00108 /** Adds the pkg1/pkg2 conflict to the baddeps list
00109  * @param *baddeps list to add conflict to
00110  * @param pkg1 first package
00111  * @param pkg2 package causing conflict
00112  */
00113 static void add_conflict(alpm_list_t **baddeps, const char *pkg1,
00114         const char *pkg2)
00115 {
00116     pmconflict_t *conflict = _alpm_conflict_new(pkg1, pkg2);
00117     if(conflict && !_alpm_conflict_isin(conflict, *baddeps)) {
00118         *baddeps = alpm_list_add(*baddeps, conflict);
00119     } else {
00120         _alpm_conflict_free(conflict);
00121     }
00122 }
00123 
00124 /** Check if packages from list1 conflict with packages from list2.
00125  * This looks at the conflicts fields of all packages from list1, and sees
00126  * if they match packages from list2.
00127  * If a conflict (pkg1, pkg2) is found, it is added to the baddeps list
00128  * in this order if order >= 0, or reverse order (pkg2,pkg1) otherwise.
00129  *
00130  * @param list1 first list of packages
00131  * @param list2 second list of packages
00132  * @param *baddeps list to store conflicts
00133  * @param order if >= 0 the conflict order is preserved, if < 0 it's reversed
00134  */
00135 static void check_conflict(alpm_list_t *list1, alpm_list_t *list2,
00136         alpm_list_t **baddeps, int order) {
00137     alpm_list_t *i, *j, *k;
00138 
00139     if(!baddeps) {
00140         return;
00141     }
00142     for(i = list1; i; i = i->next) {
00143         pmpkg_t *pkg1 = i->data;
00144         const char *pkg1name = alpm_pkg_get_name(pkg1);
00145 
00146         for(j = alpm_pkg_get_conflicts(pkg1); j; j = j->next) {
00147             const char *conflict = j->data;
00148 
00149             for(k = list2; k; k = k->next) {
00150                 pmpkg_t *pkg2 = k->data;
00151                 const char *pkg2name = alpm_pkg_get_name(pkg2);
00152 
00153                 if(strcmp(pkg1name, pkg2name) == 0) {
00154                     /* skip the package we're currently processing */
00155                     continue;
00156                 }
00157 
00158                 if(does_conflict(pkg1, conflict, pkg2)) {
00159                     if(order >= 0) {
00160                         add_conflict(baddeps, pkg1name, pkg2name);
00161                     } else {
00162                         add_conflict(baddeps, pkg2name, pkg1name);
00163                     }
00164                 }
00165             }
00166         }
00167     }
00168 }
00169 
00170 /* Check for inter-conflicts */
00171 alpm_list_t *_alpm_innerconflicts(alpm_list_t *packages)
00172 {
00173     alpm_list_t *baddeps = NULL;
00174 
00175     ALPM_LOG_FUNC;
00176 
00177     _alpm_log(PM_LOG_DEBUG, "check targets vs targets\n");
00178     check_conflict(packages, packages, &baddeps, 0);
00179 
00180     return(baddeps);
00181 }
00182 
00183 /* Check for target vs (db - target) conflicts
00184  * In case of conflict the package1 field of pmdepconflict_t contains
00185  * the target package, package2 field contains the local package
00186  */
00187 alpm_list_t *_alpm_outerconflicts(pmdb_t *db, alpm_list_t *packages)
00188 {
00189     alpm_list_t *baddeps = NULL;
00190 
00191     ALPM_LOG_FUNC;
00192 
00193     if(db == NULL) {
00194         return(NULL);
00195     }
00196 
00197     alpm_list_t *dblist = alpm_list_diff(_alpm_db_get_pkgcache(db), packages,
00198             _alpm_pkg_cmp);
00199 
00200     /* two checks to be done here for conflicts */
00201     _alpm_log(PM_LOG_DEBUG, "check targets vs db\n");
00202     check_conflict(packages, dblist, &baddeps, 1);
00203     _alpm_log(PM_LOG_DEBUG, "check db vs targets\n");
00204     check_conflict(dblist, packages, &baddeps, -1);
00205 
00206     alpm_list_free(dblist);
00207     return(baddeps);
00208 }
00209 
00210 /* Check for transaction conflicts */
00211 alpm_list_t *_alpm_checkconflicts(pmdb_t *db, alpm_list_t *packages) {
00212     return(alpm_list_join(_alpm_innerconflicts(packages), _alpm_outerconflicts(db, packages)));
00213 }
00214 
00215 /* Returns a alpm_list_t* of file conflicts.
00216  *  Hooray for set-intersects!
00217  *  Pre-condition: both lists are sorted!
00218  */
00219 static alpm_list_t *chk_fileconflicts(alpm_list_t *filesA, alpm_list_t *filesB)
00220 {
00221     alpm_list_t *ret = NULL;
00222     alpm_list_t *pA = filesA, *pB = filesB;
00223 
00224     while(pA && pB) {
00225         const char *strA = pA->data;
00226         const char *strB = pB->data;
00227         /* skip directories, we don't care about dir conflicts */
00228         if(strA[strlen(strA)-1] == '/') {
00229             pA = pA->next;
00230         } else if(strB[strlen(strB)-1] == '/') {
00231             pB = pB->next;
00232         } else {
00233             int cmp = strcmp(strA, strB);
00234             if(cmp < 0) {
00235                 /* item only in filesA, ignore it */
00236                 pA = pA->next;
00237             } else if(cmp > 0) {
00238                 /* item only in filesB, ignore it */
00239                 pB = pB->next;
00240             } else {
00241                 /* item in both, record it */
00242                 ret = alpm_list_add(ret, strdup(strA));
00243                 pA = pA->next;
00244                 pB = pB->next;
00245             }
00246       }
00247     }
00248 
00249     return(ret);
00250 }
00251 
00252 /* Returns a alpm_list_t* of files that are in filesA but *NOT* in filesB
00253  *  This is an 'A minus B' set operation
00254  *  Pre-condition: both lists are sorted!
00255  */
00256 static alpm_list_t *chk_filedifference(alpm_list_t *filesA, alpm_list_t *filesB)
00257 {
00258     alpm_list_t *ret = NULL;
00259     alpm_list_t *pA = filesA, *pB = filesB;
00260 
00261     if(pB == NULL) {
00262         return(alpm_list_strdup(pA));
00263     }
00264 
00265     while(pA && pB) {
00266         const char *strA = pA->data;
00267         const char *strB = pB->data;
00268         /* skip directories, we don't care about dir conflicts */
00269         if(strA[strlen(strA)-1] == '/') {
00270             pA = pA->next;
00271         } else if(strB[strlen(strB)-1] == '/') {
00272             pB = pB->next;
00273         } else {
00274             int cmp = strcmp(strA, strB);
00275             if(cmp < 0) {
00276                 /* item only in filesA, record it */
00277                 ret = alpm_list_add(ret, strdup(strA));
00278                 pA = pA->next;
00279             } else if(cmp > 0) {
00280                 /* item only in fileB, but this means nothing */
00281                 pB = pB->next;
00282             } else {
00283                 /* item in both, ignore it */
00284                 pA = pA->next;
00285                 pB = pB->next;
00286             }
00287       }
00288     }
00289 
00290     return(ret);
00291 }
00292 
00293 /* Adds pmfileconflict_t to a conflicts list. Pass the conflicts list, type (either
00294  * PM_FILECONFLICT_TARGET or PM_FILECONFLICT_FILESYSTEM), a file string, and either
00295  * two package names or one package name and NULL. This is a wrapper for former
00296  * functionality that was done inline.
00297  */
00298 static alpm_list_t *add_fileconflict(alpm_list_t *conflicts,
00299                     pmfileconflicttype_t type, const char *filestr,
00300                                         const char* name1, const char* name2)
00301 {
00302     pmfileconflict_t *conflict;
00303     MALLOC(conflict, sizeof(pmfileconflict_t), RET_ERR(PM_ERR_MEMORY, NULL));
00304 
00305     conflict->type = type;
00306     STRDUP(conflict->target, name1, RET_ERR(PM_ERR_MEMORY, NULL));
00307     STRDUP(conflict->file, filestr, RET_ERR(PM_ERR_MEMORY, NULL));
00308     if(name2) {
00309         STRDUP(conflict->ctarget, name2, RET_ERR(PM_ERR_MEMORY, NULL));
00310     } else {
00311         conflict->ctarget = "";
00312     }
00313 
00314     conflicts = alpm_list_add(conflicts, conflict);
00315     _alpm_log(PM_LOG_DEBUG, "found file conflict %s, packages %s and %s\n",
00316               filestr, name1, name2 ? name2 : "(filesystem)");
00317 
00318     return(conflicts);
00319 }
00320 
00321 void _alpm_fileconflict_free(pmfileconflict_t *conflict)
00322 {
00323     if(strlen(conflict->ctarget) > 0) {
00324         FREE(conflict->ctarget);
00325     }
00326     FREE(conflict->file);;
00327     FREE(conflict->target);
00328     FREE(conflict);
00329 }
00330 
00331 /* Find file conflicts that may occur during the transaction with two checks:
00332  * 1: check every target against every target
00333  * 2: check every target against the filesystem */
00334 alpm_list_t *_alpm_db_find_fileconflicts(pmdb_t *db, pmtrans_t *trans, char *root)
00335 {
00336     alpm_list_t *i, *conflicts = NULL;
00337     alpm_list_t *targets = trans->packages;
00338     int numtargs = alpm_list_count(targets);
00339     int current;
00340 
00341     ALPM_LOG_FUNC;
00342 
00343     if(db == NULL || targets == NULL || root == NULL) {
00344         return(NULL);
00345     }
00346 
00347     /* TODO this whole function needs a huge change, which hopefully will
00348      * be possible with real transactions. Right now we only do half as much
00349      * here as we do when we actually extract files in add.c with our 12
00350      * different cases. */
00351     for(current = 1, i = targets; i; i = i->next, current++) {
00352         alpm_list_t *j, *k, *tmpfiles = NULL;
00353         pmpkg_t *p1, *p2, *dbpkg;
00354         char path[PATH_MAX+1];
00355 
00356         p1 = i->data;
00357         if(!p1) {
00358             continue;
00359         }
00360 
00361         double percent = (double)current / numtargs;
00362         PROGRESS(trans, PM_TRANS_PROGRESS_CONFLICTS_START, "", (percent * 100),
00363                  numtargs, current);
00364         /* CHECK 1: check every target against every target */
00365         for(j = i->next; j; j = j->next) {
00366             p2 = j->data;
00367             if(!p2) {
00368                 continue;
00369             }
00370             _alpm_log(PM_LOG_DEBUG, "searching for file conflicts: %s and %s\n",
00371                                 alpm_pkg_get_name(p1), alpm_pkg_get_name(p2));
00372             tmpfiles = chk_fileconflicts(alpm_pkg_get_files(p1), alpm_pkg_get_files(p2));
00373 
00374             if(tmpfiles) {
00375                 for(k = tmpfiles; k; k = k->next) {
00376                     snprintf(path, PATH_MAX, "%s%s", root, (char *)k->data);
00377                     conflicts = add_fileconflict(conflicts, PM_FILECONFLICT_TARGET, path,
00378                             alpm_pkg_get_name(p1), alpm_pkg_get_name(p2));
00379                 }
00380                 FREELIST(tmpfiles);
00381             }
00382         }
00383 
00384         /* declarations for second check */
00385         struct stat lsbuf, sbuf;
00386         char *filestr = NULL;
00387 
00388         /* CHECK 2: check every target against the filesystem */
00389         _alpm_log(PM_LOG_DEBUG, "searching for filesystem conflicts: %s\n", p1->name);
00390         dbpkg = _alpm_db_get_pkgfromcache(db, p1->name);
00391 
00392         /* Do two different checks here. f the package is currently installed,
00393          * then only check files that are new in the new package. If the package
00394          * is not currently installed, then simply stat the whole filelist */
00395         if(dbpkg) {
00396             /* older ver of package currently installed */
00397             tmpfiles = chk_filedifference(alpm_pkg_get_files(p1),
00398                     alpm_pkg_get_files(dbpkg));
00399         } else {
00400             /* no version of package currently installed */
00401             tmpfiles = alpm_list_strdup(alpm_pkg_get_files(p1));
00402         }
00403 
00404         /* loop over each file to be installed */
00405         for(j = tmpfiles; j; j = j->next) {
00406             int skip_conflict = 0;
00407             filestr = j->data;
00408 
00409             snprintf(path, PATH_MAX, "%s%s", root, filestr);
00410 
00411             /* stat the file - if it exists, do some checks */
00412             if(_alpm_lstat(path, &lsbuf) != 0) {
00413                 continue;
00414             }
00415             stat(path, &sbuf);
00416 
00417             if(path[strlen(path)-1] == '/') {
00418                 if(S_ISDIR(lsbuf.st_mode)) {
00419                     _alpm_log(PM_LOG_DEBUG, "%s is a directory, not a conflict\n", path);
00420                     skip_conflict = 1;
00421                 } else if(S_ISLNK(lsbuf.st_mode) && S_ISDIR(sbuf.st_mode)) {
00422                     _alpm_log(PM_LOG_DEBUG,
00423                             "%s is a symlink to a dir, hopefully not a conflict\n", path);
00424                     skip_conflict = 1;
00425                 }
00426             }
00427             if(!skip_conflict) {
00428                 _alpm_log(PM_LOG_DEBUG, "checking possible conflict: %s\n", path);
00429 
00430                 /* Make sure the possible conflict is not a symlink that points to a
00431                  * path in the old package. This is kind of dirty with inode usage */
00432                 /* TODO this seems ripe for a cleanup */
00433                 if(dbpkg) {
00434                     struct stat pkgbuf;
00435                     char str[PATH_MAX+1];
00436                     unsigned ok = 0;
00437                     for(k = dbpkg->files; k; k = k->next) {
00438                         snprintf(str, PATH_MAX, "%s%s", root, (char*)k->data);
00439                         if(!_alpm_lstat(str, &pkgbuf) && lsbuf.st_ino == pkgbuf.st_ino) {
00440                             ok = 1;
00441                             _alpm_log(PM_LOG_DEBUG, "conflict was a symlink: %s\n", path);
00442                             break;
00443                         }
00444                     }
00445                     if(ok == 1) {
00446                         continue;
00447                     }
00448                 }
00449 
00450                 /* Look at all the targets to see if file has changed hands */
00451                 int resolved_conflict = 0; /* have we acted on this conflict? */
00452                 for(k = targets; k; k = k->next) {
00453                     p2 = k->data;
00454                     if(!p2 || strcmp(p1->name, p2->name) == 0) {
00455                         continue;
00456                     }
00457 
00458                     pmpkg_t *localp2 = _alpm_db_get_pkgfromcache(db, p2->name);
00459 
00460                     /* Check if it used to exist in a package, but doesn't anymore */
00461                     alpm_list_t *pkgfiles, *localfiles; /* added for readability */
00462                     pkgfiles = alpm_pkg_get_files(p2);
00463                     localfiles = alpm_pkg_get_files(localp2);
00464 
00465                     if(localp2 && !alpm_list_find_str(pkgfiles, filestr)
00466                          && alpm_list_find_str(localfiles, filestr)) {
00467                         /* skip removal of file, but not add. this will prevent a second
00468                          * package from removing the file when it was already installed
00469                          * by its new owner (whether the file is in backup array or not */
00470                         trans->skip_remove = alpm_list_add(trans->skip_remove, strdup(path));
00471                         _alpm_log(PM_LOG_DEBUG, "file changed packages, adding to remove skiplist: %s\n", filestr);
00472                         resolved_conflict = 1;
00473                         break;
00474                     }
00475                 }
00476                 if(!resolved_conflict) {
00477                     _alpm_log(PM_LOG_DEBUG, "file found in conflict: %s\n", path);
00478                     conflicts = add_fileconflict(conflicts, PM_FILECONFLICT_FILESYSTEM,
00479                             path, p1->name, NULL);
00480                 }
00481             }
00482         }
00483         FREELIST(tmpfiles);
00484     }
00485 
00486     return(conflicts);
00487 }
00488 
00489 const char SYMEXPORT *alpm_conflict_get_package1(pmconflict_t *conflict)
00490 {
00491     ALPM_LOG_FUNC;
00492 
00493     /* Sanity checks */
00494     ASSERT(handle != NULL, return(NULL));
00495     ASSERT(conflict != NULL, return(NULL));
00496 
00497     return conflict->package1;
00498 }
00499 
00500 const char SYMEXPORT *alpm_conflict_get_package2(pmconflict_t *conflict)
00501 {
00502     ALPM_LOG_FUNC;
00503 
00504     /* Sanity checks */
00505     ASSERT(handle != NULL, return(NULL));
00506     ASSERT(conflict != NULL, return(NULL));
00507 
00508     return conflict->package2;
00509 }
00510 
00511 const char SYMEXPORT *alpm_fileconflict_get_target(pmfileconflict_t *conflict)
00512 {
00513     ALPM_LOG_FUNC;
00514 
00515     /* Sanity checks */
00516     ASSERT(handle != NULL, return(NULL));
00517     ASSERT(conflict != NULL, return(NULL));
00518 
00519     return conflict->target;
00520 }
00521 
00522 pmfileconflicttype_t SYMEXPORT alpm_fileconflict_get_type(pmfileconflict_t *conflict)
00523 {
00524     ALPM_LOG_FUNC;
00525 
00526     /* Sanity checks */
00527     ASSERT(handle != NULL, return(-1));
00528     ASSERT(conflict != NULL, return(-1));
00529 
00530     return conflict->type;
00531 }
00532 
00533 const char SYMEXPORT *alpm_fileconflict_get_file(pmfileconflict_t *conflict)
00534 {
00535     ALPM_LOG_FUNC;
00536 
00537     /* Sanity checks */
00538     ASSERT(handle != NULL, return(NULL));
00539     ASSERT(conflict != NULL, return(NULL));
00540 
00541     return conflict->file;
00542 }
00543 
00544 const char SYMEXPORT *alpm_fileconflict_get_ctarget(pmfileconflict_t *conflict)
00545 {
00546     ALPM_LOG_FUNC;
00547 
00548     /* Sanity checks */
00549     ASSERT(handle != NULL, return(NULL));
00550     ASSERT(conflict != NULL, return(NULL));
00551 
00552     return conflict->ctarget;
00553 }
00554 /* vim: set ts=2 sw=2 noet: */

Generated on Mon Jan 14 23:53:40 2008 for libalpm by  doxygen 1.5.4