Commit 0fe00fa1 authored by Daniele Cocca's avatar Daniele Cocca

QFile doesn't need open()/close() for size() to work. Removed useless...

QFile doesn't need open()/close() for size() to work. Removed useless includes. Some more retouches.
parent 92507f19
/*
/*
* This file is part of the Chakra project
* The cache manager
......@@ -15,12 +15,6 @@
#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.
*/
......@@ -34,25 +28,25 @@ public:
: m_key(key)
, m_value(value)
{}
/*
* qSort() needs this operator to function
*/
bool operator<(const CachePair& other) const
bool operator<(CachePair const& other) const
{
return (m_key < other.m_key); /* we do a date comparison */
}
QDateTime key() const
{
return m_key;
}
QString value() const
{
return m_value;
}
private:
QDateTime m_key;
QString m_value;
......@@ -80,7 +74,7 @@ AkabeiCache::AkabeiCache(QString cachedir, QStringList& keepPackages, QHash< QSt
, m_keepPackages(keepPackages)
{
init();
/*
* Decides the operation to execute
* (more than one can be executed in this same invocation, except for CleanAll of course)
......@@ -98,16 +92,16 @@ AkabeiCache::AkabeiCache(QString cachedir, QStringList& keepPackages, QHash< QSt
}
void AkabeiCache::init()
{
{
/*
* Create a new list of cache entries with their full path.
*/
foreach (const QString& cacheFile, m_plainEntryList) {
foreach (QString const& cacheFile, m_plainEntryList) {
if (!cacheFile.startsWith(".")) {
m_cacheFiles.append(m_cachedir.absoluteFilePath(cacheFile));
}
}
/*
* Find the date exactly 2 weeks earlier than the current day.
* This is useful later because the most "old" or "unused" packages are actually removed
......@@ -131,7 +125,7 @@ QString AkabeiCache::getPathInCache(QString filename)
{
//FIXME: Fix maybe? This should return the path also if file does not exist... why the manual read hack?
//if (m_plainEntryList.contains(filename)) {
/*
* This "dummy" reading operation aims at changing the lastRead date we use
* to know which packages are the least requested in the cache.
......@@ -141,10 +135,10 @@ QString AkabeiCache::getPathInCache(QString filename)
filedummy.open(QIODevice::ReadOnly);
filedummy.readLine();
filedummy.close();
return m_cachedir.absoluteFilePath(filename);
}
return QString();*/
return m_cachedir.absoluteFilePath(filename);
}
......@@ -152,13 +146,13 @@ QString AkabeiCache::getPathInCache(QString filename)
QStringList AkabeiCache::getPackagesByName(QString pkg)
{
QStringList matches;
foreach (const QString& file, m_plainEntryList) {
foreach (QString const& file, m_plainEntryList) {
if (getPackageName(file) == pkg) {
matches.append(file);
}
}
return matches;
}
......@@ -172,42 +166,36 @@ bool AkabeiCache::writePackage(QString path, QString newName)
m_error = "Could not copy \"" + path + "\" to cache.\n";
return false;
}
/* Convert max size in bytes */
qint64 maxsize = (Config::instance()->maxCacheSize()) * 1024 * 1024;
if (maxsize > 0 && totalCacheSize() >= maxsize) {
/* No policy specified in config */
if (Config::instance()->cachePolicies().isEmpty()) {
return cleanAll();
}
foreach (CacheMaxSizePolicy policy, Config::instance()->cachePolicies()) {
switch (policy) {
case CleanAll:
{
return cleanAll(); /* no need to go on after this... */
}
case CleanOld:
{
if (!cleanOld()) {
return false;
}
break;
}
case CleanUnused:
{
if (!cleanUnused()) {
return false;
}
break;
}
case CleanAll:
return cleanAll(); /* no need to go on after this... */
case CleanOld:
if (!cleanOld()) {
return false;
}
break;
case CleanUnused:
if (!cleanUnused()) {
return false;
}
break;
}
}
}
return true;
}
......@@ -228,40 +216,40 @@ bool AkabeiCache::cleanUnused()
*/
bool AkabeiCache::cleanConditional(bool creationDate)
{
int newCacheSize = (int)(m_cacheFiles.size() / 2); /* each steps halves the cache size */
int newCacheSize( m_cacheFiles.size() / 2 ); /* each steps halves the cache size */
QList<CachePair> timedFiles;
foreach (const QString& file, m_cacheFiles) {
foreach (QString const& file, m_cacheFiles) {
QFileInfo fileInfo(file);
CachePair pair((creationDate) ? fileInfo.created() : fileInfo.lastRead(), file);
timedFiles.append(pair);
}
qSort(timedFiles);
/*
* Takes only the oldest/least accessed half from the list
*/
foreach (CachePair pair, timedFiles.mid(0, newCacheSize)) {
foreach (CachePair const& pair, timedFiles.mid(0, newCacheSize)) {
if (pair.key() >= m_weeks2older || m_keepPackages.contains(m_cachedir.relativeFilePath(pair.value()))) {
continue;
}
QFile file(pair.value());
if (!file.remove()) {
m_error = file.errorString();
return false;
}
}
return true;
}
bool AkabeiCache::cleanAll()
{
foreach (const QString& file, m_plainEntryList) {
foreach (QString const& file, m_plainEntryList) {
QString packageName = getPackageName(file);
if (file.startsWith(".") || m_keepPackages.contains(packageName)) {
continue;
}
......@@ -272,7 +260,7 @@ bool AkabeiCache::cleanAll()
return false;
}
}
return true;
}
......@@ -282,49 +270,46 @@ bool AkabeiCache::cleanAll()
*/
QString AkabeiCache::getPackageName(const QString& filename)
{
QString pkgname = "";
foreach (const QString& token, filename.split("-")) {
QString pkgname;
foreach (QString const& token, filename.split("-")) {
if (token.isEmpty() || token[0].isNumber()) {
break;
}
pkgname += token;
pkgname += "-";
}
pkgname.resize(pkgname.size()-1);
pkgname.resize(pkgname.size() - 1);
return pkgname;
}
QString AkabeiCache::getPackageVersion(const QString& filename)
{
QString pkgver = "";
foreach (const QString& token, filename.split("-")) {
QString pkgver;
foreach (QString const& token, filename.split("-")) {
if (token.isEmpty() || !token[0].isNumber()) {
continue;
}
pkgver += token;
pkgver += "-";
}
pkgver.resize(pkgver.size()-1);
pkgver.resize(pkgver.size() - 1);
return pkgver;
}
qint64 AkabeiCache::totalCacheSize()
{
qint64 total = 0;
foreach (const QString& filename, m_cacheFiles) {
QFile file(filename);
file.open(QIODevice::ReadOnly);
total += file.size();
file.close();
qint64 total( 0 );
foreach (QString const& filename, m_cacheFiles) {
total += QFile( filename ).size();
}
return total;
}
......
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