From eb16d977c86844c9562e9bbe2e3dc6f58dddcc04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sat, 6 Jul 2013 02:41:23 +0200 Subject: Add a job that removes any files that don't match a whitelist from a folder. --- asset_test.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/asset_test.cpp b/asset_test.cpp index cd2cccd4..fb797b97 100644 --- a/asset_test.cpp +++ b/asset_test.cpp @@ -1,5 +1,7 @@ +#include #include #include + #include "dlqueue.h" inline QDomElement getDomElementByTagName(QDomElement parent, QString tagname) @@ -11,6 +13,51 @@ inline QDomElement getDomElementByTagName(QDomElement parent, QString tagname) return QDomElement(); } +// a job that removes all files from the base folder that don't match the whitelist +// runs in whatever thread owns the queue. it is fast though. +class NukeAndPaveJob: public Job +{ +public: + explicit NukeAndPaveJob(QString base, QStringList whitelist) + :Job() + { + QDir dir(base); + m_base = dir.absolutePath(); + m_whitelist = whitelist; + }; + virtual void start() + { + QDirIterator iter(m_base, QDirIterator::Subdirectories); + QStringList nuke_list; + int base_length = m_base.length(); + while (iter.hasNext()) + { + QString filename = iter.next(); + QFileInfo current(filename); + // we keep the dirs... whatever + if(current.isDir()) + continue; + QString trimmedf = filename; + trimmedf.remove(0, base_length + 1); + if(m_whitelist.contains(trimmedf)) + { + //qDebug() << trimmedf << " gets to live"; + } + else + { + // DO NOT TOLERATE JUNK + //qDebug() << trimmedf << " dies"; + QFile f (filename); + f.remove(); + } + } + emit finish(); + }; +private: + QString m_base; + QStringList m_whitelist; +}; + class DlMachine : public QObject { Q_OBJECT @@ -22,6 +69,10 @@ public slots: void fetchFinished() { + QString prefix("http://s3.amazonaws.com/Minecraft.Resources/"); + QString fprefix("assets/"); + QStringList nuke_whitelist; + JobPtr firstJob = index_job->getFirstJob(); auto DlJob = firstJob.dynamicCast(); QByteArray ba = DlJob->m_data; @@ -64,10 +115,10 @@ public slots: continue; QString trimmedEtag = etagStr.remove('"'); - QString prefix("http://s3.amazonaws.com/Minecraft.Resources/"); - QString fprefix("assets/"); job->add(DownloadJob::create(QUrl(prefix + keyStr),fprefix + keyStr, trimmedEtag)); + nuke_whitelist.append(keyStr); } + job->add(JobPtr(new NukeAndPaveJob(fprefix, nuke_whitelist))); files_job.reset(job); dl.enqueue(files_job); } -- cgit v1.2.3