:spawn_command_line_sync() + general cleanups in the set_used_sectors()

* replaced popen() with Glib::spawn_command_line_sync() + general
  cleanups in the set_used_sectors() functions.
This commit is contained in:
Bart Hakvoort 2005-12-28 16:43:06 +00:00
parent 963b916c44
commit 23aa549c7c
12 changed files with 192 additions and 175 deletions

View File

@ -1,3 +1,8 @@
2005-12-28 Bart Hakvoort <hakvoort@cvs.gnome.org>
* replaced popen() with Glib::spawn_command_line_sync() + general
cleanups in the set_used_sectors() functions.
2005-12-25 Bart Hakvoort <hakvoort@cvs.gnome.org> 2005-12-25 Bart Hakvoort <hakvoort@cvs.gnome.org>
* merry christmas * merry christmas

View File

@ -51,8 +51,10 @@ public:
protected: protected:
int Execute_Command( Glib::ustring command ) ; int Execute_Command( Glib::ustring command ) ;
//those are used in several Set_Used_Sectors()..
std::vector<std::string> argv, envp ;
std::string output ; std::string output ;
Sector free_blocks, blocksize ; Sector N, S ;
unsigned int index ; unsigned int index ;
private: private:

View File

@ -34,7 +34,7 @@ public:
VBox_VisualDisk(); VBox_VisualDisk();
~VBox_VisualDisk(); ~VBox_VisualDisk();
void load_partitions( const std::vector<Partition> & partitions, const Sector device_length ); void load_partitions( const std::vector<Partition> & partitions, Sector device_length );
void set_selected( const Partition & partition ) ; void set_selected( const Partition & partition ) ;
void clear() ; void clear() ;
@ -108,6 +108,7 @@ private:
selected = false ; selected = false ;
pango_layout .clear() ; pango_layout .clear() ;
logicals .clear() ;
} }
~visual_partition() ~visual_partition()

View File

@ -59,7 +59,7 @@ VBox_VisualDisk::VBox_VisualDisk()
this ->pack_start( hbox_legend ); this ->pack_start( hbox_legend );
} }
void VBox_VisualDisk::load_partitions( const std::vector<Partition> & partitions, const Sector device_length ) void VBox_VisualDisk::load_partitions( const std::vector<Partition> & partitions, Sector device_length )
{ {
clear() ; clear() ;
@ -452,7 +452,7 @@ void VBox_VisualDisk::free_colors( std::vector<visual_partition> & visual_partit
{ {
for ( unsigned int t = 0 ; t < visual_partitions .size() ; t++ ) for ( unsigned int t = 0 ; t < visual_partitions .size() ; t++ )
{ {
this ->get_colormap( ) ->free_color( visual_partitions[ t ] .color ) ; this ->get_colormap() ->free_color( visual_partitions[ t ] .color ) ;
if ( visual_partitions[ t ] .logicals .size() > 0 ) if ( visual_partitions[ t ] .logicals .size() > 0 )
free_colors( visual_partitions[ t ] .logicals ) ; free_colors( visual_partitions[ t ] .logicals ) ;
@ -464,9 +464,9 @@ VBox_VisualDisk::~VBox_VisualDisk()
clear() ; clear() ;
//free the allocated colors //free the allocated colors
this ->get_colormap( ) ->free_colors( color_used, 1 ) ; this ->get_colormap() ->free_colors( color_used, 1 ) ;
this ->get_colormap( ) ->free_colors( color_unused, 1 ) ; this ->get_colormap() ->free_colors( color_unused, 1 ) ;
this ->get_colormap( ) ->free_colors( color_text, 1 ) ; this ->get_colormap() ->free_colors( color_text, 1 ) ;
} }
} //GParted } //GParted

View File

@ -52,9 +52,15 @@ FS ext2::get_filesystem_support( )
void ext2::Set_Used_Sectors( Partition & partition ) void ext2::Set_Used_Sectors( Partition & partition )
{ {
argv .push_back( "dumpe2fs" ) ;
argv .push_back( "-h" ) ;
argv .push_back( partition .partition ) ;
envp .push_back( "LC_ALL=C" ) ;
try try
{ {
Glib::spawn_command_line_sync("dumpe2fs -h " + partition .partition, &output ) ; Glib::spawn_sync( ".", argv, envp, Glib::SPAWN_SEARCH_PATH, sigc::slot< void >(), &output ) ;
} }
catch ( Glib::Exception & e ) catch ( Glib::Exception & e )
{ {
@ -63,25 +69,15 @@ void ext2::Set_Used_Sectors( Partition & partition )
} }
index = output .find( "Free blocks:" ) ; index = output .find( "Free blocks:" ) ;
if ( index < output .length() ) if ( index >= output .length() || sscanf( output.substr( index ) .c_str(), "Free blocks: %Ld", &N ) != 1 )
{ N = -1 ;
output = output.substr( index );
free_blocks = atol( output .substr( 13, output .find( "\n" ) - 13 ) .c_str() ) ;
}
else
free_blocks = -1 ;
index = output .find( "Block size:" ) ; index = output .find( "Block size:" ) ;
if ( index < output.length() ) if ( index >= output.length() || sscanf( output.substr( index ) .c_str(), "Block size: %Ld", &S ) != 1 )
{ S = -1 ;
output = output.substr( index );
blocksize = atol( output .substr( 12, output .find( "\n" ) - 12 ) .c_str() ) ;
}
else
blocksize = -1 ;
if ( free_blocks > -1 && blocksize > -1 ) if ( N > -1 && S > -1 )
partition .Set_Unused( free_blocks * blocksize / 512 ) ; partition .Set_Unused( N * S / 512 ) ;
} }
bool ext2::Create( const Partition & new_partition ) bool ext2::Create( const Partition & new_partition )

View File

@ -52,9 +52,15 @@ FS ext3::get_filesystem_support( )
void ext3::Set_Used_Sectors( Partition & partition ) void ext3::Set_Used_Sectors( Partition & partition )
{ {
argv .push_back( "dumpe2fs" ) ;
argv .push_back( "-h" ) ;
argv .push_back( partition .partition ) ;
envp .push_back( "LC_ALL=C" ) ;
try try
{ {
Glib::spawn_command_line_sync("dumpe2fs -h " + partition .partition, &output ) ; Glib::spawn_sync( ".", argv, envp, Glib::SPAWN_SEARCH_PATH, sigc::slot< void >(), &output ) ;
} }
catch ( Glib::Exception & e ) catch ( Glib::Exception & e )
{ {
@ -63,25 +69,15 @@ void ext3::Set_Used_Sectors( Partition & partition )
} }
index = output .find( "Free blocks:" ) ; index = output .find( "Free blocks:" ) ;
if ( index < output .length() ) if ( index >= output .length() || sscanf( output.substr( index ) .c_str(), "Free blocks: %Ld", &N ) != 1 )
{ N = -1 ;
output = output.substr( index );
free_blocks = atol( output .substr( 13, output .find( "\n" ) - 13 ) .c_str() ) ;
}
else
free_blocks = -1 ;
index = output .find( "Block size:" ) ; index = output .find( "Block size:" ) ;
if ( index < output.length() ) if ( index >= output.length() || sscanf( output.substr( index ) .c_str(), "Block size: %Ld", &S ) != 1 )
{ S = -1 ;
output = output.substr( index );
blocksize = atol( output .substr( 12, output .find( "\n" ) - 12 ) .c_str() ) ; if ( N > -1 && S > -1 )
} partition .Set_Unused( N * S / 512 ) ;
else
blocksize = -1 ;
if ( free_blocks > -1 && blocksize > -1 )
partition .Set_Unused( free_blocks * blocksize / 512 ) ;
} }
bool ext3::Create( const Partition & new_partition ) bool ext3::Create( const Partition & new_partition )

View File

@ -53,33 +53,36 @@ FS fat16::get_filesystem_support( )
void fat16::Set_Used_Sectors( Partition & partition ) void fat16::Set_Used_Sectors( Partition & partition )
{ {
char c_buf[ 512 ] ; argv .push_back( "dosfsck" ) ;
FILE *f ; argv .push_back( "-v" ) ;
argv .push_back( partition .partition ) ;
Glib::ustring output ;
Sector free_clusters = -1, bytes_per_cluster = -1 ;
//get free blocks.. envp .push_back( "LC_ALL=C" ) ;
f = popen( ( "LC_ALL=C dosfsck -v " + partition .partition ) .c_str( ), "r" ) ;
while ( fgets( c_buf, 512, f ) )
{
output = Glib::locale_to_utf8( c_buf ) ;
//bytes per cluster
if ( output .find( "bytes per cluster" ) < output .length( ) )
bytes_per_cluster = atol( output .substr( 0, output .find( "b" ) ) .c_str( ) ) ;
//free clusters
if ( output .find( partition .partition ) < output .length( ) )
{
output = output .substr( output .find( "," ) +2, output .length( ) ) ;
free_clusters = atol( output .substr( output .find( "/" ) +1, output .find( " " ) ) .c_str( ) ) - atol( output .substr( 0, output .find( "/" ) ) .c_str( ) ) ;
}
}
pclose( f ) ;
if ( free_clusters > -1 && bytes_per_cluster > -1 ) try
partition .Set_Unused( free_clusters * bytes_per_cluster / 512 ) ; {
Glib::spawn_sync( ".", argv, envp, Glib::SPAWN_SEARCH_PATH, sigc::slot< void >(), &output ) ;
}
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 ) bool fat16::Create( const Partition & new_partition )

View File

@ -52,33 +52,36 @@ FS fat32::get_filesystem_support( )
void fat32::Set_Used_Sectors( Partition & partition ) void fat32::Set_Used_Sectors( Partition & partition )
{ {
char c_buf[ 512 ] ; argv .push_back( "dosfsck" ) ;
FILE *f ; argv .push_back( "-v" ) ;
argv .push_back( partition .partition ) ;
Glib::ustring output ;
Sector free_clusters = -1, bytes_per_cluster = -1 ;
//get free blocks.. envp .push_back( "LC_ALL=C" ) ;
f = popen( ( "LC_ALL=C dosfsck -v " + partition .partition ) .c_str( ), "r" ) ;
while ( fgets( c_buf, 512, f ) )
{
output = Glib::locale_to_utf8( c_buf ) ;
//bytes per cluster
if ( output .find( "bytes per cluster" ) < output .length( ) )
bytes_per_cluster = atol( output .substr( 0, output .find( "b" ) ) .c_str( ) ) ;
//free clusters
if ( output .find( partition .partition ) < output .length( ) )
{
output = output .substr( output .find( "," ) +2, output .length( ) ) ;
free_clusters = atol( output .substr( output .find( "/" ) +1, output .find( " " ) ) .c_str( ) ) - atol( output .substr( 0, output .find( "/" ) ) .c_str( ) ) ;
}
}
pclose( f ) ;
if ( free_clusters > -1 && bytes_per_cluster > -1 ) try
partition .Set_Unused( free_clusters * bytes_per_cluster / 512 ) ; {
Glib::spawn_sync( ".", argv, envp, Glib::SPAWN_SEARCH_PATH, sigc::slot< void >(), &output ) ;
}
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 ) bool fat32::Create( const Partition & new_partition )

View File

@ -53,25 +53,28 @@ FS ntfs::get_filesystem_support( )
void ntfs::Set_Used_Sectors( Partition & partition ) void ntfs::Set_Used_Sectors( Partition & partition )
{ {
char c_buf[ 512 ] ; argv .push_back( "ntfscluster" ) ;
FILE *f ; argv .push_back( "--force" ) ;
argv .push_back( partition .partition ) ;
Glib::ustring output ;
//get free sectors.. envp .push_back( "LC_ALL=C" ) ;
f = popen( ( "LC_ALL=C ntfscluster --force " + partition .partition ) .c_str( ), "r" ) ;
while ( fgets( c_buf, 512, f ) ) try
{ {
output = Glib::locale_to_utf8( c_buf ) ; Glib::spawn_sync( ".", argv, envp, Glib::SPAWN_SEARCH_PATH, sigc::slot< void >(), &output ) ;
//free sectors
if ( output .find( "sectors of free space" ) < output .length( ) )
{
partition .Set_Unused( atol( (output .substr( output .find( ":" ) +1, output .length( ) ) ) .c_str( ) ) ) ;
break ;
}
} }
pclose( f ) ; 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 ) bool ntfs::Create( const Partition & new_partition )

View File

@ -46,30 +46,31 @@ FS reiser4::get_filesystem_support( )
void reiser4::Set_Used_Sectors( Partition & partition ) void reiser4::Set_Used_Sectors( Partition & partition )
{ {
char c_buf[ 512 ] ; argv .push_back( "debugfs.reiser4" ) ;
FILE *f ; argv .push_back( partition .partition ) ;
Glib::ustring output ;
Sector free_blocks = -1, blocksize = -1 ;
//get free blocks.. envp .push_back( "LC_ALL=C" ) ;
f = popen( ( "LC_ALL=C debugfs.reiser4 " + partition .partition ) .c_str( ), "r" ) ;
while ( fgets( c_buf, 512, f ) )
{
output = Glib::locale_to_utf8( c_buf ) ;
//blocksize
if ( output .find( "blksize" ) < output .length( ) )
blocksize = atol( (output .substr( output .find( ":" ) +1, output .length( ) ) ) .c_str( ) ) ;
//free blocks
if ( output .find( "free blocks" ) < output .length( ) )
free_blocks = atol( (output .substr( output .find( ":" ) +1, output .length( ) ) ) .c_str( ) ) ;
}
pclose( f ) ;
if ( free_blocks > -1 && blocksize > -1 ) try
partition .Set_Unused( free_blocks * blocksize / 512 ) ; {
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( "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 ) ;
} }
bool reiser4::Create( const Partition & new_partition ) bool reiser4::Create( const Partition & new_partition )

View File

@ -56,30 +56,31 @@ FS reiserfs::get_filesystem_support( )
void reiserfs::Set_Used_Sectors( Partition & partition ) void reiserfs::Set_Used_Sectors( Partition & partition )
{ {
char c_buf[ 512 ] ; argv .push_back( "debugreiserfs" ) ;
FILE *f ; argv .push_back( partition .partition ) ;
Glib::ustring output ;
Sector free_blocks = -1, blocksize = -1 ;
//get free blocks.. envp .push_back( "LC_ALL=C" ) ;
f = popen( ( "LC_ALL=C debugreiserfs " + partition .partition ) .c_str( ), "r" ) ;
while ( fgets( c_buf, 512, f ) )
{
output = Glib::locale_to_utf8( c_buf ) ;
//blocksize
if ( output .find( "Blocksize" ) < output .length( ) )
blocksize = atol( (output .substr( output .find( ":" ) +1, output .length( ) ) ) .c_str( ) ) ;
//free blocks
if ( output .find( "Free blocks" ) < output .length( ) )
free_blocks = atol( (output .substr( output .find( ":" ) +1, output .length( ) ) ) .c_str( ) ) ;
}
pclose( f ) ;
if ( free_blocks > -1 && blocksize > -1 ) try
partition .Set_Unused( free_blocks * blocksize / 512 ) ; {
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( "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 ) bool reiserfs::Create( const Partition & new_partition )

View File

@ -61,31 +61,37 @@ FS xfs::get_filesystem_support( )
void xfs::Set_Used_Sectors( Partition & partition ) void xfs::Set_Used_Sectors( Partition & partition )
{ {
char c_buf[ 512 ] ; argv .push_back( "xfs_db" ) ;
FILE *f ; argv .push_back( "-c" ) ;
argv .push_back( "sb 0" ) ;
Glib::ustring output ; argv .push_back( "-c print blocksize" ) ;
Sector free_blocks = -1, blocksize = -1 ; argv .push_back( "-c print fdblocks" ) ;
argv .push_back( "-r" ) ;
argv .push_back( partition .partition ) ;
//get free blocks.. envp .push_back( "LC_ALL=C" ) ;
f = popen( ( "LC_ALL=C xfs_db -c 'sb 0' -c print -r " + partition .partition ) .c_str( ), "r" ) ;
while ( fgets( c_buf, 512, f ) )
{
output = Glib::locale_to_utf8( c_buf ) ;
//free blocks
if ( output .find( "fdblocks" ) < output .length( ) )
free_blocks = atol( (output .substr( output .find( "=" ) +1, output .length( ) ) ) .c_str( ) ) ;
//blocksize
if ( output .find( "blocksize" ) < output .length( ) )
blocksize = atol( (output .substr( output .find( "=" ) +1, output .length( ) ) ) .c_str( ) ) ;
}
pclose( f ) ;
if ( free_blocks > -1 && blocksize > -1 ) try
partition .Set_Unused( free_blocks * blocksize / 512 ) ; {
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 ;
//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 ) ;
} }
bool xfs::Create( const Partition & new_partition ) bool xfs::Create( const Partition & new_partition )