diff --git a/src/backend.cpp b/src/backend.cpp index 5f6a558..a6a256c 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -1,1648 +1,1648 @@ /*************************************************************************** * Copyright © 2010-2012 Jonathan Thomas * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * * published by the Free Software Foundation; either version 2 of * * the License or (at your option) version 3 or any later version * * accepted by the membership of KDE e.V. (or its successor approved * * by the membership of KDE e.V.), which shall act as a proxy * * defined in Section 14 of version 3 of the license. * * * * 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 General Public License * * along with this program. If not, see . * ***************************************************************************/ #include "backend.h" // Qt includes #include #include #include // Apt includes #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // Xapian includes #undef slots #include // QApt includes #include "cache.h" #include "config.h" // krazy:exclude=includes #include "dbusinterfaces_p.h" #include "debfile.h" #include "transaction.h" namespace QApt { class BackendPrivate { public: BackendPrivate() : cache(nullptr) , records(nullptr) , maxStackSize(20) , xapianDatabase(nullptr) , xapianIndexExists(false) , config(nullptr) , actionGroup(nullptr) , frontendCaps(QApt::NoCaps) { } ~BackendPrivate() { qDeleteAll(packages); delete cache; delete records; delete config; delete xapianDatabase; delete actionGroup; } // Caches // The canonical list of all unique, non-virutal package objects PackageList packages; // A list of each package object's ID number QVector packagesIndex; // Set of group names extracted from our packages QSet groups; // Cache of origin/human-readable name pairings QHash originMap; // Relation of an origin and its hostname QHash siteMap; // Date when the distribution's release was issued. See Backend::releaseDate() QDateTime releaseDate; QDateTime getReleaseDateFromDistroInfo(const QString &releaseId, const QString &releaseCodename) const; QDateTime getReleaseDateFromArchive(const QString &releaseId, const QString &releaseCodename) const; // Counts int installedCount; // Pointer to the apt cache object Cache *cache; pkgRecords *records; // Undo/redo stuff int maxStackSize; QList undoStack; QList redoStack; // Xapian time_t xapianTimeStamp; Xapian::Database *xapianDatabase; bool xapianIndexExists; // DBus WorkerInterface *worker; // Config Config *config; bool isMultiArch; QString nativeArch; // Event compression bool compressEvents; pkgDepCache::ActionGroup *actionGroup; // Other bool writeSelectionFile(const QString &file, const QString &path) const; QString customProxy; QString initErrorMessage; QApt::FrontendCaps frontendCaps; }; QDateTime BackendPrivate::getReleaseDateFromDistroInfo(const QString &releaseId, const QString &releaseCodename) const { QDateTime releaseDate; QString line; QStringList split; QFile distro_info(QStringLiteral("/usr/share/distro-info/%1.csv").arg(releaseId.toLower())); if (distro_info.open(QFile::ReadOnly)) { QTextStream info_stream(&distro_info); line = info_stream.readLine(); split = line.split(QLatin1Char(',')); const int codenameColumn = split.indexOf(QStringLiteral("series")); const int releaseColumn = split.indexOf(QStringLiteral("release")); if (codenameColumn == -1 || releaseColumn == -1) { return QDateTime(); } do { line = info_stream.readLine(); split = line.split(QLatin1Char(',')); if (split.value(codenameColumn) == releaseCodename) { releaseDate = QDateTime::fromString(split.value(releaseColumn), Qt::ISODate); releaseDate.setTimeSpec(Qt::UTC);; break; } } while (!line.isNull()); } return releaseDate; } QDateTime BackendPrivate::getReleaseDateFromArchive(const QString &releaseId, const QString &releaseCodename) const { pkgDepCache *depCache = cache->depCache(); // We are only interested in `*ubuntu_dists__[In]Release` // in order to get the release date. In `-updates` and // `-security` the Date gets updated throughout the life of the release. pkgCache::RlsFileIterator rls; for (rls = depCache->GetCache().RlsFileBegin(); !rls.end(); ++rls) { if (rls.Origin() == releaseId && rls.Label() == releaseId && rls.Archive() == releaseCodename) { FileFd fd; if (!OpenMaybeClearSignedFile(rls.FileName(), fd)) { continue; } time_t releaseDate = -1; pkgTagSection sec; pkgTagFile tag(&fd); tag.Step(sec); if(!RFC1123StrToTime(sec.FindS("Date").data(), releaseDate)) { continue; } return QDateTime::fromSecsSinceEpoch(releaseDate); } } return QDateTime(); } bool BackendPrivate::writeSelectionFile(const QString &selectionDocument, const QString &path) const { QFile file(path); if (!file.open(QFile::WriteOnly | QFile::Text)) { return false; } else { QTextStream out(&file); out << selectionDocument; } return true; } Backend::Backend(QObject *parent) : QObject(parent) , d_ptr(new BackendPrivate) { Q_D(Backend); d->worker = new WorkerInterface(QLatin1String(s_workerReverseDomainName), QLatin1String("/"), QDBusConnection::systemBus(), this); connect(d->worker, SIGNAL(transactionQueueChanged(QString,QStringList)), this, SIGNAL(transactionQueueChanged(QString,QStringList))); DownloadProgress::registerMetaTypes(); } Backend::~Backend() { delete d_ptr; } bool Backend::init() { Q_D(Backend); if (!pkgInitConfig(*_config) || !pkgInitSystem(*_config, _system)) { setInitError(); return false; } d->cache = new Cache(this); d->config = new Config(this); d->nativeArch = config()->readEntry(QLatin1String("APT::Architecture"), QLatin1String("")); openXapianIndex(); return reloadCache(); } bool Backend::reloadCache() { Q_D(Backend); emit cacheReloadStarted(); if (!d->cache->open()) { setInitError(); return false; } pkgDepCache *depCache = d->cache->depCache(); delete d->records; d->records = new pkgRecords(*depCache); qDeleteAll(d->packages); d->packages.clear(); d->groups.clear(); d->originMap.clear(); d->siteMap.clear(); d->packagesIndex.clear(); d->installedCount = 0; int packageCount = depCache->Head().PackageCount; d->packagesIndex.resize(packageCount); d->packagesIndex.fill(-1); d->packages.reserve(packageCount); // Populate internal package cache int count = 0; d->isMultiArch = architectures().size() > 1; pkgCache::PkgIterator iter; for (iter = depCache->PkgBegin(); !iter.end(); ++iter) { if (!iter->VersionList) { continue; // Exclude virtual packages. } Package *pkg = new Package(this, iter); d->packagesIndex[iter->ID] = count; d->packages.append(pkg); ++count; if (iter->CurrentVer) { d->installedCount++; } QString group = pkg->section(); // Populate groups if (!group.isEmpty()) { d->groups << group; } pkgCache::VerIterator Ver = (*depCache)[iter].CandidateVerIter(*depCache); if(!Ver.end()) { const pkgCache::VerFileIterator VF = Ver.FileList(); const QString origin(QLatin1String(VF.File().Origin())); d->originMap[origin] = QLatin1String(VF.File().Label()); d->siteMap[origin] = QLatin1String(VF.File().Site()); } } d->originMap.remove(QString()); d->undoStack.clear(); d->redoStack.clear(); // Determine which packages are pinned for display purposes loadPackagePins(); loadReleaseDate(); emit cacheReloadFinished(); return true; } void Backend::setInitError() { Q_D(Backend); std::string message; if (_error->PopMessage(message)) d->initErrorMessage = QString::fromStdString(message); } void Backend::loadPackagePins() { Q_D(Backend); QString dirBase = d->config->findDirectory(QLatin1String("Dir::Etc")); QString dir = dirBase % QLatin1String("preferences.d/"); QDir logDirectory(dir); QStringList pinFiles = logDirectory.entryList(QDir::Files, QDir::Name); pinFiles << dirBase % QLatin1String("preferences"); for (const QString &pinName : pinFiles) { // Make all paths absolute QString pinPath = pinName.startsWith('/') ? pinName : dir % pinName; if (!QFile::exists(pinPath)) continue; FileFd Fd(pinPath.toUtf8().data(), FileFd::ReadOnly); pkgTagFile tagFile(&Fd); if (_error->PendingError()) { _error->Discard(); continue; } pkgTagSection tags; while (tagFile.Step(tags)) { std::string name = tags.FindS("Package"); Package *pkg = package(QLatin1String(name.c_str())); if (pkg) pkg->setPinned(true); } } } void Backend::loadReleaseDate() { Q_D(Backend); // Reset value in case we are re-loading cache d->releaseDate = QDateTime(); QString releaseId; QString releaseCodename; QFile lsb_release(QLatin1String("/etc/os-release")); if (!lsb_release.open(QFile::ReadOnly)) { // Though really, your system is screwed if this happens... return; } QTextStream stream(&lsb_release); QString line; do { line = stream.readLine(); QStringList split = line.split(QLatin1Char('=')); if (split.size() != 2) { continue; } if (split.at(0) == QLatin1String("VERSION_CODENAME")) { releaseCodename = split.at(1); } else if (split.at(0) == QLatin1String("ID")) { releaseId = split.at(1); } } while (!line.isNull()); d->releaseDate = d->getReleaseDateFromDistroInfo(releaseId, releaseCodename); if (!d->releaseDate.isValid()) { // If we could not find the date in the csv file, we fallback to Apt archive. d->releaseDate = d->getReleaseDateFromArchive(releaseId, releaseCodename); } } QString Backend::initErrorMessage() const { Q_D(const Backend); return d->initErrorMessage; } pkgSourceList *Backend::packageSourceList() const { Q_D(const Backend); return d->cache->list(); } Cache *Backend::cache() const { Q_D(const Backend); return d->cache; } pkgRecords *Backend::records() const { Q_D(const Backend); return d->records; } Package *Backend::package(pkgCache::PkgIterator &iter) const { Q_D(const Backend); int index = d->packagesIndex.at(iter->ID); if (index != -1 && index < d->packages.size()) { return d->packages.at(index); } return nullptr; } Package *Backend::package(const QString &name) const { return package(QLatin1String(name.toLatin1())); } Package *Backend::package(QLatin1String name) const { Q_D(const Backend); pkgCache::PkgIterator pkg = d->cache->depCache()->FindPkg(name.latin1()); if (!pkg.end()) { return package(pkg); } return nullptr; } Package *Backend::packageForFile(const QString &file) const { Q_D(const Backend); if (file.isEmpty()) { return nullptr; } for (Package *package : d->packages) { if (package->installedFilesList().contains(file)) { return package; } } return nullptr; } QStringList Backend::origins() const { Q_D(const Backend); return d->originMap.keys(); } QStringList Backend::originLabels() const { Q_D(const Backend); return d->originMap.values(); } QString Backend::originLabel(const QString &origin) const { Q_D(const Backend); return d->originMap.value(origin); } QString Backend::origin(const QString &originLabel) const { Q_D(const Backend); return d->originMap.key(originLabel); } QStringList Backend::originsForHost(const QString& host) const { Q_D(const Backend); return d->siteMap.keys(host); } int Backend::packageCount() const { Q_D(const Backend); return d->packages.size(); } int Backend::packageCount(const Package::States &states) const { Q_D(const Backend); int packageCount = 0; for (const Package *package : d->packages) { if ((package->state() & states)) { packageCount++; } } return packageCount; } int Backend::installedCount() const { Q_D(const Backend); return d->installedCount; } int Backend::toInstallCount() const { Q_D(const Backend); return d->cache->depCache()->InstCount(); } int Backend::toRemoveCount() const { Q_D(const Backend); return d->cache->depCache()->DelCount(); } qint64 Backend::downloadSize() const { Q_D(const Backend); // Raw size, ignoring already-downloaded or partially downloaded archives qint64 downloadSize = d->cache->depCache()->DebSize(); // If downloadSize() is called during a cache refresh, there is a chance it // will do so at a bad time and produce an error. Discard any errors that // happen during this function, since they will always be innocuous and at // worst will result in the less accurate DebSize() number being returned _error->PushToStack(); // If possible, get what really needs to be downloaded pkgAcquire fetcher; pkgPackageManager *PM = _system->CreatePM(d->cache->depCache()); if (PM->GetArchives(&fetcher, d->cache->list(), d->records)) { downloadSize = fetcher.FetchNeeded(); } delete PM; _error->Discard(); _error->RevertToStack(); return downloadSize; } qint64 Backend::installSize() const { Q_D(const Backend); qint64 installSize = d->cache->depCache()->UsrSize(); return installSize; } PackageList Backend::availablePackages() const { Q_D(const Backend); return d->packages; } PackageList Backend::upgradeablePackages() const { Q_D(const Backend); PackageList upgradeablePackages; for (Package *package : d->packages) { if (package->staticState() & Package::Upgradeable) { upgradeablePackages << package; } } return upgradeablePackages; } PackageList Backend::markedPackages() const { Q_D(const Backend); PackageList markedPackages; for (Package *package : d->packages) { if (package->state() & (Package::ToInstall | Package::ToReInstall | Package::ToUpgrade | Package::ToDowngrade | Package::ToRemove | Package::ToPurge)) { markedPackages << package; } } return markedPackages; } PackageList Backend::search(const QString &searchString) const { Q_D(const Backend); if (d->xapianTimeStamp == 0 || !d->xapianDatabase) { return QApt::PackageList(); } std::string unsplitSearchString = searchString.toStdString(); static int qualityCutoff = 15; PackageList searchResult; // Doesn't follow style guidelines to ease merging with synaptic try { int maxItems = d->xapianDatabase->get_doccount(); Xapian::Enquire enquire(*(d->xapianDatabase)); Xapian::QueryParser parser; parser.set_database(*(d->xapianDatabase)); parser.add_prefix("name","XP"); parser.add_prefix("section","XS"); // default op is AND to narrow down the resultset parser.set_default_op( Xapian::Query::OP_AND ); /* Workaround to allow searching an hyphenated package name using a prefix (name:) * LP: #282995 * Xapian currently doesn't support wildcard for boolean prefix and * doesn't handle implicit wildcards at the end of hypenated phrases. * * e.g searching for name:ubuntu-res will be equivalent to 'name:ubuntu res*' * however 'name:(ubuntu* res*) won't return any result because the * index is built with the full package name */ // Always search for the package name std::string xpString = "name:"; std::string::size_type pos = unsplitSearchString.find_first_of(" ,;"); if (pos > 0) { xpString += unsplitSearchString.substr(0,pos); } else { xpString += unsplitSearchString; } Xapian::Query xpQuery = parser.parse_query(xpString); pos = 0; while ( (pos = unsplitSearchString.find("-", pos)) != std::string::npos ) { unsplitSearchString.replace(pos, 1, " "); pos+=1; } // Build the query // apply a weight factor to XP term to increase relevancy on package name Xapian::Query query = parser.parse_query(unsplitSearchString, Xapian::QueryParser::FLAG_WILDCARD | Xapian::QueryParser::FLAG_BOOLEAN | Xapian::QueryParser::FLAG_PARTIAL); query = Xapian::Query(Xapian::Query::OP_OR, query, Xapian::Query(Xapian::Query::OP_SCALE_WEIGHT, xpQuery, 3)); enquire.set_query(query); Xapian::MSet matches = enquire.get_mset(0, maxItems); // Retrieve the results int top_percent = 0; for (Xapian::MSetIterator i = matches.begin(); i != matches.end(); ++i) { std::string pkgName = i.get_document().get_data(); Package* pkg = package(QLatin1String(pkgName.c_str())); // Filter out results that apt doesn't know if (!pkg) continue; // Save the confidence interval of the top value, to use it as // a reference to compute an adaptive quality cutoff if (top_percent == 0) top_percent = i.get_percent(); // Stop producing if the quality goes below a cutoff point if (i.get_percent() < qualityCutoff * top_percent / 100) { break; } searchResult.append(pkg); } } catch (const Xapian::Error & error) { qDebug() << "Search error" << QString::fromStdString(error.get_msg()); return QApt::PackageList(); } return searchResult; } GroupList Backend::availableGroups() const { Q_D(const Backend); GroupList groupList = d->groups.toList(); return groupList; } bool Backend::isMultiArchEnabled() const { Q_D(const Backend); return d->isMultiArch; } QStringList Backend::architectures() const { Q_D(const Backend); return d->config->architectures(); } QString Backend::nativeArchitecture() const { Q_D(const Backend); return d->nativeArch; } QDateTime Backend::releaseDate() const { Q_D(const Backend); return d->releaseDate; } bool Backend::areChangesMarked() const { return (toInstallCount() + toRemoveCount()); } bool Backend::isBroken() const { Q_D(const Backend); if (!d->cache->depCache()) { return true; } // Check for broken things if (d->cache->depCache()->BrokenCount()) { return true; } return false; } QApt::FrontendCaps Backend::frontendCaps() const { Q_D(const Backend); return d->frontendCaps; } QDateTime Backend::timeCacheLastUpdated() const { QDateTime sinceUpdate; QFileInfo updateStamp("/var/lib/apt/periodic/update-success-stamp"); if (!updateStamp.exists()) return sinceUpdate; return updateStamp.lastModified(); } bool Backend::xapianIndexNeedsUpdate() const { Q_D(const Backend); // If the cache has been modified after the xapian timestamp, we need to rebuild QDateTime aptCacheTime = QFileInfo(d->config->findFile("Dir::Cache::pkgcache")).lastModified(); return ((d->xapianTimeStamp < aptCacheTime.toTime_t()) || (!d->xapianIndexExists)); } bool Backend::openXapianIndex() { Q_D(Backend); QFileInfo timeStamp(QLatin1String("/var/lib/apt-xapian-index/update-timestamp")); d->xapianTimeStamp = timeStamp.lastModified().toTime_t(); if(d->xapianDatabase) { delete d->xapianDatabase; d->xapianDatabase = 0; } try { d->xapianDatabase = new Xapian::Database("/var/lib/apt-xapian-index/index"); d->xapianIndexExists = true; } catch (Xapian::DatabaseOpeningError) { d->xapianIndexExists = false; return false; }; return true; } Config *Backend::config() const { Q_D(const Backend); return d->config; } CacheState Backend::currentCacheState() const { Q_D(const Backend); CacheState state; int pkgSize = d->packages.size(); state.reserve(pkgSize); for (int i = 0; i < pkgSize; ++i) { state.append(d->packages.at(i)->state()); } return state; } QHash Backend::stateChanges(const CacheState &oldState, const PackageList &excluded) const { Q_D(const Backend); QHash changes; // Return an empty change set for invalid state caches if (oldState.isEmpty()) return changes; Q_ASSERT(d->packages.size() == oldState.size()); for (int i = 0; i < d->packages.size(); ++i) { Package *pkg = d->packages.at(i); if (excluded.contains(pkg)) continue; int status = pkg->state(); if (oldState.at(i) == status) continue; // These flags will never be set together. // We can use this to filter status down to a single flag. status &= (Package::Held | Package::NewInstall | Package::ToReInstall | Package::ToUpgrade | Package::ToDowngrade | Package::ToRemove); if (status == 0) { qWarning() << "Package" << pkg->name() << "had a state change," << "it can however not be presented as a unique state." << "This is often an indication that the package is" << "supposed to be upgraded but can't because its" << "dependencies are not satisfied. This is not" << "considered a held package unless its upgrade is" << "necessary or causing breakage. A simple unsatisfied" << "dependency without the need to upgrade is not" << "considered an issue and thus not reported.\n" << "States were:" << (Package::States)oldState.at(i) << "->" << (Package::States)pkg->state(); // Apt pretends packages like this are not held (which is reflected) // in the state loss. Whether or not this is intentional is not // obvious at the time of writing in case it isn't the states // here would add up again and the package rightfully would be // reported as held. So we would never get here. // Until then ignore these packages as we cannot serialize their // state anyway. continue; } // Add this package/status pair to the changes hash PackageList list = changes.value((Package::State)status); list.append(pkg); changes[(Package::State)status]= list; } return changes; } void Backend::saveCacheState() { Q_D(Backend); CacheState state = currentCacheState(); d->undoStack.prepend(state); d->redoStack.clear(); while (d->undoStack.size() > d->maxStackSize) { d->undoStack.removeLast(); } } void Backend::restoreCacheState(const CacheState &state) { Q_D(Backend); pkgDepCache *deps = d->cache->depCache(); pkgDepCache::ActionGroup group(*deps); int packageCount = d->packages.size(); for (int i = 0; i < packageCount; ++i) { Package *pkg = d->packages.at(i); int flags = pkg->state(); int oldflags = state.at(i); if (oldflags == flags) continue; if ((flags & Package::ToReInstall) && !(oldflags & Package::ToReInstall)) { deps->SetReInstall(pkg->packageIterator(), false); } if (oldflags & Package::ToReInstall) { deps->MarkInstall(pkg->packageIterator(), true); deps->SetReInstall(pkg->packageIterator(), true); } else if (oldflags & Package::ToInstall) { deps->MarkInstall(pkg->packageIterator(), true); } else if (oldflags & Package::ToRemove) { deps->MarkDelete(pkg->packageIterator(), (bool)(oldflags & Package::ToPurge)); } else if (oldflags & Package::ToKeep) { deps->MarkKeep(pkg->packageIterator(), false); } // fix the auto flag deps->MarkAuto(pkg->packageIterator(), (oldflags & Package::IsAuto)); } emit packageChanged(); } void Backend::setUndoRedoCacheSize(int newSize) { Q_D(Backend); d->maxStackSize = newSize; } bool Backend::isUndoStackEmpty() const { Q_D(const Backend); return d->undoStack.isEmpty(); } bool Backend::isRedoStackEmpty() const { Q_D(const Backend); return d->redoStack.isEmpty(); } bool Backend::areEventsCompressed() const { Q_D(const Backend); return d->actionGroup != nullptr; } void Backend::undo() { Q_D(Backend); if (d->undoStack.isEmpty()) { return; } // Place current state on redo stack d->redoStack.prepend(currentCacheState()); CacheState state = d->undoStack.takeFirst(); restoreCacheState(state); } void Backend::redo() { Q_D(Backend); if (d->redoStack.isEmpty()) { return; } // Place current state on undo stack d->undoStack.append(currentCacheState()); CacheState state = d->redoStack.takeFirst(); restoreCacheState(state); } void Backend::markPackagesForUpgrade() { Q_D(Backend); APT::Upgrade::Upgrade(*d->cache->depCache(), APT::Upgrade::FORBID_REMOVE_PACKAGES | APT::Upgrade::FORBID_INSTALL_NEW_PACKAGES); emit packageChanged(); } void Backend::markPackagesForDistUpgrade() { Q_D(Backend); APT::Upgrade::Upgrade(*d->cache->depCache(), APT::Upgrade::ALLOW_EVERYTHING); emit packageChanged(); } void Backend::markPackagesForAutoRemove() { Q_D(Backend); pkgDepCache &cache = *d->cache->depCache(); bool isResidual; for (pkgCache::PkgIterator pkgIter = cache.PkgBegin(); !pkgIter.end(); ++pkgIter) { // Auto-removable packages are marked as garbage in the cache if (!cache[pkgIter].Garbage) continue; isResidual = pkgIter->CurrentState == pkgCache::State::ConfigFiles; // Delete auto-removable packages, but we can't delete residual packages if (pkgIter.CurrentVer() && !isResidual) cache.MarkDelete(pkgIter, false); } emit packageChanged(); } void Backend::markPackageForInstall(const QString &name) { Package *pkg = package(name); pkg->setInstall(); } void Backend::markPackageForRemoval(const QString &name) { Package *pkg = package(name); pkg->setRemove(); } void Backend::markPackages(const QApt::PackageList &packages, QApt::Package::State action) { Q_D(Backend); if (packages.isEmpty()) { return; } pkgDepCache *deps = d->cache->depCache(); setCompressEvents(true); foreach (Package *package, packages) { const pkgCache::PkgIterator &iter = package->packageIterator(); switch (action) { case Package::ToInstall: { int state = package->staticState(); // Mark for install if not already installed, or if upgradeable if (!(state & Package::Installed) || (state & Package::Upgradeable)) { package->setInstall(); } break; } case Package::ToRemove: if (package->isInstalled()) { package->setRemove(); } break; case Package::ToUpgrade: { bool fromUser = !(package->state() & Package::IsAuto); deps->MarkInstall(iter, true, 0, fromUser); break; } case Package::ToReInstall: { int state = package->staticState(); if(state & Package::Installed && !(state & Package::NotDownloadable) && !(state & Package::Upgradeable)) { package->setReInstall(); } break; } case Package::ToKeep: package->setKeep(); break; case Package::ToPurge: { int state = package->staticState(); if ((state & Package::Installed) || (state & Package::ResidualConfig)) { package->setPurge(); } break; } default: break; } } setCompressEvents(false); emit packageChanged(); } void Backend::setCompressEvents(bool enabled) { Q_D(Backend); if (enabled) { // Ignore if already compressed if (d->actionGroup != nullptr) return; // Presence of an ActionGroup compresses marking events over its lifetime d->actionGroup = new pkgDepCache::ActionGroup(*d->cache->depCache()); } else { delete d->actionGroup; d->actionGroup = nullptr; emit packageChanged(); } } QApt::Transaction * Backend::commitChanges() { Q_D(Backend); QVariantMap packageList; for (const Package *package : d->packages) { int flags = package->state(); std::string fullName = package->packageIterator().FullName(); // Cannot have any of these flags simultaneously int status = flags & (Package::IsManuallyHeld | Package::NewInstall | Package::ToReInstall | Package::ToUpgrade | Package::ToDowngrade | Package::ToRemove); switch (status) { case Package::IsManuallyHeld: packageList.insert(QString::fromStdString(fullName), Package::Held); break; case Package::NewInstall: if (!(flags & Package::IsAuto)) { packageList.insert(QString::fromStdString(fullName), Package::ToInstall); } break; case Package::ToReInstall: packageList.insert(QString::fromStdString(fullName), Package::ToReInstall); break; case Package::ToUpgrade: packageList.insert(QString::fromStdString(fullName), Package::ToUpgrade); break; case Package::ToDowngrade: packageList.insert(QString(QString::fromStdString(fullName)) % ',' % package->availableVersion(), Package::ToDowngrade); break; case Package::ToRemove: if(flags & Package::ToPurge) { packageList.insert(QString::fromStdString(fullName), Package::ToPurge); } else { packageList.insert(QString::fromStdString(fullName), Package::ToRemove); } break; } } QDBusPendingReply rep = d->worker->commitChanges(packageList); Transaction *trans = new Transaction(rep.value()); trans->setFrontendCaps(d->frontendCaps); return trans; } QApt::Transaction * Backend::installPackages(PackageList packages) { Q_D(Backend); QVariantMap packageList; for (const Package *package : packages) { std::string fullName = package->packageIterator().FullName(); packageList.insert(QString::fromStdString(fullName), Package::ToInstall); } QDBusPendingReply rep = d->worker->commitChanges(packageList); Transaction *trans = new Transaction(rep.value()); trans->setFrontendCaps(d->frontendCaps); return trans; } QApt::Transaction * Backend::removePackages(PackageList packages) { Q_D(Backend); QVariantMap packageList; for (const Package *package : packages) { std::string fullName = package->packageIterator().FullName(); packageList.insert(QString::fromStdString(fullName), Package::ToRemove); } QDBusPendingReply rep = d->worker->commitChanges(packageList); Transaction *trans = new Transaction(rep.value()); trans->setFrontendCaps(d->frontendCaps); return trans; } Transaction *Backend::downloadArchives(const QString &listFile, const QString &destination) { Q_D(Backend); QFile file(listFile); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { return nullptr; } QByteArray buffer = file.readAll(); QList lines = buffer.split('\n'); if (lines.isEmpty() || lines.first() != QByteArray("[Download List]")) { return nullptr; } lines.removeAt(0); QStringList packages; foreach (const QByteArray &line, lines) { packages << line; } QString dirName = listFile.left(listFile.lastIndexOf('/')); QDir dir(dirName); dir.mkdir(QLatin1String("packages")); QDBusPendingReply rep = d->worker->downloadArchives(packages, destination); Transaction *trans = new Transaction(rep.value()); trans->setFrontendCaps(d->frontendCaps); return trans; } Transaction *Backend::installFile(const DebFile &debFile) { Q_D(Backend); QDBusPendingReply rep = d->worker->installFile(debFile.filePath()); Transaction *trans = new Transaction(rep.value()); trans->setFrontendCaps(d->frontendCaps); return trans; } void Backend::emitPackageChanged() { emit packageChanged(); } Transaction *Backend::updateCache() { Q_D(Backend); QDBusPendingReply rep = d->worker->updateCache(); Transaction *trans = new Transaction(rep.value()); trans->setFrontendCaps(d->frontendCaps); return trans; } Transaction *Backend::upgradeSystem(UpgradeType upgradeType) { Q_D(Backend); bool safeUpgrade = (upgradeType == QApt::SafeUpgrade); QDBusPendingReply rep = d->worker->upgradeSystem(safeUpgrade); Transaction *trans = new Transaction(rep.value()); trans->setFrontendCaps(d->frontendCaps); return trans; } bool Backend::saveInstalledPackagesList(const QString &path) const { Q_D(const Backend); QString selectionDocument; for (int i = 0; i < d->packages.size(); ++i) { if (d->packages.at(i)->isInstalled()) { selectionDocument.append(d->packages[i]->name() % - QLatin1Literal("\t\tinstall") % QLatin1Char('\n')); + QLatin1String("\t\tinstall") % QLatin1Char('\n')); } } if (selectionDocument.isEmpty()) { return false; } return d->writeSelectionFile(selectionDocument, path); } bool Backend::saveSelections(const QString &path) const { Q_D(const Backend); QString selectionDocument; for (int i = 0; i < d->packages.size(); ++i) { int flags = d->packages.at(i)->state(); if (flags & Package::ToInstall) { selectionDocument.append(d->packages[i]->name() % - QLatin1Literal("\t\tinstall") % QLatin1Char('\n')); + QLatin1String("\t\tinstall") % QLatin1Char('\n')); } else if (flags & Package::ToRemove) { selectionDocument.append(d->packages[i]->name() % - QLatin1Literal("\t\tdeinstall") % QLatin1Char('\n')); + QLatin1String("\t\tdeinstall") % QLatin1Char('\n')); } } if (selectionDocument.isEmpty()) { return false; } return d->writeSelectionFile(selectionDocument, path); } bool Backend::loadSelections(const QString &path) { Q_D(Backend); QFile file(path); if (!file.open(QFile::ReadOnly)) { return false; } int lineIndex = 0; QByteArray buffer = file.readAll(); QList lines = buffer.split('\n'); QHash actionMap; while (lineIndex < lines.size()) { QByteArray line = lines.at(lineIndex); if (line.isEmpty() || line.at(0) == '#') { lineIndex++; continue; } line = line.trimmed(); QByteArray aKey; std::string keyString; const char *data = line.constData(); if (ParseQuoteWord(data, keyString) == false) return false; aKey = QByteArray(keyString.c_str()); QByteArray aValue; std::string valueString; if (ParseQuoteWord(data, valueString) == false) return false; aValue = QByteArray(valueString.c_str()); if (aValue.at(0) == 'i') { actionMap[aKey] = Package::ToInstall; } else if ((aValue.at(0) == 'd') || (aValue.at(0) == 'u') || (aValue.at(0) == 'r')) { actionMap[aKey] = Package::ToRemove; } else if (aValue.at(0) == 'p') { actionMap[aKey] = Package::ToPurge; } ++lineIndex; } if (actionMap.isEmpty()) { return false; } pkgDepCache &cache = *d->cache->depCache(); // Should protect whatever is already selected in the cache. pkgProblemResolver Fix(&cache); pkgCache::PkgIterator pkgIter; auto mapIter = actionMap.constBegin(); while (mapIter != actionMap.constEnd()) { pkgIter = d->cache->depCache()->FindPkg(mapIter.key().constData()); if (pkgIter.end()) { return false; } Fix.Clear(pkgIter); Fix.Protect(pkgIter); switch (mapIter.value()) { case Package::ToInstall: if (pkgIter.CurrentVer().end()) { // Only mark if not already installed cache.MarkInstall(pkgIter, true); } break; case Package::ToRemove: Fix.Remove(pkgIter); cache.MarkDelete(pkgIter, false); break; case Package::ToPurge: Fix.Remove(pkgIter); cache.MarkDelete(pkgIter, true); break; } ++mapIter; } Fix.Resolve(true); emit packageChanged(); return true; } bool Backend::saveDownloadList(const QString &path) const { Q_D(const Backend); QString downloadDocument; downloadDocument.append(QLatin1String("[Download List]") % QLatin1Char('\n')); for (int i = 0; i < d->packages.size(); ++i) { int flags = d->packages.at(i)->state(); if (flags & Package::ToInstall) { downloadDocument.append(d->packages[i]->name() % QLatin1Char('\n')); } } return d->writeSelectionFile(downloadDocument, path); } bool Backend::setPackagePinned(Package *package, bool pin) { Q_D(Backend); QString dir = d->config->findDirectory("Dir::Etc") % QLatin1String("preferences.d/"); QString path = dir % package->name(); QString pinDocument; if (pin) { if (package->state() & Package::IsPinned) { return true; } - pinDocument = QLatin1Literal("Package: ") % package->name() + pinDocument = QLatin1String("Package: ") % package->name() % QLatin1Char('\n'); if (package->installedVersion().isEmpty()) { pinDocument += QLatin1String("Pin: version 0.0\n"); } else { - pinDocument += QLatin1Literal("Pin: version ") % package->installedVersion() + pinDocument += QLatin1String("Pin: version ") % package->installedVersion() % QLatin1Char('\n'); } // Make configurable? pinDocument += QLatin1String("Pin-Priority: 1001\n\n"); } else { QDir logDirectory(dir); QStringList pinFiles = logDirectory.entryList(QDir::Files, QDir::Name); pinFiles << QString::fromStdString(_config->FindDir("Dir::Etc")) % QLatin1String("preferences"); // Search all pin files, delete package stanza from file for (const QString &pinName : pinFiles) { QString pinPath; if (!pinName.startsWith(QLatin1Char('/'))) { pinPath = dir % pinName; } else { pinPath = pinName; } if (!QFile::exists(pinPath)) continue; // Open to get a file name QTemporaryFile tempFile; if (!tempFile.open()) { return false; } tempFile.close(); QString tempFileName = tempFile.fileName(); FileFd out(tempFileName.toUtf8().toStdString(), FileFd::WriteOnly|FileFd::Create|FileFd::Empty); if (!out.IsOpen()) { return false; } FileFd Fd(pinPath.toUtf8().data(), FileFd::ReadOnly); pkgTagFile tagFile(&Fd); if (_error->PendingError()) { return false; } pkgTagSection tags; while (tagFile.Step(tags)) { QString name = QLatin1String(tags.FindS("Package").c_str()); if (name.isEmpty()) { return false; } // Include all but the matching name in the new pinfile if (name != package->name()) { tags.Write(out, TFRewritePackageOrder, {}); out.Write("\n", 1); } } out.Close(); if (!tempFile.open()) { return false; } pinDocument = tempFile.readAll(); } } if (!d->worker->writeFileToDisk(pinDocument, path)) { return false; } return true; } void Backend::updateXapianIndex() { QDBusMessage m = QDBusMessage::createMethodCall(QLatin1String("org.debian.AptXapianIndex"), QLatin1String("/"), QLatin1String("org.debian.AptXapianIndex"), QLatin1String("update_async")); QVariantList dbusArgs; dbusArgs << /*force*/ true << /*update_only*/ true; m.setArguments(dbusArgs); QDBusConnection::systemBus().send(m); QDBusConnection::systemBus().connect(QLatin1String("org.debian.AptXapianIndex"), QLatin1String("/"), QLatin1String("org.debian.AptXapianIndex"), QLatin1String("UpdateProgress"), this, SIGNAL(xapianUpdateProgress(int))); QDBusConnection::systemBus().connect(QLatin1String("org.debian.AptXapianIndex"), QLatin1String("/"), QLatin1String("org.debian.AptXapianIndex"), QLatin1String("UpdateFinished"), this, SLOT(emitXapianUpdateFinished())); emit xapianUpdateStarted(); } void Backend::emitXapianUpdateFinished() { QDBusConnection::systemBus().disconnect(QLatin1String("org.debian.AptXapianIndex"), QLatin1String("/"), QLatin1String("org.debian.AptXapianIndex"), QLatin1String("UpdateProgress"), this, SIGNAL(xapianUpdateProgress(int))); QDBusConnection::systemBus().disconnect(QLatin1String("org.debian.AptXapianIndex"), QLatin1String("/"), QLatin1String("org.debian.AptXapianIndex"), QLatin1String("UpdateFinished"), this, SLOT(xapianUpdateFinished(bool))); openXapianIndex(); emit xapianUpdateFinished(); } bool Backend::addArchiveToCache(const DebFile &archive) { Q_D(Backend); // Sanity checks Package *pkg = package(archive.packageName()); if (!pkg) { // The package is not in the cache, so we can't do anything // with this .deb return false; } QString arch = archive.architecture(); if (arch != QLatin1String("all") && arch != d->config->readEntry(QLatin1String("APT::Architecture"), QString())) { // Incompatible architecture return false; } QString debVersion = archive.version(); QString candVersion = pkg->availableVersion(); if (debVersion != candVersion) { // Incompatible version return false; } if (archive.md5Sum() != pkg->md5Sum()) { // Not the same as the candidate return false; } // Add the package, but we'll need auth so the worker'll do it return d->worker->copyArchiveToCache(archive.filePath()); } void Backend::setFrontendCaps(FrontendCaps caps) { Q_D(Backend); d->frontendCaps = caps; } } diff --git a/src/debfile.cpp b/src/debfile.cpp index c78865e..5c0134f 100644 --- a/src/debfile.cpp +++ b/src/debfile.cpp @@ -1,384 +1,384 @@ /*************************************************************************** * Copyright © 2011 Jonathan Thomas * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * * published by the Free Software Foundation; either version 2 of * * the License or (at your option) version 3 or any later version * * accepted by the membership of KDE e.V. (or its successor approved * * by the membership of KDE e.V.), which shall act as a proxy * * defined in Section 14 of version 3 of the license. * * * * 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 General Public License * * along with this program. If not, see . * ***************************************************************************/ #include "debfile.h" #include #include #include #include // Must be before APT_PKG_ABI checks! #include #include #include #if APT_PKG_ABI >= 600 #include #else #include #endif #include #include namespace QApt { class DebFilePrivate { public: DebFilePrivate(const QString &path) : isValid(false) , filePath(path) , extractor(0) { init(); } ~DebFilePrivate() { delete extractor; } bool isValid; QString filePath; debDebFile::MemControlExtract *extractor; pkgTagSection *controlData; void init(); }; void DebFilePrivate::init() { FileFd in(filePath.toUtf8().data(), FileFd::ReadOnly); debDebFile deb(in); // Extract control data try { extractor = new debDebFile::MemControlExtract("control"); if(!extractor->Read(deb)) { return; // not valid. } else { isValid = true; } } catch (...) { // MemControlExtract likes to throw out of range exceptions when it // encounters an invalid file. Catch those to prevent the application // from exploding. return; } controlData = &extractor->Section; } DebFile::DebFile(const QString &filePath) : d(new DebFilePrivate(filePath)) { } DebFile::~DebFile() { delete d; } bool DebFile::isValid() const { return d->isValid; } QString DebFile::filePath() const { return d->filePath; } QString DebFile::packageName() const { return QString::fromStdString(d->controlData->FindS("Package")); } QString DebFile::sourcePackage() const { return QString::fromStdString(d->controlData->FindS("Source")); } QString DebFile::version() const { return QString::fromStdString(d->controlData->FindS("Version")); } QString DebFile::architecture() const { return QString::fromStdString(d->controlData->FindS("Architecture")); } QString DebFile::maintainer() const { return QString::fromStdString(d->controlData->FindS("Maintainer")); } QString DebFile::section() const { return QString::fromStdString(d->controlData->FindS("Section")); } QString DebFile::priority() const { return QString::fromStdString(d->controlData->FindS("Priority")); } QString DebFile::homepage() const { return QString::fromStdString(d->controlData->FindS("Homepage")); } QString DebFile::longDescription() const { QString rawDescription = QLatin1String(d->controlData->FindS("Description").c_str()); // Remove short description rawDescription.remove(shortDescription() + '\n'); QString parsedDescription; // Split at double newline, by "section" QStringList sections = rawDescription.split(QLatin1String("\n .")); for (int i = 0; i < sections.count(); ++i) { sections[i].replace(QRegExp(QLatin1String("\n( |\t)+(-|\\*)")), - QLatin1Literal("\n\r ") % QString::fromUtf8("\xE2\x80\xA2")); + QLatin1String("\n\r ") % QString::fromUtf8("\xE2\x80\xA2")); // There should be no new lines within a section. sections[i].remove(QLatin1Char('\n')); // Hack to get the lists working again. sections[i].replace(QLatin1Char('\r'), QLatin1Char('\n')); // Merge multiple whitespace chars into one sections[i].replace(QRegExp(QLatin1String("\\ \\ +")), QChar::fromLatin1(' ')); // Remove the initial whitespace sections[i].remove(0, 1); // Append to parsedDescription if (sections[i].startsWith(QLatin1String("\n ") % QString::fromUtf8("\xE2\x80\xA2 ")) || !i) { parsedDescription += sections[i]; } else { - parsedDescription += QLatin1Literal("\n\n") % sections[i]; + parsedDescription += QLatin1String("\n\n") % sections[i]; } } return parsedDescription; } QString DebFile::shortDescription() const { QString longDesc = QLatin1String(d->controlData->FindS("Description").c_str()); return longDesc.left(longDesc.indexOf(QLatin1Char('\n'))); } QString DebFile::controlField(const QLatin1String &field) const { return QString::fromStdString(d->controlData->FindS(field.latin1())); } QString DebFile::controlField(const QString &field) const { return controlField(QLatin1String(field.toLatin1())); } QByteArray DebFile::md5Sum() const { FileFd in(d->filePath.toStdString(), FileFd::ReadOnly); debDebFile deb(in); #if APT_PKG_ABI >= 600 Hashes debMD5(Hashes::MD5SUM); #else MD5Summation debMD5; #endif in.Seek(0); debMD5.AddFD(in.Fd(), in.Size()); #if APT_PKG_ABI >= 600 return QByteArray::fromStdString(debMD5.GetHashString(Hashes::MD5SUM).HashValue()); #else return QByteArray::fromStdString(debMD5.Result().Value()); #endif } QStringList DebFile::fileList() const { QTemporaryFile tempFile; if (!tempFile.open()) { return QStringList(); } QString tempFileName = tempFile.fileName(); QProcess dpkg; QProcess tar; // dpkg --fsys-tarfile filename QString program = QLatin1String("dpkg --fsys-tarfile ") + d->filePath; dpkg.setStandardOutputFile(tempFileName); dpkg.start(program); dpkg.waitForFinished(); QString program2 = QLatin1String("tar -tf ") + tempFileName; tar.start(program2); tar.waitForFinished(); QString files = tar.readAllStandardOutput(); QStringList filesList = files.split('\n'); filesList.removeFirst(); // First entry is the "./" entry filesList.removeAll(QLatin1String("")); // Remove empty entries // Remove non-file directory listings for (int i = 0; i < filesList.size() - 1; ++i) { if (filesList.at(i+1).contains(filesList.at(i))) { filesList[i] = QString(QLatin1Char(' ')); } filesList.removeAll(QChar::fromLatin1(' ')); } return filesList; } QStringList DebFile::iconList() const { QStringList fileNames = fileList(); QStringList iconsList; foreach (const QString &fileName, fileNames) { if (fileName.startsWith(QLatin1String("./usr/share/icons"))) { iconsList << fileName; } } // XPM as a fallback. It's really not pretty when scaled up if (iconsList.isEmpty()) { foreach (const QString &fileName, fileNames) { if (fileName.startsWith(QLatin1String("./usr/share/pixmaps"))) { iconsList << fileName; } } } return iconsList; } QList DebFile::depends() const { return DependencyInfo::parseDepends(QString::fromStdString(d->controlData->FindS("Depends")), Depends); } QList DebFile::preDepends() const { return DependencyInfo::parseDepends(QString::fromStdString(d->controlData->FindS("Pre-Depends")), PreDepends); } QList DebFile::suggests() const { return DependencyInfo::parseDepends(QString::fromStdString(d->controlData->FindS("Suggests")), Suggests); } QList DebFile::recommends() const { return DependencyInfo::parseDepends(QString::fromStdString(d->controlData->FindS("Recommends")), Recommends); } QList DebFile::conflicts() const { return DependencyInfo::parseDepends(QString::fromStdString(d->controlData->FindS("Conflicts")), Conflicts); } QList DebFile::replaces() const { return DependencyInfo::parseDepends(QString::fromStdString(d->controlData->FindS("Replaces")), Replaces); } QList DebFile::obsoletes() const { return DependencyInfo::parseDepends(QString::fromStdString(d->controlData->FindS("Obsoletes")), Obsoletes); } QList DebFile::breaks() const { return DependencyInfo::parseDepends(QString::fromStdString(d->controlData->FindS("Breaks")), Breaks); } QList DebFile::enhances() const { return DependencyInfo::parseDepends(QString::fromStdString(d->controlData->FindS("Enhance")), Enhances); } qint64 DebFile::installedSize() const { QString sizeString = QLatin1String(d->controlData->FindS("Installed-Size").c_str()); return sizeString.toLongLong(); } bool DebFile::extractArchive(const QString &basePath) const { // The deb extractor extracts to the working path. QString oldCurrent = QDir::currentPath(); if (!basePath.isEmpty()) { QDir::setCurrent(basePath); } FileFd in(d->filePath.toStdString(), FileFd::ReadOnly); debDebFile deb(in); pkgDirStream stream; bool res = deb.ExtractArchive(stream); // Restore working path once we are done if (!basePath.isEmpty()) { QDir::setCurrent(oldCurrent); } return res; } bool DebFile::extractFileFromArchive(const QString &fileName, const QString &destination) const { QTemporaryFile tempFile; if (!tempFile.open()) { return false; } QString tempFileName = tempFile.fileName(); // dpkg --fsys-tarfile filename QString program = QLatin1String("dpkg --fsys-tarfile ") + d->filePath; QProcess dpkg; dpkg.setStandardOutputFile(tempFileName); dpkg.start(program); dpkg.waitForFinished(); - QString program2 = QLatin1Literal("tar -xf") % tempFileName % - QLatin1Literal(" -C ") % destination % ' ' % fileName; + QString program2 = QLatin1String("tar -xf") % tempFileName % + QLatin1String(" -C ") % destination % ' ' % fileName; QProcess tar; tar.start(program2); tar.waitForFinished(); return !tar.exitCode(); } } diff --git a/src/package.cpp b/src/package.cpp index ab7ad1b..8cd844f 100644 --- a/src/package.cpp +++ b/src/package.cpp @@ -1,1353 +1,1353 @@ /*************************************************************************** * Copyright © 2010-2011 Jonathan Thomas * * Heavily inspired by Synaptic library code ;-) * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * * published by the Free Software Foundation; either version 2 of * * the License or (at your option) version 3 or any later version * * accepted by the membership of KDE e.V. (or its successor approved * * by the membership of KDE e.V.), which shall act as a proxy * * defined in Section 14 of version 3 of the license. * * * * 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 General Public License * * along with this program. If not, see . * ***************************************************************************/ //krazy:excludeall=qclasses // Qt-only library, so things like QUrl *should* be used #include "package.h" // Qt includes #include #include #include #include #include #include #include // Apt includes #include #include #include #include #include #include #include #include #include #include #include #include #include #include // Own includes #include "backend.h" #include "cache.h" #include "config.h" // krazy:exclude=includes #include "markingerrorinfo.h" namespace QApt { class PackagePrivate { public: PackagePrivate(pkgCache::PkgIterator iter, Backend *back) : packageIter(iter) , backend(back) , state(0) , staticStateCalculated(false) , foreignArchCalculated(false) , isInUpdatePhase(false) , inUpdatePhaseCalculated(false) { } ~PackagePrivate() { } pkgCache::PkgIterator packageIter; QApt::Backend *backend; int state; bool staticStateCalculated; bool isForeignArch; bool foreignArchCalculated; bool isInUpdatePhase; bool inUpdatePhaseCalculated; pkgCache::PkgFileIterator searchPkgFileIter(QLatin1String label, const QString &release) const; // Calculate state flags that cannot change void initStaticState(const pkgCache::VerIterator &ver, pkgDepCache::StateCache &stateCache); bool setInUpdatePhase(bool inUpdatePhase); }; pkgCache::PkgFileIterator PackagePrivate::searchPkgFileIter(QLatin1String label, const QString &release) const { pkgCache::VerIterator verIter = packageIter.VersionList(); pkgCache::VerFileIterator verFileIter; pkgCache::PkgFileIterator found; while (!verIter.end()) { for (verFileIter = verIter.FileList(); !verFileIter.end(); ++verFileIter) { for(found = verFileIter.File(); !found.end(); ++found) { const char *verLabel = found.Label(); const char *verOrigin = found.Origin(); const char *verArchive = found.Archive(); if (verLabel && verOrigin && verArchive) { if(verLabel == label && verOrigin == label && QLatin1String(verArchive) == release) { return found; } } } } ++verIter; } found = pkgCache::PkgFileIterator(*packageIter.Cache()); return found; } void PackagePrivate::initStaticState(const pkgCache::VerIterator &ver, pkgDepCache::StateCache &stateCache) { int packageState = 0; if (!ver.end()) { packageState |= QApt::Package::Installed; if (stateCache.CandidateVer && stateCache.Upgradable()) { packageState |= QApt::Package::Upgradeable; } } else { packageState |= QApt::Package::NotInstalled; } // Broken/garbage statuses are constant until a cache reload if (stateCache.NowBroken()) { packageState |= QApt::Package::NowBroken; } if (stateCache.InstBroken()) { packageState |= QApt::Package::InstallBroken; } if (stateCache.Garbage) { packageState |= QApt::Package::IsGarbage; } if (stateCache.NowPolicyBroken()) { packageState |= QApt::Package::NowPolicyBroken; } if (stateCache.InstPolicyBroken()) { packageState |= QApt::Package::InstallPolicyBroken; } // Essential/important status can only be changed by cache reload if (packageIter->Flags & (pkgCache::Flag::Important | pkgCache::Flag::Essential)) { packageState |= QApt::Package::IsImportant; } if (packageIter->CurrentState == pkgCache::State::ConfigFiles) { packageState |= QApt::Package::ResidualConfig; } // Packages will stay undownloadable until a sources file is refreshed // and the cache is reloaded. bool downloadable = true; if (!stateCache.CandidateVer || !stateCache.CandidateVerIter(*backend->cache()->depCache()).Downloadable()) downloadable = false; if (!downloadable) packageState |= QApt::Package::NotDownloadable; state |= packageState; staticStateCalculated = true; } bool PackagePrivate::setInUpdatePhase(bool inUpdatePhase) { inUpdatePhaseCalculated = true; isInUpdatePhase = inUpdatePhase; return inUpdatePhase; } Package::Package(QApt::Backend* backend, pkgCache::PkgIterator &packageIter) : d(new PackagePrivate(packageIter, backend)) { } Package::~Package() { delete d; } const pkgCache::PkgIterator &Package::packageIterator() const { return d->packageIter; } QLatin1String Package::name() const { return QLatin1String(d->packageIter.Name()); } int Package::id() const { return d->packageIter->ID; } QLatin1String Package::section() const { const pkgCache::VerIterator &ver = (*d->backend->cache()->depCache()).GetCandidateVersion(d->packageIter); if (!ver.end()) return QLatin1String(ver.Section()); return QLatin1String(""); } QString Package::sourcePackage() const { QString sourcePackage; // In the APT package record format, the only time when a "Source:" field // is present is when the binary package name doesn't match the source // name const pkgCache::VerIterator &ver = (*d->backend->cache()->depCache()).GetCandidateVersion(d->packageIter); if (!ver.end()) { pkgRecords::Parser &rec = d->backend->records()->Lookup(ver.FileList()); sourcePackage = QString::fromStdString(rec.SourcePkg()); } // If the package record didn't have a "Source:" field, then this package's // name must be the source package's name. (Or there isn't a record for this package) if (sourcePackage.isEmpty()) { sourcePackage = name(); } return sourcePackage; } QString Package::shortDescription() const { QString shortDescription; const pkgCache::VerIterator &ver = (*d->backend->cache()->depCache()).GetCandidateVersion(d->packageIter); if (!ver.end()) { pkgCache::DescIterator Desc = ver.TranslatedDescription(); pkgRecords::Parser & parser = d->backend->records()->Lookup(Desc.FileList()); shortDescription = QString::fromUtf8(parser.ShortDesc().data()); return shortDescription; } return shortDescription; } QString Package::longDescription() const { const pkgCache::VerIterator &ver = (*d->backend->cache()->depCache()).GetCandidateVersion(d->packageIter); if (!ver.end()) { QString rawDescription; pkgCache::DescIterator Desc = ver.TranslatedDescription(); pkgRecords::Parser & parser = d->backend->records()->Lookup(Desc.FileList()); rawDescription = QString::fromUtf8(parser.LongDesc().data()); // Apt acutally returns the whole description, we just want the // extended part. rawDescription.remove(shortDescription() % '\n'); // *Now* we're really raw. Sort of. ;) QString parsedDescription; // Split at double newline, by "section" QStringList sections = rawDescription.split(QLatin1String("\n .")); for (int i = 0; i < sections.count(); ++i) { sections[i].replace(QRegExp(QLatin1String("\n( |\t)+(-|\\*)")), - QLatin1Literal("\n\r ") % QString::fromUtf8("\xE2\x80\xA2")); + QLatin1String("\n\r ") % QString::fromUtf8("\xE2\x80\xA2")); // There should be no new lines within a section. sections[i].remove(QLatin1Char('\n')); // Hack to get the lists working again. sections[i].replace(QLatin1Char('\r'), QLatin1Char('\n')); // Merge multiple whitespace chars into one sections[i].replace(QRegExp(QLatin1String("\\ \\ +")), QChar::fromLatin1(' ')); // Remove the initial whitespace if (sections[i].startsWith(QChar::Space)) { sections[i].remove(0, 1); } // Append to parsedDescription if (sections[i].startsWith(QLatin1String("\n ") % QString::fromUtf8("\xE2\x80\xA2 ")) || !i) { parsedDescription += sections[i]; } else { - parsedDescription += QLatin1Literal("\n\n") % sections[i]; + parsedDescription += QLatin1String("\n\n") % sections[i]; } } return parsedDescription; } return QString(); } QString Package::maintainer() const { QString maintainer; const pkgCache::VerIterator &ver = (*d->backend->cache()->depCache()).GetCandidateVersion(d->packageIter); if (!ver.end()) { pkgRecords::Parser &parser = d->backend->records()->Lookup(ver.FileList()); maintainer = QString::fromUtf8(parser.Maintainer().data()); // This replacement prevents frontends from interpreting '<' as // an HTML tag opening maintainer.replace(QLatin1Char('<'), QLatin1String("<")); } return maintainer; } QString Package::homepage() const { QString homepage; const pkgCache::VerIterator &ver = (*d->backend->cache()->depCache()).GetCandidateVersion(d->packageIter); if (!ver.end()) { pkgRecords::Parser &parser = d->backend->records()->Lookup(ver.FileList()); homepage = QString::fromUtf8(parser.Homepage().data()); } return homepage; } QString Package::version() const { if (!d->packageIter->CurrentVer) { pkgDepCache::StateCache &State = (*d->backend->cache()->depCache())[d->packageIter]; if (!State.CandidateVer) { return QString(); } else { return QLatin1String(State.CandidateVerIter(*d->backend->cache()->depCache()).VerStr()); } } else { return QLatin1String(d->packageIter.CurrentVer().VerStr()); } } QString Package::upstreamVersion() const { const char *ver; if (!d->packageIter->CurrentVer) { pkgDepCache::StateCache &State = (*d->backend->cache()->depCache())[d->packageIter]; if (!State.CandidateVer) { return QString(); } else { ver = State.CandidateVerIter(*d->backend->cache()->depCache()).VerStr(); } } else { ver = d->packageIter.CurrentVer().VerStr(); } return QString::fromStdString(_system->VS->UpstreamVersion(ver)); } QString Package::upstreamVersion(const QString &version) { QByteArray ver = version.toLatin1(); return QString::fromStdString(_system->VS->UpstreamVersion(ver.constData())); } QString Package::architecture() const { pkgDepCache *depCache = d->backend->cache()->depCache(); pkgCache::VerIterator ver = (*depCache)[d->packageIter].InstVerIter(*depCache); // the arch:all property is part of the version if (ver && ver.Arch()) return QLatin1String(ver.Arch()); return QLatin1String(d->packageIter.Arch()); } QStringList Package::availableVersions() const { QStringList versions; // Get available Versions. for (auto Ver = d->packageIter.VersionList(); !Ver.end(); ++Ver) { // We always take the first available version. pkgCache::VerFileIterator VF = Ver.FileList(); if (VF.end()) continue; pkgCache::PkgFileIterator File = VF.File(); // Files without an archive will have a site QString archive = File.Archive() ? QLatin1String(File.Archive()) : QLatin1String(File.Site()); versions.append(QLatin1String(Ver.VerStr()) % QLatin1String(" (") % archive % ')'); } return versions; } QString Package::installedVersion() const { if (!d->packageIter->CurrentVer) { return QString(); } return QLatin1String(d->packageIter.CurrentVer().VerStr()); } QString Package::availableVersion() const { pkgDepCache::StateCache &State = (*d->backend->cache()->depCache())[d->packageIter]; if (!State.CandidateVer) { return QString(); } return QLatin1String(State.CandidateVerIter(*d->backend->cache()->depCache()).VerStr()); } QString Package::priority() const { const pkgCache::VerIterator &ver = (*d->backend->cache()->depCache()).GetCandidateVersion(d->packageIter); if (ver.end()) return QString(); return QLatin1String(ver.PriorityType()); } QStringList Package::installedFilesList() const { QStringList installedFilesList; QString path = QLatin1String("/var/lib/dpkg/info/") % name() % QLatin1String(".list"); // Fallback for multiarch packages if (!QFile::exists(path)) { path = QLatin1String("/var/lib/dpkg/info/") % name() % ':' % architecture() % QLatin1String(".list"); } QFile infoFile(path); if (infoFile.open(QFile::ReadOnly)) { QTextStream stream(&infoFile); QString line; do { line = stream.readLine(); installedFilesList << line; } while (!line.isNull()); // The first item won't be a file installedFilesList.removeFirst(); // Remove non-file directory listings for (int i = 0; i < installedFilesList.size() - 1; ++i) { if (installedFilesList.at(i+1).contains(installedFilesList.at(i) + '/')) { installedFilesList[i] = QString(QLatin1Char(' ')); } } installedFilesList.removeAll(QChar::fromLatin1(' ')); // Last line is empty for some reason... if (!installedFilesList.isEmpty()) { installedFilesList.removeLast(); } } return installedFilesList; } QString Package::origin() const { const pkgCache::VerIterator &Ver = (*d->backend->cache()->depCache()).GetCandidateVersion(d->packageIter); if(Ver.end()) return QString(); pkgCache::VerFileIterator VF = Ver.FileList(); return QString::fromUtf8(VF.File().Origin()); } QString Package::site() const { const pkgCache::VerIterator &Ver = (*d->backend->cache()->depCache()).GetCandidateVersion(d->packageIter); if(Ver.end()) return QString(); pkgCache::VerFileIterator VF = Ver.FileList(); return QString::fromUtf8(VF.File().Site()); } QStringList Package::archives() const { const pkgCache::VerIterator &Ver = (*d->backend->cache()->depCache()).GetCandidateVersion(d->packageIter); if(Ver.end()) return QStringList(); QStringList archiveList; for (auto VF = Ver.FileList(); !VF.end(); ++VF) archiveList << QLatin1String(VF.File().Archive()); return archiveList; } QString Package::component() const { QString sect = section(); if(sect.isEmpty()) return QString(); QStringList split = sect.split('/'); if (split.count() > 1) return split.first(); return QString("main"); } QByteArray Package::md5Sum() const { const pkgCache::VerIterator &ver = (*d->backend->cache()->depCache()).GetCandidateVersion(d->packageIter); if(ver.end()) return QByteArray(); pkgRecords::Parser &rec = d->backend->records()->Lookup(ver.FileList()); auto MD5HashString = rec.Hashes().find("MD5Sum"); return MD5HashString ? QByteArray::fromStdString(MD5HashString->HashValue()) : ""; } QUrl Package::changelogUrl() const { const pkgCache::VerIterator &ver = (*d->backend->cache()->depCache()).GetCandidateVersion(d->packageIter); if (ver.end()) return QUrl(); const QString url = QString::fromStdString(pkgAcqChangelog::URI(ver)); // pkgAcqChangelog::URI(ver) may return URIs with schemes other than http(s) // e.g. copy:// gzip:// for local files. We exclude them for backward // compatibility with libQApt <= 3.0.3. if (!url.startsWith("http")) return QUrl(); return QUrl(url); } QUrl Package::screenshotUrl(QApt::ScreenshotType type) const { QUrl url; switch (type) { case QApt::Thumbnail: url = QUrl(controlField(QLatin1String("Thumbnail-Url"))); if(url.isEmpty()) url = QUrl("http://screenshots.debian.net/thumbnail/" % name()); break; case QApt::Screenshot: url = QUrl(controlField(QLatin1String("Screenshot-Url"))); if(url.isEmpty()) url = QUrl("http://screenshots.debian.net/screenshot/" % name()); break; default: qDebug() << "I do not know how to handle the screenshot type given to me: " << QString::number(type); } return url; } QDateTime Package::supportedUntil() const { if (!isSupported()) { return QDateTime(); } QDateTime releaseDate = d->backend->releaseDate(); if (!releaseDate.isValid()) { return QDateTime(); } // Default to 18m in case the package has no "supported" field QString supportTimeString = QLatin1String("18m"); QString supportTimeField = controlField(QLatin1String("Supported")); if (!supportTimeField.isEmpty()) { supportTimeString = supportTimeField; } QChar unit = supportTimeString.at(supportTimeString.length() - 1); supportTimeString.chop(1); // Remove the letter signifying months/years const int supportTime = supportTimeString.toInt(); QDateTime supportEnd; if (unit == QLatin1Char('m')) { supportEnd = releaseDate.addMonths(supportTime); } else if (unit == QLatin1Char('y')) { supportEnd = releaseDate.addYears(supportTime); } return supportEnd; } QString Package::controlField(QLatin1String name) const { const pkgCache::VerIterator &ver = (*d->backend->cache()->depCache()).GetCandidateVersion(d->packageIter); if (ver.end()) { return QString(); } pkgRecords::Parser &rec = d->backend->records()->Lookup(ver.FileList()); return QString::fromStdString(rec.RecordField(name.latin1())); } QString Package::controlField(const QString &name) const { return controlField(QLatin1String(name.toLatin1())); } qint64 Package::currentInstalledSize() const { const pkgCache::VerIterator &ver = d->packageIter.CurrentVer(); if (!ver.end()) { return qint64(ver->InstalledSize); } else { return qint64(-1); } } qint64 Package::availableInstalledSize() const { pkgDepCache::StateCache &State = (*d->backend->cache()->depCache())[d->packageIter]; if (!State.CandidateVer) { return qint64(-1); } return qint64(State.CandidateVerIter(*d->backend->cache()->depCache())->InstalledSize); } qint64 Package::installedSize() const { const pkgCache::VerIterator &ver = d->packageIter.CurrentVer(); if (!ver.end()) { return qint64(ver->InstalledSize); } return availableInstalledSize(); } qint64 Package::downloadSize() const { pkgDepCache::StateCache &State = (*d->backend->cache()->depCache())[d->packageIter]; if (!State.CandidateVer) { return qint64(-1); } return qint64(State.CandidateVerIter(*d->backend->cache()->depCache())->Size); } int Package::state() const { int packageState = 0; const pkgCache::VerIterator &ver = d->packageIter.CurrentVer(); pkgDepCache::StateCache &stateCache = (*d->backend->cache()->depCache())[d->packageIter]; if (!d->staticStateCalculated) { d->initStaticState(ver, stateCache); } if (stateCache.Install()) { packageState |= ToInstall; } if (stateCache.Flags & pkgCache::Flag::Auto) { packageState |= QApt::Package::IsAuto; } if (stateCache.iFlags & pkgDepCache::ReInstall) { packageState |= ToReInstall; } else if (stateCache.NewInstall()) { // Order matters here. packageState |= NewInstall; } else if (stateCache.Upgrade()) { packageState |= ToUpgrade; } else if (stateCache.Downgrade()) { packageState |= ToDowngrade; } else if (stateCache.Delete()) { packageState |= ToRemove; if (stateCache.iFlags & pkgDepCache::Purge) { packageState |= ToPurge; } } else if (stateCache.Keep()) { packageState |= ToKeep; if (stateCache.Held()) { packageState |= QApt::Package::Held; } } return packageState | d->state; } int Package::staticState() const { if (!d->staticStateCalculated) { const pkgCache::VerIterator &ver = d->packageIter.CurrentVer(); pkgDepCache::StateCache &stateCache = (*d->backend->cache()->depCache())[d->packageIter]; d->initStaticState(ver, stateCache); } return d->state; } int Package::compareVersion(const QString &v1, const QString &v2) { // Make deep copies of toStdString(), since otherwise they would // go out of scope when we call c_str() std::string s1 = v1.toStdString(); std::string s2 = v2.toStdString(); const char *a = s1.c_str(); const char *b = s2.c_str(); int lenA = strlen(a); int lenB = strlen(b); return _system->VS->DoCmpVersion(a, a+lenA, b, b+lenB); } bool Package::isInstalled() const { return !d->packageIter.CurrentVer().end(); } bool Package::isSupported() const { if (origin() == QLatin1String("Ubuntu")) { QString componentString = component(); if ((componentString == QLatin1String("main") || componentString == QLatin1String("restricted")) && isTrusted()) { return true; } } return false; } bool Package::isInUpdatePhase() const { if (!(state() & Package::Upgradeable)) { return false; } // Try to use the cached values, otherwise we have to do the calculation. if (d->inUpdatePhaseCalculated) { return d->isInUpdatePhase; } bool intConversionOk = true; int phasedUpdatePercent = controlField(QLatin1String("Phased-Update-Percentage")).toInt(&intConversionOk); if (!intConversionOk) { // Upgradable but either the phase percent field is not there at all // or it has a non-convertable value. // In either case this package is good for upgrade. return d->setInUpdatePhase(true); } // This is a more or less an exact reimplementation of the update phasing // algorithm Ubuntu uses. // Deciding whether a machine is in the phasing pool or not happens in // two steps. // 1. repeatable random number generation between 0..100 // 2. comparison of random number with phasing percentage and marking // as upgradable if rand is greater than the phasing. // Repeatable discrete random number generation is based on // the MD5 hash of "sourcename-sourceversion-dbusmachineid", this // hash is used as seed for the random number generator to provide // stable randomness based on the stable seed. Combined with the discrete // quasi-randomiziation we get about even distribution of machines across // phases. static QString machineId; if (machineId.isNull()) { QFile file(QStringLiteral("/var/lib/dbus/machine-id")); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { machineId = file.readLine().trimmed(); } } if (machineId.isEmpty()) { // Without machineId we cannot differentiate one machine from another, so // we have no way to build a unique hash. return true; // Don't change cache as we might have more luck next time. } QString seedString = QStringLiteral("%1-%2-%3").arg(sourcePackage(), availableVersion(), machineId); QByteArray seed = QCryptographicHash::hash(seedString.toLatin1(), QCryptographicHash::Md5); // MD5 would be 128bits, that's two quint64 stdlib random default_engine // uses a uint32 seed though, so we'd loose precision anyway, so screw // this, we'll get the first 32bit and screw the rest! This is not // rocket science, worst case the update only arrives once the phasing // tag is removed. seed = seed.toHex(); seed.truncate(8 /* each character in a hex string values 4 bit, 8*4=32bit */); bool ok = false; uint a = seed.toUInt(&ok, 16); Q_ASSERT(ok); // Hex conversion always is supposed to work at this point. std::default_random_engine generator(a); std::uniform_int_distribution distribution(0, 100); int rand = distribution(generator); // rand is the percentage at which the machine starts to be in the phase. // Once rand is less than the phasing percentage e.g. 40rand vs. 50phase // the machine is supposed to start phasing. return d->setInUpdatePhase(rand <= phasedUpdatePercent); } bool Package::isMultiArchDuplicate() const { // Excludes installed packages, which are always "interesting" if (isInstalled()) return false; // Otherwise, check if the pkgIterator is the "best" from its group return (d->packageIter.Group().FindPkg() != d->packageIter); } QString Package::multiArchTypeString() const { return controlField(QLatin1String("Multi-Arch")); } MultiArchType Package::multiArchType() const { QString typeString = multiArchTypeString(); MultiArchType archType = InvalidMultiArchType; if (typeString == QLatin1String("same")) archType = MultiArchSame; else if (typeString == QLatin1String("foreign")) archType = MultiArchForeign; else if (typeString == QLatin1String("allowed")) archType = MultiArchAllowed; return archType; } bool Package::isForeignArch() const { if (!d->foreignArchCalculated) { QString arch = architecture(); d->isForeignArch = (d->backend->nativeArchitecture() != arch) & (arch != QLatin1String("all")); d->foreignArchCalculated = true; } return d->isForeignArch; } QList Package::depends() const { return DependencyInfo::parseDepends(controlField("Depends"), Depends); } QList Package::preDepends() const { return DependencyInfo::parseDepends(controlField("Pre-Depends"), PreDepends); } QList Package::suggests() const { return DependencyInfo::parseDepends(controlField("Suggests"), Suggests); } QList Package::recommends() const { return DependencyInfo::parseDepends(controlField("Recommends"), Recommends); } QList Package::conflicts() const { return DependencyInfo::parseDepends(controlField("Conflicts"), Conflicts); } QList Package::replaces() const { return DependencyInfo::parseDepends(controlField("Replaces"), Replaces); } QList Package::obsoletes() const { return DependencyInfo::parseDepends(controlField("Obsoletes"), Obsoletes); } QList Package::breaks() const { return DependencyInfo::parseDepends(controlField("Breaks"), Breaks); } QList Package::enhances() const { return DependencyInfo::parseDepends(controlField("Enhance"), Enhances); } QStringList Package::dependencyList(bool useCandidateVersion) const { QStringList dependsList; pkgCache::VerIterator current; pkgDepCache::StateCache &State = (*d->backend->cache()->depCache())[d->packageIter]; if(!useCandidateVersion) { current = State.InstVerIter(*d->backend->cache()->depCache()); } if(useCandidateVersion || current.end()) { current = State.CandidateVerIter(*d->backend->cache()->depCache()); } // no information found if(current.end()) { return dependsList; } for(pkgCache::DepIterator D = current.DependsList(); D.end() != true; ++D) { QString type; bool isOr = false; bool isVirtual = false; QString name; QString version; QString versionCompare; QString finalString; // check target and or-depends status pkgCache::PkgIterator Trg = D.TargetPkg(); if ((D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or) { isOr=true; } // common information type = QString::fromUtf8(D.DepType()); name = QLatin1String(Trg.Name()); if (!Trg->VersionList) { isVirtual = true; } else { version = QLatin1String(D.TargetVer()); versionCompare = QLatin1String(D.CompType()); } - finalString = QLatin1Literal("") % type % QLatin1Literal(": "); + finalString = QLatin1String("") % type % QLatin1String(": "); if (isVirtual) { - finalString += QLatin1Literal("") % name % QLatin1Literal(""); + finalString += QLatin1String("") % name % QLatin1String(""); } else { finalString += name; } // Escape the compare operator so it won't be seen as HTML if (!version.isEmpty()) { QString compMarkup(versionCompare); compMarkup.replace(QLatin1Char('<'), QLatin1String("<")); finalString += QLatin1String(" (") % compMarkup % QLatin1Char(' ') % version % QLatin1Char(')'); } if (isOr) { finalString += QLatin1String(" |"); } dependsList.append(finalString); } return dependsList; } QStringList Package::requiredByList() const { QStringList reverseDependsList; for(pkgCache::DepIterator it = d->packageIter.RevDependsList(); !it.end(); ++it) { reverseDependsList << QLatin1String(it.ParentPkg().Name()); } return reverseDependsList; } QStringList Package::providesList() const { pkgDepCache::StateCache &State = (*d->backend->cache()->depCache())[d->packageIter]; if (!State.CandidateVer) { return QStringList(); } QStringList provides; for (pkgCache::PrvIterator Prv = State.CandidateVerIter(*d->backend->cache()->depCache()).ProvidesList(); !Prv.end(); ++Prv) { provides.append(QLatin1String(Prv.Name())); } return provides; } QStringList Package::recommendsList() const { QStringList recommends; const pkgCache::VerIterator &Ver = (*d->backend->cache()->depCache()).GetCandidateVersion(d->packageIter); if (Ver.end()) { return recommends; } for(pkgCache::DepIterator it = Ver.DependsList(); !it.end(); ++it) { pkgCache::PkgIterator pkg = it.TargetPkg(); // Skip purely virtual packages if (!pkg->VersionList) { continue; } pkgDepCache::StateCache &rState = (*d->backend->cache()->depCache())[pkg]; if (it->Type == pkgCache::Dep::Recommends && (rState.CandidateVer != 0 )) { recommends << QLatin1String(it.TargetPkg().Name()); } } return recommends; } QStringList Package::suggestsList() const { QStringList suggests; const pkgCache::VerIterator &Ver = (*d->backend->cache()->depCache()).GetCandidateVersion(d->packageIter); if (Ver.end()) { return suggests; } for(pkgCache::DepIterator it = Ver.DependsList(); !it.end(); ++it) { pkgCache::PkgIterator pkg = it.TargetPkg(); // Skip purely virtual packages if (!pkg->VersionList) { continue; } pkgDepCache::StateCache &sState = (*d->backend->cache()->depCache())[pkg]; if (it->Type == pkgCache::Dep::Suggests && (sState.CandidateVer != 0 )) { suggests << QLatin1String(it.TargetPkg().Name()); } } return suggests; } QStringList Package::enhancesList() const { QStringList enhances; const pkgCache::VerIterator &Ver = (*d->backend->cache()->depCache()).GetCandidateVersion(d->packageIter); if (Ver.end()) { return enhances; } for(pkgCache::DepIterator it = Ver.DependsList(); !it.end(); ++it) { pkgCache::PkgIterator pkg = it.TargetPkg(); // Skip purely virtual packages if (!pkg->VersionList) { continue; } pkgDepCache::StateCache &eState = (*d->backend->cache()->depCache())[pkg]; if (it->Type == pkgCache::Dep::Enhances && (eState.CandidateVer != 0 )) { enhances << QLatin1String(it.TargetPkg().Name()); } } return enhances; } QStringList Package::enhancedByList() const { QStringList enhancedByList; Q_FOREACH (QApt::Package *package, d->backend->availablePackages()) { if (package->enhancesList().contains(name())) { enhancedByList << package->name(); } } return enhancedByList; } QList Package::brokenReason() const { const pkgCache::VerIterator &Ver = (*d->backend->cache()->depCache()).GetCandidateVersion(d->packageIter); QList reasons; // check if there is actually something to install if (!Ver) { QApt::DependencyInfo info(name(), QString(), NoOperand, InvalidType); QApt::MarkingErrorInfo error(QApt::ParentNotInstallable, info); reasons.append(error); return reasons; } for (pkgCache::DepIterator D = Ver.DependsList(); !D.end();) { // Compute a single dependency element (glob or) pkgCache::DepIterator Start; pkgCache::DepIterator End; D.GlobOr(Start, End); pkgCache::PkgIterator Targ = Start.TargetPkg(); if (!d->backend->cache()->depCache()->IsImportantDep(End)) { continue; } if (((*d->backend->cache()->depCache())[End] & pkgDepCache::DepGInstall) == pkgDepCache::DepGInstall) { continue; } if (!Targ->ProvidesList) { // Ok, not a virtual package since no provides pkgCache::VerIterator Ver = (*d->backend->cache()->depCache())[Targ].InstVerIter(*d->backend->cache()->depCache()); QString requiredVersion; if(Start.TargetVer() != 0) { requiredVersion = '(' % QLatin1String(Start.CompType()) % QLatin1String(Start.TargetVer()) % ')'; } if (!Ver.end()) { // Happens when a package needs an upgraded dep, but the dep won't // upgrade. Example: // "apt 0.5.4 but 0.5.3 is to be installed" QString targetName = QLatin1String(Start.TargetPkg().Name()); QApt::DependencyType relation = (QApt::DependencyType)End->Type; QApt::DependencyInfo errorInfo(targetName, requiredVersion, NoOperand, relation); QApt::MarkingErrorInfo error(QApt::WrongCandidateVersion, errorInfo); reasons.append(error); } else { // We have the package, but for some reason it won't be installed // In this case, the required version does not exist at all QString targetName = QLatin1String(Start.TargetPkg().Name()); QApt::DependencyType relation = (QApt::DependencyType)End->Type; QApt::DependencyInfo errorInfo(targetName, requiredVersion, NoOperand, relation); QApt::MarkingErrorInfo error(QApt::DepNotInstallable, errorInfo); reasons.append(error); } } else { // Ok, candidate has provides. We're a virtual package QString targetName = QLatin1String(Start.TargetPkg().Name()); QApt::DependencyType relation = (QApt::DependencyType)End->Type; QApt::DependencyInfo errorInfo(targetName, QString(), NoOperand, relation); QApt::MarkingErrorInfo error(QApt::VirtualPackage, errorInfo); reasons.append(error); } } return reasons; } bool Package::isTrusted() const { const pkgCache::VerIterator &Ver = (*d->backend->cache()->depCache()).GetCandidateVersion(d->packageIter); if (!Ver) return false; pkgSourceList *Sources = d->backend->packageSourceList(); QHash *trustCache = d->backend->cache()->trustCache(); for (pkgCache::VerFileIterator i = Ver.FileList(); !i.end(); ++i) { pkgIndexFile *Index; //FIXME: Should be done in apt auto trustIter = trustCache->constBegin(); while (trustIter != trustCache->constEnd()) { if (trustIter.key() == i.File()) break; // Found it trustIter++; } // Find the index of the package file from the package sources if (trustIter == trustCache->constEnd()) { // Not found if (!Sources->FindIndex(i.File(), Index)) continue; } else Index = trustIter.value(); if (Index->IsTrusted()) return true; } return false; } bool Package::wouldBreak() const { int pkgState = state(); if ((pkgState & ToRemove) || (!(pkgState & Installed) && (pkgState & ToKeep))) { return false; } return pkgState & InstallBroken; } void Package::setAuto(bool flag) { d->backend->cache()->depCache()->MarkAuto(d->packageIter, flag); } void Package::setKeep() { d->backend->cache()->depCache()->MarkKeep(d->packageIter, false); if (state() & ToReInstall) { d->backend->cache()->depCache()->SetReInstall(d->packageIter, false); } if (d->backend->cache()->depCache()->BrokenCount() > 0) { pkgProblemResolver Fix(d->backend->cache()->depCache()); Fix.ResolveByKeep(); } d->state |= IsManuallyHeld; if (!d->backend->areEventsCompressed()) { d->backend->emitPackageChanged(); } } void Package::setInstall() { d->backend->cache()->depCache()->MarkInstall(d->packageIter, true); d->state &= ~IsManuallyHeld; // FIXME: can't we get rid of it here? // if there is something wrong, try to fix it if (!state() & ToInstall || d->backend->cache()->depCache()->BrokenCount() > 0) { pkgProblemResolver Fix(d->backend->cache()->depCache()); Fix.Clear(d->packageIter); Fix.Protect(d->packageIter); Fix.Resolve(true); } if (!d->backend->areEventsCompressed()) { d->backend->emitPackageChanged(); } } void Package::setReInstall() { d->backend->cache()->depCache()->SetReInstall(d->packageIter, true); d->state &= ~IsManuallyHeld; if (!d->backend->areEventsCompressed()) { d->backend->emitPackageChanged(); } } // TODO: merge into one function with bool_purge param void Package::setRemove() { pkgProblemResolver Fix(d->backend->cache()->depCache()); Fix.Clear(d->packageIter); Fix.Protect(d->packageIter); Fix.Remove(d->packageIter); d->backend->cache()->depCache()->SetReInstall(d->packageIter, false); d->backend->cache()->depCache()->MarkDelete(d->packageIter, false); Fix.Resolve(true); d->state &= ~IsManuallyHeld; if (!d->backend->areEventsCompressed()) { d->backend->emitPackageChanged(); } } void Package::setPurge() { pkgProblemResolver Fix(d->backend->cache()->depCache()); Fix.Clear(d->packageIter); Fix.Protect(d->packageIter); Fix.Remove(d->packageIter); d->backend->cache()->depCache()->SetReInstall(d->packageIter, false); d->backend->cache()->depCache()->MarkDelete(d->packageIter, true); Fix.Resolve(true); d->state &= ~IsManuallyHeld; if (!d->backend->areEventsCompressed()) { d->backend->emitPackageChanged(); } } bool Package::setVersion(const QString &version) { pkgDepCache::StateCache &state = (*d->backend->cache()->depCache())[d->packageIter]; QLatin1String defaultCandVer(state.CandVersion); bool isDefault = (version == defaultCandVer); pkgVersionMatch Match(version.toLatin1().constData(), pkgVersionMatch::Version); const pkgCache::VerIterator &Ver = Match.Find(d->packageIter); if (Ver.end()) return false; d->backend->cache()->depCache()->SetCandidateVersion(Ver); for (auto VF = Ver.FileList(); !VF.end(); ++VF) { if (!VF.File() || !VF.File().Archive()) continue; d->backend->cache()->depCache()->SetCandidateRelease(Ver, VF.File().Archive()); break; } if (isDefault) d->state &= ~OverrideVersion; else d->state |= OverrideVersion; return true; } void Package::setPinned(bool pin) { pin ? d->state |= IsPinned : d->state &= ~IsPinned; } }