[gparted/psusi/refactor: 9/19] Refactor fat32 to use execute_command_async



commit fa6fe7d5dce5a12a559ba3dd3805b1375cf48b43
Author: Phillip Susi <psusi ubuntu com>
Date:   Tue Feb 14 21:08:22 2012 -0500

    Refactor fat32 to use execute_command_async
    
    Instead of Filesystem::execute_command or Utils::execute_command, the fat32
    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/fat32.h |   22 +++++-
 src/fat32.cc    |  256 +++++++++++++++++++++++++++++++++++++++----------------
 2 files changed, 204 insertions(+), 74 deletions(-)
---
diff --git a/include/fat32.h b/include/fat32.h
index 8839ad5..8fb0697 100644
--- a/include/fat32.h
+++ b/include/fat32.h
@@ -31,22 +31,42 @@ public:
 	const Glib::ustring get_custom_text( CUSTOM_TEXT ttype, int index = 0 ) ;
 	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 );
 	const static Glib::ustring ( & Change_UUID_Warning ) [] ;
+private:
+	char *fname;
+	bool set_used_sectors2( double progress, Partition *partition );
+	bool read_label2( double progress, Partition *partition );
+	bool write_label2( double progress, OperationDetail *operationdetail );
+	bool read_uuid2( double progress, Partition *partition );
+	bool write_uuid2( double progress, OperationDetail *operationdetail );
+	bool check_repair2( double progress );
 };
 
 } //GParted
diff --git a/src/fat32.cc b/src/fat32.cc
index c49ed50..d9a4443 100644
--- a/src/fat32.cc
+++ b/src/fat32.cc
@@ -88,14 +88,29 @@ FS fat32::get_filesystem_support()
 	return fs ;
 }
 
-void fat32::set_used_sectors( Partition & partition ) 
+void fat32::set_used_sectors( Partition & partition )
 {
+	set_used_sectors( partition, unlock_mutex );
+	mutex.lock();
+}
+
+void fat32::set_used_sectors( Partition & partition, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
 	//FIXME: i've encoutered a readonly fat32 file system.. this won't work with the -a ... best check also without the -a
-	exit_status = Utils::execute_command( "dosfsck -n -v " + partition .get_path(), output, error, true ) ;
+	execute_command( "dosfsck -n -v " + partition .get_path(),
+			 sigc::bind( sigc::mem_fun( *this, &fat32::set_used_sectors2 ),
+				     &partition ) );
+}
+
+bool fat32::set_used_sectors2( double progress, Partition *partition )
+{
+	if ( progress != 1.0 )
+		return false;
 	if ( exit_status == 0 || exit_status == 1 || exit_status == 256 )
 	{
 		//free clusters
-		index = output .find( ",", output .find( partition .get_path() ) + partition .get_path() .length() ) +1 ;
+		index = output.find( ",", output.find( partition->get_path() ) + partition->get_path().length() ) + 1;
 		if ( index < output .length() && sscanf( output .substr( index ) .c_str(), "%Ld/%Ld", &S, &N ) == 2 ) 
 			N -= S ;
 		else
@@ -103,147 +118,217 @@ void fat32::set_used_sectors( Partition & partition )
 
 		//bytes per cluster
 		index = output .rfind( "\n", output .find( "bytes per cluster" ) ) +1 ;
-		if ( index >= output .length() || sscanf( output .substr( index ) .c_str(), "%Ld", &S ) != 1 )
-			S = -1 ;
-	
+		if ( index >= output.length() || sscanf( output.substr( index ).c_str(), "%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 ( !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 );
 }
 
 void fat32::read_label( Partition & partition )
 {
+	read_label( partition, unlock_mutex );
+	mutex.lock();
+}
+
+void fat32::read_label( Partition & partition, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
 	//Create mtools config file
-	char fname[] = "/tmp/gparted-XXXXXXXX" ;
+	fname = strdup( "/tmp/gparted-XXXXXXXX" );
 	char dletter = 'H' ;
 	Glib::ustring err_msg = "" ;
 	err_msg = Utils::create_mtoolsrc_file( fname, dletter, partition.get_path() ) ;
 	if( err_msg.length() != 0 )
 		partition .messages .push_back( err_msg );
 
-	Glib::ustring cmd = String::ucompose( "export MTOOLSRC=%1 && mlabel -s %2:", fname, dletter ) ;
+	Glib::ustring cmd = String::ucompose( "sh -c 'MTOOLSRC=%1 mlabel -s %2:'", fname, dletter ) ;
+
+	execute_command( cmd,
+			 sigc::bind( sigc::mem_fun( *this, &fat32::read_label2 ),
+				     &partition ) );
+}
 
-	if ( ! Utils::execute_command( cmd, output, error, true ) )
+bool fat32::read_label2( double progress, Partition *partition )
+{
+	if ( progress != 1.0 )
+		return false;
+	if ( !exit_status )
 	{
-		partition .label = Utils::trim( Utils::regexp_label( output, "Volume label is ([^(]*)" ) ) ;
+		partition->label = Utils::trim( Utils::regexp_label( output, "Volume label is ([^(]*)" ) );
 	}
 	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 );
 	}
-	
+
 	//Delete mtools config file
-	err_msg = Utils::delete_mtoolsrc_file( fname );
+	Utils::delete_mtoolsrc_file( fname );
+	free( fname );
+	fname = 0;
+	return slot( 1.0 );
 }
 
 bool fat32::write_label( const Partition & partition, OperationDetail & operationdetail )
 {
+	write_label( partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void fat32::write_label( const Partition & partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
 	//Create mtools config file
-	char fname[] = "/tmp/gparted-XXXXXXXX" ;
+	fname = strdup( "/tmp/gparted-XXXXXXXX" );
 	char dletter = 'H' ;
 	Glib::ustring err_msg = "" ;
 	err_msg = Utils::create_mtoolsrc_file( fname, dletter, partition.get_path() ) ;
 
 	Glib::ustring cmd = "" ;
 	if( partition .label .empty() )
-		cmd = String::ucompose( "export MTOOLSRC=%1 && mlabel -c %2:", fname, dletter ) ;
+		cmd = String::ucompose( "sh -c 'MTOOLSRC=%1 mlabel -c %2:'", fname, dletter ) ;
 	else
-		cmd = String::ucompose( "export MTOOLSRC=%1 && mlabel %2:\"%3\"", fname, dletter, Utils::fat_compliant_label( partition .label ) ) ;
-	operationdetail .add_child( OperationDetail( cmd, STATUS_NONE, FONT_BOLD_ITALIC ) ) ;
-	
-	int exit_status = Utils::execute_command( cmd, output, error ) ;
-
-	if ( ! output .empty() )
-		operationdetail .get_last_child() .add_child( OperationDetail( output, STATUS_NONE, FONT_ITALIC ) ) ;
-
-	if ( ! error .empty() )
-		operationdetail .get_last_child() .add_child( OperationDetail( error, STATUS_NONE, FONT_ITALIC ) ) ;
+		cmd = String::ucompose( "sh -c 'MTOOLSRC=%1 mlabel %2:\"%3\"'", fname, dletter, Utils::fat_compliant_label( partition .label ) ) ;
+	execute_command( cmd, operationdetail,
+			 sigc::bind( sigc::mem_fun( *this, &fat32::write_label2 ),
+				     &operationdetail ) );
+}
 
+bool fat32::write_label2( double progress, OperationDetail *operationdetail )
+{
+	if ( progress != 1.0 )
+		return false;
 	//Delete mtools config file
-	err_msg = Utils::delete_mtoolsrc_file( fname );
-
-	return ( exit_status == 0 );
+	Utils::delete_mtoolsrc_file( fname );
+	free ( fname );
+	fname = 0;
+	success = !exit_status;
+	return slot( 1.0 );
 }
 
 void fat32::read_uuid( Partition & partition )
 {
+	read_uuid( partition, unlock_mutex );
+	mutex.lock();
+}
+
+void fat32::read_uuid( Partition & partition, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
 	//Create mtools config file
-	char fname[] = "/tmp/gparted-XXXXXXXX" ;
+	fname = strdup( "/tmp/gparted-XXXXXXXX" );
 	char dletter = 'H' ;
 	Glib::ustring err_msg = "" ;
 	err_msg = Utils::create_mtoolsrc_file( fname, dletter, partition.get_path() ) ;
 	if( err_msg.length() != 0 )
 		partition .messages .push_back( err_msg );
 
-	Glib::ustring cmd = String::ucompose( "export MTOOLSRC=%1 && mdir -f %2:", fname, dletter ) ;
+	Glib::ustring cmd = String::ucompose( "sh -c 'MTOOLSRC=%1 mdir -f %2:'", fname, dletter ) ;
+
+	execute_command( cmd,
+			 sigc::bind( sigc::mem_fun( *this, &fat32::read_uuid2 ),
+				     &partition ) );
+}
 
-	if ( ! Utils::execute_command( cmd, output, error, true ) )
+bool fat32::read_uuid2( double progress, Partition *partition )
+{
+	if ( progress != 1.0 )
+		return false;
+	if ( !exit_status )
 	{
-		partition .uuid = Utils::regexp_label( output, "Volume Serial Number is[[:blank:]]([^[:space:]]+)" ) ;
-		if ( partition .uuid == "0000-0000" )
-			partition .uuid .clear() ;
+		partition->uuid = Utils::regexp_label( output, "Volume Serial Number is[[:blank:]]([^[:space:]]+)" );
+		if ( partition->uuid == "0000-0000" )
+			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;
 	}
-
-	err_msg = Utils::delete_mtoolsrc_file( fname );
+	Utils::delete_mtoolsrc_file( fname );
+	free( fname );
+	fname = 0;
+	return slot( 1.0 );
 }
 
 
 bool fat32::write_uuid( const Partition & partition, OperationDetail & operationdetail )
 {
+	write_uuid( partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void fat32::write_uuid( const Partition & partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
 	//Create mtools config file
-	char fname[] = "/tmp/gparted-XXXXXXXX" ;
+	fname = strdup( "/tmp/gparted-XXXXXXXX" );
 	char dletter = 'H' ;
 	Glib::ustring err_msg = "" ;
 	err_msg = Utils::create_mtoolsrc_file( fname, dletter, partition.get_path() ) ;
 
-	// Wait some time - 'random' UUIDs turn out identical if generated in quick succession...
-	sleep(1);
-	Glib::ustring cmd = String::ucompose( "export MTOOLSRC=%1 && mlabel -s -n %2:", fname, dletter ) ;
-
-	operationdetail .add_child( OperationDetail( cmd, STATUS_NONE, FONT_BOLD_ITALIC ) ) ;
-
-	int exit_status = Utils::execute_command( cmd, output, error ) ;
+	Glib::ustring cmd = String::ucompose( "sh -c 'MTOOLSRC=%1 mlabel -s -n %2:'", fname, dletter ) ;
 
-	if ( ! output .empty() )
-		operationdetail .get_last_child() .add_child( OperationDetail( output, STATUS_NONE, FONT_ITALIC ) ) ;
-
-	if ( ! error .empty() )
-		operationdetail .get_last_child() .add_child( OperationDetail( error, STATUS_NONE, FONT_ITALIC ) ) ;
+	execute_command( cmd, operationdetail,
+			 sigc::bind( sigc::mem_fun( *this, &fat32::write_uuid2 ),
+				     &operationdetail ) );
+}
 
+bool fat32::write_uuid2( double progress, OperationDetail *operationdetail )
+{
+	if ( progress != 1.0 )
+		return false;
 	//Delete mtools config file
-	err_msg = Utils::delete_mtoolsrc_file( fname );
-
-	return ( exit_status == 0 );
+	Utils::delete_mtoolsrc_file( fname );
+	free( fname );
+	fname = 0;
+	success = !exit_status;
+	return slot( 1.0 );
 }
 
 bool fat32::create( const Partition & new_partition, OperationDetail & operationdetail )
 {
-	return ! execute_command( "mkdosfs -F32 -v -n \"" + Utils::fat_compliant_label( new_partition .label ) + "\" " + new_partition .get_path(), operationdetail ) ;
+	create( new_partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void fat32::create( const Partition & new_partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "mkdosfs -F32 -v -n \"" + Utils::fat_compliant_label( new_partition .label ) +
+			 "\" " + new_partition .get_path(), operationdetail,
+			 set_success );
 }
 
 bool fat32::resize( const Partition & partition_new, OperationDetail & operationdetail, bool fill_partition )
 {
-	return true ;
+	return false;
+}
+
+void fat32::resize( const Partition & partition_new, OperationDetail & operationdetail,
+		    bool fill_partition, sigc::slot<bool, double> slot )
+{
 }
 
 bool fat32::move( const Partition & partition_new
@@ -254,18 +339,43 @@ bool fat32::move( const Partition & partition_new
 	return true ;
 }
 
-bool fat32::copy( const Glib::ustring & src_part_path, 
-		  const Glib::ustring & dest_part_path,
+void fat32::move( const Partition & partition_new, const Partition & partition_old,
+		  OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+}
+
+void fat32::copy( const Glib::ustring & src_part_path, const Glib::ustring & dest_part_path,
+		  OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+}
+
+bool fat32::copy( const Glib::ustring & src_part_path, const Glib::ustring & dest_part_path,
 		  OperationDetail & operationdetail )
 {
-	return true ;
+	return false;
 }
 
 bool fat32::check_repair( const Partition & partition, OperationDetail & operationdetail )
 {
-	exit_status = execute_command( "dosfsck -a -w -v " + partition .get_path(), operationdetail ) ;
+	check_repair( partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
 
-	return ( exit_status == 0 || exit_status == 1 || exit_status == 256 ) ;
+void fat32::check_repair( const Partition & partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "dosfsck -a -w -v " + partition .get_path(),
+			 operationdetail,
+			 sigc::mem_fun( *this, &fat32::check_repair2 ) );
+}
+
+bool fat32::check_repair2( double progress )
+{
+	if ( progress != 1.0 )
+		return false;
+	success = ( exit_status == 0 || exit_status == 1 || exit_status == 256 );
+	return slot( 1.0 );
 }
 
 } //GParted



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