[gparted/psusi/refactor: 4/16] Remove mtoolsrc file



commit ccfa8cf6a92f647cbeb56a28d75857527153e96c
Author: Phillip Susi <psusi ubuntu com>
Date:   Thu Jan 17 21:13:25 2013 -0500

    Remove mtoolsrc file
    
    fat16 and fat32 were creating a temp mtoolsrc file to configure the
    command to reference a drive letter and ignore certain errors.  They have
    been changed to pass this information via the command line and environment
    instead.

 src/Utils.cc |   57 ---------------------------------------------------------
 src/fat16.cc |   57 ++++++++-------------------------------------------------
 src/fat32.cc |   56 ++++++++------------------------------------------------
 3 files changed, 16 insertions(+), 154 deletions(-)
---
diff --git a/src/Utils.cc b/src/Utils.cc
index 76c87f5..e66b1f4 100644
--- a/src/Utils.cc
+++ b/src/Utils.cc
@@ -458,63 +458,6 @@ Glib::ustring Utils::regexp_label( const Glib::ustring & text
 		return "" ;
 }
 
-Glib::ustring Utils::create_mtoolsrc_file( char file_name[], const char drive_letter,
-		const Glib::ustring & device_path )
-{
-	//Create mtools config file
-	//NOTE:  file_name will be changed by the mkstemp() function call.
-	Glib::ustring err_msg = "" ;
-	int fd ;
-	fd = mkstemp( file_name ) ;
-	if( fd != -1 ) {
-		Glib::ustring fcontents =
-			/* TO TRANSLATORS:  # Temporary file created by gparted.  It may be deleted.
-			 * means that this file is only used while gparted is applying operations.
-			 * If for some reason this file exists at any other time, then the message is
-			 * meant to inform a user that the file can be deleted with no harmful effects.
-			 * This file is typically created, exists for less than a few seconds, and is
-			 * then deleted by gparted.  Under normal circumstances a user should never
-			 * see this file.
-			 */
-			_("# Temporary file created by gparted.  It may be deleted.\n") ;
-
-		//The following file contents are mtools keywords (see man mtools.conf)
-		fcontents = String::ucompose(
-						"drive %1: file=\"%2\"\nmtools_skip_check=1\n", drive_letter, device_path
-					);
-
-		if( write( fd, fcontents .c_str(), fcontents .size() ) == -1 ) {
-			err_msg = String::ucompose(
-							/* TO TRANSLATORS: looks like
-							 * Label operation failed:  Unable to write to temporary file /tmp/Y56ZZ3M13LM.
-							 */
-							_("Label operation failed:  Unable to write to temporary file %1.\n")
-							, file_name
-						) ;
-		}
-		close( fd ) ;
-	}
-	else
-	{
-		err_msg = String::ucompose(
-						/* TO TRANSLATORS: looks like
-						 * Label operation failed:  Unable to create temporary file /tmp/Y56ZZ3M13LM.
-						 */
-						_("Label operation failed:  Unable to create temporary file %1.\n")
-						, file_name
-					) ;
-	}
-	return err_msg ;
-}
-
-Glib::ustring Utils::delete_mtoolsrc_file( const char file_name[] )
-{
-	//Delete mtools config file
-	Glib::ustring err_msg = "" ;
-	remove( file_name ) ;
-	return err_msg ;
-}
-
 Glib::ustring Utils::trim( const Glib::ustring & src, const Glib::ustring & c /* = " \t\r\n" */ )
 {
 	//Trim leading and trailing whitespace from string
diff --git a/src/fat16.cc b/src/fat16.cc
index 6b85b56..9e74de3 100644
--- a/src/fat16.cc
+++ b/src/fat16.cc
@@ -63,6 +63,9 @@ FS fat16::get_filesystem_support()
 	FS fs ;
 	fs .filesystem = GParted::FS_FAT16 ;
 		
+	// hack to disable silly mtools warnings
+	setenv( "MTOOLS_SKIP_CHECK", "1", 0 );
+
 	//find out if we can create fat16 file systems
 	if ( ! Glib::find_program_in_path( "mkdosfs" ) .empty() )
 		fs .create = GParted::FS::EXTERNAL ;
@@ -150,17 +153,7 @@ void fat16::set_used_sectors( Partition & partition )
 
 void fat16::read_label( Partition & partition )
 {
-	//Create mtools config file
-	char fname[] = "/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 ) ;
-
-	if ( ! Utils::execute_command( cmd, output, error, true ) )
+	if ( ! Utils::execute_command( "mlabel -s :: -i " + partition.get_path(), output, error, true ) )
 	{
 		partition .set_label( Utils::trim( Utils::regexp_label( output, "Volume label is ([^(]*)" ) ) ) ;
 	}
@@ -172,25 +165,15 @@ void fat16::read_label( Partition & partition )
 		if ( ! error .empty() )
 			partition .messages .push_back( error ) ;
 	}
-	
-	//Delete mtools config file
-	err_msg = Utils::delete_mtoolsrc_file( fname );
 }
 
 bool fat16::write_label( const Partition & partition, OperationDetail & operationdetail )
 {
-	//Create mtools config file
-	char fname[] = "/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 .get_label() .empty() )
-		cmd = String::ucompose( "export MTOOLSRC=%1 && mlabel -c %2:", fname, dletter ) ;
+		cmd = "mlabel -c :: -i " + partition.get_path();
 	else
-		cmd = String::ucompose( "export MTOOLSRC=%1 && mlabel %2:\"%3\"",
-		                        fname, dletter, partition .get_label() ) ;
+		cmd = "mlabel ::\"" + partition.get_label() + "\" -i " + partition.get_path();
 
 	operationdetail .add_child( OperationDetail( cmd, STATUS_NONE, FONT_BOLD_ITALIC ) ) ;
 
@@ -202,23 +185,12 @@ bool fat16::write_label( const Partition & partition, OperationDetail & operatio
 	if ( ! error .empty() )
 		operationdetail .get_last_child() .add_child( OperationDetail( error, STATUS_NONE, FONT_ITALIC ) ) ;
 
-	//Delete mtools config file
-	err_msg = Utils::delete_mtoolsrc_file( fname );
-
 	return ( exit_status == 0 );
 }
 
 void fat16::read_uuid( Partition & partition )
 {
-	//Create mtools config file
-	char fname[] = "/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 = "mdir -f :: -i " + partition.get_path();
 
 	if ( ! Utils::execute_command( cmd, output, error, true ) )
 	{
@@ -234,21 +206,11 @@ void fat16::read_uuid( Partition & partition )
 		if ( ! error .empty() )
 			partition .messages .push_back( error ) ;
 	}
-
-	err_msg = Utils::delete_mtoolsrc_file( fname );
 }
 
 bool fat16::write_uuid( const Partition & partition, OperationDetail & operationdetail )
 {
-	//Create mtools config file
-	char fname[] = "/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 = "mlabel -s -n :: -i " + partition.get_path();
 
 	operationdetail .add_child( OperationDetail( cmd, STATUS_NONE, FONT_BOLD_ITALIC ) ) ;
 
@@ -260,9 +222,6 @@ bool fat16::write_uuid( const Partition & partition, OperationDetail & operation
 	if ( ! error .empty() )
 		operationdetail .get_last_child() .add_child( OperationDetail( error, STATUS_NONE, FONT_ITALIC ) ) ;
 
-	//Delete mtools config file
-	err_msg = Utils::delete_mtoolsrc_file( fname );
-
 	return ( exit_status == 0 );
 }
 
diff --git a/src/fat32.cc b/src/fat32.cc
index 3f4c0e5..1a1d4f4 100644
--- a/src/fat32.cc
+++ b/src/fat32.cc
@@ -50,7 +50,9 @@ FS fat32::get_filesystem_support()
 {
 	FS fs ;
 	fs .filesystem = GParted::FS_FAT32 ;
-		
+
+	// hack to disable silly mtools warnings
+	setenv( "MTOOLS_SKIP_CHECK", "1", 0 );
 	//find out if we can create fat32 file systems
 	if ( ! Glib::find_program_in_path( "mkdosfs" ) .empty() )
 		fs .create = GParted::FS::EXTERNAL ;
@@ -138,15 +140,7 @@ void fat32::set_used_sectors( Partition & partition )
 
 void fat32::read_label( Partition & partition )
 {
-	//Create mtools config file
-	char fname[] = "/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 = "mlabel -s :: -i " + partition.get_path();
 
 	if ( ! Utils::execute_command( cmd, output, error, true ) )
 	{
@@ -160,25 +154,15 @@ void fat32::read_label( Partition & partition )
 		if ( ! error .empty() )
 			partition .messages .push_back( error ) ;
 	}
-	
-	//Delete mtools config file
-	err_msg = Utils::delete_mtoolsrc_file( fname );
 }
 
 bool fat32::write_label( const Partition & partition, OperationDetail & operationdetail )
 {
-	//Create mtools config file
-	char fname[] = "/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 .get_label() .empty() )
-		cmd = String::ucompose( "export MTOOLSRC=%1 && mlabel -c %2:", fname, dletter ) ;
+		cmd = "mlabel -c :: -i " + partition.get_path();
 	else
-		cmd = String::ucompose( "export MTOOLSRC=%1 && mlabel %2:\"%3\"",
-		                        fname, dletter, partition .get_label() ) ;
+		cmd = "mlabel ::\"" + partition.get_label() + "\" -i " + partition.get_path();
 
 	operationdetail .add_child( OperationDetail( cmd, STATUS_NONE, FONT_BOLD_ITALIC ) ) ;
 
@@ -190,23 +174,12 @@ bool fat32::write_label( const Partition & partition, OperationDetail & operatio
 	if ( ! error .empty() )
 		operationdetail .get_last_child() .add_child( OperationDetail( error, STATUS_NONE, FONT_ITALIC ) ) ;
 
-	//Delete mtools config file
-	err_msg = Utils::delete_mtoolsrc_file( fname );
-
 	return ( exit_status == 0 );
 }
 
 void fat32::read_uuid( Partition & partition )
 {
-	//Create mtools config file
-	char fname[] = "/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 = "mdir -f :: -i " + partition.get_path();
 
 	if ( ! Utils::execute_command( cmd, output, error, true ) )
 	{
@@ -222,22 +195,12 @@ void fat32::read_uuid( Partition & partition )
 		if ( ! error .empty() )
 			partition .messages .push_back( error ) ;
 	}
-
-	err_msg = Utils::delete_mtoolsrc_file( fname );
 }
 
 
 bool fat32::write_uuid( const Partition & partition, OperationDetail & operationdetail )
 {
-	//Create mtools config file
-	char fname[] = "/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 = "mlabel -s -n :: -i " + partition.get_path();
 
 	operationdetail .add_child( OperationDetail( cmd, STATUS_NONE, FONT_BOLD_ITALIC ) ) ;
 
@@ -249,9 +212,6 @@ bool fat32::write_uuid( const Partition & partition, OperationDetail & operation
 	if ( ! error .empty() )
 		operationdetail .get_last_child() .add_child( OperationDetail( error, STATUS_NONE, FONT_ITALIC ) ) ;
 
-	//Delete mtools config file
-	err_msg = Utils::delete_mtoolsrc_file( fname );
-
 	return ( exit_status == 0 );
 }
 



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