:spawn_sync instead of popen(). also made the filesystems call

* removed Execute_Command() and replaced it by execute_command().
  The latter uses Glib::spawn_sync instead of popen().
  also made the filesystems call execute_command rather than calling
  Glib::spawn_sync themselves.
This commit is contained in:
Bart Hakvoort 2006-01-22 13:23:58 +00:00
parent aad09e9046
commit 045edbbe95
12 changed files with 151 additions and 215 deletions

View File

@ -1,3 +1,10 @@
2006-01-22 Bart Hakvoort <hakvoort@cvs.gnome.org>
* removed Execute_Command() and replaced it by execute_command().
The latter uses Glib::spawn_sync instead of popen().
also made the filesystems call execute_command rather than calling
Glib::spawn_sync themselves.
2006-01-22 Bart Hakvoort <hakvoort@cvs.gnome.org>
* implemented Utils::sector_to_unit() and use it in several places

View File

@ -47,18 +47,17 @@ public:
long cylinder_size ; //see GParted_Core::Resize()
protected:
int Execute_Command( Glib::ustring command ) ;
int execute_command( std::vector<std::string> argv, std::vector<OperationDetails> & operation_details ) ;
int execute_command( std::vector<std::string> argv, std::string & output ) ;
//those are used in several Set_Used_Sectors()..
std::vector<std::string> argv, envp ;
//those are used in several places..
std::vector<std::string> argv ;
std::string output, error ;
Sector N, S ;
unsigned int index ;
int exit_status ;
private:
Glib::ustring cmd_output ;
};

View File

@ -26,28 +26,6 @@ FileSystem::FileSystem()
cylinder_size = 0 ;
}
int FileSystem::Execute_Command( Glib::ustring command )
{
//stderr to stdout
//command += " 2>&1" ;
std::cout << command << std::endl ;
cmd_output = command + "\n\n" ;
char c_buf[ 512 ] ;
FILE *f = popen( command .c_str( ), "r" ) ;
while ( fgets( c_buf, 512, f ) )
{
//output = Glib::locale_to_utf8( c_buf ) ;
//std::cout << output << std::endl ;
}
cmd_output = "" ;
return pclose( f ) ;
}
int FileSystem::execute_command( std::vector<std::string> argv, std::vector<OperationDetails> & operation_details )
{
Glib::ustring temp ;
@ -56,14 +34,10 @@ int FileSystem::execute_command( std::vector<std::string> argv, std::vector<Oper
operation_details .push_back( OperationDetails( temp, OperationDetails::NONE ) ) ;
envp .clear() ;
envp .push_back( "LC_ALL=C" ) ;
try
{
Glib::spawn_sync( ".",
argv,
envp,
Glib::SPAWN_SEARCH_PATH,
sigc::slot< void >(),
&output,
@ -87,4 +61,28 @@ int FileSystem::execute_command( std::vector<std::string> argv, std::vector<Oper
return exit_status ;
}
int FileSystem::execute_command( std::vector<std::string> argv, std::string & output )
{
std::vector<std::string> envp ;
envp .push_back( "LC_ALL=C" ) ;
try
{
Glib::spawn_sync( ".",
argv,
envp,
Glib::SPAWN_SEARCH_PATH,
sigc::slot<void>(),
&output,
&error, //dummy
&exit_status) ;
}
catch ( Glib::Exception & e )
{
std::cout << e .what() << std::endl ;
}
return exit_status ;
}
} //GParted

View File

@ -55,28 +55,21 @@ void ext2::Set_Used_Sectors( Partition & partition )
argv .push_back( "-h" ) ;
argv .push_back( partition .partition ) ;
envp .push_back( "LC_ALL=C" ) ;
try
if ( ! execute_command( argv, output ) )
{
Glib::spawn_sync( ".", argv, envp, Glib::SPAWN_SEARCH_PATH, sigc::slot< void >(), &output ) ;
}
catch ( Glib::Exception & e )
{
std::cout << e .what() << std::endl ;
return ;
}
index = output .find( "Free blocks:" ) ;
if ( index >= output .length() || sscanf( output.substr( index ) .c_str(), "Free blocks: %Ld", &N ) != 1 )
N = -1 ;
index = output .find( "Free blocks:" ) ;
if ( index >= output .length() ||
sscanf( output.substr( index ) .c_str(), "Free blocks: %Ld", &N ) != 1 )
N = -1 ;
index = output .find( "Block size:" ) ;
if ( index >= output.length() || sscanf( output.substr( index ) .c_str(), "Block size: %Ld", &S ) != 1 )
S = -1 ;
index = output .find( "Block size:" ) ;
if ( index >= output.length() ||
sscanf( output.substr( index ) .c_str(), "Block size: %Ld", &S ) != 1 )
S = -1 ;
if ( N > -1 && S > -1 )
partition .Set_Unused( N * S / 512 ) ;
if ( N > -1 && S > -1 )
partition .Set_Unused( Utils::Round( N * ( S / 512.0 ) ) ) ;
}
}
bool ext2::Create( const Partition & new_partition, std::vector<OperationDetails> & operation_details )

View File

@ -56,30 +56,23 @@ void ext3::Set_Used_Sectors( Partition & partition )
argv .push_back( "-h" ) ;
argv .push_back( partition .partition ) ;
envp .push_back( "LC_ALL=C" ) ;
try
if ( ! execute_command( argv, output ) )
{
Glib::spawn_sync( ".", argv, envp, Glib::SPAWN_SEARCH_PATH, sigc::slot< void >(), &output ) ;
}
catch ( Glib::Exception & e )
{
std::cout << e .what() << std::endl ;
return ;
}
index = output .find( "Free blocks:" ) ;
if ( index >= output .length() ||
sscanf( output.substr( index ) .c_str(), "Free blocks: %Ld", &N ) != 1 )
N = -1 ;
index = output .find( "Block size:" ) ;
if ( index >= output.length() ||
sscanf( output.substr( index ) .c_str(), "Block size: %Ld", &S ) != 1 )
S = -1 ;
index = output .find( "Free blocks:" ) ;
if ( index >= output .length() || sscanf( output.substr( index ) .c_str(), "Free blocks: %Ld", &N ) != 1 )
N = -1 ;
index = output .find( "Block size:" ) ;
if ( index >= output.length() || sscanf( output.substr( index ) .c_str(), "Block size: %Ld", &S ) != 1 )
S = -1 ;
if ( N > -1 && S > -1 )
partition .Set_Unused( N * S / 512 ) ;
if ( N > -1 && S > -1 )
partition .Set_Unused( Utils::Round( N * ( S / 512.0 ) ) ) ;
}
}
bool ext3::Create( const Partition & new_partition, std::vector<OperationDetails> & operation_details )
{
operation_details .push_back( OperationDetails( String::ucompose(

View File

@ -58,32 +58,23 @@ void fat16::Set_Used_Sectors( Partition & partition )
argv .push_back( "-v" ) ;
argv .push_back( partition .partition ) ;
envp .push_back( "LC_ALL=C" ) ;
try
if ( 1 >= execute_command( argv, output ) >= 0 )
{
Glib::spawn_sync( ".", argv, envp, Glib::SPAWN_SEARCH_PATH, sigc::slot< void >(), &output ) ;
//free clusters
index = output .find( ",", output .find( partition .partition ) + partition .partition .length() ) +1 ;
if ( index < output .length() && sscanf( output .substr( index ) .c_str(), "%Ld/%Ld", &S, &N ) == 2 )
N -= S ;
else
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 ;
if ( N > -1 && S > -1 )
partition .Set_Unused( Utils::Round( N * ( S / 512.0 ) ) ) ;
}
catch ( Glib::Exception & e )
{
std::cout << e .what() << std::endl ;
return ;
}
//free clusters
index = output .find( ",", output .find( partition .partition ) + partition .partition .length() ) +1 ;
if ( index < output .length() && sscanf( output .substr( index ) .c_str(), "%Ld/%Ld", &S, &N ) == 2 )
N -= S ;
else
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 ;
if ( N > -1 && S > -1 )
partition .Set_Unused( N * S / 512 ) ;
}
bool fat16::Create( const Partition & new_partition, std::vector<OperationDetails> & operation_details )

View File

@ -57,32 +57,23 @@ void fat32::Set_Used_Sectors( Partition & partition )
argv .push_back( "-v" ) ;
argv .push_back( partition .partition ) ;
envp .push_back( "LC_ALL=C" ) ;
try
if ( 1 >= execute_command( argv, output ) >= 0 )
{
Glib::spawn_sync( ".", argv, envp, Glib::SPAWN_SEARCH_PATH, sigc::slot< void >(), &output ) ;
//free clusters
index = output .find( ",", output .find( partition .partition ) + partition .partition .length() ) +1 ;
if ( index < output .length() && sscanf( output .substr( index ) .c_str(), "%Ld/%Ld", &S, &N ) == 2 )
N -= S ;
else
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 ;
if ( N > -1 && S > -1 )
partition .Set_Unused( Utils::Round( N * ( S / 512.0 ) ) ) ;
}
catch ( Glib::Exception & e )
{
std::cout << e .what() << std::endl ;
return ;
}
//free clusters
index = output .find( ",", output .find( partition .partition ) + partition .partition .length() ) +1 ;
if ( index < output .length() && sscanf( output .substr( index ) .c_str(), "%Ld / %Ld", &S, &N ) == 2 )
N -= S ;
else
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 ;
if ( N > -1 && S > -1 )
partition .Set_Unused( N * S / 512 ) ;
}
bool fat32::Create( const Partition & new_partition, std::vector<OperationDetails> & operation_details )

View File

@ -65,32 +65,27 @@ FS jfs::get_filesystem_support( )
void jfs::Set_Used_Sectors( Partition & partition )
{
char c_buf[ 512 ] ;
FILE *f ;
Glib::ustring output ;
argv .push_back( "sh" ) ;
argv .push_back( "-c" ) ;
argv .push_back( "echo dm | jfs_debugfs " + partition .partition ) ;
//get free sectors..
f = popen( ( "echo dm | LC_ALL=C jfs_debugfs " + partition .partition + " | grep dn_nfree" ) .c_str( ), "r" ) ;
while ( fgets( c_buf, 512, f ) )
if ( ! execute_command( argv, output ) )
{
output = Glib::locale_to_utf8( c_buf ) ;
//blocksize
index = output .find( "Block Size:" ) ;
if ( index >= output .length() ||
sscanf( output .substr( index ) .c_str(), "Block Size: %Ld", &S ) != 1 )
S = -1 ;
//free sectors
if ( output .find( "dn_nfree" ) < output .length( ) )
{
output = output .substr( output .find( ":" ) +1, output .length( ) ) ;
int dec_free_blocks ;
std::istringstream hex_free_blocks( output .substr( 0, output .find( "[" ) ) );
hex_free_blocks >> std::hex >> dec_free_blocks ;
partition .Set_Unused( dec_free_blocks * 8 ) ;//4096 / 512 (i've been told jfs blocksize is _always_ 4K)
break ;
}
//free blocks
index = output .find( "dn_nfree:" ) ;
if ( index >= output .length() ||
sscanf( output .substr( index ) .c_str(), "dn_nfree: %Lx", &N ) != 1 )
N = -1 ;
if ( S > -1 && N > -1 )
partition .Set_Unused( Utils::Round( N * ( S / 512.0 ) ) ) ;
}
pclose( f ) ;
}
bool jfs::Create( const Partition & new_partition, std::vector<OperationDetails> & operation_details )

View File

@ -57,24 +57,16 @@ void ntfs::Set_Used_Sectors( Partition & partition )
argv .push_back( "--force" ) ;
argv .push_back( partition .partition ) ;
envp .push_back( "LC_ALL=C" ) ;
try
if ( ! execute_command( argv, output ) )
{
Glib::spawn_sync( ".", argv, envp, Glib::SPAWN_SEARCH_PATH, sigc::slot< void >(), &output ) ;
index = output .find( "sectors of free space" ) ;
if ( index >= output .length() ||
sscanf( output .substr( index ) .c_str(), "sectors of free space : %Ld", &N ) != 1 )
N = -1 ;
if ( N > -1 )
partition .Set_Unused( N ) ;
}
catch ( Glib::Exception & e )
{
std::cout << e .what() << std::endl ;
return ;
}
index = output .find( "sectors of free space" ) ;
if ( index >= output .length() || sscanf( output .substr( index ) .c_str(), "sectors of free space : %Ld", &N ) != 1 )
N = -1 ;
if ( N > -1 )
partition .Set_Unused( N ) ;
}
bool ntfs::Create( const Partition & new_partition, std::vector<OperationDetails> & operation_details )

View File

@ -49,28 +49,21 @@ void reiser4::Set_Used_Sectors( Partition & partition )
argv .push_back( "debugfs.reiser4" ) ;
argv .push_back( partition .partition ) ;
envp .push_back( "LC_ALL=C" ) ;
try
if ( ! execute_command( argv, output ) )
{
Glib::spawn_sync( ".", argv, envp, Glib::SPAWN_SEARCH_PATH, sigc::slot< void >(), &output ) ;
}
catch ( Glib::Exception & e )
{
std::cout << e .what() << std::endl ;
return ;
}
index = output .find( "free blocks" ) ;
if ( index >= output .length() || sscanf( output.substr( index ) .c_str(), "free blocks: %Ld", &N ) != 1 )
N = -1 ;
index = output .find( "free blocks" ) ;
if ( index >= output .length() ||
sscanf( output.substr( index ) .c_str(), "free blocks: %Ld", &N ) != 1 )
N = -1 ;
index = output .find( "blksize" ) ;
if ( index >= output.length() || sscanf( output.substr( index ) .c_str(), "blksize: %Ld", &S ) != 1 )
S = -1 ;
index = output .find( "blksize" ) ;
if ( index >= output.length() ||
sscanf( output.substr( index ) .c_str(), "blksize: %Ld", &S ) != 1 )
S = -1 ;
if ( N > -1 && S > -1 )
partition .Set_Unused( N * S / 512 ) ;
if ( N > -1 && S > -1 )
partition .Set_Unused( Utils::Round( N * ( S / 512.0 ) ) ) ;
}
}
bool reiser4::Create( const Partition & new_partition, std::vector<OperationDetails> & operation_details )

View File

@ -59,28 +59,21 @@ void reiserfs::Set_Used_Sectors( Partition & partition )
argv .push_back( "debugreiserfs" ) ;
argv .push_back( partition .partition ) ;
envp .push_back( "LC_ALL=C" ) ;
try
if ( ! execute_command( argv, output ) )
{
Glib::spawn_sync( ".", argv, envp, Glib::SPAWN_SEARCH_PATH, sigc::slot< void >(), &output ) ;
index = output .find( "Blocksize" ) ;
if ( index >= output .length() ||
sscanf( output .substr( index ) .c_str(), "Blocksize: %Ld", &S ) != 1 )
S = -1 ;
index = output .find( ":", output .find( "Free blocks" ) ) +1 ;
if ( index >= output .length() ||
sscanf( output .substr( index ) .c_str(), "%Ld", &N ) != 1 )
N = -1 ;
if ( N > -1 && S > -1 )
partition .Set_Unused( Utils::Round( N * ( S / 512.0 ) ) ) ;
}
catch ( Glib::Exception & e )
{
std::cout << e .what() << std::endl ;
return ;
}
index = output .find( "Blocksize" ) ;
if ( index >= output .length() || sscanf( output .substr( index ) .c_str(), "Blocksize: %Ld", &S ) != 1 )
S = -1 ;
index = output .find( ":", output .find( "Free blocks" ) ) +1 ;
if ( index >= output .length() || sscanf( output .substr( index ) .c_str(), "%Ld", &N ) != 1 )
N = -1 ;
if ( N > -1 && S > -1 )
partition .Set_Unused( N * S / 512 ) ;
}
bool reiserfs::Create( const Partition & new_partition, std::vector<OperationDetails> & operation_details )

View File

@ -71,29 +71,20 @@ void xfs::Set_Used_Sectors( Partition & partition )
argv .push_back( "-r" ) ;
argv .push_back( partition .partition ) ;
envp .push_back( "LC_ALL=C" ) ;
try
if ( ! execute_command( argv, output ) )
{
Glib::spawn_sync( ".", argv, envp, Glib::SPAWN_SEARCH_PATH, sigc::slot< void >(), &output ) ;
}
catch ( Glib::Exception & e )
{
std::cout << e .what() << std::endl ;
return ;
}
//blocksize
if ( sscanf( output .c_str(), "blocksize = %Ld", &S ) != 1 )
S = -1 ;
//blocksize
if ( sscanf( output .c_str(), "blocksize = %Ld", &S ) != 1 )
S = -1 ;
//free blocks
output = output .substr( output .find( "fdblocks" ) ) ;
if ( sscanf( output .c_str(), "fdblocks = %Ld", &N ) != 1 )
N = -1 ;
//free blocks
output = output .substr( output .find( "fdblocks" ) ) ;
if ( sscanf( output .c_str(), "fdblocks = %Ld", &N ) != 1 )
N = -1 ;
if ( N > -1 && S > -1 )
partition .Set_Unused( N * S / 512 ) ;
if ( N > -1 && S > -1 )
partition .Set_Unused( Utils::Round( N * ( S / 512.0 ) ) ) ;
}
}
bool xfs::Create( const Partition & new_partition, std::vector<OperationDetails> & operation_details )