diff --git a/ChangeLog b/ChangeLog index 7d5e2dd8..add87326 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2005-12-28 Bart Hakvoort + + * replaced popen() with Glib::spawn_command_line_sync() + general + cleanups in the set_used_sectors() functions. + 2005-12-25 Bart Hakvoort * merry christmas diff --git a/include/FileSystem.h b/include/FileSystem.h index d8dd31eb..183dbddb 100644 --- a/include/FileSystem.h +++ b/include/FileSystem.h @@ -51,8 +51,10 @@ public: protected: int Execute_Command( Glib::ustring command ) ; + //those are used in several Set_Used_Sectors().. + std::vector argv, envp ; std::string output ; - Sector free_blocks, blocksize ; + Sector N, S ; unsigned int index ; private: diff --git a/include/VBox_VisualDisk.h b/include/VBox_VisualDisk.h index 30d98ea7..af4ad7bd 100644 --- a/include/VBox_VisualDisk.h +++ b/include/VBox_VisualDisk.h @@ -34,7 +34,7 @@ public: VBox_VisualDisk(); ~VBox_VisualDisk(); - void load_partitions( const std::vector & partitions, const Sector device_length ); + void load_partitions( const std::vector & partitions, Sector device_length ); void set_selected( const Partition & partition ) ; void clear() ; @@ -108,6 +108,7 @@ private: selected = false ; pango_layout .clear() ; + logicals .clear() ; } ~visual_partition() diff --git a/src/VBox_VisualDisk.cc b/src/VBox_VisualDisk.cc index 33a198ec..2f56ecb9 100644 --- a/src/VBox_VisualDisk.cc +++ b/src/VBox_VisualDisk.cc @@ -59,7 +59,7 @@ VBox_VisualDisk::VBox_VisualDisk() this ->pack_start( hbox_legend ); } -void VBox_VisualDisk::load_partitions( const std::vector & partitions, const Sector device_length ) +void VBox_VisualDisk::load_partitions( const std::vector & partitions, Sector device_length ) { clear() ; @@ -452,7 +452,7 @@ void VBox_VisualDisk::free_colors( std::vector & visual_partit { 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 ) free_colors( visual_partitions[ t ] .logicals ) ; @@ -464,9 +464,9 @@ VBox_VisualDisk::~VBox_VisualDisk() clear() ; //free the allocated colors - this ->get_colormap( ) ->free_colors( color_used, 1 ) ; - this ->get_colormap( ) ->free_colors( color_unused, 1 ) ; - this ->get_colormap( ) ->free_colors( color_text, 1 ) ; + this ->get_colormap() ->free_colors( color_used, 1 ) ; + this ->get_colormap() ->free_colors( color_unused, 1 ) ; + this ->get_colormap() ->free_colors( color_text, 1 ) ; } } //GParted diff --git a/src/ext2.cc b/src/ext2.cc index 541aa591..30a04f61 100644 --- a/src/ext2.cc +++ b/src/ext2.cc @@ -52,9 +52,15 @@ FS ext2::get_filesystem_support( ) 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 { - 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 ) { @@ -63,25 +69,15 @@ void ext2::Set_Used_Sectors( Partition & partition ) } index = output .find( "Free blocks:" ) ; - if ( index < output .length() ) - { - output = output.substr( index ); - free_blocks = atol( output .substr( 13, output .find( "\n" ) - 13 ) .c_str() ) ; - } - else - free_blocks = -1 ; + 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() ) - { - output = output.substr( index ); - blocksize = atol( output .substr( 12, output .find( "\n" ) - 12 ) .c_str() ) ; - } - else - blocksize = -1 ; + if ( index >= output.length() || sscanf( output.substr( index ) .c_str(), "Block size: %Ld", &S ) != 1 ) + S = -1 ; - if ( free_blocks > -1 && blocksize > -1 ) - partition .Set_Unused( free_blocks * blocksize / 512 ) ; + if ( N > -1 && S > -1 ) + partition .Set_Unused( N * S / 512 ) ; } bool ext2::Create( const Partition & new_partition ) diff --git a/src/ext3.cc b/src/ext3.cc index 8f53ef3e..e626cda9 100644 --- a/src/ext3.cc +++ b/src/ext3.cc @@ -52,9 +52,15 @@ FS ext3::get_filesystem_support( ) 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 { - 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 ) { @@ -63,25 +69,15 @@ void ext3::Set_Used_Sectors( Partition & partition ) } index = output .find( "Free blocks:" ) ; - if ( index < output .length() ) - { - output = output.substr( index ); - free_blocks = atol( output .substr( 13, output .find( "\n" ) - 13 ) .c_str() ) ; - } - else - free_blocks = -1 ; + 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() ) - { - output = output.substr( index ); - blocksize = atol( output .substr( 12, output .find( "\n" ) - 12 ) .c_str() ) ; - } - else - blocksize = -1 ; - - if ( free_blocks > -1 && blocksize > -1 ) - partition .Set_Unused( free_blocks * blocksize / 512 ) ; + 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 ) ; } bool ext3::Create( const Partition & new_partition ) diff --git a/src/fat16.cc b/src/fat16.cc index db06d436..df26b8c5 100644 --- a/src/fat16.cc +++ b/src/fat16.cc @@ -53,33 +53,36 @@ FS fat16::get_filesystem_support( ) void fat16::Set_Used_Sectors( Partition & partition ) { - char c_buf[ 512 ] ; - FILE *f ; - - Glib::ustring output ; - Sector free_clusters = -1, bytes_per_cluster = -1 ; + argv .push_back( "dosfsck" ) ; + argv .push_back( "-v" ) ; + argv .push_back( partition .partition ) ; - //get free blocks.. - 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 ) ; + envp .push_back( "LC_ALL=C" ) ; - if ( free_clusters > -1 && bytes_per_cluster > -1 ) - partition .Set_Unused( free_clusters * bytes_per_cluster / 512 ) ; + try + { + 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 ) diff --git a/src/fat32.cc b/src/fat32.cc index d463180d..459f1437 100644 --- a/src/fat32.cc +++ b/src/fat32.cc @@ -52,33 +52,36 @@ FS fat32::get_filesystem_support( ) void fat32::Set_Used_Sectors( Partition & partition ) { - char c_buf[ 512 ] ; - FILE *f ; - - Glib::ustring output ; - Sector free_clusters = -1, bytes_per_cluster = -1 ; + argv .push_back( "dosfsck" ) ; + argv .push_back( "-v" ) ; + argv .push_back( partition .partition ) ; - //get free blocks.. - 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 ) ; + envp .push_back( "LC_ALL=C" ) ; - if ( free_clusters > -1 && bytes_per_cluster > -1 ) - partition .Set_Unused( free_clusters * bytes_per_cluster / 512 ) ; + try + { + 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 ) diff --git a/src/ntfs.cc b/src/ntfs.cc index fa3929db..2e9d67d2 100644 --- a/src/ntfs.cc +++ b/src/ntfs.cc @@ -53,25 +53,28 @@ FS ntfs::get_filesystem_support( ) void ntfs::Set_Used_Sectors( Partition & partition ) { - char c_buf[ 512 ] ; - FILE *f ; - - Glib::ustring output ; + argv .push_back( "ntfscluster" ) ; + argv .push_back( "--force" ) ; + argv .push_back( partition .partition ) ; - //get free sectors.. - f = popen( ( "LC_ALL=C ntfscluster --force " + partition .partition ) .c_str( ), "r" ) ; - while ( fgets( c_buf, 512, f ) ) + envp .push_back( "LC_ALL=C" ) ; + + try { - output = Glib::locale_to_utf8( c_buf ) ; - - //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 ; - } + Glib::spawn_sync( ".", argv, envp, Glib::SPAWN_SEARCH_PATH, sigc::slot< void >(), &output ) ; } - 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 ) diff --git a/src/reiser4.cc b/src/reiser4.cc index eed097b3..887b3c68 100644 --- a/src/reiser4.cc +++ b/src/reiser4.cc @@ -46,30 +46,31 @@ FS reiser4::get_filesystem_support( ) void reiser4::Set_Used_Sectors( Partition & partition ) { - char c_buf[ 512 ] ; - FILE *f ; - - Glib::ustring output ; - Sector free_blocks = -1, blocksize = -1 ; + argv .push_back( "debugfs.reiser4" ) ; + argv .push_back( partition .partition ) ; - //get free blocks.. - 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 ) ; + envp .push_back( "LC_ALL=C" ) ; - if ( free_blocks > -1 && blocksize > -1 ) - partition .Set_Unused( free_blocks * blocksize / 512 ) ; + try + { + 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 ) diff --git a/src/reiserfs.cc b/src/reiserfs.cc index 549d45c2..7961c27f 100644 --- a/src/reiserfs.cc +++ b/src/reiserfs.cc @@ -56,30 +56,31 @@ FS reiserfs::get_filesystem_support( ) void reiserfs::Set_Used_Sectors( Partition & partition ) { - char c_buf[ 512 ] ; - FILE *f ; - - Glib::ustring output ; - Sector free_blocks = -1, blocksize = -1 ; + argv .push_back( "debugreiserfs" ) ; + argv .push_back( partition .partition ) ; - //get free blocks.. - 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 ) ; + envp .push_back( "LC_ALL=C" ) ; - if ( free_blocks > -1 && blocksize > -1 ) - partition .Set_Unused( free_blocks * blocksize / 512 ) ; + try + { + 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 ) diff --git a/src/xfs.cc b/src/xfs.cc index 5b76d903..8a98872e 100644 --- a/src/xfs.cc +++ b/src/xfs.cc @@ -61,31 +61,37 @@ FS xfs::get_filesystem_support( ) void xfs::Set_Used_Sectors( Partition & partition ) { - char c_buf[ 512 ] ; - FILE *f ; - - Glib::ustring output ; - Sector free_blocks = -1, blocksize = -1 ; + argv .push_back( "xfs_db" ) ; + argv .push_back( "-c" ) ; + argv .push_back( "sb 0" ) ; + argv .push_back( "-c print blocksize" ) ; + argv .push_back( "-c print fdblocks" ) ; + argv .push_back( "-r" ) ; + argv .push_back( partition .partition ) ; - //get free blocks.. - 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 ) ; + envp .push_back( "LC_ALL=C" ) ; - if ( free_blocks > -1 && blocksize > -1 ) - partition .Set_Unused( free_blocks * blocksize / 512 ) ; + try + { + 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 )