[libcalamaresui] Improve sizeLimit handling in log upload

The log sizeLimit can be 0 (disable log upload) but that's
not so clear in the code. While here, tidy up and add
some comments to surprising bits.
This commit is contained in:
Adriaan de Groot 2021-09-15 13:21:39 +02:00
parent b587358b11
commit f49627f417

View File

@ -33,10 +33,16 @@ using namespace CalamaresUtils::Units;
STATICTEST QByteArray
logFileContents( const qint64 sizeLimitBytes )
{
if ( sizeLimitBytes != -1 )
if ( sizeLimitBytes > 0 )
{
cDebug() << "Log upload size limit was limited to" << sizeLimitBytes << "bytes";
}
if ( sizeLimitBytes == 0 )
{
cDebug() << "Log upload size is 0, upload disabled.";
return QByteArray();
}
const QString name = Logger::logFile();
QFile pasteSourceFile( name );
if ( !pasteSourceFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
@ -44,7 +50,7 @@ logFileContents( const qint64 sizeLimitBytes )
cWarning() << "Could not open log file" << name;
return QByteArray();
}
if ( sizeLimitBytes == -1 )
if ( sizeLimitBytes < 0 )
{
return pasteSourceFile.readAll();
}
@ -52,7 +58,7 @@ logFileContents( const qint64 sizeLimitBytes )
if ( fi.size() > sizeLimitBytes )
{
cDebug() << "Only last" << sizeLimitBytes << "bytes of log file (sized" << fi.size() << "bytes) uploaded";
fi.refresh();
fi.refresh(); // Because we just wrote to the file with that cDebug() ^^
pasteSourceFile.seek( fi.size() - sizeLimitBytes );
}
return pasteSourceFile.read( sizeLimitBytes );
@ -115,7 +121,7 @@ CalamaresUtils::Paste::doLogUpload( QObject* parent )
auto [ type, serverUrl, sizeLimitBytes ] = Calamares::Branding::instance()->uploadServer();
if ( !serverUrl.isValid() )
{
cWarning() << "Upload configure with invalid URL";
cWarning() << "Upload configured with invalid URL";
return QString();
}
if ( type == Calamares::Branding::UploadServerType::None )
@ -123,6 +129,12 @@ CalamaresUtils::Paste::doLogUpload( QObject* parent )
// Early return to avoid reading the log file
return QString();
}
if ( sizeLimitBytes == 0 )
{
// Suggests that it is un-set in the config file
cWarning() << "Upload configured to send 0 bytes";
return QString();
}
QByteArray pasteData = logFileContents( sizeLimitBytes );
if ( pasteData.isEmpty() )
@ -181,5 +193,5 @@ bool
CalamaresUtils::Paste::isEnabled()
{
auto [ type, serverUrl, sizeLimitBytes ] = Calamares::Branding::instance()->uploadServer();
return type != Calamares::Branding::UploadServerType::None;
return type != Calamares::Branding::UploadServerType::None && sizeLimitBytes != 0;
}