Ensure FS_Info (blkid) cache is populated before first use (#131)

Now we always want to run blkid naming all paths, ensure the FS_Info
cache is explicitly loaded first.  Report an error if not done so and
remove the cache loading code from running blkid without naming all
paths.  Fewer code paths to consider and reason about.

Closes #131 - GParted hangs when non-named device is hung
This commit is contained in:
Mike Fleetwood 2021-01-31 12:51:13 +00:00 committed by Curtis Gedak
parent d86d9ae830
commit 52ed42de28
2 changed files with 25 additions and 14 deletions

View File

@ -46,7 +46,7 @@ public:
static Glib::ustring get_path_by_label( const Glib::ustring & label ); static Glib::ustring get_path_by_label( const Glib::ustring & label );
private: private:
static void initialize_if_required(); static bool not_initialised_then_error();
static void set_commands_found(); static void set_commands_found();
static const FS_Entry & get_cache_entry_by_path( const Glib::ustring & path ); static const FS_Entry & get_cache_entry_by_path( const Glib::ustring & path );
static void load_fs_info_cache(const std::vector<Glib::ustring>& paths); static void load_fs_info_cache(const std::vector<Glib::ustring>& paths);

View File

@ -19,6 +19,7 @@
#include "BlockSpecial.h" #include "BlockSpecial.h"
#include "Utils.h" #include "Utils.h"
#include <iostream>
#include <glibmm/ustring.h> #include <glibmm/ustring.h>
#include <glibmm/miscutils.h> #include <glibmm/miscutils.h>
#include <glibmm/shell.h> #include <glibmm/shell.h>
@ -61,7 +62,9 @@ void FS_Info::load_cache_for_paths(const std::vector<Glib::ustring>& paths)
// Retrieve the file system type for the path // Retrieve the file system type for the path
Glib::ustring FS_Info::get_fs_type( const Glib::ustring & path ) Glib::ustring FS_Info::get_fs_type( const Glib::ustring & path )
{ {
initialize_if_required(); if (not_initialised_then_error())
return "";
const FS_Entry & fs_entry = get_cache_entry_by_path( path ); const FS_Entry & fs_entry = get_cache_entry_by_path( path );
Glib::ustring fs_type = fs_entry.type; Glib::ustring fs_type = fs_entry.type;
Glib::ustring fs_sec_type = fs_entry.sec_type; Glib::ustring fs_sec_type = fs_entry.sec_type;
@ -93,7 +96,12 @@ Glib::ustring FS_Info::get_fs_type( const Glib::ustring & path )
// Retrieve the label and set found indicator for the path // Retrieve the label and set found indicator for the path
Glib::ustring FS_Info::get_label( const Glib::ustring & path, bool & found ) Glib::ustring FS_Info::get_label( const Glib::ustring & path, bool & found )
{ {
initialize_if_required(); if (not_initialised_then_error())
{
found = false;
return "";
}
BlockSpecial bs = BlockSpecial( path ); BlockSpecial bs = BlockSpecial( path );
for ( unsigned int i = 0 ; i < fs_info_cache.size() ; i ++ ) for ( unsigned int i = 0 ; i < fs_info_cache.size() ; i ++ )
if ( bs == fs_info_cache[i].path ) if ( bs == fs_info_cache[i].path )
@ -119,7 +127,9 @@ Glib::ustring FS_Info::get_label( const Glib::ustring & path, bool & found )
// Retrieve the uuid given for the path // Retrieve the uuid given for the path
Glib::ustring FS_Info::get_uuid( const Glib::ustring & path ) Glib::ustring FS_Info::get_uuid( const Glib::ustring & path )
{ {
initialize_if_required(); if (not_initialised_then_error())
return "";
const FS_Entry & fs_entry = get_cache_entry_by_path( path ); const FS_Entry & fs_entry = get_cache_entry_by_path( path );
return fs_entry.uuid; return fs_entry.uuid;
} }
@ -127,7 +137,9 @@ Glib::ustring FS_Info::get_uuid( const Glib::ustring & path )
// Retrieve the path given the uuid // Retrieve the path given the uuid
Glib::ustring FS_Info::get_path_by_uuid( const Glib::ustring & uuid ) Glib::ustring FS_Info::get_path_by_uuid( const Glib::ustring & uuid )
{ {
initialize_if_required(); if (not_initialised_then_error())
return "";
for ( unsigned int i = 0 ; i < fs_info_cache.size() ; i ++ ) for ( unsigned int i = 0 ; i < fs_info_cache.size() ; i ++ )
if ( uuid == fs_info_cache[i].uuid ) if ( uuid == fs_info_cache[i].uuid )
return fs_info_cache[i].path.m_name; return fs_info_cache[i].path.m_name;
@ -138,7 +150,9 @@ Glib::ustring FS_Info::get_path_by_uuid( const Glib::ustring & uuid )
// Retrieve the path given the label // Retrieve the path given the label
Glib::ustring FS_Info::get_path_by_label( const Glib::ustring & label ) Glib::ustring FS_Info::get_path_by_label( const Glib::ustring & label )
{ {
initialize_if_required(); if (not_initialised_then_error())
return "";
update_fs_info_cache_all_labels(); update_fs_info_cache_all_labels();
for ( unsigned int i = 0 ; i < fs_info_cache.size() ; i ++ ) for ( unsigned int i = 0 ; i < fs_info_cache.size() ; i ++ )
if ( label == fs_info_cache[i].label ) if ( label == fs_info_cache[i].label )
@ -149,17 +163,14 @@ Glib::ustring FS_Info::get_path_by_label( const Glib::ustring & label )
// Private methods // Private methods
void FS_Info::initialize_if_required() bool FS_Info::not_initialised_then_error()
{ {
if ( ! fs_info_cache_initialized ) if (! fs_info_cache_initialized)
{ std::cerr << "GParted Bug: FS_Info (blkid) cache not loaded before use" << std::endl;
set_commands_found(); return ! fs_info_cache_initialized;
std::vector<Glib::ustring> empty;
load_fs_info_cache(empty);
fs_info_cache_initialized = true;
}
} }
void FS_Info::set_commands_found() void FS_Info::set_commands_found()
{ {
//Set status of commands found //Set status of commands found