diff --git a/src/kpackage/package.cpp b/src/kpackage/package.cpp index 7fa1b3b..cf7d015 100644 --- a/src/kpackage/package.cpp +++ b/src/kpackage/package.cpp @@ -1,1006 +1,1012 @@ /****************************************************************************** * Copyright 2007 by Aaron Seigo * * Copyright 2010 by Marco Martin * * Copyright 2010 by Kevin Ottens * * Copyright 2009 by Rob Scheepmaker * * * * This library is free software; you can redistribute it and/or * * modify it under the terms of the GNU Library General Public * * License as published by the Free Software Foundation; either * * version 2 of the License, or (at your option) any later version. * * * * This library 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 * * Library General Public License for more details. * * * * You should have received a copy of the GNU Library General Public License * * along with this library; see the file COPYING.LIB. If not, write to * * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301, USA. * *******************************************************************************/ #include "package.h" #include #include #include +#include #include #include #include #include "config-package.h" #include #include #include "packagestructure.h" #include "packageloader.h" #include "private/package_p.h" //#include "private/packages_p.h" #include "private/packagejob_p.h" namespace KPackage { Package::Package(PackageStructure *structure) : d(new PackagePrivate()) { d->structure = structure; if (d->structure) { d->structure.data()->initPackage(this); addFileDefinition("metadata", QStringLiteral("metadata.desktop"), i18n("Desktop file that describes this package.")); } } Package::Package(const Package &other) : d(other.d) { } Package::~Package() { //guard against deletion on application shutdown if (PackageDeletionNotifier::self()) { Q_EMIT PackageDeletionNotifier::self()->packageDeleted(this); } } Package &Package::operator=(const Package &rhs) { if (&rhs != this) { d = rhs.d; } return *this; } bool Package::hasValidStructure() const { return d->structure; } bool Package::isValid() const { if (!d->structure) { return false; } //Minimal packages with no metadata *are* supposed to be possible //so if !metadata().isValid() go ahead if (metadata().isValid() && metadata().value(QStringLiteral("isHidden"), QStringLiteral("false")) == QLatin1String("true")) { return false; } if (d->checkedValid) { return d->valid; } const QString rootPath = d->tempRoot.isEmpty() ? d->path : d->tempRoot; if(rootPath.isEmpty()) { return false; } d->valid = true; //search for the file in all prefixes and in all possible paths for each prefix //even if it's a big nested loop, usually there is one prefix and one location //so shouldn't cause too much disk access QHashIterator it(d->contents); while (it.hasNext()) { it.next(); if (!it.value().required) { continue; } const QString foundPath = filePath(it.key(), {}); if (foundPath.isEmpty()) { //qWarning() << "Could not find required" << (it.value().directory ? "directory" : "file") << it.key() << "for package" << path() << "should be" << it.value().paths; d->valid = false; break; } } return d->valid; } QString Package::name(const QByteArray &key) const { QHash::const_iterator it = d->contents.constFind(key); if (it == d->contents.constEnd()) { return QString(); } return it.value().name; } bool Package::isRequired(const QByteArray &key) const { QHash::const_iterator it = d->contents.constFind(key); if (it == d->contents.constEnd()) { return false; } return it.value().required; } QStringList Package::mimeTypes(const QByteArray &key) const { QHash::const_iterator it = d->contents.constFind(key); if (it == d->contents.constEnd()) { return QStringList(); } if (it.value().mimeTypes.isEmpty()) { return d->mimeTypes; } return it.value().mimeTypes; } QString Package::defaultPackageRoot() const { return d->defaultPackageRoot; } void Package::setDefaultPackageRoot(const QString &packageRoot) { d.detach(); d->defaultPackageRoot = packageRoot; if (!d->defaultPackageRoot.isEmpty() && !d->defaultPackageRoot.endsWith(QLatin1Char('/'))) { d->defaultPackageRoot.append(QLatin1Char('/')); } } void Package::setFallbackPackage(const KPackage::Package &package) { if ((d->fallbackPackage && d->fallbackPackage->path() == package.path() && d->fallbackPackage->metadata() == package.metadata()) || //can't be fallback of itself (package.path() == path() && package.metadata() == metadata()) || d->hasCycle(package)) { return; } d->fallbackPackage = new Package(package); } KPackage::Package Package::fallbackPackage() const { if (d->fallbackPackage) { return (*d->fallbackPackage); } else { return Package(); } } bool Package::allowExternalPaths() const { return d->externalPaths; } void Package::setAllowExternalPaths(bool allow) { d.detach(); d->externalPaths = allow; } KPluginMetaData Package::metadata() const { //qDebug() << "metadata: " << d->path << filePath("metadata"); if (!d->metadata && !d->path.isEmpty()) { const QString metadataPath = filePath("metadata"); if (!metadataPath.isEmpty()) { d->createPackageMetadata(metadataPath); } else { // d->path might still be a file, if its path has a trailing /, // the fileInfo lookup will fail, so remove it. QString p = d->path; if (p.endsWith(QLatin1Char('/'))) { p.chop(1); } QFileInfo fileInfo(p); if (fileInfo.isDir()) { d->createPackageMetadata(d->path); } else if (fileInfo.exists()) { d->path = fileInfo.canonicalFilePath(); d->tempRoot = d->unpack(p); } } } if (!d->metadata) { d->metadata = new KPluginMetaData(); } return *d->metadata; } QString PackagePrivate::unpack(const QString &filePath) { KArchive *archive = nullptr; QMimeDatabase db; QMimeType mimeType = db.mimeTypeForFile(filePath); if (mimeType.inherits(QStringLiteral("application/zip"))) { archive = new KZip(filePath); } else if (mimeType.inherits(QStringLiteral("application/x-compressed-tar")) || mimeType.inherits(QStringLiteral("application/x-gzip")) || mimeType.inherits(QStringLiteral("application/x-tar")) || mimeType.inherits(QStringLiteral("application/x-bzip-compressed-tar")) || mimeType.inherits(QStringLiteral("application/x-xz")) || mimeType.inherits(QStringLiteral("application/x-lzma"))) { archive = new KTar(filePath); } else { //qWarning() << "Could not open package file, unsupported archive format:" << filePath << mimeType.name(); } QString tempRoot; if (archive && archive->open(QIODevice::ReadOnly)) { const KArchiveDirectory *source = archive->directory(); QTemporaryDir tempdir; tempdir.setAutoRemove(false); tempRoot = tempdir.path() + QLatin1Char('/'); source->copyTo(tempRoot); if (!QFile::exists(tempdir.path() + QLatin1String("/metadata.json")) && !QFile::exists(tempdir.path() + QLatin1String("/metadata.desktop"))) { // search metadata.desktop, the zip file might have the package contents in a subdirectory QDir unpackedPath(tempdir.path()); const auto entries = unpackedPath.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot); foreach (const auto &pack, entries) { if (QFile::exists(pack.filePath() + QLatin1String("/metadata.json")) || QFile::exists(pack.filePath() + QLatin1String("/metadata.desktop"))) { tempRoot = pack.filePath() + QLatin1Char('/'); } } } createPackageMetadata(tempRoot); } else { //qWarning() << "Could not open package file:" << path; } delete archive; return tempRoot; } bool PackagePrivate::isInsidePackageDir(const QString &canonicalPath) const { // make sure that the target file is actually inside the package dir to prevent // path traversal using symlinks or "../" path segments // make sure we got passed a valid path Q_ASSERT(QFileInfo::exists(canonicalPath)); Q_ASSERT(QFileInfo(canonicalPath).canonicalFilePath() == canonicalPath); // make sure that the base path is also canonical // this was not the case until 5.8, making this check fail e.g. if /home is a symlink // which in turn would make plasmashell not find the .qml files //installed package if (tempRoot.isEmpty()) { Q_ASSERT(QDir(path).exists()); Q_ASSERT(path == QStringLiteral("/") || QDir(path).canonicalPath() + QLatin1Char('/') == path); if (canonicalPath.startsWith(path)) { return true; } //temporary compressed package } else { Q_ASSERT(QDir(tempRoot).exists()); Q_ASSERT(tempRoot == QStringLiteral("/") || QDir(tempRoot).canonicalPath() + QLatin1Char('/') == tempRoot); if (canonicalPath.startsWith(tempRoot)) { return true; } } qWarning() << "Path traversal attempt detected:" << canonicalPath << "is not inside" << path; return false; } QString Package::filePath(const QByteArray &fileType, const QString &filename) const { if (!d->valid) { QString result = d->fallbackFilePath(fileType, filename); if (result.isEmpty()) { // qDebug() << fileType << "file with name" << filename // << "was not found in package with path" << d->path; } return result; } const QString discoveryKey(QString::fromUtf8(fileType) + filename); const auto path = d->discoveries.value(discoveryKey); if (!path.isEmpty()) { return path; } QStringList paths; if (fileType.size()) { const auto contents = d->contents.constFind(fileType); if (contents == d->contents.constEnd()) { //qDebug() << "package does not contain" << fileType << filename; return d->fallbackFilePath(fileType, filename); } paths = contents->paths; if (paths.isEmpty()) { //qDebug() << "no matching path came of it, while looking for" << fileType << filename; d->discoveries.insert(discoveryKey, QString()); return d->fallbackFilePath(fileType, filename); } } else { //when filetype is empty paths is always empty, so try with an empty string paths << QString(); } //Nested loop, but in the medium case resolves to just one iteration // qDebug() << "prefixes:" << d->contentsPrefixPaths.count() << d->contentsPrefixPaths; foreach (const QString &contentsPrefix, d->contentsPrefixPaths) { QString prefix; //We are an installed package if (d->tempRoot.isEmpty()) { prefix = fileType == "metadata" ? d->path : (d->path + contentsPrefix); //We are a compressed package temporarly uncompressed in /tmp } else { prefix = fileType == "metadata" ? d->tempRoot : (d->tempRoot + contentsPrefix); } foreach (const QString &path, paths) { QString file = prefix + path; if (!filename.isEmpty()) { file = file + QLatin1Char('/') + filename; } QFileInfo fi(file); if (fi.exists()) { if (d->externalPaths) { //qDebug() << "found" << file; d->discoveries.insert(discoveryKey, file); return file; } // ensure that we don't return files outside of our base path // due to symlink or ../ games if (d->isInsidePackageDir(fi.canonicalFilePath())) { //qDebug() << "found" << file; d->discoveries.insert(discoveryKey, file); return file; } } } } //qDebug() << fileType << filename << "does not exist in" << prefixes << "at root" << d->path; return d->fallbackFilePath(fileType, filename); } +QUrl Package::fileUrl(const QByteArray &fileType, const QString &filename) const +{ + return QUrl::fromLocalFile(filePath(fileType, filename)); +} + QStringList Package::entryList(const QByteArray &key) const { if (!d->valid) { return QStringList(); } QHash::const_iterator it = d->contents.constFind(key); if (it == d->contents.constEnd()) { //qDebug() << "couldn't find" << key; return QStringList(); } //qDebug() << "going to list" << key; QStringList list; foreach (const QString &prefix, d->contentsPrefixPaths) { //qDebug() << " looking in" << prefix; foreach (const QString &path, it.value().paths) { //qDebug() << " looking in" << path; if (it.value().directory) { //qDebug() << "it's a directory, so trying out" << d->path + prefix + path; QDir dir(d->path + prefix + path); if (d->externalPaths) { list += dir.entryList(QDir::Files | QDir::Readable); } else { // ensure that we don't return files outside of our base path // due to symlink or ../ games QString canonicalized = dir.canonicalPath(); if (canonicalized.startsWith(d->path)) { list += dir.entryList(QDir::Files | QDir::Readable); } } } else { const QString fullPath = d->path + prefix + path; //qDebug() << "it's a file at" << fullPath << QFile::exists(fullPath); if (!QFile::exists(fullPath)) { continue; } if (d->externalPaths) { list += fullPath; } else { QDir dir(fullPath); QString canonicalized = dir.canonicalPath() + QDir::separator(); //qDebug() << "testing that" << canonicalized << "is in" << d->path; if (canonicalized.startsWith(d->path)) { list += fullPath; } } } } } return list; } void Package::setPath(const QString &path) { // if the path is already what we have, don't bother if (path == d->path) { return; } // our dptr is shared, and it is almost certainly going to change. // hold onto the old pointer just in case it does not, however! QExplicitlySharedDataPointer oldD(d); d.detach(); // without structure we're doomed if (!d->structure) { d->path.clear(); d->discoveries.clear(); d->valid = false; d->checkedValid = true; return; } // empty path => nothing to do if (path.isEmpty()) { d->path.clear(); d->discoveries.clear(); d->valid = false; d->structure.data()->pathChanged(this); return; } // now we look for all possible paths, including resolving // relative paths against the system search paths QStringList paths; if (QDir::isRelativePath(path)) { QString p; if (d->defaultPackageRoot.isEmpty()) { p = path % QLatin1Char('/'); } else { p = d->defaultPackageRoot % path % QLatin1Char('/'); } if (QDir::isRelativePath(p)) { paths << QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, p, QStandardPaths::LocateDirectory); } else { const QDir dir(p); if (QFile::exists(dir.canonicalPath())) { paths << p; } } //qDebug() << "paths:" << p << paths << d->defaultPackageRoot; } else { const QDir dir(path); if (QFile::exists(dir.canonicalPath())) { paths << path; } } QFileInfo fileInfo(path); if (fileInfo.isFile() && d->tempRoot.isEmpty()) { d->path = fileInfo.canonicalFilePath(); d->tempRoot = d->unpack(path); } // now we search each path found, caching our previous path to know if // anything actually really changed const QString previousPath = d->path; foreach (const QString &p, paths) { d->checkedValid = false; QDir dir(p); Q_ASSERT(QFile::exists(dir.canonicalPath())); d->path = dir.canonicalPath(); // canonicalPath() does not include a trailing slash (unless it is the root dir) if (!d->path.endsWith(QLatin1Char('/'))) { d->path.append(QLatin1Char('/')); } const QString fallbackPath = metadata().value(QStringLiteral("X-Plasma-RootPath")); if (!fallbackPath.isEmpty()) { const KPackage::Package fp = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/Applet"), fallbackPath); setFallbackPackage(fp); } // we need to tell the structure we're changing paths ... d->structure.data()->pathChanged(this); // ... and then testing the results for validity if (isValid()) { break; } } // if nothing did change, then we go back to the old dptr if (d->path == previousPath) { d = oldD; return; } // .. but something did change, so we get rid of our discovery cache d->discoveries.clear(); delete d->metadata; d->metadata = nullptr; // uh-oh, but we didn't end up with anything valid, so we sadly reset ourselves // to futility. if (!d->valid) { d->path.clear(); d->structure.data()->pathChanged(this); } } const QString Package::path() const { return d->path; } QStringList Package::contentsPrefixPaths() const { return d->contentsPrefixPaths; } void Package::setContentsPrefixPaths(const QStringList &prefixPaths) { d.detach(); d->contentsPrefixPaths = prefixPaths; if (d->contentsPrefixPaths.isEmpty()) { d->contentsPrefixPaths << QString(); } else { // the code assumes that the prefixes have a trailing slash // so let's make that true here QMutableStringListIterator it(d->contentsPrefixPaths); while (it.hasNext()) { it.next(); if (!it.value().endsWith(QLatin1Char('/'))) { it.setValue(it.value() % QLatin1Char('/')); } } } } #ifndef PACKAGE_NO_DEPRECATED QString Package::contentsHash() const { return QString::fromLocal8Bit(cryptographicHash(QCryptographicHash::Sha1)); } #endif QByteArray Package::cryptographicHash(QCryptographicHash::Algorithm algorithm) const { if (!d->valid) { qWarning() << "can not create hash due to Package being invalid"; return QByteArray(); } QCryptographicHash hash(algorithm); const QString metadataPath = QFile::exists(d->path + QLatin1String("metadata.json")) ? d->path + QLatin1String("metadata.json") : QFile::exists(d->path + QLatin1String("metadata.desktop")) ? d->path + QLatin1String("metadata.desktop") : QString(); if (!metadataPath.isEmpty()) { QFile f(metadataPath); if (f.open(QIODevice::ReadOnly)) { while (!f.atEnd()) { hash.addData(f.read(1024)); } } else { qWarning() << "could not add" << f.fileName() << "to the hash; file could not be opened for reading."; } } else { qWarning() << "no metadata at" << metadataPath; } foreach (const QString &prefix, d->contentsPrefixPaths) { const QString basePath = d->path + prefix; QDir dir(basePath); if (!dir.exists()) { return QByteArray(); } d->updateHash(basePath, QString(), dir, hash); } return hash.result().toHex(); } void Package::addDirectoryDefinition(const QByteArray &key, const QString &path, const QString &name) { const auto contentsIt = d->contents.constFind(key); ContentStructure s; if (contentsIt != d->contents.constEnd()) { if (contentsIt->paths.contains(path) && contentsIt->directory == true && contentsIt->name == name) { return; } s = *contentsIt; } d.detach(); if (!name.isEmpty()) { s.name = name; } s.paths.append(path); s.directory = true; d->contents[key] = s; } void Package::addFileDefinition(const QByteArray &key, const QString &path, const QString &name) { const auto contentsIt = d->contents.constFind(key); ContentStructure s; if (contentsIt != d->contents.constEnd()) { if (contentsIt->paths.contains(path) && contentsIt->directory == true && contentsIt->name == name) { return; } s = *contentsIt; } d.detach(); if (!name.isEmpty()) { s.name = name; } s.paths.append(path); s.directory = false; d->contents[key] = s; } void Package::removeDefinition(const QByteArray &key) { if (d->contents.contains(key)) { d.detach(); d->contents.remove(key); } if (d->discoveries.contains(QString::fromLatin1(key))) { d.detach(); d->discoveries.remove(QString::fromLatin1(key)); } } void Package::setRequired(const QByteArray &key, bool required) { QHash::iterator it = d->contents.find(key); if (it == d->contents.end()) { return; } d.detach(); // have to find the item again after detaching: d->contents is a different object now it = d->contents.find(key); it.value().required = required; } void Package::setDefaultMimeTypes(const QStringList &mimeTypes) { d.detach(); d->mimeTypes = mimeTypes; } void Package::setMimeTypes(const QByteArray &key, const QStringList &mimeTypes) { QHash::iterator it = d->contents.find(key); if (it == d->contents.end()) { return; } d.detach(); // have to find the item again after detaching: d->contents is a different object now it = d->contents.find(key); it.value().mimeTypes = mimeTypes; } QList Package::directories() const { QList dirs; QHash::const_iterator it = d->contents.constBegin(); while (it != d->contents.constEnd()) { if (it.value().directory) { dirs << it.key(); } ++it; } return dirs; } QList Package::requiredDirectories() const { QList dirs; QHash::const_iterator it = d->contents.constBegin(); while (it != d->contents.constEnd()) { if (it.value().directory && it.value().required) { dirs << it.key(); } ++it; } return dirs; } QList Package::files() const { QList files; QHash::const_iterator it = d->contents.constBegin(); while (it != d->contents.constEnd()) { if (!it.value().directory) { files << it.key(); } ++it; } return files; } QList Package::requiredFiles() const { QList files; QHash::const_iterator it = d->contents.constBegin(); while (it != d->contents.constEnd()) { if (!it.value().directory && it.value().required) { files << it.key(); } ++it; } return files; } KJob *Package::install(const QString &sourcePackage, const QString &packageRoot) { const QString src = sourcePackage; QString dest = packageRoot.isEmpty() ? defaultPackageRoot() : packageRoot; //use absolute paths if passed, otherwise go under share if (!QDir::isAbsolutePath(dest)) { dest = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + dest; } if (!d->structure) { return nullptr; } //qDebug() << "Source: " << src; //qDebug() << "PackageRoot: " << dest; KJob *j = d->structure.data()->install(this, src, dest); return j; } KJob *Package::update(const QString &sourcePackage, const QString &packageRoot) { const QString src = sourcePackage; QString dest = packageRoot.isEmpty() ? defaultPackageRoot() : packageRoot; //use absolute paths if passed, otherwise go under share if (!QDir::isAbsolutePath(dest)) { dest = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + dest; } if (!d->structure) { return nullptr; } //qDebug() << "Source: " << src; //qDebug() << "PackageRoot: " << dest; KJob *j = d->structure.data()->update(this, src, dest); return j; } KJob *Package::uninstall(const QString &packageName, const QString &packageRoot) { d->createPackageMetadata(packageRoot + QLatin1Char('/') + packageName); if (!d->structure) { return nullptr; } return d->structure.data()->uninstall(this, packageRoot); } PackagePrivate::PackagePrivate() : QSharedData(), fallbackPackage(nullptr), metadata(nullptr), externalPaths(false), valid(false), checkedValid(false) { contentsPrefixPaths << QStringLiteral("contents/"); } PackagePrivate::PackagePrivate(const PackagePrivate &other) : QSharedData() { *this = other; metadata = nullptr; } PackagePrivate::~PackagePrivate() { if (!tempRoot.isEmpty()) { QDir dir(tempRoot); dir.removeRecursively(); } delete metadata; delete fallbackPackage; } PackagePrivate &PackagePrivate::operator=(const PackagePrivate &rhs) { if (&rhs == this) { return *this; } structure = rhs.structure; if (rhs.fallbackPackage) { fallbackPackage = new Package(*rhs.fallbackPackage); } else { fallbackPackage = nullptr; } path = rhs.path; contentsPrefixPaths = rhs.contentsPrefixPaths; contents = rhs.contents; mimeTypes = rhs.mimeTypes; defaultPackageRoot = rhs.defaultPackageRoot; metadata = nullptr; externalPaths = rhs.externalPaths; valid = rhs.valid; return *this; } void PackagePrivate::updateHash(const QString &basePath, const QString &subPath, const QDir &dir, QCryptographicHash &hash) { // hash is calculated as a function of: // * files ordered alphabetically by name, with each file's: // * path relative to the content root // * file data // * directories ordered alphabetically by name, with each dir's: // * path relative to the content root // * file listing (recursing) // symlinks (in both the file and dir case) are handled by adding // the name of the symlink itself and the abs path of what it points to const QDir::SortFlags sorting = QDir::Name | QDir::IgnoreCase; const QDir::Filters filters = QDir::Hidden | QDir::System | QDir::NoDotAndDotDot; foreach (const QString &file, dir.entryList(QDir::Files | filters, sorting)) { if (!subPath.isEmpty()) { hash.addData(subPath.toUtf8()); } hash.addData(file.toUtf8()); QFileInfo info(dir.path() + QLatin1Char('/') + file); if (info.isSymLink()) { hash.addData(info.symLinkTarget().toUtf8()); } else { QFile f(info.filePath()); if (f.open(QIODevice::ReadOnly)) { while (!f.atEnd()) { hash.addData(f.read(1024)); } } else { qWarning() << "could not add" << f.fileName() << "to the hash; file could not be opened for reading. " << "permissions fail?" << info.permissions() << info.isFile(); } } } foreach (const QString &subDirPath, dir.entryList(QDir::Dirs | filters, sorting)) { const QString relativePath = subPath + subDirPath + QLatin1Char('/'); hash.addData(relativePath.toUtf8()); QDir subDir(dir.path()); subDir.cd(subDirPath); if (subDir.path() != subDir.canonicalPath()) { hash.addData(subDir.canonicalPath().toUtf8()); } else { updateHash(basePath, relativePath, subDir, hash); } } } void PackagePrivate::createPackageMetadata(const QString &path) { const bool isDir = QFileInfo(path).isDir(); delete metadata; if (isDir && QFile::exists(path + QStringLiteral("/metadata.json"))) { metadata = new KPluginMetaData(path + QStringLiteral("/metadata.json")); } else if (isDir && QFile::exists(path + QStringLiteral("/metadata.desktop"))) { auto md = KPluginMetaData::fromDesktopFile(path + QStringLiteral("/metadata.desktop"), {QStringLiteral(":/kservicetypes5/kpackage-generic.desktop")}); metadata = new KPluginMetaData(md); } else { if (isDir) { qWarning() << "No metadata file in the package, expected it at:" << path; } else if (path.endsWith(QLatin1String(".desktop"))) { auto md = KPluginMetaData::fromDesktopFile(path, {QStringLiteral(":/kservicetypes5/kpackage-generic.desktop")}); metadata = new KPluginMetaData(md); } else { metadata = new KPluginMetaData(path); } } } QString PackagePrivate::fallbackFilePath(const QByteArray &key, const QString &filename) const { //don't fallback if the package isn't valid and never fallback the metadata file if (qstrcmp(key, "metadata") != 0 && fallbackPackage && fallbackPackage->isValid()) { return fallbackPackage->filePath(key, filename); } else { return QString(); } } bool PackagePrivate::hasCycle(const KPackage::Package &package) { if (!package.d->fallbackPackage) { return false; } //This is the Floyd cycle detection algorithm //http://en.wikipedia.org/wiki/Cycle_detection#Tortoise_and_hare KPackage::Package *slowPackage = const_cast(&package); KPackage::Package *fastPackage = const_cast(&package); while (fastPackage && fastPackage->d->fallbackPackage) { //consider two packages the same if they have the same metadata if ((fastPackage->d->fallbackPackage->metadata().isValid() && fastPackage->d->fallbackPackage->metadata() == slowPackage->metadata()) || (fastPackage->d->fallbackPackage->d->fallbackPackage && fastPackage->d->fallbackPackage->d->fallbackPackage->metadata().isValid() && fastPackage->d->fallbackPackage->d->fallbackPackage->metadata() == slowPackage->metadata())) { qWarning() << "Warning: the fallback chain of " << package.metadata().pluginId() << "contains a cyclical dependency."; return true; } fastPackage = fastPackage->d->fallbackPackage->d->fallbackPackage; slowPackage = slowPackage->d->fallbackPackage; } return false; } Q_GLOBAL_STATIC(PackageDeletionNotifier, s_packageDeletionNotifier) PackageDeletionNotifier* PackageDeletionNotifier::self() { return s_packageDeletionNotifier; } } // Namespace #include "private/moc_package_p.cpp" diff --git a/src/kpackage/package.h b/src/kpackage/package.h index 7e365a3..9747654 100644 --- a/src/kpackage/package.h +++ b/src/kpackage/package.h @@ -1,390 +1,405 @@ /****************************************************************************** * Copyright 2007-2011 by Aaron Seigo * * * * This library is free software; you can redistribute it and/or * * modify it under the terms of the GNU Library General Public * * License as published by the Free Software Foundation; either * * version 2 of the License, or (at your option) any later version. * * * * This library 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 * * Library General Public License for more details. * * * * You should have received a copy of the GNU Library General Public License * * along with this library; see the file COPYING.LIB. If not, write to * * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301, USA. * *******************************************************************************/ #ifndef KPACKAGE_PACKAGE_H #define KPACKAGE_PACKAGE_H #include #include #include #include #include #include namespace KPackage { /** * @class Package kpackage/package.h * * @short object representing an installed package * * Package defines what is in a package and provides easy access to the contents. * * To define a package, one might write the following code: * @code Package package; package.addDirectoryDefinition("images", "pics/", i18n("Images")); QStringList mimeTypes; mimeTypes << "image/svg" << "image/png" << "image/jpeg"; package.setMimeTypes("images", mimeTypes); package.addDirectoryDefinition("scripts", "code/", i18n("Executable Scripts")); mimeTypes.clear(); mimeTypes << "text/\*"; package.setMimeTypes("scripts", mimeTypes); package.addFileDefinition("mainscript", "code/main.js", i18n("Main Script File")); package.setRequired("mainscript", true); @endcode * One may also choose to create a subclass of PackageStructure and include the setup * in the constructor. * * Either way, Package creates a self-documenting contract between the packager and * the application without exposing package internals such as actual on-disk structure * of the package or requiring that all contents be explicitly known ahead of time. * * Subclassing PackageStructure does have provide a number of potential const benefits: * * the package can be notified of path changes via the virtual pathChanged() method * * the subclass may implement mechanisms to install and remove packages using the * virtual install and uninstall methods * * subclasses can be compiled as plugins for easy re-use **/ //TODO: write documentation on USING a package class PackagePrivate; class PackageStructure; class PACKAGE_EXPORT Package { public: /** * Error codes for the install/update/remove jobs * @since 5.17 */ enum JobError { RootCreationError = KJob::UserDefinedError + 1, /**< Cannot create package root directory */ PackageFileNotFoundError, /**< The package file does not exist */ UnsupportedArchiveFormatError, /**< The archive format of the package is not supported */ PackageOpenError, /**< Can't open the package file for reading */ MetadataFileMissingError, /**< The package doesn't have a metadata.desktop file */ PluginNameMissingError, /**< The metadata.desktop file doesn't specify a plugin name */ PluginNameInvalidError, /**< The plugin name contains charaters different from letters, digits, dots and underscores */ UpdatePackageTypeMismatchError, /**< A package with this plugin name was already installed, but has a different type in the metadata.desktop file */ OldVersionRemovalError, /**< Failed to remove the old version of the package during an upgrade */ NewerVersionAlreadyInstalledError, /**< We tried to update, but the same version or a newer one is already installed */ PackageAlreadyInstalledError, /**< The package is already installed and a normal install (not update) was performed */ PackageMoveError, /**< Failure to move a package from the system temporary folder to its final destination */ PackageCopyError, /**< Failure to copy a package folder from somewhere in the filesystem to its final destination */ PackageUninstallError /**< Failure to uninstall a package */ }; /** * Default constructor * * @param structure if a null pointer is passed in, this will creates an empty (invalid) Package; * otherwise the structure is allowed to set up the Package's initial layout * @since 4.6 */ explicit Package(PackageStructure *structure = nullptr); /** * Copy constructor * @since 4.6 */ Package(const Package &other); virtual ~Package(); /** * Assignment operator * @since 4.6 */ Package &operator=(const Package &rhs); /** * @return true if this package has a valid PackageStructure associatedw it with it. * A package may not be valid, but have a valid structure. Useful when dealing with * Package objects in a semi-initialized state (e.g. before calling setPath()) * @since 5.1 */ bool hasValidStructure() const; /** * @return true if all the required components exist **/ bool isValid() const; /** * Sets the path to the root of this package * @param path an absolute path, or a relative path to the default package root * @since 4.3 */ void setPath(const QString &path); /** * @return the path to the root of this particular package */ const QString path() const; /** * Get the path to a given file based on the key and an optional filename. * Example: finding the main script in a scripting package: * filePath("mainscript") * * Example: finding a specific image in the images directory: * filePath("images", "myimage.png") * * @param key the key of the file type to look for, * @param filename optional name of the file to locate within the package * @return path to the file on disk. QString() if not found. **/ QString filePath(const QByteArray &key, const QString &filename = QString()) const; + /** + * Get the url to a given file based on the key and an optional filename, is the file:// or qrc:// format + * Example: finding the main script in a scripting package: + * filePath("mainscript") + * + * Example: finding a specific image in the images directory: + * filePath("images", "myimage.png") + * + * @param key the key of the file type to look for, + * @param filename optional name of the file to locate within the package + * @return path to the file on disk. QString() if not found. + * @since 5.41 + **/ + QUrl fileUrl(const QByteArray &key, const QString &filename = QString()) const; + /** * Get the list of files of a given type. * * @param fileType the type of file to look for, as defined in the * package structure. * @return list of files by name, suitable for passing to filePath **/ QStringList entryList(const QByteArray &key) const; /** * @return user visible name for the given entry **/ QString name(const QByteArray &key) const; /** * @return true if the item at path exists and is required **/ bool isRequired(const QByteArray &key) const; /** * @return the mimeTypes associated with the path, if any **/ QStringList mimeTypes(const QByteArray &key) const; /** * @return the prefix paths inserted between the base path and content entries, in order of priority. * When searching for a file, all paths will be tried in order. * @since 4.6 */ QStringList contentsPrefixPaths() const; /** * @return preferred package root. This defaults to kpackage/generic/ */ QString defaultPackageRoot() const; /** * @return true if paths/symlinks outside the package itself should be followed. * By default this is set to false for security reasons. */ bool allowExternalPaths() const; /** * @return the package metadata object. */ KPluginMetaData metadata() const; /** * @return a SHA1 hash digest of the contents of the package in hexadecimal form * @since 4.4 * @deprecated Since 5.21 use cryptographicHash */ #ifndef PACKAGE_NO_DEPRECATED PACKAGE_DEPRECATED QString contentsHash() const; #endif /** * @return a hash digest of the contents of the package in hexadecimal form * @since 5.21 */ QByteArray cryptographicHash(QCryptographicHash::Algorithm algorithm) const; /** * Adds a directory to the structure of the package. It is added as * a not-required element with no associated mimeTypes. * * Starting in 4.6, if an entry with the given key * already exists, the path is added to it as a search alternative. * * @param key used as an internal label for this directory * @param path the path within the package for this directory * @param name the user visible (translated) name for the directory **/ void addDirectoryDefinition(const QByteArray &key, const QString &path, const QString &name); /** * Adds a file to the structure of the package. It is added as * a not-required element with no associated mimeTypes. * * Starting in 4.6, if an entry with the given key * already exists, the path is added to it as a search alternative. * * @param key used as an internal label for this file * @param path the path within the package for this file * @param name the user visible (translated) name for the file **/ void addFileDefinition(const QByteArray &key, const QString &path, const QString &name); /** * Removes a definition from the structure of the package. * @since 4.6 * @param key the internal label of the file or directory to remove */ void removeDefinition(const QByteArray &key); /** * Sets whether or not a given part of the structure is required or not. * The path must already have been added using addDirectoryDefinition * or addFileDefinition. * * @param key the entry within the package * @param required true if this entry is required, false if not */ void setRequired(const QByteArray &key, bool required); /** * Defines the default mimeTypes for any definitions that do not have * associated mimeTypes. Handy for packages with only one or predominantly * one file type. * * @param mimeTypes a list of mimeTypes **/ void setDefaultMimeTypes(const QStringList &mimeTypes); /** * Define mimeTypes for a given part of the structure * The path must already have been added using addDirectoryDefinition * or addFileDefinition. * * @param key the entry within the package * @param mimeTypes a list of mimeTypes **/ void setMimeTypes(const QByteArray &key, const QStringList &mimeTypes); /** * Sets the prefixes that all the contents in this package should * appear under. This defaults to "contents/" and is added automatically * between the base path and the entries as defined by the package * structure. Multiple entries can be added. * In this case each file request will be searched in all prefixes in order, * and the first found will be returned. * * @param prefix paths the directory prefix to use * @since 4.6 */ void setContentsPrefixPaths(const QStringList &prefixPaths); /** * Sets whether or not external paths/symlinks can be followed by a package * @param allow true if paths/symlinks outside of the package should be followed, * false if they should be rejected. */ void setAllowExternalPaths(bool allow); /** * Sets preferred package root. */ void setDefaultPackageRoot(const QString &packageRoot); /** * Sets the fallback package root path * If a file won't be found in this package, it will search it in the package * with the same structure identified by path * It is intended to be used by the packageStructure * @param path package root path @see setPath */ void setFallbackPackage(const KPackage::Package &package); /** * @return The fallback package root path */ KPackage::Package fallbackPackage() const; // Content structure description methods /** * @return all directories registered as part of this Package's structure */ QList directories() const; /** * @return all directories registered as part of this Package's required structure */ QList requiredDirectories() const; /** * @return all files registered as part of this Package's structure */ QList files() const; /** * @return all files registered as part of this Package's required structure */ QList requiredFiles() const; /** * Installs a package matching this package structure. By default installs a * native KPackage::Package. * After the KJob will emitted finished(), if the install went well * the Package instance will be guaranteed to have loaded the package just * installed, and be valid and usable. * * @return KJob to track installation progress and result **/ KJob *install(const QString &sourcePackage, const QString &packageRoot = QString()); /** * Updates a package matching this package structure. By default installs a * native KPackage::Package. If an older version is already installed, * it removes the old one. If the installed one is newer, * an error will occur. * After the KJob will emitted finished(), if the install went well * the Package instance will be guaranteed to have loaded the package just * updated, and be valid and usable. * * @return KJob to track installation progress and result * @since 5.17 **/ KJob *update(const QString &sourcePackage, const QString &packageRoot = QString()); /** * Uninstalls a package matching this package structure. * * @return KJob to track removal progress and result */ KJob *uninstall(const QString &packageName, const QString &packageRoot); private: QExplicitlySharedDataPointer d; friend class PackagePrivate; }; } Q_DECLARE_METATYPE(KPackage::Package) #endif