From d5abb2b0c8631888586dd2e9418d6535fc00ca66 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Tue, 16 Dec 2003 07:23:54 +0000 Subject: [PATCH] add RageFileManager::Remove --- stepmania/src/RageFileDriver.h | 1 + stepmania/src/RageFileDriverDirect.cpp | 56 ++++++++++++++++++++++++++ stepmania/src/RageFileDriverDirect.h | 1 + stepmania/src/RageFileManager.cpp | 20 ++++++++- stepmania/src/RageFileManager.h | 3 +- 5 files changed, 79 insertions(+), 2 deletions(-) diff --git a/stepmania/src/RageFileDriver.h b/stepmania/src/RageFileDriver.h index ef569db089..abc1ee9e90 100644 --- a/stepmania/src/RageFileDriver.h +++ b/stepmania/src/RageFileDriver.h @@ -19,6 +19,7 @@ public: virtual int GetPathValue( const CString &path ); virtual bool Ready() { return true; } /* see RageFileManager::MountpointIsReady */ virtual void FlushDirCache( const CString &sPath ); + virtual bool Remove( const CString &sPath ) { return false; } protected: FilenameDB *FDB; diff --git a/stepmania/src/RageFileDriverDirect.cpp b/stepmania/src/RageFileDriverDirect.cpp index a1df0c1dab..be1d56573c 100644 --- a/stepmania/src/RageFileDriverDirect.cpp +++ b/stepmania/src/RageFileDriverDirect.cpp @@ -68,6 +68,28 @@ static HANDLE DoFindFirstFile( const CString &sPath, WIN32_FIND_DATA *fd ) } #endif +static int DoRemove( const CString &sPath ) +{ +#if defined(XBOX) + CString TempPath = sPath; + TempPath.Replace( "/", "\\" ); + return remove( sPath ); +#else + return remove( sPath ); +#endif +} + +static int DoRmdir( const CString &sPath ) +{ +#if defined(XBOX) + CString TempPath = sPath; + TempPath.Replace( "/", "\\" ); + return rmdir( sPath ); +#else + return rmdir( sPath ); +#endif +} + /* This driver handles direct file access. */ class DirectFilenameDB: public FilenameDB @@ -279,6 +301,7 @@ RageFileObj *RageFileDriverDirect::Open( const CString &path, RageFile::OpenMode /* This partially resolves. For example, if "abc/def" exists, and we're opening * "ABC/DEF/GHI/jkl/mno", this will resolve it to "abc/def/GHI/jkl/mno"; we'll * create the missing parts below. */ + /* XXX: does it? */ FDB->ResolvePath( sPath ); if( mode == RageFile::WRITE ) @@ -291,6 +314,39 @@ RageFileObj *RageFileDriverDirect::Open( const CString &path, RageFile::OpenMode return MakeFileObjDirect( root + sPath, mode, p, err ); } +bool RageFileDriverDirect::Remove( const CString &path ) +{ + CString sPath = path; + FDB->ResolvePath( sPath ); + switch( this->GetFileType(sPath) ) + { + case TTYPE_FILE: + LOG->Trace("remove '%s'", (root + sPath).c_str()); + if( DoRemove( root + sPath ) == -1 ) + { + LOG->Warn("remove(%s) failed: %s", + (root + sPath).c_str(), strerror(errno) ); + return false; + } + FDB->DelFile( sPath ); + return true; + + case TTYPE_DIR: + LOG->Trace("rmdir '%s'", (root + sPath).c_str()); + if( DoRmdir( root + sPath ) == -1 ) + { + LOG->Warn("rmdir(%s) failed: %s", + (root + sPath).c_str(), strerror(errno) ); + return false; + } + FDB->DelFile( sPath ); + return true; + + case TTYPE_NONE: return false; + default: ASSERT(0); return false; + } +} + RageFileObj *RageFileObjDirect::Copy( RageFile &p ) const { int err; diff --git a/stepmania/src/RageFileDriverDirect.h b/stepmania/src/RageFileDriverDirect.h index 8e97de9b0e..b968a40c7d 100644 --- a/stepmania/src/RageFileDriverDirect.h +++ b/stepmania/src/RageFileDriverDirect.h @@ -9,6 +9,7 @@ public: RageFileDriverDirect( CString root ); RageFileObj *Open( const CString &path, RageFile::OpenMode mode, RageFile &p, int &err ); + bool Remove( const CString &sPath ); bool Ready(); private: diff --git a/stepmania/src/RageFileManager.cpp b/stepmania/src/RageFileManager.cpp index 839e5cb2b8..c6fa389c55 100644 --- a/stepmania/src/RageFileManager.cpp +++ b/stepmania/src/RageFileManager.cpp @@ -153,7 +153,7 @@ CString LoadedDriver::GetPath( CString path ) 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; } -void RageFileManager::GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo ) +void RageFileManager::GetDirListing( const CString &sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo ) { for( unsigned i = 0; i < g_Drivers.size(); ++i ) { @@ -179,6 +179,24 @@ void RageFileManager::GetDirListing( CString sPath, CStringArray &AddTo, bool bO AddTo.erase(it, AddTo.end()); } +bool RageFileManager::Remove( const CString &sPath ) +{ + /* Multiple drivers may have the same file. */ + bool Deleted = false; + for( unsigned i = 0; i < g_Drivers.size(); ++i ) + { + const CString p = g_Drivers[i].GetPath( sPath ); + if( p.size() == 0 ) + continue; + + bool ret = g_Drivers[i].driver->Remove( p ); + if( !ret ) + Deleted = true; + } + + return Deleted; +} + #include "RageFileDriverDirect.h" #include "RageFileDriverZip.h" void RageFileManager::Mount( CString Type, CString Root, CString MountPoint ) diff --git a/stepmania/src/RageFileManager.h b/stepmania/src/RageFileManager.h index 007f5da220..c1bcaab22f 100644 --- a/stepmania/src/RageFileManager.h +++ b/stepmania/src/RageFileManager.h @@ -10,7 +10,8 @@ public: RageFileManager(); ~RageFileManager(); - void GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo ); + void GetDirListing( const CString &sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo ); + bool Remove( const CString &sPath ); enum FileType { TYPE_FILE, TYPE_DIR, TYPE_NONE }; FileType GetFileType( const CString &sPath );