Add IFileSystem::existFileOnly (not including directory)

This commit is contained in:
Benau 2022-04-16 12:08:57 +08:00
parent 57a9a8a2aa
commit 5f9db20b4f
6 changed files with 46 additions and 25 deletions

View File

@ -326,10 +326,10 @@ public:
\return True if file exists, and false if it does not exist or an error occured. */ \return True if file exists, and false if it does not exist or an error occured. */
virtual bool existFile(const path& filename) const =0; virtual bool existFile(const path& filename) const =0;
//! Determines if a file exists and could be opened (thread-safe, ignore file archives). //! Determines if a file exists and could be opened (thread-safe, ignore file archives), this function returns false for directory
/** \param filename is the string identifying the file which should be tested for existence. /** \param filename is the string identifying the file which should be tested for existence.
\return True if file exists, and false if it does not exist or an error occured. */ \return True if file exists, and false if it does not exist or an error occured. */
virtual bool existFileThreadSafe(const path& filename) const =0; virtual bool existFileOnly(const path& filename) const =0;
//! Creates a XML Reader from a file which returns all parsed strings as wide characters (wchar_t*). //! Creates a XML Reader from a file which returns all parsed strings as wide characters (wchar_t*).
/** Use createXMLReaderUTF8() if you prefer char* instead of wchar_t*. See IIrrXMLReader for /** Use createXMLReaderUTF8() if you prefer char* instead of wchar_t*. See IIrrXMLReader for

View File

@ -20,6 +20,8 @@
#include "CLimitReadFile.h" #include "CLimitReadFile.h"
#include "irrList.h" #include "irrList.h"
#include "io/file_manager.hpp"
#if defined (_IRR_WINDOWS_API_) #if defined (_IRR_WINDOWS_API_)
#include "utils/string_utils.hpp" #include "utils/string_utils.hpp"
#if !defined ( _WIN32_WCE ) #if !defined ( _WIN32_WCE )
@ -958,10 +960,31 @@ IFileList* CFileSystem::createEmptyFileList(const io::path& path, bool ignoreCas
return new CFileList(path, ignoreCase, ignorePaths); return new CFileList(path, ignoreCase, ignorePaths);
} }
//! Determines if a file exists and could be opened (thread-safe, ignore file archives), this function returns false for directory
//! Determines if a file exists and could be opened (thread-safe, ignore file archives). bool CFileSystem::existFileOnly(const path& filename) const
bool CFileSystem::existFileThreadSafe(const path& filename) const
{ {
if (FileManager::isDirectory(filename.c_str()))
return false;
io::IReadFile* file = createReadFile(filename);
if (file)
{
file->drop();
return true;
}
return false;
}
//! determines if a file exists and would be able to be opened.
bool CFileSystem::existFile(const io::path& filename) const
{
std::unique_lock<std::recursive_mutex> ul(m_file_archives_mutex);
for (u32 i=0; i < FileArchives.size(); ++i)
if (FileArchives[i]->getFileList()->findFile(filename)!=-1)
return true;
ul.unlock();
#if defined(_IRR_WINDOWS_CE_PLATFORM_) #if defined(_IRR_WINDOWS_CE_PLATFORM_)
#if defined(_IRR_WCHAR_FILESYSTEM) #if defined(_IRR_WCHAR_FILESYSTEM)
HANDLE hFile = CreateFileW(filename.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); HANDLE hFile = CreateFileW(filename.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
@ -998,18 +1021,6 @@ bool CFileSystem::existFileThreadSafe(const path& filename) const
} }
//! determines if a file exists and would be able to be opened.
bool CFileSystem::existFile(const io::path& filename) const
{
std::unique_lock<std::recursive_mutex> ul(m_file_archives_mutex);
for (u32 i=0; i < FileArchives.size(); ++i)
if (FileArchives[i]->getFileList()->findFile(filename)!=-1)
return true;
ul.unlock();
return existFileThreadSafe(filename);
}
//! Creates a XML Reader from a file. //! Creates a XML Reader from a file.
IXMLReader* CFileSystem::createXMLReader(const io::path& filename) IXMLReader* CFileSystem::createXMLReader(const io::path& filename)
{ {

View File

@ -133,8 +133,8 @@ public:
//! determines if a file exists and would be able to be opened. //! determines if a file exists and would be able to be opened.
virtual bool existFile(const io::path& filename) const; virtual bool existFile(const io::path& filename) const;
//! Determines if a file exists and could be opened (thread-safe, ignore file archives). //! Determines if a file exists and could be opened (thread-safe, ignore file archives), this function returns false for directory
virtual bool existFileThreadSafe(const path& filename) const; virtual bool existFileOnly(const path& filename) const;
//! Creates a XML Reader from a file. //! Creates a XML Reader from a file.
virtual IXMLReader* createXMLReader(const io::path& filename); virtual IXMLReader* createXMLReader(const io::path& filename);

View File

@ -78,12 +78,22 @@ void CReadFile::openFile()
if (File) if (File)
{ {
// get FileSize // get FileSize, check for errors
// See https://stackoverflow.com/questions/18192998/plain-c-opening-a-directory-with-fopen
fseek(File, 0, SEEK_END); if (fseek(File, 0, SEEK_END) < 0)
FileSize = getPos(); goto error;
fseek(File, 0, SEEK_SET); s32 file_size = (s32)ftell(File);
if (file_size == -1)
goto error;
FileSize = file_size;
if (fseek(File, 0, SEEK_SET) < 0)
goto error;
} }
return;
error:
fclose(File);
File = 0;
} }

View File

@ -1439,7 +1439,7 @@ std::string FileManager::searchModel(const std::string& file_name) const
/** Returns true if the given name is a directory. /** Returns true if the given name is a directory.
* \param path File name to test. * \param path File name to test.
*/ */
bool FileManager::isDirectory(const std::string &path) const bool FileManager::isDirectory(const std::string &path)
{ {
struct stat mystat; struct stat mystat;
std::string s(path); std::string s(path);

View File

@ -157,7 +157,7 @@ public:
const std::string &getAddonsDir() const; const std::string &getAddonsDir() const;
std::string getAddonsFile(const std::string &name); std::string getAddonsFile(const std::string &name);
void checkAndCreateDirForAddons(const std::string &dir); void checkAndCreateDirForAddons(const std::string &dir);
bool isDirectory(const std::string &path) const; static bool isDirectory(const std::string &path);
bool removeFile(const std::string &name) const; bool removeFile(const std::string &name) const;
bool removeDirectory(const std::string &name) const; bool removeDirectory(const std::string &name) const;
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------