00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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
00087
00088
00089
00090
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
00109
00110
00111
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
00125
00126
00127
00128
00129
00130
00131
00132
00133
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
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
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
00184
00185
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
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
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
00216
00217
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
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
00236 pA = pA->next;
00237 } else if(cmp > 0) {
00238
00239 pB = pB->next;
00240 } else {
00241
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
00253
00254
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
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
00277 ret = alpm_list_add(ret, strdup(strA));
00278 pA = pA->next;
00279 } else if(cmp > 0) {
00280
00281 pB = pB->next;
00282 } else {
00283
00284 pA = pA->next;
00285 pB = pB->next;
00286 }
00287 }
00288 }
00289
00290 return(ret);
00291 }
00292
00293
00294
00295
00296
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
00332
00333
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
00348
00349
00350
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
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
00385 struct stat lsbuf, sbuf;
00386 char *filestr = NULL;
00387
00388
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
00393
00394
00395 if(dbpkg) {
00396
00397 tmpfiles = chk_filedifference(alpm_pkg_get_files(p1),
00398 alpm_pkg_get_files(dbpkg));
00399 } else {
00400
00401 tmpfiles = alpm_list_strdup(alpm_pkg_get_files(p1));
00402 }
00403
00404
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
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
00431
00432
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
00451 int resolved_conflict = 0;
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
00461 alpm_list_t *pkgfiles, *localfiles;
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
00468
00469
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
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
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
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
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
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
00549 ASSERT(handle != NULL, return(NULL));
00550 ASSERT(conflict != NULL, return(NULL));
00551
00552 return conflict->ctarget;
00553 }
00554