diff --git a/platform/chunks_download_strategy.cpp b/platform/chunks_download_strategy.cpp index 06c0ac6a73..6e24acb73b 100644 --- a/platform/chunks_download_strategy.cpp +++ b/platform/chunks_download_strategy.cpp @@ -1,7 +1,5 @@ #include "chunks_download_strategy.hpp" -#include "../defines.hpp" - #include "../coding/file_writer.hpp" #include "../coding/file_reader.hpp" @@ -40,7 +38,6 @@ ChunksDownloadStrategy::GetChunk(RangeT const & range) void ChunksDownloadStrategy::InitChunks(int64_t fileSize, int64_t chunkSize, ChunkStatusT status) { - // init chunks which should be downloaded m_chunks.reserve(fileSize / chunkSize + 2); for (int64_t i = 0; i < fileSize; i += chunkSize) m_chunks.push_back(ChunkT(i, status)); @@ -70,7 +67,7 @@ void ChunksDownloadStrategy::SaveChunks(string const & fName) { try { - FileWriter w(fName + RESUME_FILE_EXTENSION); + FileWriter w(fName); w.Write(&m_chunks[0], sizeof(ChunkT) * m_chunks.size()); return; } @@ -81,7 +78,7 @@ void ChunksDownloadStrategy::SaveChunks(string const & fName) } // Delete if no chunks or some error occured. - (void)FileWriter::DeleteFileX(fName + RESUME_FILE_EXTENSION); + (void)FileWriter::DeleteFileX(fName); } int64_t ChunksDownloadStrategy::LoadOrInitChunks( string const & fName, @@ -92,7 +89,7 @@ int64_t ChunksDownloadStrategy::LoadOrInitChunks( string const & fName, try { - FileReader r(fName + RESUME_FILE_EXTENSION); + FileReader r(fName); // Load chunks. size_t const count = r.Size() / sizeof(ChunkT); @@ -106,7 +103,7 @@ int64_t ChunksDownloadStrategy::LoadOrInitChunks( string const & fName, if (m_chunks[i].m_status != CHUNK_COMPLETE) m_chunks[i].m_status = CHUNK_FREE; else - downloadedSize += m_chunks[i+1].m_pos - m_chunks[i].m_pos; + downloadedSize += (m_chunks[i+1].m_pos - m_chunks[i].m_pos); } return downloadedSize; diff --git a/platform/chunks_download_strategy.hpp b/platform/chunks_download_strategy.hpp index 2084c0e611..07a7fbefad 100644 --- a/platform/chunks_download_strategy.hpp +++ b/platform/chunks_download_strategy.hpp @@ -54,13 +54,17 @@ private: public: ChunksDownloadStrategy(vector const & urls); + /// Init chunks vector for fileSize. void InitChunks(int64_t fileSize, int64_t chunkSize, ChunkStatusT status = CHUNK_FREE); + + /// Used in unit tests only! void AddChunk(RangeT const & range, ChunkStatusT status); void SaveChunks(string const & fName); /// @return Already downloaded size. int64_t LoadOrInitChunks(string const & fName, int64_t fileSize, int64_t chunkSize); + /// Should be called for every completed chunk (no matter successful or not). void ChunkFinished(bool success, RangeT const & range); enum ResultT diff --git a/platform/http_request.cpp b/platform/http_request.cpp index 61c5ccff1c..2ce8fdb720 100644 --- a/platform/http_request.cpp +++ b/platform/http_request.cpp @@ -182,7 +182,7 @@ class FileHttpRequest : public HttpRequest, public IHttpThreadCallback // save information for download resume ++m_goodChunksCount; if (m_status != ECompleted && m_goodChunksCount % 10 == 0) - m_strategy.SaveChunks(m_filePath); + m_strategy.SaveChunks(m_filePath + RESUME_FILE_EXTENSION); } if (m_status != EInProgress) @@ -195,12 +195,12 @@ class FileHttpRequest : public HttpRequest, public IHttpThreadCallback my::DeleteFileX(m_filePath + RESUME_FILE_EXTENSION); // Rename finished file to it's original name. - CHECK(my::RenameFileX((m_filePath + DOWNLOADING_FILE_EXTENSION).c_str(), m_filePath.c_str()), ()); + CHECK(my::RenameFileX(m_filePath + DOWNLOADING_FILE_EXTENSION, m_filePath), ()); - DisableBackupForFile(m_filePath.c_str()); + DisableBackupForFile(m_filePath); } else // or save "chunks left" otherwise - m_strategy.SaveChunks(m_filePath); + m_strategy.SaveChunks(m_filePath + RESUME_FILE_EXTENSION); m_onFinish(*this); } @@ -216,7 +216,8 @@ public: { ASSERT ( !urls.empty(), () ); - m_progress.first = m_strategy.LoadOrInitChunks(m_filePath, fileSize, chunkSize); + m_progress.first = m_strategy.LoadOrInitChunks(m_filePath + RESUME_FILE_EXTENSION, + fileSize, chunkSize); m_progress.second = fileSize; #ifdef OMIM_OS_IPHONE @@ -239,7 +240,7 @@ public: if (m_doCleanProgressFiles) { - CHECK(my::DeleteFileX(m_filePath + DOWNLOADING_FILE_EXTENSION), ()); + my::DeleteFileX(m_filePath + DOWNLOADING_FILE_EXTENSION); my::DeleteFileX(m_filePath + RESUME_FILE_EXTENSION); } }