[gparted] Ensure SWRaid_Info cache is loaded at least once (#756829)



commit bab1109d3df55b733cd1245f8661a60f21fa98c3
Author: Mike Fleetwood <mike fleetwood googlemail com>
Date:   Fri Oct 23 07:34:13 2015 +0100

    Ensure SWRaid_Info cache is loaded at least once (#756829)
    
    Automatically load the cache of SWRaid information for the first time if
    any of the querying methods are called before the first explicit
    load_cache() call.  Means we can't accidentally use the class and
    incorrectly find no SWRaid members when they do exist.
    
    Bug 756829 - SWRaid member detection enhancements

 include/SWRaid_Info.h |    2 ++
 src/SWRaid_Info.cc    |   18 ++++++++++++++++++
 2 files changed, 20 insertions(+), 0 deletions(-)
---
diff --git a/include/SWRaid_Info.h b/include/SWRaid_Info.h
index a4d5fd6..3af68b6 100644
--- a/include/SWRaid_Info.h
+++ b/include/SWRaid_Info.h
@@ -50,11 +50,13 @@ public:
        static Glib::ustring get_label( const Glib::ustring & member_path );
 
 private:
+       static void initialise_if_required();
        static void set_command_found();
        static void load_swraid_info_cache();
        static SWRaid_Member & get_cache_entry_by_member( const Glib::ustring & member_path );
        static Glib::ustring mdadm_to_canonical_uuid( const Glib::ustring & mdadm_uuid );
 
+       static bool cache_initialised;
        static bool mdadm_found;
        static std::vector<SWRaid_Member> swraid_info_cache;
 };
diff --git a/src/SWRaid_Info.cc b/src/SWRaid_Info.cc
index 66ff23c..21952ea 100644
--- a/src/SWRaid_Info.cc
+++ b/src/SWRaid_Info.cc
@@ -25,6 +25,7 @@ namespace GParted
 {
 
 // Data model:
+// cache_initialised - Has the cache been loaded?
 // mdadm_found       - Is the "mdadm" command available?
 // swraid_info_cache - Vector of member information in Linux Software RAID arrays.
 //                     Only active arrays have /dev entries.
@@ -37,6 +38,7 @@ namespace GParted
 //                     ]
 
 // Initialise static data elements
+bool SWRaid_Info::cache_initialised = false;
 bool SWRaid_Info::mdadm_found = false;
 std::vector<SWRaid_Member> SWRaid_Info::swraid_info_cache;
 
@@ -44,10 +46,12 @@ void SWRaid_Info::load_cache()
 {
        set_command_found();
        load_swraid_info_cache();
+       cache_initialised = true;
 }
 
 bool SWRaid_Info::is_member( const Glib::ustring & member_path )
 {
+       initialise_if_required();
        const SWRaid_Member & memb = get_cache_entry_by_member( member_path );
        if ( memb.member == member_path )
                return true;
@@ -57,6 +61,7 @@ bool SWRaid_Info::is_member( const Glib::ustring & member_path )
 
 bool SWRaid_Info::is_member_active( const Glib::ustring & member_path )
 {
+       initialise_if_required();
        const SWRaid_Member & memb = get_cache_entry_by_member( member_path );
        if ( memb.member == member_path )
                return memb.active;
@@ -68,6 +73,7 @@ bool SWRaid_Info::is_member_active( const Glib::ustring & member_path )
 // array is not running or there is no such member.
 Glib::ustring SWRaid_Info::get_array( const Glib::ustring & member_path )
 {
+       initialise_if_required();
        const SWRaid_Member & memb = get_cache_entry_by_member( member_path );
        return memb.array;
 }
@@ -76,6 +82,7 @@ Glib::ustring SWRaid_Info::get_array( const Glib::ustring & member_path )
 // there is no such member.
 Glib::ustring SWRaid_Info::get_uuid( const Glib::ustring & member_path )
 {
+       initialise_if_required();
        const SWRaid_Member & memb = get_cache_entry_by_member( member_path );
        return memb.uuid;
 }
@@ -86,12 +93,23 @@ Glib::ustring SWRaid_Info::get_uuid( const Glib::ustring & member_path )
 // default of hostname ":" array number when not otherwise specified).
 Glib::ustring SWRaid_Info::get_label( const Glib::ustring & member_path )
 {
+       initialise_if_required();
        const SWRaid_Member & memb = get_cache_entry_by_member( member_path );
        return memb.label;
 }
 
 // Private methods
 
+void SWRaid_Info::initialise_if_required()
+{
+       if ( ! cache_initialised )
+       {
+               set_command_found();
+               load_swraid_info_cache();
+               cache_initialised = true;
+       }
+}
+
 void SWRaid_Info::set_command_found()
 {
        mdadm_found = ! Glib::find_program_in_path( "mdadm" ).empty();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]