From b1f93615d977aaea906cf1a1854743cca700d431 Mon Sep 17 00:00:00 2001 From: Maksim Andrianov Date: Wed, 10 Oct 2018 17:53:53 +0300 Subject: [PATCH] [platform] Added RemoveFileIfExists(), TmpPathForFile(). --- platform/platform.cpp | 66 +++++++++++++++++++++++++++++++------------ platform/platform.hpp | 3 ++ 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/platform/platform.cpp b/platform/platform.cpp index 516acf0808..ce9bcbb6bc 100644 --- a/platform/platform.cpp +++ b/platform/platform.cpp @@ -10,7 +10,9 @@ #include "base/logging.hpp" #include "base/string_utils.hpp" +#include #include +#include #include "std/target_os.hpp" @@ -22,6 +24,23 @@ using namespace std; namespace { +std::string RandomString(size_t length) +{ + auto const randchar = []() + { + static std::string const charset = + "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; + size_t const maxIndex = sizeof(charset) - 1; + return charset[rand() % maxIndex]; + }; + + std::string str(length, 0); + std::generate_n(str.begin(), length, randchar); + return str; +} + bool IsSpecialDirName(string const & dirName) { return dirName == "." || dirName == ".."; @@ -44,24 +63,24 @@ Platform::EError Platform::ErrnoToError() { switch (errno) { - case ENOENT: - return ERR_FILE_DOES_NOT_EXIST; - case EACCES: - return ERR_ACCESS_FAILED; - case ENOTEMPTY: - return ERR_DIRECTORY_NOT_EMPTY; - case EEXIST: - return ERR_FILE_ALREADY_EXISTS; - case ENAMETOOLONG: - return ERR_NAME_TOO_LONG; - case ENOTDIR: - return ERR_NOT_A_DIRECTORY; - case ELOOP: - return ERR_SYMLINK_LOOP; - case EIO: - return ERR_IO_ERROR; - default: - return ERR_UNKNOWN; + case ENOENT: + return ERR_FILE_DOES_NOT_EXIST; + case EACCES: + return ERR_ACCESS_FAILED; + case ENOTEMPTY: + return ERR_DIRECTORY_NOT_EMPTY; + case EEXIST: + return ERR_FILE_ALREADY_EXISTS; + case ENAMETOOLONG: + return ERR_NAME_TOO_LONG; + case ENOTDIR: + return ERR_NOT_A_DIRECTORY; + case ELOOP: + return ERR_SYMLINK_LOOP; + case EIO: + return ERR_IO_ERROR; + default: + return ERR_UNKNOWN; } } @@ -146,6 +165,17 @@ string Platform::DefaultUrlsJSON() const return DEFAULT_URLS_JSON; } +bool Platform::RemoveFileIfExists(std::string const & filePath) +{ + return IsFileExistsByFullPath(filePath) ? base::DeleteFileX(filePath) : true; +} + +std::string Platform::TmpPathForFile() const +{ + size_t const kNameLen = 32; + return TmpDir() + RandomString(kNameLen); +} + void Platform::GetFontNames(FilesList & res) const { ASSERT(res.empty(), ()); diff --git a/platform/platform.hpp b/platform/platform.hpp index 3d252901e6..7fa2563884 100644 --- a/platform/platform.hpp +++ b/platform/platform.hpp @@ -137,6 +137,7 @@ public: static bool IsFileExistsByFullPath(std::string const & filePath); static void DisableBackupForFile(std::string const & filePath); + static bool RemoveFileIfExists(std::string const & filePath); /// @returns path to current working directory. /// @note In case of an error returns an empty std::string. @@ -176,6 +177,8 @@ public: std::string const & TmpDir() const { return m_tmpDir; } /// @return full path to file in the temporary directory std::string TmpPathForFile(std::string const & file) const { return TmpDir() + file; } + /// @return full path to temporary file + std::string TmpPathForFile() const; /// @return full path to the file where data for unit tests is stored. std::string TestsDataPathForFile(std::string const & file) const { return ReadPathForFile(file); }