From 2b751fe84ebb2850ecea4735dee460cf61af34e5 Mon Sep 17 00:00:00 2001 From: Peter Sykora Date: Sat, 31 Mar 2018 23:43:47 +0200 Subject: [PATCH] [impl] Check if the file from download cache really exists before returning it [impl] Remove downloaded update file if checksum is invalid --- src/CachedDownloader.cpp | 24 +++++++++++++----------- src/ModuleManager.cpp | 1 + 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/CachedDownloader.cpp b/src/CachedDownloader.cpp index bfc4cdd..bdd6060 100644 --- a/src/CachedDownloader.cpp +++ b/src/CachedDownloader.cpp @@ -66,20 +66,22 @@ boost::filesystem::path CachedDownloader::download(const std::string & url) auto it = m_cache.find(url); if (it != m_cache.end()) { - return m_cacheDir / it->second; - } - else - { - auto fileName = boost::filesystem::unique_path(); - auto tmpFile = m_cacheDir / fileName; + auto cachedFilename = m_cacheDir / it->second; + if (boost::filesystem::exists(cachedFilename)) { - std::ofstream os(tmpFile.string(), std::ofstream::binary); - m_httpClient.get(url, os); + return cachedFilename; } - m_cache[url] = fileName.string(); - addToCache(m_cacheDir / cacheListFilename, url, fileName.string()); - return tmpFile; } + + auto fileName = boost::filesystem::unique_path(); + auto tmpFile = m_cacheDir / fileName; + { + std::ofstream os(tmpFile.string(), std::ofstream::binary); + m_httpClient.get(url, os); + } + m_cache[url] = fileName.string(); + addToCache(m_cacheDir / cacheListFilename, url, fileName.string()); + return tmpFile; } void CachedDownloader::clearCache() diff --git a/src/ModuleManager.cpp b/src/ModuleManager.cpp index ab78dae..064b023 100644 --- a/src/ModuleManager.cpp +++ b/src/ModuleManager.cpp @@ -27,6 +27,7 @@ void ModuleManager::applyUpdate(Module & module, const ModuleUpdate & update) auto hash = calcSHA256(updatePath); if (hash != boost::algorithm::to_upper_copy(update.checksum)) { + fs::remove(updatePath); throw std::runtime_error("Integrity check of the update has failed"); }