[gparted/psusi/refactor: 12/19] Refactor jfs to use execute_command_async



commit 0a45c2127974b9508a8c4e6636f666dce1e4951f
Author: Phillip Susi <psusi ubuntu com>
Date:   Sun Feb 19 21:50:22 2012 -0500

    Refactor jfs to use execute_command_async
    
    Instead of Filesystem::execute_command or Utils::execute_command, the jfs
    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/jfs.h |   22 ++++++
 src/jfs.cc    |  235 ++++++++++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 214 insertions(+), 43 deletions(-)
---
diff --git a/include/jfs.h b/include/jfs.h
index 5eba900..f965054 100644
--- a/include/jfs.h
+++ b/include/jfs.h
@@ -30,20 +30,42 @@ class jfs : 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 );
+	Glib::ustring mount_point;
+	bool resize2( double progress, OperationDetail *operationdetail, const Partition *partition_new );
+	bool resize3( double progress, OperationDetail *operationdetail );
+	bool resize4( double progress, OperationDetail *operationdetail );
+	bool check_repair2( double progress );
 };
 
 } //GParted
diff --git a/src/jfs.cc b/src/jfs.cc
index fe1ecb5..803018e 100644
--- a/src/jfs.cc
+++ b/src/jfs.cc
@@ -66,107 +66,230 @@ FS jfs::get_filesystem_support()
 	return fs ;
 }
 
-void jfs::set_used_sectors( Partition & partition ) 
+void jfs::set_used_sectors( Partition & partition )
 {
-	if ( ! Utils::execute_command( "echo dm | jfs_debugfs " + partition .get_path(), output, error, true ) )
+	set_used_sectors( partition, unlock_mutex );
+	mutex.lock();
+}
+
+void jfs::set_used_sectors( Partition & partition, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "sh -c 'echo dm | jfs_debugfs " + partition.get_path() + "'",
+			 sigc::bind( sigc::mem_fun( *this, &jfs::set_used_sectors2 ),
+				     &partition ) );
+}
+
+bool jfs::set_used_sectors2( double progress, Partition *partition )
+{
+	if ( progress != 1.0 )
+		return false;
+	if ( !exit_status )
 	{
 		//blocksize
 		index = output .find( "Block Size:" ) ;
 		if ( index >= output .length() || 
 		     sscanf( output .substr( index ) .c_str(), "Block Size: %Ld", &S ) != 1 ) 
 			S = -1 ;
-		
 		//free blocks
 		index = output .find( "dn_nfree:" ) ;
 		if ( index >= output .length() || 
 		     sscanf( output .substr( index ) .c_str(), "dn_nfree: %Lx", &N ) != 1 ) 
 			N = -1 ;
-
 		if ( S > -1 && N > -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 jfs::read_label( Partition & partition )
 {
-	if ( ! Utils::execute_command( "jfs_tune -l " + partition .get_path(), output, error, true ) )
+	read_label( partition, unlock_mutex );
+	mutex.lock();
+}
+
+void jfs::read_label( Partition & partition, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "jfs_tune -l " + partition.get_path(),
+			 sigc::bind( sigc::mem_fun( *this, &jfs::read_label2 ),
+				     &partition ) );
+}
+
+bool jfs::read_label2( double progress, Partition *partition )
+{
+	if ( progress != 1.0 )
+		return false;
+	if ( !exit_status )
 	{
-		partition .label = Utils::regexp_label( output, "^Volume label:[\t ]*'(.*)'" ) ;
+		partition->label = Utils::regexp_label( output, "^Volume label:[\t ]*'(.*)'" );
 	}
 	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 jfs::write_label( const Partition & partition, OperationDetail & operationdetail )
 {
-	return ! execute_command( "jfs_tune -L \"" + partition .label + "\" " + partition .get_path(), operationdetail ) ;
+	write_label( partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void jfs::write_label( const Partition & partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "jfs_tune -L \"" + partition .label + "\" " + partition .get_path(),
+			 operationdetail,
+			 set_success );
 }
 
 void jfs::read_uuid( Partition & partition )
 {
-	if ( ! Utils::execute_command( "echo su | jfs_debugfs " + partition .get_path(), output, error, true ) )
+	read_uuid( partition, unlock_mutex );
+	mutex.lock();
+}
+
+void jfs::read_uuid( Partition & partition, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "echo su | jfs_debugfs " + partition .get_path(),
+			 sigc::bind( sigc::mem_fun( *this, &jfs::read_uuid2 ),
+				     &partition ) );
+}
+
+bool jfs::read_uuid2( double progress, Partition *partition )
+{
+	if ( progress != 1.0 )
+		return false;
+	if ( !exit_status )
 	{
-		partition .uuid = Utils::regexp_label( output, "s_uuid:[[:blank:]]*([^[:space:]]+)" ) ;
-		if ( partition .uuid == "00000000-0000-0000-0000-000000000000" )
-			partition .uuid .clear() ;
+		partition->uuid = Utils::regexp_label( output, "s_uuid:[[:blank:]]*([^[:space:]]+)" );
+		if ( partition->uuid == "00000000-0000-0000-0000-000000000000" )
+			partition->uuid.clear();
 	}
 	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 jfs::write_uuid( const Partition & partition, OperationDetail & operationdetail )
 {
-	return ! execute_command( "jfs_tune -U random " + partition .get_path(), operationdetail ) ;
+	write_uuid( partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void jfs::write_uuid( const Partition & partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "jfs_tune -U random " + partition.get_path(),
+			 operationdetail,
+			 set_success );
 }
 
 bool jfs::create( const Partition & new_partition, OperationDetail & operationdetail )
 {
-	return ! execute_command( "mkfs.jfs -q -L \"" + new_partition .label + "\" " + new_partition .get_path(), operationdetail ) ;
+	create( new_partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void jfs::create( const Partition & new_partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "mkfs.jfs -q -L \"" + new_partition.label + "\" " + new_partition.get_path(),
+			 operationdetail,
+			 set_success );
 }
 
 bool jfs::resize( const Partition & partition_new, OperationDetail & operationdetail, bool fill_partition )
 {
-	bool success = true ;
+	resize( partition_new, operationdetail, fill_partition, unlock_mutex );
+	mutex.lock();
+	return success;
+}
 
-	Glib::ustring mount_point = mk_temp_dir( "", operationdetail ) ;
-	if ( mount_point .empty() )
-		return false ;
+void jfs::resize( const Partition & partition_new, OperationDetail & operationdetail,
+		  bool fill_partition, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	success = true;
 
-	success &= ! execute_command_timed( "mount -v -t jfs " + partition_new .get_path() + " " + mount_point,
-	                                    operationdetail ) ;
+	mount_point = mk_temp_dir( "", operationdetail );
+	if ( mount_point.empty() )
+	{
+		success = false;
+		slot( 1.0 );
+		return;
+	}
+	execute_command( "mount -v -t jfs " + partition_new.get_path() + " " + mount_point,
+			 operationdetail,
+			 sigc::bind( sigc::mem_fun( *this, &jfs::resize2 ),
+				     &operationdetail,
+				     &partition_new ) );
+}
+
+bool jfs::resize2( double progress, OperationDetail *operationdetail, const Partition *partition_new )
+{
+	if ( progress != 1.0 )
+		return false;
+	success &= !exit_status;
 
 	if ( success )
 	{
-		success &= ! execute_command_timed( "mount -v -t jfs -o remount,resize " + partition_new .get_path() + " " + mount_point,
-		                                    operationdetail ) ;
+		execute_command( "mount -v -t jfs -o remount,resize " + partition_new->get_path() + " " + mount_point,
+				 *operationdetail,
+				 sigc::bind( sigc::mem_fun( *this, &jfs::resize3 ), operationdetail ) );
+		return false;
+	} else return slot( 1.0 );
+}
 
-		success &= ! execute_command_timed( "umount -v " + mount_point, operationdetail ) ;
+bool jfs::resize3( double progress, OperationDetail *operationdetail )
+{
+	if ( progress != 1.0 )
+		return false;
+	success &= !exit_status;
+	if ( success )
+	{
+		execute_command( "umount -v " + mount_point,
+				 *operationdetail,
+				 sigc::bind( sigc::mem_fun( *this, &jfs::resize4 ),
+					     operationdetail ) );
+		return false;
 	}
+	rm_temp_dir( mount_point, *operationdetail ) ;
+	return slot( 1.0 );
+}
 
-	rm_temp_dir( mount_point, operationdetail ) ;
-
-	return success ;
+bool jfs::resize4( double progress, OperationDetail *operationdetail )
+{
+	if ( progress != 1.0 )
+		return false;
+	success &= !exit_status;
+	rm_temp_dir( mount_point, *operationdetail ) ;
+	return slot( 1.0 );
 }
+		
 
 bool jfs::move( const Partition & partition_new
               , const Partition & partition_old
@@ -176,6 +299,11 @@ bool jfs::move( const Partition & partition_new
 	return true ;
 }
 
+void jfs::move( const Partition & partition_new, const Partition & partition_old,
+		OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+}
+
 bool jfs::copy( const Glib::ustring & src_part_path, 
 		const Glib::ustring & dest_part_path,
 		OperationDetail & operationdetail )
@@ -183,11 +311,32 @@ bool jfs::copy( const Glib::ustring & src_part_path,
 	return true ;
 }
 
+void jfs::copy( const Glib::ustring & src_part_path, const Glib::ustring & dest_part_path,
+		OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+}
+
 bool jfs::check_repair( const Partition & partition, OperationDetail & operationdetail )
 {
-	exit_status = execute_command( "jfs_fsck -f " + partition .get_path(), operationdetail ) ;
+	check_repair( partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
 
-	return ( exit_status == 0 || exit_status == 1 ) ;
+void jfs::check_repair( const Partition & partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "jfs_fsck -f " + partition.get_path(),
+			 operationdetail,
+			 sigc::mem_fun( *this, &jfs::check_repair2 ) );
+}
+
+bool jfs::check_repair2( double progress )
+{
+	if ( progress != 1.0 )
+		return false;
+	success = ( exit_status == 0 || exit_status == 1 );
+	return slot( 1.0 );
 }
 
 } //GParted



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