gparted/src/jfs.cc

253 lines
7.9 KiB
C++
Raw Normal View History

/* Copyright (C) 2004 Bart
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "../include/jfs.h"
#include <cerrno>
namespace GParted
{
FS jfs::get_filesystem_support()
{
FS fs ;
fs .filesystem = GParted::FS_JFS ;
if ( ! Glib::find_program_in_path( "jfs_debugfs" ) .empty() )
fs .read = GParted::FS::EXTERNAL ;
if ( ! Glib::find_program_in_path( "mkfs.jfs" ) .empty() )
fs .create = GParted::FS::EXTERNAL ;
if ( ! Glib::find_program_in_path( "jfs_fsck" ) .empty() )
fs .check = GParted::FS::EXTERNAL ;
//resizing of jfs requires jfs support in the kernel
std::ifstream input( "/proc/filesystems" ) ;
if ( input )
{
Glib::ustring line ;
while ( input >> line )
if ( line == "jfs" )
{
fs .grow = GParted::FS::EXTERNAL ;
break ;
}
input .close() ;
}
if ( ! Glib::find_program_in_path( "dd" ) .empty() && fs .grow )
fs .copy = GParted::FS::EXTERNAL ;
fs .MIN = 16 ;
return fs ;
}
void jfs::Set_Used_Sectors( Partition & partition )
{
argv .push_back( "sh" ) ;
argv .push_back( "-c" ) ;
argv .push_back( "echo dm | jfs_debugfs " + partition .partition ) ;
if ( ! execute_command( argv, output ) )
{
//blocksize
index = output .find( "Block Size:" ) ;
if ( index >= output .length() ||
sscanf( output .substr( index ) .c_str(), "Block Size: %Ld", &S ) != 1 )
S = -1 ;
//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 ) ) ) ;
}
}
bool jfs::Create( const Partition & new_partition, std::vector<OperationDetails> & operation_details )
{
operation_details .push_back( OperationDetails( String::ucompose(
_("create new %1 filesystem"),
Utils::Get_Filesystem_String( GParted::FS_JFS ) ) ) ) ;
argv .clear() ;
argv .push_back( "mkfs.jfs" ) ;
argv .push_back( "-q" ) ;
argv .push_back( new_partition .partition ) ;
if ( ! execute_command( argv, operation_details .back() .sub_details ) )
{
operation_details .back() .status = OperationDetails::SUCCES ;
return true ;
}
else
{
operation_details .back() .status = OperationDetails::ERROR ;
return false ;
}
}
bool jfs::Resize( const Partition & partition_new,
std::vector<OperationDetails> & operation_details,
bool fill_partition )
{
if ( fill_partition )
operation_details .push_back( OperationDetails( _("grow filesystem to fill the partition") ) ) ;
else
operation_details .push_back( OperationDetails( _("resize the filesystem") ) ) ;
bool return_value = false ;
Glib::ustring error ;
Glib::ustring TEMP_MP = Glib::get_tmp_dir() + "/gparted_tmp_jfs_mountpoint" ;
//create mountpoint...
operation_details .back() .sub_details .push_back(
OperationDetails( String::ucompose( _("create temporary mountpoint (%1)"), TEMP_MP ) ) ) ;
if ( ! mkdir( TEMP_MP .c_str(), 0 ) )
{
operation_details .back() .sub_details .back() .status = OperationDetails::SUCCES ;
//mount partition
operation_details .back() .sub_details .push_back(
OperationDetails( String::ucompose( _("mount %1 on %2"), partition_new .partition, TEMP_MP ) ) ) ;
if ( Utils::mount( partition_new .partition, TEMP_MP, "jfs", error ) )
{
operation_details .back() .sub_details .back() .status = OperationDetails::SUCCES ;
//remount the partition to resize the filesystem
operation_details .back() .sub_details .push_back(
OperationDetails( String::ucompose( _("remount %1 on %2 with the 'resize' flag enabled"),
partition_new .partition, TEMP_MP ) ) ) ;
if ( Utils::mount( partition_new .partition, TEMP_MP, "jfs", error, MS_REMOUNT, "resize" ) )
{
operation_details .back() .sub_details .back() .status = OperationDetails::SUCCES ;
return_value = true ;
}
else
{
operation_details .back() .sub_details .back() .status = OperationDetails::ERROR ;
operation_details .back() .sub_details .back() .sub_details .push_back(
OperationDetails( error, OperationDetails::NONE ) ) ;
}
//and unmount it...
operation_details .back() .sub_details .push_back(
OperationDetails( String::ucompose( _("unmount %1"), partition_new .partition ) ) ) ;
if ( Utils::unmount( partition_new .partition, TEMP_MP, error ) )
{
operation_details .back() .sub_details .back() .status = OperationDetails::SUCCES ;
}
else
{
operation_details .back() .sub_details .back() .status = OperationDetails::ERROR ;
operation_details .back() .sub_details .back() .sub_details .push_back(
OperationDetails( error, OperationDetails::NONE ) ) ;
return_value = false ;
}
}
else
{
operation_details .back() .sub_details .back() .status = OperationDetails::ERROR ;
operation_details .back() .sub_details .back() .sub_details .push_back(
OperationDetails( error, OperationDetails::NONE ) ) ;
}
//remove the mountpoint..
operation_details .back() .sub_details .push_back(
OperationDetails( String::ucompose( _("remove temporary mountpoint (%1)"), TEMP_MP ) ) ) ;
if ( ! rmdir( TEMP_MP .c_str() ) )
{
operation_details .back() .sub_details .back() .status = OperationDetails::SUCCES ;
}
else
{
operation_details .back() .sub_details .back() .status = OperationDetails::ERROR ;
operation_details .back() .sub_details .back() .sub_details .push_back(
OperationDetails( Glib::strerror( errno ), OperationDetails::NONE ) ) ;
return_value = false ;
}
}
else
{
operation_details .back() .sub_details .back() .status = OperationDetails::ERROR ;
operation_details .back() .sub_details .back() .sub_details .push_back(
OperationDetails( Glib::strerror( errno ), OperationDetails::NONE ) ) ;
}
operation_details .back() .status = return_value ? OperationDetails::SUCCES : OperationDetails::ERROR ;
return return_value ;
}
bool jfs::Copy( const Glib::ustring & src_part_path,
const Glib::ustring & dest_part_path,
std::vector<OperationDetails> & operation_details )
{
operation_details .push_back( OperationDetails(
String::ucompose( _("copy contents of %1 to %2"), src_part_path, dest_part_path ) ) ) ;
argv .clear() ;
argv .push_back( "dd" ) ;
argv .push_back( "bs=8192" ) ;
argv .push_back( "if=" + src_part_path ) ;
argv .push_back( "of=" + dest_part_path ) ;
if ( ! execute_command( argv, operation_details .back() .sub_details ) )
{
operation_details .back() .status = OperationDetails::SUCCES ;
Partition partition ;
partition .partition = dest_part_path ;
return Resize( partition, operation_details, true ) ;
}
operation_details .back() .status = OperationDetails::ERROR ;
return false ;
}
bool jfs::Check_Repair( const Partition & partition, std::vector<OperationDetails> & operation_details )
{
operation_details .push_back( OperationDetails( _("check filesystem for errors and (if possible) fix them") ) ) ;
argv .clear() ;
argv .push_back( "jfs_fsck" ) ;
argv .push_back( "-f" ) ;
argv .push_back( partition .partition ) ;
if ( 1 >= execute_command( argv, operation_details .back() .sub_details ) >= 0 )
{
operation_details .back() .status = OperationDetails::SUCCES ;
return true ;
}
else
{
operation_details .back() .status = OperationDetails::ERROR ;
return false ;
}
}
} //GParted