source: calamares/trunk/fuentes/src/modules/partition/core/DeviceList.cpp @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 16 months ago

sync with github

File size: 4.3 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2015-2016, Teo Mrnjavac <teo@kde.org>
4 *   Copyright 2018, Adriaan de Groot <groot@kde.org>
5 *
6 *   Calamares is free software: you can redistribute it and/or modify
7 *   it under the terms of the GNU General Public License as published by
8 *   the Free Software Foundation, either version 3 of the License, or
9 *   (at your option) any later version.
10 *
11 *   Calamares is distributed in the hope that it will be useful,
12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *   GNU General Public License for more details.
15 *
16 *   You should have received a copy of the GNU General Public License
17 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "DeviceList.h"
21
22#include "PartitionCoreModule.h"
23
24#include "core/DeviceModel.h"
25#include "core/KPMHelpers.h"
26#include "core/PartitionIterator.h"
27
28#include <kpmcore/backend/corebackend.h>
29#include <kpmcore/backend/corebackendmanager.h>
30#include <kpmcore/core/device.h>
31#include <kpmcore/core/partition.h>
32
33#include <utils/Logger.h>
34#include <JobQueue.h>
35#include <GlobalStorage.h>
36
37#include <QProcess>
38#include <QTemporaryDir>
39
40namespace PartUtils
41{
42
43/**
44 * Does the given @p device contain the root filesystem? This is true if
45 * the device contains a partition which is currently mounted at / .
46 */
47static bool
48hasRootPartition( Device* device )
49{
50    for ( auto it = PartitionIterator::begin( device ); it != PartitionIterator::end( device ); ++it )
51        if ( ( *it )->mountPoint() == "/" )
52            return true;
53    return false;
54}
55
56static bool
57isIso9660( const Device* device )
58{
59    QString path = device->deviceNode();
60    if ( path.isEmpty() )
61        return false;
62
63    QProcess blkid;
64    blkid.start( "blkid", { path } );
65    blkid.waitForFinished();
66    QString output = QString::fromLocal8Bit( blkid.readAllStandardOutput() );
67    if ( output.contains( "iso9660" ) )
68        return true;
69
70    if ( device->partitionTable() &&
71            !device->partitionTable()->children().isEmpty() )
72    {
73        for ( const Partition* partition : device->partitionTable()->children() )
74        {
75            path = partition->partitionPath();
76            blkid.start( "blkid", { path } );
77            blkid.waitForFinished();
78            QString output = QString::fromLocal8Bit( blkid.readAllStandardOutput() );
79            if ( output.contains( "iso9660" ) )
80                return true;
81        }
82    }
83    return false;
84}
85
86
87static inline QDebug&
88operator <<( QDebug& s, QList< Device* >::iterator& it )
89{
90    s << ( ( *it ) ? ( *it )->deviceNode() : QString( "<null device>" ) );
91    return s;
92}
93
94using DeviceList = QList< Device* >;
95
96static inline DeviceList::iterator
97erase(DeviceList& l, DeviceList::iterator& it)
98{
99    Device* p = *it;
100    auto r = l.erase( it );
101    if (p)
102        delete p;
103    return r;
104}
105
106QList< Device* > getDevices( DeviceType which, qint64 minimumSize )
107{
108    bool writableOnly = (which == DeviceType::WritableOnly);
109
110    CoreBackend* backend = CoreBackendManager::self()->backend();
111    DeviceList devices = backend->scanDevices( true );
112
113    cDebug() << "Removing unsuitable devices:" << devices.count() << "candidates.";
114
115    // Remove the device which contains / from the list
116    for ( DeviceList::iterator it = devices.begin(); it != devices.end(); )
117        if ( ! ( *it ) ||
118                ( *it )->deviceNode().startsWith( "/dev/zram" )
119        )
120        {
121            cDebug() << "  .. Removing zram" << it;
122            it = erase(devices, it );
123
124        }
125        else if ( writableOnly && hasRootPartition( *it ) )
126        {
127            cDebug() << "  .. Removing device with root filesystem (/) on it" << it;
128            it = erase(devices, it );
129        }
130        else if ( writableOnly && isIso9660( *it ) )
131        {
132            cDebug() << "  .. Removing device with iso9660 filesystem (probably a CD) on it" << it;
133            it = erase(devices, it );
134        }
135        else if ( (minimumSize >= 0) && !( (*it)->capacity() > minimumSize ) )
136        {
137            cDebug() << "  .. Removing too-small" << it;
138            it = erase(devices, it );
139        }
140        else
141            ++it;
142
143    return devices;
144}
145
146}  // namespace PartUtils
Note: See TracBrowser for help on using the repository browser.