1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757
|
/**************************************************************************
* Copyright (C) 2010-2011 by Eugene V. Lyubimkin *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License *
* (version 3 or above) as published by the Free Software Foundation. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU GPL *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA *
**************************************************************************/
#include <iostream>
using std::cout;
using std::endl;
#include <stack>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <boost/lexical_cast.hpp>
#include "../common.hpp"
#include "../handlers.hpp"
#include "../misc.hpp"
#include "../selectors.hpp"
#include "../colorizer.hpp"
#include <cupt/system/state.hpp>
#include <cupt/system/resolver.hpp>
#include <cupt/system/resolvers/native.hpp>
#include <cupt/system/snapshots.hpp>
#include <cupt/file.hpp>
#include <cupt/system/worker.hpp>
#include <cupt/cache/sourceversion.hpp>
#include <cupt/cache/releaseinfo.hpp>
#include <cupt/packagename.hpp>
#include <cupt/versionstring.hpp>
typedef Worker::Action WA;
const WA::Type fakeNotPreferredVersionAction = WA::Type(999);
const WA::Type fakeAutoRemove = WA::Type(1000);
const WA::Type fakeAutoPurge = WA::Type(1001);
const WA::Type fakeBecomeAutomaticallyInstalled = WA::Type(1002);
const WA::Type fakeBecomeManuallyInstalled = WA::Type(1003);
struct VersionChoices
{
string packageName;
vector< const BinaryVersion* > versions;
string getFullAnnotation(const string& requestAnnotation) const
{
return requestAnnotation + format2(" | for package '%s'", packageName);
}
};
struct ManagePackagesContext
{
enum class AutoInstall { Yes, No, Nop };
enum class SelectType { Traditional, Flexible };
ManagePackages::Mode mode;
AutoInstall autoinstall;
SelectType selectType;
Resolver::RequestImportance importance;
Config* config;
const Cache* cache;
Resolver* resolver;
Worker* worker;
bool oneLetterSuffixDeprecationWarningIssued = false;
typedef vector< VersionChoices > SelectedVersions;
SelectedVersions selectVersions(const string& expression, bool throwOnError = true)
{
auto selector = (selectType == SelectType::Traditional ?
selectBinaryVersionsWildcarded : selectAllBinaryVersionsWildcarded);
SelectedVersions result;
// grouping by package
for (auto version: selector(*cache, expression, throwOnError))
{
if (result.empty() || result.back().packageName != version->packageName)
{
result.push_back(VersionChoices{version->packageName, {}});
}
result.back().versions.push_back(version);
}
return result;
}
void install(const VersionChoices& versionChoices, const string& requestAnnotation)
{
resolver->installVersion(versionChoices.versions,
versionChoices.getFullAnnotation(requestAnnotation), importance);
}
void remove(const VersionChoices& versionChoices, const string& requestAnnotation)
{
const auto& fullAnnotation = versionChoices.getFullAnnotation(requestAnnotation);
if (selectType == SelectType::Traditional)
{
// removing all the package regardless of what versions of packages
// were chosen
if (!versionChoices.versions.empty())
{
const string& packageName = versionChoices.packageName;
resolver->removeVersions(cache->getBinaryPackage(packageName)->getVersions(), fullAnnotation, importance);
}
}
else
{
resolver->removeVersions(versionChoices.versions, fullAnnotation, importance);
}
}
void satisfy(const RelationExpression& relationExpression, bool inverted, const string& requestAnnotation)
{
resolver->satisfyRelationExpression(relationExpression,
inverted, requestAnnotation, importance, autoinstall != AutoInstall::No);
}
};
static const char* modeToString(ManagePackages::Mode mode)
{
switch (mode)
{
case ManagePackages::Install: return "install";
case ManagePackages::InstallIfInstalled: return "iii";
case ManagePackages::Remove: return "remove";
case ManagePackages::Purge: return "purge";
case ManagePackages::BuildDepends: return "build-dependencies of";
case ManagePackages::Reinstall: return "reinstall";
default: return "";
}
}
static string getRequestAnnotation(ManagePackages::Mode mode, const string& expression)
{
return format2("%s %s", modeToString(mode), expression);
}
static void preProcessMode(ManagePackagesContext& mpc)
{
if (mpc.mode == ManagePackages::SelfUpgrade) {
mpc.mode = ManagePackages::Install;
}
if (mpc.mode == ManagePackages::FullUpgrade || mpc.mode == ManagePackages::SafeUpgrade)
{
if (mpc.mode == ManagePackages::SafeUpgrade)
{
mpc.config->setScalar("cupt::resolver::no-remove", "yes");
}
mpc.resolver->upgrade();
// despite the main action is {safe,full}-upgrade, allow package
// modifiers in the command line just as with the install command
mpc.mode = ManagePackages::Install;
}
else if (mpc.mode == ManagePackages::Satisfy || mpc.mode == ManagePackages::BuildDepends)
{
mpc.config->setScalar("apt::install-recommends", "no");
mpc.config->setScalar("apt::install-suggests", "no");
}
else if (mpc.mode == ManagePackages::BuildDepends)
{
mpc.satisfy(RelationExpression("build-essential"), false, string());
}
}
static void unrollFileArguments(vector< string >& arguments)
{
vector< string > newArguments;
for (const string& argument: arguments)
{
if (!argument.empty() && argument[0] == '@')
{
const string path = argument.substr(1);
// reading package expressions from file
RequiredFile file(path, "r");
string line;
while (!file.getLine(line).eof())
{
newArguments.push_back(line);
}
}
else
{
newArguments.push_back(argument);
}
}
arguments.swap(newArguments);
}
void __satisfy_or_unsatisfy(ManagePackagesContext& mpc,
const RelationLine& relationLine, ManagePackages::Mode mode, const string& annotation = string())
{
for (const auto& relationExpression: relationLine)
{
mpc.satisfy(relationExpression, (mode == ManagePackages::Unsatisfy), annotation);
}
}
static void processSatisfyExpression(ManagePackagesContext& mpc, string packageExpression)
{
auto localMode = mpc.mode;
if (localMode == ManagePackages::Satisfy && !packageExpression.empty() && *(packageExpression.rbegin()) == '-')
{
localMode = ManagePackages::Unsatisfy;
packageExpression.erase(packageExpression.end() - 1);
}
auto relationLine = ArchitecturedRelationLine(packageExpression)
.toRelationLine(mpc.config->getString("apt::architecture"));
__satisfy_or_unsatisfy(mpc, relationLine, localMode);
}
static void processBuildDependsExpression(ManagePackagesContext& mpc, const string& packageExpression)
{
auto architecture = mpc.config->getString("apt::architecture");
auto versions = selectSourceVersionsWildcarded(*mpc.cache, packageExpression);
auto annotation = getRequestAnnotation(mpc.mode, packageExpression);
for (const auto& version: versions)
{
__satisfy_or_unsatisfy(mpc, version->relations[SourceVersion::RelationTypes::BuildDepends]
.toRelationLine(architecture), ManagePackages::Satisfy, annotation);
__satisfy_or_unsatisfy(mpc, version->relations[SourceVersion::RelationTypes::BuildDependsIndep]
.toRelationLine(architecture), ManagePackages::Satisfy, annotation);
__satisfy_or_unsatisfy(mpc, version->relations[SourceVersion::RelationTypes::BuildDependsArch]
.toRelationLine(architecture), ManagePackages::Satisfy, annotation);
__satisfy_or_unsatisfy(mpc, version->relations[SourceVersion::RelationTypes::BuildConflicts]
.toRelationLine(architecture), ManagePackages::Unsatisfy, annotation);
__satisfy_or_unsatisfy(mpc, version->relations[SourceVersion::RelationTypes::BuildConflictsIndep]
.toRelationLine(architecture), ManagePackages::Unsatisfy, annotation);
__satisfy_or_unsatisfy(mpc, version->relations[SourceVersion::RelationTypes::BuildConflictsArch]
.toRelationLine(architecture), ManagePackages::Unsatisfy, annotation);
}
}
static void processInstallOrRemoveExpression(ManagePackagesContext& mpc, string packageExpression)
{
auto localMode = mpc.mode;
auto versions = mpc.selectVersions(packageExpression, false);
if (versions.empty())
{
/* we have a funny situation with package names like 'g++',
where one don't know is there simple package name or '+'/'-'
modifier at the end of package name, so we enter here only if
it seems that there is no such binary package */
// "localizing" action to make it modifiable by package modifiers
if (!packageExpression.empty())
{
const auto oldPackageExpression = packageExpression;
const char& lastLetter = *(packageExpression.end() - 1);
if (lastLetter == '+')
{
localMode = ManagePackages::Install;
packageExpression.erase(packageExpression.end() - 1);
}
else if (lastLetter == '-')
{
localMode = ManagePackages::Remove;
packageExpression.erase(packageExpression.end() - 1);
}
if (!mpc.oneLetterSuffixDeprecationWarningIssued && oldPackageExpression != packageExpression)
{
warn2("Package suffixes '+' and '-' are deprecated. Please use '--install' and '--remove', respectively.");
mpc.oneLetterSuffixDeprecationWarningIssued = true;
}
}
}
if (localMode == ManagePackages::Install || localMode == ManagePackages::InstallIfInstalled)
{
if (versions.empty())
{
versions = mpc.selectVersions(packageExpression);
}
for (const auto& versionChoices: versions)
{
const auto& packageName = versionChoices.packageName;
if (localMode == ManagePackages::InstallIfInstalled)
{
if (!isPackageInstalled(*mpc.cache, packageName))
{
continue;
}
}
mpc.install(versionChoices, getRequestAnnotation(localMode, packageExpression));
if (mpc.autoinstall == ManagePackagesContext::AutoInstall::Yes)
{
mpc.resolver->setAutomaticallyInstalledFlag(packageName, true);
}
else if (mpc.autoinstall == ManagePackagesContext::AutoInstall::No)
{
mpc.resolver->setAutomaticallyInstalledFlag(packageName, false);
}
}
}
else // ManagePackages::Remove or ManagePackages::Purge
{
if (versions.empty())
{
// retry, still non-fatal to deal with packages in 'config-files' state
versions = mpc.selectVersions(packageExpression, false);
}
auto scheduleRemoval = [&mpc, localMode, &packageExpression](const VersionChoices& versionChoices)
{
mpc.remove(versionChoices, getRequestAnnotation(localMode, packageExpression));
if (localMode == ManagePackages::Purge)
{
mpc.worker->setPackagePurgeFlag(versionChoices.packageName, true);
}
};
if (!versions.empty())
{
for (const auto& versionChoices: versions)
{
scheduleRemoval(versionChoices);
}
}
else
{
checkPackageName(packageExpression);
if (!mpc.cache->getSystemState()->getInstalledInfo(packageExpression) &&
!getBinaryPackage(*mpc.cache, packageExpression, false))
{
fatal2(__("unable to find binary package/expression '%s'"), packageExpression);
}
scheduleRemoval(VersionChoices{packageExpression, {}});
}
}
}
static void processAutoFlagChangeExpression(ManagePackagesContext& mpc,
const string& packageExpression)
{
getBinaryPackage(*mpc.cache, packageExpression); // will throw if package name is wrong
mpc.resolver->setAutomaticallyInstalledFlag(packageExpression,
(mpc.mode == ManagePackages::Markauto));
}
static void processReinstallExpression(ManagePackagesContext& mpc, const string& packageExpression)
{
auto package = getBinaryPackage(*mpc.cache, packageExpression);
auto installedVersion = package->getInstalledVersion();
if (!installedVersion)
{
fatal2(__("the package '%s' is not installed"), packageExpression);
}
const auto targetVersionString = getOriginalVersionString(installedVersion->versionString);
std::vector<const BinaryVersion*> candidates;
for (auto version: *package)
{
if (version != installedVersion)
{
if (getOriginalVersionString(version->versionString).equal(targetVersionString))
{
candidates.push_back(version);
}
}
}
if (!candidates.empty())
{
mpc.resolver->installVersion(candidates, getRequestAnnotation(mpc.mode, packageExpression), mpc.importance);
}
else
{
const auto message = format2(__("the package '%s' cannot be reinstalled because there is no corresponding version (%s) available in repositories"),
packageExpression, targetVersionString.toStdString());
if (mpc.importance == Resolver::RequestImportance::Must)
{
fatal2(message);
}
else
{
warn2(message);
}
}
}
static bool processNumericImportanceOption(ManagePackagesContext& mpc, const string& arg)
{
const char field[] = "--importance=";
const size_t fieldLength = sizeof(field)-1;
if (arg.compare(0, fieldLength, field) == 0)
{
try
{
mpc.importance = boost::lexical_cast< Resolver::RequestImportance::Value >(arg.substr(fieldLength));
}
catch (boost::bad_lexical_cast&)
{
fatal2("option '--importance' requires non-negative numeric value");
}
return true;
}
else
{
return false;
}
}
static bool processPositionalOption(ManagePackagesContext& mpc, const string& arg)
{
if (arg == "--remove") mpc.mode = ManagePackages::Remove;
else if (arg == "--purge") mpc.mode = ManagePackages::Purge;
else if (arg == "--install") mpc.mode = ManagePackages::Install;
else if (arg == "--satisfy") mpc.mode = ManagePackages::Satisfy;
else if (arg == "--unsatisfy") mpc.mode = ManagePackages::Unsatisfy;
else if (arg == "--markauto") mpc.mode = ManagePackages::Markauto;
else if (arg == "--unmarkauto") mpc.mode = ManagePackages::Unmarkauto;
else if (arg == "--iii") mpc.mode = ManagePackages::InstallIfInstalled;
else if (arg == "--reinstall") mpc.mode = ManagePackages::Reinstall;
else if (arg == "--asauto=yes") mpc.autoinstall = ManagePackagesContext::AutoInstall::Yes;
else if (arg == "--asauto=no") mpc.autoinstall = ManagePackagesContext::AutoInstall::No;
else if (arg == "--asauto=default") mpc.autoinstall = ManagePackagesContext::AutoInstall::Nop;
else if (arg == "--select=traditional" || arg == "--st")
mpc.selectType = ManagePackagesContext::SelectType::Traditional;
else if (arg == "--select=flexible" || arg == "--sf")
mpc.selectType = ManagePackagesContext::SelectType::Flexible;
else if (arg == "--importance=wish" || arg == "--wish") mpc.importance = Resolver::RequestImportance::Wish;
else if (arg == "--importance=try" || arg == "--try") mpc.importance = Resolver::RequestImportance::Try;
else if (arg == "--importance=must" || arg == "--must") mpc.importance = Resolver::RequestImportance::Must;
else if (processNumericImportanceOption(mpc, arg)) ;
else
return false;
return true; // if some option was processed
}
static void processPackageExpressions(ManagePackagesContext& mpc, const vector< string >& packageExpressions)
{
for (const auto& packageExpression: packageExpressions)
{
if (processPositionalOption(mpc, packageExpression)) continue;
if (mpc.mode == ManagePackages::Satisfy || mpc.mode == ManagePackages::Unsatisfy)
{
processSatisfyExpression(mpc, packageExpression);
}
else if (mpc.mode == ManagePackages::BuildDepends)
{
processBuildDependsExpression(mpc, packageExpression);
}
else if (mpc.mode == ManagePackages::Markauto || mpc.mode == ManagePackages::Unmarkauto)
{
processAutoFlagChangeExpression(mpc, packageExpression);
}
else if (mpc.mode == ManagePackages::Reinstall)
{
processReinstallExpression(mpc, packageExpression);
}
else
{
processInstallOrRemoveExpression(mpc, packageExpression);
}
}
}
void printUnpackedSizeChanges(const map< string, ssize_t >& unpackedSizesPreview)
{
ssize_t totalUnpackedSizeChange = 0;
for (const auto& previewRecord: unpackedSizesPreview)
{
totalUnpackedSizeChange += previewRecord.second;
}
string message;
if (totalUnpackedSizeChange >= 0)
{
message = format2(__("After unpacking %s will be used."),
humanReadableSizeString(totalUnpackedSizeChange));
} else {
message = format2(__("After unpacking %s will be freed."),
humanReadableSizeString(-totalUnpackedSizeChange));
}
cout << message << endl;
}
void printDownloadSizes(const pair< size_t, size_t >& downloadSizes)
{
auto total = downloadSizes.first;
auto need = downloadSizes.second;
cout << format2(__("Need to get %s/%s of archives. "),
humanReadableSizeString(need), humanReadableSizeString(total));
}
struct VersionInfoFlags
{
bool versionString;
enum class DistributionType { None, Archive, Codename };
DistributionType distributionType;
bool component;
bool vendor;
bool showEmpty;
VersionInfoFlags(const Config& config)
{
versionString = config.getBool("cupt::console::actions-preview::show-versions");
if (config.getBool("cupt::console::actions-preview::show-archives"))
{
distributionType = DistributionType::Archive;
}
else if (config.getBool("cupt::console::actions-preview::show-codenames"))
{
distributionType = DistributionType::Codename;
}
else
{
distributionType = DistributionType::None;
}
component = config.getBool("cupt::console::actions-preview::show-components");
vendor = config.getBool("cupt::console::actions-preview::show-vendors");
showEmpty = config.getBool("cupt::console::actions-preview::show-empty-versions");
}
bool empty() const
{
return !versionString && distributionType == DistributionType::None && !component && !vendor;
}
};
void showVersionInfoIfNeeded(const Cache& cache, const string& packageName,
const Resolver::SuggestedPackage& suggestedPackage, WA::Type actionType,
VersionInfoFlags flags)
{
if (flags.empty())
{
return; // nothing to print
}
auto getVersionString = [&flags](const Version* version) -> string
{
if (!version)
{
return flags.showEmpty ? "<empty>" : "";
}
string result;
if (flags.versionString)
{
result += version->versionString;
}
if ((flags.distributionType != VersionInfoFlags::DistributionType::None)
|| flags.component || flags.vendor)
{
result += '(';
vector< string > chunks;
for (const auto& source: version->sources)
{
string chunk;
if (flags.vendor && !source.release->vendor.empty())
{
chunk += source.release->vendor;
chunk += ':';
}
if (flags.distributionType == VersionInfoFlags::DistributionType::Archive)
{
chunk += source.release->archive;
}
else if (flags.distributionType == VersionInfoFlags::DistributionType::Codename)
{
chunk += source.release->codename;
}
if (flags.component && !source.release->component.empty())
{
chunk += "/";
chunk += source.release->component;
}
if (!chunk.empty() && std::find(chunks.begin(), chunks.end(), chunk) == chunks.end())
{
chunks.push_back(chunk);
}
}
result += join(",", chunks);
result += ')';
}
return result;
};
auto package = cache.getBinaryPackage(packageName);
string oldVersionString = getVersionString(package ? package->getInstalledVersion() : nullptr);
string newVersionString = getVersionString(suggestedPackage.version);
if (!oldVersionString.empty() && !newVersionString.empty() &&
(actionType != fakeNotPreferredVersionAction || oldVersionString != newVersionString))
{
cout << format2(" [%s -> %s]", oldVersionString, newVersionString);
}
else if (!oldVersionString.empty())
{
cout << format2(" [%s]", oldVersionString);
}
else
{
cout << format2(" [%s]", newVersionString);
}
if (actionType == fakeNotPreferredVersionAction)
{
cout << ", " << __("preferred") << ": " << getVersionString(cache.getPreferredVersion(package));
}
}
void showSizeChange(ssize_t bytes)
{
if (bytes != 0)
{
auto sizeChangeString = (bytes >= 0) ?
string("+") + humanReadableSizeString(bytes) :
string("-") + humanReadableSizeString(-bytes);
cout << " <" << sizeChangeString << '>';
}
}
void printByLine(const vector< string >& strings)
{
cout << endl;
for (const auto& s: strings)
{
cout << s << endl;
}
cout << endl;
}
void checkForUntrustedPackages(const Worker::ActionsPreview& actionsPreview,
bool* isDangerous)
{
vector< string > untrustedPackageNames;
// generate loud warning for unsigned versions
const WA::Type affectedActionTypes[] = {
WA::Reinstall, WA::Install, WA::Upgrade, WA::Downgrade
};
for (auto actionType: affectedActionTypes)
{
for (const auto& suggestedRecord: actionsPreview.groups[actionType])
{
if (!(suggestedRecord.second.version->isVerified()))
{
untrustedPackageNames.push_back(suggestedRecord.first);
}
}
}
if (!untrustedPackageNames.empty())
{
*isDangerous = true;
cout << __("WARNING! The untrusted versions of the following packages will be used:") << endl;
printByLine(untrustedPackageNames);
}
}
void checkForRemovalOfPackages(const Cache& cache,
const Worker::ActionsPreview& actionsPreview, bool* isDangerous,
const char* desc, const std::function<bool(const BinaryVersion*)>& predicate)
{
vector<string> affectedPackageNames;
const WA::Type affectedActionTypes[] = { WA::Remove, WA::Purge };
for (auto actionType: affectedActionTypes)
{
for (const auto& suggestedRecord: actionsPreview.groups[actionType])
{
const string& packageName = suggestedRecord.first;
auto package = cache.getBinaryPackage(packageName);
if (package)
{
auto version = package->getInstalledVersion();
if (version) // may return false when purge of config-files package when candidates available
{
if (predicate(version))
{
affectedPackageNames.push_back(packageName);
}
}
}
}
}
if (!affectedPackageNames.empty())
{
*isDangerous = true;
cout << __(desc) << endl;
printByLine(affectedPackageNames);
}
}
void checkForRemovalOfEssentialPackages(const Cache& cache,
const Worker::ActionsPreview& actionsPreview, bool* isDangerous)
{
const char desc[] = "WARNING! The following essential packages will be removed:";
checkForRemovalOfPackages(cache, actionsPreview, isDangerous, desc,
[](const BinaryVersion* v) { return v->essential; });
}
void checkForRemovalOfImportantPackages(const Cache& cache,
const Worker::ActionsPreview& actionsPreview, bool* isDangerous)
{
const char desc[] = "WARNING! The following important packages will be removed:";
checkForRemovalOfPackages(cache, actionsPreview, isDangerous, desc,
[](const BinaryVersion* v) { return v->important; });
}
void checkForIgnoredHolds(const Cache& cache,
const Worker::ActionsPreview& actionsPreview, bool* isDangerous)
{
vector< string > ignoredHoldsPackageNames;
const WA::Type affectedActionTypes[] = { WA::Install, WA::Upgrade,
WA::Downgrade, WA::Remove, WA::Purge };
for (auto actionType: affectedActionTypes)
{
for (const auto& suggestedRecord: actionsPreview.groups[actionType])
{
const string& packageName = suggestedRecord.first;
auto installedInfo = cache.getSystemState()->getInstalledInfo(packageName);
if (installedInfo &&
installedInfo->want == system::State::InstalledRecord::Want::Hold &&
installedInfo->status != system::State::InstalledRecord::Status::ConfigFiles)
{
ignoredHoldsPackageNames.push_back(packageName);
}
}
}
if (!ignoredHoldsPackageNames.empty())
{
*isDangerous = true;
cout << __("WARNING! The following packages on hold will change their state:") << endl;
printByLine(ignoredHoldsPackageNames);
}
}
void checkForMultiArchSystem(const Config& config, bool* isDangerousAction)
{
auto foreignArchitectures = config.getList("cupt::cache::foreign-architectures");
if (!foreignArchitectures.empty())
{
*isDangerousAction = true;
cout << __("WARNING! You are running cupt on MultiArch-enabled system. This setup is not supported at the moment.") << endl;
cout << __("Any actions may break your system.") << endl;
cout << format2(__("Detected foreign architectures: %s"), join(", ", foreignArchitectures)) << endl;
cout << endl;
}
}
void checkAndPrintDangerousActions(const Config& config, const Cache& cache,
const Worker::ActionsPreview& actionsPreview, bool* isDangerousAction)
{
if (!config.getBool("cupt::console::allow-untrusted"))
{
checkForUntrustedPackages(actionsPreview, isDangerousAction);
}
if (config.getBool("cupt::console::warnings::removal-of-essential"))
{
checkForRemovalOfEssentialPackages(cache, actionsPreview, isDangerousAction);
}
if (config.getBool("cupt::console::warnings::removal-of-important"))
{
checkForRemovalOfImportantPackages(cache, actionsPreview, isDangerousAction);
}
checkForIgnoredHolds(cache, actionsPreview, isDangerousAction);
checkForMultiArchSystem(config, isDangerousAction);
if (!actionsPreview.groups[WA::Downgrade].empty())
{
*isDangerousAction = true;
}
}
void showReason(const Resolver::SuggestedPackage& suggestedPackage)
{
for (const auto& reason: suggestedPackage.reasons)
{
cout << " " << __("reason: ") << reason->toString() << endl;
}
if (!suggestedPackage.reasonPackageNames.empty())
{
cout << " " << __("caused by changes in: ") << join(", ", suggestedPackage.reasonPackageNames) << endl;
}
cout << endl;
}
void showUnsatisfiedSoftDependencies(const Resolver::Offer& offer,
bool showDetails, std::stringstream* summaryStreamPtr)
{
vector< string > messages;
for (const auto& unresolvedProblem: offer.unresolvedProblems)
{
messages.push_back(unresolvedProblem->toString());
}
if (!messages.empty())
{
if (showDetails)
{
cout << __("Leave the following dependencies unresolved:") << endl;
printByLine(messages);
}
*summaryStreamPtr << format2(__(" %u dependency problems will stay unresolved"),
offer.unresolvedProblems.size()) << endl;
}
}
void showReasonChainForAskedPackage(const Resolver::SuggestedPackages& suggestedPackages, const Worker::ActionsPreview& actionsPreview)
{
auto isPackageChangingItsState = [&actionsPreview](const string& packageName)
{
for (const auto& group: actionsPreview.groups)
{
if (group.count(packageName)) return true;
}
return false;
};
cout << __("Enter a binary package name to show reason chain for (empty to cancel): ");
string answer;
std::getline(std::cin, answer);
if (answer.empty()) return;
const auto& topPackageName = answer;
if (!isPackageChangingItsState(topPackageName))
{
cout << format2(__("The package '%s' is not going to change its state."), topPackageName) << endl;
return;
}
struct PackageAndLevel
{
string packageName;
size_t level;
};
std::stack< PackageAndLevel > reasonStack({ PackageAndLevel{ topPackageName, 0 } });
cout << endl;
while (!reasonStack.empty())
{
const string& packageName = reasonStack.top().packageName;
if (!isPackageChangingItsState(packageName))
{
reasonStack.pop();
continue;
}
auto reasonIt = suggestedPackages.find(packageName);
const auto& reasons = reasonIt->second.reasons;
if (reasons.empty())
{
fatal2i("no reasons available for the package '%s'", packageName);
}
const auto& reasonPtr = reasons[0];
size_t level = reasonStack.top().level;
cout << format2("%s%s: %s", string(level*2, ' '), packageName, reasonPtr->toString()) << endl;
reasonStack.pop();
for (const string& reasonPackageName: reasonIt->second.reasonPackageNames)
{
reasonStack.push({ reasonPackageName, level + 1 });
}
}
cout << endl;
}
Resolver::UserAnswer::Type askUserAboutSolution(const Config& config,
const Resolver::SuggestedPackages& suggestedPackages,
const Worker::ActionsPreview& actionsPreview,
bool isDangerous, bool& addArgumentsFlag)
{
string answer;
if (config.getBool("cupt::console::assume-yes"))
{
answer = isDangerous ? "q" : "y";
if (isDangerous)
{
cout << __("Didn't confirm dangerous actions.") << endl;
}
}
else
{
ask:
cout << __("Do you want to continue? [y/N/q/a/rc/?] ");
std::getline(std::cin, answer);
if (!std::cin)
{
return Resolver::UserAnswer::Abandon;
}
for (char& c: answer) { c = std::tolower(c); } // lowercasing
}
// deciding
if (answer == "y")
{
if (isDangerous)
{
const string confirmationForDangerousAction = __("Yes, do as I say!");
cout << format2(__("Dangerous actions selected. Type '%s' if you want to continue, or anything else to go back:"),
confirmationForDangerousAction) << endl;
std::getline(std::cin, answer);
if (answer != confirmationForDangerousAction)
{
goto ask;
}
}
return Resolver::UserAnswer::Accept;
}
else if (answer == "q")
{
return Resolver::UserAnswer::Abandon;
}
else if (answer == "a")
{
addArgumentsFlag = true;
return Resolver::UserAnswer::Abandon;
}
else if (answer == "rc")
{
showReasonChainForAskedPackage(suggestedPackages, actionsPreview);
goto ask;
}
else if (answer == "?")
{
cout << __("y: accept the solution") << endl;
cout << __("n: reject the solution, try to find other ones") << endl;
cout << __("q: reject the solution and exit") << endl;
cout << __("a: specify an additional binary package expression") << endl;
cout << __("rc: show a reason chain for a package") << endl;
cout << __("?: output this help") << endl << endl;
goto ask;
}
else
{
// user haven't chosen this solution, try next one
cout << __("Resolving further... ") << endl;
return Resolver::UserAnswer::Decline;
}
}
Resolver::SuggestedPackages generateNotPreferredVersionList(const Cache& cache,
const Resolver::SuggestedPackages& packages)
{
Resolver::SuggestedPackages result;
for (const auto& suggestedPackage: packages)
{
const auto& suggestedVersion = suggestedPackage.second.version;
if (suggestedVersion)
{
auto preferredVersion = cache.getPreferredVersion(
getBinaryPackage(cache, suggestedVersion->packageName));
if (!(preferredVersion == suggestedVersion))
{
result.insert(suggestedPackage);
}
}
}
return result;
}
static string colorizeByActionType(const Colorizer& colorizer,
const string& input, WA::Type actionType, bool isAutoInstalled)
{
Colorizer::Color color = Colorizer::Default;
switch (actionType)
{
case WA::Install: color = Colorizer::Cyan; break;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch"
case WA::Remove: case fakeAutoRemove: color = Colorizer::Yellow; break;
case WA::Upgrade: color = Colorizer::Green; break;
case WA::Purge: case fakeAutoPurge: color = Colorizer::Red; break;
#pragma GCC diagnostic pop
case WA::Downgrade: color = Colorizer::Magenta; break;
case WA::Configure: color = Colorizer::Blue; break;
default: ;
}
return colorizer.colorize(input, color, !isAutoInstalled /* bold */);
}
bool wasOrWillBePackageAutoInstalled(const Resolver::SuggestedPackage& suggestedPackage)
{
return suggestedPackage.automaticallyInstalledFlag;
}
static void printPackageName(const Colorizer& colorizer, const string& packageName,
WA::Type actionType, const Resolver::SuggestedPackage& suggestedPackage)
{
bool isAutoInstalled = wasOrWillBePackageAutoInstalled(suggestedPackage);
cout << colorizeByActionType(colorizer, packageName, actionType, isAutoInstalled);
}
static string colorizeActionName(const Colorizer& colorizer, const string& actionName, WA::Type actionType)
{
if (actionType != WA::Install && actionType != WA::Upgrade &&
actionType != WA::Configure && actionType != WA::ProcessTriggers &&
actionType != fakeAutoRemove && actionType != fakeAutoPurge &&
actionType != fakeBecomeAutomaticallyInstalled &&
actionType != fakeBecomeManuallyInstalled)
{
return colorizer.makeBold(actionName);
}
else
{
return actionName;
}
}
void addActionToSummary(WA::Type actionType, const string& actionName,
const Resolver::SuggestedPackages& suggestedPackages,
Colorizer& colorizer, std::stringstream* summaryStreamPtr)
{
size_t manuallyInstalledCount = std::count_if(suggestedPackages.begin(), suggestedPackages.end(),
[](const pair< string, Resolver::SuggestedPackage >& arg)
{
return !wasOrWillBePackageAutoInstalled(arg.second);
});
size_t autoInstalledCount = suggestedPackages.size() - manuallyInstalledCount;
auto getManualCountString = [manuallyInstalledCount, actionType, &colorizer]()
{
auto s = std::to_string(manuallyInstalledCount);
return colorizeByActionType(colorizer, s, actionType, false);
};
auto getAutoCountString = [autoInstalledCount, actionType, &colorizer]()
{
auto s = std::to_string(autoInstalledCount);
return colorizeByActionType(colorizer, s, actionType, true);
};
*summaryStreamPtr << " ";
if (!manuallyInstalledCount)
{
*summaryStreamPtr << format2(__("%s automatically installed packages %s"),
getAutoCountString(), actionName);
}
else if (!autoInstalledCount)
{
*summaryStreamPtr << format2(__("%s manually installed packages %s"),
getManualCountString(), actionName);
}
else
{
*summaryStreamPtr << format2(__("%s manually installed and %s automatically installed packages %s"),
getManualCountString(), getAutoCountString(), actionName);
}
*summaryStreamPtr << endl;
}
struct PackageIndicators
{
bool manuallyInstalled = false;
bool autoInstalled = false;
PackageIndicators(const Config& config, const Colorizer& colorizer)
{
if (colorizer.enabled())
return;
const string optionPrefix("cupt::console::actions-preview::package-indicators");
manuallyInstalled = config.getBool(optionPrefix + "::manually-installed");
autoInstalled = config.getBool(optionPrefix + "::automatically-installed");
}
};
struct PackageChangeInfoFlags
{
bool sizeChange;
bool reasons;
VersionInfoFlags versionFlags;
PackageIndicators packageIndicators;
PackageChangeInfoFlags(const Config& config, WA::Type actionType, const Colorizer& colorizer)
: versionFlags(config)
, packageIndicators(config, colorizer)
{
sizeChange = (config.getBool("cupt::console::actions-preview::show-size-changes") &&
actionType != fakeNotPreferredVersionAction);
reasons = (config.getBool("cupt::console::actions-preview::show-reasons") &&
actionType != fakeNotPreferredVersionAction);
}
bool packagesTakeSameLine() const
{
return versionFlags.empty() && !sizeChange && !reasons;
}
};
static void printPackageIndicators(const Resolver::SuggestedPackage& suggestedPackage, PackageIndicators indicators)
{
bool isAutoInstalled = wasOrWillBePackageAutoInstalled(suggestedPackage);
if (indicators.autoInstalled && isAutoInstalled)
cout << "{a}";
if (indicators.manuallyInstalled && !isAutoInstalled)
cout << "{m}";
}
void showPackageChanges(const Config& config, const Cache& cache, Colorizer& colorizer, WA::Type actionType,
const Resolver::SuggestedPackages& actionSuggestedPackages,
const map< string, ssize_t >& unpackedSizesPreview)
{
PackageChangeInfoFlags showFlags(config, actionType, colorizer);
for (const auto& it: actionSuggestedPackages)
{
const string& packageName = it.first;
const auto& suggestedPackage = it.second;
printPackageName(colorizer, packageName, actionType, suggestedPackage);
printPackageIndicators(suggestedPackage, showFlags.packageIndicators);
showVersionInfoIfNeeded(cache, packageName, suggestedPackage, actionType, showFlags.versionFlags);
if (showFlags.sizeChange)
showSizeChange(unpackedSizesPreview.find(packageName)->second);
if (!showFlags.packagesTakeSameLine())
cout << endl;
else
cout << ' ';
if (showFlags.reasons)
showReason(suggestedPackage);
}
if (showFlags.packagesTakeSameLine())
cout << endl;
cout << endl;
}
Resolver::SuggestedPackages filterNoLongerNeeded(const Resolver::SuggestedPackages& source, bool invert)
{
Resolver::SuggestedPackages result;
for (const auto& suggestedPackage: source)
{
bool isNoLongerNeeded = false;
for (const auto& reasonPtr: suggestedPackage.second.reasons)
{
if (dynamic_pointer_cast< const Resolver::AutoRemovalReason >(reasonPtr))
{
isNoLongerNeeded = true;
break;
}
}
if (isNoLongerNeeded != invert)
{
result.insert(result.end(), suggestedPackage);
}
}
return result;
}
Resolver::SuggestedPackages filterPureAutoStatusChanges(const Cache& cache,
const Worker::ActionsPreview& actionsPreview, bool targetAutoStatus)
{
static const shared_ptr< Resolver::UserReason > userReason;
Resolver::SuggestedPackages result;
for (const auto& autoStatusChange: actionsPreview.autoFlagChanges)
{
if (autoStatusChange.second == targetAutoStatus)
{
const string& packageName = autoStatusChange.first;
for (size_t ai = 0; ai < WA::Count; ++ai)
{
if (targetAutoStatus && ai != WA::Install) continue;
if (!targetAutoStatus && (ai != WA::Remove && ai != WA::Purge)) continue;
if (actionsPreview.groups[ai].count(packageName))
{
goto next_package; // natural, non-pure change
}
}
{
Resolver::SuggestedPackage& entry = result[packageName];
entry.version = cache.getBinaryPackage(packageName)->getInstalledVersion();
entry.automaticallyInstalledFlag = !targetAutoStatus;
entry.reasons.push_back(userReason);
}
next_package: ;
}
}
return result;
}
Resolver::SuggestedPackages getSuggestedPackagesByAction(const Cache& cache,
const Resolver::Offer& offer,
const Worker::ActionsPreview& actionsPreview, WA::Type actionType)
{
switch (actionType)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch"
case fakeNotPreferredVersionAction:
return generateNotPreferredVersionList(cache, offer.suggestedPackages);
case fakeAutoRemove:
return filterNoLongerNeeded(actionsPreview.groups[WA::Remove], false);
case fakeAutoPurge:
return filterNoLongerNeeded(actionsPreview.groups[WA::Purge], false);
case fakeBecomeAutomaticallyInstalled:
return filterPureAutoStatusChanges(cache, actionsPreview, true);
case fakeBecomeManuallyInstalled:
return filterPureAutoStatusChanges(cache, actionsPreview, false);
#pragma GCC diagnostic pop
case WA::Remove:
return filterNoLongerNeeded(actionsPreview.groups[WA::Remove], true);
case WA::Purge:
return filterNoLongerNeeded(actionsPreview.groups[WA::Purge], true);
default:
return actionsPreview.groups[actionType];
}
}
map< WA::Type, string > getActionDescriptionMap()
{
return map< WA::Type, string > {
{ WA::Install, __("will be installed") },
{ WA::Remove, __("will be removed") },
{ WA::Upgrade, __("will be upgraded") },
{ WA::Purge, __("will be purged") },
{ WA::Downgrade, __("will be downgraded") },
{ WA::Configure, __("will be configured") },
{ WA::Deconfigure, __("will be deconfigured") },
{ WA::ProcessTriggers, __("will have triggers processed") },
{ WA::Reinstall, __("will be reinstalled") },
{ fakeNotPreferredVersionAction, __("will have a not preferred version") },
{ fakeAutoRemove, __("are no longer needed and thus will be auto-removed") },
{ fakeAutoPurge, __("are no longer needed and thus will be auto-purged") },
{ fakeBecomeAutomaticallyInstalled, __("will be marked as automatically installed") },
{ fakeBecomeManuallyInstalled, __("will be marked as manually installed") },
};
}
vector< WA::Type > getActionTypesInPrintOrder(bool showNotPreferred)
{
vector< WA::Type > result = {
fakeBecomeAutomaticallyInstalled, fakeBecomeManuallyInstalled,
WA::Reinstall, WA::Install, WA::Upgrade, WA::Remove,
WA::Purge, WA::Downgrade, WA::Configure, WA::ProcessTriggers, WA::Deconfigure
};
if (showNotPreferred)
{
result.push_back(fakeNotPreferredVersionAction);
}
result.push_back(fakeAutoRemove);
result.push_back(fakeAutoPurge);
return result;
}
bool isSummaryEnabled(const Config& config, size_t actionCount)
{
auto configValue = config.getString("cupt::console::actions-preview::show-summary");
return (configValue == "yes") ||
(actionCount > 100 && configValue == "auto");
}
Resolver::CallbackType generateManagementPrompt(ManagePackagesContext& mpc,
bool showNotPreferred, bool& addArgumentsFlag, bool& thereIsNothingToDo)
{
auto result = [&mpc, showNotPreferred, &addArgumentsFlag, &thereIsNothingToDo]
(const Resolver::Offer& offer) -> Resolver::UserAnswer::Type
{
addArgumentsFlag = false;
thereIsNothingToDo = false;
auto showDetails = mpc.config->getBool("cupt::console::actions-preview::show-details");
mpc.worker->setDesiredState(offer);
auto actionsPreview = mpc.worker->getActionsPreview();
auto unpackedSizesPreview = mpc.worker->getUnpackedSizesPreview();
Colorizer colorizer(*mpc.config);
std::stringstream summaryStream;
size_t actionCount = 0;
{ // print planned actions
const auto actionDescriptions = getActionDescriptionMap();
cout << endl;
for (const WA::Type& actionType: getActionTypesInPrintOrder(showNotPreferred))
{
auto actionSuggestedPackages = getSuggestedPackagesByAction(*mpc.cache,
offer, *actionsPreview, actionType);
if (actionSuggestedPackages.empty()) continue;
if (actionType != fakeNotPreferredVersionAction)
{
actionCount += actionSuggestedPackages.size();
}
const string& actionName = actionDescriptions.find(actionType)->second;
addActionToSummary(actionType, actionName, actionSuggestedPackages,
colorizer, &summaryStream);
if (!showDetails) continue;
cout << format2(__("The following packages %s:"),
colorizeActionName(colorizer, actionName, actionType)) << endl << endl;
showPackageChanges(*mpc.config, *mpc.cache, colorizer, actionType,
actionSuggestedPackages, unpackedSizesPreview);
}
showUnsatisfiedSoftDependencies(offer, showDetails, &summaryStream);
};
// nothing to do maybe?
if (actionCount == 0)
{
thereIsNothingToDo = true;
return Resolver::UserAnswer::Abandon;
}
if (isSummaryEnabled(*mpc.config, actionCount))
{
cout << __("Action summary:") << endl << summaryStream.str() << endl;
summaryStream.clear();
}
bool isDangerousAction = false;
checkAndPrintDangerousActions(*mpc.config, *mpc.cache, *actionsPreview, &isDangerousAction);
{ // print size estimations
auto downloadSizesPreview = mpc.worker->getDownloadSizesPreview();
printDownloadSizes(downloadSizesPreview);
printUnpackedSizeChanges(unpackedSizesPreview);
}
return askUserAboutSolution(*mpc.config, offer.suggestedPackages, *actionsPreview, isDangerousAction, addArgumentsFlag);
};
return result;
}
void parseManagementOptions(Context& context, ManagePackages::Mode mode,
vector< string >& packageExpressions, bool& showNotPreferred)
{
bpo::options_description options;
options.add_options()
("no-install-recommends,R", "")
("no-remove", "")
("no-auto-remove", "")
("max-solution-count", bpo::value< string >())
("resolver", bpo::value< string >())
("show-versions,V", "")
("show-size-changes,Z", "")
("show-reasons,D", "")
("show-archives,A", "")
("show-codenames,N", "")
("show-components,C", "")
("show-deps", "")
("show-not-preferred", "")
("show-vendors,O", "")
("download-only,d", "")
("summary-only", "")
("no-summary", "")
("assume-yes", "")
("yes,y", "");
// use action modifiers as arguments, not options
auto extraParser = [](const string& input) -> pair< string, string >
{
ManagePackagesContext dummyMpc = {
ManagePackages::Mode::Install,
ManagePackagesContext::AutoInstall::Nop,
ManagePackagesContext::SelectType::Traditional,
0, nullptr, nullptr, nullptr, nullptr
};
if (processPositionalOption(dummyMpc, input))
{
return make_pair("arguments", input);
}
else
{
return make_pair(string(), string());
}
};
auto variables = parseOptions(context, options, packageExpressions, extraParser);
auto config = context.getConfig();
if (variables.count("max-solution-count"))
{
config->setScalar("cupt::resolver::max-solution-count",
variables["max-solution-count"].as<string>());
}
if (variables.count("resolver"))
{
config->setScalar("cupt::resolver::type", variables["resolver"].as<string>());
}
if (variables.count("assume-yes") || variables.count("yes"))
{
config->setScalar("cupt::console::assume-yes", "yes");
}
if (variables.count("show-reasons") || variables.count("show-deps"))
{
config->setScalar("cupt::console::actions-preview::show-reasons", "yes");
}
// we now always need reason tracking
config->setScalar("cupt::resolver::track-reasons", "yes");
if (variables.count("no-install-recommends"))
{
config->setScalar("apt::install-recommends", "no");
}
if (variables.count("no-remove"))
{
config->setScalar("cupt::resolver::no-remove", "yes");
}
if (variables.count("download-only"))
{
config->setScalar("cupt::worker::download-only", "yes");
}
if (variables.count("no-auto-remove"))
{
config->setScalar("cupt::resolver::auto-remove", "no");
}
if (variables.count("summary-only"))
{
config->setScalar("cupt::console::actions-preview::show-summary", "yes");
config->setScalar("cupt::console::actions-preview::show-details", "no");
}
if (variables.count("no-summary"))
{
config->setScalar("cupt::console::actions-preview::show-summary", "no");
config->setScalar("cupt::console::actions-preview::show-details", "yes");
}
if (variables.count("show-versions"))
{
config->setScalar("cupt::console::actions-preview::show-versions", "yes");
}
if (variables.count("show-size-changes"))
{
config->setScalar("cupt::console::actions-preview::show-size-changes", "yes");
}
if (variables.count("show-archives"))
{
config->setScalar("cupt::console::actions-preview::show-archives", "yes");
}
if (variables.count("show-codenames"))
{
config->setScalar("cupt::console::actions-preview::show-codenames", "yes");
}
if (config->getBool("cupt::console::actions-preview::show-archives") &&
config->getBool("cupt::console::actions-preview::show-codenames"))
{
fatal2(__("options 'cupt::console::actions-preview::show-archives' and 'cupt::console::actions-preview::show-codenames' cannot be used together"));
}
if (variables.count("show-components"))
{
config->setScalar("cupt::console::actions-preview::show-components", "yes");
}
if (variables.count("show-vendors"))
{
config->setScalar("cupt::console::actions-preview::show-vendors", "yes");
}
string showNotPreferredConfigValue = config->getString("cupt::console::actions-preview::show-not-preferred");
showNotPreferred = variables.count("show-not-preferred") ||
showNotPreferredConfigValue == "yes" ||
((mode == ManagePackages::FullUpgrade || mode == ManagePackages::SafeUpgrade) &&
showNotPreferredConfigValue == "for-upgrades");
if (mode == ManagePackages::SelfUpgrade)
{
packageExpressions = {"dpkg", "cupt"};
}
}
Resolver* getResolver(const shared_ptr< const Config >& config,
const shared_ptr< const Cache >& cache)
{
if (!config->getString("cupt::resolver::external-command").empty())
{
fatal2(__("using an external resolver is not supported now"));
}
return new NativeResolver(config, cache);
}
void queryAndProcessAdditionalPackageExpressions(ManagePackagesContext& mpc)
{
string answer;
do
{
cout << __("Enter package expression(s) (empty to finish): ");
std::getline(std::cin, answer);
if (!answer.empty())
{
processPackageExpressions(mpc, convertLineToShellArguments(answer));
}
else
{
break;
}
} while (true);
}
class ProgressStage
{
bool p_print;
public:
ProgressStage(const Config& config)
: p_print(config.getBool("cupt::console::show-progress-messages"))
{}
void operator()(const char* message)
{
if (p_print)
{
cout << message << endl;
}
}
};
int managePackages(Context& context, ManagePackages::Mode mode)
{
auto config = context.getConfig();
ProgressStage stage(*config);
// turn off info parsing, we don't need it
if (!shellMode)
{
Version::parseInfoOnly = false;
}
Cache::memoize = true;
vector< string > packageExpressions;
bool showNotPreferred;
parseManagementOptions(context, mode, packageExpressions, showNotPreferred);
unrollFileArguments(packageExpressions);
// snapshot handling
string snapshotName; // empty if not applicable
if (mode == ManagePackages::LoadSnapshot)
{
if (packageExpressions.size() != 1)
{
fatal2(__("exactly one argument (the snapshot name) should be specified"));
}
snapshotName = packageExpressions[0];
packageExpressions.clear();
{
Snapshots snapshots(config);
snapshots.setupConfigForSnapshotOnly(snapshotName);
}
mode = ManagePackages::Install;
}
shared_ptr< const Cache > cache;
{
// source packages are needed for for synchronizing source versions
bool buildSource = (mode == ManagePackages::BuildDepends ||
config->getString("cupt::resolver::synchronize-by-source-versions") != "none");
stage(__("Building the package cache... "));
cache = context.getCache(buildSource, true, true);
}
stage(__("Initializing package resolver and worker... "));
std::unique_ptr< Resolver > resolver(getResolver(config, cache));
if (!snapshotName.empty())
{
Snapshots snapshots(config);
snapshots.setupResolverForSnapshotOnly(snapshotName, *cache, *resolver);
}
shared_ptr< Worker > worker(new Worker(config, cache));
ManagePackagesContext mpc = { mode,
ManagePackagesContext::AutoInstall::Nop, ManagePackagesContext::SelectType::Traditional, Resolver::RequestImportance::Must,
config.get(), cache.get(), resolver.get(), worker.get() };
stage(__("Scheduling requested actions... "));
preProcessMode(mpc);
processPackageExpressions(mpc, packageExpressions);
stage(__("Resolving possible unmet dependencies... "));
bool addArgumentsFlag, thereIsNothingToDo;
auto callback = generateManagementPrompt(mpc,
showNotPreferred, addArgumentsFlag, thereIsNothingToDo);
resolve:
addArgumentsFlag = false;
thereIsNothingToDo = false;
bool resolved = resolver->resolve(callback);
if (addArgumentsFlag && std::cin)
{
queryAndProcessAdditionalPackageExpressions(mpc);
goto resolve;
}
// at this stage resolver has done its work, so to does not consume the RAM
resolver.reset();
if (thereIsNothingToDo)
{
cout << __("Nothing to do.") << endl;
return 0;
}
else if (resolved)
{
// if some solution was found and user has accepted it
auto downloadProgress = getDownloadProgress(*config);
cout << __("Performing requested actions:") << endl;
context.invalidate();
try
{
worker->changeSystem(downloadProgress);
}
catch (Exception&)
{
fatal2(__("unable to do requested actions"));
}
return 0;
}
else
{
cout << __("Abandoned or no more solutions.") << endl;
return 1;
}
}
int distUpgrade(Context& context)
{
if (shellMode)
{
fatal2(__("'dist-upgrade' command cannot be run in the shell mode"));
}
{ // 1st stage: upgrading of package management tools
cout << __("[ upgrading package management tools ]") << endl;
cout << endl;
if (managePackages(context, ManagePackages::SelfUpgrade) != 0)
{
fatal2(__("upgrading of the package management tools failed"));
}
}
{ // 2nd stage: full upgrade
cout << endl;
cout << __("[ upgrading the system ]");
cout << endl;
auto argc = context.argc;
auto argv = context.argv;
for (int i = 0; i < argc; ++i)
{
if (!strcmp(argv[i], "dist-upgrade"))
{
strcpy(argv[i], "full-upgrade");
}
}
execvp(argv[0], argv);
fatal2e(__("%s() failed"), "execvp");
}
return 0; // unreachable due to exec
}
int updateReleaseAndIndexData(Context& context)
{
bpo::options_description noOptions;
vector< string > arguments;
auto variables = parseOptions(context, noOptions, arguments);
checkNoExtraArguments(arguments);
auto config = context.getConfig();
auto cache = context.getCache(false, false, false);
auto downloadProgress = getDownloadProgress(*config);
Worker worker(config, cache);
context.invalidate();
// may throw exception
worker.updateReleaseAndIndexData(downloadProgress);
return 0;
}
int cleanArchives(Context& context, bool leaveAvailable)
{
if (!shellMode)
{
Version::parseInfoOnly = false;
Version::parseRelations = false;
}
bpo::options_description noOptions;
vector< string > arguments;
auto variables = parseOptions(context, noOptions, arguments);
checkNoExtraArguments(arguments);
auto config = context.getConfig();
auto cache = context.getCache(false, leaveAvailable, leaveAvailable);
Worker worker(config, cache);
uint64_t totalDeletedBytes = 0;
auto info = worker.getArchivesInfo();
for (const auto& infoRecord: info)
{
if (leaveAvailable && infoRecord.second /* version is not empty */)
{
continue; // skip this one
}
const string& path = infoRecord.first;
struct stat stat_structure;
if (lstat(path.c_str(), &stat_structure))
{
fatal2e(__("%s() failed: '%s'"), "lstat", path);
}
else
{
size_t size = stat_structure.st_size;
totalDeletedBytes += size;
cout << format2(__("Deleting '%s' <%s>..."), path, humanReadableSizeString(size)) << endl;
worker.deleteArchive(path);
}
}
cout << format2(__("Freed %s of disk space."), humanReadableSizeString(totalDeletedBytes)) << endl;
cout << __("Deleting partial archives...") << endl;
worker.deletePartialArchives();
return 0;
}
|