Switch to static class interface for FS_Info

The FS_Info module has a pseudo multi-object interface and used the
constructor to load the cache.  However all the data in the class is
static.  An FS_Info object doesn't contain any member variables, yet was
needed just to call the member functions.

Make all the member functions static removing the need to use any
FS_Info objects and provide an explicit load_cache() method.
This commit is contained in:
Mike Fleetwood 2016-07-08 16:26:32 +01:00 committed by Curtis Gedak
parent 5055c0b638
commit a94be2da05
3 changed files with 32 additions and 45 deletions

View file

@ -38,16 +38,15 @@ struct FS_Entry
class FS_Info
{
public:
FS_Info() ;
FS_Info( bool do_refresh ) ;
~FS_Info() ;
Glib::ustring get_fs_type( const Glib::ustring & path ) ;
Glib::ustring get_label( const Glib::ustring & path, bool & found ) ;
Glib::ustring get_uuid( const Glib::ustring & path ) ;
Glib::ustring get_path_by_uuid( const Glib::ustring & uuid ) ;
Glib::ustring get_path_by_label( const Glib::ustring & label ) ;
static void load_cache();
static Glib::ustring get_fs_type( const Glib::ustring & path );
static Glib::ustring get_label( const Glib::ustring & path, bool & found );
static Glib::ustring get_uuid( const Glib::ustring & path );
static Glib::ustring get_path_by_uuid( const Glib::ustring & uuid );
static Glib::ustring get_path_by_label( const Glib::ustring & label );
private:
static void initialize_if_required();
static void set_commands_found();
static void load_fs_info_cache();
static const FS_Entry & get_cache_entry_by_path( const Glib::ustring & path );

View file

@ -46,39 +46,17 @@ bool FS_Info::need_blkid_vfat_cache_update_workaround = true;
// ]
std::vector<FS_Entry> FS_Info::fs_info_cache;
FS_Info::FS_Info()
{
//Ensure that cache has been loaded at least once
if ( ! fs_info_cache_initialized )
{
fs_info_cache_initialized = true ;
set_commands_found() ;
load_fs_info_cache() ;
}
}
FS_Info:: FS_Info( bool do_refresh )
{
//Ensure that cache has been loaded at least once
if ( ! fs_info_cache_initialized )
{
fs_info_cache_initialized = true ;
set_commands_found() ;
if ( do_refresh == false )
load_fs_info_cache() ;
}
if ( do_refresh )
load_fs_info_cache() ;
}
FS_Info::~FS_Info()
void FS_Info::load_cache()
{
set_commands_found();
load_fs_info_cache();
fs_info_cache_initialized = true;
}
// Retrieve the file system type for the path
Glib::ustring FS_Info::get_fs_type( const Glib::ustring & path )
{
initialize_if_required();
const FS_Entry & fs_entry = get_cache_entry_by_path( path );
Glib::ustring fs_type = fs_entry.type;
Glib::ustring fs_sec_type = fs_entry.sec_type;
@ -109,6 +87,7 @@ Glib::ustring FS_Info::get_fs_type( const Glib::ustring & path )
// Retrieve the label and set found indicator for the path
Glib::ustring FS_Info::get_label( const Glib::ustring & path, bool & found )
{
initialize_if_required();
const FS_Entry & fs_entry = get_cache_entry_by_path( path );
found = fs_entry.have_label;
return fs_entry.label;
@ -117,6 +96,7 @@ Glib::ustring FS_Info::get_label( const Glib::ustring & path, bool & found )
// Retrieve the uuid given for the path
Glib::ustring FS_Info::get_uuid( const Glib::ustring & path )
{
initialize_if_required();
const FS_Entry & fs_entry = get_cache_entry_by_path( path );
return fs_entry.uuid;
}
@ -124,6 +104,7 @@ Glib::ustring FS_Info::get_uuid( const Glib::ustring & path )
// Retrieve the path given the uuid
Glib::ustring FS_Info::get_path_by_uuid( const Glib::ustring & uuid )
{
initialize_if_required();
for ( unsigned int i = 0 ; i < fs_info_cache.size() ; i ++ )
if ( uuid == fs_info_cache[i].uuid )
return fs_info_cache[i].path.m_name;
@ -134,6 +115,7 @@ Glib::ustring FS_Info::get_path_by_uuid( const Glib::ustring & uuid )
// Retrieve the path given the label
Glib::ustring FS_Info::get_path_by_label( const Glib::ustring & label )
{
initialize_if_required();
for ( unsigned int i = 0 ; i < fs_info_cache.size() ; i ++ )
if ( label == fs_info_cache[i].label )
return fs_info_cache[i].path.m_name;
@ -143,6 +125,16 @@ Glib::ustring FS_Info::get_path_by_label( const Glib::ustring & label )
// Private methods
void FS_Info::initialize_if_required()
{
if ( ! fs_info_cache_initialized )
{
set_commands_found();
load_fs_info_cache();
fs_info_cache_initialized = true;
}
}
void FS_Info::set_commands_found()
{
//Set status of commands found

View file

@ -171,7 +171,7 @@ void GParted_Core::set_devices_thread( std::vector<Device> * pdevices )
// objects are created in the following caches.
Proc_Partitions_Info::load_cache(); // SHOULD BE SECOND. Caches /proc/partitions and
// pre-populates BlockSpecial cache.
FS_Info fs_info( true ) ; //Refresh cache of file system information
FS_Info::load_cache();
DMRaid dmraid( true ) ; //Refresh cache of dmraid device information
LVM2_PV_Info::clear_cache(); // Cache automatically loaded if and when needed
btrfs::clear_cache(); // Cache incrementally loaded if and when needed
@ -1067,8 +1067,6 @@ void GParted_Core::init_maps()
void GParted_Core::read_mountpoints_from_file( const Glib::ustring & filename, MountMapping & map )
{
FS_Info fs_info ; //Use cache of file system information
FILE* fp = setmntent( filename .c_str(), "r" ) ;
if ( fp == NULL )
@ -1083,11 +1081,11 @@ void GParted_Core::read_mountpoints_from_file( const Glib::ustring & filename, M
Glib::ustring uuid = Utils::regexp_label( node, "^UUID=(.*)" ) ;
if ( ! uuid .empty() )
node = fs_info .get_path_by_uuid( uuid ) ;
node = FS_Info::get_path_by_uuid( uuid );
Glib::ustring label = Utils::regexp_label( node, "^LABEL=(.*)" ) ;
if ( ! label .empty() )
node = fs_info .get_path_by_label( label ) ;
node = FS_Info::get_path_by_label( label );
if ( ! node .empty() )
add_node_and_mountpoint( map, node, mountpoint ) ;
@ -1483,7 +1481,6 @@ void GParted_Core::set_luks_partition( PartitionLUKS & partition )
void GParted_Core::set_partition_label_and_uuid( Partition & partition )
{
FS_Info fs_info; // Use cache of file system information
Glib::ustring partition_path = partition.get_path();
// For SWRaid members only get the label and UUID from SWRaid_Info. Never use
@ -1505,14 +1502,14 @@ void GParted_Core::set_partition_label_and_uuid( Partition & partition )
if ( ! partition.filesystem_label_known() )
{
bool label_found = false;
Glib::ustring label = fs_info.get_label( partition_path, label_found );
Glib::ustring label = FS_Info::get_label( partition_path, label_found );
if ( label_found )
partition.set_filesystem_label( label );
}
// Retrieve file system UUID. Use cached method first in an effort to speed up
// device scanning.
partition.uuid = fs_info.get_uuid( partition_path );
partition.uuid = FS_Info::get_uuid( partition_path );
if ( partition.uuid.empty() )
{
read_uuid( partition );
@ -1613,7 +1610,6 @@ FILESYSTEM GParted_Core::detect_filesystem_internal( PedDevice * lp_device, PedP
FILESYSTEM GParted_Core::detect_filesystem( PedDevice * lp_device, PedPartition * lp_partition,
std::vector<Glib::ustring> & messages )
{
FS_Info fs_info ;
Glib::ustring fsname = "";
Glib::ustring path;
@ -1633,7 +1629,7 @@ FILESYSTEM GParted_Core::detect_filesystem( PedDevice * lp_device, PedPartition
// (Q2) FS_Info (blkid) file system detection
// Blkid detects more signatures and generally has less limitations so use before
// libparted detection, but it doesn't report anything for extended partitions.
fsname = fs_info.get_fs_type( path );
fsname = FS_Info::get_fs_type( path );
// (Q3) Libparted file system detection
// Only used when blkid didn't report anything and only on partitions, not whole