GH-1697 always stale files tolerate errors if a local copy is present

This fixes the situation when liteloader snapshot site is broken
and there's an older local snapshot already present.
This commit is contained in:
Petr Mrázek 2016-10-28 02:19:19 +02:00
parent 3d94fb8d24
commit dd0e996081
10 changed files with 64 additions and 11 deletions

View File

@ -131,6 +131,11 @@ QList< std::shared_ptr< NetAction > > Library::getDownloads(OpSys system, class
}
return true;
}
Net::Download::Options options;
if(isAlwaysStale)
{
options |= Net::Download::Option::AcceptLocalFiles;
}
if (isForge)
{
out.append(ForgeXzDownload::make(storage, entry));
@ -140,13 +145,13 @@ QList< std::shared_ptr< NetAction > > Library::getDownloads(OpSys system, class
if(sha1.size())
{
auto rawSha1 = QByteArray::fromHex(sha1.toLatin1());
auto dl = Net::Download::makeCached(url, entry);
auto dl = Net::Download::makeCached(url, entry, options);
dl->addValidator(new Net::ChecksumValidator(QCryptographicHash::Sha1, rawSha1));
out.append(dl);
}
else
out.append(Net::Download::makeCached(url, entry));
out.append(Net::Download::makeCached(url, entry, options));
}
return true;
};

View File

@ -51,6 +51,11 @@ public:
return Job_Failed;
}
bool hasLocalData() override
{
return false;
}
private:
QByteArray * m_output;
};

View File

@ -31,10 +31,11 @@ Download::Download():NetAction()
m_status = Job_NotStarted;
}
Download::Ptr Download::makeCached(QUrl url, MetaEntryPtr entry)
Download::Ptr Download::makeCached(QUrl url, MetaEntryPtr entry, Options options)
{
Download * dl = new Download();
dl->m_url = url;
dl->m_options = options;
auto md5Node = new ChecksumValidator(QCryptographicHash::Md5);
auto cachedNode = new MetaCacheSink(entry, md5Node);
dl->m_sink.reset(cachedNode);
@ -42,18 +43,20 @@ Download::Ptr Download::makeCached(QUrl url, MetaEntryPtr entry)
return std::shared_ptr<Download>(dl);
}
Download::Ptr Download::makeByteArray(QUrl url, QByteArray *output)
Download::Ptr Download::makeByteArray(QUrl url, QByteArray *output, Options options)
{
Download * dl = new Download();
dl->m_url = url;
dl->m_options = options;
dl->m_sink.reset(new ByteArraySink(output));
return std::shared_ptr<Download>(dl);
}
Download::Ptr Download::makeFile(QUrl url, QString path)
Download::Ptr Download::makeFile(QUrl url, QString path, Options options)
{
Download * dl = new Download();
dl->m_url = url;
dl->m_options = options;
dl->m_sink.reset(new FileSink(path));
return std::shared_ptr<Download>(dl);
}
@ -118,6 +121,14 @@ void Download::downloadError(QNetworkReply::NetworkError error)
}
else
{
if(m_options & Option::AcceptLocalFiles)
{
if(m_sink->hasLocalData())
{
m_status = Job_Failed_Proceed;
return;
}
}
// error happened during download.
qCritical() << "Failed " << m_url.toString() << " with reason " << error;
m_status = Job_Failed;
@ -162,7 +173,15 @@ void Download::downloadFinished()
}
// if the download failed before this point ...
if (m_status == Job_Failed)
if (m_status == Job_Failed_Proceed)
{
qDebug() << "Download failed but we are allowed to proceed:" << m_url.toString();
m_sink->abort();
m_reply.reset();
emit succeeded(m_index_within_job);
return;
}
else if (m_status == Job_Failed)
{
qDebug() << "Download failed in previous step:" << m_url.toString();
m_sink->abort();

View File

@ -28,14 +28,20 @@ class MULTIMC_LOGIC_EXPORT Download : public NetAction
public: /* types */
typedef std::shared_ptr<class Download> Ptr;
enum class Option
{
NoOptions = 0,
AcceptLocalFiles = 1
};
Q_DECLARE_FLAGS(Options, Option)
protected: /* con/des */
explicit Download();
public:
virtual ~Download(){};
static Download::Ptr makeCached(QUrl url, MetaEntryPtr entry);
static Download::Ptr makeByteArray(QUrl url, QByteArray *output);
static Download::Ptr makeFile(QUrl url, QString path);
static Download::Ptr makeCached(QUrl url, MetaEntryPtr entry, Options options = Option::NoOptions);
static Download::Ptr makeByteArray(QUrl url, QByteArray *output, Options options = Option::NoOptions);
static Download::Ptr makeFile(QUrl url, QString path, Options options = Option::NoOptions);
public: /* methods */
QString getTargetFilepath()
@ -62,5 +68,8 @@ private: /* data */
// FIXME: remove this, it has no business being here.
QString m_target_path;
std::unique_ptr<Sink> m_sink;
Options m_options;
};
}
Q_DECLARE_OPERATORS_FOR_FLAGS(Net::Download::Options)

View File

@ -97,4 +97,9 @@ JobStatus FileSink::finalizeCache(QNetworkReply &)
return Job_Finished;
}
bool FileSink::hasLocalData()
{
QFileInfo info(m_filename);
return info.exists() && info.size() != 0;
}
}

View File

@ -14,6 +14,7 @@ public: /* methods */
JobStatus write(QByteArray & data) override;
JobStatus abort() override;
JobStatus finalize(QNetworkReply & reply) override;
bool hasLocalData() override;
protected: /* methods */
virtual JobStatus initCache(QNetworkRequest &);

View File

@ -56,4 +56,10 @@ JobStatus MetaCacheSink::finalizeCache(QNetworkReply & reply)
ENV.metacache()->updateEntry(m_entry);
return Job_Finished;
}
bool MetaCacheSink::hasLocalData()
{
QFileInfo info(m_filename);
return info.exists() && info.size() != 0;
}
}

View File

@ -9,6 +9,7 @@ class MetaCacheSink : public FileSink
public: /* con/des */
MetaCacheSink(MetaEntryPtr entry, ChecksumValidator * md5sum);
virtual ~MetaCacheSink();
bool hasLocalData() override;
protected: /* methods */
JobStatus initCache(QNetworkRequest & request) override;

View File

@ -29,7 +29,8 @@ enum JobStatus
Job_InProgress,
Job_Finished,
Job_Failed,
Job_Aborted
Job_Aborted,
Job_Failed_Proceed
};
typedef std::shared_ptr<class NetAction> NetActionPtr;

View File

@ -17,6 +17,7 @@ public: /* methods */
virtual JobStatus write(QByteArray & data) = 0;
virtual JobStatus abort() = 0;
virtual JobStatus finalize(QNetworkReply & reply) = 0;
virtual bool hasLocalData() = 0;
void addValidator(Validator * validator)
{