diff options
author | Yuri Gorshenin <y@maps.me> | 2015-07-01 16:55:50 +0300 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 02:53:33 +0300 |
commit | 54e5990c7e4c65b0749c21ce4fe3866909a29668 (patch) | |
tree | ec9f80550c7402b7d5f577773353c0d7eb6fb0a7 /platform | |
parent | 025f73ad85d489c4ea143b9d95dde4d308daf89b (diff) |
Revert "Merge pull request #1068 from maps/revert-929-abstract-country-file"
This reverts commit 66aac38c3004d261ee06a16f7e9db372f67614df, reversing
changes made to ad8708944070f6b32a17fbb944d3c493b3fb2a24.
Diffstat (limited to 'platform')
-rw-r--r-- | platform/country_defines.cpp | 22 | ||||
-rw-r--r-- | platform/country_defines.hpp | 8 | ||||
-rw-r--r-- | platform/country_file.cpp | 2 | ||||
-rw-r--r-- | platform/local_country_file.cpp | 13 | ||||
-rw-r--r-- | platform/local_country_file.hpp | 6 | ||||
-rw-r--r-- | platform/local_country_file_utils.cpp | 31 | ||||
-rw-r--r-- | platform/platform.cpp | 8 | ||||
-rw-r--r-- | platform/platform.hpp | 11 | ||||
-rw-r--r-- | platform/platform_android.cpp | 22 | ||||
-rw-r--r-- | platform/platform_ios.mm | 2 | ||||
-rw-r--r-- | platform/platform_tests/local_country_file_tests.cpp | 214 |
11 files changed, 233 insertions, 106 deletions
diff --git a/platform/country_defines.cpp b/platform/country_defines.cpp index 5a3ab9e8f5..927f883709 100644 --- a/platform/country_defines.cpp +++ b/platform/country_defines.cpp @@ -2,14 +2,25 @@ #include "base/assert.hpp" -bool HasOptions(TMapOptions options, TMapOptions bits) +bool HasOptions(TMapOptions mask, TMapOptions options) { - return (static_cast<uint8_t>(options) & static_cast<uint8_t>(bits)) == static_cast<uint8_t>(bits); + return (static_cast<uint8_t>(mask) & static_cast<uint8_t>(options)) == + static_cast<uint8_t>(options); } -TMapOptions SetOptions(TMapOptions options, TMapOptions bits) +TMapOptions SetOptions(TMapOptions mask, TMapOptions options) { - return static_cast<TMapOptions>(static_cast<uint8_t>(options) | static_cast<uint8_t>(bits)); + return static_cast<TMapOptions>(static_cast<uint8_t>(mask) | static_cast<uint8_t>(options)); +} + +TMapOptions UnsetOptions(TMapOptions mask, TMapOptions options) +{ + return static_cast<TMapOptions>(static_cast<uint8_t>(mask) & ~static_cast<uint8_t>(options)); +} + +TMapOptions LeastSignificantOption(TMapOptions mask) +{ + return static_cast<TMapOptions>(static_cast<uint8_t>(mask) & -static_cast<uint8_t>(mask)); } string DebugPrint(TMapOptions options) @@ -24,8 +35,5 @@ string DebugPrint(TMapOptions options) return "CarRouting"; case TMapOptions::EMapWithCarRouting: return "MapWithCarRouting"; - default: - ASSERT(false, ("Unknown TMapOptions (", static_cast<uint8_t>(options), ")")); - return string(); } } diff --git a/platform/country_defines.hpp b/platform/country_defines.hpp index 11e6f97842..5c010cbd13 100644 --- a/platform/country_defines.hpp +++ b/platform/country_defines.hpp @@ -10,8 +10,12 @@ enum class TMapOptions : uint8_t EMapWithCarRouting = 0x3 }; -bool HasOptions(TMapOptions options, TMapOptions bits); +bool HasOptions(TMapOptions mask, TMapOptions options); -TMapOptions SetOptions(TMapOptions options, TMapOptions bits); +TMapOptions SetOptions(TMapOptions mask, TMapOptions options); + +TMapOptions UnsetOptions(TMapOptions mask, TMapOptions options); + +TMapOptions LeastSignificantOption(TMapOptions mask); string DebugPrint(TMapOptions options); diff --git a/platform/country_file.cpp b/platform/country_file.cpp index 8fffb2a52d..b7f4210e8b 100644 --- a/platform/country_file.cpp +++ b/platform/country_file.cpp @@ -22,8 +22,8 @@ string CountryFile::GetNameWithExt(TMapOptions file) const return m_name + DATA_FILE_EXTENSION + ROUTING_FILE_EXTENSION; default: ASSERT(false, ("Can't get name for:", file)); + return string(); } - return string(); } void CountryFile::SetRemoteSizes(uint32_t mapSize, uint32_t routingSize) diff --git a/platform/local_country_file.cpp b/platform/local_country_file.cpp index b730395ad5..2ccf776392 100644 --- a/platform/local_country_file.cpp +++ b/platform/local_country_file.cpp @@ -34,20 +34,23 @@ void LocalCountryFile::SyncWithDisk() Platform & platform = GetPlatform(); - if (platform.GetFileSizeByName(GetPath(TMapOptions::EMap), m_mapSize)) + if (platform.GetFileSizeByFullPath(GetPath(TMapOptions::EMap), m_mapSize)) m_files = SetOptions(m_files, TMapOptions::EMap); string const routingPath = GetPath(TMapOptions::ECarRouting); - if (platform.GetFileSizeByName(routingPath, m_routingSize)) + if (platform.GetFileSizeByFullPath(routingPath, m_routingSize)) m_files = SetOptions(m_files, TMapOptions::ECarRouting); } -void LocalCountryFile::DeleteFromDisk() +void LocalCountryFile::DeleteFromDisk(TMapOptions files) const { for (TMapOptions file : {TMapOptions::EMap, TMapOptions::ECarRouting}) { - if (OnDisk(file)) - VERIFY(my::DeleteFileX(GetPath(file)), (file, "from", *this, "wasn't deleted from disk.")); + if (OnDisk(file) && HasOptions(files, file)) + { + if (!my::DeleteFileX(GetPath(file))) + LOG(LERROR, (file, "from", *this, "wasn't deleted from disk.")); + } } } diff --git a/platform/local_country_file.hpp b/platform/local_country_file.hpp index c73975f772..1a2afa0180 100644 --- a/platform/local_country_file.hpp +++ b/platform/local_country_file.hpp @@ -25,8 +25,9 @@ public: // their sizes etc. with disk. void SyncWithDisk(); - // Removes known country files from disk. - void DeleteFromDisk(); + // Removes specified files from disk if they're known for LocalCountryFile, i.e. + // were found by previous SyncWithDisk() call. + void DeleteFromDisk(TMapOptions files) const; // Returns path to a file. Return value may be empty until // SyncWithDisk() is called. @@ -47,6 +48,7 @@ public: return (static_cast<unsigned>(m_files) & static_cast<unsigned>(filesMask)) == static_cast<unsigned>(filesMask); } + inline string const & GetDirectory() const { return m_directory; } 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 87e76142d3..b5ded8a11c 100644 --- a/platform/local_country_file_utils.cpp +++ b/platform/local_country_file_utils.cpp @@ -1,12 +1,16 @@ #include "platform/local_country_file_utils.hpp" #include "platform/platform.hpp" + #include "coding/file_name_utils.hpp" #include "coding/file_writer.hpp" + #include "base/string_utils.hpp" #include "base/logging.hpp" + #include "std/algorithm.hpp" #include "std/cctype.hpp" +#include "std/sstream.hpp" namespace platform { @@ -26,10 +30,11 @@ void CleanupMapsDirectory() // Remove partially downloaded maps. { Platform::FilesList files; - string const regexp = "\\" DATA_FILE_EXTENSION "\\.(downloading2?$|resume2?$)"; + // .(downloading|resume|ready)[0-9]?$ + string const regexp = "\\.(downloading|resume|ready)[0-9]?$"; platform.GetFilesByRegExp(mapsDir, regexp, files); for (string const & file : files) - FileWriter::DeleteFileX(file); + FileWriter::DeleteFileX(my::JoinFoldersToPath(mapsDir, file)); } // Find and remove Brazil and Japan maps. @@ -41,7 +46,7 @@ void CleanupMapsDirectory() if (countryFile.GetNameWithoutExt() == "Japan" || countryFile.GetNameWithoutExt() == "Brazil") { localFile.SyncWithDisk(); - localFile.DeleteFromDisk(); + localFile.DeleteFromDisk(TMapOptions::EMapWithCarRouting); } } @@ -104,9 +109,10 @@ void FindAllLocalMaps(vector<LocalCountryFile> & localFiles) { int64_t version; if (ParseVersion(subdir, version)) - FindAllLocalMapsInDirectory(my::JoinFoldersToPath(directory, subdir), version, allFiles); + FindAllLocalMapsInDirectory(my::JoinFoldersToPath(directory, subdir), version, localFiles); } } + #if defined(OMIM_OS_ANDROID) // On Android World and WorldCoasts can be stored in alternative /Android/obb/ path. for (string const & file : {WORLD_FILE_NAME, WORLD_COASTS_FILE_NAME}) @@ -135,7 +141,6 @@ void FindAllLocalMaps(vector<LocalCountryFile> & localFiles) } } #endif // defined(OMIM_OS_ANDROID) - localFiles.insert(localFiles.end(), allFiles.begin(), allFiles.end()); } @@ -143,6 +148,7 @@ bool ParseVersion(string const & s, int64_t & version) { if (s.empty() || s.size() > kMaxTimestampLength) return false; + int64_t v = 0; for (char const c : s) { @@ -162,22 +168,29 @@ shared_ptr<LocalCountryFile> PreparePlaceForCountryFiles(CountryFile const & cou return make_shared<LocalCountryFile>(platform.WritableDir(), countryFile, version); string const directory = my::JoinFoldersToPath(platform.WritableDir(), strings::to_string(version)); - switch (platform.MkDir(directory)) + Platform::EError ret = platform.MkDir(directory); + switch (ret) { case Platform::ERR_OK: return make_shared<LocalCountryFile>(directory, countryFile, version); case Platform::ERR_FILE_ALREADY_EXISTS: { Platform::EFileType type; - if (Platform::GetFileType(directory, type) != Platform::ERR_OK || - type != Platform::FILE_TYPE_DIRECTORY) + if (Platform::GetFileType(directory, type) != Platform::ERR_OK) { + LOG(LERROR, ("Can't determine file type for:", directory)); + return shared_ptr<LocalCountryFile>(); + } + if (type != Platform::FILE_TYPE_DIRECTORY) + { + LOG(LERROR, (directory, "exists, but not a directory:", type)); return shared_ptr<LocalCountryFile>(); } return make_shared<LocalCountryFile>(directory, countryFile, version); } default: + LOG(LERROR, ("Can't prepare place for", countryFile, "(", version, ") :", ret)); return shared_ptr<LocalCountryFile>(); - }; + } } } // namespace platform diff --git a/platform/platform.cpp b/platform/platform.cpp index a5b0f720a5..bfffb65a7c 100644 --- a/platform/platform.cpp +++ b/platform/platform.cpp @@ -1,5 +1,7 @@ #include "platform/platform.hpp" +#include "platform/local_country_file.hpp" + #include "coding/sha2.hpp" #include "coding/base64.hpp" #include "coding/file_name_utils.hpp" @@ -147,3 +149,9 @@ string Platform::GetIndexFileName(string const & mwmName, string const & extensi { return GetPlatform().WritablePathForCountryIndexes(mwmName) + mwmName + extension; } + +ModelReader * Platform::GetCountryReader(platform::LocalCountryFile const & file, + TMapOptions options) const +{ + return GetReader(file.GetPath(options), "f"); +} diff --git a/platform/platform.hpp b/platform/platform.hpp index 91f86740d7..7effb78d90 100644 --- a/platform/platform.hpp +++ b/platform/platform.hpp @@ -1,5 +1,7 @@ #pragma once +#include "platform/country_defines.hpp" + #include "coding/reader.hpp" #include "base/exception.hpp" @@ -12,10 +14,14 @@ #include "defines.hpp" - DECLARE_EXCEPTION(FileAbsentException, RootException); DECLARE_EXCEPTION(NotImplementedException, RootException); +namespace platform +{ +class LocalCountryFile; +} + class Platform { public: @@ -109,6 +115,9 @@ public: /// @return full path to file in the settings directory string SettingsPathForFile(string const & file) const { return SettingsDir() + file; } + ModelReader * GetCountryReader(platform::LocalCountryFile const & file, + TMapOptions options) const; + /// @return reader for file decriptor. /// @throws FileAbsentException /// @param[in] file name or full path which we want to read, don't forget to free memory or wrap it to ReaderPtr diff --git a/platform/platform_android.cpp b/platform/platform_android.cpp index 92e6fde61c..225547015e 100644 --- a/platform/platform_android.cpp +++ b/platform/platform_android.cpp @@ -20,8 +20,15 @@ Platform::Platform() namespace { - -enum SourceT { EXTERNAL_RESOURCE, RESOURCE, WRITABLE_PATH, SETTINGS_PATH, FULL_PATH }; +enum SourceT +{ + EXTERNAL_RESOURCE, + RESOURCE, + WRITABLE_PATH, + SETTINGS_PATH, + FULL_PATH, + SOURCE_COUNT +}; bool IsResource(string const & file, string const & ext) { @@ -40,7 +47,8 @@ bool IsResource(string const & file, string const & ext) return true; } -size_t GetSearchSources(string const & file, string const & searchScope, SourceT (&arr)[4]) +size_t GetSearchSources(string const & file, string const & searchScope, + SourceT (&arr)[SOURCE_COUNT]) { size_t ret = 0; @@ -88,7 +96,7 @@ ModelReader * Platform::GetReader(string const & file, string const & searchScop uint32_t const logPageSize = (ext == DATA_FILE_EXTENSION) ? READER_CHUNK_LOG_SIZE : 10; uint32_t const logPageCount = (ext == DATA_FILE_EXTENSION) ? READER_CHUNK_LOG_COUNT : 4; - SourceT sources[4]; + SourceT sources[SOURCE_COUNT]; size_t n = 0; if (searchScope.empty()) @@ -172,6 +180,7 @@ ModelReader * Platform::GetReader(string const & file, string const & searchScop } } + LOG(LERROR, ("Can't get reader for:", file)); MYTHROW(FileAbsentException, ("File not found", file)); return 0; } @@ -238,8 +247,9 @@ bool Platform::GetFileSizeByName(string const & fileName, uint64_t & size) const size = ReaderPtr<Reader>(GetReader(fileName)).Size(); return true; } - catch (RootException const &) + catch (RootException const & ex) { + LOG(LWARNING, ("Can't get file size for:", fileName)); return false; } } @@ -249,7 +259,7 @@ Platform::EError Platform::MkDir(string const & dirName) const if (mkdir(dirName.c_str(), 0755)) { LOG(LWARNING, ("Can't create directory: ", dirName)); - return Platform::ERR_UNKNOWN; + return ErrnoToError(); } return Platform::ERR_OK; } diff --git a/platform/platform_ios.mm b/platform/platform_ios.mm index 5fd1310059..cb14de0171 100644 --- a/platform/platform_ios.mm +++ b/platform/platform_ios.mm @@ -37,7 +37,7 @@ Platform::Platform() Platform::EError Platform::MkDir(string const & dirName) const { if (::mkdir(dirName.c_str(), 0755)) - return Platform::ERR_UNKNOWN; + return ErrnoToError(); return Platform::ERR_OK; } diff --git a/platform/platform_tests/local_country_file_tests.cpp b/platform/platform_tests/local_country_file_tests.cpp index 5594a4e63f..46981142f6 100644 --- a/platform/platform_tests/local_country_file_tests.cpp +++ b/platform/platform_tests/local_country_file_tests.cpp @@ -7,6 +7,7 @@ #include "coding/file_name_utils.hpp" #include "coding/file_writer.hpp" +#include "coding/internal/file_data.hpp" #include "base/scope_guard.hpp" @@ -26,24 +27,30 @@ bool Contains(vector<T> const & v, T const & t) return find(v.begin(), v.end(), t) != v.end(); } +// Scoped test directory in a writable dir. class ScopedTestDir { public: - ScopedTestDir(string const & path) : m_path(path), m_reset(false) + /// Creates test dir in a writable directory. + /// @param path Path for a testing directory, should be relative to writable-dir. + ScopedTestDir(string const & relativePath) + : m_fullPath(my::JoinFoldersToPath(GetPlatform().WritableDir(), relativePath)), + m_relativePath(relativePath), + m_reset(false) { Platform & platform = GetPlatform(); - Platform::EError ret = platform.MkDir(m_path); + Platform::EError ret = platform.MkDir(GetFullPath()); switch (ret) { case Platform::ERR_OK: break; case Platform::ERR_FILE_ALREADY_EXISTS: Platform::EFileType type; - TEST_EQUAL(Platform::ERR_OK, Platform::GetFileType(m_path, type), ()); + TEST_EQUAL(Platform::ERR_OK, Platform::GetFileType(GetFullPath(), type), ()); TEST_EQUAL(Platform::FILE_TYPE_DIRECTORY, type, ()); break; default: - CHECK(false, ("Can't create directory:", m_path, "error:", ret)); + CHECK(false, ("Can't create directory:", GetFullPath(), "error:", ret)); break; } } @@ -53,43 +60,100 @@ public: if (m_reset) return; - Platform::EError ret = Platform::RmDir(m_path); + string const fullPath = GetFullPath(); + Platform::EError ret = Platform::RmDir(fullPath); switch (ret) { case Platform::ERR_OK: break; case Platform::ERR_FILE_DOES_NOT_EXIST: - LOG(LWARNING, (m_path, "was deleted before destruction of ScopedTestDir.")); + LOG(LWARNING, (fullPath, "was deleted before destruction of ScopedTestDir.")); break; case Platform::ERR_DIRECTORY_NOT_EMPTY: - LOG(LWARNING, ("There are files in", m_path)); + LOG(LWARNING, ("There are files in", fullPath)); break; default: - LOG(LWARNING, ("Platform::RmDir() error:", ret)); + LOG(LWARNING, ("Platform::RmDir() error for", fullPath, ":", ret)); break; } } inline void Reset() { m_reset = true; } - inline string const GetPath() const { return m_path; } + inline string const & GetFullPath() const { return m_fullPath; } + + inline string const & GetRelativePath() const { return m_relativePath; } + + bool Exists() const { return GetPlatform().IsFileExistsByFullPath(GetFullPath()); } private: - string const m_path; + string const m_fullPath; + string const m_relativePath; bool m_reset; DISALLOW_COPY_AND_MOVE(ScopedTestDir); }; -void CreateTestFile(string const & testFile, string const & contents) +class ScopedTestFile { +public: + ScopedTestFile(string const & relativePath, string const & contents) + : m_fullPath(my::JoinFoldersToPath(GetPlatform().WritableDir(), relativePath)), m_reset(false) + { + { + FileWriter writer(GetFullPath()); + writer.Write(contents.data(), contents.size()); + } + TEST(Exists(), ("Can't create test file", GetFullPath())); + } + + ScopedTestFile(ScopedTestDir const & dir, CountryFile const & countryFile, TMapOptions file, + string const & contents) + : ScopedTestFile( + my::JoinFoldersToPath(dir.GetRelativePath(), countryFile.GetNameWithExt(file)), + contents) { - FileWriter writer(testFile); - writer.Write(contents.data(), contents.size()); } - TEST(Platform::IsFileExistsByFullPath(testFile), ("Can't create test file", testFile)); + + ~ScopedTestFile() + { + if (m_reset) + return; + if (!Exists()) + { + LOG(LWARNING, ("File", GetFullPath(), "was deleted before dtor of ScopedTestFile.")); + return; + } + if (!my::DeleteFileX(GetFullPath())) + LOG(LWARNING, ("Can't remove test file:", GetFullPath())); + } + + inline string const & GetFullPath() const { return m_fullPath; } + + inline void Reset() { m_reset = true; } + + bool Exists() const { return GetPlatform().IsFileExistsByFullPath(GetFullPath()); } + +private: + string const m_fullPath; + bool m_reset; + + DISALLOW_COPY_AND_MOVE(ScopedTestFile); +}; + +string DebugPrint(ScopedTestDir const & dir) +{ + ostringstream os; + os << "ScopedTestDir [" << dir.GetFullPath() << "]"; + return os.str(); } +string DebugPrint(ScopedTestFile const & file) +{ + ostringstream os; + os << "ScopedTestFile [" << file.GetFullPath() << "]"; + return os.str(); +} } // namespace // Checks that all unsigned numbers less than 10 ^ 18 can be parsed as @@ -160,17 +224,12 @@ UNIT_TEST(LocalCountryFile_DiskFiles) CountryFile countryFile("TestCountry"); countryFile.SetRemoteSizes(1 /* mapSize */, 2 /* routingSize */); - string const testMapFile = - my::JoinFoldersToPath(platform.WritableDir(), countryFile.GetNameWithExt(TMapOptions::EMap)); - string const testRoutingFile = my::JoinFoldersToPath( - platform.WritableDir(), countryFile.GetNameWithExt(TMapOptions::ECarRouting)); - LocalCountryFile localFile(platform.WritableDir(), countryFile, 0 /* version */); TEST(!localFile.OnDisk(TMapOptions::EMap), ()); TEST(!localFile.OnDisk(TMapOptions::ECarRouting), ()); TEST(!localFile.OnDisk(TMapOptions::EMapWithCarRouting), ()); - CreateTestFile(testMapFile, "map"); + ScopedTestFile testMapFile(countryFile.GetNameWithExt(TMapOptions::EMap), "map"); localFile.SyncWithDisk(); TEST(localFile.OnDisk(TMapOptions::EMap), ()); @@ -178,7 +237,7 @@ UNIT_TEST(LocalCountryFile_DiskFiles) TEST(!localFile.OnDisk(TMapOptions::EMapWithCarRouting), ()); TEST_EQUAL(3, localFile.GetSize(TMapOptions::EMap), ()); - CreateTestFile(testRoutingFile, "routing"); + ScopedTestFile testRoutingFile(countryFile.GetNameWithExt(TMapOptions::ECarRouting), "routing"); localFile.SyncWithDisk(); TEST(localFile.OnDisk(TMapOptions::EMap), ()); @@ -188,14 +247,15 @@ UNIT_TEST(LocalCountryFile_DiskFiles) TEST_EQUAL(7, localFile.GetSize(TMapOptions::ECarRouting), ()); TEST_EQUAL(10, localFile.GetSize(TMapOptions::EMapWithCarRouting), ()); - localFile.DeleteFromDisk(); - TEST(!platform.IsFileExistsByFullPath(testMapFile), - ("Map file", testMapFile, "wasn't deleted by LocalCountryFile.")); - TEST(!platform.IsFileExistsByFullPath(testRoutingFile), - ("Routing file", testRoutingFile, "wasn't deleted by LocalCountryFile.")); + localFile.DeleteFromDisk(TMapOptions::EMapWithCarRouting); + TEST(!testMapFile.Exists(), (testMapFile, "wasn't deleted by LocalCountryFile.")); + testMapFile.Reset(); + + TEST(!testRoutingFile.Exists(), (testRoutingFile, "wasn't deleted by LocalCountryFile.")); + testRoutingFile.Reset(); } -UNIT_TEST(LocalCountryFile_DirectoryCleanup) +UNIT_TEST(LocalCountryFile_CleanupMapFiles) { Platform & platform = GetPlatform(); string const mapsDir = platform.WritableDir(); @@ -204,19 +264,19 @@ UNIT_TEST(LocalCountryFile_DirectoryCleanup) CountryFile brazilFile("Brazil"); CountryFile irelandFile("Ireland"); - ScopedTestDir testDir1(my::JoinFoldersToPath(mapsDir, "1")); - LocalCountryFile japanLocalFile(testDir1.GetPath(), japanFile, 1 /* version */); - CreateTestFile(japanLocalFile.GetPath(TMapOptions::EMap), "Japan"); - - ScopedTestDir testDir2(my::JoinFoldersToPath(mapsDir, "2")); - LocalCountryFile brazilLocalFile(testDir2.GetPath(), brazilFile, 2 /* version */); - CreateTestFile(brazilLocalFile.GetPath(TMapOptions::EMap), "Brazil"); + ScopedTestDir testDir1("1"); + LocalCountryFile japanLocalFile(testDir1.GetFullPath(), japanFile, 1 /* version */); + ScopedTestFile japanMapFile(testDir1, japanFile, TMapOptions::EMap, "Japan"); - ScopedTestDir testDir3(my::JoinFoldersToPath(mapsDir, "3")); + ScopedTestDir testDir2("2"); + LocalCountryFile brazilLocalFile(testDir2.GetFullPath(), brazilFile, 2 /* version */); + ScopedTestFile brazilMapFile(testDir2, brazilFile, TMapOptions::EMap, "Brazil"); + LocalCountryFile irelandLocalFile(testDir2.GetFullPath(), irelandFile, 2 /* version */); + ScopedTestFile irelandMapFile(testDir2, irelandFile, TMapOptions::EMap, "Ireland"); - LocalCountryFile irelandLocalFile(testDir2.GetPath(), irelandFile, 2 /* version */); - CreateTestFile(irelandLocalFile.GetPath(TMapOptions::EMap), "Ireland"); + ScopedTestDir testDir3("3"); + // Check that FindAllLocalMaps() vector<LocalCountryFile> localFiles; FindAllLocalMaps(localFiles); TEST(Contains(localFiles, japanLocalFile), (japanLocalFile)); @@ -227,20 +287,47 @@ UNIT_TEST(LocalCountryFile_DirectoryCleanup) japanLocalFile.SyncWithDisk(); TEST_EQUAL(TMapOptions::ENothing, japanLocalFile.GetFiles(), ()); - TEST(!Platform::IsFileExistsByFullPath(testDir1.GetPath()), ("Empty directory wasn't removed.")); + TEST(!testDir1.Exists(), ("Empty directory", testDir1, "wasn't removed.")); testDir1.Reset(); + TEST(!japanMapFile.Exists(), (japanMapFile)); + japanMapFile.Reset(); brazilLocalFile.SyncWithDisk(); TEST_EQUAL(TMapOptions::ENothing, brazilLocalFile.GetFiles(), ()); + TEST(!brazilMapFile.Exists(), (brazilMapFile)); + brazilMapFile.Reset(); irelandLocalFile.SyncWithDisk(); TEST_EQUAL(TMapOptions::EMap, irelandLocalFile.GetFiles(), ()); - irelandLocalFile.DeleteFromDisk(); + irelandLocalFile.DeleteFromDisk(TMapOptions::EMap); + TEST(!irelandMapFile.Exists(), (irelandMapFile)); + irelandMapFile.Reset(); - TEST(!Platform::IsFileExistsByFullPath(testDir3.GetPath()), ("Empty directory wasn't removed.")); + TEST(!testDir3.Exists(), ("Empty directory", testDir3, "wasn't removed.")); testDir3.Reset(); } +UNIT_TEST(LocalCountryFile_CleanupPartiallyDownloadedFiles) +{ + ScopedTestFile toBeDeleted[] = {{"Ireland.mwm.ready", "Ireland"}, + {"Netherlands.mwm.routing.downloading2", "Netherlands"}, + {"Germany.mwm.ready3", "Germany"}, + {"UK_England.mwm.resume4", "UK"}}; + ScopedTestFile toBeKept[] = { + {"Italy.mwm", "Italy"}, {"Spain.mwm", "Spain map"}, {"Spain.mwm.routing", "Spain routing"}}; + + CleanupMapsDirectory(); + + for (ScopedTestFile & file : toBeDeleted) + { + TEST(!file.Exists(), (file)); + file.Reset(); + } + + for (ScopedTestFile & file : toBeKept) + TEST(file.Exists(), (file)); +} + // Creates test-dir and following files: // * test-dir/Ireland.mwm // * test-dir/Netherlands.mwm @@ -253,37 +340,24 @@ UNIT_TEST(LocalCountryFile_DirectoryLookup) CountryFile const irelandFile("Ireland"); CountryFile const netherlandsFile("Netherlands"); - Platform & platform = GetPlatform(); - - ScopedTestDir testDir(my::JoinFoldersToPath(platform.WritableDir(), "test-dir")); - - string const testIrelandMapFile = - my::JoinFoldersToPath(testDir.GetPath(), irelandFile.GetNameWithExt(TMapOptions::EMap)); - CreateTestFile(testIrelandMapFile, "Ireland-map"); - MY_SCOPE_GUARD(removeTestIrelandMapFile, bind(&FileWriter::DeleteFileX, testIrelandMapFile)); + ScopedTestDir testDir("test-dir"); - string const testNetherlandsMapFile = - my::JoinFoldersToPath(testDir.GetPath(), netherlandsFile.GetNameWithExt(TMapOptions::EMap)); - CreateTestFile(testNetherlandsMapFile, "Netherlands-map"); - MY_SCOPE_GUARD(removeTestNetherlandsMapFile, - bind(&FileWriter::DeleteFileX, testNetherlandsMapFile)); - - string const testNetherlandsRoutingFile = my::JoinFoldersToPath( - testDir.GetPath(), netherlandsFile.GetNameWithExt(TMapOptions::ECarRouting)); - CreateTestFile(testNetherlandsRoutingFile, "Netherlands-routing"); - MY_SCOPE_GUARD(removeTestNetherlandsRoutingFile, - bind(&FileWriter::DeleteFileX, testNetherlandsRoutingFile)); + ScopedTestFile testIrelandMapFile(testDir, irelandFile, TMapOptions::EMap, "Ireland-map"); + ScopedTestFile testNetherlandsMapFile(testDir, netherlandsFile, TMapOptions::EMap, + "Netherlands-map"); + ScopedTestFile testNetherlandsRoutingFile(testDir, netherlandsFile, TMapOptions::ECarRouting, + "Netherlands-routing"); vector<LocalCountryFile> localFiles; - FindAllLocalMapsInDirectory(testDir.GetPath(), 150309, localFiles); + FindAllLocalMapsInDirectory(testDir.GetFullPath(), 150309, localFiles); sort(localFiles.begin(), localFiles.end()); for (LocalCountryFile & localFile : localFiles) localFile.SyncWithDisk(); - LocalCountryFile expectedIrelandFile(testDir.GetPath(), irelandFile, 150309); + LocalCountryFile expectedIrelandFile(testDir.GetFullPath(), irelandFile, 150309); expectedIrelandFile.m_files = TMapOptions::EMap; - LocalCountryFile expectedNetherlandsFile(testDir.GetPath(), netherlandsFile, 150309); + LocalCountryFile expectedNetherlandsFile(testDir.GetFullPath(), netherlandsFile, 150309); expectedNetherlandsFile.m_files = TMapOptions::EMapWithCarRouting; vector<LocalCountryFile> expectedLocalFiles = {expectedIrelandFile, expectedNetherlandsFile}; @@ -301,12 +375,8 @@ UNIT_TEST(LocalCountryFile_AllLocalFilesLookup) Platform & platform = GetPlatform(); - ScopedTestDir testDir(my::JoinFoldersToPath(platform.WritableDir(), "010101")); - - string const testItalyMapFile = - my::JoinFoldersToPath(testDir.GetPath(), italyFile.GetNameWithExt(TMapOptions::EMap)); - CreateTestFile(testItalyMapFile, "Italy-map"); - MY_SCOPE_GUARD(remoteTestItalyMapFile, bind(&FileWriter::DeleteFileX, testItalyMapFile)); + ScopedTestDir testDir("010101"); + ScopedTestFile testItalyMapFile(testDir, italyFile, TMapOptions::EMap, "Italy-map"); vector<LocalCountryFile> localFiles; FindAllLocalMaps(localFiles); @@ -320,7 +390,7 @@ UNIT_TEST(LocalCountryFile_AllLocalFilesLookup) CountryFile(WORLD_COASTS_FILE_NAME), 0 /* version */); TEST_EQUAL(1, localFilesSet.count(expectedWorldCoastsFile), ()); - LocalCountryFile expectedItalyFile(testDir.GetPath(), italyFile, 10101); + LocalCountryFile expectedItalyFile(testDir.GetFullPath(), italyFile, 10101); TEST_EQUAL(1, localFilesSet.count(expectedItalyFile), ()); } @@ -335,17 +405,17 @@ UNIT_TEST(LocalCountryFile_PreparePlaceForCountryFiles) TEST(italyLocalFile.get(), ()); TEST_EQUAL(expectedItalyFile, *italyLocalFile, ()); - ScopedTestDir directoryForV1(my::JoinFoldersToPath(platform.WritableDir(), "1")); + ScopedTestDir directoryForV1("1"); CountryFile germanyFile("Germany"); - LocalCountryFile expectedGermanyFile(directoryForV1.GetPath(), germanyFile, 1 /* version */); + LocalCountryFile expectedGermanyFile(directoryForV1.GetFullPath(), germanyFile, 1 /* version */); shared_ptr<LocalCountryFile> germanyLocalFile = PreparePlaceForCountryFiles(germanyFile, 1 /* version */); TEST(germanyLocalFile.get(), ()); TEST_EQUAL(expectedGermanyFile, *germanyLocalFile, ()); CountryFile franceFile("France"); - LocalCountryFile expectedFranceFile(directoryForV1.GetPath(), franceFile, 1 /* version */); + LocalCountryFile expectedFranceFile(directoryForV1.GetFullPath(), franceFile, 1 /* version */); shared_ptr<LocalCountryFile> franceLocalFile = PreparePlaceForCountryFiles(franceFile, 1 /* version */); TEST(franceLocalFile.get(), ()); |