path normalization optimziation (don't redo it per-driver)

This commit is contained in:
Glenn Maynard
2004-06-03 05:15:08 +00:00
parent f4187fb867
commit b6e5a87a00
2 changed files with 41 additions and 25 deletions
+33 -17
View File
@@ -34,7 +34,7 @@ struct LoadedDriver
CString Type, Root, MountPoint; CString Type, Root, MountPoint;
LoadedDriver() { driver = NULL; } LoadedDriver() { driver = NULL; }
CString GetPath( CString path ); CString GetPath( const CString &path );
}; };
static vector<LoadedDriver> g_Drivers; static vector<LoadedDriver> g_Drivers;
@@ -210,15 +210,9 @@ RageFileManager::~RageFileManager()
g_Mutex = NULL; g_Mutex = NULL;
} }
CString LoadedDriver::GetPath( CString path ) /* path must be normalized (FixSlashesInPlace, CollapsePath). */
CString LoadedDriver::GetPath( const CString &path )
{ {
/* Map all slashes to forward slash. */
FixSlashesInPlace( path );
/* Collapse the path, removing any leading dots. (FilenameDB::ResolvePath requires
* this.) */
CollapsePath( path, true );
/* Default mountpoints: */ /* Default mountpoints: */
if( MountPoint.size() == 0 ) if( MountPoint.size() == 0 )
{ {
@@ -234,12 +228,20 @@ CString LoadedDriver::GetPath( CString path )
return path.Right( path.size() - MountPoint.size() ); return path.Right( path.size() - MountPoint.size() );
} }
static void NormalizePath( CString &sPath )
{
FixSlashesInPlace( sPath );
CollapsePath( sPath );
}
bool ilt( const CString &a, const CString &b ) { return a.CompareNoCase(b) < 0; } bool ilt( const CString &a, const CString &b ) { return a.CompareNoCase(b) < 0; }
bool ieq( const CString &a, const CString &b ) { return a.CompareNoCase(b) == 0; } bool ieq( const CString &a, const CString &b ) { return a.CompareNoCase(b) == 0; }
void RageFileManager::GetDirListing( const CString &sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo ) void RageFileManager::GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo )
{ {
LockMut( *g_Mutex ); LockMut( *g_Mutex );
NormalizePath( sPath );
for( unsigned i = 0; i < g_Drivers.size(); ++i ) for( unsigned i = 0; i < g_Drivers.size(); ++i )
{ {
LoadedDriver &ld = g_Drivers[i]; LoadedDriver &ld = g_Drivers[i];
@@ -264,10 +266,12 @@ void RageFileManager::GetDirListing( const CString &sPath, CStringArray &AddTo,
AddTo.erase(it, AddTo.end()); AddTo.erase(it, AddTo.end());
} }
bool RageFileManager::Remove( const CString &sPath ) bool RageFileManager::Remove( CString sPath )
{ {
LockMut( *g_Mutex ); LockMut( *g_Mutex );
NormalizePath( sPath );
/* Multiple drivers may have the same file. */ /* Multiple drivers may have the same file. */
bool Deleted = false; bool Deleted = false;
for( unsigned i = 0; i < g_Drivers.size(); ++i ) for( unsigned i = 0; i < g_Drivers.size(); ++i )
@@ -392,10 +396,12 @@ void RageFileManager::GetLoadedDrivers( vector<DriverLocation> &Mounts )
} }
} }
void RageFileManager::FlushDirCache( const CString &sPath ) void RageFileManager::FlushDirCache( CString sPath )
{ {
LockMut( *g_Mutex ); LockMut( *g_Mutex );
NormalizePath( sPath );
for( unsigned i = 0; i < g_Drivers.size(); ++i ) for( unsigned i = 0; i < g_Drivers.size(); ++i )
{ {
if( sPath.size() == 0 ) if( sPath.size() == 0 )
@@ -412,10 +418,12 @@ void RageFileManager::FlushDirCache( const CString &sPath )
} }
} }
RageFileManager::FileType RageFileManager::GetFileType( const CString &sPath ) RageFileManager::FileType RageFileManager::GetFileType( CString sPath )
{ {
LockMut( *g_Mutex ); LockMut( *g_Mutex );
NormalizePath( sPath );
for( unsigned i = 0; i < g_Drivers.size(); ++i ) for( unsigned i = 0; i < g_Drivers.size(); ++i )
{ {
const CString p = g_Drivers[i].GetPath( sPath ); const CString p = g_Drivers[i].GetPath( sPath );
@@ -430,10 +438,12 @@ RageFileManager::FileType RageFileManager::GetFileType( const CString &sPath )
} }
int RageFileManager::GetFileSizeInBytes( const CString &sPath ) int RageFileManager::GetFileSizeInBytes( CString sPath )
{ {
LockMut( *g_Mutex ); LockMut( *g_Mutex );
NormalizePath( sPath );
for( unsigned i = 0; i < g_Drivers.size(); ++i ) for( unsigned i = 0; i < g_Drivers.size(); ++i )
{ {
const CString p = g_Drivers[i].GetPath( sPath ); const CString p = g_Drivers[i].GetPath( sPath );
@@ -447,10 +457,12 @@ int RageFileManager::GetFileSizeInBytes( const CString &sPath )
return -1; return -1;
} }
int RageFileManager::GetFileHash( const CString &sPath ) int RageFileManager::GetFileHash( CString sPath )
{ {
LockMut( *g_Mutex ); LockMut( *g_Mutex );
NormalizePath( sPath );
for( unsigned i = 0; i < g_Drivers.size(); ++i ) for( unsigned i = 0; i < g_Drivers.size(); ++i )
{ {
const CString p = g_Drivers[i].GetPath( sPath ); const CString p = g_Drivers[i].GetPath( sPath );
@@ -515,7 +527,7 @@ static bool PathUsesSlowFlush( const CString &sPath )
} }
/* Used only by RageFile: */ /* Used only by RageFile: */
RageFileObj *RageFileManager::Open( const CString &sPath, int mode, RageFile &p, int &err ) RageFileObj *RageFileManager::Open( CString sPath, int mode, RageFile &p, int &err )
{ {
LockMut( *g_Mutex ); LockMut( *g_Mutex );
@@ -529,6 +541,8 @@ RageFileObj *RageFileManager::Open( const CString &sPath, int mode, RageFile &p,
if( mode & RageFile::WRITE ) if( mode & RageFile::WRITE )
return OpenForWriting( sPath, mode, p, err ); return OpenForWriting( sPath, mode, p, err );
NormalizePath( sPath );
for( unsigned i = 0; i < g_Drivers.size(); ++i ) for( unsigned i = 0; i < g_Drivers.size(); ++i )
{ {
LoadedDriver &ld = g_Drivers[i]; LoadedDriver &ld = g_Drivers[i];
@@ -568,7 +582,7 @@ RageFileObj *RageFileManager::CopyFileObj( const RageFileObj *cpy, RageFile &p )
return ret; return ret;
} }
RageFileObj *RageFileManager::OpenForWriting( const CString &sPath, int mode, RageFile &p, int &err ) RageFileObj *RageFileManager::OpenForWriting( CString sPath, int mode, RageFile &p, int &err )
{ {
LockMut( *g_Mutex ); LockMut( *g_Mutex );
@@ -591,6 +605,8 @@ RageFileObj *RageFileManager::OpenForWriting( const CString &sPath, int mode, Ra
* If the given path can not be created, return -1. This happens if a path * If the given path can not be created, return -1. This happens if a path
* that needs to be a directory is a file, or vice versa. * that needs to be a directory is a file, or vice versa.
*/ */
NormalizePath( sPath );
vector< pair<int,int> > Values; vector< pair<int,int> > Values;
unsigned i; unsigned i;
for( i = 0; i < g_Drivers.size(); ++i ) for( i = 0; i < g_Drivers.size(); ++i )
+8 -8
View File
@@ -18,18 +18,18 @@ public:
~RageFileManager(); ~RageFileManager();
void MountInitialFilesystems(); void MountInitialFilesystems();
void GetDirListing( const CString &sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo ); void GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo );
bool Remove( const CString &sPath ); bool Remove( CString sPath );
enum FileType { TYPE_FILE, TYPE_DIR, TYPE_NONE }; enum FileType { TYPE_FILE, TYPE_DIR, TYPE_NONE };
FileType GetFileType( const CString &sPath ); FileType GetFileType( CString sPath );
bool IsAFile( const CString &sPath ); bool IsAFile( const CString &sPath );
bool IsADirectory( const CString &sPath ); bool IsADirectory( const CString &sPath );
bool DoesFileExist( const CString &sPath ); bool DoesFileExist( const CString &sPath );
int GetFileSizeInBytes( const CString &sPath ); int GetFileSizeInBytes( CString sPath );
int GetFileHash( const CString &sPath ); int GetFileHash( CString sPath );
void Mount( CString Type, CString RealPath, CString MountPoint ); void Mount( CString Type, CString RealPath, CString MountPoint );
void Unmount( CString Type, CString Root, CString MountPoint ); void Unmount( CString Type, CString Root, CString MountPoint );
@@ -41,15 +41,15 @@ public:
}; };
void GetLoadedDrivers( vector<DriverLocation> &Mounts ); void GetLoadedDrivers( vector<DriverLocation> &Mounts );
void FlushDirCache( const CString &sPath ); void FlushDirCache( CString sPath );
/* Used only by RageFile: */ /* Used only by RageFile: */
RageFileObj *Open( const CString &sPath, int mode, RageFile &p, int &err ); RageFileObj *Open( CString sPath, int mode, RageFile &p, int &err );
void Close( RageFileObj *obj ); void Close( RageFileObj *obj );
RageFileObj *CopyFileObj( const RageFileObj *cpy, RageFile &p ); RageFileObj *CopyFileObj( const RageFileObj *cpy, RageFile &p );
private: private:
RageFileObj *OpenForWriting( const CString &sPath, int mode, RageFile &p, int &err ); RageFileObj *OpenForWriting( CString sPath, int mode, RageFile &p, int &err );
}; };
extern RageFileManager *FILEMAN; extern RageFileManager *FILEMAN;