diff options
author | Maxim Pimenov <m@maps.me> | 2019-03-13 15:21:49 +0300 |
---|---|---|
committer | mpimenov <mpimenov@users.noreply.github.com> | 2019-03-18 18:42:54 +0300 |
commit | a4b6780b25ddc71d8909d1cefbfa4fff1b45e71b (patch) | |
tree | 92b12a9466a30cd10b1cc027875f891a09fd2cdb /platform | |
parent | e029c9f96105fb83b6a4cdc2ee647d094fb282b4 (diff) |
[storage] Delete the downloaded diff files when cancelled; better logging.
Diffstat (limited to 'platform')
-rw-r--r-- | platform/chunks_download_strategy.cpp | 64 | ||||
-rw-r--r-- | platform/http_request.cpp | 9 | ||||
-rw-r--r-- | platform/local_country_file_utils.cpp | 76 |
3 files changed, 79 insertions, 70 deletions
diff --git a/platform/chunks_download_strategy.cpp b/platform/chunks_download_strategy.cpp index 1d24b45c02..e00fa47c82 100644 --- a/platform/chunks_download_strategy.cpp +++ b/platform/chunks_download_strategy.cpp @@ -1,5 +1,7 @@ #include "platform/chunks_download_strategy.hpp" +#include "platform/platform.hpp" + #include "coding/file_writer.hpp" #include "coding/file_reader.hpp" #include "coding/varint.hpp" @@ -8,7 +10,6 @@ #include "std/algorithm.hpp" - namespace downloader { @@ -80,7 +81,7 @@ void ChunksDownloadStrategy::SaveChunks(int64_t fileSize, string const & fName) } // Delete if no chunks or some error occured. - (void)FileWriter::DeleteFileX(fName); + UNUSED_VALUE(Platform::RemoveFileIfExists(fName)); } int64_t ChunksDownloadStrategy::LoadOrInitChunks(string const & fName, int64_t fileSize, @@ -89,39 +90,42 @@ int64_t ChunksDownloadStrategy::LoadOrInitChunks(string const & fName, int64_t f ASSERT ( fileSize > 0, () ); ASSERT ( chunkSize > 0, () ); - try + if (Platform::IsFileExistsByFullPath(fName)) { - FileReader r(fName); - ReaderSource<FileReader> src(r); - - int64_t const readSize = ReadVarInt<int64_t>(src); - if (readSize == fileSize) + try { - // Load chunks. - uint64_t const size = src.Size(); - int const stSize = sizeof(ChunkT); - size_t const count = size / stSize; - ASSERT_EQUAL(size, stSize * count, ()); - - m_chunks.resize(count); - src.Read(&m_chunks[0], stSize * count); - - // Reset status "downloading" to "free". - int64_t downloadedSize = 0; - for (size_t i = 0; i < count-1; ++i) + FileReader r(fName); + ReaderSource<FileReader> src(r); + + int64_t const readSize = ReadVarInt<int64_t>(src); + if (readSize == fileSize) { - 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); - } + // Load chunks. + uint64_t const size = src.Size(); + int const stSize = sizeof(ChunkT); + size_t const count = size / stSize; + ASSERT_EQUAL(size, stSize * count, ()); + + m_chunks.resize(count); + src.Read(&m_chunks[0], stSize * count); + + // Reset status "downloading" to "free". + int64_t downloadedSize = 0; + for (size_t i = 0; i < count - 1; ++i) + { + 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); + } - return downloadedSize; + return downloadedSize; + } + } + catch (FileReader::Exception const & e) + { + LOG(LDEBUG, (e.Msg())); } - } - catch (FileReader::Exception const & e) - { - LOG(LDEBUG, (e.Msg())); } InitChunks(fileSize, chunkSize); diff --git a/platform/http_request.cpp b/platform/http_request.cpp index 6169c6becf..7c0df58d3f 100644 --- a/platform/http_request.cpp +++ b/platform/http_request.cpp @@ -269,11 +269,10 @@ class FileHttpRequest : public HttpRequest, public IHttpThreadCallback // 3. Clean up resume file with chunks range on success if (m_status == Status::Completed) { - base::DeleteFileX(m_filePath + RESUME_FILE_EXTENSION); + Platform::RemoveFileIfExists(m_filePath + RESUME_FILE_EXTENSION); // Rename finished file to it's original name. - if (Platform::IsFileExistsByFullPath(m_filePath)) - base::DeleteFileX(m_filePath); + Platform::RemoveFileIfExists(m_filePath); CHECK(base::RenameFileX(m_filePath + DOWNLOADING_FILE_EXTENSION, m_filePath), (m_filePath, strerror(errno))); @@ -353,8 +352,8 @@ public: if (m_doCleanProgressFiles) { - base::DeleteFileX(m_filePath + DOWNLOADING_FILE_EXTENSION); - base::DeleteFileX(m_filePath + RESUME_FILE_EXTENSION); + Platform::RemoveFileIfExists(m_filePath + DOWNLOADING_FILE_EXTENSION); + Platform::RemoveFileIfExists(m_filePath + RESUME_FILE_EXTENSION); } } } diff --git a/platform/local_country_file_utils.cpp b/platform/local_country_file_utils.cpp index 282c4c656e..a31ed6afb4 100644 --- a/platform/local_country_file_utils.cpp +++ b/platform/local_country_file_utils.cpp @@ -10,8 +10,8 @@ #include "coding/reader.hpp" #include "base/assert.hpp" -#include "base/string_utils.hpp" #include "base/logging.hpp" +#include "base/string_utils.hpp" #include "std/algorithm.hpp" #include "std/cctype.hpp" @@ -24,7 +24,6 @@ namespace platform { - namespace migrate { // Set of functions to support migration between different versions of MWM @@ -43,10 +42,7 @@ bool NeedMigrate() return true; } -void SetMigrationFlag() -{ - settings::Set("LastMigration", kMinRequiredVersion); -} +void SetMigrationFlag() { settings::Set("LastMigration", kMinRequiredVersion); } } // namespace migrate namespace @@ -136,6 +132,16 @@ void FindAllDiffsInDirectory(string const & dir, vector<LocalCountryFile> & diff diffs.push_back(localDiff); } } + +string GetFilePath(int64_t version, string const & dataDir, CountryFile const & countryFile, + MapOptions options) +{ + string const readyFile = GetFileName(countryFile.GetName(), options, version); + string const dir = GetDataDirFullPath(dataDir); + if (version == 0) + return base::JoinFoldersToPath(dir, readyFile); + return base::JoinFoldersToPath({dir, strings::to_string(version)}, readyFile); +} } // namespace void DeleteDownloaderFilesForCountry(int64_t version, CountryFile const & countryFile) @@ -146,13 +152,19 @@ void DeleteDownloaderFilesForCountry(int64_t version, CountryFile const & countr void DeleteDownloaderFilesForCountry(int64_t version, string const & dataDir, CountryFile const & countryFile) { - for (MapOptions file : {MapOptions::Map, MapOptions::CarRouting, MapOptions::Diff}) + for (MapOptions opt : {MapOptions::Map, MapOptions::CarRouting, MapOptions::Diff}) { - string const path = GetFileDownloadPath(version, dataDir, countryFile, file); + string const path = GetFileDownloadPath(version, dataDir, countryFile, opt); ASSERT(strings::EndsWith(path, READY_FILE_EXTENSION), ()); - base::DeleteFileX(path); - base::DeleteFileX(path + RESUME_FILE_EXTENSION); - base::DeleteFileX(path + DOWNLOADING_FILE_EXTENSION); + Platform::RemoveFileIfExists(path); + Platform::RemoveFileIfExists(path + RESUME_FILE_EXTENSION); + Platform::RemoveFileIfExists(path + DOWNLOADING_FILE_EXTENSION); + } + + // Delete the diff that was downloaded but wasn't applied. + { + string const path = GetFilePath(version, dataDir, countryFile, MapOptions::Diff); + Platform::RemoveFileIfExists(path); } } @@ -262,8 +274,9 @@ void FindAllLocalMapsAndCleanup(int64_t latestVersion, string const & dataDir, // World and WorldCoasts can be stored in app bundle or in resources // directory, thus it's better to get them via Platform. - for (string const & file : { WORLD_FILE_NAME, - (migrate::NeedMigrate() ? WORLD_COASTS_OBSOLETE_FILE_NAME : WORLD_COASTS_FILE_NAME) }) + for (string const & file : + {WORLD_FILE_NAME, + (migrate::NeedMigrate() ? WORLD_COASTS_OBSOLETE_FILE_NAME : WORLD_COASTS_FILE_NAME)}) { auto i = localFiles.begin(); for (; i != localFiles.end(); ++i) @@ -279,8 +292,7 @@ void FindAllLocalMapsAndCleanup(int64_t latestVersion, string const & dataDir, platform.GetReader(file + DATA_FILE_EXTENSION, GetSpecialFilesSearchScope())); // Assume that empty path means the resource file. - LocalCountryFile worldFile{string(), CountryFile(file), - version::ReadVersionDate(reader)}; + LocalCountryFile worldFile{string(), CountryFile(file), version::ReadVersionDate(reader)}; worldFile.m_files = MapOptions::Map; if (i != localFiles.end()) { @@ -323,7 +335,8 @@ bool ParseVersion(string const & s, int64_t & version) return true; } -shared_ptr<LocalCountryFile> PreparePlaceForCountryFiles(int64_t version, CountryFile const & countryFile) +shared_ptr<LocalCountryFile> PreparePlaceForCountryFiles(int64_t version, + CountryFile const & countryFile) { return PreparePlaceForCountryFiles(version, string(), countryFile); } @@ -345,17 +358,19 @@ string GetFileDownloadPath(int64_t version, CountryFile const & countryFile, Map return GetFileDownloadPath(version, string(), countryFile, options); } -string GetFileDownloadPath(int64_t version, string const & dataDir, - CountryFile const & countryFile, MapOptions options) +string GetFileDownloadPath(int64_t version, string const & dataDir, CountryFile const & countryFile, + MapOptions options) { - string const readyFile = GetFileName(countryFile.GetName(), options, version) + READY_FILE_EXTENSION; + string const readyFile = + GetFileName(countryFile.GetName(), options, version) + READY_FILE_EXTENSION; string const dir = GetDataDirFullPath(dataDir); if (version == 0) return base::JoinFoldersToPath(dir, readyFile); return base::JoinFoldersToPath({dir, strings::to_string(version)}, readyFile); } -unique_ptr<ModelReader> GetCountryReader(platform::LocalCountryFile const & file, MapOptions options) +unique_ptr<ModelReader> GetCountryReader(platform::LocalCountryFile const & file, + MapOptions options) { Platform & platform = GetPlatform(); // See LocalCountryFile comment for explanation. @@ -406,15 +421,9 @@ string CountryIndexes::GetPath(LocalCountryFile const & localFile, Index index) char const * ext = nullptr; switch (index) { - case Index::Bits: - ext = kBitsExt; - break; - case Index::Nodes: - ext = kNodesExt; - break; - case Index::Offsets: - ext = kOffsetsExt; - break; + case Index::Bits: ext = kBitsExt; break; + case Index::Nodes: ext = kNodesExt; break; + case Index::Offsets: ext = kOffsetsExt; break; } return base::JoinFoldersToPath(IndexesDir(localFile), localFile.GetCountryName() + ext); } @@ -460,12 +469,9 @@ string DebugPrint(CountryIndexes::Index index) { switch (index) { - case CountryIndexes::Index::Bits: - return "Bits"; - case CountryIndexes::Index::Nodes: - return "Nodes"; - case CountryIndexes::Index::Offsets: - return "Offsets"; + case CountryIndexes::Index::Bits: return "Bits"; + case CountryIndexes::Index::Nodes: return "Nodes"; + case CountryIndexes::Index::Offsets: return "Offsets"; } UNREACHABLE(); } |