Commit 9e73150b authored by Lisa's avatar Lisa

Conversion to new cache manager (still to be tested deeply)

parent f8018747
......@@ -15,6 +15,12 @@
#include "akabeipackage.h"
#include "akabeiconfig.h"
#include <sys/types.h>
#include <utime.h>
#include <time.h>
#include <string.h>
#include <errno.h>
/*
* This is an utility class we use for sorting the cache entries according to some date/time.
*/
......@@ -56,8 +62,8 @@ private:
* This costructor is meant to be used by all the classes that needs to write/get a file from the cache.
* All the options are read from the configuration file later.
*/
AkabeiCache::AkabeiCache(QString cachedir)
: m_cachedir(cachedir)
AkabeiCache::AkabeiCache()
: m_cachedir(Config::instance()->cacheDir())
, m_plainEntryList(m_cachedir.entryList())
{
init();
......@@ -112,17 +118,23 @@ void AkabeiCache::init()
m_weeks2older = current.addDays(-14);
}
bool AkabeiCache::isPackageInCache(QString filename)
{
return m_plainEntryList.contains(filename);
}
/*
* Get the path associated with the filename requested,
* or an empty string if the filename isn't in the cache.
*/
QString AkabeiCache::getPackagePath(QString filename)
{
if (m_plainEntryList.contains(filename)) {
QString AkabeiCache::getPathInCache(QString filename)
{
if (m_plainEntryList.contains(filename)) {
/*
* This fake read has the task of updating the lastRead date we
* use later to see which packages are the least requested of the cache.
* This "dummy" reading operation aims at changing the lastRead date we use
* to know which packages are the least requested in the cache.
* TODO: find a better way? (this will probably fail with empty files)
*/
QFile filedummy(filename);
filedummy.open(QIODevice::ReadOnly);
......@@ -135,13 +147,26 @@ QString AkabeiCache::getPackagePath(QString filename)
return "";
}
QStringList AkabeiCache::getPackagesByName(QString pkg)
{
QStringList matches;
foreach (const QString& file, m_plainEntryList) {
if (getPackageName(file) == pkg) {
matches.append(file);
}
}
return matches;
}
/*
* Writes a new file on cache
* checking if the maximum size has been reached and acting appropriately if so.
*/
bool AkabeiCache::writePackage(QString& path)
bool AkabeiCache::writePackage(QString path, QString newName)
{
if (!QFile::copy(path, m_cachedir.absoluteFilePath(path))) {
if (!QFile::copy(path, m_cachedir.absoluteFilePath(newName))) {
m_error = "Could not copy \"" + path + "\" to cache.\n";
return false;
}
......@@ -215,7 +240,7 @@ bool AkabeiCache::cleanConditional(bool creationDate)
/*
* Takes only the oldest/least accessed half from the list
*/
foreach (CachePair pair, timedFiles.mid(0, newCacheSize)) {
foreach (CachePair pair, timedFiles.mid(0, newCacheSize)) {
if (pair.key() >= m_weeks2older || m_keepPackages.contains(m_cachedir.relativeFilePath(pair.value()))) {
continue;
}
......
......@@ -35,6 +35,7 @@ enum CacheMaxSizePolicy
class AkabeiCache
{
public:
/**
* Constructs a cache manager and executes the requested operations.
*
......@@ -46,10 +47,16 @@ public:
/**
* Constructs a cache manager.
*/
AkabeiCache();
/**
* Checks whether a file is in the cache.
*
* @param cachedir The cache directory
* @param filename filename.
* @returns true if file is present, false otherwise.
*/
AkabeiCache(QString);
bool isPackageInCache(QString filename);
/**
* Find a file in the cache.
......@@ -57,15 +64,24 @@ public:
* @param filename filename.
* @returns the file path, or an empty string if the file isn't found.
*/
QString getPackagePath(QString filename);
QString getPathInCache(QString filename);
/**
* Find a list of files in cache. Useful when you don't care about the version.
*
* @param pkg the name of the package to find.
* @returns a list of files with that package name.
*/
QStringList getPackagesByName(QString pkg);
/**
* Copy a new file in cache.
*
* @param filename the file path.
* @param newName the new filename in cache.
* @returns true if successful, false otherwise.
*/
bool writePackage(QString &path);
bool writePackage(QString path, QString newName);
/**
* Error message.
......
......@@ -16,6 +16,7 @@
#include "akabeibackend.h"
#include "akabeidelta.h"
#include "akabeihook.h"
#include "akabeicache.h"
#include "libarchive++/archivehandler.h"
......@@ -40,9 +41,13 @@ class Package::Version::Data : public QSharedData
void PackagePrivate::attemptLoadFromCache()
{
if (QFile::exists(Config::instance()->cacheDir().absoluteFilePath(filename))) {
AkabeiCache cache;
qDebug() << "cerco in cache" << filename;
if (cache.isPackageInCache(filename)) {
// It does!
pathToArchive = Config::instance()->cacheDir().absoluteFilePath(filename);
qDebug() << "e ci sta!";
pathToArchive = cache.getPathInCache(filename);
_p_validated = false;
_p_md5checked = false;
}
......
......@@ -14,6 +14,7 @@
#include "akabeioperationutils.h"
#include <akabeipackage.h>
#include <akabeicache.h>
#include <akabeiconfig.h>
#include <akabeihelpers.h>
#include <akabeibackend.h>
......@@ -190,6 +191,8 @@ void PlainInstallOperation::run()
if (!(processingOptions().testFlag(Akabei::DatabaseOnly))) {
// Add the archive to the cache
// Simply copy it over to our cachedir
AkabeiCache cache;
cache.writePackage(p->pathToArchive(), p->filename());
QFile::copy(p->pathToArchive(), Config::instance()->cacheDir().absoluteFilePath(p->filename()));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment