reapack

Package manager for REAPER
Log | Files | Refs | Submodules | README | LICENSE

commit 7bbd48f02e83cbf4b028a7a1cf380b1320a9ae3f
parent 772f558305e2f522e9b5d7cece9ac672334b0de4
Author: cfillion <cfillion@users.noreply.github.com>
Date:   Mon, 27 Feb 2017 13:32:10 -0500

download: always unlink temporary file on non-success

Diffstat:
Msrc/download.cpp | 8++++++++
Msrc/download.hpp | 1+
Msrc/reapack.cpp | 17+++++------------
Msrc/transaction.cpp | 9+++------
4 files changed, 17 insertions(+), 18 deletions(-)

diff --git a/src/download.cpp b/src/download.cpp @@ -179,6 +179,14 @@ FileDownload::FileDownload(const Path &target, const string &url, setName(target.join()); } +bool FileDownload::save() +{ + if(state() == Success) + return FS::rename(m_path); + else + return FS::remove(m_path.temp()); +} + ostream *FileDownload::openStream() { if(FS::open(m_stream, m_path.temp())) diff --git a/src/download.hpp b/src/download.hpp @@ -85,6 +85,7 @@ public: const NetworkOpts &, int flags = 0); const TempPath &path() const { return m_path; } + bool save(); protected: std::ostream *openStream() override; diff --git a/src/reapack.cpp b/src/reapack.cpp @@ -343,18 +343,11 @@ void ReaPack::doFetchIndex(const Remote &remote, ThreadPool *pool, }; dl->onFinish([=] { - switch(dl->state()) { - case ThreadTask::Success: - if(!FS::rename(dl->path())) - warn(FS::lastError(), AUTO_STR("Write Failed")); - break; - case ThreadTask::Failure: - if(stale || !FS::exists(dl->path().target())) - warn(dl->error().message, AUTO_STR("Download Failed")); - break; - default: - break; - } + if(!dl->save()) + warn(FS::lastError(), AUTO_STR("Write Failed")); + else if(dl->state() == ThreadTask::Failure && + (stale || !FS::exists(dl->path().target()))) + warn(dl->error().message, AUTO_STR("Download Failed")); }); pool->push(dl); diff --git a/src/transaction.cpp b/src/transaction.cpp @@ -127,13 +127,10 @@ void Transaction::fetchIndex(const Remote &remote, const function<void()> &cb) } dl->onFinish([=] { - if(dl->state() != ThreadTask::Success) - return; - - if(FS::rename(dl->path())) - cb(); - else + if(!dl->save()) m_receipt.addError({FS::lastError(), dl->path().target().join()}); + else if(dl->state() == ThreadTask::Success) + cb(); }); m_threadPool.push(dl);