[gparted/psusi/refactor: 7/19] Refactor ext2 to use execute_command_async



commit 929ef0fedbf8b5075e8e3a4c010576424c50dceb
Author: Phillip Susi <psusi ubuntu com>
Date:   Sun Feb 12 21:54:28 2012 -0500

    Refactor ext2 to use execute_command_async
    
    Instead of Filesystem::execute_command or Utils::execute_command, the ext2
    methods now use Filesystem::execute_command_async, and block the calling
    thread on a mutex until the job completes.  Methods that invoked multiple
    external utilities have been split into multiple functions that are
    invoked as the callback when the previous command has completed.

 include/ext2.h |   23 +++++++
 src/ext2.cc    |  195 ++++++++++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 183 insertions(+), 35 deletions(-)
---
diff --git a/include/ext2.h b/include/ext2.h
index 7a1de2a..96f3858 100644
--- a/include/ext2.h
+++ b/include/ext2.h
@@ -30,20 +30,43 @@ class ext2 : public FileSystem
 public:
 	FS get_filesystem_support() ;
 	void set_used_sectors( Partition & partition ) ;
+	void set_used_sectors( Partition & partition, sigc::slot<bool, double> slot );
 	void read_label( Partition & partition ) ;
+	void read_label( Partition & partition, sigc::slot<bool, double> slot );
 	bool write_label( const Partition & partition, OperationDetail & operationdetail ) ;
+	void write_label( const Partition & partition, OperationDetail & operationdetail,
+			  sigc::slot<bool, double> slot );
 	void read_uuid( Partition & partition ) ;
+	void read_uuid( Partition & partition, sigc::slot<bool, double> slot );
 	bool write_uuid( const Partition & partition, OperationDetail & operationdetail ) ;
+	void write_uuid( const Partition & partition, OperationDetail & operationdetail,
+			 sigc::slot<bool, double> slot );
 	bool create( const Partition & new_partition, OperationDetail & operationdetail ) ;
+	void create( const Partition & new_partition, OperationDetail & operationdetail,
+		     sigc::slot<bool, double> slot );
 	bool resize( const Partition & partition_new, OperationDetail & operationdetail, bool fill_partition = false ) ;
+	void resize( const Partition & partition_new, OperationDetail & operationdetail,
+		     bool fill_partition, sigc::slot<bool, double> slot );
 	bool move( const Partition & partition_new
 	         , const Partition & partition_old
 	         , OperationDetail & operationdetail
 	         ) ;
+	void move( const Partition & partition_new, const Partition & partition_old,
+		   OperationDetail & operationdetail, sigc::slot<bool, double> slot );
 	bool copy( const Glib::ustring & src_part_path,
 		   const Glib::ustring & dest_part_path,
 		   OperationDetail & operationdetail ) ;
+	void copy( const Glib::ustring & src_part_path, const Glib::ustring & dest_part_path,
+		   OperationDetail & operationdetail, sigc::slot<bool, double> slot );
 	bool check_repair( const Partition & partition, OperationDetail & operationdetail ) ;
+	void check_repair( const Partition & partition, OperationDetail & operationdetail,
+			   sigc::slot<bool, double> slot );
+
+private:
+	bool set_used_sectors2( double progress, Partition *partition );
+	bool read_label2( double progress, Partition *partition );
+	bool read_uuid2( double progress, Partition *partition );
+	bool check_repair2( double progress );
 };
 
 } //GParted
diff --git a/src/ext2.cc b/src/ext2.cc
index 1a899a4..b807cf1 100644
--- a/src/ext2.cc
+++ b/src/ext2.cc
@@ -62,92 +62,193 @@ FS ext2::get_filesystem_support()
 	return fs ;
 }
 
-void ext2::set_used_sectors( Partition & partition ) 
+void ext2::set_used_sectors( Partition & partition )
 {
-	if ( ! Utils::execute_command( "dumpe2fs -h " + partition .get_path(), output, error, true ) )
+	set_used_sectors( partition, unlock_mutex );
+	mutex.lock();
+}
+
+void ext2::set_used_sectors( Partition & partition, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "dumpe2fs -h " + partition .get_path(),
+			 sigc::bind( sigc::mem_fun( *this, &ext2::set_used_sectors2 ),
+				     &partition ) );
+}
+
+bool ext2::set_used_sectors2( double progress, Partition *partition )
+{
+	if ( progress != 1.0 )
+		return false;
+	if ( !exit_status )
 	{
 		index = output .find( "Free blocks:" ) ;
 		if ( index >= output .length() ||
 		     sscanf( output.substr( index ) .c_str(), "Free blocks: %Ld", &N ) != 1 )   
 			N = -1 ;
-	
+
 		index = output .find( "Block size:" ) ;
 		if ( index >= output.length() || 
 		     sscanf( output.substr( index ) .c_str(), "Block size: %Ld", &S ) != 1 )  
 			S = -1 ;
 
 		if ( N > -1 && S > -1 )
-			partition .Set_Unused( Utils::round( N * ( S / double(partition .sector_size) ) ) ) ;
+			partition->Set_Unused( Utils::round( N * ( S / double(partition->sector_size) ) ) );
 	}
 	else
 	{
-		if ( ! output .empty() )
-			partition .messages .push_back( output ) ;
-		
-		if ( ! error .empty() )
-			partition .messages .push_back( error ) ;
+		if ( !output.empty() )
+			partition->messages.push_back( output );
+
+		if ( !error.empty() )
+			partition->messages.push_back( error );
 	}
+	return slot( 1.0 );
 }
-	
+
 void ext2::read_label( Partition & partition )
 {
-	if ( ! Utils::execute_command( "e2label " + partition .get_path(), output, error, true ) )
+	read_label( partition, unlock_mutex );
+	mutex.lock();
+}
+
+void ext2::read_label( Partition & partition, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "e2label " + partition .get_path(),
+			 sigc::bind( sigc::mem_fun( *this, &ext2::read_label2 ),
+				     &partition ) );
+}
+
+bool ext2::read_label2( double progress, Partition *partition )
+{
+	if ( progress != 1.0 )
+		return false;
+	if ( !exit_status )
 	{
-		partition .label = Utils::trim( output ) ;
+		partition->label = Utils::trim( output );
 	}
 	else
 	{
-		if ( ! output .empty() )
-			partition .messages .push_back( output ) ;
-		
-		if ( ! error .empty() )
-			partition .messages .push_back( error ) ;
+		if ( !output.empty() )
+			partition->messages.push_back( output );
+
+		if ( !error.empty() )
+			partition->messages.push_back( error );
 	}
+	return slot( 1.0 );
 }
 
 bool ext2::write_label( const Partition & partition, OperationDetail & operationdetail )
 {
-	return ! execute_command( "e2label " + partition .get_path() + " \"" + partition .label + "\"", operationdetail ) ;
+	write_label( partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void ext2::write_label( const Partition & partition, OperationDetail & operationdetail,
+			sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "e2label " + partition .get_path() + " \"" + partition .label + "\"",
+			 operationdetail,
+			 set_success );
 }
 
 void ext2::read_uuid( Partition & partition )
 {
-	if ( ! Utils::execute_command( "tune2fs -l " + partition .get_path(), output, error, true ) )
+	read_uuid( partition, unlock_mutex );
+	mutex.lock();
+}
+
+void ext2::read_uuid( Partition & partition, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "tune2fs -l " + partition .get_path(),
+			 sigc::bind( sigc::mem_fun( *this, &ext2::read_uuid2 ),
+				     &partition ) );
+}
+
+bool ext2::read_uuid2( double progress, Partition *partition )
+{
+	if ( progress != 1.0 )
+		return false;
+	if ( !exit_status )
 	{
-		partition .uuid = Utils::regexp_label( output, "^Filesystem UUID:[[:blank:]]*([^[:space:]]*)" ) ;
-		if (partition .uuid == "<none>")
-			partition .uuid .clear() ;
+		partition->uuid = Utils::regexp_label( output, "^Filesystem UUID:[[:blank:]]*([^[:space:]]*)" );
+		if (partition->uuid == "<none>")
+			partition->uuid.clear();
 
 	}
 	else
 	{
-		if ( ! output .empty() )
-			partition .messages .push_back( output ) ;
+		if ( !output.empty() )
+			partition->messages.push_back( output );
 
-		if ( ! error .empty() )
-			partition .messages .push_back( error ) ;
+		if ( !error.empty() )
+			partition->messages.push_back( error );
 	}
+	return slot( 1.0 );
 }
 
 bool ext2::write_uuid( const Partition & partition, OperationDetail & operationdetail )
 {
-	return ! execute_command( "tune2fs -U random " + partition .get_path(), operationdetail ) ;
+	write_uuid( partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void ext2::write_uuid( const Partition & partition, OperationDetail & operationdetail,
+		       sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "tune2fs -U random " + partition .get_path(),
+			 operationdetail,
+			 set_success );
 }
 
 bool ext2::create( const Partition & new_partition, OperationDetail & operationdetail )
 {
-	return ! execute_command( "mkfs.ext2 -L \"" + new_partition .label + "\" " + new_partition .get_path(), operationdetail ) ;
+	create( new_partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void ext2::create( const Partition & new_partition, OperationDetail & operationdetail,
+	sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "mkfs.ext2 -L \"" + new_partition .label + "\" " + new_partition .get_path(),
+			 operationdetail,
+			 set_success );
 }
 
 bool ext2::resize( const Partition & partition_new, OperationDetail & operationdetail, bool fill_partition )
 {
+	resize( partition_new, operationdetail, fill_partition, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void ext2::resize( const Partition & partition_new, OperationDetail & operationdetail,
+		   bool fill_partition, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
 	Glib::ustring str_temp = "resize2fs " + partition_new .get_path() ;
 	
-	if ( ! fill_partition )
+	if ( !fill_partition )
 		str_temp += " " + Utils::num_to_str( Utils::round( Utils::sector_to_unit( 
-					partition_new .get_sector_length(), partition_new .sector_size, UNIT_KIB ) ) -1 ) + "K" ; 
-		
-	return ! execute_command( str_temp, operationdetail ) ;
+					partition_new.get_sector_length(), partition_new.sector_size, UNIT_KIB ) ) - 1 ) + "K";
+	execute_command( str_temp,
+			 operationdetail,
+			 set_success );
+}
+
+void ext2::move( const Partition & partition_new, const Partition & partition_old,
+		 OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	success = false;
+	slot( 1.0 );
 }
 
 bool ext2::move( const Partition & partition_new
@@ -158,6 +259,13 @@ bool ext2::move( const Partition & partition_new
 	return true ;
 }
 
+void ext2::copy( const Glib::ustring & src_part_path, const Glib::ustring & dest_part_path,
+		 OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	success = false;
+	slot( 1.0 );
+}
+
 bool ext2::copy( const Glib::ustring & src_part_path, 
 		 const Glib::ustring & dest_part_path,
 		 OperationDetail & operationdetail )
@@ -167,11 +275,28 @@ bool ext2::copy( const Glib::ustring & src_part_path,
 
 bool ext2::check_repair( const Partition & partition, OperationDetail & operationdetail )
 {
-	exit_status = execute_command( "e2fsck -f -y -v " + partition .get_path(), operationdetail ) ;
-	
+	check_repair( partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void ext2::check_repair( const Partition & partition, OperationDetail & operationdetail,
+			 sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "e2fsck -f -y -v " + partition .get_path(),
+			 operationdetail,
+			 sigc::mem_fun( *this, &ext2::check_repair2 ) );
+}
+
+bool ext2::check_repair2( double progress )
+{
+	if ( progress != 1.0 )
+		return false;	
 	//exitstatus 256 isn't documented, but it's returned when the 'FILE SYSTEM IS MODIFIED'
 	//this is quite normal (especially after a copy) so we let the function return true...
-	return ( exit_status == 0 || exit_status == 1 || exit_status == 2 || exit_status == 256 ) ;
+	success = ( exit_status == 0 || exit_status == 1 || exit_status == 2 || exit_status == 256 );
+	return slot( 1.0 );
 }
 
 } //GParted



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