clean up formatting

This commit is contained in:
Chris Danford
2004-06-06 18:37:57 +00:00
parent da10ebd869
commit 9d2c6d078a
@@ -26,136 +26,136 @@ void GetNewStorageDevices( vector<UsbStorageDeviceEx>& vDevicesOut );
static int RunProgram( const char *path, char *const args[], CString &out ) static int RunProgram( const char *path, char *const args[], CString &out )
{ {
int fds[2]; int fds[2];
if( pipe(fds) == -1 ) if( pipe(fds) == -1 )
return -1; return -1;
int pid = fork(); int pid = fork();
if( pid == -1 ) if( pid == -1 )
{ {
close( fds[0] ); close( fds[0] );
close( fds[1] ); close( fds[1] );
return -1; return -1;
} }
if( pid == 0 ) if( pid == 0 )
{ {
close( fds[0] ); close( fds[0] );
close( fileno(stdout) ); close( fileno(stdout) );
dup2( fds[1], fileno(stdout) ); dup2( fds[1], fileno(stdout) );
for( int i = 0; i < 1024; ++i ) for( int i = 0; i < 1024; ++i )
if( i != fileno(stdout) ) if( i != fileno(stdout) )
close( i ); close( i );
execvp( path, args ); execvp( path, args );
_exit(1); _exit(1);
} }
close( fds[1] ); close( fds[1] );
while( 1 ) while( 1 )
{ {
char buf[1024]; char buf[1024];
int got = read( fds[0], buf, sizeof(buf) ); int got = read( fds[0], buf, sizeof(buf) );
if( got == -1 ) if( got == -1 )
{ {
if( errno == EINTR ) if( errno == EINTR )
continue; continue;
fprintf( stderr, "err %s\n", strerror(errno) ); fprintf( stderr, "err %s\n", strerror(errno) );
exit(0); exit(0);
} }
if( got == 0 ) if( got == 0 )
break; break;
out.append( buf, got ); out.append( buf, got );
} }
close( fds[0] ); close( fds[0] );
int status; int status;
int ret = waitpid( pid, &status, 0 ); int ret = waitpid( pid, &status, 0 );
if( ret == -1 ) if( ret == -1 )
return -1; return -1;
if( !WIFEXITED(status) ) if( !WIFEXITED(status) )
return -1; /* signal */ return -1; /* signal */
return WEXITSTATUS(status); return WEXITSTATUS(status);
} }
template<class T> template<class T>
bool VectorsAreEqual( const T &a, const T &b ) bool VectorsAreEqual( const T &a, const T &b )
{ {
if( a.size() != b.size() ) if( a.size() != b.size() )
return false; return false;
for( unsigned i=0; i<a.size(); i++ ) for( unsigned i=0; i<a.size(); i++ )
{ {
if( a[i] != b[i] ) if( a[i] != b[i] )
return false; return false;
} }
return true; return true;
} }
static bool TestWrite( CCStringRef sDir ) static bool TestWrite( CCStringRef sDir )
{ {
// Try to write a file. // Try to write a file.
// TODO: Can we use RageFile for this? // TODO: Can we use RageFile for this?
CString sFile = sDir + "/temp"; CString sFile = sDir + "/temp";
FILE* fp = fopen( sFile, "w" ); FILE* fp = fopen( sFile, "w" );
if( fp == NULL ) if( fp == NULL )
return false; return false;
fclose( fp ); fclose( fp );
remove( sFile ); remove( sFile );
return true; return true;
} }
static bool ExecuteCommand( CCStringRef sCommand ) static bool ExecuteCommand( CCStringRef sCommand )
{ {
LOG->Trace( "executing '%s'", sCommand.c_str() ); LOG->Trace( "executing '%s'", sCommand.c_str() );
int ret = system(sCommand); int ret = system(sCommand);
LOG->Trace( "done executing '%s'", sCommand.c_str() ); LOG->Trace( "done executing '%s'", sCommand.c_str() );
if( ret != 0 ) if( ret != 0 )
LOG->Warn( "failed to execute '%s' with error %d.", sCommand.c_str(), ret ); LOG->Warn( "failed to execute '%s' with error %d.", sCommand.c_str(), ret );
return ret == 0; return ret == 0;
} }
MemoryCardDriverThreaded_Linux::MemoryCardDriverThreaded_Linux() MemoryCardDriverThreaded_Linux::MemoryCardDriverThreaded_Linux()
{ {
m_fd = open(USB_DEVICE_LIST_FILE, O_RDONLY); m_fd = open(USB_DEVICE_LIST_FILE, O_RDONLY);
if( m_fd == -1 ) if( m_fd == -1 )
{ {
LOG->Warn( "Failed to open \"%s\": %s", USB_DEVICE_LIST_FILE, strerror(errno) ); LOG->Warn( "Failed to open \"%s\": %s", USB_DEVICE_LIST_FILE, strerror(errno) );
} }
this->StartThread(); this->StartThread();
} }
void MemoryCardDriverThreaded_Linux::MountThreadReset() void MemoryCardDriverThreaded_Linux::MountThreadReset()
{ {
// //
// if usb-storage gets in a bad state, resetting usb-storage will sometimes fix it. // if usb-storage gets in a bad state, resetting usb-storage will sometimes fix it.
// //
LockMut( m_mutexStorageDevices ); LockMut( m_mutexStorageDevices );
// unmount all devices before trying to remove the module // unmount all devices before trying to remove the module
for( unsigned i=0; i<m_vStorageDevices.size(); i++ ) for( unsigned i=0; i<m_vStorageDevices.size(); i++ )
{ {
UsbStorageDeviceEx &d = m_vStorageDevices[i]; UsbStorageDeviceEx &d = m_vStorageDevices[i];
CString sCommand = "umount " + d.sOsMountDir; CString sCommand = "umount " + d.sOsMountDir;
LOG->Trace( "reset unmount %i/%i (%s)", i, m_vStorageDevices.size(), sCommand.c_str() ); LOG->Trace( "reset unmount %i/%i (%s)", i, m_vStorageDevices.size(), sCommand.c_str() );
ExecuteCommand( sCommand ); ExecuteCommand( sCommand );
LOG->Trace( "reset unmount %i/%i done", i, m_vStorageDevices.size() ); LOG->Trace( "reset unmount %i/%i done", i, m_vStorageDevices.size() );
} }
ExecuteCommand( "rmmod usb-storage" ); ExecuteCommand( "rmmod usb-storage" );
ExecuteCommand( "modprobe usb-storage" ); ExecuteCommand( "modprobe usb-storage" );
m_vDevicesLastSeen.clear(); // redetect all devices m_vDevicesLastSeen.clear(); // redetect all devices
} }
void MemoryCardDriverThreaded_Linux::MountThreadDoOneUpdate() void MemoryCardDriverThreaded_Linux::MountThreadDoOneUpdate()
@@ -174,151 +174,151 @@ void MemoryCardDriverThreaded_Linux::MountThreadDoOneUpdate()
return; return;
case -1: case -1:
if( errno != EINTR ) if( errno != EINTR )
LOG->Warn( "Error polling: %s", strerror(errno) ); LOG->Warn( "Error polling: %s", strerror(errno) );
return; return;
} }
// TRICKY: We're waiting for a change in the USB device list, but // TRICKY: We're waiting for a change in the USB device list, but
// the usb-storage descriptors take a bit longer to update. It's more convenient to wait // the usb-storage descriptors take a bit longer to update. It's more convenient to wait
// on the USB device list because the usb-storage descriptors are separate files per // on the USB device list because the usb-storage descriptors are separate files per
// device. So, sleep for a little bit of time after we detect a new USB device and give // device. So, sleep for a little bit of time after we detect a new USB device and give
// usb-storage a chance to initialize. // usb-storage a chance to initialize.
usleep(1000*300); usleep(1000*300);
vector<UsbStorageDeviceEx> vDevicesNow; vector<UsbStorageDeviceEx> vDevicesNow;
GetNewStorageDevices( vDevicesNow ); GetNewStorageDevices( vDevicesNow );
vector<UsbStorageDeviceEx> &vNew = vDevicesNow; vector<UsbStorageDeviceEx> &vNew = vDevicesNow;
vector<UsbStorageDeviceEx> &vOld = m_vDevicesLastSeen; vector<UsbStorageDeviceEx> &vOld = m_vDevicesLastSeen;
// check for disconnects // check for disconnects
vector<UsbStorageDeviceEx*> vDisconnects; vector<UsbStorageDeviceEx*> vDisconnects;
for( unsigned i=0; i<vOld.size(); i++ ) for( unsigned i=0; i<vOld.size(); i++ )
{ {
UsbStorageDeviceEx &old = vOld[i]; UsbStorageDeviceEx &old = vOld[i];
if( find(vNew.begin(),vNew.end(),old) == vNew.end() )// didn't find if( find(vNew.begin(),vNew.end(),old) == vNew.end() )// didn't find
{ {
LOG->Trace( ssprintf("Disconnected bus %d port %d level %d path %s", old.iBus, old.iPort, old.iLevel, old.sOsMountDir.c_str()) ); LOG->Trace( ssprintf("Disconnected bus %d port %d level %d path %s", old.iBus, old.iPort, old.iLevel, old.sOsMountDir.c_str()) );
vDisconnects.push_back( &old ); vDisconnects.push_back( &old );
} }
} }
// check for connects // check for connects
vector<UsbStorageDeviceEx*> vConnects; vector<UsbStorageDeviceEx*> vConnects;
for( unsigned i=0; i<vNew.size(); i++ ) for( unsigned i=0; i<vNew.size(); i++ )
{ {
UsbStorageDeviceEx &newd = vNew[i]; UsbStorageDeviceEx &newd = vNew[i];
if( find(vOld.begin(),vOld.end(),newd) == vOld.end() )// didn't find if( find(vOld.begin(),vOld.end(),newd) == vOld.end() )// didn't find
{ {
LOG->Trace( ssprintf("Connected bus %d port %d level %d path %s", newd.iBus, newd.iPort, newd.iLevel, newd.sOsMountDir.c_str()) ); LOG->Trace( ssprintf("Connected bus %d port %d level %d path %s", newd.iBus, newd.iPort, newd.iLevel, newd.sOsMountDir.c_str()) );
vConnects.push_back( &newd ); vConnects.push_back( &newd );
} }
} }
// unmount all disconnects // unmount all disconnects
for( unsigned i=0; i<vDisconnects.size(); i++ ) for( unsigned i=0; i<vDisconnects.size(); i++ )
{ {
UsbStorageDeviceEx &d = *vDisconnects[i]; UsbStorageDeviceEx &d = *vDisconnects[i];
CString sCommand = "umount " + d.sOsMountDir; CString sCommand = "umount " + d.sOsMountDir;
LOG->Trace( "unmount disconnects %i/%i (%s)", i, vDisconnects.size(), sCommand.c_str() ); LOG->Trace( "unmount disconnects %i/%i (%s)", i, vDisconnects.size(), sCommand.c_str() );
ExecuteCommand( sCommand ); ExecuteCommand( sCommand );
LOG->Trace( "unmount disconnects %i/%i done", i, vDisconnects.size() ); LOG->Trace( "unmount disconnects %i/%i done", i, vDisconnects.size() );
} }
// mount all connects // mount all connects
for( unsigned i=0; i<vConnects.size(); i++ ) for( unsigned i=0; i<vConnects.size(); i++ )
{ {
UsbStorageDeviceEx &d = *vConnects[i]; UsbStorageDeviceEx &d = *vConnects[i];
CString sCommand; CString sCommand;
// unmount this device before trying to mount it. If this device // unmount this device before trying to mount it. If this device
// wasn't unmounted before, then our mount call will fail and the // wasn't unmounted before, then our mount call will fail and the
// mount may contain an out-of-date view of the files on the device. // mount may contain an out-of-date view of the files on the device.
sCommand = "umount " + d.sOsMountDir; sCommand = "umount " + d.sOsMountDir;
LOG->Trace( "unmount old connect %i/%i (%s)", i, vConnects.size(), sCommand.c_str() ); LOG->Trace( "unmount old connect %i/%i (%s)", i, vConnects.size(), sCommand.c_str() );
ExecuteCommand( sCommand ); // don't care if this fails ExecuteCommand( sCommand ); // don't care if this fails
LOG->Trace( "unmount old connect %i/%i done", i, vConnects.size() ); LOG->Trace( "unmount old connect %i/%i done", i, vConnects.size() );
sCommand = "mount " + d.sOsMountDir; sCommand = "mount " + d.sOsMountDir;
LOG->Trace( "unmount new connect %i/%i (%s)", i, vConnects.size(), sCommand.c_str() ); LOG->Trace( "unmount new connect %i/%i (%s)", i, vConnects.size(), sCommand.c_str() );
bool bMountedSuccessfully = ExecuteCommand( sCommand ); bool bMountedSuccessfully = ExecuteCommand( sCommand );
LOG->Trace( "unmount new connect %i/%i done", i, vConnects.size() ); LOG->Trace( "unmount new connect %i/%i done", i, vConnects.size() );
d.bWriteTestSucceeded = bMountedSuccessfully && TestWrite( d.sOsMountDir ); d.bWriteTestSucceeded = bMountedSuccessfully && TestWrite( d.sOsMountDir );
// read name // read name
this->Mount( &d, TEMP_MOUNT_POINT ); this->Mount( &d, TEMP_MOUNT_POINT );
FILEMAN->FlushDirCache( TEMP_MOUNT_POINT ); FILEMAN->FlushDirCache( TEMP_MOUNT_POINT );
Profile profile; Profile profile;
CString sProfileDir = TEMP_MOUNT_POINT + PREFSMAN->m_sMemoryCardProfileSubdir + '/'; CString sProfileDir = TEMP_MOUNT_POINT + PREFSMAN->m_sMemoryCardProfileSubdir + '/';
profile.LoadEditableDataFromDir( sProfileDir ); profile.LoadEditableDataFromDir( sProfileDir );
d.sName = profile.GetDisplayName(); d.sName = profile.GetDisplayName();
LOG->Trace( "write test %s", d.bWriteTestSucceeded ? "succeeded" : "failed" ); LOG->Trace( "write test %s", d.bWriteTestSucceeded ? "succeeded" : "failed" );
} }
if( !vDisconnects.empty() || !vConnects.empty() ) if( !vDisconnects.empty() || !vConnects.empty() )
{ {
LockMut( m_mutexStorageDevices ); LockMut( m_mutexStorageDevices );
m_bStorageDevicesChanged = true; m_bStorageDevicesChanged = true;
m_vStorageDevices = vDevicesNow; m_vStorageDevices = vDevicesNow;
for( unsigned i=0; i<m_vStorageDevices.size(); i++ ) for( unsigned i=0; i<m_vStorageDevices.size(); i++ )
{ {
UsbStorageDeviceEx &d = m_vStorageDevices[i]; UsbStorageDeviceEx &d = m_vStorageDevices[i];
LOG->Trace( "index %d, bWriteTestSucceeded %d", i, d.bWriteTestSucceeded ); LOG->Trace( "index %d, bWriteTestSucceeded %d", i, d.bWriteTestSucceeded );
} }
} }
m_vDevicesLastSeen = vDevicesNow; m_vDevicesLastSeen = vDevicesNow;
CHECKPOINT; CHECKPOINT;
} }
bool ReadUsbStorageDescriptor( CString fn, int iScsiIndex, vector<UsbStorageDeviceEx>& vDevicesOut ) bool ReadUsbStorageDescriptor( CString fn, int iScsiIndex, vector<UsbStorageDeviceEx>& vDevicesOut )
{ {
LOG->Trace( "ReadUsbStorageDescriptor %s", fn.c_str() ); LOG->Trace( "ReadUsbStorageDescriptor %s", fn.c_str() );
// Read the usb-storage descriptor. It looks like: // Read the usb-storage descriptor. It looks like:
// Host scsi0: usb-storage // Host scsi0: usb-storage
// Vendor: KINGSTON // Vendor: KINGSTON
// Product: USB DRIVE // Product: USB DRIVE
// Serial Number: 1125198948886 // Serial Number: 1125198948886
// Protocol: Transparent SCSI // Protocol: Transparent SCSI
// Transport: Bulk // Transport: Bulk
// GUID: 04e801000001125198948886 // GUID: 04e801000001125198948886
// Attached: Yes // Attached: Yes
ifstream f; ifstream f;
f.open(fn); f.open(fn);
if( !f.is_open() ) if( !f.is_open() )
return false; return false;
CString sLine; CString sLine;
while( getline(f, sLine) ) while( getline(f, sLine) )
{ {
// Serial Number: 1125198948886 // Serial Number: 1125198948886
char szSerial[1024]; char szSerial[1024];
int iRet = sscanf( sLine.c_str(), "Serial Number: %[^\n]", szSerial ); int iRet = sscanf( sLine.c_str(), "Serial Number: %[^\n]", szSerial );
if( iRet == 1 ) // we found our line if( iRet == 1 ) // we found our line
{
// Search for the device corresponding to this serial number.
for( unsigned j=0; j<vDevicesOut.size(); j++ )
{
UsbStorageDevice& usbd = vDevicesOut[j];
if( usbd.sSerial == szSerial )
{ {
usbd.iScsiIndex = iScsiIndex; // Search for the device corresponding to this serial number.
LOG->Trace( "iScsiIndex: %d, iBus: %d, iLevel: %d, iPort: %d, sSerial: %s", for( unsigned j=0; j<vDevicesOut.size(); j++ )
usbd.iScsiIndex, usbd.iBus, usbd.iLevel, usbd.iPort, usbd.sSerial.c_str() ); {
break; // done looking for the corresponding device. UsbStorageDevice& usbd = vDevicesOut[j];
if( usbd.sSerial == szSerial )
{
usbd.iScsiIndex = iScsiIndex;
LOG->Trace( "iScsiIndex: %d, iBus: %d, iLevel: %d, iPort: %d, sSerial: %s",
usbd.iScsiIndex, usbd.iBus, usbd.iLevel, usbd.iPort, usbd.sSerial.c_str() );
break; // done looking for the corresponding device.
}
}
break; // we already found the line we care about
} }
}
break; // we already found the line we care about
}
} }
return true; return true;
} }
#if 0 #if 0
@@ -387,151 +387,151 @@ static bool GetPortAndLevelFromSerial( CString sSerial, UsbStorageDeviceEx &usbd
void GetNewStorageDevices( vector<UsbStorageDeviceEx>& vDevicesOut ) void GetNewStorageDevices( vector<UsbStorageDeviceEx>& vDevicesOut )
{ {
LOG->Trace( "GetNewStorageDevices" ); LOG->Trace( "GetNewStorageDevices" );
vDevicesOut.clear(); vDevicesOut.clear();
{ {
// Bus 002 Device 001: ID 0000:0000 // Bus 002 Device 001: ID 0000:0000
// iSerial 3 1125198948886 // iSerial 3 1125198948886
// bInterfaceClass 8 Mass Storage // bInterfaceClass 8 Mass Storage
CString sCommand = "/usr/sbin/lsusb";
char *szParams[] = { "/usr/sbin/lsusb", "-v", NULL };
CString sOutput;
LOG->Trace( sCommand );
RunProgram( sCommand, szParams, sOutput );
CStringArray vsLines;
split( sOutput, "\n", vsLines );
UsbStorageDeviceEx usbd;
for( unsigned i=0; i<vsLines.size(); i++ )
{
CString &sLine = vsLines[i];
int iRet, iThrowAway;
// Bus 002 Device 001: ID 0000:0000
int iBus;
iRet = sscanf( sLine.c_str(), "Bus %d", &iBus );
if( iRet == 1 )
{
usbd.iBus = iBus;
LOG->Trace( "iBus = %d", iBus );
continue; // stop processing this line
}
// iSerial 3 1125198948886
char szSerial[1024];
iRet = sscanf( sLine.c_str(), " iSerial %d %[^\n]", &iThrowAway, szSerial );
if( iRet == 2 )
{
usbd.sSerial = szSerial;
LOG->Trace( "sSerial = %s", szSerial );
continue; // stop processing this line
}
// bInterfaceClass 8 Mass Storage
int iClass;
iRet = sscanf( sLine.c_str(), " bInterfaceClass %d", &iClass );
if( iRet == 1 )
{
if( iClass == 8 ) // storage class
{
vDevicesOut.push_back( usbd );
LOG->Trace( "iScsiIndex: %d, iBus: %d, iLevel: %d, iPort: %d",
usbd.iScsiIndex, usbd.iBus, usbd.iLevel, usbd.iPort );
}
continue; // stop processing this line
}
}
}
{
// Find the usb-storage device index for all storage class devices.
const CString sDir = "/proc/scsi/usb-storage/";
LOG->Trace( sDir );
DIR *dirp = opendir( sDir );
if( dirp )
{
// 2.6 kernel style
struct dirent *direntp;
while ( (direntp = readdir( dirp )) != NULL )
{
if( stricmp(direntp->d_name,".")==0 || stricmp(direntp->d_name,"..")==0 )
continue;
int iScsiIndex = atoi( direntp->d_name );
CString fn = sDir + direntp->d_name;
ReadUsbStorageDescriptor( fn, iScsiIndex, vDevicesOut );
}
closedir( dirp );
dirp = NULL;
// Get the mapping from Scsi device number to Scsi file device. Requires "sg-utils".
// sg_scan. It looks like:
// /dev/sg0: scsi47 channel=0 id=0 lun=0 [em] type=0
// /dev/sg1: scsi46 channel=0 id=0 lun=0 [em] type=0
CString sCommand = "/usr/sbin/lsusb";
char *szParams[] = { "/usr/sbin/lsusb", "-v", NULL };
CString sOutput; CString sOutput;
CString sCommand = "/usr/bin/sg_scan";
LOG->Trace( sCommand ); LOG->Trace( sCommand );
RunProgram( sCommand, NULL, sOutput ); RunProgram( sCommand, szParams, sOutput );
CStringArray vsLines; CStringArray vsLines;
split( sOutput, "\n", vsLines ); split( sOutput, "\n", vsLines );
UsbStorageDeviceEx usbd;
for( unsigned i=0; i<vsLines.size(); i++ ) for( unsigned i=0; i<vsLines.size(); i++ )
{
const CString& sLine = vsLines[i];
LOG->Trace( "sLine: %s", sLine.c_str() );
int iSg;
int iScsiIndex;
int iRet = sscanf( sLine.c_str(), "/dev/sg%i: scsi%d", &iSg, &iScsiIndex );
if( iRet != 2 )
continue; // don't process this line
// search for the usb-storage device corresponding to the SCSI device
for( unsigned i=0; i<vDevicesOut.size(); i++ )
{
UsbStorageDevice& usbd = vDevicesOut[i];
if( usbd.iScsiIndex == iScsiIndex ) // found our match
{
usbd.sScsiDevice = ssprintf("/dev/sd%c",'a'+(char)iSg);
LOG->Trace( "iScsiIndex: %d, iBus: %d, iLevel: %d, iPort: %d, sScsiDevice: %s",
usbd.iScsiIndex, usbd.iBus, usbd.iLevel, usbd.iPort, usbd.sScsiDevice.c_str() );
break; // stop looking for a match
}
}
}
}
else
{
// 2.4 kernel style
// Find the usb-storage device index for all storage class devices.
for( unsigned i=0; true; i++ )
{ {
CString fn = ssprintf( "/proc/scsi/usb-storage-%d/%d", i, i ); CString &sLine = vsLines[i];
if( !ReadUsbStorageDescriptor( fn, i, vDevicesOut ) )
break;
}
for( unsigned i=0; i<vDevicesOut.size(); i++ ) int iRet, iThrowAway;
// Bus 002 Device 001: ID 0000:0000
int iBus;
iRet = sscanf( sLine.c_str(), "Bus %d", &iBus );
if( iRet == 1 )
{
usbd.iBus = iBus;
LOG->Trace( "iBus = %d", iBus );
continue; // stop processing this line
}
// iSerial 3 1125198948886
char szSerial[1024];
iRet = sscanf( sLine.c_str(), " iSerial %d %[^\n]", &iThrowAway, szSerial );
if( iRet == 2 )
{
usbd.sSerial = szSerial;
LOG->Trace( "sSerial = %s", szSerial );
continue; // stop processing this line
}
// bInterfaceClass 8 Mass Storage
int iClass;
iRet = sscanf( sLine.c_str(), " bInterfaceClass %d", &iClass );
if( iRet == 1 )
{
if( iClass == 8 ) // storage class
{
vDevicesOut.push_back( usbd );
LOG->Trace( "iScsiIndex: %d, iBus: %d, iLevel: %d, iPort: %d",
usbd.iScsiIndex, usbd.iBus, usbd.iLevel, usbd.iPort );
}
continue; // stop processing this line
}
}
}
{
// Find the usb-storage device index for all storage class devices.
const CString sDir = "/proc/scsi/usb-storage/";
LOG->Trace( sDir );
DIR *dirp = opendir( sDir );
if( dirp )
{ {
UsbStorageDevice& usbd = vDevicesOut[i]; // 2.6 kernel style
usbd.sScsiDevice = ssprintf("/dev/sd%c",'a'+(char)usbd.iScsiIndex);
struct dirent *direntp;
while ( (direntp = readdir( dirp )) != NULL )
{
if( stricmp(direntp->d_name,".")==0 || stricmp(direntp->d_name,"..")==0 )
continue;
int iScsiIndex = atoi( direntp->d_name );
CString fn = sDir + direntp->d_name;
ReadUsbStorageDescriptor( fn, iScsiIndex, vDevicesOut );
}
closedir( dirp );
dirp = NULL;
// Get the mapping from Scsi device number to Scsi file device. Requires "sg-utils".
// sg_scan. It looks like:
// /dev/sg0: scsi47 channel=0 id=0 lun=0 [em] type=0
// /dev/sg1: scsi46 channel=0 id=0 lun=0 [em] type=0
CString sOutput;
CString sCommand = "/usr/bin/sg_scan";
LOG->Trace( sCommand );
RunProgram( sCommand, NULL, sOutput );
CStringArray vsLines;
split( sOutput, "\n", vsLines );
for( unsigned i=0; i<vsLines.size(); i++ )
{
const CString& sLine = vsLines[i];
LOG->Trace( "sLine: %s", sLine.c_str() );
int iSg;
int iScsiIndex;
int iRet = sscanf( sLine.c_str(), "/dev/sg%i: scsi%d", &iSg, &iScsiIndex );
if( iRet != 2 )
continue; // don't process this line
// search for the usb-storage device corresponding to the SCSI device
for( unsigned i=0; i<vDevicesOut.size(); i++ )
{
UsbStorageDevice& usbd = vDevicesOut[i];
if( usbd.iScsiIndex == iScsiIndex ) // found our match
{
usbd.sScsiDevice = ssprintf("/dev/sd%c",'a'+(char)iSg);
LOG->Trace( "iScsiIndex: %d, iBus: %d, iLevel: %d, iPort: %d, sScsiDevice: %s",
usbd.iScsiIndex, usbd.iBus, usbd.iLevel, usbd.iPort, usbd.sScsiDevice.c_str() );
break; // stop looking for a match
}
}
}
}
else
{
// 2.4 kernel style
// Find the usb-storage device index for all storage class devices.
for( unsigned i=0; true; i++ )
{
CString fn = ssprintf( "/proc/scsi/usb-storage-%d/%d", i, i );
if( !ReadUsbStorageDescriptor( fn, i, vDevicesOut ) )
break;
}
for( unsigned i=0; i<vDevicesOut.size(); i++ )
{
UsbStorageDevice& usbd = vDevicesOut[i];
usbd.sScsiDevice = ssprintf("/dev/sd%c",'a'+(char)usbd.iScsiIndex);
}
} }
}
} }
{ {
@@ -589,7 +589,7 @@ void GetNewStorageDevices( vector<UsbStorageDeviceEx>& vDevicesOut )
} }
} }
leave: leave:
/* Remove any devices that we couldn't find a mountpoint for. */ /* Remove any devices that we couldn't find a mountpoint for. */
for( unsigned i=0; i<vDevicesOut.size(); i++ ) for( unsigned i=0; i<vDevicesOut.size(); i++ )
@@ -608,22 +608,22 @@ void GetNewStorageDevices( vector<UsbStorageDeviceEx>& vDevicesOut )
void MemoryCardDriverThreaded_Linux::Mount( UsbStorageDevice* pDevice, CString sMountPoint ) void MemoryCardDriverThreaded_Linux::Mount( UsbStorageDevice* pDevice, CString sMountPoint )
{ {
ASSERT( !pDevice->sOsMountDir.empty() ); ASSERT( !pDevice->sOsMountDir.empty() );
/* Unmount any previous mounts for this mountpoint. */ /* Unmount any previous mounts for this mountpoint. */
vector<RageFileManager::DriverLocation> Mounts; vector<RageFileManager::DriverLocation> Mounts;
FILEMAN->GetLoadedDrivers( Mounts ); FILEMAN->GetLoadedDrivers( Mounts );
for( unsigned i = 0; i < Mounts.size(); ++i ) for( unsigned i = 0; i < Mounts.size(); ++i )
{ {
if( Mounts[i].Type.CompareNoCase( "dir" ) ) if( Mounts[i].Type.CompareNoCase( "dir" ) )
continue; // wrong type continue; // wrong type
if( Mounts[i].MountPoint.CompareNoCase( sMountPoint ) ) if( Mounts[i].MountPoint.CompareNoCase( sMountPoint ) )
continue; // wrong mount point continue; // wrong mount point
FILEMAN->Unmount( Mounts[i].Type, Mounts[i].Root, Mounts[i].MountPoint ); FILEMAN->Unmount( Mounts[i].Type, Mounts[i].Root, Mounts[i].MountPoint );
} }
FILEMAN->Mount( "dir", pDevice->sOsMountDir, sMountPoint.c_str() ); FILEMAN->Mount( "dir", pDevice->sOsMountDir, sMountPoint.c_str() );
LOG->Trace( "FILEMAN->Mount %s %s", pDevice->sOsMountDir.c_str(), sMountPoint.c_str() ); LOG->Trace( "FILEMAN->Mount %s %s", pDevice->sOsMountDir.c_str(), sMountPoint.c_str() );
} }
void MemoryCardDriverThreaded_Linux::Unmount( UsbStorageDevice* pDevice, CString sMountPoint ) void MemoryCardDriverThreaded_Linux::Unmount( UsbStorageDevice* pDevice, CString sMountPoint )