[gparted/psusi/refactor: 5/19] Refactor ext4 to use execute_command



commit eb1e6db22b85d44ad43b0c5a00162f506be48d9a
Author: Phillip Susi <psusi ubuntu com>
Date:   Sun Feb 12 20:41:48 2012 -0500

    Refactor ext4 to use execute_command
    
    Instead of the old synchronous Filesystem::execute_command or
    Utils::execute_command, the ext4 methods now use the async
    execute_command, 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/ext4.h |   23 ++++++
 src/ext4.cc    |  239 +++++++++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 216 insertions(+), 46 deletions(-)
---
diff --git a/include/ext4.h b/include/ext4.h
index e51dbd5..ef4822f 100644
--- a/include/ext4.h
+++ b/include/ext4.h
@@ -29,20 +29,43 @@ class ext4 : 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 write_uuid2( double progress );
+	bool write_label2( double progress );
+	bool create2( double progress );
+	bool resize2( double progress );
+	bool check_repair2( double progress );
 };
 
 
diff --git a/src/ext4.cc b/src/ext4.cc
index bdd00cb..e915993 100644
--- a/src/ext4.cc
+++ b/src/ext4.cc
@@ -66,115 +66,262 @@ FS ext4::get_filesystem_support()
 	return fs ;
 }
 
-void ext4::set_used_sectors( Partition & partition ) 
+void ext4::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(); // wait for completion
+}
+
+void ext4::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, &ext4::set_used_sectors2 ),
+				     &partition ) );
+}
+
+bool ext4::set_used_sectors2( double progress, Partition *partition )
+{
+	if (progress != 1.0 )
+		return false;
+	if ( !exit_status )
 	{
-		index = output .find( "Free blocks:" ) ;
+		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:" ) ;
+			N = -1;
+		index = output.find( "Block size:" );
 		if ( index >= output.length() || 
 		     sscanf( output.substr( index ) .c_str(), "Block size: %Ld", &S ) != 1 )  
-			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) ) ) );
+		success = true;
 	}
 	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 );
+		success = false;
 	}
+	return slot( 1.0 );
 }
 
 void ext4::read_label( Partition & partition )
 {
-	if ( ! Utils::execute_command( "e2label " + partition .get_path(), output, error, true ) )
+	read_label( partition, unlock_mutex );
+	mutex.lock();
+}
+
+void ext4::read_label( Partition & partition, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "e2label " + partition.get_path(),
+			 sigc::bind( sigc::mem_fun( *this, &ext4::read_label2 ),
+				     &partition ) );
+}
+
+bool ext4::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 );
+		success = true;
 	}
 	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 );
+		success = false;
 	}
+	return slot( 1.0 );
 }
 
 bool ext4::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 ext4::write_label( const Partition & partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "e2label " + partition .get_path() + " \"" + partition .label + "\"",
+			 operationdetail,
+			 sigc::mem_fun( *this, &ext4::write_label2 ) );
+}
+
+bool ext4::write_label2( double progress )
+{
+	if ( progress != 1.0 )
+		return false;
+	success = !exit_status;
+	slot( 1.0 );
+	return false;
 }
 
 void ext4::read_uuid( Partition & partition )
 {
-	if ( ! Utils::execute_command( "tune2fs -l " + partition .get_path(), output, error, true ) )
+	read_uuid( partition, unlock_mutex );
+	mutex.lock();
+}
+
+void ext4::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, &ext4::read_uuid2 ),
+				 &partition ) );
+}
+
+bool ext4::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();
+		success = true;
 	}
 	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 );
+		success = false;
 	}
+	return slot( 1.0 );
 }
 
 bool ext4::write_uuid( const Partition & partition, OperationDetail & operationdetail )
 {
-	return ! execute_command( "tune2fs -U random " + partition .get_path(), operationdetail ) ;
+	write_uuid( partition, operationdetail );
+	mutex.lock();
+	return success;
+}
+
+void ext4::write_uuid( const Partition & partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "tune2fs -U random " + partition.get_path(),
+			 operationdetail,
+			 sigc::mem_fun( *this, &ext4::write_uuid2 ) );
+}
+
+bool ext4::write_uuid2( double progress )
+{
+	if ( progress != 1.0 )
+		return false;
+	success = !exit_status;
+	return slot( 1.0 );
 }
 
 bool ext4::create( const Partition & new_partition, OperationDetail & operationdetail )
 {
-	return ! execute_command( "mkfs.ext4 -j -O extent -L \"" + new_partition .label + "\" " + new_partition .get_path(), operationdetail ) ;
+	create( new_partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void ext4::create( const Partition & new_partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "mkfs.ext4 -j -O extent -L \"" + new_partition.label + "\" " + new_partition.get_path(),
+			 operationdetail,
+			 sigc::mem_fun( *this, &ext4::create2 ) );
+}
+
+bool ext4::create2( double progress )
+{
+	if ( progress != 1.0 )
+		return false;
+	success = !exit_status;
+	return slot( 1.0 );
 }
 
 bool ext4::resize( const Partition & partition_new, OperationDetail & operationdetail, bool fill_partition )
 {
+	resize( partition_new, operationdetail, fill_partition, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void ext4::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 )
 		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 ) ;
+	execute_command( str_temp, operationdetail, sigc::mem_fun( *this, &ext4::resize2 ) );
 }
 
-bool ext4::move( const Partition & partition_new
-               , const Partition & partition_old
-               , OperationDetail & operationdetail
-               )
+bool ext4::resize2( double progress )
 {
-	return true ;
+	if( progress != 1.0 )
+		return false;
+	success = !exit_status;
+	return slot( 1.0 );
 }
 
-bool ext4::copy( const Glib::ustring & src_part_path,
-		 const Glib::ustring & dest_part_path,
+bool ext4::move( const Partition & partition_new, const Partition & partition_old,
 		 OperationDetail & operationdetail )
 {
-	return true ;
+	return false;
+}
+
+void ext4::move( const Partition & partition_new, const Partition & partition_old,
+		 OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+}
+
+bool ext4::copy( const Glib::ustring & src_part_path, const Glib::ustring & dest_part_path,
+		 OperationDetail & operationdetail )
+{
+	return false;
+}
+
+void ext4::copy( const Glib::ustring & src_part_path, const Glib::ustring & dest_part_path,
+		 OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
 }
 
 bool ext4::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 ext4::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, &ext4::check_repair2 ) );
+}
+
+bool ext4::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]