smsvn -> ssc-hg glue: rearrange directory structure

This commit is contained in:
Devin J. Pohly
2013-06-10 15:38:43 -04:00
parent 51576d5942
commit 80057f53cd
3362 changed files with 0 additions and 0 deletions
+162
View File
@@ -0,0 +1,162 @@
#include "global.h"
#include "MemoryCardDriver.h"
#include "RageFileManager.h"
#include "RageLog.h"
#include "Foreach.h"
#include "ProfileManager.h"
static const RString TEMP_MOUNT_POINT = "/@mctemptimeout/";
bool UsbStorageDevice::operator==(const UsbStorageDevice& other) const
{
// LOG->Trace( "Comparing %d %d %d %s %s to %d %d %d %s %s",
// iBus, iPort, iLevel, sName.c_str(), sOsMountDir.c_str(),
// other.iBus, other.iPort, other.iLevel, other.sName.c_str(), other.sOsMountDir.c_str() );
#define COMPARE(x) if( x != other.x ) return false
COMPARE( iBus );
COMPARE( iPort );
COMPARE( iLevel );
COMPARE( sOsMountDir );
return true;
#undef COMPARE
}
void UsbStorageDevice::SetOsMountDir( const RString &s )
{
sOsMountDir = s;
}
bool MemoryCardDriver::NeedUpdate( bool bMount )
{
if( bMount )
{
/* Check if any devices need a write test. */
for( unsigned i=0; i<m_vDevicesLastSeen.size(); i++ )
{
const UsbStorageDevice &d = m_vDevicesLastSeen[i];
if( d.m_State == UsbStorageDevice::STATE_CHECKING )
return true;
}
}
return USBStorageDevicesChanged();
}
bool MemoryCardDriver::DoOneUpdate( bool bMount, vector<UsbStorageDevice>& vStorageDevicesOut )
{
if( !NeedUpdate(bMount) )
return false;
vector<UsbStorageDevice> vOld = m_vDevicesLastSeen; // copy
GetUSBStorageDevices( vStorageDevicesOut );
// log connects
FOREACH( UsbStorageDevice, vStorageDevicesOut, newd )
{
vector<UsbStorageDevice>::iterator iter = find( vOld.begin(), vOld.end(), *newd );
if( iter == vOld.end() ) // didn't find
LOG->Trace( "New device connected: %s", newd->sDevice.c_str() );
}
/* When we first see a device, regardless of bMount, just return it as CHECKING,
* so the main thread knows about the device. On the next call where bMount is
* true, check it. */
for( unsigned i=0; i<vStorageDevicesOut.size(); i++ )
{
UsbStorageDevice &d = vStorageDevicesOut[i];
/* If this device was just connected (it wasn't here last time), set it to
* CHECKING and return it, to let the main thread know about the device before
* we start checking. */
vector<UsbStorageDevice>::iterator iter = find( vOld.begin(), vOld.end(), d );
if( iter == vOld.end() ) // didn't find
{
LOG->Trace( "New device entering CHECKING: %s", d.sDevice.c_str() );
d.m_State = UsbStorageDevice::STATE_CHECKING;
continue;
}
/* Preserve the state of the device, and any data loaded from previous checks. */
d.m_State = iter->m_State;
d.bIsNameAvailable = iter->bIsNameAvailable;
d.sName = iter->sName;
/* The device was here last time. If CHECKING, check the device now, if
* we're allowed to. */
if( d.m_State == UsbStorageDevice::STATE_CHECKING )
{
if( !bMount )
{
/* We can't check it now. Keep STATE_CHECKING, and check it when we can. */
d.m_State = UsbStorageDevice::STATE_CHECKING;
continue;
}
if( !this->Mount(&d) )
{
d.SetError( "MountFailed" );
continue;
}
if( TestWrite(&d) )
{
/* We've successfully mounted and tested the device. Read the
* profile name (by mounting a temporary, private mountpoint),
* and then unmount it until Mount() is called. */
d.m_State = UsbStorageDevice::STATE_READY;
FILEMAN->Mount( "dir", d.sOsMountDir, TEMP_MOUNT_POINT );
d.bIsNameAvailable = PROFILEMAN->FastLoadProfileNameFromMemoryCard( TEMP_MOUNT_POINT, d.sName );
FILEMAN->Unmount( "dir", d.sOsMountDir, TEMP_MOUNT_POINT );
}
this->Unmount( &d );
LOG->Trace( "WriteTest: %s, Name: %s", d.m_State == UsbStorageDevice::STATE_ERROR? "failed":"succeeded", d.sName.c_str() );
}
}
m_vDevicesLastSeen = vStorageDevicesOut;
return true;
}
#include "arch/arch_default.h"
MemoryCardDriver *MemoryCardDriver::Create()
{
MemoryCardDriver *ret = NULL;
#ifdef ARCH_MEMORY_CARD_DRIVER
ret = new ARCH_MEMORY_CARD_DRIVER;
#endif
if( !ret )
ret = new MemoryCardDriver_Null;
return ret;
}
/*
* (c) 2002-2004 Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
+132
View File
@@ -0,0 +1,132 @@
#ifndef MEMORY_CARD_DRIVER_H
#define MEMORY_CARD_DRIVER_H
struct UsbStorageDevice
{
UsbStorageDevice() { MakeBlank(); }
void MakeBlank()
{
// -1 means "don't know"
iBus = -1;
iPort = -1;
iLevel = -1;
sDevice = "";
sSerial = "<none>"; // be different than a card with no serial
sOsMountDir = "";
m_State = STATE_NONE;
bIsNameAvailable = false;
sName = "";
idVendor = 0;
idProduct = 0;
sVendor = "";
sProduct = "";
sVolumeLabel = "";
iVolumeSizeMB = 0;
};
int iBus;
int iPort;
int iLevel;
RString sSerial;
RString sDevice;
RString sOsMountDir; // WITHOUT trailing slash
RString sSysPath; // Linux: /sys/block name
enum State
{
/* Empty device. This is used only by MemoryCardManager. */
STATE_NONE,
/* The card has been detected, but we haven't finished write tests, loading
* the quick profile information, etc. yet. We can display something on
* screen, in order to appear responsive, show that something's happening and
* aid diagnostics, though. */
STATE_CHECKING,
/* We can't write to the device; it may be write-protected, use a filesystem
* that we don't understand, unformatted, etc. */
STATE_ERROR,
/* The device is ready and usable. sName is filled in, if available. */
STATE_READY,
NUM_State,
State_INVALID
};
State m_State;
RString m_sError;
void SetError( const RString &sError ) { m_State = STATE_ERROR; m_sError = sError; }
bool bIsNameAvailable; // Name in the profile on the memory card.
RString sName; // Name in the profile on the memory card.
int idVendor;
int idProduct;
RString sVendor;
RString sProduct;
RString sVolumeLabel;
int iVolumeSizeMB;
bool IsBlank() const { return m_State == STATE_NONE; }
void SetOsMountDir( const RString &s );
bool operator==(const UsbStorageDevice& other) const;
};
class MemoryCardDriver
{
public:
static MemoryCardDriver *Create();
MemoryCardDriver() {}
virtual ~MemoryCardDriver() {}
/* Make a device accessible via its pDevice->sOsMountDir. This will be called
* before any access to the device, and before TestWrite. */
virtual bool Mount( UsbStorageDevice* pDevice ) = 0;
virtual void Unmount( UsbStorageDevice* pDevice ) = 0;
/* Poll for memory card changes. If anything has changed, fill in vStorageDevicesOut
* and return true. */
bool DoOneUpdate( bool bMount, vector<UsbStorageDevice>& vStorageDevicesOut );
protected:
/* This may be called before GetUSBStorageDevices; return false if the results of
* GetUSBStorageDevices have not changed. (This is an optimization.) */
virtual bool USBStorageDevicesChanged() { return true; }
virtual void GetUSBStorageDevices( vector<UsbStorageDevice>& vDevicesOut ) { }
/* Test the device. On failure, call pDevice->SetError() appropriately, and return false. */
virtual bool TestWrite( UsbStorageDevice* pDevice ) { return true; }
private:
vector<UsbStorageDevice> m_vDevicesLastSeen;
bool NeedUpdate( bool bMount );
};
#endif
/*
* (c) 2003-2004 Chris Danford
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
@@ -0,0 +1,348 @@
#include "global.h"
#include "MemoryCardDriverThreaded_Linux.h"
#include "RageLog.h"
#include "RageUtil.h"
#include "RageFile.h"
#include <cerrno>
#include <fcntl.h>
#include <dirent.h>
bool MemoryCardDriverThreaded_Linux::TestWrite( UsbStorageDevice* pDevice )
{
if( access(pDevice->sOsMountDir, W_OK) == -1 )
{
pDevice->SetError( "TestFailed" );
return false;
}
return true;
}
static bool ExecuteCommand( const RString &sCommand )
{
LOG->Trace( "executing '%s'", sCommand.c_str() );
int ret = system(sCommand);
LOG->Trace( "done executing '%s'", sCommand.c_str() );
if( ret != 0 )
{
RString sError = ssprintf("failed to execute '%s' with error %d", sCommand.c_str(), ret);
if( ret == -1 )
sError += ssprintf(": %s", sCommand.c_str());
LOG->Warn( "%s", sError.c_str() );
}
return ret == 0;
}
static bool ReadFile( const RString &sPath, RString &sBuf )
{
sBuf.clear();
int fd = open( sPath, O_RDONLY );
if( fd == -1 )
{
LOG->Warn( "Error opening \"%s\": %s", sPath.c_str(), strerror(errno) );
return false;
}
while(1)
{
char buf[1024];
int iGot = read( fd, buf, sizeof(buf) );
if( iGot == -1 )
{
close(fd);
LOG->Warn( "Error reading \"%s\": %s", sPath.c_str(), strerror(errno) );
return false;
}
sBuf.append( buf, iGot );
if( iGot < (int) sizeof(buf) )
break;
}
close(fd);
return true;
}
static void GetFileList( const RString &sPath, vector<RString> &out )
{
out.clear();
DIR *dp = opendir( sPath );
if( dp == NULL )
return; // false; // XXX warn
while( const struct dirent *ent = readdir(dp) )
out.push_back( ent->d_name );
closedir( dp );
}
bool MemoryCardDriverThreaded_Linux::USBStorageDevicesChanged()
{
RString sThisDevices;
/* If a device is removed and reinserted, the inode of the /sys/block entry
* will change. */
RString sDevicePath = "/sys/block/";
vector<RString> asDevices;
GetFileList( sDevicePath, asDevices );
for( unsigned i = 0; i < asDevices.size(); ++i )
{
struct stat buf;
if( stat( sDevicePath + asDevices[i], &buf ) == -1 )
continue; // XXX warn
sThisDevices += ssprintf( "%i,", (int) buf.st_ino );
}
bool bChanged = sThisDevices != m_sLastDevices;
m_sLastDevices = sThisDevices;
if( bChanged )
LOG->Trace( "Change in USB storage devices detected." );
return bChanged;
}
void MemoryCardDriverThreaded_Linux::GetUSBStorageDevices( vector<UsbStorageDevice>& vDevicesOut )
{
LOG->Trace( "GetUSBStorageDevices" );
vDevicesOut.clear();
{
vector<RString> asDevices;
RString sBlockDevicePath = "/sys/block/";
GetFileList( sBlockDevicePath, asDevices );
for( unsigned i = 0; i < asDevices.size(); ++i )
{
const RString &sDevice = asDevices[i];
if( sDevice == "." || sDevice == ".." )
continue;
UsbStorageDevice usbd;
RString sPath = sBlockDevicePath + sDevice + "/";
usbd.sSysPath = sPath;
/* Ignore non-removable devices. */
RString sBuf;
if( !ReadFile( sPath + "removable", sBuf ) )
continue; // already warned
if( atoi(sBuf) != 1 )
continue;
/* HACK: The kernel isn't exposing all of /sys atomically, so we end up
* missing the partition due to it not being shown yet. The kernel should
* be exposing all of this atomically. */
usleep(50000);
/* If the first partition device exists, eg. /sys/block/uba/uba1, use it. */
if( access(usbd.sSysPath + sDevice + "1", F_OK) != -1 )
usbd.sDevice = "/dev/" + sDevice + "1";
else
usbd.sDevice = "/dev/" + sDevice;
/*
* sPath/device should be a symlink to the actual device. For USB
* devices, it looks like this:
*
* device -> ../../devices/pci0000:00/0000:00:02.1/usb2/2-1/2-1:1.0
*
* "2-1" is "bus-port".
*/
char szLink[256];
int iRet = readlink( sPath + "device", szLink, sizeof(szLink) );
if( iRet == -1 )
{
LOG->Warn( "readlink(\"%s\"): %s", (sPath + "device").c_str(), strerror(errno) );
}
else
{
/*
* The full path looks like
*
* ../../devices/pci0000:00/0000:00:02.1/usb2/2-2/2-2.1/2-2.1:1.0
*
* Each path element refers to a new hop in the chain.
* "usb2" = second USB host
* 2- second USB host,
* -2 port 1 on the host,
* .1 port 1 on an attached hub
* .2 ... port 2 on the next hub ...
*
* We want the bus number and the port of the last hop. The level is
* the number of hops.
*/
szLink[iRet] = 0;
vector<RString> asBits;
split( szLink, "/", asBits );
if( strstr( szLink, "usb" ) != NULL )
{
RString sHostPort = asBits[asBits.size()-2];
sHostPort.Replace( "-", "." );
asBits.clear();
split( sHostPort, ".", asBits );
if( asBits.size() > 1 )
{
usbd.iBus = atoi( asBits[0] );
usbd.iPort = atoi( asBits[asBits.size()-1] );
usbd.iLevel = asBits.size() - 1;
}
}
}
if( ReadFile( sPath + "device/../idVendor", sBuf ) )
sscanf( sBuf, "%x", &usbd.idVendor );
if( ReadFile( sPath + "device/../idProduct", sBuf ) )
sscanf( sBuf, "%x", &usbd.idProduct );
if( ReadFile( sPath + "device/../serial", sBuf ) )
{
usbd.sSerial = sBuf;
TrimRight( usbd.sSerial );
}
if( ReadFile( sPath + "device/../product", sBuf ) )
{
usbd.sProduct = sBuf;
TrimRight( usbd.sProduct );
}
if( ReadFile( sPath + "device/../manufacturer", sBuf ) )
{
usbd.sVendor = sBuf;
TrimRight( usbd.sVendor );
}
vDevicesOut.push_back( usbd );
}
}
{
// Find where each device is mounted. Output looks like:
// /dev/sda1 /mnt/flash1 auto noauto,owner 0 0
// /dev/sdb1 /mnt/flash2 auto noauto,owner 0 0
// /dev/sdc1 /mnt/flash3 auto noauto,owner 0 0
RString fn = "/rootfs/etc/fstab";
RageFile f;
if( !f.Open(fn) )
{
LOG->Warn( "can't open '%s': %s", fn.c_str(), f.GetError().c_str() );
return;
}
RString sLine;
while( !f.AtEOF() )
{
switch( f.GetLine(sLine) )
{
case 0: continue; /* eof */
case -1:
LOG->Warn( "error reading '%s': %s", fn.c_str(), f.GetError().c_str() );
return;
}
char szScsiDevice[1024];
char szMountPoint[1024];
int iRet = sscanf( sLine, "%s %s", szScsiDevice, szMountPoint );
if( iRet != 2 )
continue; // don't process this line
RString sMountPoint = szMountPoint;
TrimLeft( sMountPoint );
TrimRight( sMountPoint );
// search for the mountpoint corresponding to the device
for( unsigned i=0; i<vDevicesOut.size(); i++ )
{
UsbStorageDevice& usbd = vDevicesOut[i];
if( usbd.sDevice == szScsiDevice ) // found our match
{
usbd.sOsMountDir = sMountPoint;
break; // stop looking for a match
}
}
}
}
for( unsigned i=0; i<vDevicesOut.size(); i++ )
{
UsbStorageDevice& usbd = vDevicesOut[i];
LOG->Trace( " sDevice: %s, iBus: %d, iLevel: %d, iPort: %d, id: %04X:%04X, Vendor: '%s', Product: '%s', sSerial: \"%s\", sOsMountDir: %s",
usbd.sDevice.c_str(), usbd.iBus, usbd.iLevel, usbd.iPort, usbd.idVendor, usbd.idProduct, usbd.sVendor.c_str(),
usbd.sProduct.c_str(), usbd.sSerial.c_str(), usbd.sOsMountDir.c_str() );
}
/* Remove any devices that we couldn't find a mountpoint for. */
for( unsigned i=0; i<vDevicesOut.size(); i++ )
{
UsbStorageDevice& usbd = vDevicesOut[i];
if( usbd.sOsMountDir.empty() )
{
LOG->Trace( "Ignoring %s (couldn't find in /etc/fstab)", usbd.sDevice.c_str() );
vDevicesOut.erase( vDevicesOut.begin()+i );
--i;
}
}
LOG->Trace( "Done with GetUSBStorageDevices" );
}
bool MemoryCardDriverThreaded_Linux::Mount( UsbStorageDevice* pDevice )
{
ASSERT( !pDevice->sDevice.empty() );
RString sCommand = "mount " + pDevice->sDevice;
bool bMountedSuccessfully = ExecuteCommand( sCommand );
return bMountedSuccessfully;
}
void MemoryCardDriverThreaded_Linux::Unmount( UsbStorageDevice* pDevice )
{
if( pDevice->sDevice.empty() )
return;
/* Use umount -l, so we unmount the device even if it's in use. Open
* files remain usable, and the device (eg. /dev/sda) won't be reused
* by new devices until those are closed. Without this, if something
* causes the device to not unmount here, we'll never unmount it; that
* causes a device name leak, eventually running us out of mountpoints. */
RString sCommand = "sync; umount -l \"" + pDevice->sDevice + "\"";
ExecuteCommand( sCommand );
}
/*
* (c) 2003-2005 Chris Danford, Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
@@ -0,0 +1,50 @@
#ifndef MemoryCardDriverThreaded_Linux_H
#define MemoryCardDriverThreaded_Linux_H 1
#include "MemoryCardDriver.h"
class MemoryCardDriverThreaded_Linux : public MemoryCardDriver
{
public:
virtual bool Mount( UsbStorageDevice* pDevice );
virtual void Unmount( UsbStorageDevice* pDevice );
protected:
void GetUSBStorageDevices( vector<UsbStorageDevice>& vDevicesOut );
bool USBStorageDevicesChanged();
bool TestWrite( UsbStorageDevice* pDevice );
RString m_sLastDevices;
};
#ifdef ARCH_MEMORY_CARD_DRIVER
#error "More than one MemoryCardDriver selected!"
#endif
#define ARCH_MEMORY_CARD_DRIVER MemoryCardDriverThreaded_Linux
#endif
/*
* (c) 2003-2004 Chris Danford
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
@@ -0,0 +1,249 @@
#include "global.h"
#include "MemoryCardDriverThreaded_MacOSX.h"
#include "Foreach.h"
#include "RageUtil.h"
#include "RageLog.h"
#include <Carbon/Carbon.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/storage/IOMedia.h>
#include <IOKit/usb/USBSpec.h>
#include <IOKit/usb/IOUSBLib.h>
#include <sys/param.h>
#include <sys/ucred.h>
#include <sys/mount.h>
#include <paths.h>
#include <unistd.h>
class MemoryCardDriverThreaded_MacOSX::Helper
{
public:
Helper( MemoryCardDriverThreaded_MacOSX *driver )
{
m_HandlerUPP = NewEventHandlerUPP( VolumesChanged );
EventTypeSpec types[] = { { kEventClassVolume, kEventVolumeMounted },
{ kEventClassVolume, kEventVolumeUnmounted } };
UInt32 numTypes = sizeof(types)/sizeof(types[0]);
OSStatus ret = InstallApplicationEventHandler( m_HandlerUPP, numTypes, types, driver, &m_Handler );
ASSERT( ret == noErr );
}
~Helper()
{
RemoveEventHandler( m_Handler );
DisposeEventHandlerUPP( m_HandlerUPP );
}
private:
static OSStatus VolumesChanged( EventHandlerCallRef ref, EventRef event, void *p )
{
MemoryCardDriverThreaded_MacOSX *driver = (MemoryCardDriverThreaded_MacOSX *)p;
LockMut( driver->m_ChangedLock );
driver->m_bChanged = true;
return eventNotHandledErr; // let others do something
}
EventHandlerUPP m_HandlerUPP;
EventHandlerRef m_Handler;
};
MemoryCardDriverThreaded_MacOSX::MemoryCardDriverThreaded_MacOSX() : m_ChangedLock( "MC changed lock" )
{
m_bChanged = true;
m_pHelper = new Helper( this );
}
MemoryCardDriverThreaded_MacOSX::~MemoryCardDriverThreaded_MacOSX()
{
delete m_pHelper;
}
void MemoryCardDriverThreaded_MacOSX::Unmount( UsbStorageDevice *pDevice )
{
ParamBlockRec pb;
Str255 name; // A pascal string.
const RString& base = Basename( pDevice->sOsMountDir );
memset( &pb, 0, sizeof(pb) );
name[0] = min( base.length(), size_t(255) );
strncpy( (char *)&name[1], base, name[0] );
pb.volumeParam.ioNamePtr = name;
pb.volumeParam.ioVolIndex = -1; // Use ioNamePtr to find the volume.
if( PBFlushVolSync(&pb) != noErr )
LOG->Warn( "Failed to flush the memory card." );
}
bool MemoryCardDriverThreaded_MacOSX::USBStorageDevicesChanged()
{
LockMut( m_ChangedLock );
return m_bChanged;
}
static int GetIntProperty( io_registry_entry_t entry, CFStringRef key )
{
CFTypeRef t = IORegistryEntryCreateCFProperty( entry, key, NULL, 0 );
if( !t )
return -1;
if( CFGetTypeID( t ) != CFNumberGetTypeID() )
{
CFRelease( t );
return -1;
}
int num;
if( !CFNumberGetValue(CFNumberRef(t), kCFNumberIntType, &num) )
num = -1;
CFRelease( t );
return num;
}
static RString GetStringProperty( io_registry_entry_t entry, CFStringRef key )
{
CFTypeRef t = IORegistryEntryCreateCFProperty( entry, key, NULL, 0 );
if( !t )
return RString();
if( CFGetTypeID( t ) != CFStringGetTypeID() )
{
CFRelease( t );
return RString();
}
CFStringRef s = CFStringRef( t );
RString ret;
const size_t len = CFStringGetMaximumSizeForEncoding( CFStringGetLength(s), kCFStringEncodingUTF8 );
char *buf = new char[len + 1];
if( CFStringGetCString( s, buf, len + 1, kCFStringEncodingUTF8 ) )
ret = buf;
delete[] buf;
CFRelease( t );
return ret;
}
void MemoryCardDriverThreaded_MacOSX::GetUSBStorageDevices( vector<UsbStorageDevice>& vDevicesOut )
{
LockMut( m_ChangedLock );
// First, get all device paths
struct statfs *fs;
int num = getfsstat( NULL, 0, MNT_NOWAIT );
fs = new struct statfs[num];
num = getfsstat( fs, num * sizeof(struct statfs), MNT_NOWAIT );
ASSERT( num != -1 );
for( int i = 0; i < num; ++i )
{
if( strncmp(fs[i].f_mntfromname, _PATH_DEV, strlen(_PATH_DEV)) )
continue;
const RString& sDevicePath = fs[i].f_mntfromname;
const RString& sDisk = Basename( sDevicePath ); // disk#[[s#] ...]
// Now that we have the disk name, look up the IOServices associated with it.
CFMutableDictionaryRef dict;
if( !(dict = IOBSDNameMatching(kIOMasterPortDefault, 0, sDisk)) )
continue;
// Look for certain properties: Leaf, Ejectable, Writable.
CFDictionarySetValue( dict, CFSTR(kIOMediaLeafKey), kCFBooleanTrue );
CFDictionarySetValue( dict, CFSTR(kIOMediaEjectableKey), kCFBooleanTrue );
CFDictionarySetValue( dict, CFSTR(kIOMediaWritableKey), kCFBooleanTrue );
// Get the matching iterator. As always, this consumes a reference to dict.
io_iterator_t iter;
kern_return_t ret = IOServiceGetMatchingServices( kIOMasterPortDefault, dict, &iter );
if( ret != KERN_SUCCESS || iter == 0 )
continue;
// I'm not quite sure what it means to have two services with this device.
// Iterate over them all. If one contains what we want, stop.
io_registry_entry_t device; // This is the same as an io_object_t.
while( (device = IOIteratorNext(iter)) )
{
// Look at the parent of the device until we see an IOUSBMassStorageClass
while( device != MACH_PORT_NULL && !IOObjectConformsTo(device, "IOUSBMassStorageClass") )
{
io_registry_entry_t entry;
ret = IORegistryEntryGetParentEntry( device, kIOServicePlane, &entry );
IOObjectRelease( device );
device = ret == KERN_SUCCESS? entry:MACH_PORT_NULL;
}
// Now look for the corresponding IOUSBDevice, it's likely 2 up the tree
while( device != MACH_PORT_NULL && !IOObjectConformsTo(device, "IOUSBDevice") )
{
io_registry_entry_t entry;
ret = IORegistryEntryGetParentEntry( device, kIOServicePlane, &entry );
IOObjectRelease( device );
device = ret == KERN_SUCCESS? entry:MACH_PORT_NULL;
}
if( device == MACH_PORT_NULL )
continue;
// At this point, it is pretty safe to say that we've found a USB device.
vDevicesOut.push_back( UsbStorageDevice() );
UsbStorageDevice& usbd = vDevicesOut.back();
LOG->Trace( "Found memory card at path: %s.", fs[i].f_mntonname );
usbd.SetOsMountDir( fs[i].f_mntonname );
usbd.iVolumeSizeMB = int( (uint64_t(fs[i].f_blocks) * fs[i].f_bsize) >> 20 );
// Now we can get some more information from the registry tree.
usbd.iBus = GetIntProperty( device, CFSTR("USB Address") );
usbd.iPort = GetIntProperty( device, CFSTR("PortNum") );
// usbd.iLevel ?
usbd.sSerial = GetStringProperty( device, CFSTR("USB Serial Number") );
usbd.sDevice = fs[i].f_mntfromname;
usbd.idVendor = GetIntProperty( device, CFSTR(kUSBVendorID) );
usbd.idProduct = GetIntProperty( device, CFSTR(kUSBProductID) );
usbd.sVendor = GetStringProperty( device, CFSTR("USB Vendor Name") );
usbd.sProduct = GetStringProperty( device, CFSTR("USB Product Name") );
IOObjectRelease( device );
break; // We found what we wanted
}
IOObjectRelease( iter );
}
m_bChanged = false;
delete[] fs;
}
bool MemoryCardDriverThreaded_MacOSX::TestWrite( UsbStorageDevice *pDevice )
{
if( access(pDevice->sOsMountDir, W_OK) )
{
pDevice->SetError( "TestFailed" );
return false;
}
return true;
}
/*
* (c) 2005-2006, 2008 Steve Checkoway
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
@@ -0,0 +1,61 @@
#ifndef MEMORY_CARD_DRIVER_THREADED_MACOSX_H
#define MEMORY_CARD_DRIVER_THREADED_MACOSX_H
#include "MemoryCardDriver.h"
#include "RageThreads.h"
class MemoryCardDriverThreaded_MacOSX : public MemoryCardDriver
{
public:
MemoryCardDriverThreaded_MacOSX();
~MemoryCardDriverThreaded_MacOSX();
bool Mount( UsbStorageDevice *pDevice ) { return true; }
void Unmount( UsbStorageDevice *pDevice );
protected:
bool USBStorageDevicesChanged();
void GetUSBStorageDevices( vector<UsbStorageDevice>& vStorageDevicesOut );
bool TestWrite( UsbStorageDevice *pDevice );
private:
MemoryCardDriverThreaded_MacOSX( const MemoryCardDriverThreaded_MacOSX &m );
MemoryCardDriverThreaded_MacOSX &operator=( const MemoryCardDriverThreaded_MacOSX &m );
bool m_bChanged;
RageMutex m_ChangedLock;
class Helper;
friend class Helper;
Helper *m_pHelper;
};
#ifdef ARCH_MEMORY_CARD_DRIVER
#error "More than one MemoryCardDriver selected."
#endif
#define ARCH_MEMORY_CARD_DRIVER MemoryCardDriverThreaded_MacOSX
#endif
/*
* (c) 2005-2006, 2008 Steve Checkoway
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
@@ -0,0 +1,232 @@
#include "global.h"
#include "MemoryCardDriverThreaded_Windows.h"
#include "RageUtil.h"
#include "RageLog.h"
#include "archutils/Win32/ErrorStrings.h"
#include "PlayerNumber.h"
#include "MemoryCardManager.h"
MemoryCardDriverThreaded_Windows::MemoryCardDriverThreaded_Windows()
{
m_dwLastLogicalDrives = 0;
}
MemoryCardDriverThreaded_Windows::~MemoryCardDriverThreaded_Windows()
{
}
static bool TestReady( const RString &sDrive, RString &sVolumeLabelOut )
{
TCHAR szVolumeNameBuffer[MAX_PATH];
DWORD dwVolumeSerialNumber;
DWORD dwMaximumComponentLength;
DWORD lpFileSystemFlags;
TCHAR szFileSystemNameBuffer[MAX_PATH];
if( !GetVolumeInformation(
sDrive,
szVolumeNameBuffer,
sizeof(szVolumeNameBuffer),
&dwVolumeSerialNumber,
&dwMaximumComponentLength,
&lpFileSystemFlags,
szFileSystemNameBuffer,
sizeof(szFileSystemNameBuffer)) )
return false;
sVolumeLabelOut = szVolumeNameBuffer;
return true;
}
bool MemoryCardDriverThreaded_Windows::TestWrite( UsbStorageDevice* pDevice )
{
/* 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 )
{
HANDLE hFile = CreateFile( ssprintf( "%stmp%i", pDevice->sOsMountDir.c_str(), RandomInt(100000)),
GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, CREATE_NEW, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, NULL );
if( hFile == INVALID_HANDLE_VALUE )
{
DWORD iError = GetLastError();
LOG->Warn( werr_ssprintf(iError, "Couldn't write to %s", pDevice->sOsMountDir.c_str()) );
if( iError == ERROR_FILE_EXISTS )
continue;
break;
}
CloseHandle( hFile );
return true;
}
pDevice->SetError( "TestFailed" );
return false;
}
static bool IsFloppyDrive( const RString &sDrive )
{
char szBuf[1024];
int iRet = QueryDosDevice( sDrive, szBuf, 1024 );
if( iRet == 0 )
{
LOG->Warn( werr_ssprintf(GetLastError(), "QueryDosDevice(%s)", sDrive.c_str()) );
return false;
}
// Make sure szBuf is terminated with two nulls. This only may be needed if the buffer filled.
szBuf[iRet-2] = 0;
szBuf[iRet-1] = 0;
const char *p = szBuf;
while( *p )
{
if( BeginsWith(p, "\\Device\\Floppy") )
return true;
p += strlen(p)+1;
}
return false;
}
void MemoryCardDriverThreaded_Windows::GetUSBStorageDevices( vector<UsbStorageDevice>& vDevicesOut )
{
LOG->Trace( "MemoryCardDriverThreaded_Windows::GetUSBStorageDevices" );
DWORD dwLogicalDrives = ::GetLogicalDrives();
m_dwLastLogicalDrives = dwLogicalDrives;
const int MAX_DRIVES = 26;
for( int i=0; i<MAX_DRIVES; ++i )
{
DWORD mask = (1 << i);
if( !(m_dwLastLogicalDrives & mask) )
continue; // drive letter is invalid
RString sDrive = ssprintf( "%c:", 'A'+i%26 );
LOG->Trace( sDrive );
if( IsFloppyDrive(sDrive) )
{
LOG->Trace( "IsFloppyDrive" );
continue;
}
// Testing hack: Allow non-removable drive letters to be used if that
// driver letter is specified as a m_sMemoryCardOsMountPoint.
bool bIsSpecifiedMountPoint = false;
FOREACH_ENUM( PlayerNumber, p )
bIsSpecifiedMountPoint |= MEMCARDMAN->m_sMemoryCardOsMountPoint[p].Get().EqualsNoCase(sDrive);
RString sDrivePath = sDrive + "\\";
if( bIsSpecifiedMountPoint )
{
LOG->Trace( "'%s' is a specified mount point. Allowing...", sDrive.c_str() );
}
else
{
if( GetDriveType(sDrivePath) != DRIVE_REMOVABLE )
{
LOG->Trace( "not DRIVE_REMOVABLE" );
continue;
}
}
RString sVolumeLabel;
if( !TestReady(sDrivePath, sVolumeLabel) )
{
LOG->Trace( "not TestReady" );
continue;
}
vDevicesOut.push_back( UsbStorageDevice() );
UsbStorageDevice &usbd = vDevicesOut.back();
usbd.SetOsMountDir( sDrive );
usbd.sDevice = "\\\\.\\" + sDrive;
usbd.sVolumeLabel = sVolumeLabel;
}
for( size_t i = 0; i < vDevicesOut.size(); ++i )
{
UsbStorageDevice &usbd = vDevicesOut[i];
// TODO: fill in bus/level/port with this:
// http://www.codeproject.com/system/EnumDeviceProperties.asp
// find volume size
DWORD dwSectorsPerCluster;
DWORD dwBytesPerSector;
DWORD dwNumberOfFreeClusters;
DWORD dwTotalNumberOfClusters;
if( GetDiskFreeSpace(
usbd.sOsMountDir,
&dwSectorsPerCluster,
&dwBytesPerSector,
&dwNumberOfFreeClusters,
&dwTotalNumberOfClusters ) )
{
usbd.iVolumeSizeMB = (int)roundf( dwTotalNumberOfClusters * (float)dwSectorsPerCluster * dwBytesPerSector / (1024*1024) );
}
}
}
bool MemoryCardDriverThreaded_Windows::USBStorageDevicesChanged()
{
return ::GetLogicalDrives() != m_dwLastLogicalDrives;
}
bool MemoryCardDriverThreaded_Windows::Mount( UsbStorageDevice* pDevice )
{
// nothing to do here...
return true;
}
void MemoryCardDriverThreaded_Windows::Unmount( UsbStorageDevice* pDevice )
{
/* Try to flush the device before returning. This requires administrator priviliges. */
HANDLE hDevice = CreateFile( pDevice->sDevice, GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
if( hDevice == INVALID_HANDLE_VALUE )
{
LOG->Warn( werr_ssprintf(GetLastError(), "Couldn't open memory card device to flush (%s): CreateFile", pDevice->sDevice.c_str()) );
return;
}
if( !FlushFileBuffers(hDevice) )
LOG->Warn( werr_ssprintf(GetLastError(), "Couldn't flush memory card device (%s): FlushFileBuffers", pDevice->sDevice.c_str()) );
CloseHandle( hDevice );
}
/*
* (c) 2003-2004 Chris Danford
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
@@ -0,0 +1,54 @@
#ifndef MemoryCardDriverThreaded_Windows_H
#define MemoryCardDriverThreaded_Windows_H
#include "MemoryCardDriver.h"
#include <windows.h>
class MemoryCardDriverThreaded_Windows: public MemoryCardDriver
{
public:
MemoryCardDriverThreaded_Windows();
virtual ~MemoryCardDriverThreaded_Windows();
virtual bool Mount( UsbStorageDevice* pDevice );
virtual void Unmount( UsbStorageDevice* pDevice );
private:
void GetUSBStorageDevices( vector<UsbStorageDevice>& vDevicesOut );
bool USBStorageDevicesChanged();
bool TestWrite( UsbStorageDevice* pDevice );
DWORD m_dwLastLogicalDrives;
};
#ifdef ARCH_MEMORY_CARD_DRIVER
#error "More than one MemoryCardDriver included!"
#endif
#define ARCH_MEMORY_CARD_DRIVER MemoryCardDriverThreaded_Windows
#endif
/*
* (c) 2003-2004 Chris Danford
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
@@ -0,0 +1,150 @@
#include "global.h"
#include "MemoryCardDriverThreaded_Xbox.h"
#include "RageUtil.h"
#include "RageLog.h"
MemoryCardDriverThreaded_Xbox::MemoryCardDriverThreaded_Xbox()
{
}
MemoryCardDriverThreaded_Xbox::~MemoryCardDriverThreaded_Xbox()
{
}
static bool TestReady( const RString &sDrive, RString &sVolumeLabelOut )
{
TCHAR szVolumeNameBuffer[MAX_PATH];
DWORD dwVolumeSerialNumber;
DWORD dwMaximumComponentLength;
DWORD lpFileSystemFlags;
TCHAR szFileSystemNameBuffer[MAX_PATH];
if( !GetVolumeInformation(
sDrive,
szVolumeNameBuffer,
sizeof(szVolumeNameBuffer),
&dwVolumeSerialNumber,
&dwMaximumComponentLength,
&lpFileSystemFlags,
szFileSystemNameBuffer,
sizeof(szFileSystemNameBuffer)) ){
LOG->Trace("GetVolumeInformation failed %u", GetLastError());
return false;
}
sVolumeLabelOut = szVolumeNameBuffer;
return true;
}
bool MemoryCardDriverThreaded_Xbox::TestWrite( UsbStorageDevice* pDevice )
{
/* Try to write a file, to check if the device is writable and that we have write permission.*/
for( int i = 0; i < 10; ++i )
{
HANDLE hFile = CreateFile(
ssprintf( "%s\\tmp%i", pDevice->sOsMountDir.c_str(), RandomInt(100000)),
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
CREATE_NEW,
FILE_FLAG_DELETE_ON_CLOSE,
NULL );
if( hFile == INVALID_HANDLE_VALUE )
{
DWORD iError = GetLastError();
LOG->Warn( "Couldn't write to %s (%u)", pDevice->sOsMountDir.c_str(), iError);
if( iError == ERROR_FILE_EXISTS )
continue;
break;
}
CloseHandle( hFile );
return true;
}
pDevice->SetError( "TestFailed" );
return false;
}
void MemoryCardDriverThreaded_Xbox::GetUSBStorageDevices( vector<UsbStorageDevice>& vDevicesOut )
{
DWORD devices=XGetDevices(XDEVICE_TYPE_MEMORY_UNIT);
for(int port=0;port<4;port++){
//top slot
if(devices&(1<<port)){
vDevicesOut.push_back( UsbStorageDevice() );
UsbStorageDevice &usbd = vDevicesOut.back();
usbd.iPort=port;
usbd.iLevel=0;
usbd.sDevice=ssprintf("Memory card on port %u on slot 0", port);
}
//bottom slot
if(devices&(1<<(port+16))){
vDevicesOut.push_back( UsbStorageDevice() );
UsbStorageDevice &usbd = vDevicesOut.back();
usbd.iPort=port;
usbd.iLevel=1;
usbd.sDevice=ssprintf("Memory card on port %u on slot 1", port);
}
}
}
bool MemoryCardDriverThreaded_Xbox::USBStorageDevicesChanged()
{
DWORD ins, rem;
return XGetDeviceChanges(XDEVICE_TYPE_MEMORY_UNIT, &ins, &rem)==TRUE;
}
bool MemoryCardDriverThreaded_Xbox::Mount( UsbStorageDevice* pDevice )
{
LOG->Trace( "%s", __FUNCTION__);
CHAR drive;
DWORD MountRetval=XMountMU(pDevice->iPort, pDevice->iLevel, &drive);
if(MountRetval==ERROR_SUCCESS){
LOG->Trace("Mounted memory card from port %u slot %u to %c:", pDevice->iPort, pDevice->iLevel, drive);
pDevice->SetOsMountDir(ssprintf("%c:", drive));
RString sVolumeLabel;
if( !TestReady(pDevice->sOsMountDir + "\\", sVolumeLabel) )
{
LOG->Trace( "not TestReady" );
}
pDevice->sVolumeLabel = sVolumeLabel;
return true;
}else{
LOG->Trace("Could not mount memory card %u", MountRetval);
return false;
}
}
void MemoryCardDriverThreaded_Xbox::Unmount( UsbStorageDevice* pDevice )
{
XUnmountMU(pDevice->iPort, pDevice->iLevel);
}
/*
* (c) 2003-2004 Chris Danford
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
@@ -0,0 +1,51 @@
#ifndef MemoryCardDriverThreaded_Xbox_H
#define MemoryCardDriverThreaded_Xbox_H
#include "MemoryCardDriver.h"
class MemoryCardDriverThreaded_Xbox: public MemoryCardDriver
{
public:
MemoryCardDriverThreaded_Xbox();
virtual ~MemoryCardDriverThreaded_Xbox();
virtual bool Mount( UsbStorageDevice* pDevice );
virtual void Unmount( UsbStorageDevice* pDevice );
private:
void GetUSBStorageDevices( vector<UsbStorageDevice>& vDevicesOut );
bool USBStorageDevicesChanged();
bool TestWrite( UsbStorageDevice* pDevice );
};
#ifdef ARCH_MEMORY_CARD_DRIVER
#error "More than one MemoryCardDriver included!"
#endif
#define ARCH_MEMORY_CARD_DRIVER MemoryCardDriverThreaded_Xbox
#endif
/*
* (c) 2003-2004 Chris Danford
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
@@ -0,0 +1,42 @@
#ifndef MEMORY_CARD_ENUMERATOR_NULL_H
#define MEMORY_CARD_ENUMERATOR_NULL_H
#include "MemoryCardDriver.h"
class MemoryCardDriver_Null : public MemoryCardDriver
{
public:
MemoryCardDriver_Null() {}
virtual bool USBStorageDevicesChanged() { return false; }
virtual void GetUSBStorageDevices( vector<UsbStorageDevice>& vDevicesOut ) { }
virtual bool Mount( UsbStorageDevice* pDevice ) { return false; }
virtual void Unmount( UsbStorageDevice* pDevice ) {}
virtual void Flush( UsbStorageDevice* pDevice ) {}
};
#endif
/*
* (c) 2003-2004 Chris Danford
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/