From fe866c9be6f9aaf119a90da5711653ae65fc38ea Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Tue, 6 Jan 2004 05:35:07 +0000 Subject: [PATCH] add helpers --- stepmania/src/RageFileDriverDirectHelpers.cpp | 159 ++++++++++++++++++ stepmania/src/RageFileDriverDirectHelpers.h | 32 ++++ 2 files changed, 191 insertions(+) create mode 100644 stepmania/src/RageFileDriverDirectHelpers.cpp create mode 100644 stepmania/src/RageFileDriverDirectHelpers.h diff --git a/stepmania/src/RageFileDriverDirectHelpers.cpp b/stepmania/src/RageFileDriverDirectHelpers.cpp new file mode 100644 index 0000000000..79cc4d6bda --- /dev/null +++ b/stepmania/src/RageFileDriverDirectHelpers.cpp @@ -0,0 +1,159 @@ +#include "global.h" +#include "RageFileDriverDirectHelpers.h" +#include "RageUtil.h" + +#include +#include +#include + +#if !defined(WIN32) +#include +#include +#else +#include +#include +#endif + +#if defined(XBOX) +/* Wrappers for low-level file functions, to work around Xbox issues: */ +int DoMkdir( const CString &sPath, int perm ) +{ + CString TempPath = sPath; + TempPath.Replace( "/", "\\" ); + return mkdir( TempPath ); +} + +int DoOpen( const CString &sPath, int flags, int perm ) +{ + CString TempPath = sPath; + TempPath.Replace( "/", "\\" ); + return open( TempPath, flags, perm ); +} + +int DoStat( const CString &sPath, struct stat *st ) +{ + CString TempPath = sPath; + TempPath.Replace( "/", "\\" ); + return stat( sPath, st ); +} + +int DoRemove( const CString &sPath ) +{ + CString TempPath = sPath; + TempPath.Replace( "/", "\\" ); + return remove( sPath ); +} + +int DoRmdir( const CString &sPath ) +{ + CString TempPath = sPath; + TempPath.Replace( "/", "\\" ); + return rmdir( sPath ); +} + +HANDLE DoFindFirstFile( const CString &sPath, WIN32_FIND_DATA *fd ) +{ + CString TempPath = sPath; + TempPath.Replace( "/", "\\" ); + return FindFirstFile( TempPath, fd ); +} + +#endif + +#if defined(WIN32) +int WinMoveFileInternal( const CString &sOldPath, const CString &sNewPath ) +{ + static bool Win9x = false; + + /* Windows botches rename: it returns error if the file exists. In NT, + * we can use MoveFileEx( new, old, MOVEFILE_REPLACE_EXISTING ) (though I + * don't know if it has similar atomicity guarantees to rename). In + * 9x, we're screwed, so just delete any existing file (we aren't going + * to be robust on 9x anyway). */ + if( !Win9x ) + { + if( MoveFileEx( sOldPath, sNewPath, MOVEFILE_REPLACE_EXISTING ) ) + return 1; + + if( GetLastError() == ERROR_NOT_SUPPORTED || GetLastError() == ERROR_CALL_NOT_IMPLEMENTED ) + return 0; + + Win9x = true; + } + + if( MoveFile( sOldPath, sNewPath ) ) + return 1; + + if( GetLastError() != ERROR_ALREADY_EXISTS ) + return 0; + + if( !DeleteFile( sNewPath ) ) + return 0; + + return MoveFile( sOldPath, sNewPath ); +} + +int WinMoveFile( CString sOldPath, CString sNewPath ) +{ +#if defined(XBOX) + sOldPath.Replace( "/", "\\" ); + sNewPath.Replace( "/", "\\" ); +#endif + + if( WinMoveFileInternal(sOldPath, sNewPath) ) + return 1; + if( GetLastError() != ERROR_ACCESS_DENIED ) + return 0; + /* Try turning off the read-only bit on the file we're overwriting. */ + SetFileAttributes( sNewPath, FILE_ATTRIBUTE_NORMAL ); + + return WinMoveFileInternal( sOldPath, sNewPath ); +} + +bool PathReady( CString path ) +{ +#ifdef _WINDOWS + // Windows will throw up a message box if we try to write to a + // removable drive with no disk inserted. Find out whether there's a + // disk in the drive w/o writing a file. + + // find drive letter + vector matches; + static Regex parse("^([A-Za-z]+):"); + parse.Compare( path, matches ); + if( matches.size() != 1 ) + return false; + + CString sDrive = matches[0]; + TCHAR szVolumeNameBuffer[MAX_PATH]; + DWORD dwVolumeSerialNumber; + DWORD dwMaximumComponentLength; + DWORD lpFileSystemFlags; + TCHAR szFileSystemNameBuffer[MAX_PATH]; + BOOL bResult = GetVolumeInformation( + sDrive + ":\\", + szVolumeNameBuffer, + sizeof(szVolumeNameBuffer), + &dwVolumeSerialNumber, + &dwMaximumComponentLength, + &lpFileSystemFlags, + szFileSystemNameBuffer, + sizeof(szFileSystemNameBuffer) ); + return !!bResult; +#else + // Try to create directory before writing a temp file. + CreateDirectories( path ); + + // Try to write a file. + const CString sFile = path + "temp"; + int fd = DoOpen( sFile, O_WRONLY|O_CREAT|O_TRUNC ); + if( fd == -1 ) + return false; + + close( fd ); + remove( sFile ); + return true; +#endif +} + +#endif diff --git a/stepmania/src/RageFileDriverDirectHelpers.h b/stepmania/src/RageFileDriverDirectHelpers.h new file mode 100644 index 0000000000..6d6c2f6d4e --- /dev/null +++ b/stepmania/src/RageFileDriverDirectHelpers.h @@ -0,0 +1,32 @@ +#ifndef RAGE_FILE_DRIVER_DIRECT_HELPERS_H +#define RAGE_FILE_DRIVER_DIRECT_HELPERS_H + +#include + +#if defined(XBOX) +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 DoRemove( const CString &sPath ); +int DoRmdir( const CString &sPath ); +HANDLE DoFindFirstFile( const CString &sPath, WIN32_FIND_DATA *fd ); +#else +#define DoOpen open +#define DoStat stat +#define DoMkdir mkdir +#define DoFindFirstFile FindFirstFile +#define DoRemove remove +#define DoRmdir rmdir +#endif + +#if defined(WIN32) +int WinMoveFile( CString sOldPath, CString sNewPath ); +#endif + +#if !defined(O_BINARY) +#define O_BINARY 0 +#endif + +bool PathReady( CString path ); + +#endif