Stop using floating point calculations in FS resize() methods (!119)

A number of the file system specific resize() methods use floating point
calculations to convert from the new partition size in sectors to the
new file system size to be passed to the resize command in bytes or
kibibytes.  This is bad because there could be rounding errors
converting from integer to floating point, performing the calculation
and converting back.  Replace with integer only multiply and divide
calculations.  Integer division always truncates [1] which is exactly
what is needed.  The largest integer will be the size of the file system
in bytes held in a signed 64-bit long long, or Sector or Byte_Value
typedef of the same type.  This will limit the size that a file system
can be shrunk to, to 8 EiB - 1 byte.

[1] C++ Arithmetic operators
    https://en.cppreference.com/w/cpp/language/operator_arithmetic
        "the algebraic quotient of integer division is truncated towards
        zero (fractional part is discarded)"

Closes !119 -  Tidy-ups for file system interface classes
This commit is contained in:
Mike Fleetwood 2023-10-07 20:47:34 +01:00 committed by Curtis Gedak
parent 9ace657bb0
commit f098ba1ec7
6 changed files with 6 additions and 16 deletions

View File

@ -296,8 +296,7 @@ bool btrfs::resize( const Partition & partition_new, OperationDetail & operation
{
Glib::ustring size ;
if ( ! fill_partition )
size = Utils::num_to_str( floor( Utils::sector_to_unit(
partition_new .get_sector_length(), partition_new .sector_size, UNIT_KIB ) ) ) + "K" ;
size = Utils::num_to_str(partition_new.get_byte_length() / KIBIBYTE) + "K";
else
size = "max" ;
success &= ! execute_command("btrfs filesystem resize " + devid_str + ":" + size +

View File

@ -302,8 +302,7 @@ bool ext2::resize( const Partition & partition_new, OperationDetail & operationd
Glib::ustring str_temp = "resize2fs -p " + Glib::shell_quote( partition_new.get_path() );
if ( ! fill_partition )
str_temp += " " + Utils::num_to_str( floor( Utils::sector_to_unit(
partition_new .get_sector_length(), partition_new .sector_size, UNIT_KIB ) ) ) + "K";
str_temp += " " + Utils::num_to_str(partition_new.get_byte_length() / KIBIBYTE) + "K";
return ! execute_command( str_temp, operationdetail, EXEC_CHECK_STATUS|EXEC_PROGRESS_STDOUT,
static_cast<StreamSlot>( sigc::mem_fun( *this, &ext2::resize_progress ) ) );

View File

@ -107,8 +107,7 @@ bool lvm2_pv::resize( const Partition & partition_new, OperationDetail & operati
Glib::ustring size = "" ;
if ( ! fill_partition )
size = " --yes --setphysicalvolumesize " +
Utils::num_to_str( floor( Utils::sector_to_unit(
partition_new .get_sector_length(), partition_new .sector_size, UNIT_KIB ) ) ) + "K " ;
Utils::num_to_str(partition_new.get_byte_length() / KIBIBYTE) + "K ";
return ! execute_command( "lvm pvresize -v " + size + Glib::shell_quote( partition_new.get_path() ),
operationdetail, EXEC_CHECK_STATUS );
}

View File

@ -199,8 +199,7 @@ bool nilfs2::resize( const Partition & partition_new, OperationDetail & operatio
Glib::ustring cmd = "nilfs-resize -v -y " + Glib::shell_quote( partition_new.get_path() );
if ( ! fill_partition )
{
Glib::ustring size = Utils::num_to_str( floor( Utils::sector_to_unit(
partition_new .get_sector_length(), partition_new .sector_size, UNIT_KIB ) ) ) + "K" ;
Glib::ustring size = Utils::num_to_str(partition_new.get_byte_length() / KIBIBYTE) + "K";
cmd += " " + size ;
}
success &= ! execute_command( cmd, operationdetail, EXEC_CHECK_STATUS );

View File

@ -192,10 +192,7 @@ bool ntfs::resize( const Partition & partition_new, OperationDetail & operationd
bool success;
Glib::ustring size = "" ;
if ( ! fill_partition )
{
size = " -s " + Utils::num_to_str( floor( Utils::sector_to_unit(
partition_new .get_sector_length(), partition_new .sector_size, UNIT_BYTE ) ) ) ;
}
size = " -s " + Utils::num_to_str(partition_new.get_byte_length());
Glib::ustring cmd = "ntfsresize --force --force" + size ;
//simulation..

View File

@ -177,10 +177,7 @@ bool reiserfs::resize( const Partition & partition_new, OperationDetail & operat
{
Glib::ustring size = "" ;
if ( ! fill_partition )
{
size = " -s " + Utils::num_to_str( floor( Utils::sector_to_unit(
partition_new .get_sector_length(), partition_new .sector_size, UNIT_BYTE ) ) ) ;
}
size = " -s " + Utils::num_to_str(partition_new.get_byte_length());
const Glib::ustring resize_cmd = "echo y | resize_reiserfs" + size +
" " + Glib::shell_quote( partition_new.get_path() );
exit_status = execute_command( "sh -c " + Glib::shell_quote( resize_cmd ), operationdetail );