use libblkid api and not external process invokations to read FS labels

svn path=/trunk/extragear/sysadmin/partitionmanager/; revision=1073145
This commit is contained in:
Volker Lanz 2010-01-11 18:14:16 +00:00
parent 26e7f9d7ef
commit 016addec2e
18 changed files with 73 additions and 135 deletions

View File

@ -226,7 +226,9 @@ static void scanDevicePartitions(PedDevice* pedDevice, Device& d, PedDisk* pedDi
readSectorsUsed(pedDisk, *part, mountInfo);
if (fs->supportGetLabel() == FileSystem::SupportExternal)
if (fs->supportGetLabel() == FileSystem::SupportInternal)
fs->setLabel(FileSystem::readLabelInternal(part->deviceNode()));
else if (fs->supportGetLabel() == FileSystem::SupportExternal)
fs->setLabel(fs->readLabel(part->deviceNode()));
if (fs->supportGetUUID() == FileSystem::SupportExternal)
@ -278,7 +280,7 @@ void LibParted::scanDevices(OperationStack& ostack)
{
QRegExp rxLine("\\s*(\\d+)\\s+(\\d+)\\s+(\\d+)\\s([^0-9]+)\\s+");
QByteArray line;
while (!(line = partitions.readLine()).isEmpty())
{
if (rxLine.indexIn(line) != -1)

View File

@ -48,7 +48,8 @@ namespace FS
void ext2::init()
{
m_GetUsed = findExternal("dumpe2fs") ? SupportExternal : SupportNone;
m_SetLabel = m_GetLabel = findExternal("e2label") ? SupportExternal : SupportNone;
m_GetLabel = SupportInternal;
m_SetLabel = findExternal("e2label") ? SupportExternal : SupportNone;
m_Create = findExternal("mkfs.ext2") ? SupportExternal : SupportNone;
m_Check = findExternal("e2fsck", QStringList() << "-V") ? SupportExternal : SupportNone;
m_UpdateUUID = findExternal("tune2fs") ? SupportExternal : SupportNone;
@ -96,12 +97,6 @@ namespace FS
return -1;
}
QString ext2::readLabel(const QString& deviceNode) const
{
ExternalCommand cmd("e2label", QStringList() << deviceNode);
return cmd.run() ? cmd.output().simplified() : QString();
}
bool ext2::check(Report& report, const QString& deviceNode) const
{
ExternalCommand cmd(report, "e2fsck", QStringList() << "-f" << "-y" << "-v" << deviceNode);

View File

@ -43,7 +43,6 @@ namespace FS
static void init();
virtual qint64 readUsedCapacity(const QString& deviceNode) const;
virtual QString readLabel(const QString& deviceNode) const;
virtual bool check(Report& report, const QString& deviceNode) const;
virtual bool create(Report& report, const QString& deviceNode) const;
virtual bool resize(Report& report, const QString& deviceNode, qint64 length) const;

View File

@ -22,6 +22,7 @@
#include "util/externalcommand.h"
#include "util/capacity.h"
#include <blkid/blkid.h>
#include <klocale.h>
#include <kdebug.h>
@ -74,12 +75,9 @@ static QString invokeIdUtil(const QString& util, const QString& deviceNode, cons
*/
QString FileSystem::readLabel(const QString& deviceNode) const
{
QString rval = invokeIdUtil("vol_id", deviceNode, "ID_FS_LABEL=(\\w+)");
Q_UNUSED(deviceNode);
if (rval.isEmpty())
rval = invokeIdUtil("blkid", deviceNode, "LABEL=\"(\\w+)\"");
return rval;
return QString();
}
/** Creates a new FileSystem
@ -339,6 +337,33 @@ bool FileSystem::unmount(const QString& mountPoint)
return false;
}
/** Reads the label for a device's FileSystem
@param deviceNode the device node for the Partition the FileSystem is on
@return the FileSystem label or an empty string in case of error
*/
QString FileSystem::readLabelInternal(const QString& deviceNode)
{
blkid_cache cache;
QString rval;
if (blkid_get_cache(&cache, NULL) == 0)
{
blkid_dev dev;
char* label = NULL;
if ((dev = blkid_get_dev(cache, deviceNode.toLocal8Bit(), BLKID_DEV_NORMAL)) != NULL &&
(label = blkid_get_tag_value(cache, "LABEL", deviceNode.toLocal8Bit())))
{
rval = label;
free(label);
}
blkid_put_cache(cache);
}
return rval;
}
bool FileSystem::findExternal(const QString& cmdName, const QStringList& args, int expectedCode)
{
ExternalCommand cmd(cmdName, args);

View File

@ -145,6 +145,9 @@ class FileSystem
void setLabel(const QString& s) { m_Label = s; } /**< @param s the new label */
void setUUID(const QString& s) { m_UUID = s; } /**< @param s the new UUID */
public:
static QString readLabelInternal(const QString& deviceNode);
protected:
static bool findExternal(const QString& cmdName, const QStringList& args = QStringList(), int exptectedCode = 1);
static bool findIdUtil();

View File

@ -43,8 +43,9 @@ namespace FS
void hfs::init()
{
m_GetLabel = SupportInternal;
m_Create = findExternal("hformat") ? SupportExternal : SupportNone;
m_Check = m_GetLabel = findExternal("hfsck") ? SupportExternal : SupportNone;
m_Check = findExternal("hfsck") ? SupportExternal : SupportNone;
m_GetUsed = SupportLibParted;
m_Shrink = SupportLibParted;
@ -57,21 +58,6 @@ namespace FS
{
return 2 * Capacity::unitFactor(Capacity::Byte, Capacity::TiB);
}
QString hfs::readLabel(const QString& deviceNode) const
{
ExternalCommand cmd("hfsck", QStringList() << "-v" << deviceNode);
if (cmd.run())
{
QRegExp rxVolumeName("drVN\\s*= \"(\\w+)\"");
if (rxVolumeName.indexIn(cmd.output()) != -1)
return rxVolumeName.cap(1);
}
return QString();
}
bool hfs::check(Report& report, const QString& deviceNode) const
{

View File

@ -42,7 +42,6 @@ namespace FS
public:
static void init();
virtual QString readLabel(const QString& deviceNode) const;
virtual bool check(Report& report, const QString& deviceNode) const;
virtual bool create(Report& report, const QString& deviceNode) const;
@ -54,9 +53,9 @@ namespace FS
virtual SupportType supportCheck() const { return m_Check; }
virtual SupportType supportCopy() const { return m_Copy; }
virtual SupportType supportBackup() const { return m_Backup; }
virtual qint64 maxCapacity() const;
protected:
static SupportType m_GetUsed;
static SupportType m_GetLabel;

View File

@ -51,7 +51,8 @@ namespace FS
void jfs::init()
{
m_GetUsed = findExternal("jfs_debugfs") ? SupportExternal : SupportNone;
m_SetLabel = m_GetLabel = findExternal("jfs_tune", QStringList() << "-V") ? SupportExternal : SupportNone;
m_GetLabel = SupportInternal;
m_SetLabel = findExternal("jfs_tune", QStringList() << "-V") ? SupportExternal : SupportNone;
m_Create = findExternal("mkfs.jfs", QStringList() << "-V") ? SupportExternal : SupportNone;
m_Grow = m_Check = findExternal("fsck.jfs", QStringList() << "-V") ? SupportExternal : SupportNone;
m_Copy = m_Move = (m_Check != SupportNone) ? SupportInternal : SupportNone;
@ -62,7 +63,7 @@ namespace FS
{
return 16 * Capacity::unitFactor(Capacity::Byte, Capacity::MiB);
}
qint64 jfs::readUsedCapacity(const QString& deviceNode) const
{
ExternalCommand cmd("jfs_debugfs", QStringList() << deviceNode);
@ -85,7 +86,7 @@ namespace FS
if (!ok)
nBlocks = -1;
}
qint64 nFree = -1;
QRegExp rxnFree("dn_nfree:\\s+0x([0-9a-f]+)");
@ -103,21 +104,6 @@ namespace FS
return -1;
}
QString jfs::readLabel(const QString& deviceNode) const
{
ExternalCommand cmd("jfs_tune", QStringList() << "-l" << deviceNode);
if (cmd.run())
{
QRegExp rxLabel("Volume label:\\s+'(\\w+)'");
if (rxLabel.indexIn(cmd.output()) != -1)
return rxLabel.cap(1).simplified();
}
return QString();
}
bool jfs::writeLabel(Report& report, const QString& deviceNode, const QString& newLabel)
{
return ExternalCommand(report, "jfs_tune", QStringList() << "-L" << newLabel << deviceNode).run(-1);
@ -133,7 +119,7 @@ namespace FS
{
return ExternalCommand(report, "mkfs.jfs", QStringList() << "-q" << deviceNode).run(-1);
}
bool jfs::resize(Report& report, const QString& deviceNode, qint64) const
{
KTempDir tempDir;
@ -146,16 +132,16 @@ namespace FS
bool rval = false;
ExternalCommand mountCmd(report, "mount", QStringList() << "-v" << "-t" << "jfs" << deviceNode << tempDir.name());
if (mountCmd.run(-1))
{
ExternalCommand resizeMountCmd(report, "mount", QStringList() << "-v" << "-t" << "jfs" << "-o" << "remount,resize" << deviceNode << tempDir.name());
if (resizeMountCmd.run(-1))
rval = true;
else
report.line() << i18nc("@info/plain", "Resizing JFS file system on partition <filename>%1</filename> failed: Remount failed.", deviceNode);
ExternalCommand unmountCmd(report, "umount", QStringList() << tempDir.name());
if (!unmountCmd.run(-1))
@ -163,7 +149,7 @@ namespace FS
}
else
report.line() << i18nc("@info/plain", "Resizing JFS file system on partition <filename>%1</filename> failed: Initial mount failed.", deviceNode);
return rval;
}
}

View File

@ -43,7 +43,6 @@ namespace FS
static void init();
virtual qint64 readUsedCapacity(const QString& deviceNode) const;
virtual QString readLabel(const QString& deviceNode) const;
virtual bool check(Report& report, const QString& deviceNode) const;
virtual bool create(Report& report, const QString& deviceNode) const;
virtual bool resize(Report& report, const QString& deviceNode, qint64 length) const;
@ -58,9 +57,9 @@ namespace FS
virtual SupportType supportCopy() const { return m_Copy; }
virtual SupportType supportBackup() const { return m_Backup; }
virtual SupportType supportSetLabel() const { return m_SetLabel; }
virtual qint64 minCapacity() const;
protected:
static SupportType m_GetUsed;
static SupportType m_GetLabel;

View File

@ -42,7 +42,7 @@ namespace FS
void linuxswap::init()
{
m_SetLabel = m_Shrink = m_Grow = m_Create = (findExternal("mkswap")) ? SupportExternal : SupportNone;
m_GetLabel = findIdUtil() ? SupportExternal : SupportNone;
m_GetLabel = SupportInternal;
m_Copy = SupportInternal;
m_Move = SupportInternal;
m_GetUUID = findIdUtil() ? SupportExternal : SupportNone;
@ -55,7 +55,7 @@ namespace FS
bool linuxswap::resize(Report& report, const QString& deviceNode, qint64) const
{
const QString label = readLabel(deviceNode);
const QString label = readLabelInternal(deviceNode);
QStringList args;
if (!label.isEmpty())

View File

@ -58,7 +58,8 @@ namespace FS
void ntfs::init()
{
m_Shrink = m_Grow = m_Check = m_GetUsed = findExternal("ntfsresize") ? SupportExternal : SupportNone;
m_SetLabel = m_GetLabel = findExternal("ntfslabel") ? SupportExternal : SupportNone;
m_GetLabel = SupportInternal;
m_SetLabel = findExternal("ntfslabel") ? SupportExternal : SupportNone;
m_Create = findExternal("mkfs.ntfs") ? SupportExternal : SupportNone;
m_Copy = findExternal("ntfsclone") ? SupportExternal : SupportNone;
m_Backup = SupportInternal;
@ -91,17 +92,6 @@ namespace FS
return -1;
}
QString ntfs::readLabel(const QString& deviceNode) const
{
ExternalCommand cmd("ntfslabel", QStringList() << "--force" << deviceNode);
// ntfslabel writes some warning message to stderr when the filesystem is marked as
// dirty. we don't want that message, so ignore stderr.
cmd.setProcessChannelMode(QProcess::SeparateChannels);
return cmd.run() ? cmd.output().simplified() : QString();
}
bool ntfs::writeLabel(Report& report, const QString& deviceNode, const QString& newLabel)
{
ExternalCommand writeCmd(report, "ntfslabel", QStringList() << "--force" << deviceNode << newLabel.simplified());

View File

@ -43,7 +43,6 @@ namespace FS
static void init();
virtual qint64 readUsedCapacity(const QString& deviceNode) const;
virtual QString readLabel(const QString& deviceNode) const;
virtual bool check(Report& report, const QString& deviceNode) const;
virtual bool create(Report& report, const QString& deviceNode) const;
virtual bool copy(Report& report, const QString& targetDeviceNode, const QString& sourceDeviceNode) const;

View File

@ -41,7 +41,8 @@ namespace FS
void reiser4::init()
{
m_GetUsed = m_GetLabel = findExternal("debugfs.reiser4", QStringList(), 16) ? SupportExternal : SupportNone;
m_GetLabel = SupportInternal;
m_GetUsed = findExternal("debugfs.reiser4", QStringList(), 16) ? SupportExternal : SupportNone;
m_Create = findExternal("mkfs.reiser4", QStringList(), 16) ? SupportExternal : SupportNone;
m_Check = findExternal("fsck.reiser4", QStringList(), 16) ? SupportExternal : SupportNone;
m_Move = m_Copy = (m_Check != SupportNone) ? SupportInternal : SupportNone;
@ -59,7 +60,7 @@ namespace FS
if (rxBlocks.indexIn(cmd.output()) != -1)
blocks = rxBlocks.cap(1).toLongLong();
qint64 blockSize = -1;
QRegExp rxBlockSize("blksize:\\s+(\\d+)");
@ -79,21 +80,6 @@ namespace FS
return -1;
}
QString reiser4::readLabel(const QString& deviceNode) const
{
ExternalCommand cmd("debugfs.reiser4", QStringList() << deviceNode);
if (cmd.run())
{
QRegExp rxLabel("label:\\s+(<?\\w+>?)");
if (rxLabel.indexIn(cmd.output()) != -1 && rxLabel.cap(1) != "<none>")
return rxLabel.cap(1);
}
return QString();
}
bool reiser4::check(Report& report, const QString& deviceNode) const
{
ExternalCommand cmd(report, "fsck.reiser4", QStringList() << "--fix" << "-y" << deviceNode);

View File

@ -43,7 +43,6 @@ namespace FS
static void init();
virtual qint64 readUsedCapacity(const QString& deviceNode) const;
virtual QString readLabel(const QString& deviceNode) const;
virtual bool check(Report& report, const QString& deviceNode) const;
virtual bool create(Report& report, const QString& deviceNode) const;
@ -54,7 +53,7 @@ namespace FS
virtual SupportType supportCheck() const { return m_Check; }
virtual SupportType supportCopy() const { return m_Copy; }
virtual SupportType supportBackup() const { return m_Backup; }
protected:
static SupportType m_GetUsed;
static SupportType m_GetLabel;

View File

@ -49,7 +49,8 @@ namespace FS
void reiserfs::init()
{
m_GetLabel = m_GetUsed = findExternal("debugreiserfs", QStringList(), 16) ? SupportExternal : SupportNone;
m_GetLabel = SupportInternal;
m_GetUsed = findExternal("debugreiserfs", QStringList(), 16) ? SupportExternal : SupportNone;
m_SetLabel = findExternal("reiserfstune") ? SupportExternal : SupportNone;
m_Create = findExternal("mkfs.reiserfs") ? SupportExternal : SupportNone;
m_Check = findExternal("fsck.reiserfs") ? SupportExternal : SupportNone;
@ -102,21 +103,6 @@ namespace FS
return -1;
}
QString reiserfs::readLabel(const QString& deviceNode) const
{
ExternalCommand cmd("debugreiserfs", QStringList() << deviceNode);
if (cmd.run())
{
QRegExp rxLabel("LABEL: (\\w+)");
if (rxLabel.indexIn(cmd.output()) != -1)
return rxLabel.cap(1).simplified();
}
return QString();
}
bool reiserfs::writeLabel(Report& report, const QString& deviceNode, const QString& newLabel)
{
return ExternalCommand(report, "reiserfstune", QStringList() << "-l" << newLabel << deviceNode).run(-1);

View File

@ -45,7 +45,6 @@ namespace FS
static void init();
virtual qint64 readUsedCapacity(const QString& deviceNode) const;
virtual QString readLabel(const QString& deviceNode) const;
virtual bool check(Report& report, const QString& deviceNode) const;
virtual bool create(Report& report, const QString& deviceNode) const;
virtual bool resize(Report& report, const QString& deviceNode, qint64 length) const;

View File

@ -51,7 +51,8 @@ namespace FS
void xfs::init()
{
m_SetLabel = m_GetLabel = m_GetUsed = findExternal("xfs_db") ? SupportExternal : SupportNone;
m_GetLabel = SupportInternal;
m_SetLabel = m_GetUsed = findExternal("xfs_db") ? SupportExternal : SupportNone;
m_Create = findExternal("mkfs.xfs") ? SupportExternal : SupportNone;
m_Check = findExternal("xfs_repair") ? SupportExternal : SupportNone;
@ -65,7 +66,7 @@ namespace FS
{
return 32 * Capacity::unitFactor(Capacity::Byte, Capacity::MiB);
}
qint64 xfs::readUsedCapacity(const QString& deviceNode) const
{
ExternalCommand cmd("xfs_db", QStringList() << "-c" << "sb 0" << "-c" << "print" << deviceNode);
@ -97,21 +98,6 @@ namespace FS
return -1;
}
QString xfs::readLabel(const QString& deviceNode) const
{
ExternalCommand cmd("xfs_db", QStringList() << "-c" << "sb 0" << "-c" << "label" << deviceNode);
if (cmd.run())
{
QRegExp rxLabel("label = \"(\\w+)\"");
if (rxLabel.indexIn(cmd.output()) != -1)
return rxLabel.cap(1);
}
return QString();
}
bool xfs::writeLabel(Report& report, const QString& deviceNode, const QString& newLabel)
{
return ExternalCommand(report, "xfs_db", QStringList() << "-x" << "-c" << "sb 0" << "-c" << QString("label " + newLabel) << deviceNode).run(-1);
@ -153,16 +139,16 @@ namespace FS
bool rval = false;
ExternalCommand mountCmd(report, "mount", QStringList() << "-v" << "-t" << "xfs" << deviceNode << tempDir.name());
if (mountCmd.run(-1))
{
ExternalCommand resizeCmd(report, "xfs_growfs", QStringList() << tempDir.name());
if (resizeCmd.run(-1))
rval = true;
else
report.line() << i18nc("@info/plain", "Resizing XFS file system on partition <filename>%1</filename> failed: xfs_growfs failed.", deviceNode);
ExternalCommand unmountCmd(report, "umount", QStringList() << tempDir.name());
if (!unmountCmd.run(-1))
@ -170,7 +156,7 @@ namespace FS
}
else
report.line() << i18nc("@info/plain", "Resizing XFS file system on partition <filename>%1</filename> failed: Initial mount failed.", deviceNode);
return rval;
}
}

View File

@ -43,7 +43,6 @@ namespace FS
static void init();
virtual qint64 readUsedCapacity(const QString& deviceNode) const;
virtual QString readLabel(const QString& deviceNode) const;
virtual bool check(Report& report, const QString& deviceNode) const;
virtual bool create(Report& report, const QString& deviceNode) const;
virtual bool copy(Report& report, const QString&, const QString&) const;
@ -59,9 +58,9 @@ namespace FS
virtual SupportType supportCopy() const { return m_Copy; }
virtual SupportType supportBackup() const { return m_Backup; }
virtual SupportType supportSetLabel() const { return m_SetLabel; }
virtual qint64 minCapacity() const;
protected:
static SupportType m_GetUsed;
static SupportType m_GetLabel;