Lazy initialize the cache from querying LVM2 PVs (#160787)

Previously when GParted was started LVM2_PV_Info cache was loaded twice,
executing LVM2 PV querying commands twice.  Firstly when
lvm2_pv::get_filesystem_support() was checking if LVM2 PV support was
available, and secondly when forced by a refresh in
GParted_Core::set_devices().

Implement lazy initialization.  Only load the cache when forced by the
above mentioned refresh or having to return a value when the cache is
not yet loaded.  Do not initialize the cache when just checking if LVM2
PV support is available.

Bug #160787 - lvm support
This commit is contained in:
Mike Fleetwood 2011-12-11 23:17:52 +00:00 committed by Curtis Gedak
parent 608d992e82
commit ff8ad04120
2 changed files with 20 additions and 16 deletions

View file

@ -42,6 +42,7 @@ public:
Byte_Value get_free_bytes( const Glib::ustring & path ) ;
bool has_active_lvs( const Glib::ustring & path ) ;
private:
void initialize_if_required() ;
void set_commands_found() ;
void load_lvm2_pv_info_cache() ;
Glib::ustring get_pv_attr( const Glib::ustring & path, unsigned int entry ) ;

View file

@ -51,28 +51,16 @@ Glib::ustring LVM2_PV_Info::dmsetup_info_cache = "" ;
LVM2_PV_Info::LVM2_PV_Info()
{
//Ensure that cache has been loaded at least once
if ( ! lvm2_pv_info_cache_initialized )
{
lvm2_pv_info_cache_initialized = true ;
set_commands_found() ;
load_lvm2_pv_info_cache() ;
}
}
LVM2_PV_Info::LVM2_PV_Info( bool do_refresh )
{
//Ensure that cache has been loaded at least once
if ( ! lvm2_pv_info_cache_initialized )
{
lvm2_pv_info_cache_initialized = true ;
set_commands_found() ;
if ( do_refresh == false )
load_lvm2_pv_info_cache() ;
}
if ( do_refresh )
{
set_commands_found() ;
load_lvm2_pv_info_cache() ;
lvm2_pv_info_cache_initialized = true ;
}
}
LVM2_PV_Info::~LVM2_PV_Info()
@ -81,17 +69,21 @@ LVM2_PV_Info::~LVM2_PV_Info()
bool LVM2_PV_Info::is_lvm2_pv_supported()
{
if ( ! lvm2_pv_info_cache_initialized )
set_commands_found() ;
return ( lvm_found && dmsetup_found ) ;
}
Glib::ustring LVM2_PV_Info::get_vg_name( const Glib::ustring & path )
{
initialize_if_required() ;
return get_pv_attr( path, PVATTR_VG_NAME ) ;
}
//Return number of free bytes in the PV, or -1 for error.
Byte_Value LVM2_PV_Info::get_free_bytes( const Glib::ustring & path )
{
initialize_if_required() ;
Byte_Value free_bytes = -1 ;
Glib::ustring fb_str = get_pv_attr( path, PVATTR_PV_FREE ) ;
if ( fb_str != "" )
@ -108,6 +100,7 @@ Byte_Value LVM2_PV_Info::get_free_bytes( const Glib::ustring & path )
//Report if any LVs are active in the VG stored in the PV.
bool LVM2_PV_Info::has_active_lvs( const Glib::ustring & path )
{
initialize_if_required() ;
Glib::ustring vgname = get_pv_attr( path, PVATTR_VG_NAME ) ;
if ( vgname == "" )
//PV not yet included in any VG
@ -120,6 +113,16 @@ bool LVM2_PV_Info::has_active_lvs( const Glib::ustring & path )
//Private methods
void LVM2_PV_Info::initialize_if_required()
{
if ( ! lvm2_pv_info_cache_initialized )
{
set_commands_found() ;
load_lvm2_pv_info_cache() ;
lvm2_pv_info_cache_initialized = true ;
}
}
void LVM2_PV_Info::set_commands_found()
{
//Set status of commands found