From c7b3b42efc3176480695091c72c52dd4333b84a7 Mon Sep 17 00:00:00 2001 From: Tomas Jakobsson Date: Sat, 5 Feb 2022 17:03:07 +0100 Subject: [PATCH] Skip isDirectory check on known directory paths --- es-app/src/Gamelist.cpp | 8 ++++---- es-app/src/MetaData.cpp | 4 ++-- es-core/src/ThemeData.cpp | 4 ++-- es-core/src/utils/FileSystemUtil.cpp | 28 ++++++++++++++-------------- es-core/src/utils/FileSystemUtil.h | 6 +++--- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/es-app/src/Gamelist.cpp b/es-app/src/Gamelist.cpp index c4c712b553..0dd89ca273 100644 --- a/es-app/src/Gamelist.cpp +++ b/es-app/src/Gamelist.cpp @@ -15,7 +15,7 @@ FileData* findOrCreateFile(SystemData* system, const std::string& path, FileType // first, verify that path is within the system's root folder FileData* root = system->getRootFolder(); bool contains = false; - std::string relative = Utils::FileSystem::removeCommonPath(path, root->getPath(), contains); + std::string relative = Utils::FileSystem::removeCommonPath(path, root->getPath(), contains, true); if(!contains) { @@ -117,7 +117,7 @@ void parseGamelist(SystemData* system) FileType type = typeList[i]; for(pugi::xml_node fileNode = root.child(tag); fileNode; fileNode = fileNode.next_sibling(tag)) { - const std::string path = Utils::FileSystem::resolveRelativePath(fileNode.child("path").text().get(), relativeTo, false); + const std::string path = Utils::FileSystem::resolveRelativePath(fileNode.child("path").text().get(), relativeTo, false, true); if(!trustGamelist && !Utils::FileSystem::exists(path)) { @@ -165,7 +165,7 @@ void addFileDataNode(pugi::xml_node& parent, const FileData* file, const char* t //there's something useful in there so we'll keep the node, add the path // try and make the path relative if we can so things still work if we change the rom folder location in the future - newNode.prepend_child("path").text().set(Utils::FileSystem::createRelativePath(file->getPath(), system->getStartPath(), false).c_str()); + newNode.prepend_child("path").text().set(Utils::FileSystem::createRelativePath(file->getPath(), system->getStartPath(), false, true).c_str()); } } @@ -267,7 +267,7 @@ void updateGamelist(SystemData* system) } // apply the same transformation as in Gamelist::parseGamelist - std::string xmlpath = Utils::FileSystem::resolveRelativePath(pathNode.text().get(), relativeTo, false); + std::string xmlpath = Utils::FileSystem::resolveRelativePath(pathNode.text().get(), relativeTo, false, true); for(std::vector::const_iterator cfit = changes.cbegin(); cfit != changes.cend(); ++cfit) { diff --git a/es-app/src/MetaData.cpp b/es-app/src/MetaData.cpp index f5d466b09e..4841030eaf 100644 --- a/es-app/src/MetaData.cpp +++ b/es-app/src/MetaData.cpp @@ -84,7 +84,7 @@ MetaDataList MetaDataList::createFromXML(MetaDataListType type, pugi::xml_node& std::string value = md.text().get(); if (iter->type == MD_PATH) { - value = Utils::FileSystem::resolveRelativePath(value, relativeTo, true); + value = Utils::FileSystem::resolveRelativePath(value, relativeTo, true, true); } mdl.set(iter->key, value); }else{ @@ -112,7 +112,7 @@ void MetaDataList::appendToXML(pugi::xml_node& parent, bool ignoreDefaults, cons // try and make paths relative if we can std::string value = mapIter->second; if (mddIter->type == MD_PATH) - value = Utils::FileSystem::createRelativePath(value, relativeTo, true); + value = Utils::FileSystem::createRelativePath(value, relativeTo, true, true); parent.append_child(mapIter->first.c_str()).text().set(value.c_str()); } diff --git a/es-core/src/ThemeData.cpp b/es-core/src/ThemeData.cpp index cb1b2656c2..52a35c6af4 100644 --- a/es-core/src/ThemeData.cpp +++ b/es-core/src/ThemeData.cpp @@ -288,7 +288,7 @@ void ThemeData::parseIncludes(const pugi::xml_node& root) for(pugi::xml_node node = root.child("include"); node; node = node.next_sibling("include")) { std::string relPath = resolvePlaceholders(node.text().as_string()); - std::string path = Utils::FileSystem::resolveRelativePath(relPath, mPaths.back(), true); + std::string path = Utils::FileSystem::resolveRelativePath(relPath, mPaths.back(), true, false); if(!ResourceManager::getInstance()->fileExists(path)) throw error << "Included file \"" << relPath << "\" not found! (resolved to \"" << path << "\")"; @@ -513,7 +513,7 @@ void ThemeData::parseElement(const pugi::xml_node& root, const std::mapfileExists(path)) { std::stringstream ss; diff --git a/es-core/src/utils/FileSystemUtil.cpp b/es-core/src/utils/FileSystemUtil.cpp index 415fedf09f..1ab9f43fa3 100644 --- a/es-core/src/utils/FileSystemUtil.cpp +++ b/es-core/src/utils/FileSystemUtil.cpp @@ -486,10 +486,10 @@ namespace Utils ////////////////////////////////////////////////////////////////////////// - std::string resolveRelativePath(const std::string& _path, const std::string& _relativeTo, const bool _allowHome) + std::string resolveRelativePath(const std::string& _path, const std::string& _relativeTo, const bool _allowHome, const bool _skipDirectoryCheck) { const std::string path = getGenericPath(_path); - const std::string relativeTo = isDirectory(_relativeTo) ? getGenericPath(_relativeTo) : getParent(_relativeTo); + const std::string relativeTo = (_skipDirectoryCheck || isDirectory(_relativeTo)) ? getGenericPath(_relativeTo) : getParent(_relativeTo); // nothing to resolve if(!path.length()) @@ -503,21 +503,21 @@ namespace Utils if(_allowHome && (path[0] == '~') && (path[1] == '/')) return (getHomePath() + &(path[1])); - // absolute path - if(path[0] == '/') - return path; - - // concatenate paths - return (relativeTo + '/' + path); + // absolute path + if(path[0] == '/') + return path; + + // concatenate paths + return (relativeTo + '/' + path); } // resolveRelativePath ////////////////////////////////////////////////////////////////////////// - std::string createRelativePath(const std::string& _path, const std::string& _relativeTo, const bool _allowHome) + std::string createRelativePath(const std::string& _path, const std::string& _relativeTo, const bool _allowHome, const bool _skipDirectoryCheck) { bool contains = false; - std::string path = removeCommonPath(_path, _relativeTo, contains); + std::string path = removeCommonPath(_path, _relativeTo, contains, _skipDirectoryCheck); // success if(contains) @@ -525,7 +525,7 @@ namespace Utils if(_allowHome) { - path = removeCommonPath(_path, getHomePath(), contains); + path = removeCommonPath(_path, getHomePath(), contains, true); // success if(contains) @@ -539,10 +539,10 @@ namespace Utils ////////////////////////////////////////////////////////////////////////// - std::string removeCommonPath(const std::string& _path, const std::string& _common, bool& _contains) + std::string removeCommonPath(const std::string& _path, const std::string& _common, bool& _contains, const bool _skipDirectoryCheck) { const std::string path = getGenericPath(_path); - const std::string common = isDirectory(_common) ? getGenericPath(_common) : getParent(_common); + const std::string common = (_skipDirectoryCheck || isDirectory(_common)) ? getGenericPath(_common) : getParent(_common); // check if path contains common if(path.find(common) == 0) @@ -648,7 +648,7 @@ namespace Utils { const std::unique_lock lock(mutex); - if (pathExistsIndex.find(_path) == pathExistsIndex.cend()) + if(pathExistsIndex.find(_path) == pathExistsIndex.cend()) { const std::string path = getGenericPath(_path); struct stat64 info; diff --git a/es-core/src/utils/FileSystemUtil.h b/es-core/src/utils/FileSystemUtil.h index 20a00a26f3..ac7a423843 100644 --- a/es-core/src/utils/FileSystemUtil.h +++ b/es-core/src/utils/FileSystemUtil.h @@ -27,9 +27,9 @@ namespace Utils std::string getFileName (const std::string& _path); std::string getStem (const std::string& _path); std::string getExtension (const std::string& _path); - std::string resolveRelativePath(const std::string& _path, const std::string& _relativeTo, const bool _allowHome); - std::string createRelativePath (const std::string& _path, const std::string& _relativeTo, const bool _allowHome); - std::string removeCommonPath (const std::string& _path, const std::string& _common, bool& _contains); + std::string resolveRelativePath(const std::string& _path, const std::string& _relativeTo, const bool _allowHome, const bool _skipDirectoryCheck); + std::string createRelativePath (const std::string& _path, const std::string& _relativeTo, const bool _allowHome, const bool _skipDirectoryCheck); + std::string removeCommonPath (const std::string& _path, const std::string& _common, bool& _contains, const bool _skipDirectoryCheck); std::string resolveSymlink (const std::string& _path); bool removeFile (const std::string& _path); bool createDirectory (const std::string& _path);