Skip to content

Commit

Permalink
Merge pull request #780 from tomaz82/skip_directory_check
Browse files Browse the repository at this point in the history
Skip isDirectory check on known directory paths
  • Loading branch information
tomaz82 authored Feb 13, 2022
2 parents ba62e70 + c7b3b42 commit 9277c35
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
8 changes: 4 additions & 4 deletions es-app/src/Gamelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -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<FileData*>::const_iterator cfit = changes.cbegin(); cfit != changes.cend(); ++cfit)
{
Expand Down
4 changes: 2 additions & 2 deletions es-app/src/MetaData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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());
}
Expand Down
4 changes: 2 additions & 2 deletions es-core/src/ThemeData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 << "\")";

Expand Down Expand Up @@ -513,7 +513,7 @@ void ThemeData::parseElement(const pugi::xml_node& root, const std::map<std::str
break;
case PATH:
{
std::string path = Utils::FileSystem::resolveRelativePath(str, mPaths.back(), true);
std::string path = Utils::FileSystem::resolveRelativePath(str, mPaths.back(), true, false);
if(!ResourceManager::getInstance()->fileExists(path))
{
std::stringstream ss;
Expand Down
28 changes: 14 additions & 14 deletions es-core/src/utils/FileSystemUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -503,29 +503,29 @@ 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)
return ("./" + path);

if(_allowHome)
{
path = removeCommonPath(_path, getHomePath(), contains);
path = removeCommonPath(_path, getHomePath(), contains, true);

// success
if(contains)
Expand All @@ -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)
Expand Down Expand Up @@ -648,7 +648,7 @@ namespace Utils
{
const std::unique_lock<std::recursive_mutex> lock(mutex);

if (pathExistsIndex.find(_path) == pathExistsIndex.cend())
if(pathExistsIndex.find(_path) == pathExistsIndex.cend())
{
const std::string path = getGenericPath(_path);
struct stat64 info;
Expand Down
6 changes: 3 additions & 3 deletions es-core/src/utils/FileSystemUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9277c35

Please sign in to comment.