Commit 31fc7baf authored by Lisa's avatar Lisa

Cache: fixed entry list generation; getting a package in cache updates the...

Cache: fixed entry list generation; getting a package in cache updates the last read date; clean-old and clean-unused actions now clean everything regardless of new size
parent 2417fbcc
......@@ -58,9 +58,14 @@ private:
class Cache::Private
Private(const QDir &cd) : cachedir(cd), cacheFiles(cd.entryList()) {}
Private(const QDir &cd, const QStringList &kp) : cachedir(cd), cacheFiles(cd.entryList()), keepPackages(kp) {}
Private(const QDir &cd) : cachedir(cd), plainEntryList(cd.entryList()) {}
Private(const QDir &cd, const QStringList &kp) : cachedir(cd), plainEntryList(cd.entryList()), keepPackages(kp) {}
~Private() {}
enum RemoveCriteria {
void init();
......@@ -72,7 +77,7 @@ public:
QString getPackageVersion(const QString &);
qint64 totalCacheSize();
bool cleanConditional(bool);
bool cleanConditional(RemoveCriteria criteria, bool half = false);
QDir cachedir;
QStringList cacheFiles;
......@@ -127,7 +132,7 @@ void Cache::Private::init()
* Create a new list of cache entries with their full path.
foreach (QString const& cacheFile, plainEntryList) {
if (!cacheFile.startsWith(".")) {
......@@ -155,23 +160,18 @@ bool Cache::isPackageInCache(const QString &filename) const
QString Cache::getPathInCache(const QString &filename) const
//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.
* TODO: find a better way? (this will probably fail with empty files)
/* QFile filedummy(filename);;
* This dummy read operation (don't worry, it doesn't fail if the file is empty, it returns 0)
* is needed to update the "last read date" of the file, so that we can keep track of which files
* were requested a longer time ago (see the RemoveCriteria enumeration).
QFile filedummy(filename);
if ( {
return m_cachedir.absoluteFilePath(filename);
return QString();*/
return d->cachedir.absoluteFilePath(filename);
......@@ -214,13 +214,13 @@ bool Cache::writePackage(const QString &path, const QString &newName)
return d->cleanAll(); /* no need to go on after this... */
case CleanOld:
if (!d->cleanOld()) {
if (!d->cleanConditional(d->CreationDate, true)) {
return false;
case CleanUnused:
if (!d->cleanUnused()) {
if (!d->cleanConditional(d->LastReadDate, true)) {
return false;
......@@ -233,41 +233,39 @@ bool Cache::writePackage(const QString &path, const QString &newName)
bool Cache::Private::cleanOld()
return cleanConditional(true);
return cleanConditional(CreationDate);
bool Cache::Private::cleanUnused()
return cleanConditional(false);
return cleanConditional(LastReadDate);
* The bool parameter can be extended to support more kind of "comparisons" between packages
* Right now, it just says if the creation date (for CleanOld) or the last read date (for CleanUnused)
* is to be considered as a key to the comparison.
* The bool parameter is needed because to distinguish whether we're removing packages because of an explicit
* request from the user (in that case, it removes all the old/unused packages) or if we're removing because
* the cache has exceeded the maximum size allowed. In this second case, it halves the size (at most).
bool Cache::Private::cleanConditional(bool creationDate)
bool Cache::Private::cleanConditional(RemoveCriteria criteria, bool half)
int newCacheSize( cacheFiles.size() / 2 ); /* each steps halves the cache size */
int newCacheSize( (half) ? cacheFiles.size() / 2 : cacheFiles.size() );
QList<CachePair> timedFiles;
foreach (QString const& file, cacheFiles) {
QFileInfo fileInfo(file);
CachePair pair((creationDate) ? fileInfo.created() : fileInfo.lastRead(), file);
CachePair pair((criteria == CreationDate) ? fileInfo.created() : fileInfo.lastRead(), file);
* Takes only the oldest/least accessed half from the list
foreach (CachePair const& pair, timedFiles.mid(0, newCacheSize)) {
if (pair.key() >= weeks2older || keepPackages.contains(cachedir.relativeFilePath(pair.value()))) {
QFile file(pair.value());
if (!file.remove()) {
error = file.errorString();
return false;
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