[gparted/psusi/refactor: 8/19] Refactor fat16 to use execute_command_async
- From: Phillip Susi <psusi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gparted/psusi/refactor: 8/19] Refactor fat16 to use execute_command_async
- Date: Tue, 20 Mar 2012 00:03:06 +0000 (UTC)
commit 166bcae4b2d3ee55f51ec87bd288cccc83047c97
Author: Phillip Susi <psusi ubuntu com>
Date: Tue Feb 14 20:23:57 2012 -0500
Refactor fat16 to use execute_command_async
Instead of Filesystem::execute_command or Utils::execute_command, the fat16
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/fat16.h | 26 +++++-
src/fat16.cc | 267 ++++++++++++++++++++++++++++++++++++++++---------------
2 files changed, 219 insertions(+), 74 deletions(-)
---
diff --git a/include/fat16.h b/include/fat16.h
index 7d9203b..f29a8cc 100644
--- a/include/fat16.h
+++ b/include/fat16.h
@@ -31,22 +31,46 @@ 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 );
static const Glib::ustring Change_UUID_Warning [] ;
+private:
+ 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 );
+ char *fname;
};
} //GParted
diff --git a/src/fat16.cc b/src/fat16.cc
index bd0485b..70a8eae 100644
--- a/src/fat16.cc
+++ b/src/fat16.cc
@@ -101,156 +101,246 @@ FS fat16::get_filesystem_support()
return fs ;
}
-void fat16::set_used_sectors( Partition & partition )
+void fat16::set_used_sectors( Partition & partition )
{
- exit_status = Utils::execute_command( "dosfsck -n -v " + partition .get_path(), output, error, true ) ;
+ set_used_sectors( partition, unlock_mutex );
+ mutex.lock();
+}
+
+void fat16::set_used_sectors( Partition & partition, sigc::slot<bool, double> slot )
+{
+ this->slot = slot;
+ execute_command( "dosfsck -n -v " + partition .get_path(),
+ sigc::bind( sigc::mem_fun( *this, &fat16::set_used_sectors2 ),
+ &partition ) );
+}
+
+bool fat16::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 ;
- if ( index < output .length() && sscanf( output .substr( index ) .c_str(), "%Ld/%Ld", &S, &N ) == 2 )
- N -= S ;
+ 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
- N = -1 ;
+ N = -1;
//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 ;
-
+ 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 ( 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 fat16::read_label( Partition & partition )
{
+ read_label( partition, unlock_mutex );
+ mutex.lock();
+}
+
+void fat16::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, &fat16::read_label2 ),
+ &partition ) );
+}
- if ( ! Utils::execute_command( cmd, output, error, true ) )
+bool fat16::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 fat16::write_label( const Partition & partition, OperationDetail & operationdetail )
{
+ write_label( partition, operationdetail, unlock_mutex );
+ mutex.lock();
+ return success;
+}
+
+void fat16::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 ) ) ;
+ cmd = String::ucompose( "sh -c 'MTOOLSRC=%1 mlabel %2:\"%3\"'", fname, dletter, Utils::fat_compliant_label( partition .label ) ) ;
- 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, &fat16::write_label2 ),
+ &operationdetail ) );
+}
+bool fat16::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 fat16::read_uuid( Partition & partition )
{
+ read_uuid( partition, unlock_mutex );
+ mutex.lock();
+}
+
+void fat16::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, &fat16::read_uuid2 ),
+ &partition ) );
+}
- if ( ! Utils::execute_command( cmd, output, error, true ) )
+bool fat16::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();
}
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 );
}
-
- err_msg = Utils::delete_mtoolsrc_file( fname );
+ Utils::delete_mtoolsrc_file( fname );
+ free( fname );
+ fname = 0;
+ return slot( 1.0 );
}
bool fat16::write_uuid( const Partition & partition, OperationDetail & operationdetail )
{
+ write_uuid( partition, operationdetail, unlock_mutex );
+ mutex.lock();
+ return success;
+}
+
+void fat16::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 ) ;
+ Glib::ustring cmd = String::ucompose( "sh -c '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 ) ;
-
- 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, &fat16::write_uuid2 ),
+ &operationdetail ) );
+}
+bool fat16::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 fat16::create( const Partition & new_partition, OperationDetail & operationdetail )
{
- return ! execute_command( "mkdosfs -F16 -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 fat16::create( const Partition & new_partition, OperationDetail & operationdetail,
+ sigc::slot<bool, double> slot )
+{
+ this->slot = slot;
+ execute_command( "mkdosfs -F16 -v -n \"" + Utils::fat_compliant_label( new_partition .label ) +
+ "\" " + new_partition .get_path(),
+ operationdetail,
+ set_success );
+}
+
+void fat16::resize( const Partition & partition_new, OperationDetail & operationdetail,
+ bool fill_partition, sigc::slot<bool, double> slot )
+{
+ success = false;
+ slot( 1.0 );
}
bool fat16::resize( const Partition & partition_new, OperationDetail & operationdetail, bool fill_partition )
@@ -258,6 +348,13 @@ bool fat16::resize( const Partition & partition_new, OperationDetail & operation
return true ;
}
+void fat16::move( const Partition & partition_new, const Partition & partition_old,
+ OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+ success = false;
+ slot( 1.0 );
+}
+
bool fat16::move( const Partition & partition_new
, const Partition & partition_old
, OperationDetail & operationdetail
@@ -266,6 +363,13 @@ bool fat16::move( const Partition & partition_new
return true ;
}
+void fat16::copy( const Glib::ustring & src_part_path, const Glib::ustring & dest_part_path,
+ OperationDetail & operationdetail, sigc::slot<bool, double> slot )
+{
+ success = false;
+ slot( 1.0 );
+}
+
bool fat16::copy( const Glib::ustring & src_part_path,
const Glib::ustring & dest_part_path,
OperationDetail & operationdetail )
@@ -275,9 +379,26 @@ bool fat16::copy( const Glib::ustring & src_part_path,
bool fat16::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 fat16::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, &fat16::check_repair2 ) );
+}
+
+bool fat16::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]