diff --git a/cocos/platform/android/CCFileUtils-android.cpp b/cocos/platform/android/CCFileUtils-android.cpp index 6045c1f371..2e3fc7abfe 100644 --- a/cocos/platform/android/CCFileUtils-android.cpp +++ b/cocos/platform/android/CCFileUtils-android.cpp @@ -81,6 +81,58 @@ bool FileUtilsAndroid::init() return FileUtils::init(); } +std::string FileUtilsAndroid::getNewFilename(const std::string &filename) const +{ + std::string newFileName = FileUtils::getNewFilename(filename); + // ../xxx do not fix this path + auto pos = newFileName.find("../"); + if (pos == std::string::npos || pos == 0) + { + return newFileName; + } + + std::vector v(3); + v.resize(0); + auto change = false; + size_t size = newFileName.size(); + size_t idx = 0; + bool noexit = true; + while (noexit) + { + pos = newFileName.find('/', idx); + std::string tmp; + if (pos == std::string::npos) + { + tmp = newFileName.substr(idx, size - idx); + noexit = false; + }else + { + tmp = newFileName.substr(idx, pos - idx + 1); + } + auto t = v.size(); + if (t > 0 && v[t-1].compare("../") != 0 && + (tmp.compare("../") == 0 || tmp.compare("..") == 0)) + { + v.pop_back(); + change = true; + }else + { + v.push_back(tmp); + } + idx = pos + 1; + } + + if (change) + { + newFileName.clear(); + for (auto s : v) + { + newFileName.append(s); + } + } + return newFileName; +} + bool FileUtilsAndroid::isFileExistInternal(const std::string& strFilePath) const { if (strFilePath.empty()) diff --git a/cocos/platform/android/CCFileUtils-android.h b/cocos/platform/android/CCFileUtils-android.h index d2415da2f7..cf590ffebe 100644 --- a/cocos/platform/android/CCFileUtils-android.h +++ b/cocos/platform/android/CCFileUtils-android.h @@ -61,6 +61,8 @@ public: /* override funtions */ bool init(); + std::string getNewFilename(const std::string &filename) const override; + /** @deprecated Please use FileUtils::getDataFromFile or FileUtils::getStringFromFile instead. */ CC_DEPRECATED_ATTRIBUTE virtual unsigned char* getFileData(const std::string& filename, const char* mode, ssize_t * size) override;