[gparted] Update FS_Info cache with Unicode safe labels read from blkid (#786502)



commit b022c1e3a949640214d8b6e4a1aa395a53858bb4
Author: Mike Fleetwood <mike fleetwood googlemail com>
Date:   Sun Aug 27 14:31:38 2017 +0100

    Update FS_Info cache with Unicode safe labels read from blkid (#786502)
    
    Move the code which reads the Unicode label from FS_Info::get_label()
    into new function run_blkid_update_cache_one_label() which also replaces
    the non-reversibly encoded copy loaded during the initial cache load.
    
    This is mainly a bit of code refactoring ready for the following change.
    It deliberately keeps the initial loaded labels so that reading
    /etc/fstab and decoding LABEL=<label> to block special device names via
    FS_Info::get_path_by_label() continues to works, at least for ASCII only
    labels.
    
    Bug 786502 - Support reading Unicode labels when file system specific
                 tools aren't available

 include/FS_Info.h |    1 +
 src/FS_Info.cc    |   66 +++++++++++++++++++++++++++++++++++++---------------
 2 files changed, 48 insertions(+), 19 deletions(-)
---
diff --git a/include/FS_Info.h b/include/FS_Info.h
index 0e10c65..472b74a 100644
--- a/include/FS_Info.h
+++ b/include/FS_Info.h
@@ -52,6 +52,7 @@ private:
        static void load_fs_info_cache();
        static void load_fs_info_cache_extra_for_path( const Glib::ustring & path );
        static bool run_blkid_load_cache( const Glib::ustring & path = "" );
+       static bool run_blkid_update_cache_one_label( FS_Entry & fs_entry );
 
        static bool fs_info_cache_initialized ;
        static bool blkid_found ;
diff --git a/src/FS_Info.cc b/src/FS_Info.cc
index bd9e422..ec4d783 100644
--- a/src/FS_Info.cc
+++ b/src/FS_Info.cc
@@ -89,26 +89,26 @@ Glib::ustring FS_Info::get_fs_type( const Glib::ustring & path )
 Glib::ustring FS_Info::get_label( const Glib::ustring & path, bool & found )
 {
        initialize_if_required();
-       if ( ! blkid_found )
-       {
-               found = false;
-               return "";
-       }
+       BlockSpecial bs = BlockSpecial( path );
+       for ( unsigned int i = 0 ; i < fs_info_cache.size() ; i ++ )
+               if ( bs == fs_info_cache[i].path )
+               {
+                       if ( fs_info_cache[i].type == "" )
+                       {
+                               // This is a blank cache entry for a whole disk device
+                               // containing a partition table, so no label (as created
+                               // by load_fs_info_cache_extra_for_path()).
+                               found = false;
+                               return "";
+                       }
 
-       // (#786502) Run a separate blkid execution for a single partition to get the
-       // label without blkid's default non-reversible encoding.
-       Glib::ustring output;
-       Glib::ustring error;
-       found = ! Utils::execute_command( "blkid -o value -s LABEL " + path, output, error, true );
-       size_t len = output.length();
-       if ( len > 0 && output[len-1] == '\n' )
-       {
-               // Output is either the label with a terminating new line or zero bytes
-               // when the file system has no label.  Strip optional trailing new line
-               // from blkid output.
-               output.resize( len-1 );
-       }
-       return output;
+                       // Run blkid to get the label for this one partition, update the
+                       // cache and return the found label.
+                       found = run_blkid_update_cache_one_label( fs_info_cache[i] );
+                       return fs_info_cache[i].label;
+               }
+       found = false;
+       return "";
 }
 
 // Retrieve the uuid given for the path
@@ -269,4 +269,32 @@ bool FS_Info::run_blkid_load_cache( const Glib::ustring & path )
        return loaded_entries;
 }
 
+bool FS_Info::run_blkid_update_cache_one_label( FS_Entry & fs_entry )
+{
+       if ( ! blkid_found )
+               return false;
+
+       // (#786502) Run a separate blkid execution for a single partition to get the
+       // label without blkid's default non-reversible encoding.
+       Glib::ustring output;
+       Glib::ustring error;
+       bool success = ! Utils::execute_command( "blkid -o value -s LABEL " + fs_entry.path.m_name,
+                                                output, error, true );
+       if ( ! success )
+               return false;
+
+       size_t len = output.length();
+       if ( len > 0 && output[len-1] == '\n' )
+       {
+               // Output is either the label with a terminating new line or zero bytes
+               // when the file system has no label.  Strip optional trailing new line
+               // from blkid output.
+               output.resize( len-1 );
+       }
+       // Update cache entry with the read label.
+       fs_entry.have_label = true;
+       fs_entry.label = output;
+       return true;
+}
+
 }//GParted


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