Skip to content

Commit

Permalink
Improve the overflow fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lslezak committed Jun 29, 2018
1 parent 2e3bcf6 commit 0fb3694
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 76 deletions.
121 changes: 46 additions & 75 deletions src/NCPkgPopupDiskspace.cc
Expand Up @@ -59,6 +59,9 @@

#include "NCi18n.h"

// zypp::str::form()
#include <zypp/base/String.h>

// set values as set in YQPkgDiskUsageList.cc
#define MIN_FREE_MB_WARN 400
#define MIN_FREE_MB_PROXIMITY 700
Expand Down Expand Up @@ -108,52 +111,28 @@ NCPkgDiskspace::~NCPkgDiskspace()
{
}


namespace
{

// Some partitions are skipped for the popup.

bool
skip_partition( const ZyppPartitionDu & partition )
namespace {
std::string formatSize(unsigned long long size, int width = 0)
{
if (partition.readonly)
{
yuiMilestone() << "skipping " << partition.dir << " due to read-only flag" << endl;
return true;
}

// Workaround for partitions equal or larger than 8 EiB (see bug
// #991090).

long long used_size = partition.used_size * FSize::KB; // before package installation
long long pkg_size = partition.pkg_size * FSize::KB; // after package installation
long long total_size = partition.total_size * FSize::KB;

// Skip partition if the used size after package installation is
// smaller or equal to the used size before package installation.

if (pkg_size <= used_size)
{
yuiMilestone() << "skipping " << partition.dir << " due to not being used" << endl;
return true;
}

// Skip partition if free size before package installation is negative
// since that indicates overflows.

if (total_size - used_size < 0)
{
yuiMilestone() << "skipping " << partition.dir << " due to negative value" << endl;
return true;
}
// FSize::bestUnit does not work for huge numbers so only use it for small once
FSize::Unit unit = (size >= FSize::TB) ? FSize::T : FSize(size).bestUnit();
int prec = unit == FSize::B ? 0 : 2;

return false;
return zypp::str::form( "%*.*f %s", width, prec, double(size) / FSize::factor(unit), FSize::unit(unit));
}

int usedPercentInt( unsigned long long used, unsigned long long total )
{
int percent = 0;

if ( total != 0 )
// use double to avoid overflow
percent = ( 100.0 * used ) / total;

return percent;
}
}


///////////////////////////////////////////////////////////////////
//
//
Expand Down Expand Up @@ -186,19 +165,17 @@ void NCPkgDiskspace::fillPartitionTable()

for (ZyppDuSetIterator it = b; it != e; ++it)
{
if (skip_partition (*it))
continue;

zypp::ByteCount pkg_used (it->pkg_size * 1024);
if (it->readonly)
continue;

zypp::ByteCount pkg_available ((it->total_size - it->pkg_size) * 1024);
unsigned long long pkg_used = it->pkg_size * FSize::KB;
unsigned long long pkg_available = (it->total_size - it->pkg_size) * FSize::KB;
unsigned long long total = it->total_size * FSize::KB;

zypp::ByteCount total (it->total_size * 1024);

newItem = new YTableItem( it->dir,
pkg_used.asString (8),
pkg_available.asString (8),
total.asString (8),
newItem = new YTableItem( it->dir,
formatSize(pkg_used, 0),
formatSize(pkg_available, 0),
formatSize(total, 0),
usedPercent( it->pkg_size, it->total_size ) );

partitions->addItem( newItem );
Expand Down Expand Up @@ -236,10 +213,12 @@ std::string NCPkgDiskspace::checkDiskSpace()

for (it = b; it != e; ++it)
{
if (skip_partition (*it))
if (it->readonly)
continue;

zypp::ByteCount pkg_available = (it->total_size - it->pkg_size) * 1024;
unsigned long long total = it->total_size;
unsigned long long pkg_size = it->pkg_size;
unsigned long long pkg_available = total - pkg_size;
if ( pkg_available < 0 )
{
text += "\"";
Expand All @@ -248,8 +227,7 @@ std::string NCPkgDiskspace::checkDiskSpace()
text += " ";
text += NCPkgStrings::MoreText();
text += " ";
std::string available = pkg_available.asString();
text += available.replace( 0, 1, " " ); // clear the minus sign??
text += formatSize(-pkg_available * FSize::KB);
text += " ";
text += NCPkgStrings::MoreSpaceText();
text += "<br>";
Expand All @@ -269,32 +247,32 @@ std::string NCPkgDiskspace::checkDiskSpace()
//
void NCPkgDiskspace::checkRemainingDiskSpace( const ZyppPartitionDu & partition )
{
if ( skip_partition ( partition ) )
if ( partition.readonly )
return;

FSize usedSize ( partition.pkg_size, FSize::K );
FSize totalSize ( partition.total_size, FSize::K );

int percent = 0;
unsigned long long usedSize = partition.pkg_size * FSize::KB;
unsigned long long totalSize = partition.total_size * FSize::KB;

if ( totalSize != 0 )
percent = ( 100 * usedSize ) / totalSize;
int percent = usedPercentInt(usedSize, totalSize);

int free = ( totalSize - usedSize ) / FSize::MB;
long long free = (totalSize - usedSize) / FSize::MB;

yuiMilestone() << "Partition: " << partition.dir << " Used percent: "
<< percent << " Free: " << free << endl;
yuiMilestone() << "Partition: " << partition.dir << " Total: " << totalSize
<< " Used: " << usedSize << " Used percent: " << percent
<< "% Free (MiB): " << free << endl;

if ( percent > MIN_PERCENT_WARN )
{
// Modern hard disks can be huge, so a warning based on percentage only
// can be misleading - check the absolute value, too.
if ( free < MIN_FREE_MB_PROXIMITY )
{
yuiWarning() << "free < MIN_FREE_MB_PROXIMITY (" << MIN_FREE_MB_PROXIMITY << ")" << endl;
runningOutWarning.enterProximity();
}
if ( free < MIN_FREE_MB_WARN )
{
yuiWarning() << "free < MIN_FREE_MB_WARN (" << MIN_FREE_MB_WARN << ")" << endl;
runningOutWarning.enterRange();
}
}
Expand Down Expand Up @@ -421,17 +399,10 @@ void NCPkgDiskspace::checkDiskSpaceRange( )
}
}

std::string NCPkgDiskspace::usedPercent( FSize used, FSize total )
std::string NCPkgDiskspace::usedPercent( unsigned long long used, unsigned long long total )
{
int percent = 0;
char percentStr[10];

if ( total != 0 )
percent = ( 100 * used ) / total;

sprintf( percentStr, "%d%%", percent );

return percentStr;
int percent = usedPercentInt(used, total);
return zypp::str::form( "%2d%%", percent );
}

///////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion src/NCPkgPopupDiskspace.h
Expand Up @@ -204,7 +204,7 @@ class NCPkgDiskspace {
NCPkgPopupDiskspace *popupWin;
ZyppDuSet testDiskUsage;

std::string usedPercent( FSize used, FSize total );
std::string usedPercent( unsigned long long used, unsigned long long total );

/**
* Warning range notifier about running out of disk space warning.
Expand Down

0 comments on commit 0fb3694

Please sign in to comment.