[gparted/psusi/refactor: 17/19] Refactor nilfs2 to use execute_command_async



commit d4b20ea53897575bff0c7212b0c1a8562c923893
Author: Phillip Susi <psusi ubuntu com>
Date:   Mon Feb 20 21:35:13 2012 -0500

    Refactor nilfs2 to use execute_command_async
    
    Instead of Filesystem::execute_command or Utils::execute_command, the
    nilfs2 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/nilfs2.h |   21 +++++
 src/nilfs2.cc    |  237 ++++++++++++++++++++++++++++++++++++++++++------------
 2 files changed, 205 insertions(+), 53 deletions(-)
---
diff --git a/include/nilfs2.h b/include/nilfs2.h
index dae5167..2635d0a 100644
--- a/include/nilfs2.h
+++ b/include/nilfs2.h
@@ -29,21 +29,42 @@ class nilfs2 : 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:
+	Glib::ustring mount_point;
+	bool set_used_sectors2( double progress, Partition *partition );
+	bool read_label2( double progress, Partition *partition );
+	bool read_uuid2( double progress, Partition *partition );
+	bool resize2( double progress, const Partition *partition_new, OperationDetail *operationdetail, bool fill_partition );
+	bool resize3( double progress, OperationDetail *operationdetail );
+	bool resize4( double progress, OperationDetail *operationdetail );
 };
 
 } //GParted
diff --git a/src/nilfs2.cc b/src/nilfs2.cc
index 72b8005..11fad83 100644
--- a/src/nilfs2.cc
+++ b/src/nilfs2.cc
@@ -64,112 +64,229 @@ FS nilfs2::get_filesystem_support()
 
 void nilfs2::set_used_sectors( Partition & partition )
 {
-	if ( ! Utils::execute_command( "nilfs-tune -l " + partition .get_path(), output, error, true ) )
-	{
-		Glib::ustring::size_type index = output .find( "Free blocks count:" ) ;
-		if (   index == Glib::ustring::npos
-		    || sscanf( output.substr( index ) .c_str(), "Free blocks count: %Ld", &N ) != 1
-		   )
-			N = -1 ;
-
-		index = output .find( "Block size:" ) ;
-		if (   index == Glib::ustring::npos
-		    || sscanf( output.substr( index ) .c_str(), "Block size: %Ld", &S ) != 1
-		   )
-			S = -1 ;
+	set_used_sectors( partition, unlock_mutex );
+	mutex.lock();
+}
 
+void nilfs2::set_used_sectors( Partition & partition, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "nilfs-tune -l " + partition.get_path(),
+			 sigc::bind( sigc::mem_fun( *this, &nilfs2::set_used_sectors2 ),
+				     &partition ) );
+}
+
+bool nilfs2::set_used_sectors2( double progress, Partition *partition )
+{
+	if ( progress != 1.0 )
+		return false;
+	if ( !exit_status )
+	{
+		Glib::ustring::size_type index = output.find( "Free blocks count:" );
+		if ( index == Glib::ustring::npos ||
+		     sscanf( output.substr( index ).c_str(), "Free blocks count: %Ld", &N ) != 1 )
+			N = -1;
+		index = output.find( "Block size:" );
+		if ( index == Glib::ustring::npos ||
+		     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) ) ) );
+		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 nilfs2::read_label( Partition & partition )
 {
-	if ( ! Utils::execute_command( "nilfs-tune -l " + partition .get_path(), output, error, true ) )
+	read_label( partition, unlock_mutex );
+	mutex.lock();
+}
+
+void nilfs2::read_label( Partition & partition, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "nilfs-tune -l " + partition.get_path(),
+			 sigc::bind( sigc::mem_fun( *this, &nilfs2::read_label2 ),
+				     &partition ) );
+}
+
+bool nilfs2::read_label2( double progress, Partition *partition )
+{
+	if ( progress != 1.0 )
+		return false;
+	if ( !exit_status )
 	{
-		Glib::ustring label = Utils::regexp_label( output, "^Filesystem volume name:[\t ]*(.*)$" ) ;
+		Glib::ustring label = Utils::regexp_label( output, "^Filesystem volume name:[\t ]*(.*)$" );
 		if ( label != "(none)" )
-			partition .label = label;
+			partition->label = 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 nilfs2::write_label( const Partition & partition, OperationDetail & operationdetail )
 {
-	return ! execute_command( "nilfs-tune -L \"" + partition .label + "\" " + partition .get_path(), operationdetail ) ;
+	write_label( partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void nilfs2::write_label( const Partition & partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "nilfs-tune -L \"" + partition .label + "\" " + partition .get_path(),
+			 operationdetail,
+			 set_success );
 }
 
 void nilfs2::read_uuid( Partition & partition )
 {
-	if ( ! Utils::execute_command( "nilfs-tune -l " + partition .get_path(), output, error, true ) )
+	read_uuid( partition, unlock_mutex );
+	mutex.lock();
+}
+
+void nilfs2::read_uuid( Partition & partition, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "nilfs-tune -l " + partition.get_path(),
+			 sigc::bind( sigc::mem_fun( *this, &nilfs2::read_uuid2 ),
+				     &partition ) );
+}
+
+bool nilfs2::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 == "00000000-0000-0000-0000-000000000000")
-			partition .uuid .clear() ;
+		partition->uuid = Utils::regexp_label( output, "^Filesystem 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 nilfs2::write_uuid( const Partition & partition, OperationDetail & operationdetail )
 {
-	return ! execute_command( "nilfs-tune -U " + Utils::generate_uuid() + " " + partition .get_path(), operationdetail ) ;
+	write_uuid( partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void nilfs2::write_uuid( const Partition & partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "nilfs-tune -U " + Utils::generate_uuid() + " " + partition.get_path(),
+			 operationdetail,
+			 set_success );
 }
 
 bool nilfs2::create( const Partition & new_partition, OperationDetail & operationdetail )
 {
-	return ! execute_command( "mkfs.nilfs2 -L \"" + new_partition .label + "\" " + new_partition .get_path(), operationdetail ) ;
+	create( new_partition, operationdetail, unlock_mutex );
+	mutex.lock();
+	return success;
+}
+
+void nilfs2::create( const Partition & new_partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+	this->slot = slot;
+	execute_command( "mkfs.nilfs2 -L \"" + new_partition.label + "\" " + new_partition.get_path(),
+			 operationdetail,
+			 set_success );
 }
 
 bool nilfs2::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 nilfs2::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 nilfs2 " + 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 nilfs2 " + partition_new.get_path() + " " + mount_point,
+			 operationdetail,
+			 sigc::bind( sigc::mem_fun( *this, &nilfs2::resize2 ),
+				     &partition_new, &operationdetail, fill_partition ) );
+}
+
+bool nilfs2::resize2( double progress, const Partition *partition_new, OperationDetail *operationdetail, bool fill_partition )
+{
+	if ( progress != 1.0 )
+		return false;
+	success &= !exit_status;
 
 	if ( success )
 	{
-		Glib::ustring cmd = "nilfs-resize -v -y " + partition_new .get_path() ;
-		if ( ! fill_partition )
+		Glib::ustring cmd = "nilfs-resize -v -y " + partition_new->get_path();
+		if ( !fill_partition )
 		{
 			Glib::ustring size = Utils::num_to_str( floor( Utils::sector_to_unit(
-					partition_new .get_sector_length(), partition_new .sector_size, UNIT_KIB ) ) ) + "K" ;
-			cmd += " " + size ;
+					partition_new->get_sector_length(), partition_new->sector_size, UNIT_KIB ) ) ) + "K";
+			cmd += " " + size;
 		}
-		success &= ! execute_command_timed( cmd, operationdetail ) ;
+		execute_command( cmd, *operationdetail,
+				 sigc::bind( sigc::mem_fun( *this, &nilfs2::resize3 ),
+					     operationdetail ) );
 
-		success &= ! execute_command_timed( "umount -v " + mount_point, operationdetail ) ;
+	} else {
+		rm_temp_dir( mount_point, *operationdetail );
 	}
+	return slot( 1.0 );
+}
 
-	rm_temp_dir( mount_point, operationdetail ) ;
+bool nilfs2::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, &nilfs2::resize4 ),
+				     operationdetail ) );
+	return slot( 1.0 );
+}
 
-	return success ;
+bool nilfs2::resize4( double progress, OperationDetail *operationdetail )
+{
+	if ( progress != 1.0 )
+		return false;
+	rm_temp_dir( mount_point, *operationdetail );
+	return slot( 1.0 );
 }
 
 bool nilfs2::move( const Partition & partition_new
@@ -180,6 +297,11 @@ bool nilfs2::move( const Partition & partition_new
 	return true ;
 }
 
+void nilfs2::move( const Partition & partition_new, const Partition & partition_old,
+		   OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+}
+
 bool nilfs2::copy( const Glib::ustring & src_part_path
                  , const Glib::ustring & dest_part_path
                  , OperationDetail & operationdetail
@@ -188,9 +310,18 @@ bool nilfs2::copy( const Glib::ustring & src_part_path
 	return true ;
 }
 
+void nilfs2::copy( const Glib::ustring & src_part_path, const Glib::ustring & dest_part_path,
+		   OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+}
+
 bool nilfs2::check_repair( const Partition & partition, OperationDetail & operationdetail )
 {
 	return true ;
 }
 
+void nilfs2::check_repair( const Partition & partition, OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+}
+
 } //GParted



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