From 2f782f18c4f29719aa7483904274c048ea24f9ff Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 1 Mar 2022 16:10:19 +0100 Subject: [PATCH] [libcalamares] Fix file reading - atEnd() doesn't behave as expected - drop the textstream, not needed - rename variables to be more descriptive --- src/libcalamares/partition/Mount.cpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/libcalamares/partition/Mount.cpp b/src/libcalamares/partition/Mount.cpp index 6bc3a7cd2..a0cbf3a4a 100644 --- a/src/libcalamares/partition/Mount.cpp +++ b/src/libcalamares/partition/Mount.cpp @@ -128,26 +128,32 @@ QList< MtabInfo > MtabInfo::fromMtabFilteredByPrefix( const QString& mountPrefix, const QString& mtabPath ) { QFile f( mtabPath.isEmpty() ? "/etc/mtab" : mtabPath ); - if ( !f.open( QIODevice::ReadOnly ) ) + if ( !f.open( QIODevice::ReadOnly | QIODevice::Text ) ) { return {}; } - QTextStream in( &f ); QList< MtabInfo > l; - while ( !f.atEnd() ) + // After opening, atEnd() is already true (!?) so try reading at least once + do { - QStringList line = in.readLine().split( ' ', SplitSkipEmptyParts ); - if ( line.length() == 3 && !line[ 0 ].startsWith( '#' ) ) + QString line = f.readLine(); + if ( line.isEmpty() || line.startsWith( '#' ) ) + { + continue; + } + + QStringList parts = line.split( ' ', SplitSkipEmptyParts ); + if ( parts.length() == 3 && !parts[ 0 ].startsWith( '#' ) ) { // Lines have format: , so check // the mountpoint field. Everything starts with an empty string. - if ( line[ 1 ].startsWith( mountPrefix ) ) + if ( parts[ 1 ].startsWith( mountPrefix ) ) { - l.append( { line[ 0 ], line[ 1 ] } ); + l.append( { parts[ 0 ], parts[ 1 ] } ); } } - } + } while ( !f.atEnd() ); return l; }