From 04ec4e66f43704210d2ae219eed07bcfaac31b2c Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Wed, 28 Sep 2005 22:59:12 +0000 Subject: [PATCH] add optional RageFileDriver::MoveFile override; implement for RageFileDriverDirect and RageFileDriverTimeout --- stepmania/src/RageFileDriver.h | 1 + stepmania/src/RageFileDriverDirect.cpp | 25 +++++++++++ stepmania/src/RageFileDriverDirect.h | 1 + stepmania/src/RageFileDriverDirectHelpers.cpp | 7 +++ stepmania/src/RageFileDriverDirectHelpers.h | 2 + stepmania/src/RageFileDriverTimeout.cpp | 45 ++++++++++++++++++- stepmania/src/RageFileDriverTimeout.h | 1 + 7 files changed, 81 insertions(+), 1 deletion(-) diff --git a/stepmania/src/RageFileDriver.h b/stepmania/src/RageFileDriver.h index e641a770fd..d12bee289d 100644 --- a/stepmania/src/RageFileDriver.h +++ b/stepmania/src/RageFileDriver.h @@ -19,6 +19,7 @@ public: virtual int GetFileHash( const CString &sPath ); virtual int GetPathValue( const CString &path ); virtual void FlushDirCache( const CString &sPath ); + virtual bool MoveFile( const CString &sOldPath, const CString &sNewPath ) { return false; } virtual bool Remove( const CString &sPath ) { return false; } /* Optional: Move to a different place, as if reconstructed with a different path. */ diff --git a/stepmania/src/RageFileDriverDirect.cpp b/stepmania/src/RageFileDriverDirect.cpp index 44f026189e..6447d46bdf 100644 --- a/stepmania/src/RageFileDriverDirect.cpp +++ b/stepmania/src/RageFileDriverDirect.cpp @@ -122,6 +122,31 @@ RageFileBasic *RageFileDriverDirect::Open( const CString &path, int mode, int &e return MakeFileObjDirect( root + sPath, mode, err ); } +bool RageFileDriverDirect::MoveFile( const CString &sOldPath_, const CString &sNewPath_ ) +{ + CString sOldPath = sOldPath_; + CString sNewPath = sNewPath_; + FDB->ResolvePath( sOldPath ); + FDB->ResolvePath( sNewPath ); + + if( this->GetFileType(sOldPath) == RageFileManager::TYPE_NONE ) + return false; + + { + const CString dir = Dirname(sNewPath); + CreateDirectories( root + dir ); + } + + LOG->Trace("rename \"%s\" -> \"%s\"", (root + sOldPath).c_str(), (root + sNewPath).c_str() ); + if( DoRename(root + sOldPath, root + sNewPath) == -1 ) + { + LOG->Warn( "rename(%s,%s) failed: %s", (root + sOldPath).c_str(), (root + sNewPath).c_str(), strerror(errno) ); + return false; + } + + return true; +} + bool RageFileDriverDirect::Remove( const CString &path ) { CString sPath = path; diff --git a/stepmania/src/RageFileDriverDirect.h b/stepmania/src/RageFileDriverDirect.h index a168608868..108a455387 100644 --- a/stepmania/src/RageFileDriverDirect.h +++ b/stepmania/src/RageFileDriverDirect.h @@ -11,6 +11,7 @@ public: RageFileDriverDirect( CString root ); RageFileBasic *Open( const CString &path, int mode, int &err ); + bool MoveFile( const CString &sOldPath, const CString &sNewPath ); bool Remove( const CString &sPath ); bool Remount( const CString &sPath ); diff --git a/stepmania/src/RageFileDriverDirectHelpers.cpp b/stepmania/src/RageFileDriverDirectHelpers.cpp index 83599c7475..ec12186b19 100644 --- a/stepmania/src/RageFileDriverDirectHelpers.cpp +++ b/stepmania/src/RageFileDriverDirectHelpers.cpp @@ -34,6 +34,13 @@ int DoStat( const CString &sPath, struct stat *st ) return stat( DoPathReplace(sPath), st ); } +int DoRename( const CString &sOldPath, const CString &sNewPath ) +{ + CString TempPath = sPath; + TempPath.Replace( "/", "\\" ); + return rename( sPath ); +} + int DoRemove( const CString &sPath ) { return remove( DoPathReplace(sPath) ); diff --git a/stepmania/src/RageFileDriverDirectHelpers.h b/stepmania/src/RageFileDriverDirectHelpers.h index e81d73ff74..90864e1c82 100644 --- a/stepmania/src/RageFileDriverDirectHelpers.h +++ b/stepmania/src/RageFileDriverDirectHelpers.h @@ -9,6 +9,7 @@ int DoMkdir( const CString &sPath, int perm ); int DoOpen( const CString &sPath, int flags, int perm ); int DoStat( const CString &sPath, struct stat *st ); +int DoRename( const CString &sOldPath, const CString &sNewPath ); int DoRemove( const CString &sPath ); int DoRmdir( const CString &sPath ); HANDLE DoFindFirstFile( const CString &sPath, WIN32_FIND_DATA *fd ); @@ -17,6 +18,7 @@ HANDLE DoFindFirstFile( const CString &sPath, WIN32_FIND_DATA *fd ); #define DoStat stat #define DoMkdir mkdir #define DoFindFirstFile FindFirstFile +#define DoRename rename #define DoRemove remove #define DoRmdir rmdir #endif diff --git a/stepmania/src/RageFileDriverTimeout.cpp b/stepmania/src/RageFileDriverTimeout.cpp index 7b789fb885..187f7713e5 100644 --- a/stepmania/src/RageFileDriverTimeout.cpp +++ b/stepmania/src/RageFileDriverTimeout.cpp @@ -61,6 +61,7 @@ enum ThreadRequest REQ_COPY, REQ_POPULATE_FILE_SET, REQ_FLUSH_DIR_CACHE, + REQ_MOVE_FILE, REQ_REMOVE, }; @@ -84,6 +85,7 @@ public: RageFileBasic *Copy( RageFileBasic *&pFile, CString &sError ); bool FlushDirCache( const CString &sPath ); + int MoveFile( const CString &sOldPath, const CString &sNewPath ); int Remove( const CString &sPath ); bool PopulateFileSet( FileSet &fs, const CString &sPath ); @@ -100,9 +102,12 @@ private: vector m_apDeletedFiles; RageMutex m_DeletedFilesLock; - /* REQ_OPEN, REQ_POPULATE_FILE_SET, REQ_FLUSH_DIR_CACHE, REQ_REMOVE: */ + /* REQ_OPEN, REQ_POPULATE_FILE_SET, REQ_FLUSH_DIR_CACHE, REQ_REMOVE, REQ_MOVE_FILE: */ CString m_sRequestPath; /* in */ + /* REQ_MOVE_FILE: */ + CString m_sRequestPath2; /* in */ + /* REQ_OPEN, REQ_COPY: */ RageFileBasic *m_pResultFile; /* out */ @@ -267,6 +272,12 @@ void ThreadedFileWorker::HandleRequest( int iRequest ) m_iResultRequest = m_pChildDriver->Remove( m_sRequestPath )? 0:-1; break; + case REQ_MOVE_FILE: + ASSERT( !m_sRequestPath.empty() ); + ASSERT( !m_sRequestPath2.empty() ); + m_iResultRequest = m_pChildDriver->MoveFile( m_sRequestPath, m_sRequestPath2 )? 0:-1; + break; + default: FAIL_M( ssprintf("%i", iRequest) ); } @@ -576,6 +587,26 @@ bool ThreadedFileWorker::PopulateFileSet( FileSet &fs, const CString &sPath ) return true; } +int ThreadedFileWorker::MoveFile( const CString &sOldPath, const CString &sNewPath ) +{ + ASSERT( m_pChildDriver != NULL ); /* how did you get a file to begin with? */ + + /* If we're currently in a timed-out state, fail. */ + if( IsTimedOut() ) + return -1; + + m_sRequestPath = sOldPath; + m_sRequestPath2 = sNewPath; + + if( !DoRequest(REQ_MOVE_FILE) ) + { + /* If we time out, we can no longer access pFile. */ + return -1; + } + + return m_iResultRequest; +} + int ThreadedFileWorker::Remove( const CString &sPath ) { ASSERT( m_pChildDriver != NULL ); /* how did you get a file to begin with? */ @@ -815,6 +846,18 @@ void RageFileDriverTimeout::FlushDirCache( const CString &sPath ) m_pWorker->FlushDirCache( sPath ); } +bool RageFileDriverTimeout::MoveFile( const CString &sOldPath, const CString &sNewPath ) +{ + int iRet = m_pWorker->MoveFile( sOldPath, sNewPath ); + if( iRet == -1 ) + { + LOG->Warn( "RageFileDriverTimeout::MoveFile(%s,%s) failed", sOldPath.c_str(), sNewPath.c_str() ); + return false; + } + + return true; +} + bool RageFileDriverTimeout::Remove( const CString &sPath ) { int iRet = m_pWorker->Remove( sPath ); diff --git a/stepmania/src/RageFileDriverTimeout.h b/stepmania/src/RageFileDriverTimeout.h index 15ef6e5433..01d985366b 100644 --- a/stepmania/src/RageFileDriverTimeout.h +++ b/stepmania/src/RageFileDriverTimeout.h @@ -15,6 +15,7 @@ public: RageFileBasic *Open( const CString &path, int mode, int &err ); void FlushDirCache( const CString &sPath ); + bool MoveFile( const CString &sOldPath, const CString &sNewPath ); bool Remove( const CString &sPath ); static void SetTimeout( float fSeconds );