2008-11-04 00:08:39 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Copyright 2007 by Aaron Seigo <aseigo@kde.org> *
|
2011-07-08 19:36:29 +02:00
|
|
|
* Copyright 2010 by Marco Martin <notmart@gmail.com> *
|
|
|
|
* Copyright 2010 by Kevin Ottens <ervin@kde.org> *
|
|
|
|
* Copyright 2009 by Rob Scheepmaker *
|
2008-11-04 00:08:39 +01:00
|
|
|
* *
|
|
|
|
* 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"
|
2009-09-21 23:37:44 +02:00
|
|
|
#include "config-plasma.h"
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QRegExp>
|
2009-10-20 01:04:42 +02:00
|
|
|
#include <QtNetwork/QHostInfo>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2008-11-04 03:20:46 +01:00
|
|
|
#include <karchive.h>
|
2008-11-04 03:04:34 +01:00
|
|
|
#include <kcomponentdata.h>
|
|
|
|
#include <kdesktopfile.h>
|
2010-10-14 14:27:15 +02:00
|
|
|
#ifndef PLASMA_NO_KIO
|
2008-11-04 03:04:34 +01:00
|
|
|
#include <kio/copyjob.h>
|
|
|
|
#include <kio/deletejob.h>
|
2008-11-04 03:20:46 +01:00
|
|
|
#include <kio/jobclasses.h>
|
2008-11-04 03:04:34 +01:00
|
|
|
#include <kio/job.h>
|
2010-10-14 14:27:15 +02:00
|
|
|
#endif
|
2009-07-25 11:34:19 +02:00
|
|
|
#include <kmimetype.h>
|
2008-11-04 03:04:34 +01:00
|
|
|
#include <kplugininfo.h>
|
|
|
|
#include <kstandarddirs.h>
|
2009-07-25 11:34:19 +02:00
|
|
|
#include <ktar.h>
|
2008-11-04 03:04:34 +01:00
|
|
|
#include <ktempdir.h>
|
|
|
|
#include <ktemporaryfile.h>
|
|
|
|
#include <kzip.h>
|
|
|
|
#include <kdebug.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
#include "private/package_p.h"
|
2009-11-25 02:09:17 +01:00
|
|
|
#include "private/plasmoidservice_p.h"
|
2009-09-02 04:27:16 +02:00
|
|
|
#include "private/service_p.h"
|
2011-04-29 15:18:35 +02:00
|
|
|
#include "remote/authorizationmanager.h"
|
|
|
|
#include "remote/authorizationmanager_p.h"
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2010-10-14 15:23:56 +02:00
|
|
|
#ifdef PLASMA_NO_KIO // Provide some convenience for dealing with folders
|
|
|
|
|
|
|
|
bool copyFolder(QString sourcePath, QString targetPath)
|
|
|
|
{
|
|
|
|
QDir source(sourcePath);
|
|
|
|
if(!source.exists())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QDir target(targetPath);
|
|
|
|
if(!target.exists()) {
|
|
|
|
QString targetName = target.dirName();
|
|
|
|
target.cdUp();
|
|
|
|
target.mkdir(targetName);
|
|
|
|
target = QDir(targetPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (const QString &fileName, source.entryList(QDir::Files)) {
|
|
|
|
QString sourceFilePath = sourcePath + QDir::separator() + fileName;
|
|
|
|
QString targetFilePath = targetPath + QDir::separator() + fileName;
|
|
|
|
|
|
|
|
if (!QFile::copy(sourceFilePath, targetFilePath)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (const QString &subFolderName, source.entryList(QDir::AllDirs | QDir::NoDotAndDotDot)) {
|
|
|
|
QString sourceSubFolderPath = sourcePath + QDir::separator() + subFolderName;
|
|
|
|
QString targetSubFolderPath = targetPath + QDir::separator() + subFolderName;
|
|
|
|
|
|
|
|
if (!copyFolder(sourceSubFolderPath, targetSubFolderPath)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool removeFolder(QString folderPath)
|
|
|
|
{
|
|
|
|
QDir folder(folderPath);
|
|
|
|
if(!folder.exists())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
foreach (const QString &fileName, folder.entryList(QDir::Files)) {
|
|
|
|
if (!QFile::remove(folderPath + QDir::separator() + fileName)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (const QString &subFolderName, folder.entryList(QDir::AllDirs | QDir::NoDotAndDotDot)) {
|
|
|
|
if (!removeFolder(folderPath + QDir::separator() + subFolderName)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString folderName = folder.dirName();
|
|
|
|
folder.cdUp();
|
|
|
|
return folder.rmdir(folderName);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // PLASMA_NO_KIO
|
|
|
|
|
|
|
|
|
2010-09-03 21:44:28 +02:00
|
|
|
Package::Package()
|
|
|
|
: d(new PackagePrivate(PackageStructure::Ptr(0), QString()))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
Package::Package(const QString &packagePath, PackageStructure::Ptr structure)
|
|
|
|
: d(new PackagePrivate(structure, packagePath))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-09-03 21:44:28 +02:00
|
|
|
Package::Package(const Package &other)
|
|
|
|
: d(new PackagePrivate(*other.d))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
Package::~Package()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2010-09-03 21:44:28 +02:00
|
|
|
Package &Package::operator=(const Package &rhs)
|
|
|
|
{
|
|
|
|
if (&rhs != this) {
|
|
|
|
*d = *rhs.d;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
bool Package::isValid() const
|
|
|
|
{
|
2010-12-15 07:29:36 +01:00
|
|
|
if (!d->valid) {
|
2008-11-04 00:08:39 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-04 20:18:32 +01:00
|
|
|
//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
|
2010-12-15 07:29:36 +01:00
|
|
|
QStringList prefixes = d->structure->contentsPrefixPaths();
|
|
|
|
if (prefixes.isEmpty()) {
|
|
|
|
prefixes << QString();
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
foreach (const char *dir, d->structure->requiredDirectories()) {
|
2010-11-03 23:12:30 +01:00
|
|
|
bool failed = true;
|
|
|
|
foreach (const QString &path, d->structure->searchPath(dir)) {
|
2010-12-15 07:29:36 +01:00
|
|
|
foreach (const QString &prefix, prefixes) {
|
2010-11-04 22:54:14 +01:00
|
|
|
if (QFile::exists(d->structure->path() + prefix + path)) {
|
2010-11-04 20:18:32 +01:00
|
|
|
failed = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!failed) {
|
2010-11-03 23:12:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (failed) {
|
2008-11-04 00:08:39 +01:00
|
|
|
kWarning() << "Could not find required directory" << dir;
|
|
|
|
d->valid = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (const char *file, d->structure->requiredFiles()) {
|
2010-11-03 23:12:30 +01:00
|
|
|
bool failed = true;
|
|
|
|
foreach (const QString &path, d->structure->searchPath(file)) {
|
2010-12-15 07:29:36 +01:00
|
|
|
foreach (const QString &prefix, prefixes) {
|
2010-11-04 22:54:14 +01:00
|
|
|
if (QFile::exists(d->structure->path() + prefix + path)) {
|
2010-11-04 20:18:32 +01:00
|
|
|
failed = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!failed) {
|
2010-11-03 23:12:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (failed) {
|
|
|
|
kWarning() << "Could not find required file" << file;
|
2008-11-04 00:08:39 +01:00
|
|
|
d->valid = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Package::filePath(const char *fileType, const QString &filename) const
|
|
|
|
{
|
|
|
|
if (!d->valid) {
|
2010-09-05 00:50:50 +02:00
|
|
|
//kDebug() << "package is not valid";
|
2008-11-04 00:08:39 +01:00
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2010-11-03 23:12:30 +01:00
|
|
|
QStringList paths;
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2010-03-09 06:57:57 +01:00
|
|
|
if (qstrlen(fileType) != 0) {
|
2010-11-03 23:12:30 +01:00
|
|
|
paths = d->structure->searchPath(fileType);
|
2010-03-09 06:57:57 +01:00
|
|
|
|
2010-11-03 23:12:30 +01:00
|
|
|
if (paths.isEmpty()) {
|
2010-09-05 00:50:50 +02:00
|
|
|
//kDebug() << "no matching path came of it, while looking for" << fileType << filename;
|
2010-03-09 06:57:57 +01:00
|
|
|
return QString();
|
|
|
|
}
|
2010-11-04 20:18:32 +01:00
|
|
|
} else {
|
2010-12-15 08:13:20 +01:00
|
|
|
//when filetype is empty paths is always empty, so try with an empty string
|
2010-11-04 20:18:32 +01:00
|
|
|
paths << QString();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2010-11-04 20:18:32 +01:00
|
|
|
//Nested loop, but in the medium case resolves to just one iteration
|
2010-12-15 07:29:36 +01:00
|
|
|
QStringList prefixes = d->structure->contentsPrefixPaths();
|
|
|
|
if (prefixes.isEmpty()) {
|
|
|
|
prefixes << QString();
|
|
|
|
}
|
|
|
|
|
2010-12-15 08:13:20 +01:00
|
|
|
//kDebug() << "prefixes:" << prefixes.count() << prefixes;
|
2010-12-15 07:29:36 +01:00
|
|
|
foreach (const QString &contentsPrefix, prefixes) {
|
2010-11-04 22:54:14 +01:00
|
|
|
const QString prefix(d->structure->path() + contentsPrefix);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2010-11-04 20:18:32 +01:00
|
|
|
foreach (const QString &path, paths) {
|
|
|
|
QString file = prefix + path;
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2010-11-04 20:18:32 +01:00
|
|
|
if (!filename.isEmpty()) {
|
|
|
|
file.append("/").append(filename);
|
2010-11-03 23:12:30 +01:00
|
|
|
}
|
|
|
|
|
2010-12-15 08:13:20 +01:00
|
|
|
//kDebug() << "testing" << file << QFile::exists("/bin/ls") << QFile::exists(file);
|
2010-11-04 20:18:32 +01:00
|
|
|
if (QFile::exists(file)) {
|
|
|
|
if (d->structure->allowExternalPaths()) {
|
2010-12-15 08:13:20 +01:00
|
|
|
//kDebug() << "found" << file;
|
2010-11-04 20:18:32 +01:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure that we don't return files outside of our base path
|
|
|
|
// due to symlink or ../ games
|
|
|
|
QDir dir(file);
|
|
|
|
QString canonicalized = dir.canonicalPath() + QDir::separator();
|
|
|
|
|
2010-12-15 08:13:20 +01:00
|
|
|
//kDebug() << "testing that" << canonicalized << "is in" << d->structure->path();
|
2010-11-04 20:18:32 +01:00
|
|
|
if (canonicalized.startsWith(d->structure->path())) {
|
2010-12-15 08:13:20 +01:00
|
|
|
//kDebug() << "found" << file;
|
2010-11-04 20:18:32 +01:00
|
|
|
return file;
|
|
|
|
}
|
2010-11-03 23:12:30 +01:00
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-15 08:13:20 +01:00
|
|
|
//kDebug() << fileType << filename << "does not exist in" << prefixes << "at root" << d->structure->path();
|
2008-11-04 00:08:39 +01:00
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Package::filePath(const char *fileType) const
|
|
|
|
{
|
|
|
|
return filePath(fileType, QString());
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList Package::entryList(const char *fileType) const
|
|
|
|
{
|
|
|
|
if (!d->valid) {
|
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
|
2009-04-07 07:42:30 +02:00
|
|
|
return d->structure->entryList(fileType);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-05-13 01:05:40 +02:00
|
|
|
void Package::setPath(const QString &path)
|
|
|
|
{
|
2010-09-03 21:44:28 +02:00
|
|
|
if (d->structure) {
|
|
|
|
d->structure->setPath(path);
|
|
|
|
d->valid = !d->structure->path().isEmpty();
|
|
|
|
}
|
2009-05-13 01:05:40 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
const QString Package::path() const
|
|
|
|
{
|
2010-09-03 21:44:28 +02:00
|
|
|
return d->structure ? d->structure->path() : QString();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const PackageStructure::Ptr Package::structure() const
|
|
|
|
{
|
|
|
|
return d->structure;
|
|
|
|
}
|
|
|
|
|
2011-05-27 12:26:59 +02:00
|
|
|
void PackagePrivate::updateHash(const QString &basePath, const QString &subPath, const QDir &dir, QCryptographicHash &hash)
|
2009-09-21 23:37:44 +02:00
|
|
|
{
|
|
|
|
// 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()) {
|
2011-05-27 12:26:59 +02:00
|
|
|
hash.addData(subPath.toUtf8());
|
2009-09-21 23:37:44 +02:00
|
|
|
}
|
|
|
|
|
2011-05-27 12:26:59 +02:00
|
|
|
hash.addData(file.toUtf8());
|
2009-09-21 23:37:44 +02:00
|
|
|
|
|
|
|
QFileInfo info(dir.path() + '/' + file);
|
|
|
|
if (info.isSymLink()) {
|
2011-05-27 12:26:59 +02:00
|
|
|
hash.addData(info.symLinkTarget().toUtf8());
|
2009-09-21 23:37:44 +02:00
|
|
|
} else {
|
|
|
|
QFile f(info.filePath());
|
|
|
|
if (f.open(QIODevice::ReadOnly)) {
|
|
|
|
while (!f.atEnd()) {
|
2011-05-27 12:26:59 +02:00
|
|
|
hash.addData(f.read(1024));
|
2009-09-21 23:37:44 +02:00
|
|
|
}
|
|
|
|
} else {
|
2009-09-21 23:48:19 +02:00
|
|
|
kWarning() << "could not add" << f.fileName() << "to the hash; file could not be opened for reading. "
|
|
|
|
<< "permissions fail?" << info.permissions() << info.isFile();
|
2009-09-21 23:37:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (const QString &subDirPath, dir.entryList(QDir::Dirs | filters, sorting)) {
|
|
|
|
const QString relativePath = subPath + subDirPath + '/';
|
2011-05-27 12:26:59 +02:00
|
|
|
hash.addData(relativePath.toUtf8());
|
2009-09-21 23:37:44 +02:00
|
|
|
|
|
|
|
QDir subDir(dir.path());
|
|
|
|
subDir.cd(subDirPath);
|
|
|
|
|
|
|
|
if (subDir.path() != subDir.canonicalPath()) {
|
2011-05-27 12:26:59 +02:00
|
|
|
hash.addData(subDir.canonicalPath().toUtf8());
|
2009-09-21 23:37:44 +02:00
|
|
|
} else {
|
|
|
|
updateHash(basePath, relativePath, subDir, hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-21 23:48:19 +02:00
|
|
|
QString Package::contentsHash() const
|
2009-09-21 23:37:44 +02:00
|
|
|
{
|
2010-09-03 21:44:28 +02:00
|
|
|
if (!d->valid) {
|
|
|
|
kWarning() << "can not create hash due to Package being invalid";
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2011-05-27 12:26:59 +02:00
|
|
|
QCryptographicHash hash(QCryptographicHash::Sha1);
|
2009-09-21 23:48:19 +02:00
|
|
|
QString metadataPath = d->structure->path() + "metadata.desktop";
|
|
|
|
if (QFile::exists(metadataPath)) {
|
|
|
|
QFile f(metadataPath);
|
|
|
|
if (f.open(QIODevice::ReadOnly)) {
|
|
|
|
while (!f.atEnd()) {
|
2011-05-27 12:26:59 +02:00
|
|
|
hash.addData(f.read(1024));
|
2009-09-21 23:48:19 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
kWarning() << "could not add" << f.fileName() << "to the hash; file could not be opened for reading.";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
kWarning() << "no metadata at" << metadataPath;
|
|
|
|
}
|
|
|
|
|
2010-12-15 07:29:36 +01:00
|
|
|
QStringList prefixes = d->structure->contentsPrefixPaths();
|
|
|
|
if (prefixes.isEmpty()) {
|
|
|
|
prefixes << QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (QString prefix, prefixes) {
|
2010-11-04 22:54:14 +01:00
|
|
|
const QString basePath = d->structure->path() + prefix;
|
2010-11-04 20:18:32 +01:00
|
|
|
QDir dir(basePath);
|
|
|
|
|
|
|
|
if (!dir.exists()) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
d->updateHash(basePath, QString(), dir, hash);
|
|
|
|
}
|
2011-05-27 12:26:59 +02:00
|
|
|
|
|
|
|
return hash.result();
|
2009-09-21 23:37:44 +02:00
|
|
|
}
|
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
PackagePrivate::PackagePrivate(const PackageStructure::Ptr st, const QString &p)
|
|
|
|
: structure(st),
|
|
|
|
service(0)
|
|
|
|
{
|
2010-09-03 21:44:28 +02:00
|
|
|
if (structure) {
|
|
|
|
structure->setPath(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
valid = structure && !structure->path().isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
PackagePrivate::PackagePrivate(const PackagePrivate &other)
|
|
|
|
: structure(other.structure),
|
|
|
|
service(other.service),
|
|
|
|
valid(other.valid)
|
|
|
|
{
|
2009-09-02 04:27:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PackagePrivate::~PackagePrivate()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-09-03 21:44:28 +02:00
|
|
|
PackagePrivate &PackagePrivate::operator=(const PackagePrivate &rhs)
|
|
|
|
{
|
|
|
|
structure = rhs.structure;
|
|
|
|
service = rhs.service;
|
|
|
|
valid = rhs.valid;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
} // Namespace
|