1 /* === This file is part of Calamares - <https://calamares.io> ===
2  *
3  *   SPDX-FileCopyrightText: 2014 Aurélien Gâteau <agateau@kde.org>
4  *   SPDX-FileCopyrightText: 2018 Adriaan de Groot <groot@kde.org>
5  *   SPDX-License-Identifier: GPL-3.0-or-later
6  *
7  *   Calamares is Free Software: see the License-Identifier above.
8  *
9  */
10 
11 #include "core/PartitionInfo.h"
12 
13 // KPMcore
14 #include <kpmcore/core/lvmdevice.h>
15 #include <kpmcore/core/partition.h>
16 
17 // Qt
18 #include <QVariant>
19 
20 namespace PartitionInfo
21 {
22 
23 static const char MOUNT_POINT_PROPERTY[] = "_calamares_mountPoint";
24 static const char FORMAT_PROPERTY[] = "_calamares_format";
25 static const char FLAGS_PROPERTY[] = "_calamares_flags";
26 
27 QString
mountPoint(Partition * partition)28 mountPoint( Partition* partition )
29 {
30     return partition->property( MOUNT_POINT_PROPERTY ).toString();
31 }
32 
33 void
setMountPoint(Partition * partition,const QString & value)34 setMountPoint( Partition* partition, const QString& value )
35 {
36     partition->setProperty( MOUNT_POINT_PROPERTY, value );
37 }
38 
39 bool
format(Partition * partition)40 format( Partition* partition )
41 {
42     return partition->property( FORMAT_PROPERTY ).toBool();
43 }
44 
45 void
setFormat(Partition * partition,bool value)46 setFormat( Partition* partition, bool value )
47 {
48     partition->setProperty( FORMAT_PROPERTY, value );
49 }
50 
51 PartitionTable::Flags
flags(const Partition * partition)52 flags( const Partition* partition )
53 {
54     auto v = partition->property( FLAGS_PROPERTY );
55     if ( !v.isValid() )
56     {
57         return partition->activeFlags();
58     }
59     // The underlying type of PartitionTable::Flags can be int or uint
60     // (see qflags.h) and so setting those flags can create a QVariant
61     // of those types; we don't just want to check QVariant::canConvert()
62     // here because that will also accept QByteArray and some other things.
63     if ( v.type() == QVariant::Int || v.type() == QVariant::UInt )
64     {
65         return static_cast< PartitionTable::Flags >( v.toInt() );
66     }
67     return partition->activeFlags();
68 }
69 
70 void
setFlags(Partition * partition,PartitionTable::Flags f)71 setFlags( Partition* partition, PartitionTable::Flags f )
72 {
73     partition->setProperty( FLAGS_PROPERTY, PartitionTable::Flags::Int( f ) );
74 }
75 
76 void
reset(Partition * partition)77 reset( Partition* partition )
78 {
79     partition->setProperty( MOUNT_POINT_PROPERTY, QVariant() );
80     partition->setProperty( FORMAT_PROPERTY, QVariant() );
81     partition->setProperty( FLAGS_PROPERTY, QVariant() );
82 }
83 
84 bool
isDirty(Partition * partition)85 isDirty( Partition* partition )
86 {
87     if ( LvmDevice::s_DirtyPVs.contains( partition ) )
88     {
89         return true;
90     }
91 
92     return !mountPoint( partition ).isEmpty() || format( partition ) || flags( partition ) != partition->activeFlags();
93 }
94 
95 }  // namespace PartitionInfo
96