[gparted/psusi/refactor: 10/19] Refactor xfs to use execute_command_async



commit e5ecb59b326dc9efa8a42e3283c964b862686c00
Author: Phillip Susi <psusi ubuntu com>
Date:   Thu Feb 16 21:11:39 2012 -0500

    Refactor xfs to use execute_command_async
    
    Instead of Filesystem::execute_command or Utils::execute_command, the xfs
    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/xfs.h |   41 ++++++
 src/xfs.cc    |  378 +++++++++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 344 insertions(+), 75 deletions(-)
---
diff --git a/include/xfs.h b/include/xfs.h
index bc43ba1..34d6342 100644
--- a/include/xfs.h
+++ b/include/xfs.h
@@ -30,20 +30,61 @@ class xfs : 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 );
+	bool resize3( double progress, OperationDetail *operationdetail );
+	bool resize4( double progress, OperationDetail *operationdetail );
+	bool copy2( double progress, OperationDetail *operationdetail,
+		    const Glib::ustring *src_part_path,
+		    const Glib::ustring *dest_part_path );
+	bool copy3( double progress, OperationDetail *operationdetail,
+		    const Glib::ustring *src_part_path,
+		    const Glib::ustring *dest_part_path );
+	bool copy4( double progress, OperationDetail *operationdetail,
+		    const Glib::ustring *src_part_path,
+		    const Glib::ustring *dest_part_path );
+	bool copy5( double progress, OperationDetail *operationdetail,
+		    const Glib::ustring *src_part_path,
+		    const Glib::ustring *dest_part_path );
+	bool copy6( double progress, OperationDetail *operationdetail,
+		    const Glib::ustring *src_part_path,
+		    const Glib::ustring *dest_part_path );
+	bool copy7( double progress, OperationDetail *operationdetail,
+		    const Glib::ustring *src_part_path,
+		    const Glib::ustring *dest_part_path );
+	Glib::ustring src_mount_point;
+	Glib::ustring dest_mount_point;
 };
 
 } //GParted
diff --git a/src/xfs.cc b/src/xfs.cc
index 6f4e160..b0a8f2e 100644
--- a/src/xfs.cc
+++ b/src/xfs.cc
@@ -73,116 +73,232 @@ FS xfs::get_filesystem_support()
 	return fs ;
 }
 
-void xfs::set_used_sectors( Partition & partition ) 
+void xfs::set_used_sectors( Partition & partition )
 {
-	if ( ! Utils::execute_command( 
-			"xfs_db -c 'sb 0' -c 'print blocksize' -c 'print fdblocks' -r " + partition .get_path(),
-			output,
-			error,
-			true ) )
+	set_used_sectors( partition, unlock_mutex );
+	mutex.lock();
+}
+
+void xfs::set_used_sectors( Partition & partition, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( 
+		"xfs_db -c 'sb 0' -c 'print blocksize' -c 'print fdblocks' -r " + partition.get_path(),
+		sigc::bind( sigc::mem_fun( *this, &xfs::set_used_sectors2 ),
+			    &partition ) );
+}
+
+bool xfs::set_used_sectors2( double progress, Partition *partition )
+{
+	if ( progress != 1.0 )
+		return false;
+	if ( !exit_status )
 	{
 		//blocksize
 		if ( sscanf( output .c_str(), "blocksize = %Ld", &S ) != 1 )
 			S = -1 ;
 
 		//free blocks
-		index = output .find( "fdblocks" ) ;
-		if ( index > output .length() ||
-		     sscanf( output .substr( index ) .c_str(), "fdblocks = %Ld", &N ) != 1 )
-			N = -1 ;
+		index = output.find( "fdblocks" );
+		if ( index > output.length() ||
+		     sscanf( output.substr( index ).c_str(), "fdblocks = %Ld", &N ) != 1 )
+			N = -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 xfs::read_label( Partition & partition )
 {
-	if ( ! Utils::execute_command( "xfs_db -r -c 'label' " + partition .get_path(), output, error, true ) )
+	read_label( partition, unlock_mutex );
+	mutex.lock();
+}
+
+void xfs::read_label( Partition & partition, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "xfs_db -r -c 'label' " + partition.get_path(),
+			 sigc::bind( sigc::mem_fun( *this, &xfs::read_label2 ),
+				     &partition ) );
+}
+
+bool xfs::read_label2( double progress, Partition *partition )
+{
+	if ( progress != 1.0 )
+		return false;
+	if ( !exit_status )
 	{
-		partition .label = Utils::regexp_label( output, "^label = \"(.*)\"" ) ;
+		partition->label = Utils::regexp_label( output, "^label = \"(.*)\"" );
 	}
 	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 xfs::write_label( const Partition & partition, OperationDetail & operationdetail )
 {
+	write_label( partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void xfs::write_label( const Partition & partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
 	Glib::ustring cmd = "" ;
 	if( partition .label .empty() )
 		cmd = String::ucompose( "xfs_admin -L -- %1", partition .get_path() ) ;
 	else
 		cmd = String::ucompose( "xfs_admin -L \"%1\" %2", partition .label, partition .get_path() ) ;
-	return ! execute_command( cmd, operationdetail ) ;
+	execute_command( cmd, operationdetail, set_success );
 }
 
 void xfs::read_uuid( Partition & partition )
 {
-	if ( ! Utils::execute_command( "xfs_admin -u " + partition .get_path(), output, error, true ) )
+	read_uuid( partition, unlock_mutex );
+	mutex.lock();
+}
+
+void xfs::read_uuid( Partition & partition, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "xfs_admin -u " + partition.get_path(),
+			 sigc::bind( sigc::mem_fun( *this, &xfs::read_uuid2 ),
+				     &partition ) );
+}
+
+bool xfs::read_uuid2( double progress, Partition *partition )
+{
+	if ( progress != 1.0 )
+		return false;
+	if ( !exit_status )
 	{
-		partition .uuid = Utils::regexp_label( output, "^UUID[[:blank:]]*=[[:blank:]]*([^[:space:]]*)" ) ;
-		if (partition .uuid == "00000000-0000-0000-0000-000000000000")
-			partition .uuid .clear() ;
+		partition->uuid = Utils::regexp_label( output, "^UUID[[:blank:]]*=[[: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 xfs::write_uuid( const Partition & partition, OperationDetail & operationdetail )
 {
-	return ! execute_command( "xfs_admin -U generate " + partition .get_path(), operationdetail ) ;
+	write_uuid( partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void xfs::write_uuid( const Partition & partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this-> slot = slot;
+	execute_command( "xfs_admin -U generate " + partition.get_path(),
+			 operationdetail,
+			 set_success );
 }
 
 bool xfs::create( const Partition & new_partition, OperationDetail & operationdetail )
 {
+	create( new_partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void xfs::create( const Partition & new_partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
 	//mkfs.xfs will not create file system if label is longer than 12 characters, hence truncation.
 	Glib::ustring label = new_partition .label ;
 	if( label .length() > 12 )
 		label = label.substr( 0, 12 ) ;
-	return ! execute_command( "mkfs.xfs -f -L \"" + label + "\" " + new_partition .get_path(), operationdetail ) ;
+	execute_command( "mkfs.xfs -f -L \"" + label + "\" " + new_partition.get_path(),
+			 operationdetail,
+			 set_success );
 }
 
 bool xfs::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 ) ;
+void xfs::resize( const Partition & partition_new, OperationDetail & operationdetail,
+		  bool fill_partition, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	success = true;
+	mount_point = mk_temp_dir( "", operationdetail );
 	if ( mount_point .empty() )
-		return false ;
-
-	success &= ! execute_command_timed( "mount -v -t xfs " + partition_new .get_path() + " " + mount_point,
-	                                    operationdetail ) ;
+	{
+		success = false;
+		return;
+	}
+	execute_command( "mount -v -t xfs " + partition_new.get_path() + " " + mount_point,
+			 operationdetail,
+			 sigc::bind( sigc::mem_fun( *this, &xfs::resize2 ),
+				     &operationdetail ) );
+}
 
+bool xfs::resize2( double progress, OperationDetail *operationdetail )
+{
+	if ( progress != 1.0 )
+		return false;
+	success &= !exit_status;
 	if ( success )
 	{
-		success &= ! execute_command_timed( "xfs_growfs " + mount_point, operationdetail ) ;
-
-		success &= ! execute_command_timed( "umount -v " + mount_point, operationdetail ) ;
+		execute_command( "xfs_growfs " + mount_point,
+				 *operationdetail,
+				 sigc::bind( sigc::mem_fun( *this, &xfs::resize3 ),
+					     operationdetail ) );
+		return false;
+	} else {
+		return slot( 1.0 );
 	}
+}
 
-	rm_temp_dir( mount_point, operationdetail ) ;
+bool xfs::resize3( double progress, OperationDetail *operationdetail )
+{
+	if ( progress != 1.0 )
+		return false;
+	success &= !exit_status;
+	execute_command( "umount -v " + mount_point,
+			 *operationdetail,
+			 sigc::bind( sigc::mem_fun( *this, &xfs::resize4 ),
+				     operationdetail ) );
+	return false;
+}
 
-	return success ;
+bool xfs::resize4( double progress, OperationDetail *operationdetail )
+{
+	if ( progress != 1.0 )
+		return false;
+	rm_temp_dir( mount_point, *operationdetail );
+	return slot( 1.0 );
+}
+
+void xfs::move( const Partition & partition_new, const Partition & partition_old,
+		OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
 }
 
 bool xfs::move( const Partition & partition_new
@@ -197,53 +313,165 @@ bool xfs::copy( const Glib::ustring & src_part_path,
 		const Glib::ustring & dest_part_path,
 		OperationDetail & operationdetail )
 {
-	bool success = true ;
-
-	success &= ! execute_command_timed( "mkfs.xfs -f " + dest_part_path, operationdetail ) ;
-	if ( ! success )
-		return false ;
+	copy( src_part_path, dest_part_path, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
 
-	Glib::ustring src_mount_point = mk_temp_dir( "src", operationdetail ) ;
-	if ( src_mount_point .empty() )
-		return false ;
+void xfs::copy( const Glib::ustring & src_part_path, const Glib::ustring & dest_part_path,
+		OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	success = true;
+	execute_command( "mkfs.xfs -f " + dest_part_path,
+			 operationdetail,
+			 sigc::bind( sigc::mem_fun( *this, &xfs::copy2 ),
+				     &operationdetail,
+				     &src_part_path,
+				     &dest_part_path ) );
+}
 
-	Glib::ustring dest_mount_point = mk_temp_dir( "dest", operationdetail ) ;
-	if ( dest_mount_point .empty() )
+bool xfs::copy2( double progress, OperationDetail *operationdetail,
+		 const Glib::ustring *src_part_path,
+		 const Glib::ustring *dest_part_path )
+{
+	if ( progress != 1.0 )
+		return false;
+	success &= !exit_status;
+	if ( !success )
 	{
-		rm_temp_dir( src_mount_point, operationdetail ) ;
-		return false ;
+		return slot( 1.0 );
 	}
 
-	success &= ! execute_command_timed( "mount -v -t xfs -o noatime,ro " + src_part_path +
-	                                    " " + src_mount_point, operationdetail ) ;
+	src_mount_point = mk_temp_dir( "src", *operationdetail );
+	if ( src_mount_point.empty() )
+	{
+		success = false;
+		return slot( 1.0 );
+	}
 
-	if ( success )
+	dest_mount_point = mk_temp_dir( "dest", *operationdetail );
+	if ( dest_mount_point.empty() )
 	{
-		success &= ! execute_command_timed( "mount -v -t xfs " + dest_part_path +
-		                                    " " + dest_mount_point, operationdetail ) ;
+		rm_temp_dir( src_mount_point, *operationdetail );
+		success = false;
+		return slot( 1.0 );
+	}
 
-		if ( success )
-		{
-			success &= ! execute_command_timed( "xfsdump -J - " + src_mount_point +
-			                                    " | xfsrestore -J - " + dest_mount_point,
-			                                    operationdetail ) ;
+	execute_command( "mount -v -t xfs -o noatime,ro " + *src_part_path +
+			 " " + src_mount_point,
+			 *operationdetail,
+			 sigc::bind( sigc::mem_fun( *this, &xfs::copy3 ),
+				     operationdetail,
+				     src_part_path,
+				     dest_part_path ) );
+	return false;
+}
 
-			success &= ! execute_command_timed( "umount -v " + dest_part_path, operationdetail ) ;
-		}
+bool xfs::copy3( double progress, OperationDetail *operationdetail,
+		 const Glib::ustring *src_part_path,
+		 const Glib::ustring *dest_part_path )
+{
+	if ( progress != 1.0 )
+		return false;
+	success &= !exit_status;
+	if ( !success )
+	{
+		rm_temp_dir( dest_mount_point, *operationdetail );
+		rm_temp_dir( src_mount_point, *operationdetail );
+		return slot( 1.0 );
+	}
+	execute_command( "mount -v -t xfs " + *dest_part_path + " " + dest_mount_point,
+			 *operationdetail,
+			 sigc::bind( sigc::mem_fun( *this, &xfs::copy4 ),
+				     operationdetail,
+				     src_part_path,
+				     dest_part_path ) );
+	return false;
+}
 
-		success &= ! execute_command_timed( "umount -v " + src_part_path, operationdetail ) ;
+bool xfs::copy4( double progress, OperationDetail *operationdetail,
+		 const Glib::ustring *src_part_path,
+		 const Glib::ustring *dest_part_path )
+{
+	if ( progress != 1.0 )
+		return false;
+	success &= !exit_status;
+	if ( !success )
+	{
+		execute_command( "umount -v " + *src_part_path,
+				 *operationdetail,
+				 sigc::bind( sigc::mem_fun( *this, &xfs::copy7 ),
+					     operationdetail,
+					     src_part_path,
+					     dest_part_path ) );
+		return false;
 	}
+	execute_command( "sh -c 'xfsdump -J - " + src_mount_point +
+			 " | xfsrestore -J - " + dest_mount_point + "'",
+			 *operationdetail,
+			 sigc::bind( sigc::mem_fun( *this, &xfs::copy5 ),
+				     operationdetail,
+				     src_part_path,
+				     dest_part_path ) );
+	return false;
+}
 
-	rm_temp_dir( dest_mount_point, operationdetail ) ;
+bool xfs::copy5( double progress, OperationDetail *operationdetail,
+		 const Glib::ustring *src_part_path,
+		 const Glib::ustring *dest_part_path )
+{
+	if ( progress != 1.0 )
+		return false;
+	success &= !exit_status;
+	execute_command( "umount -v " + *dest_part_path,
+			 *operationdetail,
+			 sigc::bind( sigc::mem_fun( *this, &xfs::copy6 ),
+				     operationdetail,
+				     src_part_path,
+				     dest_part_path ) );
+	return false;
+}
 
-	rm_temp_dir( src_mount_point, operationdetail ) ;
+bool xfs::copy6( double progress, OperationDetail *operationdetail,
+		 const Glib::ustring *src_part_path,
+		 const Glib::ustring *dest_part_path )
+{
+	if ( progress != 1.0 )
+		return false;
+	execute_command( "umount -v " + *src_part_path,
+			 *operationdetail,
+			 sigc::bind( sigc::mem_fun( *this, &xfs::copy7 ),
+				     operationdetail,
+				     src_part_path,
+				     dest_part_path ) );
+	return false;
+}
 
-	return success ;
+bool xfs::copy7( double progress, OperationDetail *operationdetail,
+		 const Glib::ustring *src_part_path,
+		 const Glib::ustring *dest_part_path )
+{
+	if ( progress != 1.0 )
+		return false;
+	rm_temp_dir( dest_mount_point, *operationdetail );
+	rm_temp_dir( src_mount_point, *operationdetail );
+	return slot( 1.0 );
 }
 
 bool xfs::check_repair( const Partition & partition, OperationDetail & operationdetail )
 {
-	return ! execute_command( "xfs_repair -v " + partition .get_path(), operationdetail ) ;
+	check_repair( partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void xfs::check_repair( const Partition & partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "xfs_repair -v " + partition .get_path(),
+			 operationdetail,
+			 set_success );
 }
 
 } //GParted



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