From ba1da58f0bd3760865be9ca4c4ed685b6d84eee9 Mon Sep 17 00:00:00 2001 From: Deve Date: Tue, 9 Oct 2018 21:42:16 +0200 Subject: [PATCH] Don't change current working directory. It causes issues with multithreading. Fixes #3498 --- lib/irrlicht/include/IFileSystem.h | 4 ++++ lib/irrlicht/source/Irrlicht/CFileSystem.cpp | 16 ++++++++++++---- lib/irrlicht/source/Irrlicht/CFileSystem.h | 4 ++++ src/io/file_manager.cpp | 18 +++++------------- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/lib/irrlicht/include/IFileSystem.h b/lib/irrlicht/include/IFileSystem.h index f1583631d..3f39f6073 100644 --- a/lib/irrlicht/include/IFileSystem.h +++ b/lib/irrlicht/include/IFileSystem.h @@ -304,6 +304,10 @@ public: See IReferenceCounted::drop() for more information. */ virtual IFileList* createFileList() =0; + //! Creates a list of files and directories in specified directory + //! and returns it. + virtual IFileList* createFileList(const io::path& directory) =0; + //! Creates an empty filelist /** \return a Pointer to the created IFileList is returned. After the list has been used it has to be deleted using its IFileList::drop() method. diff --git a/lib/irrlicht/source/Irrlicht/CFileSystem.cpp b/lib/irrlicht/source/Irrlicht/CFileSystem.cpp index 8056e1805..0e8ac2589 100644 --- a/lib/irrlicht/source/Irrlicht/CFileSystem.cpp +++ b/lib/irrlicht/source/Irrlicht/CFileSystem.cpp @@ -803,12 +803,18 @@ EFileSystemType CFileSystem::setFileListSystem(EFileSystemType listType) return current; } - -//! Creates a list of files and directories in the current working directory +//! Creates a list of files and directories in specified directory IFileList* CFileSystem::createFileList() { - CFileList* r = 0; io::path Path = getWorkingDirectory(); + return createFileList(Path); +} + +//! Creates a list of files and directories in the current working directory +IFileList* CFileSystem::createFileList(const io::path& directory) +{ + CFileList* r = 0; + io::path Path = directory; Path.replace('\\', '/'); if (Path.lastChar() != '/') Path.append('/'); @@ -830,8 +836,10 @@ IFileList* CFileSystem::createFileList() intptr_t hFile; #endif + io::path searchPath = Path; + searchPath.append('*'); struct _tfinddata_t c_file; - if( (hFile = _tfindfirst( _T("*"), &c_file )) != -1L ) + if( (hFile = _tfindfirst( _T(searchPath.c_str()), &c_file )) != -1L ) { do { diff --git a/lib/irrlicht/source/Irrlicht/CFileSystem.h b/lib/irrlicht/source/Irrlicht/CFileSystem.h index 0af5356af..ac91a2dca 100644 --- a/lib/irrlicht/source/Irrlicht/CFileSystem.h +++ b/lib/irrlicht/source/Irrlicht/CFileSystem.h @@ -121,6 +121,10 @@ public: //! and returns it. virtual IFileList* createFileList(); + //! Creates a list of files and directories in specified directory + //! and returns it. + virtual IFileList* createFileList(const io::path& directory); + //! Creates an empty filelist virtual IFileList* createEmptyFileList(const io::path& path, bool ignoreCase, bool ignorePaths); diff --git a/src/io/file_manager.cpp b/src/io/file_manager.cpp index a582261e4..a9ea75763 100644 --- a/src/io/file_manager.cpp +++ b/src/io/file_manager.cpp @@ -1324,25 +1324,17 @@ void FileManager::listFiles(std::set& result, { result.clear(); - if(!isDirectory(dir)) + if (!isDirectory(dir)) return; - io::path previous_cwd = m_file_system->getWorkingDirectory(); + irr::io::IFileList* files = m_file_system->createFileList(dir.c_str()); - if(!m_file_system->changeWorkingDirectoryTo( dir.c_str() )) + for (int n = 0; n < (int)files->getFileCount(); n++) { - Log::error("FileManager", "listFiles : Could not change CWD!\n"); - return; - } - irr::io::IFileList* files = m_file_system->createFileList(); - - for(int n=0; n<(int)files->getFileCount(); n++) - { - result.insert(make_full_path ? dir+"/"+ files->getFileName(n).c_str() - : files->getFileName(n).c_str() ); + result.insert(make_full_path ? dir + "/" + files->getFileName(n).c_str() + : files->getFileName(n).c_str()); } - m_file_system->changeWorkingDirectoryTo( previous_cwd ); files->drop(); } // listFiles