From 387b391d6dd2b8d02807a20800fff78da673fd60 Mon Sep 17 00:00:00 2001 From: Mike Fleetwood Date: Sat, 14 Sep 2013 14:51:16 +0100 Subject: [PATCH] Add reporting of linux-swap usage (#708107) For active swap space read the usage from /proc/swaps. (Linux kernel uses units of 1 KiB). By definition inactive swap space is 100% free. $ cat /proc/swaps Filename Type Size Used Priority /dev/sda2 partition 5242876 430552 -1 Always set fs.read = FS::EXTERNAL even if /proc/swaps doesn't exist so that an attempt is made to open the file generating a specific error, in addition to the generic error. open("/proc/swaps", O_RDONLY): No such file or directory Unable to read the contents of this file system! Because of this some operations may be unavailable. The cause might be a missing software package. The following list of software packages is required for linux- swap file system support: util-linux. Closes Bug #708107 - Usage of swap space is not reported --- include/linux_swap.h | 1 + src/GParted_Core.cc | 3 +-- src/linux_swap.cc | 45 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/include/linux_swap.h b/include/linux_swap.h index 1b3e485f..d1ab283f 100644 --- a/include/linux_swap.h +++ b/include/linux_swap.h @@ -31,6 +31,7 @@ public: virtual const Glib::ustring get_custom_text( CUSTOM_TEXT ttype, int index = 0 ) ; FS get_filesystem_support() ; + void set_used_sectors( Partition & partition ) ; void read_label( Partition & partition ) ; bool write_label( const Partition & partition, OperationDetail & operationdetail ) ; void read_uuid( Partition & partition ) ; diff --git a/src/GParted_Core.cc b/src/GParted_Core.cc index d534f14b..6c1c8a57 100644 --- a/src/GParted_Core.cc +++ b/src/GParted_Core.cc @@ -1539,8 +1539,7 @@ void GParted_Core::set_used_sectors( std::vector & partitions, PedDis { for ( unsigned int t = 0 ; t < partitions .size() ; t++ ) { - if ( partitions[ t ] .filesystem != GParted::FS_LINUX_SWAP && - partitions[ t ] .filesystem != GParted::FS_LUKS && + if ( partitions[ t ] .filesystem != GParted::FS_LUKS && partitions[ t ] .filesystem != GParted::FS_UNKNOWN ) { diff --git a/src/linux_swap.cc b/src/linux_swap.cc index 01d48726..0ad3ea20 100644 --- a/src/linux_swap.cc +++ b/src/linux_swap.cc @@ -19,6 +19,8 @@ #include "../include/linux_swap.h" +#include + namespace GParted { @@ -43,6 +45,9 @@ FS linux_swap::get_filesystem_support() FS fs ; fs .filesystem = GParted::FS_LINUX_SWAP ; + fs .read = FS::EXTERNAL ; + fs .online_read = FS::EXTERNAL ; + if ( ! Glib::find_program_in_path( "mkswap" ) .empty() ) { fs .create = GParted::FS::EXTERNAL ; @@ -65,6 +70,46 @@ FS linux_swap::get_filesystem_support() return fs ; } +void linux_swap::set_used_sectors( Partition & partition ) +{ + if ( partition .busy ) + { + T = -1 ; N = -1 ; + std::string line ; + std::ifstream input( "/proc/swaps" ) ; + if ( input ) + { + Glib::ustring path = partition .get_path() ; + Glib::ustring::size_type path_len = path.length() ; + while ( getline( input, line ) ) + { + if ( line .substr( 0, path_len ) == path ) + { + sscanf( line .substr( path_len ) .c_str(), " %*s %Ld %Ld", &T, &N ) ; + break ; + } + } + input .close() ; + } + else + { + partition .messages .push_back( "open(\"/proc/swaps\", O_RDONLY): " + Glib::strerror( errno ) ) ; + } + if ( T > -1 && N > -1 ) + { + T = Utils::round( T * ( KIBIBYTE / double(partition .sector_size) ) ) ; + N = Utils::round( N * ( KIBIBYTE / double(partition .sector_size) ) ) ; + partition .set_sector_usage( T, T - N ) ; + } + } + else + { + //By definition inactive swap space is 100% free + Sector size = partition .get_sector_length() ; + partition .set_sector_usage( size, size ) ; + } +} + void linux_swap::read_label( Partition & partition ) { if ( ! Utils::execute_command( "swaplabel " + partition .get_path(), output, error, true ) )