[gparted] Create helper functions to check for minimum kernel version



commit 5967966ff749e94393054150e386120df879aa7f
Author: Mike Fleetwood <mike fleetwood googlemail com>
Date:   Sun Feb 5 09:06:44 2012 +0000

    Create helper functions to check for minimum kernel version
    
    Create function Utils::kernel_version_at_least() to check that the
    current Linux kernel is a particular version or higher.
    
    Update nilfs2 to use this function to determine whether the kernel is
    new enough to support file system resizing.

 include/Utils.h |    4 ++++
 src/Utils.cc    |   45 +++++++++++++++++++++++++++++++++++++++++++++
 src/nilfs2.cc   |   39 ++++++++++++---------------------------
 3 files changed, 61 insertions(+), 27 deletions(-)
---
diff --git a/include/Utils.h b/include/Utils.h
index 2d53f4f..fee138b 100644
--- a/include/Utils.h
+++ b/include/Utils.h
@@ -156,6 +156,7 @@ public:
 	static Glib::ustring get_filesystem_string( FILESYSTEM filesystem ) ;
 	static Glib::ustring get_filesystem_software( FILESYSTEM filesystem ) ;
 	static bool kernel_supports_fs( const Glib::ustring & fs ) ;
+	static bool kernel_version_at_least( int major_ver, int minor_ver, int patch_ver ) ;
 	static Glib::ustring format_size( Sector sectors, Byte_Value sector_size ) ;
 	static Glib::ustring format_time( std::time_t seconds ) ;
 	static double sector_to_unit( Sector sectors, Byte_Value sector_size, SIZE_UNIT size_unit ) ;
@@ -182,6 +183,9 @@ public:
 	                   const Glib::ustring& delimiters     ) ;
 	static int convert_to_int(const Glib::ustring & src);
 	static Glib::ustring generate_uuid(void);
+
+private:
+	static bool get_kernel_version( int & major_ver, int & minor_ver, int & patch_ver ) ;
 };
 
 
diff --git a/src/Utils.cc b/src/Utils.cc
index 6413925..91196cd 100644
--- a/src/Utils.cc
+++ b/src/Utils.cc
@@ -236,6 +236,18 @@ bool Utils::kernel_supports_fs( const Glib::ustring & fs )
 	return fs_supported ;
 }
 
+//Report if kernel version is >= (major, minor, patch)
+bool Utils::kernel_version_at_least( int major_ver, int minor_ver, int patch_ver )
+{
+	int actual_major_ver, actual_minor_ver, actual_patch_ver ;
+	if ( ! get_kernel_version( actual_major_ver, actual_minor_ver, actual_patch_ver ) )
+		return false ;
+	bool result =    ( actual_major_ver > major_ver )
+	              || ( actual_major_ver == major_ver && actual_minor_ver > minor_ver )
+	              || ( actual_major_ver == major_ver && actual_minor_ver == minor_ver && actual_patch_ver >= patch_ver ) ;
+	return result ;
+}
+
 Glib::ustring Utils::format_size( Sector sectors, Byte_Value sector_size )
 {
 	std::stringstream ss ;
@@ -594,5 +606,38 @@ Glib::ustring Utils::generate_uuid(void)
 	return uuid_str;
 }
 
+//private functions ...
+
+//Read kernel version, reporting success or failure
+bool Utils::get_kernel_version( int & major_ver, int & minor_ver, int & patch_ver )
+{
+	static bool read_file = false ;
+	static int read_major_ver = -1 ;
+	static int read_minor_ver = -1 ;
+	static int read_patch_ver = -1 ;
+
+	bool success = false ;
+	if ( ! read_file )
+	{
+		std::ifstream input( "/proc/version" ) ;
+		std::string line ;
+		if ( input )
+		{
+			getline( input, line ) ;
+			sscanf( line .c_str(), "Linux version %d.%d.%d",
+			        &read_major_ver, &read_minor_ver, &read_patch_ver ) ;
+			input .close() ;
+		}
+		read_file = true ;
+	}
+	if ( read_major_ver > -1 && read_minor_ver > -1 && read_patch_ver > -1 )
+	{
+		major_ver = read_major_ver ;
+		minor_ver = read_minor_ver ;
+		patch_ver = read_patch_ver ;
+		success = true ;
+	}
+	return success ;
+}
 
 } //GParted..
diff --git a/src/nilfs2.cc b/src/nilfs2.cc
index 2ac7085..72b8005 100644
--- a/src/nilfs2.cc
+++ b/src/nilfs2.cc
@@ -40,33 +40,18 @@ FS nilfs2::get_filesystem_support()
 		fs .write_uuid = GParted::FS::EXTERNAL ;
 	}
 
-        //Nilfs2 resizing is an online only operation and needs:
-        //  mount, umount, nilfs-resize and linux >= 3.0 with nilfs2 support.
-        if ( ! Glib::find_program_in_path( "mount" ) .empty()        &&
-             ! Glib::find_program_in_path( "umount" ) .empty()       &&
-             ! Glib::find_program_in_path( "nilfs-resize" ) .empty() &&
-             Utils::kernel_supports_fs( "nilfs2" )                      )
-        {
-                std::ifstream input( "/proc/version" ) ;
-                std::string line ;
-                int linux_major_ver = -1 ;
-                int linux_minor_ver = -1 ;
-                int linux_patch_ver = -1 ;
-                if ( input )
-                {
-                        getline( input, line ) ;
-                        sscanf( line .c_str() , "Linux version %d.%d.%d",
-			        &linux_major_ver, &linux_minor_ver, &linux_patch_ver ) ;
-                        input .close() ;
-                }
-
-                if ( linux_major_ver >= 3 )
-                {
-                        fs .grow = GParted::FS::EXTERNAL ;
-                        if ( fs .read ) //needed to determine a minimum file system size.
-                                fs .shrink = GParted::FS::EXTERNAL ;
-                }
-        }
+	//Nilfs2 resizing is an online only operation and needs:
+	//  mount, umount, nilfs-resize and linux >= 3.0 with nilfs2 support.
+	if ( ! Glib::find_program_in_path( "mount" ) .empty()        &&
+	     ! Glib::find_program_in_path( "umount" ) .empty()       &&
+	     ! Glib::find_program_in_path( "nilfs-resize" ) .empty() &&
+	     Utils::kernel_supports_fs( "nilfs2" )                   &&
+	     Utils::kernel_version_at_least( 3, 0, 0 )                  )
+	{
+		fs .grow = GParted::FS::EXTERNAL ;
+		if ( fs .read ) //needed to determine a minimum file system size.
+			fs .shrink = GParted::FS::EXTERNAL ;
+	}
 
 	fs .copy = GParted::FS::GPARTED ;
 	fs .move = GParted::FS::GPARTED ;



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