[gparted] Support reading Unicode character labels via blkid (#786502)



commit 1c984ae06d9fd1f4d28ae960c1cdb0a3442ccf3f
Author: Mike Fleetwood <mike fleetwood googlemail com>
Date:   Fri Aug 25 17:38:08 2017 +0100

    Support reading Unicode character labels via blkid (#786502)
    
    For file systems which don't provide a specific tool to read labels
    (exfat, f2fs, hfs, hfsplus, udf and ufs) or when the tools aren't
    installed for any other file system, reading labels falls back to using
    the blkid command.  Blkid encodes non-ASCII bytes in it's output [1].
    For example blkid reports a short Unicode label like this:
    
        # blkid /dev/sdb12
        /dev/sdb12: LABEL="M-PM-^ZM-PM->M-QM-^HM-PM-:M-PM-0" TYPE="hfsplus"
    
    This shows encoding using 'M-' for bytes above 127 and caret '^' for
    control characters.  Unfortunately neither 'M-' or '^' are encoded by
    blkid so it is impossible to distinguish between the original label
    containing either of these sequences or encoded non-printable bytes.
    See this Bugzilla's bug 786502 entry for more details.  Unfortunately
    this makes the default output format from blkid unsuitable for reading
    Unicode character labels.
    
    Instead instruct blkid to print values without encoding them using the
    '-o value' option.  This just produces a list of new line separated
    values without being able to identify which TYPE, UUID or LABEL belongs
    to which device.  So query just the label for each block device one at
    a time.  This method has the advantage of also working with all versions
    of blkid, from version 1.0.0 (12-Feb-2003) in CentOS 5 to the latest
    version 2.30.1 (20-Jul-2017) in Fedora 26.
    
        # blkid -o value -s LABEL /dev/sdb12 | hexdump -C
        00000000  d0 9a d0 be d1 88 d0 ba  d0 b0 0a           |...........|
        0000000b
    
    (Using hexdump -C just so that this commit message only contains ASCII
    characters).
    
    Therefore this commit changes FS_Info::get_label() to query blkid as
    shown above.  Note that GParted_Core::set_partition_label_and_uuid()
    only calls get_label() when there is no file system specific tool
    available (or the tool failed).
    
    The FS_Info cache still contains copies of the labels subject to blkid
    encoding, and that will be addressed in following commits.
    
    [1] blkid.c v2.30.1 safe_print()
        https://git.kernel.org/pub/scm/utils/util-linux/util-linux.git/tree/misc-utils/blkid.c?h=v2.30.1#n111
    
    Bug 786502 - Support reading Unicode labels when file system specific
                 tools aren't available

 src/FS_Info.cc |   23 ++++++++++++++++++++---
 1 files changed, 20 insertions(+), 3 deletions(-)
---
diff --git a/src/FS_Info.cc b/src/FS_Info.cc
index fc3fa62..bd9e422 100644
--- a/src/FS_Info.cc
+++ b/src/FS_Info.cc
@@ -89,9 +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();
-       const FS_Entry & fs_entry = get_cache_entry_by_path( path );
-       found = fs_entry.have_label;
-       return fs_entry.label;
+       if ( ! blkid_found )
+       {
+               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;
 }
 
 // Retrieve the uuid given for the path


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