[gparted] Create new method GParted_Core::useable_device() (#755495)



commit 037020b1162a2d61576fb80fdea54522533c93f1
Author: Mike Fleetwood <mike fleetwood googlemail com>
Date:   Tue Oct 6 13:41:38 2015 +0100

    Create new method GParted_Core::useable_device() (#755495)
    
    Abstract code checking sector size and ensuring the first sector of a
    candidate disk device can be read into new
    GParted_Core::useable_device() method.
    
    Bug 755495 - GParted allowing partitioning of large sector devices
                 specified on the command line, when built with old
                 libparted which doesn't support it

 include/GParted_Core.h |    1 +
 src/GParted_Core.cc    |   74 ++++++++++++++++++++++++++++--------------------
 2 files changed, 44 insertions(+), 31 deletions(-)
---
diff --git a/include/GParted_Core.h b/include/GParted_Core.h
index 936ebf3..e378992 100644
--- a/include/GParted_Core.h
+++ b/include/GParted_Core.h
@@ -203,6 +203,7 @@ private:
        static bool commit( PedDisk* lp_disk );
        static bool commit_to_os( PedDisk* lp_disk, std::time_t timeout );
        static void settle_device( std::time_t timeout );
+       static bool useable_device( PedDevice * lp_device );
 
        static PedExceptionOption ped_exception_handler( PedException * e ) ;
 
diff --git a/src/GParted_Core.cc b/src/GParted_Core.cc
index 14b2563..47db4ff 100644
--- a/src/GParted_Core.cc
+++ b/src/GParted_Core.cc
@@ -213,38 +213,13 @@ void GParted_Core::set_devices_thread( std::vector<Device> * pdevices )
                PedDevice* lp_device = ped_device_get_next( NULL ) ;
                while ( lp_device ) 
                {
+                       /* TO TRANSLATORS: looks like   Confirming /dev/sda */
+                       set_thread_status_message( String::ucompose( _("Confirming %1"), lp_device->path ) );
+
                        //only add this device if we can read the first sector (which means it's a real 
device)
-                       char * buf = static_cast<char *>( malloc( lp_device ->sector_size ) ) ;
-                       if ( buf )
-                       {
-                               /*TO TRANSLATORS: looks like Confirming /dev/sda */ 
-                               set_thread_status_message( String::ucompose ( _("Confirming %1"), lp_device 
->path ) ) ;
-                               if ( ped_device_open( lp_device ) )
-                               {
-#ifdef USE_LIBPARTED_LARGE_SECTOR_SUPPORT
-                                       //Devices with sector sizes of 512 bytes and higher are supported
-                                       if ( ped_device_read( lp_device, buf, 0, 1 ) )
-                                               device_paths .push_back( lp_device ->path ) ;
-#else
-                                       //Only devices with sector sizes of 512 bytes are well supported
-                                       if ( lp_device ->sector_size != 512 )
-                                       {
-                                               /*TO TRANSLATORS: looks like  Ignoring device /dev/sde with 
logical sector size of 2048 bytes. */
-                                               Glib::ustring msg = String::ucompose ( _("Ignoring device %1 
with logical sector size of %2 bytes."), lp_device ->path, lp_device ->sector_size ) ;
-                                               msg += "\n" ;
-                                               msg += _("GParted requires libparted version 2.2 or higher to 
support devices with sector sizes larger than 512 bytes.") ;
-                                               std::cout << msg << std::endl << std::endl ;
-                                       }
-                                       else
-                                       {
-                                               if ( ped_device_read( lp_device, buf, 0, 1 ) )
-                                                       device_paths .push_back( lp_device ->path ) ;
-                                       }
-#endif
-                                       ped_device_close( lp_device ) ;
-                               }
-                               free( buf ) ;
-                       }
+                       if ( useable_device( lp_device ) )
+                               device_paths.push_back( lp_device->path );
+
                        lp_device = ped_device_get_next( lp_device ) ;
                }
 
@@ -3872,6 +3847,43 @@ void GParted_Core::fini_filesystems()
        }
 }
 
+bool GParted_Core::useable_device( PedDevice * lp_device )
+{
+       g_assert( lp_device != NULL );  // Bug: Not initialised by call to ped_device_get() or 
ped_device_get_next()
+
+#ifndef USE_LIBPARTED_LARGE_SECTOR_SUPPORT
+       if ( lp_device->sector_size != 512 )
+       {
+               /* TO TRANSLATORS: looks like   Ignoring device /dev/sde with logical sector size of 2048 
bytes. */
+               Glib::ustring msg = String::ucompose ( _("Ignoring device %1 with logical sector size of %2 
bytes."),
+                                                      lp_device ->path, lp_device ->sector_size );
+               msg += "\n";
+               msg += _("GParted requires libparted version 2.2 or higher to support devices with sector 
sizes larger than 512 bytes.");
+               std::cout << msg << std::endl << std::endl;
+               return false;
+       }
+#endif
+
+       char * buf = static_cast<char *>( malloc( lp_device->sector_size ) );
+       if ( ! buf )
+               return false;
+
+       // Must be able to read from the first sector before the disk device is considered
+       // useable in GParted.
+       bool success = false;
+       if ( ped_device_open( lp_device )            &&
+            ped_device_read( lp_device, buf, 0, 1 )    )
+       {
+               success = true;
+
+               ped_device_close( lp_device );
+       }
+
+       free( buf );
+
+       return success;
+}
+
 //Flush the Linux kernel caches, and therefore ensure coherency between the caches of the
 //  whole disk device and the partition devices.
 //


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