Commit abc614de authored by Lukas Appelhans's avatar Lukas Appelhans

Revert "whitespace; create-db syntax change for now; todo--;"

This reverts commit 8e6b481f.
parent fb951f2d
......@@ -8,16 +8,7 @@
version 2 of the License, or (at your option) any later version.
*/
#include <QtConcurrentRun>
#include <QtConcurrentMap>
#include <QDir>
#include <QFutureWatcher>
#include <QHash>
#include <QVariant>
#include <archive.h>
#include <archive_entry.h>
#include "akabeibackend_p.h"
#include "akabeiconfig.h"
#include "akabeidatabase.h"
......@@ -27,8 +18,15 @@
#include "akabeidelta_p.h"
#include "akabeigroup_p.h"
#include "akabeibackend_p.h"
#include <QtCore/QHash>
#include <QtCore/QtConcurrentRun>
#include <QtCore/QtConcurrentMap>
#include <QtCore/QDir>
#include <QtCore/QFutureWatcher>
#include <QVariant>
#include <archive.h>
#include <archive_entry.h>
Q_DECLARE_METATYPE(QUuid)
Q_DECLARE_METATYPE(QList< Akabei::Package* >)
......@@ -37,6 +35,7 @@ Q_DECLARE_METATYPE(QList< Akabei::Group* >)
namespace Akabei
{
// For concurrent queries
struct ConcurrentPackageQuery
{
ConcurrentPackageQuery(const QString &sql)
......@@ -44,7 +43,10 @@ struct ConcurrentPackageQuery
typedef QList< Package* > result_type;
QList< Package* > operator()(Database *db) { return db->queryPackages(m_sql); }
QList< Package* > operator()(Database *db)
{
return db->queryPackages(m_sql);
}
QString m_sql;
};
......@@ -56,7 +58,10 @@ struct ConcurrentGroupQuery
typedef QList< Group* > result_type;
QList< Group* > operator()(Database *db) { return db->queryGroups(m_sql); }
QList< Group* > operator()(Database *db)
{
return db->queryGroups(m_sql);
}
QString m_sql;
};
......@@ -65,53 +70,66 @@ QHash<QString, Database*> BackendPrivate::performInitialization()
{
QHash<QString, Database*> rethash;
// Check for the existence of all the needed directories
QDir dbDir(Config::instance()->databasePath());
if (!dbDir.exists()) {
qWarning() << "Database dir does not exist";
// Fail
return QHash<QString, Database*>();
}
QDir cacheDir(Config::instance()->cachePath());
if (!cacheDir.exists()) {
qWarning() << "Cache dir does not exist";
// Fail
return QHash<QString, Database*>();
}
QDir rootDir(Config::instance()->root());
if (!rootDir.exists()) {
qWarning() << "Root dir does not exist";
// Fail
return QHash<QString, Database*>();
}
// Initialize all databases. Local DB first.
if (!QFile::exists(dbDir.absoluteFilePath("local.db"))) {
qWarning() << "Couldn't find the local database";
// Fail
return QHash<QString, Database*>();
}
Database *localDb = new Database(dbDir.absoluteFilePath("local.db"));
if (!localDb->isValid()) {
qWarning() << "Local database isn't valid";
// Fail
return QHash<QString, Database*>();
}
// Ok, add it to our rethash
rethash.insert("local", localDb);
// Now process all the various databases
foreach (const QString &db, Config::instance()->databases()) {
if (!QFile::exists(dbDir.absoluteFilePath(db + ".db"))) {
qWarning() << db << " database doesn't exist";
// Fail, but just continue
// TODO, maybe add a warning here
continue;
}
Database *database = new Database(dbDir.absoluteFilePath(db + ".db"));
if (!database->isValid()) {
qWarning() << db << " database isn't valid";
// Fail, but just continue
// TODO, maybe add a warning here
delete database;
continue;
}
// Ok, add it to our rethash
rethash.insert(db, database);
}
// Return it straight
return rethash;
}
......@@ -119,21 +137,23 @@ void BackendPrivate::addDatabase(const QString &name)
{
if (name == "local")
return;
//TODO: Maybe have a private method to be used by the normal database init process as well
if (!QFile::exists(Akabei::Config::instance()->databaseDir().absoluteFilePath(name + ".db"))) {
qWarning() << name << " database doesn't exist";
// Fail, but just continue
// TODO, maybe add a warning here
return;
}
Database *database = new Database(Akabei::Config::instance()->databaseDir().absoluteFilePath(name + ".db"));
if (!database->isValid()) {
qWarning() << name << " database isn't valid";
// Fail, but just continue
// TODO, maybe add a warning here
delete database;
return;
}
databases.append(database);
}
......@@ -147,34 +167,41 @@ void BackendPrivate::setStatus(Backend::Status s)
}
}
void BackendPrivate::_initializationFinished()
void BackendPrivate::__k__initializationFinished()
{
databases.clear();
localDatabase = 0;
// Let's see what we got
QHash< QString, Database* > rethash = initializationWatcher->result();
// Delete the watcher
initializationWatcher->deleteLater();
if (rethash.isEmpty()) {
// Broken
setStatus(Akabei::Backend::StatusBroken);
return;
}
localDatabase = rethash["local"];
if (!localDatabase) {
// Something very wrong here
setStatus(Akabei::Backend::StatusBroken);
return;
}
// All the rest
for (QHash< QString, Database* >::const_iterator i = rethash.constBegin(); i != rethash.constEnd(); ++i) {
if (i.key() == "local") {
// Skip
continue;
}
databases.append(i.value());
}
// The backend is now ready
setStatus(Akabei::Backend::StatusReady);
}
......@@ -182,7 +209,7 @@ void BackendPrivate::packageQueryFinished()
{
Q_Q(Backend);
QUuid uuid = QUuid(sender()->property("_Akabei_Query_Uuid").toString());
QUuid uuid = QUuid(sender()->property("__Akabei_Query_Uuid").toString());
if (!queryPackageFuturePool.contains(uuid)) {
qWarning() << "No such UUID registered!";
emit q->queryPackagesCompleted(uuid, QList<Package*>());
......@@ -191,7 +218,7 @@ void BackendPrivate::packageQueryFinished()
}
QList<Package*> retlist;
// Create the result
QFuture< QList< Package* > > future = queryPackageFuturePool[uuid]->future();
for (QFuture< QList< Package* > >::const_iterator i = future.constBegin(); i != future.constEnd(); ++i) {
retlist << *i;
......@@ -205,7 +232,7 @@ void BackendPrivate::groupQueryFinished()
{
Q_Q(Backend);
QUuid uuid = QUuid(sender()->property("_Akabei_Query_Uuid").toString());
QUuid uuid = QUuid(sender()->property("__Akabei_Query_Uuid").toString());
if (!queryGroupFuturePool.contains(uuid)) {
qWarning() << "No such UUID registered!";
emit q->queryGroupCompleted(uuid, QList<Group*>());
......@@ -213,7 +240,7 @@ void BackendPrivate::groupQueryFinished()
}
QList<Group*> retlist;
// Create the result
QFuture< QList< Group* > > future = queryGroupFuturePool[uuid]->future();
for (QFuture< QList< Group* > >::const_iterator i = future.constBegin(); i != future.constEnd(); ++i) {
retlist << *i;
......@@ -252,7 +279,9 @@ class BackendHelper
{
public:
BackendHelper() : q(0) {}
~BackendHelper() { delete q; }
~BackendHelper() {
delete q;
}
Backend *q;
};
......@@ -260,7 +289,9 @@ Q_GLOBAL_STATIC(BackendHelper, s_globalBackend)
Backend *Backend::instance()
{
if (!s_globalBackend()->q) { new Backend; }
if (!s_globalBackend()->q) {
new Backend;
}
return s_globalBackend()->q;
}
......@@ -275,6 +306,7 @@ Backend::Backend(QObject* parent)
qRegisterMetaType< QList< Akabei::Package* > >();
qRegisterMetaType< QList< Akabei::Group* > >();
// We need at least 2 threads in QThreadPool to make everything work correctly.
if (QThreadPool::globalInstance()->maxThreadCount() < 2) {
QThreadPool::globalInstance()->setMaxThreadCount(2);
}
......@@ -291,7 +323,7 @@ void Backend::initialize()
d->setStatus(StatusInitializing);
d->initializationWatcher = new QFutureWatcher< QHash< QString, Database* > >(this);
connect(d->initializationWatcher, SIGNAL(finished()), this, SLOT(_initializationFinished()));
connect(d->initializationWatcher, SIGNAL(finished()), this, SLOT(__k__initializationFinished()));
QFuture< QHash< QString, Database* > > future = QtConcurrent::run(d, &BackendPrivate::performInitialization);
d->initializationWatcher->setFuture(future);
......@@ -337,7 +369,7 @@ QUuid Backend::queryGroups(const QString& sql)
allDbs << d->localDatabase << d->databases;
QFutureWatcher< QList< Group* > > *watcher = new QFutureWatcher< QList< Group* > >();
watcher->setProperty("_Akabei_Query_Uuid", uuid.toString());
watcher->setProperty("__Akabei_Query_Uuid", uuid.toString());
connect(watcher, SIGNAL(finished()), d, SLOT(groupQueryFinished()));
d->queryGroupFuturePool.insert(uuid, watcher);
......@@ -357,7 +389,7 @@ QUuid Backend::queryPackages(const QString& sql)
allDbs << d->localDatabase << d->databases;
QFutureWatcher< QList< Package* > > *watcher = new QFutureWatcher< QList< Package* > >();
watcher->setProperty("_Akabei_Query_Uuid", uuid.toString());
watcher->setProperty("__Akabei_Query_Uuid", uuid.toString());
connect(watcher, SIGNAL(finished()), d, SLOT(packageQueryFinished()));
d->queryPackageFuturePool.insert(uuid, watcher);
......@@ -421,32 +453,41 @@ Package* Backend::loadPackageFromFile(const QString& path)
const char *entry_name = archive_entry_pathname(entry);
if(strcmp(entry_name, ".PKGINFO") == 0) {
// Go and read from the archive
char fline[512];
while (Helpers::archive_file_readline(archive, fline) != 0) {
QString line(fline);
if (!line.contains('=') || line.startsWith('#')) {
// Skip this line, not interesting
continue;
}
line.remove('\n');
// Separate the two strings
QStringList splitted = line.split('=');
QString key = splitted.first();
// There might be an equal, so
splitted.removeAt(0);
QString value = splitted.join(QChar('='));
// Just remove all spaces ignorantly here
key = key.remove(' ');
// Recheck if it's commented over
if (key.startsWith('#')) {
// Pass by
}
// Strip whitespaces here instead
while (value.startsWith(' ')) {
value = value.remove(0, 1);
}
while (value.endsWith(' ')) {
value = value.remove(value.length() - 1, 1);
}
// Ok, now let's analyze
if (key == "pkgname") {
retpackage->d_func()->name = value;
} else if (key == "pkgver") {
......@@ -476,7 +517,7 @@ Package* Backend::loadPackageFromFile(const QString& path)
} else if (key == "replaces") {
retpackage->d_func()->replaces << value;
} else if (key == "provides") {
retpackage->d_func()->provides << value;
retpackage->d_func()->providers << value;
} else if (key == "backup") {
retpackage->d_func()->backup << value;
} else if (key == "makepkgopt") {
......@@ -515,6 +556,7 @@ Package* Backend::loadPackageFromFile(const QString& path)
}
}
// Some checks
if (ret != ARCHIVE_EOF) {
delete retpackage;
return 0;
......@@ -526,13 +568,19 @@ Package* Backend::loadPackageFromFile(const QString& path)
archive_read_finish(archive);
QFileInfo finfo(path);
// Now add md5sum and filename, of course
retpackage->d_func()->filename = path.split('/').last();
retpackage->d_func()->md5sum = Helpers::md5sumOfFile(path);
// And size
QFileInfo finfo(path);
retpackage->d_func()->size = finfo.size();
// Fill some fields
retpackage->d_func()->installDate = QDateTime();
retpackage->d_func()->reason = Package::NoReason;
// We already computed md5 and validation, so set em up
retpackage->d_func()->_p_md5checked = true;
retpackage->d_func()->_p_validated = true;
......@@ -572,32 +620,41 @@ Delta* Backend::loadDeltaFromFile(const QString& path)
const char *entry_name = archive_entry_pathname(entry);
if(strcmp(entry_name, ".DELTAINFO") == 0) {
// Go and read from the archive
char fline[512];
while (Helpers::archive_file_readline(archive, fline) != 0) {
QString line(fline);
if (!line.contains('=') || line.startsWith('#')) {
// Skip this line, not interesting
continue;
}
line.remove('\n');
// Separate the two strings
QStringList splitted = line.split('=');
QString key = splitted.first();
// There might be an equal, so
splitted.removeAt(0);
QString value = splitted.join(QChar('='));
// Just remove all spaces ignorantly here
key = key.remove(' ');
// Recheck if it's commented over
if (key.startsWith('#')) {
// Pass by
}
// Strip whitespaces here instead
while (value.startsWith(' ')) {
value = value.remove(0, 1);
}
while (value.endsWith(' ')) {
value = value.remove(value.length() - 1, 1);
}
// Ok, now let's analyze
if (key == "pkgname") {
retdelta->d_func()->name = value;
} else if (key == "verfrom") {
......@@ -611,6 +668,7 @@ Delta* Backend::loadDeltaFromFile(const QString& path)
/* for now, ignore all files starting with '.' that haven't
* already been handled (for future possibilities) */
} else {
// It can be only our delta
if (entry_name != path.split('/').last().remove(".tar.xz")) {
delete retdelta;
return 0;
......@@ -623,6 +681,7 @@ Delta* Backend::loadDeltaFromFile(const QString& path)
}
}
// Some checks
if (ret != ARCHIVE_EOF) {
delete retdelta;
return 0;
......@@ -632,8 +691,11 @@ Delta* Backend::loadDeltaFromFile(const QString& path)
return 0;
}
// Now add md5sum and filename, of course
retdelta->d_func()->filename = path.split('/').last();
retdelta->d_func()->md5sum = Helpers::md5sumOfFile(path);
// We already computed md5 and validation, so set em up
retdelta->d_func()->_p_md5checked = true;
retdelta->d_func()->_p_validated = true;
......@@ -643,9 +705,9 @@ Delta* Backend::loadDeltaFromFile(const QString& path)
Group* Backend::loadGroupFromFile(const QString &path)
{
Q_D(Backend);
Group* group = 0;
Group * group = 0;
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return group;
......@@ -663,8 +725,6 @@ Group* Backend::loadGroupFromFile(const QString &path)
group->d_ptr->icon = split.last();
}
}
return group;
}
}
......
......@@ -11,15 +11,12 @@
#ifndef AKABEIBACKEND_H
#define AKABEIBACKEND_H
#include <QObject>
#include <QtCore/QObject>
#include "akabeicore_global.h"
class QUuid;
namespace Akabei {
class Delta;
class OperationRunner;
class Group;
......@@ -27,7 +24,6 @@ class Package;
class Database;
class BackendPrivate;
class AKABEICORESHARED_EXPORT Backend : public QObject
{
Q_OBJECT
......@@ -42,13 +38,12 @@ class AKABEICORESHARED_EXPORT Backend : public QObject
StatusOnTransaction = 4,
StatusBroken = 5
};
static Backend* instance();
static Backend *instance();
virtual ~Backend();
Status status() const;
Database* localDatabase();
Database *localDatabase();
QList<Database*> databases();
QUuid packages();
......@@ -60,11 +55,11 @@ class AKABEICORESHARED_EXPORT Backend : public QObject
QUuid queryPackages(const QString &sql);
QUuid queryGroups(const QString &sql);
OperationRunner* operationRunner();
OperationRunner *operationRunner();
Package* loadPackageFromFile(const QString &path);
Delta* loadDeltaFromFile(const QString &path);
Group* loadGroupFromFile(const QString &path);
Package *loadPackageFromFile(const QString &path);
Delta *loadDeltaFromFile(const QString &path);
Group *loadGroupFromFile(const QString &path);
public Q_SLOTS:
void initialize();
......@@ -77,9 +72,9 @@ class AKABEICORESHARED_EXPORT Backend : public QObject
private:
Backend(QObject* parent = 0);
BackendPrivate* const d_ptr;
BackendPrivate * const d_ptr;
Q_PRIVATE_SLOT(d_func(), void _initializationFinished())
Q_PRIVATE_SLOT(d_func(), void __k__initializationFinished())
friend class DatabasePrivate;
friend class Config;
......
......@@ -11,15 +11,15 @@
#ifndef AKABEIBACKEND_P_H
#define AKABEIBACKEND_P_H
#include <QFileSystemWatcher>
#include <QFutureWatcher>
#include <QUuid>
#include "akabeibackend.h"
#include "akabeioperationrunner.h"
#include <QtCore/QFileSystemWatcher>
#include <QtCore/QFutureWatcher>
#include <QtCore/QUuid>
#include "akabeioperationrunner.h"
namespace Akabei {
namespace Akabei
{
class GroupPool
{
......@@ -41,7 +41,7 @@ class BackendPrivate : public QObject
{
Q_OBJECT
Q_DECLARE_PUBLIC(Backend)
Backend* const q_ptr;
Backend * const q_ptr;
public:
BackendPrivate(Backend *p)
......@@ -57,7 +57,7 @@ class BackendPrivate : public QObject
Backend::Status status;
Database* localDatabase;
Database *localDatabase;
QList<Database*> databases;
QFileSystemWatcher *fsWatcher;
......@@ -69,7 +69,7 @@ class BackendPrivate : public QObject
OperationRunner *runner;
void _initializationFinished();
void __k__initializationFinished();
QHash<QString, Database*> performInitialization();
void setStatus(Backend::Status status);
......
......@@ -8,16 +8,16 @@
version 2 of the License, or (at your option) any later version.
*/
#include <qglobal.h>
#include <QDir>
#include "akabeiconfig.h"
#include "akabeibackend.h"
#include "akabeibackend_p.h"
#include "akabeiconfig.h"
#include <qglobal.h>
#include <QtCore/QDir>
namespace Akabei {
namespace Akabei
{
class Config::Private
{
......@@ -41,8 +41,9 @@ class ConfigHelper
{
public:
ConfigHelper() : q(0) {}
~ConfigHelper() { delete q; }
~ConfigHelper() {
delete q;
}
Config *q;
};
......@@ -50,7 +51,9 @@ Q_GLOBAL_STATIC(ConfigHelper, s_globalConfig)
Config *Config::instance()
{
if (!s_globalConfig()->q) { new Config; }
if (!s_globalConfig()->q) {
new Config;
}
return s_globalConfig()->q;
}
......@@ -61,8 +64,8 @@ Config::Config()
Q_ASSERT(!s_globalConfig()->q);
s_globalConfig()->q = this;
setRoot("/");
setCachePath("/var/cache/akabei");
setDatabasePath("/var/akabei");
setCachePath(Akabei::DEFAULT_CACHE_DIR.toString());
setDatabasePath(Akabei::DEFAULT_DATABASE_DIR.toString());
}
Config::~Config()
......
......@@ -13,17 +13,15 @@
#include <akabeicore_global.h>
class QDir;
class QString;
class QStringList;
class QDir;
namespace Akabei {
class AKABEICORESHARED_EXPORT Config
{
public:
static Config* instance();
static Config *instance();
~Config();
QString root() const;
......
......@@ -11,7 +11,7 @@
#ifndef AKABEICORE_GLOBAL_H
#define AKABEICORE_GLOBAL_H
#include <qglobal.h>
#include <QtCore/qglobal.h>
#include <QUrl>
#if defined(AKABEICORE_LIBRARY)
......@@ -20,47 +20,51 @@
# define AKABEICORESHARED_EXPORT Q_DECL_IMPORT
#endif
// Define column numbers
namespace Akabei {
namespace Tables {
namespace Tables {
namespace Packages {
const int ID = 0;
const int NAME = 1;
const int VERSION = 2;
const int FILENAME = 3;
const int DESCRIPTION = 4;
const int GROUPS = 5;
const int SIZE = 6;
const int INSTALLEDSIZE = 7;
const int MD5SUM = 8;
const int URL = 9;
const int BUILDDATE = 10;
const int ARCH = 11;
const int PACKAGER = 12;
const int PROVIDES = 13;
const int CONFLICTS = 14;
const int DEPENDS = 15;
const int OPTDEPENDS = 16;
const int REPLACES = 17;