Linux: Find case-insensitive file/folder path if not found (#196)

This commit is contained in:
SSimco 2022-09-09 21:16:46 +03:00 committed by GitHub
parent 6dda53e84f
commit cf598e38c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,26 @@
#include "Common/unix/FileStream_unix.h"
fs::path findPathCI(const fs::path& path)
{
if (fs::exists(path)) return path;
fs::path fName = path.filename();
fs::path parentPath = path.parent_path();
if (!fs::exists(parentPath))
{
auto CIParent = findPathCI(parentPath);
if (fs::exists(CIParent))
return findPathCI(CIParent / fName);
}
std::error_code listErr;
for (auto&& dirEntry : fs::directory_iterator(parentPath, listErr))
if (boost::iequals(dirEntry.path().filename().string(), fName.string()))
return dirEntry;
return parentPath / fName;
}
FileStream* FileStream::openFile(std::string_view path)
{
return openFile2(path, false);
@ -188,14 +209,15 @@ FileStream::~FileStream()
FileStream::FileStream(const fs::path& path, bool isOpen, bool isWriteable)
{
fs::path CIPath = findPathCI(path);
if (isOpen)
{
m_fileStream.open(path, isWriteable ? (std::ios_base::in | std::ios_base::out | std::ios_base::binary) : (std::ios_base::in | std::ios_base::binary));
m_fileStream.open(CIPath, isWriteable ? (std::ios_base::in | std::ios_base::out | std::ios_base::binary) : (std::ios_base::in | std::ios_base::binary));
m_isValid = m_fileStream.is_open();
}
else
{
m_fileStream.open(path, std::ios_base::in | std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
m_fileStream.open(CIPath, std::ios_base::in | std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
m_isValid = m_fileStream.is_open();
}
if(m_isValid && fs::is_directory(path))