[impl] Check if the file from download cache really exists before returning it

[impl] Remove downloaded update file if checksum is invalid
This commit is contained in:
Peter Sykora 2018-03-31 23:43:47 +02:00
parent 4e1edd2a33
commit 2b751fe84e
2 changed files with 14 additions and 11 deletions

View File

@ -66,10 +66,13 @@ boost::filesystem::path CachedDownloader::download(const std::string & url)
auto it = m_cache.find(url); auto it = m_cache.find(url);
if (it != m_cache.end()) if (it != m_cache.end())
{ {
return m_cacheDir / it->second; auto cachedFilename = m_cacheDir / it->second;
} if (boost::filesystem::exists(cachedFilename))
else
{ {
return cachedFilename;
}
}
auto fileName = boost::filesystem::unique_path(); auto fileName = boost::filesystem::unique_path();
auto tmpFile = m_cacheDir / fileName; auto tmpFile = m_cacheDir / fileName;
{ {
@ -80,7 +83,6 @@ boost::filesystem::path CachedDownloader::download(const std::string & url)
addToCache(m_cacheDir / cacheListFilename, url, fileName.string()); addToCache(m_cacheDir / cacheListFilename, url, fileName.string());
return tmpFile; return tmpFile;
} }
}
void CachedDownloader::clearCache() void CachedDownloader::clearCache()
{ {

View File

@ -27,6 +27,7 @@ void ModuleManager::applyUpdate(Module & module, const ModuleUpdate & update)
auto hash = calcSHA256(updatePath); auto hash = calcSHA256(updatePath);
if (hash != boost::algorithm::to_upper_copy(update.checksum)) if (hash != boost::algorithm::to_upper_copy(update.checksum))
{ {
fs::remove(updatePath);
throw std::runtime_error("Integrity check of the update has failed"); throw std::runtime_error("Integrity check of the update has failed");
} }