Refactor device parsing logic into Proc_Partitions_Info class

The reason for refactoring is to simplify the large GParted_Core
class, to help minimize disk reads, and to group the logic for
processing the file /proc/partitions into a single logical class.
This commit is contained in:
Curtis Gedak 2010-12-07 16:01:54 -07:00
parent 319255d3bc
commit fd77e73c46
3 changed files with 42 additions and 29 deletions

View File

@ -36,10 +36,12 @@ public:
Proc_Partitions_Info() ;
Proc_Partitions_Info( bool do_refresh ) ;
~Proc_Partitions_Info() ;
std::vector<Glib::ustring> get_device_paths() ;
std::vector<Glib::ustring> get_alternate_paths( const Glib::ustring & path ) ;
private:
void load_proc_partitions_info_cache() ;
static bool proc_partitions_info_cache_initialized ;
static std::vector<Glib::ustring> device_paths_cache ;
static std::map< Glib::ustring, Glib::ustring > alternate_paths_cache ;
};

View File

@ -174,36 +174,17 @@ void GParted_Core::set_devices( std::vector<Device> & devices )
// This was a problem with no floppy drive yet BIOS indicated one existed.
// http://parted.alioth.debian.org/cgi-bin/trac.cgi/ticket/194
//
//try to find all available devices
std::ifstream proc_partitions( "/proc/partitions" ) ;
if ( proc_partitions )
//try to find all available devices if devices exist in /proc/partitions
std::vector<Glib::ustring> temp_devices = pp_info .get_device_paths() ;
if ( ! temp_devices .empty() )
{
//parse device names from /proc/partitions
std::string line ;
std::string device ;
while ( getline( proc_partitions, line ) )
//Try to find all devices in /proc/partitions
for (unsigned int k=0; k < temp_devices .size(); k++)
{
//Whole disk devices are the ones we want.
//Device names without a trailing digit refer to the whole disk.
device = Utils::regexp_label(line, "^[\t ]+[0-9]+[\t ]+[0-9]+[\t ]+[0-9]+[\t ]+([^0-9]+)$") ;
//Recognize /dev/mmcblk* devices.
//E.g., device = /dev/mmcblk0, partition = /dev/mmcblk0p1
if ( device == "" )
device = Utils::regexp_label(line, "^[\t ]+[0-9]+[\t ]+[0-9]+[\t ]+[0-9]+[\t ]+(mmcblk[0-9]+)$") ;
//Device names that end with a #[^p]# are HP Smart Array Devices (disks)
//E.g., device = /dev/cciss/c0d0, partition = /dev/cciss/c0d0p1
if ( device == "" )
device = Utils::regexp_label(line, "^[\t ]+[0-9]+[\t ]+[0-9]+[\t ]+[0-9]+[\t ]+(.*[0-9]+[^p]{1}[0-9]+)$") ;
if ( device != "" )
{
//try to have libparted detect the device and add it to the list
device = "/dev/" + device;
/*TO TRANSLATORS: looks like Scanning /dev/sda */
set_thread_status_message( String::ucompose ( _("Scanning %1"), device ) ) ;
ped_device_get( device .c_str() ) ;
}
/*TO TRANSLATORS: looks like Scanning /dev/sda */
set_thread_status_message( String::ucompose ( _("Scanning %1"), temp_devices[ k ] ) ) ;
ped_device_get( temp_devices[ k ] .c_str() ) ;
}
proc_partitions .close() ;
//Try to find all swraid devices
if (swraid .is_swraid_supported() ) {
@ -229,7 +210,7 @@ void GParted_Core::set_devices( std::vector<Device> & devices )
}
else
{
//file /proc/partitions doesn't exist so use libparted to probe devices
//No devices found in /proc/partitions so use libparted to probe devices
ped_device_probe_all();
}

View File

@ -24,6 +24,7 @@ namespace GParted
//Initialize static data elements
bool Proc_Partitions_Info::proc_partitions_info_cache_initialized = false ;
std::vector<Glib::ustring> Proc_Partitions_Info::device_paths_cache ;
std::map< Glib::ustring, Glib::ustring > Proc_Partitions_Info::alternate_paths_cache ;
Proc_Partitions_Info::Proc_Partitions_Info()
@ -54,6 +55,11 @@ Proc_Partitions_Info::~Proc_Partitions_Info()
{
}
std::vector<Glib::ustring> Proc_Partitions_Info::get_device_paths()
{
return device_paths_cache ;
}
std::vector<Glib::ustring> Proc_Partitions_Info::get_alternate_paths( const Glib::ustring & path )
{
std::vector<Glib::ustring> paths ;
@ -70,15 +76,38 @@ std::vector<Glib::ustring> Proc_Partitions_Info::get_alternate_paths( const Glib
void Proc_Partitions_Info::load_proc_partitions_info_cache()
{
alternate_paths_cache .clear();
device_paths_cache .clear() ;
//Initialize alternate_paths
std::string line ;
std::ifstream proc_partitions( "/proc/partitions" ) ;
if ( proc_partitions )
{
std::string line ;
std::string device ;
char c_str[4096+1] ;
while ( getline( proc_partitions, line ) )
{
//Build cache of disk devices.
//Whole disk devices are the ones we want.
//Device names without a trailing digit refer to the whole disk.
device = Utils::regexp_label(line, "^[\t ]+[0-9]+[\t ]+[0-9]+[\t ]+[0-9]+[\t ]+([^0-9]+)$") ;
//Recognize /dev/mmcblk* devices.
//E.g., device = /dev/mmcblk0, partition = /dev/mmcblk0p1
if ( device == "" )
device = Utils::regexp_label(line, "^[\t ]+[0-9]+[\t ]+[0-9]+[\t ]+[0-9]+[\t ]+(mmcblk[0-9]+)$") ;
//Device names that end with a #[^p]# are HP Smart Array Devices (disks)
//E.g., device = /dev/cciss/c0d0, partition = /dev/cciss/c0d0p1
if ( device == "" )
device = Utils::regexp_label(line, "^[\t ]+[0-9]+[\t ]+[0-9]+[\t ]+[0-9]+[\t ]+(.*[0-9]+[^p]{1}[0-9]+)$") ;
if ( device != "" )
{
//add potential device to the list
device = "/dev/" + device;
device_paths_cache .push_back( device ) ;
}
//Build cache of potential alternate paths
if ( sscanf( line .c_str(), "%*d %*d %*d %4096s", c_str ) == 1 )
{
line = "/dev/" ;
@ -97,6 +126,7 @@ void Proc_Partitions_Info::load_proc_partitions_info_cache()
}
}
}
proc_partitions .close() ;
}
}