From 53800b79a3dd53ff1f4c75a983d13402e6de75e1 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sat, 24 Dec 2005 03:09:33 +0000 Subject: [PATCH] try to avoid writing to the device to do a wirte check --- .../MemoryCardDriverThreaded_Windows.cpp | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_Windows.cpp b/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_Windows.cpp index 35d26612db..2f6aae011c 100644 --- a/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_Windows.cpp +++ b/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_Windows.cpp @@ -35,18 +35,31 @@ static bool TestReady( const CString &sDrive, CString &sVolumeLabelOut ) bool MemoryCardDriverThreaded_Windows::TestWrite( UsbStorageDevice* pDevice ) { - // Try to write a file. - CString sFile = pDevice->sOsMountDir + "temp"; - FILE* fp = fopen( sFile, "w" ); - if( fp == NULL ) + /* Try to write a file, to check if the device is writable and that we have write permission. + * Use FILE_ATTRIBUTE_TEMPORARY to try to avoid actually writing to the device. This reduces + * the chance of corruption if the user removes the device immediately, without doing anything. */ + for( int i = 0; i < 10; ++i ) { - pDevice->SetError( "TestFailed" ); - return false; - } - fclose( fp ); - remove( sFile ); + HANDLE hFile = CreateFile( ssprintf( "%stmp%i", pDevice->sOsMountDir.c_str(), rand() % 100000), + GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, CREATE_NEW, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, NULL ); - return true; + if( hFile == INVALID_HANDLE_VALUE ) + { + DWORD iError = GetLastError(); + if( iError == ERROR_ALREADY_EXISTS ) + continue; + + LOG->Warn( werr_ssprintf(iError, "Couldn't write to %s", pDevice->sOsMountDir.c_str()) ); + break; + } + + CloseHandle( hFile ); + return true; + } + + pDevice->SetError( "TestFailed" ); + return false; } static bool IsFloppyDrive( const CString &sDrive )