From 087e13038e7a2237142b83c84392976ce0150fb6 Mon Sep 17 00:00:00 2001 From: Yuri Gorshenin Date: Mon, 6 Jul 2015 11:31:18 +0300 Subject: [PATCH] GetCountryFile().GetNameWithoutExt() -> GetCountryName() --- indexer/indexer_tests/mwm_set_test.cpp | 2 +- indexer/mwm_set.cpp | 2 +- indexer/mwm_set.hpp | 2 +- map/feature_vec_model.cpp | 2 +- map/framework.cpp | 2 +- platform/local_country_file.hpp | 1 + platform/local_country_file_utils.cpp | 32 ++++++----- platform/local_country_file_utils.hpp | 1 + .../local_country_file_tests.cpp | 54 ++++++++++++++----- routing/routing_mapping.cpp | 2 +- storage/storage.cpp | 4 +- 11 files changed, 66 insertions(+), 38 deletions(-) diff --git a/indexer/indexer_tests/mwm_set_test.cpp b/indexer/indexer_tests/mwm_set_test.cpp index 7c80aac549..f11a1549ed 100644 --- a/indexer/indexer_tests/mwm_set_test.cpp +++ b/indexer/indexer_tests/mwm_set_test.cpp @@ -24,7 +24,7 @@ protected: // MwmSet overrides: bool GetVersion(LocalCountryFile const & localFile, MwmInfo & info) const override { - int const n = localFile.GetCountryFile().GetNameWithoutExt()[0] - '0'; + int const n = localFile.GetCountryName()[0] - '0'; info.m_maxScale = n; info.m_limitRect = m2::RectD(0, 0, 1, 1); info.m_version.format = version::lastFormat; diff --git a/indexer/mwm_set.cpp b/indexer/mwm_set.cpp index 2dab311225..76d21b8656 100644 --- a/indexer/mwm_set.cpp +++ b/indexer/mwm_set.cpp @@ -125,7 +125,7 @@ pair MwmSet::RegisterImpl(LocalCountryFile const & localF return make_pair(MwmLock(), false); info->SetStatus(MwmInfo::STATUS_REGISTERED); info->m_file = localFile; - string const name = localFile.GetCountryFile().GetNameWithoutExt(); + string const name = localFile.GetCountryName(); vector> & infos = m_info[name]; infos.push_back(info); diff --git a/indexer/mwm_set.hpp b/indexer/mwm_set.hpp index 348bfb9d7a..7c248a6d91 100644 --- a/indexer/mwm_set.hpp +++ b/indexer/mwm_set.hpp @@ -57,7 +57,7 @@ public: inline platform::LocalCountryFile const & GetLocalFile() const { return m_file; } - inline string GetCountryName() const { return m_file.GetCountryFile().GetNameWithoutExt(); } + inline string GetCountryName() const { return m_file.GetCountryName(); } inline int64_t GetVersion() const { return m_file.GetVersion(); } diff --git a/map/feature_vec_model.cpp b/map/feature_vec_model.cpp index 277f9e4371..131eea0b83 100644 --- a/map/feature_vec_model.cpp +++ b/map/feature_vec_model.cpp @@ -46,7 +46,7 @@ void FeaturesFetcher::InitClassificator() pair FeaturesFetcher::RegisterMap(LocalCountryFile const & localFile) { - string const countryFileName = localFile.GetCountryFile().GetNameWithoutExt(); + string const countryFileName = localFile.GetCountryName(); try { pair result = m_multiIndex.RegisterMap(localFile); diff --git a/map/framework.cpp b/map/framework.cpp index b47c471335..292799d0a5 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -101,7 +101,7 @@ namespace pair Framework::RegisterMap(LocalCountryFile const & localFile) { - string const countryFileName = localFile.GetCountryFile().GetNameWithoutExt(); + string const countryFileName = localFile.GetCountryName(); LOG(LINFO, ("Loading map:", countryFileName)); return m_model.RegisterMap(localFile); } diff --git a/platform/local_country_file.hpp b/platform/local_country_file.hpp index 1a2afa0180..366c2437ff 100644 --- a/platform/local_country_file.hpp +++ b/platform/local_country_file.hpp @@ -50,6 +50,7 @@ public: } inline string const & GetDirectory() const { return m_directory; } + inline string GetCountryName() const { return GetCountryFile().GetNameWithoutExt(); } inline int64_t GetVersion() const { return m_version; } inline CountryFile const & GetCountryFile() const { return m_countryFile; } diff --git a/platform/local_country_file_utils.cpp b/platform/local_country_file_utils.cpp index f462f92e3d..aa650c1a33 100644 --- a/platform/local_country_file_utils.cpp +++ b/platform/local_country_file_utils.cpp @@ -20,7 +20,7 @@ size_t const kMaxTimestampLength = 18; bool IsSpecialFile(string const & file) { return file == "." || file == ".."; } -bool CheckedGetFileType(string const & path, Platform::EFileType & type) +bool GetFileTypeChecked(string const & path, Platform::EFileType & type) { Platform::EError const ret = Platform::GetFileType(path, type); if (ret != Platform::ERR_OK) @@ -31,7 +31,7 @@ bool CheckedGetFileType(string const & path, Platform::EFileType & type) return true; } -bool CheckedMkDir(string const & directory) +bool MkDirChecked(string const & directory) { Platform & platform = GetPlatform(); Platform::EError const ret = platform.MkDir(directory); @@ -42,7 +42,7 @@ bool CheckedMkDir(string const & directory) case Platform::ERR_FILE_ALREADY_EXISTS: { Platform::EFileType type; - if (!CheckedGetFileType(directory, type)) + if (!GetFileTypeChecked(directory, type)) return false; if (type != Platform::FILE_TYPE_DIRECTORY) { @@ -197,7 +197,7 @@ shared_ptr PreparePlaceForCountryFiles(CountryFile const & cou return make_shared(platform.WritableDir(), countryFile, version); string const directory = my::JoinFoldersToPath(platform.WritableDir(), strings::to_string(version)); - if (!CheckedMkDir(directory)) + if (!MkDirChecked(directory)) return shared_ptr(); return make_shared(directory, countryFile, version); } @@ -205,39 +205,37 @@ shared_ptr PreparePlaceForCountryFiles(CountryFile const & cou // static bool CountryIndexes::PreparePlaceOnDisk(LocalCountryFile const & localFile) { - return CheckedMkDir(IndexesDir(localFile)); + return MkDirChecked(IndexesDir(localFile)); } // static bool CountryIndexes::DeleteFromDisk(LocalCountryFile const & localFile) { string const directory = IndexesDir(localFile); + bool ok = true; - vector files; - Platform::GetFilesByRegExp(directory, "\\.*", files); - for (string const & file : files) + for (auto index : {Index::Bits, Index::Nodes, Index::Offsets}) { - if (IsSpecialFile(file)) - continue; - string const path = my::JoinFoldersToPath(directory, file); - if (!my::DeleteFileX(path)) + string const path = GetPath(localFile, index); + if (Platform::IsFileExistsByFullPath(path) && !my::DeleteFileX(path)) + { LOG(LERROR, ("Can't remove country index:", path)); + ok = false; + } } Platform::EError const ret = Platform::RmDir(directory); if (ret != Platform::ERR_OK && ret != Platform::ERR_FILE_DOES_NOT_EXIST) { LOG(LERROR, ("Can't remove indexes directory:", directory, ret)); - return false; + ok = false; } - return true; + return ok; } // static string CountryIndexes::GetPath(LocalCountryFile const & localFile, Index index) { - string const directory = IndexesDir(localFile); - string const name = localFile.GetCountryFile().GetNameWithoutExt(); char const * ext = nullptr; switch (index) { @@ -251,7 +249,7 @@ string CountryIndexes::GetPath(LocalCountryFile const & localFile, Index index) ext = ".offsets"; break; } - return my::JoinFoldersToPath(directory, name + ext); + return my::JoinFoldersToPath(IndexesDir(localFile), localFile.GetCountryName() + ext); } // static diff --git a/platform/local_country_file_utils.hpp b/platform/local_country_file_utils.hpp index 2d3987e0d5..eab092e6f8 100644 --- a/platform/local_country_file_utils.hpp +++ b/platform/local_country_file_utils.hpp @@ -72,6 +72,7 @@ public: private: friend void UnitTest_LocalCountryFile_CountryIndexes(); + friend void UnitTest_LocalCountryFile_DoNotDeleteUserFiles(); static string IndexesDir(LocalCountryFile const & localFile); }; diff --git a/platform/platform_tests/local_country_file_tests.cpp b/platform/platform_tests/local_country_file_tests.cpp index 71989eb980..cb08fa11c4 100644 --- a/platform/platform_tests/local_country_file_tests.cpp +++ b/platform/platform_tests/local_country_file_tests.cpp @@ -9,6 +9,7 @@ #include "coding/file_writer.hpp" #include "coding/internal/file_data.hpp" +#include "base/logging.hpp" #include "base/scope_guard.hpp" #include "defines.hpp" @@ -424,21 +425,17 @@ UNIT_TEST(LocalCountryFile_PreparePlaceForCountryFiles) UNIT_TEST(LocalCountryFile_CountryIndexes) { - Platform & platform = GetPlatform(); + ScopedTestDir testDir("101010"); CountryFile germanyFile("Germany"); - shared_ptr germanyLocalFile = - platform::PreparePlaceForCountryFiles(germanyFile, 101010 /* version */); - TEST(germanyLocalFile.get(), ("Can't prepare place for:", germanyFile)); - TEST_EQUAL(my::JoinFoldersToPath(platform.WritableDir(), "101010"), - germanyLocalFile->GetDirectory(), ()); + LocalCountryFile germanyLocalFile(testDir.GetFullPath(), germanyFile, 101010 /* version */); TEST_EQUAL( - my::JoinFoldersToPath(germanyLocalFile->GetDirectory(), germanyFile.GetNameWithoutExt()), - CountryIndexes::IndexesDir(*germanyLocalFile), ()); - TEST(CountryIndexes::PreparePlaceOnDisk(*germanyLocalFile), - ("Can't prepare place for:", *germanyLocalFile)); + my::JoinFoldersToPath(germanyLocalFile.GetDirectory(), germanyFile.GetNameWithoutExt()), + CountryIndexes::IndexesDir(germanyLocalFile), ()); + TEST(CountryIndexes::PreparePlaceOnDisk(germanyLocalFile), + ("Can't prepare place for:", germanyLocalFile)); - string const bitsPath = CountryIndexes::GetPath(*germanyLocalFile, CountryIndexes::Index::Bits); + string const bitsPath = CountryIndexes::GetPath(germanyLocalFile, CountryIndexes::Index::Bits); TEST(!Platform::IsFileExistsByFullPath(bitsPath), (bitsPath)); { FileWriter writer(bitsPath); @@ -447,9 +444,40 @@ UNIT_TEST(LocalCountryFile_CountryIndexes) } TEST(Platform::IsFileExistsByFullPath(bitsPath), (bitsPath)); - TEST(CountryIndexes::DeleteFromDisk(*germanyLocalFile), - ("Can't delete indexes for:", *germanyLocalFile)); + TEST(CountryIndexes::DeleteFromDisk(germanyLocalFile), + ("Can't delete indexes for:", germanyLocalFile)); TEST(!Platform::IsFileExistsByFullPath(bitsPath), (bitsPath)); } + +UNIT_TEST(LocalCountryFile_DoNotDeleteUserFiles) +{ + my::LogLevel oldLogLevel = my::g_LogLevel; + my::g_LogLevel = LCRITICAL; + MY_SCOPE_GUARD(restoreLogLevel, [&oldLogLevel]() + { + my::g_LogLevel = oldLogLevel; + }); + + ScopedTestDir testDir("101010"); + + CountryFile germanyFile("Germany"); + LocalCountryFile germanyLocalFile(testDir.GetFullPath(), germanyFile, 101010 /* version */); + + TEST(CountryIndexes::PreparePlaceOnDisk(germanyLocalFile), + ("Can't prepare place for:", germanyLocalFile)); + string const userFilePath = + my::JoinFoldersToPath(CountryIndexes::IndexesDir(germanyLocalFile), "user-data.txt"); + { + FileWriter writer(userFilePath); + string const data = "user data"; + writer.Write(data.data(), data.size()); + } + TEST(!CountryIndexes::DeleteFromDisk(germanyLocalFile), + ("Indexes dir should not be deleted for:", germanyLocalFile)); + + TEST(my::DeleteFileX(userFilePath), ("Can't delete test file:", userFilePath)); + TEST(CountryIndexes::DeleteFromDisk(germanyLocalFile), + ("Can't delete indexes for:", germanyLocalFile)); +} } // namespace platform diff --git a/routing/routing_mapping.cpp b/routing/routing_mapping.cpp index a7c6f3bf97..3d73e94726 100644 --- a/routing/routing_mapping.cpp +++ b/routing/routing_mapping.cpp @@ -33,7 +33,7 @@ RoutingMapping::RoutingMapping(LocalCountryFile const & localFile, Index const * : m_mapCounter(0), m_facadeCounter(0), m_crossContextLoaded(0), - m_countryFileName(localFile.GetCountryFile().GetNameWithoutExt()), + m_countryFileName(localFile.GetCountryName()), m_isValid(true), m_error(IRouter::ResultCode::NoError) { diff --git a/storage/storage.cpp b/storage/storage.cpp index 6902c2825e..21abc749b9 100644 --- a/storage/storage.cpp +++ b/storage/storage.cpp @@ -135,7 +135,7 @@ void Storage::RegisterAllLocalMaps() } LocalCountryFile const & localFile = *i; - string const name = localFile.GetCountryFile().GetNameWithoutExt(); + string const name = localFile.GetCountryName(); TIndex index = FindIndexByFile(name); if (index.IsValid()) RegisterCountryFiles(index, localFile.GetDirectory(), localFile.GetVersion()); @@ -768,7 +768,7 @@ void Storage::RegisterCountryFiles(shared_ptr localFile) CHECK(localFile.get(), ()); localFile->SyncWithDisk(); - TIndex const index = FindIndexByFile(localFile->GetCountryFile().GetNameWithoutExt()); + TIndex const index = FindIndexByFile(localFile->GetCountryName()); shared_ptr existingFile = GetLocalFile(index, localFile->GetVersion()); if (existingFile.get() != nullptr) ASSERT_EQUAL(localFile.get(), existingFile.get(), ());