From 7f57bb109068fbc85f7bb8cb48a5959755372afc Mon Sep 17 00:00:00 2001 From: Bart Hakvoort Date: Wed, 14 Dec 2005 14:47:58 +0000 Subject: [PATCH] added mount() and unmount() use Utils::mount and Utils::umount instead of * include/Utils.h, src/Utils.cc: added mount() and unmount() * src/Win_GParted.cc, src/jfs.cc, src/xfs.cc: use Utils::mount and Utils::umount instead of wrapping CLI tools. --- ChangeLog | 9 ++++ include/Utils.h | 14 +++++- src/Utils.cc | 105 +++++++++++++++++++++++++++++++++++++++++++++ src/Win_GParted.cc | 16 ++----- src/jfs.cc | 22 ++++++---- src/xfs.cc | 34 +++++++++------ 6 files changed, 164 insertions(+), 36 deletions(-) diff --git a/ChangeLog b/ChangeLog index 04a89a30..abb8c6c6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2005-12-14 Bart Hakvoort + + * include/Utils.h, + src/Utils.cc: added mount() and unmount() + * src/Win_GParted.cc, + src/jfs.cc, + src/xfs.cc: use Utils::mount and Utils::umount instead of wrapping + CLI tools. + 2005-12-14 Bart Hakvoort * src/Partition.cc: since we now have the device_path in Partition we diff --git a/include/Utils.h b/include/Utils.h index f590fd86..fe12262c 100644 --- a/include/Utils.h +++ b/include/Utils.h @@ -30,6 +30,7 @@ #include #include +#include namespace GParted { @@ -93,10 +94,21 @@ class Utils public: static long Round( double double_value ) ; static long Sector_To_MB( Sector sectors ) ; - static Gtk::Label * mk_label( const Glib::ustring & text, bool use_markup = true, bool align_left = true, bool wrap = false, const Glib::ustring & text_color = "black" ) ; + static Gtk::Label * mk_label( const Glib::ustring & text, + bool use_markup = true, + bool align_left = true, + bool wrap = false, + const Glib::ustring & text_color = "black" ) ; static Glib::ustring num_to_str( Sector number, bool use_C_locale = false ) ; static Glib::ustring Get_Color( FILESYSTEM filesystem ) ; static Glib::ustring Get_Filesystem_String( FILESYSTEM filesystem ) ; + static bool mount( const Glib::ustring & node, + const Glib::ustring & mountpoint, + const Glib::ustring & filesystem, + Glib::ustring & error, + unsigned long flags = 0, + const Glib::ustring & data = "" ) ; + static bool unmount( const Glib::ustring & node, const Glib::ustring & mountpoint, Glib::ustring & error ) ; }; diff --git a/src/Utils.cc b/src/Utils.cc index 259a2ff9..11fa242b 100644 --- a/src/Utils.cc +++ b/src/Utils.cc @@ -18,6 +18,9 @@ #include "../include/Utils.h" #include +#include +#include + namespace GParted { @@ -113,4 +116,106 @@ Glib::ustring Utils::Get_Filesystem_String( FILESYSTEM filesystem ) } } +bool Utils::mount( const Glib::ustring & node, + const Glib::ustring & mountpoint, + const Glib::ustring & filesystem, + Glib::ustring & error, + unsigned long flags, + const Glib::ustring & data ) +{ + if ( ! ::mount( node .c_str(), mountpoint .c_str(), filesystem .c_str(), flags, data .c_str() ) ) + { + std::ifstream proc_mounts( "/proc/mounts" ) ; + + if ( proc_mounts ) + { + bool hit = false ; + char c_node[255], c_mountpoint[255] ; + std::string line ; + + //search for relevant line in /proc/mounts + while ( getline( proc_mounts, line ) ) + { + if ( line .length() > 0 && line[ 0 ] == '/' && + sscanf( line .c_str(),"%s %s", c_node, c_mountpoint ) == 2 && + c_node == node && c_mountpoint == mountpoint + ) + { + hit = true ; + break ; + } + } + + proc_mounts .close() ; + + //append 'line' to /etc/mtab + if ( hit ) + { + std::ofstream mtab( "/etc/mtab", std::ios::app ) ; + + if ( mtab ) + { + mtab << line << '\n' ; + mtab .close() ; + + return true ; + } + } + } + } + else + error = Glib::strerror( errno ) ; + + + return false ; +} + +bool Utils::unmount( const Glib::ustring & node, const Glib::ustring & mountpoint, Glib::ustring & error ) +{ + if ( ! umount( mountpoint .c_str() ) ) + { + //search in /etc/mtab voor node and mountpoint and delete that line + Glib::ustring mtab_minus_mount ; + bool hit = false ; + std::ifstream mtab_in( "/etc/mtab" ) ; + + if ( mtab_in ) + { + char c_node[255], c_mountpoint[255] ; + std::string line ; + + while ( getline( mtab_in, line ) ) + { + if ( line .length() > 0 && line[ 0 ] == '/' && + sscanf( line .c_str(),"%s %s", c_node, c_mountpoint ) == 2 && + c_node == node && c_mountpoint == mountpoint + ) + hit = true ; + else + mtab_minus_mount += line + '\n'; + } + + mtab_in .close() ; + } + + if ( hit ) + { + std::ofstream mtab_out( "/etc/mtab" ) ; + + if ( mtab_out ) + { + mtab_out << mtab_minus_mount ; + mtab_out .close() ; + + return true ; + } + } + } + else + error = Glib::strerror( errno ) ; + + + return false ; +} + } //GParted.. diff --git a/src/Win_GParted.cc b/src/Win_GParted.cc index 1f6aa3de..3d7f9f10 100644 --- a/src/Win_GParted.cc +++ b/src/Win_GParted.cc @@ -1189,25 +1189,17 @@ void Win_GParted::activate_convert( GParted::FILESYSTEM new_fs ) void Win_GParted::activate_unmount( ) { - char c_buf[ 512 ] ; - Glib::ustring output ; + Glib::ustring error ; - FILE *f = popen( ( "umount " + selected_partition .partition + " 2>&1" ) .c_str( ), "r" ) ; - - while ( fgets( c_buf, 512, f ) ) - output += c_buf ; - - pclose( f ) ; - - if ( ! output .empty( ) ) + if ( ! Utils::unmount( selected_partition .partition, selected_partition .mountpoint, error ) ) { str_temp = "" ; str_temp += String::ucompose( _("Could not unmount %1"), selected_partition .partition ) ; str_temp += "\n\n" ; - Gtk::MessageDialog dialog( *this, str_temp + output, true, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true ); + Gtk::MessageDialog dialog( *this, str_temp + error, true, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true ); dialog.run( ) ; - } + } else menu_gparted_refresh_devices( ) ; } diff --git a/src/jfs.cc b/src/jfs.cc index ab6930de..166107ac 100644 --- a/src/jfs.cc +++ b/src/jfs.cc @@ -36,18 +36,20 @@ FS jfs::get_filesystem_support( ) if ( ! system( "which jfs_fsck 1>/dev/null 2>/dev/null" ) ) fs .check = GParted::FS::EXTERNAL ; - //resizing of jfs requires mount, umount and jfs support in the kernel - if ( ! system( "which mount umount 1>/dev/null 2>/dev/null" ) ) + //resizing of jfs requires jfs support in the kernel + std::ifstream input( "/proc/filesystems" ) ; + + if ( input ) { Glib::ustring line ; - std::ifstream input( "/proc/filesystems" ) ; + while ( input >> line ) if ( line == "jfs" ) { fs .grow = GParted::FS::EXTERNAL ; break ; } - + input .close( ) ; } @@ -97,15 +99,17 @@ bool jfs::Create( const Partition & new_partition ) bool jfs::Resize( const Partition & partition_new, bool fill_partition ) { bool return_value = false ; + Glib::ustring error ; + Glib::ustring TEMP_MP = "/tmp/gparted_tmp_jfs_mountpoint" ; //jfs kan only grow if the partition is mounted.. - system( "mkdir /tmp/gparted_tmp_jfs_mountpoint" ) ; - if ( ! Execute_Command( "mount " + partition_new .partition + " /tmp/gparted_tmp_jfs_mountpoint" ) ) + system( ("mkdir " + TEMP_MP) .c_str() ) ; + if ( Utils::mount( partition_new .partition, TEMP_MP, "jfs", error ) ) { - return_value = ! Execute_Command( "mount -o remount,resize /tmp/gparted_tmp_jfs_mountpoint" ) ; - Execute_Command( "umount " + partition_new .partition ) ; + return_value = Utils::mount( partition_new .partition, TEMP_MP, "jfs", error, MS_REMOUNT, "resize" ) ; + Utils::unmount( partition_new .partition, TEMP_MP, error ) ; } - system( "rmdir /tmp/gparted_tmp_jfs_mountpoint" ) ; + system( ("rmdir " + TEMP_MP) .c_str() ) ; return return_value ; } diff --git a/src/xfs.cc b/src/xfs.cc index e89f98b4..cebfd6f4 100644 --- a/src/xfs.cc +++ b/src/xfs.cc @@ -36,8 +36,8 @@ FS xfs::get_filesystem_support( ) if ( ! system( "which xfs_repair 1>/dev/null 2>/dev/null" ) ) fs .check = GParted::FS::EXTERNAL ; - //resizing of xfs requires xfs_growfs, xfs_repair, mount, umount and xfs support in the kernel - if ( ! system( "which xfs_growfs mount umount 1>/dev/null 2>/dev/null" ) && fs .check ) + //resizing of xfs requires xfs_growfs, xfs_repair and xfs support in the kernel + if ( ! system( "which xfs_growfs 1>/dev/null 2>/dev/null" ) && fs .check ) { Glib::ustring line ; std::ifstream input( "/proc/filesystems" ) ; @@ -51,7 +51,7 @@ FS xfs::get_filesystem_support( ) input .close( ) ; } - if ( ! system( "which xfsdump xfsrestore mount umount 1>/dev/null 2>/dev/null" ) && fs .check && fs .create ) + if ( ! system( "which xfsdump xfsrestore 1>/dev/null 2>/dev/null" ) && fs .check && fs .create ) fs .copy = GParted::FS::EXTERNAL ; fs .MIN = 32 ;//official minsize = 16MB, but the smallest xfs_repair can handle is 32MB... @@ -96,15 +96,17 @@ bool xfs::Create( const Partition & new_partition ) bool xfs::Resize( const Partition & partition_new, bool fill_partition ) { bool return_value = false ; + Glib::ustring error ; + Glib::ustring TEMP_MP = "/tmp/gparted_tmp_xfs_mountpoint" ; //xfs kan only grow if the partition is mounted.. - system( "mkdir /tmp/gparted_tmp_xfs_mountpoint" ) ; - if ( ! Execute_Command( "mount " + partition_new .partition + " /tmp/gparted_tmp_xfs_mountpoint" ) ) + system( ("mkdir " + TEMP_MP) .c_str() ) ; + if ( Utils::mount( partition_new .partition, TEMP_MP, "xfs", error ) ) { - return_value = ! Execute_Command( "xfs_growfs /tmp/gparted_tmp_xfs_mountpoint" ) ; - Execute_Command( "umount " + partition_new .partition ) ; + return_value = ! Execute_Command( "xfs_growfs " + TEMP_MP ) ; + Utils::unmount( partition_new .partition, TEMP_MP, error ) ; } - system( "rmdir /tmp/gparted_tmp_xfs_mountpoint" ) ; + system( ("rmdir " + TEMP_MP) .c_str() ) ; return return_value ; } @@ -112,17 +114,21 @@ bool xfs::Resize( const Partition & partition_new, bool fill_partition ) bool xfs::Copy( const Glib::ustring & src_part_path, const Glib::ustring & dest_part_path ) { bool return_value = false ; + Glib::ustring error ; + Glib::ustring SRC = "/tmp/gparted_tmp_xfs_src_mountpoint" ; + Glib::ustring DST = "/tmp/gparted_tmp_xfs_dest_mountpoint" ; - system( "mkdir /tmp/gparted_tmp_xfs_src_mountpoint /tmp/gparted_tmp_xfs_dest_mountpoint" ) ; + system( ("mkdir " + SRC + " " + DST) .c_str() ) ; if ( ! Execute_Command( "mkfs.xfs -f " + dest_part_path ) && - ! Execute_Command( "mount " + src_part_path + " /tmp/gparted_tmp_xfs_src_mountpoint" ) && - ! Execute_Command( "mount " + dest_part_path + " /tmp/gparted_tmp_xfs_dest_mountpoint" ) + Utils::mount( src_part_path, SRC, "xfs", error, MS_NOATIME | MS_RDONLY ) && + Utils::mount( dest_part_path, DST, "xfs", error ) ) - return_value = ! Execute_Command( "xfsdump -J - /tmp/gparted_tmp_xfs_src_mountpoint | xfsrestore -J - /tmp/gparted_tmp_xfs_dest_mountpoint" ) ; + return_value = ! Execute_Command( "xfsdump -J - " + SRC + " | xfsrestore -J - " + DST ) ; - Execute_Command( "umount " + src_part_path + " " + dest_part_path ) ; - system( "rmdir /tmp/gparted_tmp_xfs_src_mountpoint /tmp/gparted_tmp_xfs_dest_mountpoint" ) ; + Utils::unmount( src_part_path, SRC, error ) ; + Utils::unmount( dest_part_path, DST, error ) ; + system( ("rmdir " + SRC + " " + DST) .c_str() ) ; return return_value ; }