make first and last usable sector methods static to libparted, no one else is

using them

move the code used to map libparted partition flags to our own flags from the
job to LibParted

svn path=/trunk/extragear/sysadmin/partitionmanager/; revision=1090002
This commit is contained in:
Volker Lanz 2010-02-14 12:35:01 +00:00
parent bfbe3eed58
commit 95ace6b7d3
4 changed files with 91 additions and 82 deletions

View File

@ -43,6 +43,21 @@
#include <parted/parted.h>
#include <unistd.h>
static const LibParted::FlagMap flagmap[] =
{
{ PED_PARTITION_BOOT, PartitionTable::FlagBoot },
{ PED_PARTITION_ROOT, PartitionTable::FlagRoot },
{ PED_PARTITION_SWAP, PartitionTable::FlagSwap },
{ PED_PARTITION_HIDDEN, PartitionTable::FlagHidden },
{ PED_PARTITION_RAID, PartitionTable::FlagRaid },
{ PED_PARTITION_LVM, PartitionTable::FlagLvm },
{ PED_PARTITION_LBA, PartitionTable::FlagLba },
{ PED_PARTITION_HPSERVICE, PartitionTable::FlagHpService },
{ PED_PARTITION_PALO, PartitionTable::FlagPalo },
{ PED_PARTITION_PREP, PartitionTable::FlagPrep },
{ PED_PARTITION_MSFT_RESERVED, PartitionTable::FlagMsftReserved }
};
/** Callback to handle exceptions from libparted
@param e the libparted exception to handle
*/
@ -86,7 +101,7 @@ typedef struct _GPTDiskData GPTDiskData;
@param d the Device in question
@return the first sector usable by a Partition
*/
quint64 LibParted::firstUsableSector(const Device& d)
static quint64 firstUsableSector(const Device& d)
{
PedDevice* pedDevice = ped_device_get(d.deviceNode().toAscii());
PedDisk* pedDisk = pedDevice ? ped_disk_new(pedDevice) : NULL;
@ -111,7 +126,7 @@ quint64 LibParted::firstUsableSector(const Device& d)
@param d the Device in question
@return the last sector usable by a Partition
*/
quint64 LibParted::lastUsableSector(const Device& d)
static quint64 lastUsableSector(const Device& d)
{
PedDevice* pedDevice = ped_device_get(d.deviceNode().toAscii());
PedDisk* pedDisk = pedDevice ? ped_disk_new(pedDevice) : NULL;
@ -183,6 +198,44 @@ static void readSectorsUsed(PedDisk* pedDisk, Partition& p, const QString& mount
p.fileSystem().setSectorsUsed(readSectorsUsedLibParted(pedDisk, p));
}
static PartitionTable::Flags activeFlags(PedPartition* p)
{
PartitionTable::Flags flags = PartitionTable::FlagNone;
// We might get here with a pedPartition just picked up from libparted that is
// unallocated. Libparted doesn't like it if we ask for flags for unallocated
// space.
if (p->num <= 0)
return flags;
for (quint32 i = 0; i < sizeof(flagmap) / sizeof(flagmap[0]); i++)
if (ped_partition_is_flag_available(p, flagmap[i].pedFlag) && ped_partition_get_flag(p, flagmap[i].pedFlag))
flags |= flagmap[i].flag;
return flags;
}
static PartitionTable::Flags availableFlags(PedPartition* p)
{
PartitionTable::Flags flags;
// see above.
if (p->num <= 0)
return flags;
for (quint32 i = 0; i < sizeof(flagmap) / sizeof(flagmap[0]); i++)
if (ped_partition_is_flag_available(p, flagmap[i].pedFlag))
{
// workaround:: see above
if (p->type != PED_PARTITION_EXTENDED || flagmap[i].flag != PartitionTable::FlagHidden)
flags |= flagmap[i].flag;
}
return flags;
}
/** Scans a Device for Partitions.
This function will scan a Device for all Partitions on it, detect the FileSystem for each Partition,
@ -243,9 +296,7 @@ static void scanDevicePartitions(PedDevice* pedDevice, Device& d, PedDisk* pedDi
const QString mountPoint = mountPoints.findByDevice(node) ? mountPoints.findByDevice(node)->mountPoint() : QString();
Partition* part = new Partition(parent, d, PartitionRole(r), fs, pedPartition->geom.start, pedPartition->geom.end,
pedPartition->num, SetPartFlagsJob::availableFlags(pedPartition),
QStringList() << mountPoint, ped_partition_is_busy(pedPartition), SetPartFlagsJob::activeFlags(pedPartition));
Partition* part = new Partition(parent, d, PartitionRole(r), fs, pedPartition->geom.start, pedPartition->geom.end, pedPartition->num, availableFlags(pedPartition), QStringList() << mountPoint, ped_partition_is_busy(pedPartition), activeFlags(pedPartition));
readSectorsUsed(pedDisk, *part, mountPoint);
@ -274,6 +325,14 @@ LibParted::LibParted()
ped_exception_set_handler(pedExceptionHandler);
}
/** Return a map of partition flags from libparted flags to PartitionTable::Flags
@return the map
*/
const LibParted::FlagMap* LibParted::flagMap()
{
return flagmap;
}
/** Create a Device for the given device_node and scan it for partitions.
@param device_node the device node (e.g. "/dev/sda")
@return the created Device object. callers need to free this.

View File

@ -21,16 +21,17 @@
#define LIBPARTED__H
#include "core/partitiontable.h"
#include <parted/parted.h>
#include <qglobal.h>
class OperationStack;
class Device;
class QString;
/** @brief Scanning for Devices.
Encapsulates Device scanning done by libparted.
/** @brief Device scanning done by libparted.
More libparted-related stuff is in the @link Job Job-derived @endlink classes.
@ -40,13 +41,19 @@ class LibParted
{
Q_DISABLE_COPY(LibParted)
public:
typedef struct
{
PedPartitionFlag pedFlag;
PartitionTable::Flag flag;
} FlagMap;
public:
LibParted();
public:
static quint64 firstUsableSector(const Device& d);
static quint64 lastUsableSector(const Device& d);
static Device* scanDevice(const QString& device_node);
static const FlagMap* flagMap();
};
#endif

View File

@ -23,30 +23,12 @@
#include "core/partition.h"
#include "core/partitionrole.h"
#include "core/partitiontable.h"
#include "core/libparted.h"
#include "util/report.h"
#include <klocale.h>
static const struct
{
PedPartitionFlag pedFlag;
PartitionTable::Flag flag;
} flagmap[] =
{
{ PED_PARTITION_BOOT, PartitionTable::FlagBoot },
{ PED_PARTITION_ROOT, PartitionTable::FlagRoot },
{ PED_PARTITION_SWAP, PartitionTable::FlagSwap },
{ PED_PARTITION_HIDDEN, PartitionTable::FlagHidden },
{ PED_PARTITION_RAID, PartitionTable::FlagRaid },
{ PED_PARTITION_LVM, PartitionTable::FlagLvm },
{ PED_PARTITION_LBA, PartitionTable::FlagLba },
{ PED_PARTITION_HPSERVICE, PartitionTable::FlagHpService },
{ PED_PARTITION_PALO, PartitionTable::FlagPalo },
{ PED_PARTITION_PREP, PartitionTable::FlagPrep },
{ PED_PARTITION_MSFT_RESERVED, PartitionTable::FlagMsftReserved }
};
/** Creates a new SetPartFlagsJob
@param d the Device the Partition whose flags are to be set is on
@param p the Partition whose flags are to be set
@ -62,7 +44,7 @@ SetPartFlagsJob::SetPartFlagsJob(Device& d, Partition& p, PartitionTable::Flags
qint32 SetPartFlagsJob::numSteps() const
{
return sizeof(flagmap) / sizeof(flagmap[0]);
return sizeof(LibParted::flagMap()) / sizeof(LibParted::flagMap()[0]);
}
bool SetPartFlagsJob::run(Report& parent)
@ -70,50 +52,50 @@ bool SetPartFlagsJob::run(Report& parent)
bool rval = true;
Report* report = jobStarted(parent);
if (openPed(device().deviceNode()))
{
PedPartition* pedPartition = (partition().roles().has(PartitionRole::Extended)) ? ped_disk_extended_partition(pedDisk()) : ped_disk_get_partition_by_sector(pedDisk(), partition().firstSector());
if (pedPartition)
{
for (quint32 i = 0; i < sizeof(flagmap) / sizeof(flagmap[0]); i++)
for (quint32 i = 0; i < sizeof(LibParted::flagMap()) / sizeof(LibParted::flagMap()[0]); i++)
{
emit progress(i + 1);
if (!ped_partition_is_flag_available(pedPartition, flagmap[i].pedFlag))
if (!ped_partition_is_flag_available(pedPartition, LibParted::flagMap()[i].pedFlag))
{
report->line() << i18nc("@info/plain", "The flag \"%1\" is not available on the partition's partition table.", PartitionTable::flagName(flagmap[i].flag));
report->line() << i18nc("@info/plain", "The flag \"%1\" is not available on the partition's partition table.", PartitionTable::flagName(LibParted::flagMap()[i].flag));
continue;
}
// Workaround: libparted claims the hidden flag is available for extended partitions, but
// throws an error when we try to set or clear it. So skip this combination (also see below in
// availableFlags()).
if (pedPartition->type == PED_PARTITION_EXTENDED && flagmap[i].flag == PartitionTable::FlagHidden)
if (pedPartition->type == PED_PARTITION_EXTENDED && LibParted::flagMap()[i].flag == PartitionTable::FlagHidden)
continue;
int state = (flags() & flagmap[i].flag) ? 1 : 0;
int state = (flags() & LibParted::flagMap()[i].flag) ? 1 : 0;
if (!ped_partition_set_flag(pedPartition, flagmap[i].pedFlag, state))
if (!ped_partition_set_flag(pedPartition, LibParted::flagMap()[i].pedFlag, state))
{
report->line() << i18nc("@info/plain", "There was an error setting flag %1 for partition <filename>%2</filename> to state %3.", PartitionTable::flagName(flagmap[i].flag), partition().deviceNode(), state ? i18nc("@info flag turned on, active", "on") : i18nc("@info flag turned off, inactive", "off"));
report->line() << i18nc("@info/plain", "There was an error setting flag %1 for partition <filename>%2</filename> to state %3.", PartitionTable::flagName(LibParted::flagMap()[i].flag), partition().deviceNode(), state ? i18nc("@info flag turned on, active", "on") : i18nc("@info flag turned off, inactive", "off"));
rval = false;
}
}
if (!commit())
rval = false;
}
else
report->line() << i18nc("@info/plain", "Could not find partition <filename>%1</filename> on device <filename>%2</filename> to set partition flags.", partition().deviceNode(), device().deviceNode());
closePed();
}
else
report->line() << i18nc("@info/plain", "Could not open device <filename>%1</filename> to set partition flags for partition <filename>%2</filename>.", device().deviceNode(), partition().deviceNode());
if (rval)
partition().setFlags(flags());
@ -122,42 +104,6 @@ bool SetPartFlagsJob::run(Report& parent)
return rval;
}
PartitionTable::Flags SetPartFlagsJob::activeFlags(PedPartition* p)
{
PartitionTable::Flags flags = PartitionTable::FlagNone;
// We might get here with a pedPartition just picked up from libparted that is
// unallocated. Libparted doesn't like it if we ask for flags for unallocated
// space.
if (p->num <= 0)
return flags;
for (quint32 i = 0; i < sizeof(flagmap) / sizeof(flagmap[0]); i++)
if (ped_partition_is_flag_available(p, flagmap[i].pedFlag) && ped_partition_get_flag(p, flagmap[i].pedFlag))
flags |= flagmap[i].flag;
return flags;
}
PartitionTable::Flags SetPartFlagsJob::availableFlags(PedPartition* p)
{
PartitionTable::Flags flags;
// see above.
if (p->num <= 0)
return flags;
for (quint32 i = 0; i < sizeof(flagmap) / sizeof(flagmap[0]); i++)
if (ped_partition_is_flag_available(p, flagmap[i].pedFlag))
{
// workaround:: see above
if (p->type != PED_PARTITION_EXTENDED || flagmap[i].flag != PartitionTable::FlagHidden)
flags |= flagmap[i].flag;
}
return flags;
}
QString SetPartFlagsJob::description() const
{
if (PartitionTable::flagNames(flags()).size() == 0)

View File

@ -49,13 +49,10 @@ class SetPartFlagsJob : public Job
virtual qint32 numSteps() const;
virtual QString description() const;
static PartitionTable::Flags activeFlags(PedPartition* p);
static PartitionTable::Flags availableFlags(PedPartition* p);
protected:
Device& device() { return m_Device; }
const Device& device() const { return m_Device; }
Partition& partition() { return m_Partition; }
const Partition& partition() const { return m_Partition; }