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
|
// Include files /*{{{*/
#include <config.h>
#include <apt-pkg/aptconfiguration.h>
#include <apt-pkg/cachefile.h>
#include <apt-pkg/configuration.h>
#include <apt-pkg/depcache.h>
#include <apt-pkg/error.h>
#include <apt-pkg/fileutl.h>
#include <apt-pkg/pkgcache.h>
#include <apt-pkg/pkgrecords.h>
#include <apt-pkg/policy.h>
#include <apt-pkg/strutl.h>
#include <apt-private/private-cachefile.h>
#include <apt-private/private-output.h>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <iomanip>
#include <iostream>
#include <langinfo.h>
#include <regex.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <termios.h>
#include <unistd.h>
#include <sstream>
#include <apti18n.h>
/*}}}*/
using namespace std;
using APT::Configuration::color;
std::ostream c0out(0);
std::ostream c1out(0);
std::ostream c2out(0);
std::ofstream devnull("/dev/null");
unsigned int ScreenWidth = 80 - 1; /* - 1 for the cursor */
static pid_t Pager = -1;
static constexpr std::array<int, 5> PagerSignalsToHandle = {
SIGINT,
SIGHUP,
SIGTERM,
SIGQUIT,
SIGPIPE,
};
// SigWinch - Window size change signal handler /*{{{*/
// ---------------------------------------------------------------------
/* */
static void SigWinch(int)
{
// Riped from GNU ls
#ifdef TIOCGWINSZ
struct winsize ws;
if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col >= 5)
ScreenWidth = ws.ws_col - 1;
#endif
}
/*}}}*/
bool IsStdoutAtty()
{
static bool is = isatty(STDOUT_FILENO);
return is;
}
static void WaitPager()
{
if (Pager != -1)
{
c0out.flush();
c1out.flush();
c2out.flush();
std::cerr.flush();
std::cout.flush();
fflush(stdout);
fflush(stderr);
close(STDOUT_FILENO);
close(STDERR_FILENO);
waitpid(Pager, nullptr, 0);
Pager = -1;
}
}
static void SignalPager(int signo)
{
if (Pager != -1)
{
// We can't flush from inside the signal handler.
close(STDOUT_FILENO);
close(STDERR_FILENO);
waitpid(Pager, nullptr, 0);
Pager = -1;
for (auto signalToHandle : PagerSignalsToHandle)
signal(signalToHandle, SIG_DFL);
raise(signo);
}
}
static std::string DeterminePager()
{
if (not IsStdoutAtty() || not _config->FindB("Pager", false))
return "";
if (auto pager = getenv("APT_PAGER"); pager)
return pager;
if (auto pager = getenv("PAGER"); pager)
return pager;
return DEFAULT_PAGER;
}
bool InitOutputPager()
{
// InitOutputPager() behaves like a singleton, do not run twice-
static bool alreadyRan = false;
if (alreadyRan)
return true;
alreadyRan = true;
auto pager = DeterminePager();
if (pager.empty() || pager == "cat")
return true;
auto pagerEnv = VectorizeString(PAGER_ENV, '\n');
int pipefds[2] = {-1, -1};
int notifyPipe[2] = {-1, -1};
DEFER([&] () {
close(pipefds[0]);
close(pipefds[1]);
close(notifyPipe[0]);
close(notifyPipe[1]);
});
if (pipe(pipefds) || pipe(notifyPipe))
return _error->Errno("pipe", "Failed to setup pipe");
Pager = ExecFork();
if (Pager < 0)
return _error->Errno("fork", "Failed to fork");
if (Pager == 0)
{
// Child process
if (dup2(pipefds[0], STDIN_FILENO) == -1)
goto err;
if (dup2(STDOUT_FILENO, STDERR_FILENO) == -1)
goto err;
for (int sig = 1; sig < NSIG; sig++)
signal(sig, SIG_DFL);
for (auto &v: pagerEnv)
{
// WARNING: VectorizeString() is not thread-safe, do not call after creating a thread.
auto keyAndValue = VectorizeString(v, '=');
if (keyAndValue.size() != 2)
{
_error->Fatal("Invalid environment string: %s", v.c_str());
goto err;
}
// NOTE: Must not override user values, so setenv(..., override=0) is used rather than putenv()
setenv(keyAndValue[0].c_str(), keyAndValue[1].c_str(), 0);
}
{
// If our pager name contains a space we need to invoke it in a shell. Boooo!
char *cmd[] = {(char*)"/bin/sh", (char*)"-c", pager.data(), nullptr};
constexpr std::string_view allowed_chars{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" "+-._=/"};
static_assert(allowed_chars.size() == 26 + 26 + 10 + 6);
if (pager.find_first_not_of(allowed_chars) == pager.npos)
{
cmd[0] = pager.data();
cmd[1] = nullptr;
}
execvp(cmd[0], cmd);
}
err:
int err = errno;
FileFd::Write(notifyPipe[1], &err, sizeof(err));
_exit(128);
}
// Parent process.
// Figure out if we were able to execvp() the child successfully. To do so,
// read from the notifyPipe. If execvp() was successful, it was closed due
// to CLOEXEC. On failure, our child code writes an errno to it.
_error->PushToStack();
close(notifyPipe[1]); // Need to close our write end so our read doesn't block
notifyPipe[1] = -1;
if (int err = 0; FileFd::Read(notifyPipe[0], &err, sizeof(err))) {
errno = err;
_error->WarningE("PagerSetup", "Could not execute pager");
_error->MergeWithStack();
return true;
}
_error->RevertToStack();
// When running a pager, we must not be reading from stdin/tty, so
// let's be safe and open /dev/null in its place.
if (int const nullfd = open("/dev/null", O_RDONLY); nullfd != -1)
{
dup2(nullfd, STDIN_FILENO);
close(nullfd);
}
// Redirect the output(s) to the pager
if (dup2(pipefds[1], STDOUT_FILENO) == -1)
abort();
if (isatty(STDERR_FILENO) && dup2(pipefds[1], STDERR_FILENO) == -1)
abort();
// From now on, we can't show any progress messages as we are outputting
// to the pager.
_config->CndSet("quiet::NoUpdate", "1");
// Setup signal handlers and exit handlers for the pager, such that
// we wait for it.
for (auto signalToHandle : PagerSignalsToHandle)
signal(signalToHandle, SignalPager);
atexit(WaitPager);
return true;
}
bool InitOutput(std::basic_streambuf<char> * const out) /*{{{*/
{
if (not IsStdoutAtty() && _config->FindI("quiet", -1) == -1)
_config->Set("quiet","1");
c0out.rdbuf(out);
c1out.rdbuf(out);
c2out.rdbuf(out);
if (_config->FindI("quiet",0) > 0)
c0out.rdbuf(devnull.rdbuf());
if (_config->FindI("quiet",0) > 1)
c1out.rdbuf(devnull.rdbuf());
// deal with window size changes
auto cols = getenv("COLUMNS");
if (cols != nullptr)
{
char * colends;
auto const sw = strtoul(cols, &colends, 10);
if (*colends != '\0' || sw == 0)
{
_error->Warning("Environment variable COLUMNS was ignored as it has an invalid value: \"%s\"", cols);
cols = nullptr;
}
else
ScreenWidth = sw;
}
if (cols == nullptr)
{
signal(SIGWINCH,SigWinch);
SigWinch(0);
}
if (not IsStdoutAtty() || not _config->FindB("APT::Color", true) || getenv("NO_COLOR") != nullptr || getenv("APT_NO_COLOR") != nullptr)
{
_config->Set("APT::Color", false);
_config->Set("APT::Color::Highlight", "");
_config->Set("APT::Color::Neutral", "");
} else {
// Colors
_config->CndSet("APT::Color::Highlight", "\x1B[32m");
_config->CndSet("APT::Color::Bold", "\x1B[1m");
_config->CndSet("APT::Color::Neutral", "\x1B[0m");
_config->CndSet("APT::Color::Red", "\x1B[31m");
_config->CndSet("APT::Color::Green", "\x1B[32m");
_config->CndSet("APT::Color::Yellow", "\x1B[33m");
_config->CndSet("APT::Color::Blue", "\x1B[34m");
_config->CndSet("APT::Color::Magenta", "\x1B[35m");
_config->CndSet("APT::Color::Cyan", "\x1B[36m");
_config->CndSet("APT::Color::White", "\x1B[37m");
_config->CndSet("APT::Color::Action::Upgrade", "green");
_config->CndSet("APT::Color::Action::Install", "green");
_config->CndSet("APT::Color::Action::Install-Dependencies", "green");
_config->CndSet("APT::Color::Action::Downgrade", "yellow");
_config->CndSet("APT::Color::Action::Remove", "red");
_config->CndSet("APT::Color::Show::Field", "\x1B[1m");
_config->CndSet("APT::Color::Show::Package", "\x1B[32m");
}
return true;
}
/*}}}*/
static std::string GetArchiveSuite(pkgCacheFile &/*CacheFile*/, pkgCache::VerIterator ver) /*{{{*/
{
std::string suite = "";
if (ver && ver.FileList())
{
pkgCache::VerFileIterator VF = ver.FileList();
for (; VF.end() == false ; ++VF)
{
if(VF.File() == NULL || VF.File().Archive() == NULL)
suite = suite + "," + _("unknown");
else
suite = suite + "," + VF.File().Archive();
//suite = VF.File().Archive();
}
suite = suite.erase(0, 1);
}
return suite;
}
/*}}}*/
static std::string GetFlagsStr(pkgCacheFile &CacheFile, pkgCache::PkgIterator P)/*{{{*/
{
pkgDepCache *DepCache = CacheFile.GetDepCache();
pkgDepCache::StateCache &state = (*DepCache)[P];
std::string flags_str;
if (state.NowBroken())
flags_str = "B";
if (P.CurrentVer() && state.Upgradable() && state.CandidateVer != NULL)
flags_str = "g";
else if (P.CurrentVer() != NULL)
flags_str = "i";
else
flags_str = "-";
return flags_str;
}
/*}}}*/
static std::string GetCandidateVersion(pkgCacheFile &CacheFile, pkgCache::PkgIterator P)/*{{{*/
{
pkgPolicy *policy = CacheFile.GetPolicy();
pkgCache::VerIterator cand = policy->GetCandidateVer(P);
return cand ? cand.VerStr() : "(none)";
}
/*}}}*/
static std::string GetInstalledVersion(pkgCacheFile &/*CacheFile*/, pkgCache::PkgIterator P)/*{{{*/
{
pkgCache::VerIterator inst = P.CurrentVer();
return inst ? inst.VerStr() : "(none)";
}
/*}}}*/
static std::string GetVersion(pkgCacheFile &/*CacheFile*/, pkgCache::VerIterator V)/*{{{*/
{
pkgCache::PkgIterator P = V.ParentPkg();
if (V == P.CurrentVer())
{
std::string inst_str = DeNull(V.VerStr());
#if 0 // FIXME: do we want this or something like this?
pkgDepCache *DepCache = CacheFile.GetDepCache();
pkgDepCache::StateCache &state = (*DepCache)[P];
if (state.Upgradable())
return "**"+inst_str;
#endif
return inst_str;
}
if(V)
return DeNull(V.VerStr());
return "(none)";
}
/*}}}*/
static std::string GetArchitecture(pkgCacheFile &CacheFile, pkgCache::PkgIterator P)/*{{{*/
{
if (P->CurrentVer == 0)
{
pkgDepCache * const DepCache = CacheFile.GetDepCache();
pkgDepCache::StateCache const &state = (*DepCache)[P];
if (state.CandidateVer != NULL)
{
pkgCache::VerIterator const CandV(CacheFile, state.CandidateVer);
return CandV.Arch();
}
else
{
pkgCache::VerIterator const V = P.VersionList();
if (V.end() == false)
return V.Arch();
else
return P.Arch();
}
}
else
return P.CurrentVer().Arch();
}
/*}}}*/
static std::string GetShortDescription(pkgCacheFile &CacheFile, pkgRecords &records, pkgCache::PkgIterator P)/*{{{*/
{
pkgPolicy *policy = CacheFile.GetPolicy();
pkgCache::VerIterator ver;
if (P.CurrentVer())
ver = P.CurrentVer();
else
ver = policy->GetCandidateVer(P);
std::string ShortDescription = "(none)";
if(ver)
{
pkgCache::DescIterator const Desc = ver.TranslatedDescription();
if (Desc.end() == false)
{
pkgRecords::Parser & parser = records.Lookup(Desc.FileList());
ShortDescription = parser.ShortDesc();
}
}
return ShortDescription;
}
/*}}}*/
static std::string GetLongDescription(pkgCacheFile &CacheFile, pkgRecords &records, pkgCache::PkgIterator P)/*{{{*/
{
pkgPolicy *policy = CacheFile.GetPolicy();
pkgCache::VerIterator ver;
if (P->CurrentVer != 0)
ver = P.CurrentVer();
else
ver = policy->GetCandidateVer(P);
std::string const EmptyDescription = "(none)";
if(ver.end() == true)
return EmptyDescription;
pkgCache::DescIterator const Desc = ver.TranslatedDescription();
if (Desc.end() == false)
{
pkgRecords::Parser & parser = records.Lookup(Desc.FileList());
std::string const longdesc = parser.LongDesc();
if (longdesc.empty() == false)
return SubstVar(longdesc, "\n ", "\n ");
}
return EmptyDescription;
}
/*}}}*/
void ListSingleVersion(pkgCacheFile &CacheFile, pkgRecords &records, /*{{{*/
pkgCache::VerIterator const &V, std::ostream &out,
std::string const &format)
{
pkgCache::PkgIterator const P = V.ParentPkg();
pkgDepCache * const DepCache = CacheFile.GetDepCache();
pkgDepCache::StateCache const &state = (*DepCache)[P];
std::string output;
if (_config->FindB("APT::Cmd::use-format", false))
output = _config->Find("APT::Cmd::format", "${db::Status-Abbrev} ${Package} ${Version} ${Origin} ${Description}");
else
output = format;
// FIXME: some of these names are really icky – and all is nowhere documented
output = SubstVar(output, "${db::Status-Abbrev}", GetFlagsStr(CacheFile, P));
output = SubstVar(output, "${Package}", P.Name());
std::string const ArchStr = GetArchitecture(CacheFile, P);
output = SubstVar(output, "${Architecture}", ArchStr);
std::string const InstalledVerStr = GetInstalledVersion(CacheFile, P);
output = SubstVar(output, "${installed:Version}", InstalledVerStr);
std::string const CandidateVerStr = GetCandidateVersion(CacheFile, P);
output = SubstVar(output, "${candidate:Version}", CandidateVerStr);
std::string const VersionStr = GetVersion(CacheFile, V);
output = SubstVar(output, "${Version}", VersionStr);
output = SubstVar(output, "${Origin}", GetArchiveSuite(CacheFile, V));
std::string StatusStr = "";
if (P->CurrentVer != 0)
{
if (P.CurrentVer() == V)
{
if (state.Upgradable() && state.CandidateVer != NULL)
strprintf(StatusStr, _("[installed,upgradable to: %s]"),
CandidateVerStr.c_str());
else if (V.Downloadable() == false)
StatusStr = _("[installed,local]");
else if(V.Automatic() == true && state.Garbage == true)
StatusStr = _("[installed,auto-removable]");
else if ((state.Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto)
StatusStr = _("[installed,automatic]");
else
StatusStr = _("[installed]");
}
else if (state.CandidateVer == V && state.Upgradable())
strprintf(StatusStr, _("[upgradable from: %s]"),
InstalledVerStr.c_str());
}
else if (V.ParentPkg()->CurrentState == pkgCache::State::ConfigFiles)
StatusStr = _("[residual-config]");
output = SubstVar(output, "${apt:Status}", StatusStr);
output = SubstVar(output, "${color:highlight}", color("Highlight"));
output = SubstVar(output, "${color:neutral}", color("Neutral"));
output = SubstVar(output, "${Description}", GetShortDescription(CacheFile, records, P));
if (output.find("${LongDescription}") != string::npos)
output = SubstVar(output, "${LongDescription}", GetLongDescription(CacheFile, records, P));
output = SubstVar(output, "${ }${ }", "${ }");
output = SubstVar(output, "${ }\n", "\n");
output = SubstVar(output, "${ }", " ");
if (APT::String::Endswith(output, " ") == true)
output.erase(output.length() - 1);
out << output;
}
/*}}}*/
// ShowWithColumns - Show a list in the style of ls /*{{{*/
// ---------------------------------------------------------------------
/* This prints out a vector of strings with the given indent and in as
many columns as will fit the screen width.
The output looks like:
abiword debootstrap gir1.2-upowerglib-1.0
abiword-common dh-make google-chrome-beta
abiword-plugin-grammar dmeventd gstreamer1.0-clutter-3.0
binfmt-support dmsetup hostname
console-setup evolution-data-server iproute2
console-setup-linux evolution-data-server-common
coreutils ffmpeg
*/
struct columnInfo
{
bool ValidLen;
size_t LineWidth;
vector<size_t> RemainingWidths;
};
void ShowWithColumns(ostream &out, vector<string> const &List, size_t Indent, size_t ScreenWidth)
{
constexpr size_t MinColumnWidth = 2;
constexpr size_t ColumnSpace = 2;
size_t const ListSize = List.size();
size_t const MaxScreenCols = (ScreenWidth - Indent) /
MinColumnWidth;
size_t const MaxNumCols = min(MaxScreenCols, ListSize);
vector<columnInfo> ColumnInfo(MaxNumCols);
for (size_t I = 0; I < MaxNumCols; ++I) {
ColumnInfo[I].ValidLen = true;
ColumnInfo[I].LineWidth = (I + 1) * MinColumnWidth;
ColumnInfo[I].RemainingWidths.resize(I + 1, MinColumnWidth);
}
for (size_t I = 0; I < ListSize; ++I) {
for (size_t J = 0; J < MaxNumCols; ++J) {
auto& Col = ColumnInfo[J];
if (!Col.ValidLen)
continue;
size_t Idx = I / ((ListSize + J) / (J + 1));
size_t RealColLen = List[I].size() + (Idx == J ? 0 : ColumnSpace);
if (Col.RemainingWidths[Idx] < RealColLen) {
Col.LineWidth += RealColLen - Col.RemainingWidths[Idx];
Col.RemainingWidths[Idx] = RealColLen;
Col.ValidLen = Col.LineWidth < ScreenWidth;
}
}
}
size_t NumCols = MaxNumCols;
while (NumCols > 1 && !ColumnInfo[NumCols - 1].ValidLen)
--NumCols;
size_t NumRows = ListSize / NumCols + (ListSize % NumCols != 0);
auto const &LineFormat = ColumnInfo[NumCols - 1];
for (size_t Row = 0; Row < NumRows; ++Row) {
size_t Col = 0;
size_t I = Row;
out << string(Indent, ' ');
while (true) {
out << List[I];
size_t CurLen = List[I].size();
size_t MaxLen = LineFormat.RemainingWidths[Col++];
I += NumRows;
if (I >= ListSize)
break;
out << string(MaxLen - CurLen, ' ');
}
out << endl;
}
}
/*}}}*/
// ShowBroken - Debugging aide /*{{{*/
// ---------------------------------------------------------------------
/* This prints out the names of all the packages that are broken along
with the name of each broken dependency and a quite version
description.
The output looks like:
The following packages have unmet dependencies:
exim: Depends: libc6 (>= 2.1.94) but 2.1.3-10 is to be installed
Depends: libldap2 (>= 2.0.2-2) but it is not going to be installed
Depends: libsasl7 but it is not going to be installed
*/
static void ShowBrokenPackage(ostream &out, pkgCacheFile * const Cache, pkgCache::PkgIterator const &Pkg, bool const Now)
{
if (Now == true)
{
if ((*Cache)[Pkg].NowBroken() == false)
return;
}
else
{
if ((*Cache)[Pkg].InstBroken() == false)
return;
}
// Print out each package and the failed dependencies
out << " " << Pkg.FullName(true) << " :";
unsigned const Indent = Pkg.FullName(true).size() + 3;
bool First = true;
pkgCache::VerIterator Ver;
if (Now == true)
Ver = Pkg.CurrentVer();
else
Ver = (*Cache)[Pkg].InstVerIter(*Cache);
if (Ver.end() == true)
{
out << endl;
return;
}
for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false;)
{
// Compute a single dependency element (glob or)
pkgCache::DepIterator Start;
pkgCache::DepIterator End;
D.GlobOr(Start,End); // advances D
if ((*Cache)->IsImportantDep(End) == false)
continue;
if (Now == true)
{
if (((*Cache)[End] & pkgDepCache::DepGNow) == pkgDepCache::DepGNow)
continue;
}
else
{
if (((*Cache)[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall)
continue;
}
bool FirstOr = true;
while (1)
{
if (First == false)
for (unsigned J = 0; J != Indent; J++)
out << ' ';
First = false;
if (FirstOr == false)
{
for (unsigned J = 0; J != strlen(End.DepType()) + 3; J++)
out << ' ';
}
else
out << ' ' << End.DepType() << ": ";
FirstOr = false;
out << Start.TargetPkg().FullName(true);
// Show a quick summary of the version requirements
if (Start.TargetVer() != 0)
out << " (" << Start.CompType() << " " << Start.TargetVer() << ")";
/* Show a summary of the target package if possible. In the case
of virtual packages we show nothing */
pkgCache::PkgIterator Targ = Start.TargetPkg();
if (Targ->ProvidesList == 0)
{
out << ' ';
pkgCache::VerIterator Ver = (*Cache)[Targ].InstVerIter(*Cache);
if (Now == true)
Ver = Targ.CurrentVer();
if (Ver.end() == false)
{
if (Now == true)
ioprintf(out,_("but %s is installed"),Ver.VerStr());
else
ioprintf(out,_("but %s is to be installed"),Ver.VerStr());
}
else
{
if ((*Cache)[Targ].CandidateVerIter(*Cache).end() == true)
{
if (Targ->ProvidesList == 0)
out << _("but it is not installable");
else
out << _("but it is a virtual package");
}
else
out << (Now?_("but it is not installed"):_("but it is not going to be installed"));
}
}
if (Start != End)
out << _(" or");
out << endl;
if (Start == End)
break;
++Start;
}
}
}
void ShowBroken(ostream &out, CacheFile &Cache, bool const Now)
{
if (Cache->BrokenCount() == 0)
return;
if (_config->FindI("APT::Output-Version") < 30)
out << _("The following packages have unmet dependencies:") << endl;
else
out << _("Unsatisfied dependencies:") << endl;
SortedPackageUniverse Universe(Cache);
for (auto const &Pkg: Universe)
ShowBrokenPackage(out, &Cache, Pkg, Now);
}
void ShowBroken(ostream &out, pkgCacheFile &Cache, bool const Now)
{
if (Cache->BrokenCount() == 0)
return;
if (_config->FindI("APT::Output-Version") < 30)
out << _("The following packages have unmet dependencies:") << endl;
else
out << _("Unsatisfied dependencies:") << endl;
APT::PackageUniverse Universe(Cache);
for (auto const &Pkg: Universe)
ShowBrokenPackage(out, &Cache, Pkg, Now);
}
/*}}}*/
// ShowNew - Show packages to newly install /*{{{*/
void ShowNew(ostream &out,CacheFile &Cache)
{
SortedPackageUniverse Universe(Cache);
if (_config->FindI("APT::Output-Version") < 30) {
ShowList(out,_("The following NEW packages will be installed:"), Universe,
[&Cache](pkgCache::PkgIterator const &Pkg) { return Cache[Pkg].NewInstall(); },
&PrettyFullName,
CandidateVersion(&Cache),
"action::install");
return;
}
ShowList(out,_("Installing:"), Universe,
[&Cache](pkgCache::PkgIterator const &Pkg) { return Cache[Pkg].NewInstall() && (Cache[Pkg].Flags & pkgCache::Flag::Auto) == 0; },
&PrettyFullName,
CandidateVersion(&Cache),
"action::install");
ShowList(out,_("Installing dependencies:"), Universe,
[&Cache](pkgCache::PkgIterator const &Pkg) { return Cache[Pkg].NewInstall() && Cache[Pkg].Flags & pkgCache::Flag::Auto;},
&PrettyFullName,
CandidateVersion(&Cache),
"action::install-dependencies");
}
/*}}}*/
// ShowDel - Show packages to delete /*{{{*/
void ShowDel(ostream &out,CacheFile &Cache)
{
SortedPackageUniverse Universe(Cache);
auto title = _config->FindI("APT::Output-Version") < 30 ? _("The following packages will be REMOVED:") : _("REMOVING:");
ShowList(out,title, Universe,
[&Cache](pkgCache::PkgIterator const &Pkg) { return Cache[Pkg].Delete(); },
[&Cache](pkgCache::PkgIterator const &Pkg)
{
std::string str = PrettyFullName(Pkg);
if (((*Cache)[Pkg].iFlags & pkgDepCache::Purge) == pkgDepCache::Purge)
str.append("*");
return str;
},
CurrentVersion(&Cache),
"action::remove");
}
/*}}}*/
// ShowPhasing - Show packages kept due to phasing /*{{{*/
void ShowPhasing(ostream &out, CacheFile &Cache, APT::PackageVector const &HeldBackPackages)
{
SortedPackageUniverse Universe(Cache);
auto title = _config->FindI("APT::Output-Version") < 30
? _("The following upgrades have been deferred due to phasing:")
: _("Not upgrading yet due to phasing:");
ShowList(out, title, HeldBackPackages,
&AlwaysTrue,
&PrettyFullName,
CurrentToCandidateVersion(&Cache));
}
/*}}}*/
// ShowKept - Show kept packages /*{{{*/
void ShowKept(ostream &out,CacheFile &Cache, APT::PackageVector const &HeldBackPackages)
{
SortedPackageUniverse Universe(Cache);
auto title = _config->FindI("APT::Output-Version") < 30 ? _("The following packages have been kept back:") : _("Not upgrading:");
ShowList(out, title, HeldBackPackages,
&AlwaysTrue,
&PrettyFullName,
CurrentToCandidateVersion(&Cache));
}
/*}}}*/
// ShowUpgraded - Show upgraded packages /*{{{*/
void ShowUpgraded(ostream &out,CacheFile &Cache)
{
SortedPackageUniverse Universe(Cache);
auto title = _config->FindI("APT::Output-Version") < 30 ? _("The following packages will be upgraded:") : _("Upgrading:");
ShowList(out, title, Universe,
[&Cache](pkgCache::PkgIterator const &Pkg)
{
return Cache[Pkg].Upgrade() == true && Cache[Pkg].NewInstall() == false;
},
&PrettyFullName,
CurrentToCandidateVersion(&Cache),
"action::upgrade");
}
/*}}}*/
// ShowDowngraded - Show downgraded packages /*{{{*/
// ---------------------------------------------------------------------
/* */
bool ShowDowngraded(ostream &out,CacheFile &Cache)
{
SortedPackageUniverse Universe(Cache);
auto title = _config->FindI("APT::Output-Version") < 30 ? _("The following packages will be DOWNGRADED:") : _("DOWNGRADING:");
return ShowList(out, title, Universe,
[&Cache](pkgCache::PkgIterator const &Pkg)
{
return Cache[Pkg].Downgrade() == true && Cache[Pkg].NewInstall() == false;
},
&PrettyFullName,
CurrentToCandidateVersion(&Cache),
"action::downgrade");
}
/*}}}*/
// ShowHold - Show held but changed packages /*{{{*/
bool ShowHold(ostream &out,CacheFile &Cache)
{
SortedPackageUniverse Universe(Cache);
auto title = _config->FindI("APT::Output-Version") < 30 ? _("The following held packages will be changed:") : _("Changing held packages:");
return ShowList(out, title, Universe,
[&Cache](pkgCache::PkgIterator const &Pkg)
{
return Pkg->SelectedState == pkgCache::State::Hold &&
Cache[Pkg].InstallVer != (pkgCache::Version *)Pkg.CurrentVer();
},
&PrettyFullName,
CurrentToCandidateVersion(&Cache));
}
/*}}}*/
// ShowEssential - Show an essential package warning /*{{{*/
// ---------------------------------------------------------------------
/* This prints out a warning message that is not to be ignored. It shows
all essential packages and their dependents that are to be removed.
It is insanely risky to remove the dependents of an essential package! */
struct APT_HIDDEN PrettyFullNameWithDue {
std::map<unsigned long long, pkgCache::PkgIterator> due;
PrettyFullNameWithDue() {}
std::string operator() (pkgCache::PkgIterator const &Pkg)
{
std::string const A = PrettyFullName(Pkg);
std::map<unsigned long long, pkgCache::PkgIterator>::const_iterator d = due.find(Pkg->ID);
if (d == due.end())
return A;
std::string const B = PrettyFullName(d->second);
std::ostringstream outstr;
ioprintf(outstr, _("%s (due to %s)"), A.c_str(), B.c_str());
return outstr.str();
}
};
bool ShowEssential(ostream &out,CacheFile &Cache)
{
std::vector<bool> Added(Cache->Head().PackageCount, false);
APT::PackageDeque pkglist;
PrettyFullNameWithDue withdue;
SortedPackageUniverse Universe(Cache);
for (pkgCache::PkgIterator const &I: Universe)
{
if ((I->Flags & pkgCache::Flag::Essential) != pkgCache::Flag::Essential &&
(I->Flags & pkgCache::Flag::Important) != pkgCache::Flag::Important)
continue;
// The essential package is being removed
if (Cache[I].Delete() == false)
continue;
if (Added[I->ID] == false)
{
Added[I->ID] = true;
pkglist.insert(I);
}
if (I->CurrentVer == 0)
continue;
// Print out any essential package depenendents that are to be removed
for (pkgCache::DepIterator D = I.CurrentVer().DependsList(); D.end() == false; ++D)
{
// Skip everything but depends
if (D->Type != pkgCache::Dep::PreDepends &&
D->Type != pkgCache::Dep::Depends)
continue;
pkgCache::PkgIterator P = D.SmartTargetPkg();
if (Cache[P].Delete() == true)
{
if (Added[P->ID] == true)
continue;
Added[P->ID] = true;
pkglist.insert(P);
withdue.due[P->ID] = I;
}
}
}
return ShowList(out,_("WARNING: The following essential packages will be removed.\n"
"This should NOT be done unless you know exactly what you are doing!"),
pkglist, &AlwaysTrue, withdue, &EmptyString, "action::remove");
}
/*}}}*/
// Stats - Show some statistics /*{{{*/
// ---------------------------------------------------------------------
/* */
void Stats(ostream &out, pkgDepCache &Dep, APT::PackageVector const &HeldBackPackages)
{
unsigned long Upgrade = 0;
unsigned long Downgrade = 0;
unsigned long Install = 0;
unsigned long ReInstall = 0;
auto outVer = _config->FindI("APT::Output-Version");
for (pkgCache::PkgIterator I = Dep.PkgBegin(); I.end() == false; ++I)
{
if (Dep[I].NewInstall() == true)
Install++;
else
{
if (Dep[I].Upgrade() == true)
Upgrade++;
else
if (Dep[I].Downgrade() == true)
Downgrade++;
}
if (Dep[I].Delete() == false && (Dep[I].iFlags & pkgDepCache::ReInstall) == pkgDepCache::ReInstall)
ReInstall++;
}
if (outVer >= 30) {
ioprintf(out, _("Summary:"));
ioprintf(out, "\n ");
}
ioprintf(out,outVer < 30 ? _("%lu upgraded, %lu newly installed, ") : _("Upgrading: %lu, Installing: %lu, "),
Upgrade,Install);
if (ReInstall != 0)
ioprintf(out,outVer < 30 ? _("%lu reinstalled, ") : _("Reinstalling: %lu, "),ReInstall);
if (Downgrade != 0)
ioprintf(out,outVer < 30 ? _("%lu downgraded, ") : _("Downgrading: %lu, "),Downgrade);
ioprintf(out, outVer < 30 ? _("%lu to remove and %lu not upgraded.\n") : _("Removing: %lu, Not Upgrading: %lu\n"),
Dep.DelCount(), HeldBackPackages.size());
if (Dep.BadCount() != 0) {
if (outVer >= 30)
ioprintf(out, " ");
ioprintf(out,_("%lu not fully installed or removed.\n"),
Dep.BadCount());
}
}
/*}}}*/
// YnPrompt - Yes No Prompt. /*{{{*/
// ---------------------------------------------------------------------
/* Returns true on a Yes.*/
bool YnPrompt(char const * const Question, bool const Default, bool const ShowGlobalErrors, std::ostream &c1o, std::ostream &c2o)
{
auto const AssumeYes = _config->FindB("APT::Get::Assume-Yes",false);
auto const AssumeNo = _config->FindB("APT::Get::Assume-No",false);
// if we ask interactively, show warnings/notices before the question
if (ShowGlobalErrors == true && AssumeYes == false && AssumeNo == false)
{
if (_config->FindB("APT::Audit"))
_error->DumpErrors(c2o, GlobalError::AUDIT);
else if (_config->FindI("quiet",0) > 0)
_error->DumpErrors(c2o);
else
_error->DumpErrors(c2o, GlobalError::NOTICE);
}
// ignore pending input on terminal
if (not AssumeYes && not AssumeNo && isatty(STDIN_FILENO) == 1)
{
tcflush(STDIN_FILENO, TCIFLUSH);
std::cin.clear();
}
c2o << Question << std::flush;
/* nl_langinfo does not support LANGUAGE setting, so we unset it here
to have the help-message (hopefully) match the expected characters */
char * language = getenv("LANGUAGE");
if (language != NULL)
language = strdup(language);
if (language != NULL)
unsetenv("LANGUAGE");
if (Default == true)
// TRANSLATOR: Yes/No question help-text: defaulting to Y[es]
// e.g. "Do you want to continue? [Y/n] "
// The user has to answer with an input matching the
// YESEXPR/NOEXPR defined in your l10n.
c2o << " " << _("[Y/n]") << " " << std::flush;
else
// TRANSLATOR: Yes/No question help-text: defaulting to N[o]
// e.g. "Should this file be removed? [y/N] "
// The user has to answer with an input matching the
// YESEXPR/NOEXPR defined in your l10n.
c2o << " " << _("[y/N]") << " " << std::flush;
if (language != NULL)
{
setenv("LANGUAGE", language, 0);
free(language);
}
if (AssumeYes)
{
// TRANSLATOR: "Yes" answer printed for a yes/no question if --assume-yes is set
c1o << _("Y") << std::endl;
return true;
}
else if (AssumeNo)
{
// TRANSLATOR: "No" answer printed for a yes/no question if --assume-no is set
c1o << _("N") << std::endl;
return false;
}
char response[1024] = "";
std::cin.getline(response, sizeof(response));
if (!std::cin)
return false;
if (isatty(STDIN_FILENO) == 0)
c1o << response << '\n';
if (strlen(response) == 0)
return Default;
regex_t Pattern;
int Res;
Res = regcomp(&Pattern, nl_langinfo(YESEXPR),
REG_EXTENDED|REG_ICASE|REG_NOSUB);
if (Res != 0) {
char Error[300];
regerror(Res,&Pattern,Error,sizeof(Error));
return _error->Error(_("Regex compilation error - %s"),Error);
}
Res = regexec(&Pattern, response, 0, NULL, 0);
if (Res == 0)
return true;
return false;
}
bool YnPrompt(char const * const Question, bool const Default)
{
return YnPrompt(Question, Default, true, c1out, c2out);
}
/*}}}*/
std::string PrettyFullName(pkgCache::PkgIterator const &Pkg)
{
return Pkg.FullName(true);
}
std::string CurrentVersion(pkgCacheFile * const Cache, pkgCache::PkgIterator const &Pkg)
{
return (*Cache)[Pkg].CurVersion;
}
std::function<std::string(pkgCache::PkgIterator const &)> CurrentVersion(pkgCacheFile * const Cache)
{
return [=](pkgCache::PkgIterator const &i) { return CurrentVersion(Cache, i); };
}
std::string CandidateVersion(pkgCacheFile * const Cache, pkgCache::PkgIterator const &Pkg)
{
return (*Cache)[Pkg].CandVersion;
}
std::function<std::string(pkgCache::PkgIterator const &)> CandidateVersion(pkgCacheFile * const Cache)
{
return [=](pkgCache::PkgIterator const &i) { return CandidateVersion(Cache, i); };
}
std::string CurrentToCandidateVersion(pkgCacheFile * const Cache, pkgCache::PkgIterator const &Pkg)
{
std::string const CurVer = (*Cache)[Pkg].CurVersion;
std::string CandVer = (*Cache)[Pkg].CandVersion;
if (CurVer == CandVer)
{
auto const CandVerIter = Cache->GetPolicy()->GetCandidateVer(Pkg);
if (not CandVerIter.end())
CandVer = CandVerIter.VerStr();
}
return CurVer + " => " + CandVer;
}
std::function<std::string(pkgCache::PkgIterator const &)> CurrentToCandidateVersion(pkgCacheFile * const Cache)
{
return [=](pkgCache::PkgIterator const &i) { return CurrentToCandidateVersion(Cache, i); };
}
bool AlwaysTrue(pkgCache::PkgIterator const &)
{
return true;
}
std::string EmptyString(pkgCache::PkgIterator const &)
{
return std::string();
}
|